FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/textformat/avtextformat.c
Date: 2026-09-28 04:46:15
Exec Total Coverage
Lines: 218 367 59.4%
Functions: 17 25 68.0%
Branches: 131 254 51.6%

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 <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "libavutil/mem.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avutil.h"
31 #include "libavutil/base64.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/common.h"
34 #include "libavutil/error.h"
35 #include "libavutil/hash.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/macros.h"
38 #include "libavutil/opt.h"
39 #include "avtextformat.h"
40
41 #define SECTION_ID_NONE (-1)
42
43 #define SHOW_OPTIONAL_FIELDS_AUTO (-1)
44 #define SHOW_OPTIONAL_FIELDS_NEVER 0
45 #define SHOW_OPTIONAL_FIELDS_ALWAYS 1
46
47 static const struct {
48 double bin_val;
49 double dec_val;
50 char bin_str[4];
51 char dec_str[4];
52 } si_prefixes[] = {
53 { 1.0, 1.0, "", "" },
54 { 1.024e3, 1e3, "Ki", "K" },
55 { 1.048576e6, 1e6, "Mi", "M" },
56 { 1.073741824e9, 1e9, "Gi", "G" },
57 { 1.099511627776e12, 1e12, "Ti", "T" },
58 { 1.125899906842624e15, 1e15, "Pi", "P" },
59 };
60
61 ✗ static const char *textcontext_get_formatter_name(void *p)
62 {
63 ✗ AVTextFormatContext *tctx = p;
64 ✗ return tctx->formatter->name;
65 }
66
67 #define OFFSET(x) offsetof(AVTextFormatContext, x)
68
69 static const AVOption textcontext_options[] = {
70 { "string_validation", "set string validation mode",
71 OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" },
72 { "sv", "set string validation mode",
73 OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" },
74 { "ignore", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_IGNORE }, .unit = "sv" },
75 { "replace", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, .unit = "sv" },
76 { "fail", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_FAIL }, .unit = "sv" },
77 { "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "" } },
78 { "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "\xEF\xBF\xBD" } },
79 { NULL }
80 };
81
82 64 static void *textcontext_child_next(void *obj, void *prev)
83 {
84 64 AVTextFormatContext *ctx = obj;
85
4/8
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 64 times.
✗ Branch 7 not taken.
64 if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv)
86 64 return ctx->priv;
87 ✗ return NULL;
88 }
89
90 static const AVClass textcontext_class = {
91 .class_name = "AVTextContext",
92 .item_name = textcontext_get_formatter_name,
93 .option = textcontext_options,
94 .version = LIBAVUTIL_VERSION_INT,
95 .child_next = textcontext_child_next,
96 };
97
98 ✗ static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
99 {
100 ✗ av_bprintf(bp, "0X");
101 ✗ for (unsigned i = 0; i < ubuf_size; i++)
102 ✗ av_bprintf(bp, "%02X", ubuf[i]);
103 ✗ }
104
105 302 int avtext_context_close(AVTextFormatContext **ptctx)
106 {
107 302 AVTextFormatContext *tctx = *ptctx;
108 302 int ret = 0;
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (!tctx)
111 ✗ return AVERROR(EINVAL);
112
113 302 av_hash_freep(&tctx->hash);
114
115
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (tctx->formatter) {
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (tctx->formatter->uninit)
117 ✗ ret = tctx->formatter->uninit(tctx);
118
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (tctx->formatter->priv_class)
119 302 av_opt_free(tctx->priv);
120 }
121
2/2
✓ Branch 0 taken 3624 times.
✓ Branch 1 taken 302 times.
3926 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
122 3624 av_bprint_finalize(&tctx->section_pbuf[i], NULL);
123 302 av_freep(&tctx->priv);
124 302 av_opt_free(tctx);
125 302 av_freep(ptctx);
126 302 return ret;
127 }
128
129
130 302 int avtext_context_open(AVTextFormatContext **ptctx, const AVTextFormatter *formatter, AVTextWriterContext *writer_context, const char *args,
131 const AVTextFormatSection *sections, int nb_sections, AVTextFormatOptions options, char *show_data_hash)
132 {
133 AVTextFormatContext *tctx;
134 302 int ret = 0;
135
136
2/4
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 302 times.
302 av_assert0(ptctx && formatter);
137
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 302 times.
302 if (!(tctx = av_mallocz(sizeof(AVTextFormatContext)))) {
139 ✗ ret = AVERROR(ENOMEM);
140 ✗ goto fail;
141 }
142
143
2/2
✓ Branch 0 taken 3624 times.
✓ Branch 1 taken 302 times.
3926 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
144 3624 av_bprint_init(&tctx->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
145
146 302 tctx->class = &textcontext_class;
147 302 av_opt_set_defaults(tctx);
148
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 302 times.
302 if (!(tctx->priv = av_mallocz(formatter->priv_size))) {
150 ✗ ret = AVERROR(ENOMEM);
151 ✗ goto fail;
152 }
153
154 302 tctx->opts = options;
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (nb_sections > SECTION_MAX_NB_SECTIONS) {
157 ✗ av_log(tctx, AV_LOG_ERROR, "The number of section definitions (%d) is larger than the maximum allowed (%d)\n", nb_sections, SECTION_MAX_NB_SECTIONS);
158 ✗ ret = AVERROR(EINVAL);
159 ✗ goto fail;
160 }
161
162 302 tctx->formatter = formatter;
163 302 tctx->level = -1;
164 302 tctx->sections = sections;
165 302 tctx->nb_sections = nb_sections;
166 302 tctx->writer = writer_context;
167
168
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (formatter->priv_class) {
169 302 void *priv_ctx = tctx->priv;
170 302 *(const AVClass **)priv_ctx = formatter->priv_class;
171 302 av_opt_set_defaults(priv_ctx);
172 }
173
174 /* convert options to dictionary */
175
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 253 times.
302 if (args) {
176 49 AVDictionary *opts = NULL;
177 49 const AVDictionaryEntry *opt = NULL;
178
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
180 ✗ av_log(tctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to textformat context\n", args);
181 ✗ av_dict_free(&opts);
182 ✗ goto fail;
183 }
184
185
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 49 times.
113 while ((opt = av_dict_iterate(opts, opt))) {
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
64 if ((ret = av_opt_set(tctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
187 ✗ av_log(tctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to textformat context\n",
188 ✗ opt->key, opt->value);
189 ✗ av_dict_free(&opts);
190 ✗ goto fail;
191 }
192 }
193
194 49 av_dict_free(&opts);
195 }
196
197
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 283 times.
302 if (show_data_hash) {
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if ((ret = av_hash_alloc(&tctx->hash, show_data_hash)) < 0) {
199 ✗ if (ret == AVERROR(EINVAL)) {
200 const char *n;
201 ✗ av_log(NULL, AV_LOG_ERROR, "Unknown hash algorithm '%s'\nKnown algorithms:", show_data_hash);
202 ✗ for (unsigned i = 0; (n = av_hash_names(i)); i++)
203 ✗ av_log(NULL, AV_LOG_ERROR, " %s", n);
204 ✗ av_log(NULL, AV_LOG_ERROR, "\n");
205 }
206 ✗ goto fail;
207 }
208 }
209
210 /* validate replace string */
211 {
212 302 const uint8_t *p = (uint8_t *)tctx->string_validation_replacement;
213 302 const uint8_t *endp = p + strlen((const char *)p);
214
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 302 times.
604 while (*p) {
215 302 const uint8_t *p0 = p;
216 int32_t code;
217 302 ret = av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (ret < 0) {
219 AVBPrint bp;
220 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
221 ✗ bprint_bytes(&bp, p0, p - p0);
222 ✗ av_log(tctx, AV_LOG_ERROR,
223 "Invalid UTF8 sequence %s found in string validation replace '%s'\n",
224 ✗ bp.str, tctx->string_validation_replacement);
225 ✗ goto fail;
226 }
227 }
228 }
229
230
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 236 times.
302 if (tctx->formatter->init)
231 66 ret = tctx->formatter->init(tctx);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (ret < 0)
233 ✗ goto fail;
234
235 302 *ptctx = tctx;
236
237 302 return 0;
238
239 ✗ fail:
240 ✗ avtext_context_close(&tctx);
241 ✗ return ret;
242 }
243
244 static const char unit_second_str[] = "s";
245
246 15838 void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id)
247 {
248
2/4
✓ Branch 0 taken 15838 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15838 times.
15838 if (section_id < 0 || section_id >= tctx->nb_sections) {
249 ✗ av_log(tctx, AV_LOG_ERROR, "Invalid section_id for section_header: %d\n", section_id);
250 ✗ return;
251 }
252
253 15838 tctx->level++;
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15838 times.
15838 av_assert0(tctx->level < SECTION_MAX_NB_LEVELS);
255
256 15838 tctx->nb_item[tctx->level] = 0;
257 15838 memset(tctx->nb_item_type[tctx->level], 0, sizeof(tctx->nb_item_type[tctx->level]));
258 15838 tctx->section[tctx->level] = &tctx->sections[section_id];
259
260
1/2
✓ Branch 0 taken 15838 times.
✗ Branch 1 not taken.
15838 if (tctx->formatter->print_section_header)
261 15838 tctx->formatter->print_section_header(tctx, data);
262 }
263
264 15838 void avtext_print_section_footer(AVTextFormatContext *tctx)
265 {
266
2/4
✓ Branch 0 taken 15838 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15838 times.
15838 if (tctx->level < 0 || tctx->level >= SECTION_MAX_NB_LEVELS) {
267 ✗ av_log(tctx, AV_LOG_ERROR, "Invalid level for section_footer: %d\n", tctx->level);
268 ✗ return;
269 }
270
271 15838 int section_id = tctx->section[tctx->level]->id;
272 31676 int parent_section_id = tctx->level ?
273
2/2
✓ Branch 0 taken 15536 times.
✓ Branch 1 taken 302 times.
15838 tctx->section[tctx->level - 1]->id : SECTION_ID_NONE;
274
275
2/2
✓ Branch 0 taken 15536 times.
✓ Branch 1 taken 302 times.
15838 if (parent_section_id != SECTION_ID_NONE) {
276 15536 tctx->nb_item[tctx->level - 1]++;
277 15536 tctx->nb_item_type[tctx->level - 1][section_id]++;
278 }
279
280
2/2
✓ Branch 0 taken 15748 times.
✓ Branch 1 taken 90 times.
15838 if (tctx->formatter->print_section_footer)
281 15748 tctx->formatter->print_section_footer(tctx);
282 15838 tctx->level--;
283 }
284
285 72811 void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t val, int flags)
286 {
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72811 times.
72811 av_assert0(tctx);
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72811 times.
72811 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
290 ✗ return;
291
292
1/2
✓ Branch 0 taken 72811 times.
✗ Branch 1 not taken.
72811 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
293
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 72381 times.
72811 && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
294
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 417 times.
430 && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
295 13 return;
296
297
3/6
✓ Branch 0 taken 72798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72798 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 72798 times.
72798 av_assert0(key && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
298
299
3/4
✓ Branch 0 taken 72798 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 45052 times.
✓ Branch 4 taken 27746 times.
72798 if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
300 45052 tctx->formatter->print_integer(tctx, key, val);
301 45052 tctx->nb_item[tctx->level]++;
302 }
303 }
304
305 8978 static inline int validate_string(AVTextFormatContext *tctx, char **dstp, const char *src)
306 {
307 8978 const uint8_t *p, *endp, *srcp = (const uint8_t *)src;
308 AVBPrint dstbuf;
309 AVBPrint invalid_seq;
310 8978 int invalid_chars_nb = 0, ret = 0;
311
312 8978 *dstp = NULL;
313 8978 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
314 8978 av_bprint_init(&invalid_seq, 0, AV_BPRINT_SIZE_UNLIMITED);
315
316 8978 endp = srcp + strlen(src);
317
2/2
✓ Branch 0 taken 258961 times.
✓ Branch 1 taken 8978 times.
267939 for (p = srcp; *p;) {
318 int32_t code;
319 258961 int invalid = 0;
320 258961 const uint8_t *p0 = p;
321
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 258961 times.
258961 if (av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags) < 0) {
323
324 ✗ av_bprint_clear(&invalid_seq);
325
326 ✗ bprint_bytes(&invalid_seq, p0, p - p0);
327
328 ✗ av_log(tctx, AV_LOG_DEBUG, "Invalid UTF-8 sequence '%s' found in string '%s'\n", invalid_seq.str, src);
329 ✗ invalid = 1;
330 }
331
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258961 times.
258961 if (invalid) {
333 ✗ invalid_chars_nb++;
334
335 ✗ switch (tctx->string_validation) {
336 ✗ case AV_TEXTFORMAT_STRING_VALIDATION_FAIL:
337 ✗ av_log(tctx, AV_LOG_ERROR, "Invalid UTF-8 sequence found in string '%s'\n", src);
338 ✗ ret = AVERROR_INVALIDDATA;
339 ✗ goto end;
340
341 ✗ case AV_TEXTFORMAT_STRING_VALIDATION_REPLACE:
342 ✗ av_bprintf(&dstbuf, "%s", tctx->string_validation_replacement);
343 ✗ break;
344 }
345 }
346
347
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 258961 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
258961 if (!invalid || tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_IGNORE)
348 258961 av_bprint_append_data(&dstbuf, p0, p-p0);
349 }
350
351
1/4
✓ Branch 0 taken 8978 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8978 if (invalid_chars_nb && tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_REPLACE)
352 ✗ av_log(tctx, AV_LOG_WARNING,
353 "%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
354 invalid_chars_nb, src, tctx->string_validation_replacement);
355
356 8978 end:
357 8978 av_bprint_finalize(&dstbuf, dstp);
358 8978 av_bprint_finalize(&invalid_seq, NULL);
359 8978 return ret;
360 }
361
362 struct unit_value {
363 union {
364 double d;
365 int64_t i;
366 } val;
367
368 AVTextFormatValueFormat fmt;
369 const char *unit;
370 };
371
372 static const char float_fmt_full[] = "%f";
373 static const char float_fmt_singledigit[] = "%.1f";
374 44403 static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_size, struct unit_value uv)
375 {
376 double vald;
377 44403 int64_t vali = 0;
378 44403 const char *float_fmt = 0;
379
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44403 times.
44403 if (uv.fmt == AV_TEXTFORMAT_VALUE_FMT_DECIBEL) {
381 ✗ vald = 20 * log10(uv.val.d);
382 ✗ float_fmt = float_fmt_singledigit;
383
2/2
✓ Branch 0 taken 33688 times.
✓ Branch 1 taken 10715 times.
44403 } else if (uv.fmt >= AV_TEXTFORMAT_VALUE_FMT_DOUBLE) {
384 33688 vald = uv.val.d;
385 33688 float_fmt = float_fmt_full;
386 } else {
387 10715 vald = (double)uv.val.i;
388 10715 vali = uv.val.i;
389 }
390
391
3/4
✓ Branch 0 taken 33688 times.
✓ Branch 1 taken 10715 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33688 times.
44403 if (uv.fmt == AV_TEXTFORMAT_VALUE_FMT_SECOND && tctx->opts.use_value_sexagesimal_format) {
392 double secs;
393 int hours, mins;
394 ✗ secs = vald;
395 ✗ mins = (int)secs / 60;
396 ✗ secs = secs - mins * 60;
397 ✗ hours = mins / 60;
398 ✗ mins %= 60;
399 ✗ snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
400 } else {
401 44403 const char *prefix_string = "";
402
403
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 44403 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
44403 if (tctx->opts.use_value_prefix && vald > 1) {
404 int64_t index;
405
406 ✗ if (uv.fmt == AV_TEXTFORMAT_VALUE_FMT_BYTE && tctx->opts.use_byte_value_binary_prefix) {
407 ✗ index = (int64_t)(log2(vald) / 10);
408 ✗ index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
409 ✗ vald /= si_prefixes[index].bin_val;
410 ✗ prefix_string = si_prefixes[index].bin_str;
411 } else {
412 ✗ index = (int64_t)(log10(vald) / 3);
413 ✗ index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
414 ✗ vald /= si_prefixes[index].dec_val;
415 ✗ prefix_string = si_prefixes[index].dec_str;
416 }
417 ✗ vali = (int64_t)vald;
418 }
419
420
3/6
✓ Branch 0 taken 10715 times.
✓ Branch 1 taken 33688 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10715 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
44403 if (float_fmt || (tctx->opts.use_value_prefix && vald != (int64_t)vald))
421
1/2
✓ Branch 0 taken 33688 times.
✗ Branch 1 not taken.
33688 snprintf(buf, buf_size, float_fmt ? float_fmt : "%f", vald);
422 else
423 10715 snprintf(buf, buf_size, "%"PRId64, vali);
424
425
2/8
✓ Branch 0 taken 44403 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44403 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
44403 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->opts.show_value_unit && uv.unit && *uv.unit ? " " : "",
426
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 44403 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
44403 prefix_string, tctx->opts.show_value_unit && uv.unit ? uv.unit : "");
427 }
428
429 44403 return buf;
430 }
431
432
433 10715 void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, AVTextFormatValueFormat fmt, const char *unit)
434 {
435 char val_str[128];
436 struct unit_value uv;
437
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10715 times.
10715 av_assert0(fmt < AV_TEXTFORMAT_VALUE_FMT_DOUBLE);
439
440 10715 uv.val.i = val;
441 10715 uv.fmt = fmt;
442 10715 uv.unit = unit;
443 10715 avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0);
444 10715 }
445
446
447 ✗ void avtext_print_unit_double(AVTextFormatContext *tctx, const char *key, double val, AVTextFormatValueFormat fmt, const char *unit)
448 {
449 char val_str[128];
450 struct unit_value uv;
451
452 ✗ av_assert0(fmt >= AV_TEXTFORMAT_VALUE_FMT_DOUBLE);
453
454 ✗ uv.val.d = val;
455 ✗ uv.fmt = fmt;
456 ✗ uv.unit = unit;
457 ✗ avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0);
458 ✗ }
459
460 106734 int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags)
461 {
462 const AVTextFormatSection *section;
463 106734 int ret = 0;
464
465
4/8
✓ Branch 0 taken 106734 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 106734 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 106734 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 106734 times.
106734 av_assert0(key && val && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
466
467 106734 section = tctx->section[tctx->level];
468
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106734 times.
106734 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
470 ✗ return 0;
471
472
1/2
✓ Branch 0 taken 106734 times.
✗ Branch 1 not taken.
106734 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
473
2/2
✓ Branch 0 taken 11267 times.
✓ Branch 1 taken 95467 times.
106734 && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
474
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 10975 times.
11267 && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
475 292 return 0;
476
477
3/4
✓ Branch 0 taken 106442 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 46461 times.
✓ Branch 4 taken 59981 times.
106442 if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
478
2/2
✓ Branch 0 taken 4489 times.
✓ Branch 1 taken 41972 times.
46461 if (flags & AV_TEXTFORMAT_PRINT_STRING_VALIDATE) {
479 4489 char *key1 = NULL, *val1 = NULL;
480 4489 ret = validate_string(tctx, &key1, key);
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4489 times.
4489 if (ret < 0) goto end;
482 4489 ret = validate_string(tctx, &val1, val);
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4489 times.
4489 if (ret < 0) goto end;
484 4489 tctx->formatter->print_string(tctx, key1, val1);
485 4489 end:
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4489 times.
4489 if (ret < 0)
487 ✗ av_log(tctx, AV_LOG_ERROR,
488 "Invalid key=value string combination %s=%s in section %s\n",
489 ✗ key, val, section->unique_name);
490 4489 av_free(key1);
491 4489 av_free(val1);
492 } else {
493 41972 tctx->formatter->print_string(tctx, key, val);
494 }
495
496 46461 tctx->nb_item[tctx->level]++;
497 }
498
499 106442 return ret;
500 }
501
502 3203 void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep)
503 {
504 char buf[44];
505 3203 snprintf(buf, sizeof(buf), "%d%c%d", q.num, sep, q.den);
506 3203 avtext_print_string(tctx, key, buf, 0);
507 3203 }
508
509 34979 void avtext_print_time(AVTextFormatContext *tctx, const char *key,
510 int64_t ts, const AVRational *time_base, int is_duration)
511 {
512
8/8
✓ Branch 0 taken 24957 times.
✓ Branch 1 taken 10022 times.
✓ Branch 2 taken 23672 times.
✓ Branch 3 taken 1285 times.
✓ Branch 4 taken 10022 times.
✓ Branch 5 taken 23672 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 10016 times.
34979 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
513 1291 avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
514 } else {
515 char buf[128];
516 33688 double d = av_q2d(*time_base) * ts;
517 struct unit_value uv;
518 33688 uv.val.d = d;
519 33688 uv.fmt = AV_TEXTFORMAT_VALUE_FMT_SECOND;
520 33688 uv.unit = unit_second_str;
521 33688 value_string(tctx, buf, sizeof(buf), uv);
522 33688 avtext_print_string(tctx, key, buf, 0);
523 }
524 34979 }
525
526 34773 void avtext_print_ts(AVTextFormatContext *tctx, const char *key, int64_t ts, int is_duration)
527 {
528
8/8
✓ Branch 0 taken 24751 times.
✓ Branch 1 taken 10022 times.
✓ Branch 2 taken 23484 times.
✓ Branch 3 taken 1267 times.
✓ Branch 4 taken 10022 times.
✓ Branch 5 taken 23484 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 10016 times.
34773 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0))
529 1273 avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
530 else
531 33500 avtext_print_integer(tctx, key, ts, 0);
532 34773 }
533
534 ✗ static void print_data_xxd(AVBPrint *bp, const uint8_t *data, int size)
535 {
536 ✗ unsigned offset = 0;
537 int i;
538
539 ✗ av_bprintf(bp, "\n");
540 ✗ while (size) {
541 ✗ av_bprintf(bp, "%08x: ", offset);
542 ✗ int l = FFMIN(size, 16);
543 ✗ for (i = 0; i < l; i++) {
544 ✗ av_bprintf(bp, "%02x", data[i]);
545 ✗ if (i & 1)
546 ✗ av_bprintf(bp, " ");
547 }
548 ✗ av_bprint_chars(bp, ' ', 41 - 2 * i - i / 2);
549 ✗ for (i = 0; i < l; i++)
550 ✗ av_bprint_chars(bp, data[i] - 32U < 95 ? data[i] : '.', 1);
551 ✗ av_bprintf(bp, "\n");
552 ✗ offset += l;
553 ✗ data += l;
554 ✗ size -= l;
555 }
556 ✗ }
557
558 ✗ static void print_data_base64(AVBPrint *bp, const uint8_t *data, int size)
559 {
560 char buf[AV_BASE64_SIZE(60)];
561
562 ✗ av_bprintf(bp, "\n");
563 ✗ while (size) {
564 ✗ int l = FFMIN(size, 60);
565 ✗ av_base64_encode(buf, sizeof(buf), data, l);
566 ✗ av_bprintf(bp, "%s\n", buf);
567 ✗ data += l;
568 ✗ size -= l;
569 }
570 ✗ }
571 ✗ void avtext_print_data(AVTextFormatContext *tctx, const char *key,
572 const uint8_t *data, int size)
573 {
574 AVBPrint bp;
575 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
576 ✗ switch (tctx->opts.data_dump_format) {
577 ✗ case AV_TEXTFORMAT_DATADUMP_XXD:
578 ✗ print_data_xxd(&bp, data, size);
579 ✗ break;
580 ✗ case AV_TEXTFORMAT_DATADUMP_BASE64:
581 ✗ print_data_base64(&bp, data, size);
582 ✗ break;
583 ✗ default:
584 ✗ av_unreachable("Invalid data dump type");
585 }
586 ✗ avtext_print_string(tctx, key, bp.str, 0);
587 ✗ av_bprint_finalize(&bp, NULL);
588 ✗ }
589
590 6687 void avtext_print_data_hash(AVTextFormatContext *tctx, const char *key,
591 const uint8_t *data, int size)
592 {
593 6687 char buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
594 int len;
595
596
2/2
✓ Branch 0 taken 4263 times.
✓ Branch 1 taken 2424 times.
6687 if (!tctx->hash)
597 4263 return;
598
599 2424 av_hash_init(tctx->hash);
600 2424 av_hash_update(tctx->hash, data, size);
601 2424 len = snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(tctx->hash));
602 2424 av_hash_final_hex(tctx->hash, (uint8_t *)&buf[len], (int)sizeof(buf) - len);
603 2424 avtext_print_string(tctx, key, buf, 0);
604 }
605
606 ✗ static const char *writercontext_get_writer_name(void *p)
607 {
608 ✗ AVTextWriterContext *wctx = p;
609 ✗ return wctx->writer->name;
610 }
611
612 ✗ static void *writercontext_child_next(void *obj, void *prev)
613 {
614 ✗ AVTextFormatContext *ctx = obj;
615 ✗ if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv)
616 ✗ return ctx->priv;
617 ✗ return NULL;
618 }
619
620 static const AVClass textwriter_class = {
621 .class_name = "AVTextWriterContext",
622 .item_name = writercontext_get_writer_name,
623 .version = LIBAVUTIL_VERSION_INT,
624 .child_next = writercontext_child_next,
625 };
626
627
628 302 int avtextwriter_context_close(AVTextWriterContext **pwctx)
629 {
630 302 AVTextWriterContext *wctx = *pwctx;
631 302 int ret = 0;
632
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (!wctx)
634 ✗ return AVERROR(EINVAL);
635
636
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (wctx->writer) {
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (wctx->writer->uninit)
638 ✗ ret = wctx->writer->uninit(wctx);
639
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (wctx->writer->priv_class)
640 302 av_opt_free(wctx->priv);
641 }
642 302 av_freep(&wctx->priv);
643 302 av_freep(pwctx);
644 302 return ret;
645 }
646
647
648 302 int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
649 {
650 AVTextWriterContext *wctx;
651 302 int ret = 0;
652
653
2/4
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 302 times.
302 if (!pwctx || !writer)
654 ✗ return AVERROR(EINVAL);
655
656
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 302 times.
302 if (!((wctx = av_mallocz(sizeof(AVTextWriterContext))))) {
657 ✗ ret = AVERROR(ENOMEM);
658 ✗ goto fail;
659 }
660
661
2/4
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 302 times.
302 if (writer->priv_size && !((wctx->priv = av_mallocz(writer->priv_size)))) {
662 ✗ ret = AVERROR(ENOMEM);
663 ✗ goto fail;
664 }
665
666
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (writer->priv_class) {
667 302 void *priv_ctx = wctx->priv;
668 302 *(const AVClass **)priv_ctx = writer->priv_class;
669 302 av_opt_set_defaults(priv_ctx);
670 }
671
672 302 wctx->class = &textwriter_class;
673 302 wctx->writer = writer;
674
675 302 av_opt_set_defaults(wctx);
676
677
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (wctx->writer->init)
679 ✗ ret = wctx->writer->init(wctx);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
302 if (ret < 0)
681 ✗ goto fail;
682
683 302 *pwctx = wctx;
684
685 302 return 0;
686
687 ✗ fail:
688 ✗ avtextwriter_context_close(&wctx);
689 ✗ return ret;
690 }
691
692 static const AVTextFormatter *const registered_formatters[] =
693 {
694 &avtextformatter_default,
695 &avtextformatter_compact,
696 &avtextformatter_csv,
697 &avtextformatter_flat,
698 &avtextformatter_ini,
699 &avtextformatter_json,
700 &avtextformatter_xml,
701 &avtextformatter_mermaid,
702 &avtextformatter_mermaidhtml,
703 NULL
704 };
705
706 302 const AVTextFormatter *avtext_get_formatter_by_name(const char *name)
707 {
708
1/2
✓ Branch 0 taken 406 times.
✗ Branch 1 not taken.
406 for (int i = 0; registered_formatters[i]; i++) {
709 const char *end;
710
2/2
✓ Branch 1 taken 302 times.
✓ Branch 2 taken 104 times.
406 if (av_strstart(name, registered_formatters[i]->name, &end) &&
711
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 302 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
302 (*end == '\0' || *end == '='))
712 302 return registered_formatters[i];
713 }
714
715 ✗ return NULL;
716 }
717