1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker * jcgryext-neon.c - grayscale colorspace conversion (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 *
6*dfc6aa5cSAndroid Build Coastguard Worker * This software is provided 'as-is', without any express or implied
7*dfc6aa5cSAndroid Build Coastguard Worker * warranty. In no event will the authors be held liable for any damages
8*dfc6aa5cSAndroid Build Coastguard Worker * arising from the use of this software.
9*dfc6aa5cSAndroid Build Coastguard Worker *
10*dfc6aa5cSAndroid Build Coastguard Worker * Permission is granted to anyone to use this software for any purpose,
11*dfc6aa5cSAndroid Build Coastguard Worker * including commercial applications, and to alter it and redistribute it
12*dfc6aa5cSAndroid Build Coastguard Worker * freely, subject to the following restrictions:
13*dfc6aa5cSAndroid Build Coastguard Worker *
14*dfc6aa5cSAndroid Build Coastguard Worker * 1. The origin of this software must not be misrepresented; you must not
15*dfc6aa5cSAndroid Build Coastguard Worker * claim that you wrote the original software. If you use this software
16*dfc6aa5cSAndroid Build Coastguard Worker * in a product, an acknowledgment in the product documentation would be
17*dfc6aa5cSAndroid Build Coastguard Worker * appreciated but is not required.
18*dfc6aa5cSAndroid Build Coastguard Worker * 2. Altered source versions must be plainly marked as such, and must not be
19*dfc6aa5cSAndroid Build Coastguard Worker * misrepresented as being the original software.
20*dfc6aa5cSAndroid Build Coastguard Worker * 3. This notice may not be removed or altered from any source distribution.
21*dfc6aa5cSAndroid Build Coastguard Worker */
22*dfc6aa5cSAndroid Build Coastguard Worker
23*dfc6aa5cSAndroid Build Coastguard Worker /* This file is included by jcgray-neon.c */
24*dfc6aa5cSAndroid Build Coastguard Worker
25*dfc6aa5cSAndroid Build Coastguard Worker
26*dfc6aa5cSAndroid Build Coastguard Worker /* RGB -> Grayscale conversion is defined by the following equation:
27*dfc6aa5cSAndroid Build Coastguard Worker * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
28*dfc6aa5cSAndroid Build Coastguard Worker *
29*dfc6aa5cSAndroid Build Coastguard Worker * Avoid floating point arithmetic by using shifted integer constants:
30*dfc6aa5cSAndroid Build Coastguard Worker * 0.29899597 = 19595 * 2^-16
31*dfc6aa5cSAndroid Build Coastguard Worker * 0.58700561 = 38470 * 2^-16
32*dfc6aa5cSAndroid Build Coastguard Worker * 0.11399841 = 7471 * 2^-16
33*dfc6aa5cSAndroid Build Coastguard Worker * These constants are defined in jcgray-neon.c
34*dfc6aa5cSAndroid Build Coastguard Worker *
35*dfc6aa5cSAndroid Build Coastguard Worker * This is the same computation as the RGB -> Y portion of RGB -> YCbCr.
36*dfc6aa5cSAndroid Build Coastguard Worker */
37*dfc6aa5cSAndroid Build Coastguard Worker
jsimd_rgb_gray_convert_neon(JDIMENSION image_width,JSAMPARRAY input_buf,JSAMPIMAGE output_buf,JDIMENSION output_row,int num_rows)38*dfc6aa5cSAndroid Build Coastguard Worker void jsimd_rgb_gray_convert_neon(JDIMENSION image_width, JSAMPARRAY input_buf,
39*dfc6aa5cSAndroid Build Coastguard Worker JSAMPIMAGE output_buf, JDIMENSION output_row,
40*dfc6aa5cSAndroid Build Coastguard Worker int num_rows)
41*dfc6aa5cSAndroid Build Coastguard Worker {
42*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW inptr;
43*dfc6aa5cSAndroid Build Coastguard Worker JSAMPROW outptr;
44*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate temporary buffer for final (image_width % 16) pixels in row. */
45*dfc6aa5cSAndroid Build Coastguard Worker ALIGN(16) uint8_t tmp_buf[16 * RGB_PIXELSIZE];
46*dfc6aa5cSAndroid Build Coastguard Worker
47*dfc6aa5cSAndroid Build Coastguard Worker while (--num_rows >= 0) {
48*dfc6aa5cSAndroid Build Coastguard Worker inptr = *input_buf++;
49*dfc6aa5cSAndroid Build Coastguard Worker outptr = output_buf[0][output_row];
50*dfc6aa5cSAndroid Build Coastguard Worker output_row++;
51*dfc6aa5cSAndroid Build Coastguard Worker
52*dfc6aa5cSAndroid Build Coastguard Worker int cols_remaining = image_width;
53*dfc6aa5cSAndroid Build Coastguard Worker for (; cols_remaining > 0; cols_remaining -= 16) {
54*dfc6aa5cSAndroid Build Coastguard Worker
55*dfc6aa5cSAndroid Build Coastguard Worker /* To prevent buffer overread by the vector load instructions, the last
56*dfc6aa5cSAndroid Build Coastguard Worker * (image_width % 16) columns of data are first memcopied to a temporary
57*dfc6aa5cSAndroid Build Coastguard Worker * buffer large enough to accommodate the vector load.
58*dfc6aa5cSAndroid Build Coastguard Worker */
59*dfc6aa5cSAndroid Build Coastguard Worker if (cols_remaining < 16) {
60*dfc6aa5cSAndroid Build Coastguard Worker memcpy(tmp_buf, inptr, cols_remaining * RGB_PIXELSIZE);
61*dfc6aa5cSAndroid Build Coastguard Worker inptr = tmp_buf;
62*dfc6aa5cSAndroid Build Coastguard Worker }
63*dfc6aa5cSAndroid Build Coastguard Worker
64*dfc6aa5cSAndroid Build Coastguard Worker #if RGB_PIXELSIZE == 4
65*dfc6aa5cSAndroid Build Coastguard Worker uint8x16x4_t input_pixels = vld4q_u8(inptr);
66*dfc6aa5cSAndroid Build Coastguard Worker #else
67*dfc6aa5cSAndroid Build Coastguard Worker uint8x16x3_t input_pixels = vld3q_u8(inptr);
68*dfc6aa5cSAndroid Build Coastguard Worker #endif
69*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t r_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_RED]));
70*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t r_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_RED]));
71*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t g_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_GREEN]));
72*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t g_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_GREEN]));
73*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t b_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_BLUE]));
74*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t b_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_BLUE]));
75*dfc6aa5cSAndroid Build Coastguard Worker
76*dfc6aa5cSAndroid Build Coastguard Worker /* Compute Y = 0.29900 * R + 0.58700 * G + 0.11400 * B */
77*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_ll = vmull_n_u16(vget_low_u16(r_l), F_0_298);
78*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_lh = vmull_n_u16(vget_high_u16(r_l), F_0_298);
79*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_hl = vmull_n_u16(vget_low_u16(r_h), F_0_298);
80*dfc6aa5cSAndroid Build Coastguard Worker uint32x4_t y_hh = vmull_n_u16(vget_high_u16(r_h), F_0_298);
81*dfc6aa5cSAndroid Build Coastguard Worker y_ll = vmlal_n_u16(y_ll, vget_low_u16(g_l), F_0_587);
82*dfc6aa5cSAndroid Build Coastguard Worker y_lh = vmlal_n_u16(y_lh, vget_high_u16(g_l), F_0_587);
83*dfc6aa5cSAndroid Build Coastguard Worker y_hl = vmlal_n_u16(y_hl, vget_low_u16(g_h), F_0_587);
84*dfc6aa5cSAndroid Build Coastguard Worker y_hh = vmlal_n_u16(y_hh, vget_high_u16(g_h), F_0_587);
85*dfc6aa5cSAndroid Build Coastguard Worker y_ll = vmlal_n_u16(y_ll, vget_low_u16(b_l), F_0_113);
86*dfc6aa5cSAndroid Build Coastguard Worker y_lh = vmlal_n_u16(y_lh, vget_high_u16(b_l), F_0_113);
87*dfc6aa5cSAndroid Build Coastguard Worker y_hl = vmlal_n_u16(y_hl, vget_low_u16(b_h), F_0_113);
88*dfc6aa5cSAndroid Build Coastguard Worker y_hh = vmlal_n_u16(y_hh, vget_high_u16(b_h), F_0_113);
89*dfc6aa5cSAndroid Build Coastguard Worker
90*dfc6aa5cSAndroid Build Coastguard Worker /* Descale Y values (rounding right shift) and narrow to 16-bit. */
91*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t y_l = vcombine_u16(vrshrn_n_u32(y_ll, 16),
92*dfc6aa5cSAndroid Build Coastguard Worker vrshrn_n_u32(y_lh, 16));
93*dfc6aa5cSAndroid Build Coastguard Worker uint16x8_t y_h = vcombine_u16(vrshrn_n_u32(y_hl, 16),
94*dfc6aa5cSAndroid Build Coastguard Worker vrshrn_n_u32(y_hh, 16));
95*dfc6aa5cSAndroid Build Coastguard Worker
96*dfc6aa5cSAndroid Build Coastguard Worker /* Narrow Y values to 8-bit and store to memory. Buffer overwrite is
97*dfc6aa5cSAndroid Build Coastguard Worker * permitted up to the next multiple of ALIGN_SIZE bytes.
98*dfc6aa5cSAndroid Build Coastguard Worker */
99*dfc6aa5cSAndroid Build Coastguard Worker vst1q_u8(outptr, vcombine_u8(vmovn_u16(y_l), vmovn_u16(y_h)));
100*dfc6aa5cSAndroid Build Coastguard Worker
101*dfc6aa5cSAndroid Build Coastguard Worker /* Increment pointers. */
102*dfc6aa5cSAndroid Build Coastguard Worker inptr += (16 * RGB_PIXELSIZE);
103*dfc6aa5cSAndroid Build Coastguard Worker outptr += 16;
104*dfc6aa5cSAndroid Build Coastguard Worker }
105*dfc6aa5cSAndroid Build Coastguard Worker }
106*dfc6aa5cSAndroid Build Coastguard Worker }
107