AVOptions: add new API for enumerating children.
[libav.git] / libavutil / opt.h
CommitLineData
6ed04040
MN
1/*
2 * AVOptions
3 * copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
6ed04040 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
6ed04040
MN
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
6ed04040
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
6ed04040
MN
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"
7e83e1c5 32#include "dict.h"
641c7afe 33#include "log.h"
6ed04040
MN
34
35enum AVOptionType{
36 FF_OPT_TYPE_FLAGS,
37 FF_OPT_TYPE_INT,
38 FF_OPT_TYPE_INT64,
39 FF_OPT_TYPE_DOUBLE,
40 FF_OPT_TYPE_FLOAT,
41 FF_OPT_TYPE_STRING,
42 FF_OPT_TYPE_RATIONAL,
43 FF_OPT_TYPE_BINARY, ///< offset must point to a pointer immediately followed by an int for the length
44 FF_OPT_TYPE_CONST=128,
45};
46
47/**
48 * AVOption
49 */
50typedef struct AVOption {
51 const char *name;
52
53 /**
54 * short English help text
55 * @todo What about other languages?
56 */
57 const char *help;
58
59 /**
60 * The offset relative to the context structure where the option
61 * value is stored. It should be 0 for named constants.
62 */
63 int offset;
64 enum AVOptionType type;
65
66 /**
67 * the default value for scalar options
68 */
6ed04040
MN
69 union {
70 double dbl;
71 const char *str;
b6675279
AK
72 /* TODO those are unused now */
73 int64_t i64;
74 AVRational q;
6ed04040 75 } default_val;
6ed04040
MN
76 double min; ///< minimum valid value for the option
77 double max; ///< maximum valid value for the option
78
79 int flags;
6ed04040
MN
80#define AV_OPT_FLAG_ENCODING_PARAM 1 ///< a generic parameter which can be set by the user for muxing or encoding
81#define AV_OPT_FLAG_DECODING_PARAM 2 ///< a generic parameter which can be set by the user for demuxing or decoding
82#define AV_OPT_FLAG_METADATA 4 ///< some data extracted or inserted into the file like title, comment, ...
83#define AV_OPT_FLAG_AUDIO_PARAM 8
84#define AV_OPT_FLAG_VIDEO_PARAM 16
85#define AV_OPT_FLAG_SUBTITLE_PARAM 32
6ed04040
MN
86//FIXME think about enc-audio, ... style flags
87
88 /**
89 * The logical unit to which the option belongs. Non-constant
90 * options and corresponding named constants share the same
91 * unit. May be NULL.
92 */
93 const char *unit;
b6675279 94} AVOption;
6ed04040 95
dc59ec5e 96#if FF_API_FIND_OPT
6ed04040
MN
97/**
98 * Look for an option in obj. Look only for the options which
99 * have the flags set as specified in mask and flags (that is,
100 * for which it is the case that opt->flags & mask == flags).
101 *
102 * @param[in] obj a pointer to a struct whose first element is a
103 * pointer to an AVClass
104 * @param[in] name the name of the option to look for
105 * @param[in] unit the unit of the option to look for, or any if NULL
106 * @return a pointer to the option found, or NULL if no option
107 * has been found
dc59ec5e
AK
108 *
109 * @deprecated use av_opt_find.
6ed04040 110 */
dc59ec5e 111attribute_deprecated
6ed04040 112const AVOption *av_find_opt(void *obj, const char *name, const char *unit, int mask, int flags);
dc59ec5e 113#endif
6ed04040
MN
114
115/**
116 * Set the field of obj with the given name to value.
117 *
118 * @param[in] obj A struct whose first element is a pointer to an
119 * AVClass.
120 * @param[in] name the name of the field to set
121 * @param[in] val The value to set. If the field is not of a string
122 * type, then the given string is parsed.
123 * SI postfixes and some named scalars are supported.
124 * If the field is of a numeric type, it has to be a numeric or named
125 * scalar. Behavior with more than one scalar and +- infix operators
126 * is undefined.
127 * If the field is of a flags type, it has to be a sequence of numeric
128 * scalars or named flags separated by '+' or '-'. Prefixing a flag
129 * with '+' causes it to be set without affecting the other flags;
130 * similarly, '-' unsets a flag.
131 * @param[out] o_out if non-NULL put here a pointer to the AVOption
132 * found
c8d787d2 133 * @param alloc this parameter is currently ignored
6ed04040
MN
134 * @return 0 if the value has been set, or an AVERROR code in case of
135 * error:
e955a682 136 * AVERROR_OPTION_NOT_FOUND if no matching option exists
6ed04040
MN
137 * AVERROR(ERANGE) if the value is out of range
138 * AVERROR(EINVAL) if the value is not valid
139 */
140int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out);
141
142const AVOption *av_set_double(void *obj, const char *name, double n);
143const AVOption *av_set_q(void *obj, const char *name, AVRational n);
144const AVOption *av_set_int(void *obj, const char *name, int64_t n);
145double av_get_double(void *obj, const char *name, const AVOption **o_out);
146AVRational av_get_q(void *obj, const char *name, const AVOption **o_out);
147int64_t av_get_int(void *obj, const char *name, const AVOption **o_out);
148const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len);
149const AVOption *av_next_option(void *obj, const AVOption *last);
150
151/**
152 * Show the obj options.
153 *
154 * @param req_flags requested flags for the options to show. Show only the
155 * options for which it is opt->flags & req_flags.
156 * @param rej_flags rejected flags for the options to show. Show only the
157 * options for which it is !(opt->flags & req_flags).
158 * @param av_log_obj log context to use for showing the options
159 */
160int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags);
161
a7e2b2cc
AK
162/**
163 * Set the values of all AVOption fields to their default values.
164 *
165 * @param s an AVOption-enabled struct (its first member must be a pointer to AVClass)
166 */
6ed04040 167void av_opt_set_defaults(void *s);
79eff913
AK
168
169#if FF_API_OLD_AVOPTIONS
170attribute_deprecated
6ed04040 171void av_opt_set_defaults2(void *s, int mask, int flags);
79eff913 172#endif
6ed04040 173
c85eef4e
SS
174/**
175 * Parse the key/value pairs list in opts. For each key/value pair
176 * found, stores the value in the field in ctx that is named like the
177 * key. ctx must be an AVClass context, storing is done using
178 * AVOptions.
179 *
180 * @param key_val_sep a 0-terminated list of characters used to
181 * separate key from value
182 * @param pairs_sep a 0-terminated list of characters used to separate
183 * two pairs from each other
184 * @return the number of successfully set key/value pairs, or a negative
185 * value corresponding to an AVERROR code in case of error:
186 * AVERROR(EINVAL) if opts cannot be parsed,
187 * the error code issued by av_set_string3() if a key/value pair
188 * cannot be set
189 */
190int av_set_options_string(void *ctx, const char *opts,
191 const char *key_val_sep, const char *pairs_sep);
192
b39b0623
AK
193/**
194 * Free all string and binary options in obj.
195 */
196void av_opt_free(void *obj);
197
cb7c11cc
MS
198/**
199 * Check whether a particular flag is set in a flags field.
200 *
201 * @param field_name the name of the flag field option
202 * @param flag_name the name of the flag to check
203 * @return non-zero if the flag is set, zero if the flag isn't set,
204 * isn't of the right type, or the flags field doesn't exist.
205 */
206int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
207
7e83e1c5
AK
208/*
209 * Set all the options from a given dictionary on an object.
210 *
211 * @param obj a struct whose first element is a pointer to AVClass
212 * @param options options to process. This dictionary will be freed and replaced
213 * by a new one containing all options not found in obj.
214 * Of course this new dictionary needs to be freed by caller
215 * with av_dict_free().
216 *
217 * @return 0 on success, a negative AVERROR if some option was found in obj,
218 * but could not be set.
219 *
220 * @see av_dict_copy()
221 */
222int av_opt_set_dict(void *obj, struct AVDictionary **options);
223
dc59ec5e
AK
224#define AV_OPT_SEARCH_CHILDREN 0x0001 /**< Search in possible children of the
225 given object first. */
c11fb828
AK
226/**
227 * The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
228 * instead of a required pointer to a struct containing AVClass. This is
229 * useful for searching for options without needing to allocate the corresponding
230 * object.
231 */
232#define AV_OPT_SEARCH_FAKE_OBJ 0x0002
dc59ec5e
AK
233
234/**
235 * Look for an option in an object. Consider only options which
236 * have all the specified flags set.
237 *
238 * @param[in] obj A pointer to a struct whose first element is a
239 * pointer to an AVClass.
c11fb828
AK
240 * Alternatively a double pointer to an AVClass, if
241 * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
dc59ec5e
AK
242 * @param[in] name The name of the option to look for.
243 * @param[in] unit When searching for named constants, name of the unit
244 * it belongs to.
245 * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
246 * @param search_flags A combination of AV_OPT_SEARCH_*.
247 *
248 * @return A pointer to the option found, or NULL if no option
249 * was found.
250 *
251 * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
252 * directly with av_set_string3(). Use special calls which take an options
253 * AVDictionary (e.g. avformat_open_input()) to set options found with this
254 * flag.
255 */
256const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
257 int opt_flags, int search_flags);
258
641c7afe
AK
259/**
260 * Look for an option in an object. Consider only options which
261 * have all the specified flags set.
262 *
263 * @param[in] obj A pointer to a struct whose first element is a
264 * pointer to an AVClass.
265 * Alternatively a double pointer to an AVClass, if
266 * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
267 * @param[in] name The name of the option to look for.
268 * @param[in] unit When searching for named constants, name of the unit
269 * it belongs to.
270 * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
271 * @param search_flags A combination of AV_OPT_SEARCH_*.
272 * @param[out] target_obj if non-NULL, an object to which the option belongs will be
273 * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
274 * in search_flags. This parameter is ignored if search_flags contain
275 * AV_OPT_SEARCH_FAKE_OBJ.
276 *
277 * @return A pointer to the option found, or NULL if no option
278 * was found.
279 */
280const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
281 int opt_flags, int search_flags, void **target_obj);
282
283/**
284 * Iterate over AVOptions-enabled children of obj.
285 *
286 * @param prev result of a previous call to this function or NULL
287 * @return next AVOptions-enabled child or NULL
288 */
289void *av_opt_child_next(void *obj, void *prev);
290
291/**
292 * Iterate over potential AVOptions-enabled children of parent.
293 *
294 * @param prev result of a previous call to this function or NULL
295 * @return AVClass corresponding to next potential child or NULL
296 */
297const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev);
298
6ed04040 299#endif /* AVUTIL_OPT_H */