FFmpeg
ffprobe.c
Go to the documentation of this file.
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 /**
22  * @file
23  * simple media prober based on the FFmpeg libraries
24  */
25 
26 #include "config.h"
27 #include "libavutil/ffversion.h"
28 
29 #include <string.h>
30 #include <math.h>
31 
32 #include "libavformat/avformat.h"
33 #include "libavformat/version.h"
34 #include "libavcodec/avcodec.h"
35 #include "libavcodec/version.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/bprint.h"
41 #include "libavutil/display.h"
42 #include "libavutil/hash.h"
46 #include "libavutil/dovi_meta.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/spherical.h"
50 #include "libavutil/stereo3d.h"
51 #include "libavutil/dict.h"
52 #include "libavutil/intreadwrite.h"
53 #include "libavutil/libm.h"
54 #include "libavutil/parseutils.h"
55 #include "libavutil/timecode.h"
56 #include "libavutil/timestamp.h"
57 #include "libavdevice/avdevice.h"
58 #include "libavdevice/version.h"
59 #include "libswscale/swscale.h"
60 #include "libswscale/version.h"
62 #include "libswresample/version.h"
64 #include "libpostproc/version.h"
65 #include "libavfilter/version.h"
66 #include "cmdutils.h"
67 #include "opt_common.h"
68 
69 #include "libavutil/thread.h"
70 
71 #if !HAVE_THREADS
72 # ifdef pthread_mutex_lock
73 # undef pthread_mutex_lock
74 # endif
75 # define pthread_mutex_lock(a) do{}while(0)
76 # ifdef pthread_mutex_unlock
77 # undef pthread_mutex_unlock
78 # endif
79 # define pthread_mutex_unlock(a) do{}while(0)
80 #endif
81 
82 typedef struct InputStream {
83  AVStream *st;
84 
86 } InputStream;
87 
88 typedef struct InputFile {
90 
92  int nb_streams;
93 } InputFile;
94 
95 const char program_name[] = "ffprobe";
96 const int program_birth_year = 2007;
97 
98 static int do_bitexact = 0;
99 static int do_count_frames = 0;
100 static int do_count_packets = 0;
101 static int do_read_frames = 0;
102 static int do_read_packets = 0;
103 static int do_show_chapters = 0;
104 static int do_show_error = 0;
105 static int do_show_format = 0;
106 static int do_show_frames = 0;
107 static int do_show_packets = 0;
108 static int do_show_programs = 0;
109 static int do_show_streams = 0;
111 static int do_show_data = 0;
112 static int do_show_program_version = 0;
114 static int do_show_pixel_formats = 0;
117 static int do_show_log = 0;
118 
119 static int do_show_chapter_tags = 0;
120 static int do_show_format_tags = 0;
121 static int do_show_frame_tags = 0;
122 static int do_show_program_tags = 0;
123 static int do_show_stream_tags = 0;
124 static int do_show_packet_tags = 0;
125 
126 static int show_value_unit = 0;
127 static int use_value_prefix = 0;
130 static int show_private_data = 1;
131 
132 #define SHOW_OPTIONAL_FIELDS_AUTO -1
133 #define SHOW_OPTIONAL_FIELDS_NEVER 0
134 #define SHOW_OPTIONAL_FIELDS_ALWAYS 1
136 
137 static char *print_format;
138 static char *stream_specifier;
139 static char *show_data_hash;
140 
141 typedef struct ReadInterval {
142  int id; ///< identifier
143  int64_t start, end; ///< start, end in second/AV_TIME_BASE units
147 } ReadInterval;
148 
150 static int read_intervals_nb = 0;
151 
152 static int find_stream_info = 1;
153 
154 /* section structure definition */
155 
156 #define SECTION_MAX_NB_CHILDREN 10
157 
158 struct section {
159  int id; ///< unique id identifying a section
160  const char *name;
161 
162 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
163 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
164 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
165  /// For these sections the element_name field is mandatory.
166  int flags;
167  int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
168  const char *element_name; ///< name of the contained element, if provided
169  const char *unique_name; ///< unique section name, in case the name is ambiguous
172 };
173 
174 typedef enum {
224 } SectionID;
225 
226 static struct section sections[] = {
228  [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
229  [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
230  [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
231  [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
232  [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
235  [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
236  [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" },
245  [SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
247  [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
251  [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
252  [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" },
253  [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", 0, { -1 }, .unique_name = "packet_side_data" },
256  [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
257  [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" },
259  [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
260  [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" },
262  [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
264  [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
265  [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
273  [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
274  [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
275  [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" },
276  [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", 0, { -1 }, .unique_name = "stream_side_data" },
277  [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
278 };
279 
280 static const OptionDef *options;
281 
282 /* FFprobe context */
283 static const char *input_filename;
284 static const char *print_input_filename;
285 static const AVInputFormat *iformat = NULL;
286 static const char *output_filename = NULL;
287 
288 static struct AVHashContext *hash;
289 
290 static const struct {
291  double bin_val;
292  double dec_val;
293  const char *bin_str;
294  const char *dec_str;
295 } si_prefixes[] = {
296  { 1.0, 1.0, "", "" },
297  { 1.024e3, 1e3, "Ki", "K" },
298  { 1.048576e6, 1e6, "Mi", "M" },
299  { 1.073741824e9, 1e9, "Gi", "G" },
300  { 1.099511627776e12, 1e12, "Ti", "T" },
301  { 1.125899906842624e15, 1e15, "Pi", "P" },
302 };
303 
304 static const char unit_second_str[] = "s" ;
305 static const char unit_hertz_str[] = "Hz" ;
306 static const char unit_byte_str[] = "byte" ;
307 static const char unit_bit_per_second_str[] = "bit/s";
308 
309 static int nb_streams;
310 static uint64_t *nb_streams_packets;
311 static uint64_t *nb_streams_frames;
312 static int *selected_streams;
313 
314 #if HAVE_THREADS
315 pthread_mutex_t log_mutex;
316 #endif
317 typedef struct LogBuffer {
320  char *log_message;
322  char *parent_name;
324 }LogBuffer;
325 
327 static int log_buffer_size;
328 
329 static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
330 {
331  AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
332  va_list vl2;
333  char line[1024];
334  static int print_prefix = 1;
335  void *new_log_buffer;
336 
337  va_copy(vl2, vl);
338  av_log_default_callback(ptr, level, fmt, vl);
339  av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
340  va_end(vl2);
341 
342 #if HAVE_THREADS
343  pthread_mutex_lock(&log_mutex);
344 
345  new_log_buffer = av_realloc_array(log_buffer, log_buffer_size + 1, sizeof(*log_buffer));
346  if (new_log_buffer) {
347  char *msg;
348  int i;
349 
350  log_buffer = new_log_buffer;
351  memset(&log_buffer[log_buffer_size], 0, sizeof(log_buffer[log_buffer_size]));
353  if (avc) {
356  }
359  for (i=strlen(msg) - 1; i>=0 && msg[i] == '\n'; i--) {
360  msg[i] = 0;
361  }
362  if (avc && avc->parent_log_context_offset) {
363  AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
365  if (parent && *parent) {
366  log_buffer[log_buffer_size].parent_name = av_strdup((*parent)->item_name(parent));
368  (*parent)->get_category ? (*parent)->get_category(parent) :(*parent)->category;
369  }
370  }
371  log_buffer_size ++;
372  }
373 
374  pthread_mutex_unlock(&log_mutex);
375 #endif
376 }
377 
378 static void ffprobe_cleanup(int ret)
379 {
380  int i;
381  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
382  av_dict_free(&(sections[i].entries_to_show));
383 
384 #if HAVE_THREADS
385  pthread_mutex_destroy(&log_mutex);
386 #endif
387 }
388 
389 struct unit_value {
390  union { double d; long long int i; } val;
391  const char *unit;
392 };
393 
394 static char *value_string(char *buf, int buf_size, struct unit_value uv)
395 {
396  double vald;
397  long long int vali;
398  int show_float = 0;
399 
400  if (uv.unit == unit_second_str) {
401  vald = uv.val.d;
402  show_float = 1;
403  } else {
404  vald = vali = uv.val.i;
405  }
406 
408  double secs;
409  int hours, mins;
410  secs = vald;
411  mins = (int)secs / 60;
412  secs = secs - mins * 60;
413  hours = mins / 60;
414  mins %= 60;
415  snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
416  } else {
417  const char *prefix_string = "";
418 
419  if (use_value_prefix && vald > 1) {
420  long long int index;
421 
423  index = (long long int) (log2(vald)) / 10;
425  vald /= si_prefixes[index].bin_val;
426  prefix_string = si_prefixes[index].bin_str;
427  } else {
428  index = (long long int) (log10(vald)) / 3;
430  vald /= si_prefixes[index].dec_val;
431  prefix_string = si_prefixes[index].dec_str;
432  }
433  vali = vald;
434  }
435 
436  if (show_float || (use_value_prefix && vald != (long long int)vald))
437  snprintf(buf, buf_size, "%f", vald);
438  else
439  snprintf(buf, buf_size, "%lld", vali);
440  av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
441  prefix_string, show_value_unit ? uv.unit : "");
442  }
443 
444  return buf;
445 }
446 
447 /* WRITERS API */
448 
449 typedef struct WriterContext WriterContext;
450 
451 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
452 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
453 
454 typedef enum {
460 
461 typedef struct Writer {
462  const AVClass *priv_class; ///< private class of the writer, if any
463  int priv_size; ///< private size for the writer context
464  const char *name;
465 
466  int (*init) (WriterContext *wctx);
467  void (*uninit)(WriterContext *wctx);
468 
471  void (*print_integer) (WriterContext *wctx, const char *, long long int);
472  void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
473  void (*print_string) (WriterContext *wctx, const char *, const char *);
474  int flags; ///< a combination or WRITER_FLAG_*
475 } Writer;
476 
477 #define SECTION_MAX_NB_LEVELS 10
478 
480  const AVClass *class; ///< class of the writer
481  const Writer *writer; ///< the Writer of which this is an instance
482  AVIOContext *avio; ///< the I/O context used to write
483 
484  void (* writer_w8)(WriterContext *wctx, int b);
485  void (* writer_put_str)(WriterContext *wctx, const char *str);
486  void (* writer_printf)(WriterContext *wctx, const char *fmt, ...);
487 
488  char *name; ///< name of this writer instance
489  void *priv; ///< private data for use by the filter
490 
491  const struct section *sections; ///< array containing all sections
492  int nb_sections; ///< number of sections
493 
494  int level; ///< current level, starting from 0
495 
496  /** number of the item printed in the given section, starting from 0 */
498 
499  /** section per each level */
501  AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
502  /// used by various writers
503 
504  unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
505  unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
506  unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
507 
511 };
512 
513 static const char *writer_get_name(void *p)
514 {
515  WriterContext *wctx = p;
516  return wctx->writer->name;
517 }
518 
519 #define OFFSET(x) offsetof(WriterContext, x)
520 
521 static const AVOption writer_options[] = {
522  { "string_validation", "set string validation mode",
523  OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
524  { "sv", "set string validation mode",
525  OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
526  { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
527  { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
528  { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
529  { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
530  { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
531  { NULL }
532 };
533 
534 static void *writer_child_next(void *obj, void *prev)
535 {
536  WriterContext *ctx = obj;
537  if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
538  return ctx->priv;
539  return NULL;
540 }
541 
542 static const AVClass writer_class = {
543  .class_name = "Writer",
544  .item_name = writer_get_name,
545  .option = writer_options,
546  .version = LIBAVUTIL_VERSION_INT,
547  .child_next = writer_child_next,
548 };
549 
550 static int writer_close(WriterContext **wctx)
551 {
552  int i;
553  int ret = 0;
554 
555  if (!*wctx)
556  return -1;
557 
558  if ((*wctx)->writer->uninit)
559  (*wctx)->writer->uninit(*wctx);
560  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
561  av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
562  if ((*wctx)->writer->priv_class)
563  av_opt_free((*wctx)->priv);
564  av_freep(&((*wctx)->priv));
565  av_opt_free(*wctx);
566  if ((*wctx)->avio) {
567  avio_flush((*wctx)->avio);
568  ret = avio_close((*wctx)->avio);
569  }
570  av_freep(wctx);
571  return ret;
572 }
573 
574 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
575 {
576  int i;
577  av_bprintf(bp, "0X");
578  for (i = 0; i < ubuf_size; i++)
579  av_bprintf(bp, "%02X", ubuf[i]);
580 }
581 
582 static inline void writer_w8_avio(WriterContext *wctx, int b)
583 {
584  avio_w8(wctx->avio, b);
585 }
586 
587 static inline void writer_put_str_avio(WriterContext *wctx, const char *str)
588 {
589  avio_write(wctx->avio, str, strlen(str));
590 }
591 
592 static inline void writer_printf_avio(WriterContext *wctx, const char *fmt, ...)
593 {
594  va_list ap;
595 
596  va_start(ap, fmt);
597  avio_vprintf(wctx->avio, fmt, ap);
598  va_end(ap);
599 }
600 
601 static inline void writer_w8_printf(WriterContext *wctx, int b)
602 {
603  printf("%c", b);
604 }
605 
606 static inline void writer_put_str_printf(WriterContext *wctx, const char *str)
607 {
608  printf("%s", str);
609 }
610 
611 static inline void writer_printf_printf(WriterContext *wctx, const char *fmt, ...)
612 {
613  va_list ap;
614 
615  va_start(ap, fmt);
616  vprintf(fmt, ap);
617  va_end(ap);
618 }
619 
620 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
621  const struct section *sections, int nb_sections, const char *output)
622 {
623  int i, ret = 0;
624 
625  if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
626  ret = AVERROR(ENOMEM);
627  goto fail;
628  }
629 
630  if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
631  ret = AVERROR(ENOMEM);
632  goto fail;
633  }
634 
635  (*wctx)->class = &writer_class;
636  (*wctx)->writer = writer;
637  (*wctx)->level = -1;
638  (*wctx)->sections = sections;
639  (*wctx)->nb_sections = nb_sections;
640 
641  av_opt_set_defaults(*wctx);
642 
643  if (writer->priv_class) {
644  void *priv_ctx = (*wctx)->priv;
645  *((const AVClass **)priv_ctx) = writer->priv_class;
646  av_opt_set_defaults(priv_ctx);
647  }
648 
649  /* convert options to dictionary */
650  if (args) {
652  const AVDictionaryEntry *opt = NULL;
653 
654  if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
655  av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
656  av_dict_free(&opts);
657  goto fail;
658  }
659 
660  while ((opt = av_dict_iterate(opts, opt))) {
661  if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
662  av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
663  opt->key, opt->value);
664  av_dict_free(&opts);
665  goto fail;
666  }
667  }
668 
669  av_dict_free(&opts);
670  }
671 
672  /* validate replace string */
673  {
674  const uint8_t *p = (*wctx)->string_validation_replacement;
675  const uint8_t *endp = p + strlen(p);
676  while (*p) {
677  const uint8_t *p0 = p;
678  int32_t code;
679  ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
680  if (ret < 0) {
681  AVBPrint bp;
683  bprint_bytes(&bp, p0, p-p0),
684  av_log(wctx, AV_LOG_ERROR,
685  "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
686  bp.str, (*wctx)->string_validation_replacement);
687  return ret;
688  }
689  }
690  }
691 
692  if (!output_filename) {
693  (*wctx)->writer_w8 = writer_w8_printf;
694  (*wctx)->writer_put_str = writer_put_str_printf;
695  (*wctx)->writer_printf = writer_printf_printf;
696  } else {
697  if ((ret = avio_open(&(*wctx)->avio, output, AVIO_FLAG_WRITE)) < 0) {
698  av_log(*wctx, AV_LOG_ERROR,
699  "Failed to open output '%s' with error: %s\n", output, av_err2str(ret));
700  goto fail;
701  }
702  (*wctx)->writer_w8 = writer_w8_avio;
703  (*wctx)->writer_put_str = writer_put_str_avio;
704  (*wctx)->writer_printf = writer_printf_avio;
705  }
706 
707  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
708  av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
709 
710  if ((*wctx)->writer->init)
711  ret = (*wctx)->writer->init(*wctx);
712  if (ret < 0)
713  goto fail;
714 
715  return 0;
716 
717 fail:
718  writer_close(wctx);
719  return ret;
720 }
721 
723  int section_id)
724 {
725  int parent_section_id;
726  wctx->level++;
728  parent_section_id = wctx->level ?
729  (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
730 
731  wctx->nb_item[wctx->level] = 0;
732  wctx->section[wctx->level] = &wctx->sections[section_id];
733 
734  if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
735  wctx->nb_section_packet = wctx->nb_section_frame =
736  wctx->nb_section_packet_frame = 0;
737  } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
738  wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
739  wctx->nb_section_packet : wctx->nb_section_frame;
740  }
741 
742  if (wctx->writer->print_section_header)
743  wctx->writer->print_section_header(wctx);
744 }
745 
747 {
748  int section_id = wctx->section[wctx->level]->id;
749  int parent_section_id = wctx->level ?
750  wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
751 
752  if (parent_section_id != SECTION_ID_NONE)
753  wctx->nb_item[wctx->level-1]++;
754  if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
755  if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
756  else wctx->nb_section_frame++;
757  }
758  if (wctx->writer->print_section_footer)
759  wctx->writer->print_section_footer(wctx);
760  wctx->level--;
761 }
762 
763 static inline void writer_print_integer(WriterContext *wctx,
764  const char *key, long long int val)
765 {
766  const struct section *section = wctx->section[wctx->level];
767 
769  wctx->writer->print_integer(wctx, key, val);
770  wctx->nb_item[wctx->level]++;
771  }
772 }
773 
774 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
775 {
776  const uint8_t *p, *endp;
777  AVBPrint dstbuf;
778  int invalid_chars_nb = 0, ret = 0;
779 
781 
782  endp = src + strlen(src);
783  for (p = (uint8_t *)src; *p;) {
784  uint32_t code;
785  int invalid = 0;
786  const uint8_t *p0 = p;
787 
788  if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
789  AVBPrint bp;
791  bprint_bytes(&bp, p0, p-p0);
792  av_log(wctx, AV_LOG_DEBUG,
793  "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
794  invalid = 1;
795  }
796 
797  if (invalid) {
798  invalid_chars_nb++;
799 
800  switch (wctx->string_validation) {
802  av_log(wctx, AV_LOG_ERROR,
803  "Invalid UTF-8 sequence found in string '%s'\n", src);
805  goto end;
806  break;
807 
809  av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
810  break;
811  }
812  }
813 
814  if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
815  av_bprint_append_data(&dstbuf, p0, p-p0);
816  }
817 
818  if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
819  av_log(wctx, AV_LOG_WARNING,
820  "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
821  invalid_chars_nb, src, wctx->string_validation_replacement);
822  }
823 
824 end:
825  av_bprint_finalize(&dstbuf, dstp);
826  return ret;
827 }
828 
829 #define PRINT_STRING_OPT 1
830 #define PRINT_STRING_VALIDATE 2
831 
832 static inline int writer_print_string(WriterContext *wctx,
833  const char *key, const char *val, int flags)
834 {
835  const struct section *section = wctx->section[wctx->level];
836  int ret = 0;
837 
840  && (flags & PRINT_STRING_OPT)
842  return 0;
843 
846  char *key1 = NULL, *val1 = NULL;
847  ret = validate_string(wctx, &key1, key);
848  if (ret < 0) goto end;
849  ret = validate_string(wctx, &val1, val);
850  if (ret < 0) goto end;
851  wctx->writer->print_string(wctx, key1, val1);
852  end:
853  if (ret < 0) {
854  av_log(wctx, AV_LOG_ERROR,
855  "Invalid key=value string combination %s=%s in section %s\n",
857  }
858  av_free(key1);
859  av_free(val1);
860  } else {
861  wctx->writer->print_string(wctx, key, val);
862  }
863 
864  wctx->nb_item[wctx->level]++;
865  }
866 
867  return ret;
868 }
869 
870 static inline void writer_print_rational(WriterContext *wctx,
871  const char *key, AVRational q, char sep)
872 {
873  AVBPrint buf;
875  av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
876  writer_print_string(wctx, key, buf.str, 0);
877 }
878 
879 static void writer_print_time(WriterContext *wctx, const char *key,
880  int64_t ts, const AVRational *time_base, int is_duration)
881 {
882  char buf[128];
883 
884  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
886  } else {
887  double d = ts * av_q2d(*time_base);
888  struct unit_value uv;
889  uv.val.d = d;
890  uv.unit = unit_second_str;
891  value_string(buf, sizeof(buf), uv);
892  writer_print_string(wctx, key, buf, 0);
893  }
894 }
895 
896 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
897 {
898  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
900  } else {
901  writer_print_integer(wctx, key, ts);
902  }
903 }
904 
905 static void writer_print_data(WriterContext *wctx, const char *name,
906  const uint8_t *data, int size)
907 {
908  AVBPrint bp;
909  int offset = 0, l, i;
910 
912  av_bprintf(&bp, "\n");
913  while (size) {
914  av_bprintf(&bp, "%08x: ", offset);
915  l = FFMIN(size, 16);
916  for (i = 0; i < l; i++) {
917  av_bprintf(&bp, "%02x", data[i]);
918  if (i & 1)
919  av_bprintf(&bp, " ");
920  }
921  av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
922  for (i = 0; i < l; i++)
923  av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
924  av_bprintf(&bp, "\n");
925  offset += l;
926  data += l;
927  size -= l;
928  }
929  writer_print_string(wctx, name, bp.str, 0);
930  av_bprint_finalize(&bp, NULL);
931 }
932 
933 static void writer_print_data_hash(WriterContext *wctx, const char *name,
934  const uint8_t *data, int size)
935 {
936  char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
937 
938  if (!hash)
939  return;
942  snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
943  p = buf + strlen(buf);
944  av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
945  writer_print_string(wctx, name, buf, 0);
946 }
947 
948 static void writer_print_integers(WriterContext *wctx, const char *name,
949  uint8_t *data, int size, const char *format,
950  int columns, int bytes, int offset_add)
951 {
952  AVBPrint bp;
953  int offset = 0, l, i;
954 
956  av_bprintf(&bp, "\n");
957  while (size) {
958  av_bprintf(&bp, "%08x: ", offset);
959  l = FFMIN(size, columns);
960  for (i = 0; i < l; i++) {
961  if (bytes == 1) av_bprintf(&bp, format, *data);
962  else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
963  else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
964  data += bytes;
965  size --;
966  }
967  av_bprintf(&bp, "\n");
968  offset += offset_add;
969  }
970  writer_print_string(wctx, name, bp.str, 0);
971  av_bprint_finalize(&bp, NULL);
972 }
973 
974 #define writer_w8(wctx_, b_) (wctx_)->writer_w8(wctx_, b_)
975 #define writer_put_str(wctx_, str_) (wctx_)->writer_put_str(wctx_, str_)
976 #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer_printf(wctx_, fmt_, __VA_ARGS__)
977 
978 #define MAX_REGISTERED_WRITERS_NB 64
979 
981 
982 static int writer_register(const Writer *writer)
983 {
984  static int next_registered_writer_idx = 0;
985 
986  if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
987  return AVERROR(ENOMEM);
988 
989  registered_writers[next_registered_writer_idx++] = writer;
990  return 0;
991 }
992 
993 static const Writer *writer_get_by_name(const char *name)
994 {
995  int i;
996 
997  for (i = 0; registered_writers[i]; i++)
998  if (!strcmp(registered_writers[i]->name, name))
999  return registered_writers[i];
1000 
1001  return NULL;
1002 }
1003 
1004 
1005 /* WRITERS */
1006 
1007 #define DEFINE_WRITER_CLASS(name) \
1008 static const char *name##_get_name(void *ctx) \
1009 { \
1010  return #name ; \
1011 } \
1012 static const AVClass name##_class = { \
1013  .class_name = #name, \
1014  .item_name = name##_get_name, \
1015  .option = name##_options \
1016 }
1017 
1018 /* Default output */
1019 
1020 typedef struct DefaultContext {
1021  const AVClass *class;
1022  int nokey;
1025 } DefaultContext;
1026 
1027 #undef OFFSET
1028 #define OFFSET(x) offsetof(DefaultContext, x)
1029 
1030 static const AVOption default_options[] = {
1031  { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1032  { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1033  { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1034  { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1035  {NULL},
1036 };
1037 
1038 DEFINE_WRITER_CLASS(default);
1039 
1040 /* lame uppercasing routine, assumes the string is lower case ASCII */
1041 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
1042 {
1043  int i;
1044  for (i = 0; src[i] && i < dst_size-1; i++)
1045  dst[i] = av_toupper(src[i]);
1046  dst[i] = 0;
1047  return dst;
1048 }
1049 
1051 {
1052  DefaultContext *def = wctx->priv;
1053  char buf[32];
1054  const struct section *section = wctx->section[wctx->level];
1055  const struct section *parent_section = wctx->level ?
1056  wctx->section[wctx->level-1] : NULL;
1057 
1058  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1059  if (parent_section &&
1060  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1061  def->nested_section[wctx->level] = 1;
1062  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1063  wctx->section_pbuf[wctx->level-1].str,
1064  upcase_string(buf, sizeof(buf),
1066  }
1067 
1068  if (def->noprint_wrappers || def->nested_section[wctx->level])
1069  return;
1070 
1072  writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
1073 }
1074 
1076 {
1077  DefaultContext *def = wctx->priv;
1078  const struct section *section = wctx->section[wctx->level];
1079  char buf[32];
1080 
1081  if (def->noprint_wrappers || def->nested_section[wctx->level])
1082  return;
1083 
1085  writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
1086 }
1087 
1088 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
1089 {
1090  DefaultContext *def = wctx->priv;
1091 
1092  if (!def->nokey)
1093  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1094  writer_printf(wctx, "%s\n", value);
1095 }
1096 
1097 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
1098 {
1099  DefaultContext *def = wctx->priv;
1100 
1101  if (!def->nokey)
1102  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1103  writer_printf(wctx, "%lld\n", value);
1104 }
1105 
1106 static const Writer default_writer = {
1107  .name = "default",
1108  .priv_size = sizeof(DefaultContext),
1111  .print_integer = default_print_int,
1112  .print_string = default_print_str,
1114  .priv_class = &default_class,
1115 };
1116 
1117 /* Compact output */
1118 
1119 /**
1120  * Apply C-language-like string escaping.
1121  */
1122 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1123 {
1124  const char *p;
1125 
1126  for (p = src; *p; p++) {
1127  switch (*p) {
1128  case '\b': av_bprintf(dst, "%s", "\\b"); break;
1129  case '\f': av_bprintf(dst, "%s", "\\f"); break;
1130  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1131  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1132  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1133  default:
1134  if (*p == sep)
1135  av_bprint_chars(dst, '\\', 1);
1136  av_bprint_chars(dst, *p, 1);
1137  }
1138  }
1139  return dst->str;
1140 }
1141 
1142 /**
1143  * Quote fields containing special characters, check RFC4180.
1144  */
1145 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1146 {
1147  char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
1148  int needs_quoting = !!src[strcspn(src, meta_chars)];
1149 
1150  if (needs_quoting)
1151  av_bprint_chars(dst, '"', 1);
1152 
1153  for (; *src; src++) {
1154  if (*src == '"')
1155  av_bprint_chars(dst, '"', 1);
1156  av_bprint_chars(dst, *src, 1);
1157  }
1158  if (needs_quoting)
1159  av_bprint_chars(dst, '"', 1);
1160  return dst->str;
1161 }
1162 
1163 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1164 {
1165  return src;
1166 }
1167 
1168 typedef struct CompactContext {
1169  const AVClass *class;
1171  char item_sep;
1172  int nokey;
1175  const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
1179 } CompactContext;
1180 
1181 #undef OFFSET
1182 #define OFFSET(x) offsetof(CompactContext, x)
1183 
1184 static const AVOption compact_options[]= {
1185  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1186  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1187  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1188  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1189  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1190  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1191  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1192  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1193  {NULL},
1194 };
1195 
1196 DEFINE_WRITER_CLASS(compact);
1197 
1199 {
1200  CompactContext *compact = wctx->priv;
1201 
1202  if (strlen(compact->item_sep_str) != 1) {
1203  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1204  compact->item_sep_str);
1205  return AVERROR(EINVAL);
1206  }
1207  compact->item_sep = compact->item_sep_str[0];
1208 
1209  if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
1210  else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
1211  else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
1212  else {
1213  av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
1214  return AVERROR(EINVAL);
1215  }
1216 
1217  return 0;
1218 }
1219 
1221 {
1222  CompactContext *compact = wctx->priv;
1223  const struct section *section = wctx->section[wctx->level];
1224  const struct section *parent_section = wctx->level ?
1225  wctx->section[wctx->level-1] : NULL;
1226  compact->terminate_line[wctx->level] = 1;
1227  compact->has_nested_elems[wctx->level] = 0;
1228 
1229  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1230  if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
1231  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1232  compact->nested_section[wctx->level] = 1;
1233  compact->has_nested_elems[wctx->level-1] = 1;
1234  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1235  wctx->section_pbuf[wctx->level-1].str,
1237  wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
1238  } else {
1239  if (parent_section && compact->has_nested_elems[wctx->level-1] &&
1241  compact->terminate_line[wctx->level-1] = 0;
1242  }
1243  if (parent_section && !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)) &&
1244  wctx->level && wctx->nb_item[wctx->level-1])
1245  writer_w8(wctx, compact->item_sep);
1246  if (compact->print_section &&
1248  writer_printf(wctx, "%s%c", section->name, compact->item_sep);
1249  }
1250 }
1251 
1253 {
1254  CompactContext *compact = wctx->priv;
1255 
1256  if (!compact->nested_section[wctx->level] &&
1257  compact->terminate_line[wctx->level] &&
1259  writer_w8(wctx, '\n');
1260 }
1261 
1262 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
1263 {
1264  CompactContext *compact = wctx->priv;
1265  AVBPrint buf;
1266 
1267  if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
1268  if (!compact->nokey)
1269  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1271  writer_put_str(wctx, compact->escape_str(&buf, value, compact->item_sep, wctx));
1272  av_bprint_finalize(&buf, NULL);
1273 }
1274 
1275 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
1276 {
1277  CompactContext *compact = wctx->priv;
1278 
1279  if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
1280  if (!compact->nokey)
1281  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1282  writer_printf(wctx, "%lld", value);
1283 }
1284 
1285 static const Writer compact_writer = {
1286  .name = "compact",
1287  .priv_size = sizeof(CompactContext),
1288  .init = compact_init,
1291  .print_integer = compact_print_int,
1292  .print_string = compact_print_str,
1294  .priv_class = &compact_class,
1295 };
1296 
1297 /* CSV output */
1298 
1299 #undef OFFSET
1300 #define OFFSET(x) offsetof(CompactContext, x)
1301 
1302 static const AVOption csv_options[] = {
1303  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1304  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1305  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1306  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1307  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1308  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1309  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1310  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1311  {NULL},
1312 };
1313 
1314 DEFINE_WRITER_CLASS(csv);
1315 
1316 static const Writer csv_writer = {
1317  .name = "csv",
1318  .priv_size = sizeof(CompactContext),
1319  .init = compact_init,
1322  .print_integer = compact_print_int,
1323  .print_string = compact_print_str,
1325  .priv_class = &csv_class,
1326 };
1327 
1328 /* Flat output */
1329 
1330 typedef struct FlatContext {
1331  const AVClass *class;
1332  const char *sep_str;
1333  char sep;
1335 } FlatContext;
1336 
1337 #undef OFFSET
1338 #define OFFSET(x) offsetof(FlatContext, x)
1339 
1340 static const AVOption flat_options[]= {
1341  {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1342  {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1343  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1344  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1345  {NULL},
1346 };
1347 
1349 
1351 {
1352  FlatContext *flat = wctx->priv;
1353 
1354  if (strlen(flat->sep_str) != 1) {
1355  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1356  flat->sep_str);
1357  return AVERROR(EINVAL);
1358  }
1359  flat->sep = flat->sep_str[0];
1360 
1361  return 0;
1362 }
1363 
1364 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1365 {
1366  const char *p;
1367 
1368  for (p = src; *p; p++) {
1369  if (!((*p >= '0' && *p <= '9') ||
1370  (*p >= 'a' && *p <= 'z') ||
1371  (*p >= 'A' && *p <= 'Z')))
1372  av_bprint_chars(dst, '_', 1);
1373  else
1374  av_bprint_chars(dst, *p, 1);
1375  }
1376  return dst->str;
1377 }
1378 
1379 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1380 {
1381  const char *p;
1382 
1383  for (p = src; *p; p++) {
1384  switch (*p) {
1385  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1386  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1387  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1388  case '"': av_bprintf(dst, "%s", "\\\""); break;
1389  case '`': av_bprintf(dst, "%s", "\\`"); break;
1390  case '$': av_bprintf(dst, "%s", "\\$"); break;
1391  default: av_bprint_chars(dst, *p, 1); break;
1392  }
1393  }
1394  return dst->str;
1395 }
1396 
1398 {
1399  FlatContext *flat = wctx->priv;
1400  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1401  const struct section *section = wctx->section[wctx->level];
1402  const struct section *parent_section = wctx->level ?
1403  wctx->section[wctx->level-1] : NULL;
1404 
1405  /* build section header */
1406  av_bprint_clear(buf);
1407  if (!parent_section)
1408  return;
1409  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1410 
1411  if (flat->hierarchical ||
1413  av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1414 
1415  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1416  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1417  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1418  av_bprintf(buf, "%d%s", n, flat->sep_str);
1419  }
1420  }
1421 }
1422 
1423 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1424 {
1425  writer_printf(wctx, "%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1426 }
1427 
1428 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1429 {
1430  FlatContext *flat = wctx->priv;
1431  AVBPrint buf;
1432 
1433  writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
1435  writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
1436  av_bprint_clear(&buf);
1437  writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
1438  av_bprint_finalize(&buf, NULL);
1439 }
1440 
1441 static const Writer flat_writer = {
1442  .name = "flat",
1443  .priv_size = sizeof(FlatContext),
1444  .init = flat_init,
1446  .print_integer = flat_print_int,
1447  .print_string = flat_print_str,
1449  .priv_class = &flat_class,
1450 };
1451 
1452 /* INI format output */
1453 
1454 typedef struct INIContext {
1455  const AVClass *class;
1457 } INIContext;
1458 
1459 #undef OFFSET
1460 #define OFFSET(x) offsetof(INIContext, x)
1461 
1462 static const AVOption ini_options[] = {
1463  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1464  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1465  {NULL},
1466 };
1467 
1468 DEFINE_WRITER_CLASS(ini);
1469 
1470 static char *ini_escape_str(AVBPrint *dst, const char *src)
1471 {
1472  int i = 0;
1473  char c = 0;
1474 
1475  while (c = src[i++]) {
1476  switch (c) {
1477  case '\b': av_bprintf(dst, "%s", "\\b"); break;
1478  case '\f': av_bprintf(dst, "%s", "\\f"); break;
1479  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1480  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1481  case '\t': av_bprintf(dst, "%s", "\\t"); break;
1482  case '\\':
1483  case '#' :
1484  case '=' :
1485  case ':' : av_bprint_chars(dst, '\\', 1);
1486  default:
1487  if ((unsigned char)c < 32)
1488  av_bprintf(dst, "\\x00%02x", c & 0xff);
1489  else
1490  av_bprint_chars(dst, c, 1);
1491  break;
1492  }
1493  }
1494  return dst->str;
1495 }
1496 
1498 {
1499  INIContext *ini = wctx->priv;
1500  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1501  const struct section *section = wctx->section[wctx->level];
1502  const struct section *parent_section = wctx->level ?
1503  wctx->section[wctx->level-1] : NULL;
1504 
1505  av_bprint_clear(buf);
1506  if (!parent_section) {
1507  writer_put_str(wctx, "# ffprobe output\n\n");
1508  return;
1509  }
1510 
1511  if (wctx->nb_item[wctx->level-1])
1512  writer_w8(wctx, '\n');
1513 
1514  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1515  if (ini->hierarchical ||
1517  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1518 
1519  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1520  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1521  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1522  av_bprintf(buf, ".%d", n);
1523  }
1524  }
1525 
1527  writer_printf(wctx, "[%s]\n", buf->str);
1528 }
1529 
1530 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1531 {
1532  AVBPrint buf;
1533 
1535  writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
1536  av_bprint_clear(&buf);
1537  writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
1538  av_bprint_finalize(&buf, NULL);
1539 }
1540 
1541 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1542 {
1543  writer_printf(wctx, "%s=%lld\n", key, value);
1544 }
1545 
1546 static const Writer ini_writer = {
1547  .name = "ini",
1548  .priv_size = sizeof(INIContext),
1550  .print_integer = ini_print_int,
1551  .print_string = ini_print_str,
1553  .priv_class = &ini_class,
1554 };
1555 
1556 /* JSON output */
1557 
1558 typedef struct JSONContext {
1559  const AVClass *class;
1561  int compact;
1562  const char *item_sep, *item_start_end;
1563 } JSONContext;
1564 
1565 #undef OFFSET
1566 #define OFFSET(x) offsetof(JSONContext, x)
1567 
1568 static const AVOption json_options[]= {
1569  { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1570  { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1571  { NULL }
1572 };
1573 
1574 DEFINE_WRITER_CLASS(json);
1575 
1577 {
1578  JSONContext *json = wctx->priv;
1579 
1580  json->item_sep = json->compact ? ", " : ",\n";
1581  json->item_start_end = json->compact ? " " : "\n";
1582 
1583  return 0;
1584 }
1585 
1586 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1587 {
1588  static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1589  static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1590  const char *p;
1591 
1592  for (p = src; *p; p++) {
1593  char *s = strchr(json_escape, *p);
1594  if (s) {
1595  av_bprint_chars(dst, '\\', 1);
1596  av_bprint_chars(dst, json_subst[s - json_escape], 1);
1597  } else if ((unsigned char)*p < 32) {
1598  av_bprintf(dst, "\\u00%02x", *p & 0xff);
1599  } else {
1600  av_bprint_chars(dst, *p, 1);
1601  }
1602  }
1603  return dst->str;
1604 }
1605 
1606 #define JSON_INDENT() writer_printf(wctx, "%*c", json->indent_level * 4, ' ')
1607 
1609 {
1610  JSONContext *json = wctx->priv;
1611  AVBPrint buf;
1612  const struct section *section = wctx->section[wctx->level];
1613  const struct section *parent_section = wctx->level ?
1614  wctx->section[wctx->level-1] : NULL;
1615 
1616  if (wctx->level && wctx->nb_item[wctx->level-1])
1617  writer_put_str(wctx, ",\n");
1618 
1620  writer_put_str(wctx, "{\n");
1621  json->indent_level++;
1622  } else {
1624  json_escape_str(&buf, section->name, wctx);
1625  JSON_INDENT();
1626 
1627  json->indent_level++;
1629  writer_printf(wctx, "\"%s\": [\n", buf.str);
1630  } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1631  writer_printf(wctx, "\"%s\": {%s", buf.str, json->item_start_end);
1632  } else {
1633  writer_printf(wctx, "{%s", json->item_start_end);
1634 
1635  /* this is required so the parser can distinguish between packets and frames */
1636  if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1637  if (!json->compact)
1638  JSON_INDENT();
1639  writer_printf(wctx, "\"type\": \"%s\"", section->name);
1640  wctx->nb_item[wctx->level]++;
1641  }
1642  }
1643  av_bprint_finalize(&buf, NULL);
1644  }
1645 }
1646 
1648 {
1649  JSONContext *json = wctx->priv;
1650  const struct section *section = wctx->section[wctx->level];
1651 
1652  if (wctx->level == 0) {
1653  json->indent_level--;
1654  writer_put_str(wctx, "\n}\n");
1655  } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1656  writer_w8(wctx, '\n');
1657  json->indent_level--;
1658  JSON_INDENT();
1659  writer_w8(wctx, ']');
1660  } else {
1661  writer_put_str(wctx, json->item_start_end);
1662  json->indent_level--;
1663  if (!json->compact)
1664  JSON_INDENT();
1665  writer_w8(wctx, '}');
1666  }
1667 }
1668 
1669 static inline void json_print_item_str(WriterContext *wctx,
1670  const char *key, const char *value)
1671 {
1672  AVBPrint buf;
1673 
1675  writer_printf(wctx, "\"%s\":", json_escape_str(&buf, key, wctx));
1676  av_bprint_clear(&buf);
1677  writer_printf(wctx, " \"%s\"", json_escape_str(&buf, value, wctx));
1678  av_bprint_finalize(&buf, NULL);
1679 }
1680 
1681 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1682 {
1683  JSONContext *json = wctx->priv;
1684  const struct section *parent_section = wctx->level ?
1685  wctx->section[wctx->level-1] : NULL;
1686 
1687  if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1688  writer_put_str(wctx, json->item_sep);
1689  if (!json->compact)
1690  JSON_INDENT();
1691  json_print_item_str(wctx, key, value);
1692 }
1693 
1694 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1695 {
1696  JSONContext *json = wctx->priv;
1697  const struct section *parent_section = wctx->level ?
1698  wctx->section[wctx->level-1] : NULL;
1699  AVBPrint buf;
1700 
1701  if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1702  writer_put_str(wctx, json->item_sep);
1703  if (!json->compact)
1704  JSON_INDENT();
1705 
1707  writer_printf(wctx, "\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1708  av_bprint_finalize(&buf, NULL);
1709 }
1710 
1711 static const Writer json_writer = {
1712  .name = "json",
1713  .priv_size = sizeof(JSONContext),
1714  .init = json_init,
1717  .print_integer = json_print_int,
1718  .print_string = json_print_str,
1720  .priv_class = &json_class,
1721 };
1722 
1723 /* XML output */
1724 
1725 typedef struct XMLContext {
1726  const AVClass *class;
1731 } XMLContext;
1732 
1733 #undef OFFSET
1734 #define OFFSET(x) offsetof(XMLContext, x)
1735 
1736 static const AVOption xml_options[] = {
1737  {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1738  {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1739  {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1740  {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1741  {NULL},
1742 };
1743 
1744 DEFINE_WRITER_CLASS(xml);
1745 
1746 static av_cold int xml_init(WriterContext *wctx)
1747 {
1748  XMLContext *xml = wctx->priv;
1749 
1750  if (xml->xsd_strict) {
1751  xml->fully_qualified = 1;
1752 #define CHECK_COMPLIANCE(opt, opt_name) \
1753  if (opt) { \
1754  av_log(wctx, AV_LOG_ERROR, \
1755  "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1756  "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1757  return AVERROR(EINVAL); \
1758  }
1759  CHECK_COMPLIANCE(show_private_data, "private");
1762  }
1763 
1764  return 0;
1765 }
1766 
1767 #define XML_INDENT() writer_printf(wctx, "%*c", xml->indent_level * 4, ' ')
1768 
1770 {
1771  XMLContext *xml = wctx->priv;
1772  const struct section *section = wctx->section[wctx->level];
1773  const struct section *parent_section = wctx->level ?
1774  wctx->section[wctx->level-1] : NULL;
1775 
1776  if (wctx->level == 0) {
1777  const char *qual = " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
1778  "xmlns:ffprobe=\"http://www.ffmpeg.org/schema/ffprobe\" "
1779  "xsi:schemaLocation=\"http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd\"";
1780 
1781  writer_put_str(wctx, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1782  writer_printf(wctx, "<%sffprobe%s>\n",
1783  xml->fully_qualified ? "ffprobe:" : "",
1784  xml->fully_qualified ? qual : "");
1785  return;
1786  }
1787 
1788  if (xml->within_tag) {
1789  xml->within_tag = 0;
1790  writer_put_str(wctx, ">\n");
1791  }
1793  xml->indent_level++;
1794  } else {
1795  if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1796  wctx->level && wctx->nb_item[wctx->level-1])
1797  writer_w8(wctx, '\n');
1798  xml->indent_level++;
1799 
1801  XML_INDENT(); writer_printf(wctx, "<%s>\n", section->name);
1802  } else {
1803  XML_INDENT(); writer_printf(wctx, "<%s ", section->name);
1804  xml->within_tag = 1;
1805  }
1806  }
1807 }
1808 
1810 {
1811  XMLContext *xml = wctx->priv;
1812  const struct section *section = wctx->section[wctx->level];
1813 
1814  if (wctx->level == 0) {
1815  writer_printf(wctx, "</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1816  } else if (xml->within_tag) {
1817  xml->within_tag = 0;
1818  writer_put_str(wctx, "/>\n");
1819  xml->indent_level--;
1821  xml->indent_level--;
1822  } else {
1823  XML_INDENT(); writer_printf(wctx, "</%s>\n", section->name);
1824  xml->indent_level--;
1825  }
1826 }
1827 
1828 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1829 {
1830  AVBPrint buf;
1831  XMLContext *xml = wctx->priv;
1832  const struct section *section = wctx->section[wctx->level];
1833 
1835 
1837  XML_INDENT();
1838  av_bprint_escape(&buf, key, NULL,
1840  writer_printf(wctx, "<%s key=\"%s\"",
1841  section->element_name, buf.str);
1842  av_bprint_clear(&buf);
1843 
1844  av_bprint_escape(&buf, value, NULL,
1846  writer_printf(wctx, " value=\"%s\"/>\n", buf.str);
1847  } else {
1848  if (wctx->nb_item[wctx->level])
1849  writer_w8(wctx, ' ');
1850 
1851  av_bprint_escape(&buf, value, NULL,
1853  writer_printf(wctx, "%s=\"%s\"", key, buf.str);
1854  }
1855 
1856  av_bprint_finalize(&buf, NULL);
1857 }
1858 
1859 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1860 {
1861  if (wctx->nb_item[wctx->level])
1862  writer_w8(wctx, ' ');
1863  writer_printf(wctx, "%s=\"%lld\"", key, value);
1864 }
1865 
1866 static Writer xml_writer = {
1867  .name = "xml",
1868  .priv_size = sizeof(XMLContext),
1869  .init = xml_init,
1872  .print_integer = xml_print_int,
1873  .print_string = xml_print_str,
1875  .priv_class = &xml_class,
1876 };
1877 
1878 static void writer_register_all(void)
1879 {
1880  static int initialized;
1881 
1882  if (initialized)
1883  return;
1884  initialized = 1;
1885 
1893 }
1894 
1895 #define print_fmt(k, f, ...) do { \
1896  av_bprint_clear(&pbuf); \
1897  av_bprintf(&pbuf, f, __VA_ARGS__); \
1898  writer_print_string(w, k, pbuf.str, 0); \
1899 } while (0)
1900 
1901 #define print_list_fmt(k, f, n, m, ...) do { \
1902  av_bprint_clear(&pbuf); \
1903  for (int idx = 0; idx < n; idx++) { \
1904  for (int idx2 = 0; idx2 < m; idx2++) { \
1905  if (idx > 0 || idx2 > 0) \
1906  av_bprint_chars(&pbuf, ' ', 1); \
1907  av_bprintf(&pbuf, f, __VA_ARGS__); \
1908  } \
1909  } \
1910  writer_print_string(w, k, pbuf.str, 0); \
1911 } while (0)
1912 
1913 #define print_int(k, v) writer_print_integer(w, k, v)
1914 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1915 #define print_str(k, v) writer_print_string(w, k, v, 0)
1916 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1917 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1918 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1919 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1920 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1921 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1922 #define print_val(k, v, u) do { \
1923  struct unit_value uv; \
1924  uv.val.i = v; \
1925  uv.unit = u; \
1926  writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1927 } while (0)
1928 
1929 #define print_section_header(s) writer_print_section_header(w, s)
1930 #define print_section_footer(s) writer_print_section_footer(w, s)
1931 
1932 #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
1933 { \
1934  ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
1935  if (ret < 0) \
1936  goto end; \
1937  memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
1938 }
1939 
1940 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1941 {
1942  const AVDictionaryEntry *tag = NULL;
1943  int ret = 0;
1944 
1945  if (!tags)
1946  return 0;
1947  writer_print_section_header(w, section_id);
1948 
1949  while ((tag = av_dict_iterate(tags, tag))) {
1950  if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1951  break;
1952  }
1954 
1955  return ret;
1956 }
1957 
1959 {
1960  if (!dovi)
1961  return;
1962 
1963  {
1964  const AVDOVIRpuDataHeader *hdr = av_dovi_get_header(dovi);
1965  const AVDOVIDataMapping *mapping = av_dovi_get_mapping(dovi);
1967  AVBPrint pbuf;
1968 
1970 
1971  // header
1972  print_int("rpu_type", hdr->rpu_type);
1973  print_int("rpu_format", hdr->rpu_format);
1974  print_int("vdr_rpu_profile", hdr->vdr_rpu_profile);
1975  print_int("vdr_rpu_level", hdr->vdr_rpu_level);
1976  print_int("chroma_resampling_explicit_filter_flag",
1978  print_int("coef_data_type", hdr->coef_data_type);
1979  print_int("coef_log2_denom", hdr->coef_log2_denom);
1980  print_int("vdr_rpu_normalized_idc", hdr->vdr_rpu_normalized_idc);
1981  print_int("bl_video_full_range_flag", hdr->bl_video_full_range_flag);
1982  print_int("bl_bit_depth", hdr->bl_bit_depth);
1983  print_int("el_bit_depth", hdr->el_bit_depth);
1984  print_int("vdr_bit_depth", hdr->vdr_bit_depth);
1985  print_int("spatial_resampling_filter_flag",
1987  print_int("el_spatial_resampling_filter_flag",
1989  print_int("disable_residual_flag", hdr->disable_residual_flag);
1990 
1991  // data mapping values
1992  print_int("vdr_rpu_id", mapping->vdr_rpu_id);
1993  print_int("mapping_color_space", mapping->mapping_color_space);
1994  print_int("mapping_chroma_format_idc",
1995  mapping->mapping_chroma_format_idc);
1996 
1997  print_int("nlq_method_idc", mapping->nlq_method_idc);
1998  switch (mapping->nlq_method_idc) {
1999  case AV_DOVI_NLQ_NONE:
2000  print_str("nlq_method_idc_name", "none");
2001  break;
2002  case AV_DOVI_NLQ_LINEAR_DZ:
2003  print_str("nlq_method_idc_name", "linear_dz");
2004  break;
2005  default:
2006  print_str("nlq_method_idc_name", "unknown");
2007  break;
2008  }
2009 
2010  print_int("num_x_partitions", mapping->num_x_partitions);
2011  print_int("num_y_partitions", mapping->num_y_partitions);
2012 
2014 
2015  for (int c = 0; c < 3; c++) {
2016  const AVDOVIReshapingCurve *curve = &mapping->curves[c];
2018 
2019  print_list_fmt("pivots", "%"PRIu16, curve->num_pivots, 1, curve->pivots[idx]);
2020 
2022  for (int i = 0; i < curve->num_pivots - 1; i++) {
2023 
2025  print_int("mapping_idc", curve->mapping_idc[i]);
2026  switch (curve->mapping_idc[i]) {
2028  print_str("mapping_idc_name", "polynomial");
2029  print_int("poly_order", curve->poly_order[i]);
2030  print_list_fmt("poly_coef", "%"PRIi64,
2031  curve->poly_order[i] + 1, 1,
2032  curve->poly_coef[i][idx]);
2033  break;
2034  case AV_DOVI_MAPPING_MMR:
2035  print_str("mapping_idc_name", "mmr");
2036  print_int("mmr_order", curve->mmr_order[i]);
2037  print_int("mmr_constant", curve->mmr_constant[i]);
2038  print_list_fmt("mmr_coef", "%"PRIi64,
2039  curve->mmr_order[i], 7,
2040  curve->mmr_coef[i][idx][idx2]);
2041  break;
2042  default:
2043  print_str("mapping_idc_name", "unknown");
2044  break;
2045  }
2046 
2047  // SECTION_ID_FRAME_SIDE_DATA_PIECE
2049  }
2050 
2051  // SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
2053 
2054  if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
2055  const AVDOVINLQParams *nlq = &mapping->nlq[c];
2056  print_int("nlq_offset", nlq->nlq_offset);
2057  print_int("vdr_in_max", nlq->vdr_in_max);
2058 
2059  switch (mapping->nlq_method_idc) {
2060  case AV_DOVI_NLQ_LINEAR_DZ:
2061  print_int("linear_deadzone_slope", nlq->linear_deadzone_slope);
2062  print_int("linear_deadzone_threshold", nlq->linear_deadzone_threshold);
2063  break;
2064  }
2065  }
2066 
2067  // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
2069  }
2070 
2071  // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
2073 
2074  // color metadata
2075  print_int("dm_metadata_id", color->dm_metadata_id);
2076  print_int("scene_refresh_flag", color->scene_refresh_flag);
2077  print_list_fmt("ycc_to_rgb_matrix", "%d/%d",
2078  FF_ARRAY_ELEMS(color->ycc_to_rgb_matrix), 1,
2079  color->ycc_to_rgb_matrix[idx].num,
2080  color->ycc_to_rgb_matrix[idx].den);
2081  print_list_fmt("ycc_to_rgb_offset", "%d/%d",
2082  FF_ARRAY_ELEMS(color->ycc_to_rgb_offset), 1,
2083  color->ycc_to_rgb_offset[idx].num,
2084  color->ycc_to_rgb_offset[idx].den);
2085  print_list_fmt("rgb_to_lms_matrix", "%d/%d",
2086  FF_ARRAY_ELEMS(color->rgb_to_lms_matrix), 1,
2087  color->rgb_to_lms_matrix[idx].num,
2088  color->rgb_to_lms_matrix[idx].den);
2089  print_int("signal_eotf", color->signal_eotf);
2090  print_int("signal_eotf_param0", color->signal_eotf_param0);
2091  print_int("signal_eotf_param1", color->signal_eotf_param1);
2092  print_int("signal_eotf_param2", color->signal_eotf_param2);
2093  print_int("signal_bit_depth", color->signal_bit_depth);
2094  print_int("signal_color_space", color->signal_color_space);
2095  print_int("signal_chroma_format", color->signal_chroma_format);
2096  print_int("signal_full_range_flag", color->signal_full_range_flag);
2097  print_int("source_min_pq", color->source_min_pq);
2098  print_int("source_max_pq", color->source_max_pq);
2099  print_int("source_diagonal", color->source_diagonal);
2100 
2101  av_bprint_finalize(&pbuf, NULL);
2102  }
2103 }
2104 
2106 {
2107  if (!metadata)
2108  return;
2109  print_int("application version", metadata->application_version);
2110  print_int("num_windows", metadata->num_windows);
2111  for (int n = 1; n < metadata->num_windows; n++) {
2112  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2113  print_q("window_upper_left_corner_x",
2114  params->window_upper_left_corner_x,'/');
2115  print_q("window_upper_left_corner_y",
2116  params->window_upper_left_corner_y,'/');
2117  print_q("window_lower_right_corner_x",
2118  params->window_lower_right_corner_x,'/');
2119  print_q("window_lower_right_corner_y",
2120  params->window_lower_right_corner_y,'/');
2121  print_q("window_upper_left_corner_x",
2122  params->window_upper_left_corner_x,'/');
2123  print_q("window_upper_left_corner_y",
2124  params->window_upper_left_corner_y,'/');
2125  print_int("center_of_ellipse_x",
2126  params->center_of_ellipse_x ) ;
2127  print_int("center_of_ellipse_y",
2128  params->center_of_ellipse_y );
2129  print_int("rotation_angle",
2130  params->rotation_angle);
2131  print_int("semimajor_axis_internal_ellipse",
2133  print_int("semimajor_axis_external_ellipse",
2135  print_int("semiminor_axis_external_ellipse",
2137  print_int("overlap_process_option",
2138  params->overlap_process_option);
2139  }
2140  print_q("targeted_system_display_maximum_luminance",
2143  print_int("num_rows_targeted_system_display_actual_peak_luminance",
2145  print_int("num_cols_targeted_system_display_actual_peak_luminance",
2147  for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
2148  for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
2149  print_q("targeted_system_display_actual_peak_luminance",
2151  }
2152  }
2153  }
2154  for (int n = 0; n < metadata->num_windows; n++) {
2155  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2156  for (int i = 0; i < 3; i++) {
2157  print_q("maxscl",params->maxscl[i],'/');
2158  }
2159  print_q("average_maxrgb",
2160  params->average_maxrgb,'/');
2161  print_int("num_distribution_maxrgb_percentiles",
2163  for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
2164  print_int("distribution_maxrgb_percentage",
2165  params->distribution_maxrgb[i].percentage);
2166  print_q("distribution_maxrgb_percentile",
2167  params->distribution_maxrgb[i].percentile,'/');
2168  }
2169  print_q("fraction_bright_pixels",
2170  params->fraction_bright_pixels,'/');
2171  }
2173  print_int("num_rows_mastering_display_actual_peak_luminance",
2175  print_int("num_cols_mastering_display_actual_peak_luminance",
2177  for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
2178  for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
2179  print_q("mastering_display_actual_peak_luminance",
2180  metadata->mastering_display_actual_peak_luminance[i][j],'/');
2181  }
2182  }
2183  }
2184 
2185  for (int n = 0; n < metadata->num_windows; n++) {
2186  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2187  if (params->tone_mapping_flag) {
2188  print_q("knee_point_x", params->knee_point_x,'/');
2189  print_q("knee_point_y", params->knee_point_y,'/');
2190  print_int("num_bezier_curve_anchors",
2191  params->num_bezier_curve_anchors );
2192  for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
2193  print_q("bezier_curve_anchors",
2194  params->bezier_curve_anchors[i],'/');
2195  }
2196  }
2197  if (params->color_saturation_mapping_flag) {
2198  print_q("color_saturation_weight",
2199  params->color_saturation_weight,'/');
2200  }
2201  }
2202 }
2203 
2205 {
2206  if (!metadata)
2207  return;
2208  print_int("system_start_code", metadata->system_start_code);
2209  print_int("num_windows", metadata->num_windows);
2210 
2211  for (int n = 0; n < metadata->num_windows; n++) {
2212  const AVHDRVividColorTransformParams *params = &metadata->params[n];
2213 
2214  print_q("minimum_maxrgb", params->minimum_maxrgb, '/');
2215  print_q("average_maxrgb", params->average_maxrgb, '/');
2216  print_q("variance_maxrgb", params->variance_maxrgb, '/');
2217  print_q("maximum_maxrgb", params->maximum_maxrgb, '/');
2218  }
2219 
2220  for (int n = 0; n < metadata->num_windows; n++) {
2221  const AVHDRVividColorTransformParams *params = &metadata->params[n];
2222 
2223  print_int("tone_mapping_mode_flag", params->tone_mapping_mode_flag);
2224  print_int("tone_mapping_param_num", params->tone_mapping_param_num);
2225  if (params->tone_mapping_mode_flag) {
2226  for (int i = 0; i < params->tone_mapping_param_num; i++) {
2227  const AVHDRVividColorToneMappingParams *tm_params = &params->tm_params[i];
2228 
2229  print_q("targeted_system_display_maximum_luminance",
2231  print_int("base_enable_flag", tm_params->base_enable_flag);
2232  if (tm_params->base_enable_flag) {
2233  print_q("base_param_m_p", tm_params->base_param_m_p, '/');
2234  print_q("base_param_m_m", tm_params->base_param_m_m, '/');
2235  print_q("base_param_m_a", tm_params->base_param_m_a, '/');
2236  print_q("base_param_m_b", tm_params->base_param_m_b, '/');
2237  print_q("base_param_m_n", tm_params->base_param_m_n, '/');
2238 
2239  print_int("base_param_k1", tm_params->base_param_k1);
2240  print_int("base_param_k2", tm_params->base_param_k2);
2241  print_int("base_param_k3", tm_params->base_param_k3);
2242  print_int("base_param_Delta_enable_mode",
2243  tm_params->base_param_Delta_enable_mode);
2244  print_q("base_param_Delta", tm_params->base_param_Delta, '/');
2245  }
2246  print_int("3Spline_enable_flag", tm_params->three_Spline_enable_flag);
2247  if (tm_params->three_Spline_enable_flag) {
2248  print_int("3Spline_num", tm_params->three_Spline_num);
2249  print_int("3Spline_TH_mode", tm_params->three_Spline_TH_mode);
2250 
2251  for (int j = 0; j < tm_params->three_Spline_num; j++) {
2252  print_q("3Spline_TH_enable_MB", tm_params->three_Spline_TH_enable_MB, '/');
2253  print_q("3Spline_TH_enable", tm_params->three_Spline_TH_enable, '/');
2254  print_q("3Spline_TH_Delta1", tm_params->three_Spline_TH_Delta1, '/');
2255  print_q("3Spline_TH_Delta2", tm_params->three_Spline_TH_Delta2, '/');
2256  print_q("3Spline_enable_Strength", tm_params->three_Spline_enable_Strength, '/');
2257  }
2258  }
2259  }
2260  }
2261 
2262  print_int("color_saturation_mapping_flag", params->color_saturation_mapping_flag);
2263  if (params->color_saturation_mapping_flag) {
2264  print_int("color_saturation_num", params->color_saturation_num);
2265  for (int i = 0; i < params->color_saturation_num; i++) {
2266  print_q("color_saturation_gain", params->color_saturation_gain[i], '/');
2267  }
2268  }
2269  }
2270 }
2271 
2273  const AVAmbientViewingEnvironment *env)
2274 {
2275  if (!env)
2276  return;
2277 
2278  print_q("ambient_illuminance", env->ambient_illuminance, '/');
2279  print_q("ambient_light_x", env->ambient_light_x, '/');
2280  print_q("ambient_light_y", env->ambient_light_y, '/');
2281 }
2282 
2284  AVCodecParameters *par,
2285  const AVPacketSideData *side_data,
2286  int nb_side_data,
2287  SectionID id_data_list,
2288  SectionID id_data)
2289 {
2290  int i;
2291 
2292  writer_print_section_header(w, id_data_list);
2293  for (i = 0; i < nb_side_data; i++) {
2294  const AVPacketSideData *sd = &side_data[i];
2295  const char *name = av_packet_side_data_name(sd->type);
2296 
2297  writer_print_section_header(w, id_data);
2298  print_str("side_data_type", name ? name : "unknown");
2299  if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2300  double rotation = av_display_rotation_get((int32_t *)sd->data);
2301  if (isnan(rotation))
2302  rotation = 0;
2303  writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2304  print_int("rotation", rotation);
2305  } else if (sd->type == AV_PKT_DATA_STEREO3D) {
2306  const AVStereo3D *stereo = (AVStereo3D *)sd->data;
2307  print_str("type", av_stereo3d_type_name(stereo->type));
2308  print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
2309  } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
2310  const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
2311  print_str("projection", av_spherical_projection_name(spherical->projection));
2312  if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
2313  print_int("padding", spherical->padding);
2314  } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
2315  size_t l, t, r, b;
2316  av_spherical_tile_bounds(spherical, par->width, par->height,
2317  &l, &t, &r, &b);
2318  print_int("bound_left", l);
2319  print_int("bound_top", t);
2320  print_int("bound_right", r);
2321  print_int("bound_bottom", b);
2322  }
2323 
2324  print_int("yaw", (double) spherical->yaw / (1 << 16));
2325  print_int("pitch", (double) spherical->pitch / (1 << 16));
2326  print_int("roll", (double) spherical->roll / (1 << 16));
2327  } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
2328  print_int("skip_samples", AV_RL32(sd->data));
2329  print_int("discard_padding", AV_RL32(sd->data + 4));
2330  print_int("skip_reason", AV_RL8(sd->data + 8));
2331  print_int("discard_reason", AV_RL8(sd->data + 9));
2332  } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
2334 
2335  if (metadata->has_primaries) {
2336  print_q("red_x", metadata->display_primaries[0][0], '/');
2337  print_q("red_y", metadata->display_primaries[0][1], '/');
2338  print_q("green_x", metadata->display_primaries[1][0], '/');
2339  print_q("green_y", metadata->display_primaries[1][1], '/');
2340  print_q("blue_x", metadata->display_primaries[2][0], '/');
2341  print_q("blue_y", metadata->display_primaries[2][1], '/');
2342 
2343  print_q("white_point_x", metadata->white_point[0], '/');
2344  print_q("white_point_y", metadata->white_point[1], '/');
2345  }
2346 
2347  if (metadata->has_luminance) {
2348  print_q("min_luminance", metadata->min_luminance, '/');
2349  print_q("max_luminance", metadata->max_luminance, '/');
2350  }
2351  } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
2353  print_int("max_content", metadata->MaxCLL);
2354  print_int("max_average", metadata->MaxFALL);
2355  } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
2357  print_int("dv_version_major", dovi->dv_version_major);
2358  print_int("dv_version_minor", dovi->dv_version_minor);
2359  print_int("dv_profile", dovi->dv_profile);
2360  print_int("dv_level", dovi->dv_level);
2361  print_int("rpu_present_flag", dovi->rpu_present_flag);
2362  print_int("el_present_flag", dovi->el_present_flag);
2363  print_int("bl_present_flag", dovi->bl_present_flag);
2364  print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
2365  } else if (sd->type == AV_PKT_DATA_AUDIO_SERVICE_TYPE) {
2366  enum AVAudioServiceType *t = (enum AVAudioServiceType *)sd->data;
2367  print_int("service_type", *t);
2368  } else if (sd->type == AV_PKT_DATA_MPEGTS_STREAM_ID) {
2369  print_int("id", *sd->data);
2370  } else if (sd->type == AV_PKT_DATA_CPB_PROPERTIES) {
2371  const AVCPBProperties *prop = (AVCPBProperties *)sd->data;
2372  print_int("max_bitrate", prop->max_bitrate);
2373  print_int("min_bitrate", prop->min_bitrate);
2374  print_int("avg_bitrate", prop->avg_bitrate);
2375  print_int("buffer_size", prop->buffer_size);
2376  print_int("vbv_delay", prop->vbv_delay);
2377  } else if (sd->type == AV_PKT_DATA_WEBVTT_IDENTIFIER ||
2379  if (do_show_data)
2380  writer_print_data(w, "data", sd->data, sd->size);
2381  writer_print_data_hash(w, "data_hash", sd->data, sd->size);
2382  } else if (sd->type == AV_PKT_DATA_AFD && sd->size > 0) {
2383  print_int("active_format", *sd->data);
2384  }
2386  }
2388 }
2389 
2391 {
2392  const char *val = av_color_range_name(color_range);
2394  print_str_opt("color_range", "unknown");
2395  } else {
2396  print_str("color_range", val);
2397  }
2398 }
2399 
2400 static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
2401 {
2402  const char *val = av_color_space_name(color_space);
2403  if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
2404  print_str_opt("color_space", "unknown");
2405  } else {
2406  print_str("color_space", val);
2407  }
2408 }
2409 
2411 {
2414  print_str_opt("color_primaries", "unknown");
2415  } else {
2416  print_str("color_primaries", val);
2417  }
2418 }
2419 
2421 {
2422  const char *val = av_color_transfer_name(color_trc);
2423  if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
2424  print_str_opt("color_transfer", "unknown");
2425  } else {
2426  print_str("color_transfer", val);
2427  }
2428 }
2429 
2430 static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
2431 {
2432  const char *val = av_chroma_location_name(chroma_location);
2433  if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
2434  print_str_opt("chroma_location", "unspecified");
2435  } else {
2436  print_str("chroma_location", val);
2437  }
2438 }
2439 
2440 
2441 static void clear_log(int need_lock)
2442 {
2443  int i;
2444 
2445  if (need_lock)
2446  pthread_mutex_lock(&log_mutex);
2447  for (i=0; i<log_buffer_size; i++) {
2448  av_freep(&log_buffer[i].context_name);
2449  av_freep(&log_buffer[i].parent_name);
2450  av_freep(&log_buffer[i].log_message);
2451  }
2452  log_buffer_size = 0;
2453  if(need_lock)
2454  pthread_mutex_unlock(&log_mutex);
2455 }
2456 
2457 static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
2458 {
2459  int i;
2460  pthread_mutex_lock(&log_mutex);
2461  if (!log_buffer_size) {
2462  pthread_mutex_unlock(&log_mutex);
2463  return 0;
2464  }
2465  writer_print_section_header(w, section_ids);
2466 
2467  for (i=0; i<log_buffer_size; i++) {
2468  if (log_buffer[i].log_level <= log_level) {
2469  writer_print_section_header(w, section_id);
2470  print_str("context", log_buffer[i].context_name);
2471  print_int("level", log_buffer[i].log_level);
2472  print_int("category", log_buffer[i].category);
2473  if (log_buffer[i].parent_name) {
2474  print_str("parent_context", log_buffer[i].parent_name);
2475  print_int("parent_category", log_buffer[i].parent_category);
2476  } else {
2477  print_str_opt("parent_context", "N/A");
2478  print_str_opt("parent_category", "N/A");
2479  }
2480  print_str("message", log_buffer[i].log_message);
2482  }
2483  }
2484  clear_log(0);
2485  pthread_mutex_unlock(&log_mutex);
2486 
2488 
2489  return 0;
2490 }
2491 
2492 static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
2493 {
2494  char val_str[128];
2495  AVStream *st = ifile->streams[pkt->stream_index].st;
2496  AVBPrint pbuf;
2497  const char *s;
2498 
2500 
2502 
2504  if (s) print_str ("codec_type", s);
2505  else print_str_opt("codec_type", "unknown");
2506  print_int("stream_index", pkt->stream_index);
2507  print_ts ("pts", pkt->pts);
2508  print_time("pts_time", pkt->pts, &st->time_base);
2509  print_ts ("dts", pkt->dts);
2510  print_time("dts_time", pkt->dts, &st->time_base);
2511  print_duration_ts("duration", pkt->duration);
2512  print_duration_time("duration_time", pkt->duration, &st->time_base);
2513  print_val("size", pkt->size, unit_byte_str);
2514  if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
2515  else print_str_opt("pos", "N/A");
2516  print_fmt("flags", "%c%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
2517  pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_',
2518  pkt->flags & AV_PKT_FLAG_CORRUPT ? 'C' : '_');
2519  if (do_show_data)
2520  writer_print_data(w, "data", pkt->data, pkt->size);
2521  writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
2522 
2523  if (pkt->side_data_elems) {
2524  size_t size;
2525  const uint8_t *side_metadata;
2526 
2528  if (side_metadata && size && do_show_packet_tags) {
2529  AVDictionary *dict = NULL;
2530  if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
2532  av_dict_free(&dict);
2533  }
2534 
2538  }
2539 
2541 
2542  av_bprint_finalize(&pbuf, NULL);
2543  fflush(stdout);
2544 }
2545 
2548 {
2549  AVBPrint pbuf;
2550 
2552 
2554 
2555  print_str ("media_type", "subtitle");
2556  print_ts ("pts", sub->pts);
2557  print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
2558  print_int ("format", sub->format);
2559  print_int ("start_display_time", sub->start_display_time);
2560  print_int ("end_display_time", sub->end_display_time);
2561  print_int ("num_rects", sub->num_rects);
2562 
2564 
2565  av_bprint_finalize(&pbuf, NULL);
2566  fflush(stdout);
2567 }
2568 
2571 {
2572  AVBPrint pbuf;
2573  char val_str[128];
2574  const char *s;
2575  int i;
2576 
2578 
2580 
2582  if (s) print_str ("media_type", s);
2583  else print_str_opt("media_type", "unknown");
2584  print_int("stream_index", stream->index);
2585  print_int("key_frame", frame->key_frame);
2586  print_ts ("pts", frame->pts);
2587  print_time("pts_time", frame->pts, &stream->time_base);
2588  print_ts ("pkt_dts", frame->pkt_dts);
2589  print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
2590  print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
2591  print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
2592 #if LIBAVUTIL_VERSION_MAJOR < 59
2594  print_duration_ts ("pkt_duration", frame->pkt_duration);
2595  print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
2596  )
2597 #endif
2598  print_duration_ts ("duration", frame->duration);
2599  print_duration_time("duration_time", frame->duration, &stream->time_base);
2600  if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
2601  else print_str_opt("pkt_pos", "N/A");
2602  if (frame->pkt_size != -1) print_val ("pkt_size", frame->pkt_size, unit_byte_str);
2603  else print_str_opt("pkt_size", "N/A");
2604 
2605  switch (stream->codecpar->codec_type) {
2606  AVRational sar;
2607 
2608  case AVMEDIA_TYPE_VIDEO:
2609  print_int("width", frame->width);
2610  print_int("height", frame->height);
2611  s = av_get_pix_fmt_name(frame->format);
2612  if (s) print_str ("pix_fmt", s);
2613  else print_str_opt("pix_fmt", "unknown");
2614  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
2615  if (sar.num) {
2616  print_q("sample_aspect_ratio", sar, ':');
2617  } else {
2618  print_str_opt("sample_aspect_ratio", "N/A");
2619  }
2620  print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
2621 #if LIBAVUTIL_VERSION_MAJOR < 59
2623  print_int("coded_picture_number", frame->coded_picture_number);
2624  print_int("display_picture_number", frame->display_picture_number);
2625  )
2626 #endif
2627  print_int("interlaced_frame", frame->interlaced_frame);
2628  print_int("top_field_first", frame->top_field_first);
2629  print_int("repeat_pict", frame->repeat_pict);
2630 
2631  print_color_range(w, frame->color_range);
2632  print_color_space(w, frame->colorspace);
2633  print_primaries(w, frame->color_primaries);
2634  print_color_trc(w, frame->color_trc);
2635  print_chroma_location(w, frame->chroma_location);
2636  break;
2637 
2638  case AVMEDIA_TYPE_AUDIO:
2639  s = av_get_sample_fmt_name(frame->format);
2640  if (s) print_str ("sample_fmt", s);
2641  else print_str_opt("sample_fmt", "unknown");
2642  print_int("nb_samples", frame->nb_samples);
2643  print_int("channels", frame->ch_layout.nb_channels);
2644  if (frame->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
2645  av_channel_layout_describe(&frame->ch_layout, val_str, sizeof(val_str));
2646  print_str ("channel_layout", val_str);
2647  } else
2648  print_str_opt("channel_layout", "unknown");
2649  break;
2650  }
2651  if (do_show_frame_tags)
2652  show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
2653  if (do_show_log)
2655  if (frame->nb_side_data) {
2657  for (i = 0; i < frame->nb_side_data; i++) {
2658  AVFrameSideData *sd = frame->side_data[i];
2659  const char *name;
2660 
2663  print_str("side_data_type", name ? name : "unknown");
2664  if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2665  double rotation = av_display_rotation_get((int32_t *)sd->data);
2666  if (isnan(rotation))
2667  rotation = 0;
2668  writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2669  print_int("rotation", rotation);
2670  } else if (sd->type == AV_FRAME_DATA_AFD && sd->size > 0) {
2671  print_int("active_format", *sd->data);
2672  } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
2673  char tcbuf[AV_TIMECODE_STR_SIZE];
2674  av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
2675  print_str("timecode", tcbuf);
2676  } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
2677  uint32_t *tc = (uint32_t*)sd->data;
2678  int m = FFMIN(tc[0],3);
2680  for (int j = 1; j <= m ; j++) {
2681  char tcbuf[AV_TIMECODE_STR_SIZE];
2682  av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
2684  print_str("value", tcbuf);
2686  }
2688  } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
2690 
2691  if (metadata->has_primaries) {
2692  print_q("red_x", metadata->display_primaries[0][0], '/');
2693  print_q("red_y", metadata->display_primaries[0][1], '/');
2694  print_q("green_x", metadata->display_primaries[1][0], '/');
2695  print_q("green_y", metadata->display_primaries[1][1], '/');
2696  print_q("blue_x", metadata->display_primaries[2][0], '/');
2697  print_q("blue_y", metadata->display_primaries[2][1], '/');
2698 
2699  print_q("white_point_x", metadata->white_point[0], '/');
2700  print_q("white_point_y", metadata->white_point[1], '/');
2701  }
2702 
2703  if (metadata->has_luminance) {
2704  print_q("min_luminance", metadata->min_luminance, '/');
2705  print_q("max_luminance", metadata->max_luminance, '/');
2706  }
2707  } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
2708  AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
2709  print_dynamic_hdr10_plus(w, metadata);
2710  } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
2712  print_int("max_content", metadata->MaxCLL);
2713  print_int("max_average", metadata->MaxFALL);
2714  } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
2716  if (tag)
2717  print_str(tag->key, tag->value);
2718  print_int("size", sd->size);
2719  } else if (sd->type == AV_FRAME_DATA_DOVI_METADATA) {
2720  print_dovi_metadata(w, (const AVDOVIMetadata *)sd->data);
2721  } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_VIVID) {
2722  AVDynamicHDRVivid *metadata = (AVDynamicHDRVivid *)sd->data;
2723  print_dynamic_hdr_vivid(w, metadata);
2724  } else if (sd->type == AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT) {
2726  w, (const AVAmbientViewingEnvironment *)sd->data);
2727  }
2729  }
2731  }
2732 
2734 
2735  av_bprint_finalize(&pbuf, NULL);
2736  fflush(stdout);
2737 }
2738 
2740  InputFile *ifile,
2741  AVFrame *frame, const AVPacket *pkt,
2742  int *packet_new)
2743 {
2744  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2745  AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
2746  AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
2747  AVSubtitle sub;
2748  int ret = 0, got_frame = 0;
2749 
2750  clear_log(1);
2751  if (dec_ctx) {
2752  switch (par->codec_type) {
2753  case AVMEDIA_TYPE_VIDEO:
2754  case AVMEDIA_TYPE_AUDIO:
2755  if (*packet_new) {
2757  if (ret == AVERROR(EAGAIN)) {
2758  ret = 0;
2759  } else if (ret >= 0 || ret == AVERROR_EOF) {
2760  ret = 0;
2761  *packet_new = 0;
2762  }
2763  }
2764  if (ret >= 0) {
2766  if (ret >= 0) {
2767  got_frame = 1;
2768  } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2769  ret = 0;
2770  }
2771  }
2772  break;
2773 
2774  case AVMEDIA_TYPE_SUBTITLE:
2775  if (*packet_new)
2776  ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
2777  *packet_new = 0;
2778  break;
2779  default:
2780  *packet_new = 0;
2781  }
2782  } else {
2783  *packet_new = 0;
2784  }
2785 
2786  if (ret < 0)
2787  return ret;
2788  if (got_frame) {
2789  int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
2791  if (do_show_frames)
2792  if (is_sub)
2793  show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
2794  else
2795  show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
2796  if (is_sub)
2797  avsubtitle_free(&sub);
2798  }
2799  return got_frame || *packet_new;
2800 }
2801 
2802 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
2803 {
2804  av_log(log_ctx, log_level, "id:%d", interval->id);
2805 
2806  if (interval->has_start) {
2807  av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
2808  av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
2809  } else {
2810  av_log(log_ctx, log_level, " start:N/A");
2811  }
2812 
2813  if (interval->has_end) {
2814  av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
2815  if (interval->duration_frames)
2816  av_log(log_ctx, log_level, "#%"PRId64, interval->end);
2817  else
2818  av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
2819  } else {
2820  av_log(log_ctx, log_level, " end:N/A");
2821  }
2822 
2823  av_log(log_ctx, log_level, "\n");
2824 }
2825 
2827  const ReadInterval *interval, int64_t *cur_ts)
2828 {
2829  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2830  AVPacket *pkt = NULL;
2831  AVFrame *frame = NULL;
2832  int ret = 0, i = 0, frame_count = 0;
2833  int64_t start = -INT64_MAX, end = interval->end;
2834  int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
2835 
2836  av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
2838 
2839  if (interval->has_start) {
2840  int64_t target;
2841  if (interval->start_is_offset) {
2842  if (*cur_ts == AV_NOPTS_VALUE) {
2844  "Could not seek to relative position since current "
2845  "timestamp is not defined\n");
2846  ret = AVERROR(EINVAL);
2847  goto end;
2848  }
2849  target = *cur_ts + interval->start;
2850  } else {
2851  target = interval->start;
2852  }
2853 
2854  av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
2855  av_ts2timestr(target, &AV_TIME_BASE_Q));
2856  if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
2857  av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
2858  interval->start, av_err2str(ret));
2859  goto end;
2860  }
2861  }
2862 
2863  frame = av_frame_alloc();
2864  if (!frame) {
2865  ret = AVERROR(ENOMEM);
2866  goto end;
2867  }
2868  pkt = av_packet_alloc();
2869  if (!pkt) {
2870  ret = AVERROR(ENOMEM);
2871  goto end;
2872  }
2873  while (!av_read_frame(fmt_ctx, pkt)) {
2874  if (fmt_ctx->nb_streams > nb_streams) {
2879  }
2881  AVRational tb = ifile->streams[pkt->stream_index].st->time_base;
2882  int64_t pts = pkt->pts != AV_NOPTS_VALUE ? pkt->pts : pkt->dts;
2883 
2884  if (pts != AV_NOPTS_VALUE)
2885  *cur_ts = av_rescale_q(pts, tb, AV_TIME_BASE_Q);
2886 
2887  if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
2888  start = *cur_ts;
2889  has_start = 1;
2890  }
2891 
2892  if (has_start && !has_end && interval->end_is_offset) {
2893  end = start + interval->end;
2894  has_end = 1;
2895  }
2896 
2897  if (interval->end_is_offset && interval->duration_frames) {
2898  if (frame_count >= interval->end)
2899  break;
2900  } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
2901  break;
2902  }
2903 
2904  frame_count++;
2905  if (do_read_packets) {
2906  if (do_show_packets)
2907  show_packet(w, ifile, pkt, i++);
2909  }
2910  if (do_read_frames) {
2911  int packet_new = 1;
2912  while (process_frame(w, ifile, frame, pkt, &packet_new) > 0);
2913  }
2914  }
2916  }
2918  //Flush remaining frames that are cached in the decoder
2919  for (i = 0; i < ifile->nb_streams; i++) {
2920  pkt->stream_index = i;
2921  if (do_read_frames) {
2922  while (process_frame(w, ifile, frame, pkt, &(int){1}) > 0);
2923  if (ifile->streams[i].dec_ctx)
2924  avcodec_flush_buffers(ifile->streams[i].dec_ctx);
2925  }
2926  }
2927 
2928 end:
2929  av_frame_free(&frame);
2930  av_packet_free(&pkt);
2931  if (ret < 0) {
2932  av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
2933  log_read_interval(interval, NULL, AV_LOG_ERROR);
2934  }
2935  return ret;
2936 }
2937 
2939 {
2940  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2941  int i, ret = 0;
2942  int64_t cur_ts = fmt_ctx->start_time;
2943 
2944  if (read_intervals_nb == 0) {
2945  ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
2946  ret = read_interval_packets(w, ifile, &interval, &cur_ts);
2947  } else {
2948  for (i = 0; i < read_intervals_nb; i++) {
2949  ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
2950  if (ret < 0)
2951  break;
2952  }
2953  }
2954 
2955  return ret;
2956 }
2957 
2958 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
2959 {
2960  AVStream *stream = ist->st;
2961  AVCodecParameters *par;
2963  char val_str[128];
2964  const char *s;
2965  AVRational sar, dar;
2966  AVBPrint pbuf;
2967  const AVCodecDescriptor *cd;
2968  int ret = 0;
2969  const char *profile = NULL;
2970 
2972 
2974 
2975  print_int("index", stream->index);
2976 
2977  par = stream->codecpar;
2978  dec_ctx = ist->dec_ctx;
2979  if (cd = avcodec_descriptor_get(par->codec_id)) {
2980  print_str("codec_name", cd->name);
2981  if (!do_bitexact) {
2982  print_str("codec_long_name",
2983  cd->long_name ? cd->long_name : "unknown");
2984  }
2985  } else {
2986  print_str_opt("codec_name", "unknown");
2987  if (!do_bitexact) {
2988  print_str_opt("codec_long_name", "unknown");
2989  }
2990  }
2991 
2992  if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
2993  print_str("profile", profile);
2994  else {
2995  if (par->profile != FF_PROFILE_UNKNOWN) {
2996  char profile_num[12];
2997  snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
2998  print_str("profile", profile_num);
2999  } else
3000  print_str_opt("profile", "unknown");
3001  }
3002 
3004  if (s) print_str ("codec_type", s);
3005  else print_str_opt("codec_type", "unknown");
3006 
3007  /* print AVI/FourCC tag */
3008  print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
3009  print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
3010 
3011  switch (par->codec_type) {
3012  case AVMEDIA_TYPE_VIDEO:
3013  print_int("width", par->width);
3014  print_int("height", par->height);
3015  if (dec_ctx) {
3016  print_int("coded_width", dec_ctx->coded_width);
3017  print_int("coded_height", dec_ctx->coded_height);
3020  }
3021  print_int("has_b_frames", par->video_delay);
3022  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
3023  if (sar.num) {
3024  print_q("sample_aspect_ratio", sar, ':');
3025  av_reduce(&dar.num, &dar.den,
3026  par->width * sar.num,
3027  par->height * sar.den,
3028  1024*1024);
3029  print_q("display_aspect_ratio", dar, ':');
3030  } else {
3031  print_str_opt("sample_aspect_ratio", "N/A");
3032  print_str_opt("display_aspect_ratio", "N/A");
3033  }
3034  s = av_get_pix_fmt_name(par->format);
3035  if (s) print_str ("pix_fmt", s);
3036  else print_str_opt("pix_fmt", "unknown");
3037  print_int("level", par->level);
3038 
3041  print_color_trc(w, par->color_trc);
3044 
3045  if (par->field_order == AV_FIELD_PROGRESSIVE)
3046  print_str("field_order", "progressive");
3047  else if (par->field_order == AV_FIELD_TT)
3048  print_str("field_order", "tt");
3049  else if (par->field_order == AV_FIELD_BB)
3050  print_str("field_order", "bb");
3051  else if (par->field_order == AV_FIELD_TB)
3052  print_str("field_order", "tb");
3053  else if (par->field_order == AV_FIELD_BT)
3054  print_str("field_order", "bt");
3055  else
3056  print_str_opt("field_order", "unknown");
3057 
3058  if (dec_ctx)
3059  print_int("refs", dec_ctx->refs);
3060  break;
3061 
3062  case AVMEDIA_TYPE_AUDIO:
3064  if (s) print_str ("sample_fmt", s);
3065  else print_str_opt("sample_fmt", "unknown");
3066  print_val("sample_rate", par->sample_rate, unit_hertz_str);
3067  print_int("channels", par->ch_layout.nb_channels);
3068 
3069  if (par->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
3070  av_channel_layout_describe(&par->ch_layout, val_str, sizeof(val_str));
3071  print_str ("channel_layout", val_str);
3072  } else {
3073  print_str_opt("channel_layout", "unknown");
3074  }
3075 
3076  print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
3077 
3078  print_int("initial_padding", par->initial_padding);
3079  break;
3080 
3081  case AVMEDIA_TYPE_SUBTITLE:
3082  if (par->width)
3083  print_int("width", par->width);
3084  else
3085  print_str_opt("width", "N/A");
3086  if (par->height)
3087  print_int("height", par->height);
3088  else
3089  print_str_opt("height", "N/A");
3090  break;
3091  }
3092 
3094  const AVOption *opt = NULL;
3095  while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
3096  uint8_t *str;
3097  if (!(opt->flags & AV_OPT_FLAG_EXPORT)) continue;
3098  if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
3099  print_str(opt->name, str);
3100  av_free(str);
3101  }
3102  }
3103  }
3104 
3105  if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
3106  else print_str_opt("id", "N/A");
3107  print_q("r_frame_rate", stream->r_frame_rate, '/');
3108  print_q("avg_frame_rate", stream->avg_frame_rate, '/');
3109  print_q("time_base", stream->time_base, '/');
3110  print_ts ("start_pts", stream->start_time);
3111  print_time("start_time", stream->start_time, &stream->time_base);
3112  print_ts ("duration_ts", stream->duration);
3113  print_time("duration", stream->duration, &stream->time_base);
3114  if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
3115  else print_str_opt("bit_rate", "N/A");
3116  if (dec_ctx && dec_ctx->rc_max_rate > 0)
3118  else
3119  print_str_opt("max_bit_rate", "N/A");
3120  if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
3121  else print_str_opt("bits_per_raw_sample", "N/A");
3122  if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
3123  else print_str_opt("nb_frames", "N/A");
3124  if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
3125  else print_str_opt("nb_read_frames", "N/A");
3126  if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
3127  else print_str_opt("nb_read_packets", "N/A");
3128  if (do_show_data)
3129  writer_print_data(w, "extradata", par->extradata,
3130  par->extradata_size);
3131 
3132  if (par->extradata_size > 0) {
3133  print_int("extradata_size", par->extradata_size);
3134  writer_print_data_hash(w, "extradata_hash", par->extradata,
3135  par->extradata_size);
3136  }
3137 
3138  /* Print disposition information */
3139 #define PRINT_DISPOSITION(flagname, name) do { \
3140  print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
3141  } while (0)
3142 
3145  PRINT_DISPOSITION(DEFAULT, "default");
3146  PRINT_DISPOSITION(DUB, "dub");
3147  PRINT_DISPOSITION(ORIGINAL, "original");
3148  PRINT_DISPOSITION(COMMENT, "comment");
3149  PRINT_DISPOSITION(LYRICS, "lyrics");
3150  PRINT_DISPOSITION(KARAOKE, "karaoke");
3151  PRINT_DISPOSITION(FORCED, "forced");
3152  PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
3153  PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
3154  PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
3155  PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
3156  PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
3157  PRINT_DISPOSITION(CAPTIONS, "captions");
3158  PRINT_DISPOSITION(DESCRIPTIONS, "descriptions");
3159  PRINT_DISPOSITION(METADATA, "metadata");
3160  PRINT_DISPOSITION(DEPENDENT, "dependent");
3161  PRINT_DISPOSITION(STILL_IMAGE, "still_image");
3163  }
3164 
3165  if (do_show_stream_tags)
3167 
3168  if (stream->nb_side_data) {
3169  print_pkt_side_data(w, stream->codecpar, stream->side_data, stream->nb_side_data,
3172  }
3173 
3175  av_bprint_finalize(&pbuf, NULL);
3176  fflush(stdout);
3177 
3178  return ret;
3179 }
3180 
3182 {
3183  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3184  int i, ret = 0;
3185 
3187  for (i = 0; i < ifile->nb_streams; i++)
3188  if (selected_streams[i]) {
3189  ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
3190  if (ret < 0)
3191  break;
3192  }
3194 
3195  return ret;
3196 }
3197 
3199 {
3200  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3201  int i, ret = 0;
3202 
3204  print_int("program_id", program->id);
3205  print_int("program_num", program->program_num);
3206  print_int("nb_streams", program->nb_stream_indexes);
3207  print_int("pmt_pid", program->pmt_pid);
3208  print_int("pcr_pid", program->pcr_pid);
3211  if (ret < 0)
3212  goto end;
3213 
3215  for (i = 0; i < program->nb_stream_indexes; i++) {
3216  if (selected_streams[program->stream_index[i]]) {
3217  ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
3218  if (ret < 0)
3219  break;
3220  }
3221  }
3223 
3224 end:
3226  return ret;
3227 }
3228 
3230 {
3231  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3232  int i, ret = 0;
3233 
3235  for (i = 0; i < fmt_ctx->nb_programs; i++) {
3237  if (!program)
3238  continue;
3240  if (ret < 0)
3241  break;
3242  }
3244  return ret;
3245 }
3246 
3248 {
3249  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3250  int i, ret = 0;
3251 
3253  for (i = 0; i < fmt_ctx->nb_chapters; i++) {
3254  AVChapter *chapter = fmt_ctx->chapters[i];
3255 
3257  print_int("id", chapter->id);
3258  print_q ("time_base", chapter->time_base, '/');
3259  print_int("start", chapter->start);
3260  print_time("start_time", chapter->start, &chapter->time_base);
3261  print_int("end", chapter->end);
3262  print_time("end_time", chapter->end, &chapter->time_base);
3266  }
3268 
3269  return ret;
3270 }
3271 
3273 {
3274  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3275  char val_str[128];
3276  int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
3277  int ret = 0;
3278 
3280  print_str_validate("filename", fmt_ctx->url);
3281  print_int("nb_streams", fmt_ctx->nb_streams);
3282  print_int("nb_programs", fmt_ctx->nb_programs);
3283  print_str("format_name", fmt_ctx->iformat->name);
3284  if (!do_bitexact) {
3285  if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
3286  else print_str_opt("format_long_name", "unknown");
3287  }
3288  print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
3289  print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
3290  if (size >= 0) print_val ("size", size, unit_byte_str);
3291  else print_str_opt("size", "N/A");
3293  else print_str_opt("bit_rate", "N/A");
3294  print_int("probe_score", fmt_ctx->probe_score);
3295  if (do_show_format_tags)
3297 
3299  fflush(stdout);
3300  return ret;
3301 }
3302 
3303 static void show_error(WriterContext *w, int err)
3304 {
3306  print_int("code", err);
3307  print_str("string", av_err2str(err));
3309 }
3310 
3311 static int open_input_file(InputFile *ifile, const char *filename,
3312  const char *print_filename)
3313 {
3314  int err, i;
3316  const AVDictionaryEntry *t = NULL;
3317  int scan_all_pmts_set = 0;
3318 
3320  if (!fmt_ctx)
3321  report_and_exit(AVERROR(ENOMEM));
3322 
3323  if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
3324  av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
3325  scan_all_pmts_set = 1;
3326  }
3327  if ((err = avformat_open_input(&fmt_ctx, filename,
3328  iformat, &format_opts)) < 0) {
3329  print_error(filename, err);
3330  return err;
3331  }
3332  if (print_filename) {
3333  av_freep(&fmt_ctx->url);
3334  fmt_ctx->url = av_strdup(print_filename);
3335  }
3336  ifile->fmt_ctx = fmt_ctx;
3337  if (scan_all_pmts_set)
3338  av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
3339  while ((t = av_dict_iterate(format_opts, t)))
3340  av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
3341 
3342  if (find_stream_info) {
3344  int orig_nb_streams = fmt_ctx->nb_streams;
3345 
3347 
3348  for (i = 0; i < orig_nb_streams; i++)
3349  av_dict_free(&opts[i]);
3350  av_freep(&opts);
3351 
3352  if (err < 0) {
3353  print_error(filename, err);
3354  return err;
3355  }
3356  }
3357 
3358  av_dump_format(fmt_ctx, 0, filename, 0);
3359 
3360  ifile->streams = av_calloc(fmt_ctx->nb_streams, sizeof(*ifile->streams));
3361  if (!ifile->streams)
3362  exit(1);
3363  ifile->nb_streams = fmt_ctx->nb_streams;
3364 
3365  /* bind a decoder to each input stream */
3366  for (i = 0; i < fmt_ctx->nb_streams; i++) {
3367  InputStream *ist = &ifile->streams[i];
3368  AVStream *stream = fmt_ctx->streams[i];
3369  const AVCodec *codec;
3370 
3371  ist->st = stream;
3372 
3373  if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
3375  "Failed to probe codec for input stream %d\n",
3376  stream->index);
3377  continue;
3378  }
3379 
3380  codec = avcodec_find_decoder(stream->codecpar->codec_id);
3381  if (!codec) {
3383  "Unsupported codec with id %d for input stream %d\n",
3384  stream->codecpar->codec_id, stream->index);
3385  continue;
3386  }
3387  {
3389  fmt_ctx, stream, codec);
3390 
3391  ist->dec_ctx = avcodec_alloc_context3(codec);
3392  if (!ist->dec_ctx)
3393  exit(1);
3394 
3395  err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
3396  if (err < 0)
3397  exit(1);
3398 
3399  if (do_show_log) {
3400  // For loging it is needed to disable at least frame threads as otherwise
3401  // the log information would need to be reordered and matches up to contexts and frames
3402  // That is in fact possible but not trivial
3403  av_dict_set(&codec_opts, "threads", "1", 0);
3404  }
3405 
3406  ist->dec_ctx->pkt_timebase = stream->time_base;
3407 
3408  if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
3409  av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
3410  stream->index);
3411  exit(1);
3412  }
3413 
3414  if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
3415  av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
3416  t->key, stream->index);
3417  return AVERROR_OPTION_NOT_FOUND;
3418  }
3419  }
3420  }
3421 
3422  ifile->fmt_ctx = fmt_ctx;
3423  return 0;
3424 }
3425 
3427 {
3428  int i;
3429 
3430  /* close decoder for each stream */
3431  for (i = 0; i < ifile->nb_streams; i++)
3432  avcodec_free_context(&ifile->streams[i].dec_ctx);
3433 
3434  av_freep(&ifile->streams);
3435  ifile->nb_streams = 0;
3436 
3437  avformat_close_input(&ifile->fmt_ctx);
3438 }
3439 
3440 static int probe_file(WriterContext *wctx, const char *filename,
3441  const char *print_filename)
3442 {
3443  InputFile ifile = { 0 };
3444  int ret, i;
3445  int section_id;
3446 
3449 
3450  ret = open_input_file(&ifile, filename, print_filename);
3451  if (ret < 0)
3452  goto end;
3453 
3454 #define CHECK_END if (ret < 0) goto end
3455 
3456  nb_streams = ifile.fmt_ctx->nb_streams;
3457  REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
3458  REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
3459  REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
3460 
3461  for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
3462  if (stream_specifier) {
3464  ifile.fmt_ctx->streams[i],
3466  CHECK_END;
3467  else
3468  selected_streams[i] = ret;
3469  ret = 0;
3470  } else {
3471  selected_streams[i] = 1;
3472  }
3473  if (!selected_streams[i])
3474  ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
3475  }
3476 
3480  section_id = SECTION_ID_PACKETS_AND_FRAMES;
3481  else if (do_show_packets && !do_show_frames)
3482  section_id = SECTION_ID_PACKETS;
3483  else // (!do_show_packets && do_show_frames)
3484  section_id = SECTION_ID_FRAMES;
3486  writer_print_section_header(wctx, section_id);
3487  ret = read_packets(wctx, &ifile);
3490  CHECK_END;
3491  }
3492 
3493  if (do_show_programs) {
3494  ret = show_programs(wctx, &ifile);
3495  CHECK_END;
3496  }
3497 
3498  if (do_show_streams) {
3499  ret = show_streams(wctx, &ifile);
3500  CHECK_END;
3501  }
3502  if (do_show_chapters) {
3503  ret = show_chapters(wctx, &ifile);
3504  CHECK_END;
3505  }
3506  if (do_show_format) {
3507  ret = show_format(wctx, &ifile);
3508  CHECK_END;
3509  }
3510 
3511 end:
3512  if (ifile.fmt_ctx)
3517 
3518  return ret;
3519 }
3520 
3521 static void show_usage(void)
3522 {
3523  av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3524  av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name);
3525  av_log(NULL, AV_LOG_INFO, "\n");
3526 }
3527 
3529 {
3530  AVBPrint pbuf;
3532 
3534  print_str("version", FFMPEG_VERSION);
3535  print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3536  program_birth_year, CONFIG_THIS_YEAR);
3537  print_str("compiler_ident", CC_IDENT);
3538  print_str("configuration", FFMPEG_CONFIGURATION);
3540 
3541  av_bprint_finalize(&pbuf, NULL);
3542 }
3543 
3544 #define SHOW_LIB_VERSION(libname, LIBNAME) \
3545  do { \
3546  if (CONFIG_##LIBNAME) { \
3547  unsigned int version = libname##_version(); \
3548  writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
3549  print_str("name", "lib" #libname); \
3550  print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
3551  print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
3552  print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
3553  print_int("version", version); \
3554  print_str("ident", LIB##LIBNAME##_IDENT); \
3555  writer_print_section_footer(w); \
3556  } \
3557  } while (0)
3558 
3560 {
3562  SHOW_LIB_VERSION(avutil, AVUTIL);
3563  SHOW_LIB_VERSION(avcodec, AVCODEC);
3564  SHOW_LIB_VERSION(avformat, AVFORMAT);
3565  SHOW_LIB_VERSION(avdevice, AVDEVICE);
3566  SHOW_LIB_VERSION(avfilter, AVFILTER);
3567  SHOW_LIB_VERSION(swscale, SWSCALE);
3568  SHOW_LIB_VERSION(swresample, SWRESAMPLE);
3569  SHOW_LIB_VERSION(postproc, POSTPROC);
3571 }
3572 
3573 #define PRINT_PIX_FMT_FLAG(flagname, name) \
3574  do { \
3575  print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
3576  } while (0)
3577 
3579 {
3580  const AVPixFmtDescriptor *pixdesc = NULL;
3581  int i, n;
3582 
3584  while (pixdesc = av_pix_fmt_desc_next(pixdesc)) {
3586  print_str("name", pixdesc->name);
3587  print_int("nb_components", pixdesc->nb_components);
3588  if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
3589  print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
3590  print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
3591  } else {
3592  print_str_opt("log2_chroma_w", "N/A");
3593  print_str_opt("log2_chroma_h", "N/A");
3594  }
3595  n = av_get_bits_per_pixel(pixdesc);
3596  if (n) print_int ("bits_per_pixel", n);
3597  else print_str_opt("bits_per_pixel", "N/A");
3600  PRINT_PIX_FMT_FLAG(BE, "big_endian");
3601  PRINT_PIX_FMT_FLAG(PAL, "palette");
3602  PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
3603  PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
3604  PRINT_PIX_FMT_FLAG(PLANAR, "planar");
3605  PRINT_PIX_FMT_FLAG(RGB, "rgb");
3606  PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
3608  }
3609  if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
3611  for (i = 0; i < pixdesc->nb_components; i++) {
3613  print_int("index", i + 1);
3614  print_int("bit_depth", pixdesc->comp[i].depth);
3616  }
3618  }
3620  }
3622 }
3623 
3624 static int opt_show_optional_fields(void *optctx, const char *opt, const char *arg)
3625 {
3629 
3632  return 0;
3633 }
3634 
3635 static int opt_format(void *optctx, const char *opt, const char *arg)
3636 {
3638  if (!iformat) {
3639  av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
3640  return AVERROR(EINVAL);
3641  }
3642  return 0;
3643 }
3644 
3645 static inline void mark_section_show_entries(SectionID section_id,
3646  int show_all_entries, AVDictionary *entries)
3647 {
3648  struct section *section = &sections[section_id];
3649 
3651  if (show_all_entries) {
3652  SectionID *id;
3653  for (id = section->children_ids; *id != -1; id++)
3655  } else {
3656  av_dict_copy(&section->entries_to_show, entries, 0);
3657  }
3658 }
3659 
3660 static int match_section(const char *section_name,
3661  int show_all_entries, AVDictionary *entries)
3662 {
3663  int i, ret = 0;
3664 
3665  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
3666  const struct section *section = &sections[i];
3667  if (!strcmp(section_name, section->name) ||
3668  (section->unique_name && !strcmp(section_name, section->unique_name))) {
3670  "'%s' matches section with unique name '%s'\n", section_name,
3672  ret++;
3674  }
3675  }
3676  return ret;
3677 }
3678 
3679 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
3680 {
3681  const char *p = arg;
3682  int ret = 0;
3683 
3684  while (*p) {
3685  AVDictionary *entries = NULL;
3686  char *section_name = av_get_token(&p, "=:");
3687  int show_all_entries = 0;
3688 
3689  if (!section_name) {
3691  "Missing section name for option '%s'\n", opt);
3692  return AVERROR(EINVAL);
3693  }
3694 
3695  if (*p == '=') {
3696  p++;
3697  while (*p && *p != ':') {
3698  char *entry = av_get_token(&p, ",:");
3699  if (!entry)
3700  break;
3702  "Adding '%s' to the entries to show in section '%s'\n",
3703  entry, section_name);
3704  av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
3705  if (*p == ',')
3706  p++;
3707  }
3708  } else {
3709  show_all_entries = 1;
3710  }
3711 
3712  ret = match_section(section_name, show_all_entries, entries);
3713  if (ret == 0) {
3714  av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
3715  ret = AVERROR(EINVAL);
3716  }
3717  av_dict_free(&entries);
3718  av_free(section_name);
3719 
3720  if (ret <= 0)
3721  break;
3722  if (*p)
3723  p++;
3724  }
3725 
3726  return ret;
3727 }
3728 
3729 static void opt_input_file(void *optctx, const char *arg)
3730 {
3731  if (input_filename) {
3733  "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3734  arg, input_filename);
3735  exit_program(1);
3736  }
3737  if (!strcmp(arg, "-"))
3738  arg = "fd:";
3739  input_filename = arg;
3740 }
3741 
3742 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
3743 {
3744  opt_input_file(optctx, arg);
3745  return 0;
3746 }
3747 
3748 static void opt_output_file(void *optctx, const char *arg)
3749 {
3750  if (output_filename) {
3752  "Argument '%s' provided as output filename, but '%s' was already specified.\n",
3753  arg, output_filename);
3754  exit_program(1);
3755  }
3756  if (!strcmp(arg, "-"))
3757  arg = "fd:";
3758  output_filename = arg;
3759 }
3760 
3761 static int opt_output_file_o(void *optctx, const char *opt, const char *arg)
3762 {
3763  opt_output_file(optctx, arg);
3764  return 0;
3765 }
3766 
3767 static int opt_print_filename(void *optctx, const char *opt, const char *arg)
3768 {
3770  return 0;
3771 }
3772 
3773 void show_help_default(const char *opt, const char *arg)
3774 {
3776  show_usage();
3777  show_help_options(options, "Main options:", 0, 0, 0);
3778  printf("\n");
3779 
3782 }
3783 
3784 /**
3785  * Parse interval specification, according to the format:
3786  * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
3787  * INTERVALS ::= INTERVAL[,INTERVALS]
3788 */
3789 static int parse_read_interval(const char *interval_spec,
3790  ReadInterval *interval)
3791 {
3792  int ret = 0;
3793  char *next, *p, *spec = av_strdup(interval_spec);
3794  if (!spec)
3795  return AVERROR(ENOMEM);
3796 
3797  if (!*spec) {
3798  av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
3799  ret = AVERROR(EINVAL);
3800  goto end;
3801  }
3802 
3803  p = spec;
3804  next = strchr(spec, '%');
3805  if (next)
3806  *next++ = 0;
3807 
3808  /* parse first part */
3809  if (*p) {
3810  interval->has_start = 1;
3811 
3812  if (*p == '+') {
3813  interval->start_is_offset = 1;
3814  p++;
3815  } else {
3816  interval->start_is_offset = 0;
3817  }
3818 
3819  ret = av_parse_time(&interval->start, p, 1);
3820  if (ret < 0) {
3821  av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
3822  goto end;
3823  }
3824  } else {
3825  interval->has_start = 0;
3826  }
3827 
3828  /* parse second part */
3829  p = next;
3830  if (p && *p) {
3831  int64_t us;
3832  interval->has_end = 1;
3833 
3834  if (*p == '+') {
3835  interval->end_is_offset = 1;
3836  p++;
3837  } else {
3838  interval->end_is_offset = 0;
3839  }
3840 
3841  if (interval->end_is_offset && *p == '#') {
3842  long long int lli;
3843  char *tail;
3844  interval->duration_frames = 1;
3845  p++;
3846  lli = strtoll(p, &tail, 10);
3847  if (*tail || lli < 0) {
3849  "Invalid or negative value '%s' for duration number of frames\n", p);
3850  goto end;
3851  }
3852  interval->end = lli;
3853  } else {
3854  interval->duration_frames = 0;
3855  ret = av_parse_time(&us, p, 1);
3856  if (ret < 0) {
3857  av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
3858  goto end;
3859  }
3860  interval->end = us;
3861  }
3862  } else {
3863  interval->has_end = 0;
3864  }
3865 
3866 end:
3867  av_free(spec);
3868  return ret;
3869 }
3870 
3871 static int parse_read_intervals(const char *intervals_spec)
3872 {
3873  int ret, n, i;
3874  char *p, *spec = av_strdup(intervals_spec);
3875  if (!spec)
3876  return AVERROR(ENOMEM);
3877 
3878  /* preparse specification, get number of intervals */
3879  for (n = 0, p = spec; *p; p++)
3880  if (*p == ',')
3881  n++;
3882  n++;
3883 
3885  if (!read_intervals) {
3886  ret = AVERROR(ENOMEM);
3887  goto end;
3888  }
3889  read_intervals_nb = n;
3890 
3891  /* parse intervals */
3892  p = spec;
3893  for (i = 0; p; i++) {
3894  char *next;
3895 
3897  next = strchr(p, ',');
3898  if (next)
3899  *next++ = 0;
3900 
3901  read_intervals[i].id = i;
3903  if (ret < 0) {
3904  av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
3905  i, p);
3906  goto end;
3907  }
3908  av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
3910  p = next;
3911  }
3913 
3914 end:
3915  av_free(spec);
3916  return ret;
3917 }
3918 
3919 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
3920 {
3921  return parse_read_intervals(arg);
3922 }
3923 
3924 static int opt_pretty(void *optctx, const char *opt, const char *arg)
3925 {
3926  show_value_unit = 1;
3927  use_value_prefix = 1;
3930  return 0;
3931 }
3932 
3933 static void print_section(SectionID id, int level)
3934 {
3935  const SectionID *pid;
3936  const struct section *section = &sections[id];
3937  printf("%c%c%c",
3938  section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
3939  section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
3941  printf("%*c %s", level * 4, ' ', section->name);
3942  if (section->unique_name)
3943  printf("/%s", section->unique_name);
3944  printf("\n");
3945 
3946  for (pid = section->children_ids; *pid != -1; pid++)
3947  print_section(*pid, level+1);
3948 }
3949 
3950 static int opt_sections(void *optctx, const char *opt, const char *arg)
3951 {
3952  printf("Sections:\n"
3953  "W.. = Section is a wrapper (contains other sections, no local entries)\n"
3954  ".A. = Section contains an array of elements of the same type\n"
3955  "..V = Section may contain a variable number of fields with variable keys\n"
3956  "FLAGS NAME/UNIQUE_NAME\n"
3957  "---\n");
3959  return 0;
3960 }
3961 
3962 static int opt_show_versions(void *optctx, const char *opt, const char *arg)
3963 {
3966  return 0;
3967 }
3968 
3969 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
3970  static int opt_show_##section(void *optctx, const char *opt, const char *arg) \
3971  { \
3972  mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
3973  return 0; \
3974  }
3975 
3976 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
3980 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
3981 DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
3982 DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
3983 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
3984 DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
3985 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
3986 
3987 static const OptionDef real_options[] = {
3989  { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
3990  { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
3991  { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
3992  { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
3993  "use binary prefixes for byte units" },
3994  { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
3995  "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
3996  { "pretty", 0, {.func_arg = opt_pretty},
3997  "prettify the format of displayed values, make it more human readable" },
3998  { "print_format", OPT_STRING | HAS_ARG, { &print_format },
3999  "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
4000  { "of", OPT_STRING | HAS_ARG, { &print_format }, "alias for -print_format", "format" },
4001  { "select_streams", OPT_STRING | HAS_ARG, { &stream_specifier }, "select the specified streams", "stream_specifier" },
4002  { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
4003  { "show_data", OPT_BOOL, { &do_show_data }, "show packets data" },
4004  { "show_data_hash", OPT_STRING | HAS_ARG, { &show_data_hash }, "show packets data hash" },
4005  { "show_error", 0, { .func_arg = &opt_show_error }, "show probing error" },
4006  { "show_format", 0, { .func_arg = &opt_show_format }, "show format/container info" },
4007  { "show_frames", 0, { .func_arg = &opt_show_frames }, "show frames info" },
4008  { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
4009  "show a set of specified entries", "entry_list" },
4010 #if HAVE_THREADS
4011  { "show_log", OPT_INT|HAS_ARG, { &do_show_log }, "show log" },
4012 #endif
4013  { "show_packets", 0, { .func_arg = &opt_show_packets }, "show packets info" },
4014  { "show_programs", 0, { .func_arg = &opt_show_programs }, "show programs info" },
4015  { "show_streams", 0, { .func_arg = &opt_show_streams }, "show streams info" },
4016  { "show_chapters", 0, { .func_arg = &opt_show_chapters }, "show chapters info" },
4017  { "count_frames", OPT_BOOL, { &do_count_frames }, "count the number of frames per stream" },
4018  { "count_packets", OPT_BOOL, { &do_count_packets }, "count the number of packets per stream" },
4019  { "show_program_version", 0, { .func_arg = &opt_show_program_version }, "show ffprobe version" },
4020  { "show_library_versions", 0, { .func_arg = &opt_show_library_versions }, "show library versions" },
4021  { "show_versions", 0, { .func_arg = &opt_show_versions }, "show program and library versions" },
4022  { "show_pixel_formats", 0, { .func_arg = &opt_show_pixel_formats }, "show pixel format descriptions" },
4023  { "show_optional_fields", HAS_ARG, { .func_arg = &