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.

3298 lines
120KB

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