enc_sse2.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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 speed-critical encoding functions.
  11. //
  12. // Author: Christian Duvivier (cduvivier@google.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2)
  15. #include <assert.h>
  16. #include <stdlib.h> // for abs()
  17. #include <emmintrin.h>
  18. #include "./common_sse2.h"
  19. #include "../enc/cost_enc.h"
  20. #include "../enc/vp8i_enc.h"
  21. //------------------------------------------------------------------------------
  22. // Transforms (Paragraph 14.4)
  23. // Does one or two inverse transforms.
  24. static void ITransform_SSE2(const uint8_t* ref, const int16_t* in, uint8_t* dst,
  25. 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 inverse
  46. // transforms in parallel). In the case of only one inverse transform, the
  47. // second half of the vectors will just contain random value we'll never
  48. // use nor store.
  49. __m128i in0, in1, in2, in3;
  50. {
  51. in0 = _mm_loadl_epi64((const __m128i*)&in[0]);
  52. in1 = _mm_loadl_epi64((const __m128i*)&in[4]);
  53. in2 = _mm_loadl_epi64((const __m128i*)&in[8]);
  54. in3 = _mm_loadl_epi64((const __m128i*)&in[12]);
  55. // a00 a10 a20 a30 x x x x
  56. // a01 a11 a21 a31 x x x x
  57. // a02 a12 a22 a32 x x x x
  58. // a03 a13 a23 a33 x x x x
  59. if (do_two) {
  60. const __m128i inB0 = _mm_loadl_epi64((const __m128i*)&in[16]);
  61. const __m128i inB1 = _mm_loadl_epi64((const __m128i*)&in[20]);
  62. const __m128i inB2 = _mm_loadl_epi64((const __m128i*)&in[24]);
  63. const __m128i inB3 = _mm_loadl_epi64((const __m128i*)&in[28]);
  64. in0 = _mm_unpacklo_epi64(in0, inB0);
  65. in1 = _mm_unpacklo_epi64(in1, inB1);
  66. in2 = _mm_unpacklo_epi64(in2, inB2);
  67. in3 = _mm_unpacklo_epi64(in3, inB3);
  68. // a00 a10 a20 a30 b00 b10 b20 b30
  69. // a01 a11 a21 a31 b01 b11 b21 b31
  70. // a02 a12 a22 a32 b02 b12 b22 b32
  71. // a03 a13 a23 a33 b03 b13 b23 b33
  72. }
  73. }
  74. // Vertical pass and subsequent transpose.
  75. {
  76. // First pass, c and d calculations are longer because of the "trick"
  77. // multiplications.
  78. const __m128i a = _mm_add_epi16(in0, in2);
  79. const __m128i b = _mm_sub_epi16(in0, in2);
  80. // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
  81. const __m128i c1 = _mm_mulhi_epi16(in1, k2);
  82. const __m128i c2 = _mm_mulhi_epi16(in3, k1);
  83. const __m128i c3 = _mm_sub_epi16(in1, in3);
  84. const __m128i c4 = _mm_sub_epi16(c1, c2);
  85. const __m128i c = _mm_add_epi16(c3, c4);
  86. // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
  87. const __m128i d1 = _mm_mulhi_epi16(in1, k1);
  88. const __m128i d2 = _mm_mulhi_epi16(in3, k2);
  89. const __m128i d3 = _mm_add_epi16(in1, in3);
  90. const __m128i d4 = _mm_add_epi16(d1, d2);
  91. const __m128i d = _mm_add_epi16(d3, d4);
  92. // Second pass.
  93. const __m128i tmp0 = _mm_add_epi16(a, d);
  94. const __m128i tmp1 = _mm_add_epi16(b, c);
  95. const __m128i tmp2 = _mm_sub_epi16(b, c);
  96. const __m128i tmp3 = _mm_sub_epi16(a, d);
  97. // Transpose the two 4x4.
  98. VP8Transpose_2_4x4_16b(&tmp0, &tmp1, &tmp2, &tmp3, &T0, &T1, &T2, &T3);
  99. }
  100. // Horizontal pass and subsequent transpose.
  101. {
  102. // First pass, c and d calculations are longer because of the "trick"
  103. // multiplications.
  104. const __m128i four = _mm_set1_epi16(4);
  105. const __m128i dc = _mm_add_epi16(T0, four);
  106. const __m128i a = _mm_add_epi16(dc, T2);
  107. const __m128i b = _mm_sub_epi16(dc, T2);
  108. // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
  109. const __m128i c1 = _mm_mulhi_epi16(T1, k2);
  110. const __m128i c2 = _mm_mulhi_epi16(T3, k1);
  111. const __m128i c3 = _mm_sub_epi16(T1, T3);
  112. const __m128i c4 = _mm_sub_epi16(c1, c2);
  113. const __m128i c = _mm_add_epi16(c3, c4);
  114. // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
  115. const __m128i d1 = _mm_mulhi_epi16(T1, k1);
  116. const __m128i d2 = _mm_mulhi_epi16(T3, k2);
  117. const __m128i d3 = _mm_add_epi16(T1, T3);
  118. const __m128i d4 = _mm_add_epi16(d1, d2);
  119. const __m128i d = _mm_add_epi16(d3, d4);
  120. // Second pass.
  121. const __m128i tmp0 = _mm_add_epi16(a, d);
  122. const __m128i tmp1 = _mm_add_epi16(b, c);
  123. const __m128i tmp2 = _mm_sub_epi16(b, c);
  124. const __m128i tmp3 = _mm_sub_epi16(a, d);
  125. const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
  126. const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
  127. const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
  128. const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
  129. // Transpose the two 4x4.
  130. VP8Transpose_2_4x4_16b(&shifted0, &shifted1, &shifted2, &shifted3, &T0, &T1,
  131. &T2, &T3);
  132. }
  133. // Add inverse transform to 'ref' and store.
  134. {
  135. const __m128i zero = _mm_setzero_si128();
  136. // Load the reference(s).
  137. __m128i ref0, ref1, ref2, ref3;
  138. if (do_two) {
  139. // Load eight bytes/pixels per line.
  140. ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
  141. ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
  142. ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
  143. ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
  144. } else {
  145. // Load four bytes/pixels per line.
  146. ref0 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[0 * BPS]));
  147. ref1 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[1 * BPS]));
  148. ref2 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[2 * BPS]));
  149. ref3 = _mm_cvtsi32_si128(WebPMemToUint32(&ref[3 * BPS]));
  150. }
  151. // Convert to 16b.
  152. ref0 = _mm_unpacklo_epi8(ref0, zero);
  153. ref1 = _mm_unpacklo_epi8(ref1, zero);
  154. ref2 = _mm_unpacklo_epi8(ref2, zero);
  155. ref3 = _mm_unpacklo_epi8(ref3, zero);
  156. // Add the inverse transform(s).
  157. ref0 = _mm_add_epi16(ref0, T0);
  158. ref1 = _mm_add_epi16(ref1, T1);
  159. ref2 = _mm_add_epi16(ref2, T2);
  160. ref3 = _mm_add_epi16(ref3, T3);
  161. // Unsigned saturate to 8b.
  162. ref0 = _mm_packus_epi16(ref0, ref0);
  163. ref1 = _mm_packus_epi16(ref1, ref1);
  164. ref2 = _mm_packus_epi16(ref2, ref2);
  165. ref3 = _mm_packus_epi16(ref3, ref3);
  166. // Store the results.
  167. if (do_two) {
  168. // Store eight bytes/pixels per line.
  169. _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);
  170. _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);
  171. _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);
  172. _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);
  173. } else {
  174. // Store four bytes/pixels per line.
  175. WebPUint32ToMem(&dst[0 * BPS], _mm_cvtsi128_si32(ref0));
  176. WebPUint32ToMem(&dst[1 * BPS], _mm_cvtsi128_si32(ref1));
  177. WebPUint32ToMem(&dst[2 * BPS], _mm_cvtsi128_si32(ref2));
  178. WebPUint32ToMem(&dst[3 * BPS], _mm_cvtsi128_si32(ref3));
  179. }
  180. }
  181. }
  182. static void FTransformPass1_SSE2(const __m128i* const in01,
  183. const __m128i* const in23,
  184. __m128i* const out01,
  185. __m128i* const out32) {
  186. const __m128i k937 = _mm_set1_epi32(937);
  187. const __m128i k1812 = _mm_set1_epi32(1812);
  188. const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);
  189. const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);
  190. const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,
  191. 2217, 5352, 2217, 5352);
  192. const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,
  193. -5352, 2217, -5352, 2217);
  194. // *in01 = 00 01 10 11 02 03 12 13
  195. // *in23 = 20 21 30 31 22 23 32 33
  196. const __m128i shuf01_p = _mm_shufflehi_epi16(*in01, _MM_SHUFFLE(2, 3, 0, 1));
  197. const __m128i shuf23_p = _mm_shufflehi_epi16(*in23, _MM_SHUFFLE(2, 3, 0, 1));
  198. // 00 01 10 11 03 02 13 12
  199. // 20 21 30 31 23 22 33 32
  200. const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);
  201. const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);
  202. // 00 01 10 11 20 21 30 31
  203. // 03 02 13 12 23 22 33 32
  204. const __m128i a01 = _mm_add_epi16(s01, s32);
  205. const __m128i a32 = _mm_sub_epi16(s01, s32);
  206. // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]
  207. // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]
  208. const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]
  209. const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]
  210. const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);
  211. const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);
  212. const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);
  213. const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);
  214. const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);
  215. const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);
  216. const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);
  217. const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);
  218. const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...
  219. const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3
  220. const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);
  221. *out01 = _mm_unpacklo_epi32(s_lo, s_hi);
  222. *out32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..
  223. }
  224. static void FTransformPass2_SSE2(const __m128i* const v01,
  225. const __m128i* const v32,
  226. int16_t* out) {
  227. const __m128i zero = _mm_setzero_si128();
  228. const __m128i seven = _mm_set1_epi16(7);
  229. const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,
  230. 5352, 2217, 5352, 2217);
  231. const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
  232. 2217, -5352, 2217, -5352);
  233. const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
  234. const __m128i k51000 = _mm_set1_epi32(51000);
  235. // Same operations are done on the (0,3) and (1,2) pairs.
  236. // a3 = v0 - v3
  237. // a2 = v1 - v2
  238. const __m128i a32 = _mm_sub_epi16(*v01, *v32);
  239. const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
  240. const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
  241. const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
  242. const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
  243. const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
  244. const __m128i d3 = _mm_add_epi32(c3, k51000);
  245. const __m128i e1 = _mm_srai_epi32(d1, 16);
  246. const __m128i e3 = _mm_srai_epi32(d3, 16);
  247. // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
  248. // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
  249. const __m128i f1 = _mm_packs_epi32(e1, e1);
  250. const __m128i f3 = _mm_packs_epi32(e3, e3);
  251. // g1 = f1 + (a3 != 0);
  252. // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
  253. // desired (0, 1), we add one earlier through k12000_plus_one.
  254. // -> g1 = f1 + 1 - (a3 == 0)
  255. const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
  256. // a0 = v0 + v3
  257. // a1 = v1 + v2
  258. const __m128i a01 = _mm_add_epi16(*v01, *v32);
  259. const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);
  260. const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
  261. const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);
  262. const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);
  263. // d0 = (a0 + a1 + 7) >> 4;
  264. // d2 = (a0 - a1 + 7) >> 4;
  265. const __m128i d0 = _mm_srai_epi16(c0, 4);
  266. const __m128i d2 = _mm_srai_epi16(c2, 4);
  267. const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);
  268. const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);
  269. _mm_storeu_si128((__m128i*)&out[0], d0_g1);
  270. _mm_storeu_si128((__m128i*)&out[8], d2_f3);
  271. }
  272. static void FTransform_SSE2(const uint8_t* src, const uint8_t* ref,
  273. int16_t* out) {
  274. const __m128i zero = _mm_setzero_si128();
  275. // Load src.
  276. const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);
  277. const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);
  278. const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);
  279. const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);
  280. // 00 01 02 03 *
  281. // 10 11 12 13 *
  282. // 20 21 22 23 *
  283. // 30 31 32 33 *
  284. // Shuffle.
  285. const __m128i src_0 = _mm_unpacklo_epi16(src0, src1);
  286. const __m128i src_1 = _mm_unpacklo_epi16(src2, src3);
  287. // 00 01 10 11 02 03 12 13 * * ...
  288. // 20 21 30 31 22 22 32 33 * * ...
  289. // Load ref.
  290. const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
  291. const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
  292. const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
  293. const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
  294. const __m128i ref_0 = _mm_unpacklo_epi16(ref0, ref1);
  295. const __m128i ref_1 = _mm_unpacklo_epi16(ref2, ref3);
  296. // Convert both to 16 bit.
  297. const __m128i src_0_16b = _mm_unpacklo_epi8(src_0, zero);
  298. const __m128i src_1_16b = _mm_unpacklo_epi8(src_1, zero);
  299. const __m128i ref_0_16b = _mm_unpacklo_epi8(ref_0, zero);
  300. const __m128i ref_1_16b = _mm_unpacklo_epi8(ref_1, zero);
  301. // Compute the difference.
  302. const __m128i row01 = _mm_sub_epi16(src_0_16b, ref_0_16b);
  303. const __m128i row23 = _mm_sub_epi16(src_1_16b, ref_1_16b);
  304. __m128i v01, v32;
  305. // First pass
  306. FTransformPass1_SSE2(&row01, &row23, &v01, &v32);
  307. // Second pass
  308. FTransformPass2_SSE2(&v01, &v32, out);
  309. }
  310. static void FTransform2_SSE2(const uint8_t* src, const uint8_t* ref,
  311. int16_t* out) {
  312. const __m128i zero = _mm_setzero_si128();
  313. // Load src and convert to 16b.
  314. const __m128i src0 = _mm_loadl_epi64((const __m128i*)&src[0 * BPS]);
  315. const __m128i src1 = _mm_loadl_epi64((const __m128i*)&src[1 * BPS]);
  316. const __m128i src2 = _mm_loadl_epi64((const __m128i*)&src[2 * BPS]);
  317. const __m128i src3 = _mm_loadl_epi64((const __m128i*)&src[3 * BPS]);
  318. const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
  319. const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
  320. const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
  321. const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
  322. // Load ref and convert to 16b.
  323. const __m128i ref0 = _mm_loadl_epi64((const __m128i*)&ref[0 * BPS]);
  324. const __m128i ref1 = _mm_loadl_epi64((const __m128i*)&ref[1 * BPS]);
  325. const __m128i ref2 = _mm_loadl_epi64((const __m128i*)&ref[2 * BPS]);
  326. const __m128i ref3 = _mm_loadl_epi64((const __m128i*)&ref[3 * BPS]);
  327. const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
  328. const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
  329. const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
  330. const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
  331. // Compute difference. -> 00 01 02 03 00' 01' 02' 03'
  332. const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
  333. const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
  334. const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
  335. const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
  336. // Unpack and shuffle
  337. // 00 01 02 03 0 0 0 0
  338. // 10 11 12 13 0 0 0 0
  339. // 20 21 22 23 0 0 0 0
  340. // 30 31 32 33 0 0 0 0
  341. const __m128i shuf01l = _mm_unpacklo_epi32(diff0, diff1);
  342. const __m128i shuf23l = _mm_unpacklo_epi32(diff2, diff3);
  343. const __m128i shuf01h = _mm_unpackhi_epi32(diff0, diff1);
  344. const __m128i shuf23h = _mm_unpackhi_epi32(diff2, diff3);
  345. __m128i v01l, v32l;
  346. __m128i v01h, v32h;
  347. // First pass
  348. FTransformPass1_SSE2(&shuf01l, &shuf23l, &v01l, &v32l);
  349. FTransformPass1_SSE2(&shuf01h, &shuf23h, &v01h, &v32h);
  350. // Second pass
  351. FTransformPass2_SSE2(&v01l, &v32l, out + 0);
  352. FTransformPass2_SSE2(&v01h, &v32h, out + 16);
  353. }
  354. static void FTransformWHTRow_SSE2(const int16_t* const in, __m128i* const out) {
  355. const __m128i kMult = _mm_set_epi16(-1, 1, -1, 1, 1, 1, 1, 1);
  356. const __m128i src0 = _mm_loadl_epi64((__m128i*)&in[0 * 16]);
  357. const __m128i src1 = _mm_loadl_epi64((__m128i*)&in[1 * 16]);
  358. const __m128i src2 = _mm_loadl_epi64((__m128i*)&in[2 * 16]);
  359. const __m128i src3 = _mm_loadl_epi64((__m128i*)&in[3 * 16]);
  360. const __m128i A01 = _mm_unpacklo_epi16(src0, src1); // A0 A1 | ...
  361. const __m128i A23 = _mm_unpacklo_epi16(src2, src3); // A2 A3 | ...
  362. const __m128i B0 = _mm_adds_epi16(A01, A23); // a0 | a1 | ...
  363. const __m128i B1 = _mm_subs_epi16(A01, A23); // a3 | a2 | ...
  364. const __m128i C0 = _mm_unpacklo_epi32(B0, B1); // a0 | a1 | a3 | a2 | ...
  365. const __m128i C1 = _mm_unpacklo_epi32(B1, B0); // a3 | a2 | a0 | a1 | ...
  366. const __m128i D = _mm_unpacklo_epi64(C0, C1); // a0 a1 a3 a2 a3 a2 a0 a1
  367. *out = _mm_madd_epi16(D, kMult);
  368. }
  369. static void FTransformWHT_SSE2(const int16_t* in, int16_t* out) {
  370. // Input is 12b signed.
  371. __m128i row0, row1, row2, row3;
  372. // Rows are 14b signed.
  373. FTransformWHTRow_SSE2(in + 0 * 64, &row0);
  374. FTransformWHTRow_SSE2(in + 1 * 64, &row1);
  375. FTransformWHTRow_SSE2(in + 2 * 64, &row2);
  376. FTransformWHTRow_SSE2(in + 3 * 64, &row3);
  377. {
  378. // The a* are 15b signed.
  379. const __m128i a0 = _mm_add_epi32(row0, row2);
  380. const __m128i a1 = _mm_add_epi32(row1, row3);
  381. const __m128i a2 = _mm_sub_epi32(row1, row3);
  382. const __m128i a3 = _mm_sub_epi32(row0, row2);
  383. const __m128i a0a3 = _mm_packs_epi32(a0, a3);
  384. const __m128i a1a2 = _mm_packs_epi32(a1, a2);
  385. // The b* are 16b signed.
  386. const __m128i b0b1 = _mm_add_epi16(a0a3, a1a2);
  387. const __m128i b3b2 = _mm_sub_epi16(a0a3, a1a2);
  388. const __m128i tmp_b2b3 = _mm_unpackhi_epi64(b3b2, b3b2);
  389. const __m128i b2b3 = _mm_unpacklo_epi64(tmp_b2b3, b3b2);
  390. _mm_storeu_si128((__m128i*)&out[0], _mm_srai_epi16(b0b1, 1));
  391. _mm_storeu_si128((__m128i*)&out[8], _mm_srai_epi16(b2b3, 1));
  392. }
  393. }
  394. //------------------------------------------------------------------------------
  395. // Compute susceptibility based on DCT-coeff histograms:
  396. // the higher, the "easier" the macroblock is to compress.
  397. static void CollectHistogram_SSE2(const uint8_t* ref, const uint8_t* pred,
  398. int start_block, int end_block,
  399. VP8Histogram* const histo) {
  400. const __m128i zero = _mm_setzero_si128();
  401. const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
  402. int j;
  403. int distribution[MAX_COEFF_THRESH + 1] = { 0 };
  404. for (j = start_block; j < end_block; ++j) {
  405. int16_t out[16];
  406. int k;
  407. FTransform_SSE2(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
  408. // Convert coefficients to bin (within out[]).
  409. {
  410. // Load.
  411. const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
  412. const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
  413. const __m128i d0 = _mm_sub_epi16(zero, out0);
  414. const __m128i d1 = _mm_sub_epi16(zero, out1);
  415. const __m128i abs0 = _mm_max_epi16(out0, d0); // abs(v), 16b
  416. const __m128i abs1 = _mm_max_epi16(out1, d1);
  417. // v = abs(out) >> 3
  418. const __m128i v0 = _mm_srai_epi16(abs0, 3);
  419. const __m128i v1 = _mm_srai_epi16(abs1, 3);
  420. // bin = min(v, MAX_COEFF_THRESH)
  421. const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
  422. const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
  423. // Store.
  424. _mm_storeu_si128((__m128i*)&out[0], bin0);
  425. _mm_storeu_si128((__m128i*)&out[8], bin1);
  426. }
  427. // Convert coefficients to bin.
  428. for (k = 0; k < 16; ++k) {
  429. ++distribution[out[k]];
  430. }
  431. }
  432. VP8SetHistogramData(distribution, histo);
  433. }
  434. //------------------------------------------------------------------------------
  435. // Intra predictions
  436. // helper for chroma-DC predictions
  437. static WEBP_INLINE void Put8x8uv_SSE2(uint8_t v, uint8_t* dst) {
  438. int j;
  439. const __m128i values = _mm_set1_epi8(v);
  440. for (j = 0; j < 8; ++j) {
  441. _mm_storel_epi64((__m128i*)(dst + j * BPS), values);
  442. }
  443. }
  444. static WEBP_INLINE void Put16_SSE2(uint8_t v, uint8_t* dst) {
  445. int j;
  446. const __m128i values = _mm_set1_epi8(v);
  447. for (j = 0; j < 16; ++j) {
  448. _mm_store_si128((__m128i*)(dst + j * BPS), values);
  449. }
  450. }
  451. static WEBP_INLINE void Fill_SSE2(uint8_t* dst, int value, int size) {
  452. if (size == 4) {
  453. int j;
  454. for (j = 0; j < 4; ++j) {
  455. memset(dst + j * BPS, value, 4);
  456. }
  457. } else if (size == 8) {
  458. Put8x8uv_SSE2(value, dst);
  459. } else {
  460. Put16_SSE2(value, dst);
  461. }
  462. }
  463. static WEBP_INLINE void VE8uv_SSE2(uint8_t* dst, const uint8_t* top) {
  464. int j;
  465. const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
  466. for (j = 0; j < 8; ++j) {
  467. _mm_storel_epi64((__m128i*)(dst + j * BPS), top_values);
  468. }
  469. }
  470. static WEBP_INLINE void VE16_SSE2(uint8_t* dst, const uint8_t* top) {
  471. const __m128i top_values = _mm_load_si128((const __m128i*)top);
  472. int j;
  473. for (j = 0; j < 16; ++j) {
  474. _mm_store_si128((__m128i*)(dst + j * BPS), top_values);
  475. }
  476. }
  477. static WEBP_INLINE void VerticalPred_SSE2(uint8_t* dst,
  478. const uint8_t* top, int size) {
  479. if (top != NULL) {
  480. if (size == 8) {
  481. VE8uv_SSE2(dst, top);
  482. } else {
  483. VE16_SSE2(dst, top);
  484. }
  485. } else {
  486. Fill_SSE2(dst, 127, size);
  487. }
  488. }
  489. static WEBP_INLINE void HE8uv_SSE2(uint8_t* dst, const uint8_t* left) {
  490. int j;
  491. for (j = 0; j < 8; ++j) {
  492. const __m128i values = _mm_set1_epi8(left[j]);
  493. _mm_storel_epi64((__m128i*)dst, values);
  494. dst += BPS;
  495. }
  496. }
  497. static WEBP_INLINE void HE16_SSE2(uint8_t* dst, const uint8_t* left) {
  498. int j;
  499. for (j = 0; j < 16; ++j) {
  500. const __m128i values = _mm_set1_epi8(left[j]);
  501. _mm_store_si128((__m128i*)dst, values);
  502. dst += BPS;
  503. }
  504. }
  505. static WEBP_INLINE void HorizontalPred_SSE2(uint8_t* dst,
  506. const uint8_t* left, int size) {
  507. if (left != NULL) {
  508. if (size == 8) {
  509. HE8uv_SSE2(dst, left);
  510. } else {
  511. HE16_SSE2(dst, left);
  512. }
  513. } else {
  514. Fill_SSE2(dst, 129, size);
  515. }
  516. }
  517. static WEBP_INLINE void TM_SSE2(uint8_t* dst, const uint8_t* left,
  518. const uint8_t* top, int size) {
  519. const __m128i zero = _mm_setzero_si128();
  520. int y;
  521. if (size == 8) {
  522. const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
  523. const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
  524. for (y = 0; y < 8; ++y, dst += BPS) {
  525. const int val = left[y] - left[-1];
  526. const __m128i base = _mm_set1_epi16(val);
  527. const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
  528. _mm_storel_epi64((__m128i*)dst, out);
  529. }
  530. } else {
  531. const __m128i top_values = _mm_load_si128((const __m128i*)top);
  532. const __m128i top_base_0 = _mm_unpacklo_epi8(top_values, zero);
  533. const __m128i top_base_1 = _mm_unpackhi_epi8(top_values, zero);
  534. for (y = 0; y < 16; ++y, dst += BPS) {
  535. const int val = left[y] - left[-1];
  536. const __m128i base = _mm_set1_epi16(val);
  537. const __m128i out_0 = _mm_add_epi16(base, top_base_0);
  538. const __m128i out_1 = _mm_add_epi16(base, top_base_1);
  539. const __m128i out = _mm_packus_epi16(out_0, out_1);
  540. _mm_store_si128((__m128i*)dst, out);
  541. }
  542. }
  543. }
  544. static WEBP_INLINE void TrueMotion_SSE2(uint8_t* dst, const uint8_t* left,
  545. const uint8_t* top, int size) {
  546. if (left != NULL) {
  547. if (top != NULL) {
  548. TM_SSE2(dst, left, top, size);
  549. } else {
  550. HorizontalPred_SSE2(dst, left, size);
  551. }
  552. } else {
  553. // true motion without left samples (hence: with default 129 value)
  554. // is equivalent to VE prediction where you just copy the top samples.
  555. // Note that if top samples are not available, the default value is
  556. // then 129, and not 127 as in the VerticalPred case.
  557. if (top != NULL) {
  558. VerticalPred_SSE2(dst, top, size);
  559. } else {
  560. Fill_SSE2(dst, 129, size);
  561. }
  562. }
  563. }
  564. static WEBP_INLINE void DC8uv_SSE2(uint8_t* dst, const uint8_t* left,
  565. const uint8_t* top) {
  566. const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
  567. const __m128i left_values = _mm_loadl_epi64((const __m128i*)left);
  568. const __m128i combined = _mm_unpacklo_epi64(top_values, left_values);
  569. const int DC = VP8HorizontalAdd8b(&combined) + 8;
  570. Put8x8uv_SSE2(DC >> 4, dst);
  571. }
  572. static WEBP_INLINE void DC8uvNoLeft_SSE2(uint8_t* dst, const uint8_t* top) {
  573. const __m128i zero = _mm_setzero_si128();
  574. const __m128i top_values = _mm_loadl_epi64((const __m128i*)top);
  575. const __m128i sum = _mm_sad_epu8(top_values, zero);
  576. const int DC = _mm_cvtsi128_si32(sum) + 4;
  577. Put8x8uv_SSE2(DC >> 3, dst);
  578. }
  579. static WEBP_INLINE void DC8uvNoTop_SSE2(uint8_t* dst, const uint8_t* left) {
  580. // 'left' is contiguous so we can reuse the top summation.
  581. DC8uvNoLeft_SSE2(dst, left);
  582. }
  583. static WEBP_INLINE void DC8uvNoTopLeft_SSE2(uint8_t* dst) {
  584. Put8x8uv_SSE2(0x80, dst);
  585. }
  586. static WEBP_INLINE void DC8uvMode_SSE2(uint8_t* dst, const uint8_t* left,
  587. const uint8_t* top) {
  588. if (top != NULL) {
  589. if (left != NULL) { // top and left present
  590. DC8uv_SSE2(dst, left, top);
  591. } else { // top, but no left
  592. DC8uvNoLeft_SSE2(dst, top);
  593. }
  594. } else if (left != NULL) { // left but no top
  595. DC8uvNoTop_SSE2(dst, left);
  596. } else { // no top, no left, nothing.
  597. DC8uvNoTopLeft_SSE2(dst);
  598. }
  599. }
  600. static WEBP_INLINE void DC16_SSE2(uint8_t* dst, const uint8_t* left,
  601. const uint8_t* top) {
  602. const __m128i top_row = _mm_load_si128((const __m128i*)top);
  603. const __m128i left_row = _mm_load_si128((const __m128i*)left);
  604. const int DC =
  605. VP8HorizontalAdd8b(&top_row) + VP8HorizontalAdd8b(&left_row) + 16;
  606. Put16_SSE2(DC >> 5, dst);
  607. }
  608. static WEBP_INLINE void DC16NoLeft_SSE2(uint8_t* dst, const uint8_t* top) {
  609. const __m128i top_row = _mm_load_si128((const __m128i*)top);
  610. const int DC = VP8HorizontalAdd8b(&top_row) + 8;
  611. Put16_SSE2(DC >> 4, dst);
  612. }
  613. static WEBP_INLINE void DC16NoTop_SSE2(uint8_t* dst, const uint8_t* left) {
  614. // 'left' is contiguous so we can reuse the top summation.
  615. DC16NoLeft_SSE2(dst, left);
  616. }
  617. static WEBP_INLINE void DC16NoTopLeft_SSE2(uint8_t* dst) {
  618. Put16_SSE2(0x80, dst);
  619. }
  620. static WEBP_INLINE void DC16Mode_SSE2(uint8_t* dst, const uint8_t* left,
  621. const uint8_t* top) {
  622. if (top != NULL) {
  623. if (left != NULL) { // top and left present
  624. DC16_SSE2(dst, left, top);
  625. } else { // top, but no left
  626. DC16NoLeft_SSE2(dst, top);
  627. }
  628. } else if (left != NULL) { // left but no top
  629. DC16NoTop_SSE2(dst, left);
  630. } else { // no top, no left, nothing.
  631. DC16NoTopLeft_SSE2(dst);
  632. }
  633. }
  634. //------------------------------------------------------------------------------
  635. // 4x4 predictions
  636. #define DST(x, y) dst[(x) + (y) * BPS]
  637. #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
  638. #define AVG2(a, b) (((a) + (b) + 1) >> 1)
  639. // We use the following 8b-arithmetic tricks:
  640. // (a + 2 * b + c + 2) >> 2 = (AC + b + 1) >> 1
  641. // where: AC = (a + c) >> 1 = [(a + c + 1) >> 1] - [(a^c) & 1]
  642. // and:
  643. // (a + 2 * b + c + 2) >> 2 = (AB + BC + 1) >> 1 - (ab|bc)&lsb
  644. // where: AC = (a + b + 1) >> 1, BC = (b + c + 1) >> 1
  645. // and ab = a ^ b, bc = b ^ c, lsb = (AC^BC)&1
  646. static WEBP_INLINE void VE4_SSE2(uint8_t* dst,
  647. const uint8_t* top) { // vertical
  648. const __m128i one = _mm_set1_epi8(1);
  649. const __m128i ABCDEFGH = _mm_loadl_epi64((__m128i*)(top - 1));
  650. const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
  651. const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
  652. const __m128i a = _mm_avg_epu8(ABCDEFGH, CDEFGH00);
  653. const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGH00), one);
  654. const __m128i b = _mm_subs_epu8(a, lsb);
  655. const __m128i avg = _mm_avg_epu8(b, BCDEFGH0);
  656. const uint32_t vals = _mm_cvtsi128_si32(avg);
  657. int i;
  658. for (i = 0; i < 4; ++i) {
  659. WebPUint32ToMem(dst + i * BPS, vals);
  660. }
  661. }
  662. static WEBP_INLINE void HE4_SSE2(uint8_t* dst,
  663. const uint8_t* top) { // horizontal
  664. const int X = top[-1];
  665. const int I = top[-2];
  666. const int J = top[-3];
  667. const int K = top[-4];
  668. const int L = top[-5];
  669. WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));
  670. WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));
  671. WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));
  672. WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));
  673. }
  674. static WEBP_INLINE void DC4_SSE2(uint8_t* dst, const uint8_t* top) {
  675. uint32_t dc = 4;
  676. int i;
  677. for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
  678. Fill_SSE2(dst, dc >> 3, 4);
  679. }
  680. static WEBP_INLINE void LD4_SSE2(uint8_t* dst,
  681. const uint8_t* top) { // Down-Left
  682. const __m128i one = _mm_set1_epi8(1);
  683. const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);
  684. const __m128i BCDEFGH0 = _mm_srli_si128(ABCDEFGH, 1);
  685. const __m128i CDEFGH00 = _mm_srli_si128(ABCDEFGH, 2);
  686. const __m128i CDEFGHH0 = _mm_insert_epi16(CDEFGH00, top[7], 3);
  687. const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, CDEFGHH0);
  688. const __m128i lsb = _mm_and_si128(_mm_xor_si128(ABCDEFGH, CDEFGHH0), one);
  689. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  690. const __m128i abcdefg = _mm_avg_epu8(avg2, BCDEFGH0);
  691. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcdefg ));
  692. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
  693. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
  694. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
  695. }
  696. static WEBP_INLINE void VR4_SSE2(uint8_t* dst,
  697. const uint8_t* top) { // Vertical-Right
  698. const __m128i one = _mm_set1_epi8(1);
  699. const int I = top[-2];
  700. const int J = top[-3];
  701. const int K = top[-4];
  702. const int X = top[-1];
  703. const __m128i XABCD = _mm_loadl_epi64((const __m128i*)(top - 1));
  704. const __m128i ABCD0 = _mm_srli_si128(XABCD, 1);
  705. const __m128i abcd = _mm_avg_epu8(XABCD, ABCD0);
  706. const __m128i _XABCD = _mm_slli_si128(XABCD, 1);
  707. const __m128i IXABCD = _mm_insert_epi16(_XABCD, (short)(I | (X << 8)), 0);
  708. const __m128i avg1 = _mm_avg_epu8(IXABCD, ABCD0);
  709. const __m128i lsb = _mm_and_si128(_mm_xor_si128(IXABCD, ABCD0), one);
  710. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  711. const __m128i efgh = _mm_avg_epu8(avg2, XABCD);
  712. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( abcd ));
  713. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( efgh ));
  714. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(abcd, 1)));
  715. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_slli_si128(efgh, 1)));
  716. // these two are hard to implement in SSE2, so we keep the C-version:
  717. DST(0, 2) = AVG3(J, I, X);
  718. DST(0, 3) = AVG3(K, J, I);
  719. }
  720. static WEBP_INLINE void VL4_SSE2(uint8_t* dst,
  721. const uint8_t* top) { // Vertical-Left
  722. const __m128i one = _mm_set1_epi8(1);
  723. const __m128i ABCDEFGH = _mm_loadl_epi64((const __m128i*)top);
  724. const __m128i BCDEFGH_ = _mm_srli_si128(ABCDEFGH, 1);
  725. const __m128i CDEFGH__ = _mm_srli_si128(ABCDEFGH, 2);
  726. const __m128i avg1 = _mm_avg_epu8(ABCDEFGH, BCDEFGH_);
  727. const __m128i avg2 = _mm_avg_epu8(CDEFGH__, BCDEFGH_);
  728. const __m128i avg3 = _mm_avg_epu8(avg1, avg2);
  729. const __m128i lsb1 = _mm_and_si128(_mm_xor_si128(avg1, avg2), one);
  730. const __m128i ab = _mm_xor_si128(ABCDEFGH, BCDEFGH_);
  731. const __m128i bc = _mm_xor_si128(CDEFGH__, BCDEFGH_);
  732. const __m128i abbc = _mm_or_si128(ab, bc);
  733. const __m128i lsb2 = _mm_and_si128(abbc, lsb1);
  734. const __m128i avg4 = _mm_subs_epu8(avg3, lsb2);
  735. const uint32_t extra_out = _mm_cvtsi128_si32(_mm_srli_si128(avg4, 4));
  736. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32( avg1 ));
  737. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32( avg4 ));
  738. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg1, 1)));
  739. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(avg4, 1)));
  740. // these two are hard to get and irregular
  741. DST(3, 2) = (extra_out >> 0) & 0xff;
  742. DST(3, 3) = (extra_out >> 8) & 0xff;
  743. }
  744. static WEBP_INLINE void RD4_SSE2(uint8_t* dst,
  745. const uint8_t* top) { // Down-right
  746. const __m128i one = _mm_set1_epi8(1);
  747. const __m128i LKJIXABC = _mm_loadl_epi64((const __m128i*)(top - 5));
  748. const __m128i LKJIXABCD = _mm_insert_epi16(LKJIXABC, top[3], 4);
  749. const __m128i KJIXABCD_ = _mm_srli_si128(LKJIXABCD, 1);
  750. const __m128i JIXABCD__ = _mm_srli_si128(LKJIXABCD, 2);
  751. const __m128i avg1 = _mm_avg_epu8(JIXABCD__, LKJIXABCD);
  752. const __m128i lsb = _mm_and_si128(_mm_xor_si128(JIXABCD__, LKJIXABCD), one);
  753. const __m128i avg2 = _mm_subs_epu8(avg1, lsb);
  754. const __m128i abcdefg = _mm_avg_epu8(avg2, KJIXABCD_);
  755. WebPUint32ToMem(dst + 3 * BPS, _mm_cvtsi128_si32( abcdefg ));
  756. WebPUint32ToMem(dst + 2 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 1)));
  757. WebPUint32ToMem(dst + 1 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 2)));
  758. WebPUint32ToMem(dst + 0 * BPS, _mm_cvtsi128_si32(_mm_srli_si128(abcdefg, 3)));
  759. }
  760. static WEBP_INLINE void HU4_SSE2(uint8_t* dst, const uint8_t* top) {
  761. const int I = top[-2];
  762. const int J = top[-3];
  763. const int K = top[-4];
  764. const int L = top[-5];
  765. DST(0, 0) = AVG2(I, J);
  766. DST(2, 0) = DST(0, 1) = AVG2(J, K);
  767. DST(2, 1) = DST(0, 2) = AVG2(K, L);
  768. DST(1, 0) = AVG3(I, J, K);
  769. DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
  770. DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
  771. DST(3, 2) = DST(2, 2) =
  772. DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
  773. }
  774. static WEBP_INLINE void HD4_SSE2(uint8_t* dst, const uint8_t* top) {
  775. const int X = top[-1];
  776. const int I = top[-2];
  777. const int J = top[-3];
  778. const int K = top[-4];
  779. const int L = top[-5];
  780. const int A = top[0];
  781. const int B = top[1];
  782. const int C = top[2];
  783. DST(0, 0) = DST(2, 1) = AVG2(I, X);
  784. DST(0, 1) = DST(2, 2) = AVG2(J, I);
  785. DST(0, 2) = DST(2, 3) = AVG2(K, J);
  786. DST(0, 3) = AVG2(L, K);
  787. DST(3, 0) = AVG3(A, B, C);
  788. DST(2, 0) = AVG3(X, A, B);
  789. DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
  790. DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
  791. DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
  792. DST(1, 3) = AVG3(L, K, J);
  793. }
  794. static WEBP_INLINE void TM4_SSE2(uint8_t* dst, const uint8_t* top) {
  795. const __m128i zero = _mm_setzero_si128();
  796. const __m128i top_values = _mm_cvtsi32_si128(WebPMemToUint32(top));
  797. const __m128i top_base = _mm_unpacklo_epi8(top_values, zero);
  798. int y;
  799. for (y = 0; y < 4; ++y, dst += BPS) {
  800. const int val = top[-2 - y] - top[-1];
  801. const __m128i base = _mm_set1_epi16(val);
  802. const __m128i out = _mm_packus_epi16(_mm_add_epi16(base, top_base), zero);
  803. WebPUint32ToMem(dst, _mm_cvtsi128_si32(out));
  804. }
  805. }
  806. #undef DST
  807. #undef AVG3
  808. #undef AVG2
  809. //------------------------------------------------------------------------------
  810. // luma 4x4 prediction
  811. // Left samples are top[-5 .. -2], top_left is top[-1], top are
  812. // located at top[0..3], and top right is top[4..7]
  813. static void Intra4Preds_SSE2(uint8_t* dst, const uint8_t* top) {
  814. DC4_SSE2(I4DC4 + dst, top);
  815. TM4_SSE2(I4TM4 + dst, top);
  816. VE4_SSE2(I4VE4 + dst, top);
  817. HE4_SSE2(I4HE4 + dst, top);
  818. RD4_SSE2(I4RD4 + dst, top);
  819. VR4_SSE2(I4VR4 + dst, top);
  820. LD4_SSE2(I4LD4 + dst, top);
  821. VL4_SSE2(I4VL4 + dst, top);
  822. HD4_SSE2(I4HD4 + dst, top);
  823. HU4_SSE2(I4HU4 + dst, top);
  824. }
  825. //------------------------------------------------------------------------------
  826. // Chroma 8x8 prediction (paragraph 12.2)
  827. static void IntraChromaPreds_SSE2(uint8_t* dst, const uint8_t* left,
  828. const uint8_t* top) {
  829. // U block
  830. DC8uvMode_SSE2(C8DC8 + dst, left, top);
  831. VerticalPred_SSE2(C8VE8 + dst, top, 8);
  832. HorizontalPred_SSE2(C8HE8 + dst, left, 8);
  833. TrueMotion_SSE2(C8TM8 + dst, left, top, 8);
  834. // V block
  835. dst += 8;
  836. if (top != NULL) top += 8;
  837. if (left != NULL) left += 16;
  838. DC8uvMode_SSE2(C8DC8 + dst, left, top);
  839. VerticalPred_SSE2(C8VE8 + dst, top, 8);
  840. HorizontalPred_SSE2(C8HE8 + dst, left, 8);
  841. TrueMotion_SSE2(C8TM8 + dst, left, top, 8);
  842. }
  843. //------------------------------------------------------------------------------
  844. // luma 16x16 prediction (paragraph 12.3)
  845. static void Intra16Preds_SSE2(uint8_t* dst,
  846. const uint8_t* left, const uint8_t* top) {
  847. DC16Mode_SSE2(I16DC16 + dst, left, top);
  848. VerticalPred_SSE2(I16VE16 + dst, top, 16);
  849. HorizontalPred_SSE2(I16HE16 + dst, left, 16);
  850. TrueMotion_SSE2(I16TM16 + dst, left, top, 16);
  851. }
  852. //------------------------------------------------------------------------------
  853. // Metric
  854. static WEBP_INLINE void SubtractAndAccumulate_SSE2(const __m128i a,
  855. const __m128i b,
  856. __m128i* const sum) {
  857. // take abs(a-b) in 8b
  858. const __m128i a_b = _mm_subs_epu8(a, b);
  859. const __m128i b_a = _mm_subs_epu8(b, a);
  860. const __m128i abs_a_b = _mm_or_si128(a_b, b_a);
  861. // zero-extend to 16b
  862. const __m128i zero = _mm_setzero_si128();
  863. const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero);
  864. const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero);
  865. // multiply with self
  866. const __m128i sum1 = _mm_madd_epi16(C0, C0);
  867. const __m128i sum2 = _mm_madd_epi16(C1, C1);
  868. *sum = _mm_add_epi32(sum1, sum2);
  869. }
  870. static WEBP_INLINE int SSE_16xN_SSE2(const uint8_t* a, const uint8_t* b,
  871. int num_pairs) {
  872. __m128i sum = _mm_setzero_si128();
  873. int32_t tmp[4];
  874. int i;
  875. for (i = 0; i < num_pairs; ++i) {
  876. const __m128i a0 = _mm_loadu_si128((const __m128i*)&a[BPS * 0]);
  877. const __m128i b0 = _mm_loadu_si128((const __m128i*)&b[BPS * 0]);
  878. const __m128i a1 = _mm_loadu_si128((const __m128i*)&a[BPS * 1]);
  879. const __m128i b1 = _mm_loadu_si128((const __m128i*)&b[BPS * 1]);
  880. __m128i sum1, sum2;
  881. SubtractAndAccumulate_SSE2(a0, b0, &sum1);
  882. SubtractAndAccumulate_SSE2(a1, b1, &sum2);
  883. sum = _mm_add_epi32(sum, _mm_add_epi32(sum1, sum2));
  884. a += 2 * BPS;
  885. b += 2 * BPS;
  886. }
  887. _mm_storeu_si128((__m128i*)tmp, sum);
  888. return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
  889. }
  890. static int SSE16x16_SSE2(const uint8_t* a, const uint8_t* b) {
  891. return SSE_16xN_SSE2(a, b, 8);
  892. }
  893. static int SSE16x8_SSE2(const uint8_t* a, const uint8_t* b) {
  894. return SSE_16xN_SSE2(a, b, 4);
  895. }
  896. #define LOAD_8x16b(ptr) \
  897. _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(ptr)), zero)
  898. static int SSE8x8_SSE2(const uint8_t* a, const uint8_t* b) {
  899. const __m128i zero = _mm_setzero_si128();
  900. int num_pairs = 4;
  901. __m128i sum = zero;
  902. int32_t tmp[4];
  903. while (num_pairs-- > 0) {
  904. const __m128i a0 = LOAD_8x16b(&a[BPS * 0]);
  905. const __m128i a1 = LOAD_8x16b(&a[BPS * 1]);
  906. const __m128i b0 = LOAD_8x16b(&b[BPS * 0]);
  907. const __m128i b1 = LOAD_8x16b(&b[BPS * 1]);
  908. // subtract
  909. const __m128i c0 = _mm_subs_epi16(a0, b0);
  910. const __m128i c1 = _mm_subs_epi16(a1, b1);
  911. // multiply/accumulate with self
  912. const __m128i d0 = _mm_madd_epi16(c0, c0);
  913. const __m128i d1 = _mm_madd_epi16(c1, c1);
  914. // collect
  915. const __m128i sum01 = _mm_add_epi32(d0, d1);
  916. sum = _mm_add_epi32(sum, sum01);
  917. a += 2 * BPS;
  918. b += 2 * BPS;
  919. }
  920. _mm_storeu_si128((__m128i*)tmp, sum);
  921. return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
  922. }
  923. #undef LOAD_8x16b
  924. static int SSE4x4_SSE2(const uint8_t* a, const uint8_t* b) {
  925. const __m128i zero = _mm_setzero_si128();
  926. // Load values. Note that we read 8 pixels instead of 4,
  927. // but the a/b buffers are over-allocated to that effect.
  928. const __m128i a0 = _mm_loadl_epi64((const __m128i*)&a[BPS * 0]);
  929. const __m128i a1 = _mm_loadl_epi64((const __m128i*)&a[BPS * 1]);
  930. const __m128i a2 = _mm_loadl_epi64((const __m128i*)&a[BPS * 2]);
  931. const __m128i a3 = _mm_loadl_epi64((const __m128i*)&a[BPS * 3]);
  932. const __m128i b0 = _mm_loadl_epi64((const __m128i*)&b[BPS * 0]);
  933. const __m128i b1 = _mm_loadl_epi64((const __m128i*)&b[BPS * 1]);
  934. const __m128i b2 = _mm_loadl_epi64((const __m128i*)&b[BPS * 2]);
  935. const __m128i b3 = _mm_loadl_epi64((const __m128i*)&b[BPS * 3]);
  936. // Combine pair of lines.
  937. const __m128i a01 = _mm_unpacklo_epi32(a0, a1);
  938. const __m128i a23 = _mm_unpacklo_epi32(a2, a3);
  939. const __m128i b01 = _mm_unpacklo_epi32(b0, b1);
  940. const __m128i b23 = _mm_unpacklo_epi32(b2, b3);
  941. // Convert to 16b.
  942. const __m128i a01s = _mm_unpacklo_epi8(a01, zero);
  943. const __m128i a23s = _mm_unpacklo_epi8(a23, zero);
  944. const __m128i b01s = _mm_unpacklo_epi8(b01, zero);
  945. const __m128i b23s = _mm_unpacklo_epi8(b23, zero);
  946. // subtract, square and accumulate
  947. const __m128i d0 = _mm_subs_epi16(a01s, b01s);
  948. const __m128i d1 = _mm_subs_epi16(a23s, b23s);
  949. const __m128i e0 = _mm_madd_epi16(d0, d0);
  950. const __m128i e1 = _mm_madd_epi16(d1, d1);
  951. const __m128i sum = _mm_add_epi32(e0, e1);
  952. int32_t tmp[4];
  953. _mm_storeu_si128((__m128i*)tmp, sum);
  954. return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
  955. }
  956. //------------------------------------------------------------------------------
  957. static void Mean16x4_SSE2(const uint8_t* ref, uint32_t dc[4]) {
  958. const __m128i mask = _mm_set1_epi16(0x00ff);
  959. const __m128i a0 = _mm_loadu_si128((const __m128i*)&ref[BPS * 0]);
  960. const __m128i a1 = _mm_loadu_si128((const __m128i*)&ref[BPS * 1]);
  961. const __m128i a2 = _mm_loadu_si128((const __m128i*)&ref[BPS * 2]);
  962. const __m128i a3 = _mm_loadu_si128((const __m128i*)&ref[BPS * 3]);
  963. const __m128i b0 = _mm_srli_epi16(a0, 8); // hi byte
  964. const __m128i b1 = _mm_srli_epi16(a1, 8);
  965. const __m128i b2 = _mm_srli_epi16(a2, 8);
  966. const __m128i b3 = _mm_srli_epi16(a3, 8);
  967. const __m128i c0 = _mm_and_si128(a0, mask); // lo byte
  968. const __m128i c1 = _mm_and_si128(a1, mask);
  969. const __m128i c2 = _mm_and_si128(a2, mask);
  970. const __m128i c3 = _mm_and_si128(a3, mask);
  971. const __m128i d0 = _mm_add_epi32(b0, c0);
  972. const __m128i d1 = _mm_add_epi32(b1, c1);
  973. const __m128i d2 = _mm_add_epi32(b2, c2);
  974. const __m128i d3 = _mm_add_epi32(b3, c3);
  975. const __m128i e0 = _mm_add_epi32(d0, d1);
  976. const __m128i e1 = _mm_add_epi32(d2, d3);
  977. const __m128i f0 = _mm_add_epi32(e0, e1);
  978. uint16_t tmp[8];
  979. _mm_storeu_si128((__m128i*)tmp, f0);
  980. dc[0] = tmp[0] + tmp[1];
  981. dc[1] = tmp[2] + tmp[3];
  982. dc[2] = tmp[4] + tmp[5];
  983. dc[3] = tmp[6] + tmp[7];
  984. }
  985. //------------------------------------------------------------------------------
  986. // Texture distortion
  987. //
  988. // We try to match the spectral content (weighted) between source and
  989. // reconstructed samples.
  990. // Hadamard transform
  991. // Returns the weighted sum of the absolute value of transformed coefficients.
  992. // w[] contains a row-major 4 by 4 symmetric matrix.
  993. static int TTransform_SSE2(const uint8_t* inA, const uint8_t* inB,
  994. const uint16_t* const w) {
  995. int32_t sum[4];
  996. __m128i tmp_0, tmp_1, tmp_2, tmp_3;
  997. const __m128i zero = _mm_setzero_si128();
  998. // Load and combine inputs.
  999. {
  1000. const __m128i inA_0 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 0]);
  1001. const __m128i inA_1 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 1]);
  1002. const __m128i inA_2 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 2]);
  1003. const __m128i inA_3 = _mm_loadl_epi64((const __m128i*)&inA[BPS * 3]);
  1004. const __m128i inB_0 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 0]);
  1005. const __m128i inB_1 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 1]);
  1006. const __m128i inB_2 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 2]);
  1007. const __m128i inB_3 = _mm_loadl_epi64((const __m128i*)&inB[BPS * 3]);
  1008. // Combine inA and inB (we'll do two transforms in parallel).
  1009. const __m128i inAB_0 = _mm_unpacklo_epi32(inA_0, inB_0);
  1010. const __m128i inAB_1 = _mm_unpacklo_epi32(inA_1, inB_1);
  1011. const __m128i inAB_2 = _mm_unpacklo_epi32(inA_2, inB_2);
  1012. const __m128i inAB_3 = _mm_unpacklo_epi32(inA_3, inB_3);
  1013. tmp_0 = _mm_unpacklo_epi8(inAB_0, zero);
  1014. tmp_1 = _mm_unpacklo_epi8(inAB_1, zero);
  1015. tmp_2 = _mm_unpacklo_epi8(inAB_2, zero);
  1016. tmp_3 = _mm_unpacklo_epi8(inAB_3, zero);
  1017. // a00 a01 a02 a03 b00 b01 b02 b03
  1018. // a10 a11 a12 a13 b10 b11 b12 b13
  1019. // a20 a21 a22 a23 b20 b21 b22 b23
  1020. // a30 a31 a32 a33 b30 b31 b32 b33
  1021. }
  1022. // Vertical pass first to avoid a transpose (vertical and horizontal passes
  1023. // are commutative because w/kWeightY is symmetric) and subsequent transpose.
  1024. {
  1025. // Calculate a and b (two 4x4 at once).
  1026. const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
  1027. const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
  1028. const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
  1029. const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
  1030. const __m128i b0 = _mm_add_epi16(a0, a1);
  1031. const __m128i b1 = _mm_add_epi16(a3, a2);
  1032. const __m128i b2 = _mm_sub_epi16(a3, a2);
  1033. const __m128i b3 = _mm_sub_epi16(a0, a1);
  1034. // a00 a01 a02 a03 b00 b01 b02 b03
  1035. // a10 a11 a12 a13 b10 b11 b12 b13
  1036. // a20 a21 a22 a23 b20 b21 b22 b23
  1037. // a30 a31 a32 a33 b30 b31 b32 b33
  1038. // Transpose the two 4x4.
  1039. VP8Transpose_2_4x4_16b(&b0, &b1, &b2, &b3, &tmp_0, &tmp_1, &tmp_2, &tmp_3);
  1040. }
  1041. // Horizontal pass and difference of weighted sums.
  1042. {
  1043. // Load all inputs.
  1044. const __m128i w_0 = _mm_loadu_si128((const __m128i*)&w[0]);
  1045. const __m128i w_8 = _mm_loadu_si128((const __m128i*)&w[8]);
  1046. // Calculate a and b (two 4x4 at once).
  1047. const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
  1048. const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
  1049. const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
  1050. const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
  1051. const __m128i b0 = _mm_add_epi16(a0, a1);
  1052. const __m128i b1 = _mm_add_epi16(a3, a2);
  1053. const __m128i b2 = _mm_sub_epi16(a3, a2);
  1054. const __m128i b3 = _mm_sub_epi16(a0, a1);
  1055. // Separate the transforms of inA and inB.
  1056. __m128i A_b0 = _mm_unpacklo_epi64(b0, b1);
  1057. __m128i A_b2 = _mm_unpacklo_epi64(b2, b3);
  1058. __m128i B_b0 = _mm_unpackhi_epi64(b0, b1);
  1059. __m128i B_b2 = _mm_unpackhi_epi64(b2, b3);
  1060. {
  1061. const __m128i d0 = _mm_sub_epi16(zero, A_b0);
  1062. const __m128i d1 = _mm_sub_epi16(zero, A_b2);
  1063. const __m128i d2 = _mm_sub_epi16(zero, B_b0);
  1064. const __m128i d3 = _mm_sub_epi16(zero, B_b2);
  1065. A_b0 = _mm_max_epi16(A_b0, d0); // abs(v), 16b
  1066. A_b2 = _mm_max_epi16(A_b2, d1);
  1067. B_b0 = _mm_max_epi16(B_b0, d2);
  1068. B_b2 = _mm_max_epi16(B_b2, d3);
  1069. }
  1070. // weighted sums
  1071. A_b0 = _mm_madd_epi16(A_b0, w_0);
  1072. A_b2 = _mm_madd_epi16(A_b2, w_8);
  1073. B_b0 = _mm_madd_epi16(B_b0, w_0);
  1074. B_b2 = _mm_madd_epi16(B_b2, w_8);
  1075. A_b0 = _mm_add_epi32(A_b0, A_b2);
  1076. B_b0 = _mm_add_epi32(B_b0, B_b2);
  1077. // difference of weighted sums
  1078. A_b0 = _mm_sub_epi32(A_b0, B_b0);
  1079. _mm_storeu_si128((__m128i*)&sum[0], A_b0);
  1080. }
  1081. return sum[0] + sum[1] + sum[2] + sum[3];
  1082. }
  1083. static int Disto4x4_SSE2(const uint8_t* const a, const uint8_t* const b,
  1084. const uint16_t* const w) {
  1085. const int diff_sum = TTransform_SSE2(a, b, w);
  1086. return abs(diff_sum) >> 5;
  1087. }
  1088. static int Disto16x16_SSE2(const uint8_t* const a, const uint8_t* const b,
  1089. const uint16_t* const w) {
  1090. int D = 0;
  1091. int x, y;
  1092. for (y = 0; y < 16 * BPS; y += 4 * BPS) {
  1093. for (x = 0; x < 16; x += 4) {
  1094. D += Disto4x4_SSE2(a + x + y, b + x + y, w);
  1095. }
  1096. }
  1097. return D;
  1098. }
  1099. //------------------------------------------------------------------------------
  1100. // Quantization
  1101. //
  1102. static WEBP_INLINE int DoQuantizeBlock_SSE2(int16_t in[16], int16_t out[16],
  1103. const uint16_t* const sharpen,
  1104. const VP8Matrix* const mtx) {
  1105. const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);
  1106. const __m128i zero = _mm_setzero_si128();
  1107. __m128i coeff0, coeff8;
  1108. __m128i out0, out8;
  1109. __m128i packed_out;
  1110. // Load all inputs.
  1111. __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);
  1112. __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);
  1113. const __m128i iq0 = _mm_loadu_si128((const __m128i*)&mtx->iq_[0]);
  1114. const __m128i iq8 = _mm_loadu_si128((const __m128i*)&mtx->iq_[8]);
  1115. const __m128i q0 = _mm_loadu_si128((const __m128i*)&mtx->q_[0]);
  1116. const __m128i q8 = _mm_loadu_si128((const __m128i*)&mtx->q_[8]);
  1117. // extract sign(in) (0x0000 if positive, 0xffff if negative)
  1118. const __m128i sign0 = _mm_cmpgt_epi16(zero, in0);
  1119. const __m128i sign8 = _mm_cmpgt_epi16(zero, in8);
  1120. // coeff = abs(in) = (in ^ sign) - sign
  1121. coeff0 = _mm_xor_si128(in0, sign0);
  1122. coeff8 = _mm_xor_si128(in8, sign8);
  1123. coeff0 = _mm_sub_epi16(coeff0, sign0);
  1124. coeff8 = _mm_sub_epi16(coeff8, sign8);
  1125. // coeff = abs(in) + sharpen
  1126. if (sharpen != NULL) {
  1127. const __m128i sharpen0 = _mm_loadu_si128((const __m128i*)&sharpen[0]);
  1128. const __m128i sharpen8 = _mm_loadu_si128((const __m128i*)&sharpen[8]);
  1129. coeff0 = _mm_add_epi16(coeff0, sharpen0);
  1130. coeff8 = _mm_add_epi16(coeff8, sharpen8);
  1131. }
  1132. // out = (coeff * iQ + B) >> QFIX
  1133. {
  1134. // doing calculations with 32b precision (QFIX=17)
  1135. // out = (coeff * iQ)
  1136. const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);
  1137. const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);
  1138. const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);
  1139. const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);
  1140. __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);
  1141. __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);
  1142. __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);
  1143. __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);
  1144. // out = (coeff * iQ + B)
  1145. const __m128i bias_00 = _mm_loadu_si128((const __m128i*)&mtx->bias_[0]);
  1146. const __m128i bias_04 = _mm_loadu_si128((const __m128i*)&mtx->bias_[4]);
  1147. const __m128i bias_08 = _mm_loadu_si128((const __m128i*)&mtx->bias_[8]);
  1148. const __m128i bias_12 = _mm_loadu_si128((const __m128i*)&mtx->bias_[12]);
  1149. out_00 = _mm_add_epi32(out_00, bias_00);
  1150. out_04 = _mm_add_epi32(out_04, bias_04);
  1151. out_08 = _mm_add_epi32(out_08, bias_08);
  1152. out_12 = _mm_add_epi32(out_12, bias_12);
  1153. // out = QUANTDIV(coeff, iQ, B, QFIX)
  1154. out_00 = _mm_srai_epi32(out_00, QFIX);
  1155. out_04 = _mm_srai_epi32(out_04, QFIX);
  1156. out_08 = _mm_srai_epi32(out_08, QFIX);
  1157. out_12 = _mm_srai_epi32(out_12, QFIX);
  1158. // pack result as 16b
  1159. out0 = _mm_packs_epi32(out_00, out_04);
  1160. out8 = _mm_packs_epi32(out_08, out_12);
  1161. // if (coeff > 2047) coeff = 2047
  1162. out0 = _mm_min_epi16(out0, max_coeff_2047);
  1163. out8 = _mm_min_epi16(out8, max_coeff_2047);
  1164. }
  1165. // get sign back (if (sign[j]) out_n = -out_n)
  1166. out0 = _mm_xor_si128(out0, sign0);
  1167. out8 = _mm_xor_si128(out8, sign8);
  1168. out0 = _mm_sub_epi16(out0, sign0);
  1169. out8 = _mm_sub_epi16(out8, sign8);
  1170. // in = out * Q
  1171. in0 = _mm_mullo_epi16(out0, q0);
  1172. in8 = _mm_mullo_epi16(out8, q8);
  1173. _mm_storeu_si128((__m128i*)&in[0], in0);
  1174. _mm_storeu_si128((__m128i*)&in[8], in8);
  1175. // zigzag the output before storing it.
  1176. //
  1177. // The zigzag pattern can almost be reproduced with a small sequence of
  1178. // shuffles. After it, we only need to swap the 7th (ending up in third
  1179. // position instead of twelfth) and 8th values.
  1180. {
  1181. __m128i outZ0, outZ8;
  1182. outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0));
  1183. outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0));
  1184. outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));
  1185. outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1));
  1186. outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0));
  1187. outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));
  1188. _mm_storeu_si128((__m128i*)&out[0], outZ0);
  1189. _mm_storeu_si128((__m128i*)&out[8], outZ8);
  1190. packed_out = _mm_packs_epi16(outZ0, outZ8);
  1191. }
  1192. {
  1193. const int16_t outZ_12 = out[12];
  1194. const int16_t outZ_3 = out[3];
  1195. out[3] = outZ_12;
  1196. out[12] = outZ_3;
  1197. }
  1198. // detect if all 'out' values are zeroes or not
  1199. return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff);
  1200. }
  1201. static int QuantizeBlock_SSE2(int16_t in[16], int16_t out[16],
  1202. const VP8Matrix* const mtx) {
  1203. return DoQuantizeBlock_SSE2(in, out, &mtx->sharpen_[0], mtx);
  1204. }
  1205. static int QuantizeBlockWHT_SSE2(int16_t in[16], int16_t out[16],
  1206. const VP8Matrix* const mtx) {
  1207. return DoQuantizeBlock_SSE2(in, out, NULL, mtx);
  1208. }
  1209. static int Quantize2Blocks_SSE2(int16_t in[32], int16_t out[32],
  1210. const VP8Matrix* const mtx) {
  1211. int nz;
  1212. const uint16_t* const sharpen = &mtx->sharpen_[0];
  1213. nz = DoQuantizeBlock_SSE2(in + 0 * 16, out + 0 * 16, sharpen, mtx) << 0;
  1214. nz |= DoQuantizeBlock_SSE2(in + 1 * 16, out + 1 * 16, sharpen, mtx) << 1;
  1215. return nz;
  1216. }
  1217. //------------------------------------------------------------------------------
  1218. // Entry point
  1219. extern void VP8EncDspInitSSE2(void);
  1220. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitSSE2(void) {
  1221. VP8CollectHistogram = CollectHistogram_SSE2;
  1222. VP8EncPredLuma16 = Intra16Preds_SSE2;
  1223. VP8EncPredChroma8 = IntraChromaPreds_SSE2;
  1224. VP8EncPredLuma4 = Intra4Preds_SSE2;
  1225. VP8EncQuantizeBlock = QuantizeBlock_SSE2;
  1226. VP8EncQuantize2Blocks = Quantize2Blocks_SSE2;
  1227. VP8EncQuantizeBlockWHT = QuantizeBlockWHT_SSE2;
  1228. VP8ITransform = ITransform_SSE2;
  1229. VP8FTransform = FTransform_SSE2;
  1230. VP8FTransform2 = FTransform2_SSE2;
  1231. VP8FTransformWHT = FTransformWHT_SSE2;
  1232. VP8SSE16x16 = SSE16x16_SSE2;
  1233. VP8SSE16x8 = SSE16x8_SSE2;
  1234. VP8SSE8x8 = SSE8x8_SSE2;
  1235. VP8SSE4x4 = SSE4x4_SSE2;
  1236. VP8TDisto4x4 = Disto4x4_SSE2;
  1237. VP8TDisto16x16 = Disto16x16_SSE2;
  1238. VP8Mean16x4 = Mean16x4_SSE2;
  1239. }
  1240. #else // !WEBP_USE_SSE2
  1241. WEBP_DSP_INIT_STUB(VP8EncDspInitSSE2)
  1242. #endif // WEBP_USE_SSE2