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.

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