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.

975 lines
28KB

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