2 * avprobe : Simple Media Prober based on the Libav libraries
3 * Copyright (c) 2007-2010 Stefano Sabatini
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavformat/avformat.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/dict.h"
29 #include "libavdevice/avdevice.h"
32 const char program_name[] = "avprobe";
33 const int program_birth_year = 2007;
35 static int do_show_format = 0;
36 static AVDictionary *fmt_entries_to_show = NULL;
37 static int nb_fmt_entries_to_show;
38 static int do_show_packets = 0;
39 static int do_show_streams = 0;
41 static int show_value_unit = 0;
42 static int use_value_prefix = 0;
43 static int use_byte_value_binary_prefix = 0;
44 static int use_value_sexagesimal_format = 0;
47 static const OptionDef options[];
50 static const char *input_filename;
51 static AVInputFormat *iformat = NULL;
53 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
54 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
56 static const char unit_second_str[] = "s" ;
57 static const char unit_hertz_str[] = "Hz" ;
58 static const char unit_byte_str[] = "byte" ;
59 static const char unit_bit_per_second_str[] = "bit/s";
61 void exit_program(int ret)
63 av_dict_free(&fmt_entries_to_show);
68 * The output is structured in array and objects that might contain items
69 * Array could require the objects within to not be named.
70 * Object could require the items within to be named.
72 * For flat representation the name of each section is saved on prefix so it
73 * can be rendered in order to represent nested structures (e.g. array of
74 * objects for the packets list).
76 * Within an array each element can need an unique identifier or an index.
78 * Nesting level is accounted separately.
88 ProbeElementType type;
98 static AVIOContext *probe_out = NULL;
99 static OutputContext octx;
102 * Default format, INI
104 * - all key and values are utf8
105 * - '.' is the subgroup separator
106 * - newlines and the following characters are escaped
107 * - '\' is the escape character
108 * - '#' is the comment
109 * - '=' is the key/value separators
110 * - ':' is not used but usually parsed as key/value separator
113 static void ini_print_header(void)
115 avio_printf(probe_out, "# avprobe output\n\n");
117 static void ini_print_footer(void)
119 avio_w8(probe_out, '\n');
122 static void ini_escape_print(const char *s)
129 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
130 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
131 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
132 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
133 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
137 case ':' : avio_w8(probe_out, '\\');
139 if ((unsigned char)c < 32)
140 avio_printf(probe_out, "\\x00%02x", c & 0xff);
142 avio_w8(probe_out, c);
148 static void ini_print_array_header(const char *name)
150 if (octx.prefix[octx.level -1].nb_elems)
151 avio_printf(probe_out, "\n");
154 static void ini_print_object_header(const char *name)
157 ProbeElement *el = octx.prefix + octx.level -1;
160 avio_printf(probe_out, "\n");
162 avio_printf(probe_out, "[");
164 for (i = 1; i < octx.level; i++) {
165 el = octx.prefix + i;
166 avio_printf(probe_out, "%s.", el->name);
168 avio_printf(probe_out, "%"PRId64".", el->index);
171 avio_printf(probe_out, "%s", name);
172 if (el && el->type == ARRAY)
173 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
174 avio_printf(probe_out, "]\n");
177 static void ini_print_integer(const char *key, int64_t value)
179 ini_escape_print(key);
180 avio_printf(probe_out, "=%"PRId64"\n", value);
184 static void ini_print_string(const char *key, const char *value)
186 ini_escape_print(key);
187 avio_printf(probe_out, "=");
188 ini_escape_print(value);
189 avio_w8(probe_out, '\n');
193 * Simple Formatter for single entries.
196 static void show_format_entry_integer(const char *key, int64_t value)
198 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
199 if (nb_fmt_entries_to_show > 1)
200 avio_printf(probe_out, "%s=", key);
201 avio_printf(probe_out, "%"PRId64"\n", value);
205 static void show_format_entry_string(const char *key, const char *value)
207 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
208 if (nb_fmt_entries_to_show > 1)
209 avio_printf(probe_out, "%s=", key);
210 avio_printf(probe_out, "%s\n", value);
215 void (*print_header)(void) = ini_print_header;
216 void (*print_footer)(void) = ini_print_footer;
218 void (*print_array_header) (const char *name) = ini_print_array_header;
219 void (*print_array_footer) (const char *name);
220 void (*print_object_header)(const char *name) = ini_print_object_header;
221 void (*print_object_footer)(const char *name);
223 void (*print_integer) (const char *key, int64_t value) = ini_print_integer;
224 void (*print_string) (const char *key, const char *value) = ini_print_string;
227 static void probe_group_enter(const char *name, int type)
232 av_realloc(octx.prefix, sizeof(ProbeElement) * (octx.level + 1));
234 if (!octx.prefix || !name) {
235 fprintf(stderr, "Out of memory\n");
240 ProbeElement *parent = octx.prefix + octx.level -1;
241 if (parent->type == ARRAY)
242 count = parent->nb_elems;
246 octx.prefix[octx.level++] = (ProbeElement){name, type, count, 0};
249 static void probe_group_leave(void)
254 static void probe_header(void)
258 probe_group_enter("root", OBJECT);
261 static void probe_footer(void)
269 static void probe_array_header(const char *name)
271 if (print_array_header)
272 print_array_header(name);
274 probe_group_enter(name, ARRAY);
277 static void probe_array_footer(const char *name)
280 if (print_array_footer)
281 print_array_footer(name);
284 static void probe_object_header(const char *name)
286 if (print_object_header)
287 print_object_header(name);
289 probe_group_enter(name, OBJECT);
292 static void probe_object_footer(const char *name)
295 if (print_object_footer)
296 print_object_footer(name);
299 static void probe_int(const char *key, int64_t value)
301 print_integer(key, value);
302 octx.prefix[octx.level -1].nb_elems++;
305 static void probe_str(const char *key, const char *value)
307 print_string(key, value);
308 octx.prefix[octx.level -1].nb_elems++;
311 static void probe_dict(AVDictionary *dict, const char *name)
313 AVDictionaryEntry *entry = NULL;
316 probe_object_header(name);
317 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
318 probe_str(entry->key, entry->value);
320 probe_object_footer(name);
323 static char *value_string(char *buf, int buf_size, double val, const char *unit)
325 if (unit == unit_second_str && use_value_sexagesimal_format) {
329 mins = (int)secs / 60;
330 secs = secs - mins * 60;
333 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
334 } else if (use_value_prefix) {
335 const char *prefix_string;
338 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
339 index = (int) (log(val)/log(2)) / 10;
340 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
341 val /= pow(2, index * 10);
342 prefix_string = binary_unit_prefixes[index];
344 index = (int) (log10(val)) / 3;
345 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
346 val /= pow(10, index * 3);
347 prefix_string = decimal_unit_prefixes[index];
349 snprintf(buf, buf_size, "%.*f%s%s",
352 show_value_unit ? unit : "");
354 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
360 static char *time_value_string(char *buf, int buf_size, int64_t val,
361 const AVRational *time_base)
363 if (val == AV_NOPTS_VALUE) {
364 snprintf(buf, buf_size, "N/A");
366 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
372 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
374 if (ts == AV_NOPTS_VALUE) {
375 snprintf(buf, buf_size, "N/A");
377 snprintf(buf, buf_size, "%"PRId64, ts);
383 static char *rational_string(char *buf, int buf_size, const char *sep,
384 const AVRational *rat)
386 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
390 static char *tag_string(char *buf, int buf_size, int tag)
392 snprintf(buf, buf_size, "0x%04x", tag);
398 static const char *media_type_string(enum AVMediaType media_type)
400 switch (media_type) {
401 case AVMEDIA_TYPE_VIDEO: return "video";
402 case AVMEDIA_TYPE_AUDIO: return "audio";
403 case AVMEDIA_TYPE_DATA: return "data";
404 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
405 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
406 default: return "unknown";
410 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
413 AVStream *st = fmt_ctx->streams[pkt->stream_index];
415 probe_object_header("packet");
416 probe_str("codec_type", media_type_string(st->codec->codec_type));
417 probe_int("stream_index", pkt->stream_index);
418 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
419 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
420 pkt->pts, &st->time_base));
421 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
422 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
423 pkt->dts, &st->time_base));
424 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
426 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
429 probe_str("size", value_string(val_str, sizeof(val_str),
430 pkt->size, unit_byte_str));
431 probe_int("pos", pkt->pos);
432 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
433 probe_object_footer("packet");
436 static void show_packets(AVFormatContext *fmt_ctx)
440 av_init_packet(&pkt);
441 probe_array_header("packets");
442 while (!av_read_frame(fmt_ctx, &pkt))
443 show_packet(fmt_ctx, &pkt);
444 probe_array_footer("packets");
447 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
449 AVStream *stream = fmt_ctx->streams[stream_idx];
450 AVCodecContext *dec_ctx;
453 AVRational display_aspect_ratio;
455 probe_object_header("stream");
457 probe_int("index", stream->index);
459 if ((dec_ctx = stream->codec)) {
460 if ((dec = dec_ctx->codec)) {
461 probe_str("codec_name", dec->name);
462 probe_str("codec_long_name", dec->long_name);
464 probe_str("codec_name", "unknown");
467 probe_str("codec_type", media_type_string(dec_ctx->codec_type));
468 probe_str("codec_time_base",
469 rational_string(val_str, sizeof(val_str),
470 "/", &dec_ctx->time_base));
472 /* print AVI/FourCC tag */
473 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
474 probe_str("codec_tag_string", val_str);
475 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
476 dec_ctx->codec_tag));
478 switch (dec_ctx->codec_type) {
479 case AVMEDIA_TYPE_VIDEO:
480 probe_int("width", dec_ctx->width);
481 probe_int("height", dec_ctx->height);
482 probe_int("has_b_frames", dec_ctx->has_b_frames);
483 if (dec_ctx->sample_aspect_ratio.num) {
484 probe_str("sample_aspect_ratio",
485 rational_string(val_str, sizeof(val_str), ":",
486 &dec_ctx->sample_aspect_ratio));
487 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
488 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
489 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
491 probe_str("display_aspect_ratio",
492 rational_string(val_str, sizeof(val_str), ":",
493 &display_aspect_ratio));
496 dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name
498 probe_int("level", dec_ctx->level);
501 case AVMEDIA_TYPE_AUDIO:
502 probe_str("sample_rate",
503 value_string(val_str, sizeof(val_str),
504 dec_ctx->sample_rate,
506 probe_int("channels", dec_ctx->channels);
507 probe_int("bits_per_sample",
508 av_get_bits_per_sample(dec_ctx->codec_id));
512 probe_str("codec_type", "unknown");
515 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
516 probe_int("id", stream->id);
517 probe_str("r_frame_rate",
518 rational_string(val_str, sizeof(val_str), "/",
519 &stream->r_frame_rate));
520 probe_str("avg_frame_rate",
521 rational_string(val_str, sizeof(val_str), "/",
522 &stream->avg_frame_rate));
523 probe_str("time_base",
524 rational_string(val_str, sizeof(val_str), "/",
525 &stream->time_base));
526 probe_str("start_time",
527 time_value_string(val_str, sizeof(val_str),
528 stream->start_time, &stream->time_base));
529 probe_str("duration",
530 time_value_string(val_str, sizeof(val_str),
531 stream->duration, &stream->time_base));
532 if (stream->nb_frames)
533 probe_int("nb_frames", stream->nb_frames);
535 probe_dict(stream->metadata, "tags");
537 probe_object_footer("stream");
540 static void show_format(AVFormatContext *fmt_ctx)
543 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
545 probe_object_header("format");
546 probe_str("filename", fmt_ctx->filename);
547 probe_int("nb_streams", fmt_ctx->nb_streams);
548 probe_str("format_name", fmt_ctx->iformat->name);
549 probe_str("format_long_name", fmt_ctx->iformat->long_name);
550 probe_str("start_time",
551 time_value_string(val_str, sizeof(val_str),
552 fmt_ctx->start_time, &AV_TIME_BASE_Q));
553 probe_str("duration",
554 time_value_string(val_str, sizeof(val_str),
555 fmt_ctx->duration, &AV_TIME_BASE_Q));
557 size >= 0 ? value_string(val_str, sizeof(val_str),
560 probe_str("bit_rate",
561 value_string(val_str, sizeof(val_str),
562 fmt_ctx->bit_rate, unit_bit_per_second_str));
564 probe_dict(fmt_ctx->metadata, "tags");
566 probe_object_footer("format");
569 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
572 AVFormatContext *fmt_ctx = NULL;
573 AVDictionaryEntry *t;
575 if ((err = avformat_open_input(&fmt_ctx, filename,
576 iformat, &format_opts)) < 0) {
577 print_error(filename, err);
580 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
581 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
582 return AVERROR_OPTION_NOT_FOUND;
586 /* fill the streams in the format context */
587 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
588 print_error(filename, err);
592 av_dump_format(fmt_ctx, 0, filename, 0);
594 /* bind a decoder to each input stream */
595 for (i = 0; i < fmt_ctx->nb_streams; i++) {
596 AVStream *stream = fmt_ctx->streams[i];
599 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
601 "Unsupported codec with id %d for input stream %d\n",
602 stream->codec->codec_id, stream->index);
603 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
604 fprintf(stderr, "Error while opening codec for input stream %d\n",
609 *fmt_ctx_ptr = fmt_ctx;
613 static void close_input_file(AVFormatContext **ctx_ptr)
616 AVFormatContext *fmt_ctx = *ctx_ptr;
618 /* close decoder for each stream */
619 for (i = 0; i < fmt_ctx->nb_streams; i++) {
620 AVStream *stream = fmt_ctx->streams[i];
622 avcodec_close(stream->codec);
624 avformat_close_input(ctx_ptr);
627 static int probe_file(const char *filename)
629 AVFormatContext *fmt_ctx;
632 if ((ret = open_input_file(&fmt_ctx, filename)))
636 show_format(fmt_ctx);
638 if (do_show_streams) {
639 probe_array_header("streams");
640 for (i = 0; i < fmt_ctx->nb_streams; i++)
641 show_stream(fmt_ctx, i);
642 probe_array_footer("streams");
646 show_packets(fmt_ctx);
648 close_input_file(&fmt_ctx);
652 static void show_usage(void)
654 printf("Simple multimedia streams analyzer\n");
655 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
659 static int opt_format(const char *opt, const char *arg)
661 iformat = av_find_input_format(arg);
663 fprintf(stderr, "Unknown input format: %s\n", arg);
664 return AVERROR(EINVAL);
669 static int opt_show_format_entry(const char *opt, const char *arg)
672 nb_fmt_entries_to_show++;
675 print_array_header = NULL;
676 print_array_footer = NULL;
677 print_object_header = NULL;
678 print_object_footer = NULL;
680 print_integer = show_format_entry_integer;
681 print_string = show_format_entry_string;
682 av_dict_set(&fmt_entries_to_show, arg, "", 0);
686 static void opt_input_file(void *optctx, const char *arg)
688 if (input_filename) {
690 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
691 arg, input_filename);
694 if (!strcmp(arg, "-"))
696 input_filename = arg;
699 static void show_help(void)
701 av_log_set_callback(log_callback_help);
703 show_help_options(options, "Main options:\n", 0, 0);
705 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
708 static void opt_pretty(void)
711 use_value_prefix = 1;
712 use_byte_value_binary_prefix = 1;
713 use_value_sexagesimal_format = 1;
716 static const OptionDef options[] = {
717 #include "cmdutils_common_opts.h"
718 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
719 { "unit", OPT_BOOL, {(void*)&show_value_unit},
720 "show unit of the displayed values" },
721 { "prefix", OPT_BOOL, {(void*)&use_value_prefix},
722 "use SI prefixes for the displayed values" },
723 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
724 "use binary prefixes for byte units" },
725 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
726 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
727 { "pretty", 0, {(void*)&opt_pretty},
728 "prettify the format of displayed values, make it more human readable" },
729 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
730 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
731 "show a particular entry from the format/container info", "entry" },
732 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
733 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
734 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default},
735 "generic catch all option", "" },
739 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
741 printf("%.*s", buf_size, buf);
745 #define AVP_BUFFSIZE 4096
747 int main(int argc, char **argv)
750 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
755 parse_loglevel(argc, argv, options);
757 avformat_network_init();
760 avdevice_register_all();
764 parse_options(NULL, argc, argv, options, opt_input_file);
766 if (!input_filename) {
768 fprintf(stderr, "You have to specify one input file.\n");
770 "Use -h to get full help or, even better, run 'man %s'.\n",
775 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
776 probe_buf_write, NULL);
781 ret = probe_file(input_filename);
783 avio_flush(probe_out);
784 avio_close(probe_out);
786 avformat_network_deinit();