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.

3280 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. const char *s;
  1552. int i;
  1553. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1554. writer_print_section_header(w, SECTION_ID_FRAME);
  1555. s = av_get_media_type_string(stream->codec->codec_type);
  1556. if (s) print_str ("media_type", s);
  1557. else print_str_opt("media_type", "unknown");
  1558. print_int("stream_index", stream->index);
  1559. print_int("key_frame", frame->key_frame);
  1560. print_ts ("pkt_pts", frame->pkt_pts);
  1561. print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
  1562. print_ts ("pkt_dts", frame->pkt_dts);
  1563. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  1564. print_ts ("best_effort_timestamp", av_frame_get_best_effort_timestamp(frame));
  1565. print_time("best_effort_timestamp_time", av_frame_get_best_effort_timestamp(frame), &stream->time_base);
  1566. print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
  1567. print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
  1568. if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
  1569. else print_str_opt("pkt_pos", "N/A");
  1570. if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
  1571. else print_str_opt("pkt_size", "N/A");
  1572. switch (stream->codec->codec_type) {
  1573. AVRational sar;
  1574. case AVMEDIA_TYPE_VIDEO:
  1575. print_int("width", frame->width);
  1576. print_int("height", frame->height);
  1577. s = av_get_pix_fmt_name(frame->format);
  1578. if (s) print_str ("pix_fmt", s);
  1579. else print_str_opt("pix_fmt", "unknown");
  1580. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
  1581. if (sar.num) {
  1582. print_q("sample_aspect_ratio", sar, ':');
  1583. } else {
  1584. print_str_opt("sample_aspect_ratio", "N/A");
  1585. }
  1586. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1587. print_int("coded_picture_number", frame->coded_picture_number);
  1588. print_int("display_picture_number", frame->display_picture_number);
  1589. print_int("interlaced_frame", frame->interlaced_frame);
  1590. print_int("top_field_first", frame->top_field_first);
  1591. print_int("repeat_pict", frame->repeat_pict);
  1592. break;
  1593. case AVMEDIA_TYPE_AUDIO:
  1594. s = av_get_sample_fmt_name(frame->format);
  1595. if (s) print_str ("sample_fmt", s);
  1596. else print_str_opt("sample_fmt", "unknown");
  1597. print_int("nb_samples", frame->nb_samples);
  1598. print_int("channels", av_frame_get_channels(frame));
  1599. if (av_frame_get_channel_layout(frame)) {
  1600. av_bprint_clear(&pbuf);
  1601. av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
  1602. av_frame_get_channel_layout(frame));
  1603. print_str ("channel_layout", pbuf.str);
  1604. } else
  1605. print_str_opt("channel_layout", "unknown");
  1606. break;
  1607. }
  1608. if (do_show_frame_tags)
  1609. show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
  1610. if (frame->nb_side_data) {
  1611. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_LIST);
  1612. for (i = 0; i < frame->nb_side_data; i++) {
  1613. AVFrameSideData *sd = frame->side_data[i];
  1614. const char *name;
  1615. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA);
  1616. name = av_frame_side_data_name(sd->type);
  1617. print_str("side_data_type", name ? name : "unknown");
  1618. print_int("side_data_size", sd->size);
  1619. if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  1620. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  1621. print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
  1622. }
  1623. writer_print_section_footer(w);
  1624. }
  1625. writer_print_section_footer(w);
  1626. }
  1627. writer_print_section_footer(w);
  1628. av_bprint_finalize(&pbuf, NULL);
  1629. fflush(stdout);
  1630. }
  1631. static av_always_inline int process_frame(WriterContext *w,
  1632. AVFormatContext *fmt_ctx,
  1633. AVFrame *frame, AVPacket *pkt)
  1634. {
  1635. AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
  1636. AVSubtitle sub;
  1637. int ret = 0, got_frame = 0;
  1638. if (dec_ctx->codec) {
  1639. switch (dec_ctx->codec_type) {
  1640. case AVMEDIA_TYPE_VIDEO:
  1641. ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
  1642. break;
  1643. case AVMEDIA_TYPE_AUDIO:
  1644. ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
  1645. break;
  1646. case AVMEDIA_TYPE_SUBTITLE:
  1647. ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
  1648. break;
  1649. }
  1650. }
  1651. if (ret < 0)
  1652. return ret;
  1653. ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
  1654. pkt->data += ret;
  1655. pkt->size -= ret;
  1656. if (got_frame) {
  1657. int is_sub = (dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE);
  1658. nb_streams_frames[pkt->stream_index]++;
  1659. if (do_show_frames)
  1660. if (is_sub)
  1661. show_subtitle(w, &sub, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
  1662. else
  1663. show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
  1664. if (is_sub)
  1665. avsubtitle_free(&sub);
  1666. }
  1667. return got_frame;
  1668. }
  1669. static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
  1670. {
  1671. av_log(log_ctx, log_level, "id:%d", interval->id);
  1672. if (interval->has_start) {
  1673. av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
  1674. av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
  1675. } else {
  1676. av_log(log_ctx, log_level, " start:N/A");
  1677. }
  1678. if (interval->has_end) {
  1679. av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
  1680. if (interval->duration_frames)
  1681. av_log(log_ctx, log_level, "#%"PRId64, interval->end);
  1682. else
  1683. av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
  1684. } else {
  1685. av_log(log_ctx, log_level, " end:N/A");
  1686. }
  1687. av_log(log_ctx, log_level, "\n");
  1688. }
  1689. static int read_interval_packets(WriterContext *w, AVFormatContext *fmt_ctx,
  1690. const ReadInterval *interval, int64_t *cur_ts)
  1691. {
  1692. AVPacket pkt, pkt1;
  1693. AVFrame *frame = NULL;
  1694. int ret = 0, i = 0, frame_count = 0;
  1695. int64_t start = -INT64_MAX, end = interval->end;
  1696. int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
  1697. av_init_packet(&pkt);
  1698. av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
  1699. log_read_interval(interval, NULL, AV_LOG_VERBOSE);
  1700. if (interval->has_start) {
  1701. int64_t target;
  1702. if (interval->start_is_offset) {
  1703. if (*cur_ts == AV_NOPTS_VALUE) {
  1704. av_log(NULL, AV_LOG_ERROR,
  1705. "Could not seek to relative position since current "
  1706. "timestamp is not defined\n");
  1707. ret = AVERROR(EINVAL);
  1708. goto end;
  1709. }
  1710. target = *cur_ts + interval->start;
  1711. } else {
  1712. target = interval->start;
  1713. }
  1714. av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
  1715. av_ts2timestr(target, &AV_TIME_BASE_Q));
  1716. if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
  1717. av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
  1718. interval->start, av_err2str(ret));
  1719. goto end;
  1720. }
  1721. }
  1722. frame = av_frame_alloc();
  1723. if (!frame) {
  1724. ret = AVERROR(ENOMEM);
  1725. goto end;
  1726. }
  1727. while (!av_read_frame(fmt_ctx, &pkt)) {
  1728. if (fmt_ctx->nb_streams > nb_streams) {
  1729. REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
  1730. REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
  1731. REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
  1732. nb_streams = fmt_ctx->nb_streams;
  1733. }
  1734. if (selected_streams[pkt.stream_index]) {
  1735. AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
  1736. if (pkt.pts != AV_NOPTS_VALUE)
  1737. *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
  1738. if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
  1739. start = *cur_ts;
  1740. has_start = 1;
  1741. }
  1742. if (has_start && !has_end && interval->end_is_offset) {
  1743. end = start + interval->end;
  1744. has_end = 1;
  1745. }
  1746. if (interval->end_is_offset && interval->duration_frames) {
  1747. if (frame_count >= interval->end)
  1748. break;
  1749. } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
  1750. break;
  1751. }
  1752. frame_count++;
  1753. if (do_read_packets) {
  1754. if (do_show_packets)
  1755. show_packet(w, fmt_ctx, &pkt, i++);
  1756. nb_streams_packets[pkt.stream_index]++;
  1757. }
  1758. if (do_read_frames) {
  1759. pkt1 = pkt;
  1760. while (pkt1.size && process_frame(w, fmt_ctx, frame, &pkt1) > 0);
  1761. }
  1762. }
  1763. av_packet_unref(&pkt);
  1764. }
  1765. av_init_packet(&pkt);
  1766. pkt.data = NULL;
  1767. pkt.size = 0;
  1768. //Flush remaining frames that are cached in the decoder
  1769. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1770. pkt.stream_index = i;
  1771. if (do_read_frames)
  1772. while (process_frame(w, fmt_ctx, frame, &pkt) > 0);
  1773. }
  1774. end:
  1775. av_frame_free(&frame);
  1776. if (ret < 0) {
  1777. av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
  1778. log_read_interval(interval, NULL, AV_LOG_ERROR);
  1779. }
  1780. return ret;
  1781. }
  1782. static int read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  1783. {
  1784. int i, ret = 0;
  1785. int64_t cur_ts = fmt_ctx->start_time;
  1786. if (read_intervals_nb == 0) {
  1787. ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
  1788. ret = read_interval_packets(w, fmt_ctx, &interval, &cur_ts);
  1789. } else {
  1790. for (i = 0; i < read_intervals_nb; i++) {
  1791. ret = read_interval_packets(w, fmt_ctx, &read_intervals[i], &cur_ts);
  1792. if (ret < 0)
  1793. break;
  1794. }
  1795. }
  1796. return ret;
  1797. }
  1798. static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
  1799. {
  1800. AVStream *stream = fmt_ctx->streams[stream_idx];
  1801. AVCodecContext *dec_ctx;
  1802. const AVCodec *dec;
  1803. char val_str[128];
  1804. const char *s;
  1805. AVRational sar, dar;
  1806. AVBPrint pbuf;
  1807. const AVCodecDescriptor *cd;
  1808. int ret = 0;
  1809. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1810. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
  1811. print_int("index", stream->index);
  1812. if ((dec_ctx = stream->codec)) {
  1813. const char *profile = NULL;
  1814. dec = dec_ctx->codec;
  1815. if (dec) {
  1816. print_str("codec_name", dec->name);
  1817. if (!do_bitexact) {
  1818. if (dec->long_name) print_str ("codec_long_name", dec->long_name);
  1819. else print_str_opt("codec_long_name", "unknown");
  1820. }
  1821. } else if ((cd = avcodec_descriptor_get(stream->codec->codec_id))) {
  1822. print_str_opt("codec_name", cd->name);
  1823. if (!do_bitexact) {
  1824. print_str_opt("codec_long_name",
  1825. cd->long_name ? cd->long_name : "unknown");
  1826. }
  1827. } else {
  1828. print_str_opt("codec_name", "unknown");
  1829. if (!do_bitexact) {
  1830. print_str_opt("codec_long_name", "unknown");
  1831. }
  1832. }
  1833. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  1834. print_str("profile", profile);
  1835. else
  1836. print_str_opt("profile", "unknown");
  1837. s = av_get_media_type_string(dec_ctx->codec_type);
  1838. if (s) print_str ("codec_type", s);
  1839. else print_str_opt("codec_type", "unknown");
  1840. print_q("codec_time_base", dec_ctx->time_base, '/');
  1841. /* print AVI/FourCC tag */
  1842. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  1843. print_str("codec_tag_string", val_str);
  1844. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  1845. switch (dec_ctx->codec_type) {
  1846. case AVMEDIA_TYPE_VIDEO:
  1847. print_int("width", dec_ctx->width);
  1848. print_int("height", dec_ctx->height);
  1849. print_int("coded_width", dec_ctx->coded_width);
  1850. print_int("coded_height", dec_ctx->coded_height);
  1851. print_int("has_b_frames", dec_ctx->has_b_frames);
  1852. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
  1853. if (sar.den) {
  1854. print_q("sample_aspect_ratio", sar, ':');
  1855. av_reduce(&dar.num, &dar.den,
  1856. dec_ctx->width * sar.num,
  1857. dec_ctx->height * sar.den,
  1858. 1024*1024);
  1859. print_q("display_aspect_ratio", dar, ':');
  1860. } else {
  1861. print_str_opt("sample_aspect_ratio", "N/A");
  1862. print_str_opt("display_aspect_ratio", "N/A");
  1863. }
  1864. s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
  1865. if (s) print_str ("pix_fmt", s);
  1866. else print_str_opt("pix_fmt", "unknown");
  1867. print_int("level", dec_ctx->level);
  1868. if (dec_ctx->color_range != AVCOL_RANGE_UNSPECIFIED)
  1869. print_str ("color_range", av_color_range_name(dec_ctx->color_range));
  1870. else
  1871. print_str_opt("color_range", "N/A");
  1872. s = av_get_colorspace_name(dec_ctx->colorspace);
  1873. if (s) print_str ("color_space", s);
  1874. else print_str_opt("color_space", "unknown");
  1875. if (dec_ctx->color_trc != AVCOL_TRC_UNSPECIFIED)
  1876. print_str("color_transfer", av_color_transfer_name(dec_ctx->color_trc));
  1877. else
  1878. print_str_opt("color_transfer", av_color_transfer_name(dec_ctx->color_trc));
  1879. if (dec_ctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
  1880. print_str("color_primaries", av_color_primaries_name(dec_ctx->color_primaries));
  1881. else
  1882. print_str_opt("color_primaries", av_color_primaries_name(dec_ctx->color_primaries));
  1883. if (dec_ctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1884. print_str("chroma_location", av_chroma_location_name(dec_ctx->chroma_sample_location));
  1885. else
  1886. print_str_opt("chroma_location", av_chroma_location_name(dec_ctx->chroma_sample_location));
  1887. if (dec_ctx->timecode_frame_start >= 0) {
  1888. char tcbuf[AV_TIMECODE_STR_SIZE];
  1889. av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
  1890. print_str("timecode", tcbuf);
  1891. } else {
  1892. print_str_opt("timecode", "N/A");
  1893. }
  1894. print_int("refs", dec_ctx->refs);
  1895. break;
  1896. case AVMEDIA_TYPE_AUDIO:
  1897. s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
  1898. if (s) print_str ("sample_fmt", s);
  1899. else print_str_opt("sample_fmt", "unknown");
  1900. print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
  1901. print_int("channels", dec_ctx->channels);
  1902. if (dec_ctx->channel_layout) {
  1903. av_bprint_clear(&pbuf);
  1904. av_bprint_channel_layout(&pbuf, dec_ctx->channels, dec_ctx->channel_layout);
  1905. print_str ("channel_layout", pbuf.str);
  1906. } else {
  1907. print_str_opt("channel_layout", "unknown");
  1908. }
  1909. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  1910. break;
  1911. case AVMEDIA_TYPE_SUBTITLE:
  1912. if (dec_ctx->width)
  1913. print_int("width", dec_ctx->width);
  1914. else
  1915. print_str_opt("width", "N/A");
  1916. if (dec_ctx->height)
  1917. print_int("height", dec_ctx->height);
  1918. else
  1919. print_str_opt("height", "N/A");
  1920. break;
  1921. }
  1922. } else {
  1923. print_str_opt("codec_type", "unknown");
  1924. }
  1925. if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  1926. const AVOption *opt = NULL;
  1927. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  1928. uint8_t *str;
  1929. if (opt->flags) continue;
  1930. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  1931. print_str(opt->name, str);
  1932. av_free(str);
  1933. }
  1934. }
  1935. }
  1936. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  1937. else print_str_opt("id", "N/A");
  1938. print_q("r_frame_rate", stream->r_frame_rate, '/');
  1939. print_q("avg_frame_rate", stream->avg_frame_rate, '/');
  1940. print_q("time_base", stream->time_base, '/');
  1941. print_ts ("start_pts", stream->start_time);
  1942. print_time("start_time", stream->start_time, &stream->time_base);
  1943. print_ts ("duration_ts", stream->duration);
  1944. print_time("duration", stream->duration, &stream->time_base);
  1945. if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
  1946. else print_str_opt("bit_rate", "N/A");
  1947. if (dec_ctx->rc_max_rate > 0) print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
  1948. else print_str_opt("max_bit_rate", "N/A");
  1949. if (dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
  1950. else print_str_opt("bits_per_raw_sample", "N/A");
  1951. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  1952. else print_str_opt("nb_frames", "N/A");
  1953. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  1954. else print_str_opt("nb_read_frames", "N/A");
  1955. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  1956. else print_str_opt("nb_read_packets", "N/A");
  1957. if (do_show_data)
  1958. writer_print_data(w, "extradata", dec_ctx->extradata,
  1959. dec_ctx->extradata_size);
  1960. writer_print_data_hash(w, "extradata_hash", dec_ctx->extradata,
  1961. dec_ctx->extradata_size);
  1962. /* Print disposition information */
  1963. #define PRINT_DISPOSITION(flagname, name) do { \
  1964. print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
  1965. } while (0)
  1966. if (do_show_stream_disposition) {
  1967. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
  1968. PRINT_DISPOSITION(DEFAULT, "default");
  1969. PRINT_DISPOSITION(DUB, "dub");
  1970. PRINT_DISPOSITION(ORIGINAL, "original");
  1971. PRINT_DISPOSITION(COMMENT, "comment");
  1972. PRINT_DISPOSITION(LYRICS, "lyrics");
  1973. PRINT_DISPOSITION(KARAOKE, "karaoke");
  1974. PRINT_DISPOSITION(FORCED, "forced");
  1975. PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
  1976. PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
  1977. PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
  1978. PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
  1979. writer_print_section_footer(w);
  1980. }
  1981. if (do_show_stream_tags)
  1982. ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
  1983. if (stream->nb_side_data) {
  1984. int i;
  1985. writer_print_section_header(w, SECTION_ID_STREAM_SIDE_DATA_LIST);
  1986. for (i = 0; i < stream->nb_side_data; i++) {
  1987. AVPacketSideData *sd = &stream->side_data[i];
  1988. const char *name = av_packet_side_data_name(sd->type);
  1989. writer_print_section_header(w, SECTION_ID_STREAM_SIDE_DATA);
  1990. print_str("side_data_type", name ? name : "unknown");
  1991. print_int("side_data_size", sd->size);
  1992. if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  1993. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  1994. print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
  1995. }
  1996. writer_print_section_footer(w);
  1997. }
  1998. writer_print_section_footer(w);
  1999. }
  2000. writer_print_section_footer(w);
  2001. av_bprint_finalize(&pbuf, NULL);
  2002. fflush(stdout);
  2003. return ret;
  2004. }
  2005. static int show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  2006. {
  2007. int i, ret = 0;
  2008. writer_print_section_header(w, SECTION_ID_STREAMS);
  2009. for (i = 0; i < fmt_ctx->nb_streams; i++)
  2010. if (selected_streams[i]) {
  2011. ret = show_stream(w, fmt_ctx, i, 0);
  2012. if (ret < 0)
  2013. break;
  2014. }
  2015. writer_print_section_footer(w);
  2016. return ret;
  2017. }
  2018. static int show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
  2019. {
  2020. int i, ret = 0;
  2021. writer_print_section_header(w, SECTION_ID_PROGRAM);
  2022. print_int("program_id", program->id);
  2023. print_int("program_num", program->program_num);
  2024. print_int("nb_streams", program->nb_stream_indexes);
  2025. print_int("pmt_pid", program->pmt_pid);
  2026. print_int("pcr_pid", program->pcr_pid);
  2027. print_ts("start_pts", program->start_time);
  2028. print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
  2029. print_ts("end_pts", program->end_time);
  2030. print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
  2031. if (do_show_program_tags)
  2032. ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
  2033. if (ret < 0)
  2034. goto end;
  2035. writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
  2036. for (i = 0; i < program->nb_stream_indexes; i++) {
  2037. if (selected_streams[program->stream_index[i]]) {
  2038. ret = show_stream(w, fmt_ctx, program->stream_index[i], 1);
  2039. if (ret < 0)
  2040. break;
  2041. }
  2042. }
  2043. writer_print_section_footer(w);
  2044. end:
  2045. writer_print_section_footer(w);
  2046. return ret;
  2047. }
  2048. static int show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
  2049. {
  2050. int i, ret = 0;
  2051. writer_print_section_header(w, SECTION_ID_PROGRAMS);
  2052. for (i = 0; i < fmt_ctx->nb_programs; i++) {
  2053. AVProgram *program = fmt_ctx->programs[i];
  2054. if (!program)
  2055. continue;
  2056. ret = show_program(w, fmt_ctx, program);
  2057. if (ret < 0)
  2058. break;
  2059. }
  2060. writer_print_section_footer(w);
  2061. return ret;
  2062. }
  2063. static int show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
  2064. {
  2065. int i, ret = 0;
  2066. writer_print_section_header(w, SECTION_ID_CHAPTERS);
  2067. for (i = 0; i < fmt_ctx->nb_chapters; i++) {
  2068. AVChapter *chapter = fmt_ctx->chapters[i];
  2069. writer_print_section_header(w, SECTION_ID_CHAPTER);
  2070. print_int("id", chapter->id);
  2071. print_q ("time_base", chapter->time_base, '/');
  2072. print_int("start", chapter->start);
  2073. print_time("start_time", chapter->start, &chapter->time_base);
  2074. print_int("end", chapter->end);
  2075. print_time("end_time", chapter->end, &chapter->time_base);
  2076. if (do_show_chapter_tags)
  2077. ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
  2078. writer_print_section_footer(w);
  2079. }
  2080. writer_print_section_footer(w);
  2081. return ret;
  2082. }
  2083. static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  2084. {
  2085. char val_str[128];
  2086. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  2087. int ret = 0;
  2088. writer_print_section_header(w, SECTION_ID_FORMAT);
  2089. print_str_validate("filename", fmt_ctx->filename);
  2090. print_int("nb_streams", fmt_ctx->nb_streams);
  2091. print_int("nb_programs", fmt_ctx->nb_programs);
  2092. print_str("format_name", fmt_ctx->iformat->name);
  2093. if (!do_bitexact) {
  2094. if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
  2095. else print_str_opt("format_long_name", "unknown");
  2096. }
  2097. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  2098. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  2099. if (size >= 0) print_val ("size", size, unit_byte_str);
  2100. else print_str_opt("size", "N/A");
  2101. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  2102. else print_str_opt("bit_rate", "N/A");
  2103. print_int("probe_score", av_format_get_probe_score(fmt_ctx));
  2104. if (do_show_format_tags)
  2105. ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
  2106. writer_print_section_footer(w);
  2107. fflush(stdout);
  2108. return ret;
  2109. }
  2110. static void show_error(WriterContext *w, int err)
  2111. {
  2112. char errbuf[128];
  2113. const char *errbuf_ptr = errbuf;
  2114. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  2115. errbuf_ptr = strerror(AVUNERROR(err));
  2116. writer_print_section_header(w, SECTION_ID_ERROR);
  2117. print_int("code", err);
  2118. print_str("string", errbuf_ptr);
  2119. writer_print_section_footer(w);
  2120. }
  2121. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  2122. {
  2123. int err, i, orig_nb_streams;
  2124. AVFormatContext *fmt_ctx = NULL;
  2125. AVDictionaryEntry *t;
  2126. AVDictionary **opts;
  2127. int scan_all_pmts_set = 0;
  2128. if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  2129. av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  2130. scan_all_pmts_set = 1;
  2131. }
  2132. if ((err = avformat_open_input(&fmt_ctx, filename,
  2133. iformat, &format_opts)) < 0) {
  2134. print_error(filename, err);
  2135. return err;
  2136. }
  2137. *fmt_ctx_ptr = fmt_ctx;
  2138. if (scan_all_pmts_set)
  2139. av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  2140. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2141. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  2142. return AVERROR_OPTION_NOT_FOUND;
  2143. }
  2144. /* fill the streams in the format context */
  2145. opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
  2146. orig_nb_streams = fmt_ctx->nb_streams;
  2147. err = avformat_find_stream_info(fmt_ctx, opts);
  2148. for (i = 0; i < orig_nb_streams; i++)
  2149. av_dict_free(&opts[i]);
  2150. av_freep(&opts);
  2151. if (err < 0) {
  2152. print_error(filename, err);
  2153. return err;
  2154. }
  2155. av_dump_format(fmt_ctx, 0, filename, 0);
  2156. /* bind a decoder to each input stream */
  2157. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  2158. AVStream *stream = fmt_ctx->streams[i];
  2159. AVCodec *codec;
  2160. if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
  2161. av_log(NULL, AV_LOG_WARNING,
  2162. "Failed to probe codec for input stream %d\n",
  2163. stream->index);
  2164. } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  2165. av_log(NULL, AV_LOG_WARNING,
  2166. "Unsupported codec with id %d for input stream %d\n",
  2167. stream->codec->codec_id, stream->index);
  2168. } else {
  2169. AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
  2170. fmt_ctx, stream, codec);
  2171. if (avcodec_open2(stream->codec, codec, &opts) < 0) {
  2172. av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
  2173. stream->index);
  2174. }
  2175. if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2176. av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
  2177. t->key, stream->index);
  2178. return AVERROR_OPTION_NOT_FOUND;
  2179. }
  2180. }
  2181. }
  2182. *fmt_ctx_ptr = fmt_ctx;
  2183. return 0;
  2184. }
  2185. static void close_input_file(AVFormatContext **ctx_ptr)
  2186. {
  2187. int i;
  2188. AVFormatContext *fmt_ctx = *ctx_ptr;
  2189. /* close decoder for each stream */
  2190. for (i = 0; i < fmt_ctx->nb_streams; i++)
  2191. if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
  2192. avcodec_close(fmt_ctx->streams[i]->codec);
  2193. avformat_close_input(ctx_ptr);
  2194. }
  2195. static int probe_file(WriterContext *wctx, const char *filename)
  2196. {
  2197. AVFormatContext *fmt_ctx = NULL;
  2198. int ret, i;
  2199. int section_id;
  2200. do_read_frames = do_show_frames || do_count_frames;
  2201. do_read_packets = do_show_packets || do_count_packets;
  2202. ret = open_input_file(&fmt_ctx, filename);
  2203. if (ret < 0)
  2204. goto end;
  2205. #define CHECK_END if (ret < 0) goto end
  2206. nb_streams = fmt_ctx->nb_streams;
  2207. REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,fmt_ctx->nb_streams);
  2208. REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,fmt_ctx->nb_streams);
  2209. REALLOCZ_ARRAY_STREAM(selected_streams,0,fmt_ctx->nb_streams);
  2210. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  2211. if (stream_specifier) {
  2212. ret = avformat_match_stream_specifier(fmt_ctx,
  2213. fmt_ctx->streams[i],
  2214. stream_specifier);
  2215. CHECK_END;
  2216. else
  2217. selected_streams[i] = ret;
  2218. ret = 0;
  2219. } else {
  2220. selected_streams[i] = 1;
  2221. }
  2222. }
  2223. if (do_read_frames || do_read_packets) {
  2224. if (do_show_frames && do_show_packets &&
  2225. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  2226. section_id = SECTION_ID_PACKETS_AND_FRAMES;
  2227. else if (do_show_packets && !do_show_frames)
  2228. section_id = SECTION_ID_PACKETS;
  2229. else // (!do_show_packets && do_show_frames)
  2230. section_id = SECTION_ID_FRAMES;
  2231. if (do_show_frames || do_show_packets)
  2232. writer_print_section_header(wctx, section_id);
  2233. ret = read_packets(wctx, fmt_ctx);
  2234. if (do_show_frames || do_show_packets)
  2235. writer_print_section_footer(wctx);
  2236. CHECK_END;
  2237. }
  2238. if (do_show_programs) {
  2239. ret = show_programs(wctx, fmt_ctx);
  2240. CHECK_END;
  2241. }
  2242. if (do_show_streams) {
  2243. ret = show_streams(wctx, fmt_ctx);
  2244. CHECK_END;
  2245. }
  2246. if (do_show_chapters) {
  2247. ret = show_chapters(wctx, fmt_ctx);
  2248. CHECK_END;
  2249. }
  2250. if (do_show_format) {
  2251. ret = show_format(wctx, fmt_ctx);
  2252. CHECK_END;
  2253. }
  2254. end:
  2255. if (fmt_ctx)
  2256. close_input_file(&fmt_ctx);
  2257. av_freep(&nb_streams_frames);
  2258. av_freep(&nb_streams_packets);
  2259. av_freep(&selected_streams);
  2260. return ret;
  2261. }
  2262. static void show_usage(void)
  2263. {
  2264. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  2265. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  2266. av_log(NULL, AV_LOG_INFO, "\n");
  2267. }
  2268. static void ffprobe_show_program_version(WriterContext *w)
  2269. {
  2270. AVBPrint pbuf;
  2271. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2272. writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
  2273. print_str("version", FFMPEG_VERSION);
  2274. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  2275. program_birth_year, CONFIG_THIS_YEAR);
  2276. print_str("compiler_ident", CC_IDENT);
  2277. print_str("configuration", FFMPEG_CONFIGURATION);
  2278. writer_print_section_footer(w);
  2279. av_bprint_finalize(&pbuf, NULL);
  2280. }
  2281. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  2282. do { \
  2283. if (CONFIG_##LIBNAME) { \
  2284. unsigned int version = libname##_version(); \
  2285. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
  2286. print_str("name", "lib" #libname); \
  2287. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  2288. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  2289. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  2290. print_int("version", version); \
  2291. print_str("ident", LIB##LIBNAME##_IDENT); \
  2292. writer_print_section_footer(w); \
  2293. } \
  2294. } while (0)
  2295. static void ffprobe_show_library_versions(WriterContext *w)
  2296. {
  2297. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
  2298. SHOW_LIB_VERSION(avutil, AVUTIL);
  2299. SHOW_LIB_VERSION(avcodec, AVCODEC);
  2300. SHOW_LIB_VERSION(avformat, AVFORMAT);
  2301. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  2302. SHOW_LIB_VERSION(avfilter, AVFILTER);
  2303. SHOW_LIB_VERSION(swscale, SWSCALE);
  2304. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  2305. SHOW_LIB_VERSION(postproc, POSTPROC);
  2306. writer_print_section_footer(w);
  2307. }
  2308. #define PRINT_PIX_FMT_FLAG(flagname, name) \
  2309. do { \
  2310. print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
  2311. } while (0)
  2312. static void ffprobe_show_pixel_formats(WriterContext *w)
  2313. {
  2314. const AVPixFmtDescriptor *pixdesc = NULL;
  2315. int i, n;
  2316. writer_print_section_header(w, SECTION_ID_PIXEL_FORMATS);
  2317. while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
  2318. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT);
  2319. print_str("name", pixdesc->name);
  2320. print_int("nb_components", pixdesc->nb_components);
  2321. if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
  2322. print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
  2323. print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
  2324. } else {
  2325. print_str_opt("log2_chroma_w", "N/A");
  2326. print_str_opt("log2_chroma_h", "N/A");
  2327. }
  2328. n = av_get_bits_per_pixel(pixdesc);
  2329. if (n) print_int ("bits_per_pixel", n);
  2330. else print_str_opt("bits_per_pixel", "N/A");
  2331. if (do_show_pixel_format_flags) {
  2332. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_FLAGS);
  2333. PRINT_PIX_FMT_FLAG(BE, "big_endian");
  2334. PRINT_PIX_FMT_FLAG(PAL, "palette");
  2335. PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
  2336. PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
  2337. PRINT_PIX_FMT_FLAG(PLANAR, "planar");
  2338. PRINT_PIX_FMT_FLAG(RGB, "rgb");
  2339. PRINT_PIX_FMT_FLAG(PSEUDOPAL, "pseudopal");
  2340. PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
  2341. writer_print_section_footer(w);
  2342. }
  2343. if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
  2344. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
  2345. for (i = 0; i < pixdesc->nb_components; i++) {
  2346. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENT);
  2347. print_int("index", i + 1);
  2348. print_int("bit_depth", pixdesc->comp[i].depth);
  2349. writer_print_section_footer(w);
  2350. }
  2351. writer_print_section_footer(w);
  2352. }
  2353. writer_print_section_footer(w);
  2354. }
  2355. writer_print_section_footer(w);
  2356. }
  2357. static int opt_format(void *optctx, const char *opt, const char *arg)
  2358. {
  2359. iformat = av_find_input_format(arg);
  2360. if (!iformat) {
  2361. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  2362. return AVERROR(EINVAL);
  2363. }
  2364. return 0;
  2365. }
  2366. static inline void mark_section_show_entries(SectionID section_id,
  2367. int show_all_entries, AVDictionary *entries)
  2368. {
  2369. struct section *section = &sections[section_id];
  2370. section->show_all_entries = show_all_entries;
  2371. if (show_all_entries) {
  2372. SectionID *id;
  2373. for (id = section->children_ids; *id != -1; id++)
  2374. mark_section_show_entries(*id, show_all_entries, entries);
  2375. } else {
  2376. av_dict_copy(&section->entries_to_show, entries, 0);
  2377. }
  2378. }
  2379. static int match_section(const char *section_name,
  2380. int show_all_entries, AVDictionary *entries)
  2381. {
  2382. int i, ret = 0;
  2383. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
  2384. const struct section *section = &sections[i];
  2385. if (!strcmp(section_name, section->name) ||
  2386. (section->unique_name && !strcmp(section_name, section->unique_name))) {
  2387. av_log(NULL, AV_LOG_DEBUG,
  2388. "'%s' matches section with unique name '%s'\n", section_name,
  2389. (char *)av_x_if_null(section->unique_name, section->name));
  2390. ret++;
  2391. mark_section_show_entries(section->id, show_all_entries, entries);
  2392. }
  2393. }
  2394. return ret;
  2395. }
  2396. static int opt_show_entries(void *optctx, const char *opt, const char *arg)
  2397. {
  2398. const char *p = arg;
  2399. int ret = 0;
  2400. while (*p) {
  2401. AVDictionary *entries = NULL;
  2402. char *section_name = av_get_token(&p, "=:");
  2403. int show_all_entries = 0;
  2404. if (!section_name) {
  2405. av_log(NULL, AV_LOG_ERROR,
  2406. "Missing section name for option '%s'\n", opt);
  2407. return AVERROR(EINVAL);
  2408. }
  2409. if (*p == '=') {
  2410. p++;
  2411. while (*p && *p != ':') {
  2412. char *entry = av_get_token(&p, ",:");
  2413. if (!entry)
  2414. break;
  2415. av_log(NULL, AV_LOG_VERBOSE,
  2416. "Adding '%s' to the entries to show in section '%s'\n",
  2417. entry, section_name);
  2418. av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
  2419. if (*p == ',')
  2420. p++;
  2421. }
  2422. } else {
  2423. show_all_entries = 1;
  2424. }
  2425. ret = match_section(section_name, show_all_entries, entries);
  2426. if (ret == 0) {
  2427. av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
  2428. ret = AVERROR(EINVAL);
  2429. }
  2430. av_dict_free(&entries);
  2431. av_free(section_name);
  2432. if (ret <= 0)
  2433. break;
  2434. if (*p)
  2435. p++;
  2436. }
  2437. return ret;
  2438. }
  2439. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  2440. {
  2441. char *buf = av_asprintf("format=%s", arg);
  2442. int ret;
  2443. if (!buf)
  2444. return AVERROR(ENOMEM);
  2445. av_log(NULL, AV_LOG_WARNING,
  2446. "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
  2447. opt, arg);
  2448. ret = opt_show_entries(optctx, opt, buf);
  2449. av_free(buf);
  2450. return ret;
  2451. }
  2452. static void opt_input_file(void *optctx, const char *arg)
  2453. {
  2454. if (input_filename) {
  2455. av_log(NULL, AV_LOG_ERROR,
  2456. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  2457. arg, input_filename);
  2458. exit_program(1);
  2459. }
  2460. if (!strcmp(arg, "-"))
  2461. arg = "pipe:";
  2462. input_filename = arg;
  2463. }
  2464. static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
  2465. {
  2466. opt_input_file(optctx, arg);
  2467. return 0;
  2468. }
  2469. void show_help_default(const char *opt, const char *arg)
  2470. {
  2471. av_log_set_callback(log_callback_help);
  2472. show_usage();
  2473. show_help_options(options, "Main options:", 0, 0, 0);
  2474. printf("\n");
  2475. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  2476. }
  2477. /**
  2478. * Parse interval specification, according to the format:
  2479. * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
  2480. * INTERVALS ::= INTERVAL[,INTERVALS]
  2481. */
  2482. static int parse_read_interval(const char *interval_spec,
  2483. ReadInterval *interval)
  2484. {
  2485. int ret = 0;
  2486. char *next, *p, *spec = av_strdup(interval_spec);
  2487. if (!spec)
  2488. return AVERROR(ENOMEM);
  2489. if (!*spec) {
  2490. av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
  2491. ret = AVERROR(EINVAL);
  2492. goto end;
  2493. }
  2494. p = spec;
  2495. next = strchr(spec, '%');
  2496. if (next)
  2497. *next++ = 0;
  2498. /* parse first part */
  2499. if (*p) {
  2500. interval->has_start = 1;
  2501. if (*p == '+') {
  2502. interval->start_is_offset = 1;
  2503. p++;
  2504. } else {
  2505. interval->start_is_offset = 0;
  2506. }
  2507. ret = av_parse_time(&interval->start, p, 1);
  2508. if (ret < 0) {
  2509. av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
  2510. goto end;
  2511. }
  2512. } else {
  2513. interval->has_start = 0;
  2514. }
  2515. /* parse second part */
  2516. p = next;
  2517. if (p && *p) {
  2518. int64_t us;
  2519. interval->has_end = 1;
  2520. if (*p == '+') {
  2521. interval->end_is_offset = 1;
  2522. p++;
  2523. } else {
  2524. interval->end_is_offset = 0;
  2525. }
  2526. if (interval->end_is_offset && *p == '#') {
  2527. long long int lli;
  2528. char *tail;
  2529. interval->duration_frames = 1;
  2530. p++;
  2531. lli = strtoll(p, &tail, 10);
  2532. if (*tail || lli < 0) {
  2533. av_log(NULL, AV_LOG_ERROR,
  2534. "Invalid or negative value '%s' for duration number of frames\n", p);
  2535. goto end;
  2536. }
  2537. interval->end = lli;
  2538. } else {
  2539. ret = av_parse_time(&us, p, 1);
  2540. if (ret < 0) {
  2541. av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
  2542. goto end;
  2543. }
  2544. interval->end = us;
  2545. }
  2546. } else {
  2547. interval->has_end = 0;
  2548. }
  2549. end:
  2550. av_free(spec);
  2551. return ret;
  2552. }
  2553. static int parse_read_intervals(const char *intervals_spec)
  2554. {
  2555. int ret, n, i;
  2556. char *p, *spec = av_strdup(intervals_spec);
  2557. if (!spec)
  2558. return AVERROR(ENOMEM);
  2559. /* preparse specification, get number of intervals */
  2560. for (n = 0, p = spec; *p; p++)
  2561. if (*p == ',')
  2562. n++;
  2563. n++;
  2564. read_intervals = av_malloc_array(n, sizeof(*read_intervals));
  2565. if (!read_intervals) {
  2566. ret = AVERROR(ENOMEM);
  2567. goto end;
  2568. }
  2569. read_intervals_nb = n;
  2570. /* parse intervals */
  2571. p = spec;
  2572. for (i = 0; p; i++) {
  2573. char *next;
  2574. av_assert0(i < read_intervals_nb);
  2575. next = strchr(p, ',');
  2576. if (next)
  2577. *next++ = 0;
  2578. read_intervals[i].id = i;
  2579. ret = parse_read_interval(p, &read_intervals[i]);
  2580. if (ret < 0) {
  2581. av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
  2582. i, p);
  2583. goto end;
  2584. }
  2585. av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
  2586. log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
  2587. p = next;
  2588. }
  2589. av_assert0(i == read_intervals_nb);
  2590. end:
  2591. av_free(spec);
  2592. return ret;
  2593. }
  2594. static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
  2595. {
  2596. return parse_read_intervals(arg);
  2597. }
  2598. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  2599. {
  2600. show_value_unit = 1;
  2601. use_value_prefix = 1;
  2602. use_byte_value_binary_prefix = 1;
  2603. use_value_sexagesimal_format = 1;
  2604. return 0;
  2605. }
  2606. static void print_section(SectionID id, int level)
  2607. {
  2608. const SectionID *pid;
  2609. const struct section *section = &sections[id];
  2610. printf("%c%c%c",
  2611. section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
  2612. section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
  2613. section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
  2614. printf("%*c %s", level * 4, ' ', section->name);
  2615. if (section->unique_name)
  2616. printf("/%s", section->unique_name);
  2617. printf("\n");
  2618. for (pid = section->children_ids; *pid != -1; pid++)
  2619. print_section(*pid, level+1);
  2620. }
  2621. static int opt_sections(void *optctx, const char *opt, const char *arg)
  2622. {
  2623. printf("Sections:\n"
  2624. "W.. = Section is a wrapper (contains other sections, no local entries)\n"
  2625. ".A. = Section contains an array of elements of the same type\n"
  2626. "..V = Section may contain a variable number of fields with variable keys\n"
  2627. "FLAGS NAME/UNIQUE_NAME\n"
  2628. "---\n");
  2629. print_section(SECTION_ID_ROOT, 0);
  2630. return 0;
  2631. }
  2632. static int opt_show_versions(const char *opt, const char *arg)
  2633. {
  2634. mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
  2635. mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
  2636. return 0;
  2637. }
  2638. #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
  2639. static int opt_show_##section(const char *opt, const char *arg) \
  2640. { \
  2641. mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
  2642. return 0; \
  2643. }
  2644. DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
  2645. DEFINE_OPT_SHOW_SECTION(error, ERROR)
  2646. DEFINE_OPT_SHOW_SECTION(format, FORMAT)
  2647. DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
  2648. DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
  2649. DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
  2650. DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
  2651. DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
  2652. DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
  2653. DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
  2654. static const OptionDef real_options[] = {
  2655. #include "cmdutils_common_opts.h"
  2656. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  2657. { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
  2658. { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
  2659. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  2660. "use binary prefixes for byte units" },
  2661. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  2662. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  2663. { "pretty", 0, {.func_arg = opt_pretty},
  2664. "prettify the format of displayed values, make it more human readable" },
  2665. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  2666. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  2667. { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
  2668. { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
  2669. { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
  2670. { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
  2671. { "show_data_hash", OPT_STRING | HAS_ARG, {(void*)&show_data_hash}, "show packets data hash" },
  2672. { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
  2673. { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
  2674. { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
  2675. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  2676. "show a particular entry from the format/container info", "entry" },
  2677. { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
  2678. "show a set of specified entries", "entry_list" },
  2679. { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
  2680. { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
  2681. { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
  2682. { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
  2683. { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
  2684. { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
  2685. { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
  2686. { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
  2687. { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
  2688. { "show_pixel_formats", 0, {(void*)&opt_show_pixel_formats}, "show pixel format descriptions" },
  2689. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  2690. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  2691. { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
  2692. { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
  2693. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
  2694. { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
  2695. { NULL, },
  2696. };
  2697. static inline int check_section_show_entries(int section_id)
  2698. {
  2699. int *id;
  2700. struct section *section = &sections[section_id];
  2701. if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
  2702. return 1;
  2703. for (id = section->children_ids; *id != -1; id++)
  2704. if (check_section_show_entries(*id))
  2705. return 1;
  2706. return 0;
  2707. }
  2708. #define SET_DO_SHOW(id, varname) do { \
  2709. if (check_section_show_entries(SECTION_ID_##id)) \
  2710. do_show_##varname = 1; \
  2711. } while (0)
  2712. int main(int argc, char **argv)
  2713. {
  2714. const Writer *w;
  2715. WriterContext *wctx;
  2716. char *buf;
  2717. char *w_name = NULL, *w_args = NULL;
  2718. int ret, i;
  2719. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  2720. register_exit(ffprobe_cleanup);
  2721. options = real_options;
  2722. parse_loglevel(argc, argv, options);
  2723. av_register_all();
  2724. avformat_network_init();
  2725. init_opts();
  2726. #if CONFIG_AVDEVICE
  2727. avdevice_register_all();
  2728. #endif
  2729. show_banner(argc, argv, options);
  2730. parse_options(NULL, argc, argv, options, opt_input_file);
  2731. /* mark things to show, based on -show_entries */
  2732. SET_DO_SHOW(CHAPTERS, chapters);
  2733. SET_DO_SHOW(ERROR, error);
  2734. SET_DO_SHOW(FORMAT, format);
  2735. SET_DO_SHOW(FRAMES, frames);
  2736. SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
  2737. SET_DO_SHOW(PACKETS, packets);
  2738. SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
  2739. SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
  2740. SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
  2741. SET_DO_SHOW(PROGRAM_VERSION, program_version);
  2742. SET_DO_SHOW(PROGRAMS, programs);
  2743. SET_DO_SHOW(STREAMS, streams);
  2744. SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
  2745. SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
  2746. SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
  2747. SET_DO_SHOW(FORMAT_TAGS, format_tags);
  2748. SET_DO_SHOW(FRAME_TAGS, frame_tags);
  2749. SET_DO_SHOW(PROGRAM_TAGS, program_tags);
  2750. SET_DO_SHOW(STREAM_TAGS, stream_tags);
  2751. SET_DO_SHOW(PACKET_TAGS, packet_tags);
  2752. if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
  2753. av_log(NULL, AV_LOG_ERROR,
  2754. "-bitexact and -show_program_version or -show_library_versions "
  2755. "options are incompatible\n");
  2756. ret = AVERROR(EINVAL);
  2757. goto end;
  2758. }
  2759. writer_register_all();
  2760. if (!print_format)
  2761. print_format = av_strdup("default");
  2762. if (!print_format) {
  2763. ret = AVERROR(ENOMEM);
  2764. goto end;
  2765. }
  2766. w_name = av_strtok(print_format, "=", &buf);
  2767. w_args = buf;
  2768. if (show_data_hash) {
  2769. if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
  2770. if (ret == AVERROR(EINVAL)) {
  2771. const char *n;
  2772. av_log(NULL, AV_LOG_ERROR,
  2773. "Unknown hash algorithm '%s'\nKnown algorithms:",
  2774. show_data_hash);
  2775. for (i = 0; (n = av_hash_names(i)); i++)
  2776. av_log(NULL, AV_LOG_ERROR, " %s", n);
  2777. av_log(NULL, AV_LOG_ERROR, "\n");
  2778. }
  2779. goto end;
  2780. }
  2781. }
  2782. w = writer_get_by_name(w_name);
  2783. if (!w) {
  2784. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  2785. ret = AVERROR(EINVAL);
  2786. goto end;
  2787. }
  2788. if ((ret = writer_open(&wctx, w, w_args,
  2789. sections, FF_ARRAY_ELEMS(sections))) >= 0) {
  2790. if (w == &xml_writer)
  2791. wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
  2792. writer_print_section_header(wctx, SECTION_ID_ROOT);
  2793. if (do_show_program_version)
  2794. ffprobe_show_program_version(wctx);
  2795. if (do_show_library_versions)
  2796. ffprobe_show_library_versions(wctx);
  2797. if (do_show_pixel_formats)
  2798. ffprobe_show_pixel_formats(wctx);
  2799. if (!input_filename &&
  2800. ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
  2801. (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
  2802. show_usage();
  2803. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  2804. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  2805. ret = AVERROR(EINVAL);
  2806. } else if (input_filename) {
  2807. ret = probe_file(wctx, input_filename);
  2808. if (ret < 0 && do_show_error)
  2809. show_error(wctx, ret);
  2810. }
  2811. writer_print_section_footer(wctx);
  2812. writer_close(&wctx);
  2813. }
  2814. end:
  2815. av_freep(&print_format);
  2816. av_freep(&read_intervals);
  2817. av_hash_freep(&hash);
  2818. uninit_opts();
  2819. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
  2820. av_dict_free(&(sections[i].entries_to_show));
  2821. avformat_network_deinit();
  2822. return ret < 0;
  2823. }