]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphamerge.c
3c62b59ad362d033e1a8c6529e429c790b84f7e7
[ffmpeg] / libavfilter / vf_alphamerge.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  * copy an alpha component from another video's luma
24  */
25
26 #include <string.h>
27
28 #include "libavutil/pixfmt.h"
29 #include "avfilter.h"
30 #include "bufferqueue.h"
31 #include "drawutils.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 enum { Y, U, V, A };
37
38 typedef struct {
39     int is_packed_rgb;
40     uint8_t rgba_map[4];
41     struct FFBufQueue queue_main;
42     struct FFBufQueue queue_alpha;
43 } AlphaMergeContext;
44
45 static av_cold void uninit(AVFilterContext *ctx)
46 {
47     AlphaMergeContext *merge = ctx->priv;
48     ff_bufqueue_discard_all(&merge->queue_main);
49     ff_bufqueue_discard_all(&merge->queue_alpha);
50 }
51
52 static int query_formats(AVFilterContext *ctx)
53 {
54     static const enum AVPixelFormat main_fmts[] = {
55         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
56         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
57         AV_PIX_FMT_NONE
58     };
59     static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
60     AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
61     AVFilterFormats *alpha_formats = ff_make_format_list(alpha_fmts);
62     ff_formats_ref(main_formats, &ctx->inputs[0]->out_formats);
63     ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats);
64     ff_formats_ref(main_formats, &ctx->outputs[0]->in_formats);
65     return 0;
66 }
67
68 static int config_input_main(AVFilterLink *inlink)
69 {
70     AlphaMergeContext *merge = inlink->dst->priv;
71     merge->is_packed_rgb =
72         ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
73     return 0;
74 }
75
76 static int config_output(AVFilterLink *outlink)
77 {
78     AVFilterContext *ctx = outlink->src;
79     AVFilterLink *mainlink = ctx->inputs[0];
80     AVFilterLink *alphalink = ctx->inputs[1];
81     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
82         av_log(ctx, AV_LOG_ERROR,
83                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
84                mainlink->w, mainlink->h,
85                alphalink->w, alphalink->h);
86         return AVERROR(EINVAL);
87     }
88
89     outlink->w = mainlink->w;
90     outlink->h = mainlink->h;
91     outlink->time_base = mainlink->time_base;
92     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
93     outlink->frame_rate = mainlink->frame_rate;
94     return 0;
95 }
96
97 static void draw_frame(AVFilterContext *ctx,
98                        AVFrame *main_buf,
99                        AVFrame *alpha_buf)
100 {
101     AlphaMergeContext *merge = ctx->priv;
102     int h = main_buf->height;
103
104     if (merge->is_packed_rgb) {
105         int x, y;
106         uint8_t *pin, *pout;
107         for (y = 0; y < h; y++) {
108             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
109             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
110             for (x = 0; x < main_buf->width; x++) {
111                 *pout = *pin;
112                 pin += 1;
113                 pout += 4;
114             }
115         }
116     } else {
117         int y;
118         const int main_linesize = main_buf->linesize[A];
119         const int alpha_linesize = alpha_buf->linesize[Y];
120         for (y = 0; y < h && y < alpha_buf->height; y++) {
121             memcpy(main_buf->data[A] + y * main_linesize,
122                    alpha_buf->data[Y] + y * alpha_linesize,
123                    FFMIN(main_linesize, alpha_linesize));
124         }
125     }
126 }
127
128 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
129 {
130     AVFilterContext *ctx = inlink->dst;
131     AlphaMergeContext *merge = ctx->priv;
132
133     int ret = 0;
134     int is_alpha = (inlink == ctx->inputs[1]);
135     struct FFBufQueue *queue =
136         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
137     ff_bufqueue_add(ctx, queue, buf);
138
139     do {
140         AVFrame *main_buf, *alpha_buf;
141
142         if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
143             !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
144
145         main_buf = ff_bufqueue_get(&merge->queue_main);
146         alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
147
148         draw_frame(ctx, main_buf, alpha_buf);
149         ret = ff_filter_frame(ctx->outputs[0], main_buf);
150         av_frame_free(&alpha_buf);
151     } while (ret >= 0);
152     return ret;
153 }
154
155 static int request_frame(AVFilterLink *outlink)
156 {
157     AVFilterContext *ctx = outlink->src;
158     AlphaMergeContext *merge = ctx->priv;
159     int in, ret;
160
161     /* TODO reindent */
162         in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
163         ret = ff_request_frame(ctx->inputs[in]);
164         if (ret < 0)
165             return ret;
166     return 0;
167 }
168
169 static const AVFilterPad alphamerge_inputs[] = {
170     {
171         .name             = "main",
172         .type             = AVMEDIA_TYPE_VIDEO,
173         .config_props     = config_input_main,
174         .filter_frame     = filter_frame,
175         .needs_writable   = 1,
176     },{
177         .name             = "alpha",
178         .type             = AVMEDIA_TYPE_VIDEO,
179         .filter_frame     = filter_frame,
180     },
181     { NULL }
182 };
183
184 static const AVFilterPad alphamerge_outputs[] = {
185     {
186         .name          = "default",
187         .type          = AVMEDIA_TYPE_VIDEO,
188         .config_props  = config_output,
189         .request_frame = request_frame,
190     },
191     { NULL }
192 };
193
194 AVFilter ff_vf_alphamerge = {
195     .name           = "alphamerge",
196     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
197                       "input into the alpha channel of the first input."),
198     .uninit         = uninit,
199     .priv_size      = sizeof(AlphaMergeContext),
200     .query_formats  = query_formats,
201     .inputs         = alphamerge_inputs,
202     .outputs        = alphamerge_outputs,
203 };