jidctint-neon.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * Accurate integer IDCT (Arm Neon)
  3. *
  4. * Copyright (C) 2020, Arm Limited. All Rights Reserved.
  5. * Copyright (C) 2020, 2024, D. R. Commander. All Rights Reserved.
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any damages
  9. * arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would be
  18. * appreciated but is not required.
  19. * 2. Altered source versions must be plainly marked as such, and must not be
  20. * misrepresented as being the original software.
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #define JPEG_INTERNALS
  24. #include "../../src/jinclude.h"
  25. #include "../../src/jpeglib.h"
  26. #include "../../src/jsimd.h"
  27. #include "../../src/jdct.h"
  28. #include "../../src/jsimddct.h"
  29. #include "../jsimd.h"
  30. #include "align.h"
  31. #include "neon-compat.h"
  32. #include <arm_neon.h>
  33. #define CONST_BITS 13
  34. #define PASS1_BITS 2
  35. #define DESCALE_P1 (CONST_BITS - PASS1_BITS)
  36. #define DESCALE_P2 (CONST_BITS + PASS1_BITS + 3)
  37. /* The computation of the inverse DCT requires the use of constants known at
  38. * compile time. Scaled integer constants are used to avoid floating-point
  39. * arithmetic:
  40. * 0.298631336 = 2446 * 2^-13
  41. * 0.390180644 = 3196 * 2^-13
  42. * 0.541196100 = 4433 * 2^-13
  43. * 0.765366865 = 6270 * 2^-13
  44. * 0.899976223 = 7373 * 2^-13
  45. * 1.175875602 = 9633 * 2^-13
  46. * 1.501321110 = 12299 * 2^-13
  47. * 1.847759065 = 15137 * 2^-13
  48. * 1.961570560 = 16069 * 2^-13
  49. * 2.053119869 = 16819 * 2^-13
  50. * 2.562915447 = 20995 * 2^-13
  51. * 3.072711026 = 25172 * 2^-13
  52. */
  53. #define F_0_298 2446
  54. #define F_0_390 3196
  55. #define F_0_541 4433
  56. #define F_0_765 6270
  57. #define F_0_899 7373
  58. #define F_1_175 9633
  59. #define F_1_501 12299
  60. #define F_1_847 15137
  61. #define F_1_961 16069
  62. #define F_2_053 16819
  63. #define F_2_562 20995
  64. #define F_3_072 25172
  65. #define F_1_175_MINUS_1_961 (F_1_175 - F_1_961)
  66. #define F_1_175_MINUS_0_390 (F_1_175 - F_0_390)
  67. #define F_0_541_MINUS_1_847 (F_0_541 - F_1_847)
  68. #define F_3_072_MINUS_2_562 (F_3_072 - F_2_562)
  69. #define F_0_298_MINUS_0_899 (F_0_298 - F_0_899)
  70. #define F_1_501_MINUS_0_899 (F_1_501 - F_0_899)
  71. #define F_2_053_MINUS_2_562 (F_2_053 - F_2_562)
  72. #define F_0_541_PLUS_0_765 (F_0_541 + F_0_765)
  73. ALIGN(16) static const int16_t jsimd_idct_islow_neon_consts[] = {
  74. F_0_899, F_0_541,
  75. F_2_562, F_0_298_MINUS_0_899,
  76. F_1_501_MINUS_0_899, F_2_053_MINUS_2_562,
  77. F_0_541_PLUS_0_765, F_1_175,
  78. F_1_175_MINUS_0_390, F_0_541_MINUS_1_847,
  79. F_3_072_MINUS_2_562, F_1_175_MINUS_1_961,
  80. 0, 0, 0, 0
  81. };
  82. /* Forward declaration of regular and sparse IDCT helper functions */
  83. static INLINE void jsimd_idct_islow_pass1_regular(int16x4_t row0,
  84. int16x4_t row1,
  85. int16x4_t row2,
  86. int16x4_t row3,
  87. int16x4_t row4,
  88. int16x4_t row5,
  89. int16x4_t row6,
  90. int16x4_t row7,
  91. int16x4_t quant_row0,
  92. int16x4_t quant_row1,
  93. int16x4_t quant_row2,
  94. int16x4_t quant_row3,
  95. int16x4_t quant_row4,
  96. int16x4_t quant_row5,
  97. int16x4_t quant_row6,
  98. int16x4_t quant_row7,
  99. int16_t *workspace_1,
  100. int16_t *workspace_2);
  101. static INLINE void jsimd_idct_islow_pass1_sparse(int16x4_t row0,
  102. int16x4_t row1,
  103. int16x4_t row2,
  104. int16x4_t row3,
  105. int16x4_t quant_row0,
  106. int16x4_t quant_row1,
  107. int16x4_t quant_row2,
  108. int16x4_t quant_row3,
  109. int16_t *workspace_1,
  110. int16_t *workspace_2);
  111. static INLINE void jsimd_idct_islow_pass2_regular(int16_t *workspace,
  112. JSAMPARRAY output_buf,
  113. JDIMENSION output_col,
  114. unsigned buf_offset);
  115. static INLINE void jsimd_idct_islow_pass2_sparse(int16_t *workspace,
  116. JSAMPARRAY output_buf,
  117. JDIMENSION output_col,
  118. unsigned buf_offset);
  119. /* Perform dequantization and inverse DCT on one block of coefficients. For
  120. * reference, the C implementation (jpeg_idct_slow()) can be found in
  121. * jidctint.c.
  122. *
  123. * Optimization techniques used for fast data access:
  124. *
  125. * In each pass, the inverse DCT is computed for the left and right 4x8 halves
  126. * of the DCT block. This avoids spilling due to register pressure, and the
  127. * increased granularity allows for an optimized calculation depending on the
  128. * values of the DCT coefficients. Between passes, intermediate data is stored
  129. * in 4x8 workspace buffers.
  130. *
  131. * Transposing the 8x8 DCT block after each pass can be achieved by transposing
  132. * each of the four 4x4 quadrants and swapping quadrants 1 and 2 (refer to the
  133. * diagram below.) Swapping quadrants is cheap, since the second pass can just
  134. * swap the workspace buffer pointers.
  135. *
  136. * +-------+-------+ +-------+-------+
  137. * | | | | | |
  138. * | 0 | 1 | | 0 | 2 |
  139. * | | | transpose | | |
  140. * +-------+-------+ ------> +-------+-------+
  141. * | | | | | |
  142. * | 2 | 3 | | 1 | 3 |
  143. * | | | | | |
  144. * +-------+-------+ +-------+-------+
  145. *
  146. * Optimization techniques used to accelerate the inverse DCT calculation:
  147. *
  148. * In a DCT coefficient block, the coefficients are increasingly likely to be 0
  149. * as you move diagonally from top left to bottom right. If whole rows of
  150. * coefficients are 0, then the inverse DCT calculation can be simplified. On
  151. * the first pass of the inverse DCT, we test for three special cases before
  152. * defaulting to a full "regular" inverse DCT:
  153. *
  154. * 1) Coefficients in rows 4-7 are all zero. In this case, we perform a
  155. * "sparse" simplified inverse DCT on rows 0-3.
  156. * 2) AC coefficients (rows 1-7) are all zero. In this case, the inverse DCT
  157. * result is equal to the dequantized DC coefficients.
  158. * 3) AC and DC coefficients are all zero. In this case, the inverse DCT
  159. * result is all zero. For the left 4x8 half, this is handled identically
  160. * to Case 2 above. For the right 4x8 half, we do no work and signal that
  161. * the "sparse" algorithm is required for the second pass.
  162. *
  163. * In the second pass, only a single special case is tested: whether the AC and
  164. * DC coefficients were all zero in the right 4x8 block during the first pass
  165. * (refer to Case 3 above.) If this is the case, then a "sparse" variant of
  166. * the second pass is performed for both the left and right halves of the DCT
  167. * block. (The transposition after the first pass means that the right 4x8
  168. * block during the first pass becomes rows 4-7 during the second pass.)
  169. */
  170. void jsimd_idct_islow_neon(void *dct_table, JCOEFPTR coef_block,
  171. JSAMPARRAY output_buf, JDIMENSION output_col)
  172. {
  173. ISLOW_MULT_TYPE *quantptr = dct_table;
  174. int16_t workspace_l[8 * DCTSIZE / 2];
  175. int16_t workspace_r[8 * DCTSIZE / 2];
  176. /* Compute IDCT first pass on left 4x8 coefficient block. */
  177. /* Load DCT coefficients in left 4x8 block. */
  178. int16x4_t row0 = vld1_s16(coef_block + 0 * DCTSIZE);
  179. int16x4_t row1 = vld1_s16(coef_block + 1 * DCTSIZE);
  180. int16x4_t row2 = vld1_s16(coef_block + 2 * DCTSIZE);
  181. int16x4_t row3 = vld1_s16(coef_block + 3 * DCTSIZE);
  182. int16x4_t row4 = vld1_s16(coef_block + 4 * DCTSIZE);
  183. int16x4_t row5 = vld1_s16(coef_block + 5 * DCTSIZE);
  184. int16x4_t row6 = vld1_s16(coef_block + 6 * DCTSIZE);
  185. int16x4_t row7 = vld1_s16(coef_block + 7 * DCTSIZE);
  186. /* Load quantization table for left 4x8 block. */
  187. int16x4_t quant_row0 = vld1_s16(quantptr + 0 * DCTSIZE);
  188. int16x4_t quant_row1 = vld1_s16(quantptr + 1 * DCTSIZE);
  189. int16x4_t quant_row2 = vld1_s16(quantptr + 2 * DCTSIZE);
  190. int16x4_t quant_row3 = vld1_s16(quantptr + 3 * DCTSIZE);
  191. int16x4_t quant_row4 = vld1_s16(quantptr + 4 * DCTSIZE);
  192. int16x4_t quant_row5 = vld1_s16(quantptr + 5 * DCTSIZE);
  193. int16x4_t quant_row6 = vld1_s16(quantptr + 6 * DCTSIZE);
  194. int16x4_t quant_row7 = vld1_s16(quantptr + 7 * DCTSIZE);
  195. /* Construct bitmap to test if DCT coefficients in left 4x8 block are 0. */
  196. int16x4_t bitmap = vorr_s16(row7, row6);
  197. bitmap = vorr_s16(bitmap, row5);
  198. bitmap = vorr_s16(bitmap, row4);
  199. int64_t bitmap_rows_4567 = vget_lane_s64(vreinterpret_s64_s16(bitmap), 0);
  200. if (bitmap_rows_4567 == 0) {
  201. bitmap = vorr_s16(bitmap, row3);
  202. bitmap = vorr_s16(bitmap, row2);
  203. bitmap = vorr_s16(bitmap, row1);
  204. int64_t left_ac_bitmap = vget_lane_s64(vreinterpret_s64_s16(bitmap), 0);
  205. if (left_ac_bitmap == 0) {
  206. int16x4_t dcval = vshl_n_s16(vmul_s16(row0, quant_row0), PASS1_BITS);
  207. int16x4x4_t quadrant = { { dcval, dcval, dcval, dcval } };
  208. /* Store 4x4 blocks to workspace, transposing in the process. */
  209. vst4_s16(workspace_l, quadrant);
  210. vst4_s16(workspace_r, quadrant);
  211. } else {
  212. jsimd_idct_islow_pass1_sparse(row0, row1, row2, row3, quant_row0,
  213. quant_row1, quant_row2, quant_row3,
  214. workspace_l, workspace_r);
  215. }
  216. } else {
  217. jsimd_idct_islow_pass1_regular(row0, row1, row2, row3, row4, row5,
  218. row6, row7, quant_row0, quant_row1,
  219. quant_row2, quant_row3, quant_row4,
  220. quant_row5, quant_row6, quant_row7,
  221. workspace_l, workspace_r);
  222. }
  223. /* Compute IDCT first pass on right 4x8 coefficient block. */
  224. /* Load DCT coefficients in right 4x8 block. */
  225. row0 = vld1_s16(coef_block + 0 * DCTSIZE + 4);
  226. row1 = vld1_s16(coef_block + 1 * DCTSIZE + 4);
  227. row2 = vld1_s16(coef_block + 2 * DCTSIZE + 4);
  228. row3 = vld1_s16(coef_block + 3 * DCTSIZE + 4);
  229. row4 = vld1_s16(coef_block + 4 * DCTSIZE + 4);
  230. row5 = vld1_s16(coef_block + 5 * DCTSIZE + 4);
  231. row6 = vld1_s16(coef_block + 6 * DCTSIZE + 4);
  232. row7 = vld1_s16(coef_block + 7 * DCTSIZE + 4);
  233. /* Load quantization table for right 4x8 block. */
  234. quant_row0 = vld1_s16(quantptr + 0 * DCTSIZE + 4);
  235. quant_row1 = vld1_s16(quantptr + 1 * DCTSIZE + 4);
  236. quant_row2 = vld1_s16(quantptr + 2 * DCTSIZE + 4);
  237. quant_row3 = vld1_s16(quantptr + 3 * DCTSIZE + 4);
  238. quant_row4 = vld1_s16(quantptr + 4 * DCTSIZE + 4);
  239. quant_row5 = vld1_s16(quantptr + 5 * DCTSIZE + 4);
  240. quant_row6 = vld1_s16(quantptr + 6 * DCTSIZE + 4);
  241. quant_row7 = vld1_s16(quantptr + 7 * DCTSIZE + 4);
  242. /* Construct bitmap to test if DCT coefficients in right 4x8 block are 0. */
  243. bitmap = vorr_s16(row7, row6);
  244. bitmap = vorr_s16(bitmap, row5);
  245. bitmap = vorr_s16(bitmap, row4);
  246. bitmap_rows_4567 = vget_lane_s64(vreinterpret_s64_s16(bitmap), 0);
  247. bitmap = vorr_s16(bitmap, row3);
  248. bitmap = vorr_s16(bitmap, row2);
  249. bitmap = vorr_s16(bitmap, row1);
  250. int64_t right_ac_bitmap = vget_lane_s64(vreinterpret_s64_s16(bitmap), 0);
  251. /* If this remains non-zero, a "regular" second pass will be performed. */
  252. int64_t right_ac_dc_bitmap = 1;
  253. if (right_ac_bitmap == 0) {
  254. bitmap = vorr_s16(bitmap, row0);
  255. right_ac_dc_bitmap = vget_lane_s64(vreinterpret_s64_s16(bitmap), 0);
  256. if (right_ac_dc_bitmap != 0) {
  257. int16x4_t dcval = vshl_n_s16(vmul_s16(row0, quant_row0), PASS1_BITS);
  258. int16x4x4_t quadrant = { { dcval, dcval, dcval, dcval } };
  259. /* Store 4x4 blocks to workspace, transposing in the process. */
  260. vst4_s16(workspace_l + 4 * DCTSIZE / 2, quadrant);
  261. vst4_s16(workspace_r + 4 * DCTSIZE / 2, quadrant);
  262. }
  263. } else {
  264. if (bitmap_rows_4567 == 0) {
  265. jsimd_idct_islow_pass1_sparse(row0, row1, row2, row3, quant_row0,
  266. quant_row1, quant_row2, quant_row3,
  267. workspace_l + 4 * DCTSIZE / 2,
  268. workspace_r + 4 * DCTSIZE / 2);
  269. } else {
  270. jsimd_idct_islow_pass1_regular(row0, row1, row2, row3, row4, row5,
  271. row6, row7, quant_row0, quant_row1,
  272. quant_row2, quant_row3, quant_row4,
  273. quant_row5, quant_row6, quant_row7,
  274. workspace_l + 4 * DCTSIZE / 2,
  275. workspace_r + 4 * DCTSIZE / 2);
  276. }
  277. }
  278. /* Second pass: compute IDCT on rows in workspace. */
  279. /* If all coefficients in right 4x8 block are 0, use "sparse" second pass. */
  280. if (right_ac_dc_bitmap == 0) {
  281. jsimd_idct_islow_pass2_sparse(workspace_l, output_buf, output_col, 0);
  282. jsimd_idct_islow_pass2_sparse(workspace_r, output_buf, output_col, 4);
  283. } else {
  284. jsimd_idct_islow_pass2_regular(workspace_l, output_buf, output_col, 0);
  285. jsimd_idct_islow_pass2_regular(workspace_r, output_buf, output_col, 4);
  286. }
  287. }
  288. /* Perform dequantization and the first pass of the accurate inverse DCT on a
  289. * 4x8 block of coefficients. (To process the full 8x8 DCT block, this
  290. * function-- or some other optimized variant-- needs to be called for both the
  291. * left and right 4x8 blocks.)
  292. *
  293. * This "regular" version assumes that no optimization can be made to the IDCT
  294. * calculation, since no useful set of AC coefficients is all 0.
  295. *
  296. * The original C implementation of the accurate IDCT (jpeg_idct_slow()) can be
  297. * found in jidctint.c. Algorithmic changes made here are documented inline.
  298. */
  299. static INLINE void jsimd_idct_islow_pass1_regular(int16x4_t row0,
  300. int16x4_t row1,
  301. int16x4_t row2,
  302. int16x4_t row3,
  303. int16x4_t row4,
  304. int16x4_t row5,
  305. int16x4_t row6,
  306. int16x4_t row7,
  307. int16x4_t quant_row0,
  308. int16x4_t quant_row1,
  309. int16x4_t quant_row2,
  310. int16x4_t quant_row3,
  311. int16x4_t quant_row4,
  312. int16x4_t quant_row5,
  313. int16x4_t quant_row6,
  314. int16x4_t quant_row7,
  315. int16_t *workspace_1,
  316. int16_t *workspace_2)
  317. {
  318. /* Load constants for IDCT computation. */
  319. #ifdef HAVE_VLD1_S16_X3
  320. const int16x4x3_t consts = vld1_s16_x3(jsimd_idct_islow_neon_consts);
  321. #else
  322. const int16x4_t consts1 = vld1_s16(jsimd_idct_islow_neon_consts);
  323. const int16x4_t consts2 = vld1_s16(jsimd_idct_islow_neon_consts + 4);
  324. const int16x4_t consts3 = vld1_s16(jsimd_idct_islow_neon_consts + 8);
  325. const int16x4x3_t consts = { { consts1, consts2, consts3 } };
  326. #endif
  327. /* Even part */
  328. int16x4_t z2_s16 = vmul_s16(row2, quant_row2);
  329. int16x4_t z3_s16 = vmul_s16(row6, quant_row6);
  330. int32x4_t tmp2 = vmull_lane_s16(z2_s16, consts.val[0], 1);
  331. int32x4_t tmp3 = vmull_lane_s16(z2_s16, consts.val[1], 2);
  332. tmp2 = vmlal_lane_s16(tmp2, z3_s16, consts.val[2], 1);
  333. tmp3 = vmlal_lane_s16(tmp3, z3_s16, consts.val[0], 1);
  334. z2_s16 = vmul_s16(row0, quant_row0);
  335. z3_s16 = vmul_s16(row4, quant_row4);
  336. int32x4_t tmp0 = vshll_n_s16(vadd_s16(z2_s16, z3_s16), CONST_BITS);
  337. int32x4_t tmp1 = vshll_n_s16(vsub_s16(z2_s16, z3_s16), CONST_BITS);
  338. int32x4_t tmp10 = vaddq_s32(tmp0, tmp3);
  339. int32x4_t tmp13 = vsubq_s32(tmp0, tmp3);
  340. int32x4_t tmp11 = vaddq_s32(tmp1, tmp2);
  341. int32x4_t tmp12 = vsubq_s32(tmp1, tmp2);
  342. /* Odd part */
  343. int16x4_t tmp0_s16 = vmul_s16(row7, quant_row7);
  344. int16x4_t tmp1_s16 = vmul_s16(row5, quant_row5);
  345. int16x4_t tmp2_s16 = vmul_s16(row3, quant_row3);
  346. int16x4_t tmp3_s16 = vmul_s16(row1, quant_row1);
  347. z3_s16 = vadd_s16(tmp0_s16, tmp2_s16);
  348. int16x4_t z4_s16 = vadd_s16(tmp1_s16, tmp3_s16);
  349. /* Implementation as per jpeg_idct_islow() in jidctint.c:
  350. * z5 = (z3 + z4) * 1.175875602;
  351. * z3 = z3 * -1.961570560; z4 = z4 * -0.390180644;
  352. * z3 += z5; z4 += z5;
  353. *
  354. * This implementation:
  355. * z3 = z3 * (1.175875602 - 1.961570560) + z4 * 1.175875602;
  356. * z4 = z3 * 1.175875602 + z4 * (1.175875602 - 0.390180644);
  357. */
  358. int32x4_t z3 = vmull_lane_s16(z3_s16, consts.val[2], 3);
  359. int32x4_t z4 = vmull_lane_s16(z3_s16, consts.val[1], 3);
  360. z3 = vmlal_lane_s16(z3, z4_s16, consts.val[1], 3);
  361. z4 = vmlal_lane_s16(z4, z4_s16, consts.val[2], 0);
  362. /* Implementation as per jpeg_idct_islow() in jidctint.c:
  363. * z1 = tmp0 + tmp3; z2 = tmp1 + tmp2;
  364. * tmp0 = tmp0 * 0.298631336; tmp1 = tmp1 * 2.053119869;
  365. * tmp2 = tmp2 * 3.072711026; tmp3 = tmp3 * 1.501321110;
  366. * z1 = z1 * -0.899976223; z2 = z2 * -2.562915447;
  367. * tmp0 += z1 + z3; tmp1 += z2 + z4;
  368. * tmp2 += z2 + z3; tmp3 += z1 + z4;
  369. *
  370. * This implementation:
  371. * tmp0 = tmp0 * (0.298631336 - 0.899976223) + tmp3 * -0.899976223;
  372. * tmp1 = tmp1 * (2.053119869 - 2.562915447) + tmp2 * -2.562915447;
  373. * tmp2 = tmp1 * -2.562915447 + tmp2 * (3.072711026 - 2.562915447);
  374. * tmp3 = tmp0 * -0.899976223 + tmp3 * (1.501321110 - 0.899976223);
  375. * tmp0 += z3; tmp1 += z4;
  376. * tmp2 += z3; tmp3 += z4;
  377. */
  378. tmp0 = vmull_lane_s16(tmp0_s16, consts.val[0], 3);
  379. tmp1 = vmull_lane_s16(tmp1_s16, consts.val[1], 1);
  380. tmp2 = vmull_lane_s16(tmp2_s16, consts.val[2], 2);
  381. tmp3 = vmull_lane_s16(tmp3_s16, consts.val[1], 0);
  382. tmp0 = vmlsl_lane_s16(tmp0, tmp3_s16, consts.val[0], 0);
  383. tmp1 = vmlsl_lane_s16(tmp1, tmp2_s16, consts.val[0], 2);
  384. tmp2 = vmlsl_lane_s16(tmp2, tmp1_s16, consts.val[0], 2);
  385. tmp3 = vmlsl_lane_s16(tmp3, tmp0_s16, consts.val[0], 0);
  386. tmp0 = vaddq_s32(tmp0, z3);
  387. tmp1 = vaddq_s32(tmp1, z4);
  388. tmp2 = vaddq_s32(tmp2, z3);
  389. tmp3 = vaddq_s32(tmp3, z4);
  390. /* Final output stage: descale and narrow to 16-bit. */
  391. int16x4x4_t rows_0123 = { {
  392. vrshrn_n_s32(vaddq_s32(tmp10, tmp3), DESCALE_P1),
  393. vrshrn_n_s32(vaddq_s32(tmp11, tmp2), DESCALE_P1),
  394. vrshrn_n_s32(vaddq_s32(tmp12, tmp1), DESCALE_P1),
  395. vrshrn_n_s32(vaddq_s32(tmp13, tmp0), DESCALE_P1)
  396. } };
  397. int16x4x4_t rows_4567 = { {
  398. vrshrn_n_s32(vsubq_s32(tmp13, tmp0), DESCALE_P1),
  399. vrshrn_n_s32(vsubq_s32(tmp12, tmp1), DESCALE_P1),
  400. vrshrn_n_s32(vsubq_s32(tmp11, tmp2), DESCALE_P1),
  401. vrshrn_n_s32(vsubq_s32(tmp10, tmp3), DESCALE_P1)
  402. } };
  403. /* Store 4x4 blocks to the intermediate workspace, ready for the second pass.
  404. * (VST4 transposes the blocks. We need to operate on rows in the next
  405. * pass.)
  406. */
  407. vst4_s16(workspace_1, rows_0123);
  408. vst4_s16(workspace_2, rows_4567);
  409. }
  410. /* Perform dequantization and the first pass of the accurate inverse DCT on a
  411. * 4x8 block of coefficients.
  412. *
  413. * This "sparse" version assumes that the AC coefficients in rows 4-7 are all
  414. * 0. This simplifies the IDCT calculation, accelerating overall performance.
  415. */
  416. static INLINE void jsimd_idct_islow_pass1_sparse(int16x4_t row0,
  417. int16x4_t row1,
  418. int16x4_t row2,
  419. int16x4_t row3,
  420. int16x4_t quant_row0,
  421. int16x4_t quant_row1,
  422. int16x4_t quant_row2,
  423. int16x4_t quant_row3,
  424. int16_t *workspace_1,
  425. int16_t *workspace_2)
  426. {
  427. /* Load constants for IDCT computation. */
  428. #ifdef HAVE_VLD1_S16_X3
  429. const int16x4x3_t consts = vld1_s16_x3(jsimd_idct_islow_neon_consts);
  430. #else
  431. const int16x4_t consts1 = vld1_s16(jsimd_idct_islow_neon_consts);
  432. const int16x4_t consts2 = vld1_s16(jsimd_idct_islow_neon_consts + 4);
  433. const int16x4_t consts3 = vld1_s16(jsimd_idct_islow_neon_consts + 8);
  434. const int16x4x3_t consts = { { consts1, consts2, consts3 } };
  435. #endif
  436. /* Even part (z3 is all 0) */
  437. int16x4_t z2_s16 = vmul_s16(row2, quant_row2);
  438. int32x4_t tmp2 = vmull_lane_s16(z2_s16, consts.val[0], 1);
  439. int32x4_t tmp3 = vmull_lane_s16(z2_s16, consts.val[1], 2);
  440. z2_s16 = vmul_s16(row0, quant_row0);
  441. int32x4_t tmp0 = vshll_n_s16(z2_s16, CONST_BITS);
  442. int32x4_t tmp1 = vshll_n_s16(z2_s16, CONST_BITS);
  443. int32x4_t tmp10 = vaddq_s32(tmp0, tmp3);
  444. int32x4_t tmp13 = vsubq_s32(tmp0, tmp3);
  445. int32x4_t tmp11 = vaddq_s32(tmp1, tmp2);
  446. int32x4_t tmp12 = vsubq_s32(tmp1, tmp2);
  447. /* Odd part (tmp0 and tmp1 are both all 0) */
  448. int16x4_t tmp2_s16 = vmul_s16(row3, quant_row3);
  449. int16x4_t tmp3_s16 = vmul_s16(row1, quant_row1);
  450. int16x4_t z3_s16 = tmp2_s16;
  451. int16x4_t z4_s16 = tmp3_s16;
  452. int32x4_t z3 = vmull_lane_s16(z3_s16, consts.val[2], 3);
  453. int32x4_t z4 = vmull_lane_s16(z3_s16, consts.val[1], 3);
  454. z3 = vmlal_lane_s16(z3, z4_s16, consts.val[1], 3);
  455. z4 = vmlal_lane_s16(z4, z4_s16, consts.val[2], 0);
  456. tmp0 = vmlsl_lane_s16(z3, tmp3_s16, consts.val[0], 0);
  457. tmp1 = vmlsl_lane_s16(z4, tmp2_s16, consts.val[0], 2);
  458. tmp2 = vmlal_lane_s16(z3, tmp2_s16, consts.val[2], 2);
  459. tmp3 = vmlal_lane_s16(z4, tmp3_s16, consts.val[1], 0);
  460. /* Final output stage: descale and narrow to 16-bit. */
  461. int16x4x4_t rows_0123 = { {
  462. vrshrn_n_s32(vaddq_s32(tmp10, tmp3), DESCALE_P1),
  463. vrshrn_n_s32(vaddq_s32(tmp11, tmp2), DESCALE_P1),
  464. vrshrn_n_s32(vaddq_s32(tmp12, tmp1), DESCALE_P1),
  465. vrshrn_n_s32(vaddq_s32(tmp13, tmp0), DESCALE_P1)
  466. } };
  467. int16x4x4_t rows_4567 = { {
  468. vrshrn_n_s32(vsubq_s32(tmp13, tmp0), DESCALE_P1),
  469. vrshrn_n_s32(vsubq_s32(tmp12, tmp1), DESCALE_P1),
  470. vrshrn_n_s32(vsubq_s32(tmp11, tmp2), DESCALE_P1),
  471. vrshrn_n_s32(vsubq_s32(tmp10, tmp3), DESCALE_P1)
  472. } };
  473. /* Store 4x4 blocks to the intermediate workspace, ready for the second pass.
  474. * (VST4 transposes the blocks. We need to operate on rows in the next
  475. * pass.)
  476. */
  477. vst4_s16(workspace_1, rows_0123);
  478. vst4_s16(workspace_2, rows_4567);
  479. }
  480. /* Perform the second pass of the accurate inverse DCT on a 4x8 block of
  481. * coefficients. (To process the full 8x8 DCT block, this function-- or some
  482. * other optimized variant-- needs to be called for both the right and left 4x8
  483. * blocks.)
  484. *
  485. * This "regular" version assumes that no optimization can be made to the IDCT
  486. * calculation, since no useful set of coefficient values are all 0 after the
  487. * first pass.
  488. *
  489. * Again, the original C implementation of the accurate IDCT (jpeg_idct_slow())
  490. * can be found in jidctint.c. Algorithmic changes made here are documented
  491. * inline.
  492. */
  493. static INLINE void jsimd_idct_islow_pass2_regular(int16_t *workspace,
  494. JSAMPARRAY output_buf,
  495. JDIMENSION output_col,
  496. unsigned buf_offset)
  497. {
  498. /* Load constants for IDCT computation. */
  499. #ifdef HAVE_VLD1_S16_X3
  500. const int16x4x3_t consts = vld1_s16_x3(jsimd_idct_islow_neon_consts);
  501. #else
  502. const int16x4_t consts1 = vld1_s16(jsimd_idct_islow_neon_consts);
  503. const int16x4_t consts2 = vld1_s16(jsimd_idct_islow_neon_consts + 4);
  504. const int16x4_t consts3 = vld1_s16(jsimd_idct_islow_neon_consts + 8);
  505. const int16x4x3_t consts = { { consts1, consts2, consts3 } };
  506. #endif
  507. /* Even part */
  508. int16x4_t z2_s16 = vld1_s16(workspace + 2 * DCTSIZE / 2);
  509. int16x4_t z3_s16 = vld1_s16(workspace + 6 * DCTSIZE / 2);
  510. int32x4_t tmp2 = vmull_lane_s16(z2_s16, consts.val[0], 1);
  511. int32x4_t tmp3 = vmull_lane_s16(z2_s16, consts.val[1], 2);
  512. tmp2 = vmlal_lane_s16(tmp2, z3_s16, consts.val[2], 1);
  513. tmp3 = vmlal_lane_s16(tmp3, z3_s16, consts.val[0], 1);
  514. z2_s16 = vld1_s16(workspace + 0 * DCTSIZE / 2);
  515. z3_s16 = vld1_s16(workspace + 4 * DCTSIZE / 2);
  516. int32x4_t tmp0 = vshll_n_s16(vadd_s16(z2_s16, z3_s16), CONST_BITS);
  517. int32x4_t tmp1 = vshll_n_s16(vsub_s16(z2_s16, z3_s16), CONST_BITS);
  518. int32x4_t tmp10 = vaddq_s32(tmp0, tmp3);
  519. int32x4_t tmp13 = vsubq_s32(tmp0, tmp3);
  520. int32x4_t tmp11 = vaddq_s32(tmp1, tmp2);
  521. int32x4_t tmp12 = vsubq_s32(tmp1, tmp2);
  522. /* Odd part */
  523. int16x4_t tmp0_s16 = vld1_s16(workspace + 7 * DCTSIZE / 2);
  524. int16x4_t tmp1_s16 = vld1_s16(workspace + 5 * DCTSIZE / 2);
  525. int16x4_t tmp2_s16 = vld1_s16(workspace + 3 * DCTSIZE / 2);
  526. int16x4_t tmp3_s16 = vld1_s16(workspace + 1 * DCTSIZE / 2);
  527. z3_s16 = vadd_s16(tmp0_s16, tmp2_s16);
  528. int16x4_t z4_s16 = vadd_s16(tmp1_s16, tmp3_s16);
  529. /* Implementation as per jpeg_idct_islow() in jidctint.c:
  530. * z5 = (z3 + z4) * 1.175875602;
  531. * z3 = z3 * -1.961570560; z4 = z4 * -0.390180644;
  532. * z3 += z5; z4 += z5;
  533. *
  534. * This implementation:
  535. * z3 = z3 * (1.175875602 - 1.961570560) + z4 * 1.175875602;
  536. * z4 = z3 * 1.175875602 + z4 * (1.175875602 - 0.390180644);
  537. */
  538. int32x4_t z3 = vmull_lane_s16(z3_s16, consts.val[2], 3);
  539. int32x4_t z4 = vmull_lane_s16(z3_s16, consts.val[1], 3);
  540. z3 = vmlal_lane_s16(z3, z4_s16, consts.val[1], 3);
  541. z4 = vmlal_lane_s16(z4, z4_s16, consts.val[2], 0);
  542. /* Implementation as per jpeg_idct_islow() in jidctint.c:
  543. * z1 = tmp0 + tmp3; z2 = tmp1 + tmp2;
  544. * tmp0 = tmp0 * 0.298631336; tmp1 = tmp1 * 2.053119869;
  545. * tmp2 = tmp2 * 3.072711026; tmp3 = tmp3 * 1.501321110;
  546. * z1 = z1 * -0.899976223; z2 = z2 * -2.562915447;
  547. * tmp0 += z1 + z3; tmp1 += z2 + z4;
  548. * tmp2 += z2 + z3; tmp3 += z1 + z4;
  549. *
  550. * This implementation:
  551. * tmp0 = tmp0 * (0.298631336 - 0.899976223) + tmp3 * -0.899976223;
  552. * tmp1 = tmp1 * (2.053119869 - 2.562915447) + tmp2 * -2.562915447;
  553. * tmp2 = tmp1 * -2.562915447 + tmp2 * (3.072711026 - 2.562915447);
  554. * tmp3 = tmp0 * -0.899976223 + tmp3 * (1.501321110 - 0.899976223);
  555. * tmp0 += z3; tmp1 += z4;
  556. * tmp2 += z3; tmp3 += z4;
  557. */
  558. tmp0 = vmull_lane_s16(tmp0_s16, consts.val[0], 3);
  559. tmp1 = vmull_lane_s16(tmp1_s16, consts.val[1], 1);
  560. tmp2 = vmull_lane_s16(tmp2_s16, consts.val[2], 2);
  561. tmp3 = vmull_lane_s16(tmp3_s16, consts.val[1], 0);
  562. tmp0 = vmlsl_lane_s16(tmp0, tmp3_s16, consts.val[0], 0);
  563. tmp1 = vmlsl_lane_s16(tmp1, tmp2_s16, consts.val[0], 2);
  564. tmp2 = vmlsl_lane_s16(tmp2, tmp1_s16, consts.val[0], 2);
  565. tmp3 = vmlsl_lane_s16(tmp3, tmp0_s16, consts.val[0], 0);
  566. tmp0 = vaddq_s32(tmp0, z3);
  567. tmp1 = vaddq_s32(tmp1, z4);
  568. tmp2 = vaddq_s32(tmp2, z3);
  569. tmp3 = vaddq_s32(tmp3, z4);
  570. /* Final output stage: descale and narrow to 16-bit. */
  571. int16x8_t cols_02_s16 = vcombine_s16(vaddhn_s32(tmp10, tmp3),
  572. vaddhn_s32(tmp12, tmp1));
  573. int16x8_t cols_13_s16 = vcombine_s16(vaddhn_s32(tmp11, tmp2),
  574. vaddhn_s32(tmp13, tmp0));
  575. int16x8_t cols_46_s16 = vcombine_s16(vsubhn_s32(tmp13, tmp0),
  576. vsubhn_s32(tmp11, tmp2));
  577. int16x8_t cols_57_s16 = vcombine_s16(vsubhn_s32(tmp12, tmp1),
  578. vsubhn_s32(tmp10, tmp3));
  579. /* Descale and narrow to 8-bit. */
  580. int8x8_t cols_02_s8 = vqrshrn_n_s16(cols_02_s16, DESCALE_P2 - 16);
  581. int8x8_t cols_13_s8 = vqrshrn_n_s16(cols_13_s16, DESCALE_P2 - 16);
  582. int8x8_t cols_46_s8 = vqrshrn_n_s16(cols_46_s16, DESCALE_P2 - 16);
  583. int8x8_t cols_57_s8 = vqrshrn_n_s16(cols_57_s16, DESCALE_P2 - 16);
  584. /* Clamp to range [0-255]. */
  585. uint8x8_t cols_02_u8 = vadd_u8(vreinterpret_u8_s8(cols_02_s8),
  586. vdup_n_u8(CENTERJSAMPLE));
  587. uint8x8_t cols_13_u8 = vadd_u8(vreinterpret_u8_s8(cols_13_s8),
  588. vdup_n_u8(CENTERJSAMPLE));
  589. uint8x8_t cols_46_u8 = vadd_u8(vreinterpret_u8_s8(cols_46_s8),
  590. vdup_n_u8(CENTERJSAMPLE));
  591. uint8x8_t cols_57_u8 = vadd_u8(vreinterpret_u8_s8(cols_57_s8),
  592. vdup_n_u8(CENTERJSAMPLE));
  593. /* Transpose 4x8 block and store to memory. (Zipping adjacent columns
  594. * together allows us to store 16-bit elements.)
  595. */
  596. uint8x8x2_t cols_01_23 = vzip_u8(cols_02_u8, cols_13_u8);
  597. uint8x8x2_t cols_45_67 = vzip_u8(cols_46_u8, cols_57_u8);
  598. uint16x4x4_t cols_01_23_45_67 = { {
  599. vreinterpret_u16_u8(cols_01_23.val[0]),
  600. vreinterpret_u16_u8(cols_01_23.val[1]),
  601. vreinterpret_u16_u8(cols_45_67.val[0]),
  602. vreinterpret_u16_u8(cols_45_67.val[1])
  603. } };
  604. JSAMPROW outptr0 = output_buf[buf_offset + 0] + output_col;
  605. JSAMPROW outptr1 = output_buf[buf_offset + 1] + output_col;
  606. JSAMPROW outptr2 = output_buf[buf_offset + 2] + output_col;
  607. JSAMPROW outptr3 = output_buf[buf_offset + 3] + output_col;
  608. /* VST4 of 16-bit elements completes the transpose. */
  609. vst4_lane_u16((uint16_t *)outptr0, cols_01_23_45_67, 0);
  610. vst4_lane_u16((uint16_t *)outptr1, cols_01_23_45_67, 1);
  611. vst4_lane_u16((uint16_t *)outptr2, cols_01_23_45_67, 2);
  612. vst4_lane_u16((uint16_t *)outptr3, cols_01_23_45_67, 3);
  613. }
  614. /* Performs the second pass of the accurate inverse DCT on a 4x8 block
  615. * of coefficients.
  616. *
  617. * This "sparse" version assumes that the coefficient values (after the first
  618. * pass) in rows 4-7 are all 0. This simplifies the IDCT calculation,
  619. * accelerating overall performance.
  620. */
  621. static INLINE void jsimd_idct_islow_pass2_sparse(int16_t *workspace,
  622. JSAMPARRAY output_buf,
  623. JDIMENSION output_col,
  624. unsigned buf_offset)
  625. {
  626. /* Load constants for IDCT computation. */
  627. #ifdef HAVE_VLD1_S16_X3
  628. const int16x4x3_t consts = vld1_s16_x3(jsimd_idct_islow_neon_consts);
  629. #else
  630. const int16x4_t consts1 = vld1_s16(jsimd_idct_islow_neon_consts);
  631. const int16x4_t consts2 = vld1_s16(jsimd_idct_islow_neon_consts + 4);
  632. const int16x4_t consts3 = vld1_s16(jsimd_idct_islow_neon_consts + 8);
  633. const int16x4x3_t consts = { { consts1, consts2, consts3 } };
  634. #endif
  635. /* Even part (z3 is all 0) */
  636. int16x4_t z2_s16 = vld1_s16(workspace + 2 * DCTSIZE / 2);
  637. int32x4_t tmp2 = vmull_lane_s16(z2_s16, consts.val[0], 1);
  638. int32x4_t tmp3 = vmull_lane_s16(z2_s16, consts.val[1], 2);
  639. z2_s16 = vld1_s16(workspace + 0 * DCTSIZE / 2);
  640. int32x4_t tmp0 = vshll_n_s16(z2_s16, CONST_BITS);
  641. int32x4_t tmp1 = vshll_n_s16(z2_s16, CONST_BITS);
  642. int32x4_t tmp10 = vaddq_s32(tmp0, tmp3);
  643. int32x4_t tmp13 = vsubq_s32(tmp0, tmp3);
  644. int32x4_t tmp11 = vaddq_s32(tmp1, tmp2);
  645. int32x4_t tmp12 = vsubq_s32(tmp1, tmp2);
  646. /* Odd part (tmp0 and tmp1 are both all 0) */
  647. int16x4_t tmp2_s16 = vld1_s16(workspace + 3 * DCTSIZE / 2);
  648. int16x4_t tmp3_s16 = vld1_s16(workspace + 1 * DCTSIZE / 2);
  649. int16x4_t z3_s16 = tmp2_s16;
  650. int16x4_t z4_s16 = tmp3_s16;
  651. int32x4_t z3 = vmull_lane_s16(z3_s16, consts.val[2], 3);
  652. z3 = vmlal_lane_s16(z3, z4_s16, consts.val[1], 3);
  653. int32x4_t z4 = vmull_lane_s16(z3_s16, consts.val[1], 3);
  654. z4 = vmlal_lane_s16(z4, z4_s16, consts.val[2], 0);
  655. tmp0 = vmlsl_lane_s16(z3, tmp3_s16, consts.val[0], 0);
  656. tmp1 = vmlsl_lane_s16(z4, tmp2_s16, consts.val[0], 2);
  657. tmp2 = vmlal_lane_s16(z3, tmp2_s16, consts.val[2], 2);
  658. tmp3 = vmlal_lane_s16(z4, tmp3_s16, consts.val[1], 0);
  659. /* Final output stage: descale and narrow to 16-bit. */
  660. int16x8_t cols_02_s16 = vcombine_s16(vaddhn_s32(tmp10, tmp3),
  661. vaddhn_s32(tmp12, tmp1));
  662. int16x8_t cols_13_s16 = vcombine_s16(vaddhn_s32(tmp11, tmp2),
  663. vaddhn_s32(tmp13, tmp0));
  664. int16x8_t cols_46_s16 = vcombine_s16(vsubhn_s32(tmp13, tmp0),
  665. vsubhn_s32(tmp11, tmp2));
  666. int16x8_t cols_57_s16 = vcombine_s16(vsubhn_s32(tmp12, tmp1),
  667. vsubhn_s32(tmp10, tmp3));
  668. /* Descale and narrow to 8-bit. */
  669. int8x8_t cols_02_s8 = vqrshrn_n_s16(cols_02_s16, DESCALE_P2 - 16);
  670. int8x8_t cols_13_s8 = vqrshrn_n_s16(cols_13_s16, DESCALE_P2 - 16);
  671. int8x8_t cols_46_s8 = vqrshrn_n_s16(cols_46_s16, DESCALE_P2 - 16);
  672. int8x8_t cols_57_s8 = vqrshrn_n_s16(cols_57_s16, DESCALE_P2 - 16);
  673. /* Clamp to range [0-255]. */
  674. uint8x8_t cols_02_u8 = vadd_u8(vreinterpret_u8_s8(cols_02_s8),
  675. vdup_n_u8(CENTERJSAMPLE));
  676. uint8x8_t cols_13_u8 = vadd_u8(vreinterpret_u8_s8(cols_13_s8),
  677. vdup_n_u8(CENTERJSAMPLE));
  678. uint8x8_t cols_46_u8 = vadd_u8(vreinterpret_u8_s8(cols_46_s8),
  679. vdup_n_u8(CENTERJSAMPLE));
  680. uint8x8_t cols_57_u8 = vadd_u8(vreinterpret_u8_s8(cols_57_s8),
  681. vdup_n_u8(CENTERJSAMPLE));
  682. /* Transpose 4x8 block and store to memory. (Zipping adjacent columns
  683. * together allows us to store 16-bit elements.)
  684. */
  685. uint8x8x2_t cols_01_23 = vzip_u8(cols_02_u8, cols_13_u8);
  686. uint8x8x2_t cols_45_67 = vzip_u8(cols_46_u8, cols_57_u8);
  687. uint16x4x4_t cols_01_23_45_67 = { {
  688. vreinterpret_u16_u8(cols_01_23.val[0]),
  689. vreinterpret_u16_u8(cols_01_23.val[1]),
  690. vreinterpret_u16_u8(cols_45_67.val[0]),
  691. vreinterpret_u16_u8(cols_45_67.val[1])
  692. } };
  693. JSAMPROW outptr0 = output_buf[buf_offset + 0] + output_col;
  694. JSAMPROW outptr1 = output_buf[buf_offset + 1] + output_col;
  695. JSAMPROW outptr2 = output_buf[buf_offset + 2] + output_col;
  696. JSAMPROW outptr3 = output_buf[buf_offset + 3] + output_col;
  697. /* VST4 of 16-bit elements completes the transpose. */
  698. vst4_lane_u16((uint16_t *)outptr0, cols_01_23_45_67, 0);
  699. vst4_lane_u16((uint16_t *)outptr1, cols_01_23_45_67, 1);
  700. vst4_lane_u16((uint16_t *)outptr2, cols_01_23_45_67, 2);
  701. vst4_lane_u16((uint16_t *)outptr3, cols_01_23_45_67, 3);
  702. }