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