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 0 : static int32_t _round(
47 : float val )
48 : {
49 0 : 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 0 : int32_t quantPhase(
60 : float phase )
61 : {
62 : int32_t phaseQ;
63 :
64 0 : phaseQ = _round( phase * PHASE_QUANT_FACTOR );
65 0 : if ( phaseQ == PHASE_MAX_VAL )
66 : {
67 0 : phaseQ = PHASE_MIN_VAL;
68 : }
69 0 : return phaseQ;
70 : }
71 :
72 :
73 : /*-------------------------------------------------------------------*
74 : * Function cplxmult_lcld()
75 : *
76 : *
77 : *-------------------------------------------------------------------*/
78 :
79 0 : void cplxmult_lcld(
80 : float *pr1,
81 : float *pi1,
82 : const float r2,
83 : const float i2 )
84 : {
85 0 : float r1 = *pr1, i1 = *pi1;
86 :
87 0 : *pr1 = r1 * r2 - i1 * i2;
88 0 : *pi1 = r1 * i2 + i1 * r2;
89 :
90 0 : return;
91 : }
92 :
93 :
94 : /*-------------------------------------------------------------------*
95 : * Function requantPhase()
96 : *
97 : *
98 : *-------------------------------------------------------------------*/
99 :
100 0 : int32_t requantPhase(
101 : int32_t phaseQ )
102 : {
103 :
104 0 : if ( phaseQ >= PHASE_MAX_VAL )
105 : {
106 0 : phaseQ += PHASE_MAX_VAL;
107 0 : phaseQ %= ( 2 * PHASE_MAX_VAL );
108 0 : phaseQ -= PHASE_MAX_VAL;
109 : }
110 0 : else if ( phaseQ < PHASE_MIN_VAL )
111 : {
112 0 : phaseQ -= PHASE_MAX_VAL;
113 0 : phaseQ %= ( 2 * PHASE_MAX_VAL );
114 0 : phaseQ += PHASE_MAX_VAL;
115 0 : if ( phaseQ == PHASE_MAX_VAL )
116 : {
117 0 : phaseQ = PHASE_MIN_VAL;
118 : }
119 : }
120 :
121 0 : return phaseQ;
122 : }
123 :
124 :
125 : /*-------------------------------------------------------------------*
126 : * Function quantPred()
127 : *
128 : *
129 : *-------------------------------------------------------------------*/
130 :
131 0 : int32_t quantPred(
132 : const float pred )
133 : {
134 0 : int32_t predQ = _round( pred * PRED_QUANT_FACTOR );
135 0 : predQ = predQ > PRED_MAX_VAL ? PRED_MAX_VAL : predQ;
136 0 : predQ = predQ < PRED_MIN_VAL ? PRED_MIN_VAL : predQ;
137 :
138 0 : 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 0 : float dequantPred( int32_t predQ )
162 : {
163 0 : return (float) predQ / PRED_QUANT_FACTOR;
164 : }
165 :
166 :
167 : /*-------------------------------------------------------------------*
168 : * Function PrepEncode()
169 : *
170 : *
171 : *-------------------------------------------------------------------*/
172 :
173 0 : int32_t PrepEncode(
174 : int32_t *piQuant,
175 : const int32_t *piMSFlags,
176 : const int32_t numBands )
177 : {
178 0 : int32_t b, numMSBands = 0;
179 :
180 0 : for ( b = 0; b < numBands; b++ )
181 : {
182 0 : if ( piMSFlags[b] )
183 : {
184 0 : piQuant[numMSBands++] = piQuant[b];
185 : }
186 : }
187 :
188 0 : for ( b = numMSBands; b < numBands; b++ )
189 : {
190 0 : piQuant[b] = 0;
191 : }
192 :
193 0 : return numMSBands;
194 : }
195 :
196 :
197 : /*-------------------------------------------------------------------*
198 : * Function EncodePhase()
199 : *
200 : *
201 : *-------------------------------------------------------------------*/
202 :
203 0 : void EncodePhase(
204 : int32_t *phaseQuant,
205 : const int32_t numMSBands,
206 : const int32_t diffDim )
207 : {
208 : int32_t b;
209 :
210 0 : if ( diffDim > 0 )
211 : {
212 : int32_t tmp1, tmp2;
213 0 : tmp1 = phaseQuant[0];
214 0 : for ( b = 1; b < numMSBands; b++ )
215 : {
216 0 : tmp2 = phaseQuant[b];
217 0 : phaseQuant[b] -= tmp1;
218 0 : tmp1 = tmp2;
219 : }
220 : }
221 :
222 0 : if ( diffDim > 1 )
223 : {
224 : int32_t tmp1, tmp2;
225 0 : tmp1 = phaseQuant[1];
226 0 : for ( b = 2; b < numMSBands; b++ )
227 : {
228 0 : tmp2 = phaseQuant[b];
229 0 : phaseQuant[b] -= tmp1;
230 0 : tmp1 = tmp2;
231 : }
232 : }
233 0 : for ( b = 1; b < numMSBands; b++ )
234 : {
235 0 : phaseQuant[b] = requantPhase( phaseQuant[b] );
236 : }
237 :
238 0 : return;
239 : }
240 :
241 :
242 : /*-------------------------------------------------------------------*
243 : * Function DecodePhase()
244 : *
245 : *
246 : *-------------------------------------------------------------------*/
247 :
248 0 : void DecodePhase(
249 : int32_t *phaseQuant,
250 : const int32_t numMSBands,
251 : const int32_t diffDim )
252 : {
253 : int32_t b;
254 :
255 0 : if ( diffDim > 1 )
256 : {
257 0 : for ( b = 2; b < numMSBands; b++ )
258 : {
259 0 : phaseQuant[b] += phaseQuant[b - 1];
260 : }
261 : }
262 :
263 0 : if ( diffDim > 0 )
264 : {
265 0 : for ( b = 1; b < numMSBands; b++ )
266 : {
267 0 : phaseQuant[b] += phaseQuant[b - 1];
268 : }
269 : }
270 :
271 0 : for ( b = 1; b < numMSBands; b++ )
272 : {
273 0 : phaseQuant[b] = requantPhase( phaseQuant[b] );
274 : }
275 :
276 0 : return;
277 : }
278 :
279 :
280 : /*-------------------------------------------------------------------*
281 : * Function EncodePredCoef()
282 : *
283 : *
284 : *-------------------------------------------------------------------*/
285 :
286 0 : int32_t EncodePredCoef(
287 : int32_t *predQuant,
288 : const int32_t numMSBands )
289 : {
290 : int32_t b, tmp1, tmp2;
291 :
292 0 : tmp1 = predQuant[0];
293 0 : for ( b = 1; b < numMSBands; b++ )
294 : {
295 0 : tmp2 = predQuant[b];
296 0 : predQuant[b] -= tmp1;
297 0 : tmp1 = tmp2;
298 : }
299 :
300 0 : return numMSBands;
301 : }
302 :
303 :
304 : /*-------------------------------------------------------------------*
305 : * Function DecodePredCoef()
306 : *
307 : *
308 : *-------------------------------------------------------------------*/
309 :
310 0 : void DecodePredCoef(
311 : int32_t *phaseQuant,
312 : const int32_t numMSBands )
313 : {
314 : int32_t b;
315 :
316 0 : for ( b = 1; b < numMSBands; b++ )
317 : {
318 0 : phaseQuant[b] += phaseQuant[b - 1];
319 : }
320 :
321 0 : return;
322 : }
323 :
324 :
325 : /*-------------------------------------------------------------------*
326 : * Function CountMSBits()
327 : *
328 : *
329 : *-------------------------------------------------------------------*/
330 :
331 0 : 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 0 : int32_t iBitsWritten = 0;
339 : int32_t b;
340 : int32_t anyNonZero;
341 0 : int32_t iNumMSBands = 0;
342 : int32_t piPhaseCopy[MAX_BANDS_48];
343 : int32_t piPredCopy[MAX_BANDS_48];
344 :
345 0 : for ( b = 0; b < MAX_BANDS_48; b++ )
346 : {
347 0 : piPhaseCopy[b] = 0;
348 0 : piPredCopy[b] = 0;
349 : }
350 :
351 0 : iBitsWritten += 2; /* iMSMode */
352 0 : for ( b = 0; b < iNumBands; b++ )
353 : {
354 0 : iNumMSBands += ( piMSFlags[b] != 0 );
355 : }
356 :
357 0 : if ( iNumMSBands == 0 )
358 : {
359 0 : return iBitsWritten;
360 : }
361 :
362 0 : if ( iNumMSBands < iNumBands )
363 : {
364 0 : iBitsWritten += iNumBands; /* piMSFlags */
365 : }
366 :
367 0 : if ( iMSMode < 3 )
368 : {
369 0 : return iBitsWritten;
370 : }
371 :
372 : /* prepare arrays for coding evaluation */
373 0 : for ( b = 0; b < iNumBands; b++ )
374 : {
375 0 : piPhaseCopy[b] = piLRPhaseDiff[b];
376 0 : piPredCopy[b] = piMSPredCoef[b];
377 : }
378 :
379 : /* Differential Coding of Phase Data*/
380 0 : PrepEncode( piPhaseCopy, piMSFlags, iNumBands );
381 0 : PrepEncode( piPredCopy, piMSFlags, iNumBands );
382 0 : EncodePhase( piPhaseCopy, iNumMSBands, PHASE_DIFF_DIM );
383 0 : EncodePredCoef( piPredCopy, iNumMSBands );
384 :
385 0 : iBitsWritten += 1; /* iMSPredAll */
386 0 : anyNonZero = 0; /* phase */
387 0 : for ( b = 0; b < iNumMSBands; b++ )
388 : {
389 0 : if ( piPhaseCopy[b] != 0 )
390 : {
391 0 : anyNonZero = 1;
392 0 : break;
393 : }
394 : }
395 0 : iBitsWritten++; /*anyNonZero Phase*/
396 0 : if ( anyNonZero )
397 : {
398 0 : iBitsWritten += PHASE_BAND0_BITS;
399 0 : for ( b = 1; b < iNumMSBands; b++ )
400 : {
401 0 : int32_t tabIdx = piPhaseCopy[b] - ENV_DELTA_MIN;
402 0 : iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
403 : }
404 : }
405 0 : anyNonZero = 0; /* prediction */
406 0 : for ( b = 0; b < iNumMSBands; b++ )
407 : {
408 0 : if ( piPredCopy[b] != 0 )
409 : {
410 0 : anyNonZero = 1;
411 0 : break;
412 : }
413 : }
414 :
415 0 : iBitsWritten++; /* any nonZero Pred */
416 0 : if ( anyNonZero )
417 : {
418 0 : iBitsWritten += PRED_BAND0_BITS;
419 0 : for ( b = 1; b < iNumMSBands; b++ )
420 : {
421 0 : int32_t tabIdx = piPredCopy[b] - ENV_DELTA_MIN;
422 0 : iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
423 : }
424 : }
425 :
426 0 : 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
|