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