dec_sse2.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 "../dec/vp8i.h"
  21. //------------------------------------------------------------------------------
  22. // Transforms (Paragraph 14.4)
  23. static void Transform(const int16_t* in, uint8_t* dst, int do_two) {
  24. // This implementation makes use of 16-bit fixed point versions of two
  25. // multiply constants:
  26. // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
  27. // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
  28. //
  29. // To be able to use signed 16-bit integers, we use the following trick to
  30. // have constants within range:
  31. // - Associated constants are obtained by subtracting the 16-bit fixed point
  32. // version of one:
  33. // k = K - (1 << 16) => K = k + (1 << 16)
  34. // K1 = 85267 => k1 = 20091
  35. // K2 = 35468 => k2 = -30068
  36. // - The multiplication of a variable by a constant become the sum of the
  37. // variable and the multiplication of that variable by the associated
  38. // constant:
  39. // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
  40. const __m128i k1 = _mm_set1_epi16(20091);
  41. const __m128i k2 = _mm_set1_epi16(-30068);
  42. __m128i T0, T1, T2, T3;
  43. // Load and concatenate the transform coefficients (we'll do two transforms
  44. // in parallel). In the case of only one transform, the second half of the
  45. // vectors will just contain random value we'll never use nor store.
  46. __m128i in0, in1, in2, in3;
  47. {
  48. in0 = _mm_loadl_epi64((__m128i*)&in[0]);
  49. in1 = _mm_loadl_epi64((__m128i*)&in[4]);
  50. in2 = _mm_loadl_epi64((__m128i*)&in[8]);
  51. in3 = _mm_loadl_epi64((__m128i*)&in[12]);
  52. // a00 a10 a20 a30 x x x x
  53. // a01 a11 a21 a31 x x x x
  54. // a02 a12 a22 a32 x x x x
  55. // a03 a13 a23 a33 x x x x
  56. if (do_two) {
  57. const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]);
  58. const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]);
  59. const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]);
  60. const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]);
  61. in0 = _mm_unpacklo_epi64(in0, inB0);
  62. in1 = _mm_unpacklo_epi64(in1, inB1);
  63. in2 = _mm_unpacklo_epi64(in2, inB2);
  64. in3 = _mm_unpacklo_epi64(in3, inB3);
  65. // a00 a10 a20 a30 b00 b10 b20 b30
  66. // a01 a11 a21 a31 b01 b11 b21 b31
  67. // a02 a12 a22 a32 b02 b12 b22 b32
  68. // a03 a13 a23 a33 b03 b13 b23 b33
  69. }
  70. }
  71. // Vertical pass and subsequent transpose.
  72. {
  73. // First pass, c and d calculations are longer because of the "trick"
  74. // multiplications.
  75. const __m128i a = _mm_add_epi16(in0, in2);
  76. const __m128i b = _mm_sub_epi16(in0, in2);
  77. // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
  78. const __m128i c1 = _mm_mulhi_epi16(in1, k2);
  79. const __m128i c2 = _mm_mulhi_epi16(in3, k1);
  80. const __m128i c3 = _mm_sub_epi16(in1, in3);
  81. const __m128i c4 = _mm_sub_epi16(c1, c2);
  82. const __m128i c = _mm_add_epi16(c3, c4);
  83. // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
  84. const __m128i d1 = _mm_mulhi_epi16(in1, k1);
  85. const __m128i d2 = _mm_mulhi_epi16(in3, k2);
  86. const __m128i d3 = _mm_add_epi16(in1, in3);
  87. const __m128i d4 = _mm_add_epi16(d1, d2);
  88. const __m128i d = _mm_add_epi16(d3, d4);
  89. // Second pass.
  90. const __m128i tmp0 = _mm_add_epi16(a, d);
  91. const __m128i tmp1 = _mm_add_epi16(b, c);
  92. const __m128i tmp2 = _mm_sub_epi16(b, c);
  93. const __m128i tmp3 = _mm_sub_epi16(a, d);
  94. // Transpose the two 4x4.
  95. // a00 a01 a02 a03 b00 b01 b02 b03
  96. // a10 a11 a12 a13 b10 b11 b12 b13
  97. // a20 a21 a22 a23 b20 b21 b22 b23
  98. // a30 a31 a32 a33 b30 b31 b32 b33
  99. const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1);
  100. const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3);
  101. const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1);
  102. const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3);
  103. // a00 a10 a01 a11 a02 a12 a03 a13
  104. // a20 a30 a21 a31 a22 a32 a23 a33
  105. // b00 b10 b01 b11 b02 b12 b03 b13
  106. // b20 b30 b21 b31 b22 b32 b23 b33
  107. const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
  108. const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
  109. const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
  110. const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
  111. // a00 a10 a20 a30 a01 a11 a21 a31
  112. // b00 b10 b20 b30 b01 b11 b21 b31
  113. // a02 a12 a22 a32 a03 a13 a23 a33
  114. // b02 b12 a22 b32 b03 b13 b23 b33
  115. T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
  116. T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
  117. T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
  118. T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
  119. // a00 a10 a20 a30 b00 b10 b20 b30
  120. // a01 a11 a21 a31 b01 b11 b21 b31
  121. // a02 a12 a22 a32 b02 b12 b22 b32
  122. // a03 a13 a23 a33 b03 b13 b23 b33
  123. }
  124. // Horizontal pass and subsequent transpose.
  125. {
  126. // First pass, c and d calculations are longer because of the "trick"
  127. // multiplications.
  128. const __m128i four = _mm_set1_epi16(4);
  129. const __m128i dc = _mm_add_epi16(T0, four);
  130. const __m128i a = _mm_add_epi16(dc, T2);
  131. const __m128i b = _mm_sub_epi16(dc, T2);
  132. // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
  133. const __m128i c1 = _mm_mulhi_epi16(T1, k2);
  134. const __m128i c2 = _mm_mulhi_epi16(T3, k1);
  135. const __m128i c3 = _mm_sub_epi16(T1, T3);
  136. const __m128i c4 = _mm_sub_epi16(c1, c2);
  137. const __m128i c = _mm_add_epi16(c3, c4);
  138. // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
  139. const __m128i d1 = _mm_mulhi_epi16(T1, k1);
  140. const __m128i d2 = _mm_mulhi_epi16(T3, k2);
  141. const __m128i d3 = _mm_add_epi16(T1, T3);
  142. const __m128i d4 = _mm_add_epi16(d1, d2);
  143. const __m128i d = _mm_add_epi16(d3, d4);
  144. // Second pass.
  145. const __m128i tmp0 = _mm_add_epi16(a, d);
  146. const __m128i tmp1 = _mm_add_epi16(b, c);
  147. const __m128i tmp2 = _mm_sub_epi16(b, c);
  148. const __m128i tmp3 = _mm_sub_epi16(a, d);
  149. const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
  150. const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
  151. const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
  152. const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
  153. // Transpose the two 4x4.
  154. // a00 a01 a02 a03 b00 b01 b02 b03
  155. // a10 a11 a12 a13 b10 b11 b12 b13
  156. // a20 a21 a22 a23 b20 b21 b22 b23
  157. // a30 a31 a32 a33 b30 b31 b32 b33
  158. const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1);
  159. const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3);
  160. const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1);
  161. const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3);
  162. // a00 a10 a01 a11 a02 a12 a03 a13
  163. // a20 a30 a21 a31 a22 a32 a23 a33
  164. // b00 b10 b01 b11 b02 b12 b03 b13
  165. // b20 b30 b21 b31 b22 b32 b23 b33
  166. const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
  167. const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
  168. const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
  169. const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
  170. // a00 a10 a20 a30 a01 a11 a21 a31
  171. // b00 b10 b20 b30 b01 b11 b21 b31
  172. // a02 a12 a22 a32 a03 a13 a23 a33
  173. // b02 b12 a22 b32 b03 b13 b23 b33
  174. T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
  175. T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
  176. T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
  177. T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
  178. // a00 a10 a20 a30 b00 b10 b20 b30
  179. // a01 a11 a21 a31 b01 b11 b21 b31
  180. // a02 a12 a22 a32 b02 b12 b22 b32
  181. // a03 a13 a23 a33 b03 b13 b23 b33
  182. }
  183. // Add inverse transform to 'dst' and store.
  184. {
  185. const __m128i zero = _mm_setzero_si128();
  186. // Load the reference(s).
  187. __m128i dst0, dst1, dst2, dst3;
  188. if (do_two) {
  189. // Load eight bytes/pixels per line.
  190. dst0 = _mm_loadl_epi64((__m128i*)(dst + 0 * BPS));
  191. dst1 = _mm_loadl_epi64((__m128i*)(dst + 1 * BPS));
  192. dst2 = _mm_loadl_epi64((__m128i*)(dst + 2 * BPS));
  193. dst3 = _mm_loadl_epi64((__m128i*)(dst + 3 * BPS));
  194. } else {
  195. // Load four bytes/pixels per line.
  196. dst0 = _mm_cvtsi32_si128(*(int*)(dst + 0 * BPS));
  197. dst1 = _mm_cvtsi32_si128(*(int*)(dst + 1 * BPS));
  198. dst2 = _mm_cvtsi32_si128(*(int*)(dst + 2 * BPS));
  199. dst3 = _mm_cvtsi32_si128(*(int*)(dst + 3 * BPS));
  200. }
  201. // Convert to 16b.
  202. dst0 = _mm_unpacklo_epi8(dst0, zero);
  203. dst1 = _mm_unpacklo_epi8(dst1, zero);
  204. dst2 = _mm_unpacklo_epi8(dst2, zero);
  205. dst3 = _mm_unpacklo_epi8(dst3, zero);
  206. // Add the inverse transform(s).
  207. dst0 = _mm_add_epi16(dst0, T0);
  208. dst1 = _mm_add_epi16(dst1, T1);
  209. dst2 = _mm_add_epi16(dst2, T2);
  210. dst3 = _mm_add_epi16(dst3, T3);
  211. // Unsigned saturate to 8b.
  212. dst0 = _mm_packus_epi16(dst0, dst0);
  213. dst1 = _mm_packus_epi16(dst1, dst1);
  214. dst2 = _mm_packus_epi16(dst2, dst2);
  215. dst3 = _mm_packus_epi16(dst3, dst3);
  216. // Store the results.
  217. if (do_two) {
  218. // Store eight bytes/pixels per line.
  219. _mm_storel_epi64((__m128i*)(dst + 0 * BPS), dst0);
  220. _mm_storel_epi64((__m128i*)(dst + 1 * BPS), dst1);
  221. _mm_storel_epi64((__m128i*)(dst + 2 * BPS), dst2);
  222. _mm_storel_epi64((__m128i*)(dst + 3 * BPS), dst3);
  223. } else {
  224. // Store four bytes/pixels per line.
  225. *(int*)(dst + 0 * BPS) = _mm_cvtsi128_si32(dst0);
  226. *(int*)(dst + 1 * BPS) = _mm_cvtsi128_si32(dst1);
  227. *(int*)(dst + 2 * BPS) = _mm_cvtsi128_si32(dst2);
  228. *(int*)(dst + 3 * BPS) = _mm_cvtsi128_si32(dst3);
  229. }
  230. }
  231. }
  232. #if defined(USE_TRANSFORM_AC3)
  233. #define MUL(a, b) (((a) * (b)) >> 16)
  234. static void TransformAC3(const int16_t* in, uint8_t* dst) {
  235. static const int kC1 = 20091 + (1 << 16);
  236. static const int kC2 = 35468;
  237. const __m128i A = _mm_set1_epi16(in[0] + 4);
  238. const __m128i c4 = _mm_set1_epi16(MUL(in[4], kC2));
  239. const __m128i d4 = _mm_set1_epi16(MUL(in[4], kC1));
  240. const int c1 = MUL(in[1], kC2);
  241. const int d1 = MUL(in[1], kC1);
  242. const __m128i CD = _mm_set_epi16(0, 0, 0, 0, -d1, -c1, c1, d1);
  243. const __m128i B = _mm_adds_epi16(A, CD);
  244. const __m128i m0 = _mm_adds_epi16(B, d4);
  245. const __m128i m1 = _mm_adds_epi16(B, c4);
  246. const __m128i m2 = _mm_subs_epi16(B, c4);
  247. const __m128i m3 = _mm_subs_epi16(B, d4);
  248. const __m128i zero = _mm_setzero_si128();
  249. // Load the source pixels.
  250. __m128i dst0 = _mm_cvtsi32_si128(*(int*)(dst + 0 * BPS));
  251. __m128i dst1 = _mm_cvtsi32_si128(*(int*)(dst + 1 * BPS));
  252. __m128i dst2 = _mm_cvtsi32_si128(*(int*)(dst + 2 * BPS));
  253. __m128i dst3 = _mm_cvtsi32_si128(*(int*)(dst + 3 * BPS));
  254. // Convert to 16b.
  255. dst0 = _mm_unpacklo_epi8(dst0, zero);
  256. dst1 = _mm_unpacklo_epi8(dst1, zero);
  257. dst2 = _mm_unpacklo_epi8(dst2, zero);
  258. dst3 = _mm_unpacklo_epi8(dst3, zero);
  259. // Add the inverse transform.
  260. dst0 = _mm_adds_epi16(dst0, _mm_srai_epi16(m0, 3));
  261. dst1 = _mm_adds_epi16(dst1, _mm_srai_epi16(m1, 3));
  262. dst2 = _mm_adds_epi16(dst2, _mm_srai_epi16(m2, 3));
  263. dst3 = _mm_adds_epi16(dst3, _mm_srai_epi16(m3, 3));
  264. // Unsigned saturate to 8b.
  265. dst0 = _mm_packus_epi16(dst0, dst0);
  266. dst1 = _mm_packus_epi16(dst1, dst1);
  267. dst2 = _mm_packus_epi16(dst2, dst2);
  268. dst3 = _mm_packus_epi16(dst3, dst3);
  269. // Store the results.
  270. *(int*)(dst + 0 * BPS) = _mm_cvtsi128_si32(dst0);
  271. *(int*)(dst + 1 * BPS) = _mm_cvtsi128_si32(dst1);
  272. *(int*)(dst + 2 * BPS) = _mm_cvtsi128_si32(dst2);
  273. *(int*)(dst + 3 * BPS) = _mm_cvtsi128_si32(dst3);
  274. }
  275. #undef MUL
  276. #endif // USE_TRANSFORM_AC3
  277. //------------------------------------------------------------------------------
  278. // Loop Filter (Paragraph 15)
  279. // Compute abs(p - q) = subs(p - q) OR subs(q - p)
  280. #define MM_ABS(p, q) _mm_or_si128( \
  281. _mm_subs_epu8((q), (p)), \
  282. _mm_subs_epu8((p), (q)))
  283. // Shift each byte of "x" by 3 bits while preserving by the sign bit.
  284. static WEBP_INLINE void SignedShift8b(__m128i* const x) {
  285. const __m128i zero = _mm_setzero_si128();
  286. const __m128i signs = _mm_cmpgt_epi8(zero, *x);
  287. const __m128i lo_0 = _mm_unpacklo_epi8(*x, signs); // s8 -> s16 sign extend
  288. const __m128i hi_0 = _mm_unpackhi_epi8(*x, signs);
  289. const __m128i lo_1 = _mm_srai_epi16(lo_0, 3);
  290. const __m128i hi_1 = _mm_srai_epi16(hi_0, 3);
  291. *x = _mm_packs_epi16(lo_1, hi_1);
  292. }
  293. #define FLIP_SIGN_BIT2(a, b) { \
  294. a = _mm_xor_si128(a, sign_bit); \
  295. b = _mm_xor_si128(b, sign_bit); \
  296. }
  297. #define FLIP_SIGN_BIT4(a, b, c, d) { \
  298. FLIP_SIGN_BIT2(a, b); \
  299. FLIP_SIGN_BIT2(c, d); \
  300. }
  301. // input/output is uint8_t
  302. static WEBP_INLINE void GetNotHEV(const __m128i* const p1,
  303. const __m128i* const p0,
  304. const __m128i* const q0,
  305. const __m128i* const q1,
  306. int hev_thresh, __m128i* const not_hev) {
  307. const __m128i zero = _mm_setzero_si128();
  308. const __m128i t_1 = MM_ABS(*p1, *p0);
  309. const __m128i t_2 = MM_ABS(*q1, *q0);
  310. const __m128i h = _mm_set1_epi8(hev_thresh);
  311. const __m128i t_3 = _mm_subs_epu8(t_1, h); // abs(p1 - p0) - hev_tresh
  312. const __m128i t_4 = _mm_subs_epu8(t_2, h); // abs(q1 - q0) - hev_tresh
  313. *not_hev = _mm_or_si128(t_3, t_4);
  314. *not_hev = _mm_cmpeq_epi8(*not_hev, zero); // not_hev <= t1 && not_hev <= t2
  315. }
  316. // input pixels are int8_t
  317. static WEBP_INLINE void GetBaseDelta(const __m128i* const p1,
  318. const __m128i* const p0,
  319. const __m128i* const q0,
  320. const __m128i* const q1,
  321. __m128i* const delta) {
  322. // beware of addition order, for saturation!
  323. const __m128i p1_q1 = _mm_subs_epi8(*p1, *q1); // p1 - q1
  324. const __m128i q0_p0 = _mm_subs_epi8(*q0, *p0); // q0 - p0
  325. const __m128i s1 = _mm_adds_epi8(p1_q1, q0_p0); // p1 - q1 + 1 * (q0 - p0)
  326. const __m128i s2 = _mm_adds_epi8(q0_p0, s1); // p1 - q1 + 2 * (q0 - p0)
  327. const __m128i s3 = _mm_adds_epi8(q0_p0, s2); // p1 - q1 + 3 * (q0 - p0)
  328. *delta = s3;
  329. }
  330. // input and output are int8_t
  331. static WEBP_INLINE void DoSimpleFilter(__m128i* const p0, __m128i* const q0,
  332. const __m128i* const fl) {
  333. const __m128i k3 = _mm_set1_epi8(3);
  334. const __m128i k4 = _mm_set1_epi8(4);
  335. __m128i v3 = _mm_adds_epi8(*fl, k3);
  336. __m128i v4 = _mm_adds_epi8(*fl, k4);
  337. SignedShift8b(&v4); // v4 >> 3
  338. SignedShift8b(&v3); // v3 >> 3
  339. *q0 = _mm_subs_epi8(*q0, v4); // q0 -= v4
  340. *p0 = _mm_adds_epi8(*p0, v3); // p0 += v3
  341. }
  342. // Updates values of 2 pixels at MB edge during complex filtering.
  343. // Update operations:
  344. // q = q - delta and p = p + delta; where delta = [(a_hi >> 7), (a_lo >> 7)]
  345. // Pixels 'pi' and 'qi' are int8_t on input, uint8_t on output (sign flip).
  346. static WEBP_INLINE void Update2Pixels(__m128i* const pi, __m128i* const qi,
  347. const __m128i* const a0_lo,
  348. const __m128i* const a0_hi) {
  349. const __m128i a1_lo = _mm_srai_epi16(*a0_lo, 7);
  350. const __m128i a1_hi = _mm_srai_epi16(*a0_hi, 7);
  351. const __m128i delta = _mm_packs_epi16(a1_lo, a1_hi);
  352. const __m128i sign_bit = _mm_set1_epi8(0x80);
  353. *pi = _mm_adds_epi8(*pi, delta);
  354. *qi = _mm_subs_epi8(*qi, delta);
  355. FLIP_SIGN_BIT2(*pi, *qi);
  356. }
  357. // input pixels are uint8_t
  358. static WEBP_INLINE void NeedsFilter(const __m128i* const p1,
  359. const __m128i* const p0,
  360. const __m128i* const q0,
  361. const __m128i* const q1,
  362. int thresh, __m128i* const mask) {
  363. const __m128i m_thresh = _mm_set1_epi8(thresh);
  364. const __m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)
  365. const __m128i kFE = _mm_set1_epi8(0xFE);
  366. const __m128i t2 = _mm_and_si128(t1, kFE); // set lsb of each byte to zero
  367. const __m128i t3 = _mm_srli_epi16(t2, 1); // abs(p1 - q1) / 2
  368. const __m128i t4 = MM_ABS(*p0, *q0); // abs(p0 - q0)
  369. const __m128i t5 = _mm_adds_epu8(t4, t4); // abs(p0 - q0) * 2
  370. const __m128i t6 = _mm_adds_epu8(t5, t3); // abs(p0-q0)*2 + abs(p1-q1)/2
  371. const __m128i t7 = _mm_subs_epu8(t6, m_thresh); // mask <= m_thresh
  372. *mask = _mm_cmpeq_epi8(t7, _mm_setzero_si128());
  373. }
  374. //------------------------------------------------------------------------------
  375. // Edge filtering functions
  376. // Applies filter on 2 pixels (p0 and q0)
  377. static WEBP_INLINE void DoFilter2(__m128i* const p1, __m128i* const p0,
  378. __m128i* const q0, __m128i* const q1,
  379. int thresh) {
  380. __m128i a, mask;
  381. const __m128i sign_bit = _mm_set1_epi8(0x80);
  382. // convert p1/q1 to int8_t (for GetBaseDelta)
  383. const __m128i p1s = _mm_xor_si128(*p1, sign_bit);
  384. const __m128i q1s = _mm_xor_si128(*q1, sign_bit);
  385. NeedsFilter(p1, p0, q0, q1, thresh, &mask);
  386. FLIP_SIGN_BIT2(*p0, *q0);
  387. GetBaseDelta(&p1s, p0, q0, &q1s, &a);
  388. a = _mm_and_si128(a, mask); // mask filter values we don't care about
  389. DoSimpleFilter(p0, q0, &a);
  390. FLIP_SIGN_BIT2(*p0, *q0);
  391. }
  392. // Applies filter on 4 pixels (p1, p0, q0 and q1)
  393. static WEBP_INLINE void DoFilter4(__m128i* const p1, __m128i* const p0,
  394. __m128i* const q0, __m128i* const q1,
  395. const __m128i* const mask, int hev_thresh) {
  396. const __m128i sign_bit = _mm_set1_epi8(0x80);
  397. const __m128i k64 = _mm_set1_epi8(0x40);
  398. const __m128i zero = _mm_setzero_si128();
  399. __m128i not_hev;
  400. __m128i t1, t2, t3;
  401. // compute hev mask
  402. GetNotHEV(p1, p0, q0, q1, hev_thresh, &not_hev);
  403. // convert to signed values
  404. FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
  405. t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1
  406. t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1)
  407. t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0
  408. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0)
  409. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0)
  410. t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0)
  411. t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about
  412. t2 = _mm_set1_epi8(3);
  413. t3 = _mm_set1_epi8(4);
  414. t2 = _mm_adds_epi8(t1, t2); // 3 * (q0 - p0) + (p1 - q1) + 3
  415. t3 = _mm_adds_epi8(t1, t3); // 3 * (q0 - p0) + (p1 - q1) + 4
  416. SignedShift8b(&t2); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3
  417. SignedShift8b(&t3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3
  418. *p0 = _mm_adds_epi8(*p0, t2); // p0 += t2
  419. *q0 = _mm_subs_epi8(*q0, t3); // q0 -= t3
  420. FLIP_SIGN_BIT2(*p0, *q0);
  421. // this is equivalent to signed (a + 1) >> 1 calculation
  422. t2 = _mm_add_epi8(t3, sign_bit);
  423. t3 = _mm_avg_epu8(t2, zero);
  424. t3 = _mm_sub_epi8(t3, k64);
  425. t3 = _mm_and_si128(not_hev, t3); // if !hev
  426. *q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3
  427. *p1 = _mm_adds_epi8(*p1, t3); // p1 += t3
  428. FLIP_SIGN_BIT2(*p1, *q1);
  429. }
  430. // Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2)
  431. static WEBP_INLINE void DoFilter6(__m128i* const p2, __m128i* const p1,
  432. __m128i* const p0, __m128i* const q0,
  433. __m128i* const q1, __m128i* const q2,
  434. const __m128i* const mask, int hev_thresh) {
  435. const __m128i zero = _mm_setzero_si128();
  436. const __m128i sign_bit = _mm_set1_epi8(0x80);
  437. __m128i a, not_hev;
  438. // compute hev mask
  439. GetNotHEV(p1, p0, q0, q1, hev_thresh, &not_hev);
  440. FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
  441. FLIP_SIGN_BIT2(*p2, *q2);
  442. GetBaseDelta(p1, p0, q0, q1, &a);
  443. { // do simple filter on pixels with hev
  444. const __m128i m = _mm_andnot_si128(not_hev, *mask);
  445. const __m128i f = _mm_and_si128(a, m);
  446. DoSimpleFilter(p0, q0, &f);
  447. }
  448. { // do strong filter on pixels with not hev
  449. const __m128i k9 = _mm_set1_epi16(0x0900);
  450. const __m128i k63 = _mm_set1_epi16(63);
  451. const __m128i m = _mm_and_si128(not_hev, *mask);
  452. const __m128i f = _mm_and_si128(a, m);
  453. const __m128i f_lo = _mm_unpacklo_epi8(zero, f);
  454. const __m128i f_hi = _mm_unpackhi_epi8(zero, f);
  455. const __m128i f9_lo = _mm_mulhi_epi16(f_lo, k9); // Filter (lo) * 9
  456. const __m128i f9_hi = _mm_mulhi_epi16(f_hi, k9); // Filter (hi) * 9
  457. const __m128i a2_lo = _mm_add_epi16(f9_lo, k63); // Filter * 9 + 63
  458. const __m128i a2_hi = _mm_add_epi16(f9_hi, k63); // Filter * 9 + 63
  459. const __m128i a1_lo = _mm_add_epi16(a2_lo, f9_lo); // Filter * 18 + 63
  460. const __m128i a1_hi = _mm_add_epi16(a2_hi, f9_hi); // Filter * 18 + 63
  461. const __m128i a0_lo = _mm_add_epi16(a1_lo, f9_lo); // Filter * 27 + 63
  462. const __m128i a0_hi = _mm_add_epi16(a1_hi, f9_hi); // Filter * 27 + 63
  463. Update2Pixels(p2, q2, &a2_lo, &a2_hi);
  464. Update2Pixels(p1, q1, &a1_lo, &a1_hi);
  465. Update2Pixels(p0, q0, &a0_lo, &a0_hi);
  466. }
  467. }
  468. // reads 8 rows across a vertical edge.
  469. //
  470. // TODO(somnath): Investigate _mm_shuffle* also see if it can be broken into
  471. // two Load4x4() to avoid code duplication.
  472. static WEBP_INLINE void Load8x4(const uint8_t* const b, int stride,
  473. __m128i* const p, __m128i* const q) {
  474. __m128i t1, t2;
  475. // Load 0th, 1st, 4th and 5th rows
  476. __m128i r0 = _mm_cvtsi32_si128(*((int*)&b[0 * stride])); // 03 02 01 00
  477. __m128i r1 = _mm_cvtsi32_si128(*((int*)&b[1 * stride])); // 13 12 11 10
  478. __m128i r4 = _mm_cvtsi32_si128(*((int*)&b[4 * stride])); // 43 42 41 40
  479. __m128i r5 = _mm_cvtsi32_si128(*((int*)&b[5 * stride])); // 53 52 51 50
  480. r0 = _mm_unpacklo_epi32(r0, r4); // 43 42 41 40 03 02 01 00
  481. r1 = _mm_unpacklo_epi32(r1, r5); // 53 52 51 50 13 12 11 10
  482. // t1 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00
  483. t1 = _mm_unpacklo_epi8(r0, r1);
  484. // Load 2nd, 3rd, 6th and 7th rows
  485. r0 = _mm_cvtsi32_si128(*((int*)&b[2 * stride])); // 23 22 21 22
  486. r1 = _mm_cvtsi32_si128(*((int*)&b[3 * stride])); // 33 32 31 30
  487. r4 = _mm_cvtsi32_si128(*((int*)&b[6 * stride])); // 63 62 61 60
  488. r5 = _mm_cvtsi32_si128(*((int*)&b[7 * stride])); // 73 72 71 70
  489. r0 = _mm_unpacklo_epi32(r0, r4); // 63 62 61 60 23 22 21 20
  490. r1 = _mm_unpacklo_epi32(r1, r5); // 73 72 71 70 33 32 31 30
  491. // t2 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20
  492. t2 = _mm_unpacklo_epi8(r0, r1);
  493. // t1 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00
  494. // t2 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40
  495. r0 = t1;
  496. t1 = _mm_unpacklo_epi16(t1, t2);
  497. t2 = _mm_unpackhi_epi16(r0, t2);
  498. // *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
  499. // *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
  500. *p = _mm_unpacklo_epi32(t1, t2);
  501. *q = _mm_unpackhi_epi32(t1, t2);
  502. }
  503. static WEBP_INLINE void Load16x4(const uint8_t* const r0,
  504. const uint8_t* const r8,
  505. int stride,
  506. __m128i* const p1, __m128i* const p0,
  507. __m128i* const q0, __m128i* const q1) {
  508. __m128i t1, t2;
  509. // Assume the pixels around the edge (|) are numbered as follows
  510. // 00 01 | 02 03
  511. // 10 11 | 12 13
  512. // ... | ...
  513. // e0 e1 | e2 e3
  514. // f0 f1 | f2 f3
  515. //
  516. // r0 is pointing to the 0th row (00)
  517. // r8 is pointing to the 8th row (80)
  518. // Load
  519. // p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
  520. // q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
  521. // p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80
  522. // q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82
  523. Load8x4(r0, stride, p1, q0);
  524. Load8x4(r8, stride, p0, q1);
  525. t1 = *p1;
  526. t2 = *q0;
  527. // p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00
  528. // p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01
  529. // q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02
  530. // q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03
  531. *p1 = _mm_unpacklo_epi64(t1, *p0);
  532. *p0 = _mm_unpackhi_epi64(t1, *p0);
  533. *q0 = _mm_unpacklo_epi64(t2, *q1);
  534. *q1 = _mm_unpackhi_epi64(t2, *q1);
  535. }
  536. static WEBP_INLINE void Store4x4(__m128i* const x, uint8_t* dst, int stride) {
  537. int i;
  538. for (i = 0; i < 4; ++i, dst += stride) {
  539. *((int32_t*)dst) = _mm_cvtsi128_si32(*x);
  540. *x = _mm_srli_si128(*x, 4);
  541. }
  542. }
  543. // Transpose back and store
  544. static WEBP_INLINE void Store16x4(const __m128i* const p1,
  545. const __m128i* const p0,
  546. const __m128i* const q0,
  547. const __m128i* const q1,
  548. uint8_t* r0, uint8_t* r8,
  549. int stride) {
  550. __m128i t1, p1_s, p0_s, q0_s, q1_s;
  551. // p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00
  552. // p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80
  553. t1 = *p0;
  554. p0_s = _mm_unpacklo_epi8(*p1, t1);
  555. p1_s = _mm_unpackhi_epi8(*p1, t1);
  556. // q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02
  557. // q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82
  558. t1 = *q0;
  559. q0_s = _mm_unpacklo_epi8(t1, *q1);
  560. q1_s = _mm_unpackhi_epi8(t1, *q1);
  561. // p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00
  562. // q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40
  563. t1 = p0_s;
  564. p0_s = _mm_unpacklo_epi16(t1, q0_s);
  565. q0_s = _mm_unpackhi_epi16(t1, q0_s);
  566. // p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80
  567. // q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0
  568. t1 = p1_s;
  569. p1_s = _mm_unpacklo_epi16(t1, q1_s);
  570. q1_s = _mm_unpackhi_epi16(t1, q1_s);
  571. Store4x4(&p0_s, r0, stride);
  572. r0 += 4 * stride;
  573. Store4x4(&q0_s, r0, stride);
  574. Store4x4(&p1_s, r8, stride);
  575. r8 += 4 * stride;
  576. Store4x4(&q1_s, r8, stride);
  577. }
  578. //------------------------------------------------------------------------------
  579. // Simple In-loop filtering (Paragraph 15.2)
  580. static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
  581. // Load
  582. __m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]);
  583. __m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]);
  584. __m128i q0 = _mm_loadu_si128((__m128i*)&p[0]);
  585. __m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]);
  586. DoFilter2(&p1, &p0, &q0, &q1, thresh);
  587. // Store
  588. _mm_storeu_si128((__m128i*)&p[-stride], p0);
  589. _mm_storeu_si128((__m128i*)&p[0], q0);
  590. }
  591. static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
  592. __m128i p1, p0, q0, q1;
  593. p -= 2; // beginning of p1
  594. Load16x4(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);
  595. DoFilter2(&p1, &p0, &q0, &q1, thresh);
  596. Store16x4(&p1, &p0, &q0, &q1, p, p + 8 * stride, stride);
  597. }
  598. static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
  599. int k;
  600. for (k = 3; k > 0; --k) {
  601. p += 4 * stride;
  602. SimpleVFilter16(p, stride, thresh);
  603. }
  604. }
  605. static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
  606. int k;
  607. for (k = 3; k > 0; --k) {
  608. p += 4;
  609. SimpleHFilter16(p, stride, thresh);
  610. }
  611. }
  612. //------------------------------------------------------------------------------
  613. // Complex In-loop filtering (Paragraph 15.3)
  614. #define MAX_DIFF1(p3, p2, p1, p0, m) do { \
  615. m = MM_ABS(p1, p0); \
  616. m = _mm_max_epu8(m, MM_ABS(p3, p2)); \
  617. m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
  618. } while (0)
  619. #define MAX_DIFF2(p3, p2, p1, p0, m) do { \
  620. m = _mm_max_epu8(m, MM_ABS(p1, p0)); \
  621. m = _mm_max_epu8(m, MM_ABS(p3, p2)); \
  622. m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
  623. } while (0)
  624. #define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) { \
  625. e1 = _mm_loadu_si128((__m128i*)&(p)[0 * stride]); \
  626. e2 = _mm_loadu_si128((__m128i*)&(p)[1 * stride]); \
  627. e3 = _mm_loadu_si128((__m128i*)&(p)[2 * stride]); \
  628. e4 = _mm_loadu_si128((__m128i*)&(p)[3 * stride]); \
  629. }
  630. #define LOADUV_H_EDGE(p, u, v, stride) do { \
  631. const __m128i U = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \
  632. const __m128i V = _mm_loadl_epi64((__m128i*)&(v)[(stride)]); \
  633. p = _mm_unpacklo_epi64(U, V); \
  634. } while (0)
  635. #define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) { \
  636. LOADUV_H_EDGE(e1, u, v, 0 * stride); \
  637. LOADUV_H_EDGE(e2, u, v, 1 * stride); \
  638. LOADUV_H_EDGE(e3, u, v, 2 * stride); \
  639. LOADUV_H_EDGE(e4, u, v, 3 * stride); \
  640. }
  641. #define STOREUV(p, u, v, stride) { \
  642. _mm_storel_epi64((__m128i*)&u[(stride)], p); \
  643. p = _mm_srli_si128(p, 8); \
  644. _mm_storel_epi64((__m128i*)&v[(stride)], p); \
  645. }
  646. static WEBP_INLINE void ComplexMask(const __m128i* const p1,
  647. const __m128i* const p0,
  648. const __m128i* const q0,
  649. const __m128i* const q1,
  650. int thresh, int ithresh,
  651. __m128i* const mask) {
  652. const __m128i it = _mm_set1_epi8(ithresh);
  653. const __m128i diff = _mm_subs_epu8(*mask, it);
  654. const __m128i thresh_mask = _mm_cmpeq_epi8(diff, _mm_setzero_si128());
  655. __m128i filter_mask;
  656. NeedsFilter(p1, p0, q0, q1, thresh, &filter_mask);
  657. *mask = _mm_and_si128(thresh_mask, filter_mask);
  658. }
  659. // on macroblock edges
  660. static void VFilter16(uint8_t* p, int stride,
  661. int thresh, int ithresh, int hev_thresh) {
  662. __m128i t1;
  663. __m128i mask;
  664. __m128i p2, p1, p0, q0, q1, q2;
  665. // Load p3, p2, p1, p0
  666. LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0);
  667. MAX_DIFF1(t1, p2, p1, p0, mask);
  668. // Load q0, q1, q2, q3
  669. LOAD_H_EDGES4(p, stride, q0, q1, q2, t1);
  670. MAX_DIFF2(t1, q2, q1, q0, mask);
  671. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  672. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  673. // Store
  674. _mm_storeu_si128((__m128i*)&p[-3 * stride], p2);
  675. _mm_storeu_si128((__m128i*)&p[-2 * stride], p1);
  676. _mm_storeu_si128((__m128i*)&p[-1 * stride], p0);
  677. _mm_storeu_si128((__m128i*)&p[+0 * stride], q0);
  678. _mm_storeu_si128((__m128i*)&p[+1 * stride], q1);
  679. _mm_storeu_si128((__m128i*)&p[+2 * stride], q2);
  680. }
  681. static void HFilter16(uint8_t* p, int stride,
  682. int thresh, int ithresh, int hev_thresh) {
  683. __m128i mask;
  684. __m128i p3, p2, p1, p0, q0, q1, q2, q3;
  685. uint8_t* const b = p - 4;
  686. Load16x4(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
  687. MAX_DIFF1(p3, p2, p1, p0, mask);
  688. Load16x4(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
  689. MAX_DIFF2(q3, 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. Store16x4(&p3, &p2, &p1, &p0, b, b + 8 * stride, stride);
  693. Store16x4(&q0, &q1, &q2, &q3, p, p + 8 * stride, stride);
  694. }
  695. // on three inner edges
  696. static void VFilter16i(uint8_t* p, int stride,
  697. int thresh, int ithresh, int hev_thresh) {
  698. int k;
  699. __m128i p3, p2, p1, p0; // loop invariants
  700. LOAD_H_EDGES4(p, stride, p3, p2, p1, p0); // prologue
  701. for (k = 3; k > 0; --k) {
  702. __m128i mask, tmp1, tmp2;
  703. uint8_t* const b = p + 2 * stride; // beginning of p1
  704. p += 4 * stride;
  705. MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask
  706. LOAD_H_EDGES4(p, stride, p3, p2, tmp1, tmp2);
  707. MAX_DIFF2(p3, p2, tmp1, tmp2, mask);
  708. // p3 and p2 are not just temporary variables here: they will be
  709. // re-used for next span. And q2/q3 will become p1/p0 accordingly.
  710. ComplexMask(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);
  711. DoFilter4(&p1, &p0, &p3, &p2, &mask, hev_thresh);
  712. // Store
  713. _mm_storeu_si128((__m128i*)&b[0 * stride], p1);
  714. _mm_storeu_si128((__m128i*)&b[1 * stride], p0);
  715. _mm_storeu_si128((__m128i*)&b[2 * stride], p3);
  716. _mm_storeu_si128((__m128i*)&b[3 * stride], p2);
  717. // rotate samples
  718. p1 = tmp1;
  719. p0 = tmp2;
  720. }
  721. }
  722. static void HFilter16i(uint8_t* p, int stride,
  723. int thresh, int ithresh, int hev_thresh) {
  724. int k;
  725. __m128i p3, p2, p1, p0; // loop invariants
  726. Load16x4(p, p + 8 * stride, stride, &p3, &p2, &p1, &p0); // prologue
  727. for (k = 3; k > 0; --k) {
  728. __m128i mask, tmp1, tmp2;
  729. uint8_t* const b = p + 2; // beginning of p1
  730. p += 4; // beginning of q0 (and next span)
  731. MAX_DIFF1(p3, p2, p1, p0, mask); // compute partial mask
  732. Load16x4(p, p + 8 * stride, stride, &p3, &p2, &tmp1, &tmp2);
  733. MAX_DIFF2(p3, p2, tmp1, tmp2, mask);
  734. ComplexMask(&p1, &p0, &p3, &p2, thresh, ithresh, &mask);
  735. DoFilter4(&p1, &p0, &p3, &p2, &mask, hev_thresh);
  736. Store16x4(&p1, &p0, &p3, &p2, b, b + 8 * stride, stride);
  737. // rotate samples
  738. p1 = tmp1;
  739. p0 = tmp2;
  740. }
  741. }
  742. // 8-pixels wide variant, for chroma filtering
  743. static void VFilter8(uint8_t* u, uint8_t* v, int stride,
  744. int thresh, int ithresh, int hev_thresh) {
  745. __m128i mask;
  746. __m128i t1, p2, p1, p0, q0, q1, q2;
  747. // Load p3, p2, p1, p0
  748. LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0);
  749. MAX_DIFF1(t1, p2, p1, p0, mask);
  750. // Load q0, q1, q2, q3
  751. LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1);
  752. MAX_DIFF2(t1, q2, q1, q0, mask);
  753. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  754. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  755. // Store
  756. STOREUV(p2, u, v, -3 * stride);
  757. STOREUV(p1, u, v, -2 * stride);
  758. STOREUV(p0, u, v, -1 * stride);
  759. STOREUV(q0, u, v, 0 * stride);
  760. STOREUV(q1, u, v, 1 * stride);
  761. STOREUV(q2, u, v, 2 * stride);
  762. }
  763. static void HFilter8(uint8_t* u, uint8_t* v, int stride,
  764. int thresh, int ithresh, int hev_thresh) {
  765. __m128i mask;
  766. __m128i p3, p2, p1, p0, q0, q1, q2, q3;
  767. uint8_t* const tu = u - 4;
  768. uint8_t* const tv = v - 4;
  769. Load16x4(tu, tv, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
  770. MAX_DIFF1(p3, p2, p1, p0, mask);
  771. Load16x4(u, v, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
  772. MAX_DIFF2(q3, q2, q1, q0, mask);
  773. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  774. DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
  775. Store16x4(&p3, &p2, &p1, &p0, tu, tv, stride);
  776. Store16x4(&q0, &q1, &q2, &q3, u, v, stride);
  777. }
  778. static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
  779. int thresh, int ithresh, int hev_thresh) {
  780. __m128i mask;
  781. __m128i t1, t2, p1, p0, q0, q1;
  782. // Load p3, p2, p1, p0
  783. LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);
  784. MAX_DIFF1(t2, t1, p1, p0, mask);
  785. u += 4 * stride;
  786. v += 4 * stride;
  787. // Load q0, q1, q2, q3
  788. LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);
  789. MAX_DIFF2(t2, t1, q1, q0, mask);
  790. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  791. DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
  792. // Store
  793. STOREUV(p1, u, v, -2 * stride);
  794. STOREUV(p0, u, v, -1 * stride);
  795. STOREUV(q0, u, v, 0 * stride);
  796. STOREUV(q1, u, v, 1 * stride);
  797. }
  798. static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
  799. int thresh, int ithresh, int hev_thresh) {
  800. __m128i mask;
  801. __m128i t1, t2, p1, p0, q0, q1;
  802. Load16x4(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0
  803. MAX_DIFF1(t2, t1, p1, p0, mask);
  804. u += 4; // beginning of q0
  805. v += 4;
  806. Load16x4(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3
  807. MAX_DIFF2(t2, t1, q1, q0, mask);
  808. ComplexMask(&p1, &p0, &q0, &q1, thresh, ithresh, &mask);
  809. DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
  810. u -= 2; // beginning of p1
  811. v -= 2;
  812. Store16x4(&p1, &p0, &q0, &q1, u, v, stride);
  813. }
  814. #endif // WEBP_USE_SSE2
  815. //------------------------------------------------------------------------------
  816. // Entry point
  817. extern void VP8DspInitSSE2(void);
  818. void VP8DspInitSSE2(void) {
  819. #if defined(WEBP_USE_SSE2)
  820. VP8Transform = Transform;
  821. #if defined(USE_TRANSFORM_AC3)
  822. VP8TransformAC3 = TransformAC3;
  823. #endif
  824. VP8VFilter16 = VFilter16;
  825. VP8HFilter16 = HFilter16;
  826. VP8VFilter8 = VFilter8;
  827. VP8HFilter8 = HFilter8;
  828. VP8VFilter16i = VFilter16i;
  829. VP8HFilter16i = HFilter16i;
  830. VP8VFilter8i = VFilter8i;
  831. VP8HFilter8i = HFilter8i;
  832. VP8SimpleVFilter16 = SimpleVFilter16;
  833. VP8SimpleHFilter16 = SimpleHFilter16;
  834. VP8SimpleVFilter16i = SimpleVFilter16i;
  835. VP8SimpleHFilter16i = SimpleHFilter16i;
  836. #endif // WEBP_USE_SSE2
  837. }