FFmpegKit iOS / macOS / tvOS API  4.4
fftools_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 
26 /*
27  * CHANGES 01.2020
28  * - ffprobe support changes
29  * - Concurrent execution support
30  * - main() function renamed as ffprobe_execute()
31  */
32 
33 #include "config.h"
34 #include "libavutil/ffversion.h"
35 
36 #include <string.h>
37 
38 #include "libavformat/avformat.h"
39 #include "libavcodec/avcodec.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/bprint.h"
43 #include "libavutil/display.h"
44 #include "libavutil/hash.h"
45 #include "libavutil/hdr_dynamic_metadata.h"
46 #include "libavutil/mastering_display_metadata.h"
47 #include "libavutil/dovi_meta.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/pixdesc.h"
50 #include "libavutil/spherical.h"
51 #include "libavutil/stereo3d.h"
52 #include "libavutil/dict.h"
53 #include "libavutil/intreadwrite.h"
54 #include "libavutil/libm.h"
55 #include "libavutil/parseutils.h"
56 #include "libavutil/timecode.h"
57 #include "libavutil/timestamp.h"
58 #include "libavdevice/avdevice.h"
59 #include "libswscale/swscale.h"
60 #include "libswresample/swresample.h"
61 #include "fftools_cmdutils.h"
62 
63 #include "libavutil/thread.h"
64 
65 #include "ffmpegkit_exception.h"
66 
67 #if !HAVE_THREADS
68 # ifdef pthread_mutex_lock
69 # undef pthread_mutex_lock
70 # endif
71 # define pthread_mutex_lock(a) do{}while(0)
72 # ifdef pthread_mutex_unlock
73 # undef pthread_mutex_unlock
74 # endif
75 # define pthread_mutex_unlock(a) do{}while(0)
76 #endif
77 
78 typedef struct InputStream {
79  AVStream *st;
80 
81  AVCodecContext *dec_ctx;
83 
84 typedef struct InputFile {
85  AVFormatContext *fmt_ctx;
86 
88  int nb_streams;
90 
91 __thread int do_bitexact = 0;
92 __thread int do_count_frames = 0;
93 __thread int do_count_packets = 0;
94 __thread int do_read_frames = 0;
95 __thread int do_read_packets = 0;
96 __thread int do_show_chapters = 0;
97 __thread int do_show_error = 0;
98 __thread int do_show_format = 0;
99 __thread int do_show_frames = 0;
100 __thread int do_show_packets = 0;
101 __thread int do_show_programs = 0;
102 __thread int do_show_streams = 0;
103 __thread int do_show_stream_disposition = 0;
104 __thread int do_show_data = 0;
105 __thread int do_show_program_version = 0;
106 __thread int do_show_library_versions = 0;
107 __thread int do_show_pixel_formats = 0;
108 __thread int do_show_pixel_format_flags = 0;
110 __thread int do_show_log = 0;
111 
112 __thread int do_show_chapter_tags = 0;
113 __thread int do_show_format_tags = 0;
114 __thread int do_show_frame_tags = 0;
115 __thread int do_show_program_tags = 0;
116 __thread int do_show_stream_tags = 0;
117 __thread int do_show_packet_tags = 0;
118 
119 __thread int show_value_unit = 0;
120 __thread int use_value_prefix = 0;
123 __thread int show_private_data = 1;
124 
125 __thread char *print_format;
126 __thread char *stream_specifier;
127 __thread char *show_data_hash;
128 
129 typedef struct ReadInterval {
130  int id;
131  int64_t start, end;
136 
138 __thread int read_intervals_nb = 0;
139 
140 /* section structure definition */
141 
142 #define SECTION_MAX_NB_CHILDREN 10
143 
144 struct section {
145  int id;
146  const char *name;
147 
148 #define SECTION_FLAG_IS_WRAPPER 1
149 #define SECTION_FLAG_IS_ARRAY 2
150 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4
152  int flags;
154  const char *element_name;
155  const char *unique_name;
156  AVDictionary *entries_to_show;
158 };
159 
160 typedef enum {
206 } SectionID;
207 
208 static struct section sections[] = {
210  [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
211  [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
212  [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
213  [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
214  [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
217  [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
218  [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" },
223  [SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
225  [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
229  [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
230  [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" },
231  [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", 0, { -1 } },
234  [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
235  [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" },
237  [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
238  [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" },
240  [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
242  [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
243  [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
251  [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
252  [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
253  [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" },
254  [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", 0, { -1 } },
255  [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
256 };
257 
258 __thread OptionDef *ffprobe_options = NULL;
259 
260 /* FFprobe context */
261 __thread const char *input_filename;
262 __thread const char *print_input_filename;
263 __thread AVInputFormat *iformat = NULL;
264 
265 __thread struct AVHashContext *hash;
266 
267 __thread volatile int main_ffprobe_return_code = 0;
268 extern __thread volatile int longjmp_value;
269 
270 static const struct {
271  double bin_val;
272  double dec_val;
273  const char *bin_str;
274  const char *dec_str;
275 } si_prefixes[] = {
276  { 1.0, 1.0, "", "" },
277  { 1.024e3, 1e3, "Ki", "K" },
278  { 1.048576e6, 1e6, "Mi", "M" },
279  { 1.073741824e9, 1e9, "Gi", "G" },
280  { 1.099511627776e12, 1e12, "Ti", "T" },
281  { 1.125899906842624e15, 1e15, "Pi", "P" },
282 };
283 
284 static const char unit_second_str[] = "s" ;
285 static const char unit_hertz_str[] = "Hz" ;
286 static const char unit_byte_str[] = "byte" ;
287 static const char unit_bit_per_second_str[] = "bit/s";
288 
289 __thread int nb_streams;
290 __thread uint64_t *nb_streams_packets;
291 __thread uint64_t *nb_streams_frames;
292 __thread int *selected_streams;
293 
294 #if HAVE_THREADS
295 __thread pthread_mutex_t log_mutex;
296 #endif
297 typedef struct LogBuffer {
300  char *log_message;
301  AVClassCategory category;
302  char *parent_name;
303  AVClassCategory parent_category;
305 
307 __thread int log_buffer_size;
308 
309 static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
310 {
311  AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
312  va_list vl2;
313  char line[1024];
314  static int print_prefix = 1;
315  void *new_log_buffer;
316 
317  va_copy(vl2, vl);
318  av_log_default_callback(ptr, level, fmt, vl);
319  av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
320  va_end(vl2);
321 
322 #if HAVE_THREADS
323  pthread_mutex_lock(&log_mutex);
324 
325  new_log_buffer = av_realloc_array(log_buffer, log_buffer_size + 1, sizeof(*log_buffer));
326  if (new_log_buffer) {
327  char *msg;
328  int i;
329 
330  log_buffer = new_log_buffer;
331  memset(&log_buffer[log_buffer_size], 0, sizeof(log_buffer[log_buffer_size]));
332  log_buffer[log_buffer_size].context_name= avc ? av_strdup(avc->item_name(ptr)) : NULL;
333  if (avc) {
334  if (avc->get_category) log_buffer[log_buffer_size].category = avc->get_category(ptr);
335  else log_buffer[log_buffer_size].category = avc->category;
336  }
338  msg = log_buffer[log_buffer_size].log_message = av_strdup(line);
339  for (i=strlen(msg) - 1; i>=0 && msg[i] == '\n'; i--) {
340  msg[i] = 0;
341  }
342  if (avc && avc->parent_log_context_offset) {
343  AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
344  avc->parent_log_context_offset);
345  if (parent && *parent) {
346  log_buffer[log_buffer_size].parent_name = av_strdup((*parent)->item_name(parent));
348  (*parent)->get_category ? (*parent)->get_category(parent) :(*parent)->category;
349  }
350  }
351  log_buffer_size ++;
352  }
353 
354  pthread_mutex_unlock(&log_mutex);
355 #endif
356 }
357 
358 static void ffprobe_cleanup(int ret)
359 {
360  int i;
361  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
362  av_dict_free(&(sections[i].entries_to_show));
363 
364 #if HAVE_THREADS
365  pthread_mutex_destroy(&log_mutex);
366 #endif
367 }
368 
369 struct unit_value {
370  union { double d; long long int i; } val;
371  const char *unit;
372 };
373 
374 static char *value_string(char *buf, int buf_size, struct unit_value uv)
375 {
376  double vald;
377  long long int vali;
378  int show_float = 0;
379 
380  if (uv.unit == unit_second_str) {
381  vald = uv.val.d;
382  show_float = 1;
383  } else {
384  vald = vali = uv.val.i;
385  }
386 
388  double secs;
389  int hours, mins;
390  secs = vald;
391  mins = (int)secs / 60;
392  secs = secs - mins * 60;
393  hours = mins / 60;
394  mins %= 60;
395  snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
396  } else {
397  const char *prefix_string = "";
398 
399  if (use_value_prefix && vald > 1) {
400  long long int index;
401 
403  index = (long long int) (log2(vald)) / 10;
404  index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
405  vald /= si_prefixes[index].bin_val;
406  prefix_string = si_prefixes[index].bin_str;
407  } else {
408  index = (long long int) (log10(vald)) / 3;
409  index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
410  vald /= si_prefixes[index].dec_val;
411  prefix_string = si_prefixes[index].dec_str;
412  }
413  vali = vald;
414  }
415 
416  if (show_float || (use_value_prefix && vald != (long long int)vald))
417  snprintf(buf, buf_size, "%f", vald);
418  else
419  snprintf(buf, buf_size, "%lld", vali);
420  av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
421  prefix_string, show_value_unit ? uv.unit : "");
422  }
423 
424  return buf;
425 }
426 
427 /* WRITERS API */
428 
429 typedef struct WriterContext WriterContext;
430 
431 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
432 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
433 
434 typedef enum {
440 
441 typedef struct Writer {
442  const AVClass *priv_class;
443  int priv_size;
444  const char *name;
445 
446  int (*init) (WriterContext *wctx);
447  void (*uninit)(WriterContext *wctx);
448 
451  void (*print_integer) (WriterContext *wctx, const char *, long long int);
452  void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
453  void (*print_string) (WriterContext *wctx, const char *, const char *);
454  int flags;
456 
457 #define SECTION_MAX_NB_LEVELS 10
458 
460  const AVClass *class;
461  const Writer *writer;
462  char *name;
463  void *priv;
464 
465  const struct section *sections;
467 
468  int level;
469 
472 
477 
478  unsigned int nb_section_packet;
479  unsigned int nb_section_frame;
480  unsigned int nb_section_packet_frame;
481 
485 };
486 
487 static const char *writer_get_name(void *p)
488 {
489  WriterContext *wctx = p;
490  return wctx->writer->name;
491 }
492 
493 #define OFFSET(x) offsetof(WriterContext, x)
494 
495 static const AVOption writer_options[] = {
496  { "string_validation", "set string validation mode",
497  OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
498  { "sv", "set string validation mode",
499  OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
500  { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
501  { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
502  { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
503  { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
504  { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
505  { NULL }
506 };
507 
508 static void *writer_child_next(void *obj, void *prev)
509 {
510  WriterContext *ctx = obj;
511  if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
512  return ctx->priv;
513  return NULL;
514 }
515 
516 static const AVClass writer_class = {
517  .class_name = "Writer",
518  .item_name = writer_get_name,
519  .option = writer_options,
520  .version = LIBAVUTIL_VERSION_INT,
521  .child_next = writer_child_next,
522 };
523 
524 static void writer_close(WriterContext **wctx)
525 {
526  int i;
527 
528  if (!*wctx)
529  return;
530 
531  if ((*wctx)->writer->uninit)
532  (*wctx)->writer->uninit(*wctx);
533  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
534  av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
535  if ((*wctx)->writer->priv_class)
536  av_opt_free((*wctx)->priv);
537  av_freep(&((*wctx)->priv));
538  av_opt_free(*wctx);
539  av_freep(wctx);
540 }
541 
542 static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
543 {
544  int i;
545  av_bprintf(bp, "0X");
546  for (i = 0; i < ubuf_size; i++)
547  av_bprintf(bp, "%02X", ubuf[i]);
548 }
549 
550 
551 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
552  const struct section *sections, int nb_sections)
553 {
554  int i, ret = 0;
555 
556  if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
557  ret = AVERROR(ENOMEM);
558  goto fail;
559  }
560 
561  if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
562  ret = AVERROR(ENOMEM);
563  goto fail;
564  }
565 
566  (*wctx)->class = &writer_class;
567  (*wctx)->writer = writer;
568  (*wctx)->level = -1;
569  (*wctx)->sections = sections;
570  (*wctx)->nb_sections = nb_sections;
571 
572  av_opt_set_defaults(*wctx);
573 
574  if (writer->priv_class) {
575  void *priv_ctx = (*wctx)->priv;
576  *((const AVClass **)priv_ctx) = writer->priv_class;
577  av_opt_set_defaults(priv_ctx);
578  }
579 
580  /* convert options to dictionary */
581  if (args) {
582  AVDictionary *opts = NULL;
583  AVDictionaryEntry *opt = NULL;
584 
585  if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
586  av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
587  av_dict_free(&opts);
588  goto fail;
589  }
590 
591  while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
592  if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
593  av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
594  opt->key, opt->value);
595  av_dict_free(&opts);
596  goto fail;
597  }
598  }
599 
600  av_dict_free(&opts);
601  }
602 
603  /* validate replace string */
604  {
605  const uint8_t *p = (*wctx)->string_validation_replacement;
606  const uint8_t *endp = p + strlen(p);
607  while (*p) {
608  const uint8_t *p0 = p;
609  int32_t code;
610  ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
611  if (ret < 0) {
612  AVBPrint bp;
613  av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
614  bprint_bytes(&bp, p0, p-p0),
615  av_log(wctx, AV_LOG_ERROR,
616  "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
617  bp.str, (*wctx)->string_validation_replacement);
618  return ret;
619  }
620  }
621  }
622 
623  for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
624  av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
625 
626  if ((*wctx)->writer->init)
627  ret = (*wctx)->writer->init(*wctx);
628  if (ret < 0)
629  goto fail;
630 
631  return 0;
632 
633 fail:
634  writer_close(wctx);
635  return ret;
636 }
637 
639  int section_id)
640 {
641  int parent_section_id;
642  wctx->level++;
643  av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
644  parent_section_id = wctx->level ?
645  (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
646 
647  wctx->nb_item[wctx->level] = 0;
648  wctx->section[wctx->level] = &wctx->sections[section_id];
649 
650  if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
651  wctx->nb_section_packet = wctx->nb_section_frame =
652  wctx->nb_section_packet_frame = 0;
653  } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
654  wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
655  wctx->nb_section_packet : wctx->nb_section_frame;
656  }
657 
658  if (wctx->writer->print_section_header)
659  wctx->writer->print_section_header(wctx);
660 }
661 
663 {
664  int section_id = wctx->section[wctx->level]->id;
665  int parent_section_id = wctx->level ?
666  wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
667 
668  if (parent_section_id != SECTION_ID_NONE)
669  wctx->nb_item[wctx->level-1]++;
670  if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
671  if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
672  else wctx->nb_section_frame++;
673  }
674  if (wctx->writer->print_section_footer)
675  wctx->writer->print_section_footer(wctx);
676  wctx->level--;
677 }
678 
679 static inline void writer_print_integer(WriterContext *wctx,
680  const char *key, long long int val)
681 {
682  const struct section *section = wctx->section[wctx->level];
683 
684  if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
685  wctx->writer->print_integer(wctx, key, val);
686  wctx->nb_item[wctx->level]++;
687  }
688 }
689 
690 static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
691 {
692  const uint8_t *p, *endp;
693  AVBPrint dstbuf;
694  int invalid_chars_nb = 0, ret = 0;
695 
696  av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
697 
698  endp = src + strlen(src);
699  for (p = (uint8_t *)src; *p;) {
700  uint32_t code;
701  int invalid = 0;
702  const uint8_t *p0 = p;
703 
704  if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
705  AVBPrint bp;
706  av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
707  bprint_bytes(&bp, p0, p-p0);
708  av_log(wctx, AV_LOG_DEBUG,
709  "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
710  invalid = 1;
711  }
712 
713  if (invalid) {
714  invalid_chars_nb++;
715 
716  switch (wctx->string_validation) {
718  av_log(wctx, AV_LOG_ERROR,
719  "Invalid UTF-8 sequence found in string '%s'\n", src);
720  ret = AVERROR_INVALIDDATA;
721  goto end;
722  break;
723 
725  av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
726  break;
727  }
728  }
729 
730  if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
731  av_bprint_append_data(&dstbuf, p0, p-p0);
732  }
733 
734  if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
735  av_log(wctx, AV_LOG_WARNING,
736  "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
737  invalid_chars_nb, src, wctx->string_validation_replacement);
738  }
739 
740 end:
741  av_bprint_finalize(&dstbuf, dstp);
742  return ret;
743 }
744 
745 #define PRINT_STRING_OPT 1
746 #define PRINT_STRING_VALIDATE 2
747 
748 static inline int writer_print_string(WriterContext *wctx,
749  const char *key, const char *val, int flags)
750 {
751  const struct section *section = wctx->section[wctx->level];
752  int ret = 0;
753 
754  if ((flags & PRINT_STRING_OPT)
756  return 0;
757 
758  if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
760  char *key1 = NULL, *val1 = NULL;
761  ret = validate_string(wctx, &key1, key);
762  if (ret < 0) goto end;
763  ret = validate_string(wctx, &val1, val);
764  if (ret < 0) goto end;
765  wctx->writer->print_string(wctx, key1, val1);
766  end:
767  if (ret < 0) {
768  av_log(wctx, AV_LOG_ERROR,
769  "Invalid key=value string combination %s=%s in section %s\n",
770  key, val, section->unique_name);
771  }
772  av_free(key1);
773  av_free(val1);
774  } else {
775  wctx->writer->print_string(wctx, key, val);
776  }
777 
778  wctx->nb_item[wctx->level]++;
779  }
780 
781  return ret;
782 }
783 
784 static inline void writer_print_rational(WriterContext *wctx,
785  const char *key, AVRational q, char sep)
786 {
787  AVBPrint buf;
788  av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
789  av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
790  writer_print_string(wctx, key, buf.str, 0);
791 }
792 
793 static void writer_print_time(WriterContext *wctx, const char *key,
794  int64_t ts, const AVRational *time_base, int is_duration)
795 {
796  char buf[128];
797 
798  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
799  writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
800  } else {
801  double d = ts * av_q2d(*time_base);
802  struct unit_value uv;
803  uv.val.d = d;
804  uv.unit = unit_second_str;
805  value_string(buf, sizeof(buf), uv);
806  writer_print_string(wctx, key, buf, 0);
807  }
808 }
809 
810 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
811 {
812  if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
813  writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
814  } else {
815  writer_print_integer(wctx, key, ts);
816  }
817 }
818 
819 static void writer_print_data(WriterContext *wctx, const char *name,
820  uint8_t *data, int size)
821 {
822  AVBPrint bp;
823  int offset = 0, l, i;
824 
825  av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
826  av_bprintf(&bp, "\n");
827  while (size) {
828  av_bprintf(&bp, "%08x: ", offset);
829  l = FFMIN(size, 16);
830  for (i = 0; i < l; i++) {
831  av_bprintf(&bp, "%02x", data[i]);
832  if (i & 1)
833  av_bprintf(&bp, " ");
834  }
835  av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
836  for (i = 0; i < l; i++)
837  av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
838  av_bprintf(&bp, "\n");
839  offset += l;
840  data += l;
841  size -= l;
842  }
843  writer_print_string(wctx, name, bp.str, 0);
844  av_bprint_finalize(&bp, NULL);
845 }
846 
847 static void writer_print_data_hash(WriterContext *wctx, const char *name,
848  uint8_t *data, int size)
849 {
850  char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
851 
852  if (!hash)
853  return;
854  av_hash_init(hash);
855  av_hash_update(hash, data, size);
856  snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
857  p = buf + strlen(buf);
858  av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
859  writer_print_string(wctx, name, buf, 0);
860 }
861 
862 static void writer_print_integers(WriterContext *wctx, const char *name,
863  uint8_t *data, int size, const char *format,
864  int columns, int bytes, int offset_add)
865 {
866  AVBPrint bp;
867  int offset = 0, l, i;
868 
869  av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
870  av_bprintf(&bp, "\n");
871  while (size) {
872  av_bprintf(&bp, "%08x: ", offset);
873  l = FFMIN(size, columns);
874  for (i = 0; i < l; i++) {
875  if (bytes == 1) av_bprintf(&bp, format, *data);
876  else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
877  else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
878  data += bytes;
879  size --;
880  }
881  av_bprintf(&bp, "\n");
882  offset += offset_add;
883  }
884  writer_print_string(wctx, name, bp.str, 0);
885  av_bprint_finalize(&bp, NULL);
886 }
887 
888 #define MAX_REGISTERED_WRITERS_NB 64
889 
891 
892 __thread int next_registered_writer_idx = 0;
893 
894 static int writer_register(const Writer *writer)
895 {
897  return AVERROR(ENOMEM);
898 
900  return 0;
901 }
902 
903 static const Writer *writer_get_by_name(const char *name)
904 {
905  int i;
906 
907  for (i = 0; registered_writers[i]; i++)
908  if (!strcmp(registered_writers[i]->name, name))
909  return registered_writers[i];
910 
911  return NULL;
912 }
913 
914 
915 /* WRITERS */
916 
917 #define DEFINE_WRITER_CLASS(name) \
918 static const char *name##_get_name(void *ctx) \
919 { \
920  return #name ; \
921 } \
922 static const AVClass name##_class = { \
923  .class_name = #name, \
924  .item_name = name##_get_name, \
925  .option = name##_options \
926 }
927 
928 /* Default output */
929 
930 typedef struct DefaultContext {
931  const AVClass *class;
932  int nokey;
936 
937 #undef OFFSET
938 #define OFFSET(x) offsetof(DefaultContext, x)
939 
940 static const AVOption default_options[] = {
941  { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
942  { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
943  { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
944  { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
945  {NULL},
946 };
947 
949 
950 /* lame uppercasing routine, assumes the string is lower case ASCII */
951 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
952 {
953  int i;
954  for (i = 0; src[i] && i < dst_size-1; i++)
955  dst[i] = av_toupper(src[i]);
956  dst[i] = 0;
957  return dst;
958 }
959 
961 {
962  DefaultContext *def = wctx->priv;
963  char buf[32];
964  const struct section *section = wctx->section[wctx->level];
965  const struct section *parent_section = wctx->level ?
966  wctx->section[wctx->level-1] : NULL;
967 
968  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
969  if (parent_section &&
970  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
971  def->nested_section[wctx->level] = 1;
972  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
973  wctx->section_pbuf[wctx->level-1].str,
974  upcase_string(buf, sizeof(buf),
975  av_x_if_null(section->element_name, section->name)));
976  }
977 
978  if (def->noprint_wrappers || def->nested_section[wctx->level])
979  return;
980 
982  av_log(NULL, AV_LOG_STDERR, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
983 }
984 
986 {
987  DefaultContext *def = wctx->priv;
988  const struct section *section = wctx->section[wctx->level];
989  char buf[32];
990 
991  if (def->noprint_wrappers || def->nested_section[wctx->level])
992  return;
993 
995  av_log(NULL, AV_LOG_STDERR, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
996 }
997 
998 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
999 {
1000  DefaultContext *def = wctx->priv;
1001 
1002  if (!def->nokey)
1003  av_log(NULL, AV_LOG_STDERR, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1004  av_log(NULL, AV_LOG_STDERR, "%s\n", value);
1005 }
1006 
1007 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
1008 {
1009  DefaultContext *def = wctx->priv;
1010 
1011  if (!def->nokey)
1012  av_log(NULL, AV_LOG_STDERR, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1013  av_log(NULL, AV_LOG_STDERR, "%lld\n", value);
1014 }
1015 
1016 static const Writer default_writer = {
1017  .name = "default",
1018  .priv_size = sizeof(DefaultContext),
1021  .print_integer = default_print_int,
1022  .print_string = default_print_str,
1024  .priv_class = &default_class,
1025 };
1026 
1027 /* Compact output */
1028 
1032 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1033 {
1034  const char *p;
1035 
1036  for (p = src; *p; p++) {
1037  switch (*p) {
1038  case '\b': av_bprintf(dst, "%s", "\\b"); break;
1039  case '\f': av_bprintf(dst, "%s", "\\f"); break;
1040  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1041  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1042  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1043  default:
1044  if (*p == sep)
1045  av_bprint_chars(dst, '\\', 1);
1046  av_bprint_chars(dst, *p, 1);
1047  }
1048  }
1049  return dst->str;
1050 }
1051 
1055 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1056 {
1057  char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
1058  int needs_quoting = !!src[strcspn(src, meta_chars)];
1059 
1060  if (needs_quoting)
1061  av_bprint_chars(dst, '"', 1);
1062 
1063  for (; *src; src++) {
1064  if (*src == '"')
1065  av_bprint_chars(dst, '"', 1);
1066  av_bprint_chars(dst, *src, 1);
1067  }
1068  if (needs_quoting)
1069  av_bprint_chars(dst, '"', 1);
1070  return dst->str;
1071 }
1072 
1073 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1074 {
1075  return src;
1076 }
1077 
1078 typedef struct CompactContext {
1079  const AVClass *class;
1081  char item_sep;
1082  int nokey;
1085  const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
1090 
1091 #undef OFFSET
1092 #define OFFSET(x) offsetof(CompactContext, x)
1093 
1094 static const AVOption compact_options[]= {
1095  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1096  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1097  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1098  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1099  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1100  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1101  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1102  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1103  {NULL},
1104 };
1105 
1107 
1108 static av_cold int compact_init(WriterContext *wctx)
1109 {
1110  CompactContext *compact = wctx->priv;
1111 
1112  if (strlen(compact->item_sep_str) != 1) {
1113  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1114  compact->item_sep_str);
1115  return AVERROR(EINVAL);
1116  }
1117  compact->item_sep = compact->item_sep_str[0];
1118 
1119  if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
1120  else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
1121  else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
1122  else {
1123  av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
1124  return AVERROR(EINVAL);
1125  }
1126 
1127  return 0;
1128 }
1129 
1131 {
1132  CompactContext *compact = wctx->priv;
1133  const struct section *section = wctx->section[wctx->level];
1134  const struct section *parent_section = wctx->level ?
1135  wctx->section[wctx->level-1] : NULL;
1136  compact->terminate_line[wctx->level] = 1;
1137  compact->has_nested_elems[wctx->level] = 0;
1138 
1139  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1140  if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
1141  !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1142  compact->nested_section[wctx->level] = 1;
1143  compact->has_nested_elems[wctx->level-1] = 1;
1144  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1145  wctx->section_pbuf[wctx->level-1].str,
1146  (char *)av_x_if_null(section->element_name, section->name));
1147  wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
1148  } else {
1149  if (parent_section && compact->has_nested_elems[wctx->level-1] &&
1151  compact->terminate_line[wctx->level-1] = 0;
1152  av_log(NULL, AV_LOG_STDERR, "\n");
1153  }
1154  if (compact->print_section &&
1156  av_log(NULL, AV_LOG_STDERR, "%s%c", section->name, compact->item_sep);
1157  }
1158 }
1159 
1161 {
1162  CompactContext *compact = wctx->priv;
1163 
1164  if (!compact->nested_section[wctx->level] &&
1165  compact->terminate_line[wctx->level] &&
1167  av_log(NULL, AV_LOG_STDERR, "\n");
1168 }
1169 
1170 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
1171 {
1172  CompactContext *compact = wctx->priv;
1173  AVBPrint buf;
1174 
1175  if (wctx->nb_item[wctx->level]) av_log(NULL, AV_LOG_STDERR, "%c", compact->item_sep);
1176  if (!compact->nokey)
1177  av_log(NULL, AV_LOG_STDERR, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1178  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1179  av_log(NULL, AV_LOG_STDERR, "%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
1180  av_bprint_finalize(&buf, NULL);
1181 }
1182 
1183 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
1184 {
1185  CompactContext *compact = wctx->priv;
1186 
1187  if (wctx->nb_item[wctx->level]) av_log(NULL, AV_LOG_STDERR, "%c", compact->item_sep);
1188  if (!compact->nokey)
1189  av_log(NULL, AV_LOG_STDERR, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1190  av_log(NULL, AV_LOG_STDERR, "%lld", value);
1191 }
1192 
1193 static const Writer compact_writer = {
1194  .name = "compact",
1195  .priv_size = sizeof(CompactContext),
1196  .init = compact_init,
1199  .print_integer = compact_print_int,
1200  .print_string = compact_print_str,
1202  .priv_class = &compact_class,
1203 };
1204 
1205 /* CSV output */
1206 
1207 #undef OFFSET
1208 #define OFFSET(x) offsetof(CompactContext, x)
1209 
1210 static const AVOption csv_options[] = {
1211  {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1212  {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1213  {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1214  {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1215  {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1216  {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1217  {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1218  {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1219  {NULL},
1220 };
1221 
1223 
1224 static const Writer csv_writer = {
1225  .name = "csv",
1226  .priv_size = sizeof(CompactContext),
1227  .init = compact_init,
1230  .print_integer = compact_print_int,
1231  .print_string = compact_print_str,
1233  .priv_class = &csv_class,
1234 };
1235 
1236 /* Flat output */
1237 
1238 typedef struct FlatContext {
1239  const AVClass *class;
1240  const char *sep_str;
1241  char sep;
1244 
1245 #undef OFFSET
1246 #define OFFSET(x) offsetof(FlatContext, x)
1247 
1248 static const AVOption flat_options[]= {
1249  {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1250  {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1251  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1252  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1253  {NULL},
1254 };
1255 
1257 
1258 static av_cold int flat_init(WriterContext *wctx)
1259 {
1260  FlatContext *flat = wctx->priv;
1261 
1262  if (strlen(flat->sep_str) != 1) {
1263  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1264  flat->sep_str);
1265  return AVERROR(EINVAL);
1266  }
1267  flat->sep = flat->sep_str[0];
1268 
1269  return 0;
1270 }
1271 
1272 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1273 {
1274  const char *p;
1275 
1276  for (p = src; *p; p++) {
1277  if (!((*p >= '0' && *p <= '9') ||
1278  (*p >= 'a' && *p <= 'z') ||
1279  (*p >= 'A' && *p <= 'Z')))
1280  av_bprint_chars(dst, '_', 1);
1281  else
1282  av_bprint_chars(dst, *p, 1);
1283  }
1284  return dst->str;
1285 }
1286 
1287 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1288 {
1289  const char *p;
1290 
1291  for (p = src; *p; p++) {
1292  switch (*p) {
1293  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1294  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1295  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1296  case '"': av_bprintf(dst, "%s", "\\\""); break;
1297  case '`': av_bprintf(dst, "%s", "\\`"); break;
1298  case '$': av_bprintf(dst, "%s", "\\$"); break;
1299  default: av_bprint_chars(dst, *p, 1); break;
1300  }
1301  }
1302  return dst->str;
1303 }
1304 
1306 {
1307  FlatContext *flat = wctx->priv;
1308  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1309  const struct section *section = wctx->section[wctx->level];
1310  const struct section *parent_section = wctx->level ?
1311  wctx->section[wctx->level-1] : NULL;
1312 
1313  /* build section header */
1314  av_bprint_clear(buf);
1315  if (!parent_section)
1316  return;
1317  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1318 
1319  if (flat->hierarchical ||
1321  av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1322 
1323  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1324  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1325  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1326  av_bprintf(buf, "%d%s", n, flat->sep_str);
1327  }
1328  }
1329 }
1330 
1331 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1332 {
1333  av_log(NULL, AV_LOG_STDERR, "%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1334 }
1335 
1336 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1337 {
1338  FlatContext *flat = wctx->priv;
1339  AVBPrint buf;
1340 
1341  av_log(NULL, AV_LOG_STDERR, "%s", wctx->section_pbuf[wctx->level].str);
1342  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1343  av_log(NULL, AV_LOG_STDERR, "%s=", flat_escape_key_str(&buf, key, flat->sep));
1344  av_bprint_clear(&buf);
1345  av_log(NULL, AV_LOG_STDERR, "\"%s\"\n", flat_escape_value_str(&buf, value));
1346  av_bprint_finalize(&buf, NULL);
1347 }
1348 
1349 static const Writer flat_writer = {
1350  .name = "flat",
1351  .priv_size = sizeof(FlatContext),
1352  .init = flat_init,
1354  .print_integer = flat_print_int,
1355  .print_string = flat_print_str,
1357  .priv_class = &flat_class,
1358 };
1359 
1360 /* INI format output */
1361 
1362 typedef struct INIContext {
1363  const AVClass *class;
1366 
1367 #undef OFFSET
1368 #define OFFSET(x) offsetof(INIContext, x)
1369 
1370 static const AVOption ini_options[] = {
1371  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1372  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1373  {NULL},
1374 };
1375 
1377 
1378 static char *ini_escape_str(AVBPrint *dst, const char *src)
1379 {
1380  int i = 0;
1381  char c = 0;
1382 
1383  while ((c = src[i++])) {
1384  switch (c) {
1385  case '\b': av_bprintf(dst, "%s", "\\b"); break;
1386  case '\f': av_bprintf(dst, "%s", "\\f"); break;
1387  case '\n': av_bprintf(dst, "%s", "\\n"); break;
1388  case '\r': av_bprintf(dst, "%s", "\\r"); break;
1389  case '\t': av_bprintf(dst, "%s", "\\t"); break;
1390  case '\\':
1391  case '#' :
1392  case '=' :
1393  case ':' : av_bprint_chars(dst, '\\', 1);
1394  default:
1395  if ((unsigned char)c < 32)
1396  av_bprintf(dst, "\\x00%02x", c & 0xff);
1397  else
1398  av_bprint_chars(dst, c, 1);
1399  break;
1400  }
1401  }
1402  return dst->str;
1403 }
1404 
1406 {
1407  INIContext *ini = wctx->priv;
1408  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1409  const struct section *section = wctx->section[wctx->level];
1410  const struct section *parent_section = wctx->level ?
1411  wctx->section[wctx->level-1] : NULL;
1412 
1413  av_bprint_clear(buf);
1414  if (!parent_section) {
1415  av_log(NULL, AV_LOG_STDERR, "# ffprobe output\n\n");
1416  return;
1417  }
1418 
1419  if (wctx->nb_item[wctx->level-1])
1420  av_log(NULL, AV_LOG_STDERR, "\n");
1421 
1422  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1423  if (ini->hierarchical ||
1425  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1426 
1427  if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1428  int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1429  wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1430  av_bprintf(buf, ".%d", n);
1431  }
1432  }
1433 
1435  av_log(NULL, AV_LOG_STDERR, "[%s]\n", buf->str);
1436 }
1437 
1438 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1439 {
1440  AVBPrint buf;
1441 
1442  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1443  av_log(NULL, AV_LOG_STDERR, "%s=", ini_escape_str(&buf, key));
1444  av_bprint_clear(&buf);
1445  av_log(NULL, AV_LOG_STDERR, "%s\n", ini_escape_str(&buf, value));
1446  av_bprint_finalize(&buf, NULL);
1447 }
1448 
1449 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1450 {
1451  av_log(NULL, AV_LOG_STDERR, "%s=%lld\n", key, value);
1452 }
1453 
1454 static const Writer ini_writer = {
1455  .name = "ini",
1456  .priv_size = sizeof(INIContext),
1458  .print_integer = ini_print_int,
1459  .print_string = ini_print_str,
1461  .priv_class = &ini_class,
1462 };
1463 
1464 /* JSON output */
1465 
1466 typedef struct JSONContext {
1467  const AVClass *class;
1469  int compact;
1470  const char *item_sep, *item_start_end;
1472 
1473 #undef OFFSET
1474 #define OFFSET(x) offsetof(JSONContext, x)
1475 
1476 static const AVOption json_options[]= {
1477  { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1478  { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1479  { NULL }
1480 };
1481 
1483 
1484 static av_cold int json_init(WriterContext *wctx)
1485 {
1486  JSONContext *json = wctx->priv;
1487 
1488  json->item_sep = json->compact ? ", " : ",\n";
1489  json->item_start_end = json->compact ? " " : "\n";
1490 
1491  return 0;
1492 }
1493 
1494 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1495 {
1496  static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1497  static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1498  const char *p;
1499 
1500  for (p = src; *p; p++) {
1501  char *s = strchr(json_escape, *p);
1502  if (s) {
1503  av_bprint_chars(dst, '\\', 1);
1504  av_bprint_chars(dst, json_subst[s - json_escape], 1);
1505  } else if ((unsigned char)*p < 32) {
1506  av_bprintf(dst, "\\u00%02x", *p & 0xff);
1507  } else {
1508  av_bprint_chars(dst, *p, 1);
1509  }
1510  }
1511  return dst->str;
1512 }
1513 
1514 #define JSON_INDENT() av_log(NULL, AV_LOG_STDERR, "%*c", json->indent_level * 4, ' ')
1515 
1517 {
1518  JSONContext *json = wctx->priv;
1519  AVBPrint buf;
1520  const struct section *section = wctx->section[wctx->level];
1521  const struct section *parent_section = wctx->level ?
1522  wctx->section[wctx->level-1] : NULL;
1523 
1524  if (wctx->level && wctx->nb_item[wctx->level-1])
1525  av_log(NULL, AV_LOG_STDERR, ",\n");
1526 
1528  av_log(NULL, AV_LOG_STDERR, "{\n");
1529  json->indent_level++;
1530  } else {
1531  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1532  json_escape_str(&buf, section->name, wctx);
1533  JSON_INDENT();
1534 
1535  json->indent_level++;
1537  av_log(NULL, AV_LOG_STDERR, "\"%s\": [\n", buf.str);
1538  } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1539  av_log(NULL, AV_LOG_STDERR, "\"%s\": {%s", buf.str, json->item_start_end);
1540  } else {
1541  av_log(NULL, AV_LOG_STDERR, "{%s", json->item_start_end);
1542 
1543  /* this is required so the parser can distinguish between packets and frames */
1544  if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1545  if (!json->compact)
1546  JSON_INDENT();
1547  av_log(NULL, AV_LOG_STDERR, "\"type\": \"%s\"", section->name);
1548  }
1549  }
1550  av_bprint_finalize(&buf, NULL);
1551  }
1552 }
1553 
1555 {
1556  JSONContext *json = wctx->priv;
1557  const struct section *section = wctx->section[wctx->level];
1558 
1559  if (wctx->level == 0) {
1560  json->indent_level--;
1561  av_log(NULL, AV_LOG_STDERR, "\n}\n");
1562  } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1563  av_log(NULL, AV_LOG_STDERR, "\n");
1564  json->indent_level--;
1565  JSON_INDENT();
1566  av_log(NULL, AV_LOG_STDERR, "]");
1567  } else {
1568  av_log(NULL, AV_LOG_STDERR, "%s", json->item_start_end);
1569  json->indent_level--;
1570  if (!json->compact)
1571  JSON_INDENT();
1572  av_log(NULL, AV_LOG_STDERR, "}");
1573  }
1574 }
1575 
1576 static inline void json_print_item_str(WriterContext *wctx,
1577  const char *key, const char *value)
1578 {
1579  AVBPrint buf;
1580 
1581  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1582  av_log(NULL, AV_LOG_STDERR, "\"%s\":", json_escape_str(&buf, key, wctx));
1583  av_bprint_clear(&buf);
1584  av_log(NULL, AV_LOG_STDERR, " \"%s\"", json_escape_str(&buf, value, wctx));
1585  av_bprint_finalize(&buf, NULL);
1586 }
1587 
1588 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1589 {
1590  JSONContext *json = wctx->priv;
1591  const struct section *parent_section = wctx->level ?
1592  wctx->section[wctx->level-1] : NULL;
1593 
1594  if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1595  av_log(NULL, AV_LOG_STDERR, "%s", json->item_sep);
1596  if (!json->compact)
1597  JSON_INDENT();
1598  json_print_item_str(wctx, key, value);
1599 }
1600 
1601 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1602 {
1603  JSONContext *json = wctx->priv;
1604  const struct section *parent_section = wctx->level ?
1605  wctx->section[wctx->level-1] : NULL;
1606  AVBPrint buf;
1607 
1608  if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1609  av_log(NULL, AV_LOG_STDERR, "%s", json->item_sep);
1610  if (!json->compact)
1611  JSON_INDENT();
1612 
1613  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1614  av_log(NULL, AV_LOG_STDERR, "\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1615  av_bprint_finalize(&buf, NULL);
1616 }
1617 
1618 static const Writer json_writer = {
1619  .name = "json",
1620  .priv_size = sizeof(JSONContext),
1621  .init = json_init,
1624  .print_integer = json_print_int,
1625  .print_string = json_print_str,
1627  .priv_class = &json_class,
1628 };
1629 
1630 /* XML output */
1631 
1632 typedef struct XMLContext {
1633  const AVClass *class;
1639 
1640 #undef OFFSET
1641 #define OFFSET(x) offsetof(XMLContext, x)
1642 
1643 static const AVOption xml_options[] = {
1644  {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1645  {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1646  {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1647  {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1648  {NULL},
1649 };
1650 
1652 
1653 static av_cold int xml_init(WriterContext *wctx)
1654 {
1655  XMLContext *xml = wctx->priv;
1656 
1657  if (xml->xsd_strict) {
1658  xml->fully_qualified = 1;
1659 #define CHECK_COMPLIANCE(opt, opt_name) \
1660  if (opt) { \
1661  av_log(wctx, AV_LOG_ERROR, \
1662  "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1663  "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1664  return AVERROR(EINVAL); \
1665  }
1666  CHECK_COMPLIANCE(show_private_data, "private");
1669 
1671  av_log(wctx, AV_LOG_ERROR,
1672  "Interleaved frames and packets are not allowed in XSD. "
1673  "Select only one between the -show_frames and the -show_packets options.\n");
1674  return AVERROR(EINVAL);
1675  }
1676  }
1677 
1678  return 0;
1679 }
1680 
1681 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1682 {
1683  const char *p;
1684 
1685  for (p = src; *p; p++) {
1686  switch (*p) {
1687  case '&' : av_bprintf(dst, "%s", "&amp;"); break;
1688  case '<' : av_bprintf(dst, "%s", "&lt;"); break;
1689  case '>' : av_bprintf(dst, "%s", "&gt;"); break;
1690  case '"' : av_bprintf(dst, "%s", "&quot;"); break;
1691  case '\'': av_bprintf(dst, "%s", "&apos;"); break;
1692  default: av_bprint_chars(dst, *p, 1);
1693  }
1694  }
1695 
1696  return dst->str;
1697 }
1698 
1699 #define XML_INDENT() av_log(NULL, AV_LOG_STDERR, "%*c", xml->indent_level * 4, ' ')
1700 
1702 {
1703  XMLContext *xml = wctx->priv;
1704  const struct section *section = wctx->section[wctx->level];
1705  const struct section *parent_section = wctx->level ?
1706  wctx->section[wctx->level-1] : NULL;
1707 
1708  if (wctx->level == 0) {
1709  const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1710  "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1711  "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1712 
1713  av_log(NULL, AV_LOG_STDERR, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1714  av_log(NULL, AV_LOG_STDERR, "<%sffprobe%s>\n",
1715  xml->fully_qualified ? "ffprobe:" : "",
1716  xml->fully_qualified ? qual : "");
1717  return;
1718  }
1719 
1720  if (xml->within_tag) {
1721  xml->within_tag = 0;
1722  av_log(NULL, AV_LOG_STDERR, ">\n");
1723  }
1725  xml->indent_level++;
1726  } else {
1727  if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1728  wctx->level && wctx->nb_item[wctx->level-1])
1729  av_log(NULL, AV_LOG_STDERR, "\n");
1730  xml->indent_level++;
1731 
1733  XML_INDENT(); av_log(NULL, AV_LOG_STDERR, "<%s>\n", section->name);
1734  } else {
1735  XML_INDENT(); av_log(NULL, AV_LOG_STDERR, "<%s ", section->name);
1736  xml->within_tag = 1;
1737  }
1738  }
1739 }
1740 
1742 {
1743  XMLContext *xml = wctx->priv;
1744  const struct section *section = wctx->section[wctx->level];
1745 
1746  if (wctx->level == 0) {
1747  av_log(NULL, AV_LOG_STDERR, "</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1748  } else if (xml->within_tag) {
1749  xml->within_tag = 0;
1750  av_log(NULL, AV_LOG_STDERR, "/>\n");
1751  xml->indent_level--;
1753  xml->indent_level--;
1754  } else {
1755  XML_INDENT(); av_log(NULL, AV_LOG_STDERR, "</%s>\n", section->name);
1756  xml->indent_level--;
1757  }
1758 }
1759 
1760 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1761 {
1762  AVBPrint buf;
1763  XMLContext *xml = wctx->priv;
1764  const struct section *section = wctx->section[wctx->level];
1765 
1766  av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1767 
1769  XML_INDENT();
1770  av_log(NULL, AV_LOG_STDERR, "<%s key=\"%s\"",
1771  section->element_name, xml_escape_str(&buf, key, wctx));
1772  av_bprint_clear(&buf);
1773  av_log(NULL, AV_LOG_STDERR, " value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1774  } else {
1775  if (wctx->nb_item[wctx->level])
1776  av_log(NULL, AV_LOG_STDERR, " ");
1777  av_log(NULL, AV_LOG_STDERR, "%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1778  }
1779 
1780  av_bprint_finalize(&buf, NULL);
1781 }
1782 
1783 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1784 {
1785  if (wctx->nb_item[wctx->level])
1786  av_log(NULL, AV_LOG_STDERR, " ");
1787  av_log(NULL, AV_LOG_STDERR, "%s=\"%lld\"", key, value);
1788 }
1789 
1790 static Writer xml_writer = {
1791  .name = "xml",
1792  .priv_size = sizeof(XMLContext),
1793  .init = xml_init,
1796  .print_integer = xml_print_int,
1797  .print_string = xml_print_str,
1799  .priv_class = &xml_class,
1800 };
1801 
1802 static void writer_register_all(void)
1803 {
1804 
1812 }
1813 
1814 #define print_fmt(k, f, ...) do { \
1815  av_bprint_clear(&pbuf); \
1816  av_bprintf(&pbuf, f, __VA_ARGS__); \
1817  writer_print_string(w, k, pbuf.str, 0); \
1818 } while (0)
1819 
1820 #define print_int(k, v) writer_print_integer(w, k, v)
1821 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1822 #define print_str(k, v) writer_print_string(w, k, v, 0)
1823 #define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1824 #define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1825 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1826 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1827 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1828 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1829 #define print_val(k, v, u) do { \
1830  struct unit_value uv; \
1831  uv.val.i = v; \
1832  uv.unit = u; \
1833  writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1834 } while (0)
1835 
1836 #define print_section_header(s) writer_print_section_header(w, s)
1837 #define print_section_footer(s) writer_print_section_footer(w, s)
1838 
1839 #define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
1840 { \
1841  ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
1842  if (ret < 0) \
1843  goto end; \
1844  memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
1845 }
1846 
1847 static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1848 {
1849  AVDictionaryEntry *tag = NULL;
1850  int ret = 0;
1851 
1852  if (!tags)
1853  return 0;
1854  writer_print_section_header(w, section_id);
1855 
1856  while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1857  if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1858  break;
1859  }
1861 
1862  return ret;
1863 }
1864 
1865 static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
1866 {
1867  if (!metadata)
1868  return;
1869  print_int("application version", metadata->application_version);
1870  print_int("num_windows", metadata->num_windows);
1871  for (int n = 1; n < metadata->num_windows; n++) {
1872  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1873  print_q("window_upper_left_corner_x",
1874  params->window_upper_left_corner_x,'/');
1875  print_q("window_upper_left_corner_y",
1876  params->window_upper_left_corner_y,'/');
1877  print_q("window_lower_right_corner_x",
1878  params->window_lower_right_corner_x,'/');
1879  print_q("window_lower_right_corner_y",
1880  params->window_lower_right_corner_y,'/');
1881  print_q("window_upper_left_corner_x",
1882  params->window_upper_left_corner_x,'/');
1883  print_q("window_upper_left_corner_y",
1884  params->window_upper_left_corner_y,'/');
1885  print_int("center_of_ellipse_x",
1886  params->center_of_ellipse_x ) ;
1887  print_int("center_of_ellipse_y",
1888  params->center_of_ellipse_y );
1889  print_int("rotation_angle",
1890  params->rotation_angle);
1891  print_int("semimajor_axis_internal_ellipse",
1892  params->semimajor_axis_internal_ellipse);
1893  print_int("semimajor_axis_external_ellipse",
1894  params->semimajor_axis_external_ellipse);
1895  print_int("semiminor_axis_external_ellipse",
1896  params->semiminor_axis_external_ellipse);
1897  print_int("overlap_process_option",
1898  params->overlap_process_option);
1899  }
1900  print_q("targeted_system_display_maximum_luminance",
1901  metadata->targeted_system_display_maximum_luminance,'/');
1902  if (metadata->targeted_system_display_actual_peak_luminance_flag) {
1903  print_int("num_rows_targeted_system_display_actual_peak_luminance",
1904  metadata->num_rows_targeted_system_display_actual_peak_luminance);
1905  print_int("num_cols_targeted_system_display_actual_peak_luminance",
1906  metadata->num_cols_targeted_system_display_actual_peak_luminance);
1907  for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
1908  for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
1909  print_q("targeted_system_display_actual_peak_luminance",
1910  metadata->targeted_system_display_actual_peak_luminance[i][j],'/');
1911  }
1912  }
1913  }
1914  for (int n = 0; n < metadata->num_windows; n++) {
1915  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1916  for (int i = 0; i < 3; i++) {
1917  print_q("maxscl",params->maxscl[i],'/');
1918  }
1919  print_q("average_maxrgb",
1920  params->average_maxrgb,'/');
1921  print_int("num_distribution_maxrgb_percentiles",
1922  params->num_distribution_maxrgb_percentiles);
1923  for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
1924  print_int("distribution_maxrgb_percentage",
1925  params->distribution_maxrgb[i].percentage);
1926  print_q("distribution_maxrgb_percentile",
1927  params->distribution_maxrgb[i].percentile,'/');
1928  }
1929  print_q("fraction_bright_pixels",
1930  params->fraction_bright_pixels,'/');
1931  }
1932  if (metadata->mastering_display_actual_peak_luminance_flag) {
1933  print_int("num_rows_mastering_display_actual_peak_luminance",
1934  metadata->num_rows_mastering_display_actual_peak_luminance);
1935  print_int("num_cols_mastering_display_actual_peak_luminance",
1936  metadata->num_cols_mastering_display_actual_peak_luminance);
1937  for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
1938  for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
1939  print_q("mastering_display_actual_peak_luminance",
1940  metadata->mastering_display_actual_peak_luminance[i][j],'/');
1941  }
1942  }
1943  }
1944 
1945  for (int n = 0; n < metadata->num_windows; n++) {
1946  const AVHDRPlusColorTransformParams *params = &metadata->params[n];
1947  if (params->tone_mapping_flag) {
1948  print_q("knee_point_x", params->knee_point_x,'/');
1949  print_q("knee_point_y", params->knee_point_y,'/');
1950  print_int("num_bezier_curve_anchors",
1951  params->num_bezier_curve_anchors );
1952  for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
1953  print_q("bezier_curve_anchors",
1954  params->bezier_curve_anchors[i],'/');
1955  }
1956  }
1957  if (params->color_saturation_mapping_flag) {
1958  print_q("color_saturation_weight",
1959  params->color_saturation_weight,'/');
1960  }
1961  }
1962 }
1963 
1965  AVCodecParameters *par,
1966  const AVPacketSideData *side_data,
1967  int nb_side_data,
1968  SectionID id_data_list,
1969  SectionID id_data)
1970 {
1971  int i;
1972 
1973  writer_print_section_header(w, id_data_list);
1974  for (i = 0; i < nb_side_data; i++) {
1975  const AVPacketSideData *sd = &side_data[i];
1976  const char *name = av_packet_side_data_name(sd->type);
1977 
1978  writer_print_section_header(w, id_data);
1979  print_str("side_data_type", name ? name : "unknown");
1980  if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
1981  writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
1982  print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
1983  } else if (sd->type == AV_PKT_DATA_STEREO3D) {
1984  const AVStereo3D *stereo = (AVStereo3D *)sd->data;
1985  print_str("type", av_stereo3d_type_name(stereo->type));
1986  print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
1987  } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
1988  const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
1989  print_str("projection", av_spherical_projection_name(spherical->projection));
1990  if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
1991  print_int("padding", spherical->padding);
1992  } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
1993  size_t l, t, r, b;
1994  av_spherical_tile_bounds(spherical, par->width, par->height,
1995  &l, &t, &r, &b);
1996  print_int("bound_left", l);
1997  print_int("bound_top", t);
1998  print_int("bound_right", r);
1999  print_int("bound_bottom", b);
2000  }
2001 
2002  print_int("yaw", (double) spherical->yaw / (1 << 16));
2003  print_int("pitch", (double) spherical->pitch / (1 << 16));
2004  print_int("roll", (double) spherical->roll / (1 << 16));
2005  } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
2006  print_int("skip_samples", AV_RL32(sd->data));
2007  print_int("discard_padding", AV_RL32(sd->data + 4));
2008  print_int("skip_reason", AV_RL8(sd->data + 8));
2009  print_int("discard_reason", AV_RL8(sd->data + 9));
2010  } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
2011  AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2012 
2013  if (metadata->has_primaries) {
2014  print_q("red_x", metadata->display_primaries[0][0], '/');
2015  print_q("red_y", metadata->display_primaries[0][1], '/');
2016  print_q("green_x", metadata->display_primaries[1][0], '/');
2017  print_q("green_y", metadata->display_primaries[1][1], '/');
2018  print_q("blue_x", metadata->display_primaries[2][0], '/');
2019  print_q("blue_y", metadata->display_primaries[2][1], '/');
2020 
2021  print_q("white_point_x", metadata->white_point[0], '/');
2022  print_q("white_point_y", metadata->white_point[1], '/');
2023  }
2024 
2025  if (metadata->has_luminance) {
2026  print_q("min_luminance", metadata->min_luminance, '/');
2027  print_q("max_luminance", metadata->max_luminance, '/');
2028  }
2029  } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
2030  AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2031  print_int("max_content", metadata->MaxCLL);
2032  print_int("max_average", metadata->MaxFALL);
2033  } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
2034  AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)sd->data;
2035  print_int("dv_version_major", dovi->dv_version_major);
2036  print_int("dv_version_minor", dovi->dv_version_minor);
2037  print_int("dv_profile", dovi->dv_profile);
2038  print_int("dv_level", dovi->dv_level);
2039  print_int("rpu_present_flag", dovi->rpu_present_flag);
2040  print_int("el_present_flag", dovi->el_present_flag);
2041  print_int("bl_present_flag", dovi->bl_present_flag);
2042  print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
2043  }
2045  }
2047 }
2048 
2049 static void print_color_range(WriterContext *w, enum AVColorRange color_range)
2050 {
2051  const char *val = av_color_range_name(color_range);
2052  if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
2053  print_str_opt("color_range", "unknown");
2054  } else {
2055  print_str("color_range", val);
2056  }
2057 }
2058 
2059 static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
2060 {
2061  const char *val = av_color_space_name(color_space);
2062  if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
2063  print_str_opt("color_space", "unknown");
2064  } else {
2065  print_str("color_space", val);
2066  }
2067 }
2068 
2069 static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
2070 {
2071  const char *val = av_color_primaries_name(color_primaries);
2072  if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
2073  print_str_opt("color_primaries", "unknown");
2074  } else {
2075  print_str("color_primaries", val);
2076  }
2077 }
2078 
2079 static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
2080 {
2081  const char *val = av_color_transfer_name(color_trc);
2082  if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
2083  print_str_opt("color_transfer", "unknown");
2084  } else {
2085  print_str("color_transfer", val);
2086  }
2087 }
2088 
2089 static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
2090 {
2091  const char *val = av_chroma_location_name(chroma_location);
2092  if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
2093  print_str_opt("chroma_location", "unspecified");
2094  } else {
2095  print_str("chroma_location", val);
2096  }
2097 }
2098 
2099 
2100 static void clear_log(int need_lock)
2101 {
2102  int i;
2103 
2104  if (need_lock)
2105  pthread_mutex_lock(&log_mutex);
2106  for (i=0; i<log_buffer_size; i++) {
2107  av_freep(&log_buffer[i].context_name);
2108  av_freep(&log_buffer[i].parent_name);
2109  av_freep(&log_buffer[i].log_message);
2110  }
2111  log_buffer_size = 0;
2112  if(need_lock)
2113  pthread_mutex_unlock(&log_mutex);
2114 }
2115 
2116 static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
2117 {
2118  int i;
2119  pthread_mutex_lock(&log_mutex);
2120  if (!log_buffer_size) {
2121  pthread_mutex_unlock(&log_mutex);
2122  return 0;
2123  }
2124  writer_print_section_header(w, section_ids);
2125 
2126  for (i=0; i<log_buffer_size; i++) {
2127  if (log_buffer[i].log_level <= log_level) {
2128  writer_print_section_header(w, section_id);
2129  print_str("context", log_buffer[i].context_name);
2130  print_int("level", log_buffer[i].log_level);
2131  print_int("category", log_buffer[i].category);
2132  if (log_buffer[i].parent_name) {
2133  print_str("parent_context", log_buffer[i].parent_name);
2134  print_int("parent_category", log_buffer[i].parent_category);
2135  } else {
2136  print_str_opt("parent_context", "N/A");
2137  print_str_opt("parent_category", "N/A");
2138  }
2139  print_str("message", log_buffer[i].log_message);
2141  }
2142  }
2143  clear_log(0);
2144  pthread_mutex_unlock(&log_mutex);
2145 
2147 
2148  return 0;
2149 }
2150 
2151 static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
2152 {
2153  char val_str[128];
2154  AVStream *st = ifile->streams[pkt->stream_index].st;
2155  AVBPrint pbuf;
2156  const char *s;
2157 
2158  av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2159 
2161 
2162  s = av_get_media_type_string(st->codecpar->codec_type);
2163  if (s) print_str ("codec_type", s);
2164  else print_str_opt("codec_type", "unknown");
2165  print_int("stream_index", pkt->stream_index);
2166  print_ts ("pts", pkt->pts);
2167  print_time("pts_time", pkt->pts, &st->time_base);
2168  print_ts ("dts", pkt->dts);
2169  print_time("dts_time", pkt->dts, &st->time_base);
2170  print_duration_ts("duration", pkt->duration);
2171  print_duration_time("duration_time", pkt->duration, &st->time_base);
2172  print_duration_ts("convergence_duration", pkt->convergence_duration);
2173  print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
2174  print_val("size", pkt->size, unit_byte_str);
2175  if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
2176  else print_str_opt("pos", "N/A");
2177  print_fmt("flags", "%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
2178  pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_');
2179 
2180  if (pkt->side_data_elems) {
2181  int size;
2182  const uint8_t *side_metadata;
2183 
2184  side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
2185  if (side_metadata && size && do_show_packet_tags) {
2186  AVDictionary *dict = NULL;
2187  if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
2189  av_dict_free(&dict);
2190  }
2191 
2192  print_pkt_side_data(w, st->codecpar, pkt->side_data, pkt->side_data_elems,
2195  }
2196 
2197  if (do_show_data)
2198  writer_print_data(w, "data", pkt->data, pkt->size);
2199  writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
2201 
2202  av_bprint_finalize(&pbuf, NULL);
2203  fflush(stdout);
2204 }
2205 
2206 static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
2207  AVFormatContext *fmt_ctx)
2208 {
2209  AVBPrint pbuf;
2210 
2211  av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2212 
2214 
2215  print_str ("media_type", "subtitle");
2216  print_ts ("pts", sub->pts);
2217  print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
2218  print_int ("format", sub->format);
2219  print_int ("start_display_time", sub->start_display_time);
2220  print_int ("end_display_time", sub->end_display_time);
2221  print_int ("num_rects", sub->num_rects);
2222 
2224 
2225  av_bprint_finalize(&pbuf, NULL);
2226  fflush(stdout);
2227 }
2228 
2229 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
2230  AVFormatContext *fmt_ctx)
2231 {
2232  AVBPrint pbuf;
2233  char val_str[128];
2234  const char *s;
2235  int i;
2236 
2237  av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2238 
2240 
2241  s = av_get_media_type_string(stream->codecpar->codec_type);
2242  if (s) print_str ("media_type", s);
2243  else print_str_opt("media_type", "unknown");
2244  print_int("stream_index", stream->index);
2245  print_int("key_frame", frame->key_frame);
2246  print_ts ("pkt_pts", frame->pts);
2247  print_time("pkt_pts_time", frame->pts, &stream->time_base);
2248  print_ts ("pkt_dts", frame->pkt_dts);
2249  print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
2250  print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
2251  print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
2252  print_duration_ts ("pkt_duration", frame->pkt_duration);
2253  print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
2254  if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
2255  else print_str_opt("pkt_pos", "N/A");
2256  if (frame->pkt_size != -1) print_val ("pkt_size", frame->pkt_size, unit_byte_str);
2257  else print_str_opt("pkt_size", "N/A");
2258 
2259  switch (stream->codecpar->codec_type) {
2260  AVRational sar;
2261 
2262  case AVMEDIA_TYPE_VIDEO:
2263  print_int("width", frame->width);
2264  print_int("height", frame->height);
2265  s = av_get_pix_fmt_name(frame->format);
2266  if (s) print_str ("pix_fmt", s);
2267  else print_str_opt("pix_fmt", "unknown");
2268  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
2269  if (sar.num) {
2270  print_q("sample_aspect_ratio", sar, ':');
2271  } else {
2272  print_str_opt("sample_aspect_ratio", "N/A");
2273  }
2274  print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
2275  print_int("coded_picture_number", frame->coded_picture_number);
2276  print_int("display_picture_number", frame->display_picture_number);
2277  print_int("interlaced_frame", frame->interlaced_frame);
2278  print_int("top_field_first", frame->top_field_first);
2279  print_int("repeat_pict", frame->repeat_pict);
2280 
2281  print_color_range(w, frame->color_range);
2282  print_color_space(w, frame->colorspace);
2283  print_primaries(w, frame->color_primaries);
2284  print_color_trc(w, frame->color_trc);
2285  print_chroma_location(w, frame->chroma_location);
2286  break;
2287 
2288  case AVMEDIA_TYPE_AUDIO:
2289  s = av_get_sample_fmt_name(frame->format);
2290  if (s) print_str ("sample_fmt", s);
2291  else print_str_opt("sample_fmt", "unknown");
2292  print_int("nb_samples", frame->nb_samples);
2293  print_int("channels", frame->channels);
2294  if (frame->channel_layout) {
2295  av_bprint_clear(&pbuf);
2296  av_bprint_channel_layout(&pbuf, frame->channels,
2297  frame->channel_layout);
2298  print_str ("channel_layout", pbuf.str);
2299  } else
2300  print_str_opt("channel_layout", "unknown");
2301  break;
2302  }
2303  if (do_show_frame_tags)
2304  show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
2305  if (do_show_log)
2307  if (frame->nb_side_data) {
2309  for (i = 0; i < frame->nb_side_data; i++) {
2310  AVFrameSideData *sd = frame->side_data[i];
2311  const char *name;
2312 
2314  name = av_frame_side_data_name(sd->type);
2315  print_str("side_data_type", name ? name : "unknown");
2316  if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2317  writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2318  print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
2319  } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
2320  char tcbuf[AV_TIMECODE_STR_SIZE];
2321  av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
2322  print_str("timecode", tcbuf);
2323  } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
2324  uint32_t *tc = (uint32_t*)sd->data;
2325  int m = FFMIN(tc[0],3);
2327  for (int j = 1; j <= m ; j++) {
2328  char tcbuf[AV_TIMECODE_STR_SIZE];
2329  av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
2331  print_str("value", tcbuf);
2333  }
2335  } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
2336  AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2337 
2338  if (metadata->has_primaries) {
2339  print_q("red_x", metadata->display_primaries[0][0], '/');
2340  print_q("red_y", metadata->display_primaries[0][1], '/');
2341  print_q("green_x", metadata->display_primaries[1][0], '/');
2342  print_q("green_y", metadata->display_primaries[1][1], '/');
2343  print_q("blue_x", metadata->display_primaries[2][0], '/');
2344  print_q("blue_y", metadata->display_primaries[2][1], '/');
2345 
2346  print_q("white_point_x", metadata->white_point[0], '/');
2347  print_q("white_point_y", metadata->white_point[1], '/');
2348  }
2349 
2350  if (metadata->has_luminance) {
2351  print_q("min_luminance", metadata->min_luminance, '/');
2352  print_q("max_luminance", metadata->max_luminance, '/');
2353  }
2354  } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
2355  AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
2356  print_dynamic_hdr10_plus(w, metadata);
2357  } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
2358  AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2359  print_int("max_content", metadata->MaxCLL);
2360  print_int("max_average", metadata->MaxFALL);
2361  } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
2362  AVDictionaryEntry *tag = av_dict_get(sd->metadata, "name", NULL, AV_DICT_MATCH_CASE);
2363  if (tag)
2364  print_str(tag->key, tag->value);
2365  print_int("size", sd->size);
2366  }
2368  }
2370  }
2371 
2373 
2374  av_bprint_finalize(&pbuf, NULL);
2375  fflush(stdout);
2376 }
2377 
2378 static av_always_inline int process_frame(WriterContext *w,
2379  InputFile *ifile,
2380  AVFrame *frame, AVPacket *pkt,
2381  int *packet_new)
2382 {
2383  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2384  AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
2385  AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
2386  AVSubtitle sub;
2387  int ret = 0, got_frame = 0;
2388 
2389  clear_log(1);
2390  if (dec_ctx && dec_ctx->codec) {
2391  switch (par->codec_type) {
2392  case AVMEDIA_TYPE_VIDEO:
2393  case AVMEDIA_TYPE_AUDIO:
2394  if (*packet_new) {
2395  ret = avcodec_send_packet(dec_ctx, pkt);
2396  if (ret == AVERROR(EAGAIN)) {
2397  ret = 0;
2398  } else if (ret >= 0 || ret == AVERROR_EOF) {
2399  ret = 0;
2400  *packet_new = 0;
2401  }
2402  }
2403  if (ret >= 0) {
2404  ret = avcodec_receive_frame(dec_ctx, frame);
2405  if (ret >= 0) {
2406  got_frame = 1;
2407  } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2408  ret = 0;
2409  }
2410  }
2411  break;
2412 
2413  case AVMEDIA_TYPE_SUBTITLE:
2414  if (*packet_new)
2415  ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
2416  *packet_new = 0;
2417  break;
2418  default:
2419  *packet_new = 0;
2420  }
2421  } else {
2422  *packet_new = 0;
2423  }
2424 
2425  if (ret < 0)
2426  return ret;
2427  if (got_frame) {
2428  int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
2429  nb_streams_frames[pkt->stream_index]++;
2430  if (do_show_frames) {
2431  if (is_sub) {
2432  show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
2433  } else {
2434  show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
2435  }
2436  }
2437  if (is_sub) {
2438  avsubtitle_free(&sub);
2439  }
2440  }
2441  return got_frame || *packet_new;
2442 }
2443 
2444 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
2445 {
2446  av_log(log_ctx, log_level, "id:%d", interval->id);
2447 
2448  if (interval->has_start) {
2449  av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
2450  av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
2451  } else {
2452  av_log(log_ctx, log_level, " start:N/A");
2453  }
2454 
2455  if (interval->has_end) {
2456  av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
2457  if (interval->duration_frames)
2458  av_log(log_ctx, log_level, "#%"PRId64, interval->end);
2459  else
2460  av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
2461  } else {
2462  av_log(log_ctx, log_level, " end:N/A");
2463  }
2464 
2465  av_log(log_ctx, log_level, "\n");
2466 }
2467 
2469  const ReadInterval *interval, int64_t *cur_ts)
2470 {
2471  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2472  AVPacket pkt;
2473  AVFrame *frame = NULL;
2474  int ret = 0, i = 0, frame_count = 0;
2475  int64_t start = -INT64_MAX, end = interval->end;
2476  int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
2477 
2478  av_init_packet(&pkt);
2479 
2480  av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
2481  log_read_interval(interval, NULL, AV_LOG_VERBOSE);
2482 
2483  if (interval->has_start) {
2484  int64_t target;
2485  if (interval->start_is_offset) {
2486  if (*cur_ts == AV_NOPTS_VALUE) {
2487  av_log(NULL, AV_LOG_ERROR,
2488  "Could not seek to relative position since current "
2489  "timestamp is not defined\n");
2490  ret = AVERROR(EINVAL);
2491  goto end;
2492  }
2493  target = *cur_ts + interval->start;
2494  } else {
2495  target = interval->start;
2496  }
2497 
2498  av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
2499  av_ts2timestr(target, &AV_TIME_BASE_Q));
2500  if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
2501  av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
2502  interval->start, av_err2str(ret));
2503  goto end;
2504  }
2505  }
2506 
2507  frame = av_frame_alloc();
2508  if (!frame) {
2509  ret = AVERROR(ENOMEM);
2510  goto end;
2511  }
2512  while (!av_read_frame(fmt_ctx, &pkt)) {
2513  if (fmt_ctx->nb_streams > nb_streams) {
2514  REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
2515  REALLOCZ_ARRAY_STREAM(nb_streams_packets, nb_streams, fmt_ctx->nb_streams);
2516  REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
2517  nb_streams = fmt_ctx->nb_streams;
2518  }
2519  if (selected_streams[pkt.stream_index]) {
2520  AVRational tb = ifile->streams[pkt.stream_index].st->time_base;
2521 
2522  if (pkt.pts != AV_NOPTS_VALUE)
2523  *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
2524 
2525  if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
2526  start = *cur_ts;
2527  has_start = 1;
2528  }
2529 
2530  if (has_start && !has_end && interval->end_is_offset) {
2531  end = start + interval->end;
2532  has_end = 1;
2533  }
2534 
2535  if (interval->end_is_offset && interval->duration_frames) {
2536  if (frame_count >= interval->end)
2537  break;
2538  } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
2539  break;
2540  }
2541 
2542  frame_count++;
2543  if (do_read_packets) {
2544  if (do_show_packets)
2545  show_packet(w, ifile, &pkt, i++);
2546  nb_streams_packets[pkt.stream_index]++;
2547  }
2548  if (do_read_frames) {
2549  int packet_new = 1;
2550  while (process_frame(w, ifile, frame, &pkt, &packet_new) > 0);
2551  }
2552  }
2553  av_packet_unref(&pkt);
2554  }
2555  av_packet_unref(&pkt);
2556  //Flush remaining frames that are cached in the decoder
2557  for (i = 0; i < fmt_ctx->nb_streams; i++) {
2558  pkt.stream_index = i;
2559  if (do_read_frames)
2560  while (process_frame(w, ifile, frame, &pkt, &(int){1}) > 0);
2561  }
2562 
2563 end:
2564  av_frame_free(&frame);
2565  if (ret < 0) {
2566  av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
2567  log_read_interval(interval, NULL, AV_LOG_ERROR);
2568  }
2569  return ret;
2570 }
2571 
2572 static int read_packets(WriterContext *w, InputFile *ifile)
2573 {
2574  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2575  int i, ret = 0;
2576  int64_t cur_ts = fmt_ctx->start_time;
2577 
2578  if (read_intervals_nb == 0) {
2579  ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
2580  ret = read_interval_packets(w, ifile, &interval, &cur_ts);
2581  } else {
2582  for (i = 0; i < read_intervals_nb; i++) {
2583  ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
2584  if (ret < 0)
2585  break;
2586  }
2587  }
2588 
2589  return ret;
2590 }
2591 
2592 static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
2593 {
2594  AVStream *stream = ist->st;
2595  AVCodecParameters *par;
2596  AVCodecContext *dec_ctx;
2597  char val_str[128];
2598  const char *s;
2599  AVRational sar, dar;
2600  AVBPrint pbuf;
2601  const AVCodecDescriptor *cd;
2602  int ret = 0;
2603  const char *profile = NULL;
2604 
2605  av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2606 
2608 
2609  print_int("index", stream->index);
2610 
2611  par = stream->codecpar;
2612  dec_ctx = ist->dec_ctx;
2613  if ((cd = avcodec_descriptor_get(par->codec_id))) {
2614  print_str("codec_name", cd->name);
2615  if (!do_bitexact) {
2616  print_str("codec_long_name",
2617  cd->long_name ? cd->long_name : "unknown");
2618  }
2619  } else {
2620  print_str_opt("codec_name", "unknown");
2621  if (!do_bitexact) {
2622  print_str_opt("codec_long_name", "unknown");
2623  }
2624  }
2625 
2626  if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
2627  print_str("profile", profile);
2628  else {
2629  if (par->profile != FF_PROFILE_UNKNOWN) {
2630  char profile_num[12];
2631  snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
2632  print_str("profile", profile_num);
2633  } else
2634  print_str_opt("profile", "unknown");
2635  }
2636 
2637  s = av_get_media_type_string(par->codec_type);
2638  if (s) print_str ("codec_type", s);
2639  else print_str_opt("codec_type", "unknown");
2640 #if FF_API_LAVF_AVCTX
2641  if (dec_ctx)
2642  print_q("codec_time_base", dec_ctx->time_base, '/');
2643 #endif
2644 
2645  /* print AVI/FourCC tag */
2646  print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
2647  print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
2648 
2649  switch (par->codec_type) {
2650  case AVMEDIA_TYPE_VIDEO:
2651  print_int("width", par->width);
2652  print_int("height", par->height);
2653 #if FF_API_LAVF_AVCTX
2654  if (dec_ctx) {
2655  print_int("coded_width", dec_ctx->coded_width);
2656  print_int("coded_height", dec_ctx->coded_height);
2657  print_int("closed_captions", !!(dec_ctx->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS));
2658  }
2659 #endif
2660  print_int("has_b_frames", par->video_delay);
2661  sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
2662  if (sar.num) {
2663  print_q("sample_aspect_ratio", sar, ':');
2664  av_reduce(&dar.num, &dar.den,
2665  par->width * sar.num,
2666  par->height * sar.den,
2667  1024*1024);
2668  print_q("display_aspect_ratio", dar, ':');
2669  } else {
2670  print_str_opt("sample_aspect_ratio", "N/A");
2671  print_str_opt("display_aspect_ratio", "N/A");
2672  }
2673  s = av_get_pix_fmt_name(par->format);
2674  if (s) print_str ("pix_fmt", s);
2675  else print_str_opt("pix_fmt", "unknown");
2676  print_int("level", par->level);
2677 
2678  print_color_range(w, par->color_range);
2679  print_color_space(w, par->color_space);
2680  print_color_trc(w, par->color_trc);
2681  print_primaries(w, par->color_primaries);
2682  print_chroma_location(w, par->chroma_location);
2683 
2684  if (par->field_order == AV_FIELD_PROGRESSIVE)
2685  print_str("field_order", "progressive");
2686  else if (par->field_order == AV_FIELD_TT)
2687  print_str("field_order", "tt");
2688  else if (par->field_order == AV_FIELD_BB)
2689  print_str("field_order", "bb");
2690  else if (par->field_order == AV_FIELD_TB)
2691  print_str("field_order", "tb");
2692  else if (par->field_order == AV_FIELD_BT)
2693  print_str("field_order", "bt");
2694  else
2695  print_str_opt("field_order", "unknown");
2696 
2697 #if FF_API_PRIVATE_OPT
2698  if (dec_ctx && dec_ctx->timecode_frame_start >= 0) {
2699  char tcbuf[AV_TIMECODE_STR_SIZE];
2700  av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
2701  print_str("timecode", tcbuf);
2702  } else {
2703  print_str_opt("timecode", "N/A");
2704  }
2705 #endif
2706  if (dec_ctx)
2707  print_int("refs", dec_ctx->refs);
2708  break;
2709 
2710  case AVMEDIA_TYPE_AUDIO:
2711  s = av_get_sample_fmt_name(par->format);
2712  if (s) print_str ("sample_fmt", s);
2713  else print_str_opt("sample_fmt", "unknown");
2714  print_val("sample_rate", par->sample_rate, unit_hertz_str);
2715  print_int("channels", par->channels);
2716 
2717  if (par->channel_layout) {
2718  av_bprint_clear(&pbuf);
2719  av_bprint_channel_layout(&pbuf, par->channels, par->channel_layout);
2720  print_str ("channel_layout", pbuf.str);
2721  } else {
2722  print_str_opt("channel_layout", "unknown");
2723  }
2724 
2725  print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
2726  break;
2727 
2728  case AVMEDIA_TYPE_SUBTITLE:
2729  if (par->width)
2730  print_int("width", par->width);
2731  else
2732  print_str_opt("width", "N/A");
2733  if (par->height)
2734  print_int("height", par->height);
2735  else
2736  print_str_opt("height", "N/A");
2737  break;
2738  }
2739 
2740  if (dec_ctx && dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
2741  const AVOption *opt = NULL;
2742  while ((opt = av_opt_next(dec_ctx->priv_data,opt))) {
2743  uint8_t *str;
2744  if (opt->flags) continue;
2745  if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
2746  print_str(opt->name, str);
2747  av_free(str);
2748  }
2749  }
2750  }
2751 
2752  if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
2753  else print_str_opt("id", "N/A");
2754  print_q("r_frame_rate", stream->r_frame_rate, '/');
2755  print_q("avg_frame_rate", stream->avg_frame_rate, '/');
2756  print_q("time_base", stream->time_base, '/');
2757  print_ts ("start_pts", stream->start_time);
2758  print_time("start_time", stream->start_time, &stream->time_base);
2759  print_ts ("duration_ts", stream->duration);
2760  print_time("duration", stream->duration, &stream->time_base);
2761  if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
2762  else print_str_opt("bit_rate", "N/A");
2763 #if FF_API_LAVF_AVCTX
2764  if (stream->codec->rc_max_rate > 0) print_val ("max_bit_rate", stream->codec->rc_max_rate, unit_bit_per_second_str);
2765  else print_str_opt("max_bit_rate", "N/A");
2766 #endif
2767  if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
2768  else print_str_opt("bits_per_raw_sample", "N/A");
2769  if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
2770  else print_str_opt("nb_frames", "N/A");
2771  if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
2772  else print_str_opt("nb_read_frames", "N/A");
2773  if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
2774  else print_str_opt("nb_read_packets", "N/A");
2775  if (do_show_data)
2776  writer_print_data(w, "extradata", par->extradata,
2777  par->extradata_size);
2778  writer_print_data_hash(w, "extradata_hash", par->extradata,
2779  par->extradata_size);
2780 
2781  /* Print disposition information */
2782 #define PRINT_DISPOSITION(flagname, name) do { \
2783  print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
2784  } while (0)
2785 
2788  PRINT_DISPOSITION(DEFAULT, "default");
2789  PRINT_DISPOSITION(DUB, "dub");
2790  PRINT_DISPOSITION(ORIGINAL, "original");
2791  PRINT_DISPOSITION(COMMENT, "comment");
2792  PRINT_DISPOSITION(LYRICS, "lyrics");
2793  PRINT_DISPOSITION(KARAOKE, "karaoke");
2794  PRINT_DISPOSITION(FORCED, "forced");
2795  PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
2796  PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
2797  PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
2798  PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
2799  PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
2801  }
2802 
2803  if (do_show_stream_tags)
2804  ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
2805 
2806  if (stream->nb_side_data) {
2807  print_pkt_side_data(w, stream->codecpar, stream->side_data, stream->nb_side_data,
2810  }
2811 
2813  av_bprint_finalize(&pbuf, NULL);
2814  fflush(stdout);
2815 
2816  return ret;
2817 }
2818 
2819 static int show_streams(WriterContext *w, InputFile *ifile)
2820 {
2821  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2822  int i, ret = 0;
2823 
2825  for (i = 0; i < ifile->nb_streams; i++)
2826  if (selected_streams[i]) {
2827  ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
2828  if (ret < 0)
2829  break;
2830  }
2832 
2833  return ret;
2834 }
2835 
2836 static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
2837 {
2838  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2839  int i, ret = 0;
2840 
2842  print_int("program_id", program->id);
2843  print_int("program_num", program->program_num);
2844  print_int("nb_streams", program->nb_stream_indexes);
2845  print_int("pmt_pid", program->pmt_pid);
2846  print_int("pcr_pid", program->pcr_pid);
2847  print_ts("start_pts", program->start_time);
2848  print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
2849  print_ts("end_pts", program->end_time);
2850  print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
2852  ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
2853  if (ret < 0)
2854  goto end;
2855 
2857  for (i = 0; i < program->nb_stream_indexes; i++) {
2858  if (selected_streams[program->stream_index[i]]) {
2859  ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
2860  if (ret < 0)
2861  break;
2862  }
2863  }
2865 
2866 end:
2868  return ret;
2869 }
2870 
2871 static int show_programs(WriterContext *w, InputFile *ifile)
2872 {
2873  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2874  int i, ret = 0;
2875 
2877  for (i = 0; i < fmt_ctx->nb_programs; i++) {
2878  AVProgram *program = fmt_ctx->programs[i];
2879  if (!program)
2880  continue;
2881  ret = show_program(w, ifile, program);
2882  if (ret < 0)
2883  break;
2884  }
2886  return ret;
2887 }
2888 
2889 static int show_chapters(WriterContext *w, InputFile *ifile)
2890 {
2891  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2892  int i, ret = 0;
2893 
2895  for (i = 0; i < fmt_ctx->nb_chapters; i++) {
2896  AVChapter *chapter = fmt_ctx->chapters[i];
2897 
2899  print_int("id", chapter->id);
2900  print_q ("time_base", chapter->time_base, '/');
2901  print_int("start", chapter->start);
2902  print_time("start_time", chapter->start, &chapter->time_base);
2903  print_int("end", chapter->end);
2904  print_time("end_time", chapter->end, &chapter->time_base);
2906  ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
2908  }
2910 
2911  return ret;
2912 }
2913 
2914 static int show_format(WriterContext *w, InputFile *ifile)
2915 {
2916  AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2917  char val_str[128];
2918  int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
2919  int ret = 0;
2920 
2922  print_str_validate("filename", fmt_ctx->url);
2923  print_int("nb_streams", fmt_ctx->nb_streams);
2924  print_int("nb_programs", fmt_ctx->nb_programs);
2925  print_str("format_name", fmt_ctx->iformat->name);
2926  if (!do_bitexact) {
2927  if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2928  else print_str_opt("format_long_name", "unknown");
2929  }
2930  print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2931  print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2932  if (size >= 0) print_val ("size", size, unit_byte_str);
2933  else print_str_opt("size", "N/A");
2934  if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2935  else print_str_opt("bit_rate", "N/A");
2936  print_int("probe_score", fmt_ctx->probe_score);
2937  if (do_show_format_tags)
2938  ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2939 
2941  fflush(stdout);
2942  return ret;
2943 }
2944 
2945 static void show_error(WriterContext *w, int err)
2946 {
2947  char errbuf[128];
2948  const char *errbuf_ptr = errbuf;
2949 
2950  if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2951  errbuf_ptr = strerror(AVUNERROR(err));
2952 
2954  print_int("code", err);
2955  print_str("string", errbuf_ptr);
2957 }
2958 
2959 static int open_input_file(InputFile *ifile, const char *filename, const char *print_filename)
2960 {
2961  int err, i;
2962  AVFormatContext *fmt_ctx = NULL;
2963  AVDictionaryEntry *t = NULL;
2964  int scan_all_pmts_set = 0;
2965 
2966  fmt_ctx = avformat_alloc_context();
2967  if (!fmt_ctx) {
2968  print_error(filename, AVERROR(ENOMEM));
2969  exit_program(1);
2970  }
2971 
2972  if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2973  av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2974  scan_all_pmts_set = 1;
2975  }
2976  if ((err = avformat_open_input(&fmt_ctx, filename,
2977  iformat, &format_opts)) < 0) {
2978  print_error(filename, err);
2979  return err;
2980  }
2981  if (print_filename) {
2982  av_freep(&fmt_ctx->url);
2983  fmt_ctx->url = av_strdup(print_filename);
2984  }
2985  ifile->fmt_ctx = fmt_ctx;
2986  if (scan_all_pmts_set)
2987  av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2988  while ((t = av_dict_get(format_opts, "", t, AV_DICT_IGNORE_SUFFIX)))
2989  av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
2990 
2991  if (find_stream_info) {
2992  AVDictionary **opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2993  int orig_nb_streams = fmt_ctx->nb_streams;
2994 
2995  err = avformat_find_stream_info(fmt_ctx, opts);
2996 
2997  for (i = 0; i < orig_nb_streams; i++)
2998  av_dict_free(&opts[i]);
2999  av_freep(&opts);
3000 
3001  if (err < 0) {
3002  print_error(filename, err);
3003  return err;
3004  }
3005  }
3006 
3007  av_dump_format(fmt_ctx, 0, filename, 0);
3008 
3009  ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
3010  sizeof(*ifile->streams));
3011  if (!ifile->streams)
3012  exit(1);
3013  ifile->nb_streams = fmt_ctx->nb_streams;
3014 
3015  /* bind a decoder to each input stream */
3016  for (i = 0; i < fmt_ctx->nb_streams; i++) {
3017  InputStream *ist = &ifile->streams[i];
3018  AVStream *stream = fmt_ctx->streams[i];
3019  AVCodec *codec;
3020 
3021  ist->st = stream;
3022 
3023  if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
3024  av_log(NULL, AV_LOG_WARNING,
3025  "Failed to probe codec for input stream %d\n",
3026  stream->index);
3027  continue;
3028  }
3029 
3030  codec = avcodec_find_decoder(stream->codecpar->codec_id);
3031  if (!codec) {
3032  av_log(NULL, AV_LOG_WARNING,
3033  "Unsupported codec with id %d for input stream %d\n",
3034  stream->codecpar->codec_id, stream->index);
3035  continue;
3036  }
3037  {
3038  AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
3039  fmt_ctx, stream, codec);
3040 
3041  ist->dec_ctx = avcodec_alloc_context3(codec);
3042  if (!ist->dec_ctx)
3043  exit(1);
3044 
3045  err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
3046  if (err < 0)
3047  exit(1);
3048 
3049  if (do_show_log) {
3050  // For loging it is needed to disable at least frame threads as otherwise
3051  // the log information would need to be reordered and matches up to contexts and frames
3052  // That is in fact possible but not trivial
3053  av_dict_set(&codec_opts, "threads", "1", 0);
3054  }
3055 
3056  ist->dec_ctx->pkt_timebase = stream->time_base;
3057  ist->dec_ctx->framerate = stream->avg_frame_rate;
3058 #if FF_API_LAVF_AVCTX
3059  ist->dec_ctx->properties = stream->codec->properties;
3060  ist->dec_ctx->coded_width = stream->codec->coded_width;
3061  ist->dec_ctx->coded_height = stream->codec->coded_height;
3062 #endif
3063 
3064  if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
3065  av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
3066  stream->index);
3067  exit(1);
3068  }
3069 
3070  if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
3071  av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
3072  t->key, stream->index);
3073  return AVERROR_OPTION_NOT_FOUND;
3074  }
3075  }
3076  }
3077 
3078  ifile->fmt_ctx = fmt_ctx;
3079  return 0;
3080 }
3081 
3082 static void close_input_file(InputFile *ifile)
3083 {
3084  int i;
3085 
3086  /* close decoder for each stream */
3087  for (i = 0; i < ifile->nb_streams; i++)
3088  if (ifile->streams[i].st->codecpar->codec_id != AV_CODEC_ID_NONE)
3089  avcodec_free_context(&ifile->streams[i].dec_ctx);
3090 
3091  av_freep(&ifile->streams);
3092  ifile->nb_streams = 0;
3093 
3094  avformat_close_input(&ifile->fmt_ctx);
3095 }
3096 
3097 static int probe_file(WriterContext *wctx, const char *filename,
3098  const char *print_filename)
3099 {
3100  InputFile ifile = { 0 };
3101  int ret, i;
3102  int section_id;
3103 
3106 
3107  ret = open_input_file(&ifile, filename, print_filename);
3108  if (ret < 0)
3109  goto end;
3110 
3111 #define CHECK_END if (ret < 0) goto end
3112 
3113  nb_streams = ifile.fmt_ctx->nb_streams;
3114  REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
3115  REALLOCZ_ARRAY_STREAM(nb_streams_packets,0,ifile.fmt_ctx->nb_streams);
3116  REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
3117 
3118  for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
3119  if (stream_specifier) {
3120  ret = avformat_match_stream_specifier(ifile.fmt_ctx,
3121  ifile.fmt_ctx->streams[i],
3123  CHECK_END;
3124  else
3125  selected_streams[i] = ret;
3126  ret = 0;
3127  } else {
3128  selected_streams[i] = 1;
3129  }
3130  if (!selected_streams[i])
3131  ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
3132  }
3133 
3137  section_id = SECTION_ID_PACKETS_AND_FRAMES;
3138  else if (do_show_packets && !do_show_frames)
3139  section_id = SECTION_ID_PACKETS;
3140  else // (!do_show_packets && do_show_frames)
3141  section_id = SECTION_ID_FRAMES;
3143  writer_print_section_header(wctx, section_id);
3144  ret = read_packets(wctx, &ifile);
3147  CHECK_END;
3148  }
3149 
3150  if (do_show_programs) {
3151  ret = show_programs(wctx, &ifile);
3152  CHECK_END;
3153  }
3154 
3155  if (do_show_streams) {
3156  ret = show_streams(wctx, &ifile);
3157  CHECK_END;
3158  }
3159  if (do_show_chapters) {
3160  ret = show_chapters(wctx, &ifile);
3161  CHECK_END;
3162  }
3163  if (do_show_format) {
3164  ret = show_format(wctx, &ifile);
3165  CHECK_END;
3166  }
3167 
3168 end:
3169  if (ifile.fmt_ctx)
3170  close_input_file(&ifile);
3171  av_freep(&nb_streams_frames);
3172  av_freep(&nb_streams_packets);
3173  av_freep(&selected_streams);
3174 
3175  return ret;
3176 }
3177 
3178 static void show_usage(void)
3179 {
3180  av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3181  av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
3182  av_log(NULL, AV_LOG_INFO, "\n");
3183 }
3184 
3186 {
3187  AVBPrint pbuf;
3188  av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
3189 
3191  print_str("version", FFMPEG_VERSION);
3192  print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3193  program_birth_year, CONFIG_THIS_YEAR);
3194  print_str("compiler_ident", CC_IDENT);
3195  print_str("configuration", FFMPEG_CONFIGURATION);
3197 
3198  av_bprint_finalize(&pbuf, NULL);
3199 }
3200 
3201 #define SHOW_LIB_VERSION(libname, LIBNAME) \
3202  do { \
3203  if (CONFIG_##LIBNAME) { \
3204  unsigned int version = libname##_version(); \
3205  writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
3206  print_str("name", "lib" #libname); \
3207  print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
3208  print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
3209  print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
3210  print_int("version", version); \
3211  print_str("ident", LIB##LIBNAME##_IDENT); \
3212  writer_print_section_footer(w); \
3213  } \
3214  } while (0)
3215 
3217 {
3219  SHOW_LIB_VERSION(avutil, AVUTIL);
3220  SHOW_LIB_VERSION(avcodec, AVCODEC);
3221  SHOW_LIB_VERSION(avformat, AVFORMAT);
3222  SHOW_LIB_VERSION(avdevice, AVDEVICE);
3223  SHOW_LIB_VERSION(avfilter, AVFILTER);
3224  SHOW_LIB_VERSION(swscale, SWSCALE);
3225  SHOW_LIB_VERSION(swresample, SWRESAMPLE);
3227 }
3228 
3229 #define PRINT_PIX_FMT_FLAG(flagname, name) \
3230  do { \
3231  print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
3232  } while (0)
3233 
3235 {
3236  const AVPixFmtDescriptor *pixdesc = NULL;
3237  int i, n;
3238 
3240  while ((pixdesc = av_pix_fmt_desc_next(pixdesc))) {
3242  print_str("name", pixdesc->name);
3243  print_int("nb_components", pixdesc->nb_components);
3244  if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
3245  print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
3246  print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
3247  } else {
3248  print_str_opt("log2_chroma_w", "N/A");
3249  print_str_opt("log2_chroma_h", "N/A");
3250  }
3251  n = av_get_bits_per_pixel(pixdesc);
3252  if (n) print_int ("bits_per_pixel", n);
3253  else print_str_opt("bits_per_pixel", "N/A");
3256  PRINT_PIX_FMT_FLAG(BE, "big_endian");
3257  PRINT_PIX_FMT_FLAG(PAL, "palette");
3258  PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
3259  PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
3260  PRINT_PIX_FMT_FLAG(PLANAR, "planar");
3261  PRINT_PIX_FMT_FLAG(RGB, "rgb");
3262 #if FF_API_PSEUDOPAL
3263  PRINT_PIX_FMT_FLAG(PSEUDOPAL, "pseudopal");
3264 #endif
3265  PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
3267  }
3268  if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
3270  for (i = 0; i < pixdesc->nb_components; i++) {
3272  print_int("index", i + 1);
3273  print_int("bit_depth", pixdesc->comp[i].depth);
3275  }
3277  }
3279  }
3281 }
3282 
3283 static int opt_format(void *optctx, const char *opt, const char *arg)
3284 {
3285  iformat = av_find_input_format(arg);
3286  if (!iformat) {
3287  av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
3288  return AVERROR(EINVAL);
3289  }
3290  return 0;
3291 }
3292 
3293 static inline void mark_section_show_entries(SectionID section_id,
3294  int show_all_entries, AVDictionary *entries)
3295 {
3296  struct section *section = &sections[section_id];
3297 
3299  if (show_all_entries) {
3300  SectionID *id;
3301  for (id = section->children_ids; *id != -1; id++)
3303  } else {
3304  av_dict_copy(&section->entries_to_show, entries, 0);
3305  }
3306 }
3307 
3308 static int match_section(const char *section_name,
3309  int show_all_entries, AVDictionary *entries)
3310 {
3311  int i, ret = 0;
3312 
3313  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
3314  const struct section *section = &sections[i];
3315  if (!strcmp(section_name, section->name) ||
3316  (section->unique_name && !strcmp(section_name, section->unique_name))) {
3317  av_log(NULL, AV_LOG_DEBUG,
3318  "'%s' matches section with unique name '%s'\n", section_name,
3319  (char *)av_x_if_null(section->unique_name, section->name));
3320  ret++;
3322  }
3323  }
3324  return ret;
3325 }
3326 
3327 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
3328 {
3329  const char *p = arg;
3330  int ret = 0;
3331 
3332  while (*p) {
3333  AVDictionary *entries = NULL;
3334  char *section_name = av_get_token(&p, "=:");
3335  int show_all_entries = 0;
3336 
3337  if (!section_name) {
3338  av_log(NULL, AV_LOG_ERROR,
3339  "Missing section name for option '%s'\n", opt);
3340  return AVERROR(EINVAL);
3341  }
3342 
3343  if (*p == '=') {
3344  p++;
3345  while (*p && *p != ':') {
3346  char *entry = av_get_token(&p, ",:");
3347  if (!entry)
3348  break;
3349  av_log(NULL, AV_LOG_VERBOSE,
3350  "Adding '%s' to the entries to show in section '%s'\n",
3351  entry, section_name);
3352  av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
3353  if (*p == ',')
3354  p++;
3355  }
3356  } else {
3357  show_all_entries = 1;
3358  }
3359 
3360  ret = match_section(section_name, show_all_entries, entries);
3361  if (ret == 0) {
3362  av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
3363  ret = AVERROR(EINVAL);
3364  }
3365  av_dict_free(&entries);
3366  av_free(section_name);
3367 
3368  if (ret <= 0)
3369  break;
3370  if (*p)
3371  p++;
3372  }
3373 
3374  return ret;
3375 }
3376 
3377 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
3378 {
3379  char *buf = av_asprintf("format=%s", arg);
3380  int ret;
3381 
3382  if (!buf)
3383  return AVERROR(ENOMEM);
3384 
3385  av_log(NULL, AV_LOG_WARNING,
3386  "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
3387  opt, arg);
3388  ret = opt_show_entries(optctx, opt, buf);
3389  av_free(buf);
3390  return ret;
3391 }
3392 
3393 static void opt_input_file(void *optctx, const char *arg)
3394 {
3395  if (input_filename) {
3396  av_log(NULL, AV_LOG_ERROR,
3397  "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3398  arg, input_filename);
3399  exit_program(1);
3400  }
3401  if (!strcmp(arg, "-"))
3402  arg = "pipe:";
3403  input_filename = arg;
3404 }
3405 
3406 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
3407 {
3408  opt_input_file(optctx, arg);
3409  return 0;
3410 }
3411 
3412 static int opt_print_filename(void *optctx, const char *opt, const char *arg)
3413 {
3414  print_input_filename = arg;
3415  return 0;
3416 }
3417 
3418 void show_help_default_ffprobe(const char *opt, const char *arg)
3419 {
3420  show_usage();
3421  show_help_options(ffprobe_options, "Main options:", 0, 0, 0);
3422  av_log(NULL, AV_LOG_STDERR, "\n");
3423 
3424  show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3425  show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3426 }
3427 
3433 static int parse_read_interval(const char *interval_spec,
3434  ReadInterval *interval)
3435 {
3436  int ret = 0;
3437  char *next, *p, *spec = av_strdup(interval_spec);
3438  if (!spec)
3439  return AVERROR(ENOMEM);
3440 
3441  if (!*spec) {
3442  av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
3443  ret = AVERROR(EINVAL);
3444  goto end;
3445  }
3446 
3447  p = spec;
3448  next = strchr(spec, '%');
3449  if (next)
3450  *next++ = 0;
3451 
3452  /* parse first part */
3453  if (*p) {
3454  interval->has_start = 1;
3455 
3456  if (*p == '+') {
3457  interval->start_is_offset = 1;
3458  p++;
3459  } else {
3460  interval->start_is_offset = 0;
3461  }
3462 
3463  ret = av_parse_time(&interval->start, p, 1);
3464  if (ret < 0) {
3465  av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
3466  goto end;
3467  }
3468  } else {
3469  interval->has_start = 0;
3470  }
3471 
3472  /* parse second part */
3473  p = next;
3474  if (p && *p) {
3475  int64_t us;
3476  interval->has_end = 1;
3477 
3478  if (*p == '+') {
3479  interval->end_is_offset = 1;
3480  p++;
3481  } else {
3482  interval->end_is_offset = 0;
3483  }
3484 
3485  if (interval->end_is_offset && *p == '#') {
3486  long long int lli;
3487  char *tail;
3488  interval->duration_frames = 1;
3489  p++;
3490  lli = strtoll(p, &tail, 10);
3491  if (*tail || lli < 0) {
3492  av_log(NULL, AV_LOG_ERROR,
3493  "Invalid or negative value '%s' for duration number of frames\n", p);
3494  goto end;
3495  }
3496  interval->end = lli;
3497  } else {
3498  interval->duration_frames = 0;
3499  ret = av_parse_time(&us, p, 1);
3500  if (ret < 0) {
3501  av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
3502  goto end;
3503  }
3504  interval->end = us;
3505  }
3506  } else {
3507  interval->has_end = 0;
3508  }
3509 
3510 end:
3511  av_free(spec);
3512  return ret;
3513 }
3514 
3515 static int parse_read_intervals(const char *intervals_spec)
3516 {
3517  int ret, n, i;
3518  char *p, *spec = av_strdup(intervals_spec);
3519  if (!spec)
3520  return AVERROR(ENOMEM);
3521 
3522  /* preparse specification, get number of intervals */
3523  for (n = 0, p = spec; *p; p++)
3524  if (*p == ',')
3525  n++;
3526  n++;
3527 
3528  read_intervals = av_malloc_array(n, sizeof(*read_intervals));
3529  if (!read_intervals) {
3530  ret = AVERROR(ENOMEM);
3531  goto end;
3532  }
3533  read_intervals_nb = n;
3534 
3535  /* parse intervals */
3536  p = spec;
3537  for (i = 0; p; i++) {
3538  char *next;
3539 
3540  av_assert0(i < read_intervals_nb);
3541  next = strchr(p, ',');
3542  if (next)
3543  *next++ = 0;
3544 
3545  read_intervals[i].id = i;
3546  ret = parse_read_interval(p, &read_intervals[i]);
3547  if (ret < 0) {
3548  av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
3549  i, p);
3550  goto end;
3551  }
3552  av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
3553  log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
3554  p = next;
3555  }
3556  av_assert0(i == read_intervals_nb);
3557 
3558 end:
3559  av_free(spec);
3560  return ret;
3561 }
3562 
3563 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
3564 {
3565  return parse_read_intervals(arg);
3566 }
3567 
3568 static int opt_pretty(void *optctx, const char *opt, const char *arg)
3569 {
3570  show_value_unit = 1;
3571  use_value_prefix = 1;
3574  return 0;
3575 }
3576 
3577 static void print_section(SectionID id, int level)
3578 {
3579  const SectionID *pid;
3580  const struct section *section = &sections[id];
3581  av_log(NULL, AV_LOG_STDERR, "%c%c%c",
3582  section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
3583  section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
3585  av_log(NULL, AV_LOG_STDERR, "%*c %s", level * 4, ' ', section->name);
3586  if (section->unique_name)
3587  av_log(NULL, AV_LOG_STDERR, "/%s", section->unique_name);
3588  av_log(NULL, AV_LOG_STDERR, "\n");
3589 
3590  for (pid = section->children_ids; *pid != -1; pid++)
3591  print_section(*pid, level+1);
3592 }
3593 
3594 static int opt_sections(void *optctx, const char *opt, const char *arg)
3595 {
3596  av_log(NULL, AV_LOG_STDERR, "Sections:\n"
3597  "W.. = Section is a wrapper (contains other sections, no local entries)\n"
3598  ".A. = Section contains an array of elements of the same type\n"
3599  "..V = Section may contain a variable number of fields with variable keys\n"
3600  "FLAGS NAME/UNIQUE_NAME\n"
3601  "---\n");
3603  return 0;
3604 }
3605 
3606 static int opt_show_versions(void *optctx, const char *opt, const char *arg)
3607 {
3610  return 0;
3611 }
3612 
3613 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
3614  static int opt_show_##section(void *optctx, const char *opt, const char *arg) \
3615  { \
3616  mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
3617  return 0; \
3618  }
3619 
3620 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
3621 DEFINE_OPT_SHOW_SECTION(error, ERROR)
3622 DEFINE_OPT_SHOW_SECTION(format, FORMAT)
3623 DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
3624 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
3625 DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
3626 DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
3627 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
3628 DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
3629 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
3630 
3631 static inline int check_section_show_entries(int section_id)
3632 {
3633  int *id;
3634  struct section *section = &sections[section_id];
3635  if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
3636  return 1;
3637  for (id = section->children_ids; *id != -1; id++)
3638  if (check_section_show_entries(*id))
3639  return 1;
3640  return 0;
3641 }
3642 
3643 #define SET_DO_SHOW(id, varname) do { \
3644  if (check_section_show_entries(SECTION_ID_##id)) \
3645  do_show_##varname = 1; \
3646  } while (0)
3647 
3650  longjmp_value = 0;
3651 
3652  do_bitexact = 0;
3653  do_count_frames = 0;
3654  do_count_packets = 0;
3655  do_read_frames = 0;
3656  do_read_packets = 0;
3657  do_show_chapters = 0;
3658  do_show_error = 0;
3659  do_show_format = 0;
3660  do_show_frames = 0;
3661  do_show_packets = 0;
3662  do_show_programs = 0;
3663  do_show_streams = 0;
3665  do_show_data = 0;
3671  do_show_log = 0;
3672 
3674  do_show_format_tags = 0;
3675  do_show_frame_tags = 0;
3677  do_show_stream_tags = 0;
3678  do_show_packet_tags = 0;
3679 
3680  show_value_unit = 0;
3681  use_value_prefix = 0;
3684  show_private_data = 1;
3685 
3686  print_format = NULL;
3687  stream_specifier = NULL;
3688  show_data_hash = NULL;
3689 
3690  read_intervals = NULL;
3691  read_intervals_nb = 0;
3692  find_stream_info = 1;
3693 
3694  ffprobe_options = NULL;
3695 
3696  input_filename = NULL;
3697  print_input_filename = NULL;
3698  iformat = NULL;
3699 
3700  hash = NULL;
3701 
3703 
3704  nb_streams = 0;
3705  nb_streams_packets = NULL;
3706  nb_streams_frames = NULL;
3707  selected_streams = NULL;
3708 
3709  log_buffer = NULL;
3710  log_buffer_size = 0;
3711 }
3712 
3713 int ffprobe_execute(int argc, char **argv)
3714 {
3715  char _program_name[] = "ffprobe";
3716  program_name = (char*)&_program_name;
3717  program_birth_year = 2007;
3718 
3719  OptionDef options[] = {
3720  { "L", OPT_EXIT, { .func_arg = show_license }, "show license" },
3721  { "h", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
3722  { "?", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
3723  { "help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
3724  { "-help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
3725  { "version", OPT_EXIT, { .func_arg = show_version }, "show version" },
3726  { "buildconf", OPT_EXIT, { .func_arg = show_buildconf }, "show build configuration" },
3727  { "formats", OPT_EXIT, { .func_arg = show_formats }, "show available formats" },
3728  { "muxers", OPT_EXIT, { .func_arg = show_muxers }, "show available muxers" },
3729  { "demuxers", OPT_EXIT, { .func_arg = show_demuxers }, "show available demuxers" },
3730  { "devices", OPT_EXIT, { .func_arg = show_devices }, "show available devices" },
3731  { "codecs", OPT_EXIT, { .func_arg = show_codecs }, "show available codecs" },
3732  { "decoders", OPT_EXIT, { .func_arg = show_decoders }, "show available decoders" },
3733  { "encoders", OPT_EXIT, { .func_arg = show_encoders }, "show available encoders" },
3734  { "bsfs", OPT_EXIT, { .func_arg = show_bsfs }, "show available bit stream filters" },
3735  { "protocols", OPT_EXIT, { .func_arg = show_protocols }, "show available protocols" },
3736  { "filters", OPT_EXIT, { .func_arg = show_filters }, "show available filters" },
3737  { "pix_fmts", OPT_EXIT, { .func_arg = show_pix_fmts }, "show available pixel formats" },
3738  { "layouts", OPT_EXIT, { .func_arg = show_layouts }, "show standard channel layouts" },
3739  { "sample_fmts", OPT_EXIT, { .func_arg = show_sample_fmts }, "show available audio sample formats" },
3740  { "colors", OPT_EXIT, { .func_arg = show_colors }, "show available color names" },
3741  { "loglevel", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" },
3742  { "v", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" },
3743  { "report", 0, { .func_arg = opt_report }, "generate a report" },
3744  { "max_alloc", HAS_ARG, { .func_arg = opt_max_alloc }, "set maximum size of a single allocated block", "bytes" },
3745  { "cpuflags", HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "force specific cpu flags", "flags" },
3746  { "hide_banner", OPT_BOOL | OPT_EXPERT, {&hide_banner}, "do not show program banner", "hide_banner" },
3747 
3748  #if CONFIG_AVDEVICE
3749  { "sources" , OPT_EXIT | HAS_ARG, { .func_arg = show_sources },
3750  "list sources of the input device", "device" },
3751  { "sinks" , OPT_EXIT | HAS_ARG, { .func_arg = show_sinks },
3752  "list sinks of the output device", "device" },
3753  #endif
3754 
3755  { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
3756  { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
3757  { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
3758  { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
3759  "use binary prefixes for byte units" },
3760  { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
3761  "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
3762  { "pretty", 0, {.func_arg = opt_pretty},
3763  "prettify the format of displayed values, make it more human readable" },
3764  { "print_format", OPT_STRING | HAS_ARG, { &print_format },
3765  "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
3766  { "of", OPT_STRING | HAS_ARG, { &print_format }, "alias for -print_format", "format" },
3767  { "select_streams", OPT_STRING | HAS_ARG, { &stream_specifier }, "select the specified streams", "stream_specifier" },
3768  { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
3769  { "show_data", OPT_BOOL, { &do_show_data }, "show packets data" },
3770  { "show_data_hash", OPT_STRING | HAS_ARG, { &show_data_hash }, "show packets data hash" },
3771  { "show_error", 0, { .func_arg = &opt_show_error }, "show probing error" },
3772  { "show_format", 0, { .func_arg = &opt_show_format }, "show format/container info" },
3773  { "show_frames", 0, { .func_arg = &opt_show_frames }, "show frames info" },
3774  { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
3775  "show a particular entry from the format/container info", "entry" },
3776  { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
3777  "show a set of specified entries", "entry_list" },
3778  #if HAVE_THREADS
3779  { "show_log", OPT_INT|HAS_ARG, { &do_show_log }, "show log" },
3780  #endif
3781  { "show_packets", 0, { .func_arg = &opt_show_packets }, "show packets info" },
3782  { "show_programs", 0, { .func_arg = &opt_show_programs }, "show programs info" },
3783  { "show_streams", 0, { .func_arg = &opt_show_streams }, "show streams info" },
3784  { "show_chapters", 0, { .func_arg = &opt_show_chapters }, "show chapters info" },
3785  { "count_frames", OPT_BOOL, { &do_count_frames }, "count the number of frames per stream" },
3786  { "count_packets", OPT_BOOL, { &do_count_packets }, "count the number of packets per stream" },
3787  { "show_program_version", 0, { .func_arg = &opt_show_program_version }, "show ffprobe version" },
3788  { "show_library_versions", 0, { .func_arg = &opt_show_library_versions }, "show library versions" },
3789  { "show_versions", 0, { .func_arg = &opt_show_versions }, "show program and library versions" },
3790  { "show_pixel_formats", 0, { .func_arg = &opt_show_pixel_formats }, "show pixel format descriptions" },
3791  { "show_private_data", OPT_BOOL, { &show_private_data }, "show private data" },
3792  { "private", OPT_BOOL, { &show_private_data }, "same as show_private_data" },
3793  { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
3794  { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
3795  { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
3796  { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
3797  { "print_filename", HAS_ARG, {.func_arg = opt_print_filename}, "override the printed input filename", "print_file"},
3798  { "find_stream_info", OPT_BOOL | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
3799  "read and decode the streams to fill missing information with heuristics" },
3800  { NULL, },
3801  };
3802 
3803  const Writer *w;
3804  WriterContext *wctx;
3805  char *buf;
3806  char *w_name = NULL, *w_args = NULL;
3807  int ret, i;
3808 
3809  int savedCode = setjmp(ex_buf__);
3810  if (savedCode == 0) {
3811 
3813 
3814  init_dynload();
3815 
3816  #if HAVE_THREADS
3817  ret = pthread_mutex_init(&log_mutex, NULL);
3818  if (ret != 0) {
3819  goto end;
3820  }
3821  #endif
3822  av_log_set_flags(AV_LOG_SKIP_REPEATED);
3824 
3825  ffprobe_options = options;
3826  parse_loglevel(argc, argv, options);
3827  avformat_network_init();
3828  init_opts();
3829  #if CONFIG_AVDEVICE
3830  avdevice_register_all();
3831  #endif
3832 
3833  show_banner(argc, argv, options);
3834  parse_options(NULL, argc, argv, options, opt_input_file);
3835 
3836  if (do_show_log)
3837  av_log_set_callback(log_callback);
3838 
3839  /* mark things to show, based on -show_entries */
3840  SET_DO_SHOW(CHAPTERS, chapters);
3841  SET_DO_SHOW(ERROR, error);
3842  SET_DO_SHOW(FORMAT, format);
3843  SET_DO_SHOW(FRAMES, frames);
3844  SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
3845  SET_DO_SHOW(PACKETS, packets);
3846  SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
3847  SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
3848  SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
3849  SET_DO_SHOW(PROGRAM_VERSION, program_version);
3850  SET_DO_SHOW(PROGRAMS, programs);
3851  SET_DO_SHOW(STREAMS, streams);
3852  SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
3853  SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
3854 
3855  SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
3856  SET_DO_SHOW(FORMAT_TAGS, format_tags);
3857  SET_DO_SHOW(FRAME_TAGS, frame_tags);
3858  SET_DO_SHOW(PROGRAM_TAGS, program_tags);
3859  SET_DO_SHOW(STREAM_TAGS, stream_tags);
3860  SET_DO_SHOW(PROGRAM_STREAM_TAGS, stream_tags);
3861  SET_DO_SHOW(PACKET_TAGS, packet_tags);
3862 
3864  av_log(NULL, AV_LOG_ERROR,
3865  "-bitexact and -show_program_version or -show_library_versions "
3866  "options are incompatible\n");
3867  ret = AVERROR(EINVAL);
3868  goto end;
3869  }
3870 
3872 
3873  if (!print_format)
3874  print_format = av_strdup("default");
3875  if (!print_format) {
3876  ret = AVERROR(ENOMEM);
3877  goto end;
3878  }
3879  w_name = av_strtok(print_format, "=", &buf);
3880  if (!w_name) {
3881  av_log(NULL, AV_LOG_ERROR,
3882  "No name specified for the output format\n");
3883  ret = AVERROR(EINVAL);
3884  goto end;
3885  }
3886  w_args = buf;
3887 
3888  if (show_data_hash) {
3889  if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
3890  if (ret == AVERROR(EINVAL)) {
3891  const char *n;
3892  av_log(NULL, AV_LOG_ERROR,
3893  "Unknown hash algorithm '%s'\nKnown algorithms:",
3894  show_data_hash);
3895  for (i = 0; (n = av_hash_names(i)); i++)
3896  av_log(NULL, AV_LOG_ERROR, " %s", n);
3897  av_log(NULL, AV_LOG_ERROR, "\n");
3898  }
3899  goto end;
3900  }
3901  }
3902 
3903  w = writer_get_by_name(w_name);
3904  if (!w) {
3905  av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
3906  ret = AVERROR(EINVAL);
3907  goto end;
3908  }
3909 
3910  if ((ret = writer_open(&wctx, w, w_args,
3911  sections, FF_ARRAY_ELEMS(sections))) >= 0) {
3912  if (w == &xml_writer)
3913  wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
3914 
3916 
3923 
3924  if (!input_filename &&
3927  show_usage();
3928  av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
3929  av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
3930  ret = AVERROR(EINVAL);
3931  } else if (input_filename) {
3933  if (ret < 0 && do_show_error)
3934  show_error(wctx, ret);
3935  }
3936 
3938  writer_close(&wctx);
3939  }
3940 
3941  main_ffprobe_return_code = ret < 0;
3942 
3943  } else {
3945  }
3946 
3947 end:
3948  av_freep(&print_format);
3949  av_freep(&read_intervals);
3950  av_hash_freep(&hash);
3951 
3952  uninit_opts();
3953  for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
3954  av_dict_free(&(sections[i].entries_to_show));
3955 
3956  avformat_network_deinit();
3957 
3958  return main_ffprobe_return_code;
3959 }
__thread jmp_buf ex_buf__
void exit_program(int ret)
int show_decoders(void *optctx, const char *opt, const char *arg)
int opt_loglevel(void *optctx, const char *opt, const char *arg)
void show_help_children(const AVClass *class, int flags)
__thread AVDictionary * codec_opts
int opt_cpuflags(void *optctx, const char *opt, const char *arg)
void init_dynload(void)
int show_help(void *optctx, const char *opt, const char *arg)
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
__thread AVDictionary * format_opts
int opt_default(void *optctx, const char *opt, const char *arg)
void print_error(const char *filename, int err)
int show_filters(void *optctx, const char *opt, const char *arg)
int show_sample_fmts(void *optctx, const char *opt, const char *arg)
int show_muxers(void *optctx, const char *opt, const char *arg)
int show_bsfs(void *optctx, const char *opt, const char *arg)
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
__thread char * program_name
int show_layouts(void *optctx, const char *opt, const char *arg)
int show_encoders(void *optctx, const char *opt, const char *arg)
int show_version(void *optctx, const char *opt, const char *arg)
void parse_loglevel(int argc, char **argv, const OptionDef *options)
__thread int program_birth_year
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *, const char *))
void show_banner(int argc, char **argv, const OptionDef *options)
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, AVCodec *codec)
int show_license(void *optctx, const char *opt, const char *arg)
int show_codecs(void *optctx, const char *opt, const char *arg)
int show_buildconf(void *optctx, const char *opt, const char *arg)
void register_exit(void(*cb)(int ret))
int show_devices(void *optctx, const char *opt, const char *arg)
void uninit_opts(void)
int show_formats(void *optctx, const char *opt, const char *arg)
__thread int hide_banner
int show_protocols(void *optctx, const char *opt, const char *arg)
int opt_max_alloc(void *optctx, const char *opt, const char *arg)
int opt_report(void *optctx, const char *opt, const char *arg)
void init_opts(void)
int show_colors(void *optctx, const char *opt, const char *arg)
int show_pix_fmts(void *optctx, const char *opt, const char *arg)
int show_demuxers(void *optctx, const char *opt, const char *arg)
#define OPT_VIDEO
#define OPT_BOOL
#define OPT_INT
#define AV_LOG_STDERR
#define OPT_INPUT
#define OPT_STRING
__thread int find_stream_info
#define OPT_AUDIO
#define OPT_EXPERT
#define OPT_EXIT
#define HAS_ARG
static void show_usage(void)
__thread int read_intervals_nb
static int writer_open(WriterContext **wctx, const Writer *writer, const char *args, const struct section *sections, int nb_sections)
static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
struct InputFile InputFile
__thread int do_show_library_versions
SectionID
@ SECTION_ID_FRAME_SIDE_DATA
@ SECTION_ID_STREAM_TAGS
@ SECTION_ID_PROGRAM_STREAM
@ SECTION_ID_PACKET
@ SECTION_ID_PROGRAM_VERSION
@ SECTION_ID_PIXEL_FORMAT_FLAGS
@ SECTION_ID_PROGRAM_TAGS
@ SECTION_ID_PIXEL_FORMATS
@ SECTION_ID_STREAM
@ SECTION_ID_LIBRARY_VERSION
@ SECTION_ID_NONE
@ SECTION_ID_FRAMES
@ SECTION_ID_STREAM_SIDE_DATA
@ SECTION_ID_PACKETS_AND_FRAMES
@ SECTION_ID_PACKET_SIDE_DATA_LIST
@ SECTION_ID_PROGRAM_STREAM_DISPOSITION
@ SECTION_ID_FRAME_SIDE_DATA_TIMECODE_LIST
@ SECTION_ID_PACKETS
@ SECTION_ID_FRAME_TAGS
@ SECTION_ID_LIBRARY_VERSIONS
@ SECTION_ID_STREAM_SIDE_DATA_LIST
@ SECTION_ID_PROGRAMS
@ SECTION_ID_PROGRAM_STREAM_TAGS
@ SECTION_ID_FORMAT_TAGS
@ SECTION_ID_CHAPTERS
@ SECTION_ID_FRAME_SIDE_DATA_TIMECODE
@ SECTION_ID_FRAME_LOGS
@ SECTION_ID_PIXEL_FORMAT_COMPONENTS
@ SECTION_ID_FORMAT
@ SECTION_ID_PACKET_TAGS
@ SECTION_ID_PIXEL_FORMAT_COMPONENT
@ SECTION_ID_SUBTITLE
@ SECTION_ID_PIXEL_FORMAT
@ SECTION_ID_FRAME_SIDE_DATA_LIST
@ SECTION_ID_STREAMS
@ SECTION_ID_CHAPTER_TAGS
@ SECTION_ID_ERROR
@ SECTION_ID_FRAME_LOG
@ SECTION_ID_FRAME
@ SECTION_ID_PACKET_SIDE_DATA
@ SECTION_ID_STREAM_DISPOSITION
@ SECTION_ID_ROOT
@ SECTION_ID_CHAPTER
@ SECTION_ID_PROGRAM_STREAMS
@ SECTION_ID_PROGRAM
#define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS
static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
__thread int do_count_frames
static const AVOption ini_options[]
__thread int do_show_program_version
#define CHECK_END
static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
__thread int do_show_streams
static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
#define SHOW_LIB_VERSION(libname, LIBNAME)
__thread int do_show_frame_tags
__thread int use_byte_value_binary_prefix
#define CHECK_COMPLIANCE(opt, opt_name)
#define print_ts(k, v)
__thread OptionDef * ffprobe_options
static av_cold int json_init(WriterContext *wctx)
#define print_int(k, v)
static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream, AVFormatContext *fmt_ctx)
#define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER
static const char * json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
static const char unit_second_str[]
static int read_packets(WriterContext *w, InputFile *ifile)
#define SECTION_FLAG_IS_WRAPPER
the section only contains other sections, but has no data at its own level
static const char unit_byte_str[]
struct DefaultContext DefaultContext
static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
static void ffprobe_show_library_versions(WriterContext *w)
static const Writer flat_writer
static void default_print_section_header(WriterContext *wctx)
static int read_interval_packets(WriterContext *w, InputFile *ifile, const ReadInterval *interval, int64_t *cur_ts)
__thread volatile int longjmp_value
static void json_print_section_footer(WriterContext *wctx)
#define SECTION_MAX_NB_LEVELS
__thread int do_show_stream_tags
static int opt_format(void *optctx, const char *opt, const char *arg)
static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream, AVFormatContext *fmt_ctx)
static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
#define SECTION_MAX_NB_CHILDREN
#define SECTION_FLAG_IS_ARRAY
the section contains an array of elements of the same type
__thread int do_show_data
static const AVOption default_options[]
static int validate_string(WriterContext *wctx, char **dstp, const char *src)
static void print_color_range(WriterContext *w, enum AVColorRange color_range)
__thread volatile int main_ffprobe_return_code
#define pthread_mutex_lock(a)
static struct section sections[]
static av_always_inline int process_frame(WriterContext *w, InputFile *ifile, AVFrame *frame, AVPacket *pkt, int *packet_new)
static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
static char * value_string(char *buf, int buf_size, struct unit_value uv)
static void writer_print_integers(WriterContext *wctx, const char *name, uint8_t *data, int size, const char *format, int columns, int bytes, int offset_add)
__thread char * show_data_hash
__thread int do_show_chapter_tags
static void compact_print_section_footer(WriterContext *wctx)
__thread int do_show_frames
static const AVOption xml_options[]
#define XML_INDENT()
__thread int log_buffer_size
int ffprobe_execute(int argc, char **argv)
static int parse_read_interval(const char *interval_spec, ReadInterval *interval)
static void writer_print_section_footer(WriterContext *wctx)
static const AVOption csv_options[]
static const Writer compact_writer
static av_cold int xml_init(WriterContext *wctx)
__thread char * stream_specifier
static const char unit_hertz_str[]
static const char * none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
static int show_streams(WriterContext *w, InputFile *ifile)
#define print_section_header(s)
static void show_error(WriterContext *w, int err)
static char * upcase_string(char *dst, size_t dst_size, const char *src)
static int opt_pretty(void *optctx, const char *opt, const char *arg)
__thread ReadInterval * read_intervals
#define print_str_validate(k, v)
static int probe_file(WriterContext *wctx, const char *filename, const char *print_filename)
static const char * c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
struct XMLContext XMLContext
__thread const char * input_filename
void ffprobe_var_cleanup()
static void json_print_item_str(WriterContext *wctx, const char *key, const char *value)
#define SECTION_FLAG_HAS_VARIABLE_FIELDS
static void compact_print_section_header(WriterContext *wctx)
static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
static const AVOption compact_options[]
__thread int do_show_log
double dec_val
__thread int next_registered_writer_idx
static void ini_print_section_header(WriterContext *wctx)
static void json_print_int(WriterContext *wctx, const char *key, long long int value)
static void flat_print_section_header(WriterContext *wctx)
__thread int * selected_streams
static const char unit_bit_per_second_str[]
StringValidation
@ WRITER_STRING_VALIDATION_NB
@ WRITER_STRING_VALIDATION_REPLACE
@ WRITER_STRING_VALIDATION_IGNORE
@ WRITER_STRING_VALIDATION_FAIL
__thread int do_show_program_tags
__thread int do_count_packets
#define print_str(k, v)
__thread int do_read_frames
static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
#define print_duration_ts(k, v)
static void writer_print_data(WriterContext *wctx, const char *name, uint8_t *data, int size)
static av_cold int flat_init(WriterContext *wctx)
static void writer_print_integer(WriterContext *wctx, const char *key, long long int val)
static void xml_print_section_footer(WriterContext *wctx)
#define DEFINE_WRITER_CLASS(name)
__thread struct AVHashContext * hash
#define PRINT_STRING_OPT
static const Writer csv_writer
static const char * xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
const char * dec_str
static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
struct JSONContext JSONContext
struct LogBuffer LogBuffer
#define MAX_REGISTERED_WRITERS_NB
__thread int do_show_error
static const char * csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
#define print_str_opt(k, v)
static void print_pkt_side_data(WriterContext *w, AVCodecParameters *par, const AVPacketSideData *side_data, int nb_side_data, SectionID id_data_list, SectionID id_data)
static void default_print_int(WriterContext *wctx, const char *key, long long int value)
static void default_print_section_footer(WriterContext *wctx)
__thread int do_bitexact
static void ffprobe_cleanup(int ret)
#define print_val(k, v, u)
__thread int do_show_format_tags
static void xml_print_section_header(WriterContext *wctx)
__thread int show_private_data
static int show_format(WriterContext *w, InputFile *ifile)
static int writer_print_string(WriterContext *wctx, const char *key, const char *val, int flags)
__thread LogBuffer * log_buffer
__thread const Writer * registered_writers[MAX_REGISTERED_WRITERS_NB+1]
static void default_print_str(WriterContext *wctx, const char *key, const char *value)
#define DEFINE_OPT_SHOW_SECTION(section, target_section_id)
static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
static int check_section_show_entries(int section_id)
static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
struct ReadInterval ReadInterval
static const AVOption writer_options[]
#define SET_DO_SHOW(id, varname)
#define print_q(k, v, s)
static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
static const Writer ini_writer
static void writer_register_all(void)
static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
static int match_section(const char *section_name, int show_all_entries, AVDictionary *entries)
static int opt_show_entries(void *optctx, const char *opt, const char *arg)
__thread uint64_t * nb_streams_frames
static void writer_print_rational(WriterContext *wctx, const char *key, AVRational q, char sep)
static void * writer_child_next(void *obj, void *prev)
__thread int do_show_packet_tags
static void writer_close(WriterContext **wctx)
__thread int show_value_unit
static const struct @3 si_prefixes[]
static void close_input_file(InputFile *ifile)
static void print_section(SectionID id, int level)
struct FlatContext FlatContext
#define pthread_mutex_unlock(a)
static void ffprobe_show_program_version(WriterContext *w)
static void writer_print_data_hash(WriterContext *wctx, const char *name, uint8_t *data, int size)
static const char * flat_escape_value_str(AVBPrint *dst, const char *src)
static void json_print_str(WriterContext *wctx, const char *key, const char *value)
static int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
static int opt_print_filename(void *optctx, const char *opt, const char *arg)
static const Writer json_writer
__thread const char * print_input_filename
static av_cold int compact_init(WriterContext *wctx)
static const char * flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
__thread int do_show_chapters
#define print_duration_time(k, v, tb)
__thread int nb_streams
const char * bin_str
static void opt_input_file(void *optctx, const char *arg)
double bin_val
static const Writer * writer_get_by_name(const char *name)
__thread int do_show_programs
struct CompactContext CompactContext
static const AVOption json_options[]
static const AVOption flat_options[]
void show_help_default_ffprobe(const char *opt, const char *arg)
static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
__thread int use_value_sexagesimal_format
__thread int do_show_pixel_format_flags
#define OFFSET(x)
static void ffprobe_show_pixel_formats(WriterContext *w)
__thread uint64_t * nb_streams_packets
__thread int do_read_packets
__thread int use_value_prefix
static char * ini_escape_str(AVBPrint *dst, const char *src)
#define print_section_footer(s)
static void writer_print_section_header(WriterContext *wctx, int section_id)
static const char * writer_get_name(void *p)
__thread int do_show_pixel_formats
static int show_programs(WriterContext *w, InputFile *ifile)
__thread char * print_format
static int open_input_file(InputFile *ifile, const char *filename, const char *print_filename)
static void writer_print_time(WriterContext *wctx, const char *key, int64_t ts, const AVRational *time_base, int is_duration)
__thread int do_show_packets
static void mark_section_show_entries(SectionID section_id, int show_all_entries, AVDictionary *entries)
#define PRINT_STRING_VALIDATE
static void json_print_section_header(WriterContext *wctx)
static int writer_register(const Writer *writer)
#define print_time(k, v, tb)
__thread int do_show_pixel_format_components
static int opt_sections(void *optctx, const char *opt, const char *arg)
static const Writer default_writer
__thread int do_show_format
#define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n)
#define PRINT_DISPOSITION(flagname, name)
static const AVClass writer_class
__thread int do_show_stream_disposition
static void clear_log(int need_lock)
static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
struct INIContext INIContext
static int parse_read_intervals(const char *intervals_spec)
#define PRINT_PIX_FMT_FLAG(flagname, name)
struct InputStream InputStream
struct Writer Writer
__thread AVInputFormat * iformat
static int show_chapters(WriterContext *w, InputFile *ifile)
static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
static Writer xml_writer
static int opt_show_versions(void *optctx, const char *opt, const char *arg)
#define JSON_INDENT()
static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
#define print_fmt(k, f,...)
static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
int terminate_line[SECTION_MAX_NB_LEVELS]
int has_nested_elems[SECTION_MAX_NB_LEVELS]
int nested_section[SECTION_MAX_NB_LEVELS]
const char *(* escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
int nested_section[SECTION_MAX_NB_LEVELS]
const char * sep_str
InputStream * streams
AVFormatContext * fmt_ctx
AVCodecContext * dec_ctx
AVStream * st
const char * item_sep
const char * item_start_end
char * context_name
AVClassCategory parent_category
AVClassCategory category
char * log_message
char * parent_name
int64_t end
start, end in second/AV_TIME_BASE units
int id
identifier
unsigned int nb_section_frame
number of the frame section in case we are in "packets_and_frames" section
unsigned int nb_item[SECTION_MAX_NB_LEVELS]
const struct section * section[SECTION_MAX_NB_LEVELS]
char * string_validation_replacement
char * name
name of this writer instance
void * priv
private data for use by the filter
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]
unsigned int nb_section_packet_frame
nb_section_packet or nb_section_frame according if is_packets_and_frames
int level
current level, starting from 0
unsigned int nb_section_packet
number of the packet section in case we are in "packets_and_frames" section
int nb_sections
number of sections
const struct section * sections
array containing all sections
unsigned int string_validation_utf8_flags
const Writer * writer
the Writer of which this is an instance
void(* print_string)(WriterContext *wctx, const char *, const char *)
int flags
a combination or WRITER_FLAG_*
int priv_size
private size for the writer context
const AVClass * priv_class
private class of the writer, if any
void(* print_integer)(WriterContext *wctx, const char *, long long int)
void(* print_rational)(WriterContext *wctx, AVRational *q, char *sep)
const char * name
int(* init)(WriterContext *wctx)
void(* print_section_header)(WriterContext *wctx)
void(* print_section_footer)(WriterContext *wctx)
void(* uninit)(WriterContext *wctx)
int show_all_entries
AVDictionary * entries_to_show
const char * name
const char * element_name
name of the contained element, if provided
int id
unique id identifying a section
int flags
For these sections the element_name field is mandatory.
const char * unique_name
unique section name, in case the name is ambiguous
int children_ids[SECTION_MAX_NB_CHILDREN+1]
list of children section IDS, terminated by -1
const char * unit
union unit_value::@4 val
long long int i