1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker * jccolext-neon.c - colorspace conversion (32-bit Arm Neon)
3*dfc6aa5cSAndroid Build Coastguard Worker *
4*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2020, Arm Limited. All Rights Reserved.
5*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2020, D. R. Commander. All Rights Reserved.
6*dfc6aa5cSAndroid Build Coastguard Worker *
7*dfc6aa5cSAndroid Build Coastguard Worker * This software is provided 'as-is', without any express or implied
8*dfc6aa5cSAndroid Build Coastguard Worker * warranty. In no event will the authors be held liable for any damages
9*dfc6aa5cSAndroid Build Coastguard Worker * arising from the use of this software.
10*dfc6aa5cSAndroid Build Coastguard Worker *
11*dfc6aa5cSAndroid Build Coastguard Worker * Permission is granted to anyone to use this software for any purpose,
12*dfc6aa5cSAndroid Build Coastguard Worker * including commercial applications, and to alter it and redistribute it
13*dfc6aa5cSAndroid Build Coastguard Worker * freely, subject to the following restrictions:
14*dfc6aa5cSAndroid Build Coastguard Worker *
15*dfc6aa5cSAndroid Build Coastguard Worker * 1. The origin of this software must not be misrepresented; you must not
16*dfc6aa5cSAndroid Build Coastguard Worker * claim that you wrote the original software. If you use this software
17*dfc6aa5cSAndroid Build Coastguard Worker * in a product, an acknowledgment in the product documentation would be
18*dfc6aa5cSAndroid Build Coastguard Worker * appreciated but is not required.
19*dfc6aa5cSAndroid Build Coastguard Worker * 2. Altered source versions must be plainly marked as such, and must not be
20*dfc6aa5cSAndroid Build Coastguard Worker * misrepresented as being the original software.
21*dfc6aa5cSAndroid Build Coastguard Worker * 3. This notice may not be removed or altered from any source distribution.
22*dfc6aa5cSAndroid Build Coastguard Worker */
23*dfc6aa5cSAndroid Build Coastguard Worker
24*dfc6aa5cSAndroid Build Coastguard Worker /* This file is included by jccolor-neon.c */
25*dfc6aa5cSAndroid Build Coastguard Worker
26*dfc6aa5cSAndroid Build Coastguard Worker
27*dfc6aa5cSAndroid Build Coastguard Worker /* RGB -> YCbCr conversion is defined by the following equations:
28*dfc6aa5cSAndroid Build Coastguard Worker * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
29*dfc6aa5cSAndroid Build Coastguard Worker * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128
30*dfc6aa5cSAndroid Build Coastguard Worker * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128
31*dfc6aa5cSAndroid Build Coastguard Worker *
32*dfc6aa5cSAndroid Build Coastguard Worker * Avoid floating point arithmetic by using shifted integer constants:
33*dfc6aa5cSAndroid Build Coastguard Worker * 0.29899597 = 19595 * 2^-16
34*dfc6aa5cSAndroid Build Coastguard Worker * 0.58700561 = 38470 * 2^-16
35*dfc6aa5cSAndroid Build Coastguard Worker * 0.11399841 = 7471 * 2^-16
36*dfc6aa5cSAndroid Build Coastguard Worker * 0.16874695 = 11059 * 2^-16
37*dfc6aa5cSAndroid Build Coastguard Worker * 0.33125305 = 21709 * 2^-16
38*dfc6aa5cSAndroid Build Coastguard Worker * 0.50000000 = 32768 * 2^-16
39*dfc6aa5cSAndroid Build Coastguard Worker * 0.41868592 = 27439 * 2^-16
40*dfc6aa5cSAndroid Build Coastguard Worker * 0.08131409 = 5329 * 2^-16
41*dfc6aa5cSAndroid Build Coastguard Worker * These constants are defined in jccolor-neon.c
42*dfc6aa5cSAndroid Build Coastguard Worker *
43*dfc6aa5cSAndroid Build Coastguard Worker * We add the fixed-point equivalent of 0.5 to Cb and Cr, which effectively
44*dfc6aa5cSAndroid Build Coastguard Worker * rounds up or down the result via integer truncation.
45*dfc6aa5cSAndroid Build Coastguard Worker */
46*dfc6aa5cSAndroid Build Coastguard Worker
jsimd_rgb_ycc_convert_neon(JDIMENSION image_width,JSAMPARRAY input_buf,JSAMPIMAGE output_buf,JDIMENSION output_row,int num_rows)47*dfc6aa5cSAndroid Build Coastguard Worker void jsimd_rgb_ycc_convert_neon(JDIMENSION image_width, JSAMPARRAY input_buf,
48*dfc6aa5cSAndroid Build Coastguard Worker JSAMPIMAGE output_buf, JDIMENSION output_row,
49*dfc6aa5cSAndroid Build Coastguard Worker int num_rows)
50*dfc6aa5cSAndroid Build Coastguard Worker {
51*dfc6aa5cSAndroid Build Coastguard Worker /* Pointer to RGB(X/A) input data */
52*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW inptr;
53*dfc6aa5cSAndroid Build Coastguard Worker /* Pointers to Y, Cb, and Cr output data */
54*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW outptr0, outptr1, outptr2;
55*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate temporary buffer for final (image_width % 8) pixels in row. */
56*dfc6aa5cSAndroid Build Coastguard Worker ALIGN(16) uint8_t tmp_buf[8 * RGB_PIXELSIZE];
57*dfc6aa5cSAndroid Build Coastguard Worker
58*dfc6aa5cSAndroid Build Coastguard Worker /* Set up conversion constants. */
59*dfc6aa5cSAndroid Build Coastguard Worker #ifdef HAVE_VLD1_U16_X2
60*dfc6aa5cSAndroid Build Coastguard Worker const uint16x4x2_t consts = vld1_u16_x2(jsimd_rgb_ycc_neon_consts);
61*dfc6aa5cSAndroid Build Coastguard Worker #else
62*dfc6aa5cSAndroid Build Coastguard Worker /* GCC does not currently support the intrinsic vld1_<type>_x2(). */
63*dfc6aa5cSAndroid Build Coastguard Worker const uint16x4_t consts1 = vld1_u16(jsimd_rgb_ycc_neon_consts);
64*dfc6aa5cSAndroid Build Coastguard Worker const uint16x4_t consts2 = vld1_u16(jsimd_rgb_ycc_neon_consts + 4);
65*dfc6aa5cSAndroid Build Coastguard Worker const uint16x4x2_t consts = { { consts1, consts2 } };
66*dfc6aa5cSAndroid Build Coastguard Worker #endif
67*dfc6aa5cSAndroid Build Coastguard Worker const uint32x4_t scaled_128_5 = vdupq_n_u32((128 << 16) + 32767);
68*dfc6aa5cSAndroid Build Coastguard Worker
69*dfc6aa5cSAndroid Build Coastguard Worker while (--num_rows >= 0) {
70*dfc6aa5cSAndroid Build Coastguard Worker inptr = *input_buf++;
71*dfc6aa5cSAndroid Build Coastguard Worker outptr0 = output_buf[0][output_row];
72*dfc6aa5cSAndroid Build Coastguard Worker outptr1 = output_buf[1][output_row];
73*dfc6aa5cSAndroid Build Coastguard Worker outptr2 = output_buf[2][output_row];
74*dfc6aa5cSAndroid Build Coastguard Worker output_row++;
75*dfc6aa5cSAndroid Build Coastguard Worker
76*dfc6aa5cSAndroid Build Coastguard Worker int cols_remaining = image_width;
77*dfc6aa5cSAndroid Build Coastguard Worker for (; cols_remaining > 0; cols_remaining -= 8) {
78*dfc6aa5cSAndroid Build Coastguard Worker
79*dfc6aa5cSAndroid Build Coastguard Worker /* To prevent buffer overread by the vector load instructions, the last
80*dfc6aa5cSAndroid Build Coastguard Worker * (image_width % 8) columns of data are first memcopied to a temporary
81*dfc6aa5cSAndroid Build Coastguard Worker * buffer large enough to accommodate the vector load.
82*dfc6aa5cSAndroid Build Coastguard Worker */
83*dfc6aa5cSAndroid Build Coastguard Worker if (cols_remaining < 8) {
84*dfc6aa5cSAndroid Build Coastguard Worker memcpy(tmp_buf, inptr, cols_remaining * RGB_PIXELSIZE);
85*dfc6aa5cSAndroid Build Coastguard Worker inptr = tmp_buf;
86*dfc6aa5cSAndroid Build Coastguard Worker }
87*dfc6aa5cSAndroid Build Coastguard Worker
88*dfc6aa5cSAndroid Build Coastguard Worker #if RGB_PIXELSIZE == 4
89*dfc6aa5cSAndroid Build Coastguard Worker uint8x8x4_t input_pixels = vld4_u8(inptr);
90*dfc6aa5cSAndroid Build Coastguard Worker #else
91*dfc6aa5cSAndroid Build Coastguard Worker uint8x8x3_t input_pixels = vld3_u8(inptr);
92*dfc6aa5cSAndroid Build Coastguard Worker #endif
93*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t r = vmovl_u8(input_pixels.val[RGB_RED]);
94*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t g = vmovl_u8(input_pixels.val[RGB_GREEN]);
95*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t b = vmovl_u8(input_pixels.val[RGB_BLUE]);
96*dfc6aa5cSAndroid Build Coastguard Worker
97*dfc6aa5cSAndroid Build Coastguard Worker /* Compute Y = 0.29900 * R + 0.58700 * G + 0.11400 * B */
98*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_low = vmull_lane_u16(vget_low_u16(r), consts.val[0], 0);
99*dfc6aa5cSAndroid Build Coastguard Worker y_low = vmlal_lane_u16(y_low, vget_low_u16(g), consts.val[0], 1);
100*dfc6aa5cSAndroid Build Coastguard Worker y_low = vmlal_lane_u16(y_low, vget_low_u16(b), consts.val[0], 2);
101*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_high = vmull_lane_u16(vget_high_u16(r), consts.val[0], 0);
102*dfc6aa5cSAndroid Build Coastguard Worker y_high = vmlal_lane_u16(y_high, vget_high_u16(g), consts.val[0], 1);
103*dfc6aa5cSAndroid Build Coastguard Worker y_high = vmlal_lane_u16(y_high, vget_high_u16(b), consts.val[0], 2);
104*dfc6aa5cSAndroid Build Coastguard Worker
105*dfc6aa5cSAndroid Build Coastguard Worker /* Compute Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128 */
106*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t cb_low = scaled_128_5;
107*dfc6aa5cSAndroid Build Coastguard Worker cb_low = vmlsl_lane_u16(cb_low, vget_low_u16(r), consts.val[0], 3);
108*dfc6aa5cSAndroid Build Coastguard Worker cb_low = vmlsl_lane_u16(cb_low, vget_low_u16(g), consts.val[1], 0);
109*dfc6aa5cSAndroid Build Coastguard Worker cb_low = vmlal_lane_u16(cb_low, vget_low_u16(b), consts.val[1], 1);
110*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t cb_high = scaled_128_5;
111*dfc6aa5cSAndroid Build Coastguard Worker cb_high = vmlsl_lane_u16(cb_high, vget_high_u16(r), consts.val[0], 3);
112*dfc6aa5cSAndroid Build Coastguard Worker cb_high = vmlsl_lane_u16(cb_high, vget_high_u16(g), consts.val[1], 0);
113*dfc6aa5cSAndroid Build Coastguard Worker cb_high = vmlal_lane_u16(cb_high, vget_high_u16(b), consts.val[1], 1);
114*dfc6aa5cSAndroid Build Coastguard Worker
115*dfc6aa5cSAndroid Build Coastguard Worker /* Compute Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128 */
116*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t cr_low = scaled_128_5;
117*dfc6aa5cSAndroid Build Coastguard Worker cr_low = vmlal_lane_u16(cr_low, vget_low_u16(r), consts.val[1], 1);
118*dfc6aa5cSAndroid Build Coastguard Worker cr_low = vmlsl_lane_u16(cr_low, vget_low_u16(g), consts.val[1], 2);
119*dfc6aa5cSAndroid Build Coastguard Worker cr_low = vmlsl_lane_u16(cr_low, vget_low_u16(b), consts.val[1], 3);
120*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t cr_high = scaled_128_5;
121*dfc6aa5cSAndroid Build Coastguard Worker cr_high = vmlal_lane_u16(cr_high, vget_high_u16(r), consts.val[1], 1);
122*dfc6aa5cSAndroid Build Coastguard Worker cr_high = vmlsl_lane_u16(cr_high, vget_high_u16(g), consts.val[1], 2);
123*dfc6aa5cSAndroid Build Coastguard Worker cr_high = vmlsl_lane_u16(cr_high, vget_high_u16(b), consts.val[1], 3);
124*dfc6aa5cSAndroid Build Coastguard Worker
125*dfc6aa5cSAndroid Build Coastguard Worker /* Descale Y values (rounding right shift) and narrow to 16-bit. */
126*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t y_u16 = vcombine_u16(vrshrn_n_u32(y_low, 16),
127*dfc6aa5cSAndroid Build Coastguard Worker vrshrn_n_u32(y_high, 16));
128*dfc6aa5cSAndroid Build Coastguard Worker /* Descale Cb values (right shift) and narrow to 16-bit. */
129*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t cb_u16 = vcombine_u16(vshrn_n_u32(cb_low, 16),
130*dfc6aa5cSAndroid Build Coastguard Worker vshrn_n_u32(cb_high, 16));
131*dfc6aa5cSAndroid Build Coastguard Worker /* Descale Cr values (right shift) and narrow to 16-bit. */
132*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t cr_u16 = vcombine_u16(vshrn_n_u32(cr_low, 16),
133*dfc6aa5cSAndroid Build Coastguard Worker vshrn_n_u32(cr_high, 16));
134*dfc6aa5cSAndroid Build Coastguard Worker /* Narrow Y, Cb, and Cr values to 8-bit and store to memory. Buffer
135*dfc6aa5cSAndroid Build Coastguard Worker * overwrite is permitted up to the next multiple of ALIGN_SIZE bytes.
136*dfc6aa5cSAndroid Build Coastguard Worker */
137*dfc6aa5cSAndroid Build Coastguard Worker vst1_u8(outptr0, vmovn_u16(y_u16));
138*dfc6aa5cSAndroid Build Coastguard Worker vst1_u8(outptr1, vmovn_u16(cb_u16));
139*dfc6aa5cSAndroid Build Coastguard Worker vst1_u8(outptr2, vmovn_u16(cr_u16));
140*dfc6aa5cSAndroid Build Coastguard Worker
141*dfc6aa5cSAndroid Build Coastguard Worker /* Increment pointers. */
142*dfc6aa5cSAndroid Build Coastguard Worker inptr += (8 * RGB_PIXELSIZE);
143*dfc6aa5cSAndroid Build Coastguard Worker outptr0 += 8;
144*dfc6aa5cSAndroid Build Coastguard Worker outptr1 += 8;
145*dfc6aa5cSAndroid Build Coastguard Worker outptr2 += 8;
146*dfc6aa5cSAndroid Build Coastguard Worker }
147*dfc6aa5cSAndroid Build Coastguard Worker }
148*dfc6aa5cSAndroid Build Coastguard Worker }
149