Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <stdint.h>
34 : #include "options.h"
35 : #include <math.h>
36 : #include "isar_lcld_prot.h"
37 : #include "isar_rom_lcld_tables.h"
38 : #include "prot.h"
39 : #include "isar_prot.h"
40 : #include "wmc_auto.h"
41 :
42 :
43 : /*-------------------------------------------------------------------*
44 : * Function activate_bit()
45 : *
46 : *
47 : *-------------------------------------------------------------------*/
48 :
49 0 : static void activate_bit(
50 : int32_t *state,
51 : const int32_t bit_id )
52 : {
53 0 : ( *state ) |= ( 1 << bit_id );
54 :
55 0 : return;
56 : }
57 :
58 :
59 : /*-------------------------------------------------------------------*
60 : * Function deactivate_bit()
61 : *
62 : *
63 : *-------------------------------------------------------------------*/
64 :
65 0 : static void deactivate_bit(
66 : int32_t *state,
67 : const int32_t bit_id )
68 : {
69 0 : ( *state ) &= ( ~( 1 << bit_id ) );
70 :
71 0 : return;
72 : }
73 :
74 :
75 : /*-------------------------------------------------------------------*
76 : * Function UpdatePredictionSubSetId()
77 : *
78 : *
79 : *-------------------------------------------------------------------*/
80 :
81 0 : void UpdatePredictionSubSetId(
82 : PredictionEncoder *psPredictionEncoder )
83 : {
84 0 : if ( ++psPredictionEncoder->iSubSetId == psPredictionEncoder->iNumSubSets )
85 : {
86 0 : psPredictionEncoder->iSubSetId = 0;
87 : }
88 :
89 0 : return;
90 : }
91 :
92 :
93 : /*-------------------------------------------------------------------*
94 : * Function CreatePredictionEncoder()
95 : *
96 : *
97 : *-------------------------------------------------------------------*/
98 :
99 0 : ivas_error CreatePredictionEncoder(
100 : PredictionEncoder **psPredictionEncoder_out,
101 : const int32_t iChannels,
102 : const int32_t iNumBlocks,
103 : const int32_t iNumSubSets,
104 : const int32_t iMaxNumPredBands )
105 : {
106 : int32_t k, n;
107 0 : PredictionEncoder *psPredictionEncoder = NULL;
108 :
109 0 : if ( ( psPredictionEncoder = (PredictionEncoder *) malloc( sizeof( PredictionEncoder ) ) ) == NULL )
110 : {
111 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
112 : }
113 :
114 0 : psPredictionEncoder->iChannels = iChannels;
115 0 : psPredictionEncoder->iNumBlocks = iNumBlocks;
116 0 : psPredictionEncoder->iSubSetId = 0;
117 0 : psPredictionEncoder->iMaxNumPredBands = iMaxNumPredBands;
118 0 : psPredictionEncoder->iNumSubSets = iNumSubSets;
119 :
120 0 : if ( ( psPredictionEncoder->piPredChanEnable = (int32_t *) malloc( sizeof( int32_t ) * iChannels ) ) == NULL )
121 : {
122 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
123 : }
124 :
125 0 : if ( ( psPredictionEncoder->piNumPredBands = (int32_t *) malloc( sizeof( int32_t ) * iChannels ) ) == NULL )
126 : {
127 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
128 : }
129 0 : for ( n = 0; n < psPredictionEncoder->iChannels; n++ )
130 : {
131 0 : psPredictionEncoder->piPredChanEnable[n] = 0;
132 0 : psPredictionEncoder->piNumPredBands[n] = 40;
133 : }
134 :
135 0 : if ( ( psPredictionEncoder->ppiPredBandEnable = (int32_t **) malloc( sizeof( int32_t * ) * iChannels ) ) == NULL )
136 : {
137 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
138 : }
139 0 : if ( ( psPredictionEncoder->ppfA1Real = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
140 : {
141 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
142 : }
143 0 : if ( ( psPredictionEncoder->ppfA1Imag = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
144 : {
145 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
146 : }
147 0 : if ( ( psPredictionEncoder->ppiA1Mag = (int32_t **) malloc( sizeof( int32_t * ) * iChannels ) ) == NULL )
148 : {
149 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
150 : }
151 0 : if ( ( psPredictionEncoder->ppiA1Phase = (int32_t **) malloc( sizeof( int32_t * ) * iChannels ) ) == NULL )
152 : {
153 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
154 : }
155 0 : if ( ( psPredictionEncoder->pppfInpBufReal = (float ***) malloc( sizeof( float ** ) * iChannels ) ) == NULL )
156 : {
157 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
158 : }
159 0 : if ( ( psPredictionEncoder->pppfInpBufImag = (float ***) malloc( sizeof( float ** ) * iChannels ) ) == NULL )
160 : {
161 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
162 : }
163 0 : if ( ( psPredictionEncoder->ppfInpPrevReal = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
164 : {
165 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
166 : }
167 0 : if ( ( psPredictionEncoder->ppfInpPrevImag = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
168 : {
169 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
170 : }
171 0 : if ( ( psPredictionEncoder->ppfPredStateReal = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
172 : {
173 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
174 : }
175 0 : if ( ( psPredictionEncoder->ppfPredStateImag = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
176 : {
177 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
178 : }
179 0 : if ( ( psPredictionEncoder->ppfPredStateRealTmp = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
180 : {
181 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
182 : }
183 0 : if ( ( psPredictionEncoder->ppfPredStateImagTmp = (float **) malloc( sizeof( float * ) * iChannels ) ) == NULL )
184 : {
185 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
186 : }
187 :
188 0 : for ( n = 0; n < psPredictionEncoder->iChannels; n++ )
189 : {
190 0 : if ( ( psPredictionEncoder->ppiPredBandEnable[n] = (int32_t *) malloc( sizeof( int32_t ) * LCLD_BANDS ) ) == NULL )
191 : {
192 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
193 : }
194 0 : if ( ( psPredictionEncoder->ppfA1Real[n] = (float *) malloc( sizeof( float ) * LCLD_BANDS ) ) == NULL )
195 : {
196 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
197 : }
198 0 : if ( ( psPredictionEncoder->ppfA1Imag[n] = (float *) malloc( sizeof( float ) * LCLD_BANDS ) ) == NULL )
199 : {
200 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
201 : }
202 0 : if ( ( psPredictionEncoder->ppiA1Mag[n] = (int32_t *) malloc( sizeof( int32_t ) * LCLD_BANDS ) ) == NULL )
203 : {
204 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
205 : }
206 0 : if ( ( psPredictionEncoder->ppiA1Phase[n] = (int32_t *) malloc( sizeof( int32_t ) * LCLD_BANDS ) ) == NULL )
207 : {
208 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
209 : }
210 0 : if ( ( psPredictionEncoder->pppfInpBufReal[n] = (float **) malloc( sizeof( float * ) * LCLD_PRED_WIN_LEN ) ) == NULL )
211 : {
212 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
213 : }
214 0 : if ( ( psPredictionEncoder->pppfInpBufImag[n] = (float **) malloc( sizeof( float * ) * LCLD_PRED_WIN_LEN ) ) == NULL )
215 : {
216 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
217 : }
218 0 : for ( k = 0; k < LCLD_PRED_WIN_LEN; k++ )
219 : {
220 0 : if ( ( psPredictionEncoder->pppfInpBufReal[n][k] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
221 : {
222 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
223 : }
224 0 : if ( ( psPredictionEncoder->pppfInpBufImag[n][k] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
225 : {
226 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
227 : }
228 0 : set_zero( psPredictionEncoder->pppfInpBufReal[n][k], LCLD_BANDS );
229 0 : set_zero( psPredictionEncoder->pppfInpBufImag[n][k], LCLD_BANDS );
230 : }
231 0 : if ( ( psPredictionEncoder->ppfPredStateReal[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
232 : {
233 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
234 : }
235 0 : if ( ( psPredictionEncoder->ppfPredStateImag[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
236 : {
237 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
238 : }
239 0 : set_zero( psPredictionEncoder->ppfPredStateReal[n], LCLD_BANDS );
240 0 : set_zero( psPredictionEncoder->ppfPredStateImag[n], LCLD_BANDS );
241 :
242 0 : if ( ( psPredictionEncoder->ppfInpPrevReal[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
243 : {
244 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
245 : }
246 0 : if ( ( psPredictionEncoder->ppfInpPrevImag[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
247 : {
248 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
249 : }
250 :
251 0 : set_zero( psPredictionEncoder->ppfInpPrevReal[n], LCLD_BANDS );
252 0 : set_zero( psPredictionEncoder->ppfInpPrevImag[n], LCLD_BANDS );
253 :
254 0 : if ( ( psPredictionEncoder->ppfPredStateRealTmp[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
255 : {
256 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
257 : }
258 0 : if ( ( psPredictionEncoder->ppfPredStateImagTmp[n] = (float *) malloc( LCLD_BANDS * sizeof( float ) ) ) == NULL )
259 : {
260 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD PredictionEncoder Module \n" ) );
261 : }
262 0 : set_zero( psPredictionEncoder->ppfPredStateRealTmp[n], LCLD_BANDS );
263 0 : set_zero( psPredictionEncoder->ppfPredStateImagTmp[n], LCLD_BANDS );
264 0 : for ( k = 0; k < LCLD_BANDS; k++ )
265 : {
266 0 : psPredictionEncoder->ppiPredBandEnable[n][k] = 0;
267 0 : psPredictionEncoder->ppfA1Real[n][k] = 0.0f;
268 0 : psPredictionEncoder->ppfA1Imag[n][k] = 0.0f;
269 : }
270 : }
271 :
272 0 : *psPredictionEncoder_out = psPredictionEncoder;
273 :
274 0 : return IVAS_ERR_OK;
275 : }
276 :
277 :
278 : /*-------------------------------------------------------------------*
279 : * Function DeletePredictionEncoder()
280 : *
281 : *
282 : *-------------------------------------------------------------------*/
283 :
284 0 : void DeletePredictionEncoder(
285 : PredictionEncoder *psPredictionEncoder )
286 : {
287 : int32_t n;
288 0 : for ( n = 0; n < psPredictionEncoder->iChannels; n++ )
289 : {
290 : int32_t k;
291 0 : free( psPredictionEncoder->ppiPredBandEnable[n] );
292 0 : free( psPredictionEncoder->ppfA1Real[n] );
293 0 : free( psPredictionEncoder->ppfA1Imag[n] );
294 0 : free( psPredictionEncoder->ppiA1Mag[n] );
295 0 : free( psPredictionEncoder->ppiA1Phase[n] );
296 0 : for ( k = 0; k < LCLD_PRED_WIN_LEN; k++ )
297 : {
298 0 : free( psPredictionEncoder->pppfInpBufReal[n][k] );
299 0 : free( psPredictionEncoder->pppfInpBufImag[n][k] );
300 : }
301 0 : free( psPredictionEncoder->pppfInpBufReal[n] );
302 0 : free( psPredictionEncoder->pppfInpBufImag[n] );
303 0 : free( psPredictionEncoder->ppfInpPrevReal[n] );
304 0 : free( psPredictionEncoder->ppfInpPrevImag[n] );
305 0 : free( psPredictionEncoder->ppfPredStateReal[n] );
306 0 : free( psPredictionEncoder->ppfPredStateImag[n] );
307 0 : free( psPredictionEncoder->ppfPredStateRealTmp[n] );
308 0 : free( psPredictionEncoder->ppfPredStateImagTmp[n] );
309 : }
310 0 : free( psPredictionEncoder->piPredChanEnable );
311 0 : free( psPredictionEncoder->piNumPredBands );
312 0 : free( psPredictionEncoder->ppiPredBandEnable );
313 0 : free( psPredictionEncoder->ppfA1Real );
314 0 : free( psPredictionEncoder->ppfA1Imag );
315 0 : free( psPredictionEncoder->ppiA1Mag );
316 0 : free( psPredictionEncoder->ppiA1Phase );
317 0 : free( psPredictionEncoder->pppfInpBufReal );
318 0 : free( psPredictionEncoder->pppfInpBufImag );
319 0 : free( psPredictionEncoder->ppfInpPrevReal );
320 0 : free( psPredictionEncoder->ppfInpPrevImag );
321 0 : free( psPredictionEncoder->ppfPredStateReal );
322 0 : free( psPredictionEncoder->ppfPredStateImag );
323 0 : free( psPredictionEncoder->ppfPredStateRealTmp );
324 0 : free( psPredictionEncoder->ppfPredStateImagTmp );
325 :
326 :
327 0 : free( psPredictionEncoder );
328 :
329 0 : return;
330 : }
331 :
332 :
333 : /*-------------------------------------------------------------------*
334 : * Function ComputePredictors()
335 : *
336 : *
337 : *-------------------------------------------------------------------*/
338 :
339 0 : void ComputePredictors(
340 : PredictionEncoder *psPredictionEncoder,
341 : float ***pppfReal,
342 : float ***pppfImag )
343 : {
344 : int32_t c;
345 :
346 0 : int32_t b0 = psPredictionEncoder->iSubSetId;
347 0 : int32_t bstep = psPredictionEncoder->iNumSubSets;
348 0 : int32_t iNumBlocks = psPredictionEncoder->iNumBlocks;
349 : float ***pppfRealBuf;
350 : float ***pppfImagBuf;
351 0 : float pfEstPredBitGain[LCLD_BANDS] = { 0 };
352 :
353 0 : if ( iNumBlocks < LCLD_PRED_WIN_LEN )
354 : {
355 0 : pppfRealBuf = psPredictionEncoder->pppfInpBufReal;
356 0 : pppfImagBuf = psPredictionEncoder->pppfInpBufImag;
357 0 : for ( c = 0; c < psPredictionEncoder->iChannels; c++ )
358 : {
359 : int32_t n;
360 0 : for ( n = 0; n < LCLD_PRED_WIN_LEN - iNumBlocks; n++ )
361 : {
362 0 : mvr2r( pppfRealBuf[c][n + iNumBlocks], pppfRealBuf[c][n], LCLD_BANDS );
363 0 : mvr2r( pppfImagBuf[c][n + iNumBlocks], pppfImagBuf[c][n], LCLD_BANDS );
364 : }
365 0 : for ( n = 0; n < iNumBlocks; n++ )
366 : {
367 0 : mvr2r( pppfReal[c][n], pppfRealBuf[c][n + LCLD_PRED_WIN_LEN - iNumBlocks], LCLD_BANDS );
368 0 : mvr2r( pppfImag[c][n], pppfImagBuf[c][n + LCLD_PRED_WIN_LEN - iNumBlocks], LCLD_BANDS );
369 : }
370 : }
371 : }
372 : else
373 : {
374 0 : pppfRealBuf = pppfReal;
375 0 : pppfImagBuf = pppfImag;
376 : }
377 :
378 0 : for ( c = 0; c < psPredictionEncoder->iChannels; c++ )
379 : {
380 : int32_t b;
381 0 : for ( b = b0; b < psPredictionEncoder->iMaxNumPredBands; b += bstep )
382 : {
383 : int32_t n;
384 0 : float fGain = 0.0;
385 0 : float fBitGain = 0.0;
386 : float *pfRxxReal;
387 : float *pfRxxImag;
388 : float fA1Real;
389 : float fA1Imag;
390 : int32_t iA1Mag;
391 : int32_t iA1Phase;
392 :
393 0 : pfRxxReal = psPredictionEncoder->pfRxxReal;
394 0 : pfRxxImag = psPredictionEncoder->pfRxxImag;
395 :
396 0 : pfRxxReal[0] = 0.0;
397 0 : pfRxxImag[0] = 0.0;
398 0 : for ( n = 0; n < LCLD_PRED_WIN_LEN; n++ )
399 : {
400 0 : pfRxxReal[0] += ( pppfRealBuf[c][n][b] * pppfRealBuf[c][n][b] + pppfImagBuf[c][n][b] * pppfImagBuf[c][n][b] );
401 : }
402 :
403 0 : pfRxxReal[1] = 0.0;
404 0 : pfRxxImag[1] = 0.0;
405 0 : for ( n = 1; n < LCLD_PRED_WIN_LEN; n++ )
406 : {
407 0 : pfRxxReal[1] += ( pppfRealBuf[c][n][b] * pppfRealBuf[c][n - 1][b] + pppfImagBuf[c][n][b] * pppfImagBuf[c][n - 1][b] );
408 0 : pfRxxImag[1] += ( pppfImagBuf[c][n][b] * pppfRealBuf[c][n - 1][b] - pppfRealBuf[c][n][b] * pppfImagBuf[c][n - 1][b] );
409 : }
410 :
411 0 : if ( pfRxxReal[0] > 1e-12f )
412 : {
413 : float fA1Mag;
414 : float fA1Phase;
415 : float fGain2;
416 : float fBitGain2;
417 0 : int32_t iNumBlocksPerPredCoef = min( iNumBlocks * psPredictionEncoder->iNumSubSets, LCLD_PRED_WIN_LEN );
418 :
419 0 : const float fMagScale = ( 2.0f * (float) ( 1 << ( PRED_QUNAT_FILTER_MAG_BITS ) ) + 1.0f ) / M_PI;
420 0 : const float fInvMagScale = M_PI / ( 2.0f * (float) ( 1 << ( PRED_QUNAT_FILTER_MAG_BITS ) ) + 1.0f );
421 0 : const float fPhaseScale = (float) ( 1 << ( PRED_QUANT_FILTER_PHASE_BITS - 1 ) ) / M_PI;
422 0 : const float fInvPhaseScale = M_PI / (float) ( 1 << ( PRED_QUANT_FILTER_PHASE_BITS - 1 ) );
423 :
424 : /* Compute filter coefficeints */
425 0 : fA1Real = -pfRxxReal[1] / pfRxxReal[0];
426 0 : fA1Imag = -pfRxxImag[1] / pfRxxReal[0];
427 :
428 : /* compute these before quant */
429 : /* Compute est coding gain based on quantized filter coefficients */
430 0 : fGain = 1.0f / ( 1.0f - fA1Real * fA1Real - fA1Imag * fA1Imag );
431 0 : fBitGain = 0.65f * log2f( fGain ) * (float) ( iNumBlocksPerPredCoef ) - (float) ( PRED_QUNAT_FILTER_MAG_BITS + PRED_QUANT_FILTER_PHASE_BITS );
432 0 : fA1Mag = sqrtf( fA1Real * fA1Real + fA1Imag * fA1Imag );
433 0 : fA1Mag = fMagScale * asinf( fA1Mag );
434 0 : iA1Mag = (int32_t) ( fA1Mag + 0.5f );
435 0 : iA1Mag = ( iA1Mag > PRED_QUANT_FILTER_MAG_MIN ) ? iA1Mag : PRED_QUANT_FILTER_MAG_MIN;
436 0 : iA1Mag = ( iA1Mag < PRED_QUANT_FILTER_MAG_MAX ) ? iA1Mag : PRED_QUANT_FILTER_MAG_MAX;
437 0 : fA1Mag = sinf( fInvMagScale * (float) iA1Mag );
438 :
439 0 : fA1Phase = atan2f( fA1Imag, fA1Real );
440 0 : fA1Phase = fPhaseScale * fA1Phase;
441 0 : iA1Phase = ( fA1Phase > 0.0f ) ? (int32_t) ( fA1Phase + 0.5f ) : (int32_t) ( fA1Phase - 0.5f );
442 0 : iA1Phase = ( iA1Phase > PRED_QUANT_FILTER_PHASE_MIN ) ? iA1Phase : PRED_QUANT_FILTER_PHASE_MIN;
443 0 : iA1Phase = ( iA1Phase < PRED_QUANT_FILTER_PHASE_MAX ) ? iA1Phase : PRED_QUANT_FILTER_PHASE_MAX;
444 0 : fA1Phase = fInvPhaseScale * (float) iA1Phase;
445 :
446 0 : fA1Real = fA1Mag * cosf( fA1Phase );
447 0 : fA1Imag = fA1Mag * sinf( fA1Phase );
448 :
449 0 : fGain2 = 1.0f / ( 1.0f - fA1Real * fA1Real - fA1Imag * fA1Imag );
450 0 : fBitGain2 = 0.65f * log2f( fGain ) * (float) ( iNumBlocksPerPredCoef ) - (float) ( PRED_QUNAT_FILTER_MAG_BITS + PRED_QUANT_FILTER_PHASE_BITS );
451 0 : fGain = ( fGain < fGain2 ) ? fGain : fGain2;
452 0 : fBitGain = ( fBitGain < fBitGain2 ) ? fBitGain : fBitGain2;
453 : }
454 : else
455 : {
456 0 : fA1Real = 0.0f;
457 0 : fA1Imag = 0.0f;
458 0 : iA1Mag = 0;
459 0 : iA1Phase = 0;
460 0 : fGain = -10.0f;
461 : }
462 :
463 0 : pfEstPredBitGain[b] = fBitGain;
464 0 : psPredictionEncoder->ppiPredBandEnable[c][b] = ( fBitGain > 0.0f );
465 0 : psPredictionEncoder->ppfA1Real[c][b] = fA1Real;
466 0 : psPredictionEncoder->ppfA1Imag[c][b] = fA1Imag;
467 0 : psPredictionEncoder->ppiA1Mag[c][b] = iA1Mag;
468 0 : psPredictionEncoder->ppiA1Phase[c][b] = iA1Phase;
469 : }
470 :
471 : {
472 : float fBestCost;
473 : int32_t iPredBands;
474 : float fBitGain;
475 0 : int32_t iPredChanEnable = 0;
476 :
477 0 : fBestCost = 0.0;
478 0 : iPredBands = 0;
479 0 : fBitGain = -7.0;
480 0 : for ( b = b0; b < psPredictionEncoder->iMaxNumPredBands; b += bstep )
481 : {
482 0 : fBitGain -= 1.0;
483 0 : if ( psPredictionEncoder->ppiPredBandEnable[c][b] == 1 )
484 : {
485 0 : fBitGain += pfEstPredBitGain[b];
486 : }
487 0 : if ( fBitGain > fBestCost )
488 : {
489 0 : fBestCost = fBitGain;
490 0 : iPredBands = b;
491 0 : iPredChanEnable = 1;
492 : }
493 : }
494 :
495 0 : if ( iPredChanEnable == 1 )
496 : {
497 0 : for ( b = iPredBands + bstep; b < LCLD_BANDS; b += bstep )
498 : {
499 0 : psPredictionEncoder->ppiPredBandEnable[c][b] = 0;
500 : }
501 0 : activate_bit( &psPredictionEncoder->piPredChanEnable[c], psPredictionEncoder->iSubSetId );
502 0 : psPredictionEncoder->piNumPredBands[c] = iPredBands + bstep;
503 : }
504 : else
505 : {
506 0 : for ( b = b0; b < LCLD_BANDS; b += bstep )
507 : {
508 0 : psPredictionEncoder->ppiPredBandEnable[c][b] = 0;
509 : }
510 0 : deactivate_bit( &psPredictionEncoder->piPredChanEnable[c], psPredictionEncoder->iSubSetId );
511 0 : psPredictionEncoder->piNumPredBands[c] = 0;
512 : }
513 : }
514 : }
515 :
516 0 : return;
517 : }
518 :
519 :
520 : /*-------------------------------------------------------------------*
521 : * Function ApplyForwardPredictors()
522 : *
523 : *
524 : *-------------------------------------------------------------------*/
525 :
526 : /*-------------------------------------------------------------------*
527 : * Function WritePredictors()
528 : *
529 : *
530 : *-------------------------------------------------------------------*/
531 :
532 0 : int32_t WritePredictors(
533 : PredictionEncoder *psPredictionEncoder,
534 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
535 : {
536 0 : int32_t iBitsWritten = 0;
537 : int32_t c;
538 0 : int32_t iNumSubSets = psPredictionEncoder->iNumSubSets;
539 0 : int32_t iSubSetId = psPredictionEncoder->iSubSetId;
540 0 : int32_t iNumPredBandBits = 6;
541 0 : const int16_t iSubSetBits = ( LCLD_MAX_NUM_PRED_SUBSETS > 4 ? 3 : 2 );
542 :
543 : /* number of subsets */
544 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iNumSubSets - 1, iSubSetBits ); /* otherwise use default */
545 0 : iBitsWritten += iSubSetBits;
546 :
547 0 : if ( iNumSubSets > 1 )
548 : {
549 : /* write current subset */
550 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iSubSetId, iSubSetBits );
551 0 : iBitsWritten += iSubSetBits;
552 0 : iNumPredBandBits = ( iNumSubSets >= 4 ? 4 : 5 );
553 : }
554 :
555 0 : for ( c = 0; c < psPredictionEncoder->iChannels; c++ )
556 : {
557 : int32_t b;
558 0 : int32_t b0 = iSubSetId;
559 :
560 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, psPredictionEncoder->piPredChanEnable[c], iNumSubSets );
561 0 : iBitsWritten += iNumSubSets;
562 :
563 0 : if ( get_bit( psPredictionEncoder->piPredChanEnable[c], iSubSetId ) )
564 : {
565 0 : int32_t iNumPredBands = ( psPredictionEncoder->piNumPredBands[c] - b0 ) / iNumSubSets;
566 :
567 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iNumPredBands, iNumPredBandBits );
568 0 : iBitsWritten += iNumPredBandBits;
569 :
570 0 : for ( b = b0; b < psPredictionEncoder->piNumPredBands[c]; b += iNumSubSets )
571 : {
572 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, psPredictionEncoder->ppiPredBandEnable[c][b], 1 );
573 0 : iBitsWritten += 1;
574 :
575 0 : if ( psPredictionEncoder->ppiPredBandEnable[c][b] == 1 )
576 : {
577 : int32_t iA1Mag;
578 : int32_t iA1Phase;
579 :
580 0 : iA1Mag = psPredictionEncoder->ppiA1Mag[c][b];
581 0 : iA1Phase = psPredictionEncoder->ppiA1Phase[c][b] - PRED_QUANT_FILTER_PHASE_MIN;
582 :
583 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iA1Mag, PRED_QUNAT_FILTER_MAG_BITS );
584 0 : iBitsWritten += PRED_QUNAT_FILTER_MAG_BITS;
585 :
586 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iA1Phase, PRED_QUANT_FILTER_PHASE_BITS );
587 0 : iBitsWritten += PRED_QUANT_FILTER_PHASE_BITS;
588 : }
589 : }
590 : }
591 : }
592 :
593 0 : return iBitsWritten;
594 : }
|