Skip to content

Commit ecb0381

Browse files
tytan652RytoEX
authored andcommitted
obs-ffmpeg: Fix deprecation with FFmpeg 7.1
1 parent 099bc26 commit ecb0381

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

plugins/obs-ffmpeg/obs-ffmpeg-audio-encoders.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,24 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder, const char
236236

237237
enc->context->sample_rate = audio_output_get_sample_rate(audio);
238238

239-
if (enc->codec->sample_fmts) {
239+
const enum AVSampleFormat *sample_fmts = NULL;
240+
const int *supported_samplerates = NULL;
241+
242+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 13, 100)
243+
sample_fmts = enc->codec->sample_fmts;
244+
supported_samplerates = enc->codec->supported_samplerates;
245+
#else
246+
avcodec_get_supported_config(enc->context, enc->codec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0,
247+
(const void **)&sample_fmts, NULL);
248+
avcodec_get_supported_config(enc->context, enc->codec, AV_CODEC_CONFIG_SAMPLE_RATE, 0,
249+
(const void **)&supported_samplerates, NULL);
250+
#endif
251+
252+
if (sample_fmts) {
240253
/* Check if the requested format is actually available for the specified
241254
* encoder. This may not always be the case due to FFmpeg changes or a
242255
* fallback being used (for example, when libopus is unavailable). */
243-
const enum AVSampleFormat *fmt = enc->codec->sample_fmts;
256+
const enum AVSampleFormat *fmt = sample_fmts;
244257
while (*fmt != AV_SAMPLE_FMT_NONE) {
245258
if (*fmt == sample_format) {
246259
enc->context->sample_fmt = *fmt;
@@ -251,15 +264,15 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder, const char
251264

252265
/* Fall back to default if requested format was not found. */
253266
if (enc->context->sample_fmt == AV_SAMPLE_FMT_NONE)
254-
enc->context->sample_fmt = enc->codec->sample_fmts[0];
267+
enc->context->sample_fmt = sample_fmts[0];
255268
} else {
256269
/* Fall back to planar float if codec does not specify formats. */
257270
enc->context->sample_fmt = AV_SAMPLE_FMT_FLTP;
258271
}
259272

260273
/* check to make sure sample rate is supported */
261-
if (enc->codec->supported_samplerates) {
262-
const int *rate = enc->codec->supported_samplerates;
274+
if (supported_samplerates) {
275+
const int *rate = supported_samplerates;
263276
int cur_rate = enc->context->sample_rate;
264277
int closest = 0;
265278

plugins/obs-ffmpeg/obs-ffmpeg-output.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static bool create_video_stream(struct ffmpeg_data *data)
170170
enum AVPixelFormat closest_format;
171171
AVCodecContext *context;
172172
struct obs_video_info ovi;
173+
const enum AVPixelFormat *pix_fmts = NULL;
173174

174175
if (!obs_get_video_info(&ovi)) {
175176
ffmpeg_log_error(LOG_WARNING, data, "No active video");
@@ -180,28 +181,35 @@ static bool create_video_stream(struct ffmpeg_data *data)
180181
data->config.video_encoder))
181182
return false;
182183

183-
closest_format = data->config.format;
184-
if (data->vcodec->pix_fmts) {
185-
const int has_alpha = closest_format == AV_PIX_FMT_BGRA;
186-
closest_format =
187-
avcodec_find_best_pix_fmt_of_list(data->vcodec->pix_fmts, closest_format, has_alpha, NULL);
188-
}
189-
190184
context = avcodec_alloc_context3(data->vcodec);
191185
context->bit_rate = (int64_t)data->config.video_bitrate * 1000;
192186
context->width = data->config.scale_width;
193187
context->height = data->config.scale_height;
194188
context->time_base = (AVRational){ovi.fps_den, ovi.fps_num};
195189
context->framerate = (AVRational){ovi.fps_num, ovi.fps_den};
196190
context->gop_size = data->config.gop_size;
197-
context->pix_fmt = closest_format;
198191
context->color_range = data->config.color_range;
199192
context->color_primaries = data->config.color_primaries;
200193
context->color_trc = data->config.color_trc;
201194
context->colorspace = data->config.colorspace;
202-
context->chroma_sample_location = determine_chroma_location(closest_format, data->config.colorspace);
203195
context->thread_count = 0;
204196

197+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 13, 100)
198+
pix_fmts = data->vcodec->pix_fmts;
199+
#else
200+
avcodec_get_supported_config(context, data->vcodec, AV_CODEC_CONFIG_PIX_FORMAT, 0, (const void **)&pix_fmts,
201+
NULL);
202+
#endif
203+
204+
closest_format = data->config.format;
205+
if (pix_fmts) {
206+
const int has_alpha = closest_format == AV_PIX_FMT_BGRA;
207+
closest_format = avcodec_find_best_pix_fmt_of_list(pix_fmts, closest_format, has_alpha, NULL);
208+
}
209+
210+
context->pix_fmt = closest_format;
211+
context->chroma_sample_location = determine_chroma_location(closest_format, data->config.colorspace);
212+
205213
data->video->time_base = context->time_base;
206214
data->video->avg_frame_rate = (AVRational){ovi.fps_num, ovi.fps_den};
207215

@@ -305,6 +313,7 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
305313
AVStream *stream;
306314
struct obs_audio_info aoi;
307315
int channels;
316+
const enum AVSampleFormat *sample_fmts = NULL;
308317

309318
if (!obs_get_audio_info(&aoi)) {
310319
ffmpeg_log_error(LOG_WARNING, data, "No active audio");
@@ -324,7 +333,14 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
324333
if (aoi.speakers == SPEAKERS_4POINT1)
325334
context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
326335

327-
context->sample_fmt = data->acodec->sample_fmts ? data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
336+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 13, 100)
337+
sample_fmts = data->acodec->sample_fmts;
338+
#else
339+
avcodec_get_supported_config(context, data->acodec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0,
340+
(const void **)&sample_fmts, NULL);
341+
#endif
342+
343+
context->sample_fmt = sample_fmts ? sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
328344

329345
stream->time_base = context->time_base;
330346

0 commit comments

Comments
 (0)