melp_chn.c
上传用户:luckfish
上传日期:2021-12-16
资源大小:77k
文件大小:5k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. version 1.2
  4. Copyright (c) 1996, 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. */
  11. /*
  12.     Name: melp_chn_write, melp_chn_read
  13.     Description: Write/read MELP channel bitstream
  14.     Inputs:
  15.       MELP parameter structure
  16.     Outputs: 
  17.       updated MELP parameter structure (channel pointers)
  18.     Returns: void
  19. */
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "melp.h"
  23. #include "vq.h"
  24. #include "melp_sub.h"
  25. /* Define number of channel bits per frame */
  26. #define NUM_CH_BITS 54
  27. #define ORIGINAL_BIT_ORDER 0  /* flag to use bit order of original version */
  28. extern float msvq_cb[];
  29. extern float fsvq_cb[];
  30. /* Define bit buffer */
  31. static unsigned int bit_buffer[NUM_CH_BITS];
  32. #if (ORIGINAL_BIT_ORDER)
  33. /* Original linear order */
  34. static int bit_order[NUM_CH_BITS] = {
  35. 0,  1,  2,  3,  4,  5,
  36. 6,  7,  8,  9,  10, 11,
  37. 12, 13, 14, 15, 16, 17, 
  38. 18, 19, 20, 21, 22, 23,
  39. 24, 25, 26, 27, 28, 29,
  40. 30, 31, 32, 33, 34, 35,
  41. 36, 37, 38, 39, 40, 41, 
  42. 42, 43, 44, 45, 46, 47, 
  43. 48, 49, 50, 51, 52, 53};
  44. #else
  45. /* Order based on priority of bits */
  46. static int bit_order[NUM_CH_BITS] = {
  47. 0,  17, 9,  28, 34, 3, 
  48. 4,  39, 1,  2,  13, 38,
  49. 14, 10, 11, 40, 15, 21,
  50. 27, 45, 12, 26, 25, 33,
  51. 20, 24, 23, 32, 44, 46,
  52. 22, 31, 53, 52, 51, 7,
  53. 6,  19, 18, 29, 37, 30,
  54. 36, 35, 43, 42, 16, 41, 
  55. 50, 49, 48, 47, 8,  5
  56. };
  57. #endif
  58. static int sync_bit = 0; /* sync bit */
  59. void melp_chn_write(struct melp_param *par)
  60. {
  61.     int i, bit_cntr;
  62.     unsigned int *bit_ptr; 
  63.     
  64.     /* FEC: code additional information in redundant indeces */
  65.     fec_code(par);
  66.     
  67.     /* Fill bit buffer */
  68.     bit_ptr = bit_buffer;
  69.     bit_cntr = 0;
  70.     pack_code(par->gain_index[1],&bit_ptr,&bit_cntr,5,1);
  71.     
  72.     /* Toggle and write sync bit */
  73.     if (sync_bit)
  74. sync_bit = 0;
  75.     else
  76. sync_bit = 1;
  77.     pack_code(sync_bit,&bit_ptr,&bit_cntr,1,1);
  78.     pack_code(par->gain_index[0],&bit_ptr,&bit_cntr,3,1);
  79.     pack_code(par->pitch_index,&bit_ptr,&bit_cntr,PIT_BITS,1);
  80.     pack_code(par->jit_index,&bit_ptr,&bit_cntr,1,1);
  81.     pack_code(par->bpvc_index,&bit_ptr,&bit_cntr,NUM_BANDS-1,1);
  82.     
  83.     for (i = 0; i < par->msvq_stages; i++) 
  84.       pack_code(par->msvq_index[i],&bit_ptr,&bit_cntr,par->msvq_bits[i],1);
  85.     
  86.     pack_code(par->fsvq_index[0],&bit_ptr,&bit_cntr,
  87.       FS_BITS,1);
  88.     
  89.     /* Write channel output buffer */
  90.     for (i = 0; i < NUM_CH_BITS; i++) {
  91. pack_code(bit_buffer[bit_order[i]],&par->chptr,&par->chbit,
  92.   1,CHWORDSIZE);
  93. if (i == 0)
  94.     *(par->chptr) |= 0x8000; /* set beginning of frame bit */
  95.     }
  96. }
  97. int melp_chn_read(struct melp_param *par, struct melp_param *prev_par)
  98. {
  99.     int erase = 0;
  100.     int i, bit_cntr;
  101.     unsigned int *bit_ptr; 
  102.     /* Read channel output buffer into bit buffer */
  103.     bit_ptr = bit_buffer;
  104.     for (i = 0; i < NUM_CH_BITS; i++) {
  105. erase |= unpack_code(&par->chptr,&par->chbit,&bit_buffer[bit_order[i]],
  106.      1,CHWORDSIZE,ERASE_MASK);
  107. bit_ptr++;
  108.     }
  109.     /* Read information from  bit buffer */
  110.     bit_ptr = bit_buffer;
  111.     bit_cntr = 0;
  112.     unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[1],5,1,0);
  113.     
  114.     /* Read sync bit */
  115.     unpack_code(&bit_ptr,&bit_cntr,&i,1,1,0);
  116.     unpack_code(&bit_ptr,&bit_cntr,&par->gain_index[0],3,1,0);
  117.     unpack_code(&bit_ptr,&bit_cntr,&par->pitch_index,PIT_BITS,1,0);
  118.     
  119.     unpack_code(&bit_ptr,&bit_cntr,&par->jit_index,1,1,0);
  120.     unpack_code(&bit_ptr,&bit_cntr,&par->bpvc_index,
  121.  NUM_BANDS-1,1,0);
  122.     
  123.     for (i = 0; i < par->msvq_stages; i++) 
  124.       unpack_code(&bit_ptr,&bit_cntr,&par->msvq_index[i],
  125.    par->msvq_bits[i],1,0);
  126.     unpack_code(&bit_ptr,&bit_cntr,&par->fsvq_index[0],
  127.  FS_BITS,1,0);
  128.     
  129.     /* Clear unvoiced flag */
  130.     par->uv_flag = 0;
  131.     
  132.     erase = fec_decode(par,erase);
  133.     
  134.     /* Decode new frame if no erasures occurred */
  135.     if (erase) {
  136. /* Erasure: frame repeat */
  137. /* Save correct values of pointers */
  138. prev_par->chptr = par->chptr;
  139. prev_par->chbit = par->chbit;
  140. *par = *prev_par; 
  141. /* Force all subframes to equal last one */
  142. for (i = 0; i < NUM_GAINFR-1; i++) {
  143.     par->gain[i] = par->gain[NUM_GAINFR-1];
  144. }
  145.     }
  146.     else {
  147. /* Decode line spectrum frequencies */
  148. vq_msd2(msvq_cb,&par->lsf[1],(float*)NULL,(float*)NULL,par->msvq_index,
  149. par->msvq_levels,par->msvq_stages,LPC_ORD,0);
  150. i = FS_LEVELS;
  151. if (par->uv_flag)
  152.   fill(par->fs_mag,1.,NUM_HARM);
  153. else
  154.   {
  155.       /* Decode Fourier magnitudes */
  156.       vq_msd2(fsvq_cb,par->fs_mag,(float*)NULL,(float*)NULL,
  157.       par->fsvq_index,&i,1,NUM_HARM,0);
  158.   }
  159. /* Decode gain terms with uniform log quantizer */
  160. q_gain_dec(par->gain, par->gain_index,GN_QLO,GN_QUP,GN_QLEV);
  161. /* Fractional pitch: */
  162. /* Decode logarithmic pitch period */
  163. if (par->uv_flag)
  164.   par->pitch = UV_PITCH;
  165. else 
  166.   {
  167.       quant_u_dec(par->pitch_index,&par->pitch,PIT_QLO,PIT_QUP,
  168.   PIT_QLEV);
  169.       par->pitch = pow(10.0,par->pitch);
  170.   }
  171. /* Decode jitter and bandpass voicing */
  172. quant_u_dec(par->jit_index,&par->jitter,0.0,MAX_JITTER,2);
  173. q_bpvc_dec(&par->bpvc[0],&par->bpvc_index,par->uv_flag,
  174.    NUM_BANDS);
  175.     }
  176.     /* Return erase flag */
  177.     return(erase);
  178. }