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