FFmpeg
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * AVOptions
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "avutil.h"
29 #include "avassert.h"
30 #include "avstring.h"
31 #include "channel_layout.h"
32 #include "dict.h"
33 #include "eval.h"
34 #include "log.h"
35 #include "mem.h"
36 #include "parseutils.h"
37 #include "pixdesc.h"
38 #include "mathematics.h"
39 #include "opt.h"
40 #include "samplefmt.h"
41 #include "bprint.h"
42 
43 #include <float.h>
44 
45 #define TYPE_BASE(type) ((type) & ~AV_OPT_TYPE_FLAG_ARRAY)
46 
47 const AVOption *av_opt_next(const void *obj, const AVOption *last)
48 {
49  const AVClass *class;
50  if (!obj)
51  return NULL;
52  class = *(const AVClass**)obj;
53  if (!last && class && class->option && class->option[0].name)
54  return class->option;
55  if (last && last[1].name)
56  return ++last;
57  return NULL;
58 }
59 
60 static const struct {
61  size_t size;
62  const char *name;
63 } opt_type_desc[] = {
64  [AV_OPT_TYPE_FLAGS] = { sizeof(unsigned), "<flags>" },
65  [AV_OPT_TYPE_INT] = { sizeof(int), "<int>" },
66  [AV_OPT_TYPE_INT64] = { sizeof(int64_t), "<int64>" },
67  [AV_OPT_TYPE_UINT] = { sizeof(unsigned), "<unsigned>" },
68  [AV_OPT_TYPE_UINT64] = { sizeof(uint64_t), "<uint64>" },
69  [AV_OPT_TYPE_DOUBLE] = { sizeof(double), "<double>" },
70  [AV_OPT_TYPE_FLOAT] = { sizeof(float), "<float>" },
71  [AV_OPT_TYPE_STRING] = { sizeof(char *), "<string>" },
72  [AV_OPT_TYPE_RATIONAL] = { sizeof(AVRational), "<rational>" },
73  [AV_OPT_TYPE_BINARY] = { sizeof(uint8_t *), "<binary>" },
74  [AV_OPT_TYPE_DICT] = { sizeof(AVDictionary *), "<dictionary>" },
75  [AV_OPT_TYPE_IMAGE_SIZE] = { sizeof(int[2]), "<image_size>" },
76  [AV_OPT_TYPE_VIDEO_RATE] = { sizeof(AVRational), "<video_rate>" },
77  [AV_OPT_TYPE_PIXEL_FMT] = { sizeof(int), "<pix_fmt>" },
78  [AV_OPT_TYPE_SAMPLE_FMT] = { sizeof(int), "<sample_fmt>" },
79  [AV_OPT_TYPE_DURATION] = { sizeof(int64_t), "<duration>" },
80  [AV_OPT_TYPE_COLOR] = { sizeof(uint8_t[4]), "<color>" },
81  [AV_OPT_TYPE_CHLAYOUT] = { sizeof(AVChannelLayout),"<channel_layout>" },
82  [AV_OPT_TYPE_BOOL] = { sizeof(int), "<boolean>" },
83 };
84 
85 // option is plain old data
86 static int opt_is_pod(enum AVOptionType type)
87 {
88  switch (type) {
89  case AV_OPT_TYPE_FLAGS:
90  case AV_OPT_TYPE_INT:
91  case AV_OPT_TYPE_INT64:
92  case AV_OPT_TYPE_DOUBLE:
93  case AV_OPT_TYPE_FLOAT:
95  case AV_OPT_TYPE_UINT64:
101  case AV_OPT_TYPE_COLOR:
102  case AV_OPT_TYPE_BOOL:
103  case AV_OPT_TYPE_UINT:
104  return 1;
105  }
106  return 0;
107 }
108 
109 static uint8_t opt_array_sep(const AVOption *o)
110 {
111  const AVOptionArrayDef *d = o->default_val.arr;
113  return (d && d->sep) ? d->sep : ',';
114 }
115 
116 static void *opt_array_pelem(const AVOption *o, void *array, unsigned idx)
117 {
119  return (uint8_t *)array + idx * opt_type_desc[TYPE_BASE(o->type)].size;
120 }
121 
122 static unsigned *opt_array_pcount(const void *parray)
123 {
124  return (unsigned *)((const void * const *)parray + 1);
125 }
126 
127 static void opt_free_elem(enum AVOptionType type, void *ptr)
128 {
129  switch (TYPE_BASE(type)) {
130  case AV_OPT_TYPE_STRING:
131  case AV_OPT_TYPE_BINARY:
132  av_freep(ptr);
133  break;
134 
135  case AV_OPT_TYPE_DICT:
136  av_dict_free((AVDictionary **)ptr);
137  break;
138 
141  break;
142 
143  default:
144  break;
145  }
146 }
147 
148 static void opt_free_array(const AVOption *o, void *parray, unsigned *count)
149 {
150  for (unsigned i = 0; i < *count; i++)
151  opt_free_elem(o->type, opt_array_pelem(o, *(void **)parray, i));
152 
153  av_freep(parray);
154  *count = 0;
155 }
156 
157 /**
158  * Perform common setup for option-setting functions.
159  *
160  * @param require_type when non-0, require the option to be of this type
161  * @param ptgt target object is written here
162  * @param po the option is written here
163  * @param pdst pointer to option value is written here
164  */
165 static int opt_set_init(void *obj, const char *name, int search_flags,
166  int require_type,
167  void **ptgt, const AVOption **po, void **pdst)
168 {
169  const AVOption *o;
170  void *tgt;
171 
172  o = av_opt_find2(obj, name, NULL, 0, search_flags, &tgt);
173  if (!o || !tgt)
175 
176  if (o->flags & AV_OPT_FLAG_READONLY)
177  return AVERROR(EINVAL);
178 
179  if (require_type && (o->type != require_type)) {
180  av_log(obj, AV_LOG_ERROR,
181  "Tried to set option '%s' of type %s from value of type %s, "
182  "this is not supported\n", o->name, opt_type_desc[o->type].name,
183  opt_type_desc[require_type].name);
184  return AVERROR(EINVAL);
185  }
186 
187  if (!(o->flags & AV_OPT_FLAG_RUNTIME_PARAM)) {
188  unsigned *state_flags = NULL;
189  const AVClass *class;
190 
191  // try state flags first from the target (child), then from its parent
192  class = *(const AVClass**)tgt;
193  if (class->state_flags_offset)
194  state_flags = (unsigned*)((uint8_t*)tgt + class->state_flags_offset);
195 
196  if (!state_flags && obj != tgt) {
197  class = *(const AVClass**)obj;
198  if (class->state_flags_offset)
199  state_flags = (unsigned*)((uint8_t*)obj + class->state_flags_offset);
200  }
201 
202  if (state_flags && (*state_flags & AV_CLASS_STATE_INITIALIZED)) {
203  av_log(obj, AV_LOG_ERROR, "Option '%s' is not a runtime option and "
204  "so cannot be set after the object has been initialized\n",
205  o->name);
206  return AVERROR(EINVAL);
207  }
208  }
209 
210  if (o->flags & AV_OPT_FLAG_DEPRECATED)
211  av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
212 
213  if (po)
214  *po = o;
215  if (ptgt)
216  *ptgt = tgt;
217  if (pdst)
218  *pdst = ((uint8_t *)tgt) + o->offset;
219 
220  return 0;
221 }
222 
224 {
225  AVRational r = av_d2q(d, 1 << 24);
226  if ((!r.num || !r.den) && d)
227  r = av_d2q(d, INT_MAX);
228  return r;
229 }
230 
231 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
232 {
233  switch (TYPE_BASE(o->type)) {
234  case AV_OPT_TYPE_FLAGS:
235  *intnum = *(unsigned int*)dst;
236  return 0;
238  *intnum = *(enum AVPixelFormat *)dst;
239  return 0;
241  *intnum = *(enum AVSampleFormat *)dst;
242  return 0;
243  case AV_OPT_TYPE_BOOL:
244  case AV_OPT_TYPE_INT:
245  *intnum = *(int *)dst;
246  return 0;
247  case AV_OPT_TYPE_UINT:
248  *intnum = *(unsigned int *)dst;
249  return 0;
251  case AV_OPT_TYPE_INT64:
252  case AV_OPT_TYPE_UINT64:
253  *intnum = *(int64_t *)dst;
254  return 0;
255  case AV_OPT_TYPE_FLOAT:
256  *num = *(float *)dst;
257  return 0;
258  case AV_OPT_TYPE_DOUBLE:
259  *num = *(double *)dst;
260  return 0;
262  *intnum = ((AVRational *)dst)->num;
263  *den = ((AVRational *)dst)->den;
264  return 0;
265  case AV_OPT_TYPE_CONST:
266  *intnum = o->default_val.i64;
267  return 0;
268  }
269  return AVERROR(EINVAL);
270 }
271 
272 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
273 {
274  const enum AVOptionType type = TYPE_BASE(o->type);
275 
276  if (type != AV_OPT_TYPE_FLAGS &&
277  (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
278  num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
279  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
280  num, o->name, o->min, o->max);
281  return AVERROR(ERANGE);
282  }
283  if (type == AV_OPT_TYPE_FLAGS) {
284  double d = num*intnum/den;
285  if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
286  av_log(obj, AV_LOG_ERROR,
287  "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
288  num*intnum/den, o->name);
289  return AVERROR(ERANGE);
290  }
291  }
292 
293  switch (type) {
295  *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
296  break;
298  *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
299  break;
300  case AV_OPT_TYPE_BOOL:
301  case AV_OPT_TYPE_FLAGS:
302  case AV_OPT_TYPE_INT:
303  case AV_OPT_TYPE_UINT:
304  *(int *)dst = llrint(num / den) * intnum;
305  break;
307  case AV_OPT_TYPE_INT64:{
308  double d = num / den;
309  if (intnum == 1 && d == (double)INT64_MAX) {
310  *(int64_t *)dst = INT64_MAX;
311  } else
312  *(int64_t *)dst = llrint(d) * intnum;
313  break;}
314  case AV_OPT_TYPE_UINT64:{
315  double d = num / den;
316  // We must special case uint64_t here as llrint() does not support values
317  // outside the int64_t range and there is no portable function which does
318  // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double
319  // while INT64_MAX is not
320  if (intnum == 1 && d == (double)UINT64_MAX) {
321  *(uint64_t *)dst = UINT64_MAX;
322  } else if (d > INT64_MAX + 1ULL) {
323  *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum;
324  } else {
325  *(uint64_t *)dst = llrint(d) * intnum;
326  }
327  break;}
328  case AV_OPT_TYPE_FLOAT:
329  *(float *)dst = num * intnum / den;
330  break;
331  case AV_OPT_TYPE_DOUBLE:
332  *(double *)dst = num * intnum / den;
333  break;
336  if ((int) num == num)
337  *(AVRational *)dst = (AVRational) { num *intnum, den };
338  else
339  *(AVRational *)dst = double_to_rational(num * intnum / den);
340  break;
341  default:
342  return AVERROR(EINVAL);
343  }
344  return 0;
345 }
346 
347 static int hexchar2int(char c) {
348  if (c >= '0' && c <= '9')
349  return c - '0';
350  if (c >= 'a' && c <= 'f')
351  return c - 'a' + 10;
352  if (c >= 'A' && c <= 'F')
353  return c - 'A' + 10;
354  return -1;
355 }
356 
357 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
358 {
359  int *lendst = (int *)(dst + 1);
360  uint8_t *bin, *ptr;
361  int len;
362 
363  av_freep(dst);
364  *lendst = 0;
365 
366  if (!val || !(len = strlen(val)))
367  return 0;
368 
369  if (len & 1)
370  return AVERROR(EINVAL);
371  len /= 2;
372 
373  ptr = bin = av_malloc(len);
374  if (!ptr)
375  return AVERROR(ENOMEM);
376  while (*val) {
377  int a = hexchar2int(*val++);
378  int b = hexchar2int(*val++);
379  if (a < 0 || b < 0) {
380  av_free(bin);
381  return AVERROR(EINVAL);
382  }
383  *ptr++ = (a << 4) | b;
384  }
385  *dst = bin;
386  *lendst = len;
387 
388  return 0;
389 }
390 
391 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
392 {
393  av_freep(dst);
394  if (!val)
395  return 0;
396  *dst = av_strdup(val);
397  return *dst ? 0 : AVERROR(ENOMEM);
398 }
399 
400 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
401  opt->type == AV_OPT_TYPE_UINT64 || \
402  opt->type == AV_OPT_TYPE_CONST || \
403  opt->type == AV_OPT_TYPE_FLAGS || \
404  opt->type == AV_OPT_TYPE_UINT || \
405  opt->type == AV_OPT_TYPE_INT) \
406  ? opt->default_val.i64 \
407  : opt->default_val.dbl)
408 
409 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
410 {
411  const enum AVOptionType type = TYPE_BASE(o->type);
412  int ret = 0;
413 
415  int num, den;
416  char c;
417  if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
418  if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
419  return ret;
420  ret = 0;
421  }
422  }
423 
424  for (;;) {
425  int i = 0;
426  char buf[256];
427  int cmd = 0;
428  double d;
429  int64_t intnum = 1;
430 
431  if (type == AV_OPT_TYPE_FLAGS) {
432  if (*val == '+' || *val == '-')
433  cmd = *(val++);
434  for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
435  buf[i] = val[i];
436  buf[i] = 0;
437  }
438 
439  {
440  int res;
441  int ci = 0;
442  double const_values[64];
443  const char * const_names[64];
444  int search_flags = (o->flags & AV_OPT_FLAG_CHILD_CONSTS) ? AV_OPT_SEARCH_CHILDREN : 0;
445  const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, search_flags);
446  if (o_named && o_named->type == AV_OPT_TYPE_CONST) {
447  d = DEFAULT_NUMVAL(o_named);
448  if (o_named->flags & AV_OPT_FLAG_DEPRECATED)
449  av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n",
450  o_named->name, o_named->help);
451  } else {
452  if (o->unit) {
453  for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
454  if (o_named->type == AV_OPT_TYPE_CONST &&
455  o_named->unit &&
456  !strcmp(o_named->unit, o->unit)) {
457  if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
458  av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
459  return AVERROR_PATCHWELCOME;
460  }
461  const_names [ci ] = o_named->name;
462  const_values[ci++] = DEFAULT_NUMVAL(o_named);
463  }
464  }
465  }
466  const_names [ci ] = "default";
467  const_values[ci++] = DEFAULT_NUMVAL(o);
468  const_names [ci ] = "max";
469  const_values[ci++] = o->max;
470  const_names [ci ] = "min";
471  const_values[ci++] = o->min;
472  const_names [ci ] = "none";
473  const_values[ci++] = 0;
474  const_names [ci ] = "all";
475  const_values[ci++] = ~0;
476  const_names [ci] = NULL;
477  const_values[ci] = 0;
478 
479  res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
480  const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
481  if (res < 0) {
482  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\"\n", o->name, val);
483  return res;
484  }
485  }
486  }
487  if (type == AV_OPT_TYPE_FLAGS) {
488  intnum = *(unsigned int*)dst;
489  if (cmd == '+')
490  d = intnum | (int64_t)d;
491  else if (cmd == '-')
492  d = intnum &~(int64_t)d;
493  }
494 
495  if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
496  return ret;
497  val += i;
498  if (!i || !*val)
499  return 0;
500  }
501 }
502 
503 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
504 {
505  int ret;
506 
507  if (!val || !strcmp(val, "none")) {
508  dst[0] =
509  dst[1] = 0;
510  return 0;
511  }
512  ret = av_parse_video_size(dst, dst + 1, val);
513  if (ret < 0)
514  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as image size\n", o->name, val);
515  return ret;
516 }
517 
518 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
519 {
520  int ret = av_parse_video_rate(dst, val);
521  if (ret < 0)
522  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as video rate\n", o->name, val);
523  return ret;
524 }
525 
526 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
527 {
528  int ret;
529 
530  if (!val) {
531  return 0;
532  } else {
533  ret = av_parse_color(dst, val, -1, obj);
534  if (ret < 0)
535  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as color\n", o->name, val);
536  return ret;
537  }
538  return 0;
539 }
540 
541 static const char *get_bool_name(int val)
542 {
543  if (val < 0)
544  return "auto";
545  return val ? "true" : "false";
546 }
547 
548 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
549 {
550  int n;
551 
552  if (!val)
553  return 0;
554 
555  if (!strcmp(val, "auto")) {
556  n = -1;
557  } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
558  n = 1;
559  } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
560  n = 0;
561  } else {
562  char *end = NULL;
563  n = strtol(val, &end, 10);
564  if (val + strlen(val) != end)
565  goto fail;
566  }
567 
568  if (n < o->min || n > o->max)
569  goto fail;
570 
571  *dst = n;
572  return 0;
573 
574 fail:
575  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as boolean\n", o->name, val);
576  return AVERROR(EINVAL);
577 }
578 
579 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
580  int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
581 {
582  int fmt, min, max;
583 
584  if (!val || !strcmp(val, "none")) {
585  fmt = -1;
586  } else {
587  fmt = get_fmt(val);
588  if (fmt == -1) {
589  char *tail;
590  fmt = strtol(val, &tail, 0);
591  if (*tail || (unsigned)fmt >= fmt_nb) {
592  av_log(obj, AV_LOG_ERROR,
593  "Unable to parse \"%s\" option value \"%s\" as %s\n", o->name, val, desc);
594  return AVERROR(EINVAL);
595  }
596  }
597  }
598 
599  min = FFMAX(o->min, -1);
600  max = FFMIN(o->max, fmt_nb-1);
601 
602  // hack for compatibility with old ffmpeg
603  if(min == 0 && max == 0) {
604  min = -1;
605  max = fmt_nb-1;
606  }
607 
608  if (fmt < min || fmt > max) {
609  av_log(obj, AV_LOG_ERROR,
610  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
611  fmt, o->name, desc, min, max);
612  return AVERROR(ERANGE);
613  }
614 
615  *(int *)dst = fmt;
616  return 0;
617 }
618 
619 static int get_pix_fmt(const char *name)
620 {
621  return av_get_pix_fmt(name);
622 }
623 
624 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
625 {
626  return set_string_fmt(obj, o, val, dst,
627  AV_PIX_FMT_NB, get_pix_fmt, "pixel format");
628 }
629 
630 static int get_sample_fmt(const char *name)
631 {
632  return av_get_sample_fmt(name);
633 }
634 
635 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
636 {
637  return set_string_fmt(obj, o, val, dst,
638  AV_SAMPLE_FMT_NB, get_sample_fmt, "sample format");
639 }
640 
641 static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
642 {
644 
645  if (val) {
646  int ret = av_dict_parse_string(&options, val, "=", ":", 0);
647  if (ret < 0) {
649  return ret;
650  }
651  }
652 
654  *dst = (uint8_t *)options;
655 
656  return 0;
657 }
658 
659 static int set_string_channel_layout(void *obj, const AVOption *o,
660  const char *val, void *dst)
661 {
662  AVChannelLayout *channel_layout = dst;
663  av_channel_layout_uninit(channel_layout);
664  if (!val)
665  return 0;
666  return av_channel_layout_from_string(channel_layout, val);
667 }
668 
669 static int opt_set_elem(void *obj, void *target_obj, const AVOption *o,
670  const char *val, void *dst)
671 {
672  const enum AVOptionType type = TYPE_BASE(o->type);
673  int ret;
674 
675  if (!val && (type != AV_OPT_TYPE_STRING &&
680  return AVERROR(EINVAL);
681 
682  switch (type) {
683  case AV_OPT_TYPE_BOOL:
684  return set_string_bool(obj, o, val, dst);
685  case AV_OPT_TYPE_STRING:
686  return set_string(obj, o, val, dst);
687  case AV_OPT_TYPE_BINARY:
688  return set_string_binary(obj, o, val, dst);
689  case AV_OPT_TYPE_FLAGS:
690  case AV_OPT_TYPE_INT:
691  case AV_OPT_TYPE_UINT:
692  case AV_OPT_TYPE_INT64:
693  case AV_OPT_TYPE_UINT64:
694  case AV_OPT_TYPE_FLOAT:
695  case AV_OPT_TYPE_DOUBLE:
697  return set_string_number(obj, target_obj, o, val, dst);
699  return set_string_image_size(obj, o, val, dst);
700  case AV_OPT_TYPE_VIDEO_RATE: {
701  AVRational tmp;
702  ret = set_string_video_rate(obj, o, val, &tmp);
703  if (ret < 0)
704  return ret;
705  return write_number(obj, o, dst, 1, tmp.den, tmp.num);
706  }
708  return set_string_pixel_fmt(obj, o, val, dst);
710  return set_string_sample_fmt(obj, o, val, dst);
712  {
713  int64_t usecs = 0;
714  if (val) {
715  if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
716  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as duration\n", o->name, val);
717  return ret;
718  }
719  }
720  if (usecs < o->min || usecs > o->max) {
721  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
722  usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
723  return AVERROR(ERANGE);
724  }
725  *(int64_t *)dst = usecs;
726  return 0;
727  }
728  case AV_OPT_TYPE_COLOR:
729  return set_string_color(obj, o, val, dst);
731  ret = set_string_channel_layout(obj, o, val, dst);
732  if (ret < 0) {
733  av_log(obj, AV_LOG_ERROR, "Unable to parse \"%s\" option value \"%s\" as channel layout\n", o->name, val);
734  ret = AVERROR(EINVAL);
735  }
736  return ret;
737  case AV_OPT_TYPE_DICT:
738  return set_string_dict(obj, o, val, dst);
739  }
740 
741  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
742  return AVERROR(EINVAL);
743 }
744 
745 static int opt_set_array(void *obj, void *target_obj, const AVOption *o,
746  const char *val, void *dst)
747 {
748  const AVOptionArrayDef *arr = o->default_val.arr;
749  const size_t elem_size = opt_type_desc[TYPE_BASE(o->type)].size;
750  const uint8_t sep = opt_array_sep(o);
751  uint8_t *str = NULL;
752 
753  void *elems = NULL;
754  unsigned nb_elems = 0;
755  int ret;
756 
757  if (val && *val) {
758  str = av_malloc(strlen(val) + 1);
759  if (!str)
760  return AVERROR(ENOMEM);
761  }
762 
763  // split and unescape the string
764  while (val && *val) {
765  uint8_t *p = str;
766  void *tmp;
767 
768  if (arr && arr->size_max && nb_elems >= arr->size_max) {
769  av_log(obj, AV_LOG_ERROR,
770  "Cannot assign more than %u elements to array option %s\n",
771  arr->size_max, o->name);
772  ret = AVERROR(EINVAL);
773  goto fail;
774  }
775 
776  for (; *val; val++, p++) {
777  if (*val == '\\' && val[1])
778  val++;
779  else if (*val == sep) {
780  val++;
781  break;
782  }
783  *p = *val;
784  }
785  *p = 0;
786 
787  tmp = av_realloc_array(elems, nb_elems + 1, elem_size);
788  if (!tmp) {
789  ret = AVERROR(ENOMEM);
790  goto fail;
791  }
792  elems = tmp;
793 
794  tmp = opt_array_pelem(o, elems, nb_elems);
795  memset(tmp, 0, elem_size);
796 
797  ret = opt_set_elem(obj, target_obj, o, str, tmp);
798  if (ret < 0)
799  goto fail;
800  nb_elems++;
801  }
802  av_freep(&str);
803 
805 
806  if (arr && nb_elems < arr->size_min) {
807  av_log(obj, AV_LOG_ERROR,
808  "Cannot assign fewer than %u elements to array option %s\n",
809  arr->size_min, o->name);
810  ret = AVERROR(EINVAL);
811  goto fail;
812  }
813 
814  *((void **)dst) = elems;
815  *opt_array_pcount(dst) = nb_elems;
816 
817  return 0;
818 fail:
819  av_freep(&str);
820  opt_free_array(o, &elems, &nb_elems);
821  return ret;
822 }
823 
824 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
825 {
826  void *dst, *target_obj;
827  const AVOption *o;
828  int ret;
829 
830  ret = opt_set_init(obj, name, search_flags, 0, &target_obj, &o, &dst);
831  if (ret < 0)
832  return ret;
833 
834  return ((o->type & AV_OPT_TYPE_FLAG_ARRAY) ?
835  opt_set_array : opt_set_elem)(obj, target_obj, o, val, dst);
836 }
837 
838 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
839 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
840  const char *val, vartype *name ## _out) \
841 { \
842  if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
843  return AVERROR(EINVAL); \
844  return set_string_number(obj, obj, o, val, name ## _out); \
845 }
846 
849 OPT_EVAL_NUMBER(uint, AV_OPT_TYPE_UINT, unsigned)
851 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
852 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
854 
855 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
856  int search_flags, int require_type)
857 {
858  void *dst;
859  const AVOption *o;
860  int ret;
861 
862  ret = opt_set_init(obj, name, search_flags, require_type, NULL, &o, &dst);
863  if (ret < 0)
864  return ret;
865 
866  return write_number(obj, o, dst, num, den, intnum);
867 }
868 
869 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
870 {
871  return set_number(obj, name, 1, 1, val, search_flags, 0);
872 }
873 
874 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
875 {
876  return set_number(obj, name, val, 1, 1, search_flags, 0);
877 }
878 
879 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
880 {
881  return set_number(obj, name, val.num, val.den, 1, search_flags, 0);
882 }
883 
884 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
885 {
886  uint8_t *ptr;
887  uint8_t **dst;
888  int *lendst;
889  int ret;
890 
891  ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_BINARY,
892  NULL, NULL, (void**)&dst);
893  if (ret < 0)
894  return ret;
895 
896  ptr = len ? av_malloc(len) : NULL;
897  if (len && !ptr)
898  return AVERROR(ENOMEM);
899 
900  lendst = (int *)(dst + 1);
901 
902  av_free(*dst);
903  *dst = ptr;
904  *lendst = len;
905  if (len)
906  memcpy(ptr, val, len);
907 
908  return 0;
909 }
910 
911 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
912 {
913  const AVOption *o;
914  int *dst;
915  int ret;
916 
917  ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_IMAGE_SIZE,
918  NULL, &o, (void**)&dst);
919  if (ret < 0)
920  return ret;
921 
922  if (w<0 || h<0) {
923  av_log(obj, AV_LOG_ERROR,
924  "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
925  return AVERROR(EINVAL);
926  }
927 
928  dst[0] = w;
929  dst[1] = h;
930 
931  return 0;
932 }
933 
934 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
935 {
936  return set_number(obj, name, val.num, val.den, 1, search_flags, AV_OPT_TYPE_VIDEO_RATE);
937 }
938 
939 static int set_format(void *obj, const char *name, int fmt, int search_flags,
940  enum AVOptionType type, const char *desc, int nb_fmts)
941 {
942  const AVOption *o;
943  int *dst;
944  int min, max, ret;
945 
946  ret = opt_set_init(obj, name, search_flags, type, NULL, &o, (void**)&dst);
947  if (ret < 0)
948  return ret;
949 
950  min = FFMAX(o->min, -1);
951  max = FFMIN(o->max, nb_fmts-1);
952 
953  if (fmt < min || fmt > max) {
954  av_log(obj, AV_LOG_ERROR,
955  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
956  fmt, name, desc, min, max);
957  return AVERROR(ERANGE);
958  }
959  *dst = fmt;
960  return 0;
961 }
962 
963 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
964 {
965  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
966 }
967 
968 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
969 {
970  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
971 }
972 
973 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
974  int search_flags)
975 {
976  AVDictionary **dst;
977  int ret;
978 
979  ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_DICT, NULL, NULL,
980  (void**)&dst);
981  if (ret < 0)
982  return ret;
983 
984  av_dict_free(dst);
985 
986  return av_dict_copy(dst, val, 0);
987 }
988 
989 int av_opt_set_chlayout(void *obj, const char *name,
990  const AVChannelLayout *channel_layout,
991  int search_flags)
992 {
994  int ret;
995 
996  ret = opt_set_init(obj, name, search_flags, AV_OPT_TYPE_CHLAYOUT, NULL, NULL,
997  (void**)&dst);
998  if (ret < 0)
999  return ret;
1000 
1001  return av_channel_layout_copy(dst, channel_layout);
1002 }
1003 
1004 static void format_duration(char *buf, size_t size, int64_t d)
1005 {
1006  char *e;
1007 
1008  av_assert0(size >= 25);
1009  if (d < 0 && d != INT64_MIN) {
1010  *(buf++) = '-';
1011  size--;
1012  d = -d;
1013  }
1014  if (d == INT64_MAX)
1015  snprintf(buf, size, "INT64_MAX");
1016  else if (d == INT64_MIN)
1017  snprintf(buf, size, "INT64_MIN");
1018  else if (d > (int64_t)3600*1000000)
1019  snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
1020  (int)((d / 60000000) % 60),
1021  (int)((d / 1000000) % 60),
1022  (int)(d % 1000000));
1023  else if (d > 60*1000000)
1024  snprintf(buf, size, "%d:%02d.%06d",
1025  (int)(d / 60000000),
1026  (int)((d / 1000000) % 60),
1027  (int)(d % 1000000));
1028  else
1029  snprintf(buf, size, "%d.%06d",
1030  (int)(d / 1000000),
1031  (int)(d % 1000000));
1032  e = buf + strlen(buf);
1033  while (e > buf && e[-1] == '0')
1034  *(--e) = 0;
1035  if (e > buf && e[-1] == '.')
1036  *(--e) = 0;
1037 }
1038 
1039 static int opt_get_elem(const AVOption *o, uint8_t **pbuf, size_t buf_len,
1040  const void *dst, int search_flags)
1041 {
1042  int ret;
1043 
1044  switch (TYPE_BASE(o->type)) {
1045  case AV_OPT_TYPE_BOOL:
1046  ret = snprintf(*pbuf, buf_len, "%s", get_bool_name(*(int *)dst));
1047  break;
1048  case AV_OPT_TYPE_FLAGS:
1049  ret = snprintf(*pbuf, buf_len, "0x%08X", *(int *)dst);
1050  break;
1051  case AV_OPT_TYPE_INT:
1052  ret = snprintf(*pbuf, buf_len, "%d", *(int *)dst);
1053  break;
1054  case AV_OPT_TYPE_UINT:
1055  ret = snprintf(*pbuf, buf_len, "%u", *(unsigned *)dst);
1056  break;
1057  case AV_OPT_TYPE_INT64:
1058  ret = snprintf(*pbuf, buf_len, "%"PRId64, *(int64_t *)dst);
1059  break;
1060  case AV_OPT_TYPE_UINT64:
1061  ret = snprintf(*pbuf, buf_len, "%"PRIu64, *(uint64_t *)dst);
1062  break;
1063  case AV_OPT_TYPE_FLOAT:
1064  ret = snprintf(*pbuf, buf_len, "%f", *(float *)dst);
1065  break;
1066  case AV_OPT_TYPE_DOUBLE:
1067  ret = snprintf(*pbuf, buf_len, "%f", *(double *)dst);
1068  break;
1070  case AV_OPT_TYPE_RATIONAL:
1071  ret = snprintf(*pbuf, buf_len, "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
1072  break;
1073  case AV_OPT_TYPE_CONST:
1074  ret = snprintf(*pbuf, buf_len, "%"PRId64, o->default_val.i64);
1075  break;
1076  case AV_OPT_TYPE_STRING:
1077  if (*(uint8_t **)dst) {
1078  *pbuf = av_strdup(*(uint8_t **)dst);
1079  } else if (search_flags & AV_OPT_ALLOW_NULL) {
1080  *pbuf = NULL;
1081  return 0;
1082  } else {
1083  *pbuf = av_strdup("");
1084  }
1085  return *pbuf ? 0 : AVERROR(ENOMEM);
1086  case AV_OPT_TYPE_BINARY: {
1087  const uint8_t *bin;
1088  int len;
1089 
1090  if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
1091  *pbuf = NULL;
1092  return 0;
1093  }
1094  len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
1095  if ((uint64_t)len * 2 + 1 > INT_MAX)
1096  return AVERROR(EINVAL);
1097  if (!(*pbuf = av_malloc(len * 2 + 1)))
1098  return AVERROR(ENOMEM);
1099  if (!len) {
1100  *pbuf[0] = '\0';
1101  return 0;
1102  }
1103  bin = *(uint8_t **)dst;
1104  for (int i = 0; i < len; i++)
1105  snprintf(*pbuf + i * 2, 3, "%02X", bin[i]);
1106  return 0;
1107  }
1109  ret = snprintf(*pbuf, buf_len, "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
1110  break;
1111  case AV_OPT_TYPE_PIXEL_FMT:
1112  ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
1113  break;
1115  ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
1116  break;
1117  case AV_OPT_TYPE_DURATION: {
1118  int64_t i64 = *(int64_t *)dst;
1119  format_duration(*pbuf, buf_len, i64);
1120  ret = strlen(*pbuf); // no overflow possible, checked by an assert
1121  break;
1122  }
1123  case AV_OPT_TYPE_COLOR:
1124  ret = snprintf(*pbuf, buf_len, "0x%02x%02x%02x%02x",
1125  (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
1126  (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
1127  break;
1128  case AV_OPT_TYPE_CHLAYOUT:
1129  ret = av_channel_layout_describe(dst, *pbuf, buf_len);
1130  break;
1131  case AV_OPT_TYPE_DICT:
1132  if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
1133  *pbuf = NULL;
1134  return 0;
1135  }
1136  return av_dict_get_string(*(AVDictionary **)dst, (char **)pbuf, '=', ':');
1137  default:
1138  return AVERROR(EINVAL);
1139  }
1140 
1141  return ret;
1142 }
1143 
1144 static int opt_get_array(const AVOption *o, void *dst, uint8_t **out_val)
1145 {
1146  const unsigned count = *opt_array_pcount(dst);
1147  const uint8_t sep = opt_array_sep(o);
1148 
1149  uint8_t *str = NULL;
1150  size_t str_len = 0;
1151  int ret;
1152 
1153  *out_val = NULL;
1154 
1155  for (unsigned i = 0; i < count; i++) {
1156  uint8_t buf[128], *out = buf;
1157  size_t out_len;
1158 
1159  ret = opt_get_elem(o, &out, sizeof(buf),
1160  opt_array_pelem(o, *(void **)dst, i), 0);
1161  if (ret < 0)
1162  goto fail;
1163 
1164  out_len = strlen(out);
1165  if (out_len > SIZE_MAX / 2 - !!i ||
1166  !!i + out_len * 2 > SIZE_MAX - str_len - 1) {
1167  ret = AVERROR(ERANGE);
1168  goto fail;
1169  }
1170 
1171  // terminator escaping separator
1172  // ↓ ↓ ↓
1173  ret = av_reallocp(&str, str_len + 1 + out_len * 2 + !!i);
1174  if (ret < 0)
1175  goto fail;
1176 
1177  // add separator if needed
1178  if (i)
1179  str[str_len++] = sep;
1180 
1181  // escape the element
1182  for (unsigned j = 0; j < out_len; j++) {
1183  uint8_t val = out[j];
1184  if (val == sep || val == '\\')
1185  str[str_len++] = '\\';
1186  str[str_len++] = val;
1187  }
1188  str[str_len] = 0;
1189 
1190 fail:
1191  if (out != buf)
1192  av_freep(&out);
1193  if (ret < 0) {
1194  av_freep(&str);
1195  return ret;
1196  }
1197  }
1198 
1199  *out_val = str;
1200 
1201  return 0;
1202 }
1203 
1204 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
1205 {
1206  void *dst, *target_obj;
1207  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1208  uint8_t *out, buf[128];
1209  int ret;
1210 
1211  if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
1212  return AVERROR_OPTION_NOT_FOUND;
1213 
1214  if (o->flags & AV_OPT_FLAG_DEPRECATED)
1215  av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
1216 
1217  dst = (uint8_t *)target_obj + o->offset;
1218 
1219  if (o->type & AV_OPT_TYPE_FLAG_ARRAY) {
1220  ret = opt_get_array(o, dst, out_val);
1221  if (ret < 0)
1222  return ret;
1223  if (!*out_val && !(search_flags & AV_OPT_ALLOW_NULL)) {
1224  *out_val = av_strdup("");
1225  if (!*out_val)
1226  return AVERROR(ENOMEM);
1227  }
1228  return 0;
1229  }
1230 
1231  buf[0] = 0;
1232  out = buf;
1233  ret = opt_get_elem(o, &out, sizeof(buf), dst, search_flags);
1234  if (ret < 0)
1235  return ret;
1236  if (out != buf) {
1237  *out_val = out;
1238  return 0;
1239  }
1240 
1241  if (ret >= sizeof(buf))
1242  return AVERROR(EINVAL);
1243  *out_val = av_strdup(out);
1244  return *out_val ? 0 : AVERROR(ENOMEM);
1245 }
1246 
1247 static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum,
1248  int search_flags)
1249 {
1250  void *dst, *target_obj;
1251  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1252  if (!o || !target_obj)
1253  return AVERROR_OPTION_NOT_FOUND;
1254  if (o->type & AV_OPT_TYPE_FLAG_ARRAY)
1255  return AVERROR(EINVAL);
1256 
1257  dst = ((uint8_t *)target_obj) + o->offset;
1258 
1259  return read_number(o, dst, num, den, intnum);
1260 }
1261 
1262 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
1263 {
1264  int64_t intnum = 1;
1265  double num = 1;
1266  int ret, den = 1;
1267 
1268  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1269  return ret;
1270  if (num == den)
1271  *out_val = intnum;
1272  else
1273  *out_val = num * intnum / den;
1274  return 0;
1275 }
1276 
1277 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
1278 {
1279  int64_t intnum = 1;
1280  double num = 1;
1281  int ret, den = 1;
1282 
1283  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1284  return ret;
1285  *out_val = num * intnum / den;
1286  return 0;
1287 }
1288 
1289 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
1290 {
1291  int64_t intnum = 1;
1292  double num = 1;
1293  int ret, den = 1;
1294 
1295  if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
1296  return ret;
1297 
1298  if (num == 1.0 && (int)intnum == intnum)
1299  *out_val = (AVRational){intnum, den};
1300  else
1301  *out_val = double_to_rational(num*intnum/den);
1302  return 0;
1303 }
1304 
1305 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
1306 {
1307  void *dst, *target_obj;
1308  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1309  if (!o || !target_obj)
1310  return AVERROR_OPTION_NOT_FOUND;
1311  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
1312  av_log(obj, AV_LOG_ERROR,
1313  "The value for option '%s' is not a image size.\n", name);
1314  return AVERROR(EINVAL);
1315  }
1316 
1317  dst = ((uint8_t*)target_obj) + o->offset;
1318  if (w_out) *w_out = *(int *)dst;
1319  if (h_out) *h_out = *((int *)dst+1);
1320  return 0;
1321 }
1322 
1323 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
1324 {
1325  return av_opt_get_q(obj, name, search_flags, out_val);
1326 }
1327 
1328 static int get_format(void *obj, const char *name, int search_flags, void *out_fmt,
1329  enum AVOptionType type, const char *desc)
1330 {
1331  void *dst, *target_obj;
1332  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1333  if (!o || !target_obj)
1334  return AVERROR_OPTION_NOT_FOUND;
1335  if (o->type != type) {
1336  av_log(obj, AV_LOG_ERROR,
1337  "The value for option '%s' is not a %s format.\n", desc, name);
1338  return AVERROR(EINVAL);
1339  }
1340 
1341  dst = ((uint8_t*)target_obj) + o->offset;
1342  if (type == AV_OPT_TYPE_PIXEL_FMT)
1343  *(enum AVPixelFormat *)out_fmt = *(enum AVPixelFormat *)dst;
1344  else
1345  *(enum AVSampleFormat*)out_fmt = *(enum AVSampleFormat*)dst;
1346 
1347  return 0;
1348 }
1349 
1350 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
1351 {
1352  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
1353 }
1354 
1355 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
1356 {
1357  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
1358 }
1359 
1360 int av_opt_get_chlayout(void *obj, const char *name, int search_flags, AVChannelLayout *cl)
1361 {
1362  void *dst, *target_obj;
1363  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1364  if (!o || !target_obj)
1365  return AVERROR_OPTION_NOT_FOUND;
1366  if (o->type != AV_OPT_TYPE_CHLAYOUT) {
1367  av_log(obj, AV_LOG_ERROR,
1368  "The value for option '%s' is not a channel layout.\n", name);
1369  return AVERROR(EINVAL);
1370  }
1371 
1372  dst = ((uint8_t*)target_obj) + o->offset;
1373  return av_channel_layout_copy(cl, dst);
1374 }
1375 
1376 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1377 {
1378  void *target_obj;
1379  AVDictionary *src;
1380  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1381 
1382  if (!o || !target_obj)
1383  return AVERROR_OPTION_NOT_FOUND;
1384  if (o->type != AV_OPT_TYPE_DICT)
1385  return AVERROR(EINVAL);
1386 
1387  src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1388 
1389  return av_dict_copy(out_val, src, 0);
1390 }
1391 
1392 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1393 {
1394  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1395  const AVOption *flag = av_opt_find(obj, flag_name,
1396  field ? field->unit : NULL, 0, 0);
1397  int64_t res;
1398 
1399  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1400  av_opt_get_int(obj, field_name, 0, &res) < 0)
1401  return 0;
1402  return res & flag->default_val.i64;
1403 }
1404 
1405 static void log_int_value(void *av_log_obj, int level, int64_t i)
1406 {
1407  if (i == INT_MAX) {
1408  av_log(av_log_obj, level, "INT_MAX");
1409  } else if (i == INT_MIN) {
1410  av_log(av_log_obj, level, "INT_MIN");
1411  } else if (i == UINT32_MAX) {
1412  av_log(av_log_obj, level, "UINT32_MAX");
1413  } else if (i == INT64_MAX) {
1414  av_log(av_log_obj, level, "I64_MAX");
1415  } else if (i == INT64_MIN) {
1416  av_log(av_log_obj, level, "I64_MIN");
1417  } else {
1418  av_log(av_log_obj, level, "%"PRId64, i);
1419  }
1420 }
1421 
1422 static void log_value(void *av_log_obj, int level, double d)
1423 {
1424  if (d == INT_MAX) {
1425  av_log(av_log_obj, level, "INT_MAX");
1426  } else if (d == INT_MIN) {
1427  av_log(av_log_obj, level, "INT_MIN");
1428  } else if (d == UINT32_MAX) {
1429  av_log(av_log_obj, level, "UINT32_MAX");
1430  } else if (d == (double)INT64_MAX) {
1431  av_log(av_log_obj, level, "I64_MAX");
1432  } else if (d == INT64_MIN) {
1433  av_log(av_log_obj, level, "I64_MIN");
1434  } else if (d == FLT_MAX) {
1435  av_log(av_log_obj, level, "FLT_MAX");
1436  } else if (d == FLT_MIN) {
1437  av_log(av_log_obj, level, "FLT_MIN");
1438  } else if (d == -FLT_MAX) {
1439  av_log(av_log_obj, level, "-FLT_MAX");
1440  } else if (d == -FLT_MIN) {
1441  av_log(av_log_obj, level, "-FLT_MIN");
1442  } else if (d == DBL_MAX) {
1443  av_log(av_log_obj, level, "DBL_MAX");
1444  } else if (d == DBL_MIN) {
1445  av_log(av_log_obj, level, "DBL_MIN");
1446  } else if (d == -DBL_MAX) {
1447  av_log(av_log_obj, level, "-DBL_MAX");
1448  } else if (d == -DBL_MIN) {
1449  av_log(av_log_obj, level, "-DBL_MIN");
1450  } else {
1451  av_log(av_log_obj, level, "%g", d);
1452  }
1453 }
1454 
1455 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1456 {
1457  const AVOption *opt = NULL;
1458 
1459  if (!unit)
1460  return NULL;
1461  while ((opt = av_opt_next(obj, opt)))
1462  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1463  opt->default_val.i64 == value)
1464  return opt->name;
1465  return NULL;
1466 }
1467 
1468 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1469 {
1470  const AVOption *opt = NULL;
1471  char flags[512];
1472 
1473  flags[0] = 0;
1474  if (!unit)
1475  return NULL;
1476  while ((opt = av_opt_next(obj, opt))) {
1477  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1478  opt->default_val.i64 & value) {
1479  if (flags[0])
1480  av_strlcatf(flags, sizeof(flags), "+");
1481  av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1482  }
1483  }
1484  if (flags[0])
1485  return av_strdup(flags);
1486  return NULL;
1487 }
1488 
1489 static void log_type(void *av_log_obj, const AVOption *o,
1490  enum AVOptionType parent_type)
1491 {
1492  const enum AVOptionType type = TYPE_BASE(o->type);
1493 
1494  if (o->type == AV_OPT_TYPE_CONST && (TYPE_BASE(parent_type) == AV_OPT_TYPE_INT || TYPE_BASE(parent_type) == AV_OPT_TYPE_INT64))
1495  av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", o->default_val.i64);
1497  if (o->type & AV_OPT_TYPE_FLAG_ARRAY)
1498  av_log(av_log_obj, AV_LOG_INFO, "[%-10s]", opt_type_desc[type].name);
1499  else
1500  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", opt_type_desc[type].name);
1501  }
1502  else
1503  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1504 }
1505 
1506 static void log_default(void *obj, void *av_log_obj, const AVOption *opt)
1507 {
1508  if (opt->type == AV_OPT_TYPE_CONST || opt->type == AV_OPT_TYPE_BINARY)
1509  return;
1510  if ((opt->type == AV_OPT_TYPE_COLOR ||
1511  opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1512  opt->type == AV_OPT_TYPE_STRING ||
1513  opt->type == AV_OPT_TYPE_DICT ||
1514  opt->type == AV_OPT_TYPE_CHLAYOUT ||
1515  opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1516  !opt->default_val.str)
1517  return;
1518 
1519  if (opt->type & AV_OPT_TYPE_FLAG_ARRAY) {
1520  const AVOptionArrayDef *arr = opt->default_val.arr;
1521  if (arr && arr->def)
1522  av_log(av_log_obj, AV_LOG_INFO, " (default %s)", arr->def);
1523  return;
1524  }
1525 
1526  av_log(av_log_obj, AV_LOG_INFO, " (default ");
1527  switch (opt->type) {
1528  case AV_OPT_TYPE_BOOL:
1529  av_log(av_log_obj, AV_LOG_INFO, "%s", get_bool_name(opt->default_val.i64));
1530  break;
1531  case AV_OPT_TYPE_FLAGS: {
1532  char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1533  if (def_flags) {
1534  av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1535  av_freep(&def_flags);
1536  } else {
1537  av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1538  }
1539  break;
1540  }
1541  case AV_OPT_TYPE_DURATION: {
1542  char buf[25];
1543  format_duration(buf, sizeof(buf), opt->default_val.i64);
1544  av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1545  break;
1546  }
1547  case AV_OPT_TYPE_UINT:
1548  case AV_OPT_TYPE_INT:
1549  case AV_OPT_TYPE_UINT64:
1550  case AV_OPT_TYPE_INT64: {
1551  const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1552  if (def_const)
1553  av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1554  else
1555  log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1556  break;
1557  }
1558  case AV_OPT_TYPE_DOUBLE:
1559  case AV_OPT_TYPE_FLOAT:
1560  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1561  break;
1562  case AV_OPT_TYPE_RATIONAL: {
1563  AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1564  av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1565  break;
1566  case AV_OPT_TYPE_PIXEL_FMT:
1567  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1568  break;
1570  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1571  break;
1572  case AV_OPT_TYPE_COLOR:
1574  case AV_OPT_TYPE_STRING:
1575  case AV_OPT_TYPE_DICT:
1577  case AV_OPT_TYPE_CHLAYOUT:
1578  av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1579  break;
1580  }
1581  av_log(av_log_obj, AV_LOG_INFO, ")");
1582 }
1583 
1584 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1585  int req_flags, int rej_flags, enum AVOptionType parent_type)
1586 {
1587  const AVOption *opt = NULL;
1588  AVOptionRanges *r;
1589  int i;
1590 
1591  while ((opt = av_opt_next(obj, opt))) {
1592  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1593  continue;
1594 
1595  /* Don't print CONST's on level one.
1596  * Don't print anything but CONST's on level two.
1597  * Only print items from the requested unit.
1598  */
1599  if (!unit && opt->type == AV_OPT_TYPE_CONST)
1600  continue;
1601  else if (unit && opt->type != AV_OPT_TYPE_CONST)
1602  continue;
1603  else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1604  continue;
1605  else if (unit && opt->type == AV_OPT_TYPE_CONST)
1606  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1607  else
1608  av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1609  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? " " : "-",
1610  opt->name);
1611 
1612  log_type(av_log_obj, opt, parent_type);
1613 
1614  av_log(av_log_obj, AV_LOG_INFO, "%c%c%c%c%c%c%c%c%c%c%c",
1615  (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.',
1616  (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.',
1617  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? 'F' : '.',
1618  (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) ? 'V' : '.',
1619  (opt->flags & AV_OPT_FLAG_AUDIO_PARAM) ? 'A' : '.',
1620  (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.',
1621  (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.',
1622  (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.',
1623  (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.',
1624  (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.',
1625  (opt->flags & AV_OPT_FLAG_DEPRECATED) ? 'P' : '.');
1626 
1627  if (opt->help)
1628  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1629 
1630  if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1631  switch (opt->type) {
1632  case AV_OPT_TYPE_INT:
1633  case AV_OPT_TYPE_UINT:
1634  case AV_OPT_TYPE_INT64:
1635  case AV_OPT_TYPE_UINT64:
1636  case AV_OPT_TYPE_DOUBLE:
1637  case AV_OPT_TYPE_FLOAT:
1638  case AV_OPT_TYPE_RATIONAL:
1639  for (i = 0; i < r->nb_ranges; i++) {
1640  av_log(av_log_obj, AV_LOG_INFO, " (from ");
1641  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1642  av_log(av_log_obj, AV_LOG_INFO, " to ");
1643  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1644  av_log(av_log_obj, AV_LOG_INFO, ")");
1645  }
1646  break;
1647  }
1649  }
1650 
1651  log_default(obj, av_log_obj, opt);
1652 
1653  av_log(av_log_obj, AV_LOG_INFO, "\n");
1654  if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1655  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1656  }
1657 }
1658 
1659 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1660 {
1661  if (!obj)
1662  return -1;
1663 
1664  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1665 
1666  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1667 
1668  return 0;
1669 }
1670 
1672 {
1673  av_opt_set_defaults2(s, 0, 0);
1674 }
1675 
1676 void av_opt_set_defaults2(void *s, int mask, int flags)
1677 {
1678  const AVOption *opt = NULL;
1679  while ((opt = av_opt_next(s, opt))) {
1680  void *dst = ((uint8_t*)s) + opt->offset;
1681 
1682  if ((opt->flags & mask) != flags)
1683  continue;
1684 
1685  if (opt->flags & AV_OPT_FLAG_READONLY)
1686  continue;
1687 
1688  if (opt->type & AV_OPT_TYPE_FLAG_ARRAY) {
1689  const AVOptionArrayDef *arr = opt->default_val.arr;
1690  const char sep = opt_array_sep(opt);
1691 
1692  av_assert0(sep && sep != '\\' &&
1693  (sep < 'a' || sep > 'z') &&
1694  (sep < 'A' || sep > 'Z') &&
1695  (sep < '0' || sep > '9'));
1696 
1697  if (arr && arr->def)
1698  opt_set_array(s, s, opt, arr->def, dst);
1699 
1700  continue;
1701  }
1702 
1703  switch (opt->type) {
1704  case AV_OPT_TYPE_CONST:
1705  /* Nothing to be done here */
1706  break;
1707  case AV_OPT_TYPE_BOOL:
1708  case AV_OPT_TYPE_FLAGS:
1709  case AV_OPT_TYPE_INT:
1710  case AV_OPT_TYPE_UINT:
1711  case AV_OPT_TYPE_INT64:
1712  case AV_OPT_TYPE_UINT64:
1713  case AV_OPT_TYPE_DURATION:
1714  case AV_OPT_TYPE_PIXEL_FMT:
1716  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1717  break;
1718  case AV_OPT_TYPE_DOUBLE:
1719  case AV_OPT_TYPE_FLOAT: {
1720  double val;
1721  val = opt->default_val.dbl;
1722  write_number(s, opt, dst, val, 1, 1);
1723  }
1724  break;
1725  case AV_OPT_TYPE_RATIONAL: {
1726  AVRational val;
1727  val = av_d2q(opt->default_val.dbl, INT_MAX);
1728  write_number(s, opt, dst, 1, val.den, val.num);
1729  }
1730  break;
1731  case AV_OPT_TYPE_COLOR:
1732  set_string_color(s, opt, opt->default_val.str, dst);
1733  break;
1734  case AV_OPT_TYPE_STRING:
1735  set_string(s, opt, opt->default_val.str, dst);
1736  break;
1738  set_string_image_size(s, opt, opt->default_val.str, dst);
1739  break;
1741  set_string_video_rate(s, opt, opt->default_val.str, dst);
1742  break;
1743  case AV_OPT_TYPE_BINARY:
1744  set_string_binary(s, opt, opt->default_val.str, dst);
1745  break;
1746  case AV_OPT_TYPE_CHLAYOUT:
1748  break;
1749  case AV_OPT_TYPE_DICT:
1750  set_string_dict(s, opt, opt->default_val.str, dst);
1751  break;
1752  default:
1753  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1754  opt->type, opt->name);
1755  }
1756  }
1757 }
1758 
1759 /**
1760  * Store the value in the field in ctx that is named like key.
1761  * ctx must be an AVClass context, storing is done using AVOptions.
1762  *
1763  * @param buf the string to parse, buf will be updated to point at the
1764  * separator just after the parsed key/value pair
1765  * @param key_val_sep a 0-terminated list of characters used to
1766  * separate key from value
1767  * @param pairs_sep a 0-terminated list of characters used to separate
1768  * two pairs from each other
1769  * @return 0 if the key/value pair has been successfully parsed and
1770  * set, or a negative value corresponding to an AVERROR code in case
1771  * of error:
1772  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1773  * the error code issued by av_opt_set() if the key/value pair
1774  * cannot be set
1775  */
1776 static int parse_key_value_pair(void *ctx, const char **buf,
1777  const char *key_val_sep, const char *pairs_sep)
1778 {
1779  char *key = av_get_token(buf, key_val_sep);
1780  char *val;
1781  int ret;
1782 
1783  if (!key)
1784  return AVERROR(ENOMEM);
1785 
1786  if (*key && strspn(*buf, key_val_sep)) {
1787  (*buf)++;
1788  val = av_get_token(buf, pairs_sep);
1789  if (!val) {
1790  av_freep(&key);
1791  return AVERROR(ENOMEM);
1792  }
1793  } else {
1794  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1795  av_free(key);
1796  return AVERROR(EINVAL);
1797  }
1798 
1799  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1800 
1803  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1804 
1805  av_free(key);
1806  av_free(val);
1807  return ret;
1808 }
1809 
1810 int av_set_options_string(void *ctx, const char *opts,
1811  const char *key_val_sep, const char *pairs_sep)
1812 {
1813  int ret, count = 0;
1814 
1815  if (!opts)
1816  return 0;
1817 
1818  while (*opts) {
1819  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1820  return ret;
1821  count++;
1822 
1823  if (*opts)
1824  opts++;
1825  }
1826 
1827  return count;
1828 }
1829 
1830 #define WHITESPACES " \n\t\r"
1831 
1832 static int is_key_char(char c)
1833 {
1834  return (unsigned)((c | 32) - 'a') < 26 ||
1835  (unsigned)(c - '0') < 10 ||
1836  c == '-' || c == '_' || c == '/' || c == '.';
1837 }
1838 
1839 /**
1840  * Read a key from a string.
1841  *
1842  * The key consists of is_key_char characters and must be terminated by a
1843  * character from the delim string; spaces are ignored.
1844  *
1845  * @return 0 for success (even with ellipsis), <0 for failure
1846  */
1847 static int get_key(const char **ropts, const char *delim, char **rkey)
1848 {
1849  const char *opts = *ropts;
1850  const char *key_start, *key_end;
1851 
1852  key_start = opts += strspn(opts, WHITESPACES);
1853  while (is_key_char(*opts))
1854  opts++;
1855  key_end = opts;
1856  opts += strspn(opts, WHITESPACES);
1857  if (!*opts || !strchr(delim, *opts))
1858  return AVERROR(EINVAL);
1859  opts++;
1860  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1861  return AVERROR(ENOMEM);
1862  memcpy(*rkey, key_start, key_end - key_start);
1863  (*rkey)[key_end - key_start] = 0;
1864  *ropts = opts;
1865  return 0;
1866 }
1867 
1868 int av_opt_get_key_value(const char **ropts,
1869  const char *key_val_sep, const char *pairs_sep,
1870  unsigned flags,
1871  char **rkey, char **rval)
1872 {
1873  int ret;
1874  char *key = NULL, *val;
1875  const char *opts = *ropts;
1876 
1877  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1879  return AVERROR(EINVAL);
1880  if (!(val = av_get_token(&opts, pairs_sep))) {
1881  av_free(key);
1882  return AVERROR(ENOMEM);
1883  }
1884  *ropts = opts;
1885  *rkey = key;
1886  *rval = val;
1887  return 0;
1888 }
1889 
1890 int av_opt_set_from_string(void *ctx, const char *opts,
1891  const char *const *shorthand,
1892  const char *key_val_sep, const char *pairs_sep)
1893 {
1894  int ret, count = 0;
1895  const char *dummy_shorthand = NULL;
1896  const char *key;
1897 
1898  if (!opts)
1899  return 0;
1900  if (!shorthand)
1901  shorthand = &dummy_shorthand;
1902 
1903  while (*opts) {
1904  char *parsed_key, *value;
1905  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1906  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1907  &parsed_key, &value);
1908  if (ret < 0) {
1909  if (ret == AVERROR(EINVAL))
1910  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1911  else
1912  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1913  av_err2str(ret));
1914  return ret;
1915  }
1916  if (*opts)
1917  opts++;
1918  if (parsed_key) {
1919  key = parsed_key;
1920  while (*shorthand) /* discard all remaining shorthand */
1921  shorthand++;
1922  } else {
1923  key = *(shorthand++);
1924  }
1925 
1926  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1927  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1929  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1930  av_free(value);
1931  av_free(parsed_key);
1932  return ret;
1933  }
1934 
1935  av_free(value);
1936  av_free(parsed_key);
1937  count++;
1938  }
1939  return count;
1940 }
1941 
1942 void av_opt_free(void *obj)
1943 {
1944  const AVOption *o = NULL;
1945  while ((o = av_opt_next(obj, o))) {
1946  void *pitem = (uint8_t *)obj + o->offset;
1947 
1949  opt_free_array(o, pitem, opt_array_pcount(pitem));
1950  else
1951  opt_free_elem(o->type, pitem);
1952  }
1953 }
1954 
1955 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1956 {
1957  const AVDictionaryEntry *t = NULL;
1958  AVDictionary *tmp = NULL;
1959  int ret;
1960 
1961  if (!options)
1962  return 0;
1963 
1964  while ((t = av_dict_iterate(*options, t))) {
1965  ret = av_opt_set(obj, t->key, t->value, search_flags);
1968  if (ret < 0) {
1969  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1970  av_dict_free(&tmp);
1971  return ret;
1972  }
1973  }
1975  *options = tmp;
1976  return 0;
1977 }
1978 
1980 {
1981  return av_opt_set_dict2(obj, options, 0);
1982 }
1983 
1984 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1985  int opt_flags, int search_flags)
1986 {
1987  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1988 }
1989 
1990 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1991  int opt_flags, int search_flags, void **target_obj)
1992 {
1993  const AVClass *c;
1994  const AVOption *o = NULL;
1995 
1996  if(!obj)
1997  return NULL;
1998 
1999  c= *(AVClass**)obj;
2000 
2001  if (!c)
2002  return NULL;
2003 
2004  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
2005  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
2006  void *iter = NULL;
2007  const AVClass *child;
2008  while (child = av_opt_child_class_iterate(c, &iter))
2009  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
2010  return o;
2011  } else {
2012  void *child = NULL;
2013  while (child = av_opt_child_next(obj, child))
2014  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
2015  return o;
2016  }
2017  }
2018 
2019  while (o = av_opt_next(obj, o)) {
2020  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
2021  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
2022  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
2023  if (target_obj) {
2024  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
2025  *target_obj = obj;
2026  else
2027  *target_obj = NULL;
2028  }
2029  return o;
2030  }
2031  }
2032  return NULL;
2033 }
2034 
2035 void *av_opt_child_next(void *obj, void *prev)
2036 {
2037  const AVClass *c = *(AVClass **)obj;
2038  if (c->child_next)
2039  return c->child_next(obj, prev);
2040  return NULL;
2041 }
2042 
2043 const AVClass *av_opt_child_class_iterate(const AVClass *parent, void **iter)
2044 {
2045  if (parent->child_class_iterate)
2046  return parent->child_class_iterate(iter);
2047  return NULL;
2048 }
2049 
2050 static int opt_copy_elem(void *logctx, enum AVOptionType type,
2051  void *dst, const void *src)
2052 {
2053  if (type == AV_OPT_TYPE_STRING) {
2054  const char *src_str = *(const char *const *)src;
2055  char **dstp = (char **)dst;
2056  if (*dstp != src_str)
2057  av_freep(dstp);
2058  if (src_str) {
2059  *dstp = av_strdup(src_str);
2060  if (!*dstp)
2061  return AVERROR(ENOMEM);
2062  }
2063  } else if (type == AV_OPT_TYPE_BINARY) {
2064  const uint8_t *const *src8 = (const uint8_t *const *)src;
2065  uint8_t **dst8 = (uint8_t **)dst;
2066  int len = *(const int *)(src8 + 1);
2067  if (*dst8 != *src8)
2068  av_freep(dst8);
2069  *dst8 = av_memdup(*src8, len);
2070  if (len && !*dst8) {
2071  *(int *)(dst8 + 1) = 0;
2072  return AVERROR(ENOMEM);
2073  }
2074  *(int *)(dst8 + 1) = len;
2075  } else if (type == AV_OPT_TYPE_CONST) {
2076  // do nothing
2077  } else if (type == AV_OPT_TYPE_DICT) {
2078  const AVDictionary *sdict = *(const AVDictionary * const *)src;
2079  AVDictionary **ddictp = (AVDictionary **)dst;
2080  if (sdict != *ddictp)
2081  av_dict_free(ddictp);
2082  *ddictp = NULL;
2083  return av_dict_copy(ddictp, sdict, 0);
2084  } else if (type == AV_OPT_TYPE_CHLAYOUT) {
2085  if (dst != src)
2086  return av_channel_layout_copy(dst, src);
2087  } else if (opt_is_pod(type)) {
2088  size_t size = opt_type_desc[type].size;
2089  memcpy(dst, src, size);
2090  } else {
2091  av_log(logctx, AV_LOG_ERROR, "Unhandled option type: %d\n", type);
2092  return AVERROR(EINVAL);
2093  }
2094 
2095  return 0;
2096 }
2097 
2098 static int opt_copy_array(void *logctx, const AVOption *o,
2099  void **pdst, const void * const *psrc)
2100 {
2101  unsigned nb_elems = *opt_array_pcount(psrc);
2102  void *dst = NULL;
2103  int ret;
2104 
2105  if (*pdst == *psrc) {
2106  *pdst = NULL;
2107  *opt_array_pcount(pdst) = 0;
2108  }
2109 
2110  opt_free_array(o, pdst, opt_array_pcount(pdst));
2111 
2112  dst = av_calloc(nb_elems, opt_type_desc[TYPE_BASE(o->type)].size);
2113  if (!dst)
2114  return AVERROR(ENOMEM);
2115 
2116  for (unsigned i = 0; i < nb_elems; i++) {
2117  ret = opt_copy_elem(logctx, TYPE_BASE(o->type),
2118  opt_array_pelem(o, dst, i),
2119  opt_array_pelem(o, *(void**)psrc, i));
2120  if (ret < 0) {
2121  opt_free_array(o, &dst, &nb_elems);
2122  return ret;
2123  }
2124  }
2125 
2126  *pdst = dst;
2127  *opt_array_pcount(pdst) = nb_elems;
2128 
2129  return 0;
2130 }
2131 
2132 int av_opt_copy(void *dst, const void *src)
2133 {
2134  const AVOption *o = NULL;
2135  const AVClass *c;
2136  int ret = 0;
2137 
2138  if (!src)
2139  return AVERROR(EINVAL);
2140 
2141  c = *(AVClass **)src;
2142  if (!c || c != *(AVClass **)dst)
2143  return AVERROR(EINVAL);
2144 
2145  while ((o = av_opt_next(src, o))) {
2146  void *field_dst = (uint8_t *)dst + o->offset;
2147  void *field_src = (uint8_t *)src + o->offset;
2148 
2149  int err = (o->type & AV_OPT_TYPE_FLAG_ARRAY) ?
2150  opt_copy_array(dst, o, field_dst, field_src) :
2151  opt_copy_elem (dst, o->type, field_dst, field_src);
2152  if (err < 0)
2153  ret = err;
2154  }
2155  return ret;
2156 }
2157 
2158 int av_opt_get_array_size(void *obj, const char *name, int search_flags,
2159  unsigned int *out_val)
2160 {
2161  void *target_obj, *parray;
2162  const AVOption *o;
2163 
2164  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
2165  if (!o || !target_obj)
2166  return AVERROR_OPTION_NOT_FOUND;
2167  if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY))
2168  return AVERROR(EINVAL);
2169 
2170  parray = (uint8_t *)target_obj + o->offset;
2171  *out_val = *opt_array_pcount(parray);
2172 
2173  return 0;
2174 }
2175 
2176 int av_opt_get_array(void *obj, const char *name, int search_flags,
2177  unsigned int start_elem, unsigned int nb_elems,
2178  enum AVOptionType out_type, void *out_val)
2179 {
2180  const size_t elem_size_out = opt_type_desc[TYPE_BASE(out_type)].size;
2181 
2182  const AVOption *o;
2183  void *target_obj;
2184 
2185  const void *parray;
2186  unsigned array_size;
2187 
2188  int ret;
2189 
2190  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
2191  if (!o || !target_obj)
2192  return AVERROR_OPTION_NOT_FOUND;
2193  if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY) ||
2194  (out_type & AV_OPT_TYPE_FLAG_ARRAY))
2195  return AVERROR(EINVAL);
2196 
2197  parray = (uint8_t *)target_obj + o->offset;
2198  array_size = *opt_array_pcount(parray);
2199 
2200  if (start_elem >= array_size ||
2201  array_size - start_elem < nb_elems)
2202  return AVERROR(EINVAL);
2203 
2204  for (unsigned i = 0; i < nb_elems; i++) {
2205  const void *src = opt_array_pelem(o, *(void**)parray, start_elem + i);
2206  void *dst = (uint8_t*)out_val + i * elem_size_out;
2207 
2208  if (out_type == TYPE_BASE(o->type)) {
2209  ret = opt_copy_elem(obj, out_type, dst, src);
2210  if (ret < 0)
2211  goto fail;
2212  } else if (out_type == AV_OPT_TYPE_STRING) {
2213  uint8_t buf[128], *out = buf;
2214 
2215  ret = opt_get_elem(o, &out, sizeof(buf), src, search_flags);
2216  if (ret < 0)
2217  goto fail;
2218 
2219  if (out == buf) {
2220  out = av_strdup(buf);
2221  if (!out) {
2222  ret = AVERROR(ENOMEM);
2223  goto fail;
2224  }
2225  }
2226 
2227  *(uint8_t**)dst = out;
2228  } else if (out_type == AV_OPT_TYPE_INT64 ||
2229  out_type == AV_OPT_TYPE_DOUBLE ||
2230  out_type == AV_OPT_TYPE_RATIONAL) {
2231  double num = 1.0;
2232  int den = 1;
2233  int64_t intnum = 1;
2234 
2235  ret = read_number(o, src, &num, &den, &intnum);
2236  if (ret < 0)
2237  goto fail;
2238 
2239  switch (out_type) {
2240  case AV_OPT_TYPE_INT64:
2241  *(int64_t*)dst = (num == den) ? intnum : num * intnum / den;
2242  break;
2243  case AV_OPT_TYPE_DOUBLE:
2244  *(double*)dst = num * intnum / den;
2245  break;
2246  case AV_OPT_TYPE_RATIONAL:
2247  *(AVRational*)dst = (num == 1.0 && (int)intnum == intnum) ?
2248  (AVRational){ intnum, den } :
2249  double_to_rational(num * intnum / den);
2250  break;
2251  default: av_assert0(0);
2252  }
2253  } else
2254  return AVERROR(ENOSYS);
2255  }
2256 
2257  return 0;
2258 fail:
2259  for (unsigned i = 0; i < nb_elems; i++)
2260  opt_free_elem(out_type, (uint8_t*)out_val + i * elem_size_out);
2261  return ret;
2262 }
2263 
2264 int av_opt_set_array(void *obj, const char *name, int search_flags,
2265  unsigned int start_elem, unsigned int nb_elems,
2266  enum AVOptionType val_type, const void *val)
2267 {
2268  const size_t elem_size_val = opt_type_desc[TYPE_BASE(val_type)].size;
2269 
2270  const AVOption *o;
2271  const AVOptionArrayDef *arr;
2272  void *target_obj;
2273 
2274  void *parray;
2275  void *new_elems;
2276  unsigned *array_size, new_size;
2277  size_t elem_size;
2278 
2279  int ret = 0;
2280 
2281  ret = opt_set_init(obj, name, search_flags, 0, &target_obj, &o, &parray);
2282  if (ret < 0)
2283  return ret;
2284 
2285  if (!(o->type & AV_OPT_TYPE_FLAG_ARRAY) ||
2286  (val_type & AV_OPT_TYPE_FLAG_ARRAY))
2287  return AVERROR(EINVAL);
2288 
2289  arr = o->default_val.arr;
2290  array_size = opt_array_pcount(parray);
2291  elem_size = opt_type_desc[TYPE_BASE(o->type)].size;
2292 
2293  if (start_elem > *array_size)
2294  return AVERROR(EINVAL);
2295 
2296  // compute new array size
2297  if (!val) {
2298  if (*array_size - start_elem < nb_elems)
2299  return AVERROR(EINVAL);
2300 
2301  new_size = *array_size - nb_elems;
2302  } else if (search_flags & AV_OPT_ARRAY_REPLACE) {
2303  if (start_elem >= UINT_MAX - nb_elems)
2304  return AVERROR(EINVAL);
2305 
2306  new_size = FFMAX(*array_size, start_elem + nb_elems);
2307  } else {
2308  if (nb_elems >= UINT_MAX - *array_size)
2309  return AVERROR(EINVAL);
2310 
2311  new_size = *array_size + nb_elems;
2312  }
2313 
2314  if (arr &&
2315  ((arr->size_max && new_size > arr->size_max) ||
2316  (arr->size_min && new_size < arr->size_min)))
2317  return AVERROR(EINVAL);
2318 
2319  // desired operation is shrinking the array
2320  if (!val) {
2321  void *array = *(void**)parray;
2322 
2323  for (unsigned i = 0; i < nb_elems; i++) {
2324  opt_free_elem(o->type,
2325  opt_array_pelem(o, array, start_elem + i));
2326  }
2327 
2328  if (new_size > 0) {
2329  memmove(opt_array_pelem(o, array, start_elem),
2330  opt_array_pelem(o, array, start_elem + nb_elems),
2331  elem_size * (*array_size - start_elem - nb_elems));
2332 
2333  array = av_realloc_array(array, new_size, elem_size);
2334  if (!array)
2335  return AVERROR(ENOMEM);
2336 
2337  *(void**)parray = array;
2338  } else
2339  av_freep(parray);
2340 
2341  *array_size = new_size;
2342 
2343  return 0;
2344  }
2345 
2346  // otherwise, desired operation is insert/replace;
2347  // first, store new elements in a separate array to simplify
2348  // rollback on failure
2349  new_elems = av_calloc(nb_elems, elem_size);
2350  if (!new_elems)
2351  return AVERROR(ENOMEM);
2352 
2353  // convert/validate each new element
2354  for (unsigned i = 0; i < nb_elems; i++) {
2355  void *dst = opt_array_pelem(o, new_elems, i);
2356  const void *src = (uint8_t*)val + i * elem_size_val;
2357 
2358  double num = 1.0;
2359  int den = 1;
2360  int64_t intnum = 1;
2361 
2362  if (val_type == TYPE_BASE(o->type)) {
2363  int err;
2364 
2365  ret = opt_copy_elem(obj, val_type, dst, src);
2366  if (ret < 0)
2367  goto fail;
2368 
2369  // validate the range for numeric options
2370  err = read_number(o, dst, &num, &den, &intnum);
2371  if (err >= 0 && TYPE_BASE(o->type) != AV_OPT_TYPE_FLAGS &&
2372  (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
2373  num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
2374  av_log(obj, AV_LOG_ERROR, "Cannot set array element %u for "
2375  "parameter '%s': value %f out of range [%g - %g]\n",
2376  start_elem + i, o->name, num, o->min, o->max);
2377  ret = AVERROR(ERANGE);
2378  goto fail;
2379  }
2380  } else if (val_type == AV_OPT_TYPE_STRING) {
2381  ret = opt_set_elem(obj, target_obj, o, *(const char **)src, dst);
2382  if (ret < 0)
2383  goto fail;
2384  } else if (val_type == AV_OPT_TYPE_INT ||
2385  val_type == AV_OPT_TYPE_INT64 ||
2386  val_type == AV_OPT_TYPE_FLOAT ||
2387  val_type == AV_OPT_TYPE_DOUBLE ||
2388  val_type == AV_OPT_TYPE_RATIONAL) {
2389 
2390  switch (val_type) {
2391  case AV_OPT_TYPE_INT: intnum = *(int*)src; break;
2392  case AV_OPT_TYPE_INT64: intnum = *(int64_t*)src; break;
2393  case AV_OPT_TYPE_FLOAT: num = *(float*)src; break;
2394  case AV_OPT_TYPE_DOUBLE: num = *(double*)src; break;
2395  case AV_OPT_TYPE_RATIONAL: intnum = ((AVRational*)src)->num;
2396  den = ((AVRational*)src)->den; break;
2397  default: av_assert0(0);
2398  }
2399 
2400  ret = write_number(obj, o, dst, num, den, intnum);
2401  if (ret < 0)
2402  goto fail;
2403  } else {
2404  ret = AVERROR(ENOSYS);
2405  goto fail;
2406  }
2407  }
2408 
2409  // commit new elements to the array
2410  if (start_elem == 0 && nb_elems == new_size) {
2411  // replacing the existing array entirely
2412  opt_free_array(o, parray, array_size);
2413  *(void**)parray = new_elems;
2414  *array_size = nb_elems;
2415 
2416  new_elems = NULL;
2417  nb_elems = 0;
2418  } else {
2419  void *array = av_realloc_array(*(void**)parray, new_size, elem_size);
2420  if (!array) {
2421  ret = AVERROR(ENOMEM);
2422  goto fail;
2423  }
2424 
2425  if (search_flags & AV_OPT_ARRAY_REPLACE) {
2426  // free the elements being overwritten
2427  for (unsigned i = start_elem; i < FFMIN(start_elem + nb_elems, *array_size); i++)
2429  } else {
2430  // shift existing elements to the end
2431  memmove(opt_array_pelem(o, array, start_elem + nb_elems),
2432  opt_array_pelem(o, array, start_elem),
2433  elem_size * (*array_size - start_elem));
2434  }
2435 
2436  memcpy((uint8_t*)array + elem_size * start_elem, new_elems, elem_size * nb_elems);
2437 
2438  av_freep(&new_elems);
2439  nb_elems = 0;
2440 
2441  *(void**)parray = array;
2442  *array_size = new_size;
2443  }
2444 
2445 fail:
2446  opt_free_array(o, &new_elems, &nb_elems);
2447 
2448  return ret;
2449 }
2450 
2451 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
2452 {
2453  int ret;
2454  const AVClass *c = *(AVClass**)obj;
2455  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = c->query_ranges;
2456 
2457  if (!callback)
2459 
2460  ret = callback(ranges_arg, obj, key, flags);
2461  if (ret >= 0) {
2463  ret = 1;
2464  (*ranges_arg)->nb_components = ret;
2465  }
2466  return ret;
2467 }
2468 
2469 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
2470 {
2471  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
2472  AVOptionRange **range_array = av_mallocz(sizeof(void*));
2473  AVOptionRange *range = av_mallocz(sizeof(*range));
2474  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
2475  int ret;
2476 
2477  *ranges_arg = NULL;
2478 
2479  if (!ranges || !range || !range_array || !field) {
2480  ret = AVERROR(ENOMEM);
2481  goto fail;
2482  }
2483 
2484  ranges->range = range_array;
2485  ranges->range[0] = range;
2486  ranges->nb_ranges = 1;
2487  ranges->nb_components = 1;
2488  range->is_range = 1;
2489  range->value_min = field->min;
2490  range->value_max = field->max;
2491 
2492  switch (field->type) {
2493  case AV_OPT_TYPE_BOOL:
2494  case AV_OPT_TYPE_INT:
2495  case AV_OPT_TYPE_UINT:
2496  case AV_OPT_TYPE_INT64:
2497  case AV_OPT_TYPE_UINT64:
2498  case AV_OPT_TYPE_PIXEL_FMT:
2500  case AV_OPT_TYPE_FLOAT:
2501  case AV_OPT_TYPE_DOUBLE:
2502  case AV_OPT_TYPE_DURATION:
2503  case AV_OPT_TYPE_COLOR:
2504  break;
2505  case AV_OPT_TYPE_STRING:
2506  range->component_min = 0;
2507  range->component_max = 0x10FFFF; // max unicode value
2508  range->value_min = -1;
2509  range->value_max = INT_MAX;
2510  break;
2511  case AV_OPT_TYPE_RATIONAL:
2512  range->component_min = INT_MIN;
2513  range->component_max = INT_MAX;
2514  break;
2516  range->component_min = 0;
2517  range->component_max = INT_MAX/128/8;
2518  range->value_min = 0;
2519  range->value_max = INT_MAX/8;
2520  break;
2522  range->component_min = 1;
2523  range->component_max = INT_MAX;
2524  range->value_min = 1;
2525  range->value_max = INT_MAX;
2526  break;
2527  default:
2528  ret = AVERROR(ENOSYS);
2529  goto fail;
2530  }
2531 
2532  *ranges_arg = ranges;
2533  return 1;
2534 fail:
2535  av_free(ranges);
2536  av_free(range);
2537  av_free(range_array);
2538  return ret;
2539 }
2540 
2542 {
2543  int i;
2544  AVOptionRanges *ranges = *rangesp;
2545 
2546  if (!ranges)
2547  return;
2548 
2549  for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
2550  AVOptionRange *range = ranges->range[i];
2551  if (range) {
2552  av_freep(&range->str);
2553  av_freep(&ranges->range[i]);
2554  }
2555  }
2556  av_freep(&ranges->range);
2557  av_freep(rangesp);
2558 }
2559 
2560 int av_opt_is_set_to_default(void *obj, const AVOption *o)
2561 {
2562  int64_t i64;
2563  double d;
2564  AVRational q;
2565  int ret, w, h;
2566  char *str;
2567  void *dst;
2568 
2569  if (!o || !obj)
2570  return AVERROR(EINVAL);
2571 
2572  dst = ((uint8_t*)obj) + o->offset;
2573 
2574  if (o->type & AV_OPT_TYPE_FLAG_ARRAY) {
2575  const char *def = o->default_val.arr ? o->default_val.arr->def : NULL;
2576  uint8_t *val;
2577 
2578  ret = opt_get_array(o, dst, &val);
2579  if (ret < 0)
2580  return ret;
2581 
2582  if (!!val != !!def)
2583  ret = 0;
2584  else if (val)
2585  ret = !strcmp(val, def);
2586  else
2587  ret = 1;
2588 
2589  av_freep(&val);
2590 
2591  return ret;
2592  }
2593 
2594  switch (o->type) {
2595  case AV_OPT_TYPE_CONST:
2596  return 1;
2597  case AV_OPT_TYPE_BOOL:
2598  case AV_OPT_TYPE_FLAGS:
2599  case AV_OPT_TYPE_PIXEL_FMT:
2601  case AV_OPT_TYPE_INT:
2602  case AV_OPT_TYPE_UINT:
2603  case AV_OPT_TYPE_DURATION:
2604  case AV_OPT_TYPE_INT64:
2605  case AV_OPT_TYPE_UINT64:
2606  read_number(o, dst, NULL, NULL, &i64);
2607  return o->default_val.i64 == i64;
2608  case AV_OPT_TYPE_CHLAYOUT: {
2609  AVChannelLayout ch_layout = { 0 };
2610  if (o->default_val.str) {
2611  if ((ret = av_channel_layout_from_string(&ch_layout, o->default_val.str)) < 0)
2612  return ret;
2613  }
2614  ret = !av_channel_layout_compare((AVChannelLayout *)dst, &ch_layout);
2615  av_channel_layout_uninit(&ch_layout);
2616  return ret;
2617  }
2618  case AV_OPT_TYPE_STRING:
2619  str = *(char **)dst;
2620  if (str == o->default_val.str) //2 NULLs
2621  return 1;
2622  if (!str || !o->default_val.str) //1 NULL
2623  return 0;
2624  return !strcmp(str, o->default_val.str);
2625  case AV_OPT_TYPE_DOUBLE:
2626  d = *(double *)dst;
2627  return o->default_val.dbl == d;
2628  case AV_OPT_TYPE_FLOAT:
2629  d = *(float *)dst;
2630  return (float)o->default_val.dbl == d;
2631  case AV_OPT_TYPE_RATIONAL:
2632  q = av_d2q(o->default_val.dbl, INT_MAX);
2633  return !av_cmp_q(*(AVRational*)dst, q);
2634  case AV_OPT_TYPE_BINARY: {
2635  struct {
2636  uint8_t *data;
2637  int size;
2638  } tmp = {0};
2639  int opt_size = *(int *)((void **)dst + 1);
2640  void *opt_ptr = *(void **)dst;
2641  if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
2642  return 1;
2643  if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
2644  return 0;
2645  if (opt_size != strlen(o->default_val.str) / 2)
2646  return 0;
2647  ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
2648  if (!ret)
2649  ret = !memcmp(opt_ptr, tmp.data, tmp.size);
2650  av_free(tmp.data);
2651  return ret;
2652  }
2653  case AV_OPT_TYPE_DICT: {
2654  AVDictionary *dict1 = NULL;
2655  AVDictionary *dict2 = *(AVDictionary **)dst;
2656  const AVDictionaryEntry *en1 = NULL;
2657  const AVDictionaryEntry *en2 = NULL;
2658  ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
2659  if (ret < 0) {
2660  av_dict_free(&dict1);
2661  return ret;
2662  }
2663  do {
2664  en1 = av_dict_iterate(dict1, en1);
2665  en2 = av_dict_iterate(dict2, en2);
2666  } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
2667  av_dict_free(&dict1);
2668  return (!en1 && !en2);
2669  }
2671  if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
2672  w = h = 0;
2673  else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
2674  return ret;
2675  return (w == *(int *)dst) && (h == *((int *)dst+1));
2677  q = (AVRational){0, 0};
2678  if (o->default_val.str) {
2679  if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
2680  return ret;
2681  }
2682  return !av_cmp_q(*(AVRational*)dst, q);
2683  case AV_OPT_TYPE_COLOR: {
2684  uint8_t color[4] = {0, 0, 0, 0};
2685  if (o->default_val.str) {
2686  if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
2687  return ret;
2688  }
2689  return !memcmp(color, dst, sizeof(color));
2690  }
2691  default:
2692  av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2693  break;
2694  }
2695  return AVERROR_PATCHWELCOME;
2696 }
2697 
2698 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2699 {
2700  const AVOption *o;
2701  void *target;
2702  if (!obj)
2703  return AVERROR(EINVAL);
2704  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2705  if (!o)
2706  return AVERROR_OPTION_NOT_FOUND;
2707  return av_opt_is_set_to_default(target, o);
2708 }
2709 
2710 static int opt_serialize(void *obj, int opt_flags, int flags, int *cnt,
2711  AVBPrint *bprint, const char key_val_sep, const char pairs_sep)
2712 {
2713  const AVOption *o = NULL;
2714  void *child = NULL;
2715  uint8_t *buf;
2716  int ret;
2717  const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2718 
2720  while (child = av_opt_child_next(obj, child)) {
2721  ret = opt_serialize(child, opt_flags, flags, cnt, bprint,
2722  key_val_sep, pairs_sep);
2723  if (ret < 0)
2724  return ret;
2725  }
2726 
2727  while (o = av_opt_next(obj, o)) {
2728  if (o->type == AV_OPT_TYPE_CONST)
2729  continue;
2730  if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2731  continue;
2732  else if (((o->flags & opt_flags) != opt_flags))
2733  continue;
2735  continue;
2736  if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2737  av_bprint_finalize(bprint, NULL);
2738  return ret;
2739  }
2740  if (buf) {
2741  if ((*cnt)++)
2742  av_bprint_append_data(bprint, &pairs_sep, 1);
2743  av_bprint_escape(bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2744  av_bprint_append_data(bprint, &key_val_sep, 1);
2745  av_bprint_escape(bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2746  av_freep(&buf);
2747  }
2748  }
2749 
2750  return 0;
2751 }
2752 
2753 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2754  const char key_val_sep, const char pairs_sep)
2755 {
2756  AVBPrint bprint;
2757  int ret, cnt = 0;
2758 
2759  if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2760  pairs_sep == '\\' || key_val_sep == '\\') {
2761  av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2762  return AVERROR(EINVAL);
2763  }
2764 
2765  if (!obj || !buffer)
2766  return AVERROR(EINVAL);
2767 
2768  *buffer = NULL;
2770 
2771  ret = opt_serialize(obj, opt_flags, flags, &cnt, &bprint,
2772  key_val_sep, pairs_sep);
2773  if (ret < 0)
2774  return ret;
2775 
2776  ret = av_bprint_finalize(&bprint, buffer);
2777  if (ret < 0)
2778  return ret;
2779  return 0;
2780 }
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:604
flags
const SwsFlags flags[]
Definition: swscale.c:85
set_string_bool
static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
Definition: opt.c:548
OPT_EVAL_NUMBER
#define OPT_EVAL_NUMBER(name, opttype, vartype)
Definition: opt.c:838
av_opt_get_dict_val
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:1376
size
size_t size
Definition: opt.c:61
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
av_opt_set_image_size
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
Definition: opt.c:911
AVOption::i64
int64_t i64
Definition: opt.h:451
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
log_default
static void log_default(void *obj, void *av_log_obj, const AVOption *opt)
Definition: opt.c:1506
log_int_value
static void log_int_value(void *av_log_obj, int level, int64_t i)
Definition: opt.c:1405
level
uint8_t level
Definition: svq3.c:208
AVOptionRanges::nb_components
int nb_components
Number of components.
Definition: opt.h:546
INFINITY
#define INFINITY
Definition: mathematics.h:118
av_opt_set_defaults
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1671
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
av_opt_flag_is_set
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:1392
flag
int flag
Definition: cpu.c:40
AV_OPT_TYPE_SAMPLE_FMT
@ AV_OPT_TYPE_SAMPLE_FMT
Underlying C type is enum AVSampleFormat.
Definition: opt.h:310
get_pix_fmt
static int get_pix_fmt(const char *name)
Definition: opt.c:619
av_opt_child_class_iterate
const AVClass * av_opt_child_class_iterate(const AVClass *parent, void **iter)
Iterate over potential AVOptions-enabled children of parent.
Definition: opt.c:2043
out
static FILE * out
Definition: movenc.c:55
color
Definition: vf_paletteuse.c:513
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
AVOptionArrayDef::sep
char sep
Separator between array elements in string representations of this option, used by av_opt_set() and a...
Definition: opt.h:422
AVOptionType
AVOptionType
An option type determines:
Definition: opt.h:250
AVOptionArrayDef
May be set as default_val for AV_OPT_TYPE_FLAG_ARRAY options.
Definition: opt.h:394
set_string_sample_fmt
static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:635
av_parse_color
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:359
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition: opt.h:314
int64_t
long long int64_t
Definition: coverity.c:34
AVOption::arr
const AVOptionArrayDef * arr
Used for AV_OPT_TYPE_FLAG_ARRAY options.
Definition: opt.h:463
mask
int mask
Definition: mediacodecdec_common.c:154
get_opt_const_name
static const char * get_opt_const_name(void *obj, const char *unit, int64_t value)
Definition: opt.c:1455
AVOptionRanges::nb_ranges
int nb_ranges
Number of ranges per component.
Definition: opt.h:542
pixdesc.h
av_opt_set_double
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
Definition: opt.c:874
av_opt_set_from_string
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition: opt.c:1890
AVOption
AVOption.
Definition: opt.h:428
is_key_char
static int is_key_char(char c)
Definition: opt.c:1832
b
#define b
Definition: input.c:43
set_string_channel_layout
static int set_string_channel_layout(void *obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:659
data
const char data[16]
Definition: mxf.c:149
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition: opt.h:318
av_opt_find2
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:1990
AVOption::help
const char * help
short English help text
Definition: opt.h:435
float.h
AVOption::flags
int flags
A combination of AV_OPT_FLAG_*.
Definition: opt.h:471
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVClass::child_class_iterate
const struct AVClass *(* child_class_iterate)(void **iter)
Iterate over the AVClasses corresponding to potential AVOptions-enabled children.
Definition: log.h:167
mathematics.h
AVDictionary
Definition: dict.c:32
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
opt_copy_array
static int opt_copy_array(void *logctx, const AVOption *o, void **pdst, const void *const *psrc)
Definition: opt.c:2098
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition: opt.h:279
set_number
static int set_number(void *obj, const char *name, double num, int den, int64_t intnum, int search_flags, int require_type)
Definition: opt.c:855
opt_array_pcount
static unsigned * opt_array_pcount(const void *parray)
Definition: opt.c:122
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
av_opt_serialize
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
Definition: opt.c:2753
const_values
static const double const_values[]
Definition: eval.c:28
AV_PIX_FMT_NB
@ AV_PIX_FMT_NB
hardware decoding through openharmony
Definition: pixfmt.h:502
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
double_to_rational
static AVRational double_to_rational(double d)
Definition: opt.c:223
AV_OPT_SERIALIZE_SEARCH_CHILDREN
#define AV_OPT_SERIALIZE_SEARCH_CHILDREN
Serialize options in possible children of the given object.
Definition: opt.h:1093
AV_OPT_TYPE_BINARY
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition: opt.h:285
av_opt_is_set_to_default
int av_opt_is_set_to_default(void *obj, const AVOption *o)
Check if given option is set to its default value.
Definition: opt.c:2560
AVOption::offset
int offset
Native access only.
Definition: opt.h:443
av_opt_get_key_value
int av_opt_get_key_value(const char **ropts, const char *key_val_sep, const char *pairs_sep, unsigned flags, char **rkey, char **rval)
Extract a key-value pair from the beginning of a string.
Definition: opt.c:1868
get_bool_name
static const char * get_bool_name(int val)
Definition: opt.c:541
samplefmt.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1942
AVERROR_OPTION_NOT_FOUND
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:63
val
static double val(void *priv, double ch)
Definition: aeval.c:77
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
av_opt_set
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:824
AV_OPT_SERIALIZE_SKIP_DEFAULTS
#define AV_OPT_SERIALIZE_SKIP_DEFAULTS
Serialize options that are not set to default values only.
Definition: opt.h:1091
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:356
get_format
static int get_format(void *obj, const char *name, int search_flags, void *out_fmt, enum AVOptionType type, const char *desc)
Definition: opt.c:1328
opt_free_array
static void opt_free_array(const AVOption *o, void *parray, unsigned *count)
Definition: opt.c:148
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:654
class
#define class
Definition: math.h:25
float
float
Definition: af_crystalizer.c:122
AVOptionArrayDef::def
const char * def
Native access only.
Definition: opt.h:401
AV_OPT_ARRAY_REPLACE
#define AV_OPT_ARRAY_REPLACE
May be used with av_opt_set_array() to signal that new elements should replace the existing ones in t...
Definition: opt.h:624
s
#define s(width, name)
Definition: cbs_vp9.c:198
opt_copy_elem
static int opt_copy_elem(void *logctx, enum AVOptionType type, void *dst, const void *src)
Definition: opt.c:2050
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
AVDictionaryEntry::key
char * key
Definition: dict.h:91
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:266
AV_OPT_FLAG_CHILD_CONSTS
#define AV_OPT_FLAG_CHILD_CONSTS
Set if option constants can also reside in child objects.
Definition: opt.h:389
av_opt_set_dict_val
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:973
av_opt_set_pixel_fmt
int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
Definition: opt.c:963
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:262
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
av_set_options_string
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:1810
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
av_opt_get_array_size
int av_opt_get_array_size(void *obj, const char *name, int search_flags, unsigned int *out_val)
For an array-type option, get the number of elements in the array.
Definition: opt.c:2158
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
WHITESPACES
#define WHITESPACES
Definition: opt.c:1830
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
av_opt_set_array
int av_opt_set_array(void *obj, const char *name, int search_flags, unsigned int start_elem, unsigned int nb_elems, enum AVOptionType val_type, const void *val)
Add, replace, or remove elements for an array option.
Definition: opt.c:2264
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:51
key
const char * key
Definition: hwcontext_opencl.c:189
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
NAN
#define NAN
Definition: mathematics.h:115
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AV_OPT_FLAG_BSF_PARAM
#define AV_OPT_FLAG_BSF_PARAM
A generic parameter which can be set by the user for bit stream filtering.
Definition: opt.h:371
av_opt_get_pixel_fmt
int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
Definition: opt.c:1350
callback
static void callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
Definition: dshow.c:342
av_opt_get_video_rate
int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:1323
if
if(ret)
Definition: filter_design.txt:179
fail
#define fail
Definition: test.h:478
AVOptionArrayDef::size_max
unsigned size_max
Maximum number of elements in the array, 0 when unlimited.
Definition: opt.h:411
opts
static AVDictionary * opts
Definition: movenc.c:51
log_type
static void log_type(void *av_log_obj, const AVOption *o, enum AVOptionType parent_type)
Definition: opt.c:1489
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
av_opt_set_video_rate
int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:934
NULL
#define NULL
Definition: coverity.c:32
av_opt_set_bin
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:884
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
opt_set_elem
static int opt_set_elem(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:669
log_value
static void log_value(void *av_log_obj, int level, double d)
Definition: opt.c:1422
av_bprint_escape
void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape the content in src and append it to dstbuf.
Definition: bprint.c:263
av_opt_get_double
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
Definition: opt.c:1277
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
set_format
static int set_format(void *obj, const char *name, int fmt, int search_flags, enum AVOptionType type, const char *desc, int nb_fmts)
Definition: opt.c:939
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition: opt.h:322
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:302
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition: opt.h:289
parseutils.h
options
Definition: swscale.c:50
av_opt_get_sample_fmt
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
Definition: opt.c:1355
AV_CLASS_STATE_INITIALIZED
@ AV_CLASS_STATE_INITIALIZED
Object initialization has finished and it is now in the 'runtime' stage.
Definition: log.h:56
AV_OPT_SEARCH_FAKE_OBJ
#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 poin...
Definition: opt.h:612
double
double
Definition: af_crystalizer.c:132
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:592
AVOptionRanges::range
AVOptionRange ** range
Array of option ranges.
Definition: opt.h:538
opt_list
static void opt_list(void *obj, void *av_log_obj, const char *unit, int req_flags, int rej_flags, enum AVOptionType parent_type)
Definition: opt.c:1584
AVOption::min
double min
minimum valid value for the option
Definition: opt.h:465
av_opt_get_array
int av_opt_get_array(void *obj, const char *name, int search_flags, unsigned int start_elem, unsigned int nb_elems, enum AVOptionType out_type, void *out_val)
For an array-type option, retrieve the values of one or more array elements.
Definition: opt.c:2176
AV_OPT_TYPE_UINT
@ AV_OPT_TYPE_UINT
Underlying C type is unsigned int.
Definition: opt.h:334
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition: opt.h:330
av_opt_get_int
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:1262
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:351
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:869
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:2132
opt_is_pod
static int opt_is_pod(enum AVOptionType type)
Definition: opt.c:86
eval.h
AV_OPT_FLAG_FILTERING_PARAM
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition: opt.h:380
av_opt_find
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:1984
AVOptionRange
A single allowed range of values, or a single allowed value.
Definition: opt.h:484
AV_SAMPLE_FMT_NB
@ AV_SAMPLE_FMT_NB
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:71
av_expr_parse_and_eval
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:839
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
AV_OPT_TYPE_FLAG_ARRAY
@ AV_OPT_TYPE_FLAG_ARRAY
May be combined with another regular option type to declare an array option.
Definition: opt.h:345
av_opt_set_chlayout
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition: opt.c:989
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:188
set_string_fmt
static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst, int fmt_nb, int((*get_fmt)(const char *)), const char *desc)
Definition: opt.c:579
read_number
static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
Definition: opt.c:231
parse_key_value_pair
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:1776
hexchar2int
static int hexchar2int(char c)
Definition: opt.c:347
AVOption::name
const char * name
Definition: opt.h:429
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AVOption::default_val
union AVOption::@551 default_val
Native access only, except when documented otherwise.
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:233
av_opt_show2
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:1659
av_parse_video_size
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:150
set_string_video_rate
static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
Definition: opt.c:518
get_sample_fmt
static int get_sample_fmt(const char *name)
Definition: opt.c:630
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:811
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
DEFAULT_NUMVAL
#define DEFAULT_NUMVAL(opt)
Definition: opt.c:400
opt_array_pelem
static void * opt_array_pelem(const AVOption *o, void *array, unsigned idx)
Definition: opt.c:116
get_number
static int get_number(void *obj, const char *name, double *num, int *den, int64_t *intnum, int search_flags)
Definition: opt.c:1247
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:270
AV_OPT_FLAG_READONLY
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:367
av_parse_video_rate
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:181
AV_OPT_FLAG_DEPRECATED
#define AV_OPT_FLAG_DEPRECATED
Set if option is deprecated, users should refer to AVOption.help text for more information.
Definition: opt.h:385
av_opt_next
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:47
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:313
bprint.h
log.h
av_malloc
#define av_malloc(s)
Definition: ops_asmgen.c:44
set_string_pixel_fmt
static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:624
av_opt_set_dict2
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
Definition: opt.c:1955
av_get_sample_fmt
enum AVSampleFormat av_get_sample_fmt(const char *name)
Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE on error.
Definition: samplefmt.c:58
AV_OPT_FLAG_SUBTITLE_PARAM
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:358
get_opt_flags_string
static char * get_opt_flags_string(void *obj, const char *unit, int64_t value)
Definition: opt.c:1468
AV_OPT_FLAG_IMPLICIT_KEY
@ AV_OPT_FLAG_IMPLICIT_KEY
Accept to parse a value without a key; the key will then be returned as NULL.
Definition: opt.h:723
AVOption::str
const char * str
Definition: opt.h:453
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
class_name
class_name
Definition: libkvazaar.c:310
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:110
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:357
set_string
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:391
AV_OPT_MULTI_COMPONENT_RANGE
#define AV_OPT_MULTI_COMPONENT_RANGE
Allows av_opt_query_ranges and av_opt_query_ranges_default to return more than one component for cert...
Definition: opt.h:631
len
int len
Definition: vorbis_enc_data.h:426
AVOptionRanges
List of AVOptionRange structs.
Definition: opt.h:507
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
get_key
static int get_key(const char **ropts, const char *delim, char **rkey)
Read a key from a string.
Definition: opt.c:1847
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:116
ret
ret
Definition: filter_design.txt:187
AVOption::dbl
double dbl
Definition: opt.h:452
set_string_color
static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:526
dict.h
AVOption::type
enum AVOptionType type
Definition: opt.h:444
const_names
static const char *const const_names[]
Definition: eval.c:34
set_string_dict
static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:641
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:3388
TYPE_BASE
#define TYPE_BASE(type)
Definition: opt.c:45
opt_array_sep
static uint8_t opt_array_sep(const AVOption *o)
Definition: opt.c:109
set_string_binary
static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:357
opt_serialize
static int opt_serialize(void *obj, int opt_flags, int flags, int *cnt, AVBPrint *bprint, const char key_val_sep, const char pairs_sep)
Definition: opt.c:2710
channel_layout.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_FLAG_RUNTIME_PARAM
#define AV_OPT_FLAG_RUNTIME_PARAM
A generic parameter which can be set by the user at runtime.
Definition: opt.h:376
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
av_get_token
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:143
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:443
av_match_name
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition: avstring.c:346
av_dict_parse_string
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:210
av_opt_get_chlayout
int av_opt_get_chlayout(void *obj, const char *name, int search_flags, AVChannelLayout *cl)
Definition: opt.c:1360
AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
#define AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
Serialize options that exactly match opt_flags only.
Definition: opt.h:1092
av_opt_child_next
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
Definition: opt.c:2035
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AV_OPT_TYPE_PIXEL_FMT
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition: opt.h:306
av_opt_set_defaults2
void av_opt_set_defaults2(void *s, int mask, int flags)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1676
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:450
av_opt_query_ranges_default
int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
Get a default list of allowed ranges for the given option.
Definition: opt.c:2469
av_opt_query_ranges
int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
Get a list of allowed ranges for the given option.
Definition: opt.c:2451
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition: opt.h:355
desc
const char * desc
Definition: libsvtav1.c:83
AVOptionArrayDef::size_min
unsigned size_min
Minimum number of elements in the array.
Definition: opt.h:407
avutil.h
mem.h
av_strdup
#define av_strdup(s)
Definition: ops_asmgen.c:47
AV_OPT_ALLOW_NULL
#define AV_OPT_ALLOW_NULL
In av_opt_get, return NULL if the option has a pointer type and is set to NULL, rather than returning...
Definition: opt.h:618
AVOption::unit
const char * unit
The logical unit to which the option belongs.
Definition: opt.h:478
llrint
#define llrint(x)
Definition: libm.h:396
av_opt_freep_ranges
void av_opt_freep_ranges(AVOptionRanges **rangesp)
Free an AVOptionRanges struct and set it to NULL.
Definition: opt.c:2541
w
uint8_t w
Definition: llvidencdsp.c:39
set_string_number
static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:409
opt_set_array
static int opt_set_array(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:745
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVDictionaryEntry
Definition: dict.h:90
AV_OPT_FLAG_EXPORT
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition: opt.h:362
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:326
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
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:86
av_dict_get_string
int av_dict_get_string(const AVDictionary *m, char **buffer, const char key_val_sep, const char pairs_sep)
Get dictionary entries as a string.
Definition: dict.c:260
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:247
format_duration
static void format_duration(char *buf, size_t size, int64_t d)
Definition: opt.c:1004
AV_ESCAPE_MODE_BACKSLASH
@ AV_ESCAPE_MODE_BACKSLASH
Use backslash escaping.
Definition: avstring.h:316
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition: opt.h:254
set_string_image_size
static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
Definition: opt.c:503
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:1204
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
name
const char * name
Definition: opt.c:62
h
h
Definition: vp9dsp_template.c:2070
av_opt_set_dict
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1979
AVDictionaryEntry::value
char * value
Definition: dict.h:92
av_opt_get_image_size
int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
Definition: opt.c:1305
avstring.h
opt_type_desc
static const struct @549 opt_type_desc[]
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:275
av_opt_set_q
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:879
av_opt_set_sample_fmt
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:968
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:148
AVOption::max
double max
maximum valid value for the option
Definition: opt.h:466
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:298
snprintf
#define snprintf
Definition: snprintf.h:34
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:42
src
#define src
Definition: vp8dsp.c:248
opt_free_elem
static void opt_free_elem(enum AVOptionType type, void *ptr)
Definition: opt.c:127
av_opt_is_set_to_default_by_name
int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
Check if given option is set to its default value.
Definition: opt.c:2698
opt_set_init
static int opt_set_init(void *obj, const char *name, int search_flags, int require_type, void **ptgt, const AVOption **po, void **pdst)
Perform common setup for option-setting functions.
Definition: opt.c:165
opt_get_elem
static int opt_get_elem(const AVOption *o, uint8_t **pbuf, size_t buf_len, const void *dst, int search_flags)
Definition: opt.c:1039
av_x_if_null
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:311
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3376
opt_get_array
static int opt_get_array(const AVOption *o, void *dst, uint8_t **out_val)
Definition: opt.c:1144
min
float min
Definition: vorbis_enc_data.h:429
write_number
static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
Definition: opt.c:272
AV_OPT_TYPE_UINT64
@ AV_OPT_TYPE_UINT64
Underlying C type is uint64_t.
Definition: opt.h:293
av_opt_get_q
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:1289