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.

1222 lines
36KB

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