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.

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