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.

2152 lines
73KB

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