Browse code

avutil/opt: add AV_OPT_TYPE_BOOL

Clément Bœsch authored on 2015/09/07 02:10:53
Showing 4 changed files
... ...
@@ -60,6 +60,7 @@ static int read_number(const AVOption *o, const void *dst, double *num, int *den
60 60
     case AV_OPT_TYPE_FLAGS:     *intnum = *(unsigned int*)dst;return 0;
61 61
     case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst;return 0;
62 62
     case AV_OPT_TYPE_SAMPLE_FMT:*intnum = *(enum AVSampleFormat*)dst;return 0;
63
+    case AV_OPT_TYPE_BOOL:
63 64
     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
64 65
     case AV_OPT_TYPE_CHANNEL_LAYOUT:
65 66
     case AV_OPT_TYPE_DURATION:
... ...
@@ -96,6 +97,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
96 96
     switch (o->type) {
97 97
     case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num/den) * intnum; break;
98 98
     case AV_OPT_TYPE_SAMPLE_FMT:*(enum AVSampleFormat*)dst = llrint(num/den) * intnum; break;
99
+    case AV_OPT_TYPE_BOOL:
99 100
     case AV_OPT_TYPE_FLAGS:
100 101
     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
101 102
     case AV_OPT_TYPE_DURATION:
... ...
@@ -297,6 +299,44 @@ static int set_string_color(void *obj, const AVOption *o, const char *val, uint8
297 297
     return 0;
298 298
 }
299 299
 
300
+static const char *get_bool_name(int val)
301
+{
302
+    if (val < 0)
303
+        return "auto";
304
+    return val ? "true" : "false";
305
+}
306
+
307
+static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
308
+{
309
+    int n;
310
+
311
+    if (!val)
312
+        return 0;
313
+
314
+    if (!strcmp(val, "auto")) {
315
+        n = -1;
316
+    } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
317
+        n = 1;
318
+    } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
319
+        n = 0;
320
+    } else {
321
+        char *end = NULL;
322
+        n = strtol(val, &end, 10);
323
+        if (val + strlen(val) != end)
324
+            goto fail;
325
+    }
326
+
327
+    if (n < o->min || n > o->max)
328
+        goto fail;
329
+
330
+    *dst = n;
331
+    return 0;
332
+
333
+fail:
334
+    av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
335
+    return AVERROR(EINVAL);
336
+}
337
+
300 338
 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
301 339
                           int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
302 340
 {
... ...
@@ -360,7 +400,7 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
360 360
                  o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
361 361
                  o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
362 362
                  o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
363
-                 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT))
363
+                 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
364 364
         return AVERROR(EINVAL);
365 365
 
366 366
     if (o->flags & AV_OPT_FLAG_READONLY)
... ...
@@ -368,6 +408,7 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
368 368
 
369 369
     dst = ((uint8_t*)target_obj) + o->offset;
