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