]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphaextract.c
lavfi/alphaextract: drop cur_linesize = out_linesize branch in draw_slice()
[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 "formats.h"
32 #include "video.h"
33
34 enum { Y, U, V, A };
35
36 typedef struct {
37     int is_packed_rgb;
38     uint8_t rgba_map[4];
39 } AlphaExtractContext;
40
41 static int query_formats(AVFilterContext *ctx)
42 {
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,
46         AV_PIX_FMT_NONE
47     };
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);
51     return 0;
52 }
53
54 static int config_input(AVFilterLink *inlink)
55 {
56     AlphaExtractContext *extract = inlink->dst->priv;
57     extract->is_packed_rgb =
58         ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
59     return 0;
60 }
61
62 static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
63 {
64     AlphaExtractContext *extract = inlink->dst->priv;
65     AVFilterBufferRef *cur_buf = inlink->cur_buf;
66     AVFilterBufferRef *out_buf = inlink->dst->outputs[0]->out_buf;
67
68     if (extract->is_packed_rgb) {
69         int x, y;
70         uint8_t *pin, *pout;
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++) {
75                 *pout = *pin;
76                 pout += 1;
77                 pin += 4;
78             }
79         }
80     } else {
81         const int linesize = FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]);
82         int y;
83         for (y = y0; y < (y0 + h); y++) {
84             memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
85                    cur_buf->data[A] + y * cur_buf->linesize[A],
86                    linesize);
87         }
88     }
89     return ff_draw_slice(inlink->dst->outputs[0], y0, h, slice_dir);
90 }
91
92 static const AVFilterPad alphaextract_inputs[] = {
93     {
94         .name         = "default",
95         .type         = AVMEDIA_TYPE_VIDEO,
96         .config_props = config_input,
97         .draw_slice   = draw_slice,
98         .min_perms    = AV_PERM_READ,
99     },
100     { NULL }
101 };
102
103 static const AVFilterPad alphaextract_outputs[] = {
104     {
105         .name = "default",
106         .type = AVMEDIA_TYPE_VIDEO,
107     },
108     { NULL }
109 };
110
111 AVFilter avfilter_vf_alphaextract = {
112     .name           = "alphaextract",
113     .description    = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
114                       "grayscale image component."),
115     .priv_size      = sizeof(AlphaExtractContext),
116     .query_formats  = query_formats,
117     .inputs         = alphaextract_inputs,
118     .outputs        = alphaextract_outputs,
119 };