FFmpeg
cmdutils.h
Go to the documentation of this file.
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 
22 #ifndef FFTOOLS_CMDUTILS_H
23 #define FFTOOLS_CMDUTILS_H
24 
25 #include <stdint.h>
26 
27 #include "config.h"
28 #include "libavcodec/avcodec.h"
29 #include "libavfilter/avfilter.h"
30 #include "libavformat/avformat.h"
31 #include "libswscale/swscale.h"
32 
33 #ifdef _WIN32
34 #undef main /* We don't want SDL to override our main() */
35 #endif
36 
37 /**
38  * program name, defined by the program for show_version().
39  */
40 extern const char program_name[];
41 
42 /**
43  * program birth year, defined by the program for show_banner()
44  */
45 extern const int program_birth_year;
46 
47 extern AVDictionary *sws_dict;
48 extern AVDictionary *swr_opts;
50 extern int hide_banner;
51 
52 /**
53  * Initialize dynamic library loading
54  */
55 void init_dynload(void);
56 
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 /**
64  * Trivial log callback.
65  * Only suitable for opt_help and similar since it lacks prefix handling.
66  */
67 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
68 
69 /**
70  * Fallback for options that are not explicitly handled, these will be
71  * parsed through AVOptions.
72  */
73 int opt_default(void *optctx, const char *opt, const char *arg);
74 
75 /**
76  * Limit the execution time.
77  */
78 int opt_timelimit(void *optctx, const char *opt, const char *arg);
79 
80 /**
81  * Parse a string and return its corresponding value as a double.
82  *
83  * @param context the context of the value to be set (e.g. the
84  * corresponding command line option name)
85  * @param numstr the string to be parsed
86  * @param type the type (OPT_INT64 or OPT_FLOAT) as which the
87  * string should be parsed
88  * @param min the minimum valid accepted value
89  * @param max the maximum valid accepted value
90  */
91 int parse_number(const char *context, const char *numstr, int type,
92  double min, double max, double *dst);
93 
94 typedef struct SpecifierOpt {
95  char *specifier; /**< stream/chapter/program/... specifier */
96  union {
97  uint8_t *str;
98  int i;
99  int64_t i64;
100  uint64_t ui64;
101  float f;
102  double dbl;
103  } u;
104 } SpecifierOpt;
105 
106 typedef struct OptionDef {
107  const char *name;
108  int flags;
109 #define HAS_ARG 0x0001
110 #define OPT_BOOL 0x0002
111 #define OPT_EXPERT 0x0004
112 #define OPT_STRING 0x0008
113 #define OPT_VIDEO 0x0010
114 #define OPT_AUDIO 0x0020
115 #define OPT_INT 0x0080
116 #define OPT_FLOAT 0x0100
117 #define OPT_SUBTITLE 0x0200
118 #define OPT_INT64 0x0400
119 #define OPT_EXIT 0x0800
120 #define OPT_DATA 0x1000
121 #define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).
122  implied by OPT_OFFSET or OPT_SPEC */
123 #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
124 #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
125  Implies OPT_OFFSET. Next element after the offset is
126  an int containing element count in the array. */
127 #define OPT_TIME 0x10000
128 #define OPT_DOUBLE 0x20000
129 #define OPT_INPUT 0x40000
130 #define OPT_OUTPUT 0x80000
131  union {
132  void *dst_ptr;
133  int (*func_arg)(void *, const char *, const char *);
134  size_t off;
135  } u;
136  const char *help;
137  const char *argname;
138 } OptionDef;
139 
140 /**
141  * Print help for all options matching specified flags.
142  *
143  * @param options a list of options
144  * @param msg title of this group. Only printed if at least one option matches.
145  * @param req_flags print only options which have all those flags set.
146  * @param rej_flags don't print options which have any of those flags set.
147  * @param alt_flags print only options that have at least one of those flags set
148  */
149 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
150  int rej_flags, int alt_flags);
151 
152 /**
153  * Show help for all options with given flags in class and all its
154  * children.
155  */
156 void show_help_children(const AVClass *class, int flags);
157 
158 /**
159  * Per-fftool specific help handler. Implemented in each
160  * fftool, called by show_help().
161  */
162 void show_help_default(const char *opt, const char *arg);
163 
164 /**
165  * Parse the command line arguments.
166  *
167  * @param optctx an opaque options context
168  * @param argc number of command line arguments
169  * @param argv values of command line arguments
170  * @param options Array with the definitions required to interpret every
171  * option of the form: -option_name [argument]
172  * @param parse_arg_function Name of the function called to process every
173  * argument without a leading option name flag. NULL if such arguments do
174  * not have to be processed.
175  */
176 int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
177  int (* parse_arg_function)(void *optctx, const char*));
178 
179 /**
180  * Parse one given option.
181  *
182  * @return on success 1 if arg was consumed, 0 otherwise; negative number on error
183  */
184 int parse_option(void *optctx, const char *opt, const char *arg,
185  const OptionDef *options);
186 
187 /**
188  * An option extracted from the commandline.
189  * Cannot use AVDictionary because of options like -map which can be
190  * used multiple times.
191  */
192 typedef struct Option {
193  const OptionDef *opt;
194  const char *key;
195  const char *val;
196 } Option;
197 
198 typedef struct OptionGroupDef {
199  /**< group name */
200  const char *name;
201  /**
202  * Option to be used as group separator. Can be NULL for groups which
203  * are terminated by a non-option argument (e.g. ffmpeg output files)
204  */
205  const char *sep;
206  /**
207  * Option flags that must be set on each option that is
208  * applied to this group
209  */
210  int flags;
212 
213 typedef struct OptionGroup {
215  const char *arg;
216 
218  int nb_opts;
219 
224 } OptionGroup;
225 
226 /**
227  * A list of option groups that all have the same group type
228  * (e.g. input files or output files)
229  */
230 typedef struct OptionGroupList {
232 
236 
237 typedef struct OptionParseContext {
239 
242 
243  /* parsing state */
246 
247 /**
248  * Parse an options group and write results into optctx.
249  *
250  * @param optctx an app-specific options context. NULL for global options group
251  */
252 int parse_optgroup(void *optctx, OptionGroup *g);
253 
254 /**
255  * Split the commandline into an intermediate form convenient for further
256  * processing.
257  *
258  * The commandline is assumed to be composed of options which either belong to a
259  * group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global
260  * (everything else).
261  *
262  * A group (defined by an OptionGroupDef struct) is a sequence of options
263  * terminated by either a group separator option (e.g. -i) or a parameter that
264  * is not an option (doesn't start with -). A group without a separator option
265  * must always be first in the supplied groups list.
266  *
267  * All options within the same group are stored in one OptionGroup struct in an
268  * OptionGroupList, all groups with the same group definition are stored in one
269  * OptionGroupList in OptionParseContext.groups. The order of group lists is the
270  * same as the order of group definitions.
271  */
272 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
273  const OptionDef *options,
274  const OptionGroupDef *groups, int nb_groups);
275 
276 /**
277  * Free all allocated memory in an OptionParseContext.
278  */
280 
281 /**
282  * Find the '-loglevel' option in the command line args and apply it.
283  */
284 void parse_loglevel(int argc, char **argv, const OptionDef *options);
285 
286 /**
287  * Return index of option opt in argv or 0 if not found.
288  */
289 int locate_option(int argc, char **argv, const OptionDef *options,
290  const char *optname);
291 
292 /**
293  * Check if the given stream matches a stream specifier.
294  *
295  * @param s Corresponding format context.
296  * @param st Stream from s to be checked.
297  * @param spec A stream specifier of the [v|a|s|d]:[<stream index>] form.
298  *
299  * @return 1 if the stream matches, 0 if it doesn't, <0 on error
300  */
301 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
302 
303 /**
304  * Filter out options for given codec.
305  *
306  * Create a new options dictionary containing only the options from
307  * opts which apply to the codec with ID codec_id.
308  *
309  * @param opts dictionary to place options in
310  * @param codec_id ID of the codec that should be filtered for
311  * @param s Corresponding format context.
312  * @param st A stream from s for which the options should be filtered.
313  * @param codec The particular codec for which the options should be filtered.
314  * If null, the default one is looked up according to the codec id.
315  * @param dst a pointer to the created dictionary
316  * @return a non-negative number on success, a negative error code on failure
317  */
319  AVFormatContext *s, AVStream *st, const AVCodec *codec,
320  AVDictionary **dst);
321 
322 /**
323  * Setup AVCodecContext options for avformat_find_stream_info().
324  *
325  * Create an array of dictionaries, one dictionary for each stream
326  * contained in s.
327  * Each dictionary will contain the options from codec_opts which can
328  * be applied to the corresponding stream codec context.
329  */
332  AVDictionary ***dst);
333 
334 /**
335  * Print an error message to stderr, indicating filename and a human
336  * readable description of the error code err.
337  *
338  * If strerror_r() is not available the use of this function in a
339  * multithreaded application may be unsafe.
340  *
341  * @see av_strerror()
342  */
343 void print_error(const char *filename, int err);
344 
345 /**
346  * Print the program banner to stderr. The banner contents depend on the
347  * current version of the repository and of the libav* libraries used by
348  * the program.
349  */
350 void show_banner(int argc, char **argv, const OptionDef *options);
351 
352 /**
353  * Return a positive value if a line read from standard input
354  * starts with [yY], otherwise return 0.
355  */
356 int read_yesno(void);
357 
358 /**
359  * Get a file corresponding to a preset file.
360  *
361  * If is_path is non-zero, look for the file in the path preset_name.
362  * Otherwise search for a file named arg.ffpreset in the directories
363  * $FFMPEG_DATADIR (if set), $HOME/.ffmpeg, and in the datadir defined
364  * at configuration time or in a "ffpresets" folder along the executable
365  * on win32, in that order. If no such file is found and
366  * codec_name is defined, then search for a file named
367  * codec_name-preset_name.avpreset in the above-mentioned directories.
368  *
369  * @param filename buffer where the name of the found filename is written
370  * @param filename_size size in bytes of the filename buffer
371  * @param preset_name name of the preset to search
372  * @param is_path tell if preset_name is a filename path
373  * @param codec_name name of the codec for which to look for the
374  * preset, may be NULL
375  */
376 FILE *get_preset_file(char *filename, size_t filename_size,
377  const char *preset_name, int is_path, const char *codec_name);
378 
379 /**
380  * Realloc array to hold new_size elements of elem_size.
381  *
382  * @param array pointer to the array to reallocate, will be updated
383  * with a new pointer on success
384  * @param elem_size size in bytes of each element
385  * @param size new element count will be written here
386  * @param new_size number of elements to place in reallocated array
387  * @return a non-negative number on success, a negative error code on failure
388  */
389 int grow_array(void **array, int elem_size, int *size, int new_size);
390 
391 /**
392  * Atomically add a new element to an array of pointers, i.e. allocate
393  * a new entry, reallocate the array of pointers and make the new last
394  * member of this array point to the newly allocated buffer.
395  *
396  * @param array array of pointers to reallocate
397  * @param elem_size size of the new element to allocate
398  * @param nb_elems pointer to the number of elements of the array array;
399  * *nb_elems will be incremented by one by this function.
400  * @return pointer to the newly allocated entry or NULL on failure
401  */
402 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
403 
404 #define GROW_ARRAY(array, nb_elems)\
405  grow_array((void**)&array, sizeof(*array), &nb_elems, nb_elems + 1)
406 
407 #define GET_PIX_FMT_NAME(pix_fmt)\
408  const char *name = av_get_pix_fmt_name(pix_fmt);
409 
410 #define GET_CODEC_NAME(id)\
411  const char *name = avcodec_descriptor_get(id)->name;
412 
413 #define GET_SAMPLE_FMT_NAME(sample_fmt)\
414  const char *name = av_get_sample_fmt_name(sample_fmt)
415 
416 #define GET_SAMPLE_RATE_NAME(rate)\
417  char name[16];\
418  snprintf(name, sizeof(name), "%d", rate);
419 
420 double get_rotation(const int32_t *displaymatrix);
421 
422 #endif /* FFTOOLS_CMDUTILS_H */
AVCodec
AVCodec.
Definition: codec.h:187
OptionGroup::group_def
const OptionGroupDef * group_def
Definition: cmdutils.h:211
show_help_default
void show_help_default(const char *opt, const char *arg)
Per-fftool specific help handler.
Definition: ffmpeg_opt.c:1176
level
uint8_t level
Definition: svq3.c:204
OptionDef::off
size_t off
Definition: cmdutils.h:131
SpecifierOpt::ui64
uint64_t ui64
Definition: cmdutils.h:100
program_name
const char program_name[]
program name, defined by the program for show_version().
Definition: ffmpeg.c:104
parse_number
int parse_number(const char *context, const char *numstr, int type, double min, double max, double *dst)
Parse a string and return its corresponding value as a double.
Definition: cmdutils.c:86
sws_dict
AVDictionary * sws_dict
Definition: cmdutils.c:58
OptionGroupList::groups
OptionGroup * groups
Definition: cmdutils.h:230
OptionDef::dst_ptr
void * dst_ptr
Definition: cmdutils.h:129
OptionGroupList::nb_groups
int nb_groups
Definition: cmdutils.h:231
codec_opts
AVDictionary * codec_opts
Definition: cmdutils.h:49
opt_timelimit
int opt_timelimit(void *optctx, const char *opt, const char *arg)
Limit the execution time.
OptionGroup::swr_opts
AVDictionary * swr_opts
Definition: cmdutils.h:220
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVDictionary
Definition: dict.c:34
get_preset_file
FILE * get_preset_file(char *filename, size_t filename_size, const char *preset_name, int is_path, const char *codec_name)
Get a file corresponding to a preset file.
Definition: cmdutils.c:845
filter_codec_opts
int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, const AVCodec *codec, AVDictionary **dst)
Filter out options for given codec.
Definition: cmdutils.c:925
OptionDef
Definition: cmdutils.h:106
SpecifierOpt::i
int i
Definition: cmdutils.h:98
OptionGroupList
A list of option groups that all have the same group type (e.g.
Definition: cmdutils.h:227
OptionParseContext
Definition: cmdutils.h:234
Option
An option extracted from the commandline.
Definition: cmdutils.h:189
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
OptionGroup::nb_opts
int nb_opts
Definition: cmdutils.h:215
OptionGroupList::group_def
const OptionGroupDef * group_def
Definition: cmdutils.h:228
OptionDef::help
const char * help
Definition: cmdutils.h:133
OptionGroupDef
Definition: cmdutils.h:195
OptionGroup::codec_opts
AVDictionary * codec_opts
Definition: cmdutils.h:217
OptionGroupDef::flags
int flags
Option flags that must be set on each option that is applied to this group.
Definition: cmdutils.h:207
setup_find_stream_info_opts
int setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts, AVDictionary ***dst)
Setup AVCodecContext options for avformat_find_stream_info().
Definition: cmdutils.c:990
SpecifierOpt::specifier
char * specifier
stream/chapter/program/...
Definition: cmdutils.h:95
s
#define s(width, name)
Definition: cbs_vp9.c:198
OptionDef::argname
const char * argname
Definition: cmdutils.h:134
uninit_opts
void uninit_opts(void)
Uninitialize the cmdutils option system, in particular free the *_opts contexts and their contents.
Definition: cmdutils.c:64
g
const char * g
Definition: vf_curves.c:127
Option::key
const char * key
Definition: cmdutils.h:191
print_error
void print_error(const char *filename, int err)
Print an error message to stderr, indicating filename and a human readable description of the error c...
Definition: cmdutils.c:829
allocate_array_elem
void * allocate_array_elem(void *array, size_t elem_size, int *nb_elems)
Atomically add a new element to an array of pointers, i.e.
Definition: cmdutils.c:1039
init_dynload
void init_dynload(void)
Initialize dynamic library loading.
Definition: cmdutils.c:77
show_help_children
void show_help_children(const AVClass *class, int flags)
Show help for all options with given flags in class and all its children.
Definition: cmdutils.c:138
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:389
arg
const char * arg
Definition: jacosubdec.c:67
OptionGroupDef::name
const char * name
< group name
Definition: cmdutils.h:197
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
opts
AVDictionary * opts
Definition: movenc.c:50
OptionGroup::format_opts
AVDictionary * format_opts
Definition: cmdutils.h:218
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
OptionParseContext::global_opts
OptionGroup global_opts
Definition: cmdutils.h:235
Option::opt
const OptionDef * opt
Definition: cmdutils.h:190
swr_opts
AVDictionary * swr_opts
Definition: cmdutils.c:59
OptionGroup::opts
Option * opts
Definition: cmdutils.h:214
OptionGroup
Definition: cmdutils.h:210
parse_optgroup
int parse_optgroup(void *optctx, OptionGroup *g)
Parse an options group and write results into optctx.
Definition: cmdutils.c:382
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
check_stream_specifier
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the given stream matches a stream specifier.
Definition: cmdutils.c:917
read_yesno
int read_yesno(void)
Return a positive value if a line read from standard input starts with [yY], otherwise return 0.
Definition: cmdutils.c:834
options
const OptionDef options[]
grow_array
int grow_array(void **array, int elem_size, int *size, int new_size)
Realloc array to hold new_size elements of elem_size.
Definition: cmdutils.c:1021
size
int size
Definition: twinvq_data.h:10344
show_banner
void show_banner(int argc, char **argv, const OptionDef *options)
Print the program banner to stderr.
Definition: opt_common.c:237
uninit_parse_context
void uninit_parse_context(OptionParseContext *octx)
Free all allocated memory in an OptionParseContext.
Definition: cmdutils.c:682
parse_loglevel
void parse_loglevel(int argc, char **argv, const OptionDef *options)
Find the '-loglevel' option in the command line args and apply it.
Definition: cmdutils.c:474
opt_default
int opt_default(void *optctx, const char *opt, const char *arg)
Fallback for options that are not explicitly handled, these will be parsed through AVOptions.
Definition: cmdutils.c:516
format_opts
AVDictionary * format_opts
Definition: cmdutils.c:60
OptionParseContext::groups
OptionGroupList * groups
Definition: cmdutils.h:237
OptionDef::u
union OptionDef::@1 u
avcodec.h
SpecifierOpt::i64
int64_t i64
Definition: cmdutils.h:99
OptionGroup::sws_dict
AVDictionary * sws_dict
Definition: cmdutils.h:219
SpecifierOpt
Definition: cmdutils.h:94
split_commandline
int split_commandline(OptionParseContext *octx, int argc, char *argv[], const OptionDef *options, const OptionGroupDef *groups, int nb_groups)
Split the commandline into an intermediate form convenient for further processing.
Definition: cmdutils.c:707
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
AVStream
Stream structure.
Definition: avformat.h:841
hide_banner
int hide_banner
Definition: cmdutils.c:62
OptionGroup::arg
const char * arg
Definition: cmdutils.h:212
avformat.h
parse_options
int parse_options(void *optctx, int argc, char **argv, const OptionDef *options, int(*parse_arg_function)(void *optctx, const char *))
Parse the command line arguments.
SpecifierOpt::str
uint8_t * str
Definition: cmdutils.h:97
avfilter.h
Option::val
const char * val
Definition: cmdutils.h:192
SpecifierOpt::u
union SpecifierOpt::@0 u
OptionDef::name
const char * name
Definition: cmdutils.h:107
SpecifierOpt::dbl
double dbl
Definition: cmdutils.h:102
OptionGroupDef::sep
const char * sep
Option to be used as group separator.
Definition: cmdutils.h:202
log_callback_help
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition: cmdutils.c:72
get_rotation
double get_rotation(const int32_t *displaymatrix)
Definition: cmdutils.c:1049
int32_t
int32_t
Definition: audioconvert.c:56
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
OptionParseContext::nb_groups
int nb_groups
Definition: cmdutils.h:238
parse_option
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
Parse one given option.
Definition: cmdutils.c:307
locate_option
int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
Return index of option opt in argv or 0 if not found.
Definition: cmdutils.c:415
int
int
Definition: ffmpeg_filter.c:368
OptionParseContext::cur_group
OptionGroup cur_group
Definition: cmdutils.h:241
SpecifierOpt::f
float f
Definition: cmdutils.h:101
swscale.h
OptionDef::func_arg
int(* func_arg)(void *, const char *, const char *)
Definition: cmdutils.h:130
show_help_options
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
Print help for all options matching specified flags.
Definition: cmdutils.c:109
program_birth_year
const int program_birth_year
program birth year, defined by the program for show_banner()
Definition: ffmpeg.c:105
min
float min
Definition: vorbis_enc_data.h:429
OptionDef::flags
int flags
Definition: cmdutils.h:108