]> git.sesse.net Git - ffmpeg/blob - libavutil/opt.h
mathops: remove undefined behaviour from sign_extend()
[ffmpeg] / libavutil / opt.h
1 /*
2  * AVOptions
3  * copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 key_val_sep a 0-terminated list of characters used to
197  * separate key from value
198  * @param pairs_sep a 0-terminated list of characters used to separate
199  * two pairs from each other
200  * @return the number of successfully set key/value pairs, or a negative
201  * value corresponding to an AVERROR code in case of error:
202  * AVERROR(EINVAL) if opts cannot be parsed,
203  * the error code issued by av_set_string3() if a key/value pair
204  * cannot be set
205  */
206 int av_set_options_string(void *ctx, const char *opts,
207                           const char *key_val_sep, const char *pairs_sep);
208
209 /**
210  * Free all string and binary options in obj.
211  */
212 void av_opt_free(void *obj);
213
214 /**
215  * Check whether a particular flag is set in a flags field.
216  *
217  * @param field_name the name of the flag field option
218  * @param flag_name the name of the flag to check
219  * @return non-zero if the flag is set, zero if the flag isn't set,
220  *         isn't of the right type, or the flags field doesn't exist.
221  */
222 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
223
224 /*
225  * Set all the options from a given dictionary on an object.
226  *
227  * @param obj a struct whose first element is a pointer to AVClass
228  * @param options options to process. This dictionary will be freed and replaced
229  *                by a new one containing all options not found in obj.
230  *                Of course this new dictionary needs to be freed by caller
231  *                with av_dict_free().
232  *
233  * @return 0 on success, a negative AVERROR if some option was found in obj,
234  *         but could not be set.
235  *
236  * @see av_dict_copy()
237  */
238 int av_opt_set_dict(void *obj, struct AVDictionary **options);
239
240 /**
241  * @defgroup opt_eval_funcs Evaluating option strings
242  * @{
243  * This group of functions can be used to evaluate option strings
244  * and get numbers out of them. They do the same thing as av_opt_set(),
245  * except the result is written into the caller-supplied pointer.
246  *
247  * @param obj a struct whose first element is a pointer to AVClass.
248  * @param o an option for which the string is to be evaluated.
249  * @param val string to be evaluated.
250  * @param *_out value of the string will be written here.
251  *
252  * @return 0 on success, a negative number on failure.
253  */
254 int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int        *flags_out);
255 int av_opt_eval_int   (void *obj, const AVOption *o, const char *val, int        *int_out);
256 int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t    *int64_out);
257 int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float      *float_out);
258 int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double     *double_out);
259 int av_opt_eval_q     (void *obj, const AVOption *o, const char *val, AVRational *q_out);
260 /**
261  * @}
262  */
263
264 #define AV_OPT_SEARCH_CHILDREN   0x0001 /**< Search in possible children of the
265                                              given object first. */
266 /**
267  *  The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
268  *  instead of a required pointer to a struct containing AVClass. This is
269  *  useful for searching for options without needing to allocate the corresponding
270  *  object.
271  */
272 #define AV_OPT_SEARCH_FAKE_OBJ   0x0002
273
274 /**
275  * Look for an option in an object. Consider only options which
276  * have all the specified flags set.
277  *
278  * @param[in] obj A pointer to a struct whose first element is a
279  *                pointer to an AVClass.
280  *                Alternatively a double pointer to an AVClass, if
281  *                AV_OPT_SEARCH_FAKE_OBJ search flag is set.
282  * @param[in] name The name of the option to look for.
283  * @param[in] unit When searching for named constants, name of the unit
284  *                 it belongs to.
285  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
286  * @param search_flags A combination of AV_OPT_SEARCH_*.
287  *
288  * @return A pointer to the option found, or NULL if no option
289  *         was found.
290  *
291  * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
292  * directly with av_set_string3(). Use special calls which take an options
293  * AVDictionary (e.g. avformat_open_input()) to set options found with this
294  * flag.
295  */
296 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
297                             int opt_flags, int search_flags);
298
299 /**
300  * Look for an option in an object. Consider only options which
301  * have all the specified flags set.
302  *
303  * @param[in] obj A pointer to a struct whose first element is a
304  *                pointer to an AVClass.
305  *                Alternatively a double pointer to an AVClass, if
306  *                AV_OPT_SEARCH_FAKE_OBJ search flag is set.
307  * @param[in] name The name of the option to look for.
308  * @param[in] unit When searching for named constants, name of the unit
309  *                 it belongs to.
310  * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
311  * @param search_flags A combination of AV_OPT_SEARCH_*.
312  * @param[out] target_obj if non-NULL, an object to which the option belongs will be
313  * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
314  * in search_flags. This parameter is ignored if search_flags contain
315  * AV_OPT_SEARCH_FAKE_OBJ.
316  *
317  * @return A pointer to the option found, or NULL if no option
318  *         was found.
319  */
320 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
321                              int opt_flags, int search_flags, void **target_obj);
322
323 /**
324  * Iterate over all AVOptions belonging to obj.
325  *
326  * @param obj an AVOptions-enabled struct or a double pointer to an
327  *            AVClass describing it.
328  * @param prev result of the previous call to av_opt_next() on this object
329  *             or NULL
330  * @return next AVOption or NULL
331  */
332 const AVOption *av_opt_next(void *obj, const AVOption *prev);
333
334 /**
335  * Iterate over AVOptions-enabled children of obj.
336  *
337  * @param prev result of a previous call to this function or NULL
338  * @return next AVOptions-enabled child or NULL
339  */
340 void *av_opt_child_next(void *obj, void *prev);
341
342 /**
343  * Iterate over potential AVOptions-enabled children of parent.
344  *
345  * @param prev result of a previous call to this function or NULL
346  * @return AVClass corresponding to next potential child or NULL
347  */
348 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev);
349
350 /**
351  * @defgroup opt_set_funcs Option setting functions
352  * @{
353  * Those functions set the field of obj with the given name to value.
354  *
355  * @param[in] obj A struct whose first element is a pointer to an AVClass.
356  * @param[in] name the name of the field to set
357  * @param[in] val The value to set. In case of av_opt_set() if the field is not
358  * of a string type, then the given string is parsed.
359  * SI postfixes and some named scalars are supported.
360  * If the field is of a numeric type, it has to be a numeric or named
361  * scalar. Behavior with more than one scalar and +- infix operators
362  * is undefined.
363  * If the field is of a flags type, it has to be a sequence of numeric
364  * scalars or named flags separated by '+' or '-'. Prefixing a flag
365  * with '+' causes it to be set without affecting the other flags;
366  * similarly, '-' unsets a flag.
367  * @param search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
368  * is passed here, then the option may be set on a child of obj.
369  *
370  * @return 0 if the value has been set, or an AVERROR code in case of
371  * error:
372  * AVERROR_OPTION_NOT_FOUND if no matching option exists
373  * AVERROR(ERANGE) if the value is out of range
374  * AVERROR(EINVAL) if the value is not valid
375  */
376 int av_opt_set       (void *obj, const char *name, const char *val, int search_flags);
377 int av_opt_set_int   (void *obj, const char *name, int64_t     val, int search_flags);
378 int av_opt_set_double(void *obj, const char *name, double      val, int search_flags);
379 int av_opt_set_q     (void *obj, const char *name, AVRational  val, int search_flags);
380 /**
381  * @}
382  */
383
384 /**
385  * @defgroup opt_get_funcs Option getting functions
386  * @{
387  * Those functions get a value of the option with the given name from an object.
388  *
389  * @param[in] obj a struct whose first element is a pointer to an AVClass.
390  * @param[in] name name of the option to get.
391  * @param[in] search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
392  * is passed here, then the option may be found in a child of obj.
393  * @param[out] out_val value of the option will be written here
394  * @return 0 on success, a negative error code otherwise
395  */
396 /**
397  * @note the returned string will av_malloc()ed and must be av_free()ed by the caller
398  */
399 int av_opt_get       (void *obj, const char *name, int search_flags, uint8_t   **out_val);
400 int av_opt_get_int   (void *obj, const char *name, int search_flags, int64_t    *out_val);
401 int av_opt_get_double(void *obj, const char *name, int search_flags, double     *out_val);
402 int av_opt_get_q     (void *obj, const char *name, int search_flags, AVRational *out_val);
403 /**
404  * @}
405  */
406
407 #endif /* AVUTIL_OPT_H */