Chromium Code Reviews| 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 static __m128i load4(const void* p) { | |
| 21 return _mm_cvtsi32_si128(*(const uint32_t*)p); | |
| 22 } | |
| 23 | |
| 24 static void store4(void* p, __m128i v) { | |
| 25 *(uint32_t*)p = _mm_cvtsi128_si32(v); | |
| 26 } | |
| 27 | |
| 28 void sk_sub4_sse2(png_row_infop row_info, png_bytep row_u8, png_const_bytep) { | |
| 29 // The Sub filter predicts each pixel as the previous pixel, a. | |
| 30 auto row = (uint32_t*)row_u8; | |
| 31 int n = row_info->rowbytes / 4; | |
| 32 | |
| 33 // There is no pixel to the left of the first pixel. It's encoded direc tly. | |
| 34 // That works with our main loop if we just say that left pixel was zero . | |
| 35 __m128i a, d = _mm_setzero_si128(); | |
| 36 | |
| 37 while (n --> 0) { | |
| 38 a = d; d = load4(row); | |
| 39 d = _mm_add_epi8(d, a); | |
| 40 store4(row++, d); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void sk_avg4_sse2(png_row_infop row_info, png_bytep row_u8, png_const_bytep prev_u8) { | |
| 45 // The Avg filter predicts each pixel as the (truncated) average of a an d b. | |
| 46 auto prev = (const uint32_t*)prev_u8; | |
| 47 auto row = (uint32_t*)row_u8; | |
| 48 int n = row_info->rowbytes / 4; | |
| 49 | |
| 50 // There's no pixel to the left of the first pixel. Luckily, it's | |
| 51 // predicted to be half of the pixel above it. So again, this works | |
| 52 // perfectly with our loop if we make sure a starts at zero. | |
| 53 const __m128i zero = _mm_setzero_si128(); | |
| 54 __m128i b; | |
| 55 __m128i a, d = zero; | |
| 56 | |
| 57 while (n --> 0) { | |
| 58 b = load4(prev++); | |
| 59 a = d; d = load4(row ); | |
| 60 | |
| 61 // PNG requires a truncating average here, so sadly we can't just us e _mm_avg_epu8... | |
| 62 __m128i avg = _mm_avg_epu8(a,b); | |
| 63 // ...but we can fix it up by subtracting off 1 if it rounded up. | |
| 64 avg = _mm_sub_epi8(avg, _mm_and_si128(_mm_xor_si128(a,b), _mm_set1_e pi8(1))); | |
| 65 | |
| 66 d = _mm_add_epi8(d, avg); | |
| 67 store4(row++, d); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 // Returns bytewise |x-y|. | |
| 72 static __m128i absdiff_u8(__m128i x, __m128i y) { | |
| 73 // One of these two saturated subtractions will be the answer, the other zero. | |
| 74 return _mm_or_si128(_mm_subs_epu8(x,y), _mm_subs_epu8(y,x)); | |
| 75 } | |
| 76 | |
| 77 // Bytewise c ? t : e. | |
| 78 static __m128i if_then_else(__m128i c, __m128i t, __m128i e) { | |
| 79 // SSE 4.1+ would be: return _mm_blendv_epi8(e,t,c); | |
|
msarett
2016/01/27 20:56:10
Why not add a #if here?
mtklein
2016/01/27 21:00:16
Mostly because we won't have a bot to test it, tho
| |
| 80 return _mm_or_si128(_mm_and_si128(c, t), _mm_andnot_si128(c, e)); | |
| 81 } | |
| 82 | |
| 83 void sk_paeth4_sse2(png_row_infop row_info, png_bytep row_u8, png_const_byte p prev_u8) { | |
| 84 auto prev = (const uint32_t*)prev_u8; | |
| 85 auto row = (uint32_t*)row_u8; | |
| 86 int n = row_info->rowbytes / 4; | |
| 87 | |
| 88 // Paeth tries to predict pixel d using the pixel to the left of it, a, | |
| 89 // and two pixels from the previous row, b and c: | |
| 90 // prev: c b | |
| 91 // row: a d | |
| 92 // The Paeth function predicts d to be whichever of a, b, or c is neares t to p=a+b-c. | |
| 93 | |
| 94 // The first pixel has no left context, and so uses an Up filter, p = b. | |
| 95 // This works naturally with our main loop's p = a+b-c if we force a and c to zero. | |
| 96 // Here we zero b and d, which become c and a respectively at the start of the loop. | |
| 97 __m128i c, b = _mm_setzero_si128(), | |
| 98 a, d = _mm_setzero_si128(); | |
| 99 | |
| 100 while (n --> 0) { | |
| 101 c = b; b = load4(prev++); | |
| 102 a = d; d = load4(row); | |
| 103 | |
| 104 // We can't express p in 8 bits, but luckily we can use this faux p instead. | |
| 105 // (I have no deep insight here... I just proved this with brute for ce.) | |
| 106 __m128i min = _mm_min_epu8(a,b), | |
| 107 max = _mm_max_epu8(a,b), | |
| 108 faux_p = _mm_adds_epu8(min, _mm_subs_epu8(max, c)); | |
| 109 | |
| 110 // We could use faux_p for calculating all three of pa, pb, and pc, | |
| 111 // but it's a little quicker to calculate the correct pa and pb dire ctly, | |
| 112 // and the predictor remains the same. (Again, brute force.) | |
| 113 __m128i pa = absdiff_u8(b,c), // |a+b-c - a| == |b-c| | |
| 114 pb = absdiff_u8(a,c), // |a+b-c - b| == |a-c| | |
| 115 faux_pc = absdiff_u8(faux_p, c); | |
| 116 | |
| 117 // From here, things are straightforward. Find the smallest distanc e to p... | |
| 118 __m128i smallest = _mm_min_epu8(_mm_min_epu8(pa, pb), faux_pc); | |
| 119 | |
| 120 // ... then the predictor is the input corresponding to that smalles t distance, | |
| 121 // breaking ties in favor of a over b over c. | |
| 122 __m128i nearest = if_then_else(_mm_cmpeq_epi8(smallest, pa), a, | |
| 123 if_then_else(_mm_cmpeq_epi8(smallest, pb), b, | |
| 124 c)); | |
| 125 | |
| 126 // We've reconstructed d! Leave it for next round to become a, and write it out. | |
| 127 d = _mm_add_epi8(d, nearest); | |
| 128 store4(row++, d); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 #endif | |
| OLD | NEW |