]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_interleave.c
avfilter/f_interleave: add duration option
[ffmpeg] / libavfilter / f_interleave.c
1 /*
2  * Copyright (c) 2013 Stefano Sabatini
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.  See the GNU
14  * Lesser General Public License for more details.
15  *
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
19  */
20
21 /**
22  * @file
23  * audio and video interleaver
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "filters.h"
33 #include "internal.h"
34 #include "audio.h"
35 #include "video.h"
36
37 typedef struct InterleaveContext {
38     const AVClass *class;
39     int nb_inputs;
40     int duration_mode;
41     int64_t pts;
42 } InterleaveContext;
43
44 #define DURATION_LONGEST  0
45 #define DURATION_SHORTEST 1
46 #define DURATION_FIRST    2
47
48 #define OFFSET(x) offsetof(InterleaveContext, x)
49
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" }, \
59    { NULL }                                                         \
60 }
61
62 static int activate(AVFilterContext *ctx)
63 {
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, nb_active_inputs = 0;
68     int nb_inputs_with_frames = 0;
69
70     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
71
72     for (i = 0; i < ctx->nb_inputs; i++) {
73         if (!ff_outlink_get_status(ctx->inputs[i])) {
74             nb_active_inputs++;
75             if (!ff_inlink_queued_frames(ctx->inputs[i]))
76                 break;
77             nb_inputs_with_frames++;
78         }
79     }
80
81     if (nb_active_inputs > 0 && nb_active_inputs == nb_inputs_with_frames) {
82         for (i = 0; i < ctx->nb_inputs; i++) {
83             AVFrame *frame;
84
85             if (ff_outlink_get_status(ctx->inputs[i]) ||
86                 ff_inlink_queued_frames(ctx->inputs[i]) == 0)
87                 continue;
88
89             frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
90             if (frame->pts == AV_NOPTS_VALUE) {
91                 int ret;
92
93                 av_log(ctx, AV_LOG_WARNING,
94                        "NOPTS value for input frame cannot be accepted, frame discarded\n");
95                 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
96                 if (ret < 0)
97                     return ret;
98                 av_frame_free(&frame);
99                 return AVERROR_INVALIDDATA;
100             }
101
102             q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
103             if (q_pts < pts) {
104                 pts = q_pts;
105                 input_idx = i;
106             }
107         }
108
109         if (input_idx >= 0) {
110             AVFrame *frame;
111             int ret;
112
113             ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
114             if (ret < 0)
115                 return ret;
116
117             frame->pts = s->pts = pts;
118             return ff_filter_frame(outlink, frame);
119         }
120     }
121
122     for (i = 0; i < ctx->nb_inputs; i++)
123         nb_eofs += !!ff_outlink_get_status(ctx->inputs[i]);
124
125     if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
126         (nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
127         (ff_outlink_get_status(ctx->inputs[0]) && s->duration_mode == DURATION_FIRST)) {
128         ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
129         return 0;
130     }
131
132     for (i = 0; i < ctx->nb_inputs; i++) {
133         if (ff_inlink_queued_frames(ctx->inputs[i]))
134             continue;
135         if (ff_outlink_frame_wanted(outlink) &&
136             !ff_outlink_get_status(ctx->inputs[i])) {
137             ff_inlink_request_frame(ctx->inputs[i]);
138             return 0;
139         }
140     }
141
142     return FFERROR_NOT_READY;
143 }
144
145 static av_cold int init(AVFilterContext *ctx)
146 {
147     InterleaveContext *s = ctx->priv;
148     const AVFilterPad *outpad = &ctx->filter->outputs[0];
149     int i, ret;
150
151     for (i = 0; i < s->nb_inputs; i++) {
152         AVFilterPad inpad = { 0 };
153
154         inpad.name = av_asprintf("input%d", i);
155         if (!inpad.name)
156             return AVERROR(ENOMEM);
157         inpad.type         = outpad->type;
158
159         switch (outpad->type) {
160         case AVMEDIA_TYPE_VIDEO:
161             inpad.get_video_buffer = ff_null_get_video_buffer; break;
162         case AVMEDIA_TYPE_AUDIO:
163             inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
164         default:
165             av_assert0(0);
166         }
167         if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
168             av_freep(&inpad.name);
169             return ret;
170         }
171     }
172
173     return 0;
174 }
175
176 static av_cold void uninit(AVFilterContext *ctx)
177 {
178     for (int i = 0; i < ctx->nb_inputs; i++)
179         av_freep(&ctx->input_pads[i].name);
180 }
181
182 static int config_output(AVFilterLink *outlink)
183 {
184     AVFilterContext *ctx = outlink->src;
185     AVFilterLink *inlink0 = ctx->inputs[0];
186     int i;
187
188     if (outlink->type == AVMEDIA_TYPE_VIDEO) {
189         outlink->time_base           = AV_TIME_BASE_Q;
190         outlink->w                   = inlink0->w;
191         outlink->h                   = inlink0->h;
192         outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
193         outlink->format              = inlink0->format;
194         outlink->frame_rate = (AVRational) {1, 0};
195         for (i = 1; i < ctx->nb_inputs; i++) {
196             AVFilterLink *inlink = ctx->inputs[i];
197
198             if (outlink->w                       != inlink->w                       ||
199                 outlink->h                       != inlink->h                       ||
200                 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
201                 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
202                 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
203                        "(size %dx%d, SAR %d:%d) do not match the corresponding "
204                        "output link parameters (%dx%d, SAR %d:%d)\n",
205                        ctx->input_pads[i].name, inlink->w, inlink->h,
206                        inlink->sample_aspect_ratio.num,
207                        inlink->sample_aspect_ratio.den,
208                        outlink->w, outlink->h,
209                        outlink->sample_aspect_ratio.num,
210                        outlink->sample_aspect_ratio.den);
211                 return AVERROR(EINVAL);
212             }
213         }
214     }
215     return 0;
216 }
217
218 #if CONFIG_INTERLEAVE_FILTER
219
220 DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
221 AVFILTER_DEFINE_CLASS(interleave);
222
223 static const AVFilterPad interleave_outputs[] = {
224     {
225         .name          = "default",
226         .type          = AVMEDIA_TYPE_VIDEO,
227         .config_props  = config_output,
228     },
229     { NULL }
230 };
231
232 AVFilter ff_vf_interleave = {
233     .name        = "interleave",
234     .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
235     .priv_size   = sizeof(InterleaveContext),
236     .init        = init,
237     .uninit      = uninit,
238     .activate    = activate,
239     .outputs     = interleave_outputs,
240     .priv_class  = &interleave_class,
241     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
242 };
243
244 #endif
245
246 #if CONFIG_AINTERLEAVE_FILTER
247
248 DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
249 AVFILTER_DEFINE_CLASS(ainterleave);
250
251 static const AVFilterPad ainterleave_outputs[] = {
252     {
253         .name          = "default",
254         .type          = AVMEDIA_TYPE_AUDIO,
255         .config_props  = config_output,
256     },
257     { NULL }
258 };
259
260 AVFilter ff_af_ainterleave = {
261     .name        = "ainterleave",
262     .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
263     .priv_size   = sizeof(InterleaveContext),
264     .init        = init,
265     .uninit      = uninit,
266     .activate    = activate,
267     .outputs     = ainterleave_outputs,
268     .priv_class  = &ainterleave_class,
269     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
270 };
271
272 #endif