1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 
15 #include "type_support.h"
16 #include "ia_css_bnlm.host.h"
17 
18 #ifndef IA_CSS_NO_DEBUG
19 #include "ia_css_debug.h" /* ia_css_debug_dtrace() */
20 #endif
21 #include <assert_support.h>
22 
23 #define BNLM_DIV_LUT_SIZE	(12)
24 static const s32 div_lut_nearests[BNLM_DIV_LUT_SIZE] = {
25 	0, 454, 948, 1484, 2070, 2710, 3412, 4184, 5035, 5978, 7025, 8191
26 };
27 
28 static const s32 div_lut_slopes[BNLM_DIV_LUT_SIZE] = {
29 	-7760, -6960, -6216, -5536, -4912, -4344, -3832, -3360, -2936, -2552, -2208, -2208
30     };
31 
32 static const s32 div_lut_intercepts[BNLM_DIV_LUT_SIZE] = {
33 	8184, 7752, 7336, 6928, 6536, 6152, 5776, 5416, 5064, 4728, 4408, 4408
34 };
35 
36 /* Encodes a look-up table from BNLM public parameters to vmem parameters.
37  * Input:
38  *	lut	:	bnlm_lut struct containing encoded vmem parameters look-up table
39  *	lut_thr	:	array containing threshold values for lut
40  *	lut_val	:	array containing output values related to lut_thr
41  *	lut_size:	Size of lut_val array
42  */
43 static inline void
44 bnlm_lut_encode(struct bnlm_lut *lut, const int32_t *lut_thr,
45 		const s32 *lut_val, const uint32_t lut_size)
46 {
47 	u32 blk, i;
48 	const u32 block_size = 16;
49 	const u32 total_blocks = ISP_VEC_NELEMS / block_size;
50 
51 	/* Create VMEM LUTs from the threshold and value arrays.
52 	 *
53 	 * Min size of the LUT is 2 entries.
54 	 *
55 	 * Max size of the LUT is 16 entries, so that the LUT can fit into a
56 	 * single group of 16 elements inside a vector.
57 	 * Then these elements are copied into other groups inside the same
58 	 * vector. If the LUT size is less than 16, then remaining elements are
59 	 * set to 0.
60 	 */
61 	assert((lut_size >= 2) && (lut_size <= block_size));
62 	/* array lut_thr has (lut_size-1) entries */
63 	for (i = 0; i < lut_size - 2; i++) {
64 		/* Check if the lut_thr is monotonically increasing */
65 		assert(lut_thr[i] <= lut_thr[i + 1]);
66 	}
67 
68 	/* Initialize */
69 	for (i = 0; i < total_blocks * block_size; i++) {
70 		lut->thr[0][i] = 0;
71 		lut->val[0][i] = 0;
72 	}
73 
74 	/* Copy all data */
75 	for (i = 0; i < lut_size - 1; i++) {
76 		lut->thr[0][i] = lut_thr[i];
77 		lut->val[0][i] = lut_val[i];
78 	}
79 	lut->val[0][i] = lut_val[i]; /* val has one more element than thr */
80 
81 	/* Copy data from first block to all blocks */
82 	for (blk = 1; blk < total_blocks; blk++) {
83 		u32 blk_offset = blk * block_size;
84 
85 		for (i = 1; i < lut_size; i++) {
86 			lut->thr[0][blk_offset + i] = lut->thr[0][i];
87 			lut->val[0][blk_offset + i] = lut->val[0][i];
88 		}
89 	}
90 }
91 
92 /*
93  * - Encodes BNLM public parameters into VMEM parameters
94  * - Generates VMEM parameters which will needed internally ISP
95  */
96 void
97 ia_css_bnlm_vmem_encode(
98     struct bnlm_vmem_params *to,
99     const struct ia_css_bnlm_config *from,
100     size_t size)
101 {
102 	int i;
103 	(void)size;
104 
105 	/* Initialize LUTs in VMEM parameters */
106 	bnlm_lut_encode(&to->mu_root_lut, from->mu_root_lut_thr, from->mu_root_lut_val,
107 			16);
108 	bnlm_lut_encode(&to->sad_norm_lut, from->sad_norm_lut_thr,
109 			from->sad_norm_lut_val, 16);
110 	bnlm_lut_encode(&to->sig_detail_lut, from->sig_detail_lut_thr,
111 			from->sig_detail_lut_val, 16);
112 	bnlm_lut_encode(&to->sig_rad_lut, from->sig_rad_lut_thr, from->sig_rad_lut_val,
113 			16);
114 	bnlm_lut_encode(&to->rad_pow_lut, from->rad_pow_lut_thr, from->rad_pow_lut_val,
115 			16);
116 	bnlm_lut_encode(&to->nl_0_lut, from->nl_0_lut_thr, from->nl_0_lut_val, 16);
117 	bnlm_lut_encode(&to->nl_1_lut, from->nl_1_lut_thr, from->nl_1_lut_val, 16);
118 	bnlm_lut_encode(&to->nl_2_lut, from->nl_2_lut_thr, from->nl_2_lut_val, 16);
119 	bnlm_lut_encode(&to->nl_3_lut, from->nl_3_lut_thr, from->nl_3_lut_val, 16);
120 
121 	/* Initialize arrays in VMEM parameters */
122 	memset(to->nl_th, 0, sizeof(to->nl_th));
123 	to->nl_th[0][0] = from->nl_th[0];
124 	to->nl_th[0][1] = from->nl_th[1];
125 	to->nl_th[0][2] = from->nl_th[2];
126 
127 	memset(to->match_quality_max_idx, 0, sizeof(to->match_quality_max_idx));
128 	to->match_quality_max_idx[0][0] = from->match_quality_max_idx[0];
129 	to->match_quality_max_idx[0][1] = from->match_quality_max_idx[1];
130 	to->match_quality_max_idx[0][2] = from->match_quality_max_idx[2];
131 	to->match_quality_max_idx[0][3] = from->match_quality_max_idx[3];
132 
133 	bnlm_lut_encode(&to->div_lut, div_lut_nearests, div_lut_slopes,
134 			BNLM_DIV_LUT_SIZE);
135 	memset(to->div_lut_intercepts, 0, sizeof(to->div_lut_intercepts));
136 	for (i = 0; i < BNLM_DIV_LUT_SIZE; i++) {
137 		to->div_lut_intercepts[0][i] = div_lut_intercepts[i];
138 	}
139 
140 	memset(to->power_of_2, 0, sizeof(to->power_of_2));
141 	for (i = 0; i < (ISP_VEC_ELEMBITS - 1); i++) {
142 		to->power_of_2[0][i] = 1 << i;
143 	}
144 }
145 
146 /* - Encodes BNLM public parameters into DMEM parameters */
147 void
148 ia_css_bnlm_encode(
149     struct bnlm_dmem_params *to,
150     const struct ia_css_bnlm_config *from,
151     size_t size)
152 {
153 	(void)size;
154 	to->rad_enable = from->rad_enable;
155 	to->rad_x_origin = from->rad_x_origin;
156 	to->rad_y_origin = from->rad_y_origin;
157 	to->avg_min_th = from->avg_min_th;
158 	to->max_min_th = from->max_min_th;
159 
160 	to->exp_coeff_a = from->exp_coeff_a;
161 	to->exp_coeff_b = from->exp_coeff_b;
162 	to->exp_coeff_c = from->exp_coeff_c;
163 	to->exp_exponent = from->exp_exponent;
164 }
165 
166 /* Prints debug traces for BNLM public parameters */
167 void
168 ia_css_bnlm_debug_trace(
169     const struct ia_css_bnlm_config *config,
170     unsigned int level)
171 {
172 	if (!config)
173 		return;
174 
175 #ifndef IA_CSS_NO_DEBUG
176 	ia_css_debug_dtrace(level, "BNLM:\n");
177 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "rad_enable", config->rad_enable);
178 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "rad_x_origin",
179 			    config->rad_x_origin);
180 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "rad_y_origin",
181 			    config->rad_y_origin);
182 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "avg_min_th", config->avg_min_th);
183 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "max_min_th", config->max_min_th);
184 
185 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "exp_coeff_a",
186 			    config->exp_coeff_a);
187 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "exp_coeff_b",
188 			    config->exp_coeff_b);
189 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "exp_coeff_c",
190 			    config->exp_coeff_c);
191 	ia_css_debug_dtrace(level, "\t%-32s = %d\n", "exp_exponent",
192 			    config->exp_exponent);
193 
194 	/* ToDo: print traces for LUTs */
195 #endif /* IA_CSS_NO_DEBUG */
196 }
197