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