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.

2190 lines
72KB

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