370 370
     switch (o->type) {
371
+    case AV_OPT_TYPE_BOOL:     return set_string_bool(obj, o, val, dst);
371 372
     case AV_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
372 373
     case AV_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
373 374
     case AV_OPT_TYPE_FLAGS:
... ...
@@ -613,6 +654,9 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
613 613
 
614 614
     buf[0] = 0;
615 615
     switch (o->type) {
616
+    case AV_OPT_TYPE_BOOL:
617
+        ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
618
+        break;
616 619
     case AV_OPT_TYPE_FLAGS:     ret = snprintf(buf, sizeof(buf), "0x%08X",  *(int    *)dst);break;
617 620
     case AV_OPT_TYPE_INT:       ret = snprintf(buf, sizeof(buf), "%d" ,     *(int    *)dst);break;
618 621
     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
... ...
@@ -950,6 +994,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
950 950
             case AV_OPT_TYPE_CHANNEL_LAYOUT:
951 951
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
952 952
                 break;
953
+            case AV_OPT_TYPE_BOOL:
954
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
955
+                break;
953 956
             case AV_OPT_TYPE_CONST:
954 957
             default:
955 958
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
... ...
@@ -995,6 +1042,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
995 995
                   !opt->default_val.str)) {
996 996
             av_log(av_log_obj, AV_LOG_INFO, " (default ");
997 997
             switch (opt->type) {
998
+            case AV_OPT_TYPE_BOOL:
999
+                av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1000
+                break;
998 1001
             case AV_OPT_TYPE_FLAGS:
999 1002
                 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1000 1003
                 break;
... ...
@@ -1070,6 +1120,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
1070 1070
             case AV_OPT_TYPE_CONST:
1071 1071
                 /* Nothing to be done here */
1072 1072
             break;
1073
+            case AV_OPT_TYPE_BOOL:
1073 1074
             case AV_OPT_TYPE_FLAGS:
1074 1075
             case AV_OPT_TYPE_INT:
1075 1076
             case AV_OPT_TYPE_INT64:
... ...
@@ -1428,6 +1479,7 @@ void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1428 1428
 static int opt_size(enum AVOptionType type)
1429 1429
 {
1430 1430
     switch(type) {
1431
+    case AV_OPT_TYPE_BOOL:
1431 1432
     case AV_OPT_TYPE_INT:
1432 1433
     case AV_OPT_TYPE_FLAGS:     return sizeof(int);
1433 1434
     case AV_OPT_TYPE_DURATION:
... ...
@@ -1545,6 +1597,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch
1545 1545
     range->value_max = field->max;
1546 1546
 
1547 1547
     switch (field->type) {
1548
+    case AV_OPT_TYPE_BOOL:
1548 1549
     case AV_OPT_TYPE_INT:
1549 1550
     case AV_OPT_TYPE_INT64:
1550 1551
     case AV_OPT_TYPE_PIXEL_FMT:
... ...
@@ -1628,6 +1681,7 @@ int av_opt_is_set_to_default(void *obj, const AVOption *o)
1628 1628
     switch (o->type) {
1629 1629
     case AV_OPT_TYPE_CONST:
1630 1630
         return 1;
1631
+    case AV_OPT_TYPE_BOOL:
1631 1632
     case AV_OPT_TYPE_FLAGS:
1632 1633
     case AV_OPT_TYPE_PIXEL_FMT:
1633 1634
     case AV_OPT_TYPE_SAMPLE_FMT:
... ...
@@ -1791,6 +1845,9 @@ typedef struct TestContext
1791 1791
     float flt;
1792 1792
     double dbl;
1793 1793
     char *escape;
1794
+    int bool1;
1795
+    int bool2;
1796
+    int bool3;
1794 1797
 } TestContext;
1795 1798
 
1796 1799
 #define OFFSET(x) offsetof(TestContext, x)
... ...
@@ -1822,6 +1879,9 @@ static const AVOption test_options[]= {
1822 1822
 {"num64",    "set num 64bit",  OFFSET(num64),    AV_OPT_TYPE_INT64,    {.i64 = 1},        0,        100, 1 },
1823 1823
 {"flt",      "set float",      OFFSET(flt),      AV_OPT_TYPE_FLOAT,    {.dbl = 1.0/3},    0,        100, 1},
1824 1824
 {"dbl",      "set double",     OFFSET(dbl),      AV_OPT_TYPE_DOUBLE,   {.dbl = 1.0/3},    0,        100, 1 },
1825
+{"bool1", "set boolean value",  OFFSET(bool1),   AV_OPT_TYPE_BOOL,     {.i64 = -1},      -1,        1, 1 },
1826
+{"bool2", "set boolean value",  OFFSET(bool2),   AV_OPT_TYPE_BOOL,     {.i64 = 1},       -1,        1, 1 },
1827
+{"bool3", "set boolean value",  OFFSET(bool3),   AV_OPT_TYPE_BOOL,     {.i64 = 0},        0,        1, 1 },
1825 1828
 {NULL},
1826 1829
 };
1827 1830
 
... ...
@@ -1987,6 +2047,8 @@ int main(void)
1987 1987
             "dbl=2.2",
1988 1988
             "dbl=-1",
1989 1989
             "dbl=101",
1990
+            "bool1=true",
1991
+            "bool2=auto",
1990 1992
         };
1991 1993
 
1992 1994
         test_ctx.class = &test_class;
... ...
@@ -236,6 +236,7 @@ enum AVOptionType{
236 236
     AV_OPT_TYPE_DURATION   = MKBETAG('D','U','R',' '),
237 237
     AV_OPT_TYPE_COLOR      = MKBETAG('C','O','L','R'),
238 238
     AV_OPT_TYPE_CHANNEL_LAYOUT = MKBETAG('C','H','L','A'),
239
+    AV_OPT_TYPE_BOOL           = MKBETAG('B','O','O','L'),
239 240
 };
240 241
 
241 242
 /**
... ...
@@ -56,7 +56,7 @@
56 56
  */
57 57
 
58 58
 #define LIBAVUTIL_VERSION_MAJOR  55
59
-#define LIBAVUTIL_VERSION_MINOR   0
59
+#define LIBAVUTIL_VERSION_MINOR   1
60 60
 #define LIBAVUTIL_VERSION_MICRO 100
61 61
 
62 62
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
... ...
@@ -40,6 +40,9 @@ TestContext AVOptions:
40 40
   -num64             <int64>      E....... set num 64bit (from 0 to 100) (default 1)
41 41
   -flt               <float>      E....... set float (from 0 to 100) (default 0.333333)
42 42
   -dbl               <double>     E....... set double (from 0 to 100) (default 0.333333)
43
+  -bool1             <boolean>    E....... set boolean value (default auto)
44
+  -bool2             <boolean>    E....... set boolean value (default true)
45
+  -bool3             <boolean>    E....... set boolean value (default false)
43 46
 
44 47
 Testing av_opt_is_set_to_default()
45 48
 name:       num default:1 error:
... ...
@@ -64,6 +67,9 @@ name:      bin2 default:1 error:
64 64
 name:     num64 default:0 error:
65 65
 name:       flt default:0 error:
66 66
 name:       dbl default:0 error:
67
+name:     bool1 default:0 error:
68
+name:     bool2 default:0 error:
69
+name:     bool3 default:1 error:
67 70
 name:       num default:1 error:
68 71
 name:    toggle default:1 error:
69 72
 name:  rational default:1 error:
... ...
@@ -86,9 +92,12 @@ name:      bin2 default:1 error:
86 86
 name:     num64 default:1 error:
87 87
 name:       flt default:1 error:
88 88
 name:       dbl default:1 error:
89
+name:     bool1 default:1 error:
90
+name:     bool2 default:1 error:
91
+name:     bool3 default:1 error:
89 92
 
90 93
 Test av_opt_serialize()
91
-num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,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
94
+num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,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,bool1=auto,bool2=true,bool3=false
92 95
 Setting entry with key 'num' to value '0'
93 96
 Setting entry with key 'toggle' to value '1'
94 97
 Setting entry with key 'rational' to value '1/1'
... ...
@@ -108,7 +117,10 @@ Setting entry with key 'bin2' to value ''
108 108
 Setting entry with key 'num64' to value '1'
109 109
 Setting entry with key 'flt' to value '0.333333'
110 110
 Setting entry with key 'dbl' to value '0.333333'
111
-num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,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
111
+Setting entry with key 'bool1' to value 'auto'
112
+Setting entry with key 'bool2' to value 'true'
113
+Setting entry with key 'bool3' to value 'false'
114
+num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,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,bool1=auto,bool2=true,bool3=false
112 115
 
113 116
 Testing av_set_options_string()
114 117
 Setting options string ''
... ...
@@ -323,6 +335,12 @@ Setting options string 'dbl=101'
323 323
 Setting entry with key 'dbl' to value '101'
324 324
 Value 101.000000 for parameter 'dbl' out of range [0 - 100]
325 325
 Error 'dbl=101'
326
+Setting options string 'bool1=true'
327
+Setting entry with key 'bool1' to value 'true'
328
+OK    'bool1=true'
329
+Setting options string 'bool2=auto'
330
+Setting entry with key 'bool2' to value 'auto'
331
+OK    'bool2=auto'
326 332
 
327 333
 Testing av_opt_set_from_string()
328 334
 Setting options string ''