You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
15KB

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