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"
27 #include "drawutils.h"
39 typedef struct ExtractPlanesContext {
47 } ExtractPlanesContext;
49 #define OFFSET(x) offsetof(ExtractPlanesContext, x)
50 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
51 static const AVOption extractplanes_options[] = {
52 { "planes", "set planes", OFFSET(requested_planes), AV_OPT_TYPE_FLAGS, {.i64=1}, 1, 0xff, FLAGS, "flags"},
53 { "y", "set luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags"},
54 { "u", "set u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags"},
55 { "v", "set v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags"},
56 { "r", "set red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags"},
57 { "g", "set green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags"},
58 { "b", "set blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags"},
59 { "a", "set alpha plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_A}, 0, 0, FLAGS, "flags"},
63 AVFILTER_DEFINE_CLASS(extractplanes);
65 #define EIGHTBIT_FORMATS \
69 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, \
70 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, \
71 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, \
72 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, \
73 AV_PIX_FMT_YUVJ411P, \
74 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, \
75 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, \
76 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, \
77 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, \
78 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, \
79 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0, \
80 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, \
81 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP
83 #define HIGHDEPTH_FORMATS(suf) \
84 AV_PIX_FMT_YA16##suf, AV_PIX_FMT_GRAY16##suf, \
85 AV_PIX_FMT_YUV420P16##suf, AV_PIX_FMT_YUVA420P16##suf, \
86 AV_PIX_FMT_YUV422P16##suf, AV_PIX_FMT_YUVA422P16##suf, \
87 AV_PIX_FMT_YUV444P16##suf, AV_PIX_FMT_YUVA444P16##suf, \
88 AV_PIX_FMT_RGB48##suf, AV_PIX_FMT_BGR48##suf, \
89 AV_PIX_FMT_RGBA64##suf, AV_PIX_FMT_BGRA64##suf, \
90 AV_PIX_FMT_GBRP16##suf, AV_PIX_FMT_GBRAP16##suf, \
91 AV_PIX_FMT_YUV420P10##suf, \
92 AV_PIX_FMT_YUV422P10##suf, \
93 AV_PIX_FMT_YUV444P10##suf, \
94 AV_PIX_FMT_YUV440P10##suf, \
95 AV_PIX_FMT_YUVA420P10##suf, \
96 AV_PIX_FMT_YUVA422P10##suf, \
97 AV_PIX_FMT_YUVA444P10##suf, \
98 AV_PIX_FMT_YUV420P12##suf, \
99 AV_PIX_FMT_YUV422P12##suf, \
100 AV_PIX_FMT_YUV444P12##suf, \
101 AV_PIX_FMT_YUV440P12##suf, \
102 AV_PIX_FMT_GBRP10##suf, AV_PIX_FMT_GBRAP10##suf, \
103 AV_PIX_FMT_GBRP12##suf, AV_PIX_FMT_GBRAP12##suf, \
104 AV_PIX_FMT_YUV420P9##suf, \
105 AV_PIX_FMT_YUV422P9##suf, \
106 AV_PIX_FMT_YUV444P9##suf, \
107 AV_PIX_FMT_YUVA420P9##suf, \
108 AV_PIX_FMT_YUVA422P9##suf, \
109 AV_PIX_FMT_YUVA444P9##suf, \
110 AV_PIX_FMT_GBRP9##suf, \
111 AV_PIX_FMT_GBRP14##suf, \
112 AV_PIX_FMT_YUV420P14##suf, \
113 AV_PIX_FMT_YUV422P14##suf, \
114 AV_PIX_FMT_YUV444P14##suf
116 static int query_formats(AVFilterContext *ctx)
118 static const enum AVPixelFormat in_pixfmts_le[] = {
120 HIGHDEPTH_FORMATS(LE),
123 static const enum AVPixelFormat in_pixfmts_be[] = {
125 HIGHDEPTH_FORMATS(BE),
128 static const enum AVPixelFormat out8_pixfmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
129 static const enum AVPixelFormat out9le_pixfmts[] = { AV_PIX_FMT_GRAY9LE, AV_PIX_FMT_NONE };
130 static const enum AVPixelFormat out9be_pixfmts[] = { AV_PIX_FMT_GRAY9BE, AV_PIX_FMT_NONE };
131 static const enum AVPixelFormat out10le_pixfmts[] = { AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_NONE };
132 static const enum AVPixelFormat out10be_pixfmts[] = { AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_NONE };
133 static const enum AVPixelFormat out12le_pixfmts[] = { AV_PIX_FMT_GRAY12LE, AV_PIX_FMT_NONE };
134 static const enum AVPixelFormat out12be_pixfmts[] = { AV_PIX_FMT_GRAY12BE, AV_PIX_FMT_NONE };
135 static const enum AVPixelFormat out14le_pixfmts[] = { AV_PIX_FMT_GRAY14LE, AV_PIX_FMT_NONE };
136 static const enum AVPixelFormat out14be_pixfmts[] = { AV_PIX_FMT_GRAY14BE, AV_PIX_FMT_NONE };
137 static const enum AVPixelFormat out16le_pixfmts[] = { AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_NONE };
138 static const enum AVPixelFormat out16be_pixfmts[] = { AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE };
139 const enum AVPixelFormat *out_pixfmts, *in_pixfmts;
140 const AVPixFmtDescriptor *desc;
141 AVFilterFormats *avff;
142 int i, ret, depth = 0, be = 0;
144 if (!ctx->inputs[0]->in_formats ||
145 !ctx->inputs[0]->in_formats->nb_formats) {
146 return AVERROR(EAGAIN);
149 avff = ctx->inputs[0]->in_formats;
150 desc = av_pix_fmt_desc_get(avff->formats[0]);
151 depth = desc->comp[0].depth;
152 be = desc->flags & AV_PIX_FMT_FLAG_BE;
154 in_pixfmts = in_pixfmts_be;
156 in_pixfmts = in_pixfmts_le;
158 if (!ctx->inputs[0]->out_formats)
159 if ((ret = ff_formats_ref(ff_make_format_list(in_pixfmts), &ctx->inputs[0]->out_formats)) < 0)
162 for (i = 1; i < avff->nb_formats; i++) {
163 desc = av_pix_fmt_desc_get(avff->formats[i]);
164 if (depth != desc->comp[0].depth ||
165 be != (desc->flags & AV_PIX_FMT_FLAG_BE)) {
166 return AVERROR(EAGAIN);
171 out_pixfmts = out8_pixfmts;
172 else if (!be && depth == 9)
173 out_pixfmts = out9le_pixfmts;
174 else if (be && depth == 9)
175 out_pixfmts = out9be_pixfmts;
176 else if (!be && depth == 10)
177 out_pixfmts = out10le_pixfmts;
178 else if (be && depth == 10)
179 out_pixfmts = out10be_pixfmts;
180 else if (!be && depth == 12)
181 out_pixfmts = out12le_pixfmts;
182 else if (be && depth == 12)
183 out_pixfmts = out12be_pixfmts;
184 else if (!be && depth == 14)
185 out_pixfmts = out14le_pixfmts;
186 else if (be && depth == 14)
187 out_pixfmts = out14be_pixfmts;
189 out_pixfmts = out16be_pixfmts;
191 out_pixfmts = out16le_pixfmts;
193 for (i = 0; i < ctx->nb_outputs; i++)
194 if ((ret = ff_formats_ref(ff_make_format_list(out_pixfmts), &ctx->outputs[i]->in_formats)) < 0)
199 static int config_input(AVFilterLink *inlink)
201 AVFilterContext *ctx = inlink->dst;
202 ExtractPlanesContext *s = ctx->priv;
203 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
204 int plane_avail, ret, i;
207 plane_avail = ((desc->flags & AV_PIX_FMT_FLAG_RGB) ? PLANE_R|PLANE_G|PLANE_B :
209 ((desc->nb_components > 2) ? PLANE_U|PLANE_V : 0)) |
210 ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? PLANE_A : 0);
211 if (s->requested_planes & ~plane_avail) {
212 av_log(ctx, AV_LOG_ERROR, "Requested planes not available.\n");
213 return AVERROR(EINVAL);
215 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
218 s->depth = desc->comp[0].depth >> 3;
219 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
220 s->is_packed = !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
221 (desc->nb_components > 1);
222 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
223 ff_fill_rgba_map(rgba_map, inlink->format);
224 for (i = 0; i < 4; i++)
225 s->map[i] = rgba_map[s->map[i]];
231 static int config_output(AVFilterLink *outlink)
233 AVFilterContext *ctx = outlink->src;
234 AVFilterLink *inlink = ctx->inputs[0];
235 ExtractPlanesContext *s = ctx->priv;
236 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
237 const int output = outlink->srcpad - ctx->output_pads;
239 if (s->map[output] == 1 || s->map[output] == 2) {
240 outlink->h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
241 outlink->w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
247 static void extract_from_packed(uint8_t *dst, int dst_linesize,
248 const uint8_t *src, int src_linesize,
249 int width, int height,
250 int depth, int step, int comp)
254 for (y = 0; y < height; y++) {
257 for (x = 0; x < width; x++)
258 dst[x] = src[x * step + comp];
261 for (x = 0; x < width; x++) {
262 dst[x * 2 ] = src[x * step + comp * 2 ];
263 dst[x * 2 + 1] = src[x * step + comp * 2 + 1];
272 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
274 AVFilterContext *ctx = inlink->dst;
275 ExtractPlanesContext *s = ctx->priv;
276 int i, eof = 0, ret = 0;
278 for (i = 0; i < ctx->nb_outputs; i++) {
279 AVFilterLink *outlink = ctx->outputs[i];
280 const int idx = s->map[i];
283 if (ff_outlink_get_status(outlink))
286 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
288 ret = AVERROR(ENOMEM);
291 av_frame_copy_props(out, frame);
294 extract_from_packed(out->data[0], out->linesize[0],
295 frame->data[0], frame->linesize[0],
296 outlink->w, outlink->h,
300 av_image_copy_plane(out->data[0], out->linesize[0],
301 frame->data[idx], frame->linesize[idx],
302 s->linesize[idx], outlink->h);
305 ret = ff_filter_frame(outlink, out);
306 if (ret == AVERROR_EOF)
311 av_frame_free(&frame);
313 if (eof == ctx->nb_outputs)
315 else if (ret == AVERROR_EOF)
320 static av_cold int init(AVFilterContext *ctx)
322 ExtractPlanesContext *s = ctx->priv;
323 int planes = (s->requested_planes & 0xf) | (s->requested_planes >> 4);
326 for (i = 0; i < 4; i++) {
328 AVFilterPad pad = { 0 };
330 if (!(planes & (1 << i)))
333 name = av_asprintf("out%d", ctx->nb_outputs);
335 return AVERROR(ENOMEM);
336 s->map[ctx->nb_outputs] = i;
338 pad.type = AVMEDIA_TYPE_VIDEO;
339 pad.config_props = config_output;
341 if ((ret = ff_insert_outpad(ctx, ctx->nb_outputs, &pad)) < 0) {
350 static av_cold void uninit(AVFilterContext *ctx)
354 for (i = 0; i < ctx->nb_outputs; i++)
355 av_freep(&ctx->output_pads[i].name);
358 static const AVFilterPad extractplanes_inputs[] = {
361 .type = AVMEDIA_TYPE_VIDEO,
362 .filter_frame = filter_frame,
363 .config_props = config_input,
368 AVFilter ff_vf_extractplanes = {
369 .name = "extractplanes",
370 .description = NULL_IF_CONFIG_SMALL("Extract planes as grayscale frames."),
371 .priv_size = sizeof(ExtractPlanesContext),
372 .priv_class = &extractplanes_class,
375 .query_formats = query_formats,
376 .inputs = extractplanes_inputs,
378 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
381 #if CONFIG_ALPHAEXTRACT_FILTER
383 static av_cold int init_alphaextract(AVFilterContext *ctx)
385 ExtractPlanesContext *s = ctx->priv;
387 s->requested_planes = PLANE_A;
392 AVFilter ff_vf_alphaextract = {
393 .name = "alphaextract",
394 .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
395 "grayscale image component."),
396 .priv_size = sizeof(ExtractPlanesContext),
397 .init = init_alphaextract,
399 .query_formats = query_formats,
400 .inputs = extractplanes_inputs,
402 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
404 #endif /* CONFIG_ALPHAEXTRACT_FILTER */