2 * Copyright (c) 2007-2010 Stefano Sabatini
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media prober based on the FFmpeg libraries
31 #include "libavformat/avformat.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/libm.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/timecode.h"
42 #include "libavutil/timestamp.h"
43 #include "libavdevice/avdevice.h"
44 #include "libswscale/swscale.h"
45 #include "libswresample/swresample.h"
46 #include "libpostproc/postprocess.h"
49 const char program_name[] = "ffprobe";
50 const int program_birth_year = 2007;
52 static int do_bitexact = 0;
53 static int do_count_frames = 0;
54 static int do_count_packets = 0;
55 static int do_read_frames = 0;
56 static int do_read_packets = 0;
57 static int do_show_chapters = 0;
58 static int do_show_error = 0;
59 static int do_show_format = 0;
60 static int do_show_frames = 0;
61 static int do_show_packets = 0;
62 static int do_show_programs = 0;
63 static int do_show_streams = 0;
64 static int do_show_stream_disposition = 0;
65 static int do_show_data = 0;
66 static int do_show_program_version = 0;
67 static int do_show_library_versions = 0;
69 static int show_value_unit = 0;
70 static int use_value_prefix = 0;
71 static int use_byte_value_binary_prefix = 0;
72 static int use_value_sexagesimal_format = 0;
73 static int show_private_data = 1;
75 static char *print_format;
76 static char *stream_specifier;
79 int id; ///< identifier
80 int64_t start, end; ///< start, end in second/AV_TIME_BASE units
81 int has_start, has_end;
82 int start_is_offset, end_is_offset;
86 static ReadInterval *read_intervals;
87 static int read_intervals_nb = 0;
89 /* section structure definition */
91 #define SECTION_MAX_NB_CHILDREN 10
94 int id; ///< unique id identifying a section
97 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
98 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
99 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
100 /// For these sections the element_name field is mandatory.
102 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
103 const char *element_name; ///< name of the contained element, if provided
104 const char *unique_name; ///< unique section name, in case the name is ambiguous
105 AVDictionary *entries_to_show;
106 int show_all_entries;
110 SECTION_ID_NONE = -1,
112 SECTION_ID_CHAPTER_TAGS,
116 SECTION_ID_FORMAT_TAGS,
119 SECTION_ID_FRAME_TAGS,
120 SECTION_ID_LIBRARY_VERSION,
121 SECTION_ID_LIBRARY_VERSIONS,
124 SECTION_ID_PACKETS_AND_FRAMES,
125 SECTION_ID_PROGRAM_STREAM_DISPOSITION,
126 SECTION_ID_PROGRAM_STREAM_TAGS,
128 SECTION_ID_PROGRAM_STREAMS,
129 SECTION_ID_PROGRAM_STREAM,
130 SECTION_ID_PROGRAM_TAGS,
131 SECTION_ID_PROGRAM_VERSION,
135 SECTION_ID_STREAM_DISPOSITION,
137 SECTION_ID_STREAM_TAGS,
140 static struct section sections[] = {
141 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
142 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
143 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
144 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
145 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
146 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
147 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, -1 } },
148 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
149 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
150 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
151 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
152 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
153 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
154 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
155 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
156 [SECTION_ID_PROGRAM_STREAM_TAGS] = { SECTION_ID_PROGRAM_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_stream_tags" },
157 [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
158 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
159 [SECTION_ID_PROGRAM_STREAM] = { SECTION_ID_PROGRAM_STREAM, "stream", 0, { SECTION_ID_PROGRAM_STREAM_DISPOSITION, SECTION_ID_PROGRAM_STREAM_TAGS, -1 }, .unique_name = "program_stream" },
160 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
161 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
162 [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
163 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
164 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAMS,
165 SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS, -1} },
166 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
167 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
168 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
169 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
172 static const OptionDef *options;
174 /* FFprobe context */
175 static const char *input_filename;
176 static AVInputFormat *iformat = NULL;
178 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
179 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
181 static const char unit_second_str[] = "s" ;
182 static const char unit_hertz_str[] = "Hz" ;
183 static const char unit_byte_str[] = "byte" ;
184 static const char unit_bit_per_second_str[] = "bit/s";
186 static uint64_t *nb_streams_packets;
187 static uint64_t *nb_streams_frames;
188 static int *selected_streams;
190 static void ffprobe_cleanup(int ret)
193 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
194 av_dict_free(&(sections[i].entries_to_show));
198 union { double d; long long int i; } val;
202 static char *value_string(char *buf, int buf_size, struct unit_value uv)
208 if (uv.unit == unit_second_str) {
212 vald = vali = uv.val.i;
215 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
219 mins = (int)secs / 60;
220 secs = secs - mins * 60;
223 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
225 const char *prefix_string = "";
227 if (use_value_prefix && vald > 1) {
230 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
231 index = (long long int) (log2(vald)) / 10;
232 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
233 vald /= exp2(index * 10);
234 prefix_string = binary_unit_prefixes[index];
236 index = (long long int) (log10(vald)) / 3;
237 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
238 vald /= pow(10, index * 3);
239 prefix_string = decimal_unit_prefixes[index];
243 if (show_float || (use_value_prefix && vald != (long long int)vald))
244 snprintf(buf, buf_size, "%f", vald);
246 snprintf(buf, buf_size, "%lld", vali);
247 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
248 prefix_string, show_value_unit ? uv.unit : "");
256 typedef struct WriterContext WriterContext;
258 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
259 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
262 WRITER_STRING_VALIDATION_FAIL,
263 WRITER_STRING_VALIDATION_REPLACE,
264 WRITER_STRING_VALIDATION_IGNORE,
265 WRITER_STRING_VALIDATION_NB
268 typedef struct Writer {
269 const AVClass *priv_class; ///< private class of the writer, if any
270 int priv_size; ///< private size for the writer context
273 int (*init) (WriterContext *wctx);
274 void (*uninit)(WriterContext *wctx);
276 void (*print_section_header)(WriterContext *wctx);
277 void (*print_section_footer)(WriterContext *wctx);
278 void (*print_integer) (WriterContext *wctx, const char *, long long int);
279 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
280 void (*print_string) (WriterContext *wctx, const char *, const char *);
281 int flags; ///< a combination or WRITER_FLAG_*
284 #define SECTION_MAX_NB_LEVELS 10
286 struct WriterContext {
287 const AVClass *class; ///< class of the writer
288 const Writer *writer; ///< the Writer of which this is an instance
289 char *name; ///< name of this writer instance
290 void *priv; ///< private data for use by the filter
292 const struct section *sections; ///< array containing all sections
293 int nb_sections; ///< number of sections
295 int level; ///< current level, starting from 0
297 /** number of the item printed in the given section, starting from 0 */
298 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
300 /** section per each level */
301 const struct section *section[SECTION_MAX_NB_LEVELS];
302 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
303 /// used by various writers
305 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
306 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
307 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
309 StringValidation string_validation;
310 char *string_validation_replacement;
311 unsigned int string_validation_utf8_flags;
314 static const char *writer_get_name(void *p)
316 WriterContext *wctx = p;
317 return wctx->writer->name;
320 #define OFFSET(x) offsetof(WriterContext, x)
322 static const AVOption writer_options[] = {
323 { "string_validation", "set string validation mode",
324 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
325 { "sv", "set string validation mode",
326 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
327 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
328 { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
329 { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
330 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
331 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
335 static void *writer_child_next(void *obj, void *prev)
337 WriterContext *ctx = obj;
338 if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
343 static const AVClass writer_class = {
347 LIBAVUTIL_VERSION_INT,
348 .option = writer_options,
349 .child_next = writer_child_next,
352 static void writer_close(WriterContext **wctx)
359 if ((*wctx)->writer->uninit)
360 (*wctx)->writer->uninit(*wctx);
361 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
362 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
363 if ((*wctx)->writer->priv_class)
364 av_opt_free((*wctx)->priv);
365 av_freep(&((*wctx)->priv));
369 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
372 av_bprintf(bp, "0X");
373 for (i = 0; i < ubuf_size; i++)
374 av_bprintf(bp, "%02X", ubuf[i]);
378 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
379 const struct section *sections, int nb_sections)
383 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
384 ret = AVERROR(ENOMEM);
388 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
389 ret = AVERROR(ENOMEM);
393 (*wctx)->class = &writer_class;
394 (*wctx)->writer = writer;
396 (*wctx)->sections = sections;
397 (*wctx)->nb_sections = nb_sections;
399 av_opt_set_defaults(*wctx);
401 if (writer->priv_class) {
402 void *priv_ctx = (*wctx)->priv;
403 *((const AVClass **)priv_ctx) = writer->priv_class;
404 av_opt_set_defaults(priv_ctx);
407 /* convert options to dictionary */
409 AVDictionary *opts = NULL;
410 AVDictionaryEntry *opt = NULL;
412 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
413 av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
418 while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
419 if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
420 av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
421 opt->key, opt->value);
430 /* validate replace string */
432 const uint8_t *p = (*wctx)->string_validation_replacement;
433 const uint8_t *endp = p + strlen(p);
435 const uint8_t *p0 = p;
437 ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
440 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
441 bprint_bytes(&bp, p0, p-p0),
442 av_log(wctx, AV_LOG_ERROR,
443 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
444 bp.str, (*wctx)->string_validation_replacement);
450 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
451 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
453 if ((*wctx)->writer->init)
454 ret = (*wctx)->writer->init(*wctx);
465 static inline void writer_print_section_header(WriterContext *wctx,
468 int parent_section_id;
470 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
471 parent_section_id = wctx->level ?
472 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
474 wctx->nb_item[wctx->level] = 0;
475 wctx->section[wctx->level] = &wctx->sections[section_id];
477 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
478 wctx->nb_section_packet = wctx->nb_section_frame =
479 wctx->nb_section_packet_frame = 0;
480 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
481 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
482 wctx->nb_section_packet : wctx->nb_section_frame;
485 if (wctx->writer->print_section_header)
486 wctx->writer->print_section_header(wctx);
489 static inline void writer_print_section_footer(WriterContext *wctx)
491 int section_id = wctx->section[wctx->level]->id;
492 int parent_section_id = wctx->level ?
493 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
495 if (parent_section_id != SECTION_ID_NONE)
496 wctx->nb_item[wctx->level-1]++;
497 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
498 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
499 else wctx->nb_section_frame++;
501 if (wctx->writer->print_section_footer)
502 wctx->writer->print_section_footer(wctx);
506 static inline void writer_print_integer(WriterContext *wctx,
507 const char *key, long long int val)
509 const struct section *section = wctx->section[wctx->level];
511 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
512 wctx->writer->print_integer(wctx, key, val);
513 wctx->nb_item[wctx->level]++;
517 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
519 const uint8_t *p, *endp;
521 int invalid_chars_nb = 0, ret = 0;
523 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
525 endp = src + strlen(src);
526 for (p = (uint8_t *)src; *p;) {
529 const uint8_t *p0 = p;
531 if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
533 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
534 bprint_bytes(&bp, p0, p-p0);
535 av_log(wctx, AV_LOG_DEBUG,
536 "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
543 switch (wctx->string_validation) {
544 case WRITER_STRING_VALIDATION_FAIL:
545 av_log(wctx, AV_LOG_ERROR,
546 "Invalid UTF-8 sequence found in string '%s'\n", src);
547 ret = AVERROR_INVALIDDATA;
551 case WRITER_STRING_VALIDATION_REPLACE:
552 av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
557 if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
558 av_bprint_append_data(&dstbuf, p0, p-p0);
561 if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
562 av_log(wctx, AV_LOG_WARNING,
563 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
564 invalid_chars_nb, src, wctx->string_validation_replacement);
568 av_bprint_finalize(&dstbuf, dstp);
572 #define PRINT_STRING_OPT 1
573 #define PRINT_STRING_VALIDATE 2
575 static inline int writer_print_string(WriterContext *wctx,
576 const char *key, const char *val, int flags)
578 const struct section *section = wctx->section[wctx->level];
581 if ((flags & PRINT_STRING_OPT)
582 && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
585 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
586 if (flags & PRINT_STRING_VALIDATE) {
587 char *key1 = NULL, *val1 = NULL;
588 ret = validate_string(wctx, &key1, key);
589 if (ret < 0) goto end;
590 ret = validate_string(wctx, &val1, val);
591 if (ret < 0) goto end;
592 wctx->writer->print_string(wctx, key1, val1);
595 av_log(wctx, AV_LOG_ERROR,
596 "Invalid key=value string combination %s=%s in section %s\n",
597 key, val, section->unique_name);
602 wctx->writer->print_string(wctx, key, val);
605 wctx->nb_item[wctx->level]++;
611 static inline void writer_print_rational(WriterContext *wctx,
612 const char *key, AVRational q, char sep)
615 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
616 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
617 writer_print_string(wctx, key, buf.str, 0);
620 static void writer_print_time(WriterContext *wctx, const char *key,
621 int64_t ts, const AVRational *time_base, int is_duration)
625 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
626 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
628 double d = ts * av_q2d(*time_base);
629 struct unit_value uv;
631 uv.unit = unit_second_str;
632 value_string(buf, sizeof(buf), uv);
633 writer_print_string(wctx, key, buf, 0);
637 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
639 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
640 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
642 writer_print_integer(wctx, key, ts);
646 static void writer_print_data(WriterContext *wctx, const char *name,
647 uint8_t *data, int size)
650 int offset = 0, l, i;
652 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
653 av_bprintf(&bp, "\n");
655 av_bprintf(&bp, "%08x: ", offset);
657 for (i = 0; i < l; i++) {
658 av_bprintf(&bp, "%02x", data[i]);
660 av_bprintf(&bp, " ");
662 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
663 for (i = 0; i < l; i++)
664 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
665 av_bprintf(&bp, "\n");
670 writer_print_string(wctx, name, bp.str, 0);
671 av_bprint_finalize(&bp, NULL);
674 #define MAX_REGISTERED_WRITERS_NB 64
676 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
678 static int writer_register(const Writer *writer)
680 static int next_registered_writer_idx = 0;
682 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
683 return AVERROR(ENOMEM);
685 registered_writers[next_registered_writer_idx++] = writer;
689 static const Writer *writer_get_by_name(const char *name)
693 for (i = 0; registered_writers[i]; i++)
694 if (!strcmp(registered_writers[i]->name, name))
695 return registered_writers[i];
703 #define DEFINE_WRITER_CLASS(name) \
704 static const char *name##_get_name(void *ctx) \
708 static const AVClass name##_class = { \
716 typedef struct DefaultContext {
717 const AVClass *class;
719 int noprint_wrappers;
720 int nested_section[SECTION_MAX_NB_LEVELS];
724 #define OFFSET(x) offsetof(DefaultContext, x)
726 static const AVOption default_options[] = {
727 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
728 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
729 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
730 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
734 DEFINE_WRITER_CLASS(default);
736 /* lame uppercasing routine, assumes the string is lower case ASCII */
737 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
740 for (i = 0; src[i] && i < dst_size-1; i++)
741 dst[i] = av_toupper(src[i]);
746 static void default_print_section_header(WriterContext *wctx)
748 DefaultContext *def = wctx->priv;
750 const struct section *section = wctx->section[wctx->level];
751 const struct section *parent_section = wctx->level ?
752 wctx->section[wctx->level-1] : NULL;
754 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
755 if (parent_section &&
756 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
757 def->nested_section[wctx->level] = 1;
758 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
759 wctx->section_pbuf[wctx->level-1].str,
760 upcase_string(buf, sizeof(buf),
761 av_x_if_null(section->element_name, section->name)));
764 if (def->noprint_wrappers || def->nested_section[wctx->level])
767 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
768 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
771 static void default_print_section_footer(WriterContext *wctx)
773 DefaultContext *def = wctx->priv;
774 const struct section *section = wctx->section[wctx->level];
777 if (def->noprint_wrappers || def->nested_section[wctx->level])
780 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
781 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
784 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
786 DefaultContext *def = wctx->priv;
789 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
790 printf("%s\n", value);
793 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
795 DefaultContext *def = wctx->priv;
798 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
799 printf("%lld\n", value);
802 static const Writer default_writer = {
804 .priv_size = sizeof(DefaultContext),
805 .print_section_header = default_print_section_header,
806 .print_section_footer = default_print_section_footer,
807 .print_integer = default_print_int,
808 .print_string = default_print_str,
809 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
810 .priv_class = &default_class,
816 * Apply C-language-like string escaping.
818 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
822 for (p = src; *p; p++) {
824 case '\b': av_bprintf(dst, "%s", "\\b"); break;
825 case '\f': av_bprintf(dst, "%s", "\\f"); break;
826 case '\n': av_bprintf(dst, "%s", "\\n"); break;
827 case '\r': av_bprintf(dst, "%s", "\\r"); break;
828 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
831 av_bprint_chars(dst, '\\', 1);
832 av_bprint_chars(dst, *p, 1);
839 * Quote fields containing special characters, check RFC4180.
841 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
843 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
844 int needs_quoting = !!src[strcspn(src, meta_chars)];
847 av_bprint_chars(dst, '"', 1);
849 for (; *src; src++) {
851 av_bprint_chars(dst, '"', 1);
852 av_bprint_chars(dst, *src, 1);
855 av_bprint_chars(dst, '"', 1);
859 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
864 typedef struct CompactContext {
865 const AVClass *class;
870 char *escape_mode_str;
871 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
872 int nested_section[SECTION_MAX_NB_LEVELS];
873 int has_nested_elems[SECTION_MAX_NB_LEVELS];
874 int terminate_line[SECTION_MAX_NB_LEVELS];
878 #define OFFSET(x) offsetof(CompactContext, x)
880 static const AVOption compact_options[]= {
881 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
882 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
883 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
884 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
885 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
886 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
887 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
888 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
892 DEFINE_WRITER_CLASS(compact);
894 static av_cold int compact_init(WriterContext *wctx)
896 CompactContext *compact = wctx->priv;
898 if (strlen(compact->item_sep_str) != 1) {
899 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
900 compact->item_sep_str);
901 return AVERROR(EINVAL);
903 compact->item_sep = compact->item_sep_str[0];
905 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
906 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
907 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
909 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
910 return AVERROR(EINVAL);
916 static void compact_print_section_header(WriterContext *wctx)
918 CompactContext *compact = wctx->priv;
919 const struct section *section = wctx->section[wctx->level];
920 const struct section *parent_section = wctx->level ?
921 wctx->section[wctx->level-1] : NULL;
922 compact->terminate_line[wctx->level] = 1;
923 compact->has_nested_elems[wctx->level] = 0;
925 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
926 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
927 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
928 compact->nested_section[wctx->level] = 1;
929 compact->has_nested_elems[wctx->level-1] = 1;
930 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
931 wctx->section_pbuf[wctx->level-1].str,
932 (char *)av_x_if_null(section->element_name, section->name));
933 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
935 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
936 (section->flags & SECTION_FLAG_IS_ARRAY)) {
937 compact->terminate_line[wctx->level-1] = 0;
940 if (compact->print_section &&
941 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
942 printf("%s%c", section->name, compact->item_sep);
946 static void compact_print_section_footer(WriterContext *wctx)
948 CompactContext *compact = wctx->priv;
950 if (!compact->nested_section[wctx->level] &&
951 compact->terminate_line[wctx->level] &&
952 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
956 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
958 CompactContext *compact = wctx->priv;
961 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
963 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
964 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
965 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
966 av_bprint_finalize(&buf, NULL);
969 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
971 CompactContext *compact = wctx->priv;
973 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
975 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
976 printf("%lld", value);
979 static const Writer compact_writer = {
981 .priv_size = sizeof(CompactContext),
982 .init = compact_init,
983 .print_section_header = compact_print_section_header,
984 .print_section_footer = compact_print_section_footer,
985 .print_integer = compact_print_int,
986 .print_string = compact_print_str,
987 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
988 .priv_class = &compact_class,
994 #define OFFSET(x) offsetof(CompactContext, x)
996 static const AVOption csv_options[] = {
997 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
998 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
999 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1000 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1001 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1002 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1003 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1004 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1008 DEFINE_WRITER_CLASS(csv);
1010 static const Writer csv_writer = {
1012 .priv_size = sizeof(CompactContext),
1013 .init = compact_init,
1014 .print_section_header = compact_print_section_header,
1015 .print_section_footer = compact_print_section_footer,
1016 .print_integer = compact_print_int,
1017 .print_string = compact_print_str,
1018 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1019 .priv_class = &csv_class,
1024 typedef struct FlatContext {
1025 const AVClass *class;
1026 const char *sep_str;
1032 #define OFFSET(x) offsetof(FlatContext, x)
1034 static const AVOption flat_options[]= {
1035 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1036 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1037 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1038 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1042 DEFINE_WRITER_CLASS(flat);
1044 static av_cold int flat_init(WriterContext *wctx)
1046 FlatContext *flat = wctx->priv;
1048 if (strlen(flat->sep_str) != 1) {
1049 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1051 return AVERROR(EINVAL);
1053 flat->sep = flat->sep_str[0];
1058 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1062 for (p = src; *p; p++) {
1063 if (!((*p >= '0' && *p <= '9') ||
1064 (*p >= 'a' && *p <= 'z') ||
1065 (*p >= 'A' && *p <= 'Z')))
1066 av_bprint_chars(dst, '_', 1);
1068 av_bprint_chars(dst, *p, 1);
1073 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1077 for (p = src; *p; p++) {
1079 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1080 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1081 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1082 case '"': av_bprintf(dst, "%s", "\\\""); break;
1083 case '`': av_bprintf(dst, "%s", "\\`"); break;
1084 case '$': av_bprintf(dst, "%s", "\\$"); break;
1085 default: av_bprint_chars(dst, *p, 1); break;
1091 static void flat_print_section_header(WriterContext *wctx)
1093 FlatContext *flat = wctx->priv;
1094 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1095 const struct section *section = wctx->section[wctx->level];
1096 const struct section *parent_section = wctx->level ?
1097 wctx->section[wctx->level-1] : NULL;
1099 /* build section header */
1100 av_bprint_clear(buf);
1101 if (!parent_section)
1103 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1105 if (flat->hierarchical ||
1106 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1107 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1109 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1110 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1111 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1112 av_bprintf(buf, "%d%s", n, flat->sep_str);
1117 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1119 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1122 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1124 FlatContext *flat = wctx->priv;
1127 printf("%s", wctx->section_pbuf[wctx->level].str);
1128 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1129 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
1130 av_bprint_clear(&buf);
1131 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
1132 av_bprint_finalize(&buf, NULL);
1135 static const Writer flat_writer = {
1137 .priv_size = sizeof(FlatContext),
1139 .print_section_header = flat_print_section_header,
1140 .print_integer = flat_print_int,
1141 .print_string = flat_print_str,
1142 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1143 .priv_class = &flat_class,
1146 /* INI format output */
1149 const AVClass *class;
1154 #define OFFSET(x) offsetof(INIContext, x)
1156 static const AVOption ini_options[] = {
1157 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1158 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1162 DEFINE_WRITER_CLASS(ini);
1164 static char *ini_escape_str(AVBPrint *dst, const char *src)
1169 while (c = src[i++]) {
1171 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1172 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1173 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1174 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1175 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1179 case ':' : av_bprint_chars(dst, '\\', 1);
1181 if ((unsigned char)c < 32)
1182 av_bprintf(dst, "\\x00%02x", c & 0xff);
1184 av_bprint_chars(dst, c, 1);
1191 static void ini_print_section_header(WriterContext *wctx)
1193 INIContext *ini = wctx->priv;
1194 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1195 const struct section *section = wctx->section[wctx->level];
1196 const struct section *parent_section = wctx->level ?
1197 wctx->section[wctx->level-1] : NULL;
1199 av_bprint_clear(buf);
1200 if (!parent_section) {
1201 printf("# ffprobe output\n\n");
1205 if (wctx->nb_item[wctx->level-1])
1208 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1209 if (ini->hierarchical ||
1210 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1211 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1213 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1214 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1215 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1216 av_bprintf(buf, ".%d", n);
1220 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1221 printf("[%s]\n", buf->str);
1224 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1228 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1229 printf("%s=", ini_escape_str(&buf, key));
1230 av_bprint_clear(&buf);
1231 printf("%s\n", ini_escape_str(&buf, value));
1232 av_bprint_finalize(&buf, NULL);
1235 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1237 printf("%s=%lld\n", key, value);
1240 static const Writer ini_writer = {
1242 .priv_size = sizeof(INIContext),
1243 .print_section_header = ini_print_section_header,
1244 .print_integer = ini_print_int,
1245 .print_string = ini_print_str,
1246 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1247 .priv_class = &ini_class,
1253 const AVClass *class;
1256 const char *item_sep, *item_start_end;
1260 #define OFFSET(x) offsetof(JSONContext, x)
1262 static const AVOption json_options[]= {
1263 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1264 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1268 DEFINE_WRITER_CLASS(json);
1270 static av_cold int json_init(WriterContext *wctx)
1272 JSONContext *json = wctx->priv;
1274 json->item_sep = json->compact ? ", " : ",\n";
1275 json->item_start_end = json->compact ? " " : "\n";
1280 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1282 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1283 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1286 for (p = src; *p; p++) {
1287 char *s = strchr(json_escape, *p);
1289 av_bprint_chars(dst, '\\', 1);
1290 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1291 } else if ((unsigned char)*p < 32) {
1292 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1294 av_bprint_chars(dst, *p, 1);
1300 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1302 static void json_print_section_header(WriterContext *wctx)
1304 JSONContext *json = wctx->priv;
1306 const struct section *section = wctx->section[wctx->level];
1307 const struct section *parent_section = wctx->level ?
1308 wctx->section[wctx->level-1] : NULL;
1310 if (wctx->level && wctx->nb_item[wctx->level-1])
1313 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1315 json->indent_level++;
1317 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1318 json_escape_str(&buf, section->name, wctx);
1321 json->indent_level++;
1322 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1323 printf("\"%s\": [\n", buf.str);
1324 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1325 printf("\"%s\": {%s", buf.str, json->item_start_end);
1327 printf("{%s", json->item_start_end);
1329 /* this is required so the parser can distinguish between packets and frames */
1330 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1333 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1336 av_bprint_finalize(&buf, NULL);
1340 static void json_print_section_footer(WriterContext *wctx)
1342 JSONContext *json = wctx->priv;
1343 const struct section *section = wctx->section[wctx->level];
1345 if (wctx->level == 0) {
1346 json->indent_level--;
1348 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1350 json->indent_level--;
1354 printf("%s", json->item_start_end);
1355 json->indent_level--;
1362 static inline void json_print_item_str(WriterContext *wctx,
1363 const char *key, const char *value)
1367 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1368 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1369 av_bprint_clear(&buf);
1370 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1371 av_bprint_finalize(&buf, NULL);
1374 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1376 JSONContext *json = wctx->priv;
1378 if (wctx->nb_item[wctx->level])
1379 printf("%s", json->item_sep);
1382 json_print_item_str(wctx, key, value);
1385 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1387 JSONContext *json = wctx->priv;
1390 if (wctx->nb_item[wctx->level])
1391 printf("%s", json->item_sep);
1395 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1396 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1397 av_bprint_finalize(&buf, NULL);
1400 static const Writer json_writer = {
1402 .priv_size = sizeof(JSONContext),
1404 .print_section_header = json_print_section_header,
1405 .print_section_footer = json_print_section_footer,
1406 .print_integer = json_print_int,
1407 .print_string = json_print_str,
1408 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1409 .priv_class = &json_class,
1415 const AVClass *class;
1418 int fully_qualified;
1423 #define OFFSET(x) offsetof(XMLContext, x)
1425 static const AVOption xml_options[] = {
1426 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1427 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1428 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1429 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1433 DEFINE_WRITER_CLASS(xml);
1435 static av_cold int xml_init(WriterContext *wctx)
1437 XMLContext *xml = wctx->priv;
1439 if (xml->xsd_strict) {
1440 xml->fully_qualified = 1;
1441 #define CHECK_COMPLIANCE(opt, opt_name) \
1443 av_log(wctx, AV_LOG_ERROR, \
1444 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1445 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1446 return AVERROR(EINVAL); \
1448 CHECK_COMPLIANCE(show_private_data, "private");
1449 CHECK_COMPLIANCE(show_value_unit, "unit");
1450 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1452 if (do_show_frames && do_show_packets) {
1453 av_log(wctx, AV_LOG_ERROR,
1454 "Interleaved frames and packets are not allowed in XSD. "
1455 "Select only one between the -show_frames and the -show_packets options.\n");
1456 return AVERROR(EINVAL);
1463 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1467 for (p = src; *p; p++) {
1469 case '&' : av_bprintf(dst, "%s", "&"); break;
1470 case '<' : av_bprintf(dst, "%s", "<"); break;
1471 case '>' : av_bprintf(dst, "%s", ">"); break;
1472 case '"' : av_bprintf(dst, "%s", """); break;
1473 case '\'': av_bprintf(dst, "%s", "'"); break;
1474 default: av_bprint_chars(dst, *p, 1);
1481 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1483 static void xml_print_section_header(WriterContext *wctx)
1485 XMLContext *xml = wctx->priv;
1486 const struct section *section = wctx->section[wctx->level];
1487 const struct section *parent_section = wctx->level ?
1488 wctx->section[wctx->level-1] : NULL;
1490 if (wctx->level == 0) {
1491 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1492 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1493 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1495 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1496 printf("<%sffprobe%s>\n",
1497 xml->fully_qualified ? "ffprobe:" : "",
1498 xml->fully_qualified ? qual : "");
1502 if (xml->within_tag) {
1503 xml->within_tag = 0;
1506 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1507 xml->indent_level++;
1509 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1510 wctx->level && wctx->nb_item[wctx->level-1])
1512 xml->indent_level++;
1514 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1515 XML_INDENT(); printf("<%s>\n", section->name);
1517 XML_INDENT(); printf("<%s ", section->name);
1518 xml->within_tag = 1;
1523 static void xml_print_section_footer(WriterContext *wctx)
1525 XMLContext *xml = wctx->priv;
1526 const struct section *section = wctx->section[wctx->level];
1528 if (wctx->level == 0) {
1529 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1530 } else if (xml->within_tag) {
1531 xml->within_tag = 0;
1533 xml->indent_level--;
1534 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1535 xml->indent_level--;
1537 XML_INDENT(); printf("</%s>\n", section->name);
1538 xml->indent_level--;
1542 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1545 XMLContext *xml = wctx->priv;
1546 const struct section *section = wctx->section[wctx->level];
1548 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1550 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1552 printf("<%s key=\"%s\"",
1553 section->element_name, xml_escape_str(&buf, key, wctx));
1554 av_bprint_clear(&buf);
1555 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1557 if (wctx->nb_item[wctx->level])
1559 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1562 av_bprint_finalize(&buf, NULL);
1565 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1567 if (wctx->nb_item[wctx->level])
1569 printf("%s=\"%lld\"", key, value);
1572 static Writer xml_writer = {
1574 .priv_size = sizeof(XMLContext),
1576 .print_section_header = xml_print_section_header,
1577 .print_section_footer = xml_print_section_footer,
1578 .print_integer = xml_print_int,
1579 .print_string = xml_print_str,
1580 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1581 .priv_class = &xml_class,
1584 static void writer_register_all(void)
1586 static int initialized;
1592 writer_register(&default_writer);
1593 writer_register(&compact_writer);
1594 writer_register(&csv_writer);
1595 writer_register(&flat_writer);
1596 writer_register(&ini_writer);
1597 writer_register(&json_writer);
1598 writer_register(&xml_writer);
1601 #define print_fmt(k, f, ...) do { \
1602 av_bprint_clear(&pbuf); \
1603 av_bprintf(&pbuf, f, __VA_ARGS__); \
1604 writer_print_string(w, k, pbuf.str, 0); \
1607 #define print_int(k, v) writer_print_integer(w, k, v)
1608 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1609 #define print_str(k, v) writer_print_string(w, k, v, 0)
1610 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1611 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1612 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1613 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1614 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1615 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1616 #define print_val(k, v, u) do { \
1617 struct unit_value uv; \
1620 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1623 #define print_section_header(s) writer_print_section_header(w, s)
1624 #define print_section_footer(s) writer_print_section_footer(w, s)
1626 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1628 AVDictionaryEntry *tag = NULL;
1633 writer_print_section_header(w, section_id);
1635 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1636 if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1639 writer_print_section_footer(w);
1644 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1647 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1651 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1653 writer_print_section_header(w, SECTION_ID_PACKET);
1655 s = av_get_media_type_string(st->codec->codec_type);
1656 if (s) print_str ("codec_type", s);
1657 else print_str_opt("codec_type", "unknown");
1658 print_int("stream_index", pkt->stream_index);
1659 print_ts ("pts", pkt->pts);
1660 print_time("pts_time", pkt->pts, &st->time_base);
1661 print_ts ("dts", pkt->dts);
1662 print_time("dts_time", pkt->dts, &st->time_base);
1663 print_duration_ts("duration", pkt->duration);
1664 print_duration_time("duration_time", pkt->duration, &st->time_base);
1665 print_duration_ts("convergence_duration", pkt->convergence_duration);
1666 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1667 print_val("size", pkt->size, unit_byte_str);
1668 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1669 else print_str_opt("pos", "N/A");
1670 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1672 writer_print_data(w, "data", pkt->data, pkt->size);
1673 writer_print_section_footer(w);
1675 av_bprint_finalize(&pbuf, NULL);
1679 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1680 AVFormatContext *fmt_ctx)
1685 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1687 writer_print_section_header(w, SECTION_ID_FRAME);
1689 s = av_get_media_type_string(stream->codec->codec_type);
1690 if (s) print_str ("media_type", s);
1691 else print_str_opt("media_type", "unknown");
1692 print_int("key_frame", frame->key_frame);
1693 print_ts ("pkt_pts", frame->pkt_pts);
1694 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1695 print_ts ("pkt_dts", frame->pkt_dts);
1696 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1697 print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1698 print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1699 if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1700 else print_str_opt("pkt_pos", "N/A");
1701 if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1702 else print_str_opt("pkt_size", "N/A");
1704 switch (stream->codec->codec_type) {
1707 case AVMEDIA_TYPE_VIDEO:
1708 print_int("width", frame->width);
1709 print_int("height", frame->height);
1710 s = av_get_pix_fmt_name(frame->format);
1711 if (s) print_str ("pix_fmt", s);
1712 else print_str_opt("pix_fmt", "unknown");
1713 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1715 print_q("sample_aspect_ratio", sar, ':');
1717 print_str_opt("sample_aspect_ratio", "N/A");
1719 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1720 print_int("coded_picture_number", frame->coded_picture_number);
1721 print_int("display_picture_number", frame->display_picture_number);
1722 print_int("interlaced_frame", frame->interlaced_frame);
1723 print_int("top_field_first", frame->top_field_first);
1724 print_int("repeat_pict", frame->repeat_pict);
1727 case AVMEDIA_TYPE_AUDIO:
1728 s = av_get_sample_fmt_name(frame->format);
1729 if (s) print_str ("sample_fmt", s);
1730 else print_str_opt("sample_fmt", "unknown");
1731 print_int("nb_samples", frame->nb_samples);
1732 print_int("channels", av_frame_get_channels(frame));
1733 if (av_frame_get_channel_layout(frame)) {
1734 av_bprint_clear(&pbuf);
1735 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1736 av_frame_get_channel_layout(frame));
1737 print_str ("channel_layout", pbuf.str);
1739 print_str_opt("channel_layout", "unknown");
1742 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1744 writer_print_section_footer(w);
1746 av_bprint_finalize(&pbuf, NULL);
1750 static av_always_inline int process_frame(WriterContext *w,
1751 AVFormatContext *fmt_ctx,
1752 AVFrame *frame, AVPacket *pkt)
1754 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1755 int ret = 0, got_frame = 0;
1757 avcodec_get_frame_defaults(frame);
1758 if (dec_ctx->codec) {
1759 switch (dec_ctx->codec_type) {
1760 case AVMEDIA_TYPE_VIDEO:
1761 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1764 case AVMEDIA_TYPE_AUDIO:
1765 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1772 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1776 nb_streams_frames[pkt->stream_index]++;
1778 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1783 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
1785 av_log(log_ctx, log_level, "id:%d", interval->id);
1787 if (interval->has_start) {
1788 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
1789 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
1791 av_log(log_ctx, log_level, " start:N/A");
1794 if (interval->has_end) {
1795 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
1796 if (interval->duration_frames)
1797 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
1799 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
1801 av_log(log_ctx, log_level, " end:N/A");
1804 av_log(log_ctx, log_level, "\n");
1807 static int read_interval_packets(WriterContext *w, AVFormatContext *fmt_ctx,
1808 const ReadInterval *interval, int64_t *cur_ts)
1812 int ret = 0, i = 0, frame_count = 0;
1813 int64_t start = -INT64_MAX, end = interval->end;
1814 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
1816 av_init_packet(&pkt);
1818 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
1819 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
1821 if (interval->has_start) {
1823 if (interval->start_is_offset) {
1824 if (*cur_ts == AV_NOPTS_VALUE) {
1825 av_log(NULL, AV_LOG_ERROR,
1826 "Could not seek to relative position since current "
1827 "timestamp is not defined\n");
1828 ret = AVERROR(EINVAL);
1831 target = *cur_ts + interval->start;
1833 target = interval->start;
1836 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
1837 av_ts2timestr(target, &AV_TIME_BASE_Q));
1838 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
1839 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
1840 interval->start, av_err2str(ret));
1845 while (!av_read_frame(fmt_ctx, &pkt)) {
1846 if (selected_streams[pkt.stream_index]) {
1847 AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
1849 if (pkt.pts != AV_NOPTS_VALUE)
1850 *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
1852 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
1857 if (has_start && !has_end && interval->end_is_offset) {
1858 end = start + interval->end;
1862 if (interval->end_is_offset && interval->duration_frames) {
1863 if (frame_count >= interval->end)
1865 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
1870 if (do_read_packets) {
1871 if (do_show_packets)
1872 show_packet(w, fmt_ctx, &pkt, i++);
1873 nb_streams_packets[pkt.stream_index]++;
1875 if (do_read_frames) {
1877 while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1880 av_free_packet(&pkt);
1882 av_init_packet(&pkt);
1885 //Flush remaining frames that are cached in the decoder
1886 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1887 pkt.stream_index = i;
1889 while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1894 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
1895 log_read_interval(interval, NULL, AV_LOG_ERROR);
1900 static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1903 int64_t cur_ts = fmt_ctx->start_time;
1905 if (read_intervals_nb == 0) {
1906 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
1907 ret = read_interval_packets(w, fmt_ctx, &interval, &cur_ts);
1909 for (i = 0; i < read_intervals_nb; i++) {
1910 ret = read_interval_packets(w, fmt_ctx, &read_intervals[i], &cur_ts);
1919 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
1921 AVStream *stream = fmt_ctx->streams[stream_idx];
1922 AVCodecContext *dec_ctx;
1926 AVRational sar, dar;
1930 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1932 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
1934 print_int("index", stream->index);
1936 if ((dec_ctx = stream->codec)) {
1937 const char *profile = NULL;
1938 dec = dec_ctx->codec;
1940 print_str("codec_name", dec->name);
1942 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1943 else print_str_opt("codec_long_name", "unknown");
1946 print_str_opt("codec_name", "unknown");
1948 print_str_opt("codec_long_name", "unknown");
1952 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1953 print_str("profile", profile);
1955 print_str_opt("profile", "unknown");
1957 s = av_get_media_type_string(dec_ctx->codec_type);
1958 if (s) print_str ("codec_type", s);
1959 else print_str_opt("codec_type", "unknown");
1960 print_q("codec_time_base", dec_ctx->time_base, '/');
1962 /* print AVI/FourCC tag */
1963 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1964 print_str("codec_tag_string", val_str);
1965 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1967 switch (dec_ctx->codec_type) {
1968 case AVMEDIA_TYPE_VIDEO:
1969 print_int("width", dec_ctx->width);
1970 print_int("height", dec_ctx->height);
1971 print_int("has_b_frames", dec_ctx->has_b_frames);
1972 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1974 print_q("sample_aspect_ratio", sar, ':');
1975 av_reduce(&dar.num, &dar.den,
1976 dec_ctx->width * sar.num,
1977 dec_ctx->height * sar.den,
1979 print_q("display_aspect_ratio", dar, ':');
1981 print_str_opt("sample_aspect_ratio", "N/A");
1982 print_str_opt("display_aspect_ratio", "N/A");
1984 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1985 if (s) print_str ("pix_fmt", s);
1986 else print_str_opt("pix_fmt", "unknown");
1987 print_int("level", dec_ctx->level);
1988 if (dec_ctx->timecode_frame_start >= 0) {
1989 char tcbuf[AV_TIMECODE_STR_SIZE];
1990 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1991 print_str("timecode", tcbuf);
1993 print_str_opt("timecode", "N/A");
1997 case AVMEDIA_TYPE_AUDIO:
1998 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1999 if (s) print_str ("sample_fmt", s);
2000 else print_str_opt("sample_fmt", "unknown");
2001 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
2002 print_int("channels", dec_ctx->channels);
2004 if (dec_ctx->channel_layout) {
2005 av_bprint_clear(&pbuf);
2006 av_bprint_channel_layout(&pbuf, dec_ctx->channels, dec_ctx->channel_layout);
2007 print_str ("channel_layout", pbuf.str);
2009 print_str_opt("channel_layout", "unknown");
2012 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
2015 case AVMEDIA_TYPE_SUBTITLE:
2017 print_int("width", dec_ctx->width);
2019 print_str_opt("width", "N/A");
2020 if (dec_ctx->height)
2021 print_int("height", dec_ctx->height);
2023 print_str_opt("height", "N/A");
2027 print_str_opt("codec_type", "unknown");
2029 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
2030 const AVOption *opt = NULL;
2031 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
2033 if (opt->flags) continue;
2034 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
2035 print_str(opt->name, str);
2041 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
2042 else print_str_opt("id", "N/A");
2043 print_q("r_frame_rate", stream->r_frame_rate, '/');
2044 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
2045 print_q("time_base", stream->time_base, '/');
2046 print_ts ("start_pts", stream->start_time);
2047 print_time("start_time", stream->start_time, &stream->time_base);
2048 print_ts ("duration_ts", stream->duration);
2049 print_time("duration", stream->duration, &stream->time_base);
2050 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
2051 else print_str_opt("bit_rate", "N/A");
2052 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
2053 else print_str_opt("nb_frames", "N/A");
2054 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
2055 else print_str_opt("nb_read_frames", "N/A");
2056 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
2057 else print_str_opt("nb_read_packets", "N/A");
2059 writer_print_data(w, "extradata", dec_ctx->extradata,
2060 dec_ctx->extradata_size);
2062 /* Print disposition information */
2063 #define PRINT_DISPOSITION(flagname, name) do { \
2064 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
2067 if (do_show_stream_disposition) {
2068 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
2069 PRINT_DISPOSITION(DEFAULT, "default");
2070 PRINT_DISPOSITION(DUB, "dub");
2071 PRINT_DISPOSITION(ORIGINAL, "original");
2072 PRINT_DISPOSITION(COMMENT, "comment");
2073 PRINT_DISPOSITION(LYRICS, "lyrics");
2074 PRINT_DISPOSITION(KARAOKE, "karaoke");
2075 PRINT_DISPOSITION(FORCED, "forced");
2076 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
2077 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
2078 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
2079 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
2080 writer_print_section_footer(w);
2083 ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
2085 writer_print_section_footer(w);
2086 av_bprint_finalize(&pbuf, NULL);
2092 static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
2096 writer_print_section_header(w, SECTION_ID_STREAMS);
2097 for (i = 0; i < fmt_ctx->nb_streams; i++)
2098 if (selected_streams[i]) {
2099 ret = show_stream(w, fmt_ctx, i, 0);
2103 writer_print_section_footer(w);
2108 static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
2112 writer_print_section_header(w, SECTION_ID_PROGRAM);
2113 print_int("program_id", program->id);
2114 print_int("program_num", program->program_num);
2115 print_int("nb_streams", program->nb_stream_indexes);
2116 print_int("pmt_pid", program->pmt_pid);
2117 print_int("pcr_pid", program->pcr_pid);
2118 print_ts("start_pts", program->start_time);
2119 print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
2120 print_ts("end_pts", program->end_time);
2121 print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
2122 ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
2126 writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
2127 for (i = 0; i < program->nb_stream_indexes; i++) {
2128 if (selected_streams[program->stream_index[i]]) {
2129 ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
2134 writer_print_section_footer(w);
2137 writer_print_section_footer(w);
2141 static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
2145 writer_print_section_header(w, SECTION_ID_PROGRAMS);
2146 for (i = 0; i < fmt_ctx->nb_programs; i++) {
2147 AVProgram *program = fmt_ctx->programs[i];
2150 ret = show_program(w, fmt_ctx, program);
2154 writer_print_section_footer(w);
2158 static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
2162 writer_print_section_header(w, SECTION_ID_CHAPTERS);
2163 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
2164 AVChapter *chapter = fmt_ctx->chapters[i];
2166 writer_print_section_header(w, SECTION_ID_CHAPTER);
2167 print_int("id", chapter->id);
2168 print_q ("time_base", chapter->time_base, '/');
2169 print_int("start", chapter->start);
2170 print_time("start_time", chapter->start, &chapter->time_base);
2171 print_int("end", chapter->end);
2172 print_time("end_time", chapter->end, &chapter->time_base);
2173 ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
2174 writer_print_section_footer(w);
2176 writer_print_section_footer(w);
2181 static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
2184 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
2187 writer_print_section_header(w, SECTION_ID_FORMAT);
2188 print_str_validate("filename", fmt_ctx->filename);
2189 print_int("nb_streams", fmt_ctx->nb_streams);
2190 print_int("nb_programs", fmt_ctx->nb_programs);
2191 print_str("format_name", fmt_ctx->iformat->name);
2193 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2194 else print_str_opt("format_long_name", "unknown");
2196 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2197 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2198 if (size >= 0) print_val ("size", size, unit_byte_str);
2199 else print_str_opt("size", "N/A");
2200 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2201 else print_str_opt("bit_rate", "N/A");
2202 print_int("probe_score", av_format_get_probe_score(fmt_ctx));
2203 ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2205 writer_print_section_footer(w);
2210 static void show_error(WriterContext *w, int err)
2213 const char *errbuf_ptr = errbuf;
2215 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2216 errbuf_ptr = strerror(AVUNERROR(err));
2218 writer_print_section_header(w, SECTION_ID_ERROR);
2219 print_int("code", err);
2220 print_str("string", errbuf_ptr);
2221 writer_print_section_footer(w);
2224 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
2226 int err, i, orig_nb_streams;
2227 AVFormatContext *fmt_ctx = NULL;
2228 AVDictionaryEntry *t;
2229 AVDictionary **opts;
2231 if ((err = avformat_open_input(&fmt_ctx, filename,
2232 iformat, &format_opts)) < 0) {
2233 print_error(filename, err);
2236 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2237 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2238 return AVERROR_OPTION_NOT_FOUND;
2241 /* fill the streams in the format context */
2242 opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2243 orig_nb_streams = fmt_ctx->nb_streams;
2245 if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
2246 print_error(filename, err);
2249 for (i = 0; i < orig_nb_streams; i++)
2250 av_dict_free(&opts[i]);
2253 av_dump_format(fmt_ctx, 0, filename, 0);
2255 /* bind a decoder to each input stream */
2256 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2257 AVStream *stream = fmt_ctx->streams[i];
2260 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
2261 av_log(NULL, AV_LOG_WARNING,
2262 "Failed to probe codec for input stream %d\n",
2264 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
2265 av_log(NULL, AV_LOG_WARNING,
2266 "Unsupported codec with id %d for input stream %d\n",
2267 stream->codec->codec_id, stream->index);
2269 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
2270 fmt_ctx, stream, codec);
2271 if (avcodec_open2(stream->codec, codec, &opts) < 0) {
2272 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
2275 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2276 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
2277 t->key, stream->index);
2278 return AVERROR_OPTION_NOT_FOUND;
2283 *fmt_ctx_ptr = fmt_ctx;
2287 static void close_input_file(AVFormatContext **ctx_ptr)
2290 AVFormatContext *fmt_ctx = *ctx_ptr;
2292 /* close decoder for each stream */
2293 for (i = 0; i < fmt_ctx->nb_streams; i++)
2294 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
2295 avcodec_close(fmt_ctx->streams[i]->codec);
2297 avformat_close_input(ctx_ptr);
2300 static int probe_file(WriterContext *wctx, const char *filename)
2302 AVFormatContext *fmt_ctx;
2306 do_read_frames = do_show_frames || do_count_frames;
2307 do_read_packets = do_show_packets || do_count_packets;
2309 ret = open_input_file(&fmt_ctx, filename);
2313 #define CHECK_END if (ret < 0) goto end
2315 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
2316 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
2317 selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
2319 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2320 if (stream_specifier) {
2321 ret = avformat_match_stream_specifier(fmt_ctx,
2322 fmt_ctx->streams[i],
2326 selected_streams[i] = ret;
2329 selected_streams[i] = 1;
2333 if (do_read_frames || do_read_packets) {
2334 if (do_show_frames && do_show_packets &&
2335 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
2336 section_id = SECTION_ID_PACKETS_AND_FRAMES;
2337 else if (do_show_packets && !do_show_frames)
2338 section_id = SECTION_ID_PACKETS;
2339 else // (!do_show_packets && do_show_frames)
2340 section_id = SECTION_ID_FRAMES;
2341 if (do_show_frames || do_show_packets)
2342 writer_print_section_header(wctx, section_id);
2343 ret = read_packets(wctx, fmt_ctx);
2344 if (do_show_frames || do_show_packets)
2345 writer_print_section_footer(wctx);
2348 if (do_show_programs) {
2349 ret = show_programs(wctx, fmt_ctx);
2353 if (do_show_streams) {
2354 ret = show_streams(wctx, fmt_ctx);
2357 if (do_show_chapters) {
2358 ret = show_chapters(wctx, fmt_ctx);
2361 if (do_show_format) {
2362 ret = show_format(wctx, fmt_ctx);
2367 close_input_file(&fmt_ctx);
2368 av_freep(&nb_streams_frames);
2369 av_freep(&nb_streams_packets);
2370 av_freep(&selected_streams);
2375 static void show_usage(void)
2377 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
2378 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
2379 av_log(NULL, AV_LOG_INFO, "\n");
2382 static void ffprobe_show_program_version(WriterContext *w)
2385 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2387 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
2388 print_str("version", FFMPEG_VERSION);
2389 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2390 program_birth_year, this_year);
2391 print_str("build_date", __DATE__);
2392 print_str("build_time", __TIME__);
2393 print_str("compiler_ident", CC_IDENT);
2394 print_str("configuration", FFMPEG_CONFIGURATION);
2395 writer_print_section_footer(w);
2397 av_bprint_finalize(&pbuf, NULL);
2400 #define SHOW_LIB_VERSION(libname, LIBNAME) \
2402 if (CONFIG_##LIBNAME) { \
2403 unsigned int version = libname##_version(); \
2404 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
2405 print_str("name", "lib" #libname); \
2406 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
2407 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
2408 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
2409 print_int("version", version); \
2410 print_str("ident", LIB##LIBNAME##_IDENT); \
2411 writer_print_section_footer(w); \
2415 static void ffprobe_show_library_versions(WriterContext *w)
2417 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
2418 SHOW_LIB_VERSION(avutil, AVUTIL);
2419 SHOW_LIB_VERSION(avcodec, AVCODEC);
2420 SHOW_LIB_VERSION(avformat, AVFORMAT);
2421 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2422 SHOW_LIB_VERSION(avfilter, AVFILTER);
2423 SHOW_LIB_VERSION(swscale, SWSCALE);
2424 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2425 SHOW_LIB_VERSION(postproc, POSTPROC);
2426 writer_print_section_footer(w);
2429 static int opt_format(void *optctx, const char *opt, const char *arg)
2431 iformat = av_find_input_format(arg);
2433 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2434 return AVERROR(EINVAL);
2439 static inline void mark_section_show_entries(SectionID section_id,
2440 int show_all_entries, AVDictionary *entries)
2442 struct section *section = §ions[section_id];
2444 section->show_all_entries = show_all_entries;
2445 if (show_all_entries) {
2447 for (id = section->children_ids; *id != -1; id++)
2448 mark_section_show_entries(*id, show_all_entries, entries);
2450 av_dict_copy(§ion->entries_to_show, entries, 0);
2454 static int match_section(const char *section_name,
2455 int show_all_entries, AVDictionary *entries)
2459 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
2460 const struct section *section = §ions[i];
2461 if (!strcmp(section_name, section->name) ||
2462 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2463 av_log(NULL, AV_LOG_DEBUG,
2464 "'%s' matches section with unique name '%s'\n", section_name,
2465 (char *)av_x_if_null(section->unique_name, section->name));
2467 mark_section_show_entries(section->id, show_all_entries, entries);
2473 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2475 const char *p = arg;
2479 AVDictionary *entries = NULL;
2480 char *section_name = av_get_token(&p, "=:");
2481 int show_all_entries = 0;
2483 if (!section_name) {
2484 av_log(NULL, AV_LOG_ERROR,
2485 "Missing section name for option '%s'\n", opt);
2486 return AVERROR(EINVAL);
2491 while (*p && *p != ':') {
2492 char *entry = av_get_token(&p, ",:");
2495 av_log(NULL, AV_LOG_VERBOSE,
2496 "Adding '%s' to the entries to show in section '%s'\n",
2497 entry, section_name);
2498 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2503 show_all_entries = 1;
2506 ret = match_section(section_name, show_all_entries, entries);
2508 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2509 ret = AVERROR(EINVAL);
2511 av_dict_free(&entries);
2512 av_free(section_name);
2523 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2525 char *buf = av_asprintf("format=%s", arg);
2528 av_log(NULL, AV_LOG_WARNING,
2529 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2531 ret = opt_show_entries(optctx, opt, buf);
2536 static void opt_input_file(void *optctx, const char *arg)
2538 if (input_filename) {
2539 av_log(NULL, AV_LOG_ERROR,
2540 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2541 arg, input_filename);
2544 if (!strcmp(arg, "-"))
2546 input_filename = arg;
2549 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2551 opt_input_file(optctx, arg);
2555 void show_help_default(const char *opt, const char *arg)
2557 av_log_set_callback(log_callback_help);
2559 show_help_options(options, "Main options:", 0, 0, 0);
2562 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2566 * Parse interval specification, according to the format:
2567 * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
2568 * INTERVALS ::= INTERVAL[,INTERVALS]
2570 static int parse_read_interval(const char *interval_spec,
2571 ReadInterval *interval)
2574 char *next, *p, *spec = av_strdup(interval_spec);
2576 return AVERROR(ENOMEM);
2579 av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
2580 ret = AVERROR(EINVAL);
2585 next = strchr(spec, '%');
2589 /* parse first part */
2591 interval->has_start = 1;
2594 interval->start_is_offset = 1;
2597 interval->start_is_offset = 0;
2600 ret = av_parse_time(&interval->start, p, 1);
2602 av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
2606 interval->has_start = 0;
2609 /* parse second part */
2613 interval->has_end = 1;
2616 interval->end_is_offset = 1;
2619 interval->end_is_offset = 0;
2622 if (interval->end_is_offset && *p == '#') {
2625 interval->duration_frames = 1;
2627 lli = strtoll(p, &tail, 10);
2628 if (*tail || lli < 0) {
2629 av_log(NULL, AV_LOG_ERROR,
2630 "Invalid or negative value '%s' for duration number of frames\n", p);
2633 interval->end = lli;
2635 ret = av_parse_time(&us, p, 1);
2637 av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
2643 interval->has_end = 0;
2651 static int parse_read_intervals(const char *intervals_spec)
2654 char *p, *spec = av_strdup(intervals_spec);
2656 return AVERROR(ENOMEM);
2658 /* preparse specification, get number of intervals */
2659 for (n = 0, p = spec; *p; p++)
2664 read_intervals = av_malloc(n * sizeof(*read_intervals));
2665 if (!read_intervals) {
2666 ret = AVERROR(ENOMEM);
2669 read_intervals_nb = n;
2671 /* parse intervals */
2673 for (i = 0; p; i++) {
2676 av_assert0(i < read_intervals_nb);
2677 next = strchr(p, ',');
2681 read_intervals[i].id = i;
2682 ret = parse_read_interval(p, &read_intervals[i]);
2684 av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
2688 av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
2689 log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
2692 av_assert0(i == read_intervals_nb);
2699 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
2701 return parse_read_intervals(arg);
2704 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2706 show_value_unit = 1;
2707 use_value_prefix = 1;
2708 use_byte_value_binary_prefix = 1;
2709 use_value_sexagesimal_format = 1;
2713 static void print_section(SectionID id, int level)
2715 const SectionID *pid;
2716 const struct section *section = §ions[id];
2718 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2719 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2720 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2721 printf("%*c %s", level * 4, ' ', section->name);
2722 if (section->unique_name)
2723 printf("/%s", section->unique_name);
2726 for (pid = section->children_ids; *pid != -1; pid++)
2727 print_section(*pid, level+1);
2730 static int opt_sections(void *optctx, const char *opt, const char *arg)
2732 printf("Sections:\n"
2733 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2734 ".A. = Section contains an array of elements of the same type\n"
2735 "..V = Section may contain a variable number of fields with variable keys\n"
2736 "FLAGS NAME/UNIQUE_NAME\n"
2738 print_section(SECTION_ID_ROOT, 0);
2742 static int opt_show_versions(const char *opt, const char *arg)
2744 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
2745 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
2749 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2750 static int opt_show_##section(const char *opt, const char *arg) \
2752 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2756 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS);
2757 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2758 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2759 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2760 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2761 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2762 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2763 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2764 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS);
2766 static const OptionDef real_options[] = {
2767 #include "cmdutils_common_opts.h"
2768 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2769 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2770 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2771 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2772 "use binary prefixes for byte units" },
2773 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2774 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2775 { "pretty", 0, {.func_arg = opt_pretty},
2776 "prettify the format of displayed values, make it more human readable" },
2777 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2778 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2779 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2780 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2781 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2782 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2783 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2784 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2785 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2786 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2787 "show a particular entry from the format/container info", "entry" },
2788 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2789 "show a set of specified entries", "entry_list" },
2790 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2791 { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
2792 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2793 { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
2794 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2795 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2796 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2797 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2798 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2799 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2800 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2801 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2802 { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
2803 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2804 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2808 static inline int check_section_show_entries(int section_id)
2811 struct section *section = §ions[section_id];
2812 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2814 for (id = section->children_ids; *id != -1; id++)
2815 if (check_section_show_entries(*id))
2820 #define SET_DO_SHOW(id, varname) do { \
2821 if (check_section_show_entries(SECTION_ID_##id)) \
2822 do_show_##varname = 1; \
2825 int main(int argc, char **argv)
2828 WriterContext *wctx;
2830 char *w_name = NULL, *w_args = NULL;
2833 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2834 register_exit(ffprobe_cleanup);
2836 options = real_options;
2837 parse_loglevel(argc, argv, options);
2839 avformat_network_init();
2842 avdevice_register_all();
2845 show_banner(argc, argv, options);
2846 parse_options(NULL, argc, argv, options, opt_input_file);
2848 /* mark things to show, based on -show_entries */
2849 SET_DO_SHOW(CHAPTERS, chapters);
2850 SET_DO_SHOW(ERROR, error);
2851 SET_DO_SHOW(FORMAT, format);
2852 SET_DO_SHOW(FRAMES, frames);
2853 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2854 SET_DO_SHOW(PACKETS, packets);
2855 SET_DO_SHOW(PROGRAM_VERSION, program_version);
2856 SET_DO_SHOW(PROGRAMS, programs);
2857 SET_DO_SHOW(STREAMS, streams);
2858 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2859 SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
2861 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2862 av_log(NULL, AV_LOG_ERROR,
2863 "-bitexact and -show_program_version or -show_library_versions "
2864 "options are incompatible\n");
2865 ret = AVERROR(EINVAL);
2869 writer_register_all();
2872 print_format = av_strdup("default");
2873 if (!print_format) {
2874 ret = AVERROR(ENOMEM);
2877 w_name = av_strtok(print_format, "=", &buf);
2880 w = writer_get_by_name(w_name);
2882 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2883 ret = AVERROR(EINVAL);
2887 if ((ret = writer_open(&wctx, w, w_args,
2888 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2889 if (w == &xml_writer)
2890 wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
2892 writer_print_section_header(wctx, SECTION_ID_ROOT);
2894 if (do_show_program_version)
2895 ffprobe_show_program_version(wctx);
2896 if (do_show_library_versions)
2897 ffprobe_show_library_versions(wctx);
2899 if (!input_filename &&
2900 ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
2901 (!do_show_program_version && !do_show_library_versions))) {
2903 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2904 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2905 ret = AVERROR(EINVAL);
2906 } else if (input_filename) {
2907 ret = probe_file(wctx, input_filename);
2908 if (ret < 0 && do_show_error)
2909 show_error(wctx, ret);
2912 writer_print_section_footer(wctx);
2913 writer_close(&wctx);
2917 av_freep(&print_format);
2918 av_freep(&read_intervals);
2921 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2922 av_dict_free(&(sections[i].entries_to_show));
2924 avformat_network_deinit();