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