2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of FFmpeg.
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.
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.
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
23 * format and noformat video filters
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/opt.h"
38 typedef struct FormatContext {
43 * pix_fmts parsed into AVPixelFormats and terminated with
46 enum AVPixelFormat *formats;
49 static av_cold void uninit(AVFilterContext *ctx)
51 FormatContext *s = ctx->priv;
52 av_freep(&s->formats);
55 static av_cold int init(AVFilterContext *ctx)
57 FormatContext *s = ctx->priv;
64 return AVERROR(EINVAL);
66 /* count the formats */
68 while ((cur = strchr(cur, '|'))) {
74 s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
76 return AVERROR(ENOMEM);
78 /* parse the list of formats */
80 for (i = 0; i < nb_formats; i++) {
81 sep = strchr(cur, '|');
85 if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
90 s->formats[nb_formats] = AV_PIX_FMT_NONE;
92 if (!strcmp(ctx->filter->name, "noformat")) {
93 const AVPixFmtDescriptor *desc = NULL;
94 enum AVPixelFormat *formats_allowed;
95 int nb_formats_lavu = 0, nb_formats_allowed = 0;
97 /* count the formats known to lavu */
98 while ((desc = av_pix_fmt_desc_next(desc)))
101 formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
102 if (!formats_allowed)
103 return AVERROR(ENOMEM);
105 /* for each format known to lavu, check if it's in the list of
106 * forbidden formats */
107 while ((desc = av_pix_fmt_desc_next(desc))) {
108 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
110 for (i = 0; i < nb_formats; i++) {
111 if (s->formats[i] == pix_fmt)
117 formats_allowed[nb_formats_allowed++] = pix_fmt;
119 formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
120 av_freep(&s->formats);
121 s->formats = formats_allowed;
127 static int query_formats(AVFilterContext *ctx)
129 FormatContext *s = ctx->priv;
130 AVFilterFormats *formats = ff_make_format_list(s->formats);
133 return AVERROR(ENOMEM);
135 ff_set_common_formats(ctx, formats);
140 #define OFFSET(x) offsetof(FormatContext, x)
141 static const AVOption options[] = {
142 { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
146 #if CONFIG_FORMAT_FILTER
148 #define format_options options
149 AVFILTER_DEFINE_CLASS(format);
151 static const AVFilterPad avfilter_vf_format_inputs[] = {
154 .type = AVMEDIA_TYPE_VIDEO,
155 .get_video_buffer = ff_null_get_video_buffer,
160 static const AVFilterPad avfilter_vf_format_outputs[] = {
163 .type = AVMEDIA_TYPE_VIDEO
168 AVFilter ff_vf_format = {
170 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
175 .query_formats = query_formats,
177 .priv_size = sizeof(FormatContext),
178 .priv_class = &format_class,
180 .inputs = avfilter_vf_format_inputs,
181 .outputs = avfilter_vf_format_outputs,
183 #endif /* CONFIG_FORMAT_FILTER */
185 #if CONFIG_NOFORMAT_FILTER
187 #define noformat_options options
188 AVFILTER_DEFINE_CLASS(noformat);
190 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
193 .type = AVMEDIA_TYPE_VIDEO,
194 .get_video_buffer = ff_null_get_video_buffer,
199 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
202 .type = AVMEDIA_TYPE_VIDEO
207 AVFilter ff_vf_noformat = {
209 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
214 .query_formats = query_formats,
216 .priv_size = sizeof(FormatContext),
217 .priv_class = &noformat_class,
219 .inputs = avfilter_vf_noformat_inputs,
220 .outputs = avfilter_vf_noformat_outputs,
222 #endif /* CONFIG_NOFORMAT_FILTER */