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 "cnst.h"
41 : #include "prot.h"
42 : #include "rom_com.h"
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 : #include "wmc_auto.h"
47 :
48 : /*----------------------------------------------------------*
49 : * pit_decode()
50 : *
51 : * Decode OL pitch lag
52 : *----------------------------------------------------------*/
53 :
54 : /*! r: floating pitch value */
55 1643880 : float pit_decode(
56 : Decoder_State *st, /* i/o: decoder state structure */
57 : const int32_t core_brate, /* i : core bitrate */
58 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
59 : const int16_t L_frame, /* i : length of the frame */
60 : int16_t i_subfr, /* i : subframe index */
61 : const int16_t coder_type, /* i : coding type */
62 : int16_t *limit_flag, /* i/o: restrained(0) or extended(1) Q limits */
63 : int16_t *T0, /* o : close loop integer pitch */
64 : int16_t *T0_frac, /* o : close loop fractional part of the pitch */
65 : int16_t *T0_min, /* i/o: delta search min */
66 : int16_t *T0_max, /* i/o: delta search max */
67 : const int16_t L_subfr, /* i : subframe length */
68 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
69 : const float tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer */
70 : )
71 : {
72 : float pitch;
73 : int16_t pitch_index, nBits, pit_flag;
74 :
75 1643880 : pitch_index = 0;
76 :
77 : /*----------------------------------------------------------------*
78 : * Set pit_flag = 0 for every subframe with absolute pitch search
79 : *----------------------------------------------------------------*/
80 :
81 1643880 : pit_flag = i_subfr;
82 1643880 : if ( i_subfr == 2 * L_SUBFR )
83 : {
84 363417 : pit_flag = 0;
85 : }
86 :
87 : /*-------------------------------------------------------*
88 : * Retrieve the pitch index
89 : *-------------------------------------------------------*/
90 :
91 1643880 : if ( !Opt_AMR_WB )
92 : {
93 : /*----------------------------------------------------------------*
94 : * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
95 : *----------------------------------------------------------------*/
96 :
97 1643880 : if ( i_subfr == 0 )
98 : {
99 383016 : *limit_flag = 1;
100 :
101 383016 : if ( coder_type == VOICED )
102 : {
103 25632 : *limit_flag = 2; /* double-extended limits */
104 : }
105 :
106 383016 : if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
107 : {
108 2733 : *limit_flag = 0;
109 : }
110 : }
111 1260864 : else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
112 : {
113 103737 : if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
114 : {
115 78315 : *limit_flag = 0;
116 : }
117 : }
118 :
119 : /*-------------------------------------------------------*
120 : * Retrieve the number of Q bits
121 : *-------------------------------------------------------*/
122 :
123 1643880 : nBits = 0;
124 1643880 : if ( coder_type != AUDIO )
125 : {
126 1577079 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_subfr];
127 :
128 1577079 : pitch_index = get_next_indice( st, nBits );
129 : }
130 :
131 : /*-------------------------------------------------------*
132 : * Pitch decoding in AUDIO mode
133 : * (both ACELP@12k8 and ACELP@16k cores)
134 : *-------------------------------------------------------*/
135 :
136 1643880 : if ( coder_type == AUDIO )
137 : {
138 66801 : if ( L_subfr == L_FRAME / 2 && i_subfr != 0 )
139 : {
140 7980 : pit_flag = L_SUBFR;
141 : }
142 :
143 66801 : if ( pit_flag == 0 )
144 : {
145 41547 : nBits = 10;
146 : }
147 : else
148 : {
149 25254 : nBits = 6;
150 : }
151 :
152 66801 : pitch_index = get_next_indice( st, nBits );
153 :
154 66801 : if ( L_subfr == L_FRAME / 2 && i_subfr != 0 && pitch_index >= 32 ) /* safety check in case of bit errors */
155 : {
156 0 : pitch_index = pitch_index >> 1;
157 0 : st->BER_detect = 1;
158 : }
159 :
160 66801 : pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
161 : }
162 1577079 : else if ( coder_type == VOICED )
163 : {
164 : /*-------------------------------------------------------*
165 : * Pitch decoding in VOICED mode
166 : * (ACELP@12k8 core only)
167 : *-------------------------------------------------------*/
168 :
169 102528 : if ( i_subfr == 2 * L_SUBFR )
170 : {
171 25632 : pit_flag = i_subfr;
172 : }
173 :
174 102528 : pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
175 : }
176 1474551 : else if ( st->idchan == 1 && ( tdm_Pitch_reuse_flag == 1 || nBits == 4 ) )
177 396 : {
178 : /*-------------------------------------------------------*
179 : * Pitch decoding with reusing of primary channel information
180 : *-------------------------------------------------------*/
181 : int16_t loc_T0, loc_frac, delta;
182 :
183 396 : delta = 4;
184 396 : pit_flag = L_SUBFR;
185 :
186 396 : if ( L_subfr == 2 * L_SUBFR )
187 : {
188 0 : loc_T0 = (int16_t) ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] );
189 0 : loc_frac = (int16_t) ( ( ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] ) - loc_T0 ) * 4.0f );
190 : }
191 : else
192 : {
193 396 : loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
194 396 : loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
195 : }
196 :
197 396 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
198 :
199 396 : if ( nBits > 0 )
200 : {
201 0 : pit_Q_dec( 0, pitch_index, nBits, delta, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
202 : }
203 : else
204 : {
205 396 : *T0 = loc_T0;
206 396 : *T0_frac = loc_frac;
207 : }
208 : }
209 : else
210 : {
211 : /*-------------------------------------------------------*
212 : * Pitch decoding in GENERIC mode
213 : * (both ACELP@12k8 and ACELP@16k cores)
214 : *-------------------------------------------------------*/
215 :
216 1474155 : if ( L_frame == L_FRAME )
217 : {
218 534759 : pit_Q_dec( 0, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
219 : }
220 : else
221 : {
222 939396 : pit16k_Q_dec( pitch_index, nBits, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
223 : }
224 : }
225 : }
226 :
227 : /*-------------------------------------------------------*
228 : * Pitch decoding in AMR-WB IO mode
229 : *-------------------------------------------------------*/
230 :
231 : else
232 : {
233 0 : *limit_flag = 0;
234 :
235 0 : if ( i_subfr == 0 || ( i_subfr == 2 * L_SUBFR && core_brate == ACELP_8k85 ) )
236 : {
237 0 : nBits = 8;
238 : }
239 : else
240 : {
241 0 : nBits = 5;
242 : }
243 :
244 0 : if ( core_brate > ACELP_8k85 )
245 : {
246 0 : nBits = 6;
247 :
248 0 : if ( i_subfr == 0 || i_subfr == 2 * L_SUBFR )
249 : {
250 0 : nBits = 9;
251 : }
252 : }
253 :
254 0 : pitch_index = get_next_indice( st, nBits );
255 :
256 0 : pit_Q_dec( 1, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
257 : }
258 :
259 : /*-------------------------------------------------------*
260 : * Compute floating pitch output
261 : *-------------------------------------------------------*/
262 :
263 1643880 : pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
264 :
265 1643880 : return pitch;
266 : }
267 :
268 :
269 : /*---------------------------------------------------------------------*
270 : * abs_pit_dec()
271 : *
272 : * Decode the absolute pitch
273 : *---------------------------------------------------------------------*/
274 :
275 124272 : void abs_pit_dec(
276 : const int16_t fr_steps, /* i : fractional resolution steps (0, 2, 4) */
277 : int16_t pitch_index, /* i : pitch index */
278 : const int16_t limit_flag, /* i : restrained(0) or extended(1) limits */
279 : int16_t *T0, /* o : integer pitch lag */
280 : int16_t *T0_frac /* o : pitch fraction */
281 : )
282 : {
283 124272 : if ( limit_flag == 0 )
284 : {
285 91869 : if ( fr_steps == 2 )
286 : {
287 10236 : if ( pitch_index < ( PIT_FR1_8b - PIT_MIN ) * 2 )
288 : {
289 5427 : *T0 = PIT_MIN + ( pitch_index / 2 );
290 5427 : *T0_frac = pitch_index - ( ( *T0 - PIT_MIN ) * 2 );
291 5427 : *T0_frac *= 2;
292 : }
293 : else
294 : {
295 4809 : *T0 = pitch_index + PIT_FR1_8b - ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
296 4809 : *T0_frac = 0;
297 : }
298 : }
299 81633 : else if ( fr_steps == 4 )
300 : {
301 81633 : if ( pitch_index < ( PIT_FR2_9b - PIT_MIN ) * 4 )
302 : {
303 66186 : *T0 = PIT_MIN + ( pitch_index / 4 );
304 66186 : *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
305 : }
306 15447 : else if ( pitch_index < ( ( PIT_FR2_9b - PIT_MIN ) * 4 + ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) )
307 : {
308 7395 : pitch_index -= ( PIT_FR2_9b - PIT_MIN ) * 4;
309 7395 : *T0 = PIT_FR2_9b + ( pitch_index / 2 );
310 7395 : *T0_frac = pitch_index - ( *T0 - PIT_FR2_9b ) * 2;
311 7395 : ( *T0_frac ) *= 2;
312 : }
313 : else
314 : {
315 8052 : *T0 = pitch_index + PIT_FR1_9b - ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) - ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
316 8052 : *T0_frac = 0;
317 : }
318 : }
319 : else /* fr_steps == 0 */
320 : {
321 : /* not used in the codec */
322 : }
323 : }
324 32403 : else if ( limit_flag == 1 ) /* extended Q range */
325 : {
326 31179 : if ( fr_steps == 2 )
327 : {
328 2898 : if ( pitch_index < ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 )
329 : {
330 1467 : *T0 = PIT_MIN_EXTEND + ( pitch_index / 2 );
331 1467 : *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_EXTEND ) * 2 );
332 1467 : *T0_frac *= 2;
333 : }
334 : else
335 : {
336 1431 : *T0 = pitch_index + PIT_FR1_EXTEND_8b - ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
337 1431 : *T0_frac = 0;
338 : }
339 : }
340 28281 : else if ( fr_steps == 4 )
341 : {
342 28281 : if ( pitch_index < ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 )
343 : {
344 24765 : *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
345 24765 : *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
346 : }
347 3516 : else if ( pitch_index < ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 + ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) )
348 : {
349 696 : pitch_index -= ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4;
350 696 : *T0 = PIT_FR2_EXTEND_9b + ( pitch_index / 2 );
351 696 : *T0_frac = pitch_index - ( *T0 - PIT_FR2_EXTEND_9b ) * 2;
352 696 : ( *T0_frac ) *= 2;
353 : }
354 : else
355 : {
356 2820 : *T0 = pitch_index + PIT_FR1_EXTEND_9b - ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) - ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 );
357 2820 : *T0_frac = 0;
358 : }
359 : }
360 : else /* fr_steps == 0 */
361 : {
362 : /* not used in the codec */
363 : }
364 : }
365 : else /* limit_flag == 2 */
366 : {
367 1224 : if ( fr_steps == 2 )
368 : {
369 0 : if ( pitch_index < ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 )
370 : {
371 0 : *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 2 );
372 0 : *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 2 );
373 0 : *T0_frac *= 2;
374 : }
375 : else
376 : {
377 0 : *T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_8b - ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
378 0 : *T0_frac = 0;
379 : }
380 : }
381 1224 : else if ( fr_steps == 4 )
382 : {
383 1224 : if ( pitch_index < ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 )
384 : {
385 1140 : *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
386 1140 : *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
387 : }
388 84 : else if ( pitch_index < ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 + ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) )
389 : {
390 30 : pitch_index -= ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4;
391 30 : *T0 = PIT_FR2_DOUBLEEXTEND_9b + ( pitch_index / 2 );
392 30 : *T0_frac = pitch_index - ( *T0 - PIT_FR2_DOUBLEEXTEND_9b ) * 2;
393 30 : ( *T0_frac ) *= 2;
394 : }
395 : else
396 : {
397 54 : *T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_9b - ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) - ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 );
398 54 : *T0_frac = 0;
399 : }
400 : }
401 : else /* fr_steps == 0 */
402 : {
403 : /* not used in the codec */
404 : }
405 : }
406 :
407 124272 : return;
408 : }
409 :
410 :
411 : /*---------------------------------------------------------------------*
412 : * delta_pit_dec()
413 : *
414 : * Decode delta pitch
415 : *---------------------------------------------------------------------*/
416 :
417 992196 : void delta_pit_dec(
418 : const int16_t fr_steps, /* i : fractional resolution steps (0, 2, 4) */
419 : const int16_t pitch_index, /* i : pitch index */
420 : int16_t *T0, /* o : integer pitch lag */
421 : int16_t *T0_frac, /* o : pitch fraction */
422 : const int16_t T0_min /* i : delta search min */
423 : )
424 : {
425 992196 : if ( fr_steps == 0 )
426 : {
427 573 : *T0 = T0_min + pitch_index;
428 573 : *T0_frac = 0;
429 : }
430 991623 : else if ( fr_steps == 2 )
431 : {
432 27690 : *T0 = T0_min + pitch_index / 2;
433 27690 : *T0_frac = pitch_index - ( *T0 - T0_min ) * 2;
434 27690 : *T0_frac *= 2;
435 : }
436 963933 : else if ( fr_steps == 4 )
437 : {
438 963933 : *T0 = T0_min + pitch_index / 4;
439 963933 : *T0_frac = pitch_index - ( *T0 - T0_min ) * 4;
440 : }
441 :
442 992196 : return;
443 : }
444 :
445 :
446 : /*-------------------------------------------------*
447 : * pit_Q_dec()
448 : *
449 : * pitch decoding
450 : *-------------------------------------------------*/
451 :
452 735864 : void pit_Q_dec(
453 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
454 : const int16_t pitch_index, /* i : pitch index */
455 : const int16_t nBits, /* i : # of Q bits */
456 : const int16_t delta, /* i : Half the CL searched interval */
457 : const int16_t pit_flag, /* i : absolute(0) or delta(1) pitch Q */
458 : const int16_t limit_flag, /* i : restrained(0) or extended(1) Q limits */
459 : int16_t *T0, /* o : integer pitch lag */
460 : int16_t *T0_frac, /* o : pitch fraction */
461 : int16_t *T0_min, /* i/o: delta search min */
462 : int16_t *T0_max, /* i/o: delta search max */
463 : int16_t *BER_detect /* o : BER detect flag */
464 : )
465 : {
466 735864 : if ( nBits == 10 ) /* absolute decoding with 10 bits */
467 : {
468 220962 : if ( limit_flag == 0 )
469 : {
470 0 : *T0 = PIT_MIN + ( pitch_index / 4 );
471 0 : *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
472 : }
473 220962 : else if ( limit_flag == 1 )
474 : {
475 196554 : *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
476 196554 : *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
477 : }
478 : else /* limit_flag == 2 */
479 : {
480 24408 : *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
481 24408 : *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
482 : }
483 : }
484 514902 : else if ( nBits == 9 ) /* absolute decoding with 9 bits */
485 : {
486 105894 : abs_pit_dec( 4, pitch_index, limit_flag, T0, T0_frac );
487 :
488 : /* find T0_min and T0_max for delta search */
489 105894 : if ( Opt_AMR_WB )
490 : {
491 0 : limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
492 : }
493 : }
494 409008 : else if ( nBits == 8 ) /* absolute decoding with 8 bits */
495 : {
496 13086 : abs_pit_dec( 2, pitch_index, limit_flag, T0, T0_frac );
497 :
498 : /* find T0_min and T0_max for delta search */
499 13086 : if ( Opt_AMR_WB )
500 : {
501 0 : limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
502 : }
503 : }
504 395922 : else if ( nBits == 6 ) /* relative decoding with 6 bits */
505 : {
506 298473 : delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
507 : }
508 97449 : else if ( nBits == 5 ) /* relative decoding with 5 bits */
509 : {
510 97449 : if ( delta == 8 )
511 : {
512 20553 : delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
513 : }
514 : else /* delta == 4 */
515 : {
516 76896 : delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
517 : }
518 : }
519 : else /* nBits == 4 */ /* relative decoding with 4 bits */
520 : {
521 0 : if ( delta == 8 )
522 : {
523 0 : delta_pit_dec( 0, pitch_index, T0, T0_frac, *T0_min );
524 : }
525 : else /* delta == 4 */
526 : {
527 0 : delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
528 : }
529 : }
530 :
531 : /* biterror detection mechanism */
532 735864 : if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT_MAX << 2 ) + 2 && pit_flag == 0 && !Opt_AMR_WB )
533 : {
534 0 : *T0 = L_SUBFR;
535 0 : *T0_frac = 0;
536 0 : *BER_detect = 1;
537 : }
538 :
539 735864 : if ( !Opt_AMR_WB )
540 : {
541 : /* find T0_min and T0_max for delta search */
542 735864 : limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
543 : }
544 :
545 735864 : return;
546 : }
547 :
548 : /*-------------------------------------------------*
549 : * pit16k_Q_dec()
550 : *
551 : * pitch decoding @16kHz core
552 : *-------------------------------------------------*/
553 :
554 982245 : void pit16k_Q_dec(
555 : const int16_t pitch_index, /* i : pitch index */
556 : const int16_t nBits, /* i : # of Q bits */
557 : const int16_t limit_flag, /* i : restrained(0) or extended(1) limits */
558 : int16_t *T0, /* o : integer pitch lag */
559 : int16_t *T0_frac, /* o : pitch fraction */
560 : int16_t *T0_min, /* i/o: delta search min */
561 : int16_t *T0_max, /* i/o: delta search max */
562 : int16_t *BER_detect /* o : BER detect flag */
563 : )
564 : {
565 : int16_t index;
566 :
567 982245 : if ( nBits == 10 ) /* absolute decoding with 10 bits */
568 : {
569 : {
570 420582 : if ( pitch_index < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
571 : {
572 409116 : *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
573 409116 : *T0_frac = pitch_index - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
574 : }
575 : else
576 : {
577 11466 : index = pitch_index - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
578 11466 : *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
579 11466 : *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
580 11466 : ( *T0_frac ) *= 2;
581 : }
582 : }
583 : }
584 561663 : else if ( nBits == 9 ) /* absolute decoding with 9 bits */
585 : {
586 : {
587 0 : if ( pitch_index < ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 )
588 : {
589 0 : *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
590 0 : *T0_frac = pitch_index - ( *T0 - PIT16k_MIN_EXTEND ) * 4;
591 : }
592 0 : else if ( pitch_index < ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 + ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) )
593 : {
594 0 : index = pitch_index - ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4;
595 0 : *T0 = PIT16k_FR2_EXTEND_9b + ( index / 2 );
596 0 : *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_9b ) * 2;
597 0 : ( *T0_frac ) *= 2;
598 : }
599 : else
600 : {
601 0 : *T0 = pitch_index + PIT16k_FR1_EXTEND_9b - ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) - ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 );
602 0 : *T0_frac = 0;
603 : }
604 : }
605 : }
606 : else /* nBits == 6 */ /* relative decoding with 6 bits */
607 : {
608 561663 : delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
609 : }
610 :
611 : /* biterror detection mechanism */
612 982245 : if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT16k_MAX << 2 ) && nBits >= 9 )
613 : {
614 0 : *T0 = L_SUBFR;
615 0 : *T0_frac = 0;
616 0 : *BER_detect = 1;
617 : }
618 :
619 : /* find T0_min and T0_max for delta search */
620 982245 : limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
621 :
622 982245 : return;
623 : }
624 :
625 :
626 : /*----------------------------------------------------------*
627 : * Mode2_pit_decode()
628 : *
629 : * Decode pitch lag
630 : *----------------------------------------------------------*/
631 :
632 : /*! r: floating pitch value */
633 9060 : float Mode2_pit_decode(
634 : const int16_t coder_type, /* i : coding model */
635 : const int16_t i_subfr, /* i : subframe index */
636 : const int16_t L_subfr, /* i : sub-frame length */
637 : int16_t **pt_indice, /* i/o: quantization indices pointer */
638 : int16_t *T0, /* i/o: close loop integer pitch */
639 : int16_t *T0_frac, /* o : close loop fractional part of the pitch */
640 : int16_t *T0_res, /* i/o: pitch resolution */
641 : int16_t *T0_min, /* i/o: lower limit for close-loop search */
642 : int16_t *T0_min_frac, /* i/o: lower limit for close-loop search */
643 : int16_t *T0_max, /* i/o: higher limit for close-loop search */
644 : int16_t *T0_max_frac, /* i/o: higher limit for close-loop search */
645 : const int16_t pit_min,
646 : const int16_t pit_fr1,
647 : const int16_t pit_fr1b,
648 : const int16_t pit_fr2,
649 : const int16_t pit_max,
650 : const int16_t pit_res_max )
651 : {
652 : float pitch;
653 : int16_t pit_res_max2;
654 :
655 9060 : if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
656 : {
657 0 : *T0 = L_subfr;
658 0 : *T0_frac = 0;
659 0 : *T0_res = 1;
660 : }
661 9060 : else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
662 : {
663 0 : if ( i_subfr == 0 )
664 : {
665 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
666 : }
667 : else
668 : {
669 0 : limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
670 0 : *T0_res = ( pit_res_max >> 1 );
671 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
672 : }
673 : }
674 9060 : else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
675 : {
676 :
677 0 : if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
678 : {
679 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
680 : }
681 : else
682 : {
683 0 : limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
684 0 : *T0_res = ( pit_res_max >> 1 );
685 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
686 : }
687 : }
688 9060 : else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
689 : {
690 4020 : pit_res_max2 = pit_res_max;
691 4020 : if ( pit_min == PIT_MIN_16k )
692 : {
693 4020 : pit_res_max2 = pit_res_max >> 1;
694 : }
695 :
696 4020 : if ( i_subfr == 0 )
697 : {
698 804 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
699 : }
700 : else
701 : {
702 3216 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
703 3216 : *T0_res = pit_res_max2;
704 3216 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
705 : }
706 : }
707 5040 : else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
708 : {
709 5040 : pit_res_max2 = pit_res_max;
710 5040 : if ( pit_min == PIT_MIN_16k )
711 : {
712 5040 : pit_res_max2 = pit_res_max >> 1;
713 : }
714 :
715 5040 : if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
716 : {
717 2016 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
718 : }
719 : else
720 : {
721 3024 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
722 3024 : *T0_res = pit_res_max2;
723 3024 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
724 : }
725 : }
726 0 : else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
727 : {
728 0 : if ( i_subfr == 0 )
729 : {
730 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
731 : }
732 : else
733 : {
734 0 : limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
735 0 : *T0_res = ( pit_res_max >> 1 );
736 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
737 : }
738 : }
739 0 : else if ( coder_type == 9 ) /* 8/0/8/0 (RF gen pred mode) */
740 : {
741 0 : if ( i_subfr == 0 )
742 : {
743 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
744 : }
745 : else
746 : {
747 0 : limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
748 0 : *T0_res = ( pit_res_max >> 1 );
749 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
750 : }
751 : }
752 : else /*RCELP 8 bits pitch delay*/
753 : {
754 0 : assert( 0 );
755 : }
756 :
757 : /*-------------------------------------------------------*
758 : * Compute floating pitch output
759 : *-------------------------------------------------------*/
760 :
761 9060 : pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / (float) ( *T0_res ); /* save subframe pitch values */
762 :
763 9060 : return pitch;
764 : }
765 :
766 :
767 : /*---------------------------------------------------------------------*
768 : * Mode2_abs_pit_dec()
769 : *
770 : * Decode the absolute pitch
771 : *---------------------------------------------------------------------*/
772 :
773 2820 : void Mode2_abs_pit_dec(
774 : int16_t *T0, /* o : integer pitch lag */
775 : int16_t *T0_frac, /* o : pitch fraction */
776 : int16_t *T0_res, /* o : pitch resolution */
777 : int16_t **pt_indice, /* i/o: pointer to Vector of Q indexes */
778 : const int16_t pit_min,
779 : const int16_t pit_fr1,
780 : const int16_t pit_fr2,
781 : const int16_t pit_res_max )
782 : {
783 : int16_t index;
784 : int16_t pit_res_max_half;
785 :
786 2820 : index = **pt_indice;
787 2820 : ( *pt_indice )++;
788 2820 : pit_res_max_half = pit_res_max >> 1;
789 :
790 2820 : if ( index < ( pit_fr2 - pit_min ) * pit_res_max )
791 : {
792 0 : *T0 = pit_min + ( index / pit_res_max );
793 :
794 0 : *T0_frac = index - ( *T0 - pit_min ) * pit_res_max;
795 0 : *T0_res = pit_res_max;
796 : }
797 2820 : else if ( index < ( ( pit_fr2 - pit_min ) * pit_res_max + ( pit_fr1 - pit_fr2 ) * pit_res_max_half ) )
798 : {
799 2496 : index -= ( pit_fr2 - pit_min ) * pit_res_max;
800 :
801 2496 : *T0 = pit_fr2 + ( index / pit_res_max_half );
802 :
803 2496 : *T0_frac = index - ( *T0 - pit_fr2 ) * pit_res_max_half;
804 2496 : *T0_res = pit_res_max_half;
805 : }
806 : else
807 : {
808 324 : *T0 = index + pit_fr1 - ( ( pit_fr2 - pit_min ) * pit_res_max ) - ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
809 324 : *T0_frac = 0;
810 324 : *T0_res = 1;
811 : }
812 :
813 2820 : return;
814 : }
815 :
816 :
817 : /*---------------------------------------------------------------------*
818 : * Routine Mode2_delta_pit_dec()
819 : *
820 : * Decode delta pitch
821 : *---------------------------------------------------------------------*/
822 :
823 6240 : void Mode2_delta_pit_dec(
824 : int16_t *T0, /* o : integer pitch lag */
825 : int16_t *T0_frac, /* o : pitch fraction */
826 : int16_t T0_res, /* i : pitch resolution */
827 : int16_t *T0_min, /* i/o: delta search min */
828 : int16_t *T0_min_frac, /* i : delta search min */
829 : int16_t **pt_indice /* i/o: pointer to Vector of Q indexes */
830 : )
831 : {
832 : int16_t index;
833 :
834 6240 : index = **pt_indice;
835 6240 : ( *pt_indice )++;
836 :
837 6240 : *T0 = *T0_min + ( index + *T0_min_frac ) / T0_res;
838 :
839 6240 : *T0_frac = index + *T0_min_frac - ( *T0 - *T0_min ) * T0_res;
840 :
841 6240 : return;
842 : }
|