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;
62 /* count the formats */
64 while ((cur = strchr(cur, '|'))) {
70 s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
72 return AVERROR(ENOMEM);
74 /* parse the list of formats */
76 for (i = 0; i < nb_formats; i++) {
77 sep = strchr(cur, '|');
81 s->formats[i] = av_get_pix_fmt(cur);
82 if (s->formats[i] == AV_PIX_FMT_NONE) {
83 av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
84 return AVERROR(EINVAL);
89 s->formats[nb_formats] = AV_PIX_FMT_NONE;
91 if (!strcmp(ctx->filter->name, "noformat")) {
92 const AVPixFmtDescriptor *desc = NULL;
93 enum AVPixelFormat *formats_allowed;
94 int nb_formats_lavu = 0, nb_formats_allowed = 0;;
96 /* count the formats known to lavu */
97 while ((desc = av_pix_fmt_desc_next(desc)))
100 formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
101 if (!formats_allowed)
102 return AVERROR(ENOMEM);
104 /* for each format known to lavu, check if it's in the list of
105 * forbidden formats */
106 while ((desc = av_pix_fmt_desc_next(desc))) {
107 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
109 for (i = 0; i < nb_formats; i++) {
110 if (s->formats[i] == pix_fmt)
116 formats_allowed[nb_formats_allowed++] = pix_fmt;
118 formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
119 av_freep(&s->formats);
120 s->formats = formats_allowed;
126 static int query_formats(AVFilterContext *ctx)
128 FormatContext *s = ctx->priv;
129 AVFilterFormats *formats = ff_make_format_list(s->formats);
132 return AVERROR(ENOMEM);
134 ff_set_common_formats(ctx, formats);
139 #define OFFSET(x) offsetof(FormatContext, x)
140 static const AVOption options[] = {
141 { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
145 #if CONFIG_FORMAT_FILTER
146 static const AVClass format_class = {
147 .class_name = "format",
148 .item_name = av_default_item_name,
150 .version = LIBAVUTIL_VERSION_INT,
153 static const AVFilterPad avfilter_vf_format_inputs[] = {
156 .type = AVMEDIA_TYPE_VIDEO,
157 .get_video_buffer = ff_null_get_video_buffer,
162 static const AVFilterPad avfilter_vf_format_outputs[] = {
165 .type = AVMEDIA_TYPE_VIDEO
170 AVFilter ff_vf_format = {
172 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
177 .query_formats = query_formats,
179 .priv_size = sizeof(FormatContext),
180 .priv_class = &format_class,
182 .inputs = avfilter_vf_format_inputs,
183 .outputs = avfilter_vf_format_outputs,
185 #endif /* CONFIG_FORMAT_FILTER */
187 #if CONFIG_NOFORMAT_FILTER
188 static const AVClass noformat_class = {
189 .class_name = "noformat",
190 .item_name = av_default_item_name,
192 .version = LIBAVUTIL_VERSION_INT,
195 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
198 .type = AVMEDIA_TYPE_VIDEO,
199 .get_video_buffer = ff_null_get_video_buffer,
204 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
207 .type = AVMEDIA_TYPE_VIDEO
212 AVFilter ff_vf_noformat = {
214 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
219 .query_formats = query_formats,
221 .priv_size = sizeof(FormatContext),
222 .priv_class = &noformat_class,
224 .inputs = avfilter_vf_noformat_inputs,
225 .outputs = avfilter_vf_noformat_outputs,
227 #endif /* CONFIG_NOFORMAT_FILTER */