FFmpeg  4.4.5
f_streamselect.c
Go to the documentation of this file.
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;
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(streamselect);
52 
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)
89  return ret;
90 }
91 
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->channels = inlink->channels;
122  outlink->channel_layout = inlink->channel_layout;
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_insert_inpad(ctx, i, &pad);
171  } else {
173  ret = ff_insert_outpad(ctx, i, &pad);
174  }
175 
176  if (ret < 0) {
177  av_freep(&pad.name);
178  return ret;
179  }
180  }
181 
182  return 0;
183 }
184 
185 static int parse_mapping(AVFilterContext *ctx, const char *map)
186 {
187  StreamSelectContext *s = ctx->priv;
188  int *new_map;
189  int new_nb_map = 0;
190 
191  if (!map) {
192  av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
193  return AVERROR(EINVAL);
194  }
195 
196  new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
197  if (!new_map)
198  return AVERROR(ENOMEM);
199 
200  while (1) {
201  char *p;
202  const int n = strtol(map, &p, 0);
203 
204  av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
205 
206  if (map == p)
207  break;
208  map = p;
209 
210  if (new_nb_map >= s->nb_inputs) {
211  av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
212  "input pads available\n", s->nb_inputs);
213  av_free(new_map);
214  return AVERROR(EINVAL);
215  }
216 
217  if (n < 0 || n >= ctx->nb_inputs) {
218  av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
219  "(there is only %d input streams defined)\n",
220  n, s->nb_inputs);
221  av_free(new_map);
222  return AVERROR(EINVAL);
223  }
224 
225  av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
226  new_map[new_nb_map++] = n;
227  }
228 
229  if (!new_nb_map) {
230  av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
231  av_free(new_map);
232  return AVERROR(EINVAL);
233  }
234 
235  av_freep(&s->map);
236  s->map = new_map;
237  s->nb_map = new_nb_map;
238 
239  av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
240 
241  return 0;
242 }
243 
244 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
245  char *res, int res_len, int flags)
246 {
247  if (!strcmp(cmd, "map")) {
248  int ret = parse_mapping(ctx, args);
249 
250  if (ret < 0)
251  return ret;
252  return avfilter_config_links(ctx);
253  }
254  return AVERROR(ENOSYS);
255 }
256 
258 {
259  StreamSelectContext *s = ctx->priv;
260  int ret, nb_outputs = 0;
261  char *map = s->map_str;
262 
263  if (!strcmp(ctx->filter->name, "astreamselect"))
264  s->is_audio = 1;
265 
266  for (; map;) {
267  char *p;
268 
269  strtol(map, &p, 0);
270  if (map == p)
271  break;
272  nb_outputs++;
273  map = p;
274  }
275 
276  s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
277  if (!s->last_pts)
278  return AVERROR(ENOMEM);
279 
280  if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
281  (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
282  return ret;
283 
284  av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
285  ctx->nb_inputs, ctx->nb_outputs);
286 
287  return parse_mapping(ctx, s->map_str);
288 }
289 
291 {
292  StreamSelectContext *s = ctx->priv;
293 
294  av_freep(&s->last_pts);
295  av_freep(&s->map);
296  av_freep(&s->frames);
297  ff_framesync_uninit(&s->fs);
298 
299  for (int i = 0; i < ctx->nb_inputs; i++)
300  av_freep(&ctx->input_pads[i].name);
301 
302  for (int i = 0; i < ctx->nb_outputs; i++)
303  av_freep(&ctx->output_pads[i].name);
304 }
305 
307 {
310  int ret, i;
311 
312  for (i = 0; i < ctx->nb_inputs; i++) {
313  formats = ff_all_formats(ctx->inputs[i]->type);
314  if ((ret = ff_set_common_formats(ctx, formats)) < 0)
315  return ret;
316 
317  if (ctx->inputs[i]->type == AVMEDIA_TYPE_AUDIO) {
319  if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
320  return ret;
322  if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
323  return ret;
324  }
325  }
326 
327  return 0;
328 }
329 
331  .name = "streamselect",
332  .description = NULL_IF_CONFIG_SMALL("Select video streams"),
333  .init = init,
334  .query_formats = query_formats,
335  .process_command = process_command,
336  .uninit = uninit,
337  .activate = activate,
338  .priv_size = sizeof(StreamSelectContext),
339  .priv_class = &streamselect_class,
341 };
342 
343 #define astreamselect_options streamselect_options
344 AVFILTER_DEFINE_CLASS(astreamselect);
345 
347  .name = "astreamselect",
348  .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
349  .init = init,
350  .query_formats = query_formats,
351  .process_command = process_command,
352  .uninit = uninit,
353  .activate = activate,
354  .priv_size = sizeof(StreamSelectContext),
355  .priv_class = &astreamselect_class,
357 };
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:193
Main libavfilter public API header.
static const int rates[]
Definition: avresample.c:176
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static int parse_mapping(AVFilterContext *ctx, const char *map)
AVFilter ff_vf_streamselect
static const AVOption streamselect_options[]
AVFilter ff_af_astreamselect
static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
#define TFLAGS
static int query_formats(AVFilterContext *ctx)
#define FLAGS
AVFILTER_DEFINE_CLASS(streamselect)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static int process_frame(FFFrameSync *fs)
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:436
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:345
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:124
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:341
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:253
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:84
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:112
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
Definition: avfilter.c:278
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:106
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
const VDPAUPixFmtMap * map
int i
Definition: input.c:407
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:240
static int ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:248
#define FF_OUTLINK_IDX(link)
Definition: internal.h:303
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVOptions.
formats
Definition: signature.h:48
Describe the class of an AVClass context structure.
Definition: log.h:67
A list of supported channel layouts.
Definition: formats.h:86
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:118
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
Input stream structure.
Definition: framesync.h:81
Frame sync structure.
Definition: framesync.h:146
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48