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;
47
48 AVFilterFormats *formats; ///< parsed from `pix_fmts`
49 AVFilterFormats *color_spaces; ///< parsed from `csps`
50 AVFilterFormats *color_ranges; ///< parsed from `ranges`
51 AVFilterFormats *alpha_modes; ///< parsed from `alphamodes`
52 AVFilterFormats *chroma_locations; ///< parsed from `chromalocations`
54
56{
57 FormatContext *s = ctx->priv;
58 ff_formats_unref(&s->formats);
59 ff_formats_unref(&s->color_spaces);
60 ff_formats_unref(&s->color_ranges);
61 ff_formats_unref(&s->alpha_modes);
62 ff_formats_unref(&s->chroma_locations);
63}
64
66 AVFilterFormats *allfmts)
67{
68 if (!allfmts)
69 return AVERROR(ENOMEM);
70 if (!*fmts) {
71 /* empty fmt list means no restriction, regardless of filter type */
72 ff_formats_unref(&allfmts);
73 return 0;
74 }
75
76 for (int i = 0; i < allfmts->nb_formats; i++) {
77 for (int j = 0; j < (*fmts)->nb_formats; j++) {
78 if (allfmts->formats[i] == (*fmts)->formats[j]) {
79 /* format is forbidden, remove it from allfmts list */
80 memmove(&allfmts->formats[i], &allfmts->formats[i+1],
81 (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats));
82 allfmts->nb_formats--;
83 i--; /* repeat loop with same idx */
84 break;
85 }
86 }
87 }
88
89 ff_formats_unref(fmts);
90 *fmts = allfmts;
91 return 0;
92}
93
94#define DEFINE_PARSE(name, Type, get, descr, extra_test) \
95static int parse_##name(enum Type *ret, const char *arg, void *log_ctx) \
96{ \
97 char *tail; \
98 int val = get(arg); \
99 if (val < 0) { \
100 val = strtol(arg, &tail, 0); \
101 if (*tail || extra_test) { \
102 av_log(log_ctx, AV_LOG_ERROR, "Invalid " descr " '%s'\n", arg); \
103 return AVERROR(EINVAL); \
104 } \
105 } \
106 *ret = val; \
107 return 0; \
108}
109
110DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val))
111DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color space", 0)
113DEFINE_PARSE(alpha_mode, AVAlphaMode, av_alpha_mode_from_name, "alpha mode", 0)
114DEFINE_PARSE(chroma_location, AVChromaLocation, av_chroma_location_from_name, "chroma location", 0)
115
117{
118 FormatContext *s = ctx->priv;
120 enum AVColorSpace csp;
121 enum AVColorRange crg;
122 enum AVAlphaMode alm;
123 enum AVChromaLocation cloc;
124 int ret;
125
126 #define PARSE_LIST(strfield, fmtfield, tvar, parse) \
127 for (char *sep, *cur = s->strfield; cur; cur = sep) { \
128 sep = strchr(cur, '|'); \
129 if (sep && *sep) \
130 *sep++ = 0; \
131 if ((ret = parse(&tvar, cur, ctx)) < 0 || \
132 (ret = ff_add_format(&s->fmtfield, tvar)) < 0) \
133 return ret; \
134 }
135
137 PARSE_LIST(csps, color_spaces, csp, parse_color_space)
138 PARSE_LIST(ranges, color_ranges, crg, parse_color_range)
139 PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode)
140 PARSE_LIST(chromalocations, chroma_locations, cloc, parse_chroma_location)
141
142 if (!strcmp(ctx->filter->name, "noformat")) {
143 if (s->chroma_locations &&
144 (ret = ff_add_format(&s->chroma_locations, AVCHROMA_LOC_UNSPECIFIED)) < 0)
145 return ret;
146 if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
147 (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
148 (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 ||
149 (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0 ||
150 (ret = invert_formats(&s->chroma_locations, ff_all_chroma_locations())) < 0)
151 return ret;
152 }
153
154 /* hold on to a ref for the lifetime of the filter */
155 if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
156 s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
157 s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 ||
158 s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0 ||
159 s->chroma_locations && (ret = ff_formats_ref(s->chroma_locations, &s->chroma_locations)) < 0)
160 return ret;
161
162 return 0;
163}
164
166 AVFilterFormatsConfig **cfg_in,
167 AVFilterFormatsConfig **cfg_out)
168{
169 FormatContext *s = ctx->priv;
170 int ret;
171
172 if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
173 s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
174 s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 ||
175 s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0 ||
176 s->chroma_locations && (ret = ff_set_common_chroma_locations2(ctx, cfg_in, cfg_out, s->chroma_locations)) < 0)
177 return ret;
178
179 return 0;
180}
181
182
183#define OFFSET(x) offsetof(FormatContext, x)
184static 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 { "chroma_locations", "A '|'-separated list of chroma sample locations", OFFSET(chromalocations), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
190 { NULL }
191};
192
194
195static const AVFilterPad inputs[] = {
196 {
197 .name = "default",
198 .type = AVMEDIA_TYPE_VIDEO,
199 .get_buffer.video = ff_null_get_video_buffer,
200 },
201};
202
203#if CONFIG_FORMAT_FILTER
204const FFFilter ff_vf_format = {
205 .p.name = "format",
206 .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
207 .p.priv_class = &format_class,
208
210
211 .init = init,
212 .uninit = uninit,
213
214 .priv_size = sizeof(FormatContext),
215
218
220};
221#endif /* CONFIG_FORMAT_FILTER */
222
223#if CONFIG_NOFORMAT_FILTER
224const FFFilter ff_vf_noformat = {
225 .p.name = "noformat",
226 .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
227 .p.priv_class = &format_class,
228
230
231 .init = init,
232 .uninit = uninit,
233
234 .priv_size = sizeof(FormatContext),
235
238
240};
241#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:445
const FFFilter ff_vf_noformat
const FFFilter ff_vf_format
static AVFormatContext * ctx
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:611
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:776
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition formats.c:706
int ff_set_common_color_ranges2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_ranges)
Definition formats.c:1127
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1200
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:580
int ff_set_common_color_spaces2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_spaces)
Definition formats.c:1103
int ff_set_common_chroma_locations2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *chroma_locations)
Definition formats.c:1175
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:815
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition formats.c:722
int ff_set_common_alpha_modes2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *alpha_modes)
Definition formats.c:1151
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition formats.c:733
AVFilterFormats * ff_all_chroma_locations(void)
Construct an AVFilterFormats representing all possible chroma locations.
Definition formats.c:744
#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:188
#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:97
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:3890
int av_chroma_location_from_name(const char *name)
Definition pixdesc.c:3911
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3416
int av_color_range_from_name(const char *name)
Definition pixdesc.c:3806
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
enum AVAlphaMode av_alpha_mode_from_name(const char *name)
Definition pixdesc.c:3955
AVChromaLocation
Location of chroma samples.
Definition pixfmt.h:807
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:808
AVColorRange
Visual content value range.
Definition pixfmt.h:753
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:821
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
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:279
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:51
char * chromalocations
Definition vf_format.c:46
char * ranges
Definition vf_format.c:44
AVFilterFormats * color_ranges
parsed from ranges
Definition vf_format.c:50
AVFilterFormats * color_spaces
parsed from csps
Definition vf_format.c:49
char * alphamodes
Definition vf_format.c:45
AVFilterFormats * chroma_locations
parsed from chromalocations
Definition vf_format.c:52
char * csps
Definition vf_format.c:43
AVFilterFormats * formats
parsed from pix_fmts
Definition vf_format.c:48
#define PARSE_LIST(strfield, fmtfield, tvar, parse)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_format.c:165
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_format.c:55
#define OFFSET(x)
Definition vf_format.c:183
#define DEFINE_PARSE(name, Type, get, descr, extra_test)
Definition vf_format.c:94
static av_cold int invert_formats(AVFilterFormats **fmts, AVFilterFormats *allfmts)
Definition vf_format.c:65
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