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.073
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
24
25 (C) 2004, 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 Pathname: ./audio/gsm-amr/c/src/dtx_dec.c
35 Functions:
36 dtx_dec_reset
37 dtx_dec
38 dtx_dec_activity_update
39 rx_dtx_handler
40
41 ------------------------------------------------------------------------------
42 MODULE DESCRIPTION
43
44 These modules decode the comfort noise when in DTX.
45
46 ------------------------------------------------------------------------------
47 */
48
49
50 /*----------------------------------------------------------------------------
51 ; INCLUDES
52 ----------------------------------------------------------------------------*/
53 #include <string.h>
54
55 #include "dtx_dec.h"
56 #include "typedef.h"
57 #include "basic_op.h"
58 #include "copy.h"
59 #include "set_zero.h"
60 #include "mode.h"
61 #include "log2.h"
62 #include "lsp_az.h"
63 #include "pow2.h"
64 #include "a_refl.h"
65 #include "b_cn_cod.h"
66 #include "syn_filt.h"
67 #include "lsp_lsf.h"
68 #include "reorder.h"
69 #include "lsp_tab.h"
70
71 /*----------------------------------------------------------------------------
72 ; MACROS
73 ; Define module specific macros here
74 ----------------------------------------------------------------------------*/
75
76
77 /*----------------------------------------------------------------------------
78 ; DEFINES
79 ; Include all pre-processor statements here. Include conditional
80 ; compile variables also.
81 ----------------------------------------------------------------------------*/
82 #define PN_INITIAL_SEED 0x70816958L /* Pseudo noise generator seed value */
83
84 /*----------------------------------------------------------------------------
85 ; LOCAL FUNCTION DEFINITIONS
86 ; Function Prototype declaration
87 ----------------------------------------------------------------------------*/
88
89
90 /*----------------------------------------------------------------------------
91 ; LOCAL VARIABLE DEFINITIONS
92 ; Variable declaration - defined here and used outside this module
93 ----------------------------------------------------------------------------*/
94
95 /***************************************************
96 * Scaling factors for the lsp variability operation *
97 ***************************************************/
98 static const Word16 lsf_hist_mean_scale[M] =
99 {
100 20000,
101 20000,
102 20000,
103 20000,
104 20000,
105 18000,
106 16384,
107 8192,
108 0,
109 0
110 };
111
112 /*************************************************
113 * level adjustment for different modes Q11 *
114 *************************************************/
115 static const Word16 dtx_log_en_adjust[9] =
116 {
117 -1023, /* MR475 */
118 -878, /* MR515 */
119 -732, /* MR59 */
120 -586, /* MR67 */
121 -440, /* MR74 */
122 -294, /* MR795 */
123 -148, /* MR102 */
124 0, /* MR122 */
125 0, /* MRDTX */
126 };
127
128
129 /*
130 ------------------------------------------------------------------------------
131 FUNCTION NAME: dtx_dec_reset
132 ------------------------------------------------------------------------------
133 INPUT AND OUTPUT DEFINITIONS
134
135 Inputs:
136 st = pointer to a structure of type dtx_decState
137
138 Outputs:
139 Structure pointed to by st is initialized to a set of initial values.
140
141 Returns:
142 return_value = 0 if memory was successfully initialized,
143 otherwise returns -1 (int)
144
145 Global Variables Used:
146 None.
147
148 Local Variables Needed:
149 None.
150
151 ------------------------------------------------------------------------------
152 FUNCTION DESCRIPTION
153
154 Reset of state memory for dtx_dec.
155
156 ------------------------------------------------------------------------------
157 REQUIREMENTS
158
159 None.
160
161 ------------------------------------------------------------------------------
162 REFERENCES
163
164 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
165
166 ------------------------------------------------------------------------------
167 PSEUDO-CODE
168
169 int dtx_dec_reset (dtx_decState *st)
170 {
171 int i;
172
173 if (st == (dtx_decState *) NULL){
174 fprintf(stderr, "dtx_dec_reset: invalid parameter\n");
175 return -1;
176 }
177
178 st->since_last_sid = 0;
179 st->true_sid_period_inv = (1 << 13);
180
181 st->log_en = 3500;
182 st->old_log_en = 3500;
183 // low level noise for better performance in DTX handover cases
184
185 st->L_pn_seed_rx = PN_INITIAL_SEED;
186
187 // Initialize state->lsp [] and state->lsp_old []
188 Copy(lsp_init_data, &st->lsp[0], M);
189 Copy(lsp_init_data, &st->lsp_old[0], M);
190
191 st->lsf_hist_ptr = 0;
192 st->log_pg_mean = 0;
193 st->log_en_hist_ptr = 0;
194
195 // initialize decoder lsf history
196 Copy(mean_lsf, &st->lsf_hist[0], M);
197
198 for (i = 1; i < DTX_HIST_SIZE; i++)
199 {
200 Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
201 }
202 Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE);
203
204 // initialize decoder log frame energy
205 for (i = 0; i < DTX_HIST_SIZE; i++)
206 {
207 st->log_en_hist[i] = st->log_en;
208 }
209
210 st->log_en_adjust = 0;
211
212 st->dtxHangoverCount = DTX_HANG_CONST;
213 st->decAnaElapsedCount = 32767;
214
215 st->sid_frame = 0;
216 st->valid_data = 0;
217 st->dtxHangoverAdded = 0;
218
219 st->dtxGlobalState = DTX;
220 st->data_updated = 0;
221 return 0;
222 }
223
224 ------------------------------------------------------------------------------
225 RESOURCES USED [optional]
226
227 When the code is written for a specific target processor the
228 the resources used should be documented below.
229
230 HEAP MEMORY USED: x bytes
231
232 STACK MEMORY USED: x bytes
233
234 CLOCK CYCLES: (cycle count equation for this function) + (variable
235 used to represent cycle count for each subroutine
236 called)
237 where: (cycle count variable) = cycle count for [subroutine
238 name]
239
240 ------------------------------------------------------------------------------
241 CAUTION [optional]
242 [State any special notes, constraints or cautions for users of this function]
243
244 ------------------------------------------------------------------------------
245 */
246
dtx_dec_reset(dtx_decState * st)247 Word16 dtx_dec_reset(dtx_decState *st)
248 {
249 Word16 i;
250
251 if (st == (dtx_decState *) NULL)
252 {
253 /* fprint(stderr, "dtx_dec_reset: invalid parameter\n"); */
254 return(-1);
255 }
256
257 st->since_last_sid = 0;
258 st->true_sid_period_inv = (1 << 13);
259
260 st->log_en = 3500;
261 st->old_log_en = 3500;
262 /* low level noise for better performance in DTX handover cases*/
263
264 st->L_pn_seed_rx = PN_INITIAL_SEED;
265
266 /* Initialize state->lsp [] */
267 st->lsp[0] = 30000;
268 st->lsp[1] = 26000;
269 st->lsp[2] = 21000;
270 st->lsp[3] = 15000;
271 st->lsp[4] = 8000;
272 st->lsp[5] = 0;
273 st->lsp[6] = -8000;
274 st->lsp[7] = -15000;
275 st->lsp[8] = -21000;
276 st->lsp[9] = -26000;
277
278 /* Initialize state->lsp_old [] */
279 st->lsp_old[0] = 30000;
280 st->lsp_old[1] = 26000;
281 st->lsp_old[2] = 21000;
282 st->lsp_old[3] = 15000;
283 st->lsp_old[4] = 8000;
284 st->lsp_old[5] = 0;
285 st->lsp_old[6] = -8000;
286 st->lsp_old[7] = -15000;
287 st->lsp_old[8] = -21000;
288 st->lsp_old[9] = -26000;
289
290 st->lsf_hist_ptr = 0;
291 st->log_pg_mean = 0;
292 st->log_en_hist_ptr = 0;
293
294 /* initialize decoder lsf history */
295 st->lsf_hist[0] = 1384;
296 st->lsf_hist[1] = 2077;
297 st->lsf_hist[2] = 3420;
298 st->lsf_hist[3] = 5108;
299 st->lsf_hist[4] = 6742;
300 st->lsf_hist[5] = 8122;
301 st->lsf_hist[6] = 9863;
302 st->lsf_hist[7] = 11092;
303 st->lsf_hist[8] = 12714;
304 st->lsf_hist[9] = 13701;
305
306 for (i = 1; i < DTX_HIST_SIZE; i++)
307 {
308 Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
309 }
310 memset(st->lsf_hist_mean, 0, sizeof(Word16)*M*DTX_HIST_SIZE);
311
312 /* initialize decoder log frame energy */
313 for (i = 0; i < DTX_HIST_SIZE; i++)
314 {
315 st->log_en_hist[i] = st->log_en;
316 }
317
318 st->log_en_adjust = 0;
319
320 st->dtxHangoverCount = DTX_HANG_CONST;
321 st->decAnaElapsedCount = 32767;
322
323 st->sid_frame = 0;
324 st->valid_data = 0;
325 st->dtxHangoverAdded = 0;
326
327 st->dtxGlobalState = DTX;
328 st->data_updated = 0;
329
330 return(0);
331 }
332
333 /****************************************************************************/
334
335 /*
336 ------------------------------------------------------------------------------
337 FUNCTION NAME: dtx_dec
338 ------------------------------------------------------------------------------
339 INPUT AND OUTPUT DEFINITIONS
340
341 Inputs:
342 st = pointer to a structure of type dtx_decState
343 mem_syn = AMR decoder state
344 lsfState = decoder lsf states
345 predState = prediction states
346 averState = CB gain average states
347 new_state = new DTX state
348 mode = AMR mode
349 parm = Vector of synthesis parameters
350
351 Outputs:
352 st points to an updated structure of type dtx_decState
353 mem_syn = AMR decoder state
354 lsfState = decoder lsf states
355 predState = prediction states
356 averState = CB gain average states
357 synth = synthesised speech
358 A_t = decoded LP filter in 4 subframes
359
360 Returns:
361 return_value = 0 (int)
362
363 Global Variables Used:
364 None.
365
366 Local Variables Needed:
367 None.
368
369 ------------------------------------------------------------------------------
370 FUNCTION DESCRIPTION
371
372 Decode comfort noise when in DTX.
373
374 ------------------------------------------------------------------------------
375 REQUIREMENTS
376
377 None
378
379 ------------------------------------------------------------------------------
380 REFERENCES
381
382 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
383
384 ------------------------------------------------------------------------------
385 PSEUDO-CODE
386
387 int dtx_dec(
388 dtx_decState *st, // i/o : State struct
389 Word16 mem_syn[], // i/o : AMR decoder state
390 D_plsfState* lsfState, // i/o : decoder lsf states
391 gc_predState* predState, // i/o : prediction states
392 Cb_gain_averageState* averState, // i/o : CB gain average states
393 enum DTXStateType new_state, // i : new DTX state
394 enum Mode mode, // i : AMR mode
395 Word16 parm[], // i : Vector of synthesis parameters
396 Word16 synth[], // o : synthesised speech
397 Word16 A_t[] // o : decoded LP filter in 4 subframes
398 )
399 {
400 Word16 log_en_index;
401 Word16 i, j;
402 Word16 int_fac;
403 Word32 L_log_en_int;
404 Word16 lsp_int[M];
405 Word16 log_en_int_e;
406 Word16 log_en_int_m;
407 Word16 level;
408 Word16 acoeff[M + 1];
409 Word16 refl[M];
410 Word16 pred_err;
411 Word16 ex[L_SUBFR];
412 Word16 ma_pred_init;
413 Word16 log_pg_e, log_pg_m;
414 Word16 log_pg;
415 Flag negative;
416 Word16 lsf_mean;
417 Word32 L_lsf_mean;
418 Word16 lsf_variab_index;
419 Word16 lsf_variab_factor;
420 Word16 lsf_int[M];
421 Word16 lsf_int_variab[M];
422 Word16 lsp_int_variab[M];
423 Word16 acoeff_variab[M + 1];
424
425 Word16 lsf[M];
426 Word32 L_lsf[M];
427 Word16 ptr;
428 Word16 tmp_int_length;
429
430
431 // This function is called if synthesis state is not SPEECH
432 // the globally passed inputs to this function are
433 // st->sid_frame
434 // st->valid_data
435 // st->dtxHangoverAdded
436 // new_state (SPEECH, DTX, DTX_MUTE)
437
438 if ((st->dtxHangoverAdded != 0) &&
439 (st->sid_frame != 0))
440 {
441 // sid_first after dtx hangover period
442 // or sid_upd after dtxhangover
443
444 // set log_en_adjust to correct value
445 st->log_en_adjust = dtx_log_en_adjust[mode];
446
447 ptr = add(st->lsf_hist_ptr, M);
448 if (sub(ptr, 80) == 0)
449 {
450 ptr = 0;
451 }
452 Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M);
453
454 ptr = add(st->log_en_hist_ptr,1);
455 if (sub(ptr, DTX_HIST_SIZE) == 0)
456 {
457 ptr = 0;
458 }
459 st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; // Q11
460
461 // compute mean log energy and lsp
462 // from decoded signal (SID_FIRST)
463 st->log_en = 0;
464 for (i = 0; i < M; i++)
465 {
466 L_lsf[i] = 0;
467 }
468
469 // average energy and lsp
470 for (i = 0; i < DTX_HIST_SIZE; i++)
471 {
472 st->log_en = add(st->log_en,
473 shr(st->log_en_hist[i],3));
474 for (j = 0; j < M; j++)
475 {
476 L_lsf[j] = L_add(L_lsf[j],
477 L_deposit_l(st->lsf_hist[i * M + j]));
478 }
479 }
480
481 for (j = 0; j < M; j++)
482 {
483 lsf[j] = extract_l(L_shr(L_lsf[j],3)); // divide by 8
484 }
485
486 Lsf_lsp(lsf, st->lsp, M);
487
488 // make log_en speech coder mode independent
489 // added again later before synthesis
490 st->log_en = sub(st->log_en, st->log_en_adjust);
491
492 // compute lsf variability vector
493 Copy(st->lsf_hist, st->lsf_hist_mean, 80);
494
495 for (i = 0; i < M; i++)
496 {
497 L_lsf_mean = 0;
498 // compute mean lsf
499 for (j = 0; j < 8; j++)
500 {
501 L_lsf_mean = L_add(L_lsf_mean,
502 L_deposit_l(st->lsf_hist_mean[i+j*M]));
503 }
504
505 lsf_mean = extract_l(L_shr(L_lsf_mean, 3));
506 // subtract mean and limit to within reasonable limits
507 // moreover the upper lsf's are attenuated
508 for (j = 0; j < 8; j++)
509 {
510 // subtract mean
511 st->lsf_hist_mean[i+j*M] =
512 sub(st->lsf_hist_mean[i+j*M], lsf_mean);
513
514 // attenuate deviation from mean, especially for upper lsf's
515 st->lsf_hist_mean[i+j*M] =
516 mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i]);
517
518 // limit the deviation
519 if (st->lsf_hist_mean[i+j*M] < 0)
520 {
521 negative = 1;
522 }
523 else
524 {
525 negative = 0;
526 }
527 st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
528
529 // apply soft limit
530 if (sub(st->lsf_hist_mean[i+j*M], 655) > 0)
531 {
532 st->lsf_hist_mean[i+j*M] =
533 add(655, shr(sub(st->lsf_hist_mean[i+j*M], 655), 2));
534 }
535
536 // apply hard limit
537 if (sub(st->lsf_hist_mean[i+j*M], 1310) > 0)
538 {
539 st->lsf_hist_mean[i+j*M] = 1310;
540 }
541 if (negative != 0)
542 {
543 st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
544 }
545
546 }
547 }
548 }
549
550 if (st->sid_frame != 0 )
551 {
552 // Set old SID parameters, always shift
553 // even if there is no new valid_data
554 Copy(st->lsp, st->lsp_old, M);
555 st->old_log_en = st->log_en;
556
557 if (st->valid_data != 0 ) // new data available (no CRC)
558 {
559 // Compute interpolation factor, since the division only works
560 // for values of since_last_sid < 32 we have to limit the
561 // interpolation to 32 frames
562 tmp_int_length = st->since_last_sid;
563 st->since_last_sid = 0;
564
565 if (sub(tmp_int_length, 32) > 0)
566 {
567 tmp_int_length = 32;
568 }
569 if (sub(tmp_int_length, 2) >= 0)
570 {
571 st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
572 }
573 else
574 {
575 st->true_sid_period_inv = 1 << 14; // 0.5 it Q15
576 }
577
578 Init_D_plsf_3(lsfState, parm[0]); // temporay initialization
579 D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp);
580 Set_zero(lsfState->past_r_q, M); // reset for next speech frame
581
582 log_en_index = parm[4];
583 // Q11 and divide by 4
584 st->log_en = shl(log_en_index, (11 - 2));
585
586 // Subtract 2.5 in Q11
587 st->log_en = sub(st->log_en, (2560 * 2));
588
589 // Index 0 is reserved for silence
590 if (log_en_index == 0)
591 {
592 st->log_en = MIN_16;
593 }
594
595 // no interpolation at startup after coder reset
596 // or when SID_UPD has been received right after SPEECH
597 if ((st->data_updated == 0) ||
598 (sub(st->dtxGlobalState, SPEECH) == 0)
599 )
600 {
601 Copy(st->lsp, st->lsp_old, M);
602 st->old_log_en = st->log_en;
603 }
604 } // endif valid_data
605
606 // initialize gain predictor memory of other modes
607 ma_pred_init = sub(shr(st->log_en,1), 9000);
608 if (ma_pred_init > 0)
609 {
610 ma_pred_init = 0;
611 }
612 if (sub(ma_pred_init, -14436) < 0)
613 {
614 ma_pred_init = -14436;
615 }
616
617 predState->past_qua_en[0] = ma_pred_init;
618 predState->past_qua_en[1] = ma_pred_init;
619 predState->past_qua_en[2] = ma_pred_init;
620 predState->past_qua_en[3] = ma_pred_init;
621
622 // past_qua_en for other modes than MR122
623 ma_pred_init = mult(5443, ma_pred_init);
624 // scale down by factor 20*log10(2) in Q15
625 predState->past_qua_en_MR122[0] = ma_pred_init;
626 predState->past_qua_en_MR122[1] = ma_pred_init;
627 predState->past_qua_en_MR122[2] = ma_pred_init;
628 predState->past_qua_en_MR122[3] = ma_pred_init;
629 } // endif sid_frame
630
631 // CN generation
632 // recompute level adjustment factor Q11
633 // st->log_en_adjust = 0.9*st->log_en_adjust +
634 // 0.1*dtx_log_en_adjust[mode]);
635 st->log_en_adjust = add(mult(st->log_en_adjust, 29491),
636 shr(mult(shl(dtx_log_en_adjust[mode],5),3277),5));
637
638 // Interpolate SID info
639 int_fac = shl(add(1,st->since_last_sid), 10); // Q10
640 int_fac = mult(int_fac, st->true_sid_period_inv); // Q10 * Q15 -> Q10
641
642 // Maximize to 1.0 in Q10
643 if (sub(int_fac, 1024) > 0)
644 {
645 int_fac = 1024;
646 }
647 int_fac = shl(int_fac, 4); // Q10 -> Q14
648
649 L_log_en_int = L_mult(int_fac, st->log_en); // Q14 * Q11->Q26
650 for(i = 0; i < M; i++)
651 {
652 lsp_int[i] = mult(int_fac, st->lsp[i]);// Q14 * Q15 -> Q14
653 }
654
655 int_fac = sub(16384, int_fac); // 1-k in Q14
656
657 // (Q14 * Q11 -> Q26) + Q26 -> Q26
658 L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en);
659 for(i = 0; i < M; i++)
660 {
661 // Q14 + (Q14 * Q15 -> Q14) -> Q14
662 lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i]));
663 lsp_int[i] = shl(lsp_int[i], 1); // Q14 -> Q15
664 }
665
666 // compute the amount of lsf variability
667 lsf_variab_factor = sub(st->log_pg_mean,2457); // -0.6 in Q12
668 // *0.3 Q12*Q15 -> Q12
669 lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830));
670
671 // limit to values between 0..1 in Q12
672 if (sub(lsf_variab_factor, 4096) > 0)
673 {
674 lsf_variab_factor = 4096;
675 }
676 if (lsf_variab_factor < 0)
677 {
678 lsf_variab_factor = 0;
679 }
680 lsf_variab_factor = shl(lsf_variab_factor, 3); // -> Q15
681
682 // get index of vector to do variability with
683 lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
684
685 // convert to lsf
686 Lsp_lsf(lsp_int, lsf_int, M);
687
688 // apply lsf variability
689 Copy(lsf_int, lsf_int_variab, M);
690 for(i = 0; i < M; i++)
691 {
692 lsf_int_variab[i] = add(lsf_int_variab[i],
693 mult(lsf_variab_factor,
694 st->lsf_hist_mean[i+lsf_variab_index*M]));
695 }
696
697 // make sure that LSP's are ordered
698 Reorder_lsf(lsf_int, LSF_GAP, M);
699 Reorder_lsf(lsf_int_variab, LSF_GAP, M);
700
701 // copy lsf to speech decoders lsf state
702 Copy(lsf_int, lsfState->past_lsf_q, M);
703
704 // convert to lsp
705 Lsf_lsp(lsf_int, lsp_int, M);
706 Lsf_lsp(lsf_int_variab, lsp_int_variab, M);
707
708 // Compute acoeffs Q12 acoeff is used for level
709 // normalization and postfilter, acoeff_variab is
710 // used for synthesis filter
711 // by doing this we make sure that the level
712 // in high frequenncies does not jump up and down
713
714 Lsp_Az(lsp_int, acoeff);
715 Lsp_Az(lsp_int_variab, acoeff_variab);
716
717 // For use in postfilter
718 Copy(acoeff, &A_t[0], M + 1);
719 Copy(acoeff, &A_t[M + 1], M + 1);
720 Copy(acoeff, &A_t[2 * (M + 1)], M + 1);
721 Copy(acoeff, &A_t[3 * (M + 1)], M + 1);
722
723 // Compute reflection coefficients Q15
724 A_Refl(&acoeff[1], refl);
725
726 // Compute prediction error in Q15
727 pred_err = MAX_16; // 0.99997 in Q15
728 for (i = 0; i < M; i++)
729 {
730 pred_err = mult(pred_err, sub(MAX_16, mult(refl[i], refl[i])));
731 }
732
733 // compute logarithm of prediction gain
734 Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m);
735
736 // convert exponent and mantissa to Word16 Q12
737 log_pg = shl(sub(log_pg_e,15), 12); // Q12
738 log_pg = shr(sub(0,add(log_pg, shr(log_pg_m, 15-12))), 1);
739 st->log_pg_mean = add(mult(29491,st->log_pg_mean),
740 mult(3277, log_pg));
741
742 // Compute interpolated log energy
743 L_log_en_int = L_shr(L_log_en_int, 10); // Q26 -> Q16
744
745 // Add 4 in Q16
746 L_log_en_int = L_add(L_log_en_int, 4 * 65536L);
747
748 // subtract prediction gain
749 L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4));
750
751 // adjust level to speech coder mode
752 L_log_en_int = L_add(L_log_en_int,
753 L_shl(L_deposit_l(st->log_en_adjust), 5));
754
755 log_en_int_e = extract_h(L_log_en_int);
756 log_en_int_m = extract_l(L_shr(L_sub(L_log_en_int,
757 L_deposit_h(log_en_int_e)), 1));
758 level = extract_l(Pow2(log_en_int_e, log_en_int_m)); // Q4
759
760 for (i = 0; i < 4; i++)
761 {
762 // Compute innovation vector
763 build_CN_code(&st->L_pn_seed_rx, ex);
764 for (j = 0; j < L_SUBFR; j++)
765 {
766 ex[j] = mult(level, ex[j]);
767 }
768 // Synthesize
769 Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
770 mem_syn, 1);
771
772 } // next i
773
774 // reset codebook averaging variables
775 averState->hangVar = 20;
776 averState->hangCount = 0;
777
778 if (sub(new_state, DTX_MUTE) == 0)
779 {
780 // mute comfort noise as it has been quite a long time since
781 * last SID update was performed
782
783 tmp_int_length = st->since_last_sid;
784 if (sub(tmp_int_length, 32) > 0)
785 {
786 tmp_int_length = 32;
787 }
788
789 // safety guard against division by zero
790 if(tmp_int_length <= 0) {
791 tmp_int_length = 8;
792 }
793
794 st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
795
796 st->since_last_sid = 0;
797 Copy(st->lsp, st->lsp_old, M);
798 st->old_log_en = st->log_en;
799 // subtract 1/8 in Q11 i.e -6/8 dB
800 st->log_en = sub(st->log_en, 256);
801 }
802
803 // reset interpolation length timer
804 // if data has been updated.
805 if ((st->sid_frame != 0) &&
806 ((st->valid_data != 0) ||
807 ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
808 {
809 st->since_last_sid = 0;
810 st->data_updated = 1;
811 }
812
813 return 0;
814 }
815
816
817 ------------------------------------------------------------------------------
818 RESOURCES USED [optional]
819
820 When the code is written for a specific target processor the
821 the resources used should be documented below.
822
823 HEAP MEMORY USED: x bytes
824
825 STACK MEMORY USED: x bytes
826
827 CLOCK CYCLES: (cycle count equation for this function) + (variable
828 used to represent cycle count for each subroutine
829 called)
830 where: (cycle count variable) = cycle count for [subroutine
831 name]
832
833 ------------------------------------------------------------------------------
834 CAUTION [optional]
835 [State any special notes, constraints or cautions for users of this function]
836
837 ------------------------------------------------------------------------------
838 */
839
dtx_dec(dtx_decState * st,Word16 mem_syn[],D_plsfState * lsfState,gc_predState * predState,Cb_gain_averageState * averState,enum DTXStateType new_state,enum Mode mode,Word16 parm[],Word16 synth[],Word16 A_t[],Flag * pOverflow)840 void dtx_dec(
841 dtx_decState *st, /* i/o : State struct */
842 Word16 mem_syn[], /* i/o : AMR decoder state */
843 D_plsfState* lsfState, /* i/o : decoder lsf states */
844 gc_predState* predState, /* i/o : prediction states */
845 Cb_gain_averageState* averState, /* i/o : CB gain average states */
846 enum DTXStateType new_state, /* i : new DTX state */
847 enum Mode mode, /* i : AMR mode */
848 Word16 parm[], /* i : Vector of synthesis parameters */
849 Word16 synth[], /* o : synthesised speech */
850 Word16 A_t[], /* o : decoded LP filter in 4 subframes*/
851 Flag *pOverflow
852 )
853 {
854 Word16 log_en_index;
855 Word16 i;
856 Word16 j;
857 Word16 int_fac;
858 Word32 L_log_en_int;
859 Word16 lsp_int[M];
860 Word16 log_en_int_e;
861 Word16 log_en_int_m;
862 Word16 level;
863 Word16 acoeff[M + 1];
864 Word16 refl[M];
865 Word16 pred_err;
866 Word16 ex[L_SUBFR];
867 Word16 ma_pred_init;
868 Word16 log_pg_e;
869 Word16 log_pg_m;
870 Word16 log_pg;
871 Flag negative;
872 Word16 lsf_mean;
873 Word32 L_lsf_mean;
874 Word16 lsf_variab_index;
875 Word16 lsf_variab_factor;
876 Word16 lsf_int[M];
877 Word16 lsf_int_variab[M];
878 Word16 lsp_int_variab[M];
879 Word16 acoeff_variab[M + 1];
880
881 Word16 lsf[M];
882 Word32 L_lsf[M];
883 Word16 ptr;
884 Word16 tmp_int_length;
885
886 Word32 L_temp;
887 Word16 temp;
888
889 /* This function is called if synthesis state is not SPEECH
890 * the globally passed inputs to this function are
891 * st->sid_frame
892 * st->valid_data
893 * st->dtxHangoverAdded
894 * new_state (SPEECH, DTX, DTX_MUTE)
895 */
896
897 if ((st->dtxHangoverAdded != 0) &&
898 (st->sid_frame != 0))
899 {
900 /* sid_first after dtx hangover period */
901 /* or sid_upd after dtxhangover */
902
903 /* set log_en_adjust to correct value */
904 st->log_en_adjust = dtx_log_en_adjust[mode];
905
906 ptr = st->lsf_hist_ptr + M;
907
908 if (ptr == 80)
909 {
910 ptr = 0;
911 }
912 Copy(&st->lsf_hist[st->lsf_hist_ptr], &st->lsf_hist[ptr], M);
913
914 ptr = st->log_en_hist_ptr + 1;
915
916 if (ptr == DTX_HIST_SIZE)
917 {
918 ptr = 0;
919 }
920
921 st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */
922
923 /* compute mean log energy and lsp *
924 * from decoded signal (SID_FIRST) */
925 st->log_en = 0;
926 for (i = M - 1; i >= 0; i--)
927 {
928 L_lsf[i] = 0;
929 }
930
931 /* average energy and lsp */
932 for (i = DTX_HIST_SIZE - 1; i >= 0; i--)
933 {
934 if (st->log_en_hist[i] < 0)
935 {
936 temp = ~((~st->log_en_hist[i]) >> 3);
937 }
938 else
939 {
940 temp = st->log_en_hist[i] >> 3;
941 }
942 st->log_en = add(st->log_en, temp, pOverflow);
943 for (j = M - 1; j >= 0; j--)
944 {
945 L_lsf[j] = L_add(L_lsf[j],
946 L_deposit_l(st->lsf_hist[i * M + j]), pOverflow);
947 }
948 }
949
950 for (j = M - 1; j >= 0; j--)
951 {
952 if (L_lsf[j] < 0)
953 {
954 lsf[j] = (Word16)(~((~L_lsf[j]) >> 3));
955 }
956 else
957 {
958 lsf[j] = (Word16)(L_lsf[j] >> 3);
959 }
960 }
961
962 Lsf_lsp(lsf, st->lsp, M, pOverflow);
963
964 /* make log_en speech coder mode independent */
965 /* added again later before synthesis */
966 st->log_en = sub(st->log_en, st->log_en_adjust, pOverflow);
967
968 /* compute lsf variability vector */
969 Copy(st->lsf_hist, st->lsf_hist_mean, 80);
970
971 for (i = M - 1; i >= 0; i--)
972 {
973 L_lsf_mean = 0;
974 /* compute mean lsf */
975 for (j = 8 - 1; j >= 0; j--)
976 {
977 L_lsf_mean = L_add(L_lsf_mean,
978 L_deposit_l(st->lsf_hist_mean[i+j*M]), pOverflow);
979 }
980
981 if (L_lsf_mean < 0)
982 {
983 lsf_mean = (Word16)(~((~L_lsf_mean) >> 3));
984 }
985 else
986 {
987 lsf_mean = (Word16)(L_lsf_mean >> 3);
988 }
989 /* subtract mean and limit to within reasonable limits *
990 * moreover the upper lsf's are attenuated */
991 for (j = 8 - 1; j >= 0; j--)
992 {
993 /* subtract mean */
994 st->lsf_hist_mean[i+j*M] =
995 sub(st->lsf_hist_mean[i+j*M], lsf_mean, pOverflow);
996
997 /* attenuate deviation from mean, especially for upper lsf's */
998 st->lsf_hist_mean[i+j*M] =
999 mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i], pOverflow);
1000
1001 /* limit the deviation */
1002 if (st->lsf_hist_mean[i+j*M] < 0)
1003 {
1004 negative = 1;
1005 }
1006 else
1007 {
1008 negative = 0;
1009 }
1010 st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
1011
1012 /* apply soft limit */
1013 if (st->lsf_hist_mean[i+j*M] > 655)
1014 {
1015 st->lsf_hist_mean[i+j*M] = 655 + ((st->lsf_hist_mean[i+j*M]
1016 - 655) >> 2);
1017 }
1018
1019 /* apply hard limit */
1020 if (st->lsf_hist_mean[i+j*M] > 1310)
1021 {
1022 st->lsf_hist_mean[i+j*M] = 1310;
1023 }
1024
1025 if (negative != 0)
1026 {
1027 st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
1028 }
1029 }
1030 }
1031 }
1032
1033
1034 if (st->sid_frame != 0)
1035 {
1036 /* Set old SID parameters, always shift */
1037 /* even if there is no new valid_data */
1038 Copy(st->lsp, st->lsp_old, M);
1039 st->old_log_en = st->log_en;
1040
1041 if (st->valid_data != 0) /* new data available (no CRC) */
1042 {
1043 /* Compute interpolation factor, since the division only works *
1044 * for values of since_last_sid < 32 we have to limit the *
1045 * interpolation to 32 frames */
1046 tmp_int_length = st->since_last_sid;
1047 st->since_last_sid = 0;
1048
1049 if (tmp_int_length >= 32)
1050 {
1051 tmp_int_length = 32;
1052 }
1053
1054 L_temp = ((Word32) tmp_int_length) << 10;
1055 if (L_temp != (Word32)((Word16) L_temp))
1056 {
1057 *pOverflow = 1;
1058 L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1059 }
1060 temp = (Word16) L_temp;
1061
1062 if (tmp_int_length >= 2)
1063 {
1064 st->true_sid_period_inv = div_s(1 << 10, temp);
1065 }
1066 else
1067 {
1068 st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */
1069 }
1070
1071 Init_D_plsf_3(lsfState, parm[0]);
1072 D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp, pOverflow);
1073 Set_zero(lsfState->past_r_q, M); /* reset for next speech frame */
1074
1075 log_en_index = parm[4];
1076 /* Q11 and divide by 4 */
1077 if ((log_en_index > 63) || (log_en_index < -64))
1078 {
1079 st->log_en = (log_en_index > 0) ? MAX_16 : MIN_16;
1080 }
1081 else
1082 {
1083 st->log_en = (log_en_index) << (11 - 2);
1084 }
1085
1086 /* Subtract 2.5 in Q11 */
1087 st->log_en = sub(st->log_en, (2560 * 2), pOverflow);
1088
1089 /* Index 0 is reserved for silence */
1090 if (log_en_index == 0)
1091 {
1092 st->log_en = MIN_16;
1093 }
1094
1095 /* no interpolation at startup after coder reset */
1096 /* or when SID_UPD has been received right after SPEECH */
1097
1098 if ((st->data_updated == 0) ||
1099 (st->dtxGlobalState == SPEECH))
1100 {
1101 Copy(st->lsp, st->lsp_old, M);
1102 st->old_log_en = st->log_en;
1103 }
1104 } /* endif valid_data */
1105
1106 /* initialize gain predictor memory of other modes */
1107 if (st->log_en < 0)
1108 {
1109 temp = ~((~st->log_en) >> 1);
1110 }
1111 else
1112 {
1113 temp = st->log_en >> 1;
1114 }
1115 ma_pred_init = sub(temp, 9000, pOverflow);
1116
1117 if (ma_pred_init > 0)
1118 {
1119 ma_pred_init = 0;
1120 }
1121 else if (ma_pred_init < -14436)
1122 {
1123 ma_pred_init = -14436;
1124 }
1125
1126 predState->past_qua_en[0] = ma_pred_init;
1127 predState->past_qua_en[1] = ma_pred_init;
1128 predState->past_qua_en[2] = ma_pred_init;
1129 predState->past_qua_en[3] = ma_pred_init;
1130
1131 /* past_qua_en for other modes than MR122 */
1132 ma_pred_init = mult(5443, ma_pred_init, pOverflow);
1133 /* scale down by factor 20*log10(2) in Q15 */
1134 predState->past_qua_en_MR122[0] = ma_pred_init;
1135 predState->past_qua_en_MR122[1] = ma_pred_init;
1136 predState->past_qua_en_MR122[2] = ma_pred_init;
1137 predState->past_qua_en_MR122[3] = ma_pred_init;
1138 } /* endif sid_frame */
1139
1140 /* CN generation */
1141 /* recompute level adjustment factor Q11 *
1142 * st->log_en_adjust = 0.9*st->log_en_adjust + *
1143 * 0.1*dtx_log_en_adjust[mode]); */
1144 if (dtx_log_en_adjust[mode] > 1023)
1145 {
1146 temp = MAX_16;
1147 }
1148 else if (dtx_log_en_adjust[mode] < -1024)
1149 {
1150 temp = MIN_16;
1151 }
1152 else
1153 {
1154 temp = mult((Word16)((Word32)dtx_log_en_adjust[mode] << 5), 3277, pOverflow);
1155 }
1156
1157 if (temp < 0)
1158 {
1159 temp = ~((~temp) >> 5);
1160 }
1161 else
1162 {
1163 temp >>= 5;
1164 }
1165 st->log_en_adjust = add(mult(st->log_en_adjust, 29491, pOverflow), temp, pOverflow);
1166
1167 /* Interpolate SID info */
1168 int_fac = shl(add(1, st->since_last_sid, pOverflow), 10, pOverflow); /* Q10 */
1169 int_fac = mult(int_fac, st->true_sid_period_inv, pOverflow); /* Q10 * Q15 -> Q10 */
1170
1171 /* Maximize to 1.0 in Q10 */
1172 if (int_fac > 1024)
1173 {
1174 int_fac = 16384;
1175 }
1176 else if (int_fac < -2048)
1177 {
1178 int_fac = MIN_16;
1179 }
1180 else
1181 {
1182 int_fac <<= 4; /* Q10 -> Q14 */
1183 }
1184
1185 L_log_en_int = L_mult(int_fac, st->log_en, pOverflow); /* Q14 * Q11->Q26 */
1186 for (i = M - 1; i >= 0; i--)
1187 {
1188 lsp_int[i] = mult(int_fac, st->lsp[i], pOverflow);/* Q14 * Q15 -> Q14 */
1189 }
1190
1191 int_fac = sub(16384, int_fac, pOverflow); /* 1-k in Q14 */
1192
1193 /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
1194 L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en, pOverflow);
1195 for (i = M - 1; i >= 0; i--)
1196 {
1197 /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
1198 lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i], pOverflow), pOverflow);
1199
1200 L_temp = ((Word32) lsp_int[i]) << 1; /* Q14 -> Q15 */
1201 if (L_temp != (Word32)((Word16) L_temp))
1202 {
1203 *pOverflow = 1;
1204 L_temp = (Word32)((lsp_int[i] > 0) ? MAX_16 : MIN_16);
1205 }
1206 lsp_int[i] = (Word16) L_temp;
1207 }
1208
1209 /* compute the amount of lsf variability */
1210 lsf_variab_factor = sub(st->log_pg_mean, 2457, pOverflow); /* -0.6 in Q12 */
1211 /* *0.3 Q12*Q15 -> Q12 */
1212 lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830, pOverflow), pOverflow);
1213
1214 /* limit to values between 0..1 in Q12 */
1215 if (lsf_variab_factor > 4095)
1216 {
1217 lsf_variab_factor = MAX_16;
1218 }
1219 else if (lsf_variab_factor < 0)
1220 {
1221 lsf_variab_factor = 0;
1222 }
1223 else
1224 {
1225 lsf_variab_factor <<= 3; /* -> Q15 */
1226 }
1227
1228 /* get index of vector to do variability with */
1229 lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
1230
1231 /* convert to lsf */
1232 Lsp_lsf(lsp_int, lsf_int, M, pOverflow);
1233
1234 /* apply lsf variability */
1235 Copy(lsf_int, lsf_int_variab, M);
1236 for (i = M - 1; i >= 0; i--)
1237 {
1238 lsf_int_variab[i] = add(lsf_int_variab[i],
1239 mult(lsf_variab_factor,
1240 st->lsf_hist_mean[i+lsf_variab_index*M], pOverflow)
1241 , pOverflow);
1242 }
1243
1244 /* make sure that LSP's are ordered */
1245 Reorder_lsf(lsf_int, LSF_GAP, M, pOverflow);
1246 Reorder_lsf(lsf_int_variab, LSF_GAP, M, pOverflow);
1247
1248 /* copy lsf to speech decoders lsf state */
1249 Copy(lsf_int, lsfState->past_lsf_q, M);
1250
1251 /* convert to lsp */
1252 Lsf_lsp(lsf_int, lsp_int, M, pOverflow);
1253 Lsf_lsp(lsf_int_variab, lsp_int_variab, M, pOverflow);
1254
1255 /* Compute acoeffs Q12 acoeff is used for level *
1256 * normalization and postfilter, acoeff_variab is *
1257 * used for synthesis filter *
1258 * by doing this we make sure that the level *
1259 * in high frequenncies does not jump up and down */
1260
1261 Lsp_Az(lsp_int, acoeff, pOverflow);
1262 Lsp_Az(lsp_int_variab, acoeff_variab, pOverflow);
1263
1264 /* For use in postfilter */
1265 Copy(acoeff, &A_t[0], M + 1);
1266 Copy(acoeff, &A_t[M + 1], M + 1);
1267 Copy(acoeff, &A_t[2 *(M + 1)], M + 1);
1268 Copy(acoeff, &A_t[3 *(M + 1)], M + 1);
1269
1270 /* Compute reflection coefficients Q15 */
1271 A_Refl(&acoeff[1], refl, pOverflow);
1272
1273 /* Compute prediction error in Q15 */
1274 pred_err = MAX_16; /* 0.99997 in Q15 */
1275 for (i = 0; i < M; i++)
1276 {
1277 L_temp = (((Word32) refl[i]) * refl[i]) >> 15;
1278 if (L_temp <= 0x00007fffL)
1279 {
1280 temp = MAX_16 - (Word16) L_temp;
1281 }
1282 else
1283 {
1284 *pOverflow = 1;
1285 temp = 0;
1286 }
1287 pred_err = mult(pred_err, temp, pOverflow);
1288 }
1289
1290 /* compute logarithm of prediction gain */
1291 Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m, pOverflow);
1292
1293 /* convert exponent and mantissa to Word16 Q12 */
1294 log_pg = shl(sub(log_pg_e, 15, pOverflow), 12, pOverflow); /* Q12 */
1295 log_pg = shr(sub(0, add(log_pg, shr(log_pg_m, 15 - 12, pOverflow),
1296 pOverflow), pOverflow), 1, pOverflow);
1297 st->log_pg_mean = add(mult(29491, st->log_pg_mean, pOverflow),
1298 mult(3277, log_pg, pOverflow), pOverflow);
1299
1300 /* Compute interpolated log energy */
1301 L_log_en_int = L_shr(L_log_en_int, 10, pOverflow); /* Q26 -> Q16 */
1302
1303 /* Add 4 in Q16 */
1304 L_log_en_int = L_add(L_log_en_int, 4 * 65536L, pOverflow);
1305
1306 /* subtract prediction gain */
1307 L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4, pOverflow), pOverflow);
1308
1309 /* adjust level to speech coder mode */
1310 L_log_en_int = L_add(L_log_en_int,
1311 L_shl(L_deposit_l(st->log_en_adjust), 5, pOverflow), pOverflow);
1312
1313 log_en_int_e = (Word16)(L_log_en_int >> 16);
1314
1315 log_en_int_m = (Word16)(L_shr(L_sub(L_log_en_int,
1316 L_deposit_h(log_en_int_e), pOverflow), 1, pOverflow));
1317 level = (Word16)(Pow2(log_en_int_e, log_en_int_m, pOverflow)); /* Q4 */
1318
1319 for (i = 0; i < 4; i++)
1320 {
1321 /* Compute innovation vector */
1322 build_CN_code(&st->L_pn_seed_rx, ex, pOverflow);
1323 for (j = L_SUBFR - 1; j >= 0; j--)
1324 {
1325 ex[j] = mult(level, ex[j], pOverflow);
1326 }
1327 /* Synthesize */
1328 Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
1329 mem_syn, 1);
1330
1331 } /* next i */
1332
1333 /* reset codebook averaging variables */
1334 averState->hangVar = 20;
1335 averState->hangCount = 0;
1336
1337 if (new_state == DTX_MUTE)
1338 {
1339 /* mute comfort noise as it has been quite a long time since
1340 * last SID update was performed */
1341
1342 tmp_int_length = st->since_last_sid;
1343
1344 if (tmp_int_length > 32)
1345 {
1346 tmp_int_length = 32;
1347 }
1348 else if (tmp_int_length <= 0)
1349 {
1350 /* safety guard against division by zero */
1351 tmp_int_length = 8;
1352 }
1353
1354 L_temp = ((Word32) tmp_int_length) << 10;
1355 if (L_temp != (Word32)((Word16) L_temp))
1356 {
1357 *pOverflow = 1;
1358 L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1359 }
1360 temp = (Word16) L_temp;
1361
1362 st->true_sid_period_inv = div_s(1 << 10, temp);
1363
1364 st->since_last_sid = 0;
1365 Copy(st->lsp, st->lsp_old, M);
1366 st->old_log_en = st->log_en;
1367 /* subtract 1/8 in Q11 i.e -6/8 dB */
1368 st->log_en = sub(st->log_en, 256, pOverflow);
1369 }
1370
1371 /* reset interpolation length timer
1372 * if data has been updated. */
1373 if ((st->sid_frame != 0) &&
1374 ((st->valid_data != 0) ||
1375 ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
1376 {
1377 st->since_last_sid = 0;
1378 st->data_updated = 1;
1379 }
1380
1381 return;
1382 }
1383
1384
1385 /****************************************************************************/
1386
1387 /*
1388 ------------------------------------------------------------------------------
1389 FUNCTION NAME: dtx_dec_activity_update
1390 ------------------------------------------------------------------------------
1391 INPUT AND OUTPUT DEFINITIONS
1392
1393 Inputs:
1394 st = pointer to a structure of type dtx_decState
1395 lsf =
1396 frame =
1397
1398 Outputs:
1399 st points to an updated structure of type dtx_decState
1400
1401 Returns:
1402 None.
1403
1404 Global Variables Used:
1405 None.
1406
1407 Local Variables Needed:
1408 None.
1409
1410 ------------------------------------------------------------------------------
1411 FUNCTION DESCRIPTION
1412
1413 This function updates the DTX parameters.
1414
1415 ------------------------------------------------------------------------------
1416 REQUIREMENTS
1417
1418 None
1419
1420 ------------------------------------------------------------------------------
1421 REFERENCES
1422
1423 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1424
1425 ------------------------------------------------------------------------------
1426 PSEUDO-CODE
1427
1428 void dtx_dec_activity_update(dtx_decState *st,
1429 Word16 lsf[],
1430 Word16 frame[])
1431 {
1432 Word16 i;
1433
1434 Word32 L_frame_en;
1435 Word16 log_en_e, log_en_m, log_en;
1436
1437 // update lsp history
1438 st->lsf_hist_ptr = add(st->lsf_hist_ptr,M);
1439 if (sub(st->lsf_hist_ptr, 80) == 0)
1440 {
1441 st->lsf_hist_ptr = 0;
1442 }
1443 Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);
1444
1445 // compute log energy based on frame energy
1446 L_frame_en = 0; // Q0
1447 for (i=0; i < L_FRAME; i++)
1448 {
1449 L_frame_en = L_mac(L_frame_en, frame[i], frame[i]);
1450 }
1451 Log2(L_frame_en, &log_en_e, &log_en_m);
1452
1453 // convert exponent and mantissa to Word16 Q10
1454 log_en = shl(log_en_e, 10); // Q10
1455 log_en = add(log_en, shr(log_en_m, 15-10));
1456
1457 // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193
1458 log_en = sub(log_en, 7497+1024);
1459
1460 // insert into log energy buffer, no division by two as *
1461 * log_en in decoder is Q11
1462 st->log_en_hist_ptr = add(st->log_en_hist_ptr, 1);
1463 if (sub(st->log_en_hist_ptr, DTX_HIST_SIZE) == 0)
1464 {
1465 st->log_en_hist_ptr = 0;
1466 }
1467 st->log_en_hist[st->log_en_hist_ptr] = log_en; // Q11
1468 }
1469
1470 ------------------------------------------------------------------------------
1471 RESOURCES USED [optional]
1472
1473 When the code is written for a specific target processor the
1474 the resources used should be documented below.
1475
1476 HEAP MEMORY USED: x bytes
1477
1478 STACK MEMORY USED: x bytes
1479
1480 CLOCK CYCLES: (cycle count equation for this function) + (variable
1481 used to represent cycle count for each subroutine
1482 called)
1483 where: (cycle count variable) = cycle count for [subroutine
1484 name]
1485
1486 ------------------------------------------------------------------------------
1487 CAUTION [optional]
1488 [State any special notes, constraints or cautions for users of this function]
1489
1490 ------------------------------------------------------------------------------
1491 */
1492
dtx_dec_activity_update(dtx_decState * st,Word16 lsf[],Word16 frame[],Flag * pOverflow)1493 void dtx_dec_activity_update(dtx_decState *st,
1494 Word16 lsf[],
1495 Word16 frame[],
1496 Flag *pOverflow)
1497 {
1498 Word16 i;
1499
1500 Word32 L_frame_en;
1501 Word32 L_temp;
1502 Word16 log_en_e;
1503 Word16 log_en_m;
1504 Word16 log_en;
1505
1506 /* update lsp history */
1507 st->lsf_hist_ptr += M;
1508
1509 if (st->lsf_hist_ptr == 80)
1510 {
1511 st->lsf_hist_ptr = 0;
1512 }
1513 Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);
1514
1515 /* compute log energy based on frame energy */
1516 L_frame_en = 0; /* Q0 */
1517 for (i = L_FRAME - 1; i >= 0; i--)
1518 {
1519 L_temp = ((Word32) frame[i]) * frame[i];
1520 if (L_temp != (Word32) 0x40000000L)
1521 {
1522 L_temp = L_temp << 1;
1523 }
1524 else
1525 {
1526 L_temp = MAX_32;
1527 }
1528 L_frame_en = L_add(L_frame_en, L_temp, pOverflow);
1529 }
1530 Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);
1531
1532 /* convert exponent and mantissa to Word16 Q10 */
1533 L_temp = ((Word32) log_en_e) << 10;
1534
1535 if (L_temp != (Word32)((Word16) L_temp))
1536 {
1537 *pOverflow = 1;
1538 L_temp = (Word32)((log_en_e > 0) ? MAX_16 : MIN_16);
1539 }
1540 log_en_e = (Word16) L_temp;
1541
1542 if (log_en_m < 0)
1543 {
1544 log_en_m = ~((~log_en_m) >> 5);
1545 }
1546 else
1547 {
1548 log_en_m >>= 5;
1549 }
1550 log_en = add(log_en_e, log_en_m, pOverflow);
1551
1552 /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
1553 log_en = sub(log_en, 7497 + 1024, pOverflow);
1554
1555 /* insert into log energy buffer, no division by two as *
1556 * log_en in decoder is Q11 */
1557 st->log_en_hist_ptr += 1;
1558
1559 if (st->log_en_hist_ptr == DTX_HIST_SIZE)
1560 {
1561 st->log_en_hist_ptr = 0;
1562 }
1563 st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */
1564
1565 return;
1566 }
1567
1568 /****************************************************************************/
1569
1570 /*
1571 ------------------------------------------------------------------------------
1572 FUNCTION NAME: rx_dtx_handler
1573 ------------------------------------------------------------------------------
1574 INPUT AND OUTPUT DEFINITIONS
1575
1576 Inputs:
1577 st = pointer to a structure of type dtx_decState
1578 frame_type = RX frame type
1579
1580 Returns:
1581 newState = variable of type DTXStateType
1582
1583 Outputs:
1584 st points to an updated structure of type dtx_decState
1585
1586 Global Variables Used:
1587 None.
1588
1589 Local Variables Needed:
1590 None.
1591
1592 ------------------------------------------------------------------------------
1593 FUNCTION DESCRIPTION
1594
1595 This function determines the new state of the decoder based on the frame_type
1596 and sets up the decoder parameters according to newState.
1597
1598 Table of new SPD synthesis states
1599
1600 | previous SPD_synthesis_state
1601 Incoming |
1602 frame_type | SPEECH | DTX | DTX_MUTE
1603 ---------------------------------------------------------------
1604 RX_SPEECH_GOOD , | | |
1605 RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH
1606 ----------------------------------------------------------------
1607 RX_SPEECH_PR_BAD, | | |
1608 RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE
1609 ----------------------------------------------------------------
1610 RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
1611 ----------------------------------------------------------------
1612 RX_SID_UPDATE, | DTX | DTX | DTX
1613 ----------------------------------------------------------------
1614 RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
1615 ----------------------------------------------------------------
1616 RX_NO_DATA | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
1617 |(class2 garb.)| |
1618 ----------------------------------------------------------------
1619 RX_ONSET | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
1620 |(class2 garb.)| |
1621 ----------------------------------------------------------------
1622
1623 ------------------------------------------------------------------------------
1624 REQUIREMENTS
1625
1626 None
1627
1628 ------------------------------------------------------------------------------
1629 REFERENCES
1630
1631 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1632
1633 ------------------------------------------------------------------------------
1634 PSEUDO-CODE
1635
1636 enum DTXStateType rx_dtx_handler(
1637 dtx_decState *st, // i/o : State struct
1638 enum RXFrameType frame_type // i : Frame type
1639 )
1640 {
1641 enum DTXStateType newState;
1642 enum DTXStateType encState;
1643
1644 // DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH)
1645 if ((sub(frame_type, RX_SID_FIRST) == 0) ||
1646 (sub(frame_type, RX_SID_UPDATE) == 0) ||
1647 (sub(frame_type, RX_SID_BAD) == 0) ||
1648 (((sub(st->dtxGlobalState, DTX) == 0) ||
1649 (sub(st->dtxGlobalState, DTX_MUTE) == 0)) &&
1650 ((sub(frame_type, RX_NO_DATA) == 0) ||
1651 (sub(frame_type, RX_SPEECH_BAD) == 0) ||
1652 (sub(frame_type, RX_ONSET) == 0))))
1653 {
1654 newState = DTX;
1655
1656 // stay in mute for these input types
1657 if ((sub(st->dtxGlobalState, DTX_MUTE) == 0) &&
1658 ((sub(frame_type, RX_SID_BAD) == 0) ||
1659 (sub(frame_type, RX_SID_FIRST) == 0) ||
1660 (sub(frame_type, RX_ONSET) == 0) ||
1661 (sub(frame_type, RX_NO_DATA) == 0)))
1662 {
1663 newState = DTX_MUTE;
1664 }
1665
1666 // evaluate if noise parameters are too old
1667 // since_last_sid is reset when CN parameters have been updated
1668 st->since_last_sid = add(st->since_last_sid, 1);
1669
1670 // no update of sid parameters in DTX for a long while
1671 // Due to the delayed update of st->since_last_sid counter
1672 // SID_UPDATE frames need to be handled separately to avoid
1673 // entering DTX_MUTE for late SID_UPDATE frames
1674 if((sub(frame_type, RX_SID_UPDATE) != 0) &&
1675 (sub(st->since_last_sid, DTX_MAX_EMPTY_THRESH) > 0))
1676 {
1677 newState = DTX_MUTE;
1678 }
1679 }
1680 else
1681 {
1682 newState = SPEECH;
1683 st->since_last_sid = 0;
1684 }
1685
1686 // reset the decAnaElapsed Counter when receiving CNI data the first
1687 // time, to robustify counter missmatch after handover
1688 // this might delay the bwd CNI analysis in the new decoder slightly.
1689
1690 if ((st->data_updated == 0) &&
1691 (sub(frame_type, RX_SID_UPDATE) == 0))
1692 {
1693 st->decAnaElapsedCount = 0;
1694 }
1695
1696 // update the SPE-SPD DTX hangover synchronization
1697 // to know when SPE has added dtx hangover
1698 st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1);
1699 st->dtxHangoverAdded = 0;
1700
1701 if ((sub(frame_type, RX_SID_FIRST) == 0) ||
1702 (sub(frame_type, RX_SID_UPDATE) == 0) ||
1703 (sub(frame_type, RX_SID_BAD) == 0) ||
1704 (sub(frame_type, RX_ONSET) == 0) ||
1705 (sub(frame_type, RX_NO_DATA) == 0))
1706 {
1707 encState = DTX;
1708
1709 // In frame errors simulations RX_NO_DATA may occasionally mean that
1710 // a speech packet was probably sent by the encoder,
1711 // the assumed _encoder_ state should be SPEECH in such cases.
1712 if((sub(frame_type, RX_NO_DATA) == 0) &&
1713 (sub(newState, SPEECH) == 0))
1714 {
1715 encState = SPEECH;
1716 }
1717
1718 // Note on RX_ONSET operation differing from RX_NO_DATA operation:
1719 // If a RX_ONSET is received in the decoder (by "accident")
1720 // it is still most likely that the encoder state
1721 // for the "ONSET frame" was DTX.
1722
1723 }
1724 else
1725 {
1726 encState = SPEECH;
1727 }
1728
1729 if (sub(encState, SPEECH) == 0)
1730 {
1731 st->dtxHangoverCount = DTX_HANG_CONST;
1732 }
1733 else
1734 {
1735 if (sub(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0)
1736 {
1737 st->dtxHangoverAdded = 1;
1738 st->decAnaElapsedCount = 0;
1739 st->dtxHangoverCount = 0;
1740 }
1741 else if (st->dtxHangoverCount == 0)
1742 {
1743 st->decAnaElapsedCount = 0;
1744 }
1745 else
1746 {
1747 st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);
1748 }
1749 }
1750
1751 if (sub(newState, SPEECH) != 0)
1752 {
1753 // DTX or DTX_MUTE
1754 // CN data is not in a first SID, first SIDs are marked as SID_BAD
1755 // but will do backwards analysis if a hangover period has been added
1756 // according to the state machine above
1757
1758 st->sid_frame = 0;
1759 st->valid_data = 0;
1760
1761 if (sub(frame_type, RX_SID_FIRST) == 0)
1762 {
1763 st->sid_frame = 1;
1764 }
1765 else if (sub(frame_type, RX_SID_UPDATE) == 0)
1766 {
1767 st->sid_frame = 1;
1768 st->valid_data = 1;
1769 }
1770 else if (sub(frame_type, RX_SID_BAD) == 0)
1771 {
1772 st->sid_frame = 1;
1773 st->dtxHangoverAdded = 0; // use old data
1774 }
1775 }
1776
1777 return newState;
1778 // newState is used by both SPEECH AND DTX synthesis routines
1779 }
1780
1781 ------------------------------------------------------------------------------
1782 RESOURCES USED [optional]
1783
1784 When the code is written for a specific target processor the
1785 the resources used should be documented below.
1786
1787 HEAP MEMORY USED: x bytes
1788
1789 STACK MEMORY USED: x bytes
1790
1791 CLOCK CYCLES: (cycle count equation for this function) + (variable
1792 used to represent cycle count for each subroutine
1793 called)
1794 where: (cycle count variable) = cycle count for [subroutine
1795 name]
1796
1797 ------------------------------------------------------------------------------
1798 CAUTION [optional]
1799 [State any special notes, constraints or cautions for users of this function]
1800
1801 ------------------------------------------------------------------------------
1802 */
1803
rx_dtx_handler(dtx_decState * st,enum RXFrameType frame_type,Flag * pOverflow)1804 enum DTXStateType rx_dtx_handler(
1805 dtx_decState *st, /* i/o : State struct */
1806 enum RXFrameType frame_type,/* i : Frame type */
1807 Flag *pOverflow)
1808 {
1809 enum DTXStateType newState;
1810 enum DTXStateType encState;
1811
1812
1813 /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
1814
1815 if ((frame_type == RX_SID_FIRST) ||
1816 (frame_type == RX_SID_UPDATE) ||
1817 (frame_type == RX_SID_BAD) ||
1818 (((st->dtxGlobalState == DTX) || (st->dtxGlobalState == DTX_MUTE)) &&
1819 ((frame_type == RX_NO_DATA) || (frame_type == RX_SPEECH_BAD) ||
1820 (frame_type == RX_ONSET))))
1821 {
1822 newState = DTX;
1823
1824 /* stay in mute for these input types */
1825
1826 if ((st->dtxGlobalState == DTX_MUTE) &&
1827 ((frame_type == RX_SID_BAD) ||
1828 (frame_type == RX_SID_FIRST) ||
1829 (frame_type == RX_ONSET) ||
1830 (frame_type == RX_NO_DATA)))
1831 {
1832 newState = DTX_MUTE;
1833 }
1834
1835 /* evaluate if noise parameters are too old */
1836 /* since_last_sid is reset when CN parameters have been updated */
1837 st->since_last_sid = add(st->since_last_sid, 1, pOverflow);
1838
1839 /* no update of sid parameters in DTX for a long while */
1840 /* Due to the delayed update of st->since_last_sid counter */
1841 /* SID_UPDATE frames need to be handled separately to avoid */
1842 /* entering DTX_MUTE for late SID_UPDATE frames */
1843 if ((frame_type != RX_SID_UPDATE) &&
1844 (st->since_last_sid > DTX_MAX_EMPTY_THRESH))
1845 {
1846 newState = DTX_MUTE;
1847 }
1848 }
1849 else
1850 {
1851 newState = SPEECH;
1852 st->since_last_sid = 0;
1853 }
1854
1855 /*
1856 reset the decAnaElapsed Counter when receiving CNI data the first
1857 time, to robustify counter missmatch after handover
1858 this might delay the bwd CNI analysis in the new decoder slightly.
1859 */
1860
1861 if ((st->data_updated == 0) &&
1862 (frame_type == RX_SID_UPDATE))
1863 {
1864 st->decAnaElapsedCount = 0;
1865 }
1866
1867 /* update the SPE-SPD DTX hangover synchronization */
1868 /* to know when SPE has added dtx hangover */
1869 st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1, pOverflow);
1870 st->dtxHangoverAdded = 0;
1871
1872 if ((frame_type == RX_SID_FIRST) ||
1873 (frame_type == RX_SID_UPDATE) ||
1874 (frame_type == RX_SID_BAD) ||
1875 (frame_type == RX_ONSET) ||
1876 (frame_type == RX_NO_DATA))
1877 {
1878 encState = DTX;
1879
1880 /*
1881 In frame errors simulations RX_NO_DATA may occasionally mean that
1882 a speech packet was probably sent by the encoder,
1883 the assumed _encoder_ state should be SPEECH in such cases.
1884 */
1885 if ((frame_type == RX_NO_DATA) &&
1886 (newState == SPEECH))
1887 {
1888 encState = SPEECH;
1889 }
1890
1891 /*
1892 Note on RX_ONSET operation differing from RX_NO_DATA operation:
1893 If a RX_ONSET is received in the decoder (by "accident")
1894 it is still most likely that the encoder state
1895 for the "ONSET frame" was DTX.
1896 */
1897 }
1898 else
1899 {
1900 encState = SPEECH;
1901 }
1902
1903
1904 if (encState == SPEECH)
1905 {
1906 st->dtxHangoverCount = DTX_HANG_CONST;
1907 }
1908 else
1909 {
1910
1911 if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH)
1912 {
1913 st->dtxHangoverAdded = 1;
1914 st->decAnaElapsedCount = 0;
1915 st->dtxHangoverCount = 0;
1916 }
1917 else if (st->dtxHangoverCount == 0)
1918 {
1919 st->decAnaElapsedCount = 0;
1920 }
1921 else
1922 {
1923 st->dtxHangoverCount -= 1;
1924 }
1925 }
1926
1927 if (newState != SPEECH)
1928 {
1929 /* DTX or DTX_MUTE
1930 * CN data is not in a first SID, first SIDs are marked as SID_BAD
1931 * but will do backwards analysis if a hangover period has been added
1932 * according to the state machine above
1933 */
1934
1935 st->sid_frame = 0;
1936 st->valid_data = 0;
1937
1938 if (frame_type == RX_SID_FIRST)
1939 {
1940 st->sid_frame = 1;
1941 }
1942 else if (frame_type == RX_SID_UPDATE)
1943 {
1944 st->sid_frame = 1;
1945 st->valid_data = 1;
1946 }
1947 else if (frame_type == RX_SID_BAD)
1948 {
1949 st->sid_frame = 1;
1950 st->dtxHangoverAdded = 0; /* use old data */
1951 }
1952 }
1953
1954 /* newState is used by both SPEECH AND DTX synthesis routines */
1955 return(newState);
1956 }
1957