opt.h 39 KB

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