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.

980 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("has_b_frames", dec_ctx->has_b_frames);
  521. if (dec_ctx->sample_aspect_ratio.num)
  522. sar = &dec_ctx->sample_aspect_ratio;
  523. else if (stream->sample_aspect_ratio.num)
  524. sar = &stream->sample_aspect_ratio;
  525. if (sar) {
  526. probe_str("sample_aspect_ratio",
  527. rational_string(val_str, sizeof(val_str), ":", sar));
  528. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  529. dec_ctx->width * sar->num, dec_ctx->height * sar->den,
  530. 1024*1024);
  531. probe_str("display_aspect_ratio",
  532. rational_string(val_str, sizeof(val_str), ":",
  533. &display_aspect_ratio));
  534. }
  535. desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
  536. probe_str("pix_fmt", desc ? desc->name : "unknown");
  537. probe_int("level", dec_ctx->level);
  538. probe_str("color_range", av_color_range_name(dec_ctx->color_range));
  539. probe_str("color_space", av_color_space_name(dec_ctx->colorspace));
  540. probe_str("color_trc", av_color_transfer_name(dec_ctx->color_trc));
  541. probe_str("color_pri", av_color_primaries_name(dec_ctx->color_primaries));
  542. probe_str("chroma_loc", av_chroma_location_name(dec_ctx->chroma_sample_location));
  543. break;
  544. case AVMEDIA_TYPE_AUDIO:
  545. probe_str("sample_rate",
  546. value_string(val_str, sizeof(val_str),
  547. dec_ctx->sample_rate,
  548. unit_hertz_str));
  549. probe_int("channels", dec_ctx->channels);
  550. probe_int("bits_per_sample",
  551. av_get_bits_per_sample(dec_ctx->codec_id));
  552. break;
  553. }
  554. } else {
  555. probe_str("codec_type", "unknown");
  556. }
  557. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  558. probe_int("id", stream->id);
  559. probe_str("avg_frame_rate",
  560. rational_string(val_str, sizeof(val_str), "/",
  561. &stream->avg_frame_rate));
  562. if (dec_ctx->bit_rate)
  563. probe_str("bit_rate",
  564. value_string(val_str, sizeof(val_str),
  565. dec_ctx->bit_rate, unit_bit_per_second_str));
  566. probe_str("time_base",
  567. rational_string(val_str, sizeof(val_str), "/",
  568. &stream->time_base));
  569. probe_str("start_time",
  570. time_value_string(val_str, sizeof(val_str),
  571. stream->start_time, &stream->time_base));
  572. probe_str("duration",
  573. time_value_string(val_str, sizeof(val_str),
  574. stream->duration, &stream->time_base));
  575. if (stream->nb_frames)
  576. probe_int("nb_frames", stream->nb_frames);
  577. probe_dict(stream->metadata, "tags");
  578. probe_object_footer("stream");
  579. }
  580. static void show_format(AVFormatContext *fmt_ctx)
  581. {
  582. char val_str[128];
  583. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  584. probe_object_header("format");
  585. probe_str("filename", fmt_ctx->filename);
  586. probe_int("nb_streams", fmt_ctx->nb_streams);
  587. probe_str("format_name", fmt_ctx->iformat->name);
  588. probe_str("format_long_name", fmt_ctx->iformat->long_name);
  589. probe_str("start_time",
  590. time_value_string(val_str, sizeof(val_str),
  591. fmt_ctx->start_time, &AV_TIME_BASE_Q));
  592. probe_str("duration",
  593. time_value_string(val_str, sizeof(val_str),
  594. fmt_ctx->duration, &AV_TIME_BASE_Q));
  595. probe_str("size",
  596. size >= 0 ? value_string(val_str, sizeof(val_str),
  597. size, unit_byte_str)
  598. : "unknown");
  599. probe_str("bit_rate",
  600. value_string(val_str, sizeof(val_str),
  601. fmt_ctx->bit_rate, unit_bit_per_second_str));
  602. probe_dict(fmt_ctx->metadata, "tags");
  603. probe_object_footer("format");
  604. }
  605. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  606. {
  607. int err, i;
  608. AVFormatContext *fmt_ctx = NULL;
  609. AVDictionaryEntry *t;
  610. if ((err = avformat_open_input(&fmt_ctx, filename,
  611. iformat, &format_opts)) < 0) {
  612. print_error(filename, err);
  613. return err;
  614. }
  615. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  616. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  617. return AVERROR_OPTION_NOT_FOUND;
  618. }
  619. /* fill the streams in the format context */
  620. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  621. print_error(filename, err);
  622. return err;
  623. }
  624. av_dump_format(fmt_ctx, 0, filename, 0);
  625. /* bind a decoder to each input stream */
  626. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  627. AVStream *stream = fmt_ctx->streams[i];
  628. AVCodec *codec;
  629. if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
  630. fprintf(stderr, "Failed to probe codec for input stream %d\n",
  631. stream->index);
  632. } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  633. fprintf(stderr,
  634. "Unsupported codec with id %d for input stream %d\n",
  635. stream->codec->codec_id, stream->index);
  636. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  637. fprintf(stderr, "Error while opening codec for input stream %d\n",
  638. stream->index);
  639. }
  640. }
  641. *fmt_ctx_ptr = fmt_ctx;
  642. return 0;
  643. }
  644. static void close_input_file(AVFormatContext **ctx_ptr)
  645. {
  646. int i;
  647. AVFormatContext *fmt_ctx = *ctx_ptr;
  648. /* close decoder for each stream */
  649. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  650. AVStream *stream = fmt_ctx->streams[i];
  651. avcodec_close(stream->codec);
  652. }
  653. avformat_close_input(ctx_ptr);
  654. }
  655. static int probe_file(const char *filename)
  656. {
  657. AVFormatContext *fmt_ctx;
  658. int ret, i;
  659. if ((ret = open_input_file(&fmt_ctx, filename)))
  660. return ret;
  661. if (do_show_format)
  662. show_format(fmt_ctx);
  663. if (do_show_streams) {
  664. probe_array_header("streams");
  665. for (i = 0; i < fmt_ctx->nb_streams; i++)
  666. show_stream(fmt_ctx, i);
  667. probe_array_footer("streams");
  668. }
  669. if (do_show_packets)
  670. show_packets(fmt_ctx);
  671. close_input_file(&fmt_ctx);
  672. return 0;
  673. }
  674. static void show_usage(void)
  675. {
  676. printf("Simple multimedia streams analyzer\n");
  677. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  678. printf("\n");
  679. }
  680. static int opt_format(void *optctx, const char *opt, const char *arg)
  681. {
  682. iformat = av_find_input_format(arg);
  683. if (!iformat) {
  684. fprintf(stderr, "Unknown input format: %s\n", arg);
  685. return AVERROR(EINVAL);
  686. }
  687. return 0;
  688. }
  689. static int opt_output_format(void *optctx, const char *opt, const char *arg)
  690. {
  691. if (!strcmp(arg, "json")) {
  692. octx.print_header = json_print_header;
  693. octx.print_footer = json_print_footer;
  694. octx.print_array_header = json_print_array_header;
  695. octx.print_array_footer = json_print_array_footer;
  696. octx.print_object_header = json_print_object_header;
  697. octx.print_object_footer = json_print_object_footer;
  698. octx.print_integer = json_print_integer;
  699. octx.print_string = json_print_string;
  700. } else if (!strcmp(arg, "ini")) {
  701. octx.print_header = ini_print_header;
  702. octx.print_footer = ini_print_footer;
  703. octx.print_array_header = ini_print_array_header;
  704. octx.print_object_header = ini_print_object_header;
  705. octx.print_integer = ini_print_integer;
  706. octx.print_string = ini_print_string;
  707. } else if (!strcmp(arg, "old")) {
  708. octx.print_header = NULL;
  709. octx.print_object_header = old_print_object_header;
  710. octx.print_object_footer = old_print_object_footer;
  711. octx.print_string = old_print_string;
  712. } else {
  713. av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
  714. return AVERROR(EINVAL);
  715. }
  716. return 0;
  717. }
  718. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  719. {
  720. do_show_format = 1;
  721. nb_fmt_entries_to_show++;
  722. octx.print_header = NULL;
  723. octx.print_footer = NULL;
  724. octx.print_array_header = NULL;
  725. octx.print_array_footer = NULL;
  726. octx.print_object_header = NULL;
  727. octx.print_object_footer = NULL;
  728. octx.print_integer = show_format_entry_integer;
  729. octx.print_string = show_format_entry_string;
  730. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  731. return 0;
  732. }
  733. static void opt_input_file(void *optctx, const char *arg)
  734. {
  735. if (input_filename) {
  736. fprintf(stderr,
  737. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  738. arg, input_filename);
  739. exit_program(1);
  740. }
  741. if (!strcmp(arg, "-"))
  742. arg = "pipe:";
  743. input_filename = arg;
  744. }
  745. void show_help_default(const char *opt, const char *arg)
  746. {
  747. av_log_set_callback(log_callback_help);
  748. show_usage();
  749. show_help_options(options, "Main options:", 0, 0, 0);
  750. printf("\n");
  751. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  752. }
  753. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  754. {
  755. show_value_unit = 1;
  756. use_value_prefix = 1;
  757. use_byte_value_binary_prefix = 1;
  758. use_value_sexagesimal_format = 1;
  759. return 0;
  760. }
  761. static const OptionDef real_options[] = {
  762. #include "cmdutils_common_opts.h"
  763. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  764. { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
  765. { "unit", OPT_BOOL, {&show_value_unit},
  766. "show unit of the displayed values" },
  767. { "prefix", OPT_BOOL, {&use_value_prefix},
  768. "use SI prefixes for the displayed values" },
  769. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  770. "use binary prefixes for byte units" },
  771. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  772. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  773. { "pretty", 0, {.func_arg = opt_pretty},
  774. "prettify the format of displayed values, make it more human readable" },
  775. { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
  776. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  777. "show a particular entry from the format/container info", "entry" },
  778. { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
  779. { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
  780. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
  781. "generic catch all option", "" },
  782. { NULL, },
  783. };
  784. static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
  785. {
  786. printf("%.*s", buf_size, buf);
  787. return 0;
  788. }
  789. #define AVP_BUFFSIZE 4096
  790. int main(int argc, char **argv)
  791. {
  792. int ret;
  793. uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
  794. if (!buffer)
  795. exit(1);
  796. register_exit(avprobe_cleanup);
  797. options = real_options;
  798. parse_loglevel(argc, argv, options);
  799. av_register_all();
  800. avformat_network_init();
  801. init_opts();
  802. #if CONFIG_AVDEVICE
  803. avdevice_register_all();
  804. #endif
  805. show_banner();
  806. octx.print_header = ini_print_header;
  807. octx.print_footer = ini_print_footer;
  808. octx.print_array_header = ini_print_array_header;
  809. octx.print_object_header = ini_print_object_header;
  810. octx.print_integer = ini_print_integer;
  811. octx.print_string = ini_print_string;
  812. parse_options(NULL, argc, argv, options, opt_input_file);
  813. if (!input_filename) {
  814. show_usage();
  815. fprintf(stderr, "You have to specify one input file.\n");
  816. fprintf(stderr,
  817. "Use -h to get full help or, even better, run 'man %s'.\n",
  818. program_name);
  819. exit_program(1);
  820. }
  821. probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
  822. probe_buf_write, NULL);
  823. if (!probe_out)
  824. exit_program(1);
  825. probe_header();
  826. ret = probe_file(input_filename);
  827. probe_footer();
  828. avio_flush(probe_out);
  829. avio_close(probe_out);
  830. avformat_network_deinit();
  831. return ret;
  832. }