FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_overlay.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 263 373 70.5%
Functions: 33 48 68.8%
Branches: 172 310 55.5%

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