FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_overlay.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 263 373 70.5%
Functions: 33 48 68.8%
Branches: 184 328 56.1%

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 1442 static inline int normalize_xy(double d, int chroma_sub)
90 {
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1442 times.
1442 if (isnan(d))
92 return INT_MAX;
93 1442 return (int)d & ~((1 << chroma_sub) - 1);
94 }
95
96 721 static void eval_expr(AVFilterContext *ctx)
97 {
98 721 OverlayContext *s = ctx->priv;
99
100 721 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
101 721 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 721 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
104 721 s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
105 721 s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
106 721 }
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
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) {
237 9 case OVERLAY_FORMAT_YUV420:
238 9 main_formats = main_pix_fmts_yuv420;
239 9 overlay_formats = overlay_pix_fmts_yuv420;
240 9 break;
241 1 case OVERLAY_FORMAT_YUV420P10:
242 1 main_formats = main_pix_fmts_yuv420p10;
243 1 overlay_formats = overlay_pix_fmts_yuv420p10;
244 1 break;
245 3 case OVERLAY_FORMAT_YUV422:
246 3 main_formats = main_pix_fmts_yuv422;
247 3 overlay_formats = overlay_pix_fmts_yuv422;
248 3 break;
249 1 case OVERLAY_FORMAT_YUV422P10:
250 1 main_formats = main_pix_fmts_yuv422p10;
251 1 overlay_formats = overlay_pix_fmts_yuv422p10;
252 1 break;
253 3 case OVERLAY_FORMAT_YUV444:
254 3 main_formats = main_pix_fmts_yuv444;
255 3 overlay_formats = overlay_pix_fmts_yuv444;
256 3 break;
257 1 case OVERLAY_FORMAT_YUV444P10:
258 1 main_formats = main_pix_fmts_yuv444p10;
259 1 overlay_formats = overlay_pix_fmts_yuv444p10;
260 1 break;
261 3 case OVERLAY_FORMAT_RGB:
262 3 main_formats = main_pix_fmts_rgb;
263 3 overlay_formats = overlay_pix_fmts_rgb;
264 3 break;
265 2 case OVERLAY_FORMAT_GBRP:
266 2 main_formats = main_pix_fmts_gbrp;
267 2 overlay_formats = overlay_pix_fmts_gbrp;
268 2 break;
269 case OVERLAY_FORMAT_AUTO:
270 return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, alpha_pix_fmts);
271 default:
272 av_assert0(0);
273 }
274
275 23 formats = ff_make_format_list(main_formats);
276
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 ||
277 23 (ret = ff_formats_ref(formats, &cfg_out[MAIN]->formats)) < 0)
278 return ret;
279
280 23 return ff_formats_ref(ff_make_format_list(overlay_formats),
281 23 &cfg_in[OVERLAY]->formats);
282 }
283
284 23 static int config_input_overlay(AVFilterLink *inlink)
285 {
286 23 AVFilterContext *ctx = inlink->dst;
287 23 OverlayContext *s = inlink->dst->priv;
288 int ret;
289 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
290
291 23 av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
292
293 /* Finish the configuration by evaluating the expressions
294 now when both inputs are configured. */
295 23 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
296 23 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
297 23 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
298 23 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
299 23 s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
300 23 s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
301 23 s->var_values[VAR_X] = NAN;
302 23 s->var_values[VAR_Y] = NAN;
303 23 s->var_values[VAR_N] = 0;
304 23 s->var_values[VAR_T] = NAN;
305
306
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 ||
307 23 (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
308 return ret;
309
310 23 s->overlay_is_packed_rgb =
311 23 ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
312 23 s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
313
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->eval_mode == EVAL_MODE_INIT) {
315 eval_expr(ctx);
316 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
317 s->var_values[VAR_X], s->x,
318 s->var_values[VAR_Y], s->y);
319 }
320
321 69 av_log(ctx, AV_LOG_VERBOSE,
322 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
323 23 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
324 23 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
325 23 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
326 23 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
327 23 return 0;
328 }
329
330 23 static int config_output(AVFilterLink *outlink)
331 {
332 23 AVFilterContext *ctx = outlink->src;
333 23 OverlayContext *s = ctx->priv;
334 int ret;
335
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
337 return ret;
338
339 23 outlink->w = ctx->inputs[MAIN]->w;
340 23 outlink->h = ctx->inputs[MAIN]->h;
341 23 outlink->time_base = ctx->inputs[MAIN]->time_base;
342
343 23 return ff_framesync_configure(&s->fs);
344 }
345
346 // divide by 255 and round to nearest
347 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
348 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
349
350 // calculate the unpremultiplied alpha, applying the general equation:
351 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
352 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
353 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
354 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
355
356 #define PTR_ADD(TYPE, ptr, byte_addend) ((TYPE*)((uint8_t*)ptr + (byte_addend)))
357 #define CPTR_ADD(TYPE, ptr, byte_addend) ((const TYPE*)((const uint8_t*)ptr + (byte_addend)))
358
359 /**
360 * Blend image in src to destination buffer dst at position (x, y).
361 */
362
363 468 static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
364 AVFrame *dst, const AVFrame *src,
365 int main_has_alpha, int x, int y,
366 int is_straight, int jobnr, int nb_jobs)
367 {
368 468 OverlayContext *s = ctx->priv;
369 int i, imax, j, jmax;
370 468 const int src_w = src->width;
371 468 const int src_h = src->height;
372 468 const int dst_w = dst->width;
373 468 const int dst_h = dst->height;
374 uint8_t alpha; ///< the amount of overlay to blend on to main
375 468 const int dr = s->main_rgba_map[R];
376 468 const int dg = s->main_rgba_map[G];
377 468 const int db = s->main_rgba_map[B];
378 468 const int da = s->main_rgba_map[A];
379 468 const int dstep = s->main_pix_step[0];
380 468 const int sr = s->overlay_rgba_map[R];
381 468 const int sg = s->overlay_rgba_map[G];
382 468 const int sb = s->overlay_rgba_map[B];
383 468 const int sa = s->overlay_rgba_map[A];
384 468 const int sstep = s->overlay_pix_step[0];
385 int slice_start, slice_end;
386 uint8_t *S, *sp, *d, *dp;
387
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 468 times.
468 i = FFMAX(-y, 0);
389 468 imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
390
391 468 slice_start = i + (imax * jobnr) / nb_jobs;
392 468 slice_end = i + (imax * (jobnr+1)) / nb_jobs;
393
394 468 sp = src->data[0] + (slice_start) * src->linesize[0];
395 468 dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
396
397
2/2
✓ Branch 0 taken 4256 times.
✓ Branch 1 taken 468 times.
4724 for (i = slice_start; i < slice_end; i++) {
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4256 times.
4256 j = FFMAX(-x, 0);
399 4256 S = sp + j * sstep;
400 4256 d = dp + (x+j) * dstep;
401
402
2/2
✓ Branch 0 taken 416768 times.
✓ Branch 1 taken 4256 times.
421024 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
403 416768 alpha = S[sa];
404
405 // if the main channel has an alpha channel, alpha has to be calculated
406 // to create an un-premultiplied (straight) alpha value
407
6/6
✓ Branch 0 taken 16384 times.
✓ Branch 1 taken 400384 times.
✓ Branch 2 taken 16288 times.
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 15829 times.
✓ Branch 5 taken 459 times.
416768 if (main_has_alpha && alpha != 0 && alpha != 255) {
408 15829 uint8_t alpha_d = d[da];
409 15829 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
410 }
411
412
3/3
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 384918 times.
✓ Branch 2 taken 31658 times.
416768 switch (alpha) {
413 192 case 0:
414 192 break;
415 384918 case 255:
416 384918 d[dr] = S[sr];
417 384918 d[dg] = S[sg];
418 384918 d[db] = S[sb];
419 384918 break;
420 31658 default:
421 // main_value = main_value * (1 - alpha) + overlay_value * alpha
422 // since alpha is in the range 0-255, the result must divided by 255
423
1/2
✓ Branch 0 taken 31658 times.
✗ Branch 1 not taken.
31658 d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
424 FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
425
1/2
✓ Branch 0 taken 31658 times.
✗ Branch 1 not taken.
31658 d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
426 FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
427
1/2
✓ Branch 0 taken 31658 times.
✗ Branch 1 not taken.
31658 d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
428 FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
429 }
430
2/2
✓ Branch 0 taken 16384 times.
✓ Branch 1 taken 400384 times.
416768 if (main_has_alpha) {
431
3/3
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 459 times.
✓ Branch 2 taken 15829 times.
16384 switch (alpha) {
432 96 case 0:
433 96 break;
434 459 case 255:
435 459 d[da] = S[sa];
436 459 break;
437 15829 default:
438 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
439 15829 d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
440 }
441 }
442 416768 d += dstep;
443 416768 S += sstep;
444 }
445 4256 dp += dst->linesize[0];
446 4256 sp += src->linesize[0];
447 }
448 468 }
449
450 #define DEFINE_BLEND_PLANE(depth, T, nbits) \
451 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
452 AVFrame *dst, const AVFrame *src, \
453 int src_w, int src_h, \
454 int dst_w, int dst_h, \
455 int i, int hsub, int vsub, \
456 int x, int y, \
457 int main_has_alpha, \
458 int dst_plane, \
459 int dst_offset, \
460 int dst_step, \
461 int straight, \
462 int yuv, \
463 int jobnr, \
464 int nb_jobs) \
465 { \
466 OverlayContext *octx = ctx->priv; \
467 int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
468 int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
469 int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
470 int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
471 int yp = y>>vsub; \
472 int xp = x>>hsub; \
473 const T max = (1 << nbits) - 1; \
474 const T mid = (1 << (nbits - 1)); \
475 \
476 const int jmin = FFMAX(-yp, 0), jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
477 const int kmin = FFMAX(-xp, 0), kmax = FFMIN(-xp + dst_wp, src_wp); \
478 const int slice_start = jmin + (jmax * jobnr) / nb_jobs; \
479 const int slice_end = jmin + (jmax * (jobnr + 1)) / nb_jobs; \
480 \
481 const uint8_t *sp = src->data[i] + (slice_start) * src->linesize[i]; \
482 uint8_t *dp = dst->data[dst_plane] \
483 + (yp + slice_start) * dst->linesize[dst_plane] \
484 + dst_offset; \
485 const uint8_t *ap = src->data[3] + (slice_start << vsub) * src->linesize[3]; \
486 const uint8_t *dap = main_has_alpha ? dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3] : NULL; \
487 \
488 for (int j = slice_start; j < slice_end; ++j) { \
489 int k = kmin; \
490 const T *s = (const T *)sp + k; \
491 const T *a = (const T *)ap + (k << hsub); \
492 const T *da = main_has_alpha ? (T *)dap + ((xp + k) << hsub) : NULL; \
493 T *d = (T *)(dp + (xp + k) * dst_step); \
494 \
495 if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
496 int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
497 (uint8_t*)a, kmax - k, src->linesize[3]); \
498 \
499 s += c; \
500 d = PTR_ADD(T, d, dst_step * c); \
501 if (main_has_alpha) \
502 da += (1 << hsub) * c; \
503 a += (1 << hsub) * c; \
504 k += c; \
505 } \
506 for (; k < kmax; k++) { \
507 int alpha_v, alpha_h, alpha; \
508 \
509 /* average alpha for color components, improve quality */ \
510 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
511 const T *next_line = CPTR_ADD(T, a, src->linesize[3]); \
512 alpha = (a[0] + next_line[0] + \
513 a[1] + next_line[1]) >> 2; \
514 } else if (hsub || vsub) { \
515 alpha_h = hsub && k+1 < src_wp ? \
516 (a[0] + a[1]) >> 1 : a[0]; \
517 alpha_v = vsub && j+1 < src_hp ? \
518 (a[0] + *CPTR_ADD(T, a, src->linesize[3])) >> 1 : a[0]; \
519 alpha = (alpha_v + alpha_h) >> 1; \
520 } else \
521 alpha = a[0]; \
522 /* if the main channel has an alpha channel, alpha has to be calculated */ \
523 /* to create an un-premultiplied (straight) alpha value */ \
524 if (main_has_alpha && alpha != 0 && alpha != max) { \
525 /* average alpha for color components, improve quality */ \
526 uint8_t alpha_d; \
527 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
528 const T *next_line = CPTR_ADD(T, da, dst->linesize[3]); \
529 alpha_d = (da[0] + next_line[0] + \
530 da[1] + next_line[1]) >> 2; \
531 } else if (hsub || vsub) { \
532 alpha_h = hsub && k+1 < src_wp ? \
533 (da[0] + da[1]) >> 1 : da[0]; \
534 alpha_v = vsub && j+1 < src_hp ? \
535 (da[0] + *CPTR_ADD(T, da, dst->linesize[3])) >> 1 : da[0]; \
536 alpha_d = (alpha_v + alpha_h) >> 1; \
537 } else \
538 alpha_d = da[0]; \
539 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
540 } \
541 if (straight) { \
542 if (nbits > 8) \
543 *d = (*d * (max - alpha) + *s * alpha) / max; \
544 else \
545 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
546 } else { \
547 if (nbits > 8) { \
548 if (i && yuv) \
549 *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
550 else \
551 *d = av_clip_uintp2((*d * (max - alpha) + *s * alpha) / max + *s - (16<<(nbits-8)),\
552 nbits);\
553 } else { \
554 if (i && yuv) \
555 *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
556 else \
557 *d = av_clip_uint8(FAST_DIV255(*d * (255 - alpha)) + *s - 16); \
558 } \
559 } \
560 s++; \
561 d = PTR_ADD(T, d, dst_step); \
562 if (main_has_alpha) \
563 da += 1 << hsub; \
564 a += 1 << hsub; \
565 } \
566 dp += dst->linesize[dst_plane]; \
567 sp += src->linesize[i]; \
568 ap += (1 << vsub) * src->linesize[3]; \
569 if (main_has_alpha) \
570 dap += (1 << vsub) * dst->linesize[3]; \
571 } \
572 }
573
61/78
✗ Branch 0 not taken.
✓ Branch 1 taken 17820 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17820 times.
✓ Branch 4 taken 2808 times.
✓ Branch 5 taken 15012 times.
✓ Branch 6 taken 25408 times.
✓ Branch 7 taken 363848 times.
✓ Branch 8 taken 181476 times.
✓ Branch 9 taken 207780 times.
✓ Branch 10 taken 1108 times.
✓ Branch 11 taken 180368 times.
✓ Branch 12 taken 207780 times.
✓ Branch 13 taken 1108 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 388148 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 60496932 times.
✓ Branch 20 taken 121925704 times.
✓ Branch 21 taken 60080164 times.
✓ Branch 22 taken 416768 times.
✓ Branch 23 taken 59814208 times.
✓ Branch 24 taken 265956 times.
✓ Branch 25 taken 59633840 times.
✓ Branch 26 taken 180368 times.
✓ Branch 27 taken 121925704 times.
✓ Branch 28 taken 863092 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 121925704 times.
✓ Branch 31 taken 863092 times.
✗ Branch 32 not taken.
✓ Branch 33 taken 673104 times.
✓ Branch 34 taken 189988 times.
✓ Branch 35 taken 446324 times.
✓ Branch 36 taken 416768 times.
✓ Branch 37 taken 180368 times.
✓ Branch 38 taken 265956 times.
✓ Branch 39 taken 2075648 times.
✓ Branch 40 taken 180346988 times.
✓ Branch 41 taken 2074648 times.
✓ Branch 42 taken 1000 times.
✓ Branch 43 taken 149768 times.
✓ Branch 44 taken 1924880 times.
✓ Branch 45 taken 23576 times.
✓ Branch 46 taken 126192 times.
✓ Branch 47 taken 7896 times.
✓ Branch 48 taken 15680 times.
✓ Branch 49 taken 7774 times.
✓ Branch 50 taken 122 times.
✓ Branch 51 taken 7774 times.
✗ Branch 52 not taken.
✓ Branch 53 taken 126192 times.
✓ Branch 54 taken 15802 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 126192 times.
✓ Branch 57 taken 15802 times.
✗ Branch 58 not taken.
✓ Branch 59 taken 15802 times.
✗ Branch 60 not taken.
✓ Branch 61 taken 122 times.
✓ Branch 62 taken 15680 times.
✗ Branch 63 not taken.
✓ Branch 64 taken 122 times.
✓ Branch 65 taken 182422636 times.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✓ Branch 71 taken 2075648 times.
✓ Branch 72 taken 180346988 times.
✓ Branch 73 taken 182422636 times.
✓ Branch 74 taken 389256 times.
✓ Branch 75 taken 25408 times.
✓ Branch 76 taken 363848 times.
✓ Branch 77 taken 389256 times.
✓ Branch 78 taken 17820 times.
182829712 DEFINE_BLEND_PLANE(8, uint8_t, 8)
574
30/68
✗ Branch 0 not taken.
✓ Branch 1 taken 243 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 243 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 243 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1920 times.
✓ Branch 8 taken 34560 times.
✓ Branch 9 taken 115200 times.
✓ Branch 10 taken 11520 times.
✓ Branch 11 taken 23040 times.
✓ Branch 12 taken 11232 times.
✓ Branch 13 taken 288 times.
✓ Branch 14 taken 10998 times.
✓ Branch 15 taken 234 times.
✓ Branch 16 taken 115200 times.
✓ Branch 17 taken 23562 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 115200 times.
✓ Branch 20 taken 23562 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 22842 times.
✓ Branch 23 taken 720 times.
✓ Branch 24 taken 522 times.
✓ Branch 25 taken 23040 times.
✓ Branch 26 taken 234 times.
✓ Branch 27 taken 288 times.
✗ Branch 28 not taken.
✓ Branch 29 taken 149760 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 149760 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 149760 times.
✓ Branch 62 taken 149760 times.
✓ Branch 63 taken 1920 times.
✗ Branch 64 not taken.
✓ Branch 65 taken 1920 times.
✓ Branch 66 taken 1920 times.
✓ Branch 67 taken 243 times.
151923 DEFINE_BLEND_PLANE(16, uint16_t, 10)
575
576 #define DEFINE_ALPHA_COMPOSITE(depth, T, nbits) \
577 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
578 int src_w, int src_h, \
579 int dst_w, int dst_h, \
580 int x, int y, \
581 int jobnr, int nb_jobs) \
582 { \
583 T alpha; /* the amount of overlay to blend on to main */ \
584 const T max = (1 << nbits) - 1; \
585 \
586 const int imin = FFMAX(-y, 0), imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h); \
587 const int jmin = FFMAX(-x, 0), jmax = FFMIN(-x + dst_w, src_w); \
588 const int slice_start = imin + ( imax * jobnr) / nb_jobs; \
589 const int slice_end = imin + ((imax * (jobnr + 1)) / nb_jobs); \
590 \
591 const uint8_t *sa = src->data[3] + (slice_start) * src->linesize[3]; \
592 uint8_t *da = dst->data[3] + (y + slice_start) * dst->linesize[3]; \
593 \
594 for (int i = slice_start; i < slice_end; ++i) { \
595 const T *s = (const T *)sa + jmin; \
596 T *d = (T *)da + x + jmin; \
597 \
598 for (int j = jmin; j < jmax; ++j) { \
599 alpha = *s; \
600 if (alpha != 0 && alpha != max) { \
601 uint8_t alpha_d = *d; \
602 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
603 } \
604 if (alpha == max) \
605 *d = *s; \
606 else if (alpha > 0) { \
607 /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
608 if (nbits > 8) \
609 *d += (max - *d) * *s / max; \
610 else \
611 *d += FAST_DIV255((max - *d) * *s); \
612 } \
613 d += 1; \
614 s += 1; \
615 } \
616 da += dst->linesize[3]; \
617 sa += src->linesize[3]; \
618 } \
619 }
620
14/16
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 936 times.
✓ Branch 4 taken 833152 times.
✓ Branch 5 taken 384 times.
✓ Branch 6 taken 63052 times.
✓ Branch 7 taken 770100 times.
✓ Branch 8 taken 770100 times.
✓ Branch 9 taken 63436 times.
✓ Branch 10 taken 63052 times.
✓ Branch 11 taken 384 times.
✓ Branch 12 taken 833536 times.
✓ Branch 13 taken 8512 times.
✓ Branch 14 taken 8512 times.
✓ Branch 15 taken 936 times.
842984 DEFINE_ALPHA_COMPOSITE(8, uint8_t, 8)
621 DEFINE_ALPHA_COMPOSITE(16, uint16_t, 10)
622
623 #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
624 static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
625 AVFrame *dst, const AVFrame *src, \
626 int hsub, int vsub, \
627 int main_has_alpha, \
628 int x, int y, \
629 int is_straight, \
630 int jobnr, int nb_jobs) \
631 { \
632 OverlayContext *s = ctx->priv; \
633 const int src_w = src->width; \
634 const int src_h = src->height; \
635 const int dst_w = dst->width; \
636 const int dst_h = dst->height; \
637 \
638 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
639 x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
640 s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs); \
641 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
642 x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
643 s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs); \
644 blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
645 x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
646 s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
647 \
648 if (main_has_alpha) \
649 alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
650 jobnr, nb_jobs); \
651 }
652
2/2
✓ Branch 3 taken 927 times.
✓ Branch 4 taken 4995 times.
5922 DEFINE_BLEND_SLICE_YUV(8, 8)
653
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
81 DEFINE_BLEND_SLICE_YUV(16, 10)
654
655 18 static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
656 AVFrame *dst, const AVFrame *src,
657 int hsub, int vsub,
658 int main_has_alpha,
659 int x, int y,
660 int is_straight,
661 int jobnr,
662 int nb_jobs)
663 {
664 18 OverlayContext *s = ctx->priv;
665 18 const int src_w = src->width;
666 18 const int src_h = src->height;
667 18 const int dst_w = dst->width;
668 18 const int dst_h = dst->height;
669
670 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
671 18 s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
672 jobnr, nb_jobs);
673 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
674 18 s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
675 jobnr, nb_jobs);
676 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
677 18 s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
678 jobnr, nb_jobs);
679
680
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (main_has_alpha)
681 9 alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
682 18 }
683
684 #define DEFINE_BLEND_SLICE_PLANAR_FMT(format_, blend_slice_fn_suffix_, hsub_, vsub_, main_has_alpha_, direct_) \
685 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
686 { \
687 OverlayContext *s = ctx->priv; \
688 ThreadData *td = arg; \
689 blend_slice_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
690 hsub_, vsub_, main_has_alpha_, \
691 s->x, s->y, direct_, \
692 jobnr, nb_jobs); \
693 return 0; \
694 }
695
696 // FMT FN H V A D
697 4977 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420, yuv_8_8bits, 1, 1, 0, 1)
698 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420, yuv_8_8bits, 1, 1, 1, 1)
699 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420p10, yuv_16_10bits, 1, 1, 0, 1)
700 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420p10, yuv_16_10bits, 1, 1, 1, 1)
701 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422p10, yuv_16_10bits, 1, 0, 0, 1)
702 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422p10, yuv_16_10bits, 1, 0, 1, 1)
703 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422, yuv_8_8bits, 1, 0, 0, 1)
704 459 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422, yuv_8_8bits, 1, 0, 1, 1)
705 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444, yuv_8_8bits, 0, 0, 0, 1)
706 459 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444, yuv_8_8bits, 0, 0, 1, 1)
707 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444p10, yuv_16_10bits, 0, 0, 0, 1)
708 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444p10, yuv_16_10bits, 0, 0, 1, 1)
709 9 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp, planar_rgb, 0, 0, 0, 1)
710 9 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap, planar_rgb, 0, 0, 1, 1)
711 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420_pm, yuv_8_8bits, 1, 1, 0, 0)
712 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420_pm, yuv_8_8bits, 1, 1, 1, 0)
713 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422_pm, yuv_8_8bits, 1, 0, 0, 0)
714 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422_pm, yuv_8_8bits, 1, 0, 1, 0)
715 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444_pm, yuv_8_8bits, 0, 0, 0, 0)
716 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444_pm, yuv_8_8bits, 0, 0, 1, 0)
717 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp_pm, planar_rgb, 0, 0, 0, 0)
718 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap_pm, planar_rgb, 0, 0, 1, 0)
719
720 #define DEFINE_BLEND_SLICE_PACKED_FMT(format_, blend_slice_fn_suffix_, main_has_alpha_, direct_) \
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, direct_, \
728 jobnr, nb_jobs); \
729 return 0; \
730 }
731
732 // FMT FN A D
733 459 DEFINE_BLEND_SLICE_PACKED_FMT(rgb, rgb, 0, 1)
734 9 DEFINE_BLEND_SLICE_PACKED_FMT(rgba, rgb, 1, 1)
735 DEFINE_BLEND_SLICE_PACKED_FMT(rgb_pm, rgb, 0, 0)
736 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_pm, rgb, 1, 0)
737
738 23 static int config_input_main(AVFilterLink *inlink)
739 {
740 23 OverlayContext *s = inlink->dst->priv;
741 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
742
743 23 av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
744
745 23 s->hsub = pix_desc->log2_chroma_w;
746 23 s->vsub = pix_desc->log2_chroma_h;
747
748 23 s->main_desc = pix_desc;
749
750 23 s->main_is_packed_rgb =
751 23 ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
752 23 s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
753
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) {
754 9 case OVERLAY_FORMAT_YUV420:
755
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
756 9 break;
757 1 case OVERLAY_FORMAT_YUV420P10:
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420p10 : blend_slice_yuv420p10;
759 1 break;
760 3 case OVERLAY_FORMAT_YUV422:
761
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
762 3 break;
763 1 case OVERLAY_FORMAT_YUV422P10:
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422p10 : blend_slice_yuv422p10;
765 1 break;
766 3 case OVERLAY_FORMAT_YUV444:
767
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
768 3 break;
769 1 case OVERLAY_FORMAT_YUV444P10:
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444p10 : blend_slice_yuv444p10;
771 1 break;
772 3 case OVERLAY_FORMAT_RGB:
773
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
774 3 break;
775 2 case OVERLAY_FORMAT_GBRP:
776
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
777 2 break;
778 case OVERLAY_FORMAT_AUTO:
779 switch (inlink->format) {
780 case AV_PIX_FMT_YUVA420P:
781 s->blend_slice = blend_slice_yuva420;
782 break;
783 case AV_PIX_FMT_YUVA420P10:
784 s->blend_slice = blend_slice_yuva420p10;
785 break;
786 case AV_PIX_FMT_YUVA422P:
787 s->blend_slice = blend_slice_yuva422;
788 break;
789 case AV_PIX_FMT_YUVA422P10:
790 s->blend_slice = blend_slice_yuva422p10;
791 break;
792 case AV_PIX_FMT_YUVA444P:
793 s->blend_slice = blend_slice_yuva444;
794 break;
795 case AV_PIX_FMT_YUVA444P10:
796 s->blend_slice = blend_slice_yuva444p10;
797 break;
798 case AV_PIX_FMT_ARGB:
799 case AV_PIX_FMT_RGBA:
800 case AV_PIX_FMT_BGRA:
801 case AV_PIX_FMT_ABGR:
802 s->blend_slice = blend_slice_rgba;
803 break;
804 case AV_PIX_FMT_GBRAP:
805 s->blend_slice = blend_slice_gbrap;
806 break;
807 default:
808 av_assert0(0);
809 break;
810 }
811 break;
812 }
813
814
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (!s->alpha_format)
815 23 goto end;
816
817 switch (s->format) {
818 case OVERLAY_FORMAT_YUV420:
819 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
820 break;
821 case OVERLAY_FORMAT_YUV422:
822 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
823 break;
824 case OVERLAY_FORMAT_YUV444:
825 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
826 break;
827 case OVERLAY_FORMAT_RGB:
828 s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
829 break;
830 case OVERLAY_FORMAT_GBRP:
831 s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
832 break;
833 case OVERLAY_FORMAT_AUTO:
834 switch (inlink->format) {
835 case AV_PIX_FMT_YUVA420P:
836 s->blend_slice = blend_slice_yuva420_pm;
837 break;
838 case AV_PIX_FMT_YUVA422P:
839 s->blend_slice = blend_slice_yuva422_pm;
840 break;
841 case AV_PIX_FMT_YUVA444P:
842 s->blend_slice = blend_slice_yuva444_pm;
843 break;
844 case AV_PIX_FMT_ARGB:
845 case AV_PIX_FMT_RGBA:
846 case AV_PIX_FMT_BGRA:
847 case AV_PIX_FMT_ABGR:
848 s->blend_slice = blend_slice_rgba_pm;
849 break;
850 case AV_PIX_FMT_GBRAP:
851 s->blend_slice = blend_slice_gbrap_pm;
852 break;
853 default:
854 av_assert0(0);
855 break;
856 }
857 break;
858 }
859
860 23 end:
861 #if ARCH_X86
862 23 ff_overlay_init_x86(s, s->format, inlink->format,
863 23 s->alpha_format, s->main_has_alpha);
864 #endif
865
866 23 return 0;
867 }
868
869 724 static int do_blend(FFFrameSync *fs)
870 {
871 724 AVFilterContext *ctx = fs->parent;
872 AVFrame *mainpic, *second;
873 724 OverlayContext *s = ctx->priv;
874 724 AVFilterLink *inlink = ctx->inputs[0];
875 724 FilterLink *inl = ff_filter_link(inlink);
876 int ret;
877
878 724 ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
724 if (ret < 0)
880 return ret;
881
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 721 times.
724 if (!second)
882 3 return ff_filter_frame(ctx->outputs[0], mainpic);
883
884
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 if (s->eval_mode == EVAL_MODE_FRAME) {
885
886 721 s->var_values[VAR_N] = inl->frame_count_out;
887 1442 s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
888
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 NAN : mainpic->pts * av_q2d(inlink->time_base);
889
890 721 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
891 721 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
892 721 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
893 721 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
894
895 721 eval_expr(ctx);
896 721 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f x:%f xi:%d y:%f yi:%d\n",
897 s->var_values[VAR_N], s->var_values[VAR_T],
898 s->var_values[VAR_X], s->x,
899 s->var_values[VAR_Y], s->y);
900 }
901
902
2/4
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 721 times.
✗ Branch 3 not taken.
721 if (s->x < mainpic->width && s->x + second->width >= 0 &&
903
2/4
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 721 times.
✗ Branch 3 not taken.
721 s->y < mainpic->height && s->y + second->height >= 0) {
904 ThreadData td;
905
906 721 td.dst = mainpic;
907 721 td.src = second;
908
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 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)),
909 ff_filter_get_nb_threads(ctx)));
910 }
911 721 return ff_filter_frame(ctx->outputs[0], mainpic);
912 }
913
914 46 static av_cold int init(AVFilterContext *ctx)
915 {
916 46 OverlayContext *s = ctx->priv;
917
918 46 s->fs.on_event = do_blend;
919 46 return 0;
920 }
921
922 1860 static int activate(AVFilterContext *ctx)
923 {
924 1860 OverlayContext *s = ctx->priv;
925 1860 return ff_framesync_activate(&s->fs);
926 }
927
928 #define OFFSET(x) offsetof(OverlayContext, x)
929 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
930 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
931
932 static const AVOption overlay_options[] = {
933 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
934 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, TFLAGS },
935 { "eof_action", "Action to take when encountering EOF from secondary input ",
936 OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
937 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, .unit = "eof_action" },
938 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, .unit = "eof_action" },
939 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, .unit = "eof_action" },
940 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, .unit = "eof_action" },
941 { "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" },
942 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
943 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
944 { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
945 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, .unit = "format" },
946 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
947 { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
948 { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
949 { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
950 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
951 { "yuv444p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444P10}, .flags = FLAGS, .unit = "format" },
952 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
953 { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
954 { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
955 { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
956 { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "alpha_format" },
957 { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
958 { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
959 { NULL }
960 };
961
962
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 82 times.
420 FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
963
964 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
965 {
966 .name = "main",
967 .type = AVMEDIA_TYPE_VIDEO,
968 .config_props = config_input_main,
969 },
970 {
971 .name = "overlay",
972 .type = AVMEDIA_TYPE_VIDEO,
973 .config_props = config_input_overlay,
974 },
975 };
976
977 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
978 {
979 .name = "default",
980 .type = AVMEDIA_TYPE_VIDEO,
981 .config_props = config_output,
982 },
983 };
984
985 const FFFilter ff_vf_overlay = {
986 .p.name = "overlay",
987 .p.description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
988 .p.priv_class = &overlay_class,
989 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
990 AVFILTER_FLAG_SLICE_THREADS,
991 .preinit = overlay_framesync_preinit,
992 .init = init,
993 .uninit = uninit,
994 .priv_size = sizeof(OverlayContext),
995 .activate = activate,
996 .process_command = process_command,
997 FILTER_INPUTS(avfilter_vf_overlay_inputs),
998 FILTER_OUTPUTS(avfilter_vf_overlay_outputs),
999 FILTER_QUERY_FUNC2(query_formats),
1000 };
1001