opt.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. #ifndef AVUTIL_OPT_H
  22. #define AVUTIL_OPT_H
  23. /**
  24. * @file
  25. * AVOptions
  26. */
  27. #include "rational.h"
  28. #include "avutil.h"
  29. #include "dict.h"
  30. #include "log.h"
  31. /**
  32. * @defgroup avoptions AVOptions
  33. * @ingroup lavu_data
  34. * @{
  35. * AVOptions provide a generic system to declare options on arbitrary structs
  36. * ("objects"). An option can have a help text, a type and a range of possible
  37. * values. Options may then be enumerated, read and written to.
  38. *
  39. * @section avoptions_implement Implementing AVOptions
  40. * This section describes how to add AVOptions capabilities to a struct.
  41. *
  42. * All AVOptions-related information is stored in an AVClass. Therefore
  43. * the first member of the struct must be a pointer to an AVClass describing it.
  44. * The option field of the AVClass must be set to a NULL-terminated static array
  45. * of AVOptions. Each AVOption must have a non-empty name, a type, a default
  46. * value and for number-type AVOptions also a range of allowed values. It must
  47. * also declare an offset in bytes from the start of the struct, where the field
  48. * associated with this AVOption is located. Other fields in the AVOption struct
  49. * should also be set when applicable, but are not required.
  50. *
  51. * The following example illustrates an AVOptions-enabled struct:
  52. * @code
  53. * typedef struct test_struct {
  54. * AVClass *class;
  55. * int int_opt;
  56. * char *str_opt;
  57. * uint8_t *bin_opt;
  58. * int bin_len;
  59. * } test_struct;
  60. *
  61. * static const AVOption test_options[] = {
  62. * { "test_int", "This is a test option of int type.", offsetof(test_struct, int_opt),
  63. * AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX },
  64. * { "test_str", "This is a test option of string type.", offsetof(test_struct, str_opt),
  65. * AV_OPT_TYPE_STRING },
  66. * { "test_bin", "This is a test option of binary type.", offsetof(test_struct, bin_opt),
  67. * AV_OPT_TYPE_BINARY },
  68. * { NULL },
  69. * };
  70. *
  71. * static const AVClass test_class = {
  72. * .class_name = "test class",
  73. * .item_name = av_default_item_name,
  74. * .option = test_options,
  75. * .version = LIBAVUTIL_VERSION_INT,
  76. * };
  77. * @endcode
  78. *
  79. * Next, when allocating your struct, you must ensure that the AVClass pointer
  80. * is set to the correct value. Then, av_opt_set_defaults() must be called to
  81. * initialize defaults. After that the struct is ready to be used with the
  82. * AVOptions API.
  83. *
  84. * When cleaning up, you may use the av_opt_free() function to automatically
  85. * free all the allocated string and binary options.
  86. *
  87. * Continuing with the above example:
  88. *
  89. * @code
  90. * test_struct *alloc_test_struct(void)
  91. * {
  92. * test_struct *ret = av_malloc(sizeof(*ret));
  93. * ret->class = &test_class;
  94. * av_opt_set_defaults(ret);
  95. * return ret;
  96. * }
  97. * void free_test_struct(test_struct **foo)
  98. * {
  99. * av_opt_free(*foo);
  100. * av_freep(foo);
  101. * }
  102. * @endcode
  103. *
  104. * @subsection avoptions_implement_nesting Nesting
  105. * It may happen that an AVOptions-enabled struct contains another
  106. * AVOptions-enabled struct as a member (e.g. AVCodecContext in
  107. * libavcodec exports generic options, while its priv_data field exports
  108. * codec-specific options). In such a case, it is possible to set up the
  109. * parent struct to export a child's options. To do that, simply
  110. * implement AVClass.child_next() and AVClass.child_class_next() in the
  111. * parent struct's AVClass.
  112. * Assuming that the test_struct from above now also contains a
  113. * child_struct field:
  114. *
  115. * @code
  116. * typedef struct child_struct {
  117. * AVClass *class;
  118. * int flags_opt;
  119. * } child_struct;
  120. * static const AVOption child_opts[] = {
  121. * { "test_flags", "This is a test option of flags type.",
  122. * offsetof(child_struct, flags_opt), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX },
  123. * { NULL },
  124. * };
  125. * static const AVClass child_class = {
  126. * .class_name = "child class",
  127. * .item_name = av_default_item_name,
  128. * .option = child_opts,
  129. * .version = LIBAVUTIL_VERSION_INT,
  130. * };
  131. *
  132. * void *child_next(void *obj, void *prev)
  133. * {
  134. * test_struct *t = obj;
  135. * if (!prev && t->child_struct)
  136. * return t->child_struct;
  137. * return NULL
  138. * }
  139. * const AVClass child_class_next(const AVClass *prev)
  140. * {
  141. * return prev ? NULL : &child_class;
  142. * }
  143. * @endcode
  144. * Putting child_next() and child_class_next() as defined above into
  145. * test_class will now make child_struct's options accessible through
  146. * test_struct (again, proper setup as described above needs to be done on
  147. * child_struct right after it is created).
  148. *
  149. * From the above example it might not be clear why both child_next()
  150. * and child_class_next() are needed. The distinction is that child_next()
  151. * iterates over actually existing objects, while child_class_next()
  152. * iterates over all possible child classes. E.g. if an AVCodecContext
  153. * was initialized to use a codec which has private options, then its
  154. * child_next() will return AVCodecContext.priv_data and finish
  155. * iterating. OTOH child_class_next() on AVCodecContext.av_class will
  156. * iterate over all available codecs with private options.
  157. *
  158. * @subsection avoptions_implement_named_constants Named constants
  159. * It is possible to create named constants for options. Simply set the unit
  160. * field of the option the constants should apply to to a string and
  161. * create the constants themselves as options of type AV_OPT_TYPE_CONST
  162. * with their unit field set to the same string.
  163. * Their default_val field should contain the value of the named
  164. * constant.
  165. * For example, to add some named constants for the test_flags option
  166. * above, put the following into the child_opts array:
  167. * @code
  168. * { "test_flags", "This is a test option of flags type.",
  169. * offsetof(child_struct, flags_opt), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, "test_unit" },
  170. * { "flag1", "This is a flag with value 16", 0, AV_OPT_TYPE_CONST, { .i64 = 16 }, 0, 0, "test_unit" },
  171. * @endcode
  172. *
  173. * @section avoptions_use Using AVOptions
  174. * This section deals with accessing options in an AVOptions-enabled struct.
  175. * Such structs in Libav are e.g. AVCodecContext in libavcodec or
  176. * AVFormatContext in libavformat.
  177. *
  178. * @subsection avoptions_use_examine Examining AVOptions
  179. * The basic functions for examining options are av_opt_next(), which iterates
  180. * over all options defined for one object, and av_opt_find(), which searches
  181. * for an option with the given name.
  182. *
  183. * The situation is more complicated with nesting. An AVOptions-enabled struct
  184. * may have AVOptions-enabled children. Passing the AV_OPT_SEARCH_CHILDREN flag
  185. * to av_opt_find() will make the function search children recursively.
  186. *
  187. * For enumerating there are basically two cases. The first is when you want to
  188. * get all options that may potentially exist on the struct and its children
  189. * (e.g. when constructing documentation). In that case you should call
  190. * av_opt_child_class_next() recursively on the parent struct's AVClass. The
  191. * second case is when you have an already initialized struct with all its
  192. * children and you want to get all options that can be actually written or read
  193. * from it. In that case you should call av_opt_child_next() recursively (and
  194. * av_opt_next() on each result).
  195. *
  196. * @subsection avoptions_use_get_set Reading and writing AVOptions
  197. * When setting options, you often have a string read directly from the
  198. * user. In such a case, simply passing it to av_opt_set() is enough. For
  199. * non-string type options, av_opt_set() will parse the string according to the
  200. * option type.
  201. *
  202. * Similarly av_opt_get() will read any option type and convert it to a string
  203. * which will be returned. Do not forget that the string is allocated, so you
  204. * have to free it with av_free().
  205. *
  206. * In some cases it may be more convenient to put all options into an
  207. * AVDictionary and call av_opt_set_dict() on it. A specific case of this
  208. * are the format/codec open functions in lavf/lavc which take a dictionary
  209. * filled with option as a parameter. This allows to set some options
  210. * that cannot be set otherwise, since e.g. the input file format is not known
  211. * before the file is actually opened.
  212. */
  213. enum AVOptionType{
  214. AV_OPT_TYPE_FLAGS,
  215. AV_OPT_TYPE_INT,
  216. AV_OPT_TYPE_INT64,
  217. AV_OPT_TYPE_DOUBLE,
  218. AV_OPT_TYPE_FLOAT,
  219. AV_OPT_TYPE_STRING,
  220. AV_OPT_TYPE_RATIONAL,
  221. AV_OPT_TYPE_BINARY, ///< offset must point to a pointer immediately followed by an int for the length
  222. AV_OPT_TYPE_DICT,
  223. AV_OPT_TYPE_CONST = 128,
  224. };
  225. /**
  226. * AVOption
  227. */
  228. typedef struct AVOption {
  229. const char *name;
  230. /**
  231. * short English help text
  232. * @todo What about other languages?
  233. */
  234. const char *help;
  235. /**
  236. * The offset relative to the context structure where the option
  237. * value is stored. It should be 0 for named constants.
  238. */
  239. int offset;
  240. enum AVOptionType type;
  241. /**
  242. * the default value for scalar options
  243. */
  244. union {
  245. int64_t i64;
  246. double dbl;
  247. const char *str;
  248. /* TODO those are unused now */
  249. AVRational q;
  250. } default_val;
  251. double min; ///< minimum valid value for the option
  252. double max; ///< maximum valid value for the option
  253. int flags;
  254. #define AV_OPT_FLAG_ENCODING_PARAM 1 ///< a generic parameter which can be set by the user for muxing or encoding
  255. #define AV_OPT_FLAG_DECODING_PARAM 2 ///< a generic parameter which can be set by the user for demuxing or decoding
  256. #if FF_API_OPT_TYPE_METADATA
  257. #define AV_OPT_FLAG_METADATA 4 ///< some data extracted or inserted into the file like title, comment, ...
  258. #endif
  259. #define AV_OPT_FLAG_AUDIO_PARAM 8
  260. #define AV_OPT_FLAG_VIDEO_PARAM 16
  261. #define AV_OPT_FLAG_SUBTITLE_PARAM 32
  262. /**
  263. * The option is inteded for exporting values to the caller.
  264. */
  265. #define AV_OPT_FLAG_EXPORT 64
  266. /**
  267. * The option may not be set through the AVOptions API, only read.
  268. * This flag only makes sense when AV_OPT_FLAG_EXPORT is also set.
  269. */
  270. #define AV_OPT_FLAG_READONLY 128
  271. //FIXME think about enc-audio, ... style flags
  272. /**
  273. * The logical unit to which the option belongs. Non-constant
  274. * options and corresponding named constants share the same
  275. * unit. May be NULL.
  276. */
  277. const char *unit;
  278. } AVOption;
  279. /**
  280. * Show the obj options.
  281. *
  282. * @param req_flags requested flags for the options to show. Show only the
  283. * options for which it is opt->flags & req_flags.
  284. * @param rej_flags rejected flags for the options to show. Show only the
  285. * options for which it is !(opt->flags & req_flags).
  286. * @param av_log_obj log context to use for showing the options
  287. */
  288. int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags);
  289. /**
  290. * Set the values of all AVOption fields to their default values.
  291. *
  292. * @param s an AVOption-enabled struct (its first member must be a pointer to AVClass)
  293. */
  294. void av_opt_set_defaults(void *s);
  295. /**
  296. * Parse the key/value pairs list in opts. For each key/value pair
  297. * found, stores the value in the field in ctx that is named like the
  298. * key. ctx must be an AVClass context, storing is done using
  299. * AVOptions.
  300. *
  301. * @param key_val_sep a 0-terminated list of characters used to
  302. * separate key from value
  303. * @param pairs_sep a 0-terminated list of characters used to separate
  304. * two pairs from each other
  305. * @return the number of successfully set key/value pairs, or a negative
  306. * value corresponding to an AVERROR code in case of error:
  307. * AVERROR(EINVAL) if opts cannot be parsed,
  308. * the error code issued by av_set_string3() if a key/value pair
  309. * cannot be set
  310. */
  311. int av_set_options_string(void *ctx, const char *opts,
  312. const char *key_val_sep, const char *pairs_sep);
  313. /**
  314. * Free all allocated objects in obj.
  315. */
  316. void av_opt_free(void *obj);
  317. /**
  318. * Check whether a particular flag is set in a flags field.
  319. *
  320. * @param field_name the name of the flag field option
  321. * @param flag_name the name of the flag to check
  322. * @return non-zero if the flag is set, zero if the flag isn't set,
  323. * isn't of the right type, or the flags field doesn't exist.
  324. */
  325. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
  326. /*
  327. * Set all the options from a given dictionary on an object.
  328. *
  329. * @param obj a struct whose first element is a pointer to AVClass
  330. * @param options options to process. This dictionary will be freed and replaced
  331. * by a new one containing all options not found in obj.
  332. * Of course this new dictionary needs to be freed by caller
  333. * with av_dict_free().
  334. *
  335. * @return 0 on success, a negative AVERROR if some option was found in obj,
  336. * but could not be set.
  337. *
  338. * @see av_dict_copy()
  339. */
  340. int av_opt_set_dict(void *obj, struct AVDictionary **options);
  341. /**
  342. * @defgroup opt_eval_funcs Evaluating option strings
  343. * @{
  344. * This group of functions can be used to evaluate option strings
  345. * and get numbers out of them. They do the same thing as av_opt_set(),
  346. * except the result is written into the caller-supplied pointer.
  347. *
  348. * @param obj a struct whose first element is a pointer to AVClass.
  349. * @param o an option for which the string is to be evaluated.
  350. * @param val string to be evaluated.
  351. * @param *_out value of the string will be written here.
  352. *
  353. * @return 0 on success, a negative number on failure.
  354. */
  355. int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int *flags_out);
  356. int av_opt_eval_int (void *obj, const AVOption *o, const char *val, int *int_out);
  357. int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t *int64_out);
  358. int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float *float_out);
  359. int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double *double_out);
  360. int av_opt_eval_q (void *obj, const AVOption *o, const char *val, AVRational *q_out);
  361. /**
  362. * @}
  363. */
  364. #define AV_OPT_SEARCH_CHILDREN 0x0001 /**< Search in possible children of the
  365. given object first. */
  366. /**
  367. * The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
  368. * instead of a required pointer to a struct containing AVClass. This is
  369. * useful for searching for options without needing to allocate the corresponding
  370. * object.
  371. */
  372. #define AV_OPT_SEARCH_FAKE_OBJ 0x0002
  373. /**
  374. * Look for an option in an object. Consider only options which
  375. * have all the specified flags set.
  376. *
  377. * @param[in] obj A pointer to a struct whose first element is a
  378. * pointer to an AVClass.
  379. * Alternatively a double pointer to an AVClass, if
  380. * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
  381. * @param[in] name The name of the option to look for.
  382. * @param[in] unit When searching for named constants, name of the unit
  383. * it belongs to.
  384. * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
  385. * @param search_flags A combination of AV_OPT_SEARCH_*.
  386. *
  387. * @return A pointer to the option found, or NULL if no option
  388. * was found.
  389. *
  390. * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
  391. * directly with av_set_string3(). Use special calls which take an options
  392. * AVDictionary (e.g. avformat_open_input()) to set options found with this
  393. * flag.
  394. */
  395. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  396. int opt_flags, int search_flags);
  397. /**
  398. * Look for an option in an object. Consider only options which
  399. * have all the specified flags set.
  400. *
  401. * @param[in] obj A pointer to a struct whose first element is a
  402. * pointer to an AVClass.
  403. * Alternatively a double pointer to an AVClass, if
  404. * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
  405. * @param[in] name The name of the option to look for.
  406. * @param[in] unit When searching for named constants, name of the unit
  407. * it belongs to.
  408. * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
  409. * @param search_flags A combination of AV_OPT_SEARCH_*.
  410. * @param[out] target_obj if non-NULL, an object to which the option belongs will be
  411. * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
  412. * in search_flags. This parameter is ignored if search_flags contain
  413. * AV_OPT_SEARCH_FAKE_OBJ.
  414. *
  415. * @return A pointer to the option found, or NULL if no option
  416. * was found.
  417. */
  418. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  419. int opt_flags, int search_flags, void **target_obj);
  420. /**
  421. * Iterate over all AVOptions belonging to obj.
  422. *
  423. * @param obj an AVOptions-enabled struct or a double pointer to an
  424. * AVClass describing it.
  425. * @param prev result of the previous call to av_opt_next() on this object
  426. * or NULL
  427. * @return next AVOption or NULL
  428. */
  429. const AVOption *av_opt_next(void *obj, const AVOption *prev);
  430. /**
  431. * Iterate over AVOptions-enabled children of obj.
  432. *
  433. * @param prev result of a previous call to this function or NULL
  434. * @return next AVOptions-enabled child or NULL
  435. */
  436. void *av_opt_child_next(void *obj, void *prev);
  437. /**
  438. * Iterate over potential AVOptions-enabled children of parent.
  439. *
  440. * @param prev result of a previous call to this function or NULL
  441. * @return AVClass corresponding to next potential child or NULL
  442. */
  443. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev);
  444. /**
  445. * @defgroup opt_set_funcs Option setting functions
  446. * @{
  447. * Those functions set the field of obj with the given name to value.
  448. *
  449. * @param[in] obj A struct whose first element is a pointer to an AVClass.
  450. * @param[in] name the name of the field to set
  451. * @param[in] val The value to set. In case of av_opt_set() if the field is not
  452. * of a string type, then the given string is parsed.
  453. * SI postfixes and some named scalars are supported.
  454. * If the field is of a numeric type, it has to be a numeric or named
  455. * scalar. Behavior with more than one scalar and +- infix operators
  456. * is undefined.
  457. * If the field is of a flags type, it has to be a sequence of numeric
  458. * scalars or named flags separated by '+' or '-'. Prefixing a flag
  459. * with '+' causes it to be set without affecting the other flags;
  460. * similarly, '-' unsets a flag.
  461. * @param search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
  462. * is passed here, then the option may be set on a child of obj.
  463. *
  464. * @return 0 if the value has been set, or an AVERROR code in case of
  465. * error:
  466. * AVERROR_OPTION_NOT_FOUND if no matching option exists
  467. * AVERROR(ERANGE) if the value is out of range
  468. * AVERROR(EINVAL) if the value is not valid
  469. */
  470. int av_opt_set (void *obj, const char *name, const char *val, int search_flags);
  471. int av_opt_set_int (void *obj, const char *name, int64_t val, int search_flags);
  472. int av_opt_set_double (void *obj, const char *name, double val, int search_flags);
  473. int av_opt_set_q (void *obj, const char *name, AVRational val, int search_flags);
  474. int av_opt_set_bin (void *obj, const char *name, const uint8_t *val, int size, int search_flags);
  475. /**
  476. * @note Any old dictionary present is discarded and replaced with a copy of the new one. The
  477. * caller still owns val is and responsible for freeing it.
  478. */
  479. int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags);
  480. /**
  481. * @}
  482. */
  483. /**
  484. * @defgroup opt_get_funcs Option getting functions
  485. * @{
  486. * Those functions get a value of the option with the given name from an object.
  487. *
  488. * @param[in] obj a struct whose first element is a pointer to an AVClass.
  489. * @param[in] name name of the option to get.
  490. * @param[in] search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
  491. * is passed here, then the option may be found in a child of obj.
  492. * @param[out] out_val value of the option will be written here
  493. * @return 0 on success, a negative error code otherwise
  494. */
  495. /**
  496. * @note the returned string will av_malloc()ed and must be av_free()ed by the caller
  497. */
  498. int av_opt_get (void *obj, const char *name, int search_flags, uint8_t **out_val);
  499. int av_opt_get_int (void *obj, const char *name, int search_flags, int64_t *out_val);
  500. int av_opt_get_double (void *obj, const char *name, int search_flags, double *out_val);
  501. int av_opt_get_q (void *obj, const char *name, int search_flags, AVRational *out_val);
  502. /**
  503. * @param[out] out_val The returned dictionary is a copy of the actual value and must
  504. * be freed with av_dict_free() by the caller
  505. */
  506. int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val);
  507. /**
  508. * @}
  509. * @}
  510. */
  511. #endif /* AVUTIL_OPT_H */