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"
38 typedef struct ConcatContext {
40 unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */
42 unsigned cur_idx; /**< index of the first input of current segment */
43 int64_t delta_ts; /**< timestamp to add to produce output timestamps */
44 unsigned nb_in_active; /**< number of active inputs in current segment */
53 #define OFFSET(x) offsetof(ConcatContext, x)
54 #define A AV_OPT_FLAG_AUDIO_PARAM
55 #define F AV_OPT_FLAG_FILTERING_PARAM
56 #define V AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption concat_options[] = {
59 { "n", "specify the number of segments", OFFSET(nb_segments),
60 AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, V|A|F},
61 { "v", "specify the number of video streams",
62 OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
63 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
64 { "a", "specify the number of audio streams",
65 OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
66 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
67 { "unsafe", "enable unsafe mode",
69 AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, V|A|F},
73 AVFILTER_DEFINE_CLASS(concat);
75 static int query_formats(AVFilterContext *ctx)
77 ConcatContext *cat = ctx->priv;
78 unsigned type, nb_str, idx0 = 0, idx, str, seg;
79 AVFilterFormats *formats, *rates = NULL;
80 AVFilterChannelLayouts *layouts = NULL;
83 for (type = 0; type < TYPE_ALL; type++) {
84 nb_str = cat->nb_streams[type];
85 for (str = 0; str < nb_str; str++) {
88 /* Set the output formats */
89 formats = ff_all_formats(type);
90 if ((ret = ff_formats_ref(formats, &ctx->outputs[idx]->in_formats)) < 0)
93 if (type == AVMEDIA_TYPE_AUDIO) {
94 rates = ff_all_samplerates();
95 if ((ret = ff_formats_ref(rates, &ctx->outputs[idx]->in_samplerates)) < 0)
97 layouts = ff_all_channel_layouts();
98 if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->in_channel_layouts)) < 0)
102 /* Set the same formats for each corresponding input */
103 for (seg = 0; seg < cat->nb_segments; seg++) {
104 if ((ret = ff_formats_ref(formats, &ctx->inputs[idx]->out_formats)) < 0)
106 if (type == AVMEDIA_TYPE_AUDIO) {
107 if ((ret = ff_formats_ref(rates, &ctx->inputs[idx]->out_samplerates)) < 0 ||
108 (ret = ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->out_channel_layouts)) < 0)
111 idx += ctx->nb_outputs;
120 static int config_output(AVFilterLink *outlink)
122 AVFilterContext *ctx = outlink->src;
123 ConcatContext *cat = ctx->priv;
124 unsigned out_no = FF_OUTLINK_IDX(outlink);
125 unsigned in_no = out_no, seg;
126 AVFilterLink *inlink = ctx->inputs[in_no];
128 /* enhancement: find a common one */
129 outlink->time_base = AV_TIME_BASE_Q;
130 outlink->w = inlink->w;
131 outlink->h = inlink->h;
132 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
133 outlink->format = inlink->format;
134 for (seg = 1; seg < cat->nb_segments; seg++) {
135 inlink = ctx->inputs[in_no += ctx->nb_outputs];
136 if (!outlink->sample_aspect_ratio.num)
137 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
138 /* possible enhancement: unsafe mode, do not check */
139 if (outlink->w != inlink->w ||
140 outlink->h != inlink->h ||
141 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num &&
142 inlink->sample_aspect_ratio.num ||
143 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
144 av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
145 "(size %dx%d, SAR %d:%d) do not match the corresponding "
146 "output link %s parameters (%dx%d, SAR %d:%d)\n",
147 ctx->input_pads[in_no].name, inlink->w, inlink->h,
148 inlink->sample_aspect_ratio.num,
149 inlink->sample_aspect_ratio.den,
150 ctx->input_pads[out_no].name, outlink->w, outlink->h,
151 outlink->sample_aspect_ratio.num,
152 outlink->sample_aspect_ratio.den);
154 return AVERROR(EINVAL);
161 static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
163 ConcatContext *cat = ctx->priv;
164 unsigned out_no = in_no % ctx->nb_outputs;
165 AVFilterLink * inlink = ctx-> inputs[ in_no];
166 AVFilterLink *outlink = ctx->outputs[out_no];
167 struct concat_in *in = &cat->in[in_no];
169 buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
172 /* add duration to input PTS */
173 if (inlink->sample_rate)
174 /* use number of audio samples */
175 in->pts += av_rescale_q(buf->nb_samples,
176 av_make_q(1, inlink->sample_rate),
178 else if (in->nb_frames >= 2)
179 /* use mean duration */
180 in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
182 buf->pts += cat->delta_ts;
183 return ff_filter_frame(outlink, buf);
186 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
188 AVFilterContext *ctx = inlink->dst;
189 unsigned in_no = FF_INLINK_IDX(inlink);
190 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
192 return ff_get_video_buffer(outlink, w, h);
195 static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
197 AVFilterContext *ctx = inlink->dst;
198 unsigned in_no = FF_INLINK_IDX(inlink);
199 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
201 return ff_get_audio_buffer(outlink, nb_samples);
204 static void close_input(AVFilterContext *ctx, unsigned in_no)
206 ConcatContext *cat = ctx->priv;
208 cat->in[in_no].eof = 1;
210 av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
211 ctx->input_pads[in_no].name, cat->nb_in_active);
214 static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
216 ConcatContext *cat = ctx->priv;
217 unsigned i = cat->cur_idx;
218 unsigned imax = i + ctx->nb_outputs;
221 pts = cat->in[i++].pts;
222 for (; i < imax; i++)
223 pts = FFMAX(pts, cat->in[i].pts);
224 cat->delta_ts += pts;
228 static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
231 ConcatContext *cat = ctx->priv;
232 AVFilterLink *outlink = ctx->outputs[out_no];
233 int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
234 int64_t nb_samples, sent = 0;
235 int frame_nb_samples, ret;
236 AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
241 nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
242 outlink->time_base, rate_tb);
243 frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
245 frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
246 buf = ff_get_audio_buffer(outlink, frame_nb_samples);
248 return AVERROR(ENOMEM);
249 av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
250 outlink->channels, outlink->format);
251 buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
252 ret = ff_filter_frame(outlink, buf);
255 sent += frame_nb_samples;
256 nb_samples -= frame_nb_samples;
261 static int flush_segment(AVFilterContext *ctx)
264 ConcatContext *cat = ctx->priv;
265 unsigned str, str_max;
268 find_next_delta_ts(ctx, &seg_delta);
269 cat->cur_idx += ctx->nb_outputs;
270 cat->nb_in_active = ctx->nb_outputs;
271 av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
274 if (cat->cur_idx < ctx->nb_inputs) {
275 /* pad audio streams with silence */
276 str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
277 str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
278 for (; str < str_max; str++) {
279 ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
288 static av_cold int init(AVFilterContext *ctx)
290 ConcatContext *cat = ctx->priv;
291 unsigned seg, type, str;
294 /* create input pads */
295 for (seg = 0; seg < cat->nb_segments; seg++) {
296 for (type = 0; type < TYPE_ALL; type++) {
297 for (str = 0; str < cat->nb_streams[type]; str++) {
300 .get_video_buffer = get_video_buffer,
301 .get_audio_buffer = get_audio_buffer,
303 pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str);
304 if ((ret = ff_insert_inpad(ctx, ctx->nb_inputs, &pad)) < 0) {
311 /* create output pads */
312 for (type = 0; type < TYPE_ALL; type++) {
313 for (str = 0; str < cat->nb_streams[type]; str++) {
316 .config_props = config_output,
318 pad.name = av_asprintf("out:%c%d", "va"[type], str);
319 if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
326 cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
328 return AVERROR(ENOMEM);
329 cat->nb_in_active = ctx->nb_outputs;
333 static av_cold void uninit(AVFilterContext *ctx)
335 ConcatContext *cat = ctx->priv;
338 for (i = 0; i < ctx->nb_inputs; i++)
339 av_freep(&ctx->input_pads[i].name);
340 for (i = 0; i < ctx->nb_outputs; i++)
341 av_freep(&ctx->output_pads[i].name);
345 static int activate(AVFilterContext *ctx)
347 ConcatContext *cat = ctx->priv;
353 /* Forward status back */
354 for (i = 0; i < ctx->nb_outputs; i++) {
355 status = ff_outlink_get_status(ctx->outputs[i]);
358 for (j = i; j < ctx->nb_inputs; j += ctx->nb_outputs) {
359 if (!cat->in[j].eof) {
361 ff_inlink_set_status(ctx->inputs[j], status);
368 /* Forward available frames */
369 if (cat->cur_idx < ctx->nb_inputs) {
370 for (i = 0; i < ctx->nb_outputs; i++) {
371 ret = ff_inlink_consume_frame(ctx->inputs[cat->cur_idx + i], &frame);
375 ff_filter_set_ready(ctx, 10);
376 return push_frame(ctx, cat->cur_idx + i, frame);
381 /* Forward status change */
382 if (cat->cur_idx < ctx->nb_inputs) {
383 for (i = 0; i < ctx->nb_outputs; i++) {
384 ret = ff_inlink_acknowledge_status(ctx->inputs[cat->cur_idx + i], &status, &pts);
387 close_input(ctx, cat->cur_idx + i);
388 if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) {
389 ff_outlink_set_status(ctx->outputs[i], status, pts);
391 if (!cat->nb_in_active) {
392 ret = flush_segment(ctx);
396 ff_filter_set_ready(ctx, 10);
402 ret = FFERROR_NOT_READY;
403 for (i = 0; i < ctx->nb_outputs; i++) {
404 if (ff_outlink_frame_wanted(ctx->outputs[i])) {
405 if (cat->in[cat->cur_idx + i].eof) {
406 for (j = 0; j < ctx->nb_outputs; j++)
407 if (!cat->in[cat->cur_idx + j].eof)
408 ff_inlink_request_frame(ctx->inputs[cat->cur_idx + j]);
411 ff_inlink_request_frame(ctx->inputs[cat->cur_idx + i]);
420 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
421 char *res, int res_len, int flags)
423 int ret = AVERROR(ENOSYS);
425 if (!strcmp(cmd, "next")) {
426 av_log(ctx, AV_LOG_VERBOSE, "Command received: next\n");
427 return flush_segment(ctx);
433 AVFilter ff_avf_concat = {
435 .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
438 .query_formats = query_formats,
439 .activate = activate,
440 .priv_size = sizeof(ConcatContext),
443 .priv_class = &concat_class,
444 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
445 .process_command = process_command,