2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avstring.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
26 #define FF_INTERNAL_FIELDS 1
27 #include "libavfilter/framequeue.h"
30 #include "drawutils.h"
41 typedef struct ExtractPlanesContext {
49 } ExtractPlanesContext;
51 #define OFFSET(x) offsetof(ExtractPlanesContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static 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"},
65 AVFILTER_DEFINE_CLASS(extractplanes);
67 static int query_formats(AVFilterContext *ctx)
69 static const enum AVPixelFormat in_pixfmts_le[] = {
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,
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_YUV420P9LE,
107 AV_PIX_FMT_YUV422P9LE,
108 AV_PIX_FMT_YUV444P9LE,
109 AV_PIX_FMT_YUVA420P9LE,
110 AV_PIX_FMT_YUVA422P9LE,
111 AV_PIX_FMT_YUVA444P9LE,
114 AV_PIX_FMT_YUV420P14LE,
115 AV_PIX_FMT_YUV422P14LE,
116 AV_PIX_FMT_YUV444P14LE,
119 static const enum AVPixelFormat in_pixfmts_be[] = {
123 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
124 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
125 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
126 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
128 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
129 AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
130 AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
131 AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
132 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
133 AV_PIX_FMT_YA16BE, AV_PIX_FMT_GRAY16BE,
134 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
135 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
136 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
137 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
138 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
139 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
140 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
141 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
142 AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRAP16BE,
143 AV_PIX_FMT_YUV420P10BE,
144 AV_PIX_FMT_YUV422P10BE,
145 AV_PIX_FMT_YUV444P10BE,
146 AV_PIX_FMT_YUV440P10BE,
147 AV_PIX_FMT_YUVA420P10BE,
148 AV_PIX_FMT_YUVA422P10BE,
149 AV_PIX_FMT_YUVA444P10BE,
150 AV_PIX_FMT_YUV420P12BE,
151 AV_PIX_FMT_YUV422P12BE,
152 AV_PIX_FMT_YUV444P12BE,
153 AV_PIX_FMT_YUV440P12BE,
154 AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRAP10BE,
155 AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRAP12BE,
156 AV_PIX_FMT_YUV420P9BE,
157 AV_PIX_FMT_YUV422P9BE,
158 AV_PIX_FMT_YUV444P9BE,
159 AV_PIX_FMT_YUVA420P9BE,
160 AV_PIX_FMT_YUVA422P9BE,
161 AV_PIX_FMT_YUVA444P9BE,
164 AV_PIX_FMT_YUV420P14BE,
165 AV_PIX_FMT_YUV422P14BE,
166 AV_PIX_FMT_YUV444P14BE,
169 static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
170 static const enum AVPixelFormat out9le_pixfmts[] = { AV_PIX_FMT_GRAY9LE, AV_PIX_FMT_NONE };
171 static const enum AVPixelFormat out9be_pixfmts[] = { AV_PIX_FMT_GRAY9BE, AV_PIX_FMT_NONE };
172 static const enum AVPixelFormat out10le_pixfmts[] = { AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_NONE };
173 static const enum AVPixelFormat out10be_pixfmts[] = { AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_NONE };
174 static const enum AVPixelFormat out12le_pixfmts[] = { AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_NONE };
175 static const enum AVPixelFormat out12be_pixfmts[] = { AV_PIX_FMT_GRAY12BE, AV_PIX_FMT_NONE };
176 static const enum AVPixelFormat out14le_pixfmts[] = { AV_PIX_FMT_GRAY14LE, AV_PIX_FMT_NONE };
177 static const enum AVPixelFormat out14be_pixfmts[] = { AV_PIX_FMT_GRAY14BE, AV_PIX_FMT_NONE };
178 static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
179 static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
180 const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
181 const AVPixFmtDescriptor *desc;
182 AVFilterFormats *avff;
183 int i, ret, depth = 0, be = 0;
185 if (!ctx->inputs[0]->in_formats ||
186 !ctx->inputs[0]->in_formats->nb_formats) {
187 return AVERROR(EAGAIN);
190 avff = ctx->inputs[0]->in_formats;
191 desc = av_pix_fmt_desc_get(avff->formats[0]);
192 depth = desc->comp[0].depth;
193 be = desc->flags & AV_PIX_FMT_FLAG_BE;
195 in_pixfmts = in_pixfmts_be;
197 in_pixfmts = in_pixfmts_le;
199 if (!ctx->inputs[0]->out_formats)
200 if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
203 for (i = 1; i < avff->nb_formats; i++) {
204 desc = av_pix_fmt_desc_get(avff->formats[i]);
205 if (depth != desc->comp[0].depth ||
206 be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
207 return AVERROR(EAGAIN);
212 out_pixfmts = out8_pixfmts;
213 else if (!be && depth == 9)
214 out_pixfmts = out9le_pixfmts;
215 else if (be && depth == 9)
216 out_pixfmts = out9be_pixfmts;
217 else if (!be && depth == 10)
218 out_pixfmts = out10le_pixfmts;
219 else if (be && depth == 10)
220 out_pixfmts = out10be_pixfmts;
221 else if (!be && depth == 12)
222 out_pixfmts = out12le_pixfmts;
223 else if (be && depth == 12)
224 out_pixfmts = out12be_pixfmts;
225 else if (!be && depth == 14)
226 out_pixfmts = out14le_pixfmts;
227 else if (be && depth == 14)
228 out_pixfmts = out14be_pixfmts;
230 out_pixfmts = out16be_pixfmts;
232 out_pixfmts = out16le_pixfmts;
234 for (i = 0; i < ctx->nb_outputs; i++)
235 if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
240 static int config_input(AVFilterLink *inlink)
242 AVFilterContext *ctx = inlink->dst;
243 ExtractPlanesContext *s = ctx->priv;
244 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
245 int plane_avail, ret, i;
248 plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
250 ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
251 ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
252 if (s->requested_planes & ~plane_avail) {
253 av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
254 return AVERROR(EINVAL);
256 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
259 s->depth = desc->comp[0].depth >> 3;
260 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
261 s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
262 (desc->nb_components > 1);
263 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
264 ff_fill_rgba_map(rgba_map, inlink->format);
265 for (i = 0; i < 4; i++)
266 s->map[i] = rgba_map[s->map[i]];
272 static int config_output(AVFilterLink *outlink)
274 AVFilterContext *ctx = outlink->src;
275 AVFilterLink *inlink = ctx->inputs[0];
276 ExtractPlanesContext *s = ctx->priv;
277 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
278 const int output = outlink->srcpad - ctx->output_pads;
280 if (s->map[output] == 1 || s->map[output] == 2) {
281 outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
282 outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
288 static void extract_from_packed(uint8_t *dst, int dst_linesize,
289 const uint8_t *src, int src_linesize,
290 int width, int height,
291 int depth, int step, int comp)
295 for (y = 0; y < height; y++) {
298 for (x = 0; x < width; x++)
299 dst[x] = src[x * step + comp];
302 for (x = 0; x < width; x++) {
303 dst[x * 2 ] = src[x * step + comp * 2 ];
304 dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
313 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
315 AVFilterContext *ctx = inlink->dst;
316 ExtractPlanesContext *s = ctx->priv;
317 int i, eof = 0, ret = 0;
319 for (i = 0; i < ctx->nb_outputs; i++) {
320 AVFilterLink *outlink = ctx->outputs[i];
321 const int idx = s->map[i];
324 if (outlink->status_in)
327 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
329 ret = AVERROR(ENOMEM);
332 av_frame_copy_props(out, frame);
335 extract_from_packed(out->data[0], out->linesize[0],
336 frame->data[0], frame->linesize[0],
337 outlink->w, outlink->h,
341 av_image_copy_plane(out->data[0], out->linesize[0],
342 frame->data[idx], frame->linesize[idx],
343 s->linesize[idx], outlink->h);
346 ret = ff_filter_frame(outlink, out);
347 if (ret == AVERROR_EOF)
352 av_frame_free(&frame);
354 if (eof == ctx->nb_outputs)
356 else if (ret == AVERROR_EOF)
361 static av_cold int init(AVFilterContext *ctx)
363 ExtractPlanesContext *s = ctx->priv;
364 int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
367 for (i = 0; i < 4; i++) {
369 AVFilterPad pad = { 0 };
371 if (!(planes & (1 << i)))
374 name = av_asprintf("out%d", ctx->nb_outputs);
376 return AVERROR(ENOMEM);
377 s->map[ctx->nb_outputs] = i;
379 pad.type = AVMEDIA_TYPE_VIDEO;
380 pad.config_props = config_output;
382 if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
391 static av_cold void uninit(AVFilterContext *ctx)
395 for (i = 0; i < ctx->nb_outputs; i++)
396 av_freep(&ctx->output_pads[i].name);
399 static const AVFilterPad extractplanes_inputs[] = {
402 .type = AVMEDIA_TYPE_VIDEO,
403 .filter_frame = filter_frame,
404 .config_props = config_input,
409 AVFilter ff_vf_extractplanes = {
410 .name = "extractplanes",
411 .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
412 .priv_size = sizeof(ExtractPlanesContext),
413 .priv_class = &extractplanes_class,
416 .query_formats = query_formats,
417 .inputs = extractplanes_inputs,
419 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
422 #if CONFIG_ALPHAEXTRACT_FILTER
424 static av_cold int init_alphaextract(AVFilterContext *ctx)
426 ExtractPlanesContext *s = ctx->priv;
428 s->requested_planes = PLANE_A;
433 AVFilter ff_vf_alphaextract = {
434 .name = "alphaextract",
435 .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
436 "grayscale image component."),
437 .priv_size = sizeof(ExtractPlanesContext),
438 .init = init_alphaextract,
440 .query_formats = query_formats,
441 .inputs = extractplanes_inputs,
443 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
445 #endif /* CONFIG_ALPHAEXTRACT_FILTER */