• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

avprobe.c

Go to the documentation of this file.
00001 /*
00002  * avprobe : Simple Media Prober based on the Libav libraries
00003  * Copyright (c) 2007-2010 Stefano Sabatini
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "config.h"
00023 
00024 #include "libavformat/avformat.h"
00025 #include "libavcodec/avcodec.h"
00026 #include "libavutil/opt.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "libavutil/dict.h"
00029 #include "libavdevice/avdevice.h"
00030 #include "cmdutils.h"
00031 
00032 const char program_name[] = "avprobe";
00033 const int program_birth_year = 2007;
00034 
00035 static int do_show_format  = 0;
00036 static int do_show_packets = 0;
00037 static int do_show_streams = 0;
00038 
00039 static int show_value_unit              = 0;
00040 static int use_value_prefix             = 0;
00041 static int use_byte_value_binary_prefix = 0;
00042 static int use_value_sexagesimal_format = 0;
00043 
00044 /* globals */
00045 static const OptionDef options[];
00046 
00047 /* AVprobe context */
00048 static const char *input_filename;
00049 static AVInputFormat *iformat = NULL;
00050 
00051 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
00052 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P"  };
00053 
00054 static const char *unit_second_str          = "s"    ;
00055 static const char *unit_hertz_str           = "Hz"   ;
00056 static const char *unit_byte_str            = "byte" ;
00057 static const char *unit_bit_per_second_str  = "bit/s";
00058 
00059 void exit_program(int ret)
00060 {
00061     exit(ret);
00062 }
00063 
00064 static char *value_string(char *buf, int buf_size, double val, const char *unit)
00065 {
00066     if (unit == unit_second_str && use_value_sexagesimal_format) {
00067         double secs;
00068         int hours, mins;
00069         secs  = val;
00070         mins  = (int)secs / 60;
00071         secs  = secs - mins * 60;
00072         hours = mins / 60;
00073         mins %= 60;
00074         snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
00075     } else if (use_value_prefix) {
00076         const char *prefix_string;
00077         int index;
00078 
00079         if (unit == unit_byte_str && use_byte_value_binary_prefix) {
00080             index = (int) (log(val)/log(2)) / 10;
00081             index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
00082             val  /= pow(2, index * 10);
00083             prefix_string = binary_unit_prefixes[index];
00084         } else {
00085             index = (int) (log10(val)) / 3;
00086             index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
00087             val  /= pow(10, index * 3);
00088             prefix_string = decimal_unit_prefixes[index];
00089         }
00090 
00091         snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string,
00092                  show_value_unit ? unit : "");
00093     } else {
00094         snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
00095     }
00096 
00097     return buf;
00098 }
00099 
00100 static char *time_value_string(char *buf, int buf_size, int64_t val,
00101                                const AVRational *time_base)
00102 {
00103     if (val == AV_NOPTS_VALUE) {
00104         snprintf(buf, buf_size, "N/A");
00105     } else {
00106         value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
00107     }
00108 
00109     return buf;
00110 }
00111 
00112 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
00113 {
00114     if (ts == AV_NOPTS_VALUE) {
00115         snprintf(buf, buf_size, "N/A");
00116     } else {
00117         snprintf(buf, buf_size, "%"PRId64, ts);
00118     }
00119 
00120     return buf;
00121 }
00122 
00123 static const char *media_type_string(enum AVMediaType media_type)
00124 {
00125     switch (media_type) {
00126     case AVMEDIA_TYPE_VIDEO:      return "video";
00127     case AVMEDIA_TYPE_AUDIO:      return "audio";
00128     case AVMEDIA_TYPE_DATA:       return "data";
00129     case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
00130     case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
00131     default:                      return "unknown";
00132     }
00133 }
00134 
00135 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
00136 {
00137     char val_str[128];
00138     AVStream *st = fmt_ctx->streams[pkt->stream_index];
00139 
00140     printf("[PACKET]\n");
00141     printf("codec_type=%s\n", media_type_string(st->codec->codec_type));
00142     printf("stream_index=%d\n", pkt->stream_index);
00143     printf("pts=%s\n", ts_value_string(val_str, sizeof(val_str), pkt->pts));
00144     printf("pts_time=%s\n", time_value_string(val_str, sizeof(val_str),
00145                                               pkt->pts, &st->time_base));
00146     printf("dts=%s\n", ts_value_string(val_str, sizeof(val_str), pkt->dts));
00147     printf("dts_time=%s\n", time_value_string(val_str, sizeof(val_str),
00148                                               pkt->dts, &st->time_base));
00149     printf("duration=%s\n", ts_value_string(val_str, sizeof(val_str),
00150                                             pkt->duration));
00151     printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str),
00152                                                    pkt->duration,
00153                                                    &st->time_base));
00154     printf("size=%s\n", value_string(val_str, sizeof(val_str),
00155                                      pkt->size, unit_byte_str));
00156     printf("pos=%"PRId64"\n", pkt->pos);
00157     printf("flags=%c\n", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
00158     printf("[/PACKET]\n");
00159 }
00160 
00161 static void show_packets(AVFormatContext *fmt_ctx)
00162 {
00163     AVPacket pkt;
00164 
00165     av_init_packet(&pkt);
00166 
00167     while (!av_read_frame(fmt_ctx, &pkt))
00168         show_packet(fmt_ctx, &pkt);
00169 }
00170 
00171 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
00172 {
00173     AVStream *stream = fmt_ctx->streams[stream_idx];
00174     AVCodecContext *dec_ctx;
00175     AVCodec *dec;
00176     char val_str[128];
00177     AVDictionaryEntry *tag = NULL;
00178     AVRational display_aspect_ratio;
00179 
00180     printf("[STREAM]\n");
00181 
00182     printf("index=%d\n", stream->index);
00183 
00184     if ((dec_ctx = stream->codec)) {
00185         if ((dec = dec_ctx->codec)) {
00186             printf("codec_name=%s\n", dec->name);
00187             printf("codec_long_name=%s\n", dec->long_name);
00188         } else {
00189             printf("codec_name=unknown\n");
00190         }
00191 
00192         printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type));
00193         printf("codec_time_base=%d/%d\n",
00194                dec_ctx->time_base.num, dec_ctx->time_base.den);
00195 
00196         /* print AVI/FourCC tag */
00197         av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
00198         printf("codec_tag_string=%s\n", val_str);
00199         printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
00200 
00201         switch (dec_ctx->codec_type) {
00202         case AVMEDIA_TYPE_VIDEO:
00203             printf("width=%d\n", dec_ctx->width);
00204             printf("height=%d\n", dec_ctx->height);
00205             printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
00206             if (dec_ctx->sample_aspect_ratio.num) {
00207                 printf("sample_aspect_ratio=%d:%d\n",
00208                        dec_ctx->sample_aspect_ratio.num,
00209                        dec_ctx->sample_aspect_ratio.den);
00210                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00211                           dec_ctx->width  * dec_ctx->sample_aspect_ratio.num,
00212                           dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
00213                           1024*1024);
00214                 printf("display_aspect_ratio=%d:%d\n",
00215                        display_aspect_ratio.num, display_aspect_ratio.den);
00216             }
00217             printf("pix_fmt=%s\n",
00218                    dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name
00219                                                     : "unknown");
00220             printf("level=%d\n", dec_ctx->level);
00221             break;
00222 
00223         case AVMEDIA_TYPE_AUDIO:
00224             printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
00225                                                     dec_ctx->sample_rate,
00226                                                     unit_hertz_str));
00227             printf("channels=%d\n", dec_ctx->channels);
00228             printf("bits_per_sample=%d\n",
00229                    av_get_bits_per_sample(dec_ctx->codec_id));
00230             break;
00231         }
00232     } else {
00233         printf("codec_type=unknown\n");
00234     }
00235 
00236     if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
00237         printf("id=0x%x\n", stream->id);
00238     printf("r_frame_rate=%d/%d\n",
00239            stream->r_frame_rate.num, stream->r_frame_rate.den);
00240     printf("avg_frame_rate=%d/%d\n",
00241            stream->avg_frame_rate.num, stream->avg_frame_rate.den);
00242     printf("time_base=%d/%d\n",
00243            stream->time_base.num, stream->time_base.den);
00244     printf("start_time=%s\n",
00245            time_value_string(val_str, sizeof(val_str),
00246                              stream->start_time, &stream->time_base));
00247     printf("duration=%s\n",
00248            time_value_string(val_str, sizeof(val_str),
00249                              stream->duration, &stream->time_base));
00250     if (stream->nb_frames)
00251         printf("nb_frames=%"PRId64"\n", stream->nb_frames);
00252 
00253     while ((tag = av_dict_get(stream->metadata, "", tag,
00254                               AV_DICT_IGNORE_SUFFIX)))
00255         printf("TAG:%s=%s\n", tag->key, tag->value);
00256 
00257     printf("[/STREAM]\n");
00258 }
00259 
00260 static void show_format(AVFormatContext *fmt_ctx)
00261 {
00262     AVDictionaryEntry *tag = NULL;
00263     char val_str[128];
00264     int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
00265 
00266     printf("[FORMAT]\n");
00267 
00268     printf("filename=%s\n", fmt_ctx->filename);
00269     printf("nb_streams=%d\n", fmt_ctx->nb_streams);
00270     printf("format_name=%s\n", fmt_ctx->iformat->name);
00271     printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
00272     printf("start_time=%s\n",
00273            time_value_string(val_str, sizeof(val_str),
00274                              fmt_ctx->start_time, &AV_TIME_BASE_Q));
00275     printf("duration=%s\n",
00276            time_value_string(val_str, sizeof(val_str),
00277                              fmt_ctx->duration, &AV_TIME_BASE_Q));
00278     printf("size=%s\n", size >= 0 ? value_string(val_str, sizeof(val_str),
00279                                                  size, unit_byte_str)
00280                                   : "unknown");
00281     printf("bit_rate=%s\n",
00282            value_string(val_str, sizeof(val_str),
00283                         fmt_ctx->bit_rate, unit_bit_per_second_str));
00284 
00285     while ((tag = av_dict_get(fmt_ctx->metadata, "", tag,
00286                               AV_DICT_IGNORE_SUFFIX)))
00287         printf("TAG:%s=%s\n", tag->key, tag->value);
00288 
00289     printf("[/FORMAT]\n");
00290 }
00291 
00292 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
00293 {
00294     int err, i;
00295     AVFormatContext *fmt_ctx = NULL;
00296     AVDictionaryEntry *t;
00297 
00298     if ((err = avformat_open_input(&fmt_ctx, filename,
00299                                    iformat, &format_opts)) < 0) {
00300         print_error(filename, err);
00301         return err;
00302     }
00303     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
00304         av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
00305         return AVERROR_OPTION_NOT_FOUND;
00306     }
00307 
00308 
00309     /* fill the streams in the format context */
00310     if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
00311         print_error(filename, err);
00312         return err;
00313     }
00314 
00315     av_dump_format(fmt_ctx, 0, filename, 0);
00316 
00317     /* bind a decoder to each input stream */
00318     for (i = 0; i < fmt_ctx->nb_streams; i++) {
00319         AVStream *stream = fmt_ctx->streams[i];
00320         AVCodec *codec;
00321 
00322         if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
00323             fprintf(stderr,
00324                     "Unsupported codec with id %d for input stream %d\n",
00325                     stream->codec->codec_id, stream->index);
00326         } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
00327             fprintf(stderr, "Error while opening codec for input stream %d\n",
00328                     stream->index);
00329         }
00330     }
00331 
00332     *fmt_ctx_ptr = fmt_ctx;
00333     return 0;
00334 }
00335 
00336 static int probe_file(const char *filename)
00337 {
00338     AVFormatContext *fmt_ctx;
00339     int ret, i;
00340 
00341     if ((ret = open_input_file(&fmt_ctx, filename)))
00342         return ret;
00343 
00344     if (do_show_packets)
00345         show_packets(fmt_ctx);
00346 
00347     if (do_show_streams)
00348         for (i = 0; i < fmt_ctx->nb_streams; i++)
00349             show_stream(fmt_ctx, i);
00350 
00351     if (do_show_format)
00352         show_format(fmt_ctx);
00353 
00354     avformat_close_input(&fmt_ctx);
00355     return 0;
00356 }
00357 
00358 static void show_usage(void)
00359 {
00360     printf("Simple multimedia streams analyzer\n");
00361     printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
00362     printf("\n");
00363 }
00364 
00365 static int opt_format(const char *opt, const char *arg)
00366 {
00367     iformat = av_find_input_format(arg);
00368     if (!iformat) {
00369         fprintf(stderr, "Unknown input format: %s\n", arg);
00370         return AVERROR(EINVAL);
00371     }
00372     return 0;
00373 }
00374 
00375 static void opt_input_file(void *optctx, const char *arg)
00376 {
00377     if (input_filename) {
00378         fprintf(stderr,
00379                 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
00380                 arg, input_filename);
00381         exit(1);
00382     }
00383     if (!strcmp(arg, "-"))
00384         arg = "pipe:";
00385     input_filename = arg;
00386 }
00387 
00388 static void show_help(void)
00389 {
00390     av_log_set_callback(log_callback_help);
00391     show_usage();
00392     show_help_options(options, "Main options:\n", 0, 0);
00393     printf("\n");
00394     show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
00395 }
00396 
00397 static void opt_pretty(void)
00398 {
00399     show_value_unit              = 1;
00400     use_value_prefix             = 1;
00401     use_byte_value_binary_prefix = 1;
00402     use_value_sexagesimal_format = 1;
00403 }
00404 
00405 static const OptionDef options[] = {
00406 #include "cmdutils_common_opts.h"
00407     { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
00408     { "unit", OPT_BOOL, {(void*)&show_value_unit},
00409       "show unit of the displayed values" },
00410     { "prefix", OPT_BOOL, {(void*)&use_value_prefix},
00411       "use SI prefixes for the displayed values" },
00412     { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
00413       "use binary prefixes for byte units" },
00414     { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
00415       "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
00416     { "pretty", 0, {(void*)&opt_pretty},
00417       "prettify the format of displayed values, make it more human readable" },
00418     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
00419     { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
00420     { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
00421     { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default},
00422       "generic catch all option", "" },
00423     { NULL, },
00424 };
00425 
00426 int main(int argc, char **argv)
00427 {
00428     int ret;
00429 
00430     parse_loglevel(argc, argv, options);
00431     av_register_all();
00432     avformat_network_init();
00433     init_opts();
00434 #if CONFIG_AVDEVICE
00435     avdevice_register_all();
00436 #endif
00437 
00438     show_banner();
00439     parse_options(NULL, argc, argv, options, opt_input_file);
00440 
00441     if (!input_filename) {
00442         show_usage();
00443         fprintf(stderr, "You have to specify one input file.\n");
00444         fprintf(stderr,
00445                 "Use -h to get full help or, even better, run 'man %s'.\n",
00446                 program_name);
00447         exit(1);
00448     }
00449 
00450     ret = probe_file(input_filename);
00451 
00452     avformat_network_deinit();
00453 
00454     return ret;
00455 }
Generated on Thu Jul 11 2013 15:38:17 for Libav by doxygen 1.7.1