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.

567 lines
19KB

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