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.

2377 lines
85KB

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