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