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.

3813 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. #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
  1423. static void xml_print_section_header(WriterContext *wctx)
  1424. {
  1425. XMLContext *xml = wctx->priv;
  1426. const struct section *section = wctx->section[wctx->level];
  1427. const struct section *parent_section = wctx->level ?
  1428. wctx->section[wctx->level-1] : NULL;
  1429. if (wctx->level == 0) {
  1430. const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  1431. "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
  1432. "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
  1433. printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  1434. printf("<%sffprobe%s>\n",
  1435. xml->fully_qualified ? "ffprobe:" : "",
  1436. xml->fully_qualified ? qual : "");
  1437. return;
  1438. }
  1439. if (xml->within_tag) {
  1440. xml->within_tag = 0;
  1441. printf(">\n");
  1442. }
  1443. if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
  1444. xml->indent_level++;
  1445. } else {
  1446. if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
  1447. wctx->level && wctx->nb_item[wctx->level-1])
  1448. printf("\n");
  1449. xml->indent_level++;
  1450. if (section->flags & SECTION_FLAG_IS_ARRAY) {
  1451. XML_INDENT(); printf("<%s>\n", section->name);
  1452. } else {
  1453. XML_INDENT(); printf("<%s ", section->name);
  1454. xml->within_tag = 1;
  1455. }
  1456. }
  1457. }
  1458. static void xml_print_section_footer(WriterContext *wctx)
  1459. {
  1460. XMLContext *xml = wctx->priv;
  1461. const struct section *section = wctx->section[wctx->level];
  1462. if (wctx->level == 0) {
  1463. printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  1464. } else if (xml->within_tag) {
  1465. xml->within_tag = 0;
  1466. printf("/>\n");
  1467. xml->indent_level--;
  1468. } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
  1469. xml->indent_level--;
  1470. } else {
  1471. XML_INDENT(); printf("</%s>\n", section->name);
  1472. xml->indent_level--;
  1473. }
  1474. }
  1475. static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
  1476. {
  1477. AVBPrint buf;
  1478. XMLContext *xml = wctx->priv;
  1479. const struct section *section = wctx->section[wctx->level];
  1480. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1481. if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
  1482. XML_INDENT();
  1483. av_bprint_escape(&buf, key, NULL,
  1484. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1485. printf("<%s key=\"%s\"",
  1486. section->element_name, buf.str);
  1487. av_bprint_clear(&buf);
  1488. av_bprint_escape(&buf, value, NULL,
  1489. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1490. printf(" value=\"%s\"/>\n", buf.str);
  1491. } else {
  1492. if (wctx->nb_item[wctx->level])
  1493. printf(" ");
  1494. av_bprint_escape(&buf, value, NULL,
  1495. AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
  1496. printf("%s=\"%s\"", key, buf.str);
  1497. }
  1498. av_bprint_finalize(&buf, NULL);
  1499. }
  1500. static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
  1501. {
  1502. if (wctx->nb_item[wctx->level])
  1503. printf(" ");
  1504. printf("%s=\"%lld\"", key, value);
  1505. }
  1506. static Writer xml_writer = {
  1507. .name = "xml",
  1508. .priv_size = sizeof(XMLContext),
  1509. .init = xml_init,
  1510. .print_section_header = xml_print_section_header,
  1511. .print_section_footer = xml_print_section_footer,
  1512. .print_integer = xml_print_int,
  1513. .print_string = xml_print_str,
  1514. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1515. .priv_class = &xml_class,
  1516. };
  1517. static void writer_register_all(void)
  1518. {
  1519. static int initialized;
  1520. if (initialized)
  1521. return;
  1522. initialized = 1;
  1523. writer_register(&default_writer);
  1524. writer_register(&compact_writer);
  1525. writer_register(&csv_writer);
  1526. writer_register(&flat_writer);
  1527. writer_register(&ini_writer);
  1528. writer_register(&json_writer);
  1529. writer_register(&xml_writer);
  1530. }
  1531. #define print_fmt(k, f, ...) do { \
  1532. av_bprint_clear(&pbuf); \
  1533. av_bprintf(&pbuf, f, __VA_ARGS__); \
  1534. writer_print_string(w, k, pbuf.str, 0); \
  1535. } while (0)
  1536. #define print_int(k, v) writer_print_integer(w, k, v)
  1537. #define print_q(k, v, s) writer_print_rational(w, k, v, s)
  1538. #define print_str(k, v) writer_print_string(w, k, v, 0)
  1539. #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
  1540. #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
  1541. #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
  1542. #define print_ts(k, v) writer_print_ts(w, k, v, 0)
  1543. #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
  1544. #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
  1545. #define print_val(k, v, u) do { \
  1546. struct unit_value uv; \
  1547. uv.val.i = v; \
  1548. uv.unit = u; \
  1549. writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
  1550. } while (0)
  1551. #define print_section_header(s) writer_print_section_header(w, s)
  1552. #define print_section_footer(s) writer_print_section_footer(w, s)
  1553. #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
  1554. { \
  1555. ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
  1556. if (ret < 0) \
  1557. goto end; \
  1558. memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
  1559. }
  1560. static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
  1561. {
  1562. AVDictionaryEntry *tag = NULL;
  1563. int ret = 0;
  1564. if (!tags)
  1565. return 0;
  1566. writer_print_section_header(w, section_id);
  1567. while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  1568. if ((ret = print_str_validate(tag->key, tag->value)) < 0)
  1569. break;
  1570. }
  1571. writer_print_section_footer(w);
  1572. return ret;
  1573. }
  1574. static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
  1575. {
  1576. if (!metadata)
  1577. return;
  1578. print_int("application version", metadata->application_version);
  1579. print_int("num_windows", metadata->num_windows);
  1580. for (int n = 1; n < metadata->num_windows; n++) {
  1581. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1582. print_q("window_upper_left_corner_x",
  1583. params->window_upper_left_corner_x,'/');
  1584. print_q("window_upper_left_corner_y",
  1585. params->window_upper_left_corner_y,'/');
  1586. print_q("window_lower_right_corner_x",
  1587. params->window_lower_right_corner_x,'/');
  1588. print_q("window_lower_right_corner_y",
  1589. params->window_lower_right_corner_y,'/');
  1590. print_q("window_upper_left_corner_x",
  1591. params->window_upper_left_corner_x,'/');
  1592. print_q("window_upper_left_corner_y",
  1593. params->window_upper_left_corner_y,'/');
  1594. print_int("center_of_ellipse_x",
  1595. params->center_of_ellipse_x ) ;
  1596. print_int("center_of_ellipse_y",
  1597. params->center_of_ellipse_y );
  1598. print_int("rotation_angle",
  1599. params->rotation_angle);
  1600. print_int("semimajor_axis_internal_ellipse",
  1601. params->semimajor_axis_internal_ellipse);
  1602. print_int("semimajor_axis_external_ellipse",
  1603. params->semimajor_axis_external_ellipse);
  1604. print_int("semiminor_axis_external_ellipse",
  1605. params->semiminor_axis_external_ellipse);
  1606. print_int("overlap_process_option",
  1607. params->overlap_process_option);
  1608. }
  1609. print_q("targeted_system_display_maximum_luminance",
  1610. metadata->targeted_system_display_maximum_luminance,'/');
  1611. if (metadata->targeted_system_display_actual_peak_luminance_flag) {
  1612. print_int("num_rows_targeted_system_display_actual_peak_luminance",
  1613. metadata->num_rows_targeted_system_display_actual_peak_luminance);
  1614. print_int("num_cols_targeted_system_display_actual_peak_luminance",
  1615. metadata->num_cols_targeted_system_display_actual_peak_luminance);
  1616. for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
  1617. for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
  1618. print_q("targeted_system_display_actual_peak_luminance",
  1619. metadata->targeted_system_display_actual_peak_luminance[i][j],'/');
  1620. }
  1621. }
  1622. }
  1623. for (int n = 0; n < metadata->num_windows; n++) {
  1624. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1625. for (int i = 0; i < 3; i++) {
  1626. print_q("maxscl",params->maxscl[i],'/');
  1627. }
  1628. print_q("average_maxrgb",
  1629. params->average_maxrgb,'/');
  1630. print_int("num_distribution_maxrgb_percentiles",
  1631. params->num_distribution_maxrgb_percentiles);
  1632. for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
  1633. print_int("distribution_maxrgb_percentage",
  1634. params->distribution_maxrgb[i].percentage);
  1635. print_q("distribution_maxrgb_percentile",
  1636. params->distribution_maxrgb[i].percentile,'/');
  1637. }
  1638. print_q("fraction_bright_pixels",
  1639. params->fraction_bright_pixels,'/');
  1640. }
  1641. if (metadata->mastering_display_actual_peak_luminance_flag) {
  1642. print_int("num_rows_mastering_display_actual_peak_luminance",
  1643. metadata->num_rows_mastering_display_actual_peak_luminance);
  1644. print_int("num_cols_mastering_display_actual_peak_luminance",
  1645. metadata->num_cols_mastering_display_actual_peak_luminance);
  1646. for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
  1647. for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
  1648. print_q("mastering_display_actual_peak_luminance",
  1649. metadata->mastering_display_actual_peak_luminance[i][j],'/');
  1650. }
  1651. }
  1652. }
  1653. for (int n = 0; n < metadata->num_windows; n++) {
  1654. const AVHDRPlusColorTransformParams *params = &metadata->params[n];
  1655. if (params->tone_mapping_flag) {
  1656. print_q("knee_point_x", params->knee_point_x,'/');
  1657. print_q("knee_point_y", params->knee_point_y,'/');
  1658. print_int("num_bezier_curve_anchors",
  1659. params->num_bezier_curve_anchors );
  1660. for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
  1661. print_q("bezier_curve_anchors",
  1662. params->bezier_curve_anchors[i],'/');
  1663. }
  1664. }
  1665. if (params->color_saturation_mapping_flag) {
  1666. print_q("color_saturation_weight",
  1667. params->color_saturation_weight,'/');
  1668. }
  1669. }
  1670. }
  1671. static void print_pkt_side_data(WriterContext *w,
  1672. AVCodecParameters *par,
  1673. const AVPacketSideData *side_data,
  1674. int nb_side_data,
  1675. SectionID id_data_list,
  1676. SectionID id_data)
  1677. {
  1678. int i;
  1679. writer_print_section_header(w, id_data_list);
  1680. for (i = 0; i < nb_side_data; i++) {
  1681. const AVPacketSideData *sd = &side_data[i];
  1682. const char *name = av_packet_side_data_name(sd->type);
  1683. writer_print_section_header(w, id_data);
  1684. print_str("side_data_type", name ? name : "unknown");
  1685. if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  1686. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  1687. print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
  1688. } else if (sd->type == AV_PKT_DATA_STEREO3D) {
  1689. const AVStereo3D *stereo = (AVStereo3D *)sd->data;
  1690. print_str("type", av_stereo3d_type_name(stereo->type));
  1691. print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
  1692. } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
  1693. const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
  1694. print_str("projection", av_spherical_projection_name(spherical->projection));
  1695. if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
  1696. print_int("padding", spherical->padding);
  1697. } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
  1698. size_t l, t, r, b;
  1699. av_spherical_tile_bounds(spherical, par->width, par->height,
  1700. &l, &t, &r, &b);
  1701. print_int("bound_left", l);
  1702. print_int("bound_top", t);
  1703. print_int("bound_right", r);
  1704. print_int("bound_bottom", b);
  1705. }
  1706. print_int("yaw", (double) spherical->yaw / (1 << 16));
  1707. print_int("pitch", (double) spherical->pitch / (1 << 16));
  1708. print_int("roll", (double) spherical->roll / (1 << 16));
  1709. } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
  1710. print_int("skip_samples", AV_RL32(sd->data));
  1711. print_int("discard_padding", AV_RL32(sd->data + 4));
  1712. print_int("skip_reason", AV_RL8(sd->data + 8));
  1713. print_int("discard_reason", AV_RL8(sd->data + 9));
  1714. } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
  1715. AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
  1716. if (metadata->has_primaries) {
  1717. print_q("red_x", metadata->display_primaries[0][0], '/');
  1718. print_q("red_y", metadata->display_primaries[0][1], '/');
  1719. print_q("green_x", metadata->display_primaries[1][0], '/');
  1720. print_q("green_y", metadata->display_primaries[1][1], '/');
  1721. print_q("blue_x", metadata->display_primaries[2][0], '/');
  1722. print_q("blue_y", metadata->display_primaries[2][1], '/');
  1723. print_q("white_point_x", metadata->white_point[0], '/');
  1724. print_q("white_point_y", metadata->white_point[1], '/');
  1725. }
  1726. if (metadata->has_luminance) {
  1727. print_q("min_luminance", metadata->min_luminance, '/');
  1728. print_q("max_luminance", metadata->max_luminance, '/');
  1729. }
  1730. } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
  1731. AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
  1732. print_int("max_content", metadata->MaxCLL);
  1733. print_int("max_average", metadata->MaxFALL);
  1734. } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
  1735. AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)sd->data;
  1736. print_int("dv_version_major", dovi->dv_version_major);
  1737. print_int("dv_version_minor", dovi->dv_version_minor);
  1738. print_int("dv_profile", dovi->dv_profile);
  1739. print_int("dv_level", dovi->dv_level);
  1740. print_int("rpu_present_flag", dovi->rpu_present_flag);
  1741. print_int("el_present_flag", dovi->el_present_flag);
  1742. print_int("bl_present_flag", dovi->bl_present_flag);
  1743. print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
  1744. }
  1745. writer_print_section_footer(w);
  1746. }
  1747. writer_print_section_footer(w);
  1748. }
  1749. static void print_color_range(WriterContext *w, enum AVColorRange color_range)
  1750. {
  1751. const char *val = av_color_range_name(color_range);
  1752. if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
  1753. print_str_opt("color_range", "unknown");
  1754. } else {
  1755. print_str("color_range", val);
  1756. }
  1757. }
  1758. static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
  1759. {
  1760. const char *val = av_color_space_name(color_space);
  1761. if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
  1762. print_str_opt("color_space", "unknown");
  1763. } else {
  1764. print_str("color_space", val);
  1765. }
  1766. }
  1767. static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
  1768. {
  1769. const char *val = av_color_primaries_name(color_primaries);
  1770. if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
  1771. print_str_opt("color_primaries", "unknown");
  1772. } else {
  1773. print_str("color_primaries", val);
  1774. }
  1775. }
  1776. static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
  1777. {
  1778. const char *val = av_color_transfer_name(color_trc);
  1779. if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
  1780. print_str_opt("color_transfer", "unknown");
  1781. } else {
  1782. print_str("color_transfer", val);
  1783. }
  1784. }
  1785. static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
  1786. {
  1787. const char *val = av_chroma_location_name(chroma_location);
  1788. if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
  1789. print_str_opt("chroma_location", "unspecified");
  1790. } else {
  1791. print_str("chroma_location", val);
  1792. }
  1793. }
  1794. static void clear_log(int need_lock)
  1795. {
  1796. int i;
  1797. if (need_lock)
  1798. pthread_mutex_lock(&log_mutex);
  1799. for (i=0; i<log_buffer_size; i++) {
  1800. av_freep(&log_buffer[i].context_name);
  1801. av_freep(&log_buffer[i].parent_name);
  1802. av_freep(&log_buffer[i].log_message);
  1803. }
  1804. log_buffer_size = 0;
  1805. if(need_lock)
  1806. pthread_mutex_unlock(&log_mutex);
  1807. }
  1808. static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
  1809. {
  1810. int i;
  1811. pthread_mutex_lock(&log_mutex);
  1812. if (!log_buffer_size) {
  1813. pthread_mutex_unlock(&log_mutex);
  1814. return 0;
  1815. }
  1816. writer_print_section_header(w, section_ids);
  1817. for (i=0; i<log_buffer_size; i++) {
  1818. if (log_buffer[i].log_level <= log_level) {
  1819. writer_print_section_header(w, section_id);
  1820. print_str("context", log_buffer[i].context_name);
  1821. print_int("level", log_buffer[i].log_level);
  1822. print_int("category", log_buffer[i].category);
  1823. if (log_buffer[i].parent_name) {
  1824. print_str("parent_context", log_buffer[i].parent_name);
  1825. print_int("parent_category", log_buffer[i].parent_category);
  1826. } else {
  1827. print_str_opt("parent_context", "N/A");
  1828. print_str_opt("parent_category", "N/A");
  1829. }
  1830. print_str("message", log_buffer[i].log_message);
  1831. writer_print_section_footer(w);
  1832. }
  1833. }
  1834. clear_log(0);
  1835. pthread_mutex_unlock(&log_mutex);
  1836. writer_print_section_footer(w);
  1837. return 0;
  1838. }
  1839. static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
  1840. {
  1841. char val_str[128];
  1842. AVStream *st = ifile->streams[pkt->stream_index].st;
  1843. AVBPrint pbuf;
  1844. const char *s;
  1845. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1846. writer_print_section_header(w, SECTION_ID_PACKET);
  1847. s = av_get_media_type_string(st->codecpar->codec_type);
  1848. if (s) print_str ("codec_type", s);
  1849. else print_str_opt("codec_type", "unknown");
  1850. print_int("stream_index", pkt->stream_index);
  1851. print_ts ("pts", pkt->pts);
  1852. print_time("pts_time", pkt->pts, &st->time_base);
  1853. print_ts ("dts", pkt->dts);
  1854. print_time("dts_time", pkt->dts, &st->time_base);
  1855. print_duration_ts("duration", pkt->duration);
  1856. print_duration_time("duration_time", pkt->duration, &st->time_base);
  1857. print_val("size", pkt->size, unit_byte_str);
  1858. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  1859. else print_str_opt("pos", "N/A");
  1860. print_fmt("flags", "%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
  1861. pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_');
  1862. if (pkt->side_data_elems) {
  1863. int size;
  1864. const uint8_t *side_metadata;
  1865. side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
  1866. if (side_metadata && size && do_show_packet_tags) {
  1867. AVDictionary *dict = NULL;
  1868. if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
  1869. show_tags(w, dict, SECTION_ID_PACKET_TAGS);
  1870. av_dict_free(&dict);
  1871. }
  1872. print_pkt_side_data(w, st->codecpar, pkt->side_data, pkt->side_data_elems,
  1873. SECTION_ID_PACKET_SIDE_DATA_LIST,
  1874. SECTION_ID_PACKET_SIDE_DATA);
  1875. }
  1876. if (do_show_data)
  1877. writer_print_data(w, "data", pkt->data, pkt->size);
  1878. writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
  1879. writer_print_section_footer(w);
  1880. av_bprint_finalize(&pbuf, NULL);
  1881. fflush(stdout);
  1882. }
  1883. static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
  1884. AVFormatContext *fmt_ctx)
  1885. {
  1886. AVBPrint pbuf;
  1887. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1888. writer_print_section_header(w, SECTION_ID_SUBTITLE);
  1889. print_str ("media_type", "subtitle");
  1890. print_ts ("pts", sub->pts);
  1891. print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
  1892. print_int ("format", sub->format);
  1893. print_int ("start_display_time", sub->start_display_time);
  1894. print_int ("end_display_time", sub->end_display_time);
  1895. print_int ("num_rects", sub->num_rects);
  1896. writer_print_section_footer(w);
  1897. av_bprint_finalize(&pbuf, NULL);
  1898. fflush(stdout);
  1899. }
  1900. static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
  1901. AVFormatContext *fmt_ctx)
  1902. {
  1903. AVBPrint pbuf;
  1904. char val_str[128];
  1905. const char *s;
  1906. int i;
  1907. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1908. writer_print_section_header(w, SECTION_ID_FRAME);
  1909. s = av_get_media_type_string(stream->codecpar->codec_type);
  1910. if (s) print_str ("media_type", s);
  1911. else print_str_opt("media_type", "unknown");
  1912. print_int("stream_index", stream->index);
  1913. print_int("key_frame", frame->key_frame);
  1914. print_ts ("pkt_pts", frame->pts);
  1915. print_time("pkt_pts_time", frame->pts, &stream->time_base);
  1916. print_ts ("pkt_dts", frame->pkt_dts);
  1917. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  1918. print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
  1919. print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
  1920. print_duration_ts ("pkt_duration", frame->pkt_duration);
  1921. print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
  1922. if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
  1923. else print_str_opt("pkt_pos", "N/A");
  1924. if (frame->pkt_size != -1) print_val ("pkt_size", frame->pkt_size, unit_byte_str);
  1925. else print_str_opt("pkt_size", "N/A");
  1926. switch (stream->codecpar->codec_type) {
  1927. AVRational sar;
  1928. case AVMEDIA_TYPE_VIDEO:
  1929. print_int("width", frame->width);
  1930. print_int("height", frame->height);
  1931. s = av_get_pix_fmt_name(frame->format);
  1932. if (s) print_str ("pix_fmt", s);
  1933. else print_str_opt("pix_fmt", "unknown");
  1934. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
  1935. if (sar.num) {
  1936. print_q("sample_aspect_ratio", sar, ':');
  1937. } else {
  1938. print_str_opt("sample_aspect_ratio", "N/A");
  1939. }
  1940. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1941. print_int("coded_picture_number", frame->coded_picture_number);
  1942. print_int("display_picture_number", frame->display_picture_number);
  1943. print_int("interlaced_frame", frame->interlaced_frame);
  1944. print_int("top_field_first", frame->top_field_first);
  1945. print_int("repeat_pict", frame->repeat_pict);
  1946. print_color_range(w, frame->color_range);
  1947. print_color_space(w, frame->colorspace);
  1948. print_primaries(w, frame->color_primaries);
  1949. print_color_trc(w, frame->color_trc);
  1950. print_chroma_location(w, frame->chroma_location);
  1951. break;
  1952. case AVMEDIA_TYPE_AUDIO:
  1953. s = av_get_sample_fmt_name(frame->format);
  1954. if (s) print_str ("sample_fmt", s);
  1955. else print_str_opt("sample_fmt", "unknown");
  1956. print_int("nb_samples", frame->nb_samples);
  1957. print_int("channels", frame->channels);
  1958. if (frame->channel_layout) {
  1959. av_bprint_clear(&pbuf);
  1960. av_bprint_channel_layout(&pbuf, frame->channels,
  1961. frame->channel_layout);
  1962. print_str ("channel_layout", pbuf.str);
  1963. } else
  1964. print_str_opt("channel_layout", "unknown");
  1965. break;
  1966. }
  1967. if (do_show_frame_tags)
  1968. show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
  1969. if (do_show_log)
  1970. show_log(w, SECTION_ID_FRAME_LOGS, SECTION_ID_FRAME_LOG, do_show_log);
  1971. if (frame->nb_side_data) {
  1972. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_LIST);
  1973. for (i = 0; i < frame->nb_side_data; i++) {
  1974. AVFrameSideData *sd = frame->side_data[i];
  1975. const char *name;
  1976. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA);
  1977. name = av_frame_side_data_name(sd->type);
  1978. print_str("side_data_type", name ? name : "unknown");
  1979. if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
  1980. writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
  1981. print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
  1982. } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
  1983. char tcbuf[AV_TIMECODE_STR_SIZE];
  1984. av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
  1985. print_str("timecode", tcbuf);
  1986. } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
  1987. uint32_t *tc = (uint32_t*)sd->data;
  1988. int m = FFMIN(tc[0],3);
  1989. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST);
  1990. for (int j = 1; j <= m ; j++) {
  1991. char tcbuf[AV_TIMECODE_STR_SIZE];
  1992. av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
  1993. writer_print_section_header(w, SECTION_ID_FRAME_SIDE_DATA_TIMECODE);
  1994. print_str("value", tcbuf);
  1995. writer_print_section_footer(w);
  1996. }
  1997. writer_print_section_footer(w);
  1998. } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
  1999. AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
  2000. if (metadata->has_primaries) {
  2001. print_q("red_x", metadata->display_primaries[0][0], '/');
  2002. print_q("red_y", metadata->display_primaries[0][1], '/');
  2003. print_q("green_x", metadata->display_primaries[1][0], '/');
  2004. print_q("green_y", metadata->display_primaries[1][1], '/');
  2005. print_q("blue_x", metadata->display_primaries[2][0], '/');
  2006. print_q("blue_y", metadata->display_primaries[2][1], '/');
  2007. print_q("white_point_x", metadata->white_point[0], '/');
  2008. print_q("white_point_y", metadata->white_point[1], '/');
  2009. }
  2010. if (metadata->has_luminance) {
  2011. print_q("min_luminance", metadata->min_luminance, '/');
  2012. print_q("max_luminance", metadata->max_luminance, '/');
  2013. }
  2014. } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
  2015. AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
  2016. print_dynamic_hdr10_plus(w, metadata);
  2017. } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
  2018. AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
  2019. print_int("max_content", metadata->MaxCLL);
  2020. print_int("max_average", metadata->MaxFALL);
  2021. } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
  2022. AVDictionaryEntry *tag = av_dict_get(sd->metadata, "name", NULL, AV_DICT_MATCH_CASE);
  2023. if (tag)
  2024. print_str(tag->key, tag->value);
  2025. print_int("size", sd->size);
  2026. }
  2027. writer_print_section_footer(w);
  2028. }
  2029. writer_print_section_footer(w);
  2030. }
  2031. writer_print_section_footer(w);
  2032. av_bprint_finalize(&pbuf, NULL);
  2033. fflush(stdout);
  2034. }
  2035. static av_always_inline int process_frame(WriterContext *w,
  2036. InputFile *ifile,
  2037. AVFrame *frame, AVPacket *pkt,
  2038. int *packet_new)
  2039. {
  2040. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2041. AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
  2042. AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
  2043. AVSubtitle sub;
  2044. int ret = 0, got_frame = 0;
  2045. clear_log(1);
  2046. if (dec_ctx && dec_ctx->codec) {
  2047. switch (par->codec_type) {
  2048. case AVMEDIA_TYPE_VIDEO:
  2049. case AVMEDIA_TYPE_AUDIO:
  2050. if (*packet_new) {
  2051. ret = avcodec_send_packet(dec_ctx, pkt);
  2052. if (ret == AVERROR(EAGAIN)) {
  2053. ret = 0;
  2054. } else if (ret >= 0 || ret == AVERROR_EOF) {
  2055. ret = 0;
  2056. *packet_new = 0;
  2057. }
  2058. }
  2059. if (ret >= 0) {
  2060. ret = avcodec_receive_frame(dec_ctx, frame);
  2061. if (ret >= 0) {
  2062. got_frame = 1;
  2063. } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  2064. ret = 0;
  2065. }
  2066. }
  2067. break;
  2068. case AVMEDIA_TYPE_SUBTITLE:
  2069. if (*packet_new)
  2070. ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
  2071. *packet_new = 0;
  2072. break;
  2073. default:
  2074. *packet_new = 0;
  2075. }
  2076. } else {
  2077. *packet_new = 0;
  2078. }
  2079. if (ret < 0)
  2080. return ret;
  2081. if (got_frame) {
  2082. int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
  2083. nb_streams_frames[pkt->stream_index]++;
  2084. if (do_show_frames)
  2085. if (is_sub)
  2086. show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
  2087. else
  2088. show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
  2089. if (is_sub)
  2090. avsubtitle_free(&sub);
  2091. }
  2092. return got_frame || *packet_new;
  2093. }
  2094. static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
  2095. {
  2096. av_log(log_ctx, log_level, "id:%d", interval->id);
  2097. if (interval->has_start) {
  2098. av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
  2099. av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
  2100. } else {
  2101. av_log(log_ctx, log_level, " start:N/A");
  2102. }
  2103. if (interval->has_end) {
  2104. av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
  2105. if (interval->duration_frames)
  2106. av_log(log_ctx, log_level, "#%"PRId64, interval->end);
  2107. else
  2108. av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
  2109. } else {
  2110. av_log(log_ctx, log_level, " end:N/A");
  2111. }
  2112. av_log(log_ctx, log_level, "\n");
  2113. }
  2114. static int read_interval_packets(WriterContext *w, InputFile *ifile,
  2115. const ReadInterval *interval, int64_t *cur_ts)
  2116. {
  2117. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2118. AVPacket *pkt = NULL;
  2119. AVFrame *frame = NULL;
  2120. int ret = 0, i = 0, frame_count = 0;
  2121. int64_t start = -INT64_MAX, end = interval->end;
  2122. int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
  2123. av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
  2124. log_read_interval(interval, NULL, AV_LOG_VERBOSE);
  2125. if (interval->has_start) {
  2126. int64_t target;
  2127. if (interval->start_is_offset) {
  2128. if (*cur_ts == AV_NOPTS_VALUE) {
  2129. av_log(NULL, AV_LOG_ERROR,
  2130. "Could not seek to relative position since current "
  2131. "timestamp is not defined\n");
  2132. ret = AVERROR(EINVAL);
  2133. goto end;
  2134. }
  2135. target = *cur_ts + interval->start;
  2136. } else {
  2137. target = interval->start;
  2138. }
  2139. av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
  2140. av_ts2timestr(target, &AV_TIME_BASE_Q));
  2141. if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
  2142. av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
  2143. interval->start, av_err2str(ret));
  2144. goto end;
  2145. }
  2146. }
  2147. frame = av_frame_alloc();
  2148. if (!frame) {
  2149. ret = AVERROR(ENOMEM);
  2150. goto end;
  2151. }
  2152. pkt = av_packet_alloc();
  2153. if (!pkt) {
  2154. ret = AVERROR(ENOMEM);
  2155. goto end;
  2156. }
  2157. while (!av_read_frame(fmt_ctx, pkt)) {
  2158. if (fmt_ctx->nb_streams > nb_streams) {
  2159. REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
  2160. REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
  2161. REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
  2162. nb_streams = fmt_ctx->nb_streams;
  2163. }
  2164. if (selected_streams[pkt->stream_index]) {
  2165. AVRational tb = ifile->streams[pkt->stream_index].st->time_base;
  2166. if (pkt->pts != AV_NOPTS_VALUE)
  2167. *cur_ts = av_rescale_q(pkt->pts, tb, AV_TIME_BASE_Q);
  2168. if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
  2169. start = *cur_ts;
  2170. has_start = 1;
  2171. }
  2172. if (has_start && !has_end && interval->end_is_offset) {
  2173. end = start + interval->end;
  2174. has_end = 1;
  2175. }
  2176. if (interval->end_is_offset && interval->duration_frames) {
  2177. if (frame_count >= interval->end)
  2178. break;
  2179. } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
  2180. break;
  2181. }
  2182. frame_count++;
  2183. if (do_read_packets) {
  2184. if (do_show_packets)
  2185. show_packet(w, ifile, pkt, i++);
  2186. nb_streams_packets[pkt->stream_index]++;
  2187. }
  2188. if (do_read_frames) {
  2189. int packet_new = 1;
  2190. while (process_frame(w, ifile, frame, pkt, &packet_new) > 0);
  2191. }
  2192. }
  2193. av_packet_unref(pkt);
  2194. }
  2195. av_packet_unref(pkt);
  2196. //Flush remaining frames that are cached in the decoder
  2197. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  2198. pkt->stream_index = i;
  2199. if (do_read_frames)
  2200. while (process_frame(w, ifile, frame, pkt, &(int){1}) > 0);
  2201. }
  2202. end:
  2203. av_frame_free(&frame);
  2204. av_packet_free(&pkt);
  2205. if (ret < 0) {
  2206. av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
  2207. log_read_interval(interval, NULL, AV_LOG_ERROR);
  2208. }
  2209. return ret;
  2210. }
  2211. static int read_packets(WriterContext *w, InputFile *ifile)
  2212. {
  2213. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2214. int i, ret = 0;
  2215. int64_t cur_ts = fmt_ctx->start_time;
  2216. if (read_intervals_nb == 0) {
  2217. ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
  2218. ret = read_interval_packets(w, ifile, &interval, &cur_ts);
  2219. } else {
  2220. for (i = 0; i < read_intervals_nb; i++) {
  2221. ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
  2222. if (ret < 0)
  2223. break;
  2224. }
  2225. }
  2226. return ret;
  2227. }
  2228. static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
  2229. {
  2230. AVStream *stream = ist->st;
  2231. AVCodecParameters *par;
  2232. AVCodecContext *dec_ctx;
  2233. char val_str[128];
  2234. const char *s;
  2235. AVRational sar, dar;
  2236. AVBPrint pbuf;
  2237. const AVCodecDescriptor *cd;
  2238. int ret = 0;
  2239. const char *profile = NULL;
  2240. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2241. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
  2242. print_int("index", stream->index);
  2243. par = stream->codecpar;
  2244. dec_ctx = ist->dec_ctx;
  2245. if (cd = avcodec_descriptor_get(par->codec_id)) {
  2246. print_str("codec_name", cd->name);
  2247. if (!do_bitexact) {
  2248. print_str("codec_long_name",
  2249. cd->long_name ? cd->long_name : "unknown");
  2250. }
  2251. } else {
  2252. print_str_opt("codec_name", "unknown");
  2253. if (!do_bitexact) {
  2254. print_str_opt("codec_long_name", "unknown");
  2255. }
  2256. }
  2257. if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
  2258. print_str("profile", profile);
  2259. else {
  2260. if (par->profile != FF_PROFILE_UNKNOWN) {
  2261. char profile_num[12];
  2262. snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
  2263. print_str("profile", profile_num);
  2264. } else
  2265. print_str_opt("profile", "unknown");
  2266. }
  2267. s = av_get_media_type_string(par->codec_type);
  2268. if (s) print_str ("codec_type", s);
  2269. else print_str_opt("codec_type", "unknown");
  2270. /* print AVI/FourCC tag */
  2271. print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
  2272. print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
  2273. switch (par->codec_type) {
  2274. case AVMEDIA_TYPE_VIDEO:
  2275. print_int("width", par->width);
  2276. print_int("height", par->height);
  2277. if (dec_ctx) {
  2278. print_int("coded_width", dec_ctx->coded_width);
  2279. print_int("coded_height", dec_ctx->coded_height);
  2280. print_int("closed_captions", !!(dec_ctx->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS));
  2281. }
  2282. print_int("has_b_frames", par->video_delay);
  2283. sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
  2284. if (sar.num) {
  2285. print_q("sample_aspect_ratio", sar, ':');
  2286. av_reduce(&dar.num, &dar.den,
  2287. par->width * sar.num,
  2288. par->height * sar.den,
  2289. 1024*1024);
  2290. print_q("display_aspect_ratio", dar, ':');
  2291. } else {
  2292. print_str_opt("sample_aspect_ratio", "N/A");
  2293. print_str_opt("display_aspect_ratio", "N/A");
  2294. }
  2295. s = av_get_pix_fmt_name(par->format);
  2296. if (s) print_str ("pix_fmt", s);
  2297. else print_str_opt("pix_fmt", "unknown");
  2298. print_int("level", par->level);
  2299. print_color_range(w, par->color_range);
  2300. print_color_space(w, par->color_space);
  2301. print_color_trc(w, par->color_trc);
  2302. print_primaries(w, par->color_primaries);
  2303. print_chroma_location(w, par->chroma_location);
  2304. if (par->field_order == AV_FIELD_PROGRESSIVE)
  2305. print_str("field_order", "progressive");
  2306. else if (par->field_order == AV_FIELD_TT)
  2307. print_str("field_order", "tt");
  2308. else if (par->field_order == AV_FIELD_BB)
  2309. print_str("field_order", "bb");
  2310. else if (par->field_order == AV_FIELD_TB)
  2311. print_str("field_order", "tb");
  2312. else if (par->field_order == AV_FIELD_BT)
  2313. print_str("field_order", "bt");
  2314. else
  2315. print_str_opt("field_order", "unknown");
  2316. if (dec_ctx)
  2317. print_int("refs", dec_ctx->refs);
  2318. break;
  2319. case AVMEDIA_TYPE_AUDIO:
  2320. s = av_get_sample_fmt_name(par->format);
  2321. if (s) print_str ("sample_fmt", s);
  2322. else print_str_opt("sample_fmt", "unknown");
  2323. print_val("sample_rate", par->sample_rate, unit_hertz_str);
  2324. print_int("channels", par->channels);
  2325. if (par->channel_layout) {
  2326. av_bprint_clear(&pbuf);
  2327. av_bprint_channel_layout(&pbuf, par->channels, par->channel_layout);
  2328. print_str ("channel_layout", pbuf.str);
  2329. } else {
  2330. print_str_opt("channel_layout", "unknown");
  2331. }
  2332. print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
  2333. break;
  2334. case AVMEDIA_TYPE_SUBTITLE:
  2335. if (par->width)
  2336. print_int("width", par->width);
  2337. else
  2338. print_str_opt("width", "N/A");
  2339. if (par->height)
  2340. print_int("height", par->height);
  2341. else
  2342. print_str_opt("height", "N/A");
  2343. break;
  2344. }
  2345. if (dec_ctx && dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  2346. const AVOption *opt = NULL;
  2347. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  2348. uint8_t *str;
  2349. if (opt->flags) continue;
  2350. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  2351. print_str(opt->name, str);
  2352. av_free(str);
  2353. }
  2354. }
  2355. }
  2356. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  2357. else print_str_opt("id", "N/A");
  2358. print_q("r_frame_rate", stream->r_frame_rate, '/');
  2359. print_q("avg_frame_rate", stream->avg_frame_rate, '/');
  2360. print_q("time_base", stream->time_base, '/');
  2361. print_ts ("start_pts", stream->start_time);
  2362. print_time("start_time", stream->start_time, &stream->time_base);
  2363. print_ts ("duration_ts", stream->duration);
  2364. print_time("duration", stream->duration, &stream->time_base);
  2365. if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
  2366. else print_str_opt("bit_rate", "N/A");
  2367. if (dec_ctx && dec_ctx->rc_max_rate > 0)
  2368. print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
  2369. else
  2370. print_str_opt("max_bit_rate", "N/A");
  2371. if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
  2372. else print_str_opt("bits_per_raw_sample", "N/A");
  2373. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  2374. else print_str_opt("nb_frames", "N/A");
  2375. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  2376. else print_str_opt("nb_read_frames", "N/A");
  2377. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  2378. else print_str_opt("nb_read_packets", "N/A");
  2379. if (do_show_data)
  2380. writer_print_data(w, "extradata", par->extradata,
  2381. par->extradata_size);
  2382. writer_print_data_hash(w, "extradata_hash", par->extradata,
  2383. par->extradata_size);
  2384. /* Print disposition information */
  2385. #define PRINT_DISPOSITION(flagname, name) do { \
  2386. print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
  2387. } while (0)
  2388. if (do_show_stream_disposition) {
  2389. writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
  2390. PRINT_DISPOSITION(DEFAULT, "default");
  2391. PRINT_DISPOSITION(DUB, "dub");
  2392. PRINT_DISPOSITION(ORIGINAL, "original");
  2393. PRINT_DISPOSITION(COMMENT, "comment");
  2394. PRINT_DISPOSITION(LYRICS, "lyrics");
  2395. PRINT_DISPOSITION(KARAOKE, "karaoke");
  2396. PRINT_DISPOSITION(FORCED, "forced");
  2397. PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
  2398. PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
  2399. PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
  2400. PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
  2401. PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
  2402. writer_print_section_footer(w);
  2403. }
  2404. if (do_show_stream_tags)
  2405. ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
  2406. if (stream->nb_side_data) {
  2407. print_pkt_side_data(w, stream->codecpar, stream->side_data, stream->nb_side_data,
  2408. SECTION_ID_STREAM_SIDE_DATA_LIST,
  2409. SECTION_ID_STREAM_SIDE_DATA);
  2410. }
  2411. writer_print_section_footer(w);
  2412. av_bprint_finalize(&pbuf, NULL);
  2413. fflush(stdout);
  2414. return ret;
  2415. }
  2416. static int show_streams(WriterContext *w, InputFile *ifile)
  2417. {
  2418. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2419. int i, ret = 0;
  2420. writer_print_section_header(w, SECTION_ID_STREAMS);
  2421. for (i = 0; i < ifile->nb_streams; i++)
  2422. if (selected_streams[i]) {
  2423. ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
  2424. if (ret < 0)
  2425. break;
  2426. }
  2427. writer_print_section_footer(w);
  2428. return ret;
  2429. }
  2430. static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
  2431. {
  2432. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2433. int i, ret = 0;
  2434. writer_print_section_header(w, SECTION_ID_PROGRAM);
  2435. print_int("program_id", program->id);
  2436. print_int("program_num", program->program_num);
  2437. print_int("nb_streams", program->nb_stream_indexes);
  2438. print_int("pmt_pid", program->pmt_pid);
  2439. print_int("pcr_pid", program->pcr_pid);
  2440. print_ts("start_pts", program->start_time);
  2441. print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
  2442. print_ts("end_pts", program->end_time);
  2443. print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
  2444. if (do_show_program_tags)
  2445. ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
  2446. if (ret < 0)
  2447. goto end;
  2448. writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
  2449. for (i = 0; i < program->nb_stream_indexes; i++) {
  2450. if (selected_streams[program->stream_index[i]]) {
  2451. ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
  2452. if (ret < 0)
  2453. break;
  2454. }
  2455. }
  2456. writer_print_section_footer(w);
  2457. end:
  2458. writer_print_section_footer(w);
  2459. return ret;
  2460. }
  2461. static int show_programs(WriterContext *w, InputFile *ifile)
  2462. {
  2463. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2464. int i, ret = 0;
  2465. writer_print_section_header(w, SECTION_ID_PROGRAMS);
  2466. for (i = 0; i < fmt_ctx->nb_programs; i++) {
  2467. AVProgram *program = fmt_ctx->programs[i];
  2468. if (!program)
  2469. continue;
  2470. ret = show_program(w, ifile, program);
  2471. if (ret < 0)
  2472. break;
  2473. }
  2474. writer_print_section_footer(w);
  2475. return ret;
  2476. }
  2477. static int show_chapters(WriterContext *w, InputFile *ifile)
  2478. {
  2479. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2480. int i, ret = 0;
  2481. writer_print_section_header(w, SECTION_ID_CHAPTERS);
  2482. for (i = 0; i < fmt_ctx->nb_chapters; i++) {
  2483. AVChapter *chapter = fmt_ctx->chapters[i];
  2484. writer_print_section_header(w, SECTION_ID_CHAPTER);
  2485. print_int("id", chapter->id);
  2486. print_q ("time_base", chapter->time_base, '/');
  2487. print_int("start", chapter->start);
  2488. print_time("start_time", chapter->start, &chapter->time_base);
  2489. print_int("end", chapter->end);
  2490. print_time("end_time", chapter->end, &chapter->time_base);
  2491. if (do_show_chapter_tags)
  2492. ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
  2493. writer_print_section_footer(w);
  2494. }
  2495. writer_print_section_footer(w);
  2496. return ret;
  2497. }
  2498. static int show_format(WriterContext *w, InputFile *ifile)
  2499. {
  2500. AVFormatContext *fmt_ctx = ifile->fmt_ctx;
  2501. char val_str[128];
  2502. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  2503. int ret = 0;
  2504. writer_print_section_header(w, SECTION_ID_FORMAT);
  2505. print_str_validate("filename", fmt_ctx->url);
  2506. print_int("nb_streams", fmt_ctx->nb_streams);
  2507. print_int("nb_programs", fmt_ctx->nb_programs);
  2508. print_str("format_name", fmt_ctx->iformat->name);
  2509. if (!do_bitexact) {
  2510. if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
  2511. else print_str_opt("format_long_name", "unknown");
  2512. }
  2513. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  2514. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  2515. if (size >= 0) print_val ("size", size, unit_byte_str);
  2516. else print_str_opt("size", "N/A");
  2517. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  2518. else print_str_opt("bit_rate", "N/A");
  2519. print_int("probe_score", fmt_ctx->probe_score);
  2520. if (do_show_format_tags)
  2521. ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
  2522. writer_print_section_footer(w);
  2523. fflush(stdout);
  2524. return ret;
  2525. }
  2526. static void show_error(WriterContext *w, int err)
  2527. {
  2528. char errbuf[128];
  2529. const char *errbuf_ptr = errbuf;
  2530. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  2531. errbuf_ptr = strerror(AVUNERROR(err));
  2532. writer_print_section_header(w, SECTION_ID_ERROR);
  2533. print_int("code", err);
  2534. print_str("string", errbuf_ptr);
  2535. writer_print_section_footer(w);
  2536. }
  2537. static int open_input_file(InputFile *ifile, const char *filename,
  2538. const char *print_filename)
  2539. {
  2540. int err, i;
  2541. AVFormatContext *fmt_ctx = NULL;
  2542. AVDictionaryEntry *t = NULL;
  2543. int scan_all_pmts_set = 0;
  2544. fmt_ctx = avformat_alloc_context();
  2545. if (!fmt_ctx) {
  2546. print_error(filename, AVERROR(ENOMEM));
  2547. exit_program(1);
  2548. }
  2549. if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  2550. av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  2551. scan_all_pmts_set = 1;
  2552. }
  2553. if ((err = avformat_open_input(&fmt_ctx, filename,
  2554. iformat, &format_opts)) < 0) {
  2555. print_error(filename, err);
  2556. return err;
  2557. }
  2558. if (print_filename) {
  2559. av_freep(&fmt_ctx->url);
  2560. fmt_ctx->url = av_strdup(print_filename);
  2561. }
  2562. ifile->fmt_ctx = fmt_ctx;
  2563. if (scan_all_pmts_set)
  2564. av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  2565. while ((t = av_dict_get(format_opts, "", t, AV_DICT_IGNORE_SUFFIX)))
  2566. av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
  2567. if (find_stream_info) {
  2568. AVDictionary **opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
  2569. int orig_nb_streams = fmt_ctx->nb_streams;
  2570. err = avformat_find_stream_info(fmt_ctx, opts);
  2571. for (i = 0; i < orig_nb_streams; i++)
  2572. av_dict_free(&opts[i]);
  2573. av_freep(&opts);
  2574. if (err < 0) {
  2575. print_error(filename, err);
  2576. return err;
  2577. }
  2578. }
  2579. av_dump_format(fmt_ctx, 0, filename, 0);
  2580. ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
  2581. sizeof(*ifile->streams));
  2582. if (!ifile->streams)
  2583. exit(1);
  2584. ifile->nb_streams = fmt_ctx->nb_streams;
  2585. /* bind a decoder to each input stream */
  2586. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  2587. InputStream *ist = &ifile->streams[i];
  2588. AVStream *stream = fmt_ctx->streams[i];
  2589. const AVCodec *codec;
  2590. ist->st = stream;
  2591. if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
  2592. av_log(NULL, AV_LOG_WARNING,
  2593. "Failed to probe codec for input stream %d\n",
  2594. stream->index);
  2595. continue;
  2596. }
  2597. codec = avcodec_find_decoder(stream->codecpar->codec_id);
  2598. if (!codec) {
  2599. av_log(NULL, AV_LOG_WARNING,
  2600. "Unsupported codec with id %d for input stream %d\n",
  2601. stream->codecpar->codec_id, stream->index);
  2602. continue;
  2603. }
  2604. {
  2605. AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
  2606. fmt_ctx, stream, codec);
  2607. ist->dec_ctx = avcodec_alloc_context3(codec);
  2608. if (!ist->dec_ctx)
  2609. exit(1);
  2610. err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
  2611. if (err < 0)
  2612. exit(1);
  2613. if (do_show_log) {
  2614. // For loging it is needed to disable at least frame threads as otherwise
  2615. // the log information would need to be reordered and matches up to contexts and frames
  2616. // That is in fact possible but not trivial
  2617. av_dict_set(&codec_opts, "threads", "1", 0);
  2618. }
  2619. ist->dec_ctx->pkt_timebase = stream->time_base;
  2620. if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
  2621. av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
  2622. stream->index);
  2623. exit(1);
  2624. }
  2625. if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2626. av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
  2627. t->key, stream->index);
  2628. return AVERROR_OPTION_NOT_FOUND;
  2629. }
  2630. }
  2631. }
  2632. ifile->fmt_ctx = fmt_ctx;
  2633. return 0;
  2634. }
  2635. static void close_input_file(InputFile *ifile)
  2636. {
  2637. int i;
  2638. /* close decoder for each stream */
  2639. for (i = 0; i < ifile->nb_streams; i++)
  2640. if (ifile->streams[i].st->codecpar->codec_id != AV_CODEC_ID_NONE)
  2641. avcodec_free_context(&ifile->streams[i].dec_ctx);
  2642. av_freep(&ifile->streams);
  2643. ifile->nb_streams = 0;
  2644. avformat_close_input(&ifile->fmt_ctx);
  2645. }
  2646. static int probe_file(WriterContext *wctx, const char *filename,
  2647. const char *print_filename)
  2648. {
  2649. InputFile ifile = { 0 };
  2650. int ret, i;
  2651. int section_id;
  2652. do_read_frames = do_show_frames || do_count_frames;
  2653. do_read_packets = do_show_packets || do_count_packets;
  2654. ret = open_input_file(&ifile, filename, print_filename);
  2655. if (ret < 0)
  2656. goto end;
  2657. #define CHECK_END if (ret < 0) goto end
  2658. nb_streams = ifile.fmt_ctx->nb_streams;
  2659. REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
  2660. REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
  2661. REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
  2662. for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
  2663. if (stream_specifier) {
  2664. ret = avformat_match_stream_specifier(ifile.fmt_ctx,
  2665. ifile.fmt_ctx->streams[i],
  2666. stream_specifier);
  2667. CHECK_END;
  2668. else
  2669. selected_streams[i] = ret;
  2670. ret = 0;
  2671. } else {
  2672. selected_streams[i] = 1;
  2673. }
  2674. if (!selected_streams[i])
  2675. ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
  2676. }
  2677. if (do_read_frames || do_read_packets) {
  2678. if (do_show_frames && do_show_packets &&
  2679. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  2680. section_id = SECTION_ID_PACKETS_AND_FRAMES;
  2681. else if (do_show_packets && !do_show_frames)
  2682. section_id = SECTION_ID_PACKETS;
  2683. else // (!do_show_packets && do_show_frames)
  2684. section_id = SECTION_ID_FRAMES;
  2685. if (do_show_frames || do_show_packets)
  2686. writer_print_section_header(wctx, section_id);
  2687. ret = read_packets(wctx, &ifile);
  2688. if (do_show_frames || do_show_packets)
  2689. writer_print_section_footer(wctx);
  2690. CHECK_END;
  2691. }
  2692. if (do_show_programs) {
  2693. ret = show_programs(wctx, &ifile);
  2694. CHECK_END;
  2695. }
  2696. if (do_show_streams) {
  2697. ret = show_streams(wctx, &ifile);
  2698. CHECK_END;
  2699. }
  2700. if (do_show_chapters) {
  2701. ret = show_chapters(wctx, &ifile);
  2702. CHECK_END;
  2703. }
  2704. if (do_show_format) {
  2705. ret = show_format(wctx, &ifile);
  2706. CHECK_END;
  2707. }
  2708. end:
  2709. if (ifile.fmt_ctx)
  2710. close_input_file(&ifile);
  2711. av_freep(&nb_streams_frames);
  2712. av_freep(&nb_streams_packets);
  2713. av_freep(&selected_streams);
  2714. return ret;
  2715. }
  2716. static void show_usage(void)
  2717. {
  2718. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  2719. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  2720. av_log(NULL, AV_LOG_INFO, "\n");
  2721. }
  2722. static void ffprobe_show_program_version(WriterContext *w)
  2723. {
  2724. AVBPrint pbuf;
  2725. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  2726. writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
  2727. print_str("version", FFMPEG_VERSION);
  2728. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  2729. program_birth_year, CONFIG_THIS_YEAR);
  2730. print_str("compiler_ident", CC_IDENT);
  2731. print_str("configuration", FFMPEG_CONFIGURATION);
  2732. writer_print_section_footer(w);
  2733. av_bprint_finalize(&pbuf, NULL);
  2734. }
  2735. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  2736. do { \
  2737. if (CONFIG_##LIBNAME) { \
  2738. unsigned int version = libname##_version(); \
  2739. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
  2740. print_str("name", "lib" #libname); \
  2741. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  2742. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  2743. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  2744. print_int("version", version); \
  2745. print_str("ident", LIB##LIBNAME##_IDENT); \
  2746. writer_print_section_footer(w); \
  2747. } \
  2748. } while (0)
  2749. static void ffprobe_show_library_versions(WriterContext *w)
  2750. {
  2751. writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
  2752. SHOW_LIB_VERSION(avutil, AVUTIL);
  2753. SHOW_LIB_VERSION(avcodec, AVCODEC);
  2754. SHOW_LIB_VERSION(avformat, AVFORMAT);
  2755. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  2756. SHOW_LIB_VERSION(avfilter, AVFILTER);
  2757. SHOW_LIB_VERSION(swscale, SWSCALE);
  2758. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  2759. SHOW_LIB_VERSION(postproc, POSTPROC);
  2760. writer_print_section_footer(w);
  2761. }
  2762. #define PRINT_PIX_FMT_FLAG(flagname, name) \
  2763. do { \
  2764. print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
  2765. } while (0)
  2766. static void ffprobe_show_pixel_formats(WriterContext *w)
  2767. {
  2768. const AVPixFmtDescriptor *pixdesc = NULL;
  2769. int i, n;
  2770. writer_print_section_header(w, SECTION_ID_PIXEL_FORMATS);
  2771. while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
  2772. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT);
  2773. print_str("name", pixdesc->name);
  2774. print_int("nb_components", pixdesc->nb_components);
  2775. if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
  2776. print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
  2777. print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
  2778. } else {
  2779. print_str_opt("log2_chroma_w", "N/A");
  2780. print_str_opt("log2_chroma_h", "N/A");
  2781. }
  2782. n = av_get_bits_per_pixel(pixdesc);
  2783. if (n) print_int ("bits_per_pixel", n);
  2784. else print_str_opt("bits_per_pixel", "N/A");
  2785. if (do_show_pixel_format_flags) {
  2786. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_FLAGS);
  2787. PRINT_PIX_FMT_FLAG(BE, "big_endian");
  2788. PRINT_PIX_FMT_FLAG(PAL, "palette");
  2789. PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
  2790. PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
  2791. PRINT_PIX_FMT_FLAG(PLANAR, "planar");
  2792. PRINT_PIX_FMT_FLAG(RGB, "rgb");
  2793. PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
  2794. writer_print_section_footer(w);
  2795. }
  2796. if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
  2797. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENTS);
  2798. for (i = 0; i < pixdesc->nb_components; i++) {
  2799. writer_print_section_header(w, SECTION_ID_PIXEL_FORMAT_COMPONENT);
  2800. print_int("index", i + 1);
  2801. print_int("bit_depth", pixdesc->comp[i].depth);
  2802. writer_print_section_footer(w);
  2803. }
  2804. writer_print_section_footer(w);
  2805. }
  2806. writer_print_section_footer(w);
  2807. }
  2808. writer_print_section_footer(w);
  2809. }
  2810. static int opt_format(void *optctx, const char *opt, const char *arg)
  2811. {
  2812. iformat = av_find_input_format(arg);
  2813. if (!iformat) {
  2814. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  2815. return AVERROR(EINVAL);
  2816. }
  2817. return 0;
  2818. }
  2819. static inline void mark_section_show_entries(SectionID section_id,
  2820. int show_all_entries, AVDictionary *entries)
  2821. {
  2822. struct section *section = &sections[section_id];
  2823. section->show_all_entries = show_all_entries;
  2824. if (show_all_entries) {
  2825. SectionID *id;
  2826. for (id = section->children_ids; *id != -1; id++)
  2827. mark_section_show_entries(*id, show_all_entries, entries);
  2828. } else {
  2829. av_dict_copy(&section->entries_to_show, entries, 0);
  2830. }
  2831. }
  2832. static int match_section(const char *section_name,
  2833. int show_all_entries, AVDictionary *entries)
  2834. {
  2835. int i, ret = 0;
  2836. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
  2837. const struct section *section = &sections[i];
  2838. if (!strcmp(section_name, section->name) ||
  2839. (section->unique_name && !strcmp(section_name, section->unique_name))) {
  2840. av_log(NULL, AV_LOG_DEBUG,
  2841. "'%s' matches section with unique name '%s'\n", section_name,
  2842. (char *)av_x_if_null(section->unique_name, section->name));
  2843. ret++;
  2844. mark_section_show_entries(section->id, show_all_entries, entries);
  2845. }
  2846. }
  2847. return ret;
  2848. }
  2849. static int opt_show_entries(void *optctx, const char *opt, const char *arg)
  2850. {
  2851. const char *p = arg;
  2852. int ret = 0;
  2853. while (*p) {
  2854. AVDictionary *entries = NULL;
  2855. char *section_name = av_get_token(&p, "=:");
  2856. int show_all_entries = 0;
  2857. if (!section_name) {
  2858. av_log(NULL, AV_LOG_ERROR,
  2859. "Missing section name for option '%s'\n", opt);
  2860. return AVERROR(EINVAL);
  2861. }
  2862. if (*p == '=') {
  2863. p++;
  2864. while (*p && *p != ':') {
  2865. char *entry = av_get_token(&p, ",:");
  2866. if (!entry)
  2867. break;
  2868. av_log(NULL, AV_LOG_VERBOSE,
  2869. "Adding '%s' to the entries to show in section '%s'\n",
  2870. entry, section_name);
  2871. av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
  2872. if (*p == ',')
  2873. p++;
  2874. }
  2875. } else {
  2876. show_all_entries = 1;
  2877. }
  2878. ret = match_section(section_name, show_all_entries, entries);
  2879. if (ret == 0) {
  2880. av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
  2881. ret = AVERROR(EINVAL);
  2882. }
  2883. av_dict_free(&entries);
  2884. av_free(section_name);
  2885. if (ret <= 0)
  2886. break;
  2887. if (*p)
  2888. p++;
  2889. }
  2890. return ret;
  2891. }
  2892. static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
  2893. {
  2894. char *buf = av_asprintf("format=%s", arg);
  2895. int ret;
  2896. if (!buf)
  2897. return AVERROR(ENOMEM);
  2898. av_log(NULL, AV_LOG_WARNING,
  2899. "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
  2900. opt, arg);
  2901. ret = opt_show_entries(optctx, opt, buf);
  2902. av_free(buf);
  2903. return ret;
  2904. }
  2905. static void opt_input_file(void *optctx, const char *arg)
  2906. {
  2907. if (input_filename) {
  2908. av_log(NULL, AV_LOG_ERROR,
  2909. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  2910. arg, input_filename);
  2911. exit_program(1);
  2912. }
  2913. if (!strcmp(arg, "-"))
  2914. arg = "pipe:";
  2915. input_filename = arg;
  2916. }
  2917. static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
  2918. {
  2919. opt_input_file(optctx, arg);
  2920. return 0;
  2921. }
  2922. static int opt_print_filename(void *optctx, const char *opt, const char *arg)
  2923. {
  2924. print_input_filename = arg;
  2925. return 0;
  2926. }
  2927. void show_help_default(const char *opt, const char *arg)
  2928. {
  2929. av_log_set_callback(log_callback_help);
  2930. show_usage();
  2931. show_help_options(options, "Main options:", 0, 0, 0);
  2932. printf("\n");
  2933. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  2934. show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  2935. }
  2936. /**
  2937. * Parse interval specification, according to the format:
  2938. * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
  2939. * INTERVALS ::= INTERVAL[,INTERVALS]
  2940. */
  2941. static int parse_read_interval(const char *interval_spec,
  2942. ReadInterval *interval)
  2943. {
  2944. int ret = 0;
  2945. char *next, *p, *spec = av_strdup(interval_spec);
  2946. if (!spec)
  2947. return AVERROR(ENOMEM);
  2948. if (!*spec) {
  2949. av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
  2950. ret = AVERROR(EINVAL);
  2951. goto end;
  2952. }
  2953. p = spec;
  2954. next = strchr(spec, '%');
  2955. if (next)
  2956. *next++ = 0;
  2957. /* parse first part */
  2958. if (*p) {
  2959. interval->has_start = 1;
  2960. if (*p == '+') {
  2961. interval->start_is_offset = 1;
  2962. p++;
  2963. } else {
  2964. interval->start_is_offset = 0;
  2965. }
  2966. ret = av_parse_time(&interval->start, p, 1);
  2967. if (ret < 0) {
  2968. av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
  2969. goto end;
  2970. }
  2971. } else {
  2972. interval->has_start = 0;
  2973. }
  2974. /* parse second part */
  2975. p = next;
  2976. if (p && *p) {
  2977. int64_t us;
  2978. interval->has_end = 1;
  2979. if (*p == '+') {
  2980. interval->end_is_offset = 1;
  2981. p++;
  2982. } else {
  2983. interval->end_is_offset = 0;
  2984. }
  2985. if (interval->end_is_offset && *p == '#') {
  2986. long long int lli;
  2987. char *tail;
  2988. interval->duration_frames = 1;
  2989. p++;
  2990. lli = strtoll(p, &tail, 10);
  2991. if (*tail || lli < 0) {
  2992. av_log(NULL, AV_LOG_ERROR,
  2993. "Invalid or negative value '%s' for duration number of frames\n", p);
  2994. goto end;
  2995. }
  2996. interval->end = lli;
  2997. } else {
  2998. interval->duration_frames = 0;
  2999. ret = av_parse_time(&us, p, 1);
  3000. if (ret < 0) {
  3001. av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
  3002. goto end;
  3003. }
  3004. interval->end = us;
  3005. }
  3006. } else {
  3007. interval->has_end = 0;
  3008. }
  3009. end:
  3010. av_free(spec);
  3011. return ret;
  3012. }
  3013. static int parse_read_intervals(const char *intervals_spec)
  3014. {
  3015. int ret, n, i;
  3016. char *p, *spec = av_strdup(intervals_spec);
  3017. if (!spec)
  3018. return AVERROR(ENOMEM);
  3019. /* preparse specification, get number of intervals */
  3020. for (n = 0, p = spec; *p; p++)
  3021. if (*p == ',')
  3022. n++;
  3023. n++;
  3024. read_intervals = av_malloc_array(n, sizeof(*read_intervals));
  3025. if (!read_intervals) {
  3026. ret = AVERROR(ENOMEM);
  3027. goto end;
  3028. }
  3029. read_intervals_nb = n;
  3030. /* parse intervals */
  3031. p = spec;
  3032. for (i = 0; p; i++) {
  3033. char *next;
  3034. av_assert0(i < read_intervals_nb);
  3035. next = strchr(p, ',');
  3036. if (next)
  3037. *next++ = 0;
  3038. read_intervals[i].id = i;
  3039. ret = parse_read_interval(p, &read_intervals[i]);
  3040. if (ret < 0) {
  3041. av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
  3042. i, p);
  3043. goto end;
  3044. }
  3045. av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
  3046. log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
  3047. p = next;
  3048. }
  3049. av_assert0(i == read_intervals_nb);
  3050. end:
  3051. av_free(spec);
  3052. return ret;
  3053. }
  3054. static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
  3055. {
  3056. return parse_read_intervals(arg);
  3057. }
  3058. static int opt_pretty(void *optctx, const char *opt, const char *arg)
  3059. {
  3060. show_value_unit = 1;
  3061. use_value_prefix = 1;
  3062. use_byte_value_binary_prefix = 1;
  3063. use_value_sexagesimal_format = 1;
  3064. return 0;
  3065. }
  3066. static void print_section(SectionID id, int level)
  3067. {
  3068. const SectionID *pid;
  3069. const struct section *section = &sections[id];
  3070. printf("%c%c%c",
  3071. section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
  3072. section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
  3073. section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
  3074. printf("%*c %s", level * 4, ' ', section->name);
  3075. if (section->unique_name)
  3076. printf("/%s", section->unique_name);
  3077. printf("\n");
  3078. for (pid = section->children_ids; *pid != -1; pid++)
  3079. print_section(*pid, level+1);
  3080. }
  3081. static int opt_sections(void *optctx, const char *opt, const char *arg)
  3082. {
  3083. printf("Sections:\n"
  3084. "W.. = Section is a wrapper (contains other sections, no local entries)\n"
  3085. ".A. = Section contains an array of elements of the same type\n"
  3086. "..V = Section may contain a variable number of fields with variable keys\n"
  3087. "FLAGS NAME/UNIQUE_NAME\n"
  3088. "---\n");
  3089. print_section(SECTION_ID_ROOT, 0);
  3090. return 0;
  3091. }
  3092. static int opt_show_versions(void *optctx, const char *opt, const char *arg)
  3093. {
  3094. mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
  3095. mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
  3096. return 0;
  3097. }
  3098. #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
  3099. static int opt_show_##section(void *optctx, const char *opt, const char *arg) \
  3100. { \
  3101. mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
  3102. return 0; \
  3103. }
  3104. DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
  3105. DEFINE_OPT_SHOW_SECTION(error, ERROR)
  3106. DEFINE_OPT_SHOW_SECTION(format, FORMAT)
  3107. DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
  3108. DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
  3109. DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
  3110. DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
  3111. DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
  3112. DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
  3113. DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
  3114. static const OptionDef real_options[] = {
  3115. CMDUTILS_COMMON_OPTIONS
  3116. { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
  3117. { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
  3118. { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
  3119. { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
  3120. "use binary prefixes for byte units" },
  3121. { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
  3122. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  3123. { "pretty", 0, {.func_arg = opt_pretty},
  3124. "prettify the format of displayed values, make it more human readable" },
  3125. { "print_format", OPT_STRING | HAS_ARG, { &print_format },
  3126. "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
  3127. { "of", OPT_STRING | HAS_ARG, { &print_format }, "alias for -print_format", "format" },
  3128. { "select_streams", OPT_STRING | HAS_ARG, { &stream_specifier }, "select the specified streams", "stream_specifier" },
  3129. { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
  3130. { "show_data", OPT_BOOL, { &do_show_data }, "show packets data" },
  3131. { "show_data_hash", OPT_STRING | HAS_ARG, { &show_data_hash }, "show packets data hash" },
  3132. { "show_error", 0, { .func_arg = &opt_show_error }, "show probing error" },
  3133. { "show_format", 0, { .func_arg = &opt_show_format }, "show format/container info" },
  3134. { "show_frames", 0, { .func_arg = &opt_show_frames }, "show frames info" },
  3135. { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
  3136. "show a particular entry from the format/container info", "entry" },
  3137. { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
  3138. "show a set of specified entries", "entry_list" },
  3139. #if HAVE_THREADS
  3140. { "show_log", OPT_INT|HAS_ARG, { &do_show_log }, "show log" },
  3141. #endif
  3142. { "show_packets", 0, { .func_arg = &opt_show_packets }, "show packets info" },
  3143. { "show_programs", 0, { .func_arg = &opt_show_programs }, "show programs info" },
  3144. { "show_streams", 0, { .func_arg = &opt_show_streams }, "show streams info" },
  3145. { "show_chapters", 0, { .func_arg = &opt_show_chapters }, "show chapters info" },
  3146. { "count_frames", OPT_BOOL, { &do_count_frames }, "count the number of frames per stream" },
  3147. { "count_packets", OPT_BOOL, { &do_count_packets }, "count the number of packets per stream" },
  3148. { "show_program_version", 0, { .func_arg = &opt_show_program_version }, "show ffprobe version" },
  3149. { "show_library_versions", 0, { .func_arg = &opt_show_library_versions }, "show library versions" },
  3150. { "show_versions", 0, { .func_arg = &opt_show_versions }, "show program and library versions" },
  3151. { "show_pixel_formats", 0, { .func_arg = &opt_show_pixel_formats }, "show pixel format descriptions" },
  3152. { "show_private_data", OPT_BOOL, { &show_private_data }, "show private data" },
  3153. { "private", OPT_BOOL, { &show_private_data }, "same as show_private_data" },
  3154. { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
  3155. { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
  3156. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
  3157. { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
  3158. { "print_filename", HAS_ARG, {.func_arg = opt_print_filename}, "override the printed input filename", "print_file"},
  3159. { "find_stream_info", OPT_BOOL | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
  3160. "read and decode the streams to fill missing information with heuristics" },
  3161. { NULL, },
  3162. };
  3163. static inline int check_section_show_entries(int section_id)
  3164. {
  3165. int *id;
  3166. struct section *section = &sections[section_id];
  3167. if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
  3168. return 1;
  3169. for (id = section->children_ids; *id != -1; id++)
  3170. if (check_section_show_entries(*id))
  3171. return 1;
  3172. return 0;
  3173. }
  3174. #define SET_DO_SHOW(id, varname) do { \
  3175. if (check_section_show_entries(SECTION_ID_##id)) \
  3176. do_show_##varname = 1; \
  3177. } while (0)
  3178. int main(int argc, char **argv)
  3179. {
  3180. const Writer *w;
  3181. WriterContext *wctx;
  3182. char *buf;
  3183. char *w_name = NULL, *w_args = NULL;
  3184. int ret, i;
  3185. init_dynload();
  3186. #if HAVE_THREADS
  3187. ret = pthread_mutex_init(&log_mutex, NULL);
  3188. if (ret != 0) {
  3189. goto end;
  3190. }
  3191. #endif
  3192. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  3193. register_exit(ffprobe_cleanup);
  3194. options = real_options;
  3195. parse_loglevel(argc, argv, options);
  3196. avformat_network_init();
  3197. init_opts();
  3198. #if CONFIG_AVDEVICE
  3199. avdevice_register_all();
  3200. #endif
  3201. show_banner(argc, argv, options);
  3202. parse_options(NULL, argc, argv, options, opt_input_file);
  3203. if (do_show_log)
  3204. av_log_set_callback(log_callback);
  3205. /* mark things to show, based on -show_entries */
  3206. SET_DO_SHOW(CHAPTERS, chapters);
  3207. SET_DO_SHOW(ERROR, error);
  3208. SET_DO_SHOW(FORMAT, format);
  3209. SET_DO_SHOW(FRAMES, frames);
  3210. SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
  3211. SET_DO_SHOW(PACKETS, packets);
  3212. SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
  3213. SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
  3214. SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
  3215. SET_DO_SHOW(PROGRAM_VERSION, program_version);
  3216. SET_DO_SHOW(PROGRAMS, programs);
  3217. SET_DO_SHOW(STREAMS, streams);
  3218. SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
  3219. SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
  3220. SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
  3221. SET_DO_SHOW(FORMAT_TAGS, format_tags);
  3222. SET_DO_SHOW(FRAME_TAGS, frame_tags);
  3223. SET_DO_SHOW(PROGRAM_TAGS, program_tags);
  3224. SET_DO_SHOW(STREAM_TAGS, stream_tags);
  3225. SET_DO_SHOW(PROGRAM_STREAM_TAGS, stream_tags);
  3226. SET_DO_SHOW(PACKET_TAGS, packet_tags);
  3227. if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
  3228. av_log(NULL, AV_LOG_ERROR,
  3229. "-bitexact and -show_program_version or -show_library_versions "
  3230. "options are incompatible\n");
  3231. ret = AVERROR(EINVAL);
  3232. goto end;
  3233. }
  3234. writer_register_all();
  3235. if (!print_format)
  3236. print_format = av_strdup("default");
  3237. if (!print_format) {
  3238. ret = AVERROR(ENOMEM);
  3239. goto end;
  3240. }
  3241. w_name = av_strtok(print_format, "=", &buf);
  3242. if (!w_name) {
  3243. av_log(NULL, AV_LOG_ERROR,
  3244. "No name specified for the output format\n");
  3245. ret = AVERROR(EINVAL);
  3246. goto end;
  3247. }
  3248. w_args = buf;
  3249. if (show_data_hash) {
  3250. if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
  3251. if (ret == AVERROR(EINVAL)) {
  3252. const char *n;
  3253. av_log(NULL, AV_LOG_ERROR,
  3254. "Unknown hash algorithm '%s'\nKnown algorithms:",
  3255. show_data_hash);
  3256. for (i = 0; (n = av_hash_names(i)); i++)
  3257. av_log(NULL, AV_LOG_ERROR, " %s", n);
  3258. av_log(NULL, AV_LOG_ERROR, "\n");
  3259. }
  3260. goto end;
  3261. }
  3262. }
  3263. w = writer_get_by_name(w_name);
  3264. if (!w) {
  3265. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  3266. ret = AVERROR(EINVAL);
  3267. goto end;
  3268. }
  3269. if ((ret = writer_open(&wctx, w, w_args,
  3270. sections, FF_ARRAY_ELEMS(sections))) >= 0) {
  3271. if (w == &xml_writer)
  3272. wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
  3273. writer_print_section_header(wctx, SECTION_ID_ROOT);
  3274. if (do_show_program_version)
  3275. ffprobe_show_program_version(wctx);
  3276. if (do_show_library_versions)
  3277. ffprobe_show_library_versions(wctx);
  3278. if (do_show_pixel_formats)
  3279. ffprobe_show_pixel_formats(wctx);
  3280. if (!input_filename &&
  3281. ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
  3282. (!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
  3283. show_usage();
  3284. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  3285. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  3286. ret = AVERROR(EINVAL);
  3287. } else if (input_filename) {
  3288. ret = probe_file(wctx, input_filename, print_input_filename);
  3289. if (ret < 0 && do_show_error)
  3290. show_error(wctx, ret);
  3291. }
  3292. writer_print_section_footer(wctx);
  3293. writer_close(&wctx);
  3294. }
  3295. end:
  3296. av_freep(&print_format);
  3297. av_freep(&read_intervals);
  3298. av_hash_freep(&hash);
  3299. uninit_opts();
  3300. for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
  3301. av_dict_free(&(sections[i].entries_to_show));
  3302. avformat_network_deinit();
  3303. return ret < 0;
  3304. }