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.

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