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/timecode.h"
41 #include "libavdevice/avdevice.h"
42 #include "libswscale/swscale.h"
43 #include "libswresample/swresample.h"
44 #include "libpostproc/postprocess.h"
47 const char program_name[] = "ffprobe";
48 const int program_birth_year = 2007;
50 static int do_bitexact = 0;
51 static int do_count_frames = 0;
52 static int do_count_packets = 0;
53 static int do_read_frames = 0;
54 static int do_read_packets = 0;
55 static int do_show_chapters = 0;
56 static int do_show_error = 0;
57 static int do_show_format = 0;
58 static int do_show_frames = 0;
59 static int do_show_packets = 0;
60 static int do_show_streams = 0;
61 static int do_show_stream_disposition = 0;
62 static int do_show_data = 0;
63 static int do_show_program_version = 0;
64 static int do_show_library_versions = 0;
66 static int show_value_unit = 0;
67 static int use_value_prefix = 0;
68 static int use_byte_value_binary_prefix = 0;
69 static int use_value_sexagesimal_format = 0;
70 static int show_private_data = 1;
72 static char *print_format;
73 static char *stream_specifier;
75 /* section structure definition */
77 #define SECTION_MAX_NB_CHILDREN 10
80 int id; ///< unique id identifying a section
83 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
84 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
85 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
86 /// For these sections the element_name field is mandatory.
88 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
89 const char *element_name; ///< name of the contained element, if provided
90 const char *unique_name; ///< unique section name, in case the name is ambiguous
91 AVDictionary *entries_to_show;
98 SECTION_ID_CHAPTER_TAGS,
102 SECTION_ID_FORMAT_TAGS,
105 SECTION_ID_FRAME_TAGS,
106 SECTION_ID_LIBRARY_VERSION,
107 SECTION_ID_LIBRARY_VERSIONS,
110 SECTION_ID_PACKETS_AND_FRAMES,
111 SECTION_ID_PROGRAM_VERSION,
114 SECTION_ID_STREAM_DISPOSITION,
116 SECTION_ID_STREAM_TAGS,
119 static struct section sections[] = {
120 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
121 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
122 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
123 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
124 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
125 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
126 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, -1 } },
127 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
128 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
129 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
130 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
131 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
132 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
133 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
134 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
135 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
136 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_STREAMS, SECTION_ID_PACKETS,
137 SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS, -1} },
138 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
139 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
140 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
141 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
144 static const OptionDef *options;
146 /* FFprobe context */
147 static const char *input_filename;
148 static AVInputFormat *iformat = NULL;
150 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
151 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
153 static const char unit_second_str[] = "s" ;
154 static const char unit_hertz_str[] = "Hz" ;
155 static const char unit_byte_str[] = "byte" ;
156 static const char unit_bit_per_second_str[] = "bit/s";
158 static uint64_t *nb_streams_packets;
159 static uint64_t *nb_streams_frames;
160 static int *selected_streams;
162 static void exit_program(void)
165 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
166 av_dict_free(&(sections[i].entries_to_show));
170 union { double d; long long int i; } val;
174 static char *value_string(char *buf, int buf_size, struct unit_value uv)
180 if (uv.unit == unit_second_str) {
184 vald = vali = uv.val.i;
187 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
191 mins = (int)secs / 60;
192 secs = secs - mins * 60;
195 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
197 const char *prefix_string = "";
199 if (use_value_prefix && vald > 1) {
202 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
203 index = (long long int) (log2(vald)) / 10;
204 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
205 vald /= exp2(index * 10);
206 prefix_string = binary_unit_prefixes[index];
208 index = (long long int) (log10(vald)) / 3;
209 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
210 vald /= pow(10, index * 3);
211 prefix_string = decimal_unit_prefixes[index];
215 if (show_float || (use_value_prefix && vald != (long long int)vald))
216 snprintf(buf, buf_size, "%f", vald);
218 snprintf(buf, buf_size, "%lld", vali);
219 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
220 prefix_string, show_value_unit ? uv.unit : "");
228 typedef struct WriterContext WriterContext;
230 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
231 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
233 typedef struct Writer {
234 const AVClass *priv_class; ///< private class of the writer, if any
235 int priv_size; ///< private size for the writer context
238 int (*init) (WriterContext *wctx);
239 void (*uninit)(WriterContext *wctx);
241 void (*print_section_header)(WriterContext *wctx);
242 void (*print_section_footer)(WriterContext *wctx);
243 void (*print_integer) (WriterContext *wctx, const char *, long long int);
244 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
245 void (*print_string) (WriterContext *wctx, const char *, const char *);
246 int flags; ///< a combination or WRITER_FLAG_*
249 #define SECTION_MAX_NB_LEVELS 10
251 struct WriterContext {
252 const AVClass *class; ///< class of the writer
253 const Writer *writer; ///< the Writer of which this is an instance
254 char *name; ///< name of this writer instance
255 void *priv; ///< private data for use by the filter
257 const struct section *sections; ///< array containing all sections
258 int nb_sections; ///< number of sections
260 int level; ///< current level, starting from 0
262 /** number of the item printed in the given section, starting from 0 */
263 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
265 /** section per each level */
266 const struct section *section[SECTION_MAX_NB_LEVELS];
267 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
268 /// used by various writers
270 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
271 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
272 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
275 static const char *writer_get_name(void *p)
277 WriterContext *wctx = p;
278 return wctx->writer->name;
281 static const AVClass writer_class = {
285 LIBAVUTIL_VERSION_INT,
288 static void writer_close(WriterContext **wctx)
295 if ((*wctx)->writer->uninit)
296 (*wctx)->writer->uninit(*wctx);
297 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
298 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
299 if ((*wctx)->writer->priv_class)
300 av_opt_free((*wctx)->priv);
301 av_freep(&((*wctx)->priv));
305 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
306 const struct section *sections, int nb_sections)
310 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
311 ret = AVERROR(ENOMEM);
315 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
316 ret = AVERROR(ENOMEM);
320 (*wctx)->class = &writer_class;
321 (*wctx)->writer = writer;
323 (*wctx)->sections = sections;
324 (*wctx)->nb_sections = nb_sections;
326 if (writer->priv_class) {
327 void *priv_ctx = (*wctx)->priv;
328 *((const AVClass **)priv_ctx) = writer->priv_class;
329 av_opt_set_defaults(priv_ctx);
332 (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
336 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
337 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
339 if ((*wctx)->writer->init)
340 ret = (*wctx)->writer->init(*wctx);
351 static inline void writer_print_section_header(WriterContext *wctx,
354 int parent_section_id;
356 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
357 parent_section_id = wctx->level ?
358 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
360 wctx->nb_item[wctx->level] = 0;
361 wctx->section[wctx->level] = &wctx->sections[section_id];
363 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
364 wctx->nb_section_packet = wctx->nb_section_frame =
365 wctx->nb_section_packet_frame = 0;
366 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
367 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
368 wctx->nb_section_packet : wctx->nb_section_frame;
371 if (wctx->writer->print_section_header)
372 wctx->writer->print_section_header(wctx);
375 static inline void writer_print_section_footer(WriterContext *wctx)
377 int section_id = wctx->section[wctx->level]->id;
378 int parent_section_id = wctx->level ?
379 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
381 if (parent_section_id != SECTION_ID_NONE)
382 wctx->nb_item[wctx->level-1]++;
383 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
384 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
385 else wctx->nb_section_frame++;
387 if (wctx->writer->print_section_footer)
388 wctx->writer->print_section_footer(wctx);
392 static inline void writer_print_integer(WriterContext *wctx,
393 const char *key, long long int val)
395 const struct section *section = wctx->section[wctx->level];
397 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
398 wctx->writer->print_integer(wctx, key, val);
399 wctx->nb_item[wctx->level]++;
403 static inline void writer_print_string(WriterContext *wctx,
404 const char *key, const char *val, int opt)
406 const struct section *section = wctx->section[wctx->level];
408 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
411 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
412 wctx->writer->print_string(wctx, key, val);
413 wctx->nb_item[wctx->level]++;
417 static inline void writer_print_rational(WriterContext *wctx,
418 const char *key, AVRational q, char sep)
421 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
422 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
423 writer_print_string(wctx, key, buf.str, 0);
426 static void writer_print_time(WriterContext *wctx, const char *key,
427 int64_t ts, const AVRational *time_base, int is_duration)
431 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
432 writer_print_string(wctx, key, "N/A", 1);
434 double d = ts * av_q2d(*time_base);
435 struct unit_value uv;
437 uv.unit = unit_second_str;
438 value_string(buf, sizeof(buf), uv);
439 writer_print_string(wctx, key, buf, 0);
443 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
445 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
446 writer_print_string(wctx, key, "N/A", 1);
448 writer_print_integer(wctx, key, ts);
452 static void writer_print_data(WriterContext *wctx, const char *name,
453 uint8_t *data, int size)
456 int offset = 0, l, i;
458 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
459 av_bprintf(&bp, "\n");
461 av_bprintf(&bp, "%08x: ", offset);
463 for (i = 0; i < l; i++) {
464 av_bprintf(&bp, "%02x", data[i]);
466 av_bprintf(&bp, " ");
468 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
469 for (i = 0; i < l; i++)
470 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
471 av_bprintf(&bp, "\n");
476 writer_print_string(wctx, name, bp.str, 0);
477 av_bprint_finalize(&bp, NULL);
480 #define MAX_REGISTERED_WRITERS_NB 64
482 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
484 static int writer_register(const Writer *writer)
486 static int next_registered_writer_idx = 0;
488 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
489 return AVERROR(ENOMEM);
491 registered_writers[next_registered_writer_idx++] = writer;
495 static const Writer *writer_get_by_name(const char *name)
499 for (i = 0; registered_writers[i]; i++)
500 if (!strcmp(registered_writers[i]->name, name))
501 return registered_writers[i];
509 #define DEFINE_WRITER_CLASS(name) \
510 static const char *name##_get_name(void *ctx) \
514 static const AVClass name##_class = { \
522 typedef struct DefaultContext {
523 const AVClass *class;
525 int noprint_wrappers;
526 int nested_section[SECTION_MAX_NB_LEVELS];
529 #define OFFSET(x) offsetof(DefaultContext, x)
531 static const AVOption default_options[] = {
532 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
533 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
534 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
535 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
539 DEFINE_WRITER_CLASS(default);
541 /* lame uppercasing routine, assumes the string is lower case ASCII */
542 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
545 for (i = 0; src[i] && i < dst_size-1; i++)
546 dst[i] = av_toupper(src[i]);
551 static void default_print_section_header(WriterContext *wctx)
553 DefaultContext *def = wctx->priv;
555 const struct section *section = wctx->section[wctx->level];
556 const struct section *parent_section = wctx->level ?
557 wctx->section[wctx->level-1] : NULL;
559 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
560 if (parent_section &&
561 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
562 def->nested_section[wctx->level] = 1;
563 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
564 wctx->section_pbuf[wctx->level-1].str,
565 upcase_string(buf, sizeof(buf),
566 av_x_if_null(section->element_name, section->name)));
569 if (def->noprint_wrappers || def->nested_section[wctx->level])
572 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
573 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
576 static void default_print_section_footer(WriterContext *wctx)
578 DefaultContext *def = wctx->priv;
579 const struct section *section = wctx->section[wctx->level];
582 if (def->noprint_wrappers || def->nested_section[wctx->level])
585 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
586 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
589 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
591 DefaultContext *def = wctx->priv;
594 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
595 printf("%s\n", value);
598 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
600 DefaultContext *def = wctx->priv;
603 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
604 printf("%lld\n", value);
607 static const Writer default_writer = {
609 .priv_size = sizeof(DefaultContext),
610 .print_section_header = default_print_section_header,
611 .print_section_footer = default_print_section_footer,
612 .print_integer = default_print_int,
613 .print_string = default_print_str,
614 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
615 .priv_class = &default_class,
621 * Apply C-language-like string escaping.
623 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
627 for (p = src; *p; p++) {
629 case '\b': av_bprintf(dst, "%s", "\\b"); break;
630 case '\f': av_bprintf(dst, "%s", "\\f"); break;
631 case '\n': av_bprintf(dst, "%s", "\\n"); break;
632 case '\r': av_bprintf(dst, "%s", "\\r"); break;
633 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
636 av_bprint_chars(dst, '\\', 1);
637 av_bprint_chars(dst, *p, 1);
644 * Quote fields containing special characters, check RFC4180.
646 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
648 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
649 int needs_quoting = !!src[strcspn(src, meta_chars)];
652 av_bprint_chars(dst, '"', 1);
654 for (; *src; src++) {
656 av_bprint_chars(dst, '"', 1);
657 av_bprint_chars(dst, *src, 1);
660 av_bprint_chars(dst, '"', 1);
664 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
669 typedef struct CompactContext {
670 const AVClass *class;
675 char *escape_mode_str;
676 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
677 int nested_section[SECTION_MAX_NB_LEVELS];
681 #define OFFSET(x) offsetof(CompactContext, x)
683 static const AVOption compact_options[]= {
684 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
685 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
686 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
687 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
688 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
689 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
690 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
691 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
695 DEFINE_WRITER_CLASS(compact);
697 static av_cold int compact_init(WriterContext *wctx)
699 CompactContext *compact = wctx->priv;
701 if (strlen(compact->item_sep_str) != 1) {
702 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
703 compact->item_sep_str);
704 return AVERROR(EINVAL);
706 compact->item_sep = compact->item_sep_str[0];
708 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
709 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
710 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
712 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
713 return AVERROR(EINVAL);
719 static void compact_print_section_header(WriterContext *wctx)
721 CompactContext *compact = wctx->priv;
722 const struct section *section = wctx->section[wctx->level];
723 const struct section *parent_section = wctx->level ?
724 wctx->section[wctx->level-1] : NULL;
726 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
727 if (parent_section &&
728 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
729 compact->nested_section[wctx->level] = 1;
730 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
731 wctx->section_pbuf[wctx->level-1].str,
732 (char *)av_x_if_null(section->element_name, section->name));
733 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
734 } else if (compact->print_section &&
735 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
736 printf("%s%c", section->name, compact->item_sep);
739 static void compact_print_section_footer(WriterContext *wctx)
741 CompactContext *compact = wctx->priv;
743 if (!compact->nested_section[wctx->level] &&
744 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
748 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
750 CompactContext *compact = wctx->priv;
753 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
755 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
756 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
757 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
758 av_bprint_finalize(&buf, NULL);
761 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
763 CompactContext *compact = wctx->priv;
765 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
767 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
768 printf("%lld", value);
771 static const Writer compact_writer = {
773 .priv_size = sizeof(CompactContext),
774 .init = compact_init,
775 .print_section_header = compact_print_section_header,
776 .print_section_footer = compact_print_section_footer,
777 .print_integer = compact_print_int,
778 .print_string = compact_print_str,
779 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
780 .priv_class = &compact_class,
786 #define OFFSET(x) offsetof(CompactContext, x)
788 static const AVOption csv_options[] = {
789 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
790 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
791 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
792 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
793 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
794 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
795 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
796 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
800 DEFINE_WRITER_CLASS(csv);
802 static const Writer csv_writer = {
804 .priv_size = sizeof(CompactContext),
805 .init = compact_init,
806 .print_section_header = compact_print_section_header,
807 .print_section_footer = compact_print_section_footer,
808 .print_integer = compact_print_int,
809 .print_string = compact_print_str,
810 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
811 .priv_class = &csv_class,
816 typedef struct FlatContext {
817 const AVClass *class;
824 #define OFFSET(x) offsetof(FlatContext, x)
826 static const AVOption flat_options[]= {
827 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
828 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
829 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
830 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
834 DEFINE_WRITER_CLASS(flat);
836 static av_cold int flat_init(WriterContext *wctx)
838 FlatContext *flat = wctx->priv;
840 if (strlen(flat->sep_str) != 1) {
841 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
843 return AVERROR(EINVAL);
845 flat->sep = flat->sep_str[0];
850 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
854 for (p = src; *p; p++) {
855 if (!((*p >= '0' && *p <= '9') ||
856 (*p >= 'a' && *p <= 'z') ||
857 (*p >= 'A' && *p <= 'Z')))
858 av_bprint_chars(dst, '_', 1);
860 av_bprint_chars(dst, *p, 1);
865 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
869 for (p = src; *p; p++) {
871 case '\n': av_bprintf(dst, "%s", "\\n"); break;
872 case '\r': av_bprintf(dst, "%s", "\\r"); break;
873 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
874 case '"': av_bprintf(dst, "%s", "\\\""); break;
875 case '`': av_bprintf(dst, "%s", "\\`"); break;
876 case '$': av_bprintf(dst, "%s", "\\$"); break;
877 default: av_bprint_chars(dst, *p, 1); break;
883 static void flat_print_section_header(WriterContext *wctx)
885 FlatContext *flat = wctx->priv;
886 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
887 const struct section *section = wctx->section[wctx->level];
888 const struct section *parent_section = wctx->level ?
889 wctx->section[wctx->level-1] : NULL;
891 /* build section header */
892 av_bprint_clear(buf);
895 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
897 if (flat->hierarchical ||
898 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
899 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
901 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
902 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
903 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
904 av_bprintf(buf, "%d%s", n, flat->sep_str);
909 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
911 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
914 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
916 FlatContext *flat = wctx->priv;
919 printf("%s", wctx->section_pbuf[wctx->level].str);
920 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
921 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
922 av_bprint_clear(&buf);
923 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
924 av_bprint_finalize(&buf, NULL);
927 static const Writer flat_writer = {
929 .priv_size = sizeof(FlatContext),
931 .print_section_header = flat_print_section_header,
932 .print_integer = flat_print_int,
933 .print_string = flat_print_str,
934 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
935 .priv_class = &flat_class,
938 /* INI format output */
941 const AVClass *class;
946 #define OFFSET(x) offsetof(INIContext, x)
948 static const AVOption ini_options[] = {
949 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
950 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
954 DEFINE_WRITER_CLASS(ini);
956 static char *ini_escape_str(AVBPrint *dst, const char *src)
961 while (c = src[i++]) {
963 case '\b': av_bprintf(dst, "%s", "\\b"); break;
964 case '\f': av_bprintf(dst, "%s", "\\f"); break;
965 case '\n': av_bprintf(dst, "%s", "\\n"); break;
966 case '\r': av_bprintf(dst, "%s", "\\r"); break;
967 case '\t': av_bprintf(dst, "%s", "\\t"); break;
971 case ':' : av_bprint_chars(dst, '\\', 1);
973 if ((unsigned char)c < 32)
974 av_bprintf(dst, "\\x00%02x", c & 0xff);
976 av_bprint_chars(dst, c, 1);
983 static void ini_print_section_header(WriterContext *wctx)
985 INIContext *ini = wctx->priv;
986 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
987 const struct section *section = wctx->section[wctx->level];
988 const struct section *parent_section = wctx->level ?
989 wctx->section[wctx->level-1] : NULL;
991 av_bprint_clear(buf);
992 if (!parent_section) {
993 printf("# ffprobe output\n\n");
997 if (wctx->nb_item[wctx->level-1])
1000 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1001 if (ini->hierarchical ||
1002 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1003 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1005 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1006 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1007 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1008 av_bprintf(buf, ".%d", n);
1012 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1013 printf("[%s]\n", buf->str);
1016 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1020 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1021 printf("%s=", ini_escape_str(&buf, key));
1022 av_bprint_clear(&buf);
1023 printf("%s\n", ini_escape_str(&buf, value));
1024 av_bprint_finalize(&buf, NULL);
1027 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1029 printf("%s=%lld\n", key, value);
1032 static const Writer ini_writer = {
1034 .priv_size = sizeof(INIContext),
1035 .print_section_header = ini_print_section_header,
1036 .print_integer = ini_print_int,
1037 .print_string = ini_print_str,
1038 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1039 .priv_class = &ini_class,
1045 const AVClass *class;
1048 const char *item_sep, *item_start_end;
1052 #define OFFSET(x) offsetof(JSONContext, x)
1054 static const AVOption json_options[]= {
1055 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1056 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1060 DEFINE_WRITER_CLASS(json);
1062 static av_cold int json_init(WriterContext *wctx)
1064 JSONContext *json = wctx->priv;
1066 json->item_sep = json->compact ? ", " : ",\n";
1067 json->item_start_end = json->compact ? " " : "\n";
1072 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1074 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1075 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1078 for (p = src; *p; p++) {
1079 char *s = strchr(json_escape, *p);
1081 av_bprint_chars(dst, '\\', 1);
1082 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1083 } else if ((unsigned char)*p < 32) {
1084 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1086 av_bprint_chars(dst, *p, 1);
1092 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1094 static void json_print_section_header(WriterContext *wctx)
1096 JSONContext *json = wctx->priv;
1098 const struct section *section = wctx->section[wctx->level];
1099 const struct section *parent_section = wctx->level ?
1100 wctx->section[wctx->level-1] : NULL;
1102 if (wctx->level && wctx->nb_item[wctx->level-1])
1105 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1107 json->indent_level++;
1109 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1110 json_escape_str(&buf, section->name, wctx);
1113 json->indent_level++;
1114 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1115 printf("\"%s\": [\n", buf.str);
1116 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1117 printf("\"%s\": {%s", buf.str, json->item_start_end);
1119 printf("{%s", json->item_start_end);
1121 /* this is required so the parser can distinguish between packets and frames */
1122 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1125 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1128 av_bprint_finalize(&buf, NULL);
1132 static void json_print_section_footer(WriterContext *wctx)
1134 JSONContext *json = wctx->priv;
1135 const struct section *section = wctx->section[wctx->level];
1137 if (wctx->level == 0) {
1138 json->indent_level--;
1140 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1142 json->indent_level--;
1146 printf("%s", json->item_start_end);
1147 json->indent_level--;
1154 static inline void json_print_item_str(WriterContext *wctx,
1155 const char *key, const char *value)
1159 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1160 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1161 av_bprint_clear(&buf);
1162 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1163 av_bprint_finalize(&buf, NULL);
1166 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1168 JSONContext *json = wctx->priv;
1170 if (wctx->nb_item[wctx->level])
1171 printf("%s", json->item_sep);
1174 json_print_item_str(wctx, key, value);
1177 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1179 JSONContext *json = wctx->priv;
1182 if (wctx->nb_item[wctx->level])
1183 printf("%s", json->item_sep);
1187 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1188 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1189 av_bprint_finalize(&buf, NULL);
1192 static const Writer json_writer = {
1194 .priv_size = sizeof(JSONContext),
1196 .print_section_header = json_print_section_header,
1197 .print_section_footer = json_print_section_footer,
1198 .print_integer = json_print_int,
1199 .print_string = json_print_str,
1200 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1201 .priv_class = &json_class,
1207 const AVClass *class;
1210 int fully_qualified;
1215 #define OFFSET(x) offsetof(XMLContext, x)
1217 static const AVOption xml_options[] = {
1218 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1219 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1220 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1221 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1225 DEFINE_WRITER_CLASS(xml);
1227 static av_cold int xml_init(WriterContext *wctx)
1229 XMLContext *xml = wctx->priv;
1231 if (xml->xsd_strict) {
1232 xml->fully_qualified = 1;
1233 #define CHECK_COMPLIANCE(opt, opt_name) \
1235 av_log(wctx, AV_LOG_ERROR, \
1236 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1237 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1238 return AVERROR(EINVAL); \
1240 CHECK_COMPLIANCE(show_private_data, "private");
1241 CHECK_COMPLIANCE(show_value_unit, "unit");
1242 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1244 if (do_show_frames && do_show_packets) {
1245 av_log(wctx, AV_LOG_ERROR,
1246 "Interleaved frames and packets are not allowed in XSD. "
1247 "Select only one between the -show_frames and the -show_packets options.\n");
1248 return AVERROR(EINVAL);
1255 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1259 for (p = src; *p; p++) {
1261 case '&' : av_bprintf(dst, "%s", "&"); break;
1262 case '<' : av_bprintf(dst, "%s", "<"); break;
1263 case '>' : av_bprintf(dst, "%s", ">"); break;
1264 case '"' : av_bprintf(dst, "%s", """); break;
1265 case '\'': av_bprintf(dst, "%s", "'"); break;
1266 default: av_bprint_chars(dst, *p, 1);
1273 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1275 static void xml_print_section_header(WriterContext *wctx)
1277 XMLContext *xml = wctx->priv;
1278 const struct section *section = wctx->section[wctx->level];
1279 const struct section *parent_section = wctx->level ?
1280 wctx->section[wctx->level-1] : NULL;
1282 if (wctx->level == 0) {
1283 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1284 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1285 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1287 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1288 printf("<%sffprobe%s>\n",
1289 xml->fully_qualified ? "ffprobe:" : "",
1290 xml->fully_qualified ? qual : "");
1294 if (xml->within_tag) {
1295 xml->within_tag = 0;
1298 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1299 xml->indent_level++;
1301 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1302 wctx->level && wctx->nb_item[wctx->level-1])
1304 xml->indent_level++;
1306 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1307 XML_INDENT(); printf("<%s>\n", section->name);
1309 XML_INDENT(); printf("<%s ", section->name);
1310 xml->within_tag = 1;
1315 static void xml_print_section_footer(WriterContext *wctx)
1317 XMLContext *xml = wctx->priv;
1318 const struct section *section = wctx->section[wctx->level];
1320 if (wctx->level == 0) {
1321 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1322 } else if (xml->within_tag) {
1323 xml->within_tag = 0;
1325 xml->indent_level--;
1326 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1327 xml->indent_level--;
1329 XML_INDENT(); printf("</%s>\n", section->name);
1330 xml->indent_level--;
1334 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1337 XMLContext *xml = wctx->priv;
1338 const struct section *section = wctx->section[wctx->level];
1340 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1342 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1344 printf("<%s key=\"%s\"",
1345 section->element_name, xml_escape_str(&buf, key, wctx));
1346 av_bprint_clear(&buf);
1347 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1349 if (wctx->nb_item[wctx->level])
1351 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1354 av_bprint_finalize(&buf, NULL);
1357 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1359 if (wctx->nb_item[wctx->level])
1361 printf("%s=\"%lld\"", key, value);
1364 static Writer xml_writer = {
1366 .priv_size = sizeof(XMLContext),
1368 .print_section_header = xml_print_section_header,
1369 .print_section_footer = xml_print_section_footer,
1370 .print_integer = xml_print_int,
1371 .print_string = xml_print_str,
1372 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1373 .priv_class = &xml_class,
1376 static void writer_register_all(void)
1378 static int initialized;
1384 writer_register(&default_writer);
1385 writer_register(&compact_writer);
1386 writer_register(&csv_writer);
1387 writer_register(&flat_writer);
1388 writer_register(&ini_writer);
1389 writer_register(&json_writer);
1390 writer_register(&xml_writer);
1393 #define print_fmt(k, f, ...) do { \
1394 av_bprint_clear(&pbuf); \
1395 av_bprintf(&pbuf, f, __VA_ARGS__); \
1396 writer_print_string(w, k, pbuf.str, 0); \
1399 #define print_int(k, v) writer_print_integer(w, k, v)
1400 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1401 #define print_str(k, v) writer_print_string(w, k, v, 0)
1402 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1403 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1404 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1405 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1406 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1407 #define print_val(k, v, u) do { \
1408 struct unit_value uv; \
1411 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1414 #define print_section_header(s) writer_print_section_header(w, s)
1415 #define print_section_footer(s) writer_print_section_footer(w, s)
1417 static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
1419 AVDictionaryEntry *tag = NULL;
1423 writer_print_section_header(wctx, section_id);
1424 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
1425 writer_print_string(wctx, tag->key, tag->value, 0);
1426 writer_print_section_footer(wctx);
1429 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1432 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1436 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1438 writer_print_section_header(w, SECTION_ID_PACKET);
1440 s = av_get_media_type_string(st->codec->codec_type);
1441 if (s) print_str ("codec_type", s);
1442 else print_str_opt("codec_type", "unknown");
1443 print_int("stream_index", pkt->stream_index);
1444 print_ts ("pts", pkt->pts);
1445 print_time("pts_time", pkt->pts, &st->time_base);
1446 print_ts ("dts", pkt->dts);
1447 print_time("dts_time", pkt->dts, &st->time_base);
1448 print_duration_ts("duration", pkt->duration);
1449 print_duration_time("duration_time", pkt->duration, &st->time_base);
1450 print_duration_ts("convergence_duration", pkt->convergence_duration);
1451 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1452 print_val("size", pkt->size, unit_byte_str);
1453 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1454 else print_str_opt("pos", "N/A");
1455 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1457 writer_print_data(w, "data", pkt->data, pkt->size);
1458 writer_print_section_footer(w);
1460 av_bprint_finalize(&pbuf, NULL);
1464 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1465 AVFormatContext *fmt_ctx)
1470 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1472 writer_print_section_header(w, SECTION_ID_FRAME);
1474 s = av_get_media_type_string(stream->codec->codec_type);
1475 if (s) print_str ("media_type", s);
1476 else print_str_opt("media_type", "unknown");
1477 print_int("key_frame", frame->key_frame);
1478 print_ts ("pkt_pts", frame->pkt_pts);
1479 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1480 print_ts ("pkt_dts", frame->pkt_dts);
1481 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1482 print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1483 print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1484 if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1485 else print_str_opt("pkt_pos", "N/A");
1486 if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1487 else print_str_opt("pkt_size", "N/A");
1489 switch (stream->codec->codec_type) {
1492 case AVMEDIA_TYPE_VIDEO:
1493 print_int("width", frame->width);
1494 print_int("height", frame->height);
1495 s = av_get_pix_fmt_name(frame->format);
1496 if (s) print_str ("pix_fmt", s);
1497 else print_str_opt("pix_fmt", "unknown");
1498 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1500 print_q("sample_aspect_ratio", sar, ':');
1502 print_str_opt("sample_aspect_ratio", "N/A");
1504 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1505 print_int("coded_picture_number", frame->coded_picture_number);
1506 print_int("display_picture_number", frame->display_picture_number);
1507 print_int("interlaced_frame", frame->interlaced_frame);
1508 print_int("top_field_first", frame->top_field_first);
1509 print_int("repeat_pict", frame->repeat_pict);
1512 case AVMEDIA_TYPE_AUDIO:
1513 s = av_get_sample_fmt_name(frame->format);
1514 if (s) print_str ("sample_fmt", s);
1515 else print_str_opt("sample_fmt", "unknown");
1516 print_int("nb_samples", frame->nb_samples);
1517 print_int("channels", av_frame_get_channels(frame));
1518 if (av_frame_get_channel_layout(frame)) {
1519 av_bprint_clear(&pbuf);
1520 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1521 av_frame_get_channel_layout(frame));
1522 print_str ("channel_layout", pbuf.str);
1524 print_str_opt("channel_layout", "unknown");
1527 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1529 writer_print_section_footer(w);
1531 av_bprint_finalize(&pbuf, NULL);
1535 static av_always_inline int process_frame(WriterContext *w,
1536 AVFormatContext *fmt_ctx,
1537 AVFrame *frame, AVPacket *pkt)
1539 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1540 int ret = 0, got_frame = 0;
1542 avcodec_get_frame_defaults(frame);
1543 if (dec_ctx->codec) {
1544 switch (dec_ctx->codec_type) {
1545 case AVMEDIA_TYPE_VIDEO:
1546 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1549 case AVMEDIA_TYPE_AUDIO:
1550 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1557 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1561 nb_streams_frames[pkt->stream_index]++;
1563 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1568 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1574 av_init_packet(&pkt);
1576 while (!av_read_frame(fmt_ctx, &pkt)) {
1577 if (selected_streams[pkt.stream_index]) {
1578 if (do_read_packets) {
1579 if (do_show_packets)
1580 show_packet(w, fmt_ctx, &pkt, i++);
1581 nb_streams_packets[pkt.stream_index]++;
1583 if (do_read_frames) {
1585 while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1588 av_free_packet(&pkt);
1590 av_init_packet(&pkt);
1593 //Flush remaining frames that are cached in the decoder
1594 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1595 pkt.stream_index = i;
1597 while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1601 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1603 AVStream *stream = fmt_ctx->streams[stream_idx];
1604 AVCodecContext *dec_ctx;
1608 AVRational sar, dar;
1611 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1613 writer_print_section_header(w, SECTION_ID_STREAM);
1615 print_int("index", stream->index);
1617 if ((dec_ctx = stream->codec)) {
1618 const char *profile = NULL;
1619 dec = dec_ctx->codec;
1621 print_str("codec_name", dec->name);
1623 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1624 else print_str_opt("codec_long_name", "unknown");
1627 print_str_opt("codec_name", "unknown");
1629 print_str_opt("codec_long_name", "unknown");
1633 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1634 print_str("profile", profile);
1636 print_str_opt("profile", "unknown");
1638 s = av_get_media_type_string(dec_ctx->codec_type);
1639 if (s) print_str ("codec_type", s);
1640 else print_str_opt("codec_type", "unknown");
1641 print_q("codec_time_base", dec_ctx->time_base, '/');
1643 /* print AVI/FourCC tag */
1644 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1645 print_str("codec_tag_string", val_str);
1646 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1648 switch (dec_ctx->codec_type) {
1649 case AVMEDIA_TYPE_VIDEO:
1650 print_int("width", dec_ctx->width);
1651 print_int("height", dec_ctx->height);
1652 print_int("has_b_frames", dec_ctx->has_b_frames);
1653 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1655 print_q("sample_aspect_ratio", sar, ':');
1656 av_reduce(&dar.num, &dar.den,
1657 dec_ctx->width * sar.num,
1658 dec_ctx->height * sar.den,
1660 print_q("display_aspect_ratio", dar, ':');
1662 print_str_opt("sample_aspect_ratio", "N/A");
1663 print_str_opt("display_aspect_ratio", "N/A");
1665 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1666 if (s) print_str ("pix_fmt", s);
1667 else print_str_opt("pix_fmt", "unknown");
1668 print_int("level", dec_ctx->level);
1669 if (dec_ctx->timecode_frame_start >= 0) {
1670 char tcbuf[AV_TIMECODE_STR_SIZE];
1671 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1672 print_str("timecode", tcbuf);
1674 print_str_opt("timecode", "N/A");
1678 case AVMEDIA_TYPE_AUDIO:
1679 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1680 if (s) print_str ("sample_fmt", s);
1681 else print_str_opt("sample_fmt", "unknown");
1682 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1683 print_int("channels", dec_ctx->channels);
1684 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1688 print_str_opt("codec_type", "unknown");
1690 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1691 const AVOption *opt = NULL;
1692 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1694 if (opt->flags) continue;
1695 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1696 print_str(opt->name, str);
1702 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1703 else print_str_opt("id", "N/A");
1704 print_q("r_frame_rate", stream->r_frame_rate, '/');
1705 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1706 print_q("time_base", stream->time_base, '/');
1707 print_ts ("start_pts", stream->start_time);
1708 print_time("start_time", stream->start_time, &stream->time_base);
1709 print_ts ("duration_ts", stream->duration);
1710 print_time("duration", stream->duration, &stream->time_base);
1711 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1712 else print_str_opt("bit_rate", "N/A");
1713 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1714 else print_str_opt("nb_frames", "N/A");
1715 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1716 else print_str_opt("nb_read_frames", "N/A");
1717 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1718 else print_str_opt("nb_read_packets", "N/A");
1720 writer_print_data(w, "extradata", dec_ctx->extradata,
1721 dec_ctx->extradata_size);
1723 /* Print disposition information */
1724 #define PRINT_DISPOSITION(flagname, name) do { \
1725 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
1728 if (do_show_stream_disposition) {
1729 writer_print_section_header(w, SECTION_ID_STREAM_DISPOSITION);
1730 PRINT_DISPOSITION(DEFAULT, "default");
1731 PRINT_DISPOSITION(DUB, "dub");
1732 PRINT_DISPOSITION(ORIGINAL, "original");
1733 PRINT_DISPOSITION(COMMENT, "comment");
1734 PRINT_DISPOSITION(LYRICS, "lyrics");
1735 PRINT_DISPOSITION(KARAOKE, "karaoke");
1736 PRINT_DISPOSITION(FORCED, "forced");
1737 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
1738 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
1739 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
1740 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
1741 writer_print_section_footer(w);
1744 show_tags(w, stream->metadata, SECTION_ID_STREAM_TAGS);
1746 writer_print_section_footer(w);
1747 av_bprint_finalize(&pbuf, NULL);
1751 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1754 writer_print_section_header(w, SECTION_ID_STREAMS);
1755 for (i = 0; i < fmt_ctx->nb_streams; i++)
1756 if (selected_streams[i])
1757 show_stream(w, fmt_ctx, i);
1758 writer_print_section_footer(w);
1761 static void show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
1765 writer_print_section_header(w, SECTION_ID_CHAPTERS);
1766 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
1767 AVChapter *chapter = fmt_ctx->chapters[i];
1769 writer_print_section_header(w, SECTION_ID_CHAPTER);
1770 print_int("id", chapter->id);
1771 print_q ("time_base", chapter->time_base, '/');
1772 print_int("start", chapter->start);
1773 print_time("start_time", chapter->start, &chapter->time_base);
1774 print_int("end", chapter->end);
1775 print_time("end_time", chapter->end, &chapter->time_base);
1776 show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
1777 writer_print_section_footer(w);
1779 writer_print_section_footer(w);
1782 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1785 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1787 writer_print_section_header(w, SECTION_ID_FORMAT);
1788 print_str("filename", fmt_ctx->filename);
1789 print_int("nb_streams", fmt_ctx->nb_streams);
1790 print_str("format_name", fmt_ctx->iformat->name);
1792 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
1793 else print_str_opt("format_long_name", "unknown");
1795 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1796 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1797 if (size >= 0) print_val ("size", size, unit_byte_str);
1798 else print_str_opt("size", "N/A");
1799 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1800 else print_str_opt("bit_rate", "N/A");
1801 show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
1803 writer_print_section_footer(w);
1807 static void show_error(WriterContext *w, int err)
1810 const char *errbuf_ptr = errbuf;
1812 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1813 errbuf_ptr = strerror(AVUNERROR(err));
1815 writer_print_section_header(w, SECTION_ID_ERROR);
1816 print_int("code", err);
1817 print_str("string", errbuf_ptr);
1818 writer_print_section_footer(w);
1821 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1823 int err, i, orig_nb_streams;
1824 AVFormatContext *fmt_ctx = NULL;
1825 AVDictionaryEntry *t;
1826 AVDictionary **opts;
1828 if ((err = avformat_open_input(&fmt_ctx, filename,
1829 iformat, &format_opts)) < 0) {
1830 print_error(filename, err);
1833 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1834 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1835 return AVERROR_OPTION_NOT_FOUND;
1838 /* fill the streams in the format context */
1839 opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
1840 orig_nb_streams = fmt_ctx->nb_streams;
1842 if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
1843 print_error(filename, err);
1846 for (i = 0; i < orig_nb_streams; i++)
1847 av_dict_free(&opts[i]);
1850 av_dump_format(fmt_ctx, 0, filename, 0);
1852 /* bind a decoder to each input stream */
1853 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1854 AVStream *stream = fmt_ctx->streams[i];
1857 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
1858 av_log(NULL, AV_LOG_ERROR,
1859 "Failed to probe codec for input stream %d\n",
1861 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1862 av_log(NULL, AV_LOG_ERROR,
1863 "Unsupported codec with id %d for input stream %d\n",
1864 stream->codec->codec_id, stream->index);
1866 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
1867 fmt_ctx, stream, codec);
1868 if (avcodec_open2(stream->codec, codec, &opts) < 0) {
1869 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1872 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1873 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
1874 t->key, stream->index);
1875 return AVERROR_OPTION_NOT_FOUND;
1880 *fmt_ctx_ptr = fmt_ctx;
1884 static void close_input_file(AVFormatContext **ctx_ptr)
1887 AVFormatContext *fmt_ctx = *ctx_ptr;
1889 /* close decoder for each stream */
1890 for (i = 0; i < fmt_ctx->nb_streams; i++)
1891 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
1892 avcodec_close(fmt_ctx->streams[i]->codec);
1894 avformat_close_input(ctx_ptr);
1897 static int probe_file(WriterContext *wctx, const char *filename)
1899 AVFormatContext *fmt_ctx;
1903 do_read_frames = do_show_frames || do_count_frames;
1904 do_read_packets = do_show_packets || do_count_packets;
1906 ret = open_input_file(&fmt_ctx, filename);
1908 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1909 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1910 selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
1912 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1913 if (stream_specifier) {
1914 ret = avformat_match_stream_specifier(fmt_ctx,
1915 fmt_ctx->streams[i],
1920 selected_streams[i] = ret;
1922 selected_streams[i] = 1;
1926 if (do_read_frames || do_read_packets) {
1927 if (do_show_frames && do_show_packets &&
1928 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1929 section_id = SECTION_ID_PACKETS_AND_FRAMES;
1930 else if (do_show_packets && !do_show_frames)
1931 section_id = SECTION_ID_PACKETS;
1932 else // (!do_show_packets && do_show_frames)
1933 section_id = SECTION_ID_FRAMES;
1934 if (do_show_frames || do_show_packets)
1935 writer_print_section_header(wctx, section_id);
1936 read_packets(wctx, fmt_ctx);
1937 if (do_show_frames || do_show_packets)
1938 writer_print_section_footer(wctx);
1940 if (do_show_streams)
1941 show_streams(wctx, fmt_ctx);
1942 if (do_show_chapters)
1943 show_chapters(wctx, fmt_ctx);
1945 show_format(wctx, fmt_ctx);
1948 close_input_file(&fmt_ctx);
1949 av_freep(&nb_streams_frames);
1950 av_freep(&nb_streams_packets);
1951 av_freep(&selected_streams);
1956 static void show_usage(void)
1958 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1959 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1960 av_log(NULL, AV_LOG_INFO, "\n");
1963 static void ffprobe_show_program_version(WriterContext *w)
1966 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1968 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
1969 print_str("version", FFMPEG_VERSION);
1970 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1971 program_birth_year, this_year);
1972 print_str("build_date", __DATE__);
1973 print_str("build_time", __TIME__);
1974 print_str("compiler_ident", CC_IDENT);
1975 print_str("configuration", FFMPEG_CONFIGURATION);
1976 writer_print_section_footer(w);
1978 av_bprint_finalize(&pbuf, NULL);
1981 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1983 if (CONFIG_##LIBNAME) { \
1984 unsigned int version = libname##_version(); \
1985 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
1986 print_str("name", "lib" #libname); \
1987 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1988 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1989 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1990 print_int("version", version); \
1991 print_str("ident", LIB##LIBNAME##_IDENT); \
1992 writer_print_section_footer(w); \
1996 static void ffprobe_show_library_versions(WriterContext *w)
1998 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
1999 SHOW_LIB_VERSION(avutil, AVUTIL);
2000 SHOW_LIB_VERSION(avcodec, AVCODEC);
2001 SHOW_LIB_VERSION(avformat, AVFORMAT);
2002 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2003 SHOW_LIB_VERSION(avfilter, AVFILTER);
2004 SHOW_LIB_VERSION(swscale, SWSCALE);
2005 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2006 SHOW_LIB_VERSION(postproc, POSTPROC);
2007 writer_print_section_footer(w);
2010 static int opt_format(void *optctx, const char *opt, const char *arg)
2012 iformat = av_find_input_format(arg);
2014 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2015 return AVERROR(EINVAL);
2020 static inline void mark_section_show_entries(SectionID section_id,
2021 int show_all_entries, AVDictionary *entries)
2023 struct section *section = §ions[section_id];
2025 section->show_all_entries = show_all_entries;
2026 if (show_all_entries) {
2028 for (id = section->children_ids; *id != -1; id++)
2029 mark_section_show_entries(*id, show_all_entries, entries);
2031 av_dict_copy(§ion->entries_to_show, entries, 0);
2035 static int match_section(const char *section_name,
2036 int show_all_entries, AVDictionary *entries)
2040 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
2041 const struct section *section = §ions[i];
2042 if (!strcmp(section_name, section->name) ||
2043 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2044 av_log(NULL, AV_LOG_DEBUG,
2045 "'%s' matches section with unique name '%s'\n", section_name,
2046 (char *)av_x_if_null(section->unique_name, section->name));
2048 mark_section_show_entries(section->id, show_all_entries, entries);
2054 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2056 const char *p = arg;
2060 AVDictionary *entries = NULL;
2061 char *section_name = av_get_token(&p, "=:");
2062 int show_all_entries = 0;
2064 if (!section_name) {
2065 av_log(NULL, AV_LOG_ERROR,
2066 "Missing section name for option '%s'\n", opt);
2067 return AVERROR(EINVAL);
2072 while (*p && *p != ':') {
2073 char *entry = av_get_token(&p, ",:");
2076 av_log(NULL, AV_LOG_VERBOSE,
2077 "Adding '%s' to the entries to show in section '%s'\n",
2078 entry, section_name);
2079 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2084 show_all_entries = 1;
2087 ret = match_section(section_name, show_all_entries, entries);
2089 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2090 ret = AVERROR(EINVAL);
2092 av_dict_free(&entries);
2093 av_free(section_name);
2104 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2106 char *buf = av_asprintf("format=%s", arg);
2109 av_log(NULL, AV_LOG_WARNING,
2110 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2112 ret = opt_show_entries(optctx, opt, buf);
2117 static void opt_input_file(void *optctx, const char *arg)
2119 if (input_filename) {
2120 av_log(NULL, AV_LOG_ERROR,
2121 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2122 arg, input_filename);
2125 if (!strcmp(arg, "-"))
2127 input_filename = arg;
2130 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2132 opt_input_file(optctx, arg);
2136 void show_help_default(const char *opt, const char *arg)
2138 av_log_set_callback(log_callback_help);
2140 show_help_options(options, "Main options:", 0, 0, 0);
2143 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2146 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2148 show_value_unit = 1;
2149 use_value_prefix = 1;
2150 use_byte_value_binary_prefix = 1;
2151 use_value_sexagesimal_format = 1;
2155 static void print_section(SectionID id, int level)
2157 const SectionID *pid;
2158 const struct section *section = §ions[id];
2160 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2161 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2162 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2163 printf("%*c %s", level * 4, ' ', section->name);
2164 if (section->unique_name)
2165 printf("/%s", section->unique_name);
2168 for (pid = section->children_ids; *pid != -1; pid++)
2169 print_section(*pid, level+1);
2172 static int opt_sections(void *optctx, const char *opt, const char *arg)
2174 printf("Sections:\n"
2175 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2176 ".A. = Section contains an array of elements of the same type\n"
2177 "..V = Section may contain a variable number of fields with variable keys\n"
2178 "FLAGS NAME/UNIQUE_NAME\n"
2180 print_section(SECTION_ID_ROOT, 0);
2184 static int opt_show_versions(const char *opt, const char *arg)
2186 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
2187 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
2191 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2192 static int opt_show_##section(const char *opt, const char *arg) \
2194 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2198 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS);
2199 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2200 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2201 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2202 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2203 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2204 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2205 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2207 static const OptionDef real_options[] = {
2208 #include "cmdutils_common_opts.h"
2209 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2210 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2211 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2212 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2213 "use binary prefixes for byte units" },
2214 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2215 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2216 { "pretty", 0, {.func_arg = opt_pretty},
2217 "prettify the format of displayed values, make it more human readable" },
2218 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2219 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2220 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2221 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2222 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2223 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2224 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2225 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2226 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2227 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2228 "show a particular entry from the format/container info", "entry" },
2229 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2230 "show a set of specified entries", "entry_list" },
2231 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2232 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2233 { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
2234 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2235 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2236 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2237 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2238 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2239 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2240 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2241 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2242 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2243 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2247 static inline int check_section_show_entries(int section_id)
2250 struct section *section = §ions[section_id];
2251 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2253 for (id = section->children_ids; *id != -1; id++)
2254 if (check_section_show_entries(*id))
2259 #define SET_DO_SHOW(id, varname) do { \
2260 if (check_section_show_entries(SECTION_ID_##id)) \
2261 do_show_##varname = 1; \
2264 int main(int argc, char **argv)
2267 WriterContext *wctx;
2269 char *w_name = NULL, *w_args = NULL;
2272 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2273 atexit(exit_program);
2275 options = real_options;
2276 parse_loglevel(argc, argv, options);
2278 avformat_network_init();
2281 avdevice_register_all();
2284 show_banner(argc, argv, options);
2285 parse_options(NULL, argc, argv, options, opt_input_file);
2287 /* mark things to show, based on -show_entries */
2288 SET_DO_SHOW(CHAPTERS, chapters);
2289 SET_DO_SHOW(ERROR, error);
2290 SET_DO_SHOW(FORMAT, format);
2291 SET_DO_SHOW(FRAMES, frames);
2292 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2293 SET_DO_SHOW(PACKETS, packets);
2294 SET_DO_SHOW(PROGRAM_VERSION, program_version);
2295 SET_DO_SHOW(STREAMS, streams);
2296 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2298 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2299 av_log(NULL, AV_LOG_ERROR,
2300 "-bitexact and -show_program_version or -show_library_versions "
2301 "options are incompatible\n");
2302 ret = AVERROR(EINVAL);
2306 writer_register_all();
2309 print_format = av_strdup("default");
2310 if (!print_format) {
2311 ret = AVERROR(ENOMEM);
2314 w_name = av_strtok(print_format, "=", &buf);
2317 w = writer_get_by_name(w_name);
2319 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2320 ret = AVERROR(EINVAL);
2324 if ((ret = writer_open(&wctx, w, w_args,
2325 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2326 writer_print_section_header(wctx, SECTION_ID_ROOT);
2328 if (do_show_program_version)
2329 ffprobe_show_program_version(wctx);
2330 if (do_show_library_versions)
2331 ffprobe_show_library_versions(wctx);
2333 if (!input_filename &&
2334 ((do_show_format || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
2335 (!do_show_program_version && !do_show_library_versions))) {
2337 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2338 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2339 ret = AVERROR(EINVAL);
2340 } else if (input_filename) {
2341 ret = probe_file(wctx, input_filename);
2342 if (ret < 0 && do_show_error)
2343 show_error(wctx, ret);
2346 writer_print_section_footer(wctx);
2347 writer_close(&wctx);
2351 av_freep(&print_format);
2354 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2355 av_dict_free(&(sections[i].entries_to_show));
2357 avformat_network_deinit();