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
15 15
16API changes, most recent first:16API changes, most recent first:
17 17
182014-11-16 - xxxxxxx - lavu 54.13.0 - opt.h
19 Add av_opt_serialize().
20
182014-11-16 - xxxxxxx - lavu 54.12.0 - opt.h212014-11-16 - xxxxxxx - lavu 54.12.0 - opt.h
19 Add av_opt_is_set_to_default().22 Add av_opt_is_set_to_default().
20 23
libavutil/opt.c+69−0
@@ -37,6 +37,7 @@ · 36 unmodified lines
37#include "pixdesc.h"37#include "pixdesc.h"
38#include "mathematics.h"38#include "mathematics.h"
39#include "samplefmt.h"39#include "samplefmt.h"
40#include "bprint.h"
40 41
41#include <float.h>42#include <float.h>
42 43
@@ -1835,6 +1836,44 @@ int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_fla · 1792 unmodified lines
1835 return av_opt_is_set_to_default(target, o);1836 return av_opt_is_set_to_default(target, o);
1836}1837}
1837 1838
1839int 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
1838#ifdef TEST1877#ifdef TEST
1839 1878
1840typedef struct TestContext1879typedef struct TestContext
@@ -1854,6 +1893,10 @@ typedef struct TestContext · 13 unmodified lines
1854 int64_t channel_layout;1893 int64_t channel_layout;
1855 void *binary;1894 void *binary;
1856 int binary_size;1895 int binary_size;
1896 void *binary1;
1897 int binary_size1;
1898 void *binary2;
1899 int binary_size2;
1857 int64_t num64;1900 int64_t num64;
1858 float flt;1901 float flt;
1859 double dbl;1902 double dbl;
@@ -1882,6 +1925,8 @@ static const AVOption test_options[]= { · 22 unmodified lines
1882{"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},1925{"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1883{"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},1926{"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX},
1884{"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0 },1927{"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 },
1885{"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100 },1930{"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100 },
1886{"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100 },1931{"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100 },
1887{"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100 },1932{"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
1951 av_opt_free(&test_ctx);1996 av_opt_free(&test_ctx);
1952 }1997 }
1953 1998
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
1954 printf("\nTesting av_set_options_string()\n");2023 printf("\nTesting av_set_options_string()\n");
1955 {2024 {
1956 TestContext test_ctx = { 0 };2025 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
869 */869 */
870int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags);870int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags);
871 871
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 */
891int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
892 const char key_val_sep, const char pairs_sep);
872/**893/**
873 * @}894 * @}
874 */895 */
libavutil/version.h+1−1
@@ -56,7 +56,7 @@ · 55 unmodified lines
56 */56 */
57 57
58#define LIBAVUTIL_VERSION_MAJOR 5458#define LIBAVUTIL_VERSION_MAJOR 54
59#define LIBAVUTIL_VERSION_MINOR 1259#define LIBAVUTIL_VERSION_MINOR 13
60#define LIBAVUTIL_VERSION_MICRO 10060#define LIBAVUTIL_VERSION_MICRO 100
61 61
62#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \62#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
34name: color default:0 error:34name: color default:0 error:
35name: cl default:0 error:35name: cl default:0 error:
36name: bin default:0 error:36name: bin default:0 error:
37name: bin1 default:1 error:
38name: bin2 default:1 error:
37name: num64 default:0 error:39name: num64 default:0 error:
38name: flt default:0 error:40name: flt default:0 error:
39name: dbl default:0 error:41name: dbl default:0 error:
@@ -53,10 +55,16 @@ name: duration default:1 error: · 13 unmodified lines
53name: color default:1 error:55name: color default:1 error:
54name: cl default:1 error:56name: cl default:1 error:
55name: bin default:1 error:57name: bin default:1 error:
58name: bin1 default:1 error:
59name: bin2 default:1 error:
56name: num64 default:1 error:60name: num64 default:1 error:
57name: flt default:1 error:61name: flt default:1 error:
58name: dbl default:1 error:62name: dbl default:1 error:
59 63
64Test av_opt_serialize()
65num=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
66num=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
60Testing av_set_options_string()68Testing av_set_options_string()
61OK ''69OK ''
62Error ':'70Error ':'

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