avprobe: output proper INI format
[libav.git] / avprobe.c
CommitLineData
336ce917 1/*
9e12f0bf 2 * avprobe : Simple Media Prober based on the Libav libraries
336ce917
SS
3 * Copyright (c) 2007-2010 Stefano Sabatini
4 *
2912e87a 5 * This file is part of Libav.
336ce917 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
336ce917
SS
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
336ce917
SS
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
336ce917
SS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
6ce98ea4
SS
22#include "config.h"
23
336ce917
SS
24#include "libavformat/avformat.h"
25#include "libavcodec/avcodec.h"
41d0eb1c 26#include "libavutil/opt.h"
336ce917 27#include "libavutil/pixdesc.h"
d2d67e42 28#include "libavutil/dict.h"
6ce98ea4 29#include "libavdevice/avdevice.h"
336ce917
SS
30#include "cmdutils.h"
31
9e12f0bf 32const char program_name[] = "avprobe";
336ce917
SS
33const int program_birth_year = 2007;
34
35static int do_show_format = 0;
08354bf4 36static AVDictionary *fmt_entries_to_show = NULL;
d8947523 37static int nb_fmt_entries_to_show;
530bbe96 38static int do_show_packets = 0;
336ce917
SS
39static int do_show_streams = 0;
40
41static int show_value_unit = 0;
42static int use_value_prefix = 0;
43static int use_byte_value_binary_prefix = 0;
44static int use_value_sexagesimal_format = 0;
45
46/* globals */
47static const OptionDef options[];
48
9e12f0bf 49/* AVprobe context */
336ce917 50static const char *input_filename;
1be784a2 51static AVInputFormat *iformat = NULL;
336ce917 52
b0f29db5
AC
53static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
54static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
336ce917 55
b0f29db5
AC
56static const char unit_second_str[] = "s" ;
57static const char unit_hertz_str[] = "Hz" ;
58static const char unit_byte_str[] = "byte" ;
59static const char unit_bit_per_second_str[] = "bit/s";
336ce917 60
dad09ff9
AK
61void exit_program(int ret)
62{
08354bf4 63 av_dict_free(&fmt_entries_to_show);
dad09ff9
AK
64 exit(ret);
65}
66
3a8c95f7
LB
67/*
68 * The output is structured in array and objects that might contain items
69 * Array could require the objects within to not be named.
70 * Object could require the items within to be named.
71 *
72 * For flat representation the name of each section is saved on prefix so it
73 * can be rendered in order to represent nested structures (e.g. array of
74 * objects for the packets list).
75 *
76 * Within an array each element can need an unique identifier or an index.
77 *
78 * Nesting level is accounted separately.
79 */
80
81typedef enum {
82 ARRAY,
83 OBJECT
84} ProbeElementType;
85
86typedef struct {
87 const char *name;
88 ProbeElementType type;
89 int64_t index;
90 int64_t nb_elems;
91} ProbeElement;
92
93typedef struct {
94 ProbeElement *prefix;
95 int level;
96} OutputContext;
97
98static AVIOContext *probe_out = NULL;
99static OutputContext octx;
100
101/*
102 * Default format, INI
103 *
104 * - all key and values are utf8
105 * - '.' is the subgroup separator
106 * - newlines and the following characters are escaped
107 * - '\' is the escape character
108 * - '#' is the comment
109 * - '=' is the key/value separators
110 * - ':' is not used but usually parsed as key/value separator
111 */
112
113static void ini_print_header(void)
114{
115 avio_printf(probe_out, "# avprobe output\n\n");
116}
117static void ini_print_footer(void)
118{
119 avio_w8(probe_out, '\n');
120}
121
122static void ini_escape_print(const char *s)
123{
124 int i = 0;
125 char c = 0;
126
127 while (c = s[i++]) {
128 switch (c) {
129 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
130 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
131 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
132 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
133 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
134 case '\\':
135 case '#' :
136 case '=' :
137 case ':' : avio_w8(probe_out, '\\');
138 default:
139 if ((unsigned char)c < 32)
140 avio_printf(probe_out, "\\x00%02x", c & 0xff);
141 else
142 avio_w8(probe_out, c);
143 break;
144 }
145 }
146}
147
148static void ini_print_array_header(const char *name)
149{
150 if (octx.prefix[octx.level -1].nb_elems)
151 avio_printf(probe_out, "\n");
152}
153
154static void ini_print_object_header(const char *name)
155{
156 int i;
157 ProbeElement *el = octx.prefix + octx.level -1;
158
159 if (el->nb_elems)
160 avio_printf(probe_out, "\n");
161
162 avio_printf(probe_out, "[");
163
164 for (i = 1; i < octx.level; i++) {
165 el = octx.prefix + i;
166 avio_printf(probe_out, "%s.", el->name);
167 if (el->index >= 0)
168 avio_printf(probe_out, "%"PRId64".", el->index);
169 }
170
171 avio_printf(probe_out, "%s", name);
172 if (el && el->type == ARRAY)
173 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
174 avio_printf(probe_out, "]\n");
175}
176
177static void ini_print_integer(const char *key, int64_t value)
178{
179 ini_escape_print(key);
180 avio_printf(probe_out, "=%"PRId64"\n", value);
181}
182
183
184static void ini_print_string(const char *key, const char *value)
185{
186 ini_escape_print(key);
187 avio_printf(probe_out, "=");
188 ini_escape_print(value);
189 avio_w8(probe_out, '\n');
190}
191
192/*
193 * Simple Formatter for single entries.
194 */
195
196static void show_format_entry_integer(const char *key, int64_t value)
197{
198 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
199 if (nb_fmt_entries_to_show > 1)
200 avio_printf(probe_out, "%s=", key);
201 avio_printf(probe_out, "%"PRId64"\n", value);
202 }
203}
204
205static void show_format_entry_string(const char *key, const char *value)
206{
207 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
208 if (nb_fmt_entries_to_show > 1)
209 avio_printf(probe_out, "%s=", key);
210 avio_printf(probe_out, "%s\n", value);
211 }
212}
213
214
215void (*print_header)(void) = ini_print_header;
216void (*print_footer)(void) = ini_print_footer;
217
218void (*print_array_header) (const char *name) = ini_print_array_header;
219void (*print_array_footer) (const char *name);
220void (*print_object_header)(const char *name) = ini_print_object_header;
221void (*print_object_footer)(const char *name);
222
223void (*print_integer) (const char *key, int64_t value) = ini_print_integer;
224void (*print_string) (const char *key, const char *value) = ini_print_string;
225
226
227static void probe_group_enter(const char *name, int type)
228{
229 int64_t count = -1;
230
231 octx.prefix =
232 av_realloc(octx.prefix, sizeof(ProbeElement) * (octx.level + 1));
233
234 if (!octx.prefix || !name) {
235 fprintf(stderr, "Out of memory\n");
236 exit(1);
237 }
238
239 if (octx.level) {
240 ProbeElement *parent = octx.prefix + octx.level -1;
241 if (parent->type == ARRAY)
242 count = parent->nb_elems;
243 parent->nb_elems++;
244 }
245
246 octx.prefix[octx.level++] = (ProbeElement){name, type, count, 0};
247}
248
249static void probe_group_leave(void)
250{
251 --octx.level;
252}
253
254static void probe_header(void)
255{
256 if (print_header)
257 print_header();
258 probe_group_enter("root", OBJECT);
259}
260
261static void probe_footer(void)
262{
263 if (print_footer)
264 print_footer();
265 probe_group_leave();
266}
267
268
269static void probe_array_header(const char *name)
270{
271 if (print_array_header)
272 print_array_header(name);
273
274 probe_group_enter(name, ARRAY);
275}
276
277static void probe_array_footer(const char *name)
278{
279 probe_group_leave();
280 if (print_array_footer)
281 print_array_footer(name);
282}
283
284static void probe_object_header(const char *name)
285{
286 if (print_object_header)
287 print_object_header(name);
288
289 probe_group_enter(name, OBJECT);
290}
291
292static void probe_object_footer(const char *name)
293{
294 probe_group_leave();
295 if (print_object_footer)
296 print_object_footer(name);
297}
298
299static void probe_int(const char *key, int64_t value)
300{
301 print_integer(key, value);
302 octx.prefix[octx.level -1].nb_elems++;
303}
304
305static void probe_str(const char *key, const char *value)
306{
307 print_string(key, value);
308 octx.prefix[octx.level -1].nb_elems++;
309}
310
311static void probe_dict(AVDictionary *dict, const char *name)
312{
313 AVDictionaryEntry *entry = NULL;
314 if (!dict)
315 return;
316 probe_object_header(name);
317 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
318 probe_str(entry->key, entry->value);
319 }
320 probe_object_footer(name);
321}
322
336ce917
SS
323static char *value_string(char *buf, int buf_size, double val, const char *unit)
324{
325 if (unit == unit_second_str && use_value_sexagesimal_format) {
326 double secs;
327 int hours, mins;
328 secs = val;
329 mins = (int)secs / 60;
330 secs = secs - mins * 60;
331 hours = mins / 60;
332 mins %= 60;
333 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
334 } else if (use_value_prefix) {
335 const char *prefix_string;
336 int index;
337
338 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
f1b267dd 339 index = (int) (log(val)/log(2)) / 10;
0184bbe2
GN
340 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
341 val /= pow(2, index * 10);
336ce917
SS
342 prefix_string = binary_unit_prefixes[index];
343 } else {
344 index = (int) (log10(val)) / 3;
0184bbe2
GN
345 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
346 val /= pow(10, index * 3);
336ce917
SS
347 prefix_string = decimal_unit_prefixes[index];
348 }
beb33fca
LB
349 snprintf(buf, buf_size, "%.*f%s%s",
350 index ? 3 : 0, val,
351 prefix_string,
0184bbe2 352 show_value_unit ? unit : "");
336ce917 353 } else {
beb33fca 354 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
336ce917
SS
355 }
356
357 return buf;
358}
359
0184bbe2
GN
360static char *time_value_string(char *buf, int buf_size, int64_t val,
361 const AVRational *time_base)
336ce917
SS
362{
363 if (val == AV_NOPTS_VALUE) {
364 snprintf(buf, buf_size, "N/A");
365 } else {
366 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
367 }
368
369 return buf;
370}
371
3a8c95f7 372static char *ts_value_string(char *buf, int buf_size, int64_t ts)
530bbe96
SS
373{
374 if (ts == AV_NOPTS_VALUE) {
375 snprintf(buf, buf_size, "N/A");
376 } else {
377 snprintf(buf, buf_size, "%"PRId64, ts);
378 }
379
380 return buf;
381}
382
3a8c95f7
LB
383static char *rational_string(char *buf, int buf_size, const char *sep,
384 const AVRational *rat)
385{
386 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
387 return buf;
388}
389
390static char *tag_string(char *buf, int buf_size, int tag)
391{
392 snprintf(buf, buf_size, "0x%04x", tag);
393 return buf;
394}
395
396
397
72415b2a 398static const char *media_type_string(enum AVMediaType media_type)
336ce917 399{
72415b2a
SS
400 switch (media_type) {
401 case AVMEDIA_TYPE_VIDEO: return "video";
402 case AVMEDIA_TYPE_AUDIO: return "audio";
403 case AVMEDIA_TYPE_DATA: return "data";
404 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
405 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
336ce917
SS
406 default: return "unknown";
407 }
408}
409
530bbe96
SS
410static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
411{
412 char val_str[128];
413 AVStream *st = fmt_ctx->streams[pkt->stream_index];
414
3a8c95f7
LB
415 probe_object_header("packet");
416 probe_str("codec_type", media_type_string(st->codec->codec_type));
417 probe_int("stream_index", pkt->stream_index);
418 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
419 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
420 pkt->pts, &st->time_base));
421 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
422 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
423 pkt->dts, &st->time_base));
424 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
425 pkt->duration));
426 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
427 pkt->duration,
428 &st->time_base));
429 probe_str("size", value_string(val_str, sizeof(val_str),
430 pkt->size, unit_byte_str));
431 probe_int("pos", pkt->pos);
432 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
433 probe_object_footer("packet");
530bbe96
SS
434}
435
436static void show_packets(AVFormatContext *fmt_ctx)
437{
438 AVPacket pkt;
439
440 av_init_packet(&pkt);
3a8c95f7 441 probe_array_header("packets");
530bbe96
SS
442 while (!av_read_frame(fmt_ctx, &pkt))
443 show_packet(fmt_ctx, &pkt);
3a8c95f7 444 probe_array_footer("packets");
530bbe96
SS
445}
446
336ce917
SS
447static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
448{
449 AVStream *stream = fmt_ctx->streams[stream_idx];
450 AVCodecContext *dec_ctx;
451 AVCodec *dec;
452 char val_str[128];
d21b2278 453 AVRational display_aspect_ratio;
336ce917 454
3a8c95f7 455 probe_object_header("stream");
336ce917 456
3a8c95f7 457 probe_int("index", stream->index);
336ce917
SS
458
459 if ((dec_ctx = stream->codec)) {
460 if ((dec = dec_ctx->codec)) {
3a8c95f7
LB
461 probe_str("codec_name", dec->name);
462 probe_str("codec_long_name", dec->long_name);
336ce917 463 } else {
3a8c95f7 464 probe_str("codec_name", "unknown");
336ce917
SS
465 }
466
3a8c95f7
LB
467 probe_str("codec_type", media_type_string(dec_ctx->codec_type));
468 probe_str("codec_time_base",
469 rational_string(val_str, sizeof(val_str),
470 "/", &dec_ctx->time_base));
336ce917
SS
471
472 /* print AVI/FourCC tag */
7e566bbe 473 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
3a8c95f7
LB
474 probe_str("codec_tag_string", val_str);
475 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
476 dec_ctx->codec_tag));
336ce917
SS
477
478 switch (dec_ctx->codec_type) {
72415b2a 479 case AVMEDIA_TYPE_VIDEO:
3a8c95f7
LB
480 probe_int("width", dec_ctx->width);
481 probe_int("height", dec_ctx->height);
482 probe_int("has_b_frames", dec_ctx->has_b_frames);
8280e2bd 483 if (dec_ctx->sample_aspect_ratio.num) {
3a8c95f7
LB
484 probe_str("sample_aspect_ratio",
485 rational_string(val_str, sizeof(val_str), ":",
486 &dec_ctx->sample_aspect_ratio));
441881b4
SS
487 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
488 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
489 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
490 1024*1024);
3a8c95f7
LB
491 probe_str("display_aspect_ratio",
492 rational_string(val_str, sizeof(val_str), ":",
493 &display_aspect_ratio));
8280e2bd 494 }
3a8c95f7
LB
495 probe_str("pix_fmt",
496 dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name
0184bbe2 497 : "unknown");
3a8c95f7 498 probe_int("level", dec_ctx->level);
336ce917
SS
499 break;
500
72415b2a 501 case AVMEDIA_TYPE_AUDIO:
3a8c95f7
LB
502 probe_str("sample_rate",
503 value_string(val_str, sizeof(val_str),
504 dec_ctx->sample_rate,
505 unit_hertz_str));
506 probe_int("channels", dec_ctx->channels);
507 probe_int("bits_per_sample",
508 av_get_bits_per_sample(dec_ctx->codec_id));
336ce917
SS
509 break;
510 }
511 } else {
3a8c95f7 512 probe_str("codec_type", "unknown");
336ce917
SS
513 }
514
515 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
3a8c95f7
LB
516 probe_int("id", stream->id);
517 probe_str("r_frame_rate",
518 rational_string(val_str, sizeof(val_str), "/",
519 &stream->r_frame_rate));
520 probe_str("avg_frame_rate",
521 rational_string(val_str, sizeof(val_str), "/",
522 &stream->avg_frame_rate));
523 probe_str("time_base",
524 rational_string(val_str, sizeof(val_str), "/",
525 &stream->time_base));
526 probe_str("start_time",
527 time_value_string(val_str, sizeof(val_str),
528 stream->start_time, &stream->time_base));
529 probe_str("duration",
530 time_value_string(val_str, sizeof(val_str),
531 stream->duration, &stream->time_base));
0a6a282a 532 if (stream->nb_frames)
3a8c95f7 533 probe_int("nb_frames", stream->nb_frames);
336ce917 534
3a8c95f7 535 probe_dict(stream->metadata, "tags");
336ce917 536
3a8c95f7 537 probe_object_footer("stream");
08354bf4
RB
538}
539
336ce917
SS
540static void show_format(AVFormatContext *fmt_ctx)
541{
336ce917 542 char val_str[128];
136ee32d 543 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
336ce917 544
3a8c95f7
LB
545 probe_object_header("format");
546 probe_str("filename", fmt_ctx->filename);
547 probe_int("nb_streams", fmt_ctx->nb_streams);
548 probe_str("format_name", fmt_ctx->iformat->name);
549 probe_str("format_long_name", fmt_ctx->iformat->long_name);
550 probe_str("start_time",
08354bf4
RB
551 time_value_string(val_str, sizeof(val_str),
552 fmt_ctx->start_time, &AV_TIME_BASE_Q));
3a8c95f7 553 probe_str("duration",
08354bf4
RB
554 time_value_string(val_str, sizeof(val_str),
555 fmt_ctx->duration, &AV_TIME_BASE_Q));
3a8c95f7 556 probe_str("size",
08354bf4
RB
557 size >= 0 ? value_string(val_str, sizeof(val_str),
558 size, unit_byte_str)
0184bbe2 559 : "unknown");
3a8c95f7 560 probe_str("bit_rate",
08354bf4
RB
561 value_string(val_str, sizeof(val_str),
562 fmt_ctx->bit_rate, unit_bit_per_second_str));
0184bbe2 563
3a8c95f7 564 probe_dict(fmt_ctx->metadata, "tags");
336ce917 565
3a8c95f7 566 probe_object_footer("format");
336ce917
SS
567}
568
569static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
570{
571 int err, i;
e0518705
AK
572 AVFormatContext *fmt_ctx = NULL;
573 AVDictionaryEntry *t;
336ce917 574
0184bbe2
GN
575 if ((err = avformat_open_input(&fmt_ctx, filename,
576 iformat, &format_opts)) < 0) {
336ce917
SS
577 print_error(filename, err);
578 return err;
579 }
e0518705
AK
580 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
581 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
582 return AVERROR_OPTION_NOT_FOUND;
583 }
584
336ce917
SS
585
586 /* fill the streams in the format context */
c960e67a 587 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
336ce917
SS
588 print_error(filename, err);
589 return err;
590 }
591
610219a5 592 av_dump_format(fmt_ctx, 0, filename, 0);
336ce917
SS
593
594 /* bind a decoder to each input stream */
595 for (i = 0; i < fmt_ctx->nb_streams; i++) {
596 AVStream *stream = fmt_ctx->streams[i];
597 AVCodec *codec;
598
599 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
0184bbe2
GN
600 fprintf(stderr,
601 "Unsupported codec with id %d for input stream %d\n",
336ce917 602 stream->codec->codec_id, stream->index);
0b950fe2 603 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
336ce917
SS
604 fprintf(stderr, "Error while opening codec for input stream %d\n",
605 stream->index);
606 }
607 }
608
609 *fmt_ctx_ptr = fmt_ctx;
610 return 0;
611}
612
093c50a4
JG
613static void close_input_file(AVFormatContext **ctx_ptr)
614{
615 int i;
616 AVFormatContext *fmt_ctx = *ctx_ptr;
617
618 /* close decoder for each stream */
619 for (i = 0; i < fmt_ctx->nb_streams; i++) {
620 AVStream *stream = fmt_ctx->streams[i];
621
622 avcodec_close(stream->codec);
623 }
624 avformat_close_input(ctx_ptr);
625}
626
336ce917
SS
627static int probe_file(const char *filename)
628{
629 AVFormatContext *fmt_ctx;
630 int ret, i;
631
632 if ((ret = open_input_file(&fmt_ctx, filename)))
633 return ret;
634
3a8c95f7
LB
635 if (do_show_format)
636 show_format(fmt_ctx);
530bbe96 637
3a8c95f7
LB
638 if (do_show_streams) {
639 probe_array_header("streams");
336ce917
SS
640 for (i = 0; i < fmt_ctx->nb_streams; i++)
641 show_stream(fmt_ctx, i);
3a8c95f7
LB
642 probe_array_footer("streams");
643 }
336ce917 644
3a8c95f7
LB
645 if (do_show_packets)
646 show_packets(fmt_ctx);
336ce917 647
093c50a4 648 close_input_file(&fmt_ctx);
336ce917
SS
649 return 0;
650}
651
652static void show_usage(void)
653{
654 printf("Simple multimedia streams analyzer\n");
9e12f0bf 655 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
336ce917
SS
656 printf("\n");
657}
658
26513856 659static int opt_format(const char *opt, const char *arg)
1be784a2
SS
660{
661 iformat = av_find_input_format(arg);
662 if (!iformat) {
663 fprintf(stderr, "Unknown input format: %s\n", arg);
26513856 664 return AVERROR(EINVAL);
1be784a2 665 }
26513856 666 return 0;
1be784a2
SS
667}
668
08354bf4
RB
669static int opt_show_format_entry(const char *opt, const char *arg)
670{
671 do_show_format = 1;
d8947523 672 nb_fmt_entries_to_show++;
3a8c95f7
LB
673 print_header = NULL;
674 print_footer = NULL;
675 print_array_header = NULL;
676 print_array_footer = NULL;
677 print_object_header = NULL;
678 print_object_footer = NULL;
679
680 print_integer = show_format_entry_integer;
681 print_string = show_format_entry_string;
08354bf4
RB
682 av_dict_set(&fmt_entries_to_show, arg, "", 0);
683 return 0;
684}
685
7cc8d638 686static void opt_input_file(void *optctx, const char *arg)
336ce917 687{
c8c0ac6b 688 if (input_filename) {
0184bbe2
GN
689 fprintf(stderr,
690 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
58621b8d 691 arg, input_filename);
c8c0ac6b
RP
692 exit(1);
693 }
912dd63e
SS
694 if (!strcmp(arg, "-"))
695 arg = "pipe:";
696 input_filename = arg;
336ce917
SS
697}
698
699static void show_help(void)
700{
6afd569e 701 av_log_set_callback(log_callback_help);
336ce917
SS
702 show_usage();
703 show_help_options(options, "Main options:\n", 0, 0);
704 printf("\n");
7a6cd995 705 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
336ce917
SS
706}
707
708static void opt_pretty(void)
709{
710 show_value_unit = 1;
711 use_value_prefix = 1;
712 use_byte_value_binary_prefix = 1;
713 use_value_sexagesimal_format = 1;
714}
715
716static const OptionDef options[] = {
717#include "cmdutils_common_opts.h"
1be784a2 718 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
0184bbe2
GN
719 { "unit", OPT_BOOL, {(void*)&show_value_unit},
720 "show unit of the displayed values" },
721 { "prefix", OPT_BOOL, {(void*)&use_value_prefix},
722 "use SI prefixes for the displayed values" },
336ce917
SS
723 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
724 "use binary prefixes for byte units" },
725 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
726 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
727 { "pretty", 0, {(void*)&opt_pretty},
728 "prettify the format of displayed values, make it more human readable" },
729 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
08354bf4
RB
730 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
731 "show a particular entry from the format/container info", "entry" },
530bbe96 732 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
818636ec 733 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
0184bbe2
GN
734 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default},
735 "generic catch all option", "" },
336ce917
SS
736 { NULL, },
737};
738
3a8c95f7
LB
739static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
740{
741 printf("%.*s", buf_size, buf);
742 return 0;
743}
744
745#define AVP_BUFFSIZE 4096
746
336ce917
SS
747int main(int argc, char **argv)
748{
6afd569e 749 int ret;
3a8c95f7
LB
750 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
751
752 if (!buffer)
753 exit(1);
6afd569e 754
182cbe43 755 parse_loglevel(argc, argv, options);
336ce917 756 av_register_all();
776f2bb9 757 avformat_network_init();
c558122e 758 init_opts();
6ce98ea4
SS
759#if CONFIG_AVDEVICE
760 avdevice_register_all();
761#endif
336ce917
SS
762
763 show_banner();
7cc8d638 764 parse_options(NULL, argc, argv, options, opt_input_file);
336ce917
SS
765
766 if (!input_filename) {
767 show_usage();
768 fprintf(stderr, "You have to specify one input file.\n");
0184bbe2
GN
769 fprintf(stderr,
770 "Use -h to get full help or, even better, run 'man %s'.\n",
771 program_name);
336ce917
SS
772 exit(1);
773 }
774
3a8c95f7
LB
775 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
776 probe_buf_write, NULL);
777 if (!probe_out)
778 exit(1);
6afd569e 779
3a8c95f7
LB
780 probe_header();
781 ret = probe_file(input_filename);
782 probe_footer();
783 avio_flush(probe_out);
784 avio_close(probe_out);
31f2c023 785
776f2bb9
MS
786 avformat_network_deinit();
787
6afd569e 788 return ret;
336ce917 789}