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.

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