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 "dict.h"
34 #include "eval.h"
35 #include "log.h"
36 #include "parseutils.h"
37 #include "pixdesc.h"
38 #include "mathematics.h"
39 #include "opt.h"
40 #include "samplefmt.h"
41 #include "bprint.h"
42 
43 #include <float.h>
44 
45 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:
62  *intnum = *(unsigned int*)dst;
63  return 0;
65  *intnum = *(enum AVPixelFormat *)dst;
66  return 0;
68  *intnum = *(enum AVSampleFormat *)dst;
69  return 0;
70  case AV_OPT_TYPE_BOOL:
71  case AV_OPT_TYPE_INT:
72  *intnum = *(int *)dst;
73  return 0;
76  case AV_OPT_TYPE_INT64:
77  *intnum = *(int64_t *)dst;
78  return 0;
79  case AV_OPT_TYPE_FLOAT:
80  *num = *(float *)dst;
81  return 0;
82  case AV_OPT_TYPE_DOUBLE:
83  *num = *(double *)dst;
84  return 0;
86  *intnum = ((AVRational *)dst)->num;
87  *den = ((AVRational *)dst)->den;
88  return 0;
89  case AV_OPT_TYPE_CONST:
90  *num = o->default_val.dbl;
91  return 0;
92  }
93  return AVERROR(EINVAL);
94 }
95 
96 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
97 {
98  if (o->type != AV_OPT_TYPE_FLAGS &&
99  (o->max * den < num * intnum || o->min * den > num * intnum)) {
100  num = den ? num * intnum / den : (num * intnum ? INFINITY : NAN);
101  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
102  num, o->name, o->min, o->max);
103  return AVERROR(ERANGE);
104  }
105  if (o->type == AV_OPT_TYPE_FLAGS) {
106  double d = num*intnum/den;
107  if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
108  av_log(obj, AV_LOG_ERROR,
109  "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
110  num*intnum/den, o->name);
111  return AVERROR(ERANGE);
112  }
113  }
114 
115  switch (o->type) {
117  *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
118  break;
120  *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
121  break;
122  case AV_OPT_TYPE_BOOL:
123  case AV_OPT_TYPE_FLAGS:
124  case AV_OPT_TYPE_INT:
125  *(int *)dst = llrint(num / den) * intnum;
126  break;
129  case AV_OPT_TYPE_INT64:
130  *(int64_t *)dst = llrint(num / den) * intnum;
131  break;
132  case AV_OPT_TYPE_FLOAT:
133  *(float *)dst = num * intnum / den;
134  break;
135  case AV_OPT_TYPE_DOUBLE:
136  *(double *)dst = num * intnum / den;
137  break;
140  if ((int) num == num)
141  *(AVRational *)dst = (AVRational) { num *intnum, den };
142  else
143  *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
144  break;
145  default:
146  return AVERROR(EINVAL);
147  }
148  return 0;
149 }
150 
151 static int hexchar2int(char c) {
152  if (c >= '0' && c <= '9')
153  return c - '0';
154  if (c >= 'a' && c <= 'f')
155  return c - 'a' + 10;
156  if (c >= 'A' && c <= 'F')
157  return c - 'A' + 10;
158  return -1;
159 }
160 
161 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
162 {
163  int *lendst = (int *)(dst + 1);
164  uint8_t *bin, *ptr;
165  int len;
166 
167  av_freep(dst);
168  *lendst = 0;
169 
170  if (!val || !(len = strlen(val)))
171  return 0;
172 
173  if (len & 1)
174  return AVERROR(EINVAL);
175  len /= 2;
176 
177  ptr = bin = av_malloc(len);
178  if (!ptr)
179  return AVERROR(ENOMEM);
180  while (*val) {
181  int a = hexchar2int(*val++);
182  int b = hexchar2int(*val++);
183  if (a < 0 || b < 0) {
184  av_free(bin);
185  return AVERROR(EINVAL);
186  }
187  *ptr++ = (a << 4) | b;
188  }
189  *dst = bin;
190  *lendst = len;
191 
192  return 0;
193 }
194 
195 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
196 {
197  av_freep(dst);
198  *dst = av_strdup(val);
199  return *dst ? 0 : AVERROR(ENOMEM);
200 }
201 
202 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
203  opt->type == AV_OPT_TYPE_CONST || \
204  opt->type == AV_OPT_TYPE_FLAGS || \
205  opt->type == AV_OPT_TYPE_INT) \
206  ? opt->default_val.i64 \
207  : opt->default_val.dbl)
208 
209 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
210 {
211  int ret = 0;
212  int num, den;
213  char c;
214 
215  if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
216  if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
217  return ret;
218  ret = 0;
219  }
220 
221  for (;;) {
222  int i = 0;
223  char buf[256];
224  int cmd = 0;
225  double d;
226  int64_t intnum = 1;
227 
228  if (o->type == AV_OPT_TYPE_FLAGS) {
229  if (*val == '+' || *val == '-')
230  cmd = *(val++);
231  for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
232  buf[i] = val[i];
233  buf[i] = 0;
234  }
235 
236  {
237  const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
238  int res;
239  int ci = 0;
240  double const_values[64];
241  const char * const_names[64];
242  if (o_named && o_named->type == AV_OPT_TYPE_CONST)
243  d = DEFAULT_NUMVAL(o_named);
244  else {
245  if (o->unit) {
246  for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
247  if (o_named->type == AV_OPT_TYPE_CONST &&
248  o_named->unit &&
249  !strcmp(o_named->unit, o->unit)) {
250  if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
251  av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
252  return AVERROR_PATCHWELCOME;
253  }
254  const_names [ci ] = o_named->name;
255  const_values[ci++] = DEFAULT_NUMVAL(o_named);
256  }
257  }
258  }
259  const_names [ci ] = "default";
260  const_values[ci++] = DEFAULT_NUMVAL(o);
261  const_names [ci ] = "max";
262  const_values[ci++] = o->max;
263  const_names [ci ] = "min";
264  const_values[ci++] = o->min;
265  const_names [ci ] = "none";
266  const_values[ci++] = 0;
267  const_names [ci ] = "all";
268  const_values[ci++] = ~0;
269  const_names [ci] = NULL;
270  const_values[ci] = 0;
271 
272  res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
273  const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
274  if (res < 0) {
275  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
276  return res;
277  }
278  }
279  }
280  if (o->type == AV_OPT_TYPE_FLAGS) {
281  read_number(o, dst, NULL, NULL, &intnum);
282  if (cmd == '+')
283  d = intnum | (int64_t)d;
284  else if (cmd == '-')
285  d = intnum &~(int64_t)d;
286  }
287 
288  if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
289  return ret;
290  val += i;
291  if (!i || !*val)
292  return 0;
293  }
294 
295  return 0;
296 }
297 
298 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
299 {
300  int ret;
301 
302  if (!val || !strcmp(val, "none")) {
303  dst[0] =
304  dst[1] = 0;
305  return 0;
306  }
307  ret = av_parse_video_size(dst, dst + 1, val);
308  if (ret < 0)
309  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
310  return ret;
311 }
312 
313 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
314 {
315  int ret;
316  if (!val) {
317  ret = AVERROR(EINVAL);
318  } else {
319  ret = av_parse_video_rate(dst, val);
320  }
321  if (ret < 0)
322  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
323  return ret;
324 }
325 
326 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
327 {
328  int ret;
329 
330  if (!val) {
331  return 0;
332  } else {
333  ret = av_parse_color(dst, val, -1, obj);
334  if (ret < 0)
335  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
336  return ret;
337  }
338  return 0;
339 }
340 
341 static const char *get_bool_name(int val)
342 {
343  if (val < 0)
344  return "auto";
345  return val ? "true" : "false";
346 }
347 
348 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
349 {
350  int n;
351 
352  if (!val)
353  return 0;
354 
355  if (!strcmp(val, "auto")) {
356  n = -1;
357  } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
358  n = 1;
359  } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
360  n = 0;
361  } else {
362  char *end = NULL;
363  n = strtol(val, &end, 10);
364  if (val + strlen(val) != end)
365  goto fail;
366  }
367 
368  if (n < o->min || n > o->max)
369  goto fail;
370 
371  *dst = n;
372  return 0;
373 
374 fail:
375  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
376  return AVERROR(EINVAL);
377 }
378 
379 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
380  int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
381 {
382  int fmt, min, max;
383 
384  if (!val || !strcmp(val, "none")) {
385  fmt = -1;
386  } else {
387  fmt = get_fmt(val);
388  if (fmt == -1) {
389  char *tail;
390  fmt = strtol(val, &tail, 0);
391  if (*tail || (unsigned)fmt >= fmt_nb) {
392  av_log(obj, AV_LOG_ERROR,
393  "Unable to parse option value \"%s\" as %s\n", val, desc);
394  return AVERROR(EINVAL);
395  }
396  }
397  }
398 
399  min = FFMAX(o->min, -1);
400  max = FFMIN(o->max, fmt_nb-1);
401 
402  // hack for compatibility with old ffmpeg
403  if(min == 0 && max == 0) {
404  min = -1;
405  max = fmt_nb-1;
406  }
407 
408  if (fmt < min || fmt > max) {
409  av_log(obj, AV_LOG_ERROR,
410  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
411  fmt, o->name, desc, min, max);
412  return AVERROR(ERANGE);
413  }
414 
415  *(int *)dst = fmt;
416  return 0;
417 }
418 
419 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
420 {
421  return set_string_fmt(obj, o, val, dst,
422  AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
423 }
424 
425 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
426 {
427  return set_string_fmt(obj, o, val, dst,
428  AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
429 }
430 
431 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
432 {
433  int ret = 0;
434  void *dst, *target_obj;
435  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
436  if (!o || !target_obj)
438  if (!val && (o->type != AV_OPT_TYPE_STRING &&
443  return AVERROR(EINVAL);
444 
445  if (o->flags & AV_OPT_FLAG_READONLY)
446  return AVERROR(EINVAL);
447 
448  dst = ((uint8_t *)target_obj) + o->offset;
449  switch (o->type) {
450  case AV_OPT_TYPE_BOOL:
451  return set_string_bool(obj, o, val, dst);
452  case AV_OPT_TYPE_STRING:
453  return set_string(obj, o, val, dst);
454  case AV_OPT_TYPE_BINARY:
455  return set_string_binary(obj, o, val, dst);
456  case AV_OPT_TYPE_FLAGS:
457  case AV_OPT_TYPE_INT:
458  case AV_OPT_TYPE_INT64:
459  case AV_OPT_TYPE_FLOAT:
460  case AV_OPT_TYPE_DOUBLE:
462  return set_string_number(obj, target_obj, o, val, dst);
464  return set_string_image_size(obj, o, val, dst);
465  case AV_OPT_TYPE_VIDEO_RATE: {
466  AVRational tmp;
467  ret = set_string_video_rate(obj, o, val, &tmp);
468  if (ret < 0)
469  return ret;
470  return write_number(obj, o, dst, 1, tmp.den, tmp.num);
471  }
473  return set_string_pixel_fmt(obj, o, val, dst);
475  return set_string_sample_fmt(obj, o, val, dst);
477  if (!val) {
478  *(int64_t *)dst = 0;
479  return 0;
480  } else {
481  if ((ret = av_parse_time(dst, val, 1)) < 0)
482  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
483  return ret;
484  }
485  break;
486  case AV_OPT_TYPE_COLOR:
487  return set_string_color(obj, o, val, dst);
489  if (!val || !strcmp(val, "none")) {
490  *(int64_t *)dst = 0;
491  } else {
492  int64_t cl = av_get_channel_layout(val);
493  if (!cl) {
494  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
495  ret = AVERROR(EINVAL);
496  }
497  *(int64_t *)dst = cl;
498  return ret;
499  }
500  break;
501  }
502 
503  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
504  return AVERROR(EINVAL);
505 }
506 
507 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
508 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
509  const char *val, vartype *name ## _out) \
510 { \
511  if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
512  return AVERROR(EINVAL); \
513  return set_string_number(obj, obj, o, val, name ## _out); \
514 }
515 
518 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
519 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
520 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
522 
523 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
524  int search_flags)
525 {
526  void *dst, *target_obj;
527  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
528 
529  if (!o || !target_obj)
531 
532  if (o->flags & AV_OPT_FLAG_READONLY)
533  return AVERROR(EINVAL);
534 
535  dst = ((uint8_t *)target_obj) + o->offset;
536  return write_number(obj, o, dst, num, den, intnum);
537 }
538 
539 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
540 {
541  return set_number(obj, name, 1, 1, val, search_flags);
542 }
543 
544 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
545 {
546  return set_number(obj, name, val, 1, 1, search_flags);
547 }
548 
549 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
550 {
551  return set_number(obj, name, val.num, val.den, 1, search_flags);
552 }
553 
554 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
555 {
556  void *target_obj;
557  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
558  uint8_t *ptr;
559  uint8_t **dst;
560  int *lendst;
561 
562  if (!o || !target_obj)
564 
566  return AVERROR(EINVAL);
567 
568  ptr = len ? av_malloc(len) : NULL;
569  if (len && !ptr)
570  return AVERROR(ENOMEM);
571 
572  dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
573  lendst = (int *)(dst + 1);
574 
575  av_free(*dst);
576  *dst = ptr;
577  *lendst = len;
578  if (len)
579  memcpy(ptr, val, len);
580 
581  return 0;
582 }
583 
584 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
585 {
586  void *target_obj;
587  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
588 
589  if (!o || !target_obj)
591  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
592  av_log(obj, AV_LOG_ERROR,
593  "The value set by option '%s' is not an image size.\n", o->name);
594  return AVERROR(EINVAL);
595  }
596  if (w<0 || h<0) {
597  av_log(obj, AV_LOG_ERROR,
598  "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
599  return AVERROR(EINVAL);
600  }
601  *(int *)(((uint8_t *)target_obj) + o->offset) = w;
602  *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
603  return 0;
604 }
605 
606 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
607 {
608  void *target_obj;
609  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
610 
611  if (!o || !target_obj)
613  if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
614  av_log(obj, AV_LOG_ERROR,
615  "The value set by option '%s' is not a video rate.\n", o->name);
616  return AVERROR(EINVAL);
617  }
618  if (val.num <= 0 || val.den <= 0)
619  return AVERROR(EINVAL);
620  return set_number(obj, name, val.num, val.den, 1, search_flags);
621 }
622 
623 static int set_format(void *obj, const char *name, int fmt, int search_flags,
624  enum AVOptionType type, const char *desc, int nb_fmts)
625 {
626  void *target_obj;
627  const AVOption *o = av_opt_find2(obj, name, NULL, 0,
628  search_flags, &target_obj);
629  int min, max;
630 
631  if (!o || !target_obj)
633  if (o->type != type) {
634  av_log(obj, AV_LOG_ERROR,
635  "The value set by option '%s' is not a %s format", name, desc);
636  return AVERROR(EINVAL);
637  }
638 
639  min = FFMAX(o->min, -1);
640  max = FFMIN(o->max, nb_fmts-1);
641 
642  if (fmt < min || fmt > max) {
643  av_log(obj, AV_LOG_ERROR,
644  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
645  fmt, name, desc, min, max);
646  return AVERROR(ERANGE);
647  }
648  *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
649  return 0;
650 }
651 
652 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
653 {
654  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
655 }
656 
657 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
658 {
659  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
660 }
661 
662 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
663 {
664  void *target_obj;
665  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
666 
667  if (!o || !target_obj)
669  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
670  av_log(obj, AV_LOG_ERROR,
671  "The value set by option '%s' is not a channel layout.\n", o->name);
672  return AVERROR(EINVAL);
673  }
674  *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
675  return 0;
676 }
677 
678 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
679  int search_flags)
680 {
681  void *target_obj;
682  AVDictionary **dst;
683  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
684 
685  if (!o || !target_obj)
687  if (o->flags & AV_OPT_FLAG_READONLY)
688  return AVERROR(EINVAL);
689 
690  dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
691  av_dict_free(dst);
692  av_dict_copy(dst, val, 0);
693 
694  return 0;
695 }
696 
697 static void format_duration(char *buf, size_t size, int64_t d)
698 {
699  char *e;
700 
701  av_assert0(size >= 25);
702  if (d < 0 && d != INT64_MIN) {
703  *(buf++) = '-';
704  size--;
705  d = -d;
706  }
707  if (d == INT64_MAX)
708  snprintf(buf, size, "INT64_MAX");
709  else if (d == INT64_MIN)
710  snprintf(buf, size, "INT64_MIN");
711  else if (d > (int64_t)3600*1000000)
712  snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
713  (int)((d / 60000000) % 60),
714  (int)((d / 1000000) % 60),
715  (int)(d % 1000000));
716  else if (d > 60*1000000)
717  snprintf(buf, size, "%d:%02d.%06d",
718  (int)(d / 60000000),
719  (int)((d / 1000000) % 60),
720  (int)(d % 1000000));
721  else
722  snprintf(buf, size, "%d.%06d",
723  (int)(d / 1000000),
724  (int)(d % 1000000));
725  e = buf + strlen(buf);
726  while (e > buf && e[-1] == '0')
727  *(--e) = 0;
728  if (e > buf && e[-1] == '.')
729  *(--e) = 0;
730 }
731 
732 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
733 {
734  void *dst, *target_obj;
735  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
736  uint8_t *bin, buf[128];
737  int len, i, ret;
738  int64_t i64;
739 
740  if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
742 
743  dst = (uint8_t *)target_obj + o->offset;
744 
745  buf[0] = 0;
746  switch (o->type) {
747  case AV_OPT_TYPE_BOOL:
748  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
749  break;
750  case AV_OPT_TYPE_FLAGS:
751  ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
752  break;
753  case AV_OPT_TYPE_INT:
754  ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
755  break;
756  case AV_OPT_TYPE_INT64:
757  ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
758  break;
759  case AV_OPT_TYPE_FLOAT:
760  ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
761  break;
762  case AV_OPT_TYPE_DOUBLE:
763  ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
764  break;
767  ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
768  break;
769  case AV_OPT_TYPE_CONST:
770  ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
771  break;
772  case AV_OPT_TYPE_STRING:
773  if (*(uint8_t **)dst) {
774  *out_val = av_strdup(*(uint8_t **)dst);
775  } else if (search_flags & AV_OPT_ALLOW_NULL) {
776  *out_val = NULL;
777  return 0;
778  } else {
779  *out_val = av_strdup("");
780  }
781  return *out_val ? 0 : AVERROR(ENOMEM);
782  case AV_OPT_TYPE_BINARY:
783  if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
784  *out_val = NULL;
785  return 0;
786  }
787  len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
788  if ((uint64_t)len * 2 + 1 > INT_MAX)
789  return AVERROR(EINVAL);
790  if (!(*out_val = av_malloc(len * 2 + 1)))
791  return AVERROR(ENOMEM);
792  if (!len) {
793  *out_val[0] = '\0';
794  return 0;
795  }
796  bin = *(uint8_t **)dst;
797  for (i = 0; i < len; i++)
798  snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
799  return 0;
801  ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
802  break;
804  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
805  break;
807  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
808  break;
810  i64 = *(int64_t *)dst;
811  format_duration(buf, sizeof(buf), i64);
812  ret = strlen(buf); // no overflow possible, checked by an assert
813  break;
814  case AV_OPT_TYPE_COLOR:
815  ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
816  (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
817  (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
818  break;
820  i64 = *(int64_t *)dst;
821  ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
822  break;
823  default:
824  return AVERROR(EINVAL);
825  }
826 
827  if (ret >= sizeof(buf))
828  return AVERROR(EINVAL);
829  *out_val = av_strdup(buf);
830  return *out_val ? 0 : AVERROR(ENOMEM);
831 }
832 
833 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
834  int search_flags)
835 {
836  void *dst, *target_obj;
837  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
838  if (!o || !target_obj)
839  goto error;
840 
841  dst = ((uint8_t *)target_obj) + o->offset;
842 
843  if (o_out) *o_out= o;
844 
845  return read_number(o, dst, num, den, intnum);
846 
847 error:
848  *den =
849  *intnum = 0;
850  return -1;
851 }
852 
853 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
854 {
855  int64_t intnum = 1;
856  double num = 1;
857  int ret, den = 1;
858 
859  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
860  return ret;
861  *out_val = num * intnum / den;
862  return 0;
863 }
864 
865 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
866 {
867  int64_t intnum = 1;
868  double num = 1;
869  int ret, den = 1;
870 
871  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
872  return ret;
873  *out_val = num * intnum / den;
874  return 0;
875 }
876 
877 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
878 {
879  int64_t intnum = 1;
880  double num = 1;
881  int ret, den = 1;
882 
883  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
884  return ret;
885 
886  if (num == 1.0 && (int)intnum == intnum)
887  *out_val = (AVRational){intnum, den};
888  else
889  *out_val = av_d2q(num*intnum/den, 1<<24);
890  return 0;
891 }
892 
893 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
894 {
895  void *dst, *target_obj;
896  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
897  if (!o || !target_obj)
899  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
900  av_log(obj, AV_LOG_ERROR,
901  "The value for option '%s' is not an image size.\n", name);
902  return AVERROR(EINVAL);
903  }
904 
905  dst = ((uint8_t*)target_obj) + o->offset;
906  if (w_out) *w_out = *(int *)dst;
907  if (h_out) *h_out = *((int *)dst+1);
908  return 0;
909 }
910 
911 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
912 {
913  int64_t intnum = 1;
914  double num = 1;
915  int ret, den = 1;
916 
917  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
918  return ret;
919 
920  if (num == 1.0 && (int)intnum == intnum)
921  *out_val = (AVRational) { intnum, den };
922  else
923  *out_val = av_d2q(num * intnum / den, 1 << 24);
924  return 0;
925 }
926 
927 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
928  enum AVOptionType type, const char *desc)
929 {
930  void *dst, *target_obj;
931  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
932  if (!o || !target_obj)
934  if (o->type != type) {
935  av_log(obj, AV_LOG_ERROR,
936  "The value for option '%s' is not a %s format.\n", desc, name);
937  return AVERROR(EINVAL);
938  }
939 
940  dst = ((uint8_t*)target_obj) + o->offset;
941  *out_fmt = *(int *)dst;
942  return 0;
943 }
944 
945 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
946 {
947  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
948 }
949 
950 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
951 {
952  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
953 }
954 
955 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
956 {
957  void *dst, *target_obj;
958  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
959  if (!o || !target_obj)
961  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
962  av_log(obj, AV_LOG_ERROR,
963  "The value for option '%s' is not a channel layout.\n", name);
964  return AVERROR(EINVAL);
965  }
966 
967  dst = ((uint8_t*)target_obj) + o->offset;
968  *cl = *(int64_t *)dst;
969  return 0;
970 }
971 
972 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
973 {
974  void *target_obj;
975  AVDictionary *src;
976  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
977 
978  if (!o || !target_obj)
980  if (o->type != AV_OPT_TYPE_DICT)
981  return AVERROR(EINVAL);
982 
983  src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
984  av_dict_copy(out_val, src, 0);
985 
986  return 0;
987 }
988 
989 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
990 {
991  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
992  const AVOption *flag = av_opt_find(obj, flag_name,
993  field ? field->unit : NULL, 0, 0);
994  int64_t res;
995 
996  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
997  av_opt_get_int(obj, field_name, 0, &res) < 0)
998  return 0;
999  return res & flag->default_val.i64;
1000 }
1001 
1002 static void log_value(void *av_log_obj, int level, double d)
1003 {
1004  if (d == INT_MAX) {
1005  av_log(av_log_obj, level, "INT_MAX");
1006  } else if (d == INT_MIN) {
1007  av_log(av_log_obj, level, "INT_MIN");
1008  } else if (d == UINT32_MAX) {
1009  av_log(av_log_obj, level, "UINT32_MAX");
1010  } else if (d == (double)INT64_MAX) {
1011  av_log(av_log_obj, level, "I64_MAX");
1012  } else if (d == INT64_MIN) {
1013  av_log(av_log_obj, level, "I64_MIN");
1014  } else if (d == FLT_MAX) {
1015  av_log(av_log_obj, level, "FLT_MAX");
1016  } else if (d == FLT_MIN) {
1017  av_log(av_log_obj, level, "FLT_MIN");
1018  } else if (d == -FLT_MAX) {
1019  av_log(av_log_obj, level, "-FLT_MAX");
1020  } else if (d == -FLT_MIN) {
1021  av_log(av_log_obj, level, "-FLT_MIN");
1022  } else if (d == DBL_MAX) {
1023  av_log(av_log_obj, level, "DBL_MAX");
1024  } else if (d == DBL_MIN) {
1025  av_log(av_log_obj, level, "DBL_MIN");
1026  } else if (d == -DBL_MAX) {
1027  av_log(av_log_obj, level, "-DBL_MAX");
1028  } else if (d == -DBL_MIN) {
1029  av_log(av_log_obj, level, "-DBL_MIN");
1030  } else {
1031  av_log(av_log_obj, level, "%g", d);
1032  }
1033 }
1034 
1035 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1036 {
1037  const AVOption *opt = NULL;
1038 
1039  if (!unit)
1040  return NULL;
1041  while ((opt = av_opt_next(obj, opt)))
1042  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1043  opt->default_val.i64 == value)
1044  return opt->name;
1045  return NULL;
1046 }
1047 
1048 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1049 {
1050  const AVOption *opt = NULL;
1051  char flags[512];
1052 
1053  flags[0] = 0;
1054  if (!unit)
1055  return NULL;
1056  while ((opt = av_opt_next(obj, opt))) {
1057  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1058  opt->default_val.i64 & value) {
1059  if (flags[0])
1060  av_strlcatf(flags, sizeof(flags), "+");
1061  av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1062  }
1063  }
1064  if (flags[0])
1065  return av_strdup(flags);
1066  return NULL;
1067 }
1068 
1069 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1070  int req_flags, int rej_flags)
1071 {
1072  const AVOption *opt = NULL;
1073  AVOptionRanges *r;
1074  int i;
1075 
1076  while ((opt = av_opt_next(obj, opt))) {
1077  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1078  continue;
1079 
1080  /* Don't print CONST's on level one.
1081  * Don't print anything but CONST's on level two.
1082  * Only print items from the requested unit.
1083  */
1084  if (!unit && opt->type == AV_OPT_TYPE_CONST)
1085  continue;
1086  else if (unit && opt->type != AV_OPT_TYPE_CONST)
1087  continue;
1088  else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1089  continue;
1090  else if (unit && opt->type == AV_OPT_TYPE_CONST)
1091  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1092  else
1093  av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1094  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1095  opt->name);
1096 
1097  switch (opt->type) {
1098  case AV_OPT_TYPE_FLAGS:
1099  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1100  break;
1101  case AV_OPT_TYPE_INT:
1102  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1103  break;
1104  case AV_OPT_TYPE_INT64:
1105  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1106  break;
1107  case AV_OPT_TYPE_DOUBLE:
1108  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1109  break;
1110  case AV_OPT_TYPE_FLOAT:
1111  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1112  break;
1113  case AV_OPT_TYPE_STRING:
1114  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1115  break;
1116  case AV_OPT_TYPE_RATIONAL:
1117  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1118  break;
1119  case AV_OPT_TYPE_BINARY:
1120  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1121  break;
1123  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1124  break;
1126  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1127  break;
1128  case AV_OPT_TYPE_PIXEL_FMT:
1129  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1130  break;
1132  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1133  break;
1134  case AV_OPT_TYPE_DURATION:
1135  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1136  break;
1137  case AV_OPT_TYPE_COLOR:
1138  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1139  break;
1141  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1142  break;
1143  case AV_OPT_TYPE_BOOL:
1144  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1145  break;
1146  case AV_OPT_TYPE_CONST:
1147  default:
1148  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1149  break;
1150  }
1151  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1152  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1153  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1154  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1155  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1156  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1157  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1158  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1159 
1160  if (opt->help)
1161  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1162 
1163  if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1164  switch (opt->type) {
1165  case AV_OPT_TYPE_INT:
1166  case AV_OPT_TYPE_INT64:
1167  case AV_OPT_TYPE_DOUBLE:
1168  case AV_OPT_TYPE_FLOAT:
1169  case AV_OPT_TYPE_RATIONAL:
1170  for (i = 0; i < r->nb_ranges; i++) {
1171  av_log(av_log_obj, AV_LOG_INFO, " (from ");
1172  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1173  av_log(av_log_obj, AV_LOG_INFO, " to ");
1174  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1175  av_log(av_log_obj, AV_LOG_INFO, ")");
1176  }
1177  break;
1178  }
1179  av_opt_freep_ranges(&r);
1180  }
1181 
1182  if (opt->type != AV_OPT_TYPE_CONST &&
1183  opt->type != AV_OPT_TYPE_BINARY &&
1184  !((opt->type == AV_OPT_TYPE_COLOR ||
1185  opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1186  opt->type == AV_OPT_TYPE_STRING ||
1187  opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1188  !opt->default_val.str)) {
1189  av_log(av_log_obj, AV_LOG_INFO, " (default ");
1190  switch (opt->type) {
1191  case AV_OPT_TYPE_BOOL:
1192  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1193  break;
1194  case AV_OPT_TYPE_FLAGS: {
1195  char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1196  if (def_flags) {
1197  av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1198  av_freep(&def_flags);
1199  } else {
1200  av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1201  }
1202  break;
1203  }
1204  case AV_OPT_TYPE_DURATION: {
1205  char buf[25];
1206  format_duration(buf, sizeof(buf), opt->default_val.i64);
1207  av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1208  break;
1209  }
1210  case AV_OPT_TYPE_INT:
1211  case AV_OPT_TYPE_INT64: {
1212  const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1213  if (def_const)
1214  av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1215  else
1216  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1217  break;
1218  }
1219  case AV_OPT_TYPE_DOUBLE:
1220  case AV_OPT_TYPE_FLOAT:
1221  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1222  break;
1223  case AV_OPT_TYPE_RATIONAL: {
1224  AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1225  av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1226  break;
1227  case AV_OPT_TYPE_PIXEL_FMT:
1228  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1229  break;
1231  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1232  break;
1233  case AV_OPT_TYPE_COLOR:
1235  case AV_OPT_TYPE_STRING:
1237  av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1238  break;
1240  av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1241  break;
1242  }
1243  av_log(av_log_obj, AV_LOG_INFO, ")");
1244  }
1245 
1246  av_log(av_log_obj, AV_LOG_INFO, "\n");
1247  if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1248  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1249  }
1250 }
1251 
1252 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1253 {
1254  if (!obj)
1255  return -1;
1256 
1257  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1258 
1259  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1260 
1261  return 0;
1262 }
1263 
1265 {
1266  av_opt_set_defaults2(s, 0, 0);
1267 }
1268 
1269 void av_opt_set_defaults2(void *s, int mask, int flags)
1270 {
1271  const AVOption *opt = NULL;
1272  while ((opt = av_opt_next(s, opt))) {
1273  void *dst = ((uint8_t*)s) + opt->offset;
1274 
1275  if ((opt->flags & mask) != flags)
1276  continue;
1277 
1278  if (opt->flags & AV_OPT_FLAG_READONLY)
1279  continue;
1280 
1281  switch (opt->type) {
1282  case AV_OPT_TYPE_CONST:
1283  /* Nothing to be done here */
1284  break;
1285  case AV_OPT_TYPE_BOOL:
1286  case AV_OPT_TYPE_FLAGS:
1287  case AV_OPT_TYPE_INT:
1288  case AV_OPT_TYPE_INT64:
1289  case AV_OPT_TYPE_DURATION:
1291  case AV_OPT_TYPE_PIXEL_FMT:
1293  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1294  break;
1295  case AV_OPT_TYPE_DOUBLE:
1296  case AV_OPT_TYPE_FLOAT: {
1297  double val;
1298  val = opt->default_val.dbl;
1299  write_number(s, opt, dst, val, 1, 1);
1300  }
1301  break;
1302  case AV_OPT_TYPE_RATIONAL: {
1303  AVRational val;
1304  val = av_d2q(opt->default_val.dbl, INT_MAX);
1305  write_number(s, opt, dst, 1, val.den, val.num);
1306  }
1307  break;
1308  case AV_OPT_TYPE_COLOR:
1309  set_string_color(s, opt, opt->default_val.str, dst);
1310  break;
1311  case AV_OPT_TYPE_STRING:
1312  set_string(s, opt, opt->default_val.str, dst);
1313  break;
1315  set_string_image_size(s, opt, opt->default_val.str, dst);
1316  break;
1318  set_string_video_rate(s, opt, opt->default_val.str, dst);
1319  break;
1320  case AV_OPT_TYPE_BINARY:
1321  set_string_binary(s, opt, opt->default_val.str, dst);
1322  break;
1323  case AV_OPT_TYPE_DICT:
1324  /* Cannot set defaults for these types */
1325  break;
1326  default:
1327  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1328  opt->type, opt->name);
1329  }
1330  }
1331 }
1332 
1333 /**
1334  * Store the value in the field in ctx that is named like key.
1335  * ctx must be an AVClass context, storing is done using AVOptions.
1336  *
1337  * @param buf the string to parse, buf will be updated to point at the
1338  * separator just after the parsed key/value pair
1339  * @param key_val_sep a 0-terminated list of characters used to
1340  * separate key from value
1341  * @param pairs_sep a 0-terminated list of characters used to separate
1342  * two pairs from each other
1343  * @return 0 if the key/value pair has been successfully parsed and
1344  * set, or a negative value corresponding to an AVERROR code in case
1345  * of error:
1346  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1347  * the error code issued by av_opt_set() if the key/value pair
1348  * cannot be set
1349  */
1350 static int parse_key_value_pair(void *ctx, const char **buf,
1351  const char *key_val_sep, const char *pairs_sep)
1352 {
1353  char *key = av_get_token(buf, key_val_sep);
1354  char *val;
1355  int ret;
1356 
1357  if (!key)
1358  return AVERROR(ENOMEM);
1359 
1360  if (*key && strspn(*buf, key_val_sep)) {
1361  (*buf)++;
1362  val = av_get_token(buf, pairs_sep);
1363  if (!val) {
1364  av_freep(&key);
1365  return AVERROR(ENOMEM);
1366  }
1367  } else {
1368  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1369  av_free(key);
1370  return AVERROR(EINVAL);
1371  }
1372 
1373  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1374 
1375  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1376  if (ret == AVERROR_OPTION_NOT_FOUND)
1377  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1378 
1379  av_free(key);
1380  av_free(val);
1381  return ret;
1382 }
1383 
1384 int av_set_options_string(void *ctx, const char *opts,
1385  const char *key_val_sep, const char *pairs_sep)
1386 {
1387  int ret, count = 0;
1388 
1389  if (!opts)
1390  return 0;
1391 
1392  while (*opts) {
1393  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1394  return ret;
1395  count++;
1396 
1397  if (*opts)
1398  opts++;
1399  }
1400 
1401  return count;
1402 }
1403 
1404 #define WHITESPACES " \n\t\r"
1405 
1406 static int is_key_char(char c)
1407 {
1408  return (unsigned)((c | 32) - 'a') < 26 ||
1409  (unsigned)(c - '0') < 10 ||
1410  c == '-' || c == '_' || c == '/' || c == '.';
1411 }
1412 
1413 /**
1414  * Read a key from a string.
1415  *
1416  * The key consists of is_key_char characters and must be terminated by a
1417  * character from the delim string; spaces are ignored.
1418  *
1419  * @return 0 for success (even with ellipsis), <0 for failure
1420  */
1421 static int get_key(const char **ropts, const char *delim, char **rkey)
1422 {
1423  const char *opts = *ropts;
1424  const char *key_start, *key_end;
1425 
1426  key_start = opts += strspn(opts, WHITESPACES);
1427  while (is_key_char(*opts))
1428  opts++;
1429  key_end = opts;
1430  opts += strspn(opts, WHITESPACES);
1431  if (!*opts || !strchr(delim, *opts))
1432  return AVERROR(EINVAL);
1433  opts++;
1434  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1435  return AVERROR(ENOMEM);
1436  memcpy(*rkey, key_start, key_end - key_start);
1437  (*rkey)[key_end - key_start] = 0;
1438  *ropts = opts;
1439  return 0;
1440 }
1441 
1442 int av_opt_get_key_value(const char **ropts,
1443  const char *key_val_sep, const char *pairs_sep,
1444  unsigned flags,
1445  char **rkey, char **rval)
1446 {
1447  int ret;
1448  char *key = NULL, *val;
1449  const char *opts = *ropts;
1450 
1451  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1452  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1453  return AVERROR(EINVAL);
1454  if (!(val = av_get_token(&opts, pairs_sep))) {
1455  av_free(key);
1456  return AVERROR(ENOMEM);
1457  }
1458  *ropts = opts;
1459  *rkey = key;
1460  *rval = val;
1461  return 0;
1462 }
1463 
1464 int av_opt_set_from_string(void *ctx, const char *opts,
1465  const char *const *shorthand,
1466  const char *key_val_sep, const char *pairs_sep)
1467 {
1468  int ret, count = 0;
1469  const char *dummy_shorthand = NULL;
1470  char *av_uninit(parsed_key), *av_uninit(value);
1471  const char *key;
1472 
1473  if (!opts)
1474  return 0;
1475  if (!shorthand)
1476  shorthand = &dummy_shorthand;
1477 
1478  while (*opts) {
1479  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1480  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1481  &parsed_key, &value);
1482  if (ret < 0) {
1483  if (ret == AVERROR(EINVAL))
1484  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1485  else
1486  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1487  av_err2str(ret));
1488  return ret;
1489  }
1490  if (*opts)
1491  opts++;
1492  if (parsed_key) {
1493  key = parsed_key;
1494  while (*shorthand) /* discard all remaining shorthand */
1495  shorthand++;
1496  } else {
1497  key = *(shorthand++);
1498  }
1499 
1500  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1501  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1502  if (ret == AVERROR_OPTION_NOT_FOUND)
1503  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1504  av_free(value);
1505  av_free(parsed_key);
1506  return ret;
1507  }
1508 
1509  av_free(value);
1510  av_free(parsed_key);
1511  count++;
1512  }
1513  return count;
1514 }
1515 
1516 void av_opt_free(void *obj)
1517 {
1518  const AVOption *o = NULL;
1519  while ((o = av_opt_next(obj, o))) {
1520  switch (o->type) {
1521  case AV_OPT_TYPE_STRING:
1522  case AV_OPT_TYPE_BINARY:
1523  av_freep((uint8_t *)obj + o->offset);
1524  break;
1525 
1526  case AV_OPT_TYPE_DICT:
1527  av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1528  break;
1529 
1530  default:
1531  break;
1532  }
1533  }
1534 }
1535 
1536 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1537 {
1538  AVDictionaryEntry *t = NULL;
1539  AVDictionary *tmp = NULL;
1540  int ret = 0;
1541 
1542  if (!options)
1543  return 0;
1544 
1545  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1546  ret = av_opt_set(obj, t->key, t->value, search_flags);
1547  if (ret == AVERROR_OPTION_NOT_FOUND)
1548  ret = av_dict_set(&tmp, t->key, t->value, 0);
1549  if (ret < 0) {
1550  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1551  av_dict_free(&tmp);
1552  return ret;
1553  }
1554  ret = 0;
1555  }
1556  av_dict_free(options);
1557  *options = tmp;
1558  return ret;
1559 }
1560 
1562 {
1563  return av_opt_set_dict2(obj, options, 0);
1564 }
1565 
1566 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1567  int opt_flags, int search_flags)
1568 {
1569  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1570 }
1571 
1572 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1573  int opt_flags, int search_flags, void **target_obj)
1574 {
1575  const AVClass *c;
1576  const AVOption *o = NULL;
1577 
1578  if(!obj)
1579  return NULL;
1580 
1581  c= *(AVClass**)obj;
1582 
1583  if (!c)
1584  return NULL;
1585 
1586  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1587  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1588  const AVClass *child = NULL;
1589  while (child = av_opt_child_class_next(c, child))
1590  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1591  return o;
1592  } else {
1593  void *child = NULL;
1594  while (child = av_opt_child_next(obj, child))
1595  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1596  return o;
1597  }
1598  }
1599 
1600  while (o = av_opt_next(obj, o)) {
1601  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1602  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1603  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1604  if (target_obj) {
1605  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1606  *target_obj = obj;
1607  else
1608  *target_obj = NULL;
1609  }
1610  return o;
1611  }
1612  }
1613  return NULL;
1614 }
1615 
1616 void *av_opt_child_next(void *obj, void *prev)
1617 {
1618  const AVClass *c = *(AVClass **)obj;
1619  if (c->child_next)
1620  return c->child_next(obj, prev);
1621  return NULL;
1622 }
1623 
1624 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1625 {
1626  if (parent->child_class_next)
1627  return parent->child_class_next(prev);
1628  return NULL;
1629 }
1630 
1631 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1632 {
1633  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1634  if(!opt)
1635  return NULL;
1636  return (uint8_t*)obj + opt->offset;
1637 }
1638 
1639 static int opt_size(enum AVOptionType type)
1640 {
1641  switch(type) {
1642  case AV_OPT_TYPE_BOOL:
1643  case AV_OPT_TYPE_INT:
1644  case AV_OPT_TYPE_FLAGS:
1645  return sizeof(int);
1646  case AV_OPT_TYPE_DURATION:
1648  case AV_OPT_TYPE_INT64:
1649  return sizeof(int64_t);
1650  case AV_OPT_TYPE_DOUBLE:
1651  return sizeof(double);
1652  case AV_OPT_TYPE_FLOAT:
1653  return sizeof(float);
1654  case AV_OPT_TYPE_STRING:
1655  return sizeof(uint8_t*);
1657  case AV_OPT_TYPE_RATIONAL:
1658  return sizeof(AVRational);
1659  case AV_OPT_TYPE_BINARY:
1660  return sizeof(uint8_t*) + sizeof(int);
1662  return sizeof(int[2]);
1663  case AV_OPT_TYPE_PIXEL_FMT:
1664  return sizeof(enum AVPixelFormat);
1666  return sizeof(enum AVSampleFormat);
1667  case AV_OPT_TYPE_COLOR:
1668  return 4;
1669  }
1670  return AVERROR(EINVAL);
1671 }
1672 
1673 int av_opt_copy(void *dst, const void *src)
1674 {
1675  const AVOption *o = NULL;
1676  const AVClass *c;
1677  int ret = 0;
1678 
1679  if (!src)
1680  return AVERROR(EINVAL);
1681 
1682  c = *(AVClass **)src;
1683  if (!c || c != *(AVClass **)dst)
1684  return AVERROR(EINVAL);
1685 
1686  while ((o = av_opt_next(src, o))) {
1687  void *field_dst = (uint8_t *)dst + o->offset;
1688  void *field_src = (uint8_t *)src + o->offset;
1689  uint8_t **field_dst8 = (uint8_t **)field_dst;
1690  uint8_t **field_src8 = (uint8_t **)field_src;
1691 
1692  if (o->type == AV_OPT_TYPE_STRING) {
1693  if (*field_dst8 != *field_src8)
1694  av_freep(field_dst8);
1695  *field_dst8 = av_strdup(*field_src8);
1696  if (*field_src8 && !*field_dst8)
1697  ret = AVERROR(ENOMEM);
1698  } else if (o->type == AV_OPT_TYPE_BINARY) {
1699  int len = *(int *)(field_src8 + 1);
1700  if (*field_dst8 != *field_src8)
1701  av_freep(field_dst8);
1702  *field_dst8 = av_memdup(*field_src8, len);
1703  if (len && !*field_dst8) {
1704  ret = AVERROR(ENOMEM);
1705  len = 0;
1706  }
1707  *(int *)(field_dst8 + 1) = len;
1708  } else if (o->type == AV_OPT_TYPE_CONST) {
1709  // do nothing
1710  } else if (o->type == AV_OPT_TYPE_DICT) {
1711  AVDictionary **sdict = (AVDictionary **) field_src;
1712  AVDictionary **ddict = (AVDictionary **) field_dst;
1713  if (*sdict != *ddict)
1714  av_dict_free(ddict);
1715  *ddict = NULL;
1716  av_dict_copy(ddict, *sdict, 0);
1717  if (av_dict_count(*sdict) != av_dict_count(*ddict))
1718  ret = AVERROR(ENOMEM);
1719  } else {
1720  int size = opt_size(o->type);
1721  if (size < 0)
1722  ret = size;
1723  else
1724  memcpy(field_dst, field_src, size);
1725  }
1726  }
1727  return ret;
1728 }
1729 
1730 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1731 {
1732  int ret;
1733  const AVClass *c = *(AVClass**)obj;
1734  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1735 
1736  if (c->version > (52 << 16 | 11 << 8))
1737  callback = c->query_ranges;
1738 
1739  if (!callback)
1741 
1742  ret = callback(ranges_arg, obj, key, flags);
1743  if (ret >= 0) {
1744  if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1745  ret = 1;
1746  (*ranges_arg)->nb_components = ret;
1747  }
1748  return ret;
1749 }
1750 
1751 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1752 {
1753  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1754  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1755  AVOptionRange *range = av_mallocz(sizeof(*range));
1756  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1757  int ret;
1758 
1759  *ranges_arg = NULL;
1760 
1761  if (!ranges || !range || !range_array || !field) {
1762  ret = AVERROR(ENOMEM);
1763  goto fail;
1764  }
1765 
1766  ranges->range = range_array;
1767  ranges->range[0] = range;
1768  ranges->nb_ranges = 1;
1769  ranges->nb_components = 1;
1770  range->is_range = 1;
1771  range->value_min = field->min;
1772  range->value_max = field->max;
1773 
1774  switch (field->type) {
1775  case AV_OPT_TYPE_BOOL:
1776  case AV_OPT_TYPE_INT:
1777  case AV_OPT_TYPE_INT64:
1778  case AV_OPT_TYPE_PIXEL_FMT:
1780  case AV_OPT_TYPE_FLOAT:
1781  case AV_OPT_TYPE_DOUBLE:
1782  case AV_OPT_TYPE_DURATION:
1783  case AV_OPT_TYPE_COLOR:
1785  break;
1786  case AV_OPT_TYPE_STRING:
1787  range->component_min = 0;
1788  range->component_max = 0x10FFFF; // max unicode value
1789  range->value_min = -1;
1790  range->value_max = INT_MAX;
1791  break;
1792  case AV_OPT_TYPE_RATIONAL:
1793  range->component_min = INT_MIN;
1794  range->component_max = INT_MAX;
1795  break;
1797  range->component_min = 0;
1798  range->component_max = INT_MAX/128/8;
1799  range->value_min = 0;
1800  range->value_max = INT_MAX/8;
1801  break;
1803  range->component_min = 1;
1804  range->component_max = INT_MAX;
1805  range->value_min = 1;
1806  range->value_max = INT_MAX;
1807  break;
1808  default:
1809  ret = AVERROR(ENOSYS);
1810  goto fail;
1811  }
1812 
1813  *ranges_arg = ranges;
1814  return 1;
1815 fail:
1816  av_free(ranges);
1817  av_free(range);
1818  av_free(range_array);
1819  return ret;
1820 }
1821 
1823 {
1824  int i;
1825  AVOptionRanges *ranges = *rangesp;
1826 
1827  if (!ranges)
1828  return;
1829 
1830  for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1831  AVOptionRange *range = ranges->range[i];
1832  if (range) {
1833  av_freep(&range->str);
1834  av_freep(&ranges->range[i]);
1835  }
1836  }
1837  av_freep(&ranges->range);
1838  av_freep(rangesp);
1839 }
1840 
1841 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1842 {
1843  int64_t i64;
1844  double d, d2;
1845  float f;
1846  AVRational q;
1847  int ret, w, h;
1848  char *str;
1849  void *dst;
1850 
1851  if (!o || !obj)
1852  return AVERROR(EINVAL);
1853 
1854  dst = ((uint8_t*)obj) + o->offset;
1855 
1856  switch (o->type) {
1857  case AV_OPT_TYPE_CONST:
1858  return 1;
1859  case AV_OPT_TYPE_BOOL:
1860  case AV_OPT_TYPE_FLAGS:
1861  case AV_OPT_TYPE_PIXEL_FMT:
1863  case AV_OPT_TYPE_INT:
1865  case AV_OPT_TYPE_DURATION:
1866  case AV_OPT_TYPE_INT64:
1867  read_number(o, dst, NULL, NULL, &i64);
1868  return o->default_val.i64 == i64;
1869  case AV_OPT_TYPE_STRING:
1870  str = *(char **)dst;
1871  if (str == o->default_val.str) //2 NULLs
1872  return 1;
1873  if (!str || !o->default_val.str) //1 NULL
1874  return 0;
1875  return !strcmp(str, o->default_val.str);
1876  case AV_OPT_TYPE_DOUBLE:
1877  read_number(o, dst, &d, NULL, NULL);
1878  return o->default_val.dbl == d;
1879  case AV_OPT_TYPE_FLOAT:
1880  read_number(o, dst, &d, NULL, NULL);
1881  f = o->default_val.dbl;
1882  d2 = f;
1883  return d2 == d;
1884  case AV_OPT_TYPE_RATIONAL:
1885  q = av_d2q(o->default_val.dbl, INT_MAX);
1886  return !av_cmp_q(*(AVRational*)dst, q);
1887  case AV_OPT_TYPE_BINARY: {
1888  struct {
1889  uint8_t *data;
1890  int size;
1891  } tmp = {0};
1892  int opt_size = *(int *)((void **)dst + 1);
1893  void *opt_ptr = *(void **)dst;
1894  if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1895  return 1;
1896  if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1897  return 0;
1898  if (opt_size != strlen(o->default_val.str) / 2)
1899  return 0;
1900  ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1901  if (!ret)
1902  ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1903  av_free(tmp.data);
1904  return ret;
1905  }
1906  case AV_OPT_TYPE_DICT:
1907  /* Binary and dict have not default support yet. Any pointer is not default. */
1908  return !!(*(void **)dst);
1910  if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1911  w = h = 0;
1912  else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1913  return ret;
1914  return (w == *(int *)dst) && (h == *((int *)dst+1));
1916  q = (AVRational){0, 0};
1917  if (o->default_val.str) {
1918  if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1919  return ret;
1920  }
1921  return !av_cmp_q(*(AVRational*)dst, q);
1922  case AV_OPT_TYPE_COLOR: {
1923  uint8_t color[4] = {0, 0, 0, 0};
1924  if (o->default_val.str) {
1925  if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1926  return ret;
1927  }
1928  return !memcmp(color, dst, sizeof(color));
1929  }
1930  default:
1931  av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1932  break;
1933  }
1934  return AVERROR_PATCHWELCOME;
1935 }
1936 
1937 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1938 {
1939  const AVOption *o;
1940  void *target;
1941  if (!obj)
1942  return AVERROR(EINVAL);
1943  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1944  if (!o)
1945  return AVERROR_OPTION_NOT_FOUND;
1946  return av_opt_is_set_to_default(target, o);
1947 }
1948 
1949 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1950  const char key_val_sep, const char pairs_sep)
1951 {
1952  const AVOption *o = NULL;
1953  uint8_t *buf;
1954  AVBPrint bprint;
1955  int ret, cnt = 0;
1956  const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1957 
1958  if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1959  pairs_sep == '\\' || key_val_sep == '\\') {
1960  av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1961  return AVERROR(EINVAL);
1962  }
1963 
1964  if (!obj || !buffer)
1965  return AVERROR(EINVAL);
1966 
1967  *buffer = NULL;
1969 
1970  while (o = av_opt_next(obj, o)) {
1971  if (o->type == AV_OPT_TYPE_CONST)
1972  continue;
1973  if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1974  continue;
1975  else if (((o->flags & opt_flags) != opt_flags))
1976  continue;
1977  if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1978  continue;
1979  if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1980  av_bprint_finalize(&bprint, NULL);
1981  return ret;
1982  }
1983  if (buf) {
1984  if (cnt++)
1985  av_bprint_append_data(&bprint, &pairs_sep, 1);
1986  av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1987  av_bprint_append_data(&bprint, &key_val_sep, 1);
1988  av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1989  av_freep(&buf);
1990  }
1991  }
1992  av_bprint_finalize(&bprint, buffer);
1993  return 0;
1994 }
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:972
#define NULL
Definition: coverity.c:32
static const char * get_bool_name(int val)
Definition: opt.c:341
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
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:72
static char * get_opt_flags_string(void *obj, const char *unit, int64_t value)
Definition: opt.c:1048
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:549
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 intended for exporting values to the caller.
Definition: opt.h:286
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:282
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:298
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
Definition: opt.c:1616
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
const char * desc
Definition: nvenc.c:89
static int get_key(const char **ropts, const char *delim, char **rkey)
Read a key from a string.
Definition: opt.c:1421
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:1624
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1264
static int opt_size(enum AVOptionType type)
Definition: opt.c:1639
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 flag
Definition: cpu.c:34
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:1384
static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:425
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:216
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:554
Use backslash escaping.
Definition: avstring.h:310
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:877
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:1949
static void opt_list(void *obj, void *av_log_obj, const char *unit, int req_flags, int rej_flags)
Definition: opt.c:1069
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:678
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:1937
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:1841
int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:911
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:1464
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:927
Public dictionary API.
const char * name
Definition: opt.h:246
#define WHITESPACES
Definition: opt.c:1404
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
Accept to parse a value without a key; the key will then be returned as NULL.
Definition: opt.h:529
static const char * get_opt_const_name(void *obj, const char *unit, int64_t value)
Definition: opt.c:1035
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:544
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:3969
#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:833
int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:606
#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 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 const double const_values[]
Definition: eval.c:27
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:419
static int hexchar2int(char c)
Definition: opt.c:151
int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
Definition: opt.c:945
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:723
int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
Definition: opt.c:652
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_BPRINT_SIZE_UNLIMITED
union AVOption::@222 default_val
the default value for scalar options
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:1631
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:202
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:539
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:81
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:1566
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:195
AVDictionary * opts
Definition: movenc.c:50
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
Definition: opt.c:950
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
#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
AVFormatContext * ctx
Definition: movenc.c:48
const char * unit
The logical unit to which the option belongs.
Definition: opt.h:300
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:1350
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:662
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:209
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:1269
static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:161
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:1536
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:1751
#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:853
static void log_value(void *av_log_obj, int level, double d)
Definition: opt.c:1002
#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:623
void av_opt_freep_ranges(AVOptionRanges **rangesp)
Free an AVOptionRanges struct and set it to NULL.
Definition: opt.c:1822
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:58
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1561
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:523
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:1252
#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:697
static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
Definition: opt.c:348
#define OPT_EVAL_NUMBER(name, opttype, vartype)
Definition: opt.c:507
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:955
#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
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:193
int version
LIBAVUTIL_VERSION with which this structure was created.
Definition: log.h:93
static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
Definition: opt.c:313
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:1516
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:1730
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:202
#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
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
Definition: opt.c:865
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:379
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1673
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:96
char * key
Definition: dict.h:86
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:732
#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:87
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:1442
int64_t i64
Definition: opt.h:265
static uint8_t tmp[8]
Definition: des.c:38
#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:989
#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:306
#define av_freep(p)
static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
Definition: opt.c:326
#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:70
#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:1572
static const char *const const_names[]
Definition: eval.c:33
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2150
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:2138
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:657
#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:60
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
Definition: opt.c:584
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:431
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:1406
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:893