os/mm/mmlibs/mmfw/Codecs/Src/Gsm610CodecCommon/codec.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "rpeltp.h"
    17 #include "codec.h"
    18 #include "gsm610fr.h"
    19 
    20 /*
    21 ** LPC analysis part of the RPE-LTP-coder
    22 **
    23 ** Input:
    24 **      sop[0..159]
    25 **              unprocessed sample frame 
    26 ** Output:
    27 **      d[0..159]
    28 **              residual 
    29 **      LARc[0..7]
    30 **              coded reflection coefficients 
    31 **      *SP_flag:
    32 **              decision of tx DTX (boolean)
    33 **      *VAD_flag:
    34 **              decision of VAD (boolean)
    35 **
    36 ** Return value:
    37 **      None
    38 */
    39 void LPC_analysis(CGSM610FR_Encoder* aEncoder, int2 ibuf[], struct codes *ecodes)
    40 {
    41 
    42   int4 L_ACF[9];
    43   int2 LAR[8]; /* used for r[], LAR[], LARpp */
    44   int2 rp[8]; /* used for LARp[], rp[] */
    45 //  int2 scalauto; /* returned from autoc to be used by vad */
    46 
    47 
    48   prepr( aEncoder, ibuf, ibuf ); /* sof <- so */
    49   preemp( aEncoder, ibuf, ibuf ); /* s <- sof */
    50 //  scalauto = autoc( L_ACF, ibuf ); /* L_ACF <- , s <- s */
    51   autoc( L_ACF, ibuf ); /* L_ACF <- , s <- s */
    52 
    53   /*
    54   ** VAD decision could be computed here when L_ACF and scalauto are available.
    55   ** In current version vad is executed after LAR computation
    56   */
    57 
    58   schur( LAR, L_ACF ); /* r <- L_ACF */
    59 
    60   larcomp( LAR, LAR ); /* LAR <- r */
    61 
    62   codlar( ecodes->LARc, LAR ); /* LARc <- LAR */
    63   declar( LAR, ecodes->LARc ); /* LARpp <- LARc */
    64 
    65   cparc1( rp, aEncoder->LARpp_prev, LAR ); /* LARp <- LARpp_prev, LARpp */
    66   crp( rp, rp ); /* rp <- LARp */
    67   invfil( aEncoder, ibuf, ibuf, rp, 0, 12 ); /* d <- s */
    68 
    69   cparc2( rp, aEncoder->LARpp_prev, LAR ); /* LARp <- LARpp_prev, LARpp */
    70   crp( rp, rp ); /* rp <- LARp */
    71   invfil( aEncoder, ibuf, ibuf, rp, 13, 26 ); /* d <- s */
    72 
    73   cparc3( rp, aEncoder->LARpp_prev, LAR ); /* LARp <- LARpp_prev, LARpp */
    74   crp( rp, rp ); /* rp <- LARp */
    75   invfil( aEncoder, ibuf, ibuf, rp, 27, 39 ); /* d <- s*/
    76 
    77   cparc4( rp, aEncoder->LARpp_prev, LAR ); /* LARp <- LARpp_prev, LARpp */
    78   crp( rp, rp ); /* rp <- LARp */
    79   invfil( aEncoder, ibuf, ibuf, rp, 40, 159 ); /* d <- s */
    80   return;
    81 }
    82 
    83 /*
    84 ** Encoding of the residual signal of the LPC analysis filter
    85 ** Input:
    86 **
    87 ** d[k_start..k_start+39]
    88 **      LPC residual (output of LPC analysis filter)
    89 **
    90 ** Output:
    91 **
    92 ** bc, Nc
    93 **      encoded LTP parameters (gain and lag)
    94 ** xmaxc
    95 **      block maximum of the encoded subframe.
    96 ** xMc[0..12]
    97 **              coded normalized RPE pulses
    98 **
    99 ** return xmax for SID computation
   100 */
   101 int2 residual_encoder( CGSM610FR_Encoder* aEncoder, int sf_nro, int2 d[], struct sfcodes *sfc )
   102 {
   103   int k_start;
   104   int2 xmax; /* return value */
   105 
   106   /* Note: d[] is used also for for x[] and e[] */
   107 
   108   int2 xM[13]; /* used for xM[], xMp[] */
   109   /* xM[] is required simultaneously with xMc because LTP
   110    * also decodes xMc
   111    */
   112   int2 dpp[40]; /* required simultaneously with ep[] that is
   113 		 * stored into d[]
   114 		 */
   115   int2 Exp;
   116   int2 mant;
   117 
   118   k_start = sf_nro * 40;
   119 
   120   ltpcomp( aEncoder, &(sfc->Nc), &(sfc->bc), d, k_start );
   121   ltpfil( aEncoder, &d[k_start], dpp, d, sfc->bc, sfc->Nc, k_start ); /* e, dpp <- d */
   122   weight( &d[k_start], &d[k_start] ); /* x <- e */
   123   sfc->Mc = gridsel( xM, &d[k_start] ); /* xM <- x */
   124 
   125   /*
   126   ** quatize residual and store unquantized xmax for SID
   127   ** computation
   128   */
   129   xmax = apcm( &(sfc->xmaxc), xM, sfc->xMc, &Exp, &mant);
   130   /* EXP, mant computed int APCM */
   131 
   132   iapcm( xM, sfc->xMc, Exp, mant ); /* xMp <- xMc */
   133   gridpos( &d[k_start], xM, sfc->Mc ); /* ep <- xMc,Mc */
   134   ltpupd( aEncoder, dpp, &d[k_start] ); /* dp <- dpp, x */
   135 
   136   return( xmax );
   137 }
   138 
   139 
   140 /*
   141 ** Decoding of the coded LPC-residual
   142 **
   143 ** Input:
   144 ** xmaxcr
   145 **      coded block maxmimum
   146 ** xMcr[0..12]
   147 **      coded normalized RPE pulses
   148 **
   149 ** Output:
   150 ** drp[k_start..k_start+39]
   151 **      decoded LPC residual (input signal for LPC-synthesis filter)
   152 */
   153 void residual_decoder(CGSM610FR_Decoder* aDecoder, int sf_nro, struct sfcodes *sfc, int2 wt[])
   154 {
   155   int k_start;
   156 
   157   int2 EXP;
   158   int2 mant;
   159   int2 xMrp[13];
   160   int2 erp[40];
   161 
   162   k_start = sf_nro * 40;
   163 
   164   /* in decoder EXP ja mant must be computed from xmaxcr */
   165   expman( &EXP, &mant, sfc->xmaxc ); /* EXP, mant <- xmaxc */
   166   iapcm( xMrp, sfc->xMc, EXP, mant ); /* xMrp <- xMc */
   167   gridpos( erp, xMrp, sfc->Mc ); /* erp <- xMc,Mc */
   168 	
   169   ltpsyn( aDecoder, erp, &wt[k_start], sfc->bc, sfc->Nc );
   170 }
   171 
   172 /*
   173 ** LPC synthesis part of the RPE-LTP-coder
   174 **
   175 ** Input:
   176 **      LARcr[0..7]
   177 **              coded reflection coefficients
   178 **      wt[0..159]
   179 **              decoded residual
   180 **
   181 ** Output:
   182 **      srop[0..159]
   183 **              decoded speech
   184 */
   185 void LPC_synthesis(CGSM610FR_Decoder* aDecoder, struct codes *dcodes, int2 wt[], int2 obuf[])
   186 {
   187   int2 LARr[8]; /* used for LARr[], LARpp */
   188   int2 rrp[8]; /* used for LARp[], rrp[] */
   189 
   190   declar(LARr, dcodes->LARc); /* LARrpp <- LARc */
   191 
   192   cparc1( rrp, aDecoder->LARrpp_prev, LARr ); /* LARp <- LARrpp_prev, LARr */
   193   crp( rrp, rrp ); /* rrp <- LARp */
   194   synfil( aDecoder, obuf, wt, rrp, 0, 12 ); /* sr <- wt */
   195 
   196   cparc2( rrp, aDecoder->LARrpp_prev, LARr ); /* LARp <- LARrpp_prev, LARr */
   197   crp( rrp, rrp ); /* rrp <- LARp */
   198   synfil( aDecoder, obuf, wt, rrp, 13, 26 ); /* sr <- wt */
   199 
   200   cparc3( rrp, aDecoder->LARrpp_prev, LARr ); /* LARp <- LARrpp_prev, LARr */
   201   crp( rrp, rrp ); /* rrp <- LARp */
   202   synfil( aDecoder, obuf, wt, rrp, 27, 39 ); /* sr <- wt */
   203 
   204   cparc4( rrp, aDecoder->LARrpp_prev, LARr ); /* LARp <- LARrpp_prev, LARr */
   205   crp( rrp, rrp ); /* rrp <- LARp */
   206   synfil( aDecoder, obuf, wt, rrp, 40, 159 ); /* sr <- wt */
   207   postpr( aDecoder, obuf, obuf );
   208   /* combines deemphasis, upscaling and truncation */
   209 
   210 }
   211 
   212 
   213 /*
   214 ** RPE-LTP Encoder
   215 **
   216 ** void RPELTP_encoder(CGSM610FR_Encoder* aEncoder, int2 ibuf[], struct codes *ecodes)
   217 **
   218 ** Input:
   219 **      ibuf[0..159]
   220 **              Original speech to be coded
   221 **
   222 ** Output:
   223 **      ecodes
   224 **              encoded speech stored as codewords
   225 */
   226 void RPELTP_encoder(CGSM610FR_Encoder* aEncoder, int2 ibuf[], struct codes *ecodes)
   227 {
   228   int i;
   229 //  int2 xmax[4]; /* collect unquantized xmax data for dtx */	
   230 
   231   LPC_analysis( aEncoder, ibuf, ecodes );
   232   for (i = 0; i < 4; i++)
   233 //    xmax[i] = residual_encoder( aEncoder, i, ibuf, &(ecodes->sfc[i]) );	
   234     residual_encoder( aEncoder, i, ibuf, &(ecodes->sfc[i]) );
   235 }
   236 
   237 
   238 /*
   239 ** RPE-LTP Decoder
   240 **
   241 ** void RPELTP_decoder(struct codes *dcodes, int2 obuf[])
   242 ** Input:
   243 **      dcodes
   244 **              encoded speech stored as codewords to be decoded
   245 **
   246 ** Output:
   247 **      obuf[0..159]
   248 **              Decoded speech
   249 */
   250 void RPELTP_decoder(CGSM610FR_Decoder* aDecoder, struct codes *dcodes, int2 obuf[])
   251 {
   252   int i;
   253 
   254   dcodes->LARc[0] &= 0x7fff; /* VAD flag in sequences */
   255   dcodes->LARc[1] &= 0x7fff; /* SP flag in sequences */
   256 
   257   for (i = 0; i < 4; i++)
   258     residual_decoder(aDecoder, i, &(dcodes->sfc[i]), obuf); /* -> wt */
   259 
   260   LPC_synthesis(aDecoder, dcodes, obuf, obuf); /* wt -> srop */
   261 }
   262