]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphaextract.c
x86: Fix assembly with NASM
[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 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,
84                linesize * h);
85     } else {
86         const int linesize = FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]);
87         int y;
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],
91                    linesize);
92         }
93     }
94     return ff_draw_slice(inlink->dst->outputs[0], y0, h, slice_dir);
95 }
96
97 AVFilter avfilter_vf_alphaextract = {
98     .name           = "alphaextract",
99     .description    = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
100                       "grayscale image component."),
101     .priv_size      = sizeof(AlphaExtractContext),
102     .query_formats  = query_formats,
103
104     .inputs    = (const AVFilterPad[]) {
105         { .name             = "default",
106           .type             = AVMEDIA_TYPE_VIDEO,
107           .config_props     = config_input,
108           .draw_slice       = draw_slice,
109           .min_perms        = AV_PERM_READ },
110         { .name = NULL }
111     },
112     .outputs   = (const AVFilterPad[]) {
113       { .name               = "default",
114         .type               = AVMEDIA_TYPE_VIDEO, },
115       { .name = NULL }
116     },
117 };