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