FFmpeg
Loading...
Searching...
No Matches
tf_flat.c
Go to the documentation of this file.
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 <string.h>
25
26#include "avtextformat.h"
27#include "libavutil/bprint.h"
28#include "libavutil/error.h"
29#include "libavutil/opt.h"
30#include "tf_internal.h"
31
32/* Flat output */
33
34typedef struct FlatContext {
35 const AVClass *class;
36 const char *sep_str;
37 char sep;
40
41#undef OFFSET
42#define OFFSET(x) offsetof(FlatContext, x)
43
44static const AVOption flat_options[] = {
45 { "sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 },
46 { "s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 },
47 { "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
48 { "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
49 { NULL },
50};
51
53
55{
56 FlatContext *flat = wctx->priv;
57
58 if (strlen(flat->sep_str) != 1) {
59 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
60 flat->sep_str);
61 return AVERROR(EINVAL);
62 }
63 flat->sep = flat->sep_str[0];
64
65 return 0;
66}
67
68static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
69{
70 const char *p;
71
72 for (p = src; *p; p++) {
73 if (!((*p >= '0' && *p <= '9') ||
74 (*p >= 'a' && *p <= 'z') ||
75 (*p >= 'A' && *p <= 'Z')))
76 av_bprint_chars(dst, '_', 1);
77 else
78 av_bprint_chars(dst, *p, 1);
79 }
80 return dst->str;
81}
82
83static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
84{
85 const char *p;
86
87 for (p = src; *p; p++) {
88 switch (*p) {
89 case '\n': av_bprintf(dst, "%s", "\\n"); break;
90 case '\r': av_bprintf(dst, "%s", "\\r"); break;
91 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
92 case '"': av_bprintf(dst, "%s", "\\\""); break;
93 case '`': av_bprintf(dst, "%s", "\\`"); break;
94 case '$': av_bprintf(dst, "%s", "\\$"); break;
95 default: av_bprint_chars(dst, *p, 1); break;
96 }
97 }
98 return dst->str;
99}
100
102{
103 FlatContext *flat = wctx->priv;
104 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
105 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
106 const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
107
108 if (!section)
109 return;
110
111 /* build section header */
112 av_bprint_clear(buf);
113 if (!parent_section)
114 return;
115
116 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
117
118 if (flat->hierarchical ||
120 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
121
122 if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
124 ? wctx->nb_item_type[wctx->level - 1][section->id]
125 : wctx->nb_item[wctx->level - 1];
126
127 av_bprintf(buf, "%d%s", n, flat->sep_str);
128 }
129 }
130}
131
132static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
133{
134 writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value);
135}
136
137static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
138{
139 FlatContext *flat = wctx->priv;
140 AVBPrint buf;
141
142 writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
144 writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
145 av_bprint_clear(&buf);
146 writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
148}
149
151 .name = "flat",
152 .priv_size = sizeof(FlatContext),
153 .init = flat_init,
154 .print_section_header = flat_print_section_header,
155 .print_integer = flat_print_int,
156 .print_string = flat_print_str,
158 .priv_class = &flat_class,
159};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS
#define AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER
the section only contains other sections, but has no data at its own level
#define AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY
the section contains an array of elements of the same type
#define AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT
#define AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
the items in this array section should be numbered individually by type
const AVTextFormatter avtextformatter_flat
Definition tf_flat.c:150
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:69
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
error code definitions
double value
Definition eval.c:102
const char * key
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:235
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:130
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition bprint.c:227
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define av_cold
Definition attributes.h:117
const char data[16]
Definition mxf.c:149
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
unsigned int nb_item_type[SECTION_MAX_NB_LEVELS][SECTION_MAX_NB_SECTIONS]
void * priv
private data for use by the filter
const AVTextFormatSection * section[SECTION_MAX_NB_LEVELS]
section per each level
int level
current level, starting from 0
unsigned int nb_item[SECTION_MAX_NB_LEVELS]
number of the item printed in the given section, starting from 0
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]
generic print buffer dedicated to each section, used by various formatters
const char * name
int id
unique id identifying a section
const char * sep_str
Definition tf_flat.c:36
char sep
Definition tf_flat.c:37
int hierarchical
Definition tf_flat.c:38
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition tf_flat.c:132
static const char * flat_escape_value_str(AVBPrint *dst, const char *src)
Definition tf_flat.c:83
static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
Definition tf_flat.c:137
static void flat_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition tf_flat.c:101
static av_cold int flat_init(AVTextFormatContext *wctx)
Definition tf_flat.c:54
static const char * flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
Definition tf_flat.c:68
static const AVOption flat_options[]
Definition tf_flat.c:44
#define OFFSET(x)
Definition tf_flat.c:42
Internal utilities for text formatters.
static const AVTextFormatSection * tf_get_section(AVTextFormatContext *tfc, int level)
Safely validate and access a section at a given level.
Definition tf_internal.h:42
static const AVTextFormatSection * tf_get_parent_section(AVTextFormatContext *tfc, int level)
Safely access the parent section.
Definition tf_internal.h:55
#define DEFINE_FORMATTER_CLASS(name)
Definition tf_internal.h:31
static void writer_printf(AVTextFormatContext *wctx, const char *fmt,...)
Definition tf_internal.h:73
static void writer_put_str(AVTextFormatContext *wctx, const char *str)
Definition tf_internal.h:68
static av_always_inline void flat(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)