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 0 : 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 0 : BSTR_ENC_HANDLE hBstr = st->hBstr;
91 :
92 : /*---------------------------------*
93 : * ISF quantization of SID frames
94 : *---------------------------------*/
95 :
96 0 : if ( st->core_brate == SID_1k75 )
97 : {
98 0 : qisf_ns_28b( hBstr, isf_new );
99 :
100 0 : reorder_isf( isf_new, ISF_GAP, M, INT_FS_12k8 );
101 :
102 0 : 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 0 : return;
106 : }
107 :
108 : /* check resonance for pitch clipping algorithm */
109 0 : 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 0 : if ( st->core_brate == ACELP_6k60 )
116 : {
117 0 : qisf_2s_36b( hBstr, isf_new, 4, st->mem_AR, st->mem_MA );
118 : }
119 0 : else if ( st->core_brate >= ACELP_8k85 )
120 : {
121 0 : qisf_2s_46b( hBstr, isf_new, 4, st->mem_AR, st->mem_MA );
122 : }
123 :
124 0 : reorder_isf( isf_new, ISF_GAP, M, INT_FS_12k8 );
125 :
126 : /* convert quantized ISFs back to ISPs */
127 0 : isf2isp( isf_new, isp_new, M, INT_FS_12k8 );
128 :
129 : /*------------------------------------------------------------------*
130 : * ISP interpolation
131 : * A(z) calculation
132 : *------------------------------------------------------------------*/
133 :
134 0 : 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 0 : 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 0 : if ( st->last_core_brate != SID_1k75 )
147 : {
148 0 : st->stab_fac = lsf_stab( isf_new, st->lsf_old, 1, st->L_frame );
149 : }
150 :
151 0 : return;
152 : }
153 :
154 : /*-------------------------------------------------------------------*
155 : * qisf_ns_28b()
156 : *
157 : * ISF quantizer for SID frames (only in AMR-WB IO mode)
158 : *-------------------------------------------------------------------*/
159 :
160 0 : 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 0 : for ( i = 0; i < M; i++ )
169 : {
170 0 : isf[i] -= mean_isf_noise_amr_wb[i];
171 : }
172 :
173 0 : indice[0] = sub_VQ( &isf[0], dico1_ns_28b, 2, DICO1_NS_28b, &tmp );
174 0 : indice[1] = sub_VQ( &isf[2], dico2_ns_28b, 3, DICO2_NS_28b, &tmp );
175 0 : indice[2] = sub_VQ( &isf[5], dico3_ns_28b, 3, DICO3_NS_28b, &tmp );
176 0 : indice[3] = sub_VQ( &isf[8], dico4_ns_28b, 4, DICO4_NS_28b, &tmp );
177 0 : 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 0 : push_indice( hBstr, IND_ISF_0_0, indice[0], 6 );
181 0 : push_indice( hBstr, IND_ISF_0_1, indice[1], 6 );
182 0 : push_indice( hBstr, IND_ISF_0_2, indice[2], 6 );
183 0 : push_indice( hBstr, IND_ISF_0_3, indice[3], 5 );
184 0 : push_indice( hBstr, IND_ISF_0_4, indice[4], 5 );
185 :
186 : /* decoding the ISFs */
187 0 : disf_ns_28b( indice, isf );
188 :
189 0 : 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 0 : 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 0 : for ( i = 0; i < M; i++ )
219 : {
220 0 : isf[i] -= mean_isf_amr_wb[i] + MU_MA * mem_MA[i];
221 : }
222 :
223 : /*------------------------------------------------------------------------*
224 : * Quantize ISFs 0 - 8
225 : *------------------------------------------------------------------------*/
226 :
227 0 : VQ_stage1( &isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv );
228 :
229 0 : distance = 1.0e30f;
230 0 : if ( nb_surv > N_SURV_MAX )
231 : {
232 0 : nb_surv = N_SURV_MAX;
233 : }
234 :
235 0 : for ( k = 0; k < nb_surv; k++ )
236 : {
237 0 : for ( i = 0; i < 9; i++ )
238 : {
239 0 : isf2[i] = isf[i] - dico1_isf[i + surv1[k] * 9];
240 : }
241 :
242 0 : tmp_ind[0] = sub_VQ( &isf2[0], dico21_isf_36b, 5, SIZE_BK21_36b, &min_err );
243 0 : temp = min_err;
244 :
245 0 : tmp_ind[1] = sub_VQ( &isf2[5], dico22_isf_36b, 4, SIZE_BK22_36b, &min_err );
246 0 : temp += min_err;
247 :
248 0 : if ( temp < distance )
249 : {
250 0 : distance = temp;
251 0 : indice[0] = surv1[k];
252 0 : for ( i = 0; i < 2; i++ )
253 : {
254 0 : indice[i + 2] = tmp_ind[i];
255 : }
256 : }
257 : }
258 :
259 : /*------------------------------------------------------------------------*
260 : * Quantize ISFs 9 - 15
261 : *------------------------------------------------------------------------*/
262 :
263 0 : VQ_stage1( &isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv );
264 :
265 0 : distance = 1.0e30f;
266 0 : for ( k = 0; k < nb_surv; k++ )
267 : {
268 0 : for ( i = 0; i < 7; i++ )
269 : {
270 0 : isf2[9 + i] = isf[9 + i] - dico2_isf[i + surv1[k] * 7];
271 : }
272 :
273 0 : tmp_ind[0] = sub_VQ( &isf2[9], dico23_isf_36b, 3, SIZE_BK23_36b, &min_err );
274 0 : temp = min_err;
275 0 : if ( temp < distance )
276 : {
277 0 : distance = temp;
278 0 : indice[1] = surv1[k];
279 0 : indice[4] = tmp_ind[0];
280 : }
281 : }
282 :
283 : /*------------------------------------------------------------------------*
284 : * write indices to array
285 : *------------------------------------------------------------------------*/
286 :
287 0 : push_indice( hBstr, IND_ISF_0_0, indice[0], 8 );
288 0 : push_indice( hBstr, IND_ISF_0_1, indice[1], 8 );
289 0 : push_indice( hBstr, IND_ISF_1_0, indice[2], 7 );
290 0 : push_indice( hBstr, IND_ISF_1_1, indice[3], 7 );
291 0 : push_indice( hBstr, IND_ISF_1_2, indice[4], 6 );
292 :
293 : /*------------------------------------------------------------------------*
294 : * decoding the ISFs
295 : *------------------------------------------------------------------------*/
296 :
297 0 : disf_2s_36b( indice, isf, mem_AR, mem_MA );
298 :
299 0 : 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 0 : 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 0 : for ( i = 0; i < M; i++ )
330 : {
331 0 : isf[i] -= mean_isf_amr_wb[i] + MU_MA * mem_MA[i];
332 : }
333 :
334 : /*------------------------------------------------------------------------*
335 : * Quantize ISFs 0 - 8
336 : *------------------------------------------------------------------------*/
337 :
338 0 : VQ_stage1( &isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv );
339 :
340 0 : distance = 1.0e30f;
341 0 : if ( nb_surv > N_SURV_MAX )
342 : {
343 0 : nb_surv = N_SURV_MAX;
344 : }
345 :
346 0 : for ( k = 0; k < nb_surv; k++ )
347 : {
348 0 : for ( i = 0; i < 9; i++ )
349 : {
350 0 : isf2[i] = isf[i] - dico1_isf[i + surv1[k] * 9];
351 : }
352 :
353 0 : tmp_ind[0] = sub_VQ( &isf2[0], dico21_isf_46b, 3, SIZE_BK21, &min_err );
354 0 : temp = min_err;
355 0 : tmp_ind[1] = sub_VQ( &isf2[3], dico22_isf_46b, 3, SIZE_BK22, &min_err );
356 0 : temp += min_err;
357 0 : tmp_ind[2] = sub_VQ( &isf2[6], dico23_isf_46b, 3, SIZE_BK23, &min_err );
358 0 : temp += min_err;
359 0 : if ( temp < distance )
360 : {
361 0 : distance = temp;
362 0 : indice[0] = surv1[k];
363 0 : for ( i = 0; i < 3; i++ )
364 : {
365 0 : indice[i + 2] = tmp_ind[i];
366 : }
367 : }
368 : }
369 :
370 : /*------------------------------------------------------------------------*
371 : * Quantize ISFs 9 - 15
372 : *------------------------------------------------------------------------*/
373 :
374 0 : VQ_stage1( &isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv );
375 :
376 0 : distance = 1.0e30f;
377 0 : for ( k = 0; k < nb_surv; k++ )
378 : {
379 0 : for ( i = 0; i < 7; i++ )
380 : {
381 0 : isf2[9 + i] = isf[9 + i] - dico2_isf[i + surv1[k] * 7];
382 : }
383 0 : tmp_ind[0] = sub_VQ( &isf2[9], dico24_isf_46b, 3, SIZE_BK24, &min_err );
384 0 : temp = min_err;
385 :
386 0 : tmp_ind[1] = sub_VQ( &isf2[12], dico25_isf_46b, 4, SIZE_BK25, &min_err );
387 0 : temp += min_err;
388 :
389 0 : if ( temp < distance )
390 : {
391 :
392 0 : distance = temp;
393 0 : indice[1] = surv1[k];
394 0 : for ( i = 0; i < 2; i++ )
395 : {
396 0 : indice[i + 5] = tmp_ind[i];
397 : }
398 : }
399 : }
400 :
401 : /*------------------------------------------------------------------------*
402 : * write indices to array
403 : *------------------------------------------------------------------------*/
404 :
405 0 : push_indice( hBstr, IND_ISF_0_0, indice[0], 8 );
406 0 : push_indice( hBstr, IND_ISF_0_1, indice[1], 8 );
407 0 : push_indice( hBstr, IND_ISF_1_0, indice[2], 6 );
408 0 : push_indice( hBstr, IND_ISF_1_1, indice[3], 7 );
409 0 : push_indice( hBstr, IND_ISF_1_2, indice[4], 7 );
410 0 : push_indice( hBstr, IND_ISF_1_3, indice[5], 5 );
411 0 : push_indice( hBstr, IND_ISF_1_4, indice[6], 5 );
412 :
413 : /*------------------------------------------------------------------------*
414 : * decoding the ISFs
415 : *------------------------------------------------------------------------*/
416 :
417 0 : disf_2s_46b( indice, isf, mem_AR, mem_MA );
418 :
419 0 : return;
420 : }
421 :
422 : /*-------------------------------------------------------------------*
423 : * VQ_stage1()
424 : *
425 : * 1st stage of ISF quantization
426 : *-------------------------------------------------------------------*/
427 :
428 0 : 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 0 : for ( i = 0; i < surv; i++ )
444 : {
445 0 : dist_min[i] = 1.0e30f;
446 0 : index[i] = i;
447 : }
448 :
449 0 : p_dico = dico;
450 :
451 0 : for ( i = 0; i < dico_size; i++ )
452 : {
453 0 : dist = 0.0;
454 0 : for ( j = 0; j < dim; j++ )
455 : {
456 0 : temp = x[j] - *p_dico++;
457 0 : dist += temp * temp;
458 : }
459 :
460 0 : for ( k = 0; k < surv; k++ )
461 : {
462 0 : if ( dist < dist_min[k] )
463 : {
464 0 : for ( l = surv - 1; l > k; l-- )
465 : {
466 0 : dist_min[l] = dist_min[l - 1];
467 0 : index[l] = index[l - 1];
468 : }
469 0 : dist_min[k] = dist;
470 0 : index[k] = i;
471 0 : break;
472 : }
473 : }
474 : }
475 0 : 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 0 : 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 0 : dist_min = 1.0e30f;
499 0 : p_dico = dico;
500 :
501 0 : index = 0;
502 0 : for ( i = 0; i < dico_size; i++ )
503 : {
504 0 : dist = 0.0f;
505 0 : for ( j = 0; j < dim; j++ )
506 : {
507 0 : temp = x[j] - *p_dico++;
508 0 : dist += temp * temp;
509 : }
510 0 : if ( dist < dist_min )
511 : {
512 0 : dist_min = dist;
513 0 : index = i;
514 : }
515 : }
516 :
517 0 : *distance = dist_min;
518 :
519 : /* Reading the selected vector */
520 0 : p_dico = &dico[index * dim];
521 0 : for ( j = 0; j < dim; j++ )
522 : {
523 0 : x[j] = *p_dico++;
524 : }
525 0 : return index;
526 : }
|