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.

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