00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/avassert.h"
00028 #include "libavutil/opt.h"
00029 #include "avfilter.h"
00030 #define FF_BUFQUEUE_SIZE 256
00031 #include "bufferqueue.h"
00032 #include "internal.h"
00033 #include "video.h"
00034 #include "audio.h"
00035
00036 #define TYPE_ALL 2
00037
00038 typedef struct {
00039 const AVClass *class;
00040 unsigned nb_streams[TYPE_ALL];
00041 unsigned nb_segments;
00042 unsigned cur_idx;
00043 int64_t delta_ts;
00044 unsigned nb_in_active;
00045 struct concat_in {
00046 int64_t pts;
00047 int64_t nb_frames;
00048 unsigned eof;
00049 struct FFBufQueue queue;
00050 } *in;
00051 } ConcatContext;
00052
00053 #define OFFSET(x) offsetof(ConcatContext, x)
00054 #define A AV_OPT_FLAG_AUDIO_PARAM
00055 #define F AV_OPT_FLAG_FILTERING_PARAM
00056 #define V AV_OPT_FLAG_VIDEO_PARAM
00057
00058 static const AVOption concat_options[] = {
00059 { "n", "specify the number of segments", OFFSET(nb_segments),
00060 AV_OPT_TYPE_INT, { .i64 = 2 }, 2, INT_MAX, V|A|F},
00061 { "v", "specify the number of video streams",
00062 OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]),
00063 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F },
00064 { "a", "specify the number of audio streams",
00065 OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]),
00066 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F},
00067 { 0 }
00068 };
00069
00070 AVFILTER_DEFINE_CLASS(concat);
00071
00072 static int query_formats(AVFilterContext *ctx)
00073 {
00074 ConcatContext *cat = ctx->priv;
00075 unsigned type, nb_str, idx0 = 0, idx, str, seg;
00076 AVFilterFormats *formats, *rates;
00077 AVFilterChannelLayouts *layouts;
00078
00079 for (type = 0; type < TYPE_ALL; type++) {
00080 nb_str = cat->nb_streams[type];
00081 for (str = 0; str < nb_str; str++) {
00082 idx = idx0;
00083
00084
00085 formats = ff_all_formats(type);
00086 if (!formats)
00087 return AVERROR(ENOMEM);
00088 ff_formats_ref(formats, &ctx->outputs[idx]->in_formats);
00089 if (type == AVMEDIA_TYPE_AUDIO) {
00090 rates = ff_all_samplerates();
00091 if (!rates)
00092 return AVERROR(ENOMEM);
00093 ff_formats_ref(rates, &ctx->outputs[idx]->in_samplerates);
00094 layouts = ff_all_channel_layouts();
00095 if (!layouts)
00096 return AVERROR(ENOMEM);
00097 ff_channel_layouts_ref(layouts, &ctx->outputs[idx]->in_channel_layouts);
00098 }
00099
00100
00101 for (seg = 0; seg < cat->nb_segments; seg++) {
00102 ff_formats_ref(formats, &ctx->inputs[idx]->out_formats);
00103 if (type == AVMEDIA_TYPE_AUDIO) {
00104 ff_formats_ref(rates, &ctx->inputs[idx]->out_samplerates);
00105 ff_channel_layouts_ref(layouts, &ctx->inputs[idx]->out_channel_layouts);
00106 }
00107 idx += ctx->nb_outputs;
00108 }
00109
00110 idx0++;
00111 }
00112 }
00113 return 0;
00114 }
00115
00116 static int config_output(AVFilterLink *outlink)
00117 {
00118 AVFilterContext *ctx = outlink->src;
00119 ConcatContext *cat = ctx->priv;
00120 unsigned out_no = FF_OUTLINK_IDX(outlink);
00121 unsigned in_no = out_no, seg;
00122 AVFilterLink *inlink = ctx->inputs[in_no];
00123
00124
00125 outlink->time_base = AV_TIME_BASE_Q;
00126 outlink->w = inlink->w;
00127 outlink->h = inlink->h;
00128 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
00129 outlink->format = inlink->format;
00130 for (seg = 1; seg < cat->nb_segments; seg++) {
00131 inlink = ctx->inputs[in_no += ctx->nb_outputs];
00132
00133 if (outlink->w != inlink->w ||
00134 outlink->h != inlink->h ||
00135 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
00136 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
00137 av_log(ctx, AV_LOG_ERROR, "Input link %s parameters "
00138 "(size %dx%d, SAR %d:%d) do not match the corresponding "
00139 "output link %s parameters (%dx%d, SAR %d:%d)\n",
00140 ctx->input_pads[in_no].name, inlink->w, inlink->h,
00141 inlink->sample_aspect_ratio.num,
00142 inlink->sample_aspect_ratio.den,
00143 ctx->input_pads[out_no].name, outlink->w, outlink->h,
00144 outlink->sample_aspect_ratio.num,
00145 outlink->sample_aspect_ratio.den);
00146 return AVERROR(EINVAL);
00147 }
00148 }
00149
00150 return 0;
00151 }
00152
00153 static void push_frame(AVFilterContext *ctx, unsigned in_no,
00154 AVFilterBufferRef *buf)
00155 {
00156 ConcatContext *cat = ctx->priv;
00157 unsigned out_no = in_no % ctx->nb_outputs;
00158 AVFilterLink * inlink = ctx-> inputs[ in_no];
00159 AVFilterLink *outlink = ctx->outputs[out_no];
00160 struct concat_in *in = &cat->in[in_no];
00161
00162 buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
00163 in->pts = buf->pts;
00164 in->nb_frames++;
00165
00166 if (inlink->sample_rate)
00167
00168 in->pts += av_rescale_q(buf->audio->nb_samples,
00169 (AVRational){ 1, inlink->sample_rate },
00170 outlink->time_base);
00171 else if (in->nb_frames >= 2)
00172
00173 in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
00174
00175 buf->pts += cat->delta_ts;
00176 switch (buf->type) {
00177 case AVMEDIA_TYPE_VIDEO:
00178 ff_start_frame(outlink, buf);
00179 ff_draw_slice(outlink, 0, outlink->h, 1);
00180 ff_end_frame(outlink);
00181 break;
00182 case AVMEDIA_TYPE_AUDIO:
00183 ff_filter_samples(outlink, buf);
00184 break;
00185 }
00186 }
00187
00188 static void process_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
00189 {
00190 AVFilterContext *ctx = inlink->dst;
00191 ConcatContext *cat = ctx->priv;
00192 unsigned in_no = FF_INLINK_IDX(inlink);
00193
00194 if (in_no < cat->cur_idx) {
00195 av_log(ctx, AV_LOG_ERROR, "Frame after EOF on input %s\n",
00196 ctx->input_pads[in_no].name);
00197 avfilter_unref_buffer(buf);
00198 } if (in_no >= cat->cur_idx + ctx->nb_outputs) {
00199 ff_bufqueue_add(ctx, &cat->in[in_no].queue, buf);
00200 } else {
00201 push_frame(ctx, in_no, buf);
00202 }
00203 }
00204
00205 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms,
00206 int w, int h)
00207 {
00208 AVFilterContext *ctx = inlink->dst;
00209 unsigned in_no = FF_INLINK_IDX(inlink);
00210 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
00211
00212 return ff_get_video_buffer(outlink, perms, w, h);
00213 }
00214
00215 static AVFilterBufferRef *get_audio_buffer(AVFilterLink *inlink, int perms,
00216 int nb_samples)
00217 {
00218 AVFilterContext *ctx = inlink->dst;
00219 unsigned in_no = FF_INLINK_IDX(inlink);
00220 AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
00221
00222 return ff_get_audio_buffer(outlink, perms, nb_samples);
00223 }
00224
00225 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
00226 {
00227 return 0;
00228 }
00229
00230 static int draw_slice(AVFilterLink *inlink, int y, int h, int dir)
00231 {
00232 return 0;
00233 }
00234
00235 static int end_frame(AVFilterLink *inlink)
00236 {
00237 process_frame(inlink, inlink->cur_buf);
00238 inlink->cur_buf = NULL;
00239 return 0;
00240 }
00241
00242 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
00243 {
00244 process_frame(inlink, buf);
00245 return 0;
00246 }
00247
00248 static void close_input(AVFilterContext *ctx, unsigned in_no)
00249 {
00250 ConcatContext *cat = ctx->priv;
00251
00252 cat->in[in_no].eof = 1;
00253 cat->nb_in_active--;
00254 av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n",
00255 ctx->input_pads[in_no].name, cat->nb_in_active);
00256 }
00257
00258 static void find_next_delta_ts(AVFilterContext *ctx)
00259 {
00260 ConcatContext *cat = ctx->priv;
00261 unsigned i = cat->cur_idx;
00262 unsigned imax = i + ctx->nb_outputs;
00263 int64_t pts;
00264
00265 pts = cat->in[i++].pts;
00266 for (; i < imax; i++)
00267 pts = FFMAX(pts, cat->in[i].pts);
00268 cat->delta_ts += pts;
00269 }
00270
00271 static void send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no)
00272 {
00273 ConcatContext *cat = ctx->priv;
00274 AVFilterLink *outlink = ctx->outputs[out_no];
00275 int64_t base_pts = cat->in[in_no].pts + cat->delta_ts;
00276 int64_t nb_samples, sent = 0;
00277 int frame_nb_samples;
00278 AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
00279 AVFilterBufferRef *buf;
00280 int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
00281
00282 if (!rate_tb.den)
00283 return;
00284 nb_samples = av_rescale_q(cat->delta_ts - base_pts,
00285 outlink->time_base, rate_tb);
00286 frame_nb_samples = FFMAX(9600, rate_tb.den / 5);
00287 while (nb_samples) {
00288 frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
00289 buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, frame_nb_samples);
00290 if (!buf)
00291 return;
00292 av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
00293 nb_channels, outlink->format);
00294 buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
00295 ff_filter_samples(outlink, buf);
00296 sent += frame_nb_samples;
00297 nb_samples -= frame_nb_samples;
00298 }
00299 }
00300
00301 static void flush_segment(AVFilterContext *ctx)
00302 {
00303 ConcatContext *cat = ctx->priv;
00304 unsigned str, str_max;
00305
00306 find_next_delta_ts(ctx);
00307 cat->cur_idx += ctx->nb_outputs;
00308 cat->nb_in_active = ctx->nb_outputs;
00309 av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
00310 cat->delta_ts);
00311
00312 if (cat->cur_idx < ctx->nb_inputs) {
00313
00314 str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
00315 str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
00316 for (; str < str_max; str++)
00317 send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str);
00318
00319
00320 str_max = cat->cur_idx + ctx->nb_outputs;
00321 for (str = cat->cur_idx; str < str_max; str++)
00322 while (cat->in[str].queue.available)
00323 push_frame(ctx, str, ff_bufqueue_get(&cat->in[str].queue));
00324 }
00325 }
00326
00327 static int request_frame(AVFilterLink *outlink)
00328 {
00329 AVFilterContext *ctx = outlink->src;
00330 ConcatContext *cat = ctx->priv;
00331 unsigned out_no = FF_OUTLINK_IDX(outlink);
00332 unsigned in_no = out_no + cat->cur_idx;
00333 unsigned str, str_max;
00334 int ret;
00335
00336 while (1) {
00337 if (in_no >= ctx->nb_inputs)
00338 return AVERROR_EOF;
00339 if (!cat->in[in_no].eof) {
00340 ret = ff_request_frame(ctx->inputs[in_no]);
00341 if (ret != AVERROR_EOF)
00342 return ret;
00343 close_input(ctx, in_no);
00344 }
00345
00346
00347 str_max = cat->cur_idx + ctx->nb_outputs - 1;
00348 for (str = cat->cur_idx; cat->nb_in_active;
00349 str = str == str_max ? cat->cur_idx : str + 1) {
00350 if (cat->in[str].eof)
00351 continue;
00352 ret = ff_request_frame(ctx->inputs[str]);
00353 if (ret == AVERROR_EOF)
00354 close_input(ctx, str);
00355 else if (ret < 0)
00356 return ret;
00357 }
00358 flush_segment(ctx);
00359 in_no += ctx->nb_outputs;
00360 }
00361 }
00362
00363 static av_cold int init(AVFilterContext *ctx, const char *args)
00364 {
00365 ConcatContext *cat = ctx->priv;
00366 int ret;
00367 unsigned seg, type, str;
00368 char name[32];
00369
00370 cat->class = &concat_class;
00371 av_opt_set_defaults(cat);
00372 ret = av_set_options_string(cat, args, "=", ":");
00373 if (ret < 0) {
00374 av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
00375 return ret;
00376 }
00377
00378
00379 for (seg = 0; seg < cat->nb_segments; seg++) {
00380 for (type = 0; type < TYPE_ALL; type++) {
00381 for (str = 0; str < cat->nb_streams[type]; str++) {
00382 AVFilterPad pad = {
00383 .type = type,
00384 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
00385 .get_video_buffer = get_video_buffer,
00386 .get_audio_buffer = get_audio_buffer,
00387 };
00388 snprintf(name, sizeof(name), "in%d:%c%d", seg, "va"[type], str);
00389 pad.name = av_strdup(name);
00390 if (type == AVMEDIA_TYPE_VIDEO) {
00391 pad.start_frame = start_frame;
00392 pad.draw_slice = draw_slice;
00393 pad.end_frame = end_frame;
00394 } else {
00395 pad.filter_samples = filter_samples;
00396 }
00397 ff_insert_inpad(ctx, ctx->nb_inputs, &pad);
00398 }
00399 }
00400 }
00401
00402 for (type = 0; type < TYPE_ALL; type++) {
00403 for (str = 0; str < cat->nb_streams[type]; str++) {
00404 AVFilterPad pad = {
00405 .type = type,
00406 .config_props = config_output,
00407 .request_frame = request_frame,
00408 };
00409 snprintf(name, sizeof(name), "out:%c%d", "va"[type], str);
00410 pad.name = av_strdup(name);
00411 ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
00412 }
00413 }
00414
00415 cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in));
00416 if (!cat->in)
00417 return AVERROR(ENOMEM);
00418 cat->nb_in_active = ctx->nb_outputs;
00419 return 0;
00420 }
00421
00422 static av_cold void uninit(AVFilterContext *ctx)
00423 {
00424 ConcatContext *cat = ctx->priv;
00425 unsigned i;
00426
00427 for (i = 0; i < ctx->nb_inputs; i++) {
00428 av_freep(&ctx->input_pads[i].name);
00429 ff_bufqueue_discard_all(&cat->in[i].queue);
00430 }
00431 for (i = 0; i < ctx->nb_outputs; i++)
00432 av_freep(&ctx->output_pads[i].name);
00433 av_free(cat->in);
00434 }
00435
00436 AVFilter avfilter_avf_concat = {
00437 .name = "concat",
00438 .description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."),
00439 .init = init,
00440 .uninit = uninit,
00441 .query_formats = query_formats,
00442 .priv_size = sizeof(ConcatContext),
00443 .inputs = (const AVFilterPad[]) { { .name = NULL } },
00444 .outputs = (const AVFilterPad[]) { { .name = NULL } },
00445 .priv_class = &concat_class,
00446 };