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/I04Dns2x4Ftr.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
41void bbf_I04Dns2x4Ftr_init( struct bbs_Context* cpA,
42						    struct bbf_I04Dns2x4Ftr* ptrA )
43{
44	bbf_Feature_init( cpA, &ptrA->baseE );
45	ptrA->baseE.typeE = ( uint32 )bbf_FT_I04_DNS_2X4_FTR;
46	ptrA->baseE.vpActivityE = bbf_I04Dns2x4Ftr_activity;
47	bbs_UInt32Arr_init( cpA, &ptrA->dataArrE );
48	bbs_Int16Arr_init( cpA, &ptrA->tableE );
49	ptrA->activityFactorE = 0;
50}
51
52/* ------------------------------------------------------------------------- */
53
54void bbf_I04Dns2x4Ftr_exit( struct bbs_Context* cpA,
55						    struct bbf_I04Dns2x4Ftr* ptrA )
56{
57	bbf_Feature_exit( cpA, &ptrA->baseE );
58	bbs_UInt32Arr_exit( cpA, &ptrA->dataArrE );
59	bbs_Int16Arr_exit( cpA, &ptrA->tableE );
60	ptrA->activityFactorE = 0;
61}
62
63/* ------------------------------------------------------------------------- */
64
65/* ========================================================================= */
66/*                                                                           */
67/* ---- \ghd{ operators } -------------------------------------------------- */
68/*                                                                           */
69/* ========================================================================= */
70
71/* ------------------------------------------------------------------------- */
72
73void bbf_I04Dns2x4Ftr_copy( struct bbs_Context* cpA,
74						    struct bbf_I04Dns2x4Ftr* ptrA,
75						    const struct bbf_I04Dns2x4Ftr* srcPtrA )
76{
77	bbf_Feature_copy( cpA, &ptrA->baseE, &srcPtrA->baseE );
78	bbs_UInt32Arr_copy( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE );
79	bbs_Int16Arr_copy( cpA, &ptrA->tableE, &srcPtrA->tableE );
80	ptrA->activityFactorE = srcPtrA->activityFactorE;
81}
82
83/* ------------------------------------------------------------------------- */
84
85flag bbf_I04Dns2x4Ftr_equal( struct bbs_Context* cpA,
86						     const struct bbf_I04Dns2x4Ftr* ptrA,
87						     const struct bbf_I04Dns2x4Ftr* srcPtrA )
88{
89	if( !bbf_Feature_equal( cpA, &ptrA->baseE, &srcPtrA->baseE ) ) return FALSE;
90	if( !bbs_UInt32Arr_equal( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE ) ) return FALSE;
91	if( !bbs_Int16Arr_equal( cpA, &ptrA->tableE, &srcPtrA->tableE ) ) 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
122uint32 bbf_I04Dns2x4Ftr_memSize( struct bbs_Context* cpA,
123							     const struct bbf_I04Dns2x4Ftr* 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_UInt32Arr_memSize( cpA, &ptrA->dataArrE );
130	memSizeL += bbs_Int16Arr_memSize( cpA, &ptrA->tableE );
131	memSizeL += bbs_SIZEOF16( ptrA->activityFactorE );
132
133	return memSizeL;
134}
135
136/* ------------------------------------------------------------------------- */
137
138uint32 bbf_I04Dns2x4Ftr_memWrite( struct bbs_Context* cpA,
139							      const struct bbf_I04Dns2x4Ftr* ptrA,
140								  uint16* memPtrA )
141{
142	uint32 memSizeL = bbf_I04Dns2x4Ftr_memSize( cpA, ptrA );
143	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
144	memPtrA += bbs_memWriteUInt32( bbf_I04_DNS_2X4_FTR_VERSION, memPtrA );
145	memPtrA += bbf_Feature_memWrite( cpA, &ptrA->baseE, memPtrA );
146	memPtrA += bbs_UInt32Arr_memWrite( cpA, &ptrA->dataArrE, memPtrA );
147	memPtrA += bbs_Int16Arr_memWrite( cpA, &ptrA->tableE, memPtrA );
148	memPtrA += bbs_memWrite32( &ptrA->activityFactorE, memPtrA );
149	return memSizeL;
150}
151
152/* ------------------------------------------------------------------------- */
153
154uint32 bbf_I04Dns2x4Ftr_memRead( struct bbs_Context* cpA,
155							     struct bbf_I04Dns2x4Ftr* 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_I04_DNS_2X4_FTR_VERSION, memPtrA );
165	memPtrA += bbf_Feature_memRead( cpA, &ptrA->baseE, memPtrA );
166	memPtrA += bbs_UInt32Arr_memRead( cpA, &ptrA->dataArrE, memPtrA, espL );
167	memPtrA += bbs_Int16Arr_memRead( cpA, &ptrA->tableE, memPtrA, espL );
168	memPtrA += bbs_memRead32( &ptrA->activityFactorE, memPtrA );
169	if( memSizeL != bbf_I04Dns2x4Ftr_memSize( cpA, ptrA ) )
170	{
171		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbf_I04Dns2x4Ftr_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
189int32 bbf_I04Dns2x4Ftr_activity( const struct bbf_Feature* ptrA, const uint32* patchA )
190{
191	const struct bbf_I04Dns2x4Ftr* ptrL = ( struct bbf_I04Dns2x4Ftr* )ptrA;
192
193	uint32 wL = ptrL->baseE.patchWidthE - 1;
194    uint32 hL = ptrL->baseE.patchHeightE - 3;
195
196	const uint32* dataPtrL = ptrL->dataArrE.arrPtrE;
197	const int16*  tableL   = ptrL->tableE.arrPtrE;
198
199	uint32 blocksL = ptrL->baseE.patchHeightE >> 2;
200	uint32 iL;
201
202	uint32 borderMaskL = ( ( uint32 )1 << hL ) - 1;
203
204	int32 sumL = 0;
205
206	uint32 sL[ 8 ];
207	uint32 mL[ 4 ];
208
209	for( iL = 0; iL < wL; iL++ )
210	{
211		uint32 vL = 0;
212
213		uint32 s1L = patchA[ iL     ];
214		uint32 s2L = patchA[ iL + 1 ];
215
216		/* comparison of pixels with patchHeightE - 3 features */
217		sL[ 0 ] = ( ( s1L      ) ^ dataPtrL[ 0 ] ) & borderMaskL;
218		sL[ 1 ] = ( ( s1L >> 1 ) ^ dataPtrL[ 1 ] ) & borderMaskL;
219		sL[ 2 ] = ( ( s1L >> 2 ) ^ dataPtrL[ 2 ] ) & borderMaskL;
220		sL[ 3 ] = ( ( s1L >> 3 ) ^ dataPtrL[ 3 ] ) & borderMaskL;
221
222		sL[ 4 ] = ( ( s2L      ) ^ dataPtrL[ 4 ] ) & borderMaskL;
223		sL[ 5 ] = ( ( s2L >> 1 ) ^ dataPtrL[ 5 ] ) & borderMaskL;
224		sL[ 6 ] = ( ( s2L >> 2 ) ^ dataPtrL[ 6 ] ) & borderMaskL;
225		sL[ 7 ] = ( ( s2L >> 3 ) ^ dataPtrL[ 7 ] ) & borderMaskL;
226
227		/* parallel bit counting of patchHeightE - 3 features */
228		mL[ 0 ] = ( ( sL[ 0 ] & 0x11111111 ) + ( sL[ 1 ] & 0x11111111 ) +
229					( sL[ 2 ] & 0x11111111 ) + ( sL[ 3 ] & 0x11111111 ) +
230				    ( sL[ 4 ] & 0x11111111 ) + ( sL[ 5 ] & 0x11111111 ) +
231					( sL[ 6 ] & 0x11111111 ) + ( sL[ 7 ] & 0x11111111 ) );
232
233		mL[ 1 ] = ( ( sL[ 0 ] & 0x22222222 ) + ( sL[ 1 ] & 0x22222222 ) +
234					( sL[ 2 ] & 0x22222222 ) + ( sL[ 3 ] & 0x22222222 ) +
235				    ( sL[ 4 ] & 0x22222222 ) + ( sL[ 5 ] & 0x22222222 ) +
236					( sL[ 6 ] & 0x22222222 ) + ( sL[ 7 ] & 0x22222222 ) ) >> 1;
237
238		mL[ 2 ] = ( ( sL[ 0 ] & 0x44444444 ) + ( sL[ 1 ] & 0x44444444 ) +
239					( sL[ 2 ] & 0x44444444 ) + ( sL[ 3 ] & 0x44444444 ) +
240				    ( sL[ 4 ] & 0x44444444 ) + ( sL[ 5 ] & 0x44444444 ) +
241					( sL[ 6 ] & 0x44444444 ) + ( sL[ 7 ] & 0x44444444 ) ) >> 2;
242
243		mL[ 3 ] = ( ( sL[ 0 ] & 0x88888888 ) + ( sL[ 1 ] & 0x88888888 ) +
244					( sL[ 2 ] & 0x88888888 ) + ( sL[ 3 ] & 0x88888888 ) +
245				    ( sL[ 4 ] & 0x88888888 ) + ( sL[ 5 ] & 0x88888888 ) +
246					( sL[ 6 ] & 0x88888888 ) + ( sL[ 7 ] & 0x88888888 ) ) >> 3;
247
248		/* parallel comparison with thresholds and packing of results into bit array of size patchHeightE - 3 */
249		vL |= ( ( mL[ 0 ] + dataPtrL[  8 ] ) & 0x88888888 ) >> 3;
250		vL |= ( ( mL[ 1 ] + dataPtrL[  9 ] ) & 0x88888888 ) >> 2;
251		vL |= ( ( mL[ 2 ] + dataPtrL[ 10 ] ) & 0x88888888 ) >> 1;
252		vL |= ( ( mL[ 3 ] + dataPtrL[ 11 ] ) & 0x88888888 );
253
254		vL = ( ~vL ) & 0x1FFFFFFF;
255
256		/* parallel processing of weights (4 weights at a time) */
257		if( hL == 29 )
258		{
259			sumL += tableL[         ( vL       ) & 0x0F   ];
260			sumL += tableL[  16 + ( ( vL >>  4 ) & 0x0F ) ];
261			sumL += tableL[  32 + ( ( vL >>  8 ) & 0x0F ) ];
262			sumL += tableL[  48 + ( ( vL >> 12 ) & 0x0F ) ];
263			sumL += tableL[  64 + ( ( vL >> 16 ) & 0x0F ) ];
264			sumL += tableL[  80 + ( ( vL >> 20 ) & 0x0F ) ];
265			sumL += tableL[  96 + ( ( vL >> 24 ) & 0x0F ) ];
266			sumL += tableL[ 112 + ( ( vL >> 28 ) & 0x0F ) ];
267			tableL += 128;
268		}
269		else
270		{
271			uint32 jL;
272			for( jL = 0; jL < blocksL; jL++ )
273			{
274				sumL += tableL[ vL & 0x0F ];
275				vL >>= 4;
276				tableL += 16;
277			}
278		}
279
280		dataPtrL += 12;
281	}
282
283	return sumL * ( ptrL->activityFactorE >> 8 ) + ( ( sumL * ( int32 )( ptrL->activityFactorE & 0x0FF ) ) >> 8 );
284}
285
286/* ------------------------------------------------------------------------- */
287
288/* ========================================================================= */
289
290