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