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.

2962 lines
104KB

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