a9ca1934ca5741f5b6a33731e92e43fb0b8a79fc
[libav.git] / avtools / avprobe.c
1 /*
2 * avprobe : Simple Media Prober based on the Libav libraries
3 * Copyright (c) 2007-2010 Stefano Sabatini
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23
24 #include "libavformat/avformat.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/display.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/spherical.h"
31 #include "libavutil/stereo3d.h"
32 #include "libavutil/dict.h"
33 #include "libavutil/libm.h"
34 #include "libavdevice/avdevice.h"
35 #include "cmdutils.h"
36
37 typedef struct InputStream {
38 AVStream *st;
39
40 AVCodecContext *dec_ctx;
41 } InputStream;
42
43 typedef struct InputFile {
44 AVFormatContext *fmt_ctx;
45
46 InputStream *streams;
47 int nb_streams;
48 } InputFile;
49
50 const char program_name[] = "avprobe";
51 const int program_birth_year = 2007;
52
53 static int do_show_format = 0;
54 static AVDictionary *fmt_entries_to_show = NULL;
55 static int nb_fmt_entries_to_show;
56 static int do_show_packets = 0;
57 static int do_show_streams = 0;
58 static AVDictionary *stream_entries_to_show = NULL;
59 static int nb_stream_entries_to_show;
60
61 /* key used to print when probe_{int,str}(NULL, ..) is invoked */
62 static const char *header_key;
63
64 static int show_value_unit = 0;
65 static int use_value_prefix = 0;
66 static int use_byte_value_binary_prefix = 0;
67 static int use_value_sexagesimal_format = 0;
68
69 /* globals */
70 static const OptionDef *options;
71
72 /* avprobe context */
73 static const char *input_filename;
74 static AVInputFormat *iformat = NULL;
75
76 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
77 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
78
79 static const char unit_second_str[] = "s" ;
80 static const char unit_hertz_str[] = "Hz" ;
81 static const char unit_byte_str[] = "byte" ;
82 static const char unit_bit_per_second_str[] = "bit/s";
83
84 static void avprobe_cleanup(int ret)
85 {
86 av_dict_free(&fmt_entries_to_show);
87 av_dict_free(&stream_entries_to_show);
88 }
89
90 /*
91 * The output is structured in array and objects that might contain items
92 * Array could require the objects within to not be named.
93 * Object could require the items within to be named.
94 *
95 * For flat representation the name of each section is saved on prefix so it
96 * can be rendered in order to represent nested structures (e.g. array of
97 * objects for the packets list).
98 *
99 * Within an array each element can need an unique identifier or an index.
100 *
101 * Nesting level is accounted separately.
102 */
103
104 typedef enum {
105 ARRAY,
106 OBJECT
107 } PrintElementType;
108
109 typedef struct PrintElement {
110 const char *name;
111 PrintElementType type;
112 int64_t index;
113 int64_t nb_elems;
114 } PrintElement;
115
116 typedef struct PrintContext {
117 PrintElement *prefix;
118 int level;
119 void (*print_header)(void);
120 void (*print_footer)(void);
121
122 void (*print_array_header) (const char *name, int plain_values);
123 void (*print_array_footer) (const char *name, int plain_values);
124 void (*print_object_header)(const char *name);
125 void (*print_object_footer)(const char *name);
126
127 void (*print_integer) (const char *key, int64_t value);
128 void (*print_string) (const char *key, const char *value);
129 } PrintContext;
130
131 static AVIOContext *probe_out = NULL;
132 static PrintContext octx;
133 #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
134
135 /*
136 * Default format, INI
137 *
138 * - all key and values are utf8
139 * - '.' is the subgroup separator
140 * - newlines and the following characters are escaped
141 * - '\' is the escape character
142 * - '#' is the comment
143 * - '=' is the key/value separators
144 * - ':' is not used but usually parsed as key/value separator
145 */
146
147 static void ini_print_header(void)
148 {
149 avio_printf(probe_out, "# avprobe output\n\n");
150 }
151 static void ini_print_footer(void)
152 {
153 avio_w8(probe_out, '\n');
154 }
155
156 static void ini_escape_print(const char *s)
157 {
158 int i = 0;
159 char c = 0;
160
161 while (c = s[i++]) {
162 switch (c) {
163 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
164 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
165 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
166 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
167 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
168 case '\\':
169 case '#' :
170 case '=' :
171 case ':' : avio_w8(probe_out, '\\');
172 default:
173 if ((unsigned char)c < 32)
174 avio_printf(probe_out, "\\x00%02x", c & 0xff);
175 else
176 avio_w8(probe_out, c);
177 break;
178 }
179 }
180 }
181
182 static void ini_print_array_header(const char *name, int plain_values)
183 {
184 if (!plain_values) {
185 /* Add a new line if we create a new full group */
186 if (octx.prefix[octx.level -1].nb_elems)
187 avio_printf(probe_out, "\n");
188 } else {
189 ini_escape_print(name);
190 avio_w8(probe_out, '=');
191 }
192 }
193
194 static void ini_print_array_footer(const char *name, int plain_values)
195 {
196 if (plain_values)
197 avio_printf(probe_out, "\n");
198 }
199
200 static void ini_print_object_header(const char *name)
201 {
202 int i;
203 PrintElement *el = octx.prefix + octx.level -1;
204
205 if (el->nb_elems)
206 avio_printf(probe_out, "\n");
207
208 avio_printf(probe_out, "[");
209
210 for (i = 1; i < octx.level; i++) {
211 el = octx.prefix + i;
212 avio_printf(probe_out, "%s.", el->name);
213 if (el->index >= 0)
214 avio_printf(probe_out, "%"PRId64".", el->index);
215 }
216
217 avio_printf(probe_out, "%s", name);
218 if (el->type == ARRAY)
219 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
220 avio_printf(probe_out, "]\n");
221 }
222
223 static void ini_print_integer(const char *key, int64_t value)
224 {
225 if (key) {
226 ini_escape_print(key);
227 avio_printf(probe_out, "=%"PRId64"\n", value);
228 } else {
229 if (octx.prefix[octx.level -1].nb_elems)
230 avio_printf(probe_out, ",");
231 avio_printf(probe_out, "%"PRId64, value);
232 }
233 }
234
235
236 static void ini_print_string(const char *key, const char *value)
237 {
238 ini_escape_print(key);
239 avio_printf(probe_out, "=");
240 ini_escape_print(value);
241 avio_w8(probe_out, '\n');
242 }
243
244 /*
245 * Alternate format, JSON
246 */
247
248 static void json_print_header(void)
249 {
250 avio_printf(probe_out, "{");
251 }
252 static void json_print_footer(void)
253 {
254 avio_printf(probe_out, "}\n");
255 }
256
257 static void json_print_array_header(const char *name, int plain_values)
258 {
259 if (octx.prefix[octx.level -1].nb_elems)
260 avio_printf(probe_out, ",\n");
261 AVP_INDENT();
262 avio_printf(probe_out, "\"%s\" : ", name);
263 avio_printf(probe_out, "[\n");
264 }
265
266 static void json_print_array_footer(const char *name, int plain_values)
267 {
268 avio_printf(probe_out, "\n");
269 AVP_INDENT();
270 avio_printf(probe_out, "]");
271 }
272
273 static void json_print_object_header(const char *name)
274 {
275 if (octx.prefix[octx.level -1].nb_elems)
276 avio_printf(probe_out, ",\n");
277 AVP_INDENT();
278 if (octx.prefix[octx.level -1].type == OBJECT)
279 avio_printf(probe_out, "\"%s\" : ", name);
280 avio_printf(probe_out, "{\n");
281 }
282
283 static void json_print_object_footer(const char *name)
284 {
285 avio_printf(probe_out, "\n");
286 AVP_INDENT();
287 avio_printf(probe_out, "}");
288 }
289
290 static void json_print_integer(const char *key, int64_t value)
291 {
292 if (key) {
293 if (octx.prefix[octx.level -1].nb_elems)
294 avio_printf(probe_out, ",\n");
295 AVP_INDENT();
296 avio_printf(probe_out, "\"%s\" : ", key);
297 } else {
298 if (octx.prefix[octx.level -1].nb_elems)
299 avio_printf(probe_out, ", ");
300 else
301 AVP_INDENT();
302 }
303 avio_printf(probe_out, "%"PRId64, value);
304 }
305
306 static void json_escape_print(const char *s)
307 {
308 int i = 0;
309 char c = 0;
310
311 while (c = s[i++]) {
312 switch (c) {
313 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
314 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
315 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
316 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
317 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
318 case '\\':
319 case '"' : avio_w8(probe_out, '\\');
320 default:
321 if ((unsigned char)c < 32)
322 avio_printf(probe_out, "\\u00%02x", c & 0xff);
323 else
324 avio_w8(probe_out, c);
325 break;
326 }
327 }
328 }
329
330 static void json_print_string(const char *key, const char *value)
331 {
332 if (octx.prefix[octx.level -1].nb_elems)
333 avio_printf(probe_out, ",\n");
334 AVP_INDENT();
335 avio_w8(probe_out, '\"');
336 json_escape_print(key);
337 avio_printf(probe_out, "\" : \"");
338 json_escape_print(value);
339 avio_w8(probe_out, '\"');
340 }
341
342 /*
343 * old-style pseudo-INI
344 */
345 static void old_print_object_header(const char *name)
346 {
347 char *str, *p;
348
349 if (!strcmp(name, "tags"))
350 return;
351
352 str = p = av_strdup(name);
353 if (!str)
354 return;
355 while (*p) {
356 *p = av_toupper(*p);
357 p++;
358 }
359
360 avio_printf(probe_out, "[%s]\n", str);
361 av_freep(&str);
362 }
363
364 static void old_print_object_footer(const char *name)
365 {
366 char *str, *p;
367
368 if (!strcmp(name, "tags"))
369 return;
370
371 str = p = av_strdup(name);
372 if (!str)
373 return;
374 while (*p) {
375 *p = av_toupper(*p);
376 p++;
377 }
378
379 avio_printf(probe_out, "[/%s]\n", str);
380 av_freep(&str);
381 }
382
383 static void old_print_string(const char *key, const char *value)
384 {
385 if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
386 avio_printf(probe_out, "TAG:");
387 ini_print_string(key, value);
388 }
389
390 /*
391 * Simple Formatter for single entries.
392 */
393
394 static void show_format_entry_integer(const char *key, int64_t value)
395 {
396 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
397 if (nb_fmt_entries_to_show > 1)
398 avio_printf(probe_out, "%s=", key);
399 avio_printf(probe_out, "%"PRId64"\n", value);
400 }
401 }
402
403 static void show_format_entry_string(const char *key, const char *value)
404 {
405 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
406 if (nb_fmt_entries_to_show > 1)
407 avio_printf(probe_out, "%s=", key);
408 avio_printf(probe_out, "%s\n", value);
409 }
410 }
411
412 static void show_stream_entry_header(const char *key, int value)
413 {
414 header_key = key;
415 }
416
417 static void show_stream_entry_footer(const char *key, int value)
418 {
419 header_key = NULL;
420 }
421
422 static void show_stream_entry_integer(const char *key, int64_t value)
423 {
424 if (!key)
425 key = header_key;
426
427 if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
428 if (nb_stream_entries_to_show > 1)
429 avio_printf(probe_out, "%s=", key);
430 avio_printf(probe_out, "%"PRId64"\n", value);
431 }
432 }
433
434 static void show_stream_entry_string(const char *key, const char *value)
435 {
436 if (key && av_dict_get(stream_entries_to_show, key, NULL, 0)) {
437 if (nb_stream_entries_to_show > 1)
438 avio_printf(probe_out, "%s=", key);
439 avio_printf(probe_out, "%s\n", value);
440 }
441 }
442
443 static void probe_group_enter(const char *name, int type)
444 {
445 int64_t count = -1;
446
447 octx.prefix =
448 av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
449
450 if (!octx.prefix || !name) {
451 fprintf(stderr, "Out of memory\n");
452 exit_program(1);
453 }
454
455 if (octx.level) {
456 PrintElement *parent = octx.prefix + octx.level -1;
457 if (parent->type == ARRAY)
458 count = parent->nb_elems;
459 parent->nb_elems++;
460 }
461
462 octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
463 }
464
465 static void probe_group_leave(void)
466 {
467 --octx.level;
468 }
469
470 static void probe_header(void)
471 {
472 if (octx.print_header)
473 octx.print_header();
474 probe_group_enter("root", OBJECT);
475 }
476
477 static void probe_footer(void)
478 {
479 if (octx.print_footer)
480 octx.print_footer();
481 probe_group_leave();
482 }
483
484
485 static void probe_array_header(const char *name, int plain_values)
486 {
487 if (octx.print_array_header)
488 octx.print_array_header(name, plain_values);
489
490 probe_group_enter(name, ARRAY);
491 }
492
493 static void probe_array_footer(const char *name, int plain_values)
494 {
495 probe_group_leave();
496 if (octx.print_array_footer)
497 octx.print_array_footer(name, plain_values);
498 }
499
500 static void probe_object_header(const char *name)
501 {
502 if (octx.print_object_header)
503 octx.print_object_header(name);
504
505 probe_group_enter(name, OBJECT);
506 }
507
508 static void probe_object_footer(const char *name)
509 {
510 probe_group_leave();
511 if (octx.print_object_footer)
512 octx.print_object_footer(name);
513 }
514
515 static void probe_int(const char *key, int64_t value)
516 {
517 octx.print_integer(key, value);
518 octx.prefix[octx.level -1].nb_elems++;
519 }
520
521 static void probe_str(const char *key, const char *value)
522 {
523 octx.print_string(key, value);
524 octx.prefix[octx.level -1].nb_elems++;
525 }
526
527 static void probe_dict(AVDictionary *dict, const char *name)
528 {
529 AVDictionaryEntry *entry = NULL;
530 if (!dict)
531 return;
532 probe_object_header(name);
533 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
534 probe_str(entry->key, entry->value);
535 }
536 probe_object_footer(name);
537 }
538
539 static char *value_string(char *buf, int buf_size, double val, const char *unit)
540 {
541 if (unit == unit_second_str && use_value_sexagesimal_format) {
542 double secs;
543 int hours, mins;
544 secs = val;
545 mins = (int)secs / 60;
546 secs = secs - mins * 60;
547 hours = mins / 60;
548 mins %= 60;
549 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
550 } else if (use_value_prefix) {
551 const char *prefix_string;
552 int index;
553
554 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
555 index = (int) log2(val) / 10;
556 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
557 val /= pow(2, index * 10);
558 prefix_string = binary_unit_prefixes[index];
559 } else {
560 index = (int) (log10(val)) / 3;
561 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
562 val /= pow(10, index * 3);
563 prefix_string = decimal_unit_prefixes[index];
564 }
565 snprintf(buf, buf_size, "%.*f%s%s",
566 index ? 3 : 0, val,
567 prefix_string,
568 show_value_unit ? unit : "");
569 } else {
570 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
571 }
572
573 return buf;
574 }
575
576 static char *time_value_string(char *buf, int buf_size, int64_t val,
577 const AVRational *time_base)
578 {
579 if (val == AV_NOPTS_VALUE) {
580 snprintf(buf, buf_size, "N/A");
581 } else {
582 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
583 }
584
585 return buf;
586 }
587
588 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
589 {
590 if (ts == AV_NOPTS_VALUE) {
591 snprintf(buf, buf_size, "N/A");
592 } else {
593 snprintf(buf, buf_size, "%"PRId64, ts);
594 }
595
596 return buf;
597 }
598
599 static char *rational_string(char *buf, int buf_size, const char *sep,
600 const AVRational *rat)
601 {
602 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
603 return buf;
604 }
605
606 static char *tag_string(char *buf, int buf_size, int tag)
607 {
608 snprintf(buf, buf_size, "0x%04x", tag);
609 return buf;
610 }
611
612 static char *unknown_string(char *buf, int buf_size, int val)
613 {
614 snprintf(buf, buf_size, "Unknown (%d)", val);
615 return buf;
616 }
617
618 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
619 {
620 char val_str[128];
621 AVStream *st = fmt_ctx->streams[pkt->stream_index];
622
623 probe_object_header("packet");
624 probe_str("codec_type", media_type_string(st->codecpar->codec_type));
625 probe_int("stream_index", pkt->stream_index);
626 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
627 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
628 pkt->pts, &st->time_base));
629 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
630 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
631 pkt->dts, &st->time_base));
632 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
633 pkt->duration));
634 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
635 pkt->duration,
636 &st->time_base));
637 probe_str("size", value_string(val_str, sizeof(val_str),
638 pkt->size, unit_byte_str));
639 probe_int("pos", pkt->pos);
640 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
641 probe_object_footer("packet");
642 }
643
644 static void show_packets(InputFile *ifile)
645 {
646 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
647 AVPacket pkt;
648
649 av_init_packet(&pkt);
650 probe_array_header("packets", 0);
651 while (!av_read_frame(fmt_ctx, &pkt)) {
652 show_packet(fmt_ctx, &pkt);
653 av_packet_unref(&pkt);
654 }
655 probe_array_footer("packets", 0);
656 }
657
658 static void show_stream(InputFile *ifile, InputStream *ist)
659 {
660 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
661 AVStream *stream = ist->st;
662 AVCodecParameters *par;
663 AVCodecContext *dec_ctx;
664 const AVCodecDescriptor *codec_desc;
665 const char *profile;
666 char val_str[128];
667 AVRational display_aspect_ratio, *sar = NULL;
668 const AVPixFmtDescriptor *desc;
669 const char *val;
670
671 probe_object_header("stream");
672
673 probe_int("index", stream->index);
674
675 par = stream->codecpar;
676 dec_ctx = ist->dec_ctx;
677 codec_desc = avcodec_descriptor_get(par->codec_id);
678 if (codec_desc) {
679 probe_str("codec_name", codec_desc->name);
680 probe_str("codec_long_name", codec_desc->long_name);
681 } else {
682 probe_str("codec_name", "unknown");
683 }
684
685 probe_str("codec_type", media_type_string(par->codec_type));
686
687 /* print AVI/FourCC tag */
688 av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
689 probe_str("codec_tag_string", val_str);
690 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
691 par->codec_tag));
692
693 /* print profile, if there is one */
694 profile = avcodec_profile_name(par->codec_id, par->profile);
695 if (profile)
696 probe_str("profile", profile);
697
698 switch (par->codec_type) {
699 case AVMEDIA_TYPE_VIDEO:
700 probe_int("width", par->width);
701 probe_int("height", par->height);
702 if (dec_ctx) {
703 probe_int("coded_width", dec_ctx->coded_width);
704 probe_int("coded_height", dec_ctx->coded_height);
705 probe_int("has_b_frames", dec_ctx->has_b_frames);
706 }
707 if (dec_ctx && dec_ctx->sample_aspect_ratio.num)
708 sar = &dec_ctx->sample_aspect_ratio;
709 else if (par->sample_aspect_ratio.num)
710 sar = &par->sample_aspect_ratio;
711 else if (stream->sample_aspect_ratio.num)
712 sar = &stream->sample_aspect_ratio;
713
714 if (sar) {
715 probe_str("sample_aspect_ratio",
716 rational_string(val_str, sizeof(val_str), ":", sar));
717 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
718 par->width * sar->num, par->height * sar->den,
719 1024*1024);
720 probe_str("display_aspect_ratio",
721 rational_string(val_str, sizeof(val_str), ":",
722 &display_aspect_ratio));
723 }
724 desc = av_pix_fmt_desc_get(par->format);
725 probe_str("pix_fmt", desc ? desc->name : "unknown");
726 probe_int("level", par->level);
727
728 val = av_color_range_name(par->color_range);
729 if (!val)
730 val = unknown_string(val_str, sizeof(val_str), par->color_range);
731 probe_str("color_range", val);
732
733 val = av_color_space_name(par->color_space);
734 if (!val)
735 val = unknown_string(val_str, sizeof(val_str), par->color_space);
736 probe_str("color_space", val);
737
738 val = av_color_transfer_name(par->color_trc);
739 if (!val)
740 val = unknown_string(val_str, sizeof(val_str), par->color_trc);
741 probe_str("color_trc", val);
742
743 val = av_color_primaries_name(par->color_primaries);
744 if (!val)
745 val = unknown_string(val_str, sizeof(val_str), par->color_primaries);
746 probe_str("color_pri", val);
747
748 val = av_chroma_location_name(par->chroma_location);
749 if (!val)
750 val = unknown_string(val_str, sizeof(val_str), par->chroma_location);
751 probe_str("chroma_loc", val);
752 break;
753
754 case AVMEDIA_TYPE_AUDIO:
755 probe_str("sample_rate",
756 value_string(val_str, sizeof(val_str),
757 par->sample_rate,
758 unit_hertz_str));
759 probe_int("channels", par->channels);
760 probe_int("bits_per_sample",
761 av_get_bits_per_sample(par->codec_id));
762 break;
763 }
764
765 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
766 probe_int("id", stream->id);
767 probe_str("avg_frame_rate",
768 rational_string(val_str, sizeof(val_str), "/",
769 &stream->avg_frame_rate));
770
771 if (par->bit_rate)
772 probe_str("bit_rate",
773 value_string(val_str, sizeof(val_str),
774 par->bit_rate, unit_bit_per_second_str));
775 probe_str("time_base",
776 rational_string(val_str, sizeof(val_str), "/",
777 &stream->time_base));
778 probe_str("start_time",
779 time_value_string(val_str, sizeof(val_str),
780 stream->start_time, &stream->time_base));
781 probe_str("duration",
782 time_value_string(val_str, sizeof(val_str),
783 stream->duration, &stream->time_base));
784 if (stream->nb_frames)
785 probe_int("nb_frames", stream->nb_frames);
786
787 probe_dict(stream->metadata, "tags");
788
789 if (stream->nb_side_data) {
790 int i, j;
791
792 probe_object_header("sidedata");
793 for (i = 0; i < stream->nb_side_data; i++) {
794 const AVPacketSideData* sd = &stream->side_data[i];
795 AVStereo3D *stereo;
796 AVSphericalMapping *spherical;
797
798 switch (sd->type) {
799 case AV_PKT_DATA_DISPLAYMATRIX:
800 probe_object_header("displaymatrix");
801 probe_array_header("matrix", 1);
802 for (j = 0; j < 9; j++)
803 probe_int(NULL, ((int32_t *)sd->data)[j]);
804 probe_array_footer("matrix", 1);
805 probe_int("rotation",
806 av_display_rotation_get((int32_t *)sd->data));
807 probe_object_footer("displaymatrix");
808 break;
809 case AV_PKT_DATA_STEREO3D:
810 stereo = (AVStereo3D *)sd->data;
811 probe_object_header("stereo3d");
812 probe_str("type", av_stereo3d_type_name(stereo->type));
813 probe_int("inverted",
814 !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
815 probe_object_footer("stereo3d");
816 break;
817 case AV_PKT_DATA_SPHERICAL:
818 spherical = (AVSphericalMapping *)sd->data;
819 probe_object_header("spherical");
820 probe_str("projection", av_spherical_projection_name(spherical->projection));
821
822 if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
823 probe_int("padding", spherical->padding);
824 } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
825 size_t l, t, r, b;
826 av_spherical_tile_bounds(spherical, par->width, par->height,
827 &l, &t, &r, &b);
828 probe_object_header("bounding");
829 probe_int("left", l);
830 probe_int("top", t);
831 probe_int("right", r);
832 probe_int("bottom", b);
833 probe_object_footer("bounding");
834 }
835
836 probe_object_header("orientation");
837 probe_int("yaw", (double) spherical->yaw / (1 << 16));
838 probe_int("pitch", (double) spherical->pitch / (1 << 16));
839 probe_int("roll", (double) spherical->roll / (1 << 16));
840 probe_object_footer("orientation");
841
842 probe_object_footer("spherical");
843 break;
844 }
845 }
846 probe_object_footer("sidedata");
847 }
848
849 probe_object_footer("stream");
850 }
851
852 static void show_format(InputFile *ifile)
853 {
854 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
855 char val_str[128];
856 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
857
858 probe_object_header("format");
859 probe_str("filename", fmt_ctx->filename);
860 probe_int("nb_streams", fmt_ctx->nb_streams);
861 probe_str("format_name", fmt_ctx->iformat->name);
862 probe_str("format_long_name", fmt_ctx->iformat->long_name);
863 probe_str("start_time",
864 time_value_string(val_str, sizeof(val_str),
865 fmt_ctx->start_time, &AV_TIME_BASE_Q));
866 probe_str("duration",
867 time_value_string(val_str, sizeof(val_str),
868 fmt_ctx->duration, &AV_TIME_BASE_Q));
869 probe_str("size",
870 size >= 0 ? value_string(val_str, sizeof(val_str),
871 size, unit_byte_str)
872 : "unknown");
873 probe_str("bit_rate",
874 value_string(val_str, sizeof(val_str),
875 fmt_ctx->bit_rate, unit_bit_per_second_str));
876
877 probe_dict(fmt_ctx->metadata, "tags");
878
879 probe_object_footer("format");
880 }
881
882 static int open_input_file(InputFile *ifile, const char *filename)
883 {
884 int err, i;
885 AVFormatContext *fmt_ctx = NULL;
886 AVDictionaryEntry *t;
887
888 if ((err = avformat_open_input(&fmt_ctx, filename,
889 iformat, &format_opts)) < 0) {
890 print_error(filename, err);
891 return err;
892 }
893 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
894 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
895 return AVERROR_OPTION_NOT_FOUND;
896 }
897
898
899 /* fill the streams in the format context */
900 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
901 print_error(filename, err);
902 return err;
903 }
904
905 av_dump_format(fmt_ctx, 0, filename, 0);
906
907 ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
908 sizeof(*ifile->streams));
909 if (!ifile->streams)
910 exit(1);
911 ifile->nb_streams = fmt_ctx->nb_streams;
912
913 /* bind a decoder to each input stream */
914 for (i = 0; i < fmt_ctx->nb_streams; i++) {
915 InputStream *ist = &ifile->streams[i];
916 AVStream *stream = fmt_ctx->streams[i];
917 AVCodec *codec;
918
919 ist->st = stream;
920
921 if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
922 fprintf(stderr, "Failed to probe codec for input stream %d\n",
923 stream->index);
924 continue;
925 }
926
927 codec = avcodec_find_decoder(stream->codecpar->codec_id);
928 if (!codec) {
929 fprintf(stderr,
930 "Unsupported codec with id %d for input stream %d\n",
931 stream->codecpar->codec_id, stream->index);
932 continue;
933 }
934
935 ist->dec_ctx = avcodec_alloc_context3(codec);
936 if (!ist->dec_ctx)
937 exit(1);
938
939 err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
940 if (err < 0)
941 exit(1);
942
943 err = avcodec_open2(ist->dec_ctx, NULL, NULL);
944 if (err < 0) {
945 fprintf(stderr, "Error while opening codec for input stream %d\n",
946 stream->index);
947 exit(1);
948
949 }
950 }
951
952 ifile->fmt_ctx = fmt_ctx;
953 return 0;
954 }
955
956 static void close_input_file(InputFile *ifile)
957 {
958 int i;
959
960 /* close decoder for each stream */
961 for (i = 0; i < ifile->nb_streams; i++) {
962 InputStream *ist = &ifile->streams[i];
963
964 avcodec_free_context(&ist->dec_ctx);
965 }
966
967 av_freep(&ifile->streams);
968 ifile->nb_streams = 0;
969
970 avformat_close_input(&ifile->fmt_ctx);
971 }
972
973 static int probe_file(const char *filename)
974 {
975 InputFile ifile;
976 int ret, i;
977
978 ret = open_input_file(&ifile, filename);
979 if (ret < 0)
980 return ret;
981
982 if (do_show_format)
983 show_format(&ifile);
984
985 if (do_show_streams) {
986 probe_array_header("streams", 0);
987 for (i = 0; i < ifile.nb_streams; i++)
988 show_stream(&ifile, &ifile.streams[i]);
989 probe_array_footer("streams", 0);
990 }
991
992 if (do_show_packets)
993 show_packets(&ifile);
994
995 close_input_file(&ifile);
996 return 0;
997 }
998
999 static void show_usage(void)
1000 {
1001 printf("Simple multimedia streams analyzer\n");
1002 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1003 printf("\n");
1004 }
1005
1006 static int opt_format(void *optctx, const char *opt, const char *arg)
1007 {
1008 iformat = av_find_input_format(arg);
1009 if (!iformat) {
1010 fprintf(stderr, "Unknown input format: %s\n", arg);
1011 return AVERROR(EINVAL);
1012 }
1013 return 0;
1014 }
1015
1016 static int opt_output_format(void *optctx, const char *opt, const char *arg)
1017 {
1018
1019 if (!strcmp(arg, "json")) {
1020 octx.print_header = json_print_header;
1021 octx.print_footer = json_print_footer;
1022 octx.print_array_header = json_print_array_header;
1023 octx.print_array_footer = json_print_array_footer;
1024 octx.print_object_header = json_print_object_header;
1025 octx.print_object_footer = json_print_object_footer;
1026
1027 octx.print_integer = json_print_integer;
1028 octx.print_string = json_print_string;
1029 } else if (!strcmp(arg, "ini")) {
1030 octx.print_header = ini_print_header;
1031 octx.print_footer = ini_print_footer;
1032 octx.print_array_header = ini_print_array_header;
1033 octx.print_array_footer = ini_print_array_footer;
1034 octx.print_object_header = ini_print_object_header;
1035
1036 octx.print_integer = ini_print_integer;
1037 octx.print_string = ini_print_string;
1038 } else if (!strcmp(arg, "old")) {
1039 octx.print_header = NULL;
1040 octx.print_object_header = old_print_object_header;
1041 octx.print_object_footer = old_print_object_footer;
1042
1043 octx.print_string = old_print_string;
1044 } else {
1045 av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
1046 return AVERROR(EINVAL);
1047 }
1048 return 0;
1049 }
1050
1051 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
1052 {
1053 do_show_format = 1;
1054 nb_fmt_entries_to_show++;
1055 octx.print_header = NULL;
1056 octx.print_footer = NULL;
1057 octx.print_array_header = NULL;
1058 octx.print_array_footer = NULL;
1059 octx.print_object_header = NULL;
1060 octx.print_object_footer = NULL;
1061
1062 octx.print_integer = show_format_entry_integer;
1063 octx.print_string = show_format_entry_string;
1064 av_dict_set(&fmt_entries_to_show, arg, "", 0);
1065 return 0;
1066 }
1067
1068 static int opt_show_stream_entry(void *optctx, const char *opt, const char *arg)
1069 {
1070 const char *p = arg;
1071
1072 do_show_streams = 1;
1073 nb_stream_entries_to_show++;
1074 octx.print_header = NULL;
1075 octx.print_footer = NULL;
1076 octx.print_array_header = show_stream_entry_header;
1077 octx.print_array_footer = show_stream_entry_footer;
1078 octx.print_object_header = NULL;
1079 octx.print_object_footer = NULL;
1080
1081 octx.print_integer = show_stream_entry_integer;
1082 octx.print_string = show_stream_entry_string;
1083
1084 while (*p) {
1085 char *val = av_get_token(&p, ",");
1086 if (!val)
1087 return AVERROR(ENOMEM);
1088
1089 av_dict_set(&stream_entries_to_show, val, "", 0);
1090
1091 av_free(val);
1092 if (*p)
1093 p++;
1094 }
1095
1096 return 0;
1097 }
1098
1099 static void opt_input_file(void *optctx, const char *arg)
1100 {
1101 if (input_filename) {
1102 fprintf(stderr,
1103 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1104 arg, input_filename);
1105 exit_program(1);
1106 }
1107 if (!strcmp(arg, "-"))
1108 arg = "pipe:";
1109 input_filename = arg;
1110 }
1111
1112 void show_help_default(const char *opt, const char *arg)
1113 {
1114 av_log_set_callback(log_callback_help);
1115 show_usage();
1116 show_help_options(options, "Main options:", 0, 0, 0);
1117 printf("\n");
1118 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1119 }
1120
1121 static int opt_pretty(void *optctx, const char *opt, const char *arg)
1122 {
1123 show_value_unit = 1;
1124 use_value_prefix = 1;
1125 use_byte_value_binary_prefix = 1;
1126 use_value_sexagesimal_format = 1;
1127 return 0;
1128 }
1129
1130 static const OptionDef real_options[] = {
1131 CMDUTILS_COMMON_OPTIONS
1132 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
1133 { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
1134 { "unit", OPT_BOOL, {&show_value_unit},
1135 "show unit of the displayed values" },
1136 { "prefix", OPT_BOOL, {&use_value_prefix},
1137 "use SI prefixes for the displayed values" },
1138 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
1139 "use binary prefixes for byte units" },
1140 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
1141 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1142 { "pretty", 0, {.func_arg = opt_pretty},
1143 "prettify the format of displayed values, make it more human readable" },
1144 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
1145 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
1146 "show a particular entry from the format/container info", "entry" },
1147 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
1148 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
1149 { "show_stream_entry", HAS_ARG, {.func_arg = opt_show_stream_entry},
1150 "show a particular entry from all streams (comma separated)", "entry" },
1151 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
1152 "generic catch all option", "" },
1153 { NULL, },
1154 };
1155
1156 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
1157 {
1158 printf("%.*s", buf_size, buf);
1159 return 0;
1160 }
1161
1162 #define AVP_BUFFSIZE 4096
1163
1164 int main(int argc, char **argv)
1165 {
1166 int ret;
1167 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
1168
1169 if (!buffer)
1170 exit(1);
1171
1172 register_exit(avprobe_cleanup);
1173
1174 options = real_options;
1175 parse_loglevel(argc, argv, options);
1176 av_register_all();
1177 avformat_network_init();
1178 init_opts();
1179 #if CONFIG_AVDEVICE
1180 avdevice_register_all();
1181 #endif
1182
1183 show_banner();
1184
1185 octx.print_header = ini_print_header;
1186 octx.print_footer = ini_print_footer;
1187
1188 octx.print_array_header = ini_print_array_header;
1189 octx.print_array_footer = ini_print_array_footer;
1190 octx.print_object_header = ini_print_object_header;
1191
1192 octx.print_integer = ini_print_integer;
1193 octx.print_string = ini_print_string;
1194
1195 parse_options(NULL, argc, argv, options, opt_input_file);
1196
1197 if (!input_filename) {
1198 show_usage();
1199 fprintf(stderr, "You have to specify one input file.\n");
1200 fprintf(stderr,
1201 "Use -h to get full help or, even better, run 'man %s'.\n",
1202 program_name);
1203 exit_program(1);
1204 }
1205
1206 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
1207 probe_buf_write, NULL);
1208 if (!probe_out)
1209 exit_program(1);
1210
1211 probe_header();
1212 ret = probe_file(input_filename);
1213 probe_footer();
1214 avio_flush(probe_out);
1215 avio_context_free(&probe_out);
1216 av_freep(&buffer);
1217 uninit_opts();
1218 avformat_network_deinit();
1219
1220 return ret;
1221 }