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