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.

1034 lines
31KB

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