History

avfilter/vf_maskedmerge: fix rounding when masking

Paul B Mahol committed 4 years ago

Commit dae95b31 parent 59520f03 files · +23 −16Browse files

Unified · Split

3 files
  1. libavfilter/maskedmerge.h +1 −1
  2. libavfilter/vf_maskedmerge.c +12 −8
  3. libavfilter/x86/vf_maskedmerge.asm +10 −7
libavfilter/maskedmerge.h+1−1
@@ -30,7 +30,7 @@ typedef struct MaskedMergeContext { · 29 unmodified lines
30 int linesize[4];30 int linesize[4];
31 int nb_planes;31 int nb_planes;
32 int planes;32 int planes;
33 int half, depth;33 int half, depth, max;
34 FFFrameSync fs;34 FFFrameSync fs;
35 35
36 void (*maskedmerge)(const uint8_t *bsrc, const uint8_t *osrc,36 void (*maskedmerge)(const uint8_t *bsrc, const uint8_t *osrc,
libavfilter/vf_maskedmerge.c+12−8
@@ -96,7 +96,7 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) · 95 unmodified lines
96 base->linesize[p], overlay->linesize[p],96 base->linesize[p], overlay->linesize[p],
97 mask->linesize[p], out->linesize[p],97 mask->linesize[p], out->linesize[p],
98 s->width[p], slice_end - slice_start,98 s->width[p], slice_end - slice_start,
99 s->half, s->depth);99 s->half, s->max);
100 }100 }
101 101
102 return 0;102 return 0;
@@ -138,13 +138,13 @@ static int process_frame(FFFrameSync *fs) · 35 unmodified lines
138 return ff_filter_frame(outlink, out);138 return ff_filter_frame(outlink, out);
139}139}
140 140
141#define MASKEDMERGE(n, type, half, shift) \141#define MASKEDMERGE(n, type, ctype, half, max, div) \
142static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \142static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \
143 const uint8_t *mmsrc, uint8_t *ddst, \143 const uint8_t *mmsrc, uint8_t *ddst, \
144 ptrdiff_t blinesize, ptrdiff_t olinesize, \144 ptrdiff_t blinesize, ptrdiff_t olinesize, \
145 ptrdiff_t mlinesize, ptrdiff_t dlinesize, \145 ptrdiff_t mlinesize, ptrdiff_t dlinesize, \
146 int w, int h, \146 int w, int h, \
147 int hhalf, int sshift) \147 int hhalf, int mmax) \
148{ \148{ \
149 const type *bsrc = (const type *)bbsrc; \149 const type *bsrc = (const type *)bbsrc; \
150 const type *osrc = (const type *)oosrc; \150 const type *osrc = (const type *)oosrc; \
@@ -158,7 +158,10 @@ static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \ · 7 unmodified lines
158 \158 \
159 for (int y = 0; y < h; y++) { \159 for (int y = 0; y < h; y++) { \
160 for (int x = 0; x < w; x++) { \160 for (int x = 0; x < w; x++) { \
161 dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + half) shift); \161 const type invm = max - msrc[x]; \
162 const ctype r = ((ctype)(bsrc[x] * invm) + \
163 (ctype)(msrc[x] * osrc[x] + half)) div; \
164 dst[x] = r; \
162 } \165 } \
163 \166 \
164 dst += dlinesize; \167 dst += dlinesize; \
@@ -168,9 +171,9 @@ static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \ · 3 unmodified lines
168 } \171 } \
169}172}
170 173
171MASKEDMERGE(8, uint8_t, 128, >> 8)174MASKEDMERGE(8, uint8_t, uint16_t, 127, 255, / 255)
172MASKEDMERGE(16, uint16_t, hhalf, >> sshift)175MASKEDMERGE(16, uint16_t, uint32_t, hhalf, mmax, / mmax)
173MASKEDMERGE(32, float, 0.f, + 0.f)176MASKEDMERGE(32, float, float, 0.f, 1.f, + 0.f)
174 177
175static int config_input(AVFilterLink *inlink)178static int config_input(AVFilterLink *inlink)
176{179{
@@ -189,7 +192,8 @@ static int config_input(AVFilterLink *inlink) · 12 unmodified lines
189 s->width[0] = s->width[3] = inlink->w;192 s->width[0] = s->width[3] = inlink->w;
190 193
191 s->depth = desc->comp[0].depth;194 s->depth = desc->comp[0].depth;
192 s->half = (1 << s->depth) / 2;195 s->max = (1 << s->depth) - 1;
196 s->half = s->max / 2;
193 197
194 if (s->depth == 8)198 if (s->depth == 8)
195 s->maskedmerge = maskedmerge8;199 s->maskedmerge = maskedmerge8;
libavfilter/x86/vf_maskedmerge.asm+10−7
@@ -24,26 +24,28 @@ · 23 unmodified lines
24 24
25SECTION_RODATA25SECTION_RODATA
26 26
27pw_128: times 8 dw 12827pw_127: times 8 dw 127
28pw_256: times 8 dw 25628pw_255: times 8 dw 255
29pw_32897: times 8 dw 32897
29 30
30SECTION .text31SECTION .text
31 32
32INIT_XMM sse233INIT_XMM sse2
33%if ARCH_X86_6434%if ARCH_X86_64
34cglobal maskedmerge8, 8, 11, 7, bsrc, osrc, msrc, dst, blinesize, olinesize, mlinesize, dlinesize, w, h, x35cglobal maskedmerge8, 8, 11, 8, bsrc, osrc, msrc, dst, blinesize, olinesize, mlinesize, dlinesize, w, h, x
35 mov wd, dword wm36 mov wd, dword wm
36 mov hd, dword hm37 mov hd, dword hm
37%else38%else
38cglobal maskedmerge8, 5, 7, 7, bsrc, osrc, msrc, dst, blinesize, w, x39cglobal maskedmerge8, 5, 7, 8, bsrc, osrc, msrc, dst, blinesize, w, x
39 mov wd, r8m40 mov wd, r8m
40%define olinesizeq r5mp41%define olinesizeq r5mp
41%define mlinesizeq r6mp42%define mlinesizeq r6mp
42%define dlinesizeq r7mp43%define dlinesizeq r7mp
43%define hd r9mp44%define hd r9mp
44%endif45%endif
45 mova m4, [pw_256]46 mova m4, [pw_255]
46 mova m5, [pw_128]47 mova m5, [pw_127]
48 mova m7, [pw_32897]
47 pxor m6, m649 pxor m6, m6
48 add bsrcq, wq50 add bsrcq, wq
49 add osrcq, wq51 add osrcq, wq
@@ -66,7 +68,8 @@ cglobal maskedmerge8, 5, 7, 7, bsrc, osrc, msrc, dst, blinesize, w, x · 16 unmodified lines
66 pmullw m1, m368 pmullw m1, m3
67 paddw m1, m269 paddw m1, m2
68 paddw m1, m570 paddw m1, m5
69 psrlw m1, 871 pmulhuw m1, m7
72 psrlw m1, 7
70 packuswb m1, m173 packuswb m1, m1
71 movh [dstq + xq], m174 movh [dstq + xq], m1
72 add xq, mmsize / 275 add xq, mmsize / 2

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t