enc_neon.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. // Copyright 2012 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. // ARM NEON version of speed-critical encoding functions.
  11. //
  12. // adapted from libvpx (http://www.webmproject.org/code/)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_NEON)
  15. #include <assert.h>
  16. #include "./neon.h"
  17. #include "../enc/vp8enci.h"
  18. //------------------------------------------------------------------------------
  19. // Transforms (Paragraph 14.4)
  20. // Inverse transform.
  21. // This code is pretty much the same as TransformOne in the dec_neon.c, except
  22. // for subtraction to *ref. See the comments there for algorithmic explanations.
  23. static const int16_t kC1 = 20091;
  24. static const int16_t kC2 = 17734; // half of kC2, actually. See comment above.
  25. // This code works but is *slower* than the inlined-asm version below
  26. // (with gcc-4.6). So we disable it for now. Later, it'll be conditional to
  27. // USE_INTRINSICS define.
  28. // With gcc-4.8, it's a little faster speed than inlined-assembly.
  29. #if defined(USE_INTRINSICS)
  30. // Treats 'v' as an uint8x8_t and zero extends to an int16x8_t.
  31. static WEBP_INLINE int16x8_t ConvertU8ToS16(uint32x2_t v) {
  32. return vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(v)));
  33. }
  34. // Performs unsigned 8b saturation on 'dst01' and 'dst23' storing the result
  35. // to the corresponding rows of 'dst'.
  36. static WEBP_INLINE void SaturateAndStore4x4(uint8_t* const dst,
  37. const int16x8_t dst01,
  38. const int16x8_t dst23) {
  39. // Unsigned saturate to 8b.
  40. const uint8x8_t dst01_u8 = vqmovun_s16(dst01);
  41. const uint8x8_t dst23_u8 = vqmovun_s16(dst23);
  42. // Store the results.
  43. vst1_lane_u32((uint32_t*)(dst + 0 * BPS), vreinterpret_u32_u8(dst01_u8), 0);
  44. vst1_lane_u32((uint32_t*)(dst + 1 * BPS), vreinterpret_u32_u8(dst01_u8), 1);
  45. vst1_lane_u32((uint32_t*)(dst + 2 * BPS), vreinterpret_u32_u8(dst23_u8), 0);
  46. vst1_lane_u32((uint32_t*)(dst + 3 * BPS), vreinterpret_u32_u8(dst23_u8), 1);
  47. }
  48. static WEBP_INLINE void Add4x4(const int16x8_t row01, const int16x8_t row23,
  49. const uint8_t* const ref, uint8_t* const dst) {
  50. uint32x2_t dst01 = vdup_n_u32(0);
  51. uint32x2_t dst23 = vdup_n_u32(0);
  52. // Load the source pixels.
  53. dst01 = vld1_lane_u32((uint32_t*)(ref + 0 * BPS), dst01, 0);
  54. dst23 = vld1_lane_u32((uint32_t*)(ref + 2 * BPS), dst23, 0);
  55. dst01 = vld1_lane_u32((uint32_t*)(ref + 1 * BPS), dst01, 1);
  56. dst23 = vld1_lane_u32((uint32_t*)(ref + 3 * BPS), dst23, 1);
  57. {
  58. // Convert to 16b.
  59. const int16x8_t dst01_s16 = ConvertU8ToS16(dst01);
  60. const int16x8_t dst23_s16 = ConvertU8ToS16(dst23);
  61. // Descale with rounding.
  62. const int16x8_t out01 = vrsraq_n_s16(dst01_s16, row01, 3);
  63. const int16x8_t out23 = vrsraq_n_s16(dst23_s16, row23, 3);
  64. // Add the inverse transform.
  65. SaturateAndStore4x4(dst, out01, out23);
  66. }
  67. }
  68. static WEBP_INLINE void Transpose8x2(const int16x8_t in0, const int16x8_t in1,
  69. int16x8x2_t* const out) {
  70. // a0 a1 a2 a3 | b0 b1 b2 b3 => a0 b0 c0 d0 | a1 b1 c1 d1
  71. // c0 c1 c2 c3 | d0 d1 d2 d3 a2 b2 c2 d2 | a3 b3 c3 d3
  72. const int16x8x2_t tmp0 = vzipq_s16(in0, in1); // a0 c0 a1 c1 a2 c2 ...
  73. // b0 d0 b1 d1 b2 d2 ...
  74. *out = vzipq_s16(tmp0.val[0], tmp0.val[1]);
  75. }
  76. static WEBP_INLINE void TransformPass(int16x8x2_t* const rows) {
  77. // {rows} = in0 | in4
  78. // in8 | in12
  79. // B1 = in4 | in12
  80. const int16x8_t B1 =
  81. vcombine_s16(vget_high_s16(rows->val[0]), vget_high_s16(rows->val[1]));
  82. // C0 = kC1 * in4 | kC1 * in12
  83. // C1 = kC2 * in4 | kC2 * in12
  84. const int16x8_t C0 = vsraq_n_s16(B1, vqdmulhq_n_s16(B1, kC1), 1);
  85. const int16x8_t C1 = vqdmulhq_n_s16(B1, kC2);
  86. const int16x4_t a = vqadd_s16(vget_low_s16(rows->val[0]),
  87. vget_low_s16(rows->val[1])); // in0 + in8
  88. const int16x4_t b = vqsub_s16(vget_low_s16(rows->val[0]),
  89. vget_low_s16(rows->val[1])); // in0 - in8
  90. // c = kC2 * in4 - kC1 * in12
  91. // d = kC1 * in4 + kC2 * in12
  92. const int16x4_t c = vqsub_s16(vget_low_s16(C1), vget_high_s16(C0));
  93. const int16x4_t d = vqadd_s16(vget_low_s16(C0), vget_high_s16(C1));
  94. const int16x8_t D0 = vcombine_s16(a, b); // D0 = a | b
  95. const int16x8_t D1 = vcombine_s16(d, c); // D1 = d | c
  96. const int16x8_t E0 = vqaddq_s16(D0, D1); // a+d | b+c
  97. const int16x8_t E_tmp = vqsubq_s16(D0, D1); // a-d | b-c
  98. const int16x8_t E1 = vcombine_s16(vget_high_s16(E_tmp), vget_low_s16(E_tmp));
  99. Transpose8x2(E0, E1, rows);
  100. }
  101. static void ITransformOne(const uint8_t* ref,
  102. const int16_t* in, uint8_t* dst) {
  103. int16x8x2_t rows;
  104. INIT_VECTOR2(rows, vld1q_s16(in + 0), vld1q_s16(in + 8));
  105. TransformPass(&rows);
  106. TransformPass(&rows);
  107. Add4x4(rows.val[0], rows.val[1], ref, dst);
  108. }
  109. #else
  110. static void ITransformOne(const uint8_t* ref,
  111. const int16_t* in, uint8_t* dst) {
  112. const int kBPS = BPS;
  113. const int16_t kC1C2[] = { kC1, kC2, 0, 0 };
  114. __asm__ volatile (
  115. "vld1.16 {q1, q2}, [%[in]] \n"
  116. "vld1.16 {d0}, [%[kC1C2]] \n"
  117. // d2: in[0]
  118. // d3: in[8]
  119. // d4: in[4]
  120. // d5: in[12]
  121. "vswp d3, d4 \n"
  122. // q8 = {in[4], in[12]} * kC1 * 2 >> 16
  123. // q9 = {in[4], in[12]} * kC2 >> 16
  124. "vqdmulh.s16 q8, q2, d0[0] \n"
  125. "vqdmulh.s16 q9, q2, d0[1] \n"
  126. // d22 = a = in[0] + in[8]
  127. // d23 = b = in[0] - in[8]
  128. "vqadd.s16 d22, d2, d3 \n"
  129. "vqsub.s16 d23, d2, d3 \n"
  130. // q8 = in[4]/[12] * kC1 >> 16
  131. "vshr.s16 q8, q8, #1 \n"
  132. // Add {in[4], in[12]} back after the multiplication.
  133. "vqadd.s16 q8, q2, q8 \n"
  134. // d20 = c = in[4]*kC2 - in[12]*kC1
  135. // d21 = d = in[4]*kC1 + in[12]*kC2
  136. "vqsub.s16 d20, d18, d17 \n"
  137. "vqadd.s16 d21, d19, d16 \n"
  138. // d2 = tmp[0] = a + d
  139. // d3 = tmp[1] = b + c
  140. // d4 = tmp[2] = b - c
  141. // d5 = tmp[3] = a - d
  142. "vqadd.s16 d2, d22, d21 \n"
  143. "vqadd.s16 d3, d23, d20 \n"
  144. "vqsub.s16 d4, d23, d20 \n"
  145. "vqsub.s16 d5, d22, d21 \n"
  146. "vzip.16 q1, q2 \n"
  147. "vzip.16 q1, q2 \n"
  148. "vswp d3, d4 \n"
  149. // q8 = {tmp[4], tmp[12]} * kC1 * 2 >> 16
  150. // q9 = {tmp[4], tmp[12]} * kC2 >> 16
  151. "vqdmulh.s16 q8, q2, d0[0] \n"
  152. "vqdmulh.s16 q9, q2, d0[1] \n"
  153. // d22 = a = tmp[0] + tmp[8]
  154. // d23 = b = tmp[0] - tmp[8]
  155. "vqadd.s16 d22, d2, d3 \n"
  156. "vqsub.s16 d23, d2, d3 \n"
  157. "vshr.s16 q8, q8, #1 \n"
  158. "vqadd.s16 q8, q2, q8 \n"
  159. // d20 = c = in[4]*kC2 - in[12]*kC1
  160. // d21 = d = in[4]*kC1 + in[12]*kC2
  161. "vqsub.s16 d20, d18, d17 \n"
  162. "vqadd.s16 d21, d19, d16 \n"
  163. // d2 = tmp[0] = a + d
  164. // d3 = tmp[1] = b + c
  165. // d4 = tmp[2] = b - c
  166. // d5 = tmp[3] = a - d
  167. "vqadd.s16 d2, d22, d21 \n"
  168. "vqadd.s16 d3, d23, d20 \n"
  169. "vqsub.s16 d4, d23, d20 \n"
  170. "vqsub.s16 d5, d22, d21 \n"
  171. "vld1.32 d6[0], [%[ref]], %[kBPS] \n"
  172. "vld1.32 d6[1], [%[ref]], %[kBPS] \n"
  173. "vld1.32 d7[0], [%[ref]], %[kBPS] \n"
  174. "vld1.32 d7[1], [%[ref]], %[kBPS] \n"
  175. "sub %[ref], %[ref], %[kBPS], lsl #2 \n"
  176. // (val) + 4 >> 3
  177. "vrshr.s16 d2, d2, #3 \n"
  178. "vrshr.s16 d3, d3, #3 \n"
  179. "vrshr.s16 d4, d4, #3 \n"
  180. "vrshr.s16 d5, d5, #3 \n"
  181. "vzip.16 q1, q2 \n"
  182. "vzip.16 q1, q2 \n"
  183. // Must accumulate before saturating
  184. "vmovl.u8 q8, d6 \n"
  185. "vmovl.u8 q9, d7 \n"
  186. "vqadd.s16 q1, q1, q8 \n"
  187. "vqadd.s16 q2, q2, q9 \n"
  188. "vqmovun.s16 d0, q1 \n"
  189. "vqmovun.s16 d1, q2 \n"
  190. "vst1.32 d0[0], [%[dst]], %[kBPS] \n"
  191. "vst1.32 d0[1], [%[dst]], %[kBPS] \n"
  192. "vst1.32 d1[0], [%[dst]], %[kBPS] \n"
  193. "vst1.32 d1[1], [%[dst]] \n"
  194. : [in] "+r"(in), [dst] "+r"(dst) // modified registers
  195. : [kBPS] "r"(kBPS), [kC1C2] "r"(kC1C2), [ref] "r"(ref) // constants
  196. : "memory", "q0", "q1", "q2", "q8", "q9", "q10", "q11" // clobbered
  197. );
  198. }
  199. #endif // USE_INTRINSICS
  200. static void ITransform(const uint8_t* ref,
  201. const int16_t* in, uint8_t* dst, int do_two) {
  202. ITransformOne(ref, in, dst);
  203. if (do_two) {
  204. ITransformOne(ref + 4, in + 16, dst + 4);
  205. }
  206. }
  207. // Load all 4x4 pixels into a single uint8x16_t variable.
  208. static uint8x16_t Load4x4(const uint8_t* src) {
  209. uint32x4_t out = vdupq_n_u32(0);
  210. out = vld1q_lane_u32((const uint32_t*)(src + 0 * BPS), out, 0);
  211. out = vld1q_lane_u32((const uint32_t*)(src + 1 * BPS), out, 1);
  212. out = vld1q_lane_u32((const uint32_t*)(src + 2 * BPS), out, 2);
  213. out = vld1q_lane_u32((const uint32_t*)(src + 3 * BPS), out, 3);
  214. return vreinterpretq_u8_u32(out);
  215. }
  216. // Forward transform.
  217. #if defined(USE_INTRINSICS)
  218. static WEBP_INLINE void Transpose4x4_S16(const int16x4_t A, const int16x4_t B,
  219. const int16x4_t C, const int16x4_t D,
  220. int16x8_t* const out01,
  221. int16x8_t* const out32) {
  222. const int16x4x2_t AB = vtrn_s16(A, B);
  223. const int16x4x2_t CD = vtrn_s16(C, D);
  224. const int32x2x2_t tmp02 = vtrn_s32(vreinterpret_s32_s16(AB.val[0]),
  225. vreinterpret_s32_s16(CD.val[0]));
  226. const int32x2x2_t tmp13 = vtrn_s32(vreinterpret_s32_s16(AB.val[1]),
  227. vreinterpret_s32_s16(CD.val[1]));
  228. *out01 = vreinterpretq_s16_s64(
  229. vcombine_s64(vreinterpret_s64_s32(tmp02.val[0]),
  230. vreinterpret_s64_s32(tmp13.val[0])));
  231. *out32 = vreinterpretq_s16_s64(
  232. vcombine_s64(vreinterpret_s64_s32(tmp13.val[1]),
  233. vreinterpret_s64_s32(tmp02.val[1])));
  234. }
  235. static WEBP_INLINE int16x8_t DiffU8ToS16(const uint8x8_t a,
  236. const uint8x8_t b) {
  237. return vreinterpretq_s16_u16(vsubl_u8(a, b));
  238. }
  239. static void FTransform(const uint8_t* src, const uint8_t* ref,
  240. int16_t* out) {
  241. int16x8_t d0d1, d3d2; // working 4x4 int16 variables
  242. {
  243. const uint8x16_t S0 = Load4x4(src);
  244. const uint8x16_t R0 = Load4x4(ref);
  245. const int16x8_t D0D1 = DiffU8ToS16(vget_low_u8(S0), vget_low_u8(R0));
  246. const int16x8_t D2D3 = DiffU8ToS16(vget_high_u8(S0), vget_high_u8(R0));
  247. const int16x4_t D0 = vget_low_s16(D0D1);
  248. const int16x4_t D1 = vget_high_s16(D0D1);
  249. const int16x4_t D2 = vget_low_s16(D2D3);
  250. const int16x4_t D3 = vget_high_s16(D2D3);
  251. Transpose4x4_S16(D0, D1, D2, D3, &d0d1, &d3d2);
  252. }
  253. { // 1rst pass
  254. const int32x4_t kCst937 = vdupq_n_s32(937);
  255. const int32x4_t kCst1812 = vdupq_n_s32(1812);
  256. const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)
  257. const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)
  258. const int16x8_t a0a1_2 = vshlq_n_s16(a0a1, 3);
  259. const int16x4_t tmp0 = vadd_s16(vget_low_s16(a0a1_2),
  260. vget_high_s16(a0a1_2));
  261. const int16x4_t tmp2 = vsub_s16(vget_low_s16(a0a1_2),
  262. vget_high_s16(a0a1_2));
  263. const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);
  264. const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);
  265. const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);
  266. const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);
  267. const int16x4_t tmp1 = vshrn_n_s32(vaddq_s32(a2_p_a3, kCst1812), 9);
  268. const int16x4_t tmp3 = vshrn_n_s32(vaddq_s32(a3_m_a2, kCst937), 9);
  269. Transpose4x4_S16(tmp0, tmp1, tmp2, tmp3, &d0d1, &d3d2);
  270. }
  271. { // 2nd pass
  272. // the (1<<16) addition is for the replacement: a3!=0 <-> 1-(a3==0)
  273. const int32x4_t kCst12000 = vdupq_n_s32(12000 + (1 << 16));
  274. const int32x4_t kCst51000 = vdupq_n_s32(51000);
  275. const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)
  276. const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)
  277. const int16x4_t a0_k7 = vadd_s16(vget_low_s16(a0a1), vdup_n_s16(7));
  278. const int16x4_t out0 = vshr_n_s16(vadd_s16(a0_k7, vget_high_s16(a0a1)), 4);
  279. const int16x4_t out2 = vshr_n_s16(vsub_s16(a0_k7, vget_high_s16(a0a1)), 4);
  280. const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);
  281. const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);
  282. const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);
  283. const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);
  284. const int16x4_t tmp1 = vaddhn_s32(a2_p_a3, kCst12000);
  285. const int16x4_t out3 = vaddhn_s32(a3_m_a2, kCst51000);
  286. const int16x4_t a3_eq_0 =
  287. vreinterpret_s16_u16(vceq_s16(vget_low_s16(a3a2), vdup_n_s16(0)));
  288. const int16x4_t out1 = vadd_s16(tmp1, a3_eq_0);
  289. vst1_s16(out + 0, out0);
  290. vst1_s16(out + 4, out1);
  291. vst1_s16(out + 8, out2);
  292. vst1_s16(out + 12, out3);
  293. }
  294. }
  295. #else
  296. // adapted from vp8/encoder/arm/neon/shortfdct_neon.asm
  297. static const int16_t kCoeff16[] = {
  298. 5352, 5352, 5352, 5352, 2217, 2217, 2217, 2217
  299. };
  300. static const int32_t kCoeff32[] = {
  301. 1812, 1812, 1812, 1812,
  302. 937, 937, 937, 937,
  303. 12000, 12000, 12000, 12000,
  304. 51000, 51000, 51000, 51000
  305. };
  306. static void FTransform(const uint8_t* src, const uint8_t* ref,
  307. int16_t* out) {
  308. const int kBPS = BPS;
  309. const uint8_t* src_ptr = src;
  310. const uint8_t* ref_ptr = ref;
  311. const int16_t* coeff16 = kCoeff16;
  312. const int32_t* coeff32 = kCoeff32;
  313. __asm__ volatile (
  314. // load src into q4, q5 in high half
  315. "vld1.8 {d8}, [%[src_ptr]], %[kBPS] \n"
  316. "vld1.8 {d10}, [%[src_ptr]], %[kBPS] \n"
  317. "vld1.8 {d9}, [%[src_ptr]], %[kBPS] \n"
  318. "vld1.8 {d11}, [%[src_ptr]] \n"
  319. // load ref into q6, q7 in high half
  320. "vld1.8 {d12}, [%[ref_ptr]], %[kBPS] \n"
  321. "vld1.8 {d14}, [%[ref_ptr]], %[kBPS] \n"
  322. "vld1.8 {d13}, [%[ref_ptr]], %[kBPS] \n"
  323. "vld1.8 {d15}, [%[ref_ptr]] \n"
  324. // Pack the high values in to q4 and q6
  325. "vtrn.32 q4, q5 \n"
  326. "vtrn.32 q6, q7 \n"
  327. // d[0-3] = src - ref
  328. "vsubl.u8 q0, d8, d12 \n"
  329. "vsubl.u8 q1, d9, d13 \n"
  330. // load coeff16 into q8(d16=5352, d17=2217)
  331. "vld1.16 {q8}, [%[coeff16]] \n"
  332. // load coeff32 high half into q9 = 1812, q10 = 937
  333. "vld1.32 {q9, q10}, [%[coeff32]]! \n"
  334. // load coeff32 low half into q11=12000, q12=51000
  335. "vld1.32 {q11,q12}, [%[coeff32]] \n"
  336. // part 1
  337. // Transpose. Register dN is the same as dN in C
  338. "vtrn.32 d0, d2 \n"
  339. "vtrn.32 d1, d3 \n"
  340. "vtrn.16 d0, d1 \n"
  341. "vtrn.16 d2, d3 \n"
  342. "vadd.s16 d4, d0, d3 \n" // a0 = d0 + d3
  343. "vadd.s16 d5, d1, d2 \n" // a1 = d1 + d2
  344. "vsub.s16 d6, d1, d2 \n" // a2 = d1 - d2
  345. "vsub.s16 d7, d0, d3 \n" // a3 = d0 - d3
  346. "vadd.s16 d0, d4, d5 \n" // a0 + a1
  347. "vshl.s16 d0, d0, #3 \n" // temp[0+i*4] = (a0+a1) << 3
  348. "vsub.s16 d2, d4, d5 \n" // a0 - a1
  349. "vshl.s16 d2, d2, #3 \n" // (temp[2+i*4] = (a0-a1) << 3
  350. "vmlal.s16 q9, d7, d16 \n" // a3*5352 + 1812
  351. "vmlal.s16 q10, d7, d17 \n" // a3*2217 + 937
  352. "vmlal.s16 q9, d6, d17 \n" // a2*2217 + a3*5352 + 1812
  353. "vmlsl.s16 q10, d6, d16 \n" // a3*2217 + 937 - a2*5352
  354. // temp[1+i*4] = (d2*2217 + d3*5352 + 1812) >> 9
  355. // temp[3+i*4] = (d3*2217 + 937 - d2*5352) >> 9
  356. "vshrn.s32 d1, q9, #9 \n"
  357. "vshrn.s32 d3, q10, #9 \n"
  358. // part 2
  359. // transpose d0=ip[0], d1=ip[4], d2=ip[8], d3=ip[12]
  360. "vtrn.32 d0, d2 \n"
  361. "vtrn.32 d1, d3 \n"
  362. "vtrn.16 d0, d1 \n"
  363. "vtrn.16 d2, d3 \n"
  364. "vmov.s16 d26, #7 \n"
  365. "vadd.s16 d4, d0, d3 \n" // a1 = ip[0] + ip[12]
  366. "vadd.s16 d5, d1, d2 \n" // b1 = ip[4] + ip[8]
  367. "vsub.s16 d6, d1, d2 \n" // c1 = ip[4] - ip[8]
  368. "vadd.s16 d4, d4, d26 \n" // a1 + 7
  369. "vsub.s16 d7, d0, d3 \n" // d1 = ip[0] - ip[12]
  370. "vadd.s16 d0, d4, d5 \n" // op[0] = a1 + b1 + 7
  371. "vsub.s16 d2, d4, d5 \n" // op[8] = a1 - b1 + 7
  372. "vmlal.s16 q11, d7, d16 \n" // d1*5352 + 12000
  373. "vmlal.s16 q12, d7, d17 \n" // d1*2217 + 51000
  374. "vceq.s16 d4, d7, #0 \n"
  375. "vshr.s16 d0, d0, #4 \n"
  376. "vshr.s16 d2, d2, #4 \n"
  377. "vmlal.s16 q11, d6, d17 \n" // c1*2217 + d1*5352 + 12000
  378. "vmlsl.s16 q12, d6, d16 \n" // d1*2217 - c1*5352 + 51000
  379. "vmvn d4, d4 \n" // !(d1 == 0)
  380. // op[4] = (c1*2217 + d1*5352 + 12000)>>16
  381. "vshrn.s32 d1, q11, #16 \n"
  382. // op[4] += (d1!=0)
  383. "vsub.s16 d1, d1, d4 \n"
  384. // op[12]= (d1*2217 - c1*5352 + 51000)>>16
  385. "vshrn.s32 d3, q12, #16 \n"
  386. // set result to out array
  387. "vst1.16 {q0, q1}, [%[out]] \n"
  388. : [src_ptr] "+r"(src_ptr), [ref_ptr] "+r"(ref_ptr),
  389. [coeff32] "+r"(coeff32) // modified registers
  390. : [kBPS] "r"(kBPS), [coeff16] "r"(coeff16),
  391. [out] "r"(out) // constants
  392. : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
  393. "q10", "q11", "q12", "q13" // clobbered
  394. );
  395. }
  396. #endif
  397. #define LOAD_LANE_16b(VALUE, LANE) do { \
  398. (VALUE) = vld1_lane_s16(src, (VALUE), (LANE)); \
  399. src += stride; \
  400. } while (0)
  401. static void FTransformWHT(const int16_t* src, int16_t* out) {
  402. const int stride = 16;
  403. const int16x4_t zero = vdup_n_s16(0);
  404. int32x4x4_t tmp0;
  405. int16x4x4_t in;
  406. INIT_VECTOR4(in, zero, zero, zero, zero);
  407. LOAD_LANE_16b(in.val[0], 0);
  408. LOAD_LANE_16b(in.val[1], 0);
  409. LOAD_LANE_16b(in.val[2], 0);
  410. LOAD_LANE_16b(in.val[3], 0);
  411. LOAD_LANE_16b(in.val[0], 1);
  412. LOAD_LANE_16b(in.val[1], 1);
  413. LOAD_LANE_16b(in.val[2], 1);
  414. LOAD_LANE_16b(in.val[3], 1);
  415. LOAD_LANE_16b(in.val[0], 2);
  416. LOAD_LANE_16b(in.val[1], 2);
  417. LOAD_LANE_16b(in.val[2], 2);
  418. LOAD_LANE_16b(in.val[3], 2);
  419. LOAD_LANE_16b(in.val[0], 3);
  420. LOAD_LANE_16b(in.val[1], 3);
  421. LOAD_LANE_16b(in.val[2], 3);
  422. LOAD_LANE_16b(in.val[3], 3);
  423. {
  424. // a0 = in[0 * 16] + in[2 * 16]
  425. // a1 = in[1 * 16] + in[3 * 16]
  426. // a2 = in[1 * 16] - in[3 * 16]
  427. // a3 = in[0 * 16] - in[2 * 16]
  428. const int32x4_t a0 = vaddl_s16(in.val[0], in.val[2]);
  429. const int32x4_t a1 = vaddl_s16(in.val[1], in.val[3]);
  430. const int32x4_t a2 = vsubl_s16(in.val[1], in.val[3]);
  431. const int32x4_t a3 = vsubl_s16(in.val[0], in.val[2]);
  432. tmp0.val[0] = vaddq_s32(a0, a1);
  433. tmp0.val[1] = vaddq_s32(a3, a2);
  434. tmp0.val[2] = vsubq_s32(a3, a2);
  435. tmp0.val[3] = vsubq_s32(a0, a1);
  436. }
  437. {
  438. const int32x4x4_t tmp1 = Transpose4x4(tmp0);
  439. // a0 = tmp[0 + i] + tmp[ 8 + i]
  440. // a1 = tmp[4 + i] + tmp[12 + i]
  441. // a2 = tmp[4 + i] - tmp[12 + i]
  442. // a3 = tmp[0 + i] - tmp[ 8 + i]
  443. const int32x4_t a0 = vaddq_s32(tmp1.val[0], tmp1.val[2]);
  444. const int32x4_t a1 = vaddq_s32(tmp1.val[1], tmp1.val[3]);
  445. const int32x4_t a2 = vsubq_s32(tmp1.val[1], tmp1.val[3]);
  446. const int32x4_t a3 = vsubq_s32(tmp1.val[0], tmp1.val[2]);
  447. const int32x4_t b0 = vhaddq_s32(a0, a1); // (a0 + a1) >> 1
  448. const int32x4_t b1 = vhaddq_s32(a3, a2); // (a3 + a2) >> 1
  449. const int32x4_t b2 = vhsubq_s32(a3, a2); // (a3 - a2) >> 1
  450. const int32x4_t b3 = vhsubq_s32(a0, a1); // (a0 - a1) >> 1
  451. const int16x4_t out0 = vmovn_s32(b0);
  452. const int16x4_t out1 = vmovn_s32(b1);
  453. const int16x4_t out2 = vmovn_s32(b2);
  454. const int16x4_t out3 = vmovn_s32(b3);
  455. vst1_s16(out + 0, out0);
  456. vst1_s16(out + 4, out1);
  457. vst1_s16(out + 8, out2);
  458. vst1_s16(out + 12, out3);
  459. }
  460. }
  461. #undef LOAD_LANE_16b
  462. //------------------------------------------------------------------------------
  463. // Texture distortion
  464. //
  465. // We try to match the spectral content (weighted) between source and
  466. // reconstructed samples.
  467. // This code works but is *slower* than the inlined-asm version below
  468. // (with gcc-4.6). So we disable it for now. Later, it'll be conditional to
  469. // USE_INTRINSICS define.
  470. // With gcc-4.8, it's only slightly slower than the inlined.
  471. #if defined(USE_INTRINSICS)
  472. // Zero extend an uint16x4_t 'v' to an int32x4_t.
  473. static WEBP_INLINE int32x4_t ConvertU16ToS32(uint16x4_t v) {
  474. return vreinterpretq_s32_u32(vmovl_u16(v));
  475. }
  476. // Does a regular 4x4 transpose followed by an adjustment of the upper columns
  477. // in the inner rows to restore the source order of differences,
  478. // i.e., a0 - a1 | a3 - a2.
  479. static WEBP_INLINE int32x4x4_t DistoTranspose4x4(const int32x4x4_t rows) {
  480. int32x4x4_t out = Transpose4x4(rows);
  481. // restore source order in the columns containing differences.
  482. const int32x2_t r1h = vget_high_s32(out.val[1]);
  483. const int32x2_t r2h = vget_high_s32(out.val[2]);
  484. out.val[1] = vcombine_s32(vget_low_s32(out.val[1]), r2h);
  485. out.val[2] = vcombine_s32(vget_low_s32(out.val[2]), r1h);
  486. return out;
  487. }
  488. static WEBP_INLINE int32x4x4_t DistoHorizontalPass(const uint8x8_t r0r1,
  489. const uint8x8_t r2r3) {
  490. // a0 = in[0] + in[2] | a1 = in[1] + in[3]
  491. const uint16x8_t a0a1 = vaddl_u8(r0r1, r2r3);
  492. // a3 = in[0] - in[2] | a2 = in[1] - in[3]
  493. const uint16x8_t a3a2 = vsubl_u8(r0r1, r2r3);
  494. const int32x4_t tmp0 = vpaddlq_s16(vreinterpretq_s16_u16(a0a1)); // a0 + a1
  495. const int32x4_t tmp1 = vpaddlq_s16(vreinterpretq_s16_u16(a3a2)); // a3 + a2
  496. // no pairwise subtraction; reorder to perform tmp[2]/tmp[3] calculations.
  497. // a0a0 a3a3 a0a0 a3a3 a0a0 a3a3 a0a0 a3a3
  498. // a1a1 a2a2 a1a1 a2a2 a1a1 a2a2 a1a1 a2a2
  499. const int16x8x2_t transpose =
  500. vtrnq_s16(vreinterpretq_s16_u16(a0a1), vreinterpretq_s16_u16(a3a2));
  501. // tmp[3] = a0 - a1 | tmp[2] = a3 - a2
  502. const int32x4_t tmp32_1 = vsubl_s16(vget_low_s16(transpose.val[0]),
  503. vget_low_s16(transpose.val[1]));
  504. const int32x4_t tmp32_2 = vsubl_s16(vget_high_s16(transpose.val[0]),
  505. vget_high_s16(transpose.val[1]));
  506. // [0]: tmp[3] [1]: tmp[2]
  507. const int32x4x2_t split = vtrnq_s32(tmp32_1, tmp32_2);
  508. const int32x4x4_t res = { { tmp0, tmp1, split.val[1], split.val[0] } };
  509. return res;
  510. }
  511. static WEBP_INLINE int32x4x4_t DistoVerticalPass(const int32x4x4_t rows) {
  512. // a0 = tmp[0 + i] + tmp[8 + i];
  513. const int32x4_t a0 = vaddq_s32(rows.val[0], rows.val[1]);
  514. // a1 = tmp[4 + i] + tmp[12+ i];
  515. const int32x4_t a1 = vaddq_s32(rows.val[2], rows.val[3]);
  516. // a2 = tmp[4 + i] - tmp[12+ i];
  517. const int32x4_t a2 = vsubq_s32(rows.val[2], rows.val[3]);
  518. // a3 = tmp[0 + i] - tmp[8 + i];
  519. const int32x4_t a3 = vsubq_s32(rows.val[0], rows.val[1]);
  520. const int32x4_t b0 = vqabsq_s32(vaddq_s32(a0, a1)); // abs(a0 + a1)
  521. const int32x4_t b1 = vqabsq_s32(vaddq_s32(a3, a2)); // abs(a3 + a2)
  522. const int32x4_t b2 = vabdq_s32(a3, a2); // abs(a3 - a2)
  523. const int32x4_t b3 = vabdq_s32(a0, a1); // abs(a0 - a1)
  524. const int32x4x4_t res = { { b0, b1, b2, b3 } };
  525. return res;
  526. }
  527. // Calculate the weighted sum of the rows in 'b'.
  528. static WEBP_INLINE int64x1_t DistoSum(const int32x4x4_t b,
  529. const int32x4_t w0, const int32x4_t w1,
  530. const int32x4_t w2, const int32x4_t w3) {
  531. const int32x4_t s0 = vmulq_s32(w0, b.val[0]);
  532. const int32x4_t s1 = vmlaq_s32(s0, w1, b.val[1]);
  533. const int32x4_t s2 = vmlaq_s32(s1, w2, b.val[2]);
  534. const int32x4_t s3 = vmlaq_s32(s2, w3, b.val[3]);
  535. const int64x2_t sum1 = vpaddlq_s32(s3);
  536. const int64x1_t sum2 = vadd_s64(vget_low_s64(sum1), vget_high_s64(sum1));
  537. return sum2;
  538. }
  539. #define LOAD_LANE_32b(src, VALUE, LANE) \
  540. (VALUE) = vld1q_lane_u32((const uint32_t*)(src), (VALUE), (LANE))
  541. // Hadamard transform
  542. // Returns the weighted sum of the absolute value of transformed coefficients.
  543. static int Disto4x4(const uint8_t* const a, const uint8_t* const b,
  544. const uint16_t* const w) {
  545. uint32x4_t d0d1 = { 0, 0, 0, 0 };
  546. uint32x4_t d2d3 = { 0, 0, 0, 0 };
  547. LOAD_LANE_32b(a + 0 * BPS, d0d1, 0); // a00 a01 a02 a03
  548. LOAD_LANE_32b(a + 1 * BPS, d0d1, 1); // a10 a11 a12 a13
  549. LOAD_LANE_32b(b + 0 * BPS, d0d1, 2); // b00 b01 b02 b03
  550. LOAD_LANE_32b(b + 1 * BPS, d0d1, 3); // b10 b11 b12 b13
  551. LOAD_LANE_32b(a + 2 * BPS, d2d3, 0); // a20 a21 a22 a23
  552. LOAD_LANE_32b(a + 3 * BPS, d2d3, 1); // a30 a31 a32 a33
  553. LOAD_LANE_32b(b + 2 * BPS, d2d3, 2); // b20 b21 b22 b23
  554. LOAD_LANE_32b(b + 3 * BPS, d2d3, 3); // b30 b31 b32 b33
  555. {
  556. // a00 a01 a20 a21 a10 a11 a30 a31 b00 b01 b20 b21 b10 b11 b30 b31
  557. // a02 a03 a22 a23 a12 a13 a32 a33 b02 b03 b22 b23 b12 b13 b32 b33
  558. const uint16x8x2_t tmp =
  559. vtrnq_u16(vreinterpretq_u16_u32(d0d1), vreinterpretq_u16_u32(d2d3));
  560. const uint8x16_t d0d1u8 = vreinterpretq_u8_u16(tmp.val[0]);
  561. const uint8x16_t d2d3u8 = vreinterpretq_u8_u16(tmp.val[1]);
  562. const int32x4x4_t hpass_a = DistoHorizontalPass(vget_low_u8(d0d1u8),
  563. vget_low_u8(d2d3u8));
  564. const int32x4x4_t hpass_b = DistoHorizontalPass(vget_high_u8(d0d1u8),
  565. vget_high_u8(d2d3u8));
  566. const int32x4x4_t tmp_a = DistoTranspose4x4(hpass_a);
  567. const int32x4x4_t tmp_b = DistoTranspose4x4(hpass_b);
  568. const int32x4x4_t vpass_a = DistoVerticalPass(tmp_a);
  569. const int32x4x4_t vpass_b = DistoVerticalPass(tmp_b);
  570. const int32x4_t w0 = ConvertU16ToS32(vld1_u16(w + 0));
  571. const int32x4_t w1 = ConvertU16ToS32(vld1_u16(w + 4));
  572. const int32x4_t w2 = ConvertU16ToS32(vld1_u16(w + 8));
  573. const int32x4_t w3 = ConvertU16ToS32(vld1_u16(w + 12));
  574. const int64x1_t sum1 = DistoSum(vpass_a, w0, w1, w2, w3);
  575. const int64x1_t sum2 = DistoSum(vpass_b, w0, w1, w2, w3);
  576. const int32x2_t diff = vabd_s32(vreinterpret_s32_s64(sum1),
  577. vreinterpret_s32_s64(sum2));
  578. const int32x2_t res = vshr_n_s32(diff, 5);
  579. return vget_lane_s32(res, 0);
  580. }
  581. }
  582. #undef LOAD_LANE_32b
  583. #else
  584. // Hadamard transform
  585. // Returns the weighted sum of the absolute value of transformed coefficients.
  586. static int Disto4x4(const uint8_t* const a, const uint8_t* const b,
  587. const uint16_t* const w) {
  588. const int kBPS = BPS;
  589. const uint8_t* A = a;
  590. const uint8_t* B = b;
  591. const uint16_t* W = w;
  592. int sum;
  593. __asm__ volatile (
  594. "vld1.32 d0[0], [%[a]], %[kBPS] \n"
  595. "vld1.32 d0[1], [%[a]], %[kBPS] \n"
  596. "vld1.32 d2[0], [%[a]], %[kBPS] \n"
  597. "vld1.32 d2[1], [%[a]] \n"
  598. "vld1.32 d1[0], [%[b]], %[kBPS] \n"
  599. "vld1.32 d1[1], [%[b]], %[kBPS] \n"
  600. "vld1.32 d3[0], [%[b]], %[kBPS] \n"
  601. "vld1.32 d3[1], [%[b]] \n"
  602. // a d0/d2, b d1/d3
  603. // d0/d1: 01 01 01 01
  604. // d2/d3: 23 23 23 23
  605. // But: it goes 01 45 23 67
  606. // Notice the middle values are transposed
  607. "vtrn.16 q0, q1 \n"
  608. // {a0, a1} = {in[0] + in[2], in[1] + in[3]}
  609. "vaddl.u8 q2, d0, d2 \n"
  610. "vaddl.u8 q10, d1, d3 \n"
  611. // {a3, a2} = {in[0] - in[2], in[1] - in[3]}
  612. "vsubl.u8 q3, d0, d2 \n"
  613. "vsubl.u8 q11, d1, d3 \n"
  614. // tmp[0] = a0 + a1
  615. "vpaddl.s16 q0, q2 \n"
  616. "vpaddl.s16 q8, q10 \n"
  617. // tmp[1] = a3 + a2
  618. "vpaddl.s16 q1, q3 \n"
  619. "vpaddl.s16 q9, q11 \n"
  620. // No pair subtract
  621. // q2 = {a0, a3}
  622. // q3 = {a1, a2}
  623. "vtrn.16 q2, q3 \n"
  624. "vtrn.16 q10, q11 \n"
  625. // {tmp[3], tmp[2]} = {a0 - a1, a3 - a2}
  626. "vsubl.s16 q12, d4, d6 \n"
  627. "vsubl.s16 q13, d5, d7 \n"
  628. "vsubl.s16 q14, d20, d22 \n"
  629. "vsubl.s16 q15, d21, d23 \n"
  630. // separate tmp[3] and tmp[2]
  631. // q12 = tmp[3]
  632. // q13 = tmp[2]
  633. "vtrn.32 q12, q13 \n"
  634. "vtrn.32 q14, q15 \n"
  635. // Transpose tmp for a
  636. "vswp d1, d26 \n" // vtrn.64
  637. "vswp d3, d24 \n" // vtrn.64
  638. "vtrn.32 q0, q1 \n"
  639. "vtrn.32 q13, q12 \n"
  640. // Transpose tmp for b
  641. "vswp d17, d30 \n" // vtrn.64
  642. "vswp d19, d28 \n" // vtrn.64
  643. "vtrn.32 q8, q9 \n"
  644. "vtrn.32 q15, q14 \n"
  645. // The first Q register is a, the second b.
  646. // q0/8 tmp[0-3]
  647. // q13/15 tmp[4-7]
  648. // q1/9 tmp[8-11]
  649. // q12/14 tmp[12-15]
  650. // These are still in 01 45 23 67 order. We fix it easily in the addition
  651. // case but the subtraction propagates them.
  652. "vswp d3, d27 \n"
  653. "vswp d19, d31 \n"
  654. // a0 = tmp[0] + tmp[8]
  655. "vadd.s32 q2, q0, q1 \n"
  656. "vadd.s32 q3, q8, q9 \n"
  657. // a1 = tmp[4] + tmp[12]
  658. "vadd.s32 q10, q13, q12 \n"
  659. "vadd.s32 q11, q15, q14 \n"
  660. // a2 = tmp[4] - tmp[12]
  661. "vsub.s32 q13, q13, q12 \n"
  662. "vsub.s32 q15, q15, q14 \n"
  663. // a3 = tmp[0] - tmp[8]
  664. "vsub.s32 q0, q0, q1 \n"
  665. "vsub.s32 q8, q8, q9 \n"
  666. // b0 = a0 + a1
  667. "vadd.s32 q1, q2, q10 \n"
  668. "vadd.s32 q9, q3, q11 \n"
  669. // b1 = a3 + a2
  670. "vadd.s32 q12, q0, q13 \n"
  671. "vadd.s32 q14, q8, q15 \n"
  672. // b2 = a3 - a2
  673. "vsub.s32 q0, q0, q13 \n"
  674. "vsub.s32 q8, q8, q15 \n"
  675. // b3 = a0 - a1
  676. "vsub.s32 q2, q2, q10 \n"
  677. "vsub.s32 q3, q3, q11 \n"
  678. "vld1.64 {q10, q11}, [%[w]] \n"
  679. // abs(b0)
  680. "vabs.s32 q1, q1 \n"
  681. "vabs.s32 q9, q9 \n"
  682. // abs(b1)
  683. "vabs.s32 q12, q12 \n"
  684. "vabs.s32 q14, q14 \n"
  685. // abs(b2)
  686. "vabs.s32 q0, q0 \n"
  687. "vabs.s32 q8, q8 \n"
  688. // abs(b3)
  689. "vabs.s32 q2, q2 \n"
  690. "vabs.s32 q3, q3 \n"
  691. // expand w before using.
  692. "vmovl.u16 q13, d20 \n"
  693. "vmovl.u16 q15, d21 \n"
  694. // w[0] * abs(b0)
  695. "vmul.u32 q1, q1, q13 \n"
  696. "vmul.u32 q9, q9, q13 \n"
  697. // w[4] * abs(b1)
  698. "vmla.u32 q1, q12, q15 \n"
  699. "vmla.u32 q9, q14, q15 \n"
  700. // expand w before using.
  701. "vmovl.u16 q13, d22 \n"
  702. "vmovl.u16 q15, d23 \n"
  703. // w[8] * abs(b1)
  704. "vmla.u32 q1, q0, q13 \n"
  705. "vmla.u32 q9, q8, q13 \n"
  706. // w[12] * abs(b1)
  707. "vmla.u32 q1, q2, q15 \n"
  708. "vmla.u32 q9, q3, q15 \n"
  709. // Sum the arrays
  710. "vpaddl.u32 q1, q1 \n"
  711. "vpaddl.u32 q9, q9 \n"
  712. "vadd.u64 d2, d3 \n"
  713. "vadd.u64 d18, d19 \n"
  714. // Hadamard transform needs 4 bits of extra precision (2 bits in each
  715. // direction) for dynamic raw. Weights w[] are 16bits at max, so the maximum
  716. // precision for coeff is 8bit of input + 4bits of Hadamard transform +
  717. // 16bits for w[] + 2 bits of abs() summation.
  718. //
  719. // This uses a maximum of 31 bits (signed). Discarding the top 32 bits is
  720. // A-OK.
  721. // sum2 - sum1
  722. "vsub.u32 d0, d2, d18 \n"
  723. // abs(sum2 - sum1)
  724. "vabs.s32 d0, d0 \n"
  725. // abs(sum2 - sum1) >> 5
  726. "vshr.u32 d0, #5 \n"
  727. // It would be better to move the value straight into r0 but I'm not
  728. // entirely sure how this works with inline assembly.
  729. "vmov.32 %[sum], d0[0] \n"
  730. : [sum] "=r"(sum), [a] "+r"(A), [b] "+r"(B), [w] "+r"(W)
  731. : [kBPS] "r"(kBPS)
  732. : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
  733. "q10", "q11", "q12", "q13", "q14", "q15" // clobbered
  734. ) ;
  735. return sum;
  736. }
  737. #endif // USE_INTRINSICS
  738. static int Disto16x16(const uint8_t* const a, const uint8_t* const b,
  739. const uint16_t* const w) {
  740. int D = 0;
  741. int x, y;
  742. for (y = 0; y < 16 * BPS; y += 4 * BPS) {
  743. for (x = 0; x < 16; x += 4) {
  744. D += Disto4x4(a + x + y, b + x + y, w);
  745. }
  746. }
  747. return D;
  748. }
  749. //------------------------------------------------------------------------------
  750. static void CollectHistogram(const uint8_t* ref, const uint8_t* pred,
  751. int start_block, int end_block,
  752. VP8Histogram* const histo) {
  753. const uint16x8_t max_coeff_thresh = vdupq_n_u16(MAX_COEFF_THRESH);
  754. int j;
  755. for (j = start_block; j < end_block; ++j) {
  756. int16_t out[16];
  757. FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
  758. {
  759. int k;
  760. const int16x8_t a0 = vld1q_s16(out + 0);
  761. const int16x8_t b0 = vld1q_s16(out + 8);
  762. const uint16x8_t a1 = vreinterpretq_u16_s16(vabsq_s16(a0));
  763. const uint16x8_t b1 = vreinterpretq_u16_s16(vabsq_s16(b0));
  764. const uint16x8_t a2 = vshrq_n_u16(a1, 3);
  765. const uint16x8_t b2 = vshrq_n_u16(b1, 3);
  766. const uint16x8_t a3 = vminq_u16(a2, max_coeff_thresh);
  767. const uint16x8_t b3 = vminq_u16(b2, max_coeff_thresh);
  768. vst1q_s16(out + 0, vreinterpretq_s16_u16(a3));
  769. vst1q_s16(out + 8, vreinterpretq_s16_u16(b3));
  770. // Convert coefficients to bin.
  771. for (k = 0; k < 16; ++k) {
  772. histo->distribution[out[k]]++;
  773. }
  774. }
  775. }
  776. }
  777. //------------------------------------------------------------------------------
  778. static WEBP_INLINE void AccumulateSSE16(const uint8_t* const a,
  779. const uint8_t* const b,
  780. uint32x4_t* const sum) {
  781. const uint8x16_t a0 = vld1q_u8(a);
  782. const uint8x16_t b0 = vld1q_u8(b);
  783. const uint8x16_t abs_diff = vabdq_u8(a0, b0);
  784. uint16x8_t prod = vmull_u8(vget_low_u8(abs_diff), vget_low_u8(abs_diff));
  785. prod = vmlal_u8(prod, vget_high_u8(abs_diff), vget_high_u8(abs_diff));
  786. *sum = vpadalq_u16(*sum, prod); // pair-wise add and accumulate
  787. }
  788. // Horizontal sum of all four uint32_t values in 'sum'.
  789. static int SumToInt(uint32x4_t sum) {
  790. const uint64x2_t sum2 = vpaddlq_u32(sum);
  791. const uint64_t sum3 = vgetq_lane_u64(sum2, 0) + vgetq_lane_u64(sum2, 1);
  792. return (int)sum3;
  793. }
  794. static int SSE16x16(const uint8_t* a, const uint8_t* b) {
  795. uint32x4_t sum = vdupq_n_u32(0);
  796. int y;
  797. for (y = 0; y < 16; ++y) {
  798. AccumulateSSE16(a + y * BPS, b + y * BPS, &sum);
  799. }
  800. return SumToInt(sum);
  801. }
  802. static int SSE16x8(const uint8_t* a, const uint8_t* b) {
  803. uint32x4_t sum = vdupq_n_u32(0);
  804. int y;
  805. for (y = 0; y < 8; ++y) {
  806. AccumulateSSE16(a + y * BPS, b + y * BPS, &sum);
  807. }
  808. return SumToInt(sum);
  809. }
  810. static int SSE8x8(const uint8_t* a, const uint8_t* b) {
  811. uint32x4_t sum = vdupq_n_u32(0);
  812. int y;
  813. for (y = 0; y < 8; ++y) {
  814. const uint8x8_t a0 = vld1_u8(a + y * BPS);
  815. const uint8x8_t b0 = vld1_u8(b + y * BPS);
  816. const uint8x8_t abs_diff = vabd_u8(a0, b0);
  817. const uint16x8_t prod = vmull_u8(abs_diff, abs_diff);
  818. sum = vpadalq_u16(sum, prod);
  819. }
  820. return SumToInt(sum);
  821. }
  822. static int SSE4x4(const uint8_t* a, const uint8_t* b) {
  823. const uint8x16_t a0 = Load4x4(a);
  824. const uint8x16_t b0 = Load4x4(b);
  825. const uint8x16_t abs_diff = vabdq_u8(a0, b0);
  826. uint16x8_t prod = vmull_u8(vget_low_u8(abs_diff), vget_low_u8(abs_diff));
  827. prod = vmlal_u8(prod, vget_high_u8(abs_diff), vget_high_u8(abs_diff));
  828. return SumToInt(vpaddlq_u16(prod));
  829. }
  830. //------------------------------------------------------------------------------
  831. // Compilation with gcc-4.6.x is problematic for now.
  832. #if !defined(WORK_AROUND_GCC)
  833. static int16x8_t Quantize(int16_t* const in,
  834. const VP8Matrix* const mtx, int offset) {
  835. const uint16x8_t sharp = vld1q_u16(&mtx->sharpen_[offset]);
  836. const uint16x8_t q = vld1q_u16(&mtx->q_[offset]);
  837. const uint16x8_t iq = vld1q_u16(&mtx->iq_[offset]);
  838. const uint32x4_t bias0 = vld1q_u32(&mtx->bias_[offset + 0]);
  839. const uint32x4_t bias1 = vld1q_u32(&mtx->bias_[offset + 4]);
  840. const int16x8_t a = vld1q_s16(in + offset); // in
  841. const uint16x8_t b = vreinterpretq_u16_s16(vabsq_s16(a)); // coeff = abs(in)
  842. const int16x8_t sign = vshrq_n_s16(a, 15); // sign
  843. const uint16x8_t c = vaddq_u16(b, sharp); // + sharpen
  844. const uint32x4_t m0 = vmull_u16(vget_low_u16(c), vget_low_u16(iq));
  845. const uint32x4_t m1 = vmull_u16(vget_high_u16(c), vget_high_u16(iq));
  846. const uint32x4_t m2 = vhaddq_u32(m0, bias0);
  847. const uint32x4_t m3 = vhaddq_u32(m1, bias1); // (coeff * iQ + bias) >> 1
  848. const uint16x8_t c0 = vcombine_u16(vshrn_n_u32(m2, 16),
  849. vshrn_n_u32(m3, 16)); // QFIX=17 = 16+1
  850. const uint16x8_t c1 = vminq_u16(c0, vdupq_n_u16(MAX_LEVEL));
  851. const int16x8_t c2 = veorq_s16(vreinterpretq_s16_u16(c1), sign);
  852. const int16x8_t c3 = vsubq_s16(c2, sign); // restore sign
  853. const int16x8_t c4 = vmulq_s16(c3, vreinterpretq_s16_u16(q));
  854. vst1q_s16(in + offset, c4);
  855. assert(QFIX == 17); // this function can't work as is if QFIX != 16+1
  856. return c3;
  857. }
  858. static const uint8_t kShuffles[4][8] = {
  859. { 0, 1, 2, 3, 8, 9, 16, 17 },
  860. { 10, 11, 4, 5, 6, 7, 12, 13 },
  861. { 18, 19, 24, 25, 26, 27, 20, 21 },
  862. { 14, 15, 22, 23, 28, 29, 30, 31 }
  863. };
  864. static int QuantizeBlock(int16_t in[16], int16_t out[16],
  865. const VP8Matrix* const mtx) {
  866. const int16x8_t out0 = Quantize(in, mtx, 0);
  867. const int16x8_t out1 = Quantize(in, mtx, 8);
  868. uint8x8x4_t shuffles;
  869. // vtbl4_u8 is marked unavailable for iOS arm64, use wider versions there.
  870. #if defined(__APPLE__) && defined(__aarch64__)
  871. uint8x16x2_t all_out;
  872. INIT_VECTOR2(all_out, vreinterpretq_u8_s16(out0), vreinterpretq_u8_s16(out1));
  873. INIT_VECTOR4(shuffles,
  874. vtbl2q_u8(all_out, vld1_u8(kShuffles[0])),
  875. vtbl2q_u8(all_out, vld1_u8(kShuffles[1])),
  876. vtbl2q_u8(all_out, vld1_u8(kShuffles[2])),
  877. vtbl2q_u8(all_out, vld1_u8(kShuffles[3])));
  878. #else
  879. uint8x8x4_t all_out;
  880. INIT_VECTOR4(all_out,
  881. vreinterpret_u8_s16(vget_low_s16(out0)),
  882. vreinterpret_u8_s16(vget_high_s16(out0)),
  883. vreinterpret_u8_s16(vget_low_s16(out1)),
  884. vreinterpret_u8_s16(vget_high_s16(out1)));
  885. INIT_VECTOR4(shuffles,
  886. vtbl4_u8(all_out, vld1_u8(kShuffles[0])),
  887. vtbl4_u8(all_out, vld1_u8(kShuffles[1])),
  888. vtbl4_u8(all_out, vld1_u8(kShuffles[2])),
  889. vtbl4_u8(all_out, vld1_u8(kShuffles[3])));
  890. #endif
  891. // Zigzag reordering
  892. vst1_u8((uint8_t*)(out + 0), shuffles.val[0]);
  893. vst1_u8((uint8_t*)(out + 4), shuffles.val[1]);
  894. vst1_u8((uint8_t*)(out + 8), shuffles.val[2]);
  895. vst1_u8((uint8_t*)(out + 12), shuffles.val[3]);
  896. // test zeros
  897. if (*(uint64_t*)(out + 0) != 0) return 1;
  898. if (*(uint64_t*)(out + 4) != 0) return 1;
  899. if (*(uint64_t*)(out + 8) != 0) return 1;
  900. if (*(uint64_t*)(out + 12) != 0) return 1;
  901. return 0;
  902. }
  903. #endif // !WORK_AROUND_GCC
  904. #endif // WEBP_USE_NEON
  905. //------------------------------------------------------------------------------
  906. // Entry point
  907. extern void VP8EncDspInitNEON(void);
  908. void VP8EncDspInitNEON(void) {
  909. #if defined(WEBP_USE_NEON)
  910. VP8ITransform = ITransform;
  911. VP8FTransform = FTransform;
  912. VP8FTransformWHT = FTransformWHT;
  913. VP8TDisto4x4 = Disto4x4;
  914. VP8TDisto16x16 = Disto16x16;
  915. VP8CollectHistogram = CollectHistogram;
  916. VP8SSE16x16 = SSE16x16;
  917. VP8SSE16x8 = SSE16x8;
  918. VP8SSE8x8 = SSE8x8;
  919. VP8SSE4x4 = SSE4x4;
  920. #if !defined(WORK_AROUND_GCC)
  921. VP8EncQuantizeBlock = QuantizeBlock;
  922. #endif
  923. #endif // WEBP_USE_NEON
  924. }