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.

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