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