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.

1648 lines
53KB

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