On date Tuesday 2014-11-11 11:02:32 +0100, wm4 encoded:
> On Tue, 11 Nov 2014 08:31:24 +0100
> Lukasz Marek <lukasz.m.lu...@gmail.com> wrote:
> 
> > TODO: bump minor version, update doc/APIchanges
> > 
> > Function allows to create string containing object's serialized options.
> > Such string may be passed back to av_set_options_string() in order to 
> > restore options.
> > 
> > Signed-off-by: Lukasz Marek <lukasz.m.lu...@gmail.com>
> > +Test av_opt_serialize()
> > +num=0,toggle=1,rational=1,string=default,flags=1,size=200x300,pix_fmt=297,sample_fmt=1,video_rate=25,duration=0.001000,color=0xffc0cbff,cl=311,bin=62696e00,bin1=,bin2=,num64=1,flt=0.3333333433,dbl=0.3333333333
> > +num=0,toggle=1,rational=1,string=default,flags=1,size=200x300,pix_fmt=297,sample_fmt=1,video_rate=25,duration=0.001000,color=0xffc0cbff,cl=311,bin=62696e00,bin1=,bin2=,num64=1,flt=0.3333333433,dbl=0.3333333333
> > +
> >  Testing av_set_options_string()
> >  OK    setting options string: ''
> >  Error setting options string: ':'
> 
[...]
> IMO it's quite WTFish to introduce such extensive functionality that
> is only going to be needed by a single broken thing (FFserver).

No I think this is useful on its own (I had a patch about that, in
attachment, it was part of a larger changeset:
 lavu/opt: make av_opt_get() return NULL in case a string is not set
 lavu/bprint: add av_bprint_options()
 lavc: add "s" and "pix_fmt" options to AVCodecContext
 doc: add section "Options lookup order"
 lavc: add sample_fmt option
 lavc/utils: shows context options

For example it is useful to print the configuration of an encoder, and
copy&paste its configuration from one application to another.
 
> Can't FFserver's design be fixed instead?

How do you suggest to fix it?
-- 
FFmpeg = Fundamentalist and Fiendish Mere Puritan Excellent Gadget
>From 32e802c0f6d10fa4284a696f7f2219efad3cf2b4 Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefa...@gmail.com>
Date: Mon, 4 Mar 2013 18:30:31 +0100
Subject: [PATCH] lavu/opt: make av_opt_get() return NULL in case a string is
 not set

This allows to distinguish between the case when the string is not set,
and the string is set to the empty string.

TODO: bump micro
---
 libavutil/opt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavutil/opt.c b/libavutil/opt.c
index c035307..aeea82d 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -600,7 +600,7 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
         if (*(uint8_t**)dst)
             *out_val = av_strdup(*(uint8_t**)dst);
         else
-            *out_val = av_strdup("");
+            *out_val = NULL;
         return 0;
     case AV_OPT_TYPE_BINARY:
         len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
-- 
1.8.3.2

>From 8b0a701cc17262f649aa0339f55561ee19b9b1b7 Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefa...@gmail.com>
Date: Thu, 9 Aug 2012 19:59:48 +0200
Subject: [PATCH] lavu/bprint: add av_bprint_options()

This function will be useful to serialize an object context.

TODO: add APIChanges entry, bump minor
---
 libavutil/bprint.c | 23 +++++++++++++++++++++++
 libavutil/bprint.h | 15 +++++++++++++++
 libavutil/opt.c    | 10 +++++++++-
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index cd91bc4..64edecb 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -29,6 +29,7 @@
 #include "compat/va_copy.h"
 #include "error.h"
 #include "mem.h"
+#include "opt.h"
 
 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
@@ -286,6 +287,28 @@ void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_cha
     }
 }
 
+void av_bprint_options(struct AVBPrint *bp, void *ctx,
+                       const char *key_val_sep, const char *pairs_sep)
+{
+    const AVOption *opt = NULL, *prev_opt = NULL;
+
+    while (opt = av_opt_next(ctx, opt)) {
+        uint8_t *val;
+        char sep[2] = { pairs_sep[0], 0 };
+
+        /* skip duplicated option values */
+        if (prev_opt && prev_opt->offset == opt->offset)
+            continue;
+        av_opt_get(ctx, opt->name, AV_OPT_SEARCH_CHILDREN, &val);
+        if (opt->offset && val) {
+            av_bprintf(bp, "%s%s%c", prev_opt ? sep : "", opt->name, key_val_sep[0]);
+            av_bprint_escape(bp, val, pairs_sep, AV_ESCAPE_MODE_AUTO, 0);
+            av_freep(&val);
+        }
+        prev_opt = opt;
+    }
+}
+
 #ifdef TEST
 
 #undef printf
diff --git a/libavutil/bprint.h b/libavutil/bprint.h
index bb1de25..9053703 100644
--- a/libavutil/bprint.h
+++ b/libavutil/bprint.h
@@ -204,4 +204,19 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str);
 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
                       enum AVEscapeMode mode, int flags);
 
+/**
+ * Append a compact description of the context options to a bprint buffer.
+ * The obtained string is escaped, and can be used to set options through the function
+ * av_opt_set_from_string() or equivalent.
+ *
+ * @param bp           already inited destination bprint buffer
+ * @param ctx          pointer to a context whose first field is a pointer to an AVClass
+ * @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 ','
+ */
+void av_bprint_options(struct AVBPrint *bp, void *ctx,
+                       const char *key_val_sep, const char *pairs_sep);
+
 #endif /* AVUTIL_BPRINT_H */
diff --git a/libavutil/opt.c b/libavutil/opt.c
index aeea82d..3e35d4f 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -27,7 +27,7 @@
 
 #include "avutil.h"
 #include "avstring.h"
-#include "common.h"
+#include "bprint.h"
 #include "opt.h"
 #include "eval.h"
 #include "dict.h"
@@ -1480,6 +1480,7 @@ static const AVOption test_options[]= {
 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
 {"mu",       "set mu flag ",   0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_MU},   INT_MIN,  INT_MAX, 0, "flags" },
 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
+{"s",        "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE,  {.str = "25"}, 0,     0                   },
@@ -1502,6 +1503,7 @@ static const AVClass test_class = {
 int main(void)
 {
     int i;
+    AVBPrint bp;
 
     printf("\nTesting av_set_options_string()\n");
     {
@@ -1559,6 +1561,12 @@ int main(void)
                 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
             printf("\n");
         }
+
+        av_bprint_init(&bp, 1, AV_BPRINT_SIZE_UNLIMITED);
+        av_bprint_options(&bp, &test_ctx, "=", ":");
+        printf("test_ctx=%s\n", bp.str);
+        av_bprint_clear(&bp);
+
         av_freep(&test_ctx.string);
     }
 
-- 
1.8.3.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to