2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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;
63 av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
64 return AVERROR(EINVAL);
67 /* count the formats */
69 while ((cur = strchr(cur, '|'))) {
75 s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
77 return AVERROR(ENOMEM);
79 /* parse the list of formats */
81 for (i = 0; i < nb_formats; i++) {
82 sep = strchr(cur, '|');
86 s->formats[i] = av_get_pix_fmt(cur);
87 if (s->formats[i] == AV_PIX_FMT_NONE) {
88 av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
89 return AVERROR(EINVAL);
94 s->formats[nb_formats] = AV_PIX_FMT_NONE;
96 if (!strcmp(ctx->filter->name, "noformat")) {
97 const AVPixFmtDescriptor *desc = NULL;
98 enum AVPixelFormat *formats_allowed;
99 int nb_formats_lavu = 0, nb_formats_allowed = 0;;
101 /* count the formats known to lavu */
102 while ((desc = av_pix_fmt_desc_next(desc)))
105 formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
106 if (!formats_allowed)
107 return AVERROR(ENOMEM);
109 /* for each format known to lavu, check if it's in the list of
110 * forbidden formats */
111 while ((desc = av_pix_fmt_desc_next(desc))) {
112 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
114 for (i = 0; i < nb_formats; i++) {
115 if (s->formats[i] == pix_fmt)
121 formats_allowed[nb_formats_allowed++] = pix_fmt;
123 formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
124 av_freep(&s->formats);
125 s->formats = formats_allowed;
131 static int query_formats(AVFilterContext *ctx)
133 FormatContext *s = ctx->priv;
134 AVFilterFormats *formats = ff_make_format_list(s->formats);
137 return AVERROR(ENOMEM);
139 ff_set_common_formats(ctx, formats);
144 #define OFFSET(x) offsetof(FormatContext, x)
145 static const AVOption options[] = {
146 { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
150 #if CONFIG_FORMAT_FILTER
151 static const AVClass format_class = {
152 .class_name = "format",
153 .item_name = av_default_item_name,
155 .version = LIBAVUTIL_VERSION_INT,
158 static const AVFilterPad avfilter_vf_format_inputs[] = {
161 .type = AVMEDIA_TYPE_VIDEO,
162 .get_video_buffer = ff_null_get_video_buffer,
167 static const AVFilterPad avfilter_vf_format_outputs[] = {
170 .type = AVMEDIA_TYPE_VIDEO
175 AVFilter ff_vf_format = {
177 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
182 .query_formats = query_formats,
184 .priv_size = sizeof(FormatContext),
185 .priv_class = &format_class,
187 .inputs = avfilter_vf_format_inputs,
188 .outputs = avfilter_vf_format_outputs,
190 #endif /* CONFIG_FORMAT_FILTER */
192 #if CONFIG_NOFORMAT_FILTER
193 static const AVClass noformat_class = {
194 .class_name = "noformat",
195 .item_name = av_default_item_name,
197 .version = LIBAVUTIL_VERSION_INT,
200 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
203 .type = AVMEDIA_TYPE_VIDEO,
204 .get_video_buffer = ff_null_get_video_buffer,
209 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
212 .type = AVMEDIA_TYPE_VIDEO
217 AVFilter ff_vf_noformat = {
219 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
224 .query_formats = query_formats,
226 .priv_size = sizeof(FormatContext),
227 .priv_class = &noformat_class,
229 .inputs = avfilter_vf_noformat_inputs,
230 .outputs = avfilter_vf_noformat_outputs,
232 #endif /* CONFIG_NOFORMAT_FILTER */