OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkPngFilters.h" | |
9 #include "SkTypes.h" | |
10 | |
11 // Functions in this file look at most 3 pixels (a,b,c) to predict the fourth (d ). | |
12 // They're positioned like this: | |
13 // prev: c b | |
14 // row: a d | |
15 // The Sub filter predicts d=a, Avg d=(a+b)/2, and Paeth predicts d to be which ever | |
16 // of a, b, or c is closest to p=a+b-c. (Up also exists, predicting d=b.) | |
17 | |
18 #if defined(__SSE2__) | |
19 | |
20 template <int bpp> | |
21 static __m128i load(const void* p) { | |
22 static_assert(bpp <= 4, ""); | |
23 | |
24 uint32_t packed; | |
25 memcpy(&packed, p, bpp); | |
26 return _mm_cvtsi32_si128(packed); | |
27 } | |
28 | |
29 template <int bpp> | |
30 static void store(void* p, __m128i v) { | |
31 static_assert(bpp <= 4, ""); | |
32 | |
33 uint32_t packed = _mm_cvtsi128_si32(v); | |
34 memcpy(p, &packed, bpp); | |
35 } | |
36 | |
37 template <int bpp> | |
38 static void sk_sub_sse2(png_row_infop row_info, png_bytep row, png_const_byt ep) { | |
39 // The Sub filter predicts each pixel as the previous pixel, a. | |
40 // There is no pixel to the left of the first pixel. It's encoded direc tly. | |
41 // That works with our main loop if we just say that left pixel was zero . | |
42 __m128i a, d = _mm_setzero_si128(); | |
43 | |
44 int rb = row_info->rowbytes; | |
45 while (rb > 0) { | |
46 a = d; d = load<bpp>(row); | |
47 d = _mm_add_epi8(d, a); | |
48 store<bpp>(row, d); | |
49 | |
50 row += bpp; | |
51 rb -= bpp; | |
52 } | |
53 } | |
54 | |
55 template <int bpp> | |
56 void sk_avg_sse2(png_row_infop row_info, png_bytep row, png_const_bytep prev ) { | |
57 // The Avg filter predicts each pixel as the (truncated) average of a an d b. | |
58 // There's no pixel to the left of the first pixel. Luckily, it's | |
59 // predicted to be half of the pixel above it. So again, this works | |
60 // perfectly with our loop if we make sure a starts at zero. | |
61 const __m128i zero = _mm_setzero_si128(); | |
62 __m128i b; | |
63 __m128i a, d = zero; | |
64 | |
65 int rb = row_info->rowbytes; | |
66 while (rb > 0) { | |
67 b = load<bpp>(prev); | |
68 a = d; d = load<bpp>(row ); | |
69 | |
70 // PNG requires a truncating average here, so sadly we can't just us e _mm_avg_epu8... | |
71 __m128i avg = _mm_avg_epu8(a,b); | |
72 // ...but we can fix it up by subtracting off 1 if it rounded up. | |
73 avg = _mm_sub_epi8(avg, _mm_and_si128(_mm_xor_si128(a,b), _mm_set1_e pi8(1))); | |
74 | |
75 d = _mm_add_epi8(d, avg); | |
76 store<bpp>(row, d); | |
77 | |
78 prev += bpp; | |
79 row += bpp; | |
80 rb -= bpp; | |
81 } | |
82 } | |
83 | |
84 // Returns bytewise |x-y|. | |
85 static __m128i absdiff_u8(__m128i x, __m128i y) { | |
86 // One of these two saturated subtractions will be the answer, the other zero. | |
87 return _mm_or_si128(_mm_subs_epu8(x,y), _mm_subs_epu8(y,x)); | |
88 } | |
89 | |
90 // Bytewise c ? t : e. | |
91 static __m128i if_then_else(__m128i c, __m128i t, __m128i e) { | |
92 // SSE 4.1+ would be: return _mm_blendv_epi8(e,t,c); | |
93 return _mm_or_si128(_mm_and_si128(c, t), _mm_andnot_si128(c, e)); | |
94 } | |
95 | |
96 template <int bpp> | |
97 void sk_paeth_sse2(png_row_infop row_info, png_bytep row, png_const_bytep pr ev) { | |
98 // Paeth tries to predict pixel d using the pixel to the left of it, a, | |
99 // and two pixels from the previous row, b and c: | |
100 // prev: c b | |
101 // row: a d | |
102 // The Paeth function predicts d to be whichever of a, b, or c is neares t to p=a+b-c. | |
103 | |
104 // The first pixel has no left context, and so uses an Up filter, p = b. | |
105 // This works naturally with our main loop's p = a+b-c if we force a and c to zero. | |
106 // Here we zero b and d, which become c and a respectively at the start of the loop. | |
107 __m128i c, b = _mm_setzero_si128(), | |
108 a, d = _mm_setzero_si128(); | |
109 | |
110 int rb = row_info->rowbytes; | |
111 while (rb > 0) { | |
112 c = b; b = load<bpp>(prev); | |
113 a = d; d = load<bpp>(row ); | |
114 | |
115 // We can't express p in 8 bits, but luckily we can use this faux p instead. | |
116 // (I have no deep insight here... I just proved this with brute for ce.) | |
117 __m128i min = _mm_min_epu8(a,b), | |
118 max = _mm_max_epu8(a,b), | |
119 faux_p = _mm_adds_epu8(min, _mm_subs_epu8(max, c)); | |
120 | |
121 // We could use faux_p for calculating all three of pa, pb, and pc, | |
122 // but it's a little quicker to calculate the correct pa and pb dire ctly, | |
123 // and the predictor remains the same. (Again, brute force.) | |
msarett
2016/01/27 20:56:10
I've also confirmed this with brute force. Though
| |
124 __m128i pa = absdiff_u8(b,c), // |a+b-c - a| == |b-c| | |
125 pb = absdiff_u8(a,c), // |a+b-c - b| == |a-c| | |
126 faux_pc = absdiff_u8(faux_p, c); | |
127 | |
128 // From here, things are straightforward. Find the smallest distanc e to p... | |
129 __m128i smallest = _mm_min_epu8(_mm_min_epu8(pa, pb), faux_pc); | |
130 | |
131 // ... then the predictor is the input corresponding to that smalles t distance, | |
132 // breaking ties in favor of a over b over c. | |
133 __m128i nearest = if_then_else(_mm_cmpeq_epi8(smallest, pa), a, | |
134 if_then_else(_mm_cmpeq_epi8(smallest, pb), b, | |
135 c)); | |
136 | |
137 // We've reconstructed d! Leave it for next round to become a, and write it out. | |
138 d = _mm_add_epi8(d, nearest); | |
139 store<bpp>(row, d); | |
140 | |
141 prev += bpp; | |
142 row += bpp; | |
143 rb -= bpp; | |
144 } | |
145 } | |
146 | |
147 void sk_sub3_sse2(png_row_infop row_info, png_bytep row, png_const_bytep pre v) { | |
148 sk_sub_sse2<3>(row_info, row, prev); | |
149 } | |
150 void sk_sub4_sse2(png_row_infop row_info, png_bytep row, png_const_bytep pre v) { | |
151 sk_sub_sse2<4>(row_info, row, prev); | |
152 } | |
153 | |
154 void sk_avg3_sse2(png_row_infop row_info, png_bytep row, png_const_bytep pre v) { | |
155 sk_avg_sse2<3>(row_info, row, prev); | |
156 } | |
157 void sk_avg4_sse2(png_row_infop row_info, png_bytep row, png_const_bytep pre v) { | |
158 sk_avg_sse2<4>(row_info, row, prev); | |
159 } | |
160 | |
161 void sk_paeth3_sse2(png_row_infop row_info, png_bytep row, png_const_bytep p rev) { | |
162 sk_paeth_sse2<3>(row_info, row, prev); | |
163 } | |
164 void sk_paeth4_sse2(png_row_infop row_info, png_bytep row, png_const_bytep p rev) { | |
165 sk_paeth_sse2<4>(row_info, row, prev); | |
166 } | |
167 | |
168 #endif | |
OLD | NEW |