avconv: Add an option for automatically rotating video according to display matrix
[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
636ced8e 63static void avprobe_cleanup(int ret)
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
a4baf96a 85} PrintElementType;
3a8c95f7 86
bf704132 87typedef struct PrintElement {
3a8c95f7 88 const char *name;
a4baf96a 89 PrintElementType type;
3a8c95f7
LB
90 int64_t index;
91 int64_t nb_elems;
a4baf96a 92} PrintElement;
3a8c95f7 93
bf704132 94typedef struct PrintContext {
a4baf96a 95 PrintElement *prefix;
3a8c95f7 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);
a4baf96a 107} PrintContext;
3a8c95f7
LB
108
109static AVIOContext *probe_out = NULL;
a4baf96a 110static PrintContext 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;
a4baf96a 169 PrintElement *el = octx.prefix + octx.level -1;
3a8c95f7
LB
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);
aeb23fc4 184 if (el->type == ARRAY)
3a8c95f7
LB
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);
b1306823
VG
305 if (!str)
306 return;
8271f55b 307 while (*p) {
efa7f420 308 *p = av_toupper(*p);
8271f55b
AK
309 p++;
310 }
311
312 avio_printf(probe_out, "[%s]\n", str);
313 av_freep(&str);
314}
315
316static 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);
b1306823
VG
324 if (!str)
325 return;
8271f55b 326 while (*p) {
efa7f420 327 *p = av_toupper(*p);
8271f55b
AK
328 p++;
329 }
330
331 avio_printf(probe_out, "[/%s]\n", str);
332 av_freep(&str);
333}
334
335static 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/*
3a8c95f7
LB
343 * Simple Formatter for single entries.
344 */
345
346static 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
355static 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
3a8c95f7
LB
364static void probe_group_enter(const char *name, int type)
365{
366 int64_t count = -1;
367
368 octx.prefix =
a4baf96a 369 av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
3a8c95f7
LB
370
371 if (!octx.prefix || !name) {
372 fprintf(stderr, "Out of memory\n");
636ced8e 373 exit_program(1);
3a8c95f7
LB
374 }
375
376 if (octx.level) {
a4baf96a 377 PrintElement *parent = octx.prefix + octx.level -1;
3a8c95f7
LB
378 if (parent->type == ARRAY)
379 count = parent->nb_elems;
380 parent->nb_elems++;
381 }
382
a4baf96a 383 octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
3a8c95f7
LB
384}
385
386static void probe_group_leave(void)
387{
388 --octx.level;
389}
390
391static void probe_header(void)
392{
52899558
LB
393 if (octx.print_header)
394 octx.print_header();
3a8c95f7
LB
395 probe_group_enter("root", OBJECT);
396}
397
398static void probe_footer(void)
399{
52899558
LB
400 if (octx.print_footer)
401 octx.print_footer();
3a8c95f7
LB
402 probe_group_leave();
403}
404
405
406static void probe_array_header(const char *name)
407{
52899558
LB
408 if (octx.print_array_header)
409 octx.print_array_header(name);
3a8c95f7
LB
410
411 probe_group_enter(name, ARRAY);
412}
413
414static void probe_array_footer(const char *name)
415{
416 probe_group_leave();
52899558
LB
417 if (octx.print_array_footer)
418 octx.print_array_footer(name);
3a8c95f7
LB
419}
420
421static void probe_object_header(const char *name)
422{
52899558
LB
423 if (octx.print_object_header)
424 octx.print_object_header(name);
3a8c95f7
LB
425
426 probe_group_enter(name, OBJECT);
427}
428
429static void probe_object_footer(const char *name)
430{
431 probe_group_leave();
52899558
LB
432 if (octx.print_object_footer)
433 octx.print_object_footer(name);
3a8c95f7
LB
434}
435
436static void probe_int(const char *key, int64_t value)
437{
52899558 438 octx.print_integer(key, value);
3a8c95f7
LB
439 octx.prefix[octx.level -1].nb_elems++;
440}
441
442static void probe_str(const char *key, const char *value)
443{
52899558 444 octx.print_string(key, value);
3a8c95f7
LB
445 octx.prefix[octx.level -1].nb_elems++;
446}
447
448static 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
336ce917
SS
460static 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) {
d752509b 476 index = (int) log2(val) / 10;
0184bbe2
GN
477 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
478 val /= pow(2, index * 10);
336ce917
SS
479 prefix_string = binary_unit_prefixes[index];
480 } else {
481 index = (int) (log10(val)) / 3;
0184bbe2
GN
482 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
483 val /= pow(10, index * 3);
336ce917
SS
484 prefix_string = decimal_unit_prefixes[index];
485 }
beb33fca
LB
486 snprintf(buf, buf_size, "%.*f%s%s",
487 index ? 3 : 0, val,
488 prefix_string,
0184bbe2 489 show_value_unit ? unit : "");
336ce917 490 } else {
beb33fca 491 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
336ce917
SS
492 }
493
494 return buf;
495}
496
0184bbe2
GN
497static char *time_value_string(char *buf, int buf_size, int64_t val,
498 const AVRational *time_base)
336ce917
SS
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
3a8c95f7 509static char *ts_value_string(char *buf, int buf_size, int64_t ts)
530bbe96
SS
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
3a8c95f7
LB
520static 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
527static char *tag_string(char *buf, int buf_size, int tag)
528{
529 snprintf(buf, buf_size, "0x%04x", tag);
530 return buf;
531}
532
530bbe96
SS
533static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
534{
535 char val_str[128];
536 AVStream *st = fmt_ctx->streams[pkt->stream_index];
537
3a8c95f7
LB
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");
530bbe96
SS
557}
558
559static void show_packets(AVFormatContext *fmt_ctx)
560{
561 AVPacket pkt;
562
563 av_init_packet(&pkt);
3a8c95f7 564 probe_array_header("packets");
530bbe96
SS
565 while (!av_read_frame(fmt_ctx, &pkt))
566 show_packet(fmt_ctx, &pkt);
3a8c95f7 567 probe_array_footer("packets");
530bbe96
SS
568}
569
336ce917
SS
570static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
571{
572 AVStream *stream = fmt_ctx->streams[stream_idx];
573 AVCodecContext *dec_ctx;
8b134457 574 const AVCodec *dec;
b1d22dc5 575 const char *profile;
336ce917 576 char val_str[128];
dda20a6e 577 AVRational display_aspect_ratio, *sar = NULL;
b7f1010c 578 const AVPixFmtDescriptor *desc;
336ce917 579
3a8c95f7 580 probe_object_header("stream");
336ce917 581
3a8c95f7 582 probe_int("index", stream->index);
336ce917
SS
583
584 if ((dec_ctx = stream->codec)) {
585 if ((dec = dec_ctx->codec)) {
3a8c95f7
LB
586 probe_str("codec_name", dec->name);
587 probe_str("codec_long_name", dec->long_name);
336ce917 588 } else {
3a8c95f7 589 probe_str("codec_name", "unknown");
336ce917
SS
590 }
591
3a8c95f7
LB
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));
336ce917
SS
596
597 /* print AVI/FourCC tag */
7e566bbe 598 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
3a8c95f7
LB
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));
336ce917 602
b1d22dc5
CS
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
336ce917 607 switch (dec_ctx->codec_type) {
72415b2a 608 case AVMEDIA_TYPE_VIDEO:
3a8c95f7
LB
609 probe_int("width", dec_ctx->width);
610 probe_int("height", dec_ctx->height);
7b9cb7b3
VG
611 probe_int("coded_width", dec_ctx->coded_width);
612 probe_int("coded_height", dec_ctx->coded_height);
3a8c95f7 613 probe_int("has_b_frames", dec_ctx->has_b_frames);
dda20a6e
AK
614 if (dec_ctx->sample_aspect_ratio.num)
615 sar = &dec_ctx->sample_aspect_ratio;
616 else if (stream->sample_aspect_ratio.num)
617 sar = &stream->sample_aspect_ratio;
618
619 if (sar) {
3a8c95f7 620 probe_str("sample_aspect_ratio",
dda20a6e 621 rational_string(val_str, sizeof(val_str), ":", sar));
441881b4 622 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
dda20a6e 623 dec_ctx->width * sar->num, dec_ctx->height * sar->den,
441881b4 624 1024*1024);
3a8c95f7
LB
625 probe_str("display_aspect_ratio",
626 rational_string(val_str, sizeof(val_str), ":",
627 &display_aspect_ratio));
8280e2bd 628 }
b7f1010c
AK
629 desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
630 probe_str("pix_fmt", desc ? desc->name : "unknown");
3a8c95f7 631 probe_int("level", dec_ctx->level);
a2c00d22
VG
632
633 probe_str("color_range", av_color_range_name(dec_ctx->color_range));
634 probe_str("color_space", av_color_space_name(dec_ctx->colorspace));
635 probe_str("color_trc", av_color_transfer_name(dec_ctx->color_trc));
636 probe_str("color_pri", av_color_primaries_name(dec_ctx->color_primaries));
637 probe_str("chroma_loc", av_chroma_location_name(dec_ctx->chroma_sample_location));
336ce917
SS
638 break;
639
72415b2a 640 case AVMEDIA_TYPE_AUDIO:
3a8c95f7
LB
641 probe_str("sample_rate",
642 value_string(val_str, sizeof(val_str),
643 dec_ctx->sample_rate,
644 unit_hertz_str));
645 probe_int("channels", dec_ctx->channels);
646 probe_int("bits_per_sample",
647 av_get_bits_per_sample(dec_ctx->codec_id));
336ce917
SS
648 break;
649 }
650 } else {
3a8c95f7 651 probe_str("codec_type", "unknown");
336ce917
SS
652 }
653
654 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
3a8c95f7 655 probe_int("id", stream->id);
3a8c95f7
LB
656 probe_str("avg_frame_rate",
657 rational_string(val_str, sizeof(val_str), "/",
658 &stream->avg_frame_rate));
89a4465b
JG
659 if (dec_ctx->bit_rate)
660 probe_str("bit_rate",
661 value_string(val_str, sizeof(val_str),
662 dec_ctx->bit_rate, unit_bit_per_second_str));
3a8c95f7
LB
663 probe_str("time_base",
664 rational_string(val_str, sizeof(val_str), "/",
665 &stream->time_base));
666 probe_str("start_time",
667 time_value_string(val_str, sizeof(val_str),
668 stream->start_time, &stream->time_base));
669 probe_str("duration",
670 time_value_string(val_str, sizeof(val_str),
671 stream->duration, &stream->time_base));
0a6a282a 672 if (stream->nb_frames)
3a8c95f7 673 probe_int("nb_frames", stream->nb_frames);
336ce917 674
3a8c95f7 675 probe_dict(stream->metadata, "tags");
336ce917 676
3a8c95f7 677 probe_object_footer("stream");
08354bf4
RB
678}
679
336ce917
SS
680static void show_format(AVFormatContext *fmt_ctx)
681{
336ce917 682 char val_str[128];
136ee32d 683 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
336ce917 684
3a8c95f7
LB
685 probe_object_header("format");
686 probe_str("filename", fmt_ctx->filename);
687 probe_int("nb_streams", fmt_ctx->nb_streams);
688 probe_str("format_name", fmt_ctx->iformat->name);
689 probe_str("format_long_name", fmt_ctx->iformat->long_name);
690 probe_str("start_time",
08354bf4
RB
691 time_value_string(val_str, sizeof(val_str),
692 fmt_ctx->start_time, &AV_TIME_BASE_Q));
3a8c95f7 693 probe_str("duration",
08354bf4
RB
694 time_value_string(val_str, sizeof(val_str),
695 fmt_ctx->duration, &AV_TIME_BASE_Q));
3a8c95f7 696 probe_str("size",
08354bf4
RB
697 size >= 0 ? value_string(val_str, sizeof(val_str),
698 size, unit_byte_str)
0184bbe2 699 : "unknown");
3a8c95f7 700 probe_str("bit_rate",
08354bf4
RB
701 value_string(val_str, sizeof(val_str),
702 fmt_ctx->bit_rate, unit_bit_per_second_str));
0184bbe2 703
3a8c95f7 704 probe_dict(fmt_ctx->metadata, "tags");
336ce917 705
3a8c95f7 706 probe_object_footer("format");
336ce917
SS
707}
708
709static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
710{
711 int err, i;
e0518705
AK
712 AVFormatContext *fmt_ctx = NULL;
713 AVDictionaryEntry *t;
336ce917 714
0184bbe2
GN
715 if ((err = avformat_open_input(&fmt_ctx, filename,
716 iformat, &format_opts)) < 0) {
336ce917
SS
717 print_error(filename, err);
718 return err;
719 }
e0518705
AK
720 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
721 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
722 return AVERROR_OPTION_NOT_FOUND;
723 }
724
336ce917
SS
725
726 /* fill the streams in the format context */
c960e67a 727 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
336ce917
SS
728 print_error(filename, err);
729 return err;
730 }
731
610219a5 732 av_dump_format(fmt_ctx, 0, filename, 0);
336ce917
SS
733
734 /* bind a decoder to each input stream */
735 for (i = 0; i < fmt_ctx->nb_streams; i++) {
736 AVStream *stream = fmt_ctx->streams[i];
737 AVCodec *codec;
738
36ef5369 739 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
503f2feb
AC
740 fprintf(stderr, "Failed to probe codec for input stream %d\n",
741 stream->index);
742 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
0184bbe2
GN
743 fprintf(stderr,
744 "Unsupported codec with id %d for input stream %d\n",
336ce917 745 stream->codec->codec_id, stream->index);
0b950fe2 746 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
336ce917
SS
747 fprintf(stderr, "Error while opening codec for input stream %d\n",
748 stream->index);
749 }
750 }
751
752 *fmt_ctx_ptr = fmt_ctx;
753 return 0;
754}
755
093c50a4
JG
756static void close_input_file(AVFormatContext **ctx_ptr)
757{
758 int i;
759 AVFormatContext *fmt_ctx = *ctx_ptr;
760
761 /* close decoder for each stream */
762 for (i = 0; i < fmt_ctx->nb_streams; i++) {
763 AVStream *stream = fmt_ctx->streams[i];
764
765 avcodec_close(stream->codec);
766 }
767 avformat_close_input(ctx_ptr);
768}
769
336ce917
SS
770static int probe_file(const char *filename)
771{
772 AVFormatContext *fmt_ctx;
773 int ret, i;
774
775 if ((ret = open_input_file(&fmt_ctx, filename)))
776 return ret;
777
3a8c95f7
LB
778 if (do_show_format)
779 show_format(fmt_ctx);
530bbe96 780
3a8c95f7
LB
781 if (do_show_streams) {
782 probe_array_header("streams");
336ce917
SS
783 for (i = 0; i < fmt_ctx->nb_streams; i++)
784 show_stream(fmt_ctx, i);
3a8c95f7
LB
785 probe_array_footer("streams");
786 }
336ce917 787
3a8c95f7
LB
788 if (do_show_packets)
789 show_packets(fmt_ctx);
336ce917 790
093c50a4 791 close_input_file(&fmt_ctx);
336ce917
SS
792 return 0;
793}
794
795static void show_usage(void)
796{
797 printf("Simple multimedia streams analyzer\n");
9e12f0bf 798 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
336ce917
SS
799 printf("\n");
800}
801
11d957fb 802static int opt_format(void *optctx, const char *opt, const char *arg)
1be784a2
SS
803{
804 iformat = av_find_input_format(arg);
805 if (!iformat) {
806 fprintf(stderr, "Unknown input format: %s\n", arg);
26513856 807 return AVERROR(EINVAL);
1be784a2 808 }
26513856 809 return 0;
1be784a2
SS
810}
811
11d957fb 812static int opt_output_format(void *optctx, const char *opt, const char *arg)
0d242a77
LB
813{
814
815 if (!strcmp(arg, "json")) {
52899558
LB
816 octx.print_header = json_print_header;
817 octx.print_footer = json_print_footer;
818 octx.print_array_header = json_print_array_header;
819 octx.print_array_footer = json_print_array_footer;
820 octx.print_object_header = json_print_object_header;
821 octx.print_object_footer = json_print_object_footer;
822
823 octx.print_integer = json_print_integer;
824 octx.print_string = json_print_string;
d30acfa9 825 } else if (!strcmp(arg, "ini")) {
52899558
LB
826 octx.print_header = ini_print_header;
827 octx.print_footer = ini_print_footer;
828 octx.print_array_header = ini_print_array_header;
829 octx.print_object_header = ini_print_object_header;
0d242a77 830
52899558
LB
831 octx.print_integer = ini_print_integer;
832 octx.print_string = ini_print_string;
8271f55b 833 } else if (!strcmp(arg, "old")) {
52899558
LB
834 octx.print_header = NULL;
835 octx.print_object_header = old_print_object_header;
836 octx.print_object_footer = old_print_object_footer;
8271f55b 837
52899558 838 octx.print_string = old_print_string;
0d242a77
LB
839 } else {
840 av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
70026be8 841 return AVERROR(EINVAL);
0d242a77 842 }
70026be8 843 return 0;
0d242a77
LB
844}
845
11d957fb 846static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
08354bf4
RB
847{
848 do_show_format = 1;
d8947523 849 nb_fmt_entries_to_show++;
52899558
LB
850 octx.print_header = NULL;
851 octx.print_footer = NULL;
852 octx.print_array_header = NULL;
853 octx.print_array_footer = NULL;
854 octx.print_object_header = NULL;
855 octx.print_object_footer = NULL;
856
857 octx.print_integer = show_format_entry_integer;
858 octx.print_string = show_format_entry_string;
08354bf4
RB
859 av_dict_set(&fmt_entries_to_show, arg, "", 0);
860 return 0;
861}
862
7cc8d638 863static void opt_input_file(void *optctx, const char *arg)
336ce917 864{
c8c0ac6b 865 if (input_filename) {
0184bbe2
GN
866 fprintf(stderr,
867 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
58621b8d 868 arg, input_filename);
636ced8e 869 exit_program(1);
c8c0ac6b 870 }
912dd63e
SS
871 if (!strcmp(arg, "-"))
872 arg = "pipe:";
873 input_filename = arg;
336ce917
SS
874}
875
a3ad68d3 876void show_help_default(const char *opt, const char *arg)
336ce917 877{
6afd569e 878 av_log_set_callback(log_callback_help);
336ce917 879 show_usage();
f9fada27 880 show_help_options(options, "Main options:", 0, 0, 0);
336ce917 881 printf("\n");
7a6cd995 882 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
336ce917
SS
883}
884
11d957fb 885static int opt_pretty(void *optctx, const char *opt, const char *arg)
336ce917
SS
886{
887 show_value_unit = 1;
888 use_value_prefix = 1;
889 use_byte_value_binary_prefix = 1;
890 use_value_sexagesimal_format = 1;
b42be2d5 891 return 0;
336ce917
SS
892}
893
dcb7ef54 894static const OptionDef real_options[] = {
336ce917 895#include "cmdutils_common_opts.h"
b42be2d5
MS
896 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
897 { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
898 { "unit", OPT_BOOL, {&show_value_unit},
0184bbe2 899 "show unit of the displayed values" },
b42be2d5 900 { "prefix", OPT_BOOL, {&use_value_prefix},
0184bbe2 901 "use SI prefixes for the displayed values" },
b42be2d5 902 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
336ce917 903 "use binary prefixes for byte units" },
b42be2d5 904 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
336ce917 905 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
b42be2d5 906 { "pretty", 0, {.func_arg = opt_pretty},
336ce917 907 "prettify the format of displayed values, make it more human readable" },
b42be2d5
MS
908 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
909 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
08354bf4 910 "show a particular entry from the format/container info", "entry" },
b42be2d5
MS
911 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
912 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
913 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
0184bbe2 914 "generic catch all option", "" },
336ce917
SS
915 { NULL, },
916};
917
3a8c95f7
LB
918static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
919{
920 printf("%.*s", buf_size, buf);
921 return 0;
922}
923
924#define AVP_BUFFSIZE 4096
925
336ce917
SS
926int main(int argc, char **argv)
927{
6afd569e 928 int ret;
3a8c95f7
LB
929 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
930
931 if (!buffer)
932 exit(1);
6afd569e 933
636ced8e 934 register_exit(avprobe_cleanup);
5e3f9979 935
dcb7ef54 936 options = real_options;
182cbe43 937 parse_loglevel(argc, argv, options);
336ce917 938 av_register_all();
776f2bb9 939 avformat_network_init();
c558122e 940 init_opts();
6ce98ea4
SS
941#if CONFIG_AVDEVICE
942 avdevice_register_all();
943#endif
336ce917
SS
944
945 show_banner();
43b50e62
LB
946
947 octx.print_header = ini_print_header;
948 octx.print_footer = ini_print_footer;
949
950 octx.print_array_header = ini_print_array_header;
951 octx.print_object_header = ini_print_object_header;
952
953 octx.print_integer = ini_print_integer;
954 octx.print_string = ini_print_string;
955
7cc8d638 956 parse_options(NULL, argc, argv, options, opt_input_file);
336ce917
SS
957
958 if (!input_filename) {
959 show_usage();
960 fprintf(stderr, "You have to specify one input file.\n");
0184bbe2
GN
961 fprintf(stderr,
962 "Use -h to get full help or, even better, run 'man %s'.\n",
963 program_name);
636ced8e 964 exit_program(1);
336ce917
SS
965 }
966
3a8c95f7
LB
967 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
968 probe_buf_write, NULL);
969 if (!probe_out)
636ced8e 970 exit_program(1);
6afd569e 971
3a8c95f7
LB
972 probe_header();
973 ret = probe_file(input_filename);
974 probe_footer();
975 avio_flush(probe_out);
976 avio_close(probe_out);
31f2c023 977
776f2bb9
MS
978 avformat_network_deinit();
979
6afd569e 980 return ret;
336ce917 981}