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