FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/cmdutils.h
Date: 2024-07-24 19:24:46
Exec Total Coverage
Lines: 0 3 0.0%
Functions: 0 1 0.0%
Branches: 0 0 -%

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