[FFmpeg-devel] [PATCH] lavfi/movie: add support to setting format and codec options

Stefano Sabatini stefasab at gmail.com
Sun Apr 29 21:43:22 CEST 2012


---
 libavfilter/src_movie.c |   40 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index 1d65ade..7c98eb6 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -61,13 +61,19 @@ typedef struct {
     int bps;            ///< bytes per sample
     AVPacket pkt, pkt0;
     AVFilterBufferRef *samplesref;
+    char *format_opts;
+    char *codec_opts;
 } MovieContext;
 
 #define OFFSET(x) offsetof(MovieContext, x)
 
 static const AVOption movie_options[]= {
+{"codec_opts",   "set codec options",       OFFSET(codec_opts),   AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
+{"copts",        "set format options",      OFFSET(codec_opts),   AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
 {"format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
 {"f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
+{"format_opts",  "set format options",      OFFSET(format_opts),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
+{"fopts",        "set format options",      OFFSET(format_opts),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
 {"stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
 {"si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
 {"seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000 },
@@ -87,6 +93,19 @@ static const AVClass movie_class = {
     movie_options
 };
 
+static void warn_options_not_found(void *log_ctx, AVDictionary *dict, const char *ctx)
+{
+    AVDictionaryEntry *e = NULL;
+    int is_first = 1;
+
+    av_log(log_ctx, AV_LOG_WARNING, "The following %s options were not found: ", ctx);
+    while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)) {
+        av_log(log_ctx, AV_LOG_WARNING, "%s%s", is_first ? "" : ", ", e->key);
+        is_first = 0;
+    }
+    av_log(log_ctx, AV_LOG_WARNING, "\n");
+}
+
 static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
                                      enum AVMediaType type)
 {
@@ -94,6 +113,7 @@ static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, voi
     AVInputFormat *iformat = NULL;
     AVCodec *codec;
     int64_t timestamp;
+    AVDictionary *format_opts_dict = NULL, *codec_opts_dict = NULL;
     int ret;
 
     movie->class = &movie_class;
@@ -119,11 +139,18 @@ static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, voi
     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
 
     movie->format_ctx = NULL;
-    if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
+    if ((ret = av_dict_set_from_string(&format_opts_dict, movie->format_opts, 0, "=", ",", 0, ctx)) < 0)
+        return ret;
+    if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, &format_opts_dict)) < 0) {
         av_log(ctx, AV_LOG_ERROR,
                "Failed to avformat_open_input '%s'\n", movie->file_name);
         return ret;
     }
+    if (format_opts_dict) {
+        warn_options_not_found(ctx, format_opts_dict, "format");
+        av_dict_free(&format_opts_dict);
+    }
+
     if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
 
@@ -167,10 +194,16 @@ static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, voi
         return AVERROR(EINVAL);
     }
 
-    if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
+    if ((ret = av_dict_set_from_string(&codec_opts_dict, movie->codec_opts, 0, "=", "", 0, ctx)) < 0)
+        return ret;
+    if ((ret = avcodec_open2(movie->codec_ctx, codec, &codec_opts_dict)) < 0) {
         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
         return ret;
     }
+    if (codec_opts_dict) {
+        warn_options_not_found(ctx, codec_opts_dict, "codec");
+        av_dict_free(&codec_opts_dict);
+    }
 
     av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
            movie->seek_point, movie->format_name, movie->file_name,
@@ -188,8 +221,11 @@ static av_cold void movie_common_uninit(AVFilterContext *ctx)
 {
     MovieContext *movie = ctx->priv;
 
+    av_free(movie->codec_opts);
     av_free(movie->file_name);
     av_free(movie->format_name);
+    av_free(movie->format_opts);
+
     if (movie->codec_ctx)
         avcodec_close(movie->codec_ctx);
     if (movie->format_ctx)
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list