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