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 static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
92 {
93  char *tail;
94  int pix_fmt = av_get_pix_fmt(arg);
95  if (pix_fmt == AV_PIX_FMT_NONE) {
96  pix_fmt = strtol(arg, &tail, 0);
97  if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
98  av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
99  return AVERROR(EINVAL);
100  }
101  }
102  *ret = pix_fmt;
103  return 0;
104 }
105 
107 {
108  FormatContext *s = ctx->priv;
109  enum AVPixelFormat pix_fmt;
110  int ret;
111 
112  for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
113  sep = strchr(cur, '|');
114  if (sep && *sep)
115  *sep++ = 0;
116  if ((ret = parse_pixel_format(&pix_fmt, cur, ctx)) < 0 ||
117  (ret = ff_add_format(&s->formats, pix_fmt)) < 0)
118  return ret;
119  }
120 
121  for (char *sep, *cur = s->csps; cur; cur = sep) {
122  sep = strchr(cur, '|');
123  if (sep && *sep)
124  *sep++ = 0;
125  if ((ret = av_color_space_from_name(cur)) < 0 ||
126  (ret = ff_add_format(&s->color_spaces, ret)) < 0)
127  return ret;
128  }
129 
130  for (char *sep, *cur = s->ranges; cur; cur = sep) {
131  sep = strchr(cur, '|');
132  if (sep && *sep)
133  *sep++ = 0;
134  if ((ret = av_color_range_from_name(cur)) < 0 ||
135  (ret = ff_add_format(&s->color_ranges, ret)) < 0)
136  return ret;
137  }
138 
139  for (char *sep, *cur = s->alphamodes; cur; cur = sep) {
140  sep = strchr(cur, '|');
141  if (sep && *sep)
142  *sep++ = 0;
143  if ((ret = av_alpha_mode_from_name(cur)) < 0 ||
144  (ret = ff_add_format(&s->alpha_modes, ret)) < 0)
145  return ret;
146  }
147 
148  if (!strcmp(ctx->filter->name, "noformat")) {
149  if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
150  (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
151  (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 ||
152  (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0)
153  return ret;
154  }
155 
156  /* hold on to a ref for the lifetime of the filter */
157  if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
158  s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
159  s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 ||
160  s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0)
161  return ret;
162 
163  return 0;
164 }
165 
167  AVFilterFormatsConfig **cfg_in,
168  AVFilterFormatsConfig **cfg_out)
169 {
170  FormatContext *s = ctx->priv;
171  int ret;
172 
173  if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
174  s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
175  s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 ||
176  s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0)
177  return ret;
178 
179  return 0;
180 }
181 
182 
183 #define OFFSET(x) offsetof(FormatContext, x)
184 static const AVOption options[] = {
185  { "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 },
186  { "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 },
187  { "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 },
188  { "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 },
189  { NULL }
190 };
191 
193 
194 static const AVFilterPad inputs[] = {
195  {
196  .name = "default",
197  .type = AVMEDIA_TYPE_VIDEO,
198  .get_buffer.video = ff_null_get_video_buffer,
199  },
200 };
201 
202 #if CONFIG_FORMAT_FILTER
203 const FFFilter ff_vf_format = {
204  .p.name = "format",
205  .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
206  .p.priv_class = &format_class,
207 
208  .p.flags = AVFILTER_FLAG_METADATA_ONLY,
209 
210  .init = init,
211  .uninit = uninit,
212 
213  .priv_size = sizeof(FormatContext),
214 
217 
219 };
220 #endif /* CONFIG_FORMAT_FILTER */
221 
222 #if CONFIG_NOFORMAT_FILTER
223 const FFFilter ff_vf_noformat = {
224  .p.name = "noformat",
225  .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
226  .p.priv_class = &format_class,
227 
228  .p.flags = AVFILTER_FLAG_METADATA_ONLY,
229 
230  .init = init,
231  .uninit = uninit,
232 
233  .priv_size = sizeof(FormatContext),
234 
237 
239 };
240 #endif /* CONFIG_NOFORMAT_FILTER */
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
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:1028
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3447
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1076
FormatContext::color_ranges
AVFilterFormats * color_ranges
parsed from ranges
Definition: vf_format.c:49
inputs
static const AVFilterPad inputs[]
Definition: vf_format.c:194
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:263
av_color_space_from_name
int av_color_space_from_name(const char *name)
Definition: pixdesc.c:3829
FormatContext::alphamodes
char * alphamodes
Definition: vf_format.c:45
pixdesc.h
av_color_range_from_name
int av_color_range_from_name(const char *name)
Definition: pixdesc.c:3769
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:53
AVOption
AVOption.
Definition: opt.h:429
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
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
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:551
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:39
ff_vf_format
const FFFilter ff_vf_format
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:90
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:1004
FFFilter
Definition: filters.h:266
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:705
filters.h
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:298
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:1052
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:264
arg
const char * arg
Definition: jacosubdec.c:67
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
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:520
AVFilterFormats::nb_formats
unsigned nb_formats
number of formats
Definition: formats.h:65
options
Definition: swscale.c:43
ff_all_color_spaces
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition: formats.c:646
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:121
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:381
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:94
OFFSET
#define OFFSET(x)
Definition: vf_format.c:183
FormatContext::color_spaces
AVFilterFormats * color_spaces
parsed from csps
Definition: vf_format.c:48
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:662
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:744
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:106
internal.h
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:240
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:358
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:45
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:3894
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_format.c:166
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:3379
parse_pixel_format
static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Definition: vf_format.c:91
FormatContext::alpha_modes
AVFilterFormats * alpha_modes
parsed from alphamodes
Definition: vf_format.c:50
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
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:183
ff_all_alpha_modes
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition: formats.c:673
FormatContext::csps
char * csps
Definition: vf_format.c:43
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:270
mem.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:276