yading@10: /* yading@10: * Various utilities for command line tools yading@10: * copyright (c) 2003 Fabrice Bellard yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #ifndef FFMPEG_CMDUTILS_H yading@10: #define FFMPEG_CMDUTILS_H yading@10: yading@10: #include yading@10: yading@10: #include "libavcodec/avcodec.h" yading@10: #include "libavfilter/avfilter.h" yading@10: #include "libavformat/avformat.h" yading@10: #include "libswscale/swscale.h" yading@10: yading@10: #ifdef __MINGW32__ yading@10: #undef main /* We don't want SDL to override our main() */ yading@10: #endif yading@10: yading@10: /** yading@10: * program name, defined by the program for show_version(). yading@10: */ yading@10: extern const char program_name[]; yading@10: yading@10: /** yading@10: * program birth year, defined by the program for show_banner() yading@10: */ yading@10: extern const int program_birth_year; yading@10: yading@10: /** yading@10: * this year, defined by the program for show_banner() yading@10: */ yading@10: extern const int this_year; yading@10: yading@10: extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB]; yading@10: extern AVFormatContext *avformat_opts; yading@10: extern struct SwsContext *sws_opts; yading@10: extern AVDictionary *swr_opts; yading@10: extern AVDictionary *format_opts, *codec_opts, *resample_opts; yading@10: yading@10: /** yading@10: * Initialize the cmdutils option system, in particular yading@10: * allocate the *_opts contexts. yading@10: */ yading@10: void init_opts(void); yading@10: /** yading@10: * Uninitialize the cmdutils option system, in particular yading@10: * free the *_opts contexts and their contents. yading@10: */ yading@10: void uninit_opts(void); yading@10: yading@10: /** yading@10: * Trivial log callback. yading@10: * Only suitable for opt_help and similar since it lacks prefix handling. yading@10: */ yading@10: void log_callback_help(void* ptr, int level, const char* fmt, va_list vl); yading@10: yading@10: /** yading@10: * Fallback for options that are not explicitly handled, these will be yading@10: * parsed through AVOptions. yading@10: */ yading@10: int opt_default(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Set the libav* libraries log level. yading@10: */ yading@10: int opt_loglevel(void *optctx, const char *opt, const char *arg); yading@10: yading@10: int opt_report(const char *opt); yading@10: yading@10: int opt_max_alloc(void *optctx, const char *opt, const char *arg); yading@10: yading@10: int opt_cpuflags(void *optctx, const char *opt, const char *arg); yading@10: yading@10: int opt_codec_debug(void *optctx, const char *opt, const char *arg); yading@10: yading@10: int opt_opencl(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Limit the execution time. yading@10: */ yading@10: int opt_timelimit(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Parse a string and return its corresponding value as a double. yading@10: * Exit from the application if the string cannot be correctly yading@10: * parsed or the corresponding value is invalid. yading@10: * yading@10: * @param context the context of the value to be set (e.g. the yading@10: * corresponding command line option name) yading@10: * @param numstr the string to be parsed yading@10: * @param type the type (OPT_INT64 or OPT_FLOAT) as which the yading@10: * string should be parsed yading@10: * @param min the minimum valid accepted value yading@10: * @param max the maximum valid accepted value yading@10: */ yading@10: double parse_number_or_die(const char *context, const char *numstr, int type, yading@10: double min, double max); yading@10: yading@10: /** yading@10: * Parse a string specifying a time and return its corresponding yading@10: * value as a number of microseconds. Exit from the application if yading@10: * the string cannot be correctly parsed. yading@10: * yading@10: * @param context the context of the value to be set (e.g. the yading@10: * corresponding command line option name) yading@10: * @param timestr the string to be parsed yading@10: * @param is_duration a flag which tells how to interpret timestr, if yading@10: * not zero timestr is interpreted as a duration, otherwise as a yading@10: * date yading@10: * yading@10: * @see av_parse_time() yading@10: */ yading@10: int64_t parse_time_or_die(const char *context, const char *timestr, yading@10: int is_duration); yading@10: yading@10: typedef struct SpecifierOpt { yading@10: char *specifier; /**< stream/chapter/program/... specifier */ yading@10: union { yading@10: uint8_t *str; yading@10: int i; yading@10: int64_t i64; yading@10: float f; yading@10: double dbl; yading@10: } u; yading@10: } SpecifierOpt; yading@10: yading@10: typedef struct OptionDef { yading@10: const char *name; yading@10: int flags; yading@10: #define HAS_ARG 0x0001 yading@10: #define OPT_BOOL 0x0002 yading@10: #define OPT_EXPERT 0x0004 yading@10: #define OPT_STRING 0x0008 yading@10: #define OPT_VIDEO 0x0010 yading@10: #define OPT_AUDIO 0x0020 yading@10: #define OPT_INT 0x0080 yading@10: #define OPT_FLOAT 0x0100 yading@10: #define OPT_SUBTITLE 0x0200 yading@10: #define OPT_INT64 0x0400 yading@10: #define OPT_EXIT 0x0800 yading@10: #define OPT_DATA 0x1000 yading@10: #define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only). yading@10: implied by OPT_OFFSET or OPT_SPEC */ yading@10: #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */ yading@10: #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt. yading@10: Implies OPT_OFFSET. Next element after the offset is yading@10: an int containing element count in the array. */ yading@10: #define OPT_TIME 0x10000 yading@10: #define OPT_DOUBLE 0x20000 yading@10: #define OPT_INPUT 0x40000 yading@10: #define OPT_OUTPUT 0x80000 yading@10: union { yading@10: void *dst_ptr; yading@10: int (*func_arg)(void *, const char *, const char *); yading@10: size_t off; yading@10: } u; yading@10: const char *help; yading@10: const char *argname; yading@10: } OptionDef; yading@10: yading@10: /** yading@10: * Print help for all options matching specified flags. yading@10: * yading@10: * @param options a list of options yading@10: * @param msg title of this group. Only printed if at least one option matches. yading@10: * @param req_flags print only options which have all those flags set. yading@10: * @param rej_flags don't print options which have any of those flags set. yading@10: * @param alt_flags print only options that have at least one of those flags set yading@10: */ yading@10: void show_help_options(const OptionDef *options, const char *msg, int req_flags, yading@10: int rej_flags, int alt_flags); yading@10: yading@10: /** yading@10: * Show help for all options with given flags in class and all its yading@10: * children. yading@10: */ yading@10: void show_help_children(const AVClass *class, int flags); yading@10: yading@10: /** yading@10: * Per-fftool specific help handler. Implemented in each yading@10: * fftool, called by show_help(). yading@10: */ yading@10: void show_help_default(const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Generic -h handler common to all fftools. yading@10: */ yading@10: int show_help(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Parse the command line arguments. yading@10: * yading@10: * @param optctx an opaque options context yading@10: * @param argc number of command line arguments yading@10: * @param argv values of command line arguments yading@10: * @param options Array with the definitions required to interpret every yading@10: * option of the form: -option_name [argument] yading@10: * @param parse_arg_function Name of the function called to process every yading@10: * argument without a leading option name flag. NULL if such arguments do yading@10: * not have to be processed. yading@10: */ yading@10: void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, yading@10: void (* parse_arg_function)(void *optctx, const char*)); yading@10: yading@10: /** yading@10: * Parse one given option. yading@10: * yading@10: * @return on success 1 if arg was consumed, 0 otherwise; negative number on error yading@10: */ yading@10: int parse_option(void *optctx, const char *opt, const char *arg, yading@10: const OptionDef *options); yading@10: yading@10: /** yading@10: * An option extracted from the commandline. yading@10: * Cannot use AVDictionary because of options like -map which can be yading@10: * used multiple times. yading@10: */ yading@10: typedef struct Option { yading@10: const OptionDef *opt; yading@10: const char *key; yading@10: const char *val; yading@10: } Option; yading@10: yading@10: typedef struct OptionGroupDef { yading@10: /**< group name */ yading@10: const char *name; yading@10: /** yading@10: * Option to be used as group separator. Can be NULL for groups which yading@10: * are terminated by a non-option argument (e.g. ffmpeg output files) yading@10: */ yading@10: const char *sep; yading@10: /** yading@10: * Option flags that must be set on each option that is yading@10: * applied to this group yading@10: */ yading@10: int flags; yading@10: } OptionGroupDef; yading@10: yading@10: typedef struct OptionGroup { yading@10: const OptionGroupDef *group_def; yading@10: const char *arg; yading@10: yading@10: Option *opts; yading@10: int nb_opts; yading@10: yading@10: AVDictionary *codec_opts; yading@10: AVDictionary *format_opts; yading@10: AVDictionary *resample_opts; yading@10: struct SwsContext *sws_opts; yading@10: AVDictionary *swr_opts; yading@10: } OptionGroup; yading@10: yading@10: /** yading@10: * A list of option groups that all have the same group type yading@10: * (e.g. input files or output files) yading@10: */ yading@10: typedef struct OptionGroupList { yading@10: const OptionGroupDef *group_def; yading@10: yading@10: OptionGroup *groups; yading@10: int nb_groups; yading@10: } OptionGroupList; yading@10: yading@10: typedef struct OptionParseContext { yading@10: OptionGroup global_opts; yading@10: yading@10: OptionGroupList *groups; yading@10: int nb_groups; yading@10: yading@10: /* parsing state */ yading@10: OptionGroup cur_group; yading@10: } OptionParseContext; yading@10: yading@10: /** yading@10: * Parse an options group and write results into optctx. yading@10: * yading@10: * @param optctx an app-specific options context. NULL for global options group yading@10: */ yading@10: int parse_optgroup(void *optctx, OptionGroup *g); yading@10: yading@10: /** yading@10: * Split the commandline into an intermediate form convenient for further yading@10: * processing. yading@10: * yading@10: * The commandline is assumed to be composed of options which either belong to a yading@10: * group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global yading@10: * (everything else). yading@10: * yading@10: * A group (defined by an OptionGroupDef struct) is a sequence of options yading@10: * terminated by either a group separator option (e.g. -i) or a parameter that yading@10: * is not an option (doesn't start with -). A group without a separator option yading@10: * must always be first in the supplied groups list. yading@10: * yading@10: * All options within the same group are stored in one OptionGroup struct in an yading@10: * OptionGroupList, all groups with the same group definition are stored in one yading@10: * OptionGroupList in OptionParseContext.groups. The order of group lists is the yading@10: * same as the order of group definitions. yading@10: */ yading@10: int split_commandline(OptionParseContext *octx, int argc, char *argv[], yading@10: const OptionDef *options, yading@10: const OptionGroupDef *groups, int nb_groups); yading@10: yading@10: /** yading@10: * Free all allocated memory in an OptionParseContext. yading@10: */ yading@10: void uninit_parse_context(OptionParseContext *octx); yading@10: yading@10: /** yading@10: * Find the '-loglevel' option in the command line args and apply it. yading@10: */ yading@10: void parse_loglevel(int argc, char **argv, const OptionDef *options); yading@10: yading@10: /** yading@10: * Return index of option opt in argv or 0 if not found. yading@10: */ yading@10: int locate_option(int argc, char **argv, const OptionDef *options, yading@10: const char *optname); yading@10: yading@10: /** yading@10: * Check if the given stream matches a stream specifier. yading@10: * yading@10: * @param s Corresponding format context. yading@10: * @param st Stream from s to be checked. yading@10: * @param spec A stream specifier of the [v|a|s|d]:[\] form. yading@10: * yading@10: * @return 1 if the stream matches, 0 if it doesn't, <0 on error yading@10: */ yading@10: int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec); yading@10: yading@10: /** yading@10: * Filter out options for given codec. yading@10: * yading@10: * Create a new options dictionary containing only the options from yading@10: * opts which apply to the codec with ID codec_id. yading@10: * yading@10: * @param opts dictionary to place options in yading@10: * @param codec_id ID of the codec that should be filtered for yading@10: * @param s Corresponding format context. yading@10: * @param st A stream from s for which the options should be filtered. yading@10: * @param codec The particular codec for which the options should be filtered. yading@10: * If null, the default one is looked up according to the codec id. yading@10: * @return a pointer to the created dictionary yading@10: */ yading@10: AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, yading@10: AVFormatContext *s, AVStream *st, AVCodec *codec); yading@10: yading@10: /** yading@10: * Setup AVCodecContext options for avformat_find_stream_info(). yading@10: * yading@10: * Create an array of dictionaries, one dictionary for each stream yading@10: * contained in s. yading@10: * Each dictionary will contain the options from codec_opts which can yading@10: * be applied to the corresponding stream codec context. yading@10: * yading@10: * @return pointer to the created array of dictionaries, NULL if it yading@10: * cannot be created yading@10: */ yading@10: AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, yading@10: AVDictionary *codec_opts); yading@10: yading@10: /** yading@10: * Print an error message to stderr, indicating filename and a human yading@10: * readable description of the error code err. yading@10: * yading@10: * If strerror_r() is not available the use of this function in a yading@10: * multithreaded application may be unsafe. yading@10: * yading@10: * @see av_strerror() yading@10: */ yading@10: void print_error(const char *filename, int err); yading@10: yading@10: /** yading@10: * Print the program banner to stderr. The banner contents depend on the yading@10: * current version of the repository and of the libav* libraries used by yading@10: * the program. yading@10: */ yading@10: void show_banner(int argc, char **argv, const OptionDef *options); yading@10: yading@10: /** yading@10: * Print the version of the program to stdout. The version message yading@10: * depends on the current versions of the repository and of the libav* yading@10: * libraries. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_version(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print the license of the program to stdout. The license depends on yading@10: * the license of the libraries compiled into the program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_license(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the formats supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_formats(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the codecs supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_codecs(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the decoders supported by the yading@10: * program. yading@10: */ yading@10: int show_decoders(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the encoders supported by the yading@10: * program. yading@10: */ yading@10: int show_encoders(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the filters supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_filters(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the bit stream filters supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_bsfs(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the protocols supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_protocols(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the pixel formats supported by the yading@10: * program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_pix_fmts(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the standard channel layouts supported by yading@10: * the program. yading@10: * This option processing function does not utilize the arguments. yading@10: */ yading@10: int show_layouts(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Print a listing containing all the sample formats supported by the yading@10: * program. yading@10: */ yading@10: int show_sample_fmts(void *optctx, const char *opt, const char *arg); yading@10: yading@10: /** yading@10: * Return a positive value if a line read from standard input yading@10: * starts with [yY], otherwise return 0. yading@10: */ yading@10: int read_yesno(void); yading@10: yading@10: /** yading@10: * Read the file with name filename, and put its content in a newly yading@10: * allocated 0-terminated buffer. yading@10: * yading@10: * @param filename file to read from yading@10: * @param bufptr location where pointer to buffer is returned yading@10: * @param size location where size of buffer is returned yading@10: * @return 0 in case of success, a negative value corresponding to an yading@10: * AVERROR error code in case of failure. yading@10: */ yading@10: int cmdutils_read_file(const char *filename, char **bufptr, size_t *size); yading@10: yading@10: /** yading@10: * Get a file corresponding to a preset file. yading@10: * yading@10: * If is_path is non-zero, look for the file in the path preset_name. yading@10: * Otherwise search for a file named arg.ffpreset in the directories yading@10: * $FFMPEG_DATADIR (if set), $HOME/.ffmpeg, and in the datadir defined yading@10: * at configuration time or in a "ffpresets" folder along the executable yading@10: * on win32, in that order. If no such file is found and yading@10: * codec_name is defined, then search for a file named yading@10: * codec_name-preset_name.avpreset in the above-mentioned directories. yading@10: * yading@10: * @param filename buffer where the name of the found filename is written yading@10: * @param filename_size size in bytes of the filename buffer yading@10: * @param preset_name name of the preset to search yading@10: * @param is_path tell if preset_name is a filename path yading@10: * @param codec_name name of the codec for which to look for the yading@10: * preset, may be NULL yading@10: */ yading@10: FILE *get_preset_file(char *filename, size_t filename_size, yading@10: const char *preset_name, int is_path, const char *codec_name); yading@10: yading@10: /** yading@10: * Realloc array to hold new_size elements of elem_size. yading@10: * Calls exit() on failure. yading@10: * yading@10: * @param array array to reallocate yading@10: * @param elem_size size in bytes of each element yading@10: * @param size new element count will be written here yading@10: * @param new_size number of elements to place in reallocated array yading@10: * @return reallocated array yading@10: */ yading@10: void *grow_array(void *array, int elem_size, int *size, int new_size); yading@10: yading@10: #define media_type_string av_get_media_type_string yading@10: yading@10: #define GROW_ARRAY(array, nb_elems)\ yading@10: array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1) yading@10: yading@10: #define GET_PIX_FMT_NAME(pix_fmt)\ yading@10: const char *name = av_get_pix_fmt_name(pix_fmt); yading@10: yading@10: #define GET_SAMPLE_FMT_NAME(sample_fmt)\ yading@10: const char *name = av_get_sample_fmt_name(sample_fmt) yading@10: yading@10: #define GET_SAMPLE_RATE_NAME(rate)\ yading@10: char name[16];\ yading@10: snprintf(name, sizeof(name), "%d", rate); yading@10: yading@10: #define GET_CH_LAYOUT_NAME(ch_layout)\ yading@10: char name[16];\ yading@10: snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout); yading@10: yading@10: #define GET_CH_LAYOUT_DESC(ch_layout)\ yading@10: char name[128];\ yading@10: av_get_channel_layout_string(name, sizeof(name), 0, ch_layout); yading@10: yading@10: #endif /* CMDUTILS_H */