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"
42 * List of flags telling if a given image format has been listed
43 * as argument to the filter.
45 int listed_pix_fmt_flags[AV_PIX_FMT_NB];
48 #define AV_PIX_FMT_NAME_MAXSIZE 32
50 static av_cold int init(AVFilterContext *ctx)
52 FormatContext *format = ctx->priv;
53 const char *cur, *sep;
54 char pix_fmt_name[AV_PIX_FMT_NAME_MAXSIZE];
56 enum AVPixelFormat pix_fmt;
58 /* parse the list of formats */
59 for (cur = format->pix_fmts; cur; cur = sep ? sep + 1 : NULL) {
60 if (!(sep = strchr(cur, '|')))
61 pix_fmt_name_len = strlen(cur);
63 pix_fmt_name_len = sep - cur;
64 if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
65 av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
69 memcpy(pix_fmt_name, cur, pix_fmt_name_len);
70 pix_fmt_name[pix_fmt_name_len] = 0;
71 pix_fmt = av_get_pix_fmt(pix_fmt_name);
73 if (pix_fmt == AV_PIX_FMT_NONE) {
74 av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
78 format->listed_pix_fmt_flags[pix_fmt] = 1;
84 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
86 AVFilterFormats *formats;
87 enum AVPixelFormat pix_fmt;
89 formats = av_mallocz(sizeof(AVFilterFormats));
90 formats->formats = av_malloc(sizeof(enum AVPixelFormat) * AV_PIX_FMT_NB);
92 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
93 if (format->listed_pix_fmt_flags[pix_fmt] == flag)
94 formats->formats[formats->format_count++] = pix_fmt;
99 #define OFFSET(x) offsetof(FormatContext, x)
100 static const AVOption options[] = {
101 { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
105 #if CONFIG_FORMAT_FILTER
106 static int query_formats_format(AVFilterContext *ctx)
108 ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
112 static const AVClass format_class = {
113 .class_name = "format",
114 .item_name = av_default_item_name,
116 .version = LIBAVUTIL_VERSION_INT,
119 static const AVFilterPad avfilter_vf_format_inputs[] = {
122 .type = AVMEDIA_TYPE_VIDEO,
123 .get_video_buffer = ff_null_get_video_buffer,
128 static const AVFilterPad avfilter_vf_format_outputs[] = {
131 .type = AVMEDIA_TYPE_VIDEO
136 AVFilter avfilter_vf_format = {
138 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
142 .query_formats = query_formats_format,
144 .priv_size = sizeof(FormatContext),
145 .priv_class = &format_class,
147 .inputs = avfilter_vf_format_inputs,
148 .outputs = avfilter_vf_format_outputs,
150 #endif /* CONFIG_FORMAT_FILTER */
152 #if CONFIG_NOFORMAT_FILTER
153 static int query_formats_noformat(AVFilterContext *ctx)
155 ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
159 static const AVClass noformat_class = {
160 .class_name = "noformat",
161 .item_name = av_default_item_name,
163 .version = LIBAVUTIL_VERSION_INT,
166 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
169 .type = AVMEDIA_TYPE_VIDEO,
170 .get_video_buffer = ff_null_get_video_buffer,
175 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
178 .type = AVMEDIA_TYPE_VIDEO
183 AVFilter avfilter_vf_noformat = {
185 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
189 .query_formats = query_formats_noformat,
191 .priv_size = sizeof(FormatContext),
192 .priv_class = &noformat_class,
194 .inputs = avfilter_vf_noformat_inputs,
195 .outputs = avfilter_vf_noformat_outputs,
197 #endif /* CONFIG_NOFORMAT_FILTER */