encode.c
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /*
  2. $Log: encode.c,v $
  3. Revision 1.1.1.1  2002/07/28 05:23:27  freeman_yong
  4. lpc10 codec
  5.  * Revision 1.1  1996/08/19  22:32:21  jaf
  6.  * Initial revision
  7.  *
  8. */
  9. #ifdef P_R_O_T_O_T_Y_P_E_S
  10. extern int encode_(integer *voice, integer *pitch, real *rms, real *rc, integer *ipitch, integer *irms, integer *irc);
  11. /* comlen contrl_ 12 */
  12. #endif
  13. /*  -- translated by f2c (version 19951025).
  14.    You must link the resulting object file with the libraries:
  15. -lf2c -lm   (in that order)
  16. */
  17. #include "f2c.h"
  18. /* Common Block Declarations */
  19. extern struct {
  20.     integer order, lframe;
  21.     logical corrp;
  22. } contrl_;
  23. #define contrl_1 contrl_
  24. /* Table of constant values */
  25. static integer c__2 = 2;
  26. /* ***************************************************************** */
  27. /*  ENCODE Version 54 */
  28. /* $Log: encode.c,v $
  29. /* Revision 1.1.1.1  2002/07/28 05:23:27  freeman_yong
  30. /* lpc10 codec
  31. /*
  32.  * Revision 1.1  1996/08/19  22:32:21  jaf
  33.  * Initial revision
  34.  * */
  35. /* Revision 1.5  1996/03/26  19:35:50  jaf */
  36. /* Commented out trace statements. */
  37. /* Revision 1.4  1996/03/21  00:26:29  jaf */
  38. /* Added the comment that this subroutine has no local state. */
  39. /* In the last check-in, I forgot to mention that I had added comments */
  40. /* explaining which indices of array arguments are read or written. */
  41. /* Revision 1.3  1996/03/21  00:22:39  jaf */
  42. /* Added comments explaining that all local arrays are effectively */
  43. /* constants. */
  44. /* Revision 1.2  1996/03/13  18:48:33  jaf */
  45. /* Comments added explaining that none of the local variables of this */
  46. /* subroutine need to be saved from one invocation to the next. */
  47. /* Revision 1.1  1996/02/07 14:45:29  jaf */
  48. /* Initial revision */
  49. /* ***************************************************************** */
  50. /*  Quantize LPC parameters for transmission */
  51. /* INPUTS: */
  52. /*  VOICE  - Half frame voicing decisions */
  53. /*           Indices 1 through 2 read. */
  54. /*  PITCH  - Pitch */
  55. /*  RMS    - Energy */
  56. /*  RC     - Reflection coefficients */
  57. /*           Indices 1 through ORDER read. */
  58. /*  CORRP  - Error Correction: TRUE = yes, FALSE = none */
  59. /*           (this is defined in file control.fh) */
  60. /* OUTPUTS: */
  61. /*  IPITCH - Coded pitch and voicing */
  62. /*  IRMS   - Quantized energy */
  63. /*  IRC    - Quantized reflection coefficients */
  64. /*           Indices 1 through MAX(ORDER,2) written. */
  65. /*           If CORRP is .TRUE., then indices 1 through 10 written */
  66. /*           for unvoiced frames. */
  67. /* This subroutine has no local state. */
  68. /* Subroutine */ int encode_(integer *voice, integer *pitch, real *rms, real *
  69. rc, integer *ipitch, integer *irms, integer *irc)
  70. {
  71.     /* Initialized data */
  72.     static integer enctab[16] = { 0,7,11,12,13,10,6,1,14,9,5,2,3,4,8,15 };
  73.     static integer entau[60] = { 19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,
  74.     42,43,41,45,37,53,49,51,50,54,52,60,56,58,26,90,88,92,84,86,82,83,
  75.     81,85,69,77,73,75,74,78,70,71,67,99,97,113,112,114,98,106,104,108,
  76.     100,101,76 };
  77.     static integer enadd[8] = { 1920,-768,2432,1280,3584,1536,2816,-1152 };
  78.     static real enscl[8] = { .0204f,.0167f,.0145f,.0147f,.0143f,.0135f,.0125f,
  79.     .0112f };
  80.     static integer enbits[8] = { 6,5,4,4,4,4,3,3 };
  81.     static integer entab6[64] = { 0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,
  82.     3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,
  83.     9,10,10,11,11,12,13,14,15 };
  84.     static integer rmst[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
  85.     384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
  86.     92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
  87.     14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
  88.     /* System generated locals */
  89.     integer i__1, i__2;
  90.     /* Builtin functions */
  91.     integer pow_ii(integer *, integer *);
  92.     /* Local variables */
  93.     integer idel, nbit, i__, j, i2, i3, mrk;
  94. /* $Log: encode.c,v $
  95. /* Revision 1.1.1.1  2002/07/28 05:23:27  freeman_yong
  96. /* lpc10 codec
  97. /*
  98.  * Revision 1.1  1996/08/19  22:32:21  jaf
  99.  * Initial revision
  100.  * */
  101. /* Revision 1.3  1996/03/29  22:03:47  jaf */
  102. /* Removed definitions for any constants that were no longer used. */
  103. /* Revision 1.2  1996/03/26  19:34:33  jaf */
  104. /* Added comments indicating which constants are not needed in an */
  105. /* application that uses the LPC-10 coder. */
  106. /* Revision 1.1  1996/02/07  14:43:51  jaf */
  107. /* Initial revision */
  108. /*   LPC Configuration parameters: */
  109. /* Frame size, Prediction order, Pitch period */
  110. /*       Arguments */
  111. /* $Log: encode.c,v $
  112. /* Revision 1.1.1.1  2002/07/28 05:23:27  freeman_yong
  113. /* lpc10 codec
  114. /*
  115.  * Revision 1.1  1996/08/19  22:32:21  jaf
  116.  * Initial revision
  117.  * */
  118. /* Revision 1.3  1996/03/29  22:05:55  jaf */
  119. /* Commented out the common block variables that are not needed by the */
  120. /* embedded version. */
  121. /* Revision 1.2  1996/03/26  19:34:50  jaf */
  122. /* Added comments indicating which constants are not needed in an */
  123. /* application that uses the LPC-10 coder. */
  124. /* Revision 1.1  1996/02/07  14:44:09  jaf */
  125. /* Initial revision */
  126. /*   LPC Processing control variables: */
  127. /* *** Read-only: initialized in setup */
  128. /*  Files for Speech, Parameter, and Bitstream Input & Output, */
  129. /*    and message and debug outputs. */
  130. /* Here are the only files which use these variables: */
  131. /* lpcsim.f setup.f trans.f error.f vqsetup.f */
  132. /* Many files which use fdebug are not listed, since it is only used in */
  133. /* those other files conditionally, to print trace statements. */
  134. /*  integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
  135. /*  LPC order, Frame size, Quantization rate, Bits per frame, */
  136. /*    Error correction */
  137. /* Subroutine SETUP is the only place where order is assigned a value, */
  138. /* and that value is 10.  It could increase efficiency 1% or so to */
  139. /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as 
  140. */
  141. /* a variable in a COMMON block, since it is used in many places in the */
  142. /* core of the coding and decoding routines.  Actually, I take that back. 
  143. */
  144. /* At least when compiling with f2c, the upper bound of DO loops is */
  145. /* stored in a local variable before the DO loop begins, and then that is 
  146. */
  147. /* compared against on each iteration. */
  148. /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
  149. /* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
  150. /* is used in only a few places, and never in the core coding and */
  151. /* decoding routines, so it could be eliminated entirely. */
  152. /* nbits is similar to quant, and is given a value of 54 in SETUP. */
  153. /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
  154. /* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
  155. /* coder significantly whether it is .TRUE. or .FALSE., or whether it is 
  156. */
  157. /* a constant or a variable, since it is only examined once per frame. */
  158. /* Leaving it as a variable that is set to .TRUE.  seems like a good */
  159. /* idea, since it does enable some error-correction capability for */
  160. /* unvoiced frames, with no change in the coding rate, and no noticeable 
  161. */
  162. /* quality difference in the decoded speech. */
  163. /*  integer quant, nbits */
  164. /* *** Read/write: variables for debugging, not needed for LPC algorithm 
  165. */
  166. /*  Current frame, Unstable frames, Output clip count, Max onset buffer, 
  167. */
  168. /*    Debug listing detail level, Line count on listing page */
  169. /* nframe is not needed for an embedded LPC10 at all. */
  170. /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
  171. /* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
  172. /* an application, I would recommend removing the call to ERROR in RCCHK, 
  173. */
  174. /* and remove ERROR and nunsfm completely. */
  175. /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in 
  176. */
  177. /* sread.f.  When LPC10 is embedded into an application, one might want */
  178. /* to cause it to be incremented in a routine that takes the output of */
  179. /* SYNTHS and sends it to an audio device.  It could be optionally */
  180. /* displayed, for those that might want to know what it is. */
  181. /* maxosp is never initialized to 0 in SETUP, although it probably should 
  182. */
  183. /* be, and it is updated in subroutine ANALYS.  I doubt that its value */
  184. /* would be of much interest to an application in which LPC10 is */
  185. /* embedded. */
  186. /* listl and lincnt are not needed for an embedded LPC10 at all. */
  187. /*  integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
  188. /*  common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
  189. /*  common /contrl/ quant, nbits */
  190. /*  common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
  191. /*       Parameters/constants */
  192. /*       These arrays are not Fortran PARAMETER's, but they are defined */
  193. /*       by DATA statements below, and their contents are never altered. 
  194. */
  195. /*       Local variables that need not be saved */
  196.     /* Parameter adjustments */
  197.     --irc;
  198.     --rc;
  199.     --voice;
  200.     /* Function Body */
  201. /*  Scale RMS and RC's to integers */
  202.     *irms = *rms;
  203.     i__1 = contrl_1.order;
  204.     for (i__ = 1; i__ <= i__1; ++i__) {
  205. irc[i__] = rc[i__] * 32768.f;
  206.     }
  207. /*  IF(LISTL.GE.3)WRITE(FDEBUG,800)VOICE,PITCH,IRMS,(IRC(I),I=1,ORDER) */
  208. /* 800 FORMAT(1X,/,' <<ENCODE IN>>',T32,2I3,I6,I5,T50,10I8) */
  209. /*  Encode pitch and voicing */
  210.     if (voice[1] != 0 && voice[2] != 0) {
  211. *ipitch = entau[*pitch - 1];
  212.     } else {
  213. if (contrl_1.corrp) {
  214.     *ipitch = 0;
  215.     if (voice[1] != voice[2]) {
  216. *ipitch = 127;
  217.     }
  218. } else {
  219.     *ipitch = (voice[1] << 1) + voice[2];
  220. }
  221.     }
  222. /*  Encode RMS by binary table search */
  223.     j = 32;
  224.     idel = 16;
  225.     *irms = min(*irms,1023);
  226.     while(idel > 0) {
  227. if (*irms > rmst[j - 1]) {
  228.     j -= idel;
  229. }
  230. if (*irms < rmst[j - 1]) {
  231.     j += idel;
  232. }
  233. idel /= 2;
  234.     }
  235.     if (*irms > rmst[j - 1]) {
  236. --j;
  237.     }
  238.     *irms = 31 - j / 2;
  239. /*  Encode RC(1) and (2) as log-area-ratios */
  240.     for (i__ = 1; i__ <= 2; ++i__) {
  241. i2 = irc[i__];
  242. mrk = 0;
  243. if (i2 < 0) {
  244.     i2 = -i2;
  245.     mrk = 1;
  246. }
  247. i2 /= 512;
  248. i2 = min(i2,63);
  249. i2 = entab6[i2];
  250. if (mrk != 0) {
  251.     i2 = -i2;
  252. }
  253. irc[i__] = i2;
  254.     }
  255. /*  Encode RC(3) - (10) linearly, remove bias then scale */
  256.     i__1 = contrl_1.order;
  257.     for (i__ = 3; i__ <= i__1; ++i__) {
  258. i2 = irc[i__] / 2;
  259. i2 = (i2 + enadd[contrl_1.order + 1 - i__ - 1]) * enscl[
  260. contrl_1.order + 1 - i__ - 1];
  261. /* Computing MIN */
  262. i__2 = max(i2,-127);
  263. i2 = min(i__2,127);
  264. nbit = enbits[contrl_1.order + 1 - i__ - 1];
  265. i3 = 0;
  266. if (i2 < 0) {
  267.     i3 = -1;
  268. }
  269. i2 /= pow_ii(&c__2, &nbit);
  270. if (i3 == -1) {
  271.     --i2;
  272. }
  273. irc[i__] = i2;
  274.     }
  275. /*          Protect the most significant bits of the most */
  276. /*     important parameters during non-voiced frames. */
  277. /*     RC(1) - RC(4) are protected using 20 parity bits */
  278. /*     replacing RC(5) - RC(10). */
  279.     if (contrl_1.corrp) {
  280. if (*ipitch == 0 || *ipitch == 127) {
  281.     irc[5] = enctab[(irc[1] & 30) / 2];
  282.     irc[6] = enctab[(irc[2] & 30) / 2];
  283.     irc[7] = enctab[(irc[3] & 30) / 2];
  284.     irc[8] = enctab[(*irms & 30) / 2];
  285.     irc[9] = enctab[(irc[4] & 30) / 2] / 2;
  286.     irc[10] = enctab[(irc[4] & 30) / 2] & 1;
  287. }
  288.     }
  289. /*  IF(LISTL.GE.3)WRITE(FDEBUG,801)VOICE,IPITCH,IRMS,(IRC(J),J=1,ORDER) */
  290. /* 801 FORMAT(1X,'<<ENCODE OUT>>',T32,2I3,I6,I5,T50,10I8) */
  291.     return 0;
  292. } /* encode_ */