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 "common.h"
31 #include "opt.h"
32 #include "eval.h"
33 #include "dict.h"
34 #include "log.h"
35 #include "parseutils.h"
36 #include "pixdesc.h"
37 #include "mathematics.h"
38 #include "samplefmt.h"
39 
40 #include <float.h>
41 
42 #if FF_API_FIND_OPT
43 //FIXME order them and do a bin search
44 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
45 {
46  const AVOption *o = NULL;
47 
48  while ((o = av_next_option(v, o))) {
49  if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
50  return o;
51  }
52  return NULL;
53 }
54 #endif
55 
56 #if FF_API_OLD_AVOPTIONS
57 const AVOption *av_next_option(void *obj, const AVOption *last)
58 {
59  return av_opt_next(obj, last);
60 }
61 #endif
62 
63 const AVOption *av_opt_next(void *obj, const AVOption *last)
64 {
65  AVClass *class = *(AVClass**)obj;
66  if (!last && class && class->option && class->option[0].name)
67  return class->option;
68  if (last && last[1].name)
69  return ++last;
70  return NULL;
71 }
72 
73 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
74 {
75  switch (o->type) {
76  case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
79  case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
81  case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
82  case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
83  case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
84  case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
85  *den = ((AVRational*)dst)->den;
86  return 0;
87  case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
88  }
89  return AVERROR(EINVAL);
90 }
91 
92 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
93 {
94  if (o->max*den < num*intnum || o->min*den > num*intnum) {
95  av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
96  num*intnum/den, o->name, o->min, o->max);
97  return AVERROR(ERANGE);
98  }
99 
100  switch (o->type) {
101  case AV_OPT_TYPE_FLAGS:
104  case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
106  case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
107  case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
108  case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
110  if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
111  else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
112  break;
113  default:
114  return AVERROR(EINVAL);
115  }
116  return 0;
117 }
118 
119 static const double const_values[] = {
120  M_PI,
121  M_E,
122  FF_QP2LAMBDA,
123  0
124 };
125 
126 static const char * const const_names[] = {
127  "PI",
128  "E",
129  "QP2LAMBDA",
130  0
131 };
132 
133 static int hexchar2int(char c) {
134  if (c >= '0' && c <= '9') return c - '0';
135  if (c >= 'a' && c <= 'f') return c - 'a' + 10;
136  if (c >= 'A' && c <= 'F') return c - 'A' + 10;
137  return -1;
138 }
139 
140 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
141 {
142  int *lendst = (int *)(dst + 1);
143  uint8_t *bin, *ptr;
144  int len = strlen(val);
145 
146  av_freep(dst);
147  *lendst = 0;
148 
149  if (len & 1)
150  return AVERROR(EINVAL);
151  len /= 2;
152 
153  ptr = bin = av_malloc(len);
154  while (*val) {
155  int a = hexchar2int(*val++);
156  int b = hexchar2int(*val++);
157  if (a < 0 || b < 0) {
158  av_free(bin);
159  return AVERROR(EINVAL);
160  }
161  *ptr++ = (a << 4) | b;
162  }
163  *dst = bin;
164  *lendst = len;
165 
166  return 0;
167 }
168 
169 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
170 {
171  av_freep(dst);
172  *dst = av_strdup(val);
173  return 0;
174 }
175 
176 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
177  opt->type == AV_OPT_TYPE_CONST || \
178  opt->type == AV_OPT_TYPE_FLAGS || \
179  opt->type == AV_OPT_TYPE_INT) ? \
180  opt->default_val.i64 : opt->default_val.dbl)
181 
182 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
183 {
184  int ret = 0, notfirst = 0;
185  for (;;) {
186  int i, den = 1;
187  char buf[256];
188  int cmd = 0;
189  double d, num = 1;
190  int64_t intnum = 1;
191 
192  i = 0;
193  if (*val == '+' || *val == '-') {
194  if (o->type == AV_OPT_TYPE_FLAGS)
195  cmd = *(val++);
196  else if (!notfirst)
197  buf[i++] = *val;
198  }
199 
200  for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
201  buf[i] = val[i];
202  buf[i] = 0;
203 
204  {
205  const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
206  if (o_named && o_named->type == AV_OPT_TYPE_CONST)
207  d = DEFAULT_NUMVAL(o_named);
208  else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
209  else if (!strcmp(buf, "max" )) d = o->max;
210  else if (!strcmp(buf, "min" )) d = o->min;
211  else if (!strcmp(buf, "none" )) d = 0;
212  else if (!strcmp(buf, "all" )) d = ~0;
213  else {
214  int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
215  if (res < 0) {
216  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
217  return res;
218  }
219  }
220  }
221  if (o->type == AV_OPT_TYPE_FLAGS) {
222  read_number(o, dst, NULL, NULL, &intnum);
223  if (cmd == '+') d = intnum | (int64_t)d;
224  else if (cmd == '-') d = intnum &~(int64_t)d;
225  } else {
226  read_number(o, dst, &num, &den, &intnum);
227  if (cmd == '+') d = notfirst*num*intnum/den + d;
228  else if (cmd == '-') d = notfirst*num*intnum/den - d;
229  }
230 
231  if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
232  return ret;
233  val += i;
234  if (!*val)
235  return 0;
236  notfirst = 1;
237  }
238 
239  return 0;
240 }
241 
242 #if FF_API_OLD_AVOPTIONS
243 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
244 {
245  const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
246  if (o_out)
247  *o_out = o;
248  return av_opt_set(obj, name, val, 0);
249 }
250 #endif
251 
252 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
253 {
254  int ret;
255  void *dst, *target_obj;
256  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
257  if (!o || !target_obj)
259  if (!val && (o->type != AV_OPT_TYPE_STRING &&
263  return AVERROR(EINVAL);
264 
265  dst = ((uint8_t*)target_obj) + o->offset;
266  switch (o->type) {
267  case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
268  case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
269  case AV_OPT_TYPE_FLAGS:
270  case AV_OPT_TYPE_INT:
271  case AV_OPT_TYPE_INT64:
272  case AV_OPT_TYPE_FLOAT:
273  case AV_OPT_TYPE_DOUBLE:
274  case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
276  if (!val || !strcmp(val, "none")) {
277  *(int *)dst = *((int *)dst + 1) = 0;
278  return 0;
279  }
280  ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
281  if (ret < 0)
282  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
283  return ret;
285  if (!val) {
286  ret = AVERROR(EINVAL);
287  } else {
288  ret = av_parse_video_rate(dst, val);
289  }
290  if (ret < 0)
291  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
292  return ret;
294  if (!val || !strcmp(val, "none")) {
295  ret = AV_PIX_FMT_NONE;
296  } else {
297  ret = av_get_pix_fmt(val);
298  if (ret == AV_PIX_FMT_NONE) {
299  char *tail;
300  ret = strtol(val, &tail, 0);
301  if (*tail || (unsigned)ret >= AV_PIX_FMT_NB) {
302  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as pixel format\n", val);
303  return AVERROR(EINVAL);
304  }
305  }
306  }
307  *(enum AVPixelFormat *)dst = ret;
308  return 0;
310  if (!val || !strcmp(val, "none")) {
311  ret = AV_SAMPLE_FMT_NONE;
312  } else {
313  ret = av_get_sample_fmt(val);
314  if (ret == AV_SAMPLE_FMT_NONE) {
315  char *tail;
316  ret = strtol(val, &tail, 0);
317  if (*tail || (unsigned)ret >= AV_SAMPLE_FMT_NB) {
318  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as sample format\n", val);
319  return AVERROR(EINVAL);
320  }
321  }
322  }
323  *(enum AVSampleFormat *)dst = ret;
324  return 0;
326  if (!val) {
327  *(int64_t *)dst = 0;
328  return 0;
329  } else {
330  if ((ret = av_parse_time(dst, val, 1)) < 0)
331  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
332  return ret;
333  }
334  break;
335  case AV_OPT_TYPE_COLOR:
336  if (!val) {
337  return 0;
338  } else {
339  ret = av_parse_color(dst, val, -1, obj);
340  if (ret < 0)
341  av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
342  return ret;
343  }
344  }
345 
346  av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
347  return AVERROR(EINVAL);
348 }
349 
350 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
351  int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
352  {\
353  if (!o || o->type != opttype)\
354  return AVERROR(EINVAL);\
355  return set_string_number(obj, obj, o, val, name ## _out);\
356  }
357 
360 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
361 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
362 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
364 
365 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
366  int search_flags)
367 {
368  void *dst, *target_obj;
369  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
370 
371  if (!o || !target_obj)
373 
374  dst = ((uint8_t*)target_obj) + o->offset;
375  return write_number(obj, o, dst, num, den, intnum);
376 }
377 
378 #if FF_API_OLD_AVOPTIONS
379 const AVOption *av_set_double(void *obj, const char *name, double n)
380 {
381  const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
382  if (set_number(obj, name, n, 1, 1, 0) < 0)
383  return NULL;
384  return o;
385 }
386 
387 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
388 {
389  const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
390  if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
391  return NULL;
392  return o;
393 }
394 
395 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
396 {
397  const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
398  if (set_number(obj, name, 1, 1, n, 0) < 0)
399  return NULL;
400  return o;
401 }
402 #endif
403 
404 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
405 {
406  return set_number(obj, name, 1, 1, val, search_flags);
407 }
408 
409 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
410 {
411  return set_number(obj, name, val, 1, 1, search_flags);
412 }
413 
414 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
415 {
416  return set_number(obj, name, val.num, val.den, 1, search_flags);
417 }
418 
419 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
420 {
421  void *target_obj;
422  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
423  uint8_t *ptr;
424  uint8_t **dst;
425  int *lendst;
426 
427  if (!o || !target_obj)
429 
430  if (o->type != AV_OPT_TYPE_BINARY)
431  return AVERROR(EINVAL);
432 
433  ptr = len ? av_malloc(len) : NULL;
434  if (len && !ptr)
435  return AVERROR(ENOMEM);
436 
437  dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
438  lendst = (int *)(dst + 1);
439 
440  av_free(*dst);
441  *dst = ptr;
442  *lendst = len;
443  if (len)
444  memcpy(ptr, val, len);
445 
446  return 0;
447 }
448 
449 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
450 {
451  void *target_obj;
452  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
453 
454  if (!o || !target_obj)
456  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
457  av_log(obj, AV_LOG_ERROR,
458  "The value set by option '%s' is not an image size.\n", o->name);
459  return AVERROR(EINVAL);
460  }
461  if (w<0 || h<0) {
462  av_log(obj, AV_LOG_ERROR,
463  "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
464  return AVERROR(EINVAL);
465  }
466  *(int *)(((uint8_t *)target_obj) + o->offset) = w;
467  *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
468  return 0;
469 }
470 
471 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
472 {
473  void *target_obj;
474  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
475 
476  if (!o || !target_obj)
478  if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
479  av_log(obj, AV_LOG_ERROR,
480  "The value set by option '%s' is not a video rate.\n", o->name);
481  return AVERROR(EINVAL);
482  }
483  if (val.num <= 0 || val.den <= 0)
484  return AVERROR(EINVAL);
485  return set_number(obj, name, val.num, val.den, 1, search_flags);
486 }
487 
488 static int set_format(void *obj, const char *name, int fmt, int search_flags,
489  enum AVOptionType type, const char *desc, int nb_fmts)
490 {
491  void *target_obj;
492  const AVOption *o = av_opt_find2(obj, name, NULL, 0,
493  search_flags, &target_obj);
494  int min, max;
495  const AVClass *class = *(AVClass **)obj;
496 
497  if (!o || !target_obj)
499  if (o->type != type) {
500  av_log(obj, AV_LOG_ERROR,
501  "The value set by option '%s' is not a %s format", name, desc);
502  return AVERROR(EINVAL);
503  }
504 
505 #if LIBAVUTIL_VERSION_MAJOR < 53
506  if (class->version && class->version < AV_VERSION_INT(52, 11, 100)) {
507  min = -1;
508  max = nb_fmts-1;
509  } else
510 #endif
511  {
512  min = FFMIN(o->min, -1);
513  max = FFMAX(o->max, nb_fmts-1);
514  }
515  if (fmt < min || fmt > max) {
516  av_log(obj, AV_LOG_ERROR,
517  "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
518  fmt, name, desc, min, max);
519  return AVERROR(ERANGE);
520  }
521  *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
522  return 0;
523 }
524 
525 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
526 {
527  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
528 }
529 
530 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
531 {
532  return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
533 }
534 
535 #if FF_API_OLD_AVOPTIONS
536 /**
537  *
538  * @param buf a buffer which is used for returning non string values as strings, can be NULL
539  * @param buf_len allocated length in bytes of buf
540  */
541 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
542 {
543  const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
544  void *dst;
545  uint8_t *bin;
546  int len, i;
547  if (!o)
548  return NULL;
549  if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
550  return NULL;
551 
552  dst= ((uint8_t*)obj) + o->offset;
553  if (o_out) *o_out= o;
554 
555  switch (o->type) {
556  case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
557  case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
558  case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
559  case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
560  case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
561  case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
562  case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
563  case AV_OPT_TYPE_STRING: return *(void**)dst;
564  case AV_OPT_TYPE_BINARY:
565  len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
566  if (len >= (buf_len + 1)/2) return NULL;
567  bin = *(uint8_t**)dst;
568  for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
569  break;
570  default: return NULL;
571  }
572  return buf;
573 }
574 #endif
575 
576 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
577 {
578  void *dst, *target_obj;
579  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
580  uint8_t *bin, buf[128];
581  int len, i, ret;
582  int64_t i64;
583 
584  if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
586 
587  dst = (uint8_t*)target_obj + o->offset;
588 
589  buf[0] = 0;
590  switch (o->type) {
591  case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
592  case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
593  case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
594  case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
595  case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
597  case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
598  case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
599  case AV_OPT_TYPE_STRING:
600  if (*(uint8_t**)dst)
601  *out_val = av_strdup(*(uint8_t**)dst);
602  else
603  *out_val = av_strdup("");
604  return 0;
605  case AV_OPT_TYPE_BINARY:
606  len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
607  if ((uint64_t)len*2 + 1 > INT_MAX)
608  return AVERROR(EINVAL);
609  if (!(*out_val = av_malloc(len*2 + 1)))
610  return AVERROR(ENOMEM);
611  bin = *(uint8_t**)dst;
612  for (i = 0; i < len; i++)
613  snprintf(*out_val + i*2, 3, "%02X", bin[i]);
614  return 0;
616  ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
617  break;
619  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
620  break;
622  ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
623  break;
625  i64 = *(int64_t *)dst;
626  ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
627  i64 / 3600000000, (int)((i64 / 60000000) % 60),
628  (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
629  break;
630  case AV_OPT_TYPE_COLOR:
631  ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", ((int *)dst)[0], ((int *)dst)[1], ((int *)dst)[2], ((int *)dst)[3]);
632  break;
633  default:
634  return AVERROR(EINVAL);
635  }
636 
637  if (ret >= sizeof(buf))
638  return AVERROR(EINVAL);
639  *out_val = av_strdup(buf);
640  return 0;
641 }
642 
643 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
644  int search_flags)
645 {
646  void *dst, *target_obj;
647  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
648  if (!o || !target_obj)
649  goto error;
650 
651  dst = ((uint8_t*)target_obj) + o->offset;
652 
653  if (o_out) *o_out= o;
654 
655  return read_number(o, dst, num, den, intnum);
656 
657 error:
658  *den=*intnum=0;
659  return -1;
660 }
661 
662 #if FF_API_OLD_AVOPTIONS
663 double av_get_double(void *obj, const char *name, const AVOption **o_out)
664 {
665  int64_t intnum=1;
666  double num=1;
667  int den=1;
668 
669  if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
670  return NAN;
671  return num*intnum/den;
672 }
673 
674 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
675 {
676  int64_t intnum=1;
677  double num=1;
678  int den=1;
679 
680  if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
681  return (AVRational){0, 0};
682  if (num == 1.0 && (int)intnum == intnum)
683  return (AVRational){intnum, den};
684  else
685  return av_d2q(num*intnum/den, 1<<24);
686 }
687 
688 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
689 {
690  int64_t intnum=1;
691  double num=1;
692  int den=1;
693 
694  if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
695  return -1;
696  return num*intnum/den;
697 }
698 #endif
699 
700 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
701 {
702  int64_t intnum = 1;
703  double num = 1;
704  int ret, den = 1;
705 
706  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
707  return ret;
708  *out_val = num*intnum/den;
709  return 0;
710 }
711 
712 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
713 {
714  int64_t intnum = 1;
715  double num = 1;
716  int ret, den = 1;
717 
718  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
719  return ret;
720  *out_val = num*intnum/den;
721  return 0;
722 }
723 
724 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
725 {
726  int64_t intnum = 1;
727  double num = 1;
728  int ret, den = 1;
729 
730  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
731  return ret;
732 
733  if (num == 1.0 && (int)intnum == intnum)
734  *out_val = (AVRational){intnum, den};
735  else
736  *out_val = av_d2q(num*intnum/den, 1<<24);
737  return 0;
738 }
739 
740 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
741 {
742  void *dst, *target_obj;
743  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
744  if (!o || !target_obj)
746  if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
747  av_log(obj, AV_LOG_ERROR,
748  "The value for option '%s' is not an image size.\n", name);
749  return AVERROR(EINVAL);
750  }
751 
752  dst = ((uint8_t*)target_obj) + o->offset;
753  if (w_out) *w_out = *(int *)dst;
754  if (h_out) *h_out = *((int *)dst+1);
755  return 0;
756 }
757 
758 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
759 {
760  int64_t intnum = 1;
761  double num = 1;
762  int ret, den = 1;
763 
764  if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
765  return ret;
766 
767  if (num == 1.0 && (int)intnum == intnum)
768  *out_val = (AVRational){intnum, den};
769  else
770  *out_val = av_d2q(num*intnum/den, 1<<24);
771  return 0;
772 }
773 
774 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
775  enum AVOptionType type, const char *desc)
776 {
777  void *dst, *target_obj;
778  const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
779  if (!o || !target_obj)
781  if (o->type != type) {
782  av_log(obj, AV_LOG_ERROR,
783  "The value for option '%s' is not a %s format.\n", desc, name);
784  return AVERROR(EINVAL);
785  }
786 
787  dst = ((uint8_t*)target_obj) + o->offset;
788  *out_fmt = *(int *)dst;
789  return 0;
790 }
791 
792 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
793 {
794  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
795 }
796 
797 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
798 {
799  return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
800 }
801 
802 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
803 {
804  const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
805  const AVOption *flag = av_opt_find(obj, flag_name,
806  field ? field->unit : NULL, 0, 0);
807  int64_t res;
808 
809  if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
810  av_opt_get_int(obj, field_name, 0, &res) < 0)
811  return 0;
812  return res & flag->default_val.i64;
813 }
814 
815 static void log_value(void *av_log_obj, int level, double d)
816 {
817  if (d == INT_MAX) {
818  av_log(av_log_obj, level, "INT_MAX");
819  } else if (d == INT_MIN) {
820  av_log(av_log_obj, level, "INT_MIN");
821  } else if (d == (double)INT64_MAX) {
822  av_log(av_log_obj, level, "I64_MAX");
823  } else if (d == INT64_MIN) {
824  av_log(av_log_obj, level, "I64_MIN");
825  } else if (d == FLT_MAX) {
826  av_log(av_log_obj, level, "FLT_MAX");
827  } else if (d == FLT_MIN) {
828  av_log(av_log_obj, level, "FLT_MIN");
829  } else {
830  av_log(av_log_obj, level, "%g", d);
831  }
832 }
833 
834 static void opt_list(void *obj, void *av_log_obj, const char *unit,
835  int req_flags, int rej_flags)
836 {
837  const AVOption *opt=NULL;
838  AVOptionRanges *r;
839  int i;
840 
841  while ((opt = av_opt_next(obj, opt))) {
842  if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
843  continue;
844 
845  /* Don't print CONST's on level one.
846  * Don't print anything but CONST's on level two.
847  * Only print items from the requested unit.
848  */
849  if (!unit && opt->type==AV_OPT_TYPE_CONST)
850  continue;
851  else if (unit && opt->type!=AV_OPT_TYPE_CONST)
852  continue;
853  else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
854  continue;
855  else if (unit && opt->type == AV_OPT_TYPE_CONST)
856  av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
857  else
858  av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
859  (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
860  opt->name);
861 
862  switch (opt->type) {
863  case AV_OPT_TYPE_FLAGS:
864  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
865  break;
866  case AV_OPT_TYPE_INT:
867  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
868  break;
869  case AV_OPT_TYPE_INT64:
870  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
871  break;
872  case AV_OPT_TYPE_DOUBLE:
873  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
874  break;
875  case AV_OPT_TYPE_FLOAT:
876  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
877  break;
878  case AV_OPT_TYPE_STRING:
879  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
880  break;
882  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
883  break;
884  case AV_OPT_TYPE_BINARY:
885  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
886  break;
888  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
889  break;
891  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
892  break;
894  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
895  break;
897  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
898  break;
900  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
901  break;
902  case AV_OPT_TYPE_COLOR:
903  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
904  break;
905  case AV_OPT_TYPE_CONST:
906  default:
907  av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
908  break;
909  }
910  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
911  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
912  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
913  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
914  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
915  av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
916 
917  if (opt->help)
918  av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
919 
920  if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
921  switch (opt->type) {
922  case AV_OPT_TYPE_INT:
923  case AV_OPT_TYPE_INT64:
924  case AV_OPT_TYPE_DOUBLE:
925  case AV_OPT_TYPE_FLOAT:
927  for (i = 0; i < r->nb_ranges; i++) {
928  av_log(av_log_obj, AV_LOG_INFO, " (from ");
929  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
930  av_log(av_log_obj, AV_LOG_INFO, " to ");
931  log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
932  av_log(av_log_obj, AV_LOG_INFO, ")");
933  }
934  break;
935  }
937  }
938 
939  av_log(av_log_obj, AV_LOG_INFO, "\n");
940  if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
941  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
942  }
943  }
944 }
945 
946 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
947 {
948  if (!obj)
949  return -1;
950 
951  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
952 
953  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
954 
955  return 0;
956 }
957 
959 {
960 #if FF_API_OLD_AVOPTIONS
961  av_opt_set_defaults2(s, 0, 0);
962 }
963 
964 void av_opt_set_defaults2(void *s, int mask, int flags)
965 {
966 #endif
967  const AVClass *class = *(AVClass **)s;
968  const AVOption *opt = NULL;
969  while ((opt = av_opt_next(s, opt)) != NULL) {
970 #if FF_API_OLD_AVOPTIONS
971  if ((opt->flags & mask) != flags)
972  continue;
973 #endif
974  switch (opt->type) {
975  case AV_OPT_TYPE_CONST:
976  /* Nothing to be done here */
977  break;
978  case AV_OPT_TYPE_FLAGS:
979  case AV_OPT_TYPE_INT:
980  case AV_OPT_TYPE_INT64:
982  av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
983  break;
984  case AV_OPT_TYPE_DOUBLE:
985  case AV_OPT_TYPE_FLOAT: {
986  double val;
987  val = opt->default_val.dbl;
988  av_opt_set_double(s, opt->name, val, 0);
989  }
990  break;
991  case AV_OPT_TYPE_RATIONAL: {
992  AVRational val;
993  val = av_d2q(opt->default_val.dbl, INT_MAX);
994  av_opt_set_q(s, opt->name, val, 0);
995  }
996  break;
997  case AV_OPT_TYPE_COLOR:
998  case AV_OPT_TYPE_STRING:
1001  av_opt_set(s, opt->name, opt->default_val.str, 0);
1002  break;
1003  case AV_OPT_TYPE_PIXEL_FMT:
1004 #if LIBAVUTIL_VERSION_MAJOR < 53
1005  if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1006  av_opt_set(s, opt->name, opt->default_val.str, 0);
1007  else
1008 #endif
1009  av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
1010  break;
1012 #if LIBAVUTIL_VERSION_MAJOR < 53
1013  if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1014  av_opt_set(s, opt->name, opt->default_val.str, 0);
1015  else
1016 #endif
1017  av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
1018  break;
1019  case AV_OPT_TYPE_BINARY:
1020  /* Cannot set default for binary */
1021  break;
1022  default:
1023  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1024  }
1025  }
1026 }
1027 
1028 /**
1029  * Store the value in the field in ctx that is named like key.
1030  * ctx must be an AVClass context, storing is done using AVOptions.
1031  *
1032  * @param buf the string to parse, buf will be updated to point at the
1033  * separator just after the parsed key/value pair
1034  * @param key_val_sep a 0-terminated list of characters used to
1035  * separate key from value
1036  * @param pairs_sep a 0-terminated list of characters used to separate
1037  * two pairs from each other
1038  * @return 0 if the key/value pair has been successfully parsed and
1039  * set, or a negative value corresponding to an AVERROR code in case
1040  * of error:
1041  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1042  * the error code issued by av_opt_set() if the key/value pair
1043  * cannot be set
1044  */
1045 static int parse_key_value_pair(void *ctx, const char **buf,
1046  const char *key_val_sep, const char *pairs_sep)
1047 {
1048  char *key = av_get_token(buf, key_val_sep);
1049  char *val;
1050  int ret;
1051 
1052  if (!key)
1053  return AVERROR(ENOMEM);
1054 
1055  if (*key && strspn(*buf, key_val_sep)) {
1056  (*buf)++;
1057  val = av_get_token(buf, pairs_sep);
1058  if (!val) {
1059  av_freep(&key);
1060  return AVERROR(ENOMEM);
1061  }
1062  } else {
1063  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1064  av_free(key);
1065  return AVERROR(EINVAL);
1066  }
1067 
1068  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1069 
1070  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1071  if (ret == AVERROR_OPTION_NOT_FOUND)
1072  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1073 
1074  av_free(key);
1075  av_free(val);
1076  return ret;
1077 }
1078 
1079 int av_set_options_string(void *ctx, const char *opts,
1080  const char *key_val_sep, const char *pairs_sep)
1081 {
1082  int ret, count = 0;
1083 
1084  if (!opts)
1085  return 0;
1086 
1087  while (*opts) {
1088  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1089  return ret;
1090  count++;
1091 
1092  if (*opts)
1093  opts++;
1094  }
1095 
1096  return count;
1097 }
1098 
1099 #define WHITESPACES " \n\t"
1100 
1101 static int is_key_char(char c)
1102 {
1103  return (unsigned)((c | 32) - 'a') < 26 ||
1104  (unsigned)(c - '0') < 10 ||
1105  c == '-' || c == '_' || c == '/' || c == '.';
1106 }
1107 
1108 /**
1109  * Read a key from a string.
1110  *
1111  * The key consists of is_key_char characters and must be terminated by a
1112  * character from the delim string; spaces are ignored.
1113  *
1114  * @return 0 for success (even with ellipsis), <0 for failure
1115  */
1116 static int get_key(const char **ropts, const char *delim, char **rkey)
1117 {
1118  const char *opts = *ropts;
1119  const char *key_start, *key_end;
1120 
1121  key_start = opts += strspn(opts, WHITESPACES);
1122  while (is_key_char(*opts))
1123  opts++;
1124  key_end = opts;
1125  opts += strspn(opts, WHITESPACES);
1126  if (!*opts || !strchr(delim, *opts))
1127  return AVERROR(EINVAL);
1128  opts++;
1129  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1130  return AVERROR(ENOMEM);
1131  memcpy(*rkey, key_start, key_end - key_start);
1132  (*rkey)[key_end - key_start] = 0;
1133  *ropts = opts;
1134  return 0;
1135 }
1136 
1137 int av_opt_get_key_value(const char **ropts,
1138  const char *key_val_sep, const char *pairs_sep,
1139  unsigned flags,
1140  char **rkey, char **rval)
1141 {
1142  int ret;
1143  char *key = NULL, *val;
1144  const char *opts = *ropts;
1145 
1146  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1147  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1148  return AVERROR(EINVAL);
1149  if (!(val = av_get_token(&opts, pairs_sep))) {
1150  av_free(key);
1151  return AVERROR(ENOMEM);
1152  }
1153  *ropts = opts;
1154  *rkey = key;
1155  *rval = val;
1156  return 0;
1157 }
1158 
1159 int av_opt_set_from_string(void *ctx, const char *opts,
1160  const char *const *shorthand,
1161  const char *key_val_sep, const char *pairs_sep)
1162 {
1163  int ret, count = 0;
1164  const char *dummy_shorthand = NULL;
1165  char *av_uninit(parsed_key), *av_uninit(value);
1166  const char *key;
1167 
1168  if (!opts)
1169  return 0;
1170  if (!shorthand)
1171  shorthand = &dummy_shorthand;
1172 
1173  while (*opts) {
1174  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1175  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1176  &parsed_key, &value);
1177  if (ret < 0) {
1178  if (ret == AVERROR(EINVAL))
1179  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1180  else
1181  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1182  av_err2str(ret));
1183  return ret;
1184  }
1185  if (*opts)
1186  opts++;
1187  if (parsed_key) {
1188  key = parsed_key;
1189  while (*shorthand) /* discard all remaining shorthand */
1190  shorthand++;
1191  } else {
1192  key = *(shorthand++);
1193  }
1194 
1195  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1196  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1197  if (ret == AVERROR_OPTION_NOT_FOUND)
1198  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1199  av_free(value);
1200  av_free(parsed_key);
1201  return ret;
1202  }
1203 
1204  av_free(value);
1205  av_free(parsed_key);
1206  count++;
1207  }
1208  return count;
1209 }
1210 
1211 void av_opt_free(void *obj)
1212 {
1213  const AVOption *o = NULL;
1214  while ((o = av_opt_next(obj, o)))
1215  if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1216  av_freep((uint8_t *)obj + o->offset);
1217 }
1218 
1220 {
1222  AVDictionary *tmp = NULL;
1223  int ret = 0;
1224 
1225  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1226  ret = av_opt_set(obj, t->key, t->value, 0);
1227  if (ret == AVERROR_OPTION_NOT_FOUND)
1228  av_dict_set(&tmp, t->key, t->value, 0);
1229  else if (ret < 0) {
1230  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1231  break;
1232  }
1233  ret = 0;
1234  }
1235  av_dict_free(options);
1236  *options = tmp;
1237  return ret;
1238 }
1239 
1240 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1241  int opt_flags, int search_flags)
1242 {
1243  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1244 }
1245 
1246 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1247  int opt_flags, int search_flags, void **target_obj)
1248 {
1249  const AVClass *c;
1250  const AVOption *o = NULL;
1251 
1252  if(!obj)
1253  return NULL;
1254 
1255  c= *(AVClass**)obj;
1256 
1257  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1258  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1259  const AVClass *child = NULL;
1260  while (child = av_opt_child_class_next(c, child))
1261  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1262  return o;
1263  } else {
1264  void *child = NULL;
1265  while (child = av_opt_child_next(obj, child))
1266  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1267  return o;
1268  }
1269  }
1270 
1271  while (o = av_opt_next(obj, o)) {
1272  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1273  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1274  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1275  if (target_obj) {
1276  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1277  *target_obj = obj;
1278  else
1279  *target_obj = NULL;
1280  }
1281  return o;
1282  }
1283  }
1284  return NULL;
1285 }
1286 
1287 void *av_opt_child_next(void *obj, void *prev)
1288 {
1289  const AVClass *c = *(AVClass**)obj;
1290  if (c->child_next)
1291  return c->child_next(obj, prev);
1292  return NULL;
1293 }
1294 
1295 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1296 {
1297  if (parent->child_class_next)
1298  return parent->child_class_next(prev);
1299  return NULL;
1300 }
1301 
1302 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1303 {
1304  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1305  if(!opt)
1306  return NULL;
1307  return (uint8_t*)obj + opt->offset;
1308 }
1309 
1310 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1311 {
1312  const AVClass *c = *(AVClass**)obj;
1313  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1314 
1315  if (c->version > (52 << 16 | 11 << 8))
1316  callback = c->query_ranges;
1317 
1318  if (!callback)
1320 
1321  return callback(ranges_arg, obj, key, flags);
1322 }
1323 
1324 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1325 {
1326  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1327  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1328  AVOptionRange *range = av_mallocz(sizeof(*range));
1329  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1330  int ret;
1331 
1332  *ranges_arg = NULL;
1333 
1334  if (!ranges || !range || !range_array || !field) {
1335  ret = AVERROR(ENOMEM);
1336  goto fail;
1337  }
1338 
1339  ranges->range = range_array;
1340  ranges->range[0] = range;
1341  ranges->nb_ranges = 1;
1342  range->is_range = 1;
1343  range->value_min = field->min;
1344  range->value_max = field->max;
1345 
1346  switch (field->type) {
1347  case AV_OPT_TYPE_INT:
1348  case AV_OPT_TYPE_INT64:
1349  case AV_OPT_TYPE_PIXEL_FMT:
1351  case AV_OPT_TYPE_FLOAT:
1352  case AV_OPT_TYPE_DOUBLE:
1353  case AV_OPT_TYPE_DURATION:
1354  case AV_OPT_TYPE_COLOR:
1355  break;
1356  case AV_OPT_TYPE_STRING:
1357  range->component_min = 0;
1358  range->component_max = 0x10FFFF; // max unicode value
1359  range->value_min = -1;
1360  range->value_max = INT_MAX;
1361  break;
1362  case AV_OPT_TYPE_RATIONAL:
1363  range->component_min = INT_MIN;
1364  range->component_max = INT_MAX;
1365  break;
1367  range->component_min = 0;
1368  range->component_max = INT_MAX/128/8;
1369  range->value_min = 0;
1370  range->value_max = INT_MAX/8;
1371  break;
1373  range->component_min = 1;
1374  range->component_max = INT_MAX;
1375  range->value_min = 1;
1376  range->value_max = INT_MAX;
1377  break;
1378  default:
1379  ret = AVERROR(ENOSYS);
1380  goto fail;
1381  }
1382 
1383  *ranges_arg = ranges;
1384  return 0;
1385 fail:
1386  av_free(ranges);
1387  av_free(range);
1388  av_free(range_array);
1389  return ret;
1390 }
1391 
1393 {
1394  int i;
1395  AVOptionRanges *ranges = *rangesp;
1396 
1397  for (i = 0; i < ranges->nb_ranges; i++) {
1398  AVOptionRange *range = ranges->range[i];
1399  av_freep(&range->str);
1400  av_freep(&ranges->range[i]);
1401  }
1402  av_freep(&ranges->range);
1403  av_freep(rangesp);
1404 }
1405 
1406 #ifdef TEST
1407 
1408 typedef struct TestContext
1409 {
1410  const AVClass *class;
1411  int num;
1412  int toggle;
1413  char *string;
1414  int flags;
1415  AVRational rational;
1416  AVRational video_rate;
1417  int w, h;
1418  enum AVPixelFormat pix_fmt;
1419  enum AVSampleFormat sample_fmt;
1420  int64_t duration;
1421  uint8_t color[4];
1422 } TestContext;
1423 
1424 #define OFFSET(x) offsetof(TestContext, x)
1425 
1426 #define TEST_FLAG_COOL 01
1427 #define TEST_FLAG_LAME 02
1428 #define TEST_FLAG_MU 04
1429 
1430 static const AVOption test_options[]= {
1431 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1432 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1433 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1434 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1435 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1436 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1437 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1438 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1439 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1440 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
1441 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
1442 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1443 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1444 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1445 {NULL},
1446 };
1447 
1448 static const char *test_get_name(void *ctx)
1449 {
1450  return "test";
1451 }
1452 
1453 static const AVClass test_class = {
1454  "TestContext",
1455  test_get_name,
1456  test_options
1457 };
1458 
1459 int main(void)
1460 {
1461  int i;
1462 
1463  printf("\nTesting av_set_options_string()\n");
1464  {
1465  TestContext test_ctx = { 0 };
1466  const char *options[] = {
1467  "",
1468  ":",
1469  "=",
1470  "foo=:",
1471  ":=foo",
1472  "=foo",
1473  "foo=",
1474  "foo",
1475  "foo=val",
1476  "foo==val",
1477  "toggle=:",
1478  "string=:",
1479  "toggle=1 : foo",
1480  "toggle=100",
1481  "toggle==1",
1482  "flags=+mu-lame : num=42: toggle=0",
1483  "num=42 : string=blahblah",
1484  "rational=0 : rational=1/2 : rational=1/-1",
1485  "rational=-1/0",
1486  "size=1024x768",
1487  "size=pal",
1488  "size=bogus",
1489  "pix_fmt=yuv420p",
1490  "pix_fmt=2",
1491  "pix_fmt=bogus",
1492  "sample_fmt=s16",
1493  "sample_fmt=2",
1494  "sample_fmt=bogus",
1495  "video_rate=pal",
1496  "video_rate=25",
1497  "video_rate=30000/1001",
1498  "video_rate=30/1.001",
1499  "video_rate=bogus",
1500  "duration=bogus",
1501  "duration=123.45",
1502  "duration=1\\:23\\:45.67",
1503  "color=blue",
1504  "color=0x223300",
1505  "color=0x42FF07AA",
1506  };
1507 
1508  test_ctx.class = &test_class;
1509  av_opt_set_defaults(&test_ctx);
1510 
1512 
1513  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1514  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1515  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1516  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1517  printf("\n");
1518  }
1519  av_freep(&test_ctx.string);
1520  }
1521 
1522  printf("\nTesting av_opt_set_from_string()\n");
1523  {
1524  TestContext test_ctx = { 0 };
1525  const char *options[] = {
1526  "",
1527  "5",
1528  "5:hello",
1529  "5:hello:size=pal",
1530  "5:size=pal:hello",
1531  ":",
1532  "=",
1533  " 5 : hello : size = pal ",
1534  "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1535  };
1536  const char *shorthand[] = { "num", "string", NULL };
1537 
1538  test_ctx.class = &test_class;
1539  av_opt_set_defaults(&test_ctx);
1540  test_ctx.string = av_strdup("default");
1541 
1543 
1544  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1545  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1546  if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1547  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1548  printf("\n");
1549  }
1550  av_freep(&test_ctx.string);
1551  }
1552 
1553  return 0;
1554 }
1555 
1556 #endif