Skip to content

Commit 4add774

Browse files
committed
Fix FFmpeg 8 audio filters cause distortion
See https://forum.shotcut.org/t/stereo-enhancer-filter-broke-at-some- point-after-25-05-11/50443
1 parent f923c26 commit 4add774

2 files changed

Lines changed: 87 additions & 8 deletions

File tree

src/modules/avformat/filter_avfilter.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct
5757
mlt_colorspace colorspace;
5858
int full_range;
5959
int reset;
60+
AVChannelLayout ch_layout;
6061
} private_data;
6162

6263
#if LIBAVUTIL_VERSION_INT >= ((56 << 16) + (35 << 8) + 101)
@@ -202,10 +203,8 @@ static void init_audio_filtergraph(mlt_filter filter,
202203
sample_rates[0] = frequency;
203204
channel_counts[0] = channels;
204205
#if HAVE_FFMPEG_CH_LAYOUT
205-
AVChannelLayout ch_layout;
206-
av_channel_layout_default(&ch_layout, channels);
207-
av_channel_layout_describe(&ch_layout, channel_layout_str, sizeof(channel_layout_str));
208-
av_channel_layout_uninit(&ch_layout);
206+
av_channel_layout_default(&pdata->ch_layout, channels);
207+
av_channel_layout_describe(&pdata->ch_layout, channel_layout_str, sizeof(channel_layout_str));
209208
#else
210209
int64_t channel_layouts[] = {-1, -1};
211210
channel_layouts[0] = av_get_default_channel_layout(channels);
@@ -280,6 +279,44 @@ static void init_audio_filtergraph(mlt_filter filter,
280279
mlt_log_error(filter, "Cannot create audio buffer sink\n");
281280
goto fail;
282281
}
282+
283+
#if LIBAVFILTER_VERSION_INT >= ((10 << 16) + (6 << 8) + 100)
284+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
285+
"sample_formats",
286+
AV_OPT_SEARCH_CHILDREN,
287+
0,
288+
1,
289+
AV_OPT_TYPE_SAMPLE_FMT,
290+
sample_fmts);
291+
if (ret < 0) {
292+
mlt_log_error(filter, "Cannot set sink sample formats\n");
293+
goto fail;
294+
}
295+
296+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
297+
"samplerates",
298+
AV_OPT_SEARCH_CHILDREN,
299+
0,
300+
1,
301+
AV_OPT_TYPE_INT,
302+
sample_rates);
303+
if (ret < 0) {
304+
mlt_log_error(filter, "Cannot set sink sample rates\n");
305+
goto fail;
306+
}
307+
308+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
309+
"channel_layouts",
310+
AV_OPT_SEARCH_CHILDREN,
311+
0,
312+
1,
313+
AV_OPT_TYPE_CHLAYOUT,
314+
&pdata->ch_layout);
315+
if (ret < 0) {
316+
mlt_log_error(filter, "Cannot set sink channel layouts\n");
317+
goto fail;
318+
}
319+
#else
283320
ret = av_opt_set_int_list(pdata->avbuffsink_ctx,
284321
"sample_fmts",
285322
sample_fmts,
@@ -298,6 +335,7 @@ static void init_audio_filtergraph(mlt_filter filter,
298335
mlt_log_error(filter, "Cannot set sink sample rates\n");
299336
goto fail;
300337
}
338+
301339
#if HAVE_FFMPEG_CH_LAYOUT
302340
ret = av_opt_set(pdata->avbuffsink_ctx,
303341
"ch_layouts",
@@ -326,6 +364,7 @@ static void init_audio_filtergraph(mlt_filter filter,
326364
mlt_log_error(filter, "Cannot set sink channel_layouts\n");
327365
goto fail;
328366
}
367+
#endif
329368
#endif
330369
ret = avfilter_init_str(pdata->avbuffsink_ctx, NULL);
331370
if (ret < 0) {
@@ -1020,6 +1059,7 @@ static void filter_close(mlt_filter filter)
10201059
avfilter_graph_free(&pdata->avfilter_graph);
10211060
av_frame_free(&pdata->avinframe);
10221061
av_frame_free(&pdata->avoutframe);
1062+
av_channel_layout_uninit(&pdata->ch_layout);
10231063
free(pdata);
10241064
}
10251065
filter->child = NULL;

src/modules/avformat/link_avfilter.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct
6161
int reset;
6262
mlt_position expected_frame;
6363
mlt_position continuity_frame;
64+
AVChannelLayout ch_layout;
6465
} private_data;
6566

6667
#if LIBAVUTIL_VERSION_INT >= ((56 << 16) + (35 << 8) + 101)
@@ -219,10 +220,8 @@ static void init_audio_filtergraph(mlt_link self,
219220
sample_rates[0] = frequency;
220221
channel_counts[0] = channels;
221222
#if HAVE_FFMPEG_CH_LAYOUT
222-
AVChannelLayout ch_layout;
223-
av_channel_layout_default(&ch_layout, channels);
224-
av_channel_layout_describe(&ch_layout, channel_layout_str, sizeof(channel_layout_str));
225-
av_channel_layout_uninit(&ch_layout);
223+
av_channel_layout_default(&pdata->ch_layout, channels);
224+
av_channel_layout_describe(&pdata->ch_layout, channel_layout_str, sizeof(channel_layout_str));
226225
#else
227226
int64_t channel_layouts[] = {-1, -1};
228227
channel_layouts[0] = av_get_default_channel_layout(channels);
@@ -297,6 +296,44 @@ static void init_audio_filtergraph(mlt_link self,
297296
mlt_log_error(self, "Cannot create audio buffer sink\n");
298297
goto fail;
299298
}
299+
300+
#if LIBAVFILTER_VERSION_INT >= ((10 << 16) + (6 << 8) + 100)
301+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
302+
"sample_formats",
303+
AV_OPT_SEARCH_CHILDREN,
304+
0,
305+
1,
306+
AV_OPT_TYPE_SAMPLE_FMT,
307+
sample_fmts);
308+
if (ret < 0) {
309+
mlt_log_error(self, "Cannot set sink sample formats\n");
310+
goto fail;
311+
}
312+
313+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
314+
"samplerates",
315+
AV_OPT_SEARCH_CHILDREN,
316+
0,
317+
1,
318+
AV_OPT_TYPE_INT,
319+
sample_rates);
320+
if (ret < 0) {
321+
mlt_log_error(self, "Cannot set sink sample rates\n");
322+
goto fail;
323+
}
324+
325+
ret = av_opt_set_array(pdata->avbuffsink_ctx,
326+
"channel_layouts",
327+
AV_OPT_SEARCH_CHILDREN,
328+
0,
329+
1,
330+
AV_OPT_TYPE_CHLAYOUT,
331+
&pdata->ch_layout);
332+
if (ret < 0) {
333+
mlt_log_error(self, "Cannot set sink channel layouts\n");
334+
goto fail;
335+
}
336+
#else
300337
ret = av_opt_set_int_list(pdata->avbuffsink_ctx,
301338
"sample_fmts",
302339
sample_fmts,
@@ -343,6 +380,7 @@ static void init_audio_filtergraph(mlt_link self,
343380
mlt_log_error(self, "Cannot set sink channel_layouts\n");
344381
goto fail;
345382
}
383+
#endif
346384
#endif
347385
ret = avfilter_init_str(pdata->avbuffsink_ctx, NULL);
348386
if (ret < 0) {
@@ -1129,6 +1167,7 @@ static void link_close(mlt_link self)
11291167
avfilter_graph_free(&pdata->avfilter_graph);
11301168
av_frame_free(&pdata->avinframe);
11311169
av_frame_free(&pdata->avoutframe);
1170+
av_channel_layout_uninit(&pdata->ch_layout);
11321171
free(pdata);
11331172
}
11341173
self->close = NULL;

0 commit comments

Comments
 (0)