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