AVOptions: add flags for read/read-only options
[libav.git] / libavutil / opt.c
CommitLineData
6ed04040
MN
1/*
2 * AVOptions
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
6ed04040 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
6ed04040
MN
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
6ed04040
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
6ed04040
MN
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"
c85eef4e 29#include "avstring.h"
1d9c2dc8 30#include "common.h"
6ed04040 31#include "opt.h"
be0e596a 32#include "eval.h"
7e83e1c5 33#include "dict.h"
abc78a5a 34#include "log.h"
3893feec 35#include "mathematics.h"
6ed04040 36
8c5dcaad
AK
37const AVOption *av_opt_next(void *obj, const AVOption *last)
38{
e1e22851 39 AVClass *class = *(AVClass**)obj;
d02202e0
JR
40 if (!last && class->option && class->option[0].name)
41 return class->option;
42 if (last && last[1].name)
43 return ++last;
e1e22851 44 return NULL;
6ed04040
MN
45}
46
059a037f
AK
47static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
48{
49 switch (o->type) {
145f741e
AK
50 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
51 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
52 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
53 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
54 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
55 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
059a037f
AK
56 *den = ((AVRational*)dst)->den;
57 return 0;
58 }
59 return AVERROR(EINVAL);
60}
61
7bb1807c 62static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
8f1afd3b 63{
0b357a80
AK
64 if (o->type != AV_OPT_TYPE_FLAGS &&
65 (o->max * den < num * intnum || o->min * den > num * intnum)) {
bcb8d9eb 66 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
0426c693 67 num*intnum/den, o->name);
6ed04040
MN
68 return AVERROR(ERANGE);
69 }
70
8f1afd3b 71 switch (o->type) {
145f741e
AK
72 case AV_OPT_TYPE_FLAGS:
73 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
74 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
75 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
76 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
77 case AV_OPT_TYPE_RATIONAL:
8f1afd3b
SS
78 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
79 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
6ed04040
MN
80 break;
81 default:
82 return AVERROR(EINVAL);
83 }
84 return 0;
85}
86
8f1afd3b 87static const double const_values[] = {
6ed04040
MN
88 M_PI,
89 M_E,
90 FF_QP2LAMBDA,
91 0
92};
93
8f1afd3b 94static const char * const const_names[] = {
6ed04040
MN
95 "PI",
96 "E",
97 "QP2LAMBDA",
98 0
99};
100
101static int hexchar2int(char c) {
102 if (c >= '0' && c <= '9') return c - '0';
103 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
104 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
105 return -1;
106}
107
c8d787d2 108static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
8f1afd3b 109{
c8d787d2
AK
110 int *lendst = (int *)(dst + 1);
111 uint8_t *bin, *ptr;
112 int len = strlen(val);
113
114 av_freep(dst);
115 *lendst = 0;
116
117 if (len & 1)
6ed04040 118 return AVERROR(EINVAL);
c8d787d2 119 len /= 2;
6ed04040 120
c8d787d2
AK
121 ptr = bin = av_malloc(len);
122 while (*val) {
123 int a = hexchar2int(*val++);
124 int b = hexchar2int(*val++);
125 if (a < 0 || b < 0) {
126 av_free(bin);
127 return AVERROR(EINVAL);
6ed04040 128 }
c8d787d2 129 *ptr++ = (a << 4) | b;
6ed04040 130 }
c8d787d2
AK
131 *dst = bin;
132 *lendst = len;
133
134 return 0;
135}
136
137static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
138{
139 av_freep(dst);
140 *dst = av_strdup(val);
141 return 0;
142}
143
124134e4 144#define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
d58dd4b5 145 opt->type == AV_OPT_TYPE_CONST || \
e6153f17
MS
146 opt->type == AV_OPT_TYPE_FLAGS || \
147 opt->type == AV_OPT_TYPE_INT) ? \
4d7adec8
AK
148 opt->default_val.i64 : opt->default_val.dbl)
149
4d67ff8e 150static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
c8d787d2
AK
151{
152 int ret = 0, notfirst = 0;
153 for (;;) {
059a037f 154 int i, den = 1;
c8d787d2
AK
155 char buf[256];
156 int cmd = 0;
059a037f
AK
157 double d, num = 1;
158 int64_t intnum = 1;
c8d787d2 159
9d5c62ba
JR
160 i = 0;
161 if (*val == '+' || *val == '-') {
162 if (o->type == AV_OPT_TYPE_FLAGS)
163 cmd = *(val++);
164 else if (!notfirst)
165 buf[i++] = *val;
166 }
c8d787d2 167
9d5c62ba 168 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
c8d787d2
AK
169 buf[i] = val[i];
170 buf[i] = 0;
171
172 {
4d67ff8e 173 const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
145f741e 174 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
4d7adec8
AK
175 d = DEFAULT_NUMVAL(o_named);
176 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
c8d787d2
AK
177 else if (!strcmp(buf, "max" )) d = o->max;
178 else if (!strcmp(buf, "min" )) d = o->min;
179 else if (!strcmp(buf, "none" )) d = 0;
180 else if (!strcmp(buf, "all" )) d = ~0;
181 else {
182 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
183 if (res < 0) {
184 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
185 return res;
6ed04040
MN
186 }
187 }
6ed04040 188 }
145f741e 189 if (o->type == AV_OPT_TYPE_FLAGS) {
059a037f
AK
190 read_number(o, dst, NULL, NULL, &intnum);
191 if (cmd == '+') d = intnum | (int64_t)d;
192 else if (cmd == '-') d = intnum &~(int64_t)d;
c8d787d2 193 } else {
059a037f
AK
194 read_number(o, dst, &num, &den, &intnum);
195 if (cmd == '+') d = notfirst*num*intnum/den + d;
196 else if (cmd == '-') d = notfirst*num*intnum/den - d;
c8d787d2 197 }
6ed04040 198
7bb1807c 199 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
c8d787d2
AK
200 return ret;
201 val += i;
202 if (!*val)
203 return 0;
204 notfirst = 1;
6ed04040
MN
205 }
206
6ed04040
MN
207 return 0;
208}
209
dac66da6
AK
210int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
211{
212 void *dst, *target_obj;
213 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
214 if (!o || !target_obj)
c8d787d2 215 return AVERROR_OPTION_NOT_FOUND;
c3ecd968 216 if (!val || o->flags & AV_OPT_FLAG_READONLY)
c8d787d2
AK
217 return AVERROR(EINVAL);
218
dac66da6 219 dst = ((uint8_t*)target_obj) + o->offset;
c8d787d2 220 switch (o->type) {
145f741e
AK
221 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
222 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
223 case AV_OPT_TYPE_FLAGS:
224 case AV_OPT_TYPE_INT:
225 case AV_OPT_TYPE_INT64:
226 case AV_OPT_TYPE_FLOAT:
227 case AV_OPT_TYPE_DOUBLE:
4d67ff8e 228 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
c8d787d2
AK
229 }
230
231 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
232 return AVERROR(EINVAL);
233}
234
dca055be
AK
235#define OPT_EVAL_NUMBER(name, opttype, vartype)\
236 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
237 {\
c3ecd968 238 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
dca055be 239 return AVERROR(EINVAL);\
4d67ff8e 240 return set_string_number(obj, obj, o, val, name ## _out);\
dca055be
AK
241 }
242
145f741e
AK
243OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
244OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
245OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
246OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
247OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
248OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
dca055be 249
dac66da6
AK
250static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
251 int search_flags)
b003d0be 252{
dac66da6
AK
253 void *dst, *target_obj;
254 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
7bb1807c 255
dac66da6
AK
256 if (!o || !target_obj)
257 return AVERROR_OPTION_NOT_FOUND;
7bb1807c 258
c3ecd968
AK
259 if (o->flags & AV_OPT_FLAG_READONLY)
260 return AVERROR(EINVAL);
261
dac66da6
AK
262 dst = ((uint8_t*)target_obj) + o->offset;
263 return write_number(obj, o, dst, num, den, intnum);
b003d0be
AK
264}
265
dac66da6
AK
266int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
267{
268 return set_number(obj, name, 1, 1, val, search_flags);
269}
270
271int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
272{
273 return set_number(obj, name, val, 1, 1, search_flags);
274}
275
276int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
277{
278 return set_number(obj, name, val.num, val.den, 1, search_flags);
6ed04040
MN
279}
280
154486f9
SP
281int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
282{
283 void *target_obj;
284 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
285 uint8_t *ptr;
286 uint8_t **dst;
287 int *lendst;
288
289 if (!o || !target_obj)
290 return AVERROR_OPTION_NOT_FOUND;
291
c3ecd968 292 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
154486f9
SP
293 return AVERROR(EINVAL);
294
295 ptr = av_malloc(len);
296 if (!ptr)
297 return AVERROR(ENOMEM);
298
299 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
300 lendst = (int *)(dst + 1);
301
302 av_free(*dst);
303 *dst = ptr;
304 *lendst = len;
305 memcpy(ptr, val, len);
306
307 return 0;
308}
309
41d9d51a 310int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
8f1afd3b 311{
41d9d51a
AK
312 void *dst, *target_obj;
313 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
314 uint8_t *bin, buf[128];
315 int len, i, ret;
316
317 if (!o || !target_obj)
318 return AVERROR_OPTION_NOT_FOUND;
319
320 dst = (uint8_t*)target_obj + o->offset;
321
322 buf[0] = 0;
323 switch (o->type) {
145f741e
AK
324 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
325 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
326 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
327 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
328 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
329 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
330 case AV_OPT_TYPE_STRING:
41d9d51a
AK
331 if (*(uint8_t**)dst)
332 *out_val = av_strdup(*(uint8_t**)dst);
333 else
334 *out_val = av_strdup("");
335 return 0;
145f741e 336 case AV_OPT_TYPE_BINARY:
41d9d51a
AK
337 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
338 if ((uint64_t)len*2 + 1 > INT_MAX)
339 return AVERROR(EINVAL);
340 if (!(*out_val = av_malloc(len*2 + 1)))
341 return AVERROR(ENOMEM);
342 bin = *(uint8_t**)dst;
343 for (i = 0; i < len; i++)
344 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
345 return 0;
346 default:
347 return AVERROR(EINVAL);
348 }
349
350 if (ret >= sizeof(buf))
351 return AVERROR(EINVAL);
352 *out_val = av_strdup(buf);
353 return 0;
354}
355
9676b9a2 356static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
41d9d51a
AK
357 int search_flags)
358{
359 void *dst, *target_obj;
360 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
361 if (!o || !target_obj)
6ed04040
MN
362 goto error;
363
41d9d51a 364 dst = ((uint8_t*)target_obj) + o->offset;
6ed04040 365
059a037f
AK
366 return read_number(o, dst, num, den, intnum);
367
6ed04040
MN
368error:
369 *den=*intnum=0;
370 return -1;
371}
372
41d9d51a
AK
373int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
374{
375 int64_t intnum = 1;
376 double num = 1;
377 int ret, den = 1;
378
9676b9a2 379 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
41d9d51a
AK
380 return ret;
381 *out_val = num*intnum/den;
382 return 0;
383}
384
385int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
386{
387 int64_t intnum = 1;
388 double num = 1;
389 int ret, den = 1;
390
9676b9a2 391 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
41d9d51a
AK
392 return ret;
393 *out_val = num*intnum/den;
394 return 0;
395}
396
397int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
398{
399 int64_t intnum = 1;
400 double num = 1;
401 int ret, den = 1;
402
9676b9a2 403 if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
41d9d51a
AK
404 return ret;
405
406 if (num == 1.0 && (int)intnum == intnum)
407 *out_val = (AVRational){intnum, den};
408 else
409 *out_val = av_d2q(num*intnum/den, 1<<24);
410 return 0;
411}
6ed04040 412
cb7c11cc
MS
413int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
414{
3b3ea346 415 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
299234a0
MS
416 const AVOption *flag = av_opt_find(obj, flag_name,
417 field ? field->unit : NULL, 0, 0);
3b3ea346 418 int64_t res;
cb7c11cc 419
145f741e 420 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
3b3ea346 421 av_opt_get_int(obj, field_name, 0, &res) < 0)
cb7c11cc 422 return 0;
124134e4 423 return res & flag->default_val.i64;
cb7c11cc
MS
424}
425
6ed04040
MN
426static void opt_list(void *obj, void *av_log_obj, const char *unit,
427 int req_flags, int rej_flags)
428{
429 const AVOption *opt=NULL;
430
3b3ea346 431 while ((opt = av_opt_next(obj, opt))) {
6ed04040
MN
432 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
433 continue;
434
435 /* Don't print CONST's on level one.
436 * Don't print anything but CONST's on level two.
437 * Only print items from the requested unit.
438 */
145f741e 439 if (!unit && opt->type==AV_OPT_TYPE_CONST)
6ed04040 440 continue;
145f741e 441 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
6ed04040 442 continue;
145f741e 443 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
6ed04040 444 continue;
145f741e 445 else if (unit && opt->type == AV_OPT_TYPE_CONST)
6ed04040
MN
446 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
447 else
448 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
449
8f1afd3b 450 switch (opt->type) {
145f741e 451 case AV_OPT_TYPE_FLAGS:
8f1afd3b 452 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
6ed04040 453 break;
145f741e 454 case AV_OPT_TYPE_INT:
8f1afd3b 455 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
6ed04040 456 break;
145f741e 457 case AV_OPT_TYPE_INT64:
8f1afd3b 458 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
6ed04040 459 break;
145f741e 460 case AV_OPT_TYPE_DOUBLE:
8f1afd3b 461 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
6ed04040 462 break;
145f741e 463 case AV_OPT_TYPE_FLOAT:
8f1afd3b 464 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
6ed04040 465 break;
145f741e 466 case AV_OPT_TYPE_STRING:
8f1afd3b 467 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
6ed04040 468 break;
145f741e 469 case AV_OPT_TYPE_RATIONAL:
8f1afd3b 470 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
6ed04040 471 break;
145f741e 472 case AV_OPT_TYPE_BINARY:
8f1afd3b 473 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
6ed04040 474 break;
145f741e 475 case AV_OPT_TYPE_CONST:
6ed04040 476 default:
8f1afd3b 477 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
6ed04040
MN
478 break;
479 }
480 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
481 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
482 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
483 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
484 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
c3ecd968
AK
485 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
486 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
6ed04040 487
8f1afd3b 488 if (opt->help)
6ed04040
MN
489 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
490 av_log(av_log_obj, AV_LOG_INFO, "\n");
145f741e 491 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
6ed04040
MN
492 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
493 }
494 }
495}
496
497int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
498{
8f1afd3b 499 if (!obj)
6ed04040
MN
500 return -1;
501
502 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
503
504 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
505
506 return 0;
507}
508
79eff913
AK
509void av_opt_set_defaults(void *s)
510{
6ed04040 511 const AVOption *opt = NULL;
3b3ea346 512 while ((opt = av_opt_next(s, opt)) != NULL) {
c3ecd968
AK
513 if (opt->flags & AV_OPT_FLAG_READONLY)
514 continue;
515
8f1afd3b 516 switch (opt->type) {
145f741e 517 case AV_OPT_TYPE_CONST:
6ed04040
MN
518 /* Nothing to be done here */
519 break;
d58dd4b5 520 case AV_OPT_TYPE_FLAGS:
e6153f17 521 case AV_OPT_TYPE_INT:
145f741e 522 case AV_OPT_TYPE_INT64:
4d7adec8 523 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
6ed04040 524 break;
145f741e
AK
525 case AV_OPT_TYPE_DOUBLE:
526 case AV_OPT_TYPE_FLOAT: {
6ed04040 527 double val;
b6675279 528 val = opt->default_val.dbl;
3b3ea346 529 av_opt_set_double(s, opt->name, val, 0);
6ed04040
MN
530 }
531 break;
145f741e 532 case AV_OPT_TYPE_RATIONAL: {
6ed04040 533 AVRational val;
b6675279 534 val = av_d2q(opt->default_val.dbl, INT_MAX);
3b3ea346 535 av_opt_set_q(s, opt->name, val, 0);
6ed04040
MN
536 }
537 break;
145f741e 538 case AV_OPT_TYPE_STRING:
3b3ea346 539 av_opt_set(s, opt->name, opt->default_val.str, 0);
6c117d43 540 break;
145f741e 541 case AV_OPT_TYPE_BINARY:
6c117d43 542 /* Cannot set default for binary */
6ed04040
MN
543 break;
544 default:
545 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
546 }
547 }
548}
549
c85eef4e
SS
550/**
551 * Store the value in the field in ctx that is named like key.
552 * ctx must be an AVClass context, storing is done using AVOptions.
553 *
554 * @param buf the string to parse, buf will be updated to point at the
555 * separator just after the parsed key/value pair
556 * @param key_val_sep a 0-terminated list of characters used to
557 * separate key from value
558 * @param pairs_sep a 0-terminated list of characters used to separate
559 * two pairs from each other
560 * @return 0 if the key/value pair has been successfully parsed and
561 * set, or a negative value corresponding to an AVERROR code in case
562 * of error:
563 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
3b3ea346 564 * the error code issued by av_opt_set() if the key/value pair
c85eef4e
SS
565 * cannot be set
566 */
567static int parse_key_value_pair(void *ctx, const char **buf,
568 const char *key_val_sep, const char *pairs_sep)
569{
570 char *key = av_get_token(buf, key_val_sep);
571 char *val;
572 int ret;
573
bcc94328
AK
574 if (!key)
575 return AVERROR(ENOMEM);
576
c85eef4e
SS
577 if (*key && strspn(*buf, key_val_sep)) {
578 (*buf)++;
579 val = av_get_token(buf, pairs_sep);
bcc94328
AK
580 if (!val) {
581 av_freep(&key);
582 return AVERROR(ENOMEM);
583 }
c85eef4e
SS
584 } else {
585 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
586 av_free(key);
587 return AVERROR(EINVAL);
588 }
589
590 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
591
cf53704c 592 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
58f84639 593 if (ret == AVERROR_OPTION_NOT_FOUND)
c85eef4e
SS
594 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
595
596 av_free(key);
597 av_free(val);
598 return ret;
599}
600
601int av_set_options_string(void *ctx, const char *opts,
602 const char *key_val_sep, const char *pairs_sep)
603{
604 int ret, count = 0;
605
85afbb1d
AK
606 if (!opts)
607 return 0;
608
c85eef4e
SS
609 while (*opts) {
610 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
611 return ret;
612 count++;
613
614 if (*opts)
615 opts++;
616 }
617
618 return count;
619}
620
b39b0623
AK
621void av_opt_free(void *obj)
622{
623 const AVOption *o = NULL;
3b3ea346 624 while ((o = av_opt_next(obj, o)))
145f741e 625 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
b39b0623
AK
626 av_freep((uint8_t *)obj + o->offset);
627}
628
7e83e1c5
AK
629int av_opt_set_dict(void *obj, AVDictionary **options)
630{
631 AVDictionaryEntry *t = NULL;
632 AVDictionary *tmp = NULL;
633 int ret = 0;
634
635 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
3b3ea346 636 ret = av_opt_set(obj, t->key, t->value, 0);
7e83e1c5
AK
637 if (ret == AVERROR_OPTION_NOT_FOUND)
638 av_dict_set(&tmp, t->key, t->value, 0);
639 else if (ret < 0) {
640 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
641 break;
642 }
643 ret = 0;
644 }
645 av_dict_free(options);
646 *options = tmp;
647 return ret;
648}
649
dc59ec5e
AK
650const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
651 int opt_flags, int search_flags)
652{
641c7afe
AK
653 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
654}
655
656const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
657 int opt_flags, int search_flags, void **target_obj)
658{
659 const AVClass *c = *(AVClass**)obj;
dc59ec5e
AK
660 const AVOption *o = NULL;
661
c7e921a5
MS
662 if (!c)
663 return NULL;
664
641c7afe
AK
665 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
666 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
667 const AVClass *child = NULL;
668 while (child = av_opt_child_class_next(c, child))
669 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
670 return o;
671 } else {
672 void *child = NULL;
673 while (child = av_opt_child_next(obj, child))
674 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
675 return o;
676 }
677 }
dc59ec5e 678
3b3ea346 679 while (o = av_opt_next(obj, o)) {
a726d7fd 680 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
145f741e 681 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
641c7afe
AK
682 (unit && o->unit && !strcmp(o->unit, unit)))) {
683 if (target_obj) {
684 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
685 *target_obj = obj;
686 else
687 *target_obj = NULL;
688 }
dc59ec5e 689 return o;
641c7afe 690 }
dc59ec5e
AK
691 }
692 return NULL;
693}
694
641c7afe
AK
695void *av_opt_child_next(void *obj, void *prev)
696{
697 const AVClass *c = *(AVClass**)obj;
698 if (c->child_next)
699 return c->child_next(obj, prev);
700 return NULL;
701}
702
703const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
704{
705 if (parent->child_class_next)
706 return parent->child_class_next(prev);
707 return NULL;
708}
709
c85eef4e
SS
710#ifdef TEST
711
c85eef4e
SS
712typedef struct TestContext
713{
714 const AVClass *class;
715 int num;
716 int toggle;
717 char *string;
718 int flags;
719 AVRational rational;
720} TestContext;
721
722#define OFFSET(x) offsetof(TestContext, x)
723
724#define TEST_FLAG_COOL 01
725#define TEST_FLAG_LAME 02
726#define TEST_FLAG_MU 04
727
728static const AVOption test_options[]= {
e6153f17
MS
729{"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
730{"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
21bc4403 731{"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
145f741e 732{"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
d58dd4b5 733{"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
124134e4
MS
734{"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
735{"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
736{"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
c85eef4e
SS
737{NULL},
738};
739
740static const char *test_get_name(void *ctx)
741{
742 return "test";
743}
744
745static const AVClass test_class = {
746 "TestContext",
747 test_get_name,
748 test_options
749};
750
751int main(void)
752{
753 int i;
754
755 printf("\nTesting av_set_options_string()\n");
756 {
757 TestContext test_ctx;
758 const char *options[] = {
759 "",
760 ":",
761 "=",
762 "foo=:",
763 ":=foo",
764 "=foo",
765 "foo=",
766 "foo",
767 "foo=val",
768 "foo==val",
769 "toggle=:",
770 "string=:",
771 "toggle=1 : foo",
772 "toggle=100",
773 "toggle==1",
774 "flags=+mu-lame : num=42: toggle=0",
775 "num=42 : string=blahblah",
776 "rational=0 : rational=1/2 : rational=1/-1",
777 "rational=-1/0",
778 };
779
780 test_ctx.class = &test_class;
79eff913 781 av_opt_set_defaults(&test_ctx);
c85eef4e
SS
782 test_ctx.string = av_strdup("default");
783
784 av_log_set_level(AV_LOG_DEBUG);
785
786 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
787 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
788 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
789 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
790 printf("\n");
791 }
792 }
793
794 return 0;
795}
796
797#endif