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 "formats.h"
37 #include "internal.h"
38 #include "video.h"
39
40 typedef struct FormatContext {
41 const AVClass *class;
42 char *pix_fmts;
43
44 /**
45 * pix_fmts parsed into AVPixelFormats and terminated with
46 * AV_PIX_FMT_NONE
47 */
48 enum AVPixelFormat *formats;
49 } FormatContext;
50
uninit(AVFilterContext * ctx)51 static av_cold void uninit(AVFilterContext *ctx)
52 {
53 FormatContext *s = ctx->priv;
54 av_freep(&s->formats);
55 }
56
init(AVFilterContext * ctx)57 static av_cold int init(AVFilterContext *ctx)
58 {
59 FormatContext *s = ctx->priv;
60 char *cur, *sep;
61 int nb_formats = 1;
62 int i;
63 int ret;
64
65 if (!s->pix_fmts) {
66 av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
67 return AVERROR(EINVAL);
68 }
69
70 /* count the formats */
71 cur = s->pix_fmts;
72 while ((cur = strchr(cur, '|'))) {
73 nb_formats++;
74 if (*cur)
75 cur++;
76 }
77
78 s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
79 if (!s->formats)
80 return AVERROR(ENOMEM);
81
82 /* parse the list of formats */
83 cur = s->pix_fmts;
84 for (i = 0; i < nb_formats; i++) {
85 sep = strchr(cur, '|');
86 if (sep)
87 *sep++ = 0;
88
89 if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
90 return ret;
91
92 cur = sep;
93 }
94 s->formats[nb_formats] = AV_PIX_FMT_NONE;
95
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;
100
101 /* count the formats known to lavu */
102 while ((desc = av_pix_fmt_desc_next(desc)))
103 nb_formats_lavu++;
104
105 formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
106 if (!formats_allowed)
107 return AVERROR(ENOMEM);
108
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);
113
114 for (i = 0; i < nb_formats; i++) {
115 if (s->formats[i] == pix_fmt)
116 break;
117 }
118 if (i < nb_formats)
119 continue;
120
121 formats_allowed[nb_formats_allowed++] = pix_fmt;
122 }
123 formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
124 av_freep(&s->formats);
125 s->formats = formats_allowed;
126 }
127
128 return 0;
129 }
130
query_formats(AVFilterContext * ctx)131 static int query_formats(AVFilterContext *ctx)
132 {
133 FormatContext *s = ctx->priv;
134
135 return ff_set_common_formats_from_list(ctx, s->formats);
136 }
137
138
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 | AV_OPT_FLAG_FILTERING_PARAM },
142 { NULL }
143 };
144
145 AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options);
146
147 #if CONFIG_FORMAT_FILTER
148
149 static const AVFilterPad avfilter_vf_format_inputs[] = {
150 {
151 .name = "default",
152 .type = AVMEDIA_TYPE_VIDEO,
153 .get_buffer.video = ff_null_get_video_buffer,
154 },
155 };
156
157 static const AVFilterPad avfilter_vf_format_outputs[] = {
158 {
159 .name = "default",
160 .type = AVMEDIA_TYPE_VIDEO
161 },
162 };
163
164 const AVFilter ff_vf_format = {
165 .name = "format",
166 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
167
168 .init = init,
169 .uninit = uninit,
170
171 .priv_size = sizeof(FormatContext),
172 .priv_class = &format_class,
173
174 .flags = AVFILTER_FLAG_METADATA_ONLY,
175
176 FILTER_INPUTS(avfilter_vf_format_inputs),
177 FILTER_OUTPUTS(avfilter_vf_format_outputs),
178
179 FILTER_QUERY_FUNC(query_formats),
180 };
181 #endif /* CONFIG_FORMAT_FILTER */
182
183 #if CONFIG_NOFORMAT_FILTER
184
185 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
186 {
187 .name = "default",
188 .type = AVMEDIA_TYPE_VIDEO,
189 .get_buffer.video = ff_null_get_video_buffer,
190 },
191 };
192
193 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
194 {
195 .name = "default",
196 .type = AVMEDIA_TYPE_VIDEO
197 },
198 };
199
200 const AVFilter ff_vf_noformat = {
201 .name = "noformat",
202 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
203 .priv_class = &format_class,
204
205 .init = init,
206 .uninit = uninit,
207
208 .priv_size = sizeof(FormatContext),
209
210 .flags = AVFILTER_FLAG_METADATA_ONLY,
211
212 FILTER_INPUTS(avfilter_vf_noformat_inputs),
213 FILTER_OUTPUTS(avfilter_vf_noformat_outputs),
214
215 FILTER_QUERY_FUNC(query_formats),
216 };
217 #endif /* CONFIG_NOFORMAT_FILTER */
218