I’m trying to combine two raw audio chunks (s16, 48000, mono) through acrossfade.
I create filter nodes (skip all error checking here):
avfilter_graph_create_filter(&mediaIn_1,
avfilter_get_by_name("abuffer"),
"MediaIn_1",
"sample_rate=48000:sample_fmt=s16:channel_layout=mono",
NULL, graph);
avfilter_graph_create_filter(&mediaIn_2,
avfilter_get_by_name("abuffer"),
"MediaIn_2",
"sample_rate=48000:sample_fmt=s16:channel_layout=mono",
NULL, graph);
avfilter_graph_create_filter(&mediaOut,
avfilter_get_by_name("abuffersink"),
"MediaOut",
NULL, NULL, graph);
avfilter_graph_create_filter(&crossfade,
avfilter_get_by_name("acrossfade"),
"crossfade", "nb_samples=6000:c1=tri:c2=tri", NULL, graph);
Then I link them in a graph:
avfilter_link(mediaIn_1, 0, crossfade, 0);
avfilter_link(mediaIn_2, 0, crossfade, 1);
avfilter_link(crossfade, 0, mediaOut, 0);
avfilter_graph_config(graph, NULL);
After that I create a frame with all the chunk data:
frame1 = av_frame_alloc();
frame1->format = AV_SAMPLE_FMT_S16;
frame1->nb_samples = buf1sz / 2;
frame1->sample_rate = 48000;
frame1->ch_layout.order = AV_CHANNEL_ORDER_NATIVE;
frame1->ch_layout.nb_channels = 1;
frame1->ch_layout.u.mask = AV_CH_LAYOUT_MONO;
frame1->ch_layout.opaque = NULL;
frame1->pts = 0;
frame1->duration = buf1sz / 2;
frame1->time_base.num = 1;
frame1->time_base.den = 48000;
av_frame_get_buffer(frame1, 0);
memcpy(frame1->buf[0]->data, buf1, buf1sz);
Same for second chunk.
And send frames into each input buffer:
av_buffersrc_add_frame_flags(mediaIn_1, frame1, 0);
av_buffersrc_add_frame_flags(mediaIn_1, NULL, AV_BUFFERSRC_FLAG_PUSH);
...
av_buffersrc_add_frame_flags(mediaIn_2, frame2, 0);
av_buffersrc_add_frame_flags(mediaIn_2, NULL, AV_BUFFERSRC_FLAG_PUSH);
Then I’m getting output frame:
oframe = av_frame_alloc();
av_buffersink_get_frame_flags(mediaOut, oframe, 0);
...
av_frame_unref(oframe);
The oframe contains (frame1->nb_samples – 6000) number of samples instead of (frame1->nb_samples + frame2->nb_samples – something_for_xfade_needs).
Next call to av_buffersink_get_frame_flags
returns AVERROR_EOF.
What is wrong with this algorithm?
I tried afade filter with “t=in” input and it works fine. I think the problem is with two inputs. I don’t get it.