FFmpegKit Linux API  4.5.1
fftools_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 /*
23  * CHANGES 01.2020
24  * - ffprobe support changes
25  * - AV_LOG_STDERR introduced
26  *
27  * CHANGES 12.2019
28  * - Concurrent execution support
29  *
30  * CHANGES 03.2019
31  * --------------------------------------------------------
32  * - config.h include removed
33  *
34  * CHANGES 08.2018
35  * --------------------------------------------------------
36  * - fftools_ prefix added to file name and include guards
37  *
38  * CHANGES 07.2018
39  * --------------------------------------------------------
40  * - Include guards renamed
41  * - Unused headers removed
42  */
43 
44 #ifndef FFTOOLS_CMDUTILS_H
45 #define FFTOOLS_CMDUTILS_H
46 
47 #include <stdint.h>
48 
49 #include "libavcodec/avcodec.h"
50 #include "libavfilter/avfilter.h"
51 #include "libavformat/avformat.h"
52 #include "libswscale/swscale.h"
53 
54 #ifdef _WIN32
55 #undef main /* We don't want SDL to override our main() */
56 #endif
57 
61 #define AV_LOG_STDERR -16
62 
66 extern __thread char *program_name;
67 
71 extern __thread int program_birth_year;
72 
73 extern __thread AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
74 extern __thread AVFormatContext *avformat_opts;
75 extern __thread AVDictionary *sws_dict;
76 extern __thread AVDictionary *swr_opts;
77 extern __thread AVDictionary *format_opts, *codec_opts, *resample_opts;
78 extern __thread int hide_banner;
79 extern __thread int find_stream_info;
80 
84 void register_exit(void (*cb)(int ret));
85 
89 void exit_program(int ret) av_noreturn;
90 
94 void init_dynload(void);
95 
100 void init_opts(void);
105 void uninit_opts(void);
106 
111 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
112 
116 int opt_cpuflags(void *optctx, const char *opt, const char *arg);
117 
121 int opt_cpucount(void *optctx, const char *opt, const char *arg);
122 
127 int opt_default(void *optctx, const char *opt, const char *arg);
128 
132 int opt_loglevel(void *optctx, const char *opt, const char *arg);
133 
134 int opt_report(void *optctx, const char *opt, const char *arg);
135 
136 int opt_max_alloc(void *optctx, const char *opt, const char *arg);
137 
138 int opt_codec_debug(void *optctx, const char *opt, const char *arg);
139 
143 int opt_timelimit(void *optctx, const char *opt, const char *arg);
144 
158 double parse_number_or_die(const char *context, const char *numstr, int type,
159  double min, double max);
160 
175 int64_t parse_time_or_die(const char *context, const char *timestr,
176  int is_duration);
177 
178 typedef struct SpecifierOpt {
179  char *specifier;
180  union {
181  uint8_t *str;
182  int i;
183  int64_t i64;
184  uint64_t ui64;
185  float f;
186  double dbl;
187  } u;
188 } SpecifierOpt;
189 
190 typedef struct OptionDef {
191  const char *name;
192  int flags;
193 #define HAS_ARG 0x0001
194 #define OPT_BOOL 0x0002
195 #define OPT_EXPERT 0x0004
196 #define OPT_STRING 0x0008
197 #define OPT_VIDEO 0x0010
198 #define OPT_AUDIO 0x0020
199 #define OPT_INT 0x0080
200 #define OPT_FLOAT 0x0100
201 #define OPT_SUBTITLE 0x0200
202 #define OPT_INT64 0x0400
203 #define OPT_EXIT 0x0800
204 #define OPT_DATA 0x1000
205 #define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).
206  implied by OPT_OFFSET or OPT_SPEC */
207 #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
208 #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
209  Implies OPT_OFFSET. Next element after the offset is
210  an int containing element count in the array. */
211 #define OPT_TIME 0x10000
212 #define OPT_DOUBLE 0x20000
213 #define OPT_INPUT 0x40000
214 #define OPT_OUTPUT 0x80000
215  union {
216  void *dst_ptr;
217  int (*func_arg)(void *, const char *, const char *);
218  size_t off;
219  } u;
220  const char *help;
221  const char *argname;
222 } OptionDef;
223 
233 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
234  int rej_flags, int alt_flags);
235 
240 void show_help_children(const AVClass *avClass, int flags);
241 
246 void show_help_default_ffmpeg(const char *opt, const char *arg);
247 void show_help_default_ffprobe(const char *opt, const char *arg);
248 
252 int show_help(void *optctx, const char *opt, const char *arg);
253 
266 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
267  void (* parse_arg_function)(void *optctx, const char*));
268 
274 int parse_option(void *optctx, const char *opt, const char *arg,
275  const OptionDef *options);
276 
282 typedef struct Option {
283  const OptionDef *opt;
284  const char *key;
285  const char *val;
286 } Option;
287 
288 typedef struct OptionGroupDef {
290  const char *name;
295  const char *sep;
300  int flags;
302 
303 typedef struct OptionGroup {
305  const char *arg;
306 
308  int nb_opts;
309 
310  AVDictionary *codec_opts;
311  AVDictionary *format_opts;
312  AVDictionary *resample_opts;
313  AVDictionary *sws_dict;
314  AVDictionary *swr_opts;
315 } OptionGroup;
316 
321 typedef struct OptionGroupList {
323 
327 
328 typedef struct OptionParseContext {
330 
333 
334  /* parsing state */
337 
344 int parse_optgroup(void *optctx, OptionGroup *g);
345 
364 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
365  const OptionDef *options,
366  const OptionGroupDef *groups, int nb_groups);
367 
372 
376 void parse_loglevel(int argc, char **argv, const OptionDef *options);
377 
381 int locate_option(int argc, char **argv, const OptionDef *options,
382  const char *optname);
383 
393 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
394 
409 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
410  AVFormatContext *s, AVStream *st, const AVCodec *codec);
411 
423 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
424  AVDictionary *codec_opts);
425 
435 void print_error(const char *filename, int err);
436 
442 void show_banner(int argc, char **argv, const OptionDef *options);
443 
450 int show_version(void *optctx, const char *opt, const char *arg);
451 
457 int show_buildconf(void *optctx, const char *opt, const char *arg);
458 
464 int show_license(void *optctx, const char *opt, const char *arg);
465 
471 int show_formats(void *optctx, const char *opt, const char *arg);
472 
478 int show_muxers(void *optctx, const char *opt, const char *arg);
479 
485 int show_demuxers(void *optctx, const char *opt, const char *arg);
486 
492 int show_devices(void *optctx, const char *opt, const char *arg);
493 
494 #if CONFIG_AVDEVICE
495 
499 int show_sinks(void *optctx, const char *opt, const char *arg);
500 
505 int show_sources(void *optctx, const char *opt, const char *arg);
506 #endif
507 
513 int show_codecs(void *optctx, const char *opt, const char *arg);
514 
519 int show_decoders(void *optctx, const char *opt, const char *arg);
520 
525 int show_encoders(void *optctx, const char *opt, const char *arg);
526 
532 int show_filters(void *optctx, const char *opt, const char *arg);
533 
539 int show_bsfs(void *optctx, const char *opt, const char *arg);
540 
546 int show_protocols(void *optctx, const char *opt, const char *arg);
547 
553 int show_pix_fmts(void *optctx, const char *opt, const char *arg);
554 
560 int show_layouts(void *optctx, const char *opt, const char *arg);
561 
566 int show_sample_fmts(void *optctx, const char *opt, const char *arg);
567 
572 int show_colors(void *optctx, const char *opt, const char *arg);
573 
578 int read_yesno(void);
579 
598 FILE *get_preset_file(char *filename, size_t filename_size,
599  const char *preset_name, int is_path, const char *codec_name);
600 
611 void *grow_array(void *array, int elem_size, int *size, int new_size);
612 
613 #define media_type_string av_get_media_type_string
614 
615 #define GROW_ARRAY(array, nb_elems)\
616  array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
617 
618 #define GET_PIX_FMT_NAME(pix_fmt)\
619  const char *name = av_get_pix_fmt_name(pix_fmt);
620 
621 #define GET_CODEC_NAME(id)\
622  const char *name = avcodec_descriptor_get(id)->name;
623 
624 #define GET_SAMPLE_FMT_NAME(sample_fmt)\
625  const char *name = av_get_sample_fmt_name(sample_fmt)
626 
627 #define GET_SAMPLE_RATE_NAME(rate)\
628  char name[16];\
629  snprintf(name, sizeof(name), "%d", rate);
630 
631 #define GET_CH_LAYOUT_NAME(ch_layout)\
632  char name[16];\
633  snprintf(name, sizeof(name), "0x%" PRIx64, ch_layout);
634 
635 #define GET_CH_LAYOUT_DESC(ch_layout)\
636  char name[128];\
637  av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
638 
639 double get_rotation(AVStream *st);
640 
641 #endif /* FFTOOLS_CMDUTILS_H */
init_opts
void init_opts(void)
OptionGroup
struct OptionGroup OptionGroup
OptionGroupList::nb_groups
int nb_groups
Definition: fftools_cmdutils.h:322
show_encoders
int show_encoders(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1697
show_buildconf
int show_buildconf(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1250
SpecifierOpt
struct SpecifierOpt SpecifierOpt
show_demuxers
int show_demuxers(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1411
program_name
__thread char * program_name
Definition: fftools_cmdutils.c:97
OptionDef::flags
int flags
Definition: fftools_cmdutils.h:192
parse_optgroup
int parse_optgroup(void *optctx, OptionGroup *g)
Definition: fftools_cmdutils.c:444
show_help_default_ffmpeg
void show_help_default_ffmpeg(const char *opt, const char *arg)
Definition: fftools_ffmpeg_opt.c:3296
Option::val
const char * val
Definition: fftools_cmdutils.h:282
parse_number_or_die
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
Definition: fftools_cmdutils.c:171
show_protocols
int show_protocols(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1715
opt_timelimit
int opt_timelimit(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1121
avformat_opts
__thread AVFormatContext * avformat_opts
show_help_default_ffprobe
void show_help_default_ffprobe(const char *opt, const char *arg)
Definition: fftools_ffprobe.c:3422
Option
Definition: fftools_cmdutils.h:279
OptionDef::name
const char * name
Definition: fftools_cmdutils.h:191
filter_codec_opts
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, const AVCodec *codec)
Definition: fftools_cmdutils.c:2156
avcodec_opts
__thread AVCodecContext * avcodec_opts[AVMEDIA_TYPE_NB]
grow_array
void * grow_array(void *array, int elem_size, int *size, int new_size)
Definition: fftools_cmdutils.c:2234
opt_max_alloc
int opt_max_alloc(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1107
show_muxers
int show_muxers(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1406
parse_options
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *optctx, const char *))
show_decoders
int show_decoders(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1691
init_dynload
void init_dynload(void)
Definition: fftools_cmdutils.c:144
find_stream_info
__thread int find_stream_info
Definition: fftools_ffmpeg_opt.c:203
opt_cpucount
int opt_cpucount(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:893
get_preset_file
FILE * get_preset_file(char *filename, size_t filename_size, const char *preset_name, int is_path, const char *codec_name)
Definition: fftools_cmdutils.c:2098
OptionGroup::codec_opts
AVDictionary * codec_opts
Definition: fftools_cmdutils.h:307
OptionDef::argname
const char * argname
Definition: fftools_cmdutils.h:218
show_help
int show_help(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:2042
SpecifierOpt::i64
int64_t i64
Definition: fftools_cmdutils.h:183
sws_dict
__thread AVDictionary * sws_dict
Definition: fftools_cmdutils.c:100
OptionGroupList
Definition: fftools_cmdutils.h:318
parse_option
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
Definition: fftools_cmdutils.c:376
OptionGroup::swr_opts
AVDictionary * swr_opts
Definition: fftools_cmdutils.h:311
opt_report
int opt_report(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1102
OptionGroup::sws_dict
AVDictionary * sws_dict
Definition: fftools_cmdutils.h:310
SpecifierOpt::dbl
double dbl
Definition: fftools_cmdutils.h:186
opt_codec_debug
int opt_codec_debug(void *optctx, const char *opt, const char *arg)
OptionDef::off
size_t off
Definition: fftools_cmdutils.h:215
OptionGroup::opts
Option * opts
Definition: fftools_cmdutils.h:304
parse_time_or_die
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
Definition: fftools_cmdutils.c:192
program_birth_year
__thread int program_birth_year
Definition: fftools_cmdutils.c:98
SpecifierOpt::i
int i
Definition: fftools_cmdutils.h:182
log_callback_help
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
swr_opts
__thread AVDictionary * swr_opts
Definition: fftools_cmdutils.c:101
OptionParseContext::nb_groups
int nb_groups
Definition: fftools_cmdutils.h:329
show_help_children
void show_help_children(const AVClass *avClass, int flags)
Definition: fftools_cmdutils.c:233
show_codecs
int show_codecs(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1596
OptionGroupDef::sep
const char * sep
Definition: fftools_cmdutils.h:292
OptionGroupList::groups
OptionGroup * groups
Definition: fftools_cmdutils.h:321
show_version
int show_version(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1242
show_sample_fmts
int show_sample_fmts(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1853
OptionGroup::arg
const char * arg
Definition: fftools_cmdutils.h:302
show_filters
int show_filters(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1730
OptionDef::u
union OptionDef::@1 u
uninit_opts
void uninit_opts(void)
Definition: fftools_cmdutils.c:115
parse_loglevel
void parse_loglevel(int argc, char **argv, const OptionDef *options)
Definition: fftools_cmdutils.c:536
SpecifierOpt
Definition: fftools_cmdutils.h:178
opt_loglevel
int opt_loglevel(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:919
OptionParseContext
Definition: fftools_cmdutils.h:325
split_commandline
int split_commandline(OptionParseContext *octx, int argc, char *argv[], const OptionDef *options, const OptionGroupDef *groups, int nb_groups)
Definition: fftools_cmdutils.c:770
uninit_parse_context
void uninit_parse_context(OptionParseContext *octx)
Definition: fftools_cmdutils.c:744
locate_option
int locate_option(int argc, char **argv, const OptionDef *options, const char *optname)
Definition: fftools_cmdutils.c:477
OptionGroupDef
Definition: fftools_cmdutils.h:285
read_yesno
int read_yesno(void)
Definition: fftools_cmdutils.c:2087
OptionParseContext::groups
OptionGroupList * groups
Definition: fftools_cmdutils.h:328
Option::opt
const OptionDef * opt
Definition: fftools_cmdutils.h:280
hide_banner
__thread int hide_banner
Definition: fftools_cmdutils.c:106
show_colors
int show_colors(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1777
OptionGroup::nb_opts
int nb_opts
Definition: fftools_cmdutils.h:305
int
int
Definition: fftools_ffmpeg_filter.c:165
SpecifierOpt::f
float f
Definition: fftools_cmdutils.h:185
format_opts
__thread AVDictionary * format_opts
Definition: fftools_cmdutils.c:102
show_bsfs
int show_bsfs(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1703
get_rotation
double get_rotation(AVStream *st)
Definition: fftools_cmdutils.c:2253
opt_cpuflags
int opt_cpuflags(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:881
show_pix_fmts
int show_pix_fmts(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1791
OptionDef
struct OptionDef OptionDef
OptionGroup::group_def
const OptionGroupDef * group_def
Definition: fftools_cmdutils.h:301
OptionDef
Definition: fftools_cmdutils.h:190
SpecifierOpt::specifier
char * specifier
Definition: fftools_cmdutils.h:179
resample_opts
__thread AVDictionary * resample_opts
Definition: fftools_cmdutils.h:77
OptionGroupDef::name
const char * name
Definition: fftools_cmdutils.h:287
OptionDef::func_arg
int(* func_arg)(void *, const char *, const char *)
Definition: fftools_cmdutils.h:214
show_license
int show_license(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1257
OptionGroupList::group_def
const OptionGroupDef * group_def
Definition: fftools_cmdutils.h:319
OptionGroup::resample_opts
AVDictionary * resample_opts
Definition: fftools_cmdutils.h:309
Option::key
const char * key
Definition: fftools_cmdutils.h:281
register_exit
void register_exit(void(*cb)(int ret))
Definition: fftools_cmdutils.c:155
OptionGroup::format_opts
AVDictionary * format_opts
Definition: fftools_cmdutils.h:308
SpecifierOpt::u
union SpecifierOpt::@0 u
SpecifierOpt::str
uint8_t * str
Definition: fftools_cmdutils.h:181
OptionGroupList
struct OptionGroupList OptionGroupList
OptionDef::dst_ptr
void * dst_ptr
Definition: fftools_cmdutils.h:213
show_layouts
int show_layouts(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1824
show_banner
void show_banner(int argc, char **argv, const OptionDef *options)
Definition: fftools_cmdutils.c:1231
OptionParseContext::cur_group
OptionGroup cur_group
Definition: fftools_cmdutils.h:332
codec_opts
__thread AVDictionary * codec_opts
Definition: fftools_cmdutils.h:77
Option
struct Option Option
OptionParseContext::global_opts
OptionGroup global_opts
Definition: fftools_cmdutils.h:326
OptionGroupDef::flags
int flags
Definition: fftools_cmdutils.h:297
OptionGroupDef
struct OptionGroupDef OptionGroupDef
OptionDef::help
const char * help
Definition: fftools_cmdutils.h:217
OptionGroup
Definition: fftools_cmdutils.h:300
opt_default
int opt_default(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:575
OptionParseContext
struct OptionParseContext OptionParseContext
show_formats
int show_formats(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1401
check_stream_specifier
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Definition: fftools_cmdutils.c:2148
exit_program
void exit_program(int ret) av_noreturn
Definition: fftools_cmdutils.c:160
show_help_options
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
Definition: fftools_cmdutils.c:204
setup_find_stream_info_opts
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
Definition: fftools_cmdutils.c:2214
SpecifierOpt::ui64
uint64_t ui64
Definition: fftools_cmdutils.h:184
show_devices
int show_devices(void *optctx, const char *opt, const char *arg)
Definition: fftools_cmdutils.c:1416
print_error
void print_error(const char *filename, int err)
Definition: fftools_cmdutils.c:1134