jccolext-neon.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * jccolext-neon.c - colorspace conversion (32-bit Arm Neon)
  3. *
  4. * Copyright (C) 2020, Arm Limited. All Rights Reserved.
  5. * Copyright (C) 2020, 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. /* This file is included by jccolor-neon.c */
  24. /* RGB -> YCbCr conversion is defined by the following equations:
  25. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  26. * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128
  27. * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128
  28. *
  29. * Avoid floating point arithmetic by using shifted integer constants:
  30. * 0.29899597 = 19595 * 2^-16
  31. * 0.58700561 = 38470 * 2^-16
  32. * 0.11399841 = 7471 * 2^-16
  33. * 0.16874695 = 11059 * 2^-16
  34. * 0.33125305 = 21709 * 2^-16
  35. * 0.50000000 = 32768 * 2^-16
  36. * 0.41868592 = 27439 * 2^-16
  37. * 0.08131409 = 5329 * 2^-16
  38. * These constants are defined in jccolor-neon.c
  39. *
  40. * We add the fixed-point equivalent of 0.5 to Cb and Cr, which effectively
  41. * rounds up or down the result via integer truncation.
  42. */
  43. void jsimd_rgb_ycc_convert_neon(JDIMENSION image_width, JSAMPARRAY input_buf,
  44. JSAMPIMAGE output_buf, JDIMENSION output_row,
  45. int num_rows)
  46. {
  47. /* Pointer to RGB(X/A) input data */
  48. JSAMPROW inptr;
  49. /* Pointers to Y, Cb, and Cr output data */
  50. JSAMPROW outptr0, outptr1, outptr2;
  51. /* Allocate temporary buffer for final (image_width % 8) pixels in row. */
  52. ALIGN(16) uint8_t tmp_buf[8 * RGB_PIXELSIZE];
  53. /* Set up conversion constants. */
  54. #ifdef HAVE_VLD1_U16_X2
  55. const uint16x4x2_t consts = vld1_u16_x2(jsimd_rgb_ycc_neon_consts);
  56. #else
  57. /* GCC does not currently support the intrinsic vld1_<type>_x2(). */
  58. const uint16x4_t consts1 = vld1_u16(jsimd_rgb_ycc_neon_consts);
  59. const uint16x4_t consts2 = vld1_u16(jsimd_rgb_ycc_neon_consts + 4);
  60. const uint16x4x2_t consts = { { consts1, consts2 } };
  61. #endif
  62. const uint32x4_t scaled_128_5 = vdupq_n_u32((128 << 16) + 32767);
  63. while (--num_rows >= 0) {
  64. inptr = *input_buf++;
  65. outptr0 = output_buf[0][output_row];
  66. outptr1 = output_buf[1][output_row];
  67. outptr2 = output_buf[2][output_row];
  68. output_row++;
  69. int cols_remaining = image_width;
  70. for (; cols_remaining > 0; cols_remaining -= 8) {
  71. /* To prevent buffer overread by the vector load instructions, the last
  72. * (image_width % 8) columns of data are first memcopied to a temporary
  73. * buffer large enough to accommodate the vector load.
  74. */
  75. if (cols_remaining < 8) {
  76. memcpy(tmp_buf, inptr, cols_remaining * RGB_PIXELSIZE);
  77. inptr = tmp_buf;
  78. }
  79. #if RGB_PIXELSIZE == 4
  80. uint8x8x4_t input_pixels = vld4_u8(inptr);
  81. #else
  82. uint8x8x3_t input_pixels = vld3_u8(inptr);
  83. #endif
  84. uint16x8_t r = vmovl_u8(input_pixels.val[RGB_RED]);
  85. uint16x8_t g = vmovl_u8(input_pixels.val[RGB_GREEN]);
  86. uint16x8_t b = vmovl_u8(input_pixels.val[RGB_BLUE]);
  87. /* Compute Y = 0.29900 * R + 0.58700 * G + 0.11400 * B */
  88. uint32x4_t y_low = vmull_lane_u16(vget_low_u16(r), consts.val[0], 0);
  89. y_low = vmlal_lane_u16(y_low, vget_low_u16(g), consts.val[0], 1);
  90. y_low = vmlal_lane_u16(y_low, vget_low_u16(b), consts.val[0], 2);
  91. uint32x4_t y_high = vmull_lane_u16(vget_high_u16(r), consts.val[0], 0);
  92. y_high = vmlal_lane_u16(y_high, vget_high_u16(g), consts.val[0], 1);
  93. y_high = vmlal_lane_u16(y_high, vget_high_u16(b), consts.val[0], 2);
  94. /* Compute Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128 */
  95. uint32x4_t cb_low = scaled_128_5;
  96. cb_low = vmlsl_lane_u16(cb_low, vget_low_u16(r), consts.val[0], 3);
  97. cb_low = vmlsl_lane_u16(cb_low, vget_low_u16(g), consts.val[1], 0);
  98. cb_low = vmlal_lane_u16(cb_low, vget_low_u16(b), consts.val[1], 1);
  99. uint32x4_t cb_high = scaled_128_5;
  100. cb_high = vmlsl_lane_u16(cb_high, vget_high_u16(r), consts.val[0], 3);
  101. cb_high = vmlsl_lane_u16(cb_high, vget_high_u16(g), consts.val[1], 0);
  102. cb_high = vmlal_lane_u16(cb_high, vget_high_u16(b), consts.val[1], 1);
  103. /* Compute Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128 */
  104. uint32x4_t cr_low = scaled_128_5;
  105. cr_low = vmlal_lane_u16(cr_low, vget_low_u16(r), consts.val[1], 1);
  106. cr_low = vmlsl_lane_u16(cr_low, vget_low_u16(g), consts.val[1], 2);
  107. cr_low = vmlsl_lane_u16(cr_low, vget_low_u16(b), consts.val[1], 3);
  108. uint32x4_t cr_high = scaled_128_5;
  109. cr_high = vmlal_lane_u16(cr_high, vget_high_u16(r), consts.val[1], 1);
  110. cr_high = vmlsl_lane_u16(cr_high, vget_high_u16(g), consts.val[1], 2);
  111. cr_high = vmlsl_lane_u16(cr_high, vget_high_u16(b), consts.val[1], 3);
  112. /* Descale Y values (rounding right shift) and narrow to 16-bit. */
  113. uint16x8_t y_u16 = vcombine_u16(vrshrn_n_u32(y_low, 16),
  114. vrshrn_n_u32(y_high, 16));
  115. /* Descale Cb values (right shift) and narrow to 16-bit. */
  116. uint16x8_t cb_u16 = vcombine_u16(vshrn_n_u32(cb_low, 16),
  117. vshrn_n_u32(cb_high, 16));
  118. /* Descale Cr values (right shift) and narrow to 16-bit. */
  119. uint16x8_t cr_u16 = vcombine_u16(vshrn_n_u32(cr_low, 16),
  120. vshrn_n_u32(cr_high, 16));
  121. /* Narrow Y, Cb, and Cr values to 8-bit and store to memory. Buffer
  122. * overwrite is permitted up to the next multiple of ALIGN_SIZE bytes.
  123. */
  124. vst1_u8(outptr0, vmovn_u16(y_u16));
  125. vst1_u8(outptr1, vmovn_u16(cb_u16));
  126. vst1_u8(outptr2, vmovn_u16(cr_u16));
  127. /* Increment pointers. */
  128. inptr += (8 * RGB_PIXELSIZE);
  129. outptr0 += 8;
  130. outptr1 += 8;
  131. outptr2 += 8;
  132. }
  133. }
  134. }