History

lavu/opt: introduce av_opt_is_set_to_default()

New function allows to check if option is set to its default value

Lukasz Marek committed 12 years ago

Commit eec69331 parent 01974a55 files · +194 −1Browse files

Unified · Split

5 files
  1. doc/APIchanges +3 −0
  2. libavutil/opt.c +124 −0
  3. libavutil/opt.h +26 −0
  4. libavutil/version.h +1 −1
  5. tests/ref/fate/opt +40 −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.12.0 - opt.h
19 Add av_opt_is_set_to_default().
20
182014-11-xx - xxxxxxx - lavc 56.6.0 - vorbis_parser.h212014-11-xx - xxxxxxx - lavc 56.6.0 - vorbis_parser.h
19 Add a public API for parsing vorbis packets.22 Add a public API for parsing vorbis packets.
20 23
libavutil/opt.c+124−0
@@ -1732,6 +1732,109 @@ void av_opt_freep_ranges(AVOptionRanges **rangesp) · 1731 unmodified lines
1732 av_freep(rangesp);1732 av_freep(rangesp);
1733}1733}
1734 1734
1735int av_opt_is_set_to_default(void *obj, const AVOption *o)
1736{
1737 int64_t i64;
1738 double d, d2;
1739 float f;
1740 AVRational q;
1741 int ret, w, h;
1742 char *str;
1743 void *dst;
1744
1745 if (!o || !obj)
1746 return AVERROR(EINVAL);
1747
1748 dst = ((uint8_t*)obj) + o->offset;
1749
1750 switch (o->type) {
1751 case AV_OPT_TYPE_CONST:
1752 return 1;
1753 case AV_OPT_TYPE_FLAGS:
1754 case AV_OPT_TYPE_PIXEL_FMT:
1755 case AV_OPT_TYPE_SAMPLE_FMT:
1756 case AV_OPT_TYPE_INT:
1757 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1758 case AV_OPT_TYPE_DURATION:
1759 case AV_OPT_TYPE_INT64:
1760 read_number(o, dst, NULL, NULL, &i64);
1761 return o->default_val.i64 == i64;
1762 case AV_OPT_TYPE_STRING:
1763 str = *(char **)dst;
1764 if (str == o->default_val.str) //2 NULLs
1765 return 1;
1766 if (!str || !o->default_val.str) //1 NULL
1767 return 0;
1768 return !strcmp(str, o->default_val.str);
1769 case AV_OPT_TYPE_DOUBLE:
1770 read_number(o, dst, &d, NULL, NULL);
1771 return o->default_val.dbl == d;
1772 case AV_OPT_TYPE_FLOAT:
1773 read_number(o, dst, &d, NULL, NULL);
1774 f = o->default_val.dbl;
1775 d2 = f;
1776 return d2 == d;
1777 case AV_OPT_TYPE_RATIONAL:
1778 q = av_d2q(o->default_val.dbl, INT_MAX);
1779 return !av_cmp_q(*(AVRational*)dst, q);
1780 case AV_OPT_TYPE_BINARY: {
1781 struct {
1782 uint8_t *data;
1783 int size;
1784 } tmp = {0};
1785 int opt_size = *(int *)((void **)dst + 1);
1786 void *opt_ptr = *(void **)dst;
1787 if (!opt_ptr && (!o->default_val.str || !strlen(o->default_val.str)))
1788 return 1;
1789 if (opt_ptr && o->default_val.str && !strlen(o->default_val.str))
1790 return 0;
1791 if (opt_size != strlen(o->default_val.str) / 2)
1792 return 0;
1793 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1794 if (!ret)
1795 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1796 av_free(tmp.data);
1797 return ret;
1798 }
1799 case AV_OPT_TYPE_DICT:
1800 /* Binary and dict have not default support yet. Any pointer is not default. */
1801 return !!(*(void **)dst);
1802 case AV_OPT_TYPE_IMAGE_SIZE:
1803 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1804 w = h = 0;
1805 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1806 return ret;
1807 return (w == *(int *)dst) && (h == *((int *)dst+1));
1808 case AV_OPT_TYPE_VIDEO_RATE:
1809 q = (AVRational){0, 0};
1810 if (o->default_val.str)
1811 av_parse_video_rate(&q, o->default_val.str);
1812 return !av_cmp_q(*(AVRational*)dst, q);
1813 case AV_OPT_TYPE_COLOR: {
1814 uint8_t color[4] = {0, 0, 0, 0};
1815 if (o->default_val.str)
1816 av_parse_color(color, o->default_val.str, -1, NULL);
1817 return !memcmp(color, dst, sizeof(color));
1818 }
1819 default:
1820 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1821 break;
1822 }
1823 return AVERROR_PATCHWELCOME;
1824}
1825
1826int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1827{
1828 const AVOption *o;
1829 void *target;
1830 if (!obj)
1831 return AVERROR(EINVAL);
1832 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1833 if (!o)
1834 return AVERROR_OPTION_NOT_FOUND;
1835 return av_opt_is_set_to_default(target, o);
1836}
1837
1735#ifdef TEST1838#ifdef TEST
1736 1839
1737typedef struct TestContext1840typedef struct TestContext
@@ -1827,6 +1930,27 @@ int main(void) · 89 unmodified lines
1827 av_opt_free(&test_ctx);1930 av_opt_free(&test_ctx);
1828 }1931 }
1829 1932
1933 printf("\nTesting av_opt_is_set_to_default()\n");
1934 {
1935 int ret;
1936 TestContext test_ctx = { 0 };
1937 const AVOption *o = NULL;
1938 test_ctx.class = &test_class;
1939
1940 av_log_set_level(AV_LOG_QUIET);
1941
1942 while (o = av_opt_next(&test_ctx, o)) {
1943 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
1944 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
1945 }
1946 av_opt_set_defaults(&test_ctx);
1947 while (o = av_opt_next(&test_ctx, o)) {
1948 ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
1949 printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
1950 }
1951 av_opt_free(&test_ctx);
1952 }
1953
1830 printf("\nTesting av_set_options_string()\n");1954 printf("\nTesting av_set_options_string()\n");
1831 {1955 {
1832 TestContext test_ctx = { 0 };1956 TestContext test_ctx = { 0 };
libavutil/opt.h+26−0
@@ -843,6 +843,32 @@ int av_opt_copy(void *dest, void *src); · 842 unmodified lines
843 */843 */
844int av_opt_query_ranges_default(AVOptionRanges **, void *obj, const char *key, int flags);844int av_opt_query_ranges_default(AVOptionRanges **, void *obj, const char *key, int flags);
845 845
846/**
847 * Check if given option is set to its default value.
848 *
849 * Options o must belong to the obj. This function must not be called to check child's options state.
850 * @see av_opt_is_set_to_default_by_name().
851 *
852 * @param obj AVClass object to check option on
853 * @param o option to be checked
854 * @return >0 when option is set to its default,
855 * 0 when option is not set its default,
856 * <0 on error
857 */
858int av_opt_is_set_to_default(void *obj, const AVOption *o);
859
860/**
861 * Check if given option is set to its default value.
862 *
863 * @param obj AVClass object to check option on
864 * @param name option name
865 * @param search_flags combination of AV_OPT_SEARCH_*
866 * @return >0 when option is set to its default,
867 * 0 when option is not set its default,
868 * <0 on error
869 */
870int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags);
871
846/**872/**
847 * @}873 * @}
848 */874 */
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 1159#define LIBAVUTIL_VERSION_MINOR 12
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+40−0
@@ -17,6 +17,46 @@ num64=1 · 16 unmodified lines
17flt=0.33333317flt=0.333333
18dbl=0.33333318dbl=0.333333
19 19
20Testing av_opt_is_set_to_default()
21name: num default:1 error:
22name: toggle default:0 error:
23name: rational default:0 error:
24name: string default:0 error:
25name: flags default:0 error:
26name: cool default:1 error:Option not found
27name: lame default:1 error:Option not found
28name: mu default:1 error:Option not found
29name: size default:0 error:
30name: pix_fmt default:0 error:
31name:sample_fmt default:0 error:
32name:video_rate default:0 error:
33name: duration default:0 error:
34name: color default:0 error:
35name: cl default:0 error:
36name: bin default:0 error:
37name: num64 default:0 error:
38name: flt default:0 error:
39name: dbl default:0 error:
40name: num default:1 error:
41name: toggle default:1 error:
42name: rational default:1 error:
43name: string default:1 error:
44name: flags default:1 error:
45name: cool default:1 error:Option not found
46name: lame default:1 error:Option not found
47name: mu default:1 error:Option not found
48name: size default:1 error:
49name: pix_fmt default:1 error:
50name:sample_fmt default:1 error:
51name:video_rate default:1 error:
52name: duration default:1 error:
53name: color default:1 error:
54name: cl default:1 error:
55name: bin default:1 error:
56name: num64 default:1 error:
57name: flt default:1 error:
58name: dbl default:1 error:
59
20Testing av_set_options_string()60Testing av_set_options_string()
21OK ''61OK ''
22Error ':'62Error ':'

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