2 * Copyright (c) 2012 Steven Robertson
4 * This file is part of FFmpeg.
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.
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.
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
23 * copy an alpha component from another video's luma
28 #include "libavutil/pixfmt.h"
30 #include "bufferqueue.h"
31 #include "drawutils.h"
41 struct FFBufQueue queue_main;
42 struct FFBufQueue queue_alpha;
45 static av_cold void uninit(AVFilterContext *ctx)
47 AlphaMergeContext *merge = ctx->priv;
48 ff_bufqueue_discard_all(&merge->queue_main);
49 ff_bufqueue_discard_all(&merge->queue_alpha);
52 static int query_formats(AVFilterContext *ctx)
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,
59 static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
60 AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
63 if (!(main_formats = ff_make_format_list(main_fmts)) ||
64 !(alpha_formats = ff_make_format_list(alpha_fmts))) {
65 ret = AVERROR(ENOMEM);
68 if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
69 (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
70 (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
75 av_freep(&main_formats->formats);
76 av_freep(&main_formats);
78 av_freep(&alpha_formats->formats);
79 av_freep(&alpha_formats);
83 static int config_input_main(AVFilterLink *inlink)
85 AlphaMergeContext *merge = inlink->dst->priv;
86 merge->is_packed_rgb =
87 ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
91 static int config_output(AVFilterLink *outlink)
93 AVFilterContext *ctx = outlink->src;
94 AVFilterLink *mainlink = ctx->inputs[0];
95 AVFilterLink *alphalink = ctx->inputs[1];
96 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
97 av_log(ctx, AV_LOG_ERROR,
98 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
99 mainlink->w, mainlink->h,
100 alphalink->w, alphalink->h);
101 return AVERROR(EINVAL);
104 outlink->w = mainlink->w;
105 outlink->h = mainlink->h;
106 outlink->time_base = mainlink->time_base;
107 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
108 outlink->frame_rate = mainlink->frame_rate;
112 static void draw_frame(AVFilterContext *ctx,
116 AlphaMergeContext *merge = ctx->priv;
117 int h = main_buf->height;
119 if (merge->is_packed_rgb) {
122 for (y = 0; y < h; y++) {
123 pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
124 pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
125 for (x = 0; x < main_buf->width; x++) {
133 const int main_linesize = main_buf->linesize[A];
134 const int alpha_linesize = alpha_buf->linesize[Y];
135 for (y = 0; y < h && y < alpha_buf->height; y++) {
136 memcpy(main_buf->data[A] + y * main_linesize,
137 alpha_buf->data[Y] + y * alpha_linesize,
138 FFMIN(main_linesize, alpha_linesize));
143 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
145 AVFilterContext *ctx = inlink->dst;
146 AlphaMergeContext *merge = ctx->priv;
149 int is_alpha = (inlink == ctx->inputs[1]);
150 struct FFBufQueue *queue =
151 (is_alpha ? &merge->queue_alpha : &merge->queue_main);
152 ff_bufqueue_add(ctx, queue, buf);
155 AVFrame *main_buf, *alpha_buf;
157 if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
158 !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
160 main_buf = ff_bufqueue_get(&merge->queue_main);
161 alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
163 draw_frame(ctx, main_buf, alpha_buf);
164 ret = ff_filter_frame(ctx->outputs[0], main_buf);
165 av_frame_free(&alpha_buf);
170 static int request_frame(AVFilterLink *outlink)
172 AVFilterContext *ctx = outlink->src;
173 AlphaMergeContext *merge = ctx->priv;
176 in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
177 ret = ff_request_frame(ctx->inputs[in]);
183 static const AVFilterPad alphamerge_inputs[] = {
186 .type = AVMEDIA_TYPE_VIDEO,
187 .config_props = config_input_main,
188 .filter_frame = filter_frame,
192 .type = AVMEDIA_TYPE_VIDEO,
193 .filter_frame = filter_frame,
198 static const AVFilterPad alphamerge_outputs[] = {
201 .type = AVMEDIA_TYPE_VIDEO,
202 .config_props = config_output,
203 .request_frame = request_frame,
208 AVFilter ff_vf_alphamerge = {
209 .name = "alphamerge",
210 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
211 "input into the alpha channel of the first input."),
213 .priv_size = sizeof(AlphaMergeContext),
214 .query_formats = query_formats,
215 .inputs = alphamerge_inputs,
216 .outputs = alphamerge_outputs,