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.

984 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 exit_program(void)
  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. } ProbeElementType;
  74. typedef struct {
  75. const char *name;
  76. ProbeElementType type;
  77. int64_t index;
  78. int64_t nb_elems;
  79. } ProbeElement;
  80. typedef struct {
  81. ProbeElement *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. } OutputContext;
  92. static AVIOContext *probe_out = NULL;
  93. static OutputContext 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. ProbeElement *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 && 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. while (*p) {
  263. *p = av_toupper(*p);
  264. p++;
  265. }
  266. avio_printf(probe_out, "[%s]\n", str);
  267. av_freep(&str);
  268. }
  269. static void old_print_object_footer(const char *name)
  270. {
  271. char *str, *p;
  272. if (!strcmp(name, "tags"))
  273. return;
  274. str = p = av_strdup(name);
  275. while (*p) {
  276. *p = av_toupper(*p);
  277. p++;
  278. }
  279. avio_printf(probe_out, "[/%s]\n", str);
  280. av_freep(&str);
  281. }
  282. static void old_print_string(const char *key, const char *value)
  283. {
  284. if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
  285. avio_printf(probe_out, "TAG:");
  286. ini_print_string(key, value);
  287. }
  288. /*
  289. * Simple Formatter for single entries.
  290. */
  291. static void show_format_entry_integer(const char *key, int64_t value)
  292. {
  293. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  294. if (nb_fmt_entries_to_show > 1)
  295. avio_printf(probe_out, "%s=", key);
  296. avio_printf(probe_out, "%"PRId64"\n", value);
  297. }
  298. }
  299. static void show_format_entry_string(const char *key, const char *value)
  300. {
  301. if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  302. if (nb_fmt_entries_to_show > 1)
  303. avio_printf(probe_out, "%s=", key);
  304. avio_printf(probe_out, "%s\n", value);
  305. }
  306. }
  307. static void probe_group_enter(const char *name, int type)
  308. {
  309. int64_t count = -1;
  310. octx.prefix =
  311. av_realloc(octx.prefix, sizeof(ProbeElement) * (octx.level + 1));
  312. if (!octx.prefix || !name) {
  313. fprintf(stderr, "Out of memory\n");
  314. exit(1);
  315. }
  316. if (octx.level) {
  317. ProbeElement *parent = octx.prefix + octx.level -1;
  318. if (parent->type == ARRAY)
  319. count = parent->nb_elems;
  320. parent->nb_elems++;
  321. }
  322. octx.prefix[octx.level++] = (ProbeElement){name, type, count, 0};
  323. }
  324. static void probe_group_leave(void)
  325. {
  326. --octx.level;
  327. }
  328. static void probe_header(void)
  329. {
  330. if (octx.print_header)
  331. octx.print_header();
  332. probe_group_enter("root", OBJECT);
  333. }
  334. static void probe_footer(void)
  335. {
  336. if (octx.print_footer)
  337. octx.print_footer();
  338. probe_group_leave();
  339. }
  340. static void probe_array_header(const char *name)
  341. {
  342. if (octx.print_array_header)
  343. octx.print_array_header(name);
  344. probe_group_enter(name, ARRAY);
  345. }
  346. static void probe_array_footer(const char *name)
  347. {
  348. probe_group_leave();
  349. if (octx.print_array_footer)
  350. octx.print_array_footer(name);
  351. }
  352. static void probe_object_header(const char *name)
  353. {
  354. if (octx.print_object_header)
  355. octx.print_object_header(name);
  356. probe_group_enter(name, OBJECT);
  357. }
  358. static void probe_object_footer(const char *name)
  359. {
  360. probe_group_leave();
  361. if (octx.print_object_footer)
  362. octx.print_object_footer(name);
  363. }
  364. static void probe_int(const char *key, int64_t value)
  365. {
  366. octx.print_integer(key, value);
  367. octx.prefix[octx.level -1].nb_elems++;
  368. }
  369. static void probe_str(const char *key, const char *value)
  370. {
  371. octx.print_string(key, value);
  372. octx.prefix[octx.level -1].nb_elems++;
  373. }
  374. static void probe_dict(AVDictionary *dict, const char *name)
  375. {
  376. AVDictionaryEntry *entry = NULL;
  377. if (!dict)
  378. return;
  379. probe_object_header(name);
  380. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  381. probe_str(entry->key, entry->value);
  382. }
  383. probe_object_footer(name);
  384. }
  385. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  386. {
  387. if (unit == unit_second_str && use_value_sexagesimal_format) {
  388. double secs;
  389. int hours, mins;
  390. secs = val;
  391. mins = (int)secs / 60;
  392. secs = secs - mins * 60;
  393. hours = mins / 60;
  394. mins %= 60;
  395. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  396. } else if (use_value_prefix) {
  397. const char *prefix_string;
  398. int index;
  399. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  400. index = (int) log2(val) / 10;
  401. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
  402. val /= pow(2, index * 10);
  403. prefix_string = binary_unit_prefixes[index];
  404. } else {
  405. index = (int) (log10(val)) / 3;
  406. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
  407. val /= pow(10, index * 3);
  408. prefix_string = decimal_unit_prefixes[index];
  409. }
  410. snprintf(buf, buf_size, "%.*f%s%s",
  411. index ? 3 : 0, val,
  412. prefix_string,
  413. show_value_unit ? unit : "");
  414. } else {
  415. snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
  416. }
  417. return buf;
  418. }
  419. static char *time_value_string(char *buf, int buf_size, int64_t val,
  420. const AVRational *time_base)
  421. {
  422. if (val == AV_NOPTS_VALUE) {
  423. snprintf(buf, buf_size, "N/A");
  424. } else {
  425. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  426. }
  427. return buf;
  428. }
  429. static char *ts_value_string(char *buf, int buf_size, int64_t ts)
  430. {
  431. if (ts == AV_NOPTS_VALUE) {
  432. snprintf(buf, buf_size, "N/A");
  433. } else {
  434. snprintf(buf, buf_size, "%"PRId64, ts);
  435. }
  436. return buf;
  437. }
  438. static char *rational_string(char *buf, int buf_size, const char *sep,
  439. const AVRational *rat)
  440. {
  441. snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
  442. return buf;
  443. }
  444. static char *tag_string(char *buf, int buf_size, int tag)
  445. {
  446. snprintf(buf, buf_size, "0x%04x", tag);
  447. return buf;
  448. }
  449. static const char *media_type_string(enum AVMediaType media_type)
  450. {
  451. switch (media_type) {
  452. case AVMEDIA_TYPE_VIDEO: return "video";
  453. case AVMEDIA_TYPE_AUDIO: return "audio";
  454. case AVMEDIA_TYPE_DATA: return "data";
  455. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  456. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  457. default: return "unknown";
  458. }
  459. }
  460. static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
  461. {
  462. char val_str[128];
  463. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  464. probe_object_header("packet");
  465. probe_str("codec_type", media_type_string(st->codec->codec_type));
  466. probe_int("stream_index", pkt->stream_index);
  467. probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
  468. probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
  469. pkt->pts, &st->time_base));
  470. probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
  471. probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
  472. pkt->dts, &st->time_base));
  473. probe_str("duration", ts_value_string(val_str, sizeof(val_str),
  474. pkt->duration));
  475. probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
  476. pkt->duration,
  477. &st->time_base));
  478. probe_str("size", value_string(val_str, sizeof(val_str),
  479. pkt->size, unit_byte_str));
  480. probe_int("pos", pkt->pos);
  481. probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
  482. probe_object_footer("packet");
  483. }
  484. static void show_packets(AVFormatContext *fmt_ctx)
  485. {
  486. AVPacket pkt;
  487. av_init_packet(&pkt);
  488. probe_array_header("packets");
  489. while (!av_read_frame(fmt_ctx, &pkt))
  490. show_packet(fmt_ctx, &pkt);
  491. probe_array_footer("packets");
  492. }
  493. static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
  494. {
  495. AVStream *stream = fmt_ctx->streams[stream_idx];
  496. AVCodecContext *dec_ctx;
  497. const AVCodec *dec;
  498. const char *profile;
  499. char val_str[128];
  500. AVRational display_aspect_ratio, *sar = NULL;
  501. const AVPixFmtDescriptor *desc;
  502. probe_object_header("stream");
  503. probe_int("index", stream->index);
  504. if ((dec_ctx = stream->codec)) {
  505. if ((dec = dec_ctx->codec)) {
  506. probe_str("codec_name", dec->name);
  507. probe_str("codec_long_name", dec->long_name);
  508. } else {
  509. probe_str("codec_name", "unknown");
  510. }
  511. probe_str("codec_type", media_type_string(dec_ctx->codec_type));
  512. probe_str("codec_time_base",
  513. rational_string(val_str, sizeof(val_str),
  514. "/", &dec_ctx->time_base));
  515. /* print AVI/FourCC tag */
  516. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  517. probe_str("codec_tag_string", val_str);
  518. probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
  519. dec_ctx->codec_tag));
  520. /* print profile, if there is one */
  521. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  522. probe_str("profile", profile);
  523. switch (dec_ctx->codec_type) {
  524. case AVMEDIA_TYPE_VIDEO:
  525. probe_int("width", dec_ctx->width);
  526. probe_int("height", dec_ctx->height);
  527. probe_int("has_b_frames", dec_ctx->has_b_frames);
  528. if (dec_ctx->sample_aspect_ratio.num)
  529. sar = &dec_ctx->sample_aspect_ratio;
  530. else if (stream->sample_aspect_ratio.num)
  531. sar = &stream->sample_aspect_ratio;
  532. if (sar) {
  533. probe_str("sample_aspect_ratio",
  534. rational_string(val_str, sizeof(val_str), ":", sar));
  535. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  536. dec_ctx->width * sar->num, dec_ctx->height * sar->den,
  537. 1024*1024);
  538. probe_str("display_aspect_ratio",
  539. rational_string(val_str, sizeof(val_str), ":",
  540. &display_aspect_ratio));
  541. }
  542. desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
  543. probe_str("pix_fmt", desc ? desc->name : "unknown");
  544. probe_int("level", dec_ctx->level);
  545. break;
  546. case AVMEDIA_TYPE_AUDIO:
  547. probe_str("sample_rate",
  548. value_string(val_str, sizeof(val_str),
  549. dec_ctx->sample_rate,
  550. unit_hertz_str));
  551. probe_int("channels", dec_ctx->channels);
  552. probe_int("bits_per_sample",
  553. av_get_bits_per_sample(dec_ctx->codec_id));
  554. break;
  555. }
  556. } else {
  557. probe_str("codec_type", "unknown");
  558. }
  559. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  560. probe_int("id", stream->id);
  561. probe_str("avg_frame_rate",
  562. rational_string(val_str, sizeof(val_str), "/",
  563. &stream->avg_frame_rate));
  564. if (dec_ctx->bit_rate)
  565. probe_str("bit_rate",
  566. value_string(val_str, sizeof(val_str),
  567. dec_ctx->bit_rate, unit_bit_per_second_str));
  568. probe_str("time_base",
  569. rational_string(val_str, sizeof(val_str), "/",
  570. &stream->time_base));
  571. probe_str("start_time",
  572. time_value_string(val_str, sizeof(val_str),
  573. stream->start_time, &stream->time_base));
  574. probe_str("duration",
  575. time_value_string(val_str, sizeof(val_str),
  576. stream->duration, &stream->time_base));
  577. if (stream->nb_frames)
  578. probe_int("nb_frames", stream->nb_frames);
  579. probe_dict(stream->metadata, "tags");
  580. probe_object_footer("stream");
  581. }
  582. static void show_format(AVFormatContext *fmt_ctx)
  583. {
  584. char val_str[128];
  585. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  586. probe_object_header("format");
  587. probe_str("filename", fmt_ctx->filename);
  588. probe_int("nb_streams", fmt_ctx->nb_streams);
  589. probe_str("format_name", fmt_ctx->iformat->name);
  590. probe_str("format_long_name", fmt_ctx->iformat->long_name);
  591. probe_str("start_time",
  592. time_value_string(val_str, sizeof(val_str),
  593. fmt_ctx->start_time, &AV_TIME_BASE_Q));
  594. probe_str("duration",
  595. time_value_string(val_str, sizeof(val_str),
  596. fmt_ctx->duration, &AV_TIME_BASE_Q));
  597. probe_str("size",
  598. size >= 0 ? value_string(val_str, sizeof(val_str),
  599. size, unit_byte_str)
  600. : "unknown");
  601. probe_str("bit_rate",
  602. value_string(val_str, sizeof(val_str),
  603. fmt_ctx->bit_rate, unit_bit_per_second_str));
  604. probe_dict(fmt_ctx->metadata, "tags");
  605. probe_object_footer("format");
  606. }
  607. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  608. {
  609. int err, i;
  610. AVFormatContext *fmt_ctx = NULL;
  611. AVDictionaryEntry *t;
  612. if ((err = avformat_open_input(&fmt_ctx, filename,
  613. iformat, &format_opts)) < 0) {
  614. print_error(filename, err);
  615. return err;
  616. }
  617. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  618. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  619. return AVERROR_OPTION_NOT_FOUND;
  620. }
  621. /* fill the streams in the format context */
  622. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  623. print_error(filename, err);
  624. return err;
  625. }
  626. av_dump_format(fmt_ctx, 0, filename, 0);
  627. /* bind a decoder to each input stream */
  628. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  629. AVStream *stream = fmt_ctx->streams[i];
  630. AVCodec *codec;
  631. if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
  632. fprintf(stderr, "Failed to probe codec for input stream %d\n",
  633. stream->index);
  634. } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  635. fprintf(stderr,
  636. "Unsupported codec with id %d for input stream %d\n",
  637. stream->codec->codec_id, stream->index);
  638. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  639. fprintf(stderr, "Error while opening codec for input stream %d\n",
  640. stream->index);
  641. }
  642. }
  643. *fmt_ctx_ptr = fmt_ctx;
  644. return 0;
  645. }
  646. static void close_input_file(AVFormatContext **ctx_ptr)
  647. {
  648. int i;
  649. AVFormatContext *fmt_ctx = *ctx_ptr;
  650. /* close decoder for each stream */
  651. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  652. AVStream *stream = fmt_ctx->streams[i];
  653. avcodec_close(stream->codec);
  654. }
  655. avformat_close_input(ctx_ptr);
  656. }
  657. static int probe_file(const char *filename)
  658. {
  659. AVFormatContext *fmt_ctx;
  660. int ret, i;
  661. if ((ret = open_input_file(&fmt_ctx, filename)))
  662. return ret;
  663. if (do_show_format)
  664. show_format(fmt_ctx);
  665. if (do_show_streams) {
  666. probe_array_header("streams");
  667. for (i = 0; i < fmt_ctx->nb_streams; i++)
  668. show_stream(fmt_ctx, i);
  669. probe_array_footer("streams");
  670. }
  671. if (do_show_packets)
  672. show_packets(fmt_ctx);
  673. close_input_file(&fmt_ctx);
  674. return 0;
  675. }
  676. static void show_usage(void)
  677. {
  678. printf("Simple multimedia streams analyzer\n");
  679. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  680. printf("\n");
  681. }
  682. static int opt_format(void *optctx, const char *opt, const char *arg)
  683. {
  684. iformat = av_find_input_format(arg);
  685. if (!iformat) {
  686. fprintf(stderr, "Unknown input format: %s\n", arg);
  687. return AVERROR(EINVAL);
  688. }
  689. return 0;
  690. }
  691. static int opt_output_format(void *optctx, const char *opt, const char *arg)
  692. {
  693. if (!strcmp(arg, "json")) {
  694. octx.print_header = json_print_header;
  695. octx.print_footer = json_print_footer;
  696. octx.print_array_header = json_print_array_header;
  697. octx.print_array_footer = json_print_array_footer;
  698. octx.print_object_header = json_print_object_header;
  699. octx.print_object_footer = json_print_object_footer;
  700. octx.print_integer = json_print_integer;
  701. octx.print_string = json_print_string;
  702. } else if (!strcmp(arg, "ini")) {
  703. octx.print_header = ini_print_header;
  704. octx.print_footer = ini_print_footer;
  705. octx.print_array_header = ini_print_array_header;
  706. octx.print_object_header = ini_print_object_header;
  707. octx.print_integer = ini_print_integer;
  708. octx.print_string = ini_print_string;
  709. } else if (!strcmp(arg, "old")) {
  710. octx.print_header = NULL;
  711. octx.print_object_header = old_print_object_header;
  712. octx.print_object_footer = old_print_object_footer;
  713. octx.print_string = old_print_string;
  714. } else {
  715. av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
  716. return AVERROR(EINVAL);
  717. }
  718. return 0;
  719. }
  720. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  721. {
  722. do_show_format = 1;
  723. nb_fmt_entries_to_show++;
  724. octx.print_header = NULL;
  725. octx.print_footer = NULL;
  726. octx.print_array_header = NULL;
  727. octx.print_array_footer = NULL;
  728. octx.print_object_header = NULL;
  729. octx.print_object_footer = NULL;
  730. octx.print_integer = show_format_entry_integer;
  731. octx.print_string = show_format_entry_string;
  732. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  733. return 0;
  734. }
  735. static void opt_input_file(void *optctx, const char *arg)
  736. {
  737. if (input_filename) {
  738. fprintf(stderr,
  739. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  740. arg, input_filename);
  741. exit(1);
  742. }
  743. if (!strcmp(arg, "-"))
  744. arg = "pipe:";
  745. input_filename = arg;
  746. }
  747. void show_help_default(const char *opt, const char *arg)
  748. {
  749. av_log_set_callback(log_callback_help);
  750. show_usage();
  751. show_help_options(options, "Main options:", 0, 0, 0);
  752. printf("\n");
  753. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  754. }
  755. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  756. {
  757. show_value_unit = 1;
  758. use_value_prefix = 1;
  759. use_byte_value_binary_prefix = 1;
  760. use_value_sexagesimal_format = 1;
  761. return 0;
  762. }
  763. static const OptionDef real_options[] = {
  764. #include "cmdutils_common_opts.h"
  765. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  766. { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
  767. { "unit", OPT_BOOL, {&show_value_unit},
  768. "show unit of the displayed values" },
  769. { "prefix", OPT_BOOL, {&use_value_prefix},
  770. "use SI prefixes for the displayed values" },
  771. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  772. "use binary prefixes for byte units" },
  773. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  774. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  775. { "pretty", 0, {.func_arg = opt_pretty},
  776. "prettify the format of displayed values, make it more human readable" },
  777. { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
  778. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  779. "show a particular entry from the format/container info", "entry" },
  780. { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
  781. { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
  782. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
  783. "generic catch all option", "" },
  784. { NULL, },
  785. };
  786. static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
  787. {
  788. printf("%.*s", buf_size, buf);
  789. return 0;
  790. }
  791. #define AVP_BUFFSIZE 4096
  792. int main(int argc, char **argv)
  793. {
  794. int ret;
  795. uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
  796. if (!buffer)
  797. exit(1);
  798. atexit(exit_program);
  799. options = real_options;
  800. parse_loglevel(argc, argv, options);
  801. av_register_all();
  802. avformat_network_init();
  803. init_opts();
  804. #if CONFIG_AVDEVICE
  805. avdevice_register_all();
  806. #endif
  807. show_banner();
  808. octx.print_header = ini_print_header;
  809. octx.print_footer = ini_print_footer;
  810. octx.print_array_header = ini_print_array_header;
  811. octx.print_object_header = ini_print_object_header;
  812. octx.print_integer = ini_print_integer;
  813. octx.print_string = ini_print_string;
  814. parse_options(NULL, argc, argv, options, opt_input_file);
  815. if (!input_filename) {
  816. show_usage();
  817. fprintf(stderr, "You have to specify one input file.\n");
  818. fprintf(stderr,
  819. "Use -h to get full help or, even better, run 'man %s'.\n",
  820. program_name);
  821. exit(1);
  822. }
  823. probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
  824. probe_buf_write, NULL);
  825. if (!probe_out)
  826. exit(1);
  827. probe_header();
  828. ret = probe_file(input_filename);
  829. probe_footer();
  830. avio_flush(probe_out);
  831. avio_close(probe_out);
  832. avformat_network_deinit();
  833. return ret;
  834. }