]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_streamselect.c
avfilter/f_streamselect: add check case when nothing is done
[ffmpeg] / libavfilter / f_streamselect.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/avstring.h"
20 #include "libavutil/internal.h"
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "filters.h"
25 #include "formats.h"
26 #include "framesync.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct StreamSelectContext {
31     const AVClass *class;
32     int nb_inputs;
33     char *map_str;
34     int *map;
35     int nb_map;
36     int is_audio;
37     int64_t *last_pts;
38     AVFrame **frames;
39     FFFrameSync fs;
40 } StreamSelectContext;
41
42 #define OFFSET(x) offsetof(StreamSelectContext, x)
43 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
44 static const AVOption streamselect_options[] = {
45     { "inputs",  "number of input streams",           OFFSET(nb_inputs),  AV_OPT_TYPE_INT,    {.i64=2},    2, INT_MAX,  .flags=FLAGS },
46     { "map",     "input indexes to remap to outputs", OFFSET(map_str),    AV_OPT_TYPE_STRING, {.str=NULL},              .flags=FLAGS },
47     { NULL }
48 };
49
50 AVFILTER_DEFINE_CLASS(streamselect);
51
52 static int process_frame(FFFrameSync *fs)
53 {
54     AVFilterContext *ctx = fs->parent;
55     StreamSelectContext *s = fs->opaque;
56     AVFrame **in = s->frames;
57     int i, j, ret = 0, have_out = 0;
58
59     for (i = 0; i < ctx->nb_inputs; i++) {
60         if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
61             return ret;
62     }
63
64     for (j = 0; j < ctx->nb_inputs; j++) {
65         for (i = 0; i < s->nb_map; i++) {
66             if (s->map[i] == j) {
67                 AVFrame *out;
68
69                 if (s->is_audio && s->last_pts[j] == in[j]->pts &&
70                     ctx->outputs[i]->frame_count_in > 0)
71                     continue;
72                 out = av_frame_clone(in[j]);
73                 if (!out)
74                     return AVERROR(ENOMEM);
75
76                 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
77                 s->last_pts[j] = in[j]->pts;
78                 ret = ff_filter_frame(ctx->outputs[i], out);
79                 have_out = 1;
80                 if (ret < 0)
81                     return ret;
82             }
83         }
84     }
85
86     if (!have_out)
87         ff_filter_set_ready(ctx, 100);
88     return ret;
89 }
90
91 static int activate(AVFilterContext *ctx)
92 {
93     StreamSelectContext *s = ctx->priv;
94     return ff_framesync_activate(&s->fs);
95 }
96
97 static int config_output(AVFilterLink *outlink)
98 {
99     AVFilterContext *ctx = outlink->src;
100     StreamSelectContext *s = ctx->priv;
101     const int outlink_idx = FF_OUTLINK_IDX(outlink);
102     const int inlink_idx  = s->map[outlink_idx];
103     AVFilterLink *inlink = ctx->inputs[inlink_idx];
104     FFFrameSyncIn *in;
105     int i, ret;
106
107     av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
108            "with settings from input link %d\n",
109            outlink_idx, inlink_idx);
110
111     switch (outlink->type) {
112     case AVMEDIA_TYPE_VIDEO:
113         outlink->w = inlink->w;
114         outlink->h = inlink->h;
115         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
116         outlink->frame_rate = inlink->frame_rate;
117         break;
118     case AVMEDIA_TYPE_AUDIO:
119         outlink->sample_rate    = inlink->sample_rate;
120         outlink->channels       = inlink->channels;
121         outlink->channel_layout = inlink->channel_layout;
122         break;
123     }
124
125     outlink->time_base = inlink->time_base;
126     outlink->format = inlink->format;
127
128     if (s->fs.opaque == s)
129         return 0;
130
131     if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
132         return ret;
133
134     in = s->fs.in;
135     s->fs.opaque = s;
136     s->fs.on_event = process_frame;
137
138     for (i = 0; i < ctx->nb_inputs; i++) {
139         in[i].time_base = ctx->inputs[i]->time_base;
140         in[i].sync      = 1;
141         in[i].before    = EXT_STOP;
142         in[i].after     = EXT_STOP;
143     }
144
145     s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
146     if (!s->frames)
147         return AVERROR(ENOMEM);
148
149     return ff_framesync_configure(&s->fs);
150 }
151
152 static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
153 {
154     const char *padtype = is_input ? "in" : "out";
155     int i = 0, ret = 0;
156
157     for (i = 0; i < nb_pads; i++) {
158         AVFilterPad pad = { 0 };
159
160         pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
161
162         pad.name = av_asprintf("%sput%d", padtype, i);
163         if (!pad.name)
164             return AVERROR(ENOMEM);
165
166         av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
167
168         if (is_input) {
169             ret = ff_insert_inpad(ctx, i, &pad);
170         } else {
171             pad.config_props  = config_output;
172             ret = ff_insert_outpad(ctx, i, &pad);
173         }
174
175         if (ret < 0) {
176             av_freep(&pad.name);
177             return ret;
178         }
179     }
180
181     return 0;
182 }
183
184 static int parse_mapping(AVFilterContext *ctx, const char *map)
185 {
186     StreamSelectContext *s = ctx->priv;
187     int *new_map;
188     int new_nb_map = 0;
189
190     if (!map) {
191         av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
192         return AVERROR(EINVAL);
193     }
194
195     new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
196     if (!new_map)
197         return AVERROR(ENOMEM);
198
199     while (1) {
200         char *p;
201         const int n = strtol(map, &p, 0);
202
203         av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
204
205         if (map == p)
206             break;
207         map = p;
208
209         if (new_nb_map >= s->nb_inputs) {
210             av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
211                    "input pads available\n", s->nb_inputs);
212             av_free(new_map);
213             return AVERROR(EINVAL);
214         }
215
216         if (n < 0 || n >= ctx->nb_inputs) {
217             av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
218                    "(there is only %d input streams defined)\n",
219                    n, s->nb_inputs);
220             av_free(new_map);
221             return AVERROR(EINVAL);
222         }
223
224         av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
225         new_map[new_nb_map++] = n;
226     }
227
228     if (!new_nb_map) {
229         av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
230         av_free(new_map);
231         return AVERROR(EINVAL);
232     }
233
234     av_freep(&s->map);
235     s->map = new_map;
236     s->nb_map = new_nb_map;
237
238     av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
239
240     return 0;
241 }
242
243 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
244                            char *res, int res_len, int flags)
245 {
246     if (!strcmp(cmd, "map")) {
247         int ret = parse_mapping(ctx, args);
248
249         if (ret < 0)
250             return ret;
251         return avfilter_config_links(ctx);
252     }
253     return AVERROR(ENOSYS);
254 }
255
256 static av_cold int init(AVFilterContext *ctx)
257 {
258     StreamSelectContext *s = ctx->priv;
259     int ret, nb_outputs = 0;
260     char *map = s->map_str;
261
262     if (!strcmp(ctx->filter->name, "astreamselect"))
263         s->is_audio = 1;
264
265     for (; map;) {
266         char *p;
267
268         strtol(map, &p, 0);
269         if (map == p)
270             break;
271         nb_outputs++;
272         map = p;
273     }
274
275     s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
276     if (!s->last_pts)
277         return AVERROR(ENOMEM);
278
279     if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
280         (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
281         return ret;
282
283     av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
284            ctx->nb_inputs, ctx->nb_outputs);
285
286     return parse_mapping(ctx, s->map_str);
287 }
288
289 static av_cold void uninit(AVFilterContext *ctx)
290 {
291     StreamSelectContext *s = ctx->priv;
292
293     av_freep(&s->last_pts);
294     av_freep(&s->map);
295     av_freep(&s->frames);
296     ff_framesync_uninit(&s->fs);
297 }
298
299 static int query_formats(AVFilterContext *ctx)
300 {
301     AVFilterFormats *formats, *rates = NULL;
302     AVFilterChannelLayouts *layouts = NULL;
303     int ret, i;
304
305     for (i = 0; i < ctx->nb_inputs; i++) {
306         formats = ff_all_formats(ctx->inputs[i]->type);
307         if ((ret = ff_set_common_formats(ctx, formats)) < 0)
308             return ret;
309
310         if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
311             rates = ff_all_samplerates();
312             if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
313                 return ret;
314             layouts = ff_all_channel_counts();
315             if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
316                 return ret;
317         }
318     }
319
320     return 0;
321 }
322
323 AVFilter ff_vf_streamselect = {
324     .name            = "streamselect",
325     .description     = NULL_IF_CONFIG_SMALL("Select video streams"),
326     .init            = init,
327     .query_formats   = query_formats,
328     .process_command = process_command,
329     .uninit          = uninit,
330     .activate        = activate,
331     .priv_size       = sizeof(StreamSelectContext),
332     .priv_class      = &streamselect_class,
333     .flags           = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
334 };
335
336 #define astreamselect_options streamselect_options
337 AVFILTER_DEFINE_CLASS(astreamselect);
338
339 AVFilter ff_af_astreamselect = {
340     .name            = "astreamselect",
341     .description     = NULL_IF_CONFIG_SMALL("Select audio streams"),
342     .init            = init,
343     .query_formats   = query_formats,
344     .process_command = process_command,
345     .uninit          = uninit,
346     .activate        = activate,
347     .priv_size       = sizeof(StreamSelectContext),
348     .priv_class      = &astreamselect_class,
349     .flags           = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
350 };