FFmpeg
Loading...
Searching...
No Matches
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
40typedef struct FormatContext {
41 const AVClass *class;
42 char *pix_fmts;
43 char *csps;
44 char *ranges;
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) \
92static 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
107DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val))
108DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color space", 0)
110DEFINE_PARSE(alpha_mode, AVAlphaMode, av_alpha_mode_from_name, "alpha mode", 0)
111
113{
114 FormatContext *s = ctx->priv;
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)
172static 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
182static 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
191const 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
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
211const 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
217
218 .init = init,
219 .uninit = uninit,
220
221 .priv_size = sizeof(FormatContext),
222
225
227};
228#endif /* CONFIG_NOFORMAT_FILTER */
static double val(void *priv, double ch)
Definition aeval.c:77
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const char *const format[]
Definition af_aiir.c:444
const FFFilter ff_vf_noformat
const FFFilter ff_vf_format
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static int parse_pixel_format(AVCodecContext *avctx)
Definition dds.c:112
static enum AVPixelFormat pix_fmt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
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
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition formats.c:697
int ff_set_common_color_ranges2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_ranges)
Definition formats.c:1089
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
int ff_set_common_color_spaces2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_spaces)
Definition formats.c:1065
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:795
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition formats.c:713
int ff_set_common_alpha_modes2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *alpha_modes)
Definition formats.c:1113
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition formats.c:724
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition opt.h:380
#define AV_OPT_FLAG_VIDEO_PARAM
Definition opt.h:357
@ 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
#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
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
Memory handling functions.
AVOptions.
int av_color_space_from_name(const char *name)
Definition pixdesc.c:3866
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3392
int av_color_range_from_name(const char *name)
Definition pixdesc.c:3782
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
enum AVAlphaMode av_alpha_mode_from_name(const char *name)
Definition pixdesc.c:3931
AVColorRange
Visual content value range.
Definition pixfmt.h:748
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:816
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
int * formats
list of media formats
Definition formats.h:66
unsigned nb_formats
number of formats
Definition formats.h:65
A filter pad used for either input or output.
Definition filters.h:40
AVOption.
Definition opt.h:428
char * pix_fmts
Definition vf_format.c:42
AVFilterFormats * alpha_modes
parsed from alphamodes
Definition vf_format.c:50
char * ranges
Definition vf_format.c:44
AVFilterFormats * color_ranges
parsed from ranges
Definition vf_format.c:49
AVFilterFormats * color_spaces
parsed from csps
Definition vf_format.c:48
char * alphamodes
Definition vf_format.c:45
char * csps
Definition vf_format.c:43
AVFilterFormats * formats
parsed from pix_fmts
Definition vf_format.c:47
static AVFormatContext * ctx
Definition movenc.c:49
#define PARSE_LIST(strfield, fmtfield, tvar, parse)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_format.c:154
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_format.c:53
#define OFFSET(x)
Definition vf_format.c:171
#define DEFINE_PARSE(name, Type, get, descr, extra_test)
Definition vf_format.c:91
static av_cold int invert_formats(AVFilterFormats **fmts, AVFilterFormats *allfmts)
Definition vf_format.c:62
color_range
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:44
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