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.

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