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.

3304 lines
119KB

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