1e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/*
2e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Copyright 2003-2010, VisualOn, Inc.
3e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
4e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Licensed under the Apache License, Version 2.0 (the "License");
5e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** you may not use this file except in compliance with the License.
6e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** You may obtain a copy of the License at
7e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
8e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **     http://www.apache.org/licenses/LICENSE-2.0
9e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
10e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Unless required by applicable law or agreed to in writing, software
11e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** distributed under the License is distributed on an "AS IS" BASIS,
12e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** See the License for the specific language governing permissions and
14e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** limitations under the License.
15e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard */
16e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
17e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/**************************************************************************
18e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      File: gpclip.c                                                     *
19e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*                                                                         *
20e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      Description:To avoid unstable synthesis on frame erasure, the gain *
21e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      need to be limited(gain pitch < 1.0) when the following            *
22e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      case occurs                                                        *
23e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      a resonance on LPC filter(lp_disp < 60Hz)                          *
24e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard*      a good pitch prediction (lp_gp > 0.95)                             *
25b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard*                                                                         *
26e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard***************************************************************************/
27e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#include "typedef.h"
28e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#include "basic_op.h"
29e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
30e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#define DIST_ISF_MAX    307                /* 120 Hz (6400Hz=16384) */
31e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#define DIST_ISF_THRES  154                /* 60     (6400Hz=16384) */
32e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#define GAIN_PIT_THRES  14746              /* 0.9 in Q14 */
33e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#define GAIN_PIT_MIN    9830               /* 0.6 in Q14 */
34e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#define M               16
35e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
36e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
37e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgardvoid Init_gp_clip(
385d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 mem[]                          /* (o) : memory of gain of pitch clipping algorithm */
395d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        )
40e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard{
415d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    mem[0] = DIST_ISF_MAX;
425d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    mem[1] = GAIN_PIT_MIN;
43e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard}
44e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
45e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
46e2e838afcf03e603a41a0455846eaf9614537c16Mans RullgardWord16 Gp_clip(
475d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 mem[]                          /* (i/o) : memory of gain of pitch clipping algorithm */
485d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen          )
49e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard{
505d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    Word16 clip = 0;
515d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    if ((mem[0] < DIST_ISF_THRES) && (mem[1] > GAIN_PIT_THRES))
525d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        clip = 1;
53e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
545d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    return (clip);
55e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard}
56e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
57e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
58e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgardvoid Gp_clip_test_isf(
595d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 isf[],                         /* (i)   : isf values (in frequency domain)           */
605d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 mem[]                          /* (i/o) : memory of gain of pitch clipping algorithm */
615d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        )
62e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard{
635d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    Word16 dist, dist_min;
645d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    Word32 i;
65e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
665d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    dist_min = vo_sub(isf[1], isf[0]);
67e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
685d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    for (i = 2; i < M - 1; i++)
695d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    {
705d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        dist = vo_sub(isf[i], isf[i - 1]);
715d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        if(dist < dist_min)
725d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        {
735d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen            dist_min = dist;
745d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        }
755d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    }
76e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
775d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    dist = extract_h(L_mac(vo_L_mult(26214, mem[0]), 6554, dist_min));
78e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
795d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    if (dist > DIST_ISF_MAX)
805d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    {
815d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        dist = DIST_ISF_MAX;
825d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    }
835d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    mem[0] = dist;
84e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
855d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    return;
86e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard}
87e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
88e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
89e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgardvoid Gp_clip_test_gain_pit(
905d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 gain_pit,                      /* (i) Q14 : gain of quantized pitch                    */
915d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        Word16 mem[]                          /* (i/o)   : memory of gain of pitch clipping algorithm */
925d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        )
93e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard{
945d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    Word16 gain;
955d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    Word32 L_tmp;
965d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    L_tmp = (29491 * mem[1])<<1;
975d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    L_tmp += (3277 * gain_pit)<<1;
985d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen
995d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    gain = extract_h(L_tmp);
1005d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen
1015d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    if(gain < GAIN_PIT_MIN)
1025d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    {
1035d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen        gain = GAIN_PIT_MIN;
1045d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    }
1055d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    mem[1] = gain;
1065d5c3a132bb446ac78a37dfaac24a46cacf0dd73Marco Nelissen    return;
107e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard}
108e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
109e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
110e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
111