FFmpeg  5.0.1
opt.h
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 #ifndef AVUTIL_OPT_H
23 #define AVUTIL_OPT_H
24 
25 /**
26  * @file
27  * AVOptions
28  */
29 
30 #include "rational.h"
31 #include "avutil.h"
32 #include "dict.h"
33 #include "log.h"
34 #include "pixfmt.h"
35 #include "samplefmt.h"
36 
37 /**
38  * @defgroup avoptions AVOptions
39  * @ingroup lavu_data
40  * @{
41  * AVOptions provide a generic system to declare options on arbitrary structs
42  * ("objects"). An option can have a help text, a type and a range of possible
43  * values. Options may then be enumerated, read and written to.
44  *
45  * @section avoptions_implement Implementing AVOptions
46  * This section describes how to add AVOptions capabilities to a struct.
47  *
48  * All AVOptions-related information is stored in an AVClass. Therefore
49  * the first member of the struct should be a pointer to an AVClass describing it.
50  * The option field of the AVClass must be set to a NULL-terminated static array
51  * of AVOptions. Each AVOption must have a non-empty name, a type, a default
52  * value and for number-type AVOptions also a range of allowed values. It must
53  * also declare an offset in bytes from the start of the struct, where the field
54  * associated with this AVOption is located. Other fields in the AVOption struct
55  * should also be set when applicable, but are not required.
56  *
57  * The following example illustrates an AVOptions-enabled struct:
58  * @code
59  * typedef struct test_struct {
60  * const AVClass *class;
61  * int int_opt;
62  * char *str_opt;
63  * uint8_t *bin_opt;
64  * int bin_len;
65  * } test_struct;
66  *
67  * static const AVOption test_options[] = {
68  * { "test_int", "This is a test option of int type.", offsetof(test_struct, int_opt),
69  * AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX },
70  * { "test_str", "This is a test option of string type.", offsetof(test_struct, str_opt),
71  * AV_OPT_TYPE_STRING },
72  * { "test_bin", "This is a test option of binary type.", offsetof(test_struct, bin_opt),
73  * AV_OPT_TYPE_BINARY },
74  * { NULL },
75  * };
76  *
77  * static const AVClass test_class = {
78  * .class_name = "test class",
79  * .item_name = av_default_item_name,
80  * .option = test_options,
81  * .version = LIBAVUTIL_VERSION_INT,
82  * };
83  * @endcode
84  *
85  * Next, when allocating your struct, you must ensure that the AVClass pointer
86  * is set to the correct value. Then, av_opt_set_defaults() can be called to
87  * initialize defaults. After that the struct is ready to be used with the
88  * AVOptions API.
89  *
90  * When cleaning up, you may use the av_opt_free() function to automatically
91  * free all the allocated string and binary options.
92  *
93  * Continuing with the above example:
94  *
95  * @code
96  * test_struct *alloc_test_struct(void)
97  * {
98  * test_struct *ret = av_mallocz(sizeof(*ret));
99  * ret->class = &test_class;
100  * av_opt_set_defaults(ret);
101  * return ret;
102  * }
103  * void free_test_struct(test_struct **foo)
104  * {
105  * av_opt_free(*foo);
106  * av_freep(foo);
107  * }
108  * @endcode
109  *
110  * @subsection avoptions_implement_nesting Nesting
111  * It may happen that an AVOptions-enabled struct contains another
112  * AVOptions-enabled struct as a member (e.g. AVCodecContext in
113  * libavcodec exports generic options, while its priv_data field exports
114  * codec-specific options). In such a case, it is possible to set up the
115  * parent struct to export a child's options. To do that, simply
116  * implement AVClass.child_next() and AVClass.child_class_iterate() in the
117  * parent struct's AVClass.
118  * Assuming that the test_struct from above now also contains a
119  * child_struct field:
120  *
121  * @code
122  * typedef struct child_struct {
123  * AVClass *class;
124  * int flags_opt;
125  * } child_struct;
126  * static const AVOption child_opts[] = {
127  * { "test_flags", "This is a test option of flags type.",
128  * offsetof(child_struct, flags_opt), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX },
129  * { NULL },
130  * };
131  * static const AVClass child_class = {
132  * .class_name = "child class",
133  * .item_name = av_default_item_name,
134  * .option = child_opts,
135  * .version = LIBAVUTIL_VERSION_INT,
136  * };
137  *
138  * void *child_next(void *obj, void *prev)
139  * {
140  * test_struct *t = obj;
141  * if (!prev && t->child_struct)
142  * return t->child_struct;
143  * return NULL
144  * }
145  * const AVClass child_class_iterate(void **iter)
146  * {
147  * const AVClass *c = *iter ? NULL : &child_class;
148  * *iter = (void*)(uintptr_t)c;
149  * return c;
150  * }
151  * @endcode
152  * Putting child_next() and child_class_iterate() as defined above into
153  * test_class will now make child_struct's options accessible through
154  * test_struct (again, proper setup as described above needs to be done on
155  * child_struct right after it is created).
156  *
157  * From the above example it might not be clear why both child_next()
158  * and child_class_iterate() are needed. The distinction is that child_next()
159  * iterates over actually existing objects, while child_class_iterate()
160  * iterates over all possible child classes. E.g. if an AVCodecContext
161  * was initialized to use a codec which has private options, then its
162  * child_next() will return AVCodecContext.priv_data and finish
163  * iterating. OTOH child_class_iterate() on AVCodecContext.av_class will
164  * iterate over all available codecs with private options.
165  *
166  * @subsection avoptions_implement_named_constants Named constants
167  * It is possible to create named constants for options. Simply set the unit
168  * field of the option the constants should apply to a string and
169  * create the constants themselves as options of type AV_OPT_TYPE_CONST
170  * with their unit field set to the same string.
171  * Their default_val field should contain the value of the named
172  * constant.
173  * For example, to add some named constants for the test_flags option
174  * above, put the following into the child_opts array:
175  * @code
176  * { "test_flags", "This is a test option of flags type.",
177  * offsetof(child_struct, flags_opt), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, "test_unit" },
178  * { "flag1", "This is a flag with value 16", 0, AV_OPT_TYPE_CONST, { .i64 = 16 }, 0, 0, "test_unit" },
179  * @endcode
180  *
181  * @section avoptions_use Using AVOptions
182  * This section deals with accessing options in an AVOptions-enabled struct.
183  * Such structs in FFmpeg are e.g. AVCodecContext in libavcodec or
184  * AVFormatContext in libavformat.
185  *
186  * @subsection avoptions_use_examine Examining AVOptions
187  * The basic functions for examining options are av_opt_next(), which iterates
188  * over all options defined for one object, and av_opt_find(), which searches
189  * for an option with the given name.
190  *
191  * The situation is more complicated with nesting. An AVOptions-enabled struct
192  * may have AVOptions-enabled children. Passing the AV_OPT_SEARCH_CHILDREN flag
193  * to av_opt_find() will make the function search children recursively.
194  *
195  * For enumerating there are basically two cases. The first is when you want to
196  * get all options that may potentially exist on the struct and its children
197  * (e.g. when constructing documentation). In that case you should call
198  * av_opt_child_class_iterate() recursively on the parent struct's AVClass. The
199  * second case is when you have an already initialized struct with all its
200  * children and you want to get all options that can be actually written or read
201  * from it. In that case you should call av_opt_child_next() recursively (and
202  * av_opt_next() on each result).
203  *
204  * @subsection avoptions_use_get_set Reading and writing AVOptions
205  * When setting options, you often have a string read directly from the
206  * user. In such a case, simply passing it to av_opt_set() is enough. For
207  * non-string type options, av_opt_set() will parse the string according to the
208  * option type.
209  *
210  * Similarly av_opt_get() will read any option type and convert it to a string
211  * which will be returned. Do not forget that the string is allocated, so you
212  * have to free it with av_free().
213  *
214  * In some cases it may be more convenient to put all options into an
215  * AVDictionary and call av_opt_set_dict() on it. A specific case of this
216  * are the format/codec open functions in lavf/lavc which take a dictionary
217  * filled with option as a parameter. This makes it possible to set some options
218  * that cannot be set otherwise, since e.g. the input file format is not known
219  * before the file is actually opened.
220  */
221 
230  AV_OPT_TYPE_BINARY, ///< offset must point to a pointer immediately followed by an int for the length
234  AV_OPT_TYPE_IMAGE_SIZE, ///< offset must point to two consecutive integers
237  AV_OPT_TYPE_VIDEO_RATE, ///< offset must point to AVRational
242 };
243 
244 /**
245  * AVOption
246  */
247 typedef struct AVOption {
248  const char *name;
249 
250  /**
251  * short English help text
252  * @todo What about other languages?
253  */
254  const char *help;
255 
256  /**
257  * The offset relative to the context structure where the option
258  * value is stored. It should be 0 for named constants.
259  */
260  int offset;
261  enum AVOptionType type;
262 
263  /**
264  * the default value for scalar options
265  */
266  union {
267  int64_t i64;
268  double dbl;
269  const char *str;
270  /* TODO those are unused now */
273  double min; ///< minimum valid value for the option
274  double max; ///< maximum valid value for the option
275 
276  int flags;
277 #define AV_OPT_FLAG_ENCODING_PARAM 1 ///< a generic parameter which can be set by the user for muxing or encoding
278 #define AV_OPT_FLAG_DECODING_PARAM 2 ///< a generic parameter which can be set by the user for demuxing or decoding
279 #define AV_OPT_FLAG_AUDIO_PARAM 8
280 #define AV_OPT_FLAG_VIDEO_PARAM 16
281 #define AV_OPT_FLAG_SUBTITLE_PARAM 32
282 /**
283  * The option is intended for exporting values to the caller.
284  */
285 #define AV_OPT_FLAG_EXPORT 64
286 /**
287  * The option may not be set through the AVOptions API, only read.
288  * This flag only makes sense when AV_OPT_FLAG_EXPORT is also set.
289  */
290 #define AV_OPT_FLAG_READONLY 128
291 #define AV_OPT_FLAG_BSF_PARAM (1<<8) ///< a generic parameter which can be set by the user for bit stream filtering
292 #define AV_OPT_FLAG_RUNTIME_PARAM (1<<15) ///< a generic parameter which can be set by the user at runtime
293 #define AV_OPT_FLAG_FILTERING_PARAM (1<<16) ///< a generic parameter which can be set by the user for filtering
294 #define AV_OPT_FLAG_DEPRECATED (1<<17) ///< set if option is deprecated, users should refer to AVOption.help text for more information
295 #define AV_OPT_FLAG_CHILD_CONSTS (1<<18) ///< set if option constants can also reside in child objects
296 //FIXME think about enc-audio, ... style flags
297 
298  /**
299  * The logical unit to which the option belongs. Non-constant
300  * options and corresponding named constants share the same
301  * unit. May be NULL.
302  */
303  const char *unit;
304 } AVOption;
305 
306 /**
307  * A single allowed range of values, or a single allowed value.
308  */
309 typedef struct AVOptionRange {
310  const char *str;
311  /**
312  * Value range.
313  * For string ranges this represents the min/max length.
314  * For dimensions this represents the min/max pixel count or width/height in multi-component case.
315  */
317  /**
318  * Value's component range.
319  * For string this represents the unicode range for chars, 0-127 limits to ASCII.
320  */
322  /**
323  * Range flag.
324  * If set to 1 the struct encodes a range, if set to 0 a single value.
325  */
326  int is_range;
327 } AVOptionRange;
328 
329 /**
330  * List of AVOptionRange structs.
331  */
332 typedef struct AVOptionRanges {
333  /**
334  * Array of option ranges.
335  *
336  * Most of option types use just one component.
337  * Following describes multi-component option types:
338  *
339  * AV_OPT_TYPE_IMAGE_SIZE:
340  * component index 0: range of pixel count (width * height).
341  * component index 1: range of width.
342  * component index 2: range of height.
343  *
344  * @note To obtain multi-component version of this structure, user must
345  * provide AV_OPT_MULTI_COMPONENT_RANGE to av_opt_query_ranges or
346  * av_opt_query_ranges_default function.
347  *
348  * Multi-component range can be read as in following example:
349  *
350  * @code
351  * int range_index, component_index;
352  * AVOptionRanges *ranges;
353  * AVOptionRange *range[3]; //may require more than 3 in the future.
354  * av_opt_query_ranges(&ranges, obj, key, AV_OPT_MULTI_COMPONENT_RANGE);
355  * for (range_index = 0; range_index < ranges->nb_ranges; range_index++) {
356  * for (component_index = 0; component_index < ranges->nb_components; component_index++)
357  * range[component_index] = ranges->range[ranges->nb_ranges * component_index + range_index];
358  * //do something with range here.
359  * }
360  * av_opt_freep_ranges(&ranges);
361  * @endcode
362  */
364  /**
365  * Number of ranges per component.
366  */
368  /**
369  * Number of componentes.
370  */
373 
374 /**
375  * Show the obj options.
376  *
377  * @param req_flags requested flags for the options to show. Show only the
378  * options for which it is opt->flags & req_flags.
379  * @param rej_flags rejected flags for the options to show. Show only the
380  * options for which it is !(opt->flags & req_flags).
381  * @param av_log_obj log context to use for showing the options
382  */
383 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags);
384 
385 /**
386  * Set the values of all AVOption fields to their default values.
387  *
388  * @param s an AVOption-enabled struct (its first member must be a pointer to AVClass)
389  */
390 void av_opt_set_defaults(void *s);
391 
392 /**
393  * Set the values of all AVOption fields to their default values. Only these
394  * AVOption fields for which (opt->flags & mask) == flags will have their
395  * default applied to s.
396  *
397  * @param s an AVOption-enabled struct (its first member must be a pointer to AVClass)
398  * @param mask combination of AV_OPT_FLAG_*
399  * @param flags combination of AV_OPT_FLAG_*
400  */
401 void av_opt_set_defaults2(void *s, int mask, int flags);
402 
403 /**
404  * Parse the key/value pairs list in opts. For each key/value pair
405  * found, stores the value in the field in ctx that is named like the
406  * key. ctx must be an AVClass context, storing is done using
407  * AVOptions.
408  *
409  * @param opts options string to parse, may be NULL
410  * @param key_val_sep a 0-terminated list of characters used to
411  * separate key from value
412  * @param pairs_sep a 0-terminated list of characters used to separate
413  * two pairs from each other
414  * @return the number of successfully set key/value pairs, or a negative
415  * value corresponding to an AVERROR code in case of error:
416  * AVERROR(EINVAL) if opts cannot be parsed,
417  * the error code issued by av_opt_set() if a key/value pair
418  * cannot be set
419  */
420 int av_set_options_string(void *ctx, const char *opts,
421  const char *key_val_sep, const char *pairs_sep);
422 
423 /**
424  * Parse the key-value pairs list in opts. For each key=value pair found,
425  * set the value of the corresponding option in ctx.
426  *
427  * @param ctx the AVClass object to set options on
428  * @param opts the options string, key-value pairs separated by a
429  * delimiter
430  * @param shorthand a NULL-terminated array of options names for shorthand
431  * notation: if the first field in opts has no key part,
432  * the key is taken from the first element of shorthand;
433  * then again for the second, etc., until either opts is
434  * finished, shorthand is finished or a named option is
435  * found; after that, all options must be named
436  * @param key_val_sep a 0-terminated list of characters used to separate
437  * key from value, for example '='
438  * @param pairs_sep a 0-terminated list of characters used to separate
439  * two pairs from each other, for example ':' or ','
440  * @return the number of successfully set key=value pairs, or a negative
441  * value corresponding to an AVERROR code in case of error:
442  * AVERROR(EINVAL) if opts cannot be parsed,
443  * the error code issued by av_set_string3() if a key/value pair
444  * cannot be set
445  *
446  * Options names must use only the following characters: a-z A-Z 0-9 - . / _
447  * Separators must use characters distinct from option names and from each
448  * other.
449  */
450 int av_opt_set_from_string(void *ctx, const char *opts,
451  const char *const *shorthand,
452  const char *key_val_sep, const char *pairs_sep);
453 /**
454  * Free all allocated objects in obj.
455  */
456 void av_opt_free(void *obj);
457 
458 /**
459  * Check whether a particular flag is set in a flags field.
460  *
461  * @param field_name the name of the flag field option
462  * @param flag_name the name of the flag to check
463  * @return non-zero if the flag is set, zero if the flag isn't set,
464  * isn't of the right type, or the flags field doesn't exist.
465  */
466 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
467 
468 /**
469  * Set all the options from a given dictionary on an object.
470  *
471  * @param obj a struct whose first element is a pointer to AVClass
472  * @param options options to process. This dictionary will be freed and replaced
473  * by a new one containing all options not found in obj.
474  * Of course this new dictionary needs to be freed by caller
475  * with av_dict_free().
476  *
477  * @return 0 on success, a negative AVERROR if some option was found in obj,
478  * but could not be set.
479  *
480  * @see av_dict_copy()
481  */
482 int av_opt_set_dict(void *obj, struct AVDictionary **options);
483 
484 
485 /**
486  * Set all the options from a given dictionary on an object.
487  *
488  * @param obj a struct whose first element is a pointer to AVClass
489  * @param options options to process. This dictionary will be freed and replaced
490  * by a new one containing all options not found in obj.
491  * Of course this new dictionary needs to be freed by caller
492  * with av_dict_free().
493  * @param search_flags A combination of AV_OPT_SEARCH_*.
494  *
495  * @return 0 on success, a negative AVERROR if some option was found in obj,
496  * but could not be set.
497  *
498  * @see av_dict_copy()
499  */
500 int av_opt_set_dict2(void *obj, struct AVDictionary **options, int search_flags);
501 
502 /**
503  * Extract a key-value pair from the beginning of a string.
504  *
505  * @param ropts pointer to the options string, will be updated to
506  * point to the rest of the string (one of the pairs_sep
507  * or the final NUL)
508  * @param key_val_sep a 0-terminated list of characters used to separate
509  * key from value, for example '='
510  * @param pairs_sep a 0-terminated list of characters used to separate
511  * two pairs from each other, for example ':' or ','
512  * @param flags flags; see the AV_OPT_FLAG_* values below
513  * @param rkey parsed key; must be freed using av_free()
514  * @param rval parsed value; must be freed using av_free()
515  *
516  * @return >=0 for success, or a negative value corresponding to an
517  * AVERROR code in case of error; in particular:
518  * AVERROR(EINVAL) if no key is present
519  *
520  */
521 int av_opt_get_key_value(const char **ropts,
522  const char *key_val_sep, const char *pairs_sep,
523  unsigned flags,
524  char **rkey, char **rval);
525 
526 enum {
527 
528  /**
529  * Accept to parse a value without a key; the key will then be returned
530  * as NULL.
531  */
533 };
534 
535 /**
536  * @defgroup opt_eval_funcs Evaluating option strings
537  * @{
538  * This group of functions can be used to evaluate option strings
539  * and get numbers out of them. They do the same thing as av_opt_set(),
540  * except the result is written into the caller-supplied pointer.
541  *
542  * @param obj a struct whose first element is a pointer to AVClass.
543  * @param o an option for which the string is to be evaluated.
544  * @param val string to be evaluated.
545  * @param *_out value of the string will be written here.
546  *
547  * @return 0 on success, a negative number on failure.
548  */
549 int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int *flags_out);
550 int av_opt_eval_int (void *obj, const AVOption *o, const char *val, int *int_out);
551 int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t *int64_out);
552 int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float *float_out);
553 int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double *double_out);
554 int av_opt_eval_q (void *obj, const AVOption *o, const char *val, AVRational *q_out);
555 /**
556  * @}
557  */
558 
559 #define AV_OPT_SEARCH_CHILDREN (1 << 0) /**< Search in possible children of the
560  given object first. */
561 /**
562  * The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
563  * instead of a required pointer to a struct containing AVClass. This is
564  * useful for searching for options without needing to allocate the corresponding
565  * object.
566  */
567 #define AV_OPT_SEARCH_FAKE_OBJ (1 << 1)
568 
569 /**
570  * In av_opt_get, return NULL if the option has a pointer type and is set to NULL,
571  * rather than returning an empty string.
572  */
573 #define AV_OPT_ALLOW_NULL (1 << 2)
574 
575 /**
576  * Allows av_opt_query_ranges and av_opt_query_ranges_default to return more than
577  * one component for certain option types.
578  * @see AVOptionRanges for details.
579  */
580 #define AV_OPT_MULTI_COMPONENT_RANGE (1 << 12)
581 
582 /**
583  * Look for an option in an object. Consider only options which
584  * have all the specified flags set.
585  *
586  * @param[in] obj A pointer to a struct whose first element is a
587  * pointer to an AVClass.
588  * Alternatively a double pointer to an AVClass, if
589  * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
590  * @param[in] name The name of the option to look for.
591  * @param[in] unit When searching for named constants, name of the unit
592  * it belongs to.
593  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
594  * @param search_flags A combination of AV_OPT_SEARCH_*.
595  *
596  * @return A pointer to the option found, or NULL if no option
597  * was found.
598  *
599  * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
600  * directly with av_opt_set(). Use special calls which take an options
601  * AVDictionary (e.g. avformat_open_input()) to set options found with this
602  * flag.
603  */
604 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
605  int opt_flags, int search_flags);
606 
607 /**
608  * Look for an option in an object. Consider only options which
609  * have all the specified flags set.
610  *
611  * @param[in] obj A pointer to a struct whose first element is a
612  * pointer to an AVClass.
613  * Alternatively a double pointer to an AVClass, if
614  * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
615  * @param[in] name The name of the option to look for.
616  * @param[in] unit When searching for named constants, name of the unit
617  * it belongs to.
618  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
619  * @param search_flags A combination of AV_OPT_SEARCH_*.
620  * @param[out] target_obj if non-NULL, an object to which the option belongs will be
621  * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
622  * in search_flags. This parameter is ignored if search_flags contain
623  * AV_OPT_SEARCH_FAKE_OBJ.
624  *
625  * @return A pointer to the option found, or NULL if no option
626  * was found.
627  */
628 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
629  int opt_flags, int search_flags, void **target_obj);
630 
631 /**
632  * Iterate over all AVOptions belonging to obj.
633  *
634  * @param obj an AVOptions-enabled struct or a double pointer to an
635  * AVClass describing it.
636  * @param prev result of the previous call to av_opt_next() on this object
637  * or NULL
638  * @return next AVOption or NULL
639  */
640 const AVOption *av_opt_next(const void *obj, const AVOption *prev);
641 
642 /**
643  * Iterate over AVOptions-enabled children of obj.
644  *
645  * @param prev result of a previous call to this function or NULL
646  * @return next AVOptions-enabled child or NULL
647  */
648 void *av_opt_child_next(void *obj, void *prev);
649 
650 /**
651  * Iterate over potential AVOptions-enabled children of parent.
652  *
653  * @param iter a pointer where iteration state is stored.
654  * @return AVClass corresponding to next potential child or NULL
655  */
656 const AVClass *av_opt_child_class_iterate(const AVClass *parent, void **iter);
657 
658 /**
659  * @defgroup opt_set_funcs Option setting functions
660  * @{
661  * Those functions set the field of obj with the given name to value.
662  *
663  * @param[in] obj A struct whose first element is a pointer to an AVClass.
664  * @param[in] name the name of the field to set
665  * @param[in] val The value to set. In case of av_opt_set() if the field is not
666  * of a string type, then the given string is parsed.
667  * SI postfixes and some named scalars are supported.
668  * If the field is of a numeric type, it has to be a numeric or named
669  * scalar. Behavior with more than one scalar and +- infix operators
670  * is undefined.
671  * If the field is of a flags type, it has to be a sequence of numeric
672  * scalars or named flags separated by '+' or '-'. Prefixing a flag
673  * with '+' causes it to be set without affecting the other flags;
674  * similarly, '-' unsets a flag.
675  * If the field is of a dictionary type, it has to be a ':' separated list of
676  * key=value parameters. Values containing ':' special characters must be
677  * escaped.
678  * @param search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
679  * is passed here, then the option may be set on a child of obj.
680  *
681  * @return 0 if the value has been set, or an AVERROR code in case of
682  * error:
683  * AVERROR_OPTION_NOT_FOUND if no matching option exists
684  * AVERROR(ERANGE) if the value is out of range
685  * AVERROR(EINVAL) if the value is not valid
686  */
687 int av_opt_set (void *obj, const char *name, const char *val, int search_flags);
688 int av_opt_set_int (void *obj, const char *name, int64_t val, int search_flags);
689 int av_opt_set_double (void *obj, const char *name, double val, int search_flags);
690 int av_opt_set_q (void *obj, const char *name, AVRational val, int search_flags);
691 int av_opt_set_bin (void *obj, const char *name, const uint8_t *val, int size, int search_flags);
692 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags);
693 int av_opt_set_pixel_fmt (void *obj, const char *name, enum AVPixelFormat fmt, int search_flags);
694 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags);
695 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags);
696 int av_opt_set_channel_layout(void *obj, const char *name, int64_t ch_layout, int search_flags);
697 /**
698  * @note Any old dictionary present is discarded and replaced with a copy of the new one. The
699  * caller still owns val is and responsible for freeing it.
700  */
701 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags);
702 
703 /**
704  * Set a binary option to an integer list.
705  *
706  * @param obj AVClass object to set options on
707  * @param name name of the binary option
708  * @param val pointer to an integer list (must have the correct type with
709  * regard to the contents of the list)
710  * @param term list terminator (usually 0 or -1)
711  * @param flags search flags
712  */
713 #define av_opt_set_int_list(obj, name, val, term, flags) \
714  (av_int_list_length(val, term) > INT_MAX / sizeof(*(val)) ? \
715  AVERROR(EINVAL) : \
716  av_opt_set_bin(obj, name, (const uint8_t *)(val), \
717  av_int_list_length(val, term) * sizeof(*(val)), flags))
718 
719 /**
720  * @}
721  */
722 
723 /**
724  * @defgroup opt_get_funcs Option getting functions
725  * @{
726  * Those functions get a value of the option with the given name from an object.
727  *
728  * @param[in] obj a struct whose first element is a pointer to an AVClass.
729  * @param[in] name name of the option to get.
730  * @param[in] search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
731  * is passed here, then the option may be found in a child of obj.
732  * @param[out] out_val value of the option will be written here
733  * @return >=0 on success, a negative error code otherwise
734  */
735 /**
736  * @note the returned string will be av_malloc()ed and must be av_free()ed by the caller
737  *
738  * @note if AV_OPT_ALLOW_NULL is set in search_flags in av_opt_get, and the
739  * option is of type AV_OPT_TYPE_STRING, AV_OPT_TYPE_BINARY or AV_OPT_TYPE_DICT
740  * and is set to NULL, *out_val will be set to NULL instead of an allocated
741  * empty string.
742  */
743 int av_opt_get (void *obj, const char *name, int search_flags, uint8_t **out_val);
744 int av_opt_get_int (void *obj, const char *name, int search_flags, int64_t *out_val);
745 int av_opt_get_double (void *obj, const char *name, int search_flags, double *out_val);
746 int av_opt_get_q (void *obj, const char *name, int search_flags, AVRational *out_val);
747 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out);
748 int av_opt_get_pixel_fmt (void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt);
749 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt);
750 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val);
751 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *ch_layout);
752 /**
753  * @param[out] out_val The returned dictionary is a copy of the actual value and must
754  * be freed with av_dict_free() by the caller
755  */
756 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val);
757 /**
758  * @}
759  */
760 /**
761  * Gets a pointer to the requested field in a struct.
762  * This function allows accessing a struct even when its fields are moved or
763  * renamed since the application making the access has been compiled,
764  *
765  * @returns a pointer to the field, it can be cast to the correct type and read
766  * or written to.
767  */
768 void *av_opt_ptr(const AVClass *avclass, void *obj, const char *name);
769 
770 /**
771  * Free an AVOptionRanges struct and set it to NULL.
772  */
774 
775 /**
776  * Get a list of allowed ranges for the given option.
777  *
778  * The returned list may depend on other fields in obj like for example profile.
779  *
780  * @param flags is a bitmask of flags, undefined flags should not be set and should be ignored
781  * AV_OPT_SEARCH_FAKE_OBJ indicates that the obj is a double pointer to a AVClass instead of a full instance
782  * AV_OPT_MULTI_COMPONENT_RANGE indicates that function may return more than one component, @see AVOptionRanges
783  *
784  * The result must be freed with av_opt_freep_ranges.
785  *
786  * @return number of compontents returned on success, a negative errro code otherwise
787  */
788 int av_opt_query_ranges(AVOptionRanges **, void *obj, const char *key, int flags);
789 
790 /**
791  * Copy options from src object into dest object.
792  *
793  * The underlying AVClass of both src and dest must coincide. The guarantee
794  * below does not apply if this is not fulfilled.
795  *
796  * Options that require memory allocation (e.g. string or binary) are malloc'ed in dest object.
797  * Original memory allocated for such options is freed unless both src and dest options points to the same memory.
798  *
799  * Even on error it is guaranteed that allocated options from src and dest
800  * no longer alias each other afterwards; in particular calling av_opt_free()
801  * on both src and dest is safe afterwards if dest has been memdup'ed from src.
802  *
803  * @param dest Object to copy from
804  * @param src Object to copy into
805  * @return 0 on success, negative on error
806  */
807 int av_opt_copy(void *dest, const void *src);
808 
809 /**
810  * Get a default list of allowed ranges for the given option.
811  *
812  * This list is constructed without using the AVClass.query_ranges() callback
813  * and can be used as fallback from within the callback.
814  *
815  * @param flags is a bitmask of flags, undefined flags should not be set and should be ignored
816  * AV_OPT_SEARCH_FAKE_OBJ indicates that the obj is a double pointer to a AVClass instead of a full instance
817  * AV_OPT_MULTI_COMPONENT_RANGE indicates that function may return more than one component, @see AVOptionRanges
818  *
819  * The result must be freed with av_opt_free_ranges.
820  *
821  * @return number of compontents returned on success, a negative errro code otherwise
822  */
823 int av_opt_query_ranges_default(AVOptionRanges **, void *obj, const char *key, int flags);
824 
825 /**
826  * Check if given option is set to its default value.
827  *
828  * Options o must belong to the obj. This function must not be called to check child's options state.
829  * @see av_opt_is_set_to_default_by_name().
830  *
831  * @param obj AVClass object to check option on
832  * @param o option to be checked
833  * @return >0 when option is set to its default,
834  * 0 when option is not set its default,
835  * <0 on error
836  */
837 int av_opt_is_set_to_default(void *obj, const AVOption *o);
838 
839 /**
840  * Check if given option is set to its default value.
841  *
842  * @param obj AVClass object to check option on
843  * @param name option name
844  * @param search_flags combination of AV_OPT_SEARCH_*
845  * @return >0 when option is set to its default,
846  * 0 when option is not set its default,
847  * <0 on error
848  */
849 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags);
850 
851 
852 #define AV_OPT_SERIALIZE_SKIP_DEFAULTS 0x00000001 ///< Serialize options that are not set to default values only.
853 #define AV_OPT_SERIALIZE_OPT_FLAGS_EXACT 0x00000002 ///< Serialize options that exactly match opt_flags only.
854 
855 /**
856  * Serialize object's options.
857  *
858  * Create a string containing object's serialized options.
859  * Such string may be passed back to av_opt_set_from_string() in order to restore option values.
860  * A key/value or pairs separator occurring in the serialized value or
861  * name string are escaped through the av_escape() function.
862  *
863  * @param[in] obj AVClass object to serialize
864  * @param[in] opt_flags serialize options with all the specified flags set (AV_OPT_FLAG)
865  * @param[in] flags combination of AV_OPT_SERIALIZE_* flags
866  * @param[out] buffer Pointer to buffer that will be allocated with string containg serialized options.
867  * Buffer must be freed by the caller when is no longer needed.
868  * @param[in] key_val_sep character used to separate key from value
869  * @param[in] pairs_sep character used to separate two pairs from each other
870  * @return >= 0 on success, negative on error
871  * @warning Separators cannot be neither '\\' nor '\0'. They also cannot be the same.
872  */
873 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
874  const char key_val_sep, const char pairs_sep);
875 /**
876  * @}
877  */
878 
879 #endif /* AVUTIL_OPT_H */
Convenience header that includes libavutil's core.
Public dictionary API.
int av_opt_copy(void *dest, const void *src)
Copy options from src object into dest object.
int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
Check whether a particular flag is set in a flags field.
int av_opt_set_dict2(void *obj, struct AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
int av_opt_get_key_value(const char **ropts, const char *key_val_sep, const char *pairs_sep, unsigned flags, char **rkey, char **rval)
Extract a key-value pair from the beginning of a string.
int av_opt_is_set_to_default(void *obj, const AVOption *o)
Check if given option is set to its default value.
const AVOption * av_opt_find2(void *obj, const char *name, const char *unit, int opt_flags, int search_flags, void **target_obj)
Look for an option in an object.
int av_opt_query_ranges_default(AVOptionRanges **, void *obj, const char *key, int flags)
Get a default list of allowed ranges for the given option.
int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer, const char key_val_sep, const char pairs_sep)
Serialize object's options.
void av_opt_freep_ranges(AVOptionRanges **ranges)
Free an AVOptionRanges struct and set it to NULL.
int av_opt_query_ranges(AVOptionRanges **, void *obj, const char *key, int flags)
Get a list of allowed ranges for the given option.
int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
Check if given option is set to its default value.
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
void * av_opt_child_next(void *obj, void *prev)
Iterate over AVOptions-enabled children of obj.
void * av_opt_ptr(const AVClass *avclass, void *obj, const char *name)
Gets a pointer to the requested field in a struct.
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
const AVOption * av_opt_next(const void *obj, const AVOption *prev)
Iterate over all AVOptions belonging to obj.
AVOptionType
Definition: opt.h:222
void av_opt_free(void *obj)
Free all allocated objects in obj.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
const AVClass * av_opt_child_class_iterate(const AVClass *parent, void **iter)
Iterate over potential AVOptions-enabled children of parent.
void av_opt_set_defaults2(void *s, int mask, int flags)
Set the values of all AVOption fields to their default values.
int av_opt_set_dict(void *obj, struct AVDictionary **options)
Set all the options from a given dictionary on an object.
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
Show the obj options.
@ AV_OPT_FLAG_IMPLICIT_KEY
Accept to parse a value without a key; the key will then be returned as NULL.
Definition: opt.h:532
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:234
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
@ AV_OPT_TYPE_PIXEL_FMT
Definition: opt.h:235
@ AV_OPT_TYPE_BINARY
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:230
@ AV_OPT_TYPE_DURATION
Definition: opt.h:238
@ AV_OPT_TYPE_CHANNEL_LAYOUT
Definition: opt.h:240
@ AV_OPT_TYPE_SAMPLE_FMT
Definition: opt.h:236
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:229
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:223
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:237
@ AV_OPT_TYPE_INT64
Definition: opt.h:225
@ AV_OPT_TYPE_UINT64
Definition: opt.h:232
@ AV_OPT_TYPE_INT
Definition: opt.h:224
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
@ AV_OPT_TYPE_DICT
Definition: opt.h:231
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
@ AV_OPT_TYPE_COLOR
Definition: opt.h:239
struct AVDictionary AVDictionary
Definition: dict.h:84
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int av_opt_eval_float(void *obj, const AVOption *o, const char *val, float *float_out)
int av_opt_eval_int(void *obj, const AVOption *o, const char *val, int *int_out)
int av_opt_eval_q(void *obj, const AVOption *o, const char *val, AVRational *q_out)
int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double *double_out)
int av_opt_eval_int64(void *obj, const AVOption *o, const char *val, int64_t *int64_out)
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *ch_layout)
int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int size, int search_flags)
int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
int av_opt_set_channel_layout(void *obj, const char *name, int64_t ch_layout, int search_flags)
pixel format definitions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
Utilties for rational number calculation.
Describe the class of an AVClass context structure.
Definition: log.h:66
A single allowed range of values, or a single allowed value.
Definition: opt.h:309
double component_max
Definition: opt.h:321
double component_min
Value's component range.
Definition: opt.h:321
const char * str
Definition: opt.h:310
double value_max
Definition: opt.h:316
double value_min
Value range.
Definition: opt.h:316
int is_range
Range flag.
Definition: opt.h:326
List of AVOptionRange structs.
Definition: opt.h:332
int nb_ranges
Number of ranges per component.
Definition: opt.h:367
AVOptionRange ** range
Array of option ranges.
Definition: opt.h:363
int nb_components
Number of componentes.
Definition: opt.h:371
AVOption.
Definition: opt.h:247
double max
maximum valid value for the option
Definition: opt.h:274
int offset
The offset relative to the context structure where the option value is stored.
Definition: opt.h:260
const char * unit
The logical unit to which the option belongs.
Definition: opt.h:303
AVRational q
Definition: opt.h:271
const char * str
Definition: opt.h:269
const char * help
short English help text
Definition: opt.h:254
const char * name
Definition: opt.h:248
int64_t i64
Definition: opt.h:267
double min
minimum valid value for the option
Definition: opt.h:273
int flags
Definition: opt.h:276
union AVOption::@10 default_val
the default value for scalar options
enum AVOptionType type
Definition: opt.h:261
double dbl
Definition: opt.h:268
Rational number (pair of numerator and denominator).
Definition: rational.h:58