FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "common.h"
33 #include "opt.h"
34 #include "eval.h"
35 #include "dict.h"
36 #include "log.h"
37 #include "parseutils.h"
38 #include "pixdesc.h"
39 #include "mathematics.h"
40 #include "samplefmt.h"
41 #include "bprint.h"
42 
43 #include <float.h>
44 
45 const AVOption *av_opt_next(const void *obj, const AVOption *last)
46 {
47  const AVClass *class;
48  if (!obj)
49  return NULL;
50  class = *(const AVClass**)obj;
51  if (!last && class && class->option && class->option[0].name)
52  return class->option;
53  if (last && last[1].name)
54  return ++last;
55  return NULL;
56 }
57 
58 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
59 {
60  switch (o->type) {
61  case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
62  case AV_OPT_TYPE_PIXEL_FMT: *intnum = *(enum AVPixelFormat *)dst;return 0;
63  case AV_OPT_TYPE_SAMPLE_FMT:*intnum = *(enum AVSampleFormat*)dst;return 0;
64  case AV_OPT_TYPE_BOOL:
65  case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
68  case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
69  case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
70  case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
71  case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
72  *den = ((AVRational*)dst)->den;
73  return 0;
74  case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
75  }
76  return AVERROR(EINVAL);
77 }
78 
79 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
80 {
81  if (o->type != AV_OPT_TYPE_FLAGS &&
82  (o->max * den < num * intnum || o->min * den > num * intnum)) {
83  num = den ? num*intnum/den : (num*intnum ? INFINITY : NAN);
84  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
85  num, o->name, o->min, o->max);
86  return AVERROR(ERANGE);
87  }
88  if (o->type == AV_OPT_TYPE_FLAGS) {
89  double d = num*intnum/den;
90  if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
91  av_log(obj, AV_LOG_ERROR,
92  "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
93  num*intnum/den, o->name);
94  return AVERROR(ERANGE);
95  }
96  }
97 
98  switch (o->type) {
99  case AV_OPT_TYPE_PIXEL_FMT: *(enum AVPixelFormat *)dst = llrint(num/den) * intnum; break;
100  case AV_OPT_TYPE_SAMPLE_FMT:*(enum AVSampleFormat*)dst = llrint(num/den) * intnum; break;
101  case AV_OPT_TYPE_BOOL:
102  case AV_OPT_TYPE_FLAGS:
103  case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
106  case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
107  case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
108  case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
110  if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
111  else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
112  break;
113  default:
114  return AVERROR(EINVAL);
115  }
116  return 0;
117 }
118 
119 static int hexchar2int(char c) {
120  if (c >= '0' && c <= '9') return c - '0';
121  if (c >= 'a' && c <= 'f') return c - 'a' + 10;
122  if (c >= 'A' && c <= 'F') return c - 'A' + 10;
123  return -1;
124 }
125 
126 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
127 {
128  int *lendst = (int *)(dst + 1);
129  uint8_t *bin, *ptr;
130  int len;
131 
132  av_freep(dst);
133  *lendst = 0;
134 
135  if (!val || !(len = strlen(val)))
136  return 0;
137 
138  if (len & 1)
139  return AVERROR(EINVAL);
140  len /= 2;
141 
142  ptr = bin = av_malloc(len);
143  if (!ptr)
144  return AVERROR(ENOMEM);
145  while (*val) {
146  int a = hexchar2int(*val++);
147  int b = hexchar2int(*val++);
148  if (a < 0 || b < 0) {
149  av_free(bin);
150  return AVERROR(EINVAL);
151  }
152  *ptr++ = (a << 4) | b;
153  }
154  *dst = bin;
155  *lendst = len;
156 
157  return 0;
158 }
159 
160 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
161 {
162  av_freep(dst);
163  *dst = av_strdup(val);
164  return *dst ? 0 : AVERROR(ENOMEM);
165 }
166 
167 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
168  opt->type == AV_OPT_TYPE_CONST || \
169  opt->type == AV_OPT_TYPE_FLAGS || \
170  opt->type == AV_OPT_TYPE_INT) ? \
171  opt->default_val.i64 : opt->default_val.dbl)
172 
173 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
174 {
175  int ret = 0;
176  int num, den;
177  char c;
178 
179  if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
180  if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
181  return ret;
182  ret = 0;
183  }
184 
185  for (;;) {
186  int i = 0;
187  char buf[256];
188  int cmd = 0;
189  double d;
190  int64_t intnum = 1;
191 
192  if (o->type == AV_OPT_TYPE_FLAGS) {
193  if (*val == '+' || *val == '-')
194  cmd = *(val++);
195  for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
196  buf[i] = val[i];
197  buf[i] = 0;
198  }
199 
200  {
201  const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
202  int res;
203  int ci = 0;
204  double const_values[64];
205  const char * const_names[64];
206  if (o_named && o_named->type == AV_OPT_TYPE_CONST)
207  d = DEFAULT_NUMVAL(o_named);
208  else {
209  if (o->unit) {
210  for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
211  if (o_named->type == AV_OPT_TYPE_CONST &&
212  o_named->unit &&
213  !strcmp(o_named->unit, o->unit)) {
214  if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
215  av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
216  return AVERROR_PATCHWELCOME;
217  }
218  const_names [ci ] = o_named->name;
219  const_values[ci++] = DEFAULT_NUMVAL(o_named);
220  }
221  }
222  }
223  const_names [ci ] = "default";
224  const_values[ci++] = DEFAULT_NUMVAL(o);
225  const_names [ci ] = "max";
226  const_values[ci++] = o->max;
227  const_names [ci ] = "min";
228  const_values[ci++] = o->min;
229  const_names [ci ] = "none";
230  const_values[ci++] = 0;
231  const_names [ci ] = "all";
232  const_values[ci++] = ~0;
233  const_names [ci] = NULL;
234  const_values[ci] = 0;
235 
236  res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
237  const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
238  if (res < 0) {
239  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
240  return res;
241  }
242  }
243  }
244  if (o->type == AV_OPT_TYPE_FLAGS) {
245  read_number(o, dst, NULL, NULL, &intnum);
246  if (cmd == '+') d = intnum | (int64_t)d;
247  else if (cmd == '-') d = intnum &~(int64_t)d;
248  }
249 
250  if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
251  return ret;
252  val += i;
253  if (!i || !*val)
254  return 0;
255  }
256 
257  return 0;
258 }
259 
260 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
261 {
262  int ret;
263 
264  if (!val || !strcmp(val, "none")) {
265  dst[0] =
266  dst[1] = 0;
267  return 0;
268  }
269  ret = av_parse_video_size(dst, dst + 1, val);
270  if (ret < 0)
271  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
272  return ret;
273 }
274 
275 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
276 {
277  int ret;
278  if (!val) {
279  ret = AVERROR(EINVAL);
280  } else {
281  ret = av_parse_video_rate(dst, val);
282  }
283  if (ret < 0)
284  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
285  return ret;
286 }
287 
288 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
289 {
290  int ret;
291 
292  if (!val) {
293  return 0;
294  } else {
295  ret = av_parse_color(dst, val, -1, obj);
296  if (ret < 0)
297  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
298  return ret;
299  }
300  return 0;
301 }
302 
303 static const char *get_bool_name(int val)
304 {
305  if (val < 0)
306  return "auto";
307  return val ? "true" : "false";
308 }
309 
310 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
311 {
312  int n;
313 
314  if (!val)
315  return 0;
316 
317  if (!strcmp(val, "auto")) {
318  n = -1;
319  } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
320  n = 1;
321  } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
322  n = 0;
323  } else {
324  char *end = NULL;
325  n = strtol(val, &end, 10);
326  if (val + strlen(val) != end)
327  goto fail;
328  }
329 
330  if (n < o->min || n > o->max)
331  goto fail;
332 
333  *dst = n;
334  return 0;
335 
336 fail:
337  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
338  return AVERROR(EINVAL);
339 }
340 
341 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
342  int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
343 {
344  int fmt, min, max;
345 
346  if (!val || !strcmp(val, "none")) {
347  fmt = -1;
348  } else {
349  fmt = get_fmt(val);
350  if (fmt == -1) {
351  char *tail;
352  fmt = strtol(val, &tail, 0);
353  if (*tail || (unsigned)fmt >= fmt_nb) {
354  av_log(obj, AV_LOG_ERROR,
355  "Unable to parse option value \"%s\" as %s\n", val, desc);
356  return AVERROR(EINVAL);
357  }
358  }
359  }
360 
361  min = FFMAX(o->min, -1);
362  max = FFMIN(o->max, fmt_nb-1);
363 
364  // hack for compatibility with old ffmpeg
365  if(min == 0 && max == 0) {
366  min = -1;
367  max = fmt_nb-1;
368  }
369 
370  if (fmt < min || fmt > max) {
371  av_log(obj, AV_LOG_ERROR,
372  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
373  fmt, o->name, desc, min, max);
374  return AVERROR(ERANGE);
375  }
376 
377  *(int *)dst = fmt;
378  return 0;
379 }
380 
381 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
382 {
383  return set_string_fmt(obj, o, val, dst,
384  AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
385 }
386 
387 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
388 {
389  return set_string_fmt(obj, o, val, dst,
390  AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
391 }
392 
393 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
394 {
395  int ret = 0;
396  void *dst, *target_obj;
397  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
398  if (!o || !target_obj)
400  if (!val && (o->type != AV_OPT_TYPE_STRING &&
405  return AVERROR(EINVAL);
406 
407  if (o->flags & AV_OPT_FLAG_READONLY)
408  return AVERROR(EINVAL);
409 
410  dst = ((uint8_t*)target_obj) + o->offset;
411  switch (o->type) {
412  case AV_OPT_TYPE_BOOL: return set_string_bool(obj, o, val, dst);
413  case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
414  case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
415  case AV_OPT_TYPE_FLAGS:
416  case AV_OPT_TYPE_INT:
417  case AV_OPT_TYPE_INT64:
418  case AV_OPT_TYPE_FLOAT:
419  case AV_OPT_TYPE_DOUBLE:
420  case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
421  case AV_OPT_TYPE_IMAGE_SIZE: return set_string_image_size(obj, o, val, dst);
422  case AV_OPT_TYPE_VIDEO_RATE: return set_string_video_rate(obj, o, val, dst);
423  case AV_OPT_TYPE_PIXEL_FMT: return set_string_pixel_fmt(obj, o, val, dst);
424  case AV_OPT_TYPE_SAMPLE_FMT: return set_string_sample_fmt(obj, o, val, dst);
426  if (!val) {
427  *(int64_t *)dst = 0;
428  return 0;
429  } else {
430  if ((ret = av_parse_time(dst, val, 1)) < 0)
431  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
432  return ret;
433  }
434  break;
435  case AV_OPT_TYPE_COLOR: return set_string_color(obj, o, val, dst);
437  if (!val || !strcmp(val, "none")) {
438  *(int64_t *)dst = 0;
439  } else {
440  int64_t cl = av_get_channel_layout(val);
441  if (!cl) {
442  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
443  ret = AVERROR(EINVAL);
444  }
445  *(int64_t *)dst = cl;
446  return ret;
447  }
448  break;
449  }
450 
451  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
452  return AVERROR(EINVAL);
453 }
454 
455 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
456  int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
457  {\
458  if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
459  return AVERROR(EINVAL);\
460  return set_string_number(obj, obj, o, val, name ## _out);\
461  }
462 
465 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
466 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
467 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
469 
470 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
471  int search_flags)
472 {
473  void *dst, *target_obj;
474  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
475 
476  if (!o || !target_obj)
478 
479  if (o->flags & AV_OPT_FLAG_READONLY)
480  return AVERROR(EINVAL);
481 
482  dst = ((uint8_t*)target_obj) + o->offset;
483  return write_number(obj, o, dst, num, den, intnum);
484 }
485 
486 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
487 {
488  return set_number(obj, name, 1, 1, val, search_flags);
489 }
490 
491 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
492 {
493  return set_number(obj, name, val, 1, 1, search_flags);
494 }
495 
496 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
497 {
498  return set_number(obj, name, val.num, val.den, 1, search_flags);
499 }
500 
501 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
502 {
503  void *target_obj;
504  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
505  uint8_t *ptr;
506  uint8_t **dst;
507  int *lendst;
508 
509  if (!o || !target_obj)
511 
513  return AVERROR(EINVAL);
514 
515  ptr = len ? av_malloc(len) : NULL;
516  if (len && !ptr)
517  return AVERROR(ENOMEM);
518 
519  dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
520  lendst = (int *)(dst + 1);
521 
522  av_free(*dst);
523  *dst = ptr;
524  *lendst = len;
525  if (len)
526  memcpy(ptr, val, len);
527 
528  return 0;
529 }
530 
531 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
532 {
533  void *target_obj;
534  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
535 
536  if (!o || !target_obj)
538  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
539  av_log(obj, AV_LOG_ERROR,
540  "The value set by option '%s' is not an image size.\n", o->name);
541  return AVERROR(EINVAL);
542  }
543  if (w<0 || h<0) {
544  av_log(obj, AV_LOG_ERROR,
545  "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
546  return AVERROR(EINVAL);
547  }
548  *(int *)(((uint8_t *)target_obj) + o->offset) = w;
549  *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
550  return 0;
551 }
552 
553 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
554 {
555  void *target_obj;
556  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
557 
558  if (!o || !target_obj)
560  if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
561  av_log(obj, AV_LOG_ERROR,
562  "The value set by option '%s' is not a video rate.\n", o->name);
563  return AVERROR(EINVAL);
564  }
565  if (val.num <= 0 || val.den <= 0)
566  return AVERROR(EINVAL);
567  return set_number(obj, name, val.num, val.den, 1, search_flags);
568 }
569 
570 static int set_format(void *obj, const char *name, int fmt, int search_flags,
571  enum AVOptionType type, const char *desc, int nb_fmts)
572 {
573  void *target_obj;
574  const AVOption *o = av_opt_find2(obj, name, NULL, 0,
575  search_flags, &target_obj);
576  int min, max;
577 
578  if (!o || !target_obj)
580  if (o->type != type) {
581  av_log(obj, AV_LOG_ERROR,
582  "The value set by option '%s' is not a %s format", name, desc);
583  return AVERROR(EINVAL);
584  }
585 
586  min = FFMAX(o->min, -1);
587  max = FFMIN(o->max, nb_fmts-1);
588 
589  if (fmt < min || fmt > max) {
590  av_log(obj, AV_LOG_ERROR,
591  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
592  fmt, name, desc, min, max);
593  return AVERROR(ERANGE);
594  }
595  *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
596  return 0;
597 }
598 
599 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
600 {
601  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
602 }
603 
604 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
605 {
606  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
607 }
608 
609 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
610 {
611  void *target_obj;
612  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
613 
614  if (!o || !target_obj)
616  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
617  av_log(obj, AV_LOG_ERROR,
618  "The value set by option '%s' is not a channel layout.\n", o->name);
619  return AVERROR(EINVAL);
620  }
621  *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
622  return 0;
623 }
624 
625 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
626 {
627  void *target_obj;
628  AVDictionary **dst;
629  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
630 
631  if (!o || !target_obj)
633  if (o->flags & AV_OPT_FLAG_READONLY)
634  return AVERROR(EINVAL);
635 
636  dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
637  av_dict_free(dst);
638  av_dict_copy(dst, val, 0);
639 
640  return 0;
641 }
642 
643 static void format_duration(char *buf, size_t size, int64_t d)
644 {
645  char *e;
646 
647  av_assert0(size >= 25);
648  if (d < 0 && d != INT64_MIN) {
649  *(buf++) = '-';
650  size--;
651  d = -d;
652  }
653  if (d == INT64_MAX)
654  snprintf(buf, size, "INT64_MAX");
655  else if (d == INT64_MIN)
656  snprintf(buf, size, "INT64_MIN");
657  else if (d > (int64_t)3600*1000000)
658  snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
659  (int)((d / 60000000) % 60),
660  (int)((d / 1000000) % 60),
661  (int)(d % 1000000));
662  else if (d > 60*1000000)
663  snprintf(buf, size, "%d:%02d.%06d",
664  (int)(d / 60000000),
665  (int)((d / 1000000) % 60),
666  (int)(d % 1000000));
667  else
668  snprintf(buf, size, "%d.%06d",
669  (int)(d / 1000000),
670  (int)(d % 1000000));
671  e = buf + strlen(buf);
672  while (e > buf && e[-1] == '0')
673  *(--e) = 0;
674  if (e > buf && e[-1] == '.')
675  *(--e) = 0;
676 }
677 
678 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
679 {
680  void *dst, *target_obj;
681  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
682  uint8_t *bin, buf[128];
683  int len, i, ret;
684  int64_t i64;
685 
686  if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
688 
689  dst = (uint8_t*)target_obj + o->offset;
690 
691  buf[0] = 0;
692  switch (o->type) {
693  case AV_OPT_TYPE_BOOL:
694  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
695  break;
696  case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
697  case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
698  case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
699  case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
700  case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
702  case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
703  case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
704  case AV_OPT_TYPE_STRING:
705  if (*(uint8_t**)dst) {
706  *out_val = av_strdup(*(uint8_t**)dst);
707  } else if (search_flags & AV_OPT_ALLOW_NULL) {
708  *out_val = NULL;
709  return 0;
710  } else {
711  *out_val = av_strdup("");
712  }
713  return *out_val ? 0 : AVERROR(ENOMEM);
714  case AV_OPT_TYPE_BINARY:
715  if (!*(uint8_t**)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
716  *out_val = NULL;
717  return 0;
718  }
719  len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
720  if ((uint64_t)len*2 + 1 > INT_MAX)
721  return AVERROR(EINVAL);
722  if (!(*out_val = av_malloc(len*2 + 1)))
723  return AVERROR(ENOMEM);
724  if (!len) {
725  *out_val[0] = '\0';
726  return 0;
727  }
728  bin = *(uint8_t**)dst;
729  for (i = 0; i < len; i++)
730  snprintf(*out_val + i*2, 3, "%02X", bin[i]);
731  return 0;
733  ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
734  break;
736  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
737  break;
739  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
740  break;
742  i64 = *(int64_t *)dst;
743  format_duration(buf, sizeof(buf), i64);
744  ret = strlen(buf); // no overflow possible, checked by an assert
745  break;
746  case AV_OPT_TYPE_COLOR:
747  ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
748  (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
749  (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
750  break;
752  i64 = *(int64_t *)dst;
753  ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
754  break;
755  default:
756  return AVERROR(EINVAL);
757  }
758 
759  if (ret >= sizeof(buf))
760  return AVERROR(EINVAL);
761  *out_val = av_strdup(buf);
762  return *out_val ? 0 : AVERROR(ENOMEM);
763 }
764 
765 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
766  int search_flags)
767 {
768  void *dst, *target_obj;
769  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
770  if (!o || !target_obj)
771  goto error;
772 
773  dst = ((uint8_t*)target_obj) + o->offset;
774 
775  if (o_out) *o_out= o;
776 
777  return read_number(o, dst, num, den, intnum);
778 
779 error:
780  *den=*intnum=0;
781  return -1;
782 }
783 
784 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
785 {
786  int64_t intnum = 1;
787  double num = 1;
788  int ret, den = 1;
789 
790  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
791  return ret;
792  *out_val = num*intnum/den;
793  return 0;
794 }
795 
796 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
797 {
798  int64_t intnum = 1;
799  double num = 1;
800  int ret, den = 1;
801 
802  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
803  return ret;
804  *out_val = num*intnum/den;
805  return 0;
806 }
807 
808 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
809 {
810  int64_t intnum = 1;
811  double num = 1;
812  int ret, den = 1;
813 
814  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
815  return ret;
816 
817  if (num == 1.0 && (int)intnum == intnum)
818  *out_val = (AVRational){intnum, den};
819  else
820  *out_val = av_d2q(num*intnum/den, 1<<24);
821  return 0;
822 }
823 
824 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
825 {
826  void *dst, *target_obj;
827  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
828  if (!o || !target_obj)
830  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
831  av_log(obj, AV_LOG_ERROR,
832  "The value for option '%s' is not an image size.\n", name);
833  return AVERROR(EINVAL);
834  }
835 
836  dst = ((uint8_t*)target_obj) + o->offset;
837  if (w_out) *w_out = *(int *)dst;
838  if (h_out) *h_out = *((int *)dst+1);
839  return 0;
840 }
841 
842 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
843 {
844  int64_t intnum = 1;
845  double num = 1;
846  int ret, den = 1;
847 
848  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
849  return ret;
850 
851  if (num == 1.0 && (int)intnum == intnum)
852  *out_val = (AVRational){intnum, den};
853  else
854  *out_val = av_d2q(num*intnum/den, 1<<24);
855  return 0;
856 }
857 
858 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
859  enum AVOptionType type, const char *desc)
860 {
861  void *dst, *target_obj;
862  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
863  if (!o || !target_obj)
865  if (o->type != type) {
866  av_log(obj, AV_LOG_ERROR,
867  "The value for option '%s' is not a %s format.\n", desc, name);
868  return AVERROR(EINVAL);
869  }
870 
871  dst = ((uint8_t*)target_obj) + o->offset;
872  *out_fmt = *(int *)dst;
873  return 0;
874 }
875 
876 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
877 {
878  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
879 }
880 
881 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
882 {
883  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
884 }
885 
886 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
887 {
888  void *dst, *target_obj;
889  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
890  if (!o || !target_obj)
892  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
893  av_log(obj, AV_LOG_ERROR,
894  "The value for option '%s' is not a channel layout.\n", name);
895  return AVERROR(EINVAL);
896  }
897 
898  dst = ((uint8_t*)target_obj) + o->offset;
899  *cl = *(int64_t *)dst;
900  return 0;
901 }
902 
903 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
904 {
905  void *target_obj;
906  AVDictionary *src;
907  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
908 
909  if (!o || !target_obj)
911  if (o->type != AV_OPT_TYPE_DICT)
912  return AVERROR(EINVAL);
913 
914  src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
915  av_dict_copy(out_val, src, 0);
916 
917  return 0;
918 }
919 
920 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
921 {
922  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
923  const AVOption *flag = av_opt_find(obj, flag_name,
924  field ? field->unit : NULL, 0, 0);
925  int64_t res;
926 
927  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
928  av_opt_get_int(obj, field_name, 0, &res) < 0)
929  return 0;
930  return res & flag->default_val.i64;
931 }
932 
933 static void log_value(void *av_log_obj, int level, double d)
934 {
935  if (d == INT_MAX) {
936  av_log(av_log_obj, level, "INT_MAX");
937  } else if (d == INT_MIN) {
938  av_log(av_log_obj, level, "INT_MIN");
939  } else if (d == UINT32_MAX) {
940  av_log(av_log_obj, level, "UINT32_MAX");
941  } else if (d == (double)INT64_MAX) {
942  av_log(av_log_obj, level, "I64_MAX");
943  } else if (d == INT64_MIN) {
944  av_log(av_log_obj, level, "I64_MIN");
945  } else if (d == FLT_MAX) {
946  av_log(av_log_obj, level, "FLT_MAX");
947  } else if (d == FLT_MIN) {
948  av_log(av_log_obj, level, "FLT_MIN");
949  } else if (d == -FLT_MAX) {
950  av_log(av_log_obj, level, "-FLT_MAX");
951  } else if (d == -FLT_MIN) {
952  av_log(av_log_obj, level, "-FLT_MIN");
953  } else if (d == DBL_MAX) {
954  av_log(av_log_obj, level, "DBL_MAX");
955  } else if (d == DBL_MIN) {
956  av_log(av_log_obj, level, "DBL_MIN");
957  } else if (d == -DBL_MAX) {
958  av_log(av_log_obj, level, "-DBL_MAX");
959  } else if (d == -DBL_MIN) {
960  av_log(av_log_obj, level, "-DBL_MIN");
961  } else {
962  av_log(av_log_obj, level, "%g", d);
963  }
964 }
965 
966 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
967 {
968  const AVOption *opt = NULL;
969 
970  if (!unit)
971  return NULL;
972  while ((opt = av_opt_next(obj, opt)))
973  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
974  opt->default_val.i64 == value)
975  return opt->name;
976  return NULL;
977 }
978 
979 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
980 {
981  const AVOption *opt = NULL;
982  char flags[512];
983 
984  flags[0] = 0;
985  if (!unit)
986  return NULL;
987  while ((opt = av_opt_next(obj, opt))) {
988  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
989  opt->default_val.i64 & value) {
990  if (flags[0])
991  av_strlcatf(flags, sizeof(flags), "+");
992  av_strlcatf(flags, sizeof(flags), "%s", opt->name);
993  }
994  }
995  if (flags[0])
996  return av_strdup(flags);
997  return NULL;
998 }
999 
1000 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1001  int req_flags, int rej_flags)
1002 {
1003  const AVOption *opt=NULL;
1004  AVOptionRanges *r;
1005  int i;
1006 
1007  while ((opt = av_opt_next(obj, opt))) {
1008  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1009  continue;
1010 
1011  /* Don't print CONST's on level one.
1012  * Don't print anything but CONST's on level two.
1013  * Only print items from the requested unit.
1014  */
1015  if (!unit && opt->type==AV_OPT_TYPE_CONST)
1016  continue;
1017  else if (unit && opt->type!=AV_OPT_TYPE_CONST)
1018  continue;
1019  else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1020  continue;
1021  else if (unit && opt->type == AV_OPT_TYPE_CONST)
1022  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1023  else
1024  av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1025  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1026  opt->name);
1027 
1028  switch (opt->type) {
1029  case AV_OPT_TYPE_FLAGS:
1030  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1031  break;
1032  case AV_OPT_TYPE_INT:
1033  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1034  break;
1035  case AV_OPT_TYPE_INT64:
1036  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1037  break;
1038  case AV_OPT_TYPE_DOUBLE:
1039  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1040  break;
1041  case AV_OPT_TYPE_FLOAT:
1042  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1043  break;
1044  case AV_OPT_TYPE_STRING:
1045  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1046  break;
1047  case AV_OPT_TYPE_RATIONAL:
1048  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1049  break;
1050  case AV_OPT_TYPE_BINARY:
1051  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1052  break;
1054  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1055  break;
1057  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1058  break;
1059  case AV_OPT_TYPE_PIXEL_FMT:
1060  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1061  break;
1063  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1064  break;
1065  case AV_OPT_TYPE_DURATION:
1066  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1067  break;
1068  case AV_OPT_TYPE_COLOR:
1069  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1070  break;
1072  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1073  break;
1074  case AV_OPT_TYPE_BOOL:
1075  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1076  break;
1077  case AV_OPT_TYPE_CONST:
1078  default:
1079  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1080  break;
1081  }
1082  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1083  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1084  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1085  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1086  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1087  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1088  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1089  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1090 
1091  if (opt->help)
1092  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1093 
1094  if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1095  switch (opt->type) {
1096  case AV_OPT_TYPE_INT:
1097  case AV_OPT_TYPE_INT64:
1098  case AV_OPT_TYPE_DOUBLE:
1099  case AV_OPT_TYPE_FLOAT:
1100  case AV_OPT_TYPE_RATIONAL:
1101  for (i = 0; i < r->nb_ranges; i++) {
1102  av_log(av_log_obj, AV_LOG_INFO, " (from ");
1103  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1104  av_log(av_log_obj, AV_LOG_INFO, " to ");
1105  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1106  av_log(av_log_obj, AV_LOG_INFO, ")");
1107  }
1108  break;
1109  }
1110  av_opt_freep_ranges(&r);
1111  }
1112 
1113  if (opt->type != AV_OPT_TYPE_CONST &&
1114  opt->type != AV_OPT_TYPE_BINARY &&
1115  !((opt->type == AV_OPT_TYPE_COLOR ||
1116  opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1117  opt->type == AV_OPT_TYPE_STRING ||
1118  opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1119  !opt->default_val.str)) {
1120  av_log(av_log_obj, AV_LOG_INFO, " (default ");
1121  switch (opt->type) {
1122  case AV_OPT_TYPE_BOOL:
1123  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1124  break;
1125  case AV_OPT_TYPE_FLAGS: {
1126  char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1127  if (def_flags) {
1128  av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1129  av_freep(&def_flags);
1130  } else {
1131  av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1132  }
1133  break;
1134  }
1135  case AV_OPT_TYPE_DURATION: {
1136  char buf[25];
1137  format_duration(buf, sizeof(buf), opt->default_val.i64);
1138  av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1139  break;
1140  }
1141  case AV_OPT_TYPE_INT:
1142  case AV_OPT_TYPE_INT64: {
1143  const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1144  if (def_const)
1145  av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1146  else
1147  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1148  break;
1149  }
1150  case AV_OPT_TYPE_DOUBLE:
1151  case AV_OPT_TYPE_FLOAT:
1152  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1153  break;
1154  case AV_OPT_TYPE_RATIONAL: {
1155  AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1156  av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1157  break;
1158  case AV_OPT_TYPE_PIXEL_FMT:
1159  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1160  break;
1162  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1163  break;
1164  case AV_OPT_TYPE_COLOR:
1166  case AV_OPT_TYPE_STRING:
1168  av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1169  break;
1171  av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1172  break;
1173  }
1174  av_log(av_log_obj, AV_LOG_INFO, ")");
1175  }
1176 
1177  av_log(av_log_obj, AV_LOG_INFO, "\n");
1178  if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
1179  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1180  }
1181  }
1182 }
1183 
1184 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1185 {
1186  if (!obj)
1187  return -1;
1188 
1189  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
1190 
1191  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1192 
1193  return 0;
1194 }
1195 
1197 {
1198  av_opt_set_defaults2(s, 0, 0);
1199 }
1200 
1201 void av_opt_set_defaults2(void *s, int mask, int flags)
1202 {
1203  const AVOption *opt = NULL;
1204  while ((opt = av_opt_next(s, opt))) {
1205  void *dst = ((uint8_t*)s) + opt->offset;
1206 
1207  if ((opt->flags & mask) != flags)
1208  continue;
1209 
1210  if (opt->flags & AV_OPT_FLAG_READONLY)
1211  continue;
1212 
1213  switch (opt->type) {
1214  case AV_OPT_TYPE_CONST:
1215  /* Nothing to be done here */
1216  break;
1217  case AV_OPT_TYPE_BOOL:
1218  case AV_OPT_TYPE_FLAGS:
1219  case AV_OPT_TYPE_INT:
1220  case AV_OPT_TYPE_INT64:
1221  case AV_OPT_TYPE_DURATION:
1223  case AV_OPT_TYPE_PIXEL_FMT:
1225  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1226  break;
1227  case AV_OPT_TYPE_DOUBLE:
1228  case AV_OPT_TYPE_FLOAT: {
1229  double val;
1230  val = opt->default_val.dbl;
1231  write_number(s, opt, dst, val, 1, 1);
1232  }
1233  break;
1234  case AV_OPT_TYPE_RATIONAL: {
1235  AVRational val;
1236  val = av_d2q(opt->default_val.dbl, INT_MAX);
1237  write_number(s, opt, dst, 1, val.den, val.num);
1238  }
1239  break;
1240  case AV_OPT_TYPE_COLOR:
1241  set_string_color(s, opt, opt->default_val.str, dst);
1242  break;
1243  case AV_OPT_TYPE_STRING:
1244  set_string(s, opt, opt->default_val.str, dst);
1245  break;
1247  set_string_image_size(s, opt, opt->default_val.str, dst);
1248  break;
1250  set_string_video_rate(s, opt, opt->default_val.str, dst);
1251  break;
1252  case AV_OPT_TYPE_BINARY:
1253  set_string_binary(s, opt, opt->default_val.str, dst);
1254  break;
1255  case AV_OPT_TYPE_DICT:
1256  /* Cannot set defaults for these types */
1257  break;
1258  default:
1259  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1260  }
1261  }
1262 }
1263 
1264 /**
1265  * Store the value in the field in ctx that is named like key.
1266  * ctx must be an AVClass context, storing is done using AVOptions.
1267  *
1268  * @param buf the string to parse, buf will be updated to point at the
1269  * separator just after the parsed key/value pair
1270  * @param key_val_sep a 0-terminated list of characters used to
1271  * separate key from value
1272  * @param pairs_sep a 0-terminated list of characters used to separate
1273  * two pairs from each other
1274  * @return 0 if the key/value pair has been successfully parsed and
1275  * set, or a negative value corresponding to an AVERROR code in case
1276  * of error:
1277  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1278  * the error code issued by av_opt_set() if the key/value pair
1279  * cannot be set
1280  */
1281 static int parse_key_value_pair(void *ctx, const char **buf,
1282  const char *key_val_sep, const char *pairs_sep)
1283 {
1284  char *key = av_get_token(buf, key_val_sep);
1285  char *val;
1286  int ret;
1287 
1288  if (!key)
1289  return AVERROR(ENOMEM);
1290 
1291  if (*key && strspn(*buf, key_val_sep)) {
1292  (*buf)++;
1293  val = av_get_token(buf, pairs_sep);
1294  if (!val) {
1295  av_freep(&key);
1296  return AVERROR(ENOMEM);
1297  }
1298  } else {
1299  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1300  av_free(key);
1301  return AVERROR(EINVAL);
1302  }
1303 
1304  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1305 
1306  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1307  if (ret == AVERROR_OPTION_NOT_FOUND)
1308  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1309 
1310  av_free(key);
1311  av_free(val);
1312  return ret;
1313 }
1314 
1315 int av_set_options_string(void *ctx, const char *opts,
1316  const char *key_val_sep, const char *pairs_sep)
1317 {
1318  int ret, count = 0;
1319 
1320  if (!opts)
1321  return 0;
1322 
1323  while (*opts) {
1324  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1325  return ret;
1326  count++;
1327 
1328  if (*opts)
1329  opts++;
1330  }
1331 
1332  return count;
1333 }
1334 
1335 #define WHITESPACES " \n\t"
1336 
1337 static int is_key_char(char c)
1338 {
1339  return (unsigned)((c | 32) - 'a') < 26 ||
1340  (unsigned)(c - '0') < 10 ||
1341  c == '-' || c == '_' || c == '/' || c == '.';
1342 }
1343 
1344 /**
1345  * Read a key from a string.
1346  *
1347  * The key consists of is_key_char characters and must be terminated by a
1348  * character from the delim string; spaces are ignored.
1349  *
1350  * @return 0 for success (even with ellipsis), <0 for failure
1351  */
1352 static int get_key(const char **ropts, const char *delim, char **rkey)
1353 {
1354  const char *opts = *ropts;
1355  const char *key_start, *key_end;
1356 
1357  key_start = opts += strspn(opts, WHITESPACES);
1358  while (is_key_char(*opts))
1359  opts++;
1360  key_end = opts;
1361  opts += strspn(opts, WHITESPACES);
1362  if (!*opts || !strchr(delim, *opts))
1363  return AVERROR(EINVAL);
1364  opts++;
1365  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1366  return AVERROR(ENOMEM);
1367  memcpy(*rkey, key_start, key_end - key_start);
1368  (*rkey)[key_end - key_start] = 0;
1369  *ropts = opts;
1370  return 0;
1371 }
1372 
1373 int av_opt_get_key_value(const char **ropts,
1374  const char *key_val_sep, const char *pairs_sep,
1375  unsigned flags,
1376  char **rkey, char **rval)
1377 {
1378  int ret;
1379  char *key = NULL, *val;
1380  const char *opts = *ropts;
1381 
1382  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1383  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1384  return AVERROR(EINVAL);
1385  if (!(val = av_get_token(&opts, pairs_sep))) {
1386  av_free(key);
1387  return AVERROR(ENOMEM);
1388  }
1389  *ropts = opts;
1390  *rkey = key;
1391  *rval = val;
1392  return 0;
1393 }
1394 
1395 int av_opt_set_from_string(void *ctx, const char *opts,
1396  const char *const *shorthand,
1397  const char *key_val_sep, const char *pairs_sep)
1398 {
1399  int ret, count = 0;
1400  const char *dummy_shorthand = NULL;
1401  char *av_uninit(parsed_key), *av_uninit(value);
1402  const char *key;
1403 
1404  if (!opts)
1405  return 0;
1406  if (!shorthand)
1407  shorthand = &dummy_shorthand;
1408 
1409  while (*opts) {
1410  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1411  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1412  &parsed_key, &value);
1413  if (ret < 0) {
1414  if (ret == AVERROR(EINVAL))
1415  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1416  else
1417  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1418  av_err2str(ret));
1419  return ret;
1420  }
1421  if (*opts)
1422  opts++;
1423  if (parsed_key) {
1424  key = parsed_key;
1425  while (*shorthand) /* discard all remaining shorthand */
1426  shorthand++;
1427  } else {
1428  key = *(shorthand++);
1429  }
1430 
1431  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1432  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1433  if (ret == AVERROR_OPTION_NOT_FOUND)
1434  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1435  av_free(value);
1436  av_free(parsed_key);
1437  return ret;
1438  }
1439 
1440  av_free(value);
1441  av_free(parsed_key);
1442  count++;
1443  }
1444  return count;
1445 }
1446 
1447 void av_opt_free(void *obj)
1448 {
1449  const AVOption *o = NULL;
1450  while ((o = av_opt_next(obj, o))) {
1451  switch (o->type) {
1452  case AV_OPT_TYPE_STRING:
1453  case AV_OPT_TYPE_BINARY:
1454  av_freep((uint8_t *)obj + o->offset);
1455  break;
1456 
1457  case AV_OPT_TYPE_DICT:
1458  av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1459  break;
1460 
1461  default:
1462  break;
1463  }
1464  }
1465 }
1466 
1467 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1468 {
1469  AVDictionaryEntry *t = NULL;
1470  AVDictionary *tmp = NULL;
1471  int ret = 0;
1472 
1473  if (!options)
1474  return 0;
1475 
1476  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1477  ret = av_opt_set(obj, t->key, t->value, search_flags);
1478  if (ret == AVERROR_OPTION_NOT_FOUND)
1479  ret = av_dict_set(&tmp, t->key, t->value, 0);
1480  if (ret < 0) {
1481  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1482  av_dict_free(&tmp);
1483  return ret;
1484  }
1485  ret = 0;
1486  }
1487  av_dict_free(options);
1488  *options = tmp;
1489  return ret;
1490 }
1491 
1493 {
1494  return av_opt_set_dict2(obj, options, 0);
1495 }
1496 
1497 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1498  int opt_flags, int search_flags)
1499 {
1500  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1501 }
1502 
1503 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1504  int opt_flags, int search_flags, void **target_obj)
1505 {
1506  const AVClass *c;
1507  const AVOption *o = NULL;
1508 
1509  if(!obj)
1510  return NULL;
1511 
1512  c= *(AVClass**)obj;
1513 
1514  if (!c)
1515  return NULL;
1516 
1517  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1518  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1519  const AVClass *child = NULL;
1520  while (child = av_opt_child_class_next(c, child))
1521  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1522  return o;
1523  } else {
1524  void *child = NULL;
1525  while (child = av_opt_child_next(obj, child))
1526  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1527  return o;
1528  }
1529  }
1530 
1531  while (o = av_opt_next(obj, o)) {
1532  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1533  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1534  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1535  if (target_obj) {
1536  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1537  *target_obj = obj;
1538  else
1539  *target_obj = NULL;
1540  }
1541  return o;
1542  }
1543  }
1544  return NULL;
1545 }
1546 
1547 void *av_opt_child_next(void *obj, void *prev)
1548 {
1549  const AVClass *c = *(AVClass**)obj;
1550  if (c->child_next)
1551  return c->child_next(obj, prev);
1552  return NULL;
1553 }
1554 
1555 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1556 {
1557  if (parent->child_class_next)
1558  return parent->child_class_next(prev);
1559  return NULL;
1560 }
1561 
1562 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1563 {
1564  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1565  if(!opt)
1566  return NULL;
1567  return (uint8_t*)obj + opt->offset;
1568 }
1569 
1570 static int opt_size(enum AVOptionType type)
1571 {
1572  switch(type) {
1573  case AV_OPT_TYPE_BOOL:
1574  case AV_OPT_TYPE_INT:
1575  case AV_OPT_TYPE_FLAGS: return sizeof(int);
1576  case AV_OPT_TYPE_DURATION:
1578  case AV_OPT_TYPE_INT64: return sizeof(int64_t);
1579  case AV_OPT_TYPE_DOUBLE: return sizeof(double);
1580  case AV_OPT_TYPE_FLOAT: return sizeof(float);
1581  case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
1583  case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
1584  case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
1585  case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1586  case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1587  case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1588  case AV_OPT_TYPE_COLOR: return 4;
1589  }
1590  return 0;
1591 }
1592 
1593 int av_opt_copy(void *dst, const void *src)
1594 {
1595  const AVOption *o = NULL;
1596  const AVClass *c;
1597  int ret = 0;
1598 
1599  if (!src)
1600  return 0;
1601 
1602  c = *(AVClass**)src;
1603  if (*(AVClass**)dst && c != *(AVClass**)dst)
1604  return AVERROR(EINVAL);
1605 
1606  while ((o = av_opt_next(src, o))) {
1607  void *field_dst = ((uint8_t*)dst) + o->offset;
1608  void *field_src = ((uint8_t*)src) + o->offset;
1609  uint8_t **field_dst8 = (uint8_t**)field_dst;
1610  uint8_t **field_src8 = (uint8_t**)field_src;
1611 
1612  if (o->type == AV_OPT_TYPE_STRING) {
1613  if (*field_dst8 != *field_src8)
1614  av_freep(field_dst8);
1615  *field_dst8 = av_strdup(*field_src8);
1616  if (*field_src8 && !*field_dst8)
1617  ret = AVERROR(ENOMEM);
1618  } else if (o->type == AV_OPT_TYPE_BINARY) {
1619  int len = *(int*)(field_src8 + 1);
1620  if (*field_dst8 != *field_src8)
1621  av_freep(field_dst8);
1622  *field_dst8 = av_memdup(*field_src8, len);
1623  if (len && !*field_dst8) {
1624  ret = AVERROR(ENOMEM);
1625  len = 0;
1626  }
1627  *(int*)(field_dst8 + 1) = len;
1628  } else if (o->type == AV_OPT_TYPE_CONST) {
1629  // do nothing
1630  } else if (o->type == AV_OPT_TYPE_DICT) {
1631  AVDictionary **sdict = (AVDictionary **) field_src;
1632  AVDictionary **ddict = (AVDictionary **) field_dst;
1633  if (*sdict != *ddict)
1634  av_dict_free(ddict);
1635  *ddict = NULL;
1636  av_dict_copy(ddict, *sdict, 0);
1637  if (av_dict_count(*sdict) != av_dict_count(*ddict))
1638  ret = AVERROR(ENOMEM);
1639  } else {
1640  memcpy(field_dst, field_src, opt_size(o->type));
1641  }
1642  }
1643  return ret;
1644 }
1645 
1646 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1647 {
1648  int ret;
1649  const AVClass *c = *(AVClass**)obj;
1650  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1651 
1652  if (c->version > (52 << 16 | 11 << 8))
1653  callback = c->query_ranges;
1654 
1655  if (!callback)
1657 
1658  ret = callback(ranges_arg, obj, key, flags);
1659  if (ret >= 0) {
1660  if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1661  ret = 1;
1662  (*ranges_arg)->nb_components = ret;
1663  }
1664  return ret;
1665 }
1666 
1667 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1668 {
1669  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1670  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1671  AVOptionRange *range = av_mallocz(sizeof(*range));
1672  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1673  int ret;
1674 
1675  *ranges_arg = NULL;
1676 
1677  if (!ranges || !range || !range_array || !field) {
1678  ret = AVERROR(ENOMEM);
1679  goto fail;
1680  }
1681 
1682  ranges->range = range_array;
1683  ranges->range[0] = range;
1684  ranges->nb_ranges = 1;
1685  ranges->nb_components = 1;
1686  range->is_range = 1;
1687  range->value_min = field->min;
1688  range->value_max = field->max;
1689 
1690  switch (field->type) {
1691  case AV_OPT_TYPE_BOOL:
1692  case AV_OPT_TYPE_INT:
1693  case AV_OPT_TYPE_INT64:
1694  case AV_OPT_TYPE_PIXEL_FMT:
1696  case AV_OPT_TYPE_FLOAT:
1697  case AV_OPT_TYPE_DOUBLE:
1698  case AV_OPT_TYPE_DURATION:
1699  case AV_OPT_TYPE_COLOR:
1701  break;
1702  case AV_OPT_TYPE_STRING:
1703  range->component_min = 0;
1704  range->component_max = 0x10FFFF; // max unicode value
1705  range->value_min = -1;
1706  range->value_max = INT_MAX;
1707  break;
1708  case AV_OPT_TYPE_RATIONAL:
1709  range->component_min = INT_MIN;
1710  range->component_max = INT_MAX;
1711  break;
1713  range->component_min = 0;
1714  range->component_max = INT_MAX/128/8;
1715  range->value_min = 0;
1716  range->value_max = INT_MAX/8;
1717  break;
1719  range->component_min = 1;
1720  range->component_max = INT_MAX;
1721  range->value_min = 1;
1722  range->value_max = INT_MAX;
1723  break;
1724  default:
1725  ret = AVERROR(ENOSYS);
1726  goto fail;
1727  }
1728 
1729  *ranges_arg = ranges;
1730  return 1;
1731 fail:
1732  av_free(ranges);
1733  av_free(range);
1734  av_free(range_array);
1735  return ret;
1736 }
1737 
1739 {
1740  int i;
1741  AVOptionRanges *ranges = *rangesp;
1742 
1743  if (!ranges)
1744  return;
1745 
1746  for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1747  AVOptionRange *range = ranges->range[i];
1748  if (range) {
1749  av_freep(&range->str);
1750  av_freep(&ranges->range[i]);
1751  }
1752  }
1753  av_freep(&ranges->range);
1754  av_freep(rangesp);
1755 }
1756 
1757 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1758 {
1759  int64_t i64;
1760  double d, d2;
1761  float f;
1762  AVRational q;
1763  int ret, w, h;
1764  char *str;
1765  void *dst;
1766 
1767  if (!o || !obj)
1768  return AVERROR(EINVAL);
1769 
1770  dst = ((uint8_t*)obj) + o->offset;
1771 
1772  switch (o->type) {
1773  case AV_OPT_TYPE_CONST:
1774  return 1;
1775  case AV_OPT_TYPE_BOOL:
1776  case AV_OPT_TYPE_FLAGS:
1777  case AV_OPT_TYPE_PIXEL_FMT:
1779  case AV_OPT_TYPE_INT:
1781  case AV_OPT_TYPE_DURATION:
1782  case AV_OPT_TYPE_INT64:
1783  read_number(o, dst, NULL, NULL, &i64);
1784  return o->default_val.i64 == i64;
1785  case AV_OPT_TYPE_STRING:
1786  str = *(char **)dst;
1787  if (str == o->default_val.str) //2 NULLs
1788  return 1;
1789  if (!str || !o->default_val.str) //1 NULL
1790  return 0;
1791  return !strcmp(str, o->default_val.str);
1792  case AV_OPT_TYPE_DOUBLE:
1793  read_number(o, dst, &d, NULL, NULL);
1794  return o->default_val.dbl == d;
1795  case AV_OPT_TYPE_FLOAT:
1796  read_number(o, dst, &d, NULL, NULL);
1797  f = o->default_val.dbl;
1798  d2 = f;
1799  return d2 == d;
1800  case AV_OPT_TYPE_RATIONAL:
1801  q = av_d2q(o->default_val.dbl, INT_MAX);
1802  return !av_cmp_q(*(AVRational*)dst, q);
1803  case AV_OPT_TYPE_BINARY: {
1804  struct {
1805  uint8_t *data;
1806  int size;
1807  } tmp = {0};
1808  int opt_size = *(int *)((void **)dst + 1);
1809  void *opt_ptr = *(void **)dst;
1810  if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1811  return 1;
1812  if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1813  return 0;
1814  if (opt_size != strlen(o->default_val.str) / 2)
1815  return 0;
1816  ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1817  if (!ret)
1818  ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1819  av_free(tmp.data);
1820  return ret;
1821  }
1822  case AV_OPT_TYPE_DICT:
1823  /* Binary and dict have not default support yet. Any pointer is not default. */
1824  return !!(*(void **)dst);
1826  if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1827  w = h = 0;
1828  else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1829  return ret;
1830  return (w == *(int *)dst) && (h == *((int *)dst+1));
1832  q = (AVRational){0, 0};
1833  if (o->default_val.str) {
1834  if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1835  return ret;
1836  }
1837  return !av_cmp_q(*(AVRational*)dst, q);
1838  case AV_OPT_TYPE_COLOR: {
1839  uint8_t color[4] = {0, 0, 0, 0};
1840  if (o->default_val.str) {
1841  if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1842  return ret;
1843  }
1844  return !memcmp(color, dst, sizeof(color));
1845  }
1846  default:
1847  av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1848  break;
1849  }
1850  return AVERROR_PATCHWELCOME;
1851 }
1852 
1853 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1854 {
1855  const AVOption *o;
1856  void *target;
1857  if (!obj)
1858  return AVERROR(EINVAL);
1859  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1860  if (!o)
1861  return AVERROR_OPTION_NOT_FOUND;
1862  return av_opt_is_set_to_default(target, o);
1863 }
1864 
1865 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1866  const char key_val_sep, const char pairs_sep)
1867 {
1868  const AVOption *o = NULL;
1869  uint8_t *buf;
1870  AVBPrint bprint;
1871  int ret, cnt = 0;
1872  const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1873 
1874  if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1875  pairs_sep == '\\' || key_val_sep == '\\') {
1876  av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1877  return AVERROR(EINVAL);
1878  }
1879 
1880  if (!obj || !buffer)
1881  return AVERROR(EINVAL);
1882 
1883  *buffer = NULL;
1885 
1886  while (o = av_opt_next(obj, o)) {
1887  if (o->type == AV_OPT_TYPE_CONST)
1888  continue;
1889  if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1890  continue;
1891  else if (((o->flags & opt_flags) != opt_flags))
1892  continue;
1893  if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1894  continue;
1895  if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1896  av_bprint_finalize(&bprint, NULL);
1897  return ret;
1898  }
1899  if (buf) {
1900  if (cnt++)
1901  av_bprint_append_data(&bprint, &pairs_sep, 1);
1902  av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1903  av_bprint_append_data(&bprint, &key_val_sep, 1);
1904  av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1905  av_freep(&buf);
1906  }
1907  }
1908  av_bprint_finalize(&bprint, buffer);
1909  return 0;
1910 }
1911 
1912 #ifdef TEST
1913 
1914 typedef struct TestContext
1915 {
1916  const AVClass *class;
1917  int num;
1918  int toggle;
1919  char *string;
1920  int flags;
1921  AVRational rational;
1922  AVRational video_rate;
1923  int w, h;
1924  enum AVPixelFormat pix_fmt;
1925  enum AVSampleFormat sample_fmt;
1926  int64_t duration;
1927  uint8_t color[4];
1928  int64_t channel_layout;
1929  void *binary;
1930  int binary_size;
1931  void *binary1;
1932  int binary_size1;
1933  void *binary2;
1934  int binary_size2;
1935  int64_t num64;
1936  float flt;
1937  double dbl;
1938  char *escape;
1939  int bool1;
1940  int bool2;
1941  int bool3;
1942 } TestContext;
1943 
1944 #define OFFSET(x) offsetof(TestContext, x)
1945 
1946 #define TEST_FLAG_COOL 01
1947 #define TEST_FLAG_LAME 02
1948 #define TEST_FLAG_MU 04
1949 
1950 static const AVOption test_options[]= {
1951 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, 1 },
1952 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, 1 },
1953 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, 10, 1 },
1954 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX, 1 },
1955 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, {.str = "\\=,"}, CHAR_MIN, CHAR_MAX, 1 },
1956 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 1}, 0, INT_MAX, 1, "flags" },
1957 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 1, "flags" },
1958 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 1, "flags" },
1959 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 1, "flags" },
1960 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{.str="200x300"}, 0, 0, 1},
1961 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_0BGR}, -1, INT_MAX, 1},
1962 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_S16}, -1, INT_MAX, 1},
1963 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 , 1},
1964 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 1000}, 0, INT64_MAX, 1},
1965 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0, 1},
1966 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX, 1},
1967 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0, 1 },
1968 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, {.str=NULL}, 0, 0, 1 },
1969 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, {.str=""}, 0, 0, 1 },
1970 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100, 1 },
1971 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100, 1},
1972 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100, 1 },
1973 {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, 1 },
1974 {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, 1 },
1975 {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 1 },
1976 {NULL},
1977 };
1978 
1979 static const char *test_get_name(void *ctx)
1980 {
1981  return "test";
1982 }
1983 
1984 static const AVClass test_class = {
1985  "TestContext",
1986  test_get_name,
1987  test_options
1988 };
1989 
1990 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
1991 {
1992  vfprintf(stdout, fmt, vl);
1993 }
1994 
1995 int main(void)
1996 {
1997  int i;
1998 
2001 
2002  printf("Testing default values\n");
2003  {
2004  TestContext test_ctx = { 0 };
2005  test_ctx.class = &test_class;
2006  av_opt_set_defaults(&test_ctx);
2007 
2008  printf("num=%d\n", test_ctx.num);
2009  printf("toggle=%d\n", test_ctx.toggle);
2010  printf("string=%s\n", test_ctx.string);
2011  printf("escape=%s\n", test_ctx.escape);
2012  printf("flags=%d\n", test_ctx.flags);
2013  printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
2014  printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
2015  printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
2016  printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
2017  printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
2018  printf("duration=%"PRId64"\n", test_ctx.duration);
2019  printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
2020  printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
2021  if (test_ctx.binary)
2022  printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]);
2023  printf("binary_size=%d\n", test_ctx.binary_size);
2024  printf("num64=%"PRId64"\n", test_ctx.num64);
2025  printf("flt=%.6f\n", test_ctx.flt);
2026  printf("dbl=%.6f\n", test_ctx.dbl);
2027 
2028  av_opt_show2(&test_ctx, NULL, -1, 0);
2029 
2030  av_opt_free(&test_ctx);
2031  }
2032 
2033  printf("\nTesting av_opt_is_set_to_default()\n");
2034  {
2035  int ret;
2036  TestContext test_ctx = { 0 };
2037  const AVOption *o = NULL;
2038  test_ctx.class = &test_class;
2039 
2041 
2042  while (o = av_opt_next(&test_ctx, o)) {
2043  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2044  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2045  }
2046  av_opt_set_defaults(&test_ctx);
2047  while (o = av_opt_next(&test_ctx, o)) {
2048  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2049  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2050  }
2051  av_opt_free(&test_ctx);
2052  }
2053 
2054  printf("\nTest av_opt_serialize()\n");
2055  {
2056  TestContext test_ctx = { 0 };
2057  char *buf;
2058  test_ctx.class = &test_class;
2059 
2061 
2062  av_opt_set_defaults(&test_ctx);
2063  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2064  printf("%s\n", buf);
2065  av_opt_free(&test_ctx);
2066  memset(&test_ctx, 0, sizeof(test_ctx));
2067  test_ctx.class = &test_class;
2068  av_set_options_string(&test_ctx, buf, "=", ",");
2069  av_free(buf);
2070  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2071  printf("%s\n", buf);
2072  av_free(buf);
2073  }
2074  }
2075  av_opt_free(&test_ctx);
2076  }
2077 
2078  printf("\nTesting av_set_options_string()\n");
2079  {
2080  TestContext test_ctx = { 0 };
2081  static const char * const options[] = {
2082  "",
2083  ":",
2084  "=",
2085  "foo=:",
2086  ":=foo",
2087  "=foo",
2088  "foo=",
2089  "foo",
2090  "foo=val",
2091  "foo==val",
2092  "toggle=:",
2093  "string=:",
2094  "toggle=1 : foo",
2095  "toggle=100",
2096  "toggle==1",
2097  "flags=+mu-lame : num=42: toggle=0",
2098  "num=42 : string=blahblah",
2099  "rational=0 : rational=1/2 : rational=1/-1",
2100  "rational=-1/0",
2101  "size=1024x768",
2102  "size=pal",
2103  "size=bogus",
2104  "pix_fmt=yuv420p",
2105  "pix_fmt=2",
2106  "pix_fmt=bogus",
2107  "sample_fmt=s16",
2108  "sample_fmt=2",
2109  "sample_fmt=bogus",
2110  "video_rate=pal",
2111  "video_rate=25",
2112  "video_rate=30000/1001",
2113  "video_rate=30/1.001",
2114  "video_rate=bogus",
2115  "duration=bogus",
2116  "duration=123.45",
2117  "duration=1\\:23\\:45.67",
2118  "color=blue",
2119  "color=0x223300",
2120  "color=0x42FF07AA",
2121  "cl=stereo+downmix",
2122  "cl=foo",
2123  "bin=boguss",
2124  "bin=111",
2125  "bin=ffff",
2126  "num64=bogus",
2127  "num64=44",
2128  "num64=44.4",
2129  "num64=-1",
2130  "num64=101",
2131  "flt=bogus",
2132  "flt=2",
2133  "flt=2.2",
2134  "flt=-1",
2135  "flt=101",
2136  "dbl=bogus",
2137  "dbl=2",
2138  "dbl=2.2",
2139  "dbl=-1",
2140  "dbl=101",
2141  "bool1=true",
2142  "bool2=auto",
2143  };
2144 
2145  test_ctx.class = &test_class;
2146  av_opt_set_defaults(&test_ctx);
2147 
2149 
2150  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2151  int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
2152  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2153  if (silence_log)
2155  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
2156  printf("Error '%s'\n", options[i]);
2157  else
2158  printf("OK '%s'\n", options[i]);
2160  }
2161  av_opt_free(&test_ctx);
2162  }
2163 
2164  printf("\nTesting av_opt_set_from_string()\n");
2165  {
2166  TestContext test_ctx = { 0 };
2167  static const char * const options[] = {
2168  "",
2169  "5",
2170  "5:hello",
2171  "5:hello:size=pal",
2172  "5:size=pal:hello",
2173  ":",
2174  "=",
2175  " 5 : hello : size = pal ",
2176  "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2177  };
2178  static const char * const shorthand[] = { "num", "string", NULL };
2179 
2180  test_ctx.class = &test_class;
2181  av_opt_set_defaults(&test_ctx);
2182 
2184 
2185  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2186  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2187  if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
2188  printf("Error '%s'\n", options[i]);
2189  else
2190  printf("OK '%s'\n", options[i]);
2191  }
2192  av_opt_free(&test_ctx);
2193  }
2194 
2195  return 0;
2196 }
2197 
2198 #endif
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:903
#define NULL
Definition: coverity.c:32
static const char * get_bool_name(int val)
Definition: opt.c:303
const char const char void * val
Definition: avisynth_c.h:634
A single allowed range of values, or a single allowed value.
Definition: opt.h:306
const char * s
Definition: avisynth_c.h:631
static enum AVPixelFormat pix_fmt
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:73
static char * get_opt_flags_string(void *obj, const char *unit, int64_t value)
Definition: opt.c:979
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:174
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:496
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:143
#define AV_OPT_FLAG_EXPORT
The option is inteded for exporting values to the caller.
Definition: opt.h:286
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:282
AVFormatContext * ctx
Definition: movenc-test.c:48
const char * fmt
Definition: avisynth_c.h:632
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
Definition: opt.c:260
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
Definition: opt.c:1547
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:559
static int get_key(const char **ropts, const char *delim, char **rkey)
Read a key from a string.
Definition: opt.c:1352
void av_log_set_level(int level)
Set the log level.
Definition: log.c:382
double value_max
Definition: opt.h:313
const AVClass * av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
Iterate over potential AVOptions-enabled children of parent.
Definition: opt.c:1555
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1196
static int opt_size(enum AVOptionType type)
Definition: opt.c:1570
int num
numerator
Definition: rational.h:44
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:280
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:34
const char * b
Definition: vf_curves.c:109
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:1315
static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:387
external API header
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:213
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:501
Use backslash escaping.
Definition: avstring.h:310
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:247
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:808
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:1865
static void opt_list(void *obj, void *av_log_obj, const char *unit, int req_flags, int rej_flags)
Definition: opt.c:1000
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:625
AVOptionType
Definition: opt.h:221
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:1853
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
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:1757
GLsizei const char ** string
Definition: opengl_enc.c:120
int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:842
#define AV_LOG_QUIET
Print no output.
Definition: log.h:158
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:1395
uint64_t av_get_channel_layout(const char *name)
Return a channel layout id that matches name, or 0 if no match is found.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int get_format(void *obj, const char *name, int search_flags, int *out_fmt, enum AVOptionType type, const char *desc)
Definition: opt.c:858
Public dictionary API.
const char * name
Definition: opt.h:246
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition: cmdutils.c:89
#define WHITESPACES
Definition: opt.c:1335
uint8_t
AVOptionRange ** range
Array of option ranges.
Definition: opt.h:360
#define av_malloc(s)
class_name
Definition: libkvazaar.c:276
const char * help
short English help text
Definition: opt.h:252
AVOptions.
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:594
static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
Definition: opt.c:58
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static const char * get_opt_const_name(void *obj, const char *unit, int64_t value)
Definition: opt.c:966
Accept to parse a value without a key; the key will then be returned as NULL.
Definition: opt.h:529
#define AV_CH_LAYOUT_HEXAGONAL
const char * str
Definition: opt.h:267
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
Definition: opt.c:491
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
double component_max
Definition: opt.h:318
ptrdiff_t size
Definition: opengl_enc.c:101
const OptionDef options[]
Definition: ffserver.c:3962
#define av_log(a,...)
static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum, int search_flags)
Definition: opt.c:765
int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:553
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:275
int flag
Definition: checkasm.c:115
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:349
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:300
static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:381
static int hexchar2int(char c)
Definition: opt.c:119
int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
Definition: opt.c:876
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:722
int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
Definition: opt.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_BPRINT_SIZE_UNLIMITED
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:1562
static const uint16_t mask[17]
Definition: lzw.c:38
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
Definition: opt.h:292
double max
maximum valid value for the option
Definition: opt.h:272
#define AVERROR(e)
Definition: error.h:43
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:486
simple assert() macros that are a bit more flexible than ISO C assert().
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
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:320
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:47
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:80
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:149
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:1497
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:160
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
Definition: opt.c:881
static void callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
Definition: dshow.c:161
audio channel layout utility functions
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
const char * str
Definition: opt.h:307
#define FFMIN(a, b)
Definition: common.h:96
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:397
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:556
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
const char * unit
The logical unit to which the option belongs.
Definition: opt.h:300
int64_t duration
Definition: movenc-test.c:63
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:1281
double component_min
Value's component range.
Definition: opt.h:318
int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
Definition: opt.c:609
double min
minimum valid value for the option
Definition: opt.h:271
static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
Definition: opt.c:173
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:1201
static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:126
int n
Definition: avisynth_c.h:547
#define src
Definition: vp9dsp.c:530
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:1467
const struct AVClass *(* child_class_next)(const struct AVClass *prev)
Return an AVClass corresponding to the next potential AVOptions-enabled child.
Definition: log.h:123
#define FF_ARRAY_ELEMS(a)
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
int flags
Definition: opt.h:274
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:1667
#define INFINITY
Definition: math.h:27
void * av_memdup(const void *p, size_t size)
Duplicate the buffer p.
Definition: mem.c:299
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int is_range
Range flag.
Definition: opt.h:323
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:784
static void log_value(void *av_log_obj, int level, double d)
Definition: opt.c:933
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
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:570
void av_opt_freep_ranges(AVOptionRanges **rangesp)
Free an AVOptionRanges struct and set it to NULL.
Definition: opt.c:1738
int offset
The offset relative to the context structure where the option value is stored.
Definition: opt.h:258
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1492
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
static int set_number(void *obj, const char *name, double num, int den, int64_t intnum, int search_flags)
Definition: opt.c:470
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:1184
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
static void format_duration(char *buf, size_t size, int64_t d)
Definition: opt.c:643
static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
Definition: opt.c:310
#define OPT_EVAL_NUMBER(name, opttype, vartype)
Definition: opt.c:455
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
Definition: opt.c:886
#define llrint(x)
Definition: libm.h:394
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:69
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:276
#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:571
double dbl
Definition: opt.h:266
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:54
offset must point to AVRational
Definition: opt.h:235
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
#define snprintf
Definition: snprintf.h:34
union AVOption::@200 default_val
the default value for scalar options
offset must point to two consecutive integers
Definition: opt.h:232
misc parsing utilities
static int flags
Definition: cpu.c:47
uint8_t level
Definition: svq3.c:150
int version
LIBAVUTIL_VERSION with which this structure was created.
Definition: log.h:93
#define OFFSET(x)
Definition: ffmpeg_opt.c:3106
static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
Definition: opt.c:275
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:265
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1447
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:1646
double value_min
Value range.
Definition: opt.h:313
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:282
#define DEFAULT_NUMVAL(opt)
Definition: opt.c:167
#define class
Definition: math.h:25
int(* query_ranges)(struct AVOptionRanges **, void *obj, const char *key, int flags)
Callback to return the supported/allowed ranges.
Definition: log.h:142
signed 16 bits
Definition: samplefmt.h:62
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
Definition: opt.c:796
static double c[64]
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:341
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1593
void *(* child_next)(void *obj, void *prev)
Return next AVOptions-enabled child or NULL.
Definition: log.h:113
#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:565
static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
Definition: opt.c:79
char * key
Definition: dict.h:87
int den
denominator
Definition: rational.h:45
int nb_ranges
Number of ranges per component.
Definition: opt.h:364
List of AVOptionRange structs.
Definition: opt.h:329
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:678
#define av_free(p)
#define AV_OPT_SERIALIZE_SKIP_DEFAULTS
Serialize options that are not set to default values only.
Definition: opt.h:839
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:61
char * value
Definition: dict.h:88
enum AVOptionType type
Definition: opt.h:259
#define NAN
Definition: math.h:28
int len
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:1373
int64_t i64
Definition: opt.h:265
AVDictionary * opts
Definition: movenc-test.c:50
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:291
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:920
#define av_uninit(x)
Definition: attributes.h:149
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:295
#define av_freep(p)
static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:288
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
#define AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
Serialize options that exactly match opt_flags only.
Definition: opt.h:840
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:1503
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2090
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:2078
int main(int argc, char **argv)
Definition: main.c:22
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:604
#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:578
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
Definition: opt.c:531
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:393
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
static int is_key_char(char c)
Definition: opt.c:1337
GLuint buffer
Definition: opengl_enc.c:102
simple arithmetic expression evaluator
const char * name
Definition: opengl_enc.c:103
int nb_components
Number of componentes.
Definition: opt.h:368
int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
Definition: opt.c:824