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