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