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.

2134 lines
75KB

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