libavfilter/avf_concat.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2012 Nicolas George | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 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. | ||
| 10 | * | ||
| 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. | ||
| 15 | * | ||
| 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 | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * concat audio-video filter | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "libavutil/avstring.h" | ||
| 27 | #include "libavutil/channel_layout.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/opt.h" | ||
| 30 | #include "avfilter.h" | ||
| 31 | #include "filters.h" | ||
| 32 | #include "formats.h" | ||
| 33 | #include "video.h" | ||
| 34 | #include "audio.h" | ||
| 35 | |||
| 36 | #define TYPE_ALL 2 | ||
| 37 | |||
| 38 | typedef struct ConcatContext { | ||
| 39 | const AVClass *class; | ||
| 40 | unsigned nb_streams[TYPE_ALL]; /**< number of out streams of each type */ | ||
| 41 | unsigned nb_segments; | ||
| 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 */ | ||
| 45 | unsigned unsafe; | ||
| 46 | struct concat_in { | ||
| 47 | int64_t pts; | ||
| 48 | int64_t nb_frames; | ||
| 49 | unsigned eof; | ||
| 50 | } *in; | ||
| 51 | struct concat_silence { | ||
| 52 | int64_t end_pts; | ||
| 53 | int64_t nb_samples; | ||
| 54 | } *silence; | ||
| 55 | } ConcatContext; | ||
| 56 | |||
| 57 | #define OFFSET(x) offsetof(ConcatContext, x) | ||
| 58 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
| 59 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
| 60 | #define V AV_OPT_FLAG_VIDEO_PARAM | ||
| 61 | |||
| 62 | static const AVOption concat_options[] = { | ||
| 63 | { "n", "specify the number of segments", OFFSET(nb_segments), | ||
| 64 | AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, V|A|F}, | ||
| 65 | { "v", "specify the number of video streams", | ||
| 66 | OFFSET(nb_streams[AVMEDIA_TYPE_VIDEO]), | ||
| 67 | AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, V|F }, | ||
| 68 | { "a", "specify the number of audio streams", | ||
| 69 | OFFSET(nb_streams[AVMEDIA_TYPE_AUDIO]), | ||
| 70 | AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A|F}, | ||
| 71 | { "unsafe", "enable unsafe mode", | ||
| 72 | OFFSET(unsafe), | ||
| 73 | AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, V|A|F}, | ||
| 74 | { NULL } | ||
| 75 | }; | ||
| 76 | |||
| 77 | AVFILTER_DEFINE_CLASS(concat); | ||
| 78 | |||
| 79 | 7 | static int query_formats(const AVFilterContext *ctx, | |
| 80 | AVFilterFormatsConfig **cfg_in, | ||
| 81 | AVFilterFormatsConfig **cfg_out) | ||
| 82 | { | ||
| 83 | 7 | const ConcatContext *cat = ctx->priv; | |
| 84 | 7 | unsigned type, nb_str, idx0 = 0, idx, str, seg; | |
| 85 | 7 | AVFilterFormats *formats, *rates = NULL; | |
| 86 | 7 | AVFilterChannelLayouts *layouts = NULL; | |
| 87 | int ret; | ||
| 88 | |||
| 89 | 21 | for (type = 0; type < TYPE_ALL; type++) { | |
| 90 | 14 | nb_str = cat->nb_streams[type]; | |
| 91 | 23 | for (str = 0; str < nb_str; str++) { | |
| 92 | 9 | idx = idx0; | |
| 93 | |||
| 94 | /* Set the output formats */ | ||
| 95 | 9 | formats = ff_all_formats(type); | |
| 96 | 9 | if ((ret = ff_formats_ref(formats, &cfg_out[idx]->formats)) < 0) | |
| 97 | ✗ | return ret; | |
| 98 | |||
| 99 | 9 | if (type == AVMEDIA_TYPE_AUDIO) { | |
| 100 | 2 | rates = ff_all_samplerates(); | |
| 101 | 2 | if ((ret = ff_formats_ref(rates, &cfg_out[idx]->samplerates)) < 0) | |
| 102 | ✗ | return ret; | |
| 103 | 2 | layouts = ff_all_channel_layouts(); | |
| 104 | 2 | if ((ret = ff_channel_layouts_ref(layouts, &cfg_out[idx]->channel_layouts)) < 0) | |
| 105 | ✗ | return ret; | |
| 106 | } | ||
| 107 | |||
| 108 | /* Set the same formats for each corresponding input */ | ||
| 109 | 31 | for (seg = 0; seg < cat->nb_segments; seg++) { | |
| 110 | 22 | if ((ret = ff_formats_ref(formats, &cfg_in[idx]->formats)) < 0) | |
| 111 | ✗ | return ret; | |
| 112 | 22 | if (type == AVMEDIA_TYPE_AUDIO) { | |
| 113 | 12 | if ((ret = ff_formats_ref(rates, &cfg_in[idx]->samplerates)) < 0 || | |
| 114 | 6 | (ret = ff_channel_layouts_ref(layouts, &cfg_in[idx]->channel_layouts)) < 0) | |
| 115 | ✗ | return ret; | |
| 116 | } | ||
| 117 | 22 | idx += ctx->nb_outputs; | |
| 118 | } | ||
| 119 | |||
| 120 | 9 | idx0++; | |
| 121 | } | ||
| 122 | } | ||
| 123 | 7 | return 0; | |
| 124 | } | ||
| 125 | |||
| 126 | 9 | static int config_output(AVFilterLink *outlink) | |
| 127 | { | ||
| 128 | 9 | FilterLink *outl = ff_filter_link(outlink); | |
| 129 | 9 | AVFilterContext *ctx = outlink->src; | |
| 130 | 9 | ConcatContext *cat = ctx->priv; | |
| 131 | 9 | unsigned out_no = FF_OUTLINK_IDX(outlink); | |
| 132 | 9 | unsigned in_no = out_no, seg; | |
| 133 | 9 | AVFilterLink *inlink = ctx->inputs[in_no]; | |
| 134 | 9 | FilterLink *inl = ff_filter_link(inlink); | |
| 135 | |||
| 136 | /* enhancement: find a common one */ | ||
| 137 | 9 | outlink->time_base = AV_TIME_BASE_Q; | |
| 138 | 9 | outlink->w = inlink->w; | |
| 139 | 9 | outlink->h = inlink->h; | |
| 140 | 9 | outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; | |
| 141 | 9 | outlink->format = inlink->format; | |
| 142 | 9 | outl->frame_rate = inl->frame_rate; | |
| 143 | |||
| 144 | 20 | for (seg = 1; seg < cat->nb_segments; seg++) { | |
| 145 | 12 | inlink = ctx->inputs[in_no + seg * ctx->nb_outputs]; | |
| 146 | 12 | inl = ff_filter_link(inlink); | |
| 147 | 12 | if (outl->frame_rate.num != inl->frame_rate.num || | |
| 148 | 11 | outl->frame_rate.den != inl->frame_rate.den) { | |
| 149 | 1 | av_log(ctx, AV_LOG_VERBOSE, | |
| 150 | "Video inputs have different frame rates, output will be VFR\n"); | ||
| 151 | 1 | outl->frame_rate = av_make_q(1, 0); | |
| 152 | 1 | break; | |
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | 22 | for (seg = 1; seg < cat->nb_segments; seg++) { | |
| 157 | 13 | inlink = ctx->inputs[in_no + seg * ctx->nb_outputs]; | |
| 158 | 13 | if (!outlink->sample_aspect_ratio.num) | |
| 159 | 5 | outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; | |
| 160 | /* possible enhancement: unsafe mode, do not check */ | ||
| 161 | 13 | if (outlink->w != inlink->w || | |
| 162 | 12 | outlink->h != inlink->h || | |
| 163 | 12 | outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num && | |
| 164 | ✗ | inlink->sample_aspect_ratio.num || | |
| 165 | 12 | outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) { | |
| 166 | 1 | av_log(ctx, AV_LOG_ERROR, "Input link %s parameters " | |
| 167 | "(size %dx%d, SAR %d:%d) do not match the corresponding " | ||
| 168 | "output link %s parameters (%dx%d, SAR %d:%d)\n", | ||
| 169 | 1 | ctx->input_pads[in_no].name, inlink->w, inlink->h, | |
| 170 | inlink->sample_aspect_ratio.num, | ||
| 171 | inlink->sample_aspect_ratio.den, | ||
| 172 | 1 | ctx->input_pads[out_no].name, outlink->w, outlink->h, | |
| 173 | outlink->sample_aspect_ratio.num, | ||
| 174 | outlink->sample_aspect_ratio.den); | ||
| 175 | 1 | if (!cat->unsafe) | |
| 176 | ✗ | return AVERROR(EINVAL); | |
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | 9 | return 0; | |
| 181 | } | ||
| 182 | |||
| 183 | 480 | static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf) | |
| 184 | { | ||
| 185 | 480 | ConcatContext *cat = ctx->priv; | |
| 186 | 480 | unsigned out_no = in_no % ctx->nb_outputs; | |
| 187 | 480 | AVFilterLink * inlink = ctx-> inputs[ in_no]; | |
| 188 | 480 | AVFilterLink *outlink = ctx->outputs[out_no]; | |
| 189 | 480 | struct concat_in *in = &cat->in[in_no]; | |
| 190 | |||
| 191 | 480 | buf->pts = av_rescale_q(buf->pts, inlink->time_base, outlink->time_base); | |
| 192 | 480 | buf->duration = av_rescale_q(buf->duration, inlink->time_base, outlink->time_base); | |
| 193 | 480 | in->pts = buf->pts; | |
| 194 | 480 | in->nb_frames++; | |
| 195 | /* add duration to input PTS */ | ||
| 196 | 480 | if (inlink->sample_rate) | |
| 197 | /* use number of audio samples */ | ||
| 198 | 350 | in->pts += av_rescale_q(buf->nb_samples, | |
| 199 | av_make_q(1, inlink->sample_rate), | ||
| 200 | outlink->time_base); | ||
| 201 | 130 | else if (in->nb_frames >= 2) | |
| 202 | /* use mean duration */ | ||
| 203 | 114 | in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1); | |
| 204 | |||
| 205 | 480 | buf->pts += cat->delta_ts; | |
| 206 | 480 | return ff_filter_frame(outlink, buf); | |
| 207 | } | ||
| 208 | |||
| 209 | 79 | static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h) | |
| 210 | { | ||
| 211 | 79 | AVFilterContext *ctx = inlink->dst; | |
| 212 | 79 | unsigned in_no = FF_INLINK_IDX(inlink); | |
| 213 | 79 | AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs]; | |
| 214 | |||
| 215 | 79 | return ff_get_video_buffer(outlink, w, h); | |
| 216 | } | ||
| 217 | |||
| 218 | 350 | static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples) | |
| 219 | { | ||
| 220 | 350 | AVFilterContext *ctx = inlink->dst; | |
| 221 | 350 | unsigned in_no = FF_INLINK_IDX(inlink); | |
| 222 | 350 | AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs]; | |
| 223 | |||
| 224 | 350 | return ff_get_audio_buffer(outlink, nb_samples); | |
| 225 | } | ||
| 226 | |||
| 227 | 22 | static void close_input(AVFilterContext *ctx, unsigned in_no) | |
| 228 | { | ||
| 229 | 22 | ConcatContext *cat = ctx->priv; | |
| 230 | |||
| 231 | 22 | cat->in[in_no].eof = 1; | |
| 232 | 22 | cat->nb_in_active--; | |
| 233 | 22 | av_log(ctx, AV_LOG_VERBOSE, "EOF on %s, %d streams left in segment.\n", | |
| 234 | 22 | ctx->input_pads[in_no].name, cat->nb_in_active); | |
| 235 | 22 | } | |
| 236 | |||
| 237 | 16 | static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta) | |
| 238 | { | ||
| 239 | 16 | ConcatContext *cat = ctx->priv; | |
| 240 | 16 | unsigned i = cat->cur_idx; | |
| 241 | 16 | unsigned imax = i + ctx->nb_outputs; | |
| 242 | int64_t pts; | ||
| 243 | |||
| 244 | 16 | pts = cat->in[i++].pts; | |
| 245 | 22 | for (; i < imax; i++) | |
| 246 | 6 | pts = FFMAX(pts, cat->in[i].pts); | |
| 247 | 16 | cat->delta_ts += pts; | |
| 248 | 16 | *seg_delta = pts; | |
| 249 | 16 | } | |
| 250 | |||
| 251 | 3588 | static int silence_pending(AVFilterContext *ctx, unsigned out_no) | |
| 252 | { | ||
| 253 | 3588 | ConcatContext *cat = ctx->priv; | |
| 254 | |||
| 255 | 3618 | return cat->silence[out_no].nb_samples && | |
| 256 | 30 | !ff_outlink_get_status(ctx->outputs[out_no]); | |
| 257 | } | ||
| 258 | |||
| 259 | 4 | static int queue_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no, | |
| 260 | int64_t seg_delta) | ||
| 261 | { | ||
| 262 | 4 | ConcatContext *cat = ctx->priv; | |
| 263 | 4 | AVFilterLink *outlink = ctx->outputs[out_no]; | |
| 264 | 4 | struct concat_silence *silence = &cat->silence[out_no]; | |
| 265 | int64_t nb_samples; | ||
| 266 | 4 | AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate }; | |
| 267 | |||
| 268 | 4 | if (!rate_tb.den) | |
| 269 | ✗ | return AVERROR_BUG; | |
| 270 | 4 | if (cat->in[in_no].pts < INT64_MIN + seg_delta) | |
| 271 | ✗ | return AVERROR_INVALIDDATA; | |
| 272 | 4 | if (seg_delta < cat->in[in_no].pts) | |
| 273 | ✗ | return AVERROR_INVALIDDATA; | |
| 274 | 4 | nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts, | |
| 275 | outlink->time_base, rate_tb); | ||
| 276 | 4 | if (nb_samples < 0 || nb_samples > INT64_MAX - silence->nb_samples) | |
| 277 | ✗ | return AVERROR_INVALIDDATA; | |
| 278 | 4 | silence->nb_samples += nb_samples; | |
| 279 | 4 | silence->end_pts = cat->delta_ts; | |
| 280 | 4 | return 0; | |
| 281 | } | ||
| 282 | |||
| 283 | 10 | static int send_silence(AVFilterContext *ctx, unsigned out_no) | |
| 284 | { | ||
| 285 | 10 | ConcatContext *cat = ctx->priv; | |
| 286 | 10 | AVFilterLink *outlink = ctx->outputs[out_no]; | |
| 287 | 10 | struct concat_silence *silence = &cat->silence[out_no]; | |
| 288 | 10 | AVRational rate_tb = { 1, outlink->sample_rate }; | |
| 289 | 10 | int frame_nb_samples = FFMIN(FFMAX(9600, rate_tb.den / 5), /* arbitrary */ | |
| 290 | silence->nb_samples); | ||
| 291 | 10 | AVFrame *buf = ff_get_audio_buffer(outlink, frame_nb_samples); | |
| 292 | |||
| 293 | 10 | if (!buf) | |
| 294 | ✗ | return AVERROR(ENOMEM); | |
| 295 | 10 | av_samples_set_silence(buf->extended_data, 0, frame_nb_samples, | |
| 296 | 10 | outlink->ch_layout.nb_channels, outlink->format); | |
| 297 | 10 | buf->pts = silence->end_pts - | |
| 298 | 10 | av_rescale_q(silence->nb_samples, rate_tb, outlink->time_base); | |
| 299 | 10 | silence->nb_samples -= frame_nb_samples; | |
| 300 | 10 | return ff_filter_frame(outlink, buf); | |
| 301 | } | ||
| 302 | |||
| 303 | 16 | static int flush_segment(AVFilterContext *ctx) | |
| 304 | { | ||
| 305 | int ret; | ||
| 306 | 16 | ConcatContext *cat = ctx->priv; | |
| 307 | unsigned str, str_max; | ||
| 308 | int64_t seg_delta; | ||
| 309 | |||
| 310 | 16 | find_next_delta_ts(ctx, &seg_delta); | |
| 311 | 16 | cat->cur_idx += ctx->nb_outputs; | |
| 312 | 16 | cat->nb_in_active = ctx->nb_outputs; | |
| 313 | 16 | av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n", | |
| 314 | cat->delta_ts); | ||
| 315 | |||
| 316 | 16 | if (cat->cur_idx < ctx->nb_inputs) { | |
| 317 | /* pad audio streams with silence */ | ||
| 318 | 9 | str = cat->nb_streams[AVMEDIA_TYPE_VIDEO]; | |
| 319 | 9 | str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO]; | |
| 320 | 13 | for (; str < str_max; str++) { | |
| 321 | 4 | ret = queue_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str, | |
| 322 | seg_delta); | ||
| 323 | 4 | if (ret < 0) | |
| 324 | ✗ | return ret; | |
| 325 | } | ||
| 326 | } | ||
| 327 | 16 | return 0; | |
| 328 | } | ||
| 329 | |||
| 330 | 12 | static av_cold int init(AVFilterContext *ctx) | |
| 331 | { | ||
| 332 | 12 | ConcatContext *cat = ctx->priv; | |
| 333 | unsigned seg, type, str; | ||
| 334 | int ret; | ||
| 335 | |||
| 336 | /* create input pads */ | ||
| 337 | 40 | for (seg = 0; seg < cat->nb_segments; seg++) { | |
| 338 | 84 | for (type = 0; type < TYPE_ALL; type++) { | |
| 339 | 96 | for (str = 0; str < cat->nb_streams[type]; str++) { | |
| 340 | 40 | AVFilterPad pad = { | |
| 341 | .type = type, | ||
| 342 | }; | ||
| 343 | 40 | if (type == AVMEDIA_TYPE_VIDEO) | |
| 344 | 28 | pad.get_buffer.video = get_video_buffer; | |
| 345 | else | ||
| 346 | 12 | pad.get_buffer.audio = get_audio_buffer; | |
| 347 | 40 | pad.name = av_asprintf("in%d:%c%d", seg, "va"[type], str); | |
| 348 | 40 | if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0) | |
| 349 | ✗ | return ret; | |
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | /* create output pads */ | ||
| 354 | 36 | for (type = 0; type < TYPE_ALL; type++) { | |
| 355 | 40 | for (str = 0; str < cat->nb_streams[type]; str++) { | |
| 356 | 16 | AVFilterPad pad = { | |
| 357 | .type = type, | ||
| 358 | .config_props = config_output, | ||
| 359 | }; | ||
| 360 | 16 | pad.name = av_asprintf("out:%c%d", "va"[type], str); | |
| 361 | 16 | if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0) | |
| 362 | ✗ | return ret; | |
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | 12 | cat->in = av_calloc(ctx->nb_inputs, sizeof(*cat->in)); | |
| 367 | 12 | cat->silence = av_calloc(ctx->nb_outputs, sizeof(*cat->silence)); | |
| 368 | 12 | if (!cat->in || !cat->silence) | |
| 369 | ✗ | return AVERROR(ENOMEM); | |
| 370 | 12 | cat->nb_in_active = ctx->nb_outputs; | |
| 371 | 12 | return 0; | |
| 372 | } | ||
| 373 | |||
| 374 | 12 | static av_cold void uninit(AVFilterContext *ctx) | |
| 375 | { | ||
| 376 | 12 | ConcatContext *cat = ctx->priv; | |
| 377 | |||
| 378 | 12 | av_freep(&cat->in); | |
| 379 | 12 | av_freep(&cat->silence); | |
| 380 | 12 | } | |
| 381 | |||
| 382 | 1065 | static int activate(AVFilterContext *ctx) | |
| 383 | { | ||
| 384 | 1065 | ConcatContext *cat = ctx->priv; | |
| 385 | AVFrame *frame; | ||
| 386 | unsigned i, j; | ||
| 387 | int ret, status; | ||
| 388 | int64_t pts; | ||
| 389 | |||
| 390 | /* Forward status back */ | ||
| 391 | 2970 | for (i = 0; i < ctx->nb_outputs; i++) { | |
| 392 | 1905 | status = ff_outlink_get_status(ctx->outputs[i]); | |
| 393 | 1905 | if (!status) | |
| 394 | 1872 | continue; | |
| 395 | 129 | for (j = i; j < ctx->nb_inputs; j += ctx->nb_outputs) { | |
| 396 | 96 | if (!cat->in[j].eof) { | |
| 397 | ✗ | cat->in[j].eof = 1; | |
| 398 | ✗ | ff_inlink_set_status(ctx->inputs[j], status); | |
| 399 | ✗ | return 0; | |
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | } | ||
| 404 | |||
| 405 | /* Forward available frames */ | ||
| 406 | 1065 | if (cat->cur_idx < ctx->nb_inputs) { | |
| 407 | 2431 | for (i = 0; i < ctx->nb_outputs; i++) { | |
| 408 | 1849 | if (silence_pending(ctx, i)) | |
| 409 | 10 | continue; | |
| 410 | 1839 | ret = ff_inlink_consume_frame(ctx->inputs[cat->cur_idx + i], &frame); | |
| 411 | 1839 | if (ret < 0) | |
| 412 | ✗ | return ret; | |
| 413 | 1839 | if (ret) { | |
| 414 | 480 | ff_filter_set_ready(ctx, 10); | |
| 415 | 480 | return push_frame(ctx, cat->cur_idx + i, frame); | |
| 416 | } | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | /* Forward status change */ | ||
| 421 | 585 | if (cat->cur_idx < ctx->nb_inputs) { | |
| 422 | 1573 | for (i = 0; i < ctx->nb_outputs; i++) { | |
| 423 | 1013 | AVFilterLink *inlink = ctx->inputs[cat->cur_idx + i]; | |
| 424 | |||
| 425 | 1013 | if (silence_pending(ctx, i)) | |
| 426 | 10 | continue; | |
| 427 | 1003 | ret = ff_inlink_acknowledge_status(inlink, &status, &pts); | |
| 428 | /* TODO use pts */ | ||
| 429 | 1003 | if (ret > 0) { | |
| 430 | 22 | close_input(ctx, cat->cur_idx + i); | |
| 431 | 22 | if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) { | |
| 432 | 9 | int64_t eof_pts = cat->delta_ts; | |
| 433 | |||
| 434 | 9 | eof_pts += av_rescale_q(pts, inlink->time_base, ctx->outputs[i]->time_base); | |
| 435 | 9 | ff_outlink_set_status(ctx->outputs[i], status, eof_pts); | |
| 436 | } | ||
| 437 | 22 | if (!cat->nb_in_active) { | |
| 438 | 16 | ret = flush_segment(ctx); | |
| 439 | 16 | if (ret < 0) | |
| 440 | ✗ | return ret; | |
| 441 | } | ||
| 442 | 22 | ff_filter_set_ready(ctx, 10); | |
| 443 | 22 | return 0; | |
| 444 | } | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | 563 | ret = FFERROR_NOT_READY; | |
| 449 | 1335 | for (i = 0; i < ctx->nb_outputs; i++) { | |
| 450 | 890 | if (ff_outlink_frame_wanted(ctx->outputs[i])) { | |
| 451 | 510 | if (silence_pending(ctx, i)) | |
| 452 | 10 | return send_silence(ctx, i); | |
| 453 | 500 | if (cat->in[cat->cur_idx + i].eof) { | |
| 454 | 324 | for (j = 0; j < ctx->nb_outputs; j++) { | |
| 455 | 216 | if (silence_pending(ctx, j)) | |
| 456 | ✗ | return send_silence(ctx, j); | |
| 457 | 216 | if (!cat->in[cat->cur_idx + j].eof) | |
| 458 | 108 | ff_inlink_request_frame(ctx->inputs[cat->cur_idx + j]); | |
| 459 | } | ||
| 460 | 108 | return 0; | |
| 461 | } else { | ||
| 462 | 392 | ff_inlink_request_frame(ctx->inputs[cat->cur_idx + i]); | |
| 463 | 392 | ret = 0; | |
| 464 | } | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | 445 | return ret; | |
| 469 | } | ||
| 470 | |||
| 471 | 1 | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
| 472 | char *res, int res_len, int flags) | ||
| 473 | { | ||
| 474 | 1 | int ret = AVERROR(ENOSYS); | |
| 475 | |||
| 476 | 1 | if (!strcmp(cmd, "next")) { | |
| 477 | 1 | ConcatContext *cat = ctx->priv; | |
| 478 | |||
| 479 | 1 | av_log(ctx, AV_LOG_VERBOSE, "Command received: next\n"); | |
| 480 | 1 | if (cat->cur_idx + ctx->nb_outputs >= ctx->nb_inputs) { | |
| 481 | 1 | av_log(ctx, AV_LOG_WARNING, "There is no next segment\n"); | |
| 482 | 1 | return AVERROR(EINVAL); | |
| 483 | } | ||
| 484 | ✗ | return flush_segment(ctx); | |
| 485 | } | ||
| 486 | |||
| 487 | ✗ | return ret; | |
| 488 | } | ||
| 489 | |||
| 490 | const FFFilter ff_avf_concat = { | ||
| 491 | .p.name = "concat", | ||
| 492 | .p.description = NULL_IF_CONFIG_SMALL("Concatenate audio and video streams."), | ||
| 493 | .p.inputs = NULL, | ||
| 494 | .p.outputs = NULL, | ||
| 495 | .p.priv_class = &concat_class, | ||
| 496 | .p.flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS, | ||
| 497 | .init = init, | ||
| 498 | .uninit = uninit, | ||
| 499 | .activate = activate, | ||
| 500 | .priv_size = sizeof(ConcatContext), | ||
| 501 | FILTER_QUERY_FUNC2(query_formats), | ||
| 502 | .process_command = process_command, | ||
| 503 | }; | ||
| 504 |