jchuff-neon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * jchuff-neon.c - Huffman entropy encoding (64-bit Arm Neon)
  3. *
  4. * Copyright (C) 2020-2021, Arm Limited. All Rights Reserved.
  5. * Copyright (C) 2020, 2022, 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. * NOTE: All referenced figures are from
  24. * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
  25. */
  26. #define JPEG_INTERNALS
  27. #include "../../../jinclude.h"
  28. #include "../../../jpeglib.h"
  29. #include "../../../jsimd.h"
  30. #include "../../../jdct.h"
  31. #include "../../../jsimddct.h"
  32. #include "../../jsimd.h"
  33. #include "../align.h"
  34. #include "../jchuff.h"
  35. #include "neon-compat.h"
  36. #include <limits.h>
  37. #include <arm_neon.h>
  38. ALIGN(16) static const uint8_t jsimd_huff_encode_one_block_consts[] = {
  39. 0, 1, 2, 3, 16, 17, 32, 33,
  40. 18, 19, 4, 5, 6, 7, 20, 21,
  41. 34, 35, 48, 49, 255, 255, 50, 51,
  42. 36, 37, 22, 23, 8, 9, 10, 11,
  43. 255, 255, 6, 7, 20, 21, 34, 35,
  44. 48, 49, 255, 255, 50, 51, 36, 37,
  45. 54, 55, 40, 41, 26, 27, 12, 13,
  46. 14, 15, 28, 29, 42, 43, 56, 57,
  47. 6, 7, 20, 21, 34, 35, 48, 49,
  48. 50, 51, 36, 37, 22, 23, 8, 9,
  49. 26, 27, 12, 13, 255, 255, 14, 15,
  50. 28, 29, 42, 43, 56, 57, 255, 255,
  51. 52, 53, 54, 55, 40, 41, 26, 27,
  52. 12, 13, 255, 255, 14, 15, 28, 29,
  53. 26, 27, 40, 41, 42, 43, 28, 29,
  54. 14, 15, 30, 31, 44, 45, 46, 47
  55. };
  56. /* The AArch64 implementation of the FLUSH() macro triggers a UBSan misaligned
  57. * address warning because the macro sometimes writes a 64-bit value to a
  58. * non-64-bit-aligned address. That behavior is technically undefined per
  59. * the C specification, but it is supported by the AArch64 architecture and
  60. * compilers.
  61. */
  62. #if defined(__has_feature)
  63. #if __has_feature(undefined_behavior_sanitizer)
  64. __attribute__((no_sanitize("alignment")))
  65. #endif
  66. #endif
  67. JOCTET *jsimd_huff_encode_one_block_neon(void *state, JOCTET *buffer,
  68. JCOEFPTR block, int last_dc_val,
  69. c_derived_tbl *dctbl,
  70. c_derived_tbl *actbl)
  71. {
  72. uint16_t block_diff[DCTSIZE2];
  73. /* Load lookup table indices for rows of zig-zag ordering. */
  74. #ifdef HAVE_VLD1Q_U8_X4
  75. const uint8x16x4_t idx_rows_0123 =
  76. vld1q_u8_x4(jsimd_huff_encode_one_block_consts + 0 * DCTSIZE);
  77. const uint8x16x4_t idx_rows_4567 =
  78. vld1q_u8_x4(jsimd_huff_encode_one_block_consts + 8 * DCTSIZE);
  79. #else
  80. /* GCC does not currently support intrinsics vl1dq_<type>_x4(). */
  81. const uint8x16x4_t idx_rows_0123 = { {
  82. vld1q_u8(jsimd_huff_encode_one_block_consts + 0 * DCTSIZE),
  83. vld1q_u8(jsimd_huff_encode_one_block_consts + 2 * DCTSIZE),
  84. vld1q_u8(jsimd_huff_encode_one_block_consts + 4 * DCTSIZE),
  85. vld1q_u8(jsimd_huff_encode_one_block_consts + 6 * DCTSIZE)
  86. } };
  87. const uint8x16x4_t idx_rows_4567 = { {
  88. vld1q_u8(jsimd_huff_encode_one_block_consts + 8 * DCTSIZE),
  89. vld1q_u8(jsimd_huff_encode_one_block_consts + 10 * DCTSIZE),
  90. vld1q_u8(jsimd_huff_encode_one_block_consts + 12 * DCTSIZE),
  91. vld1q_u8(jsimd_huff_encode_one_block_consts + 14 * DCTSIZE)
  92. } };
  93. #endif
  94. /* Load 8x8 block of DCT coefficients. */
  95. #ifdef HAVE_VLD1Q_U8_X4
  96. const int8x16x4_t tbl_rows_0123 =
  97. vld1q_s8_x4((int8_t *)(block + 0 * DCTSIZE));
  98. const int8x16x4_t tbl_rows_4567 =
  99. vld1q_s8_x4((int8_t *)(block + 4 * DCTSIZE));
  100. #else
  101. const int8x16x4_t tbl_rows_0123 = { {
  102. vld1q_s8((int8_t *)(block + 0 * DCTSIZE)),
  103. vld1q_s8((int8_t *)(block + 1 * DCTSIZE)),
  104. vld1q_s8((int8_t *)(block + 2 * DCTSIZE)),
  105. vld1q_s8((int8_t *)(block + 3 * DCTSIZE))
  106. } };
  107. const int8x16x4_t tbl_rows_4567 = { {
  108. vld1q_s8((int8_t *)(block + 4 * DCTSIZE)),
  109. vld1q_s8((int8_t *)(block + 5 * DCTSIZE)),
  110. vld1q_s8((int8_t *)(block + 6 * DCTSIZE)),
  111. vld1q_s8((int8_t *)(block + 7 * DCTSIZE))
  112. } };
  113. #endif
  114. /* Initialise extra lookup tables. */
  115. const int8x16x4_t tbl_rows_2345 = { {
  116. tbl_rows_0123.val[2], tbl_rows_0123.val[3],
  117. tbl_rows_4567.val[0], tbl_rows_4567.val[1]
  118. } };
  119. const int8x16x3_t tbl_rows_567 =
  120. { { tbl_rows_4567.val[1], tbl_rows_4567.val[2], tbl_rows_4567.val[3] } };
  121. /* Shuffle coefficients into zig-zag order. */
  122. int16x8_t row0 =
  123. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_0123, idx_rows_0123.val[0]));
  124. int16x8_t row1 =
  125. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_0123, idx_rows_0123.val[1]));
  126. int16x8_t row2 =
  127. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_2345, idx_rows_0123.val[2]));
  128. int16x8_t row3 =
  129. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_0123, idx_rows_0123.val[3]));
  130. int16x8_t row4 =
  131. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_4567, idx_rows_4567.val[0]));
  132. int16x8_t row5 =
  133. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_2345, idx_rows_4567.val[1]));
  134. int16x8_t row6 =
  135. vreinterpretq_s16_s8(vqtbl4q_s8(tbl_rows_4567, idx_rows_4567.val[2]));
  136. int16x8_t row7 =
  137. vreinterpretq_s16_s8(vqtbl3q_s8(tbl_rows_567, idx_rows_4567.val[3]));
  138. /* Compute DC coefficient difference value (F.1.1.5.1). */
  139. row0 = vsetq_lane_s16(block[0] - last_dc_val, row0, 0);
  140. /* Initialize AC coefficient lanes not reachable by lookup tables. */
  141. row1 =
  142. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_4567.val[0]),
  143. 0), row1, 2);
  144. row2 =
  145. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_0123.val[1]),
  146. 4), row2, 0);
  147. row2 =
  148. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_4567.val[2]),
  149. 0), row2, 5);
  150. row5 =
  151. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_0123.val[1]),
  152. 7), row5, 2);
  153. row5 =
  154. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_4567.val[2]),
  155. 3), row5, 7);
  156. row6 =
  157. vsetq_lane_s16(vgetq_lane_s16(vreinterpretq_s16_s8(tbl_rows_0123.val[3]),
  158. 7), row6, 5);
  159. /* DCT block is now in zig-zag order; start Huffman encoding process. */
  160. /* Construct bitmap to accelerate encoding of AC coefficients. A set bit
  161. * means that the corresponding coefficient != 0.
  162. */
  163. uint16x8_t row0_ne_0 = vtstq_s16(row0, row0);
  164. uint16x8_t row1_ne_0 = vtstq_s16(row1, row1);
  165. uint16x8_t row2_ne_0 = vtstq_s16(row2, row2);
  166. uint16x8_t row3_ne_0 = vtstq_s16(row3, row3);
  167. uint16x8_t row4_ne_0 = vtstq_s16(row4, row4);
  168. uint16x8_t row5_ne_0 = vtstq_s16(row5, row5);
  169. uint16x8_t row6_ne_0 = vtstq_s16(row6, row6);
  170. uint16x8_t row7_ne_0 = vtstq_s16(row7, row7);
  171. uint8x16_t row10_ne_0 = vuzp1q_u8(vreinterpretq_u8_u16(row1_ne_0),
  172. vreinterpretq_u8_u16(row0_ne_0));
  173. uint8x16_t row32_ne_0 = vuzp1q_u8(vreinterpretq_u8_u16(row3_ne_0),
  174. vreinterpretq_u8_u16(row2_ne_0));
  175. uint8x16_t row54_ne_0 = vuzp1q_u8(vreinterpretq_u8_u16(row5_ne_0),
  176. vreinterpretq_u8_u16(row4_ne_0));
  177. uint8x16_t row76_ne_0 = vuzp1q_u8(vreinterpretq_u8_u16(row7_ne_0),
  178. vreinterpretq_u8_u16(row6_ne_0));
  179. /* { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 } */
  180. const uint8x16_t bitmap_mask =
  181. vreinterpretq_u8_u64(vdupq_n_u64(0x0102040810204080));
  182. uint8x16_t bitmap_rows_10 = vandq_u8(row10_ne_0, bitmap_mask);
  183. uint8x16_t bitmap_rows_32 = vandq_u8(row32_ne_0, bitmap_mask);
  184. uint8x16_t bitmap_rows_54 = vandq_u8(row54_ne_0, bitmap_mask);
  185. uint8x16_t bitmap_rows_76 = vandq_u8(row76_ne_0, bitmap_mask);
  186. uint8x16_t bitmap_rows_3210 = vpaddq_u8(bitmap_rows_32, bitmap_rows_10);
  187. uint8x16_t bitmap_rows_7654 = vpaddq_u8(bitmap_rows_76, bitmap_rows_54);
  188. uint8x16_t bitmap_rows_76543210 = vpaddq_u8(bitmap_rows_7654,
  189. bitmap_rows_3210);
  190. uint8x8_t bitmap_all = vpadd_u8(vget_low_u8(bitmap_rows_76543210),
  191. vget_high_u8(bitmap_rows_76543210));
  192. /* Shift left to remove DC bit. */
  193. bitmap_all =
  194. vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(bitmap_all), 1));
  195. /* Count bits set (number of non-zero coefficients) in bitmap. */
  196. unsigned int non_zero_coefficients = vaddv_u8(vcnt_u8(bitmap_all));
  197. /* Move bitmap to 64-bit scalar register. */
  198. uint64_t bitmap = vget_lane_u64(vreinterpret_u64_u8(bitmap_all), 0);
  199. /* Set up state and bit buffer for output bitstream. */
  200. working_state *state_ptr = (working_state *)state;
  201. int free_bits = state_ptr->cur.free_bits;
  202. size_t put_buffer = state_ptr->cur.put_buffer;
  203. /* Encode DC coefficient. */
  204. /* For negative coeffs: diff = abs(coeff) -1 = ~abs(coeff) */
  205. int16x8_t abs_row0 = vabsq_s16(row0);
  206. int16x8_t row0_lz = vclzq_s16(abs_row0);
  207. uint16x8_t row0_mask = vshlq_u16(vcltzq_s16(row0), vnegq_s16(row0_lz));
  208. uint16x8_t row0_diff = veorq_u16(vreinterpretq_u16_s16(abs_row0), row0_mask);
  209. /* Find nbits required to specify sign and amplitude of coefficient. */
  210. unsigned int lz = vgetq_lane_u16(vreinterpretq_u16_s16(row0_lz), 0);
  211. unsigned int nbits = 16 - lz;
  212. /* Emit Huffman-coded symbol and additional diff bits. */
  213. unsigned int diff = vgetq_lane_u16(row0_diff, 0);
  214. PUT_CODE(dctbl->ehufco[nbits], dctbl->ehufsi[nbits], diff)
  215. /* Encode AC coefficients. */
  216. unsigned int r = 0; /* r = run length of zeros */
  217. unsigned int i = 1; /* i = number of coefficients encoded */
  218. /* Code and size information for a run length of 16 zero coefficients */
  219. const unsigned int code_0xf0 = actbl->ehufco[0xf0];
  220. const unsigned int size_0xf0 = actbl->ehufsi[0xf0];
  221. /* The most efficient method of computing nbits and diff depends on the
  222. * number of non-zero coefficients. If the bitmap is not too sparse (> 8
  223. * non-zero AC coefficients), it is beneficial to do all of the work using
  224. * Neon; else we do some of the work using Neon and the rest on demand using
  225. * scalar code.
  226. */
  227. if (non_zero_coefficients > 8) {
  228. uint8_t block_nbits[DCTSIZE2];
  229. int16x8_t abs_row1 = vabsq_s16(row1);
  230. int16x8_t abs_row2 = vabsq_s16(row2);
  231. int16x8_t abs_row3 = vabsq_s16(row3);
  232. int16x8_t abs_row4 = vabsq_s16(row4);
  233. int16x8_t abs_row5 = vabsq_s16(row5);
  234. int16x8_t abs_row6 = vabsq_s16(row6);
  235. int16x8_t abs_row7 = vabsq_s16(row7);
  236. int16x8_t row1_lz = vclzq_s16(abs_row1);
  237. int16x8_t row2_lz = vclzq_s16(abs_row2);
  238. int16x8_t row3_lz = vclzq_s16(abs_row3);
  239. int16x8_t row4_lz = vclzq_s16(abs_row4);
  240. int16x8_t row5_lz = vclzq_s16(abs_row5);
  241. int16x8_t row6_lz = vclzq_s16(abs_row6);
  242. int16x8_t row7_lz = vclzq_s16(abs_row7);
  243. /* Narrow leading zero count to 8 bits. */
  244. uint8x16_t row01_lz = vuzp1q_u8(vreinterpretq_u8_s16(row0_lz),
  245. vreinterpretq_u8_s16(row1_lz));
  246. uint8x16_t row23_lz = vuzp1q_u8(vreinterpretq_u8_s16(row2_lz),
  247. vreinterpretq_u8_s16(row3_lz));
  248. uint8x16_t row45_lz = vuzp1q_u8(vreinterpretq_u8_s16(row4_lz),
  249. vreinterpretq_u8_s16(row5_lz));
  250. uint8x16_t row67_lz = vuzp1q_u8(vreinterpretq_u8_s16(row6_lz),
  251. vreinterpretq_u8_s16(row7_lz));
  252. /* Compute nbits needed to specify magnitude of each coefficient. */
  253. uint8x16_t row01_nbits = vsubq_u8(vdupq_n_u8(16), row01_lz);
  254. uint8x16_t row23_nbits = vsubq_u8(vdupq_n_u8(16), row23_lz);
  255. uint8x16_t row45_nbits = vsubq_u8(vdupq_n_u8(16), row45_lz);
  256. uint8x16_t row67_nbits = vsubq_u8(vdupq_n_u8(16), row67_lz);
  257. /* Store nbits. */
  258. vst1q_u8(block_nbits + 0 * DCTSIZE, row01_nbits);
  259. vst1q_u8(block_nbits + 2 * DCTSIZE, row23_nbits);
  260. vst1q_u8(block_nbits + 4 * DCTSIZE, row45_nbits);
  261. vst1q_u8(block_nbits + 6 * DCTSIZE, row67_nbits);
  262. /* Mask bits not required to specify sign and amplitude of diff. */
  263. uint16x8_t row1_mask = vshlq_u16(vcltzq_s16(row1), vnegq_s16(row1_lz));
  264. uint16x8_t row2_mask = vshlq_u16(vcltzq_s16(row2), vnegq_s16(row2_lz));
  265. uint16x8_t row3_mask = vshlq_u16(vcltzq_s16(row3), vnegq_s16(row3_lz));
  266. uint16x8_t row4_mask = vshlq_u16(vcltzq_s16(row4), vnegq_s16(row4_lz));
  267. uint16x8_t row5_mask = vshlq_u16(vcltzq_s16(row5), vnegq_s16(row5_lz));
  268. uint16x8_t row6_mask = vshlq_u16(vcltzq_s16(row6), vnegq_s16(row6_lz));
  269. uint16x8_t row7_mask = vshlq_u16(vcltzq_s16(row7), vnegq_s16(row7_lz));
  270. /* diff = abs(coeff) ^ sign(coeff) [no-op for positive coefficients] */
  271. uint16x8_t row1_diff = veorq_u16(vreinterpretq_u16_s16(abs_row1),
  272. row1_mask);
  273. uint16x8_t row2_diff = veorq_u16(vreinterpretq_u16_s16(abs_row2),
  274. row2_mask);
  275. uint16x8_t row3_diff = veorq_u16(vreinterpretq_u16_s16(abs_row3),
  276. row3_mask);
  277. uint16x8_t row4_diff = veorq_u16(vreinterpretq_u16_s16(abs_row4),
  278. row4_mask);
  279. uint16x8_t row5_diff = veorq_u16(vreinterpretq_u16_s16(abs_row5),
  280. row5_mask);
  281. uint16x8_t row6_diff = veorq_u16(vreinterpretq_u16_s16(abs_row6),
  282. row6_mask);
  283. uint16x8_t row7_diff = veorq_u16(vreinterpretq_u16_s16(abs_row7),
  284. row7_mask);
  285. /* Store diff bits. */
  286. vst1q_u16(block_diff + 0 * DCTSIZE, row0_diff);
  287. vst1q_u16(block_diff + 1 * DCTSIZE, row1_diff);
  288. vst1q_u16(block_diff + 2 * DCTSIZE, row2_diff);
  289. vst1q_u16(block_diff + 3 * DCTSIZE, row3_diff);
  290. vst1q_u16(block_diff + 4 * DCTSIZE, row4_diff);
  291. vst1q_u16(block_diff + 5 * DCTSIZE, row5_diff);
  292. vst1q_u16(block_diff + 6 * DCTSIZE, row6_diff);
  293. vst1q_u16(block_diff + 7 * DCTSIZE, row7_diff);
  294. while (bitmap != 0) {
  295. r = BUILTIN_CLZLL(bitmap);
  296. i += r;
  297. bitmap <<= r;
  298. nbits = block_nbits[i];
  299. diff = block_diff[i];
  300. while (r > 15) {
  301. /* If run length > 15, emit special run-length-16 codes. */
  302. PUT_BITS(code_0xf0, size_0xf0)
  303. r -= 16;
  304. }
  305. /* Emit Huffman symbol for run length / number of bits. (F.1.2.2.1) */
  306. unsigned int rs = (r << 4) + nbits;
  307. PUT_CODE(actbl->ehufco[rs], actbl->ehufsi[rs], diff)
  308. i++;
  309. bitmap <<= 1;
  310. }
  311. } else if (bitmap != 0) {
  312. uint16_t block_abs[DCTSIZE2];
  313. /* Compute and store absolute value of coefficients. */
  314. int16x8_t abs_row1 = vabsq_s16(row1);
  315. int16x8_t abs_row2 = vabsq_s16(row2);
  316. int16x8_t abs_row3 = vabsq_s16(row3);
  317. int16x8_t abs_row4 = vabsq_s16(row4);
  318. int16x8_t abs_row5 = vabsq_s16(row5);
  319. int16x8_t abs_row6 = vabsq_s16(row6);
  320. int16x8_t abs_row7 = vabsq_s16(row7);
  321. vst1q_u16(block_abs + 0 * DCTSIZE, vreinterpretq_u16_s16(abs_row0));
  322. vst1q_u16(block_abs + 1 * DCTSIZE, vreinterpretq_u16_s16(abs_row1));
  323. vst1q_u16(block_abs + 2 * DCTSIZE, vreinterpretq_u16_s16(abs_row2));
  324. vst1q_u16(block_abs + 3 * DCTSIZE, vreinterpretq_u16_s16(abs_row3));
  325. vst1q_u16(block_abs + 4 * DCTSIZE, vreinterpretq_u16_s16(abs_row4));
  326. vst1q_u16(block_abs + 5 * DCTSIZE, vreinterpretq_u16_s16(abs_row5));
  327. vst1q_u16(block_abs + 6 * DCTSIZE, vreinterpretq_u16_s16(abs_row6));
  328. vst1q_u16(block_abs + 7 * DCTSIZE, vreinterpretq_u16_s16(abs_row7));
  329. /* Compute diff bits (without nbits mask) and store. */
  330. uint16x8_t row1_diff = veorq_u16(vreinterpretq_u16_s16(abs_row1),
  331. vcltzq_s16(row1));
  332. uint16x8_t row2_diff = veorq_u16(vreinterpretq_u16_s16(abs_row2),
  333. vcltzq_s16(row2));
  334. uint16x8_t row3_diff = veorq_u16(vreinterpretq_u16_s16(abs_row3),
  335. vcltzq_s16(row3));
  336. uint16x8_t row4_diff = veorq_u16(vreinterpretq_u16_s16(abs_row4),
  337. vcltzq_s16(row4));
  338. uint16x8_t row5_diff = veorq_u16(vreinterpretq_u16_s16(abs_row5),
  339. vcltzq_s16(row5));
  340. uint16x8_t row6_diff = veorq_u16(vreinterpretq_u16_s16(abs_row6),
  341. vcltzq_s16(row6));
  342. uint16x8_t row7_diff = veorq_u16(vreinterpretq_u16_s16(abs_row7),
  343. vcltzq_s16(row7));
  344. vst1q_u16(block_diff + 0 * DCTSIZE, row0_diff);
  345. vst1q_u16(block_diff + 1 * DCTSIZE, row1_diff);
  346. vst1q_u16(block_diff + 2 * DCTSIZE, row2_diff);
  347. vst1q_u16(block_diff + 3 * DCTSIZE, row3_diff);
  348. vst1q_u16(block_diff + 4 * DCTSIZE, row4_diff);
  349. vst1q_u16(block_diff + 5 * DCTSIZE, row5_diff);
  350. vst1q_u16(block_diff + 6 * DCTSIZE, row6_diff);
  351. vst1q_u16(block_diff + 7 * DCTSIZE, row7_diff);
  352. /* Same as above but must mask diff bits and compute nbits on demand. */
  353. while (bitmap != 0) {
  354. r = BUILTIN_CLZLL(bitmap);
  355. i += r;
  356. bitmap <<= r;
  357. lz = BUILTIN_CLZ(block_abs[i]);
  358. nbits = 32 - lz;
  359. diff = ((unsigned int)block_diff[i] << lz) >> lz;
  360. while (r > 15) {
  361. /* If run length > 15, emit special run-length-16 codes. */
  362. PUT_BITS(code_0xf0, size_0xf0)
  363. r -= 16;
  364. }
  365. /* Emit Huffman symbol for run length / number of bits. (F.1.2.2.1) */
  366. unsigned int rs = (r << 4) + nbits;
  367. PUT_CODE(actbl->ehufco[rs], actbl->ehufsi[rs], diff)
  368. i++;
  369. bitmap <<= 1;
  370. }
  371. }
  372. /* If the last coefficient(s) were zero, emit an end-of-block (EOB) code.
  373. * The value of RS for the EOB code is 0.
  374. */
  375. if (i != 64) {
  376. PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0])
  377. }
  378. state_ptr->cur.put_buffer = put_buffer;
  379. state_ptr->cur.free_bits = free_bits;
  380. return buffer;
  381. }