[FFmpeg-devel] [PATCH 3/4] lavu/opt: implement av_opt_get_key_value().

Nicolas George nicolas.george at normalesup.org
Fri Nov 2 13:36:14 CET 2012


This function does the low-level work of av_opt_set_from_string()
but can be used when there is no option context or when a generic
handling of unknown keys is needed.

av_opt_set_from_string() is changed to make use of it.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 doc/APIchanges      |    3 +++
 libavutil/opt.c     |   52 +++++++++++++++++++++++++++++++++++++--------------
 libavutil/opt.h     |   32 +++++++++++++++++++++++++++++++
 libavutil/version.h |    2 +-
 4 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index aa82846..fddd73f 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2012-10-22
 
 API changes, most recent first:
 
+2012-11-02 - xxxxxxx - lavu  52.2.100 - opt.h
+  Add av_opt_get_key_value().
+
 2012-10-21 - xxxxxxx - lavc  54.68.100 - avcodec.h
                        lavfi  3.20.100 - avfilter.h
   Add AV_PKT_DATA_STRINGS_METADATA side data type, used to transmit key/value
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 46f93de..b577500 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -828,13 +828,37 @@ static int get_key(const char **ropts, const char *delim, char **rkey)
     return 0;
 }
 
+int av_opt_get_key_value(const char **ropts,
+                         const char *key_val_sep, const char *pairs_sep,
+                         unsigned flags,
+                         char **rkey, char **rval)
+{
+    int ret;
+    char *key = NULL, *val;
+    const char *opts = *ropts;
+
+    if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
+        !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
+        return AVERROR(EINVAL);
+    if (!(val = av_get_token(&opts, pairs_sep))) {
+        av_free(key);
+        return AVERROR(ENOMEM);
+    }
+    if (*opts && strchr(pairs_sep, *opts))
+        opts++;
+    *ropts = opts;
+    *rkey  = key;
+    *rval  = val;
+    return 0;
+}
+
 int av_opt_set_from_string(void *ctx, const char *opts,
                            const char *const *shorthand,
                            const char *key_val_sep, const char *pairs_sep)
 {
     int ret, count = 0;
     const char *dummy_shorthand = NULL;
-    char *parsed_key, *value;
+    char *av_uninit(parsed_key), *av_uninit(value);
     const char *key;
 
     if (!opts)
@@ -843,25 +867,25 @@ int av_opt_set_from_string(void *ctx, const char *opts,
         shorthand = &dummy_shorthand;
 
     while (*opts) {
-        parsed_key = NULL; /* so we can free it anyway */
-        if ((ret = get_key(&opts, key_val_sep, &parsed_key)) < 0) {
-            if (*shorthand) {
-                key = *(shorthand++);
-            } else {
+        ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
+                                   *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
+                                   &parsed_key, &value);
+        if (ret < 0) {
+            if (ret == AVERROR(EINVAL))
                 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
-                return AVERROR(EINVAL);
-            }
-        } else {
+            else
+                av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
+                       av_err2str(ret));
+            return ret;
+        }
+        if (parsed_key) {
             key = parsed_key;
             while (*shorthand) /* discard all remaining shorthand */
                 shorthand++;
+        } else {
+            key = *(shorthand++);
         }
 
-        if (!(value = av_get_token(&opts, pairs_sep)))
-            return AVERROR(ENOMEM);
-        if (*opts && strchr(pairs_sep, *opts))
-            opts++;
-
         av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
         if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
             if (ret == AVERROR_OPTION_NOT_FOUND)
diff --git a/libavutil/opt.h b/libavutil/opt.h
index 81fefd9..a1165f0 100644
--- a/libavutil/opt.h
+++ b/libavutil/opt.h
@@ -455,6 +455,38 @@ int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
 int av_opt_set_dict(void *obj, struct AVDictionary **options);
 
 /**
+ * Extract a key-value pair from the beginning of a string.
+ *
+ * @param ropts        pointer to the options string, will be updated to
+ *                     point to the rest of the string
+ * @param key_val_sep  a 0-terminated list of characters used to separate
+ *                     key from value, for example '='
+ * @param pairs_sep    a 0-terminated list of characters used to separate
+ *                     two pairs from each other, for example ':' or ','
+ * @param flags        flags; see the AV_OPT_FLAG_* values below
+ * @param rkey         parsed key; must be freed using av_free()
+ * @param rval         parsed value; must be freed using av_free()
+ *
+ * @return  0 for success, or a negative value corresponding to an AVERROR
+ *          code in case of error; in particular:
+ *          AVERROR(EINVAL) if no key is present
+ *
+ */
+int av_opt_get_key_value(const char **ropts,
+                         const char *key_val_sep, const char *pairs_sep,
+                         unsigned flags,
+                         char **rkey, char **rval);
+
+enum {
+
+    /**
+     * Accept to parse a value without a key; the key will then be returned
+     * as NULL
+     */
+    AV_OPT_FLAG_IMPLICIT_KEY = 1,
+};
+
+/**
  * @defgroup opt_eval_funcs Evaluating option strings
  * @{
  * This group of functions can be used to evaluate option strings
diff --git a/libavutil/version.h b/libavutil/version.h
index 94a4a47..60a7450 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -75,7 +75,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  52
-#define LIBAVUTIL_VERSION_MINOR   1
+#define LIBAVUTIL_VERSION_MINOR   2
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list