2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * concat audio-video filter
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
31 #define FF_BUFQUEUE_SIZE 256
32 #include "bufferqueue.h"
41 unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */
43 unsigned cur_idx; /**< index of the first input of current segment */
44 int64_t delta_ts; /**< timestamp to add to produce output timestamps */
45 unsigned nb_in_active; /**< number of active inputs in current segment */
51 struct FFBufQueue queue;
55 #define OFFSET(x) offsetof(ConcatContext, x)
56 #define A AV_OPT_FLAG_AUDIO_PARAM
57 #define F AV_OPT_FLAG_FILTERING_PARAM
58 #define V AV_OPT_FLAG_VIDEO_PARAM
60 static const AVOption concat_options[] = {
61 { "n", "specify the number of segments", OFFSET(nb_segments),
62 AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, V|A|F},
63 { "v", "specify the number of video streams",
64 OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
65 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
66 { "a", "specify the number of audio streams",
67 OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
68 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
69 { "unsafe", "enable unsafe mode",
71 AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, V|A|F},
75 AVFILTER_DEFINE_CLASS(concat);
77 static int query_formats(AVFilterContext *ctx)
79 ConcatContext *cat = ctx->priv;
80 unsigned type, nb_str, idx0 = 0, idx, str, seg;
81 AVFilterFormats *formats, *rates = NULL;
82 AVFilterChannelLayouts *layouts = NULL;
85 for (type = 0; type < TYPE_ALL; type++) {
86 nb_str = cat->nb_streams[type];
87 for (str = 0; str < nb_str; str++) {
90 /* Set the output formats */
91 formats = ff_all_formats(type);
92 if ((ret = ff_formats_ref(formats, &ctx->outputs[idx]->in_formats)) < 0)
95 if (type == AVMEDIA_TYPE_AUDIO) {
96 rates = ff_all_samplerates();
97 if ((ret = ff_formats_ref(rates, &ctx->outputs[idx]->in_samplerates)) < 0)
99 layouts = ff_all_channel_layouts();
100 if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->in_channel_layouts)) < 0)
104 /* Set the same formats for each corresponding input */
105 for (seg = 0; seg < cat->nb_segments; seg++) {
106 if ((ret = ff_formats_ref(formats, &ctx->inputs[idx]->out_formats)) < 0)
108 if (type == AVMEDIA_TYPE_AUDIO) {
109 if ((ret = ff_formats_ref(rates, &ctx->inputs[idx]->out_samplerates)) < 0 ||
110 (ret = ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->out_channel_layouts)) < 0)
113 idx += ctx->nb_outputs;
122 static int config_output(AVFilterLink *outlink)
124 AVFilterContext *ctx = outlink->src;
125 ConcatContext *cat = ctx->priv;
126 unsigned out_no = FF_OUTLINK_IDX(outlink);
127 unsigned in_no = out_no, seg;
128 AVFilterLink *inlink = ctx->inputs[in_no];
130 /* enhancement: find a common one */
131 outlink->time_base = AV_TIME_BASE_Q;
132 outlink->w = inlink->w;
133 outlink->h = inlink->h;
134 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
135 outlink->format = inlink->format;
136 for (seg = 1; seg < cat->nb_segments; seg++) {
137 inlink = ctx->inputs[in_no += ctx->nb_outputs];
138 if (!outlink->sample_aspect_ratio.num)
139 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
140 /* possible enhancement: unsafe mode, do not check */
141 if (outlink->w != inlink->w ||
142 outlink->h != inlink->h ||
143 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num &&
144 inlink->sample_aspect_ratio.num ||
145 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
146 av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
147 "(size %dx%d, SAR %d:%d) do not match the corresponding "
148 "output link %s parameters (%dx%d, SAR %d:%d)\n",
149 ctx->input_pads[in_no].name, inlink->w, inlink->h,
150 inlink->sample_aspect_ratio.num,
151 inlink->sample_aspect_ratio.den,
152 ctx->input_pads[out_no].name, outlink->w, outlink->h,
153 outlink->sample_aspect_ratio.num,
154 outlink->sample_aspect_ratio.den);
156 return AVERROR(EINVAL);
163 static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
165 ConcatContext *cat = ctx->priv;
166 unsigned out_no = in_no % ctx->nb_outputs;
167 AVFilterLink * inlink = ctx-> inputs[ in_no];
168 AVFilterLink *outlink = ctx->outputs[out_no];
169 struct concat_in *in = &cat->in[in_no];
171 buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
174 /* add duration to input PTS */
175 if (inlink->sample_rate)
176 /* use number of audio samples */
177 in->pts += av_rescale_q(buf->nb_samples,
178 av_make_q(1, inlink->sample_rate),
180 else if (in->nb_frames >= 2)
181 /* use mean duration */
182 in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
184 buf->pts += cat->delta_ts;
185 return ff_filter_frame(outlink, buf);
188 static int process_frame(AVFilterLink *inlink, AVFrame *buf)
190 AVFilterContext *ctx = inlink->dst;
191 ConcatContext *cat = ctx->priv;
192 unsigned in_no = FF_INLINK_IDX(inlink);
194 if (in_no < cat->cur_idx) {
195 av_log(ctx, AV_LOG_ERROR, "Frame after EOF on input %s\n",
196 ctx->input_pads[in_no].name);
198 } else if (in_no >= cat->cur_idx + ctx->nb_outputs) {
199 ff_bufqueue_add(ctx, &cat->in[in_no].queue, buf);
201 return push_frame(ctx, in_no, buf);
206 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
208 AVFilterContext *ctx = inlink->dst;
209 unsigned in_no = FF_INLINK_IDX(inlink);
210 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
212 return ff_get_video_buffer(outlink, w, h);
215 static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
217 AVFilterContext *ctx = inlink->dst;
218 unsigned in_no = FF_INLINK_IDX(inlink);
219 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
221 return ff_get_audio_buffer(outlink, nb_samples);
224 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
226 return process_frame(inlink, buf);
229 static void close_input(AVFilterContext *ctx, unsigned in_no)
231 ConcatContext *cat = ctx->priv;
233 cat->in[in_no].eof = 1;
235 av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
236 ctx->input_pads[in_no].name, cat->nb_in_active);
239 static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
241 ConcatContext *cat = ctx->priv;
242 unsigned i = cat->cur_idx;
243 unsigned imax = i + ctx->nb_outputs;
246 pts = cat->in[i++].pts;
247 for (; i < imax; i++)
248 pts = FFMAX(pts, cat->in[i].pts);
249 cat->delta_ts += pts;
253 static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
256 ConcatContext *cat = ctx->priv;
257 AVFilterLink *outlink = ctx->outputs[out_no];
258 int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
259 int64_t nb_samples, sent = 0;
260 int frame_nb_samples, ret;
261 AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
263 int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
267 nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
268 outlink->time_base, rate_tb);
269 frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
271 frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
272 buf = ff_get_audio_buffer(outlink, frame_nb_samples);
274 return AVERROR(ENOMEM);
275 av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
276 nb_channels, outlink->format);
277 buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
278 ret = ff_filter_frame(outlink, buf);
281 sent += frame_nb_samples;
282 nb_samples -= frame_nb_samples;
287 static int flush_segment(AVFilterContext *ctx)
290 ConcatContext *cat = ctx->priv;
291 unsigned str, str_max;
294 find_next_delta_ts(ctx, &seg_delta);
295 cat->cur_idx += ctx->nb_outputs;
296 cat->nb_in_active = ctx->nb_outputs;
297 av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
300 if (cat->cur_idx < ctx->nb_inputs) {
301 /* pad audio streams with silence */
302 str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
303 str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
304 for (; str < str_max; str++) {
305 ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
310 /* flush queued buffers */
311 /* possible enhancement: flush in PTS order */
312 str_max = cat->cur_idx + ctx->nb_outputs;
313 for (str = cat->cur_idx; str < str_max; str++) {
314 while (cat->in[str].queue.available) {
315 ret = push_frame(ctx, str, ff_bufqueue_get(&cat->in[str].queue));
324 static int request_frame(AVFilterLink *outlink)
326 AVFilterContext *ctx = outlink->src;
327 ConcatContext *cat = ctx->priv;
328 unsigned out_no = FF_OUTLINK_IDX(outlink);
329 unsigned in_no = out_no + cat->cur_idx;
330 unsigned str, str_max;
334 if (in_no >= ctx->nb_inputs)
336 if (!cat->in[in_no].eof) {
337 ret = ff_request_frame(ctx->inputs[in_no]);
338 if (ret != AVERROR_EOF)
340 close_input(ctx, in_no);
342 /* cycle on all inputs to finish the segment */
343 /* possible enhancement: request in PTS order */
344 str_max = cat->cur_idx + ctx->nb_outputs - 1;
345 for (str = cat->cur_idx; cat->nb_in_active;
346 str = str == str_max ? cat->cur_idx : str + 1) {
347 if (cat->in[str].eof)
349 ret = ff_request_frame(ctx->inputs[str]);
350 if (ret != AVERROR_EOF)
352 close_input(ctx, str);
354 ret = flush_segment(ctx);
357 in_no += ctx->nb_outputs;
361 static av_cold int init(AVFilterContext *ctx)
363 ConcatContext *cat = ctx->priv;
364 unsigned seg, type, str;
366 /* create input pads */
367 for (seg = 0; seg < cat->nb_segments; seg++) {
368 for (type = 0; type < TYPE_ALL; type++) {
369 for (str = 0; str < cat->nb_streams[type]; str++) {
372 .get_video_buffer = get_video_buffer,
373 .get_audio_buffer = get_audio_buffer,
374 .filter_frame = filter_frame,
376 pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
377 ff_insert_inpad(ctx, ctx->nb_inputs, &pad);
381 /* create output pads */
382 for (type = 0; type < TYPE_ALL; type++) {
383 for (str = 0; str < cat->nb_streams[type]; str++) {
386 .config_props = config_output,
387 .request_frame = request_frame,
389 pad.name = av_asprintf("out:%c%d", "va"[type], str);
390 ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
394 cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
396 return AVERROR(ENOMEM);
397 cat->nb_in_active = ctx->nb_outputs;
401 static av_cold void uninit(AVFilterContext *ctx)
403 ConcatContext *cat = ctx->priv;
406 for (i = 0; i < ctx->nb_inputs; i++) {
407 av_freep(&ctx->input_pads[i].name);
408 ff_bufqueue_discard_all(&cat->in[i].queue);
410 for (i = 0; i < ctx->nb_outputs; i++)
411 av_freep(&ctx->output_pads[i].name);
415 AVFilter ff_avf_concat = {
417 .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
420 .query_formats = query_formats,
421 .priv_size = sizeof(ConcatContext),
424 .priv_class = &concat_class,
425 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,