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.

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