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.

574 lines
18KB

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