encode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /*======================================================================*/
  51. /* Enhanced Variable Rate Codec - Bit-Exact C Specification */
  52. /* Copyright (C) 1997-1998 Telecommunications Industry Association. */
  53. /* All rights reserved. */
  54. /*----------------------------------------------------------------------*/
  55. /* Note: Reproduction and use of this software for the design and */
  56. /* development of North American Wideband CDMA Digital */
  57. /* Cellular Telephony Standards is authorized by the TIA. */
  58. /* The TIA does not authorize the use of this software for any */
  59. /* other purpose. */
  60. /* */
  61. /* The availability of this software does not provide any license */
  62. /* by implication, estoppel, or otherwise under any patent rights */
  63. /* of TIA member companies or others covering any use of the */
  64. /* contents herein. */
  65. /* */
  66. /* Any copies of this software or derivative works must include */
  67. /* this and all other proprietary notices. */
  68. /*======================================================================*/
  69. /* EVRC Encoder */
  70. /*======================================================================*/
  71. /* ..Includes. */
  72. /*----------------------------------------------------------------------*/
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include <stdlib.h>
  76. #include "mathadv.h"
  77. //#include "mathevrc.h"
  78. #include "dsp_math.h"
  79. #include "mathdp31.h"
  80. #include "e_globs.h"
  81. #include "globs.h"
  82. #include "macro.h"
  83. #include "proto.h"
  84. #include "rom.h"
  85. #include "acelp_pf.h" /* for ACELP fixed codebook */
  86. //#include "tty.h"
  87. /*======================================================================*/
  88. /* ..Reset RCELP encode rate. */
  89. /*----------------------------------------------------------------------*/
  90. void
  91. SetEncodeRate(INT16 rate)
  92. {
  93. /*....execute.... */
  94. bit_rate = rate;
  95. switch (bit_rate)
  96. {
  97. case 1:
  98. FCBGainSize = 16; /*...use half-rate... */
  99. gnvq = gnvq_4;
  100. break;
  101. case 3:
  102. nsub = nsub22;
  103. nsize = nsize22;
  104. lognsize = lognsize22;
  105. lsptab = lsptab22;
  106. knum = 3;
  107. FCBGainSize = 16;
  108. gnvq = gnvq_4;
  109. break;
  110. case 4:
  111. nsub = nsub28;
  112. nsize = nsize28;
  113. lognsize = lognsize28;
  114. lsptab = lsptab28;
  115. knum = 4;
  116. FCBGainSize = 32;
  117. gnvq = gnvq_8;
  118. break;
  119. }
  120. }
  121. /*======================================================================*/
  122. /* ..Reset Encoder parameters. */
  123. /*----------------------------------------------------------------------*/
  124. void InitEncoder()
  125. {
  126. /*....(local) variables.... */
  127. int j;
  128. /****************************************************/
  129. /* Algorithm (one time) initializations */
  130. /****************************************************/
  131. SetEncodeRate(4);
  132. encode_fcnt = 0;
  133. for (j = 0; j < ORDER; j++)
  134. SynMemoryM[j] = 0;
  135. OldlspE[0] = 1573;
  136. OldlspE[1] = 3146;
  137. OldlspE[2] = 4719;
  138. OldlspE[3] = 6291;
  139. OldlspE[4] = 7864;
  140. OldlspE[5] = 9437;
  141. OldlspE[6] = 11010;
  142. OldlspE[7] = 12583;
  143. OldlspE[8] = 14156;
  144. OldlspE[9] = 15729;
  145. Oldlsp_nq[0] = 1573;
  146. Oldlsp_nq[1] = 3146;
  147. Oldlsp_nq[2] = 4719;
  148. Oldlsp_nq[3] = 6291;
  149. Oldlsp_nq[4] = 7864;
  150. Oldlsp_nq[5] = 9437;
  151. Oldlsp_nq[6] = 11010;
  152. Oldlsp_nq[7] = 12583;
  153. Oldlsp_nq[8] = 14156;
  154. Oldlsp_nq[9] = 15729;
  155. for (j = 0; j < 10; j++)
  156. residual[j] = 0;
  157. for (j = 0; j < GUARD*2; j++)
  158. ConstHPspeech[j] = 0;
  159. for (j = 0; j < ORDER; j++)
  160. WFmemIIR[j] = WFmemFIR[j] = 0;
  161. for (j = 0; j < ACBMemSize; j++)
  162. Excitation[j] = 0;
  163. accshift = 0;
  164. shiftSTATE = 0;
  165. dpm = 0;
  166. pdelay = 40;
  167. LPCgain = 0x0008;
  168. }
  169. /*======================================================================*/
  170. /* ..Encode speech data. */
  171. /*----------------------------------------------------------------------*/
  172. void encode(
  173. INT16 rate,
  174. INT16 *codeBuf
  175. )
  176. {
  177. /*....(local) variables.... */
  178. register INT16 i, j, n;
  179. INT32 delayi[3];
  180. INT16 subframesize;
  181. INT16 Aveidxppg;
  182. INT16 sum1;
  183. INT32 lsum1;
  184. INT16 shft_fctr;
  185. INT32 l_fcbGain;
  186. /* Re-initialize PackWdsPtr */
  187. PackWdsPtr[0] = 16;
  188. PackWdsPtr[1] = 0;
  189. for (i = 0; i < PACKWDSNUM; i++)
  190. PackedWords[i] = 0;
  191. /*....execute.... */
  192. SetEncodeRate(rate);
  193. /* Handle 800bps seperatly */
  194. if (bit_rate == 1)
  195. {
  196. /* Reset accumulated shift */
  197. accshift = 0;
  198. dpm = 0;
  199. lspmaq(lsp_nq, ORDER, 1, 2, nsub8, nsize8, 16384, lsp_ex, SScratch, bit_rate, lsptab8);
  200. /* Bit-pack the quantization indices */
  201. for (i = 0; i < 2; i++)
  202. Bitpack(SScratch[i], (UINT16 *) PackedWords, lognsize8[i], PackWdsPtr);
  203. for (i = 0; i < NoOfSubFrames; i++)
  204. {
  205. if (i < 2)
  206. subframesize = SubFrameSize - 1;
  207. else
  208. subframesize = SubFrameSize;
  209. /* interpolate lsp */
  210. Interpol(lspi, OldlspE, lsp_ex, i, ORDER);
  211. /* Convert lsp to PC */
  212. lsp2a(pci, lspi);
  213. /* Get lpc gain */
  214. /* Calculate impulse response of 1/A(z) */
  215. ImpulseRzp(H, pci, pci, 0x7fff, 0x7fff, ORDER, Hlength);
  216. /* Get energy of H */
  217. lsum1 = 0;
  218. for (j = 0; j < subframesize; j++)
  219. lsum1 = L_mac(lsum1, H[j], H[j]);
  220. shft_fctr = norm_l(lsum1);
  221. lsum1 = L_shl(lsum1, shft_fctr);
  222. sum1 = sqroot(lsum1);
  223. if (shft_fctr & 0x0001)
  224. sum1 = mult(sum1, 23170);
  225. shft_fctr = shr(shft_fctr, 1);
  226. sum1 = shr(sum1, shft_fctr - 1);
  227. if (lastrateE != 1 && i == 0 && encode_fcnt == 0)
  228. j = 0; /* Reset seed */
  229. else
  230. j = 1;
  231. GetExc800bps(Excitation, &idxcbg, sum1, residual + GUARD + i * (SubFrameSize - 1), subframesize, j, i);
  232. /*...another puff fix... */
  233. ZeroInput(zir, pci, pci, Excitation + ACBMemSize - subframesize, GAMMA1, GAMMA2, ORDER, subframesize, 1);
  234. }
  235. /* Trap for all ones output packet. */
  236. /* If rate 1/8 packet is all ones, flip msb of gain code */
  237. /* (encoder shall never generate an all ones rate 1/8 packet..*/
  238. if ((SScratch[0]&SScratch[1]&0xf)==0xf && (idxcbg==0xff))
  239. {
  240. /* Clear Frame Energy Gain MSB if output packet == all ones */
  241. idxcbg=0x7f;
  242. }
  243. Bitpack(idxcbg, (UINT16 *) PackedWords, 8, PackWdsPtr);
  244. }
  245. else
  246. { /* bit rates 4 or 8kbps */
  247. /* Send indication on the spectrum transition */
  248. if (bit_rate == 4)
  249. Bitpack(LPCflag, (UINT16 *) PackedWords, 1, PackWdsPtr);
  250. /* Quantize the LSPs */
  251. lspmaq(lsp_nq, ORDER, 1, knum, nsub, nsize, 16384, lsp_ex, SScratch, bit_rate, lsptab);
  252. /* Bit-pack the quantization indices */
  253. for (i = 0; i < knum; i++)
  254. Bitpack(SScratch[i], (UINT16 *) PackedWords,
  255. lognsize[i], PackWdsPtr);
  256. /* Update shiftSTATE with hysteresis */
  257. if (beta < 3277)
  258. {
  259. accshift = 0;
  260. dpm = 0;
  261. shiftSTATE = 0;
  262. }
  263. if (accshift > 5120)
  264. shiftSTATE = -1;
  265. if (accshift < -5120)
  266. shiftSTATE = 1;
  267. if (accshift <= 2560 && shiftSTATE == -1)
  268. shiftSTATE = 0;
  269. if (accshift >= -2560 && shiftSTATE == 1)
  270. shiftSTATE = 0;
  271. /* Control accshift */
  272. if (shiftSTATE == 1 && beta < 13107)
  273. delay = add(delay, 1);
  274. else if (shiftSTATE == -1 && beta < 13107)
  275. delay = sub(delay, 1);
  276. if (delay > DMAX)
  277. delay = DMAX;
  278. if (delay < DMIN)
  279. delay = DMIN;
  280. #if 0
  281. if( tty_option == TTY_NO_GAIN && tty_enc_flag != 0 )
  282. {
  283. /* Pack TTY information */
  284. Bitpack((INT16) tty_enc_header, (UINT16 *) PackedWords, 2, PackWdsPtr);
  285. Bitpack((INT16) tty_enc_char, (UINT16 *) PackedWords, 5, PackWdsPtr);
  286. }
  287. else
  288. #endif
  289. {
  290. Bitpack(sub(delay, DMIN), (UINT16 *) PackedWords, 7, PackWdsPtr);
  291. }
  292. if (bit_rate == 4)
  293. {
  294. j = sub(delay, pdelay);
  295. //if (abs(j) > 15)
  296. if (abs(j) > 15) //maolin 2014-07-22
  297. j = 0;
  298. else
  299. j = add(j, 16);
  300. Bitpack(j, (UINT16 *) PackedWords, 5, PackWdsPtr);
  301. }
  302. /* Smooth interpolation if the difference between delays is too big */
  303. if (abs_s(sub(delay, pdelay)) > 15)
  304. pdelay = delay;
  305. Aveidxppg = 0;
  306. /*********************************
  307. * CELP codebook search procedure *
  308. *********************************/
  309. for (i = 0; i < NoOfSubFrames; i++)
  310. {
  311. if (i < 2)
  312. subframesize = sub(SubFrameSize, 1);
  313. else
  314. subframesize = SubFrameSize;
  315. /* interpolate lsp */
  316. Interpol(lspi, OldlspE, lsp_ex, i, ORDER);
  317. Interpol(lspi_nq, Oldlsp_nq, lsp_nq, i, ORDER);
  318. /* Convert lsp to PC */
  319. lsp2a(pci, lspi);
  320. lsp2a(pci_nq, lspi_nq);
  321. /* Get zir */
  322. ZeroInput(zir, pci_nq, pci, Excitation + ACBMemSize, GAMMA1, GAMMA2, ORDER, subframesize, 0);
  323. /* Calculate impulse response of 1/A(z) * A(z/g1) / A(z/g2) */
  324. ImpulseRzp(H, pci_nq, pci, GAMMA1, GAMMA2, ORDER, Hlength);
  325. /* Interpolate delay */
  326. Interpol_delay(delayi, &pdelay, &delay, i);
  327. ComputeACB(residualm, Excitation + ACBMemSize, delayi,
  328. residual + GUARD + i * (SubFrameSize - 1),
  329. FrameSize + GUARD - i * (SubFrameSize - 1), &dpm,
  330. &accshift, beta, subframesize, RSHIFT);
  331. /* Get weighted speech */
  332. /* ORIGM */
  333. SynthesisFilter(origm, residualm, pci_nq, SynMemoryM, ORDER, subframesize);
  334. /* Weighting filter */
  335. weight(wpci, pci_nq, GAMMA1, ORDER);
  336. fir(Scratch, origm, wpci, WFmemFIR, ORDER, subframesize);
  337. weight(wpci, pci_nq, GAMMA2, ORDER);
  338. iir(worigm, Scratch, wpci, WFmemIIR, ORDER, subframesize);
  339. /* Remove Zero input response from weighted speech */
  340. for (j = 0; j < subframesize; j++)
  341. worigm[j] = sub(worigm[j], zir[j]);
  342. /* Calculate closed loop gain */
  343. getgain(Excitation + ACBMemSize, &sum1, H, &idxppg, ppvq, ppvq_mid, ACBGainSize, 1, worigm, subframesize, Hlength);
  344. Aveidxppg = add(Aveidxppg, idxppg);
  345. /* Get TARGET for fixed c.b. */
  346. /* Convolve excitation with H */
  347. /* ExconvH stored in Scratch memory */
  348. ConvolveImpulseR(ExconvH, Excitation + ACBMemSize, H, Hlength, subframesize);
  349. for (j = 0; j < subframesize; j++)
  350. TARGETw[j] = sub(worigm[j], ExconvH[j]);
  351. /* Convert TARGET from weighted domain to residual domain */
  352. Weight2Res(TARGET, TARGETw, pci_nq, pci, GAMMA1, GAMMA2, ORDER, SubFrameSize);
  353. if (subframesize < SubFrameSize)
  354. TARGETw[subframesize] = TARGET[subframesize] = Scratch[subframesize] = 0;
  355. /* get delay for current subframe */
  356. n = extract_h(L_add(L_shr(L_add(delayi[1], delayi[0]), 1), 32768));
  357. /* Compute fixed codebook contribution */
  358. if (n > subframesize)
  359. n = 200;
  360. /* ACELP fixed codebook search */
  361. if (bit_rate == 4)
  362. {
  363. ACELP_Code(TARGETw, TARGET, H, n, sum1, subframesize, Scratch,
  364. &fcbGain, y2, fcbIndexVector, 1);
  365. /* constrain fcb gain */
  366. /* fcbGain *= (1.0 - ppvq[idxppg] * 0.15); */
  367. l_fcbGain = L_mpy_ls(L_sub(2147483647, L_mult(ppvq[idxppg], 9830)), fcbGain);
  368. }
  369. else
  370. {
  371. /** Processing half rate case **/
  372. ACELP_Code(TARGETw, TARGET, H, n, sum1, subframesize, Scratch,
  373. &fcbGain, y2, fcbIndexVector, 0);
  374. /* Constrain fcb gain */
  375. /* fcbGain *= (0.9 - ppvq[idxppg] * 0.1); */
  376. l_fcbGain = L_mpy_ls(L_sub(1932735283, L_mult(ppvq[idxppg], 6554)), fcbGain);
  377. }
  378. /* scale fcbGain +6db to account for 6db loss at input */
  379. l_fcbGain = L_shl(l_fcbGain, 1);
  380. /* Quantize FCB Gain : */
  381. fcb_gainq(&idxcbg, &fcbGain, l_fcbGain, gnvq, FCBGainSize);
  382. /* adjust fcbGain -6db */
  383. fcbGain = shr(fcbGain, 1);
  384. /* Add to total excitation */
  385. for (j = 0; j < subframesize; j++)
  386. Excitation[j + ACBMemSize] = add(Excitation[j + ACBMemSize], round32(L_mult(Scratch[j], fcbGain)));
  387. /* Update filters memory */
  388. ZeroInput(zir, pci_nq, pci, Excitation + ACBMemSize, GAMMA1, GAMMA2, ORDER, subframesize, 1);
  389. /* checking outputs */
  390. /* Update residualm */
  391. for (j = 0; j < dpm; j++)
  392. residualm[j] = residualm[j + subframesize];
  393. /* Update excitation */
  394. for (j = 0; j < ACBMemSize; j++)
  395. Excitation[j] = Excitation[j + subframesize];
  396. /* Pack bits */
  397. /* ACB gain index */
  398. Bitpack(idxppg, (UINT16 *) PackedWords, 3, PackWdsPtr);
  399. if (bit_rate == 4)
  400. {
  401. idxcb = fcbIndexVector[0];
  402. Bitpack(idxcb, (UINT16 *) PackedWords, 8, PackWdsPtr);
  403. idxcb = fcbIndexVector[1];
  404. Bitpack(idxcb, (UINT16 *) PackedWords, 8, PackWdsPtr);
  405. idxcb = fcbIndexVector[2];
  406. Bitpack(idxcb, (UINT16 *) PackedWords, 8, PackWdsPtr);
  407. idxcb = fcbIndexVector[3];
  408. Bitpack(idxcb, (UINT16 *) PackedWords, 11, PackWdsPtr);
  409. /* FCB gain index */
  410. Bitpack(idxcbg, (UINT16 *) PackedWords, 5, PackWdsPtr);
  411. }
  412. else
  413. {
  414. /** Packing half-rate bits **/
  415. idxcb = fcbIndexVector[0];
  416. /* FCB shape index 10 bits */
  417. Bitpack(idxcb, (UINT16 *) PackedWords, 10, PackWdsPtr);
  418. /* FCB gain index */
  419. Bitpack(idxcbg, (UINT16 *) PackedWords, 4, PackWdsPtr);
  420. }
  421. }
  422. /* prevent overflow */
  423. if (Aveidxppg <= 1 && idxppg != 1)
  424. {
  425. accshift = 0;
  426. dpm = 0;
  427. }
  428. /* Update encoder variables */
  429. pdelay = delay;
  430. #if 0
  431. if( tty_option == TTY_NO_GAIN && tty_enc_flag != 0 )
  432. {
  433. if( bit_rate == 4 )
  434. {
  435. /* Zero the delta delay when TTY is in the lag bits */
  436. PackedWords[2] = PackedWords[2] & ~0x0F80;
  437. /* Pack TTY Baud Rate information in Bit 171*/
  438. Bitpack((INT16) tty_enc_baud_rate, (UINT16 *) PackedWords, 1, PackWdsPtr);
  439. }
  440. }
  441. #endif
  442. } /* Ends if for rate of 4 or 8kbps */
  443. lastrateE = bit_rate;
  444. for (i = 0; i < PACKWDSNUM; i++)
  445. codeBuf[i] = PackedWords[i];
  446. encode_fcnt++;
  447. }
  448. /*======================================================================*/
  449. /* ..Save LSPs. */
  450. /*----------------------------------------------------------------------*/
  451. void post_encode()
  452. {
  453. /*....(local) variables.... */
  454. register int j;
  455. /*....execute.... */
  456. for (j = 0; j < ORDER; j++)
  457. {
  458. OldlspE[j] = lsp_ex[j];
  459. Oldlsp_nq[j] = lsp_nq[j];
  460. }
  461. }