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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef LIBAV_CMDUTILS_H
23 #define LIBAV_CMDUTILS_H
24 
25 #include <stdint.h>
26 
27 #include "libavcodec/avcodec.h"
28 #include "libavfilter/avfilter.h"
29 #include "libavformat/avformat.h"
30 #include "libswscale/swscale.h"
31 
35 extern const char program_name[];
36 
40 extern const int program_birth_year;
41 
44 extern struct SwsContext *sws_opts;
46 
51 void init_opts(void);
56 void uninit_opts(void);
57 
62 void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
63 
68 int opt_default(void *optctx, const char *opt, const char *arg);
69 
73 int opt_loglevel(void *optctx, const char *opt, const char *arg);
74 
78 int opt_timelimit(void *optctx, const char *opt, const char *arg);
79 
93 double parse_number_or_die(const char *context, const char *numstr, int type,
94  double min, double max);
95 
110 int64_t parse_time_or_die(const char *context, const char *timestr,
111  int is_duration);
112 
113 typedef struct SpecifierOpt {
114  char *specifier;
115  union {
117  int i;
118  int64_t i64;
119  float f;
120  double dbl;
121  } u;
122 } SpecifierOpt;
123 
124 typedef struct OptionDef {
125  const char *name;
126  int flags;
127 #define HAS_ARG 0x0001
128 #define OPT_BOOL 0x0002
129 #define OPT_EXPERT 0x0004
130 #define OPT_STRING 0x0008
131 #define OPT_VIDEO 0x0010
132 #define OPT_AUDIO 0x0020
133 #define OPT_INT 0x0080
134 #define OPT_FLOAT 0x0100
135 #define OPT_SUBTITLE 0x0200
136 #define OPT_INT64 0x0400
137 #define OPT_EXIT 0x0800
138 #define OPT_DATA 0x1000
139 #define OPT_PERFILE 0x2000 /* the option is per-file (currently avconv-only).
140  implied by OPT_OFFSET or OPT_SPEC */
141 #define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
142 #define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
143  Implies OPT_OFFSET. Next element after the offset is
144  an int containing element count in the array. */
145 #define OPT_TIME 0x10000
146 #define OPT_DOUBLE 0x20000
147  union {
148  void *dst_ptr;
149  int (*func_arg)(void *, const char *, const char *);
150  size_t off;
151  } u;
152  const char *help;
153  const char *argname;
154 } OptionDef;
155 
165 void show_help_options(const OptionDef *options, const char *msg, int req_flags,
166  int rej_flags, int alt_flags);
167 
172 void show_help_children(const AVClass *class, int flags);
173 
178 void show_help_default(const char *opt, const char *arg);
179 
183 int show_help(void *optctx, const char *opt, const char *arg);
184 
197 void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
198  void (* parse_arg_function)(void *optctx, const char*));
199 
205 int parse_option(void *optctx, const char *opt, const char *arg,
206  const OptionDef *options);
207 
213 typedef struct Option {
214  const OptionDef *opt;
215  const char *key;
216  const char *val;
217 } Option;
218 
219 typedef struct OptionGroupDef {
221  const char *name;
226  const char *sep;
228 
229 typedef struct OptionGroup {
231  const char *arg;
232 
234  int nb_opts;
235 
239 } OptionGroup;
240 
245 typedef struct OptionGroupList {
247 
251 
252 typedef struct OptionParseContext {
254 
257 
258  /* parsing state */
261 
267 int parse_optgroup(void *optctx, OptionGroup *g);
268 
287 int split_commandline(OptionParseContext *octx, int argc, char *argv[],
288  const OptionDef *options,
289  const OptionGroupDef *groups, int nb_groups);
290 
295 
299 void parse_loglevel(int argc, char **argv, const OptionDef *options);
300 
304 int locate_option(int argc, char **argv, const OptionDef *options,
305  const char *optname);
306 
316 int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
317 
333  AVFormatContext *s, AVStream *st, AVCodec *codec);
334 
347  AVDictionary *codec_opts);
348 
358 void print_error(const char *filename, int err);
359 
365 void show_banner(void);
366 
372 int show_version(void *optctx, const char *opt, const char *arg);
373 
378 int show_license(void *optctx, const char *opt, const char *arg);
379 
384 int show_formats(void *optctx, const char *opt, const char *arg);
385 
390 int show_codecs(void *optctx, const char *opt, const char *arg);
391 
396 int show_decoders(void *optctx, const char *opt, const char *arg);
397 
402 int show_encoders(void *optctx, const char *opt, const char *arg);
403 
408 int show_filters(void *optctx, const char *opt, const char *arg);
409 
414 int show_bsfs(void *optctx, const char *opt, const char *arg);
415 
420 int show_protocols(void *optctx, const char *opt, const char *arg);
421 
426 int show_pix_fmts(void *optctx, const char *opt, const char *arg);
427 
432 int show_sample_fmts(void *optctx, const char *opt, const char *arg);
433 
438 int read_yesno(void);
439 
450 int cmdutils_read_file(const char *filename, char **bufptr, size_t *size);
451 
452 typedef struct PtsCorrectionContext {
453  int64_t num_faulty_pts;
454  int64_t num_faulty_dts;
455  int64_t last_pts;
456  int64_t last_dts;
458 
463 
475 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts);
476 
494 FILE *get_preset_file(char *filename, size_t filename_size,
495  const char *preset_name, int is_path, const char *codec_name);
496 
507 void *grow_array(void *array, int elem_size, int *size, int new_size);
508 
509 #define GROW_ARRAY(array, nb_elems)\
510  array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
511 
512 typedef struct FrameBuffer {
515  int linesize[4];
516 
517  int h, w;
519 
520  int refcount;
521  struct FrameBuffer **pool;
522  struct FrameBuffer *next;
523 } FrameBuffer;
524 
534 int codec_get_buffer(AVCodecContext *s, AVFrame *frame);
535 
541 
548 
554 
555 #define GET_PIX_FMT_NAME(pix_fmt)\
556  const char *name = av_get_pix_fmt_name(pix_fmt);
557 
558 #define GET_SAMPLE_FMT_NAME(sample_fmt)\
559  const char *name = av_get_sample_fmt_name(sample_fmt)
560 
561 #define GET_SAMPLE_RATE_NAME(rate)\
562  char name[16];\
563  snprintf(name, sizeof(name), "%d", rate);
564 
565 #define GET_CH_LAYOUT_NAME(ch_layout)\
566  char name[16];\
567  snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
568 
569 #define GET_CH_LAYOUT_DESC(ch_layout)\
570  char name[128];\
571  av_get_channel_layout_string(name, sizeof(name), 0, ch_layout);
572 
573 #endif /* LIBAV_CMDUTILS_H */
int64_t num_faulty_dts
Number of incorrect PTS values so far.
Definition: cmdutils.h:454
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:366
int show_bsfs(void *optctx, const char *opt, const char *arg)
Print a listing containing all the bit stream filters supported by the program.
Definition: cmdutils.c:1085
struct OptionParseContext OptionParseContext
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int show_pix_fmts(void *optctx, const char *opt, const char *arg)
Print a listing containing all the pixel formats supported by the program.
Definition: cmdutils.c:1123
const char * name
< group name
Definition: cmdutils.h:221
int64_t num_faulty_pts
Definition: cmdutils.h:453
int parse_optgroup(void *optctx, OptionGroup *g)
Parse an options group and write results into optctx.
Definition: cmdutils.c:343
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the given stream matches a stream specifier.
Definition: cmdutils.c:1388
void uninit_parse_context(OptionParseContext *octx)
Free all allocated memory in an OptionParseContext.
Definition: cmdutils.c:531
int(* func_arg)(void *, const char *, const char *)
Definition: cmdutils.h:149
double dbl
Definition: cmdutils.h:120
uint8_t * data[4]
Definition: cmdutils.h:514
struct OptionDef OptionDef
const char * arg
Definition: cmdutils.h:231
AVCodecContext * avcodec_opts[AVMEDIA_TYPE_NB]
const char * sep
Option to be used as group separator.
Definition: cmdutils.h:226
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:62
int64_t last_pts
Number of incorrect DTS values so far.
Definition: cmdutils.h:455
AVCodec.
Definition: avcodec.h:2960
int show_decoders(void *optctx, const char *opt, const char *arg)
Print a listing containing all the decoders supported by the program.
Definition: cmdutils.c:1073
Format I/O context.
Definition: avformat.h:828
AVDictionary * format_opts
Definition: cmdutils.c:57
struct FrameBuffer * next
Definition: cmdutils.h:522
uint8_t
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
Parse a string and return its corresponding value as a double.
Definition: cmdutils.c:84
int opt_timelimit(void *optctx, const char *opt, const char *arg)
Limit the execution time.
Definition: cmdutils.c:692
int show_protocols(void *optctx, const char *opt, const char *arg)
Print a listing containing all the protocols supported by the program.
Definition: cmdutils.c:1096
int nb_opts
Definition: cmdutils.h:234
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:392
struct OptionGroupDef OptionGroupDef
int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
Get a frame from the pool.
Definition: cmdutils.c:1597
static int flags
Definition: log.c:42
external api for the swscale stuff
void uninit_opts(void)
Uninitialize the cmdutils option system, in particular free the *_opts contexts and their contents...
Definition: cmdutils.c:69
uint8_t * base[4]
Definition: cmdutils.h:513
int show_license(void *optctx, const char *opt, const char *arg)
Print the license of the program to stdout.
Definition: cmdutils.c:783
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
struct SwsContext * sws_opts
Definition: cmdutils.c:56
void filter_release_buffer(AVFilterBuffer *fb)
A callback to be used for AVFilterBuffer.free.
Definition: cmdutils.c:1653
g
Definition: yuv2rgb.c:540
void init_opts(void)
Initialize the cmdutils option system, in particular allocate the *_opts contexts.
Definition: cmdutils.c:61
const char * name
Definition: cmdutils.h:125
void free_buffer_pool(FrameBuffer **pool)
Free all the buffers in the pool.
Definition: cmdutils.c:1660
void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
A callback to be used for AVCodecContext.release_buffer along with codec_get_buffer().
Definition: cmdutils.c:1642
int flags
Definition: cmdutils.h:126
const char * val
Definition: cmdutils.h:216
enum AVCodecID codec_id
Definition: mov_chan.c:432
const int program_birth_year
program birth year, defined by the program for show_banner()
Definition: avconv.c:83
enum AVPixelFormat pix_fmt
Definition: cmdutils.h:518
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
Setup AVCodecContext options for avformat_find_stream_info().
Definition: cmdutils.c:1500
OptionGroup * groups
Definition: cmdutils.h:248
size_t off
Definition: cmdutils.h:150
void init_pts_correction(PtsCorrectionContext *ctx)
Reset the state of the PtsCorrectionContext.
Definition: cmdutils.c:1327
int64_t i64
Definition: cmdutils.h:118
const OptionGroupDef * group_def
Definition: cmdutils.h:246
A list of option groups that all have the same group type (e.g.
Definition: cmdutils.h:245
int linesize[4]
Definition: cmdutils.h:515
void show_help_default(const char *opt, const char *arg)
Per-avtool specific help handler.
Definition: avconv_opt.c:1786
union SpecifierOpt::@1 u
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:1355
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
Parse one given option.
Definition: cmdutils.c:279
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:402
char * specifier
stream/chapter/program/...
Definition: cmdutils.h:114
struct OptionGroupList OptionGroupList
A list of option groups that all have the same group type (e.g.
static const OptionGroupDef groups[]
Definition: avconv_opt.c:1862
struct FrameBuffer FrameBuffer
void * dst_ptr
Definition: cmdutils.h:148
void show_banner(void)
Print the program banner to stderr.
Definition: cmdutils.c:762
struct OptionGroup OptionGroup
int opt_loglevel(void *optctx, const char *opt, const char *arg)
Set the libav* libraries log level.
Definition: cmdutils.c:657
uint8_t * str
Definition: cmdutils.h:116
struct PtsCorrectionContext PtsCorrectionContext
Stream structure.
Definition: avformat.h:622
int show_version(void *optctx, const char *opt, const char *arg)
Print the version of the program to stdout.
Definition: cmdutils.c:774
external API header
const char * help
Definition: cmdutils.h:152
int show_formats(void *optctx, const char *opt, const char *arg)
Print a listing containing all the formats supported by the program.
Definition: cmdutils.c:854
struct FrameBuffer ** pool
head of the buffer pool
Definition: cmdutils.h:521
main external API structure.
Definition: avcodec.h:1339
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
Parse a string specifying a time and return its corresponding value as a number of microseconds...
Definition: cmdutils.c:105
AVDictionary * codec_opts
Definition: cmdutils.c:57
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, AVCodec *codec)
Filter out options for given codec.
Definition: cmdutils.c:1442
const OptionGroupDef * group_def
Definition: cmdutils.h:230
Describe the class of an AVClass context structure.
Definition: log.h:33
static OutputContext octx
Definition: avprobe.c:109
const char * argname
Definition: cmdutils.h:153
int cmdutils_read_file(const char *filename, char **bufptr, size_t *size)
Read the file with name filename, and put its content in a newly allocated 0-terminated buffer...
Definition: cmdutils.c:1290
int show_codecs(void *optctx, const char *opt, const char *arg)
Print a listing containing all the codecs supported by the program.
Definition: cmdutils.c:994
struct SpecifierOpt SpecifierOpt
int64_t last_dts
PTS of the last frame.
Definition: cmdutils.h:456
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:556
uint8_t level
Definition: svq3.c:125
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *optctx, const char *))
Parse the command line arguments.
const OptionDef options[]
Definition: avserver.c:4665
An option extracted from the commandline.
Definition: cmdutils.h:213
Main libavformat public API header.
int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts)
Attempt to guess proper monotonic timestamps for decoded video frames which might have incorrect time...
Definition: cmdutils.c:1333
AVDictionary * format_opts
Definition: cmdutils.h:237
OptionGroupList * groups
Definition: cmdutils.h:255
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:117
OptionGroup global_opts
Definition: cmdutils.h:253
AVFormatContext * avformat_opts
void * 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:1520
const char * key
Definition: cmdutils.h:215
union OptionDef::@2 u
int show_encoders(void *optctx, const char *opt, const char *arg)
Print a listing containing all the encoders supported by the program.
Definition: cmdutils.c:1079
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:146
int show_help(void *optctx, const char *opt, const char *arg)
Generic -h handler common to all avtools.
Definition: cmdutils.c:1251
const OptionDef * opt
Definition: cmdutils.h:214
int refcount
Definition: cmdutils.h:520
int show_sample_fmts(void *optctx, const char *opt, const char *arg)
Print a listing containing all the sample formats supported by the program.
Definition: cmdutils.c:1156
OptionGroup cur_group
Definition: cmdutils.h:259
AVDictionary * codec_opts
Definition: cmdutils.h:236
Option * opts
Definition: cmdutils.h:233
void log_callback_help(void *ptr, int level, const char *fmt, va_list vl)
Trivial log callback.
Definition: cmdutils.c:79
struct Option Option
An option extracted from the commandline.
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:705
int show_filters(void *optctx, const char *opt, const char *arg)
Print a listing containing all the filters supported by the program.
Definition: cmdutils.c:1111
struct SwsContext * sws_opts
Definition: cmdutils.h:238
float min
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
int read_yesno(void)
Return a positive value if a line read from standard input starts with [yY], otherwise return 0...
Definition: cmdutils.c:1279
const char program_name[]
program name, defined by the program for show_version().
Definition: avconv.c:82