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.

1085 lines
32KB

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