]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / opt.h
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
35 enum AVOptionType{
36     AV_OPT_TYPE_FLAGS,
37     AV_OPT_TYPE_INT,
38     AV_OPT_TYPE_INT64,
39     AV_OPT_TYPE_DOUBLE,
40     AV_OPT_TYPE_FLOAT,
41     AV_OPT_TYPE_STRING,
42     AV_OPT_TYPE_RATIONAL,
43     AV_OPT_TYPE_BINARY,  ///< offset must point to a pointer immediately followed by an int for the length
44     AV_OPT_TYPE_CONST = 128,
45 #if FF_API_OLD_AVOPTIONS
46     FF_OPT_TYPE_FLAGS = 0,
47     FF_OPT_TYPE_INT,
48     FF_OPT_TYPE_INT64,
49     FF_OPT_TYPE_DOUBLE,
50     FF_OPT_TYPE_FLOAT,
51     FF_OPT_TYPE_STRING,
52     FF_OPT_TYPE_RATIONAL,
53     FF_OPT_TYPE_BINARY,  ///< offset must point to a pointer immediately followed by an int for the length
54     FF_OPT_TYPE_CONST=128,
55 #endif
56 };
57
58 /**
59  * AVOption
60  */
61 typedef struct AVOption {
62     const char *name;
63
64     /**
65      * short English help text
66      * @todo What about other languages?
67      */
68     const char *help;
69
70     /**
71      * The offset relative to the context structure where the option
72      * value is stored. It should be 0 for named constants.
73      */
74     int offset;
75     enum AVOptionType type;
76
77     /**
78      * the default value for scalar options
79      */
80     union {
81         double dbl;
82         const char *str;
83         /* TODO those are unused now */
84         int64_t i64;
85         AVRational q;
86     } default_val;
87     double min;                 ///< minimum valid value for the option
88     double max;                 ///< maximum valid value for the option
89
90     int flags;
91 #define AV_OPT_FLAG_ENCODING_PARAM  1   ///< a generic parameter which can be set by the user for muxing or encoding
92 #define AV_OPT_FLAG_DECODING_PARAM  2   ///< a generic parameter which can be set by the user for demuxing or decoding
93 #define AV_OPT_FLAG_METADATA        4   ///< some data extracted or inserted into the file like title, comment, ...
94 #define AV_OPT_FLAG_AUDIO_PARAM     8
95 #define AV_OPT_FLAG_VIDEO_PARAM     16
96 #define AV_OPT_FLAG_SUBTITLE_PARAM  32
97 //FIXME think about enc-audio, ... style flags
98
99     /**
100      * The logical unit to which the option belongs. Non-constant
101      * options and corresponding named constants share the same
102      * unit. May be NULL.
103      */
104     const char *unit;
105 } AVOption;
106
107 #if FF_API_FIND_OPT
108 /**
109  * Look for an option in obj. Look only for the options which
110  * have the flags set as specified in mask and flags (that is,
111  * for which it is the case that opt->flags & mask == flags).
112  *
113  * @param[in] obj a pointer to a struct whose first element is a
114  * pointer to an AVClass
115  * @param[in] name the name of the option to look for
116  * @param[in] unit the unit of the option to look for, or any if NULL
117  * @return a pointer to the option found, or NULL if no option
118  * has been found
119  *
120  * @deprecated use av_opt_find.
121  */
122 attribute_deprecated
123 const AVOption *av_find_opt(void *obj, const char *name, const char *unit, int mask, int flags);
124 #endif
125
126 #if FF_API_OLD_AVOPTIONS
127 /**
128  * Set the field of obj with the given name to value.
129  *
130  * @param[in] obj A struct whose first element is a pointer to an
131  * AVClass.
132  * @param[in] name the name of the field to set
133  * @param[in] val The value to set. If the field is not of a string
134  * type, then the given string is parsed.
135  * SI postfixes and some named scalars are supported.
136  * If the field is of a numeric type, it has to be a numeric or named
137  * scalar. Behavior with more than one scalar and +- infix operators
138  * is undefined.
139  * If the field is of a flags type, it has to be a sequence of numeric
140  * scalars or named flags separated by '+' or '-'. Prefixing a flag
141  * with '+' causes it to be set without affecting the other flags;
142  * similarly, '-' unsets a flag.
143  * @param[out] o_out if non-NULL put here a pointer to the AVOption
144  * found
145  * @param alloc this parameter is currently ignored
146  * @return 0 if the value has been set, or an AVERROR code in case of
147  * error:
148  * AVERROR_OPTION_NOT_FOUND if no matching option exists
149  * AVERROR(ERANGE) if the value is out of range
150  * AVERROR(EINVAL) if the value is not valid
151  * @deprecated use av_opt_set()
152  */
153 attribute_deprecated
154 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out);
155
156 attribute_deprecated const AVOption *av_set_double(void *obj, const char *name, double n);
157 attribute_deprecated const AVOption *av_set_q(void *obj, const char *name, AVRational n);
158 attribute_deprecated const AVOption *av_set_int(void *obj, const char *name, int64_t n);
159
160 attribute_deprecated double av_get_double(void *obj, const char *name, const AVOption **o_out);
161 attribute_deprecated AVRational av_get_q(void *obj, const char *name, const AVOption **o_out);
162 attribute_deprecated int64_t av_get_int(void *obj, const char *name, const AVOption **o_out);
163 attribute_deprecated const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len);
164 attribute_deprecated const AVOption *av_next_option(void *obj, const AVOption *last);
165 #endif
166
167 /**
168  * Show the obj options.
169  *
170  * @param req_flags requested flags for the options to show. Show only the
171  * options for which it is opt->flags & req_flags.
172  * @param rej_flags rejected flags for the options to show. Show only the
173  * options for which it is !(opt->flags & req_flags).
174  * @param av_log_obj log context to use for showing the options
175  */
176 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags);
177
178 /**
179  * Set the values of all AVOption fields to their default values.
180  *
181  * @param s an AVOption-enabled struct (its first member must be a pointer to AVClass)
182  */
183 void av_opt_set_defaults(void *s);
184
185 #if FF_API_OLD_AVOPTIONS
186 attribute_deprecated
187 void av_opt_set_defaults2(void *s, int mask, int flags);
188 #endif
189
190 /**
191  * Parse the key/value pairs list in opts. For each key/value pair
192  * found, stores the value in the field in ctx that is named like the
193  * key. ctx must be an AVClass context, storing is done using
194  * AVOptions.
195  *
196  * @param opts options string to parse, may be NULL
197  * @param key_val_sep a 0-terminated list of characters used to
198  * separate key from value
199  * @param pairs_sep a 0-terminated list of characters used to separate
200  * two pairs from each other
201  * @return the number of successfully set key/value pairs, or a negative
202  * value corresponding to an AVERROR code in case of error:
203  * AVERROR(EINVAL) if opts cannot be parsed,
204  * the error code issued by av_set_string3() if a key/value pair
205  * cannot be set
206  */
207 int av_set_options_string(void *ctx, const char *opts,
208                           const char *key_val_sep, const char *pairs_sep);
209
210 /**
211  * Free all string and binary options in obj.
212  */
213 void av_opt_free(void *obj);
214
215 /**
216  * Check whether a particular flag is set in a flags field.
217  *
218  * @param field_name the name of the flag field option
219  * @param flag_name the name of the flag to check
220  * @return non-zero if the flag is set, zero if the flag isn't set,
221  *         isn't of the right type, or the flags field doesn't exist.
222  */
223 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
224
225 /*
226  * Set all the options from a given dictionary on an object.
227  *
228  * @param obj a struct whose first element is a pointer to AVClass
229  * @param options options to process. This dictionary will be freed and replaced
230  *                by a new one containing all options not found in obj.
231  *                Of course this new dictionary needs to be freed by caller
232  *                with av_dict_free().
233  *
234  * @return 0 on success, a negative AVERROR if some option was found in obj,
235  *         but could not be set.
236  *
237  * @see av_dict_copy()
238  */
239 int av_opt_set_dict(void *obj, struct AVDictionary **options);
240
241 /**
242  * @defgroup opt_eval_funcs Evaluating option strings
243  * @{
244  * This group of functions can be used to evaluate option strings
245  * and get numbers out of them. They do the same thing as av_opt_set(),
246  * except the result is written into the caller-supplied pointer.
247  *
248  * @param obj a struct whose first element is a pointer to AVClass.
249  * @param o an option for which the string is to be evaluated.
250  * @param val string to be evaluated.
251  * @param *_out value of the string will be written here.
252  *
253  * @return 0 on success, a negative number on failure.
254  */
255 int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int        *flags_out);
256 int av_opt_eval_int   (void *obj, const AVOption *o, const char *val, int        *int_out);
257 int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t    *int64_out);
258 int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float      *float_out);
259 int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double     *double_out);
260 int av_opt_eval_q     (void *obj, const AVOption *o, const char *val, AVRational *q_out);
261 /**
262  * @}
263  */
264
265 #define AV_OPT_SEARCH_CHILDREN   0x0001 /**< Search in possible children of the
266                                              given object first. */
267 /**
268  *  The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
269  *  instead of a required pointer to a struct containing AVClass. This is
270  *  useful for searching for options without needing to allocate the corresponding
271  *  object.
272  */
273 #define AV_OPT_SEARCH_FAKE_OBJ   0x0002
274
275 /**
276  * Look for an option in an object. Consider only options which
277  * have all the specified flags set.
278  *
279  * @param[in] obj A pointer to a struct whose first element is a
280  *                pointer to an AVClass.
281  *                Alternatively a double pointer to an AVClass, if
282  *                AV_OPT_SEARCH_FAKE_OBJ search flag is set.
283  * @param[in] name The name of the option to look for.
284  * @param[in] unit When searching for named constants, name of the unit
285  *                 it belongs to.
286  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
287  * @param search_flags A combination of AV_OPT_SEARCH_*.
288  *
289  * @return A pointer to the option found, or NULL if no option
290  *         was found.
291  *
292  * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
293  * directly with av_set_string3(). Use special calls which take an options
294  * AVDictionary (e.g. avformat_open_input()) to set options found with this
295  * flag.
296  */
297 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
298                             int opt_flags, int search_flags);
299
300 /**
301  * Look for an option in an object. Consider only options which
302  * have all the specified flags set.
303  *
304  * @param[in] obj A pointer to a struct whose first element is a
305  *                pointer to an AVClass.
306  *                Alternatively a double pointer to an AVClass, if
307  *                AV_OPT_SEARCH_FAKE_OBJ search flag is set.
308  * @param[in] name The name of the option to look for.
309  * @param[in] unit When searching for named constants, name of the unit
310  *                 it belongs to.
311  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
312  * @param search_flags A combination of AV_OPT_SEARCH_*.
313  * @param[out] target_obj if non-NULL, an object to which the option belongs will be
314  * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
315  * in search_flags. This parameter is ignored if search_flags contain
316  * AV_OPT_SEARCH_FAKE_OBJ.
317  *
318  * @return A pointer to the option found, or NULL if no option
319  *         was found.
320  */
321 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
322                              int opt_flags, int search_flags, void **target_obj);
323
324 /**
325  * Iterate over all AVOptions belonging to obj.
326  *
327  * @param obj an AVOptions-enabled struct or a double pointer to an
328  *            AVClass describing it.
329  * @param prev result of the previous call to av_opt_next() on this object
330  *             or NULL
331  * @return next AVOption or NULL
332  */
333 const AVOption *av_opt_next(void *obj, const AVOption *prev);
334
335 /**
336  * Iterate over AVOptions-enabled children of obj.
337  *
338  * @param prev result of a previous call to this function or NULL
339  * @return next AVOptions-enabled child or NULL
340  */
341 void *av_opt_child_next(void *obj, void *prev);
342
343 /**
344  * Iterate over potential AVOptions-enabled children of parent.
345  *
346  * @param prev result of a previous call to this function or NULL
347  * @return AVClass corresponding to next potential child or NULL
348  */
349 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev);
350
351 /**
352  * @defgroup opt_set_funcs Option setting functions
353  * @{
354  * Those functions set the field of obj with the given name to value.
355  *
356  * @param[in] obj A struct whose first element is a pointer to an AVClass.
357  * @param[in] name the name of the field to set
358  * @param[in] val The value to set. In case of av_opt_set() if the field is not
359  * of a string type, then the given string is parsed.
360  * SI postfixes and some named scalars are supported.
361  * If the field is of a numeric type, it has to be a numeric or named
362  * scalar. Behavior with more than one scalar and +- infix operators
363  * is undefined.
364  * If the field is of a flags type, it has to be a sequence of numeric
365  * scalars or named flags separated by '+' or '-'. Prefixing a flag
366  * with '+' causes it to be set without affecting the other flags;
367  * similarly, '-' unsets a flag.
368  * @param search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
369  * is passed here, then the option may be set on a child of obj.
370  *
371  * @return 0 if the value has been set, or an AVERROR code in case of
372  * error:
373  * AVERROR_OPTION_NOT_FOUND if no matching option exists
374  * AVERROR(ERANGE) if the value is out of range
375  * AVERROR(EINVAL) if the value is not valid
376  */
377 int av_opt_set       (void *obj, const char *name, const char *val, int search_flags);
378 int av_opt_set_int   (void *obj, const char *name, int64_t     val, int search_flags);
379 int av_opt_set_double(void *obj, const char *name, double      val, int search_flags);
380 int av_opt_set_q     (void *obj, const char *name, AVRational  val, int search_flags);
381 /**
382  * @}
383  */
384
385 /**
386  * @defgroup opt_get_funcs Option getting functions
387  * @{
388  * Those functions get a value of the option with the given name from an object.
389  *
390  * @param[in] obj a struct whose first element is a pointer to an AVClass.
391  * @param[in] name name of the option to get.
392  * @param[in] search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
393  * is passed here, then the option may be found in a child of obj.
394  * @param[out] out_val value of the option will be written here
395  * @return 0 on success, a negative error code otherwise
396  */
397 /**
398  * @note the returned string will av_malloc()ed and must be av_free()ed by the caller
399  */
400 int av_opt_get       (void *obj, const char *name, int search_flags, uint8_t   **out_val);
401 int av_opt_get_int   (void *obj, const char *name, int search_flags, int64_t    *out_val);
402 int av_opt_get_double(void *obj, const char *name, int search_flags, double     *out_val);
403 int av_opt_get_q     (void *obj, const char *name, int search_flags, AVRational *out_val);
404 /**
405  * @}
406  */
407
408 #endif /* AVUTIL_OPT_H */