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.

554 lines
17KB

  1. /*
  2. * Various utilities for command line tools
  3. * copyright (c) 2003 Fabrice Bellard
  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 LIBAV_CMDUTILS_H
  22. #define LIBAV_CMDUTILS_H
  23. #include <stdint.h>
  24. #include "libavcodec/avcodec.h"
  25. #include "libavfilter/avfilter.h"
  26. #include "libavformat/avformat.h"
  27. #include "libswscale/swscale.h"
  28. /**
  29. * program name, defined by the program for show_version().
  30. */
  31. extern const char program_name[];
  32. /**
  33. * program birth year, defined by the program for show_banner()
  34. */
  35. extern const int program_birth_year;
  36. extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
  37. extern AVFormatContext *avformat_opts;
  38. extern struct SwsContext *sws_opts;
  39. extern AVDictionary *format_opts, *codec_opts, *resample_opts;
  40. /**
  41. * Register a program-specific cleanup routine.
  42. */
  43. void register_exit(void (*cb)(int ret));
  44. /**
  45. * Wraps exit with a program-specific cleanup routine.
  46. */
  47. void exit_program(int ret);
  48. /**
  49. * Initialize the cmdutils option system, in particular
  50. * allocate the *_opts contexts.
  51. */
  52. void init_opts(void);
  53. /**
  54. * Uninitialize the cmdutils option system, in particular
  55. * free the *_opts contexts and their contents.
  56. */
  57. void uninit_opts(void);
  58. /**
  59. * Trivial log callback.
  60. * Only suitable for show_help and similar since it lacks prefix handling.
  61. */
  62. void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
  63. /**
  64. * Fallback for options that are not explicitly handled, these will be
  65. * parsed through AVOptions.
  66. */
  67. int opt_default(void *optctx, const char *opt, const char *arg);
  68. /**
  69. * Set the libav* libraries log level.
  70. */
  71. int opt_loglevel(void *optctx, const char *opt, const char *arg);
  72. /**
  73. * Limit the execution time.
  74. */
  75. int opt_timelimit(void *optctx, const char *opt, const char *arg);
  76. /**
  77. * Parse a string and return its corresponding value as a double.
  78. * Exit from the application if the string cannot be correctly
  79. * parsed or the corresponding value is invalid.
  80. *
  81. * @param context the context of the value to be set (e.g. the
  82. * corresponding command line option name)
  83. * @param numstr the string to be parsed
  84. * @param type the type (OPT_INT64 or OPT_FLOAT) as which the
  85. * string should be parsed
  86. * @param min the minimum valid accepted value
  87. * @param max the maximum valid accepted value
  88. */
  89. double parse_number_or_die(const char *context, const char *numstr, int type,
  90. double min, double max);
  91. /**
  92. * Parse a string specifying a time and return its corresponding
  93. * value as a number of microseconds. Exit from the application if
  94. * the string cannot be correctly parsed.
  95. *
  96. * @param context the context of the value to be set (e.g. the
  97. * corresponding command line option name)
  98. * @param timestr the string to be parsed
  99. * @param is_duration a flag which tells how to interpret timestr, if
  100. * not zero timestr is interpreted as a duration, otherwise as a
  101. * date
  102. *
  103. * @see parse_date()
  104. */
  105. int64_t parse_time_or_die(const char *context, const char *timestr,
  106. int is_duration);
  107. typedef struct SpecifierOpt {
  108. char *specifier; /**< stream/chapter/program/... specifier */
  109. union {
  110. uint8_t *str;
  111. int i;
  112. int64_t i64;
  113. float f;
  114. double dbl;
  115. } u;
  116. } SpecifierOpt;
  117. typedef struct OptionDef {
  118. const char *name;
  119. int flags;
  120. #define HAS_ARG 0x0001
  121. #define OPT_BOOL 0x0002
  122. #define OPT_EXPERT 0x0004
  123. #define OPT_STRING 0x0008
  124. #define OPT_VIDEO 0x0010
  125. #define OPT_AUDIO 0x0020
  126. #define OPT_INT 0x0080
  127. #define OPT_FLOAT 0x0100
  128. #define OPT_SUBTITLE 0x0200
  129. #define OPT_INT64 0x0400
  130. #define OPT_EXIT 0x0800
  131. #define OPT_DATA 0x1000
  132. #define OPT_PERFILE 0x2000 /* the option is per-file (currently avconv-only).
  133. implied by OPT_OFFSET or OPT_SPEC */
  134. #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
  135. #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
  136. Implies OPT_OFFSET. Next element after the offset is
  137. an int containing element count in the array. */
  138. #define OPT_TIME 0x10000
  139. #define OPT_DOUBLE 0x20000
  140. #define OPT_INPUT 0x40000
  141. #define OPT_OUTPUT 0x80000
  142. union {
  143. void *dst_ptr;
  144. int (*func_arg)(void *, const char *, const char *);
  145. size_t off;
  146. } u;
  147. const char *help;
  148. const char *argname;
  149. } OptionDef;
  150. /**
  151. * Print help for all options matching specified flags.
  152. *
  153. * @param options a list of options
  154. * @param msg title of this group. Only printed if at least one option matches.
  155. * @param req_flags print only options which have all those flags set.
  156. * @param rej_flags don't print options which have any of those flags set.
  157. * @param alt_flags print only options that have at least one of those flags set
  158. */
  159. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  160. int rej_flags, int alt_flags);
  161. /**
  162. * Show help for all options with given flags in class and all its
  163. * children.
  164. */
  165. void show_help_children(const AVClass *class, int flags);
  166. /**
  167. * Per-avtool specific help handler. Implemented in each
  168. * avtool, called by show_help().
  169. */
  170. void show_help_default(const char *opt, const char *arg);
  171. /**
  172. * Generic -h handler common to all avtools.
  173. */
  174. int show_help(void *optctx, const char *opt, const char *arg);
  175. /**
  176. * Parse the command line arguments.
  177. *
  178. * @param optctx an opaque options context
  179. * @param argc number of command line arguments
  180. * @param argv values of command line arguments
  181. * @param options Array with the definitions required to interpret every
  182. * option of the form: -option_name [argument]
  183. * @param parse_arg_function Name of the function called to process every
  184. * argument without a leading option name flag. NULL if such arguments do
  185. * not have to be processed.
  186. */
  187. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  188. void (* parse_arg_function)(void *optctx, const char*));
  189. /**
  190. * Parse one given option.
  191. *
  192. * @return on success 1 if arg was consumed, 0 otherwise; negative number on error
  193. */
  194. int parse_option(void *optctx, const char *opt, const char *arg,
  195. const OptionDef *options);
  196. /**
  197. * An option extracted from the commandline.
  198. * Cannot use AVDictionary because of options like -map which can be
  199. * used multiple times.
  200. */
  201. typedef struct Option {
  202. const OptionDef *opt;
  203. const char *key;
  204. const char *val;
  205. } Option;
  206. typedef struct OptionGroupDef {
  207. /**< group name */
  208. const char *name;
  209. /**
  210. * Option to be used as group separator. Can be NULL for groups which
  211. * are terminated by a non-option argument (e.g. avconv output files)
  212. */
  213. const char *sep;
  214. /**
  215. * Option flags that must be set on each option that is
  216. * applied to this group
  217. */
  218. int flags;
  219. } OptionGroupDef;
  220. typedef struct OptionGroup {
  221. const OptionGroupDef *group_def;
  222. const char *arg;
  223. Option *opts;
  224. int nb_opts;
  225. AVDictionary *codec_opts;
  226. AVDictionary *format_opts;
  227. AVDictionary *resample_opts;
  228. struct SwsContext *sws_opts;
  229. } OptionGroup;
  230. /**
  231. * A list of option groups that all have the same group type
  232. * (e.g. input files or output files)
  233. */
  234. typedef struct OptionGroupList {
  235. const OptionGroupDef *group_def;
  236. OptionGroup *groups;
  237. int nb_groups;
  238. } OptionGroupList;
  239. typedef struct OptionParseContext {
  240. OptionGroup global_opts;
  241. OptionGroupList *groups;
  242. int nb_groups;
  243. /* parsing state */
  244. OptionGroup cur_group;
  245. } OptionParseContext;
  246. /**
  247. * Parse an options group and write results into optctx.
  248. *
  249. * @param optctx an app-specific options context. NULL for global options group
  250. */
  251. int parse_optgroup(void *optctx, OptionGroup *g);
  252. /**
  253. * Split the commandline into an intermediate form convenient for further
  254. * processing.
  255. *
  256. * The commandline is assumed to be composed of options which either belong to a
  257. * group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global
  258. * (everything else).
  259. *
  260. * A group (defined by an OptionGroupDef struct) is a sequence of options
  261. * terminated by either a group separator option (e.g. -i) or a parameter that
  262. * is not an option (doesn't start with -). A group without a separator option
  263. * must always be first in the supplied groups list.
  264. *
  265. * All options within the same group are stored in one OptionGroup struct in an
  266. * OptionGroupList, all groups with the same group definition are stored in one
  267. * OptionGroupList in OptionParseContext.groups. The order of group lists is the
  268. * same as the order of group definitions.
  269. */
  270. int split_commandline(OptionParseContext *octx, int argc, char *argv[],
  271. const OptionDef *options,
  272. const OptionGroupDef *groups, int nb_groups);
  273. /**
  274. * Free all allocated memory in an OptionParseContext.
  275. */
  276. void uninit_parse_context(OptionParseContext *octx);
  277. /**
  278. * Find the '-loglevel' option in the command line args and apply it.
  279. */
  280. void parse_loglevel(int argc, char **argv, const OptionDef *options);
  281. /**
  282. * Return index of option opt in argv or 0 if not found.
  283. */
  284. int locate_option(int argc, char **argv, const OptionDef *options,
  285. const char *optname);
  286. /**
  287. * Check if the given stream matches a stream specifier.
  288. *
  289. * @param s Corresponding format context.
  290. * @param st Stream from s to be checked.
  291. * @param spec A stream specifier of the [v|a|s|d]:[\<stream index\>] form.
  292. *
  293. * @return 1 if the stream matches, 0 if it doesn't, <0 on error
  294. */
  295. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
  296. /**
  297. * Filter out options for given codec.
  298. *
  299. * Create a new options dictionary containing only the options from
  300. * opts which apply to the codec with ID codec_id.
  301. *
  302. * @param opts dictionary to place options in
  303. * @param codec_id ID of the codec that should be filtered for
  304. * @param s Corresponding format context.
  305. * @param st A stream from s for which the options should be filtered.
  306. * @param codec The particular codec for which the options should be filtered.
  307. * If null, the default one is looked up according to the codec id.
  308. * @return a pointer to the created dictionary
  309. */
  310. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  311. AVFormatContext *s, AVStream *st, AVCodec *codec);
  312. /**
  313. * Setup AVCodecContext options for avformat_find_stream_info().
  314. *
  315. * Create an array of dictionaries, one dictionary for each stream
  316. * contained in s.
  317. * Each dictionary will contain the options from codec_opts which can
  318. * be applied to the corresponding stream codec context.
  319. *
  320. * @return pointer to the created array of dictionaries, NULL if it
  321. * cannot be created
  322. */
  323. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  324. AVDictionary *codec_opts);
  325. /**
  326. * Print an error message to stderr, indicating filename and a human
  327. * readable description of the error code err.
  328. *
  329. * If strerror_r() is not available the use of this function in a
  330. * multithreaded application may be unsafe.
  331. *
  332. * @see av_strerror()
  333. */
  334. void print_error(const char *filename, int err);
  335. /**
  336. * Print the program banner to stderr. The banner contents depend on the
  337. * current version of the repository and of the libav* libraries used by
  338. * the program.
  339. */
  340. void show_banner(void);
  341. /**
  342. * Print the version of the program to stdout. The version message
  343. * depends on the current versions of the repository and of the libav*
  344. * libraries.
  345. */
  346. int show_version(void *optctx, const char *opt, const char *arg);
  347. /**
  348. * Print the license of the program to stdout. The license depends on
  349. * the license of the libraries compiled into the program.
  350. */
  351. int show_license(void *optctx, const char *opt, const char *arg);
  352. /**
  353. * Print a listing containing all the formats supported by the
  354. * program.
  355. */
  356. int show_formats(void *optctx, const char *opt, const char *arg);
  357. /**
  358. * Print a listing containing all the codecs supported by the
  359. * program.
  360. */
  361. int show_codecs(void *optctx, const char *opt, const char *arg);
  362. /**
  363. * Print a listing containing all the decoders supported by the
  364. * program.
  365. */
  366. int show_decoders(void *optctx, const char *opt, const char *arg);
  367. /**
  368. * Print a listing containing all the encoders supported by the
  369. * program.
  370. */
  371. int show_encoders(void *optctx, const char *opt, const char *arg);
  372. /**
  373. * Print a listing containing all the filters supported by the
  374. * program.
  375. */
  376. int show_filters(void *optctx, const char *opt, const char *arg);
  377. /**
  378. * Print a listing containing all the bit stream filters supported by the
  379. * program.
  380. */
  381. int show_bsfs(void *optctx, const char *opt, const char *arg);
  382. /**
  383. * Print a listing containing all the protocols supported by the
  384. * program.
  385. */
  386. int show_protocols(void *optctx, const char *opt, const char *arg);
  387. /**
  388. * Print a listing containing all the pixel formats supported by the
  389. * program.
  390. */
  391. int show_pix_fmts(void *optctx, const char *opt, const char *arg);
  392. /**
  393. * Print a listing containing all the sample formats supported by the
  394. * program.
  395. */
  396. int show_sample_fmts(void *optctx, const char *opt, const char *arg);
  397. /**
  398. * Return a positive value if a line read from standard input
  399. * starts with [yY], otherwise return 0.
  400. */
  401. int read_yesno(void);
  402. /**
  403. * Read the file with name filename, and put its content in a newly
  404. * allocated 0-terminated buffer.
  405. *
  406. * @param filename file to read from
  407. * @param bufptr location where pointer to buffer is returned
  408. * @param size location where size of buffer is returned
  409. * @return 0 in case of success, a negative value corresponding to an
  410. * AVERROR error code in case of failure.
  411. */
  412. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size);
  413. typedef struct PtsCorrectionContext {
  414. int64_t num_faulty_pts; /// Number of incorrect PTS values so far
  415. int64_t num_faulty_dts; /// Number of incorrect DTS values so far
  416. int64_t last_pts; /// PTS of the last frame
  417. int64_t last_dts; /// DTS of the last frame
  418. } PtsCorrectionContext;
  419. /**
  420. * Reset the state of the PtsCorrectionContext.
  421. */
  422. void init_pts_correction(PtsCorrectionContext *ctx);
  423. /**
  424. * Attempt to guess proper monotonic timestamps for decoded video frames
  425. * which might have incorrect times. Input timestamps may wrap around, in
  426. * which case the output will as well.
  427. *
  428. * @param ctx the PtsCorrectionContext carrying stream pts information
  429. * @param pts the pts field of the decoded AVPacket, as passed through
  430. * AVCodecContext.reordered_opaque
  431. * @param dts the dts field of the decoded AVPacket
  432. * @return one of the input values, may be AV_NOPTS_VALUE
  433. */
  434. int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts);
  435. /**
  436. * Get a file corresponding to a preset file.
  437. *
  438. * If is_path is non-zero, look for the file in the path preset_name.
  439. * Otherwise search for a file named arg.avpreset in the directories
  440. * $AVCONV_DATADIR (if set), $HOME/.avconv, and in the datadir defined
  441. * at configuration time, in that order. If no such file is found and
  442. * codec_name is defined, then search for a file named
  443. * codec_name-preset_name.avpreset in the above-mentioned directories.
  444. *
  445. * @param filename buffer where the name of the found filename is written
  446. * @param filename_size size in bytes of the filename buffer
  447. * @param preset_name name of the preset to search
  448. * @param is_path tell if preset_name is a filename path
  449. * @param codec_name name of the codec for which to look for the
  450. * preset, may be NULL
  451. */
  452. FILE *get_preset_file(char *filename, size_t filename_size,
  453. const char *preset_name, int is_path, const char *codec_name);
  454. /**
  455. * Realloc array to hold new_size elements of elem_size.
  456. * Calls exit() on failure.
  457. *
  458. * @param array array to reallocate
  459. * @param elem_size size in bytes of each element
  460. * @param size new element count will be written here
  461. * @param new_size number of elements to place in reallocated array
  462. * @return reallocated array
  463. */
  464. void *grow_array(void *array, int elem_size, int *size, int new_size);
  465. /**
  466. * Get a string describing a media type.
  467. */
  468. const char *media_type_string(enum AVMediaType media_type);
  469. #define GROW_ARRAY(array, nb_elems)\
  470. array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
  471. #define GET_PIX_FMT_NAME(pix_fmt)\
  472. const char *name = av_get_pix_fmt_name(pix_fmt);
  473. #define GET_SAMPLE_FMT_NAME(sample_fmt)\
  474. const char *name = av_get_sample_fmt_name(sample_fmt)
  475. #define GET_SAMPLE_RATE_NAME(rate)\
  476. char name[16];\
  477. snprintf(name, sizeof(name), "%d", rate);
  478. #define GET_CH_LAYOUT_NAME(ch_layout)\
  479. char name[16];\
  480. snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
  481. #define GET_CH_LAYOUT_DESC(ch_layout)\
  482. char name[128];\
  483. av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
  484. #endif /* LIBAV_CMDUTILS_H */