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/display.h"
37 #include "libavutil/hash.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/libm.h"
43 #include "libavutil/parseutils.h"
44 #include "libavutil/timecode.h"
45 #include "libavutil/timestamp.h"
46 #include "libavdevice/avdevice.h"
47 #include "libswscale/swscale.h"
48 #include "libswresample/swresample.h"
49 #include "libpostproc/postprocess.h"
52 const char program_name[] = "ffprobe";
53 const int program_birth_year = 2007;
55 static int do_bitexact = 0;
56 static int do_count_frames = 0;
57 static int do_count_packets = 0;
58 static int do_read_frames = 0;
59 static int do_read_packets = 0;
60 static int do_show_chapters = 0;
61 static int do_show_error = 0;
62 static int do_show_format = 0;
63 static int do_show_frames = 0;
64 static int do_show_packets = 0;
65 static int do_show_programs = 0;
66 static int do_show_streams = 0;
67 static int do_show_stream_disposition = 0;
68 static int do_show_data = 0;
69 static int do_show_program_version = 0;
70 static int do_show_library_versions = 0;
71 static int do_show_pixel_formats = 0;
72 static int do_show_pixel_format_flags = 0;
73 static int do_show_pixel_format_components = 0;
75 static int do_show_chapter_tags = 0;
76 static int do_show_format_tags = 0;
77 static int do_show_frame_tags = 0;
78 static int do_show_program_tags = 0;
79 static int do_show_stream_tags = 0;
80 static int do_show_packet_tags = 0;
82 static int show_value_unit = 0;
83 static int use_value_prefix = 0;
84 static int use_byte_value_binary_prefix = 0;
85 static int use_value_sexagesimal_format = 0;
86 static int show_private_data = 1;
88 static char *print_format;
89 static char *stream_specifier;
90 static char *show_data_hash;
92 typedef struct ReadInterval {
93 int id; ///< identifier
94 int64_t start, end; ///< start, end in second/AV_TIME_BASE units
95 int has_start, has_end;
96 int start_is_offset, end_is_offset;
100 static ReadInterval *read_intervals;
101 static int read_intervals_nb = 0;
103 /* section structure definition */
105 #define SECTION_MAX_NB_CHILDREN 10
108 int id; ///< unique id identifying a section
111 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
112 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
113 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
114 /// For these sections the element_name field is mandatory.
116 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
117 const char *element_name; ///< name of the contained element, if provided
118 const char *unique_name; ///< unique section name, in case the name is ambiguous
119 AVDictionary *entries_to_show;
120 int show_all_entries;
124 SECTION_ID_NONE = -1,
126 SECTION_ID_CHAPTER_TAGS,
130 SECTION_ID_FORMAT_TAGS,
133 SECTION_ID_FRAME_TAGS,
134 SECTION_ID_FRAME_SIDE_DATA_LIST,
135 SECTION_ID_FRAME_SIDE_DATA,
136 SECTION_ID_LIBRARY_VERSION,
137 SECTION_ID_LIBRARY_VERSIONS,
139 SECTION_ID_PACKET_TAGS,
141 SECTION_ID_PACKETS_AND_FRAMES,
142 SECTION_ID_PACKET_SIDE_DATA_LIST,
143 SECTION_ID_PACKET_SIDE_DATA,
144 SECTION_ID_PIXEL_FORMAT,
145 SECTION_ID_PIXEL_FORMAT_FLAGS,
146 SECTION_ID_PIXEL_FORMAT_COMPONENT,
147 SECTION_ID_PIXEL_FORMAT_COMPONENTS,
148 SECTION_ID_PIXEL_FORMATS,
149 SECTION_ID_PROGRAM_STREAM_DISPOSITION,
150 SECTION_ID_PROGRAM_STREAM_TAGS,
152 SECTION_ID_PROGRAM_STREAMS,
153 SECTION_ID_PROGRAM_STREAM,
154 SECTION_ID_PROGRAM_TAGS,
155 SECTION_ID_PROGRAM_VERSION,
159 SECTION_ID_STREAM_DISPOSITION,
161 SECTION_ID_STREAM_TAGS,
162 SECTION_ID_STREAM_SIDE_DATA_LIST,
163 SECTION_ID_STREAM_SIDE_DATA,
167 static struct section sections[] = {
168 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
169 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
170 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
171 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
172 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
173 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
174 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, SECTION_ID_SUBTITLE, -1 } },
175 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, SECTION_ID_FRAME_SIDE_DATA_LIST, -1 } },
176 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
177 [SECTION_ID_FRAME_SIDE_DATA_LIST] ={ SECTION_ID_FRAME_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME_SIDE_DATA, -1 } },
178 [SECTION_ID_FRAME_SIDE_DATA] = { SECTION_ID_FRAME_SIDE_DATA, "side_data", 0, { -1 } },
179 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
180 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
181 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
182 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
183 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { SECTION_ID_PACKET_TAGS, SECTION_ID_PACKET_SIDE_DATA_LIST, -1 } },
184 [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
185 [SECTION_ID_PACKET_SIDE_DATA_LIST] ={ SECTION_ID_PACKET_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET_SIDE_DATA, -1 } },
186 [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", 0, { -1 } },
187 [SECTION_ID_PIXEL_FORMATS] = { SECTION_ID_PIXEL_FORMATS, "pixel_formats", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PIXEL_FORMAT, -1 } },
188 [SECTION_ID_PIXEL_FORMAT] = { SECTION_ID_PIXEL_FORMAT, "pixel_format", 0, { SECTION_ID_PIXEL_FORMAT_FLAGS, SECTION_ID_PIXEL_FORMAT_COMPONENTS, -1 } },
189 [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
190 [SECTION_ID_PIXEL_FORMAT_COMPONENTS] = { SECTION_ID_PIXEL_FORMAT_COMPONENTS, "components", SECTION_FLAG_IS_ARRAY, {SECTION_ID_PIXEL_FORMAT_COMPONENT, -1 }, .unique_name = "pixel_format_components" },
191 [SECTION_ID_PIXEL_FORMAT_COMPONENT] = { SECTION_ID_PIXEL_FORMAT_COMPONENT, "component", 0, { -1 } },
192 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
193 [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" },
194 [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
195 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
196 [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" },
197 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
198 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
199 [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
200 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
201 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAMS,
202 SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS,
203 SECTION_ID_PIXEL_FORMATS, -1} },
204 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
205 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, SECTION_ID_STREAM_SIDE_DATA_LIST, -1 } },
206 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
207 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
208 [SECTION_ID_STREAM_SIDE_DATA_LIST] ={ SECTION_ID_STREAM_SIDE_DATA_LIST, "side_data_list", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM_SIDE_DATA, -1 } },
209 [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", 0, { -1 } },
210 [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
213 static const OptionDef *options;
215 /* FFprobe context */
216 static const char *input_filename;
217 static AVInputFormat *iformat = NULL;
219 static struct AVHashContext *hash;
221 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
222 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
224 static const char unit_second_str[] = "s" ;
225 static const char unit_hertz_str[] = "Hz" ;
226 static const char unit_byte_str[] = "byte" ;
227 static const char unit_bit_per_second_str[] = "bit/s";
229 static int nb_streams;
230 static uint64_t *nb_streams_packets;
231 static uint64_t *nb_streams_frames;
232 static int *selected_streams;
234 static void ffprobe_cleanup(int ret)
237 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
238 av_dict_free(&(sections[i].entries_to_show));
242 union { double d; long long int i; } val;
246 static char *value_string(char *buf, int buf_size, struct unit_value uv)
252 if (uv.unit == unit_second_str) {
256 vald = vali = uv.val.i;
259 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
263 mins = (int)secs / 60;
264 secs = secs - mins * 60;
267 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
269 const char *prefix_string = "";
271 if (use_value_prefix && vald > 1) {
274 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
275 index = (long long int) (log2(vald)) / 10;
276 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
277 vald /= exp2(index * 10);
278 prefix_string = binary_unit_prefixes[index];
280 index = (long long int) (log10(vald)) / 3;
281 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
282 vald /= pow(10, index * 3);
283 prefix_string = decimal_unit_prefixes[index];
288 if (show_float || (use_value_prefix && vald != (long long int)vald))
289 snprintf(buf, buf_size, "%f", vald);
291 snprintf(buf, buf_size, "%lld", vali);
292 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
293 prefix_string, show_value_unit ? uv.unit : "");
301 typedef struct WriterContext WriterContext;
303 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
304 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
307 WRITER_STRING_VALIDATION_FAIL,
308 WRITER_STRING_VALIDATION_REPLACE,
309 WRITER_STRING_VALIDATION_IGNORE,
310 WRITER_STRING_VALIDATION_NB
313 typedef struct Writer {
314 const AVClass *priv_class; ///< private class of the writer, if any
315 int priv_size; ///< private size for the writer context
318 int (*init) (WriterContext *wctx);
319 void (*uninit)(WriterContext *wctx);
321 void (*print_section_header)(WriterContext *wctx);
322 void (*print_section_footer)(WriterContext *wctx);
323 void (*print_integer) (WriterContext *wctx, const char *, long long int);
324 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
325 void (*print_string) (WriterContext *wctx, const char *, const char *);
326 int flags; ///< a combination or WRITER_FLAG_*
329 #define SECTION_MAX_NB_LEVELS 10
331 struct WriterContext {
332 const AVClass *class; ///< class of the writer
333 const Writer *writer; ///< the Writer of which this is an instance
334 char *name; ///< name of this writer instance
335 void *priv; ///< private data for use by the filter
337 const struct section *sections; ///< array containing all sections
338 int nb_sections; ///< number of sections
340 int level; ///< current level, starting from 0
342 /** number of the item printed in the given section, starting from 0 */
343 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
345 /** section per each level */
346 const struct section *section[SECTION_MAX_NB_LEVELS];
347 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
348 /// used by various writers
350 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
351 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
352 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
354 int string_validation;
355 char *string_validation_replacement;
356 unsigned int string_validation_utf8_flags;
359 static const char *writer_get_name(void *p)
361 WriterContext *wctx = p;
362 return wctx->writer->name;
365 #define OFFSET(x) offsetof(WriterContext, x)
367 static const AVOption writer_options[] = {
368 { "string_validation", "set string validation mode",
369 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
370 { "sv", "set string validation mode",
371 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
372 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
373 { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
374 { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
375 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
376 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
380 static void *writer_child_next(void *obj, void *prev)
382 WriterContext *ctx = obj;
383 if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
388 static const AVClass writer_class = {
389 .class_name = "Writer",
390 .item_name = writer_get_name,
391 .option = writer_options,
392 .version = LIBAVUTIL_VERSION_INT,
393 .child_next = writer_child_next,
396 static void writer_close(WriterContext **wctx)
403 if ((*wctx)->writer->uninit)
404 (*wctx)->writer->uninit(*wctx);
405 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
406 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
407 if ((*wctx)->writer->priv_class)
408 av_opt_free((*wctx)->priv);
409 av_freep(&((*wctx)->priv));
414 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
417 av_bprintf(bp, "0X");
418 for (i = 0; i < ubuf_size; i++)
419 av_bprintf(bp, "%02X", ubuf[i]);
423 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
424 const struct section *sections, int nb_sections)
428 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
429 ret = AVERROR(ENOMEM);
433 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
434 ret = AVERROR(ENOMEM);
438 (*wctx)->class = &writer_class;
439 (*wctx)->writer = writer;
441 (*wctx)->sections = sections;
442 (*wctx)->nb_sections = nb_sections;
444 av_opt_set_defaults(*wctx);
446 if (writer->priv_class) {
447 void *priv_ctx = (*wctx)->priv;
448 *((const AVClass **)priv_ctx) = writer->priv_class;
449 av_opt_set_defaults(priv_ctx);
452 /* convert options to dictionary */
454 AVDictionary *opts = NULL;
455 AVDictionaryEntry *opt = NULL;
457 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
458 av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
463 while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
464 if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
465 av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
466 opt->key, opt->value);
475 /* validate replace string */
477 const uint8_t *p = (*wctx)->string_validation_replacement;
478 const uint8_t *endp = p + strlen(p);
480 const uint8_t *p0 = p;
482 ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
485 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
486 bprint_bytes(&bp, p0, p-p0),
487 av_log(wctx, AV_LOG_ERROR,
488 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
489 bp.str, (*wctx)->string_validation_replacement);
495 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
496 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
498 if ((*wctx)->writer->init)
499 ret = (*wctx)->writer->init(*wctx);
510 static inline void writer_print_section_header(WriterContext *wctx,
513 int parent_section_id;
515 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
516 parent_section_id = wctx->level ?
517 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
519 wctx->nb_item[wctx->level] = 0;
520 wctx->section[wctx->level] = &wctx->sections[section_id];
522 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
523 wctx->nb_section_packet = wctx->nb_section_frame =
524 wctx->nb_section_packet_frame = 0;
525 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
526 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
527 wctx->nb_section_packet : wctx->nb_section_frame;
530 if (wctx->writer->print_section_header)
531 wctx->writer->print_section_header(wctx);
534 static inline void writer_print_section_footer(WriterContext *wctx)
536 int section_id = wctx->section[wctx->level]->id;
537 int parent_section_id = wctx->level ?
538 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
540 if (parent_section_id != SECTION_ID_NONE)
541 wctx->nb_item[wctx->level-1]++;
542 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
543 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
544 else wctx->nb_section_frame++;
546 if (wctx->writer->print_section_footer)
547 wctx->writer->print_section_footer(wctx);
551 static inline void writer_print_integer(WriterContext *wctx,
552 const char *key, long long int val)
554 const struct section *section = wctx->section[wctx->level];
556 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
557 wctx->writer->print_integer(wctx, key, val);
558 wctx->nb_item[wctx->level]++;
562 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
564 const uint8_t *p, *endp;
566 int invalid_chars_nb = 0, ret = 0;
568 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
570 endp = src + strlen(src);
571 for (p = (uint8_t *)src; *p;) {
574 const uint8_t *p0 = p;
576 if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
578 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
579 bprint_bytes(&bp, p0, p-p0);
580 av_log(wctx, AV_LOG_DEBUG,
581 "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
588 switch (wctx->string_validation) {
589 case WRITER_STRING_VALIDATION_FAIL:
590 av_log(wctx, AV_LOG_ERROR,
591 "Invalid UTF-8 sequence found in string '%s'\n", src);
592 ret = AVERROR_INVALIDDATA;
596 case WRITER_STRING_VALIDATION_REPLACE:
597 av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
602 if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
603 av_bprint_append_data(&dstbuf, p0, p-p0);
606 if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
607 av_log(wctx, AV_LOG_WARNING,
608 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
609 invalid_chars_nb, src, wctx->string_validation_replacement);
613 av_bprint_finalize(&dstbuf, dstp);
617 #define PRINT_STRING_OPT 1
618 #define PRINT_STRING_VALIDATE 2
620 static inline int writer_print_string(WriterContext *wctx,
621 const char *key, const char *val, int flags)
623 const struct section *section = wctx->section[wctx->level];
626 if ((flags & PRINT_STRING_OPT)
627 && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
630 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
631 if (flags & PRINT_STRING_VALIDATE) {
632 char *key1 = NULL, *val1 = NULL;
633 ret = validate_string(wctx, &key1, key);
634 if (ret < 0) goto end;
635 ret = validate_string(wctx, &val1, val);
636 if (ret < 0) goto end;
637 wctx->writer->print_string(wctx, key1, val1);
640 av_log(wctx, AV_LOG_ERROR,
641 "Invalid key=value string combination %s=%s in section %s\n",
642 key, val, section->unique_name);
647 wctx->writer->print_string(wctx, key, val);
650 wctx->nb_item[wctx->level]++;
656 static inline void writer_print_rational(WriterContext *wctx,
657 const char *key, AVRational q, char sep)
660 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
661 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
662 writer_print_string(wctx, key, buf.str, 0);
665 static void writer_print_time(WriterContext *wctx, const char *key,
666 int64_t ts, const AVRational *time_base, int is_duration)
670 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
671 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
673 double d = ts * av_q2d(*time_base);
674 struct unit_value uv;
676 uv.unit = unit_second_str;
677 value_string(buf, sizeof(buf), uv);
678 writer_print_string(wctx, key, buf, 0);
682 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
684 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
685 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
687 writer_print_integer(wctx, key, ts);
691 static void writer_print_data(WriterContext *wctx, const char *name,
692 uint8_t *data, int size)
695 int offset = 0, l, i;
697 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
698 av_bprintf(&bp, "\n");
700 av_bprintf(&bp, "%08x: ", offset);
702 for (i = 0; i < l; i++) {
703 av_bprintf(&bp, "%02x", data[i]);
705 av_bprintf(&bp, " ");
707 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
708 for (i = 0; i < l; i++)
709 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
710 av_bprintf(&bp, "\n");
715 writer_print_string(wctx, name, bp.str, 0);
716 av_bprint_finalize(&bp, NULL);
719 static void writer_print_data_hash(WriterContext *wctx, const char *name,
720 uint8_t *data, int size)
722 char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
727 av_hash_update(hash, data, size);
728 snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
729 p = buf + strlen(buf);
730 av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
731 writer_print_string(wctx, name, buf, 0);
734 static void writer_print_integers(WriterContext *wctx, const char *name,
735 uint8_t *data, int size, const char *format,
736 int columns, int bytes, int offset_add)
739 int offset = 0, l, i;
741 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
742 av_bprintf(&bp, "\n");
744 av_bprintf(&bp, "%08x: ", offset);
745 l = FFMIN(size, columns);
746 for (i = 0; i < l; i++) {
747 if (bytes == 1) av_bprintf(&bp, format, *data);
748 else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
749 else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
753 av_bprintf(&bp, "\n");
754 offset += offset_add;
756 writer_print_string(wctx, name, bp.str, 0);
757 av_bprint_finalize(&bp, NULL);
760 #define MAX_REGISTERED_WRITERS_NB 64
762 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
764 static int writer_register(const Writer *writer)
766 static int next_registered_writer_idx = 0;
768 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
769 return AVERROR(ENOMEM);
771 registered_writers[next_registered_writer_idx++] = writer;
775 static const Writer *writer_get_by_name(const char *name)
779 for (i = 0; registered_writers[i]; i++)
780 if (!strcmp(registered_writers[i]->name, name))
781 return registered_writers[i];
789 #define DEFINE_WRITER_CLASS(name) \
790 static const char *name##_get_name(void *ctx) \
794 static const AVClass name##_class = { \
795 .class_name = #name, \
796 .item_name = name##_get_name, \
797 .option = name##_options \
802 typedef struct DefaultContext {
803 const AVClass *class;
805 int noprint_wrappers;
806 int nested_section[SECTION_MAX_NB_LEVELS];
810 #define OFFSET(x) offsetof(DefaultContext, x)
812 static const AVOption default_options[] = {
813 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
814 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
815 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
816 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
820 DEFINE_WRITER_CLASS(default);
822 /* lame uppercasing routine, assumes the string is lower case ASCII */
823 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
826 for (i = 0; src[i] && i < dst_size-1; i++)
827 dst[i] = av_toupper(src[i]);
832 static void default_print_section_header(WriterContext *wctx)
834 DefaultContext *def = wctx->priv;
836 const struct section *section = wctx->section[wctx->level];
837 const struct section *parent_section = wctx->level ?
838 wctx->section[wctx->level-1] : NULL;
840 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
841 if (parent_section &&
842 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
843 def->nested_section[wctx->level] = 1;
844 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
845 wctx->section_pbuf[wctx->level-1].str,
846 upcase_string(buf, sizeof(buf),
847 av_x_if_null(section->element_name, section->name)));
850 if (def->noprint_wrappers || def->nested_section[wctx->level])
853 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
854 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
857 static void default_print_section_footer(WriterContext *wctx)
859 DefaultContext *def = wctx->priv;
860 const struct section *section = wctx->section[wctx->level];
863 if (def->noprint_wrappers || def->nested_section[wctx->level])
866 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
867 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
870 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
872 DefaultContext *def = wctx->priv;
875 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
876 printf("%s\n", value);
879 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
881 DefaultContext *def = wctx->priv;
884 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
885 printf("%lld\n", value);
888 static const Writer default_writer = {
890 .priv_size = sizeof(DefaultContext),
891 .print_section_header = default_print_section_header,
892 .print_section_footer = default_print_section_footer,
893 .print_integer = default_print_int,
894 .print_string = default_print_str,
895 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
896 .priv_class = &default_class,
902 * Apply C-language-like string escaping.
904 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
908 for (p = src; *p; p++) {
910 case '\b': av_bprintf(dst, "%s", "\\b"); break;
911 case '\f': av_bprintf(dst, "%s", "\\f"); break;
912 case '\n': av_bprintf(dst, "%s", "\\n"); break;
913 case '\r': av_bprintf(dst, "%s", "\\r"); break;
914 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
917 av_bprint_chars(dst, '\\', 1);
918 av_bprint_chars(dst, *p, 1);
925 * Quote fields containing special characters, check RFC4180.
927 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
929 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
930 int needs_quoting = !!src[strcspn(src, meta_chars)];
933 av_bprint_chars(dst, '"', 1);
935 for (; *src; src++) {
937 av_bprint_chars(dst, '"', 1);
938 av_bprint_chars(dst, *src, 1);
941 av_bprint_chars(dst, '"', 1);
945 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
950 typedef struct CompactContext {
951 const AVClass *class;
956 char *escape_mode_str;
957 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
958 int nested_section[SECTION_MAX_NB_LEVELS];
959 int has_nested_elems[SECTION_MAX_NB_LEVELS];
960 int terminate_line[SECTION_MAX_NB_LEVELS];
964 #define OFFSET(x) offsetof(CompactContext, x)
966 static const AVOption compact_options[]= {
967 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
968 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
969 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
970 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
971 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
972 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
973 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
974 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
978 DEFINE_WRITER_CLASS(compact);
980 static av_cold int compact_init(WriterContext *wctx)
982 CompactContext *compact = wctx->priv;
984 if (strlen(compact->item_sep_str) != 1) {
985 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
986 compact->item_sep_str);
987 return AVERROR(EINVAL);
989 compact->item_sep = compact->item_sep_str[0];
991 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
992 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
993 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
995 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
996 return AVERROR(EINVAL);
1002 static void compact_print_section_header(WriterContext *wctx)
1004 CompactContext *compact = wctx->priv;
1005 const struct section *section = wctx->section[wctx->level];
1006 const struct section *parent_section = wctx->level ?
1007 wctx->section[wctx->level-1] : NULL;
1008 compact->terminate_line[wctx->level] = 1;
1009 compact->has_nested_elems[wctx->level] = 0;
1011 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1012 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
1013 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1014 compact->nested_section[wctx->level] = 1;
1015 compact->has_nested_elems[wctx->level-1] = 1;
1016 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1017 wctx->section_pbuf[wctx->level-1].str,
1018 (char *)av_x_if_null(section->element_name, section->name));
1019 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
1021 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
1022 (section->flags & SECTION_FLAG_IS_ARRAY)) {
1023 compact->terminate_line[wctx->level-1] = 0;
1026 if (compact->print_section &&
1027 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
1028 printf("%s%c", section->name, compact->item_sep);
1032 static void compact_print_section_footer(WriterContext *wctx)
1034 CompactContext *compact = wctx->priv;
1036 if (!compact->nested_section[wctx->level] &&
1037 compact->terminate_line[wctx->level] &&
1038 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
1042 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
1044 CompactContext *compact = wctx->priv;
1047 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
1048 if (!compact->nokey)
1049 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1050 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1051 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
1052 av_bprint_finalize(&buf, NULL);
1055 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
1057 CompactContext *compact = wctx->priv;
1059 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
1060 if (!compact->nokey)
1061 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
1062 printf("%lld", value);
1065 static const Writer compact_writer = {
1067 .priv_size = sizeof(CompactContext),
1068 .init = compact_init,
1069 .print_section_header = compact_print_section_header,
1070 .print_section_footer = compact_print_section_footer,
1071 .print_integer = compact_print_int,
1072 .print_string = compact_print_str,
1073 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1074 .priv_class = &compact_class,
1080 #define OFFSET(x) offsetof(CompactContext, x)
1082 static const AVOption csv_options[] = {
1083 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
1084 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
1085 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1086 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1087 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1088 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
1089 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1090 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1094 DEFINE_WRITER_CLASS(csv);
1096 static const Writer csv_writer = {
1098 .priv_size = sizeof(CompactContext),
1099 .init = compact_init,
1100 .print_section_header = compact_print_section_header,
1101 .print_section_footer = compact_print_section_footer,
1102 .print_integer = compact_print_int,
1103 .print_string = compact_print_str,
1104 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
1105 .priv_class = &csv_class,
1110 typedef struct FlatContext {
1111 const AVClass *class;
1112 const char *sep_str;
1118 #define OFFSET(x) offsetof(FlatContext, x)
1120 static const AVOption flat_options[]= {
1121 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1122 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
1123 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1124 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1128 DEFINE_WRITER_CLASS(flat);
1130 static av_cold int flat_init(WriterContext *wctx)
1132 FlatContext *flat = wctx->priv;
1134 if (strlen(flat->sep_str) != 1) {
1135 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1137 return AVERROR(EINVAL);
1139 flat->sep = flat->sep_str[0];
1144 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1148 for (p = src; *p; p++) {
1149 if (!((*p >= '0' && *p <= '9') ||
1150 (*p >= 'a' && *p <= 'z') ||
1151 (*p >= 'A' && *p <= 'Z')))
1152 av_bprint_chars(dst, '_', 1);
1154 av_bprint_chars(dst, *p, 1);
1159 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1163 for (p = src; *p; p++) {
1165 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1166 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1167 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1168 case '"': av_bprintf(dst, "%s", "\\\""); break;
1169 case '`': av_bprintf(dst, "%s", "\\`"); break;
1170 case '$': av_bprintf(dst, "%s", "\\$"); break;
1171 default: av_bprint_chars(dst, *p, 1); break;
1177 static void flat_print_section_header(WriterContext *wctx)
1179 FlatContext *flat = wctx->priv;
1180 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1181 const struct section *section = wctx->section[wctx->level];
1182 const struct section *parent_section = wctx->level ?
1183 wctx->section[wctx->level-1] : NULL;
1185 /* build section header */
1186 av_bprint_clear(buf);
1187 if (!parent_section)
1189 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1191 if (flat->hierarchical ||
1192 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1193 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1195 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1196 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1197 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1198 av_bprintf(buf, "%d%s", n, flat->sep_str);
1203 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1205 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1208 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1210 FlatContext *flat = wctx->priv;
1213 printf("%s", wctx->section_pbuf[wctx->level].str);
1214 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1215 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
1216 av_bprint_clear(&buf);
1217 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
1218 av_bprint_finalize(&buf, NULL);
1221 static const Writer flat_writer = {
1223 .priv_size = sizeof(FlatContext),
1225 .print_section_header = flat_print_section_header,
1226 .print_integer = flat_print_int,
1227 .print_string = flat_print_str,
1228 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1229 .priv_class = &flat_class,
1232 /* INI format output */
1234 typedef struct INIContext {
1235 const AVClass *class;
1240 #define OFFSET(x) offsetof(INIContext, x)
1242 static const AVOption ini_options[] = {
1243 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1244 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1248 DEFINE_WRITER_CLASS(ini);
1250 static char *ini_escape_str(AVBPrint *dst, const char *src)
1255 while (c = src[i++]) {
1257 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1258 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1259 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1260 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1261 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1265 case ':' : av_bprint_chars(dst, '\\', 1);
1267 if ((unsigned char)c < 32)
1268 av_bprintf(dst, "\\x00%02x", c & 0xff);
1270 av_bprint_chars(dst, c, 1);
1277 static void ini_print_section_header(WriterContext *wctx)
1279 INIContext *ini = wctx->priv;
1280 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1281 const struct section *section = wctx->section[wctx->level];
1282 const struct section *parent_section = wctx->level ?
1283 wctx->section[wctx->level-1] : NULL;
1285 av_bprint_clear(buf);
1286 if (!parent_section) {
1287 printf("# ffprobe output\n\n");
1291 if (wctx->nb_item[wctx->level-1])
1294 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1295 if (ini->hierarchical ||
1296 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1297 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1299 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1300 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1301 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1302 av_bprintf(buf, ".%d", n);
1306 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1307 printf("[%s]\n", buf->str);
1310 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1314 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1315 printf("%s=", ini_escape_str(&buf, key));
1316 av_bprint_clear(&buf);
1317 printf("%s\n", ini_escape_str(&buf, value));
1318 av_bprint_finalize(&buf, NULL);
1321 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1323 printf("%s=%lld\n", key, value);
1326 static const Writer ini_writer = {
1328 .priv_size = sizeof(INIContext),
1329 .print_section_header = ini_print_section_header,
1330 .print_integer = ini_print_int,
1331 .print_string = ini_print_str,
1332 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1333 .priv_class = &ini_class,
1338 typedef struct JSONContext {
1339 const AVClass *class;
1342 const char *item_sep, *item_start_end;
1346 #define OFFSET(x) offsetof(JSONContext, x)
1348 static const AVOption json_options[]= {
1349 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1350 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1354 DEFINE_WRITER_CLASS(json);
1356 static av_cold int json_init(WriterContext *wctx)
1358 JSONContext *json = wctx->priv;
1360 json->item_sep = json->compact ? ", " : ",\n";
1361 json->item_start_end = json->compact ? " " : "\n";
1366 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1368 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1369 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1372 for (p = src; *p; p++) {
1373 char *s = strchr(json_escape, *p);
1375 av_bprint_chars(dst, '\\', 1);
1376 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1377 } else if ((unsigned char)*p < 32) {
1378 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1380 av_bprint_chars(dst, *p, 1);
1386 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1388 static void json_print_section_header(WriterContext *wctx)
1390 JSONContext *json = wctx->priv;
1392 const struct section *section = wctx->section[wctx->level];
1393 const struct section *parent_section = wctx->level ?
1394 wctx->section[wctx->level-1] : NULL;
1396 if (wctx->level && wctx->nb_item[wctx->level-1])
1399 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1401 json->indent_level++;
1403 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1404 json_escape_str(&buf, section->name, wctx);
1407 json->indent_level++;
1408 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1409 printf("\"%s\": [\n", buf.str);
1410 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1411 printf("\"%s\": {%s", buf.str, json->item_start_end);
1413 printf("{%s", json->item_start_end);
1415 /* this is required so the parser can distinguish between packets and frames */
1416 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1419 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1422 av_bprint_finalize(&buf, NULL);
1426 static void json_print_section_footer(WriterContext *wctx)
1428 JSONContext *json = wctx->priv;
1429 const struct section *section = wctx->section[wctx->level];
1431 if (wctx->level == 0) {
1432 json->indent_level--;
1434 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1436 json->indent_level--;
1440 printf("%s", json->item_start_end);
1441 json->indent_level--;
1448 static inline void json_print_item_str(WriterContext *wctx,
1449 const char *key, const char *value)
1453 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1454 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1455 av_bprint_clear(&buf);
1456 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1457 av_bprint_finalize(&buf, NULL);
1460 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1462 JSONContext *json = wctx->priv;
1464 if (wctx->nb_item[wctx->level])
1465 printf("%s", json->item_sep);
1468 json_print_item_str(wctx, key, value);
1471 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1473 JSONContext *json = wctx->priv;
1476 if (wctx->nb_item[wctx->level])
1477 printf("%s", json->item_sep);
1481 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1482 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1483 av_bprint_finalize(&buf, NULL);
1486 static const Writer json_writer = {
1488 .priv_size = sizeof(JSONContext),
1490 .print_section_header = json_print_section_header,
1491 .print_section_footer = json_print_section_footer,
1492 .print_integer = json_print_int,
1493 .print_string = json_print_str,
1494 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1495 .priv_class = &json_class,
1500 typedef struct XMLContext {
1501 const AVClass *class;
1504 int fully_qualified;
1509 #define OFFSET(x) offsetof(XMLContext, x)
1511 static const AVOption xml_options[] = {
1512 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1513 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1514 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1515 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1519 DEFINE_WRITER_CLASS(xml);
1521 static av_cold int xml_init(WriterContext *wctx)
1523 XMLContext *xml = wctx->priv;
1525 if (xml->xsd_strict) {
1526 xml->fully_qualified = 1;
1527 #define CHECK_COMPLIANCE(opt, opt_name) \
1529 av_log(wctx, AV_LOG_ERROR, \
1530 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1531 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1532 return AVERROR(EINVAL); \
1534 CHECK_COMPLIANCE(show_private_data, "private");
1535 CHECK_COMPLIANCE(show_value_unit, "unit");
1536 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1538 if (do_show_frames && do_show_packets) {
1539 av_log(wctx, AV_LOG_ERROR,
1540 "Interleaved frames and packets are not allowed in XSD. "
1541 "Select only one between the -show_frames and the -show_packets options.\n");
1542 return AVERROR(EINVAL);
1549 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1553 for (p = src; *p; p++) {
1555 case '&' : av_bprintf(dst, "%s", "&"); break;
1556 case '<' : av_bprintf(dst, "%s", "<"); break;
1557 case '>' : av_bprintf(dst, "%s", ">"); break;
1558 case '"' : av_bprintf(dst, "%s", """); break;
1559 case '\'': av_bprintf(dst, "%s", "'"); break;
1560 default: av_bprint_chars(dst, *p, 1);
1567 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1569 static void xml_print_section_header(WriterContext *wctx)
1571 XMLContext *xml = wctx->priv;
1572 const struct section *section = wctx->section[wctx->level];
1573 const struct section *parent_section = wctx->level ?
1574 wctx->section[wctx->level-1] : NULL;
1576 if (wctx->level == 0) {
1577 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1578 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1579 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1581 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1582 printf("<%sffprobe%s>\n",
1583 xml->fully_qualified ? "ffprobe:" : "",
1584 xml->fully_qualified ? qual : "");
1588 if (xml->within_tag) {
1589 xml->within_tag = 0;
1592 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1593 xml->indent_level++;
1595 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1596 wctx->level && wctx->nb_item[wctx->level-1])
1598 xml->indent_level++;
1600 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1601 XML_INDENT(); printf("<%s>\n", section->name);
1603 XML_INDENT(); printf("<%s ", section->name);
1604 xml->within_tag = 1;
1609 static void xml_print_section_footer(WriterContext *wctx)
1611 XMLContext *xml = wctx->priv;
1612 const struct section *section = wctx->section[wctx->level];
1614 if (wctx->level == 0) {
1615 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1616 } else if (xml->within_tag) {
1617 xml->within_tag = 0;
1619 xml->indent_level--;
1620 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1621 xml->indent_level--;
1623 XML_INDENT(); printf("</%s>\n", section->name);
1624 xml->indent_level--;
1628 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1631 XMLContext *xml = wctx->priv;
1632 const struct section *section = wctx->section[wctx->level];
1634 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1636 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1638 printf("<%s key=\"%s\"",
1639 section->element_name, xml_escape_str(&buf, key, wctx));
1640 av_bprint_clear(&buf);
1641 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1643 if (wctx->nb_item[wctx->level])
1645 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1648 av_bprint_finalize(&buf, NULL);
1651 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1653 if (wctx->nb_item[wctx->level])
1655 printf("%s=\"%lld\"", key, value);
1658 static Writer xml_writer = {
1660 .priv_size = sizeof(XMLContext),
1662 .print_section_header = xml_print_section_header,
1663 .print_section_footer = xml_print_section_footer,
1664 .print_integer = xml_print_int,
1665 .print_string = xml_print_str,
1666 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1667 .priv_class = &xml_class,
1670 static void writer_register_all(void)
1672 static int initialized;
1678 writer_register(&default_writer);
1679 writer_register(&compact_writer);
1680 writer_register(&csv_writer);
1681 writer_register(&flat_writer);
1682 writer_register(&ini_writer);
1683 writer_register(&json_writer);
1684 writer_register(&xml_writer);
1687 #define print_fmt(k, f, ...) do { \
1688 av_bprint_clear(&pbuf); \
1689 av_bprintf(&pbuf, f, __VA_ARGS__); \
1690 writer_print_string(w, k, pbuf.str, 0); \
1693 #define print_int(k, v) writer_print_integer(w, k, v)
1694 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1695 #define print_str(k, v) writer_print_string(w, k, v, 0)
1696 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1697 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1698 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1699 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1700 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1701 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1702 #define print_val(k, v, u) do { \
1703 struct unit_value uv; \
1706 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1709 #define print_section_header(s) writer_print_section_header(w, s)
1710 #define print_section_footer(s) writer_print_section_footer(w, s)
1712 #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
1714 ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
1717 memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
1720 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1722 AVDictionaryEntry *tag = NULL;
1727 writer_print_section_header(w, section_id);
1729 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1730 if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1733 writer_print_section_footer(w);
1738 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1741 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1745 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1747 writer_print_section_header(w, SECTION_ID_PACKET);
1749 s = av_get_media_type_string(st->codec->codec_type);
1750 if (s) print_str ("codec_type", s);
1751 else print_str_opt("codec_type", "unknown");
1752 print_int("stream_index", pkt->stream_index);
1753 print_ts ("pts", pkt->pts);
1754 print_time("pts_time", pkt->pts, &st->time_base);
1755 print_ts ("dts", pkt->dts);
1756 print_time("dts_time", pkt->dts, &st->time_base);
1757 print_duration_ts("duration", pkt->duration);
1758 print_duration_time("duration_time", pkt->duration, &st->time_base);
1759 print_duration_ts("convergence_duration", pkt->convergence_duration);
1760 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1761 print_val("size", pkt->size, unit_byte_str);
1762 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1763 else print_str_opt("pos", "N/A");
1764 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1766 if (pkt->side_data_elems) {
1769 const uint8_t *side_metadata;
1771 side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
1772 if (side_metadata && size && do_show_packet_tags) {
1773 AVDictionary *dict = NULL;
1774 if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
1775 show_tags(w, dict, SECTION_ID_PACKET_TAGS);
1776 av_dict_free(&dict);
1778 writer_print_section_header(w, SECTION_ID_PACKET_SIDE_DATA_LIST);
1779 for (i = 0; i < pkt->side_data_elems; i++) {
1780 AVPacketSideData *sd = &pkt->side_data[i];
1781 const char *name = av_packet_side_data_name(sd->type);
1782 writer_print_section_header(w, SECTION_ID_PACKET_SIDE_DATA);
1783 print_str("side_data_type", name ? name : "unknown");
1784 print_int("side_data_size", sd->size);
1785 if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
1786 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
1787 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
1789 writer_print_section_footer(w);
1791 writer_print_section_footer(w);
1795 writer_print_data(w, "data", pkt->data, pkt->size);
1796 writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
1797 writer_print_section_footer(w);
1799 av_bprint_finalize(&pbuf, NULL);
1803 static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
1804 AVFormatContext *fmt_ctx)
1808 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1810 writer_print_section_header(w, SECTION_ID_SUBTITLE);
1812 print_str ("media_type", "subtitle");
1813 print_ts ("pts", sub->pts);
1814 print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
1815 print_int ("format", sub->format);
1816 print_int ("start_display_time", sub->start_display_time);
1817 print_int ("end_display_time", sub->end_display_time);
1818 print_int ("num_rects", sub->num_rects);
1820 writer_print_section_footer(w);
1822 av_bprint_finalize(&pbuf, NULL);
1826 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1827 AVFormatContext *fmt_ctx)
1834 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1836 writer_print_section_header(w, SECTION_ID_FRAME);
1838 s = av_get_media_type_string(stream->codec->codec_type);
1839 if (s) print_str ("media_type", s);
1840 else print_str_opt("media_type", "unknown");
1841 print_int("stream_index", stream->index);
1842 print_int("key_frame", frame->key_frame);
1843 print_ts ("pkt_pts", frame->pkt_pts);
1844 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1845 print_ts ("pkt_dts", frame->pkt_dts);
1846 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1847 print_ts ("best_effort_timestamp", av_frame_get_best_effort_timestamp(frame));
1848 print_time("best_effort_timestamp_time", av_frame_get_best_effort_timestamp(frame), &stream->time_base);
1849 print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1850 print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1851 if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1852 else print_str_opt("pkt_pos", "N/A");
1853 if (av_frame_get_pkt_size(frame) != -1) print_val ("pkt_size", av_frame_get_pkt_size(frame), unit_byte_str);
1854 else print_str_opt("pkt_size", "N/A");
1856 switch (stream->codec->codec_type) {
1859 case AVMEDIA_TYPE_VIDEO:
1860 print_int("width", frame->width);
1861 print_int("height", frame->height);
1862 s = av_get_pix_fmt_name(frame->format);
1863 if (s) print_str ("pix_fmt", s);
1864 else print_str_opt("pix_fmt", "unknown");
1865 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1867 print_q("sample_aspect_ratio", sar, ':');
1869 print_str_opt("sample_aspect_ratio", "N/A");
1871 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1872 print_int("coded_picture_number", frame->coded_picture_number);
1873 print_int("display_picture_number", frame->display_picture_number);
1874 print_int("interlaced_frame", frame->interlaced_frame);
1875 print_int("top_field_first", frame->top_field_first);
1876 print_int("repeat_pict", frame->repeat_pict);
1879 case AVMEDIA_TYPE_AUDIO:
1880 s = av_get_sample_fmt_name(frame->format);
1881 if (s) print_str ("sample_fmt", s);
1882 else print_str_opt("sample_fmt", "unknown");
1883 print_int("nb_samples", frame->nb_samples);
1884 print_int("channels", av_frame_get_channels(frame));
1885 if (av_frame_get_channel_layout(frame)) {
1886 av_bprint_clear(&pbuf);
1887 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1888 av_frame_get_channel_layout(frame));
1889 print_str ("channel_layout", pbuf.str);
1891 print_str_opt("channel_layout", "unknown");
1894 if (do_show_frame_tags)
1895 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1896 if (frame->nb_side_data) {
1897 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_LIST);
1898 for (i = 0; i < frame->nb_side_data; i++) {
1899 AVFrameSideData *sd = frame->side_data[i];
1902 writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA);
1903 name = av_frame_side_data_name(sd->type);
1904 print_str("side_data_type", name ? name : "unknown");
1905 print_int("side_data_size", sd->size);
1906 if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
1907 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
1908 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
1910 writer_print_section_footer(w);
1912 writer_print_section_footer(w);
1915 writer_print_section_footer(w);
1917 av_bprint_finalize(&pbuf, NULL);
1921 static av_always_inline int process_frame(WriterContext *w,
1922 AVFormatContext *fmt_ctx,
1923 AVFrame *frame, AVPacket *pkt)
1925 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1927 int ret = 0, got_frame = 0;
1929 if (dec_ctx->codec) {
1930 switch (dec_ctx->codec_type) {
1931 case AVMEDIA_TYPE_VIDEO:
1932 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1935 case AVMEDIA_TYPE_AUDIO:
1936 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1939 case AVMEDIA_TYPE_SUBTITLE:
1940 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
1947 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1951 int is_sub = (dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE);
1952 nb_streams_frames[pkt->stream_index]++;
1955 show_subtitle(w, &sub, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1957 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1959 avsubtitle_free(&sub);
1964 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
1966 av_log(log_ctx, log_level, "id:%d", interval->id);
1968 if (interval->has_start) {
1969 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
1970 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
1972 av_log(log_ctx, log_level, " start:N/A");
1975 if (interval->has_end) {
1976 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
1977 if (interval->duration_frames)
1978 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
1980 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
1982 av_log(log_ctx, log_level, " end:N/A");
1985 av_log(log_ctx, log_level, "\n");
1988 static int read_interval_packets(WriterContext *w, AVFormatContext *fmt_ctx,
1989 const ReadInterval *interval, int64_t *cur_ts)
1992 AVFrame *frame = NULL;
1993 int ret = 0, i = 0, frame_count = 0;
1994 int64_t start = -INT64_MAX, end = interval->end;
1995 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
1997 av_init_packet(&pkt);
1999 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
2000 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
2002 if (interval->has_start) {
2004 if (interval->start_is_offset) {
2005 if (*cur_ts == AV_NOPTS_VALUE) {
2006 av_log(NULL, AV_LOG_ERROR,
2007 "Could not seek to relative position since current "
2008 "timestamp is not defined\n");
2009 ret = AVERROR(EINVAL);
2012 target = *cur_ts + interval->start;
2014 target = interval->start;
2017 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
2018 av_ts2timestr(target, &AV_TIME_BASE_Q));
2019 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
2020 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
2021 interval->start, av_err2str(ret));
2026 frame = av_frame_alloc();
2028 ret = AVERROR(ENOMEM);
2031 while (!av_read_frame(fmt_ctx, &pkt)) {
2032 if (fmt_ctx->nb_streams > nb_streams) {
2033 REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
2034 REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
2035 REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
2036 nb_streams = fmt_ctx->nb_streams;
2038 if (selected_streams[pkt.stream_index]) {
2039 AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
2041 if (pkt.pts != AV_NOPTS_VALUE)
2042 *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
2044 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
2049 if (has_start && !has_end && interval->end_is_offset) {
2050 end = start + interval->end;
2054 if (interval->end_is_offset && interval->duration_frames) {
2055 if (frame_count >= interval->end)
2057 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
2062 if (do_read_packets) {
2063 if (do_show_packets)
2064 show_packet(w, fmt_ctx, &pkt, i++);
2065 nb_streams_packets[pkt.stream_index]++;
2067 if (do_read_frames) {
2069 while (pkt1.size && process_frame(w, fmt_ctx, frame, &pkt1) > 0);
2072 av_packet_unref(&pkt);
2074 av_init_packet(&pkt);
2077 //Flush remaining frames that are cached in the decoder
2078 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2079 pkt.stream_index = i;
2081 while (process_frame(w, fmt_ctx, frame, &pkt) > 0);
2085 av_frame_free(&frame);
2087 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
2088 log_read_interval(interval, NULL, AV_LOG_ERROR);
2093 static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
2096 int64_t cur_ts = fmt_ctx->start_time;
2098 if (read_intervals_nb == 0) {
2099 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
2100 ret = read_interval_packets(w, fmt_ctx, &interval, &cur_ts);
2102 for (i = 0; i < read_intervals_nb; i++) {
2103 ret = read_interval_packets(w, fmt_ctx, &read_intervals[i], &cur_ts);
2112 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
2114 AVStream *stream = fmt_ctx->streams[stream_idx];
2115 AVCodecContext *dec_ctx;
2119 AVRational sar, dar;
2121 const AVCodecDescriptor *cd;
2124 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2126 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
2128 print_int("index", stream->index);
2130 if ((dec_ctx = stream->codec)) {
2131 const char *profile = NULL;
2132 dec = dec_ctx->codec;
2134 print_str("codec_name", dec->name);
2136 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
2137 else print_str_opt("codec_long_name", "unknown");
2139 } else if ((cd = avcodec_descriptor_get(stream->codec->codec_id))) {
2140 print_str_opt("codec_name", cd->name);
2142 print_str_opt("codec_long_name",
2143 cd->long_name ? cd->long_name : "unknown");
2146 print_str_opt("codec_name", "unknown");
2148 print_str_opt("codec_long_name", "unknown");
2152 if (!do_bitexact && dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
2153 print_str("profile", profile);
2155 if (dec_ctx->profile != FF_PROFILE_UNKNOWN) {
2156 char profile_num[12];
2157 snprintf(profile_num, sizeof(profile_num), "%d", dec_ctx->profile);
2158 print_str("profile", profile_num);
2160 print_str_opt("profile", "unknown");
2163 s = av_get_media_type_string(dec_ctx->codec_type);
2164 if (s) print_str ("codec_type", s);
2165 else print_str_opt("codec_type", "unknown");
2166 print_q("codec_time_base", dec_ctx->time_base, '/');
2168 /* print AVI/FourCC tag */
2169 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
2170 print_str("codec_tag_string", val_str);
2171 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
2173 switch (dec_ctx->codec_type) {
2174 case AVMEDIA_TYPE_VIDEO:
2175 print_int("width", dec_ctx->width);
2176 print_int("height", dec_ctx->height);
2177 print_int("coded_width", dec_ctx->coded_width);
2178 print_int("coded_height", dec_ctx->coded_height);
2179 print_int("has_b_frames", dec_ctx->has_b_frames);
2180 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
2182 print_q("sample_aspect_ratio", sar, ':');
2183 av_reduce(&dar.num, &dar.den,
2184 dec_ctx->width * sar.num,
2185 dec_ctx->height * sar.den,
2187 print_q("display_aspect_ratio", dar, ':');
2189 print_str_opt("sample_aspect_ratio", "N/A");
2190 print_str_opt("display_aspect_ratio", "N/A");
2192 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
2193 if (s) print_str ("pix_fmt", s);
2194 else print_str_opt("pix_fmt", "unknown");
2195 print_int("level", dec_ctx->level);
2196 if (dec_ctx->color_range != AVCOL_RANGE_UNSPECIFIED)
2197 print_str ("color_range", av_color_range_name(dec_ctx->color_range));
2199 print_str_opt("color_range", "N/A");
2200 s = av_get_colorspace_name(dec_ctx->colorspace);
2201 if (s) print_str ("color_space", s);
2202 else print_str_opt("color_space", "unknown");
2204 if (dec_ctx->color_trc != AVCOL_TRC_UNSPECIFIED)
2205 print_str("color_transfer", av_color_transfer_name(dec_ctx->color_trc));
2207 print_str_opt("color_transfer", av_color_transfer_name(dec_ctx->color_trc));
2209 if (dec_ctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
2210 print_str("color_primaries", av_color_primaries_name(dec_ctx->color_primaries));
2212 print_str_opt("color_primaries", av_color_primaries_name(dec_ctx->color_primaries));
2214 if (dec_ctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
2215 print_str("chroma_location", av_chroma_location_name(dec_ctx->chroma_sample_location));
2217 print_str_opt("chroma_location", av_chroma_location_name(dec_ctx->chroma_sample_location));
2219 if (dec_ctx->timecode_frame_start >= 0) {
2220 char tcbuf[AV_TIMECODE_STR_SIZE];
2221 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
2222 print_str("timecode", tcbuf);
2224 print_str_opt("timecode", "N/A");
2226 print_int("refs", dec_ctx->refs);
2229 case AVMEDIA_TYPE_AUDIO:
2230 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
2231 if (s) print_str ("sample_fmt", s);
2232 else print_str_opt("sample_fmt", "unknown");
2233 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
2234 print_int("channels", dec_ctx->channels);
2236 if (dec_ctx->channel_layout) {
2237 av_bprint_clear(&pbuf);
2238 av_bprint_channel_layout(&pbuf, dec_ctx->channels, dec_ctx->channel_layout);
2239 print_str ("channel_layout", pbuf.str);
2241 print_str_opt("channel_layout", "unknown");
2244 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
2247 case AVMEDIA_TYPE_SUBTITLE:
2249 print_int("width", dec_ctx->width);
2251 print_str_opt("width", "N/A");
2252 if (dec_ctx->height)
2253 print_int("height", dec_ctx->height);
2255 print_str_opt("height", "N/A");
2259 print_str_opt("codec_type", "unknown");
2261 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
2262 const AVOption *opt = NULL;
2263 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
2265 if (opt->flags) continue;
2266 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
2267 print_str(opt->name, str);
2273 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
2274 else print_str_opt("id", "N/A");
2275 print_q("r_frame_rate", stream->r_frame_rate, '/');
2276 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
2277 print_q("time_base", stream->time_base, '/');
2278 print_ts ("start_pts", stream->start_time);
2279 print_time("start_time", stream->start_time, &stream->time_base);
2280 print_ts ("duration_ts", stream->duration);
2281 print_time("duration", stream->duration, &stream->time_base);
2282 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
2283 else print_str_opt("bit_rate", "N/A");
2284 if (dec_ctx->rc_max_rate > 0) print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
2285 else print_str_opt("max_bit_rate", "N/A");
2286 if (dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
2287 else print_str_opt("bits_per_raw_sample", "N/A");
2288 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
2289 else print_str_opt("nb_frames", "N/A");
2290 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
2291 else print_str_opt("nb_read_frames", "N/A");
2292 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
2293 else print_str_opt("nb_read_packets", "N/A");
2295 writer_print_data(w, "extradata", dec_ctx->extradata,
2296 dec_ctx->extradata_size);
2297 writer_print_data_hash(w, "extradata_hash", dec_ctx->extradata,
2298 dec_ctx->extradata_size);
2300 /* Print disposition information */
2301 #define PRINT_DISPOSITION(flagname, name) do { \
2302 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
2305 if (do_show_stream_disposition) {
2306 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
2307 PRINT_DISPOSITION(DEFAULT, "default");
2308 PRINT_DISPOSITION(DUB, "dub");
2309 PRINT_DISPOSITION(ORIGINAL, "original");
2310 PRINT_DISPOSITION(COMMENT, "comment");
2311 PRINT_DISPOSITION(LYRICS, "lyrics");
2312 PRINT_DISPOSITION(KARAOKE, "karaoke");
2313 PRINT_DISPOSITION(FORCED, "forced");
2314 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
2315 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
2316 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
2317 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
2318 writer_print_section_footer(w);
2321 if (do_show_stream_tags)
2322 ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
2324 if (stream->nb_side_data) {
2326 writer_print_section_header(w, SECTION_ID_STREAM_SIDE_DATA_LIST);
2327 for (i = 0; i < stream->nb_side_data; i++) {
2328 AVPacketSideData *sd = &stream->side_data[i];
2329 const char *name = av_packet_side_data_name(sd->type);
2331 writer_print_section_header(w, SECTION_ID_STREAM_SIDE_DATA);
2332 print_str("side_data_type", name ? name : "unknown");
2333 print_int("side_data_size", sd->size);
2334 if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2335 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2336 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
2338 writer_print_section_footer(w);
2340 writer_print_section_footer(w);
2343 writer_print_section_footer(w);
2344 av_bprint_finalize(&pbuf, NULL);
2350 static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
2354 writer_print_section_header(w, SECTION_ID_STREAMS);
2355 for (i = 0; i < fmt_ctx->nb_streams; i++)
2356 if (selected_streams[i]) {
2357 ret = show_stream(w, fmt_ctx, i, 0);
2361 writer_print_section_footer(w);
2366 static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
2370 writer_print_section_header(w, SECTION_ID_PROGRAM);
2371 print_int("program_id", program->id);
2372 print_int("program_num", program->program_num);
2373 print_int("nb_streams", program->nb_stream_indexes);
2374 print_int("pmt_pid", program->pmt_pid);
2375 print_int("pcr_pid", program->pcr_pid);
2376 print_ts("start_pts", program->start_time);
2377 print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
2378 print_ts("end_pts", program->end_time);
2379 print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
2380 if (do_show_program_tags)
2381 ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
2385 writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
2386 for (i = 0; i < program->nb_stream_indexes; i++) {
2387 if (selected_streams[program->stream_index[i]]) {
2388 ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
2393 writer_print_section_footer(w);
2396 writer_print_section_footer(w);
2400 static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
2404 writer_print_section_header(w, SECTION_ID_PROGRAMS);
2405 for (i = 0; i < fmt_ctx->nb_programs; i++) {
2406 AVProgram *program = fmt_ctx->programs[i];
2409 ret = show_program(w, fmt_ctx, program);
2413 writer_print_section_footer(w);
2417 static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
2421 writer_print_section_header(w, SECTION_ID_CHAPTERS);
2422 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
2423 AVChapter *chapter = fmt_ctx->chapters[i];
2425 writer_print_section_header(w, SECTION_ID_CHAPTER);
2426 print_int("id", chapter->id);
2427 print_q ("time_base", chapter->time_base, '/');
2428 print_int("start", chapter->start);
2429 print_time("start_time", chapter->start, &chapter->time_base);
2430 print_int("end", chapter->end);
2431 print_time("end_time", chapter->end, &chapter->time_base);
2432 if (do_show_chapter_tags)
2433 ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
2434 writer_print_section_footer(w);
2436 writer_print_section_footer(w);
2441 static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
2444 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
2447 writer_print_section_header(w, SECTION_ID_FORMAT);
2448 print_str_validate("filename", fmt_ctx->filename);
2449 print_int("nb_streams", fmt_ctx->nb_streams);
2450 print_int("nb_programs", fmt_ctx->nb_programs);
2451 print_str("format_name", fmt_ctx->iformat->name);
2453 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2454 else print_str_opt("format_long_name", "unknown");
2456 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2457 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2458 if (size >= 0) print_val ("size", size, unit_byte_str);
2459 else print_str_opt("size", "N/A");
2460 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2461 else print_str_opt("bit_rate", "N/A");
2462 print_int("probe_score", av_format_get_probe_score(fmt_ctx));
2463 if (do_show_format_tags)
2464 ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2466 writer_print_section_footer(w);
2471 static void show_error(WriterContext *w, int err)
2474 const char *errbuf_ptr = errbuf;
2476 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2477 errbuf_ptr = strerror(AVUNERROR(err));
2479 writer_print_section_header(w, SECTION_ID_ERROR);
2480 print_int("code", err);
2481 print_str("string", errbuf_ptr);
2482 writer_print_section_footer(w);
2485 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
2487 int err, i, orig_nb_streams;
2488 AVFormatContext *fmt_ctx = NULL;
2489 AVDictionaryEntry *t;
2490 AVDictionary **opts;
2491 int scan_all_pmts_set = 0;
2493 if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2494 av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2495 scan_all_pmts_set = 1;
2497 if ((err = avformat_open_input(&fmt_ctx, filename,
2498 iformat, &format_opts)) < 0) {
2499 print_error(filename, err);
2502 *fmt_ctx_ptr = fmt_ctx;
2503 if (scan_all_pmts_set)
2504 av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2505 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2506 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2507 return AVERROR_OPTION_NOT_FOUND;
2510 /* fill the streams in the format context */
2511 opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2512 orig_nb_streams = fmt_ctx->nb_streams;
2514 err = avformat_find_stream_info(fmt_ctx, opts);
2516 for (i = 0; i < orig_nb_streams; i++)
2517 av_dict_free(&opts[i]);
2521 print_error(filename, err);
2525 av_dump_format(fmt_ctx, 0, filename, 0);
2527 /* bind a decoder to each input stream */
2528 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2529 AVStream *stream = fmt_ctx->streams[i];
2532 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
2533 av_log(NULL, AV_LOG_WARNING,
2534 "Failed to probe codec for input stream %d\n",
2536 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
2537 av_log(NULL, AV_LOG_WARNING,
2538 "Unsupported codec with id %d for input stream %d\n",
2539 stream->codec->codec_id, stream->index);
2541 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
2542 fmt_ctx, stream, codec);
2543 if (avcodec_open2(stream->codec, codec, &opts) < 0) {
2544 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
2547 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2548 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
2549 t->key, stream->index);
2550 return AVERROR_OPTION_NOT_FOUND;
2555 *fmt_ctx_ptr = fmt_ctx;
2559 static void close_input_file(AVFormatContext **ctx_ptr)
2562 AVFormatContext *fmt_ctx = *ctx_ptr;
2564 /* close decoder for each stream */
2565 for (i = 0; i < fmt_ctx->nb_streams; i++)
2566 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
2567 avcodec_close(fmt_ctx->streams[i]->codec);
2569 avformat_close_input(ctx_ptr);
2572 static int probe_file(WriterContext *wctx, const char *filename)
2574 AVFormatContext *fmt_ctx = NULL;
2578 do_read_frames = do_show_frames || do_count_frames;
2579 do_read_packets = do_show_packets || do_count_packets;
2581 ret = open_input_file(&fmt_ctx, filename);
2585 #define CHECK_END if (ret < 0) goto end
2587 nb_streams = fmt_ctx->nb_streams;
2588 REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,fmt_ctx->nb_streams);
2589 REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,fmt_ctx->nb_streams);
2590 REALLOCZ_ARRAY_STREAM(selected_streams,0,fmt_ctx->nb_streams);
2592 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2593 if (stream_specifier) {
2594 ret = avformat_match_stream_specifier(fmt_ctx,
2595 fmt_ctx->streams[i],
2599 selected_streams[i] = ret;
2602 selected_streams[i] = 1;
2606 if (do_read_frames || do_read_packets) {
2607 if (do_show_frames && do_show_packets &&
2608 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
2609 section_id = SECTION_ID_PACKETS_AND_FRAMES;
2610 else if (do_show_packets && !do_show_frames)
2611 section_id = SECTION_ID_PACKETS;
2612 else // (!do_show_packets && do_show_frames)
2613 section_id = SECTION_ID_FRAMES;
2614 if (do_show_frames || do_show_packets)
2615 writer_print_section_header(wctx, section_id);
2616 ret = read_packets(wctx, fmt_ctx);
2617 if (do_show_frames || do_show_packets)
2618 writer_print_section_footer(wctx);
2622 if (do_show_programs) {
2623 ret = show_programs(wctx, fmt_ctx);
2627 if (do_show_streams) {
2628 ret = show_streams(wctx, fmt_ctx);
2631 if (do_show_chapters) {
2632 ret = show_chapters(wctx, fmt_ctx);
2635 if (do_show_format) {
2636 ret = show_format(wctx, fmt_ctx);
2642 close_input_file(&fmt_ctx);
2643 av_freep(&nb_streams_frames);
2644 av_freep(&nb_streams_packets);
2645 av_freep(&selected_streams);
2650 static void show_usage(void)
2652 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
2653 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
2654 av_log(NULL, AV_LOG_INFO, "\n");
2657 static void ffprobe_show_program_version(WriterContext *w)
2660 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2662 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
2663 print_str("version", FFMPEG_VERSION);
2664 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2665 program_birth_year, CONFIG_THIS_YEAR);
2666 print_str("compiler_ident", CC_IDENT);
2667 print_str("configuration", FFMPEG_CONFIGURATION);
2668 writer_print_section_footer(w);
2670 av_bprint_finalize(&pbuf, NULL);
2673 #define SHOW_LIB_VERSION(libname, LIBNAME) \
2675 if (CONFIG_##LIBNAME) { \
2676 unsigned int version = libname##_version(); \
2677 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
2678 print_str("name", "lib" #libname); \
2679 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
2680 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
2681 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
2682 print_int("version", version); \
2683 print_str("ident", LIB##LIBNAME##_IDENT); \
2684 writer_print_section_footer(w); \
2688 static void ffprobe_show_library_versions(WriterContext *w)
2690 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
2691 SHOW_LIB_VERSION(avutil, AVUTIL);
2692 SHOW_LIB_VERSION(avcodec, AVCODEC);
2693 SHOW_LIB_VERSION(avformat, AVFORMAT);
2694 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2695 SHOW_LIB_VERSION(avfilter, AVFILTER);
2696 SHOW_LIB_VERSION(swscale, SWSCALE);
2697 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2698 SHOW_LIB_VERSION(postproc, POSTPROC);
2699 writer_print_section_footer(w);
2702 #define PRINT_PIX_FMT_FLAG(flagname, name) \
2704 print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
2707 static void ffprobe_show_pixel_formats(WriterContext *w)
2709 const AVPixFmtDescriptor *pixdesc = NULL;
2712 writer_print_section_header(w, SECTION_ID_PIXEL_FORMATS);
2713 while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
2714 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT);
2715 print_str("name", pixdesc->name);
2716 print_int("nb_components", pixdesc->nb_components);
2717 if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
2718 print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
2719 print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
2721 print_str_opt("log2_chroma_w", "N/A");
2722 print_str_opt("log2_chroma_h", "N/A");
2724 n = av_get_bits_per_pixel(pixdesc);
2725 if (n) print_int ("bits_per_pixel", n);
2726 else print_str_opt("bits_per_pixel", "N/A");
2727 if (do_show_pixel_format_flags) {
2728 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_FLAGS);
2729 PRINT_PIX_FMT_FLAG(BE, "big_endian");
2730 PRINT_PIX_FMT_FLAG(PAL, "palette");
2731 PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
2732 PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
2733 PRINT_PIX_FMT_FLAG(PLANAR, "planar");
2734 PRINT_PIX_FMT_FLAG(RGB, "rgb");
2735 PRINT_PIX_FMT_FLAG(PSEUDOPAL, "pseudopal");
2736 PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
2737 writer_print_section_footer(w);
2739 if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
2740 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
2741 for (i = 0; i < pixdesc->nb_components; i++) {
2742 writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENT);
2743 print_int("index", i + 1);
2744 print_int("bit_depth", pixdesc->comp[i].depth);
2745 writer_print_section_footer(w);
2747 writer_print_section_footer(w);
2749 writer_print_section_footer(w);
2751 writer_print_section_footer(w);
2754 static int opt_format(void *optctx, const char *opt, const char *arg)
2756 iformat = av_find_input_format(arg);
2758 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2759 return AVERROR(EINVAL);
2764 static inline void mark_section_show_entries(SectionID section_id,
2765 int show_all_entries, AVDictionary *entries)
2767 struct section *section = §ions[section_id];
2769 section->show_all_entries = show_all_entries;
2770 if (show_all_entries) {
2772 for (id = section->children_ids; *id != -1; id++)
2773 mark_section_show_entries(*id, show_all_entries, entries);
2775 av_dict_copy(§ion->entries_to_show, entries, 0);
2779 static int match_section(const char *section_name,
2780 int show_all_entries, AVDictionary *entries)
2784 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
2785 const struct section *section = §ions[i];
2786 if (!strcmp(section_name, section->name) ||
2787 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2788 av_log(NULL, AV_LOG_DEBUG,
2789 "'%s' matches section with unique name '%s'\n", section_name,
2790 (char *)av_x_if_null(section->unique_name, section->name));
2792 mark_section_show_entries(section->id, show_all_entries, entries);
2798 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2800 const char *p = arg;
2804 AVDictionary *entries = NULL;
2805 char *section_name = av_get_token(&p, "=:");
2806 int show_all_entries = 0;
2808 if (!section_name) {
2809 av_log(NULL, AV_LOG_ERROR,
2810 "Missing section name for option '%s'\n", opt);
2811 return AVERROR(EINVAL);
2816 while (*p && *p != ':') {
2817 char *entry = av_get_token(&p, ",:");
2820 av_log(NULL, AV_LOG_VERBOSE,
2821 "Adding '%s' to the entries to show in section '%s'\n",
2822 entry, section_name);
2823 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2828 show_all_entries = 1;
2831 ret = match_section(section_name, show_all_entries, entries);
2833 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2834 ret = AVERROR(EINVAL);
2836 av_dict_free(&entries);
2837 av_free(section_name);
2848 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2850 char *buf = av_asprintf("format=%s", arg);
2854 return AVERROR(ENOMEM);
2856 av_log(NULL, AV_LOG_WARNING,
2857 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2859 ret = opt_show_entries(optctx, opt, buf);
2864 static void opt_input_file(void *optctx, const char *arg)
2866 if (input_filename) {
2867 av_log(NULL, AV_LOG_ERROR,
2868 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2869 arg, input_filename);
2872 if (!strcmp(arg, "-"))
2874 input_filename = arg;
2877 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2879 opt_input_file(optctx, arg);
2883 void show_help_default(const char *opt, const char *arg)
2885 av_log_set_callback(log_callback_help);
2887 show_help_options(options, "Main options:", 0, 0, 0);
2890 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2894 * Parse interval specification, according to the format:
2895 * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
2896 * INTERVALS ::= INTERVAL[,INTERVALS]
2898 static int parse_read_interval(const char *interval_spec,
2899 ReadInterval *interval)
2902 char *next, *p, *spec = av_strdup(interval_spec);
2904 return AVERROR(ENOMEM);
2907 av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
2908 ret = AVERROR(EINVAL);
2913 next = strchr(spec, '%');
2917 /* parse first part */
2919 interval->has_start = 1;
2922 interval->start_is_offset = 1;
2925 interval->start_is_offset = 0;
2928 ret = av_parse_time(&interval->start, p, 1);
2930 av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
2934 interval->has_start = 0;
2937 /* parse second part */
2941 interval->has_end = 1;
2944 interval->end_is_offset = 1;
2947 interval->end_is_offset = 0;
2950 if (interval->end_is_offset && *p == '#') {
2953 interval->duration_frames = 1;
2955 lli = strtoll(p, &tail, 10);
2956 if (*tail || lli < 0) {
2957 av_log(NULL, AV_LOG_ERROR,
2958 "Invalid or negative value '%s' for duration number of frames\n", p);
2961 interval->end = lli;
2963 ret = av_parse_time(&us, p, 1);
2965 av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
2971 interval->has_end = 0;
2979 static int parse_read_intervals(const char *intervals_spec)
2982 char *p, *spec = av_strdup(intervals_spec);
2984 return AVERROR(ENOMEM);
2986 /* preparse specification, get number of intervals */
2987 for (n = 0, p = spec; *p; p++)
2992 read_intervals = av_malloc_array(n, sizeof(*read_intervals));
2993 if (!read_intervals) {
2994 ret = AVERROR(ENOMEM);
2997 read_intervals_nb = n;
2999 /* parse intervals */
3001 for (i = 0; p; i++) {
3004 av_assert0(i < read_intervals_nb);
3005 next = strchr(p, ',');
3009 read_intervals[i].id = i;
3010 ret = parse_read_interval(p, &read_intervals[i]);
3012 av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
3016 av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
3017 log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
3020 av_assert0(i == read_intervals_nb);
3027 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
3029 return parse_read_intervals(arg);
3032 static int opt_pretty(void *optctx, const char *opt, const char *arg)
3034 show_value_unit = 1;
3035 use_value_prefix = 1;
3036 use_byte_value_binary_prefix = 1;
3037 use_value_sexagesimal_format = 1;
3041 static void print_section(SectionID id, int level)
3043 const SectionID *pid;
3044 const struct section *section = §ions[id];
3046 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
3047 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
3048 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
3049 printf("%*c %s", level * 4, ' ', section->name);
3050 if (section->unique_name)
3051 printf("/%s", section->unique_name);
3054 for (pid = section->children_ids; *pid != -1; pid++)
3055 print_section(*pid, level+1);
3058 static int opt_sections(void *optctx, const char *opt, const char *arg)
3060 printf("Sections:\n"
3061 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
3062 ".A. = Section contains an array of elements of the same type\n"
3063 "..V = Section may contain a variable number of fields with variable keys\n"
3064 "FLAGS NAME/UNIQUE_NAME\n"
3066 print_section(SECTION_ID_ROOT, 0);
3070 static int opt_show_versions(const char *opt, const char *arg)
3072 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
3073 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
3077 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
3078 static int opt_show_##section(const char *opt, const char *arg) \
3080 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
3084 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
3085 DEFINE_OPT_SHOW_SECTION(error, ERROR)
3086 DEFINE_OPT_SHOW_SECTION(format, FORMAT)
3087 DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
3088 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
3089 DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
3090 DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
3091 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
3092 DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
3093 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
3095 static const OptionDef real_options[] = {
3096 #include "cmdutils_common_opts.h"
3097 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
3098 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
3099 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
3100 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
3101 "use binary prefixes for byte units" },
3102 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
3103 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
3104 { "pretty", 0, {.func_arg = opt_pretty},
3105 "prettify the format of displayed values, make it more human readable" },
3106 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
3107 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
3108 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
3109 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
3110 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
3111 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
3112 { "show_data_hash", OPT_STRING | HAS_ARG, {(void*)&show_data_hash}, "show packets data hash" },
3113 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
3114 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
3115 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
3116 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
3117 "show a particular entry from the format/container info", "entry" },
3118 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
3119 "show a set of specified entries", "entry_list" },
3120 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
3121 { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
3122 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
3123 { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
3124 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
3125 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
3126 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
3127 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
3128 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
3129 { "show_pixel_formats", 0, {(void*)&opt_show_pixel_formats}, "show pixel format descriptions" },
3130 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
3131 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
3132 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
3133 { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
3134 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
3135 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
3139 static inline int check_section_show_entries(int section_id)
3142 struct section *section = §ions[section_id];
3143 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
3145 for (id = section->children_ids; *id != -1; id++)
3146 if (check_section_show_entries(*id))
3151 #define SET_DO_SHOW(id, varname) do { \
3152 if (check_section_show_entries(SECTION_ID_##id)) \
3153 do_show_##varname = 1; \
3156 int main(int argc, char **argv)
3159 WriterContext *wctx;
3161 char *w_name = NULL, *w_args = NULL;
3164 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3165 register_exit(ffprobe_cleanup);
3167 options = real_options;
3168 parse_loglevel(argc, argv, options);
3170 avformat_network_init();
3173 avdevice_register_all();
3176 show_banner(argc, argv, options);
3177 parse_options(NULL, argc, argv, options, opt_input_file);
3179 /* mark things to show, based on -show_entries */
3180 SET_DO_SHOW(CHAPTERS, chapters);
3181 SET_DO_SHOW(ERROR, error);
3182 SET_DO_SHOW(FORMAT, format);
3183 SET_DO_SHOW(FRAMES, frames);
3184 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
3185 SET_DO_SHOW(PACKETS, packets);
3186 SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
3187 SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
3188 SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
3189 SET_DO_SHOW(PROGRAM_VERSION, program_version);
3190 SET_DO_SHOW(PROGRAMS, programs);
3191 SET_DO_SHOW(STREAMS, streams);
3192 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
3193 SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
3195 SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
3196 SET_DO_SHOW(FORMAT_TAGS, format_tags);
3197 SET_DO_SHOW(FRAME_TAGS, frame_tags);
3198 SET_DO_SHOW(PROGRAM_TAGS, program_tags);
3199 SET_DO_SHOW(STREAM_TAGS, stream_tags);
3200 SET_DO_SHOW(PACKET_TAGS, packet_tags);
3202 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
3203 av_log(NULL, AV_LOG_ERROR,
3204 "-bitexact and -show_program_version or -show_library_versions "
3205 "options are incompatible\n");
3206 ret = AVERROR(EINVAL);
3210 writer_register_all();
3213 print_format = av_strdup("default");
3214 if (!print_format) {
3215 ret = AVERROR(ENOMEM);
3218 w_name = av_strtok(print_format, "=", &buf);
3221 if (show_data_hash) {
3222 if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
3223 if (ret == AVERROR(EINVAL)) {
3225 av_log(NULL, AV_LOG_ERROR,
3226 "Unknown hash algorithm '%s'\nKnown algorithms:",
3228 for (i = 0; (n = av_hash_names(i)); i++)
3229 av_log(NULL, AV_LOG_ERROR, " %s", n);
3230 av_log(NULL, AV_LOG_ERROR, "\n");
3236 w = writer_get_by_name(w_name);
3238 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
3239 ret = AVERROR(EINVAL);
3243 if ((ret = writer_open(&wctx, w, w_args,
3244 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
3245 if (w == &xml_writer)
3246 wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
3248 writer_print_section_header(wctx, SECTION_ID_ROOT);
3250 if (do_show_program_version)
3251 ffprobe_show_program_version(wctx);
3252 if (do_show_library_versions)
3253 ffprobe_show_library_versions(wctx);
3254 if (do_show_pixel_formats)
3255 ffprobe_show_pixel_formats(wctx);
3257 if (!input_filename &&
3258 ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
3259 (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
3261 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
3262 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
3263 ret = AVERROR(EINVAL);
3264 } else if (input_filename) {
3265 ret = probe_file(wctx, input_filename);
3266 if (ret < 0 && do_show_error)
3267 show_error(wctx, ret);
3270 writer_print_section_footer(wctx);
3271 writer_close(&wctx);
3275 av_freep(&print_format);
3276 av_freep(&read_intervals);
3277 av_hash_freep(&hash);
3280 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
3281 av_dict_free(&(sections[i].entries_to_show));
3283 avformat_network_deinit();