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