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