jcsample-neon.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * jcsample-neon.c - downsampling (Arm Neon)
  3. *
  4. * Copyright (C) 2020, Arm Limited. All Rights Reserved.
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any damages
  8. * arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute it
  12. * freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. #define JPEG_INTERNALS
  23. #include "../../jinclude.h"
  24. #include "../../jpeglib.h"
  25. #include "../../jsimd.h"
  26. #include "../../jdct.h"
  27. #include "../../jsimddct.h"
  28. #include "../jsimd.h"
  29. #include "align.h"
  30. #include <arm_neon.h>
  31. ALIGN(16) static const uint8_t jsimd_h2_downsample_consts[] = {
  32. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 0 */
  33. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  34. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 1 */
  35. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0E,
  36. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 2 */
  37. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0D, 0x0D,
  38. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 3 */
  39. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0C, 0x0C, 0x0C,
  40. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 4 */
  41. 0x08, 0x09, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
  42. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 5 */
  43. 0x08, 0x09, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
  44. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 6 */
  45. 0x08, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  46. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 7 */
  47. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  48. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* Pad 8 */
  49. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  50. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, /* Pad 9 */
  51. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  52. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x05, 0x05, /* Pad 10 */
  53. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  54. 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x04, 0x04, /* Pad 11 */
  55. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  56. 0x00, 0x01, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, /* Pad 12 */
  57. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  58. 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* Pad 13 */
  59. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  60. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* Pad 14 */
  61. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Pad 15 */
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  64. };
  65. /* Downsample pixel values of a single component.
  66. * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  67. * without smoothing.
  68. */
  69. void jsimd_h2v1_downsample_neon(JDIMENSION image_width, int max_v_samp_factor,
  70. JDIMENSION v_samp_factor,
  71. JDIMENSION width_in_blocks,
  72. JSAMPARRAY input_data, JSAMPARRAY output_data)
  73. {
  74. JSAMPROW inptr, outptr;
  75. /* Load expansion mask to pad remaining elements of last DCT block. */
  76. const int mask_offset = 16 * ((width_in_blocks * 2 * DCTSIZE) - image_width);
  77. const uint8x16_t expand_mask =
  78. vld1q_u8(&jsimd_h2_downsample_consts[mask_offset]);
  79. /* Load bias pattern (alternating every pixel.) */
  80. /* { 0, 1, 0, 1, 0, 1, 0, 1 } */
  81. const uint16x8_t bias = vreinterpretq_u16_u32(vdupq_n_u32(0x00010000));
  82. unsigned i, outrow;
  83. for (outrow = 0; outrow < v_samp_factor; outrow++) {
  84. outptr = output_data[outrow];
  85. inptr = input_data[outrow];
  86. /* Downsample all but the last DCT block of pixels. */
  87. for (i = 0; i < width_in_blocks - 1; i++) {
  88. uint8x16_t pixels = vld1q_u8(inptr + i * 2 * DCTSIZE);
  89. /* Add adjacent pixel values, widen to 16-bit, and add bias. */
  90. uint16x8_t samples_u16 = vpadalq_u8(bias, pixels);
  91. /* Divide total by 2 and narrow to 8-bit. */
  92. uint8x8_t samples_u8 = vshrn_n_u16(samples_u16, 1);
  93. /* Store samples to memory. */
  94. vst1_u8(outptr + i * DCTSIZE, samples_u8);
  95. }
  96. /* Load pixels in last DCT block into a table. */
  97. uint8x16_t pixels = vld1q_u8(inptr + (width_in_blocks - 1) * 2 * DCTSIZE);
  98. #if defined(__aarch64__) || defined(_M_ARM64)
  99. /* Pad the empty elements with the value of the last pixel. */
  100. pixels = vqtbl1q_u8(pixels, expand_mask);
  101. #else
  102. uint8x8x2_t table = { { vget_low_u8(pixels), vget_high_u8(pixels) } };
  103. pixels = vcombine_u8(vtbl2_u8(table, vget_low_u8(expand_mask)),
  104. vtbl2_u8(table, vget_high_u8(expand_mask)));
  105. #endif
  106. /* Add adjacent pixel values, widen to 16-bit, and add bias. */
  107. uint16x8_t samples_u16 = vpadalq_u8(bias, pixels);
  108. /* Divide total by 2, narrow to 8-bit, and store. */
  109. uint8x8_t samples_u8 = vshrn_n_u16(samples_u16, 1);
  110. vst1_u8(outptr + (width_in_blocks - 1) * DCTSIZE, samples_u8);
  111. }
  112. }
  113. /* Downsample pixel values of a single component.
  114. * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  115. * without smoothing.
  116. */
  117. void jsimd_h2v2_downsample_neon(JDIMENSION image_width, int max_v_samp_factor,
  118. JDIMENSION v_samp_factor,
  119. JDIMENSION width_in_blocks,
  120. JSAMPARRAY input_data, JSAMPARRAY output_data)
  121. {
  122. JSAMPROW inptr0, inptr1, outptr;
  123. /* Load expansion mask to pad remaining elements of last DCT block. */
  124. const int mask_offset = 16 * ((width_in_blocks * 2 * DCTSIZE) - image_width);
  125. const uint8x16_t expand_mask =
  126. vld1q_u8(&jsimd_h2_downsample_consts[mask_offset]);
  127. /* Load bias pattern (alternating every pixel.) */
  128. /* { 1, 2, 1, 2, 1, 2, 1, 2 } */
  129. const uint16x8_t bias = vreinterpretq_u16_u32(vdupq_n_u32(0x00020001));
  130. unsigned i, outrow;
  131. for (outrow = 0; outrow < v_samp_factor; outrow++) {
  132. outptr = output_data[outrow];
  133. inptr0 = input_data[outrow];
  134. inptr1 = input_data[outrow + 1];
  135. /* Downsample all but the last DCT block of pixels. */
  136. for (i = 0; i < width_in_blocks - 1; i++) {
  137. uint8x16_t pixels_r0 = vld1q_u8(inptr0 + i * 2 * DCTSIZE);
  138. uint8x16_t pixels_r1 = vld1q_u8(inptr1 + i * 2 * DCTSIZE);
  139. /* Add adjacent pixel values in row 0, widen to 16-bit, and add bias. */
  140. uint16x8_t samples_u16 = vpadalq_u8(bias, pixels_r0);
  141. /* Add adjacent pixel values in row 1, widen to 16-bit, and accumulate.
  142. */
  143. samples_u16 = vpadalq_u8(samples_u16, pixels_r1);
  144. /* Divide total by 4 and narrow to 8-bit. */
  145. uint8x8_t samples_u8 = vshrn_n_u16(samples_u16, 2);
  146. /* Store samples to memory and increment pointers. */
  147. vst1_u8(outptr + i * DCTSIZE, samples_u8);
  148. }
  149. /* Load pixels in last DCT block into a table. */
  150. uint8x16_t pixels_r0 =
  151. vld1q_u8(inptr0 + (width_in_blocks - 1) * 2 * DCTSIZE);
  152. uint8x16_t pixels_r1 =
  153. vld1q_u8(inptr1 + (width_in_blocks - 1) * 2 * DCTSIZE);
  154. #if defined(__aarch64__) || defined(_M_ARM64)
  155. /* Pad the empty elements with the value of the last pixel. */
  156. pixels_r0 = vqtbl1q_u8(pixels_r0, expand_mask);
  157. pixels_r1 = vqtbl1q_u8(pixels_r1, expand_mask);
  158. #else
  159. uint8x8x2_t table_r0 =
  160. { { vget_low_u8(pixels_r0), vget_high_u8(pixels_r0) } };
  161. uint8x8x2_t table_r1 =
  162. { { vget_low_u8(pixels_r1), vget_high_u8(pixels_r1) } };
  163. pixels_r0 = vcombine_u8(vtbl2_u8(table_r0, vget_low_u8(expand_mask)),
  164. vtbl2_u8(table_r0, vget_high_u8(expand_mask)));
  165. pixels_r1 = vcombine_u8(vtbl2_u8(table_r1, vget_low_u8(expand_mask)),
  166. vtbl2_u8(table_r1, vget_high_u8(expand_mask)));
  167. #endif
  168. /* Add adjacent pixel values in row 0, widen to 16-bit, and add bias. */
  169. uint16x8_t samples_u16 = vpadalq_u8(bias, pixels_r0);
  170. /* Add adjacent pixel values in row 1, widen to 16-bit, and accumulate. */
  171. samples_u16 = vpadalq_u8(samples_u16, pixels_r1);
  172. /* Divide total by 4, narrow to 8-bit, and store. */
  173. uint8x8_t samples_u8 = vshrn_n_u16(samples_u16, 2);
  174. vst1_u8(outptr + (width_in_blocks - 1) * DCTSIZE, samples_u8);
  175. }
  176. }