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