FFmpeg  4.0
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 "formats.h"
25 #include "framesync.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct StreamSelectContext {
30  const AVClass *class;
31  int nb_inputs;
32  char *map_str;
33  int *map;
34  int nb_map;
35  int is_audio;
36  int64_t *last_pts;
40 
41 #define OFFSET(x) offsetof(StreamSelectContext, x)
42 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
43 static const AVOption streamselect_options[] = {
44  { "inputs", "number of input streams", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags=FLAGS },
45  { "map", "input indexes to remap to outputs", OFFSET(map_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=FLAGS },
46  { NULL }
47 };
48 
49 AVFILTER_DEFINE_CLASS(streamselect);
50 
52 {
53  AVFilterContext *ctx = fs->parent;
55  AVFrame **in = s->frames;
56  int i, j, ret = 0;
57 
58  for (i = 0; i < ctx->nb_inputs; i++) {
59  if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
60  return ret;
61  }
62 
63  for (j = 0; j < ctx->nb_inputs; j++) {
64  for (i = 0; i < s->nb_map; i++) {
65  if (s->map[i] == j) {
66  AVFrame *out;
67 
68  if (s->is_audio && s->last_pts[j] == in[j]->pts &&
69  ctx->outputs[i]->frame_count_in > 0)
70  continue;
71  out = av_frame_clone(in[j]);
72  if (!out)
73  return AVERROR(ENOMEM);
74 
75  out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, ctx->outputs[i]->time_base);
76  s->last_pts[j] = in[j]->pts;
77  ret = ff_filter_frame(ctx->outputs[i], out);
78  if (ret < 0)
79  return ret;
80  }
81  }
82  }
83 
84  return ret;
85 }
86 
88 {
89  StreamSelectContext *s = ctx->priv;
90  return ff_framesync_activate(&s->fs);
91 }
92 
93 static int config_output(AVFilterLink *outlink)
94 {
95  AVFilterContext *ctx = outlink->src;
96  StreamSelectContext *s = ctx->priv;
97  const int outlink_idx = FF_OUTLINK_IDX(outlink);
98  const int inlink_idx = s->map[outlink_idx];
99  AVFilterLink *inlink = ctx->inputs[inlink_idx];
100  FFFrameSyncIn *in;
101  int i, ret;
102 
103  av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
104  "with settings from input link %d\n",
105  outlink_idx, inlink_idx);
106 
107  switch (outlink->type) {
108  case AVMEDIA_TYPE_VIDEO:
109  outlink->w = inlink->w;
110  outlink->h = inlink->h;
111  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
112  outlink->frame_rate = inlink->frame_rate;
113  break;
114  case AVMEDIA_TYPE_AUDIO:
115  outlink->sample_rate = inlink->sample_rate;
116  outlink->channels = inlink->channels;
117  outlink->channel_layout = inlink->channel_layout;
118  break;
119  }
120 
121  outlink->time_base = inlink->time_base;
122  outlink->format = inlink->format;
123 
124  if (s->fs.opaque == s)
125  return 0;
126 
127  if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
128  return ret;
129 
130  in = s->fs.in;
131  s->fs.opaque = s;
133 
134  for (i = 0; i < ctx->nb_inputs; i++) {
135  in[i].time_base = ctx->inputs[i]->time_base;
136  in[i].sync = 1;
137  in[i].before = EXT_STOP;
138  in[i].after = EXT_STOP;
139  }
140 
141  s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
142  if (!s->frames)
143  return AVERROR(ENOMEM);
144 
145  return ff_framesync_configure(&s->fs);
146 }
147 
148 static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
149 {
150  const char *padtype = is_input ? "in" : "out";
151  int i = 0, ret = 0;
152 
153  for (i = 0; i < nb_pads; i++) {
154  AVFilterPad pad = { 0 };
155 
156  pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
157 
158  pad.name = av_asprintf("%sput%d", padtype, i);
159  if (!pad.name)
160  return AVERROR(ENOMEM);
161 
162  av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
163 
164  if (is_input) {
165  ret = ff_insert_inpad(ctx, i, &pad);
166  } else {
168  ret = ff_insert_outpad(ctx, i, &pad);
169  }
170 
171  if (ret < 0) {
172  av_freep(&pad.name);
173  return ret;
174  }
175  }
176 
177  return 0;
178 }
179 
180 static int parse_mapping(AVFilterContext *ctx, const char *map)
181 {
182  StreamSelectContext *s = ctx->priv;
183  int *new_map;
184  int new_nb_map = 0;
185 
186  if (!map) {
187  av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
188  return AVERROR(EINVAL);
189  }
190 
191  new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
192  if (!new_map)
193  return AVERROR(ENOMEM);
194 
195  while (1) {
196  char *p;
197  const int n = strtol(map, &p, 0);
198 
199  av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
200 
201  if (map == p)
202  break;
203  map = p;
204 
205  if (new_nb_map >= s->nb_inputs) {
206  av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
207  "input pads available\n", s->nb_inputs);
208  av_free(new_map);
209  return AVERROR(EINVAL);
210  }
211 
212  if (n < 0 || n >= ctx->nb_inputs) {
213  av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
214  "(there is only %d input streams defined)\n",
215  n, s->nb_inputs);
216  av_free(new_map);
217  return AVERROR(EINVAL);
218  }
219 
220  av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
221  new_map[new_nb_map++] = n;
222  }
223 
224  if (!new_nb_map) {
225  av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
226  av_free(new_map);
227  return AVERROR(EINVAL);
228  }
229 
230  av_freep(&s->map);
231  s->map = new_map;
232  s->nb_map = new_nb_map;
233 
234  av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
235 
236  return 0;
237 }
238 
239 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
240  char *res, int res_len, int flags)
241 {
242  if (!strcmp(cmd, "map")) {
243  int ret = parse_mapping(ctx, args);
244 
245  if (ret < 0)
246  return ret;
247  return avfilter_config_links(ctx);
248  }
249  return AVERROR(ENOSYS);
250 }
251 
253 {
254  StreamSelectContext *s = ctx->priv;
255  int ret, nb_outputs = 0;
256  char *map = s->map_str;
257 
258  if (!strcmp(ctx->filter->name, "astreamselect"))
259  s->is_audio = 1;
260 
261  for (; map;) {
262  char *p;
263 
264  strtol(map, &p, 0);
265  if (map == p)
266  break;
267  nb_outputs++;
268  map = p;
269  }
270 
271  s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
272  if (!s->last_pts)
273  return AVERROR(ENOMEM);
274 
275  if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
276  (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
277  return ret;
278 
279  av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
280  ctx->nb_inputs, ctx->nb_outputs);
281 
282  return parse_mapping(ctx, s->map_str);
283 }
284 
286 {
287  StreamSelectContext *s = ctx->priv;
288 
289  av_freep(&s->last_pts);
290  av_freep(&s->map);
291  av_freep(&s->frames);
292  ff_framesync_uninit(&s->fs);
293 }
294 
296 {
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  rates = ff_all_samplerates();
308  if ((ret = ff_set_common_samplerates(ctx, rates)) < 0)
309  return ret;
310  layouts = ff_all_channel_counts();
311  if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
312  return ret;
313  }
314  }
315 
316  return 0;
317 }
318 
320  .name = "streamselect",
321  .description = NULL_IF_CONFIG_SMALL("Select video streams"),
322  .init = init,
323  .query_formats = query_formats,
324  .process_command = process_command,
325  .uninit = uninit,
326  .activate = activate,
327  .priv_size = sizeof(StreamSelectContext),
328  .priv_class = &streamselect_class,
330 };
331 
332 #define astreamselect_options streamselect_options
333 AVFILTER_DEFINE_CLASS(astreamselect);
334 
336  .name = "astreamselect",
337  .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
338  .init = init,
339  .query_formats = query_formats,
340  .process_command = process_command,
341  .uninit = uninit,
342  .activate = activate,
343  .priv_size = sizeof(StreamSelectContext),
344  .priv_class = &astreamselect_class,
346 };
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
const char * s
Definition: avisynth_c.h:768
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
static av_cold void uninit(AVFilterContext *ctx)
static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
AVOption.
Definition: opt.h:246
static int query_formats(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
Main libavfilter public API header.
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
#define FF_OUTLINK_IDX(link)
Definition: internal.h:349
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:117
int64_t pts
Timestamp of the current event.
Definition: framesync.h:167
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVFilter ff_vf_streamselect
const char * name
Pad name.
Definition: internal.h:60
AVFilterContext * parent
Parent filter context.
Definition: framesync.h:152
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
FFFrameSyncIn * in
Pointer to array of inputs.
Definition: framesync.h:203
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
Definition: avfilter.c:277
static int flags
Definition: log.c:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static int config_output(AVFilterLink *outlink)
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:111
Input stream structure.
Definition: framesync.h:81
#define av_log(a,...)
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:350
A filter pad used for either input or output.
Definition: internal.h:54
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:293
Frame sync structure.
Definition: framesync.h:146
#define AVERROR(e)
Definition: error.h:43
unsigned nb_outputs
number of output pads
Definition: avfilter.h:351
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter&#39;s input and try to produce output.
Definition: framesync.c:344
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:172
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
common internal API header
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:129
static int activate(AVFilterContext *ctx)
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
const char AVS_Value args
Definition: avisynth_c.h:780
AVFormatContext * ctx
Definition: movenc.c:48
AVRational time_base
Time base for the output events.
Definition: framesync.h:162
int n
Definition: avisynth_c.h:684
void * opaque
Opaque pointer, not used by the API.
Definition: framesync.h:177
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:538
A list of supported channel layouts.
Definition: formats.h:85
static int parse_mapping(AVFilterContext *ctx, const char *map)
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:77
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)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events...
Definition: framesync.h:139
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define FLAGS
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
#define OFFSET(x)
AVFilter ff_af_astreamselect
Completely stop all streams with this one.
Definition: framesync.h:65
#define av_free(p)
static int process_frame(FFFrameSync *fs)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static const AVOption streamselect_options[]
formats
Definition: signature.h:48
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:256
static const int rates[]
Definition: avresample.c:176
static int ff_insert_outpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new output pad for the filter.
Definition: internal.h:285
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
AVFILTER_DEFINE_CLASS(streamselect)
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:277