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.

982 lines
29KB

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