FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_streamselect.c
Date: 2024-07-26 21:54:09
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/mem.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "framesync.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct StreamSelectContext {
32 const AVClass *class;
33 int nb_inputs;
34 char *map_str;
35 int *map;
36 int nb_map;
37 int is_audio;
38 int64_t *last_pts;
39 AVFrame **frames;
40 FFFrameSync fs;
41 } StreamSelectContext;
42
43 #define OFFSET(x) offsetof(StreamSelectContext, x)
44 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
45 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
46 static const AVOption streamselect_options[] = {
47 { "inputs", "number of input streams", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags=FLAGS },
48 { "map", "input indexes to remap to outputs", OFFSET(map_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=TFLAGS },
49 { NULL }
50 };
51
52 AVFILTER_DEFINE_CLASS_EXT(streamselect, "(a)streamselect", streamselect_options);
53
54 static int process_frame(FFFrameSync *fs)
55 {
56 AVFilterContext *ctx = fs->parent;
57 StreamSelectContext *s = fs->opaque;
58 AVFrame **in = s->frames;
59 int i, j, ret = 0, have_out = 0;
60
61 for (i = 0; i < ctx->nb_inputs; i++) {
62 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
63 return ret;
64 }
65
66 for (j = 0; j < ctx->nb_inputs; j++) {
67 for (i = 0; i < s->nb_map; i++) {
68 if (s->map[i] == j) {
69 AVFrame *out;
70
71 if (s->is_audio && s->last_pts[j] == in[j]->pts &&
72 ctx->outputs[i]->frame_count_in > 0)
73 continue;
74 out = av_frame_clone(in[j]);
75 if (!out)
76 return AVERROR(ENOMEM);
77
78 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
79 s->last_pts[j] = in[j]->pts;
80 ret = ff_filter_frame(ctx->outputs[i], out);
81 have_out = 1;
82 if (ret < 0)
83 return ret;
84 }
85 }
86 }
87
88 if (!have_out)
89 ff_filter_set_ready(ctx, 100);
90 return ret;
91 }
92
93 static int activate(AVFilterContext *ctx)
94 {
95 StreamSelectContext *s = ctx->priv;
96 return ff_framesync_activate(&s->fs);
97 }
98
99 static int config_output(AVFilterLink *outlink)
100 {
101 AVFilterContext *ctx = outlink->src;
102 StreamSelectContext *s = ctx->priv;
103 const int outlink_idx = FF_OUTLINK_IDX(outlink);
104 const int inlink_idx = s->map[outlink_idx];
105 AVFilterLink *inlink = ctx->inputs[inlink_idx];
106 FFFrameSyncIn *in;
107 int i, ret;
108
109 av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
110 "with settings from input link %d\n",
111 outlink_idx, inlink_idx);
112
113 switch (outlink->type) {
114 case AVMEDIA_TYPE_VIDEO:
115 outlink->w = inlink->w;
116 outlink->h = inlink->h;
117 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
118 outlink->frame_rate = inlink->frame_rate;
119 break;
120 case AVMEDIA_TYPE_AUDIO:
121 outlink->sample_rate = inlink->sample_rate;
122 outlink->ch_layout.nb_channels = inlink->ch_layout.nb_channels;
123 break;
124 }
125
126 outlink->time_base = inlink->time_base;
127 outlink->format = inlink->format;
128
129 if (s->fs.opaque == s)
130 return 0;
131
132 if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
133 return ret;
134
135 in = s->fs.in;
136 s->fs.opaque = s;
137 s->fs.on_event = process_frame;
138
139 for (i = 0; i < ctx->nb_inputs; i++) {
140 in[i].time_base = ctx->inputs[i]->time_base;
141 in[i].sync = 1;
142 in[i].before = EXT_STOP;
143 in[i].after = EXT_STOP;
144 }
145
146 s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
147 if (!s->frames)
148 return AVERROR(ENOMEM);
149
150 return ff_framesync_configure(&s->fs);
151 }
152
153 static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
154 {
155 const char *padtype = is_input ? "in" : "out";
156 int i = 0, ret = 0;
157
158 for (i = 0; i < nb_pads; i++) {
159 AVFilterPad pad = { 0 };
160
161 pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
162
163 pad.name = av_asprintf("%sput%d", padtype, i);
164 if (!pad.name)
165 return AVERROR(ENOMEM);
166
167 av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
168
169 if (is_input) {
170 ret = ff_append_inpad_free_name(ctx, &pad);
171 } else {
172 pad.config_props = config_output;
173 ret = ff_append_outpad_free_name(ctx, &pad);
174 }
175 if (ret < 0)
176 return ret;
177 }
178
179 return 0;
180 }
181
182 static int parse_mapping(AVFilterContext *ctx, const char *map)
183 {
184 StreamSelectContext *s = ctx->priv;
185 int *new_map;
186 int new_nb_map = 0;
187
188 if (!map) {
189 av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
190 return AVERROR(EINVAL);
191 }
192
193 new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
194 if (!new_map)
195 return AVERROR(ENOMEM);
196
197 while (1) {
198 char *p;
199 const int n = strtol(map, &p, 0);
200
201 av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
202
203 if (map == p)
204 break;
205 map = p;
206
207 if (new_nb_map >= s->nb_inputs) {
208 av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
209 "input pads available\n", s->nb_inputs);
210 av_free(new_map);
211 return AVERROR(EINVAL);
212 }
213
214 if (n < 0 || n >= ctx->nb_inputs) {
215 av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
216 "(there is only %d input streams defined)\n",
217 n, s->nb_inputs);
218 av_free(new_map);
219 return AVERROR(EINVAL);
220 }
221
222 av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
223 new_map[new_nb_map++] = n;
224 }
225
226 if (!new_nb_map) {
227 av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
228 av_free(new_map);
229 return AVERROR(EINVAL);
230 }
231
232 av_freep(&s->map);
233 s->map = new_map;
234 s->nb_map = new_nb_map;
235
236 av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
237
238 return 0;
239 }
240
241 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
242 char *res, int res_len, int flags)
243 {
244 if (!strcmp(cmd, "map")) {
245 int ret = parse_mapping(ctx, args);
246
247 if (ret < 0)
248 return ret;
249 return ff_filter_config_links(ctx);
250 }
251 return AVERROR(ENOSYS);
252 }
253
254 static av_cold int init(AVFilterContext *ctx)
255 {
256 StreamSelectContext *s = ctx->priv;
257 int ret, nb_outputs = 0;
258 char *map = s->map_str;
259
260 if (!strcmp(ctx->filter->name, "astreamselect"))
261 s->is_audio = 1;
262
263 for (; map;) {
264 char *p;
265
266 strtol(map, &p, 0);
267 if (map == p)
268 break;
269 nb_outputs++;
270 map = p;
271 }
272
273 s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
274 if (!s->last_pts)
275 return AVERROR(ENOMEM);
276
277 if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
278 (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
279 return ret;
280
281 av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
282 ctx->nb_inputs, ctx->nb_outputs);
283
284 return parse_mapping(ctx, s->map_str);
285 }
286
287 static av_cold void uninit(AVFilterContext *ctx)
288 {
289 StreamSelectContext *s = ctx->priv;
290
291 av_freep(&s->last_pts);
292 av_freep(&s->map);
293 av_freep(&s->frames);
294 ff_framesync_uninit(&s->fs);
295 }
296
297 static int query_formats(AVFilterContext *ctx)
298 {
299 AVFilterFormats *formats;
300 int ret, i;
301
302 for (i = 0; i < ctx->nb_inputs; i++) {
303 formats = ff_all_formats(ctx->inputs[i]->type);
304 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
305 return ret;
306
307 if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
308 if ((ret = ff_set_common_all_samplerates (ctx)) < 0 ||
309 (ret = ff_set_common_all_channel_counts(ctx)) < 0)
310 return ret;
311 }
312 }
313
314 return 0;
315 }
316
317 const AVFilter ff_vf_streamselect = {
318 .name = "streamselect",
319 .description = NULL_IF_CONFIG_SMALL("Select video streams"),
320 .init = init,
321 FILTER_QUERY_FUNC(query_formats),
322 .process_command = process_command,
323 .uninit = uninit,
324 .activate = activate,
325 .priv_size = sizeof(StreamSelectContext),
326 .priv_class = &streamselect_class,
327 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
328 };
329
330 const AVFilter ff_af_astreamselect = {
331 .name = "astreamselect",
332 .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
333 .priv_class = &streamselect_class,
334 .init = init,
335 FILTER_QUERY_FUNC(query_formats),
336 .process_command = process_command,
337 .uninit = uninit,
338 .activate = activate,
339 .priv_size = sizeof(StreamSelectContext),
340 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
341 };
342