melp_chn.c
上传用户:csczyc
上传日期:2021-02-19
资源大小:1051k
文件大小:8k
- /*
- 2.4 kbps MELP Proposed Federal Standard speech coder
- Fixed-point C code, version 1.0
- Copyright (c) 1998, Texas Instruments, Inc.
- Texas Instruments has intellectual property rights on the MELP
- algorithm. The Texas Instruments contact for licensing issues for
- commercial and non-government use is William Gordon, Director,
- Government Contracts, Texas Instruments Incorporated, Semiconductor
- Group (phone 972 480 7442).
- The fixed-point version of the voice codec Mixed Excitation Linear
- Prediction (MELP) is based on specifications on the C-language software
- simulation contained in GSM 06.06 which is protected by copyright and
- is the property of the European Telecommunications Standards Institute
- (ETSI). This standard is available from the ETSI publication office
- tel. +33 (0)4 92 94 42 58. ETSI has granted a license to United States
- Department of Defense to use the C-language software simulation contained
- in GSM 06.06 for the purposes of the development of a fixed-point
- version of the voice codec Mixed Excitation Linear Prediction (MELP).
- Requests for authorization to make other use of the GSM 06.06 or
- otherwise distribute or modify them need to be addressed to the ETSI
- Secretariat fax: +33 493 65 47 16.
- */
- /*
- Name: melp_chn_write, melp_chn_read
- Description: Write/read MELP channel bitstream
- Inputs:
- MELP parameter structure
- Outputs:
- updated MELP parameter structure (channel pointers)
- Returns: void
- */
- #include <stdio.h>
- #include <math.h>
- #include "typedefs.h"
- #include "melp.h"
- #include "vq.h"
- #include "melp_sub.h"
- #include "math_lib.h"
- #include "constant.h"
- /* Define number of channel bits per frame */
- #define NUM_CH_BITS 54
- #define ORIGINAL_BIT_ORDER 0 /* flag to use bit order of original version */
- extern Shortword msvq_cb[];
- extern Shortword msvq_cb_mean[];
- extern Shortword fsvq_cb[];
- /* Define bit buffer */
- static UShortword bit_buffer[NUM_CH_BITS];
- #if (ORIGINAL_BIT_ORDER)
- /* Original linear order */
- static Shortword bit_order[NUM_CH_BITS] = {
- 0, 1, 2, 3, 4, 5,
- 6, 7, 8, 9, 10, 11,
- 12, 13, 14, 15, 16, 17,
- 18, 19, 20, 21, 22, 23,
- 24, 25, 26, 27, 28, 29,
- 30, 31, 32, 33, 34, 35,
- 36, 37, 38, 39, 40, 41,
- 42, 43, 44, 45, 46, 47,
- 48, 49, 50, 51, 52, 53};
- #else
- /* Order based on priority of bits */
- static Shortword bit_order[NUM_CH_BITS] = {
- 0, 17, 9, 28, 34, 3,
- 4, 39, 1, 2, 13, 38,
- 14, 10, 11, 40, 15, 21,
- 27, 45, 12, 26, 25, 33,
- 20, 24, 23, 32, 44, 46,
- 22, 31, 53, 52, 51, 7,
- 6, 19, 18, 29, 37, 30,
- 36, 35, 43, 42, 16, 41,
- 50, 49, 48, 47, 8, 5
- };
- #endif
- static Shortword sync_bit = 0; /* sync bit */
- void melp_chn_write(struct melp_param *par)
- {
- Shortword i, bit_cntr;
- UShortword *bit_ptr;
- unsigned char out_dat[7];
- unsigned char pos[]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
- unsigned char *chptr;
- /* FEC: code additional information in redundant indeces */
- fec_code(par);
-
- /* Fill bit buffer */
- bit_ptr = bit_buffer;
- bit_cntr = 0;
- pack_code(par->gain_index[1],&bit_ptr,&bit_cntr,5,1);
-
- /* Toggle and write sync bit */
- if (sync_bit)
- sync_bit = 0;
- else
- sync_bit = 1;
- pack_code(sync_bit,&bit_ptr,&bit_cntr,1,1);
- pack_code(par->gain_index[0],&bit_ptr,&bit_cntr,3,1);
- pack_code(par->pitch_index,&bit_ptr,&bit_cntr,PIT_BITS,1);
- pack_code(par->jit_index,&bit_ptr,&bit_cntr,1,1);
- pack_code(par->bpvc_index,&bit_ptr,&bit_cntr,NUM_BANDS-1,1);
-
- for (i = 0; i < par->msvq_stages; i++)
- pack_code(par->msvq_index[i],&bit_ptr,&bit_cntr,par->msvq_bits[i],1);
-
- pack_code(par->fsvq_index[0],&bit_ptr,&bit_cntr,
- FS_BITS,1);
-
- /* Write channel output buffer */
- // for (i = 0; i < NUM_CH_BITS; i++) {
- // pack_code(bit_buffer[bit_order[i]],&par->chptr,&par->chbit,
- // 1,CHWORDSIZE);
- // if (i == 0)
- // *(par->chptr) |= (UShortword)0x8000; /* set beginning of frame bit */
- // }
- //
- chptr = par->chptr;
- for (i=0;i<7;i++){
- out_dat[i]=0;
- }
- for(i=0;i<54;i++){
- if (bit_buffer[bit_order[i]]!=0)
- out_dat[i/8]^=pos[i%8];
- }
- for(i=0;i<7;i++){
- *chptr=out_dat[i];
- chptr++;
- }
- }
- Shortword melp_chn_read(struct melp_param *par, struct melp_param *prev_par)
- {
- Shortword erase = 0;
- Shortword i, j, bit_cntr;
- UShortword *bit_ptr;
- unsigned char out_dat[7];
- unsigned char pos[]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
- //UShortword dat[54];
- unsigned char *chptr;
- /* Read channel output buffer into bit buffer */
- bit_ptr = bit_buffer;
- // for (i = 0; i < NUM_CH_BITS; i++) {
- // erase |= unpack_code(&par->chptr,&par->chbit,
- // (Shortword *)&bit_buffer[bit_order[i]],
- // 1,CHWORDSIZE,ERASE_MASK);
- // bit_ptr++;
- // }
- chptr= par->chptr ;
- for(i=0;i<7;i++)
- {out_dat[i]=*chptr;
- chptr++;
- }
- // for(i=0;i<54;i++){
- // dat[i]=0;
- // }
- for(i=0;i<7;i++)
- {
- for(j=0;j<8;j++)
- {if(out_dat[i]&pos[j])
- // dat[i*8+j]=1;
- bit_buffer[bit_order[i*8+j]]=1;
- else
- bit_buffer[bit_order[i*8+j]]=0;
- }
- if((i*8+j)>53)
- break;
- }
- /* Read information from bit buffer */
- bit_ptr = bit_buffer;
- bit_cntr = 0;
- unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[1],5,1,0);
-
- /* Read sync bit */
- unpack_code(&bit_ptr,&bit_cntr,&i,1,1,0);
- unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[0],3,1,0);
- unpack_code(&bit_ptr,&bit_cntr,&par->pitch_index,PIT_BITS,1,0);
-
- unpack_code(&bit_ptr,&bit_cntr,&par->jit_index,1,1,0);
- unpack_code(&bit_ptr,&bit_cntr,&par->bpvc_index,
- NUM_BANDS-1,1,0);
-
- for (i = 0; i < par->msvq_stages; i++)
- unpack_code(&bit_ptr,&bit_cntr,&par->msvq_index[i],
- par->msvq_bits[i],1,0);
- unpack_code(&bit_ptr,&bit_cntr,&par->fsvq_index[0],
- FS_BITS,1,0);
-
- /* Clear unvoiced flag */
- par->uv_flag = 0;
-
- erase = fec_decode(par,erase);
-
- /* Decode new frame if no erasures occurred */
- if (erase) {
-
- /* Erasure: frame repeat */
-
- /* Save correct values of pointers */
- prev_par->chptr = par->chptr;
- prev_par->chbit = par->chbit;
- *par = *prev_par;
-
- /* Force all subframes to equal last one */
- for (i = 0; i < NUM_GAINFR-1; i++) {
- par->gain[i] = par->gain[NUM_GAINFR-1];
- }
- }
- else {
-
- /* Decode line spectrum frequencies */
- vq_msd2(msvq_cb,&par->lsf[1],msvq_cb_mean,(Shortword*)NULL,
- par->msvq_index,par->msvq_levels,par->msvq_stages,LPC_ORD,0,2);
- i = FS_LEVELS;
- if (par->uv_flag)
- fill(par->fs_mag,(Shortword)ONE_Q13,(Shortword)NUM_HARM);
- else
- {
- /* Decode Fourier magnitudes */
- vq_msd2(fsvq_cb,par->fs_mag,(Shortword*)NULL,(Shortword*)NULL,
- par->fsvq_index,&i,1,(Shortword)NUM_HARM,0,0);
- }
- /* Decode gain terms with uniform log quantizer */
- q_gain_dec(par->gain,par->gain_index,(Shortword)GN_QLO_Q8,
- (Shortword)GN_QUP_Q8,GN_QLEV_M1_Q10,5);
- /* Fractional pitch: */
- /* Decode logarithmic pitch period */
- if (par->uv_flag)
- par->pitch = (Shortword)UV_PITCH_Q7;
- else
- {
- quant_u_dec(par->pitch_index,&par->pitch,
- (Shortword)PIT_QLO_Q12,(Shortword)PIT_QUP_Q12,
- PIT_QLEV_M1_Q8,7);
- par->pitch = pow10_fxp(par->pitch,7);
- }
- /* Decode jitter */
- /* quant_u_dec(par->jit_index,&par->jitter,0.0,MAX_JITTER,2); */
- if (par->jit_index == 0)
- par->jitter = 0;
- else
- par->jitter = (Shortword)MAX_JITTER_Q15;
- /* Decode bandpass voicing */
- q_bpvc_dec(&par->bpvc[0],&par->bpvc_index,par->uv_flag,
- NUM_BANDS);
- }
- /* Return erase flag */
- return(erase);
- }