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/imgutils.h"
29 #include "libavutil/pixfmt.h"
31 #include "drawutils.h"
39 typedef struct AlphaMergeContext {
46 static int query_formats(AVFilterContext *ctx)
48 static const enum AVPixelFormat main_fmts[] = {
49 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
51 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
54 static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
55 AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
58 if (!(main_formats = ff_make_format_list(main_fmts)) ||
59 !(alpha_formats = ff_make_format_list(alpha_fmts))) {
60 ret = AVERROR(ENOMEM);
63 if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
64 (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
65 (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
70 av_freep(&main_formats->formats);
71 av_freep(&main_formats);
73 av_freep(&alpha_formats->formats);
74 av_freep(&alpha_formats);
78 static int config_input_main(AVFilterLink *inlink)
80 AlphaMergeContext *merge = inlink->dst->priv;
81 merge->is_packed_rgb =
82 ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0 &&
83 inlink->format != AV_PIX_FMT_GBRAP;
87 static int config_output(AVFilterLink *outlink)
89 AVFilterContext *ctx = outlink->src;
90 AVFilterLink *mainlink = ctx->inputs[0];
91 AVFilterLink *alphalink = ctx->inputs[1];
92 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
93 av_log(ctx, AV_LOG_ERROR,
94 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
95 mainlink->w, mainlink->h,
96 alphalink->w, alphalink->h);
97 return AVERROR(EINVAL);
100 outlink->w = mainlink->w;
101 outlink->h = mainlink->h;
102 outlink->time_base = mainlink->time_base;
103 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
104 outlink->frame_rate = mainlink->frame_rate;
108 static void draw_frame(AVFilterContext *ctx,
112 AlphaMergeContext *merge = ctx->priv;
113 int h = main_buf->height;
115 if (merge->is_packed_rgb) {
118 for (y = 0; y < h; y++) {
119 pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
120 pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
121 for (x = 0; x < main_buf->width; x++) {
128 const int main_linesize = main_buf->linesize[A];
129 const int alpha_linesize = alpha_buf->linesize[Y];
130 av_image_copy_plane(main_buf->data[A], main_linesize,
131 alpha_buf->data[Y], alpha_linesize,
132 FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
136 static int activate(AVFilterContext *ctx)
138 AVFilterLink *outlink = ctx->outputs[0];
139 AlphaMergeContext *s = ctx->priv;
142 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
144 if (!s->main_frame) {
145 ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
150 if (!s->alpha_frame) {
151 ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
156 if (s->main_frame && s->alpha_frame) {
157 draw_frame(ctx, s->main_frame, s->alpha_frame);
158 ret = ff_filter_frame(outlink, s->main_frame);
159 av_frame_free(&s->alpha_frame);
160 s->main_frame = NULL;
164 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
165 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
167 if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
168 !ff_outlink_get_status(ctx->inputs[0]) &&
170 ff_inlink_request_frame(ctx->inputs[0]);
174 if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
175 !ff_outlink_get_status(ctx->inputs[1]) &&
177 ff_inlink_request_frame(ctx->inputs[1]);
181 return FFERROR_NOT_READY;
184 static const AVFilterPad alphamerge_inputs[] = {
187 .type = AVMEDIA_TYPE_VIDEO,
188 .config_props = config_input_main,
192 .type = AVMEDIA_TYPE_VIDEO,
197 static const AVFilterPad alphamerge_outputs[] = {
200 .type = AVMEDIA_TYPE_VIDEO,
201 .config_props = config_output,
206 AVFilter ff_vf_alphamerge = {
207 .name = "alphamerge",
208 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
209 "input into the alpha channel of the first input."),
210 .priv_size = sizeof(AlphaMergeContext),
211 .query_formats = query_formats,
212 .inputs = alphamerge_inputs,
213 .outputs = alphamerge_outputs,
214 .activate = activate,