FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tf_ini.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 
29 #include "libavutil/bprint.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 /* Default output */
48 
49 typedef struct DefaultContext {
50  const AVClass *class;
51  int nokey;
52  int noprint_wrappers;
55 
56 /* INI format output */
57 
58 typedef struct INIContext {
59  const AVClass *class;
61 } INIContext;
62 
63 #undef OFFSET
64 #define OFFSET(x) offsetof(INIContext, x)
65 
66 static const AVOption ini_options[] = {
67  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
68  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
69  {NULL},
70 };
71 
73 
74 static char *ini_escape_str(AVBPrint *dst, const char *src)
75 {
76  int i = 0;
77  char c = 0;
78 
79  while (c = src[i++]) {
80  switch (c) {
81  case '\b': av_bprintf(dst, "%s", "\\b"); break;
82  case '\f': av_bprintf(dst, "%s", "\\f"); break;
83  case '\n': av_bprintf(dst, "%s", "\\n"); break;
84  case '\r': av_bprintf(dst, "%s", "\\r"); break;
85  case '\t': av_bprintf(dst, "%s", "\\t"); break;
86  case '\\':
87  case '#' :
88  case '=' :
89  case ':' : av_bprint_chars(dst, '\\', 1);
90  default:
91  if ((unsigned char)c < 32)
92  av_bprintf(dst, "\\x00%02x", c & 0xff);
93  else
94  av_bprint_chars(dst, c, 1);
95  break;
96  }
97  }
98  return dst->str;
99 }
100 
101 static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
102 {
103  INIContext *ini = wctx->priv;
104  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
105  const struct AVTextFormatSection *section = wctx->section[wctx->level];
106  const struct AVTextFormatSection *parent_section = wctx->level ?
107  wctx->section[wctx->level-1] : NULL;
108 
109  av_bprint_clear(buf);
110  if (!parent_section) {
111  writer_put_str(wctx, "# ffprobe output\n\n");
112  return;
113  }
114 
115  if (wctx->nb_item[wctx->level-1])
116  writer_w8(wctx, '\n');
117 
118  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
119  if (ini->hierarchical ||
121  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
122 
123  if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
124  int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE ?
125  wctx->nb_item_type[wctx->level-1][section->id] :
126  wctx->nb_item[wctx->level-1];
127  av_bprintf(buf, ".%d", n);
128  }
129  }
130 
132  writer_printf(wctx, "[%s]\n", buf->str);
133 }
134 
135 static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
136 {
137  AVBPrint buf;
138 
140  writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
141  av_bprint_clear(&buf);
142  writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
143  av_bprint_finalize(&buf, NULL);
144 }
145 
146 static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
147 {
148  writer_printf(wctx, "%s=%"PRId64"\n", key, value);
149 }
150 
152  .name = "ini",
153  .priv_size = sizeof(INIContext),
154  .print_section_header = ini_print_section_header,
155  .print_integer = ini_print_int,
156  .print_string = ini_print_str,
158  .priv_class = &ini_class,
159 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
ini_print_str
static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
Definition: tf_ini.c:135
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
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
writer_printf
#define writer_printf(wctx_, fmt_,...)
Definition: tf_ini.c:34
DefaultContext
Definition: tf_default.c:48
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
avtextformat.h
AVTextFormatContext
Definition: avtextformat.h:88
DefaultContext::nokey
int nokey
Definition: tf_default.c:50
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
INIContext
Definition: tf_ini.c:58
AVTextFormatSection::name
const char * name
Definition: avtextformat.h:39
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
ini_print_section_header
static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition: tf_ini.c:101
ini_options
static const AVOption ini_options[]
Definition: tf_ini.c:66
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:48
AVTextFormatter
Definition: avtextformat.h:69
AVTextFormatSection
Definition: avtextformat.h:37
limits.h
AVTextFormatContext::priv
void * priv
private data for use by the filter
Definition: avtextformat.h:94
OFFSET
#define OFFSET(x)
Definition: tf_ini.c:64
key
const char * key
Definition: hwcontext_opencl.c:189
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SECTION_MAX_NB_LEVELS
#define SECTION_MAX_NB_LEVELS
Definition: avtextformat.h:85
ini_print_int
static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition: tf_ini.c:146
DefaultContext::noprint_wrappers
int noprint_wrappers
Definition: tf_default.c:51
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
writer_put_str
#define writer_put_str(wctx_, str_)
Definition: tf_ini.c:33
writer_w8
#define writer_w8(wctx_, b_)
Definition: tf_ini.c:32
DefaultContext::nested_section
int nested_section[SECTION_MAX_NB_LEVELS]
Definition: tf_default.c:52
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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
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
ini_escape_str
static char * ini_escape_str(AVBPrint *dst, const char *src)
Definition: tf_ini.c:74
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
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
avtextformatter_ini
const AVTextFormatter avtextformatter_ini
Definition: tf_ini.c:151
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
DEFINE_FORMATTER_CLASS
#define DEFINE_FORMATTER_CLASS(name)
Definition: tf_ini.c:36
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_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
INIContext::hierarchical
int hierarchical
Definition: tf_ini.c:60
src
#define src
Definition: vp8dsp.c:248