GSMAMR voice encoder
1. Introduction to GSMAMR CODEC
Digital cellular system adaptive multi-rate speech transmission decoder (Adaptive Multi-Rate Speech Codec: AMR) is the SMG11 (Special Mobile Group11) subordinated by the European Telecommunication Standardization Association (Special Mobile Group11) for the GSM system. Its purpose is to obtain a wired sound-coded quality in the case of half rate channel capacity. As the fourth-generation GSM voice codec standard, AMR provides an adaptive solution to track rapidly changing wireless channel conditions and local traffic. Today's GSM voice and channel encoder work on fixed code rates, which are selected in the design phase, which is an ideal channel performance and channel error robustness. On the other hand, the AMR encoder selects one of a variety of code rates in real time according to the channel type (full rate or half rate), thereby achieving the optimal combination of speech coding and channel coding to meet transient wireless channel conditions and local capacity requirements. . AMR provides a variety of code rate options from 4.75kbits / s to 12.2kbits / s. AMR has become the third generation system candidate coding of UMTS and ITU with its excellent performance.
Second, GSMAMR CODEC encoding method
GSMAMR CODEC is suitable for encoding 8,000 samples, 16-bit, mono audio data per second. At 16-bit sampling accuracy, each sample value is half-word, and linear continuously. GSMAMR CODEC encodes one frame data (160 sample values) each time, depending on the different coding rate, the length of the content after the encoding is different, see Table 1.
Rate encoding length (bit) after encoding length (bytes) 4.75 kbps95125.15 kbps103135.9 kbps118156.7 kbps134177.4 kbps148197.95 kbps1592010.2 kbps2042612.2 kbps (GSM EFR) 24431
Table I
After the frame voice data is encoded, a corresponding GSMAMR data frame can be formed, as shown below:
Frame Type (16 BIT) Speech Rate (16 Bit) Speech Content (n * 8 bit)
Frame Type accounts for a half word, sixteen, indicates a frame type.
Frame type value Non_SPEECH-1SPEECH_GOOD0SPEECH_DEGRADED1ONSET2SPEECH_BAD3SID_FIRST4SID_UPDATE5SID_FIRST4SID_UPDATE5SID_BAD6NO_DATA7N_FRAMETYPES8
Speech Rate accounts for one half word, sixteen, indicating the coding rate.
Code rate value Rate_undefined-1rate_47500rate_51501rate_59002rate_67003rate_74004rate_79505rate_102006rate_122007rate_dtx8
The Speech Content length is determined by Frame Type and Speech Rate, see Table 1 for details.
Third, GSMAMR CODEC code analysis
AMR CODEC API related source files have eight, as shown in the following table:
File Name Function BitPack.c Pixel Package / Unpacking Function Encgusmamr.cgsmamr Code Function Decgsmamr.cgsmamr Decoding Function DTX_UTIL.CDTX Function GAIN_UTIL.C Gain Function Tab_UTIL.CGSMAMR Code Parameter Table Util.c Auxiliary Function OwngSmamr.h Constant Definition, Type Definition and inline function definition
Where ENCGSMAMR.C and Decgsmamr.c are main files, the programmer is mainly used as follows:
Code class:
============================================================================================================================================================================================================= ==================== Apigsmamr_status apigsmamrencoder_getsize
(Gsmamrencoder_obj * encoderobj, unsigned int * pcodecsize)
Function: Get the size of the GSMAMR encoding object.
Parameter definition:
GSmamRencoder_obj * encoderobj
GSMAMR encoding object pointer.
Unsigned int * PCODECSIZE
Store the size of the GSMAMR encoding object.
Code analysis:
GSmamRencoder_obj is defined as follows:
Struct _Gsmamrencoder_obj {
Gsmamrcoder_obj objprm;
/ * Preprocess State * /
Char * preproc; / * High Pass pre processing filter memory * /
Encoder_State COD_AMR_STATE;
GSmamr_Rate_t Rate; / * encode rate * /
}
The gsmamrcoder_obj in the GSmamRencoder_Obj structure is defined as follows:
Typedef struct _gsmamrcoder_obj {
Int objsize;
Int key;
INT MODE; / * ENCODER MODE's * /
GSMAMRCODEC_TYPE CODECTYPE;
} GSMAMRCODER_OBJ;
The member variable Objsize is the size of the GSMAMR encoding object, which is initialized in the function APIGSMAMRENCODER_INIT.
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRencoder_alloc
(const gsmamrenc_params * gsm_params, unsigned int * psizetab)
Function: How much memory area needs to be assigned a GSMAMR encoding object.
Parameter definition:
Const gsmamrenc_params * GSM_PARAMS
GSMAMR encoding parameter pointer.
Unsigned int * psizetab
Store the size of the memory area required for the GSMAMR encoding object.
Code analysis: The GSMAMR encoding parameter structure is defined as follows:
Typedef struct_gsmamrenc_params {
GSMAMRCODEC_TYPE CODECTYPE;
INT MODE;
Gsmamrenc_params;
Gsmamrcodec_type is an enumeration type, indicating that the encoder type is GSMAMR
Typedef enum _gsmamrcodec_type {
GSMAMR_CODEC = 0
} GSMAMRCODEC_TYPE;
Mode represents the encoding mode of the GSMAMR encoder
Typedef enum _gsmamrencode_mode {
Gsmamrencode_defaultmode = 0, / * Default mode * /
GSmamRencode_vad1_enabled, / * VAD1 mode * /
GSmamRencode_vad2_enabled / * VAD2 mode * /
} GSMAMRENCODE_MODE;
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRencoder_init
(Gsmamrencoder_obj * encoderobj, unsigned int mode)
Function: Initializing GSMAMR encoding object
Parameter definition:
GSmamRencoder_obj * encoderobj
GSMAMR encoding object pointer.
Unsigned int mode
GSMAMR encoding mode, refer to the following definition
Code analysis:
The encoding mode is one of the following:
Typedef enum _gsmamrencode_mode {
Gsmamrencode_defaultmode = 0, / * Default mode * /
GSmamRencode_vad1_enabled, / * VAD1 mode * /
GSmamRencode_vad2_enabled / * VAD2 mode * /
} GSMAMRENCODE_MODE;
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRencode
(Gsmamrencoder_obj * encoderobj, const short * src, gsmamr_rate_t rate,
Unsigned char * dst, int * pvad)
Function: GSMAMR encoding parameter definition and analysis:
GSmamRencoder_obj * encoderobj
GSMAMR encoding object pointer.
Const Short * SRC
The pointer to which the encoded content is required. Since the GSMAMR encodes 160 samples (20 ms long speech content at 8000 Hz), the pointer length is 160.
GSMAMR_RATE_T RATE
The coding rate is used, referring to the following definition
Typedef enum {
Gsmamr_rate_undefined = -1,
GSMAMR_RATE_4750 = 0, / * MR475 4.75 kbps * /
GSMAMR_RATE_5150, / * MR515 5.15 kbps * /
GSMAMR_RATE_5900, / * MR59 5.9 Kbps * /
GSMAMR_RATE_6700, / * MR67 6.7 Kbps * /
GSMAMR_RATE_7400, / * MR74 7.4 Kbps * /
GSMAMR_RATE_7950, / * MR795 7.95 kbps * /
GSMAMR_RATE_10200, / * MR102 10.2 Kbps * /
GSMAMR_RATE_122200, / * MR122 12.2 Kbps (GSM EFR) * /
GSMAMR_RATE_DTX / * MRDTX Discontinuous TX Mode * /
} GSMAMR_RATE_T;
Unsigned char * DST
Store encoded data.
INT * PVAD
Returns whether the encoded data frame is valid, if it is valid, return 1, otherwise returns 0.
Decoding class:
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRDecoder_getsize
(Gsmamrdecoder_obj * decoderobj, unsigned int * pcodecsize)
Function: Get the size of the GSMAMR decoded object.
Parameter definition:
Gsmamrdecoder_obj * decoderobj
GSMAMR decoded object pointer.
Unsigned int * PCODECSIZE
Store the size of the size of the GSMAMR decoded object.
Code analysis:
The GSmamrdecoder_Obj structure is defined as follows:
Struct _Gsmamrdecoder_obj {
/ * post process state * /
Gsmamrcoder_obj objprm;
Char * Postproc; / * High Pass Post Processing Filter Memory * /
Decoder_State Decoder_amrstate;
/ * Postfiltering State * /
Post_filterstate filt_state; gsmamr_rate_t rate; / * decode rate * /
}
The gsmamrcoder_obj in the GSmamrdecoder_Obj structure is defined as follows:
Typedef struct _gsmamrcoder_obj {
Int objsize;
Int key;
INT MODE; / * ENCODER MODE's * /
GSMAMRCODEC_TYPE CODECTYPE;
} GSMAMRCODER_OBJ;
The member variable Objsize is the size of the GSMAMR decoded object, which is initialized in the function APIGSMAMRDECODER_INIT.
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRDecoder_alloc
(const gsmamrdec_params * gsm_params, unsigned int * psizetab)
Function: Calculate how much memory area for GSMAMR decoding objects need to be assigned.
Parameter definition:
Const gsmamrdec_params * GSM_PARAMS
GSMAMR decoding parameter pointer.
Unsigned int * psizetab
Store the size of the memory area required for the GSMAMR decoding object.
Code analysis:
The GSMAMR decoding parameter structure is defined as follows:
Typedef struct_gsmamrdec_params {
GSMAMRCODEC_TYPE CODECTYPE;
INT MODE;
Gsmamrdec_params;
Gsmamrcodec_type is an enumeration type, indicating that the decoder type is GSMAMR
Typedef enum _gsmamrcodec_type {
GSMAMR_CODEC = 0
} GSMAMRCODEC_TYPE;
Mode represents the decoding mode of the GSMAMR decoder
TypedEf enum _gsmamrdecode_mode {
Gsmamrdecode_defaultmode = 0
} Gsmamrdecode_mode;
============================================================================================================================================================================================================= ====================
Apigsmamr_Status ApigsmamRDecoder_init
(Gsmamrdecoder_obj * decoderobj, unsigned int mode) Function: Initializing GSMAMR decoding object
Parameter definition and analysis:
Gsmamrdecoder_obj * decoderobj
GSMAMR encoding object pointer.
Unsigned int mode
GSMAMR encoding mode, refer to the following definition:
TypedEf enum _gsmamrdecode_mode {
Gsmamrdecode_defaultmode = 0
} Gsmamrdecode_mode;
============================================================================================================================================================================================================= ====================
Apigsmamr_Status Apigsmamrdecode
(Gsmamrdecoder_obj * decoderobj, const unsigned char * src,
GSMAMR_RATE_T RATE, RXFRAMEPE RX_TYPE, SHORT * DST)
Function: GSMAMR decoding
Gsmamrdecoder_obj * decoderobj
GSMAMR decoded object pointer.
Const unsigned char * src
The pointer to which the decoded content is required, depending on the coding rate, the length is different.
GSMAMR_RATE_T RATE
The decoding rate is used, referring to the following definition
Typedef enum {
Gsmamr_rate_undefined = -1,
GSMAMR_RATE_4750 = 0, / * MR475 4.75 kbps * /
GSMAMR_RATE_5150, / * MR515 5.15 kbps * /
GSMAMR_RATE_5900, / * MR59 5.9 Kbps * /
GSMAMR_RATE_6700, / * MR67 6.7 Kbps * /
GSMAMR_RATE_7400, / * MR74 7.4 Kbps * /
GSMAMR_RATE_7950, / * MR795 7.95 kbps * /
GSMAMR_RATE_10200, / * MR102 10.2 Kbps * /
GSMAMR_RATE_122200, / * MR122 12.2 Kbps (GSM EFR) * /
GSMAMR_RATE_DTX / * MRDTX Discontinuous TX Mode * /
} GSMAMR_RATE_T;
RxFrameType RX_TYPE
The type of encoded data frame received, refer to the following definition
Typedef enum {
RX_NON_SPEECH = -1,
RX_SPEECH_GOOD = 0,
RX_SPEECH_DEGRADED,
Rx_onset,
RX_SPEECH_BAD,
RX_SID_FIRST,
RX_SID_UPDATE,
RX_SID_BAD,
RX_NO_DATA,
RX_N_FRAMEPES / * NUMBER OF FRAME TYPES * /
} Rxframetype; short * DST
The pointer to the decoded content is stored, since the GSMAMR is decoded to generate 160 sample values (20 ms long speech content at 8000 Hz), the pointer length is 160.
Four, GSMAMR CODEC on SITSANG board
In order to use GSMAMR CODEC on the SITSANG board, we have made it a dynamic library, and makefile is as follows.
######################################################################################################################################################################################################################################################################################################## #########
# Change the definitions accounting to your system setting #
######################################################################################################################################################################################################################################################################################################## #########
IFNDEF IPPROOT
IpPROOT: = / usr / local / ipp / ippxsc30
ENDIF
###############################################
# Do not edit below this line #
###############################################
# XiStem specific
INCLSFX = .h
CPPSFX = .c
Objsfx = .o
EXESFX =
# 网 iPP Dependencies
Ippincls = -i $ (ipproot) / include
Ipplibs = $ (ipproot) /lib/ippsc_xsc30lnx_r.a
# Compiler Specifics
CC = ARM-Linux-GCC
Cflags = -wall -l.
Libcflags = $ (cflags) -fpic
Ccopts = -c -o2
CCDefs = -d_ipp_pca -dlinux32
CCINCLS = -i. / Include $ (ippincls)
LNK = ARM-Linux-LD
Lnkout = -O
Lnkopts =
Lnklibs = $ (ipplibs)
Build: prepare_directory build_sharedlib; @echo "done all"
##### API Object #####
CPPSRC_API =. / Src / API / BitPack.c /
./src/api/decgsmamr.c /
./src/api/dtx_util.c /
./src/api/encgsmamr.c /
./src/api/gain_util.c /
./src/api/tab_util.c /
./src/api/util.c
Object_api = $ (PatSubst% $ (CPPSFX),% $ (Objsfx), $ (CPPSRC_API))
##### 网pp Object #####
CPPSRC_IPP =. / Src / _ipp / extra_ipps.c /
./src/_ipp/extra_ippsc.c /
./src/_ipp/owncts.c
Object_ipp = $ (PatSubst% $ (CPPSFX),% $ (Objsfx), $ (cppsrc_ipp))
##### Link Shared lib #####
Object_shared_name = libamrcodec.so.1.0.0
Object_shared =. / Lib / $ (Object_shared_name)
Soname = libamrcodec.so.1
Build_sharedlib: $ (Object_shared); @echo "Done Sharedlib !!"
$ (Object_Shared): $ (Object_API) $ (Object_IPP) $ (CC) -shared -wl, -soname, $ (SONAME) -O $ @ $ (lnkopts) $ ^ $ (lnklibs)
CD LIB
Ln -sf $ (Object_shared_name) ./lib/libamrcodec.so
ln -sf $ (object_shared_name) ./lib/$( 4AME)
CD ..
##### compile shared lib Objects all together #####
Objects = $ (Object_API) $ (Object_IPP)
$ (Objects):% $ (Objsfx):% $ (CPPSFX)
$ (CC) $ (Libcflags) $ (CCINCLS) $ (ccopts) -o $ @ $ <
Prepare_directory: Force
@if Test! -d ./lib; the mkdir ./lib; fi
Clean: Force
RM -F $ (Objects)
Distclean: Clean
RM -F./LIB/*
FORCE:
This way, libamrcodec.so can be generated, if other programs need to be called, add -lamrcodec parameters when compiling.
5. GSMAMR CODEC usage
Code sample code:
We agree that PSPEECHIN points to the address of the memory cell that needs to be encoded, PencodeDout points to the address of the memory cell that stores the encoded data frame.
/ * Define the encoder object pointer * /
Static gsmamrencoder_obj * encoder;
/ * Define the encoding mode * /
Static int = gsmamrencode_defaultmode;
/ * Define the coding rate * /
GSMAMR_RATE_T WRKRATE = GSMAMR_RATE_12200;
/ * Coding parameters * /
Static gsmamrenc_params gsmenc_params;
/ * Define temporary variables * /
Unsigned int esize = 0, VAD;
/ * Require encoded audio content * /
Short * pspeechin;
/ * Address of the coding result * /
UNSIGNED Char * Pencodedout;
/ * Distribute memory space for encoders * /
Gsmenc_params.mode = emode;
Gsmenc_params.codectype = (gsmamrcodec_type) 0;
ApigsmamRencoder_alloc (& gsmenc_params, & eSize);
IF (! (encoder = (gsmamrencoder_obj *) ippsmalloc_8u (eSize))) {
Printf ("/ NLOW MEMORY:% u Bytes Not Allocated / N", ESIZE);
Exit (1);
}
/ * Initialization encoder * /
ApigsmamRencoder_init ((gsmamrencoder_obj *) encoder, emode);
/ * Code * /
ApigsmamRencode (Encoder, PSPEECHIN, WRKRATE, PENCODEDOUT 2, & VAD);
IF (VAD) / * If the code is valid * /
{
Pencodedout [0] = Speech_Good; / * SET FRAME TYPE * /
Pencodedout [1] = Wrkrate; / * set speech rate * /
}
Decoding sample code:
We agree that PSPEECHOUT points to the address of the memory cell that stores the decoding audio content, and the Pencodedin refers to the address of the memory unit of the data frame that needs to decode. / * Define the decoder object pointer * /
Static gsmamrdecoder_obj * decoder;
/ * Define decoding mode * /
Static int dmode = gsmamrdecode_defaultmode;
/ * Define the decoding rate, * /
GSMAMR_RATE_T WRKRATE = GSMAMR_RATE_12200;
/ * Define frame type * /
Rxframetype rx_type;
/ * Coding parameters * /
Static gsmamrdec_params gsmdec_params;
/ * Define temporary variables * /
Unsigned int esize = 0;
/ * Audio content that needs to decode * /
UNSIGNED Char * PencodedIn;
/ * Addresses the address of the decoding result * /
Short * pspeech;
/ * Distribute memory space for the decoder * /
GSMDEC_PARAMS.MODE = DMODE;
GSMDEC_PARAMS.CODECTYPE = (gsmamrcodec_type) 0;
ApigsmamRDecoder_alloc (& gsmdec_params, & eSize);
IF (! (encoder = (gsmamrdecoder_obj *) ippsmalloc_8u (eSize))) {
Printf ("/ NLOW MEMORY:% u Bytes Not Allocated / N", ESIZE);
Exit (1);
}
/ * Initialization decoder * /
Apigsmamrdecoder_init ((gsmamrdecoder_obj *) Decoder, DMODE);
/ * Read frame information * /
RX_TYPE = (rxframetype) Pencodedin [0];
Wrkrate = (gsmamr_rate_t) Pencodedin [1];
/ * Code * /
Apigsmamrdecode (Decoder, PencodedIn 2, Wrkrate, RX_TYPE, PSPEECHOUT);