Line | Branch | Exec | Source |
---|---|---|---|
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 "framesync.h" | ||
36 | #include "video.h" | ||
37 | |||
38 | enum { Y, U, V, A }; | ||
39 | |||
40 | typedef struct AlphaMergeContext { | ||
41 | const AVClass *class; | ||
42 | |||
43 | int is_packed_rgb; | ||
44 | uint8_t rgba_map[4]; | ||
45 | |||
46 | FFFrameSync fs; | ||
47 | } AlphaMergeContext; | ||
48 | |||
49 | 200 | static int do_alphamerge(FFFrameSync *fs) | |
50 | { | ||
51 | 200 | AVFilterContext *ctx = fs->parent; | |
52 | 200 | AVFilterLink *outlink = ctx->outputs[0]; | |
53 | 200 | AlphaMergeContext *s = ctx->priv; | |
54 | AVFrame *main_buf, *alpha_buf; | ||
55 | int ret; | ||
56 | |||
57 | 200 | ret = ff_framesync_dualinput_get_writable(fs, &main_buf, &alpha_buf); | |
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (ret < 0) |
59 | ✗ | return ret; | |
60 | 200 | main_buf->alpha_mode = outlink->alpha_mode; | |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (!alpha_buf) |
62 | ✗ | return ff_filter_frame(ctx->outputs[0], main_buf); | |
63 | |||
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | if (alpha_buf->color_range == AVCOL_RANGE_MPEG) { |
65 | ✗ | av_log(ctx, AV_LOG_WARNING, "alpha plane color range tagged as %s, " | |
66 | "output will be wrong!\n", | ||
67 | ✗ | av_color_range_name(alpha_buf->color_range)); | |
68 | } | ||
69 | |||
70 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 100 times.
|
200 | if (s->is_packed_rgb) { |
71 | int x, y; | ||
72 | uint8_t *pin, *pout; | ||
73 |
2/2✓ Branch 0 taken 28800 times.
✓ Branch 1 taken 100 times.
|
28900 | for (y = 0; y < main_buf->height; y++) { |
74 | 28800 | pin = alpha_buf->data[0] + y * alpha_buf->linesize[0]; | |
75 | 28800 | pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A]; | |
76 |
2/2✓ Branch 0 taken 10137600 times.
✓ Branch 1 taken 28800 times.
|
10166400 | for (x = 0; x < main_buf->width; x++) { |
77 | 10137600 | *pout = *pin; | |
78 | 10137600 | pin += 1; | |
79 | 10137600 | pout += 4; | |
80 | } | ||
81 | } | ||
82 | } else { | ||
83 | 100 | const int main_linesize = main_buf->linesize[A]; | |
84 | 100 | const int alpha_linesize = alpha_buf->linesize[Y]; | |
85 | 100 | av_image_copy_plane(main_buf->data[A], main_linesize, | |
86 | 100 | alpha_buf->data[Y], alpha_linesize, | |
87 | 100 | FFMIN(main_linesize, alpha_linesize), alpha_buf->height); | |
88 | } | ||
89 | |||
90 | 200 | return ff_filter_frame(ctx->outputs[0], main_buf); | |
91 | } | ||
92 | |||
93 | 8 | static av_cold int init(AVFilterContext *ctx) | |
94 | { | ||
95 | 8 | AlphaMergeContext *s = ctx->priv; | |
96 | |||
97 | 8 | s->fs.on_event = do_alphamerge; | |
98 | 8 | return 0; | |
99 | } | ||
100 | |||
101 | 4 | static int query_formats(const AVFilterContext *ctx, | |
102 | AVFilterFormatsConfig **cfg_in, | ||
103 | AVFilterFormatsConfig **cfg_out) | ||
104 | { | ||
105 | static const enum AVPixelFormat main_fmts[] = { | ||
106 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, | ||
107 | AV_PIX_FMT_GBRAP, | ||
108 | AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, | ||
109 | AV_PIX_FMT_NONE | ||
110 | }; | ||
111 | static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE }; | ||
112 | int ret; | ||
113 | |||
114 | 4 | ret = ff_formats_ref(ff_make_format_list(alpha_fmts), | |
115 | 4 | &cfg_in[1]->formats); | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
117 | ✗ | return ret; | |
118 | |||
119 | 4 | ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, main_fmts); | |
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
121 | ✗ | return ret; | |
122 | |||
123 | 4 | ret = ff_formats_ref(ff_make_formats_list_singleton(AVALPHA_MODE_STRAIGHT), | |
124 | 4 | &cfg_out[0]->alpha_modes); | |
125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
126 | ✗ | return ret; | |
127 | |||
128 | 4 | return 0; | |
129 | } | ||
130 | |||
131 | 4 | static int config_input_main(AVFilterLink *inlink) | |
132 | { | ||
133 | 4 | AlphaMergeContext *s = inlink->dst->priv; | |
134 | 4 | s->is_packed_rgb = | |
135 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
6 | ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 && |
136 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | inlink->format != AV_PIX_FMT_GBRAP; |
137 | 4 | return 0; | |
138 | } | ||
139 | |||
140 | 4 | static int config_output(AVFilterLink *outlink) | |
141 | { | ||
142 | 4 | FilterLink *outl = ff_filter_link(outlink); | |
143 | 4 | AVFilterContext *ctx = outlink->src; | |
144 | 4 | AlphaMergeContext *s = ctx->priv; | |
145 | 4 | AVFilterLink *mainlink = ctx->inputs[0]; | |
146 | 4 | FilterLink *ml = ff_filter_link(mainlink); | |
147 | 4 | AVFilterLink *alphalink = ctx->inputs[1]; | |
148 | int ret; | ||
149 | |||
150 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) { |
151 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
152 | "Input frame sizes do not match (%dx%d vs %dx%d).\n", | ||
153 | mainlink->w, mainlink->h, | ||
154 | alphalink->w, alphalink->h); | ||
155 | ✗ | return AVERROR(EINVAL); | |
156 | } | ||
157 | |||
158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0) |
159 | ✗ | return ret; | |
160 | |||
161 | 4 | outlink->w = mainlink->w; | |
162 | 4 | outlink->h = mainlink->h; | |
163 | 4 | outlink->time_base = mainlink->time_base; | |
164 | 4 | outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; | |
165 | 4 | outl->frame_rate = ml->frame_rate; | |
166 | |||
167 | 4 | return ff_framesync_configure(&s->fs); | |
168 | } | ||
169 | |||
170 | 606 | static int activate(AVFilterContext *ctx) | |
171 | { | ||
172 | 606 | AlphaMergeContext *s = ctx->priv; | |
173 | 606 | return ff_framesync_activate(&s->fs); | |
174 | } | ||
175 | |||
176 | 8 | static av_cold void uninit(AVFilterContext *ctx) | |
177 | { | ||
178 | 8 | AlphaMergeContext *s = ctx->priv; | |
179 | |||
180 | 8 | ff_framesync_uninit(&s->fs); | |
181 | 8 | } | |
182 | |||
183 | static const AVFilterPad alphamerge_inputs[] = { | ||
184 | { | ||
185 | .name = "main", | ||
186 | .type = AVMEDIA_TYPE_VIDEO, | ||
187 | .config_props = config_input_main, | ||
188 | },{ | ||
189 | .name = "alpha", | ||
190 | .type = AVMEDIA_TYPE_VIDEO, | ||
191 | }, | ||
192 | }; | ||
193 | |||
194 | static const AVFilterPad alphamerge_outputs[] = { | ||
195 | { | ||
196 | .name = "default", | ||
197 | .type = AVMEDIA_TYPE_VIDEO, | ||
198 | .config_props = config_output, | ||
199 | }, | ||
200 | }; | ||
201 | |||
202 | static const AVOption alphamerge_options[] = { | ||
203 | { NULL } | ||
204 | }; | ||
205 | |||
206 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
16 | FRAMESYNC_DEFINE_CLASS(alphamerge, AlphaMergeContext, fs); |
207 | |||
208 | const FFFilter ff_vf_alphamerge = { | ||
209 | .p.name = "alphamerge", | ||
210 | .p.description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second " | ||
211 | "input into the alpha channel of the first input."), | ||
212 | .p.priv_class = &alphamerge_class, | ||
213 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, | ||
214 | .preinit = alphamerge_framesync_preinit, | ||
215 | .priv_size = sizeof(AlphaMergeContext), | ||
216 | .init = init, | ||
217 | FILTER_INPUTS(alphamerge_inputs), | ||
218 | FILTER_OUTPUTS(alphamerge_outputs), | ||
219 | FILTER_QUERY_FUNC2(query_formats), | ||
220 | .uninit = uninit, | ||
221 | .activate = activate, | ||
222 | }; | ||
223 |