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