FFmpeg
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;
37  int64_t *last_pts;
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 
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) {
171  } else {
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 avfilter_config_links(ctx);
250  }
251  return AVERROR(ENOSYS);
252 }
253 
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 
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 
298 {
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 ||
310  return ret;
311  }
312  }
313 
314  return 0;
315 }
316 
318  .name = "streamselect",
319  .description = NULL_IF_CONFIG_SMALL("Select video streams"),
320  .init = init,
322  .process_command = process_command,
323  .uninit = uninit,
324  .activate = activate,
325  .priv_size = sizeof(StreamSelectContext),
326  .priv_class = &streamselect_class,
328 };
329 
331  .name = "astreamselect",
332  .description = NULL_IF_CONFIG_SMALL("Select audio streams"),
333  .priv_class = &streamselect_class,
334  .init = init,
336  .process_command = process_command,
337  .uninit = uninit,
338  .activate = activate,
339  .priv_size = sizeof(StreamSelectContext),
341 };
formats
formats
Definition: signature.h:48
FFFrameSyncIn::time_base
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
ff_framesync_configure
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:119
StreamSelectContext::map_str
char * map_str
Definition: f_streamselect.c:33
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_framesync_uninit
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:285
out
FILE * out
Definition: movenc.c:54
StreamSelectContext::nb_inputs
int nb_inputs
Definition: f_streamselect.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
ff_framesync_get_frame
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:248
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(streamselect, "(a)streamselect", streamselect_options)
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:689
parse_mapping
static int parse_mapping(AVFilterContext *ctx, const char *map)
Definition: f_streamselect.c:182
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
FFFrameSync
Frame sync structure.
Definition: framesync.h:146
video.h
StreamSelectContext::frames
AVFrame ** frames
Definition: f_streamselect.c:38
process_frame
static int process_frame(FFFrameSync *fs)
Definition: f_streamselect.c:53
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
EXT_STOP
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
streamselect_options
static const AVOption streamselect_options[]
Definition: f_streamselect.c:45
FFFrameSyncIn
Input stream structure.
Definition: framesync.h:81
avfilter_config_links
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
Definition: avfilter.c:290
FFFrameSyncIn::sync
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition: framesync.h:139
AVFILTER_FLAG_DYNAMIC_INPUTS
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:110
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:439
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
StreamSelectContext::is_audio
int is_audio
Definition: f_streamselect.c:36
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
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:699
ff_vf_streamselect
const AVFilter ff_vf_streamselect
Definition: f_streamselect.c:317
TFLAGS
#define TFLAGS
Definition: f_streamselect.c:44
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FF_OUTLINK_IDX
#define FF_OUTLINK_IDX(link)
Definition: internal.h:335
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: f_streamselect.c:297
filters.h
ff_af_astreamselect
const AVFilter ff_af_astreamselect
Definition: f_streamselect.c:330
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
activate
static int activate(AVFilterContext *ctx)
Definition: f_streamselect.c:92
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:424
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
config_output
static int config_output(AVFilterLink *outlink)
Definition: f_streamselect.c:98
NULL
#define NULL
Definition: coverity.c:32
StreamSelectContext::map
int * map
Definition: f_streamselect.c:34
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
ff_append_inpad_free_name
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:144
StreamSelectContext::fs
FFFrameSync fs
Definition: f_streamselect.c:39
StreamSelectContext
Definition: f_streamselect.c:30
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: f_streamselect.c:241
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:671
StreamSelectContext::nb_map
int nb_map
Definition: f_streamselect.c:35
AVFILTER_FLAG_DYNAMIC_OUTPUTS
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:116
AVFilterPad::config_props
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:130
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_streamselect.c:254
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
FLAGS
#define FLAGS
Definition: f_streamselect.c:43
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
internal.h
StreamSelectContext::last_pts
int64_t * last_pts
Definition: f_streamselect.c:37
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:61
ff_framesync_init
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:79
FFFrameSyncIn::before
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
framesync.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
OFFSET
#define OFFSET(x)
Definition: f_streamselect.c:42
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
ff_append_outpad_free_name
int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:155
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_streamselect.c:287
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
parse_definition
static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
Definition: f_streamselect.c:153
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FFFrameSyncIn::after
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
ff_framesync_activate
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:336
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:211