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.

409 lines
16KB

  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 "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 opts options string to parse, may be NULL
  179. * @param key_val_sep a 0-terminated list of characters used to
  180. * separate key from value
  181. * @param pairs_sep a 0-terminated list of characters used to separate
  182. * two pairs from each other
  183. * @return the number of successfully set key/value pairs, or a negative
  184. * value corresponding to an AVERROR code in case of error:
  185. * AVERROR(EINVAL) if opts cannot be parsed,
  186. * the error code issued by av_set_string3() if a key/value pair
  187. * cannot be set
  188. */
  189. int av_set_options_string(void *ctx, const char *opts,
  190. const char *key_val_sep, const char *pairs_sep);
  191. /**
  192. * Free all string and binary options in obj.
  193. */
  194. void av_opt_free(void *obj);
  195. /**
  196. * Check whether a particular flag is set in a flags field.
  197. *
  198. * @param field_name the name of the flag field option
  199. * @param flag_name the name of the flag to check
  200. * @return non-zero if the flag is set, zero if the flag isn't set,
  201. * isn't of the right type, or the flags field doesn't exist.
  202. */
  203. int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name);
  204. /*
  205. * Set all the options from a given dictionary on an object.
  206. *
  207. * @param obj a struct whose first element is a pointer to AVClass
  208. * @param options options to process. This dictionary will be freed and replaced
  209. * by a new one containing all options not found in obj.
  210. * Of course this new dictionary needs to be freed by caller
  211. * with av_dict_free().
  212. *
  213. * @return 0 on success, a negative AVERROR if some option was found in obj,
  214. * but could not be set.
  215. *
  216. * @see av_dict_copy()
  217. */
  218. int av_opt_set_dict(void *obj, struct AVDictionary **options);
  219. /**
  220. * @defgroup opt_eval_funcs Evaluating option strings
  221. * @{
  222. * This group of functions can be used to evaluate option strings
  223. * and get numbers out of them. They do the same thing as av_opt_set(),
  224. * except the result is written into the caller-supplied pointer.
  225. *
  226. * @param obj a struct whose first element is a pointer to AVClass.
  227. * @param o an option for which the string is to be evaluated.
  228. * @param val string to be evaluated.
  229. * @param *_out value of the string will be written here.
  230. *
  231. * @return 0 on success, a negative number on failure.
  232. */
  233. int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int *flags_out);
  234. int av_opt_eval_int (void *obj, const AVOption *o, const char *val, int *int_out);
  235. int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t *int64_out);
  236. int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float *float_out);
  237. int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double *double_out);
  238. int av_opt_eval_q (void *obj, const AVOption *o, const char *val, AVRational *q_out);
  239. /**
  240. * @}
  241. */
  242. #define AV_OPT_SEARCH_CHILDREN 0x0001 /**< Search in possible children of the
  243. given object first. */
  244. /**
  245. * The obj passed to av_opt_find() is fake -- only a double pointer to AVClass
  246. * instead of a required pointer to a struct containing AVClass. This is
  247. * useful for searching for options without needing to allocate the corresponding
  248. * object.
  249. */
  250. #define AV_OPT_SEARCH_FAKE_OBJ 0x0002
  251. /**
  252. * Look for an option in an object. Consider only options which
  253. * have all the specified flags set.
  254. *
  255. * @param[in] obj A pointer to a struct whose first element is a
  256. * pointer to an AVClass.
  257. * Alternatively a double pointer to an AVClass, if
  258. * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
  259. * @param[in] name The name of the option to look for.
  260. * @param[in] unit When searching for named constants, name of the unit
  261. * it belongs to.
  262. * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
  263. * @param search_flags A combination of AV_OPT_SEARCH_*.
  264. *
  265. * @return A pointer to the option found, or NULL if no option
  266. * was found.
  267. *
  268. * @note Options found with AV_OPT_SEARCH_CHILDREN flag may not be settable
  269. * directly with av_set_string3(). Use special calls which take an options
  270. * AVDictionary (e.g. avformat_open_input()) to set options found with this
  271. * flag.
  272. */
  273. const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
  274. int opt_flags, int search_flags);
  275. /**
  276. * Look for an option in an object. Consider only options which
  277. * have all the specified flags set.
  278. *
  279. * @param[in] obj A pointer to a struct whose first element is a
  280. * pointer to an AVClass.
  281. * Alternatively a double pointer to an AVClass, if
  282. * AV_OPT_SEARCH_FAKE_OBJ search flag is set.
  283. * @param[in] name The name of the option to look for.
  284. * @param[in] unit When searching for named constants, name of the unit
  285. * it belongs to.
  286. * @param opt_flags Find only options with all the specified flags set (AV_OPT_FLAG).
  287. * @param search_flags A combination of AV_OPT_SEARCH_*.
  288. * @param[out] target_obj if non-NULL, an object to which the option belongs will be
  289. * written here. It may be different from obj if AV_OPT_SEARCH_CHILDREN is present
  290. * in search_flags. This parameter is ignored if search_flags contain
  291. * AV_OPT_SEARCH_FAKE_OBJ.
  292. *
  293. * @return A pointer to the option found, or NULL if no option
  294. * was found.
  295. */
  296. const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
  297. int opt_flags, int search_flags, void **target_obj);
  298. /**
  299. * Iterate over all AVOptions belonging to obj.
  300. *
  301. * @param obj an AVOptions-enabled struct or a double pointer to an
  302. * AVClass describing it.
  303. * @param prev result of the previous call to av_opt_next() on this object
  304. * or NULL
  305. * @return next AVOption or NULL
  306. */
  307. const AVOption *av_opt_next(void *obj, const AVOption *prev);
  308. /**
  309. * Iterate over AVOptions-enabled children of obj.
  310. *
  311. * @param prev result of a previous call to this function or NULL
  312. * @return next AVOptions-enabled child or NULL
  313. */
  314. void *av_opt_child_next(void *obj, void *prev);
  315. /**
  316. * Iterate over potential AVOptions-enabled children of parent.
  317. *
  318. * @param prev result of a previous call to this function or NULL
  319. * @return AVClass corresponding to next potential child or NULL
  320. */
  321. const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev);
  322. /**
  323. * @defgroup opt_set_funcs Option setting functions
  324. * @{
  325. * Those functions set the field of obj with the given name to value.
  326. *
  327. * @param[in] obj A struct whose first element is a pointer to an AVClass.
  328. * @param[in] name the name of the field to set
  329. * @param[in] val The value to set. In case of av_opt_set() if the field is not
  330. * of a string type, then the given string is parsed.
  331. * SI postfixes and some named scalars are supported.
  332. * If the field is of a numeric type, it has to be a numeric or named
  333. * scalar. Behavior with more than one scalar and +- infix operators
  334. * is undefined.
  335. * If the field is of a flags type, it has to be a sequence of numeric
  336. * scalars or named flags separated by '+' or '-'. Prefixing a flag
  337. * with '+' causes it to be set without affecting the other flags;
  338. * similarly, '-' unsets a flag.
  339. * @param search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
  340. * is passed here, then the option may be set on a child of obj.
  341. *
  342. * @return 0 if the value has been set, or an AVERROR code in case of
  343. * error:
  344. * AVERROR_OPTION_NOT_FOUND if no matching option exists
  345. * AVERROR(ERANGE) if the value is out of range
  346. * AVERROR(EINVAL) if the value is not valid
  347. */
  348. int av_opt_set (void *obj, const char *name, const char *val, int search_flags);
  349. int av_opt_set_int (void *obj, const char *name, int64_t val, int search_flags);
  350. int av_opt_set_double(void *obj, const char *name, double val, int search_flags);
  351. int av_opt_set_q (void *obj, const char *name, AVRational val, int search_flags);
  352. /**
  353. * @}
  354. */
  355. /**
  356. * @defgroup opt_get_funcs Option getting functions
  357. * @{
  358. * Those functions get a value of the option with the given name from an object.
  359. *
  360. * @param[in] obj a struct whose first element is a pointer to an AVClass.
  361. * @param[in] name name of the option to get.
  362. * @param[in] search_flags flags passed to av_opt_find2. I.e. if AV_OPT_SEARCH_CHILDREN
  363. * is passed here, then the option may be found in a child of obj.
  364. * @param[out] out_val value of the option will be written here
  365. * @return 0 on success, a negative error code otherwise
  366. */
  367. /**
  368. * @note the returned string will av_malloc()ed and must be av_free()ed by the caller
  369. */
  370. int av_opt_get (void *obj, const char *name, int search_flags, uint8_t **out_val);
  371. int av_opt_get_int (void *obj, const char *name, int search_flags, int64_t *out_val);
  372. int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val);
  373. int av_opt_get_q (void *obj, const char *name, int search_flags, AVRational *out_val);
  374. /**
  375. * @}
  376. */
  377. #endif /* AVUTIL_OPT_H */