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"
42 struct FFBufQueue queue_main;
43 struct FFBufQueue queue_alpha;
46 static av_cold void uninit(AVFilterContext *ctx)
48 AlphaMergeContext *merge = ctx->priv;
49 ff_bufqueue_discard_all(&merge->queue_main);
50 ff_bufqueue_discard_all(&merge->queue_alpha);
53 static int query_formats(AVFilterContext *ctx)
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,
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);
69 static int config_input_main(AVFilterLink *inlink)
71 AlphaMergeContext *merge = inlink->dst->priv;
72 merge->is_packed_rgb =
73 ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
77 static int config_output(AVFilterLink *outlink)
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);
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;
98 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) {return 0;}
99 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) {return 0;}
101 static void draw_frame(AVFilterContext *ctx,
102 AVFilterBufferRef *main_buf,
103 AVFilterBufferRef *alpha_buf)
105 AlphaMergeContext *merge = ctx->priv;
106 int h = main_buf->video->h;
108 if (merge->is_packed_rgb) {
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++) {
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));
130 ff_draw_slice(ctx->outputs[0], 0, h, 1);
133 static int end_frame(AVFilterLink *inlink)
135 AVFilterContext *ctx = inlink->dst;
136 AlphaMergeContext *merge = ctx->priv;
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;
145 AVFilterBufferRef *main_buf, *alpha_buf;
147 if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
148 !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
150 main_buf = ff_bufqueue_get(&merge->queue_main);
151 alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
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);
163 static int request_frame(AVFilterLink *outlink)
165 AVFilterContext *ctx = outlink->src;
166 AlphaMergeContext *merge = ctx->priv;
169 merge->frame_requested = 1;
170 while (merge->frame_requested) {
171 in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
172 ret = ff_request_frame(ctx->inputs[in]);
179 AVFilter avfilter_vf_alphamerge = {
180 .name = "alphamerge",
181 .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
182 "input into the alpha channel of the first input."),
184 .priv_size = sizeof(AlphaMergeContext),
185 .query_formats = query_formats,
187 .inputs = (const AVFilterPad[]) {
189 .type = AVMEDIA_TYPE_VIDEO,
190 .config_props = config_input_main,
191 .get_video_buffer = ff_null_get_video_buffer,
192 .start_frame = start_frame,
193 .draw_slice = draw_slice,
194 .end_frame = end_frame,
195 .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE },
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 | AV_PERM_PRESERVE },
204 .outputs = (const AVFilterPad[]) {
206 .type = AVMEDIA_TYPE_VIDEO,
207 .config_props = config_output,
208 .request_frame = request_frame },