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