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.

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