Commit | Line | Data |
---|---|---|
2ba45a60 DM |
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 <string.h> | |
27 | ||
28 | #include "libavutil/internal.h" | |
29 | #include "libavutil/mem.h" | |
30 | #include "libavutil/pixdesc.h" | |
31 | #include "libavutil/opt.h" | |
32 | ||
33 | #include "avfilter.h" | |
34 | #include "formats.h" | |
35 | #include "internal.h" | |
36 | #include "video.h" | |
37 | ||
38 | typedef struct FormatContext { | |
39 | const AVClass *class; | |
40 | char *pix_fmts; | |
41 | ||
42 | /** | |
43 | * pix_fmts parsed into AVPixelFormats and terminated with | |
44 | * AV_PIX_FMT_NONE | |
45 | */ | |
46 | enum AVPixelFormat *formats; | |
47 | } FormatContext; | |
48 | ||
49 | static av_cold void uninit(AVFilterContext *ctx) | |
50 | { | |
51 | FormatContext *s = ctx->priv; | |
52 | av_freep(&s->formats); | |
53 | } | |
54 | ||
55 | static av_cold int init(AVFilterContext *ctx) | |
56 | { | |
57 | FormatContext *s = ctx->priv; | |
58 | char *cur, *sep; | |
59 | int nb_formats = 1; | |
60 | int i; | |
61 | int ret; | |
62 | ||
63 | if (!s->pix_fmts) | |
64 | return AVERROR(EINVAL); | |
65 | ||
66 | /* count the formats */ | |
67 | cur = s->pix_fmts; | |
68 | while ((cur = strchr(cur, '|'))) { | |
69 | nb_formats++; | |
70 | if (*cur) | |
71 | cur++; | |
72 | } | |
73 | ||
74 | s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats)); | |
75 | if (!s->formats) | |
76 | return AVERROR(ENOMEM); | |
77 | ||
78 | /* parse the list of formats */ | |
79 | cur = s->pix_fmts; | |
80 | for (i = 0; i < nb_formats; i++) { | |
81 | sep = strchr(cur, '|'); | |
82 | if (sep) | |
83 | *sep++ = 0; | |
84 | ||
85 | if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0) | |
86 | return ret; | |
87 | ||
88 | cur = sep; | |
89 | } | |
90 | s->formats[nb_formats] = AV_PIX_FMT_NONE; | |
91 | ||
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; | |
96 | ||
97 | /* count the formats known to lavu */ | |
98 | while ((desc = av_pix_fmt_desc_next(desc))) | |
99 | nb_formats_lavu++; | |
100 | ||
101 | formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed)); | |
102 | if (!formats_allowed) | |
103 | return AVERROR(ENOMEM); | |
104 | ||
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); | |
109 | ||
110 | for (i = 0; i < nb_formats; i++) { | |
111 | if (s->formats[i] == pix_fmt) | |
112 | break; | |
113 | } | |
114 | if (i < nb_formats) | |
115 | continue; | |
116 | ||
117 | formats_allowed[nb_formats_allowed++] = pix_fmt; | |
118 | } | |
119 | formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE; | |
120 | av_freep(&s->formats); | |
121 | s->formats = formats_allowed; | |
122 | } | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | static int query_formats(AVFilterContext *ctx) | |
128 | { | |
129 | FormatContext *s = ctx->priv; | |
130 | AVFilterFormats *formats = ff_make_format_list(s->formats); | |
131 | ||
132 | if (!formats) | |
133 | return AVERROR(ENOMEM); | |
134 | ||
135 | ff_set_common_formats(ctx, formats); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | ||
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 }, | |
143 | { NULL } | |
144 | }; | |
145 | ||
146 | #if CONFIG_FORMAT_FILTER | |
147 | ||
148 | #define format_options options | |
149 | AVFILTER_DEFINE_CLASS(format); | |
150 | ||
151 | static const AVFilterPad avfilter_vf_format_inputs[] = { | |
152 | { | |
153 | .name = "default", | |
154 | .type = AVMEDIA_TYPE_VIDEO, | |
155 | .get_video_buffer = ff_null_get_video_buffer, | |
156 | }, | |
157 | { NULL } | |
158 | }; | |
159 | ||
160 | static const AVFilterPad avfilter_vf_format_outputs[] = { | |
161 | { | |
162 | .name = "default", | |
163 | .type = AVMEDIA_TYPE_VIDEO | |
164 | }, | |
165 | { NULL } | |
166 | }; | |
167 | ||
168 | AVFilter ff_vf_format = { | |
169 | .name = "format", | |
170 | .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."), | |
171 | ||
172 | .init = init, | |
173 | .uninit = uninit, | |
174 | ||
175 | .query_formats = query_formats, | |
176 | ||
177 | .priv_size = sizeof(FormatContext), | |
178 | .priv_class = &format_class, | |
179 | ||
180 | .inputs = avfilter_vf_format_inputs, | |
181 | .outputs = avfilter_vf_format_outputs, | |
182 | }; | |
183 | #endif /* CONFIG_FORMAT_FILTER */ | |
184 | ||
185 | #if CONFIG_NOFORMAT_FILTER | |
186 | ||
187 | #define noformat_options options | |
188 | AVFILTER_DEFINE_CLASS(noformat); | |
189 | ||
190 | static const AVFilterPad avfilter_vf_noformat_inputs[] = { | |
191 | { | |
192 | .name = "default", | |
193 | .type = AVMEDIA_TYPE_VIDEO, | |
194 | .get_video_buffer = ff_null_get_video_buffer, | |
195 | }, | |
196 | { NULL } | |
197 | }; | |
198 | ||
199 | static const AVFilterPad avfilter_vf_noformat_outputs[] = { | |
200 | { | |
201 | .name = "default", | |
202 | .type = AVMEDIA_TYPE_VIDEO | |
203 | }, | |
204 | { NULL } | |
205 | }; | |
206 | ||
207 | AVFilter ff_vf_noformat = { | |
208 | .name = "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."), | |
210 | ||
211 | .init = init, | |
212 | .uninit = uninit, | |
213 | ||
214 | .query_formats = query_formats, | |
215 | ||
216 | .priv_size = sizeof(FormatContext), | |
217 | .priv_class = &noformat_class, | |
218 | ||
219 | .inputs = avfilter_vf_noformat_inputs, | |
220 | .outputs = avfilter_vf_noformat_outputs, | |
221 | }; | |
222 | #endif /* CONFIG_NOFORMAT_FILTER */ |