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.

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