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.

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