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.

2927 lines
103KB

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