FFmpeg
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 <limits.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "avtextformat.h"
28 #include <libavutil/mem.h>
29 #include <libavutil/avassert.h>
30 #include <libavutil/bprint.h>
31 #include <libavutil/error.h>
32 #include <libavutil/macros.h>
33 #include <libavutil/opt.h>
34 
35 #define writer_w8(wctx_, b_) (wctx_)->writer->writer->writer_w8((wctx_)->writer, b_)
36 #define writer_put_str(wctx_, str_) (wctx_)->writer->writer->writer_put_str((wctx_)->writer, str_)
37 #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer->writer->writer_printf((wctx_)->writer, fmt_, __VA_ARGS__)
38 
39 #define DEFINE_FORMATTER_CLASS(name) \
40 static const char *name##_get_name(void *ctx) \
41 { \
42  return #name ; \
43 } \
44 static const AVClass name##_class = { \
45  .class_name = #name, \
46  .item_name = name##_get_name, \
47  .option = name##_options \
48 }
49 
50 
51 /* Flat output */
52 
53 typedef struct FlatContext {
54  const AVClass *class;
55  const char *sep_str;
56  char sep;
58 } FlatContext;
59 
60 #undef OFFSET
61 #define OFFSET(x) offsetof(FlatContext, x)
62 
63 static const AVOption flat_options[]= {
64  {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
65  {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
66  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
67  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
68  {NULL},
69 };
70 
72 
74 {
75  FlatContext *flat = wctx->priv;
76 
77  if (strlen(flat->sep_str) != 1) {
78  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
79  flat->sep_str);
80  return AVERROR(EINVAL);
81  }
82  flat->sep = flat->sep_str[0];
83 
84  return 0;
85 }
86 
87 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
88 {
89  const char *p;
90 
91  for (p = src; *p; p++) {
92  if (!((*p >= '0' && *p <= '9') ||
93  (*p >= 'a' && *p <= 'z') ||
94  (*p >= 'A' && *p <= 'Z')))
95  av_bprint_chars(dst, '_', 1);
96  else
97  av_bprint_chars(dst, *p, 1);
98  }
99  return dst->str;
100 }
101 
102 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
103 {
104  const char *p;
105 
106  for (p = src; *p; p++) {
107  switch (*p) {
108  case '\n': av_bprintf(dst, "%s", "\\n"); break;
109  case '\r': av_bprintf(dst, "%s", "\\r"); break;
110  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
111  case '"': av_bprintf(dst, "%s", "\\\""); break;
112  case '`': av_bprintf(dst, "%s", "\\`"); break;
113  case '$': av_bprintf(dst, "%s", "\\$"); break;
114  default: av_bprint_chars(dst, *p, 1); break;
115  }
116  }
117  return dst->str;
118 }
119 
120 static void flat_print_section_header(AVTextFormatContext *wctx, const void *data)
121 {
122  FlatContext *flat = wctx->priv;
123  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
124  const struct AVTextFormatSection *section = wctx->section[wctx->level];
125  const struct AVTextFormatSection *parent_section = wctx->level ?
126  wctx->section[wctx->level-1] : NULL;
127 
128  /* build section header */
129  av_bprint_clear(buf);
130  if (!parent_section)
131  return;
132  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
133 
134  if (flat->hierarchical ||
136  av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
137 
138  if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
139  int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE ?
140  wctx->nb_item_type[wctx->level-1][section->id] :
141  wctx->nb_item[wctx->level-1];
142  av_bprintf(buf, "%d%s", n, flat->sep_str);
143  }
144  }
145 }
146 
147 static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
148 {
149  writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value);
150 }
151 
152 static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
153 {
154  FlatContext *flat = wctx->priv;
155  AVBPrint buf;
156 
157  writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
159  writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
160  av_bprint_clear(&buf);
161  writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
162  av_bprint_finalize(&buf, NULL);
163 }
164 
166  .name = "flat",
167  .priv_size = sizeof(FlatContext),
168  .init = flat_init,
169  .print_section_header = flat_print_section_header,
170  .print_integer = flat_print_int,
171  .print_string = flat_print_str,
173  .priv_class = &flat_class,
174 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVTextFormatContext::section
const struct AVTextFormatSection * section[SECTION_MAX_NB_LEVELS]
section per each level
Definition: avtextformat.h:106
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
AVTextFormatContext::nb_item_type
unsigned int nb_item_type[SECTION_MAX_NB_LEVELS][SECTION_MAX_NB_SECTIONS]
Definition: avtextformat.h:103
int64_t
long long int64_t
Definition: coverity.c:34
flat_print_str
static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
Definition: tf_flat.c:152
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
avtextformat.h
AVTextFormatContext
Definition: avtextformat.h:88
flat_escape_value_str
static const char * flat_escape_value_str(AVBPrint *dst, const char *src)
Definition: tf_flat.c:102
flat_print_int
static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition: tf_flat.c:147
AVTextFormatSection::id
int id
unique id identifying a section
Definition: avtextformat.h:38
AVTextFormatContext::level
int level
current level, starting from 0
Definition: avtextformat.h:99
AVTextFormatSection::name
const char * name
Definition: avtextformat.h:39
OFFSET
#define OFFSET(x)
Definition: tf_flat.c:61
macros.h
AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
#define AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
the items in this array section should be numbered individually by type
Definition: avtextformat.h:46
writer_printf
#define writer_printf(wctx_, fmt_,...)
Definition: tf_flat.c:37
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:48
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
writer_put_str
#define writer_put_str(wctx_, str_)
Definition: tf_flat.c:36
AVTextFormatter
Definition: avtextformat.h:69
AVTextFormatSection
Definition: avtextformat.h:37
FlatContext
Definition: tf_flat.c:53
limits.h
AVTextFormatContext::priv
void * priv
private data for use by the filter
Definition: avtextformat.h:94
key
const char * key
Definition: hwcontext_opencl.c:189
flat_init
static av_cold int flat_init(AVTextFormatContext *wctx)
Definition: tf_flat.c:73
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
FlatContext::hierarchical
int hierarchical
Definition: tf_flat.c:57
error.h
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AVTextFormatter::name
const char * name
Definition: avtextformat.h:72
AVTextFormatContext::section_pbuf
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]
generic print buffer dedicated to each section, used by various formatters
Definition: avtextformat.h:107
flat_print_section_header
static void flat_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition: tf_flat.c:120
bprint.h
DEFINE_FORMATTER_CLASS
#define DEFINE_FORMATTER_CLASS(name)
Definition: tf_flat.c:39
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT
#define AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT
Definition: avtextformat.h:60
flat_escape_key_str
static const char * flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
Definition: tf_flat.c:87
AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER
#define AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER
the section only contains other sections, but has no data at its own level
Definition: avtextformat.h:41
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
avtextformatter_flat
const AVTextFormatter avtextformatter_flat
Definition: tf_flat.c:165
FlatContext::sep_str
const char * sep_str
Definition: tf_flat.c:55
mem.h
AVTextFormatContext::nb_item
unsigned int nb_item[SECTION_MAX_NB_LEVELS]
number of the item printed in the given section, starting from 0
Definition: avtextformat.h:102
FlatContext::sep
char sep
Definition: tf_flat.c:56
flat_options
static const AVOption flat_options[]
Definition: tf_flat.c:63
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
flat
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)
Definition: vf_waveform.c:1104
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS
#define AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS
Definition: avtextformat.h:59
AV_OPT_TYPE_STRING
@ 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:276
AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY
#define AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY
the section contains an array of elements of the same type
Definition: avtextformat.h:42
src
#define src
Definition: vp8dsp.c:248