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 "prot.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * core_encode_twodiv()
49 : *
50 : * Two-div core encoder
51 : *-------------------------------------------------------------------*/
52 :
53 11494 : void core_encode_twodiv(
54 : Encoder_State *st, /* i/o: coder memory state */
55 : const float new_samples[], /* i : new samples */
56 : float Aw[NB_SUBFR16k * ( M + 1 )], /* i : weighted A(z) unquant. for subframes*/
57 : const int16_t vad_hover_flag /* i : VAD hangover flag */
58 : )
59 : {
60 11494 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
61 : int16_t n;
62 : float lsp_new[M], lsp_mid[M];
63 : float lsf_q[M], lsp_q[M];
64 : float lspmid_q[M];
65 : float A_q[M + 1];
66 : int16_t param_lpc[NPRM_LPC_NEW];
67 : int16_t nbits_lpc[2];
68 : int16_t param_core[2 * NPRM_DIV];
69 : int16_t target_bits;
70 : float gainlpc[2][FDNS_NPTS];
71 : int16_t tnsSize[2]; /* number of tns parameters put into prm */
72 : int16_t tnsBits[2]; /* number of tns bits in the frame */
73 : int16_t ltpBits;
74 : int16_t bitsAvailable;
75 : int16_t indexBuffer[2 * ( ( N_MAX / 2 ) + 1 )];
76 : CONTEXT_HM_CONFIG hm_cfg[2];
77 : int16_t bits_param_lpc[10], no_param_lpc;
78 : int16_t i, T_op[3];
79 :
80 11494 : hm_cfg[0].indexBuffer = &indexBuffer[0];
81 11494 : hm_cfg[1].indexBuffer = &indexBuffer[N_MAX / 2 + 1];
82 :
83 11494 : set_s( tnsSize, 0, 2 );
84 11494 : set_s( tnsBits, 0, 2 );
85 11494 : ltpBits = 0;
86 :
87 45976 : for ( i = 0; i < 3; i++ )
88 : {
89 34482 : T_op[i] = st->pitch[i];
90 :
91 : /* check minimum pitch for quantization */
92 34482 : if ( T_op[i] < PIT_MIN_SHORTER )
93 : {
94 2644 : T_op[i] *= 2;
95 : }
96 :
97 : /* convert pitch values to core sampling-rate */
98 34482 : if ( st->L_frame != L_FRAME )
99 : {
100 34482 : T_op[i] = (int16_t) ( T_op[i] * (float) st->L_frame / (float) L_FRAME + 0.5f );
101 : }
102 : }
103 :
104 : /*--------------------------------------------------------------*
105 : * TCX20/TCX10 switching decision
106 : *---------------------------------------------------------------*/
107 :
108 11494 : if ( st->hTcxEnc->tcxMode == TCX_10 )
109 : {
110 454 : st->core = TCX_10_CORE;
111 : }
112 11040 : else if ( st->hTcxEnc->tcxMode == TCX_20 )
113 : {
114 11040 : st->core = TCX_20_CORE;
115 : }
116 :
117 : /*--------------------------------------------------------------*
118 : * Core Signal Analysis: MDCT, TNS, LPC analysis
119 : *---------------------------------------------------------------*/
120 :
121 11494 : core_signal_analysis_high_bitrate( new_samples, T_op, lsp_new, lsp_mid, st, NULL, tnsSize, tnsBits, param_core, <pBits, NULL, st->L_frame, hTcxEnc->L_frameTCX, EVS_MONO, vad_hover_flag );
122 :
123 : /*--------------------------------------------------------------*
124 : * LPC Quantization
125 : *---------------------------------------------------------------*/
126 :
127 11494 : lpc_quantization( st, lsp_new, lsp_mid, lsp_q, lsf_q, lspmid_q, st->coder_type, 0, /*No acelp->no need to compute any mid-LPC*/
128 : param_lpc, nbits_lpc, bits_param_lpc, &no_param_lpc );
129 :
130 : /*--------------------------------------------------------------*
131 : * Rate switching
132 : *--------------------------------------------------------------*/
133 :
134 11494 : if ( st->rate_switching_reset )
135 : {
136 89 : mvr2r( lsp_q, st->lsp_old, M );
137 89 : mvr2r( lsf_q, st->lsf_old, M );
138 : }
139 :
140 : /*--------------------------------------------------------------*
141 : * Run Two TCX10
142 : *---------------------------------------------------------------*/
143 :
144 11494 : if ( st->core == TCX_10_CORE )
145 : {
146 454 : const int16_t last_ace_mode = st->last_core;
147 :
148 1362 : for ( n = 0; n < 2; n++ )
149 : {
150 908 : if ( n == 0 )
151 : {
152 454 : lsp2a_stab( lspmid_q, A_q, M );
153 : }
154 : else
155 : {
156 454 : lsp2a_stab( lsp_q, A_q, M );
157 : }
158 :
159 : /* Shape spectrum */
160 908 : ShapeSpectrum( st->hTcxCfg, A_q, gainlpc[n], st->L_frame / 2, st->hTcxCfg->tcx_coded_lines / 2, hTcxEnc->spectrum[n], hTcxEnc->fUseTns[n], st, NULL );
161 :
162 908 : st->last_core = st->core;
163 : }
164 454 : st->last_core = last_ace_mode;
165 :
166 : /* Calculate target bits */
167 454 : bitsAvailable = st->bits_frame_core - nbits_lpc[0] - nbits_lpc[1] - st->nb_bits_header_tcx;
168 :
169 : /* subtract bits for TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
170 454 : bitsAvailable -= ( st->hTcxCfg->tcx_curr_overlap_mode == HALF_OVERLAP || st->hTcxCfg->tcx_curr_overlap_mode == MIN_OVERLAP ) ? 2 : 1;
171 : {
172 454 : bitsAvailable -= st->hIGFEnc->infoTotalBitsWritten;
173 : }
174 :
175 454 : hTcxEnc->measuredBwRatio = 1.f;
176 :
177 1362 : for ( n = 0; n < 2; n++ )
178 : {
179 908 : target_bits = ( ( bitsAvailable + 1 - n ) >> 1 ) - tnsBits[n];
180 :
181 908 : if ( n == 0 )
182 : {
183 454 : target_bits -= ltpBits;
184 : }
185 :
186 908 : if ( st->enablePlcWaveadjust && n )
187 : {
188 454 : target_bits -= 1;
189 : }
190 :
191 : /* Run TCX10 encoder */
192 908 : QuantizeSpectrum( st, A_q, NULL, gainlpc[n], st->synth + n * st->L_frame / 2, target_bits, tnsSize[n], param_core + n * NPRM_DIV, n, &hm_cfg[n], vad_hover_flag );
193 :
194 : /* Update tcx overlap mode */
195 908 : if ( ( n > 0 ) || !st->tcxonly )
196 : {
197 454 : st->hTcxCfg->tcx_last_overlap_mode = st->hTcxCfg->tcx_curr_overlap_mode;
198 : }
199 : }
200 :
201 454 : coder_tcx_post( st, A_q, Aw );
202 : }
203 :
204 : /*--------------------------------------------------------------*
205 : * Run One TCX20
206 : *---------------------------------------------------------------*/
207 :
208 11494 : if ( st->core == TCX_20_CORE )
209 : {
210 11040 : lsp2a_stab( lsp_q, A_q, M );
211 :
212 11040 : ShapeSpectrum( st->hTcxCfg, A_q, gainlpc[0], st->L_frame, st->hTcxCfg->tcx_coded_lines, hTcxEnc->spectrum_long, hTcxEnc->fUseTns[0], st, NULL );
213 :
214 11040 : hTcxEnc->measuredBwRatio = 1.f;
215 :
216 : /* Calculate target bits */
217 11040 : target_bits = st->bits_frame_core - tnsBits[0] - nbits_lpc[0] - st->nb_bits_header_tcx - ltpBits;
218 :
219 : /* subtract bits for TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
220 11040 : target_bits -= ( st->hTcxCfg->tcx_curr_overlap_mode == HALF_OVERLAP || st->hTcxCfg->tcx_curr_overlap_mode == MIN_OVERLAP ) ? 2 : 1;
221 : {
222 11040 : target_bits -= st->hIGFEnc->infoTotalBitsPerFrameWritten;
223 : }
224 11040 : if ( st->enablePlcWaveadjust )
225 : {
226 11040 : target_bits -= 1;
227 : }
228 :
229 11040 : QuantizeSpectrum( st, A_q, NULL, gainlpc[0], st->synth, target_bits, tnsSize[0], param_core, 0, &hm_cfg[0], vad_hover_flag );
230 :
231 11040 : coder_tcx_post( st, A_q, Aw );
232 : }
233 :
234 : /* Update lsp/lsf memory */
235 11494 : mvr2r( lsf_q, st->lsf_old, M );
236 11494 : mvr2r( lsp_q, st->lsp_old, M );
237 :
238 :
239 : /*--------------------------------------------------------------*
240 : * Generate Bitstream
241 : *---------------------------------------------------------------*/
242 :
243 11494 : enc_prm( st, param_core, param_lpc, hm_cfg, bits_param_lpc, no_param_lpc );
244 :
245 11494 : return;
246 : }
|