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  av_opt_set_defaults2(s, 0, 0);
1181 }
1182 
1183 void av_opt_set_defaults2(void *s, int mask, int flags)
1184 {
1185  const AVOption *opt = NULL;
1186  while ((opt = av_opt_next(s, opt))) {
1187  void *dst = ((uint8_t*)s) + opt->offset;
1188 
1189  if ((opt->flags & mask) != flags)
1190  continue;
1191 
1192  if (opt->flags & AV_OPT_FLAG_READONLY)
1193  continue;
1194 
1195  switch (opt->type) {
1196  case AV_OPT_TYPE_CONST:
1197  /* Nothing to be done here */
1198  break;
1199  case AV_OPT_TYPE_FLAGS:
1200  case AV_OPT_TYPE_INT:
1201  case AV_OPT_TYPE_INT64:
1202  case AV_OPT_TYPE_DURATION:
1204  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1205  break;
1206  case AV_OPT_TYPE_DOUBLE:
1207  case AV_OPT_TYPE_FLOAT: {
1208  double val;
1209  val = opt->default_val.dbl;
1210  write_number(s, opt, dst, val, 1, 1);
1211  }
1212  break;
1213  case AV_OPT_TYPE_RATIONAL: {
1214  AVRational val;
1215  val = av_d2q(opt->default_val.dbl, INT_MAX);
1216  write_number(s, opt, dst, 1, val.den, val.num);
1217  }
1218  break;
1219  case AV_OPT_TYPE_COLOR:
1220  set_string_color(s, opt, opt->default_val.str, dst);
1221  break;
1222  case AV_OPT_TYPE_STRING:
1223  set_string(s, opt, opt->default_val.str, dst);
1224  break;
1226  set_string_image_size(s, opt, opt->default_val.str, dst);
1227  break;
1229  set_string_video_rate(s, opt, opt->default_val.str, dst);
1230  break;
1231  case AV_OPT_TYPE_PIXEL_FMT:
1232  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1233  break;
1235  write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1236  break;
1237  case AV_OPT_TYPE_BINARY:
1238  set_string_binary(s, opt, opt->default_val.str, dst);
1239  break;
1240  case AV_OPT_TYPE_DICT:
1241  /* Cannot set defaults for these types */
1242  break;
1243  default:
1244  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1245  }
1246  }
1247 }
1248 
1249 /**
1250  * Store the value in the field in ctx that is named like key.
1251  * ctx must be an AVClass context, storing is done using AVOptions.
1252  *
1253  * @param buf the string to parse, buf will be updated to point at the
1254  * separator just after the parsed key/value pair
1255  * @param key_val_sep a 0-terminated list of characters used to
1256  * separate key from value
1257  * @param pairs_sep a 0-terminated list of characters used to separate
1258  * two pairs from each other
1259  * @return 0 if the key/value pair has been successfully parsed and
1260  * set, or a negative value corresponding to an AVERROR code in case
1261  * of error:
1262  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1263  * the error code issued by av_opt_set() if the key/value pair
1264  * cannot be set
1265  */
1266 static int parse_key_value_pair(void *ctx, const char **buf,
1267  const char *key_val_sep, const char *pairs_sep)
1268 {
1269  char *key = av_get_token(buf, key_val_sep);
1270  char *val;
1271  int ret;
1272 
1273  if (!key)
1274  return AVERROR(ENOMEM);
1275 
1276  if (*key && strspn(*buf, key_val_sep)) {
1277  (*buf)++;
1278  val = av_get_token(buf, pairs_sep);
1279  if (!val) {
1280  av_freep(&key);
1281  return AVERROR(ENOMEM);
1282  }
1283  } else {
1284  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1285  av_free(key);
1286  return AVERROR(EINVAL);
1287  }
1288 
1289  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1290 
1291  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1292  if (ret == AVERROR_OPTION_NOT_FOUND)
1293  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1294 
1295  av_free(key);
1296  av_free(val);
1297  return ret;
1298 }
1299 
1300 int av_set_options_string(void *ctx, const char *opts,
1301  const char *key_val_sep, const char *pairs_sep)
1302 {
1303  int ret, count = 0;
1304 
1305  if (!opts)
1306  return 0;
1307 
1308  while (*opts) {
1309  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1310  return ret;
1311  count++;
1312 
1313  if (*opts)
1314  opts++;
1315  }
1316 
1317  return count;
1318 }
1319 
1320 #define WHITESPACES " \n\t"
1321 
1322 static int is_key_char(char c)
1323 {
1324  return (unsigned)((c | 32) - 'a') < 26 ||
1325  (unsigned)(c - '0') < 10 ||
1326  c == '-' || c == '_' || c == '/' || c == '.';
1327 }
1328 
1329 /**
1330  * Read a key from a string.
1331  *
1332  * The key consists of is_key_char characters and must be terminated by a
1333  * character from the delim string; spaces are ignored.
1334  *
1335  * @return 0 for success (even with ellipsis), <0 for failure
1336  */
1337 static int get_key(const char **ropts, const char *delim, char **rkey)
1338 {
1339  const char *opts = *ropts;
1340  const char *key_start, *key_end;
1341 
1342  key_start = opts += strspn(opts, WHITESPACES);
1343  while (is_key_char(*opts))
1344  opts++;
1345  key_end = opts;
1346  opts += strspn(opts, WHITESPACES);
1347  if (!*opts || !strchr(delim, *opts))
1348  return AVERROR(EINVAL);
1349  opts++;
1350  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1351  return AVERROR(ENOMEM);
1352  memcpy(*rkey, key_start, key_end - key_start);
1353  (*rkey)[key_end - key_start] = 0;
1354  *ropts = opts;
1355  return 0;
1356 }
1357 
1358 int av_opt_get_key_value(const char **ropts,
1359  const char *key_val_sep, const char *pairs_sep,
1360  unsigned flags,
1361  char **rkey, char **rval)
1362 {
1363  int ret;
1364  char *key = NULL, *val;
1365  const char *opts = *ropts;
1366 
1367  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1368  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1369  return AVERROR(EINVAL);
1370  if (!(val = av_get_token(&opts, pairs_sep))) {
1371  av_free(key);
1372  return AVERROR(ENOMEM);
1373  }
1374  *ropts = opts;
1375  *rkey = key;
1376  *rval = val;
1377  return 0;
1378 }
1379 
1380 int av_opt_set_from_string(void *ctx, const char *opts,
1381  const char *const *shorthand,
1382  const char *key_val_sep, const char *pairs_sep)
1383 {
1384  int ret, count = 0;
1385  const char *dummy_shorthand = NULL;
1386  char *av_uninit(parsed_key), *av_uninit(value);
1387  const char *key;
1388 
1389  if (!opts)
1390  return 0;
1391  if (!shorthand)
1392  shorthand = &dummy_shorthand;
1393 
1394  while (*opts) {
1395  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1396  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1397  &parsed_key, &value);
1398  if (ret < 0) {
1399  if (ret == AVERROR(EINVAL))
1400  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1401  else
1402  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1403  av_err2str(ret));
1404  return ret;
1405  }
1406  if (*opts)
1407  opts++;
1408  if (parsed_key) {
1409  key = parsed_key;
1410  while (*shorthand) /* discard all remaining shorthand */
1411  shorthand++;
1412  } else {
1413  key = *(shorthand++);
1414  }
1415 
1416  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1417  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1418  if (ret == AVERROR_OPTION_NOT_FOUND)
1419  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1420  av_free(value);
1421  av_free(parsed_key);
1422  return ret;
1423  }
1424 
1425  av_free(value);
1426  av_free(parsed_key);
1427  count++;
1428  }
1429  return count;
1430 }
1431 
1432 void av_opt_free(void *obj)
1433 {
1434  const AVOption *o = NULL;
1435  while ((o = av_opt_next(obj, o))) {
1436  switch (o->type) {
1437  case AV_OPT_TYPE_STRING:
1438  case AV_OPT_TYPE_BINARY:
1439  av_freep((uint8_t *)obj + o->offset);
1440  break;
1441 
1442  case AV_OPT_TYPE_DICT:
1443  av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1444  break;
1445 
1446  default:
1447  break;
1448  }
1449  }
1450 }
1451 
1452 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1453 {
1454  AVDictionaryEntry *t = NULL;
1455  AVDictionary *tmp = NULL;
1456  int ret = 0;
1457 
1458  if (!options)
1459  return 0;
1460 
1461  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1462  ret = av_opt_set(obj, t->key, t->value, search_flags);
1463  if (ret == AVERROR_OPTION_NOT_FOUND)
1464  av_dict_set(&tmp, t->key, t->value, 0);
1465  else if (ret < 0) {
1466  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1467  break;
1468  }
1469  ret = 0;
1470  }
1471  av_dict_free(options);
1472  *options = tmp;
1473  return ret;
1474 }
1475 
1477 {
1478  return av_opt_set_dict2(obj, options, 0);
1479 }
1480 
1481 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1482  int opt_flags, int search_flags)
1483 {
1484  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1485 }
1486 
1487 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1488  int opt_flags, int search_flags, void **target_obj)
1489 {
1490  const AVClass *c;
1491  const AVOption *o = NULL;
1492 
1493  if(!obj)
1494  return NULL;
1495 
1496  c= *(AVClass**)obj;
1497 
1498  if (!c)
1499  return NULL;
1500 
1501  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1502  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1503  const AVClass *child = NULL;
1504  while (child = av_opt_child_class_next(c, child))
1505  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1506  return o;
1507  } else {
1508  void *child = NULL;
1509  while (child = av_opt_child_next(obj, child))
1510  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1511  return o;
1512  }
1513  }
1514 
1515  while (o = av_opt_next(obj, o)) {
1516  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1517  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1518  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1519  if (target_obj) {
1520  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1521  *target_obj = obj;
1522  else
1523  *target_obj = NULL;
1524  }
1525  return o;
1526  }
1527  }
1528  return NULL;
1529 }
1530 
1531 void *av_opt_child_next(void *obj, void *prev)
1532 {
1533  const AVClass *c = *(AVClass**)obj;
1534  if (c->child_next)
1535  return c->child_next(obj, prev);
1536  return NULL;
1537 }
1538 
1539 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1540 {
1541  if (parent->child_class_next)
1542  return parent->child_class_next(prev);
1543  return NULL;
1544 }
1545 
1546 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1547 {
1548  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1549  if(!opt)
1550  return NULL;
1551  return (uint8_t*)obj + opt->offset;
1552 }
1553 
1554 static int opt_size(enum AVOptionType type)
1555 {
1556  switch(type) {
1557  case AV_OPT_TYPE_INT:
1558  case AV_OPT_TYPE_FLAGS: return sizeof(int);
1559  case AV_OPT_TYPE_DURATION:
1561  case AV_OPT_TYPE_INT64: return sizeof(int64_t);
1562  case AV_OPT_TYPE_DOUBLE: return sizeof(double);
1563  case AV_OPT_TYPE_FLOAT: return sizeof(float);
1564  case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
1566  case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
1567  case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
1568  case AV_OPT_TYPE_IMAGE_SIZE:return sizeof(int[2]);
1569  case AV_OPT_TYPE_PIXEL_FMT: return sizeof(enum AVPixelFormat);
1570  case AV_OPT_TYPE_SAMPLE_FMT:return sizeof(enum AVSampleFormat);
1571  case AV_OPT_TYPE_COLOR: return 4;
1572  }
1573  return 0;
1574 }
1575 
1576 int av_opt_copy(void *dst, FF_CONST_AVUTIL55 void *src)
1577 {
1578  const AVOption *o = NULL;
1579  const AVClass *c;
1580  int ret = 0;
1581 
1582  if (!src)
1583  return 0;
1584 
1585  c = *(AVClass**)src;
1586  if (*(AVClass**)dst && c != *(AVClass**)dst)
1587  return AVERROR(EINVAL);
1588 
1589  while ((o = av_opt_next(src, o))) {
1590  void *field_dst = ((uint8_t*)dst) + o->offset;
1591  void *field_src = ((uint8_t*)src) + o->offset;
1592  uint8_t **field_dst8 = (uint8_t**)field_dst;
1593  uint8_t **field_src8 = (uint8_t**)field_src;
1594 
1595  if (o->type == AV_OPT_TYPE_STRING) {
1596  if (*field_dst8 != *field_src8)
1597  av_freep(field_dst8);
1598  *field_dst8 = av_strdup(*field_src8);
1599  if (*field_src8 && !*field_dst8)
1600  ret = AVERROR(ENOMEM);
1601  } else if (o->type == AV_OPT_TYPE_BINARY) {
1602  int len = *(int*)(field_src8 + 1);
1603  if (*field_dst8 != *field_src8)
1604  av_freep(field_dst8);
1605  *field_dst8 = av_memdup(*field_src8, len);
1606  if (len && !*field_dst8) {
1607  ret = AVERROR(ENOMEM);
1608  len = 0;
1609  }
1610  *(int*)(field_dst8 + 1) = len;
1611  } else if (o->type == AV_OPT_TYPE_CONST) {
1612  // do nothing
1613  } else if (o->type == AV_OPT_TYPE_DICT) {
1614  AVDictionary **sdict = (AVDictionary **) field_src;
1615  AVDictionary **ddict = (AVDictionary **) field_dst;
1616  if (*sdict != *ddict)
1617  av_dict_free(ddict);
1618  *ddict = NULL;
1619  av_dict_copy(ddict, *sdict, 0);
1620  if (av_dict_count(*sdict) != av_dict_count(*ddict))
1621  ret = AVERROR(ENOMEM);
1622  } else {
1623  memcpy(field_dst, field_src, opt_size(o->type));
1624  }
1625  }
1626  return ret;
1627 }
1628 
1629 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1630 {
1631  int ret;
1632  const AVClass *c = *(AVClass**)obj;
1633  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1634 
1635  if (c->version > (52 << 16 | 11 << 8))
1636  callback = c->query_ranges;
1637 
1638  if (!callback)
1640 
1641  ret = callback(ranges_arg, obj, key, flags);
1642  if (ret >= 0) {
1643  if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1644  ret = 1;
1645  (*ranges_arg)->nb_components = ret;
1646  }
1647  return ret;
1648 }
1649 
1650 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1651 {
1652  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1653  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1654  AVOptionRange *range = av_mallocz(sizeof(*range));
1655  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1656  int ret;
1657 
1658  *ranges_arg = NULL;
1659 
1660  if (!ranges || !range || !range_array || !field) {
1661  ret = AVERROR(ENOMEM);
1662  goto fail;
1663  }
1664 
1665  ranges->range = range_array;
1666  ranges->range[0] = range;
1667  ranges->nb_ranges = 1;
1668  ranges->nb_components = 1;
1669  range->is_range = 1;
1670  range->value_min = field->min;
1671  range->value_max = field->max;
1672 
1673  switch (field->type) {
1674  case AV_OPT_TYPE_INT:
1675  case AV_OPT_TYPE_INT64:
1676  case AV_OPT_TYPE_PIXEL_FMT:
1678  case AV_OPT_TYPE_FLOAT:
1679  case AV_OPT_TYPE_DOUBLE:
1680  case AV_OPT_TYPE_DURATION:
1681  case AV_OPT_TYPE_COLOR:
1683  break;
1684  case AV_OPT_TYPE_STRING:
1685  range->component_min = 0;
1686  range->component_max = 0x10FFFF; // max unicode value
1687  range->value_min = -1;
1688  range->value_max = INT_MAX;
1689  break;
1690  case AV_OPT_TYPE_RATIONAL:
1691  range->component_min = INT_MIN;
1692  range->component_max = INT_MAX;
1693  break;
1695  range->component_min = 0;
1696  range->component_max = INT_MAX/128/8;
1697  range->value_min = 0;
1698  range->value_max = INT_MAX/8;
1699  break;
1701  range->component_min = 1;
1702  range->component_max = INT_MAX;
1703  range->value_min = 1;
1704  range->value_max = INT_MAX;
1705  break;
1706  default:
1707  ret = AVERROR(ENOSYS);
1708  goto fail;
1709  }
1710 
1711  *ranges_arg = ranges;
1712  return 1;
1713 fail:
1714  av_free(ranges);
1715  av_free(range);
1716  av_free(range_array);
1717  return ret;
1718 }
1719 
1721 {
1722  int i;
1723  AVOptionRanges *ranges = *rangesp;
1724 
1725  if (!ranges)
1726  return;
1727 
1728  for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1729  AVOptionRange *range = ranges->range[i];
1730  if (range) {
1731  av_freep(&range->str);
1732  av_freep(&ranges->range[i]);
1733  }
1734  }
1735  av_freep(&ranges->range);
1736  av_freep(rangesp);
1737 }
1738 
1739 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1740 {
1741  int64_t i64;
1742  double d, d2;
1743  float f;
1744  AVRational q;
1745  int ret, w, h;
1746  char *str;
1747  void *dst;
1748 
1749  if (!o || !obj)
1750  return AVERROR(EINVAL);
1751 
1752  dst = ((uint8_t*)obj) + o->offset;
1753 
1754  switch (o->type) {
1755  case AV_OPT_TYPE_CONST:
1756  return 1;
1757  case AV_OPT_TYPE_FLAGS:
1758  case AV_OPT_TYPE_PIXEL_FMT:
1760  case AV_OPT_TYPE_INT:
1762  case AV_OPT_TYPE_DURATION:
1763  case AV_OPT_TYPE_INT64:
1764  read_number(o, dst, NULL, NULL, &i64);
1765  return o->default_val.i64 == i64;
1766  case AV_OPT_TYPE_STRING:
1767  str = *(char **)dst;
1768  if (str == o->default_val.str) //2 NULLs
1769  return 1;
1770  if (!str || !o->default_val.str) //1 NULL
1771  return 0;
1772  return !strcmp(str, o->default_val.str);
1773  case AV_OPT_TYPE_DOUBLE:
1774  read_number(o, dst, &d, NULL, NULL);
1775  return o->default_val.dbl == d;
1776  case AV_OPT_TYPE_FLOAT:
1777  read_number(o, dst, &d, NULL, NULL);
1778  f = o->default_val.dbl;
1779  d2 = f;
1780  return d2 == d;
1781  case AV_OPT_TYPE_RATIONAL:
1782  q = av_d2q(o->default_val.dbl, INT_MAX);
1783  return !av_cmp_q(*(AVRational*)dst, q);
1784  case AV_OPT_TYPE_BINARY: {
1785  struct {
1786  uint8_t *data;
1787  int size;
1788  } tmp = {0};
1789  int opt_size = *(int *)((void **)dst + 1);
1790  void *opt_ptr = *(void **)dst;
1791  if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1792  return 1;
1793  if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1794  return 0;
1795  if (opt_size != strlen(o->default_val.str) / 2)
1796  return 0;
1797  ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1798  if (!ret)
1799  ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1800  av_free(tmp.data);
1801  return ret;
1802  }
1803  case AV_OPT_TYPE_DICT:
1804  /* Binary and dict have not default support yet. Any pointer is not default. */
1805  return !!(*(void **)dst);
1807  if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1808  w = h = 0;
1809  else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1810  return ret;
1811  return (w == *(int *)dst) && (h == *((int *)dst+1));
1813  q = (AVRational){0, 0};
1814  if (o->default_val.str) {
1815  if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1816  return ret;
1817  }
1818  return !av_cmp_q(*(AVRational*)dst, q);
1819  case AV_OPT_TYPE_COLOR: {
1820  uint8_t color[4] = {0, 0, 0, 0};
1821  if (o->default_val.str) {
1822  if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1823  return ret;
1824  }
1825  return !memcmp(color, dst, sizeof(color));
1826  }
1827  default:
1828  av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1829  break;
1830  }
1831  return AVERROR_PATCHWELCOME;
1832 }
1833 
1834 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1835 {
1836  const AVOption *o;
1837  void *target;
1838  if (!obj)
1839  return AVERROR(EINVAL);
1840  o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1841  if (!o)
1842  return AVERROR_OPTION_NOT_FOUND;
1843  return av_opt_is_set_to_default(target, o);
1844 }
1845 
1846 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1847  const char key_val_sep, const char pairs_sep)
1848 {
1849  const AVOption *o = NULL;
1850  uint8_t *buf;
1851  AVBPrint bprint;
1852  int ret, cnt = 0;
1853  const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1854 
1855  if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1856  pairs_sep == '\\' || key_val_sep == '\\') {
1857  av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1858  return AVERROR(EINVAL);
1859  }
1860 
1861  if (!obj || !buffer)
1862  return AVERROR(EINVAL);
1863 
1864  *buffer = NULL;
1866 
1867  while (o = av_opt_next(obj, o)) {
1868  if (o->type == AV_OPT_TYPE_CONST)
1869  continue;
1870  if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
1871  continue;
1872  else if (((o->flags & opt_flags) != opt_flags))
1873  continue;
1874  if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
1875  continue;
1876  if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
1877  av_bprint_finalize(&bprint, NULL);
1878  return ret;
1879  }
1880  if (buf) {
1881  if (cnt++)
1882  av_bprint_append_data(&bprint, &pairs_sep, 1);
1883  av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1884  av_bprint_append_data(&bprint, &key_val_sep, 1);
1885  av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
1886  av_freep(&buf);
1887  }
1888  }
1889  av_bprint_finalize(&bprint, buffer);
1890  return 0;
1891 }
1892 
1893 #ifdef TEST
1894 
1895 typedef struct TestContext
1896 {
1897  const AVClass *class;
1898  int num;
1899  int toggle;
1900  char *string;
1901  int flags;
1902  AVRational rational;
1903  AVRational video_rate;
1904  int w, h;
1905  enum AVPixelFormat pix_fmt;
1906  enum AVSampleFormat sample_fmt;
1907  int64_t duration;
1908  uint8_t color[4];
1909  int64_t channel_layout;
1910  void *binary;
1911  int binary_size;
1912  void *binary1;
1913  int binary_size1;
1914  void *binary2;
1915  int binary_size2;
1916  int64_t num64;
1917  float flt;
1918  double dbl;
1919  char *escape;
1920 } TestContext;
1921 
1922 #define OFFSET(x) offsetof(TestContext, x)
1923 
1924 #define TEST_FLAG_COOL 01
1925 #define TEST_FLAG_LAME 02
1926 #define TEST_FLAG_MU 04
1927 
1928 static const AVOption test_options[]= {
1929 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100, 1 },
1930 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, 1 },
1931 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, 10, 1 },
1932 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX, 1 },
1933 {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, {.str = "\\=,"}, CHAR_MIN, CHAR_MAX, 1 },
1934 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 1}, 0, INT_MAX, 1, "flags" },
1935 {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 1, "flags" },
1936 {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 1, "flags" },
1937 {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 1, "flags" },
1938 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{.str="200x300"}, 0, 0, 1},
1939 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_0BGR}, -1, INT_MAX, 1},
1940 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_S16}, -1, INT_MAX, 1},
1941 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 , 1},
1942 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 1000}, 0, INT64_MAX, 1},
1943 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0, 1},
1944 {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = AV_CH_LAYOUT_HEXAGONAL}, 0, INT64_MAX, 1},
1945 {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, {.str="62696e00"}, 0, 0, 1 },
1946 {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, {.str=NULL}, 0, 0, 1 },
1947 {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, {.str=""}, 0, 0, 1 },
1948 {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, {.i64 = 1}, 0, 100, 1 },
1949 {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, {.dbl = 1.0/3}, 0, 100, 1},
1950 {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0/3}, 0, 100, 1 },
1951 {NULL},
1952 };
1953 
1954 static const char *test_get_name(void *ctx)
1955 {
1956  return "test";
1957 }
1958 
1959 static const AVClass test_class = {
1960  "TestContext",
1961  test_get_name,
1962  test_options
1963 };
1964 
1965 static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
1966 {
1967  vfprintf(stdout, fmt, vl);
1968 }
1969 
1970 int main(void)
1971 {
1972  int i;
1973 
1976 
1977  printf("Testing default values\n");
1978  {
1979  TestContext test_ctx = { 0 };
1980  test_ctx.class = &test_class;
1981  av_opt_set_defaults(&test_ctx);
1982 
1983  printf("num=%d\n", test_ctx.num);
1984  printf("toggle=%d\n", test_ctx.toggle);
1985  printf("string=%s\n", test_ctx.string);
1986  printf("escape=%s\n", test_ctx.escape);
1987  printf("flags=%d\n", test_ctx.flags);
1988  printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
1989  printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
1990  printf("width=%d height=%d\n", test_ctx.w, test_ctx.h);
1991  printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt));
1992  printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt));
1993  printf("duration=%"PRId64"\n", test_ctx.duration);
1994  printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
1995  printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL);
1996  if (test_ctx.binary)
1997  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]);
1998  printf("binary_size=%d\n", test_ctx.binary_size);
1999  printf("num64=%"PRId64"\n", test_ctx.num64);
2000  printf("flt=%.6f\n", test_ctx.flt);
2001  printf("dbl=%.6f\n", test_ctx.dbl);
2002 
2003  av_opt_show2(&test_ctx, NULL, -1, 0);
2004 
2005  av_opt_free(&test_ctx);
2006  }
2007 
2008  printf("\nTesting av_opt_is_set_to_default()\n");
2009  {
2010  int ret;
2011  TestContext test_ctx = { 0 };
2012  const AVOption *o = NULL;
2013  test_ctx.class = &test_class;
2014 
2016 
2017  while (o = av_opt_next(&test_ctx, o)) {
2018  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2019  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2020  }
2021  av_opt_set_defaults(&test_ctx);
2022  while (o = av_opt_next(&test_ctx, o)) {
2023  ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0);
2024  printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : "");
2025  }
2026  av_opt_free(&test_ctx);
2027  }
2028 
2029  printf("\nTest av_opt_serialize()\n");
2030  {
2031  TestContext test_ctx = { 0 };
2032  char *buf;
2033  test_ctx.class = &test_class;
2034 
2036 
2037  av_opt_set_defaults(&test_ctx);
2038  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2039  printf("%s\n", buf);
2040  av_opt_free(&test_ctx);
2041  memset(&test_ctx, 0, sizeof(test_ctx));
2042  test_ctx.class = &test_class;
2043  av_set_options_string(&test_ctx, buf, "=", ",");
2044  av_free(buf);
2045  if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) {
2046  printf("%s\n", buf);
2047  av_free(buf);
2048  }
2049  }
2050  av_opt_free(&test_ctx);
2051  }
2052 
2053  printf("\nTesting av_set_options_string()\n");
2054  {
2055  TestContext test_ctx = { 0 };
2056  static const char * const options[] = {
2057  "",
2058  ":",
2059  "=",
2060  "foo=:",
2061  ":=foo",
2062  "=foo",
2063  "foo=",
2064  "foo",
2065  "foo=val",
2066  "foo==val",
2067  "toggle=:",
2068  "string=:",
2069  "toggle=1 : foo",
2070  "toggle=100",
2071  "toggle==1",
2072  "flags=+mu-lame : num=42: toggle=0",
2073  "num=42 : string=blahblah",
2074  "rational=0 : rational=1/2 : rational=1/-1",
2075  "rational=-1/0",
2076  "size=1024x768",
2077  "size=pal",
2078  "size=bogus",
2079  "pix_fmt=yuv420p",
2080  "pix_fmt=2",
2081  "pix_fmt=bogus",
2082  "sample_fmt=s16",
2083  "sample_fmt=2",
2084  "sample_fmt=bogus",
2085  "video_rate=pal",
2086  "video_rate=25",
2087  "video_rate=30000/1001",
2088  "video_rate=30/1.001",
2089  "video_rate=bogus",
2090  "duration=bogus",
2091  "duration=123.45",
2092  "duration=1\\:23\\:45.67",
2093  "color=blue",
2094  "color=0x223300",
2095  "color=0x42FF07AA",
2096  "cl=stereo+downmix",
2097  "cl=foo",
2098  "bin=boguss",
2099  "bin=111",
2100  "bin=ffff",
2101  "num64=bogus",
2102  "num64=44",
2103  "num64=44.4",
2104  "num64=-1",
2105  "num64=101",
2106  "flt=bogus",
2107  "flt=2",
2108  "flt=2.2",
2109  "flt=-1",
2110  "flt=101",
2111  "dbl=bogus",
2112  "dbl=2",
2113  "dbl=2.2",
2114  "dbl=-1",
2115  "dbl=101",
2116  };
2117 
2118  test_ctx.class = &test_class;
2119  av_opt_set_defaults(&test_ctx);
2120 
2122 
2123  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2124  int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms
2125  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2126  if (silence_log)
2128  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
2129  printf("Error '%s'\n", options[i]);
2130  else
2131  printf("OK '%s'\n", options[i]);
2133  }
2134  av_opt_free(&test_ctx);
2135  }
2136 
2137  printf("\nTesting av_opt_set_from_string()\n");
2138  {
2139  TestContext test_ctx = { 0 };
2140  static const char * const options[] = {
2141  "",
2142  "5",
2143  "5:hello",
2144  "5:hello:size=pal",
2145  "5:size=pal:hello",
2146  ":",
2147  "=",
2148  " 5 : hello : size = pal ",
2149  "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2150  };
2151  static const char * const shorthand[] = { "num", "string", NULL };
2152 
2153  test_ctx.class = &test_class;
2154  av_opt_set_defaults(&test_ctx);
2155 
2157 
2158  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
2159  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
2160  if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
2161  printf("Error '%s'\n", options[i]);
2162  else
2163  printf("OK '%s'\n", options[i]);
2164  }
2165  av_opt_free(&test_ctx);
2166  }
2167 
2168  return 0;
2169 }
2170 
2171 #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:1531
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:1337
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:1539
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:1554
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:1300
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:278
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:1846
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:1834
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:1739
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:1380
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
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition: cmdutils.c:88
#define WHITESPACES
Definition: opt.c:1320
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:584
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:326
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:76
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:300
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:718
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:1546
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
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:79
#define fail()
Definition: checkasm.h:57
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:1481
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:81
int av_opt_copy(void *dst, FF_CONST_AVUTIL55 void *src)
Copy options from src object into dest object.
Definition: opt.c:1576
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:611
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:1266
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
void av_opt_set_defaults2(void *s, int mask, int flags)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1183
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
double av_get_double(void *obj, const char *name, const AVOption **o_out)
Definition: opt.c:786
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:1452
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:1650
#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:1720
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:1476
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
int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
Definition: opt.c:811
#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:3000
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:1432
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:1629
double value_min
Value range.
Definition: opt.h:323
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:280
#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:620
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:884
#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:1358
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:328
#define FF_CONST_AVUTIL55
Definition: version.h:140
#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:885
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:1487
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2062
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:2050
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:627
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
Definition: opt.c: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:1322
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