14f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/* ------------------------------------------------------------------
24f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * Copyright (C) 1998-2009 PacketVideo
34f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber *
44f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
54f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * you may not use this file except in compliance with the License.
64f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * You may obtain a copy of the License at
74f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber *
84f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
94f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber *
104f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * Unless required by applicable law or agreed to in writing, software
114f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
124f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
134f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * express or implied.
144f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * See the License for the specific language governing permissions
154f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * and limitations under the License.
164f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber * -------------------------------------------------------------------
174f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber */
184f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/****************************************************************************************
194f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas HuberPortions of this file are derived from the following 3GPP standard:
204f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
214f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    3GPP TS 26.173
224f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
234f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    Available from http://www.3gpp.org
244f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
254f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber(C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
264f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas HuberPermission to distribute, modify and use this file under the standard license
274f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huberterms listed above has been obtained from the copyright holder.
284f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber****************************************************************************************/
294f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*
304f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
314f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
324f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
334f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
344f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber Filename: agc2_amr_wb.cpp
354f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
364f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber     Date: 05/08/2007
374f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
384f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
394f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber REVISION HISTORY
404f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
414f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
424f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber Description:
434f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
444f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
454f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber INPUT AND OUTPUT DEFINITIONS
464f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
474f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber     int16 * sig_in,            (i)     : postfilter input signal
484f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber     int16 * sig_out,           (i/o)   : postfilter output signal
494f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber     int16 l_trm                (i)     : subframe size
504f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
514f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
524f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
534f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber FUNCTION DESCRIPTION
544f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
554f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    Performs adaptive gain control
564f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
574f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
584f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber REQUIREMENTS
594f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
604f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
614f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
624f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber REFERENCES
634f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
644f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
654f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber PSEUDO-CODE
664f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
674f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber------------------------------------------------------------------------------
684f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber*/
694f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
704f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
714f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
724f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; INCLUDES
734f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
744f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
754f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber#include "pvamrwbdecoder_cnst.h"
764f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber#include "pvamrwbdecoder_acelp.h"
774f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber#include "pv_amr_wb_type_defs.h"
784f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber#include "pvamrwbdecoder_basic_op.h"
794f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber#include "pvamrwb_math_op.h"
804f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
814f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
824f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; MACROS
834f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Define module specific macros here
844f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
854f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
864f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
874f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
884f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; DEFINES
894f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Include all pre-processor statements here. Include conditional
904f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; compile variables also.
914f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
924f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
934f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
944f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; LOCAL FUNCTION DEFINITIONS
954f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Function Prototype declaration
964f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
974f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
984f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
994f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; LOCAL STORE/BUFFER/POINTER DEFINITIONS
1004f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Variable declaration - defined here and used outside this module
1014f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
1024f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1034f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
1044f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; EXTERNAL FUNCTION REFERENCES
1054f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Declare functions defined elsewhere and referenced in this module
1064f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
1074f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1084f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
1094f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
1104f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; Declare variables used in this module but defined elsewhere
1114f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
1124f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1134f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber/*----------------------------------------------------------------------------
1144f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber; FUNCTION CODE
1154f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber----------------------------------------------------------------------------*/
1164f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1174f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Hubervoid agc2_amr_wb(
1184f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 * sig_in,          /* (i)     : postfilter input signal  */
1194f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 * sig_out,         /* (i/o)   : postfilter output signal */
1204f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 l_trm              /* (i)     : subframe size            */
1214f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber)
1224f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber{
1234f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1244f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 i, exp;
1254f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 gain_in, gain_out, g0;
1264f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int32 s;
1274f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1284f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    int16 temp;
1294f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1304f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    /* calculate gain_out with exponent */
1314f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1324f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    temp = sig_out[0] >> 2;
1334f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    s = fxp_mul_16by16(temp, temp) << 1;
1344f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    for (i = 1; i < l_trm; i++)
1354f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1364f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        temp = sig_out[i] >> 2;
1374f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = mac_16by16_to_int32(s, temp, temp);
1384f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1394f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1404f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1414f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    if (s == 0)
1424f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1434f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        return;
1444f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1454f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    exp = normalize_amr_wb(s) - 1;
1464f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    gain_out = amr_wb_round(s << exp);
1474f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1484f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    /* calculate gain_in with exponent */
1494f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1504f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    temp = sig_in[0] >> 2;
1514f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    s = mul_16by16_to_int32(temp, temp);
1524f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    for (i = 1; i < l_trm; i++)
1534f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1544f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        temp = sig_in[i] >> 2;
1554f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = mac_16by16_to_int32(s, temp, temp);
1564f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1574f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1584f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1594f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    if (s == 0)
1604f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1614f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        g0 = 0;
1624f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1634f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    else
1644f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1654f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        i = normalize_amr_wb(s);
1664f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        gain_in = amr_wb_round(s << i);
1674f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        exp -= i;
1684f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1694f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        /*
1704f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber         *  g0 = sqrt(gain_in/gain_out)
1714f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber         */
1724f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1734f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = div_16by16(gain_out, gain_in);
1744f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = shl_int32(s, 7);                   /* s = gain_out / gain_in */
1754f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = shr_int32(s, exp);                 /* add exponent */
1764f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1774f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        s = one_ov_sqrt(s);
1784f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        g0 = amr_wb_round(shl_int32(s, 9));
1794f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1804f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    /* sig_out(n) = gain(n) sig_out(n) */
1814f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1824f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    for (i = 0; i < l_trm; i++)
1834f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    {
1844f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber        sig_out[i] = extract_h(shl_int32(fxp_mul_16by16(sig_out[i], g0), 3));
1854f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1864f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    }
1874f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
1884f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber    return;
1894f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber}
1904f1efc098cb5791c3e9f483f2af84aef70d2d0a0Andreas Huber
191