FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/textformat/tf_default.c
Date: 2026-10-12 23:00:26
Exec Total Coverage
Lines: 45 45 100.0%
Functions: 6 6 100.0%
Branches: 26 30 86.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) The FFmpeg developers
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "avtextformat.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/opt.h"
31 #include "tf_internal.h"
32
33 /* Default output */
34
35 typedef struct DefaultContext {
36 const AVClass *class;
37 int nokey;
38 int noprint_wrappers;
39 } DefaultContext;
40
41 #undef OFFSET
42 #define OFFSET(x) offsetof(DefaultContext, x)
43
44 static const AVOption default_options[] = {
45 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
46 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
47 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
48 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
49 { NULL },
50 };
51
52 DEFINE_FORMATTER_CLASS(default);
53
54 /* lame uppercasing routine, assumes the string is lower case ASCII */
55 7372 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
56 {
57 unsigned i;
58
59
3/4
✓ Branch 0 taken 50603 times.
✓ Branch 1 taken 7372 times.
✓ Branch 2 taken 50603 times.
✗ Branch 3 not taken.
57975 for (i = 0; src[i] && i < dst_size - 1; i++)
60 50603 dst[i] = (char)av_toupper(src[i]);
61 7372 dst[i] = 0;
62 7372 return dst;
63 }
64
65 9558 static int default_is_nested(AVTextFormatContext *wctx)
66 {
67 9558 const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
68
69
2/2
✓ Branch 0 taken 9097 times.
✓ Branch 1 taken 461 times.
18655 return parent_section &&
70
2/2
✓ Branch 0 taken 2394 times.
✓ Branch 1 taken 6703 times.
9097 !(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY));
71 }
72
73 4803 static void default_print_section_header(AVTextFormatContext *wctx, const void *data)
74 {
75 4803 DefaultContext *def = wctx->priv;
76 char buf[32];
77 4803 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
78 4803 int nested = default_is_nested(wctx);
79
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4803 times.
4803 if (!section)
81 1240 return;
82
83 4803 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
84
2/2
✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 3601 times.
4803 if (nested)
85 1202 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
86 1202 wctx->section_pbuf[wctx->level - 1].str,
87 upcase_string(buf, sizeof(buf),
88 1202 av_x_if_null(section->element_name, section->name)));
89
90
4/4
✓ Branch 0 taken 4755 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 1192 times.
✓ Branch 3 taken 3563 times.
4803 if (def->noprint_wrappers || nested)
91 1240 return;
92
93
2/2
✓ Branch 0 taken 3085 times.
✓ Branch 1 taken 478 times.
3563 if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
94 3085 writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
95 }
96
97 4803 static void default_print_section_footer(AVTextFormatContext *wctx)
98 {
99 4803 DefaultContext *def = wctx->priv;
100 4803 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
101
102 char buf[32];
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4803 times.
4803 if (!section)
105 1240 return;
106
107
4/4
✓ Branch 0 taken 4755 times.
✓ Branch 1 taken 48 times.
✓ Branch 3 taken 1192 times.
✓ Branch 4 taken 3563 times.
4803 if (def->noprint_wrappers || default_is_nested(wctx))
108 1240 return;
109
110
2/2
✓ Branch 0 taken 3085 times.
✓ Branch 1 taken 478 times.
3563 if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
111 3085 writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
112 }
113
114 12627 static void default_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
115 {
116 12627 DefaultContext *def = wctx->priv;
117
118
2/2
✓ Branch 0 taken 12590 times.
✓ Branch 1 taken 37 times.
12627 if (!def->nokey)
119 12590 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
120 12627 writer_printf(wctx, "%s\n", value);
121 12627 }
122
123 12661 static void default_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
124 {
125 12661 DefaultContext *def = wctx->priv;
126
127
1/2
✓ Branch 0 taken 12661 times.
✗ Branch 1 not taken.
12661 if (!def->nokey)
128 12661 writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
129 12661 writer_printf(wctx, "%"PRId64"\n", value);
130 12661 }
131
132 const AVTextFormatter avtextformatter_default = {
133 .name = "default",
134 .priv_size = sizeof(DefaultContext),
135 .print_section_header = default_print_section_header,
136 .print_section_footer = default_print_section_footer,
137 .print_integer = default_print_int,
138 .print_string = default_print_str,
139 .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
140 .priv_class = &default_class,
141 };
142