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.

501 lines
17KB

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