summaryrefslogtreecommitdiff
path: root/libavfilter/vf_extractplanes.c (plain)
blob: 65bba33d90c694ef05153955e2274fce0fcef65a
1/*
2 * Copyright (c) 2013 Paul B Mahol
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#include "libavutil/avstring.h"
22#include "libavutil/imgutils.h"
23#include "libavutil/opt.h"
24#include "libavutil/pixdesc.h"
25
26#define FF_INTERNAL_FIELDS 1
27#include "libavfilter/framequeue.h"
28
29#include "avfilter.h"
30#include "drawutils.h"
31#include "internal.h"
32
33#define PLANE_R 0x01
34#define PLANE_G 0x02
35#define PLANE_B 0x04
36#define PLANE_A 0x08
37#define PLANE_Y 0x10
38#define PLANE_U 0x20
39#define PLANE_V 0x40
40
41typedef struct {
42 const AVClass *class;
43 int requested_planes;
44 int map[4];
45 int linesize[4];
46 int is_packed;
47 int depth;
48 int step;
49} ExtractPlanesContext;
50
51#define OFFSET(x) offsetof(ExtractPlanesContext, x)
52#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53static const AVOption extractplanes_options[] = {
54 { "planes", "set planes", OFFSET(requested_planes), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, 0xff, FLAGS, "flags"},
55 { "y", "set luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags"},
56 { "u", "set u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags"},
57 { "v", "set v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags"},
58 { "r", "set red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags"},
59 { "g", "set green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags"},
60 { "b", "set blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags"},
61 { "a", "set alpha plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_A}, 0, 0, FLAGS, "flags"},
62 { NULL }
63};
64
65AVFILTER_DEFINE_CLASS(extractplanes);
66
67static int query_formats(AVFilterContext *ctx)
68{
69 static const enum AVPixelFormat in_pixfmts_le[] = {
70 AV_PIX_FMT_YUV410P,
71 AV_PIX_FMT_YUV411P,
72 AV_PIX_FMT_YUV440P,
73 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
74 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
75 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
76 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
77 AV_PIX_FMT_YUVJ411P,
78 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
79 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
80 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
81 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
82 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
83 AV_PIX_FMT_YA16LE, AV_PIX_FMT_GRAY16LE,
84 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
85 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
86 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
87 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
88 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
89 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
90 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
91 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
92 AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRAP16LE,
93 AV_PIX_FMT_YUV420P10LE,
94 AV_PIX_FMT_YUV422P10LE,
95 AV_PIX_FMT_YUV444P10LE,
96 AV_PIX_FMT_YUV440P10LE,
97 AV_PIX_FMT_YUVA420P10LE,
98 AV_PIX_FMT_YUVA422P10LE,
99 AV_PIX_FMT_YUVA444P10LE,
100 AV_PIX_FMT_YUV420P12LE,
101 AV_PIX_FMT_YUV422P12LE,
102 AV_PIX_FMT_YUV444P12LE,
103 AV_PIX_FMT_YUV440P12LE,
104 AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRAP10LE,
105 AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRAP12LE,
106 AV_PIX_FMT_NONE,
107 };
108 static const enum AVPixelFormat in_pixfmts_be[] = {
109 AV_PIX_FMT_YUV410P,
110 AV_PIX_FMT_YUV411P,
111 AV_PIX_FMT_YUV440P,
112 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
113 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
114 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
115 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
116 AV_PIX_FMT_YUVJ411P,
117 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
118 AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
119 AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
120 AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
121 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
122 AV_PIX_FMT_YA16BE, AV_PIX_FMT_GRAY16BE,
123 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
124 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
125 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
126 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
127 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
128 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
129 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
130 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
131 AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRAP16BE,
132 AV_PIX_FMT_YUV420P10BE,
133 AV_PIX_FMT_YUV422P10BE,
134 AV_PIX_FMT_YUV444P10BE,
135 AV_PIX_FMT_YUV440P10BE,
136 AV_PIX_FMT_YUVA420P10BE,
137 AV_PIX_FMT_YUVA422P10BE,
138 AV_PIX_FMT_YUVA444P10BE,
139 AV_PIX_FMT_YUV420P12BE,
140 AV_PIX_FMT_YUV422P12BE,
141 AV_PIX_FMT_YUV444P12BE,
142 AV_PIX_FMT_YUV440P12BE,
143 AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRAP10BE,
144 AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRAP12BE,
145 AV_PIX_FMT_NONE,
146 };
147 static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
148 static const enum AVPixelFormat out10le_pixfmts[] = { AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_NONE };
149 static const enum AVPixelFormat out10be_pixfmts[] = { AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_NONE };
150 static const enum AVPixelFormat out12le_pixfmts[] = { AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_NONE };
151 static const enum AVPixelFormat out12be_pixfmts[] = { AV_PIX_FMT_GRAY12BE, AV_PIX_FMT_NONE };
152 static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
153 static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
154 const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
155 const AVPixFmtDescriptor *desc;
156 AVFilterFormats *avff;
157 int i, ret, depth = 0, be = 0;
158
159 if (!ctx->inputs[0]->in_formats ||
160 !ctx->inputs[0]->in_formats->nb_formats) {
161 return AVERROR(EAGAIN);
162 }
163
164 avff = ctx->inputs[0]->in_formats;
165 desc = av_pix_fmt_desc_get(avff->formats[0]);
166 depth = desc->comp[0].depth;
167 be = desc->flags & AV_PIX_FMT_FLAG_BE;
168 if (be) {
169 in_pixfmts = in_pixfmts_be;
170 } else {
171 in_pixfmts = in_pixfmts_le;
172 }
173 if (!ctx->inputs[0]->out_formats)
174 if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
175 return ret;
176
177 for (i = 1; i < avff->nb_formats; i++) {
178 desc = av_pix_fmt_desc_get(avff->formats[i]);
179 if (depth != desc->comp[0].depth ||
180 be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
181 return AVERROR(EAGAIN);
182 }
183 }
184
185 if (depth == 8)
186 out_pixfmts = out8_pixfmts;
187 else if (!be && depth == 10)
188 out_pixfmts = out10le_pixfmts;
189 else if (be && depth == 10)
190 out_pixfmts = out10be_pixfmts;
191 else if (!be && depth == 12)
192 out_pixfmts = out12le_pixfmts;
193 else if (be && depth == 12)
194 out_pixfmts = out12be_pixfmts;
195 else if (be)
196 out_pixfmts = out16be_pixfmts;
197 else
198 out_pixfmts = out16le_pixfmts;
199
200 for (i = 0; i < ctx->nb_outputs; i++)
201 if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
202 return ret;
203 return 0;
204}
205
206static int config_input(AVFilterLink *inlink)
207{
208 AVFilterContext *ctx = inlink->dst;
209 ExtractPlanesContext *s = ctx->priv;
210 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
211 int plane_avail, ret, i;
212 uint8_t rgba_map[4];
213
214 plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
215 PLANE_Y |
216 ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
217 ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
218 if (s->requested_planes & ~plane_avail) {
219 av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
220 return AVERROR(EINVAL);
221 }
222 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
223 return ret;
224
225 s->depth = desc->comp[0].depth >> 3;
226 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
227 s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
228 (desc->nb_components > 1);
229 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
230 ff_fill_rgba_map(rgba_map, inlink->format);
231 for (i = 0; i < 4; i++)
232 s->map[i] = rgba_map[s->map[i]];
233 }
234
235 return 0;
236}
237
238static int config_output(AVFilterLink *outlink)
239{
240 AVFilterContext *ctx = outlink->src;
241 AVFilterLink *inlink = ctx->inputs[0];
242 ExtractPlanesContext *s = ctx->priv;
243 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
244 const int output = outlink->srcpad - ctx->output_pads;
245
246 if (s->map[output] == 1 || s->map[output] == 2) {
247 outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
248 outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
249 }
250
251 return 0;
252}
253
254static void extract_from_packed(uint8_t *dst, int dst_linesize,
255 const uint8_t *src, int src_linesize,
256 int width, int height,
257 int depth, int step, int comp)
258{
259 int x, y;
260
261 for (y = 0; y < height; y++) {
262 switch (depth) {
263 case 1:
264 for (x = 0; x < width; x++)
265 dst[x] = src[x * step + comp];
266 break;
267 case 2:
268 for (x = 0; x < width; x++) {
269 dst[x * 2 ] = src[x * step + comp * 2 ];
270 dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
271 }
272 break;
273 }
274 dst += dst_linesize;
275 src += src_linesize;
276 }
277}
278
279static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
280{
281 AVFilterContext *ctx = inlink->dst;
282 ExtractPlanesContext *s = ctx->priv;
283 int i, eof = 0, ret = 0;
284
285 for (i = 0; i < ctx->nb_outputs; i++) {
286 AVFilterLink *outlink = ctx->outputs[i];
287 const int idx = s->map[i];
288 AVFrame *out;
289
290 if (outlink->status_in)
291 continue;
292
293 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
294 if (!out) {
295 ret = AVERROR(ENOMEM);
296 break;
297 }
298 av_frame_copy_props(out, frame);
299
300 if (s->is_packed) {
301 extract_from_packed(out->data[0], out->linesize[0],
302 frame->data[0], frame->linesize[0],
303 outlink->w, outlink->h,
304 s->depth,
305 s->step, idx);
306 } else {
307 av_image_copy_plane(out->data[0], out->linesize[0],
308 frame->data[idx], frame->linesize[idx],
309 s->linesize[idx], outlink->h);
310 }
311
312 ret = ff_filter_frame(outlink, out);
313 if (ret == AVERROR_EOF)
314 eof++;
315 else if (ret < 0)
316 break;
317 }
318 av_frame_free(&frame);
319
320 if (eof == ctx->nb_outputs)
321 ret = AVERROR_EOF;
322 else if (ret == AVERROR_EOF)
323 ret = 0;
324 return ret;
325}
326
327static av_cold int init(AVFilterContext *ctx)
328{
329 ExtractPlanesContext *s = ctx->priv;
330 int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
331 int i;
332
333 for (i = 0; i < 4; i++) {
334 char *name;
335 AVFilterPad pad = { 0 };
336
337 if (!(planes & (1 << i)))
338 continue;
339
340 name = av_asprintf("out%d", ctx->nb_outputs);
341 if (!name)
342 return AVERROR(ENOMEM);
343 s->map[ctx->nb_outputs] = i;
344 pad.name = name;
345 pad.type = AVMEDIA_TYPE_VIDEO;
346 pad.config_props = config_output;
347
348 ff_insert_outpad(ctx, ctx->nb_outputs, &pad);
349 }
350
351 return 0;
352}
353
354static av_cold void uninit(AVFilterContext *ctx)
355{
356 int i;
357
358 for (i = 0; i < ctx->nb_outputs; i++)
359 av_freep(&ctx->output_pads[i].name);
360}
361
362static const AVFilterPad extractplanes_inputs[] = {
363 {
364 .name = "default",
365 .type = AVMEDIA_TYPE_VIDEO,
366 .filter_frame = filter_frame,
367 .config_props = config_input,
368 },
369 { NULL }
370};
371
372AVFilter ff_vf_extractplanes = {
373 .name = "extractplanes",
374 .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
375 .priv_size = sizeof(ExtractPlanesContext),
376 .priv_class = &extractplanes_class,
377 .init = init,
378 .uninit = uninit,
379 .query_formats = query_formats,
380 .inputs = extractplanes_inputs,
381 .outputs = NULL,
382 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
383};
384
385#if CONFIG_ALPHAEXTRACT_FILTER
386
387static av_cold int init_alphaextract(AVFilterContext *ctx)
388{
389 ExtractPlanesContext *s = ctx->priv;
390
391 s->requested_planes = PLANE_A;
392
393 return init(ctx);
394}
395
396AVFilter ff_vf_alphaextract = {
397 .name = "alphaextract",
398 .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
399 "grayscale image component."),
400 .priv_size = sizeof(ExtractPlanesContext),
401 .init = init_alphaextract,
402 .uninit = uninit,
403 .query_formats = query_formats,
404 .inputs = extractplanes_inputs,
405 .outputs = NULL,
406 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
407};
408#endif /* CONFIG_ALPHAEXTRACT_FILTER */
409