encode.c 12 KB

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