FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_streamselect.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 0 160 0.0%
Functions: 0 9 0.0%
Branches: 0 83 0.0%

Line Branch Exec Source
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 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
45 static const AVOption streamselect_options[] = {
46 { "inputs", "number of input streams", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags=FLAGS },
47 { "map", "input indexes to remap to outputs", OFFSET(map_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=TFLAGS },
48 { NULL }
49 };
50
51 AVFILTER_DEFINE_CLASS_EXT(streamselect, "(a)streamselect", streamselect_options);
52
53 static int process_frame(FFFrameSync *fs)
54 {
55 AVFilterContext *ctx = fs->parent;
56 StreamSelectContext *s = fs->opaque;
57 AVFrame **in = s->frames;
58 int i, j, ret = 0, have_out = 0;
59
60 for (i = 0; i < ctx->nb_inputs; i++) {
61 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
62 return ret;
63 }
64
65 for (j = 0; j < ctx->nb_inputs; j++) {
66 for (i = 0; i < s->nb_map; i++) {
67 if (s->map[i] == j) {
68 AVFrame *out;
69
70 if (s->is_audio && s->last_pts[j] == in[j]->pts &&
71 ctx->outputs[i]->frame_count_in > 0)
72 continue;
73 out = av_frame_clone(in[j]);
74 if (!out)
75 return AVERROR(ENOMEM);
76
77 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
78 s->last_pts[j] = in[j]->pts;
79 ret = ff_filter_frame(ctx->outputs[i], out);
80 have_out = 1;
81 if (ret < 0)
82 return ret;
83 }
84 }
85 }
86
87 if (!have_out)
88 ff_filter_set_ready(ctx, 100);
89 return ret;
90 }
91
92 static int activate(AVFilterContext *ctx)
93 {
94 StreamSelectContext *s = ctx->priv;
95 return ff_framesync_activate(&s->fs);
96 }
97
98 static int config_output(AVFilterLink *outlink)
99 {
100 AVFilterContext *ctx = outlink->src;
101 StreamSelectContext *s = ctx->priv;
102 const int outlink_idx = FF_OUTLINK_IDX(outlink);
103 const int inlink_idx = s->map[outlink_idx];
104 AVFilterLink *inlink = ctx->inputs[inlink_idx];
105 FFFrameSyncIn *in;
106 int i, ret;
107
108 av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
109 "with settings from input link %d\n",
110 outlink_idx, inlink_idx);
111
112 switch (outlink->type) {
113 case AVMEDIA_TYPE_VIDEO:
114 outlink->w = inlink->w;
115 outlink->h = inlink->h;
116 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
117 outlink->frame_rate = inlink->frame_rate;
118 break;
119 case AVMEDIA_TYPE_AUDIO:
120 outlink->sample_rate = inlink->sample_rate;
121 outlink->ch_layout.nb_channels = inlink->ch_layout.nb_channels;
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_append_inpad_free_name(ctx, &pad);
170 } else {
171 pad.config_props = config_output;
172 ret = ff_append_outpad_free_name(ctx, &pad);
173 }
174 if (ret < 0)
175 return ret;
176 }
177
178 return 0;
179 }
180
181 static int parse_mapping(AVFilterContext *ctx, const char *map)
182 {
183 StreamSelectContext *s = ctx->priv;
184 int *new_map;
185 int new_nb_map = 0;
186
187 if (!map) {
188 av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
189 return AVERROR(EINVAL);
190 }
191
192 new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
193 if (!new_map)
194 return AVERROR(ENOMEM);
195
196 while (1) {
197 char *p;
198 const int n = strtol(map, &p, 0);
199
200 av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
201
202 if (map == p)
203 break;
204 map = p;
205
206 if (new_nb_map >= s->nb_inputs) {
207 av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
208 "input pads available\n", s->nb_inputs);
209 av_free(new_map);
210 return AVERROR(EINVAL);
211 }
212
213 if (n < 0 || n >= ctx->nb_inputs) {
214 av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
215 "(there is only %d input streams defined)\n",
216 n, s->nb_inputs);
217 av_free(new_map);
218 return AVERROR(EINVAL);
219 }
220
221 av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
222 new_map[new_nb_map++] = n;
223 }
224
225 if (!new_nb_map) {
226 av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
227 av_free(new_map);
228 return AVERROR(EINVAL);
229 }
230
231 av_freep(&s->map);
232 s->map = new_map;
233 s->nb_map = new_nb_map;
234
235 av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
236
237 return 0;
238 }
239
240 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
241 char *res, int res_len, int flags)
242 {
243 if (!strcmp(cmd, "map")) {
244 int ret = parse_mapping(ctx, args);
245
246 if (ret < 0)
247 return ret;
248 return ff_filter_config_links(ctx);
249 }
250 return AVERROR(ENOSYS);
251 }
252
253 static av_cold int init(AVFilterContext *ctx)
254 {
255 StreamSelectContext *s = ctx->priv;
256 int ret, nb_outputs = 0;
257 char *map = s->map_str;
258
259 if (!strcmp(ctx->filter->name, "astreamselect"))
260 s->is_audio = 1;
261
262 for (; map;) {
263 char *p;
264
265 strtol(map, &p, 0);
266 if (map == p)
267 break;
268 nb_outputs++;
269 map = p;
270 }
271
272 s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
273 if (!s->last_pts)
274 return AVERROR(ENOMEM);
275
276 if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
277 (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
278 return ret;
279
280 av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
281 ctx->nb_inputs, ctx->nb_outputs);
282
283 return parse_mapping(ctx, s->map_str);
284 }
285
286 static av_cold void uninit(AVFilterContext *ctx)
287 {
288 StreamSelectContext *s = ctx->priv;
289
290 av_freep(&s->last_pts);
291 av_freep(&s->map);
292 av_freep(&s->frames);
293 ff_framesync_uninit(&s->fs);
294 }
295
296 static int query_formats(AVFilterContext *ctx)
297 {
298 AVFilterFormats *formats;
299 int ret, i;
300
301 for (i = 0; i < ctx->nb_inputs; i++) {
302 formats = ff_all_formats(ctx->inputs[i]->type);
303 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
304 return ret;
305
306 if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
307 if ((ret = ff_set_common_all_samplerates (ctx)) < 0 ||
308 (ret = ff_set_common_all_channel_counts(ctx)) < 0)
309 return ret;
310 }
311 }
312
313 return 0;
314 }
315
316 const AVFilter ff_vf_streamselect = {
317 .name = "streamselect",
318 .description = NULL_IF_CONFIG_SMALL("Select video streams"),
319 .init = init,
320 FILTER_QUERY_FUNC(query_formats),
321 .process_command = process_command,
322 .uninit = uninit,
323 .activate = activate,
324 .priv_size = sizeof(StreamSelectContext),
325 .priv_class = &streamselect_class,
326 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
327 };
328
329 const AVFilter ff_af_astreamselect = {
330 .name = "astreamselect",
331 .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
332 .priv_class = &streamselect_class,
333 .init = init,
334 FILTER_QUERY_FUNC(query_formats),
335 .process_command = process_command,
336 .uninit = uninit,
337 .activate = activate,
338 .priv_size = sizeof(StreamSelectContext),
339 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
340 };
341