FFmpeg
vf_format.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
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 /**
22  * @file
23  * format and noformat video filters
24  */
25 
26 #include "config_components.h"
27 
28 #include <string.h>
29 
30 #include "libavutil/internal.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/opt.h"
34 
35 #include "avfilter.h"
36 #include "filters.h"
37 #include "formats.h"
38 #include "video.h"
39 
40 typedef struct FormatContext {
41  const AVClass *class;
42  char *pix_fmts;
43  char *csps;
44  char *ranges;
45  char *alphamodes;
46 
47  AVFilterFormats *formats; ///< parsed from `pix_fmts`
48  AVFilterFormats *color_spaces; ///< parsed from `csps`
49  AVFilterFormats *color_ranges; ///< parsed from `ranges`
50  AVFilterFormats *alpha_modes; ///< parsed from `alphamodes`
52 
54 {
55  FormatContext *s = ctx->priv;
56  ff_formats_unref(&s->formats);
57  ff_formats_unref(&s->color_spaces);
58  ff_formats_unref(&s->color_ranges);
59  ff_formats_unref(&s->alpha_modes);
60 }
61 
63  AVFilterFormats *allfmts)
64 {
65  if (!allfmts)
66  return AVERROR(ENOMEM);
67  if (!*fmts) {
68  /* empty fmt list means no restriction, regardless of filter type */
69  ff_formats_unref(&allfmts);
70  return 0;
71  }
72 
73  for (int i = 0; i < allfmts->nb_formats; i++) {
74  for (int j = 0; j < (*fmts)->nb_formats; j++) {
75  if (allfmts->formats[i] == (*fmts)->formats[j]) {
76  /* format is forbidden, remove it from allfmts list */
77  memmove(&allfmts->formats[i], &allfmts->formats[i+1],
78  (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats));
79  allfmts->nb_formats--;
80  i--; /* repeat loop with same idx */
81  break;
82  }
83  }
84  }
85 
86  ff_formats_unref(fmts);
87  *fmts = allfmts;
88  return 0;
89 }
90 
91 #define DEFINE_PARSE(name, Type, get, descr, extra_test) \
92 static int parse_##name(enum Type *ret, const char *arg, void *log_ctx) \
93 { \
94  char *tail; \
95  int val = get(arg); \
96  if (val < 0) { \
97  val = strtol(arg, &tail, 0); \
98  if (*tail || extra_test) { \
99  av_log(log_ctx, AV_LOG_ERROR, "Invalid " descr " '%s'\n", arg); \
100  return AVERROR(EINVAL); \
101  } \
102  } \
103  *ret = val; \
104  return 0; \
105 }
106 
107 DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val))
108 DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color space", 0)
111 
113 {
114  FormatContext *s = ctx->priv;
115  enum AVPixelFormat pix_fmt;
116  enum AVColorSpace csp;
117  enum AVColorRange crg;
118  enum AVAlphaMode alm;
119  int ret;
120 
121  #define PARSE_LIST(strfield, fmtfield, tvar, parse) \
122  for (char *sep, *cur = s->strfield; cur; cur = sep) { \
123  sep = strchr(cur, '|'); \
124  if (sep && *sep) \
125  *sep++ = 0; \
126  if ((ret = parse(&tvar, cur, ctx)) < 0 || \
127  (ret = ff_add_format(&s->fmtfield, tvar)) < 0) \
128  return ret; \
129  }
130 
132  PARSE_LIST(csps, color_spaces, csp, parse_color_space)
133  PARSE_LIST(ranges, color_ranges, crg, parse_color_range)
134  PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode)
135 
136  if (!strcmp(ctx->filter->name, "noformat")) {
137  if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
138  (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
139  (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 ||
140  (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0)
141  return ret;
142  }
143 
144  /* hold on to a ref for the lifetime of the filter */
145  if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
146  s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
147  s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 ||
148  s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0)
149  return ret;
150 
151  return 0;
152 }
153 
155  AVFilterFormatsConfig **cfg_in,
156  AVFilterFormatsConfig **cfg_out)
157 {
158  FormatContext *s = ctx->priv;
159  int ret;
160 
161  if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
162  s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
163  s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 ||
164  s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0)
165  return ret;
166 
167  return 0;
168 }
169 
170 
171 #define OFFSET(x) offsetof(FormatContext, x)
172 static const AVOption options[] = {
173  { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
174  { "color_spaces", "A '|'-separated list of color spaces", OFFSET(csps), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
175  { "color_ranges", "A '|'-separated list of color ranges", OFFSET(ranges), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
176  { "alpha_modes", "A '|'-separated list of alpha modes", OFFSET(alphamodes), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
177  { NULL }
178 };
179 
181 
182 static const AVFilterPad inputs[] = {
183  {
184  .name = "default",
185  .type = AVMEDIA_TYPE_VIDEO,
186  .get_buffer.video = ff_null_get_video_buffer,
187  },
188 };
189 
190 #if CONFIG_FORMAT_FILTER
191 const FFFilter ff_vf_format = {
192  .p.name = "format",
193  .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
194  .p.priv_class = &format_class,
195 
196  .p.flags = AVFILTER_FLAG_METADATA_ONLY,
197 
198  .init = init,
199  .uninit = uninit,
200 
201  .priv_size = sizeof(FormatContext),
202 
205 
207 };
208 #endif /* CONFIG_FORMAT_FILTER */
209 
210 #if CONFIG_NOFORMAT_FILTER
211 const FFFilter ff_vf_noformat = {
212  .p.name = "noformat",
213  .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
214  .p.priv_class = &format_class,
215 
216  .p.flags = AVFILTER_FLAG_METADATA_ONLY,
217 
218  .init = init,
219  .uninit = uninit,
220 
221  .priv_size = sizeof(FormatContext),
222 
225 
227 };
228 #endif /* CONFIG_NOFORMAT_FILTER */
formats
formats
Definition: signature.h:47
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FormatContext::ranges
char * ranges
Definition: vf_format.c:44
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
color
Definition: vf_paletteuse.c:513
ff_set_common_color_ranges2
int ff_set_common_color_ranges2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_ranges)
Definition: formats.c:1102
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3460
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1150
av_cold
#define av_cold
Definition: attributes.h:119
FormatContext::color_ranges
AVFilterFormats * color_ranges
parsed from ranges
Definition: vf_format.c:49
inputs
static const AVFilterPad inputs[]
Definition: vf_format.c:182
av_color_space_from_name
int av_color_space_from_name(const char *name)
Definition: pixdesc.c:3866
FormatContext::alphamodes
char * alphamodes
Definition: vf_format.c:45
mode
Definition: swscale.c:71
pixdesc.h
av_color_range_from_name
int av_color_range_from_name(const char *name)
Definition: pixdesc.c:3782
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:53
AVOption
AVOption.
Definition: opt.h:428
filters.h
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:219
FormatContext::pix_fmts
char * pix_fmts
Definition: vf_format.c:42
video.h
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:241
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:602
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:40
ff_vf_format
const FFFilter ff_vf_format
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
ff_set_common_color_spaces2
int ff_set_common_color_spaces2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_spaces)
Definition: formats.c:1078
FFFilter
Definition: filters.h:267
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:265
FormatContext::formats
AVFilterFormats * formats
parsed from pix_fmts
Definition: vf_format.c:47
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:764
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
ff_set_common_alpha_modes2
int ff_set_common_alpha_modes2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *alpha_modes)
Definition: formats.c:1126
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
color_range
color_range
Definition: vf_selectivecolor.c:43
FormatContext
Definition: vf_format.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
AVFilterFormats::nb_formats
unsigned nb_formats
number of formats
Definition: formats.h:65
options
Definition: swscale.c:50
ff_all_color_spaces
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition: formats.c:697
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:120
AVAlphaMode
AVAlphaMode
Correlation between the alpha channel and color values.
Definition: pixfmt.h:816
AV_OPT_FLAG_FILTERING_PARAM
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition: opt.h:380
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:88
OFFSET
#define OFFSET(x)
Definition: vf_format.c:171
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
FormatContext::color_spaces
AVFilterFormats * color_spaces
parsed from csps
Definition: vf_format.c:48
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
invert_formats
static av_cold int invert_formats(AVFilterFormats **fmts, AVFilterFormats *allfmts)
Definition: vf_format.c:62
ff_all_color_ranges
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition: formats.c:713
ff_null_get_video_buffer
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:44
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options)
ff_vf_noformat
const FFFilter ff_vf_noformat
ff_formats_unref
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to,...
Definition: formats.c:803
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:112
internal.h
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:706
s
uint8_t s
Definition: llvidencdsp.c:39
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:357
parse_pixel_format
static int parse_pixel_format(AVCodecContext *avctx)
Definition: dds.c:112
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:46
ret
ret
Definition: filter_design.txt:187
av_alpha_mode_from_name
enum AVAlphaMode av_alpha_mode_from_name(const char *name)
Definition: pixdesc.c:3931
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_format.c:154
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:264
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:3392
FormatContext::alpha_modes
AVFilterFormats * alpha_modes
parsed from alphamodes
Definition: vf_format.c:50
PARSE_LIST
#define PARSE_LIST(strfield, fmtfield, tvar, parse)
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:182
ff_all_alpha_modes
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition: formats.c:732
FormatContext::csps
char * csps
Definition: vf_format.c:43
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:271
mem.h
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVFormatContext::name
char * name
Name of this format context, only used for logging purposes.
Definition: avformat.h:1963
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:275
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:748
DEFINE_PARSE
#define DEFINE_PARSE(name, Type, get, descr, extra_test)
Definition: vf_format.c:91