📄 util2.c
字号:
/*
**
** File: util2.c
**
** Description: utility functions for the lbc codec
**
** Functions:
**
** I/O functions:
**
** Read_lbc()
** Write_lbc()
**
** High-pass filtering:
**
** Rem_Dc()
**
** Miscellaneous signal processing functions:
**
** Vec_Norm()
** Mem_Shift()
** Scale()
**
** Bit stream packing/unpacking:
**
** Line_Pack()
** Line_Unpk()
**
** Mathematical functions:
**
** Rand_lbc()
*/
/*
ITU-T G.723.1 Floating Point Speech Coder ANSI C Source Code. Version 5.1F
Original fixed-point code copyright (c) 1995,
AudioCodes, DSP Group, France Telecom, Universite de Sherbrooke.
All rights reserved.
Floating-point code copyright (c) 1995,
Intel Corporation and France Telecom (CNET).
All rights reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include "typedef2.h"
#include "cst2.h"
#include "lbccode2.h"
#include "coder2.h"
#include "decod2.h"
#include "util2.h"
/*
**
** Function: Read_lbc()
**
** Description: Read in a file
**
** Links to text: Sections 2.2 & 4
**
** Arguments:
**
** FLOAT *Dpnt
** int Len
** FILE *Fp
**
** Outputs:
**
** FLOAT *Dpnt
**
** Return value: None
**
*/
void Read_lbc (FLOAT *Dpnt, int Len, FILE *Fp)
{
Word16 Ibuf[Frame];
int i,n;
n = fread (Ibuf, sizeof(Word16), Len, Fp);
for (i=0; i<n; i++)
Dpnt[i] = (FLOAT) Ibuf[i];
for (i=n; i<Len; i++)
Dpnt[i] = (FLOAT)0.0;
}
/*
**
** Function: Write_lbc()
**
** Description: Write a file
**
** Links to text: Section
**
** Arguments:
**
** FLOAT *Dpnt
** int Len
** FILE *Fp
**
** Outputs: None
**
** Return value: None
**
*/
void Write_lbc(FLOAT *Dpnt, int Len, FILE *Fp)
{
Word16 Obuf[Frame];
int i;
for (i=0; i<Len; i++)
{
if (Dpnt[i] < (FLOAT)-32767.5)
Obuf[i] = -32768L;
else if (Dpnt[i] > (FLOAT)32766.5)
Obuf[i] = 32767;
else
{
if (Dpnt[i] < 0)
Obuf[i] = (Word16) (Dpnt[i]-(FLOAT)0.5);
else
Obuf[i] = (Word16) (Dpnt[i]+(FLOAT)0.5);
}
}
fwrite(Obuf, sizeof(Word16), Len, Fp);
}
void Line_Wr(char *Line, FILE *Fp)
{
Word16 Info;
int Size;
Info = (Word16) (Line[0] & 0x0003);
/* Check frame type and rate information */
switch (Info) {
case 0x0002 : { /* SID frame */
Size = 4;
break;
}
case 0x0003 : { /* untransmitted silence frame */
Size = 1;
break;
}
case 0x0001 : { /* active frame, low rate */
Size = 20;
break;
}
default : { /* active frame, high rate */
Size = 24;
}
}
fwrite(Line, Size, 1, Fp);
}
int Line_Rd(char *Line, FILE *Fp)
{
Word16 Info;
int Size;
if (fread(Line, 1,1, Fp) != 1)
return (-1);
Info = (Word16) (Line[0] & 0x0003);
/* Check frame type and rate information */
switch (Info) {
/* Active frame, high rate */
case 0 : {
Size = 23;
break;
}
/* Active frame, low rate */
case 1 : {
Size = 19;
break;
}
/* Sid Frame */
case 2 : {
Size = 3;
break;
}
/* untransmitted */
default : {
return 0;
}
}
fread(&Line[1], Size, 1, Fp);
return 0;
}
/*
**
** Function: Rem_Dc()
**
** Description: High-pass filtering
**
** Links to text: Section 2.3
**
** Arguments:
**
** FLOAT *Dpnt
**
** Inputs:
**
** CodStat.HpfZdl FIR filter memory from previous frame (1 word)
** CodStat.HpfPdl IIR filter memory from previous frame (1 word)
**
** Outputs:
**
** FLOAT *Dpnt
**
** Return value: None
**
*/
void Rem_Dc(FLOAT *Dpnt)
{
int i;
FLOAT acc0;
if (UseHp)
{
for (i=0; i < Frame; i++)
{
acc0 = Dpnt[i] - CodStat.HpfZdl;
CodStat.HpfZdl = Dpnt[i];
Dpnt[i] = CodStat.HpfPdl =
acc0 + CodStat.HpfPdl*((FLOAT)127.0/(FLOAT)128.0);
}
}
}
/*
**
** Function: Mem_Shift()
**
** Description: Memory shift, update of the high-passed input speech signal
**
** Links to text:
**
** Arguments:
**
** FLOAT *PrevDat
** FLOAT *DataBuff
**
** Outputs:
**
** FLOAT *PrevDat
** FLOAT *DataBuff
**
** Return value: None
**
*/
void Mem_Shift(FLOAT *PrevDat, FLOAT *DataBuff)
{
int i;
FLOAT Dpnt[Frame+LpcFrame-SubFrLen];
/* Form Buffer */
for (i=0; i < LpcFrame-SubFrLen; i++)
Dpnt[i] = PrevDat[i];
for (i=0; i < Frame; i++)
Dpnt[i+LpcFrame-SubFrLen] = DataBuff[i];
/* Update PrevDat */
for (i=0; i < LpcFrame-SubFrLen; i++)
PrevDat[i] = Dpnt[Frame+i];
/* Update DataBuff */
for (i=0; i < Frame; i++)
DataBuff[i] = Dpnt[(LpcFrame-SubFrLen)/2+i];
}
/*
**
** Function: Line_Pack()
**
** Description: Packing coded parameters in bitstream of 16-bit words
**
** Links to text: Section 4
**
** Arguments:
**
** LINEDEF *Line Coded parameters for a frame
** char *Vout bitstream chars
** Word16 Ftyp Voice Activity Indicator
**
** Outputs:
**
** Word16 *Vout
**
** Return value: None
**
*/
void Line_Pack(LINEDEF *Line, char *Vout, Word16 Ftyp)
{
int i;
int BitCount;
Word16 BitStream[192];
Word16 *Bsp = BitStream;
Word32 Temp;
/* Clear the output vector */
for ( i = 0 ; i < 24 ; i ++ )
Vout[i] = 0 ;
/*
* Add the coder rate info and frame type info to the 2 msb
* of the first word of the frame.
* The signaling is as follows:
* Ftyp WrkRate => X1X0
* 1 Rate63 00 : High Rate
* 1 Rate53 01 : Low Rate
* 2 x 10 : Silence Insertion Descriptor frame
* 0 x 11 : Used only for simulation of
* untransmitted silence frames
*/
switch (Ftyp) {
case 0 : {
Temp = 0x00000003L;
break;
}
case 2 : {
Temp = 0x00000002L;
break;
}
default : {
if ( WrkRate == Rate63 )
Temp = 0x00000000L;
else
Temp = 0x00000001L;
break;
}
}
/* Serialize Control info */
Bsp = Par2Ser( Temp, Bsp, 2 ) ;
/* Check for Speech/NonSpeech case */
if ( Ftyp == 1 ) {
/* 24 bit LspId */
Temp = (*Line).LspId ;
Bsp = Par2Ser( Temp, Bsp, 24 ) ;
/*
* Do the part common to both rates
*/
/* Adaptive code book lags */
Temp = (Word32) (*Line).Olp[0] - (Word32) PitchMin ;
Bsp = Par2Ser( Temp, Bsp, 7 ) ;
Temp = (Word32) (*Line).Sfs[1].AcLg ;
Bsp = Par2Ser( Temp, Bsp, 2 ) ;
Temp = (Word32) (*Line).Olp[1] - (Word32) PitchMin ;
Bsp = Par2Ser( Temp, Bsp, 7 ) ;
Temp = (Word32) (*Line).Sfs[3].AcLg ;
Bsp = Par2Ser( Temp, Bsp, 2 ) ;
/* Write combined 12 bit index of all the gains */
for ( i = 0 ; i < SubFrames ; i ++ ) {
Temp = (*Line).Sfs[i].AcGn*NumOfGainLev + (*Line).Sfs[i].Mamp ;
if ( WrkRate == Rate63 )
Temp += (Word32) (*Line).Sfs[i].Tran << 11 ;
Bsp = Par2Ser( Temp, Bsp, 12 ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -