FFmpegKit iOS / macOS / tvOS API 5.1
fftools_ffprobe.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2010 Stefano Sabatini
3 * Copyright (c) 2020 Taner Sener ( tanersener gmail com )
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
27/*
28 * This file is the modified version of ffprobe.c file living in ffmpeg source code under the fftools folder. We
29 * manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
30 * by us to develop mobile-ffmpeg and later ffmpeg-kit libraries.
31 *
32 * mobile-ffmpeg / ffmpeg-kit changes by Taner Sener
33 *
34 * 09.2022
35 * --------------------------------------------------------
36 * - implemented writer_printf_printf using av_vlog
37 * - printf statements replaced with av_log
38 * - volatile dropped from thread local variables
39 *
40 * 01.2020
41 * --------------------------------------------------------
42 * - ffprobe support (volatile added to critical variables, options updated as ffprobe_options, printf
43 * statements replaced with av_log, dropped static variables in methods, show_help_default renamed as
44 * show_help_default_ffprobe, implemented exit_program with longjmp, OptionDef defines combined)
45 * - concurrent execution support ("__thread" specifier added to variables used by multiple threads,
46 * static keyword removed from methods called by both ffmpeg and ffprobe)
47 * - main() function renamed as ffprobe_execute()
48 */
49
50#include "config.h"
51#include "libavutil/ffversion.h"
52
53#include <string.h>
54
55#include "libavformat/avformat.h"
56#include "libavcodec/avcodec.h"
57#include "libavutil/avassert.h"
58#include "libavutil/avstring.h"
59#include "libavutil/bprint.h"
60#include "libavutil/channel_layout.h"
61#include "libavutil/display.h"
62#include "libavutil/hash.h"
63#include "libavutil/hdr_dynamic_metadata.h"
64#include "libavutil/mastering_display_metadata.h"
65#include "libavutil/hdr_dynamic_vivid_metadata.h"
66#include "libavutil/dovi_meta.h"
67#include "libavutil/opt.h"
68#include "libavutil/pixdesc.h"
69#include "libavutil/spherical.h"
70#include "libavutil/stereo3d.h"
71#include "libavutil/dict.h"
72#include "libavutil/intreadwrite.h"
73#include "libavutil/libm.h"
74#include "libavutil/parseutils.h"
75#include "libavutil/timecode.h"
76#include "libavutil/timestamp.h"
77#include "libavdevice/avdevice.h"
78#include "libswscale/swscale.h"
79#include "libswresample/swresample.h"
80#include "fftools_cmdutils.h"
81#include "fftools_opt_common.h"
82
83#include "libavutil/thread.h"
84
85#include "ffmpegkit_exception.h"
86
87#if !HAVE_THREADS
88# ifdef pthread_mutex_lock
89# undef pthread_mutex_lock
90# endif
91# define pthread_mutex_lock(a) do{}while(0)
92# ifdef pthread_mutex_unlock
93# undef pthread_mutex_unlock
94# endif
95# define pthread_mutex_unlock(a) do{}while(0)
96#endif
97
98typedef struct InputStream {
99 AVStream *st;
100
101 AVCodecContext *dec_ctx;
103
104typedef struct InputFile {
105 AVFormatContext *fmt_ctx;
106
108 int nb_streams;
110
111__thread int do_bitexact = 0;
112__thread int do_count_frames = 0;
113__thread int do_count_packets = 0;
114__thread int do_read_frames = 0;
115__thread int do_read_packets = 0;
116__thread int do_show_chapters = 0;
117__thread int do_show_error = 0;
118__thread int do_show_format = 0;
119__thread int do_show_frames = 0;
120__thread int do_show_packets = 0;
121__thread int do_show_programs = 0;
122__thread int do_show_streams = 0;
124__thread int do_show_data = 0;
125__thread int do_show_program_version = 0;
127__thread int do_show_pixel_formats = 0;
130__thread int do_show_log = 0;
131
132__thread int do_show_chapter_tags = 0;
133__thread int do_show_format_tags = 0;
134__thread int do_show_frame_tags = 0;
135__thread int do_show_program_tags = 0;
136__thread int do_show_stream_tags = 0;
137__thread int do_show_packet_tags = 0;
138
139__thread int show_value_unit = 0;
140__thread int use_value_prefix = 0;
143__thread int show_private_data = 1;
144
145#define SHOW_OPTIONAL_FIELDS_AUTO -1
146#define SHOW_OPTIONAL_FIELDS_NEVER 0
147#define SHOW_OPTIONAL_FIELDS_ALWAYS 1
149
150__thread char *print_format;
151__thread char *stream_specifier;
152__thread char *show_data_hash;
153
154typedef struct ReadInterval {
155 int id;
156 int64_t start, end;
161
163__thread int read_intervals_nb = 0;
164
165/* section structure definition */
166
167#define SECTION_MAX_NB_CHILDREN 10
168
169struct section {
170 int id;
171 const char *name;
172
173#define SECTION_FLAG_IS_WRAPPER 1
174#define SECTION_FLAG_IS_ARRAY 2
175#define SECTION_FLAG_HAS_VARIABLE_FIELDS 4
177 int flags;
179 const char *element_name;
180 const char *unique_name;
181 AVDictionary *entries_to_show;
183};
184
185typedef enum {
235} SectionID;
236
237__thread static struct section sections[] = {
240 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
241 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
242 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
243 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
246 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
247 [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" },
256 [SECTION_ID_FRAME_LOG] = { SECTION_ID_FRAME_LOG, "log", 0, { -1 }, },
258 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
262 [SECTION_ID_PACKET_TAGS] = { SECTION_ID_PACKET_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "packet_tags" },
263 [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" },
264 [SECTION_ID_PACKET_SIDE_DATA] = { SECTION_ID_PACKET_SIDE_DATA, "side_data", 0, { -1 }, .unique_name = "packet_side_data" },
267 [SECTION_ID_PIXEL_FORMAT_FLAGS] = { SECTION_ID_PIXEL_FORMAT_FLAGS, "flags", 0, { -1 }, .unique_name = "pixel_format_flags" },
268 [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" },
270 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
271 [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" },
273 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
275 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
276 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
284 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
285 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
286 [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" },
287 [SECTION_ID_STREAM_SIDE_DATA] = { SECTION_ID_STREAM_SIDE_DATA, "side_data", 0, { -1 }, .unique_name = "stream_side_data" },
288 [SECTION_ID_SUBTITLE] = { SECTION_ID_SUBTITLE, "subtitle", 0, { -1 } },
289};
290
291__thread OptionDef *ffprobe_options = NULL;
292
293/* FFprobe context */
294__thread const char *input_filename;
295__thread const char *print_input_filename;
296__thread const AVInputFormat *iformat = NULL;
297__thread const char *output_filename = NULL;
298
299__thread struct AVHashContext *hash;
300
302extern __thread int longjmp_value;
303
304static const struct {
305 double bin_val;
306 double dec_val;
307 const char *bin_str;
308 const char *dec_str;
309} si_prefixes[] = {
310 { 1.0, 1.0, "", "" },
311 { 1.024e3, 1e3, "Ki", "K" },
312 { 1.048576e6, 1e6, "Mi", "M" },
313 { 1.073741824e9, 1e9, "Gi", "G" },
314 { 1.099511627776e12, 1e12, "Ti", "T" },
315 { 1.125899906842624e15, 1e15, "Pi", "P" },
317
318static const char unit_second_str[] = "s" ;
319static const char unit_hertz_str[] = "Hz" ;
320static const char unit_byte_str[] = "byte" ;
321static const char unit_bit_per_second_str[] = "bit/s";
322
323__thread int nb_streams;
324__thread uint64_t *nb_streams_packets;
325__thread uint64_t *nb_streams_frames;
326__thread int *selected_streams;
327
328#if HAVE_THREADS
329__thread pthread_mutex_t log_mutex;
330#endif
331typedef struct LogBuffer {
335 AVClassCategory category;
337 AVClassCategory parent_category;
339
341__thread int log_buffer_size;
342
343static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
344{
345 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
346 va_list vl2;
347 char line[1024];
348 static int print_prefix = 1;
349 void *new_log_buffer;
350
351 va_copy(vl2, vl);
352 av_log_default_callback(ptr, level, fmt, vl);
353 av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
354 va_end(vl2);
355
356#if HAVE_THREADS
357 pthread_mutex_lock(&log_mutex);
358
359 new_log_buffer = av_realloc_array(log_buffer, log_buffer_size + 1, sizeof(*log_buffer));
360 if (new_log_buffer) {
361 char *msg;
362 int i;
363
364 log_buffer = new_log_buffer;
366 log_buffer[log_buffer_size].context_name= avc ? av_strdup(avc->item_name(ptr)) : NULL;
367 if (avc) {
368 if (avc->get_category) log_buffer[log_buffer_size].category = avc->get_category(ptr);
369 else log_buffer[log_buffer_size].category = avc->category;
370 }
372 msg = log_buffer[log_buffer_size].log_message = av_strdup(line);
373 for (i=strlen(msg) - 1; i>=0 && msg[i] == '\n'; i--) {
374 msg[i] = 0;
375 }
376 if (avc && avc->parent_log_context_offset) {
377 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
378 avc->parent_log_context_offset);
379 if (parent && *parent) {
380 log_buffer[log_buffer_size].parent_name = av_strdup((*parent)->item_name(parent));
382 (*parent)->get_category ? (*parent)->get_category(parent) :(*parent)->category;
383 }
384 }
386 }
387
388 pthread_mutex_unlock(&log_mutex);
389#endif
390}
391
392static void ffprobe_cleanup(int ret)
393{
394 int i;
395 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
396 av_dict_free(&(sections[i].entries_to_show));
397
398#if HAVE_THREADS
399 pthread_mutex_destroy(&log_mutex);
400#endif
401}
402
404 union { double d; long long int i; } val;
405 const char *unit;
406};
407
408static char *value_string(char *buf, int buf_size, struct unit_value uv)
409{
410 double vald;
411 long long int vali;
412 int show_float = 0;
413
414 if (uv.unit == unit_second_str) {
415 vald = uv.val.d;
416 show_float = 1;
417 } else {
418 vald = vali = uv.val.i;
419 }
420
422 double secs;
423 int hours, mins;
424 secs = vald;
425 mins = (int)secs / 60;
426 secs = secs - mins * 60;
427 hours = mins / 60;
428 mins %= 60;
429 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
430 } else {
431 const char *prefix_string = "";
432
433 if (use_value_prefix && vald > 1) {
434 long long int index;
435
437 index = (long long int) (log2(vald)) / 10;
438 index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
439 vald /= si_prefixes[index].bin_val;
440 prefix_string = si_prefixes[index].bin_str;
441 } else {
442 index = (long long int) (log10(vald)) / 3;
443 index = av_clip(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
444 vald /= si_prefixes[index].dec_val;
445 prefix_string = si_prefixes[index].dec_str;
446 }
447 vali = vald;
448 }
449
450 if (show_float || (use_value_prefix && vald != (long long int)vald))
451 snprintf(buf, buf_size, "%f", vald);
452 else
453 snprintf(buf, buf_size, "%lld", vali);
454 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
455 prefix_string, show_value_unit ? uv.unit : "");
456 }
457
458 return buf;
459}
460
461/* WRITERS API */
462
464
465#define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
466#define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
467
468typedef enum {
474
475typedef struct Writer {
476 const AVClass *priv_class;
478 const char *name;
479
481 void (*uninit)(WriterContext *wctx);
482
485 void (*print_integer) (WriterContext *wctx, const char *, long long int);
486 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
487 void (*print_string) (WriterContext *wctx, const char *, const char *);
488 int flags;
490
491#define SECTION_MAX_NB_LEVELS 10
492
494 const AVClass *class;
495 const Writer *writer;
496 AVIOContext *avio;
497
498 void (* writer_w8)(WriterContext *wctx, int b);
499 void (* writer_put_str)(WriterContext *wctx, const char *str);
500 void (* writer_printf)(WriterContext *wctx, const char *fmt, ...);
501
502 char *name;
503 void *priv;
504
505 const struct section *sections;
507
508 int level;
509
512
517
518 unsigned int nb_section_packet;
519 unsigned int nb_section_frame;
521
525};
526
527static const char *writer_get_name(void *p)
528{
529 WriterContext *wctx = p;
530 return wctx->writer->name;
531}
532
533#define OFFSET(x) offsetof(WriterContext, x)
534
535static const AVOption writer_options[] = {
536 { "string_validation", "set string validation mode",
537 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
538 { "sv", "set string validation mode",
539 OFFSET(string_validation), AV_OPT_TYPE_INT, {.i64=WRITER_STRING_VALIDATION_REPLACE}, 0, WRITER_STRING_VALIDATION_NB-1, .unit = "sv" },
540 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_IGNORE}, .unit = "sv" },
541 { "replace", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_REPLACE}, .unit = "sv" },
542 { "fail", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = WRITER_STRING_VALIDATION_FAIL}, .unit = "sv" },
543 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str=""}},
544 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, {.str="\xEF\xBF\xBD"}},
545 { NULL }
546};
547
548static void *writer_child_next(void *obj, void *prev)
549{
550 WriterContext *ctx = obj;
551 if (!prev && ctx->writer && ctx->writer->priv_class && ctx->priv)
552 return ctx->priv;
553 return NULL;
554}
555
556static const AVClass writer_class = {
557 .class_name = "Writer",
558 .item_name = writer_get_name,
559 .option = writer_options,
560 .version = LIBAVUTIL_VERSION_INT,
561 .child_next = writer_child_next,
562};
563
564static int writer_close(WriterContext **wctx)
565{
566 int i;
567 int ret = 0;
568
569 if (!*wctx)
570 return -1;
571
572 if ((*wctx)->writer->uninit)
573 (*wctx)->writer->uninit(*wctx);
574 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
575 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
576 if ((*wctx)->writer->priv_class)
577 av_opt_free((*wctx)->priv);
578 av_freep(&((*wctx)->priv));
579 av_opt_free(*wctx);
580 if ((*wctx)->avio) {
581 avio_flush((*wctx)->avio);
582 ret = avio_close((*wctx)->avio);
583 }
584 av_freep(wctx);
585 return ret;
586}
587
588static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
589{
590 int i;
591 av_bprintf(bp, "0X");
592 for (i = 0; i < ubuf_size; i++)
593 av_bprintf(bp, "%02X", ubuf[i]);
594}
595
596static inline void writer_w8_avio(WriterContext *wctx, int b)
597{
598 avio_w8(wctx->avio, b);
599}
600
601static inline void writer_put_str_avio(WriterContext *wctx, const char *str)
602{
603 avio_write(wctx->avio, str, strlen(str));
604}
605
606static inline void writer_printf_avio(WriterContext *wctx, const char *fmt, ...)
607{
608 va_list ap;
609
610 va_start(ap, fmt);
611 avio_vprintf(wctx->avio, fmt, ap);
612 va_end(ap);
613}
614
615static inline void writer_w8_printf(WriterContext *wctx, int b)
616{
617 av_log(NULL, AV_LOG_STDERR, "%c", b);
618}
619
620static inline void writer_put_str_printf(WriterContext *wctx, const char *str)
621{
622 av_log(NULL, AV_LOG_STDERR, "%s", str);
623}
624
625static inline void writer_printf_printf(WriterContext *wctx, const char *fmt, ...)
626{
627 va_list ap;
628 va_start(ap, fmt);
629 av_vlog(NULL, AV_LOG_STDERR, fmt, ap);
630 va_end(ap);
631}
632
633static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
634 const struct section *sections, int nb_sections, const char *output)
635{
636 int i, ret = 0;
637
638 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
639 ret = AVERROR(ENOMEM);
640 goto fail;
641 }
642
643 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
644 ret = AVERROR(ENOMEM);
645 goto fail;
646 }
647
648 (*wctx)->class = &writer_class;
649 (*wctx)->writer = writer;
650 (*wctx)->level = -1;
651 (*wctx)->sections = sections;
652 (*wctx)->nb_sections = nb_sections;
653
654 av_opt_set_defaults(*wctx);
655
656 if (writer->priv_class) {
657 void *priv_ctx = (*wctx)->priv;
658 *((const AVClass **)priv_ctx) = writer->priv_class;
659 av_opt_set_defaults(priv_ctx);
660 }
661
662 /* convert options to dictionary */
663 if (args) {
664 AVDictionary *opts = NULL;
665 const AVDictionaryEntry *opt = NULL;
666
667 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
668 av_log(*wctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to writer context\n", args);
669 av_dict_free(&opts);
670 goto fail;
671 }
672
673 while ((opt = av_dict_get(opts, "", opt, AV_DICT_IGNORE_SUFFIX))) {
674 if ((ret = av_opt_set(*wctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
675 av_log(*wctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to writer context\n",
676 opt->key, opt->value);
677 av_dict_free(&opts);
678 goto fail;
679 }
680 }
681
682 av_dict_free(&opts);
683 }
684
685 /* validate replace string */
686 {
687 const uint8_t *p = (*wctx)->string_validation_replacement;
688 const uint8_t *endp = p + strlen(p);
689 while (*p) {
690 const uint8_t *p0 = p;
691 int32_t code;
692 ret = av_utf8_decode(&code, &p, endp, (*wctx)->string_validation_utf8_flags);
693 if (ret < 0) {
694 AVBPrint bp;
695 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
696 bprint_bytes(&bp, p0, p-p0),
697 av_log(wctx, AV_LOG_ERROR,
698 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
699 bp.str, (*wctx)->string_validation_replacement);
700 return ret;
701 }
702 }
703 }
704
705 if (!output_filename) {
706 (*wctx)->writer_w8 = writer_w8_printf;
707 (*wctx)->writer_put_str = writer_put_str_printf;
708 (*wctx)->writer_printf = writer_printf_printf;
709 } else {
710 if ((ret = avio_open(&(*wctx)->avio, output, AVIO_FLAG_WRITE)) < 0) {
711 av_log(*wctx, AV_LOG_ERROR,
712 "Failed to open output '%s' with error: %s\n", output, av_err2str(ret));
713 goto fail;
714 }
715 (*wctx)->writer_w8 = writer_w8_avio;
716 (*wctx)->writer_put_str = writer_put_str_avio;
717 (*wctx)->writer_printf = writer_printf_avio;
718 }
719
720 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
721 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
722
723 if ((*wctx)->writer->init)
724 ret = (*wctx)->writer->init(*wctx);
725 if (ret < 0)
726 goto fail;
727
728 return 0;
729
730fail:
731 writer_close(wctx);
732 return ret;
733}
734
736 int section_id)
737{
738 int parent_section_id;
739 wctx->level++;
740 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
741 parent_section_id = wctx->level ?
742 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
743
744 wctx->nb_item[wctx->level] = 0;
745 wctx->section[wctx->level] = &wctx->sections[section_id];
746
747 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
748 wctx->nb_section_packet = wctx->nb_section_frame =
749 wctx->nb_section_packet_frame = 0;
750 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
751 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
753 }
754
755 if (wctx->writer->print_section_header)
756 wctx->writer->print_section_header(wctx);
757}
758
760{
761 int section_id = wctx->section[wctx->level]->id;
762 int parent_section_id = wctx->level ?
763 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
764
765 if (parent_section_id != SECTION_ID_NONE)
766 wctx->nb_item[wctx->level-1]++;
767 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
768 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
769 else wctx->nb_section_frame++;
770 }
771 if (wctx->writer->print_section_footer)
772 wctx->writer->print_section_footer(wctx);
773 wctx->level--;
774}
775
776static inline void writer_print_integer(WriterContext *wctx,
777 const char *key, long long int val)
778{
779 const struct section *section = wctx->section[wctx->level];
780
781 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
782 wctx->writer->print_integer(wctx, key, val);
783 wctx->nb_item[wctx->level]++;
784 }
785}
786
787static inline int validate_string(WriterContext *wctx, char **dstp, const char *src)
788{
789 const uint8_t *p, *endp;
790 AVBPrint dstbuf;
791 int invalid_chars_nb = 0, ret = 0;
792
793 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
794
795 endp = src + strlen(src);
796 for (p = (uint8_t *)src; *p;) {
797 uint32_t code;
798 int invalid = 0;
799 const uint8_t *p0 = p;
800
801 if (av_utf8_decode(&code, &p, endp, wctx->string_validation_utf8_flags) < 0) {
802 AVBPrint bp;
803 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
804 bprint_bytes(&bp, p0, p-p0);
805 av_log(wctx, AV_LOG_DEBUG,
806 "Invalid UTF-8 sequence %s found in string '%s'\n", bp.str, src);
807 invalid = 1;
808 }
809
810 if (invalid) {
811 invalid_chars_nb++;
812
813 switch (wctx->string_validation) {
815 av_log(wctx, AV_LOG_ERROR,
816 "Invalid UTF-8 sequence found in string '%s'\n", src);
817 ret = AVERROR_INVALIDDATA;
818 goto end;
819 break;
820
822 av_bprintf(&dstbuf, "%s", wctx->string_validation_replacement);
823 break;
824 }
825 }
826
827 if (!invalid || wctx->string_validation == WRITER_STRING_VALIDATION_IGNORE)
828 av_bprint_append_data(&dstbuf, p0, p-p0);
829 }
830
831 if (invalid_chars_nb && wctx->string_validation == WRITER_STRING_VALIDATION_REPLACE) {
832 av_log(wctx, AV_LOG_WARNING,
833 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
834 invalid_chars_nb, src, wctx->string_validation_replacement);
835 }
836
837end:
838 av_bprint_finalize(&dstbuf, dstp);
839 return ret;
840}
841
842#define PRINT_STRING_OPT 1
843#define PRINT_STRING_VALIDATE 2
844
845static inline int writer_print_string(WriterContext *wctx,
846 const char *key, const char *val, int flags)
847{
848 const struct section *section = wctx->section[wctx->level];
849 int ret = 0;
850
855 return 0;
856
857 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
859 char *key1 = NULL, *val1 = NULL;
860 ret = validate_string(wctx, &key1, key);
861 if (ret < 0) goto end;
862 ret = validate_string(wctx, &val1, val);
863 if (ret < 0) goto end;
864 wctx->writer->print_string(wctx, key1, val1);
865 end:
866 if (ret < 0) {
867 av_log(wctx, AV_LOG_ERROR,
868 "Invalid key=value string combination %s=%s in section %s\n",
869 key, val, section->unique_name);
870 }
871 av_free(key1);
872 av_free(val1);
873 } else {
874 wctx->writer->print_string(wctx, key, val);
875 }
876
877 wctx->nb_item[wctx->level]++;
878 }
879
880 return ret;
881}
882
883static inline void writer_print_rational(WriterContext *wctx,
884 const char *key, AVRational q, char sep)
885{
886 AVBPrint buf;
887 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
888 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
889 writer_print_string(wctx, key, buf.str, 0);
890}
891
892static void writer_print_time(WriterContext *wctx, const char *key,
893 int64_t ts, const AVRational *time_base, int is_duration)
894{
895 char buf[128];
896
897 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
898 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
899 } else {
900 double d = ts * av_q2d(*time_base);
901 struct unit_value uv;
902 uv.val.d = d;
904 value_string(buf, sizeof(buf), uv);
905 writer_print_string(wctx, key, buf, 0);
906 }
907}
908
909static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
910{
911 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
912 writer_print_string(wctx, key, "N/A", PRINT_STRING_OPT);
913 } else {
914 writer_print_integer(wctx, key, ts);
915 }
916}
917
918static void writer_print_data(WriterContext *wctx, const char *name,
919 const uint8_t *data, int size)
920{
921 AVBPrint bp;
922 int offset = 0, l, i;
923
924 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
925 av_bprintf(&bp, "\n");
926 while (size) {
927 av_bprintf(&bp, "%08x: ", offset);
928 l = FFMIN(size, 16);
929 for (i = 0; i < l; i++) {
930 av_bprintf(&bp, "%02x", data[i]);
931 if (i & 1)
932 av_bprintf(&bp, " ");
933 }
934 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
935 for (i = 0; i < l; i++)
936 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
937 av_bprintf(&bp, "\n");
938 offset += l;
939 data += l;
940 size -= l;
941 }
942 writer_print_string(wctx, name, bp.str, 0);
943 av_bprint_finalize(&bp, NULL);
944}
945
946static void writer_print_data_hash(WriterContext *wctx, const char *name,
947 const uint8_t *data, int size)
948{
949 char *p, buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
950
951 if (!hash)
952 return;
953 av_hash_init(hash);
954 av_hash_update(hash, data, size);
955 snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(hash));
956 p = buf + strlen(buf);
957 av_hash_final_hex(hash, p, buf + sizeof(buf) - p);
958 writer_print_string(wctx, name, buf, 0);
959}
960
961static void writer_print_integers(WriterContext *wctx, const char *name,
962 uint8_t *data, int size, const char *format,
963 int columns, int bytes, int offset_add)
964{
965 AVBPrint bp;
966 int offset = 0, l, i;
967
968 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
969 av_bprintf(&bp, "\n");
970 while (size) {
971 av_bprintf(&bp, "%08x: ", offset);
972 l = FFMIN(size, columns);
973 for (i = 0; i < l; i++) {
974 if (bytes == 1) av_bprintf(&bp, format, *data);
975 else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
976 else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
977 data += bytes;
978 size --;
979 }
980 av_bprintf(&bp, "\n");
981 offset += offset_add;
982 }
983 writer_print_string(wctx, name, bp.str, 0);
984 av_bprint_finalize(&bp, NULL);
985}
986
987#define writer_w8(wctx_, b_) (wctx_)->writer_w8(wctx_, b_)
988#define writer_put_str(wctx_, str_) (wctx_)->writer_put_str(wctx_, str_)
989#define writer_printf(wctx_, fmt_, ...) (wctx_)->writer_printf(wctx_, fmt_, __VA_ARGS__)
990
991#define MAX_REGISTERED_WRITERS_NB 64
992
994
996
997static int writer_register(const Writer *writer)
998{
1000 return AVERROR(ENOMEM);
1001
1003 return 0;
1004}
1005
1006static const Writer *writer_get_by_name(const char *name)
1007{
1008 int i;
1009
1010 for (i = 0; registered_writers[i]; i++)
1011 if (!strcmp(registered_writers[i]->name, name))
1012 return registered_writers[i];
1013
1014 return NULL;
1015}
1016
1017
1018/* WRITERS */
1019
1020#define DEFINE_WRITER_CLASS(name) \
1021static const char *name##_get_name(void *ctx) \
1022{ \
1023 return #name ; \
1024} \
1025static const AVClass name##_class = { \
1026 .class_name = #name, \
1027 .item_name = name##_get_name, \
1028 .option = name##_options \
1029}
1030
1031/* Default output */
1032
1033typedef struct DefaultContext {
1034 const AVClass *class;
1039
1040#undef OFFSET
1041#define OFFSET(x) offsetof(DefaultContext, x)
1042
1043static const AVOption default_options[] = {
1044 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1045 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1046 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1047 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1048 {NULL},
1049};
1050
1052
1053/* lame uppercasing routine, assumes the string is lower case ASCII */
1054static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
1055{
1056 int i;
1057 for (i = 0; src[i] && i < dst_size-1; i++)
1058 dst[i] = av_toupper(src[i]);
1059 dst[i] = 0;
1060 return dst;
1061}
1062
1064{
1065 DefaultContext *def = wctx->priv;
1066 char buf[32];
1067 const struct section *section = wctx->section[wctx->level];
1068 const struct section *parent_section = wctx->level ?
1069 wctx->section[wctx->level-1] : NULL;
1070
1071 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1072 if (parent_section &&
1073 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1074 def->nested_section[wctx->level] = 1;
1075 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1076 wctx->section_pbuf[wctx->level-1].str,
1077 upcase_string(buf, sizeof(buf),
1078 av_x_if_null(section->element_name, section->name)));
1079 }
1080
1081 if (def->noprint_wrappers || def->nested_section[wctx->level])
1082 return;
1083
1085 writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
1086}
1087
1089{
1090 DefaultContext *def = wctx->priv;
1091 const struct section *section = wctx->section[wctx->level];
1092 char buf[32];
1093
1094 if (def->noprint_wrappers || def->nested_section[wctx->level])
1095 return;
1096
1098 writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
1099}
1100
1101static void default_print_str(WriterContext *wctx, const char *key, const char *value)
1102{
1103 DefaultContext *def = wctx->priv;
1104
1105 if (!def->nokey)
1106 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1107 writer_printf(wctx, "%s\n", value);
1108}
1109
1110static void default_print_int(WriterContext *wctx, const char *key, long long int value)
1111{
1112 DefaultContext *def = wctx->priv;
1113
1114 if (!def->nokey)
1115 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1116 writer_printf(wctx, "%lld\n", value);
1117}
1118
1119static const Writer default_writer = {
1120 .name = "default",
1121 .priv_size = sizeof(DefaultContext),
1124 .print_integer = default_print_int,
1125 .print_string = default_print_str,
1127 .priv_class = &default_class,
1128};
1129
1130/* Compact output */
1131
1135static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1136{
1137 const char *p;
1138
1139 for (p = src; *p; p++) {
1140 switch (*p) {
1141 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1142 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1143 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1144 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1145 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1146 default:
1147 if (*p == sep)
1148 av_bprint_chars(dst, '\\', 1);
1149 av_bprint_chars(dst, *p, 1);
1150 }
1151 }
1152 return dst->str;
1153}
1154
1158static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1159{
1160 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
1161 int needs_quoting = !!src[strcspn(src, meta_chars)];
1162
1163 if (needs_quoting)
1164 av_bprint_chars(dst, '"', 1);
1165
1166 for (; *src; src++) {
1167 if (*src == '"')
1168 av_bprint_chars(dst, '"', 1);
1169 av_bprint_chars(dst, *src, 1);
1170 }
1171 if (needs_quoting)
1172 av_bprint_chars(dst, '"', 1);
1173 return dst->str;
1174}
1175
1176static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
1177{
1178 return src;
1179}
1180
1181typedef struct CompactContext {
1182 const AVClass *class;
1188 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
1193
1194#undef OFFSET
1195#define OFFSET(x) offsetof(CompactContext, x)
1196
1197static const AVOption compact_options[]= {
1198 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1199 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, 0, 0 },
1200 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1201 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1202 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1203 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, 0, 0 },
1204 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1205 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1206 {NULL},
1207};
1208
1210
1211static av_cold int compact_init(WriterContext *wctx)
1212{
1213 CompactContext *compact = wctx->priv;
1214
1215 if (strlen(compact->item_sep_str) != 1) {
1216 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1217 compact->item_sep_str);
1218 return AVERROR(EINVAL);
1219 }
1220 compact->item_sep = compact->item_sep_str[0];
1221
1222 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
1223 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
1224 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
1225 else {
1226 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
1227 return AVERROR(EINVAL);
1228 }
1229
1230 return 0;
1231}
1232
1234{
1235 CompactContext *compact = wctx->priv;
1236 const struct section *section = wctx->section[wctx->level];
1237 const struct section *parent_section = wctx->level ?
1238 wctx->section[wctx->level-1] : NULL;
1239 compact->terminate_line[wctx->level] = 1;
1240 compact->has_nested_elems[wctx->level] = 0;
1241
1242 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
1243 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
1244 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
1245 compact->nested_section[wctx->level] = 1;
1246 compact->has_nested_elems[wctx->level-1] = 1;
1247 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
1248 wctx->section_pbuf[wctx->level-1].str,
1249 (char *)av_x_if_null(section->element_name, section->name));
1250 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
1251 } else {
1252 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
1254 compact->terminate_line[wctx->level-1] = 0;
1255 }
1256 if (parent_section && !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)) &&
1257 wctx->level && wctx->nb_item[wctx->level-1])
1258 writer_w8(wctx, compact->item_sep);
1259 if (compact->print_section &&
1261 writer_printf(wctx, "%s%c", section->name, compact->item_sep);
1262 }
1263}
1264
1266{
1267 CompactContext *compact = wctx->priv;
1268
1269 if (!compact->nested_section[wctx->level] &&
1270 compact->terminate_line[wctx->level] &&
1272 writer_w8(wctx, '\n');
1273}
1274
1275static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
1276{
1277 CompactContext *compact = wctx->priv;
1278 AVBPrint buf;
1279
1280 if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
1281 if (!compact->nokey)
1282 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1283 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1284 writer_put_str(wctx, compact->escape_str(&buf, value, compact->item_sep, wctx));
1285 av_bprint_finalize(&buf, NULL);
1286}
1287
1288static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
1289{
1290 CompactContext *compact = wctx->priv;
1291
1292 if (wctx->nb_item[wctx->level]) writer_w8(wctx, compact->item_sep);
1293 if (!compact->nokey)
1294 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
1295 writer_printf(wctx, "%lld", value);
1296}
1297
1298static const Writer compact_writer = {
1299 .name = "compact",
1300 .priv_size = sizeof(CompactContext),
1301 .init = compact_init,
1304 .print_integer = compact_print_int,
1305 .print_string = compact_print_str,
1307 .priv_class = &compact_class,
1308};
1309
1310/* CSV output */
1311
1312#undef OFFSET
1313#define OFFSET(x) offsetof(CompactContext, x)
1314
1315static const AVOption csv_options[] = {
1316 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1317 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, 0, 0 },
1318 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1319 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1320 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1321 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, 0, 0 },
1322 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1323 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1324 {NULL},
1325};
1326
1328
1329static const Writer csv_writer = {
1330 .name = "csv",
1331 .priv_size = sizeof(CompactContext),
1332 .init = compact_init,
1335 .print_integer = compact_print_int,
1336 .print_string = compact_print_str,
1338 .priv_class = &csv_class,
1339};
1340
1341/* Flat output */
1342
1343typedef struct FlatContext {
1344 const AVClass *class;
1345 const char *sep_str;
1346 char sep;
1349
1350#undef OFFSET
1351#define OFFSET(x) offsetof(FlatContext, x)
1352
1353static const AVOption flat_options[]= {
1354 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1355 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
1356 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1357 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1358 {NULL},
1359};
1360
1362
1363static av_cold int flat_init(WriterContext *wctx)
1364{
1365 FlatContext *flat = wctx->priv;
1366
1367 if (strlen(flat->sep_str) != 1) {
1368 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
1369 flat->sep_str);
1370 return AVERROR(EINVAL);
1371 }
1372 flat->sep = flat->sep_str[0];
1373
1374 return 0;
1375}
1376
1377static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
1378{
1379 const char *p;
1380
1381 for (p = src; *p; p++) {
1382 if (!((*p >= '0' && *p <= '9') ||
1383 (*p >= 'a' && *p <= 'z') ||
1384 (*p >= 'A' && *p <= 'Z')))
1385 av_bprint_chars(dst, '_', 1);
1386 else
1387 av_bprint_chars(dst, *p, 1);
1388 }
1389 return dst->str;
1390}
1391
1392static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
1393{
1394 const char *p;
1395
1396 for (p = src; *p; p++) {
1397 switch (*p) {
1398 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1399 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1400 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
1401 case '"': av_bprintf(dst, "%s", "\\\""); break;
1402 case '`': av_bprintf(dst, "%s", "\\`"); break;
1403 case '$': av_bprintf(dst, "%s", "\\$"); break;
1404 default: av_bprint_chars(dst, *p, 1); break;
1405 }
1406 }
1407 return dst->str;
1408}
1409
1411{
1412 FlatContext *flat = wctx->priv;
1413 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1414 const struct section *section = wctx->section[wctx->level];
1415 const struct section *parent_section = wctx->level ?
1416 wctx->section[wctx->level-1] : NULL;
1417
1418 /* build section header */
1419 av_bprint_clear(buf);
1420 if (!parent_section)
1421 return;
1422 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1423
1424 if (flat->hierarchical ||
1426 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
1427
1428 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1429 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1430 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1431 av_bprintf(buf, "%d%s", n, flat->sep_str);
1432 }
1433 }
1434}
1435
1436static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
1437{
1438 writer_printf(wctx, "%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
1439}
1440
1441static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
1442{
1443 FlatContext *flat = wctx->priv;
1444 AVBPrint buf;
1445
1446 writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
1447 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1448 writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
1449 av_bprint_clear(&buf);
1450 writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
1451 av_bprint_finalize(&buf, NULL);
1452}
1453
1454static const Writer flat_writer = {
1455 .name = "flat",
1456 .priv_size = sizeof(FlatContext),
1457 .init = flat_init,
1459 .print_integer = flat_print_int,
1460 .print_string = flat_print_str,
1462 .priv_class = &flat_class,
1463};
1464
1465/* INI format output */
1466
1467typedef struct INIContext {
1468 const AVClass *class;
1471
1472#undef OFFSET
1473#define OFFSET(x) offsetof(INIContext, x)
1474
1475static const AVOption ini_options[] = {
1476 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1477 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
1478 {NULL},
1479};
1480
1482
1483static char *ini_escape_str(AVBPrint *dst, const char *src)
1484{
1485 int i = 0;
1486 char c = 0;
1487
1488 while ((c = src[i++])) {
1489 switch (c) {
1490 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1491 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1492 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1493 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1494 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1495 case '\\':
1496 case '#' :
1497 case '=' :
1498 case ':' : av_bprint_chars(dst, '\\', 1);
1499 default:
1500 if ((unsigned char)c < 32)
1501 av_bprintf(dst, "\\x00%02x", c & 0xff);
1502 else
1503 av_bprint_chars(dst, c, 1);
1504 break;
1505 }
1506 }
1507 return dst->str;
1508}
1509
1511{
1512 INIContext *ini = wctx->priv;
1513 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1514 const struct section *section = wctx->section[wctx->level];
1515 const struct section *parent_section = wctx->level ?
1516 wctx->section[wctx->level-1] : NULL;
1517
1518 av_bprint_clear(buf);
1519 if (!parent_section) {
1520 writer_put_str(wctx, "# ffprobe output\n\n");
1521 return;
1522 }
1523
1524 if (wctx->nb_item[wctx->level-1])
1525 writer_w8(wctx, '\n');
1526
1527 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1528 if (ini->hierarchical ||
1530 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1531
1532 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1533 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1534 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1535 av_bprintf(buf, ".%d", n);
1536 }
1537 }
1538
1540 writer_printf(wctx, "[%s]\n", buf->str);
1541}
1542
1543static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1544{
1545 AVBPrint buf;
1546
1547 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1548 writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
1549 av_bprint_clear(&buf);
1550 writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
1551 av_bprint_finalize(&buf, NULL);
1552}
1553
1554static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1555{
1556 writer_printf(wctx, "%s=%lld\n", key, value);
1557}
1558
1559static const Writer ini_writer = {
1560 .name = "ini",
1561 .priv_size = sizeof(INIContext),
1563 .print_integer = ini_print_int,
1564 .print_string = ini_print_str,
1566 .priv_class = &ini_class,
1567};
1568
1569/* JSON output */
1570
1571typedef struct JSONContext {
1572 const AVClass *class;
1577
1578#undef OFFSET
1579#define OFFSET(x) offsetof(JSONContext, x)
1580
1581static const AVOption json_options[]= {
1582 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1583 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1584 { NULL }
1585};
1586
1588
1589static av_cold int json_init(WriterContext *wctx)
1590{
1591 JSONContext *json = wctx->priv;
1592
1593 json->item_sep = json->compact ? ", " : ",\n";
1594 json->item_start_end = json->compact ? " " : "\n";
1595
1596 return 0;
1597}
1598
1599static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1600{
1601 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1602 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1603 const char *p;
1604
1605 for (p = src; *p; p++) {
1606 char *s = strchr(json_escape, *p);
1607 if (s) {
1608 av_bprint_chars(dst, '\\', 1);
1609 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1610 } else if ((unsigned char)*p < 32) {
1611 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1612 } else {
1613 av_bprint_chars(dst, *p, 1);
1614 }
1615 }
1616 return dst->str;
1617}
1618
1619#define JSON_INDENT() writer_printf(wctx, "%*c", json->indent_level * 4, ' ')
1620
1622{
1623 JSONContext *json = wctx->priv;
1624 AVBPrint buf;
1625 const struct section *section = wctx->section[wctx->level];
1626 const struct section *parent_section = wctx->level ?
1627 wctx->section[wctx->level-1] : NULL;
1628
1629 if (wctx->level && wctx->nb_item[wctx->level-1])
1630 writer_put_str(wctx, ",\n");
1631
1633 writer_put_str(wctx, "{\n");
1634 json->indent_level++;
1635 } else {
1636 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1637 json_escape_str(&buf, section->name, wctx);
1638 JSON_INDENT();
1639
1640 json->indent_level++;
1642 writer_printf(wctx, "\"%s\": [\n", buf.str);
1643 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1644 writer_printf(wctx, "\"%s\": {%s", buf.str, json->item_start_end);
1645 } else {
1646 writer_printf(wctx, "{%s", json->item_start_end);
1647
1648 /* this is required so the parser can distinguish between packets and frames */
1649 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1650 if (!json->compact)
1651 JSON_INDENT();
1652 writer_printf(wctx, "\"type\": \"%s\"", section->name);
1653 wctx->nb_item[wctx->level]++;
1654 }
1655 }
1656 av_bprint_finalize(&buf, NULL);
1657 }
1658}
1659
1661{
1662 JSONContext *json = wctx->priv;
1663 const struct section *section = wctx->section[wctx->level];
1664
1665 if (wctx->level == 0) {
1666 json->indent_level--;
1667 writer_put_str(wctx, "\n}\n");
1668 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1669 writer_w8(wctx, '\n');
1670 json->indent_level--;
1671 JSON_INDENT();
1672 writer_w8(wctx, ']');
1673 } else {
1674 writer_put_str(wctx, json->item_start_end);
1675 json->indent_level--;
1676 if (!json->compact)
1677 JSON_INDENT();
1678 writer_w8(wctx, '}');
1679 }
1680}
1681
1682static inline void json_print_item_str(WriterContext *wctx,
1683 const char *key, const char *value)
1684{
1685 AVBPrint buf;
1686
1687 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1688 writer_printf(wctx, "\"%s\":", json_escape_str(&buf, key, wctx));
1689 av_bprint_clear(&buf);
1690 writer_printf(wctx, " \"%s\"", json_escape_str(&buf, value, wctx));
1691 av_bprint_finalize(&buf, NULL);
1692}
1693
1694static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1695{
1696 JSONContext *json = wctx->priv;
1697 const struct section *parent_section = wctx->level ?
1698 wctx->section[wctx->level-1] : NULL;
1699
1700 if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1701 writer_put_str(wctx, json->item_sep);
1702 if (!json->compact)
1703 JSON_INDENT();
1704 json_print_item_str(wctx, key, value);
1705}
1706
1707static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1708{
1709 JSONContext *json = wctx->priv;
1710 const struct section *parent_section = wctx->level ?
1711 wctx->section[wctx->level-1] : NULL;
1712 AVBPrint buf;
1713
1714 if (wctx->nb_item[wctx->level] || (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES))
1715 writer_put_str(wctx, json->item_sep);
1716 if (!json->compact)
1717 JSON_INDENT();
1718
1719 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1720 writer_printf(wctx, "\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1721 av_bprint_finalize(&buf, NULL);
1722}
1723
1724static const Writer json_writer = {
1725 .name = "json",
1726 .priv_size = sizeof(JSONContext),
1727 .init = json_init,
1730 .print_integer = json_print_int,
1731 .print_string = json_print_str,
1733 .priv_class = &json_class,
1734};
1735
1736/* XML output */
1737
1738typedef struct XMLContext {
1739 const AVClass *class;
1745
1746#undef OFFSET
1747#define OFFSET(x) offsetof(XMLContext, x)
1748
1749static const AVOption xml_options[] = {
1750 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1751 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1752 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1753 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
1754 {NULL},
1755};
1756
1758
1759static av_cold int xml_init(WriterContext *wctx)
1760{
1761 XMLContext *xml = wctx->priv;
1762
1763 if (xml->xsd_strict) {
1764 xml->fully_qualified = 1;
1765#define CHECK_COMPLIANCE(opt, opt_name) \
1766 if (opt) { \
1767 av_log(wctx, AV_LOG_ERROR, \
1768 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1769 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1770 return AVERROR(EINVAL); \
1771 }
1775 }
1776
1777 return 0;
1778}
1779
1780#define XML_INDENT() writer_printf(wctx, "%*c", xml->indent_level * 4, ' ')
1781
1783{
1784 XMLContext *xml = wctx->priv;
1785 const struct section *section = wctx->section[wctx->level];
1786 const struct section *parent_section = wctx->level ?
1787 wctx->section[wctx->level-1] : NULL;
1788
1789 if (wctx->level == 0) {
1790 const char *qual = " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
1791 "xmlns:ffprobe=\"http://www.ffmpeg.org/schema/ffprobe\" "
1792 "xsi:schemaLocation=\"http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd\"";
1793
1794 writer_put_str(wctx, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1795 writer_printf(wctx, "<%sffprobe%s>\n",
1796 xml->fully_qualified ? "ffprobe:" : "",
1797 xml->fully_qualified ? qual : "");
1798 return;
1799 }
1800
1801 if (xml->within_tag) {
1802 xml->within_tag = 0;
1803 writer_put_str(wctx, ">\n");
1804 }
1806 xml->indent_level++;
1807 } else {
1808 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1809 wctx->level && wctx->nb_item[wctx->level-1])
1810 writer_w8(wctx, '\n');
1811 xml->indent_level++;
1812
1814 XML_INDENT(); writer_printf(wctx, "<%s>\n", section->name);
1815 } else {
1816 XML_INDENT(); writer_printf(wctx, "<%s ", section->name);
1817 xml->within_tag = 1;
1818 }
1819 }
1820}
1821
1823{
1824 XMLContext *xml = wctx->priv;
1825 const struct section *section = wctx->section[wctx->level];
1826
1827 if (wctx->level == 0) {
1828 writer_printf(wctx, "</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1829 } else if (xml->within_tag) {
1830 xml->within_tag = 0;
1831 writer_put_str(wctx, "/>\n");
1832 xml->indent_level--;
1834 xml->indent_level--;
1835 } else {
1836 XML_INDENT(); writer_printf(wctx, "</%s>\n", section->name);
1837 xml->indent_level--;
1838 }
1839}
1840
1841static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1842{
1843 AVBPrint buf;
1844 XMLContext *xml = wctx->priv;
1845 const struct section *section = wctx->section[wctx->level];
1846
1847 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1848
1850 XML_INDENT();
1851 av_bprint_escape(&buf, key, NULL,
1852 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1853 writer_printf(wctx, "<%s key=\"%s\"",
1854 section->element_name, buf.str);
1855 av_bprint_clear(&buf);
1856
1857 av_bprint_escape(&buf, value, NULL,
1858 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1859 writer_printf(wctx, " value=\"%s\"/>\n", buf.str);
1860 } else {
1861 if (wctx->nb_item[wctx->level])
1862 writer_w8(wctx, ' ');
1863
1864 av_bprint_escape(&buf, value, NULL,
1865 AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
1866 writer_printf(wctx, "%s=\"%s\"", key, buf.str);
1867 }
1868
1869 av_bprint_finalize(&buf, NULL);
1870}
1871
1872static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1873{
1874 if (wctx->nb_item[wctx->level])
1875 writer_w8(wctx, ' ');
1876 writer_printf(wctx, "%s=\"%lld\"", key, value);
1877}
1878
1880 .name = "xml",
1881 .priv_size = sizeof(XMLContext),
1882 .init = xml_init,
1885 .print_integer = xml_print_int,
1886 .print_string = xml_print_str,
1888 .priv_class = &xml_class,
1889};
1890
1891static void writer_register_all(void)
1892{
1893
1901}
1902
1903#define print_fmt(k, f, ...) do { \
1904 av_bprint_clear(&pbuf); \
1905 av_bprintf(&pbuf, f, __VA_ARGS__); \
1906 writer_print_string(w, k, pbuf.str, 0); \
1907} while (0)
1908
1909#define print_list_fmt(k, f, n, ...) do { \
1910 av_bprint_clear(&pbuf); \
1911 for (int idx = 0; idx < n; idx++) { \
1912 if (idx > 0) \
1913 av_bprint_chars(&pbuf, ' ', 1); \
1914 av_bprintf(&pbuf, f, __VA_ARGS__); \
1915 } \
1916 writer_print_string(w, k, pbuf.str, 0); \
1917} while (0)
1918
1919#define print_int(k, v) writer_print_integer(w, k, v)
1920#define print_q(k, v, s) writer_print_rational(w, k, v, s)
1921#define print_str(k, v) writer_print_string(w, k, v, 0)
1922#define print_str_opt(k, v) writer_print_string(w, k, v, PRINT_STRING_OPT)
1923#define print_str_validate(k, v) writer_print_string(w, k, v, PRINT_STRING_VALIDATE)
1924#define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1925#define print_ts(k, v) writer_print_ts(w, k, v, 0)
1926#define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1927#define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1928#define print_val(k, v, u) do { \
1929 struct unit_value uv; \
1930 uv.val.i = v; \
1931 uv.unit = u; \
1932 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1933} while (0)
1934
1935#define print_section_header(s) writer_print_section_header(w, s)
1936#define print_section_footer(s) writer_print_section_footer(w, s)
1937
1938#define REALLOCZ_ARRAY_STREAM(ptr, cur_n, new_n) \
1939{ \
1940 ret = av_reallocp_array(&(ptr), (new_n), sizeof(*(ptr))); \
1941 if (ret < 0) \
1942 goto end; \
1943 memset( (ptr) + (cur_n), 0, ((new_n) - (cur_n)) * sizeof(*(ptr)) ); \
1944}
1945
1946static inline int show_tags(WriterContext *w, AVDictionary *tags, int section_id)
1947{
1948 const AVDictionaryEntry *tag = NULL;
1949 int ret = 0;
1950
1951 if (!tags)
1952 return 0;
1953 writer_print_section_header(w, section_id);
1954
1955 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1956 if ((ret = print_str_validate(tag->key, tag->value)) < 0)
1957 break;
1958 }
1960
1961 return ret;
1962}
1963
1964static void print_dovi_metadata(WriterContext *w, const AVDOVIMetadata *dovi)
1965{
1966 if (!dovi)
1967 return;
1968
1969 {
1970 const AVDOVIRpuDataHeader *hdr = av_dovi_get_header(dovi);
1971 const AVDOVIDataMapping *mapping = av_dovi_get_mapping(dovi);
1972 const AVDOVIColorMetadata *color = av_dovi_get_color(dovi);
1973 AVBPrint pbuf;
1974
1975 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1976
1977 // header
1978 print_int("rpu_type", hdr->rpu_type);
1979 print_int("rpu_format", hdr->rpu_format);
1980 print_int("vdr_rpu_profile", hdr->vdr_rpu_profile);
1981 print_int("vdr_rpu_level", hdr->vdr_rpu_level);
1982 print_int("chroma_resampling_explicit_filter_flag",
1983 hdr->chroma_resampling_explicit_filter_flag);
1984 print_int("coef_data_type", hdr->coef_data_type);
1985 print_int("coef_log2_denom", hdr->coef_log2_denom);
1986 print_int("vdr_rpu_normalized_idc", hdr->vdr_rpu_normalized_idc);
1987 print_int("bl_video_full_range_flag", hdr->bl_video_full_range_flag);
1988 print_int("bl_bit_depth", hdr->bl_bit_depth);
1989 print_int("el_bit_depth", hdr->el_bit_depth);
1990 print_int("vdr_bit_depth", hdr->vdr_bit_depth);
1991 print_int("spatial_resampling_filter_flag",
1992 hdr->spatial_resampling_filter_flag);
1993 print_int("el_spatial_resampling_filter_flag",
1994 hdr->el_spatial_resampling_filter_flag);
1995 print_int("disable_residual_flag", hdr->disable_residual_flag);
1996
1997 // data mapping values
1998 print_int("vdr_rpu_id", mapping->vdr_rpu_id);
1999 print_int("mapping_color_space", mapping->mapping_color_space);
2000 print_int("mapping_chroma_format_idc",
2001 mapping->mapping_chroma_format_idc);
2002
2003 print_int("nlq_method_idc", mapping->nlq_method_idc);
2004 switch (mapping->nlq_method_idc) {
2005 case AV_DOVI_NLQ_NONE:
2006 print_str("nlq_method_idc_name", "none");
2007 break;
2008 case AV_DOVI_NLQ_LINEAR_DZ:
2009 print_str("nlq_method_idc_name", "linear_dz");
2010 break;
2011 default:
2012 print_str("nlq_method_idc_name", "unknown");
2013 break;
2014 }
2015
2016 print_int("num_x_partitions", mapping->num_x_partitions);
2017 print_int("num_y_partitions", mapping->num_y_partitions);
2018
2020
2021 for (int c = 0; c < 3; c++) {
2022 const AVDOVIReshapingCurve *curve = &mapping->curves[c];
2024
2025 print_list_fmt("pivots", "%"PRIu16, curve->num_pivots, curve->pivots[idx]);
2026
2028 for (int i = 0; i < curve->num_pivots - 1; i++) {
2029
2031 print_int("mapping_idc", curve->mapping_idc[i]);
2032 switch (curve->mapping_idc[i]) {
2033 case AV_DOVI_MAPPING_POLYNOMIAL:
2034 print_str("mapping_idc_name", "polynomial");
2035 print_int("poly_order", curve->poly_order[i]);
2036 print_list_fmt("poly_coef", "%"PRIi64,
2037 curve->poly_order[i] + 1,
2038 curve->poly_coef[i][idx]);
2039 break;
2040 case AV_DOVI_MAPPING_MMR:
2041 print_str("mapping_idc_name", "mmr");
2042 print_int("mmr_order", curve->mmr_order[i]);
2043 print_int("mmr_constant", curve->mmr_constant[i]);
2044 print_list_fmt("mmr_coef", "%"PRIi64,
2045 curve->mmr_order[i] * 7,
2046 curve->mmr_coef[i][0][idx]);
2047 break;
2048 default:
2049 print_str("mapping_idc_name", "unknown");
2050 break;
2051 }
2052
2053 // SECTION_ID_FRAME_SIDE_DATA_PIECE
2055 }
2056
2057 // SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
2059
2060 if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
2061 const AVDOVINLQParams *nlq = &mapping->nlq[c];
2062 print_int("nlq_offset", nlq->nlq_offset);
2063 print_int("vdr_in_max", nlq->vdr_in_max);
2064
2065 switch (mapping->nlq_method_idc) {
2066 case AV_DOVI_NLQ_LINEAR_DZ:
2067 print_int("linear_deadzone_slope", nlq->linear_deadzone_slope);
2068 print_int("linear_deadzone_threshold", nlq->linear_deadzone_threshold);
2069 break;
2070 }
2071 }
2072
2073 // SECTION_ID_FRAME_SIDE_DATA_COMPONENT
2075 }
2076
2077 // SECTION_ID_FRAME_SIDE_DATA_COMPONENT_LIST
2079
2080 // color metadata
2081 print_int("dm_metadata_id", color->dm_metadata_id);
2082 print_int("scene_refresh_flag", color->scene_refresh_flag);
2083 print_list_fmt("ycc_to_rgb_matrix", "%d/%d",
2084 FF_ARRAY_ELEMS(color->ycc_to_rgb_matrix),
2085 color->ycc_to_rgb_matrix[idx].num,
2086 color->ycc_to_rgb_matrix[idx].den);
2087 print_list_fmt("ycc_to_rgb_offset", "%d/%d",
2088 FF_ARRAY_ELEMS(color->ycc_to_rgb_offset),
2089 color->ycc_to_rgb_offset[idx].num,
2090 color->ycc_to_rgb_offset[idx].den);
2091 print_list_fmt("rgb_to_lms_matrix", "%d/%d",
2092 FF_ARRAY_ELEMS(color->rgb_to_lms_matrix),
2093 color->rgb_to_lms_matrix[idx].num,
2094 color->rgb_to_lms_matrix[idx].den);
2095 print_int("signal_eotf", color->signal_eotf);
2096 print_int("signal_eotf_param0", color->signal_eotf_param0);
2097 print_int("signal_eotf_param1", color->signal_eotf_param1);
2098 print_int("signal_eotf_param2", color->signal_eotf_param2);
2099 print_int("signal_bit_depth", color->signal_bit_depth);
2100 print_int("signal_color_space", color->signal_color_space);
2101 print_int("signal_chroma_format", color->signal_chroma_format);
2102 print_int("signal_full_range_flag", color->signal_full_range_flag);
2103 print_int("source_min_pq", color->source_min_pq);
2104 print_int("source_max_pq", color->source_max_pq);
2105 print_int("source_diagonal", color->source_diagonal);
2106
2107 av_bprint_finalize(&pbuf, NULL);
2108 }
2109}
2110
2111static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
2112{
2113 if (!metadata)
2114 return;
2115 print_int("application version", metadata->application_version);
2116 print_int("num_windows", metadata->num_windows);
2117 for (int n = 1; n < metadata->num_windows; n++) {
2118 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2119 print_q("window_upper_left_corner_x",
2120 params->window_upper_left_corner_x,'/');
2121 print_q("window_upper_left_corner_y",
2122 params->window_upper_left_corner_y,'/');
2123 print_q("window_lower_right_corner_x",
2124 params->window_lower_right_corner_x,'/');
2125 print_q("window_lower_right_corner_y",
2126 params->window_lower_right_corner_y,'/');
2127 print_q("window_upper_left_corner_x",
2128 params->window_upper_left_corner_x,'/');
2129 print_q("window_upper_left_corner_y",
2130 params->window_upper_left_corner_y,'/');
2131 print_int("center_of_ellipse_x",
2132 params->center_of_ellipse_x ) ;
2133 print_int("center_of_ellipse_y",
2134 params->center_of_ellipse_y );
2135 print_int("rotation_angle",
2136 params->rotation_angle);
2137 print_int("semimajor_axis_internal_ellipse",
2138 params->semimajor_axis_internal_ellipse);
2139 print_int("semimajor_axis_external_ellipse",
2140 params->semimajor_axis_external_ellipse);
2141 print_int("semiminor_axis_external_ellipse",
2142 params->semiminor_axis_external_ellipse);
2143 print_int("overlap_process_option",
2144 params->overlap_process_option);
2145 }
2146 print_q("targeted_system_display_maximum_luminance",
2147 metadata->targeted_system_display_maximum_luminance,'/');
2148 if (metadata->targeted_system_display_actual_peak_luminance_flag) {
2149 print_int("num_rows_targeted_system_display_actual_peak_luminance",
2150 metadata->num_rows_targeted_system_display_actual_peak_luminance);
2151 print_int("num_cols_targeted_system_display_actual_peak_luminance",
2152 metadata->num_cols_targeted_system_display_actual_peak_luminance);
2153 for (int i = 0; i < metadata->num_rows_targeted_system_display_actual_peak_luminance; i++) {
2154 for (int j = 0; j < metadata->num_cols_targeted_system_display_actual_peak_luminance; j++) {
2155 print_q("targeted_system_display_actual_peak_luminance",
2156 metadata->targeted_system_display_actual_peak_luminance[i][j],'/');
2157 }
2158 }
2159 }
2160 for (int n = 0; n < metadata->num_windows; n++) {
2161 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2162 for (int i = 0; i < 3; i++) {
2163 print_q("maxscl",params->maxscl[i],'/');
2164 }
2165 print_q("average_maxrgb",
2166 params->average_maxrgb,'/');
2167 print_int("num_distribution_maxrgb_percentiles",
2168 params->num_distribution_maxrgb_percentiles);
2169 for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
2170 print_int("distribution_maxrgb_percentage",
2171 params->distribution_maxrgb[i].percentage);
2172 print_q("distribution_maxrgb_percentile",
2173 params->distribution_maxrgb[i].percentile,'/');
2174 }
2175 print_q("fraction_bright_pixels",
2176 params->fraction_bright_pixels,'/');
2177 }
2178 if (metadata->mastering_display_actual_peak_luminance_flag) {
2179 print_int("num_rows_mastering_display_actual_peak_luminance",
2180 metadata->num_rows_mastering_display_actual_peak_luminance);
2181 print_int("num_cols_mastering_display_actual_peak_luminance",
2182 metadata->num_cols_mastering_display_actual_peak_luminance);
2183 for (int i = 0; i < metadata->num_rows_mastering_display_actual_peak_luminance; i++) {
2184 for (int j = 0; j < metadata->num_cols_mastering_display_actual_peak_luminance; j++) {
2185 print_q("mastering_display_actual_peak_luminance",
2186 metadata->mastering_display_actual_peak_luminance[i][j],'/');
2187 }
2188 }
2189 }
2190
2191 for (int n = 0; n < metadata->num_windows; n++) {
2192 const AVHDRPlusColorTransformParams *params = &metadata->params[n];
2193 if (params->tone_mapping_flag) {
2194 print_q("knee_point_x", params->knee_point_x,'/');
2195 print_q("knee_point_y", params->knee_point_y,'/');
2196 print_int("num_bezier_curve_anchors",
2197 params->num_bezier_curve_anchors );
2198 for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
2199 print_q("bezier_curve_anchors",
2200 params->bezier_curve_anchors[i],'/');
2201 }
2202 }
2203 if (params->color_saturation_mapping_flag) {
2204 print_q("color_saturation_weight",
2205 params->color_saturation_weight,'/');
2206 }
2207 }
2208}
2209
2210static void print_dynamic_hdr_vivid(WriterContext *w, const AVDynamicHDRVivid *metadata)
2211{
2212 if (!metadata)
2213 return;
2214 print_int("system_start_code", metadata->system_start_code);
2215 print_int("num_windows", metadata->num_windows);
2216
2217 for (int n = 0; n < metadata->num_windows; n++) {
2218 const AVHDRVividColorTransformParams *params = &metadata->params[n];
2219
2220 print_q("minimum_maxrgb", params->minimum_maxrgb, '/');
2221 print_q("average_maxrgb", params->average_maxrgb, '/');
2222 print_q("variance_maxrgb", params->variance_maxrgb, '/');
2223 print_q("maximum_maxrgb", params->maximum_maxrgb, '/');
2224 }
2225
2226 for (int n = 0; n < metadata->num_windows; n++) {
2227 const AVHDRVividColorTransformParams *params = &metadata->params[n];
2228
2229 print_int("tone_mapping_mode_flag", params->tone_mapping_mode_flag);
2230 print_int("tone_mapping_param_num", params->tone_mapping_param_num);
2231 if (params->tone_mapping_mode_flag) {
2232 for (int i = 0; i < params->tone_mapping_param_num; i++) {
2233 const AVHDRVividColorToneMappingParams *tm_params = &params->tm_params[i];
2234
2235 print_q("targeted_system_display_maximum_luminance",
2236 tm_params->targeted_system_display_maximum_luminance, '/');
2237 print_int("base_enable_flag", tm_params->base_enable_flag);
2238 if (tm_params->base_enable_flag) {
2239 print_q("base_param_m_p", tm_params->base_param_m_p, '/');
2240 print_q("base_param_m_m", tm_params->base_param_m_m, '/');
2241 print_q("base_param_m_a", tm_params->base_param_m_a, '/');
2242 print_q("base_param_m_b", tm_params->base_param_m_b, '/');
2243 print_q("base_param_m_n", tm_params->base_param_m_n, '/');
2244
2245 print_int("base_param_k1", tm_params->base_param_k1);
2246 print_int("base_param_k2", tm_params->base_param_k2);
2247 print_int("base_param_k3", tm_params->base_param_k3);
2248 print_int("base_param_Delta_enable_mode",
2249 tm_params->base_param_Delta_enable_mode);
2250 print_q("base_param_Delta", tm_params->base_param_Delta, '/');
2251 }
2252 print_int("3Spline_enable_flag", tm_params->three_Spline_enable_flag);
2253 if (tm_params->three_Spline_enable_flag) {
2254 print_int("3Spline_num", tm_params->three_Spline_num);
2255 print_int("3Spline_TH_mode", tm_params->three_Spline_TH_mode);
2256
2257 for (int j = 0; j < tm_params->three_Spline_num; j++) {
2258 print_q("3Spline_TH_enable_MB", tm_params->three_Spline_TH_enable_MB, '/');
2259 print_q("3Spline_TH_enable", tm_params->three_Spline_TH_enable, '/');
2260 print_q("3Spline_TH_Delta1", tm_params->three_Spline_TH_Delta1, '/');
2261 print_q("3Spline_TH_Delta2", tm_params->three_Spline_TH_Delta2, '/');
2262 print_q("3Spline_enable_Strength", tm_params->three_Spline_enable_Strength, '/');
2263 }
2264 }
2265 }
2266 }
2267
2268 print_int("color_saturation_mapping_flag", params->color_saturation_mapping_flag);
2269 if (params->color_saturation_mapping_flag) {
2270 print_int("color_saturation_num", params->color_saturation_num);
2271 for (int i = 0; i < params->color_saturation_num; i++) {
2272 print_q("color_saturation_gain", params->color_saturation_gain[i], '/');
2273 }
2274 }
2275 }
2276}
2277
2279 AVCodecParameters *par,
2280 const AVPacketSideData *side_data,
2281 int nb_side_data,
2282 SectionID id_data_list,
2283 SectionID id_data)
2284{
2285 int i;
2286
2287 writer_print_section_header(w, id_data_list);
2288 for (i = 0; i < nb_side_data; i++) {
2289 const AVPacketSideData *sd = &side_data[i];
2290 const char *name = av_packet_side_data_name(sd->type);
2291
2292 writer_print_section_header(w, id_data);
2293 print_str("side_data_type", name ? name : "unknown");
2294 if (sd->type == AV_PKT_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2295 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2296 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
2297 } else if (sd->type == AV_PKT_DATA_STEREO3D) {
2298 const AVStereo3D *stereo = (AVStereo3D *)sd->data;
2299 print_str("type", av_stereo3d_type_name(stereo->type));
2300 print_int("inverted", !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
2301 } else if (sd->type == AV_PKT_DATA_SPHERICAL) {
2302 const AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
2303 print_str("projection", av_spherical_projection_name(spherical->projection));
2304 if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
2305 print_int("padding", spherical->padding);
2306 } else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
2307 size_t l, t, r, b;
2308 av_spherical_tile_bounds(spherical, par->width, par->height,
2309 &l, &t, &r, &b);
2310 print_int("bound_left", l);
2311 print_int("bound_top", t);
2312 print_int("bound_right", r);
2313 print_int("bound_bottom", b);
2314 }
2315
2316 print_int("yaw", (double) spherical->yaw / (1 << 16));
2317 print_int("pitch", (double) spherical->pitch / (1 << 16));
2318 print_int("roll", (double) spherical->roll / (1 << 16));
2319 } else if (sd->type == AV_PKT_DATA_SKIP_SAMPLES && sd->size == 10) {
2320 print_int("skip_samples", AV_RL32(sd->data));
2321 print_int("discard_padding", AV_RL32(sd->data + 4));
2322 print_int("skip_reason", AV_RL8(sd->data + 8));
2323 print_int("discard_reason", AV_RL8(sd->data + 9));
2324 } else if (sd->type == AV_PKT_DATA_MASTERING_DISPLAY_METADATA) {
2325 AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2326
2327 if (metadata->has_primaries) {
2328 print_q("red_x", metadata->display_primaries[0][0], '/');
2329 print_q("red_y", metadata->display_primaries[0][1], '/');
2330 print_q("green_x", metadata->display_primaries[1][0], '/');
2331 print_q("green_y", metadata->display_primaries[1][1], '/');
2332 print_q("blue_x", metadata->display_primaries[2][0], '/');
2333 print_q("blue_y", metadata->display_primaries[2][1], '/');
2334
2335 print_q("white_point_x", metadata->white_point[0], '/');
2336 print_q("white_point_y", metadata->white_point[1], '/');
2337 }
2338
2339 if (metadata->has_luminance) {
2340 print_q("min_luminance", metadata->min_luminance, '/');
2341 print_q("max_luminance", metadata->max_luminance, '/');
2342 }
2343 } else if (sd->type == AV_PKT_DATA_CONTENT_LIGHT_LEVEL) {
2344 AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2345 print_int("max_content", metadata->MaxCLL);
2346 print_int("max_average", metadata->MaxFALL);
2347 } else if (sd->type == AV_PKT_DATA_DOVI_CONF) {
2348 AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)sd->data;
2349 print_int("dv_version_major", dovi->dv_version_major);
2350 print_int("dv_version_minor", dovi->dv_version_minor);
2351 print_int("dv_profile", dovi->dv_profile);
2352 print_int("dv_level", dovi->dv_level);
2353 print_int("rpu_present_flag", dovi->rpu_present_flag);
2354 print_int("el_present_flag", dovi->el_present_flag);
2355 print_int("bl_present_flag", dovi->bl_present_flag);
2356 print_int("dv_bl_signal_compatibility_id", dovi->dv_bl_signal_compatibility_id);
2357 } else if (sd->type == AV_PKT_DATA_AUDIO_SERVICE_TYPE) {
2358 enum AVAudioServiceType *t = (enum AVAudioServiceType *)sd->data;
2359 print_int("service_type", *t);
2360 } else if (sd->type == AV_PKT_DATA_MPEGTS_STREAM_ID) {
2361 print_int("id", *sd->data);
2362 } else if (sd->type == AV_PKT_DATA_CPB_PROPERTIES) {
2363 const AVCPBProperties *prop = (AVCPBProperties *)sd->data;
2364 print_int("max_bitrate", prop->max_bitrate);
2365 print_int("min_bitrate", prop->min_bitrate);
2366 print_int("avg_bitrate", prop->avg_bitrate);
2367 print_int("buffer_size", prop->buffer_size);
2368 print_int("vbv_delay", prop->vbv_delay);
2369 } else if (sd->type == AV_PKT_DATA_WEBVTT_IDENTIFIER ||
2370 sd->type == AV_PKT_DATA_WEBVTT_SETTINGS) {
2371 if (do_show_data)
2372 writer_print_data(w, "data", sd->data, sd->size);
2373 writer_print_data_hash(w, "data_hash", sd->data, sd->size);
2374 } else if (sd->type == AV_PKT_DATA_AFD && sd->size > 0) {
2375 print_int("active_format", *sd->data);
2376 }
2378 }
2380}
2381
2382static void print_color_range(WriterContext *w, enum AVColorRange color_range)
2383{
2384 const char *val = av_color_range_name(color_range);
2385 if (!val || color_range == AVCOL_RANGE_UNSPECIFIED) {
2386 print_str_opt("color_range", "unknown");
2387 } else {
2388 print_str("color_range", val);
2389 }
2390}
2391
2392static void print_color_space(WriterContext *w, enum AVColorSpace color_space)
2393{
2394 const char *val = av_color_space_name(color_space);
2395 if (!val || color_space == AVCOL_SPC_UNSPECIFIED) {
2396 print_str_opt("color_space", "unknown");
2397 } else {
2398 print_str("color_space", val);
2399 }
2400}
2401
2402static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
2403{
2404 const char *val = av_color_primaries_name(color_primaries);
2405 if (!val || color_primaries == AVCOL_PRI_UNSPECIFIED) {
2406 print_str_opt("color_primaries", "unknown");
2407 } else {
2408 print_str("color_primaries", val);
2409 }
2410}
2411
2412static void print_color_trc(WriterContext *w, enum AVColorTransferCharacteristic color_trc)
2413{
2414 const char *val = av_color_transfer_name(color_trc);
2415 if (!val || color_trc == AVCOL_TRC_UNSPECIFIED) {
2416 print_str_opt("color_transfer", "unknown");
2417 } else {
2418 print_str("color_transfer", val);
2419 }
2420}
2421
2422static void print_chroma_location(WriterContext *w, enum AVChromaLocation chroma_location)
2423{
2424 const char *val = av_chroma_location_name(chroma_location);
2425 if (!val || chroma_location == AVCHROMA_LOC_UNSPECIFIED) {
2426 print_str_opt("chroma_location", "unspecified");
2427 } else {
2428 print_str("chroma_location", val);
2429 }
2430}
2431
2432
2433static void clear_log(int need_lock)
2434{
2435 int i;
2436
2437 if (need_lock)
2438 pthread_mutex_lock(&log_mutex);
2439 for (i=0; i<log_buffer_size; i++) {
2440 av_freep(&log_buffer[i].context_name);
2441 av_freep(&log_buffer[i].parent_name);
2442 av_freep(&log_buffer[i].log_message);
2443 }
2444 log_buffer_size = 0;
2445 if(need_lock)
2446 pthread_mutex_unlock(&log_mutex);
2447}
2448
2449static int show_log(WriterContext *w, int section_ids, int section_id, int log_level)
2450{
2451 int i;
2452 pthread_mutex_lock(&log_mutex);
2453 if (!log_buffer_size) {
2454 pthread_mutex_unlock(&log_mutex);
2455 return 0;
2456 }
2457 writer_print_section_header(w, section_ids);
2458
2459 for (i=0; i<log_buffer_size; i++) {
2460 if (log_buffer[i].log_level <= log_level) {
2461 writer_print_section_header(w, section_id);
2462 print_str("context", log_buffer[i].context_name);
2463 print_int("level", log_buffer[i].log_level);
2464 print_int("category", log_buffer[i].category);
2465 if (log_buffer[i].parent_name) {
2466 print_str("parent_context", log_buffer[i].parent_name);
2467 print_int("parent_category", log_buffer[i].parent_category);
2468 } else {
2469 print_str_opt("parent_context", "N/A");
2470 print_str_opt("parent_category", "N/A");
2471 }
2472 print_str("message", log_buffer[i].log_message);
2474 }
2475 }
2476 clear_log(0);
2477 pthread_mutex_unlock(&log_mutex);
2478
2480
2481 return 0;
2482}
2483
2484static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int packet_idx)
2485{
2486 char val_str[128];
2487 AVStream *st = ifile->streams[pkt->stream_index].st;
2488 AVBPrint pbuf;
2489 const char *s;
2490
2491 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2492
2494
2495 s = av_get_media_type_string(st->codecpar->codec_type);
2496 if (s) print_str ("codec_type", s);
2497 else print_str_opt("codec_type", "unknown");
2498 print_int("stream_index", pkt->stream_index);
2499 print_ts ("pts", pkt->pts);
2500 print_time("pts_time", pkt->pts, &st->time_base);
2501 print_ts ("dts", pkt->dts);
2502 print_time("dts_time", pkt->dts, &st->time_base);
2503 print_duration_ts("duration", pkt->duration);
2504 print_duration_time("duration_time", pkt->duration, &st->time_base);
2505 print_val("size", pkt->size, unit_byte_str);
2506 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
2507 else print_str_opt("pos", "N/A");
2508 print_fmt("flags", "%c%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_',
2509 pkt->flags & AV_PKT_FLAG_DISCARD ? 'D' : '_');
2510
2511 if (pkt->side_data_elems) {
2512 size_t size;
2513 const uint8_t *side_metadata;
2514
2515 side_metadata = av_packet_get_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, &size);
2516 if (side_metadata && size && do_show_packet_tags) {
2517 AVDictionary *dict = NULL;
2518 if (av_packet_unpack_dictionary(side_metadata, size, &dict) >= 0)
2520 av_dict_free(&dict);
2521 }
2522
2523 print_pkt_side_data(w, st->codecpar, pkt->side_data, pkt->side_data_elems,
2526 }
2527
2528 if (do_show_data)
2529 writer_print_data(w, "data", pkt->data, pkt->size);
2530 writer_print_data_hash(w, "data_hash", pkt->data, pkt->size);
2532
2533 av_bprint_finalize(&pbuf, NULL);
2534 fflush(stdout);
2535}
2536
2537static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
2538 AVFormatContext *fmt_ctx)
2539{
2540 AVBPrint pbuf;
2541
2542 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2543
2545
2546 print_str ("media_type", "subtitle");
2547 print_ts ("pts", sub->pts);
2548 print_time("pts_time", sub->pts, &AV_TIME_BASE_Q);
2549 print_int ("format", sub->format);
2550 print_int ("start_display_time", sub->start_display_time);
2551 print_int ("end_display_time", sub->end_display_time);
2552 print_int ("num_rects", sub->num_rects);
2553
2555
2556 av_bprint_finalize(&pbuf, NULL);
2557 fflush(stdout);
2558}
2559
2560static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
2561 AVFormatContext *fmt_ctx)
2562{
2563 AVBPrint pbuf;
2564 char val_str[128];
2565 const char *s;
2566 int i;
2567
2568 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2569
2571
2572 s = av_get_media_type_string(stream->codecpar->codec_type);
2573 if (s) print_str ("media_type", s);
2574 else print_str_opt("media_type", "unknown");
2575 print_int("stream_index", stream->index);
2576 print_int("key_frame", frame->key_frame);
2577 print_ts ("pts", frame->pts);
2578 print_time("pts_time", frame->pts, &stream->time_base);
2579 print_ts ("pkt_dts", frame->pkt_dts);
2580 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
2581 print_ts ("best_effort_timestamp", frame->best_effort_timestamp);
2582 print_time("best_effort_timestamp_time", frame->best_effort_timestamp, &stream->time_base);
2583 print_duration_ts ("pkt_duration", frame->pkt_duration);
2584 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
2585 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
2586 else print_str_opt("pkt_pos", "N/A");
2587 if (frame->pkt_size != -1) print_val ("pkt_size", frame->pkt_size, unit_byte_str);
2588 else print_str_opt("pkt_size", "N/A");
2589
2590 switch (stream->codecpar->codec_type) {
2591 AVRational sar;
2592
2593 case AVMEDIA_TYPE_VIDEO:
2594 print_int("width", frame->width);
2595 print_int("height", frame->height);
2596 s = av_get_pix_fmt_name(frame->format);
2597 if (s) print_str ("pix_fmt", s);
2598 else print_str_opt("pix_fmt", "unknown");
2599 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
2600 if (sar.num) {
2601 print_q("sample_aspect_ratio", sar, ':');
2602 } else {
2603 print_str_opt("sample_aspect_ratio", "N/A");
2604 }
2605 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
2606 print_int("coded_picture_number", frame->coded_picture_number);
2607 print_int("display_picture_number", frame->display_picture_number);
2608 print_int("interlaced_frame", frame->interlaced_frame);
2609 print_int("top_field_first", frame->top_field_first);
2610 print_int("repeat_pict", frame->repeat_pict);
2611
2612 print_color_range(w, frame->color_range);
2613 print_color_space(w, frame->colorspace);
2614 print_primaries(w, frame->color_primaries);
2615 print_color_trc(w, frame->color_trc);
2616 print_chroma_location(w, frame->chroma_location);
2617 break;
2618
2619 case AVMEDIA_TYPE_AUDIO:
2620 s = av_get_sample_fmt_name(frame->format);
2621 if (s) print_str ("sample_fmt", s);
2622 else print_str_opt("sample_fmt", "unknown");
2623 print_int("nb_samples", frame->nb_samples);
2624 print_int("channels", frame->ch_layout.nb_channels);
2625 if (frame->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
2626 av_channel_layout_describe(&frame->ch_layout, val_str, sizeof(val_str));
2627 print_str ("channel_layout", val_str);
2628 } else
2629 print_str_opt("channel_layout", "unknown");
2630 break;
2631 }
2633 show_tags(w, frame->metadata, SECTION_ID_FRAME_TAGS);
2634 if (do_show_log)
2636 if (frame->nb_side_data) {
2638 for (i = 0; i < frame->nb_side_data; i++) {
2639 AVFrameSideData *sd = frame->side_data[i];
2640 const char *name;
2641
2643 name = av_frame_side_data_name(sd->type);
2644 print_str("side_data_type", name ? name : "unknown");
2645 if (sd->type == AV_FRAME_DATA_DISPLAYMATRIX && sd->size >= 9*4) {
2646 writer_print_integers(w, "displaymatrix", sd->data, 9, " %11d", 3, 4, 1);
2647 print_int("rotation", av_display_rotation_get((int32_t *)sd->data));
2648 } else if (sd->type == AV_FRAME_DATA_AFD && sd->size > 0) {
2649 print_int("active_format", *sd->data);
2650 } else if (sd->type == AV_FRAME_DATA_GOP_TIMECODE && sd->size >= 8) {
2651 char tcbuf[AV_TIMECODE_STR_SIZE];
2652 av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
2653 print_str("timecode", tcbuf);
2654 } else if (sd->type == AV_FRAME_DATA_S12M_TIMECODE && sd->size == 16) {
2655 uint32_t *tc = (uint32_t*)sd->data;
2656 int m = FFMIN(tc[0],3);
2658 for (int j = 1; j <= m ; j++) {
2659 char tcbuf[AV_TIMECODE_STR_SIZE];
2660 av_timecode_make_smpte_tc_string2(tcbuf, stream->avg_frame_rate, tc[j], 0, 0);
2662 print_str("value", tcbuf);
2664 }
2666 } else if (sd->type == AV_FRAME_DATA_MASTERING_DISPLAY_METADATA) {
2667 AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
2668
2669 if (metadata->has_primaries) {
2670 print_q("red_x", metadata->display_primaries[0][0], '/');
2671 print_q("red_y", metadata->display_primaries[0][1], '/');
2672 print_q("green_x", metadata->display_primaries[1][0], '/');
2673 print_q("green_y", metadata->display_primaries[1][1], '/');
2674 print_q("blue_x", metadata->display_primaries[2][0], '/');
2675 print_q("blue_y", metadata->display_primaries[2][1], '/');
2676
2677 print_q("white_point_x", metadata->white_point[0], '/');
2678 print_q("white_point_y", metadata->white_point[1], '/');
2679 }
2680
2681 if (metadata->has_luminance) {
2682 print_q("min_luminance", metadata->min_luminance, '/');
2683 print_q("max_luminance", metadata->max_luminance, '/');
2684 }
2685 } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS) {
2686 AVDynamicHDRPlus *metadata = (AVDynamicHDRPlus *)sd->data;
2687 print_dynamic_hdr10_plus(w, metadata);
2688 } else if (sd->type == AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) {
2689 AVContentLightMetadata *metadata = (AVContentLightMetadata *)sd->data;
2690 print_int("max_content", metadata->MaxCLL);
2691 print_int("max_average", metadata->MaxFALL);
2692 } else if (sd->type == AV_FRAME_DATA_ICC_PROFILE) {
2693 const AVDictionaryEntry *tag = av_dict_get(sd->metadata, "name", NULL, AV_DICT_MATCH_CASE);
2694 if (tag)
2695 print_str(tag->key, tag->value);
2696 print_int("size", sd->size);
2697 } else if (sd->type == AV_FRAME_DATA_DOVI_METADATA) {
2698 print_dovi_metadata(w, (const AVDOVIMetadata *)sd->data);
2699 } else if (sd->type == AV_FRAME_DATA_DYNAMIC_HDR_VIVID) {
2700 AVDynamicHDRVivid *metadata = (AVDynamicHDRVivid *)sd->data;
2701 print_dynamic_hdr_vivid(w, metadata);
2702 }
2704 }
2706 }
2707
2709
2710 av_bprint_finalize(&pbuf, NULL);
2711 fflush(stdout);
2712}
2713
2714static av_always_inline int process_frame(WriterContext *w,
2715 InputFile *ifile,
2716 AVFrame *frame, AVPacket *pkt,
2717 int *packet_new)
2718{
2719 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2720 AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
2721 AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
2722 AVSubtitle sub;
2723 int ret = 0, got_frame = 0;
2724
2725 clear_log(1);
2726 if (dec_ctx) {
2727 switch (par->codec_type) {
2728 case AVMEDIA_TYPE_VIDEO:
2729 case AVMEDIA_TYPE_AUDIO:
2730 if (*packet_new) {
2731 ret = avcodec_send_packet(dec_ctx, pkt);
2732 if (ret == AVERROR(EAGAIN)) {
2733 ret = 0;
2734 } else if (ret >= 0 || ret == AVERROR_EOF) {
2735 ret = 0;
2736 *packet_new = 0;
2737 }
2738 }
2739 if (ret >= 0) {
2740 ret = avcodec_receive_frame(dec_ctx, frame);
2741 if (ret >= 0) {
2742 got_frame = 1;
2743 } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2744 ret = 0;
2745 }
2746 }
2747 break;
2748
2749 case AVMEDIA_TYPE_SUBTITLE:
2750 if (*packet_new)
2751 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
2752 *packet_new = 0;
2753 break;
2754 default:
2755 *packet_new = 0;
2756 }
2757 } else {
2758 *packet_new = 0;
2759 }
2760
2761 if (ret < 0)
2762 return ret;
2763 if (got_frame) {
2764 int is_sub = (par->codec_type == AVMEDIA_TYPE_SUBTITLE);
2765 nb_streams_frames[pkt->stream_index]++;
2766 if (do_show_frames) {
2767 if (is_sub) {
2768 show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
2769 } else {
2770 show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
2771 }
2772 }
2773 if (is_sub) {
2774 avsubtitle_free(&sub);
2775 }
2776 }
2777 return got_frame || *packet_new;
2778}
2779
2780static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
2781{
2782 av_log(log_ctx, log_level, "id:%d", interval->id);
2783
2784 if (interval->has_start) {
2785 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
2786 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
2787 } else {
2788 av_log(log_ctx, log_level, " start:N/A");
2789 }
2790
2791 if (interval->has_end) {
2792 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
2793 if (interval->duration_frames)
2794 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
2795 else
2796 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
2797 } else {
2798 av_log(log_ctx, log_level, " end:N/A");
2799 }
2800
2801 av_log(log_ctx, log_level, "\n");
2802}
2803
2805 const ReadInterval *interval, int64_t *cur_ts)
2806{
2807 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2808 AVPacket *pkt = NULL;
2809 AVFrame *frame = NULL;
2810 int ret = 0, i = 0, frame_count = 0;
2811 int64_t start = -INT64_MAX, end = interval->end;
2812 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
2813
2814 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
2815 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
2816
2817 if (interval->has_start) {
2818 int64_t target;
2819 if (interval->start_is_offset) {
2820 if (*cur_ts == AV_NOPTS_VALUE) {
2821 av_log(NULL, AV_LOG_ERROR,
2822 "Could not seek to relative position since current "
2823 "timestamp is not defined\n");
2824 ret = AVERROR(EINVAL);
2825 goto end;
2826 }
2827 target = *cur_ts + interval->start;
2828 } else {
2829 target = interval->start;
2830 }
2831
2832 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
2833 av_ts2timestr(target, &AV_TIME_BASE_Q));
2834 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
2835 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
2836 interval->start, av_err2str(ret));
2837 goto end;
2838 }
2839 }
2840
2841 frame = av_frame_alloc();
2842 if (!frame) {
2843 ret = AVERROR(ENOMEM);
2844 goto end;
2845 }
2846 pkt = av_packet_alloc();
2847 if (!pkt) {
2848 ret = AVERROR(ENOMEM);
2849 goto end;
2850 }
2851 while (!av_read_frame(fmt_ctx, pkt)) {
2852 if (fmt_ctx->nb_streams > nb_streams) {
2853 REALLOCZ_ARRAY_STREAM(nb_streams_frames, nb_streams, fmt_ctx->nb_streams);
2855 REALLOCZ_ARRAY_STREAM(selected_streams, nb_streams, fmt_ctx->nb_streams);
2856 nb_streams = fmt_ctx->nb_streams;
2857 }
2858 if (selected_streams[pkt->stream_index]) {
2859 AVRational tb = ifile->streams[pkt->stream_index].st->time_base;
2860
2861 if (pkt->pts != AV_NOPTS_VALUE)
2862 *cur_ts = av_rescale_q(pkt->pts, tb, AV_TIME_BASE_Q);
2863
2864 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
2865 start = *cur_ts;
2866 has_start = 1;
2867 }
2868
2869 if (has_start && !has_end && interval->end_is_offset) {
2870 end = start + interval->end;
2871 has_end = 1;
2872 }
2873
2874 if (interval->end_is_offset && interval->duration_frames) {
2875 if (frame_count >= interval->end)
2876 break;
2877 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
2878 break;
2879 }
2880
2881 frame_count++;
2882 if (do_read_packets) {
2883 if (do_show_packets)
2884 show_packet(w, ifile, pkt, i++);
2885 nb_streams_packets[pkt->stream_index]++;
2886 }
2887 if (do_read_frames) {
2888 int packet_new = 1;
2889 while (process_frame(w, ifile, frame, pkt, &packet_new) > 0);
2890 }
2891 }
2892 av_packet_unref(pkt);
2893 }
2894 av_packet_unref(pkt);
2895 //Flush remaining frames that are cached in the decoder
2896 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2897 pkt->stream_index = i;
2898 if (do_read_frames) {
2899 while (process_frame(w, ifile, frame, pkt, &(int){1}) > 0);
2900 if (ifile->streams[i].dec_ctx)
2901 avcodec_flush_buffers(ifile->streams[i].dec_ctx);
2902 }
2903 }
2904
2905end:
2906 av_frame_free(&frame);
2907 av_packet_free(&pkt);
2908 if (ret < 0) {
2909 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
2910 log_read_interval(interval, NULL, AV_LOG_ERROR);
2911 }
2912 return ret;
2913}
2914
2916{
2917 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
2918 int i, ret = 0;
2919 int64_t cur_ts = fmt_ctx->start_time;
2920
2921 if (read_intervals_nb == 0) {
2922 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
2923 ret = read_interval_packets(w, ifile, &interval, &cur_ts);
2924 } else {
2925 for (i = 0; i < read_intervals_nb; i++) {
2926 ret = read_interval_packets(w, ifile, &read_intervals[i], &cur_ts);
2927 if (ret < 0)
2928 break;
2929 }
2930 }
2931
2932 return ret;
2933}
2934
2935static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, InputStream *ist, int in_program)
2936{
2937 AVStream *stream = ist->st;
2938 AVCodecParameters *par;
2939 AVCodecContext *dec_ctx;
2940 char val_str[128];
2941 const char *s;
2942 AVRational sar, dar;
2943 AVBPrint pbuf;
2944 const AVCodecDescriptor *cd;
2945 int ret = 0;
2946 const char *profile = NULL;
2947
2948 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2949
2951
2952 print_int("index", stream->index);
2953
2954 par = stream->codecpar;
2955 dec_ctx = ist->dec_ctx;
2956 if ((cd = avcodec_descriptor_get(par->codec_id))) {
2957 print_str("codec_name", cd->name);
2958 if (!do_bitexact) {
2959 print_str("codec_long_name",
2960 cd->long_name ? cd->long_name : "unknown");
2961 }
2962 } else {
2963 print_str_opt("codec_name", "unknown");
2964 if (!do_bitexact) {
2965 print_str_opt("codec_long_name", "unknown");
2966 }
2967 }
2968
2969 if (!do_bitexact && (profile = avcodec_profile_name(par->codec_id, par->profile)))
2970 print_str("profile", profile);
2971 else {
2972 if (par->profile != FF_PROFILE_UNKNOWN) {
2973 char profile_num[12];
2974 snprintf(profile_num, sizeof(profile_num), "%d", par->profile);
2975 print_str("profile", profile_num);
2976 } else
2977 print_str_opt("profile", "unknown");
2978 }
2979
2980 s = av_get_media_type_string(par->codec_type);
2981 if (s) print_str ("codec_type", s);
2982 else print_str_opt("codec_type", "unknown");
2983
2984 /* print AVI/FourCC tag */
2985 print_str("codec_tag_string", av_fourcc2str(par->codec_tag));
2986 print_fmt("codec_tag", "0x%04"PRIx32, par->codec_tag);
2987
2988 switch (par->codec_type) {
2989 case AVMEDIA_TYPE_VIDEO:
2990 print_int("width", par->width);
2991 print_int("height", par->height);
2992 if (dec_ctx) {
2993 print_int("coded_width", dec_ctx->coded_width);
2994 print_int("coded_height", dec_ctx->coded_height);
2995 print_int("closed_captions", !!(dec_ctx->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS));
2996 print_int("film_grain", !!(dec_ctx->properties & FF_CODEC_PROPERTY_FILM_GRAIN));
2997 }
2998 print_int("has_b_frames", par->video_delay);
2999 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
3000 if (sar.num) {
3001 print_q("sample_aspect_ratio", sar, ':');
3002 av_reduce(&dar.num, &dar.den,
3003 par->width * sar.num,
3004 par->height * sar.den,
3005 1024*1024);
3006 print_q("display_aspect_ratio", dar, ':');
3007 } else {
3008 print_str_opt("sample_aspect_ratio", "N/A");
3009 print_str_opt("display_aspect_ratio", "N/A");
3010 }
3011 s = av_get_pix_fmt_name(par->format);
3012 if (s) print_str ("pix_fmt", s);
3013 else print_str_opt("pix_fmt", "unknown");
3014 print_int("level", par->level);
3015
3016 print_color_range(w, par->color_range);
3017 print_color_space(w, par->color_space);
3018 print_color_trc(w, par->color_trc);
3019 print_primaries(w, par->color_primaries);
3020 print_chroma_location(w, par->chroma_location);
3021
3022 if (par->field_order == AV_FIELD_PROGRESSIVE)
3023 print_str("field_order", "progressive");
3024 else if (par->field_order == AV_FIELD_TT)
3025 print_str("field_order", "tt");
3026 else if (par->field_order == AV_FIELD_BB)
3027 print_str("field_order", "bb");
3028 else if (par->field_order == AV_FIELD_TB)
3029 print_str("field_order", "tb");
3030 else if (par->field_order == AV_FIELD_BT)
3031 print_str("field_order", "bt");
3032 else
3033 print_str_opt("field_order", "unknown");
3034
3035 if (dec_ctx)
3036 print_int("refs", dec_ctx->refs);
3037 break;
3038
3039 case AVMEDIA_TYPE_AUDIO:
3040 s = av_get_sample_fmt_name(par->format);
3041 if (s) print_str ("sample_fmt", s);
3042 else print_str_opt("sample_fmt", "unknown");
3043 print_val("sample_rate", par->sample_rate, unit_hertz_str);
3044 print_int("channels", par->ch_layout.nb_channels);
3045
3046 if (par->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
3047 av_channel_layout_describe(&par->ch_layout, val_str, sizeof(val_str));
3048 print_str ("channel_layout", val_str);
3049 } else {
3050 print_str_opt("channel_layout", "unknown");
3051 }
3052
3053 print_int("bits_per_sample", av_get_bits_per_sample(par->codec_id));
3054 break;
3055
3056 case AVMEDIA_TYPE_SUBTITLE:
3057 if (par->width)
3058 print_int("width", par->width);
3059 else
3060 print_str_opt("width", "N/A");
3061 if (par->height)
3062 print_int("height", par->height);
3063 else
3064 print_str_opt("height", "N/A");
3065 break;
3066 }
3067
3068 if (dec_ctx && dec_ctx->codec->priv_class && show_private_data) {
3069 const AVOption *opt = NULL;
3070 while ((opt = av_opt_next(dec_ctx->priv_data,opt))) {
3071 uint8_t *str;
3072 if (!(opt->flags & AV_OPT_FLAG_EXPORT)) continue;
3073 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
3074 print_str(opt->name, str);
3075 av_free(str);
3076 }
3077 }
3078 }
3079
3080 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
3081 else print_str_opt("id", "N/A");
3082 print_q("r_frame_rate", stream->r_frame_rate, '/');
3083 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
3084 print_q("time_base", stream->time_base, '/');
3085 print_ts ("start_pts", stream->start_time);
3086 print_time("start_time", stream->start_time, &stream->time_base);
3087 print_ts ("duration_ts", stream->duration);
3088 print_time("duration", stream->duration, &stream->time_base);
3089 if (par->bit_rate > 0) print_val ("bit_rate", par->bit_rate, unit_bit_per_second_str);
3090 else print_str_opt("bit_rate", "N/A");
3091 if (dec_ctx && dec_ctx->rc_max_rate > 0)
3092 print_val ("max_bit_rate", dec_ctx->rc_max_rate, unit_bit_per_second_str);
3093 else
3094 print_str_opt("max_bit_rate", "N/A");
3095 if (dec_ctx && dec_ctx->bits_per_raw_sample > 0) print_fmt("bits_per_raw_sample", "%d", dec_ctx->bits_per_raw_sample);
3096 else print_str_opt("bits_per_raw_sample", "N/A");
3097 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
3098 else print_str_opt("nb_frames", "N/A");
3099 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
3100 else print_str_opt("nb_read_frames", "N/A");
3101 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
3102 else print_str_opt("nb_read_packets", "N/A");
3103 if (do_show_data)
3104 writer_print_data(w, "extradata", par->extradata,
3105 par->extradata_size);
3106
3107 if (par->extradata_size > 0) {
3108 print_int("extradata_size", par->extradata_size);
3109 writer_print_data_hash(w, "extradata_hash", par->extradata,
3110 par->extradata_size);
3111 }
3112
3113 /* Print disposition information */
3114#define PRINT_DISPOSITION(flagname, name) do { \
3115 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
3116 } while (0)
3117
3120 PRINT_DISPOSITION(DEFAULT, "default");
3121 PRINT_DISPOSITION(DUB, "dub");
3122 PRINT_DISPOSITION(ORIGINAL, "original");
3123 PRINT_DISPOSITION(COMMENT, "comment");
3124 PRINT_DISPOSITION(LYRICS, "lyrics");
3125 PRINT_DISPOSITION(KARAOKE, "karaoke");
3126 PRINT_DISPOSITION(FORCED, "forced");
3127 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
3128 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
3129 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
3130 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
3131 PRINT_DISPOSITION(TIMED_THUMBNAILS, "timed_thumbnails");
3132 PRINT_DISPOSITION(CAPTIONS, "captions");
3133 PRINT_DISPOSITION(DESCRIPTIONS, "descriptions");
3134 PRINT_DISPOSITION(METADATA, "metadata");
3135 PRINT_DISPOSITION(DEPENDENT, "dependent");
3136 PRINT_DISPOSITION(STILL_IMAGE, "still_image");
3138 }
3139
3141 ret = show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
3142
3143 if (stream->nb_side_data) {
3144 print_pkt_side_data(w, stream->codecpar, stream->side_data, stream->nb_side_data,
3147 }
3148
3150 av_bprint_finalize(&pbuf, NULL);
3151 fflush(stdout);
3152
3153 return ret;
3154}
3155
3157{
3158 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3159 int i, ret = 0;
3160
3162 for (i = 0; i < ifile->nb_streams; i++)
3163 if (selected_streams[i]) {
3164 ret = show_stream(w, fmt_ctx, i, &ifile->streams[i], 0);
3165 if (ret < 0)
3166 break;
3167 }
3169
3170 return ret;
3171}
3172
3173static int show_program(WriterContext *w, InputFile *ifile, AVProgram *program)
3174{
3175 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3176 int i, ret = 0;
3177
3179 print_int("program_id", program->id);
3180 print_int("program_num", program->program_num);
3181 print_int("nb_streams", program->nb_stream_indexes);
3182 print_int("pmt_pid", program->pmt_pid);
3183 print_int("pcr_pid", program->pcr_pid);
3185 ret = show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
3186 if (ret < 0)
3187 goto end;
3188
3190 for (i = 0; i < program->nb_stream_indexes; i++) {
3191 if (selected_streams[program->stream_index[i]]) {
3192 ret = show_stream(w, fmt_ctx, program->stream_index[i], &ifile->streams[program->stream_index[i]], 1);
3193 if (ret < 0)
3194 break;
3195 }
3196 }
3198
3199end:
3201 return ret;
3202}
3203
3205{
3206 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3207 int i, ret = 0;
3208
3210 for (i = 0; i < fmt_ctx->nb_programs; i++) {
3211 AVProgram *program = fmt_ctx->programs[i];
3212 if (!program)
3213 continue;
3214 ret = show_program(w, ifile, program);
3215 if (ret < 0)
3216 break;
3217 }
3219 return ret;
3220}
3221
3223{
3224 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3225 int i, ret = 0;
3226
3228 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
3229 AVChapter *chapter = fmt_ctx->chapters[i];
3230
3232 print_int("id", chapter->id);
3233 print_q ("time_base", chapter->time_base, '/');
3234 print_int("start", chapter->start);
3235 print_time("start_time", chapter->start, &chapter->time_base);
3236 print_int("end", chapter->end);
3237 print_time("end_time", chapter->end, &chapter->time_base);
3239 ret = show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
3241 }
3243
3244 return ret;
3245}
3246
3248{
3249 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
3250 char val_str[128];
3251 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
3252 int ret = 0;
3253
3255 print_str_validate("filename", fmt_ctx->url);
3256 print_int("nb_streams", fmt_ctx->nb_streams);
3257 print_int("nb_programs", fmt_ctx->nb_programs);
3258 print_str("format_name", fmt_ctx->iformat->name);
3259 if (!do_bitexact) {
3260 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
3261 else print_str_opt("format_long_name", "unknown");
3262 }
3263 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
3264 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
3265 if (size >= 0) print_val ("size", size, unit_byte_str);
3266 else print_str_opt("size", "N/A");
3267 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
3268 else print_str_opt("bit_rate", "N/A");
3269 print_int("probe_score", fmt_ctx->probe_score);
3271 ret = show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
3272
3274 fflush(stdout);
3275 return ret;
3276}
3277
3278static void show_error(WriterContext *w, int err)
3279{
3280 char errbuf[128];
3281 const char *errbuf_ptr = errbuf;
3282
3283 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
3284 errbuf_ptr = strerror(AVUNERROR(err));
3285
3287 print_int("code", err);
3288 print_str("string", errbuf_ptr);
3290}
3291
3292static int open_input_file(InputFile *ifile, const char *filename,
3293 const char *print_filename)
3294{
3295 int err, i;
3296 AVFormatContext *fmt_ctx = NULL;
3297 const AVDictionaryEntry *t = NULL;
3298 int scan_all_pmts_set = 0;
3299
3300 fmt_ctx = avformat_alloc_context();
3301 if (!fmt_ctx) {
3302 print_error(filename, AVERROR(ENOMEM));
3303 exit_program(1);
3304 }
3305
3306 if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
3307 av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
3308 scan_all_pmts_set = 1;
3309 }
3310 if ((err = avformat_open_input(&fmt_ctx, filename,
3311 iformat, &format_opts)) < 0) {
3312 print_error(filename, err);
3313 return err;
3314 }
3315 if (print_filename) {
3316 av_freep(&fmt_ctx->url);
3317 fmt_ctx->url = av_strdup(print_filename);
3318 }
3319 ifile->fmt_ctx = fmt_ctx;
3320 if (scan_all_pmts_set)
3321 av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
3322 while ((t = av_dict_get(format_opts, "", t, AV_DICT_IGNORE_SUFFIX)))
3323 av_log(NULL, AV_LOG_WARNING, "Option %s skipped - not known to demuxer.\n", t->key);
3324
3325 if (find_stream_info) {
3326 AVDictionary **opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
3327 int orig_nb_streams = fmt_ctx->nb_streams;
3328
3329 err = avformat_find_stream_info(fmt_ctx, opts);
3330
3331 for (i = 0; i < orig_nb_streams; i++)
3332 av_dict_free(&opts[i]);
3333 av_freep(&opts);
3334
3335 if (err < 0) {
3336 print_error(filename, err);
3337 return err;
3338 }
3339 }
3340
3341 av_dump_format(fmt_ctx, 0, filename, 0);
3342
3343 ifile->streams = av_calloc(fmt_ctx->nb_streams, sizeof(*ifile->streams));
3344 if (!ifile->streams)
3345 exit(1);
3346 ifile->nb_streams = fmt_ctx->nb_streams;
3347
3348 /* bind a decoder to each input stream */
3349 for (i = 0; i < fmt_ctx->nb_streams; i++) {
3350 InputStream *ist = &ifile->streams[i];
3351 AVStream *stream = fmt_ctx->streams[i];
3352 const AVCodec *codec;
3353
3354 ist->st = stream;
3355
3356 if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
3357 av_log(NULL, AV_LOG_WARNING,
3358 "Failed to probe codec for input stream %d\n",
3359 stream->index);
3360 continue;
3361 }
3362
3363 codec = avcodec_find_decoder(stream->codecpar->codec_id);
3364 if (!codec) {
3365 av_log(NULL, AV_LOG_WARNING,
3366 "Unsupported codec with id %d for input stream %d\n",
3367 stream->codecpar->codec_id, stream->index);
3368 continue;
3369 }
3370 {
3371 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
3372 fmt_ctx, stream, codec);
3373
3374 ist->dec_ctx = avcodec_alloc_context3(codec);
3375 if (!ist->dec_ctx)
3376 exit(1);
3377
3378 err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
3379 if (err < 0)
3380 exit(1);
3381
3382 if (do_show_log) {
3383 // For loging it is needed to disable at least frame threads as otherwise
3384 // the log information would need to be reordered and matches up to contexts and frames
3385 // That is in fact possible but not trivial
3386 av_dict_set(&codec_opts, "threads", "1", 0);
3387 }
3388
3389 ist->dec_ctx->pkt_timebase = stream->time_base;
3390
3391 if (avcodec_open2(ist->dec_ctx, codec, &opts) < 0) {
3392 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
3393 stream->index);
3394 exit(1);
3395 }
3396
3397 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
3398 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
3399 t->key, stream->index);
3400 return AVERROR_OPTION_NOT_FOUND;
3401 }
3402 }
3403 }
3404
3405 ifile->fmt_ctx = fmt_ctx;
3406 return 0;
3407}
3408
3409static void close_input_file(InputFile *ifile)
3410{
3411 int i;
3412
3413 /* close decoder for each stream */
3414 for (i = 0; i < ifile->nb_streams; i++)
3415 avcodec_free_context(&ifile->streams[i].dec_ctx);
3416
3417 av_freep(&ifile->streams);
3418 ifile->nb_streams = 0;
3419
3420 avformat_close_input(&ifile->fmt_ctx);
3421}
3422
3423static int probe_file(WriterContext *wctx, const char *filename,
3424 const char *print_filename)
3425{
3426 InputFile ifile = { 0 };
3427 int ret, i;
3428 int section_id;
3429
3432
3433 ret = open_input_file(&ifile, filename, print_filename);
3434 if (ret < 0)
3435 goto end;
3436
3437#define CHECK_END if (ret < 0) goto end
3438
3439 nb_streams = ifile.fmt_ctx->nb_streams;
3440 REALLOCZ_ARRAY_STREAM(nb_streams_frames,0,ifile.fmt_ctx->nb_streams);
3442 REALLOCZ_ARRAY_STREAM(selected_streams,0,ifile.fmt_ctx->nb_streams);
3443
3444 for (i = 0; i < ifile.fmt_ctx->nb_streams; i++) {
3445 if (stream_specifier) {
3446 ret = avformat_match_stream_specifier(ifile.fmt_ctx,
3447 ifile.fmt_ctx->streams[i],
3449 CHECK_END;
3450 else
3451 selected_streams[i] = ret;
3452 ret = 0;
3453 } else {
3454 selected_streams[i] = 1;
3455 }
3456 if (!selected_streams[i])
3457 ifile.fmt_ctx->streams[i]->discard = AVDISCARD_ALL;
3458 }
3459
3463 section_id = SECTION_ID_PACKETS_AND_FRAMES;
3464 else if (do_show_packets && !do_show_frames)
3465 section_id = SECTION_ID_PACKETS;
3466 else // (!do_show_packets && do_show_frames)
3467 section_id = SECTION_ID_FRAMES;
3469 writer_print_section_header(wctx, section_id);
3470 ret = read_packets(wctx, &ifile);
3473 CHECK_END;
3474 }
3475
3476 if (do_show_programs) {
3477 ret = show_programs(wctx, &ifile);
3478 CHECK_END;
3479 }
3480
3481 if (do_show_streams) {
3482 ret = show_streams(wctx, &ifile);
3483 CHECK_END;
3484 }
3485 if (do_show_chapters) {
3486 ret = show_chapters(wctx, &ifile);
3487 CHECK_END;
3488 }
3489 if (do_show_format) {
3490 ret = show_format(wctx, &ifile);
3491 CHECK_END;
3492 }
3493
3494end:
3495 if (ifile.fmt_ctx)
3496 close_input_file(&ifile);
3497 av_freep(&nb_streams_frames);
3498 av_freep(&nb_streams_packets);
3499 av_freep(&selected_streams);
3500
3501 return ret;
3502}
3503
3504static void show_usage(void)
3505{
3506 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3507 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name);
3508 av_log(NULL, AV_LOG_INFO, "\n");
3509}
3510
3512{
3513 AVBPrint pbuf;
3514 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
3515
3517 print_str("version", FFMPEG_VERSION);
3518 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3519 program_birth_year, CONFIG_THIS_YEAR);
3520 print_str("compiler_ident", CC_IDENT);
3521 print_str("configuration", FFMPEG_CONFIGURATION);
3523
3524 av_bprint_finalize(&pbuf, NULL);
3525}
3526
3527#define SHOW_LIB_VERSION(libname, LIBNAME) \
3528 do { \
3529 if (CONFIG_##LIBNAME) { \
3530 unsigned int version = libname##_version(); \
3531 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
3532 print_str("name", "lib" #libname); \
3533 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
3534 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
3535 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
3536 print_int("version", version); \
3537 print_str("ident", LIB##LIBNAME##_IDENT); \
3538 writer_print_section_footer(w); \
3539 } \
3540 } while (0)
3541
3543{
3545 SHOW_LIB_VERSION(avutil, AVUTIL);
3546 SHOW_LIB_VERSION(avcodec, AVCODEC);
3547 SHOW_LIB_VERSION(avformat, AVFORMAT);
3548 SHOW_LIB_VERSION(avdevice, AVDEVICE);
3549 SHOW_LIB_VERSION(avfilter, AVFILTER);
3550 SHOW_LIB_VERSION(swscale, SWSCALE);
3551 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
3553}
3554
3555#define PRINT_PIX_FMT_FLAG(flagname, name) \
3556 do { \
3557 print_int(name, !!(pixdesc->flags & AV_PIX_FMT_FLAG_##flagname)); \
3558 } while (0)
3559
3561{
3562 const AVPixFmtDescriptor *pixdesc = NULL;
3563 int i, n;
3564
3566 while ((pixdesc = av_pix_fmt_desc_next(pixdesc))) {
3568 print_str("name", pixdesc->name);
3569 print_int("nb_components", pixdesc->nb_components);
3570 if ((pixdesc->nb_components >= 3) && !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB)) {
3571 print_int ("log2_chroma_w", pixdesc->log2_chroma_w);
3572 print_int ("log2_chroma_h", pixdesc->log2_chroma_h);
3573 } else {
3574 print_str_opt("log2_chroma_w", "N/A");
3575 print_str_opt("log2_chroma_h", "N/A");
3576 }
3577 n = av_get_bits_per_pixel(pixdesc);
3578 if (n) print_int ("bits_per_pixel", n);
3579 else print_str_opt("bits_per_pixel", "N/A");
3582 PRINT_PIX_FMT_FLAG(BE, "big_endian");
3583 PRINT_PIX_FMT_FLAG(PAL, "palette");
3584 PRINT_PIX_FMT_FLAG(BITSTREAM, "bitstream");
3585 PRINT_PIX_FMT_FLAG(HWACCEL, "hwaccel");
3586 PRINT_PIX_FMT_FLAG(PLANAR, "planar");
3587 PRINT_PIX_FMT_FLAG(RGB, "rgb");
3588 PRINT_PIX_FMT_FLAG(ALPHA, "alpha");
3590 }
3591 if (do_show_pixel_format_components && (pixdesc->nb_components > 0)) {
3593 for (i = 0; i < pixdesc->nb_components; i++) {
3595 print_int("index", i + 1);
3596 print_int("bit_depth", pixdesc->comp[i].depth);
3598 }
3600 }
3602 }
3604}
3605
3606static int opt_show_optional_fields(void *optctx, const char *opt, const char *arg)
3607{
3608 if (!av_strcasecmp(arg, "always")) show_optional_fields = SHOW_OPTIONAL_FIELDS_ALWAYS;
3609 else if (!av_strcasecmp(arg, "never")) show_optional_fields = SHOW_OPTIONAL_FIELDS_NEVER;
3610 else if (!av_strcasecmp(arg, "auto")) show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
3611
3612 if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto"))
3614 return 0;
3615}
3616
3617static int opt_format(void *optctx, const char *opt, const char *arg)
3618{
3619 iformat = av_find_input_format(arg);
3620 if (!iformat) {
3621 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
3622 return AVERROR(EINVAL);
3623 }
3624 return 0;
3625}
3626
3627static inline void mark_section_show_entries(SectionID section_id,
3628 int show_all_entries, AVDictionary *entries)
3629{
3630 struct section *section = &sections[section_id];
3631
3633 if (show_all_entries) {
3634 SectionID *id;
3635 for (id = section->children_ids; *id != -1; id++)
3637 } else {
3638 av_dict_copy(&section->entries_to_show, entries, 0);
3639 }
3640}
3641
3642static int match_section(const char *section_name,
3643 int show_all_entries, AVDictionary *entries)
3644{
3645 int i, ret = 0;
3646
3647 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
3648 const struct section *section = &sections[i];
3649 if (!strcmp(section_name, section->name) ||
3650 (section->unique_name && !strcmp(section_name, section->unique_name))) {
3651 av_log(NULL, AV_LOG_DEBUG,
3652 "'%s' matches section with unique name '%s'\n", section_name,
3653 (char *)av_x_if_null(section->unique_name, section->name));
3654 ret++;
3656 }
3657 }
3658 return ret;
3659}
3660
3661static int opt_show_entries(void *optctx, const char *opt, const char *arg)
3662{
3663 const char *p = arg;
3664 int ret = 0;
3665
3666 while (*p) {
3667 AVDictionary *entries = NULL;
3668 char *section_name = av_get_token(&p, "=:");
3669 int show_all_entries = 0;
3670
3671 if (!section_name) {
3672 av_log(NULL, AV_LOG_ERROR,
3673 "Missing section name for option '%s'\n", opt);
3674 return AVERROR(EINVAL);
3675 }
3676
3677 if (*p == '=') {
3678 p++;
3679 while (*p && *p != ':') {
3680 char *entry = av_get_token(&p, ",:");
3681 if (!entry)
3682 break;
3683 av_log(NULL, AV_LOG_VERBOSE,
3684 "Adding '%s' to the entries to show in section '%s'\n",
3685 entry, section_name);
3686 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
3687 if (*p == ',')
3688 p++;
3689 }
3690 } else {
3691 show_all_entries = 1;
3692 }
3693
3694 ret = match_section(section_name, show_all_entries, entries);
3695 if (ret == 0) {
3696 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
3697 ret = AVERROR(EINVAL);
3698 }
3699 av_dict_free(&entries);
3700 av_free(section_name);
3701
3702 if (ret <= 0)
3703 break;
3704 if (*p)
3705 p++;
3706 }
3707
3708 return ret;
3709}
3710
3711static void opt_input_file(void *optctx, const char *arg)
3712{
3713 if (input_filename) {
3714 av_log(NULL, AV_LOG_ERROR,
3715 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3716 arg, input_filename);
3717 exit_program(1);
3718 }
3719 if (!strcmp(arg, "-"))
3720 arg = "pipe:";
3721 input_filename = arg;
3722}
3723
3724static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
3725{
3726 opt_input_file(optctx, arg);
3727 return 0;
3728}
3729
3730static void opt_output_file(void *optctx, const char *arg)
3731{
3732 if (output_filename) {
3733 av_log(NULL, AV_LOG_ERROR,
3734 "Argument '%s' provided as output filename, but '%s' was already specified.\n",
3735 arg, output_filename);
3736 exit_program(1);
3737 }
3738 if (!strcmp(arg, "-"))
3739 arg = "pipe:";
3740 output_filename = arg;
3741}
3742
3743static int opt_output_file_o(void *optctx, const char *opt, const char *arg)
3744{
3745 opt_output_file(optctx, arg);
3746 return 0;
3747}
3748
3749static int opt_print_filename(void *optctx, const char *opt, const char *arg)
3750{
3752 return 0;
3753}
3754
3755void show_help_default_ffprobe(const char *opt, const char *arg)
3756{
3757 show_usage();
3758 show_help_options(ffprobe_options, "Main options:", 0, 0, 0);
3759 av_log(NULL, AV_LOG_STDERR, "\n");
3760
3761 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3762 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3763}
3764
3770static int parse_read_interval(const char *interval_spec,
3771 ReadInterval *interval)
3772{
3773 int ret = 0;
3774 char *next, *p, *spec = av_strdup(interval_spec);
3775 if (!spec)
3776 return AVERROR(ENOMEM);
3777
3778 if (!*spec) {
3779 av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
3780 ret = AVERROR(EINVAL);
3781 goto end;
3782 }
3783
3784 p = spec;
3785 next = strchr(spec, '%');
3786 if (next)
3787 *next++ = 0;
3788
3789 /* parse first part */
3790 if (*p) {
3791 interval->has_start = 1;
3792
3793 if (*p == '+') {
3794 interval->start_is_offset = 1;
3795 p++;
3796 } else {
3797 interval->start_is_offset = 0;
3798 }
3799
3800 ret = av_parse_time(&interval->start, p, 1);
3801 if (ret < 0) {
3802 av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
3803 goto end;
3804 }
3805 } else {
3806 interval->has_start = 0;
3807 }
3808
3809 /* parse second part */
3810 p = next;
3811 if (p && *p) {
3812 int64_t us;
3813 interval->has_end = 1;
3814
3815 if (*p == '+') {
3816 interval->end_is_offset = 1;
3817 p++;
3818 } else {
3819 interval->end_is_offset = 0;
3820 }
3821
3822 if (interval->end_is_offset && *p == '#') {
3823 long long int lli;
3824 char *tail;
3825 interval->duration_frames = 1;
3826 p++;
3827 lli = strtoll(p, &tail, 10);
3828 if (*tail || lli < 0) {
3829 av_log(NULL, AV_LOG_ERROR,
3830 "Invalid or negative value '%s' for duration number of frames\n", p);
3831 goto end;
3832 }
3833 interval->end = lli;
3834 } else {
3835 interval->duration_frames = 0;
3836 ret = av_parse_time(&us, p, 1);
3837 if (ret < 0) {
3838 av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
3839 goto end;
3840 }
3841 interval->end = us;
3842 }
3843 } else {
3844 interval->has_end = 0;
3845 }
3846
3847end:
3848 av_free(spec);
3849 return ret;
3850}
3851
3852static int parse_read_intervals(const char *intervals_spec)
3853{
3854 int ret, n, i;
3855 char *p, *spec = av_strdup(intervals_spec);
3856 if (!spec)
3857 return AVERROR(ENOMEM);
3858
3859 /* preparse specification, get number of intervals */
3860 for (n = 0, p = spec; *p; p++)
3861 if (*p == ',')
3862 n++;
3863 n++;
3864
3865 read_intervals = av_malloc_array(n, sizeof(*read_intervals));
3866 if (!read_intervals) {
3867 ret = AVERROR(ENOMEM);
3868 goto end;
3869 }
3871
3872 /* parse intervals */
3873 p = spec;
3874 for (i = 0; p; i++) {
3875 char *next;
3876
3877 av_assert0(i < read_intervals_nb);
3878 next = strchr(p, ',');
3879 if (next)
3880 *next++ = 0;
3881
3882 read_intervals[i].id = i;
3883 ret = parse_read_interval(p, &read_intervals[i]);
3884 if (ret < 0) {
3885 av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
3886 i, p);
3887 goto end;
3888 }
3889 av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
3890 log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
3891 p = next;
3892 }
3893 av_assert0(i == read_intervals_nb);
3894
3895end:
3896 av_free(spec);
3897 return ret;
3898}
3899
3900static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
3901{
3902 return parse_read_intervals(arg);
3903}
3904
3905static int opt_pretty(void *optctx, const char *opt, const char *arg)
3906{
3907 show_value_unit = 1;
3908 use_value_prefix = 1;
3911 return 0;
3912}
3913
3914static void print_section(SectionID id, int level)
3915{
3916 const SectionID *pid;
3917 const struct section *section = &sections[id];
3918 av_log(NULL, AV_LOG_STDERR, "%c%c%c",
3919 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
3920 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
3922 av_log(NULL, AV_LOG_STDERR, "%*c %s", level * 4, ' ', section->name);
3923 if (section->unique_name)
3924 av_log(NULL, AV_LOG_STDERR, "/%s", section->unique_name);
3925 av_log(NULL, AV_LOG_STDERR, "\n");
3926
3927 for (pid = section->children_ids; *pid != -1; pid++)
3928 print_section(*pid, level+1);
3929}
3930
3931static int opt_sections(void *optctx, const char *opt, const char *arg)
3932{
3933 av_log(NULL, AV_LOG_STDERR, "Sections:\n"
3934 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
3935 ".A. = Section contains an array of elements of the same type\n"
3936 "..V = Section may contain a variable number of fields with variable keys\n"
3937 "FLAGS NAME/UNIQUE_NAME\n"
3938 "---\n");
3940 return 0;
3941}
3942
3943static int opt_show_versions(void *optctx, const char *opt, const char *arg)
3944{
3947 return 0;
3948}
3949
3950#define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
3951 static int opt_show_##section(void *optctx, const char *opt, const char *arg) \
3952 { \
3953 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
3954 return 0; \
3955 }
3956
3957DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS)
3958DEFINE_OPT_SHOW_SECTION(error, ERROR)
3959DEFINE_OPT_SHOW_SECTION(format, FORMAT)
3960DEFINE_OPT_SHOW_SECTION(frames, FRAMES)
3961DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS)
3962DEFINE_OPT_SHOW_SECTION(packets, PACKETS)
3963DEFINE_OPT_SHOW_SECTION(pixel_formats, PIXEL_FORMATS)
3964DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION)
3965DEFINE_OPT_SHOW_SECTION(streams, STREAMS)
3966DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS)
3967
3968static inline int check_section_show_entries(int section_id)
3969{
3970 int *id;
3971 struct section *section = &sections[section_id];
3972 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
3973 return 1;
3974 for (id = section->children_ids; *id != -1; id++)
3976 return 1;
3977 return 0;
3978}
3979
3980#define SET_DO_SHOW(id, varname) do { \
3981 if (check_section_show_entries(SECTION_ID_##id)) \
3982 do_show_##varname = 1; \
3983 } while (0)
3984
3987 longjmp_value = 0;
3988
3989 do_bitexact = 0;
3990 do_count_frames = 0;
3991 do_count_packets = 0;
3992 do_read_frames = 0;
3993 do_read_packets = 0;
3994 do_show_chapters = 0;
3995 do_show_error = 0;
3996 do_show_format = 0;
3997 do_show_frames = 0;
3998 do_show_packets = 0;
3999 do_show_programs = 0;
4000 do_show_streams = 0;
4002 do_show_data = 0;
4008 do_show_log = 0;
4009
4016
4017 show_value_unit = 0;
4018 use_value_prefix = 0;
4022
4023 print_format = NULL;
4024 stream_specifier = NULL;
4025 show_data_hash = NULL;
4026
4027 read_intervals = NULL;
4029 find_stream_info = 1;
4030
4031 ffprobe_options = NULL;
4032
4033 input_filename = NULL;
4034 print_input_filename = NULL;
4035 iformat = NULL;
4036
4037 hash = NULL;
4038
4040
4041 nb_streams = 0;
4042 nb_streams_packets = NULL;
4043 nb_streams_frames = NULL;
4044 selected_streams = NULL;
4045
4046 log_buffer = NULL;
4047 log_buffer_size = 0;
4048}
4049
4050int ffprobe_execute(int argc, char **argv)
4051{
4052 char _program_name[] = "ffprobe";
4053 program_name = (char*)&_program_name;
4054 program_birth_year = 2007;
4055
4056 OptionDef options[] = {
4057 { "L", OPT_EXIT, { .func_arg = show_license }, "show license" },
4058 { "h", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
4059 { "?", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
4060 { "help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
4061 { "-help", OPT_EXIT, { .func_arg = show_help }, "show help", "topic" },
4062 { "version", OPT_EXIT, { .func_arg = show_version }, "show version" },
4063 { "buildconf", OPT_EXIT, { .func_arg = show_buildconf }, "show build configuration" },
4064 { "formats", OPT_EXIT, { .func_arg = show_formats }, "show available formats" },
4065 { "muxers", OPT_EXIT, { .func_arg = show_muxers }, "show available muxers" },
4066 { "demuxers", OPT_EXIT, { .func_arg = show_demuxers }, "show available demuxers" },
4067 { "devices", OPT_EXIT, { .func_arg = show_devices }, "show available devices" },
4068 { "codecs", OPT_EXIT, { .func_arg = show_codecs }, "show available codecs" },
4069 { "decoders", OPT_EXIT, { .func_arg = show_decoders }, "show available decoders" },
4070 { "encoders", OPT_EXIT, { .func_arg = show_encoders }, "show available encoders" },
4071 { "bsfs", OPT_EXIT, { .func_arg = show_bsfs }, "show available bit stream filters" },
4072 { "protocols", OPT_EXIT, { .func_arg = show_protocols }, "show available protocols" },
4073 { "filters", OPT_EXIT, { .func_arg = show_filters }, "show available filters" },
4074 { "pix_fmts", OPT_EXIT, { .func_arg = show_pix_fmts }, "show available pixel formats" },
4075 { "layouts", OPT_EXIT, { .func_arg = show_layouts }, "show standard channel layouts" },
4076 { "sample_fmts", OPT_EXIT, { .func_arg = show_sample_fmts }, "show available audio sample formats" },
4077 { "dispositions", OPT_EXIT, { .func_arg = show_dispositions}, "show available stream dispositions" },
4078 { "colors", OPT_EXIT, { .func_arg = show_colors }, "show available color names" },
4079 { "loglevel", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" },
4080 { "v", HAS_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" },
4081 { "report", 0, { .func_arg = opt_report }, "generate a report" },
4082 { "max_alloc", HAS_ARG, { .func_arg = opt_max_alloc }, "set maximum size of a single allocated block", "bytes" },
4083 { "cpuflags", HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "force specific cpu flags", "flags" },
4084 { "cpucount", HAS_ARG | OPT_EXPERT, { .func_arg = opt_cpucount }, "force specific cpu count", "count" },
4085 { "hide_banner", OPT_BOOL | OPT_EXPERT, {&hide_banner}, "do not show program banner", "hide_banner" },
4086
4087 #if CONFIG_AVDEVICE
4088 { "sources" , OPT_EXIT | HAS_ARG, { .func_arg = show_sources },
4089 "list sources of the input device", "device" },
4090 { "sinks" , OPT_EXIT | HAS_ARG, { .func_arg = show_sinks },
4091 "list sinks of the output device", "device" },
4092 #endif
4093
4094 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
4095 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
4096 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
4097 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
4098 "use binary prefixes for byte units" },
4099 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
4100 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
4101 { "pretty", 0, {.func_arg = opt_pretty},
4102 "prettify the format of displayed values, make it more human readable" },
4103 { "print_format", OPT_STRING | HAS_ARG, { &print_format },
4104 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
4105 { "of", OPT_STRING | HAS_ARG, { &print_format }, "alias for -print_format", "format" },
4106 { "select_streams", OPT_STRING | HAS_ARG, { &stream_specifier }, "select the specified streams", "stream_specifier" },
4107 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
4108 { "show_data", OPT_BOOL, { &do_show_data }, "show packets data" },
4109 { "show_data_hash", OPT_STRING | HAS_ARG, { &show_data_hash }, "show packets data hash" },
4110 { "show_error", 0, { .func_arg = &opt_show_error }, "show probing error" },
4111 { "show_format", 0, { .func_arg = &opt_show_format }, "show format/container info" },
4112 { "show_frames", 0, { .func_arg = &opt_show_frames }, "show frames info" },
4113 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
4114 "show a set of specified entries", "entry_list" },
4115 #if HAVE_THREADS
4116 { "show_log", OPT_INT|HAS_ARG, { &do_show_log }, "show log" },
4117 #endif
4118 { "show_packets", 0, { .func_arg = &opt_show_packets }, "show packets info" },
4119 { "show_programs", 0, { .func_arg = &opt_show_programs }, "show programs info" },
4120 { "show_streams", 0, { .func_arg = &opt_show_streams }, "show streams info" },
4121 { "show_chapters", 0, { .func_arg = &opt_show_chapters }, "show chapters info" },
4122 { "count_frames", OPT_BOOL, { &do_count_frames }, "count the number of frames per stream" },
4123 { "count_packets", OPT_BOOL, { &do_count_packets }, "count the number of packets per stream" },
4124 { "show_program_version", 0, { .func_arg = &opt_show_program_version }, "show ffprobe version" },
4125 { "show_library_versions", 0, { .func_arg = &opt_show_library_versions }, "show library versions" },
4126 { "show_versions", 0, { .func_arg = &opt_show_versions }, "show program and library versions" },
4127 { "show_pixel_formats", 0, { .func_arg = &opt_show_pixel_formats }, "show pixel format descriptions" },
4128 { "show_optional_fields", HAS_ARG, { .func_arg = &opt_show_optional_fields }, "show optional fields" },
4129 { "show_private_data", OPT_BOOL, { &show_private_data }, "show private data" },
4130 { "private", OPT_BOOL, { &show_private_data }, "same as show_private_data" },
4131 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
4132 { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
4133 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
4134 { "o", HAS_ARG, {.func_arg = opt_output_file_o}, "write to specified output", "output_file"},
4135 { "print_filename", HAS_ARG, {.func_arg = opt_print_filename}, "override the printed input filename", "print_file"},
4136 { "find_stream_info", OPT_BOOL | OPT_INPUT | OPT_EXPERT, { &find_stream_info },
4137 "read and decode the streams to fill missing information with heuristics" },
4138 { NULL, },
4139 };
4140
4141 const Writer *w;
4142 WriterContext *wctx;
4143 char *buf;
4144 char *w_name = NULL, *w_args = NULL;
4145 int ret, input_ret, i;
4146
4147 int savedCode = setjmp(ex_buf__);
4148 if (savedCode == 0) {
4149
4151
4152 init_dynload();
4153
4154 #if HAVE_THREADS
4155 ret = pthread_mutex_init(&log_mutex, NULL);
4156 if (ret != 0) {
4157 goto end;
4158 }
4159 #endif
4160 av_log_set_flags(AV_LOG_SKIP_REPEATED);
4162
4163 ffprobe_options = options;
4164 parse_loglevel(argc, argv, options);
4165 avformat_network_init();
4166 #if CONFIG_AVDEVICE
4167 avdevice_register_all();
4168 #endif
4169
4170 show_banner(argc, argv, options);
4171 parse_options(NULL, argc, argv, options, opt_input_file);
4172
4173 if (do_show_log)
4174 av_log_set_callback(log_callback);
4175
4176 /* mark things to show, based on -show_entries */
4177 SET_DO_SHOW(CHAPTERS, chapters);
4178 SET_DO_SHOW(ERROR, error);
4179 SET_DO_SHOW(FORMAT, format);
4180 SET_DO_SHOW(FRAMES, frames);
4181 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
4182 SET_DO_SHOW(PACKETS, packets);
4183 SET_DO_SHOW(PIXEL_FORMATS, pixel_formats);
4184 SET_DO_SHOW(PIXEL_FORMAT_FLAGS, pixel_format_flags);
4185 SET_DO_SHOW(PIXEL_FORMAT_COMPONENTS, pixel_format_components);
4186 SET_DO_SHOW(PROGRAM_VERSION, program_version);
4187 SET_DO_SHOW(PROGRAMS, programs);
4188 SET_DO_SHOW(STREAMS, streams);
4189 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
4190 SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
4191
4192 SET_DO_SHOW(CHAPTER_TAGS, chapter_tags);
4193 SET_DO_SHOW(FORMAT_TAGS, format_tags);
4194 SET_DO_SHOW(FRAME_TAGS, frame_tags);
4195 SET_DO_SHOW(PROGRAM_TAGS, program_tags);
4196 SET_DO_SHOW(STREAM_TAGS, stream_tags);
4197 SET_DO_SHOW(PROGRAM_STREAM_TAGS, stream_tags);
4198 SET_DO_SHOW(PACKET_TAGS, packet_tags);
4199
4201 av_log(NULL, AV_LOG_ERROR,
4202 "-bitexact and -show_program_version or -show_library_versions "
4203 "options are incompatible\n");
4204 ret = AVERROR(EINVAL);
4205 goto end;
4206 }
4207
4209
4210 if (!print_format)
4211 print_format = av_strdup("default");
4212 if (!print_format) {
4213 ret = AVERROR(ENOMEM);
4214 goto end;
4215 }
4216 w_name = av_strtok(print_format, "=", &buf);
4217 if (!w_name) {
4218 av_log(NULL, AV_LOG_ERROR,
4219 "No name specified for the output format\n");
4220 ret = AVERROR(EINVAL);
4221 goto end;
4222 }
4223 w_args = buf;
4224
4225 if (show_data_hash) {
4226 if ((ret = av_hash_alloc(&hash, show_data_hash)) < 0) {
4227 if (ret == AVERROR(EINVAL)) {
4228 const char *n;
4229 av_log(NULL, AV_LOG_ERROR,
4230 "Unknown hash algorithm '%s'\nKnown algorithms:",
4232 for (i = 0; (n = av_hash_names(i)); i++)
4233 av_log(NULL, AV_LOG_ERROR, " %s", n);
4234 av_log(NULL, AV_LOG_ERROR, "\n");
4235 }
4236 goto end;
4237 }
4238 }
4239
4240 w = writer_get_by_name(w_name);
4241 if (!w) {
4242 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
4243 ret = AVERROR(EINVAL);
4244 goto end;
4245 }
4246
4247 if ((ret = writer_open(&wctx, w, w_args,
4248 sections, FF_ARRAY_ELEMS(sections), output_filename)) >= 0) {
4249 if (w == &xml_writer)
4250 wctx->string_validation_utf8_flags |= AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES;
4251
4253
4260
4261 if (!input_filename &&
4264 show_usage();
4265 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
4266 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
4267 ret = AVERROR(EINVAL);
4268 } else if (input_filename) {
4270 if (ret < 0 && do_show_error)
4271 show_error(wctx, ret);
4272 }
4273
4274 input_ret = ret;
4275
4277 ret = writer_close(&wctx);
4278 if (ret < 0)
4279 av_log(NULL, AV_LOG_ERROR, "Writing output failed: %s\n", av_err2str(ret));
4280
4281 ret = FFMIN(ret, input_ret);
4282 }
4283
4284 main_ffprobe_return_code = ret < 0;
4285
4286 } else {
4288 }
4289
4290end:
4291 av_freep(&print_format);
4292 av_freep(&read_intervals);
4293 av_hash_freep(&hash);
4294
4295 uninit_opts();
4296 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
4297 av_dict_free(&(sections[i].entries_to_show));
4298
4299 avformat_network_deinit();
4300
4302}
__thread jmp_buf ex_buf__
void exit_program(int ret)
void show_help_children(const AVClass *class, int flags)
__thread AVDictionary * codec_opts
void init_dynload(void)
void show_help_options(const OptionDef *options, const char *msg, int req_flags, int rej_flags, int alt_flags)
__thread AVDictionary * format_opts
void print_error(const char *filename, int err)
__thread char * program_name
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 register_exit(void(*cb)(int ret))
void uninit_opts(void)
AVDictionary * filter_codec_opts(AVDictionary *opts, enum AVCodecID codec_id, AVFormatContext *s, AVStream *st, const AVCodec *codec)
__thread int hide_banner
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts)
int show_help(void *optctx, const char *opt, const char *arg)
#define OPT_BOOL
#define OPT_INT
#define AV_LOG_STDERR
#define OPT_INPUT
#define OPT_STRING
__thread int find_stream_info
void show_banner(int argc, char **argv, const OptionDef *options)
#define OPT_EXPERT
#define OPT_EXIT
#define HAS_ARG
static void show_usage(void)
__thread int read_intervals_nb
static void writer_w8_avio(WriterContext *wctx, int b)
#define writer_w8(wctx_, b_)
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_FRAME_SIDE_DATA_COMPONENT_LIST
@ SECTION_ID_PROGRAM_TAGS
@ SECTION_ID_PIXEL_FORMATS
@ SECTION_ID_FRAME_SIDE_DATA_PIECE_LIST
@ 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_FRAME_SIDE_DATA_PIECE
@ 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_FRAME_SIDE_DATA_COMPONENT
@ 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 const char * writer_get_name(void *p)
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)
static void writer_print_data(WriterContext *wctx, const char *name, const uint8_t *data, int size)
__thread OptionDef * ffprobe_options
static av_cold int json_init(WriterContext *wctx)
#define print_int(k, v)
static int writer_open(WriterContext **wctx, const Writer *writer, const char *args, const struct section *sections, int nb_sections, const char *output)
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 unit_second_str[]
static void writer_printf_avio(WriterContext *wctx, const char *fmt,...)
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 writer_put_str_printf(WriterContext *wctx, const char *str)
static void print_primaries(WriterContext *w, enum AVColorPrimaries color_primaries)
static void ffprobe_show_library_versions(WriterContext *w)
static const Writer flat_writer
#define writer_put_str(wctx_, str_)
static void default_print_section_header(WriterContext *wctx)
static int read_interval_packets(WriterContext *w, InputFile *ifile, const ReadInterval *interval, int64_t *cur_ts)
#define SHOW_OPTIONAL_FIELDS_AUTO
static __thread struct section sections[]
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 void writer_print_data_hash(WriterContext *wctx, const char *name, const uint8_t *data, int size)
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)
#define print_list_fmt(k, f, n,...)
#define pthread_mutex_lock(a)
static void print_dynamic_hdr_vivid(WriterContext *w, const AVDynamicHDRVivid *metadata)
static int writer_close(WriterContext **wctx)
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 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 * flat_escape_value_str(AVBPrint *dst, const char *src)
static const char unit_hertz_str[]
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 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)
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
static void opt_output_file(void *optctx, const char *arg)
double dec_val
static int opt_show_optional_fields(void *optctx, const char *opt, const char *arg)
__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
#define print_duration_ts(k, v)
static av_cold int flat_init(WriterContext *wctx)
static void writer_w8_printf(WriterContext *wctx, int b)
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
__thread int longjmp_value
static char * ini_escape_str(AVBPrint *dst, const char *src)
#define PRINT_STRING_OPT
static const Writer csv_writer
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
#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
#define writer_printf(wctx_, fmt_,...)
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 char * output_filename
__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[]
static void writer_put_str_avio(WriterContext *wctx, const char *str)
#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)
__thread int do_show_packet_tags
__thread int show_value_unit
static const struct @3 si_prefixes[]
static char * value_string(char *buf, int buf_size, struct unit_value uv)
static void close_input_file(InputFile *ifile)
static const char * csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
static void print_section(SectionID id, int level)
struct FlatContext FlatContext
#define pthread_mutex_unlock(a)
static const char * c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
static void ffprobe_show_program_version(WriterContext *w)
static const char * json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
static void print_dovi_metadata(WriterContext *w, const AVDOVIMetadata *dovi)
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)
__thread const AVInputFormat * iformat
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
__thread int do_show_programs
struct CompactContext CompactContext
static const AVOption json_options[]
static const char * flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
__thread int main_ffprobe_return_code
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)
static int opt_output_file_o(void *optctx, const char *opt, const char *arg)
__thread int use_value_sexagesimal_format
__thread int do_show_pixel_format_flags
#define OFFSET(x)
static void writer_printf_printf(WriterContext *wctx, const char *fmt,...)
static void ffprobe_show_pixel_formats(WriterContext *w)
__thread uint64_t * nb_streams_packets
__thread int do_read_packets
__thread int use_value_prefix
#define print_section_footer(s)
static void writer_print_section_header(WriterContext *wctx, int section_id)
__thread int do_show_pixel_formats
static int show_programs(WriterContext *w, InputFile *ifile)
static char * upcase_string(char *dst, size_t dst_size, const char *src)
#define SHOW_OPTIONAL_FIELDS_ALWAYS
__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)
__thread int show_optional_fields
#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)
static const Writer * writer_get_by_name(const char *name)
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
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)
static void * writer_child_next(void *obj, void *prev)
#define print_fmt(k, f,...)
static const char * none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
static void print_dynamic_hdr10_plus(WriterContext *w, const AVDynamicHDRPlus *metadata)
#define SHOW_OPTIONAL_FIELDS_NEVER
int show_decoders(void *optctx, const char *opt, const char *arg)
int opt_loglevel(void *optctx, const char *opt, const char *arg)
int opt_cpuflags(void *optctx, const char *opt, const char *arg)
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)
int show_dispositions(void *optctx, const char *opt, const char *arg)
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)
int opt_cpucount(void *optctx, const char *opt, const char *arg)
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)
int show_devices(void *optctx, const char *opt, const char *arg)
int show_formats(void *optctx, const char *opt, const char *arg)
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)
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)
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]
void(* writer_put_str)(WriterContext *wctx, const char *str)
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]
void(* writer_w8)(WriterContext *wctx, int b)
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
AVIOContext * avio
the I/O context used to write
void(* writer_printf)(WriterContext *wctx, const char *fmt,...)
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
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