1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21 3GPP TS 26.173
22 ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
24
25 (C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34 Filename: pvamrwbdecoder.cpp
35
36 Date: 05/08/2004
37
38 ------------------------------------------------------------------------------
39 REVISION HISTORY
40
41
42 Description:
43
44 ------------------------------------------------------------------------------
45 INPUT AND OUTPUT DEFINITIONS
46
47 int16 mode, input : used mode
48 int16 prms[], input : parameter vector
49 int16 synth16k[], output: synthesis speech
50 int16 * frame_length, output: lenght of the frame
51 void *spd_state, i/o : State structure
52 int16 frame_type, input : received frame type
53 int16 ScratchMem[]
54
55 ------------------------------------------------------------------------------
56 FUNCTION DESCRIPTION
57
58 Performs the main decoder routine AMR WB ACELP coding algorithm with 20 ms
59 speech frames for wideband speech signals.
60
61
62 ------------------------------------------------------------------------------
63 REQUIREMENTS
64
65
66 ------------------------------------------------------------------------------
67 REFERENCES
68
69 ------------------------------------------------------------------------------
70 PSEUDO-CODE
71
72 ------------------------------------------------------------------------------
73 */
74
75
76 /*----------------------------------------------------------------------------
77 ; INCLUDES
78 ----------------------------------------------------------------------------*/
79
80 #include "pv_amr_wb_type_defs.h"
81 #include "pvamrwbdecoder_mem_funcs.h"
82 #include "pvamrwbdecoder_basic_op.h"
83 #include "pvamrwbdecoder_cnst.h"
84 #include "pvamrwbdecoder_acelp.h"
85 #include "e_pv_amrwbdec.h"
86 #include "get_amr_wb_bits.h"
87 #include "pvamrwb_math_op.h"
88 #include "pvamrwbdecoder_api.h"
89 #include "pvamrwbdecoder.h"
90 #include "synthesis_amr_wb.h"
91
92
93 /*----------------------------------------------------------------------------
94 ; MACROS
95 ; Define module specific macros here
96 ----------------------------------------------------------------------------*/
97
98
99 /*----------------------------------------------------------------------------
100 ; DEFINES
101 ; Include all pre-processor statements here. Include conditional
102 ; compile variables also.
103 ----------------------------------------------------------------------------*/
104
105 /*----------------------------------------------------------------------------
106 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
107 ; Variable declaration - defined here and used outside this module
108 ----------------------------------------------------------------------------*/
109
110 /* LPC interpolation coef {0.45, 0.8, 0.96, 1.0}; in Q15 */
111 static const int16 interpol_frac[NB_SUBFR] = {14746, 26214, 31457, 32767};
112
113
114 /* isp tables for initialization */
115
116 static const int16 isp_init[M] =
117 {
118 32138, 30274, 27246, 23170, 18205, 12540, 6393, 0,
119 -6393, -12540, -18205, -23170, -27246, -30274, -32138, 1475
120 };
121
122 static const int16 isf_init[M] =
123 {
124 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192,
125 9216, 10240, 11264, 12288, 13312, 14336, 15360, 3840
126 };
127
128 /*----------------------------------------------------------------------------
129 ; EXTERNAL FUNCTION REFERENCES
130 ; Declare functions defined elsewhere and referenced in this module
131 ----------------------------------------------------------------------------*/
132
133 /*----------------------------------------------------------------------------
134 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
135 ; Declare variables used in this module but defined elsewhere
136 ----------------------------------------------------------------------------*/
137
138 /*----------------------------------------------------------------------------
139 ; FUNCTION CODE
140 ----------------------------------------------------------------------------*/
141
142 /*----------------------------------------------------------------------------
143 FUNCTION DESCRIPTION pvDecoder_AmrWb_Init
144
145 Initialization of variables for the decoder section.
146
147 ----------------------------------------------------------------------------*/
148
149
150
151
pvDecoder_AmrWb_Init(void ** spd_state,void * pt_st,int16 ** ScratchMem)152 void pvDecoder_AmrWb_Init(void **spd_state, void *pt_st, int16 **ScratchMem)
153 {
154 /* Decoder states */
155 Decoder_State *st = &(((PV_AmrWbDec *)pt_st)->state);
156
157 *ScratchMem = ((PV_AmrWbDec *)pt_st)->ScratchMem;
158 /*
159 * Init dtx decoding
160 */
161 dtx_dec_amr_wb_reset(&(st->dtx_decSt), isf_init);
162
163 pvDecoder_AmrWb_Reset((void *) st, 1);
164
165 *spd_state = (void *) st;
166
167 return;
168 }
169
170 /*----------------------------------------------------------------------------
171 ; FUNCTION CODE
172 ----------------------------------------------------------------------------*/
173
pvDecoder_AmrWb_Reset(void * st,int16 reset_all)174 void pvDecoder_AmrWb_Reset(void *st, int16 reset_all)
175 {
176 int16 i;
177
178 Decoder_State *dec_state;
179
180 dec_state = (Decoder_State *) st;
181
182 pv_memset((void *)dec_state->old_exc,
183 0,
184 (PIT_MAX + L_INTERPOL)*sizeof(*dec_state->old_exc));
185
186 pv_memset((void *)dec_state->past_isfq,
187 0,
188 M*sizeof(*dec_state->past_isfq));
189
190
191 dec_state->old_T0_frac = 0; /* old pitch value = 64.0 */
192 dec_state->old_T0 = 64;
193 dec_state->first_frame = 1;
194 dec_state->L_gc_thres = 0;
195 dec_state->tilt_code = 0;
196
197 pv_memset((void *)dec_state->disp_mem,
198 0,
199 8*sizeof(*dec_state->disp_mem));
200
201
202 /* scaling memories for excitation */
203 dec_state->Q_old = Q_MAX;
204 dec_state->Qsubfr[3] = Q_MAX;
205 dec_state->Qsubfr[2] = Q_MAX;
206 dec_state->Qsubfr[1] = Q_MAX;
207 dec_state->Qsubfr[0] = Q_MAX;
208
209 if (reset_all != 0)
210 {
211 /* routines initialization */
212
213 dec_gain2_amr_wb_init(dec_state->dec_gain);
214 oversamp_12k8_to_16k_init(dec_state->mem_oversamp);
215 band_pass_6k_7k_init(dec_state->mem_hf);
216 low_pass_filt_7k_init(dec_state->mem_hf3);
217 highpass_50Hz_at_12k8_init(dec_state->mem_sig_out);
218 highpass_400Hz_at_12k8_init(dec_state->mem_hp400);
219 Init_Lagconc(dec_state->lag_hist);
220
221 /* isp initialization */
222
223 pv_memcpy((void *)dec_state->ispold, (void *)isp_init, M*sizeof(*isp_init));
224
225 pv_memcpy((void *)dec_state->isfold, (void *)isf_init, M*sizeof(*isf_init));
226 for (i = 0; i < L_MEANBUF; i++)
227 {
228 pv_memcpy((void *)&dec_state->isf_buf[i * M],
229 (void *)isf_init,
230 M*sizeof(*isf_init));
231 }
232 /* variable initialization */
233
234 dec_state->mem_deemph = 0;
235
236 dec_state->seed = 21845; /* init random with 21845 */
237 dec_state->seed2 = 21845;
238 dec_state->seed3 = 21845;
239
240 dec_state->state = 0;
241 dec_state->prev_bfi = 0;
242
243 /* Static vectors to zero */
244
245 pv_memset((void *)dec_state->mem_syn_hf,
246 0,
247 M16k*sizeof(*dec_state->mem_syn_hf));
248
249 pv_memset((void *)dec_state->mem_syn_hi,
250 0,
251 M*sizeof(*dec_state->mem_syn_hi));
252
253 pv_memset((void *)dec_state->mem_syn_lo,
254 0,
255 M*sizeof(*dec_state->mem_syn_lo));
256
257
258 dtx_dec_amr_wb_reset(&(dec_state->dtx_decSt), isf_init);
259 dec_state->vad_hist = 0;
260
261 }
262 return;
263 }
264
265 /*----------------------------------------------------------------------------
266 ; FUNCTION CODE
267 ----------------------------------------------------------------------------*/
268
pvDecoder_AmrWbMemRequirements()269 int32 pvDecoder_AmrWbMemRequirements()
270 {
271 return(sizeof(PV_AmrWbDec));
272 }
273
274
275 /*----------------------------------------------------------------------------
276 ; FUNCTION CODE
277 ----------------------------------------------------------------------------*/
278
279 /* Main decoder routine. */
280
pvDecoder_AmrWb(int16 mode,int16 prms[],int16 synth16k[],int16 * frame_length,void * spd_state,int16 frame_type,int16 ScratchMem[])281 int32 pvDecoder_AmrWb(
282 int16 mode, /* input : used mode */
283 int16 prms[], /* input : parameter vector */
284 int16 synth16k[], /* output: synthesis speech */
285 int16 * frame_length, /* output: lenght of the frame */
286 void *spd_state, /* i/o : State structure */
287 int16 frame_type, /* input : received frame type */
288 int16 ScratchMem[]
289 )
290 {
291
292 /* Decoder states */
293 Decoder_State *st;
294
295 int16 *ScratchMem2 = &ScratchMem[ L_SUBFR + L_SUBFR16k + ((L_SUBFR + M + M16k +1)<<1)];
296
297
298 /* Excitation vector */
299
300
301 int16 *old_exc = ScratchMem2;
302
303 int16 *Aq = &old_exc[(L_FRAME + 1) + PIT_MAX + L_INTERPOL];/* A(z) quantized for the 4 subframes */
304
305 int16 *ispnew = &Aq[NB_SUBFR * (M + 1)];/* immittance spectral pairs at 4nd sfr */
306 int16 *isf = &ispnew[M]; /* ISF (frequency domain) at 4nd sfr */
307 int16 *isf_tmp = &isf[M];
308 int16 *code = &isf_tmp[M]; /* algebraic codevector */
309 int16 *excp = &code[L_SUBFR];
310 int16 *exc2 = &excp[L_SUBFR]; /* excitation vector */
311 int16 *HfIsf = &exc2[L_FRAME];
312
313
314 int16 *exc;
315
316 /* LPC coefficients */
317
318 int16 *p_Aq; /* ptr to A(z) for the 4 subframes */
319
320
321
322 int16 fac, stab_fac, voice_fac, Q_new = 0;
323 int32 L_tmp, L_gain_code;
324
325 /* Scalars */
326
327 int16 i, j, i_subfr, index, ind[8], tmp;
328 int32 max;
329 int16 T0, T0_frac, pit_flag, T0_max, select, T0_min = 0;
330 int16 gain_pit, gain_code;
331 int16 newDTXState, bfi, unusable_frame, nb_bits;
332 int16 vad_flag;
333 int16 pit_sharp;
334
335 int16 corr_gain = 0;
336
337 st = (Decoder_State *) spd_state;
338
339 /* mode verification */
340 if (mode < 0 || mode >= NUM_OF_MODES)
341 {
342 return (-1);
343 }
344 nb_bits = AMR_WB_COMPRESSED[mode];
345
346 *frame_length = AMR_WB_PCM_FRAME;
347
348 /* find the new DTX state SPEECH OR DTX */
349 newDTXState = rx_amr_wb_dtx_handler(&(st->dtx_decSt), frame_type);
350
351
352 if (newDTXState != SPEECH)
353 {
354 dtx_dec_amr_wb(&(st->dtx_decSt), exc2, newDTXState, isf, &prms);
355 }
356 /* SPEECH action state machine */
357
358 if ((frame_type == RX_SPEECH_BAD) ||
359 (frame_type == RX_SPEECH_PROBABLY_DEGRADED))
360 {
361 /* bfi for all index, bits are not usable */
362 bfi = 1;
363 unusable_frame = 0;
364 }
365 else if ((frame_type == RX_NO_DATA) ||
366 (frame_type == RX_SPEECH_LOST))
367 {
368 /* bfi only for lsf, gains and pitch period */
369 bfi = 1;
370 unusable_frame = 1;
371 }
372 else
373 {
374 bfi = 0;
375 unusable_frame = 0;
376 }
377
378 if (bfi != 0)
379 {
380 st->state += 1;
381
382 if (st->state > 6)
383 {
384 st->state = 6;
385 }
386 }
387 else
388 {
389 st->state >>= 1;
390 }
391
392 /* If this frame is the first speech frame after CNI period,
393 * set the BFH state machine to an appropriate state depending
394 * on whether there was DTX muting before start of speech or not
395 * If there was DTX muting, the first speech frame is muted.
396 * If there was no DTX muting, the first speech frame is not
397 * muted. The BFH state machine starts from state 5, however, to
398 * keep the audible noise resulting from a SID frame which is
399 * erroneously interpreted as a good speech frame as small as
400 * possible (the decoder output in this case is quickly muted)
401 */
402
403 if (st->dtx_decSt.dtxGlobalState == DTX)
404 {
405 st->state = 5;
406 st->prev_bfi = 0;
407 }
408 else if (st->dtx_decSt.dtxGlobalState == DTX_MUTE)
409 {
410 st->state = 5;
411 st->prev_bfi = 1;
412 }
413
414 if (newDTXState == SPEECH)
415 {
416 vad_flag = Serial_parm_1bit(&prms);
417
418 if (bfi == 0)
419 {
420 if (vad_flag == 0)
421 {
422 st->vad_hist = add_int16(st->vad_hist, 1);
423 }
424 else
425 {
426 st->vad_hist = 0;
427 }
428 }
429 }
430 /*
431 * DTX-CNG
432 */
433
434 if (newDTXState != SPEECH) /* CNG mode */
435 {
436 /* increase slightly energy of noise below 200 Hz */
437
438 /* Convert ISFs to the cosine domain */
439 Isf_isp(isf, ispnew, M);
440
441 Isp_Az(ispnew, Aq, M, 1);
442
443 pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
444
445
446 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
447 {
448 j = i_subfr >> 6;
449
450 for (i = 0; i < M; i++)
451 {
452 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
453 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
454 HfIsf[i] = amr_wb_round(L_tmp);
455 }
456
457 synthesis_amr_wb(Aq,
458 &exc2[i_subfr],
459 0,
460 &synth16k[i_subfr *5/4],
461 1,
462 HfIsf,
463 nb_bits,
464 newDTXState,
465 st,
466 bfi,
467 ScratchMem);
468 }
469
470 /* reset speech coder memories */
471 pvDecoder_AmrWb_Reset(st, 0);
472
473 pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
474
475 st->prev_bfi = bfi;
476 st->dtx_decSt.dtxGlobalState = newDTXState;
477
478 return 0;
479 }
480 /*
481 * ACELP
482 */
483
484 /* copy coder memory state into working space (internal memory for DSP) */
485
486 pv_memcpy((void *)old_exc, (void *)st->old_exc, (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
487
488 exc = old_exc + PIT_MAX + L_INTERPOL;
489
490 /* Decode the ISFs */
491
492 if (nb_bits > NBBITS_7k) /* all rates but 6.6 Kbps */
493 {
494 ind[0] = Serial_parm(8, &prms); /* index of 1st ISP subvector */
495 ind[1] = Serial_parm(8, &prms); /* index of 2nd ISP subvector */
496 ind[2] = Serial_parm(6, &prms); /* index of 3rd ISP subvector */
497 ind[3] = Serial_parm(7, &prms); /* index of 4th ISP subvector */
498 ind[4] = Serial_parm(7, &prms); /* index of 5th ISP subvector */
499 ind[5] = Serial_parm(5, &prms); /* index of 6th ISP subvector */
500 ind[6] = Serial_parm(5, &prms); /* index of 7th ISP subvector */
501
502 Dpisf_2s_46b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
503 }
504 else
505 {
506 ind[0] = Serial_parm(8, &prms);
507 ind[1] = Serial_parm(8, &prms);
508 ind[2] = Serial_parm(14, &prms);
509 ind[3] = ind[2] & 0x007F;
510 ind[2] >>= 7;
511 ind[4] = Serial_parm(6, &prms);
512
513 Dpisf_2s_36b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
514 }
515
516 /* Convert ISFs to the cosine domain */
517
518 Isf_isp(isf, ispnew, M);
519
520 if (st->first_frame != 0)
521 {
522 st->first_frame = 0;
523 pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
524
525 }
526 /* Find the interpolated ISPs and convert to a[] for all subframes */
527 interpolate_isp(st->ispold, ispnew, interpol_frac, Aq);
528
529 /* update ispold[] for the next frame */
530 pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
531
532 /* Check stability on isf : distance between old isf and current isf */
533
534 L_tmp = 0;
535 for (i = 0; i < M - 1; i++)
536 {
537 tmp = sub_int16(isf[i], st->isfold[i]);
538 L_tmp = mac_16by16_to_int32(L_tmp, tmp, tmp);
539 }
540 tmp = extract_h(shl_int32(L_tmp, 8));
541 tmp = mult_int16(tmp, 26214); /* tmp = L_tmp*0.8/256 */
542
543 tmp = 20480 - tmp; /* 1.25 - tmp */
544 stab_fac = shl_int16(tmp, 1); /* Q14 -> Q15 with saturation */
545
546 if (stab_fac < 0)
547 {
548 stab_fac = 0;
549 }
550 pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
551
552 pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
553
554 /*
555 * Loop for every subframe in the analysis frame
556 *
557 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR
558 * times
559 * - decode the pitch delay and filter mode
560 * - decode algebraic code
561 * - decode pitch and codebook gains
562 * - find voicing factor and tilt of code for next subframe.
563 * - find the excitation and compute synthesis speech
564 */
565
566 p_Aq = Aq; /* pointer to interpolated LPC parameters */
567
568
569 /*
570 * Sub process next 3 subframes
571 */
572
573
574 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
575 {
576 pit_flag = i_subfr;
577
578
579 if ((i_subfr == 2*L_SUBFR) && (nb_bits > NBBITS_7k))
580 {
581 pit_flag = 0; /* set to 0 for 3rd subframe, <=> is not 6.6 kbps */
582 }
583 /*-------------------------------------------------*
584 * - Decode pitch lag *
585 * Lag indeces received also in case of BFI, *
586 * so that the parameter pointer stays in sync. *
587 *-------------------------------------------------*/
588
589 if (pit_flag == 0)
590 {
591
592 if (nb_bits <= NBBITS_9k)
593 {
594 index = Serial_parm(8, &prms);
595
596 if (index < (PIT_FR1_8b - PIT_MIN) * 2)
597 {
598 T0 = PIT_MIN + (index >> 1);
599 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 1));
600 T0_frac = shl_int16(T0_frac, 1);
601 }
602 else
603 {
604 T0 = add_int16(index, PIT_FR1_8b - ((PIT_FR1_8b - PIT_MIN) * 2));
605 T0_frac = 0;
606 }
607 }
608 else
609 {
610 index = Serial_parm(9, &prms);
611
612 if (index < (PIT_FR2 - PIT_MIN) * 4)
613 {
614 T0 = PIT_MIN + (index >> 2);
615 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 2));
616 }
617 else if (index < (((PIT_FR2 - PIT_MIN) << 2) + ((PIT_FR1_9b - PIT_FR2) << 1)))
618 {
619 index -= (PIT_FR2 - PIT_MIN) << 2;
620 T0 = PIT_FR2 + (index >> 1);
621 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_FR2), 1));
622 T0_frac = shl_int16(T0_frac, 1);
623 }
624 else
625 {
626 T0 = add_int16(index, (PIT_FR1_9b - ((PIT_FR2 - PIT_MIN) * 4) - ((PIT_FR1_9b - PIT_FR2) * 2)));
627 T0_frac = 0;
628 }
629 }
630
631 /* find T0_min and T0_max for subframe 2 and 4 */
632
633 T0_min = T0 - 8;
634
635 if (T0_min < PIT_MIN)
636 {
637 T0_min = PIT_MIN;
638 }
639 T0_max = T0_min + 15;
640
641 if (T0_max > PIT_MAX)
642 {
643 T0_max = PIT_MAX;
644 T0_min = PIT_MAX - 15;
645 }
646 }
647 else
648 { /* if subframe 2 or 4 */
649
650 if (nb_bits <= NBBITS_9k)
651 {
652 index = Serial_parm(5, &prms);
653
654 T0 = T0_min + (index >> 1);
655 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 1));
656 T0_frac = shl_int16(T0_frac, 1);
657 }
658 else
659 {
660 index = Serial_parm(6, &prms);
661
662 T0 = T0_min + (index >> 2);
663 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 2));
664 }
665 }
666
667 /* check BFI after pitch lag decoding */
668
669 if (bfi != 0) /* if frame erasure */
670 {
671 lagconceal(&(st->dec_gain[17]), st->lag_hist, &T0, &(st->old_T0), &(st->seed3), unusable_frame);
672 T0_frac = 0;
673 }
674 /*
675 * Find the pitch gain, the interpolation filter
676 * and the adaptive codebook vector.
677 */
678
679 Pred_lt4(&exc[i_subfr], T0, T0_frac, L_SUBFR + 1);
680
681
682 if (unusable_frame)
683 {
684 select = 1;
685 }
686 else
687 {
688
689 if (nb_bits <= NBBITS_9k)
690 {
691 select = 0;
692 }
693 else
694 {
695 select = Serial_parm_1bit(&prms);
696 }
697 }
698
699
700 if (select == 0)
701 {
702 /* find pitch excitation with lp filter */
703 for (i = 0; i < L_SUBFR; i++)
704 {
705 L_tmp = ((int32) exc[i-1+i_subfr] + exc[i+1+i_subfr]);
706 L_tmp *= 5898;
707 L_tmp += ((int32) exc[i+i_subfr] * 20972);
708
709 code[i] = amr_wb_round(L_tmp << 1);
710 }
711 pv_memcpy((void *)&exc[i_subfr], (void *)code, L_SUBFR*sizeof(*code));
712
713 }
714 /*
715 * Decode innovative codebook.
716 * Add the fixed-gain pitch contribution to code[].
717 */
718
719 if (unusable_frame != 0)
720 {
721 /* the innovative code doesn't need to be scaled (see Q_gain2) */
722 for (i = 0; i < L_SUBFR; i++)
723 {
724 code[i] = noise_gen_amrwb(&(st->seed)) >> 3;
725 }
726 }
727 else if (nb_bits <= NBBITS_7k)
728 {
729 ind[0] = Serial_parm(12, &prms);
730 dec_acelp_2p_in_64(ind[0], code);
731 }
732 else if (nb_bits <= NBBITS_9k)
733 {
734 for (i = 0; i < 4; i++)
735 {
736 ind[i] = Serial_parm(5, &prms);
737 }
738 dec_acelp_4p_in_64(ind, 20, code);
739 }
740 else if (nb_bits <= NBBITS_12k)
741 {
742 for (i = 0; i < 4; i++)
743 {
744 ind[i] = Serial_parm(9, &prms);
745 }
746 dec_acelp_4p_in_64(ind, 36, code);
747 }
748 else if (nb_bits <= NBBITS_14k)
749 {
750 ind[0] = Serial_parm(13, &prms);
751 ind[1] = Serial_parm(13, &prms);
752 ind[2] = Serial_parm(9, &prms);
753 ind[3] = Serial_parm(9, &prms);
754 dec_acelp_4p_in_64(ind, 44, code);
755 }
756 else if (nb_bits <= NBBITS_16k)
757 {
758 for (i = 0; i < 4; i++)
759 {
760 ind[i] = Serial_parm(13, &prms);
761 }
762 dec_acelp_4p_in_64(ind, 52, code);
763 }
764 else if (nb_bits <= NBBITS_18k)
765 {
766 for (i = 0; i < 4; i++)
767 {
768 ind[i] = Serial_parm(2, &prms);
769 }
770 for (i = 4; i < 8; i++)
771 {
772 ind[i] = Serial_parm(14, &prms);
773 }
774 dec_acelp_4p_in_64(ind, 64, code);
775 }
776 else if (nb_bits <= NBBITS_20k)
777 {
778 ind[0] = Serial_parm(10, &prms);
779 ind[1] = Serial_parm(10, &prms);
780 ind[2] = Serial_parm(2, &prms);
781 ind[3] = Serial_parm(2, &prms);
782 ind[4] = Serial_parm(10, &prms);
783 ind[5] = Serial_parm(10, &prms);
784 ind[6] = Serial_parm(14, &prms);
785 ind[7] = Serial_parm(14, &prms);
786 dec_acelp_4p_in_64(ind, 72, code);
787 }
788 else
789 {
790 for (i = 0; i < 8; i++)
791 {
792 ind[i] = Serial_parm(11, &prms);
793 }
794
795 dec_acelp_4p_in_64(ind, 88, code);
796 }
797
798 preemph_amrwb_dec(code, st->tilt_code, L_SUBFR);
799
800 tmp = T0;
801
802 if (T0_frac > 2)
803 {
804 tmp++;
805 }
806 Pit_shrp(code, tmp, PIT_SHARP, L_SUBFR);
807
808 /*
809 * Decode codebooks gains.
810 */
811
812 if (nb_bits <= NBBITS_9k)
813 {
814 index = Serial_parm(6, &prms); /* codebook gain index */
815
816 dec_gain2_amr_wb(index,
817 6,
818 code,
819 L_SUBFR,
820 &gain_pit,
821 &L_gain_code,
822 bfi,
823 st->prev_bfi,
824 st->state,
825 unusable_frame,
826 st->vad_hist,
827 st->dec_gain);
828 }
829 else
830 {
831 index = Serial_parm(7, &prms); /* codebook gain index */
832
833 dec_gain2_amr_wb(index,
834 7,
835 code,
836 L_SUBFR,
837 &gain_pit,
838 &L_gain_code,
839 bfi,
840 st->prev_bfi,
841 st->state,
842 unusable_frame,
843 st->vad_hist,
844 st->dec_gain);
845 }
846
847 /* find best scaling to perform on excitation (Q_new) */
848
849 tmp = st->Qsubfr[0];
850 for (i = 1; i < 4; i++)
851 {
852 if (st->Qsubfr[i] < tmp)
853 {
854 tmp = st->Qsubfr[i];
855 }
856 }
857
858 /* limit scaling (Q_new) to Q_MAX: see pv_amr_wb_cnst.h and syn_filt_32() */
859
860 if (tmp > Q_MAX)
861 {
862 tmp = Q_MAX;
863 }
864 Q_new = 0;
865 L_tmp = L_gain_code; /* L_gain_code in Q16 */
866
867
868 while ((L_tmp < 0x08000000L) && (Q_new < tmp))
869 {
870 L_tmp <<= 1;
871 Q_new += 1;
872
873 }
874 gain_code = amr_wb_round(L_tmp); /* scaled gain_code with Qnew */
875
876 scale_signal(exc + i_subfr - (PIT_MAX + L_INTERPOL),
877 PIT_MAX + L_INTERPOL + L_SUBFR,
878 (int16)(Q_new - st->Q_old));
879
880 st->Q_old = Q_new;
881
882
883 /*
884 * Update parameters for the next subframe.
885 * - tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
886 */
887
888
889 if (bfi == 0)
890 {
891 /* LTP-Lag history update */
892 for (i = 4; i > 0; i--)
893 {
894 st->lag_hist[i] = st->lag_hist[i - 1];
895 }
896 st->lag_hist[0] = T0;
897
898 st->old_T0 = T0;
899 st->old_T0_frac = 0; /* Remove fraction in case of BFI */
900 }
901 /* find voice factor in Q15 (1=voiced, -1=unvoiced) */
902
903 /*
904 * Scale down by 1/8
905 */
906 for (i = L_SUBFR - 1; i >= 0; i--)
907 {
908 exc2[i] = (exc[i_subfr + i] + (0x0004 * (exc[i_subfr + i] != MAX_16))) >> 3;
909 }
910
911
912 /* post processing of excitation elements */
913
914 if (nb_bits <= NBBITS_9k)
915 {
916 pit_sharp = shl_int16(gain_pit, 1);
917
918 if (pit_sharp > 16384)
919 {
920 for (i = 0; i < L_SUBFR; i++)
921 {
922 tmp = mult_int16(exc2[i], pit_sharp);
923 L_tmp = mul_16by16_to_int32(tmp, gain_pit);
924 L_tmp >>= 1;
925 excp[i] = amr_wb_round(L_tmp);
926 }
927 }
928 }
929 else
930 {
931 pit_sharp = 0;
932 }
933
934 voice_fac = voice_factor(exc2, -3, gain_pit, code, gain_code, L_SUBFR);
935
936 /* tilt of code for next subframe: 0.5=voiced, 0=unvoiced */
937
938 st->tilt_code = (voice_fac >> 2) + 8192;
939
940 /*
941 * - Find the total excitation.
942 * - Find synthesis speech corresponding to exc[].
943 * - Find maximum value of excitation for next scaling
944 */
945
946 pv_memcpy((void *)exc2, (void *)&exc[i_subfr], L_SUBFR*sizeof(*exc2));
947 max = 1;
948
949 for (i = 0; i < L_SUBFR; i++)
950 {
951 L_tmp = mul_16by16_to_int32(code[i], gain_code);
952 L_tmp = shl_int32(L_tmp, 5);
953 L_tmp = mac_16by16_to_int32(L_tmp, exc[i + i_subfr], gain_pit);
954 L_tmp = shl_int32(L_tmp, 1);
955 tmp = amr_wb_round(L_tmp);
956 exc[i + i_subfr] = tmp;
957 tmp = tmp - (tmp < 0);
958 max |= tmp ^(tmp >> 15); /* |= tmp ^sign(tmp) */
959 }
960
961
962 /* tmp = scaling possible according to max value of excitation */
963 tmp = add_int16(norm_s(max), Q_new) - 1;
964
965 st->Qsubfr[3] = st->Qsubfr[2];
966 st->Qsubfr[2] = st->Qsubfr[1];
967 st->Qsubfr[1] = st->Qsubfr[0];
968 st->Qsubfr[0] = tmp;
969
970 /*
971 * phase dispersion to enhance noise in low bit rate
972 */
973
974
975 if (nb_bits <= NBBITS_7k)
976 {
977 j = 0; /* high dispersion for rate <= 7.5 kbit/s */
978 }
979 else if (nb_bits <= NBBITS_9k)
980 {
981 j = 1; /* low dispersion for rate <= 9.6 kbit/s */
982 }
983 else
984 {
985 j = 2; /* no dispersion for rate > 9.6 kbit/s */
986 }
987
988 /* L_gain_code in Q16 */
989
990 phase_dispersion((int16)(L_gain_code >> 16),
991 gain_pit,
992 code,
993 j,
994 st->disp_mem,
995 ScratchMem);
996
997 /*
998 * noise enhancer
999 * - Enhance excitation on noise. (modify gain of code)
1000 * If signal is noisy and LPC filter is stable, move gain
1001 * of code 1.5 dB toward gain of code threshold.
1002 * This decrease by 3 dB noise energy variation.
1003 */
1004
1005 tmp = 16384 - (voice_fac >> 1); /* 1=unvoiced, 0=voiced */
1006 fac = mult_int16(stab_fac, tmp);
1007
1008 L_tmp = L_gain_code;
1009
1010 if (L_tmp < st->L_gc_thres)
1011 {
1012 L_tmp += fxp_mul32_by_16b(L_gain_code, 6226) << 1;
1013
1014 if (L_tmp > st->L_gc_thres)
1015 {
1016 L_tmp = st->L_gc_thres;
1017 }
1018 }
1019 else
1020 {
1021 L_tmp = fxp_mul32_by_16b(L_gain_code, 27536) << 1;
1022
1023 if (L_tmp < st->L_gc_thres)
1024 {
1025 L_tmp = st->L_gc_thres;
1026 }
1027 }
1028 st->L_gc_thres = L_tmp;
1029
1030 L_gain_code = fxp_mul32_by_16b(L_gain_code, (32767 - fac)) << 1;
1031
1032
1033 L_gain_code = add_int32(L_gain_code, fxp_mul32_by_16b(L_tmp, fac) << 1);
1034
1035 /*
1036 * pitch enhancer
1037 * - Enhance excitation on voice. (HP filtering of code)
1038 * On voiced signal, filtering of code by a smooth fir HP
1039 * filter to decrease energy of code in low frequency.
1040 */
1041
1042 tmp = (voice_fac >> 3) + 4096;/* 0.25=voiced, 0=unvoiced */
1043
1044 /* build excitation */
1045
1046 gain_code = amr_wb_round(shl_int32(L_gain_code, Q_new));
1047
1048 L_tmp = (int32)(code[0] << 16);
1049 L_tmp = msu_16by16_from_int32(L_tmp, code[1], tmp);
1050 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1051 L_tmp = shl_int32(L_tmp, 5);
1052 L_tmp = mac_16by16_to_int32(L_tmp, exc2[0], gain_pit);
1053 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1054 exc2[0] = amr_wb_round(L_tmp);
1055
1056
1057 for (i = 1; i < L_SUBFR - 1; i++)
1058 {
1059 L_tmp = (int32)(code[i] << 16);
1060 L_tmp = msu_16by16_from_int32(L_tmp, (code[i + 1] + code[i - 1]), tmp);
1061 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1062 L_tmp = shl_int32(L_tmp, 5);
1063 L_tmp = mac_16by16_to_int32(L_tmp, exc2[i], gain_pit);
1064 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1065 exc2[i] = amr_wb_round(L_tmp);
1066 }
1067
1068 L_tmp = (int32)(code[L_SUBFR - 1] << 16);
1069 L_tmp = msu_16by16_from_int32(L_tmp, code[L_SUBFR - 2], tmp);
1070 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1071 L_tmp = shl_int32(L_tmp, 5);
1072 L_tmp = mac_16by16_to_int32(L_tmp, exc2[L_SUBFR - 1], gain_pit);
1073 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1074 exc2[L_SUBFR - 1] = amr_wb_round(L_tmp);
1075
1076
1077
1078 if (nb_bits <= NBBITS_9k)
1079 {
1080 if (pit_sharp > 16384)
1081 {
1082 for (i = 0; i < L_SUBFR; i++)
1083 {
1084 excp[i] = add_int16(excp[i], exc2[i]);
1085 }
1086 agc2_amr_wb(exc2, excp, L_SUBFR);
1087 pv_memcpy((void *)exc2, (void *)excp, L_SUBFR*sizeof(*exc2));
1088
1089 }
1090 }
1091 if (nb_bits <= NBBITS_7k)
1092 {
1093 j = i_subfr >> 6;
1094 for (i = 0; i < M; i++)
1095 {
1096 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
1097 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
1098 HfIsf[i] = amr_wb_round(L_tmp);
1099 }
1100 }
1101 else
1102 {
1103 pv_memset((void *)st->mem_syn_hf,
1104 0,
1105 (M16k - M)*sizeof(*st->mem_syn_hf));
1106 }
1107
1108 if (nb_bits >= NBBITS_24k)
1109 {
1110 corr_gain = Serial_parm(4, &prms);
1111 }
1112 else
1113 {
1114 corr_gain = 0;
1115 }
1116
1117 synthesis_amr_wb(p_Aq,
1118 exc2,
1119 Q_new,
1120 &synth16k[i_subfr + (i_subfr>>2)],
1121 corr_gain,
1122 HfIsf,
1123 nb_bits,
1124 newDTXState,
1125 st,
1126 bfi,
1127 ScratchMem);
1128
1129 p_Aq += (M + 1); /* interpolated LPC parameters for next subframe */
1130 }
1131
1132 /*
1133 * Update signal for next frame.
1134 * -> save past of exc[]
1135 * -> save pitch parameters
1136 */
1137
1138 pv_memcpy((void *)st->old_exc,
1139 (void *)&old_exc[L_FRAME],
1140 (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
1141
1142 scale_signal(exc, L_FRAME, (int16)(-Q_new));
1143
1144 dtx_dec_amr_wb_activity_update(&(st->dtx_decSt), isf, exc);
1145
1146 st->dtx_decSt.dtxGlobalState = newDTXState;
1147
1148 st->prev_bfi = bfi;
1149
1150 return 0;
1151 }
1152
1153