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 
29 #include "libavutil/bprint.h"
30 #include "libavutil/opt.h"
31 #include "tf_internal.h"
32 
33 /* Default output */
34 
35 typedef struct DefaultContext {
36  const AVClass *class;
37  int nokey;
38  int noprint_wrappers;
41 
42 /* INI format output */
43 
44 typedef struct INIContext {
45  const AVClass *class;
47 } INIContext;
48 
49 #undef OFFSET
50 #define OFFSET(x) offsetof(INIContext, x)
51 
52 static const AVOption ini_options[] = {
53  { "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
54  { "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
55  { NULL },
56 };
57 
59 
60 static char *ini_escape_str(AVBPrint *dst, const char *src)
61 {
62  int i = 0;
63  char c;
64 
65  while ((c = src[i++])) {
66  switch (c) {
67  case '\b': av_bprintf(dst, "%s", "\\b"); break;
68  case '\f': av_bprintf(dst, "%s", "\\f"); break;
69  case '\n': av_bprintf(dst, "%s", "\\n"); break;
70  case '\r': av_bprintf(dst, "%s", "\\r"); break;
71  case '\t': av_bprintf(dst, "%s", "\\t"); break;
72  case '\\':
73  case '#':
74  case '=':
75  case ':':
76  av_bprint_chars(dst, '\\', 1);
77  /* fallthrough */
78  default:
79  if ((unsigned char)c < 32)
80  av_bprintf(dst, "\\x00%02x", (unsigned char)c);
81  else
82  av_bprint_chars(dst, c, 1);
83  break;
84  }
85  }
86  return dst->str;
87 }
88 
89 static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
90 {
91  INIContext *ini = wctx->priv;
92  AVBPrint *buf = &wctx->section_pbuf[wctx->level];
93  const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
94  const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
95 
96  if (!section)
97  return;
98 
99  av_bprint_clear(buf);
100  if (!parent_section) {
101  writer_put_str(wctx, "# ffprobe output\n\n");
102  return;
103  }
104 
105  if (wctx->nb_item[wctx->level - 1])
106  writer_w8(wctx, '\n');
107 
108  av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
109  if (ini->hierarchical ||
111  av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
112 
113  if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
114  unsigned n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
115  ? wctx->nb_item_type[wctx->level - 1][section->id]
116  : wctx->nb_item[wctx->level - 1];
117  av_bprintf(buf, ".%u", n);
118  }
119  }
120 
122  writer_printf(wctx, "[%s]\n", buf->str);
123 }
124 
125 static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
126 {
127  AVBPrint buf;
128 
130  writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
131  av_bprint_clear(&buf);
132  writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
133  av_bprint_finalize(&buf, NULL);
134 }
135 
136 static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
137 {
138  writer_printf(wctx, "%s=%"PRId64"\n", key, value);
139 }
140 
142  .name = "ini",
143  .priv_size = sizeof(INIContext),
144  .print_section_header = ini_print_section_header,
145  .print_integer = ini_print_int,
146  .print_string = ini_print_str,
148  .priv_class = &ini_class,
149 };
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:125
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
opt.h
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:127
int64_t
long long int64_t
Definition: coverity.c:34
DefaultContext
Definition: tf_default.c:34
writer_printf
static void writer_printf(AVTextFormatContext *wctx, const char *fmt,...)
Definition: tf_internal.h:73
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
avtextformat.h
AVTextFormatContext
Definition: avtextformat.h:112
DefaultContext::nokey
int nokey
Definition: tf_default.c:36
AVTextFormatSection::id
int id
unique id identifying a section
Definition: avtextformat.h:42
AVTextFormatContext::level
int level
current level, starting from 0
Definition: avtextformat.h:123
INIContext
Definition: tf_ini.c:44
AVTextFormatSection::name
const char * name
Definition: avtextformat.h:43
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:50
DEFINE_FORMATTER_CLASS
DEFINE_FORMATTER_CLASS(ini)
ini_print_section_header
static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition: tf_ini.c:89
ini_options
static const AVOption ini_options[]
Definition: tf_ini.c:52
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:56
AVTextFormatter
Definition: avtextformat.h:94
AVTextFormatSection
Definition: avtextformat.h:41
limits.h
AVTextFormatContext::priv
void * priv
private data for use by the filter
Definition: avtextformat.h:118
OFFSET
#define OFFSET(x)
Definition: tf_ini.c:50
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
AVTextFormatContext::section
const AVTextFormatSection * section[SECTION_MAX_NB_LEVELS]
section per each level
Definition: avtextformat.h:130
writer_w8
static void writer_w8(AVTextFormatContext *wctx, int b)
Definition: tf_internal.h:63
tf_get_parent_section
static const AVTextFormatSection * tf_get_parent_section(AVTextFormatContext *tfc, int level)
Safely access the parent section.
Definition: tf_internal.h:55
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:109
ini_print_int
static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition: tf_ini.c:136
DefaultContext::noprint_wrappers
int noprint_wrappers
Definition: tf_default.c:37
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:87
AVTextFormatter::name
const char * name
Definition: avtextformat.h:97
AVTextFormatContext::section_pbuf
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]
generic print buffer dedicated to each section, used by various formatters
Definition: avtextformat.h:131
tf_internal.h
DefaultContext::nested_section
int nested_section[SECTION_MAX_NB_LEVELS]
Definition: tf_default.c:38
writer_put_str
static void writer_put_str(AVTextFormatContext *wctx, const char *str)
Definition: tf_internal.h:68
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:72
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:45
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:60
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:126
avtextformatter_ini
const AVTextFormatter avtextformatter_ini
Definition: tf_ini.c:141
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
tf_get_section
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
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:71
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:46
INIContext::hierarchical
int hierarchical
Definition: tf_ini.c:46
src
#define src
Definition: vp8dsp.c:248