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.

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