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.

1796 lines
59KB

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