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.

1246 lines
37KB

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