OSDN Git Service

rtsp: Fix the indentation of a linewrapped statement
[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/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/libm.h"
31 #include "libavdevice/avdevice.h"
32 #include "cmdutils.h"
33
34 const char program_name[] = "avprobe";
35 const int program_birth_year = 2007;
36
37 static int do_show_format  = 0;
38 static AVDictionary *fmt_entries_to_show = NULL;
39 static int nb_fmt_entries_to_show;
40 static int do_show_packets = 0;
41 static int do_show_streams = 0;
42
43 static int show_value_unit              = 0;
44 static int use_value_prefix             = 0;
45 static int use_byte_value_binary_prefix = 0;
46 static int use_value_sexagesimal_format = 0;
47
48 /* globals */
49 static const OptionDef *options;
50
51 /* AVprobe context */
52 static const char *input_filename;
53 static AVInputFormat *iformat = NULL;
54
55 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
56 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P"  };
57
58 static const char unit_second_str[]         = "s"    ;
59 static const char unit_hertz_str[]          = "Hz"   ;
60 static const char unit_byte_str[]           = "byte" ;
61 static const char unit_bit_per_second_str[] = "bit/s";
62
63 static void avprobe_cleanup(int ret)
64 {
65     av_dict_free(&fmt_entries_to_show);
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 } PrintElementType;
86
87 typedef struct PrintElement {
88     const char *name;
89     PrintElementType type;
90     int64_t index;
91     int64_t nb_elems;
92 } PrintElement;
93
94 typedef struct PrintContext {
95     PrintElement *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 } PrintContext;
108
109 static AVIOContext *probe_out = NULL;
110 static PrintContext 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     PrintElement *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->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     if (!str)
306         return;
307     while (*p) {
308         *p = av_toupper(*p);
309         p++;
310     }
311
312     avio_printf(probe_out, "[%s]\n", str);
313     av_freep(&str);
314 }
315
316 static void old_print_object_footer(const char *name)
317 {
318     char *str, *p;
319
320     if (!strcmp(name, "tags"))
321         return;
322
323     str = p = av_strdup(name);
324     if (!str)
325         return;
326     while (*p) {
327         *p = av_toupper(*p);
328         p++;
329     }
330
331     avio_printf(probe_out, "[/%s]\n", str);
332     av_freep(&str);
333 }
334
335 static void old_print_string(const char *key, const char *value)
336 {
337     if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
338         avio_printf(probe_out, "TAG:");
339     ini_print_string(key, value);
340 }
341
342 /*
343  * Simple Formatter for single entries.
344  */
345
346 static void show_format_entry_integer(const char *key, int64_t value)
347 {
348     if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
349         if (nb_fmt_entries_to_show > 1)
350             avio_printf(probe_out, "%s=", key);
351         avio_printf(probe_out, "%"PRId64"\n", value);
352     }
353 }
354
355 static void show_format_entry_string(const char *key, const char *value)
356 {
357     if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
358         if (nb_fmt_entries_to_show > 1)
359             avio_printf(probe_out, "%s=", key);
360         avio_printf(probe_out, "%s\n", value);
361     }
362 }
363
364 static void probe_group_enter(const char *name, int type)
365 {
366     int64_t count = -1;
367
368     octx.prefix =
369         av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
370
371     if (!octx.prefix || !name) {
372         fprintf(stderr, "Out of memory\n");
373         exit_program(1);
374     }
375
376     if (octx.level) {
377         PrintElement *parent = octx.prefix + octx.level -1;
378         if (parent->type == ARRAY)
379             count = parent->nb_elems;
380         parent->nb_elems++;
381     }
382
383     octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
384 }
385
386 static void probe_group_leave(void)
387 {
388     --octx.level;
389 }
390
391 static void probe_header(void)
392 {
393     if (octx.print_header)
394         octx.print_header();
395     probe_group_enter("root", OBJECT);
396 }
397
398 static void probe_footer(void)
399 {
400     if (octx.print_footer)
401         octx.print_footer();
402     probe_group_leave();
403 }
404
405
406 static void probe_array_header(const char *name)
407 {
408     if (octx.print_array_header)
409         octx.print_array_header(name);
410
411     probe_group_enter(name, ARRAY);
412 }
413
414 static void probe_array_footer(const char *name)
415 {
416     probe_group_leave();
417     if (octx.print_array_footer)
418         octx.print_array_footer(name);
419 }
420
421 static void probe_object_header(const char *name)
422 {
423     if (octx.print_object_header)
424         octx.print_object_header(name);
425
426     probe_group_enter(name, OBJECT);
427 }
428
429 static void probe_object_footer(const char *name)
430 {
431     probe_group_leave();
432     if (octx.print_object_footer)
433         octx.print_object_footer(name);
434 }
435
436 static void probe_int(const char *key, int64_t value)
437 {
438     octx.print_integer(key, value);
439     octx.prefix[octx.level -1].nb_elems++;
440 }
441
442 static void probe_str(const char *key, const char *value)
443 {
444     octx.print_string(key, value);
445     octx.prefix[octx.level -1].nb_elems++;
446 }
447
448 static void probe_dict(AVDictionary *dict, const char *name)
449 {
450     AVDictionaryEntry *entry = NULL;
451     if (!dict)
452         return;
453     probe_object_header(name);
454     while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
455         probe_str(entry->key, entry->value);
456     }
457     probe_object_footer(name);
458 }
459
460 static char *value_string(char *buf, int buf_size, double val, const char *unit)
461 {
462     if (unit == unit_second_str && use_value_sexagesimal_format) {
463         double secs;
464         int hours, mins;
465         secs  = val;
466         mins  = (int)secs / 60;
467         secs  = secs - mins * 60;
468         hours = mins / 60;
469         mins %= 60;
470         snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
471     } else if (use_value_prefix) {
472         const char *prefix_string;
473         int index;
474
475         if (unit == unit_byte_str && use_byte_value_binary_prefix) {
476             index = (int) log2(val) / 10;
477             index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
478             val  /= pow(2, index * 10);
479             prefix_string = binary_unit_prefixes[index];
480         } else {
481             index = (int) (log10(val)) / 3;
482             index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
483             val  /= pow(10, index * 3);
484             prefix_string = decimal_unit_prefixes[index];
485         }
486         snprintf(buf, buf_size, "%.*f%s%s",
487                  index ? 3 : 0, val,
488                  prefix_string,
489                  show_value_unit ? unit : "");
490     } else {
491         snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
492     }
493
494     return buf;
495 }
496
497 static char *time_value_string(char *buf, int buf_size, int64_t val,
498                                const AVRational *time_base)
499 {
500     if (val == AV_NOPTS_VALUE) {
501         snprintf(buf, buf_size, "N/A");
502     } else {
503         value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
504     }
505
506     return buf;
507 }
508
509 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
510 {
511     if (ts == AV_NOPTS_VALUE) {
512         snprintf(buf, buf_size, "N/A");
513     } else {
514         snprintf(buf, buf_size, "%"PRId64, ts);
515     }
516
517     return buf;
518 }
519
520 static char *rational_string(char *buf, int buf_size, const char *sep,
521                              const AVRational *rat)
522 {
523     snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
524     return buf;
525 }
526
527 static char *tag_string(char *buf, int buf_size, int tag)
528 {
529     snprintf(buf, buf_size, "0x%04x", tag);
530     return buf;
531 }
532
533 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
534 {
535     char val_str[128];
536     AVStream *st = fmt_ctx->streams[pkt->stream_index];
537
538     probe_object_header("packet");
539     probe_str("codec_type", media_type_string(st->codec->codec_type));
540     probe_int("stream_index", pkt->stream_index);
541     probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
542     probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
543                                                pkt->pts, &st->time_base));
544     probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
545     probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
546                                                pkt->dts, &st->time_base));
547     probe_str("duration", ts_value_string(val_str, sizeof(val_str),
548                                              pkt->duration));
549     probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
550                                                     pkt->duration,
551                                                     &st->time_base));
552     probe_str("size", value_string(val_str, sizeof(val_str),
553                                       pkt->size, unit_byte_str));
554     probe_int("pos", pkt->pos);
555     probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
556     probe_object_footer("packet");
557 }
558
559 static void show_packets(AVFormatContext *fmt_ctx)
560 {
561     AVPacket pkt;
562
563     av_init_packet(&pkt);
564     probe_array_header("packets");
565     while (!av_read_frame(fmt_ctx, &pkt))
566         show_packet(fmt_ctx, &pkt);
567     probe_array_footer("packets");
568 }
569
570 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
571 {
572     AVStream *stream = fmt_ctx->streams[stream_idx];
573     AVCodecContext *dec_ctx;
574     const AVCodec *dec;
575     const char *profile;
576     char val_str[128];
577     AVRational display_aspect_ratio, *sar = NULL;
578     const AVPixFmtDescriptor *desc;
579
580     probe_object_header("stream");
581
582     probe_int("index", stream->index);
583
584     if ((dec_ctx = stream->codec)) {
585         if ((dec = dec_ctx->codec)) {
586             probe_str("codec_name", dec->name);
587             probe_str("codec_long_name", dec->long_name);
588         } else {
589             probe_str("codec_name", "unknown");
590         }
591
592         probe_str("codec_type", media_type_string(dec_ctx->codec_type));
593         probe_str("codec_time_base",
594                   rational_string(val_str, sizeof(val_str),
595                                   "/", &dec_ctx->time_base));
596
597         /* print AVI/FourCC tag */
598         av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
599         probe_str("codec_tag_string", val_str);
600         probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
601                                           dec_ctx->codec_tag));
602
603         /* print profile, if there is one */
604         if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
605             probe_str("profile", profile);
606
607         switch (dec_ctx->codec_type) {
608         case AVMEDIA_TYPE_VIDEO:
609             probe_int("width", dec_ctx->width);
610             probe_int("height", dec_ctx->height);
611             probe_int("has_b_frames", dec_ctx->has_b_frames);
612             if (dec_ctx->sample_aspect_ratio.num)
613                 sar = &dec_ctx->sample_aspect_ratio;
614             else if (stream->sample_aspect_ratio.num)
615                 sar = &stream->sample_aspect_ratio;
616
617             if (sar) {
618                 probe_str("sample_aspect_ratio",
619                           rational_string(val_str, sizeof(val_str), ":", sar));
620                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
621                           dec_ctx->width  * sar->num, dec_ctx->height * sar->den,
622                           1024*1024);
623                 probe_str("display_aspect_ratio",
624                           rational_string(val_str, sizeof(val_str), ":",
625                           &display_aspect_ratio));
626             }
627             desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
628             probe_str("pix_fmt", desc ? desc->name : "unknown");
629             probe_int("level", dec_ctx->level);
630
631             probe_str("color_range", av_color_range_name(dec_ctx->color_range));
632             probe_str("color_space", av_color_space_name(dec_ctx->colorspace));
633             probe_str("color_trc", av_color_transfer_name(dec_ctx->color_trc));
634             probe_str("color_pri", av_color_primaries_name(dec_ctx->color_primaries));
635             probe_str("chroma_loc", av_chroma_location_name(dec_ctx->chroma_sample_location));
636             break;
637
638         case AVMEDIA_TYPE_AUDIO:
639             probe_str("sample_rate",
640                       value_string(val_str, sizeof(val_str),
641                                    dec_ctx->sample_rate,
642                                    unit_hertz_str));
643             probe_int("channels", dec_ctx->channels);
644             probe_int("bits_per_sample",
645                       av_get_bits_per_sample(dec_ctx->codec_id));
646             break;
647         }
648     } else {
649         probe_str("codec_type", "unknown");
650     }
651
652     if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
653         probe_int("id", stream->id);
654     probe_str("avg_frame_rate",
655               rational_string(val_str, sizeof(val_str), "/",
656               &stream->avg_frame_rate));
657     if (dec_ctx->bit_rate)
658         probe_str("bit_rate",
659                   value_string(val_str, sizeof(val_str),
660                                dec_ctx->bit_rate, unit_bit_per_second_str));
661     probe_str("time_base",
662               rational_string(val_str, sizeof(val_str), "/",
663               &stream->time_base));
664     probe_str("start_time",
665               time_value_string(val_str, sizeof(val_str),
666                                 stream->start_time, &stream->time_base));
667     probe_str("duration",
668               time_value_string(val_str, sizeof(val_str),
669                                 stream->duration, &stream->time_base));
670     if (stream->nb_frames)
671         probe_int("nb_frames", stream->nb_frames);
672
673     probe_dict(stream->metadata, "tags");
674
675     probe_object_footer("stream");
676 }
677
678 static void show_format(AVFormatContext *fmt_ctx)
679 {
680     char val_str[128];
681     int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
682
683     probe_object_header("format");
684     probe_str("filename",         fmt_ctx->filename);
685     probe_int("nb_streams",       fmt_ctx->nb_streams);
686     probe_str("format_name",      fmt_ctx->iformat->name);
687     probe_str("format_long_name", fmt_ctx->iformat->long_name);
688     probe_str("start_time",
689                        time_value_string(val_str, sizeof(val_str),
690                                          fmt_ctx->start_time, &AV_TIME_BASE_Q));
691     probe_str("duration",
692                        time_value_string(val_str, sizeof(val_str),
693                                          fmt_ctx->duration, &AV_TIME_BASE_Q));
694     probe_str("size",
695                        size >= 0 ? value_string(val_str, sizeof(val_str),
696                                                 size, unit_byte_str)
697                                   : "unknown");
698     probe_str("bit_rate",
699                        value_string(val_str, sizeof(val_str),
700                                     fmt_ctx->bit_rate, unit_bit_per_second_str));
701
702     probe_dict(fmt_ctx->metadata, "tags");
703
704     probe_object_footer("format");
705 }
706
707 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
708 {
709     int err, i;
710     AVFormatContext *fmt_ctx = NULL;
711     AVDictionaryEntry *t;
712
713     if ((err = avformat_open_input(&fmt_ctx, filename,
714                                    iformat, &format_opts)) < 0) {
715         print_error(filename, err);
716         return err;
717     }
718     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
719         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
720         return AVERROR_OPTION_NOT_FOUND;
721     }
722
723
724     /* fill the streams in the format context */
725     if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
726         print_error(filename, err);
727         return err;
728     }
729
730     av_dump_format(fmt_ctx, 0, filename, 0);
731
732     /* bind a decoder to each input stream */
733     for (i = 0; i < fmt_ctx->nb_streams; i++) {
734         AVStream *stream = fmt_ctx->streams[i];
735         AVCodec *codec;
736
737         if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
738             fprintf(stderr, "Failed to probe codec for input stream %d\n",
739                     stream->index);
740         } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
741             fprintf(stderr,
742                     "Unsupported codec with id %d for input stream %d\n",
743                     stream->codec->codec_id, stream->index);
744         } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
745             fprintf(stderr, "Error while opening codec for input stream %d\n",
746                     stream->index);
747         }
748     }
749
750     *fmt_ctx_ptr = fmt_ctx;
751     return 0;
752 }
753
754 static void close_input_file(AVFormatContext **ctx_ptr)
755 {
756     int i;
757     AVFormatContext *fmt_ctx = *ctx_ptr;
758
759     /* close decoder for each stream */
760     for (i = 0; i < fmt_ctx->nb_streams; i++) {
761         AVStream *stream = fmt_ctx->streams[i];
762
763         avcodec_close(stream->codec);
764     }
765     avformat_close_input(ctx_ptr);
766 }
767
768 static int probe_file(const char *filename)
769 {
770     AVFormatContext *fmt_ctx;
771     int ret, i;
772
773     if ((ret = open_input_file(&fmt_ctx, filename)))
774         return ret;
775
776     if (do_show_format)
777         show_format(fmt_ctx);
778
779     if (do_show_streams) {
780         probe_array_header("streams");
781         for (i = 0; i < fmt_ctx->nb_streams; i++)
782             show_stream(fmt_ctx, i);
783         probe_array_footer("streams");
784     }
785
786     if (do_show_packets)
787         show_packets(fmt_ctx);
788
789     close_input_file(&fmt_ctx);
790     return 0;
791 }
792
793 static void show_usage(void)
794 {
795     printf("Simple multimedia streams analyzer\n");
796     printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
797     printf("\n");
798 }
799
800 static int opt_format(void *optctx, const char *opt, const char *arg)
801 {
802     iformat = av_find_input_format(arg);
803     if (!iformat) {
804         fprintf(stderr, "Unknown input format: %s\n", arg);
805         return AVERROR(EINVAL);
806     }
807     return 0;
808 }
809
810 static int opt_output_format(void *optctx, const char *opt, const char *arg)
811 {
812
813     if (!strcmp(arg, "json")) {
814         octx.print_header        = json_print_header;
815         octx.print_footer        = json_print_footer;
816         octx.print_array_header  = json_print_array_header;
817         octx.print_array_footer  = json_print_array_footer;
818         octx.print_object_header = json_print_object_header;
819         octx.print_object_footer = json_print_object_footer;
820
821         octx.print_integer = json_print_integer;
822         octx.print_string  = json_print_string;
823     } else if (!strcmp(arg, "ini")) {
824         octx.print_header        = ini_print_header;
825         octx.print_footer        = ini_print_footer;
826         octx.print_array_header  = ini_print_array_header;
827         octx.print_object_header = ini_print_object_header;
828
829         octx.print_integer = ini_print_integer;
830         octx.print_string  = ini_print_string;
831     } else if (!strcmp(arg, "old")) {
832         octx.print_header        = NULL;
833         octx.print_object_header = old_print_object_header;
834         octx.print_object_footer = old_print_object_footer;
835
836         octx.print_string        = old_print_string;
837     } else {
838         av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
839         return AVERROR(EINVAL);
840     }
841     return 0;
842 }
843
844 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
845 {
846     do_show_format = 1;
847     nb_fmt_entries_to_show++;
848     octx.print_header        = NULL;
849     octx.print_footer        = NULL;
850     octx.print_array_header  = NULL;
851     octx.print_array_footer  = NULL;
852     octx.print_object_header = NULL;
853     octx.print_object_footer = NULL;
854
855     octx.print_integer = show_format_entry_integer;
856     octx.print_string  = show_format_entry_string;
857     av_dict_set(&fmt_entries_to_show, arg, "", 0);
858     return 0;
859 }
860
861 static void opt_input_file(void *optctx, const char *arg)
862 {
863     if (input_filename) {
864         fprintf(stderr,
865                 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
866                 arg, input_filename);
867         exit_program(1);
868     }
869     if (!strcmp(arg, "-"))
870         arg = "pipe:";
871     input_filename = arg;
872 }
873
874 void show_help_default(const char *opt, const char *arg)
875 {
876     av_log_set_callback(log_callback_help);
877     show_usage();
878     show_help_options(options, "Main options:", 0, 0, 0);
879     printf("\n");
880     show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
881 }
882
883 static int opt_pretty(void *optctx, const char *opt, const char *arg)
884 {
885     show_value_unit              = 1;
886     use_value_prefix             = 1;
887     use_byte_value_binary_prefix = 1;
888     use_value_sexagesimal_format = 1;
889     return 0;
890 }
891
892 static const OptionDef real_options[] = {
893 #include "cmdutils_common_opts.h"
894     { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
895     { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
896     { "unit", OPT_BOOL, {&show_value_unit},
897       "show unit of the displayed values" },
898     { "prefix", OPT_BOOL, {&use_value_prefix},
899       "use SI prefixes for the displayed values" },
900     { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
901       "use binary prefixes for byte units" },
902     { "sexagesimal", OPT_BOOL,  {&use_value_sexagesimal_format},
903       "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
904     { "pretty", 0, {.func_arg = opt_pretty},
905       "prettify the format of displayed values, make it more human readable" },
906     { "show_format",  OPT_BOOL, {&do_show_format} , "show format/container info" },
907     { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
908       "show a particular entry from the format/container info", "entry" },
909     { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
910     { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
911     { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
912       "generic catch all option", "" },
913     { NULL, },
914 };
915
916 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
917 {
918     printf("%.*s", buf_size, buf);
919     return 0;
920 }
921
922 #define AVP_BUFFSIZE 4096
923
924 int main(int argc, char **argv)
925 {
926     int ret;
927     uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
928
929     if (!buffer)
930         exit(1);
931
932     register_exit(avprobe_cleanup);
933
934     options = real_options;
935     parse_loglevel(argc, argv, options);
936     av_register_all();
937     avformat_network_init();
938     init_opts();
939 #if CONFIG_AVDEVICE
940     avdevice_register_all();
941 #endif
942
943     show_banner();
944
945     octx.print_header = ini_print_header;
946     octx.print_footer = ini_print_footer;
947
948     octx.print_array_header = ini_print_array_header;
949     octx.print_object_header = ini_print_object_header;
950
951     octx.print_integer = ini_print_integer;
952     octx.print_string = ini_print_string;
953
954     parse_options(NULL, argc, argv, options, opt_input_file);
955
956     if (!input_filename) {
957         show_usage();
958         fprintf(stderr, "You have to specify one input file.\n");
959         fprintf(stderr,
960                 "Use -h to get full help or, even better, run 'man %s'.\n",
961                 program_name);
962         exit_program(1);
963     }
964
965     probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
966                                  probe_buf_write, NULL);
967     if (!probe_out)
968         exit_program(1);
969
970     probe_header();
971     ret = probe_file(input_filename);
972     probe_footer();
973     avio_flush(probe_out);
974     avio_close(probe_out);
975
976     avformat_network_deinit();
977
978     return ret;
979 }