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.

2167 lines
76KB

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