You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
15KB

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