FFmpeg
Loading...
Searching...
No Matches
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/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 "video.h"
29
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
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
51AVFILTER_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 FilterLink *outl = ff_filter_link(ctx->outputs[i]);
68 if (s->map[i] == j) {
69 AVFrame *out;
70
71 if (s->is_audio && s->last_pts[j] == in[j]->pts &&
72 outl->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)
90 return ret;
91}
92
94{
95 StreamSelectContext *s = ctx->priv;
96 return ff_framesync_activate(&s->fs);
97}
98
99static int config_output(AVFilterLink *outlink)
100{
101 FilterLink *outl = ff_filter_link(outlink);
102 AVFilterContext *ctx = outlink->src;
103 StreamSelectContext *s = ctx->priv;
104 const int outlink_idx = FF_OUTLINK_IDX(outlink);
105 const int inlink_idx = s->map[outlink_idx];
106 AVFilterLink *inlink = ctx->inputs[inlink_idx];
107 FilterLink *inl = ff_filter_link(inlink);
108 FFFrameSyncIn *in;
109 int i, ret;
110
111 av_log(ctx, AV_LOG_VERBOSE, "config output link %d "
112 "with settings from input link %d\n",
113 outlink_idx, inlink_idx);
114
115 switch (outlink->type) {
117 outlink->w = inlink->w;
118 outlink->h = inlink->h;
119 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
120 outl->frame_rate = inl->frame_rate;
121 break;
123 outlink->sample_rate = inlink->sample_rate;
124 outlink->ch_layout.nb_channels = inlink->ch_layout.nb_channels;
125 break;
126 }
127
128 outlink->time_base = inlink->time_base;
129 outlink->format = inlink->format;
130
131 if (s->fs.opaque == s)
132 return 0;
133
134 if ((ret = ff_framesync_init(&s->fs, ctx, ctx->nb_inputs)) < 0)
135 return ret;
136
137 in = s->fs.in;
138 s->fs.opaque = s;
139 s->fs.on_event = process_frame;
140
141 for (i = 0; i < ctx->nb_inputs; i++) {
142 in[i].time_base = ctx->inputs[i]->time_base;
143 in[i].sync = 1;
144 in[i].before = EXT_STOP;
145 in[i].after = EXT_STOP;
146 }
147
148 s->frames = av_calloc(ctx->nb_inputs, sizeof(*s->frames));
149 if (!s->frames)
150 return AVERROR(ENOMEM);
151
152 return ff_framesync_configure(&s->fs);
153}
154
155static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
156{
157 const char *padtype = is_input ? "in" : "out";
158 int i = 0, ret = 0;
159
160 for (i = 0; i < nb_pads; i++) {
161 AVFilterPad pad = { 0 };
162
163 pad.type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
164
165 pad.name = av_asprintf("%sput%d", padtype, i);
166 if (!pad.name)
167 return AVERROR(ENOMEM);
168
169 av_log(ctx, AV_LOG_DEBUG, "Add %s pad %s\n", padtype, pad.name);
170
171 if (is_input) {
172 ret = ff_append_inpad_free_name(ctx, &pad);
173 } else {
175 ret = ff_append_outpad_free_name(ctx, &pad);
176 }
177 if (ret < 0)
178 return ret;
179 }
180
181 return 0;
182}
183
184static int parse_mapping(AVFilterContext *ctx, const char *map)
185{
186 StreamSelectContext *s = ctx->priv;
187 int *new_map;
188 int new_nb_map = 0;
189
190 if (!map) {
191 av_log(ctx, AV_LOG_ERROR, "mapping definition is not set\n");
192 return AVERROR(EINVAL);
193 }
194
195 new_map = av_calloc(s->nb_inputs, sizeof(*new_map));
196 if (!new_map)
197 return AVERROR(ENOMEM);
198
199 while (1) {
200 char *p;
201 const int n = strtol(map, &p, 0);
202
203 av_log(ctx, AV_LOG_DEBUG, "n=%d map=%p p=%p\n", n, map, p);
204
205 if (map == p)
206 break;
207 map = p;
208
209 if (new_nb_map >= s->nb_inputs) {
210 av_log(ctx, AV_LOG_ERROR, "Unable to map more than the %d "
211 "input pads available\n", s->nb_inputs);
212 av_free(new_map);
213 return AVERROR(EINVAL);
214 }
215
216 if (n < 0 || n >= ctx->nb_inputs) {
217 av_log(ctx, AV_LOG_ERROR, "Input stream index %d doesn't exist "
218 "(there is only %d input streams defined)\n",
219 n, s->nb_inputs);
220 av_free(new_map);
221 return AVERROR(EINVAL);
222 }
223
224 av_log(ctx, AV_LOG_VERBOSE, "Map input stream %d to output stream %d\n", n, new_nb_map);
225 new_map[new_nb_map++] = n;
226 }
227
228 if (!new_nb_map) {
229 av_log(ctx, AV_LOG_ERROR, "invalid mapping\n");
230 av_free(new_map);
231 return AVERROR(EINVAL);
232 }
233
234 av_freep(&s->map);
235 s->map = new_map;
236 s->nb_map = new_nb_map;
237
238 av_log(ctx, AV_LOG_VERBOSE, "%d map set\n", s->nb_map);
239
240 return 0;
241}
242
243static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
244 char *res, int res_len, int flags)
245{
246 if (!strcmp(cmd, "map")) {
247 int ret = parse_mapping(ctx, args);
248
249 if (ret < 0)
250 return ret;
251 return 0;
252 }
253 return AVERROR(ENOSYS);
254}
255
257{
258 StreamSelectContext *s = ctx->priv;
259 int ret, nb_outputs = 0;
260 char *map = s->map_str;
261
262 if (!strcmp(ctx->filter->name, "astreamselect"))
263 s->is_audio = 1;
264
265 for (; map;) {
266 char *p;
267
268 strtol(map, &p, 0);
269 if (map == p)
270 break;
271 nb_outputs++;
272 map = p;
273 }
274
275 s->last_pts = av_calloc(s->nb_inputs, sizeof(*s->last_pts));
276 if (!s->last_pts)
277 return AVERROR(ENOMEM);
278
279 if ((ret = parse_definition(ctx, s->nb_inputs, 1, s->is_audio)) < 0 ||
280 (ret = parse_definition(ctx, nb_outputs, 0, s->is_audio)) < 0)
281 return ret;
282
283 av_log(ctx, AV_LOG_DEBUG, "Configured with %d inpad and %d outpad\n",
284 ctx->nb_inputs, ctx->nb_outputs);
285
286 return parse_mapping(ctx, s->map_str);
287}
288
290{
291 StreamSelectContext *s = ctx->priv;
292
293 av_freep(&s->last_pts);
294 av_freep(&s->map);
295 av_freep(&s->frames);
297}
298
300 .p.name = "streamselect",
301 .p.description = NULL_IF_CONFIG_SMALL("Select video streams"),
302 .p.priv_class = &streamselect_class,
304 .init = init,
305 .process_command = process_command,
306 .uninit = uninit,
307 .activate = activate,
308 .priv_size = sizeof(StreamSelectContext),
309};
310
312 .p.name = "astreamselect",
313 .p.description = NULL_IF_CONFIG_SMALL("Select audio streams"),
314 .p.priv_class = &streamselect_class,
316 .init = init,
317 .process_command = process_command,
318 .uninit = uninit,
319 .activate = activate,
320 .priv_size = sizeof(StreamSelectContext),
321};
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
#define TFLAGS
Definition af_afade.c:66
const FFFilter ff_af_astreamselect
const FFFilter ff_vf_streamselect
static FILE * out
static AVFormatContext * ctx
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition avfilter.c:132
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
Definition avfilter.c:143
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
Main libavfilter public API header.
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int parse_mapping(AVFilterContext *ctx, const char *map)
static const AVOption streamselect_options[]
static int parse_definition(AVFilterContext *ctx, int nb_pads, int is_input, int is_audio)
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 void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static int process_frame(FFFrameSync *fs)
static av_always_inline int process_frame(AVTextFormatContext *tfc, InputFile *ifile, AVFrame *frame, const AVPacket *pkt, int *packet_new)
Definition ffprobe.c:1596
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition framesync.c:269
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition framesync.c:86
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition avfilter.h:161
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition avfilter.h:155
#define AVERROR(e)
Definition error.h:45
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const VDPAUPixFmtMap * map
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FF_OUTLINK_IDX(link)
Definition filters.h:216
#define av_cold
Definition attributes.h:117
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:88
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
@ EXT_STOP
Completely stop all streams with this one.
Definition packetsync.h:62
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition filters.h:120
enum AVMediaType type
AVFilterPad type.
Definition filters.h:51
const char * name
Pad name.
Definition filters.h:46
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
AVOption.
Definition opt.h:428
Input stream structure.
Definition framesync.h:102
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition framesync.h:112
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition framesync.h:107
AVRational time_base
Time base for the incoming frames.
Definition framesync.h:117
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition framesync.h:160
Frame sync structure.
Definition framesync.h:168
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)