History

lavu/opt: introduce av_opt_serialize()

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.

Lukasz Marek committed 12 years ago

Commit bee58441 parent eec69335 files · +102 −1Browse files

Unified · Split

5 files
  1. doc/APIchanges +3 −0
  2. libavutil/opt.c +69 −0
  3. libavutil/opt.h +21 −0
  4. libavutil/version.h +1 −1
  5. tests/ref/fate/opt +8 −0
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2014-08-09 · 14 unmodified lines
1515
1616 API changes, most recent first:
1717
18+2014-11-16 - xxxxxxx - lavu 54.13.0 - opt.h
19+ Add av_opt_serialize().
20+
1821 2014-11-16 - xxxxxxx - lavu 54.12.0 - opt.h
1922 Add av_opt_is_set_to_default().
2023
libavutil/opt.c+69−0
@@ -37,6 +37,7 @@ · 36 unmodified lines
3737 #include "pixdesc.h"
3838 #include "mathematics.h"
3939 #include "samplefmt.h"
40+#include "bprint.h"
4041
4142 #include <float.h>
4243
@@ -1835,6 +1836,44 @@ int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_fla · 1792 unmodified lines
18351836 return av_opt_is_set_to_default(target, o);
18361837 }
18371838
1839+int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1840+ const char key_val_sep, const char pairs_sep)
1841+{
1842+ const AVOption *o = NULL;
1843+ uint8_t *buf;
1844+ AVBPrint bprint;
1845+ int ret, cnt = 0;
1846+
1847+ if (!obj || !buffer)
1848+ return AVERROR(EINVAL);
1849+
1850+ *buffer = NULL;
1851+ av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
1852+
1853+ while (o = av_opt_next(obj, o)) {
1854+ if (o->type == AV_OPT_TYPE_CONST)
1855+ continue;
1856+ if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1857+ continue;
1858+ else if (((o->flags & opt_flags) != opt_flags))
1859+ continue;
1860+ if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1861+ continue;
1862+ if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1863+ av_bprint_finalize(&bprint, NULL);
1864+ return ret;
1865+ }
1866+ if (buf) {
1867+ if (cnt++)
1868+ av_bprint_append_data(&bprint, &pairs_sep, 1);
1869+ av_bprintf(&bprint, "%s%c%s", o->name, key_val_sep, buf);
1870+ av_freep(&buf);
1871+ }
1872+ }
1873+ av_bprint_finalize(&bprint, buffer);
1874+ return 0;
1875+}
1876+
18381877 #ifdef TEST
18391878
18401879 typedef struct TestContext
@@ -1854,6 +1893,10 @@ typedef struct TestContext · 13 unmodified lines
18541893 int64_t channel_layout;
18551894 void *binary;
18561895 int binary_size;
1896+ void *binary1;
1897+ int binary_size1;
1898+ void *binary2;
1899+ int binary_size2;
18571900 int64_t num64;
18581901 float flt;
18591902 double dbl;
@@ -1882,6 +1925,8 @@ static const AVOption test_options[]= { · 22 unmodified lines
18821925 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
18831926 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
18841927 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0 },
1928+{"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, {.str=NULL}, 0, 0 },
1929+{"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, {.str=""}, 0, 0 },
18851930 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100 },
18861931 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100 },
18871932 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100 },
@@ -1951,6 +1996,30 @@ int main(void) · 63 unmodified lines
19511996 av_opt_free(&test_ctx);
19521997 }
19531998
1999+ printf("\nTest av_opt_serialize()\n");
2000+ {
2001+ TestContext test_ctx = { 0 };
2002+ char *buf;
2003+ test_ctx.class = &test_class;
2004+
2005+ av_log_set_level(AV_LOG_QUIET);
2006+
2007+ av_opt_set_defaults(&test_ctx);
2008+ if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2009+ printf("%s\n", buf);
2010+ av_opt_free(&test_ctx);
2011+ memset(&test_ctx, 0, sizeof(test_ctx));
2012+ test_ctx.class = &test_class;
2013+ av_set_options_string(&test_ctx, buf, "=", ",");
2014+ av_free(buf);
2015+ if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2016+ printf("%s\n", buf);
2017+ av_free(buf);
2018+ }
2019+ }
2020+ av_opt_free(&test_ctx);
2021+ }
2022+
19542023 printf("\nTesting av_set_options_string()\n");
19552024 {
19562025 TestContext test_ctx = { 0 };
libavutil/opt.h+21−0
@@ -869,6 +869,27 @@ int av_opt_is_set_to_default(void *obj, const AVOption *o); · 868 unmodified lines
869869 */
870870 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags);
871871
872+
873+#define AV_OPT_SERIALIZE_SKIP_DEFAULTS 0x00000001 ///< Serialize options that are not set to default values only.
874+#define AV_OPT_SERIALIZE_OPT_FLAGS_EXACT 0x00000002 ///< Serialize options that exactly match opt_flags only.
875+
876+/**
877+ * Serialize object's options.
878+ *
879+ * Create a string containing object's serialized options.
880+ * Such string may be passed back to av_opt_set_from_string() in order to restore option values.
881+ *
882+ * @param[in] obj AVClass object to serialize
883+ * @param[in] opt_flags serialize options with all the specified flags set (AV_OPT_FLAG)
884+ * @param[in] flags combination of AV_OPT_SERIALIZE_* flags
885+ * @param[out] buffer Pointer to buffer that will be allocated with string containg serialized options.
886+ * Buffer must be freed by the caller when is no longer needed.
887+ * @param[in] key_val_sep character used to separate key from value
888+ * @param[in] pairs_sep character used to separate two pairs from each other
889+ * @return >= 0 on success, negative on error
890+ */
891+int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
892+ const char key_val_sep, const char pairs_sep);
872893 /**
873894 * @}
874895 */
libavutil/version.h+1−1
@@ -56,7 +56,7 @@ · 55 unmodified lines
5656 */
5757
5858 #define LIBAVUTIL_VERSION_MAJOR 54
59−#define LIBAVUTIL_VERSION_MINOR 12
59+#define LIBAVUTIL_VERSION_MINOR 13
6060 #define LIBAVUTIL_VERSION_MICRO 100
6161
6262 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
tests/ref/fate/opt+8−0
@@ -34,6 +34,8 @@ name: duration default:0 error: · 33 unmodified lines
3434 name: color default:0 error:
3535 name: cl default:0 error:
3636 name: bin default:0 error:
37+name: bin1 default:1 error:
38+name: bin2 default:1 error:
3739 name: num64 default:0 error:
3840 name: flt default:0 error:
3941 name: dbl default:0 error:
@@ -53,10 +55,16 @@ name: duration default:1 error: · 13 unmodified lines
5355 name: color default:1 error:
5456 name: cl default:1 error:
5557 name: bin default:1 error:
58+name: bin1 default:1 error:
59+name: bin2 default:1 error:
5660 name: num64 default:1 error:
5761 name: flt default:1 error:
5862 name: dbl default:1 error:
5963
64+Test av_opt_serialize()
65+num=0,toggle=1,rational=1/1,string=default,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0:00:00.001000,color=0xffc0cbff,cl=0x137,bin=62696E00,bin1=,bin2=,num64=1,flt=0.333333,dbl=0.333333
66+num=0,toggle=1,rational=1/1,string=default,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0:00:00.001000,color=0xffc0cbff,cl=0x137,bin=62696E00,bin1=,bin2=,num64=1,flt=0.333333,dbl=0.333333
67+
6068 Testing av_set_options_string()
6169 OK ''
6270 Error ':'

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t