]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphaextract.c
Merge commit '27c8337e595a058347150269d5c2c48281e4285b'
[ffmpeg] / libavfilter / vf_alphaextract.c
1 /*
2  * Copyright (c) 2012 Steven Robertson
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  * simple channel-swapping filter to get at the alpha component
24  */
25
26 #include <string.h>
27
28 #include "libavutil/pixfmt.h"
29 #include "avfilter.h"
30 #include "drawutils.h"
31 #include "internal.h"
32 #include "formats.h"
33 #include "video.h"
34
35 enum { Y, U, V, A };
36
37 typedef struct {
38     int is_packed_rgb;
39     uint8_t rgba_map[4];
40 } AlphaExtractContext;
41
42 static int query_formats(AVFilterContext *ctx)
43 {
44     enum AVPixelFormat in_fmts[] = {
45         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
46         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
47         AV_PIX_FMT_NONE
48     };
49     enum AVPixelFormat out_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
50     ff_formats_ref(ff_make_format_list(in_fmts), &ctx->inputs[0]->out_formats);
51     ff_formats_ref(ff_make_format_list(out_fmts), &ctx->outputs[0]->in_formats);
52     return 0;
53 }
54
55 static int config_input(AVFilterLink *inlink)
56 {
57     AlphaExtractContext *extract = inlink->dst->priv;
58     extract->is_packed_rgb =
59         ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
60     return 0;
61 }
62
63 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *cur_buf)
64 {
65     AlphaExtractContext *extract = inlink->dst->priv;
66     AVFilterLink *outlink = inlink->dst->outputs[0];
67     AVFilterBufferRef *out_buf =
68         ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
69     int ret;
70
71     if (!out_buf) {
72         ret = AVERROR(ENOMEM);
73         goto end;
74     }
75     avfilter_copy_buffer_ref_props(out_buf, cur_buf);
76
77     if (extract->is_packed_rgb) {
78         int x, y;
79         uint8_t *pcur, *pout;
80         for (y = 0; y < outlink->h; y++) {
81             pcur = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
82             pout = out_buf->data[0] + y * out_buf->linesize[0];
83             for (x = 0; x < outlink->w; x++) {
84                 *pout = *pcur;
85                 pout += 1;
86                 pcur += 4;
87             }
88         }
89     } else {
90         const int linesize = abs(FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]));
91         int y;
92         for (y = 0; y < outlink->h; y++) {
93             memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
94                    cur_buf->data[A] + y * cur_buf->linesize[A],
95                    linesize);
96         }
97     }
98
99     ret = ff_filter_frame(outlink, out_buf);
100
101 end:
102     avfilter_unref_buffer(cur_buf);
103     return ret;
104 }
105
106 static const AVFilterPad alphaextract_inputs[] = {
107     {
108         .name         = "default",
109         .type         = AVMEDIA_TYPE_VIDEO,
110         .config_props = config_input,
111         .filter_frame = filter_frame,
112         .min_perms    = AV_PERM_READ,
113     },
114     { NULL }
115 };
116
117 static const AVFilterPad alphaextract_outputs[] = {
118     {
119         .name = "default",
120         .type = AVMEDIA_TYPE_VIDEO,
121     },
122     { NULL }
123 };
124
125 AVFilter avfilter_vf_alphaextract = {
126     .name           = "alphaextract",
127     .description    = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
128                       "grayscale image component."),
129     .priv_size      = sizeof(AlphaExtractContext),
130     .query_formats  = query_formats,
131     .inputs         = alphaextract_inputs,
132     .outputs        = alphaextract_outputs,
133 };