FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_overlay.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 266 335 79.4%
Functions: 34 57 59.6%
Branches: 202 399 50.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * overlay one video on top of another
26 */
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "libavutil/common.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/timestamp.h"
38 #include "filters.h"
39 #include "drawutils.h"
40 #include "framesync.h"
41 #include "video.h"
42 #include "vf_overlay.h"
43
44 typedef struct ThreadData {
45 AVFrame *dst, *src;
46 } ThreadData;
47
48 static const char *const var_names[] = {
49 "main_w", "W", ///< width of the main video
50 "main_h", "H", ///< height of the main video
51 "overlay_w", "w", ///< width of the overlay video
52 "overlay_h", "h", ///< height of the overlay video
53 "hsub",
54 "vsub",
55 "x",
56 "y",
57 "n", ///< number of frame
58 "t", ///< timestamp expressed in seconds
59 NULL
60 };
61
62 #define MAIN 0
63 #define OVERLAY 1
64
65 #define R 0
66 #define G 1
67 #define B 2
68 #define A 3
69
70 #define Y 0
71 #define U 1
72 #define V 2
73
74 enum EvalMode {
75 EVAL_MODE_INIT,
76 EVAL_MODE_FRAME,
77 EVAL_MODE_NB
78 };
79
80 46 static av_cold void uninit(AVFilterContext *ctx)
81 {
82 46 OverlayContext *s = ctx->priv;
83
84 46 ff_framesync_uninit(&s->fs);
85 46 av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
86 46 av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
87 46 }
88
89 1470 static inline int normalize_xy(double d, int chroma_sub)
90 {
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470 times.
1470 if (isnan(d))
92 return INT_MAX;
93 1470 return (int)d & ~((1 << chroma_sub) - 1);
94 }
95
96 735 static void eval_expr(AVFilterContext *ctx)
97 {
98 735 OverlayContext *s = ctx->priv;
99
100 735 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
101 735 s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
102 /* It is necessary if x is expressed from y */
103 735 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
104 735 s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
105 735 s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
106 735 }
107
108 46 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
109 {
110 int ret;
111 46 AVExpr *old = NULL;
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (*pexpr)
114 old = *pexpr;
115 46 ret = av_expr_parse(pexpr, expr, var_names,
116 NULL, NULL, NULL, NULL, 0, log_ctx);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0) {
118 av_log(log_ctx, AV_LOG_ERROR,
119 "Error when evaluating the expression '%s' for %s\n",
120 expr, option);
121 *pexpr = old;
122 return ret;
123 }
124
125 46 av_expr_free(old);
126 46 return 0;
127 }
128
129 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
130 char *res, int res_len, int flags)
131 {
132 OverlayContext *s = ctx->priv;
133 int ret;
134
135 if (!strcmp(cmd, "x"))
136 ret = set_expr(&s->x_pexpr, args, cmd, ctx);
137 else if (!strcmp(cmd, "y"))
138 ret = set_expr(&s->y_pexpr, args, cmd, ctx);
139 else
140 ret = AVERROR(ENOSYS);
141
142 if (ret < 0)
143 return ret;
144
145 if (s->eval_mode == EVAL_MODE_INIT) {
146 eval_expr(ctx);
147 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
148 s->var_values[VAR_X], s->x,
149 s->var_values[VAR_Y], s->y);
150 }
151 return ret;
152 }
153
154 static const enum AVPixelFormat alpha_pix_fmts[] = {
155 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
156 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
157 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
158 AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
159 };
160
161 23 static int query_formats(const AVFilterContext *ctx,
162 AVFilterFormatsConfig **cfg_in,
163 AVFilterFormatsConfig **cfg_out)
164 {
165 23 const OverlayContext *s = ctx->priv;
166
167 /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
168 static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
169 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
170 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
171 AV_PIX_FMT_NONE
172 };
173 static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
174 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
175 };
176
177 static const enum AVPixelFormat main_pix_fmts_yuv420p10[] = {
178 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
179 AV_PIX_FMT_NONE
180 };
181 static const enum AVPixelFormat overlay_pix_fmts_yuv420p10[] = {
182 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_NONE
183 };
184
185 static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
186 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
187 };
188 static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
189 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
190 };
191
192 static const enum AVPixelFormat main_pix_fmts_yuv422p10[] = {
193 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
194 };
195 static const enum AVPixelFormat overlay_pix_fmts_yuv422p10[] = {
196 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
197 };
198
199 static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
200 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
201 };
202 static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
203 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
204 };
205
206 static const enum AVPixelFormat main_pix_fmts_yuv444p10[] = {
207 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
208 };
209 static const enum AVPixelFormat overlay_pix_fmts_yuv444p10[] = {
210 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
211 };
212
213 static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
214 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
215 };
216 static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
217 AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
218 };
219
220 static const enum AVPixelFormat main_pix_fmts_rgb[] = {
221 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
222 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
223 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
224 AV_PIX_FMT_NONE
225 };
226 static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
227 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
228 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
229 AV_PIX_FMT_NONE
230 };
231
232 const enum AVPixelFormat *main_formats, *overlay_formats;
233 AVFilterFormats *formats;
234 int ret;
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->alpha_mode != AVALPHA_MODE_UNSPECIFIED) {
237 formats = ff_make_formats_list_singleton(s->alpha_mode);
238 ret = ff_formats_ref(formats, &cfg_in[OVERLAY]->alpha_modes);
239 if (ret < 0)
240 return ret;
241 }
242
243
8/10
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
23 switch (s->format) {
244 9 case OVERLAY_FORMAT_YUV420:
245 9 main_formats = main_pix_fmts_yuv420;
246 9 overlay_formats = overlay_pix_fmts_yuv420;
247 9 break;
248 1 case OVERLAY_FORMAT_YUV420P10:
249 1 main_formats = main_pix_fmts_yuv420p10;
250 1 overlay_formats = overlay_pix_fmts_yuv420p10;
251 1 break;
252 3 case OVERLAY_FORMAT_YUV422:
253 3 main_formats = main_pix_fmts_yuv422;
254 3 overlay_formats = overlay_pix_fmts_yuv422;
255 3 break;
256 1 case OVERLAY_FORMAT_YUV422P10:
257 1 main_formats = main_pix_fmts_yuv422p10;
258 1 overlay_formats = overlay_pix_fmts_yuv422p10;
259 1 break;
260 3 case OVERLAY_FORMAT_YUV444:
261 3 main_formats = main_pix_fmts_yuv444;
262 3 overlay_formats = overlay_pix_fmts_yuv444;
263 3 break;
264 1 case OVERLAY_FORMAT_YUV444P10:
265 1 main_formats = main_pix_fmts_yuv444p10;
266 1 overlay_formats = overlay_pix_fmts_yuv444p10;
267 1 break;
268 3 case OVERLAY_FORMAT_RGB:
269 3 main_formats = main_pix_fmts_rgb;
270 3 overlay_formats = overlay_pix_fmts_rgb;
271 3 break;
272 2 case OVERLAY_FORMAT_GBRP:
273 2 main_formats = main_pix_fmts_gbrp;
274 2 overlay_formats = overlay_pix_fmts_gbrp;
275 2 break;
276 case OVERLAY_FORMAT_AUTO:
277 return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, alpha_pix_fmts);
278 default:
279 av_assert0(0);
280 }
281
282 23 formats = ff_make_format_list(main_formats);
283
2/4
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
46 if ((ret = ff_formats_ref(formats, &cfg_in[MAIN]->formats)) < 0 ||
284 23 (ret = ff_formats_ref(formats, &cfg_out[MAIN]->formats)) < 0)
285 return ret;
286
287 23 return ff_formats_ref(ff_make_format_list(overlay_formats),
288 23 &cfg_in[OVERLAY]->formats);
289 }
290
291 23 static int config_input_overlay(AVFilterLink *inlink)
292 {
293 23 AVFilterContext *ctx = inlink->dst;
294 23 OverlayContext *s = inlink->dst->priv;
295 int ret;
296 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
297
298 23 av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
299
300 /* Finish the configuration by evaluating the expressions
301 now when both inputs are configured. */
302 23 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
303 23 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
304 23 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
305 23 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
306 23 s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
307 23 s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
308 23 s->var_values[VAR_X] = NAN;
309 23 s->var_values[VAR_Y] = NAN;
310 23 s->var_values[VAR_N] = 0;
311 23 s->var_values[VAR_T] = NAN;
312
313
2/4
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
46 if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
314 23 (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
315 return ret;
316
317 23 s->overlay_is_packed_rgb =
318 23 ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
319 23 s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
320
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->eval_mode == EVAL_MODE_INIT) {
322 eval_expr(ctx);
323 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
324 s->var_values[VAR_X], s->x,
325 s->var_values[VAR_Y], s->y);
326 }
327
328 69 av_log(ctx, AV_LOG_VERBOSE,
329 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
330 23 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
331 23 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
332 23 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
333 23 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
334 23 return 0;
335 }
336
337 23 static int config_output(AVFilterLink *outlink)
338 {
339 23 AVFilterContext *ctx = outlink->src;
340 23 OverlayContext *s = ctx->priv;
341 int ret;
342
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
344 return ret;
345
346 23 outlink->w = ctx->inputs[MAIN]->w;
347 23 outlink->h = ctx->inputs[MAIN]->h;
348 23 outlink->time_base = ctx->inputs[MAIN]->time_base;
349
350 23 return ff_framesync_configure(&s->fs);
351 }
352
353 // divide by 255 and round to nearest
354 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
355 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
356
357 // calculate the unpremultiplied alpha, applying the general equation:
358 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
359 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
360 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
361 // this is only needed when blending onto straight alpha main images
362 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
363
364 #define PTR_ADD(TYPE, ptr, byte_addend) ((TYPE*)((uint8_t*)ptr + (byte_addend)))
365 #define CPTR_ADD(TYPE, ptr, byte_addend) ((const TYPE*)((const uint8_t*)ptr + (byte_addend)))
366
367 /**
368 * Blend image in src to destination buffer dst at position (x, y).
369 */
370
371 486 static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
372 AVFrame *dst, const AVFrame *src,
373 int main_has_alpha, int x, int y,
374 int overlay_straight, int main_straight,
375 int jobnr, int nb_jobs)
376 {
377 486 OverlayContext *s = ctx->priv;
378 int i, imax, j, jmax;
379 486 const int src_w = src->width;
380 486 const int src_h = src->height;
381 486 const int dst_w = dst->width;
382 486 const int dst_h = dst->height;
383 uint8_t alpha; ///< the amount of overlay to blend on to main
384 486 const int dr = s->main_rgba_map[R];
385 486 const int dg = s->main_rgba_map[G];
386 486 const int db = s->main_rgba_map[B];
387 486 const int da = s->main_rgba_map[A];
388 486 const int dstep = s->main_pix_step[0];
389 486 const int sr = s->overlay_rgba_map[R];
390 486 const int sg = s->overlay_rgba_map[G];
391 486 const int sb = s->overlay_rgba_map[B];
392 486 const int sa = s->overlay_rgba_map[A];
393 486 const int sstep = s->overlay_pix_step[0];
394 int slice_start, slice_end;
395 uint8_t *S, *sp, *d, *dp;
396
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 486 times.
486 i = FFMAX(-y, 0);
398 486 imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
399
400 486 slice_start = i + (imax * jobnr) / nb_jobs;
401 486 slice_end = i + (imax * (jobnr+1)) / nb_jobs;
402
403 486 sp = src->data[0] + (slice_start) * src->linesize[0];
404 486 dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
405
406
2/2
✓ Branch 0 taken 4512 times.
✓ Branch 1 taken 486 times.
4998 for (i = slice_start; i < slice_end; i++) {
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4512 times.
4512 j = FFMAX(-x, 0);
408 4512 S = sp + j * sstep;
409 4512 d = dp + (x+j) * dstep;
410
411
2/2
✓ Branch 0 taken 449536 times.
✓ Branch 1 taken 4512 times.
454048 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
412 449536 alpha = S[sa];
413
414 // if the main channel has an alpha channel, alpha has to be calculated
415 // to create an un-premultiplied (straight) alpha value
416
6/6
✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 416768 times.
✓ Branch 2 taken 32576 times.
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 31658 times.
✓ Branch 5 taken 918 times.
449536 if (main_straight && alpha != 0 && alpha != 255) {
417 31658 uint8_t alpha_d = d[da];
418 31658 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
419 }
420
421
3/3
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 385836 times.
✓ Branch 2 taken 63316 times.
449536 switch (alpha) {
422 384 case 0:
423 384 break;
424 385836 case 255:
425 385836 d[dr] = S[sr];
426 385836 d[dg] = S[sg];
427 385836 d[db] = S[sb];
428 385836 break;
429 63316 default:
430 // main_value = main_value * (1 - alpha) + overlay_value * alpha
431 // since alpha is in the range 0-255, the result must divided by 255
432
1/2
✓ Branch 0 taken 63316 times.
✗ Branch 1 not taken.
63316 d[dr] = overlay_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
433 FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
434
1/2
✓ Branch 0 taken 63316 times.
✗ Branch 1 not taken.
63316 d[dg] = overlay_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
435 FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
436
1/2
✓ Branch 0 taken 63316 times.
✗ Branch 1 not taken.
63316 d[db] = overlay_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
437 FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
438 }
439
2/2
✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 416768 times.
449536 if (main_has_alpha) {
440
3/3
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 918 times.
✓ Branch 2 taken 31658 times.
32768 switch (alpha) {
441 192 case 0:
442 192 break;
443 918 case 255:
444 918 d[da] = S[sa];
445 918 break;
446 31658 default:
447 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
448 31658 d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
449 }
450 }
451 449536 d += dstep;
452 449536 S += sstep;
453 }
454 4512 dp += dst->linesize[0];
455 4512 sp += src->linesize[0];
456 }
457 486 }
458
459 #define DEFINE_BLEND_PLANE(depth, T, nbits) \
460 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
461 AVFrame *dst, const AVFrame *src, \
462 int src_w, int src_h, \
463 int dst_w, int dst_h, \
464 int i, int hsub, int vsub, \
465 int x, int y, \
466 int main_straight, \
467 int dst_plane, \
468 int dst_offset, \
469 int dst_step, \
470 int overlay_straight, \
471 int yuv, \
472 int jobnr, \
473 int nb_jobs) \
474 { \
475 OverlayContext *octx = ctx->priv; \
476 int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
477 int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
478 int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
479 int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
480 int yp = y>>vsub; \
481 int xp = x>>hsub; \
482 const T max = (1 << nbits) - 1; \
483 const T mid = (1 << (nbits - 1)); \
484 \
485 const int jmin = FFMAX(-yp, 0), jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
486 const int kmin = FFMAX(-xp, 0), kmax = FFMIN(-xp + dst_wp, src_wp); \
487 const int slice_start = jmin + (jmax * jobnr) / nb_jobs; \
488 const int slice_end = jmin + (jmax * (jobnr + 1)) / nb_jobs; \
489 \
490 const uint8_t *sp = src->data[i] + (slice_start) * src->linesize[i]; \
491 uint8_t *dp = dst->data[dst_plane] \
492 + (yp + slice_start) * dst->linesize[dst_plane] \
493 + dst_offset; \
494 const uint8_t *ap = src->data[3] + (slice_start << vsub) * src->linesize[3]; \
495 const uint8_t *dap = main_straight ? dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3] : NULL; \
496 \
497 for (int j = slice_start; j < slice_end; ++j) { \
498 int k = kmin; \
499 const T *s = (const T *)sp + k; \
500 const T *a = (const T *)ap + (k << hsub); \
501 const T *da = main_straight ? (T *)dap + ((xp + k) << hsub) : NULL; \
502 T *d = (T *)(dp + (xp + k) * dst_step); \
503 \
504 if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
505 int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
506 (uint8_t*)a, kmax - k, src->linesize[3]); \
507 \
508 s += c; \
509 d = PTR_ADD(T, d, dst_step * c); \
510 if (main_straight) \
511 da += (1 << hsub) * c; \
512 a += (1 << hsub) * c; \
513 k += c; \
514 } \
515 for (; k < kmax; k++) { \
516 int alpha_v, alpha_h, alpha; \
517 \
518 /* average alpha for color components, improve quality */ \
519 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
520 const T *next_line = CPTR_ADD(T, a, src->linesize[3]); \
521 alpha = (a[0] + next_line[0] + \
522 a[1] + next_line[1]) >> 2; \
523 } else if (hsub || vsub) { \
524 alpha_h = hsub && k+1 < src_wp ? \
525 (a[0] + a[1]) >> 1 : a[0]; \
526 alpha_v = vsub && j+1 < src_hp ? \
527 (a[0] + *CPTR_ADD(T, a, src->linesize[3])) >> 1 : a[0]; \
528 alpha = (alpha_v + alpha_h) >> 1; \
529 } else \
530 alpha = a[0]; \
531 /* if the main channel has an alpha channel, alpha has to be calculated */ \
532 /* to create an un-premultiplied (straight) alpha value */ \
533 if (main_straight && alpha != 0 && alpha != max) { \
534 /* average alpha for color components, improve quality */ \
535 uint8_t alpha_d; \
536 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
537 const T *next_line = CPTR_ADD(T, da, dst->linesize[3]); \
538 alpha_d = (da[0] + next_line[0] + \
539 da[1] + next_line[1]) >> 2; \
540 } else if (hsub || vsub) { \
541 alpha_h = hsub && k+1 < src_wp ? \
542 (da[0] + da[1]) >> 1 : da[0]; \
543 alpha_v = vsub && j+1 < src_hp ? \
544 (da[0] + *CPTR_ADD(T, da, dst->linesize[3])) >> 1 : da[0]; \
545 alpha_d = (alpha_v + alpha_h) >> 1; \
546 } else \
547 alpha_d = da[0]; \
548 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
549 } \
550 if (overlay_straight) { \
551 if (nbits > 8) \
552 *d = (*d * (max - alpha) + *s * alpha) / max; \
553 else \
554 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
555 } else { \
556 if (nbits > 8) { \
557 if (i && yuv) \
558 *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
559 else \
560 *d = av_clip_uintp2((*d * (max - alpha) + *s * alpha) / max + *s - (16<<(nbits-8)),\
561 nbits);\
562 } else { \
563 if (i && yuv) \
564 *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
565 else \
566 *d = av_clip_uint8(FAST_DIV255(*d * (255 - alpha)) + *s - 16); \
567 } \
568 } \
569 s++; \
570 d = PTR_ADD(T, d, dst_step); \
571 if (main_straight) \
572 da += 1 << hsub; \
573 a += 1 << hsub; \
574 } \
575 dp += dst->linesize[dst_plane]; \
576 sp += src->linesize[i]; \
577 ap += (1 << vsub) * src->linesize[3]; \
578 if (main_straight) \
579 dap += (1 << vsub) * dst->linesize[3]; \
580 } \
581 }
582
61/78
✗ Branch 0 not taken.
✓ Branch 1 taken 18063 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18063 times.
✓ Branch 4 taken 2916 times.
✓ Branch 5 taken 15147 times.
✓ Branch 6 taken 26816 times.
✓ Branch 7 taken 365768 times.
✓ Branch 8 taken 181988 times.
✓ Branch 9 taken 210596 times.
✓ Branch 10 taken 1114 times.
✓ Branch 11 taken 180874 times.
✓ Branch 12 taken 210596 times.
✓ Branch 13 taken 1114 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 391470 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 60578852 times.
✓ Branch 20 taken 122253384 times.
✓ Branch 21 taken 60129316 times.
✓ Branch 22 taken 449536 times.
✓ Branch 23 taken 59862848 times.
✓ Branch 24 taken 266468 times.
✓ Branch 25 taken 59681974 times.
✓ Branch 26 taken 180874 times.
✓ Branch 27 taken 122253384 times.
✓ Branch 28 taken 896878 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 122253384 times.
✓ Branch 31 taken 896878 times.
✗ Branch 32 not taken.
✓ Branch 33 taken 705866 times.
✓ Branch 34 taken 191012 times.
✓ Branch 35 taken 447342 times.
✓ Branch 36 taken 449536 times.
✓ Branch 37 taken 180874 times.
✓ Branch 38 taken 266468 times.
✓ Branch 39 taken 2231296 times.
✓ Branch 40 taken 180600940 times.
✓ Branch 41 taken 2229296 times.
✓ Branch 42 taken 2000 times.
✓ Branch 43 taken 299536 times.
✓ Branch 44 taken 1929760 times.
✓ Branch 45 taken 47152 times.
✓ Branch 46 taken 252384 times.
✓ Branch 47 taken 15792 times.
✓ Branch 48 taken 31360 times.
✓ Branch 49 taken 15548 times.
✓ Branch 50 taken 244 times.
✓ Branch 51 taken 15548 times.
✗ Branch 52 not taken.
✓ Branch 53 taken 252384 times.
✓ Branch 54 taken 31604 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 252384 times.
✓ Branch 57 taken 31604 times.
✗ Branch 58 not taken.
✓ Branch 59 taken 31604 times.
✗ Branch 60 not taken.
✓ Branch 61 taken 244 times.
✓ Branch 62 taken 31360 times.
✗ Branch 63 not taken.
✓ Branch 64 taken 244 times.
✓ Branch 65 taken 182832236 times.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✓ Branch 71 taken 2231296 times.
✓ Branch 72 taken 180600940 times.
✓ Branch 73 taken 182832236 times.
✓ Branch 74 taken 392584 times.
✓ Branch 75 taken 26816 times.
✓ Branch 76 taken 365768 times.
✓ Branch 77 taken 392584 times.
✓ Branch 78 taken 18063 times.
183242883 DEFINE_BLEND_PLANE(8, uint8_t, 8)
583
30/68
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 324 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 324 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2560 times.
✓ Branch 8 taken 46080 times.
✓ Branch 9 taken 153600 times.
✓ Branch 10 taken 15360 times.
✓ Branch 11 taken 30720 times.
✓ Branch 12 taken 14976 times.
✓ Branch 13 taken 384 times.
✓ Branch 14 taken 14664 times.
✓ Branch 15 taken 312 times.
✓ Branch 16 taken 153600 times.
✓ Branch 17 taken 31416 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 153600 times.
✓ Branch 20 taken 31416 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 30456 times.
✓ Branch 23 taken 960 times.
✓ Branch 24 taken 696 times.
✓ Branch 25 taken 30720 times.
✓ Branch 26 taken 312 times.
✓ Branch 27 taken 384 times.
✗ Branch 28 not taken.
✓ Branch 29 taken 199680 times.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✓ Branch 54 taken 199680 times.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✓ Branch 61 taken 199680 times.
✓ Branch 62 taken 199680 times.
✓ Branch 63 taken 2560 times.
✗ Branch 64 not taken.
✓ Branch 65 taken 2560 times.
✓ Branch 66 taken 2560 times.
✓ Branch 67 taken 324 times.
202564 DEFINE_BLEND_PLANE(16, uint16_t, 10)
584
585 #define DEFINE_ALPHA_COMPOSITE(depth, T, nbits) \
586 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
587 int src_w, int src_h, \
588 int dst_w, int dst_h, \
589 int x, int y, int main_straight, \
590 int jobnr, int nb_jobs) \
591 { \
592 T alpha; /* the amount of overlay to blend on to main */ \
593 const T max = (1 << nbits) - 1; \
594 \
595 const int imin = FFMAX(-y, 0), imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h); \
596 const int jmin = FFMAX(-x, 0), jmax = FFMIN(-x + dst_w, src_w); \
597 const int slice_start = imin + ( imax * jobnr) / nb_jobs; \
598 const int slice_end = imin + ((imax * (jobnr + 1)) / nb_jobs); \
599 \
600 const uint8_t *sa = src->data[3] + (slice_start) * src->linesize[3]; \
601 uint8_t *da = dst->data[3] + (y + slice_start) * dst->linesize[3]; \
602 \
603 for (int i = slice_start; i < slice_end; ++i) { \
604 const T *s = (const T *)sa + jmin; \
605 T *d = (T *)da + x + jmin; \
606 \
607 for (int j = jmin; j < jmax; ++j) { \
608 alpha = *s; \
609 if (main_straight && alpha != 0 && alpha != max) { \
610 uint8_t alpha_d = *d; \
611 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
612 } \
613 if (alpha == max) \
614 *d = *s; \
615 else if (alpha > 0) { \
616 /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
617 if (nbits > 8) \
618 *d += (max - *d) * *s / max; \
619 else \
620 *d += FAST_DIV255((max - *d) * *s); \
621 } \
622 d += 1; \
623 s += 1; \
624 } \
625 da += dst->linesize[3]; \
626 sa += src->linesize[3]; \
627 } \
628 }
629
15/18
✗ Branch 0 not taken.
✓ Branch 1 taken 972 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 972 times.
✓ Branch 4 taken 899072 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 898304 times.
✓ Branch 7 taken 768 times.
✓ Branch 8 taken 126104 times.
✓ Branch 9 taken 772200 times.
✓ Branch 10 taken 772200 times.
✓ Branch 11 taken 126872 times.
✓ Branch 12 taken 126104 times.
✓ Branch 13 taken 768 times.
✓ Branch 14 taken 899072 times.
✓ Branch 15 taken 9024 times.
✓ Branch 16 taken 9024 times.
✓ Branch 17 taken 972 times.
909068 DEFINE_ALPHA_COMPOSITE(8, uint8_t, 8)
630 DEFINE_ALPHA_COMPOSITE(16, uint16_t, 10)
631
632 #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
633 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
634 AVFrame *dst, const AVFrame *src, \
635 int hsub, int vsub, \
636 int main_straight, \
637 int x, int y, \
638 int overlay_straight, \
639 int jobnr, int nb_jobs) \
640 { \
641 OverlayContext *s = ctx->priv; \
642 const int src_w = src->width; \
643 const int src_h = src->height; \
644 const int dst_w = dst->width; \
645 const int dst_h = dst->height; \
646 \
647 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
648 x, y, main_straight, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
649 s->main_desc->comp[0].step, overlay_straight, 1, jobnr, nb_jobs); \
650 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
651 x, y, main_straight, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
652 s->main_desc->comp[1].step, overlay_straight, 1, jobnr, nb_jobs); \
653 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
654 x, y, main_straight, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
655 s->main_desc->comp[2].step, overlay_straight, 1, jobnr, nb_jobs); \
656 \
657 if (s->main_has_alpha) \
658 alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, main_straight, \
659 jobnr, nb_jobs); \
660 }
661
2/2
✓ Branch 3 taken 954 times.
✓ Branch 4 taken 5031 times.
5985 DEFINE_BLEND_SLICE_YUV(8, 8)
662
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
108 DEFINE_BLEND_SLICE_YUV(16, 10)
663
664 36 static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
665 AVFrame *dst, const AVFrame *src,
666 int hsub, int vsub,
667 int main_straight,
668 int x, int y,
669 int overlay_straight,
670 int jobnr,
671 int nb_jobs)
672 {
673 36 OverlayContext *s = ctx->priv;
674 36 const int src_w = src->width;
675 36 const int src_h = src->height;
676 36 const int dst_w = dst->width;
677 36 const int dst_h = dst->height;
678
679 36 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_straight,
680 36 s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, overlay_straight, 0,
681 jobnr, nb_jobs);
682 36 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_straight,
683 36 s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, overlay_straight, 0,
684 jobnr, nb_jobs);
685 36 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_straight,
686 36 s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, overlay_straight, 0,
687 jobnr, nb_jobs);
688
689
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 if (s->main_has_alpha)
690 18 alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, main_straight, jobnr, nb_jobs);
691 36 }
692
693 #define DEFINE_BLEND_SLICE_PLANAR_FMT_(format_, blend_slice_fn_suffix_, hsub_, vsub_, main_straight_, overlay_straight_) \
694 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
695 { \
696 OverlayContext *s = ctx->priv; \
697 ThreadData *td = arg; \
698 blend_slice_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
699 hsub_, vsub_, main_straight_, \
700 s->x, s->y, overlay_straight_, \
701 jobnr, nb_jobs); \
702 return 0; \
703 }
704
705 #define DEFINE_BLEND_SLICE_PLANAR_FMT(format_, blend_slice_fn_suffix_, hsub_, vsub_) \
706 DEFINE_BLEND_SLICE_PLANAR_FMT_(format_ ## _ss, blend_slice_fn_suffix_, hsub_, vsub_, 1, 1) \
707 DEFINE_BLEND_SLICE_PLANAR_FMT_(format_ ## _sp, blend_slice_fn_suffix_, hsub_, vsub_, 1, 0) \
708 DEFINE_BLEND_SLICE_PLANAR_FMT_(format_ ## _ps, blend_slice_fn_suffix_, hsub_, vsub_, 0, 1) \
709 DEFINE_BLEND_SLICE_PLANAR_FMT_(format_ ## _pp, blend_slice_fn_suffix_, hsub_, vsub_, 0, 0)
710
711 // FMT FN H V
712 10026 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420, yuv_8_8bits, 1, 1)
713 72 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420p10, yuv_16_10bits, 1, 1)
714 72 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422p10, yuv_16_10bits, 1, 0)
715 972 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422, yuv_8_8bits, 1, 0)
716 972 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444, yuv_8_8bits, 0, 0)
717 72 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444p10, yuv_16_10bits, 0, 0)
718 72 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp, planar_rgb, 0, 0)
719
720 #define DEFINE_BLEND_SLICE_PACKED_FMT(format_, blend_slice_fn_suffix_, main_has_alpha_, main_straight_, overlay_straight_) \
721 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
722 { \
723 OverlayContext *s = ctx->priv; \
724 ThreadData *td = arg; \
725 blend_slice_packed_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
726 main_has_alpha_, \
727 s->x, s->y, \
728 overlay_straight_, \
729 main_straight_, \
730 jobnr, nb_jobs); \
731 return 0; \
732 }
733
734 // FMT FN A MS OS
735 468 DEFINE_BLEND_SLICE_PACKED_FMT(rgb, rgb, 0, 0, 1)
736 DEFINE_BLEND_SLICE_PACKED_FMT(rgb_pm, rgb, 0, 0, 0)
737 18 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_ss, rgb, 1, 1, 1)
738 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_sp, rgb, 1, 1, 0)
739 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_ps, rgb, 1, 0, 1)
740 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_pp, rgb, 1, 0, 0)
741
742 23 static int config_input_main(AVFilterLink *inlink)
743 {
744 23 OverlayContext *s = inlink->dst->priv;
745 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
746
747 23 av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
748
749 23 s->hsub = pix_desc->log2_chroma_w;
750 23 s->vsub = pix_desc->log2_chroma_h;
751
752 23 s->main_desc = pix_desc;
753
754 23 s->main_is_packed_rgb =
755 23 ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
756 23 s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
757 23 return 0;
758 }
759
760 735 static int init_slice_fn(AVFilterContext *ctx)
761 {
762 735 OverlayContext *s = ctx->priv;
763 735 const AVFilterLink *main = ctx->inputs[MAIN];
764 735 const AVFilterLink *overlay = ctx->inputs[OVERLAY];
765
3/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 625 times.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
735 const int main_straight = s->main_has_alpha && main->alpha_mode != AVALPHA_MODE_PREMULTIPLIED;
766 735 const int overlay_straight = overlay->alpha_mode != AVALPHA_MODE_PREMULTIPLIED;
767
768 #define ASSIGN_BLEND_SLICE(format_) \
769 do { \
770 s->blend_slice = main_straight ? (overlay_straight ? format_##_ss : format_##_sp) \
771 : (overlay_straight ? format_##_ps : format_##_pp); \
772 } while (0)
773
774
8/10
✓ Branch 0 taken 557 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 54 times.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
735 switch (s->format) {
775 557 case OVERLAY_FORMAT_YUV420:
776
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 555 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 555 times.
✗ Branch 5 not taken.
557 ASSIGN_BLEND_SLICE(blend_slice_yuv420);
777 557 break;
778 4 case OVERLAY_FORMAT_YUV420P10:
779
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 ASSIGN_BLEND_SLICE(blend_slice_yuv420p10);
780 4 break;
781 54 case OVERLAY_FORMAT_YUV422:
782
4/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
54 ASSIGN_BLEND_SLICE(blend_slice_yuv422);
783 54 break;
784 4 case OVERLAY_FORMAT_YUV422P10:
785
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 ASSIGN_BLEND_SLICE(blend_slice_yuv422p10);
786 4 break;
787 54 case OVERLAY_FORMAT_YUV444:
788
4/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
54 ASSIGN_BLEND_SLICE(blend_slice_yuv444);
789 54 break;
790 4 case OVERLAY_FORMAT_YUV444P10:
791
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 ASSIGN_BLEND_SLICE(blend_slice_yuv444p10);
792 4 break;
793 54 case OVERLAY_FORMAT_RGB:
794
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 if (s->main_has_alpha)
795
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 ASSIGN_BLEND_SLICE(blend_slice_rgba);
796 else
797
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 s->blend_slice = overlay_straight ? blend_slice_rgb : blend_slice_rgb_pm;
798 54 break;
799 4 case OVERLAY_FORMAT_GBRP:
800
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 ASSIGN_BLEND_SLICE(blend_slice_gbrp);
801 4 break;
802 case OVERLAY_FORMAT_AUTO:
803 switch (main->format) {
804 case AV_PIX_FMT_YUVA420P:
805 ASSIGN_BLEND_SLICE(blend_slice_yuv420);
806 break;
807 case AV_PIX_FMT_YUVA420P10:
808 ASSIGN_BLEND_SLICE(blend_slice_yuv420p10);
809 break;
810 case AV_PIX_FMT_YUVA422P:
811 ASSIGN_BLEND_SLICE(blend_slice_yuv422);
812 break;
813 case AV_PIX_FMT_YUVA422P10:
814 ASSIGN_BLEND_SLICE(blend_slice_yuv422p10);
815 break;
816 case AV_PIX_FMT_YUVA444P:
817 ASSIGN_BLEND_SLICE(blend_slice_yuv444);
818 break;
819 case AV_PIX_FMT_YUVA444P10:
820 ASSIGN_BLEND_SLICE(blend_slice_yuv444p10);
821 break;
822 case AV_PIX_FMT_ARGB:
823 case AV_PIX_FMT_RGBA:
824 case AV_PIX_FMT_BGRA:
825 case AV_PIX_FMT_ABGR:
826 ASSIGN_BLEND_SLICE(blend_slice_rgba);
827 break;
828 case AV_PIX_FMT_GBRAP:
829 ASSIGN_BLEND_SLICE(blend_slice_gbrp);
830 break;
831 default:
832 av_unreachable("Invalid pixel format for overlay");
833 break;
834 }
835 break;
836 }
837
838 #if ARCH_X86
839 735 ff_overlay_init_x86(ctx);
840 #endif
841
842 735 return 0;
843 }
844
845 738 static int do_blend(FFFrameSync *fs)
846 {
847 738 AVFilterContext *ctx = fs->parent;
848 AVFrame *mainpic, *second;
849 738 OverlayContext *s = ctx->priv;
850 738 AVFilterLink *inlink = ctx->inputs[0];
851 738 FilterLink *inl = ff_filter_link(inlink);
852 int ret;
853
854 738 ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 738 times.
738 if (ret < 0)
856 return ret;
857
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 735 times.
738 if (!second)
858 3 return ff_filter_frame(ctx->outputs[0], mainpic);
859
860
1/2
✓ Branch 0 taken 735 times.
✗ Branch 1 not taken.
735 if (s->eval_mode == EVAL_MODE_FRAME) {
861
862 735 s->var_values[VAR_N] = inl->frame_count_out;
863 1470 s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
864
1/2
✓ Branch 0 taken 735 times.
✗ Branch 1 not taken.
735 NAN : mainpic->pts * av_q2d(inlink->time_base);
865
866 735 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
867 735 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
868 735 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
869 735 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
870
871 735 eval_expr(ctx);
872 735 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f x:%f xi:%d y:%f yi:%d\n",
873 s->var_values[VAR_N], s->var_values[VAR_T],
874 s->var_values[VAR_X], s->x,
875 s->var_values[VAR_Y], s->y);
876 }
877
878
2/4
✓ Branch 0 taken 735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 735 times.
✗ Branch 3 not taken.
735 if (s->x < mainpic->width && s->x + second->width >= 0 &&
879
2/4
✓ Branch 0 taken 735 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 735 times.
✗ Branch 3 not taken.
735 s->y < mainpic->height && s->y + second->height >= 0) {
880 ThreadData td;
881
882 735 init_slice_fn(ctx);
883
884 735 td.dst = mainpic;
885 735 td.src = second;
886
1/2
✓ Branch 0 taken 735 times.
✗ Branch 1 not taken.
735 ff_filter_execute(ctx, s->blend_slice, &td, NULL, FFMIN(FFMAX(1, FFMIN3(s->y + second->height, FFMIN(second->height, mainpic->height), mainpic->height - s->y)),
887 ff_filter_get_nb_threads(ctx)));
888 }
889 735 return ff_filter_frame(ctx->outputs[0], mainpic);
890 }
891
892 46 static av_cold int init(AVFilterContext *ctx)
893 {
894 46 OverlayContext *s = ctx->priv;
895
896 46 s->fs.on_event = do_blend;
897 46 return 0;
898 }
899
900 2762 static int activate(AVFilterContext *ctx)
901 {
902 2762 OverlayContext *s = ctx->priv;
903 2762 return ff_framesync_activate(&s->fs);
904 }
905
906 #define OFFSET(x) offsetof(OverlayContext, x)
907 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
908 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
909
910 static const AVOption overlay_options[] = {
911 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
912 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
913 { "eof_action", "Action to take when encountering EOF from secondary input ",
914 OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
915 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, .unit = "eof_action" },
916 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, .unit = "eof_action" },
917 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, .unit = "eof_action" },
918 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, .unit = "eof_action" },
919 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" },
920 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
921 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
922 { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
923 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, .unit = "format" },
924 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
925 { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
926 { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
927 { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
928 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
929 { "yuv444p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444P10}, .flags = FLAGS, .unit = "format" },
930 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
931 { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
932 { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
933 { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
934 { "alpha", "alpha format", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=AVALPHA_MODE_UNSPECIFIED}, 0, AVALPHA_MODE_NB-1, FLAGS, .unit = "alpha_mode" },
935 { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, .flags = FLAGS, .unit = "alpha_mode" },
936 { "unknown", "", 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_UNSPECIFIED}, .flags = FLAGS, .unit = "alpha_mode" },
937 { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_STRAIGHT}, .flags = FLAGS, .unit = "alpha_mode" },
938 { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=AVALPHA_MODE_PREMULTIPLIED}, .flags = FLAGS, .unit = "alpha_mode" },
939 { NULL }
940 };
941
942
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 82 times.
420 FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
943
944 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
945 {
946 .name = "main",
947 .type = AVMEDIA_TYPE_VIDEO,
948 .config_props = config_input_main,
949 },
950 {
951 .name = "overlay",
952 .type = AVMEDIA_TYPE_VIDEO,
953 .config_props = config_input_overlay,
954 },
955 };
956
957 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
958 {
959 .name = "default",
960 .type = AVMEDIA_TYPE_VIDEO,
961 .config_props = config_output,
962 },
963 };
964
965 const FFFilter ff_vf_overlay = {
966 .p.name = "overlay",
967 .p.description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
968 .p.priv_class = &overlay_class,
969 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
970 AVFILTER_FLAG_SLICE_THREADS,
971 .preinit = overlay_framesync_preinit,
972 .init = init,
973 .uninit = uninit,
974 .priv_size = sizeof(OverlayContext),
975 .activate = activate,
976 .process_command = process_command,
977 FILTER_INPUTS(avfilter_vf_overlay_inputs),
978 FILTER_OUTPUTS(avfilter_vf_overlay_outputs),
979 FILTER_QUERY_FUNC2(query_formats),
980 };
981