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