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.

3820 lines
140KB

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