]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_interleave.c
avfilter/f_interleave: switch to activate
[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     int64_t pts;
41 } InterleaveContext;
42
43 #define OFFSET(x) offsetof(InterleaveContext, x)
44
45 #define DEFINE_OPTIONS(filt_name, flags_)                           \
46 static const AVOption filt_name##_options[] = {                     \
47    { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
48    { "n",         "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
49    { NULL }                                                         \
50 }
51
52 static int activate(AVFilterContext *ctx)
53 {
54     AVFilterLink *outlink = ctx->outputs[0];
55     InterleaveContext *s = ctx->priv;
56     int64_t q_pts, pts = INT64_MAX;
57     int i, nb_eofs = 0, input_idx = -1;
58
59     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
60
61     for (i = 0; i < ctx->nb_inputs; i++) {
62         if (!ff_outlink_get_status(ctx->inputs[i]) &&
63             !ff_inlink_queued_frames(ctx->inputs[i]))
64             break;
65     }
66
67     if (i == ctx->nb_inputs) {
68         for (i = 0; i < ctx->nb_inputs; i++) {
69             AVFrame *frame;
70
71             if (ff_outlink_get_status(ctx->inputs[i]))
72                 continue;
73
74             frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
75             if (frame->pts == AV_NOPTS_VALUE) {
76                 int ret;
77
78                 av_log(ctx, AV_LOG_WARNING,
79                        "NOPTS value for input frame cannot be accepted, frame discarded\n");
80                 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
81                 if (ret < 0)
82                     return ret;
83                 av_frame_free(&frame);
84                 return AVERROR_INVALIDDATA;
85             }
86
87             q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
88             if (q_pts < pts) {
89                 pts = q_pts;
90                 input_idx = i;
91             }
92         }
93
94         if (input_idx >= 0) {
95             AVFrame *frame;
96             int ret;
97
98             ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
99             if (ret < 0)
100                 return ret;
101
102             frame->pts = s->pts = pts;
103             return ff_filter_frame(outlink, frame);
104         }
105     }
106
107     for (i = 0; i < ctx->nb_inputs; i++) {
108         if (ff_inlink_queued_frames(ctx->inputs[i]))
109             continue;
110         if (ff_outlink_frame_wanted(outlink) &&
111             !ff_outlink_get_status(ctx->inputs[i])) {
112             ff_inlink_request_frame(ctx->inputs[i]);
113             return 0;
114         }
115         nb_eofs++;
116     }
117
118     if (nb_eofs == ctx->nb_inputs) {
119         ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
120         return 0;
121     }
122
123     return FFERROR_NOT_READY;
124 }
125
126 static av_cold int init(AVFilterContext *ctx)
127 {
128     InterleaveContext *s = ctx->priv;
129     const AVFilterPad *outpad = &ctx->filter->outputs[0];
130     int i, ret;
131
132     for (i = 0; i < s->nb_inputs; i++) {
133         AVFilterPad inpad = { 0 };
134
135         inpad.name = av_asprintf("input%d", i);
136         if (!inpad.name)
137             return AVERROR(ENOMEM);
138         inpad.type         = outpad->type;
139
140         switch (outpad->type) {
141         case AVMEDIA_TYPE_VIDEO:
142             inpad.get_video_buffer = ff_null_get_video_buffer; break;
143         case AVMEDIA_TYPE_AUDIO:
144             inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
145         default:
146             av_assert0(0);
147         }
148         if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
149             av_freep(&inpad.name);
150             return ret;
151         }
152     }
153
154     return 0;
155 }
156
157 static av_cold void uninit(AVFilterContext *ctx)
158 {
159     for (int i = 0; i < ctx->nb_inputs; i++)
160         av_freep(&ctx->input_pads[i].name);
161 }
162
163 static int config_output(AVFilterLink *outlink)
164 {
165     AVFilterContext *ctx = outlink->src;
166     AVFilterLink *inlink0 = ctx->inputs[0];
167     int i;
168
169     if (outlink->type == AVMEDIA_TYPE_VIDEO) {
170         outlink->time_base           = AV_TIME_BASE_Q;
171         outlink->w                   = inlink0->w;
172         outlink->h                   = inlink0->h;
173         outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
174         outlink->format              = inlink0->format;
175         outlink->frame_rate = (AVRational) {1, 0};
176         for (i = 1; i < ctx->nb_inputs; i++) {
177             AVFilterLink *inlink = ctx->inputs[i];
178
179             if (outlink->w                       != inlink->w                       ||
180                 outlink->h                       != inlink->h                       ||
181                 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
182                 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
183                 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
184                        "(size %dx%d, SAR %d:%d) do not match the corresponding "
185                        "output link parameters (%dx%d, SAR %d:%d)\n",
186                        ctx->input_pads[i].name, inlink->w, inlink->h,
187                        inlink->sample_aspect_ratio.num,
188                        inlink->sample_aspect_ratio.den,
189                        outlink->w, outlink->h,
190                        outlink->sample_aspect_ratio.num,
191                        outlink->sample_aspect_ratio.den);
192                 return AVERROR(EINVAL);
193             }
194         }
195     }
196     return 0;
197 }
198
199 #if CONFIG_INTERLEAVE_FILTER
200
201 DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
202 AVFILTER_DEFINE_CLASS(interleave);
203
204 static const AVFilterPad interleave_outputs[] = {
205     {
206         .name          = "default",
207         .type          = AVMEDIA_TYPE_VIDEO,
208         .config_props  = config_output,
209     },
210     { NULL }
211 };
212
213 AVFilter ff_vf_interleave = {
214     .name        = "interleave",
215     .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
216     .priv_size   = sizeof(InterleaveContext),
217     .init        = init,
218     .uninit      = uninit,
219     .activate    = activate,
220     .outputs     = interleave_outputs,
221     .priv_class  = &interleave_class,
222     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
223 };
224
225 #endif
226
227 #if CONFIG_AINTERLEAVE_FILTER
228
229 DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
230 AVFILTER_DEFINE_CLASS(ainterleave);
231
232 static const AVFilterPad ainterleave_outputs[] = {
233     {
234         .name          = "default",
235         .type          = AVMEDIA_TYPE_AUDIO,
236         .config_props  = config_output,
237     },
238     { NULL }
239 };
240
241 AVFilter ff_af_ainterleave = {
242     .name        = "ainterleave",
243     .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
244     .priv_size   = sizeof(InterleaveContext),
245     .init        = init,
246     .uninit      = uninit,
247     .activate    = activate,
248     .outputs     = ainterleave_outputs,
249     .priv_class  = &ainterleave_class,
250     .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
251 };
252
253 #endif