History

avfilter/vf_maskedmerge: fix rounding when masking

Paul B Mahol committed 4 years ago

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

Showing the changes to libavfilter (3 of 3 files). Show all

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
3030 int linesize[4];
3131 int nb_planes;
3232 int planes;
33− int half, depth;
33+ int half, depth, max;
3434 FFFrameSync fs;
3535
3636 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
9696 base->linesize[p], overlay->linesize[p],
9797 mask->linesize[p], out->linesize[p],
9898 s->width[p], slice_end - slice_start,
99− s->half, s->depth);
99+ s->half, s->max);
100100 }
101101
102102 return 0;
@@ -138,13 +138,13 @@ static int process_frame(FFFrameSync *fs) · 35 unmodified lines
138138 return ff_filter_frame(outlink, out);
139139 }
140140
141−#define MASKEDMERGE(n, type, half, shift) \
141+#define MASKEDMERGE(n, type, ctype, half, max, div) \
142142 static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \
143143 const uint8_t *mmsrc, uint8_t *ddst, \
144144 ptrdiff_t blinesize, ptrdiff_t olinesize, \
145145 ptrdiff_t mlinesize, ptrdiff_t dlinesize, \
146146 int w, int h, \
147− int hhalf, int sshift) \
147+ int hhalf, int mmax) \
148148 { \
149149 const type *bsrc = (const type *)bbsrc; \
150150 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
158158 \
159159 for (int y = 0; y < h; y++) { \
160160 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; \
162165 } \
163166 \
164167 dst += dlinesize; \
@@ -168,9 +171,9 @@ static void maskedmerge##n(const uint8_t *bbsrc, const uint8_t *oosrc, \ · 3 unmodified lines
168171 } \
169172 }
170173
171−MASKEDMERGE(8, uint8_t, 128, >> 8)
172−MASKEDMERGE(16, uint16_t, hhalf, >> sshift)
173−MASKEDMERGE(32, float, 0.f, + 0.f)
174+MASKEDMERGE(8, uint8_t, uint16_t, 127, 255, / 255)
175+MASKEDMERGE(16, uint16_t, uint32_t, hhalf, mmax, / mmax)
176+MASKEDMERGE(32, float, float, 0.f, 1.f, + 0.f)
174177
175178 static int config_input(AVFilterLink *inlink)
176179 {
@@ -189,7 +192,8 @@ static int config_input(AVFilterLink *inlink) · 12 unmodified lines
189192 s->width[0] = s->width[3] = inlink->w;
190193
191194 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;
193197
194198 if (s->depth == 8)
195199 s->maskedmerge = maskedmerge8;
libavfilter/x86/vf_maskedmerge.asm+10−7
@@ -24,26 +24,28 @@ · 23 unmodified lines
2424
2525 SECTION_RODATA
2626
27−pw_128: times 8 dw 128
28−pw_256: times 8 dw 256
27+pw_127: times 8 dw 127
28+pw_255: times 8 dw 255
29+pw_32897: times 8 dw 32897
2930
3031 SECTION .text
3132
3233 INIT_XMM sse2
3334 %if ARCH_X86_64
34−cglobal maskedmerge8, 8, 11, 7, bsrc, osrc, msrc, dst, blinesize, olinesize, mlinesize, dlinesize, w, h, x
35+cglobal maskedmerge8, 8, 11, 8, bsrc, osrc, msrc, dst, blinesize, olinesize, mlinesize, dlinesize, w, h, x
3536 mov wd, dword wm
3637 mov hd, dword hm
3738 %else
38−cglobal maskedmerge8, 5, 7, 7, bsrc, osrc, msrc, dst, blinesize, w, x
39+cglobal maskedmerge8, 5, 7, 8, bsrc, osrc, msrc, dst, blinesize, w, x
3940 mov wd, r8m
4041 %define olinesizeq r5mp
4142 %define mlinesizeq r6mp
4243 %define dlinesizeq r7mp
4344 %define hd r9mp
4445 %endif
45− mova m4, [pw_256]
46− mova m5, [pw_128]
46+ mova m4, [pw_255]
47+ mova m5, [pw_127]
48+ mova m7, [pw_32897]
4749 pxor m6, m6
4850 add bsrcq, wq
4951 add osrcq, wq
@@ -66,7 +68,8 @@ cglobal maskedmerge8, 5, 7, 7, bsrc, osrc, msrc, dst, blinesize, w, x · 16 unmodified lines
6668 pmullw m1, m3
6769 paddw m1, m2
6870 paddw m1, m5
69− psrlw m1, 8
71+ pmulhuw m1, m7
72+ psrlw m1, 7
7073 packuswb m1, m1
7174 movh [dstq + xq], m1
7275 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