melp_chn.c
上传用户:cxx_68
上传日期:2021-02-21
资源大小:161k
文件大小:7k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. Fixed-point C code, version 1.0
  4. Copyright (c) 1998, Texas Instruments, Inc.  
  5. Texas Instruments has intellectual property rights on the MELP
  6. algorithm.  The Texas Instruments contact for licensing issues for
  7. commercial and non-government use is William Gordon, Director,
  8. Government Contracts, Texas Instruments Incorporated, Semiconductor
  9. Group (phone 972 480 7442).
  10. The fixed-point version of the voice codec Mixed Excitation Linear
  11. Prediction (MELP) is based on specifications on the C-language software
  12. simulation contained in GSM 06.06 which is protected by copyright and
  13. is the property of the European Telecommunications Standards Institute
  14. (ETSI). This standard is available from the ETSI publication office
  15. tel. +33 (0)4 92 94 42 58. ETSI has granted a license to United States
  16. Department of Defense to use the C-language software simulation contained
  17. in GSM 06.06 for the purposes of the development of a fixed-point
  18. version of the voice codec Mixed Excitation Linear Prediction (MELP).
  19. Requests for authorization to make other use of the GSM 06.06 or
  20. otherwise distribute or modify them need to be addressed to the ETSI
  21. Secretariat fax: +33 493 65 47 16.
  22. */
  23. /*
  24.     Name: melp_chn_write, melp_chn_read
  25.     Description: Write/read MELP channel bitstream
  26.     Inputs:
  27.       MELP parameter structure
  28.     Outputs: 
  29.       updated MELP parameter structure (channel pointers)
  30.     Returns: void
  31. */
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include "typedefs.h"
  35. #include "melp.h"
  36. #include "vq.h"
  37. #include "melp_sub.h"
  38. #include "math_lib.h"
  39. #include "constant.h"
  40. /* Define number of channel bits per frame */
  41. #define NUM_CH_BITS 54
  42. #define ORIGINAL_BIT_ORDER 0  /* flag to use bit order of original version */
  43. extern Shortword msvq_cb[];
  44. extern Shortword msvq_cb_mean[];
  45. extern Shortword fsvq_cb[];
  46. /* Define bit buffer */
  47. static UShortword bit_buffer[NUM_CH_BITS];
  48. #if (ORIGINAL_BIT_ORDER)
  49. /* Original linear order */
  50. static Shortword bit_order[NUM_CH_BITS] = {
  51. 0,  1,  2,  3,  4,  5,
  52. 6,  7,  8,  9,  10, 11,
  53. 12, 13, 14, 15, 16, 17, 
  54. 18, 19, 20, 21, 22, 23,
  55. 24, 25, 26, 27, 28, 29,
  56. 30, 31, 32, 33, 34, 35,
  57. 36, 37, 38, 39, 40, 41, 
  58. 42, 43, 44, 45, 46, 47, 
  59. 48, 49, 50, 51, 52, 53};
  60. #else
  61. /* Order based on priority of bits */
  62. static Shortword bit_order[NUM_CH_BITS] = {
  63. 0,  17, 9,  28, 34, 3, 
  64. 4,  39, 1,  2,  13, 38,
  65. 14, 10, 11, 40, 15, 21,
  66. 27, 45, 12, 26, 25, 33,
  67. 20, 24, 23, 32, 44, 46,
  68. 22, 31, 53, 52, 51, 7,
  69. 6,  19, 18, 29, 37, 30,
  70. 36, 35, 43, 42, 16, 41, 
  71. 50, 49, 48, 47, 8,  5
  72. };
  73. #endif
  74. static Shortword sync_bit = 0; /* sync bit */
  75. void melp_chn_write(struct melp_param *par)
  76. {
  77.     Shortword i, bit_cntr;
  78.     UShortword *bit_ptr; 
  79.     
  80.     /* FEC: code additional information in redundant indeces */
  81.     fec_code(par);
  82.     
  83.     /* Fill bit buffer */
  84.     bit_ptr = bit_buffer;
  85.     bit_cntr = 0;
  86.     pack_code(par->gain_index[1],&bit_ptr,&bit_cntr,5,1);
  87.     
  88.     /* Toggle and write sync bit */
  89.     if (sync_bit)
  90. sync_bit = 0;
  91.     else
  92. sync_bit = 1;
  93.     pack_code(sync_bit,&bit_ptr,&bit_cntr,1,1);
  94.     pack_code(par->gain_index[0],&bit_ptr,&bit_cntr,3,1);
  95.     pack_code(par->pitch_index,&bit_ptr,&bit_cntr,PIT_BITS,1);
  96.     pack_code(par->jit_index,&bit_ptr,&bit_cntr,1,1);
  97.     pack_code(par->bpvc_index,&bit_ptr,&bit_cntr,NUM_BANDS-1,1);
  98.     
  99.     for (i = 0; i < par->msvq_stages; i++) 
  100.       pack_code(par->msvq_index[i],&bit_ptr,&bit_cntr,par->msvq_bits[i],1);
  101.     
  102.     pack_code(par->fsvq_index[0],&bit_ptr,&bit_cntr,
  103.       FS_BITS,1);
  104.     
  105.     /* Write channel output buffer */
  106.     for (i = 0; i < NUM_CH_BITS; i++) {
  107. pack_code(bit_buffer[bit_order[i]],&par->chptr,&par->chbit,
  108.   1,CHWORDSIZE);
  109. if (i == 0)
  110.   *(par->chptr) |= (UShortword)0x8000; /* set beginning of frame bit */
  111.     }
  112. }
  113. Shortword melp_chn_read(struct melp_param *par, struct melp_param *prev_par)
  114. {
  115.     Shortword erase = 0;
  116.     Shortword i, bit_cntr;
  117.     UShortword *bit_ptr; 
  118.     /* Read channel output buffer into bit buffer */
  119.     bit_ptr = bit_buffer;
  120.     for (i = 0; i < NUM_CH_BITS; i++) {
  121. erase |= unpack_code(&par->chptr,&par->chbit,
  122.      (Shortword *)&bit_buffer[bit_order[i]],
  123.      1,CHWORDSIZE,ERASE_MASK);
  124. bit_ptr++;
  125.     }
  126.     /* Read information from  bit buffer */
  127.     bit_ptr = bit_buffer;
  128.     bit_cntr = 0;
  129.     unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[1],5,1,0);
  130.     
  131.     /* Read sync bit */
  132.     unpack_code(&bit_ptr,&bit_cntr,&i,1,1,0);
  133.     unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[0],3,1,0);
  134.     unpack_code(&bit_ptr,&bit_cntr,&par->pitch_index,PIT_BITS,1,0);
  135.     
  136.     unpack_code(&bit_ptr,&bit_cntr,&par->jit_index,1,1,0);
  137.     unpack_code(&bit_ptr,&bit_cntr,&par->bpvc_index,
  138.  NUM_BANDS-1,1,0);
  139.     
  140.     for (i = 0; i < par->msvq_stages; i++) 
  141.       unpack_code(&bit_ptr,&bit_cntr,&par->msvq_index[i],
  142.    par->msvq_bits[i],1,0);
  143.     unpack_code(&bit_ptr,&bit_cntr,&par->fsvq_index[0],
  144.  FS_BITS,1,0);
  145.     
  146.     /* Clear unvoiced flag */
  147.     par->uv_flag = 0;
  148.     
  149.     erase = fec_decode(par,erase);
  150.     
  151.     /* Decode new frame if no erasures occurred */
  152.     if (erase) {
  153. /* Erasure: frame repeat */
  154. /* Save correct values of pointers */
  155. prev_par->chptr = par->chptr;
  156. prev_par->chbit = par->chbit;
  157. *par = *prev_par; 
  158. /* Force all subframes to equal last one */
  159. for (i = 0; i < NUM_GAINFR-1; i++) {
  160.     par->gain[i] = par->gain[NUM_GAINFR-1];
  161. }
  162.     }
  163.     else {
  164. /* Decode line spectrum frequencies */
  165. vq_msd2(msvq_cb,&par->lsf[1],msvq_cb_mean,(Shortword*)NULL,
  166. par->msvq_index,par->msvq_levels,par->msvq_stages,LPC_ORD,0,2);
  167. i = FS_LEVELS;
  168. if (par->uv_flag)
  169.   fill(par->fs_mag,(Shortword)ONE_Q13,(Shortword)NUM_HARM);
  170. else
  171.   {
  172.       /* Decode Fourier magnitudes */
  173.       vq_msd2(fsvq_cb,par->fs_mag,(Shortword*)NULL,(Shortword*)NULL,
  174.       par->fsvq_index,&i,1,(Shortword)NUM_HARM,0,0);
  175.   }
  176. /* Decode gain terms with uniform log quantizer */
  177. q_gain_dec(par->gain,par->gain_index,(Shortword)GN_QLO_Q8,
  178.    (Shortword)GN_QUP_Q8,GN_QLEV_M1_Q10,5);
  179. /* Fractional pitch: */
  180. /* Decode logarithmic pitch period */
  181. if (par->uv_flag)
  182.   par->pitch = (Shortword)UV_PITCH_Q7;
  183. else 
  184.   {
  185.       quant_u_dec(par->pitch_index,&par->pitch,
  186.   (Shortword)PIT_QLO_Q12,(Shortword)PIT_QUP_Q12,
  187.   PIT_QLEV_M1_Q8,7);
  188.       par->pitch = pow10_fxp(par->pitch,7);
  189.   }
  190. /* Decode jitter */
  191. /* quant_u_dec(par->jit_index,&par->jitter,0.0,MAX_JITTER,2); */
  192. if (par->jit_index == 0)
  193.   par->jitter = 0;
  194. else
  195.   par->jitter = (Shortword)MAX_JITTER_Q15;
  196. /* Decode bandpass voicing */
  197. q_bpvc_dec(&par->bpvc[0],&par->bpvc_index,par->uv_flag,
  198.    NUM_BANDS);
  199.     }
  200.     /* Return erase flag */
  201.     return(erase);
  202. }