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