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 <assert.h>
37 : #include "prot.h"
38 : #include "isar_prot.h"
39 : #include "wmc_auto.h"
40 :
41 : /*------------------------------------------------------------------------------------------*
42 : * Local structures
43 : *------------------------------------------------------------------------------------------*/
44 :
45 : struct LCLD_ENCODER
46 : {
47 : int32_t iSampleRate;
48 : int32_t iChannels;
49 : int32_t iNumBlocks;
50 :
51 : int32_t iTargetBitRate;
52 :
53 : int32_t iNumBands;
54 : const int32_t *piBandwidths;
55 :
56 : int32_t iMSMode;
57 : int32_t *piMSFlags;
58 : int32_t piMSPredCoefs[MAX_BANDS];
59 : int32_t piLRPhaseDiffs[MAX_BANDS];
60 : int32_t iAllowSidePred;
61 :
62 : int32_t iRealOnlyOut;
63 :
64 : RMSEnvelopeGrouping *psRMSEnvelopeGrouping;
65 :
66 : int32_t iCommonGrouping;
67 : int32_t *piNumGroups;
68 : int32_t **ppiGroupLengths;
69 :
70 : int32_t ***pppiRMSEnvelope;
71 : int32_t ***pppiSMR;
72 : int32_t ***pppiExcitation;
73 : int32_t ***pppiAlloc;
74 :
75 : int32_t iAllocOffset;
76 :
77 : int32_t ***pppiLCLDSignReal;
78 : int32_t ***pppiLCLDSignImag;
79 : int32_t ***pppiQLCLDReal;
80 : int32_t ***pppiQLCLDImag;
81 :
82 : PredictionEncoder *psPredictionEncoder;
83 : };
84 :
85 :
86 : /*------------------------------------------------------------------------------------------*
87 : * Function Quantize()
88 : *
89 : *
90 : *------------------------------------------------------------------------------------------*/
91 :
92 0 : static int32_t Quantize(
93 : const float fVal,
94 : const float fScale,
95 : int32_t *iSign,
96 : const int32_t iMaxVal )
97 : {
98 : int32_t iVal;
99 :
100 0 : if ( fVal > 0.0f )
101 : {
102 0 : iVal = (int32_t) ( fScale * fVal + 0.5f );
103 0 : *iSign = 0;
104 : }
105 : else
106 : {
107 0 : iVal = (int32_t) ( -fScale * fVal + 0.5f );
108 0 : *iSign = 1;
109 : }
110 0 : iVal = ( iVal < iMaxVal ) ? iVal : iMaxVal;
111 :
112 0 : return iVal;
113 : }
114 :
115 :
116 : /*------------------------------------------------------------------------------------------*
117 : * Function UnQuantize()
118 : *
119 : *
120 : *------------------------------------------------------------------------------------------*/
121 :
122 0 : static float UnQuantize(
123 : const int32_t iVal,
124 : const float fScale,
125 : const int32_t iSign )
126 : {
127 : float fVal;
128 :
129 0 : if ( iSign == 0 )
130 : {
131 0 : fVal = fScale * (float) iVal;
132 : }
133 : else
134 : {
135 0 : fVal = -fScale * (float) iVal;
136 : }
137 :
138 0 : return fVal;
139 : }
140 :
141 :
142 0 : static void PackReal(
143 : const int32_t iChannels,
144 : const int32_t iNumBlocks,
145 : float ***pppfReal,
146 : float ***pppfImag )
147 : {
148 : int32_t ch, b, n;
149 :
150 0 : for ( ch = 0; ch < iChannels; ch++ )
151 : {
152 0 : for ( b = 0; b < LCLD_BANDS; b++ )
153 : {
154 0 : int32_t iRealBlock = 0;
155 0 : for ( n = 0; n < iNumBlocks; n += 2 )
156 : {
157 0 : pppfImag[ch][iRealBlock][b] = pppfReal[ch][n + 1][b];
158 0 : pppfReal[ch][iRealBlock][b] = pppfReal[ch][n][b];
159 0 : iRealBlock++;
160 : }
161 : }
162 : }
163 :
164 0 : return;
165 : }
166 :
167 : /*------------------------------------------------------------------------------------------*
168 : * Function CreateLCLDEncoder()
169 : *
170 : *
171 : *------------------------------------------------------------------------------------------*/
172 :
173 0 : ivas_error CreateLCLDEncoder(
174 : LCLDEncoder **psLCLDEncoder_out,
175 : const int32_t iSampleRate,
176 : const int32_t iChannels,
177 : const int32_t iTargetBitRate,
178 : const int32_t iAllowSidePred,
179 : const int16_t iNumBlocks,
180 : const int16_t iNumSubSets,
181 : const int32_t iRealOnlyOut )
182 : {
183 : int32_t n;
184 : LCLDEncoder *psLCLDEncoder;
185 : ivas_error error;
186 0 : int32_t iMaxNumPredBands = 0;
187 :
188 0 : assert( iSampleRate == 48000 );
189 0 : assert( iNumBlocks == 16 || iNumBlocks == 8 || iNumBlocks == 4 );
190 0 : assert( iNumSubSets > 0 && iNumSubSets <= LCLD_MAX_NUM_PRED_SUBSETS );
191 :
192 0 : if ( ( psLCLDEncoder = (LCLDEncoder *) malloc( sizeof( LCLDEncoder ) ) ) == NULL )
193 : {
194 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
195 : }
196 :
197 0 : psLCLDEncoder->iSampleRate = iSampleRate;
198 0 : psLCLDEncoder->iChannels = iChannels;
199 0 : psLCLDEncoder->iRealOnlyOut = iRealOnlyOut;
200 0 : psLCLDEncoder->iAllocOffset = 0;
201 :
202 0 : psLCLDEncoder->iTargetBitRate = iTargetBitRate;
203 :
204 0 : psLCLDEncoder->piBandwidths = c_aiBandwidths48;
205 0 : psLCLDEncoder->iNumBands = DEF_BANDS_48; /* 22 bands = 50 CLDFB bands (rather than 23 bands) */
206 0 : iMaxNumPredBands = min( c_aiNumLcldBandsPerBand[psLCLDEncoder->iNumBands - 1], 50 );
207 0 : if ( iRealOnlyOut == 1 )
208 : {
209 0 : iMaxNumPredBands = 0;
210 0 : assert( iNumSubSets == 1 );
211 0 : psLCLDEncoder->iNumBlocks = iNumBlocks / 2;
212 : }
213 : else
214 : {
215 0 : psLCLDEncoder->iNumBlocks = iNumBlocks;
216 : }
217 0 : psLCLDEncoder->iMSMode = 0;
218 :
219 0 : if ( ( psLCLDEncoder->piMSFlags = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
220 : {
221 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
222 : }
223 :
224 0 : for ( n = 0; n < MAX_BANDS; n++ )
225 : {
226 0 : psLCLDEncoder->piLRPhaseDiffs[n] = 0;
227 0 : psLCLDEncoder->piMSPredCoefs[n] = 0;
228 : }
229 0 : psLCLDEncoder->iAllowSidePred = iAllowSidePred;
230 0 : psLCLDEncoder->psRMSEnvelopeGrouping = CreateRMSEnvelopeGrouping( psLCLDEncoder->iNumBlocks );
231 :
232 0 : psLCLDEncoder->iCommonGrouping = 1; /*Common grouping always on only impacts stereo */
233 0 : if ( ( psLCLDEncoder->piNumGroups = (int32_t *) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ) ) ) == NULL )
234 : {
235 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
236 : }
237 :
238 0 : if ( ( psLCLDEncoder->ppiGroupLengths = (int32_t **) malloc( psLCLDEncoder->iChannels * sizeof( int32_t * ) ) ) == NULL )
239 : {
240 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
241 : }
242 :
243 0 : if ( ( psLCLDEncoder->pppiRMSEnvelope = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
244 : {
245 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
246 : }
247 :
248 0 : if ( ( psLCLDEncoder->pppiSMR = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
249 : {
250 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
251 : }
252 :
253 0 : if ( ( psLCLDEncoder->pppiExcitation = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
254 : {
255 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
256 : }
257 :
258 0 : if ( ( psLCLDEncoder->pppiAlloc = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
259 : {
260 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
261 : }
262 :
263 0 : if ( ( psLCLDEncoder->pppiLCLDSignReal = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
264 : {
265 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
266 : }
267 :
268 0 : if ( ( psLCLDEncoder->pppiLCLDSignImag = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
269 : {
270 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
271 : }
272 :
273 0 : if ( ( psLCLDEncoder->pppiQLCLDReal = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
274 : {
275 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
276 : }
277 :
278 0 : if ( ( psLCLDEncoder->pppiQLCLDImag = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
279 : {
280 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
281 : }
282 :
283 0 : for ( n = 0; n < iChannels; n++ )
284 : {
285 : int32_t k;
286 0 : if ( ( psLCLDEncoder->ppiGroupLengths[n] = (int32_t *) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t ) ) ) == NULL )
287 : {
288 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
289 : }
290 :
291 0 : if ( ( psLCLDEncoder->pppiRMSEnvelope[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
292 : {
293 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
294 : }
295 :
296 0 : if ( ( psLCLDEncoder->pppiSMR[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
297 : {
298 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
299 : }
300 :
301 0 : if ( ( psLCLDEncoder->pppiExcitation[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
302 : {
303 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
304 : }
305 :
306 0 : if ( ( psLCLDEncoder->pppiAlloc[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
307 : {
308 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
309 : }
310 :
311 :
312 0 : if ( ( psLCLDEncoder->pppiLCLDSignReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
313 : {
314 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
315 : }
316 :
317 0 : if ( ( psLCLDEncoder->pppiLCLDSignImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
318 : {
319 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
320 : }
321 :
322 0 : if ( ( psLCLDEncoder->pppiQLCLDReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
323 : {
324 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
325 : }
326 :
327 0 : if ( ( psLCLDEncoder->pppiQLCLDImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
328 : {
329 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
330 : }
331 :
332 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
333 : {
334 0 : if ( ( psLCLDEncoder->pppiRMSEnvelope[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
335 : {
336 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
337 : }
338 :
339 0 : if ( ( psLCLDEncoder->pppiSMR[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
340 : {
341 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
342 : }
343 :
344 0 : if ( ( psLCLDEncoder->pppiExcitation[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
345 : {
346 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
347 : }
348 :
349 0 : if ( ( psLCLDEncoder->pppiAlloc[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
350 : {
351 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
352 : }
353 :
354 :
355 0 : if ( ( psLCLDEncoder->pppiLCLDSignReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
356 : {
357 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
358 : }
359 :
360 0 : if ( ( psLCLDEncoder->pppiLCLDSignImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
361 : {
362 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
363 : }
364 :
365 0 : if ( ( psLCLDEncoder->pppiQLCLDReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
366 : {
367 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
368 : }
369 :
370 0 : if ( ( psLCLDEncoder->pppiQLCLDImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
371 : {
372 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
373 : }
374 : }
375 : }
376 :
377 0 : if ( ( error = CreatePredictionEncoder( &( psLCLDEncoder->psPredictionEncoder ), iChannels, psLCLDEncoder->iNumBlocks, (int32_t) iNumSubSets, iMaxNumPredBands ) ) != IVAS_ERR_OK )
378 : {
379 0 : return error;
380 : }
381 :
382 0 : *psLCLDEncoder_out = psLCLDEncoder;
383 :
384 0 : return IVAS_ERR_OK;
385 : }
386 :
387 :
388 : /*------------------------------------------------------------------------------------------*
389 : * Function DeleteLCLDEncoder()
390 : *
391 : *
392 : *------------------------------------------------------------------------------------------*/
393 :
394 0 : void DeleteLCLDEncoder(
395 : LCLDEncoder *psLCLDEncoder )
396 : {
397 : int32_t k, n;
398 :
399 0 : if ( psLCLDEncoder != NULL )
400 : {
401 :
402 0 : if ( psLCLDEncoder->piMSFlags != NULL )
403 : {
404 0 : free( psLCLDEncoder->piMSFlags );
405 : }
406 :
407 0 : if ( psLCLDEncoder->piNumGroups != NULL )
408 : {
409 0 : free( psLCLDEncoder->piNumGroups );
410 : }
411 :
412 0 : if ( psLCLDEncoder->psRMSEnvelopeGrouping != NULL )
413 : {
414 0 : DeleteRMSEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping );
415 : }
416 :
417 0 : if ( psLCLDEncoder->ppiGroupLengths != NULL )
418 : {
419 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
420 : {
421 0 : free( psLCLDEncoder->ppiGroupLengths[n] );
422 : }
423 0 : free( psLCLDEncoder->ppiGroupLengths );
424 : }
425 :
426 0 : if ( psLCLDEncoder->pppiRMSEnvelope != NULL )
427 : {
428 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
429 : {
430 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
431 : {
432 0 : free( psLCLDEncoder->pppiRMSEnvelope[n][k] );
433 : }
434 0 : free( psLCLDEncoder->pppiRMSEnvelope[n] );
435 : }
436 0 : free( psLCLDEncoder->pppiRMSEnvelope );
437 : }
438 :
439 0 : if ( psLCLDEncoder->pppiSMR != NULL )
440 : {
441 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
442 : {
443 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
444 : {
445 0 : free( psLCLDEncoder->pppiSMR[n][k] );
446 : }
447 0 : free( psLCLDEncoder->pppiSMR[n] );
448 : }
449 0 : free( psLCLDEncoder->pppiSMR );
450 : }
451 :
452 0 : if ( psLCLDEncoder->pppiExcitation != NULL )
453 : {
454 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
455 : {
456 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
457 : {
458 0 : free( psLCLDEncoder->pppiExcitation[n][k] );
459 : }
460 0 : free( psLCLDEncoder->pppiExcitation[n] );
461 : }
462 0 : free( psLCLDEncoder->pppiExcitation );
463 : }
464 :
465 0 : if ( psLCLDEncoder->pppiAlloc != NULL )
466 : {
467 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
468 : {
469 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
470 : {
471 0 : free( psLCLDEncoder->pppiAlloc[n][k] );
472 : }
473 0 : free( psLCLDEncoder->pppiAlloc[n] );
474 : }
475 0 : free( psLCLDEncoder->pppiAlloc );
476 : }
477 :
478 0 : if ( psLCLDEncoder->pppiLCLDSignReal != NULL )
479 : {
480 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
481 : {
482 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
483 : {
484 0 : free( psLCLDEncoder->pppiLCLDSignReal[n][k] );
485 : }
486 0 : free( psLCLDEncoder->pppiLCLDSignReal[n] );
487 : }
488 0 : free( psLCLDEncoder->pppiLCLDSignReal );
489 : }
490 :
491 0 : if ( psLCLDEncoder->pppiLCLDSignImag != NULL )
492 : {
493 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
494 : {
495 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
496 : {
497 0 : free( psLCLDEncoder->pppiLCLDSignImag[n][k] );
498 : }
499 0 : free( psLCLDEncoder->pppiLCLDSignImag[n] );
500 : }
501 0 : free( psLCLDEncoder->pppiLCLDSignImag );
502 : }
503 :
504 0 : if ( psLCLDEncoder->pppiQLCLDReal != NULL )
505 : {
506 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
507 : {
508 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
509 : {
510 0 : free( psLCLDEncoder->pppiQLCLDReal[n][k] );
511 : }
512 0 : free( psLCLDEncoder->pppiQLCLDReal[n] );
513 : }
514 0 : free( psLCLDEncoder->pppiQLCLDReal );
515 : }
516 :
517 0 : if ( psLCLDEncoder->pppiQLCLDImag != NULL )
518 : {
519 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
520 : {
521 0 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
522 : {
523 0 : free( psLCLDEncoder->pppiQLCLDImag[n][k] );
524 : }
525 0 : free( psLCLDEncoder->pppiQLCLDImag[n] );
526 : }
527 0 : free( psLCLDEncoder->pppiQLCLDImag );
528 : }
529 :
530 0 : DeletePredictionEncoder( psLCLDEncoder->psPredictionEncoder );
531 0 : free( psLCLDEncoder );
532 : }
533 :
534 0 : return;
535 : }
536 :
537 :
538 : /*------------------------------------------------------------------------------------------*
539 : * Local function declarations
540 : *------------------------------------------------------------------------------------------*/
541 :
542 : static int32_t MSModeCalculation( const int32_t iNumBlocks, const int32_t iNumBands, const int32_t *piBandwidths, float ***pppfReal, float ***pppfImag, int32_t *piMSMode, int32_t *piLRPhaseDiff, int32_t *piMSPredCoef, const int32_t iAllowSidePred, const int32_t iRealOnlyOut, int32_t *piMSFlags );
543 :
544 : static void RemoveRMSEnvelope( const int32_t iNumBands, const int32_t *piBandwidths, const int32_t iNumGroups, const int32_t *piGroupLengths, int32_t **ppiRMSEnvelope, float **ppfReal, float **ppfImag );
545 :
546 : static int32_t CountLCLDBits( const int32_t iNumGroups, const int32_t *piGroupLengths, const int32_t iNumBands, const int32_t *piBandwidths, const int32_t *piPredEnable, int32_t **ppiAlloc, int32_t **ppiQReal, int32_t **ppiQImag );
547 :
548 : static int32_t WriteHeaderInformation( const int32_t iNumBands, ISAR_SPLIT_REND_BITS_HANDLE pBits );
549 :
550 : static int32_t WriteMSInformation( const int32_t iNumBands, const int32_t iMSMode, const int32_t *piMSFlags, const int32_t *piLRPhaseDiffs, const int32_t *piMSPredCoefs, const int32_t iNumMSPredBands, ISAR_SPLIT_REND_BITS_HANDLE pBits );
551 :
552 : static int32_t WriteGroupInformation( const int32_t iChannels, const int32_t iCommonGrouping, const int32_t *piNumGroups, int32_t **ppiGroupLengths, ISAR_SPLIT_REND_BITS_HANDLE pBits );
553 :
554 : static int32_t WriteRMSEnvelope( const int32_t iChannels, const int32_t *piNumGroups, const int32_t iNumBands, int32_t ***pppiRMSEnvelope, ISAR_SPLIT_REND_BITS_HANDLE pBits );
555 :
556 : static int32_t WriteAllocInformation( const int32_t iAllocOffset, ISAR_SPLIT_REND_BITS_HANDLE pBits );
557 :
558 : static int32_t WriteLCLDData( const int32_t *piNumGroups, int32_t **ppiGroupLengths, const int32_t iNumBands, const int32_t iNumChannels, int32_t **ppiPredEnable, const int32_t iNumSubSets, const int32_t iSubSetId, int32_t ***pppiAlloc, int32_t ***pppiSignReal, int32_t ***pppiSignImag, int32_t ***pppiQReal, int32_t ***pppiQImag, ISAR_SPLIT_REND_BITS_HANDLE pBits );
559 :
560 : static int32_t ComputeAllocation( const int32_t iChannels, const int32_t *piNumGroups, int32_t **ppiGroupLengths, const int32_t iNumBands, const int32_t *piBandwidths, float ***pppfReal, float ***pppfImag, int32_t ***pppiSMR, const int32_t iAvailableBits, int32_t *piAllocOffset, int32_t ***pppiAlloc, int32_t ***pppiQReal, int32_t ***pppiQImag, int32_t ***pppiSignReal, int32_t ***pppiSignImag, PredictionEncoder *psPredictionEncoder );
561 :
562 :
563 : /*------------------------------------------------------------------------------------------*
564 : * Function EncodeLCLDFrame()
565 : *
566 : *
567 : *------------------------------------------------------------------------------------------*/
568 :
569 0 : int32_t EncodeLCLDFrame(
570 : LCLDEncoder *psLCLDEncoder,
571 : float ***pppfLCLDReal,
572 : float ***pppfLCLDImag,
573 : int32_t *piBitsWritten,
574 : const int32_t available_bits,
575 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
576 : {
577 : int32_t k, n;
578 : int32_t iAvailableBits, iBitsWritten;
579 0 : int32_t iNumMSBands = 0;
580 : int32_t iAudioBitsWritten;
581 :
582 0 : iAvailableBits = available_bits; /* HCBR for now*/
583 0 : iBitsWritten = 0;
584 0 : assert( available_bits <= pBits->buf_len * 8 );
585 :
586 0 : if ( psLCLDEncoder->iRealOnlyOut == 1 )
587 : {
588 0 : PackReal( psLCLDEncoder->iChannels, psLCLDEncoder->iNumBlocks * 2, pppfLCLDReal, pppfLCLDImag );
589 : }
590 :
591 : /* Do MS calc here */
592 0 : if ( psLCLDEncoder->iChannels == 2 )
593 : {
594 0 : iNumMSBands = MSModeCalculation( psLCLDEncoder->iNumBlocks,
595 : psLCLDEncoder->iNumBands,
596 : psLCLDEncoder->piBandwidths,
597 : pppfLCLDReal,
598 : pppfLCLDImag,
599 : &psLCLDEncoder->iMSMode,
600 0 : psLCLDEncoder->piLRPhaseDiffs,
601 0 : psLCLDEncoder->piMSPredCoefs,
602 : psLCLDEncoder->iAllowSidePred,
603 : psLCLDEncoder->iRealOnlyOut,
604 : psLCLDEncoder->piMSFlags );
605 :
606 0 : if ( psLCLDEncoder->iMSMode > 0 )
607 : {
608 0 : psLCLDEncoder->iCommonGrouping = 1; /* Make sure common grouping is enabled when MS is in use */
609 : }
610 : }
611 :
612 : /* Compute Grouping and RMS Envelopes */
613 0 : if ( psLCLDEncoder->iChannels == 2 && psLCLDEncoder->iCommonGrouping == 1 )
614 : {
615 0 : ComputeEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping,
616 : psLCLDEncoder->iChannels,
617 : psLCLDEncoder->iNumBands,
618 : psLCLDEncoder->piBandwidths,
619 : pppfLCLDReal,
620 : pppfLCLDImag,
621 : &psLCLDEncoder->piNumGroups[0],
622 0 : psLCLDEncoder->ppiGroupLengths[0],
623 : psLCLDEncoder->pppiRMSEnvelope );
624 :
625 0 : psLCLDEncoder->piNumGroups[1] = psLCLDEncoder->piNumGroups[0];
626 0 : for ( n = 0; n < psLCLDEncoder->piNumGroups[0]; n++ )
627 : {
628 0 : psLCLDEncoder->ppiGroupLengths[1][n] = psLCLDEncoder->ppiGroupLengths[0][n];
629 : }
630 : }
631 : else
632 : {
633 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
634 : {
635 0 : ComputeEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping,
636 : psLCLDEncoder->iChannels,
637 : psLCLDEncoder->iNumBands,
638 : psLCLDEncoder->piBandwidths,
639 0 : &pppfLCLDReal[n],
640 0 : &pppfLCLDImag[n],
641 0 : &psLCLDEncoder->piNumGroups[n],
642 0 : psLCLDEncoder->ppiGroupLengths[n],
643 0 : &psLCLDEncoder->pppiRMSEnvelope[n] );
644 : }
645 : }
646 :
647 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
648 : {
649 0 : RemoveRMSEnvelope( psLCLDEncoder->iNumBands,
650 : psLCLDEncoder->piBandwidths,
651 0 : psLCLDEncoder->piNumGroups[n],
652 0 : (const int32_t *) psLCLDEncoder->ppiGroupLengths[n],
653 0 : psLCLDEncoder->pppiRMSEnvelope[n],
654 0 : pppfLCLDReal[n],
655 0 : pppfLCLDImag[n] );
656 : }
657 :
658 0 : ComputePredictors( psLCLDEncoder->psPredictionEncoder, pppfLCLDReal, pppfLCLDImag );
659 :
660 0 : iBitsWritten += WriteHeaderInformation( psLCLDEncoder->iNumBands, pBits );
661 :
662 0 : if ( psLCLDEncoder->iChannels == 2 )
663 : {
664 0 : iBitsWritten += WriteMSInformation( psLCLDEncoder->iNumBands,
665 : psLCLDEncoder->iMSMode,
666 0 : (const int32_t *) psLCLDEncoder->piMSFlags,
667 0 : (const int32_t *) psLCLDEncoder->piLRPhaseDiffs,
668 0 : (const int32_t *) psLCLDEncoder->piMSPredCoefs,
669 : iNumMSBands,
670 : pBits );
671 : }
672 :
673 0 : iBitsWritten += WritePredictors( psLCLDEncoder->psPredictionEncoder, pBits );
674 :
675 0 : iBitsWritten += WriteGroupInformation( psLCLDEncoder->iChannels, psLCLDEncoder->iCommonGrouping, (const int32_t *) psLCLDEncoder->piNumGroups, psLCLDEncoder->ppiGroupLengths, pBits );
676 :
677 0 : iBitsWritten += WriteRMSEnvelope( psLCLDEncoder->iChannels, (const int32_t *) psLCLDEncoder->piNumGroups, psLCLDEncoder->iNumBands, psLCLDEncoder->pppiRMSEnvelope, pBits );
678 :
679 0 : if ( psLCLDEncoder->iChannels == 2 && psLCLDEncoder->iCommonGrouping == 1 )
680 : {
681 0 : for ( k = 0; k < psLCLDEncoder->piNumGroups[0]; k++ )
682 : {
683 0 : PerceptualModelStereo( psLCLDEncoder->iNumBands,
684 0 : psLCLDEncoder->piMSFlags,
685 0 : psLCLDEncoder->pppiRMSEnvelope[0][k],
686 0 : psLCLDEncoder->pppiRMSEnvelope[1][k],
687 0 : psLCLDEncoder->pppiExcitation[0][k],
688 0 : psLCLDEncoder->pppiExcitation[1][k],
689 0 : psLCLDEncoder->pppiSMR[0][k],
690 0 : psLCLDEncoder->pppiSMR[1][k] );
691 : }
692 : }
693 : else
694 : {
695 0 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
696 : {
697 0 : for ( k = 0; k < psLCLDEncoder->piNumGroups[n]; k++ )
698 : {
699 0 : PerceptualModel( psLCLDEncoder->iNumBands,
700 0 : psLCLDEncoder->pppiRMSEnvelope[n][k],
701 0 : psLCLDEncoder->pppiExcitation[n][k],
702 0 : psLCLDEncoder->pppiSMR[n][k] );
703 : }
704 : }
705 : }
706 : #ifdef DEBUG_WRITE_PREDICTORS
707 : {
708 : static FILE *fid;
709 : if ( !fid )
710 : fid = fopen( "pred_enc.txt", "wt" );
711 : for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
712 : {
713 : int16_t b;
714 : for ( b = 0; b < 60; b++ )
715 : fprintf( fid, "%.5f ", (float) psLCLDEncoder->psPredictionEncoder->ppiPredBandEnable[n][b] * psLCLDEncoder->psPredictionEncoder->ppfA1Imag[n][b] );
716 : }
717 : fprintf( fid, "%d %d\n", psLCLDEncoder->psPredictionEncoder->iSubSetId, psLCLDEncoder->psPredictionEncoder->piPredChanEnable[n] );
718 : }
719 : #endif
720 :
721 0 : iAvailableBits -= iBitsWritten;
722 0 : ComputeAllocation( psLCLDEncoder->iChannels,
723 0 : (const int32_t *) psLCLDEncoder->piNumGroups,
724 : psLCLDEncoder->ppiGroupLengths,
725 : psLCLDEncoder->iNumBands,
726 : psLCLDEncoder->piBandwidths,
727 : pppfLCLDReal,
728 : pppfLCLDImag,
729 : psLCLDEncoder->pppiSMR,
730 : iAvailableBits,
731 : &psLCLDEncoder->iAllocOffset,
732 : psLCLDEncoder->pppiAlloc,
733 : psLCLDEncoder->pppiQLCLDReal,
734 : psLCLDEncoder->pppiQLCLDImag,
735 : psLCLDEncoder->pppiLCLDSignReal,
736 : psLCLDEncoder->pppiLCLDSignImag,
737 : psLCLDEncoder->psPredictionEncoder );
738 :
739 0 : iBitsWritten += WriteAllocInformation( psLCLDEncoder->iAllocOffset, pBits );
740 :
741 0 : iAudioBitsWritten = iBitsWritten;
742 0 : iBitsWritten += WriteLCLDData( psLCLDEncoder->piNumGroups,
743 : psLCLDEncoder->ppiGroupLengths,
744 : psLCLDEncoder->iNumBands,
745 : psLCLDEncoder->iChannels,
746 0 : psLCLDEncoder->psPredictionEncoder->ppiPredBandEnable,
747 0 : psLCLDEncoder->psPredictionEncoder->iNumSubSets,
748 0 : psLCLDEncoder->psPredictionEncoder->iSubSetId,
749 : psLCLDEncoder->pppiAlloc,
750 : psLCLDEncoder->pppiLCLDSignReal,
751 : psLCLDEncoder->pppiLCLDSignImag,
752 : psLCLDEncoder->pppiQLCLDReal,
753 : psLCLDEncoder->pppiQLCLDImag,
754 : pBits );
755 0 : *piBitsWritten = iBitsWritten;
756 0 : iAudioBitsWritten = iBitsWritten - iAudioBitsWritten;
757 :
758 0 : UpdatePredictionSubSetId( psLCLDEncoder->psPredictionEncoder );
759 :
760 0 : return 0;
761 : }
762 :
763 :
764 : /*------------------------------------------------------------------------------------------*
765 : * Local functions
766 : *
767 : *
768 : *------------------------------------------------------------------------------------------*/
769 :
770 : enum MSPred_Types
771 : {
772 : MS_PHASE_AND_PRED = 0, /* LR phase alignment + real-valued M/S prediction */
773 : MS_PRED_ONLY = 1, /* real-valued M/S prediction */
774 : MS_PHASE_ONLY = 2 /* LR phase alignment + M/S */
775 : };
776 :
777 : enum MS_BS_TYPES
778 : {
779 : MS_OFF = 0,
780 : MS_ALL = 1,
781 : MS_SOME = 2,
782 : MS_PRED = 3
783 : };
784 :
785 0 : static int32_t MSModeCalculation(
786 : const int32_t iNumBlocks,
787 : const int32_t iNumBands,
788 : const int32_t *piBandwidths,
789 : float ***pppfReal,
790 : float ***pppfImag,
791 : int32_t *piMSMode,
792 : int32_t *piLRPhaseDiffs,
793 : int32_t *piMSPredCoefs,
794 : const int32_t iAllowSidePred,
795 : const int32_t iRealOnlyOut,
796 : int32_t *piMSFlags )
797 : {
798 : int32_t b;
799 : int32_t iFBOffset;
800 : int32_t iNumMSBands;
801 : int32_t iMSPredType;
802 0 : float fMSBitGain = 0.0f;
803 : float pfMSPredBitGain[3];
804 : float fPred;
805 : int32_t piMSPredFlags0[MAX_BANDS];
806 : int32_t piMSPredFlags1[MAX_BANDS];
807 : int32_t piMSPredFlags2[MAX_BANDS];
808 : int32_t *ppiMSPredFlags[3];
809 : int32_t piMSPredCoefs0[MAX_BANDS];
810 : int32_t piMSPredCoefs1[MAX_BANDS];
811 : int32_t piMSPredCoefs2[MAX_BANDS];
812 : int32_t *ppiMSPredCoefs[3];
813 : int32_t piMSPredPhase0[MAX_BANDS];
814 : int32_t piMSPredPhase1[MAX_BANDS];
815 : int32_t piMSPredPhase2[MAX_BANDS];
816 : int32_t *ppiMSPredPhase[3];
817 : int32_t iMsInfoBits;
818 : int32_t piMsPredInfoBits[3];
819 :
820 0 : const float feps = 1e-12f;
821 0 : float fBitsFactor = 3.32192809488736f; /* = 1/log10(2), from dB/10 to bits assuming 1 bit per log2(SNR) or 1 bit per 3dB SNR */
822 :
823 0 : set_zero( pfMSPredBitGain, 3 );
824 0 : set_l( piMsPredInfoBits, 0, 3 );
825 :
826 0 : set_l( piMSPredFlags0, 0, MAX_BANDS );
827 0 : set_l( piMSPredFlags1, 0, MAX_BANDS );
828 0 : set_l( piMSPredFlags2, 0, MAX_BANDS );
829 0 : set_l( piMSPredCoefs0, 0, MAX_BANDS );
830 0 : set_l( piMSPredCoefs1, 0, MAX_BANDS );
831 0 : set_l( piMSPredCoefs2, 0, MAX_BANDS );
832 0 : set_l( piMSPredPhase0, 0, MAX_BANDS );
833 0 : set_l( piMSPredPhase1, 0, MAX_BANDS );
834 0 : set_l( piMSPredPhase2, 0, MAX_BANDS );
835 :
836 0 : if ( iNumBlocks < LCLD_BLOCKS_PER_FRAME )
837 : {
838 0 : fBitsFactor *= ( 0.7f + (float) ( iNumBlocks - 4 ) / (float) ( LCLD_BLOCKS_PER_FRAME - 4 ) * ( 1.0f - 0.7f ) ); /* Tuning for relatively higher side rate due to shorter frame length */
839 : }
840 :
841 0 : ppiMSPredFlags[0] = piMSPredFlags0;
842 0 : ppiMSPredFlags[1] = piMSPredFlags1;
843 0 : ppiMSPredFlags[2] = piMSPredFlags2;
844 :
845 0 : ppiMSPredCoefs[0] = piMSPredCoefs0;
846 0 : ppiMSPredCoefs[1] = piMSPredCoefs1;
847 0 : ppiMSPredCoefs[2] = piMSPredCoefs2;
848 :
849 0 : ppiMSPredPhase[0] = piMSPredPhase0;
850 0 : ppiMSPredPhase[1] = piMSPredPhase1;
851 0 : ppiMSPredPhase[2] = piMSPredPhase2;
852 :
853 0 : *piMSMode = MS_OFF;
854 0 : iFBOffset = 0;
855 0 : iNumMSBands = 0;
856 0 : for ( b = 0; b < iNumBands; b++ )
857 : {
858 : int32_t n;
859 : float fLeftEnergy;
860 : float fRightEnergy;
861 : float fMidEnergy;
862 : float fSideEnergy;
863 : float fLRRatio;
864 : float fMSRatio;
865 0 : float pfMSPredRatio[3] = { 0.0f };
866 : float fMidEnergyPred;
867 : float fSideEnergyPred;
868 0 : float fLRCovReal = 0.0f;
869 0 : float fLRCovImag = 0.0f;
870 : int32_t iPhase;
871 : int32_t iPred;
872 0 : int32_t tabIdx = 0;
873 0 : float fNumLines = (float) ( iRealOnlyOut == 1 ? iNumBlocks * piBandwidths[b] * 4 : iNumBlocks * piBandwidths[b] * 2 ); /* per band per channel */
874 0 : float fLevelToSMRdBFactor = (float) c_aiDefaultTheta48[b] / (float) ( 1 << PERCEPTUAL_MODEL_SLGAIN_SHIFT ); /* frequency dependent SMR slope in psy model */
875 0 : fLeftEnergy = 0.0f;
876 0 : fRightEnergy = 0.0f;
877 0 : fMidEnergy = 0.0f;
878 0 : fSideEnergy = 0.0f;
879 :
880 0 : for ( n = 0; n < piBandwidths[b]; n++ )
881 : {
882 : int32_t k;
883 0 : for ( k = 0; k < iNumBlocks; k++ )
884 : {
885 : float fMidReal;
886 : float fMidImag;
887 : float fSideReal;
888 : float fSideImag;
889 :
890 0 : fMidReal = 0.5f * ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
891 0 : fMidImag = 0.5f * ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
892 0 : fSideReal = 0.5f * ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
893 0 : fSideImag = 0.5f * ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
894 :
895 0 : fLeftEnergy += ( pppfReal[0][k][iFBOffset] * pppfReal[0][k][iFBOffset] + pppfImag[0][k][iFBOffset] * pppfImag[0][k][iFBOffset] );
896 0 : fRightEnergy += ( pppfReal[1][k][iFBOffset] * pppfReal[1][k][iFBOffset] + pppfImag[1][k][iFBOffset] * pppfImag[1][k][iFBOffset] );
897 0 : fMidEnergy += ( fMidReal * fMidReal + fMidImag * fMidImag );
898 0 : fSideEnergy += ( fSideReal * fSideReal + fSideImag * fSideImag );
899 :
900 0 : fLRCovReal += ( pppfReal[0][k][iFBOffset] * pppfReal[1][k][iFBOffset] + pppfImag[0][k][iFBOffset] * pppfImag[1][k][iFBOffset] );
901 0 : fLRCovImag += ( pppfImag[0][k][iFBOffset] * pppfReal[1][k][iFBOffset] - pppfImag[1][k][iFBOffset] * pppfReal[0][k][iFBOffset] );
902 : }
903 :
904 0 : iFBOffset++;
905 : }
906 :
907 : /* M/S prediction without phase alignment*/
908 0 : fPred = 0.25f * ( fLeftEnergy - fRightEnergy ) / ( fMidEnergy + feps );
909 0 : iPred = quantPred( fPred );
910 0 : fPred = dequantPred( iPred );
911 0 : fSideEnergyPred = fSideEnergy + ( fPred * fPred * fMidEnergy - 2.0f * fPred * 0.25f * ( fLeftEnergy - fRightEnergy ) );
912 :
913 0 : ppiMSPredCoefs[MS_PRED_ONLY][b] = iPred;
914 0 : ppiMSPredPhase[MS_PRED_ONLY][b] = 0;
915 0 : pfMSPredRatio[MS_PRED_ONLY] = log10f( ( fMidEnergy + feps ) / ( fSideEnergyPred + feps ) );
916 :
917 : /* Phase alignment*/
918 0 : iPhase = 0;
919 0 : if ( fLRCovReal * fLRCovReal + fLRCovImag * fLRCovImag > 0.5f * fLeftEnergy * fRightEnergy )
920 : {
921 0 : float fPhase = atan2f( fLRCovImag, fLRCovReal );
922 0 : iPhase = quantPhase( fPhase );
923 : }
924 :
925 : /* adjust covariance */
926 0 : tabIdx = iPhase - PHASE_MIN_VAL;
927 0 : cplxmult_lcld( &fLRCovReal, &fLRCovImag, c_afRotRealImag[tabIdx][0], -c_afRotRealImag[tabIdx][1] );
928 :
929 : /* compute MS prediction coefficient based on adjusted covariance */
930 0 : fMidEnergyPred = 0.25f * ( fLeftEnergy + fRightEnergy + 2.0f * fLRCovReal );
931 0 : fSideEnergyPred = 0.25f * ( fLeftEnergy + fRightEnergy - 2.0f * fLRCovReal );
932 :
933 : /* M/S with LR phase alignment but without prediction */
934 0 : ppiMSPredCoefs[MS_PHASE_ONLY][b] = 0;
935 0 : ppiMSPredPhase[MS_PHASE_ONLY][b] = iPhase;
936 0 : pfMSPredRatio[MS_PHASE_ONLY] = log10f( ( fMidEnergyPred + feps ) / ( fSideEnergyPred + feps ) );
937 :
938 : /* M/S with LR phase alignment and prediction */
939 0 : fPred = fMidEnergyPred == 0.0f ? 0.0f : 0.25f * ( fLeftEnergy - fRightEnergy ) / fMidEnergyPred;
940 0 : iPred = quantPred( fPred );
941 0 : fPred = dequantPred( iPred );
942 0 : fSideEnergyPred += ( fPred * fPred * fMidEnergyPred - 2.0f * fPred * 0.25f * ( fLeftEnergy - fRightEnergy ) );
943 : /* -= fPred * fPred * fMidEnergyPred doesn't work because fPred is quantized and does not match MS/MM exactly */
944 0 : ppiMSPredCoefs[MS_PHASE_AND_PRED][b] = iPred;
945 0 : ppiMSPredPhase[MS_PHASE_AND_PRED][b] = iPhase;
946 0 : pfMSPredRatio[MS_PHASE_AND_PRED] = log10f( ( fMidEnergyPred + feps ) / ( fSideEnergyPred + feps ) );
947 :
948 : /* Plain M/S */
949 0 : fLeftEnergy = log10f( fLeftEnergy + feps );
950 0 : fRightEnergy = log10f( fRightEnergy + feps );
951 0 : fMidEnergy = log10f( fMidEnergy + feps );
952 0 : fSideEnergy = log10f( fSideEnergy + feps );
953 :
954 0 : fLRRatio = ( fLeftEnergy > fRightEnergy ? fLeftEnergy - fRightEnergy : fRightEnergy - fLeftEnergy );
955 0 : fMSRatio = ( fMidEnergy > fSideEnergy ? fMidEnergy - fSideEnergy : fSideEnergy - fMidEnergy );
956 :
957 0 : if ( fMSRatio > fLRRatio )
958 : {
959 0 : iNumMSBands++;
960 0 : piMSFlags[b] = 1;
961 0 : fMSBitGain += fNumLines * ( fMSRatio - fLRRatio ) * fLevelToSMRdBFactor * fBitsFactor;
962 : }
963 : else
964 : {
965 0 : piMSFlags[b] = 0;
966 : }
967 0 : piLRPhaseDiffs[b] = 0;
968 0 : piMSPredCoefs[b] = 0;
969 :
970 : /* MSPred bit gains based on increase of level ratio compared to L/R ratio and the level dependent psy-model */
971 0 : for ( iMSPredType = 0; iMSPredType < 3; iMSPredType++ )
972 : {
973 0 : if ( pfMSPredRatio[iMSPredType] > fLRRatio )
974 : {
975 0 : ppiMSPredFlags[iMSPredType][b] = 1;
976 0 : pfMSPredBitGain[iMSPredType] += fNumLines * ( pfMSPredRatio[iMSPredType] - fLRRatio ) * fLevelToSMRdBFactor * fBitsFactor;
977 : }
978 : }
979 : }
980 :
981 : /* remove signalling cost from bit gains */
982 0 : for ( iMSPredType = 0; iMSPredType < 3; iMSPredType++ )
983 : {
984 0 : piMsPredInfoBits[iMSPredType] = CountMSBits( iNumBands, MS_PRED, ppiMSPredFlags[iMSPredType], ppiMSPredPhase[iMSPredType], ppiMSPredCoefs[iMSPredType] );
985 0 : pfMSPredBitGain[iMSPredType] = max( pfMSPredBitGain[iMSPredType] - piMsPredInfoBits[iMSPredType], 0.0f );
986 : }
987 :
988 : /* find the best M/S Pred type */
989 0 : if ( iRealOnlyOut == 1 )
990 : {
991 0 : iMSPredType = MS_PRED_ONLY;
992 : }
993 : else
994 : {
995 0 : iMSPredType = MS_PHASE_AND_PRED;
996 0 : iMSPredType = ( pfMSPredBitGain[MS_PRED_ONLY] > pfMSPredBitGain[iMSPredType] ? MS_PRED_ONLY : iMSPredType );
997 0 : iMSPredType = ( pfMSPredBitGain[MS_PHASE_ONLY] > pfMSPredBitGain[iMSPredType] ? MS_PHASE_ONLY : iMSPredType );
998 : }
999 :
1000 : /* plain M/S */
1001 0 : iMsInfoBits = CountMSBits( iNumBands, MS_SOME, piMSFlags, NULL, NULL );
1002 0 : fMSBitGain = max( fMSBitGain - iMsInfoBits, 0.0f );
1003 0 : if ( iAllowSidePred && pfMSPredBitGain[iMSPredType] > 1.1f * fMSBitGain )
1004 : {
1005 0 : *piMSMode = MS_PRED;
1006 0 : iNumMSBands = 0;
1007 0 : for ( b = 0; b < iNumBands; b++ )
1008 : {
1009 0 : piMSFlags[b] = ppiMSPredFlags[iMSPredType][b];
1010 0 : if ( piMSFlags[b] == 1 )
1011 : {
1012 0 : piMSPredCoefs[b] = ppiMSPredCoefs[iMSPredType][b];
1013 0 : piLRPhaseDiffs[b] = ppiMSPredPhase[iMSPredType][b];
1014 0 : iNumMSBands++;
1015 : }
1016 : else
1017 : {
1018 0 : piMSPredCoefs[b] = 0;
1019 0 : piLRPhaseDiffs[b] = 0;
1020 : }
1021 : }
1022 : }
1023 0 : else if ( iNumMSBands == iNumBands )
1024 : {
1025 0 : *piMSMode = MS_ALL;
1026 : }
1027 0 : else if ( iNumMSBands > 0 )
1028 : {
1029 0 : *piMSMode = MS_SOME;
1030 : }
1031 : else
1032 : {
1033 0 : *piMSMode = MS_OFF;
1034 : }
1035 : #ifdef DEBUG_WRITE_MS_PRED
1036 : {
1037 : static FILE *fid;
1038 : int32_t iActualInfoBits = CountMSBits( iNumBands, *piMSMode, piMSFlags, piLRPhaseDiffs, piMSPredCoefs );
1039 : if ( !fid )
1040 : fid = fopen( "ms_info_bits.txt", "wt" );
1041 : fprintf( fid, "%d %d %d %d %d\n", iMsInfoBits, piMsPredInfoBits[MS_PHASE_AND_PRED], piMsPredInfoBits[MS_PRED_ONLY], piMsPredInfoBits[MS_PHASE_ONLY], iActualInfoBits );
1042 : }
1043 : #endif
1044 :
1045 0 : if ( *piMSMode != MS_OFF )
1046 : {
1047 0 : iFBOffset = 0;
1048 0 : for ( b = 0; b < iNumBands; b++ )
1049 : {
1050 0 : if ( piMSFlags[b] == 1 )
1051 : {
1052 : int32_t n;
1053 : int32_t phaseIdx;
1054 0 : phaseIdx = piLRPhaseDiffs[b] - PHASE_MIN_VAL;
1055 0 : fPred = dequantPred( piMSPredCoefs[b] );
1056 0 : for ( n = 0; n < piBandwidths[b]; n++ )
1057 : {
1058 : int32_t k;
1059 0 : for ( k = 0; k < iNumBlocks; k++ )
1060 : {
1061 : float fMidReal;
1062 : float fMidImag;
1063 : float fSideReal;
1064 : float fSideImag;
1065 :
1066 0 : if ( *piMSMode == MS_PRED )
1067 : {
1068 0 : cplxmult_lcld( &pppfReal[1][k][iFBOffset], &pppfImag[1][k][iFBOffset], c_afRotRealImag[phaseIdx][0], c_afRotRealImag[phaseIdx][1] );
1069 : }
1070 :
1071 0 : fMidReal = 0.5f * ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
1072 0 : fMidImag = 0.5f * ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
1073 0 : fSideReal = 0.5f * ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
1074 0 : fSideImag = 0.5f * ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
1075 :
1076 0 : if ( *piMSMode == MS_PRED )
1077 : {
1078 0 : fSideReal -= fPred * fMidReal;
1079 0 : fSideImag -= fPred * fMidImag;
1080 : }
1081 :
1082 0 : pppfReal[0][k][iFBOffset] = fMidReal;
1083 0 : pppfReal[1][k][iFBOffset] = fSideReal;
1084 0 : pppfImag[0][k][iFBOffset] = fMidImag;
1085 0 : pppfImag[1][k][iFBOffset] = fSideImag;
1086 : }
1087 0 : iFBOffset++;
1088 : }
1089 : }
1090 : else
1091 : {
1092 0 : iFBOffset += piBandwidths[b];
1093 : }
1094 : }
1095 : }
1096 : #ifdef DEBUG_WRITE_MS_PRED
1097 : {
1098 : static FILE *fid;
1099 : if ( !fid )
1100 : fid = fopen( "ms_enc.txt", "wt" );
1101 : writeMSPred( piLRPhaseDiffs, piMSPredCoefs, *piMSMode, iNumMSBands, iNumBands, fid, piMSFlags );
1102 : }
1103 : #endif
1104 0 : if ( *piMSMode == MS_PRED )
1105 : {
1106 : /* Differential Coding of Phase Data*/
1107 0 : PrepEncode( piLRPhaseDiffs, piMSFlags, iNumBands );
1108 0 : PrepEncode( piMSPredCoefs, piMSFlags, iNumBands );
1109 : #ifdef DEBUG_WRITE_MS_PRED
1110 : {
1111 : static FILE *fid;
1112 : if ( !fid )
1113 : fid = fopen( "ms_pred_enc.txt", "wt" );
1114 : writeMSPred( piLRPhaseDiffs, piMSPredCoefs, *piMSMode, iNumMSBands, iNumBands, fid, piMSFlags );
1115 : }
1116 : #endif
1117 : /* Differential Coding*/
1118 0 : EncodePhase( piLRPhaseDiffs, iNumMSBands, PHASE_DIFF_DIM );
1119 0 : EncodePredCoef( piMSPredCoefs, iNumMSBands );
1120 : }
1121 :
1122 0 : return iNumMSBands;
1123 : }
1124 :
1125 :
1126 0 : static void RemoveRMSEnvelope(
1127 : const int32_t iNumBands,
1128 : const int32_t *piBandwidths,
1129 : const int32_t iNumGroups,
1130 : const int32_t *piGroupLengths,
1131 : int32_t **ppiRMSEnvelope,
1132 : float **ppfReal,
1133 : float **ppfImag )
1134 : {
1135 : int32_t k, n, b, iFBOffset, m, iRMSEnv;
1136 : int32_t iBlockOffset;
1137 : float fGain;
1138 :
1139 0 : iBlockOffset = 0;
1140 0 : for ( n = 0; n < iNumGroups; n++ )
1141 : {
1142 0 : for ( k = 0; k < piGroupLengths[n]; k++ )
1143 : {
1144 0 : iFBOffset = 0;
1145 0 : for ( b = 0; b < iNumBands; b++ )
1146 : {
1147 0 : iRMSEnv = ppiRMSEnvelope[n][b];
1148 0 : fGain = c_afRMSEnvReconstructTable[ENV_RECONSTRUCT_TABLE_CENTER - iRMSEnv];
1149 0 : for ( m = 0; m < piBandwidths[b]; m++ )
1150 : {
1151 0 : ppfReal[iBlockOffset][iFBOffset] *= fGain;
1152 0 : ppfImag[iBlockOffset][iFBOffset] *= fGain;
1153 0 : iFBOffset++;
1154 : }
1155 : }
1156 0 : iBlockOffset++;
1157 : }
1158 : }
1159 :
1160 0 : return;
1161 : }
1162 :
1163 :
1164 0 : static void QuantizeSpectrumDPCM_Opt(
1165 : const int32_t iNumGroups,
1166 : const int32_t *piGroupLengths,
1167 : const int32_t iNumBands,
1168 : const int32_t *piBandwidths,
1169 : int32_t **ppiAlloc,
1170 : float **ppfReal,
1171 : float **ppfImag,
1172 : int32_t **ppiQReal,
1173 : int32_t **ppiQImag,
1174 : int32_t **ppiSignReal,
1175 : int32_t **ppiSignImag,
1176 : const int32_t iNumSubSets,
1177 : const int32_t iSubSetId,
1178 : const int32_t *piPredEnable,
1179 : float *pfA1Real,
1180 : float *pfA1Imag,
1181 : float *pfPredStateReal,
1182 : float *pfPredStateImag )
1183 : {
1184 : int32_t b, n;
1185 : int32_t iFBOffset;
1186 : int32_t k, iAlloc, iMaxQuantVal;
1187 : float fSCFGain, fInvSCFGain;
1188 :
1189 0 : iFBOffset = 0;
1190 0 : for ( b = 0; b < iNumBands; b++ )
1191 : {
1192 : int32_t m;
1193 0 : for ( m = 0; m < piBandwidths[b]; m++ )
1194 : {
1195 0 : int32_t iBlockOffset = 0;
1196 0 : if ( piPredEnable[iFBOffset] == 1 )
1197 : {
1198 : float fReal;
1199 : float fImag;
1200 0 : int32_t iSubset = iFBOffset % iNumSubSets;
1201 0 : float fPrevReal = 0.0f;
1202 0 : float fPrevImag = 0.0f;
1203 0 : if ( iSubset != iSubSetId )
1204 : {
1205 : /* run predictors across sub-frames */
1206 0 : fPrevReal = pfPredStateReal[iFBOffset];
1207 0 : fPrevImag = pfPredStateImag[iFBOffset];
1208 : }
1209 0 : for ( n = 0; n < iNumGroups; n++ )
1210 : {
1211 0 : iAlloc = ppiAlloc[n][b];
1212 0 : iMaxQuantVal = c_aiQuantMaxValues[iAlloc];
1213 0 : fSCFGain = c_afScaleFactor[iAlloc];
1214 0 : fInvSCFGain = c_afInvScaleFactor[iAlloc];
1215 0 : for ( k = 0; k < piGroupLengths[n]; k++ )
1216 : {
1217 : /* prediction */
1218 0 : fReal = pfA1Real[iFBOffset] * fPrevReal - pfA1Imag[iFBOffset] * fPrevImag;
1219 0 : fImag = pfA1Real[iFBOffset] * fPrevImag + pfA1Imag[iFBOffset] * fPrevReal;
1220 :
1221 0 : ppiQReal[iBlockOffset][iFBOffset] = Quantize( ppfReal[iBlockOffset][iFBOffset] + fReal, /* quantize residual */
1222 : fSCFGain,
1223 0 : &ppiSignReal[iBlockOffset][iFBOffset],
1224 : iMaxQuantVal );
1225 :
1226 0 : ppiQImag[iBlockOffset][iFBOffset] = Quantize( ppfImag[iBlockOffset][iFBOffset] + fImag,
1227 : fSCFGain,
1228 0 : &ppiSignImag[iBlockOffset][iFBOffset],
1229 : iMaxQuantVal );
1230 :
1231 0 : fPrevReal = UnQuantize( ppiQReal[iBlockOffset][iFBOffset],
1232 : fInvSCFGain,
1233 0 : ppiSignReal[iBlockOffset][iFBOffset] ) -
1234 : fReal; /* add prediction to quantized residual = reconstructed sample */
1235 :
1236 0 : fPrevImag = UnQuantize( ppiQImag[iBlockOffset][iFBOffset],
1237 : fInvSCFGain,
1238 0 : ppiSignImag[iBlockOffset][iFBOffset] ) -
1239 : fImag;
1240 :
1241 0 : iBlockOffset++;
1242 : } /* group length */
1243 : } /* groups */
1244 0 : pfPredStateReal[iFBOffset] = fPrevReal;
1245 0 : pfPredStateImag[iFBOffset] = fPrevImag;
1246 : } /* predEnable */
1247 : else
1248 : { /* no prediction */
1249 0 : for ( n = 0; n < iNumGroups; n++ )
1250 : {
1251 0 : iAlloc = ppiAlloc[n][b];
1252 0 : iMaxQuantVal = c_aiQuantMaxValues[iAlloc];
1253 0 : fSCFGain = c_afScaleFactor[iAlloc];
1254 0 : fInvSCFGain = c_afInvScaleFactor[iAlloc];
1255 0 : for ( k = 0; k < piGroupLengths[n]; k++ )
1256 : {
1257 0 : ppiQReal[iBlockOffset][iFBOffset] = Quantize( ppfReal[iBlockOffset][iFBOffset],
1258 : fSCFGain,
1259 0 : &ppiSignReal[iBlockOffset][iFBOffset],
1260 : iMaxQuantVal );
1261 :
1262 0 : ppiQImag[iBlockOffset][iFBOffset] = Quantize( ppfImag[iBlockOffset][iFBOffset],
1263 : fSCFGain,
1264 0 : &ppiSignImag[iBlockOffset][iFBOffset],
1265 : iMaxQuantVal );
1266 :
1267 0 : iBlockOffset++;
1268 : } /* group length */
1269 : } /* groups */
1270 : } /* predEnable */
1271 0 : iFBOffset++;
1272 : } /* bandwidth */
1273 : } /* bands */
1274 :
1275 0 : return;
1276 : }
1277 :
1278 :
1279 0 : static int32_t CountLCLDBits(
1280 : const int32_t iNumGroups,
1281 : const int32_t *piGroupLengths,
1282 : const int32_t iNumBands,
1283 : const int32_t *piBandwidths,
1284 : const int32_t *piPredEnable,
1285 : int32_t **ppiAlloc,
1286 : int32_t **ppiQReal,
1287 : int32_t **ppiQImag )
1288 : {
1289 : int32_t k, n, b, iFBOffset;
1290 : int32_t iBits, iBlockOffest;
1291 : int32_t m, iAlloc, iHuffDim, iHuffMod;
1292 :
1293 0 : iBits = 0;
1294 0 : iBlockOffest = 0;
1295 0 : for ( n = 0; n < iNumGroups; n++ )
1296 : {
1297 0 : for ( k = 0; k < piGroupLengths[n]; k++ )
1298 : {
1299 0 : iFBOffset = 0;
1300 0 : for ( b = 0; b < iNumBands; b++ )
1301 : {
1302 0 : iAlloc = ppiAlloc[n][b];
1303 :
1304 0 : iHuffDim = c_aiHuffmanDim[iAlloc];
1305 0 : iHuffMod = c_aiHuffmanMod[iAlloc];
1306 :
1307 0 : if ( iAlloc > 0 )
1308 : {
1309 0 : const uint16_t( *pauiHuffmanTable )[2] = NULL;
1310 0 : const uint16_t( *pauiHuffmanTableDPCM )[2] = NULL;
1311 :
1312 0 : pauiHuffmanTable = c_apauiHuffEncTabels[iAlloc];
1313 0 : pauiHuffmanTableDPCM = c_apauiHuffEncTabels[ALLOC_TABLE_SIZE + iAlloc];
1314 :
1315 0 : for ( m = 0; m < piBandwidths[b]; m++ )
1316 : {
1317 : int32_t iQuantValue1;
1318 : int32_t iQuantValue2;
1319 :
1320 0 : iQuantValue1 = ppiQReal[iBlockOffest][iFBOffset];
1321 0 : iQuantValue2 = ppiQImag[iBlockOffest][iFBOffset];
1322 :
1323 0 : iBits += ( iQuantValue1 > 0 ) ? 1 : 0; /* Sign bit for vals > 0 */
1324 0 : iBits += ( iQuantValue2 > 0 ) ? 1 : 0; /* Sign bit for vals > 0 */
1325 :
1326 0 : if ( piPredEnable[iFBOffset] == 1 )
1327 : {
1328 0 : if ( iHuffDim == 2 )
1329 : {
1330 0 : iQuantValue1 *= iHuffMod;
1331 0 : iQuantValue1 += iQuantValue2;
1332 0 : iBits += pauiHuffmanTableDPCM[iQuantValue1][0];
1333 : }
1334 : else
1335 : {
1336 0 : iBits += pauiHuffmanTableDPCM[iQuantValue1][0];
1337 0 : iBits += pauiHuffmanTableDPCM[iQuantValue2][0];
1338 : }
1339 : }
1340 : else
1341 : {
1342 0 : if ( iHuffDim == 2 )
1343 : {
1344 0 : iQuantValue1 *= iHuffMod;
1345 0 : iQuantValue1 += iQuantValue2;
1346 0 : iBits += pauiHuffmanTable[iQuantValue1][0];
1347 : }
1348 : else
1349 : {
1350 0 : iBits += pauiHuffmanTable[iQuantValue1][0];
1351 0 : iBits += pauiHuffmanTable[iQuantValue2][0];
1352 : }
1353 : }
1354 :
1355 0 : iFBOffset++;
1356 : }
1357 : }
1358 : else
1359 : {
1360 0 : iFBOffset += piBandwidths[b];
1361 : }
1362 : }
1363 :
1364 0 : iBlockOffest++;
1365 : }
1366 : }
1367 :
1368 0 : return iBits;
1369 : }
1370 :
1371 :
1372 : /* Currently only the number of bands in frame */
1373 0 : static int32_t WriteHeaderInformation(
1374 : const int32_t iNumBands,
1375 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1376 : {
1377 : int32_t iBitsWritten;
1378 :
1379 0 : iBitsWritten = 0;
1380 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iNumBands, 5 );
1381 0 : iBitsWritten += 5;
1382 :
1383 0 : return iBitsWritten;
1384 : }
1385 :
1386 :
1387 0 : static int32_t WriteMSInformation(
1388 : const int32_t iNumBands,
1389 : const int32_t iMSMode,
1390 : const int32_t *piMSFlags,
1391 : const int32_t *piLRPhaseDiff,
1392 : const int32_t *piMSPredCoef,
1393 : const int32_t iNumMSPredBands,
1394 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1395 : {
1396 : int32_t iBitsWritten;
1397 0 : int32_t iMSPredAll = ( iNumMSPredBands == iNumBands );
1398 : #ifdef DEBUG_WRITE_MS_PRED
1399 : int32_t iBitsWrittenTmp = 0;
1400 : #endif
1401 :
1402 0 : iBitsWritten = 0;
1403 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iMSMode, 2 );
1404 0 : iBitsWritten += 2;
1405 :
1406 0 : if ( iMSMode == 3 )
1407 : {
1408 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iMSPredAll, 1 );
1409 0 : iBitsWritten += 1;
1410 : }
1411 :
1412 0 : if ( iMSMode == 2 || ( iMSMode == 3 && !iMSPredAll ) )
1413 : {
1414 : int32_t n;
1415 0 : for ( n = 0; n < iNumBands; n++ )
1416 : {
1417 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, piMSFlags[n], 1 );
1418 0 : iBitsWritten += 1;
1419 : }
1420 : }
1421 :
1422 : #ifdef DEBUG_WRITE_MS_PRED
1423 : iBitsWrittenTmp = iBitsWritten;
1424 : #endif
1425 0 : if ( iMSMode == 3 )
1426 : {
1427 : int32_t b;
1428 : int32_t anyNonZero;
1429 0 : anyNonZero = 0;
1430 0 : for ( b = 0; b < iNumMSPredBands; b++ )
1431 : {
1432 0 : if ( piLRPhaseDiff[b] != 0 )
1433 : {
1434 0 : anyNonZero = 1;
1435 0 : break;
1436 : }
1437 : }
1438 :
1439 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, anyNonZero, 1 );
1440 0 : iBitsWritten++;
1441 :
1442 0 : if ( anyNonZero )
1443 : {
1444 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, piLRPhaseDiff[0] - PHASE_MIN_VAL, PHASE_BAND0_BITS );
1445 0 : iBitsWritten += PHASE_BAND0_BITS;
1446 0 : for ( b = 1; b < iNumMSPredBands; b++ )
1447 : {
1448 0 : int32_t tabIdx = piLRPhaseDiff[b] - ENV_DELTA_MIN;
1449 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[tabIdx][1], c_aaiRMSEnvHuffEnc[tabIdx][0] );
1450 0 : iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
1451 : }
1452 : }
1453 :
1454 0 : anyNonZero = 0;
1455 0 : for ( b = 0; b < iNumMSPredBands; b++ )
1456 : {
1457 0 : if ( piMSPredCoef[b] != 0 )
1458 : {
1459 0 : anyNonZero = 1;
1460 0 : break;
1461 : }
1462 : }
1463 :
1464 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, anyNonZero, 1 );
1465 0 : iBitsWritten++;
1466 :
1467 0 : if ( anyNonZero )
1468 : {
1469 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, piMSPredCoef[0] - PRED_MIN_VAL, PRED_BAND0_BITS );
1470 0 : iBitsWritten += PRED_BAND0_BITS;
1471 0 : for ( b = 1; b < iNumMSPredBands; b++ )
1472 : {
1473 0 : int32_t tabIdx = piMSPredCoef[b] - ENV_DELTA_MIN;
1474 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[tabIdx][1], c_aaiRMSEnvHuffEnc[tabIdx][0] );
1475 0 : iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
1476 : }
1477 : }
1478 : }
1479 : #ifdef DEBUG_WRITE_MS_PRED
1480 : {
1481 : static FILE *fid = 0;
1482 : if ( !fid )
1483 : {
1484 : fid = fopen( "ms_pred_bitrate.txt", "wt" );
1485 : }
1486 : fprintf( fid, "%f\n", (float) ( ( iBitsWritten - iBitsWrittenTmp ) * ( iMSMode == 3 ) * 50 ) / 1000.0f ); /*kb/s*/
1487 : }
1488 : #endif
1489 :
1490 0 : return iBitsWritten;
1491 : }
1492 :
1493 :
1494 0 : static int32_t WriteGroupInformation(
1495 : const int32_t iChannels,
1496 : const int32_t iCommonGrouping,
1497 : const int32_t *piNumGroups,
1498 : int32_t **ppiGroupLengths,
1499 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1500 : {
1501 : int32_t c, k, n, iBitsWritten;
1502 :
1503 0 : iBitsWritten = 0;
1504 0 : if ( iChannels == 2 && iCommonGrouping == 1 )
1505 : {
1506 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iCommonGrouping, 1 );
1507 0 : iBitsWritten += 1;
1508 :
1509 0 : for ( n = 0; n < piNumGroups[0]; n++ )
1510 : {
1511 0 : for ( k = 1; k < ppiGroupLengths[0][n]; k++ )
1512 : {
1513 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
1514 0 : iBitsWritten += 1;
1515 : }
1516 0 : if ( n < ( piNumGroups[0] - 1 ) )
1517 : {
1518 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
1519 0 : iBitsWritten += 1;
1520 : }
1521 : }
1522 : }
1523 0 : else if ( iChannels == 2 )
1524 : {
1525 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, iCommonGrouping, 1 );
1526 0 : iBitsWritten += 1;
1527 :
1528 0 : for ( c = 0; c < iChannels; c++ )
1529 : {
1530 0 : for ( n = 0; n < piNumGroups[c]; n++ )
1531 : {
1532 0 : for ( k = 1; k < ppiGroupLengths[c][n]; k++ )
1533 : {
1534 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
1535 0 : iBitsWritten += 1;
1536 : }
1537 0 : if ( n < ( piNumGroups[c] - 1 ) )
1538 : {
1539 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
1540 0 : iBitsWritten += 1;
1541 : }
1542 : }
1543 : }
1544 : }
1545 : else
1546 : {
1547 0 : for ( c = 0; c < iChannels; c++ )
1548 : {
1549 0 : for ( n = 0; n < piNumGroups[c]; n++ )
1550 : {
1551 0 : for ( k = 1; k < ppiGroupLengths[c][n]; k++ )
1552 : {
1553 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
1554 0 : iBitsWritten += 1;
1555 : }
1556 :
1557 0 : if ( n < ( piNumGroups[c] - 1 ) )
1558 : {
1559 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
1560 0 : iBitsWritten += 1;
1561 : }
1562 : }
1563 : }
1564 : }
1565 :
1566 0 : return iBitsWritten;
1567 : }
1568 :
1569 :
1570 0 : static int32_t WriteRMSEnvelope(
1571 : const int32_t iChannels,
1572 : const int32_t *piNumGroups,
1573 : const int32_t iNumBands,
1574 : int32_t ***pppiRMSEnvelope,
1575 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1576 : {
1577 : int32_t k, n;
1578 : int32_t iBitsWritten;
1579 :
1580 0 : iBitsWritten = 0;
1581 0 : for ( n = 0; n < iChannels; n++ )
1582 : {
1583 0 : for ( k = 0; k < piNumGroups[n]; k++ )
1584 : {
1585 : int32_t b;
1586 : int32_t iLastRMSVal;
1587 :
1588 0 : iLastRMSVal = pppiRMSEnvelope[n][k][0];
1589 0 : iLastRMSVal = ( iLastRMSVal > ENV_MIN ) ? iLastRMSVal : ENV_MIN;
1590 0 : iLastRMSVal = ( iLastRMSVal < ENV_MAX ) ? iLastRMSVal : ENV_MAX;
1591 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, ( iLastRMSVal - ENV_MIN ), ENV0_BITS );
1592 0 : iBitsWritten += ENV0_BITS;
1593 :
1594 0 : for ( b = 1; b < iNumBands; b++ )
1595 : {
1596 : int32_t iDelta;
1597 :
1598 0 : iDelta = pppiRMSEnvelope[n][k][b] - iLastRMSVal;
1599 0 : iDelta = ( iDelta > ENV_DELTA_MIN ) ? iDelta : ENV_DELTA_MIN;
1600 0 : iDelta = ( iDelta < ENV_DELTA_MAX ) ? iDelta : ENV_DELTA_MAX;
1601 0 : iDelta -= ENV_DELTA_MIN;
1602 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[iDelta][1], c_aaiRMSEnvHuffEnc[iDelta][0] );
1603 0 : iBitsWritten += c_aaiRMSEnvHuffEnc[iDelta][0];
1604 :
1605 0 : iLastRMSVal = pppiRMSEnvelope[n][k][b];
1606 : }
1607 : }
1608 : }
1609 :
1610 0 : return iBitsWritten;
1611 : }
1612 :
1613 :
1614 0 : static int32_t WriteAllocInformation(
1615 : const int32_t iAllocOffset,
1616 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1617 : {
1618 : int32_t iBitsWritten;
1619 :
1620 0 : iBitsWritten = 0;
1621 :
1622 0 : if ( iAllocOffset < MIN_ALLOC_OFFSET || iAllocOffset > MAX_ALLOC_OFFSET )
1623 : {
1624 0 : printf( "Serious error\n" );
1625 : }
1626 :
1627 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, ( iAllocOffset - MIN_ALLOC_OFFSET ), ALLOC_OFFSET_BITS );
1628 0 : iBitsWritten += ALLOC_OFFSET_BITS;
1629 :
1630 0 : return iBitsWritten;
1631 : }
1632 :
1633 :
1634 0 : static int32_t WriteLCLDData(
1635 : const int32_t *piNumGroups,
1636 : int32_t **ppiGroupLengths,
1637 : const int32_t iNumBands,
1638 : const int32_t iNumChannels,
1639 : int32_t **ppiPredEnable,
1640 : const int32_t iNumSubSets,
1641 : const int32_t iSubSetId,
1642 : int32_t ***pppiAlloc,
1643 : int32_t ***pppiSignReal,
1644 : int32_t ***pppiSignImag,
1645 : int32_t ***pppiQReal,
1646 : int32_t ***pppiQImag,
1647 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1648 : {
1649 : int32_t iBitsWritten;
1650 0 : int32_t iNumLcldBands = c_aiNumLcldBandsPerBand[iNumBands - 1];
1651 : int32_t s;
1652 0 : int32_t iSet = iSubSetId;
1653 :
1654 0 : iBitsWritten = 0;
1655 0 : for ( s = 0; s < iNumSubSets; s++, iSet-- )
1656 : {
1657 : int32_t ch;
1658 0 : if ( iSet < 0 )
1659 : {
1660 0 : iSet = iNumSubSets - 1;
1661 : }
1662 :
1663 0 : for ( ch = 0; ch < iNumChannels; ch++ )
1664 : {
1665 0 : int32_t iBlockOffest = 0;
1666 : int32_t n;
1667 0 : for ( n = 0; n < piNumGroups[ch]; n++ )
1668 : {
1669 : int32_t k;
1670 0 : for ( k = 0; k < ppiGroupLengths[ch][n]; k++ )
1671 : {
1672 : int32_t iFBOffset;
1673 0 : for ( iFBOffset = iSet; iFBOffset < iNumLcldBands; iFBOffset += iNumSubSets )
1674 : {
1675 : int32_t b;
1676 : int32_t iAlloc;
1677 : int32_t iHuffDim;
1678 : int32_t iHuffMod;
1679 :
1680 0 : b = c_aiBandIdPerLcldBand[iFBOffset];
1681 :
1682 0 : iAlloc = pppiAlloc[ch][n][b];
1683 :
1684 0 : iHuffDim = c_aiHuffmanDim[iAlloc];
1685 0 : iHuffMod = c_aiHuffmanMod[iAlloc];
1686 :
1687 0 : if ( iAlloc > 0 )
1688 : {
1689 0 : const uint16_t( *pauiHuffmanTable )[2] = NULL;
1690 0 : const uint16_t( *pauiHuffmanTableDPCM )[2] = NULL;
1691 : int32_t iQuantValue1;
1692 : int32_t iQuantValue2;
1693 0 : pauiHuffmanTable = c_apauiHuffEncTabels[iAlloc];
1694 0 : pauiHuffmanTableDPCM = c_apauiHuffEncTabels[ALLOC_TABLE_SIZE + iAlloc];
1695 :
1696 0 : iQuantValue1 = pppiQReal[ch][iBlockOffest][iFBOffset];
1697 0 : iQuantValue2 = pppiQImag[ch][iBlockOffest][iFBOffset];
1698 : #ifdef LCLD_HANDLE_PRED_START_SAMPLE
1699 : if ( ppiPredEnable[ch][iFBOffset] == 1 && ( iBlockOffest > 0 || iSet != iSubSetId ) )
1700 : #else
1701 0 : if ( ppiPredEnable[ch][iFBOffset] == 1 )
1702 : #endif
1703 : {
1704 0 : if ( iHuffDim == 2 )
1705 : {
1706 : int32_t iSymbol;
1707 0 : iSymbol = iQuantValue1;
1708 0 : iSymbol *= iHuffMod;
1709 0 : iSymbol += iQuantValue2;
1710 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iSymbol][1], pauiHuffmanTableDPCM[iSymbol][0] );
1711 0 : iBitsWritten += pauiHuffmanTableDPCM[iSymbol][0];
1712 : }
1713 : else
1714 : {
1715 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iQuantValue1][1], pauiHuffmanTableDPCM[iQuantValue1][0] );
1716 0 : iBitsWritten += pauiHuffmanTableDPCM[iQuantValue1][0];
1717 :
1718 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iQuantValue2][1], pauiHuffmanTableDPCM[iQuantValue2][0] );
1719 0 : iBitsWritten += pauiHuffmanTableDPCM[iQuantValue2][0];
1720 : }
1721 : }
1722 : else
1723 : {
1724 0 : if ( iHuffDim == 2 )
1725 : {
1726 : int32_t iSymbol;
1727 0 : iSymbol = iQuantValue1;
1728 0 : iSymbol *= iHuffMod;
1729 0 : iSymbol += iQuantValue2;
1730 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iSymbol][1], pauiHuffmanTable[iSymbol][0] );
1731 0 : iBitsWritten += pauiHuffmanTable[iSymbol][0];
1732 : }
1733 : else
1734 : {
1735 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iQuantValue1][1], pauiHuffmanTable[iQuantValue1][0] );
1736 0 : iBitsWritten += pauiHuffmanTable[iQuantValue1][0];
1737 :
1738 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iQuantValue2][1], pauiHuffmanTable[iQuantValue2][0] );
1739 0 : iBitsWritten += pauiHuffmanTable[iQuantValue2][0];
1740 : }
1741 : }
1742 :
1743 0 : if ( iQuantValue1 > 0 )
1744 : {
1745 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pppiSignReal[ch][iBlockOffest][iFBOffset], 1 );
1746 0 : iBitsWritten += 1;
1747 : }
1748 0 : if ( iQuantValue2 > 0 )
1749 : {
1750 0 : ISAR_SPLIT_REND_BITStream_write_int32( pBits, pppiSignImag[ch][iBlockOffest][iFBOffset], 1 );
1751 0 : iBitsWritten += 1;
1752 : }
1753 : }
1754 : }
1755 0 : iBlockOffest++;
1756 : }
1757 : }
1758 : }
1759 : }
1760 :
1761 0 : return iBitsWritten;
1762 : }
1763 :
1764 :
1765 0 : static int32_t ComputeAllocation(
1766 : const int32_t iChannels,
1767 : const int32_t *piNumGroups,
1768 : int32_t **ppiGroupLengths,
1769 : const int32_t iNumBands,
1770 : const int32_t *piBandwidths,
1771 : float ***pppfReal,
1772 : float ***pppfImag,
1773 : int32_t ***pppiSMR,
1774 : const int32_t iAvailableBits,
1775 : int32_t *piAllocOffset,
1776 : int32_t ***pppiAlloc,
1777 : int32_t ***pppiQReal,
1778 : int32_t ***pppiQImag,
1779 : int32_t ***pppiSignReal,
1780 : int32_t ***pppiSignImag,
1781 : PredictionEncoder *psPredictionEncoder )
1782 : {
1783 : int32_t iBitsUsed, iDone, iDelta;
1784 : int32_t b, k, n;
1785 : int32_t iLimitAllocOffset;
1786 :
1787 0 : iBitsUsed = ALLOC_OFFSET_BITS; /* Bits used for Alloc Offset */
1788 :
1789 0 : iDone = 0;
1790 0 : iDelta = -MIN_ALLOC_OFFSET;
1791 0 : *piAllocOffset = 0;
1792 :
1793 0 : while ( iDone == 0 )
1794 : {
1795 0 : iBitsUsed = ALLOC_OFFSET_BITS;
1796 :
1797 0 : iLimitAllocOffset = *piAllocOffset;
1798 0 : iLimitAllocOffset = ( iLimitAllocOffset > MIN_ALLOC_OFFSET ) ? iLimitAllocOffset : MIN_ALLOC_OFFSET;
1799 0 : iLimitAllocOffset = ( iLimitAllocOffset < MAX_ALLOC_OFFSET ) ? iLimitAllocOffset : MAX_ALLOC_OFFSET;
1800 :
1801 0 : for ( n = 0; n < iChannels; n++ )
1802 : {
1803 0 : for ( k = 0; k < piNumGroups[n]; k++ )
1804 : {
1805 0 : for ( b = 0; b < iNumBands; b++ )
1806 : {
1807 : int32_t iAlloc;
1808 0 : iAlloc = ( ( pppiSMR[n][k][b] + iLimitAllocOffset * ALLOC_OFFSET_SCALE ) >> 5 );
1809 0 : iAlloc = ( iAlloc > MIN_ALLOC ) ? iAlloc : MIN_ALLOC;
1810 0 : iAlloc = ( iAlloc < MAX_ALLOC ) ? iAlloc : MAX_ALLOC;
1811 0 : pppiAlloc[n][k][b] = iAlloc;
1812 : }
1813 : }
1814 :
1815 0 : if ( psPredictionEncoder->iNumSubSets > 1 )
1816 : {
1817 0 : mvr2r( psPredictionEncoder->ppfPredStateReal[n], psPredictionEncoder->ppfPredStateRealTmp[n], LCLD_BANDS );
1818 0 : mvr2r( psPredictionEncoder->ppfPredStateImag[n], psPredictionEncoder->ppfPredStateImagTmp[n], LCLD_BANDS );
1819 : }
1820 :
1821 0 : QuantizeSpectrumDPCM_Opt( piNumGroups[n],
1822 0 : (const int32_t *) ppiGroupLengths[n],
1823 : iNumBands,
1824 : piBandwidths,
1825 0 : pppiAlloc[n],
1826 0 : pppfReal[n],
1827 0 : pppfImag[n],
1828 0 : pppiQReal[n],
1829 0 : pppiQImag[n],
1830 0 : pppiSignReal[n],
1831 0 : pppiSignImag[n],
1832 : psPredictionEncoder->iNumSubSets,
1833 : psPredictionEncoder->iSubSetId,
1834 0 : psPredictionEncoder->ppiPredBandEnable[n],
1835 0 : psPredictionEncoder->ppfA1Real[n],
1836 0 : psPredictionEncoder->ppfA1Imag[n],
1837 0 : psPredictionEncoder->ppfPredStateRealTmp[n],
1838 0 : psPredictionEncoder->ppfPredStateImagTmp[n] );
1839 :
1840 0 : iBitsUsed += CountLCLDBits( piNumGroups[n],
1841 0 : (const int32_t *) ppiGroupLengths[n],
1842 : iNumBands,
1843 : piBandwidths,
1844 0 : (const int32_t *) psPredictionEncoder->ppiPredBandEnable[n],
1845 0 : pppiAlloc[n],
1846 0 : pppiQReal[n],
1847 0 : pppiQImag[n] );
1848 : }
1849 :
1850 0 : if ( *piAllocOffset <= MIN_ALLOC_OFFSET && iBitsUsed > iAvailableBits )
1851 : {
1852 : #ifdef DEBUG_VERBOSE
1853 : printf( "Frame can not be coded with the number of bits available\n" );
1854 : #endif
1855 : /* iLastError = ENC_ERROR_STREAM_FAILURE;*/
1856 0 : return -1;
1857 : }
1858 0 : else if ( *piAllocOffset >= MAX_ALLOC_OFFSET && iBitsUsed < iAvailableBits )
1859 : {
1860 0 : *piAllocOffset = MAX_ALLOC_OFFSET;
1861 0 : iDone++;
1862 : }
1863 : else
1864 : {
1865 0 : if ( iDelta == 0 && iBitsUsed > iAvailableBits )
1866 : {
1867 0 : iDelta = 1;
1868 : }
1869 0 : else if ( iDelta == 0 && iBitsUsed < iAvailableBits )
1870 : {
1871 0 : iDone++;
1872 : }
1873 0 : else if ( iBitsUsed == iAvailableBits )
1874 : {
1875 0 : iDone++;
1876 : }
1877 :
1878 0 : if ( iBitsUsed > iAvailableBits )
1879 : {
1880 0 : *piAllocOffset -= iDelta;
1881 0 : iDelta >>= 1;
1882 : }
1883 0 : else if ( iBitsUsed < iAvailableBits )
1884 : {
1885 0 : *piAllocOffset += iDelta;
1886 0 : iDelta >>= 1;
1887 : }
1888 : }
1889 : }
1890 :
1891 0 : if ( psPredictionEncoder->iNumSubSets > 1 )
1892 : {
1893 0 : for ( n = 0; n < iChannels; n++ )
1894 : {
1895 0 : mvr2r( psPredictionEncoder->ppfPredStateRealTmp[n], psPredictionEncoder->ppfPredStateReal[n], LCLD_BANDS );
1896 0 : mvr2r( psPredictionEncoder->ppfPredStateImagTmp[n], psPredictionEncoder->ppfPredStateImag[n], LCLD_BANDS );
1897 : }
1898 : }
1899 :
1900 : #ifdef DEBUGGING
1901 : /*
1902 : printf("%d\n",*piAllocOffset);
1903 : printf("%d\t%d\t%d\n",pppiAlloc[0][0][0],pppiAlloc[0][0][1],pppiAlloc[0][0][22]);
1904 :
1905 : printf("%d\t%d\t%d\t%d\n",*piAllocOffset,iAvailableBits,iBitsUsed,iAvailableBits - iBitsUsed);
1906 : */
1907 : #endif
1908 0 : return iBitsUsed;
1909 : }
|