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
27 #include "libavutil/ffversion.h"
31 #include "libavformat/avformat.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/libm.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/timecode.h"
42 #include "libavutil/timestamp.h"
43 #include "libavdevice/avdevice.h"
44 #include "libswscale/swscale.h"
45 #include "libswresample/swresample.h"
46 #include "libpostproc/postprocess.h"
49 const char program_name[] = "ffprobe";
50 const int program_birth_year = 2007;
52 static int do_bitexact = 0;
53 static int do_count_frames = 0;
54 static int do_count_packets = 0;
55 static int do_read_frames = 0;
56 static int do_read_packets = 0;
57 static int do_show_chapters = 0;
58 static int do_show_error = 0;
59 static int do_show_format = 0;
60 static int do_show_frames = 0;
61 static int do_show_packets = 0;
62 static int do_show_programs = 0;
63 static int do_show_streams = 0;
64 static int do_show_stream_disposition = 0;
65 static int do_show_data = 0;
66 static int do_show_program_version = 0;
67 static int do_show_library_versions = 0;
69 static int show_value_unit = 0;
70 static int use_value_prefix = 0;
71 static int use_byte_value_binary_prefix = 0;
72 static int use_value_sexagesimal_format = 0;
73 static int show_private_data = 1;
75 static char *print_format;
76 static char *stream_specifier;
79 int id; ///< identifier
80 int64_t start, end; ///< start, end in second/AV_TIME_BASE units
81 int has_start, has_end;
82 int start_is_offset, end_is_offset;
86 static ReadInterval *read_intervals;
87 static int read_intervals_nb = 0;
89 /* section structure definition */
91 #define SECTION_MAX_NB_CHILDREN 10
94 int id; ///< unique id identifying a section
97 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
98 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
99 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
100 /// For these sections the element_name field is mandatory.
102 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
103 const char *element_name; ///< name of the contained element, if provided
104 const char *unique_name; ///< unique section name, in case the name is ambiguous
105 AVDictionary *entries_to_show;
106 int show_all_entries;
110 SECTION_ID_NONE = -1,
112 SECTION_ID_CHAPTER_TAGS,
116 SECTION_ID_FORMAT_TAGS,
119 SECTION_ID_FRAME_TAGS,
120 SECTION_ID_LIBRARY_VERSION,
121 SECTION_ID_LIBRARY_VERSIONS,
124 SECTION_ID_PACKETS_AND_FRAMES,
125 SECTION_ID_PROGRAM_STREAM_DISPOSITION,
126 SECTION_ID_PROGRAM_STREAM_TAGS,
128 SECTION_ID_PROGRAM_STREAMS,
129 SECTION_ID_PROGRAM_STREAM,
130 SECTION_ID_PROGRAM_TAGS,
131 SECTION_ID_PROGRAM_VERSION,
135 SECTION_ID_STREAM_DISPOSITION,
137 SECTION_ID_STREAM_TAGS,
141 static struct section sections[] = {
142 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
143 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
144 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
145 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
146 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
147 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
148 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, SECTION_ID_SUBTITLE, -1 } },
149 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
150 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
151 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
152 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
153 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
154 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
155 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
156 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
157 [SECTION_ID_PROGRAM_STREAM_TAGS] = { SECTION_ID_PROGRAM_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_stream_tags" },
158 [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
159 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
160 [SECTION_ID_PROGRAM_STREAM] = { SECTION_ID_PROGRAM_STREAM, "stream", 0, { SECTION_ID_PROGRAM_STREAM_DISPOSITION, SECTION_ID_PROGRAM_STREAM_TAGS, -1 }, .unique_name = "program_stream" },
161 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
162 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
163 [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
164 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
165 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAMS,
166 SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS, -1} },
167 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
168 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
169 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
170 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
171 [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
174 static const OptionDef *options;
176 /* FFprobe context */
177 static const char *input_filename;
178 static AVInputFormat *iformat = NULL;
180 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
181 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
183 static const char unit_second_str[] = "s" ;
184 static const char unit_hertz_str[] = "Hz" ;
185 static const char unit_byte_str[] = "byte" ;
186 static const char unit_bit_per_second_str[] = "bit/s";
188 static uint64_t *nb_streams_packets;
189 static uint64_t *nb_streams_frames;
190 static int *selected_streams;
192 static void ffprobe_cleanup(int ret)
195 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
196 av_dict_free(&(sections[i].entries_to_show));
200 union { double d; long long int i; } val;
204 static char *value_string(char *buf, int buf_size, struct unit_value uv)
210 if (uv.unit == unit_second_str) {
214 vald = vali = uv.val.i;
217 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
221 mins = (int)secs / 60;
222 secs = secs - mins * 60;
225 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
227 const char *prefix_string = "";
229 if (use_value_prefix && vald > 1) {
232 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
233 index = (long long int) (log2(vald)) / 10;
234 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
235 vald /= exp2(index * 10);
236 prefix_string = binary_unit_prefixes[index];
238 index = (long long int) (log10(vald)) / 3;
239 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
240 vald /= pow(10, index * 3);
241 prefix_string = decimal_unit_prefixes[index];
245 if (show_float || (use_value_prefix && vald != (long long int)vald))
246 snprintf(buf, buf_size, "%f", vald);
248 snprintf(buf, buf_size, "%lld", vali);
249 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
250 prefix_string, show_value_unit ? uv.unit : "");
258 typedef struct WriterContext WriterContext;
260 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
261 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
264 WRITER_STRING_VALIDATION_FAIL,
265 WRITER_STRING_VALIDATION_REPLACE,
266 WRITER_STRING_VALIDATION_IGNORE,
267 WRITER_STRING_VALIDATION_NB
270 typedef struct Writer {
271 const AVClass *priv_class; ///< private class of the writer, if any
272 int priv_size; ///< private size for the writer context
275 int (*init) (WriterContext *wctx);
276 void (*uninit)(WriterContext *wctx);
278 void (*print_section_header)(WriterContext *wctx);
279 void (*print_section_footer)(WriterContext *wctx);
280 void (*print_integer) (WriterContext *wctx, const char *, long long int);
281 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
282 void (*print_string) (WriterContext *wctx, const char *, const char *);
283 int flags; ///< a combination or WRITER_FLAG_*
286 #define SECTION_MAX_NB_LEVELS 10
288 struct WriterContext {
289 const AVClass *class; ///< class of the writer
290 const Writer *writer; ///< the Writer of which this is an instance
291 char *name; ///< name of this writer instance
292 void *priv; ///< private data for use by the filter
294 const struct section *sections; ///< array containing all sections
295 int nb_sections; ///< number of sections
297 int level; ///< current level, starting from 0
299 /** number of the item printed in the given section, starting from 0 */
300 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
302 /** section per each level */
303 const struct section *section[SECTION_MAX_NB_LEVELS];
304 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
305 /// used by various writers
307 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
308 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
309 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
311 StringValidation string_validation;
312 char *string_validation_replacement;
313 unsigned int string_validation_utf8_flags;
316 static const char *writer_get_name(void *p)
318 WriterContext *wctx = p;
319 return wctx->writer->name;
322 #define OFFSET(x) offsetof(WriterContext, x)
324 static const AVOption writer_options[] = {
325 { "string_validation", "set string validation mode",
326 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
327 { "sv", "set string validation mode",
328 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
329 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
330 { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
331 { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
332 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
333 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
337 static void *writer_child_next(void *obj, void *prev)
339 WriterContext *ctx = obj;
340 if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
345 static const AVClass writer_class = {
346 .class_name = "Writer",
347 .item_name = writer_get_name,
348 .option = writer_options,
349 .version = LIBAVUTIL_VERSION_INT,
350 .child_next = writer_child_next,
353 static void writer_close(WriterContext **wctx)
360 if ((*wctx)->writer->uninit)
361 (*wctx)->writer->uninit(*wctx);
362 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
363 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
364 if ((*wctx)->writer->priv_class)
365 av_opt_free((*wctx)->priv);
366 av_freep(&((*wctx)->priv));
371 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
374 av_bprintf(bp, "0X");
375 for (i = 0; i < ubuf_size; i++)
376 av_bprintf(bp, "%02X", ubuf[i]);
380 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
381 const struct section *sections, int nb_sections)
385 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
386 ret = AVERROR(ENOMEM);
390 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
391 ret = AVERROR(ENOMEM);
395 (*wctx)->class = &writer_class;
396 (*wctx)->writer = writer;
398 (*wctx)->sections = sections;
399 (*wctx)->nb_sections = nb_sections;
401 av_opt_set_defaults(*wctx);
403 if (writer->priv_class) {
404 void *priv_ctx = (*wctx)->priv;
405 *((const AVClass **)priv_ctx) = writer->priv_class;
406 av_opt_set_defaults(priv_ctx);
409 /* convert options to dictionary */
411 AVDictionary *opts = NULL;
412 AVDictionaryEntry *opt = NULL;
414 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
415 av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
420 while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
421 if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
422 av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
423 opt->key, opt->value);
432 /* validate replace string */
434 const uint8_t *p = (*wctx)->string_validation_replacement;
435 const uint8_t *endp = p + strlen(p);
437 const uint8_t *p0 = p;
439 ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
442 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
443 bprint_bytes(&bp, p0, p-p0),
444 av_log(wctx, AV_LOG_ERROR,
445 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
446 bp.str, (*wctx)->string_validation_replacement);
452 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
453 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
455 if ((*wctx)->writer->init)
456 ret = (*wctx)->writer->init(*wctx);
467 static inline void writer_print_section_header(WriterContext *wctx,
470 int parent_section_id;
472 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
473 parent_section_id = wctx->level ?
474 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
476 wctx->nb_item[wctx->level] = 0;
477 wctx->section[wctx->level] = &wctx->sections[section_id];
479 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
480 wctx->nb_section_packet = wctx->nb_section_frame =
481 wctx->nb_section_packet_frame = 0;
482 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
483 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
484 wctx->nb_section_packet : wctx->nb_section_frame;
487 if (wctx->writer->print_section_header)
488 wctx->writer->print_section_header(wctx);
491 static inline void writer_print_section_footer(WriterContext *wctx)
493 int section_id = wctx->section[wctx->level]->id;
494 int parent_section_id = wctx->level ?
495 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
497 if (parent_section_id != SECTION_ID_NONE)
498 wctx->nb_item[wctx->level-1]++;
499 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
500 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
501 else wctx->nb_section_frame++;
503 if (wctx->writer->print_section_footer)
504 wctx->writer->print_section_footer(wctx);
508 static inline void writer_print_integer(WriterContext *wctx,
509 const char *key, long long int val)
511 const struct section *section = wctx->section[wctx->level];
513 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
514 wctx->writer->print_integer(wctx, key, val);
515 wctx->nb_item[wctx->level]++;
519 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
521 const uint8_t *p, *endp;
523 int invalid_chars_nb = 0, ret = 0;
525 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
527 endp = src + strlen(src);
528 for (p = (uint8_t *)src; *p;) {
531 const uint8_t *p0 = p;
533 if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
535 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
536 bprint_bytes(&bp, p0, p-p0);
537 av_log(wctx, AV_LOG_DEBUG,
538 "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
545 switch (wctx->string_validation) {
546 case WRITER_STRING_VALIDATION_FAIL:
547 av_log(wctx, AV_LOG_ERROR,
548 "Invalid UTF-8 sequence found in string '%s'\n", src);
549 ret = AVERROR_INVALIDDATA;
553 case WRITER_STRING_VALIDATION_REPLACE:
554 av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
559 if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
560 av_bprint_append_data(&dstbuf, p0, p-p0);
563 if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
564 av_log(wctx, AV_LOG_WARNING,
565 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
566 invalid_chars_nb, src, wctx->string_validation_replacement);
570 av_bprint_finalize(&dstbuf, dstp);
574 #define PRINT_STRING_OPT 1
575 #define PRINT_STRING_VALIDATE 2
577 static inline int writer_print_string(WriterContext *wctx,
578 const char *key, const char *val, int flags)
580 const struct section *section = wctx->section[wctx->level];
583 if ((flags & PRINT_STRING_OPT)
584 && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
587 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
588 if (flags & PRINT_STRING_VALIDATE) {
589 char *key1 = NULL, *val1 = NULL;
590 ret = validate_string(wctx, &key1, key);
591 if (ret < 0) goto end;
592 ret = validate_string(wctx, &val1, val);
593 if (ret < 0) goto end;
594 wctx->writer->print_string(wctx, key1, val1);
597 av_log(wctx, AV_LOG_ERROR,
598 "Invalid key=value string combination %s=%s in section %s\n",
599 key, val, section->unique_name);
604 wctx->writer->print_string(wctx, key, val);
607 wctx->nb_item[wctx->level]++;
613 static inline void writer_print_rational(WriterContext *wctx,
614 const char *key, AVRational q, char sep)
617 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
618 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
619 writer_print_string(wctx, key, buf.str, 0);
622 static void writer_print_time(WriterContext *wctx, const char *key,
623 int64_t ts, const AVRational *time_base, int is_duration)
627 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
628 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
630 double d = ts * av_q2d(*time_base);
631 struct unit_value uv;
633 uv.unit = unit_second_str;
634 value_string(buf, sizeof(buf), uv);
635 writer_print_string(wctx, key, buf, 0);
639 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
641 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
642 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
644 writer_print_integer(wctx, key, ts);
648 static void writer_print_data(WriterContext *wctx, const char *name,
649 uint8_t *data, int size)
652 int offset = 0, l, i;
654 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
655 av_bprintf(&bp, "\n");
657 av_bprintf(&bp, "%08x: ", offset);
659 for (i = 0; i < l; i++) {
660 av_bprintf(&bp, "%02x", data[i]);
662 av_bprintf(&bp, " ");
664 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
665 for (i = 0; i < l; i++)
666 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
667 av_bprintf(&bp, "\n");
672 writer_print_string(wctx, name, bp.str, 0);
673 av_bprint_finalize(&bp, NULL);
676 #define MAX_REGISTERED_WRITERS_NB 64
678 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
680 static int writer_register(const Writer *writer)
682 static int next_registered_writer_idx = 0;
684 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
685 return AVERROR(ENOMEM);
687 registered_writers[next_registered_writer_idx++] = writer;
691 static const Writer *writer_get_by_name(const char *name)
695 for (i = 0; registered_writers[i]; i++)
696 if (!strcmp(registered_writers[i]->name, name))
697 return registered_writers[i];
705 #define DEFINE_WRITER_CLASS(name) \
706 static const char *name##_get_name(void *ctx) \
710 static const AVClass name##_class = { \
711 .class_name = #name, \
712 .item_name = name##_get_name, \
713 .option = name##_options \
718 typedef struct DefaultContext {
719 const AVClass *class;
721 int noprint_wrappers;
722 int nested_section[SECTION_MAX_NB_LEVELS];
726 #define OFFSET(x) offsetof(DefaultContext, x)
728 static const AVOption default_options[] = {
729 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
730 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
731 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
732 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
736 DEFINE_WRITER_CLASS(default);
738 /* lame uppercasing routine, assumes the string is lower case ASCII */
739 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
742 for (i = 0; src[i] && i < dst_size-1; i++)
743 dst[i] = av_toupper(src[i]);
748 static void default_print_section_header(WriterContext *wctx)
750 DefaultContext *def = wctx->priv;
752 const struct section *section = wctx->section[wctx->level];
753 const struct section *parent_section = wctx->level ?
754 wctx->section[wctx->level-1] : NULL;
756 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
757 if (parent_section &&
758 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
759 def->nested_section[wctx->level] = 1;
760 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
761 wctx->section_pbuf[wctx->level-1].str,
762 upcase_string(buf, sizeof(buf),
763 av_x_if_null(section->element_name, section->name)));
766 if (def->noprint_wrappers || def->nested_section[wctx->level])
769 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
770 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
773 static void default_print_section_footer(WriterContext *wctx)
775 DefaultContext *def = wctx->priv;
776 const struct section *section = wctx->section[wctx->level];
779 if (def->noprint_wrappers || def->nested_section[wctx->level])
782 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
783 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
786 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
788 DefaultContext *def = wctx->priv;
791 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
792 printf("%s\n", value);
795 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
797 DefaultContext *def = wctx->priv;
800 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
801 printf("%lld\n", value);
804 static const Writer default_writer = {
806 .priv_size = sizeof(DefaultContext),
807 .print_section_header = default_print_section_header,
808 .print_section_footer = default_print_section_footer,
809 .print_integer = default_print_int,
810 .print_string = default_print_str,
811 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
812 .priv_class = &default_class,
818 * Apply C-language-like string escaping.
820 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
824 for (p = src; *p; p++) {
826 case '\b': av_bprintf(dst, "%s", "\\b"); break;
827 case '\f': av_bprintf(dst, "%s", "\\f"); break;
828 case '\n': av_bprintf(dst, "%s", "\\n"); break;
829 case '\r': av_bprintf(dst, "%s", "\\r"); break;
830 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
833 av_bprint_chars(dst, '\\', 1);
834 av_bprint_chars(dst, *p, 1);
841 * Quote fields containing special characters, check RFC4180.
843 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
845 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
846 int needs_quoting = !!src[strcspn(src, meta_chars)];
849 av_bprint_chars(dst, '"', 1);
851 for (; *src; src++) {
853 av_bprint_chars(dst, '"', 1);
854 av_bprint_chars(dst, *src, 1);
857 av_bprint_chars(dst, '"', 1);
861 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
866 typedef struct CompactContext {
867 const AVClass *class;
872 char *escape_mode_str;
873 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
874 int nested_section[SECTION_MAX_NB_LEVELS];
875 int has_nested_elems[SECTION_MAX_NB_LEVELS];
876 int terminate_line[SECTION_MAX_NB_LEVELS];
880 #define OFFSET(x) offsetof(CompactContext, x)
882 static const AVOption compact_options[]= {
883 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
884 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
885 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
886 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
887 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
888 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
889 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
890 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
894 DEFINE_WRITER_CLASS(compact);
896 static av_cold int compact_init(WriterContext *wctx)
898 CompactContext *compact = wctx->priv;
900 if (strlen(compact->item_sep_str) != 1) {
901 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
902 compact->item_sep_str);
903 return AVERROR(EINVAL);
905 compact->item_sep = compact->item_sep_str[0];
907 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
908 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
909 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
911 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
912 return AVERROR(EINVAL);
918 static void compact_print_section_header(WriterContext *wctx)
920 CompactContext *compact = wctx->priv;
921 const struct section *section = wctx->section[wctx->level];
922 const struct section *parent_section = wctx->level ?
923 wctx->section[wctx->level-1] : NULL;
924 compact->terminate_line[wctx->level] = 1;
925 compact->has_nested_elems[wctx->level] = 0;
927 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
928 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
929 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
930 compact->nested_section[wctx->level] = 1;
931 compact->has_nested_elems[wctx->level-1] = 1;
932 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
933 wctx->section_pbuf[wctx->level-1].str,
934 (char *)av_x_if_null(section->element_name, section->name));
935 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
937 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
938 (section->flags & SECTION_FLAG_IS_ARRAY)) {
939 compact->terminate_line[wctx->level-1] = 0;
942 if (compact->print_section &&
943 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
944 printf("%s%c", section->name, compact->item_sep);
948 static void compact_print_section_footer(WriterContext *wctx)
950 CompactContext *compact = wctx->priv;
952 if (!compact->nested_section[wctx->level] &&
953 compact->terminate_line[wctx->level] &&
954 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
958 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
960 CompactContext *compact = wctx->priv;
963 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
965 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
966 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
967 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
968 av_bprint_finalize(&buf, NULL);
971 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
973 CompactContext *compact = wctx->priv;
975 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
977 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
978 printf("%lld", value);
981 static const Writer compact_writer = {
983 .priv_size = sizeof(CompactContext),
984 .init = compact_init,
985 .print_section_header = compact_print_section_header,
986 .print_section_footer = compact_print_section_footer,
987 .print_integer = compact_print_int,
988 .print_string = compact_print_str,
989 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
990 .priv_class = &compact_class,
996 #define OFFSET(x) offsetof(CompactContext, x)
998 static const AVOption csv_options[] = {
999 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
1000 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
1001 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1002 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1003 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1004 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1005 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1006 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1010 DEFINE_WRITER_CLASS(csv);
1012 static const Writer csv_writer = {
1014 .priv_size = sizeof(CompactContext),
1015 .init = compact_init,
1016 .print_section_header = compact_print_section_header,
1017 .print_section_footer = compact_print_section_footer,
1018 .print_integer = compact_print_int,
1019 .print_string = compact_print_str,
1020 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1021 .priv_class = &csv_class,
1026 typedef struct FlatContext {
1027 const AVClass *class;
1028 const char *sep_str;
1034 #define OFFSET(x) offsetof(FlatContext, x)
1036 static const AVOption flat_options[]= {
1037 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1038 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1039 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1040 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1044 DEFINE_WRITER_CLASS(flat);
1046 static av_cold int flat_init(WriterContext *wctx)
1048 FlatContext *flat = wctx->priv;
1050 if (strlen(flat->sep_str) != 1) {
1051 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1053 return AVERROR(EINVAL);
1055 flat->sep = flat->sep_str[0];
1060 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1064 for (p = src; *p; p++) {
1065 if (!((*p >= '0' && *p <= '9') ||
1066 (*p >= 'a' && *p <= 'z') ||
1067 (*p >= 'A' && *p <= 'Z')))
1068 av_bprint_chars(dst, '_', 1);
1070 av_bprint_chars(dst, *p, 1);
1075 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1079 for (p = src; *p; p++) {
1081 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1082 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1083 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1084 case '"': av_bprintf(dst, "%s", "\\\""); break;
1085 case '`': av_bprintf(dst, "%s", "\\`"); break;
1086 case '$': av_bprintf(dst, "%s", "\\$"); break;
1087 default: av_bprint_chars(dst, *p, 1); break;
1093 static void flat_print_section_header(WriterContext *wctx)
1095 FlatContext *flat = wctx->priv;
1096 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1097 const struct section *section = wctx->section[wctx->level];
1098 const struct section *parent_section = wctx->level ?
1099 wctx->section[wctx->level-1] : NULL;
1101 /* build section header */
1102 av_bprint_clear(buf);
1103 if (!parent_section)
1105 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1107 if (flat->hierarchical ||
1108 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1109 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1111 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1112 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1113 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1114 av_bprintf(buf, "%d%s", n, flat->sep_str);
1119 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1121 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1124 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1126 FlatContext *flat = wctx->priv;
1129 printf("%s", wctx->section_pbuf[wctx->level].str);
1130 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1131 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
1132 av_bprint_clear(&buf);
1133 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
1134 av_bprint_finalize(&buf, NULL);
1137 static const Writer flat_writer = {
1139 .priv_size = sizeof(FlatContext),
1141 .print_section_header = flat_print_section_header,
1142 .print_integer = flat_print_int,
1143 .print_string = flat_print_str,
1144 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1145 .priv_class = &flat_class,
1148 /* INI format output */
1151 const AVClass *class;
1156 #define OFFSET(x) offsetof(INIContext, x)
1158 static const AVOption ini_options[] = {
1159 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1160 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
1164 DEFINE_WRITER_CLASS(ini);
1166 static char *ini_escape_str(AVBPrint *dst, const char *src)
1171 while (c = src[i++]) {
1173 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1174 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1175 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1176 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1177 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1181 case ':' : av_bprint_chars(dst, '\\', 1);
1183 if ((unsigned char)c < 32)
1184 av_bprintf(dst, "\\x00%02x", c & 0xff);
1186 av_bprint_chars(dst, c, 1);
1193 static void ini_print_section_header(WriterContext *wctx)
1195 INIContext *ini = wctx->priv;
1196 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1197 const struct section *section = wctx->section[wctx->level];
1198 const struct section *parent_section = wctx->level ?
1199 wctx->section[wctx->level-1] : NULL;
1201 av_bprint_clear(buf);
1202 if (!parent_section) {
1203 printf("# ffprobe output\n\n");
1207 if (wctx->nb_item[wctx->level-1])
1210 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1211 if (ini->hierarchical ||
1212 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1213 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1215 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1216 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1217 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1218 av_bprintf(buf, ".%d", n);
1222 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1223 printf("[%s]\n", buf->str);
1226 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1230 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1231 printf("%s=", ini_escape_str(&buf, key));
1232 av_bprint_clear(&buf);
1233 printf("%s\n", ini_escape_str(&buf, value));
1234 av_bprint_finalize(&buf, NULL);
1237 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1239 printf("%s=%lld\n", key, value);
1242 static const Writer ini_writer = {
1244 .priv_size = sizeof(INIContext),
1245 .print_section_header = ini_print_section_header,
1246 .print_integer = ini_print_int,
1247 .print_string = ini_print_str,
1248 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1249 .priv_class = &ini_class,
1255 const AVClass *class;
1258 const char *item_sep, *item_start_end;
1262 #define OFFSET(x) offsetof(JSONContext, x)
1264 static const AVOption json_options[]= {
1265 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1266 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1270 DEFINE_WRITER_CLASS(json);
1272 static av_cold int json_init(WriterContext *wctx)
1274 JSONContext *json = wctx->priv;
1276 json->item_sep = json->compact ? ", " : ",\n";
1277 json->item_start_end = json->compact ? " " : "\n";
1282 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1284 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1285 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1288 for (p = src; *p; p++) {
1289 char *s = strchr(json_escape, *p);
1291 av_bprint_chars(dst, '\\', 1);
1292 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1293 } else if ((unsigned char)*p < 32) {
1294 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1296 av_bprint_chars(dst, *p, 1);
1302 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1304 static void json_print_section_header(WriterContext *wctx)
1306 JSONContext *json = wctx->priv;
1308 const struct section *section = wctx->section[wctx->level];
1309 const struct section *parent_section = wctx->level ?
1310 wctx->section[wctx->level-1] : NULL;
1312 if (wctx->level && wctx->nb_item[wctx->level-1])
1315 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1317 json->indent_level++;
1319 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1320 json_escape_str(&buf, section->name, wctx);
1323 json->indent_level++;
1324 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1325 printf("\"%s\": [\n", buf.str);
1326 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1327 printf("\"%s\": {%s", buf.str, json->item_start_end);
1329 printf("{%s", json->item_start_end);
1331 /* this is required so the parser can distinguish between packets and frames */
1332 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1335 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1338 av_bprint_finalize(&buf, NULL);
1342 static void json_print_section_footer(WriterContext *wctx)
1344 JSONContext *json = wctx->priv;
1345 const struct section *section = wctx->section[wctx->level];
1347 if (wctx->level == 0) {
1348 json->indent_level--;
1350 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1352 json->indent_level--;
1356 printf("%s", json->item_start_end);
1357 json->indent_level--;
1364 static inline void json_print_item_str(WriterContext *wctx,
1365 const char *key, const char *value)
1369 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1370 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1371 av_bprint_clear(&buf);
1372 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1373 av_bprint_finalize(&buf, NULL);
1376 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1378 JSONContext *json = wctx->priv;
1380 if (wctx->nb_item[wctx->level])
1381 printf("%s", json->item_sep);
1384 json_print_item_str(wctx, key, value);
1387 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1389 JSONContext *json = wctx->priv;
1392 if (wctx->nb_item[wctx->level])
1393 printf("%s", json->item_sep);
1397 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1398 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1399 av_bprint_finalize(&buf, NULL);
1402 static const Writer json_writer = {
1404 .priv_size = sizeof(JSONContext),
1406 .print_section_header = json_print_section_header,
1407 .print_section_footer = json_print_section_footer,
1408 .print_integer = json_print_int,
1409 .print_string = json_print_str,
1410 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1411 .priv_class = &json_class,
1417 const AVClass *class;
1420 int fully_qualified;
1425 #define OFFSET(x) offsetof(XMLContext, x)
1427 static const AVOption xml_options[] = {
1428 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1429 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1430 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1431 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1435 DEFINE_WRITER_CLASS(xml);
1437 static av_cold int xml_init(WriterContext *wctx)
1439 XMLContext *xml = wctx->priv;
1441 if (xml->xsd_strict) {
1442 xml->fully_qualified = 1;
1443 #define CHECK_COMPLIANCE(opt, opt_name) \
1445 av_log(wctx, AV_LOG_ERROR, \
1446 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1447 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1448 return AVERROR(EINVAL); \
1450 CHECK_COMPLIANCE(show_private_data, "private");
1451 CHECK_COMPLIANCE(show_value_unit, "unit");
1452 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1454 if (do_show_frames && do_show_packets) {
1455 av_log(wctx, AV_LOG_ERROR,
1456 "Interleaved frames and packets are not allowed in XSD. "
1457 "Select only one between the -show_frames and the -show_packets options.\n");
1458 return AVERROR(EINVAL);
1465 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1469 for (p = src; *p; p++) {
1471 case '&' : av_bprintf(dst, "%s", "&"); break;
1472 case '<' : av_bprintf(dst, "%s", "<"); break;
1473 case '>' : av_bprintf(dst, "%s", ">"); break;
1474 case '"' : av_bprintf(dst, "%s", """); break;
1475 case '\'': av_bprintf(dst, "%s", "'"); break;
1476 default: av_bprint_chars(dst, *p, 1);
1483 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1485 static void xml_print_section_header(WriterContext *wctx)
1487 XMLContext *xml = wctx->priv;
1488 const struct section *section = wctx->section[wctx->level];
1489 const struct section *parent_section = wctx->level ?
1490 wctx->section[wctx->level-1] : NULL;
1492 if (wctx->level == 0) {
1493 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1494 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1495 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1497 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1498 printf("<%sffprobe%s>\n",
1499 xml->fully_qualified ? "ffprobe:" : "",
1500 xml->fully_qualified ? qual : "");
1504 if (xml->within_tag) {
1505 xml->within_tag = 0;
1508 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1509 xml->indent_level++;
1511 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1512 wctx->level && wctx->nb_item[wctx->level-1])
1514 xml->indent_level++;
1516 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1517 XML_INDENT(); printf("<%s>\n", section->name);
1519 XML_INDENT(); printf("<%s ", section->name);
1520 xml->within_tag = 1;
1525 static void xml_print_section_footer(WriterContext *wctx)
1527 XMLContext *xml = wctx->priv;
1528 const struct section *section = wctx->section[wctx->level];
1530 if (wctx->level == 0) {
1531 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1532 } else if (xml->within_tag) {
1533 xml->within_tag = 0;
1535 xml->indent_level--;
1536 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1537 xml->indent_level--;
1539 XML_INDENT(); printf("</%s>\n", section->name);
1540 xml->indent_level--;
1544 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1547 XMLContext *xml = wctx->priv;
1548 const struct section *section = wctx->section[wctx->level];
1550 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1552 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1554 printf("<%s key=\"%s\"",
1555 section->element_name, xml_escape_str(&buf, key, wctx));
1556 av_bprint_clear(&buf);
1557 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1559 if (wctx->nb_item[wctx->level])
1561 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1564 av_bprint_finalize(&buf, NULL);
1567 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1569 if (wctx->nb_item[wctx->level])
1571 printf("%s=\"%lld\"", key, value);
1574 static Writer xml_writer = {
1576 .priv_size = sizeof(XMLContext),
1578 .print_section_header = xml_print_section_header,
1579 .print_section_footer = xml_print_section_footer,
1580 .print_integer = xml_print_int,
1581 .print_string = xml_print_str,
1582 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1583 .priv_class = &xml_class,
1586 static void writer_register_all(void)
1588 static int initialized;
1594 writer_register(&default_writer);
1595 writer_register(&compact_writer);
1596 writer_register(&csv_writer);
1597 writer_register(&flat_writer);
1598 writer_register(&ini_writer);
1599 writer_register(&json_writer);
1600 writer_register(&xml_writer);
1603 #define print_fmt(k, f, ...) do { \
1604 av_bprint_clear(&pbuf); \
1605 av_bprintf(&pbuf, f, __VA_ARGS__); \
1606 writer_print_string(w, k, pbuf.str, 0); \
1609 #define print_int(k, v) writer_print_integer(w, k, v)
1610 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1611 #define print_str(k, v) writer_print_string(w, k, v, 0)
1612 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1613 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1614 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1615 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1616 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1617 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1618 #define print_val(k, v, u) do { \
1619 struct unit_value uv; \
1622 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1625 #define print_section_header(s) writer_print_section_header(w, s)
1626 #define print_section_footer(s) writer_print_section_footer(w, s)
1628 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1630 AVDictionaryEntry *tag = NULL;
1635 writer_print_section_header(w, section_id);
1637 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1638 if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1641 writer_print_section_footer(w);
1646 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1649 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1653 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1655 writer_print_section_header(w, SECTION_ID_PACKET);
1657 s = av_get_media_type_string(st->codec->codec_type);
1658 if (s) print_str ("codec_type", s);
1659 else print_str_opt("codec_type", "unknown");
1660 print_int("stream_index", pkt->stream_index);
1661 print_ts ("pts", pkt->pts);
1662 print_time("pts_time", pkt->pts, &st->time_base);
1663 print_ts ("dts", pkt->dts);
1664 print_time("dts_time", pkt->dts, &st->time_base);
1665 print_duration_ts("duration", pkt->duration);
1666 print_duration_time("duration_time", pkt->duration, &st->time_base);
1667 print_duration_ts("convergence_duration", pkt->convergence_duration);
1668 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1669 print_val("size", pkt->size, unit_byte_str);
1670 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1671 else print_str_opt("pos", "N/A");
1672 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1674 writer_print_data(w, "data", pkt->data, pkt->size);
1675 writer_print_section_footer(w);
1677 av_bprint_finalize(&pbuf, NULL);
1681 static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
1682 AVFormatContext *fmt_ctx)
1686 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1688 writer_print_section_header(w, SECTION_ID_SUBTITLE);
1690 print_str ("media_type", "subtitle");
1691 print_ts ("pts", sub->pts);
1692 print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
1693 print_int ("format", sub->format);
1694 print_int ("start_display_time", sub->start_display_time);
1695 print_int ("end_display_time", sub->end_display_time);
1696 print_int ("num_rects", sub->num_rects);
1698 writer_print_section_footer(w);
1700 av_bprint_finalize(&pbuf, NULL);
1704 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1705 AVFormatContext *fmt_ctx)
1710 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1712 writer_print_section_header(w, SECTION_ID_FRAME);
1714 s = av_get_media_type_string(stream->codec->codec_type);
1715 if (s) print_str ("media_type", s);
1716 else print_str_opt("media_type", "unknown");
1717 print_int("key_frame", frame->key_frame);
1718 print_ts ("pkt_pts", frame->pkt_pts);
1719 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1720 print_ts ("pkt_dts", frame->pkt_dts);
1721 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1722 print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1723 print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1724 if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1725 else print_str_opt("pkt_pos", "N/A");
1726 if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1727 else print_str_opt("pkt_size", "N/A");
1729 switch (stream->codec->codec_type) {
1732 case AVMEDIA_TYPE_VIDEO:
1733 print_int("width", frame->width);
1734 print_int("height", frame->height);
1735 s = av_get_pix_fmt_name(frame->format);
1736 if (s) print_str ("pix_fmt", s);
1737 else print_str_opt("pix_fmt", "unknown");
1738 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1740 print_q("sample_aspect_ratio", sar, ':');
1742 print_str_opt("sample_aspect_ratio", "N/A");
1744 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1745 print_int("coded_picture_number", frame->coded_picture_number);
1746 print_int("display_picture_number", frame->display_picture_number);
1747 print_int("interlaced_frame", frame->interlaced_frame);
1748 print_int("top_field_first", frame->top_field_first);
1749 print_int("repeat_pict", frame->repeat_pict);
1752 case AVMEDIA_TYPE_AUDIO:
1753 s = av_get_sample_fmt_name(frame->format);
1754 if (s) print_str ("sample_fmt", s);
1755 else print_str_opt("sample_fmt", "unknown");
1756 print_int("nb_samples", frame->nb_samples);
1757 print_int("channels", av_frame_get_channels(frame));
1758 if (av_frame_get_channel_layout(frame)) {
1759 av_bprint_clear(&pbuf);
1760 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1761 av_frame_get_channel_layout(frame));
1762 print_str ("channel_layout", pbuf.str);
1764 print_str_opt("channel_layout", "unknown");
1767 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1769 writer_print_section_footer(w);
1771 av_bprint_finalize(&pbuf, NULL);
1775 static av_always_inline int process_frame(WriterContext *w,
1776 AVFormatContext *fmt_ctx,
1777 AVFrame *frame, AVPacket *pkt)
1779 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1781 int ret = 0, got_frame = 0;
1783 avcodec_get_frame_defaults(frame);
1784 if (dec_ctx->codec) {
1785 switch (dec_ctx->codec_type) {
1786 case AVMEDIA_TYPE_VIDEO:
1787 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1790 case AVMEDIA_TYPE_AUDIO:
1791 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1794 case AVMEDIA_TYPE_SUBTITLE:
1795 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
1802 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1806 int is_sub = (dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE);
1807 nb_streams_frames[pkt->stream_index]++;
1810 show_subtitle(w, &sub, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1812 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1814 avsubtitle_free(&sub);
1819 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
1821 av_log(log_ctx, log_level, "id:%d", interval->id);
1823 if (interval->has_start) {
1824 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
1825 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
1827 av_log(log_ctx, log_level, " start:N/A");
1830 if (interval->has_end) {
1831 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
1832 if (interval->duration_frames)
1833 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
1835 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
1837 av_log(log_ctx, log_level, " end:N/A");
1840 av_log(log_ctx, log_level, "\n");
1843 static int read_interval_packets(WriterContext *w, AVFormatContext *fmt_ctx,
1844 const ReadInterval *interval, int64_t *cur_ts)
1848 int ret = 0, i = 0, frame_count = 0;
1849 int64_t start = -INT64_MAX, end = interval->end;
1850 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
1852 av_init_packet(&pkt);
1854 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
1855 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
1857 if (interval->has_start) {
1859 if (interval->start_is_offset) {
1860 if (*cur_ts == AV_NOPTS_VALUE) {
1861 av_log(NULL, AV_LOG_ERROR,
1862 "Could not seek to relative position since current "
1863 "timestamp is not defined\n");
1864 ret = AVERROR(EINVAL);
1867 target = *cur_ts + interval->start;
1869 target = interval->start;
1872 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
1873 av_ts2timestr(target, &AV_TIME_BASE_Q));
1874 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
1875 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
1876 interval->start, av_err2str(ret));
1881 while (!av_read_frame(fmt_ctx, &pkt)) {
1882 if (selected_streams[pkt.stream_index]) {
1883 AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
1885 if (pkt.pts != AV_NOPTS_VALUE)
1886 *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
1888 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
1893 if (has_start && !has_end && interval->end_is_offset) {
1894 end = start + interval->end;
1898 if (interval->end_is_offset && interval->duration_frames) {
1899 if (frame_count >= interval->end)
1901 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
1906 if (do_read_packets) {
1907 if (do_show_packets)
1908 show_packet(w, fmt_ctx, &pkt, i++);
1909 nb_streams_packets[pkt.stream_index]++;
1911 if (do_read_frames) {
1913 while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1916 av_free_packet(&pkt);
1918 av_init_packet(&pkt);
1921 //Flush remaining frames that are cached in the decoder
1922 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1923 pkt.stream_index = i;
1925 while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1930 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
1931 log_read_interval(interval, NULL, AV_LOG_ERROR);
1936 static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1939 int64_t cur_ts = fmt_ctx->start_time;
1941 if (read_intervals_nb == 0) {
1942 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
1943 ret = read_interval_packets(w, fmt_ctx, &interval, &cur_ts);
1945 for (i = 0; i < read_intervals_nb; i++) {
1946 ret = read_interval_packets(w, fmt_ctx, &read_intervals[i], &cur_ts);
1955 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
1957 AVStream *stream = fmt_ctx->streams[stream_idx];
1958 AVCodecContext *dec_ctx;
1962 AVRational sar, dar;
1966 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1968 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
1970 print_int("index", stream->index);
1972 if ((dec_ctx = stream->codec)) {
1973 const char *profile = NULL;
1974 dec = dec_ctx->codec;
1976 print_str("codec_name", dec->name);
1978 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1979 else print_str_opt("codec_long_name", "unknown");
1982 print_str_opt("codec_name", "unknown");
1984 print_str_opt("codec_long_name", "unknown");
1988 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1989 print_str("profile", profile);
1991 print_str_opt("profile", "unknown");
1993 s = av_get_media_type_string(dec_ctx->codec_type);
1994 if (s) print_str ("codec_type", s);
1995 else print_str_opt("codec_type", "unknown");
1996 print_q("codec_time_base", dec_ctx->time_base, '/');
1998 /* print AVI/FourCC tag */
1999 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
2000 print_str("codec_tag_string", val_str);
2001 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
2003 switch (dec_ctx->codec_type) {
2004 case AVMEDIA_TYPE_VIDEO:
2005 print_int("width", dec_ctx->width);
2006 print_int("height", dec_ctx->height);
2007 print_int("has_b_frames", dec_ctx->has_b_frames);
2008 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
2010 print_q("sample_aspect_ratio", sar, ':');
2011 av_reduce(&dar.num, &dar.den,
2012 dec_ctx->width * sar.num,
2013 dec_ctx->height * sar.den,
2015 print_q("display_aspect_ratio", dar, ':');
2017 print_str_opt("sample_aspect_ratio", "N/A");
2018 print_str_opt("display_aspect_ratio", "N/A");
2020 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
2021 if (s) print_str ("pix_fmt", s);
2022 else print_str_opt("pix_fmt", "unknown");
2023 print_int("level", dec_ctx->level);
2024 if (dec_ctx->timecode_frame_start >= 0) {
2025 char tcbuf[AV_TIMECODE_STR_SIZE];
2026 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
2027 print_str("timecode", tcbuf);
2029 print_str_opt("timecode", "N/A");
2033 case AVMEDIA_TYPE_AUDIO:
2034 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
2035 if (s) print_str ("sample_fmt", s);
2036 else print_str_opt("sample_fmt", "unknown");
2037 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
2038 print_int("channels", dec_ctx->channels);
2040 if (dec_ctx->channel_layout) {
2041 av_bprint_clear(&pbuf);
2042 av_bprint_channel_layout(&pbuf, dec_ctx->channels, dec_ctx->channel_layout);
2043 print_str ("channel_layout", pbuf.str);
2045 print_str_opt("channel_layout", "unknown");
2048 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
2051 case AVMEDIA_TYPE_SUBTITLE:
2053 print_int("width", dec_ctx->width);
2055 print_str_opt("width", "N/A");
2056 if (dec_ctx->height)
2057 print_int("height", dec_ctx->height);
2059 print_str_opt("height", "N/A");
2063 print_str_opt("codec_type", "unknown");
2065 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
2066 const AVOption *opt = NULL;
2067 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
2069 if (opt->flags) continue;
2070 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
2071 print_str(opt->name, str);
2077 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
2078 else print_str_opt("id", "N/A");
2079 print_q("r_frame_rate", stream->r_frame_rate, '/');
2080 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
2081 print_q("time_base", stream->time_base, '/');
2082 print_ts ("start_pts", stream->start_time);
2083 print_time("start_time", stream->start_time, &stream->time_base);
2084 print_ts ("duration_ts", stream->duration);
2085 print_time("duration", stream->duration, &stream->time_base);
2086 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
2087 else print_str_opt("bit_rate", "N/A");
2088 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
2089 else print_str_opt("nb_frames", "N/A");
2090 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
2091 else print_str_opt("nb_read_frames", "N/A");
2092 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
2093 else print_str_opt("nb_read_packets", "N/A");
2095 writer_print_data(w, "extradata", dec_ctx->extradata,
2096 dec_ctx->extradata_size);
2098 /* Print disposition information */
2099 #define PRINT_DISPOSITION(flagname, name) do { \
2100 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
2103 if (do_show_stream_disposition) {
2104 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
2105 PRINT_DISPOSITION(DEFAULT, "default");
2106 PRINT_DISPOSITION(DUB, "dub");
2107 PRINT_DISPOSITION(ORIGINAL, "original");
2108 PRINT_DISPOSITION(COMMENT, "comment");
2109 PRINT_DISPOSITION(LYRICS, "lyrics");
2110 PRINT_DISPOSITION(KARAOKE, "karaoke");
2111 PRINT_DISPOSITION(FORCED, "forced");
2112 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
2113 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
2114 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
2115 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
2116 writer_print_section_footer(w);
2119 ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
2121 writer_print_section_footer(w);
2122 av_bprint_finalize(&pbuf, NULL);
2128 static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
2132 writer_print_section_header(w, SECTION_ID_STREAMS);
2133 for (i = 0; i < fmt_ctx->nb_streams; i++)
2134 if (selected_streams[i]) {
2135 ret = show_stream(w, fmt_ctx, i, 0);
2139 writer_print_section_footer(w);
2144 static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
2148 writer_print_section_header(w, SECTION_ID_PROGRAM);
2149 print_int("program_id", program->id);
2150 print_int("program_num", program->program_num);
2151 print_int("nb_streams", program->nb_stream_indexes);
2152 print_int("pmt_pid", program->pmt_pid);
2153 print_int("pcr_pid", program->pcr_pid);
2154 print_ts("start_pts", program->start_time);
2155 print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
2156 print_ts("end_pts", program->end_time);
2157 print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
2158 ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
2162 writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
2163 for (i = 0; i < program->nb_stream_indexes; i++) {
2164 if (selected_streams[program->stream_index[i]]) {
2165 ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
2170 writer_print_section_footer(w);
2173 writer_print_section_footer(w);
2177 static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
2181 writer_print_section_header(w, SECTION_ID_PROGRAMS);
2182 for (i = 0; i < fmt_ctx->nb_programs; i++) {
2183 AVProgram *program = fmt_ctx->programs[i];
2186 ret = show_program(w, fmt_ctx, program);
2190 writer_print_section_footer(w);
2194 static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
2198 writer_print_section_header(w, SECTION_ID_CHAPTERS);
2199 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
2200 AVChapter *chapter = fmt_ctx->chapters[i];
2202 writer_print_section_header(w, SECTION_ID_CHAPTER);
2203 print_int("id", chapter->id);
2204 print_q ("time_base", chapter->time_base, '/');
2205 print_int("start", chapter->start);
2206 print_time("start_time", chapter->start, &chapter->time_base);
2207 print_int("end", chapter->end);
2208 print_time("end_time", chapter->end, &chapter->time_base);
2209 ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
2210 writer_print_section_footer(w);
2212 writer_print_section_footer(w);
2217 static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
2220 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
2223 writer_print_section_header(w, SECTION_ID_FORMAT);
2224 print_str_validate("filename", fmt_ctx->filename);
2225 print_int("nb_streams", fmt_ctx->nb_streams);
2226 print_int("nb_programs", fmt_ctx->nb_programs);
2227 print_str("format_name", fmt_ctx->iformat->name);
2229 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2230 else print_str_opt("format_long_name", "unknown");
2232 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2233 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2234 if (size >= 0) print_val ("size", size, unit_byte_str);
2235 else print_str_opt("size", "N/A");
2236 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2237 else print_str_opt("bit_rate", "N/A");
2238 print_int("probe_score", av_format_get_probe_score(fmt_ctx));
2239 ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2241 writer_print_section_footer(w);
2246 static void show_error(WriterContext *w, int err)
2249 const char *errbuf_ptr = errbuf;
2251 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2252 errbuf_ptr = strerror(AVUNERROR(err));
2254 writer_print_section_header(w, SECTION_ID_ERROR);
2255 print_int("code", err);
2256 print_str("string", errbuf_ptr);
2257 writer_print_section_footer(w);
2260 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
2262 int err, i, orig_nb_streams;
2263 AVFormatContext *fmt_ctx = NULL;
2264 AVDictionaryEntry *t;
2265 AVDictionary **opts;
2267 if ((err = avformat_open_input(&fmt_ctx, filename,
2268 iformat, &format_opts)) < 0) {
2269 print_error(filename, err);
2272 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2273 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2274 return AVERROR_OPTION_NOT_FOUND;
2277 /* fill the streams in the format context */
2278 opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2279 orig_nb_streams = fmt_ctx->nb_streams;
2281 if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
2282 print_error(filename, err);
2285 for (i = 0; i < orig_nb_streams; i++)
2286 av_dict_free(&opts[i]);
2289 av_dump_format(fmt_ctx, 0, filename, 0);
2291 /* bind a decoder to each input stream */
2292 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2293 AVStream *stream = fmt_ctx->streams[i];
2296 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
2297 av_log(NULL, AV_LOG_WARNING,
2298 "Failed to probe codec for input stream %d\n",
2300 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
2301 av_log(NULL, AV_LOG_WARNING,
2302 "Unsupported codec with id %d for input stream %d\n",
2303 stream->codec->codec_id, stream->index);
2305 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
2306 fmt_ctx, stream, codec);
2307 if (avcodec_open2(stream->codec, codec, &opts) < 0) {
2308 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
2311 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2312 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
2313 t->key, stream->index);
2314 return AVERROR_OPTION_NOT_FOUND;
2319 *fmt_ctx_ptr = fmt_ctx;
2323 static void close_input_file(AVFormatContext **ctx_ptr)
2326 AVFormatContext *fmt_ctx = *ctx_ptr;
2328 /* close decoder for each stream */
2329 for (i = 0; i < fmt_ctx->nb_streams; i++)
2330 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
2331 avcodec_close(fmt_ctx->streams[i]->codec);
2333 avformat_close_input(ctx_ptr);
2336 static int probe_file(WriterContext *wctx, const char *filename)
2338 AVFormatContext *fmt_ctx;
2342 do_read_frames = do_show_frames || do_count_frames;
2343 do_read_packets = do_show_packets || do_count_packets;
2345 ret = open_input_file(&fmt_ctx, filename);
2349 #define CHECK_END if (ret < 0) goto end
2351 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
2352 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
2353 selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
2355 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2356 if (stream_specifier) {
2357 ret = avformat_match_stream_specifier(fmt_ctx,
2358 fmt_ctx->streams[i],
2362 selected_streams[i] = ret;
2365 selected_streams[i] = 1;
2369 if (do_read_frames || do_read_packets) {
2370 if (do_show_frames && do_show_packets &&
2371 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
2372 section_id = SECTION_ID_PACKETS_AND_FRAMES;
2373 else if (do_show_packets && !do_show_frames)
2374 section_id = SECTION_ID_PACKETS;
2375 else // (!do_show_packets && do_show_frames)
2376 section_id = SECTION_ID_FRAMES;
2377 if (do_show_frames || do_show_packets)
2378 writer_print_section_header(wctx, section_id);
2379 ret = read_packets(wctx, fmt_ctx);
2380 if (do_show_frames || do_show_packets)
2381 writer_print_section_footer(wctx);
2384 if (do_show_programs) {
2385 ret = show_programs(wctx, fmt_ctx);
2389 if (do_show_streams) {
2390 ret = show_streams(wctx, fmt_ctx);
2393 if (do_show_chapters) {
2394 ret = show_chapters(wctx, fmt_ctx);
2397 if (do_show_format) {
2398 ret = show_format(wctx, fmt_ctx);
2403 close_input_file(&fmt_ctx);
2404 av_freep(&nb_streams_frames);
2405 av_freep(&nb_streams_packets);
2406 av_freep(&selected_streams);
2411 static void show_usage(void)
2413 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
2414 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
2415 av_log(NULL, AV_LOG_INFO, "\n");
2418 static void ffprobe_show_program_version(WriterContext *w)
2421 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2423 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
2424 print_str("version", FFMPEG_VERSION);
2425 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2426 program_birth_year, CONFIG_THIS_YEAR);
2427 print_str("build_date", __DATE__);
2428 print_str("build_time", __TIME__);
2429 print_str("compiler_ident", CC_IDENT);
2430 print_str("configuration", FFMPEG_CONFIGURATION);
2431 writer_print_section_footer(w);
2433 av_bprint_finalize(&pbuf, NULL);
2436 #define SHOW_LIB_VERSION(libname, LIBNAME) \
2438 if (CONFIG_##LIBNAME) { \
2439 unsigned int version = libname##_version(); \
2440 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
2441 print_str("name", "lib" #libname); \
2442 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
2443 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
2444 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
2445 print_int("version", version); \
2446 print_str("ident", LIB##LIBNAME##_IDENT); \
2447 writer_print_section_footer(w); \
2451 static void ffprobe_show_library_versions(WriterContext *w)
2453 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
2454 SHOW_LIB_VERSION(avutil, AVUTIL);
2455 SHOW_LIB_VERSION(avcodec, AVCODEC);
2456 SHOW_LIB_VERSION(avformat, AVFORMAT);
2457 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2458 SHOW_LIB_VERSION(avfilter, AVFILTER);
2459 SHOW_LIB_VERSION(swscale, SWSCALE);
2460 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2461 SHOW_LIB_VERSION(postproc, POSTPROC);
2462 writer_print_section_footer(w);
2465 static int opt_format(void *optctx, const char *opt, const char *arg)
2467 iformat = av_find_input_format(arg);
2469 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2470 return AVERROR(EINVAL);
2475 static inline void mark_section_show_entries(SectionID section_id,
2476 int show_all_entries, AVDictionary *entries)
2478 struct section *section = §ions[section_id];
2480 section->show_all_entries = show_all_entries;
2481 if (show_all_entries) {
2483 for (id = section->children_ids; *id != -1; id++)
2484 mark_section_show_entries(*id, show_all_entries, entries);
2486 av_dict_copy(§ion->entries_to_show, entries, 0);
2490 static int match_section(const char *section_name,
2491 int show_all_entries, AVDictionary *entries)
2495 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
2496 const struct section *section = §ions[i];
2497 if (!strcmp(section_name, section->name) ||
2498 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2499 av_log(NULL, AV_LOG_DEBUG,
2500 "'%s' matches section with unique name '%s'\n", section_name,
2501 (char *)av_x_if_null(section->unique_name, section->name));
2503 mark_section_show_entries(section->id, show_all_entries, entries);
2509 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2511 const char *p = arg;
2515 AVDictionary *entries = NULL;
2516 char *section_name = av_get_token(&p, "=:");
2517 int show_all_entries = 0;
2519 if (!section_name) {
2520 av_log(NULL, AV_LOG_ERROR,
2521 "Missing section name for option '%s'\n", opt);
2522 return AVERROR(EINVAL);
2527 while (*p && *p != ':') {
2528 char *entry = av_get_token(&p, ",:");
2531 av_log(NULL, AV_LOG_VERBOSE,
2532 "Adding '%s' to the entries to show in section '%s'\n",
2533 entry, section_name);
2534 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2539 show_all_entries = 1;
2542 ret = match_section(section_name, show_all_entries, entries);
2544 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2545 ret = AVERROR(EINVAL);
2547 av_dict_free(&entries);
2548 av_free(section_name);
2559 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2561 char *buf = av_asprintf("format=%s", arg);
2564 av_log(NULL, AV_LOG_WARNING,
2565 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2567 ret = opt_show_entries(optctx, opt, buf);
2572 static void opt_input_file(void *optctx, const char *arg)
2574 if (input_filename) {
2575 av_log(NULL, AV_LOG_ERROR,
2576 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2577 arg, input_filename);
2580 if (!strcmp(arg, "-"))
2582 input_filename = arg;
2585 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2587 opt_input_file(optctx, arg);
2591 void show_help_default(const char *opt, const char *arg)
2593 av_log_set_callback(log_callback_help);
2595 show_help_options(options, "Main options:", 0, 0, 0);
2598 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2602 * Parse interval specification, according to the format:
2603 * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
2604 * INTERVALS ::= INTERVAL[,INTERVALS]
2606 static int parse_read_interval(const char *interval_spec,
2607 ReadInterval *interval)
2610 char *next, *p, *spec = av_strdup(interval_spec);
2612 return AVERROR(ENOMEM);
2615 av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
2616 ret = AVERROR(EINVAL);
2621 next = strchr(spec, '%');
2625 /* parse first part */
2627 interval->has_start = 1;
2630 interval->start_is_offset = 1;
2633 interval->start_is_offset = 0;
2636 ret = av_parse_time(&interval->start, p, 1);
2638 av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
2642 interval->has_start = 0;
2645 /* parse second part */
2649 interval->has_end = 1;
2652 interval->end_is_offset = 1;
2655 interval->end_is_offset = 0;
2658 if (interval->end_is_offset && *p == '#') {
2661 interval->duration_frames = 1;
2663 lli = strtoll(p, &tail, 10);
2664 if (*tail || lli < 0) {
2665 av_log(NULL, AV_LOG_ERROR,
2666 "Invalid or negative value '%s' for duration number of frames\n", p);
2669 interval->end = lli;
2671 ret = av_parse_time(&us, p, 1);
2673 av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
2679 interval->has_end = 0;
2687 static int parse_read_intervals(const char *intervals_spec)
2690 char *p, *spec = av_strdup(intervals_spec);
2692 return AVERROR(ENOMEM);
2694 /* preparse specification, get number of intervals */
2695 for (n = 0, p = spec; *p; p++)
2700 read_intervals = av_malloc(n * sizeof(*read_intervals));
2701 if (!read_intervals) {
2702 ret = AVERROR(ENOMEM);
2705 read_intervals_nb = n;
2707 /* parse intervals */
2709 for (i = 0; p; i++) {
2712 av_assert0(i < read_intervals_nb);
2713 next = strchr(p, ',');
2717 read_intervals[i].id = i;
2718 ret = parse_read_interval(p, &read_intervals[i]);
2720 av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
2724 av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
2725 log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
2728 av_assert0(i == read_intervals_nb);
2735 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
2737 return parse_read_intervals(arg);
2740 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2742 show_value_unit = 1;
2743 use_value_prefix = 1;
2744 use_byte_value_binary_prefix = 1;
2745 use_value_sexagesimal_format = 1;
2749 static void print_section(SectionID id, int level)
2751 const SectionID *pid;
2752 const struct section *section = §ions[id];
2754 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2755 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2756 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2757 printf("%*c %s", level * 4, ' ', section->name);
2758 if (section->unique_name)
2759 printf("/%s", section->unique_name);
2762 for (pid = section->children_ids; *pid != -1; pid++)
2763 print_section(*pid, level+1);
2766 static int opt_sections(void *optctx, const char *opt, const char *arg)
2768 printf("Sections:\n"
2769 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2770 ".A. = Section contains an array of elements of the same type\n"
2771 "..V = Section may contain a variable number of fields with variable keys\n"
2772 "FLAGS NAME/UNIQUE_NAME\n"
2774 print_section(SECTION_ID_ROOT, 0);
2778 static int opt_show_versions(const char *opt, const char *arg)
2780 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
2781 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
2785 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2786 static int opt_show_##section(const char *opt, const char *arg) \
2788 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2792 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS);
2793 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2794 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2795 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2796 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2797 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2798 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2799 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2800 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS);
2802 static const OptionDef real_options[] = {
2803 #include "cmdutils_common_opts.h"
2804 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2805 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2806 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2807 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2808 "use binary prefixes for byte units" },
2809 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2810 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2811 { "pretty", 0, {.func_arg = opt_pretty},
2812 "prettify the format of displayed values, make it more human readable" },
2813 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2814 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2815 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2816 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2817 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2818 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2819 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2820 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2821 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2822 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2823 "show a particular entry from the format/container info", "entry" },
2824 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2825 "show a set of specified entries", "entry_list" },
2826 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2827 { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
2828 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2829 { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
2830 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2831 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2832 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2833 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2834 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2835 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2836 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2837 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2838 { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
2839 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2840 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2844 static inline int check_section_show_entries(int section_id)
2847 struct section *section = §ions[section_id];
2848 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2850 for (id = section->children_ids; *id != -1; id++)
2851 if (check_section_show_entries(*id))
2856 #define SET_DO_SHOW(id, varname) do { \
2857 if (check_section_show_entries(SECTION_ID_##id)) \
2858 do_show_##varname = 1; \
2861 int main(int argc, char **argv)
2864 WriterContext *wctx;
2866 char *w_name = NULL, *w_args = NULL;
2869 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2870 register_exit(ffprobe_cleanup);
2872 options = real_options;
2873 parse_loglevel(argc, argv, options);
2875 avformat_network_init();
2878 avdevice_register_all();
2881 show_banner(argc, argv, options);
2882 parse_options(NULL, argc, argv, options, opt_input_file);
2884 /* mark things to show, based on -show_entries */
2885 SET_DO_SHOW(CHAPTERS, chapters);
2886 SET_DO_SHOW(ERROR, error);
2887 SET_DO_SHOW(FORMAT, format);
2888 SET_DO_SHOW(FRAMES, frames);
2889 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2890 SET_DO_SHOW(PACKETS, packets);
2891 SET_DO_SHOW(PROGRAM_VERSION, program_version);
2892 SET_DO_SHOW(PROGRAMS, programs);
2893 SET_DO_SHOW(STREAMS, streams);
2894 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2895 SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
2897 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2898 av_log(NULL, AV_LOG_ERROR,
2899 "-bitexact and -show_program_version or -show_library_versions "
2900 "options are incompatible\n");
2901 ret = AVERROR(EINVAL);
2905 writer_register_all();
2908 print_format = av_strdup("default");
2909 if (!print_format) {
2910 ret = AVERROR(ENOMEM);
2913 w_name = av_strtok(print_format, "=", &buf);
2916 w = writer_get_by_name(w_name);
2918 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2919 ret = AVERROR(EINVAL);
2923 if ((ret = writer_open(&wctx, w, w_args,
2924 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2925 if (w == &xml_writer)
2926 wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
2928 writer_print_section_header(wctx, SECTION_ID_ROOT);
2930 if (do_show_program_version)
2931 ffprobe_show_program_version(wctx);
2932 if (do_show_library_versions)
2933 ffprobe_show_library_versions(wctx);
2935 if (!input_filename &&
2936 ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
2937 (!do_show_program_version && !do_show_library_versions))) {
2939 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2940 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2941 ret = AVERROR(EINVAL);
2942 } else if (input_filename) {
2943 ret = probe_file(wctx, input_filename);
2944 if (ret < 0 && do_show_error)
2945 show_error(wctx, ret);
2948 writer_print_section_footer(wctx);
2949 writer_close(&wctx);
2953 av_freep(&print_format);
2954 av_freep(&read_intervals);
2957 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2958 av_dict_free(&(sections[i].entries_to_show));
2960 avformat_network_deinit();