OSDN Git Service

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