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.

494 lines
16KB

  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. /**
  40. * this year, defined by the program for show_banner()
  41. */
  42. extern const int this_year;
  43. extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
  44. extern AVFormatContext *avformat_opts;
  45. extern struct SwsContext *sws_opts;
  46. extern struct SwrContext *swr_opts;
  47. extern AVDictionary *format_opts, *codec_opts;
  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 opt_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. int opt_report(const char *opt);
  73. int opt_report_file(void *optctx, const char *opt, const char *arg);
  74. int opt_max_alloc(void *optctx, const char *opt, const char *arg);
  75. int opt_cpuflags(void *optctx, const char *opt, const char *arg);
  76. int opt_codec_debug(void *optctx, const char *opt, const char *arg);
  77. /**
  78. * Limit the execution time.
  79. */
  80. int opt_timelimit(void *optctx, const char *opt, const char *arg);
  81. /**
  82. * Parse a string and return its corresponding value as a double.
  83. * Exit from the application if the string cannot be correctly
  84. * parsed or the corresponding value is invalid.
  85. *
  86. * @param context the context of the value to be set (e.g. the
  87. * corresponding command line option name)
  88. * @param numstr the string to be parsed
  89. * @param type the type (OPT_INT64 or OPT_FLOAT) as which the
  90. * string should be parsed
  91. * @param min the minimum valid accepted value
  92. * @param max the maximum valid accepted value
  93. */
  94. double parse_number_or_die(const char *context, const char *numstr, int type,
  95. double min, double max);
  96. /**
  97. * Parse a string specifying a time and return its corresponding
  98. * value as a number of microseconds. Exit from the application if
  99. * the string cannot be correctly parsed.
  100. *
  101. * @param context the context of the value to be set (e.g. the
  102. * corresponding command line option name)
  103. * @param timestr the string to be parsed
  104. * @param is_duration a flag which tells how to interpret timestr, if
  105. * not zero timestr is interpreted as a duration, otherwise as a
  106. * date
  107. *
  108. * @see parse_date()
  109. */
  110. int64_t parse_time_or_die(const char *context, const char *timestr,
  111. int is_duration);
  112. typedef struct SpecifierOpt {
  113. char *specifier; /**< stream/chapter/program/... specifier */
  114. union {
  115. uint8_t *str;
  116. int i;
  117. int64_t i64;
  118. float f;
  119. double dbl;
  120. } u;
  121. } SpecifierOpt;
  122. typedef struct OptionDef {
  123. const char *name;
  124. int flags;
  125. #define HAS_ARG 0x0001
  126. #define OPT_BOOL 0x0002
  127. #define OPT_EXPERT 0x0004
  128. #define OPT_STRING 0x0008
  129. #define OPT_VIDEO 0x0010
  130. #define OPT_AUDIO 0x0020
  131. #define OPT_INT 0x0080
  132. #define OPT_FLOAT 0x0100
  133. #define OPT_SUBTITLE 0x0200
  134. #define OPT_INT64 0x0400
  135. #define OPT_EXIT 0x0800
  136. #define OPT_DATA 0x1000
  137. #define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).
  138. implied by OPT_OFFSET or OPT_SPEC */
  139. #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
  140. #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
  141. Implies OPT_OFFSET. Next element after the offset is
  142. an int containing element count in the array. */
  143. #define OPT_TIME 0x10000
  144. #define OPT_DOUBLE 0x20000
  145. union {
  146. void *dst_ptr;
  147. int (*func_arg)(void *, const char *, const char *);
  148. size_t off;
  149. } u;
  150. const char *help;
  151. const char *argname;
  152. } OptionDef;
  153. /**
  154. * Print help for all options matching specified flags.
  155. *
  156. * @param options a list of options
  157. * @param msg title of this group. Only printed if at least one option matches.
  158. * @param req_flags print only options which have all those flags set.
  159. * @param rej_flags don't print options which have any of those flags set.
  160. * @param alt_flags print only options that have at least one of those flags set
  161. */
  162. void show_help_options(const OptionDef *options, const char *msg, int req_flags,
  163. int rej_flags, int alt_flags);
  164. /**
  165. * Show help for all options with given flags in class and all its
  166. * children.
  167. */
  168. void show_help_children(const AVClass *class, int flags);
  169. /**
  170. * Per-avtool specific help handler. Implemented in each
  171. * avtool, called by show_help().
  172. */
  173. void show_help_default(const char *opt, const char *arg);
  174. /**
  175. * Generic -h handler common to all avtools.
  176. */
  177. int show_help(void *optctx, const char *opt, const char *arg);
  178. /**
  179. * Parse the command line arguments.
  180. *
  181. * @param optctx an opaque options context
  182. * @param argc number of command line arguments
  183. * @param argv values of command line arguments
  184. * @param options Array with the definitions required to interpret every
  185. * option of the form: -option_name [argument]
  186. * @param parse_arg_function Name of the function called to process every
  187. * argument without a leading option name flag. NULL if such arguments do
  188. * not have to be processed.
  189. */
  190. void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
  191. void (* parse_arg_function)(void *optctx, const char*));
  192. /**
  193. * Parse one given option.
  194. *
  195. * @return on success 1 if arg was consumed, 0 otherwise; negative number on error
  196. */
  197. int parse_option(void *optctx, const char *opt, const char *arg,
  198. const OptionDef *options);
  199. /**
  200. * Find the '-loglevel' option in the command line args and apply it.
  201. */
  202. void parse_loglevel(int argc, char **argv, const OptionDef *options);
  203. /**
  204. * Return index of option opt in argv or 0 if not found.
  205. */
  206. int locate_option(int argc, char **argv, const OptionDef *options,
  207. const char *optname);
  208. /**
  209. * Check if the given stream matches a stream specifier.
  210. *
  211. * @param s Corresponding format context.
  212. * @param st Stream from s to be checked.
  213. * @param spec A stream specifier of the [v|a|s|d]:[\<stream index\>] form.
  214. *
  215. * @return 1 if the stream matches, 0 if it doesn't, <0 on error
  216. */
  217. int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
  218. /**
  219. * Filter out options for given codec.
  220. *
  221. * Create a new options dictionary containing only the options from
  222. * opts which apply to the codec with ID codec_id.
  223. *
  224. * @param opts dictionary to place options in
  225. * @param codec_id ID of the codec that should be filtered for
  226. * @param s Corresponding format context.
  227. * @param st A stream from s for which the options should be filtered.
  228. * @param codec The particular codec for which the options should be filtered.
  229. * If null, the default one is looked up according to the codec id.
  230. * @return a pointer to the created dictionary
  231. */
  232. AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
  233. AVFormatContext *s, AVStream *st, AVCodec *codec);
  234. /**
  235. * Setup AVCodecContext options for avformat_find_stream_info().
  236. *
  237. * Create an array of dictionaries, one dictionary for each stream
  238. * contained in s.
  239. * Each dictionary will contain the options from codec_opts which can
  240. * be applied to the corresponding stream codec context.
  241. *
  242. * @return pointer to the created array of dictionaries, NULL if it
  243. * cannot be created
  244. */
  245. AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
  246. AVDictionary *codec_opts);
  247. /**
  248. * Print an error message to stderr, indicating filename and a human
  249. * readable description of the error code err.
  250. *
  251. * If strerror_r() is not available the use of this function in a
  252. * multithreaded application may be unsafe.
  253. *
  254. * @see av_strerror()
  255. */
  256. void print_error(const char *filename, int err);
  257. /**
  258. * Print the program banner to stderr. The banner contents depend on the
  259. * current version of the repository and of the libav* libraries used by
  260. * the program.
  261. */
  262. void show_banner(int argc, char **argv, const OptionDef *options);
  263. /**
  264. * Print the version of the program to stdout. The version message
  265. * depends on the current versions of the repository and of the libav*
  266. * libraries.
  267. * This option processing function does not utilize the arguments.
  268. */
  269. int show_version(void *optctx, const char *opt, const char *arg);
  270. /**
  271. * Print the license of the program to stdout. The license depends on
  272. * the license of the libraries compiled into the program.
  273. * This option processing function does not utilize the arguments.
  274. */
  275. int show_license(void *optctx, const char *opt, const char *arg);
  276. /**
  277. * Print a listing containing all the formats supported by the
  278. * program.
  279. * This option processing function does not utilize the arguments.
  280. */
  281. int show_formats(void *optctx, const char *opt, const char *arg);
  282. /**
  283. * Print a listing containing all the codecs supported by the
  284. * program.
  285. * This option processing function does not utilize the arguments.
  286. */
  287. int show_codecs(void *optctx, const char *opt, const char *arg);
  288. /**
  289. * Print a listing containing all the decoders supported by the
  290. * program.
  291. */
  292. int show_decoders(void *optctx, const char *opt, const char *arg);
  293. /**
  294. * Print a listing containing all the encoders supported by the
  295. * program.
  296. */
  297. int show_encoders(void *optctx, const char *opt, const char *arg);
  298. /**
  299. * Print a listing containing all the filters supported by the
  300. * program.
  301. * This option processing function does not utilize the arguments.
  302. */
  303. int show_filters(void *optctx, const char *opt, const char *arg);
  304. /**
  305. * Print a listing containing all the bit stream filters supported by the
  306. * program.
  307. * This option processing function does not utilize the arguments.
  308. */
  309. int show_bsfs(void *optctx, const char *opt, const char *arg);
  310. /**
  311. * Print a listing containing all the protocols supported by the
  312. * program.
  313. * This option processing function does not utilize the arguments.
  314. */
  315. int show_protocols(void *optctx, const char *opt, const char *arg);
  316. /**
  317. * Print a listing containing all the pixel formats supported by the
  318. * program.
  319. * This option processing function does not utilize the arguments.
  320. */
  321. int show_pix_fmts(void *optctx, const char *opt, const char *arg);
  322. /**
  323. * Print a listing containing all the standard channel layouts supported by
  324. * the program.
  325. * This option processing function does not utilize the arguments.
  326. */
  327. int show_layouts(void *optctx, const char *opt, const char *arg);
  328. /**
  329. * Print a listing containing all the sample formats supported by the
  330. * program.
  331. */
  332. int show_sample_fmts(void *optctx, const char *opt, const char *arg);
  333. /**
  334. * Return a positive value if a line read from standard input
  335. * starts with [yY], otherwise return 0.
  336. */
  337. int read_yesno(void);
  338. /**
  339. * Read the file with name filename, and put its content in a newly
  340. * allocated 0-terminated buffer.
  341. *
  342. * @param filename file to read from
  343. * @param bufptr location where pointer to buffer is returned
  344. * @param size location where size of buffer is returned
  345. * @return 0 in case of success, a negative value corresponding to an
  346. * AVERROR error code in case of failure.
  347. */
  348. int cmdutils_read_file(const char *filename, char **bufptr, size_t *size);
  349. /**
  350. * Get a file corresponding to a preset file.
  351. *
  352. * If is_path is non-zero, look for the file in the path preset_name.
  353. * Otherwise search for a file named arg.ffpreset in the directories
  354. * $FFMPEG_DATADIR (if set), $HOME/.ffmpeg, and in the datadir defined
  355. * at configuration time or in a "ffpresets" folder along the executable
  356. * on win32, in that order. If no such file is found and
  357. * codec_name is defined, then search for a file named
  358. * codec_name-preset_name.avpreset in the above-mentioned directories.
  359. *
  360. * @param filename buffer where the name of the found filename is written
  361. * @param filename_size size in bytes of the filename buffer
  362. * @param preset_name name of the preset to search
  363. * @param is_path tell if preset_name is a filename path
  364. * @param codec_name name of the codec for which to look for the
  365. * preset, may be NULL
  366. */
  367. FILE *get_preset_file(char *filename, size_t filename_size,
  368. const char *preset_name, int is_path, const char *codec_name);
  369. /**
  370. * Realloc array to hold new_size elements of elem_size.
  371. * Calls exit() on failure.
  372. *
  373. * @param array array to reallocate
  374. * @param elem_size size in bytes of each element
  375. * @param size new element count will be written here
  376. * @param new_size number of elements to place in reallocated array
  377. * @return reallocated array
  378. */
  379. void *grow_array(void *array, int elem_size, int *size, int new_size);
  380. typedef struct FrameBuffer {
  381. uint8_t *base[4];
  382. uint8_t *data[4];
  383. int linesize[4];
  384. int h, w;
  385. enum AVPixelFormat pix_fmt;
  386. int refcount;
  387. struct FrameBuffer **pool; ///< head of the buffer pool
  388. struct FrameBuffer *next;
  389. } FrameBuffer;
  390. /**
  391. * Get a frame from the pool. This is intended to be used as a callback for
  392. * AVCodecContext.get_buffer.
  393. *
  394. * @param s codec context. s->opaque must be a pointer to the head of the
  395. * buffer pool.
  396. * @param frame frame->opaque will be set to point to the FrameBuffer
  397. * containing the frame data.
  398. */
  399. int codec_get_buffer(AVCodecContext *s, AVFrame *frame);
  400. /**
  401. * A callback to be used for AVCodecContext.release_buffer along with
  402. * codec_get_buffer().
  403. */
  404. void codec_release_buffer(AVCodecContext *s, AVFrame *frame);
  405. /**
  406. * A callback to be used for AVFilterBuffer.free.
  407. * @param fb buffer to free. fb->priv must be a pointer to the FrameBuffer
  408. * containing the buffer data.
  409. */
  410. void filter_release_buffer(AVFilterBuffer *fb);
  411. /**
  412. * Free all the buffers in the pool. This must be called after all the
  413. * buffers have been released.
  414. */
  415. void free_buffer_pool(FrameBuffer **pool);
  416. #define GET_PIX_FMT_NAME(pix_fmt)\
  417. const char *name = av_get_pix_fmt_name(pix_fmt);
  418. #define GET_SAMPLE_FMT_NAME(sample_fmt)\
  419. const char *name = av_get_sample_fmt_name(sample_fmt)
  420. #define GET_SAMPLE_RATE_NAME(rate)\
  421. char name[16];\
  422. snprintf(name, sizeof(name), "%d", rate);
  423. #define GET_CH_LAYOUT_NAME(ch_layout)\
  424. char name[16];\
  425. snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
  426. #define GET_CH_LAYOUT_DESC(ch_layout)\
  427. char name[128];\
  428. av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
  429. #endif /* CMDUTILS_H */