fftools/textformat: Introduce common header and deduplicate code

Also change writer_printf signature in AVTextWriter to use va_list,
so that it can be called by the new function writer_printf()
in tf_internal.h.

Reviewed-by: Stefano Sabatini <stefasab@gmail.com>
Signed-off-by: softworkz <softworkz@hotmail.com>
This commit is contained in:
softworkz
2025-04-29 01:35:50 +02:00
parent e4830b8c5e
commit cee7b8a051
11 changed files with 160 additions and 141 deletions

View File

@@ -27,22 +27,7 @@
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/opt.h"
#define writer_w8(wctx_, b_) (wctx_)->writer->writer->writer_w8((wctx_)->writer, b_)
#define writer_put_str(wctx_, str_) (wctx_)->writer->writer->writer_put_str((wctx_)->writer, str_)
#define writer_printf(wctx_, fmt_, ...) (wctx_)->writer->writer->writer_printf((wctx_)->writer, fmt_, __VA_ARGS__)
#define DEFINE_FORMATTER_CLASS(name) \
static const char *name##_get_name(void *ctx) \
{ \
return #name ; \
} \
static const AVClass name##_class = { \
.class_name = #name, \
.item_name = name##_get_name, \
.option = name##_options \
}
#include "tf_internal.h"
/* JSON output */
@@ -103,10 +88,13 @@ static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx
static void json_print_section_header(AVTextFormatContext *wctx, const void *data)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
AVBPrint buf;
const AVTextFormatSection *section = wctx->section[wctx->level];
const AVTextFormatSection *parent_section = wctx->level ? wctx->section[wctx->level - 1] : NULL;
if (!section)
return;
if (wctx->level && wctx->nb_item[wctx->level - 1])
writer_put_str(wctx, ",\n");
@@ -141,8 +129,11 @@ static void json_print_section_header(AVTextFormatContext *wctx, const void *dat
static void json_print_section_footer(AVTextFormatContext *wctx)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
const struct AVTextFormatSection *section = wctx->section[wctx->level];
if (!section)
return;
if (wctx->level == 0) {
json->indent_level--;
@@ -175,9 +166,12 @@ static inline void json_print_item_str(AVTextFormatContext *wctx,
static void json_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
const struct AVTextFormatSection *parent_section = wctx->level ?
wctx->section[wctx->level-1] : NULL;
if (!section)
return;
if (wctx->nb_item[wctx->level] || (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE))
writer_put_str(wctx, json->item_sep);
@@ -188,10 +182,14 @@ static void json_print_str(AVTextFormatContext *wctx, const char *key, const cha
static void json_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
const AVTextFormatSection *parent_section = wctx->level ? wctx->section[wctx->level - 1] : NULL;
AVBPrint buf;
if (!section)
return;
if (wctx->nb_item[wctx->level] || (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE))
writer_put_str(wctx, json->item_sep);
if (!json->compact)
@@ -213,4 +211,3 @@ const AVTextFormatter avtextformatter_json = {
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
.priv_class = &json_class,
};