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.

910 lines
27KB

  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. /*
  57. * The output is structured in array and objects that might contain items
  58. * Array could require the objects within to not be named.
  59. * Object could require the items within to be named.
  60. *
  61. * For flat representation the name of each section is saved on prefix so it
  62. * can be rendered in order to represent nested structures (e.g. array of
  63. * objects for the packets list).
  64. *
  65. * Within an array each element can need an unique identifier or an index.
  66. *
  67. * Nesting level is accounted separately.
  68. */
  69. typedef enum {
  70. ARRAY,
  71. OBJECT
  72. } ProbeElementType;
  73. typedef struct {
  74. const char *name;
  75. ProbeElementType type;
  76. int64_t index;
  77. int64_t nb_elems;
  78. } ProbeElement;
  79. typedef struct {
  80. ProbeElement *prefix;
  81. int level;
  82. } OutputContext;
  83. static AVIOContext *probe_out = NULL;
  84. static OutputContext octx;
  85. #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
  86. /*
  87. * Default format, INI
  88. *
  89. * - all key and values are utf8
  90. * - '.' is the subgroup separator
  91. * - newlines and the following characters are escaped
  92. * - '\' is the escape character
  93. * - '#' is the comment
  94. * - '=' is the key/value separators
  95. * - ':' is not used but usually parsed as key/value separator
  96. */
  97. static void ini_print_header(void)
  98. {
  99. avio_printf(probe_out, "# avprobe output\n\n");
  100. }
  101. static void ini_print_footer(void)
  102. {
  103. avio_w8(probe_out, '\n');
  104. }
  105. static void ini_escape_print(const char *s)
  106. {
  107. int i = 0;
  108. char c = 0;
  109. while (c = s[i++]) {
  110. switch (c) {
  111. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  112. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  113. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  114. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  115. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  116. case '\\':
  117. case '#' :
  118. case '=' :
  119. case ':' : avio_w8(probe_out, '\\');
  120. default:
  121. if ((unsigned char)c < 32)
  122. avio_printf(probe_out, "\\x00%02x", c & 0xff);
  123. else
  124. avio_w8(probe_out, c);
  125. break;
  126. }
  127. }
  128. }
  129. static void ini_print_array_header(const char *name)
  130. {
  131. if (octx.prefix[octx.level -1].nb_elems)
  132. avio_printf(probe_out, "\n");
  133. }
  134. static void ini_print_object_header(const char *name)
  135. {
  136. int i;
  137. ProbeElement *el = octx.prefix + octx.level -1;
  138. if (el->nb_elems)
  139. avio_printf(probe_out, "\n");
  140. avio_printf(probe_out, "[");
  141. for (i = 1; i < octx.level; i++) {
  142. el = octx.prefix + i;
  143. avio_printf(probe_out, "%s.", el->name);
  144. if (el->index >= 0)
  145. avio_printf(probe_out, "%"PRId64".", el->index);
  146. }
  147. avio_printf(probe_out, "%s", name);
  148. if (el && el->type == ARRAY)
  149. avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
  150. avio_printf(probe_out, "]\n");
  151. }
  152. static void ini_print_integer(const char *key, int64_t value)
  153. {
  154. ini_escape_print(key);
  155. avio_printf(probe_out, "=%"PRId64"\n", value);
  156. }
  157. static void ini_print_string(const char *key, const char *value)
  158. {
  159. ini_escape_print(key);
  160. avio_printf(probe_out, "=");
  161. ini_escape_print(value);
  162. avio_w8(probe_out, '\n');
  163. }
  164. /*
  165. * Alternate format, JSON
  166. */
  167. static void json_print_header(void)
  168. {
  169. avio_printf(probe_out, "{");
  170. }
  171. static void json_print_footer(void)
  172. {
  173. avio_printf(probe_out, "}\n");
  174. }
  175. static void json_print_array_header(const char *name)
  176. {
  177. if (octx.prefix[octx.level -1].nb_elems)
  178. avio_printf(probe_out, ",\n");
  179. AVP_INDENT();
  180. avio_printf(probe_out, "\"%s\" : ", name);
  181. avio_printf(probe_out, "[\n");
  182. }
  183. static void json_print_array_footer(const char *name)
  184. {
  185. avio_printf(probe_out, "\n");
  186. AVP_INDENT();
  187. avio_printf(probe_out, "]");
  188. }
  189. static void json_print_object_header(const char *name)
  190. {
  191. if (octx.prefix[octx.level -1].nb_elems)
  192. avio_printf(probe_out, ",\n");
  193. AVP_INDENT();
  194. if (octx.prefix[octx.level -1].type == OBJECT)
  195. avio_printf(probe_out, "\"%s\" : ", name);
  196. avio_printf(probe_out, "{\n");
  197. }
  198. static void json_print_object_footer(const char *name)
  199. {
  200. avio_printf(probe_out, "\n");
  201. AVP_INDENT();
  202. avio_printf(probe_out, "}");
  203. }
  204. static void json_print_integer(const char *key, int64_t value)
  205. {
  206. if (octx.prefix[octx.level -1].nb_elems)
  207. avio_printf(probe_out, ",\n");
  208. AVP_INDENT();
  209. avio_printf(probe_out, "\"%s\" : %"PRId64"", key, value);
  210. }
  211. static void json_escape_print(const char *s)
  212. {
  213. int i = 0;
  214. char c = 0;
  215. while (c = s[i++]) {
  216. switch (c) {
  217. case '\r': avio_printf(probe_out, "%s", "\\r"); break;
  218. case '\n': avio_printf(probe_out, "%s", "\\n"); break;
  219. case '\f': avio_printf(probe_out, "%s", "\\f"); break;
  220. case '\b': avio_printf(probe_out, "%s", "\\b"); break;
  221. case '\t': avio_printf(probe_out, "%s", "\\t"); break;
  222. case '\\':
  223. case '"' : avio_w8(probe_out, '\\');
  224. default:
  225. if ((unsigned char)c < 32)
  226. avio_printf(probe_out, "\\u00%02x", c & 0xff);
  227. else
  228. avio_w8(probe_out, c);
  229. break;
  230. }
  231. }
  232. }
  233. static void json_print_string(const char *key, const char *value)
  234. {
  235. if (octx.prefix[octx.level -1].nb_elems)
  236. avio_printf(probe_out, ",\n");
  237. AVP_INDENT();
  238. avio_w8(probe_out, '\"');
  239. json_escape_print(key);
  240. avio_printf(probe_out, "\" : \"");
  241. json_escape_print(value);
  242. avio_w8(probe_out, '\"');
  243. }
  244. /*
  245. * Simple Formatter for single entries.
  246. */
  247. static void show_format_entry_integer(const char *key, int64_t value)
  248. {
  249. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  250. if (nb_fmt_entries_to_show > 1)
  251. avio_printf(probe_out, "%s=", key);
  252. avio_printf(probe_out, "%"PRId64"\n", value);
  253. }
  254. }
  255. static void show_format_entry_string(const char *key, const char *value)
  256. {
  257. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  258. if (nb_fmt_entries_to_show > 1)
  259. avio_printf(probe_out, "%s=", key);
  260. avio_printf(probe_out, "%s\n", value);
  261. }
  262. }
  263. void (*print_header)(void) = ini_print_header;
  264. void (*print_footer)(void) = ini_print_footer;
  265. void (*print_array_header) (const char *name) = ini_print_array_header;
  266. void (*print_array_footer) (const char *name);
  267. void (*print_object_header)(const char *name) = ini_print_object_header;
  268. void (*print_object_footer)(const char *name);
  269. void (*print_integer) (const char *key, int64_t value) = ini_print_integer;
  270. void (*print_string) (const char *key, const char *value) = ini_print_string;
  271. static void probe_group_enter(const char *name, int type)
  272. {
  273. int64_t count = -1;
  274. octx.prefix =
  275. av_realloc(octx.prefix, sizeof(ProbeElement) * (octx.level + 1));
  276. if (!octx.prefix || !name) {
  277. fprintf(stderr, "Out of memory\n");
  278. exit(1);
  279. }
  280. if (octx.level) {
  281. ProbeElement *parent = octx.prefix + octx.level -1;
  282. if (parent->type == ARRAY)
  283. count = parent->nb_elems;
  284. parent->nb_elems++;
  285. }
  286. octx.prefix[octx.level++] = (ProbeElement){name, type, count, 0};
  287. }
  288. static void probe_group_leave(void)
  289. {
  290. --octx.level;
  291. }
  292. static void probe_header(void)
  293. {
  294. if (print_header)
  295. print_header();
  296. probe_group_enter("root", OBJECT);
  297. }
  298. static void probe_footer(void)
  299. {
  300. if (print_footer)
  301. print_footer();
  302. probe_group_leave();
  303. }
  304. static void probe_array_header(const char *name)
  305. {
  306. if (print_array_header)
  307. print_array_header(name);
  308. probe_group_enter(name, ARRAY);
  309. }
  310. static void probe_array_footer(const char *name)
  311. {
  312. probe_group_leave();
  313. if (print_array_footer)
  314. print_array_footer(name);
  315. }
  316. static void probe_object_header(const char *name)
  317. {
  318. if (print_object_header)
  319. print_object_header(name);
  320. probe_group_enter(name, OBJECT);
  321. }
  322. static void probe_object_footer(const char *name)
  323. {
  324. probe_group_leave();
  325. if (print_object_footer)
  326. print_object_footer(name);
  327. }
  328. static void probe_int(const char *key, int64_t value)
  329. {
  330. print_integer(key, value);
  331. octx.prefix[octx.level -1].nb_elems++;
  332. }
  333. static void probe_str(const char *key, const char *value)
  334. {
  335. print_string(key, value);
  336. octx.prefix[octx.level -1].nb_elems++;
  337. }
  338. static void probe_dict(AVDictionary *dict, const char *name)
  339. {
  340. AVDictionaryEntry *entry = NULL;
  341. if (!dict)
  342. return;
  343. probe_object_header(name);
  344. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  345. probe_str(entry->key, entry->value);
  346. }
  347. probe_object_footer(name);
  348. }
  349. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  350. {
  351. if (unit == unit_second_str && use_value_sexagesimal_format) {
  352. double secs;
  353. int hours, mins;
  354. secs = val;
  355. mins = (int)secs / 60;
  356. secs = secs - mins * 60;
  357. hours = mins / 60;
  358. mins %= 60;
  359. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  360. } else if (use_value_prefix) {
  361. const char *prefix_string;
  362. int index;
  363. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  364. index = (int) (log(val)/log(2)) / 10;
  365. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
  366. val /= pow(2, index * 10);
  367. prefix_string = binary_unit_prefixes[index];
  368. } else {
  369. index = (int) (log10(val)) / 3;
  370. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
  371. val /= pow(10, index * 3);
  372. prefix_string = decimal_unit_prefixes[index];
  373. }
  374. snprintf(buf, buf_size, "%.*f%s%s",
  375. index ? 3 : 0, val,
  376. prefix_string,
  377. show_value_unit ? unit : "");
  378. } else {
  379. snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
  380. }
  381. return buf;
  382. }
  383. static char *time_value_string(char *buf, int buf_size, int64_t val,
  384. const AVRational *time_base)
  385. {
  386. if (val == AV_NOPTS_VALUE) {
  387. snprintf(buf, buf_size, "N/A");
  388. } else {
  389. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  390. }
  391. return buf;
  392. }
  393. static char *ts_value_string(char *buf, int buf_size, int64_t ts)
  394. {
  395. if (ts == AV_NOPTS_VALUE) {
  396. snprintf(buf, buf_size, "N/A");
  397. } else {
  398. snprintf(buf, buf_size, "%"PRId64, ts);
  399. }
  400. return buf;
  401. }
  402. static char *rational_string(char *buf, int buf_size, const char *sep,
  403. const AVRational *rat)
  404. {
  405. snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
  406. return buf;
  407. }
  408. static char *tag_string(char *buf, int buf_size, int tag)
  409. {
  410. snprintf(buf, buf_size, "0x%04x", tag);
  411. return buf;
  412. }
  413. static const char *media_type_string(enum AVMediaType media_type)
  414. {
  415. switch (media_type) {
  416. case AVMEDIA_TYPE_VIDEO: return "video";
  417. case AVMEDIA_TYPE_AUDIO: return "audio";
  418. case AVMEDIA_TYPE_DATA: return "data";
  419. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  420. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  421. default: return "unknown";
  422. }
  423. }
  424. static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
  425. {
  426. char val_str[128];
  427. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  428. probe_object_header("packet");
  429. probe_str("codec_type", media_type_string(st->codec->codec_type));
  430. probe_int("stream_index", pkt->stream_index);
  431. probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
  432. probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
  433. pkt->pts, &st->time_base));
  434. probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
  435. probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
  436. pkt->dts, &st->time_base));
  437. probe_str("duration", ts_value_string(val_str, sizeof(val_str),
  438. pkt->duration));
  439. probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
  440. pkt->duration,
  441. &st->time_base));
  442. probe_str("size", value_string(val_str, sizeof(val_str),
  443. pkt->size, unit_byte_str));
  444. probe_int("pos", pkt->pos);
  445. probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
  446. probe_object_footer("packet");
  447. }
  448. static void show_packets(AVFormatContext *fmt_ctx)
  449. {
  450. AVPacket pkt;
  451. av_init_packet(&pkt);
  452. probe_array_header("packets");
  453. while (!av_read_frame(fmt_ctx, &pkt))
  454. show_packet(fmt_ctx, &pkt);
  455. probe_array_footer("packets");
  456. }
  457. static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
  458. {
  459. AVStream *stream = fmt_ctx->streams[stream_idx];
  460. AVCodecContext *dec_ctx;
  461. AVCodec *dec;
  462. char val_str[128];
  463. AVRational display_aspect_ratio;
  464. probe_object_header("stream");
  465. probe_int("index", stream->index);
  466. if ((dec_ctx = stream->codec)) {
  467. if ((dec = dec_ctx->codec)) {
  468. probe_str("codec_name", dec->name);
  469. probe_str("codec_long_name", dec->long_name);
  470. } else {
  471. probe_str("codec_name", "unknown");
  472. }
  473. probe_str("codec_type", media_type_string(dec_ctx->codec_type));
  474. probe_str("codec_time_base",
  475. rational_string(val_str, sizeof(val_str),
  476. "/", &dec_ctx->time_base));
  477. /* print AVI/FourCC tag */
  478. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  479. probe_str("codec_tag_string", val_str);
  480. probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
  481. dec_ctx->codec_tag));
  482. switch (dec_ctx->codec_type) {
  483. case AVMEDIA_TYPE_VIDEO:
  484. probe_int("width", dec_ctx->width);
  485. probe_int("height", dec_ctx->height);
  486. probe_int("has_b_frames", dec_ctx->has_b_frames);
  487. if (dec_ctx->sample_aspect_ratio.num) {
  488. probe_str("sample_aspect_ratio",
  489. rational_string(val_str, sizeof(val_str), ":",
  490. &dec_ctx->sample_aspect_ratio));
  491. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  492. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  493. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  494. 1024*1024);
  495. probe_str("display_aspect_ratio",
  496. rational_string(val_str, sizeof(val_str), ":",
  497. &display_aspect_ratio));
  498. }
  499. probe_str("pix_fmt",
  500. dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name
  501. : "unknown");
  502. probe_int("level", dec_ctx->level);
  503. break;
  504. case AVMEDIA_TYPE_AUDIO:
  505. probe_str("sample_rate",
  506. value_string(val_str, sizeof(val_str),
  507. dec_ctx->sample_rate,
  508. unit_hertz_str));
  509. probe_int("channels", dec_ctx->channels);
  510. probe_int("bits_per_sample",
  511. av_get_bits_per_sample(dec_ctx->codec_id));
  512. break;
  513. }
  514. } else {
  515. probe_str("codec_type", "unknown");
  516. }
  517. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  518. probe_int("id", stream->id);
  519. probe_str("r_frame_rate",
  520. rational_string(val_str, sizeof(val_str), "/",
  521. &stream->r_frame_rate));
  522. probe_str("avg_frame_rate",
  523. rational_string(val_str, sizeof(val_str), "/",
  524. &stream->avg_frame_rate));
  525. probe_str("time_base",
  526. rational_string(val_str, sizeof(val_str), "/",
  527. &stream->time_base));
  528. probe_str("start_time",
  529. time_value_string(val_str, sizeof(val_str),
  530. stream->start_time, &stream->time_base));
  531. probe_str("duration",
  532. time_value_string(val_str, sizeof(val_str),
  533. stream->duration, &stream->time_base));
  534. if (stream->nb_frames)
  535. probe_int("nb_frames", stream->nb_frames);
  536. probe_dict(stream->metadata, "tags");
  537. probe_object_footer("stream");
  538. }
  539. static void show_format(AVFormatContext *fmt_ctx)
  540. {
  541. char val_str[128];
  542. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  543. probe_object_header("format");
  544. probe_str("filename", fmt_ctx->filename);
  545. probe_int("nb_streams", fmt_ctx->nb_streams);
  546. probe_str("format_name", fmt_ctx->iformat->name);
  547. probe_str("format_long_name", fmt_ctx->iformat->long_name);
  548. probe_str("start_time",
  549. time_value_string(val_str, sizeof(val_str),
  550. fmt_ctx->start_time, &AV_TIME_BASE_Q));
  551. probe_str("duration",
  552. time_value_string(val_str, sizeof(val_str),
  553. fmt_ctx->duration, &AV_TIME_BASE_Q));
  554. probe_str("size",
  555. size >= 0 ? value_string(val_str, sizeof(val_str),
  556. size, unit_byte_str)
  557. : "unknown");
  558. probe_str("bit_rate",
  559. value_string(val_str, sizeof(val_str),
  560. fmt_ctx->bit_rate, unit_bit_per_second_str));
  561. probe_dict(fmt_ctx->metadata, "tags");
  562. probe_object_footer("format");
  563. }
  564. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  565. {
  566. int err, i;
  567. AVFormatContext *fmt_ctx = NULL;
  568. AVDictionaryEntry *t;
  569. if ((err = avformat_open_input(&fmt_ctx, filename,
  570. iformat, &format_opts)) < 0) {
  571. print_error(filename, err);
  572. return err;
  573. }
  574. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  575. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  576. return AVERROR_OPTION_NOT_FOUND;
  577. }
  578. /* fill the streams in the format context */
  579. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  580. print_error(filename, err);
  581. return err;
  582. }
  583. av_dump_format(fmt_ctx, 0, filename, 0);
  584. /* bind a decoder to each input stream */
  585. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  586. AVStream *stream = fmt_ctx->streams[i];
  587. AVCodec *codec;
  588. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  589. fprintf(stderr,
  590. "Unsupported codec with id %d for input stream %d\n",
  591. stream->codec->codec_id, stream->index);
  592. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  593. fprintf(stderr, "Error while opening codec for input stream %d\n",
  594. stream->index);
  595. }
  596. }
  597. *fmt_ctx_ptr = fmt_ctx;
  598. return 0;
  599. }
  600. static void close_input_file(AVFormatContext **ctx_ptr)
  601. {
  602. int i;
  603. AVFormatContext *fmt_ctx = *ctx_ptr;
  604. /* close decoder for each stream */
  605. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  606. AVStream *stream = fmt_ctx->streams[i];
  607. avcodec_close(stream->codec);
  608. }
  609. avformat_close_input(ctx_ptr);
  610. }
  611. static int probe_file(const char *filename)
  612. {
  613. AVFormatContext *fmt_ctx;
  614. int ret, i;
  615. if ((ret = open_input_file(&fmt_ctx, filename)))
  616. return ret;
  617. if (do_show_format)
  618. show_format(fmt_ctx);
  619. if (do_show_streams) {
  620. probe_array_header("streams");
  621. for (i = 0; i < fmt_ctx->nb_streams; i++)
  622. show_stream(fmt_ctx, i);
  623. probe_array_footer("streams");
  624. }
  625. if (do_show_packets)
  626. show_packets(fmt_ctx);
  627. close_input_file(&fmt_ctx);
  628. return 0;
  629. }
  630. static void show_usage(void)
  631. {
  632. printf("Simple multimedia streams analyzer\n");
  633. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  634. printf("\n");
  635. }
  636. static int opt_format(const char *opt, const char *arg)
  637. {
  638. iformat = av_find_input_format(arg);
  639. if (!iformat) {
  640. fprintf(stderr, "Unknown input format: %s\n", arg);
  641. return AVERROR(EINVAL);
  642. }
  643. return 0;
  644. }
  645. static void opt_output_format(const char *opt, const char *arg)
  646. {
  647. if (!strcmp(arg, "json")) {
  648. print_header = json_print_header;
  649. print_footer = json_print_footer;
  650. print_array_header = json_print_array_header;
  651. print_array_footer = json_print_array_footer;
  652. print_object_header = json_print_object_header;
  653. print_object_footer = json_print_object_footer;
  654. print_integer = json_print_integer;
  655. print_string = json_print_string;
  656. } else
  657. if (!strcmp(arg, "ini")) {
  658. print_header = ini_print_header;
  659. print_footer = ini_print_footer;
  660. print_array_header = ini_print_array_header;
  661. print_object_header = ini_print_object_header;
  662. print_integer = ini_print_integer;
  663. print_string = ini_print_string;
  664. } else {
  665. av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
  666. exit(1);
  667. }
  668. }
  669. static int opt_show_format_entry(const char *opt, const char *arg)
  670. {
  671. do_show_format = 1;
  672. nb_fmt_entries_to_show++;
  673. print_header = NULL;
  674. print_footer = NULL;
  675. print_array_header = NULL;
  676. print_array_footer = NULL;
  677. print_object_header = NULL;
  678. print_object_footer = NULL;
  679. print_integer = show_format_entry_integer;
  680. print_string = show_format_entry_string;
  681. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  682. return 0;
  683. }
  684. static void opt_input_file(void *optctx, const char *arg)
  685. {
  686. if (input_filename) {
  687. fprintf(stderr,
  688. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  689. arg, input_filename);
  690. exit(1);
  691. }
  692. if (!strcmp(arg, "-"))
  693. arg = "pipe:";
  694. input_filename = arg;
  695. }
  696. static void show_help(void)
  697. {
  698. av_log_set_callback(log_callback_help);
  699. show_usage();
  700. show_help_options(options, "Main options:\n", 0, 0);
  701. printf("\n");
  702. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  703. }
  704. static void opt_pretty(void)
  705. {
  706. show_value_unit = 1;
  707. use_value_prefix = 1;
  708. use_byte_value_binary_prefix = 1;
  709. use_value_sexagesimal_format = 1;
  710. }
  711. static const OptionDef options[] = {
  712. #include "cmdutils_common_opts.h"
  713. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  714. { "of", HAS_ARG, {(void*)&opt_output_format}, "output the document either as ini or json", "output_format" },
  715. { "unit", OPT_BOOL, {(void*)&show_value_unit},
  716. "show unit of the displayed values" },
  717. { "prefix", OPT_BOOL, {(void*)&use_value_prefix},
  718. "use SI prefixes for the displayed values" },
  719. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  720. "use binary prefixes for byte units" },
  721. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  722. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  723. { "pretty", 0, {(void*)&opt_pretty},
  724. "prettify the format of displayed values, make it more human readable" },
  725. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  726. { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
  727. "show a particular entry from the format/container info", "entry" },
  728. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  729. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  730. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default},
  731. "generic catch all option", "" },
  732. { NULL, },
  733. };
  734. static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
  735. {
  736. printf("%.*s", buf_size, buf);
  737. return 0;
  738. }
  739. #define AVP_BUFFSIZE 4096
  740. int main(int argc, char **argv)
  741. {
  742. int ret;
  743. uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
  744. if (!buffer)
  745. exit(1);
  746. parse_loglevel(argc, argv, options);
  747. av_register_all();
  748. avformat_network_init();
  749. init_opts();
  750. #if CONFIG_AVDEVICE
  751. avdevice_register_all();
  752. #endif
  753. show_banner();
  754. parse_options(NULL, argc, argv, options, opt_input_file);
  755. if (!input_filename) {
  756. show_usage();
  757. fprintf(stderr, "You have to specify one input file.\n");
  758. fprintf(stderr,
  759. "Use -h to get full help or, even better, run 'man %s'.\n",
  760. program_name);
  761. exit(1);
  762. }
  763. probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
  764. probe_buf_write, NULL);
  765. if (!probe_out)
  766. exit(1);
  767. probe_header();
  768. ret = probe_file(input_filename);
  769. probe_footer();
  770. avio_flush(probe_out);
  771. avio_close(probe_out);
  772. avformat_network_deinit();
  773. return ret;
  774. }