]> git.sesse.net Git - ffmpeg/commitdiff
opt: Add av_opt_copy()
authorMichael Niedermayer <michaelni@gmx.at>
Fri, 30 May 2014 19:04:14 +0000 (21:04 +0200)
committerMartin Storsjö <martin@martin.st>
Thu, 24 Mar 2016 08:34:15 +0000 (10:34 +0200)
This includes documentation and other modifications by
Lukasz Marek and Martin Storsjö.

Signed-off-by: Martin Storsjö <martin@martin.st>
doc/APIchanges
libavutil/opt.c
libavutil/opt.h
libavutil/version.h

index 4dae05ecde60bddcfcf19e2ec26428568959dc92..c3b81faf36d13448896e4eb21820f2de50ecad58 100644 (file)
@@ -13,6 +13,9 @@ libavutil:     2015-08-28
 
 API changes, most recent first:
 
+2016-xx-xx - xxxxxxx - lavu 55.10.0 - opt.h
+  Add av_opt_copy().
+
 2016-xx-xx - xxxxxxx - lavc 57.16.0 - avcodec.h
   Add a new audio/video encoding and decoding API with decoupled input
   and output -- avcodec_send_packet(), avcodec_receive_frame(),
index c5a1933aa35cd7312be15c2b1065861846c74182..01502435b772eacfa5f23e007420466a5decda1d 100644 (file)
@@ -757,6 +757,72 @@ const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *pre
     return NULL;
 }
 
+static int opt_size(enum AVOptionType type)
+{
+    switch(type) {
+    case AV_OPT_TYPE_INT:
+    case AV_OPT_TYPE_FLAGS:     return sizeof(int);
+    case AV_OPT_TYPE_INT64:     return sizeof(int64_t);
+    case AV_OPT_TYPE_DOUBLE:    return sizeof(double);
+    case AV_OPT_TYPE_FLOAT:     return sizeof(float);
+    case AV_OPT_TYPE_STRING:    return sizeof(uint8_t*);
+    case AV_OPT_TYPE_RATIONAL:  return sizeof(AVRational);
+    case AV_OPT_TYPE_BINARY:    return sizeof(uint8_t*) + sizeof(int);
+    }
+    return AVERROR(EINVAL);
+}
+
+int av_opt_copy(void *dst, const void *src)
+{
+    const AVOption *o = NULL;
+    const AVClass *c;
+    int ret = 0;
+
+    if (!src)
+        return AVERROR(EINVAL);
+
+    c = *(AVClass**)src;
+    if (!c || c != *(AVClass**)dst)
+        return AVERROR(EINVAL);
+
+    while ((o = av_opt_next(src, o))) {
+        void *field_dst = ((uint8_t*)dst) + o->offset;
+        void *field_src = ((uint8_t*)src) + o->offset;
+        uint8_t **field_dst8 = (uint8_t**)field_dst;
+        uint8_t **field_src8 = (uint8_t**)field_src;
+
+        if (o->type == AV_OPT_TYPE_STRING) {
+            set_string(dst, o, *field_src8, field_dst8);
+            if (*field_src8 && !*field_dst8)
+                ret = AVERROR(ENOMEM);
+        } else if (o->type == AV_OPT_TYPE_BINARY) {
+            int len = *(int*)(field_src8 + 1);
+            if (*field_dst8 != *field_src8)
+                av_freep(field_dst8);
+            if (len) {
+                *field_dst8 = av_malloc(len);
+                if (!*field_dst8) {
+                    ret = AVERROR(ENOMEM);
+                    len = 0;
+                }
+                memcpy(*field_dst8, *field_src8, len);
+            } else {
+                *field_dst8 = NULL;
+            }
+            *(int*)(field_dst8 + 1) = len;
+        } else if (o->type == AV_OPT_TYPE_CONST) {
+            // do nothing
+        } else {
+            int size = opt_size(o->type);
+            if (size < 0)
+                ret = size;
+            else
+                memcpy(field_dst, field_src, size);
+        }
+    }
+    return ret;
+}
+
 #ifdef TEST
 
 typedef struct TestContext
index 99d727cad3dba98708b9fc0beb87f3d96664c325..906b869d511025c4d6af0c6be4d912deca4c9bfe 100644 (file)
@@ -530,6 +530,19 @@ int av_opt_get_q       (void *obj, const char *name, int search_flags, AVRationa
  * be freed with av_dict_free() by the caller
  */
 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val);
+
+/**
+ * Copy options from src object into dest object.
+ *
+ * Options that require memory allocation (e.g. string or binary) are malloc'ed in dest object.
+ * Original memory allocated for such options is freed unless both src and dest options points to the same memory.
+ *
+ * @param dest Object to copy from
+ * @param src  Object to copy into
+ * @return 0 on success, negative on error
+ */
+int av_opt_copy(void *dest, const void *src);
+
 /**
  * @}
  * @}
index 235cdfd4ac0c00af035a4cc02c967d81725e896b..186ebd8eb5e8237fd40ade16f3128c27910e1cee 100644 (file)
@@ -54,8 +54,8 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 55
-#define LIBAVUTIL_VERSION_MINOR  9
-#define LIBAVUTIL_VERSION_MICRO  1
+#define LIBAVUTIL_VERSION_MINOR 10
+#define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \