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