00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023
00024 #include "libavformat/avformat.h"
00025 #include "libavcodec/avcodec.h"
00026 #include "libavcodec/opt.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "libavdevice/avdevice.h"
00029 #include "cmdutils.h"
00030
00031 const char program_name[] = "FFprobe";
00032 const int program_birth_year = 2007;
00033
00034 static int do_show_format = 0;
00035 static int do_show_packets = 0;
00036 static int do_show_streams = 0;
00037
00038 static int show_value_unit = 0;
00039 static int use_value_prefix = 0;
00040 static int use_byte_value_binary_prefix = 0;
00041 static int use_value_sexagesimal_format = 0;
00042
00043
00044 static const OptionDef options[];
00045
00046
00047 static const char *input_filename;
00048 static AVInputFormat *iformat = NULL;
00049
00050 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
00051 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
00052
00053 static const char *unit_second_str = "s" ;
00054 static const char *unit_hertz_str = "Hz" ;
00055 static const char *unit_byte_str = "byte" ;
00056 static const char *unit_bit_per_second_str = "bit/s";
00057
00058 static char *value_string(char *buf, int buf_size, double val, const char *unit)
00059 {
00060 if (unit == unit_second_str && use_value_sexagesimal_format) {
00061 double secs;
00062 int hours, mins;
00063 secs = val;
00064 mins = (int)secs / 60;
00065 secs = secs - mins * 60;
00066 hours = mins / 60;
00067 mins %= 60;
00068 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
00069 } else if (use_value_prefix) {
00070 const char *prefix_string;
00071 int index;
00072
00073 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
00074 index = (int) (log(val)/log(2)) / 10;
00075 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
00076 val /= pow(2, index*10);
00077 prefix_string = binary_unit_prefixes[index];
00078 } else {
00079 index = (int) (log10(val)) / 3;
00080 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
00081 val /= pow(10, index*3);
00082 prefix_string = decimal_unit_prefixes[index];
00083 }
00084
00085 snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : "");
00086 } else {
00087 snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
00088 }
00089
00090 return buf;
00091 }
00092
00093 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
00094 {
00095 if (val == AV_NOPTS_VALUE) {
00096 snprintf(buf, buf_size, "N/A");
00097 } else {
00098 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
00099 }
00100
00101 return buf;
00102 }
00103
00104 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
00105 {
00106 if (ts == AV_NOPTS_VALUE) {
00107 snprintf(buf, buf_size, "N/A");
00108 } else {
00109 snprintf(buf, buf_size, "%"PRId64, ts);
00110 }
00111
00112 return buf;
00113 }
00114
00115 static const char *media_type_string(enum AVMediaType media_type)
00116 {
00117 switch (media_type) {
00118 case AVMEDIA_TYPE_VIDEO: return "video";
00119 case AVMEDIA_TYPE_AUDIO: return "audio";
00120 case AVMEDIA_TYPE_DATA: return "data";
00121 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
00122 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
00123 default: return "unknown";
00124 }
00125 }
00126
00127 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
00128 {
00129 char val_str[128];
00130 AVStream *st = fmt_ctx->streams[pkt->stream_index];
00131
00132 printf("[PACKET]\n");
00133 printf("codec_type=%s\n" , media_type_string(st->codec->codec_type));
00134 printf("stream_index=%d\n" , pkt->stream_index);
00135 printf("pts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->pts));
00136 printf("pts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
00137 printf("dts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->dts));
00138 printf("dts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
00139 printf("duration=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->duration));
00140 printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
00141 printf("size=%s\n" , value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str));
00142 printf("pos=%"PRId64"\n" , pkt->pos);
00143 printf("flags=%c\n" , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
00144 printf("[/PACKET]\n");
00145 }
00146
00147 static void show_packets(AVFormatContext *fmt_ctx)
00148 {
00149 AVPacket pkt;
00150
00151 av_init_packet(&pkt);
00152
00153 while (!av_read_frame(fmt_ctx, &pkt))
00154 show_packet(fmt_ctx, &pkt);
00155 }
00156
00157 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
00158 {
00159 AVStream *stream = fmt_ctx->streams[stream_idx];
00160 AVCodecContext *dec_ctx;
00161 AVCodec *dec;
00162 char val_str[128];
00163 AVMetadataTag *tag = NULL;
00164 AVRational display_aspect_ratio;
00165
00166 printf("[STREAM]\n");
00167
00168 printf("index=%d\n", stream->index);
00169
00170 if ((dec_ctx = stream->codec)) {
00171 if ((dec = dec_ctx->codec)) {
00172 printf("codec_name=%s\n", dec->name);
00173 printf("codec_long_name=%s\n", dec->long_name);
00174 } else {
00175 printf("codec_name=unknown\n");
00176 }
00177
00178 printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type));
00179 printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
00180
00181
00182 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
00183 printf("codec_tag_string=%s\n", val_str);
00184 printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
00185
00186 switch (dec_ctx->codec_type) {
00187 case AVMEDIA_TYPE_VIDEO:
00188 printf("width=%d\n", dec_ctx->width);
00189 printf("height=%d\n", dec_ctx->height);
00190 printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
00191 if (dec_ctx->sample_aspect_ratio.num) {
00192 printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
00193 dec_ctx->sample_aspect_ratio.den);
00194 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00195 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
00196 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
00197 1024*1024);
00198 printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
00199 display_aspect_ratio.den);
00200 }
00201 printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ?
00202 av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
00203 break;
00204
00205 case AVMEDIA_TYPE_AUDIO:
00206 printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
00207 dec_ctx->sample_rate,
00208 unit_hertz_str));
00209 printf("channels=%d\n", dec_ctx->channels);
00210 printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id));
00211 break;
00212 }
00213 } else {
00214 printf("codec_type=unknown\n");
00215 }
00216
00217 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
00218 printf("id=0x%x\n", stream->id);
00219 printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den);
00220 printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
00221 printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den);
00222 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time,
00223 &stream->time_base));
00224 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration,
00225 &stream->time_base));
00226 if (stream->nb_frames)
00227 printf("nb_frames=%"PRId64"\n", stream->nb_frames);
00228
00229 while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00230 printf("TAG:%s=%s\n", tag->key, tag->value);
00231
00232 printf("[/STREAM]\n");
00233 }
00234
00235 static void show_format(AVFormatContext *fmt_ctx)
00236 {
00237 AVMetadataTag *tag = NULL;
00238 char val_str[128];
00239
00240 printf("[FORMAT]\n");
00241
00242 printf("filename=%s\n", fmt_ctx->filename);
00243 printf("nb_streams=%d\n", fmt_ctx->nb_streams);
00244 printf("format_name=%s\n", fmt_ctx->iformat->name);
00245 printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
00246 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
00247 &AV_TIME_BASE_Q));
00248 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
00249 &AV_TIME_BASE_Q));
00250 printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
00251 unit_byte_str));
00252 printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
00253 unit_bit_per_second_str));
00254
00255 while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00256 printf("TAG:%s=%s\n", tag->key, tag->value);
00257
00258 printf("[/FORMAT]\n");
00259 }
00260
00261 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
00262 {
00263 int err, i;
00264 AVFormatContext *fmt_ctx;
00265
00266 fmt_ctx = avformat_alloc_context();
00267 set_context_opts(fmt_ctx, avformat_opts, AV_OPT_FLAG_DECODING_PARAM, NULL);
00268
00269 if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
00270 print_error(filename, err);
00271 return err;
00272 }
00273
00274
00275 if ((err = av_find_stream_info(fmt_ctx)) < 0) {
00276 print_error(filename, err);
00277 return err;
00278 }
00279
00280 av_dump_format(fmt_ctx, 0, filename, 0);
00281
00282
00283 for (i = 0; i < fmt_ctx->nb_streams; i++) {
00284 AVStream *stream = fmt_ctx->streams[i];
00285 AVCodec *codec;
00286
00287 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
00288 fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
00289 stream->codec->codec_id, stream->index);
00290 } else if (avcodec_open(stream->codec, codec) < 0) {
00291 fprintf(stderr, "Error while opening codec for input stream %d\n",
00292 stream->index);
00293 }
00294 }
00295
00296 *fmt_ctx_ptr = fmt_ctx;
00297 return 0;
00298 }
00299
00300 static int probe_file(const char *filename)
00301 {
00302 AVFormatContext *fmt_ctx;
00303 int ret, i;
00304
00305 if ((ret = open_input_file(&fmt_ctx, filename)))
00306 return ret;
00307
00308 if (do_show_packets)
00309 show_packets(fmt_ctx);
00310
00311 if (do_show_streams)
00312 for (i = 0; i < fmt_ctx->nb_streams; i++)
00313 show_stream(fmt_ctx, i);
00314
00315 if (do_show_format)
00316 show_format(fmt_ctx);
00317
00318 av_close_input_file(fmt_ctx);
00319 return 0;
00320 }
00321
00322 static void show_usage(void)
00323 {
00324 printf("Simple multimedia streams analyzer\n");
00325 printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
00326 printf("\n");
00327 }
00328
00329 static void opt_format(const char *arg)
00330 {
00331 iformat = av_find_input_format(arg);
00332 if (!iformat) {
00333 fprintf(stderr, "Unknown input format: %s\n", arg);
00334 exit(1);
00335 }
00336 }
00337
00338 static void opt_input_file(const char *arg)
00339 {
00340 if (input_filename) {
00341 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
00342 arg, input_filename);
00343 exit(1);
00344 }
00345 if (!strcmp(arg, "-"))
00346 arg = "pipe:";
00347 input_filename = arg;
00348 }
00349
00350 static void show_help(void)
00351 {
00352 av_log_set_callback(log_callback_help);
00353 show_usage();
00354 show_help_options(options, "Main options:\n", 0, 0);
00355 printf("\n");
00356 av_opt_show2(avformat_opts, NULL,
00357 AV_OPT_FLAG_DECODING_PARAM, 0);
00358 }
00359
00360 static void opt_pretty(void)
00361 {
00362 show_value_unit = 1;
00363 use_value_prefix = 1;
00364 use_byte_value_binary_prefix = 1;
00365 use_value_sexagesimal_format = 1;
00366 }
00367
00368 static const OptionDef options[] = {
00369 #include "cmdutils_common_opts.h"
00370 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
00371 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
00372 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
00373 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
00374 "use binary prefixes for byte units" },
00375 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
00376 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
00377 { "pretty", 0, {(void*)&opt_pretty},
00378 "prettify the format of displayed values, make it more human readable" },
00379 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
00380 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
00381 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
00382 { "default", OPT_FUNC2 | HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
00383 { NULL, },
00384 };
00385
00386 int main(int argc, char **argv)
00387 {
00388 int ret;
00389
00390 av_register_all();
00391 #if CONFIG_AVDEVICE
00392 avdevice_register_all();
00393 #endif
00394
00395 avformat_opts = avformat_alloc_context();
00396
00397 show_banner();
00398 parse_options(argc, argv, options, opt_input_file);
00399
00400 if (!input_filename) {
00401 show_usage();
00402 fprintf(stderr, "You have to specify one input file.\n");
00403 fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
00404 exit(1);
00405 }
00406
00407 ret = probe_file(input_filename);
00408
00409 av_free(avformat_opts);
00410
00411 return ret;
00412 }