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