FFmpeg
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 #include <libavutil/mem.h>
29 #include <libavutil/avassert.h>
30 #include <libavutil/bprint.h>
31 #include <libavutil/opt.h>
32 
33 #define writer_w8(wctx_, b_) (wctx_)->writer->writer->writer_w8((wctx_)->writer, b_)
34 #define writer_put_str(wctx_, str_) (wctx_)->writer->writer->writer_put_str((wctx_)->writer, str_)
35 #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer->writer->writer_printf((wctx_)->writer, fmt_, __VA_ARGS__)
36 
37 #define DEFINE_FORMATTER_CLASS(name) \
38 static const char *name##_get_name(void *ctx) \
39 { \
40  return #name ; \
41 } \
42 static const AVClass name##_class = { \
43  .class_name = #name, \
44  .item_name = name##_get_name, \
45  .option = name##_options \
46 }
47 
48 /* Default output */
49 
50 typedef struct DefaultContext {
51  const AVClass *class;
52  int nokey;
53  int noprint_wrappers;
56 
57 /* INI format output */
58 
59 typedef struct INIContext {
60  const AVClass *class;
62 } INIContext;
63 
64 #undef OFFSET
65 #define OFFSET(x) offsetof(INIContext, x)
66 
67 static const AVOption ini_options[] = {
68  {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
69  {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1 },
70  {NULL},
71 };
72 
74 
75 static char *ini_escape_str(AVBPrint *dst, const char *src)
76 {
77  int i = 0;
78  char c = 0;
79 
80  while (c = src[i++]) {
81  switch (c) {
82  case '\b': av_bprintf(dst, "%s", "\\b"); break;
83  case '\f': av_bprintf(dst, "%s", "\\f"); break;
84  case '\n': av_bprintf(dst, "%s", "\\n"); break;
85  case '\r': av_bprintf(dst, "%s", "\\r"); break;
86  case '\t': av_bprintf(dst, "%s", "\\t"); break;
87  case '\\':
88  case '#' :
89  case '=' :
90  case ':' : av_bprint_chars(dst, '\\', 1);
91  default:
92  if ((unsigned char)c < 32)
93  av_bprintf(dst, "\\x00%02x", c & 0xff);
94  else
95  av_bprint_chars(dst, c, 1);
96  break;
97  }
98  }
99  return dst->str;
100 }
101 
102 static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
103 {
104  INIContext *ini = wctx->priv;
105  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
106  const struct AVTextFormatSection *section = wctx->section[wctx->level];
107  const struct AVTextFormatSection *parent_section = wctx->level ?
108  wctx->section[wctx->level-1] : NULL;
109 
110  av_bprint_clear(buf);
111  if (!parent_section) {
112  writer_put_str(wctx, "# ffprobe output\n\n");
113  return;
114  }
115 
116  if (wctx->nb_item[wctx->level-1])
117  writer_w8(wctx, '\n');
118 
119  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
120  if (ini->hierarchical ||
122  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
123 
124  if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
125  int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE ?
126  wctx->nb_item_type[wctx->level-1][section->id] :
127  wctx->nb_item[wctx->level-1];
128  av_bprintf(buf, ".%d", n);
129  }
130  }
131 
133  writer_printf(wctx, "[%s]\n", buf->str);
134 }
135 
136 static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
137 {
138  AVBPrint buf;
139 
141  writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
142  av_bprint_clear(&buf);
143  writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
144  av_bprint_finalize(&buf, NULL);
145 }
146 
147 static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
148 {
149  writer_printf(wctx, "%s=%"PRId64"\n", key, value);
150 }
151 
153  .name = "ini",
154  .priv_size = sizeof(INIContext),
155  .print_section_header = ini_print_section_header,
156  .print_integer = ini_print_int,
157  .print_string = ini_print_str,
159  .priv_class = &ini_class,
160 };
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:136
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:35
DefaultContext
Definition: tf_default.c:50
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:52
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:59
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:102
ini_options
static const AVOption ini_options[]
Definition: tf_ini.c:67
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:48
avassert.h
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:65
key
const char * key
Definition: hwcontext_opencl.c:189
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:147
DefaultContext::noprint_wrappers
int noprint_wrappers
Definition: tf_default.c:53
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:34
writer_w8
#define writer_w8(wctx_, b_)
Definition: tf_ini.c:33
DefaultContext::nested_section
int nested_section[SECTION_MAX_NB_LEVELS]
Definition: tf_default.c:54
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:75
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
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
avtextformatter_ini
const AVTextFormatter avtextformatter_ini
Definition: tf_ini.c:152
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:37
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:61
src
#define src
Definition: vp8dsp.c:248