FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/textformat/avtextformat.c
Date: 2026-09-13 11:21:06
Exec Total Coverage
Lines: 216 367 58.9%
Functions: 17 25 68.0%
Branches: 128 254 50.4%

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 62 static void *textcontext_child_next(void *obj, void *prev)
83 {
84 62 AVTextFormatContext *ctx = obj;
85
4/8
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 62 times.
✗ Branch 7 not taken.
62 if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv)
86 62 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 287 int avtext_context_close(AVTextFormatContext **ptctx)
106 {
107 287 AVTextFormatContext *tctx = *ptctx;
108 287 int ret = 0;
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (!tctx)
111 return AVERROR(EINVAL);
112
113 287 av_hash_freep(&tctx->hash);
114
115
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 if (tctx->formatter) {
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (tctx->formatter->uninit)
117 ret = tctx->formatter->uninit(tctx);
118
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 if (tctx->formatter->priv_class)
119 287 av_opt_free(tctx->priv);
120 }
121
2/2
✓ Branch 0 taken 3444 times.
✓ Branch 1 taken 287 times.
3731 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
122 3444 av_bprint_finalize(&tctx->section_pbuf[i], NULL);
123 287 av_freep(&tctx->priv);
124 287 av_opt_free(tctx);
125 287 av_freep(ptctx);
126 287 return ret;
127 }
128
129
130 287 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 287 int ret = 0;
135
136
2/4
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 287 times.
287 av_assert0(ptctx && formatter);
137
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 287 times.
287 if (!(tctx = av_mallocz(sizeof(AVTextFormatContext)))) {
139 ret = AVERROR(ENOMEM);
140 goto fail;
141 }
142
143
2/2
✓ Branch 0 taken 3444 times.
✓ Branch 1 taken 287 times.
3731 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
144 3444 av_bprint_init(&tctx->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
145
146 287 tctx->class = &textcontext_class;
147 287 av_opt_set_defaults(tctx);
148
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 287 times.
287 if (!(tctx->priv = av_mallocz(formatter->priv_size))) {
150 ret = AVERROR(ENOMEM);
151 goto fail;
152 }
153
154 287 tctx->opts = options;
155
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 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 287 tctx->formatter = formatter;
163 287 tctx->level = -1;
164 287 tctx->sections = sections;
165 287 tctx->nb_sections = nb_sections;
166 287 tctx->writer = writer_context;
167
168
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 if (formatter->priv_class) {
169 287 void *priv_ctx = tctx->priv;
170 287 *(const AVClass **)priv_ctx = formatter->priv_class;
171 287 av_opt_set_defaults(priv_ctx);
172 }
173
174 /* convert options to dictionary */
175
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 239 times.
287 if (args) {
176 48 AVDictionary *opts = NULL;
177 48 const AVDictionaryEntry *opt = NULL;
178
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
48 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 62 times.
✓ Branch 2 taken 48 times.
110 while ((opt = av_dict_iterate(opts, opt))) {
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 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 48 av_dict_free(&opts);
195 }
196
197
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 269 times.
287 if (show_data_hash) {
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 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 287 const uint8_t *p = (uint8_t *)tctx->string_validation_replacement;
213 287 const uint8_t *endp = p + strlen((const char *)p);
214
2/2
✓ Branch 0 taken 287 times.
✓ Branch 1 taken 287 times.
574 while (*p) {
215 287 const uint8_t *p0 = p;
216 int32_t code;
217 287 ret = av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 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 61 times.
✓ Branch 1 taken 226 times.
287 if (tctx->formatter->init)
231 61 ret = tctx->formatter->init(tctx);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (ret < 0)
233 goto fail;
234
235 287 *ptctx = tctx;
236
237 287 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 15643 void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id)
247 {
248
2/4
✓ Branch 0 taken 15643 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15643 times.
15643 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 15643 tctx->level++;
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15643 times.
15643 av_assert0(tctx->level < SECTION_MAX_NB_LEVELS);
255
256 15643 tctx->nb_item[tctx->level] = 0;
257 15643 memset(tctx->nb_item_type[tctx->level], 0, sizeof(tctx->nb_item_type[tctx->level]));
258 15643 tctx->section[tctx->level] = &tctx->sections[section_id];
259
260
1/2
✓ Branch 0 taken 15643 times.
✗ Branch 1 not taken.
15643 if (tctx->formatter->print_section_header)
261 15643 tctx->formatter->print_section_header(tctx, data);
262 }
263
264 15643 void avtext_print_section_footer(AVTextFormatContext *tctx)
265 {
266
2/4
✓ Branch 0 taken 15643 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15643 times.
15643 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 15643 int section_id = tctx->section[tctx->level]->id;
272 31286 int parent_section_id = tctx->level ?
273
2/2
✓ Branch 0 taken 15356 times.
✓ Branch 1 taken 287 times.
15643 tctx->section[tctx->level - 1]->id : SECTION_ID_NONE;
274
275
2/2
✓ Branch 0 taken 15356 times.
✓ Branch 1 taken 287 times.
15643 if (parent_section_id != SECTION_ID_NONE) {
276 15356 tctx->nb_item[tctx->level - 1]++;
277 15356 tctx->nb_item_type[tctx->level - 1][section_id]++;
278 }
279
280
2/2
✓ Branch 0 taken 15553 times.
✓ Branch 1 taken 90 times.
15643 if (tctx->formatter->print_section_footer)
281 15553 tctx->formatter->print_section_footer(tctx);
282 15643 tctx->level--;
283 }
284
285 70098 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 70098 times.
70098 av_assert0(tctx);
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70098 times.
70098 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
290 return;
291
292
1/2
✓ Branch 0 taken 70098 times.
✗ Branch 1 not taken.
70098 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70098 times.
70098 && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
294 && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
295 return;
296
297
3/6
✓ Branch 0 taken 70098 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70098 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 70098 times.
70098 av_assert0(key && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
298
299
3/4
✓ Branch 0 taken 70098 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 44383 times.
✓ Branch 4 taken 25715 times.
70098 if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
300 44383 tctx->formatter->print_integer(tctx, key, val);
301 44383 tctx->nb_item[tctx->level]++;
302 }
303 }
304
305 8920 static inline int validate_string(AVTextFormatContext *tctx, char **dstp, const char *src)
306 {
307 8920 const uint8_t *p, *endp, *srcp = (const uint8_t *)src;
308 AVBPrint dstbuf;
309 AVBPrint invalid_seq;
310 8920 int invalid_chars_nb = 0, ret = 0;
311
312 8920 *dstp = NULL;
313 8920 av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
314 8920 av_bprint_init(&invalid_seq, 0, AV_BPRINT_SIZE_UNLIMITED);
315
316 8920 endp = srcp + strlen(src);
317
2/2
✓ Branch 0 taken 257848 times.
✓ Branch 1 taken 8920 times.
266768 for (p = srcp; *p;) {
318 int32_t code;
319 257848 int invalid = 0;
320 257848 const uint8_t *p0 = p;
321
322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 257848 times.
257848 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 257848 times.
257848 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 257848 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
257848 if (!invalid || tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_IGNORE)
348 257848 av_bprint_append_data(&dstbuf, p0, p-p0);
349 }
350
351
1/4
✓ Branch 0 taken 8920 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8920 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 8920 end:
357 8920 av_bprint_finalize(&dstbuf, dstp);
358 8920 av_bprint_finalize(&invalid_seq, NULL);
359 8920 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 43553 static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_size, struct unit_value uv)
375 {
376 double vald;
377 43553 int64_t vali = 0;
378 43553 const char *float_fmt = 0;
379
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43553 times.
43553 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 33169 times.
✓ Branch 1 taken 10384 times.
43553 } else if (uv.fmt >= AV_TEXTFORMAT_VALUE_FMT_DOUBLE) {
384 33169 vald = uv.val.d;
385 33169 float_fmt = float_fmt_full;
386 } else {
387 10384 vald = (double)uv.val.i;
388 10384 vali = uv.val.i;
389 }
390
391
3/4
✓ Branch 0 taken 33169 times.
✓ Branch 1 taken 10384 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33169 times.
43553 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 43553 const char *prefix_string = "";
402
403
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 43553 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
43553 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 10384 times.
✓ Branch 1 taken 33169 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10384 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
43553 if (float_fmt || (tctx->opts.use_value_prefix && vald != (int64_t)vald))
421
1/2
✓ Branch 0 taken 33169 times.
✗ Branch 1 not taken.
33169 snprintf(buf, buf_size, float_fmt ? float_fmt : "%f", vald);
422 else
423 10384 snprintf(buf, buf_size, "%"PRId64, vali);
424
425
2/8
✓ Branch 0 taken 43553 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43553 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
43553 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 43553 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
43553 prefix_string, tctx->opts.show_value_unit && uv.unit ? uv.unit : "");
427 }
428
429 43553 return buf;
430 }
431
432
433 10384 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 10384 times.
10384 av_assert0(fmt < AV_TEXTFORMAT_VALUE_FMT_DOUBLE);
439
440 10384 uv.val.i = val;
441 10384 uv.fmt = fmt;
442 10384 uv.unit = unit;
443 10384 avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0);
444 10384 }
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 102551 int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags)
461 {
462 const AVTextFormatSection *section;
463 102551 int ret = 0;
464
465
4/8
✓ Branch 0 taken 102551 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102551 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 102551 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 102551 times.
102551 av_assert0(key && val && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
466
467 102551 section = tctx->section[tctx->level];
468
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102551 times.
102551 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
470 return 0;
471
472
1/2
✓ Branch 0 taken 102551 times.
✗ Branch 1 not taken.
102551 if (tctx->opts.show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
473
2/2
✓ Branch 0 taken 9725 times.
✓ Branch 1 taken 92826 times.
102551 && (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
474
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 9433 times.
9725 && !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
475 292 return 0;
476
477
3/4
✓ Branch 0 taken 102259 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 45561 times.
✓ Branch 4 taken 56698 times.
102259 if (!tctx->opts.is_key_selected || tctx->opts.is_key_selected(tctx, key)) {
478
2/2
✓ Branch 0 taken 4460 times.
✓ Branch 1 taken 41101 times.
45561 if (flags & AV_TEXTFORMAT_PRINT_STRING_VALIDATE) {
479 4460 char *key1 = NULL, *val1 = NULL;
480 4460 ret = validate_string(tctx, &key1, key);
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4460 times.
4460 if (ret < 0) goto end;
482 4460 ret = validate_string(tctx, &val1, val);
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4460 times.
4460 if (ret < 0) goto end;
484 4460 tctx->formatter->print_string(tctx, key1, val1);
485 4460 end:
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4460 times.
4460 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 4460 av_free(key1);
491 4460 av_free(val1);
492 } else {
493 41101 tctx->formatter->print_string(tctx, key, val);
494 }
495
496 45561 tctx->nb_item[tctx->level]++;
497 }
498
499 102259 return ret;
500 }
501
502 2921 void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep)
503 {
504 char buf[44];
505 2921 snprintf(buf, sizeof(buf), "%d%c%d", q.num, sep, q.den);
506 2921 avtext_print_string(tctx, key, buf, 0);
507 2921 }
508
509 34134 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 24307 times.
✓ Branch 1 taken 9827 times.
✓ Branch 2 taken 23348 times.
✓ Branch 3 taken 959 times.
✓ Branch 4 taken 9827 times.
✓ Branch 5 taken 23348 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 9821 times.
34134 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
513 965 avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
514 } else {
515 char buf[128];
516 33169 double d = av_q2d(*time_base) * ts;
517 struct unit_value uv;
518 33169 uv.val.d = d;
519 33169 uv.fmt = AV_TEXTFORMAT_VALUE_FMT_SECOND;
520 33169 uv.unit = unit_second_str;
521 33169 value_string(tctx, buf, sizeof(buf), uv);
522 33169 avtext_print_string(tctx, key, buf, 0);
523 }
524 34134 }
525
526 33934 void avtext_print_ts(AVTextFormatContext *tctx, const char *key, int64_t ts, int is_duration)
527 {
528
8/8
✓ Branch 0 taken 24107 times.
✓ Branch 1 taken 9827 times.
✓ Branch 2 taken 23166 times.
✓ Branch 3 taken 941 times.
✓ Branch 4 taken 9827 times.
✓ Branch 5 taken 23166 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 9821 times.
33934 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0))
529 947 avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
530 else
531 32987 avtext_print_integer(tctx, key, ts, 0);
532 33934 }
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 6607 void avtext_print_data_hash(AVTextFormatContext *tctx, const char *key,
591 const uint8_t *data, int size)
592 {
593 6607 char buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
594 int len;
595
596
2/2
✓ Branch 0 taken 4184 times.
✓ Branch 1 taken 2423 times.
6607 if (!tctx->hash)
597 4184 return;
598
599 2423 av_hash_init(tctx->hash);
600 2423 av_hash_update(tctx->hash, data, size);
601 2423 len = snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(tctx->hash));
602 2423 av_hash_final_hex(tctx->hash, (uint8_t *)&buf[len], (int)sizeof(buf) - len);
603 2423 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 287 int avtextwriter_context_close(AVTextWriterContext **pwctx)
629 {
630 287 AVTextWriterContext *wctx = *pwctx;
631 287 int ret = 0;
632
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (!wctx)
634 return AVERROR(EINVAL);
635
636
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 if (wctx->writer) {
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (wctx->writer->uninit)
638 ret = wctx->writer->uninit(wctx);
639
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 if (wctx->writer->priv_class)
640 287 av_opt_free(wctx->priv);
641 }
642 287 av_freep(&wctx->priv);
643 287 av_freep(pwctx);
644 287 return ret;
645 }
646
647
648 287 int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
649 {
650 AVTextWriterContext *wctx;
651 287 int ret = 0;
652
653
2/4
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 287 times.
287 if (!pwctx || !writer)
654 return AVERROR(EINVAL);
655
656
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 287 times.
287 if (!((wctx = av_mallocz(sizeof(AVTextWriterContext))))) {
657 ret = AVERROR(ENOMEM);
658 goto fail;
659 }
660
661
2/4
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 287 times.
287 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 287 times.
✗ Branch 1 not taken.
287 if (writer->priv_class) {
667 287 void *priv_ctx = wctx->priv;
668 287 *(const AVClass **)priv_ctx = writer->priv_class;
669 287 av_opt_set_defaults(priv_ctx);
670 }
671
672 287 wctx->class = &textwriter_class;
673 287 wctx->writer = writer;
674
675 287 av_opt_set_defaults(wctx);
676
677
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (wctx->writer->init)
679 ret = wctx->writer->init(wctx);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (ret < 0)
681 goto fail;
682
683 287 *pwctx = wctx;
684
685 287 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 287 const AVTextFormatter *avtext_get_formatter_by_name(const char *name)
707 {
708
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 for (int i = 0; registered_formatters[i]; i++) {
709 const char *end;
710
2/2
✓ Branch 1 taken 287 times.
✓ Branch 2 taken 99 times.
386 if (av_strstart(name, registered_formatters[i]->name, &end) &&
711
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
287 (*end == '\0' || *end == '='))
712 287 return registered_formatters[i];
713 }
714
715 return NULL;
716 }
717