]> git.sesse.net Git - ffmpeg/blobdiff - cmdutils.c
av_string: add av_asprintf().
[ffmpeg] / cmdutils.c
index 084c441fa46dc945b29a8286e9cdf60a538932a7..bde0384c06d25462d9acfae6dbc5c6bf63691b32 100644 (file)
@@ -49,8 +49,6 @@
 #include <sys/resource.h>
 #endif
 
-AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
-AVFormatContext *avformat_opts;
 struct SwsContext *sws_opts;
 AVDictionary *format_opts, *codec_opts;
 
@@ -58,10 +56,6 @@ static const int this_year = 2011;
 
 void init_opts(void)
 {
-    int i;
-    for (i = 0; i < AVMEDIA_TYPE_NB; i++)
-        avcodec_opts[i] = avcodec_alloc_context3(NULL);
-    avformat_opts = avformat_alloc_context();
 #if CONFIG_SWSCALE
     sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
 #endif
@@ -69,11 +63,6 @@ void init_opts(void)
 
 void uninit_opts(void)
 {
-    int i;
-    for (i = 0; i < AVMEDIA_TYPE_NB; i++)
-        av_freep(&avcodec_opts[i]);
-    av_freep(&avformat_opts->key);
-    av_freep(&avformat_opts);
 #if CONFIG_SWSCALE
     sws_freeContext(sws_opts);
     sws_opts = NULL;
@@ -141,8 +130,11 @@ void show_help_options(const OptionDef *options, const char *msg, int mask, int
 }
 
 static const OptionDef* find_option(const OptionDef *po, const char *name){
+    const char *p = strchr(name, ':');
+    int len = p ? p - name : strlen(name);
+
     while (po->name != NULL) {
-        if (!strcmp(name, po->name))
+        if (!strncmp(name, po->name, len) && strlen(po->name) == len)
             break;
         po++;
     }
@@ -284,17 +276,25 @@ unknown_opt:
     }
 }
 
-#define FLAGS (o->type == FF_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
+#define FLAGS(o) ((o)->type == FF_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
 int opt_default(const char *opt, const char *arg)
 {
-    const AVOption *o;
-    if ((o = av_opt_find(avcodec_opts[0], opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)) ||
+    const AVOption *oc, *of, *os;
+    char opt_stripped[128];
+    const char *p;
+    const AVClass *cc = avcodec_get_class(), *fc = avformat_get_class(), *sc = sws_get_class();
+
+    if (!(p = strchr(opt, ':')))
+        p = opt + strlen(opt);
+    av_strlcpy(opt_stripped, opt, FFMIN(sizeof(opt_stripped), p - opt + 1));
+
+    if ((oc = av_opt_find(&cc, opt_stripped, NULL, 0, AV_OPT_SEARCH_CHILDREN|AV_OPT_SEARCH_FAKE_OBJ)) ||
          ((opt[0] == 'v' || opt[0] == 'a' || opt[0] == 's') &&
-          (o = av_opt_find(avcodec_opts[0], opt+1, NULL, 0, 0))))
-        av_dict_set(&codec_opts, opt, arg, FLAGS);
-    else if ((o = av_opt_find(avformat_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)))
-        av_dict_set(&format_opts, opt, arg, FLAGS);
-    else if ((o = av_opt_find(sws_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
+          (oc = av_opt_find(&cc, opt+1, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ))))
+        av_dict_set(&codec_opts, opt, arg, FLAGS(oc));
+    if ((of = av_opt_find(&fc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)))
+        av_dict_set(&format_opts, opt, arg, FLAGS(of));
+    if ((os = av_opt_find(&sc, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
         // XXX we only support sws_flags, not arbitrary sws options
         int ret = av_set_string3(sws_opts, opt, arg, 1, NULL);
         if (ret < 0) {
@@ -303,7 +303,7 @@ int opt_default(const char *opt, const char *arg)
         }
     }
 
-    if (o)
+    if (oc || of || os)
         return 0;
     fprintf(stderr, "Unrecognized option '%s'\n", opt);
     return AVERROR_OPTION_NOT_FOUND;
@@ -783,13 +783,45 @@ FILE *get_preset_file(char *filename, size_t filename_size,
     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;
+        default: abort(); // never reached, silence warning
+        }
+        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;
     AVDictionaryEntry *t = NULL;
-    AVCodec       *codec = encoder ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
-    int            flags = encoder ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
+    AVCodec       *codec = s->oformat ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
+    int            flags = s->oformat ? AV_OPT_FLAG_ENCODING_PARAM : AV_OPT_FLAG_DECODING_PARAM;
     char          prefix = 0;
+    const AVClass    *cc = avcodec_get_class();
 
     if (!codec)
         return NULL;
@@ -801,11 +833,24 @@ AVDictionary *filter_codec_opts(AVDictionary *opts, enum CodecID codec_id, int e
     }
 
     while (t = av_dict_get(opts, "", t, AV_DICT_IGNORE_SUFFIX)) {
-        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)))
+        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(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
+            (codec && codec->priv_class && av_opt_find(&codec->priv_class, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ)))
             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(&cc, t->key+1, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ))
             av_dict_set(&ret, t->key+1, t->value, 0);
+
+        if (p)
+            *p = ':';
     }
     return ret;
 }
@@ -823,7 +868,7 @@ AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *cod
         return NULL;
     }
     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;
 }