FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_overlay.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 265 375 70.7%
Functions: 33 48 68.8%
Branches: 174 312 55.8%

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 "internal.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 #if FF_API_FRAME_PKT
59 "pos", ///< position in the file
60 #endif
61 "t", ///< timestamp expressed in seconds
62 NULL
63 };
64
65 #define MAIN 0
66 #define OVERLAY 1
67
68 #define R 0
69 #define G 1
70 #define B 2
71 #define A 3
72
73 #define Y 0
74 #define U 1
75 #define V 2
76
77 enum EvalMode {
78 EVAL_MODE_INIT,
79 EVAL_MODE_FRAME,
80 EVAL_MODE_NB
81 };
82
83 46 static av_cold void uninit(AVFilterContext *ctx)
84 {
85 46 OverlayContext *s = ctx->priv;
86
87 46 ff_framesync_uninit(&s->fs);
88 46 av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
89 46 av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
90 46 }
91
92 1442 static inline int normalize_xy(double d, int chroma_sub)
93 {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1442 times.
1442 if (isnan(d))
95 return INT_MAX;
96 1442 return (int)d & ~((1 << chroma_sub) - 1);
97 }
98
99 721 static void eval_expr(AVFilterContext *ctx)
100 {
101 721 OverlayContext *s = ctx->priv;
102
103 721 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
104 721 s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
105 /* It is necessary if x is expressed from y */
106 721 s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
107 721 s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
108 721 s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
109 721 }
110
111 46 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
112 {
113 int ret;
114 46 AVExpr *old = NULL;
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (*pexpr)
117 old = *pexpr;
118 46 ret = av_expr_parse(pexpr, expr, var_names,
119 NULL, NULL, NULL, NULL, 0, log_ctx);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0) {
121 av_log(log_ctx, AV_LOG_ERROR,
122 "Error when evaluating the expression '%s' for %s\n",
123 expr, option);
124 *pexpr = old;
125 return ret;
126 }
127
128 46 av_expr_free(old);
129 46 return 0;
130 }
131
132 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
133 char *res, int res_len, int flags)
134 {
135 OverlayContext *s = ctx->priv;
136 int ret;
137
138 if (!strcmp(cmd, "x"))
139 ret = set_expr(&s->x_pexpr, args, cmd, ctx);
140 else if (!strcmp(cmd, "y"))
141 ret = set_expr(&s->y_pexpr, args, cmd, ctx);
142 else
143 ret = AVERROR(ENOSYS);
144
145 if (ret < 0)
146 return ret;
147
148 if (s->eval_mode == EVAL_MODE_INIT) {
149 eval_expr(ctx);
150 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
151 s->var_values[VAR_X], s->x,
152 s->var_values[VAR_Y], s->y);
153 }
154 return ret;
155 }
156
157 static const enum AVPixelFormat alpha_pix_fmts[] = {
158 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
159 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
160 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
161 AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
162 };
163
164 23 static int query_formats(AVFilterContext *ctx)
165 {
166 23 OverlayContext *s = ctx->priv;
167
168 /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
169 static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
170 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
171 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
172 AV_PIX_FMT_NONE
173 };
174 static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
175 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
176 };
177
178 static const enum AVPixelFormat main_pix_fmts_yuv420p10[] = {
179 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
180 AV_PIX_FMT_NONE
181 };
182 static const enum AVPixelFormat overlay_pix_fmts_yuv420p10[] = {
183 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_NONE
184 };
185
186 static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
187 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
188 };
189 static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
190 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
191 };
192
193 static const enum AVPixelFormat main_pix_fmts_yuv422p10[] = {
194 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
195 };
196 static const enum AVPixelFormat overlay_pix_fmts_yuv422p10[] = {
197 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
198 };
199
200 static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
201 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
202 };
203 static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
204 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
205 };
206
207 static const enum AVPixelFormat main_pix_fmts_yuv444p10[] = {
208 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
209 };
210 static const enum AVPixelFormat overlay_pix_fmts_yuv444p10[] = {
211 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_NONE
212 };
213
214 static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
215 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
216 };
217 static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
218 AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
219 };
220
221 static const enum AVPixelFormat main_pix_fmts_rgb[] = {
222 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
223 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
224 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
225 AV_PIX_FMT_NONE
226 };
227 static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
228 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
229 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
230 AV_PIX_FMT_NONE
231 };
232
233 const enum AVPixelFormat *main_formats, *overlay_formats;
234 AVFilterFormats *formats;
235 int ret;
236
237
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) {
238 9 case OVERLAY_FORMAT_YUV420:
239 9 main_formats = main_pix_fmts_yuv420;
240 9 overlay_formats = overlay_pix_fmts_yuv420;
241 9 break;
242 1 case OVERLAY_FORMAT_YUV420P10:
243 1 main_formats = main_pix_fmts_yuv420p10;
244 1 overlay_formats = overlay_pix_fmts_yuv420p10;
245 1 break;
246 3 case OVERLAY_FORMAT_YUV422:
247 3 main_formats = main_pix_fmts_yuv422;
248 3 overlay_formats = overlay_pix_fmts_yuv422;
249 3 break;
250 1 case OVERLAY_FORMAT_YUV422P10:
251 1 main_formats = main_pix_fmts_yuv422p10;
252 1 overlay_formats = overlay_pix_fmts_yuv422p10;
253 1 break;
254 3 case OVERLAY_FORMAT_YUV444:
255 3 main_formats = main_pix_fmts_yuv444;
256 3 overlay_formats = overlay_pix_fmts_yuv444;
257 3 break;
258 1 case OVERLAY_FORMAT_YUV444P10:
259 1 main_formats = main_pix_fmts_yuv444p10;
260 1 overlay_formats = overlay_pix_fmts_yuv444p10;
261 1 break;
262 3 case OVERLAY_FORMAT_RGB:
263 3 main_formats = main_pix_fmts_rgb;
264 3 overlay_formats = overlay_pix_fmts_rgb;
265 3 break;
266 2 case OVERLAY_FORMAT_GBRP:
267 2 main_formats = main_pix_fmts_gbrp;
268 2 overlay_formats = overlay_pix_fmts_gbrp;
269 2 break;
270 case OVERLAY_FORMAT_AUTO:
271 return ff_set_common_formats_from_list(ctx, alpha_pix_fmts);
272 default:
273 av_assert0(0);
274 }
275
276 23 formats = ff_make_format_list(main_formats);
277
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, &ctx->inputs[MAIN]->outcfg.formats)) < 0 ||
278 23 (ret = ff_formats_ref(formats, &ctx->outputs[MAIN]->incfg.formats)) < 0)
279 return ret;
280
281 23 return ff_formats_ref(ff_make_format_list(overlay_formats),
282 23 &ctx->inputs[OVERLAY]->outcfg.formats);
283 }
284
285 23 static int config_input_overlay(AVFilterLink *inlink)
286 {
287 23 AVFilterContext *ctx = inlink->dst;
288 23 OverlayContext *s = inlink->dst->priv;
289 int ret;
290 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
291
292 23 av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
293
294 /* Finish the configuration by evaluating the expressions
295 now when both inputs are configured. */
296 23 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
297 23 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
298 23 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
299 23 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
300 23 s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
301 23 s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
302 23 s->var_values[VAR_X] = NAN;
303 23 s->var_values[VAR_Y] = NAN;
304 23 s->var_values[VAR_N] = 0;
305 23 s->var_values[VAR_T] = NAN;
306 #if FF_API_FRAME_PKT
307 23 s->var_values[VAR_POS] = NAN;
308 #endif
309
310
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 ||
311 23 (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
312 return ret;
313
314 23 s->overlay_is_packed_rgb =
315 23 ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
316 23 s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->eval_mode == EVAL_MODE_INIT) {
319 eval_expr(ctx);
320 av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
321 s->var_values[VAR_X], s->x,
322 s->var_values[VAR_Y], s->y);
323 }
324
325 69 av_log(ctx, AV_LOG_VERBOSE,
326 "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
327 23 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
328 23 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
329 23 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
330 23 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
331 23 return 0;
332 }
333
334 23 static int config_output(AVFilterLink *outlink)
335 {
336 23 AVFilterContext *ctx = outlink->src;
337 23 OverlayContext *s = ctx->priv;
338 int ret;
339
340
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
341 return ret;
342
343 23 outlink->w = ctx->inputs[MAIN]->w;
344 23 outlink->h = ctx->inputs[MAIN]->h;
345 23 outlink->time_base = ctx->inputs[MAIN]->time_base;
346
347 23 return ff_framesync_configure(&s->fs);
348 }
349
350 // divide by 255 and round to nearest
351 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
352 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
353
354 // calculate the unpremultiplied alpha, applying the general equation:
355 // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
356 // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
357 // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
358 #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
359
360 /**
361 * Blend image in src to destination buffer dst at position (x, y).
362 */
363
364 468 static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
365 AVFrame *dst, const AVFrame *src,
366 int main_has_alpha, int x, int y,
367 int is_straight, int jobnr, int nb_jobs)
368 {
369 468 OverlayContext *s = ctx->priv;
370 int i, imax, j, jmax;
371 468 const int src_w = src->width;
372 468 const int src_h = src->height;
373 468 const int dst_w = dst->width;
374 468 const int dst_h = dst->height;
375 uint8_t alpha; ///< the amount of overlay to blend on to main
376 468 const int dr = s->main_rgba_map[R];
377 468 const int dg = s->main_rgba_map[G];
378 468 const int db = s->main_rgba_map[B];
379 468 const int da = s->main_rgba_map[A];
380 468 const int dstep = s->main_pix_step[0];
381 468 const int sr = s->overlay_rgba_map[R];
382 468 const int sg = s->overlay_rgba_map[G];
383 468 const int sb = s->overlay_rgba_map[B];
384 468 const int sa = s->overlay_rgba_map[A];
385 468 const int sstep = s->overlay_pix_step[0];
386 int slice_start, slice_end;
387 uint8_t *S, *sp, *d, *dp;
388
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 468 times.
468 i = FFMAX(-y, 0);
390 468 imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
391
392 468 slice_start = i + (imax * jobnr) / nb_jobs;
393 468 slice_end = i + (imax * (jobnr+1)) / nb_jobs;
394
395 468 sp = src->data[0] + (slice_start) * src->linesize[0];
396 468 dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
397
398
2/2
✓ Branch 0 taken 4256 times.
✓ Branch 1 taken 468 times.
4724 for (i = slice_start; i < slice_end; i++) {
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4256 times.
4256 j = FFMAX(-x, 0);
400 4256 S = sp + j * sstep;
401 4256 d = dp + (x+j) * dstep;
402
403
2/2
✓ Branch 0 taken 416768 times.
✓ Branch 1 taken 4256 times.
421024 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
404 416768 alpha = S[sa];
405
406 // if the main channel has an alpha channel, alpha has to be calculated
407 // to create an un-premultiplied (straight) alpha value
408
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) {
409 15829 uint8_t alpha_d = d[da];
410 15829 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
411 }
412
413
3/3
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 384918 times.
✓ Branch 2 taken 31658 times.
416768 switch (alpha) {
414 192 case 0:
415 192 break;
416 384918 case 255:
417 384918 d[dr] = S[sr];
418 384918 d[dg] = S[sg];
419 384918 d[db] = S[sb];
420 384918 break;
421 31658 default:
422 // main_value = main_value * (1 - alpha) + overlay_value * alpha
423 // since alpha is in the range 0-255, the result must divided by 255
424
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) :
425 FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
426
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) :
427 FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
428
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) :
429 FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
430 }
431
2/2
✓ Branch 0 taken 16384 times.
✓ Branch 1 taken 400384 times.
416768 if (main_has_alpha) {
432
3/3
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 459 times.
✓ Branch 2 taken 15829 times.
16384 switch (alpha) {
433 96 case 0:
434 96 break;
435 459 case 255:
436 459 d[da] = S[sa];
437 459 break;
438 15829 default:
439 // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
440 15829 d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
441 }
442 }
443 416768 d += dstep;
444 416768 S += sstep;
445 }
446 4256 dp += dst->linesize[0];
447 4256 sp += src->linesize[0];
448 }
449 468 }
450
451 #define DEFINE_BLEND_PLANE(depth, nbits) \
452 static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
453 AVFrame *dst, const AVFrame *src, \
454 int src_w, int src_h, \
455 int dst_w, int dst_h, \
456 int i, int hsub, int vsub, \
457 int x, int y, \
458 int main_has_alpha, \
459 int dst_plane, \
460 int dst_offset, \
461 int dst_step, \
462 int straight, \
463 int yuv, \
464 int jobnr, \
465 int nb_jobs) \
466 { \
467 OverlayContext *octx = ctx->priv; \
468 int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
469 int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
470 int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
471 int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
472 int yp = y>>vsub; \
473 int xp = x>>hsub; \
474 uint##depth##_t *s, *sp, *d, *dp, *dap, *a, *da, *ap; \
475 int jmax, j, k, kmax; \
476 int slice_start, slice_end; \
477 const uint##depth##_t max = (1 << nbits) - 1; \
478 const uint##depth##_t mid = (1 << (nbits -1)) ; \
479 int bytes = depth / 8; \
480 \
481 dst_step /= bytes; \
482 j = FFMAX(-yp, 0); \
483 jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
484 \
485 slice_start = j + (jmax * jobnr) / nb_jobs; \
486 slice_end = j + (jmax * (jobnr+1)) / nb_jobs; \
487 \
488 sp = (uint##depth##_t *)(src->data[i] + (slice_start) * src->linesize[i]); \
489 dp = (uint##depth##_t *)(dst->data[dst_plane] \
490 + (yp + slice_start) * dst->linesize[dst_plane] \
491 + dst_offset); \
492 ap = (uint##depth##_t *)(src->data[3] + (slice_start << vsub) * src->linesize[3]); \
493 dap = (uint##depth##_t *)(dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3]); \
494 \
495 for (j = slice_start; j < slice_end; j++) { \
496 k = FFMAX(-xp, 0); \
497 d = dp + (xp+k) * dst_step; \
498 s = sp + k; \
499 a = ap + (k<<hsub); \
500 da = dap + ((xp+k) << hsub); \
501 kmax = FFMIN(-xp + dst_wp, src_wp); \
502 \
503 if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
504 int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
505 (uint8_t*)a, kmax - k, src->linesize[3]); \
506 \
507 s += c; \
508 d += dst_step * c; \
509 da += (1 << hsub) * c; \
510 a += (1 << hsub) * c; \
511 k += c; \
512 } \
513 for (; k < kmax; k++) { \
514 int alpha_v, alpha_h, alpha; \
515 \
516 /* average alpha for color components, improve quality */ \
517 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
518 alpha = (a[0] + a[src->linesize[3]] + \
519 a[1] + a[src->linesize[3]+1]) >> 2; \
520 } else if (hsub || vsub) { \
521 alpha_h = hsub && k+1 < src_wp ? \
522 (a[0] + a[1]) >> 1 : a[0]; \
523 alpha_v = vsub && j+1 < src_hp ? \
524 (a[0] + a[src->linesize[3]]) >> 1 : a[0]; \
525 alpha = (alpha_v + alpha_h) >> 1; \
526 } else \
527 alpha = a[0]; \
528 /* if the main channel has an alpha channel, alpha has to be calculated */ \
529 /* to create an un-premultiplied (straight) alpha value */ \
530 if (main_has_alpha && alpha != 0 && alpha != max) { \
531 /* average alpha for color components, improve quality */ \
532 uint8_t alpha_d; \
533 if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
534 alpha_d = (da[0] + da[dst->linesize[3]] + \
535 da[1] + da[dst->linesize[3]+1]) >> 2; \
536 } else if (hsub || vsub) { \
537 alpha_h = hsub && k+1 < src_wp ? \
538 (da[0] + da[1]) >> 1 : da[0]; \
539 alpha_v = vsub && j+1 < src_hp ? \
540 (da[0] + da[dst->linesize[3]]) >> 1 : da[0]; \
541 alpha_d = (alpha_v + alpha_h) >> 1; \
542 } else \
543 alpha_d = da[0]; \
544 alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
545 } \
546 if (straight) { \
547 if (nbits > 8) \
548 *d = (*d * (max - alpha) + *s * alpha) / max; \
549 else \
550 *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
551 } else { \
552 if (nbits > 8) { \
553 if (i && yuv) \
554 *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
555 else \
556 *d = av_clip_uintp2((*d * (max - alpha) + *s * alpha) / max + *s - (16<<(nbits-8)),\
557 nbits);\
558 } else { \
559 if (i && yuv) \
560 *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
561 else \
562 *d = av_clip_uint8(FAST_DIV255(*d * (255 - alpha)) + *s - 16); \
563 } \
564 } \
565 s++; \
566 d += dst_step; \
567 da += 1 << hsub; \
568 a += 1 << hsub; \
569 } \
570 dp += dst->linesize[dst_plane] / bytes; \
571 sp += src->linesize[i] / bytes; \
572 ap += (1 << vsub) * src->linesize[3] / bytes; \
573 dap += (1 << vsub) * dst->linesize[3] / bytes; \
574 } \
575 }
576
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 149504 times.
✓ Branch 38 taken 1925144 times.
✓ Branch 39 taken 23576 times.
✓ Branch 40 taken 125928 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 125928 times.
✓ Branch 48 taken 15802 times.
✗ Branch 49 not taken.
✓ Branch 50 taken 125928 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)
577
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)
578
579 #define DEFINE_ALPHA_COMPOSITE(depth, nbits) \
580 static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
581 int src_w, int src_h, \
582 int dst_w, int dst_h, \
583 int x, int y, \
584 int jobnr, int nb_jobs) \
585 { \
586 uint##depth##_t alpha; /* the amount of overlay to blend on to main */ \
587 uint##depth##_t *s, *sa, *d, *da; \
588 int i, imax, j, jmax; \
589 int slice_start, slice_end; \
590 const uint##depth##_t max = (1 << nbits) - 1; \
591 int bytes = depth / 8; \
592 \
593 imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h); \
594 i = FFMAX(-y, 0); \
595 \
596 slice_start = i + (imax * jobnr) / nb_jobs; \
597 slice_end = i + ((imax * (jobnr+1)) / nb_jobs); \
598 \
599 sa = (uint##depth##_t *)(src->data[3] + (slice_start) * src->linesize[3]); \
600 da = (uint##depth##_t *)(dst->data[3] + (y + slice_start) * dst->linesize[3]); \
601 \
602 for (i = slice_start; i < slice_end; i++) { \
603 j = FFMAX(-x, 0); \
604 s = sa + j; \
605 d = da + x+j; \
606 \
607 for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) { \
608 alpha = *s; \
609 if (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] / bytes; \
626 sa += src->linesize[3] / bytes; \
627 } \
628 }
629
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 62964 times.
✓ Branch 7 taken 770188 times.
✓ Branch 8 taken 770188 times.
✓ Branch 9 taken 63348 times.
✓ Branch 10 taken 62964 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)
630 DEFINE_ALPHA_COMPOSITE(16, 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_has_alpha, \
637 int x, int y, \
638 int is_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_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
649 s->main_desc->comp[0].step, is_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_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
652 s->main_desc->comp[1].step, is_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_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
655 s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
656 \
657 if (main_has_alpha) \
658 alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
659 jobnr, nb_jobs); \
660 }
661
2/2
✓ Branch 3 taken 927 times.
✓ Branch 4 taken 4995 times.
5922 DEFINE_BLEND_SLICE_YUV(8, 8)
662
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
81 DEFINE_BLEND_SLICE_YUV(16, 10)
663
664 18 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_has_alpha,
668 int x, int y,
669 int is_straight,
670 int jobnr,
671 int nb_jobs)
672 {
673 18 OverlayContext *s = ctx->priv;
674 18 const int src_w = src->width;
675 18 const int src_h = src->height;
676 18 const int dst_w = dst->width;
677 18 const int dst_h = dst->height;
678
679 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
680 18 s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
681 jobnr, nb_jobs);
682 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
683 18 s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
684 jobnr, nb_jobs);
685 18 blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
686 18 s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
687 jobnr, nb_jobs);
688
689
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (main_has_alpha)
690 9 alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
691 18 }
692
693 #define DEFINE_BLEND_SLICE_PLANAR_FMT(format_, blend_slice_fn_suffix_, hsub_, vsub_, main_has_alpha_, direct_) \
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_has_alpha_, \
700 s->x, s->y, direct_, \
701 jobnr, nb_jobs); \
702 return 0; \
703 }
704
705 // FMT FN H V A D
706 4977 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420, yuv_8_8bits, 1, 1, 0, 1);
707 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420, yuv_8_8bits, 1, 1, 1, 1);
708 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420p10, yuv_16_10bits, 1, 1, 0, 1);
709 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420p10, yuv_16_10bits, 1, 1, 1, 1);
710 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422p10, yuv_16_10bits, 1, 0, 0, 1);
711 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422p10, yuv_16_10bits, 1, 0, 1, 1);
712 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422, yuv_8_8bits, 1, 0, 0, 1);
713 459 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422, yuv_8_8bits, 1, 0, 1, 1);
714 9 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444, yuv_8_8bits, 0, 0, 0, 1);
715 459 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444, yuv_8_8bits, 0, 0, 1, 1);
716 27 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444p10, yuv_16_10bits, 0, 0, 0, 1);
717 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444p10, yuv_16_10bits, 0, 0, 1, 1);
718 9 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp, planar_rgb, 0, 0, 0, 1);
719 9 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap, planar_rgb, 0, 0, 1, 1);
720 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv420_pm, yuv_8_8bits, 1, 1, 0, 0);
721 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva420_pm, yuv_8_8bits, 1, 1, 1, 0);
722 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv422_pm, yuv_8_8bits, 1, 0, 0, 0);
723 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva422_pm, yuv_8_8bits, 1, 0, 1, 0);
724 DEFINE_BLEND_SLICE_PLANAR_FMT(yuv444_pm, yuv_8_8bits, 0, 0, 0, 0);
725 DEFINE_BLEND_SLICE_PLANAR_FMT(yuva444_pm, yuv_8_8bits, 0, 0, 1, 0);
726 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrp_pm, planar_rgb, 0, 0, 0, 0);
727 DEFINE_BLEND_SLICE_PLANAR_FMT(gbrap_pm, planar_rgb, 0, 0, 1, 0);
728
729 #define DEFINE_BLEND_SLICE_PACKED_FMT(format_, blend_slice_fn_suffix_, main_has_alpha_, direct_) \
730 static int blend_slice_##format_(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
731 { \
732 OverlayContext *s = ctx->priv; \
733 ThreadData *td = arg; \
734 blend_slice_packed_##blend_slice_fn_suffix_(ctx, td->dst, td->src, \
735 main_has_alpha_, \
736 s->x, s->y, direct_, \
737 jobnr, nb_jobs); \
738 return 0; \
739 }
740
741 // FMT FN A D
742 459 DEFINE_BLEND_SLICE_PACKED_FMT(rgb, rgb, 0, 1);
743 9 DEFINE_BLEND_SLICE_PACKED_FMT(rgba, rgb, 1, 1);
744 DEFINE_BLEND_SLICE_PACKED_FMT(rgb_pm, rgb, 0, 0);
745 DEFINE_BLEND_SLICE_PACKED_FMT(rgba_pm, rgb, 1, 0);
746
747 23 static int config_input_main(AVFilterLink *inlink)
748 {
749 23 OverlayContext *s = inlink->dst->priv;
750 23 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
751
752 23 av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
753
754 23 s->hsub = pix_desc->log2_chroma_w;
755 23 s->vsub = pix_desc->log2_chroma_h;
756
757 23 s->main_desc = pix_desc;
758
759 23 s->main_is_packed_rgb =
760 23 ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
761 23 s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
762
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) {
763 9 case OVERLAY_FORMAT_YUV420:
764
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;
765 9 break;
766 1 case OVERLAY_FORMAT_YUV420P10:
767
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;
768 1 break;
769 3 case OVERLAY_FORMAT_YUV422:
770
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;
771 3 break;
772 1 case OVERLAY_FORMAT_YUV422P10:
773
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;
774 1 break;
775 3 case OVERLAY_FORMAT_YUV444:
776
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;
777 3 break;
778 1 case OVERLAY_FORMAT_YUV444P10:
779
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;
780 1 break;
781 3 case OVERLAY_FORMAT_RGB:
782
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;
783 3 break;
784 2 case OVERLAY_FORMAT_GBRP:
785
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;
786 2 break;
787 case OVERLAY_FORMAT_AUTO:
788 switch (inlink->format) {
789 case AV_PIX_FMT_YUVA420P:
790 s->blend_slice = blend_slice_yuva420;
791 break;
792 case AV_PIX_FMT_YUVA420P10:
793 s->blend_slice = blend_slice_yuva420p10;
794 break;
795 case AV_PIX_FMT_YUVA422P:
796 s->blend_slice = blend_slice_yuva422;
797 break;
798 case AV_PIX_FMT_YUVA422P10:
799 s->blend_slice = blend_slice_yuva422p10;
800 break;
801 case AV_PIX_FMT_YUVA444P:
802 s->blend_slice = blend_slice_yuva444;
803 break;
804 case AV_PIX_FMT_YUVA444P10:
805 s->blend_slice = blend_slice_yuva444p10;
806 break;
807 case AV_PIX_FMT_ARGB:
808 case AV_PIX_FMT_RGBA:
809 case AV_PIX_FMT_BGRA:
810 case AV_PIX_FMT_ABGR:
811 s->blend_slice = blend_slice_rgba;
812 break;
813 case AV_PIX_FMT_GBRAP:
814 s->blend_slice = blend_slice_gbrap;
815 break;
816 default:
817 av_assert0(0);
818 break;
819 }
820 break;
821 }
822
823
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (!s->alpha_format)
824 23 goto end;
825
826 switch (s->format) {
827 case OVERLAY_FORMAT_YUV420:
828 s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
829 break;
830 case OVERLAY_FORMAT_YUV422:
831 s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
832 break;
833 case OVERLAY_FORMAT_YUV444:
834 s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
835 break;
836 case OVERLAY_FORMAT_RGB:
837 s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
838 break;
839 case OVERLAY_FORMAT_GBRP:
840 s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
841 break;
842 case OVERLAY_FORMAT_AUTO:
843 switch (inlink->format) {
844 case AV_PIX_FMT_YUVA420P:
845 s->blend_slice = blend_slice_yuva420_pm;
846 break;
847 case AV_PIX_FMT_YUVA422P:
848 s->blend_slice = blend_slice_yuva422_pm;
849 break;
850 case AV_PIX_FMT_YUVA444P:
851 s->blend_slice = blend_slice_yuva444_pm;
852 break;
853 case AV_PIX_FMT_ARGB:
854 case AV_PIX_FMT_RGBA:
855 case AV_PIX_FMT_BGRA:
856 case AV_PIX_FMT_ABGR:
857 s->blend_slice = blend_slice_rgba_pm;
858 break;
859 case AV_PIX_FMT_GBRAP:
860 s->blend_slice = blend_slice_gbrap_pm;
861 break;
862 default:
863 av_assert0(0);
864 break;
865 }
866 break;
867 }
868
869 23 end:
870 #if ARCH_X86
871 23 ff_overlay_init_x86(s, s->format, inlink->format,
872 23 s->alpha_format, s->main_has_alpha);
873 #endif
874
875 23 return 0;
876 }
877
878 724 static int do_blend(FFFrameSync *fs)
879 {
880 724 AVFilterContext *ctx = fs->parent;
881 AVFrame *mainpic, *second;
882 724 OverlayContext *s = ctx->priv;
883 724 AVFilterLink *inlink = ctx->inputs[0];
884 int ret;
885
886 724 ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
724 if (ret < 0)
888 return ret;
889
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 721 times.
724 if (!second)
890 3 return ff_filter_frame(ctx->outputs[0], mainpic);
891
892
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 if (s->eval_mode == EVAL_MODE_FRAME) {
893
894 721 s->var_values[VAR_N] = inlink->frame_count_out;
895 1442 s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
896
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 NAN : mainpic->pts * av_q2d(inlink->time_base);
897 #if FF_API_FRAME_PKT
898 FF_DISABLE_DEPRECATION_WARNINGS
899 {
900 721 int64_t pos = mainpic->pkt_pos;
901
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 370 times.
721 s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
902 }
903 FF_ENABLE_DEPRECATION_WARNINGS
904 #endif
905
906 721 s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
907 721 s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
908 721 s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
909 721 s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
910
911 721 eval_expr(ctx);
912 721 av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f x:%f xi:%d y:%f yi:%d\n",
913 s->var_values[VAR_N], s->var_values[VAR_T],
914 s->var_values[VAR_X], s->x,
915 s->var_values[VAR_Y], s->y);
916 }
917
918
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 &&
919
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) {
920 ThreadData td;
921
922 721 td.dst = mainpic;
923 721 td.src = second;
924
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)),
925 ff_filter_get_nb_threads(ctx)));
926 }
927 721 return ff_filter_frame(ctx->outputs[0], mainpic);
928 }
929
930 46 static av_cold int init(AVFilterContext *ctx)
931 {
932 46 OverlayContext *s = ctx->priv;
933
934 46 s->fs.on_event = do_blend;
935 46 return 0;
936 }
937
938 1860 static int activate(AVFilterContext *ctx)
939 {
940 1860 OverlayContext *s = ctx->priv;
941 1860 return ff_framesync_activate(&s->fs);
942 }
943
944 #define OFFSET(x) offsetof(OverlayContext, x)
945 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
946
947 static const AVOption overlay_options[] = {
948 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
949 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
950 { "eof_action", "Action to take when encountering EOF from secondary input ",
951 OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
952 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, .unit = "eof_action" },
953 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, .unit = "eof_action" },
954 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, .unit = "eof_action" },
955 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, .unit = "eof_action" },
956 { "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" },
957 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
958 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
959 { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
960 { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, .unit = "format" },
961 { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
962 { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
963 { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
964 { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
965 { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
966 { "yuv444p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444P10}, .flags = FLAGS, .unit = "format" },
967 { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
968 { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
969 { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
970 { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
971 { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "alpha_format" },
972 { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
973 { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
974 { NULL }
975 };
976
977
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 82 times.
420 FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
978
979 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
980 {
981 .name = "main",
982 .type = AVMEDIA_TYPE_VIDEO,
983 .config_props = config_input_main,
984 },
985 {
986 .name = "overlay",
987 .type = AVMEDIA_TYPE_VIDEO,
988 .config_props = config_input_overlay,
989 },
990 };
991
992 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
993 {
994 .name = "default",
995 .type = AVMEDIA_TYPE_VIDEO,
996 .config_props = config_output,
997 },
998 };
999
1000 const AVFilter ff_vf_overlay = {
1001 .name = "overlay",
1002 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
1003 .preinit = overlay_framesync_preinit,
1004 .init = init,
1005 .uninit = uninit,
1006 .priv_size = sizeof(OverlayContext),
1007 .priv_class = &overlay_class,
1008 .activate = activate,
1009 .process_command = process_command,
1010 FILTER_INPUTS(avfilter_vf_overlay_inputs),
1011 FILTER_OUTPUTS(avfilter_vf_overlay_outputs),
1012 FILTER_QUERY_FUNC(query_formats),
1013 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
1014 AVFILTER_FLAG_SLICE_THREADS,
1015 };
1016