Libav
opt.c
Go to the documentation of this file.
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 
28 #include "avutil.h"
29 #include "avstring.h"
30 #include "common.h"
31 #include "opt.h"
32 #include "eval.h"
33 #include "dict.h"
34 #include "log.h"
35 #include "mathematics.h"
36 
37 const AVOption *av_opt_next(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, void *dst, double *num, int *den, int64_t *intnum)
48 {
49  switch (o->type) {
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;
56  *den = ((AVRational*)dst)->den;
57  return 0;
58  }
59  return AVERROR(EINVAL);
60 }
61 
62 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
63 {
64  if (o->type != AV_OPT_TYPE_FLAGS &&
65  (o->max * den < num * intnum || o->min * den > num * intnum)) {
66  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
67  num*intnum/den, o->name);
68  return AVERROR(ERANGE);
69  }
70 
71  switch (o->type) {
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;
78  if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
79  else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
80  break;
81  default:
82  return AVERROR(EINVAL);
83  }
84  return 0;
85 }
86 
87 static const double const_values[] = {
88  M_PI,
89  M_E,
91  0
92 };
93 
94 static const char * const const_names[] = {
95  "PI",
96  "E",
97  "QP2LAMBDA",
98  0
99 };
100 
101 static 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 
108 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
109 {
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)
118  return AVERROR(EINVAL);
119  len /= 2;
120 
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);
128  }
129  *ptr++ = (a << 4) | b;
130  }
131  *dst = bin;
132  *lendst = len;
133 
134  return 0;
135 }
136 
137 static 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 
144 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
145  opt->type == AV_OPT_TYPE_CONST || \
146  opt->type == AV_OPT_TYPE_FLAGS || \
147  opt->type == AV_OPT_TYPE_INT) ? \
148  opt->default_val.i64 : opt->default_val.dbl)
149 
150 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
151 {
152  int ret = 0, notfirst = 0;
153  for (;;) {
154  int i, den = 1;
155  char buf[256];
156  int cmd = 0;
157  double d, num = 1;
158  int64_t intnum = 1;
159 
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  }
167 
168  for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
169  buf[i] = val[i];
170  buf[i] = 0;
171 
172  {
173  const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
174  if (o_named && o_named->type == AV_OPT_TYPE_CONST)
175  d = DEFAULT_NUMVAL(o_named);
176  else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
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;
186  }
187  }
188  }
189  if (o->type == AV_OPT_TYPE_FLAGS) {
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;
193  } else {
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;
197  }
198 
199  if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
200  return ret;
201  val += i;
202  if (!*val)
203  return 0;
204  notfirst = 1;
205  }
206 
207  return 0;
208 }
209 
210 int 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)
216  if (!val)
217  return AVERROR(EINVAL);
218 
219  dst = ((uint8_t*)target_obj) + o->offset;
220  switch (o->type) {
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:
228  case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
229  }
230 
231  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
232  return AVERROR(EINVAL);
233 }
234 
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  {\
238  if (!o || o->type != opttype)\
239  return AVERROR(EINVAL);\
240  return set_string_number(obj, obj, o, val, name ## _out);\
241  }
242 
245 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
246 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
247 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
249 
250 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
251  int search_flags)
252 {
253  void *dst, *target_obj;
254  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
255 
256  if (!o || !target_obj)
258 
259  dst = ((uint8_t*)target_obj) + o->offset;
260  return write_number(obj, o, dst, num, den, intnum);
261 }
262 
263 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
264 {
265  return set_number(obj, name, 1, 1, val, search_flags);
266 }
267 
268 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
269 {
270  return set_number(obj, name, val, 1, 1, search_flags);
271 }
272 
273 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
274 {
275  return set_number(obj, name, val.num, val.den, 1, search_flags);
276 }
277 
278 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
279 {
280  void *target_obj;
281  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
282  uint8_t *ptr;
283  uint8_t **dst;
284  int *lendst;
285 
286  if (!o || !target_obj)
288 
289  if (o->type != AV_OPT_TYPE_BINARY)
290  return AVERROR(EINVAL);
291 
292  ptr = av_malloc(len);
293  if (!ptr)
294  return AVERROR(ENOMEM);
295 
296  dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
297  lendst = (int *)(dst + 1);
298 
299  av_free(*dst);
300  *dst = ptr;
301  *lendst = len;
302  memcpy(ptr, val, len);
303 
304  return 0;
305 }
306 
307 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
308 {
309  void *dst, *target_obj;
310  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
311  uint8_t *bin, buf[128];
312  int len, i, ret;
313 
314  if (!o || !target_obj)
316 
317  dst = (uint8_t*)target_obj + o->offset;
318 
319  buf[0] = 0;
320  switch (o->type) {
321  case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
322  case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
323  case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
324  case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
325  case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
326  case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
327  case AV_OPT_TYPE_STRING:
328  if (*(uint8_t**)dst)
329  *out_val = av_strdup(*(uint8_t**)dst);
330  else
331  *out_val = av_strdup("");
332  return 0;
333  case AV_OPT_TYPE_BINARY:
334  len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
335  if ((uint64_t)len*2 + 1 > INT_MAX)
336  return AVERROR(EINVAL);
337  if (!(*out_val = av_malloc(len*2 + 1)))
338  return AVERROR(ENOMEM);
339  bin = *(uint8_t**)dst;
340  for (i = 0; i < len; i++)
341  snprintf(*out_val + i*2, 3, "%02X", bin[i]);
342  return 0;
343  default:
344  return AVERROR(EINVAL);
345  }
346 
347  if (ret >= sizeof(buf))
348  return AVERROR(EINVAL);
349  *out_val = av_strdup(buf);
350  return 0;
351 }
352 
353 static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
354  int search_flags)
355 {
356  void *dst, *target_obj;
357  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
358  if (!o || !target_obj)
359  goto error;
360 
361  dst = ((uint8_t*)target_obj) + o->offset;
362 
363  return read_number(o, dst, num, den, intnum);
364 
365 error:
366  *den=*intnum=0;
367  return -1;
368 }
369 
370 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
371 {
372  int64_t intnum = 1;
373  double num = 1;
374  int ret, den = 1;
375 
376  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
377  return ret;
378  *out_val = num*intnum/den;
379  return 0;
380 }
381 
382 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
383 {
384  int64_t intnum = 1;
385  double num = 1;
386  int ret, den = 1;
387 
388  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
389  return ret;
390  *out_val = num*intnum/den;
391  return 0;
392 }
393 
394 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
395 {
396  int64_t intnum = 1;
397  double num = 1;
398  int ret, den = 1;
399 
400  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
401  return ret;
402 
403  if (num == 1.0 && (int)intnum == intnum)
404  *out_val = (AVRational){intnum, den};
405  else
406  *out_val = av_d2q(num*intnum/den, 1<<24);
407  return 0;
408 }
409 
410 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
411 {
412  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
413  const AVOption *flag = av_opt_find(obj, flag_name,
414  field ? field->unit : NULL, 0, 0);
415  int64_t res;
416 
417  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
418  av_opt_get_int(obj, field_name, 0, &res) < 0)
419  return 0;
420  return res & flag->default_val.i64;
421 }
422 
423 static void opt_list(void *obj, void *av_log_obj, const char *unit,
424  int req_flags, int rej_flags)
425 {
426  const AVOption *opt=NULL;
427 
428  while ((opt = av_opt_next(obj, opt))) {
429  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
430  continue;
431 
432  /* Don't print CONST's on level one.
433  * Don't print anything but CONST's on level two.
434  * Only print items from the requested unit.
435  */
436  if (!unit && opt->type==AV_OPT_TYPE_CONST)
437  continue;
438  else if (unit && opt->type!=AV_OPT_TYPE_CONST)
439  continue;
440  else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
441  continue;
442  else if (unit && opt->type == AV_OPT_TYPE_CONST)
443  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
444  else
445  av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
446 
447  switch (opt->type) {
448  case AV_OPT_TYPE_FLAGS:
449  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
450  break;
451  case AV_OPT_TYPE_INT:
452  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
453  break;
454  case AV_OPT_TYPE_INT64:
455  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
456  break;
457  case AV_OPT_TYPE_DOUBLE:
458  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
459  break;
460  case AV_OPT_TYPE_FLOAT:
461  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
462  break;
463  case AV_OPT_TYPE_STRING:
464  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
465  break;
467  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
468  break;
469  case AV_OPT_TYPE_BINARY:
470  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
471  break;
472  case AV_OPT_TYPE_CONST:
473  default:
474  av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
475  break;
476  }
477  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
478  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
479  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
480  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
481  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
482 
483  if (opt->help)
484  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
485  av_log(av_log_obj, AV_LOG_INFO, "\n");
486  if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
487  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
488  }
489  }
490 }
491 
492 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
493 {
494  if (!obj)
495  return -1;
496 
497  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
498 
499  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
500 
501  return 0;
502 }
503 
504 void av_opt_set_defaults(void *s)
505 {
506  const AVOption *opt = NULL;
507  while ((opt = av_opt_next(s, opt)) != NULL) {
508  switch (opt->type) {
509  case AV_OPT_TYPE_CONST:
510  /* Nothing to be done here */
511  break;
512  case AV_OPT_TYPE_FLAGS:
513  case AV_OPT_TYPE_INT:
514  case AV_OPT_TYPE_INT64:
515  av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
516  break;
517  case AV_OPT_TYPE_DOUBLE:
518  case AV_OPT_TYPE_FLOAT: {
519  double val;
520  val = opt->default_val.dbl;
521  av_opt_set_double(s, opt->name, val, 0);
522  }
523  break;
524  case AV_OPT_TYPE_RATIONAL: {
525  AVRational val;
526  val = av_d2q(opt->default_val.dbl, INT_MAX);
527  av_opt_set_q(s, opt->name, val, 0);
528  }
529  break;
530  case AV_OPT_TYPE_STRING:
531  av_opt_set(s, opt->name, opt->default_val.str, 0);
532  break;
533  case AV_OPT_TYPE_BINARY:
534  /* Cannot set default for binary */
535  break;
536  default:
537  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
538  }
539  }
540 }
541 
559 static int parse_key_value_pair(void *ctx, const char **buf,
560  const char *key_val_sep, const char *pairs_sep)
561 {
562  char *key = av_get_token(buf, key_val_sep);
563  char *val;
564  int ret;
565 
566  if (!key)
567  return AVERROR(ENOMEM);
568 
569  if (*key && strspn(*buf, key_val_sep)) {
570  (*buf)++;
571  val = av_get_token(buf, pairs_sep);
572  if (!val) {
573  av_freep(&key);
574  return AVERROR(ENOMEM);
575  }
576  } else {
577  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
578  av_free(key);
579  return AVERROR(EINVAL);
580  }
581 
582  av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
583 
584  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
585  if (ret == AVERROR_OPTION_NOT_FOUND)
586  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
587 
588  av_free(key);
589  av_free(val);
590  return ret;
591 }
592 
593 int av_set_options_string(void *ctx, const char *opts,
594  const char *key_val_sep, const char *pairs_sep)
595 {
596  int ret, count = 0;
597 
598  if (!opts)
599  return 0;
600 
601  while (*opts) {
602  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
603  return ret;
604  count++;
605 
606  if (*opts)
607  opts++;
608  }
609 
610  return count;
611 }
612 
613 void av_opt_free(void *obj)
614 {
615  const AVOption *o = NULL;
616  while ((o = av_opt_next(obj, o)))
617  if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
618  av_freep((uint8_t *)obj + o->offset);
619 }
620 
622 {
624  AVDictionary *tmp = NULL;
625  int ret = 0;
626 
627  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
628  ret = av_opt_set(obj, t->key, t->value, 0);
629  if (ret == AVERROR_OPTION_NOT_FOUND)
630  av_dict_set(&tmp, t->key, t->value, 0);
631  else if (ret < 0) {
632  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
633  break;
634  }
635  ret = 0;
636  }
637  av_dict_free(options);
638  *options = tmp;
639  return ret;
640 }
641 
642 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
643  int opt_flags, int search_flags)
644 {
645  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
646 }
647 
648 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
649  int opt_flags, int search_flags, void **target_obj)
650 {
651  const AVClass *c = *(AVClass**)obj;
652  const AVOption *o = NULL;
653 
654  if (!c)
655  return NULL;
656 
657  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
658  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
659  const AVClass *child = NULL;
660  while (child = av_opt_child_class_next(c, child))
661  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
662  return o;
663  } else {
664  void *child = NULL;
665  while (child = av_opt_child_next(obj, child))
666  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
667  return o;
668  }
669  }
670 
671  while (o = av_opt_next(obj, o)) {
672  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
673  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
674  (unit && o->unit && !strcmp(o->unit, unit)))) {
675  if (target_obj) {
676  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
677  *target_obj = obj;
678  else
679  *target_obj = NULL;
680  }
681  return o;
682  }
683  }
684  return NULL;
685 }
686 
687 void *av_opt_child_next(void *obj, void *prev)
688 {
689  const AVClass *c = *(AVClass**)obj;
690  if (c->child_next)
691  return c->child_next(obj, prev);
692  return NULL;
693 }
694 
695 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
696 {
697  if (parent->child_class_next)
698  return parent->child_class_next(prev);
699  return NULL;
700 }
701 
702 #ifdef TEST
703 
704 typedef struct TestContext
705 {
706  const AVClass *class;
707  int num;
708  int toggle;
709  char *string;
710  int flags;
711  AVRational rational;
712 } TestContext;
713 
714 #define OFFSET(x) offsetof(TestContext, x)
715 
716 #define TEST_FLAG_COOL 01
717 #define TEST_FLAG_LAME 02
718 #define TEST_FLAG_MU 04
719 
720 static const AVOption test_options[]= {
721 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
722 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
723 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
724 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
725 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
726 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
727 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
728 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
729 {NULL},
730 };
731 
732 static const char *test_get_name(void *ctx)
733 {
734  return "test";
735 }
736 
737 static const AVClass test_class = {
738  "TestContext",
739  test_get_name,
740  test_options
741 };
742 
743 int main(void)
744 {
745  int i;
746 
747  printf("\nTesting av_set_options_string()\n");
748  {
749  TestContext test_ctx;
750  const char *options[] = {
751  "",
752  ":",
753  "=",
754  "foo=:",
755  ":=foo",
756  "=foo",
757  "foo=",
758  "foo",
759  "foo=val",
760  "foo==val",
761  "toggle=:",
762  "string=:",
763  "toggle=1 : foo",
764  "toggle=100",
765  "toggle==1",
766  "flags=+mu-lame : num=42: toggle=0",
767  "num=42 : string=blahblah",
768  "rational=0 : rational=1/2 : rational=1/-1",
769  "rational=-1/0",
770  };
771 
772  test_ctx.class = &test_class;
773  av_opt_set_defaults(&test_ctx);
774  test_ctx.string = av_strdup("default");
775 
777 
778  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
779  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
780  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
781  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
782  printf("\n");
783  }
784  }
785 
786  return 0;
787 }
788 
789 #endif
union AVOption::@128 default_val
the default value for scalar options
static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
Definition: opt.c:47
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
class_name
Definition: ffv1enc.c:1077
AVOption.
Definition: opt.h:233
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:273
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:268
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
Definition: opt.c:687
void av_log_set_level(int level)
Set the log level.
Definition: log.c:170
const AVClass * av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
Iterate over potential AVOptions-enabled children of parent.
Definition: opt.c:695
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:504
int num
numerator
Definition: rational.h:44
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:266
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:593
external API header
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:278
#define FF_ARRAY_ELEMS(a)
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:394
static void opt_list(void *obj, void *av_log_obj, const char *unit, int req_flags, int rej_flags)
Definition: opt.c:423
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Public dictionary API.
const char * name
Definition: opt.h:234
uint8_t
const char * help
short English help text
Definition: opt.h:240
AVOptions.
#define b
Definition: input.c:52
const struct AVOption * option
a pointer to the first option specified in the class if any or NULL
Definition: log.h:51
const char * str
Definition: opt.h:255
const char * name
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
Definition: opt.c:268
static const double const_values[]
Definition: opt.c:87
static int flags
Definition: log.c:44
static float t
Definition: output.c:52
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:263
static int hexchar2int(char c)
Definition: opt.c:101
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:551
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
double max
maximum valid value for the option
Definition: opt.h:260
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:159
const AVOption * av_opt_next(void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:37
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:263
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
int main(int argc, char **argv)
Definition: avconv.c:2434
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:121
static const char *const const_names[]
Definition: opt.c:94
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition: opt.c:642
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:137
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:105
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:371
#define M_E
Definition: ratecontrol.c:38
const char * unit
The logical unit to which the option belongs.
Definition: opt.h:276
static int parse_key_value_pair(void *ctx, const char **buf, const char *key_val_sep, const char *pairs_sep)
Store the value in the field in ctx that is named like key.
Definition: opt.c:559
double min
minimum valid value for the option
Definition: opt.h:259
static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:150
static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:108
const struct AVClass *(* child_class_next)(const struct AVClass *prev)
Return an AVClass corresponding to the next potential AVOptions-enabled child.
Definition: log.h:89
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:226
int flags
Definition: opt.h:262
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:370
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
int offset
The offset relative to the context structure where the option value is stored.
Definition: opt.h:246
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:621
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
static int set_number(void *obj, const char *name, double num, int den, int64_t intnum, int search_flags)
Definition: opt.c:250
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:492
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:267
#define OPT_EVAL_NUMBER(name, opttype, vartype)
Definition: opt.c:235
#define llrint(x)
Definition: libm.h:101
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
Describe the class of an AVClass context structure.
Definition: log.h:33
rational number numerator/denominator
Definition: rational.h:43
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:264
double dbl
Definition: opt.h:254
const OptionDef options[]
Definition: avserver.c:4624
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:613
common internal and external API header
#define DEFAULT_NUMVAL(opt)
Definition: opt.c:144
#define class
Definition: math.h:25
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
Definition: opt.c:382
void *(* child_next)(void *obj, void *prev)
Return next AVOptions-enabled child or NULL.
Definition: log.h:79
#define AV_OPT_SEARCH_FAKE_OBJ
The obj passed to av_opt_find() is fake – only a double pointer to AVClass instead of a required poi...
Definition: opt.h:380
static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
Definition: opt.c:62
char * key
Definition: dict.h:75
int den
denominator
Definition: rational.h:45
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:307
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:56
char * value
Definition: dict.h:76
#define OFFSET(x)
Definition: avconv_opt.c:2151
enum AVOptionType type
Definition: opt.h:247
int len
int64_t i64
Definition: opt.h:253
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:207
int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
Check whether a particular flag is set in a flags field.
Definition: opt.c:410
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
const AVOption * av_opt_find2(void *obj, const char *name, const char *unit, int opt_flags, int search_flags, void **target_obj)
Look for an option in an object.
Definition: opt.c:648
float min
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:210
simple arithmetic expression evaluator
static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum, int search_flags)
Definition: opt.c:353