2 * Copyright (c) 2012 Steven Robertson
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
23 * simple channel-swapping filter to get at the alpha component
28 #include "libavutil/pixfmt.h"
30 #include "drawutils.h"
39 } AlphaExtractContext;
41 static int query_formats(AVFilterContext *ctx)
43 enum AVPixelFormat in_fmts[] = {
44 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
45 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
48 enum AVPixelFormat out_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
49 ff_formats_ref(ff_make_format_list(in_fmts), &ctx->inputs[0]->out_formats);
50 ff_formats_ref(ff_make_format_list(out_fmts), &ctx->outputs[0]->in_formats);
54 static int config_input(AVFilterLink *inlink)
56 AlphaExtractContext *extract = inlink->dst->priv;
57 extract->is_packed_rgb =
58 ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
62 static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
64 AlphaExtractContext *extract = inlink->dst->priv;
65 AVFilterBufferRef *cur_buf = inlink->cur_buf;
66 AVFilterBufferRef *out_buf = inlink->dst->outputs[0]->out_buf;
68 if (extract->is_packed_rgb) {
71 for (y = y0; y < (y0 + h); y++) {
72 pin = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
73 pout = out_buf->data[0] + y * out_buf->linesize[0];
74 for (x = 0; x < out_buf->video->w; x++) {
80 } else if (cur_buf->linesize[A] == out_buf->linesize[Y]) {
81 const int linesize = cur_buf->linesize[A];
82 memcpy(out_buf->data[Y] + y0 * linesize,
83 cur_buf->data[A] + y0 * linesize,
86 const int linesize = FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]);
88 for (y = y0; y < (y0 + h); y++) {
89 memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
90 cur_buf->data[A] + y * cur_buf->linesize[A],
94 return ff_draw_slice(inlink->dst->outputs[0], y0, h, slice_dir);
97 static const AVFilterPad alphaextract_inputs[] = {
100 .type = AVMEDIA_TYPE_VIDEO,
101 .config_props = config_input,
102 .draw_slice = draw_slice,
103 .min_perms = AV_PERM_READ,
108 static const AVFilterPad alphaextract_outputs[] = {
111 .type = AVMEDIA_TYPE_VIDEO,
116 AVFilter avfilter_vf_alphaextract = {
117 .name = "alphaextract",
118 .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
119 "grayscale image component."),
120 .priv_size = sizeof(AlphaExtractContext),
121 .query_formats = query_formats,
122 .inputs = alphaextract_inputs,
123 .outputs = alphaextract_outputs,