1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15 
16 #include "hmm.h"
17 
18 #include "ia_css_frame_public.h"
19 #define IA_CSS_INCLUDE_CONFIGURATIONS
20 #include "ia_css_isp_configs.h"
21 
22 #include "ia_css_types.h"
23 #include "ia_css_host_data.h"
24 #include "sh_css_param_dvs.h"
25 #include "sh_css_params.h"
26 #include "ia_css_binary.h"
27 #include "ia_css_debug.h"
28 #include "assert_support.h"
29 
30 #include "ia_css_dvs.host.h"
31 
32 static const struct ia_css_dvs_configuration default_config = {
33 	.info = (struct ia_css_frame_info *)NULL,
34 };
35 
36 void
ia_css_dvs_config(struct sh_css_isp_dvs_isp_config * to,const struct ia_css_dvs_configuration * from,unsigned int size)37 ia_css_dvs_config(
38     struct sh_css_isp_dvs_isp_config *to,
39     const struct ia_css_dvs_configuration  *from,
40     unsigned int size)
41 {
42 	(void)size;
43 	to->num_horizontal_blocks =
44 	    DVS_NUM_BLOCKS_X(from->info->res.width);
45 	to->num_vertical_blocks =
46 	    DVS_NUM_BLOCKS_Y(from->info->res.height);
47 }
48 
ia_css_dvs_configure(const struct ia_css_binary * binary,const struct ia_css_frame_info * info)49 int ia_css_dvs_configure(const struct ia_css_binary     *binary,
50 			 const struct ia_css_frame_info *info)
51 {
52 	struct ia_css_dvs_configuration config = default_config;
53 
54 	config.info = info;
55 
56 	return ia_css_configure_dvs(binary, &config);
57 }
58 
59 static void
convert_coords_to_ispparams(struct ia_css_host_data * gdc_warp_table,const struct ia_css_dvs_6axis_config * config,unsigned int i_stride,unsigned int o_width,unsigned int o_height,unsigned int uv_flag)60 convert_coords_to_ispparams(
61     struct ia_css_host_data *gdc_warp_table,
62     const struct ia_css_dvs_6axis_config *config,
63     unsigned int i_stride,
64     unsigned int o_width,
65     unsigned int o_height,
66     unsigned int uv_flag)
67 {
68 	unsigned int i, j;
69 	gdc_warp_param_mem_t s = { 0 };
70 	unsigned int x00, x01, x10, x11,
71 		 y00, y01, y10, y11;
72 
73 	unsigned int xmin, ymin, xmax, ymax;
74 	unsigned int topleft_x, topleft_y, bottom_x, bottom_y,
75 		 topleft_x_frac, topleft_y_frac;
76 	unsigned int dvs_interp_envelope = (DVS_GDC_INTERP_METHOD == HRT_GDC_BLI_MODE ?
77 					    DVS_GDC_BLI_INTERP_ENVELOPE : DVS_GDC_BCI_INTERP_ENVELOPE);
78 
79 	/* number of blocks per height and width */
80 	unsigned int num_blocks_y =  (uv_flag ? DVS_NUM_BLOCKS_Y_CHROMA(
81 					  o_height) : DVS_NUM_BLOCKS_Y(o_height));
82 	unsigned int num_blocks_x =  (uv_flag ? DVS_NUM_BLOCKS_X_CHROMA(
83 					  o_width)  : DVS_NUM_BLOCKS_X(
84 					  o_width)); // round num_x up to blockdim_x, if it concerns the Y0Y1 block (uv_flag==0) round up to even
85 
86 	unsigned int in_stride = i_stride * DVS_INPUT_BYTES_PER_PIXEL;
87 	unsigned int width, height;
88 	unsigned int *xbuff = NULL;
89 	unsigned int *ybuff = NULL;
90 	struct gdc_warp_param_mem_s *ptr;
91 
92 	assert(config);
93 	assert(gdc_warp_table);
94 	assert(gdc_warp_table->address);
95 
96 	ptr = (struct gdc_warp_param_mem_s *)gdc_warp_table->address;
97 
98 	ptr += (2 * uv_flag); /* format is Y0 Y1 UV, so UV starts at 3rd position */
99 
100 	if (uv_flag == 0) {
101 		xbuff = config->xcoords_y;
102 		ybuff = config->ycoords_y;
103 		width = config->width_y;
104 		height = config->height_y;
105 	} else {
106 		xbuff = config->xcoords_uv;
107 		ybuff = config->ycoords_uv;
108 		width = config->width_uv;
109 		height = config->height_uv;
110 	}
111 
112 	IA_CSS_LOG("blockdim_x %d blockdim_y %d",
113 		   DVS_BLOCKDIM_X, DVS_BLOCKDIM_Y_LUMA >> uv_flag);
114 	IA_CSS_LOG("num_blocks_x %d num_blocks_y %d", num_blocks_x, num_blocks_y);
115 	IA_CSS_LOG("width %d height %d", width, height);
116 
117 	assert(width == num_blocks_x +
118 	       1); // the width and height of the provided morphing table should be 1 more than the number of blocks
119 	assert(height == num_blocks_y + 1);
120 
121 	for (j = 0; j < num_blocks_y; j++) {
122 		for (i = 0; i < num_blocks_x; i++) {
123 			x00 = xbuff[j * width + i];
124 			x01 = xbuff[j * width + (i + 1)];
125 			x10 = xbuff[(j + 1) * width + i];
126 			x11 = xbuff[(j + 1) * width + (i + 1)];
127 
128 			y00 = ybuff[j * width + i];
129 			y01 = ybuff[j * width + (i + 1)];
130 			y10 = ybuff[(j + 1) * width + i];
131 			y11 = ybuff[(j + 1) * width + (i + 1)];
132 
133 			xmin = min(x00, x10);
134 			xmax = max(x01, x11);
135 			ymin = min(y00, y01);
136 			ymax = max(y10, y11);
137 
138 			/* Assert that right column's X is greater */
139 			assert(x01 >= xmin);
140 			assert(x11 >= xmin);
141 			/* Assert that bottom row's Y is greater */
142 			assert(y10 >= ymin);
143 			assert(y11 >= ymin);
144 
145 			topleft_y = ymin >> DVS_COORD_FRAC_BITS;
146 			topleft_x = ((xmin >> DVS_COORD_FRAC_BITS)
147 				     >> XMEM_ALIGN_LOG2)
148 				    << (XMEM_ALIGN_LOG2);
149 			s.in_addr_offset = topleft_y * in_stride + topleft_x;
150 
151 			/* similar to topleft_y calculation, but round up if ymax
152 			 * has any fraction bits */
153 			bottom_y = CEIL_DIV(ymax, 1 << DVS_COORD_FRAC_BITS);
154 			s.in_block_height = bottom_y - topleft_y + dvs_interp_envelope;
155 
156 			bottom_x = CEIL_DIV(xmax, 1 << DVS_COORD_FRAC_BITS);
157 			s.in_block_width = bottom_x - topleft_x + dvs_interp_envelope;
158 
159 			topleft_x_frac = topleft_x << (DVS_COORD_FRAC_BITS);
160 			topleft_y_frac = topleft_y << (DVS_COORD_FRAC_BITS);
161 
162 			s.p0_x = x00 - topleft_x_frac;
163 			s.p1_x = x01 - topleft_x_frac;
164 			s.p2_x = x10 - topleft_x_frac;
165 			s.p3_x = x11 - topleft_x_frac;
166 
167 			s.p0_y = y00 - topleft_y_frac;
168 			s.p1_y = y01 - topleft_y_frac;
169 			s.p2_y = y10 - topleft_y_frac;
170 			s.p3_y = y11 - topleft_y_frac;
171 
172 			// block should fit within the boundingbox.
173 			assert(s.p0_x < (s.in_block_width << DVS_COORD_FRAC_BITS));
174 			assert(s.p1_x < (s.in_block_width << DVS_COORD_FRAC_BITS));
175 			assert(s.p2_x < (s.in_block_width << DVS_COORD_FRAC_BITS));
176 			assert(s.p3_x < (s.in_block_width << DVS_COORD_FRAC_BITS));
177 			assert(s.p0_y < (s.in_block_height << DVS_COORD_FRAC_BITS));
178 			assert(s.p1_y < (s.in_block_height << DVS_COORD_FRAC_BITS));
179 			assert(s.p2_y < (s.in_block_height << DVS_COORD_FRAC_BITS));
180 			assert(s.p3_y < (s.in_block_height << DVS_COORD_FRAC_BITS));
181 
182 			// block size should be greater than zero.
183 			assert(s.p0_x < s.p1_x);
184 			assert(s.p2_x < s.p3_x);
185 			assert(s.p0_y < s.p2_y);
186 			assert(s.p1_y < s.p3_y);
187 
188 #if 0
189 			printf("j: %d\ti:%d\n", j, i);
190 			printf("offset: %d\n", s.in_addr_offset);
191 			printf("p0_x: %d\n", s.p0_x);
192 			printf("p0_y: %d\n", s.p0_y);
193 			printf("p1_x: %d\n", s.p1_x);
194 			printf("p1_y: %d\n", s.p1_y);
195 			printf("p2_x: %d\n", s.p2_x);
196 			printf("p2_y: %d\n", s.p2_y);
197 			printf("p3_x: %d\n", s.p3_x);
198 			printf("p3_y: %d\n", s.p3_y);
199 
200 			printf("p0_x_nofrac[0]: %d\n", s.p0_x >> DVS_COORD_FRAC_BITS);
201 			printf("p0_y_nofrac[1]: %d\n", s.p0_y >> DVS_COORD_FRAC_BITS);
202 			printf("p1_x_nofrac[2]: %d\n", s.p1_x >> DVS_COORD_FRAC_BITS);
203 			printf("p1_y_nofrac[3]: %d\n", s.p1_y >> DVS_COORD_FRAC_BITS);
204 			printf("p2_x_nofrac[0]: %d\n", s.p2_x >> DVS_COORD_FRAC_BITS);
205 			printf("p2_y_nofrac[1]: %d\n", s.p2_y >> DVS_COORD_FRAC_BITS);
206 			printf("p3_x_nofrac[2]: %d\n", s.p3_x >> DVS_COORD_FRAC_BITS);
207 			printf("p3_y_nofrac[3]: %d\n", s.p3_y >> DVS_COORD_FRAC_BITS);
208 			printf("\n");
209 #endif
210 
211 			*ptr = s;
212 
213 			// storage format:
214 			// Y0 Y1 UV0 Y2 Y3 UV1
215 			/* if uv_flag equals true increment with 2 incase x is odd, this to
216 			skip the uv position. */
217 			if (uv_flag)
218 				ptr += 3;
219 			else
220 				ptr += (1 + (i & 1));
221 		}
222 	}
223 }
224 
225 struct ia_css_host_data *
convert_allocate_dvs_6axis_config(const struct ia_css_dvs_6axis_config * dvs_6axis_config,const struct ia_css_binary * binary,const struct ia_css_frame_info * dvs_in_frame_info)226 convert_allocate_dvs_6axis_config(
227     const struct ia_css_dvs_6axis_config *dvs_6axis_config,
228     const struct ia_css_binary *binary,
229     const struct ia_css_frame_info *dvs_in_frame_info)
230 {
231 	unsigned int i_stride;
232 	unsigned int o_width;
233 	unsigned int o_height;
234 	struct ia_css_host_data *me;
235 
236 	assert(binary);
237 	assert(dvs_6axis_config);
238 	assert(dvs_in_frame_info);
239 
240 	me = ia_css_host_data_allocate((size_t)((DVS_6AXIS_BYTES(binary) / 2) * 3));
241 
242 	if (!me)
243 		return NULL;
244 
245 	/*DVS only supports input frame of YUV420 or NV12. Fail for all other cases*/
246 	assert((dvs_in_frame_info->format == IA_CSS_FRAME_FORMAT_NV12)
247 	       || (dvs_in_frame_info->format == IA_CSS_FRAME_FORMAT_YUV420));
248 
249 	i_stride  = dvs_in_frame_info->padded_width;
250 
251 	o_width  = binary->out_frame_info[0].res.width;
252 	o_height = binary->out_frame_info[0].res.height;
253 
254 	/* Y plane */
255 	convert_coords_to_ispparams(me, dvs_6axis_config,
256 				    i_stride, o_width, o_height, 0);
257 
258 	if (dvs_in_frame_info->format == IA_CSS_FRAME_FORMAT_YUV420) {
259 		/*YUV420 has half the stride for U/V plane*/
260 		i_stride /= 2;
261 	}
262 
263 	/* UV plane (packed inside the y plane) */
264 	convert_coords_to_ispparams(me, dvs_6axis_config,
265 				    i_stride, o_width / 2, o_height / 2, 1);
266 
267 	return me;
268 }
269 
270 int
store_dvs_6axis_config(const struct ia_css_dvs_6axis_config * dvs_6axis_config,const struct ia_css_binary * binary,const struct ia_css_frame_info * dvs_in_frame_info,ia_css_ptr ddr_addr_y)271 store_dvs_6axis_config(
272     const struct ia_css_dvs_6axis_config *dvs_6axis_config,
273     const struct ia_css_binary *binary,
274     const struct ia_css_frame_info *dvs_in_frame_info,
275     ia_css_ptr ddr_addr_y) {
276 	struct ia_css_host_data *me;
277 
278 	assert(dvs_6axis_config);
279 	assert(ddr_addr_y != mmgr_NULL);
280 	assert(dvs_in_frame_info);
281 
282 	me = convert_allocate_dvs_6axis_config(dvs_6axis_config,
283 					       binary,
284 					       dvs_in_frame_info);
285 
286 	if (!me)
287 	{
288 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
289 		return -ENOMEM;
290 	}
291 
292 	ia_css_params_store_ia_css_host_data(
293 	    ddr_addr_y,
294 	    me);
295 	ia_css_host_data_free(me);
296 
297 	return 0;
298 }
299