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