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