Commit d4863fc1 authored by Anton Khirnov's avatar Anton Khirnov

cmdutils: allow precisely specifying a stream for AVOptions.

parent f60d1366
...@@ -674,8 +674,10 @@ static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCode ...@@ -674,8 +674,10 @@ static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCode
ost->index = idx; ost->index = idx;
ost->st = st; ost->st = st;
ost->enc = codec; ost->enc = codec;
if (codec) if (codec) {
ost->opts = filter_codec_opts(codec_opts, codec->id, 1); st->codec->codec_type = codec->type;
ost->opts = filter_codec_opts(codec_opts, codec->id, oc, st);
}
avcodec_get_context_defaults3(st->codec, codec); avcodec_get_context_defaults3(st->codec, codec);
...@@ -3137,7 +3139,7 @@ static int opt_input_file(const char *opt, const char *filename) ...@@ -3137,7 +3139,7 @@ static int opt_input_file(const char *opt, const char *filename)
ist->st = st; ist->st = st;
ist->file_index = nb_input_files; ist->file_index = nb_input_files;
ist->discard = 1; ist->discard = 1;
ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, 0); ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
if (i < nb_ts_scale) if (i < nb_ts_scale)
ist->ts_scale = ts_scale[i]; ist->ts_scale = ts_scale[i];
......
...@@ -2134,7 +2134,7 @@ static int stream_component_open(VideoState *is, int stream_index) ...@@ -2134,7 +2134,7 @@ static int stream_component_open(VideoState *is, int stream_index)
return -1; return -1;
avctx = ic->streams[stream_index]->codec; avctx = ic->streams[stream_index]->codec;
opts = filter_codec_opts(codec_opts, avctx->codec_id, 0); opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index]);
/* prepare audio output */ /* prepare audio output */
if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
......
...@@ -289,7 +289,14 @@ unknown_opt: ...@@ -289,7 +289,14 @@ unknown_opt:
int opt_default(const char *opt, const char *arg) int opt_default(const char *opt, const char *arg)
{ {
const AVOption *o; const AVOption *o;
if ((o = av_opt_find(avcodec_opts[0], opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)) || char opt_stripped[128];
const char *p;
if (!(p = strchr(opt, ':')))
p = opt + strlen(opt);
av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
if ((o = av_opt_find(avcodec_opts[0], opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN)) ||
((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') && ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
(o = av_opt_find(avcodec_opts[0], opt+1, NULL, 0, 0)))) (o = av_opt_find(avcodec_opts[0], opt+1, NULL, 0, 0))))
av_dict_set(&codec_opts, opt, arg, FLAGS); av_dict_set(&codec_opts, opt, arg, FLAGS);
...@@ -782,12 +789,42 @@ FILE *get_preset_file(char *filename, size_t filename_size, ...@@ -782,12 +789,42 @@ FILE *get_preset_file(char *filename, size_t filename_size,
return f; return f;
} }
AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int encoder) int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
{
if (*spec <= '9' && *spec >= '0') /* opt:index */
return strtol(spec, NULL, 0) == st->index;
else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd') { /* opt:[vasd] */
enum AVMediaType type;
switch (*spec++) {
case 'v': type = AVMEDIA_TYPE_VIDEO; break;
case 'a': type = AVMEDIA_TYPE_AUDIO; break;
case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
case 'd': type = AVMEDIA_TYPE_DATA; break;
}
if (type != st->codec->codec_type)
return 0;
if (*spec++ == ':') { /* possibly followed by :index */
int i, index = strtol(spec, NULL, 0);
for (i = 0; i < s->nb_streams; i++)
if (s->streams[i]->codec->codec_type == type && index-- == 0)
return i == st->index;
return 0;
}
return 1;
} else if (!*spec) /* empty specifier, matches everything */
return 1;
av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
return AVERROR(EINVAL);
}
AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, AVFormatContext *s, AVStream *st)
{ {
AVDictionary *ret = NULL; AVDictionary *ret = NULL;
AVDictionaryEntry *t = NULL; AVDictionaryEntry *t = NULL;
AVCodec *codec = encoder ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id); AVCodec *codec = s->oformat ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
int flags = encoder ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM; int flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
char prefix = 0; char prefix = 0;
if (!codec) if (!codec)
...@@ -800,11 +837,24 @@ AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int e ...@@ -800,11 +837,24 @@ AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int e
} }
while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) { while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
char *p = strchr(t->key, ':');
/* check stream specification in opt name */
if (p)
switch (check_stream_specifier(s, st, p + 1)) {
case 1: *p = 0; break;
case 0: continue;
default: return NULL;
}
if (av_opt_find(avcodec_opts[0], t->key, NULL, flags, 0) || if (av_opt_find(avcodec_opts[0], t->key, NULL, flags, 0) ||
(codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, 0))) (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, 0)))
av_dict_set(&ret, t->key, t->value, 0); av_dict_set(&ret, t->key, t->value, 0);
else if (t->key[0] == prefix && av_opt_find(avcodec_opts[0], t->key+1, NULL, flags, 0)) else if (t->key[0] == prefix && av_opt_find(avcodec_opts[0], t->key+1, NULL, flags, 0))
av_dict_set(&ret, t->key+1, t->value, 0); av_dict_set(&ret, t->key+1, t->value, 0);
if (p)
*p = ':';
} }
return ret; return ret;
} }
...@@ -822,7 +872,7 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *cod ...@@ -822,7 +872,7 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *cod
return NULL; return NULL;
} }
for (i = 0; i < s->nb_streams; i++) for (i = 0; i < s->nb_streams; i++)
opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id, 0); opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codec->codec_id, s, s->streams[i]);
return opts; return opts;
} }
......
...@@ -148,16 +148,28 @@ void show_help_options(const OptionDef *options, const char *msg, int mask, int ...@@ -148,16 +148,28 @@ void show_help_options(const OptionDef *options, const char *msg, int mask, int
void parse_options(int argc, char **argv, const OptionDef *options, void parse_options(int argc, char **argv, const OptionDef *options,
void (* parse_arg_function)(const char*)); void (* parse_arg_function)(const char*));
/**
* Check if the given stream matches a stream specifier.
*
* @param s Corresponding format context.
* @param st Stream from s to be checked.
* @param spec A stream specifier of the [v|a|s|d]:[<stream index>] form.
*
* @return 1 if the stream matches, 0 if it doesn't, <0 on error
*/
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
/** /**
* Filter out options for given codec. * Filter out options for given codec.
* *
* Create a new options dictionary containing only the options from * Create a new options dictionary containing only the options from
* opts which apply to the codec with ID codec_id. * opts which apply to the codec with ID codec_id.
* *
* @param encoder if non-zero the codec is an encoder, otherwise is a decoder * @param s Corresponding format context.
* @param st A stream from s for which the options should be filtered.
* @return a pointer to the created dictionary * @return a pointer to the created dictionary
*/ */
AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int encoder); AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, AVFormatContext *s, AVStream *st);
/** /**
* Setup AVCodecContext options for avformat_find_stream_info(). * Setup AVCodecContext options for avformat_find_stream_info().
......
...@@ -114,5 +114,22 @@ muxer: ...@@ -114,5 +114,22 @@ muxer:
ffmpeg -i input.flac -id3v2_version 3 out.mp3 ffmpeg -i input.flac -id3v2_version 3 out.mp3
@end example @end example
You can precisely specify which stream(s) should the codec AVOption apply to by
appending a stream specifier of the form
@option{[:@var{stream_type}][:@var{stream_index}]} to the option name.
@var{stream_type} is 'v' for video, 'a' for audio and 's' for subtitle streams.
@var{stream_index} is a global stream index when @var{stream_type} isn't
given, otherwise it counts streams of the given type only. As always, the index
is zero-based. For example
@example
-foo -- applies to all applicable streams
-foo:v -- applies to all video streams
-foo:a:2 -- applies to the third audio stream
-foo:0 -- applies to the first stream
@end example
Note -nooption syntax cannot be used for boolean AVOptions, use -option Note -nooption syntax cannot be used for boolean AVOptions, use -option
0/-option 1. 0/-option 1.
Note2 old undocumented way of specifying per-stream AVOptions by prepending
v/a/s to the options name is now obsolete and will be removed soon.
...@@ -683,7 +683,7 @@ static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCode ...@@ -683,7 +683,7 @@ static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, AVCode
ost->st = st; ost->st = st;
ost->enc = codec; ost->enc = codec;
if (codec) if (codec)
ost->opts = filter_codec_opts(codec_opts, codec->id, 1); ost->opts = filter_codec_opts(codec_opts, codec->id, oc, st);
avcodec_get_context_defaults3(st->codec, codec); avcodec_get_context_defaults3(st->codec, codec);
...@@ -3293,7 +3293,7 @@ static int opt_input_file(const char *opt, const char *filename) ...@@ -3293,7 +3293,7 @@ static int opt_input_file(const char *opt, const char *filename)
ist->st = st; ist->st = st;
ist->file_index = nb_input_files; ist->file_index = nb_input_files;
ist->discard = 1; ist->discard = 1;
ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, 0); ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
if (i < nb_ts_scale) if (i < nb_ts_scale)
ist->ts_scale = ts_scale[i]; ist->ts_scale = ts_scale[i];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment