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