FFmpeg
cmdutils.h
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 
40 extern const char program_name[];
41 
45 extern const int program_birth_year;
46 
47 extern AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
48 extern AVFormatContext *avformat_opts;
49 extern AVDictionary *sws_dict;
50 extern AVDictionary *swr_opts;
51 extern AVDictionary *format_opts, *codec_opts, *resample_opts;
52 extern int hide_banner;
53 
57 void register_exit(void (*cb)(int ret));
58 
62 void exit_program(int ret) av_noreturn;
63 
67 void init_dynload(void);
68 
73 void init_opts(void);
78 void uninit_opts(void);
79 
84 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
85 
89 int opt_cpuflags(void *optctx, const char *opt, const char *arg);
90 
95 int opt_default(void *optctx, const char *opt, const char *arg);
96 
100 int opt_loglevel(void *optctx, const char *opt, const char *arg);
101 
102 int opt_report(const char *opt);
103 
104 int opt_max_alloc(void *optctx, const char *opt, const char *arg);
105 
106 int opt_codec_debug(void *optctx, const char *opt, const char *arg);
107 
111 int opt_timelimit(void *optctx, const char *opt, const char *arg);
112 
126 double parse_number_or_die(const char *context, const char *numstr, int type,
127  double min, double max);
128 
143 int64_t parse_time_or_die(const char *context, const char *timestr,
144  int is_duration);
145 
146 typedef struct SpecifierOpt {
147  char *specifier;
148  union {
149  uint8_t *str;
150  int i;
151  int64_t i64;
152  uint64_t ui64;
153  float f;
154  double dbl;
155  } u;
156 } SpecifierOpt;
157 
158 typedef struct OptionDef {
159  const char *name;
160  int flags;
161 #define HAS_ARG 0x0001
162 #define OPT_BOOL 0x0002
163 #define OPT_EXPERT 0x0004
164 #define OPT_STRING 0x0008
165 #define OPT_VIDEO 0x0010
166 #define OPT_AUDIO 0x0020
167 #define OPT_INT 0x0080
168 #define OPT_FLOAT 0x0100
169 #define OPT_SUBTITLE 0x0200
170 #define OPT_INT64 0x0400
171 #define OPT_EXIT 0x0800
172 #define OPT_DATA 0x1000
173 #define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).
174  implied by OPT_OFFSET or OPT_SPEC */
175 #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
176 #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
177  Implies OPT_OFFSET. Next element after the offset is
178  an int containing element count in the array. */
179 #define OPT_TIME 0x10000
180 #define OPT_DOUBLE 0x20000
181 #define OPT_INPUT 0x40000
182 #define OPT_OUTPUT 0x80000
183  union {
184  void *dst_ptr;
185  int (*func_arg)(void *, const char *, const char *);
186  size_t off;
187  } u;
188  const char *help;
189  const char *argname;
190 } OptionDef;
191 
201 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
202  int rej_flags, int alt_flags);
203 
204 #if CONFIG_AVDEVICE
205 #define CMDUTILS_COMMON_OPTIONS_AVDEVICE \
206  { "sources" , OPT_EXIT | HAS_ARG, { .func_arg = show_sources }, \
207  "list sources of the input device", "device" }, \
208  { "sinks" , OPT_EXIT | HAS_ARG, { .func_arg = show_sinks }, \
209  "list sinks of the output device", "device" }, \
210 
211 #else
212 #define CMDUTILS_COMMON_OPTIONS_AVDEVICE
213 #endif
214 
215 #define CMDUTILS_COMMON_OPTIONS \
216  { "L", OPT_EXIT, { .func_arg = show_license }, "show license" }, \
217  { "h", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
218  { "?", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
219  { "help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
220  { "-help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
221  { "version", OPT_EXIT, { .func_arg = show_version }, "show version" }, \
222  { "buildconf", OPT_EXIT, { .func_arg = show_buildconf }, "show build configuration" }, \
223  { "formats", OPT_EXIT, { .func_arg = show_formats }, "show available formats" }, \
224  { "muxers", OPT_EXIT, { .func_arg = show_muxers }, "show available muxers" }, \
225  { "demuxers", OPT_EXIT, { .func_arg = show_demuxers }, "show available demuxers" }, \
226  { "devices", OPT_EXIT, { .func_arg = show_devices }, "show available devices" }, \
227  { "codecs", OPT_EXIT, { .func_arg = show_codecs }, "show available codecs" }, \
228  { "decoders", OPT_EXIT, { .func_arg = show_decoders }, "show available decoders" }, \
229  { "encoders", OPT_EXIT, { .func_arg = show_encoders }, "show available encoders" }, \
230  { "bsfs", OPT_EXIT, { .func_arg = show_bsfs }, "show available bit stream filters" }, \
231  { "protocols", OPT_EXIT, { .func_arg = show_protocols }, "show available protocols" }, \
232  { "filters", OPT_EXIT, { .func_arg = show_filters }, "show available filters" }, \
233  { "pix_fmts", OPT_EXIT, { .func_arg = show_pix_fmts }, "show available pixel formats" }, \
234  { "layouts", OPT_EXIT, { .func_arg = show_layouts }, "show standard channel layouts" }, \
235  { "sample_fmts", OPT_EXIT, { .func_arg = show_sample_fmts }, "show available audio sample formats" }, \
236  { "colors", OPT_EXIT, { .func_arg = show_colors }, "show available color names" }, \
237  { "loglevel", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" }, \
238  { "v", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" }, \
239  { "report", 0, { (void*)opt_report }, "generate a report" }, \
240  { "max_alloc", HAS_ARG, { .func_arg = opt_max_alloc }, "set maximum size of a single allocated block", "bytes" }, \
241  { "cpuflags", HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "force specific cpu flags", "flags" }, \
242  { "hide_banner", OPT_BOOL | OPT_EXPERT, {&hide_banner}, "do not show program banner", "hide_banner" }, \
243  CMDUTILS_COMMON_OPTIONS_AVDEVICE \
244 
245 
249 void show_help_children(const AVClass *class, int flags);
250 
255 void show_help_default(const char *opt, const char *arg);
256 
260 int show_help(void *optctx, const char *opt, const char *arg);
261 
274 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
275  void (* parse_arg_function)(void *optctx, const char*));
276 
282 int parse_option(void *optctx, const char *opt, const char *arg,
283  const OptionDef *options);
284 
290 typedef struct Option {
291  const OptionDef *opt;
292  const char *key;
293  const char *val;
294 } Option;
295 
296 typedef struct OptionGroupDef {
298  const char *name;
303  const char *sep;
308  int flags;
310 
311 typedef struct OptionGroup {
312  const OptionGroupDef *group_def;
313  const char *arg;
314 
315  Option *opts;
316  int nb_opts;
317 
318  AVDictionary *codec_opts;
319  AVDictionary *format_opts;
320  AVDictionary *resample_opts;
321  AVDictionary *sws_dict;
322  AVDictionary *swr_opts;
323 } OptionGroup;
324 
329 typedef struct OptionGroupList {
330  const OptionGroupDef *group_def;
331 
332  OptionGroup *groups;
333  int nb_groups;
335 
336 typedef struct OptionParseContext {
337  OptionGroup global_opts;
338 
339  OptionGroupList *groups;
340  int nb_groups;
341 
342  /* parsing state */
343  OptionGroup cur_group;
345 
351 int parse_optgroup(void *optctx, OptionGroup *g);
352 
371 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
372  const OptionDef *options,
373  const OptionGroupDef *groups, int nb_groups);
374 
378 void uninit_parse_context(OptionParseContext *octx);
379 
383 void parse_loglevel(int argc, char **argv, const OptionDef *options);
384 
388 int locate_option(int argc, char **argv, const OptionDef *options,
389  const char *optname);
390 
400 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
401 
416 AVDictionary *filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id,
417  AVFormatContext *s, AVStream *st, AVCodec *codec);
418 
430 AVDictionary **setup_find_stream_info_opts(AVFormatContext *s,
431  AVDictionary *codec_opts);
432 
442 void print_error(const char *filename, int err);
443 
449 void show_banner(int argc, char **argv, const OptionDef *options);
450 
457 int show_version(void *optctx, const char *opt, const char *arg);
458 
464 int show_buildconf(void *optctx, const char *opt, const char *arg);
465 
471 int show_license(void *optctx, const char *opt, const char *arg);
472 
478 int show_formats(void *optctx, const char *opt, const char *arg);
479 
485 int show_muxers(void *optctx, const char *opt, const char *arg);
486 
492 int show_demuxers(void *optctx, const char *opt, const char *arg);
493 
499 int show_devices(void *optctx, const char *opt, const char *arg);
500 
501 #if CONFIG_AVDEVICE
502 
506 int show_sinks(void *optctx, const char *opt, const char *arg);
507 
512 int show_sources(void *optctx, const char *opt, const char *arg);
513 #endif
514 
520 int show_codecs(void *optctx, const char *opt, const char *arg);
521 
526 int show_decoders(void *optctx, const char *opt, const char *arg);
527 
532 int show_encoders(void *optctx, const char *opt, const char *arg);
533 
539 int show_filters(void *optctx, const char *opt, const char *arg);
540 
546 int show_bsfs(void *optctx, const char *opt, const char *arg);
547 
553 int show_protocols(void *optctx, const char *opt, const char *arg);
554 
560 int show_pix_fmts(void *optctx, const char *opt, const char *arg);
561 
567 int show_layouts(void *optctx, const char *opt, const char *arg);
568 
573 int show_sample_fmts(void *optctx, const char *opt, const char *arg);
574 
579 int show_colors(void *optctx, const char *opt, const char *arg);
580 
585 int read_yesno(void);
586 
605 FILE *get_preset_file(char *filename, size_t filename_size,
606  const char *preset_name, int is_path, const char *codec_name);
607 
618 void *grow_array(void *array, int elem_size, int *size, int new_size);
619 
620 #define media_type_string av_get_media_type_string
621 
622 #define GROW_ARRAY(array, nb_elems)\
623  array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
624 
625 #define GET_PIX_FMT_NAME(pix_fmt)\
626  const char *name = av_get_pix_fmt_name(pix_fmt);
627 
628 #define GET_CODEC_NAME(id)\
629  const char *name = avcodec_descriptor_get(id)->name;
630 
631 #define GET_SAMPLE_FMT_NAME(sample_fmt)\
632  const char *name = av_get_sample_fmt_name(sample_fmt)
633 
634 #define GET_SAMPLE_RATE_NAME(rate)\
635  char name[16];\
636  snprintf(name, sizeof(name), "%d", rate);
637 
638 #define GET_CH_LAYOUT_NAME(ch_layout)\
639  char name[16];\
640  snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
641 
642 #define GET_CH_LAYOUT_DESC(ch_layout)\
643  char name[128];\
644  av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
645 
646 double get_rotation(AVStream *st);
647 
648 #endif /* FFTOOLS_CMDUTILS_H */
const char * name
< group name
Definition: cmdutils.h:298
Main libavfilter public API header.
Definition: cmdutils.h:158
const char * sep
Option to be used as group separator.
Definition: cmdutils.h:303
Definition: cmdutils.h:311
Definition: cmdutils.h:146
AVCodec.
Definition: avcodec.h:3408
Format I/O context.
Definition: avformat.h:1342
Definition: cmdutils.h:296
external API header
int flags
Option flags that must be set on each option that is applied to this group.
Definition: cmdutils.h:308
Definition: dict.c:30
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
Definition: cmdutils.h:336
A list of option groups that all have the same group type (e.g.
Definition: cmdutils.h:329
char * specifier
stream/chapter/program/...
Definition: cmdutils.h:147
const char program_name[]
program name, defined by the program for show_version().
Definition: ffmpeg.c:109
void show_help_default(const char *opt, const char *arg)
Per-fftool specific help handler.
Definition: ffplay.c:3608
Stream structure.
Definition: avformat.h:873
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1518
Describe the class of an AVClass context structure.
Definition: log.h:67
An option extracted from the commandline.
Definition: cmdutils.h:290
Main libavformat public API header.
const int program_birth_year
program birth year, defined by the program for show_banner()
Definition: ffmpeg.c:110