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