FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_alphamerge.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 58 66 87.9%
Functions: 8 9 88.9%
Branches: 17 28 60.7%

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