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.

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