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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "prot.h"
41 : #include "rom_com.h"
42 : #include "wmc_auto.h"
43 :
44 : /*---------------------------------------------------------------------*
45 : * Local function prototypes
46 : *---------------------------------------------------------------------*/
47 :
48 : static void D_ACELP_decode_arithtrack( float v[], uint32_t s, int16_t p, const int16_t trackstep, const int16_t tracklen );
49 :
50 : /*---------------------------------------------------------------------*
51 : * Function D_ACELP_indexing()
52 : *
53 : *---------------------------------------------------------------------*/
54 :
55 1674933 : void D_ACELP_indexing(
56 : float code[],
57 : PulseConfig config,
58 : const int16_t num_tracks,
59 : int16_t index[],
60 : int16_t *BER_detect )
61 : {
62 : int16_t track, pulses, k, pulsestrack[NB_TRACK_FCB_4T];
63 : uint32_t s;
64 : uint32_t index_n[NB_TRACK_FCB_4T];
65 : uint16_t trackpos, idxs[MAX_IDX_LEN];
66 : int16_t restpulses, wordcnt;
67 :
68 1674933 : assert( num_tracks == NB_TRACK_FCB_4T );
69 :
70 1674933 : wordcnt = ( config.bits + 15 ) >> 4; /* ceil(bits/16) */
71 :
72 : /* check if some tracks have more pulses */
73 1674933 : restpulses = config.nb_pulse & ( num_tracks - 1 );
74 :
75 : /* cast to short */
76 7039308 : for ( k = 0; k < wordcnt; k++ )
77 : {
78 5364375 : idxs[k] = (uint16_t) index[k];
79 : }
80 :
81 1674933 : if ( restpulses )
82 : {
83 : /* check if we need to code track positions */
84 1191648 : switch ( config.codetrackpos )
85 : {
86 190944 : case TRACKPOS_FREE_THREE:
87 : /* Find track with less pulses */
88 190944 : trackpos = idxs[0] & 3;
89 190944 : longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
90 :
91 : /* set number of pulses per track */
92 190944 : set_s( pulsestrack, ( config.nb_pulse >> 2 ) + 1, 4 );
93 190944 : pulsestrack[trackpos]--; /* this one has less pulses */
94 190944 : break;
95 63825 : case TRACKPOS_FREE_ONE:
96 : /* Find track with more pulses */
97 63825 : trackpos = idxs[0] & 3;
98 63825 : longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
99 :
100 : /* set number of pulses per track */
101 63825 : set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
102 63825 : pulsestrack[trackpos]++; /* this one has more pulses */
103 63825 : break;
104 0 : case TRACKPOS_FIXED_EVEN:
105 : /* Pulses on even tracks */
106 0 : pulsestrack[0] = ( config.nb_pulse + 1 ) >> 1;
107 0 : pulsestrack[1] = 0;
108 0 : pulsestrack[2] = config.nb_pulse >> 1;
109 0 : pulsestrack[3] = 0;
110 0 : break;
111 719154 : case TRACKPOS_FIXED_FIRST:
112 : /* set number of pulses per track */
113 719154 : set_s( pulsestrack, config.nb_pulse / num_tracks, 4 );
114 2145861 : for ( k = 0; k < restpulses; k++ )
115 : {
116 1426707 : pulsestrack[k]++;
117 : }
118 719154 : break;
119 217725 : case TRACKPOS_FIXED_TWO:
120 : /* 1100, 0110, 0011, 1001 */
121 : /* Find track with less pulses */
122 217725 : trackpos = idxs[0] & 3;
123 217725 : longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
124 :
125 : /* set number of pulses per track */
126 217725 : set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
127 217725 : pulsestrack[trackpos]++;
128 217725 : trackpos++;
129 217725 : trackpos &= 3;
130 217725 : pulsestrack[trackpos]++;
131 217725 : break;
132 0 : default:
133 0 : assert( 0 );
134 : break;
135 : }
136 : }
137 : else
138 : {
139 : /* set number of pulses per track */
140 483285 : set_s( pulsestrack, ( config.nb_pulse / num_tracks ), num_tracks );
141 : }
142 :
143 1674933 : if ( config.bits == 43 )
144 : {
145 99102 : D_ACELP_decode_43bit( idxs, code, pulsestrack );
146 : }
147 : else
148 : {
149 1575831 : fcb_pulse_track_joint_decode( idxs, wordcnt, index_n, pulsestrack, num_tracks );
150 :
151 6303324 : for ( track = num_tracks - 1; track >= 1; track-- )
152 : {
153 4727493 : pulses = pulsestrack[track];
154 :
155 4727493 : if ( pulses )
156 : {
157 4678728 : s = index_n[track];
158 :
159 : /* decode state to actual pulse positions on track */
160 : /*D_ACELP_decode_arithtrack_old(code+track, s, pulses, 4); */
161 4678728 : D_ACELP_decode_arithtrack( code + track, s, pulses, num_tracks, 16 );
162 : }
163 : else
164 : {
165 : /* track is empty */
166 829005 : for ( k = track; k < 16 * num_tracks; k += num_tracks )
167 : {
168 780240 : code[k] = 0.0f;
169 : }
170 : }
171 : }
172 1575831 : s = index_n[0];
173 1575831 : pulses = pulsestrack[0];
174 : /* safety check in case of bit errors */
175 1575831 : if ( s >= pulsestostates[16][pulses - 1] )
176 : {
177 0 : set_f( code, 0.0f, L_SUBFR );
178 0 : *BER_detect = 1;
179 0 : return;
180 : }
181 1575831 : if ( pulses )
182 : {
183 1560780 : D_ACELP_decode_arithtrack( code, s, pulses, num_tracks, 16 );
184 : }
185 : else
186 : {
187 : /* track is empty */
188 255867 : for ( k = 0; k < 16 * num_tracks; k += num_tracks )
189 : {
190 240816 : code[k] = 0.0f;
191 : }
192 : }
193 : }
194 :
195 1674933 : return;
196 : }
197 :
198 :
199 6239508 : static void D_ACELP_decode_arithtrack(
200 : float v[],
201 : uint32_t s,
202 : int16_t p,
203 : const int16_t trackstep,
204 : const int16_t tracklen )
205 : {
206 : int16_t k;
207 :
208 106071636 : for ( k = (tracklen) -1; k >= 0; k-- )
209 : {
210 99832128 : v[k * trackstep] = 0.0f; /* default: there is no pulse here */
211 116130534 : while ( ( p ) && ( s >= pulsestostates[k][p - 1] ) )
212 : {
213 16298406 : s -= pulsestostates[k][p - 1];
214 16298406 : if ( v[k * trackstep] )
215 : {
216 : /* there is a pulse here already = sign is known */
217 921204 : if ( v[k * trackstep] > 0.0f )
218 : {
219 454746 : v[k * trackstep]++; /* place one more pulse here */
220 : }
221 : else
222 : {
223 466458 : v[k * trackstep]--; /* place one more pulse here */
224 : }
225 : }
226 : else
227 : {
228 : /* this is the first pulse here -> determine sign */
229 15377202 : if ( s & 1 )
230 : {
231 7705758 : v[k * trackstep] = -1.0f; /* place a negative pulse here */
232 : }
233 : else
234 : {
235 7671444 : v[k * trackstep] = +1.0f; /* place a negative pulse here */
236 : }
237 15377202 : s >>= 1;
238 : }
239 16298406 : p--; /* one pulse placed, so one less left */
240 : }
241 : }
242 :
243 6239508 : return;
244 : }
245 :
246 :
247 1575831 : void fcb_pulse_track_joint_decode(
248 : uint16_t *idxs,
249 : const int16_t wordcnt,
250 : uint32_t *index_n,
251 : const int16_t *pulse_num,
252 : const int16_t track_num )
253 : {
254 1575831 : int16_t hi_to_low[10] = { 0, 0, 0, 3, 9, 5, 3, 1, 8, 8 };
255 :
256 : uint64_t index;
257 : int16_t indx_flag, indx_flag_1;
258 : int16_t track, track_num1, pulse_num0, pulse_num1;
259 : int64_t indx_tmp, div_tmp; /* must be long */
260 : int16_t indx_flag_2;
261 :
262 1575831 : indx_flag = 0;
263 1575831 : indx_flag_1 = 0;
264 1575831 : indx_flag_2 = 0;
265 7879155 : for ( track = 0; track < track_num; track++ )
266 : {
267 6303324 : indx_flag += ( pulse_num[track] >> 2 );
268 6303324 : indx_flag_1 += ( pulse_num[track] >> 1 );
269 6303324 : indx_flag_2 += ( pulse_num[track] >> 3 );
270 : }
271 :
272 1575831 : if ( indx_flag >= track_num )
273 : {
274 241710 : hi_to_low[4] = 9;
275 : }
276 : else
277 : {
278 1334121 : hi_to_low[4] = 1;
279 : }
280 :
281 1575831 : if ( indx_flag_2 >= 1 )
282 : {
283 90 : hi_to_low[7] = 9;
284 : }
285 : else
286 : {
287 1575741 : hi_to_low[7] = 1;
288 : }
289 :
290 1575831 : if ( indx_flag_1 >= track_num )
291 : {
292 999795 : if ( indx_flag >= track_num )
293 : {
294 241710 : index = 0;
295 241710 : if ( indx_flag_2 >= 1 )
296 : {
297 90 : for ( track = ( wordcnt - 1 ); track >= 6; track-- )
298 : {
299 0 : index = ( index << 16 ) + idxs[track];
300 : }
301 90 : index_n[3] = ( ( (uint32_t) idxs[5] ) << 8 ) + ( ( idxs[4] >> 8 ) & 0xff );
302 90 : index_n[2] = ( ( ( (uint32_t) idxs[4] ) << 16 ) + idxs[3] ) & 0xffffffUL;
303 90 : index_n[1] = ( ( (uint32_t) idxs[2] ) << 8 ) + ( ( idxs[1] >> 8 ) & 0xff );
304 90 : index_n[0] = ( ( ( (uint32_t) idxs[1] ) << 16 ) + idxs[0] ) & 0xffffffUL;
305 : }
306 : else
307 : {
308 508260 : for ( track = ( wordcnt - 1 ); track >= track_num; track-- )
309 : {
310 266640 : index = ( index << 16 ) + idxs[track];
311 : }
312 1208100 : for ( track = 0; track < track_num; track++ )
313 : {
314 966480 : index_n[track] = idxs[track];
315 : }
316 : }
317 : }
318 : else
319 : {
320 758085 : index = 0;
321 1854402 : for ( track = ( wordcnt - 1 ); track >= 2; track-- )
322 : {
323 1096317 : index = ( index << 16 ) + idxs[track];
324 : }
325 :
326 758085 : index_n[3] = idxs[1] & 0xff;
327 758085 : index_n[2] = idxs[1] >> 8;
328 758085 : index_n[1] = idxs[0] & 0xff;
329 758085 : index_n[0] = idxs[0] >> 8;
330 : }
331 :
332 999795 : track_num1 = track_num - 1;
333 999795 : pulse_num1 = pulse_num[track_num1];
334 999795 : index = ( index << hi_to_low[pulse_num1] ) + ( index_n[track_num1] >> low_len[pulse_num1] );
335 3999180 : for ( track = ( track_num - 1 ); track > 0; track-- )
336 : {
337 2999385 : track_num1 = track - 1;
338 2999385 : pulse_num0 = pulse_num[track_num1];
339 2999385 : pulse_num1 = pulse_num[track];
340 2999385 : index = ( index << hi_to_low[pulse_num0] ) + ( index_n[track_num1] >> low_len[pulse_num0] );
341 :
342 2999385 : div_tmp = index / indx_fact[pulse_num1];
343 2999385 : indx_tmp = index - div_tmp * indx_fact[pulse_num1];
344 2999385 : index_n[track] = (uint32_t) ( ( index_n[track] & low_mask[pulse_num1] ) + ( indx_tmp << low_len[pulse_num1] ) );
345 2999385 : index = div_tmp;
346 : }
347 999795 : pulse_num1 = pulse_num[0];
348 999795 : index_n[0] = (uint32_t) ( ( index_n[0] & low_mask[pulse_num1] ) + ( index << low_len[pulse_num1] ) );
349 : }
350 : else
351 : {
352 576036 : index = 0;
353 1796958 : for ( track = ( wordcnt - 1 ); track >= 0; track-- )
354 : {
355 1220922 : index = ( index << 16 ) + idxs[track];
356 : }
357 2304144 : for ( track = 3; track > 0; track-- )
358 : {
359 1728108 : pulse_num1 = pulse_num[track];
360 1728108 : index_n[track] = index & index_mask_ACELP[pulse_num1];
361 1728108 : index = index >> index_len[pulse_num1];
362 : }
363 576036 : index_n[0] = (uint32_t) index;
364 : }
365 :
366 1575831 : return;
367 : }
|