Commit | Line | Data |
---|---|---|
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 | 32 | const char program_name[] = "avprobe"; |
336ce917 SS |
33 | const int program_birth_year = 2007; |
34 | ||
35 | static int do_show_format = 0; | |
530bbe96 | 36 | static int do_show_packets = 0; |
336ce917 SS |
37 | static int do_show_streams = 0; |
38 | ||
39 | static int show_value_unit = 0; | |
40 | static int use_value_prefix = 0; | |
41 | static int use_byte_value_binary_prefix = 0; | |
42 | static int use_value_sexagesimal_format = 0; | |
43 | ||
44 | /* globals */ | |
45 | static const OptionDef options[]; | |
46 | ||
9e12f0bf | 47 | /* AVprobe context */ |
336ce917 | 48 | static const char *input_filename; |
1be784a2 | 49 | static AVInputFormat *iformat = NULL; |
336ce917 SS |
50 | |
51 | static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; | |
52 | static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" }; | |
53 | ||
54 | static const char *unit_second_str = "s" ; | |
55 | static const char *unit_hertz_str = "Hz" ; | |
56 | static const char *unit_byte_str = "byte" ; | |
57 | static const char *unit_bit_per_second_str = "bit/s"; | |
58 | ||
dad09ff9 AK |
59 | void exit_program(int ret) |
60 | { | |
61 | exit(ret); | |
62 | } | |
63 | ||
336ce917 SS |
64 | static char *value_string(char *buf, int buf_size, double val, const char *unit) |
65 | { | |
66 | if (unit == unit_second_str && use_value_sexagesimal_format) { | |
67 | double secs; | |
68 | int hours, mins; | |
69 | secs = val; | |
70 | mins = (int)secs / 60; | |
71 | secs = secs - mins * 60; | |
72 | hours = mins / 60; | |
73 | mins %= 60; | |
74 | snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs); | |
75 | } else if (use_value_prefix) { | |
76 | const char *prefix_string; | |
77 | int index; | |
78 | ||
79 | if (unit == unit_byte_str && use_byte_value_binary_prefix) { | |
f1b267dd | 80 | index = (int) (log(val)/log(2)) / 10; |
336ce917 SS |
81 | index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1); |
82 | val /= pow(2, index*10); | |
83 | prefix_string = binary_unit_prefixes[index]; | |
84 | } else { | |
85 | index = (int) (log10(val)) / 3; | |
86 | index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1); | |
87 | val /= pow(10, index*3); | |
88 | prefix_string = decimal_unit_prefixes[index]; | |
89 | } | |
90 | ||
91 | snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : ""); | |
92 | } else { | |
93 | snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : ""); | |
94 | } | |
95 | ||
96 | return buf; | |
97 | } | |
98 | ||
99 | static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base) | |
100 | { | |
101 | if (val == AV_NOPTS_VALUE) { | |
102 | snprintf(buf, buf_size, "N/A"); | |
103 | } else { | |
104 | value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str); | |
105 | } | |
106 | ||
107 | return buf; | |
108 | } | |
109 | ||
530bbe96 SS |
110 | static char *ts_value_string (char *buf, int buf_size, int64_t ts) |
111 | { | |
112 | if (ts == AV_NOPTS_VALUE) { | |
113 | snprintf(buf, buf_size, "N/A"); | |
114 | } else { | |
115 | snprintf(buf, buf_size, "%"PRId64, ts); | |
116 | } | |
117 | ||
118 | return buf; | |
119 | } | |
120 | ||
72415b2a | 121 | static const char *media_type_string(enum AVMediaType media_type) |
336ce917 | 122 | { |
72415b2a SS |
123 | switch (media_type) { |
124 | case AVMEDIA_TYPE_VIDEO: return "video"; | |
125 | case AVMEDIA_TYPE_AUDIO: return "audio"; | |
126 | case AVMEDIA_TYPE_DATA: return "data"; | |
127 | case AVMEDIA_TYPE_SUBTITLE: return "subtitle"; | |
128 | case AVMEDIA_TYPE_ATTACHMENT: return "attachment"; | |
336ce917 SS |
129 | default: return "unknown"; |
130 | } | |
131 | } | |
132 | ||
530bbe96 SS |
133 | static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt) |
134 | { | |
135 | char val_str[128]; | |
136 | AVStream *st = fmt_ctx->streams[pkt->stream_index]; | |
137 | ||
138 | printf("[PACKET]\n"); | |
139 | printf("codec_type=%s\n" , media_type_string(st->codec->codec_type)); | |
140 | printf("stream_index=%d\n" , pkt->stream_index); | |
141 | printf("pts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->pts)); | |
142 | printf("pts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base)); | |
143 | printf("dts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->dts)); | |
144 | printf("dts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base)); | |
145 | printf("duration=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->duration)); | |
146 | printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base)); | |
147 | printf("size=%s\n" , value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str)); | |
148 | printf("pos=%"PRId64"\n" , pkt->pos); | |
149 | printf("flags=%c\n" , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_'); | |
150 | printf("[/PACKET]\n"); | |
151 | } | |
152 | ||
153 | static void show_packets(AVFormatContext *fmt_ctx) | |
154 | { | |
155 | AVPacket pkt; | |
156 | ||
157 | av_init_packet(&pkt); | |
158 | ||
159 | while (!av_read_frame(fmt_ctx, &pkt)) | |
160 | show_packet(fmt_ctx, &pkt); | |
161 | } | |
162 | ||
336ce917 SS |
163 | static void show_stream(AVFormatContext *fmt_ctx, int stream_idx) |
164 | { | |
165 | AVStream *stream = fmt_ctx->streams[stream_idx]; | |
166 | AVCodecContext *dec_ctx; | |
167 | AVCodec *dec; | |
168 | char val_str[128]; | |
d2d67e42 | 169 | AVDictionaryEntry *tag = NULL; |
d21b2278 | 170 | AVRational display_aspect_ratio; |
336ce917 SS |
171 | |
172 | printf("[STREAM]\n"); | |
173 | ||
174 | printf("index=%d\n", stream->index); | |
175 | ||
176 | if ((dec_ctx = stream->codec)) { | |
177 | if ((dec = dec_ctx->codec)) { | |
178 | printf("codec_name=%s\n", dec->name); | |
179 | printf("codec_long_name=%s\n", dec->long_name); | |
180 | } else { | |
181 | printf("codec_name=unknown\n"); | |
182 | } | |
183 | ||
72415b2a | 184 | printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type)); |
336ce917 SS |
185 | printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den); |
186 | ||
187 | /* print AVI/FourCC tag */ | |
7e566bbe SS |
188 | av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag); |
189 | printf("codec_tag_string=%s\n", val_str); | |
190 | printf("codec_tag=0x%04x\n", dec_ctx->codec_tag); | |
336ce917 SS |
191 | |
192 | switch (dec_ctx->codec_type) { | |
72415b2a | 193 | case AVMEDIA_TYPE_VIDEO: |
336ce917 SS |
194 | printf("width=%d\n", dec_ctx->width); |
195 | printf("height=%d\n", dec_ctx->height); | |
196 | printf("has_b_frames=%d\n", dec_ctx->has_b_frames); | |
8280e2bd | 197 | if (dec_ctx->sample_aspect_ratio.num) { |
441881b4 SS |
198 | printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num, |
199 | dec_ctx->sample_aspect_ratio.den); | |
200 | av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, | |
201 | dec_ctx->width * dec_ctx->sample_aspect_ratio.num, | |
202 | dec_ctx->height * dec_ctx->sample_aspect_ratio.den, | |
203 | 1024*1024); | |
204 | printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num, | |
205 | display_aspect_ratio.den); | |
8280e2bd | 206 | } |
336ce917 SS |
207 | printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ? |
208 | av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown"); | |
364d6427 | 209 | printf("level=%d\n", dec_ctx->level); |
336ce917 SS |
210 | break; |
211 | ||
72415b2a | 212 | case AVMEDIA_TYPE_AUDIO: |
336ce917 SS |
213 | printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str), |
214 | dec_ctx->sample_rate, | |
215 | unit_hertz_str)); | |
216 | printf("channels=%d\n", dec_ctx->channels); | |
217 | printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id)); | |
218 | break; | |
219 | } | |
220 | } else { | |
221 | printf("codec_type=unknown\n"); | |
222 | } | |
223 | ||
224 | if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) | |
225 | printf("id=0x%x\n", stream->id); | |
226 | printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den); | |
227 | printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den); | |
228 | printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den); | |
336ce917 SS |
229 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time, |
230 | &stream->time_base)); | |
231 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration, | |
232 | &stream->time_base)); | |
0a6a282a RK |
233 | if (stream->nb_frames) |
234 | printf("nb_frames=%"PRId64"\n", stream->nb_frames); | |
336ce917 | 235 | |
d2d67e42 | 236 | while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) |
b6a41853 | 237 | printf("TAG:%s=%s\n", tag->key, tag->value); |
336ce917 SS |
238 | |
239 | printf("[/STREAM]\n"); | |
240 | } | |
241 | ||
242 | static void show_format(AVFormatContext *fmt_ctx) | |
243 | { | |
d2d67e42 | 244 | AVDictionaryEntry *tag = NULL; |
336ce917 SS |
245 | char val_str[128]; |
246 | ||
247 | printf("[FORMAT]\n"); | |
248 | ||
249 | printf("filename=%s\n", fmt_ctx->filename); | |
250 | printf("nb_streams=%d\n", fmt_ctx->nb_streams); | |
251 | printf("format_name=%s\n", fmt_ctx->iformat->name); | |
252 | printf("format_long_name=%s\n", fmt_ctx->iformat->long_name); | |
253 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, | |
254 | &AV_TIME_BASE_Q)); | |
255 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, | |
256 | &AV_TIME_BASE_Q)); | |
257 | printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, | |
258 | unit_byte_str)); | |
259 | printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, | |
260 | unit_bit_per_second_str)); | |
261 | ||
d2d67e42 | 262 | while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) |
336ce917 SS |
263 | printf("TAG:%s=%s\n", tag->key, tag->value); |
264 | ||
265 | printf("[/FORMAT]\n"); | |
266 | } | |
267 | ||
268 | static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename) | |
269 | { | |
270 | int err, i; | |
e0518705 AK |
271 | AVFormatContext *fmt_ctx = NULL; |
272 | AVDictionaryEntry *t; | |
336ce917 | 273 | |
e0518705 | 274 | if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) { |
336ce917 SS |
275 | print_error(filename, err); |
276 | return err; | |
277 | } | |
e0518705 AK |
278 | if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) { |
279 | av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key); | |
280 | return AVERROR_OPTION_NOT_FOUND; | |
281 | } | |
282 | ||
336ce917 SS |
283 | |
284 | /* fill the streams in the format context */ | |
c960e67a | 285 | if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { |
336ce917 SS |
286 | print_error(filename, err); |
287 | return err; | |
288 | } | |
289 | ||
610219a5 | 290 | av_dump_format(fmt_ctx, 0, filename, 0); |
336ce917 SS |
291 | |
292 | /* bind a decoder to each input stream */ | |
293 | for (i = 0; i < fmt_ctx->nb_streams; i++) { | |
294 | AVStream *stream = fmt_ctx->streams[i]; | |
295 | AVCodec *codec; | |
296 | ||
297 | if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) { | |
9c3f5ef6 | 298 | fprintf(stderr, "Unsupported codec with id %d for input stream %d\n", |
336ce917 | 299 | stream->codec->codec_id, stream->index); |
0b950fe2 | 300 | } else if (avcodec_open2(stream->codec, codec, NULL) < 0) { |
336ce917 SS |
301 | fprintf(stderr, "Error while opening codec for input stream %d\n", |
302 | stream->index); | |
303 | } | |
304 | } | |
305 | ||
306 | *fmt_ctx_ptr = fmt_ctx; | |
307 | return 0; | |
308 | } | |
309 | ||
310 | static int probe_file(const char *filename) | |
311 | { | |
312 | AVFormatContext *fmt_ctx; | |
313 | int ret, i; | |
314 | ||
315 | if ((ret = open_input_file(&fmt_ctx, filename))) | |
316 | return ret; | |
317 | ||
530bbe96 SS |
318 | if (do_show_packets) |
319 | show_packets(fmt_ctx); | |
320 | ||
336ce917 SS |
321 | if (do_show_streams) |
322 | for (i = 0; i < fmt_ctx->nb_streams; i++) | |
323 | show_stream(fmt_ctx, i); | |
324 | ||
325 | if (do_show_format) | |
326 | show_format(fmt_ctx); | |
327 | ||
328 | av_close_input_file(fmt_ctx); | |
329 | return 0; | |
330 | } | |
331 | ||
332 | static void show_usage(void) | |
333 | { | |
334 | printf("Simple multimedia streams analyzer\n"); | |
9e12f0bf | 335 | printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name); |
336ce917 SS |
336 | printf("\n"); |
337 | } | |
338 | ||
26513856 | 339 | static int opt_format(const char *opt, const char *arg) |
1be784a2 SS |
340 | { |
341 | iformat = av_find_input_format(arg); | |
342 | if (!iformat) { | |
343 | fprintf(stderr, "Unknown input format: %s\n", arg); | |
26513856 | 344 | return AVERROR(EINVAL); |
1be784a2 | 345 | } |
26513856 | 346 | return 0; |
1be784a2 SS |
347 | } |
348 | ||
7cc8d638 | 349 | static void opt_input_file(void *optctx, const char *arg) |
336ce917 | 350 | { |
c8c0ac6b | 351 | if (input_filename) { |
58621b8d SS |
352 | fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", |
353 | arg, input_filename); | |
c8c0ac6b RP |
354 | exit(1); |
355 | } | |
912dd63e SS |
356 | if (!strcmp(arg, "-")) |
357 | arg = "pipe:"; | |
358 | input_filename = arg; | |
336ce917 SS |
359 | } |
360 | ||
361 | static void show_help(void) | |
362 | { | |
20c21f8b | 363 | const AVClass *class = avformat_get_class(); |
6afd569e | 364 | av_log_set_callback(log_callback_help); |
336ce917 SS |
365 | show_usage(); |
366 | show_help_options(options, "Main options:\n", 0, 0); | |
367 | printf("\n"); | |
20c21f8b | 368 | av_opt_show2(&class, NULL, |
6afd569e | 369 | AV_OPT_FLAG_DECODING_PARAM, 0); |
336ce917 SS |
370 | } |
371 | ||
372 | static void opt_pretty(void) | |
373 | { | |
374 | show_value_unit = 1; | |
375 | use_value_prefix = 1; | |
376 | use_byte_value_binary_prefix = 1; | |
377 | use_value_sexagesimal_format = 1; | |
378 | } | |
379 | ||
380 | static const OptionDef options[] = { | |
381 | #include "cmdutils_common_opts.h" | |
1be784a2 | 382 | { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" }, |
818636ec SS |
383 | { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" }, |
384 | { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" }, | |
336ce917 SS |
385 | { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix}, |
386 | "use binary prefixes for byte units" }, | |
387 | { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format}, | |
388 | "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" }, | |
389 | { "pretty", 0, {(void*)&opt_pretty}, | |
390 | "prettify the format of displayed values, make it more human readable" }, | |
391 | { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" }, | |
530bbe96 | 392 | { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" }, |
818636ec | 393 | { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" }, |
26513856 | 394 | { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" }, |
336ce917 SS |
395 | { NULL, }, |
396 | }; | |
397 | ||
398 | int main(int argc, char **argv) | |
399 | { | |
6afd569e SS |
400 | int ret; |
401 | ||
182cbe43 | 402 | parse_loglevel(argc, argv, options); |
336ce917 | 403 | av_register_all(); |
c558122e | 404 | init_opts(); |
6ce98ea4 SS |
405 | #if CONFIG_AVDEVICE |
406 | avdevice_register_all(); | |
407 | #endif | |
336ce917 SS |
408 | |
409 | show_banner(); | |
7cc8d638 | 410 | parse_options(NULL, argc, argv, options, opt_input_file); |
336ce917 SS |
411 | |
412 | if (!input_filename) { | |
413 | show_usage(); | |
414 | fprintf(stderr, "You have to specify one input file.\n"); | |
9e12f0bf | 415 | fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name); |
336ce917 SS |
416 | exit(1); |
417 | } | |
418 | ||
6afd569e SS |
419 | ret = probe_file(input_filename); |
420 | ||
6afd569e | 421 | return ret; |
336ce917 | 422 | } |