OSDN Git Service

ffprobe: in value_string(), do not print trailing space in case of no suffix
[coroid/ffmpeg_saccubus.git] / ffprobe.c
1 /*
2  * ffprobe : Simple Media Prober based on the FFmpeg libraries
3  * Copyright (c) 2007-2010 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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
32 const char program_name[] = "ffprobe";
33 const int program_birth_year = 2007;
34
35 static int do_show_format  = 0;
36 static int do_show_packets = 0;
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
47 /* FFprobe context */
48 static const char *input_filename;
49 static AVInputFormat *iformat = NULL;
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
59 static char *value_string(char *buf, int buf_size, double val, const char *unit)
60 {
61     if (unit == unit_second_str && use_value_sexagesimal_format) {
62         double secs;
63         int hours, mins;
64         secs  = val;
65         mins  = (int)secs / 60;
66         secs  = secs - mins * 60;
67         hours = mins / 60;
68         mins %= 60;
69         snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
70     } else if (use_value_prefix) {
71         const char *prefix_string;
72         int index;
73
74         if (unit == unit_byte_str && use_byte_value_binary_prefix) {
75             index = (int) (log(val)/log(2)) / 10;
76             index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
77             val /= pow(2, index*10);
78             prefix_string = binary_unit_prefixes[index];
79         } else {
80             index = (int) (log10(val)) / 3;
81             index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
82             val /= pow(10, index*3);
83             prefix_string = decimal_unit_prefixes[index];
84         }
85
86         snprintf(buf, buf_size, "%.3f%s%s%s", val, prefix_string || show_value_unit ? " " : "",
87                  prefix_string, show_value_unit ? unit : "");
88     } else {
89         snprintf(buf, buf_size, "%f%s%s", val, show_value_unit ? " " : "",
90                  show_value_unit ? unit : "");
91     }
92
93     return buf;
94 }
95
96 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
97 {
98     if (val == AV_NOPTS_VALUE) {
99         snprintf(buf, buf_size, "N/A");
100     } else {
101         value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
102     }
103
104     return buf;
105 }
106
107 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
108 {
109     if (ts == AV_NOPTS_VALUE) {
110         snprintf(buf, buf_size, "N/A");
111     } else {
112         snprintf(buf, buf_size, "%"PRId64, ts);
113     }
114
115     return buf;
116 }
117
118 static const char *media_type_string(enum AVMediaType media_type)
119 {
120     switch (media_type) {
121     case AVMEDIA_TYPE_VIDEO:      return "video";
122     case AVMEDIA_TYPE_AUDIO:      return "audio";
123     case AVMEDIA_TYPE_DATA:       return "data";
124     case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
125     case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
126     default:                      return "unknown";
127     }
128 }
129
130 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
131 {
132     char val_str[128];
133     AVStream *st = fmt_ctx->streams[pkt->stream_index];
134
135     printf("[PACKET]\n");
136     printf("codec_type=%s\n"   , media_type_string(st->codec->codec_type));
137     printf("stream_index=%d\n" , pkt->stream_index);
138     printf("pts=%s\n"          , ts_value_string  (val_str, sizeof(val_str), pkt->pts));
139     printf("pts_time=%s\n"     , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
140     printf("dts=%s\n"          , ts_value_string  (val_str, sizeof(val_str), pkt->dts));
141     printf("dts_time=%s\n"     , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
142     printf("duration=%s\n"     , ts_value_string  (val_str, sizeof(val_str), pkt->duration));
143     printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
144     printf("size=%s\n"         , value_string     (val_str, sizeof(val_str), pkt->size, unit_byte_str));
145     printf("pos=%"PRId64"\n"   , pkt->pos);
146     printf("flags=%c\n"        , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
147     printf("[/PACKET]\n");
148     fflush(stdout);
149 }
150
151 static void show_packets(AVFormatContext *fmt_ctx)
152 {
153     AVPacket pkt;
154
155     av_init_packet(&pkt);
156
157     while (!av_read_frame(fmt_ctx, &pkt))
158         show_packet(fmt_ctx, &pkt);
159 }
160
161 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
162 {
163     AVStream *stream = fmt_ctx->streams[stream_idx];
164     AVCodecContext *dec_ctx;
165     AVCodec *dec;
166     char val_str[128];
167     AVDictionaryEntry *tag = NULL;
168     AVRational display_aspect_ratio;
169
170     printf("[STREAM]\n");
171
172     printf("index=%d\n",        stream->index);
173
174     if ((dec_ctx = stream->codec)) {
175         if ((dec = dec_ctx->codec)) {
176             printf("codec_name=%s\n",         dec->name);
177             printf("codec_long_name=%s\n",    dec->long_name);
178         } else {
179             printf("codec_name=unknown\n");
180         }
181
182         printf("codec_type=%s\n",         media_type_string(dec_ctx->codec_type));
183         printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
184
185         /* print AVI/FourCC tag */
186         av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
187         printf("codec_tag_string=%s\n", val_str);
188         printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
189
190         switch (dec_ctx->codec_type) {
191         case AVMEDIA_TYPE_VIDEO:
192             printf("width=%d\n",                   dec_ctx->width);
193             printf("height=%d\n",                  dec_ctx->height);
194             printf("has_b_frames=%d\n",            dec_ctx->has_b_frames);
195             if (dec_ctx->sample_aspect_ratio.num) {
196                 printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
197                                                       dec_ctx->sample_aspect_ratio.den);
198                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
199                           dec_ctx->width  * dec_ctx->sample_aspect_ratio.num,
200                           dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
201                           1024*1024);
202                 printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
203                                                        display_aspect_ratio.den);
204             }
205             printf("pix_fmt=%s\n",                 dec_ctx->pix_fmt != PIX_FMT_NONE ?
206                    av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
207             printf("level=%d\n",                   dec_ctx->level);
208             break;
209
210         case AVMEDIA_TYPE_AUDIO:
211             printf("sample_rate=%s\n",             value_string(val_str, sizeof(val_str),
212                                                                 dec_ctx->sample_rate,
213                                                                 unit_hertz_str));
214             printf("channels=%d\n",                dec_ctx->channels);
215             printf("bits_per_sample=%d\n",         av_get_bits_per_sample(dec_ctx->codec_id));
216             break;
217         }
218     } else {
219         printf("codec_type=unknown\n");
220     }
221
222     if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
223         printf("id=0x%x\n", stream->id);
224     printf("r_frame_rate=%d/%d\n",         stream->r_frame_rate.num,   stream->r_frame_rate.den);
225     printf("avg_frame_rate=%d/%d\n",       stream->avg_frame_rate.num, stream->avg_frame_rate.den);
226     printf("time_base=%d/%d\n",            stream->time_base.num,      stream->time_base.den);
227     printf("start_time=%s\n",   time_value_string(val_str, sizeof(val_str), stream->start_time,
228                                                   &stream->time_base));
229     printf("duration=%s\n",     time_value_string(val_str, sizeof(val_str), stream->duration,
230                                                   &stream->time_base));
231     if (stream->nb_frames)
232         printf("nb_frames=%"PRId64"\n",    stream->nb_frames);
233
234     while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
235         printf("TAG:%s=%s\n", tag->key, tag->value);
236
237     printf("[/STREAM]\n");
238     fflush(stdout);
239 }
240
241 static void show_format(AVFormatContext *fmt_ctx)
242 {
243     AVDictionaryEntry *tag = NULL;
244     char val_str[128];
245
246     printf("[FORMAT]\n");
247
248     printf("filename=%s\n",         fmt_ctx->filename);
249     printf("nb_streams=%d\n",       fmt_ctx->nb_streams);
250     printf("format_name=%s\n",      fmt_ctx->iformat->name);
251     printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
252     printf("start_time=%s\n",       time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
253                                                       &AV_TIME_BASE_Q));
254     printf("duration=%s\n",         time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
255                                                       &AV_TIME_BASE_Q));
256     printf("size=%s\n",             value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
257                                                  unit_byte_str));
258     printf("bit_rate=%s\n",         value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
259                                                  unit_bit_per_second_str));
260
261     while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
262         printf("TAG:%s=%s\n", tag->key, tag->value);
263
264     printf("[/FORMAT]\n");
265     fflush(stdout);
266 }
267
268 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
269 {
270     int err, i;
271     AVFormatContext *fmt_ctx = NULL;
272     AVDictionaryEntry *t;
273
274     if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
275         print_error(filename, err);
276         return err;
277     }
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
283
284     /* fill the streams in the format context */
285     if ((err = av_find_stream_info(fmt_ctx)) < 0) {
286         print_error(filename, err);
287         return err;
288     }
289
290     av_dump_format(fmt_ctx, 0, filename, 0);
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))) {
298             fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
299                     stream->codec->codec_id, stream->index);
300         } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
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
318     if (do_show_packets)
319         show_packets(fmt_ctx);
320
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");
335     printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
336     printf("\n");
337 }
338
339 static int opt_format(const char *opt, const char *arg)
340 {
341     iformat = av_find_input_format(arg);
342     if (!iformat) {
343         fprintf(stderr, "Unknown input format: %s\n", arg);
344         return AVERROR(EINVAL);
345     }
346     return 0;
347 }
348
349 static int opt_input_file(const char *opt, const char *arg)
350 {
351     if (input_filename) {
352         fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
353                 arg, input_filename);
354         exit(1);
355     }
356     if (!strcmp(arg, "-"))
357         arg = "pipe:";
358     input_filename = arg;
359     return 0;
360 }
361
362 static int opt_help(const char *opt, const char *arg)
363 {
364     av_log_set_callback(log_callback_help);
365     show_usage();
366     show_help_options(options, "Main options:\n", 0, 0);
367     printf("\n");
368     av_opt_show2(avformat_opts, NULL,
369                  AV_OPT_FLAG_DECODING_PARAM, 0);
370     return 0;
371 }
372
373 static int opt_pretty(const char *opt, const char *arg)
374 {
375     show_value_unit              = 1;
376     use_value_prefix             = 1;
377     use_byte_value_binary_prefix = 1;
378     use_value_sexagesimal_format = 1;
379     return 0;
380 }
381
382 static const OptionDef options[] = {
383 #include "cmdutils_common_opts.h"
384     { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
385     { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
386     { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
387     { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
388       "use binary prefixes for byte units" },
389     { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
390       "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
391     { "pretty", 0, {(void*)&opt_pretty},
392       "prettify the format of displayed values, make it more human readable" },
393     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
394     { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
395     { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
396     { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
397     { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
398     { NULL, },
399 };
400
401 int main(int argc, char **argv)
402 {
403     int ret;
404
405     av_register_all();
406     init_opts();
407 #if CONFIG_AVDEVICE
408     avdevice_register_all();
409 #endif
410
411     show_banner();
412     parse_options(argc, argv, options, opt_input_file);
413
414     if (!input_filename) {
415         show_usage();
416         fprintf(stderr, "You have to specify one input file.\n");
417         fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
418         exit(1);
419     }
420
421     ret = probe_file(input_filename);
422
423     av_free(avformat_opts);
424
425     return ret;
426 }