testprogs: K&R formatting cosmetics
[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 "avstring.h"
29 #include "avutil.h"
30 #include "common.h"
31 #include "dict.h"
32 #include "eval.h"
33 #include "log.h"
34 #include "mathematics.h"
35 #include "opt.h"
36
37 const AVOption *av_opt_next(const void *obj, const AVOption *last)
38 {
39 AVClass *class = *(AVClass **)obj;
40 if (!last && class->option && class->option[0].name)
41 return class->option;
42 if (last && last[1].name)
43 return ++last;
44 return NULL;
45 }
46
47 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
48 {
49 switch (o->type) {
50 case AV_OPT_TYPE_FLAGS:
51 *intnum = *(unsigned int *)dst;
52 return 0;
53 case AV_OPT_TYPE_INT:
54 *intnum = *(int *)dst;
55 return 0;
56 case AV_OPT_TYPE_INT64:
57 *intnum = *(int64_t *)dst;
58 return 0;
59 case AV_OPT_TYPE_FLOAT:
60 *num = *(float *)dst;
61 return 0;
62 case AV_OPT_TYPE_DOUBLE:
63 *num = *(double *)dst;
64 return 0;
65 case AV_OPT_TYPE_RATIONAL:
66 *intnum = ((AVRational *)dst)->num;
67 *den = ((AVRational *)dst)->den;
68 return 0;
69 }
70 return AVERROR(EINVAL);
71 }
72
73 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
74 {
75 if (o->type != AV_OPT_TYPE_FLAGS &&
76 (o->max * den < num * intnum || o->min * den > num * intnum)) {
77 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
78 num * intnum / den, o->name);
79 return AVERROR(ERANGE);
80 }
81
82 switch (o->type) {
83 case AV_OPT_TYPE_FLAGS:
84 case AV_OPT_TYPE_INT:
85 *(int *)dst = llrint(num / den) * intnum;
86 break;
87 case AV_OPT_TYPE_INT64:
88 *(int64_t *)dst = llrint(num / den) * intnum;
89 break;
90 case AV_OPT_TYPE_FLOAT:
91 *(float *)dst = num * intnum / den;
92 break;
93 case AV_OPT_TYPE_DOUBLE:
94 *(double *)dst = num * intnum / den;
95 break;
96 case AV_OPT_TYPE_RATIONAL:
97 if ((int) num == num)
98 *(AVRational *)dst = (AVRational) { num *intnum, den };
99 else
100 *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
101 break;
102 default:
103 return AVERROR(EINVAL);
104 }
105 return 0;
106 }
107
108 static const double const_values[] = {
109 M_PI,
110 M_E,
111 FF_QP2LAMBDA,
112 0
113 };
114
115 static const char *const const_names[] = {
116 "PI",
117 "E",
118 "QP2LAMBDA",
119 0
120 };
121
122 static int hexchar2int(char c)
123 {
124 if (c >= '0' && c <= '9')
125 return c - '0';
126 if (c >= 'a' && c <= 'f')
127 return c - 'a' + 10;
128 if (c >= 'A' && c <= 'F')
129 return c - 'A' + 10;
130 return -1;
131 }
132
133 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
134 {
135 int *lendst = (int *)(dst + 1);
136 uint8_t *bin, *ptr;
137 int len = strlen(val);
138
139 av_freep(dst);
140 *lendst = 0;
141
142 if (len & 1)
143 return AVERROR(EINVAL);
144 len /= 2;
145
146 ptr = bin = av_malloc(len);
147 if (!ptr)
148 return AVERROR(ENOMEM);
149 while (*val) {
150 int a = hexchar2int(*val++);
151 int b = hexchar2int(*val++);
152 if (a < 0 || b < 0) {
153 av_free(bin);
154 return AVERROR(EINVAL);
155 }
156 *ptr++ = (a << 4) | b;
157 }
158 *dst = bin;
159 *lendst = len;
160
161 return 0;
162 }
163
164 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
165 {
166 av_freep(dst);
167 *dst = av_strdup(val);
168 return *dst ? 0 : AVERROR(ENOMEM);
169 }
170
171 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
172 opt->type == AV_OPT_TYPE_CONST || \
173 opt->type == AV_OPT_TYPE_FLAGS || \
174 opt->type == AV_OPT_TYPE_INT) \
175 ? opt->default_val.i64 \
176 : opt->default_val.dbl)
177
178 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
179 {
180 int ret = 0, notfirst = 0;
181 for (;;) {
182 int i, den = 1;
183 char buf[256];
184 int cmd = 0;
185 double d, num = 1;
186 int64_t intnum = 1;
187
188 i = 0;
189 if (*val == '+' || *val == '-') {
190 if (o->type == AV_OPT_TYPE_FLAGS)
191 cmd = *(val++);
192 else if (!notfirst)
193 buf[i++] = *val;
194 }
195
196 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
197 buf[i] = val[i];
198 buf[i] = 0;
199
200 {
201 const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
202 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
203 d = DEFAULT_NUMVAL(o_named);
204 else if (!strcmp(buf, "default"))
205 d = DEFAULT_NUMVAL(o);
206 else if (!strcmp(buf, "max"))
207 d = o->max;
208 else if (!strcmp(buf, "min"))
209 d = o->min;
210 else if (!strcmp(buf, "none"))
211 d = 0;
212 else if (!strcmp(buf, "all"))
213 d = ~0;
214 else {
215 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
216 if (res < 0) {
217 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
218 return res;
219 }
220 }
221 }
222 if (o->type == AV_OPT_TYPE_FLAGS) {
223 read_number(o, dst, NULL, NULL, &intnum);
224 if (cmd == '+')
225 d = intnum | (int64_t)d;
226 else if (cmd == '-')
227 d = intnum & ~(int64_t)d;
228 } else {
229 read_number(o, dst, &num, &den, &intnum);
230 if (cmd == '+')
231 d = notfirst * num * intnum / den + d;
232 else if (cmd == '-')
233 d = notfirst * num * intnum / den - d;
234 }
235
236 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
237 return ret;
238 val += i;
239 if (!*val)
240 return 0;
241 notfirst = 1;
242 }
243
244 return 0;
245 }
246
247 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
248 {
249 void *dst, *target_obj;
250 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
251 if (!o || !target_obj)
252 return AVERROR_OPTION_NOT_FOUND;
253 if (!val || o->flags & AV_OPT_FLAG_READONLY)
254 return AVERROR(EINVAL);
255
256 dst = ((uint8_t *)target_obj) + o->offset;
257 switch (o->type) {
258 case AV_OPT_TYPE_STRING:
259 return set_string(obj, o, val, dst);
260 case AV_OPT_TYPE_BINARY:
261 return set_string_binary(obj, o, val, dst);
262 case AV_OPT_TYPE_FLAGS:
263 case AV_OPT_TYPE_INT:
264 case AV_OPT_TYPE_INT64:
265 case AV_OPT_TYPE_FLOAT:
266 case AV_OPT_TYPE_DOUBLE:
267 case AV_OPT_TYPE_RATIONAL:
268 return set_string_number(obj, target_obj, o, val, dst);
269 }
270
271 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
272 return AVERROR(EINVAL);
273 }
274
275 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
276 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
277 const char *val, vartype *name ## _out) \
278 { \
279 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
280 return AVERROR(EINVAL); \
281 return set_string_number(obj, obj, o, val, name ## _out); \
282 }
283
284 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
285 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
286 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
287 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
288 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
289 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
290
291 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
292 int search_flags)
293 {
294 void *dst, *target_obj;
295 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
296
297 if (!o || !target_obj)
298 return AVERROR_OPTION_NOT_FOUND;
299
300 if (o->flags & AV_OPT_FLAG_READONLY)
301 return AVERROR(EINVAL);
302
303 dst = ((uint8_t *)target_obj) + o->offset;
304 return write_number(obj, o, dst, num, den, intnum);
305 }
306
307 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
308 {
309 return set_number(obj, name, 1, 1, val, search_flags);
310 }
311
312 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
313 {
314 return set_number(obj, name, val, 1, 1, search_flags);
315 }
316
317 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
318 {
319 return set_number(obj, name, val.num, val.den, 1, search_flags);
320 }
321
322 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
323 {
324 void *target_obj;
325 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
326 uint8_t *ptr;
327 uint8_t **dst;
328 int *lendst;
329
330 if (!o || !target_obj)
331 return AVERROR_OPTION_NOT_FOUND;
332
333 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
334 return AVERROR(EINVAL);
335
336 ptr = av_malloc(len);
337 if (!ptr)
338 return AVERROR(ENOMEM);
339
340 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
341 lendst = (int *)(dst + 1);
342
343 av_free(*dst);
344 *dst = ptr;
345 *lendst = len;
346 memcpy(ptr, val, len);
347
348 return 0;
349 }
350
351 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
352 int search_flags)
353 {
354 void *target_obj;
355 AVDictionary **dst;
356 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
357
358 if (!o || !target_obj)
359 return AVERROR_OPTION_NOT_FOUND;
360 if (o->flags & AV_OPT_FLAG_READONLY)
361 return AVERROR(EINVAL);
362
363 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
364 av_dict_free(dst);
365 av_dict_copy(dst, val, 0);
366
367 return 0;
368 }
369
370 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
371 {
372 void *dst, *target_obj;
373 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
374 uint8_t *bin, buf[128];
375 int len, i, ret;
376
377 if (!o || !target_obj)
378 return AVERROR_OPTION_NOT_FOUND;
379
380 dst = (uint8_t *)target_obj + o->offset;
381
382 buf[0] = 0;
383 switch (o->type) {
384 case AV_OPT_TYPE_FLAGS:
385 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
386 break;
387 case AV_OPT_TYPE_INT:
388 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
389 break;
390 case AV_OPT_TYPE_INT64:
391 ret = snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t *)dst);
392 break;
393 case AV_OPT_TYPE_FLOAT:
394 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
395 break;
396 case AV_OPT_TYPE_DOUBLE:
397 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
398 break;
399 case AV_OPT_TYPE_RATIONAL:
400 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num,
401 ((AVRational *)dst)->den);
402 break;
403 case AV_OPT_TYPE_STRING:
404 if (*(uint8_t **)dst)
405 *out_val = av_strdup(*(uint8_t **)dst);
406 else
407 *out_val = av_strdup("");
408 return *out_val ? 0 : AVERROR(ENOMEM);
409 case AV_OPT_TYPE_BINARY:
410 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
411 if ((uint64_t)len * 2 + 1 > INT_MAX)
412 return AVERROR(EINVAL);
413 if (!(*out_val = av_malloc(len * 2 + 1)))
414 return AVERROR(ENOMEM);
415 bin = *(uint8_t **)dst;
416 for (i = 0; i < len; i++)
417 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
418 return 0;
419 default:
420 return AVERROR(EINVAL);
421 }
422
423 if (ret >= sizeof(buf))
424 return AVERROR(EINVAL);
425 *out_val = av_strdup(buf);
426 return *out_val ? 0 : AVERROR(ENOMEM);
427 }
428
429 static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
430 int search_flags)
431 {
432 void *dst, *target_obj;
433 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
434 if (!o || !target_obj)
435 goto error;
436
437 dst = ((uint8_t *)target_obj) + o->offset;
438
439 return read_number(o, dst, num, den, intnum);
440
441 error:
442 *den =
443 *intnum = 0;
444 return -1;
445 }
446
447 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
448 {
449 int64_t intnum = 1;
450 double num = 1;
451 int ret, den = 1;
452
453 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
454 return ret;
455 *out_val = num * intnum / den;
456 return 0;
457 }
458
459 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
460 {
461 int64_t intnum = 1;
462 double num = 1;
463 int ret, den = 1;
464
465 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
466 return ret;
467 *out_val = num * intnum / den;
468 return 0;
469 }
470
471 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
472 {
473 int64_t intnum = 1;
474 double num = 1;
475 int ret, den = 1;
476
477 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
478 return ret;
479
480 if (num == 1.0 && (int)intnum == intnum)
481 *out_val = (AVRational) { intnum, den };
482 else
483 *out_val = av_d2q(num * intnum / den, 1 << 24);
484 return 0;
485 }
486
487 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
488 {
489 void *target_obj;
490 AVDictionary *src;
491 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
492
493 if (!o || !target_obj)
494 return AVERROR_OPTION_NOT_FOUND;
495 if (o->type != AV_OPT_TYPE_DICT)
496 return AVERROR(EINVAL);
497
498 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
499 av_dict_copy(out_val, src, 0);
500
501 return 0;
502 }
503
504 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
505 {
506 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
507 const AVOption *flag = av_opt_find(obj, flag_name,
508 field ? field->unit : NULL, 0, 0);
509 int64_t res;
510
511 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
512 av_opt_get_int(obj, field_name, 0, &res) < 0)
513 return 0;
514 return res & flag->default_val.i64;
515 }
516
517 static void opt_list(void *obj, void *av_log_obj, const char *unit,
518 int req_flags, int rej_flags)
519 {
520 const AVOption *opt = NULL;
521
522 while ((opt = av_opt_next(obj, opt))) {
523 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
524 continue;
525
526 /* Don't print CONST's on level one.
527 * Don't print anything but CONST's on level two.
528 * Only print items from the requested unit.
529 */
530 if (!unit && opt->type == AV_OPT_TYPE_CONST)
531 continue;
532 else if (unit && opt->type != AV_OPT_TYPE_CONST)
533 continue;
534 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
535 continue;
536 else if (unit && opt->type == AV_OPT_TYPE_CONST)
537 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
538 else
539 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
540
541 switch (opt->type) {
542 case AV_OPT_TYPE_FLAGS:
543 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
544 break;
545 case AV_OPT_TYPE_INT:
546 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
547 break;
548 case AV_OPT_TYPE_INT64:
549 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
550 break;
551 case AV_OPT_TYPE_DOUBLE:
552 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
553 break;
554 case AV_OPT_TYPE_FLOAT:
555 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
556 break;
557 case AV_OPT_TYPE_STRING:
558 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
559 break;
560 case AV_OPT_TYPE_RATIONAL:
561 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
562 break;
563 case AV_OPT_TYPE_BINARY:
564 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
565 break;
566 case AV_OPT_TYPE_CONST:
567 default:
568 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
569 break;
570 }
571 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
572 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
573 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) ? 'V' : '.');
574 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM) ? 'A' : '.');
575 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
576 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
577 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
578
579 if (opt->help)
580 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
581 av_log(av_log_obj, AV_LOG_INFO, "\n");
582 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
583 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
584 }
585 }
586
587 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
588 {
589 if (!obj)
590 return -1;
591
592 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
593
594 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
595
596 return 0;
597 }
598
599 void av_opt_set_defaults(void *s)
600 {
601 const AVOption *opt = NULL;
602 while ((opt = av_opt_next(s, opt))) {
603 if (opt->flags & AV_OPT_FLAG_READONLY)
604 continue;
605
606 switch (opt->type) {
607 case AV_OPT_TYPE_CONST:
608 /* Nothing to be done here */
609 break;
610 case AV_OPT_TYPE_FLAGS:
611 case AV_OPT_TYPE_INT:
612 case AV_OPT_TYPE_INT64:
613 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
614 break;
615 case AV_OPT_TYPE_DOUBLE:
616 case AV_OPT_TYPE_FLOAT:
617 {
618 double val;
619 val = opt->default_val.dbl;
620 av_opt_set_double(s, opt->name, val, 0);
621 }
622 break;
623 case AV_OPT_TYPE_RATIONAL:
624 {
625 AVRational val;
626 val = av_d2q(opt->default_val.dbl, INT_MAX);
627 av_opt_set_q(s, opt->name, val, 0);
628 }
629 break;
630 case AV_OPT_TYPE_STRING:
631 av_opt_set(s, opt->name, opt->default_val.str, 0);
632 break;
633 case AV_OPT_TYPE_BINARY:
634 case AV_OPT_TYPE_DICT:
635 /* Cannot set defaults for these types */
636 break;
637 default:
638 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
639 opt->type, opt->name);
640 }
641 }
642 }
643
644 /**
645 * Store the value in the field in ctx that is named like key.
646 * ctx must be an AVClass context, storing is done using AVOptions.
647 *
648 * @param buf the string to parse, buf will be updated to point at the
649 * separator just after the parsed key/value pair
650 * @param key_val_sep a 0-terminated list of characters used to
651 * separate key from value
652 * @param pairs_sep a 0-terminated list of characters used to separate
653 * two pairs from each other
654 * @return 0 if the key/value pair has been successfully parsed and
655 * set, or a negative value corresponding to an AVERROR code in case
656 * of error:
657 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
658 * the error code issued by av_opt_set() if the key/value pair
659 * cannot be set
660 */
661 static int parse_key_value_pair(void *ctx, const char **buf,
662 const char *key_val_sep, const char *pairs_sep)
663 {
664 char *key = av_get_token(buf, key_val_sep);
665 char *val;
666 int ret;
667
668 if (!key)
669 return AVERROR(ENOMEM);
670
671 if (*key && strspn(*buf, key_val_sep)) {
672 (*buf)++;
673 val = av_get_token(buf, pairs_sep);
674 if (!val) {
675 av_freep(&key);
676 return AVERROR(ENOMEM);
677 }
678 } else {
679 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
680 av_free(key);
681 return AVERROR(EINVAL);
682 }
683
684 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
685
686 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
687 if (ret == AVERROR_OPTION_NOT_FOUND)
688 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
689
690 av_free(key);
691 av_free(val);
692 return ret;
693 }
694
695 int av_set_options_string(void *ctx, const char *opts,
696 const char *key_val_sep, const char *pairs_sep)
697 {
698 int ret, count = 0;
699
700 if (!opts)
701 return 0;
702
703 while (*opts) {
704 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
705 return ret;
706 count++;
707
708 if (*opts)
709 opts++;
710 }
711
712 return count;
713 }
714
715 void av_opt_free(void *obj)
716 {
717 const AVOption *o = NULL;
718 while ((o = av_opt_next(obj, o))) {
719 switch (o->type) {
720 case AV_OPT_TYPE_STRING:
721 case AV_OPT_TYPE_BINARY:
722 av_freep((uint8_t *)obj + o->offset);
723 break;
724
725 case AV_OPT_TYPE_DICT:
726 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
727 break;
728
729 default:
730 break;
731 }
732 }
733 }
734
735 int av_opt_set_dict(void *obj, AVDictionary **options)
736 {
737 AVDictionaryEntry *t = NULL;
738 AVDictionary *tmp = NULL;
739 int ret = 0;
740
741 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
742 ret = av_opt_set(obj, t->key, t->value, 0);
743 if (ret == AVERROR_OPTION_NOT_FOUND)
744 av_dict_set(&tmp, t->key, t->value, 0);
745 else if (ret < 0) {
746 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
747 break;
748 }
749 ret = 0;
750 }
751 av_dict_free(options);
752 *options = tmp;
753 return ret;
754 }
755
756 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
757 int opt_flags, int search_flags)
758 {
759 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
760 }
761
762 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
763 int opt_flags, int search_flags, void **target_obj)
764 {
765 const AVClass *c = *(AVClass **)obj;
766 const AVOption *o = NULL;
767
768 if (!c)
769 return NULL;
770
771 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
772 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
773 const AVClass *child = NULL;
774 while (child = av_opt_child_class_next(c, child))
775 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
776 return o;
777 } else {
778 void *child = NULL;
779 while (child = av_opt_child_next(obj, child))
780 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
781 return o;
782 }
783 }
784
785 while (o = av_opt_next(obj, o)) {
786 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
787 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
788 (unit && o->unit && !strcmp(o->unit, unit)))) {
789 if (target_obj) {
790 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
791 *target_obj = obj;
792 else
793 *target_obj = NULL;
794 }
795 return o;
796 }
797 }
798 return NULL;
799 }
800
801 void *av_opt_child_next(void *obj, void *prev)
802 {
803 const AVClass *c = *(AVClass **)obj;
804 if (c->child_next)
805 return c->child_next(obj, prev);
806 return NULL;
807 }
808
809 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
810 {
811 if (parent->child_class_next)
812 return parent->child_class_next(prev);
813 return NULL;
814 }
815
816 static int opt_size(enum AVOptionType type)
817 {
818 switch (type) {
819 case AV_OPT_TYPE_INT:
820 case AV_OPT_TYPE_FLAGS:
821 return sizeof(int);
822 case AV_OPT_TYPE_INT64:
823 return sizeof(int64_t);
824 case AV_OPT_TYPE_DOUBLE:
825 return sizeof(double);
826 case AV_OPT_TYPE_FLOAT:
827 return sizeof(float);
828 case AV_OPT_TYPE_STRING:
829 return sizeof(uint8_t *);
830 case AV_OPT_TYPE_RATIONAL:
831 return sizeof(AVRational);
832 case AV_OPT_TYPE_BINARY:
833 return sizeof(uint8_t *) + sizeof(int);
834 }
835 return AVERROR(EINVAL);
836 }
837
838 int av_opt_copy(void *dst, const void *src)
839 {
840 const AVOption *o = NULL;
841 const AVClass *c;
842 int ret = 0;
843
844 if (!src)
845 return AVERROR(EINVAL);
846
847 c = *(AVClass **)src;
848 if (!c || c != *(AVClass **)dst)
849 return AVERROR(EINVAL);
850
851 while ((o = av_opt_next(src, o))) {
852 void *field_dst = (uint8_t *)dst + o->offset;
853 void *field_src = (uint8_t *)src + o->offset;
854 uint8_t **field_dst8 = (uint8_t **)field_dst;
855 uint8_t **field_src8 = (uint8_t **)field_src;
856
857 if (o->type == AV_OPT_TYPE_STRING) {
858 set_string(dst, o, *field_src8, field_dst8);
859 if (*field_src8 && !*field_dst8)
860 ret = AVERROR(ENOMEM);
861 } else if (o->type == AV_OPT_TYPE_BINARY) {
862 int len = *(int *)(field_src8 + 1);
863 if (*field_dst8 != *field_src8)
864 av_freep(field_dst8);
865 if (len) {
866 *field_dst8 = av_malloc(len);
867 if (!*field_dst8) {
868 ret = AVERROR(ENOMEM);
869 len = 0;
870 }
871 memcpy(*field_dst8, *field_src8, len);
872 } else {
873 *field_dst8 = NULL;
874 }
875 *(int *)(field_dst8 + 1) = len;
876 } else if (o->type == AV_OPT_TYPE_CONST) {
877 // do nothing
878 } else {
879 int size = opt_size(o->type);
880 if (size < 0)
881 ret = size;
882 else
883 memcpy(field_dst, field_src, size);
884 }
885 }
886 return ret;
887 }
888
889 #ifdef TEST
890
891 typedef struct TestContext {
892 const AVClass *class;
893 int num;
894 int toggle;
895 char *string;
896 int flags;
897 AVRational rational;
898 } TestContext;
899
900 #define OFFSET(x) offsetof(TestContext, x)
901
902 #define TEST_FLAG_COOL 01
903 #define TEST_FLAG_LAME 02
904 #define TEST_FLAG_MU 04
905
906 static const AVOption test_options[] = {
907 { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 },
908 { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 },
909 { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 },
910 { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX },
911 { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"},
912 { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"},
913 { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"},
914 { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"},
915 { NULL },
916 };
917
918 static const char *test_get_name(void *ctx)
919 {
920 return "test";
921 }
922
923 static const AVClass test_class = {
924 "TestContext",
925 test_get_name,
926 test_options
927 };
928
929 int main(void)
930 {
931 int i;
932 TestContext test_ctx = { .class = &test_class };
933 const char *options[] = {
934 "",
935 ":",
936 "=",
937 "foo=:",
938 ":=foo",
939 "=foo",
940 "foo=",
941 "foo",
942 "foo=val",
943 "foo==val",
944 "toggle=:",
945 "string=:",
946 "toggle=1 : foo",
947 "toggle=100",
948 "toggle==1",
949 "flags=+mu-lame : num=42: toggle=0",
950 "num=42 : string=blahblah",
951 "rational=0 : rational=1/2 : rational=1/-1",
952 "rational=-1/0",
953 };
954
955 printf("\nTesting av_set_options_string()\n");
956
957 av_opt_set_defaults(&test_ctx);
958 test_ctx.string = av_strdup("default");
959 if (!test_ctx.string)
960 return AVERROR(ENOMEM);
961
962 av_log_set_level(AV_LOG_DEBUG);
963
964 for (i = 0; i < FF_ARRAY_ELEMS(options); i++) {
965 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
966 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
967 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
968 printf("\n");
969 }
970
971 return 0;
972 }
973
974 #endif