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 "options.h"
34 : #include <stdint.h>
35 : #include "isar_rom_lcld_tables.h"
36 : #include "isar_lcld_prot.h"
37 : #include "isar_prot.h"
38 : #include "wmc_auto.h"
39 : #include "basop_util.h"
40 : #include "enh64.h"
41 : #include <assert.h>
42 :
43 :
44 : /*-------------------------------------------------------------------*
45 : * Function cplxmult()
46 : *
47 : *
48 : *-------------------------------------------------------------------*/
49 :
50 0 : void cplxmult_fx(
51 : Word32 *pr1,
52 : Word32 *pi1,
53 : const Word32 r2,
54 : const Word32 i2 )
55 : {
56 0 : Word32 r1 = *pr1, i1 = *pi1;
57 :
58 0 : *pr1 = L_sub( Mpy_32_32( r1, r2 ), Mpy_32_32( i1, i2 ) );
59 0 : *pi1 = L_add( Mpy_32_32( r1, i2 ), Mpy_32_32( i1, r2 ) );
60 :
61 0 : return;
62 : }
63 :
64 :
65 : /*-------------------------------------------------------------------*
66 : * Function quantPhase()
67 : *
68 : *
69 : *-------------------------------------------------------------------*/
70 :
71 0 : Word32 quantPhase_fx(
72 : const Word32 phase,
73 : const Word16 exp ) // i:31?
74 : {
75 : Word32 phaseQ;
76 :
77 : // phaseQ = _round(phase * PHASE_QUANT_FACTOR);
78 0 : phaseQ = L_shr_r( Mpy_32_32( phase, PHASE_QUANT_FACTOR_FX_Q29 ), 29 - exp ); // Q60
79 0 : if ( phaseQ == PHASE_MAX_VAL )
80 : {
81 0 : phaseQ = PHASE_MIN_VAL;
82 : }
83 0 : return phaseQ;
84 : }
85 :
86 :
87 : /*-------------------------------------------------------------------*
88 : * Function requantPhase()
89 : *
90 : *
91 : *-------------------------------------------------------------------*/
92 :
93 0 : Word32 requantPhase(
94 : Word32 phaseQ )
95 : {
96 :
97 0 : IF( GE_32( phaseQ, PHASE_MAX_VAL ) )
98 : {
99 0 : phaseQ = L_add( phaseQ, PHASE_MAX_VAL );
100 0 : phaseQ %= ( L_shl( PHASE_MAX_VAL, 1 ) );
101 0 : phaseQ = L_sub( phaseQ, PHASE_MAX_VAL );
102 : }
103 0 : ELSE IF( LT_32( phaseQ, PHASE_MIN_VAL ) )
104 : {
105 0 : phaseQ = L_sub( phaseQ, PHASE_MAX_VAL );
106 0 : phaseQ %= ( L_shl( PHASE_MAX_VAL, 1 ) );
107 0 : phaseQ = L_add( phaseQ, PHASE_MAX_VAL );
108 0 : IF( EQ_32( phaseQ, PHASE_MAX_VAL ) )
109 : {
110 0 : phaseQ = PHASE_MIN_VAL;
111 0 : move32();
112 : }
113 : }
114 :
115 0 : return phaseQ;
116 : }
117 :
118 :
119 : /*-------------------------------------------------------------------*
120 : * Function quantPred()
121 : *
122 : *
123 : *-------------------------------------------------------------------*/
124 :
125 0 : Word32 quantPred_fx(
126 : const Word32 pred,
127 : const Word16 exp ) // i:Q31
128 : {
129 : // int32_t predQ = _round(pred * PRED_QUANT_FACTOR);
130 0 : Word32 predQ = W_round32_s( W_shl( W_mult_32_32( pred, (Word32) PRED_QUANT_FACTOR ), exp ) );
131 0 : predQ = predQ > PRED_MAX_VAL ? PRED_MAX_VAL : predQ;
132 0 : predQ = predQ < PRED_MIN_VAL ? PRED_MIN_VAL : predQ;
133 :
134 0 : return predQ;
135 : }
136 :
137 :
138 : /*-------------------------------------------------------------------*
139 : * Function dequantPred()
140 : *
141 : *
142 : *-------------------------------------------------------------------*/
143 :
144 0 : Word32 dequantPred_fx(
145 : const Word32 predQ )
146 : {
147 0 : if ( predQ == 12 )
148 : {
149 0 : return MAX_32;
150 : }
151 : Word16 tmp_e;
152 : Word32 tmp, value;
153 0 : if ( predQ == 0 )
154 : {
155 0 : return predQ;
156 : }
157 0 : tmp = BASOP_Util_Divide3216_Scale( predQ, PRED_QUANT_FACTOR_FX, &tmp_e );
158 0 : value = L_shl( tmp, sub( 31, negate( add( 1, tmp_e ) ) ) ); // Q31
159 0 : return value;
160 : }
161 :
162 :
163 : /*-------------------------------------------------------------------*
164 : * Function PrepEncode()
165 : *
166 : *
167 : *-------------------------------------------------------------------*/
168 :
169 0 : Word32 PrepEncode(
170 : Word32 *piQuant,
171 : const Word32 *piMSFlags,
172 : const Word32 numBands )
173 : {
174 : Word32 b, numMSBands;
175 0 : numMSBands = 0;
176 0 : move32();
177 :
178 0 : FOR( b = 0; b < numBands; b++ )
179 : {
180 0 : IF( piMSFlags[b] )
181 : {
182 0 : piQuant[numMSBands++] = piQuant[b];
183 0 : move32();
184 : }
185 : }
186 :
187 0 : FOR( b = numMSBands; b < numBands; b++ )
188 : {
189 0 : piQuant[b] = 0;
190 0 : move32();
191 : }
192 :
193 0 : return numMSBands;
194 : }
195 :
196 :
197 : /*-------------------------------------------------------------------*
198 : * Function EncodePhase()
199 : *
200 : *
201 : *-------------------------------------------------------------------*/
202 :
203 0 : void EncodePhase(
204 : Word32 *phaseQuant,
205 : const Word32 numMSBands,
206 : const Word32 diffDim )
207 : {
208 : Word32 b;
209 :
210 0 : IF( GT_32( diffDim, 0 ) )
211 : {
212 : Word32 tmp1, tmp2;
213 0 : tmp1 = phaseQuant[0];
214 0 : move32();
215 0 : FOR( b = 1; b < numMSBands; b++ )
216 : {
217 0 : tmp2 = phaseQuant[b];
218 0 : move32();
219 0 : phaseQuant[b] = L_sub( phaseQuant[b], tmp1 );
220 0 : tmp1 = tmp2;
221 0 : move32();
222 : }
223 : }
224 :
225 0 : IF( GT_32( diffDim, 1 ) )
226 : {
227 : Word32 tmp1, tmp2;
228 0 : tmp1 = phaseQuant[1];
229 0 : move32();
230 0 : FOR( b = 2; b < numMSBands; b++ )
231 : {
232 0 : tmp2 = phaseQuant[b];
233 0 : move32();
234 0 : phaseQuant[b] = L_sub( phaseQuant[b], tmp1 );
235 0 : tmp1 = tmp2;
236 0 : move32();
237 : }
238 : }
239 0 : FOR( b = 1; b < numMSBands; b++ )
240 : {
241 0 : phaseQuant[b] = requantPhase( phaseQuant[b] );
242 : }
243 :
244 0 : return;
245 : }
246 :
247 :
248 : /*-------------------------------------------------------------------*
249 : * Function DecodePhase()
250 : *
251 : *
252 : *-------------------------------------------------------------------*/
253 :
254 0 : void DecodePhase(
255 : Word32 *phaseQuant,
256 : const Word32 numMSBands,
257 : const Word32 diffDim )
258 : {
259 : Word32 b;
260 :
261 0 : IF( GT_32( diffDim, 1 ) )
262 : {
263 0 : FOR( b = 2; b < numMSBands; b++ )
264 : {
265 0 : phaseQuant[b] = L_add( phaseQuant[b], phaseQuant[b - 1] );
266 0 : move32();
267 : }
268 : }
269 :
270 0 : IF( GT_32( diffDim, 0 ) )
271 : {
272 0 : FOR( b = 1; b < numMSBands; b++ )
273 : {
274 0 : phaseQuant[b] = L_add( phaseQuant[b], phaseQuant[b - 1] );
275 0 : move32();
276 : }
277 : }
278 :
279 0 : FOR( b = 1; b < numMSBands; b++ )
280 : {
281 0 : phaseQuant[b] = requantPhase( phaseQuant[b] );
282 0 : move32();
283 : }
284 :
285 0 : return;
286 : }
287 :
288 :
289 : /*-------------------------------------------------------------------*
290 : * Function EncodePredCoef()
291 : *
292 : *
293 : *-------------------------------------------------------------------*/
294 :
295 0 : Word32 EncodePredCoef(
296 : Word32 *predQuant,
297 : const Word32 numMSBands )
298 : {
299 : Word32 b, tmp1, tmp2;
300 :
301 0 : tmp1 = predQuant[0];
302 0 : move32();
303 0 : FOR( b = 1; b < numMSBands; b++ )
304 : {
305 0 : tmp2 = predQuant[b];
306 0 : move32();
307 0 : predQuant[b] = L_sub( predQuant[b], tmp1 );
308 0 : tmp1 = tmp2;
309 0 : move32();
310 : }
311 :
312 0 : return numMSBands;
313 : }
314 :
315 :
316 : /*-------------------------------------------------------------------*
317 : * Function DecodePredCoef()
318 : *
319 : *
320 : *-------------------------------------------------------------------*/
321 :
322 0 : void DecodePredCoef(
323 : Word32 *phaseQuant,
324 : const Word32 numMSBands )
325 : {
326 : Word32 b;
327 :
328 0 : FOR( b = 1; b < numMSBands; b++ )
329 : {
330 0 : phaseQuant[b] = L_add( phaseQuant[b], phaseQuant[b - 1] );
331 : }
332 :
333 0 : return;
334 : }
335 :
336 :
337 : /*-------------------------------------------------------------------*
338 : * Function CountMSBits()
339 : *
340 : *
341 : *-------------------------------------------------------------------*/
342 :
343 0 : Word32 CountMSBits(
344 : Word32 iNumBands,
345 : const Word32 iMSMode,
346 : const Word32 *piMSFlags,
347 : const Word32 *piLRPhaseDiff,
348 : const Word32 *piMSPredCoef )
349 : {
350 0 : Word32 iBitsWritten = 0;
351 0 : move32();
352 : Word32 b;
353 : Word32 anyNonZero;
354 0 : Word32 iNumMSBands = 0;
355 0 : move32();
356 : Word32 piPhaseCopy[MAX_BANDS_48];
357 : Word32 piPredCopy[MAX_BANDS_48];
358 :
359 0 : FOR( b = 0; b < MAX_BANDS_48; b++ )
360 : {
361 0 : piPhaseCopy[b] = 0;
362 0 : move32();
363 0 : piPredCopy[b] = 0;
364 0 : move32();
365 : }
366 :
367 0 : iBitsWritten = L_add( iBitsWritten, 2 ); /* iMSMode */
368 0 : FOR( b = 0; b < iNumBands; b++ )
369 : {
370 0 : iNumMSBands = L_add( iNumMSBands, NE_32( piMSFlags[b], 0 ) );
371 : }
372 :
373 0 : IF( EQ_32( iNumMSBands, 0 ) )
374 : {
375 0 : return iBitsWritten;
376 : }
377 :
378 0 : IF( LT_32( iNumMSBands, iNumBands ) )
379 : {
380 0 : iBitsWritten = L_add( iBitsWritten, iNumBands ); /* piMSFlags */
381 : }
382 :
383 0 : IF( LT_32( iMSMode, 3 ) )
384 : {
385 0 : return iBitsWritten;
386 : }
387 :
388 : /* prepare arrays for coding evaluation */
389 0 : FOR( b = 0; b < iNumBands; b++ )
390 : {
391 0 : piPhaseCopy[b] = piLRPhaseDiff[b];
392 0 : move32();
393 0 : piPredCopy[b] = piMSPredCoef[b];
394 0 : move32();
395 : }
396 :
397 : /* Differential Coding of Phase Data*/
398 0 : PrepEncode( piPhaseCopy, piMSFlags, iNumBands );
399 0 : PrepEncode( piPredCopy, piMSFlags, iNumBands );
400 0 : EncodePhase( piPhaseCopy, iNumMSBands, PHASE_DIFF_DIM );
401 0 : EncodePredCoef( piPredCopy, iNumMSBands );
402 :
403 0 : iBitsWritten = L_add( iBitsWritten, 1 ); /* iMSPredAll */
404 0 : anyNonZero = 0; /* phase */
405 0 : move32();
406 0 : FOR( b = 0; b < iNumMSBands; b++ )
407 : {
408 0 : IF( NE_32( piPhaseCopy[b], 0 ) )
409 : {
410 0 : anyNonZero = 1;
411 0 : move32();
412 0 : BREAK;
413 : }
414 : }
415 0 : iBitsWritten = L_add( iBitsWritten, 1 ); /*anyNonZero Phase*/
416 0 : IF( anyNonZero )
417 : {
418 0 : iBitsWritten = L_add( iBitsWritten, PHASE_BAND0_BITS );
419 0 : FOR( b = 1; b < iNumMSBands; b++ )
420 : {
421 0 : Word32 tabIdx = L_sub( piPhaseCopy[b], ENV_DELTA_MIN );
422 0 : iBitsWritten = L_add( iBitsWritten, c_aaiRMSEnvHuffEnc[tabIdx][0] );
423 : }
424 : }
425 0 : anyNonZero = 0; /* prediction */
426 0 : move32();
427 0 : FOR( b = 0; b < iNumMSBands; b++ )
428 : {
429 0 : IF( NE_32( piPredCopy[b], 0 ) )
430 : {
431 0 : anyNonZero = 1;
432 0 : move32();
433 0 : break;
434 : }
435 : }
436 :
437 0 : iBitsWritten = L_add( iBitsWritten, 1 ); /* any nonZero Pred */
438 0 : IF( anyNonZero )
439 : {
440 0 : iBitsWritten = L_add( iBitsWritten, PRED_BAND0_BITS );
441 0 : FOR( b = 1; b < iNumMSBands; b++ )
442 : {
443 0 : Word32 tabIdx = L_sub( piPredCopy[b], ENV_DELTA_MIN );
444 0 : iBitsWritten = L_add( iBitsWritten, c_aaiRMSEnvHuffEnc[tabIdx][0] );
445 : }
446 : }
447 :
448 0 : return iBitsWritten;
449 : }
450 :
451 :
452 : #ifdef DEBUG_WRITE_MS_PRED
453 : void writeMSPred(
454 : int32_t *phaseQuant,
455 : int32_t *predQuant,
456 : int32_t MSMode,
457 : int32_t numMSBands,
458 : int32_t numBands,
459 : void *fid,
460 : int32_t *piMsFlags )
461 : {
462 : int32_t b;
463 :
464 : fid = (FILE *) fid;
465 : fprintf( fid, "%d %d", MSMode, numMSBands );
466 : for ( b = 0; b < numMSBands; b++ )
467 : {
468 : fprintf( fid, " %d", phaseQuant[b] );
469 : }
470 : for ( b = numMSBands; b < numBands; b++ )
471 : {
472 : fprintf( fid, " %d", 0 );
473 : }
474 : for ( b = 0; b < numMSBands; b++ )
475 : {
476 : fprintf( fid, " %d", predQuant[b] );
477 : }
478 : for ( b = numMSBands; b < numBands; b++ )
479 : {
480 : fprintf( fid, " %d", 0 );
481 : }
482 : for ( b = 0; b < numBands; b++ )
483 : {
484 : fprintf( fid, " %d", piMsFlags[b] );
485 : }
486 : fprintf( fid, "\n" );
487 :
488 : return;
489 : }
490 : #endif
|