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.

2137 lines
72KB

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