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