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 <stdint.h>
34 : #include "options.h"
35 : #include "prot.h"
36 : #include "ivas_prot.h"
37 : #ifdef DEBUGGING
38 : #include "debug.h"
39 : #endif
40 : #include <assert.h>
41 : #include "ivas_cnst.h"
42 : #include "wmc_auto.h"
43 :
44 :
45 : /*-----------------------------------------------------------------------*
46 : * Local function definitions
47 : *-----------------------------------------------------------------------*/
48 :
49 36 : static int32_t ivas_bitstream_read_int32(
50 : Decoder_State *st0,
51 : const int16_t bits )
52 : {
53 : int32_t val;
54 :
55 : /* MSB */
56 36 : val = get_next_indice( st0, bits - 16 ) << 16;
57 :
58 : /* + LSB */
59 36 : val += get_next_indice( st0, 16 );
60 :
61 36 : return val;
62 : }
63 :
64 :
65 32982 : static void pca_dec_reset_dquat(
66 : float *ql,
67 : float *qr )
68 : {
69 32982 : set_zero( ql, IVAS_PCA_INTERP );
70 32982 : ql[0] = 1.0f;
71 :
72 32982 : set_zero( qr, IVAS_PCA_INTERP );
73 32982 : qr[0] = 1.0f;
74 :
75 32982 : return;
76 : }
77 :
78 :
79 31329 : static void pca_dec_reset_mem_eigvec(
80 : PCA_DEC_STATE *hPCA )
81 : {
82 : int16_t i;
83 :
84 2036385 : for ( i = 0; i < ( IVAS_PCA_LEN_INTERP_EIG_DEC >> 4 ); i++ )
85 : {
86 2005056 : eye_matrix( &hPCA->mem_eigVec_interp[16 * i], FOA_CHANNELS, 1.0f );
87 : }
88 :
89 31329 : return;
90 : }
91 :
92 :
93 145560 : static void pca_inv_transform_sub(
94 : float *eigVec,
95 : float *transformed_data[], /* i : input/transformed audio channels */
96 : const int16_t start,
97 : const int16_t len,
98 : const int16_t n_channels )
99 : {
100 : int16_t i, j, k;
101 : float temp;
102 : float buffer_data[FOA_CHANNELS];
103 :
104 2610840 : for ( j = 0; j < len; j++ )
105 : {
106 12326400 : for ( k = 0; k < n_channels; k++ )
107 : {
108 9861120 : buffer_data[k] = transformed_data[k][j + start];
109 : }
110 :
111 12326400 : for ( k = 0; k < n_channels; k++ )
112 : {
113 9861120 : temp = 0.0f;
114 49305600 : for ( i = 0; i < n_channels; i++ )
115 : {
116 39444480 : temp += eigVec[k * IVAS_PCA_INTERP + i] * buffer_data[i];
117 : }
118 9861120 : transformed_data[k][j + start] = temp;
119 : }
120 : }
121 :
122 145560 : return;
123 : }
124 :
125 :
126 3639 : static void pca_dec_inv_transform(
127 : PCA_DEC_STATE *hPCA,
128 : float *ql,
129 : float *qr,
130 : const int16_t n_samples,
131 : const int16_t n_channels,
132 : float *decoded_data[] )
133 : {
134 : int16_t j;
135 : int16_t slot_len;
136 : float ql_interp[IVAS_PCA_LEN_INTERP_Q], qr_interp[IVAS_PCA_LEN_INTERP_Q];
137 :
138 3639 : quat_shortestpath( hPCA->prev_ql, ql, hPCA->prev_qr, qr );
139 :
140 3639 : pca_interp_preproc( hPCA->prev_ql, hPCA->prev_qr, ql, qr, IVAS_PCA_N_SLOTS, ql_interp, qr_interp );
141 :
142 3639 : slot_len = (int16_t) ( n_samples / IVAS_PCA_N_SLOTS );
143 :
144 149199 : for ( j = 0; j < IVAS_PCA_N_SLOTS; j++ )
145 : {
146 : /* convert from double quaternion to 4D matrix */
147 145560 : dquat2mat( &ql_interp[4 * j], &qr_interp[4 * j], &hPCA->mem_eigVec_interp[16 * j + IVAS_PCA_DELAY_CMP * 16] );
148 :
149 145560 : pca_inv_transform_sub( &hPCA->mem_eigVec_interp[16 * j], decoded_data, slot_len * j, slot_len, n_channels );
150 : }
151 :
152 3639 : return;
153 : }
154 :
155 :
156 34857 : static void pca_dec_update_dquat(
157 : PCA_DEC_STATE *hPCA,
158 : const float *ql,
159 : const float *qr )
160 : {
161 : /* update state for next frame */
162 34857 : mvr2r( qr, hPCA->prev_qr, IVAS_PCA_INTERP );
163 34857 : mvr2r( ql, hPCA->prev_ql, IVAS_PCA_INTERP );
164 :
165 34857 : return;
166 : }
167 :
168 :
169 : /*-------------------------------------------------------------------------
170 : * ivas_pca_dec_init()
171 : *
172 : * Initialize PCA decoder
173 : *------------------------------------------------------------------------*/
174 :
175 111 : void ivas_pca_dec_init(
176 : PCA_DEC_STATE *hPCA /* i/o: PCA decoder structure */
177 : )
178 : {
179 111 : pca_dec_reset_dquat( hPCA->prev_ql, hPCA->prev_qr );
180 111 : pca_dec_reset_mem_eigvec( hPCA );
181 :
182 : /* set counter saturated to 2 frames */
183 111 : hPCA->prev_pca_bypass = 2;
184 :
185 111 : return;
186 : }
187 :
188 :
189 : /*-------------------------------------------------------------------------
190 : * ivas_pca_read_bits()
191 : *
192 : * Decode PCA indexes
193 : *------------------------------------------------------------------------*/
194 :
195 32577 : void ivas_pca_read_bits(
196 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
197 : PCA_DEC_STATE *hPCA )
198 : {
199 : /*first bit in the PCA payload (first bit after 3 header bits) signals bypass/active*/
200 32577 : hPCA->pca_bypass = get_next_indice( st0, 1 );
201 :
202 32577 : if ( hPCA->pca_bypass == PCA_MODE_INACTIVE )
203 : {
204 32559 : return;
205 : }
206 :
207 18 : hPCA->index[0] = ivas_bitstream_read_int32( st0, IVAS_PCA_QBITS - 1 );
208 18 : hPCA->index[1] = ivas_bitstream_read_int32( st0, IVAS_PCA_QBITS );
209 :
210 18 : return;
211 : }
212 :
213 :
214 : /*-------------------------------------------------------------------------
215 : * ivas_pca_dec()
216 : *
217 : * PCA decoder
218 : *------------------------------------------------------------------------*/
219 :
220 34857 : void ivas_pca_dec(
221 : PCA_DEC_STATE *hPCA, /* i/o: PCA decoder structure */
222 : const int16_t output_frame, /* i : output frame length */
223 : const int16_t n_channels, /* i : number of channels */
224 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
225 : const int32_t last_ivas_total_brate, /* i : last IVAS total bitrate */
226 : const int16_t bfi, /* i : bad frame indicator */
227 : float *pcm_out[] /* o : output audio channels */
228 : )
229 : {
230 : float ql[IVAS_PCA_INTERP], qr[IVAS_PCA_INTERP];
231 : int16_t pca_bypass;
232 :
233 34857 : mvr2r( &hPCA->mem_eigVec_interp[IVAS_PCA_N_SLOTS * 16], hPCA->mem_eigVec_interp, IVAS_PCA_DELAY_CMP * 16 );
234 :
235 : /* @@@TODO: check how ivas_total_brate is set if bfi == 1 */ // ToDo: and what happens in DTX where "ivas_total_brate" can be close to zero?
236 :
237 : /* handle bit rate switching */
238 34857 : if ( ivas_total_brate != PCA_BRATE || ( ivas_total_brate == PCA_BRATE && n_channels > FOA_CHANNELS ) )
239 : {
240 : /* set PCA by-pass mode in current frame and interpolate transform as previous frame used PCA */
241 0 : pca_dec_reset_dquat( ql, qr );
242 :
243 0 : if ( ( last_ivas_total_brate != PCA_BRATE ) || ( last_ivas_total_brate == PCA_BRATE && hPCA->prev_pca_bypass > 1 ) )
244 : {
245 0 : pca_dec_reset_mem_eigvec( hPCA );
246 : }
247 : else
248 : {
249 0 : pca_dec_inv_transform( hPCA, ql, qr, output_frame, n_channels, pcm_out );
250 : }
251 :
252 0 : pca_dec_update_dquat( hPCA, ql, qr );
253 0 : hPCA->prev_pca_bypass += 1;
254 :
255 0 : if ( hPCA->prev_pca_bypass > 2 )
256 : {
257 0 : hPCA->prev_pca_bypass = 2;
258 : }
259 :
260 0 : return;
261 : }
262 :
263 : #ifdef DEBUGGING
264 : assert( ivas_total_brate == PCA_BRATE ); /* the remaining code is defined at 256k where there are 4 dmx channel */
265 : #endif
266 34857 : if ( !bfi )
267 : {
268 : /* set PCA by-pass mode indicator */
269 32577 : pca_bypass = hPCA->pca_bypass;
270 : }
271 : else
272 : {
273 2280 : pca_bypass = hPCA->prev_pca_bypass;
274 : }
275 :
276 34857 : if ( pca_bypass == PCA_MODE_INACTIVE )
277 : {
278 32871 : pca_dec_reset_dquat( ql, qr );
279 :
280 32871 : if ( hPCA->prev_pca_bypass > 1 ) //&& (hPCA->pca_off_hangover == 0))
281 : {
282 : /* copy input data into output directly as previous frame was already in by-pass mode */
283 31218 : pca_dec_reset_mem_eigvec( hPCA );
284 : }
285 : else
286 : {
287 1653 : pca_dec_inv_transform( hPCA, ql, qr, output_frame, n_channels, pcm_out );
288 : }
289 :
290 32871 : pca_dec_update_dquat( hPCA, ql, qr );
291 :
292 32871 : hPCA->prev_pca_bypass += 1;
293 32871 : hPCA->prev_pca_bypass = min( hPCA->prev_pca_bypass, 2 );
294 :
295 32871 : return;
296 : }
297 :
298 1986 : if ( !bfi )
299 : {
300 18 : pca_dec_s3( hPCA->index[0], ql );
301 18 : pca_dec_s3( hPCA->index[1], qr );
302 : }
303 : else
304 : {
305 : /* freeze */
306 1968 : mvr2r( hPCA->prev_ql, ql, IVAS_PCA_INTERP );
307 1968 : mvr2r( hPCA->prev_qr, qr, IVAS_PCA_INTERP );
308 : }
309 :
310 1986 : pca_dec_inv_transform( hPCA, ql, qr, output_frame, n_channels, pcm_out );
311 :
312 : /* update for next frame */
313 1986 : pca_dec_update_dquat( hPCA, ql, qr );
314 1986 : hPCA->prev_pca_bypass = 0;
315 :
316 1986 : return;
317 : }
|