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);
466  return set_string_video_rate(obj, o, val, dst);
468  return set_string_pixel_fmt(obj, o, val, dst);
470  return set_string_sample_fmt(obj, o, val, dst);
472  if (!val) {
473  *(int64_t *)dst = 0;
474  return 0;
475  } else {
476  if ((ret = av_parse_time(dst, val, 1)) < 0)
477  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
478  return ret;
479  }
480  break;
481  case AV_OPT_TYPE_COLOR:
482  return set_string_color(obj, o, val, dst);
484  if (!val || !strcmp(val, "none")) {
485  *(int64_t *)dst = 0;
486  } else {
487  int64_t cl = av_get_channel_layout(val);
488  if (!cl) {
489  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
490  ret = AVERROR(EINVAL);
491  }
492  *(int64_t *)dst = cl;
493  return ret;
494  }
495  break;
496  }
497 
498  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
499  return AVERROR(EINVAL);
500 }
501 
502 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
503 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
504  const char *val, vartype *name ## _out) \
505 { \
506  if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
507  return AVERROR(EINVAL); \
508  return set_string_number(obj, obj, o, val, name ## _out); \
509 }
510 
513 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
514 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
515 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
517 
518 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
519  int search_flags)
520 {
521  void *dst, *target_obj;
522  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
523 
524  if (!o || !target_obj)
526 
527  if (o->flags & AV_OPT_FLAG_READONLY)
528  return AVERROR(EINVAL);
529 
530  dst = ((uint8_t *)target_obj) + o->offset;
531  return write_number(obj, o, dst, num, den, intnum);
532 }
533 
534 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
535 {
536  return set_number(obj, name, 1, 1, val, search_flags);
537 }
538 
539 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
540 {
541  return set_number(obj, name, val, 1, 1, search_flags);
542 }
543 
544 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
545 {
546  return set_number(obj, name, val.num, val.den, 1, search_flags);
547 }
548 
549 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
550 {
551  void *target_obj;
552  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
553  uint8_t *ptr;
554  uint8_t **dst;
555  int *lendst;
556 
557  if (!o || !target_obj)
559 
561  return AVERROR(EINVAL);
562 
563  ptr = len ? av_malloc(len) : NULL;
564  if (len && !ptr)
565  return AVERROR(ENOMEM);
566 
567  dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
568  lendst = (int *)(dst + 1);
569 
570  av_free(*dst);
571  *dst = ptr;
572  *lendst = len;
573  if (len)
574  memcpy(ptr, val, len);
575 
576  return 0;
577 }
578 
579 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
580 {
581  void *target_obj;
582  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
583 
584  if (!o || !target_obj)
586  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
587  av_log(obj, AV_LOG_ERROR,
588  "The value set by option '%s' is not an image size.\n", o->name);
589  return AVERROR(EINVAL);
590  }
591  if (w<0 || h<0) {
592  av_log(obj, AV_LOG_ERROR,
593  "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
594  return AVERROR(EINVAL);
595  }
596  *(int *)(((uint8_t *)target_obj) + o->offset) = w;
597  *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
598  return 0;
599 }
600 
601 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
602 {
603  void *target_obj;
604  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
605 
606  if (!o || !target_obj)
608  if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
609  av_log(obj, AV_LOG_ERROR,
610  "The value set by option '%s' is not a video rate.\n", o->name);
611  return AVERROR(EINVAL);
612  }
613  if (val.num <= 0 || val.den <= 0)
614  return AVERROR(EINVAL);
615  return set_number(obj, name, val.num, val.den, 1, search_flags);
616 }
617 
618 static int set_format(void *obj, const char *name, int fmt, int search_flags,
619  enum AVOptionType type, const char *desc, int nb_fmts)
620 {
621  void *target_obj;
622  const AVOption *o = av_opt_find2(obj, name, NULL, 0,
623  search_flags, &target_obj);
624  int min, max;
625 
626  if (!o || !target_obj)
628  if (o->type != type) {
629  av_log(obj, AV_LOG_ERROR,
630  "The value set by option '%s' is not a %s format", name, desc);
631  return AVERROR(EINVAL);
632  }
633 
634  min = FFMAX(o->min, -1);
635  max = FFMIN(o->max, nb_fmts-1);
636 
637  if (fmt < min || fmt > max) {
638  av_log(obj, AV_LOG_ERROR,
639  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
640  fmt, name, desc, min, max);
641  return AVERROR(ERANGE);
642  }
643  *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
644  return 0;
645 }
646 
647 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
648 {
649  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
650 }
651 
652 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
653 {
654  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
655 }
656 
657 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
658 {
659  void *target_obj;
660  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
661 
662  if (!o || !target_obj)
664  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
665  av_log(obj, AV_LOG_ERROR,
666  "The value set by option '%s' is not a channel layout.\n", o->name);
667  return AVERROR(EINVAL);
668  }
669  *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
670  return 0;
671 }
672 
673 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
674  int search_flags)
675 {
676  void *target_obj;
677  AVDictionary **dst;
678  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
679 
680  if (!o || !target_obj)
682  if (o->flags & AV_OPT_FLAG_READONLY)
683  return AVERROR(EINVAL);
684 
685  dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
686  av_dict_free(dst);
687  av_dict_copy(dst, val, 0);
688 
689  return 0;
690 }
691 
692 static void format_duration(char *buf, size_t size, int64_t d)
693 {
694  char *e;
695 
696  av_assert0(size >= 25);
697  if (d < 0 && d != INT64_MIN) {
698  *(buf++) = '-';
699  size--;
700  d = -d;
701  }
702  if (d == INT64_MAX)
703  snprintf(buf, size, "INT64_MAX");
704  else if (d == INT64_MIN)
705  snprintf(buf, size, "INT64_MIN");
706  else if (d > (int64_t)3600*1000000)
707  snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
708  (int)((d / 60000000) % 60),
709  (int)((d / 1000000) % 60),
710  (int)(d % 1000000));
711  else if (d > 60*1000000)
712  snprintf(buf, size, "%d:%02d.%06d",
713  (int)(d / 60000000),
714  (int)((d / 1000000) % 60),
715  (int)(d % 1000000));
716  else
717  snprintf(buf, size, "%d.%06d",
718  (int)(d / 1000000),
719  (int)(d % 1000000));
720  e = buf + strlen(buf);
721  while (e > buf && e[-1] == '0')
722  *(--e) = 0;
723  if (e > buf && e[-1] == '.')
724  *(--e) = 0;
725 }
726 
727 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
728 {
729  void *dst, *target_obj;
730  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
731  uint8_t *bin, buf[128];
732  int len, i, ret;
733  int64_t i64;
734 
735  if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
737 
738  dst = (uint8_t *)target_obj + o->offset;
739 
740  buf[0] = 0;
741  switch (o->type) {
742  case AV_OPT_TYPE_BOOL:
743  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
744  break;
745  case AV_OPT_TYPE_FLAGS:
746  ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
747  break;
748  case AV_OPT_TYPE_INT:
749  ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
750  break;
751  case AV_OPT_TYPE_INT64:
752  ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
753  break;
754  case AV_OPT_TYPE_FLOAT:
755  ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
756  break;
757  case AV_OPT_TYPE_DOUBLE:
758  ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
759  break;
762  ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
763  break;
764  case AV_OPT_TYPE_CONST:
765  ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
766  break;
767  case AV_OPT_TYPE_STRING:
768  if (*(uint8_t **)dst) {
769  *out_val = av_strdup(*(uint8_t **)dst);
770  } else if (search_flags & AV_OPT_ALLOW_NULL) {
771  *out_val = NULL;
772  return 0;
773  } else {
774  *out_val = av_strdup("");
775  }
776  return *out_val ? 0 : AVERROR(ENOMEM);
777  case AV_OPT_TYPE_BINARY:
778  if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
779  *out_val = NULL;
780  return 0;
781  }
782  len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
783  if ((uint64_t)len * 2 + 1 > INT_MAX)
784  return AVERROR(EINVAL);
785  if (!(*out_val = av_malloc(len * 2 + 1)))
786  return AVERROR(ENOMEM);
787  if (!len) {
788  *out_val[0] = '\0';
789  return 0;
790  }
791  bin = *(uint8_t **)dst;
792  for (i = 0; i < len; i++)
793  snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
794  return 0;
796  ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
797  break;
799  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
800  break;
802  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
803  break;
805  i64 = *(int64_t *)dst;
806  format_duration(buf, sizeof(buf), i64);
807  ret = strlen(buf); // no overflow possible, checked by an assert
808  break;
809  case AV_OPT_TYPE_COLOR:
810  ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
811  (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
812  (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
813  break;
815  i64 = *(int64_t *)dst;
816  ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
817  break;
818  default:
819  return AVERROR(EINVAL);
820  }
821 
822  if (ret >= sizeof(buf))
823  return AVERROR(EINVAL);
824  *out_val = av_strdup(buf);
825  return *out_val ? 0 : AVERROR(ENOMEM);
826 }
827 
828 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
829  int search_flags)
830 {
831  void *dst, *target_obj;
832  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
833  if (!o || !target_obj)
834  goto error;
835 
836  dst = ((uint8_t *)target_obj) + o->offset;
837 
838  if (o_out) *o_out= o;
839 
840  return read_number(o, dst, num, den, intnum);
841 
842 error:
843  *den =
844  *intnum = 0;
845  return -1;
846 }
847 
848 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
849 {
850  int64_t intnum = 1;
851  double num = 1;
852  int ret, den = 1;
853 
854  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
855  return ret;
856  *out_val = num * intnum / den;
857  return 0;
858 }
859 
860 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
861 {
862  int64_t intnum = 1;
863  double num = 1;
864  int ret, den = 1;
865 
866  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
867  return ret;
868  *out_val = num * intnum / den;
869  return 0;
870 }
871 
872 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
873 {
874  int64_t intnum = 1;
875  double num = 1;
876  int ret, den = 1;
877 
878  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
879  return ret;
880 
881  if (num == 1.0 && (int)intnum == intnum)
882  *out_val = (AVRational){intnum, den};
883  else
884  *out_val = av_d2q(num*intnum/den, 1<<24);
885  return 0;
886 }
887 
888 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
889 {
890  void *dst, *target_obj;
891  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
892  if (!o || !target_obj)
894  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
895  av_log(obj, AV_LOG_ERROR,
896  "The value for option '%s' is not an image size.\n", name);
897  return AVERROR(EINVAL);
898  }
899 
900  dst = ((uint8_t*)target_obj) + o->offset;
901  if (w_out) *w_out = *(int *)dst;
902  if (h_out) *h_out = *((int *)dst+1);
903  return 0;
904 }
905 
906 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
907 {
908  int64_t intnum = 1;
909  double num = 1;
910  int ret, den = 1;
911 
912  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
913  return ret;
914 
915  if (num == 1.0 && (int)intnum == intnum)
916  *out_val = (AVRational) { intnum, den };
917  else
918  *out_val = av_d2q(num * intnum / den, 1 << 24);
919  return 0;
920 }
921 
922 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
923  enum AVOptionType type, const char *desc)
924 {
925  void *dst, *target_obj;
926  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
927  if (!o || !target_obj)
929  if (o->type != type) {
930  av_log(obj, AV_LOG_ERROR,
931  "The value for option '%s' is not a %s format.\n", desc, name);
932  return AVERROR(EINVAL);
933  }
934 
935  dst = ((uint8_t*)target_obj) + o->offset;
936  *out_fmt = *(int *)dst;
937  return 0;
938 }
939 
940 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
941 {
942  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
943 }
944 
945 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
946 {
947  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
948 }
949 
950 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
951 {
952  void *dst, *target_obj;
953  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
954  if (!o || !target_obj)
956  if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
957  av_log(obj, AV_LOG_ERROR,
958  "The value for option '%s' is not a channel layout.\n", name);
959  return AVERROR(EINVAL);
960  }
961 
962  dst = ((uint8_t*)target_obj) + o->offset;
963  *cl = *(int64_t *)dst;
964  return 0;
965 }
966 
967 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
968 {
969  void *target_obj;
970  AVDictionary *src;
971  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
972 
973  if (!o || !target_obj)
975  if (o->type != AV_OPT_TYPE_DICT)
976  return AVERROR(EINVAL);
977 
978  src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
979  av_dict_copy(out_val, src, 0);
980 
981  return 0;
982 }
983 
984 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
985 {
986  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
987  const AVOption *flag = av_opt_find(obj, flag_name,
988  field ? field->unit : NULL, 0, 0);
989  int64_t res;
990 
991  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
992  av_opt_get_int(obj, field_name, 0, &res) < 0)
993  return 0;
994  return res & flag->default_val.i64;
995 }
996 
997 static void log_value(void *av_log_obj, int level, double d)
998 {
999  if (d == INT_MAX) {
1000  av_log(av_log_obj, level, "INT_MAX");
1001  } else if (d == INT_MIN) {
1002  av_log(av_log_obj, level, "INT_MIN");
1003  } else if (d == UINT32_MAX) {
1004  av_log(av_log_obj, level, "UINT32_MAX");
1005  } else if (d == (double)INT64_MAX) {
1006  av_log(av_log_obj, level, "I64_MAX");
1007  } else if (d == INT64_MIN) {
1008  av_log(av_log_obj, level, "I64_MIN");
1009  } else if (d == FLT_MAX) {
1010  av_log(av_log_obj, level, "FLT_MAX");
1011  } else if (d == FLT_MIN) {
1012  av_log(av_log_obj, level, "FLT_MIN");
1013  } else if (d == -FLT_MAX) {
1014  av_log(av_log_obj, level, "-FLT_MAX");
1015  } else if (d == -FLT_MIN) {
1016  av_log(av_log_obj, level, "-FLT_MIN");
1017  } else if (d == DBL_MAX) {
1018  av_log(av_log_obj, level, "DBL_MAX");
1019  } else if (d == DBL_MIN) {
1020  av_log(av_log_obj, level, "DBL_MIN");
1021  } else if (d == -DBL_MAX) {
1022  av_log(av_log_obj, level, "-DBL_MAX");
1023  } else if (d == -DBL_MIN) {
1024  av_log(av_log_obj, level, "-DBL_MIN");
1025  } else {
1026  av_log(av_log_obj, level, "%g", d);
1027  }
1028 }
1029 
1030 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1031 {
1032  const AVOption *opt = NULL;
1033 
1034  if (!unit)
1035  return NULL;
1036  while ((opt = av_opt_next(obj, opt)))
1037  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1038  opt->default_val.i64 == value)
1039  return opt->name;
1040  return NULL;
1041 }
1042 
1043 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1044 {
1045  const AVOption *opt = NULL;
1046  char flags[512];
1047 
1048  flags[0] = 0;
1049  if (!unit)
1050  return NULL;
1051  while ((opt = av_opt_next(obj, opt))) {
1052  if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1053  opt->default_val.i64 & value) {
1054  if (flags[0])
1055  av_strlcatf(flags, sizeof(flags), "+");
1056  av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1057  }
1058  }
1059  if (flags[0])
1060  return av_strdup(flags);
1061  return NULL;
1062 }
1063 
1064 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1065  int req_flags, int rej_flags)
1066 {
1067  const AVOption *opt = NULL;
1068  AVOptionRanges *r;
1069  int i;
1070 
1071  while ((opt = av_opt_next(obj, opt))) {
1072  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1073  continue;
1074 
1075  /* Don't print CONST's on level one.
1076  * Don't print anything but CONST's on level two.
1077  * Only print items from the requested unit.
1078  */
1079  if (!unit && opt->type == AV_OPT_TYPE_CONST)
1080  continue;
1081  else if (unit && opt->type != AV_OPT_TYPE_CONST)
1082  continue;
1083  else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1084  continue;
1085  else if (unit && opt->type == AV_OPT_TYPE_CONST)
1086  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1087  else
1088  av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1089  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1090  opt->name);
1091 
1092  switch (opt->type) {
1093  case AV_OPT_TYPE_FLAGS:
1094  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1095  break;
1096  case AV_OPT_TYPE_INT:
1097  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1098  break;
1099  case AV_OPT_TYPE_INT64:
1100  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1101  break;
1102  case AV_OPT_TYPE_DOUBLE:
1103  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1104  break;
1105  case AV_OPT_TYPE_FLOAT:
1106  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1107  break;
1108  case AV_OPT_TYPE_STRING:
1109  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1110  break;
1111  case AV_OPT_TYPE_RATIONAL:
1112  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1113  break;
1114  case AV_OPT_TYPE_BINARY:
1115  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1116  break;
1118  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1119  break;
1121  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1122  break;
1123  case AV_OPT_TYPE_PIXEL_FMT:
1124  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1125  break;
1127  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1128  break;
1129  case AV_OPT_TYPE_DURATION:
1130  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1131  break;
1132  case AV_OPT_TYPE_COLOR:
1133  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1134  break;
1136  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1137  break;
1138  case AV_OPT_TYPE_BOOL:
1139  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1140  break;
1141  case AV_OPT_TYPE_CONST:
1142  default:
1143  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1144  break;
1145  }
1146  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1147  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1148  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1149  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1150  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1151  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1152  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1153  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1154 
1155  if (opt->help)
1156  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1157 
1158  if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1159  switch (opt->type) {
1160  case AV_OPT_TYPE_INT:
1161  case AV_OPT_TYPE_INT64:
1162  case AV_OPT_TYPE_DOUBLE:
1163  case AV_OPT_TYPE_FLOAT:
1164  case AV_OPT_TYPE_RATIONAL:
1165  for (i = 0; i < r->nb_ranges; i++) {
1166  av_log(av_log_obj, AV_LOG_INFO, " (from ");
1167  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1168  av_log(av_log_obj, AV_LOG_INFO, " to ");
1169  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1170  av_log(av_log_obj, AV_LOG_INFO, ")");
1171  }
1172  break;
1173  }
1174  av_opt_freep_ranges(&r);
1175  }
1176 
1177  if (opt->type != AV_OPT_TYPE_CONST &&
1178  opt->type != AV_OPT_TYPE_BINARY &&
1179  !((opt->type == AV_OPT_TYPE_COLOR ||
1180  opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1181  opt->type == AV_OPT_TYPE_STRING ||
1182  opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1183  !opt->default_val.str)) {
1184  av_log(av_log_obj, AV_LOG_INFO, " (default ");
1185  switch (opt->type) {
1186  case AV_OPT_TYPE_BOOL:
1187  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1188  break;
1189  case AV_OPT_TYPE_FLAGS: {
1190  char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1191  if (def_flags) {
1192  av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1193  av_freep(&def_flags);
1194  } else {
1195  av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1196  }
1197  break;
1198  }
1199  case AV_OPT_TYPE_DURATION: {
1200  char buf[25];
1201  format_duration(buf, sizeof(buf), opt->default_val.i64);
1202  av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1203  break;
1204  }
1205  case AV_OPT_TYPE_INT:
1206  case AV_OPT_TYPE_INT64: {
1207  const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1208  if (def_const)
1209  av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1210  else
1211  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1212  break;
1213  }
1214  case AV_OPT_TYPE_DOUBLE:
1215  case AV_OPT_TYPE_FLOAT:
1216  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1217  break;
1218  case AV_OPT_TYPE_RATIONAL: {
1219  AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1220  av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1221  break;
1222  case AV_OPT_TYPE_PIXEL_FMT:
1223  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1224  break;
1226  av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1227  break;
1228  case AV_OPT_TYPE_COLOR:
1230  case AV_OPT_TYPE_STRING:
1232  av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1233  break;
1235  av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1236  break;
1237  }
1238  av_log(av_log_obj, AV_LOG_INFO, ")");
1239  }
1240 
1241  av_log(av_log_obj, AV_LOG_INFO, "\n");
1242  if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1243  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1244  }
1245 }
1246 
1247 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1248 {
1249  if (!obj)
1250  return -1;
1251 
1252  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1253 
1254  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1255 
1256  return 0;
1257 }
1258 
1260 {
1261  av_opt_set_defaults2(s, 0, 0);
1262 }
1263 
1264 void av_opt_set_defaults2(void *s, int mask, int flags)
1265 {
1266  const AVOption *opt = NULL;
1267  while ((opt = av_opt_next(s, opt))) {
1268  void *dst = ((uint8_t*)s) + opt->offset;
1269 
1270  if ((opt->flags & mask) != flags)
1271  continue;
1272 
1273  if (opt->flags & AV_OPT_FLAG_READONLY)
1274  continue;
1275 
1276  switch (opt->type) {
1277  case AV_OPT_TYPE_CONST:
1278  /* Nothing to be done here */
1279  break;
1280  case AV_OPT_TYPE_BOOL:
1281  case AV_OPT_TYPE_FLAGS:
1282  case AV_OPT_TYPE_INT:
1283  case AV_OPT_TYPE_INT64:
1284  case AV_OPT_TYPE_DURATION:
1286  case AV_OPT_TYPE_PIXEL_FMT:
1288  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1289  break;
1290  case AV_OPT_TYPE_DOUBLE:
1291  case AV_OPT_TYPE_FLOAT: {
1292  double val;
1293  val = opt->default_val.dbl;
1294  write_number(s, opt, dst, val, 1, 1);
1295  }
1296  break;
1297  case AV_OPT_TYPE_RATIONAL: {
1298  AVRational val;
1299  val = av_d2q(opt->default_val.dbl, INT_MAX);
1300  write_number(s, opt, dst, 1, val.den, val.num);
1301  }
1302  break;
1303  case AV_OPT_TYPE_COLOR:
1304  set_string_color(s, opt, opt->default_val.str, dst);
1305  break;
1306  case AV_OPT_TYPE_STRING:
1307  set_string(s, opt, opt->default_val.str, dst);
1308  break;
1310  set_string_image_size(s, opt, opt->default_val.str, dst);
1311  break;
1313  set_string_video_rate(s, opt, opt->default_val.str, dst);
1314  break;
1315  case AV_OPT_TYPE_BINARY:
1316  set_string_binary(s, opt, opt->default_val.str, dst);
1317  break;
1318  case AV_OPT_TYPE_DICT:
1319  /* Cannot set defaults for these types */
1320  break;
1321  default:
1322  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1323  opt->type, opt->name);
1324  }
1325  }
1326 }
1327 
1328 /**
1329  * Store the value in the field in ctx that is named like key.
1330  * ctx must be an AVClass context, storing is done using AVOptions.
1331  *
1332  * @param buf the string to parse, buf will be updated to point at the
1333  * separator just after the parsed key/value pair
1334  * @param key_val_sep a 0-terminated list of characters used to
1335  * separate key from value
1336  * @param pairs_sep a 0-terminated list of characters used to separate
1337  * two pairs from each other
1338  * @return 0 if the key/value pair has been successfully parsed and
1339  * set, or a negative value corresponding to an AVERROR code in case
1340  * of error:
1341  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1342  * the error code issued by av_opt_set() if the key/value pair
1343  * cannot be set
1344  */
1345 static int parse_key_value_pair(void *ctx, const char **buf,
1346  const char *key_val_sep, const char *pairs_sep)
1347 {
1348  char *key = av_get_token(buf, key_val_sep);
1349  char *val;
1350  int ret;
1351 
1352  if (!key)
1353  return AVERROR(ENOMEM);
1354 
1355  if (*key && strspn(*buf, key_val_sep)) {
1356  (*buf)++;
1357  val = av_get_token(buf, pairs_sep);
1358  if (!val) {
1359  av_freep(&key);
1360  return AVERROR(ENOMEM);
1361  }
1362  } else {
1363  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1364  av_free(key);
1365  return AVERROR(EINVAL);
1366  }
1367 
1368  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1369 
1370  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1371  if (ret == AVERROR_OPTION_NOT_FOUND)
1372  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1373 
1374  av_free(key);
1375  av_free(val);
1376  return ret;
1377 }
1378 
1379 int av_set_options_string(void *ctx, const char *opts,
1380  const char *key_val_sep, const char *pairs_sep)
1381 {
1382  int ret, count = 0;
1383 
1384  if (!opts)
1385  return 0;
1386 
1387  while (*opts) {
1388  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1389  return ret;
1390  count++;
1391 
1392  if (*opts)
1393  opts++;
1394  }
1395 
1396  return count;
1397 }
1398 
1399 #define WHITESPACES " \n\t\r"
1400 
1401 static int is_key_char(char c)
1402 {
1403  return (unsigned)((c | 32) - 'a') < 26 ||
1404  (unsigned)(c - '0') < 10 ||
1405  c == '-' || c == '_' || c == '/' || c == '.';
1406 }
1407 
1408 /**
1409  * Read a key from a string.
1410  *
1411  * The key consists of is_key_char characters and must be terminated by a
1412  * character from the delim string; spaces are ignored.
1413  *
1414  * @return 0 for success (even with ellipsis), <0 for failure
1415  */
1416 static int get_key(const char **ropts, const char *delim, char **rkey)
1417 {
1418  const char *opts = *ropts;
1419  const char *key_start, *key_end;
1420 
1421  key_start = opts += strspn(opts, WHITESPACES);
1422  while (is_key_char(*opts))
1423  opts++;
1424  key_end = opts;
1425  opts += strspn(opts, WHITESPACES);
1426  if (!*opts || !strchr(delim, *opts))
1427  return AVERROR(EINVAL);
1428  opts++;
1429  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1430  return AVERROR(ENOMEM);
1431  memcpy(*rkey, key_start, key_end - key_start);
1432  (*rkey)[key_end - key_start] = 0;
1433  *ropts = opts;
1434  return 0;
1435 }
1436 
1437 int av_opt_get_key_value(const char **ropts,
1438  const char *key_val_sep, const char *pairs_sep,
1439  unsigned flags,
1440  char **rkey, char **rval)
1441 {
1442  int ret;
1443  char *key = NULL, *val;
1444  const char *opts = *ropts;
1445 
1446  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1447  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1448  return AVERROR(EINVAL);
1449  if (!(val = av_get_token(&opts, pairs_sep))) {
1450  av_free(key);
1451  return AVERROR(ENOMEM);
1452  }
1453  *ropts = opts;
1454  *rkey = key;
1455  *rval = val;
1456  return 0;
1457 }
1458 
1459 int av_opt_set_from_string(void *ctx, const char *opts,
1460  const char *const *shorthand,
1461  const char *key_val_sep, const char *pairs_sep)
1462 {
1463  int ret, count = 0;
1464  const char *dummy_shorthand = NULL;
1465  char *av_uninit(parsed_key), *av_uninit(value);
1466  const char *key;
1467 
1468  if (!opts)
1469  return 0;
1470  if (!shorthand)
1471  shorthand = &dummy_shorthand;
1472 
1473  while (*opts) {
1474  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1475  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1476  &parsed_key, &value);
1477  if (ret < 0) {
1478  if (ret == AVERROR(EINVAL))
1479  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1480  else
1481  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1482  av_err2str(ret));
1483  return ret;
1484  }
1485  if (*opts)
1486  opts++;
1487  if (parsed_key) {
1488  key = parsed_key;
1489  while (*shorthand) /* discard all remaining shorthand */
1490  shorthand++;
1491  } else {
1492  key = *(shorthand++);
1493  }
1494 
1495  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1496  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1497  if (ret == AVERROR_OPTION_NOT_FOUND)
1498  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1499  av_free(value);
1500  av_free(parsed_key);
1501  return ret;
1502  }
1503 
1504  av_free(value);
1505  av_free(parsed_key);
1506  count++;
1507  }
1508  return count;
1509 }
1510 
1511 void av_opt_free(void *obj)
1512 {
1513  const AVOption *o = NULL;
1514  while ((o = av_opt_next(obj, o))) {
1515  switch (o->type) {
1516  case AV_OPT_TYPE_STRING:
1517  case AV_OPT_TYPE_BINARY:
1518  av_freep((uint8_t *)obj + o->offset);
1519  break;
1520 
1521  case AV_OPT_TYPE_DICT:
1522  av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1523  break;
1524 
1525  default:
1526  break;
1527  }
1528  }
1529 }
1530 
1531 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1532 {
1533  AVDictionaryEntry *t = NULL;
1534  AVDictionary *tmp = NULL;
1535  int ret = 0;
1536 
1537  if (!options)
1538  return 0;
1539 
1540  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1541  ret = av_opt_set(obj, t->key, t->value, search_flags);
1542  if (ret == AVERROR_OPTION_NOT_FOUND)
1543  ret = av_dict_set(&tmp, t->key, t->value, 0);
1544  if (ret < 0) {
1545  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1546  av_dict_free(&tmp);
1547  return ret;
1548  }
1549  ret = 0;
1550  }
1551  av_dict_free(options);
1552  *options = tmp;
1553  return ret;
1554 }
1555 
1557 {
1558  return av_opt_set_dict2(obj, options, 0);
1559 }
1560 
1561 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1562  int opt_flags, int search_flags)
1563 {
1564  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1565 }
1566 
1567 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1568  int opt_flags, int search_flags, void **target_obj)
1569 {
1570  const AVClass *c;
1571  const AVOption *o = NULL;
1572 
1573  if(!obj)
1574  return NULL;
1575 
1576  c= *(AVClass**)obj;
1577 
1578  if (!c)
1579  return NULL;
1580 
1581  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1582  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1583  const AVClass *child = NULL;
1584  while (child = av_opt_child_class_next(c, child))
1585  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1586  return o;
1587  } else {
1588  void *child = NULL;
1589  while (child = av_opt_child_next(obj, child))
1590  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1591  return o;
1592  }
1593  }
1594 
1595  while (o = av_opt_next(obj, o)) {
1596  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1597  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1598  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1599  if (target_obj) {
1600  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1601  *target_obj = obj;
1602  else
1603  *target_obj = NULL;
1604  }
1605  return o;
1606  }
1607  }
1608  return NULL;
1609 }
1610 
1611 void *av_opt_child_next(void *obj, void *prev)
1612 {
1613  const AVClass *c = *(AVClass **)obj;
1614  if (c->child_next)
1615  return c->child_next(obj, prev);
1616  return NULL;
1617 }
1618 
1619 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1620 {
1621  if (parent->child_class_next)
1622  return parent->child_class_next(prev);
1623  return NULL;
1624 }
1625 
1626 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1627 {
1628  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1629  if(!opt)
1630  return NULL;
1631  return (uint8_t*)obj + opt->offset;
1632 }
1633 
1634 static int opt_size(enum AVOptionType type)
1635 {
1636  switch(type) {
1637  case AV_OPT_TYPE_BOOL:
1638  case AV_OPT_TYPE_INT:
1639  case AV_OPT_TYPE_FLAGS:
1640  return sizeof(int);
1641  case AV_OPT_TYPE_DURATION:
1643  case AV_OPT_TYPE_INT64:
1644  return sizeof(int64_t);
1645  case AV_OPT_TYPE_DOUBLE:
1646  return sizeof(double);
1647  case AV_OPT_TYPE_FLOAT:
1648  return sizeof(float);
1649  case AV_OPT_TYPE_STRING:
1650  return sizeof(uint8_t*);
1652  case AV_OPT_TYPE_RATIONAL:
1653  return sizeof(AVRational);
1654  case AV_OPT_TYPE_BINARY:
1655  return sizeof(uint8_t*) + sizeof(int);
1657  return sizeof(int[2]);
1658  case AV_OPT_TYPE_PIXEL_FMT:
1659  return sizeof(enum AVPixelFormat);
1661  return sizeof(enum AVSampleFormat);
1662  case AV_OPT_TYPE_COLOR:
1663  return 4;
1664  }
1665  return AVERROR(EINVAL);
1666 }
1667 
1668 int av_opt_copy(void *dst, const void *src)
1669 {
1670  const AVOption *o = NULL;
1671  const AVClass *c;
1672  int ret = 0;
1673 
1674  if (!src)
1675  return AVERROR(EINVAL);
1676 
1677  c = *(AVClass **)src;
1678  if (!c || c != *(AVClass **)dst)
1679  return AVERROR(EINVAL);
1680 
1681  while ((o = av_opt_next(src, o))) {
1682  void *field_dst = (uint8_t *)dst + o->offset;
1683  void *field_src = (uint8_t *)src + o->offset;
1684  uint8_t **field_dst8 = (uint8_t **)field_dst;
1685  uint8_t **field_src8 = (uint8_t **)field_src;
1686 
1687  if (o->type == AV_OPT_TYPE_STRING) {
1688  if (*field_dst8 != *field_src8)
1689  av_freep(field_dst8);
1690  *field_dst8 = av_strdup(*field_src8);
1691  if (*field_src8 && !*field_dst8)
1692  ret = AVERROR(ENOMEM);
1693  } else if (o->type == AV_OPT_TYPE_BINARY) {
1694  int len = *(int *)(field_src8 + 1);
1695  if (*field_dst8 != *field_src8)
1696  av_freep(field_dst8);
1697  *field_dst8 = av_memdup(*field_src8, len);
1698  if (len && !*field_dst8) {
1699  ret = AVERROR(ENOMEM);
1700  len = 0;
1701  }
1702  *(int *)(field_dst8 + 1) = len;
1703  } else if (o->type == AV_OPT_TYPE_CONST) {
1704  // do nothing
1705  } else if (o->type == AV_OPT_TYPE_DICT) {
1706  AVDictionary **sdict = (AVDictionary **) field_src;
1707  AVDictionary **ddict = (AVDictionary **) field_dst;
1708  if (*sdict != *ddict)
1709  av_dict_free(ddict);
1710  *ddict = NULL;
1711  av_dict_copy(ddict, *sdict, 0);
1712  if (av_dict_count(*sdict) != av_dict_count(*ddict))
1713  ret = AVERROR(ENOMEM);
1714  } else {
1715  int size = opt_size(o->type);
1716  if (size < 0)
1717  ret = size;
1718  else
1719  memcpy(field_dst, field_src, size);
1720  }
1721  }
1722  return ret;
1723 }
1724 
1725 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1726 {
1727  int ret;
1728  const AVClass *c = *(AVClass**)obj;
1729  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1730 
1731  if (c->version > (52 << 16 | 11 << 8))
1732  callback = c->query_ranges;
1733 
1734  if (!callback)
1736 
1737  ret = callback(ranges_arg, obj, key, flags);
1738  if (ret >= 0) {
1739  if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1740  ret = 1;
1741  (*ranges_arg)->nb_components = ret;
1742  }
1743  return ret;
1744 }
1745 
1746 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1747 {
1748  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1749  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1750  AVOptionRange *range = av_mallocz(sizeof(*range));
1751  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1752  int ret;
1753 
1754  *ranges_arg = NULL;
1755 
1756  if (!ranges || !range || !range_array || !field) {
1757  ret = AVERROR(ENOMEM);
1758  goto fail;
1759  }
1760 
1761  ranges->range = range_array;
1762  ranges->range[0] = range;
1763  ranges->nb_ranges = 1;
1764  ranges->nb_components = 1;
1765  range->is_range = 1;
1766  range->value_min = field->min;
1767  range->value_max = field->max;
1768 
1769  switch (field->type) {
1770  case AV_OPT_TYPE_BOOL:
1771  case AV_OPT_TYPE_INT:
1772  case AV_OPT_TYPE_INT64:
1773  case AV_OPT_TYPE_PIXEL_FMT:
1775  case AV_OPT_TYPE_FLOAT:
1776  case AV_OPT_TYPE_DOUBLE:
1777  case AV_OPT_TYPE_DURATION:
1778  case AV_OPT_TYPE_COLOR:
1780  break;
1781  case AV_OPT_TYPE_STRING:
1782  range->component_min = 0;
1783  range->component_max = 0x10FFFF; // max unicode value
1784  range->value_min = -1;
1785  range->value_max = INT_MAX;
1786  break;
1787  case AV_OPT_TYPE_RATIONAL:
1788  range->component_min = INT_MIN;
1789  range->component_max = INT_MAX;
1790  break;
1792  range->component_min = 0;
1793  range->component_max = INT_MAX/128/8;
1794  range->value_min = 0;
1795  range->value_max = INT_MAX/8;
1796  break;
1798  range->component_min = 1;
1799  range->component_max = INT_MAX;
1800  range->value_min = 1;
1801  range->value_max = INT_MAX;
1802  break;
1803  default:
1804  ret = AVERROR(ENOSYS);
1805  goto fail;
1806  }
1807 
1808  *ranges_arg = ranges;
1809  return 1;
1810 fail:
1811  av_free(ranges);
1812  av_free(range);
1813  av_free(range_array);
1814  return ret;
1815 }
1816 
1818 {
1819  int i;
1820  AVOptionRanges *ranges = *rangesp;
1821 
1822  if (!ranges)
1823  return;
1824 
1825  for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1826  AVOptionRange *range = ranges->range[i];
1827  if (range) {
1828  av_freep(&range->str);
1829  av_freep(&ranges->range[i]);
1830  }
1831  }
1832  av_freep(&ranges->range);
1833  av_freep(rangesp);
1834 }
1835 
1836 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1837 {
1838  int64_t i64;
1839  double d, d2;
1840  float f;
1841  AVRational q;
1842  int ret, w, h;
1843  char *str;
1844  void *dst;
1845 
1846  if (!o || !obj)
1847  return AVERROR(EINVAL);
1848 
1849  dst = ((uint8_t*)obj) + o->offset;
1850 
1851  switch (o->type) {
1852  case AV_OPT_TYPE_CONST:
1853  return 1;
1854  case AV_OPT_TYPE_BOOL:
1855  case AV_OPT_TYPE_FLAGS:
1856  case AV_OPT_TYPE_PIXEL_FMT:
1858  case AV_OPT_TYPE_INT:
1860  case AV_OPT_TYPE_DURATION:
1861  case AV_OPT_TYPE_INT64:
1862  read_number(o, dst, NULL, NULL, &i64);
1863  return o->default_val.i64 == i64;
1864  case AV_OPT_TYPE_STRING:
1865  str = *(char **)dst;
1866  if (str == o->default_val.str) //2 NULLs
1867  return 1;
1868  if (!str || !o->default_val.str) //1 NULL
1869  return 0;
1870  return !strcmp(str, o->default_val.str);
1871  case AV_OPT_TYPE_DOUBLE:
1872  read_number(o, dst, &d, NULL, NULL);
1873  return o->default_val.dbl == d;
1874  case AV_OPT_TYPE_FLOAT:
1875  read_number(o, dst, &d, NULL, NULL);
1876  f = o->default_val.dbl;
1877  d2 = f;
1878  return d2 == d;
1879  case AV_OPT_TYPE_RATIONAL:
1880  q = av_d2q(o->default_val.dbl, INT_MAX);
1881  return !av_cmp_q(*(AVRational*)dst, q);
1882  case AV_OPT_TYPE_BINARY: {
1883  struct {
1884  uint8_t *data;
1885  int size;
1886  } tmp = {0};
1887  int opt_size = *(int *)((void **)dst + 1);
1888  void *opt_ptr = *(void **)dst;
1889  if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1890  return 1;
1891  if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1892  return 0;
1893  if (opt_size != strlen(o->default_val.str) / 2)
1894  return 0;
1895  ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1896  if (!ret)
1897  ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1898  av_free(tmp.data);
1899  return ret;
1900  }
1901  case AV_OPT_TYPE_DICT:
1902  /* Binary and dict have not default support yet. Any pointer is not default. */
1903  return !!(*(void **)dst);
1905  if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1906  w = h = 0;
1907  else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1908  return ret;
1909  return (w == *(int *)dst) && (h == *((int *)dst+1));
1911  q = (AVRational){0, 0};
1912  if (o->default_val.str) {
1913  if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1914  return ret;
1915  }
1916  return !av_cmp_q(*(AVRational*)dst, q);
1917  case AV_OPT_TYPE_COLOR: {
1918  uint8_t color[4] = {0, 0, 0, 0};
1919  if (o->default_val.str) {
1920  if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1921  return ret;
1922  }
1923  return !memcmp(color, dst, sizeof(color));
1924  }
1925  default:
1926  av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1927  break;
1928  }
1929  return AVERROR_PATCHWELCOME;
1930 }
1931 
1932 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1933 {
1934  const AVOption *o;
1935  void *target;
1936  if (!obj)
1937  return AVERROR(EINVAL);
1938  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1939  if (!o)
1940  return AVERROR_OPTION_NOT_FOUND;
1941  return av_opt_is_set_to_default(target, o);
1942 }
1943 
1944 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1945  const char key_val_sep, const char pairs_sep)
1946 {
1947  const AVOption *o = NULL;
1948  uint8_t *buf;
1949  AVBPrint bprint;
1950  int ret, cnt = 0;
1951  const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1952 
1953  if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1954  pairs_sep == '\\' || key_val_sep == '\\') {
1955  av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1956  return AVERROR(EINVAL);
1957  }
1958 
1959  if (!obj || !buffer)
1960  return AVERROR(EINVAL);
1961 
1962  *buffer = NULL;
1964 
1965  while (o = av_opt_next(obj, o)) {
1966  if (o->type == AV_OPT_TYPE_CONST)
1967  continue;
1968  if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1969  continue;
1970  else if (((o->flags & opt_flags) != opt_flags))
1971  continue;
1972  if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1973  continue;
1974  if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1975  av_bprint_finalize(&bprint, NULL);
1976  return ret;
1977  }
1978  if (buf) {
1979  if (cnt++)
1980  av_bprint_append_data(&bprint, &pairs_sep, 1);
1981  av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1982  av_bprint_append_data(&bprint, &key_val_sep, 1);
1983  av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1984  av_freep(&buf);
1985  }
1986  }
1987  av_bprint_finalize(&bprint, buffer);
1988  return 0;
1989 }
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:967
#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:1043
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:544
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:298
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
Definition: opt.c:1611
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:237
static int get_key(const char **ropts, const char *delim, char **rkey)
Read a key from a string.
Definition: opt.c:1416
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:1619
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1259
static int opt_size(enum AVOptionType type)
Definition: opt.c:1634
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:1379
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:549
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:872
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:1944
static void opt_list(void *obj, void *av_log_obj, const char *unit, int req_flags, int rej_flags)
Definition: opt.c:1064
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:673
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:1932
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:1836
int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
Definition: opt.c:906
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:1459
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:922
Public dictionary API.
const char * name
Definition: opt.h:246
#define WHITESPACES
Definition: opt.c:1399
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:1030
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:539
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:828
int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
Definition: opt.c:601
#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 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:940
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:647
#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:1626
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:534
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
Accept to parse a value without a key; the key will then be returned as NULL.
Definition: opt.h:529
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:1561
union AVOption::@219 default_val
the default value for scalar options
static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
Definition: opt.c:195
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
Definition: opt.c:945
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
int flag
Definition: cpu-test.c:33
#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
static uint8_t tmp[8]
Definition: des-test.c:38
#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
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:1345
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:657
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:1264
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:1531
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:1746
#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:848
static void log_value(void *av_log_obj, int level, double d)
Definition: opt.c:997
#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:618
void av_opt_freep_ranges(AVOptionRanges **rangesp)
Free an AVOptionRanges struct and set it to NULL.
Definition: opt.c:1817
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:1556
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:518
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
Definition: opt.c:1247
#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:692
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:502
static const double const_values[]
Definition: eval-test.c:27
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:950
#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 const char *const const_names[]
Definition: eval-test.c:33
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:1511
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:1725
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:860
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:1668
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:727
#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:1437
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:984
#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:303
#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:1567
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2121
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:2109
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:652
#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:579
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:1401
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:888