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"
30 #define FF_INTERNAL_FIELDS 1
31 #include "framequeue.h"
34 #include "bufferqueue.h"
40 typedef struct InterleaveContext {
43 struct FFBufQueue *queues;
46 #define OFFSET(x) offsetof(InterleaveContext, x)
48 #define DEFINE_OPTIONS(filt_name, flags_) \
49 static const AVOption filt_name##_options[] = { \
50 { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
51 { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
55 inline static int push_frame(AVFilterContext *ctx)
57 InterleaveContext *s = ctx->priv;
59 int i, queue_idx = -1;
60 int64_t pts_min = INT64_MAX;
62 /* look for oldest frame */
63 for (i = 0; i < ctx->nb_inputs; i++) {
64 struct FFBufQueue *q = &s->queues[i];
66 if (!q->available && !ctx->inputs[i]->status_out)
69 frame = ff_bufqueue_peek(q, 0);
70 if (frame->pts < pts_min) {
77 /* all inputs are closed */
81 frame = ff_bufqueue_get(&s->queues[queue_idx]);
82 av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
83 queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
84 return ff_filter_frame(ctx->outputs[0], frame);
87 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
89 AVFilterContext *ctx = inlink->dst;
90 InterleaveContext *s = ctx->priv;
91 unsigned in_no = FF_INLINK_IDX(inlink);
93 if (frame->pts == AV_NOPTS_VALUE) {
94 av_log(ctx, AV_LOG_WARNING,
95 "NOPTS value for input frame cannot be accepted, frame discarded\n");
96 av_frame_free(&frame);
97 return AVERROR_INVALIDDATA;
101 frame->pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
102 av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d available:%d\n",
103 frame->pts * av_q2d(AV_TIME_BASE_Q), in_no, s->queues[in_no].available);
104 ff_bufqueue_add(ctx, &s->queues[in_no], frame);
106 return push_frame(ctx);
109 static av_cold int init(AVFilterContext *ctx)
111 InterleaveContext *s = ctx->priv;
112 const AVFilterPad *outpad = &ctx->filter->outputs[0];
115 s->queues = av_calloc(s->nb_inputs, sizeof(s->queues[0]));
117 return AVERROR(ENOMEM);
119 for (i = 0; i < s->nb_inputs; i++) {
120 AVFilterPad inpad = { 0 };
122 inpad.name = av_asprintf("input%d", i);
124 return AVERROR(ENOMEM);
125 inpad.type = outpad->type;
126 inpad.filter_frame = filter_frame;
128 switch (outpad->type) {
129 case AVMEDIA_TYPE_VIDEO:
130 inpad.get_video_buffer = ff_null_get_video_buffer; break;
131 case AVMEDIA_TYPE_AUDIO:
132 inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
136 if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
137 av_freep(&inpad.name);
145 static av_cold void uninit(AVFilterContext *ctx)
147 InterleaveContext *s = ctx->priv;
150 for (i = 0; i < ctx->nb_inputs; i++) {
151 ff_bufqueue_discard_all(&s->queues[i]);
152 av_freep(&s->queues[i]);
153 av_freep(&ctx->input_pads[i].name);
157 static int config_output(AVFilterLink *outlink)
159 AVFilterContext *ctx = outlink->src;
160 AVFilterLink *inlink0 = ctx->inputs[0];
163 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
164 outlink->time_base = AV_TIME_BASE_Q;
165 outlink->w = inlink0->w;
166 outlink->h = inlink0->h;
167 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
168 outlink->format = inlink0->format;
169 outlink->frame_rate = (AVRational) {1, 0};
170 for (i = 1; i < ctx->nb_inputs; i++) {
171 AVFilterLink *inlink = ctx->inputs[i];
173 if (outlink->w != inlink->w ||
174 outlink->h != inlink->h ||
175 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
176 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
177 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
178 "(size %dx%d, SAR %d:%d) do not match the corresponding "
179 "output link parameters (%dx%d, SAR %d:%d)\n",
180 ctx->input_pads[i].name, inlink->w, inlink->h,
181 inlink->sample_aspect_ratio.num,
182 inlink->sample_aspect_ratio.den,
183 outlink->w, outlink->h,
184 outlink->sample_aspect_ratio.num,
185 outlink->sample_aspect_ratio.den);
186 return AVERROR(EINVAL);
193 static int request_frame(AVFilterLink *outlink)
195 AVFilterContext *ctx = outlink->src;
196 InterleaveContext *s = ctx->priv;
199 for (i = 0; i < ctx->nb_inputs; i++) {
200 if (!s->queues[i].available && !ctx->inputs[i]->status_out) {
201 ret = ff_request_frame(ctx->inputs[i]);
202 if (ret != AVERROR_EOF)
207 return push_frame(ctx);
210 #if CONFIG_INTERLEAVE_FILTER
212 DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
213 AVFILTER_DEFINE_CLASS(interleave);
215 static const AVFilterPad interleave_outputs[] = {
218 .type = AVMEDIA_TYPE_VIDEO,
219 .config_props = config_output,
220 .request_frame = request_frame,
225 AVFilter ff_vf_interleave = {
226 .name = "interleave",
227 .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
228 .priv_size = sizeof(InterleaveContext),
231 .outputs = interleave_outputs,
232 .priv_class = &interleave_class,
233 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
238 #if CONFIG_AINTERLEAVE_FILTER
240 DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
241 AVFILTER_DEFINE_CLASS(ainterleave);
243 static const AVFilterPad ainterleave_outputs[] = {
246 .type = AVMEDIA_TYPE_AUDIO,
247 .config_props = config_output,
248 .request_frame = request_frame,
253 AVFilter ff_af_ainterleave = {
254 .name = "ainterleave",
255 .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
256 .priv_size = sizeof(InterleaveContext),
259 .outputs = ainterleave_outputs,
260 .priv_class = &ainterleave_class,
261 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,