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