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.

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