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