1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: c_g_aver.cpp
39 ------------------------------------------------------------------------------
42 This file contains functions that reset and perform
43 codebook gain calculations.
45 ------------------------------------------------------------------------------
49 /*----------------------------------------------------------------------------
51 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Define module specific macros here
63 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Include all pre-processor statements here. Include conditional
69 ; compile variables also.
70 ----------------------------------------------------------------------------*/
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
83 ------------------------------------------------------------------------------
84 FUNCTION NAME: Cb_gain_average_reset
85 ------------------------------------------------------------------------------
86 INPUT AND OUTPUT DEFINITIONS
89 state = pointer to a structure of type Cb_gain_averageState
92 Structure pointed to by state is initialized to zeros
95 Returns 0 if memory was successfully initialized,
98 Global Variables Used:
101 Local Variables Needed:
104 ------------------------------------------------------------------------------
109 ------------------------------------------------------------------------------
114 ------------------------------------------------------------------------------
117 c_g_aver.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
119 ------------------------------------------------------------------------------
122 Word16 Cb_gain_average_reset (Cb_gain_averageState *state)
124 if (state == (Cb_gain_averageState *) NULL){
125 fprintf(stderr, "Cb_gain_average_reset: invalid parameter\n");
129 // Static vectors to zero
130 Set_zero (state->cbGainHistory, L_CBGAINHIST);
132 // Initialize hangover handling
139 ------------------------------------------------------------------------------
141 [State any special notes, constraints or cautions for users of this function]
143 ------------------------------------------------------------------------------
146 Word16 Cb_gain_average_reset(Cb_gain_averageState *state)
148 if (state == (Cb_gain_averageState *) NULL)
150 /* fprint(stderr, "Cb_gain_average_reset: invalid parameter\n"); */
154 /* Static vectors to zero */
155 oscl_memset(state->cbGainHistory, 0, L_CBGAINHIST*sizeof(Word16));
157 /* Initialize hangover handling */
159 state->hangCount = 0;
164 /****************************************************************************/
167 ------------------------------------------------------------------------------
168 FUNCTION NAME: Cb_gain_average
169 ------------------------------------------------------------------------------
170 INPUT AND OUTPUT DEFINITIONS
173 st = pointer to structure of type Cb_gain_averageState
174 mode = AMR mode (enum Mode)
175 gain_code = CB gain (Word16)
176 lsp = the LSP for the current frame (Word16)
177 lspAver = the average of LSP for 8 frames (Word16)
178 bfi = bad frame indication flag (Word16)
179 prev_bf = previous bad frame indication flag (Word16)
180 pdfi = potential degraded bad frame ind flag (Word16)
181 prev_pdf = prev pot. degraded bad frame ind flag (Word16)
182 inBackgroundNoise = background noise decision (Word16)
183 voicedHangover = # of frames after last voiced frame (Word16)
184 pOverflow = address of overflow (Flag)
187 cbGainMix = codebook gain (Word16)
192 Global Variables Used:
195 Local Variables Needed:
198 ------------------------------------------------------------------------------
201 The mix cb gains for MR475, MR515, MR59, MR67, MR102; gain_code other modes
203 ------------------------------------------------------------------------------
208 ------------------------------------------------------------------------------
211 c_g_aver.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
213 ------------------------------------------------------------------------------
216 Word16 Cb_gain_average (
217 Cb_gain_averageState *st, // i/o : State variables for CB gain avergeing
218 enum Mode mode, // i : AMR mode
219 Word16 gain_code, // i : CB gain Q1
220 Word16 lsp[], // i : The LSP for the current frame Q15
221 Word16 lspAver[], // i : The average of LSP for 8 frames Q15
222 Word16 bfi, // i : bad frame indication flag
223 Word16 prev_bf, // i : previous bad frame indication flag
224 Word16 pdfi, // i : potential degraded bad frame ind flag
225 Word16 prev_pdf, // i : prev pot. degraded bad frame ind flag
226 Word16 inBackgroundNoise, // i : background noise decision
227 Word16 voicedHangover // i : # of frames after last voiced frame
230 //---------------------------------------------------------*
231 * Compute mixed cb gain, used to make cb gain more *
232 * smooth in background noise for modes 5.15, 5.9 and 6.7 *
233 * states that needs to be updated by all *
234 *---------------------------------------------------------
236 Word16 cbGainMix, diff, tmp_diff, bgMix, cbGainMean;
238 Word16 tmp[M], tmp1, tmp2, shift1, shift2, shift;
240 // set correct cbGainMix for MR74, MR795, MR122
241 cbGainMix = gain_code;
243 *-------------------------------------------------------*
244 * Store list of CB gain needed in the CB gain *
246 *-------------------------------------------------------*
247 for (i = 0; i < (L_CBGAINHIST-1); i++)
249 st->cbGainHistory[i] = st->cbGainHistory[i+1];
251 st->cbGainHistory[L_CBGAINHIST-1] = gain_code;
253 // compute lsp difference
254 for (i = 0; i < M; i++) {
255 tmp1 = abs_s(sub(lspAver[i], lsp[i])); // Q15
256 shift1 = sub(norm_s(tmp1), 1); // Qn
257 tmp1 = shl(tmp1, shift1); // Q15+Qn
258 shift2 = norm_s(lspAver[i]); // Qm
259 tmp2 = shl(lspAver[i], shift2); // Q15+Qm
260 tmp[i] = div_s(tmp1, tmp2); // Q15+(Q15+Qn)-(Q15+Qm)
261 shift = sub(add(2, shift1), shift2);
264 tmp[i] = shr(tmp[i], shift); // Q15+Qn-Qm-Qx=Q13
268 tmp[i] = shl(tmp[i], negate(shift)); // Q15+Qn-Qm-Qx=Q13
273 for (i = 1; i < M; i++) {
274 diff = add(diff, tmp[i]); // Q13
278 if (sub(diff, 5325) > 0) // 0.65 in Q11
280 st->hangVar = add(st->hangVar, 1);
287 if (sub(st->hangVar, 10) > 0)
289 st->hangCount = 0; // Speech period, reset hangover variable
292 // Compute mix constant (bgMix)
293 bgMix = 8192; // 1 in Q13
294 if ((sub(mode, MR67) <= 0) || (sub(mode, MR102) == 0))
295 // MR475, MR515, MR59, MR67, MR102
297 // if errors and presumed noise make smoothing probability stronger
298 if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) || (prev_bf != 0)) &&
299 (sub(voicedHangover, 1) > 0) && (inBackgroundNoise != 0) &&
300 ((sub(mode, MR475) == 0) ||
301 (sub(mode, MR515) == 0) ||
302 (sub(mode, MR59) == 0)) ))
304 // bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25;
305 tmp_diff = sub(diff, 4506); // 0.55 in Q13
307 // max(0.0, diff-0.55)
318 if (sub(2048, tmp1) < 0)
324 bgMix = shl(tmp1, 2);
329 // bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25;
330 tmp_diff = sub(diff, 3277); // 0.4 in Q13
332 // max(0.0, diff-0.40)
343 if (sub(2048, tmp1) < 0)
349 bgMix = shl(tmp1, 2);
353 if ((sub(st->hangCount, 40) < 0) || (sub(diff, 5325) > 0)) // 0.65 in Q13
355 bgMix = 8192; // disable mix if too short time since
358 // Smoothen the cb gain trajectory
359 // smoothing depends on mix constant bgMix
360 L_sum = L_mult(6554, st->cbGainHistory[2]); // 0.2 in Q15; L_sum in Q17
361 for (i = 3; i < L_CBGAINHIST; i++)
363 L_sum = L_mac(L_sum, 6554, st->cbGainHistory[i]);
365 cbGainMean = pv_round(L_sum); // Q1
367 // more smoothing in error and bg noise (NB no DFI used here)
368 if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0) &&
369 ((sub(mode, MR475) == 0) ||
370 (sub(mode, MR515) == 0) ||
371 (sub(mode, MR59) == 0)) )
373 L_sum = L_mult(4681, st->cbGainHistory[0]); // 0.143 in Q15; L_sum in Q17
374 for (i = 1; i < L_CBGAINHIST; i++)
376 L_sum = L_mac(L_sum, 4681, st->cbGainHistory[i]);
378 cbGainMean = pv_round(L_sum); // Q1
381 // cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean;
382 L_sum = L_mult(bgMix, cbGainMix); // L_sum in Q15
383 L_sum = L_mac(L_sum, 8192, cbGainMean);
384 L_sum = L_msu(L_sum, bgMix, cbGainMean);
385 cbGainMix = pv_round(L_shl(L_sum, 2)); // Q1
388 st->hangCount = add(st->hangCount, 1);
392 ------------------------------------------------------------------------------
394 [State any special notes, constraints or cautions for users of this function]
396 ------------------------------------------------------------------------------
399 Word16 Cb_gain_average(
400 Cb_gain_averageState *st, /* i/o : State variables for CB gain averaging */
401 enum Mode mode, /* i : AMR mode */
402 Word16 gain_code, /* i : CB gain Q1 */
403 Word16 lsp[], /* i : The LSP for the current frame Q15 */
404 Word16 lspAver[], /* i : The average of LSP for 8 frames Q15 */
405 Word16 bfi, /* i : bad frame indication flag */
406 Word16 prev_bf, /* i : previous bad frame indication flag */
407 Word16 pdfi, /* i : potential degraded bad frame ind flag */
408 Word16 prev_pdf, /* i : prev pot. degraded bad frame ind flag */
409 Word16 inBackgroundNoise, /* i : background noise decision */
410 Word16 voicedHangover, /* i : # of frames after last voiced frame */
428 /*---------------------------------------------------------*
429 * Compute mixed cb gain, used to make cb gain more *
430 * smooth in background noise for modes 5.15, 5.9 and 6.7 *
431 * states that needs to be updated by all *
432 *---------------------------------------------------------*/
434 /* set correct cbGainMix for MR74, MR795, MR122 */
435 cbGainMix = gain_code;
437 /*-------------------------------------------------------*
438 * Store list of CB gain needed in the CB gain *
440 *-------------------------------------------------------*/
441 for (i = 0; i < (L_CBGAINHIST - 1); i++)
443 st->cbGainHistory[i] = st->cbGainHistory[i+1];
445 st->cbGainHistory[L_CBGAINHIST-1] = gain_code;
449 /* compute lsp difference */
450 for (i = 0; i < M; i++)
452 tmp1 = abs_s(sub(*(lspAver + i), *(lsp + i), pOverflow));
454 shift1 = norm_s(tmp1) - 1 ; /* Qn */
455 tmp1 = shl(tmp1, shift1, pOverflow); /* Q15+Qn */
456 shift2 = norm_s(*(lspAver + i)); /* Qm */
457 tmp2 = shl(*(lspAver + i), shift2, pOverflow); /* Q15+Qm */
458 tmp[i] = div_s(tmp1, tmp2); /* Q15+(Q15+Qn)-(Q15+Qm) */
460 shift = 2 + shift1 - shift2;
464 *(tmp + i) = shr(*(tmp + i), shift, pOverflow);
465 /* Q15+Qn-Qm-Qx=Q13 */
469 *(tmp + i) = shl(*(tmp + i), negate(shift), pOverflow);
470 /* Q15+Qn-Qm-Qx=Q13 */
473 diff = add_16(diff, *(tmp + i), pOverflow); /* Q13 */
476 /* Compute hangover */
478 if (diff > 5325) /* 0.65 in Q11 */
488 if (st->hangVar > 10)
490 /* Speech period, reset hangover variable */
494 /* Compute mix constant (bgMix) */
495 bgMix = 8192; /* 1 in Q13 */
497 if ((mode <= MR67) || (mode == MR102))
498 /* MR475, MR515, MR59, MR67, MR102 */
500 /* if errors and presumed noise make smoothing probability stronger */
502 if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) ||
504 && (voicedHangover > 1)
505 && (inBackgroundNoise != 0)
506 && ((mode == MR475) || (mode == MR515) ||
509 /* bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; */
510 tmp_diff = diff - 4506; /* 0.55 in Q13 */
514 /* bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; */
515 tmp_diff = diff - 3277; /* 0.4 in Q13 */
518 /* max(0.0, diff-0.55) or */
519 /* max(0.0, diff-0.40) */
529 /* min(0.25, tmp1) */
536 bgMix = shl(tmp1, 2, pOverflow);
539 if ((st->hangCount < 40) || (diff > 5325)) /* 0.65 in Q13 */
541 /* disable mix if too short time since */
545 /* Smoothen the cb gain trajectory */
546 /* smoothing depends on mix constant bgMix */
547 L_sum = L_mult(6554, st->cbGainHistory[2], pOverflow);
548 /* 0.2 in Q15; L_sum in Q17 */
550 for (i = 3; i < L_CBGAINHIST; i++)
552 L_sum = L_mac(L_sum, 6554, st->cbGainHistory[i], pOverflow);
554 cbGainMean = pv_round(L_sum, pOverflow); /* Q1 */
556 /* more smoothing in error and bg noise (NB no DFI used here) */
558 if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0)
559 && ((mode == MR475) || (mode == MR515)
562 /* 0.143 in Q15; L_sum in Q17 */
563 L_sum = L_mult(4681, st->cbGainHistory[0], pOverflow);
564 for (i = 1; i < L_CBGAINHIST; i++)
567 L_mac(L_sum, 4681, st->cbGainHistory[i], pOverflow);
569 cbGainMean = pv_round(L_sum, pOverflow); /* Q1 */
572 /* cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; */
574 L_sum = L_mult(bgMix, cbGainMix, pOverflow);
575 L_sum = L_mac(L_sum, 8192, cbGainMean, pOverflow);
576 L_sum = L_msu(L_sum, bgMix, cbGainMean, pOverflow);
577 cbGainMix = pv_round(L_shl(L_sum, 2, pOverflow), pOverflow); /* Q1 */