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_error = 0;
56 static int do_show_format = 0;
57 static int do_show_frames = 0;
58 static int do_show_packets = 0;
59 static int do_show_streams = 0;
60 static int do_show_stream_disposition = 0;
61 static int do_show_data = 0;
62 static int do_show_program_version = 0;
63 static int do_show_library_versions = 0;
65 static int show_value_unit = 0;
66 static int use_value_prefix = 0;
67 static int use_byte_value_binary_prefix = 0;
68 static int use_value_sexagesimal_format = 0;
69 static int show_private_data = 1;
71 static char *print_format;
72 static char *stream_specifier;
74 /* section structure definition */
76 #define SECTION_MAX_NB_CHILDREN 10
79 int id; ///< unique id identifying a section
82 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
83 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
84 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
85 /// For these sections the element_name field is mandatory.
87 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
88 const char *element_name; ///< name of the contained element, if provided
89 const char *unique_name; ///< unique section name, in case the name is ambiguous
90 AVDictionary *entries_to_show;
98 SECTION_ID_FORMAT_TAGS,
101 SECTION_ID_FRAME_TAGS,
102 SECTION_ID_LIBRARY_VERSION,
103 SECTION_ID_LIBRARY_VERSIONS,
106 SECTION_ID_PACKETS_AND_FRAMES,
107 SECTION_ID_PROGRAM_VERSION,
110 SECTION_ID_STREAM_DISPOSITION,
112 SECTION_ID_STREAM_TAGS,
115 static struct section sections[] = {
116 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
117 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
118 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
119 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, -1 } },
120 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
121 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
122 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
123 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
124 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
125 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
126 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
127 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
128 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
129 { SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_STREAMS, SECTION_ID_PACKETS,
130 SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS, -1} },
131 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
132 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
133 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
134 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
137 static const OptionDef *options;
139 /* FFprobe context */
140 static const char *input_filename;
141 static AVInputFormat *iformat = NULL;
143 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
144 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
146 static const char unit_second_str[] = "s" ;
147 static const char unit_hertz_str[] = "Hz" ;
148 static const char unit_byte_str[] = "byte" ;
149 static const char unit_bit_per_second_str[] = "bit/s";
151 static uint64_t *nb_streams_packets;
152 static uint64_t *nb_streams_frames;
153 static int *selected_streams;
155 static void exit_program(void)
158 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
159 av_dict_free(&(sections[i].entries_to_show));
163 union { double d; long long int i; } val;
167 static char *value_string(char *buf, int buf_size, struct unit_value uv)
173 if (uv.unit == unit_second_str) {
177 vald = vali = uv.val.i;
180 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
184 mins = (int)secs / 60;
185 secs = secs - mins * 60;
188 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
190 const char *prefix_string = "";
192 if (use_value_prefix && vald > 1) {
195 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
196 index = (long long int) (log2(vald)) / 10;
197 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
198 vald /= exp2(index * 10);
199 prefix_string = binary_unit_prefixes[index];
201 index = (long long int) (log10(vald)) / 3;
202 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
203 vald /= pow(10, index * 3);
204 prefix_string = decimal_unit_prefixes[index];
208 if (show_float || (use_value_prefix && vald != (long long int)vald))
209 snprintf(buf, buf_size, "%f", vald);
211 snprintf(buf, buf_size, "%lld", vali);
212 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
213 prefix_string, show_value_unit ? uv.unit : "");
221 typedef struct WriterContext WriterContext;
223 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
224 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
226 typedef struct Writer {
227 const AVClass *priv_class; ///< private class of the writer, if any
228 int priv_size; ///< private size for the writer context
231 int (*init) (WriterContext *wctx);
232 void (*uninit)(WriterContext *wctx);
234 void (*print_section_header)(WriterContext *wctx);
235 void (*print_section_footer)(WriterContext *wctx);
236 void (*print_integer) (WriterContext *wctx, const char *, long long int);
237 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
238 void (*print_string) (WriterContext *wctx, const char *, const char *);
239 int flags; ///< a combination or WRITER_FLAG_*
242 #define SECTION_MAX_NB_LEVELS 10
244 struct WriterContext {
245 const AVClass *class; ///< class of the writer
246 const Writer *writer; ///< the Writer of which this is an instance
247 char *name; ///< name of this writer instance
248 void *priv; ///< private data for use by the filter
250 const struct section *sections; ///< array containing all sections
251 int nb_sections; ///< number of sections
253 int level; ///< current level, starting from 0
255 /** number of the item printed in the given section, starting from 0 */
256 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
258 /** section per each level */
259 const struct section *section[SECTION_MAX_NB_LEVELS];
260 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
261 /// used by various writers
263 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
264 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
265 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
268 static const char *writer_get_name(void *p)
270 WriterContext *wctx = p;
271 return wctx->writer->name;
274 static const AVClass writer_class = {
278 LIBAVUTIL_VERSION_INT,
281 static void writer_close(WriterContext **wctx)
288 if ((*wctx)->writer->uninit)
289 (*wctx)->writer->uninit(*wctx);
290 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
291 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
292 if ((*wctx)->writer->priv_class)
293 av_opt_free((*wctx)->priv);
294 av_freep(&((*wctx)->priv));
298 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
299 const struct section *sections, int nb_sections)
303 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
304 ret = AVERROR(ENOMEM);
308 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
309 ret = AVERROR(ENOMEM);
313 (*wctx)->class = &writer_class;
314 (*wctx)->writer = writer;
316 (*wctx)->sections = sections;
317 (*wctx)->nb_sections = nb_sections;
319 if (writer->priv_class) {
320 void *priv_ctx = (*wctx)->priv;
321 *((const AVClass **)priv_ctx) = writer->priv_class;
322 av_opt_set_defaults(priv_ctx);
325 (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
329 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
330 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
332 if ((*wctx)->writer->init)
333 ret = (*wctx)->writer->init(*wctx);
344 static inline void writer_print_section_header(WriterContext *wctx,
347 int parent_section_id;
349 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
350 parent_section_id = wctx->level ?
351 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
353 wctx->nb_item[wctx->level] = 0;
354 wctx->section[wctx->level] = &wctx->sections[section_id];
356 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
357 wctx->nb_section_packet = wctx->nb_section_frame =
358 wctx->nb_section_packet_frame = 0;
359 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
360 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
361 wctx->nb_section_packet : wctx->nb_section_frame;
364 if (wctx->writer->print_section_header)
365 wctx->writer->print_section_header(wctx);
368 static inline void writer_print_section_footer(WriterContext *wctx)
370 int section_id = wctx->section[wctx->level]->id;
371 int parent_section_id = wctx->level ?
372 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
374 if (parent_section_id != SECTION_ID_NONE)
375 wctx->nb_item[wctx->level-1]++;
376 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
377 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
378 else wctx->nb_section_frame++;
380 if (wctx->writer->print_section_footer)
381 wctx->writer->print_section_footer(wctx);
385 static inline void writer_print_integer(WriterContext *wctx,
386 const char *key, long long int val)
388 const struct section *section = wctx->section[wctx->level];
390 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
391 wctx->writer->print_integer(wctx, key, val);
392 wctx->nb_item[wctx->level]++;
396 static inline void writer_print_string(WriterContext *wctx,
397 const char *key, const char *val, int opt)
399 const struct section *section = wctx->section[wctx->level];
401 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
404 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
405 wctx->writer->print_string(wctx, key, val);
406 wctx->nb_item[wctx->level]++;
410 static inline void writer_print_rational(WriterContext *wctx,
411 const char *key, AVRational q, char sep)
414 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
415 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
416 writer_print_string(wctx, key, buf.str, 0);
419 static void writer_print_time(WriterContext *wctx, const char *key,
420 int64_t ts, const AVRational *time_base, int is_duration)
424 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
425 writer_print_string(wctx, key, "N/A", 1);
427 double d = ts * av_q2d(*time_base);
428 struct unit_value uv;
430 uv.unit = unit_second_str;
431 value_string(buf, sizeof(buf), uv);
432 writer_print_string(wctx, key, buf, 0);
436 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
438 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
439 writer_print_string(wctx, key, "N/A", 1);
441 writer_print_integer(wctx, key, ts);
445 static void writer_print_data(WriterContext *wctx, const char *name,
446 uint8_t *data, int size)
449 int offset = 0, l, i;
451 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
452 av_bprintf(&bp, "\n");
454 av_bprintf(&bp, "%08x: ", offset);
456 for (i = 0; i < l; i++) {
457 av_bprintf(&bp, "%02x", data[i]);
459 av_bprintf(&bp, " ");
461 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
462 for (i = 0; i < l; i++)
463 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
464 av_bprintf(&bp, "\n");
469 writer_print_string(wctx, name, bp.str, 0);
470 av_bprint_finalize(&bp, NULL);
473 #define MAX_REGISTERED_WRITERS_NB 64
475 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
477 static int writer_register(const Writer *writer)
479 static int next_registered_writer_idx = 0;
481 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
482 return AVERROR(ENOMEM);
484 registered_writers[next_registered_writer_idx++] = writer;
488 static const Writer *writer_get_by_name(const char *name)
492 for (i = 0; registered_writers[i]; i++)
493 if (!strcmp(registered_writers[i]->name, name))
494 return registered_writers[i];
502 #define DEFINE_WRITER_CLASS(name) \
503 static const char *name##_get_name(void *ctx) \
507 static const AVClass name##_class = { \
515 typedef struct DefaultContext {
516 const AVClass *class;
518 int noprint_wrappers;
519 int nested_section[SECTION_MAX_NB_LEVELS];
522 #define OFFSET(x) offsetof(DefaultContext, x)
524 static const AVOption default_options[] = {
525 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
526 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
527 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
528 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
532 DEFINE_WRITER_CLASS(default);
534 /* lame uppercasing routine, assumes the string is lower case ASCII */
535 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
538 for (i = 0; src[i] && i < dst_size-1; i++)
539 dst[i] = av_toupper(src[i]);
544 static void default_print_section_header(WriterContext *wctx)
546 DefaultContext *def = wctx->priv;
548 const struct section *section = wctx->section[wctx->level];
549 const struct section *parent_section = wctx->level ?
550 wctx->section[wctx->level-1] : NULL;
552 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
553 if (parent_section &&
554 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
555 def->nested_section[wctx->level] = 1;
556 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
557 wctx->section_pbuf[wctx->level-1].str,
558 upcase_string(buf, sizeof(buf),
559 av_x_if_null(section->element_name, section->name)));
562 if (def->noprint_wrappers || def->nested_section[wctx->level])
565 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
566 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
569 static void default_print_section_footer(WriterContext *wctx)
571 DefaultContext *def = wctx->priv;
572 const struct section *section = wctx->section[wctx->level];
575 if (def->noprint_wrappers || def->nested_section[wctx->level])
578 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
579 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
582 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
584 DefaultContext *def = wctx->priv;
587 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
588 printf("%s\n", value);
591 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
593 DefaultContext *def = wctx->priv;
596 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
597 printf("%lld\n", value);
600 static const Writer default_writer = {
602 .priv_size = sizeof(DefaultContext),
603 .print_section_header = default_print_section_header,
604 .print_section_footer = default_print_section_footer,
605 .print_integer = default_print_int,
606 .print_string = default_print_str,
607 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
608 .priv_class = &default_class,
614 * Apply C-language-like string escaping.
616 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
620 for (p = src; *p; p++) {
622 case '\b': av_bprintf(dst, "%s", "\\b"); break;
623 case '\f': av_bprintf(dst, "%s", "\\f"); break;
624 case '\n': av_bprintf(dst, "%s", "\\n"); break;
625 case '\r': av_bprintf(dst, "%s", "\\r"); break;
626 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
629 av_bprint_chars(dst, '\\', 1);
630 av_bprint_chars(dst, *p, 1);
637 * Quote fields containing special characters, check RFC4180.
639 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
641 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
642 int needs_quoting = !!src[strcspn(src, meta_chars)];
645 av_bprint_chars(dst, '\"', 1);
647 for (; *src; src++) {
649 av_bprint_chars(dst, '\"', 1);
650 av_bprint_chars(dst, *src, 1);
653 av_bprint_chars(dst, '\"', 1);
657 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
662 typedef struct CompactContext {
663 const AVClass *class;
668 char *escape_mode_str;
669 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
670 int nested_section[SECTION_MAX_NB_LEVELS];
674 #define OFFSET(x) offsetof(CompactContext, x)
676 static const AVOption compact_options[]= {
677 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
678 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
679 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
680 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
681 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
682 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
683 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
684 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
688 DEFINE_WRITER_CLASS(compact);
690 static av_cold int compact_init(WriterContext *wctx)
692 CompactContext *compact = wctx->priv;
694 if (strlen(compact->item_sep_str) != 1) {
695 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
696 compact->item_sep_str);
697 return AVERROR(EINVAL);
699 compact->item_sep = compact->item_sep_str[0];
701 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
702 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
703 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
705 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
706 return AVERROR(EINVAL);
712 static void compact_print_section_header(WriterContext *wctx)
714 CompactContext *compact = wctx->priv;
715 const struct section *section = wctx->section[wctx->level];
716 const struct section *parent_section = wctx->level ?
717 wctx->section[wctx->level-1] : NULL;
719 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
720 if (parent_section &&
721 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
722 compact->nested_section[wctx->level] = 1;
723 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
724 wctx->section_pbuf[wctx->level-1].str,
725 (char *)av_x_if_null(section->element_name, section->name));
726 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
727 } else if (compact->print_section &&
728 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
729 printf("%s%c", section->name, compact->item_sep);
732 static void compact_print_section_footer(WriterContext *wctx)
734 CompactContext *compact = wctx->priv;
736 if (!compact->nested_section[wctx->level] &&
737 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
741 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
743 CompactContext *compact = wctx->priv;
746 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
748 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
749 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
750 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
751 av_bprint_finalize(&buf, NULL);
754 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
756 CompactContext *compact = wctx->priv;
758 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
760 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
761 printf("%lld", value);
764 static const Writer compact_writer = {
766 .priv_size = sizeof(CompactContext),
767 .init = compact_init,
768 .print_section_header = compact_print_section_header,
769 .print_section_footer = compact_print_section_footer,
770 .print_integer = compact_print_int,
771 .print_string = compact_print_str,
772 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
773 .priv_class = &compact_class,
779 #define OFFSET(x) offsetof(CompactContext, x)
781 static const AVOption csv_options[] = {
782 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
783 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
784 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
785 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
786 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
787 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
788 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
789 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
793 DEFINE_WRITER_CLASS(csv);
795 static const Writer csv_writer = {
797 .priv_size = sizeof(CompactContext),
798 .init = compact_init,
799 .print_section_header = compact_print_section_header,
800 .print_section_footer = compact_print_section_footer,
801 .print_integer = compact_print_int,
802 .print_string = compact_print_str,
803 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
804 .priv_class = &csv_class,
809 typedef struct FlatContext {
810 const AVClass *class;
817 #define OFFSET(x) offsetof(FlatContext, x)
819 static const AVOption flat_options[]= {
820 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
821 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
822 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
823 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
827 DEFINE_WRITER_CLASS(flat);
829 static av_cold int flat_init(WriterContext *wctx)
831 FlatContext *flat = wctx->priv;
833 if (strlen(flat->sep_str) != 1) {
834 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
836 return AVERROR(EINVAL);
838 flat->sep = flat->sep_str[0];
843 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
847 for (p = src; *p; p++) {
848 if (!((*p >= '0' && *p <= '9') ||
849 (*p >= 'a' && *p <= 'z') ||
850 (*p >= 'A' && *p <= 'Z')))
851 av_bprint_chars(dst, '_', 1);
853 av_bprint_chars(dst, *p, 1);
858 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
862 for (p = src; *p; p++) {
864 case '\n': av_bprintf(dst, "%s", "\\n"); break;
865 case '\r': av_bprintf(dst, "%s", "\\r"); break;
866 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
867 case '"': av_bprintf(dst, "%s", "\\\""); break;
868 case '`': av_bprintf(dst, "%s", "\\`"); break;
869 case '$': av_bprintf(dst, "%s", "\\$"); break;
870 default: av_bprint_chars(dst, *p, 1); break;
876 static void flat_print_section_header(WriterContext *wctx)
878 FlatContext *flat = wctx->priv;
879 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
880 const struct section *section = wctx->section[wctx->level];
881 const struct section *parent_section = wctx->level ?
882 wctx->section[wctx->level-1] : NULL;
884 /* build section header */
885 av_bprint_clear(buf);
888 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
890 if (flat->hierarchical ||
891 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
892 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
894 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
895 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
896 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
897 av_bprintf(buf, "%d%s", n, flat->sep_str);
902 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
904 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
907 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
909 FlatContext *flat = wctx->priv;
912 printf("%s", wctx->section_pbuf[wctx->level].str);
913 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
914 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
915 av_bprint_clear(&buf);
916 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
917 av_bprint_finalize(&buf, NULL);
920 static const Writer flat_writer = {
922 .priv_size = sizeof(FlatContext),
924 .print_section_header = flat_print_section_header,
925 .print_integer = flat_print_int,
926 .print_string = flat_print_str,
927 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
928 .priv_class = &flat_class,
931 /* INI format output */
934 const AVClass *class;
939 #define OFFSET(x) offsetof(INIContext, x)
941 static const AVOption ini_options[] = {
942 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
943 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
947 DEFINE_WRITER_CLASS(ini);
949 static char *ini_escape_str(AVBPrint *dst, const char *src)
954 while (c = src[i++]) {
956 case '\b': av_bprintf(dst, "%s", "\\b"); break;
957 case '\f': av_bprintf(dst, "%s", "\\f"); break;
958 case '\n': av_bprintf(dst, "%s", "\\n"); break;
959 case '\r': av_bprintf(dst, "%s", "\\r"); break;
960 case '\t': av_bprintf(dst, "%s", "\\t"); break;
964 case ':' : av_bprint_chars(dst, '\\', 1);
966 if ((unsigned char)c < 32)
967 av_bprintf(dst, "\\x00%02x", c & 0xff);
969 av_bprint_chars(dst, c, 1);
976 static void ini_print_section_header(WriterContext *wctx)
978 INIContext *ini = wctx->priv;
979 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
980 const struct section *section = wctx->section[wctx->level];
981 const struct section *parent_section = wctx->level ?
982 wctx->section[wctx->level-1] : NULL;
984 av_bprint_clear(buf);
985 if (!parent_section) {
986 printf("# ffprobe output\n\n");
990 if (wctx->nb_item[wctx->level-1])
993 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
994 if (ini->hierarchical ||
995 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
996 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
998 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
999 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1000 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1001 av_bprintf(buf, ".%d", n);
1005 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1006 printf("[%s]\n", buf->str);
1009 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1013 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1014 printf("%s=", ini_escape_str(&buf, key));
1015 av_bprint_clear(&buf);
1016 printf("%s\n", ini_escape_str(&buf, value));
1017 av_bprint_finalize(&buf, NULL);
1020 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1022 printf("%s=%lld\n", key, value);
1025 static const Writer ini_writer = {
1027 .priv_size = sizeof(INIContext),
1028 .print_section_header = ini_print_section_header,
1029 .print_integer = ini_print_int,
1030 .print_string = ini_print_str,
1031 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1032 .priv_class = &ini_class,
1038 const AVClass *class;
1041 const char *item_sep, *item_start_end;
1045 #define OFFSET(x) offsetof(JSONContext, x)
1047 static const AVOption json_options[]= {
1048 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1049 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1053 DEFINE_WRITER_CLASS(json);
1055 static av_cold int json_init(WriterContext *wctx)
1057 JSONContext *json = wctx->priv;
1059 json->item_sep = json->compact ? ", " : ",\n";
1060 json->item_start_end = json->compact ? " " : "\n";
1065 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1067 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1068 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1071 for (p = src; *p; p++) {
1072 char *s = strchr(json_escape, *p);
1074 av_bprint_chars(dst, '\\', 1);
1075 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1076 } else if ((unsigned char)*p < 32) {
1077 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1079 av_bprint_chars(dst, *p, 1);
1085 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1087 static void json_print_section_header(WriterContext *wctx)
1089 JSONContext *json = wctx->priv;
1091 const struct section *section = wctx->section[wctx->level];
1092 const struct section *parent_section = wctx->level ?
1093 wctx->section[wctx->level-1] : NULL;
1095 if (wctx->level && wctx->nb_item[wctx->level-1])
1098 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1100 json->indent_level++;
1102 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1103 json_escape_str(&buf, section->name, wctx);
1106 json->indent_level++;
1107 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1108 printf("\"%s\": [\n", buf.str);
1109 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1110 printf("\"%s\": {%s", buf.str, json->item_start_end);
1112 printf("{%s", json->item_start_end);
1114 /* this is required so the parser can distinguish between packets and frames */
1115 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1118 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1121 av_bprint_finalize(&buf, NULL);
1125 static void json_print_section_footer(WriterContext *wctx)
1127 JSONContext *json = wctx->priv;
1128 const struct section *section = wctx->section[wctx->level];
1130 if (wctx->level == 0) {
1131 json->indent_level--;
1133 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1135 json->indent_level--;
1139 printf("%s", json->item_start_end);
1140 json->indent_level--;
1147 static inline void json_print_item_str(WriterContext *wctx,
1148 const char *key, const char *value)
1152 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1153 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1154 av_bprint_clear(&buf);
1155 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1156 av_bprint_finalize(&buf, NULL);
1159 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1161 JSONContext *json = wctx->priv;
1163 if (wctx->nb_item[wctx->level])
1164 printf("%s", json->item_sep);
1167 json_print_item_str(wctx, key, value);
1170 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1172 JSONContext *json = wctx->priv;
1175 if (wctx->nb_item[wctx->level])
1176 printf("%s", json->item_sep);
1180 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1181 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1182 av_bprint_finalize(&buf, NULL);
1185 static const Writer json_writer = {
1187 .priv_size = sizeof(JSONContext),
1189 .print_section_header = json_print_section_header,
1190 .print_section_footer = json_print_section_footer,
1191 .print_integer = json_print_int,
1192 .print_string = json_print_str,
1193 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1194 .priv_class = &json_class,
1200 const AVClass *class;
1203 int fully_qualified;
1208 #define OFFSET(x) offsetof(XMLContext, x)
1210 static const AVOption xml_options[] = {
1211 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1212 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1213 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1214 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1218 DEFINE_WRITER_CLASS(xml);
1220 static av_cold int xml_init(WriterContext *wctx)
1222 XMLContext *xml = wctx->priv;
1224 if (xml->xsd_strict) {
1225 xml->fully_qualified = 1;
1226 #define CHECK_COMPLIANCE(opt, opt_name) \
1228 av_log(wctx, AV_LOG_ERROR, \
1229 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1230 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1231 return AVERROR(EINVAL); \
1233 CHECK_COMPLIANCE(show_private_data, "private");
1234 CHECK_COMPLIANCE(show_value_unit, "unit");
1235 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1237 if (do_show_frames && do_show_packets) {
1238 av_log(wctx, AV_LOG_ERROR,
1239 "Interleaved frames and packets are not allowed in XSD. "
1240 "Select only one between the -show_frames and the -show_packets options.\n");
1241 return AVERROR(EINVAL);
1248 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1252 for (p = src; *p; p++) {
1254 case '&' : av_bprintf(dst, "%s", "&"); break;
1255 case '<' : av_bprintf(dst, "%s", "<"); break;
1256 case '>' : av_bprintf(dst, "%s", ">"); break;
1257 case '\"': av_bprintf(dst, "%s", """); break;
1258 case '\'': av_bprintf(dst, "%s", "'"); break;
1259 default: av_bprint_chars(dst, *p, 1);
1266 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1268 static void xml_print_section_header(WriterContext *wctx)
1270 XMLContext *xml = wctx->priv;
1271 const struct section *section = wctx->section[wctx->level];
1272 const struct section *parent_section = wctx->level ?
1273 wctx->section[wctx->level-1] : NULL;
1275 if (wctx->level == 0) {
1276 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1277 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1278 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1280 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1281 printf("<%sffprobe%s>\n",
1282 xml->fully_qualified ? "ffprobe:" : "",
1283 xml->fully_qualified ? qual : "");
1287 if (xml->within_tag) {
1288 xml->within_tag = 0;
1291 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1292 xml->indent_level++;
1294 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1295 wctx->level && wctx->nb_item[wctx->level-1])
1297 xml->indent_level++;
1299 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1300 XML_INDENT(); printf("<%s>\n", section->name);
1302 XML_INDENT(); printf("<%s ", section->name);
1303 xml->within_tag = 1;
1308 static void xml_print_section_footer(WriterContext *wctx)
1310 XMLContext *xml = wctx->priv;
1311 const struct section *section = wctx->section[wctx->level];
1313 if (wctx->level == 0) {
1314 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1315 } else if (xml->within_tag) {
1316 xml->within_tag = 0;
1318 xml->indent_level--;
1319 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1320 xml->indent_level--;
1322 XML_INDENT(); printf("</%s>\n", section->name);
1323 xml->indent_level--;
1327 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1330 XMLContext *xml = wctx->priv;
1331 const struct section *section = wctx->section[wctx->level];
1333 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1335 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1337 printf("<%s key=\"%s\"",
1338 section->element_name, xml_escape_str(&buf, key, wctx));
1339 av_bprint_clear(&buf);
1340 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1342 if (wctx->nb_item[wctx->level])
1344 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1347 av_bprint_finalize(&buf, NULL);
1350 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1352 if (wctx->nb_item[wctx->level])
1354 printf("%s=\"%lld\"", key, value);
1357 static Writer xml_writer = {
1359 .priv_size = sizeof(XMLContext),
1361 .print_section_header = xml_print_section_header,
1362 .print_section_footer = xml_print_section_footer,
1363 .print_integer = xml_print_int,
1364 .print_string = xml_print_str,
1365 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1366 .priv_class = &xml_class,
1369 static void writer_register_all(void)
1371 static int initialized;
1377 writer_register(&default_writer);
1378 writer_register(&compact_writer);
1379 writer_register(&csv_writer);
1380 writer_register(&flat_writer);
1381 writer_register(&ini_writer);
1382 writer_register(&json_writer);
1383 writer_register(&xml_writer);
1386 #define print_fmt(k, f, ...) do { \
1387 av_bprint_clear(&pbuf); \
1388 av_bprintf(&pbuf, f, __VA_ARGS__); \
1389 writer_print_string(w, k, pbuf.str, 0); \
1392 #define print_int(k, v) writer_print_integer(w, k, v)
1393 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1394 #define print_str(k, v) writer_print_string(w, k, v, 0)
1395 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1396 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1397 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1398 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1399 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1400 #define print_val(k, v, u) do { \
1401 struct unit_value uv; \
1404 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1407 #define print_section_header(s) writer_print_section_header(w, s)
1408 #define print_section_footer(s) writer_print_section_footer(w, s)
1410 static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
1412 AVDictionaryEntry *tag = NULL;
1416 writer_print_section_header(wctx, section_id);
1417 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
1418 writer_print_string(wctx, tag->key, tag->value, 0);
1419 writer_print_section_footer(wctx);
1422 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1425 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1429 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1431 writer_print_section_header(w, SECTION_ID_PACKET);
1433 s = av_get_media_type_string(st->codec->codec_type);
1434 if (s) print_str ("codec_type", s);
1435 else print_str_opt("codec_type", "unknown");
1436 print_int("stream_index", pkt->stream_index);
1437 print_ts ("pts", pkt->pts);
1438 print_time("pts_time", pkt->pts, &st->time_base);
1439 print_ts ("dts", pkt->dts);
1440 print_time("dts_time", pkt->dts, &st->time_base);
1441 print_duration_ts("duration", pkt->duration);
1442 print_duration_time("duration_time", pkt->duration, &st->time_base);
1443 print_duration_ts("convergence_duration", pkt->convergence_duration);
1444 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1445 print_val("size", pkt->size, unit_byte_str);
1446 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1447 else print_str_opt("pos", "N/A");
1448 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1450 writer_print_data(w, "data", pkt->data, pkt->size);
1451 writer_print_section_footer(w);
1453 av_bprint_finalize(&pbuf, NULL);
1457 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1458 AVFormatContext *fmt_ctx)
1463 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1465 writer_print_section_header(w, SECTION_ID_FRAME);
1467 s = av_get_media_type_string(stream->codec->codec_type);
1468 if (s) print_str ("media_type", s);
1469 else print_str_opt("media_type", "unknown");
1470 print_int("key_frame", frame->key_frame);
1471 print_ts ("pkt_pts", frame->pkt_pts);
1472 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1473 print_ts ("pkt_dts", frame->pkt_dts);
1474 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1475 print_duration_ts ("pkt_duration", frame->pkt_duration);
1476 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
1477 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1478 else print_str_opt("pkt_pos", "N/A");
1479 if (frame->pkt_size != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1480 else print_str_opt("pkt_size", "N/A");
1482 switch (stream->codec->codec_type) {
1485 case AVMEDIA_TYPE_VIDEO:
1486 print_int("width", frame->width);
1487 print_int("height", frame->height);
1488 s = av_get_pix_fmt_name(frame->format);
1489 if (s) print_str ("pix_fmt", s);
1490 else print_str_opt("pix_fmt", "unknown");
1491 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1493 print_q("sample_aspect_ratio", sar, ':');
1495 print_str_opt("sample_aspect_ratio", "N/A");
1497 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1498 print_int("coded_picture_number", frame->coded_picture_number);
1499 print_int("display_picture_number", frame->display_picture_number);
1500 print_int("interlaced_frame", frame->interlaced_frame);
1501 print_int("top_field_first", frame->top_field_first);
1502 print_int("repeat_pict", frame->repeat_pict);
1503 print_int("reference", frame->reference);
1506 case AVMEDIA_TYPE_AUDIO:
1507 s = av_get_sample_fmt_name(frame->format);
1508 if (s) print_str ("sample_fmt", s);
1509 else print_str_opt("sample_fmt", "unknown");
1510 print_int("nb_samples", frame->nb_samples);
1511 print_int("channels", av_frame_get_channels(frame));
1512 if (av_frame_get_channel_layout(frame)) {
1513 av_bprint_clear(&pbuf);
1514 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1515 av_frame_get_channel_layout(frame));
1516 print_str ("channel_layout", pbuf.str);
1518 print_str_opt("channel_layout", "unknown");
1521 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1523 writer_print_section_footer(w);
1525 av_bprint_finalize(&pbuf, NULL);
1529 static av_always_inline int process_frame(WriterContext *w,
1530 AVFormatContext *fmt_ctx,
1531 AVFrame *frame, AVPacket *pkt)
1533 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1534 int ret = 0, got_frame = 0;
1536 avcodec_get_frame_defaults(frame);
1537 if (dec_ctx->codec) {
1538 switch (dec_ctx->codec_type) {
1539 case AVMEDIA_TYPE_VIDEO:
1540 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1543 case AVMEDIA_TYPE_AUDIO:
1544 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1551 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1555 nb_streams_frames[pkt->stream_index]++;
1557 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1562 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1568 av_init_packet(&pkt);
1570 while (!av_read_frame(fmt_ctx, &pkt)) {
1571 if (selected_streams[pkt.stream_index]) {
1572 if (do_read_packets) {
1573 if (do_show_packets)
1574 show_packet(w, fmt_ctx, &pkt, i++);
1575 nb_streams_packets[pkt.stream_index]++;
1577 if (do_read_frames) {
1579 while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1582 av_free_packet(&pkt);
1584 av_init_packet(&pkt);
1587 //Flush remaining frames that are cached in the decoder
1588 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1589 pkt.stream_index = i;
1591 while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1595 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1597 AVStream *stream = fmt_ctx->streams[stream_idx];
1598 AVCodecContext *dec_ctx;
1602 AVRational sar, dar;
1605 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1607 writer_print_section_header(w, SECTION_ID_STREAM);
1609 print_int("index", stream->index);
1611 if ((dec_ctx = stream->codec)) {
1612 const char *profile = NULL;
1613 dec = dec_ctx->codec;
1615 print_str("codec_name", dec->name);
1617 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1618 else print_str_opt("codec_long_name", "unknown");
1621 print_str_opt("codec_name", "unknown");
1623 print_str_opt("codec_long_name", "unknown");
1627 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1628 print_str("profile", profile);
1630 print_str_opt("profile", "unknown");
1632 s = av_get_media_type_string(dec_ctx->codec_type);
1633 if (s) print_str ("codec_type", s);
1634 else print_str_opt("codec_type", "unknown");
1635 print_q("codec_time_base", dec_ctx->time_base, '/');
1637 /* print AVI/FourCC tag */
1638 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1639 print_str("codec_tag_string", val_str);
1640 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1642 switch (dec_ctx->codec_type) {
1643 case AVMEDIA_TYPE_VIDEO:
1644 print_int("width", dec_ctx->width);
1645 print_int("height", dec_ctx->height);
1646 print_int("has_b_frames", dec_ctx->has_b_frames);
1647 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1649 print_q("sample_aspect_ratio", sar, ':');
1650 av_reduce(&dar.num, &dar.den,
1651 dec_ctx->width * sar.num,
1652 dec_ctx->height * sar.den,
1654 print_q("display_aspect_ratio", dar, ':');
1656 print_str_opt("sample_aspect_ratio", "N/A");
1657 print_str_opt("display_aspect_ratio", "N/A");
1659 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1660 if (s) print_str ("pix_fmt", s);
1661 else print_str_opt("pix_fmt", "unknown");
1662 print_int("level", dec_ctx->level);
1663 if (dec_ctx->timecode_frame_start >= 0) {
1664 char tcbuf[AV_TIMECODE_STR_SIZE];
1665 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1666 print_str("timecode", tcbuf);
1668 print_str_opt("timecode", "N/A");
1672 case AVMEDIA_TYPE_AUDIO:
1673 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1674 if (s) print_str ("sample_fmt", s);
1675 else print_str_opt("sample_fmt", "unknown");
1676 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1677 print_int("channels", dec_ctx->channels);
1678 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1682 print_str_opt("codec_type", "unknown");
1684 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1685 const AVOption *opt = NULL;
1686 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1688 if (opt->flags) continue;
1689 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1690 print_str(opt->name, str);
1696 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1697 else print_str_opt("id", "N/A");
1698 print_q("r_frame_rate", stream->r_frame_rate, '/');
1699 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1700 print_q("time_base", stream->time_base, '/');
1701 print_ts ("start_pts", stream->start_time);
1702 print_time("start_time", stream->start_time, &stream->time_base);
1703 print_ts ("duration_ts", stream->duration);
1704 print_time("duration", stream->duration, &stream->time_base);
1705 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1706 else print_str_opt("bit_rate", "N/A");
1707 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1708 else print_str_opt("nb_frames", "N/A");
1709 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1710 else print_str_opt("nb_read_frames", "N/A");
1711 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1712 else print_str_opt("nb_read_packets", "N/A");
1714 writer_print_data(w, "extradata", dec_ctx->extradata,
1715 dec_ctx->extradata_size);
1717 /* Print disposition information */
1718 #define PRINT_DISPOSITION(flagname, name) do { \
1719 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
1722 if (do_show_stream_disposition) {
1723 writer_print_section_header(w, SECTION_ID_STREAM_DISPOSITION);
1724 PRINT_DISPOSITION(DEFAULT, "default");
1725 PRINT_DISPOSITION(DUB, "dub");
1726 PRINT_DISPOSITION(ORIGINAL, "original");
1727 PRINT_DISPOSITION(COMMENT, "comment");
1728 PRINT_DISPOSITION(LYRICS, "lyrics");
1729 PRINT_DISPOSITION(KARAOKE, "karaoke");
1730 PRINT_DISPOSITION(FORCED, "forced");
1731 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
1732 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
1733 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
1734 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
1735 writer_print_section_footer(w);
1738 show_tags(w, stream->metadata, SECTION_ID_STREAM_TAGS);
1740 writer_print_section_footer(w);
1741 av_bprint_finalize(&pbuf, NULL);
1745 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1748 writer_print_section_header(w, SECTION_ID_STREAMS);
1749 for (i = 0; i < fmt_ctx->nb_streams; i++)
1750 if (selected_streams[i])
1751 show_stream(w, fmt_ctx, i);
1752 writer_print_section_footer(w);
1755 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1758 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1760 writer_print_section_header(w, SECTION_ID_FORMAT);
1761 print_str("filename", fmt_ctx->filename);
1762 print_int("nb_streams", fmt_ctx->nb_streams);
1763 print_str("format_name", fmt_ctx->iformat->name);
1765 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
1766 else print_str_opt("format_long_name", "unknown");
1768 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1769 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1770 if (size >= 0) print_val ("size", size, unit_byte_str);
1771 else print_str_opt("size", "N/A");
1772 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1773 else print_str_opt("bit_rate", "N/A");
1774 show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
1776 writer_print_section_footer(w);
1780 static void show_error(WriterContext *w, int err)
1783 const char *errbuf_ptr = errbuf;
1785 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1786 errbuf_ptr = strerror(AVUNERROR(err));
1788 writer_print_section_header(w, SECTION_ID_ERROR);
1789 print_int("code", err);
1790 print_str("string", errbuf_ptr);
1791 writer_print_section_footer(w);
1794 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1797 AVFormatContext *fmt_ctx = NULL;
1798 AVDictionaryEntry *t;
1800 if ((err = avformat_open_input(&fmt_ctx, filename,
1801 iformat, &format_opts)) < 0) {
1802 print_error(filename, err);
1805 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1806 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1807 return AVERROR_OPTION_NOT_FOUND;
1811 /* fill the streams in the format context */
1812 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1813 print_error(filename, err);
1817 av_dump_format(fmt_ctx, 0, filename, 0);
1819 /* bind a decoder to each input stream */
1820 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1821 AVStream *stream = fmt_ctx->streams[i];
1824 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
1825 av_log(NULL, AV_LOG_ERROR,
1826 "Failed to probe codec for input stream %d\n",
1828 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1829 av_log(NULL, AV_LOG_ERROR,
1830 "Unsupported codec with id %d for input stream %d\n",
1831 stream->codec->codec_id, stream->index);
1832 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1833 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1838 *fmt_ctx_ptr = fmt_ctx;
1842 static void close_input_file(AVFormatContext **ctx_ptr)
1845 AVFormatContext *fmt_ctx = *ctx_ptr;
1847 /* close decoder for each stream */
1848 for (i = 0; i < fmt_ctx->nb_streams; i++)
1849 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
1850 avcodec_close(fmt_ctx->streams[i]->codec);
1852 avformat_close_input(ctx_ptr);
1855 static int probe_file(WriterContext *wctx, const char *filename)
1857 AVFormatContext *fmt_ctx;
1861 do_read_frames = do_show_frames || do_count_frames;
1862 do_read_packets = do_show_packets || do_count_packets;
1864 ret = open_input_file(&fmt_ctx, filename);
1866 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1867 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1868 selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
1870 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1871 if (stream_specifier) {
1872 ret = avformat_match_stream_specifier(fmt_ctx,
1873 fmt_ctx->streams[i],
1878 selected_streams[i] = ret;
1880 selected_streams[i] = 1;
1884 if (do_read_frames || do_read_packets) {
1885 if (do_show_frames && do_show_packets &&
1886 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1887 section_id = SECTION_ID_PACKETS_AND_FRAMES;
1888 else if (do_show_packets && !do_show_frames)
1889 section_id = SECTION_ID_PACKETS;
1890 else // (!do_show_packets && do_show_frames)
1891 section_id = SECTION_ID_FRAMES;
1892 if (do_show_frames || do_show_packets)
1893 writer_print_section_header(wctx, section_id);
1894 read_packets(wctx, fmt_ctx);
1895 if (do_show_frames || do_show_packets)
1896 writer_print_section_footer(wctx);
1898 if (do_show_streams)
1899 show_streams(wctx, fmt_ctx);
1901 show_format(wctx, fmt_ctx);
1904 close_input_file(&fmt_ctx);
1905 av_freep(&nb_streams_frames);
1906 av_freep(&nb_streams_packets);
1907 av_freep(&selected_streams);
1912 static void show_usage(void)
1914 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1915 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1916 av_log(NULL, AV_LOG_INFO, "\n");
1919 static void ffprobe_show_program_version(WriterContext *w)
1922 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1924 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
1925 print_str("version", FFMPEG_VERSION);
1926 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1927 program_birth_year, this_year);
1928 print_str("build_date", __DATE__);
1929 print_str("build_time", __TIME__);
1930 print_str("compiler_ident", CC_IDENT);
1931 print_str("configuration", FFMPEG_CONFIGURATION);
1932 writer_print_section_footer(w);
1934 av_bprint_finalize(&pbuf, NULL);
1937 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1939 if (CONFIG_##LIBNAME) { \
1940 unsigned int version = libname##_version(); \
1941 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
1942 print_str("name", "lib" #libname); \
1943 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1944 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1945 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1946 print_int("version", version); \
1947 print_str("ident", LIB##LIBNAME##_IDENT); \
1948 writer_print_section_footer(w); \
1952 static void ffprobe_show_library_versions(WriterContext *w)
1954 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
1955 SHOW_LIB_VERSION(avutil, AVUTIL);
1956 SHOW_LIB_VERSION(avcodec, AVCODEC);
1957 SHOW_LIB_VERSION(avformat, AVFORMAT);
1958 SHOW_LIB_VERSION(avdevice, AVDEVICE);
1959 SHOW_LIB_VERSION(avfilter, AVFILTER);
1960 SHOW_LIB_VERSION(swscale, SWSCALE);
1961 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
1962 SHOW_LIB_VERSION(postproc, POSTPROC);
1963 writer_print_section_footer(w);
1966 static int opt_format(void *optctx, const char *opt, const char *arg)
1968 iformat = av_find_input_format(arg);
1970 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1971 return AVERROR(EINVAL);
1976 static inline void mark_section_show_entries(SectionID section_id,
1977 int show_all_entries, AVDictionary *entries)
1979 struct section *section = §ions[section_id];
1981 section->show_all_entries = show_all_entries;
1982 if (show_all_entries) {
1984 for (id = section->children_ids; *id != -1; id++)
1985 mark_section_show_entries(*id, show_all_entries, entries);
1987 av_dict_copy(§ion->entries_to_show, entries, 0);
1991 static int match_section(const char *section_name,
1992 int show_all_entries, AVDictionary *entries)
1996 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
1997 const struct section *section = §ions[i];
1998 if (!strcmp(section_name, section->name) ||
1999 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2000 av_log(NULL, AV_LOG_DEBUG,
2001 "'%s' matches section with unique name '%s'\n", section_name,
2002 (char *)av_x_if_null(section->unique_name, section->name));
2004 mark_section_show_entries(section->id, show_all_entries, entries);
2010 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2012 const char *p = arg;
2016 AVDictionary *entries = NULL;
2017 char *section_name = av_get_token(&p, "=:");
2018 int show_all_entries = 0;
2020 if (!section_name) {
2021 av_log(NULL, AV_LOG_ERROR,
2022 "Missing section name for option '%s'\n", opt);
2023 return AVERROR(EINVAL);
2028 while (*p && *p != ':') {
2029 char *entry = av_get_token(&p, ",:");
2032 av_log(NULL, AV_LOG_VERBOSE,
2033 "Adding '%s' to the entries to show in section '%s'\n",
2034 entry, section_name);
2035 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2040 show_all_entries = 1;
2043 ret = match_section(section_name, show_all_entries, entries);
2045 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2046 ret = AVERROR(EINVAL);
2048 av_dict_free(&entries);
2049 av_free(section_name);
2060 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2062 char *buf = av_asprintf("format=%s", arg);
2065 av_log(NULL, AV_LOG_WARNING,
2066 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2068 ret = opt_show_entries(optctx, opt, buf);
2073 static void opt_input_file(void *optctx, const char *arg)
2075 if (input_filename) {
2076 av_log(NULL, AV_LOG_ERROR,
2077 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2078 arg, input_filename);
2081 if (!strcmp(arg, "-"))
2083 input_filename = arg;
2086 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2088 opt_input_file(optctx, arg);
2092 void show_help_default(const char *opt, const char *arg)
2094 av_log_set_callback(log_callback_help);
2096 show_help_options(options, "Main options:", 0, 0, 0);
2099 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2102 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2104 show_value_unit = 1;
2105 use_value_prefix = 1;
2106 use_byte_value_binary_prefix = 1;
2107 use_value_sexagesimal_format = 1;
2111 static void print_section(SectionID id, int level)
2113 const SectionID *pid;
2114 const struct section *section = §ions[id];
2116 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2117 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2118 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2119 printf("%*c %s", level * 4, ' ', section->name);
2120 if (section->unique_name)
2121 printf("/%s", section->unique_name);
2124 for (pid = section->children_ids; *pid != -1; pid++)
2125 print_section(*pid, level+1);
2128 static int opt_sections(void *optctx, const char *opt, const char *arg)
2130 printf("Sections:\n"
2131 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2132 ".A. = Section contains an array of elements of the same type\n"
2133 "..V = Section may contain a variable number of fields with variable keys\n"
2134 "FLAGS NAME/UNIQUE_NAME\n"
2136 print_section(SECTION_ID_ROOT, 0);
2140 static int opt_show_versions(const char *opt, const char *arg)
2142 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
2143 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
2147 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2148 static int opt_show_##section(const char *opt, const char *arg) \
2150 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2154 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2155 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2156 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2157 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2158 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2159 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2160 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2162 static const OptionDef real_options[] = {
2163 #include "cmdutils_common_opts.h"
2164 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2165 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2166 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2167 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2168 "use binary prefixes for byte units" },
2169 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2170 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2171 { "pretty", 0, {.func_arg = opt_pretty},
2172 "prettify the format of displayed values, make it more human readable" },
2173 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2174 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2175 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2176 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2177 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2178 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2179 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2180 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2181 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2182 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2183 "show a particular entry from the format/container info", "entry" },
2184 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2185 "show a set of specified entries", "entry_list" },
2186 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2187 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2188 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2189 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2190 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2191 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2192 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2193 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2194 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2195 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2196 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2197 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2201 static inline int check_section_show_entries(int section_id)
2204 struct section *section = §ions[section_id];
2205 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2207 for (id = section->children_ids; *id != -1; id++)
2208 if (check_section_show_entries(*id))
2213 #define SET_DO_SHOW(id, varname) do { \
2214 if (check_section_show_entries(SECTION_ID_##id)) \
2215 do_show_##varname = 1; \
2218 int main(int argc, char **argv)
2221 WriterContext *wctx;
2223 char *w_name = NULL, *w_args = NULL;
2226 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2227 atexit(exit_program);
2229 options = real_options;
2230 parse_loglevel(argc, argv, options);
2232 avformat_network_init();
2235 avdevice_register_all();
2238 show_banner(argc, argv, options);
2239 parse_options(NULL, argc, argv, options, opt_input_file);
2241 /* mark things to show, based on -show_entries */
2242 SET_DO_SHOW(ERROR, error);
2243 SET_DO_SHOW(FORMAT, format);
2244 SET_DO_SHOW(FRAMES, frames);
2245 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2246 SET_DO_SHOW(PACKETS, packets);
2247 SET_DO_SHOW(PROGRAM_VERSION, program_version);
2248 SET_DO_SHOW(STREAMS, streams);
2249 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2251 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2252 av_log(NULL, AV_LOG_ERROR,
2253 "-bitexact and -show_program_version or -show_library_versions "
2254 "options are incompatible\n");
2255 ret = AVERROR(EINVAL);
2259 writer_register_all();
2262 print_format = av_strdup("default");
2263 if (!print_format) {
2264 ret = AVERROR(ENOMEM);
2267 w_name = av_strtok(print_format, "=", &buf);
2270 w = writer_get_by_name(w_name);
2272 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2273 ret = AVERROR(EINVAL);
2277 if ((ret = writer_open(&wctx, w, w_args,
2278 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2279 writer_print_section_header(wctx, SECTION_ID_ROOT);
2281 if (do_show_program_version)
2282 ffprobe_show_program_version(wctx);
2283 if (do_show_library_versions)
2284 ffprobe_show_library_versions(wctx);
2286 if (!input_filename &&
2287 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
2288 (!do_show_program_version && !do_show_library_versions))) {
2290 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2291 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2292 ret = AVERROR(EINVAL);
2293 } else if (input_filename) {
2294 ret = probe_file(wctx, input_filename);
2295 if (ret < 0 && do_show_error)
2296 show_error(wctx, ret);
2299 writer_print_section_footer(wctx);
2300 writer_close(&wctx);
2304 av_freep(&print_format);
2307 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2308 av_dict_free(&(sections[i].entries_to_show));
2310 avformat_network_deinit();