FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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/bprint.h"
29 #include "libavutil/error.h"
30 #include "libavutil/opt.h"
31 
32 #define writer_w8(wctx_, b_) (wctx_)->writer->writer->writer_w8((wctx_)->writer, b_)
33 #define writer_put_str(wctx_, str_) (wctx_)->writer->writer->writer_put_str((wctx_)->writer, str_)
34 #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer->writer->writer_printf((wctx_)->writer, fmt_, __VA_ARGS__)
35 
36 #define DEFINE_FORMATTER_CLASS(name) \
37 static const char *name##_get_name(void *ctx) \
38 { \
39  return #name ; \
40 } \
41 static const AVClass name##_class = { \
42  .class_name = #name, \
43  .item_name = name##_get_name, \
44  .option = name##_options \
45 }
46 
47 
48 /* Flat output */
49 
50 typedef struct FlatContext {
51  const AVClass *class;
52  const char *sep_str;
53  char sep;
55 } FlatContext;
56 
57 #undef OFFSET
58 #define OFFSET(x) offsetof(FlatContext, x)
59 
60 static const AVOption flat_options[]= {
61  {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
62  {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, 0, 0 },
63  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
64  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
65  {NULL},
66 };
67 
69 
71 {
72  FlatContext *flat = wctx->priv;
73 
74  if (strlen(flat->sep_str) != 1) {
75  av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
76  flat->sep_str);
77  return AVERROR(EINVAL);
78  }
79  flat->sep = flat->sep_str[0];
80 
81  return 0;
82 }
83 
84 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
85 {
86  const char *p;
87 
88  for (p = src; *p; p++) {
89  if (!((*p >= '0' && *p <= '9') ||
90  (*p >= 'a' && *p <= 'z') ||
91  (*p >= 'A' && *p <= 'Z')))
92  av_bprint_chars(dst, '_', 1);
93  else
94  av_bprint_chars(dst, *p, 1);
95  }
96  return dst->str;
97 }
98 
99 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
100 {
101  const char *p;
102 
103  for (p = src; *p; p++) {
104  switch (*p) {
105  case '\n': av_bprintf(dst, "%s", "\\n"); break;
106  case '\r': av_bprintf(dst, "%s", "\\r"); break;
107  case '\\': av_bprintf(dst, "%s", "\\\\"); break;
108  case '"': av_bprintf(dst, "%s", "\\\""); break;
109  case '`': av_bprintf(dst, "%s", "\\`"); break;
110  case '$': av_bprintf(dst, "%s", "\\$"); break;
111  default: av_bprint_chars(dst, *p, 1); break;
112  }
113  }
114  return dst->str;
115 }
116 
117 static void flat_print_section_header(AVTextFormatContext *wctx, const void *data)
118 {
119  FlatContext *flat = wctx->priv;
120  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
121  const struct AVTextFormatSection *section = wctx->section[wctx->level];
122  const struct AVTextFormatSection *parent_section = wctx->level ?
123  wctx->section[wctx->level-1] : NULL;
124 
125  /* build section header */
126  av_bprint_clear(buf);
127  if (!parent_section)
128  return;
129  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
130 
131  if (flat->hierarchical ||
133  av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
134 
135  if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
136  int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE ?
137  wctx->nb_item_type[wctx->level-1][section->id] :
138  wctx->nb_item[wctx->level-1];
139  av_bprintf(buf, "%d%s", n, flat->sep_str);
140  }
141  }
142 }
143 
144 static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
145 {
146  writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value);
147 }
148 
149 static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
150 {
151  FlatContext *flat = wctx->priv;
152  AVBPrint buf;
153 
154  writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
156  writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
157  av_bprint_clear(&buf);
158  writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
159  av_bprint_finalize(&buf, NULL);
160 }
161 
163  .name = "flat",
164  .priv_size = sizeof(FlatContext),
165  .init = flat_init,
166  .print_section_header = flat_print_section_header,
167  .print_integer = flat_print_int,
168  .print_string = flat_print_str,
170  .priv_class = &flat_class,
171 };
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:149
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:99
flat_print_int
static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition: tf_flat.c:144
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:58
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:34
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:48
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:90
writer_put_str
#define writer_put_str(wctx_, str_)
Definition: tf_flat.c:33
AVTextFormatter
Definition: avtextformat.h:69
AVTextFormatSection
Definition: avtextformat.h:37
FlatContext
Definition: tf_flat.c:50
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:70
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
FlatContext::hierarchical
int hierarchical
Definition: tf_flat.c:54
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:117
bprint.h
DEFINE_FORMATTER_CLASS
#define DEFINE_FORMATTER_CLASS(name)
Definition: tf_flat.c:36
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:84
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:162
FlatContext::sep_str
const char * sep_str
Definition: tf_flat.c:52
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:53
flat_options
static const AVOption flat_options[]
Definition: tf_flat.c:60
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