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