/*1* Various utilities for command line tools2* copyright (c) 2003 Fabrice Bellard3*4* This file is part of FFmpeg.5*6* FFmpeg is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* version 2.1 of the License, or (at your option) any later version.10*11* FFmpeg is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with FFmpeg; if not, write to the Free Software18* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA19*/2021#ifndef CMDUTILS_H22#define CMDUTILS_H2324#include <stdint.h>2526#include "config.h"27#include "libavcodec/avcodec.h"28#include "libavfilter/avfilter.h"29#include "libavformat/avformat.h"30#include "libswscale/swscale.h"3132#ifdef _WIN3233#undef main /* We don't want SDL to override our main() */34#endif3536/**37* program name, defined by the program for show_version().38*/39extern const char program_name[];4041/**42* program birth year, defined by the program for show_banner()43*/44extern const int program_birth_year;4546extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];47extern AVFormatContext *avformat_opts;48extern AVDictionary *sws_dict;49extern AVDictionary *swr_opts;50extern AVDictionary *format_opts, *codec_opts, *resample_opts;51extern int hide_banner;5253/**54* Register a program-specific cleanup routine.55*/56void register_exit(void (*cb)(int ret));5758/**59* Wraps exit with a program-specific cleanup routine.60*/61void exit_program(int ret) av_noreturn;6263/**64* Initialize the cmdutils option system, in particular65* allocate the *_opts contexts.66*/67void init_opts(void);68/**69* Uninitialize the cmdutils option system, in particular70* free the *_opts contexts and their contents.71*/72void uninit_opts(void);7374/**75* Trivial log callback.76* Only suitable for opt_help and similar since it lacks prefix handling.77*/78void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);7980/**81* Override the cpuflags.82*/83int opt_cpuflags(void *optctx, const char *opt, const char *arg);8485/**86* Fallback for options that are not explicitly handled, these will be87* parsed through AVOptions.88*/89int opt_default(void *optctx, const char *opt, const char *arg);9091/**92* Set the libav* libraries log level.93*/94int opt_loglevel(void *optctx, const char *opt, const char *arg);9596int opt_report(const char *opt);9798int opt_max_alloc(void *optctx, const char *opt, const char *arg);99100int opt_codec_debug(void *optctx, const char *opt, const char *arg);101102#if CONFIG_OPENCL103int opt_opencl(void *optctx, const char *opt, const char *arg);104105int opt_opencl_bench(void *optctx, const char *opt, const char *arg);106#endif107108/**109* Limit the execution time.110*/111int opt_timelimit(void *optctx, const char *opt, const char *arg);112113/**114* Parse a string and return its corresponding value as a double.115* Exit from the application if the string cannot be correctly116* parsed or the corresponding value is invalid.117*118* @param context the context of the value to be set (e.g. the119* corresponding command line option name)120* @param numstr the string to be parsed121* @param type the type (OPT_INT64 or OPT_FLOAT) as which the122* string should be parsed123* @param min the minimum valid accepted value124* @param max the maximum valid accepted value125*/126double parse_number_or_die(const char *context, const char *numstr, int type,127double min, double max);128129/**130* Parse a string specifying a time and return its corresponding131* value as a number of microseconds. Exit from the application if132* the string cannot be correctly parsed.133*134* @param context the context of the value to be set (e.g. the135* corresponding command line option name)136* @param timestr the string to be parsed137* @param is_duration a flag which tells how to interpret timestr, if138* not zero timestr is interpreted as a duration, otherwise as a139* date140*141* @see av_parse_time()142*/143int64_t parse_time_or_die(const char *context, const char *timestr,144int is_duration);145146typedef struct SpecifierOpt {147char *specifier; /**< stream/chapter/program/... specifier */148union {149uint8_t *str;150int i;151int64_t i64;152float f;153double dbl;154} u;155} SpecifierOpt;156157typedef struct OptionDef {158const char *name;159int flags;160#define HAS_ARG 0x0001161#define OPT_BOOL 0x0002162#define OPT_EXPERT 0x0004163#define OPT_STRING 0x0008164#define OPT_VIDEO 0x0010165#define OPT_AUDIO 0x0020166#define OPT_INT 0x0080167#define OPT_FLOAT 0x0100168#define OPT_SUBTITLE 0x0200169#define OPT_INT64 0x0400170#define OPT_EXIT 0x0800171#define OPT_DATA 0x1000172#define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).173implied by OPT_OFFSET or OPT_SPEC */174#define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */175#define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.176Implies OPT_OFFSET. Next element after the offset is177an int containing element count in the array. */178#define OPT_TIME 0x10000179#define OPT_DOUBLE 0x20000180#define OPT_INPUT 0x40000181#define OPT_OUTPUT 0x80000182union {183void *dst_ptr;184int (*func_arg)(void *, const char *, const char *);185size_t off;186} u;187const char *help;188const char *argname;189} OptionDef;190191/**192* Print help for all options matching specified flags.193*194* @param options a list of options195* @param msg title of this group. Only printed if at least one option matches.196* @param req_flags print only options which have all those flags set.197* @param rej_flags don't print options which have any of those flags set.198* @param alt_flags print only options that have at least one of those flags set199*/200void show_help_options(const OptionDef *options, const char *msg, int req_flags,201int rej_flags, int alt_flags);202203/**204* Show help for all options with given flags in class and all its205* children.206*/207void show_help_children(const AVClass *class, int flags);208209/**210* Per-fftool specific help handler. Implemented in each211* fftool, called by show_help().212*/213void show_help_default(const char *opt, const char *arg);214215/**216* Generic -h handler common to all fftools.217*/218int show_help(void *optctx, const char *opt, const char *arg);219220/**221* Parse the command line arguments.222*223* @param optctx an opaque options context224* @param argc number of command line arguments225* @param argv values of command line arguments226* @param options Array with the definitions required to interpret every227* option of the form: -option_name [argument]228* @param parse_arg_function Name of the function called to process every229* argument without a leading option name flag. NULL if such arguments do230* not have to be processed.231*/232void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,233void (* parse_arg_function)(void *optctx, const char*));234235/**236* Parse one given option.237*238* @return on success 1 if arg was consumed, 0 otherwise; negative number on error239*/240int parse_option(void *optctx, const char *opt, const char *arg,241const OptionDef *options);242243/**244* An option extracted from the commandline.245* Cannot use AVDictionary because of options like -map which can be246* used multiple times.247*/248typedef struct Option {249const OptionDef *opt;250const char *key;251const char *val;252} Option;253254typedef struct OptionGroupDef {255/**< group name */256const char *name;257/**258* Option to be used as group separator. Can be NULL for groups which259* are terminated by a non-option argument (e.g. ffmpeg output files)260*/261const char *sep;262/**263* Option flags that must be set on each option that is264* applied to this group265*/266int flags;267} OptionGroupDef;268269typedef struct OptionGroup {270const OptionGroupDef *group_def;271const char *arg;272273Option *opts;274int nb_opts;275276AVDictionary *codec_opts;277AVDictionary *format_opts;278AVDictionary *resample_opts;279AVDictionary *sws_dict;280AVDictionary *swr_opts;281} OptionGroup;282283/**284* A list of option groups that all have the same group type285* (e.g. input files or output files)286*/287typedef struct OptionGroupList {288const OptionGroupDef *group_def;289290OptionGroup *groups;291int nb_groups;292} OptionGroupList;293294typedef struct OptionParseContext {295OptionGroup global_opts;296297OptionGroupList *groups;298int nb_groups;299300/* parsing state */301OptionGroup cur_group;302} OptionParseContext;303304/**305* Parse an options group and write results into optctx.306*307* @param optctx an app-specific options context. NULL for global options group308*/309int parse_optgroup(void *optctx, OptionGroup *g);310311/**312* Split the commandline into an intermediate form convenient for further313* processing.314*315* The commandline is assumed to be composed of options which either belong to a316* group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global317* (everything else).318*319* A group (defined by an OptionGroupDef struct) is a sequence of options320* terminated by either a group separator option (e.g. -i) or a parameter that321* is not an option (doesn't start with -). A group without a separator option322* must always be first in the supplied groups list.323*324* All options within the same group are stored in one OptionGroup struct in an325* OptionGroupList, all groups with the same group definition are stored in one326* OptionGroupList in OptionParseContext.groups. The order of group lists is the327* same as the order of group definitions.328*/329int split_commandline(OptionParseContext *octx, int argc, char *argv[],330const OptionDef *options,331const OptionGroupDef *groups, int nb_groups);332333/**334* Free all allocated memory in an OptionParseContext.335*/336void uninit_parse_context(OptionParseContext *octx);337338/**339* Find the '-loglevel' option in the command line args and apply it.340*/341void parse_loglevel(int argc, char **argv, const OptionDef *options);342343/**344* Return index of option opt in argv or 0 if not found.345*/346int locate_option(int argc, char **argv, const OptionDef *options,347const char *optname);348349/**350* Check if the given stream matches a stream specifier.351*352* @param s Corresponding format context.353* @param st Stream from s to be checked.354* @param spec A stream specifier of the [v|a|s|d]:[\<stream index\>] form.355*356* @return 1 if the stream matches, 0 if it doesn't, <0 on error357*/358int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);359360/**361* Filter out options for given codec.362*363* Create a new options dictionary containing only the options from364* opts which apply to the codec with ID codec_id.365*366* @param opts dictionary to place options in367* @param codec_id ID of the codec that should be filtered for368* @param s Corresponding format context.369* @param st A stream from s for which the options should be filtered.370* @param codec The particular codec for which the options should be filtered.371* If null, the default one is looked up according to the codec id.372* @return a pointer to the created dictionary373*/374AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,375AVFormatContext *s, AVStream *st, AVCodec *codec);376377/**378* Setup AVCodecContext options for avformat_find_stream_info().379*380* Create an array of dictionaries, one dictionary for each stream381* contained in s.382* Each dictionary will contain the options from codec_opts which can383* be applied to the corresponding stream codec context.384*385* @return pointer to the created array of dictionaries, NULL if it386* cannot be created387*/388AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,389AVDictionary *codec_opts);390391/**392* Print an error message to stderr, indicating filename and a human393* readable description of the error code err.394*395* If strerror_r() is not available the use of this function in a396* multithreaded application may be unsafe.397*398* @see av_strerror()399*/400void print_error(const char *filename, int err);401402/**403* Print the program banner to stderr. The banner contents depend on the404* current version of the repository and of the libav* libraries used by405* the program.406*/407void show_banner(int argc, char **argv, const OptionDef *options);408409/**410* Print the version of the program to stdout. The version message411* depends on the current versions of the repository and of the libav*412* libraries.413* This option processing function does not utilize the arguments.414*/415int show_version(void *optctx, const char *opt, const char *arg);416417/**418* Print the build configuration of the program to stdout. The contents419* depend on the definition of FFMPEG_CONFIGURATION.420* This option processing function does not utilize the arguments.421*/422int show_buildconf(void *optctx, const char *opt, const char *arg);423424/**425* Print the license of the program to stdout. The license depends on426* the license of the libraries compiled into the program.427* This option processing function does not utilize the arguments.428*/429int show_license(void *optctx, const char *opt, const char *arg);430431/**432* Print a listing containing all the formats supported by the433* program (including devices).434* This option processing function does not utilize the arguments.435*/436int show_formats(void *optctx, const char *opt, const char *arg);437438/**439* Print a listing containing all the devices supported by the440* program.441* This option processing function does not utilize the arguments.442*/443int show_devices(void *optctx, const char *opt, const char *arg);444445#if CONFIG_AVDEVICE446/**447* Print a listing containing audodetected sinks of the output device.448* Device name with options may be passed as an argument to limit results.449*/450int show_sinks(void *optctx, const char *opt, const char *arg);451452/**453* Print a listing containing audodetected sources of the input device.454* Device name with options may be passed as an argument to limit results.455*/456int show_sources(void *optctx, const char *opt, const char *arg);457#endif458459/**460* Print a listing containing all the codecs supported by the461* program.462* This option processing function does not utilize the arguments.463*/464int show_codecs(void *optctx, const char *opt, const char *arg);465466/**467* Print a listing containing all the decoders supported by the468* program.469*/470int show_decoders(void *optctx, const char *opt, const char *arg);471472/**473* Print a listing containing all the encoders supported by the474* program.475*/476int show_encoders(void *optctx, const char *opt, const char *arg);477478/**479* Print a listing containing all the filters supported by the480* program.481* This option processing function does not utilize the arguments.482*/483int show_filters(void *optctx, const char *opt, const char *arg);484485/**486* Print a listing containing all the bit stream filters supported by the487* program.488* This option processing function does not utilize the arguments.489*/490int show_bsfs(void *optctx, const char *opt, const char *arg);491492/**493* Print a listing containing all the protocols supported by the494* program.495* This option processing function does not utilize the arguments.496*/497int show_protocols(void *optctx, const char *opt, const char *arg);498499/**500* Print a listing containing all the pixel formats supported by the501* program.502* This option processing function does not utilize the arguments.503*/504int show_pix_fmts(void *optctx, const char *opt, const char *arg);505506/**507* Print a listing containing all the standard channel layouts supported by508* the program.509* This option processing function does not utilize the arguments.510*/511int show_layouts(void *optctx, const char *opt, const char *arg);512513/**514* Print a listing containing all the sample formats supported by the515* program.516*/517int show_sample_fmts(void *optctx, const char *opt, const char *arg);518519/**520* Print a listing containing all the color names and values recognized521* by the program.522*/523int show_colors(void *optctx, const char *opt, const char *arg);524525/**526* Return a positive value if a line read from standard input527* starts with [yY], otherwise return 0.528*/529int read_yesno(void);530531/**532* Get a file corresponding to a preset file.533*534* If is_path is non-zero, look for the file in the path preset_name.535* Otherwise search for a file named arg.ffpreset in the directories536* $FFMPEG_DATADIR (if set), $HOME/.ffmpeg, and in the datadir defined537* at configuration time or in a "ffpresets" folder along the executable538* on win32, in that order. If no such file is found and539* codec_name is defined, then search for a file named540* codec_name-preset_name.avpreset in the above-mentioned directories.541*542* @param filename buffer where the name of the found filename is written543* @param filename_size size in bytes of the filename buffer544* @param preset_name name of the preset to search545* @param is_path tell if preset_name is a filename path546* @param codec_name name of the codec for which to look for the547* preset, may be NULL548*/549FILE *get_preset_file(char *filename, size_t filename_size,550const char *preset_name, int is_path, const char *codec_name);551552/**553* Realloc array to hold new_size elements of elem_size.554* Calls exit() on failure.555*556* @param array array to reallocate557* @param elem_size size in bytes of each element558* @param size new element count will be written here559* @param new_size number of elements to place in reallocated array560* @return reallocated array561*/562void *grow_array(void *array, int elem_size, int *size, int new_size);563564#define media_type_string av_get_media_type_string565566#define GROW_ARRAY(array, nb_elems)\567array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)568569#define GET_PIX_FMT_NAME(pix_fmt)\570const char *name = av_get_pix_fmt_name(pix_fmt);571572#define GET_SAMPLE_FMT_NAME(sample_fmt)\573const char *name = av_get_sample_fmt_name(sample_fmt)574575#define GET_SAMPLE_RATE_NAME(rate)\576char name[16];\577snprintf(name, sizeof(name), "%d", rate);578579#define GET_CH_LAYOUT_NAME(ch_layout)\580char name[16];\581snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);582583#define GET_CH_LAYOUT_DESC(ch_layout)\584char name[128];\585av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);586587double get_rotation(AVStream *st);588589#endif /* CMDUTILS_H */590591592