FFmpeg
tf_default.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;
56 
57 #undef OFFSET
58 #define OFFSET(x) offsetof(DefaultContext, x)
59 
60 static const AVOption default_options[] = {
61  { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
62  { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
63  { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
64  { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 },
65  {NULL},
66 };
67 
68 DEFINE_FORMATTER_CLASS(default);
69 
70 /* lame uppercasing routine, assumes the string is lower case ASCII */
71 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
72 {
73  int i;
74  for (i = 0; src[i] && i < dst_size-1; i++)
75  dst[i] = av_toupper(src[i]);
76  dst[i] = 0;
77  return dst;
78 }
79 
80 static void default_print_section_header(AVTextFormatContext *wctx, const void *data)
81 {
82  DefaultContext *def = wctx->priv;
83  char buf[32];
84  const struct AVTextFormatSection *section = wctx->section[wctx->level];
85  const struct AVTextFormatSection *parent_section = wctx->level ?
86  wctx->section[wctx->level-1] : NULL;
87 
88  av_bprint_clear(&wctx->section_pbuf[wctx->level]);
89  if (parent_section &&
91  def->nested_section[wctx->level] = 1;
92  av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
93  wctx->section_pbuf[wctx->level-1].str,
94  upcase_string(buf, sizeof(buf),
95  av_x_if_null(section->element_name, section->name)));
96  }
97 
98  if (def->noprint_wrappers || def->nested_section[wctx->level])
99  return;
100 
102  writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
103 }
104 
106 {
107  DefaultContext *def = wctx->priv;
108  const struct AVTextFormatSection *section = wctx->section[wctx->level];
109  char buf[32];
110 
111  if (def->noprint_wrappers || def->nested_section[wctx->level])
112  return;
113 
115  writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
116 }
117 
118 static void default_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
119 {
120  DefaultContext *def = wctx->priv;
121 
122  if (!def->nokey)
123  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
124  writer_printf(wctx, "%s\n", value);
125 }
126 
127 static void default_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
128 {
129  DefaultContext *def = wctx->priv;
130 
131  if (!def->nokey)
132  writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
133  writer_printf(wctx, "%"PRId64"\n", value);
134 }
135 
137  .name = "default",
138  .priv_size = sizeof(DefaultContext),
139  .print_section_header = default_print_section_header,
140  .print_section_footer = default_print_section_footer,
141  .print_integer = default_print_int,
142  .print_string = default_print_str,
144  .priv_class = &default_class,
145 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
opt.h
AVTextFormatContext::section
const struct AVTextFormatSection * section[SECTION_MAX_NB_LEVELS]
section per each level
Definition: avtextformat.h:106
int64_t
long long int64_t
Definition: coverity.c:34
DefaultContext
Definition: tf_default.c:50
default_print_str
static void default_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
Definition: tf_default.c:118
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
AVTextFormatContext::level
int level
current level, starting from 0
Definition: avtextformat.h:99
AVTextFormatSection::name
const char * name
Definition: avtextformat.h:39
upcase_string
static char * upcase_string(char *dst, size_t dst_size, const char *src)
Definition: tf_default.c:71
AVTextFormatSection::flags
int flags
Definition: avtextformat.h:48
avassert.h
AVTextFormatSection::element_name
const char * element_name
name of the contained element, if provided
Definition: avtextformat.h:50
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
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
OFFSET
#define OFFSET(x)
Definition: tf_default.c:58
SECTION_MAX_NB_LEVELS
#define SECTION_MAX_NB_LEVELS
Definition: avtextformat.h:85
DefaultContext::noprint_wrappers
int noprint_wrappers
Definition: tf_default.c:53
default_print_section_footer
static void default_print_section_footer(AVTextFormatContext *wctx)
Definition: tf_default.c:105
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
avtextformatter_default
const AVTextFormatter avtextformatter_default
Definition: tf_default.c:136
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
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
default_print_int
static void default_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition: tf_default.c:127
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_toupper
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:227
default_print_section_header
static void default_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition: tf_default.c:80
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
mem.h
DEFINE_FORMATTER_CLASS
#define DEFINE_FORMATTER_CLASS(name)
Definition: tf_default.c:37
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
default_options
static const AVOption default_options[]
Definition: tf_default.c:60
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
src
#define src
Definition: vp8dsp.c:248
writer_printf
#define writer_printf(wctx_, fmt_,...)
Definition: tf_default.c:35
av_x_if_null
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:312