]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_alphamerge.c
avfilter/vf_alphamerge: add timeline support
[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/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixfmt.h"
31 #include "avfilter.h"
32 #include "drawutils.h"
33 #include "formats.h"
34 #include "filters.h"
35 #include "internal.h"
36 #include "video.h"
37
38 enum { Y, U, V, A };
39
40 typedef struct AlphaMergeContext {
41     int is_packed_rgb;
42     uint8_t rgba_map[4];
43     AVFrame *main_frame;
44     AVFrame *alpha_frame;
45 } AlphaMergeContext;
46
47 static int query_formats(AVFilterContext *ctx)
48 {
49     static const enum AVPixelFormat main_fmts[] = {
50         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
51         AV_PIX_FMT_GBRAP,
52         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
53         AV_PIX_FMT_NONE
54     };
55     static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
56     AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
57     int ret;
58
59     if (!(main_formats = ff_make_format_list(main_fmts)) ||
60         !(alpha_formats = ff_make_format_list(alpha_fmts))) {
61             ret = AVERROR(ENOMEM);
62             goto fail;
63         }
64     if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
65         (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
66         (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
67             goto fail;
68     return 0;
69 fail:
70     if (main_formats)
71         av_freep(&main_formats->formats);
72     av_freep(&main_formats);
73     if (alpha_formats)
74         av_freep(&alpha_formats->formats);
75     av_freep(&alpha_formats);
76     return ret;
77 }
78
79 static int config_input_main(AVFilterLink *inlink)
80 {
81     AlphaMergeContext *s = inlink->dst->priv;
82     s->is_packed_rgb =
83         ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
84         inlink->format != AV_PIX_FMT_GBRAP;
85     return 0;
86 }
87
88 static int config_output(AVFilterLink *outlink)
89 {
90     AVFilterContext *ctx = outlink->src;
91     AVFilterLink *mainlink = ctx->inputs[0];
92     AVFilterLink *alphalink = ctx->inputs[1];
93     if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
94         av_log(ctx, AV_LOG_ERROR,
95                "Input frame sizes do not match (%dx%d vs %dx%d).\n",
96                mainlink->w, mainlink->h,
97                alphalink->w, alphalink->h);
98         return AVERROR(EINVAL);
99     }
100
101     outlink->w = mainlink->w;
102     outlink->h = mainlink->h;
103     outlink->time_base = mainlink->time_base;
104     outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
105     outlink->frame_rate = mainlink->frame_rate;
106     return 0;
107 }
108
109 static void draw_frame(AVFilterContext *ctx,
110                        AVFrame *main_buf,
111                        AVFrame *alpha_buf)
112 {
113     AlphaMergeContext *s = ctx->priv;
114     int h = main_buf->height;
115
116     if (s->is_packed_rgb) {
117         int x, y;
118         uint8_t *pin, *pout;
119         for (y = 0; y < h; y++) {
120             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
121             pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
122             for (x = 0; x < main_buf->width; x++) {
123                 *pout = *pin;
124                 pin += 1;
125                 pout += 4;
126             }
127         }
128     } else {
129         const int main_linesize = main_buf->linesize[A];
130         const int alpha_linesize = alpha_buf->linesize[Y];
131         av_image_copy_plane(main_buf->data[A], main_linesize,
132                             alpha_buf->data[Y], alpha_linesize,
133                             FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
134     }
135 }
136
137 static int activate(AVFilterContext *ctx)
138 {
139     AVFilterLink *outlink = ctx->outputs[0];
140     AlphaMergeContext *s = ctx->priv;
141     int ret;
142
143     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
144
145     if (!s->main_frame) {
146         ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
147         if (ret < 0)
148             return ret;
149     }
150
151     if (!s->alpha_frame) {
152         ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
153         if (ret < 0)
154             return ret;
155     }
156
157     if (s->main_frame && s->alpha_frame) {
158         if (!ctx->is_disabled)
159             draw_frame(ctx, s->main_frame, s->alpha_frame);
160         ret = ff_filter_frame(outlink, s->main_frame);
161         av_frame_free(&s->alpha_frame);
162         s->main_frame = NULL;
163         return ret;
164     }
165
166     FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
167     FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
168
169     if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
170         !ff_outlink_get_status(ctx->inputs[0]) &&
171         !s->main_frame) {
172         ff_inlink_request_frame(ctx->inputs[0]);
173         return 0;
174     }
175
176     if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
177         !ff_outlink_get_status(ctx->inputs[1]) &&
178         !s->alpha_frame) {
179         ff_inlink_request_frame(ctx->inputs[1]);
180         return 0;
181     }
182
183     return FFERROR_NOT_READY;
184 }
185
186 static const AVFilterPad alphamerge_inputs[] = {
187     {
188         .name             = "main",
189         .type             = AVMEDIA_TYPE_VIDEO,
190         .config_props     = config_input_main,
191         .needs_writable   = 1,
192     },{
193         .name             = "alpha",
194         .type             = AVMEDIA_TYPE_VIDEO,
195     },
196     { NULL }
197 };
198
199 static const AVFilterPad alphamerge_outputs[] = {
200     {
201         .name          = "default",
202         .type          = AVMEDIA_TYPE_VIDEO,
203         .config_props  = config_output,
204     },
205     { NULL }
206 };
207
208 static const AVOption alphamerge_options[] = {
209     { NULL }
210 };
211
212 AVFILTER_DEFINE_CLASS(alphamerge);
213
214 AVFilter ff_vf_alphamerge = {
215     .name           = "alphamerge",
216     .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
217                       "input into the alpha channel of the first input."),
218     .priv_size      = sizeof(AlphaMergeContext),
219     .priv_class     = &alphamerge_class,
220     .query_formats  = query_formats,
221     .inputs         = alphamerge_inputs,
222     .outputs        = alphamerge_outputs,
223     .activate       = activate,
224     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
225 };