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