FFmpeg
Loading...
Searching...
No Matches
blend_modes.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/common.h"
22#include "libavutil/intfloat.h"
23#include "avfilter.h"
24#include "video.h"
25#include "blend.h"
26
27#undef PIXEL
28#undef MAX
29#undef HALF
30#undef CLIP
31
32#if DEPTH == 8
33#define PIXEL uint8_t
34#define MAX 255
35#define HALF 128
36#define CLIP(x) (av_clip_uint8(x))
37#define BUILD_TYPE_SPECIFIC_FUNCS
38#elif DEPTH == 32
39#define PIXEL float
40#define MAX 1.f
41#define HALF 0.5f
42#define CLIP(x) (x)
43#define BUILD_TYPE_SPECIFIC_FUNCS
44#else
45#define PIXEL uint16_t
46#define MAX ((1 << DEPTH) - 1)
47#define HALF (1 << (DEPTH - 1))
48#define CLIP(x) ((int)av_clip_uintp2(x, DEPTH))
49#if DEPTH == 16
50#define BUILD_TYPE_SPECIFIC_FUNCS
51#endif
52#endif
53
54#undef MULTIPLY
55#undef SCREEN
56#undef BURN
57#undef DODGE
58#undef GEOMETRIC
59#undef INT2FLOAT
60#undef FLOAT2INT
61#undef MDIV
62#undef LRINTF
63
64#if DEPTH < 32
65#define MULTIPLY(x, a, b) ((x) * (((a) * (b)) / MAX))
66#define SCREEN(x, a, b) (MAX - (x) * ((MAX - (a)) * (MAX - (b)) / MAX))
67#define BURN(a, b) (((a) == 0) ? (a) : FFMAX(0, MAX - ((MAX - (b)) << DEPTH) / (a)))
68#define DODGE(a, b) (((a) == MAX) ? (a) : FFMIN(MAX, (((b) << DEPTH) / (MAX - (a)))))
69#define GEOMETRIC(a, b) (lrintf(sqrtf((unsigned)A * B)))
70#define INT2FLOAT(x) (x)
71#define FLOAT2INT(x) (x)
72#define MDIV (0.125f * (1 << DEPTH))
73#define LRINTF(x) lrintf(x)
74#else
75#define MULTIPLY(x, a, b) ((x) * (((a) * (b)) / 1.0))
76#define SCREEN(x, a, b) (1.0 - (x) * ((1.0 - (a)) * (1.0 - (b)) / 1.0))
77#define BURN(a, b) (((a) <= 0.0) ? (a) : FFMAX(0.0, 1.0 - (1.0 - (b)) / (a)))
78#define DODGE(a, b) (((a) >= 1.0) ? (a) : FFMIN(1.0, ((b) / (1.0 - (a)))))
79#define GEOMETRIC(a, b) (sqrtf(fmaxf(A, 0) * fmaxf(B, 0)))
80#define INT2FLOAT(x) av_int2float(x)
81#define FLOAT2INT(x) av_float2int(x)
82#define MDIV 0.125f
83#define LRINTF(x) (x)
84#endif
85
86#define A top[j]
87#define B bottom[j]
88
89#define fn2(a, b) blend_##a##_##b##bit
90#define fn1(name, depth) fn2(name, depth)
91#define fn0(name) fn1(name, DEPTH)
92
93#define fn(NAME, EXPR) \
94static void fn0(NAME)(const uint8_t *_top, ptrdiff_t top_linesize, \
95 const uint8_t *_bottom, ptrdiff_t bottom_linesize, \
96 uint8_t *_dst, ptrdiff_t dst_linesize, \
97 ptrdiff_t width, ptrdiff_t height, \
98 FilterParams *param, SliceParams *sliceparam) \
99{ \
100 const float opacity = param->opacity; \
101 \
102 for (int i = 0; i < height; i++) { \
103 const PIXEL *top = (const PIXEL *)_top; \
104 const PIXEL *bottom = (const PIXEL *)_bottom; \
105 PIXEL *dst = (PIXEL *)_dst; \
106 for (int j = 0; j < width; j++) { \
107 dst[j] = top[j] + ((EXPR)-top[j]) * opacity; \
108 } \
109 _dst += dst_linesize; \
110 _top += top_linesize; \
111 _bottom += bottom_linesize; \
112 } \
113}
114
115fn(addition, FFMIN(MAX, A + B))
116fn(grainmerge, CLIP(A + B - HALF))
117fn(multiply, MULTIPLY(1, A, B))
118fn(multiply128,CLIP((A - HALF) * B / MDIV + HALF))
119fn(negation, MAX - FFABS(MAX - A - B))
120fn(extremity, FFABS(MAX - A - B))
121fn(grainextract, CLIP(HALF + A - B))
122fn(screen, SCREEN(1, A, B))
123fn(overlay, (A < HALF) ? MULTIPLY(2, A, B) : SCREEN(2, A, B))
124fn(hardlight, (B < HALF) ? MULTIPLY(2, B, A) : SCREEN(2, B, A))
125fn(hardmix, (A < (MAX - B)) ? 0: MAX)
126fn(heat, (A == 0) ? 0 : MAX - FFMIN(((MAX - B) * (MAX - B)) / A, MAX))
127fn(freeze, (B == 0) ? 0 : MAX - FFMIN(((MAX - A) * (MAX - A)) / B, MAX))
128fn(divide, CLIP(B == 0 ? MAX : MAX * A / B))
129fn(dodge, DODGE(A, B))
130fn(burn, BURN(A, B))
131fn(softlight, CLIP(A * A / MAX + (2 * (B * ((A * (MAX - A)) / MAX) / MAX))))
132fn(exclusion, A + B - 2 * A * B / MAX)
133fn(pinlight, (B < HALF) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - HALF)))
134fn(phoenix, FFMIN(A, B) - FFMAX(A, B) + MAX)
135fn(reflect, (B == MAX) ? B : FFMIN(MAX, (A * A / (MAX - B))))
136fn(glow, (A == MAX) ? A : FFMIN(MAX, (B * B / (MAX - A))))
137fn(vividlight, (A < HALF) ? BURN(2 * A, B) : DODGE(2 * (A - HALF), B))
138fn(linearlight,CLIP((B < HALF) ? B + 2 * A - MAX : B + 2 * (A - HALF)))
139fn(softdifference,CLIP((A > B) ? (B == MAX) ? 0 : (A - B) * MAX / (MAX - B) : (B == 0) ? 0 : (B - A) * MAX / B))
140fn(bleach, (MAX - B) + (MAX - A) - MAX)
141fn(stain, 2 * MAX - A - B)
142fn(interpolate,LRINTF(MAX * (2 - cosf(A * M_PI / MAX) - cosf(B * M_PI / MAX)) * 0.25f))
143fn(hardoverlay,A == MAX ? MAX : FFMIN(MAX, MAX * B / (2 * MAX - 2 * A) * (A > HALF) + 2 * A * B / MAX * (A <= HALF)))
144
145#ifdef BUILD_TYPE_SPECIFIC_FUNCS
146fn(average, (A + B) / 2)
147fn(subtract, FFMAX(0, A - B))
148fn(difference, FFABS(A - B))
149fn(darken, FFMIN(A, B))
150fn(lighten, FFMAX(A, B))
154fn(geometric, GEOMETRIC(A, B))
155fn(harmonic, A == 0 && B == 0 ? 0 : 2LL * A * B / (A + B))
156#undef BUILD_TYPE_SPECIFIC_FUNCS
157#endif
#define fn(a)
static SoftFloat_IEEE754 multiply(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b)
multiply two softfloats and handle the rounding off
Definition alsdec.c:1406
#define A(x)
Definition vpx_arith.h:28
Main libavfilter public API header.
#define HALF
Definition bgmc.c:36
#define MAX
Definition blend_modes.c:46
#define LRINTF(x)
Definition blend_modes.c:73
#define fn(NAME, EXPR)
Definition blend_modes.c:93
#define INT2FLOAT(x)
Definition blend_modes.c:70
#define CLIP(x)
Definition blend_modes.c:48
#define GEOMETRIC(a, b)
Definition blend_modes.c:69
#define FLOAT2INT(x)
Definition blend_modes.c:71
#define MDIV
Definition blend_modes.c:72
#define BURN(a, b)
Definition blend_modes.c:67
#define SCREEN(x, a, b)
Definition blend_modes.c:66
#define MULTIPLY(x, a, b)
Definition blend_modes.c:65
#define DODGE(a, b)
Definition blend_modes.c:68
common internal and external API header
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define B
Definition huffyuv.h:42
#define cosf(x)
Definition libm.h:80
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
static void interpolate(float *out, float v1, float v2, int size)
Definition twinvq.c:85
static void difference(IPlane *g, IPlane *f, int y0, int y1)
Definition vf_morpho.c:495