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.

2151 lines
71KB

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