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.
Unified · Split
| @@ -15,6 +15,9 @@ libavutil: 2014-08-09 · 14 unmodified lines | ||
| 15 | 15 | |
| 16 | 16 | API changes, most recent first: |
| 17 | 17 | |
| 18 | +2014-11-16 - xxxxxxx - lavu 54.13.0 - opt.h | |
| 19 | + Add av_opt_serialize(). | |
| 20 | + | |
| 18 | 21 | 2014-11-16 - xxxxxxx - lavu 54.12.0 - opt.h |
| 19 | 22 | Add av_opt_is_set_to_default(). |
| 20 | 23 | |
| @@ -37,6 +37,7 @@ · 36 unmodified lines | ||
| 37 | 37 | #include "pixdesc.h" |
| 38 | 38 | #include "mathematics.h" |
| 39 | 39 | #include "samplefmt.h" |
| 40 | +#include "bprint.h" | |
| 40 | 41 | |
| 41 | 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 | 1836 | return av_opt_is_set_to_default(target, o); |
| 1836 | 1837 | } |
| 1837 | 1838 | |
| 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 | + | |
| 1838 | 1877 | #ifdef TEST |
| 1839 | 1878 | |
| 1840 | 1879 | typedef struct TestContext |
| @@ -1854,6 +1893,10 @@ typedef struct TestContext · 13 unmodified lines | ||
| 1854 | 1893 | int64_t channel_layout; |
| 1855 | 1894 | void *binary; |
| 1856 | 1895 | int binary_size; |
| 1896 | + void *binary1; | |
| 1897 | + int binary_size1; | |
| 1898 | + void *binary2; | |
| 1899 | + int binary_size2; | |
| 1857 | 1900 | int64_t num64; |
| 1858 | 1901 | float flt; |
| 1859 | 1902 | double dbl; |
| @@ -1882,6 +1925,8 @@ static const AVOption test_options[]= { · 22 unmodified lines | ||
| 1882 | 1925 | {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0}, |
| 1883 | 1926 | {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX}, |
| 1884 | 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 | 1930 | {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100 }, |
| 1886 | 1931 | {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100 }, |
| 1887 | 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 | 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 | 2023 | printf("\nTesting av_set_options_string()\n"); |
| 1955 | 2024 | { |
| 1956 | 2025 | TestContext test_ctx = { 0 }; |
| @@ -869,6 +869,27 @@ int av_opt_is_set_to_default(void *obj, const AVOption *o); · 868 unmodified lines | ||
| 869 | 869 | */ |
| 870 | 870 | int 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 | + */ | |
| 891 | +int 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 | */ |
| @@ -56,7 +56,7 @@ · 55 unmodified lines | ||
| 56 | 56 | */ |
| 57 | 57 | |
| 58 | 58 | #define LIBAVUTIL_VERSION_MAJOR 54 |
| 59 | −#define LIBAVUTIL_VERSION_MINOR 12 | |
| 59 | +#define LIBAVUTIL_VERSION_MINOR 13 | |
| 60 | 60 | #define LIBAVUTIL_VERSION_MICRO 100 |
| 61 | 61 | |
| 62 | 62 | #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |
| @@ -34,6 +34,8 @@ name: duration default:0 error: · 33 unmodified lines | ||
| 34 | 34 | name: color default:0 error: |
| 35 | 35 | name: cl default:0 error: |
| 36 | 36 | name: bin default:0 error: |
| 37 | +name: bin1 default:1 error: | |
| 38 | +name: bin2 default:1 error: | |
| 37 | 39 | name: num64 default:0 error: |
| 38 | 40 | name: flt default:0 error: |
| 39 | 41 | name: dbl default:0 error: |
| @@ -53,10 +55,16 @@ name: duration default:1 error: · 13 unmodified lines | ||
| 53 | 55 | name: color default:1 error: |
| 54 | 56 | name: cl default:1 error: |
| 55 | 57 | name: bin default:1 error: |
| 58 | +name: bin1 default:1 error: | |
| 59 | +name: bin2 default:1 error: | |
| 56 | 60 | name: num64 default:1 error: |
| 57 | 61 | name: flt default:1 error: |
| 58 | 62 | name: dbl default:1 error: |
| 59 | 63 | |
| 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 | + | |
| 60 | 68 | Testing av_set_options_string() |
| 61 | 69 | OK '' |
| 62 | 70 | Error ':' |