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.

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