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.

1760 lines
57KB

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