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 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "rom_enc.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-----------------------------------------------------------------*
49 : * Local constants
50 : *-----------------------------------------------------------------*/
51 :
52 : #define DICO1_NS_19b 16 /* codebook dimensions for SID ISF quantizers */
53 : #define DICO2_NS_19b 16
54 : #define DICO3_NS_19b 16
55 : #define DICO4_NS_19b 8
56 : #define DICO5_NS_19b 16
57 :
58 : #define DICO1_NS_28b 64
59 : #define DICO2_NS_28b 64
60 : #define DICO3_NS_28b 64
61 : #define DICO4_NS_28b 32
62 : #define DICO5_NS_28b 32
63 :
64 : #define N_SURV_MAX 4 /* maximum number of survivors */
65 :
66 : /*---------------------------------------------------------------------*
67 : * Local function prototypes
68 : *---------------------------------------------------------------------*/
69 :
70 : static void qisf_ns_28b( BSTR_ENC_HANDLE hBstr, float *isf );
71 : static void qisf_2s_46b( BSTR_ENC_HANDLE hBstr, float *isf, int16_t nb_surv, float *mem_AR, float *mem_MA );
72 : static void qisf_2s_36b( BSTR_ENC_HANDLE hBstr, float *isf, int16_t nb_surv, float *mem_AR, float *mem_MA );
73 : static void VQ_stage1( const float *x, const float *dico, const int16_t dim, const int16_t dico_size, int16_t *index, const int16_t surv );
74 : static int16_t sub_VQ( float *x, const float *dico, const int16_t dim, const int16_t dico_size, float *distance );
75 :
76 :
77 : /*-------------------------------------------------------------------*
78 : * isf_enc_amr_wb()
79 : *
80 : * Quantization of ISF parameters in AMR-WB IO mode
81 : *-------------------------------------------------------------------*/
82 :
83 4509 : void isf_enc_amr_wb(
84 : Encoder_State *st, /* i/o: state structure */
85 : float *isf_new, /* i/o: quantized ISF vector */
86 : float *isp_new, /* i/o: ISP vector to quantize/quantized */
87 : float *Aq /* o : quantized A(z) for 4 subframes */
88 : )
89 : {
90 4509 : BSTR_ENC_HANDLE hBstr = st->hBstr;
91 :
92 : /*---------------------------------*
93 : * ISF quantization of SID frames
94 : *---------------------------------*/
95 :
96 4509 : if ( st->core_brate == SID_1k75 )
97 : {
98 107 : qisf_ns_28b( hBstr, isf_new );
99 :
100 107 : reorder_isf( isf_new, ISF_GAP, M, INT_FS_12k8 );
101 :
102 107 : isf2isp( isf_new, isp_new, M, INT_FS_12k8 );
103 :
104 : /* return if SID frame (conversion to A(z) done in the calling function) */
105 107 : return;
106 : }
107 :
108 : /* check resonance for pitch clipping algorithm */
109 4402 : gp_clip_test_lsf( st->element_mode, st->core_brate, isf_new, st->clip_var, 1 );
110 :
111 : /*---------------------------------------*
112 : * ISF quantization of all other frames
113 : *---------------------------------------*/
114 :
115 4402 : if ( st->core_brate == ACELP_6k60 )
116 : {
117 370 : qisf_2s_36b( hBstr, isf_new, 4, st->mem_AR, st->mem_MA );
118 : }
119 4032 : else if ( st->core_brate >= ACELP_8k85 )
120 : {
121 4032 : qisf_2s_46b( hBstr, isf_new, 4, st->mem_AR, st->mem_MA );
122 : }
123 :
124 4402 : reorder_isf( isf_new, ISF_GAP, M, INT_FS_12k8 );
125 :
126 : /* convert quantized ISFs back to ISPs */
127 4402 : isf2isp( isf_new, isp_new, M, INT_FS_12k8 );
128 :
129 : /*------------------------------------------------------------------*
130 : * ISP interpolation
131 : * A(z) calculation
132 : *------------------------------------------------------------------*/
133 :
134 4402 : if ( st->rate_switching_reset )
135 : {
136 0 : mvr2r( isf_new, st->lsf_old, M );
137 0 : mvr2r( isp_new, st->lsp_old, M );
138 : }
139 :
140 4402 : int_lsp( L_FRAME, st->lsp_old, isp_new, Aq, M, interpol_isp_amr_wb, 1 );
141 :
142 : /*------------------------------------------------------------------*
143 : * Calculate ISF stability (distance between old ISF and current ISF)
144 : *------------------------------------------------------------------*/
145 :
146 4402 : if ( st->last_core_brate != SID_1k75 )
147 : {
148 4401 : st->stab_fac = lsf_stab( isf_new, st->lsf_old, 1, st->L_frame );
149 : }
150 :
151 4402 : return;
152 : }
153 :
154 : /*-------------------------------------------------------------------*
155 : * qisf_ns_28b()
156 : *
157 : * ISF quantizer for SID frames (only in AMR-WB IO mode)
158 : *-------------------------------------------------------------------*/
159 :
160 107 : static void qisf_ns_28b(
161 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
162 : float *isf /* i/o: unquantized/quantized ISF vector */
163 : )
164 : {
165 : int16_t i, indice[5];
166 : float tmp;
167 :
168 1819 : for ( i = 0; i < M; i++ )
169 : {
170 1712 : isf[i] -= mean_isf_noise_amr_wb[i];
171 : }
172 :
173 107 : indice[0] = sub_VQ( &isf[0], dico1_ns_28b, 2, DICO1_NS_28b, &tmp );
174 107 : indice[1] = sub_VQ( &isf[2], dico2_ns_28b, 3, DICO2_NS_28b, &tmp );
175 107 : indice[2] = sub_VQ( &isf[5], dico3_ns_28b, 3, DICO3_NS_28b, &tmp );
176 107 : indice[3] = sub_VQ( &isf[8], dico4_ns_28b, 4, DICO4_NS_28b, &tmp );
177 107 : indice[4] = sub_VQ( &isf[12], dico5_ns_28b + 4, 4, DICO5_NS_28b - 1, &tmp ) + 1; /* First vector has a problem -> do not allow */
178 :
179 : /* write indices to array */
180 107 : push_indice( hBstr, IND_ISF_0_0, indice[0], 6 );
181 107 : push_indice( hBstr, IND_ISF_0_1, indice[1], 6 );
182 107 : push_indice( hBstr, IND_ISF_0_2, indice[2], 6 );
183 107 : push_indice( hBstr, IND_ISF_0_3, indice[3], 5 );
184 107 : push_indice( hBstr, IND_ISF_0_4, indice[4], 5 );
185 :
186 : /* decoding the ISFs */
187 107 : disf_ns_28b( indice, isf );
188 :
189 107 : return;
190 : }
191 :
192 :
193 : /*---------------------------------------------------------------------*
194 : * qisf_2s_36b()
195 : *
196 : * ISF quantizer for AMR-WB 6k60 frames
197 : *
198 : * The ISF vector is quantized using two-stage MA-prediction VQ with split-by-2
199 : * in 1st stage and split-by-3 in the second stage.
200 : *---------------------------------------------------------------------*/
201 :
202 370 : static void qisf_2s_36b(
203 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
204 : float *isf, /* i/o: unquantized/quantized ISF vector */
205 : int16_t nb_surv, /* i : number of survivors (1, 2, 3 or 4) */
206 : float *mem_AR, /* o : quantizer memory for AR model */
207 : float *mem_MA /* i/o: quantizer memory for MA model */
208 : )
209 : {
210 : int16_t i, k, indice[5], tmp_ind[2];
211 : int16_t surv1[N_SURV_MAX]; /* indices of survivors from 1st stage */
212 : float temp, min_err, distance, isf2[M];
213 :
214 : /*------------------------------------------------------------------------*
215 : * Subtract mean
216 : *------------------------------------------------------------------------*/
217 :
218 6290 : for ( i = 0; i < M; i++ )
219 : {
220 5920 : isf[i] -= mean_isf_amr_wb[i] + MU_MA * mem_MA[i];
221 : }
222 :
223 : /*------------------------------------------------------------------------*
224 : * Quantize ISFs 0 - 8
225 : *------------------------------------------------------------------------*/
226 :
227 370 : VQ_stage1( &isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv );
228 :
229 370 : distance = 1.0e30f;
230 370 : if ( nb_surv > N_SURV_MAX )
231 : {
232 0 : nb_surv = N_SURV_MAX;
233 : }
234 :
235 1850 : for ( k = 0; k < nb_surv; k++ )
236 : {
237 14800 : for ( i = 0; i < 9; i++ )
238 : {
239 13320 : isf2[i] = isf[i] - dico1_isf[i + surv1[k] * 9];
240 : }
241 :
242 1480 : tmp_ind[0] = sub_VQ( &isf2[0], dico21_isf_36b, 5, SIZE_BK21_36b, &min_err );
243 1480 : temp = min_err;
244 :
245 1480 : tmp_ind[1] = sub_VQ( &isf2[5], dico22_isf_36b, 4, SIZE_BK22_36b, &min_err );
246 1480 : temp += min_err;
247 :
248 1480 : if ( temp < distance )
249 : {
250 543 : distance = temp;
251 543 : indice[0] = surv1[k];
252 1629 : for ( i = 0; i < 2; i++ )
253 : {
254 1086 : indice[i + 2] = tmp_ind[i];
255 : }
256 : }
257 : }
258 :
259 : /*------------------------------------------------------------------------*
260 : * Quantize ISFs 9 - 15
261 : *------------------------------------------------------------------------*/
262 :
263 370 : VQ_stage1( &isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv );
264 :
265 370 : distance = 1.0e30f;
266 1850 : for ( k = 0; k < nb_surv; k++ )
267 : {
268 11840 : for ( i = 0; i < 7; i++ )
269 : {
270 10360 : isf2[9 + i] = isf[9 + i] - dico2_isf[i + surv1[k] * 7];
271 : }
272 :
273 1480 : tmp_ind[0] = sub_VQ( &isf2[9], dico23_isf_36b, 3, SIZE_BK23_36b, &min_err );
274 1480 : temp = min_err;
275 1480 : if ( temp < distance )
276 : {
277 658 : distance = temp;
278 658 : indice[1] = surv1[k];
279 658 : indice[4] = tmp_ind[0];
280 : }
281 : }
282 :
283 : /*------------------------------------------------------------------------*
284 : * write indices to array
285 : *------------------------------------------------------------------------*/
286 :
287 370 : push_indice( hBstr, IND_ISF_0_0, indice[0], 8 );
288 370 : push_indice( hBstr, IND_ISF_0_1, indice[1], 8 );
289 370 : push_indice( hBstr, IND_ISF_1_0, indice[2], 7 );
290 370 : push_indice( hBstr, IND_ISF_1_1, indice[3], 7 );
291 370 : push_indice( hBstr, IND_ISF_1_2, indice[4], 6 );
292 :
293 : /*------------------------------------------------------------------------*
294 : * decoding the ISFs
295 : *------------------------------------------------------------------------*/
296 :
297 370 : disf_2s_36b( indice, isf, mem_AR, mem_MA );
298 :
299 370 : return;
300 : }
301 :
302 :
303 : /*-------------------------------------------------------------------*
304 : * qisf_2s_46b()
305 : *
306 : * ISF quantizer for all other AMR-WB frames
307 : *
308 : * The ISF vector is quantized using two-stage VQ with split-by-2
309 : * in 1st stage and split-by-5 in the second stage.
310 : *-------------------------------------------------------------------*/
311 :
312 4032 : static void qisf_2s_46b(
313 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
314 : float *isf, /* i/o: unquantized/quantized ISF vector */
315 : int16_t nb_surv, /* i : number of survivors (1, 2, 3 or 4) */
316 : float *mem_AR, /* o : quantizer memory for AR model */
317 : float *mem_MA /* i/o: quantizer memory for MA model */
318 : )
319 : {
320 : int16_t i, k, indice[7], tmp_ind[5];
321 : int16_t surv1[N_SURV_MAX]; /* indices of survivors from 1st stage */
322 : float temp, min_err, distance, isf2[M];
323 :
324 :
325 : /*------------------------------------------------------------------------*
326 : * Subtract mean
327 : *------------------------------------------------------------------------*/
328 :
329 68544 : for ( i = 0; i < M; i++ )
330 : {
331 64512 : isf[i] -= mean_isf_amr_wb[i] + MU_MA * mem_MA[i];
332 : }
333 :
334 : /*------------------------------------------------------------------------*
335 : * Quantize ISFs 0 - 8
336 : *------------------------------------------------------------------------*/
337 :
338 4032 : VQ_stage1( &isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv );
339 :
340 4032 : distance = 1.0e30f;
341 4032 : if ( nb_surv > N_SURV_MAX )
342 : {
343 0 : nb_surv = N_SURV_MAX;
344 : }
345 :
346 20160 : for ( k = 0; k < nb_surv; k++ )
347 : {
348 161280 : for ( i = 0; i < 9; i++ )
349 : {
350 145152 : isf2[i] = isf[i] - dico1_isf[i + surv1[k] * 9];
351 : }
352 :
353 16128 : tmp_ind[0] = sub_VQ( &isf2[0], dico21_isf_46b, 3, SIZE_BK21, &min_err );
354 16128 : temp = min_err;
355 16128 : tmp_ind[1] = sub_VQ( &isf2[3], dico22_isf_46b, 3, SIZE_BK22, &min_err );
356 16128 : temp += min_err;
357 16128 : tmp_ind[2] = sub_VQ( &isf2[6], dico23_isf_46b, 3, SIZE_BK23, &min_err );
358 16128 : temp += min_err;
359 16128 : if ( temp < distance )
360 : {
361 6457 : distance = temp;
362 6457 : indice[0] = surv1[k];
363 25828 : for ( i = 0; i < 3; i++ )
364 : {
365 19371 : indice[i + 2] = tmp_ind[i];
366 : }
367 : }
368 : }
369 :
370 : /*------------------------------------------------------------------------*
371 : * Quantize ISFs 9 - 15
372 : *------------------------------------------------------------------------*/
373 :
374 4032 : VQ_stage1( &isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv );
375 :
376 4032 : distance = 1.0e30f;
377 20160 : for ( k = 0; k < nb_surv; k++ )
378 : {
379 129024 : for ( i = 0; i < 7; i++ )
380 : {
381 112896 : isf2[9 + i] = isf[9 + i] - dico2_isf[i + surv1[k] * 7];
382 : }
383 16128 : tmp_ind[0] = sub_VQ( &isf2[9], dico24_isf_46b, 3, SIZE_BK24, &min_err );
384 16128 : temp = min_err;
385 :
386 16128 : tmp_ind[1] = sub_VQ( &isf2[12], dico25_isf_46b, 4, SIZE_BK25, &min_err );
387 16128 : temp += min_err;
388 :
389 16128 : if ( temp < distance )
390 : {
391 :
392 6404 : distance = temp;
393 6404 : indice[1] = surv1[k];
394 19212 : for ( i = 0; i < 2; i++ )
395 : {
396 12808 : indice[i + 5] = tmp_ind[i];
397 : }
398 : }
399 : }
400 :
401 : /*------------------------------------------------------------------------*
402 : * write indices to array
403 : *------------------------------------------------------------------------*/
404 :
405 4032 : push_indice( hBstr, IND_ISF_0_0, indice[0], 8 );
406 4032 : push_indice( hBstr, IND_ISF_0_1, indice[1], 8 );
407 4032 : push_indice( hBstr, IND_ISF_1_0, indice[2], 6 );
408 4032 : push_indice( hBstr, IND_ISF_1_1, indice[3], 7 );
409 4032 : push_indice( hBstr, IND_ISF_1_2, indice[4], 7 );
410 4032 : push_indice( hBstr, IND_ISF_1_3, indice[5], 5 );
411 4032 : push_indice( hBstr, IND_ISF_1_4, indice[6], 5 );
412 :
413 : /*------------------------------------------------------------------------*
414 : * decoding the ISFs
415 : *------------------------------------------------------------------------*/
416 :
417 4032 : disf_2s_46b( indice, isf, mem_AR, mem_MA );
418 :
419 4032 : return;
420 : }
421 :
422 : /*-------------------------------------------------------------------*
423 : * VQ_stage1()
424 : *
425 : * 1st stage of ISF quantization
426 : *-------------------------------------------------------------------*/
427 :
428 8804 : static void VQ_stage1(
429 : const float *x, /* i : ISF vector */
430 : const float *dico, /* i : ISF codebook */
431 : const int16_t dim, /* i : codebook dimension */
432 : const int16_t dico_size, /* i : codebook size */
433 : int16_t *index, /* o : indices of best vector candidates */
434 : const int16_t surv /* i : nb of surviving best candidates */
435 : )
436 : {
437 : float dist_min[N_SURV_MAX];
438 : float dist, temp;
439 : const float *p_dico;
440 : int16_t i, j, k, l;
441 :
442 :
443 44020 : for ( i = 0; i < surv; i++ )
444 : {
445 35216 : dist_min[i] = 1.0e30f;
446 35216 : index[i] = i;
447 : }
448 :
449 8804 : p_dico = dico;
450 :
451 2262628 : for ( i = 0; i < dico_size; i++ )
452 : {
453 2253824 : dist = 0.0;
454 20284416 : for ( j = 0; j < dim; j++ )
455 : {
456 18030592 : temp = x[j] - *p_dico++;
457 18030592 : dist += temp * temp;
458 : }
459 :
460 10768745 : for ( k = 0; k < surv; k++ )
461 : {
462 8694528 : if ( dist < dist_min[k] )
463 : {
464 500375 : for ( l = surv - 1; l > k; l-- )
465 : {
466 320768 : dist_min[l] = dist_min[l - 1];
467 320768 : index[l] = index[l - 1];
468 : }
469 179607 : dist_min[k] = dist;
470 179607 : index[k] = i;
471 179607 : break;
472 : }
473 : }
474 : }
475 8804 : return;
476 : }
477 :
478 : /*-------------------------------------------------------------------*
479 : * sub_VQ()
480 : *
481 : * Quantization of a subvector in Split-VQ of ISFs
482 : *-------------------------------------------------------------------*/
483 :
484 : /*! r: selected codebook vector index */
485 85615 : static int16_t sub_VQ(
486 : float *x, /* i/o: ISF vector */
487 : const float *dico, /* i : ISF codebook */
488 : const int16_t dim, /* i : codebook dimension */
489 : const int16_t dico_size, /* i : codebook size */
490 : float *distance /* o : quantization error (min. distance) */
491 : )
492 : {
493 : float dist_min, dist, temp;
494 : const float *p_dico;
495 : int16_t i, j, index;
496 :
497 :
498 85615 : dist_min = 1.0e30f;
499 85615 : p_dico = dico;
500 :
501 85615 : index = 0;
502 6779652 : for ( i = 0; i < dico_size; i++ )
503 : {
504 6694037 : dist = 0.0f;
505 27860457 : for ( j = 0; j < dim; j++ )
506 : {
507 21166420 : temp = x[j] - *p_dico++;
508 21166420 : dist += temp * temp;
509 : }
510 6694037 : if ( dist < dist_min )
511 : {
512 464648 : dist_min = dist;
513 464648 : index = i;
514 : }
515 : }
516 :
517 85615 : *distance = dist_min;
518 :
519 : /* Reading the selected vector */
520 85615 : p_dico = &dico[index * dim];
521 363135 : for ( j = 0; j < dim; j++ )
522 : {
523 277520 : x[j] = *p_dico++;
524 : }
525 85615 : return index;
526 : }
|