enc_neon.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 (https://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/vp8i_enc.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. // WEBP_USE_INTRINSICS define.
  28. // With gcc-4.8, it's a little faster speed than inlined-assembly.
  29. #if defined(WEBP_USE_INTRINSICS)
  30. // Treats 'v' as an uint8x8_t and zero extends to an int16x8_t.
  31. static WEBP_INLINE int16x8_t ConvertU8ToS16_NEON(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_NEON(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_NEON(const int16x8_t row01,
  49. const int16x8_t row23,
  50. const uint8_t* const ref,
  51. uint8_t* const dst) {
  52. uint32x2_t dst01 = vdup_n_u32(0);
  53. uint32x2_t dst23 = vdup_n_u32(0);
  54. // Load the source pixels.
  55. dst01 = vld1_lane_u32((uint32_t*)(ref + 0 * BPS), dst01, 0);
  56. dst23 = vld1_lane_u32((uint32_t*)(ref + 2 * BPS), dst23, 0);
  57. dst01 = vld1_lane_u32((uint32_t*)(ref + 1 * BPS), dst01, 1);
  58. dst23 = vld1_lane_u32((uint32_t*)(ref + 3 * BPS), dst23, 1);
  59. {
  60. // Convert to 16b.
  61. const int16x8_t dst01_s16 = ConvertU8ToS16_NEON(dst01);
  62. const int16x8_t dst23_s16 = ConvertU8ToS16_NEON(dst23);
  63. // Descale with rounding.
  64. const int16x8_t out01 = vrsraq_n_s16(dst01_s16, row01, 3);
  65. const int16x8_t out23 = vrsraq_n_s16(dst23_s16, row23, 3);
  66. // Add the inverse transform.
  67. SaturateAndStore4x4_NEON(dst, out01, out23);
  68. }
  69. }
  70. static WEBP_INLINE void Transpose8x2_NEON(const int16x8_t in0,
  71. const int16x8_t in1,
  72. int16x8x2_t* const out) {
  73. // a0 a1 a2 a3 | b0 b1 b2 b3 => a0 b0 c0 d0 | a1 b1 c1 d1
  74. // c0 c1 c2 c3 | d0 d1 d2 d3 a2 b2 c2 d2 | a3 b3 c3 d3
  75. const int16x8x2_t tmp0 = vzipq_s16(in0, in1); // a0 c0 a1 c1 a2 c2 ...
  76. // b0 d0 b1 d1 b2 d2 ...
  77. *out = vzipq_s16(tmp0.val[0], tmp0.val[1]);
  78. }
  79. static WEBP_INLINE void TransformPass_NEON(int16x8x2_t* const rows) {
  80. // {rows} = in0 | in4
  81. // in8 | in12
  82. // B1 = in4 | in12
  83. const int16x8_t B1 =
  84. vcombine_s16(vget_high_s16(rows->val[0]), vget_high_s16(rows->val[1]));
  85. // C0 = kC1 * in4 | kC1 * in12
  86. // C1 = kC2 * in4 | kC2 * in12
  87. const int16x8_t C0 = vsraq_n_s16(B1, vqdmulhq_n_s16(B1, kC1), 1);
  88. const int16x8_t C1 = vqdmulhq_n_s16(B1, kC2);
  89. const int16x4_t a = vqadd_s16(vget_low_s16(rows->val[0]),
  90. vget_low_s16(rows->val[1])); // in0 + in8
  91. const int16x4_t b = vqsub_s16(vget_low_s16(rows->val[0]),
  92. vget_low_s16(rows->val[1])); // in0 - in8
  93. // c = kC2 * in4 - kC1 * in12
  94. // d = kC1 * in4 + kC2 * in12
  95. const int16x4_t c = vqsub_s16(vget_low_s16(C1), vget_high_s16(C0));
  96. const int16x4_t d = vqadd_s16(vget_low_s16(C0), vget_high_s16(C1));
  97. const int16x8_t D0 = vcombine_s16(a, b); // D0 = a | b
  98. const int16x8_t D1 = vcombine_s16(d, c); // D1 = d | c
  99. const int16x8_t E0 = vqaddq_s16(D0, D1); // a+d | b+c
  100. const int16x8_t E_tmp = vqsubq_s16(D0, D1); // a-d | b-c
  101. const int16x8_t E1 = vcombine_s16(vget_high_s16(E_tmp), vget_low_s16(E_tmp));
  102. Transpose8x2_NEON(E0, E1, rows);
  103. }
  104. static void ITransformOne_NEON(const uint8_t* ref,
  105. const int16_t* in, uint8_t* dst) {
  106. int16x8x2_t rows;
  107. INIT_VECTOR2(rows, vld1q_s16(in + 0), vld1q_s16(in + 8));
  108. TransformPass_NEON(&rows);
  109. TransformPass_NEON(&rows);
  110. Add4x4_NEON(rows.val[0], rows.val[1], ref, dst);
  111. }
  112. #else
  113. static void ITransformOne_NEON(const uint8_t* ref,
  114. const int16_t* in, uint8_t* dst) {
  115. const int kBPS = BPS;
  116. const int16_t kC1C2[] = { kC1, kC2, 0, 0 };
  117. __asm__ volatile (
  118. "vld1.16 {q1, q2}, [%[in]] \n"
  119. "vld1.16 {d0}, [%[kC1C2]] \n"
  120. // d2: in[0]
  121. // d3: in[8]
  122. // d4: in[4]
  123. // d5: in[12]
  124. "vswp d3, d4 \n"
  125. // q8 = {in[4], in[12]} * kC1 * 2 >> 16
  126. // q9 = {in[4], in[12]} * kC2 >> 16
  127. "vqdmulh.s16 q8, q2, d0[0] \n"
  128. "vqdmulh.s16 q9, q2, d0[1] \n"
  129. // d22 = a = in[0] + in[8]
  130. // d23 = b = in[0] - in[8]
  131. "vqadd.s16 d22, d2, d3 \n"
  132. "vqsub.s16 d23, d2, d3 \n"
  133. // q8 = in[4]/[12] * kC1 >> 16
  134. "vshr.s16 q8, q8, #1 \n"
  135. // Add {in[4], in[12]} back after the multiplication.
  136. "vqadd.s16 q8, q2, q8 \n"
  137. // d20 = c = in[4]*kC2 - in[12]*kC1
  138. // d21 = d = in[4]*kC1 + in[12]*kC2
  139. "vqsub.s16 d20, d18, d17 \n"
  140. "vqadd.s16 d21, d19, d16 \n"
  141. // d2 = tmp[0] = a + d
  142. // d3 = tmp[1] = b + c
  143. // d4 = tmp[2] = b - c
  144. // d5 = tmp[3] = a - d
  145. "vqadd.s16 d2, d22, d21 \n"
  146. "vqadd.s16 d3, d23, d20 \n"
  147. "vqsub.s16 d4, d23, d20 \n"
  148. "vqsub.s16 d5, d22, d21 \n"
  149. "vzip.16 q1, q2 \n"
  150. "vzip.16 q1, q2 \n"
  151. "vswp d3, d4 \n"
  152. // q8 = {tmp[4], tmp[12]} * kC1 * 2 >> 16
  153. // q9 = {tmp[4], tmp[12]} * kC2 >> 16
  154. "vqdmulh.s16 q8, q2, d0[0] \n"
  155. "vqdmulh.s16 q9, q2, d0[1] \n"
  156. // d22 = a = tmp[0] + tmp[8]
  157. // d23 = b = tmp[0] - tmp[8]
  158. "vqadd.s16 d22, d2, d3 \n"
  159. "vqsub.s16 d23, d2, d3 \n"
  160. "vshr.s16 q8, q8, #1 \n"
  161. "vqadd.s16 q8, q2, q8 \n"
  162. // d20 = c = in[4]*kC2 - in[12]*kC1
  163. // d21 = d = in[4]*kC1 + in[12]*kC2
  164. "vqsub.s16 d20, d18, d17 \n"
  165. "vqadd.s16 d21, d19, d16 \n"
  166. // d2 = tmp[0] = a + d
  167. // d3 = tmp[1] = b + c
  168. // d4 = tmp[2] = b - c
  169. // d5 = tmp[3] = a - d
  170. "vqadd.s16 d2, d22, d21 \n"
  171. "vqadd.s16 d3, d23, d20 \n"
  172. "vqsub.s16 d4, d23, d20 \n"
  173. "vqsub.s16 d5, d22, d21 \n"
  174. "vld1.32 d6[0], [%[ref]], %[kBPS] \n"
  175. "vld1.32 d6[1], [%[ref]], %[kBPS] \n"
  176. "vld1.32 d7[0], [%[ref]], %[kBPS] \n"
  177. "vld1.32 d7[1], [%[ref]], %[kBPS] \n"
  178. "sub %[ref], %[ref], %[kBPS], lsl #2 \n"
  179. // (val) + 4 >> 3
  180. "vrshr.s16 d2, d2, #3 \n"
  181. "vrshr.s16 d3, d3, #3 \n"
  182. "vrshr.s16 d4, d4, #3 \n"
  183. "vrshr.s16 d5, d5, #3 \n"
  184. "vzip.16 q1, q2 \n"
  185. "vzip.16 q1, q2 \n"
  186. // Must accumulate before saturating
  187. "vmovl.u8 q8, d6 \n"
  188. "vmovl.u8 q9, d7 \n"
  189. "vqadd.s16 q1, q1, q8 \n"
  190. "vqadd.s16 q2, q2, q9 \n"
  191. "vqmovun.s16 d0, q1 \n"
  192. "vqmovun.s16 d1, q2 \n"
  193. "vst1.32 d0[0], [%[dst]], %[kBPS] \n"
  194. "vst1.32 d0[1], [%[dst]], %[kBPS] \n"
  195. "vst1.32 d1[0], [%[dst]], %[kBPS] \n"
  196. "vst1.32 d1[1], [%[dst]] \n"
  197. : [in] "+r"(in), [dst] "+r"(dst) // modified registers
  198. : [kBPS] "r"(kBPS), [kC1C2] "r"(kC1C2), [ref] "r"(ref) // constants
  199. : "memory", "q0", "q1", "q2", "q8", "q9", "q10", "q11" // clobbered
  200. );
  201. }
  202. #endif // WEBP_USE_INTRINSICS
  203. static void ITransform_NEON(const uint8_t* ref,
  204. const int16_t* in, uint8_t* dst, int do_two) {
  205. ITransformOne_NEON(ref, in, dst);
  206. if (do_two) {
  207. ITransformOne_NEON(ref + 4, in + 16, dst + 4);
  208. }
  209. }
  210. // Load all 4x4 pixels into a single uint8x16_t variable.
  211. static uint8x16_t Load4x4_NEON(const uint8_t* src) {
  212. uint32x4_t out = vdupq_n_u32(0);
  213. out = vld1q_lane_u32((const uint32_t*)(src + 0 * BPS), out, 0);
  214. out = vld1q_lane_u32((const uint32_t*)(src + 1 * BPS), out, 1);
  215. out = vld1q_lane_u32((const uint32_t*)(src + 2 * BPS), out, 2);
  216. out = vld1q_lane_u32((const uint32_t*)(src + 3 * BPS), out, 3);
  217. return vreinterpretq_u8_u32(out);
  218. }
  219. // Forward transform.
  220. #if defined(WEBP_USE_INTRINSICS)
  221. static WEBP_INLINE void Transpose4x4_S16_NEON(const int16x4_t A,
  222. const int16x4_t B,
  223. const int16x4_t C,
  224. const int16x4_t D,
  225. int16x8_t* const out01,
  226. int16x8_t* const out32) {
  227. const int16x4x2_t AB = vtrn_s16(A, B);
  228. const int16x4x2_t CD = vtrn_s16(C, D);
  229. const int32x2x2_t tmp02 = vtrn_s32(vreinterpret_s32_s16(AB.val[0]),
  230. vreinterpret_s32_s16(CD.val[0]));
  231. const int32x2x2_t tmp13 = vtrn_s32(vreinterpret_s32_s16(AB.val[1]),
  232. vreinterpret_s32_s16(CD.val[1]));
  233. *out01 = vreinterpretq_s16_s64(
  234. vcombine_s64(vreinterpret_s64_s32(tmp02.val[0]),
  235. vreinterpret_s64_s32(tmp13.val[0])));
  236. *out32 = vreinterpretq_s16_s64(
  237. vcombine_s64(vreinterpret_s64_s32(tmp13.val[1]),
  238. vreinterpret_s64_s32(tmp02.val[1])));
  239. }
  240. static WEBP_INLINE int16x8_t DiffU8ToS16_NEON(const uint8x8_t a,
  241. const uint8x8_t b) {
  242. return vreinterpretq_s16_u16(vsubl_u8(a, b));
  243. }
  244. static void FTransform_NEON(const uint8_t* src, const uint8_t* ref,
  245. int16_t* out) {
  246. int16x8_t d0d1, d3d2; // working 4x4 int16 variables
  247. {
  248. const uint8x16_t S0 = Load4x4_NEON(src);
  249. const uint8x16_t R0 = Load4x4_NEON(ref);
  250. const int16x8_t D0D1 = DiffU8ToS16_NEON(vget_low_u8(S0), vget_low_u8(R0));
  251. const int16x8_t D2D3 = DiffU8ToS16_NEON(vget_high_u8(S0), vget_high_u8(R0));
  252. const int16x4_t D0 = vget_low_s16(D0D1);
  253. const int16x4_t D1 = vget_high_s16(D0D1);
  254. const int16x4_t D2 = vget_low_s16(D2D3);
  255. const int16x4_t D3 = vget_high_s16(D2D3);
  256. Transpose4x4_S16_NEON(D0, D1, D2, D3, &d0d1, &d3d2);
  257. }
  258. { // 1rst pass
  259. const int32x4_t kCst937 = vdupq_n_s32(937);
  260. const int32x4_t kCst1812 = vdupq_n_s32(1812);
  261. const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)
  262. const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)
  263. const int16x8_t a0a1_2 = vshlq_n_s16(a0a1, 3);
  264. const int16x4_t tmp0 = vadd_s16(vget_low_s16(a0a1_2),
  265. vget_high_s16(a0a1_2));
  266. const int16x4_t tmp2 = vsub_s16(vget_low_s16(a0a1_2),
  267. vget_high_s16(a0a1_2));
  268. const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);
  269. const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);
  270. const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);
  271. const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);
  272. const int16x4_t tmp1 = vshrn_n_s32(vaddq_s32(a2_p_a3, kCst1812), 9);
  273. const int16x4_t tmp3 = vshrn_n_s32(vaddq_s32(a3_m_a2, kCst937), 9);
  274. Transpose4x4_S16_NEON(tmp0, tmp1, tmp2, tmp3, &d0d1, &d3d2);
  275. }
  276. { // 2nd pass
  277. // the (1<<16) addition is for the replacement: a3!=0 <-> 1-(a3==0)
  278. const int32x4_t kCst12000 = vdupq_n_s32(12000 + (1 << 16));
  279. const int32x4_t kCst51000 = vdupq_n_s32(51000);
  280. const int16x8_t a0a1 = vaddq_s16(d0d1, d3d2); // d0+d3 | d1+d2 (=a0|a1)
  281. const int16x8_t a3a2 = vsubq_s16(d0d1, d3d2); // d0-d3 | d1-d2 (=a3|a2)
  282. const int16x4_t a0_k7 = vadd_s16(vget_low_s16(a0a1), vdup_n_s16(7));
  283. const int16x4_t out0 = vshr_n_s16(vadd_s16(a0_k7, vget_high_s16(a0a1)), 4);
  284. const int16x4_t out2 = vshr_n_s16(vsub_s16(a0_k7, vget_high_s16(a0a1)), 4);
  285. const int32x4_t a3_2217 = vmull_n_s16(vget_low_s16(a3a2), 2217);
  286. const int32x4_t a2_2217 = vmull_n_s16(vget_high_s16(a3a2), 2217);
  287. const int32x4_t a2_p_a3 = vmlal_n_s16(a2_2217, vget_low_s16(a3a2), 5352);
  288. const int32x4_t a3_m_a2 = vmlsl_n_s16(a3_2217, vget_high_s16(a3a2), 5352);
  289. const int16x4_t tmp1 = vaddhn_s32(a2_p_a3, kCst12000);
  290. const int16x4_t out3 = vaddhn_s32(a3_m_a2, kCst51000);
  291. const int16x4_t a3_eq_0 =
  292. vreinterpret_s16_u16(vceq_s16(vget_low_s16(a3a2), vdup_n_s16(0)));
  293. const int16x4_t out1 = vadd_s16(tmp1, a3_eq_0);
  294. vst1_s16(out + 0, out0);
  295. vst1_s16(out + 4, out1);
  296. vst1_s16(out + 8, out2);
  297. vst1_s16(out + 12, out3);
  298. }
  299. }
  300. #else
  301. // adapted from vp8/encoder/arm/neon/shortfdct_neon.asm
  302. static const int16_t kCoeff16[] = {
  303. 5352, 5352, 5352, 5352, 2217, 2217, 2217, 2217
  304. };
  305. static const int32_t kCoeff32[] = {
  306. 1812, 1812, 1812, 1812,
  307. 937, 937, 937, 937,
  308. 12000, 12000, 12000, 12000,
  309. 51000, 51000, 51000, 51000
  310. };
  311. static void FTransform_NEON(const uint8_t* src, const uint8_t* ref,
  312. int16_t* out) {
  313. const int kBPS = BPS;
  314. const uint8_t* src_ptr = src;
  315. const uint8_t* ref_ptr = ref;
  316. const int16_t* coeff16 = kCoeff16;
  317. const int32_t* coeff32 = kCoeff32;
  318. __asm__ volatile (
  319. // load src into q4, q5 in high half
  320. "vld1.8 {d8}, [%[src_ptr]], %[kBPS] \n"
  321. "vld1.8 {d10}, [%[src_ptr]], %[kBPS] \n"
  322. "vld1.8 {d9}, [%[src_ptr]], %[kBPS] \n"
  323. "vld1.8 {d11}, [%[src_ptr]] \n"
  324. // load ref into q6, q7 in high half
  325. "vld1.8 {d12}, [%[ref_ptr]], %[kBPS] \n"
  326. "vld1.8 {d14}, [%[ref_ptr]], %[kBPS] \n"
  327. "vld1.8 {d13}, [%[ref_ptr]], %[kBPS] \n"
  328. "vld1.8 {d15}, [%[ref_ptr]] \n"
  329. // Pack the high values in to q4 and q6
  330. "vtrn.32 q4, q5 \n"
  331. "vtrn.32 q6, q7 \n"
  332. // d[0-3] = src - ref
  333. "vsubl.u8 q0, d8, d12 \n"
  334. "vsubl.u8 q1, d9, d13 \n"
  335. // load coeff16 into q8(d16=5352, d17=2217)
  336. "vld1.16 {q8}, [%[coeff16]] \n"
  337. // load coeff32 high half into q9 = 1812, q10 = 937
  338. "vld1.32 {q9, q10}, [%[coeff32]]! \n"
  339. // load coeff32 low half into q11=12000, q12=51000
  340. "vld1.32 {q11,q12}, [%[coeff32]] \n"
  341. // part 1
  342. // Transpose. Register dN is the same as dN in C
  343. "vtrn.32 d0, d2 \n"
  344. "vtrn.32 d1, d3 \n"
  345. "vtrn.16 d0, d1 \n"
  346. "vtrn.16 d2, d3 \n"
  347. "vadd.s16 d4, d0, d3 \n" // a0 = d0 + d3
  348. "vadd.s16 d5, d1, d2 \n" // a1 = d1 + d2
  349. "vsub.s16 d6, d1, d2 \n" // a2 = d1 - d2
  350. "vsub.s16 d7, d0, d3 \n" // a3 = d0 - d3
  351. "vadd.s16 d0, d4, d5 \n" // a0 + a1
  352. "vshl.s16 d0, d0, #3 \n" // temp[0+i*4] = (a0+a1) << 3
  353. "vsub.s16 d2, d4, d5 \n" // a0 - a1
  354. "vshl.s16 d2, d2, #3 \n" // (temp[2+i*4] = (a0-a1) << 3
  355. "vmlal.s16 q9, d7, d16 \n" // a3*5352 + 1812
  356. "vmlal.s16 q10, d7, d17 \n" // a3*2217 + 937
  357. "vmlal.s16 q9, d6, d17 \n" // a2*2217 + a3*5352 + 1812
  358. "vmlsl.s16 q10, d6, d16 \n" // a3*2217 + 937 - a2*5352
  359. // temp[1+i*4] = (d2*2217 + d3*5352 + 1812) >> 9
  360. // temp[3+i*4] = (d3*2217 + 937 - d2*5352) >> 9
  361. "vshrn.s32 d1, q9, #9 \n"
  362. "vshrn.s32 d3, q10, #9 \n"
  363. // part 2
  364. // transpose d0=ip[0], d1=ip[4], d2=ip[8], d3=ip[12]
  365. "vtrn.32 d0, d2 \n"
  366. "vtrn.32 d1, d3 \n"
  367. "vtrn.16 d0, d1 \n"
  368. "vtrn.16 d2, d3 \n"
  369. "vmov.s16 d26, #7 \n"
  370. "vadd.s16 d4, d0, d3 \n" // a1 = ip[0] + ip[12]
  371. "vadd.s16 d5, d1, d2 \n" // b1 = ip[4] + ip[8]
  372. "vsub.s16 d6, d1, d2 \n" // c1 = ip[4] - ip[8]
  373. "vadd.s16 d4, d4, d26 \n" // a1 + 7
  374. "vsub.s16 d7, d0, d3 \n" // d1 = ip[0] - ip[12]
  375. "vadd.s16 d0, d4, d5 \n" // op[0] = a1 + b1 + 7
  376. "vsub.s16 d2, d4, d5 \n" // op[8] = a1 - b1 + 7
  377. "vmlal.s16 q11, d7, d16 \n" // d1*5352 + 12000
  378. "vmlal.s16 q12, d7, d17 \n" // d1*2217 + 51000
  379. "vceq.s16 d4, d7, #0 \n"
  380. "vshr.s16 d0, d0, #4 \n"
  381. "vshr.s16 d2, d2, #4 \n"
  382. "vmlal.s16 q11, d6, d17 \n" // c1*2217 + d1*5352 + 12000
  383. "vmlsl.s16 q12, d6, d16 \n" // d1*2217 - c1*5352 + 51000
  384. "vmvn d4, d4 \n" // !(d1 == 0)
  385. // op[4] = (c1*2217 + d1*5352 + 12000)>>16
  386. "vshrn.s32 d1, q11, #16 \n"
  387. // op[4] += (d1!=0)
  388. "vsub.s16 d1, d1, d4 \n"
  389. // op[12]= (d1*2217 - c1*5352 + 51000)>>16
  390. "vshrn.s32 d3, q12, #16 \n"
  391. // set result to out array
  392. "vst1.16 {q0, q1}, [%[out]] \n"
  393. : [src_ptr] "+r"(src_ptr), [ref_ptr] "+r"(ref_ptr),
  394. [coeff32] "+r"(coeff32) // modified registers
  395. : [kBPS] "r"(kBPS), [coeff16] "r"(coeff16),
  396. [out] "r"(out) // constants
  397. : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
  398. "q10", "q11", "q12", "q13" // clobbered
  399. );
  400. }
  401. #endif
  402. #define LOAD_LANE_16b(VALUE, LANE) do { \
  403. (VALUE) = vld1_lane_s16(src, (VALUE), (LANE)); \
  404. src += stride; \
  405. } while (0)
  406. static void FTransformWHT_NEON(const int16_t* src, int16_t* out) {
  407. const int stride = 16;
  408. const int16x4_t zero = vdup_n_s16(0);
  409. int32x4x4_t tmp0;
  410. int16x4x4_t in;
  411. INIT_VECTOR4(in, zero, zero, zero, zero);
  412. LOAD_LANE_16b(in.val[0], 0);
  413. LOAD_LANE_16b(in.val[1], 0);
  414. LOAD_LANE_16b(in.val[2], 0);
  415. LOAD_LANE_16b(in.val[3], 0);
  416. LOAD_LANE_16b(in.val[0], 1);
  417. LOAD_LANE_16b(in.val[1], 1);
  418. LOAD_LANE_16b(in.val[2], 1);
  419. LOAD_LANE_16b(in.val[3], 1);
  420. LOAD_LANE_16b(in.val[0], 2);
  421. LOAD_LANE_16b(in.val[1], 2);
  422. LOAD_LANE_16b(in.val[2], 2);
  423. LOAD_LANE_16b(in.val[3], 2);
  424. LOAD_LANE_16b(in.val[0], 3);
  425. LOAD_LANE_16b(in.val[1], 3);
  426. LOAD_LANE_16b(in.val[2], 3);
  427. LOAD_LANE_16b(in.val[3], 3);
  428. {
  429. // a0 = in[0 * 16] + in[2 * 16]
  430. // a1 = in[1 * 16] + in[3 * 16]
  431. // a2 = in[1 * 16] - in[3 * 16]
  432. // a3 = in[0 * 16] - in[2 * 16]
  433. const int32x4_t a0 = vaddl_s16(in.val[0], in.val[2]);
  434. const int32x4_t a1 = vaddl_s16(in.val[1], in.val[3]);
  435. const int32x4_t a2 = vsubl_s16(in.val[1], in.val[3]);
  436. const int32x4_t a3 = vsubl_s16(in.val[0], in.val[2]);
  437. tmp0.val[0] = vaddq_s32(a0, a1);
  438. tmp0.val[1] = vaddq_s32(a3, a2);
  439. tmp0.val[2] = vsubq_s32(a3, a2);
  440. tmp0.val[3] = vsubq_s32(a0, a1);
  441. }
  442. {
  443. const int32x4x4_t tmp1 = Transpose4x4_NEON(tmp0);
  444. // a0 = tmp[0 + i] + tmp[ 8 + i]
  445. // a1 = tmp[4 + i] + tmp[12 + i]
  446. // a2 = tmp[4 + i] - tmp[12 + i]
  447. // a3 = tmp[0 + i] - tmp[ 8 + i]
  448. const int32x4_t a0 = vaddq_s32(tmp1.val[0], tmp1.val[2]);
  449. const int32x4_t a1 = vaddq_s32(tmp1.val[1], tmp1.val[3]);
  450. const int32x4_t a2 = vsubq_s32(tmp1.val[1], tmp1.val[3]);
  451. const int32x4_t a3 = vsubq_s32(tmp1.val[0], tmp1.val[2]);
  452. const int32x4_t b0 = vhaddq_s32(a0, a1); // (a0 + a1) >> 1
  453. const int32x4_t b1 = vhaddq_s32(a3, a2); // (a3 + a2) >> 1
  454. const int32x4_t b2 = vhsubq_s32(a3, a2); // (a3 - a2) >> 1
  455. const int32x4_t b3 = vhsubq_s32(a0, a1); // (a0 - a1) >> 1
  456. const int16x4_t out0 = vmovn_s32(b0);
  457. const int16x4_t out1 = vmovn_s32(b1);
  458. const int16x4_t out2 = vmovn_s32(b2);
  459. const int16x4_t out3 = vmovn_s32(b3);
  460. vst1_s16(out + 0, out0);
  461. vst1_s16(out + 4, out1);
  462. vst1_s16(out + 8, out2);
  463. vst1_s16(out + 12, out3);
  464. }
  465. }
  466. #undef LOAD_LANE_16b
  467. //------------------------------------------------------------------------------
  468. // Texture distortion
  469. //
  470. // We try to match the spectral content (weighted) between source and
  471. // reconstructed samples.
  472. // a 0123, b 0123
  473. // a 4567, b 4567
  474. // a 89ab, b 89ab
  475. // a cdef, b cdef
  476. //
  477. // transpose
  478. //
  479. // a 048c, b 048c
  480. // a 159d, b 159d
  481. // a 26ae, b 26ae
  482. // a 37bf, b 37bf
  483. //
  484. static WEBP_INLINE int16x8x4_t DistoTranspose4x4S16_NEON(int16x8x4_t q4_in) {
  485. const int16x8x2_t q2_tmp0 = vtrnq_s16(q4_in.val[0], q4_in.val[1]);
  486. const int16x8x2_t q2_tmp1 = vtrnq_s16(q4_in.val[2], q4_in.val[3]);
  487. const int32x4x2_t q2_tmp2 = vtrnq_s32(vreinterpretq_s32_s16(q2_tmp0.val[0]),
  488. vreinterpretq_s32_s16(q2_tmp1.val[0]));
  489. const int32x4x2_t q2_tmp3 = vtrnq_s32(vreinterpretq_s32_s16(q2_tmp0.val[1]),
  490. vreinterpretq_s32_s16(q2_tmp1.val[1]));
  491. q4_in.val[0] = vreinterpretq_s16_s32(q2_tmp2.val[0]);
  492. q4_in.val[2] = vreinterpretq_s16_s32(q2_tmp2.val[1]);
  493. q4_in.val[1] = vreinterpretq_s16_s32(q2_tmp3.val[0]);
  494. q4_in.val[3] = vreinterpretq_s16_s32(q2_tmp3.val[1]);
  495. return q4_in;
  496. }
  497. static WEBP_INLINE int16x8x4_t DistoHorizontalPass_NEON(
  498. const int16x8x4_t q4_in) {
  499. // {a0, a1} = {in[0] + in[2], in[1] + in[3]}
  500. // {a3, a2} = {in[0] - in[2], in[1] - in[3]}
  501. const int16x8_t q_a0 = vaddq_s16(q4_in.val[0], q4_in.val[2]);
  502. const int16x8_t q_a1 = vaddq_s16(q4_in.val[1], q4_in.val[3]);
  503. const int16x8_t q_a3 = vsubq_s16(q4_in.val[0], q4_in.val[2]);
  504. const int16x8_t q_a2 = vsubq_s16(q4_in.val[1], q4_in.val[3]);
  505. int16x8x4_t q4_out;
  506. // tmp[0] = a0 + a1
  507. // tmp[1] = a3 + a2
  508. // tmp[2] = a3 - a2
  509. // tmp[3] = a0 - a1
  510. INIT_VECTOR4(q4_out,
  511. vabsq_s16(vaddq_s16(q_a0, q_a1)),
  512. vabsq_s16(vaddq_s16(q_a3, q_a2)),
  513. vabdq_s16(q_a3, q_a2), vabdq_s16(q_a0, q_a1));
  514. return q4_out;
  515. }
  516. static WEBP_INLINE int16x8x4_t DistoVerticalPass_NEON(const uint8x8x4_t q4_in) {
  517. const int16x8_t q_a0 = vreinterpretq_s16_u16(vaddl_u8(q4_in.val[0],
  518. q4_in.val[2]));
  519. const int16x8_t q_a1 = vreinterpretq_s16_u16(vaddl_u8(q4_in.val[1],
  520. q4_in.val[3]));
  521. const int16x8_t q_a2 = vreinterpretq_s16_u16(vsubl_u8(q4_in.val[1],
  522. q4_in.val[3]));
  523. const int16x8_t q_a3 = vreinterpretq_s16_u16(vsubl_u8(q4_in.val[0],
  524. q4_in.val[2]));
  525. int16x8x4_t q4_out;
  526. INIT_VECTOR4(q4_out,
  527. vaddq_s16(q_a0, q_a1), vaddq_s16(q_a3, q_a2),
  528. vsubq_s16(q_a3, q_a2), vsubq_s16(q_a0, q_a1));
  529. return q4_out;
  530. }
  531. static WEBP_INLINE int16x4x4_t DistoLoadW_NEON(const uint16_t* w) {
  532. const uint16x8_t q_w07 = vld1q_u16(&w[0]);
  533. const uint16x8_t q_w8f = vld1q_u16(&w[8]);
  534. int16x4x4_t d4_w;
  535. INIT_VECTOR4(d4_w,
  536. vget_low_s16(vreinterpretq_s16_u16(q_w07)),
  537. vget_high_s16(vreinterpretq_s16_u16(q_w07)),
  538. vget_low_s16(vreinterpretq_s16_u16(q_w8f)),
  539. vget_high_s16(vreinterpretq_s16_u16(q_w8f)));
  540. return d4_w;
  541. }
  542. static WEBP_INLINE int32x2_t DistoSum_NEON(const int16x8x4_t q4_in,
  543. const int16x4x4_t d4_w) {
  544. int32x2_t d_sum;
  545. // sum += w[ 0] * abs(b0);
  546. // sum += w[ 4] * abs(b1);
  547. // sum += w[ 8] * abs(b2);
  548. // sum += w[12] * abs(b3);
  549. int32x4_t q_sum0 = vmull_s16(d4_w.val[0], vget_low_s16(q4_in.val[0]));
  550. int32x4_t q_sum1 = vmull_s16(d4_w.val[1], vget_low_s16(q4_in.val[1]));
  551. int32x4_t q_sum2 = vmull_s16(d4_w.val[2], vget_low_s16(q4_in.val[2]));
  552. int32x4_t q_sum3 = vmull_s16(d4_w.val[3], vget_low_s16(q4_in.val[3]));
  553. q_sum0 = vmlsl_s16(q_sum0, d4_w.val[0], vget_high_s16(q4_in.val[0]));
  554. q_sum1 = vmlsl_s16(q_sum1, d4_w.val[1], vget_high_s16(q4_in.val[1]));
  555. q_sum2 = vmlsl_s16(q_sum2, d4_w.val[2], vget_high_s16(q4_in.val[2]));
  556. q_sum3 = vmlsl_s16(q_sum3, d4_w.val[3], vget_high_s16(q4_in.val[3]));
  557. q_sum0 = vaddq_s32(q_sum0, q_sum1);
  558. q_sum2 = vaddq_s32(q_sum2, q_sum3);
  559. q_sum2 = vaddq_s32(q_sum0, q_sum2);
  560. d_sum = vpadd_s32(vget_low_s32(q_sum2), vget_high_s32(q_sum2));
  561. d_sum = vpadd_s32(d_sum, d_sum);
  562. return d_sum;
  563. }
  564. #define LOAD_LANE_32b(src, VALUE, LANE) \
  565. (VALUE) = vld1_lane_u32((const uint32_t*)(src), (VALUE), (LANE))
  566. // Hadamard transform
  567. // Returns the weighted sum of the absolute value of transformed coefficients.
  568. // w[] contains a row-major 4 by 4 symmetric matrix.
  569. static int Disto4x4_NEON(const uint8_t* const a, const uint8_t* const b,
  570. const uint16_t* const w) {
  571. uint32x2_t d_in_ab_0123 = vdup_n_u32(0);
  572. uint32x2_t d_in_ab_4567 = vdup_n_u32(0);
  573. uint32x2_t d_in_ab_89ab = vdup_n_u32(0);
  574. uint32x2_t d_in_ab_cdef = vdup_n_u32(0);
  575. uint8x8x4_t d4_in;
  576. // load data a, b
  577. LOAD_LANE_32b(a + 0 * BPS, d_in_ab_0123, 0);
  578. LOAD_LANE_32b(a + 1 * BPS, d_in_ab_4567, 0);
  579. LOAD_LANE_32b(a + 2 * BPS, d_in_ab_89ab, 0);
  580. LOAD_LANE_32b(a + 3 * BPS, d_in_ab_cdef, 0);
  581. LOAD_LANE_32b(b + 0 * BPS, d_in_ab_0123, 1);
  582. LOAD_LANE_32b(b + 1 * BPS, d_in_ab_4567, 1);
  583. LOAD_LANE_32b(b + 2 * BPS, d_in_ab_89ab, 1);
  584. LOAD_LANE_32b(b + 3 * BPS, d_in_ab_cdef, 1);
  585. INIT_VECTOR4(d4_in,
  586. vreinterpret_u8_u32(d_in_ab_0123),
  587. vreinterpret_u8_u32(d_in_ab_4567),
  588. vreinterpret_u8_u32(d_in_ab_89ab),
  589. vreinterpret_u8_u32(d_in_ab_cdef));
  590. {
  591. // Vertical pass first to avoid a transpose (vertical and horizontal passes
  592. // are commutative because w/kWeightY is symmetric) and subsequent
  593. // transpose.
  594. const int16x8x4_t q4_v = DistoVerticalPass_NEON(d4_in);
  595. const int16x4x4_t d4_w = DistoLoadW_NEON(w);
  596. // horizontal pass
  597. const int16x8x4_t q4_t = DistoTranspose4x4S16_NEON(q4_v);
  598. const int16x8x4_t q4_h = DistoHorizontalPass_NEON(q4_t);
  599. int32x2_t d_sum = DistoSum_NEON(q4_h, d4_w);
  600. // abs(sum2 - sum1) >> 5
  601. d_sum = vabs_s32(d_sum);
  602. d_sum = vshr_n_s32(d_sum, 5);
  603. return vget_lane_s32(d_sum, 0);
  604. }
  605. }
  606. #undef LOAD_LANE_32b
  607. static int Disto16x16_NEON(const uint8_t* const a, const uint8_t* const b,
  608. const uint16_t* const w) {
  609. int D = 0;
  610. int x, y;
  611. for (y = 0; y < 16 * BPS; y += 4 * BPS) {
  612. for (x = 0; x < 16; x += 4) {
  613. D += Disto4x4_NEON(a + x + y, b + x + y, w);
  614. }
  615. }
  616. return D;
  617. }
  618. //------------------------------------------------------------------------------
  619. static void CollectHistogram_NEON(const uint8_t* ref, const uint8_t* pred,
  620. int start_block, int end_block,
  621. VP8Histogram* const histo) {
  622. const uint16x8_t max_coeff_thresh = vdupq_n_u16(MAX_COEFF_THRESH);
  623. int j;
  624. int distribution[MAX_COEFF_THRESH + 1] = { 0 };
  625. for (j = start_block; j < end_block; ++j) {
  626. int16_t out[16];
  627. FTransform_NEON(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
  628. {
  629. int k;
  630. const int16x8_t a0 = vld1q_s16(out + 0);
  631. const int16x8_t b0 = vld1q_s16(out + 8);
  632. const uint16x8_t a1 = vreinterpretq_u16_s16(vabsq_s16(a0));
  633. const uint16x8_t b1 = vreinterpretq_u16_s16(vabsq_s16(b0));
  634. const uint16x8_t a2 = vshrq_n_u16(a1, 3);
  635. const uint16x8_t b2 = vshrq_n_u16(b1, 3);
  636. const uint16x8_t a3 = vminq_u16(a2, max_coeff_thresh);
  637. const uint16x8_t b3 = vminq_u16(b2, max_coeff_thresh);
  638. vst1q_s16(out + 0, vreinterpretq_s16_u16(a3));
  639. vst1q_s16(out + 8, vreinterpretq_s16_u16(b3));
  640. // Convert coefficients to bin.
  641. for (k = 0; k < 16; ++k) {
  642. ++distribution[out[k]];
  643. }
  644. }
  645. }
  646. VP8SetHistogramData(distribution, histo);
  647. }
  648. //------------------------------------------------------------------------------
  649. static WEBP_INLINE void AccumulateSSE16_NEON(const uint8_t* const a,
  650. const uint8_t* const b,
  651. uint32x4_t* const sum) {
  652. const uint8x16_t a0 = vld1q_u8(a);
  653. const uint8x16_t b0 = vld1q_u8(b);
  654. const uint8x16_t abs_diff = vabdq_u8(a0, b0);
  655. const uint16x8_t prod1 = vmull_u8(vget_low_u8(abs_diff),
  656. vget_low_u8(abs_diff));
  657. const uint16x8_t prod2 = vmull_u8(vget_high_u8(abs_diff),
  658. vget_high_u8(abs_diff));
  659. /* pair-wise adds and widen */
  660. const uint32x4_t sum1 = vpaddlq_u16(prod1);
  661. const uint32x4_t sum2 = vpaddlq_u16(prod2);
  662. *sum = vaddq_u32(*sum, vaddq_u32(sum1, sum2));
  663. }
  664. // Horizontal sum of all four uint32_t values in 'sum'.
  665. static int SumToInt_NEON(uint32x4_t sum) {
  666. const uint64x2_t sum2 = vpaddlq_u32(sum);
  667. const uint64_t sum3 = vgetq_lane_u64(sum2, 0) + vgetq_lane_u64(sum2, 1);
  668. return (int)sum3;
  669. }
  670. static int SSE16x16_NEON(const uint8_t* a, const uint8_t* b) {
  671. uint32x4_t sum = vdupq_n_u32(0);
  672. int y;
  673. for (y = 0; y < 16; ++y) {
  674. AccumulateSSE16_NEON(a + y * BPS, b + y * BPS, &sum);
  675. }
  676. return SumToInt_NEON(sum);
  677. }
  678. static int SSE16x8_NEON(const uint8_t* a, const uint8_t* b) {
  679. uint32x4_t sum = vdupq_n_u32(0);
  680. int y;
  681. for (y = 0; y < 8; ++y) {
  682. AccumulateSSE16_NEON(a + y * BPS, b + y * BPS, &sum);
  683. }
  684. return SumToInt_NEON(sum);
  685. }
  686. static int SSE8x8_NEON(const uint8_t* a, const uint8_t* b) {
  687. uint32x4_t sum = vdupq_n_u32(0);
  688. int y;
  689. for (y = 0; y < 8; ++y) {
  690. const uint8x8_t a0 = vld1_u8(a + y * BPS);
  691. const uint8x8_t b0 = vld1_u8(b + y * BPS);
  692. const uint8x8_t abs_diff = vabd_u8(a0, b0);
  693. const uint16x8_t prod = vmull_u8(abs_diff, abs_diff);
  694. sum = vpadalq_u16(sum, prod);
  695. }
  696. return SumToInt_NEON(sum);
  697. }
  698. static int SSE4x4_NEON(const uint8_t* a, const uint8_t* b) {
  699. const uint8x16_t a0 = Load4x4_NEON(a);
  700. const uint8x16_t b0 = Load4x4_NEON(b);
  701. const uint8x16_t abs_diff = vabdq_u8(a0, b0);
  702. const uint16x8_t prod1 = vmull_u8(vget_low_u8(abs_diff),
  703. vget_low_u8(abs_diff));
  704. const uint16x8_t prod2 = vmull_u8(vget_high_u8(abs_diff),
  705. vget_high_u8(abs_diff));
  706. /* pair-wise adds and widen */
  707. const uint32x4_t sum1 = vpaddlq_u16(prod1);
  708. const uint32x4_t sum2 = vpaddlq_u16(prod2);
  709. return SumToInt_NEON(vaddq_u32(sum1, sum2));
  710. }
  711. //------------------------------------------------------------------------------
  712. // Compilation with gcc-4.6.x is problematic for now.
  713. #if !defined(WORK_AROUND_GCC)
  714. static int16x8_t Quantize_NEON(int16_t* const in,
  715. const VP8Matrix* const mtx, int offset) {
  716. const uint16x8_t sharp = vld1q_u16(&mtx->sharpen_[offset]);
  717. const uint16x8_t q = vld1q_u16(&mtx->q_[offset]);
  718. const uint16x8_t iq = vld1q_u16(&mtx->iq_[offset]);
  719. const uint32x4_t bias0 = vld1q_u32(&mtx->bias_[offset + 0]);
  720. const uint32x4_t bias1 = vld1q_u32(&mtx->bias_[offset + 4]);
  721. const int16x8_t a = vld1q_s16(in + offset); // in
  722. const uint16x8_t b = vreinterpretq_u16_s16(vabsq_s16(a)); // coeff = abs(in)
  723. const int16x8_t sign = vshrq_n_s16(a, 15); // sign
  724. const uint16x8_t c = vaddq_u16(b, sharp); // + sharpen
  725. const uint32x4_t m0 = vmull_u16(vget_low_u16(c), vget_low_u16(iq));
  726. const uint32x4_t m1 = vmull_u16(vget_high_u16(c), vget_high_u16(iq));
  727. const uint32x4_t m2 = vhaddq_u32(m0, bias0);
  728. const uint32x4_t m3 = vhaddq_u32(m1, bias1); // (coeff * iQ + bias) >> 1
  729. const uint16x8_t c0 = vcombine_u16(vshrn_n_u32(m2, 16),
  730. vshrn_n_u32(m3, 16)); // QFIX=17 = 16+1
  731. const uint16x8_t c1 = vminq_u16(c0, vdupq_n_u16(MAX_LEVEL));
  732. const int16x8_t c2 = veorq_s16(vreinterpretq_s16_u16(c1), sign);
  733. const int16x8_t c3 = vsubq_s16(c2, sign); // restore sign
  734. const int16x8_t c4 = vmulq_s16(c3, vreinterpretq_s16_u16(q));
  735. vst1q_s16(in + offset, c4);
  736. assert(QFIX == 17); // this function can't work as is if QFIX != 16+1
  737. return c3;
  738. }
  739. static const uint8_t kShuffles[4][8] = {
  740. { 0, 1, 2, 3, 8, 9, 16, 17 },
  741. { 10, 11, 4, 5, 6, 7, 12, 13 },
  742. { 18, 19, 24, 25, 26, 27, 20, 21 },
  743. { 14, 15, 22, 23, 28, 29, 30, 31 }
  744. };
  745. static int QuantizeBlock_NEON(int16_t in[16], int16_t out[16],
  746. const VP8Matrix* const mtx) {
  747. const int16x8_t out0 = Quantize_NEON(in, mtx, 0);
  748. const int16x8_t out1 = Quantize_NEON(in, mtx, 8);
  749. uint8x8x4_t shuffles;
  750. // vtbl?_u8 are marked unavailable for iOS arm64 with Xcode < 6.3, use
  751. // non-standard versions there.
  752. #if defined(__APPLE__) && defined(__aarch64__) && \
  753. defined(__apple_build_version__) && (__apple_build_version__< 6020037)
  754. uint8x16x2_t all_out;
  755. INIT_VECTOR2(all_out, vreinterpretq_u8_s16(out0), vreinterpretq_u8_s16(out1));
  756. INIT_VECTOR4(shuffles,
  757. vtbl2q_u8(all_out, vld1_u8(kShuffles[0])),
  758. vtbl2q_u8(all_out, vld1_u8(kShuffles[1])),
  759. vtbl2q_u8(all_out, vld1_u8(kShuffles[2])),
  760. vtbl2q_u8(all_out, vld1_u8(kShuffles[3])));
  761. #else
  762. uint8x8x4_t all_out;
  763. INIT_VECTOR4(all_out,
  764. vreinterpret_u8_s16(vget_low_s16(out0)),
  765. vreinterpret_u8_s16(vget_high_s16(out0)),
  766. vreinterpret_u8_s16(vget_low_s16(out1)),
  767. vreinterpret_u8_s16(vget_high_s16(out1)));
  768. INIT_VECTOR4(shuffles,
  769. vtbl4_u8(all_out, vld1_u8(kShuffles[0])),
  770. vtbl4_u8(all_out, vld1_u8(kShuffles[1])),
  771. vtbl4_u8(all_out, vld1_u8(kShuffles[2])),
  772. vtbl4_u8(all_out, vld1_u8(kShuffles[3])));
  773. #endif
  774. // Zigzag reordering
  775. vst1_u8((uint8_t*)(out + 0), shuffles.val[0]);
  776. vst1_u8((uint8_t*)(out + 4), shuffles.val[1]);
  777. vst1_u8((uint8_t*)(out + 8), shuffles.val[2]);
  778. vst1_u8((uint8_t*)(out + 12), shuffles.val[3]);
  779. // test zeros
  780. if (*(uint64_t*)(out + 0) != 0) return 1;
  781. if (*(uint64_t*)(out + 4) != 0) return 1;
  782. if (*(uint64_t*)(out + 8) != 0) return 1;
  783. if (*(uint64_t*)(out + 12) != 0) return 1;
  784. return 0;
  785. }
  786. static int Quantize2Blocks_NEON(int16_t in[32], int16_t out[32],
  787. const VP8Matrix* const mtx) {
  788. int nz;
  789. nz = QuantizeBlock_NEON(in + 0 * 16, out + 0 * 16, mtx) << 0;
  790. nz |= QuantizeBlock_NEON(in + 1 * 16, out + 1 * 16, mtx) << 1;
  791. return nz;
  792. }
  793. #endif // !WORK_AROUND_GCC
  794. //------------------------------------------------------------------------------
  795. // Entry point
  796. extern void VP8EncDspInitNEON(void);
  797. WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspInitNEON(void) {
  798. VP8ITransform = ITransform_NEON;
  799. VP8FTransform = FTransform_NEON;
  800. VP8FTransformWHT = FTransformWHT_NEON;
  801. VP8TDisto4x4 = Disto4x4_NEON;
  802. VP8TDisto16x16 = Disto16x16_NEON;
  803. VP8CollectHistogram = CollectHistogram_NEON;
  804. VP8SSE16x16 = SSE16x16_NEON;
  805. VP8SSE16x8 = SSE16x8_NEON;
  806. VP8SSE8x8 = SSE8x8_NEON;
  807. VP8SSE4x4 = SSE4x4_NEON;
  808. #if !defined(WORK_AROUND_GCC)
  809. VP8EncQuantizeBlock = QuantizeBlock_NEON;
  810. VP8EncQuantize2Blocks = Quantize2Blocks_NEON;
  811. #endif
  812. }
  813. #else // !WEBP_USE_NEON
  814. WEBP_DSP_INIT_STUB(VP8EncDspInitNEON)
  815. #endif // WEBP_USE_NEON