]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphamerge.c
overlay: clear cur_buf on main input link.
[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 frame_requested;
40     int is_packed_rgb;
41     uint8_t rgba_map[4];
42     struct FFBufQueue queue_main;
43     struct FFBufQueue queue_alpha;
44 } AlphaMergeContext;
45
46 static av_cold void uninit(AVFilterContext *ctx)
47 {
48     AlphaMergeContext *merge = ctx->priv;
49     ff_bufqueue_discard_all(&merge->queue_main);
50     ff_bufqueue_discard_all(&merge->queue_alpha);
51 }
52
53 static int query_formats(AVFilterContext *ctx)
54 {
55     enum PixelFormat main_fmts[] = {
56         PIX_FMT_YUVA444P, PIX_FMT_YUVA422P, PIX_FMT_YUVA420P,
57         PIX_FMT_RGBA, PIX_FMT_BGRA, PIX_FMT_ARGB, PIX_FMT_ABGR,
58         PIX_FMT_NONE
59     };
60     enum PixelFormat alpha_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
61     AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
62     AVFilterFormats *alpha_formats = ff_make_format_list(alpha_fmts);
63     ff_formats_ref(main_formats, &ctx->inputs[0]->out_formats);
64     ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats);
65     ff_formats_ref(main_formats, &ctx->outputs[0]->in_formats);
66     return 0;
67 }
68
69 static int config_input_main(AVFilterLink *inlink)
70 {
71     AlphaMergeContext *merge = inlink->dst->priv;
72     merge->is_packed_rgb =
73         ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
74     return 0;
75 }
76
77 static int config_output(AVFilterLink *outlink)
78 {
79     AVFilterContext *ctx = outlink->src;
80     AVFilterLink *mainlink = ctx->inputs[0];
81     AVFilterLink *alphalink = ctx->inputs[1];
82     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
83         av_log(ctx, AV_LOG_ERROR,
84                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
85                mainlink->w, mainlink->h,
86                alphalink->w, alphalink->h);
87         return AVERROR(EINVAL);
88     }
89
90     outlink->w = mainlink->w;
91     outlink->h = mainlink->h;
92     outlink->time_base = mainlink->time_base;
93     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
94     outlink->frame_rate = mainlink->frame_rate;
95     return 0;
96 }
97
98 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) {}
99 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) {}
100
101 static void draw_frame(AVFilterContext *ctx,
102                        AVFilterBufferRef *main_buf,
103                        AVFilterBufferRef *alpha_buf)
104 {
105     AlphaMergeContext *merge = ctx->priv;
106     int h = main_buf->video->h;
107
108     if (merge->is_packed_rgb) {
109         int x, y;
110         uint8_t *pin, *pout;
111         for (y = 0; y < h; y++) {
112             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
113             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
114             for (x = 0; x < main_buf->video->w; x++) {
115                 *pout = *pin;
116                 pin += 1;
117                 pout += 4;
118             }
119         }
120     } else {
121         int y;
122         const int main_linesize = main_buf->linesize[A];
123         const int alpha_linesize = alpha_buf->linesize[Y];
124         for (y = 0; y < h && y < alpha_buf->video->h; y++) {
125             memcpy(main_buf->data[A] + y * main_linesize,
126                    alpha_buf->data[Y] + y * alpha_linesize,
127                    FFMIN(main_linesize, alpha_linesize));
128         }
129     }
130     ff_draw_slice(ctx->outputs[0], 0, h, 1);
131 }
132
133 static void end_frame(AVFilterLink *inlink)
134 {
135     AVFilterContext *ctx = inlink->dst;
136     AlphaMergeContext *merge = ctx->priv;
137
138     int is_alpha = (inlink == ctx->inputs[1]);
139     struct FFBufQueue *queue =
140         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
141     ff_bufqueue_add(ctx, queue, inlink->cur_buf);
142     inlink->cur_buf = NULL;
143
144     while (1) {
145         AVFilterBufferRef *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         ctx->outputs[0]->out_buf = main_buf;
154         ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(main_buf, ~0));
155         merge->frame_requested = 0;
156         draw_frame(ctx, main_buf, alpha_buf);
157         ff_end_frame(ctx->outputs[0]);
158         avfilter_unref_buffer(alpha_buf);
159     }
160 }
161
162 static int request_frame(AVFilterLink *outlink)
163 {
164     AVFilterContext *ctx = outlink->src;
165     AlphaMergeContext *merge = ctx->priv;
166     int in, ret;
167
168     merge->frame_requested = 1;
169     while (merge->frame_requested) {
170         in = ff_bufqueue_peek(&merge->queue_main, 0) ? 0 : 1;
171         ret = ff_request_frame(ctx->inputs[in]);
172         if (ret < 0)
173             return ret;
174     }
175     return 0;
176 }
177
178 AVFilter avfilter_vf_alphamerge = {
179     .name           = "alphamerge",
180     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
181                       "input into the alpha channel of the first input."),
182     .uninit         = uninit,
183     .priv_size      = sizeof(AlphaMergeContext),
184     .query_formats  = query_formats,
185
186     .inputs    = (const AVFilterPad[]) {
187         { .name             = "main",
188           .type             = AVMEDIA_TYPE_VIDEO,
189           .config_props     = config_input_main,
190           .get_video_buffer = ff_null_get_video_buffer,
191           .start_frame      = start_frame,
192           .draw_slice       = draw_slice,
193           .end_frame        = end_frame,
194           .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
195           .rej_perms        = AV_PERM_REUSE2 | AV_PERM_PRESERVE },
196         { .name             = "alpha",
197           .type             = AVMEDIA_TYPE_VIDEO,
198           .start_frame      = start_frame,
199           .draw_slice       = draw_slice,
200           .end_frame        = end_frame,
201           .min_perms        = AV_PERM_READ,
202           .rej_perms        = AV_PERM_REUSE2 },
203         { .name = NULL }
204     },
205     .outputs   = (const AVFilterPad[]) {
206       { .name               = "default",
207         .type               = AVMEDIA_TYPE_VIDEO,
208         .config_props       = config_output,
209         .request_frame      = request_frame },
210       { .name = NULL }
211     },
212 };