876ea4c5112da248131b49cbc56e813ada54b6c8
[libav.git] / libavutil / opt.c
1 /*
2 * AVOptions
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * AVOptions
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "avutil.h"
29 #include "avstring.h"
30 #include "opt.h"
31 #include "eval.h"
32 #include "dict.h"
33 #include "log.h"
34
35 #if FF_API_FIND_OPT
36 //FIXME order them and do a bin search
37 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
38 {
39 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
40 const AVOption *o= c->option;
41
42 for (; o && o->name; o++) {
43 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
44 return o;
45 }
46 return NULL;
47 }
48 #endif
49
50 const AVOption *av_next_option(void *obj, const AVOption *last)
51 {
52 if (last && last[1].name) return ++last;
53 else if (last) return NULL;
54 else return (*(AVClass**)obj)->option;
55 }
56
57 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
58 {
59 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
60 void *dst;
61 if (o_out)
62 *o_out= o;
63 if (!o)
64 return AVERROR_OPTION_NOT_FOUND;
65
66 if (o->max*den < num*intnum || o->min*den > num*intnum) {
67 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
68 return AVERROR(ERANGE);
69 }
70
71 dst= ((uint8_t*)obj) + o->offset;
72
73 switch (o->type) {
74 case FF_OPT_TYPE_FLAGS:
75 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
76 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
77 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
78 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
79 case FF_OPT_TYPE_RATIONAL:
80 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
81 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
82 break;
83 default:
84 return AVERROR(EINVAL);
85 }
86 return 0;
87 }
88
89 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
90 {
91 const AVOption *o = NULL;
92 if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
93 return NULL;
94 else
95 return o;
96 }
97
98 static const double const_values[] = {
99 M_PI,
100 M_E,
101 FF_QP2LAMBDA,
102 0
103 };
104
105 static const char * const const_names[] = {
106 "PI",
107 "E",
108 "QP2LAMBDA",
109 0
110 };
111
112 static int hexchar2int(char c) {
113 if (c >= '0' && c <= '9') return c - '0';
114 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
115 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
116 return -1;
117 }
118
119 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
120 {
121 int *lendst = (int *)(dst + 1);
122 uint8_t *bin, *ptr;
123 int len = strlen(val);
124
125 av_freep(dst);
126 *lendst = 0;
127
128 if (len & 1)
129 return AVERROR(EINVAL);
130 len /= 2;
131
132 ptr = bin = av_malloc(len);
133 while (*val) {
134 int a = hexchar2int(*val++);
135 int b = hexchar2int(*val++);
136 if (a < 0 || b < 0) {
137 av_free(bin);
138 return AVERROR(EINVAL);
139 }
140 *ptr++ = (a << 4) | b;
141 }
142 *dst = bin;
143 *lendst = len;
144
145 return 0;
146 }
147
148 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
149 {
150 av_freep(dst);
151 *dst = av_strdup(val);
152 return 0;
153 }
154
155 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
156 {
157 int ret = 0, notfirst = 0;
158 for (;;) {
159 int i;
160 char buf[256];
161 int cmd = 0;
162 double d;
163
164 if (*val == '+' || *val == '-')
165 cmd = *(val++);
166
167 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
168 buf[i] = val[i];
169 buf[i] = 0;
170
171 {
172 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
173 if (o_named && o_named->type == FF_OPT_TYPE_CONST)
174 d = o_named->default_val.dbl;
175 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
176 else if (!strcmp(buf, "max" )) d = o->max;
177 else if (!strcmp(buf, "min" )) d = o->min;
178 else if (!strcmp(buf, "none" )) d = 0;
179 else if (!strcmp(buf, "all" )) d = ~0;
180 else {
181 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
182 if (res < 0) {
183 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
184 return res;
185 }
186 }
187 }
188 if (o->type == FF_OPT_TYPE_FLAGS) {
189 if (cmd == '+') d = av_get_int(obj, o->name, NULL) | (int64_t)d;
190 else if (cmd == '-') d = av_get_int(obj, o->name, NULL) &~(int64_t)d;
191 } else {
192 if (cmd == '+') d = notfirst*av_get_double(obj, o->name, NULL) + d;
193 else if (cmd == '-') d = notfirst*av_get_double(obj, o->name, NULL) - d;
194 }
195
196 if ((ret = av_set_number2(obj, o->name, d, 1, 1, NULL)) < 0)
197 return ret;
198 val += i;
199 if (!*val)
200 return 0;
201 notfirst = 1;
202 }
203
204 return 0;
205 }
206
207 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
208 {
209 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
210 void *dst;
211 if (o_out)
212 *o_out = o;
213 if (!o)
214 return AVERROR_OPTION_NOT_FOUND;
215 if (!val)
216 return AVERROR(EINVAL);
217
218 dst = ((uint8_t*)obj) + o->offset;
219 switch (o->type) {
220 case FF_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
221 case FF_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
222 case FF_OPT_TYPE_FLAGS:
223 case FF_OPT_TYPE_INT:
224 case FF_OPT_TYPE_INT64:
225 case FF_OPT_TYPE_FLOAT:
226 case FF_OPT_TYPE_DOUBLE:
227 case FF_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
228 }
229
230 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
231 return AVERROR(EINVAL);
232 }
233
234 const AVOption *av_set_double(void *obj, const char *name, double n)
235 {
236 return av_set_number(obj, name, n, 1, 1);
237 }
238
239 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
240 {
241 return av_set_number(obj, name, n.num, n.den, 1);
242 }
243
244 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
245 {
246 return av_set_number(obj, name, 1, 1, n);
247 }
248
249 /**
250 *
251 * @param buf a buffer which is used for returning non string values as strings, can be NULL
252 * @param buf_len allocated length in bytes of buf
253 */
254 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
255 {
256 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
257 void *dst;
258 uint8_t *bin;
259 int len, i;
260 if (!o)
261 return NULL;
262 if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
263 return NULL;
264
265 dst= ((uint8_t*)obj) + o->offset;
266 if (o_out) *o_out= o;
267
268 switch (o->type) {
269 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
270 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
271 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
272 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
273 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
274 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
275 case FF_OPT_TYPE_STRING: return *(void**)dst;
276 case FF_OPT_TYPE_BINARY:
277 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
278 if (len >= (buf_len + 1)/2) return NULL;
279 bin = *(uint8_t**)dst;
280 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
281 break;
282 default: return NULL;
283 }
284 return buf;
285 }
286
287 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
288 {
289 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
290 void *dst;
291 if (!o)
292 goto error;
293
294 dst= ((uint8_t*)obj) + o->offset;
295
296 if (o_out) *o_out= o;
297
298 switch (o->type) {
299 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
300 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
301 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
302 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
303 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
304 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
305 *den = ((AVRational*)dst)->den;
306 return 0;
307 }
308 error:
309 *den=*intnum=0;
310 return -1;
311 }
312
313 double av_get_double(void *obj, const char *name, const AVOption **o_out)
314 {
315 int64_t intnum=1;
316 double num=1;
317 int den=1;
318
319 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
320 return NAN;
321 return num*intnum/den;
322 }
323
324 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
325 {
326 int64_t intnum=1;
327 double num=1;
328 int den=1;
329
330 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
331 return (AVRational){0, 0};
332 if (num == 1.0 && (int)intnum == intnum)
333 return (AVRational){intnum, den};
334 else
335 return av_d2q(num*intnum/den, 1<<24);
336 }
337
338 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
339 {
340 int64_t intnum=1;
341 double num=1;
342 int den=1;
343
344 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
345 return -1;
346 return num*intnum/den;
347 }
348
349 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
350 {
351 const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
352 const AVOption *flag = av_find_opt(obj, flag_name, NULL, 0, 0);
353
354 if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
355 return 0;
356 return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
357 }
358
359 static void opt_list(void *obj, void *av_log_obj, const char *unit,
360 int req_flags, int rej_flags)
361 {
362 const AVOption *opt=NULL;
363
364 while ((opt= av_next_option(obj, opt))) {
365 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
366 continue;
367
368 /* Don't print CONST's on level one.
369 * Don't print anything but CONST's on level two.
370 * Only print items from the requested unit.
371 */
372 if (!unit && opt->type==FF_OPT_TYPE_CONST)
373 continue;
374 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
375 continue;
376 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
377 continue;
378 else if (unit && opt->type == FF_OPT_TYPE_CONST)
379 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
380 else
381 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
382
383 switch (opt->type) {
384 case FF_OPT_TYPE_FLAGS:
385 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
386 break;
387 case FF_OPT_TYPE_INT:
388 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
389 break;
390 case FF_OPT_TYPE_INT64:
391 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
392 break;
393 case FF_OPT_TYPE_DOUBLE:
394 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
395 break;
396 case FF_OPT_TYPE_FLOAT:
397 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
398 break;
399 case FF_OPT_TYPE_STRING:
400 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
401 break;
402 case FF_OPT_TYPE_RATIONAL:
403 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
404 break;
405 case FF_OPT_TYPE_BINARY:
406 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
407 break;
408 case FF_OPT_TYPE_CONST:
409 default:
410 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
411 break;
412 }
413 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
414 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
415 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
416 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
417 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
418
419 if (opt->help)
420 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
421 av_log(av_log_obj, AV_LOG_INFO, "\n");
422 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
423 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
424 }
425 }
426 }
427
428 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
429 {
430 if (!obj)
431 return -1;
432
433 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
434
435 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
436
437 return 0;
438 }
439
440 void av_opt_set_defaults(void *s)
441 {
442 #if FF_API_OLD_AVOPTIONS
443 av_opt_set_defaults2(s, 0, 0);
444 }
445
446 void av_opt_set_defaults2(void *s, int mask, int flags)
447 {
448 #endif
449 const AVOption *opt = NULL;
450 while ((opt = av_next_option(s, opt)) != NULL) {
451 #if FF_API_OLD_AVOPTIONS
452 if ((opt->flags & mask) != flags)
453 continue;
454 #endif
455 switch (opt->type) {
456 case FF_OPT_TYPE_CONST:
457 /* Nothing to be done here */
458 break;
459 case FF_OPT_TYPE_FLAGS:
460 case FF_OPT_TYPE_INT: {
461 int val;
462 val = opt->default_val.dbl;
463 av_set_int(s, opt->name, val);
464 }
465 break;
466 case FF_OPT_TYPE_INT64:
467 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
468 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
469 av_set_int(s, opt->name, opt->default_val.dbl);
470 break;
471 case FF_OPT_TYPE_DOUBLE:
472 case FF_OPT_TYPE_FLOAT: {
473 double val;
474 val = opt->default_val.dbl;
475 av_set_double(s, opt->name, val);
476 }
477 break;
478 case FF_OPT_TYPE_RATIONAL: {
479 AVRational val;
480 val = av_d2q(opt->default_val.dbl, INT_MAX);
481 av_set_q(s, opt->name, val);
482 }
483 break;
484 case FF_OPT_TYPE_STRING:
485 av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
486 break;
487 case FF_OPT_TYPE_BINARY:
488 /* Cannot set default for binary */
489 break;
490 default:
491 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
492 }
493 }
494 }
495
496 /**
497 * Store the value in the field in ctx that is named like key.
498 * ctx must be an AVClass context, storing is done using AVOptions.
499 *
500 * @param buf the string to parse, buf will be updated to point at the
501 * separator just after the parsed key/value pair
502 * @param key_val_sep a 0-terminated list of characters used to
503 * separate key from value
504 * @param pairs_sep a 0-terminated list of characters used to separate
505 * two pairs from each other
506 * @return 0 if the key/value pair has been successfully parsed and
507 * set, or a negative value corresponding to an AVERROR code in case
508 * of error:
509 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
510 * the error code issued by av_set_string3() if the key/value pair
511 * cannot be set
512 */
513 static int parse_key_value_pair(void *ctx, const char **buf,
514 const char *key_val_sep, const char *pairs_sep)
515 {
516 char *key = av_get_token(buf, key_val_sep);
517 char *val;
518 int ret;
519
520 if (*key && strspn(*buf, key_val_sep)) {
521 (*buf)++;
522 val = av_get_token(buf, pairs_sep);
523 } else {
524 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
525 av_free(key);
526 return AVERROR(EINVAL);
527 }
528
529 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
530
531 ret = av_set_string3(ctx, key, val, 1, NULL);
532 if (ret == AVERROR_OPTION_NOT_FOUND)
533 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
534
535 av_free(key);
536 av_free(val);
537 return ret;
538 }
539
540 int av_set_options_string(void *ctx, const char *opts,
541 const char *key_val_sep, const char *pairs_sep)
542 {
543 int ret, count = 0;
544
545 while (*opts) {
546 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
547 return ret;
548 count++;
549
550 if (*opts)
551 opts++;
552 }
553
554 return count;
555 }
556
557 void av_opt_free(void *obj)
558 {
559 const AVOption *o = NULL;
560 while ((o = av_next_option(obj, o)))
561 if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
562 av_freep((uint8_t *)obj + o->offset);
563 }
564
565 int av_opt_set_dict(void *obj, AVDictionary **options)
566 {
567 AVDictionaryEntry *t = NULL;
568 AVDictionary *tmp = NULL;
569 int ret = 0;
570
571 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
572 ret = av_set_string3(obj, t->key, t->value, 1, NULL);
573 if (ret == AVERROR_OPTION_NOT_FOUND)
574 av_dict_set(&tmp, t->key, t->value, 0);
575 else if (ret < 0) {
576 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
577 break;
578 }
579 ret = 0;
580 }
581 av_dict_free(options);
582 *options = tmp;
583 return ret;
584 }
585
586 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
587 int opt_flags, int search_flags)
588 {
589 AVClass *c = *(AVClass**)obj;
590 const AVOption *o = NULL;
591
592 if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
593 (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
594 return o;
595
596 while (o = av_next_option(obj, o)) {
597 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
598 ((!unit && o->type != FF_OPT_TYPE_CONST) ||
599 (unit && o->unit && !strcmp(o->unit, unit))))
600 return o;
601 }
602 return NULL;
603 }
604
605 #ifdef TEST
606
607 #undef printf
608
609 typedef struct TestContext
610 {
611 const AVClass *class;
612 int num;
613 int toggle;
614 char *string;
615 int flags;
616 AVRational rational;
617 } TestContext;
618
619 #define OFFSET(x) offsetof(TestContext, x)
620
621 #define TEST_FLAG_COOL 01
622 #define TEST_FLAG_LAME 02
623 #define TEST_FLAG_MU 04
624
625 static const AVOption test_options[]= {
626 {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
627 {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
628 {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
629 {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
630 {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
631 {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
632 {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
633 {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
634 {NULL},
635 };
636
637 static const char *test_get_name(void *ctx)
638 {
639 return "test";
640 }
641
642 static const AVClass test_class = {
643 "TestContext",
644 test_get_name,
645 test_options
646 };
647
648 int main(void)
649 {
650 int i;
651
652 printf("\nTesting av_set_options_string()\n");
653 {
654 TestContext test_ctx;
655 const char *options[] = {
656 "",
657 ":",
658 "=",
659 "foo=:",
660 ":=foo",
661 "=foo",
662 "foo=",
663 "foo",
664 "foo=val",
665 "foo==val",
666 "toggle=:",
667 "string=:",
668 "toggle=1 : foo",
669 "toggle=100",
670 "toggle==1",
671 "flags=+mu-lame : num=42: toggle=0",
672 "num=42 : string=blahblah",
673 "rational=0 : rational=1/2 : rational=1/-1",
674 "rational=-1/0",
675 };
676
677 test_ctx.class = &test_class;
678 av_opt_set_defaults(&test_ctx);
679 test_ctx.string = av_strdup("default");
680
681 av_log_set_level(AV_LOG_DEBUG);
682
683 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
684 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
685 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
686 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
687 printf("\n");
688 }
689 }
690
691 return 0;
692 }
693
694 #endif