2 * Copyright (c) 2013 Stefano Sabatini
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. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * audio and video interleaver
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
37 typedef struct InterleaveContext {
44 #define DURATION_LONGEST 0
45 #define DURATION_SHORTEST 1
46 #define DURATION_FIRST 2
48 #define OFFSET(x) offsetof(InterleaveContext, x)
50 #define DEFINE_OPTIONS(filt_name, flags_) \
51 static const AVOption filt_name##_options[] = { \
52 { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
53 { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
54 { "duration", "how to determine the end-of-stream", \
55 OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, flags_, "duration" }, \
56 { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, flags_, "duration" }, \
57 { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, flags_, "duration" }, \
58 { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, flags_, "duration" }, \
62 static int activate(AVFilterContext *ctx)
64 AVFilterLink *outlink = ctx->outputs[0];
65 InterleaveContext *s = ctx->priv;
66 int64_t q_pts, pts = INT64_MAX;
67 int i, nb_eofs = 0, input_idx = -1;
68 int nb_inputs_with_frames = 0;
70 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
72 for (i = 0; i < ctx->nb_inputs; i++) {
73 if (!ff_outlink_get_status(ctx->inputs[i])) {
74 if (!ff_inlink_queued_frames(ctx->inputs[i]))
76 nb_inputs_with_frames++;
80 if (nb_inputs_with_frames > 0) {
81 for (i = 0; i < ctx->nb_inputs; i++) {
84 if (ff_inlink_queued_frames(ctx->inputs[i]) == 0)
87 frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
88 if (frame->pts == AV_NOPTS_VALUE) {
91 av_log(ctx, AV_LOG_WARNING,
92 "NOPTS value for input frame cannot be accepted, frame discarded\n");
93 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
96 av_frame_free(&frame);
97 return AVERROR_INVALIDDATA;
100 q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
107 if (input_idx >= 0) {
111 ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
115 frame->pts = s->pts = pts;
116 return ff_filter_frame(outlink, frame);
120 for (i = 0; i < ctx->nb_inputs; i++)
121 nb_eofs += !!ff_outlink_get_status(ctx->inputs[i]);
123 if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
124 (nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
125 (ff_outlink_get_status(ctx->inputs[0]) && s->duration_mode == DURATION_FIRST)) {
126 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
130 for (i = 0; i < ctx->nb_inputs; i++) {
131 if (ff_inlink_queued_frames(ctx->inputs[i]))
133 if (ff_outlink_frame_wanted(outlink) &&
134 !ff_outlink_get_status(ctx->inputs[i])) {
135 ff_inlink_request_frame(ctx->inputs[i]);
140 return FFERROR_NOT_READY;
143 static av_cold int init(AVFilterContext *ctx)
145 InterleaveContext *s = ctx->priv;
146 const AVFilterPad *outpad = &ctx->filter->outputs[0];
149 for (i = 0; i < s->nb_inputs; i++) {
150 AVFilterPad inpad = { 0 };
152 inpad.name = av_asprintf("input%d", i);
154 return AVERROR(ENOMEM);
155 inpad.type = outpad->type;
157 switch (outpad->type) {
158 case AVMEDIA_TYPE_VIDEO:
159 inpad.get_video_buffer = ff_null_get_video_buffer; break;
160 case AVMEDIA_TYPE_AUDIO:
161 inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
165 if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
166 av_freep(&inpad.name);
174 static av_cold void uninit(AVFilterContext *ctx)
176 for (int i = 0; i < ctx->nb_inputs; i++)
177 av_freep(&ctx->input_pads[i].name);
180 static int config_output(AVFilterLink *outlink)
182 AVFilterContext *ctx = outlink->src;
183 AVFilterLink *inlink0 = ctx->inputs[0];
186 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
187 outlink->time_base = AV_TIME_BASE_Q;
188 outlink->w = inlink0->w;
189 outlink->h = inlink0->h;
190 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
191 outlink->format = inlink0->format;
192 outlink->frame_rate = (AVRational) {1, 0};
193 for (i = 1; i < ctx->nb_inputs; i++) {
194 AVFilterLink *inlink = ctx->inputs[i];
196 if (outlink->w != inlink->w ||
197 outlink->h != inlink->h ||
198 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
199 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
200 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
201 "(size %dx%d, SAR %d:%d) do not match the corresponding "
202 "output link parameters (%dx%d, SAR %d:%d)\n",
203 ctx->input_pads[i].name, inlink->w, inlink->h,
204 inlink->sample_aspect_ratio.num,
205 inlink->sample_aspect_ratio.den,
206 outlink->w, outlink->h,
207 outlink->sample_aspect_ratio.num,
208 outlink->sample_aspect_ratio.den);
209 return AVERROR(EINVAL);
216 #if CONFIG_INTERLEAVE_FILTER
218 DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
219 AVFILTER_DEFINE_CLASS(interleave);
221 static const AVFilterPad interleave_outputs[] = {
224 .type = AVMEDIA_TYPE_VIDEO,
225 .config_props = config_output,
230 AVFilter ff_vf_interleave = {
231 .name = "interleave",
232 .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
233 .priv_size = sizeof(InterleaveContext),
236 .activate = activate,
237 .outputs = interleave_outputs,
238 .priv_class = &interleave_class,
239 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
244 #if CONFIG_AINTERLEAVE_FILTER
246 DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
247 AVFILTER_DEFINE_CLASS(ainterleave);
249 static const AVFilterPad ainterleave_outputs[] = {
252 .type = AVMEDIA_TYPE_AUDIO,
253 .config_props = config_output,
258 AVFilter ff_af_ainterleave = {
259 .name = "ainterleave",
260 .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
261 .priv_size = sizeof(InterleaveContext),
264 .activate = activate,
265 .outputs = ainterleave_outputs,
266 .priv_class = &ainterleave_class,
267 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,