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.

1542 lines
48KB

  1. /*
  2. * Copyright (c) 2007-2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * simple media prober based on the FFmpeg libraries
  23. */
  24. #include "config.h"
  25. #include "libavformat/avformat.h"
  26. #include "libavcodec/avcodec.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/dict.h"
  31. #include "libavdevice/avdevice.h"
  32. #include "cmdutils.h"
  33. const char program_name[] = "ffprobe";
  34. const int program_birth_year = 2007;
  35. static int do_show_error = 0;
  36. static int do_show_format = 0;
  37. static int do_show_packets = 0;
  38. static int do_show_streams = 0;
  39. static int show_value_unit = 0;
  40. static int use_value_prefix = 0;
  41. static int use_byte_value_binary_prefix = 0;
  42. static int use_value_sexagesimal_format = 0;
  43. static int show_private_data = 1;
  44. static char *print_format;
  45. static const OptionDef options[];
  46. /* FFprobe context */
  47. static const char *input_filename;
  48. static AVInputFormat *iformat = NULL;
  49. static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  50. static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  51. static const char *unit_second_str = "s" ;
  52. static const char *unit_hertz_str = "Hz" ;
  53. static const char *unit_byte_str = "byte" ;
  54. static const char *unit_bit_per_second_str = "bit/s";
  55. void av_noreturn exit_program(int ret)
  56. {
  57. exit(ret);
  58. }
  59. struct unit_value {
  60. union { double d; int i; } val;
  61. const char *unit;
  62. };
  63. static char *value_string(char *buf, int buf_size, struct unit_value uv)
  64. {
  65. double vald;
  66. int show_float = 0;
  67. if (uv.unit == unit_second_str) {
  68. vald = uv.val.d;
  69. show_float = 1;
  70. } else {
  71. vald = uv.val.i;
  72. }
  73. if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
  74. double secs;
  75. int hours, mins;
  76. secs = vald;
  77. mins = (int)secs / 60;
  78. secs = secs - mins * 60;
  79. hours = mins / 60;
  80. mins %= 60;
  81. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  82. } else if (use_value_prefix) {
  83. const char *prefix_string;
  84. int index, l;
  85. if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
  86. index = (int) (log(vald)/log(2)) / 10;
  87. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
  88. vald /= pow(2, index*10);
  89. prefix_string = binary_unit_prefixes[index];
  90. } else {
  91. index = (int) (log10(vald)) / 3;
  92. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
  93. vald /= pow(10, index*3);
  94. prefix_string = decimal_unit_prefixes[index];
  95. }
  96. if (show_float || vald != (int)vald) l = snprintf(buf, buf_size, "%.3f", vald);
  97. else l = snprintf(buf, buf_size, "%d", (int)vald);
  98. snprintf(buf+l, buf_size-l, "%s%s%s", prefix_string || show_value_unit ? " " : "",
  99. prefix_string, show_value_unit ? uv.unit : "");
  100. } else {
  101. int l;
  102. if (show_float) l = snprintf(buf, buf_size, "%.3f", vald);
  103. else l = snprintf(buf, buf_size, "%d", (int)vald);
  104. snprintf(buf+l, buf_size-l, "%s%s", show_value_unit ? " " : "",
  105. show_value_unit ? uv.unit : "");
  106. }
  107. return buf;
  108. }
  109. /* WRITERS API */
  110. typedef struct WriterContext WriterContext;
  111. #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
  112. typedef struct Writer {
  113. int priv_size; ///< private size for the writer context
  114. const char *name;
  115. int (*init) (WriterContext *wctx, const char *args, void *opaque);
  116. void (*uninit)(WriterContext *wctx);
  117. void (*print_header)(WriterContext *ctx);
  118. void (*print_footer)(WriterContext *ctx);
  119. void (*print_chapter_header)(WriterContext *wctx, const char *);
  120. void (*print_chapter_footer)(WriterContext *wctx, const char *);
  121. void (*print_section_header)(WriterContext *wctx, const char *);
  122. void (*print_section_footer)(WriterContext *wctx, const char *);
  123. void (*print_integer) (WriterContext *wctx, const char *, long long int);
  124. void (*print_string) (WriterContext *wctx, const char *, const char *);
  125. void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
  126. int flags; ///< a combination or WRITER_FLAG_*
  127. } Writer;
  128. struct WriterContext {
  129. const AVClass *class; ///< class of the writer
  130. const Writer *writer; ///< the Writer of which this is an instance
  131. char *name; ///< name of this writer instance
  132. void *priv; ///< private data for use by the filter
  133. unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
  134. unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
  135. unsigned int nb_chapter; ///< number of the chapter, starting at 0
  136. };
  137. static const char *writer_get_name(void *p)
  138. {
  139. WriterContext *wctx = p;
  140. return wctx->writer->name;
  141. }
  142. static const AVClass writer_class = {
  143. "Writer",
  144. writer_get_name,
  145. NULL,
  146. LIBAVUTIL_VERSION_INT,
  147. };
  148. static void writer_close(WriterContext **wctx)
  149. {
  150. if (!*wctx)
  151. return;
  152. if ((*wctx)->writer->uninit)
  153. (*wctx)->writer->uninit(*wctx);
  154. av_freep(&((*wctx)->priv));
  155. av_freep(wctx);
  156. }
  157. static int writer_open(WriterContext **wctx, const Writer *writer,
  158. const char *args, void *opaque)
  159. {
  160. int ret = 0;
  161. if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
  162. ret = AVERROR(ENOMEM);
  163. goto fail;
  164. }
  165. if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
  166. ret = AVERROR(ENOMEM);
  167. goto fail;
  168. }
  169. (*wctx)->class = &writer_class;
  170. (*wctx)->writer = writer;
  171. if ((*wctx)->writer->init)
  172. ret = (*wctx)->writer->init(*wctx, args, opaque);
  173. if (ret < 0)
  174. goto fail;
  175. return 0;
  176. fail:
  177. writer_close(wctx);
  178. return ret;
  179. }
  180. static inline void writer_print_header(WriterContext *wctx)
  181. {
  182. if (wctx->writer->print_header)
  183. wctx->writer->print_header(wctx);
  184. wctx->nb_chapter = 0;
  185. }
  186. static inline void writer_print_footer(WriterContext *wctx)
  187. {
  188. if (wctx->writer->print_footer)
  189. wctx->writer->print_footer(wctx);
  190. }
  191. static inline void writer_print_chapter_header(WriterContext *wctx,
  192. const char *header)
  193. {
  194. if (wctx->writer->print_chapter_header)
  195. wctx->writer->print_chapter_header(wctx, header);
  196. wctx->nb_section = 0;
  197. }
  198. static inline void writer_print_chapter_footer(WriterContext *wctx,
  199. const char *footer)
  200. {
  201. if (wctx->writer->print_chapter_footer)
  202. wctx->writer->print_chapter_footer(wctx, footer);
  203. wctx->nb_chapter++;
  204. }
  205. static inline void writer_print_section_header(WriterContext *wctx,
  206. const char *header)
  207. {
  208. if (wctx->writer->print_section_header)
  209. wctx->writer->print_section_header(wctx, header);
  210. wctx->nb_item = 0;
  211. }
  212. static inline void writer_print_section_footer(WriterContext *wctx,
  213. const char *footer)
  214. {
  215. if (wctx->writer->print_section_footer)
  216. wctx->writer->print_section_footer(wctx, footer);
  217. wctx->nb_section++;
  218. }
  219. static inline void writer_print_integer(WriterContext *wctx,
  220. const char *key, long long int val)
  221. {
  222. wctx->writer->print_integer(wctx, key, val);
  223. wctx->nb_item++;
  224. }
  225. static inline void writer_print_string(WriterContext *wctx,
  226. const char *key, const char *val, int opt)
  227. {
  228. if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
  229. return;
  230. wctx->writer->print_string(wctx, key, val);
  231. wctx->nb_item++;
  232. }
  233. static void writer_print_time(WriterContext *wctx, const char *key,
  234. int64_t ts, const AVRational *time_base)
  235. {
  236. char buf[128];
  237. if (ts == AV_NOPTS_VALUE) {
  238. writer_print_string(wctx, key, "N/A", 1);
  239. } else {
  240. double d = ts * av_q2d(*time_base);
  241. value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
  242. writer_print_string(wctx, key, buf, 0);
  243. }
  244. }
  245. static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
  246. {
  247. if (ts == AV_NOPTS_VALUE) {
  248. writer_print_string(wctx, key, "N/A", 1);
  249. } else {
  250. writer_print_integer(wctx, key, ts);
  251. }
  252. }
  253. static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
  254. {
  255. wctx->writer->show_tags(wctx, dict);
  256. }
  257. #define MAX_REGISTERED_WRITERS_NB 64
  258. static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
  259. static int writer_register(const Writer *writer)
  260. {
  261. static int next_registered_writer_idx = 0;
  262. if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
  263. return AVERROR(ENOMEM);
  264. registered_writers[next_registered_writer_idx++] = writer;
  265. return 0;
  266. }
  267. static const Writer *writer_get_by_name(const char *name)
  268. {
  269. int i;
  270. for (i = 0; registered_writers[i]; i++)
  271. if (!strcmp(registered_writers[i]->name, name))
  272. return registered_writers[i];
  273. return NULL;
  274. }
  275. /* Print helpers */
  276. struct print_buf {
  277. char *s;
  278. int len;
  279. };
  280. static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
  281. {
  282. va_list va;
  283. int len;
  284. va_start(va, fmt);
  285. len = vsnprintf(NULL, 0, fmt, va);
  286. va_end(va);
  287. if (len < 0)
  288. goto fail;
  289. if (pbuf->len < len) {
  290. char *p = av_realloc(pbuf->s, len + 1);
  291. if (!p)
  292. goto fail;
  293. pbuf->s = p;
  294. pbuf->len = len;
  295. }
  296. va_start(va, fmt);
  297. len = vsnprintf(pbuf->s, len + 1, fmt, va);
  298. va_end(va);
  299. if (len < 0)
  300. goto fail;
  301. return pbuf->s;
  302. fail:
  303. av_freep(&pbuf->s);
  304. pbuf->len = 0;
  305. return NULL;
  306. }
  307. #define ESCAPE_INIT_BUF_SIZE 256
  308. #define ESCAPE_CHECK_SIZE(src, size, max_size) \
  309. if (size > max_size) { \
  310. char buf[64]; \
  311. snprintf(buf, sizeof(buf), "%s", src); \
  312. av_log(log_ctx, AV_LOG_WARNING, \
  313. "String '%s...' with is too big\n", buf); \
  314. return "FFPROBE_TOO_BIG_STRING"; \
  315. }
  316. #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
  317. if (*dst_size_p < size) { \
  318. char *q = av_realloc(*dst_p, size); \
  319. if (!q) { \
  320. char buf[64]; \
  321. snprintf(buf, sizeof(buf), "%s", src); \
  322. av_log(log_ctx, AV_LOG_WARNING, \
  323. "String '%s...' could not be escaped\n", buf); \
  324. return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
  325. } \
  326. *dst_size_p = size; \
  327. *dst = q; \
  328. }
  329. /* WRITERS */
  330. /* Default output */
  331. static void default_print_footer(WriterContext *wctx)
  332. {
  333. printf("\n");
  334. }
  335. static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
  336. {
  337. if (wctx->nb_chapter)
  338. printf("\n");
  339. }
  340. /* lame uppercasing routine, assumes the string is lower case ASCII */
  341. static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
  342. {
  343. int i;
  344. for (i = 0; src[i] && i < dst_size-1; i++)
  345. dst[i] = src[i]-32;
  346. dst[i] = 0;
  347. return dst;
  348. }
  349. static void default_print_section_header(WriterContext *wctx, const char *section)
  350. {
  351. char buf[32];
  352. if (wctx->nb_section)
  353. printf("\n");
  354. printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
  355. }
  356. static void default_print_section_footer(WriterContext *wctx, const char *section)
  357. {
  358. char buf[32];
  359. printf("[/%s]", upcase_string(buf, sizeof(buf), section));
  360. }
  361. static void default_print_str(WriterContext *wctx, const char *key, const char *value)
  362. {
  363. printf("%s=%s\n", key, value);
  364. }
  365. static void default_print_int(WriterContext *wctx, const char *key, long long int value)
  366. {
  367. printf("%s=%lld\n", key, value);
  368. }
  369. static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
  370. {
  371. AVDictionaryEntry *tag = NULL;
  372. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  373. printf("TAG:");
  374. writer_print_string(wctx, tag->key, tag->value, 0);
  375. }
  376. }
  377. static const Writer default_writer = {
  378. .name = "default",
  379. .print_footer = default_print_footer,
  380. .print_chapter_header = default_print_chapter_header,
  381. .print_section_header = default_print_section_header,
  382. .print_section_footer = default_print_section_footer,
  383. .print_integer = default_print_int,
  384. .print_string = default_print_str,
  385. .show_tags = default_show_tags,
  386. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  387. };
  388. /* Compact output */
  389. /**
  390. * Escape \n, \r, \\ and sep characters contained in s, and print the
  391. * resulting string.
  392. */
  393. static const char *c_escape_str(char **dst, size_t *dst_size,
  394. const char *src, const char sep, void *log_ctx)
  395. {
  396. const char *p;
  397. char *q;
  398. size_t size = 1;
  399. /* precompute size */
  400. for (p = src; *p; p++, size++) {
  401. ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-2);
  402. if (*p == '\n' || *p == '\r' || *p == '\\')
  403. size++;
  404. }
  405. ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
  406. q = *dst;
  407. for (p = src; *p; p++) {
  408. switch (*src) {
  409. case '\n': *q++ = '\\'; *q++ = 'n'; break;
  410. case '\r': *q++ = '\\'; *q++ = 'r'; break;
  411. case '\\': *q++ = '\\'; *q++ = '\\'; break;
  412. default:
  413. if (*p == sep)
  414. *q++ = '\\';
  415. *q++ = *p;
  416. }
  417. }
  418. *q = 0;
  419. return *dst;
  420. }
  421. /**
  422. * Quote fields containing special characters, check RFC4180.
  423. */
  424. static const char *csv_escape_str(char **dst, size_t *dst_size,
  425. const char *src, const char sep, void *log_ctx)
  426. {
  427. const char *p;
  428. char *q;
  429. size_t size = 1;
  430. int quote = 0;
  431. /* precompute size */
  432. for (p = src; *p; p++, size++) {
  433. ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-4);
  434. if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
  435. if (!quote) {
  436. quote = 1;
  437. size += 2;
  438. }
  439. if (*p == '"')
  440. size++;
  441. }
  442. ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
  443. q = *dst;
  444. p = src;
  445. if (quote)
  446. *q++ = '\"';
  447. while (*p) {
  448. if (*p == '"')
  449. *q++ = '\"';
  450. *q++ = *p++;
  451. }
  452. if (quote)
  453. *q++ = '\"';
  454. *q = 0;
  455. return *dst;
  456. }
  457. static const char *none_escape_str(char **dst, size_t *dst_size,
  458. const char *src, const char sep, void *log_ctx)
  459. {
  460. return src;
  461. }
  462. typedef struct CompactContext {
  463. const AVClass *class;
  464. char *item_sep_str;
  465. char item_sep;
  466. int nokey;
  467. char *buf;
  468. size_t buf_size;
  469. char *escape_mode_str;
  470. const char * (*escape_str)(char **dst, size_t *dst_size,
  471. const char *src, const char sep, void *log_ctx);
  472. } CompactContext;
  473. #define OFFSET(x) offsetof(CompactContext, x)
  474. static const AVOption compact_options[]= {
  475. {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  476. {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  477. {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  478. {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  479. {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  480. {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  481. {NULL},
  482. };
  483. static const char *compact_get_name(void *ctx)
  484. {
  485. return "compact";
  486. }
  487. static const AVClass compact_class = {
  488. "CompactContext",
  489. compact_get_name,
  490. compact_options
  491. };
  492. static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
  493. {
  494. CompactContext *compact = wctx->priv;
  495. int err;
  496. compact->class = &compact_class;
  497. av_opt_set_defaults(compact);
  498. if (args &&
  499. (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
  500. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  501. return err;
  502. }
  503. if (strlen(compact->item_sep_str) != 1) {
  504. av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
  505. compact->item_sep_str);
  506. return AVERROR(EINVAL);
  507. }
  508. compact->item_sep = compact->item_sep_str[0];
  509. compact->buf_size = ESCAPE_INIT_BUF_SIZE;
  510. if (!(compact->buf = av_malloc(compact->buf_size)))
  511. return AVERROR(ENOMEM);
  512. if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
  513. else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
  514. else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
  515. else {
  516. av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
  517. return AVERROR(EINVAL);
  518. }
  519. return 0;
  520. }
  521. static av_cold void compact_uninit(WriterContext *wctx)
  522. {
  523. CompactContext *compact = wctx->priv;
  524. av_freep(&compact->item_sep_str);
  525. av_freep(&compact->buf);
  526. av_freep(&compact->escape_mode_str);
  527. }
  528. static void compact_print_section_header(WriterContext *wctx, const char *section)
  529. {
  530. CompactContext *compact = wctx->priv;
  531. printf("%s%c", section, compact->item_sep);
  532. }
  533. static void compact_print_section_footer(WriterContext *wctx, const char *section)
  534. {
  535. printf("\n");
  536. }
  537. static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
  538. {
  539. CompactContext *compact = wctx->priv;
  540. if (wctx->nb_item) printf("%c", compact->item_sep);
  541. if (!compact->nokey)
  542. printf("%s=", key);
  543. printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
  544. value, compact->item_sep, wctx));
  545. }
  546. static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
  547. {
  548. CompactContext *compact = wctx->priv;
  549. if (wctx->nb_item) printf("%c", compact->item_sep);
  550. if (!compact->nokey)
  551. printf("%s=", key);
  552. printf("%lld", value);
  553. }
  554. static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
  555. {
  556. CompactContext *compact = wctx->priv;
  557. AVDictionaryEntry *tag = NULL;
  558. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  559. if (wctx->nb_item) printf("%c", compact->item_sep);
  560. if (!compact->nokey)
  561. printf("tag:%s=", compact->escape_str(&compact->buf, &compact->buf_size,
  562. tag->key, compact->item_sep, wctx));
  563. printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
  564. tag->value, compact->item_sep, wctx));
  565. }
  566. }
  567. static const Writer compact_writer = {
  568. .name = "compact",
  569. .priv_size = sizeof(CompactContext),
  570. .init = compact_init,
  571. .uninit = compact_uninit,
  572. .print_section_header = compact_print_section_header,
  573. .print_section_footer = compact_print_section_footer,
  574. .print_integer = compact_print_int,
  575. .print_string = compact_print_str,
  576. .show_tags = compact_show_tags,
  577. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  578. };
  579. /* CSV output */
  580. static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
  581. {
  582. return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
  583. }
  584. static const Writer csv_writer = {
  585. .name = "csv",
  586. .priv_size = sizeof(CompactContext),
  587. .init = csv_init,
  588. .uninit = compact_uninit,
  589. .print_section_header = compact_print_section_header,
  590. .print_section_footer = compact_print_section_footer,
  591. .print_integer = compact_print_int,
  592. .print_string = compact_print_str,
  593. .show_tags = compact_show_tags,
  594. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  595. };
  596. /* JSON output */
  597. typedef struct {
  598. int multiple_entries; ///< tells if the given chapter requires multiple entries
  599. char *buf;
  600. size_t buf_size;
  601. } JSONContext;
  602. static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
  603. {
  604. JSONContext *json = wctx->priv;
  605. json->buf_size = ESCAPE_INIT_BUF_SIZE;
  606. if (!(json->buf = av_malloc(json->buf_size)))
  607. return AVERROR(ENOMEM);
  608. return 0;
  609. }
  610. static av_cold void json_uninit(WriterContext *wctx)
  611. {
  612. JSONContext *json = wctx->priv;
  613. av_freep(&json->buf);
  614. }
  615. static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
  616. void *log_ctx)
  617. {
  618. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  619. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  620. const char *p;
  621. char *q;
  622. size_t size = 1;
  623. // compute the length of the escaped string
  624. for (p = src; *p; p++) {
  625. ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
  626. if (strchr(json_escape, *p)) size += 2; // simple escape
  627. else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
  628. else size += 1; // char copy
  629. }
  630. ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
  631. q = *dst;
  632. for (p = src; *p; p++) {
  633. char *s = strchr(json_escape, *p);
  634. if (s) {
  635. *q++ = '\\';
  636. *q++ = json_subst[s - json_escape];
  637. } else if ((unsigned char)*p < 32) {
  638. snprintf(q, 7, "\\u00%02x", *p & 0xff);
  639. q += 6;
  640. } else {
  641. *q++ = *p;
  642. }
  643. }
  644. *q = 0;
  645. return *dst;
  646. }
  647. static void json_print_header(WriterContext *wctx)
  648. {
  649. printf("{");
  650. }
  651. static void json_print_footer(WriterContext *wctx)
  652. {
  653. printf("\n}\n");
  654. }
  655. static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
  656. {
  657. JSONContext *json = wctx->priv;
  658. if (wctx->nb_chapter)
  659. printf(",");
  660. json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
  661. printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
  662. json->multiple_entries ? " [" : " ");
  663. }
  664. static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
  665. {
  666. JSONContext *json = wctx->priv;
  667. if (json->multiple_entries)
  668. printf("]");
  669. }
  670. static void json_print_section_header(WriterContext *wctx, const char *section)
  671. {
  672. if (wctx->nb_section) printf(",");
  673. printf("{\n");
  674. }
  675. static void json_print_section_footer(WriterContext *wctx, const char *section)
  676. {
  677. printf("\n }");
  678. }
  679. static inline void json_print_item_str(WriterContext *wctx,
  680. const char *key, const char *value,
  681. const char *indent)
  682. {
  683. JSONContext *json = wctx->priv;
  684. printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
  685. printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
  686. }
  687. #define INDENT " "
  688. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  689. {
  690. if (wctx->nb_item) printf(",\n");
  691. json_print_item_str(wctx, key, value, INDENT);
  692. }
  693. static void json_print_int(WriterContext *wctx, const char *key, long long int value)
  694. {
  695. JSONContext *json = wctx->priv;
  696. if (wctx->nb_item) printf(",\n");
  697. printf(INDENT "\"%s\": %lld",
  698. json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
  699. }
  700. static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
  701. {
  702. AVDictionaryEntry *tag = NULL;
  703. int is_first = 1;
  704. if (!dict)
  705. return;
  706. printf(",\n" INDENT "\"tags\": {\n");
  707. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  708. if (is_first) is_first = 0;
  709. else printf(",\n");
  710. json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
  711. }
  712. printf("\n }");
  713. }
  714. static const Writer json_writer = {
  715. .name = "json",
  716. .priv_size = sizeof(JSONContext),
  717. .init = json_init,
  718. .uninit = json_uninit,
  719. .print_header = json_print_header,
  720. .print_footer = json_print_footer,
  721. .print_chapter_header = json_print_chapter_header,
  722. .print_chapter_footer = json_print_chapter_footer,
  723. .print_section_header = json_print_section_header,
  724. .print_section_footer = json_print_section_footer,
  725. .print_integer = json_print_int,
  726. .print_string = json_print_str,
  727. .show_tags = json_show_tags,
  728. };
  729. /* XML output */
  730. typedef struct {
  731. const AVClass *class;
  732. int within_tag;
  733. int multiple_entries; ///< tells if the given chapter requires multiple entries
  734. int indent_level;
  735. int fully_qualified;
  736. int xsd_strict;
  737. char *buf;
  738. size_t buf_size;
  739. } XMLContext;
  740. #undef OFFSET
  741. #define OFFSET(x) offsetof(XMLContext, x)
  742. static const AVOption xml_options[] = {
  743. {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  744. {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  745. {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  746. {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  747. {NULL},
  748. };
  749. static const char *xml_get_name(void *ctx)
  750. {
  751. return "xml";
  752. }
  753. static const AVClass xml_class = {
  754. "XMLContext",
  755. xml_get_name,
  756. xml_options
  757. };
  758. static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
  759. {
  760. XMLContext *xml = wctx->priv;
  761. int err;
  762. xml->class = &xml_class;
  763. av_opt_set_defaults(xml);
  764. if (args &&
  765. (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
  766. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  767. return err;
  768. }
  769. if (xml->xsd_strict) {
  770. xml->fully_qualified = 1;
  771. #define CHECK_COMPLIANCE(opt, opt_name) \
  772. if (opt) { \
  773. av_log(wctx, AV_LOG_ERROR, \
  774. "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
  775. "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
  776. return AVERROR(EINVAL); \
  777. }
  778. CHECK_COMPLIANCE(show_private_data, "private");
  779. CHECK_COMPLIANCE(show_value_unit, "unit");
  780. CHECK_COMPLIANCE(use_value_prefix, "prefix");
  781. }
  782. xml->buf_size = ESCAPE_INIT_BUF_SIZE;
  783. if (!(xml->buf = av_malloc(xml->buf_size)))
  784. return AVERROR(ENOMEM);
  785. return 0;
  786. }
  787. static av_cold void xml_uninit(WriterContext *wctx)
  788. {
  789. XMLContext *xml = wctx->priv;
  790. av_freep(&xml->buf);
  791. }
  792. static const char *xml_escape_str(char **dst, size_t *dst_size, const char *src,
  793. void *log_ctx)
  794. {
  795. const char *p;
  796. char *q;
  797. size_t size = 1;
  798. /* precompute size */
  799. for (p = src; *p; p++, size++) {
  800. ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-10);
  801. switch (*p) {
  802. case '&' : size += strlen("&amp;"); break;
  803. case '<' : size += strlen("&lt;"); break;
  804. case '>' : size += strlen("&gt;"); break;
  805. case '\"': size += strlen("&quot;"); break;
  806. case '\'': size += strlen("&apos;"); break;
  807. default: size++;
  808. }
  809. }
  810. ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
  811. #define COPY_STR(str) { \
  812. const char *s = str; \
  813. while (*s) \
  814. *q++ = *s++; \
  815. }
  816. p = src;
  817. q = *dst;
  818. while (*p) {
  819. switch (*p) {
  820. case '&' : COPY_STR("&amp;"); break;
  821. case '<' : COPY_STR("&lt;"); break;
  822. case '>' : COPY_STR("&gt;"); break;
  823. case '\"': COPY_STR("&quot;"); break;
  824. case '\'': COPY_STR("&apos;"); break;
  825. default: *q++ = *p;
  826. }
  827. p++;
  828. }
  829. *q = 0;
  830. return *dst;
  831. }
  832. static void xml_print_header(WriterContext *wctx)
  833. {
  834. XMLContext *xml = wctx->priv;
  835. const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  836. "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
  837. "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
  838. printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  839. printf("<%sffprobe%s>\n",
  840. xml->fully_qualified ? "ffprobe:" : "",
  841. xml->fully_qualified ? qual : "");
  842. xml->indent_level++;
  843. }
  844. static void xml_print_footer(WriterContext *wctx)
  845. {
  846. XMLContext *xml = wctx->priv;
  847. xml->indent_level--;
  848. printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  849. }
  850. #define XML_INDENT() { int i; for (i = 0; i < xml->indent_level; i++) printf(INDENT); }
  851. static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
  852. {
  853. XMLContext *xml = wctx->priv;
  854. if (wctx->nb_chapter)
  855. printf("\n");
  856. xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
  857. if (xml->multiple_entries) {
  858. XML_INDENT(); printf("<%s>\n", chapter);
  859. xml->indent_level++;
  860. }
  861. }
  862. static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
  863. {
  864. XMLContext *xml = wctx->priv;
  865. if (xml->multiple_entries) {
  866. xml->indent_level--;
  867. XML_INDENT(); printf("</%s>\n", chapter);
  868. }
  869. }
  870. static void xml_print_section_header(WriterContext *wctx, const char *section)
  871. {
  872. XMLContext *xml = wctx->priv;
  873. XML_INDENT(); printf("<%s ", section);
  874. xml->within_tag = 1;
  875. }
  876. static void xml_print_section_footer(WriterContext *wctx, const char *section)
  877. {
  878. XMLContext *xml = wctx->priv;
  879. if (xml->within_tag)
  880. printf("/>\n");
  881. else {
  882. XML_INDENT(); printf("</%s>\n", section);
  883. }
  884. }
  885. static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
  886. {
  887. XMLContext *xml = wctx->priv;
  888. if (wctx->nb_item)
  889. printf(" ");
  890. printf("%s=\"%s\"", key, xml_escape_str(&xml->buf, &xml->buf_size, value, wctx));
  891. }
  892. static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
  893. {
  894. if (wctx->nb_item)
  895. printf(" ");
  896. printf("%s=\"%lld\"", key, value);
  897. }
  898. static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
  899. {
  900. XMLContext *xml = wctx->priv;
  901. AVDictionaryEntry *tag = NULL;
  902. int is_first = 1;
  903. xml->indent_level++;
  904. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  905. if (is_first) {
  906. /* close section tag */
  907. printf(">\n");
  908. xml->within_tag = 0;
  909. is_first = 0;
  910. }
  911. XML_INDENT();
  912. printf("<tag key=\"%s\"",
  913. xml_escape_str(&xml->buf, &xml->buf_size, tag->key, wctx));
  914. printf(" value=\"%s\"/>\n",
  915. xml_escape_str(&xml->buf, &xml->buf_size, tag->value, wctx));
  916. }
  917. xml->indent_level--;
  918. }
  919. static Writer xml_writer = {
  920. .name = "xml",
  921. .priv_size = sizeof(XMLContext),
  922. .init = xml_init,
  923. .uninit = xml_uninit,
  924. .print_header = xml_print_header,
  925. .print_footer = xml_print_footer,
  926. .print_chapter_header = xml_print_chapter_header,
  927. .print_chapter_footer = xml_print_chapter_footer,
  928. .print_section_header = xml_print_section_header,
  929. .print_section_footer = xml_print_section_footer,
  930. .print_integer = xml_print_int,
  931. .print_string = xml_print_str,
  932. .show_tags = xml_show_tags,
  933. };
  934. static void writer_register_all(void)
  935. {
  936. static int initialized;
  937. if (initialized)
  938. return;
  939. initialized = 1;
  940. writer_register(&default_writer);
  941. writer_register(&compact_writer);
  942. writer_register(&csv_writer);
  943. writer_register(&json_writer);
  944. writer_register(&xml_writer);
  945. }
  946. #define print_fmt(k, f, ...) do { \
  947. if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
  948. writer_print_string(w, k, pbuf.s, 0); \
  949. } while (0)
  950. #define print_fmt_opt(k, f, ...) do { \
  951. if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
  952. writer_print_string(w, k, pbuf.s, 1); \
  953. } while (0)
  954. #define print_int(k, v) writer_print_integer(w, k, v)
  955. #define print_str(k, v) writer_print_string(w, k, v, 0)
  956. #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
  957. #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
  958. #define print_ts(k, v) writer_print_ts(w, k, v)
  959. #define print_val(k, v, u) writer_print_string(w, k, \
  960. value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
  961. #define print_section_header(s) writer_print_section_header(w, s)
  962. #define print_section_footer(s) writer_print_section_footer(w, s)
  963. #define show_tags(metadata) writer_show_tags(w, metadata)
  964. static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
  965. {
  966. char val_str[128];
  967. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  968. struct print_buf pbuf = {.s = NULL};
  969. const char *s;
  970. print_section_header("packet");
  971. s = av_get_media_type_string(st->codec->codec_type);
  972. if (s) print_str ("codec_type", s);
  973. else print_str_opt("codec_type", "unknown");
  974. print_int("stream_index", pkt->stream_index);
  975. print_ts ("pts", pkt->pts);
  976. print_time("pts_time", pkt->pts, &st->time_base);
  977. print_ts ("dts", pkt->dts);
  978. print_time("dts_time", pkt->dts, &st->time_base);
  979. print_ts ("duration", pkt->duration);
  980. print_time("duration_time", pkt->duration, &st->time_base);
  981. print_val("size", pkt->size, unit_byte_str);
  982. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  983. else print_str_opt("pos", "N/A");
  984. print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  985. print_section_footer("packet");
  986. av_free(pbuf.s);
  987. fflush(stdout);
  988. }
  989. static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  990. {
  991. AVPacket pkt;
  992. int i = 0;
  993. av_init_packet(&pkt);
  994. while (!av_read_frame(fmt_ctx, &pkt))
  995. show_packet(w, fmt_ctx, &pkt, i++);
  996. }
  997. static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
  998. {
  999. AVStream *stream = fmt_ctx->streams[stream_idx];
  1000. AVCodecContext *dec_ctx;
  1001. AVCodec *dec;
  1002. char val_str[128];
  1003. const char *s;
  1004. AVRational display_aspect_ratio;
  1005. struct print_buf pbuf = {.s = NULL};
  1006. print_section_header("stream");
  1007. print_int("index", stream->index);
  1008. if ((dec_ctx = stream->codec)) {
  1009. if ((dec = dec_ctx->codec)) {
  1010. print_str("codec_name", dec->name);
  1011. print_str("codec_long_name", dec->long_name);
  1012. } else {
  1013. print_str_opt("codec_name", "unknown");
  1014. print_str_opt("codec_long_name", "unknown");
  1015. }
  1016. s = av_get_media_type_string(dec_ctx->codec_type);
  1017. if (s) print_str ("codec_type", s);
  1018. else print_str_opt("codec_type", "unknown");
  1019. print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
  1020. /* print AVI/FourCC tag */
  1021. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  1022. print_str("codec_tag_string", val_str);
  1023. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  1024. switch (dec_ctx->codec_type) {
  1025. case AVMEDIA_TYPE_VIDEO:
  1026. print_int("width", dec_ctx->width);
  1027. print_int("height", dec_ctx->height);
  1028. print_int("has_b_frames", dec_ctx->has_b_frames);
  1029. if (dec_ctx->sample_aspect_ratio.num) {
  1030. print_fmt("sample_aspect_ratio", "%d:%d",
  1031. dec_ctx->sample_aspect_ratio.num,
  1032. dec_ctx->sample_aspect_ratio.den);
  1033. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1034. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  1035. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  1036. 1024*1024);
  1037. print_fmt("display_aspect_ratio", "%d:%d",
  1038. display_aspect_ratio.num,
  1039. display_aspect_ratio.den);
  1040. } else {
  1041. print_str_opt("sample_aspect_ratio", "N/A");
  1042. print_str_opt("display_aspect_ratio", "N/A");
  1043. }
  1044. s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
  1045. if (s) print_str ("pix_fmt", s);
  1046. else print_str_opt("pix_fmt", "unknown");
  1047. print_int("level", dec_ctx->level);
  1048. if (dec_ctx->timecode_frame_start >= 0) {
  1049. uint32_t tc = dec_ctx->timecode_frame_start;
  1050. print_fmt("timecode", "%02d:%02d:%02d%c%02d",
  1051. tc>>19 & 0x1f, // hours
  1052. tc>>13 & 0x3f, // minutes
  1053. tc>>6 & 0x3f, // seconds
  1054. tc & 1<<24 ? ';' : ':', // drop
  1055. tc & 0x3f); // frames
  1056. } else {
  1057. print_str_opt("timecode", "N/A");
  1058. }
  1059. break;
  1060. case AVMEDIA_TYPE_AUDIO:
  1061. s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
  1062. if (s) print_str ("sample_fmt", s);
  1063. else print_str_opt("sample_fmt", "unknown");
  1064. print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
  1065. print_int("channels", dec_ctx->channels);
  1066. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  1067. break;
  1068. }
  1069. } else {
  1070. print_str_opt("codec_type", "unknown");
  1071. }
  1072. if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  1073. const AVOption *opt = NULL;
  1074. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  1075. uint8_t *str;
  1076. if (opt->flags) continue;
  1077. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  1078. print_str(opt->name, str);
  1079. av_free(str);
  1080. }
  1081. }
  1082. }
  1083. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  1084. else print_str_opt("id", "N/A");
  1085. print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
  1086. print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  1087. print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
  1088. print_time("start_time", stream->start_time, &stream->time_base);
  1089. print_time("duration", stream->duration, &stream->time_base);
  1090. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  1091. else print_str_opt("nb_frames", "N/A");
  1092. show_tags(stream->metadata);
  1093. print_section_footer("stream");
  1094. av_free(pbuf.s);
  1095. fflush(stdout);
  1096. }
  1097. static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  1098. {
  1099. int i;
  1100. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1101. show_stream(w, fmt_ctx, i);
  1102. }
  1103. static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  1104. {
  1105. char val_str[128];
  1106. int64_t size = avio_size(fmt_ctx->pb);
  1107. print_section_header("format");
  1108. print_str("filename", fmt_ctx->filename);
  1109. print_int("nb_streams", fmt_ctx->nb_streams);
  1110. print_str("format_name", fmt_ctx->iformat->name);
  1111. print_str("format_long_name", fmt_ctx->iformat->long_name);
  1112. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  1113. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  1114. if (size >= 0) print_val ("size", size, unit_byte_str);
  1115. else print_str_opt("size", "N/A");
  1116. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  1117. else print_str_opt("bit_rate", "N/A");
  1118. show_tags(fmt_ctx->metadata);
  1119. print_section_footer("format");
  1120. fflush(stdout);
  1121. }
  1122. static void show_error(WriterContext *w, int err)
  1123. {
  1124. char errbuf[128];
  1125. const char *errbuf_ptr = errbuf;
  1126. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  1127. errbuf_ptr = strerror(AVUNERROR(err));
  1128. writer_print_chapter_header(w, "error");
  1129. print_section_header("error");
  1130. print_int("code", err);
  1131. print_str("string", errbuf_ptr);
  1132. print_section_footer("error");
  1133. writer_print_chapter_footer(w, "error");
  1134. }
  1135. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  1136. {
  1137. int err, i;
  1138. AVFormatContext *fmt_ctx = NULL;
  1139. AVDictionaryEntry *t;
  1140. if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
  1141. print_error(filename, err);
  1142. return err;
  1143. }
  1144. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  1145. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  1146. return AVERROR_OPTION_NOT_FOUND;
  1147. }
  1148. /* fill the streams in the format context */
  1149. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  1150. print_error(filename, err);
  1151. return err;
  1152. }
  1153. av_dump_format(fmt_ctx, 0, filename, 0);
  1154. /* bind a decoder to each input stream */
  1155. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1156. AVStream *stream = fmt_ctx->streams[i];
  1157. AVCodec *codec;
  1158. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  1159. av_log(NULL, AV_LOG_ERROR, "Unsupported codec with id %d for input stream %d\n",
  1160. stream->codec->codec_id, stream->index);
  1161. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  1162. av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
  1163. stream->index);
  1164. }
  1165. }
  1166. *fmt_ctx_ptr = fmt_ctx;
  1167. return 0;
  1168. }
  1169. #define PRINT_CHAPTER(name) do { \
  1170. if (do_show_ ## name) { \
  1171. writer_print_chapter_header(wctx, #name); \
  1172. show_ ## name (wctx, fmt_ctx); \
  1173. writer_print_chapter_footer(wctx, #name); \
  1174. } \
  1175. } while (0)
  1176. static int probe_file(const char *filename)
  1177. {
  1178. AVFormatContext *fmt_ctx;
  1179. int ret;
  1180. const Writer *w;
  1181. char *buf;
  1182. char *w_name = NULL, *w_args = NULL;
  1183. WriterContext *wctx;
  1184. writer_register_all();
  1185. if (!print_format)
  1186. print_format = av_strdup("default");
  1187. w_name = av_strtok(print_format, "=", &buf);
  1188. w_args = buf;
  1189. w = writer_get_by_name(w_name);
  1190. if (!w) {
  1191. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  1192. ret = AVERROR(EINVAL);
  1193. goto end;
  1194. }
  1195. if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
  1196. goto end;
  1197. writer_print_header(wctx);
  1198. ret = open_input_file(&fmt_ctx, filename);
  1199. if (ret >= 0) {
  1200. PRINT_CHAPTER(packets);
  1201. PRINT_CHAPTER(streams);
  1202. PRINT_CHAPTER(format);
  1203. avformat_close_input(&fmt_ctx);
  1204. } else if (do_show_error) {
  1205. show_error(wctx, ret);
  1206. }
  1207. writer_print_footer(wctx);
  1208. writer_close(&wctx);
  1209. end:
  1210. av_freep(&print_format);
  1211. return ret;
  1212. }
  1213. static void show_usage(void)
  1214. {
  1215. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  1216. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  1217. av_log(NULL, AV_LOG_INFO, "\n");
  1218. }
  1219. static int opt_format(const char *opt, const char *arg)
  1220. {
  1221. iformat = av_find_input_format(arg);
  1222. if (!iformat) {
  1223. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  1224. return AVERROR(EINVAL);
  1225. }
  1226. return 0;
  1227. }
  1228. static void opt_input_file(void *optctx, const char *arg)
  1229. {
  1230. if (input_filename) {
  1231. av_log(NULL, AV_LOG_ERROR, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  1232. arg, input_filename);
  1233. exit(1);
  1234. }
  1235. if (!strcmp(arg, "-"))
  1236. arg = "pipe:";
  1237. input_filename = arg;
  1238. }
  1239. static int opt_help(const char *opt, const char *arg)
  1240. {
  1241. av_log_set_callback(log_callback_help);
  1242. show_usage();
  1243. show_help_options(options, "Main options:\n", 0, 0);
  1244. printf("\n");
  1245. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  1246. return 0;
  1247. }
  1248. static int opt_pretty(const char *opt, const char *arg)
  1249. {
  1250. show_value_unit = 1;
  1251. use_value_prefix = 1;
  1252. use_byte_value_binary_prefix = 1;
  1253. use_value_sexagesimal_format = 1;
  1254. return 0;
  1255. }
  1256. static const OptionDef options[] = {
  1257. #include "cmdutils_common_opts.h"
  1258. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  1259. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  1260. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  1261. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  1262. "use binary prefixes for byte units" },
  1263. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  1264. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  1265. { "pretty", 0, {(void*)&opt_pretty},
  1266. "prettify the format of displayed values, make it more human readable" },
  1267. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  1268. "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
  1269. { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
  1270. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  1271. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  1272. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  1273. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  1274. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  1275. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  1276. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  1277. { NULL, },
  1278. };
  1279. int main(int argc, char **argv)
  1280. {
  1281. int ret;
  1282. parse_loglevel(argc, argv, options);
  1283. av_register_all();
  1284. avformat_network_init();
  1285. init_opts();
  1286. #if CONFIG_AVDEVICE
  1287. avdevice_register_all();
  1288. #endif
  1289. show_banner(argc, argv, options);
  1290. parse_options(NULL, argc, argv, options, opt_input_file);
  1291. if (!input_filename) {
  1292. show_usage();
  1293. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  1294. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  1295. exit(1);
  1296. }
  1297. ret = probe_file(input_filename);
  1298. avformat_network_deinit();
  1299. return ret;
  1300. }