1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* ---- includes ----------------------------------------------------------- */
18 
19 #include "b_BasicEm/Functions.h"
20 #include "b_BasicEm/Math.h"
21 #include "b_BitFeatureEm/L06DnsNx4x4Ftr.h"
22 
23 /* ------------------------------------------------------------------------- */
24 
25 /* ========================================================================= */
26 /*                                                                           */
27 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
28 /*                                                                           */
29 /* ========================================================================= */
30 
31 /* ------------------------------------------------------------------------- */
32 
33 /* ========================================================================= */
34 /*                                                                           */
35 /* ---- \ghd{ constructor / destructor } ----------------------------------- */
36 /*                                                                           */
37 /* ========================================================================= */
38 
39 /* ------------------------------------------------------------------------- */
40 
bbf_L06DnsNx4x4Ftr_init(struct bbs_Context * cpA,struct bbf_L06DnsNx4x4Ftr * ptrA)41 void bbf_L06DnsNx4x4Ftr_init( struct bbs_Context* cpA,
42 						    struct bbf_L06DnsNx4x4Ftr* ptrA )
43 {
44 	bbf_Feature_init( cpA, &ptrA->baseE );
45 	ptrA->baseE.typeE = ( uint32 )bbf_FT_L06_DNS_NX4X4_FTR;
46 	ptrA->baseE.vpActivityE = bbf_L06DnsNx4x4Ftr_activity;
47 	ptrA->layersE = 0;
48 	bbs_UInt32Arr_init( cpA, &ptrA->dataArrE );
49 	ptrA->activityFactorE = 0;
50 }
51 
52 /* ------------------------------------------------------------------------- */
53 
bbf_L06DnsNx4x4Ftr_exit(struct bbs_Context * cpA,struct bbf_L06DnsNx4x4Ftr * ptrA)54 void bbf_L06DnsNx4x4Ftr_exit( struct bbs_Context* cpA,
55 						    struct bbf_L06DnsNx4x4Ftr* ptrA )
56 {
57 	bbf_Feature_exit( cpA, &ptrA->baseE );
58 	ptrA->layersE = 0;
59 	bbs_UInt32Arr_exit( cpA, &ptrA->dataArrE );
60 	ptrA->activityFactorE = 0;
61 }
62 
63 /* ------------------------------------------------------------------------- */
64 
65 /* ========================================================================= */
66 /*                                                                           */
67 /* ---- \ghd{ operators } -------------------------------------------------- */
68 /*                                                                           */
69 /* ========================================================================= */
70 
71 /* ------------------------------------------------------------------------- */
72 
bbf_L06DnsNx4x4Ftr_copy(struct bbs_Context * cpA,struct bbf_L06DnsNx4x4Ftr * ptrA,const struct bbf_L06DnsNx4x4Ftr * srcPtrA)73 void bbf_L06DnsNx4x4Ftr_copy( struct bbs_Context* cpA,
74 						    struct bbf_L06DnsNx4x4Ftr* ptrA,
75 						    const struct bbf_L06DnsNx4x4Ftr* srcPtrA )
76 {
77 	bbf_Feature_copy( cpA, &ptrA->baseE, &srcPtrA->baseE );
78 	ptrA->layersE = srcPtrA->layersE;
79 	bbs_UInt32Arr_copy( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE );
80 	ptrA->activityFactorE = srcPtrA->activityFactorE;
81 }
82 
83 /* ------------------------------------------------------------------------- */
84 
bbf_L06DnsNx4x4Ftr_equal(struct bbs_Context * cpA,const struct bbf_L06DnsNx4x4Ftr * ptrA,const struct bbf_L06DnsNx4x4Ftr * srcPtrA)85 flag bbf_L06DnsNx4x4Ftr_equal( struct bbs_Context* cpA,
86 						     const struct bbf_L06DnsNx4x4Ftr* ptrA,
87 						     const struct bbf_L06DnsNx4x4Ftr* srcPtrA )
88 {
89 	if( !bbf_Feature_equal( cpA, &ptrA->baseE, &srcPtrA->baseE ) ) return FALSE;
90 	if( ptrA->layersE != srcPtrA->layersE ) return FALSE;
91 	if( !bbs_UInt32Arr_equal( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE ) ) return FALSE;
92 	if( ptrA->activityFactorE != srcPtrA->activityFactorE ) return FALSE;
93 	return TRUE;
94 }
95 
96 /* ------------------------------------------------------------------------- */
97 
98 /* ========================================================================= */
99 /*                                                                           */
100 /* ---- \ghd{ query functions } -------------------------------------------- */
101 /*                                                                           */
102 /* ========================================================================= */
103 
104 /* ------------------------------------------------------------------------- */
105 
106 /* ========================================================================= */
107 /*                                                                           */
108 /* ---- \ghd{ modify functions } ------------------------------------------- */
109 /*                                                                           */
110 /* ========================================================================= */
111 
112 /* ------------------------------------------------------------------------- */
113 
114 /* ========================================================================= */
115 /*                                                                           */
116 /* ---- \ghd{ I/O } -------------------------------------------------------- */
117 /*                                                                           */
118 /* ========================================================================= */
119 
120 /* ------------------------------------------------------------------------- */
121 
bbf_L06DnsNx4x4Ftr_memSize(struct bbs_Context * cpA,const struct bbf_L06DnsNx4x4Ftr * ptrA)122 uint32 bbf_L06DnsNx4x4Ftr_memSize( struct bbs_Context* cpA,
123 							     const struct bbf_L06DnsNx4x4Ftr* ptrA )
124 {
125 	uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
126 					  bbs_SIZEOF16( uint32 ); /* version */
127 
128 	memSizeL += bbf_Feature_memSize( cpA, &ptrA->baseE );
129 	memSizeL += bbs_SIZEOF16( ptrA->layersE );
130 	memSizeL += bbs_UInt32Arr_memSize( cpA, &ptrA->dataArrE );
131 	memSizeL += bbs_SIZEOF16( ptrA->activityFactorE );
132 
133 	return memSizeL;
134 }
135 
136 /* ------------------------------------------------------------------------- */
137 
bbf_L06DnsNx4x4Ftr_memWrite(struct bbs_Context * cpA,const struct bbf_L06DnsNx4x4Ftr * ptrA,uint16 * memPtrA)138 uint32 bbf_L06DnsNx4x4Ftr_memWrite( struct bbs_Context* cpA,
139 							      const struct bbf_L06DnsNx4x4Ftr* ptrA,
140 								  uint16* memPtrA )
141 {
142 	uint32 memSizeL = bbf_L06DnsNx4x4Ftr_memSize( cpA, ptrA );
143 	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
144 	memPtrA += bbs_memWriteUInt32( bbf_L06_DNS_NX4X4_FTR_VERSION, memPtrA );
145 	memPtrA += bbf_Feature_memWrite( cpA, &ptrA->baseE, memPtrA );
146 	memPtrA += bbs_memWrite32( &ptrA->layersE, memPtrA );
147 	memPtrA += bbs_UInt32Arr_memWrite( cpA, &ptrA->dataArrE, memPtrA );
148 	memPtrA += bbs_memWrite32( &ptrA->activityFactorE, memPtrA );
149 	return memSizeL;
150 }
151 
152 /* ------------------------------------------------------------------------- */
153 
bbf_L06DnsNx4x4Ftr_memRead(struct bbs_Context * cpA,struct bbf_L06DnsNx4x4Ftr * ptrA,const uint16 * memPtrA,struct bbs_MemTbl * mtpA)154 uint32 bbf_L06DnsNx4x4Ftr_memRead( struct bbs_Context* cpA,
155 							     struct bbf_L06DnsNx4x4Ftr* ptrA,
156 							     const uint16* memPtrA,
157 							     struct bbs_MemTbl* mtpA )
158 {
159 	uint32 memSizeL, versionL;
160 	struct bbs_MemTbl memTblL = *mtpA;
161 	struct bbs_MemSeg* espL = bbs_MemTbl_fastestSegPtr( cpA, &memTblL, 0 );
162 	if( bbs_Context_error( cpA ) ) return 0;
163 	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
164 	memPtrA += bbs_memReadVersion32( cpA, &versionL, bbf_L06_DNS_NX4X4_FTR_VERSION, memPtrA );
165 	memPtrA += bbf_Feature_memRead( cpA, &ptrA->baseE, memPtrA );
166 	memPtrA += bbs_memRead32( &ptrA->layersE, memPtrA );
167 	memPtrA += bbs_UInt32Arr_memRead( cpA, &ptrA->dataArrE, memPtrA, espL );
168 	memPtrA += bbs_memRead32( &ptrA->activityFactorE, memPtrA );
169 	if( memSizeL != bbf_L06DnsNx4x4Ftr_memSize( cpA, ptrA ) )
170 	{
171 		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbf_L06DnsNx4x4Ftr_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
172 			        "size mismatch" );
173 		return 0;
174 	}
175 
176 	return memSizeL;
177 }
178 
179 /* ------------------------------------------------------------------------- */
180 
181 /* ========================================================================= */
182 /*                                                                           */
183 /* ---- \ghd{ exec functions } --------------------------------------------- */
184 /*                                                                           */
185 /* ========================================================================= */
186 
187 /* ------------------------------------------------------------------------- */
188 
bbf_L06DnsNx4x4Ftr_activity(const struct bbf_Feature * ptrA,const uint32 * patchA)189 int32 bbf_L06DnsNx4x4Ftr_activity( const struct bbf_Feature* ptrA, const uint32* patchA )
190 {
191 	const struct bbf_L06DnsNx4x4Ftr* ptrL = ( struct bbf_L06DnsNx4x4Ftr* )ptrA;
192 
193 	uint32 wL = ptrL->baseE.patchWidthE - 3;
194     uint32 hL = ptrL->baseE.patchHeightE - 3;
195 	const uint32* dataPtrL = ptrL->dataArrE.arrPtrE;
196 	uint32 iL, jL;
197 
198 	uint32 borderMaskL = ( ( uint32 )1 << hL ) - 1;
199 
200 	uint32 sL[ 16 ];
201 	uint32 bL[ 6 ] = { 0, 0, 0, 0, 0, 0 }; /* bit sum */
202 
203 	for( jL = 0 ; jL < ptrL->layersE; jL++ )
204 	{
205 		for( iL = 0; iL < wL; iL++ )
206 		{
207 			uint32 vL, mL, tL; /* bit sum and thresholds */
208 
209 			uint32 s1L = patchA[ iL     ];
210 			uint32 s2L = patchA[ iL + 1 ];
211 			uint32 s3L = patchA[ iL + 2 ];
212 			uint32 s4L = patchA[ iL + 3 ];
213 
214 			/* comparison of pixels with patchHeightE - 3 features */
215 			sL[  0 ] = ( ( s1L      ) ^ dataPtrL[  0 ] ) & borderMaskL;
216 			sL[  1 ] = ( ( s1L >> 1 ) ^ dataPtrL[  1 ] ) & borderMaskL;
217 			sL[  2 ] = ( ( s1L >> 2 ) ^ dataPtrL[  2 ] ) & borderMaskL;
218 			sL[  3 ] = ( ( s1L >> 3 ) ^ dataPtrL[  3 ] ) & borderMaskL;
219 
220 			sL[  4 ] = ( ( s2L      ) ^ dataPtrL[  4 ] ) & borderMaskL;
221 			sL[  5 ] = ( ( s2L >> 1 ) ^ dataPtrL[  5 ] ) & borderMaskL;
222 			sL[  6 ] = ( ( s2L >> 2 ) ^ dataPtrL[  6 ] ) & borderMaskL;
223 			sL[  7 ] = ( ( s2L >> 3 ) ^ dataPtrL[  7 ] ) & borderMaskL;
224 
225 			sL[  8 ] = ( ( s3L      ) ^ dataPtrL[  8 ] ) & borderMaskL;
226 			sL[  9 ] = ( ( s3L >> 1 ) ^ dataPtrL[  9 ] ) & borderMaskL;
227 			sL[ 10 ] = ( ( s3L >> 2 ) ^ dataPtrL[ 10 ] ) & borderMaskL;
228 			sL[ 11 ] = ( ( s3L >> 3 ) ^ dataPtrL[ 11 ] ) & borderMaskL;
229 
230 			sL[ 12 ] = ( ( s4L      ) ^ dataPtrL[ 12 ] ) & borderMaskL;
231 			sL[ 13 ] = ( ( s4L >> 1 ) ^ dataPtrL[ 13 ] ) & borderMaskL;
232 			sL[ 14 ] = ( ( s4L >> 2 ) ^ dataPtrL[ 14 ] ) & borderMaskL;
233 			sL[ 15 ] = ( ( s4L >> 3 ) ^ dataPtrL[ 15 ] ) & borderMaskL;
234 
235 			/* parallel bit counting of patchHeightE - 2 features */
236 
237 			vL = 0;
238 
239 			mL =  ( ( sL[  0 ] & 0x11111111 ) + ( sL[  1 ] & 0x11111111 ) + ( sL[  2 ] & 0x11111111 ) +
240 					( sL[  3 ] & 0x11111111 ) + ( sL[  4 ] & 0x11111111 ) + ( sL[  5 ] & 0x11111111 ) +
241 		  			( sL[  6 ] & 0x11111111 ) + ( sL[  7 ] & 0x11111111 ) + ( sL[  8 ] & 0x11111111 ) +
242 		  			( sL[  9 ] & 0x11111111 ) + ( sL[ 10 ] & 0x11111111 ) + ( sL[ 11 ] & 0x11111111 ) +
243 		  			( sL[ 12 ] & 0x11111111 ) + ( sL[ 13 ] & 0x11111111 ) + ( sL[ 14 ] & 0x11111111 ) );
244 
245 			tL = dataPtrL[ 16 ];
246 
247 			/* compare with thresholds and store results in vL */
248 			vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) + (   sL[ 15 ]        & 0x01010101 ) ) & 0x10101010 ) >> 4;
249 			vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) + ( ( sL[ 15 ] >> 4 ) & 0x01010101 ) ) & 0x10101010 );
250 
251 			mL =  ( ( sL[  0 ] & 0x02222222 ) + ( sL[  1 ] & 0x02222222 ) + ( sL[  2 ] & 0x02222222 ) +
252 					( sL[  3 ] & 0x02222222 ) + ( sL[  4 ] & 0x02222222 ) + ( sL[  5 ] & 0x02222222 ) +
253 		  			( sL[  6 ] & 0x02222222 ) + ( sL[  7 ] & 0x02222222 ) + ( sL[  8 ] & 0x02222222 ) +
254 		  			( sL[  9 ] & 0x02222222 ) + ( sL[ 10 ] & 0x02222222 ) + ( sL[ 11 ] & 0x02222222 ) +
255 		  			( sL[ 12 ] & 0x02222222 ) + ( sL[ 13 ] & 0x02222222 ) + ( sL[ 14 ] & 0x02222222 ) ) >> 1;
256 
257 			sL[ 15 ] >>= 1;
258 			tL = dataPtrL[ 17 ];
259 
260 			/* compare with thresholds and store results in vL */
261 			vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) + (   sL[ 15 ]        & 0x01010101 ) ) & 0x10101010 ) >> 3;
262 			vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) + ( ( sL[ 15 ] >> 4 ) & 0x01010101 ) ) & 0x10101010 ) << 1;
263 
264 			mL =  ( ( sL[  0 ] & 0x04444444 ) + ( sL[  1 ] & 0x04444444 ) + ( sL[  2 ] & 0x04444444 ) +
265 					( sL[  3 ] & 0x04444444 ) + ( sL[  4 ] & 0x04444444 ) + ( sL[  5 ] & 0x04444444 ) +
266 		  			( sL[  6 ] & 0x04444444 ) + ( sL[  7 ] & 0x04444444 ) + ( sL[  8 ] & 0x04444444 ) +
267 		  			( sL[  9 ] & 0x04444444 ) + ( sL[ 10 ] & 0x04444444 ) + ( sL[ 11 ] & 0x04444444 ) +
268 		  			( sL[ 12 ] & 0x04444444 ) + ( sL[ 13 ] & 0x04444444 ) + ( sL[ 14 ] & 0x04444444 ) ) >> 2;
269 
270 			sL[ 15 ] >>= 1;
271 			tL = dataPtrL[ 18 ];
272 
273 			/* compare with thresholds and store results in vL */
274 			vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) + (   sL[ 15 ]        & 0x01010101 ) ) & 0x10101010 ) >> 2;
275 			vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) + ( ( sL[ 15 ] >> 4 ) & 0x01010101 ) ) & 0x10101010 ) << 2;
276 
277 			mL =  ( ( sL[  0 ] & 0x08888888 ) + ( sL[  1 ] & 0x08888888 ) + ( sL[  2 ] & 0x08888888 ) +
278 					( sL[  3 ] & 0x08888888 ) + ( sL[  4 ] & 0x08888888 ) + ( sL[  5 ] & 0x08888888 ) +
279 		  			( sL[  6 ] & 0x08888888 ) + ( sL[  7 ] & 0x08888888 ) + ( sL[  8 ] & 0x08888888 ) +
280 		  			( sL[  9 ] & 0x08888888 ) + ( sL[ 10 ] & 0x08888888 ) + ( sL[ 11 ] & 0x08888888 ) +
281 		  			( sL[ 12 ] & 0x08888888 ) + ( sL[ 13 ] & 0x08888888 ) + ( sL[ 14 ] & 0x08888888 ) ) >> 3;
282 
283 			sL[ 15 ] >>= 1;
284 			tL = dataPtrL[ 19 ];
285 
286 			/* compare with thresholds and store results in vL */
287 			vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) + (   sL[ 15 ]        & 0x01010101 ) ) & 0x10101010 ) >> 1;
288 			vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) + ( ( sL[ 15 ] >> 4 ) & 0x01010101 ) ) & 0x10101010 ) << 3;
289 
290 			vL = ~vL;
291 
292 			{
293 				uint32 vmL;
294 				vmL = vL & dataPtrL[ 20 ];
295 				bL[ 0 ] += bbf_BIT_SUM_32( vmL );
296 				vmL = vL & dataPtrL[ 21 ];
297 				bL[ 1 ] += bbf_BIT_SUM_32( vmL);
298 				vmL = vL & dataPtrL[ 22 ];
299 				bL[ 2 ] += bbf_BIT_SUM_32( vmL );
300 				vmL = vL & dataPtrL[ 23 ];
301 				bL[ 3 ] += bbf_BIT_SUM_32( vmL );
302 				vmL = vL & dataPtrL[ 24 ];
303 				bL[ 4 ] += bbf_BIT_SUM_32( vmL );
304 				vmL = vL & dataPtrL[ 25 ];
305 				bL[ 5 ] += bbf_BIT_SUM_32( vmL );
306 			}
307 
308 			dataPtrL += 26;
309 		}
310 	}
311 
312 	/* compute final activity */
313 	{
314 		uint32 actL = ( ( bL[ 0 ] << 5 ) + ( bL[ 1 ] << 4 ) + ( bL[ 2 ] << 3 ) +
315 				        ( bL[ 3 ] << 2 ) + ( bL[ 4 ] << 1 ) + ( bL[ 5 ]      ) );
316 
317 		return actL * ptrL->activityFactorE;
318 	}
319 }
320 
321 /* ------------------------------------------------------------------------- */
322 
323 /* ========================================================================= */
324 
325