dec_sse2.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // SSE2 version of some decoding functions (idct, loop filtering).
  11. //
  12. // Author: somnath@google.com (Somnath Banerjee)
  13. // cduvivier@google.com (Christian Duvivier)
  14. #include "./dsp.h"
  15. #if defined(WEBP_USE_SSE2)
  16. // The 3-coeff sparse transform in SSE2 is not really faster than the plain-C
  17. // one it seems => disable it by default. Uncomment the following to enable:
  18. // #define USE_TRANSFORM_AC3
  19. #include <emmintrin.h>
  20. #include "./common_sse2.h"
  21. #include "../dec/vp8i_dec.h"
  22. #include "../utils/utils.h"
  23. //------------------------------------------------------------------------------
  24. // Transforms (Paragraph 14.4)
  25. static void Transform(const int16_t* in, uint8_t* dst, int do_two) {
  26. // This implementation makes use of 16-bit fixed point versions of two
  27. // multiply constants:
  28. // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
  29. // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
  30. //
  31. // To be able to use signed 16-bit integers, we use the following trick to
  32. // have constants within range:
  33. // - Associated constants are obtained by subtracting the 16-bit fixed point
  34. // version of one:
  35. // k = K - (1 << 16) => K = k + (1 << 16)
  36. // K1 = 85267 => k1 = 20091
  37. // K2 = 35468 => k2 = -30068
  38. // - The multiplication of a variable by a constant become the sum of the
  39. // variable and the multiplication of that variable by the associated
  40. // constant:
  41. // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
  42. const __m128i k1 = _mm_set1_epi16(20091);
  43. const __m128i k2 = _mm_set1_epi16(-30068);
  44. __m128i T0, T1, T2, T3;
  45. // Load and concatenate the transform coefficients (we'll do two transforms
  46. // in parallel). In the case of only one transform, the second half of the
  47. // vectors will just contain random value we'll never use nor store.
  48. __m128i in0, in1, in2, in3;
  49. {
  50. in0 = _mm_loadl_epi64((const __m128i*)&in[0]);
  51. in1 = _mm_loadl_epi64((const __m128i*)&in[4]);
  52. in2 = _mm_loadl_epi64((const __m128i*)&in[8]);
  53. in3 = _mm_loadl_epi64((const __m128i*)&in[12]);
  54. // a00 a10 a20 a30 x x x x
  55. // a01 a11 a21 a31 x x x x
  56. // a02 a12 a22 a32 x x x x
  57. // a03 a13 a23 a33 x x x x
  58. if (do_two) {
  59. const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);
  60. const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);
  61. const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);
  62. const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);
  63. in0 = _mm_unpacklo_epi64(in0, inB0);
  64. in1 = _mm_unpacklo_epi64(in1, inB1);
  65. in2 = _mm_unpacklo_epi64(in2, inB2);
  66. in3 = _mm_unpacklo_epi64(in3, inB3);
  67. // a00 a10 a20 a30 b00 b10 b20 b30
  68. // a01 a11 a21 a31 b01 b11 b21 b31
  69. // a02 a12 a22 a32 b02 b12 b22 b32
  70. // a03 a13 a23 a33 b03 b13 b23 b33
  71. }
  72. }
  73. // Vertical pass and subsequent transpose.
  74. {
  75. // First pass, c and d calculations are longer because of the "trick"
  76. // multiplications.
  77. const __m128i a = _mm_add_epi16(in0, in2);
  78. const __m128i b = _mm_sub_epi16(in0, in2);
  79. // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
  80. const __m128i c1 = _mm_mulhi_epi16(in1, k2);
  81. const __m128i c2 = _mm_mulhi_epi16(in3, k1);
  82. const __m128i c3 = _mm_sub_epi16(in1, in3);
  83. const __m128i c4 = _mm_sub_epi16(c1, c2);
  84. const __m128i c = _mm_add_epi16(c3, c4);
  85. // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
  86. const __m128i d1 = _mm_mulhi_epi16(in1, k1);
  87. const __m128i d2 = _mm_mulhi_epi16(in3, k2);
  88. const __m128i d3 = _mm_add_epi16(in1, in3);
  89. const __m128i d4 = _mm_add_epi16(d1, d2);
  90. const __m128i d = _mm_add_epi16(d3, d4);
  91. // Second pass.
  92. const __m128i tmp0 = _mm_add_epi16(a, d);
  93. const __m128i tmp1 = _mm_add_epi16(b, c);
  94. const __m128i tmp2 = _mm_sub_epi16(b, c);
  95. const __m128i tmp3 = _mm_sub_epi16(a, d);
  96. // Transpose the two 4x4.
  97. VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);
  98. }
  99. // Horizontal pass and subsequent transpose.
  100. {
  101. // First pass, c and d calculations are longer because of the "trick"
  102. // multiplications.
  103. const __m128i four = _mm_set1_epi16(4);
  104. const __m128i dc = _mm_add_epi16(T0, four);
  105. const __m128i a = _mm_add_epi16(dc, T2);
  106. const __m128i b = _mm_sub_epi16(dc, T2);
  107. // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
  108. const __m128i c1 = _mm_mulhi_epi16(T1, k2);
  109. const __m128i c2 = _mm_mulhi_epi16(T3, k1);
  110. const __m128i c3 = _mm_sub_epi16(T1, T3);
  111. const __m128i c4 = _mm_sub_epi16(c1, c2);
  112. const __m128i c = _mm_add_epi16(c3, c4);
  113. // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
  114. const __m128i d1 = _mm_mulhi_epi16(T1, k1);
  115. const __m128i d2 = _mm_mulhi_epi16(T3, k2);
  116. const __m128i d3 = _mm_add_epi16(T1, T3);
  117. const __m128i d4 = _mm_add_epi16(d1, d2);
  118. const __m128i d = _mm_add_epi16(d3, d4);
  119. // Second pass.
  120. const __m128i tmp0 = _mm_add_epi16(a, d);
  121. const __m128i tmp1 = _mm_add_epi16(b, c);
  122. const __m128i tmp2 = _mm_sub_epi16(b, c);
  123. const __m128i tmp3 = _mm_sub_epi16(a, d);
  124. const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
  125. const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
  126. const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
  127. const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
  128. // Transpose the two 4x4.
  129. VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,
  130. &T2, &T3);
  131. }
  132. // Add inverse transform to 'dst' and store.
  133. {
  134. const __m128i zero = _mm_setzero_si128();
  135. // Load the reference(s).
  136. __m128i dst0, dst1, dst2, dst3;
  137. if (do_two) {
  138. // Load eight bytes/pixels per line.
  139. dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS));
  140. dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS));
  141. dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS));
  142. dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS));
  143. } else {
  144. // Load four bytes/pixels per line.
  145. dst0 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 0 * BPS));
  146. dst1 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 1 * BPS));
  147. dst2 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 2 * BPS));
  148. dst3 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 3 * BPS));
  149. }
  150. // Convert to 16b.
  151. dst0 = _mm_unpacklo_epi8(dst0, zero);
  152. dst1 = _mm_unpacklo_epi8(dst1, zero);
  153. dst2 = _mm_unpacklo_epi8(dst2, zero);
  154. dst3 = _mm_unpacklo_epi8(dst3, zero);
  155. // Add the inverse transform(s).
  156. dst0 = _mm_add_epi16(dst0, T0);
  157. dst1 = _mm_add_epi16(dst1, T1);
  158. dst2 = _mm_add_epi16(dst2, T2);
  159. dst3 = _mm_add_epi16(dst3, T3);
  160. // Unsigned saturate to 8b.
  161. dst0 = _mm_packus_epi16(dst0, dst0);
  162. dst1 = _mm_packus_epi16(dst1, dst1);
  163. dst2 = _mm_packus_epi16(dst2, dst2);
  164. dst3 = _mm_packus_epi16(dst3, dst3);
  165. // Store the results.
  166. if (do_two) {
  167. // Store eight bytes/pixels per line.
  168. _mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0);
  169. _mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1);
  170. _mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2);
  171. _mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3);
  172. } else {
  173. // Store four bytes/pixels per line.
  174. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));
  175. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));
  176. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));
  177. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));
  178. }
  179. }
  180. }
  181. #if defined(USE_TRANSFORM_AC3)
  182. #define MUL(a, b) (((a) * (b)) >> 16)
  183. static void TransformAC3(const int16_t* in, uint8_t* dst) {
  184. static const int kC1 = 20091 + (1 << 16);
  185. static const int kC2 = 35468;
  186. const __m128i A = _mm_set1_epi16(in[0] + 4);
  187. const __m128i c4 = _mm_set1_epi16(MUL(in[4], kC2));
  188. const __m128i d4 = _mm_set1_epi16(MUL(in[4], kC1));
  189. const int c1 = MUL(in[1], kC2);
  190. const int d1 = MUL(in[1], kC1);
  191. const __m128i CD = _mm_set_epi16(0, 0, 0, 0, -d1, -c1, c1, d1);
  192. const __m128i B = _mm_adds_epi16(A, CD);
  193. const __m128i m0 = _mm_adds_epi16(B, d4);
  194. const __m128i m1 = _mm_adds_epi16(B, c4);
  195. const __m128i m2 = _mm_subs_epi16(B, c4);
  196. const __m128i m3 = _mm_subs_epi16(B, d4);
  197. const __m128i zero = _mm_setzero_si128();
  198. // Load the source pixels.
  199. __m128i dst0 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 0 * BPS));
  200. __m128i dst1 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 1 * BPS));
  201. __m128i dst2 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 2 * BPS));
  202. __m128i dst3 = _mm_cvtsi32_si128(WebPMemToUint32(dst + 3 * BPS));
  203. // Convert to 16b.
  204. dst0 = _mm_unpacklo_epi8(dst0, zero);
  205. dst1 = _mm_unpacklo_epi8(dst1, zero);
  206. dst2 = _mm_unpacklo_epi8(dst2, zero);
  207. dst3 = _mm_unpacklo_epi8(dst3, zero);
  208. // Add the inverse transform.
  209. dst0 = _mm_adds_epi16(dst0, _mm_srai_epi16(m0, 3));
  210. dst1 = _mm_adds_epi16(dst1, _mm_srai_epi16(m1, 3));
  211. dst2 = _mm_adds_epi16(dst2, _mm_srai_epi16(m2, 3));
  212. dst3 = _mm_adds_epi16(dst3, _mm_srai_epi16(m3, 3));
  213. // Unsigned saturate to 8b.
  214. dst0 = _mm_packus_epi16(dst0, dst0);
  215. dst1 = _mm_packus_epi16(dst1, dst1);
  216. dst2 = _mm_packus_epi16(dst2, dst2);
  217. dst3 = _mm_packus_epi16(dst3, dst3);
  218. // Store the results.
  219. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(dst0));
  220. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(dst1));
  221. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(dst2));
  222. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(dst3));
  223. }
  224. #undef MUL
  225. #endif // USE_TRANSFORM_AC3
  226. //------------------------------------------------------------------------------
  227. // Loop Filter (Paragraph 15)
  228. // Compute abs(p - q) = subs(p - q) OR subs(q - p)
  229. #define MM_ABS(p, q) _mm_or_si128( \
  230. _mm_subs_epu8((q), (p)), \
  231. _mm_subs_epu8((p), (q)))
  232. // Shift each byte of "x" by 3 bits while preserving by the sign bit.
  233. static WEBP_INLINE void SignedShift8b(__m128i* const x) {
  234. const __m128i zero = _mm_setzero_si128();
  235. const __m128i lo_0 = _mm_unpacklo_epi8(zero, *x);
  236. const __m128i hi_0 = _mm_unpackhi_epi8(zero, *x);
  237. const __m128i lo_1 = _mm_srai_epi16(lo_0, 3 + 8);
  238. const __m128i hi_1 = _mm_srai_epi16(hi_0, 3 + 8);
  239. *x = _mm_packs_epi16(lo_1, hi_1);
  240. }
  241. #define FLIP_SIGN_BIT2(a, b) { \
  242. a = _mm_xor_si128(a, sign_bit); \
  243. b = _mm_xor_si128(b, sign_bit); \
  244. }
  245. #define FLIP_SIGN_BIT4(a, b, c, d) { \
  246. FLIP_SIGN_BIT2(a, b); \
  247. FLIP_SIGN_BIT2(c, d); \
  248. }
  249. // input/output is uint8_t
  250. static WEBP_INLINE void GetNotHEV(const __m128i* const p1,
  251. const __m128i* const p0,
  252. const __m128i* const q0,
  253. const __m128i* const q1,
  254. int hev_thresh, __m128i* const not_hev) {
  255. const __m128i zero = _mm_setzero_si128();
  256. const __m128i t_1 = MM_ABS(*p1, *p0);
  257. const __m128i t_2 = MM_ABS(*q1, *q0);
  258. const __m128i h = _mm_set1_epi8(hev_thresh);
  259. const __m128i t_max = _mm_max_epu8(t_1, t_2);
  260. const __m128i t_max_h = _mm_subs_epu8(t_max, h);
  261. *not_hev = _mm_cmpeq_epi8(t_max_h, zero); // not_hev <= t1 && not_hev <= t2
  262. }
  263. // input pixels are int8_t
  264. static WEBP_INLINE void GetBaseDelta(const __m128i* const p1,
  265. const __m128i* const p0,
  266. const __m128i* const q0,
  267. const __m128i* const q1,
  268. __m128i* const delta) {
  269. // beware of addition order, for saturation!
  270. const __m128i p1_q1 = _mm_subs_epi8(*p1, *q1); // p1 - q1
  271. const __m128i q0_p0 = _mm_subs_epi8(*q0, *p0); // q0 - p0
  272. const __m128i s1 = _mm_adds_epi8(p1_q1, q0_p0); // p1 - q1 + 1 * (q0 - p0)
  273. const __m128i s2 = _mm_adds_epi8(q0_p0, s1); // p1 - q1 + 2 * (q0 - p0)
  274. const __m128i s3 = _mm_adds_epi8(q0_p0, s2); // p1 - q1 + 3 * (q0 - p0)
  275. *delta = s3;
  276. }
  277. // input and output are int8_t
  278. static WEBP_INLINE void DoSimpleFilter(__m128i* const p0, __m128i* const q0,
  279. const __m128i* const fl) {
  280. const __m128i k3 = _mm_set1_epi8(3);
  281. const __m128i k4 = _mm_set1_epi8(4);
  282. __m128i v3 = _mm_adds_epi8(*fl, k3);
  283. __m128i v4 = _mm_adds_epi8(*fl, k4);
  284. SignedShift8b(&v4); // v4 >> 3
  285. SignedShift8b(&v3); // v3 >> 3
  286. *q0 = _mm_subs_epi8(*q0, v4); // q0 -= v4
  287. *p0 = _mm_adds_epi8(*p0, v3); // p0 += v3
  288. }
  289. // Updates values of 2 pixels at MB edge during complex filtering.
  290. // Update operations:
  291. // q = q - delta and p = p + delta; where delta = [(a_hi >> 7), (a_lo >> 7)]
  292. // Pixels 'pi' and 'qi' are int8_t on input, uint8_t on output (sign flip).
  293. static WEBP_INLINE void Update2Pixels(__m128i* const pi, __m128i* const qi,
  294. const __m128i* const a0_lo,
  295. const __m128i* const a0_hi) {
  296. const __m128i a1_lo = _mm_srai_epi16(*a0_lo, 7);
  297. const __m128i a1_hi = _mm_srai_epi16(*a0_hi, 7);
  298. const __m128i delta = _mm_packs_epi16(a1_lo, a1_hi);
  299. const __m128i sign_bit = _mm_set1_epi8(0x80);
  300. *pi = _mm_adds_epi8(*pi, delta);
  301. *qi = _mm_subs_epi8(*qi, delta);
  302. FLIP_SIGN_BIT2(*pi, *qi);
  303. }
  304. // input pixels are uint8_t
  305. static WEBP_INLINE void NeedsFilter(const __m128i* const p1,
  306. const __m128i* const p0,
  307. const __m128i* const q0,
  308. const __m128i* const q1,
  309. int thresh, __m128i* const mask) {
  310. const __m128i m_thresh = _mm_set1_epi8(thresh);
  311. const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)
  312. const __m128i kFE = _mm_set1_epi8(0xFE);
  313. const __m128i t2 = _mm_and_si128(t1, kFE); // set lsb of each byte to zero
  314. const __m128i t3 = _mm_srli_epi16(t2, 1); // abs(p1 - q1) / 2
  315. const __m128i t4 = MM_ABS(*p0, *q0); // abs(p0 - q0)
  316. const __m128i t5 = _mm_adds_epu8(t4, t4); // abs(p0 - q0) * 2
  317. const __m128i t6 = _mm_adds_epu8(t5, t3); // abs(p0-q0)*2 + abs(p1-q1)/2
  318. const __m128i t7 = _mm_subs_epu8(t6, m_thresh); // mask <= m_thresh
  319. *mask = _mm_cmpeq_epi8(t7, _mm_setzero_si128());
  320. }
  321. //------------------------------------------------------------------------------
  322. // Edge filtering functions
  323. // Applies filter on 2 pixels (p0 and q0)
  324. static WEBP_INLINE void DoFilter2(__m128i* const p1, __m128i* const p0,
  325. __m128i* const q0, __m128i* const q1,
  326. int thresh) {
  327. __m128i a, mask;
  328. const __m128i sign_bit = _mm_set1_epi8(0x80);
  329. // convert p1/q1 to int8_t (for GetBaseDelta)
  330. const __m128i p1s = _mm_xor_si128(*p1, sign_bit);
  331. const __m128i q1s = _mm_xor_si128(*q1, sign_bit);
  332. NeedsFilter(p1, p0, q0, q1, thresh, &mask);
  333. FLIP_SIGN_BIT2(*p0, *q0);
  334. GetBaseDelta(&p1s, p0, q0, &q1s, &a);
  335. a = _mm_and_si128(a, mask); // mask filter values we don't care about
  336. DoSimpleFilter(p0, q0, &a);
  337. FLIP_SIGN_BIT2(*p0, *q0);
  338. }
  339. // Applies filter on 4 pixels (p1, p0, q0 and q1)
  340. static WEBP_INLINE void DoFilter4(__m128i* const p1, __m128i* const p0,
  341. __m128i* const q0, __m128i* const q1,
  342. const __m128i* const mask, int hev_thresh) {
  343. const __m128i zero = _mm_setzero_si128();
  344. const __m128i sign_bit = _mm_set1_epi8(0x80);
  345. const __m128i k64 = _mm_set1_epi8(64);
  346. const __m128i k3 = _mm_set1_epi8(3);
  347. const __m128i k4 = _mm_set1_epi8(4);
  348. __m128i not_hev;
  349. __m128i t1, t2, t3;
  350. // compute hev mask
  351. GetNotHEV(p1, p0, q0, q1, hev_thresh, &not_hev);
  352. // convert to signed values
  353. FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
  354. t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1
  355. t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1)
  356. t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0
  357. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0)
  358. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0)
  359. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0)
  360. t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about
  361. t2 = _mm_adds_epi8(t1, k3); // 3 * (q0 - p0) + hev(p1 - q1) + 3
  362. t3 = _mm_adds_epi8(t1, k4); // 3 * (q0 - p0) + hev(p1 - q1) + 4
  363. SignedShift8b(&t2); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3
  364. SignedShift8b(&t3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3
  365. *p0 = _mm_adds_epi8(*p0, t2); // p0 += t2
  366. *q0 = _mm_subs_epi8(*q0, t3); // q0 -= t3
  367. FLIP_SIGN_BIT2(*p0, *q0);
  368. // this is equivalent to signed (a + 1) >> 1 calculation
  369. t2 = _mm_add_epi8(t3, sign_bit);
  370. t3 = _mm_avg_epu8(t2, zero);
  371. t3 = _mm_sub_epi8(t3, k64);
  372. t3 = _mm_and_si128(not_hev, t3); // if !hev
  373. *q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3
  374. *p1 = _mm_adds_epi8(*p1, t3); // p1 += t3
  375. FLIP_SIGN_BIT2(*p1, *q1);
  376. }
  377. // Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2)
  378. static WEBP_INLINE void DoFilter6(__m128i* const p2, __m128i* const p1,
  379. __m128i* const p0, __m128i* const q0,
  380. __m128i* const q1, __m128i* const q2,
  381. const __m128i* const mask, int hev_thresh) {
  382. const __m128i zero = _mm_setzero_si128();
  383. const __m128i sign_bit = _mm_set1_epi8(0x80);
  384. __m128i a, not_hev;
  385. // compute hev mask
  386. GetNotHEV(p1, p0, q0, q1, hev_thresh, &not_hev);
  387. FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
  388. FLIP_SIGN_BIT2(*p2, *q2);
  389. GetBaseDelta(p1, p0, q0, q1, &a);
  390. { // do simple filter on pixels with hev
  391. const __m128i m = _mm_andnot_si128(not_hev, *mask);
  392. const __m128i f = _mm_and_si128(a, m);
  393. DoSimpleFilter(p0, q0, &f);
  394. }
  395. { // do strong filter on pixels with not hev
  396. const __m128i k9 = _mm_set1_epi16(0x0900);
  397. const __m128i k63 = _mm_set1_epi16(63);
  398. const __m128i m = _mm_and_si128(not_hev, *mask);
  399. const __m128i f = _mm_and_si128(a, m);
  400. const __m128i f_lo = _mm_unpacklo_epi8(zero, f);
  401. const __m128i f_hi = _mm_unpackhi_epi8(zero, f);
  402. const __m128i f9_lo = _mm_mulhi_epi16(f_lo, k9); // Filter (lo) * 9
  403. const __m128i f9_hi = _mm_mulhi_epi16(f_hi, k9); // Filter (hi) * 9
  404. const __m128i a2_lo = _mm_add_epi16(f9_lo, k63); // Filter * 9 + 63
  405. const __m128i a2_hi = _mm_add_epi16(f9_hi, k63); // Filter * 9 + 63
  406. const __m128i a1_lo = _mm_add_epi16(a2_lo, f9_lo); // Filter * 18 + 63
  407. const __m128i a1_hi = _mm_add_epi16(a2_hi, f9_hi); // Filter * 18 + 63
  408. const __m128i a0_lo = _mm_add_epi16(a1_lo, f9_lo); // Filter * 27 + 63
  409. const __m128i a0_hi = _mm_add_epi16(a1_hi, f9_hi); // Filter * 27 + 63
  410. Update2Pixels(p2, q2, &a2_lo, &a2_hi);
  411. Update2Pixels(p1, q1, &a1_lo, &a1_hi);
  412. Update2Pixels(p0, q0, &a0_lo, &a0_hi);
  413. }
  414. }
  415. // reads 8 rows across a vertical edge.
  416. static WEBP_INLINE void Load8x4(const uint8_t* const b, int stride,
  417. __m128i* const p, __m128i* const q) {
  418. // A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00
  419. // A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10
  420. const __m128i A0 = _mm_set_epi32(
  421. WebPMemToUint32(&b[6 * stride]), WebPMemToUint32(&b[2 * stride]),
  422. WebPMemToUint32(&b[4 * stride]), WebPMemToUint32(&b[0 * stride]));
  423. const __m128i A1 = _mm_set_epi32(
  424. WebPMemToUint32(&b[7 * stride]), WebPMemToUint32(&b[3 * stride]),
  425. WebPMemToUint32(&b[5 * stride]), WebPMemToUint32(&b[1 * stride]));
  426. // B0 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00
  427. // B1 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20
  428. const __m128i B0 = _mm_unpacklo_epi8(A0, A1);
  429. const __m128i B1 = _mm_unpackhi_epi8(A0, A1);
  430. // C0 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00
  431. // C1 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40
  432. const __m128i C0 = _mm_unpacklo_epi16(B0, B1);
  433. const __m128i C1 = _mm_unpackhi_epi16(B0, B1);
  434. // *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
  435. // *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
  436. *p = _mm_unpacklo_epi32(C0, C1);
  437. *q = _mm_unpackhi_epi32(C0, C1);
  438. }
  439. static WEBP_INLINE void Load16x4(const uint8_t* const r0,
  440. const uint8_t* const r8,
  441. int stride,
  442. __m128i* const p1, __m128i* const p0,
  443. __m128i* const q0, __m128i* const q1) {
  444. // Assume the pixels around the edge (|) are numbered as follows
  445. // 00 01 | 02 03
  446. // 10 11 | 12 13
  447. // ... | ...
  448. // e0 e1 | e2 e3
  449. // f0 f1 | f2 f3
  450. //
  451. // r0 is pointing to the 0th row (00)
  452. // r8 is pointing to the 8th row (80)
  453. // Load
  454. // p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
  455. // q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
  456. // p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80
  457. // q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82
  458. Load8x4(r0, stride, p1, q0);
  459. Load8x4(r8, stride, p0, q1);
  460. {
  461. // p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00
  462. // p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01
  463. // q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02
  464. // q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03
  465. const __m128i t1 = *p1;
  466. const __m128i t2 = *q0;
  467. *p1 = _mm_unpacklo_epi64(t1, *p0);
  468. *p0 = _mm_unpackhi_epi64(t1, *p0);
  469. *q0 = _mm_unpacklo_epi64(t2, *q1);
  470. *q1 = _mm_unpackhi_epi64(t2, *q1);
  471. }
  472. }
  473. static WEBP_INLINE void Store4x4(__m128i* const x, uint8_t* dst, int stride) {
  474. int i;
  475. for (i = 0; i < 4; ++i, dst += stride) {
  476. WebPUint32ToMem(dst, _mm_cvtsi128_si32(*x));
  477. *x = _mm_srli_si128(*x, 4);
  478. }
  479. }
  480. // Transpose back and store
  481. static WEBP_INLINE void Store16x4(const __m128i* const p1,
  482. const __m128i* const p0,
  483. const __m128i* const q0,
  484. const __m128i* const q1,
  485. uint8_t* r0, uint8_t* r8,
  486. int stride) {
  487. __m128i t1, p1_s, p0_s, q0_s, q1_s;
  488. // p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00
  489. // p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80
  490. t1 = *p0;
  491. p0_s = _mm_unpacklo_epi8(*p1, t1);
  492. p1_s = _mm_unpackhi_epi8(*p1, t1);
  493. // q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02
  494. // q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82
  495. t1 = *q0;
  496. q0_s = _mm_unpacklo_epi8(t1, *q1);
  497. q1_s = _mm_unpackhi_epi8(t1, *q1);
  498. // p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00
  499. // q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40
  500. t1 = p0_s;
  501. p0_s = _mm_unpacklo_epi16(t1, q0_s);
  502. q0_s = _mm_unpackhi_epi16(t1, q0_s);
  503. // p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80
  504. // q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0
  505. t1 = p1_s;
  506. p1_s = _mm_unpacklo_epi16(t1, q1_s);
  507. q1_s = _mm_unpackhi_epi16(t1, q1_s);
  508. Store4x4(&p0_s, r0, stride);
  509. r0 += 4 * stride;
  510. Store4x4(&q0_s, r0, stride);
  511. Store4x4(&p1_s, r8, stride);
  512. r8 += 4 * stride;
  513. Store4x4(&q1_s, r8, stride);
  514. }
  515. //------------------------------------------------------------------------------
  516. // Simple In-loop filtering (Paragraph 15.2)
  517. static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
  518. // Load
  519. __m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]);
  520. __m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]);
  521. __m128i q0 = _mm_loadu_si128((__m128i*)&p[0]);
  522. __m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]);
  523. DoFilter2(&p1, &p0, &q0, &q1, thresh);
  524. // Store
  525. _mm_storeu_si128((__m128i*)&p[-stride], p0);
  526. _mm_storeu_si128((__m128i*)&p[0], q0);
  527. }
  528. static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
  529. __m128i p1, p0, q0, q1;
  530. p -= 2; // beginning of p1
  531. Load16x4(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);
  532. DoFilter2(&p1, &p0, &q0, &q1, thresh);
  533. Store16x4(&p1, &p0, &q0, &q1, p, p + 8 * stride, stride);
  534. }
  535. static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
  536. int k;
  537. for (k = 3; k > 0; --k) {
  538. p += 4 * stride;
  539. SimpleVFilter16(p, stride, thresh);
  540. }
  541. }
  542. static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
  543. int k;
  544. for (k = 3; k > 0; --k) {
  545. p += 4;
  546. SimpleHFilter16(p, stride, thresh);
  547. }
  548. }
  549. //------------------------------------------------------------------------------
  550. // Complex In-loop filtering (Paragraph 15.3)
  551. #define MAX_DIFF1(p3, p2, p1, p0, m) do { \
  552. m = MM_ABS(p1, p0); \
  553. m = _mm_max_epu8(m, MM_ABS(p3, p2)); \
  554. m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
  555. } while (0)
  556. #define MAX_DIFF2(p3, p2, p1, p0, m) do { \
  557. m = _mm_max_epu8(m, MM_ABS(p1, p0)); \
  558. m = _mm_max_epu8(m, MM_ABS(p3, p2)); \
  559. m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
  560. } while (0)
  561. #define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) { \
  562. e1 = _mm_loadu_si128((__m128i*)&(p)[0 * stride]); \
  563. e2 = _mm_loadu_si128((__m128i*)&(p)[1 * stride]); \
  564. e3 = _mm_loadu_si128((__m128i*)&(p)[2 * stride]); \
  565. e4 = _mm_loadu_si128((__m128i*)&(p)[3 * stride]); \
  566. }
  567. #define LOADUV_H_EDGE(p, u, v, stride) do { \
  568. const __m128i U = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \
  569. const __m128i V = _mm_loadl_epi64((__m128i*)&(v)[(stride)]); \
  570. p = _mm_unpacklo_epi64(U, V); \
  571. } while (0)
  572. #define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) { \
  573. LOADUV_H_EDGE(e1, u, v, 0 * stride); \
  574. LOADUV_H_EDGE(e2, u, v, 1 * stride); \
  575. LOADUV_H_EDGE(e3, u, v, 2 * stride); \
  576. LOADUV_H_EDGE(e4, u, v, 3 * stride); \
  577. }
  578. #define STOREUV(p, u, v, stride) { \
  579. _mm_storel_epi64((__m128i*)&u[(stride)], p); \
  580. p = _mm_srli_si128(p, 8); \
  581. _mm_storel_epi64((__m128i*)&v[(stride)], p); \
  582. }
  583. static WEBP_INLINE void ComplexMask(const __m128i* const p1,
  584. const __m128i* const p0,
  585. const __m128i* const q0,
  586. const __m128i* const q1,
  587. int thresh, int ithresh,
  588. __m128i* const mask) {
  589. const __m128i it = _mm_set1_epi8(ithresh);
  590. const __m128i diff = _mm_subs_epu8(*mask, it);
  591. const __m128i thresh_mask = _mm_cmpeq_epi8(diff, _mm_setzero_si128());
  592. __m128i filter_mask;
  593. NeedsFilter(p1, p0, q0, q1, thresh, &filter_mask);
  594. *mask = _mm_and_si128(thresh_mask, filter_mask);
  595. }
  596. // on macroblock edges
  597. static void VFilter16(uint8_t* p, int stride,
  598. int thresh, int ithresh, int hev_thresh) {
  599. __m128i t1;
  600. __m128i mask;
  601. __m128i p2, p1, p0, q0, q1, q2;
  602. // Load p3, p2, p1, p0
  603. LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0);
  604. MAX_DIFF1(t1, p2, p1, p0, mask);
  605. // Load q0, q1, q2, q3
  606. LOAD_H_EDGES4(p, stride, q0, q1, q2, t1);
  607. MAX_DIFF2(t1, q2, q1, q0, mask);
  608. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  609. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  610. // Store
  611. _mm_storeu_si128((__m128i*)&p[-3 * stride], p2);
  612. _mm_storeu_si128((__m128i*)&p[-2 * stride], p1);
  613. _mm_storeu_si128((__m128i*)&p[-1 * stride], p0);
  614. _mm_storeu_si128((__m128i*)&p[+0 * stride], q0);
  615. _mm_storeu_si128((__m128i*)&p[+1 * stride], q1);
  616. _mm_storeu_si128((__m128i*)&p[+2 * stride], q2);
  617. }
  618. static void HFilter16(uint8_t* p, int stride,
  619. int thresh, int ithresh, int hev_thresh) {
  620. __m128i mask;
  621. __m128i p3, p2, p1, p0, q0, q1, q2, q3;
  622. uint8_t* const b = p - 4;
  623. Load16x4(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
  624. MAX_DIFF1(p3, p2, p1, p0, mask);
  625. Load16x4(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
  626. MAX_DIFF2(q3, q2, q1, q0, mask);
  627. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  628. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  629. Store16x4(&p3, &p2, &p1, &p0, b, b + 8 * stride, stride);
  630. Store16x4(&q0, &q1, &q2, &q3, p, p + 8 * stride, stride);
  631. }
  632. // on three inner edges
  633. static void VFilter16i(uint8_t* p, int stride,
  634. int thresh, int ithresh, int hev_thresh) {
  635. int k;
  636. __m128i p3, p2, p1, p0; // loop invariants
  637. LOAD_H_EDGES4(p, stride, p3, p2, p1, p0); // prologue
  638. for (k = 3; k > 0; --k) {
  639. __m128i mask, tmp1, tmp2;
  640. uint8_t* const b = p + 2 * stride; // beginning of p1
  641. p += 4 * stride;
  642. MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask
  643. LOAD_H_EDGES4(p, stride, p3, p2, tmp1, tmp2);
  644. MAX_DIFF2(p3, p2, tmp1, tmp2, mask);
  645. // p3 and p2 are not just temporary variables here: they will be
  646. // re-used for next span. And q2/q3 will become p1/p0 accordingly.
  647. ComplexMask(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);
  648. DoFilter4(&p1, &p0, &p3, &p2, &mask, hev_thresh);
  649. // Store
  650. _mm_storeu_si128((__m128i*)&b[0 * stride], p1);
  651. _mm_storeu_si128((__m128i*)&b[1 * stride], p0);
  652. _mm_storeu_si128((__m128i*)&b[2 * stride], p3);
  653. _mm_storeu_si128((__m128i*)&b[3 * stride], p2);
  654. // rotate samples
  655. p1 = tmp1;
  656. p0 = tmp2;
  657. }
  658. }
  659. static void HFilter16i(uint8_t* p, int stride,
  660. int thresh, int ithresh, int hev_thresh) {
  661. int k;
  662. __m128i p3, p2, p1, p0; // loop invariants
  663. Load16x4(p, p + 8 * stride, stride, &p3, &p2, &p1, &p0); // prologue
  664. for (k = 3; k > 0; --k) {
  665. __m128i mask, tmp1, tmp2;
  666. uint8_t* const b = p + 2; // beginning of p1
  667. p += 4; // beginning of q0 (and next span)
  668. MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask
  669. Load16x4(p, p + 8 * stride, stride, &p3, &p2, &tmp1, &tmp2);
  670. MAX_DIFF2(p3, p2, tmp1, tmp2, mask);
  671. ComplexMask(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);
  672. DoFilter4(&p1, &p0, &p3, &p2, &mask, hev_thresh);
  673. Store16x4(&p1, &p0, &p3, &p2, b, b + 8 * stride, stride);
  674. // rotate samples
  675. p1 = tmp1;
  676. p0 = tmp2;
  677. }
  678. }
  679. // 8-pixels wide variant, for chroma filtering
  680. static void VFilter8(uint8_t* u, uint8_t* v, int stride,
  681. int thresh, int ithresh, int hev_thresh) {
  682. __m128i mask;
  683. __m128i t1, p2, p1, p0, q0, q1, q2;
  684. // Load p3, p2, p1, p0
  685. LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0);
  686. MAX_DIFF1(t1, p2, p1, p0, mask);
  687. // Load q0, q1, q2, q3
  688. LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1);
  689. MAX_DIFF2(t1, q2, q1, q0, mask);
  690. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  691. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  692. // Store
  693. STOREUV(p2, u, v, -3 * stride);
  694. STOREUV(p1, u, v, -2 * stride);
  695. STOREUV(p0, u, v, -1 * stride);
  696. STOREUV(q0, u, v, 0 * stride);
  697. STOREUV(q1, u, v, 1 * stride);
  698. STOREUV(q2, u, v, 2 * stride);
  699. }
  700. static void HFilter8(uint8_t* u, uint8_t* v, int stride,
  701. int thresh, int ithresh, int hev_thresh) {
  702. __m128i mask;
  703. __m128i p3, p2, p1, p0, q0, q1, q2, q3;
  704. uint8_t* const tu = u - 4;
  705. uint8_t* const tv = v - 4;
  706. Load16x4(tu, tv, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
  707. MAX_DIFF1(p3, p2, p1, p0, mask);
  708. Load16x4(u, v, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
  709. MAX_DIFF2(q3, q2, q1, q0, mask);
  710. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  711. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  712. Store16x4(&p3, &p2, &p1, &p0, tu, tv, stride);
  713. Store16x4(&q0, &q1, &q2, &q3, u, v, stride);
  714. }
  715. static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
  716. int thresh, int ithresh, int hev_thresh) {
  717. __m128i mask;
  718. __m128i t1, t2, p1, p0, q0, q1;
  719. // Load p3, p2, p1, p0
  720. LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);
  721. MAX_DIFF1(t2, t1, p1, p0, mask);
  722. u += 4 * stride;
  723. v += 4 * stride;
  724. // Load q0, q1, q2, q3
  725. LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);
  726. MAX_DIFF2(t2, t1, q1, q0, mask);
  727. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  728. DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
  729. // Store
  730. STOREUV(p1, u, v, -2 * stride);
  731. STOREUV(p0, u, v, -1 * stride);
  732. STOREUV(q0, u, v, 0 * stride);
  733. STOREUV(q1, u, v, 1 * stride);
  734. }
  735. static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
  736. int thresh, int ithresh, int hev_thresh) {
  737. __m128i mask;
  738. __m128i t1, t2, p1, p0, q0, q1;
  739. Load16x4(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0
  740. MAX_DIFF1(t2, t1, p1, p0, mask);
  741. u += 4; // beginning of q0
  742. v += 4;
  743. Load16x4(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3
  744. MAX_DIFF2(t2, t1, q1, q0, mask);
  745. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  746. DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
  747. u -= 2; // beginning of p1
  748. v -= 2;
  749. Store16x4(&p1, &p0, &q0, &q1, u, v, stride);
  750. }
  751. //------------------------------------------------------------------------------
  752. // 4x4 predictions
  753. #define DST(x, y) dst[(x) + (y) * BPS]
  754. #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
  755. // We use the following 8b-arithmetic tricks:
  756. // (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1
  757. // where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]
  758. // and:
  759. // (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb
  760. // where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1
  761. // and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1
  762. static void VE4(uint8_t* dst) { // vertical
  763. const __m128i one = _mm_set1_epi8(1);
  764. const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));
  765. const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
  766. const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
  767. const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);
  768. const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);
  769. const __m128i b = _mm_subs_epu8(a, lsb);
  770. const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);
  771. const uint32_t vals = _mm_cvtsi128_si32(avg);
  772. int i;
  773. for (i = 0; i < 4; ++i) {
  774. WebPUint32ToMem(dst + i * BPS, vals);
  775. }
  776. }
  777. static void LD4(uint8_t* dst) { // Down-Left
  778. const __m128i one = _mm_set1_epi8(1);
  779. const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));
  780. const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
  781. const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
  782. const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, dst[-BPS + 7], 3);
  783. const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);
  784. const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);
  785. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  786. const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);
  787. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));
  788. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
  789. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
  790. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
  791. }
  792. static void VR4(uint8_t* dst) { // Vertical-Right
  793. const __m128i one = _mm_set1_epi8(1);
  794. const int I = dst[-1 + 0 * BPS];
  795. const int J = dst[-1 + 1 * BPS];
  796. const int K = dst[-1 + 2 * BPS];
  797. const int X = dst[-1 - BPS];
  798. const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));
  799. const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);
  800. const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);
  801. const __m128i _XABCD = _mm_slli_si128(XABCD, 1);
  802. const __m128i IXABCD = _mm_insert_epi16(_XABCD, I | (X << 8), 0);
  803. const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);
  804. const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);
  805. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  806. const __m128i efgh = _mm_avg_epu8(avg2, XABCD);
  807. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));
  808. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));
  809. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));
  810. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));
  811. // these two are hard to implement in SSE2, so we keep the C-version:
  812. DST(0, 2) = AVG3(J, I, X);
  813. DST(0, 3) = AVG3(K, J, I);
  814. }
  815. static void VL4(uint8_t* dst) { // Vertical-Left
  816. const __m128i one = _mm_set1_epi8(1);
  817. const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(dst - BPS));
  818. const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);
  819. const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);
  820. const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);
  821. const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);
  822. const __m128i avg3 = _mm_avg_epu8(avg1, avg2);
  823. const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);
  824. const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);
  825. const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);
  826. const __m128i abbc = _mm_or_si128(ab, bc);
  827. const __m128i lsb2 = _mm_and_si128(abbc, lsb1);
  828. const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);
  829. const uint32_t extra_out = _mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));
  830. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));
  831. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));
  832. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));
  833. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));
  834. // these two are hard to get and irregular
  835. DST(3, 2) = (extra_out >> 0) & 0xff;
  836. DST(3, 3) = (extra_out >> 8) & 0xff;
  837. }
  838. static void RD4(uint8_t* dst) { // Down-right
  839. const __m128i one = _mm_set1_epi8(1);
  840. const __m128i XABCD = _mm_loadl_epi64((__m128i*)(dst - BPS - 1));
  841. const __m128i ____XABCD = _mm_slli_si128(XABCD, 4);
  842. const uint32_t I = dst[-1 + 0 * BPS];
  843. const uint32_t J = dst[-1 + 1 * BPS];
  844. const uint32_t K = dst[-1 + 2 * BPS];
  845. const uint32_t L = dst[-1 + 3 * BPS];
  846. const __m128i LKJI_____ =
  847. _mm_cvtsi32_si128(L | (K << 8) | (J << 16) | (I << 24));
  848. const __m128i LKJIXABCD = _mm_or_si128(LKJI_____, ____XABCD);
  849. const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);
  850. const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);
  851. const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);
  852. const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);
  853. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  854. const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);
  855. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));
  856. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
  857. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
  858. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
  859. }
  860. #undef DST
  861. #undef AVG3
  862. //------------------------------------------------------------------------------
  863. // Luma 16x16
  864. static WEBP_INLINE void TrueMotion(uint8_t* dst, int size) {
  865. const uint8_t* top = dst - BPS;
  866. const __m128i zero = _mm_setzero_si128();
  867. int y;
  868. if (size == 4) {
  869. const __m128i top_values = _mm_cvtsi32_si128(WebPMemToUint32(top));
  870. const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
  871. for (y = 0; y < 4; ++y, dst += BPS) {
  872. const int val = dst[-1] - top[-1];
  873. const __m128i base = _mm_set1_epi16(val);
  874. const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
  875. WebPUint32ToMem(dst, _mm_cvtsi128_si32(out));
  876. }
  877. } else if (size == 8) {
  878. const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
  879. const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
  880. for (y = 0; y < 8; ++y, dst += BPS) {
  881. const int val = dst[-1] - top[-1];
  882. const __m128i base = _mm_set1_epi16(val);
  883. const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
  884. _mm_storel_epi64((__m128i*)dst, out);
  885. }
  886. } else {
  887. const __m128i top_values = _mm_loadu_si128((const __m128i*)top);
  888. const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);
  889. const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);
  890. for (y = 0; y < 16; ++y, dst += BPS) {
  891. const int val = dst[-1] - top[-1];
  892. const __m128i base = _mm_set1_epi16(val);
  893. const __m128i out_0 = _mm_add_epi16(base, top_base_0);
  894. const __m128i out_1 = _mm_add_epi16(base, top_base_1);
  895. const __m128i out = _mm_packus_epi16(out_0, out_1);
  896. _mm_storeu_si128((__m128i*)dst, out);
  897. }
  898. }
  899. }
  900. static void TM4(uint8_t* dst) { TrueMotion(dst, 4); }
  901. static void TM8uv(uint8_t* dst) { TrueMotion(dst, 8); }
  902. static void TM16(uint8_t* dst) { TrueMotion(dst, 16); }
  903. static void VE16(uint8_t* dst) {
  904. const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));
  905. int j;
  906. for (j = 0; j < 16; ++j) {
  907. _mm_storeu_si128((__m128i*)(dst + j * BPS), top);
  908. }
  909. }
  910. static void HE16(uint8_t* dst) { // horizontal
  911. int j;
  912. for (j = 16; j > 0; --j) {
  913. const __m128i values = _mm_set1_epi8(dst[-1]);
  914. _mm_storeu_si128((__m128i*)dst, values);
  915. dst += BPS;
  916. }
  917. }
  918. static WEBP_INLINE void Put16(uint8_t v, uint8_t* dst) {
  919. int j;
  920. const __m128i values = _mm_set1_epi8(v);
  921. for (j = 0; j < 16; ++j) {
  922. _mm_storeu_si128((__m128i*)(dst + j * BPS), values);
  923. }
  924. }
  925. static void DC16(uint8_t* dst) { // DC
  926. const __m128i zero = _mm_setzero_si128();
  927. const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));
  928. const __m128i sad8x2 = _mm_sad_epu8(top, zero);
  929. // sum the two sads: sad8x2[0:1] + sad8x2[8:9]
  930. const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));
  931. int left = 0;
  932. int j;
  933. for (j = 0; j < 16; ++j) {
  934. left += dst[-1 + j * BPS];
  935. }
  936. {
  937. const int DC = _mm_cvtsi128_si32(sum) + left + 16;
  938. Put16(DC >> 5, dst);
  939. }
  940. }
  941. static void DC16NoTop(uint8_t* dst) { // DC with top samples not available
  942. int DC = 8;
  943. int j;
  944. for (j = 0; j < 16; ++j) {
  945. DC += dst[-1 + j * BPS];
  946. }
  947. Put16(DC >> 4, dst);
  948. }
  949. static void DC16NoLeft(uint8_t* dst) { // DC with left samples not available
  950. const __m128i zero = _mm_setzero_si128();
  951. const __m128i top = _mm_loadu_si128((const __m128i*)(dst - BPS));
  952. const __m128i sad8x2 = _mm_sad_epu8(top, zero);
  953. // sum the two sads: sad8x2[0:1] + sad8x2[8:9]
  954. const __m128i sum = _mm_add_epi16(sad8x2, _mm_shuffle_epi32(sad8x2, 2));
  955. const int DC = _mm_cvtsi128_si32(sum) + 8;
  956. Put16(DC >> 4, dst);
  957. }
  958. static void DC16NoTopLeft(uint8_t* dst) { // DC with no top and left samples
  959. Put16(0x80, dst);
  960. }
  961. //------------------------------------------------------------------------------
  962. // Chroma
  963. static void VE8uv(uint8_t* dst) { // vertical
  964. int j;
  965. const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));
  966. for (j = 0; j < 8; ++j) {
  967. _mm_storel_epi64((__m128i*)(dst + j * BPS), top);
  968. }
  969. }
  970. static void HE8uv(uint8_t* dst) { // horizontal
  971. int j;
  972. for (j = 0; j < 8; ++j) {
  973. const __m128i values = _mm_set1_epi8(dst[-1]);
  974. _mm_storel_epi64((__m128i*)dst, values);
  975. dst += BPS;
  976. }
  977. }
  978. // helper for chroma-DC predictions
  979. static WEBP_INLINE void Put8x8uv(uint8_t v, uint8_t* dst) {
  980. int j;
  981. const __m128i values = _mm_set1_epi8(v);
  982. for (j = 0; j < 8; ++j) {
  983. _mm_storel_epi64((__m128i*)(dst + j * BPS), values);
  984. }
  985. }
  986. static void DC8uv(uint8_t* dst) { // DC
  987. const __m128i zero = _mm_setzero_si128();
  988. const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));
  989. const __m128i sum = _mm_sad_epu8(top, zero);
  990. int left = 0;
  991. int j;
  992. for (j = 0; j < 8; ++j) {
  993. left += dst[-1 + j * BPS];
  994. }
  995. {
  996. const int DC = _mm_cvtsi128_si32(sum) + left + 8;
  997. Put8x8uv(DC >> 4, dst);
  998. }
  999. }
  1000. static void DC8uvNoLeft(uint8_t* dst) { // DC with no left samples
  1001. const __m128i zero = _mm_setzero_si128();
  1002. const __m128i top = _mm_loadl_epi64((const __m128i*)(dst - BPS));
  1003. const __m128i sum = _mm_sad_epu8(top, zero);
  1004. const int DC = _mm_cvtsi128_si32(sum) + 4;
  1005. Put8x8uv(DC >> 3, dst);
  1006. }
  1007. static void DC8uvNoTop(uint8_t* dst) { // DC with no top samples
  1008. int dc0 = 4;
  1009. int i;
  1010. for (i = 0; i < 8; ++i) {
  1011. dc0 += dst[-1 + i * BPS];
  1012. }
  1013. Put8x8uv(dc0 >> 3, dst);
  1014. }
  1015. static void DC8uvNoTopLeft(uint8_t* dst) { // DC with nothing
  1016. Put8x8uv(0x80, dst);
  1017. }
  1018. //------------------------------------------------------------------------------
  1019. // Entry point
  1020. extern void VP8DspInitSSE2(void);
  1021. WEBP_TSAN_IGNORE_FUNCTION void VP8DspInitSSE2(void) {
  1022. VP8Transform = Transform;
  1023. #if defined(USE_TRANSFORM_AC3)
  1024. VP8TransformAC3 = TransformAC3;
  1025. #endif
  1026. VP8VFilter16 = VFilter16;
  1027. VP8HFilter16 = HFilter16;
  1028. VP8VFilter8 = VFilter8;
  1029. VP8HFilter8 = HFilter8;
  1030. VP8VFilter16i = VFilter16i;
  1031. VP8HFilter16i = HFilter16i;
  1032. VP8VFilter8i = VFilter8i;
  1033. VP8HFilter8i = HFilter8i;
  1034. VP8SimpleVFilter16 = SimpleVFilter16;
  1035. VP8SimpleHFilter16 = SimpleHFilter16;
  1036. VP8SimpleVFilter16i = SimpleVFilter16i;
  1037. VP8SimpleHFilter16i = SimpleHFilter16i;
  1038. VP8PredLuma4[1] = TM4;
  1039. VP8PredLuma4[2] = VE4;
  1040. VP8PredLuma4[4] = RD4;
  1041. VP8PredLuma4[5] = VR4;
  1042. VP8PredLuma4[6] = LD4;
  1043. VP8PredLuma4[7] = VL4;
  1044. VP8PredLuma16[0] = DC16;
  1045. VP8PredLuma16[1] = TM16;
  1046. VP8PredLuma16[2] = VE16;
  1047. VP8PredLuma16[3] = HE16;
  1048. VP8PredLuma16[4] = DC16NoTop;
  1049. VP8PredLuma16[5] = DC16NoLeft;
  1050. VP8PredLuma16[6] = DC16NoTopLeft;
  1051. VP8PredChroma8[0] = DC8uv;
  1052. VP8PredChroma8[1] = TM8uv;
  1053. VP8PredChroma8[2] = VE8uv;
  1054. VP8PredChroma8[3] = HE8uv;
  1055. VP8PredChroma8[4] = DC8uvNoTop;
  1056. VP8PredChroma8[5] = DC8uvNoLeft;
  1057. VP8PredChroma8[6] = DC8uvNoTopLeft;
  1058. }
  1059. #else // !WEBP_USE_SSE2
  1060. WEBP_DSP_INIT_STUB(VP8DspInitSSE2)
  1061. #endif // WEBP_USE_SSE2