enc_sse2.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 <stdlib.h> // for abs()
  16. #include <emmintrin.h>
  17. #include "../enc/cost.h"
  18. #include "../enc/vp8enci.h"
  19. #include "../utils/utils.h"
  20. //------------------------------------------------------------------------------
  21. // Quite useful macro for debugging. Left here for convenience.
  22. #if 0
  23. #include <stdio.h>
  24. static void PrintReg(const __m128i r, const char* const name, int size) {
  25. int n;
  26. union {
  27. __m128i r;
  28. uint8_t i8[16];
  29. uint16_t i16[8];
  30. uint32_t i32[4];
  31. uint64_t i64[2];
  32. } tmp;
  33. tmp.r = r;
  34. printf("%s\t: ", name);
  35. if (size == 8) {
  36. for (n = 0; n < 16; ++n) printf("%.2x ", tmp.i8[n]);
  37. } else if (size == 16) {
  38. for (n = 0; n < 8; ++n) printf("%.4x ", tmp.i16[n]);
  39. } else if (size == 32) {
  40. for (n = 0; n < 4; ++n) printf("%.8x ", tmp.i32[n]);
  41. } else {
  42. for (n = 0; n < 2; ++n) printf("%.16lx ", tmp.i64[n]);
  43. }
  44. printf("\n");
  45. }
  46. #endif
  47. //------------------------------------------------------------------------------
  48. // Compute susceptibility based on DCT-coeff histograms:
  49. // the higher, the "easier" the macroblock is to compress.
  50. static void CollectHistogram(const uint8_t* ref, const uint8_t* pred,
  51. int start_block, int end_block,
  52. VP8Histogram* const histo) {
  53. const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
  54. int j;
  55. for (j = start_block; j < end_block; ++j) {
  56. int16_t out[16];
  57. int k;
  58. VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
  59. // Convert coefficients to bin (within out[]).
  60. {
  61. // Load.
  62. const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
  63. const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
  64. // sign(out) = out >> 15 (0x0000 if positive, 0xffff if negative)
  65. const __m128i sign0 = _mm_srai_epi16(out0, 15);
  66. const __m128i sign1 = _mm_srai_epi16(out1, 15);
  67. // abs(out) = (out ^ sign) - sign
  68. const __m128i xor0 = _mm_xor_si128(out0, sign0);
  69. const __m128i xor1 = _mm_xor_si128(out1, sign1);
  70. const __m128i abs0 = _mm_sub_epi16(xor0, sign0);
  71. const __m128i abs1 = _mm_sub_epi16(xor1, sign1);
  72. // v = abs(out) >> 3
  73. const __m128i v0 = _mm_srai_epi16(abs0, 3);
  74. const __m128i v1 = _mm_srai_epi16(abs1, 3);
  75. // bin = min(v, MAX_COEFF_THRESH)
  76. const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
  77. const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
  78. // Store.
  79. _mm_storeu_si128((__m128i*)&out[0], bin0);
  80. _mm_storeu_si128((__m128i*)&out[8], bin1);
  81. }
  82. // Convert coefficients to bin.
  83. for (k = 0; k < 16; ++k) {
  84. histo->distribution[out[k]]++;
  85. }
  86. }
  87. }
  88. //------------------------------------------------------------------------------
  89. // Transforms (Paragraph 14.4)
  90. // Does one or two inverse transforms.
  91. static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst,
  92. int do_two) {
  93. // This implementation makes use of 16-bit fixed point versions of two
  94. // multiply constants:
  95. // K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
  96. // K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
  97. //
  98. // To be able to use signed 16-bit integers, we use the following trick to
  99. // have constants within range:
  100. // - Associated constants are obtained by subtracting the 16-bit fixed point
  101. // version of one:
  102. // k = K - (1 << 16) => K = k + (1 << 16)
  103. // K1 = 85267 => k1 = 20091
  104. // K2 = 35468 => k2 = -30068
  105. // - The multiplication of a variable by a constant become the sum of the
  106. // variable and the multiplication of that variable by the associated
  107. // constant:
  108. // (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
  109. const __m128i k1 = _mm_set1_epi16(20091);
  110. const __m128i k2 = _mm_set1_epi16(-30068);
  111. __m128i T0, T1, T2, T3;
  112. // Load and concatenate the transform coefficients (we'll do two inverse
  113. // transforms in parallel). In the case of only one inverse transform, the
  114. // second half of the vectors will just contain random value we'll never
  115. // use nor store.
  116. __m128i in0, in1, in2, in3;
  117. {
  118. in0 = _mm_loadl_epi64((__m128i*)&in[0]);
  119. in1 = _mm_loadl_epi64((__m128i*)&in[4]);
  120. in2 = _mm_loadl_epi64((__m128i*)&in[8]);
  121. in3 = _mm_loadl_epi64((__m128i*)&in[12]);
  122. // a00 a10 a20 a30 x x x x
  123. // a01 a11 a21 a31 x x x x
  124. // a02 a12 a22 a32 x x x x
  125. // a03 a13 a23 a33 x x x x
  126. if (do_two) {
  127. const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]);
  128. const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]);
  129. const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]);
  130. const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]);
  131. in0 = _mm_unpacklo_epi64(in0, inB0);
  132. in1 = _mm_unpacklo_epi64(in1, inB1);
  133. in2 = _mm_unpacklo_epi64(in2, inB2);
  134. in3 = _mm_unpacklo_epi64(in3, inB3);
  135. // a00 a10 a20 a30 b00 b10 b20 b30
  136. // a01 a11 a21 a31 b01 b11 b21 b31
  137. // a02 a12 a22 a32 b02 b12 b22 b32
  138. // a03 a13 a23 a33 b03 b13 b23 b33
  139. }
  140. }
  141. // Vertical pass and subsequent transpose.
  142. {
  143. // First pass, c and d calculations are longer because of the "trick"
  144. // multiplications.
  145. const __m128i a = _mm_add_epi16(in0, in2);
  146. const __m128i b = _mm_sub_epi16(in0, in2);
  147. // c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
  148. const __m128i c1 = _mm_mulhi_epi16(in1, k2);
  149. const __m128i c2 = _mm_mulhi_epi16(in3, k1);
  150. const __m128i c3 = _mm_sub_epi16(in1, in3);
  151. const __m128i c4 = _mm_sub_epi16(c1, c2);
  152. const __m128i c = _mm_add_epi16(c3, c4);
  153. // d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
  154. const __m128i d1 = _mm_mulhi_epi16(in1, k1);
  155. const __m128i d2 = _mm_mulhi_epi16(in3, k2);
  156. const __m128i d3 = _mm_add_epi16(in1, in3);
  157. const __m128i d4 = _mm_add_epi16(d1, d2);
  158. const __m128i d = _mm_add_epi16(d3, d4);
  159. // Second pass.
  160. const __m128i tmp0 = _mm_add_epi16(a, d);
  161. const __m128i tmp1 = _mm_add_epi16(b, c);
  162. const __m128i tmp2 = _mm_sub_epi16(b, c);
  163. const __m128i tmp3 = _mm_sub_epi16(a, d);
  164. // Transpose the two 4x4.
  165. // a00 a01 a02 a03 b00 b01 b02 b03
  166. // a10 a11 a12 a13 b10 b11 b12 b13
  167. // a20 a21 a22 a23 b20 b21 b22 b23
  168. // a30 a31 a32 a33 b30 b31 b32 b33
  169. const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1);
  170. const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3);
  171. const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1);
  172. const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3);
  173. // a00 a10 a01 a11 a02 a12 a03 a13
  174. // a20 a30 a21 a31 a22 a32 a23 a33
  175. // b00 b10 b01 b11 b02 b12 b03 b13
  176. // b20 b30 b21 b31 b22 b32 b23 b33
  177. const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
  178. const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
  179. const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
  180. const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
  181. // a00 a10 a20 a30 a01 a11 a21 a31
  182. // b00 b10 b20 b30 b01 b11 b21 b31
  183. // a02 a12 a22 a32 a03 a13 a23 a33
  184. // b02 b12 a22 b32 b03 b13 b23 b33
  185. T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
  186. T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
  187. T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
  188. T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
  189. // a00 a10 a20 a30 b00 b10 b20 b30
  190. // a01 a11 a21 a31 b01 b11 b21 b31
  191. // a02 a12 a22 a32 b02 b12 b22 b32
  192. // a03 a13 a23 a33 b03 b13 b23 b33
  193. }
  194. // Horizontal pass and subsequent transpose.
  195. {
  196. // First pass, c and d calculations are longer because of the "trick"
  197. // multiplications.
  198. const __m128i four = _mm_set1_epi16(4);
  199. const __m128i dc = _mm_add_epi16(T0, four);
  200. const __m128i a = _mm_add_epi16(dc, T2);
  201. const __m128i b = _mm_sub_epi16(dc, T2);
  202. // c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
  203. const __m128i c1 = _mm_mulhi_epi16(T1, k2);
  204. const __m128i c2 = _mm_mulhi_epi16(T3, k1);
  205. const __m128i c3 = _mm_sub_epi16(T1, T3);
  206. const __m128i c4 = _mm_sub_epi16(c1, c2);
  207. const __m128i c = _mm_add_epi16(c3, c4);
  208. // d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
  209. const __m128i d1 = _mm_mulhi_epi16(T1, k1);
  210. const __m128i d2 = _mm_mulhi_epi16(T3, k2);
  211. const __m128i d3 = _mm_add_epi16(T1, T3);
  212. const __m128i d4 = _mm_add_epi16(d1, d2);
  213. const __m128i d = _mm_add_epi16(d3, d4);
  214. // Second pass.
  215. const __m128i tmp0 = _mm_add_epi16(a, d);
  216. const __m128i tmp1 = _mm_add_epi16(b, c);
  217. const __m128i tmp2 = _mm_sub_epi16(b, c);
  218. const __m128i tmp3 = _mm_sub_epi16(a, d);
  219. const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
  220. const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
  221. const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
  222. const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
  223. // Transpose the two 4x4.
  224. // a00 a01 a02 a03 b00 b01 b02 b03
  225. // a10 a11 a12 a13 b10 b11 b12 b13
  226. // a20 a21 a22 a23 b20 b21 b22 b23
  227. // a30 a31 a32 a33 b30 b31 b32 b33
  228. const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1);
  229. const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3);
  230. const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1);
  231. const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3);
  232. // a00 a10 a01 a11 a02 a12 a03 a13
  233. // a20 a30 a21 a31 a22 a32 a23 a33
  234. // b00 b10 b01 b11 b02 b12 b03 b13
  235. // b20 b30 b21 b31 b22 b32 b23 b33
  236. const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
  237. const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
  238. const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
  239. const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
  240. // a00 a10 a20 a30 a01 a11 a21 a31
  241. // b00 b10 b20 b30 b01 b11 b21 b31
  242. // a02 a12 a22 a32 a03 a13 a23 a33
  243. // b02 b12 a22 b32 b03 b13 b23 b33
  244. T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
  245. T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
  246. T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
  247. T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
  248. // a00 a10 a20 a30 b00 b10 b20 b30
  249. // a01 a11 a21 a31 b01 b11 b21 b31
  250. // a02 a12 a22 a32 b02 b12 b22 b32
  251. // a03 a13 a23 a33 b03 b13 b23 b33
  252. }
  253. // Add inverse transform to 'ref' and store.
  254. {
  255. const __m128i zero = _mm_setzero_si128();
  256. // Load the reference(s).
  257. __m128i ref0, ref1, ref2, ref3;
  258. if (do_two) {
  259. // Load eight bytes/pixels per line.
  260. ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
  261. ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
  262. ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
  263. ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
  264. } else {
  265. // Load four bytes/pixels per line.
  266. ref0 = _mm_cvtsi32_si128(*(int*)&ref[0 * BPS]);
  267. ref1 = _mm_cvtsi32_si128(*(int*)&ref[1 * BPS]);
  268. ref2 = _mm_cvtsi32_si128(*(int*)&ref[2 * BPS]);
  269. ref3 = _mm_cvtsi32_si128(*(int*)&ref[3 * BPS]);
  270. }
  271. // Convert to 16b.
  272. ref0 = _mm_unpacklo_epi8(ref0, zero);
  273. ref1 = _mm_unpacklo_epi8(ref1, zero);
  274. ref2 = _mm_unpacklo_epi8(ref2, zero);
  275. ref3 = _mm_unpacklo_epi8(ref3, zero);
  276. // Add the inverse transform(s).
  277. ref0 = _mm_add_epi16(ref0, T0);
  278. ref1 = _mm_add_epi16(ref1, T1);
  279. ref2 = _mm_add_epi16(ref2, T2);
  280. ref3 = _mm_add_epi16(ref3, T3);
  281. // Unsigned saturate to 8b.
  282. ref0 = _mm_packus_epi16(ref0, ref0);
  283. ref1 = _mm_packus_epi16(ref1, ref1);
  284. ref2 = _mm_packus_epi16(ref2, ref2);
  285. ref3 = _mm_packus_epi16(ref3, ref3);
  286. // Store the results.
  287. if (do_two) {
  288. // Store eight bytes/pixels per line.
  289. _mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);
  290. _mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);
  291. _mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);
  292. _mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);
  293. } else {
  294. // Store four bytes/pixels per line.
  295. *((int32_t *)&dst[0 * BPS]) = _mm_cvtsi128_si32(ref0);
  296. *((int32_t *)&dst[1 * BPS]) = _mm_cvtsi128_si32(ref1);
  297. *((int32_t *)&dst[2 * BPS]) = _mm_cvtsi128_si32(ref2);
  298. *((int32_t *)&dst[3 * BPS]) = _mm_cvtsi128_si32(ref3);
  299. }
  300. }
  301. }
  302. static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) {
  303. const __m128i zero = _mm_setzero_si128();
  304. const __m128i seven = _mm_set1_epi16(7);
  305. const __m128i k937 = _mm_set1_epi32(937);
  306. const __m128i k1812 = _mm_set1_epi32(1812);
  307. const __m128i k51000 = _mm_set1_epi32(51000);
  308. const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
  309. const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,
  310. 5352, 2217, 5352, 2217);
  311. const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
  312. 2217, -5352, 2217, -5352);
  313. const __m128i k88p = _mm_set_epi16(8, 8, 8, 8, 8, 8, 8, 8);
  314. const __m128i k88m = _mm_set_epi16(-8, 8, -8, 8, -8, 8, -8, 8);
  315. const __m128i k5352_2217p = _mm_set_epi16(2217, 5352, 2217, 5352,
  316. 2217, 5352, 2217, 5352);
  317. const __m128i k5352_2217m = _mm_set_epi16(-5352, 2217, -5352, 2217,
  318. -5352, 2217, -5352, 2217);
  319. __m128i v01, v32;
  320. // Difference between src and ref and initial transpose.
  321. {
  322. // Load src and convert to 16b.
  323. const __m128i src0 = _mm_loadl_epi64((__m128i*)&src[0 * BPS]);
  324. const __m128i src1 = _mm_loadl_epi64((__m128i*)&src[1 * BPS]);
  325. const __m128i src2 = _mm_loadl_epi64((__m128i*)&src[2 * BPS]);
  326. const __m128i src3 = _mm_loadl_epi64((__m128i*)&src[3 * BPS]);
  327. const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
  328. const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
  329. const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
  330. const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
  331. // Load ref and convert to 16b.
  332. const __m128i ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
  333. const __m128i ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
  334. const __m128i ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
  335. const __m128i ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
  336. const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
  337. const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
  338. const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
  339. const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
  340. // Compute difference. -> 00 01 02 03 00 00 00 00
  341. const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
  342. const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
  343. const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
  344. const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
  345. // Unpack and shuffle
  346. // 00 01 02 03 0 0 0 0
  347. // 10 11 12 13 0 0 0 0
  348. // 20 21 22 23 0 0 0 0
  349. // 30 31 32 33 0 0 0 0
  350. const __m128i shuf01 = _mm_unpacklo_epi32(diff0, diff1);
  351. const __m128i shuf23 = _mm_unpacklo_epi32(diff2, diff3);
  352. // 00 01 10 11 02 03 12 13
  353. // 20 21 30 31 22 23 32 33
  354. const __m128i shuf01_p =
  355. _mm_shufflehi_epi16(shuf01, _MM_SHUFFLE(2, 3, 0, 1));
  356. const __m128i shuf23_p =
  357. _mm_shufflehi_epi16(shuf23, _MM_SHUFFLE(2, 3, 0, 1));
  358. // 00 01 10 11 03 02 13 12
  359. // 20 21 30 31 23 22 33 32
  360. const __m128i s01 = _mm_unpacklo_epi64(shuf01_p, shuf23_p);
  361. const __m128i s32 = _mm_unpackhi_epi64(shuf01_p, shuf23_p);
  362. // 00 01 10 11 20 21 30 31
  363. // 03 02 13 12 23 22 33 32
  364. const __m128i a01 = _mm_add_epi16(s01, s32);
  365. const __m128i a32 = _mm_sub_epi16(s01, s32);
  366. // [d0 + d3 | d1 + d2 | ...] = [a0 a1 | a0' a1' | ... ]
  367. // [d0 - d3 | d1 - d2 | ...] = [a3 a2 | a3' a2' | ... ]
  368. const __m128i tmp0 = _mm_madd_epi16(a01, k88p); // [ (a0 + a1) << 3, ... ]
  369. const __m128i tmp2 = _mm_madd_epi16(a01, k88m); // [ (a0 - a1) << 3, ... ]
  370. const __m128i tmp1_1 = _mm_madd_epi16(a32, k5352_2217p);
  371. const __m128i tmp3_1 = _mm_madd_epi16(a32, k5352_2217m);
  372. const __m128i tmp1_2 = _mm_add_epi32(tmp1_1, k1812);
  373. const __m128i tmp3_2 = _mm_add_epi32(tmp3_1, k937);
  374. const __m128i tmp1 = _mm_srai_epi32(tmp1_2, 9);
  375. const __m128i tmp3 = _mm_srai_epi32(tmp3_2, 9);
  376. const __m128i s03 = _mm_packs_epi32(tmp0, tmp2);
  377. const __m128i s12 = _mm_packs_epi32(tmp1, tmp3);
  378. const __m128i s_lo = _mm_unpacklo_epi16(s03, s12); // 0 1 0 1 0 1...
  379. const __m128i s_hi = _mm_unpackhi_epi16(s03, s12); // 2 3 2 3 2 3
  380. const __m128i v23 = _mm_unpackhi_epi32(s_lo, s_hi);
  381. v01 = _mm_unpacklo_epi32(s_lo, s_hi);
  382. v32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2)); // 3 2 3 2 3 2..
  383. }
  384. // Second pass
  385. {
  386. // Same operations are done on the (0,3) and (1,2) pairs.
  387. // a0 = v0 + v3
  388. // a1 = v1 + v2
  389. // a3 = v0 - v3
  390. // a2 = v1 - v2
  391. const __m128i a01 = _mm_add_epi16(v01, v32);
  392. const __m128i a32 = _mm_sub_epi16(v01, v32);
  393. const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
  394. const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
  395. const __m128i a01_plus_7 = _mm_add_epi16(a01, seven);
  396. // d0 = (a0 + a1 + 7) >> 4;
  397. // d2 = (a0 - a1 + 7) >> 4;
  398. const __m128i c0 = _mm_add_epi16(a01_plus_7, a11);
  399. const __m128i c2 = _mm_sub_epi16(a01_plus_7, a11);
  400. const __m128i d0 = _mm_srai_epi16(c0, 4);
  401. const __m128i d2 = _mm_srai_epi16(c2, 4);
  402. // f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
  403. // f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
  404. const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
  405. const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
  406. const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
  407. const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
  408. const __m128i d3 = _mm_add_epi32(c3, k51000);
  409. const __m128i e1 = _mm_srai_epi32(d1, 16);
  410. const __m128i e3 = _mm_srai_epi32(d3, 16);
  411. const __m128i f1 = _mm_packs_epi32(e1, e1);
  412. const __m128i f3 = _mm_packs_epi32(e3, e3);
  413. // f1 = f1 + (a3 != 0);
  414. // The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
  415. // desired (0, 1), we add one earlier through k12000_plus_one.
  416. // -> f1 = f1 + 1 - (a3 == 0)
  417. const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
  418. const __m128i d0_g1 = _mm_unpacklo_epi64(d0, g1);
  419. const __m128i d2_f3 = _mm_unpacklo_epi64(d2, f3);
  420. _mm_storeu_si128((__m128i*)&out[0], d0_g1);
  421. _mm_storeu_si128((__m128i*)&out[8], d2_f3);
  422. }
  423. }
  424. static void FTransformWHT(const int16_t* in, int16_t* out) {
  425. int32_t tmp[16];
  426. int i;
  427. for (i = 0; i < 4; ++i, in += 64) {
  428. const int a0 = (in[0 * 16] + in[2 * 16]);
  429. const int a1 = (in[1 * 16] + in[3 * 16]);
  430. const int a2 = (in[1 * 16] - in[3 * 16]);
  431. const int a3 = (in[0 * 16] - in[2 * 16]);
  432. tmp[0 + i * 4] = a0 + a1;
  433. tmp[1 + i * 4] = a3 + a2;
  434. tmp[2 + i * 4] = a3 - a2;
  435. tmp[3 + i * 4] = a0 - a1;
  436. }
  437. {
  438. const __m128i src0 = _mm_loadu_si128((__m128i*)&tmp[0]);
  439. const __m128i src1 = _mm_loadu_si128((__m128i*)&tmp[4]);
  440. const __m128i src2 = _mm_loadu_si128((__m128i*)&tmp[8]);
  441. const __m128i src3 = _mm_loadu_si128((__m128i*)&tmp[12]);
  442. const __m128i a0 = _mm_add_epi32(src0, src2);
  443. const __m128i a1 = _mm_add_epi32(src1, src3);
  444. const __m128i a2 = _mm_sub_epi32(src1, src3);
  445. const __m128i a3 = _mm_sub_epi32(src0, src2);
  446. const __m128i b0 = _mm_srai_epi32(_mm_add_epi32(a0, a1), 1);
  447. const __m128i b1 = _mm_srai_epi32(_mm_add_epi32(a3, a2), 1);
  448. const __m128i b2 = _mm_srai_epi32(_mm_sub_epi32(a3, a2), 1);
  449. const __m128i b3 = _mm_srai_epi32(_mm_sub_epi32(a0, a1), 1);
  450. const __m128i out0 = _mm_packs_epi32(b0, b1);
  451. const __m128i out1 = _mm_packs_epi32(b2, b3);
  452. _mm_storeu_si128((__m128i*)&out[0], out0);
  453. _mm_storeu_si128((__m128i*)&out[8], out1);
  454. }
  455. }
  456. //------------------------------------------------------------------------------
  457. // Metric
  458. static int SSE_Nx4(const uint8_t* a, const uint8_t* b,
  459. int num_quads, int do_16) {
  460. const __m128i zero = _mm_setzero_si128();
  461. __m128i sum1 = zero;
  462. __m128i sum2 = zero;
  463. while (num_quads-- > 0) {
  464. // Note: for the !do_16 case, we read 16 pixels instead of 8 but that's ok,
  465. // thanks to buffer over-allocation to that effect.
  466. const __m128i a0 = _mm_loadu_si128((__m128i*)&a[BPS * 0]);
  467. const __m128i a1 = _mm_loadu_si128((__m128i*)&a[BPS * 1]);
  468. const __m128i a2 = _mm_loadu_si128((__m128i*)&a[BPS * 2]);
  469. const __m128i a3 = _mm_loadu_si128((__m128i*)&a[BPS * 3]);
  470. const __m128i b0 = _mm_loadu_si128((__m128i*)&b[BPS * 0]);
  471. const __m128i b1 = _mm_loadu_si128((__m128i*)&b[BPS * 1]);
  472. const __m128i b2 = _mm_loadu_si128((__m128i*)&b[BPS * 2]);
  473. const __m128i b3 = _mm_loadu_si128((__m128i*)&b[BPS * 3]);
  474. // compute clip0(a-b) and clip0(b-a)
  475. const __m128i a0p = _mm_subs_epu8(a0, b0);
  476. const __m128i a0m = _mm_subs_epu8(b0, a0);
  477. const __m128i a1p = _mm_subs_epu8(a1, b1);
  478. const __m128i a1m = _mm_subs_epu8(b1, a1);
  479. const __m128i a2p = _mm_subs_epu8(a2, b2);
  480. const __m128i a2m = _mm_subs_epu8(b2, a2);
  481. const __m128i a3p = _mm_subs_epu8(a3, b3);
  482. const __m128i a3m = _mm_subs_epu8(b3, a3);
  483. // compute |a-b| with 8b arithmetic as clip0(a-b) | clip0(b-a)
  484. const __m128i diff0 = _mm_or_si128(a0p, a0m);
  485. const __m128i diff1 = _mm_or_si128(a1p, a1m);
  486. const __m128i diff2 = _mm_or_si128(a2p, a2m);
  487. const __m128i diff3 = _mm_or_si128(a3p, a3m);
  488. // unpack (only four operations, instead of eight)
  489. const __m128i low0 = _mm_unpacklo_epi8(diff0, zero);
  490. const __m128i low1 = _mm_unpacklo_epi8(diff1, zero);
  491. const __m128i low2 = _mm_unpacklo_epi8(diff2, zero);
  492. const __m128i low3 = _mm_unpacklo_epi8(diff3, zero);
  493. // multiply with self
  494. const __m128i low_madd0 = _mm_madd_epi16(low0, low0);
  495. const __m128i low_madd1 = _mm_madd_epi16(low1, low1);
  496. const __m128i low_madd2 = _mm_madd_epi16(low2, low2);
  497. const __m128i low_madd3 = _mm_madd_epi16(low3, low3);
  498. // collect in a cascading way
  499. const __m128i low_sum0 = _mm_add_epi32(low_madd0, low_madd1);
  500. const __m128i low_sum1 = _mm_add_epi32(low_madd2, low_madd3);
  501. sum1 = _mm_add_epi32(sum1, low_sum0);
  502. sum2 = _mm_add_epi32(sum2, low_sum1);
  503. if (do_16) { // if necessary, process the higher 8 bytes similarly
  504. const __m128i hi0 = _mm_unpackhi_epi8(diff0, zero);
  505. const __m128i hi1 = _mm_unpackhi_epi8(diff1, zero);
  506. const __m128i hi2 = _mm_unpackhi_epi8(diff2, zero);
  507. const __m128i hi3 = _mm_unpackhi_epi8(diff3, zero);
  508. const __m128i hi_madd0 = _mm_madd_epi16(hi0, hi0);
  509. const __m128i hi_madd1 = _mm_madd_epi16(hi1, hi1);
  510. const __m128i hi_madd2 = _mm_madd_epi16(hi2, hi2);
  511. const __m128i hi_madd3 = _mm_madd_epi16(hi3, hi3);
  512. const __m128i hi_sum0 = _mm_add_epi32(hi_madd0, hi_madd1);
  513. const __m128i hi_sum1 = _mm_add_epi32(hi_madd2, hi_madd3);
  514. sum1 = _mm_add_epi32(sum1, hi_sum0);
  515. sum2 = _mm_add_epi32(sum2, hi_sum1);
  516. }
  517. a += 4 * BPS;
  518. b += 4 * BPS;
  519. }
  520. {
  521. int32_t tmp[4];
  522. const __m128i sum = _mm_add_epi32(sum1, sum2);
  523. _mm_storeu_si128((__m128i*)tmp, sum);
  524. return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
  525. }
  526. }
  527. static int SSE16x16(const uint8_t* a, const uint8_t* b) {
  528. return SSE_Nx4(a, b, 4, 1);
  529. }
  530. static int SSE16x8(const uint8_t* a, const uint8_t* b) {
  531. return SSE_Nx4(a, b, 2, 1);
  532. }
  533. static int SSE8x8(const uint8_t* a, const uint8_t* b) {
  534. return SSE_Nx4(a, b, 2, 0);
  535. }
  536. static int SSE4x4(const uint8_t* a, const uint8_t* b) {
  537. const __m128i zero = _mm_setzero_si128();
  538. // Load values. Note that we read 8 pixels instead of 4,
  539. // but the a/b buffers are over-allocated to that effect.
  540. const __m128i a0 = _mm_loadl_epi64((__m128i*)&a[BPS * 0]);
  541. const __m128i a1 = _mm_loadl_epi64((__m128i*)&a[BPS * 1]);
  542. const __m128i a2 = _mm_loadl_epi64((__m128i*)&a[BPS * 2]);
  543. const __m128i a3 = _mm_loadl_epi64((__m128i*)&a[BPS * 3]);
  544. const __m128i b0 = _mm_loadl_epi64((__m128i*)&b[BPS * 0]);
  545. const __m128i b1 = _mm_loadl_epi64((__m128i*)&b[BPS * 1]);
  546. const __m128i b2 = _mm_loadl_epi64((__m128i*)&b[BPS * 2]);
  547. const __m128i b3 = _mm_loadl_epi64((__m128i*)&b[BPS * 3]);
  548. // Combine pair of lines and convert to 16b.
  549. const __m128i a01 = _mm_unpacklo_epi32(a0, a1);
  550. const __m128i a23 = _mm_unpacklo_epi32(a2, a3);
  551. const __m128i b01 = _mm_unpacklo_epi32(b0, b1);
  552. const __m128i b23 = _mm_unpacklo_epi32(b2, b3);
  553. const __m128i a01s = _mm_unpacklo_epi8(a01, zero);
  554. const __m128i a23s = _mm_unpacklo_epi8(a23, zero);
  555. const __m128i b01s = _mm_unpacklo_epi8(b01, zero);
  556. const __m128i b23s = _mm_unpacklo_epi8(b23, zero);
  557. // Compute differences; (a-b)^2 = (abs(a-b))^2 = (sat8(a-b) + sat8(b-a))^2
  558. // TODO(cduvivier): Dissassemble and figure out why this is fastest. We don't
  559. // need absolute values, there is no need to do calculation
  560. // in 8bit as we are already in 16bit, ... Yet this is what
  561. // benchmarks the fastest!
  562. const __m128i d0 = _mm_subs_epu8(a01s, b01s);
  563. const __m128i d1 = _mm_subs_epu8(b01s, a01s);
  564. const __m128i d2 = _mm_subs_epu8(a23s, b23s);
  565. const __m128i d3 = _mm_subs_epu8(b23s, a23s);
  566. // Square and add them all together.
  567. const __m128i madd0 = _mm_madd_epi16(d0, d0);
  568. const __m128i madd1 = _mm_madd_epi16(d1, d1);
  569. const __m128i madd2 = _mm_madd_epi16(d2, d2);
  570. const __m128i madd3 = _mm_madd_epi16(d3, d3);
  571. const __m128i sum0 = _mm_add_epi32(madd0, madd1);
  572. const __m128i sum1 = _mm_add_epi32(madd2, madd3);
  573. const __m128i sum2 = _mm_add_epi32(sum0, sum1);
  574. int32_t tmp[4];
  575. _mm_storeu_si128((__m128i*)tmp, sum2);
  576. return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
  577. }
  578. //------------------------------------------------------------------------------
  579. // Texture distortion
  580. //
  581. // We try to match the spectral content (weighted) between source and
  582. // reconstructed samples.
  583. // Hadamard transform
  584. // Returns the difference between the weighted sum of the absolute value of
  585. // transformed coefficients.
  586. static int TTransform(const uint8_t* inA, const uint8_t* inB,
  587. const uint16_t* const w) {
  588. int32_t sum[4];
  589. __m128i tmp_0, tmp_1, tmp_2, tmp_3;
  590. const __m128i zero = _mm_setzero_si128();
  591. // Load, combine and transpose inputs.
  592. {
  593. const __m128i inA_0 = _mm_loadl_epi64((__m128i*)&inA[BPS * 0]);
  594. const __m128i inA_1 = _mm_loadl_epi64((__m128i*)&inA[BPS * 1]);
  595. const __m128i inA_2 = _mm_loadl_epi64((__m128i*)&inA[BPS * 2]);
  596. const __m128i inA_3 = _mm_loadl_epi64((__m128i*)&inA[BPS * 3]);
  597. const __m128i inB_0 = _mm_loadl_epi64((__m128i*)&inB[BPS * 0]);
  598. const __m128i inB_1 = _mm_loadl_epi64((__m128i*)&inB[BPS * 1]);
  599. const __m128i inB_2 = _mm_loadl_epi64((__m128i*)&inB[BPS * 2]);
  600. const __m128i inB_3 = _mm_loadl_epi64((__m128i*)&inB[BPS * 3]);
  601. // Combine inA and inB (we'll do two transforms in parallel).
  602. const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0);
  603. const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1);
  604. const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2);
  605. const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3);
  606. // a00 b00 a01 b01 a02 b03 a03 b03 0 0 0 0 0 0 0 0
  607. // a10 b10 a11 b11 a12 b12 a13 b13 0 0 0 0 0 0 0 0
  608. // a20 b20 a21 b21 a22 b22 a23 b23 0 0 0 0 0 0 0 0
  609. // a30 b30 a31 b31 a32 b32 a33 b33 0 0 0 0 0 0 0 0
  610. // Transpose the two 4x4, discarding the filling zeroes.
  611. const __m128i transpose0_0 = _mm_unpacklo_epi8(inAB_0, inAB_2);
  612. const __m128i transpose0_1 = _mm_unpacklo_epi8(inAB_1, inAB_3);
  613. // a00 a20 b00 b20 a01 a21 b01 b21 a02 a22 b02 b22 a03 a23 b03 b23
  614. // a10 a30 b10 b30 a11 a31 b11 b31 a12 a32 b12 b32 a13 a33 b13 b33
  615. const __m128i transpose1_0 = _mm_unpacklo_epi8(transpose0_0, transpose0_1);
  616. const __m128i transpose1_1 = _mm_unpackhi_epi8(transpose0_0, transpose0_1);
  617. // a00 a10 a20 a30 b00 b10 b20 b30 a01 a11 a21 a31 b01 b11 b21 b31
  618. // a02 a12 a22 a32 b02 b12 b22 b32 a03 a13 a23 a33 b03 b13 b23 b33
  619. // Convert to 16b.
  620. tmp_0 = _mm_unpacklo_epi8(transpose1_0, zero);
  621. tmp_1 = _mm_unpackhi_epi8(transpose1_0, zero);
  622. tmp_2 = _mm_unpacklo_epi8(transpose1_1, zero);
  623. tmp_3 = _mm_unpackhi_epi8(transpose1_1, zero);
  624. // a00 a10 a20 a30 b00 b10 b20 b30
  625. // a01 a11 a21 a31 b01 b11 b21 b31
  626. // a02 a12 a22 a32 b02 b12 b22 b32
  627. // a03 a13 a23 a33 b03 b13 b23 b33
  628. }
  629. // Horizontal pass and subsequent transpose.
  630. {
  631. // Calculate a and b (two 4x4 at once).
  632. const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
  633. const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
  634. const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
  635. const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
  636. const __m128i b0 = _mm_add_epi16(a0, a1);
  637. const __m128i b1 = _mm_add_epi16(a3, a2);
  638. const __m128i b2 = _mm_sub_epi16(a3, a2);
  639. const __m128i b3 = _mm_sub_epi16(a0, a1);
  640. // a00 a01 a02 a03 b00 b01 b02 b03
  641. // a10 a11 a12 a13 b10 b11 b12 b13
  642. // a20 a21 a22 a23 b20 b21 b22 b23
  643. // a30 a31 a32 a33 b30 b31 b32 b33
  644. // Transpose the two 4x4.
  645. const __m128i transpose0_0 = _mm_unpacklo_epi16(b0, b1);
  646. const __m128i transpose0_1 = _mm_unpacklo_epi16(b2, b3);
  647. const __m128i transpose0_2 = _mm_unpackhi_epi16(b0, b1);
  648. const __m128i transpose0_3 = _mm_unpackhi_epi16(b2, b3);
  649. // a00 a10 a01 a11 a02 a12 a03 a13
  650. // a20 a30 a21 a31 a22 a32 a23 a33
  651. // b00 b10 b01 b11 b02 b12 b03 b13
  652. // b20 b30 b21 b31 b22 b32 b23 b33
  653. const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
  654. const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
  655. const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
  656. const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
  657. // a00 a10 a20 a30 a01 a11 a21 a31
  658. // b00 b10 b20 b30 b01 b11 b21 b31
  659. // a02 a12 a22 a32 a03 a13 a23 a33
  660. // b02 b12 a22 b32 b03 b13 b23 b33
  661. tmp_0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
  662. tmp_1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
  663. tmp_2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
  664. tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
  665. // a00 a10 a20 a30 b00 b10 b20 b30
  666. // a01 a11 a21 a31 b01 b11 b21 b31
  667. // a02 a12 a22 a32 b02 b12 b22 b32
  668. // a03 a13 a23 a33 b03 b13 b23 b33
  669. }
  670. // Vertical pass and difference of weighted sums.
  671. {
  672. // Load all inputs.
  673. // TODO(cduvivier): Make variable declarations and allocations aligned so
  674. // we can use _mm_load_si128 instead of _mm_loadu_si128.
  675. const __m128i w_0 = _mm_loadu_si128((__m128i*)&w[0]);
  676. const __m128i w_8 = _mm_loadu_si128((__m128i*)&w[8]);
  677. // Calculate a and b (two 4x4 at once).
  678. const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
  679. const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
  680. const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
  681. const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
  682. const __m128i b0 = _mm_add_epi16(a0, a1);
  683. const __m128i b1 = _mm_add_epi16(a3, a2);
  684. const __m128i b2 = _mm_sub_epi16(a3, a2);
  685. const __m128i b3 = _mm_sub_epi16(a0, a1);
  686. // Separate the transforms of inA and inB.
  687. __m128i A_b0 = _mm_unpacklo_epi64(b0, b1);
  688. __m128i A_b2 = _mm_unpacklo_epi64(b2, b3);
  689. __m128i B_b0 = _mm_unpackhi_epi64(b0, b1);
  690. __m128i B_b2 = _mm_unpackhi_epi64(b2, b3);
  691. {
  692. // sign(b) = b >> 15 (0x0000 if positive, 0xffff if negative)
  693. const __m128i sign_A_b0 = _mm_srai_epi16(A_b0, 15);
  694. const __m128i sign_A_b2 = _mm_srai_epi16(A_b2, 15);
  695. const __m128i sign_B_b0 = _mm_srai_epi16(B_b0, 15);
  696. const __m128i sign_B_b2 = _mm_srai_epi16(B_b2, 15);
  697. // b = abs(b) = (b ^ sign) - sign
  698. A_b0 = _mm_xor_si128(A_b0, sign_A_b0);
  699. A_b2 = _mm_xor_si128(A_b2, sign_A_b2);
  700. B_b0 = _mm_xor_si128(B_b0, sign_B_b0);
  701. B_b2 = _mm_xor_si128(B_b2, sign_B_b2);
  702. A_b0 = _mm_sub_epi16(A_b0, sign_A_b0);
  703. A_b2 = _mm_sub_epi16(A_b2, sign_A_b2);
  704. B_b0 = _mm_sub_epi16(B_b0, sign_B_b0);
  705. B_b2 = _mm_sub_epi16(B_b2, sign_B_b2);
  706. }
  707. // weighted sums
  708. A_b0 = _mm_madd_epi16(A_b0, w_0);
  709. A_b2 = _mm_madd_epi16(A_b2, w_8);
  710. B_b0 = _mm_madd_epi16(B_b0, w_0);
  711. B_b2 = _mm_madd_epi16(B_b2, w_8);
  712. A_b0 = _mm_add_epi32(A_b0, A_b2);
  713. B_b0 = _mm_add_epi32(B_b0, B_b2);
  714. // difference of weighted sums
  715. A_b0 = _mm_sub_epi32(A_b0, B_b0);
  716. _mm_storeu_si128((__m128i*)&sum[0], A_b0);
  717. }
  718. return sum[0] + sum[1] + sum[2] + sum[3];
  719. }
  720. static int Disto4x4(const uint8_t* const a, const uint8_t* const b,
  721. const uint16_t* const w) {
  722. const int diff_sum = TTransform(a, b, w);
  723. return abs(diff_sum) >> 5;
  724. }
  725. static int Disto16x16(const uint8_t* const a, const uint8_t* const b,
  726. const uint16_t* const w) {
  727. int D = 0;
  728. int x, y;
  729. for (y = 0; y < 16 * BPS; y += 4 * BPS) {
  730. for (x = 0; x < 16; x += 4) {
  731. D += Disto4x4(a + x + y, b + x + y, w);
  732. }
  733. }
  734. return D;
  735. }
  736. //------------------------------------------------------------------------------
  737. // Quantization
  738. //
  739. static WEBP_INLINE int DoQuantizeBlock(int16_t in[16], int16_t out[16],
  740. const uint16_t* const sharpen,
  741. const VP8Matrix* const mtx) {
  742. const __m128i max_coeff_2047 = _mm_set1_epi16(MAX_LEVEL);
  743. const __m128i zero = _mm_setzero_si128();
  744. __m128i coeff0, coeff8;
  745. __m128i out0, out8;
  746. __m128i packed_out;
  747. // Load all inputs.
  748. // TODO(cduvivier): Make variable declarations and allocations aligned so that
  749. // we can use _mm_load_si128 instead of _mm_loadu_si128.
  750. __m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);
  751. __m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);
  752. const __m128i iq0 = _mm_loadu_si128((__m128i*)&mtx->iq_[0]);
  753. const __m128i iq8 = _mm_loadu_si128((__m128i*)&mtx->iq_[8]);
  754. const __m128i q0 = _mm_loadu_si128((__m128i*)&mtx->q_[0]);
  755. const __m128i q8 = _mm_loadu_si128((__m128i*)&mtx->q_[8]);
  756. // extract sign(in) (0x0000 if positive, 0xffff if negative)
  757. const __m128i sign0 = _mm_cmpgt_epi16(zero, in0);
  758. const __m128i sign8 = _mm_cmpgt_epi16(zero, in8);
  759. // coeff = abs(in) = (in ^ sign) - sign
  760. coeff0 = _mm_xor_si128(in0, sign0);
  761. coeff8 = _mm_xor_si128(in8, sign8);
  762. coeff0 = _mm_sub_epi16(coeff0, sign0);
  763. coeff8 = _mm_sub_epi16(coeff8, sign8);
  764. // coeff = abs(in) + sharpen
  765. if (sharpen != NULL) {
  766. const __m128i sharpen0 = _mm_loadu_si128((__m128i*)&sharpen[0]);
  767. const __m128i sharpen8 = _mm_loadu_si128((__m128i*)&sharpen[8]);
  768. coeff0 = _mm_add_epi16(coeff0, sharpen0);
  769. coeff8 = _mm_add_epi16(coeff8, sharpen8);
  770. }
  771. // out = (coeff * iQ + B) >> QFIX
  772. {
  773. // doing calculations with 32b precision (QFIX=17)
  774. // out = (coeff * iQ)
  775. const __m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);
  776. const __m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);
  777. const __m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);
  778. const __m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);
  779. __m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);
  780. __m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);
  781. __m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);
  782. __m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);
  783. // out = (coeff * iQ + B)
  784. const __m128i bias_00 = _mm_loadu_si128((__m128i*)&mtx->bias_[0]);
  785. const __m128i bias_04 = _mm_loadu_si128((__m128i*)&mtx->bias_[4]);
  786. const __m128i bias_08 = _mm_loadu_si128((__m128i*)&mtx->bias_[8]);
  787. const __m128i bias_12 = _mm_loadu_si128((__m128i*)&mtx->bias_[12]);
  788. out_00 = _mm_add_epi32(out_00, bias_00);
  789. out_04 = _mm_add_epi32(out_04, bias_04);
  790. out_08 = _mm_add_epi32(out_08, bias_08);
  791. out_12 = _mm_add_epi32(out_12, bias_12);
  792. // out = QUANTDIV(coeff, iQ, B, QFIX)
  793. out_00 = _mm_srai_epi32(out_00, QFIX);
  794. out_04 = _mm_srai_epi32(out_04, QFIX);
  795. out_08 = _mm_srai_epi32(out_08, QFIX);
  796. out_12 = _mm_srai_epi32(out_12, QFIX);
  797. // pack result as 16b
  798. out0 = _mm_packs_epi32(out_00, out_04);
  799. out8 = _mm_packs_epi32(out_08, out_12);
  800. // if (coeff > 2047) coeff = 2047
  801. out0 = _mm_min_epi16(out0, max_coeff_2047);
  802. out8 = _mm_min_epi16(out8, max_coeff_2047);
  803. }
  804. // get sign back (if (sign[j]) out_n = -out_n)
  805. out0 = _mm_xor_si128(out0, sign0);
  806. out8 = _mm_xor_si128(out8, sign8);
  807. out0 = _mm_sub_epi16(out0, sign0);
  808. out8 = _mm_sub_epi16(out8, sign8);
  809. // in = out * Q
  810. in0 = _mm_mullo_epi16(out0, q0);
  811. in8 = _mm_mullo_epi16(out8, q8);
  812. _mm_storeu_si128((__m128i*)&in[0], in0);
  813. _mm_storeu_si128((__m128i*)&in[8], in8);
  814. // zigzag the output before storing it.
  815. //
  816. // The zigzag pattern can almost be reproduced with a small sequence of
  817. // shuffles. After it, we only need to swap the 7th (ending up in third
  818. // position instead of twelfth) and 8th values.
  819. {
  820. __m128i outZ0, outZ8;
  821. outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0));
  822. outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0));
  823. outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));
  824. outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1));
  825. outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0));
  826. outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));
  827. _mm_storeu_si128((__m128i*)&out[0], outZ0);
  828. _mm_storeu_si128((__m128i*)&out[8], outZ8);
  829. packed_out = _mm_packs_epi16(outZ0, outZ8);
  830. }
  831. {
  832. const int16_t outZ_12 = out[12];
  833. const int16_t outZ_3 = out[3];
  834. out[3] = outZ_12;
  835. out[12] = outZ_3;
  836. }
  837. // detect if all 'out' values are zeroes or not
  838. return (_mm_movemask_epi8(_mm_cmpeq_epi8(packed_out, zero)) != 0xffff);
  839. }
  840. static int QuantizeBlock(int16_t in[16], int16_t out[16],
  841. const VP8Matrix* const mtx) {
  842. return DoQuantizeBlock(in, out, &mtx->sharpen_[0], mtx);
  843. }
  844. static int QuantizeBlockWHT(int16_t in[16], int16_t out[16],
  845. const VP8Matrix* const mtx) {
  846. return DoQuantizeBlock(in, out, NULL, mtx);
  847. }
  848. // Forward declaration.
  849. void VP8SetResidualCoeffsSSE2(const int16_t* const coeffs,
  850. VP8Residual* const res);
  851. void VP8SetResidualCoeffsSSE2(const int16_t* const coeffs,
  852. VP8Residual* const res) {
  853. const __m128i c0 = _mm_loadu_si128((const __m128i*)coeffs);
  854. const __m128i c1 = _mm_loadu_si128((const __m128i*)(coeffs + 8));
  855. // Use SSE to compare 8 values with a single instruction.
  856. const __m128i zero = _mm_setzero_si128();
  857. const __m128i m0 = _mm_cmpeq_epi16(c0, zero);
  858. const __m128i m1 = _mm_cmpeq_epi16(c1, zero);
  859. // Get the comparison results as a bitmask, consisting of two times 16 bits:
  860. // two identical bits for each result. Concatenate both bitmasks to get a
  861. // single 32 bit value. Negate the mask to get the position of entries that
  862. // are not equal to zero. We don't need to mask out least significant bits
  863. // according to res->first, since coeffs[0] is 0 if res->first > 0
  864. const uint32_t mask =
  865. ~(((uint32_t)_mm_movemask_epi8(m1) << 16) | _mm_movemask_epi8(m0));
  866. // The position of the most significant non-zero bit indicates the position of
  867. // the last non-zero value. Divide the result by two because __movemask_epi8
  868. // operates on 8 bit values instead of 16 bit values.
  869. assert(res->first == 0 || coeffs[0] == 0);
  870. res->last = mask ? (BitsLog2Floor(mask) >> 1) : -1;
  871. res->coeffs = coeffs;
  872. }
  873. #endif // WEBP_USE_SSE2
  874. //------------------------------------------------------------------------------
  875. // Entry point
  876. extern void VP8EncDspInitSSE2(void);
  877. void VP8EncDspInitSSE2(void) {
  878. #if defined(WEBP_USE_SSE2)
  879. VP8CollectHistogram = CollectHistogram;
  880. VP8EncQuantizeBlock = QuantizeBlock;
  881. VP8EncQuantizeBlockWHT = QuantizeBlockWHT;
  882. VP8ITransform = ITransform;
  883. VP8FTransform = FTransform;
  884. VP8FTransformWHT = FTransformWHT;
  885. VP8SSE16x16 = SSE16x16;
  886. VP8SSE16x8 = SSE16x8;
  887. VP8SSE8x8 = SSE8x8;
  888. VP8SSE4x4 = SSE4x4;
  889. VP8TDisto4x4 = Disto4x4;
  890. VP8TDisto16x16 = Disto16x16;
  891. #endif // WEBP_USE_SSE2
  892. }