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  if (opt->type != AV_OPT_TYPE_CONST &&
940  opt->type != AV_OPT_TYPE_BINARY &&
941  !((opt->type == AV_OPT_TYPE_COLOR ||
942  opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
943  opt->type == AV_OPT_TYPE_STRING ||
944  opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
945  !opt->default_val.str)) {
946  av_log(av_log_obj, AV_LOG_INFO, " (default ");
947  switch (opt->type) {
948  case AV_OPT_TYPE_FLAGS:
949  av_log(av_log_obj, AV_LOG_INFO, "%0llX", opt->default_val.i64);
950  break;
952  case AV_OPT_TYPE_INT:
953  case AV_OPT_TYPE_INT64:
954  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
955  break;
956  case AV_OPT_TYPE_DOUBLE:
957  case AV_OPT_TYPE_FLOAT:
958  log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
959  break;
960  case AV_OPT_TYPE_RATIONAL: {
961  AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
962  av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
963  break;
964  case AV_OPT_TYPE_COLOR:
966  case AV_OPT_TYPE_STRING:
968  av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
969  }
970  av_log(av_log_obj, AV_LOG_INFO, ")");
971  }
972 
973  av_log(av_log_obj, AV_LOG_INFO, "\n");
974  if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
975  opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
976  }
977  }
978 }
979 
980 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
981 {
982  if (!obj)
983  return -1;
984 
985  av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
986 
987  opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
988 
989  return 0;
990 }
991 
993 {
994 #if FF_API_OLD_AVOPTIONS
995  av_opt_set_defaults2(s, 0, 0);
996 }
997 
998 void av_opt_set_defaults2(void *s, int mask, int flags)
999 {
1000 #endif
1001  const AVClass *class = *(AVClass **)s;
1002  const AVOption *opt = NULL;
1003  while ((opt = av_opt_next(s, opt)) != NULL) {
1004 #if FF_API_OLD_AVOPTIONS
1005  if ((opt->flags & mask) != flags)
1006  continue;
1007 #endif
1008  switch (opt->type) {
1009  case AV_OPT_TYPE_CONST:
1010  /* Nothing to be done here */
1011  break;
1012  case AV_OPT_TYPE_FLAGS:
1013  case AV_OPT_TYPE_INT:
1014  case AV_OPT_TYPE_INT64:
1015  case AV_OPT_TYPE_DURATION:
1016  av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
1017  break;
1018  case AV_OPT_TYPE_DOUBLE:
1019  case AV_OPT_TYPE_FLOAT: {
1020  double val;
1021  val = opt->default_val.dbl;
1022  av_opt_set_double(s, opt->name, val, 0);
1023  }
1024  break;
1025  case AV_OPT_TYPE_RATIONAL: {
1026  AVRational val;
1027  val = av_d2q(opt->default_val.dbl, INT_MAX);
1028  av_opt_set_q(s, opt->name, val, 0);
1029  }
1030  break;
1031  case AV_OPT_TYPE_COLOR:
1032  case AV_OPT_TYPE_STRING:
1035  av_opt_set(s, opt->name, opt->default_val.str, 0);
1036  break;
1037  case AV_OPT_TYPE_PIXEL_FMT:
1038 #if LIBAVUTIL_VERSION_MAJOR < 53
1039  if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1040  av_opt_set(s, opt->name, opt->default_val.str, 0);
1041  else
1042 #endif
1043  av_opt_set_pixel_fmt(s, opt->name, opt->default_val.i64, 0);
1044  break;
1046 #if LIBAVUTIL_VERSION_MAJOR < 53
1047  if (class->version && class->version < AV_VERSION_INT(52, 10, 100))
1048  av_opt_set(s, opt->name, opt->default_val.str, 0);
1049  else
1050 #endif
1051  av_opt_set_sample_fmt(s, opt->name, opt->default_val.i64, 0);
1052  break;
1053  case AV_OPT_TYPE_BINARY:
1054  /* Cannot set default for binary */
1055  break;
1056  default:
1057  av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
1058  }
1059  }
1060 }
1061 
1062 /**
1063  * Store the value in the field in ctx that is named like key.
1064  * ctx must be an AVClass context, storing is done using AVOptions.
1065  *
1066  * @param buf the string to parse, buf will be updated to point at the
1067  * separator just after the parsed key/value pair
1068  * @param key_val_sep a 0-terminated list of characters used to
1069  * separate key from value
1070  * @param pairs_sep a 0-terminated list of characters used to separate
1071  * two pairs from each other
1072  * @return 0 if the key/value pair has been successfully parsed and
1073  * set, or a negative value corresponding to an AVERROR code in case
1074  * of error:
1075  * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1076  * the error code issued by av_opt_set() if the key/value pair
1077  * cannot be set
1078  */
1079 static int parse_key_value_pair(void *ctx, const char **buf,
1080  const char *key_val_sep, const char *pairs_sep)
1081 {
1082  char *key = av_get_token(buf, key_val_sep);
1083  char *val;
1084  int ret;
1085 
1086  if (!key)
1087  return AVERROR(ENOMEM);
1088 
1089  if (*key && strspn(*buf, key_val_sep)) {
1090  (*buf)++;
1091  val = av_get_token(buf, pairs_sep);
1092  if (!val) {
1093  av_freep(&key);
1094  return AVERROR(ENOMEM);
1095  }
1096  } else {
1097  av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1098  av_free(key);
1099  return AVERROR(EINVAL);
1100  }
1101 
1102  av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1103 
1104  ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1105  if (ret == AVERROR_OPTION_NOT_FOUND)
1106  av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1107 
1108  av_free(key);
1109  av_free(val);
1110  return ret;
1111 }
1112 
1113 int av_set_options_string(void *ctx, const char *opts,
1114  const char *key_val_sep, const char *pairs_sep)
1115 {
1116  int ret, count = 0;
1117 
1118  if (!opts)
1119  return 0;
1120 
1121  while (*opts) {
1122  if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1123  return ret;
1124  count++;
1125 
1126  if (*opts)
1127  opts++;
1128  }
1129 
1130  return count;
1131 }
1132 
1133 #define WHITESPACES " \n\t"
1134 
1135 static int is_key_char(char c)
1136 {
1137  return (unsigned)((c | 32) - 'a') < 26 ||
1138  (unsigned)(c - '0') < 10 ||
1139  c == '-' || c == '_' || c == '/' || c == '.';
1140 }
1141 
1142 /**
1143  * Read a key from a string.
1144  *
1145  * The key consists of is_key_char characters and must be terminated by a
1146  * character from the delim string; spaces are ignored.
1147  *
1148  * @return 0 for success (even with ellipsis), <0 for failure
1149  */
1150 static int get_key(const char **ropts, const char *delim, char **rkey)
1151 {
1152  const char *opts = *ropts;
1153  const char *key_start, *key_end;
1154 
1155  key_start = opts += strspn(opts, WHITESPACES);
1156  while (is_key_char(*opts))
1157  opts++;
1158  key_end = opts;
1159  opts += strspn(opts, WHITESPACES);
1160  if (!*opts || !strchr(delim, *opts))
1161  return AVERROR(EINVAL);
1162  opts++;
1163  if (!(*rkey = av_malloc(key_end - key_start + 1)))
1164  return AVERROR(ENOMEM);
1165  memcpy(*rkey, key_start, key_end - key_start);
1166  (*rkey)[key_end - key_start] = 0;
1167  *ropts = opts;
1168  return 0;
1169 }
1170 
1171 int av_opt_get_key_value(const char **ropts,
1172  const char *key_val_sep, const char *pairs_sep,
1173  unsigned flags,
1174  char **rkey, char **rval)
1175 {
1176  int ret;
1177  char *key = NULL, *val;
1178  const char *opts = *ropts;
1179 
1180  if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1181  !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1182  return AVERROR(EINVAL);
1183  if (!(val = av_get_token(&opts, pairs_sep))) {
1184  av_free(key);
1185  return AVERROR(ENOMEM);
1186  }
1187  *ropts = opts;
1188  *rkey = key;
1189  *rval = val;
1190  return 0;
1191 }
1192 
1193 int av_opt_set_from_string(void *ctx, const char *opts,
1194  const char *const *shorthand,
1195  const char *key_val_sep, const char *pairs_sep)
1196 {
1197  int ret, count = 0;
1198  const char *dummy_shorthand = NULL;
1199  char *av_uninit(parsed_key), *av_uninit(value);
1200  const char *key;
1201 
1202  if (!opts)
1203  return 0;
1204  if (!shorthand)
1205  shorthand = &dummy_shorthand;
1206 
1207  while (*opts) {
1208  ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1209  *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1210  &parsed_key, &value);
1211  if (ret < 0) {
1212  if (ret == AVERROR(EINVAL))
1213  av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1214  else
1215  av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1216  av_err2str(ret));
1217  return ret;
1218  }
1219  if (*opts)
1220  opts++;
1221  if (parsed_key) {
1222  key = parsed_key;
1223  while (*shorthand) /* discard all remaining shorthand */
1224  shorthand++;
1225  } else {
1226  key = *(shorthand++);
1227  }
1228 
1229  av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1230  if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1231  if (ret == AVERROR_OPTION_NOT_FOUND)
1232  av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1233  av_free(value);
1234  av_free(parsed_key);
1235  return ret;
1236  }
1237 
1238  av_free(value);
1239  av_free(parsed_key);
1240  count++;
1241  }
1242  return count;
1243 }
1244 
1245 void av_opt_free(void *obj)
1246 {
1247  const AVOption *o = NULL;
1248  while ((o = av_opt_next(obj, o)))
1249  if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
1250  av_freep((uint8_t *)obj + o->offset);
1251 }
1252 
1254 {
1255  AVDictionaryEntry *t = NULL;
1256  AVDictionary *tmp = NULL;
1257  int ret = 0;
1258 
1259  while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1260  ret = av_opt_set(obj, t->key, t->value, 0);
1261  if (ret == AVERROR_OPTION_NOT_FOUND)
1262  av_dict_set(&tmp, t->key, t->value, 0);
1263  else if (ret < 0) {
1264  av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1265  break;
1266  }
1267  ret = 0;
1268  }
1269  av_dict_free(options);
1270  *options = tmp;
1271  return ret;
1272 }
1273 
1274 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1275  int opt_flags, int search_flags)
1276 {
1277  return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1278 }
1279 
1280 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1281  int opt_flags, int search_flags, void **target_obj)
1282 {
1283  const AVClass *c;
1284  const AVOption *o = NULL;
1285 
1286  if(!obj)
1287  return NULL;
1288 
1289  c= *(AVClass**)obj;
1290 
1291  if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1292  if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1293  const AVClass *child = NULL;
1294  while (child = av_opt_child_class_next(c, child))
1295  if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1296  return o;
1297  } else {
1298  void *child = NULL;
1299  while (child = av_opt_child_next(obj, child))
1300  if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1301  return o;
1302  }
1303  }
1304 
1305  while (o = av_opt_next(obj, o)) {
1306  if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1307  ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1308  (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1309  if (target_obj) {
1310  if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1311  *target_obj = obj;
1312  else
1313  *target_obj = NULL;
1314  }
1315  return o;
1316  }
1317  }
1318  return NULL;
1319 }
1320 
1321 void *av_opt_child_next(void *obj, void *prev)
1322 {
1323  const AVClass *c = *(AVClass**)obj;
1324  if (c->child_next)
1325  return c->child_next(obj, prev);
1326  return NULL;
1327 }
1328 
1329 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1330 {
1331  if (parent->child_class_next)
1332  return parent->child_class_next(prev);
1333  return NULL;
1334 }
1335 
1336 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1337 {
1338  const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1339  if(!opt)
1340  return NULL;
1341  return (uint8_t*)obj + opt->offset;
1342 }
1343 
1344 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1345 {
1346  const AVClass *c = *(AVClass**)obj;
1347  int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1348 
1349  if (c->version > (52 << 16 | 11 << 8))
1350  callback = c->query_ranges;
1351 
1352  if (!callback)
1354 
1355  return callback(ranges_arg, obj, key, flags);
1356 }
1357 
1358 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1359 {
1360  AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1361  AVOptionRange **range_array = av_mallocz(sizeof(void*));
1362  AVOptionRange *range = av_mallocz(sizeof(*range));
1363  const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1364  int ret;
1365 
1366  *ranges_arg = NULL;
1367 
1368  if (!ranges || !range || !range_array || !field) {
1369  ret = AVERROR(ENOMEM);
1370  goto fail;
1371  }
1372 
1373  ranges->range = range_array;
1374  ranges->range[0] = range;
1375  ranges->nb_ranges = 1;
1376  range->is_range = 1;
1377  range->value_min = field->min;
1378  range->value_max = field->max;
1379 
1380  switch (field->type) {
1381  case AV_OPT_TYPE_INT:
1382  case AV_OPT_TYPE_INT64:
1383  case AV_OPT_TYPE_PIXEL_FMT:
1385  case AV_OPT_TYPE_FLOAT:
1386  case AV_OPT_TYPE_DOUBLE:
1387  case AV_OPT_TYPE_DURATION:
1388  case AV_OPT_TYPE_COLOR:
1389  break;
1390  case AV_OPT_TYPE_STRING:
1391  range->component_min = 0;
1392  range->component_max = 0x10FFFF; // max unicode value
1393  range->value_min = -1;
1394  range->value_max = INT_MAX;
1395  break;
1396  case AV_OPT_TYPE_RATIONAL:
1397  range->component_min = INT_MIN;
1398  range->component_max = INT_MAX;
1399  break;
1401  range->component_min = 0;
1402  range->component_max = INT_MAX/128/8;
1403  range->value_min = 0;
1404  range->value_max = INT_MAX/8;
1405  break;
1407  range->component_min = 1;
1408  range->component_max = INT_MAX;
1409  range->value_min = 1;
1410  range->value_max = INT_MAX;
1411  break;
1412  default:
1413  ret = AVERROR(ENOSYS);
1414  goto fail;
1415  }
1416 
1417  *ranges_arg = ranges;
1418  return 0;
1419 fail:
1420  av_free(ranges);
1421  av_free(range);
1422  av_free(range_array);
1423  return ret;
1424 }
1425 
1427 {
1428  int i;
1429  AVOptionRanges *ranges = *rangesp;
1430 
1431  for (i = 0; i < ranges->nb_ranges; i++) {
1432  AVOptionRange *range = ranges->range[i];
1433  av_freep(&range->str);
1434  av_freep(&ranges->range[i]);
1435  }
1436  av_freep(&ranges->range);
1437  av_freep(rangesp);
1438 }
1439 
1440 #ifdef TEST
1441 
1442 typedef struct TestContext
1443 {
1444  const AVClass *class;
1445  int num;
1446  int toggle;
1447  char *string;
1448  int flags;
1449  AVRational rational;
1450  AVRational video_rate;
1451  int w, h;
1452  enum AVPixelFormat pix_fmt;
1453  enum AVSampleFormat sample_fmt;
1454  int64_t duration;
1455  uint8_t color[4];
1456 } TestContext;
1457 
1458 #define OFFSET(x) offsetof(TestContext, x)
1459 
1460 #define TEST_FLAG_COOL 01
1461 #define TEST_FLAG_LAME 02
1462 #define TEST_FLAG_MU 04
1463 
1464 static const AVOption test_options[]= {
1465 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
1466 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
1467 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
1468 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {.str = "default"}, CHAR_MIN, CHAR_MAX },
1469 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
1470 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
1471 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
1472 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
1473 {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 },
1474 {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
1475 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
1476 {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 },
1477 {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
1478 {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, {.str = "pink"}, 0, 0},
1479 {NULL},
1480 };
1481 
1482 static const char *test_get_name(void *ctx)
1483 {
1484  return "test";
1485 }
1486 
1487 static const AVClass test_class = {
1488  "TestContext",
1489  test_get_name,
1490  test_options
1491 };
1492 
1493 int main(void)
1494 {
1495  int i;
1496 
1497  printf("\nTesting av_set_options_string()\n");
1498  {
1499  TestContext test_ctx = { 0 };
1500  const char *options[] = {
1501  "",
1502  ":",
1503  "=",
1504  "foo=:",
1505  ":=foo",
1506  "=foo",
1507  "foo=",
1508  "foo",
1509  "foo=val",
1510  "foo==val",
1511  "toggle=:",
1512  "string=:",
1513  "toggle=1 : foo",
1514  "toggle=100",
1515  "toggle==1",
1516  "flags=+mu-lame : num=42: toggle=0",
1517  "num=42 : string=blahblah",
1518  "rational=0 : rational=1/2 : rational=1/-1",
1519  "rational=-1/0",
1520  "size=1024x768",
1521  "size=pal",
1522  "size=bogus",
1523  "pix_fmt=yuv420p",
1524  "pix_fmt=2",
1525  "pix_fmt=bogus",
1526  "sample_fmt=s16",
1527  "sample_fmt=2",
1528  "sample_fmt=bogus",
1529  "video_rate=pal",
1530  "video_rate=25",
1531  "video_rate=30000/1001",
1532  "video_rate=30/1.001",
1533  "video_rate=bogus",
1534  "duration=bogus",
1535  "duration=123.45",
1536  "duration=1\\:23\\:45.67",
1537  "color=blue",
1538  "color=0x223300",
1539  "color=0x42FF07AA",
1540  };
1541 
1542  test_ctx.class = &test_class;
1543  av_opt_set_defaults(&test_ctx);
1544 
1546 
1547  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1548  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1549  if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
1550  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1551  printf("\n");
1552  }
1553  av_freep(&test_ctx.string);
1554  }
1555 
1556  printf("\nTesting av_opt_set_from_string()\n");
1557  {
1558  TestContext test_ctx = { 0 };
1559  const char *options[] = {
1560  "",
1561  "5",
1562  "5:hello",
1563  "5:hello:size=pal",
1564  "5:size=pal:hello",
1565  ":",
1566  "=",
1567  " 5 : hello : size = pal ",
1568  "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
1569  };
1570  const char *shorthand[] = { "num", "string", NULL };
1571 
1572  test_ctx.class = &test_class;
1573  av_opt_set_defaults(&test_ctx);
1574  test_ctx.string = av_strdup("default");
1575 
1577 
1578  for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
1579  av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
1580  if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0)
1581  av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);
1582  printf("\n");
1583  }
1584  av_freep(&test_ctx.string);
1585  }
1586 
1587  return 0;
1588 }
1589 
1590 #endif