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.

2153 lines
73KB

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