]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphamerge.c
Merge commit '588b6215b4c74945994eb9636b0699028c069ed2'
[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, *alpha_formats;
61     int ret;
62
63     if (!(main_formats = ff_make_format_list(main_fmts)) ||
64         !(alpha_formats = ff_make_format_list(alpha_fmts)))
65         return AVERROR(ENOMEM);
66     if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
67         (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
68         (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
69         return ret;
70     return 0;
71 }
72
73 static int config_input_main(AVFilterLink *inlink)
74 {
75     AlphaMergeContext *merge = inlink->dst->priv;
76     merge->is_packed_rgb =
77         ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
78     return 0;
79 }
80
81 static int config_output(AVFilterLink *outlink)
82 {
83     AVFilterContext *ctx = outlink->src;
84     AVFilterLink *mainlink = ctx->inputs[0];
85     AVFilterLink *alphalink = ctx->inputs[1];
86     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
87         av_log(ctx, AV_LOG_ERROR,
88                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
89                mainlink->w, mainlink->h,
90                alphalink->w, alphalink->h);
91         return AVERROR(EINVAL);
92     }
93
94     outlink->w = mainlink->w;
95     outlink->h = mainlink->h;
96     outlink->time_base = mainlink->time_base;
97     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
98     outlink->frame_rate = mainlink->frame_rate;
99     return 0;
100 }
101
102 static void draw_frame(AVFilterContext *ctx,
103                        AVFrame *main_buf,
104                        AVFrame *alpha_buf)
105 {
106     AlphaMergeContext *merge = ctx->priv;
107     int h = main_buf->height;
108
109     if (merge->is_packed_rgb) {
110         int x, y;
111         uint8_t *pin, *pout;
112         for (y = 0; y < h; y++) {
113             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
114             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
115             for (x = 0; x < main_buf->width; x++) {
116                 *pout = *pin;
117                 pin += 1;
118                 pout += 4;
119             }
120         }
121     } else {
122         int y;
123         const int main_linesize = main_buf->linesize[A];
124         const int alpha_linesize = alpha_buf->linesize[Y];
125         for (y = 0; y < h && y < alpha_buf->height; y++) {
126             memcpy(main_buf->data[A] + y * main_linesize,
127                    alpha_buf->data[Y] + y * alpha_linesize,
128                    FFMIN(main_linesize, alpha_linesize));
129         }
130     }
131 }
132
133 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
134 {
135     AVFilterContext *ctx = inlink->dst;
136     AlphaMergeContext *merge = ctx->priv;
137
138     int ret = 0;
139     int is_alpha = (inlink == ctx->inputs[1]);
140     struct FFBufQueue *queue =
141         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
142     ff_bufqueue_add(ctx, queue, buf);
143
144     do {
145         AVFrame *main_buf, *alpha_buf;
146
147         if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
148             !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
149
150         main_buf = ff_bufqueue_get(&merge->queue_main);
151         alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
152
153         draw_frame(ctx, main_buf, alpha_buf);
154         ret = ff_filter_frame(ctx->outputs[0], main_buf);
155         av_frame_free(&alpha_buf);
156     } while (ret >= 0);
157     return ret;
158 }
159
160 static int request_frame(AVFilterLink *outlink)
161 {
162     AVFilterContext *ctx = outlink->src;
163     AlphaMergeContext *merge = ctx->priv;
164     int in, ret;
165
166     in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
167     ret = ff_request_frame(ctx->inputs[in]);
168     if (ret < 0)
169         return ret;
170     return 0;
171 }
172
173 static const AVFilterPad alphamerge_inputs[] = {
174     {
175         .name             = "main",
176         .type             = AVMEDIA_TYPE_VIDEO,
177         .config_props     = config_input_main,
178         .filter_frame     = filter_frame,
179         .needs_writable   = 1,
180     },{
181         .name             = "alpha",
182         .type             = AVMEDIA_TYPE_VIDEO,
183         .filter_frame     = filter_frame,
184     },
185     { NULL }
186 };
187
188 static const AVFilterPad alphamerge_outputs[] = {
189     {
190         .name          = "default",
191         .type          = AVMEDIA_TYPE_VIDEO,
192         .config_props  = config_output,
193         .request_frame = request_frame,
194     },
195     { NULL }
196 };
197
198 AVFilter ff_vf_alphamerge = {
199     .name           = "alphamerge",
200     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
201                       "input into the alpha channel of the first input."),
202     .uninit         = uninit,
203     .priv_size      = sizeof(AlphaMergeContext),
204     .query_formats  = query_formats,
205     .inputs         = alphamerge_inputs,
206     .outputs        = alphamerge_outputs,
207 };