d_no_fer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. /* Memory Usage: */
  70. /* ROM: */
  71. /* Static/Global RAM: */
  72. /* Stack/Local RAM: */
  73. /*----------------------------------------------------------------------*/
  74. /*----------------------------------------------------------------------*/
  75. /* EVRC Decoder -- (called when no frame errors have occured.) */
  76. /*======================================================================*/
  77. /* ..Includes. */
  78. /*----------------------------------------------------------------------*/
  79. #include <stdio.h>
  80. #include <string.h>
  81. #include <stdlib.h>
  82. #include "mathadv.h"
  83. //#include "mathevrc.h"
  84. #include "dsp_math.h"
  85. #include "mathdp31.h"
  86. #include "d_globs.h"
  87. #include "globs.h"
  88. #include "macro.h"
  89. #include "proto.h"
  90. #include "rom.h"
  91. #include "acelp_pf.h" /* for ACELP */
  92. extern void decode_rate_1(INT16*, INT16, INT16*);
  93. void pit_shrp(INT16 *, INT16, INT16, INT16);
  94. void Fix_delay_contour(INT32 *);
  95. /*======================================================================*/
  96. /* ..Decode bitstream data. */
  97. /*----------------------------------------------------------------------*/
  98. void decode_no_fer(
  99. INT16 *codeBuf,
  100. INT16 post_filter,
  101. INT16 *outFbuf
  102. )
  103. {
  104. /*....(local) variables.... */
  105. register INT16 i, j, n;
  106. register INT16 *foutP;
  107. INT32 delayi[3];
  108. INT16 subframesize;
  109. INT16 sum1;
  110. INT16 sum_acb;
  111. #define P75 24576
  112. #define P20 6554
  113. /*....execute.... */
  114. for (i = 0; i < PACKWDSNUM; i++){
  115. PackedWords[i] = codeBuf[i];
  116. }
  117. /* Re-initialize PackWdsPtr */
  118. PackWdsPtr[0] = 16;
  119. PackWdsPtr[1] = 0;
  120. {
  121. fer_counter -= 1;
  122. if (fer_counter < 0)
  123. fer_counter = 0;
  124. }
  125. if (bit_rate == 1)
  126. {
  127. decode_rate_1(codeBuf, post_filter, outFbuf);
  128. }
  129. else
  130. { /* If rate is 4kbps or 8kbps */
  131. if (bit_rate == 4)
  132. BitUnpack(&LPCflag, (UINT16 *) PackedWords, 1, PackWdsPtr);
  133. else
  134. LPCflag = 0;
  135. /* Bit-unpack the quantization indices */
  136. for (i = 0; i < knum; i++)
  137. BitUnpack(&SScratch[i], (UINT16 *) PackedWords,
  138. lognsize[i], PackWdsPtr);
  139. {
  140. lspmaq_dec(ORDER, 1, knum, nsub, nsize, lsp_ex, SScratch, bit_rate, lsptab);
  141. /* Check for monotonic LSP */
  142. for (j = 1; j < ORDER; j++)
  143. if (lsp_ex[j] <= lsp_ex[j - 1])
  144. {
  145. errorFlag = 1;
  146. return;
  147. }
  148. /* Check for minimum separation of LSPs at the splits */
  149. if( bit_rate == 3 ) /* Check Half Rate splits */
  150. {
  151. if( (lsp_ex[3] <= add(lsp_ex[2],MIN_LSP_SEP))
  152. || (lsp_ex[6] <= add(lsp_ex[5],MIN_LSP_SEP)))
  153. {
  154. errorFlag = 1;
  155. return;
  156. }
  157. }
  158. else
  159. { /* Check Full Rate splits */
  160. if( (lsp_ex[2] <= add(lsp_ex[1],MIN_LSP_SEP))
  161. || (lsp_ex[4] <= add(lsp_ex[3],MIN_LSP_SEP))
  162. || (lsp_ex[7] <= add(lsp_ex[6],MIN_LSP_SEP)))
  163. {
  164. errorFlag = 1;
  165. return;
  166. }
  167. }
  168. }
  169. BitUnpack(&idxppg, (UINT16 *) PackedWords, 7, PackWdsPtr);
  170. idxppg += DMIN;
  171. /* Check in delay is possible */
  172. if (idxppg > DMAX)
  173. {
  174. errorFlag = 1;
  175. return;
  176. }
  177. delay = idxppg;
  178. if (bit_rate == 4) { /* check for full-rate */
  179. /* unpack delta_delay */
  180. BitUnpack(&idxppg, (UINT16 *) PackedWords, 5, PackWdsPtr);
  181. #if ANSI_EVRC_DDELAY_CHECK
  182. /* check for out-of-range previous delay before Fix_delay_contour */
  183. if (idxppg != 0)
  184. {
  185. /* prev_delay = delay - delta_delta + 16 */
  186. n = sub(delay, sub(idxppg, 16));
  187. if (n < DMIN)
  188. {
  189. errorFlag = 1;
  190. return;
  191. }
  192. if (n > DMAX)
  193. {
  194. errorFlag = 1;
  195. return;
  196. }
  197. }
  198. #endif
  199. /* Fix delay contour */
  200. if (fer_counter == 2 && idxppg != 0)
  201. {
  202. Fix_delay_contour(delayi);
  203. }
  204. }
  205. /* Smooth interpolation if the difference between delays is too big */
  206. if (abs(sub(delay, pdelayD)) > 15)
  207. pdelayD = delay;
  208. /* Update fer coefficients */
  209. ave_acb_gain = ave_fcb_gain = 0;
  210. foutP = outFbuf;
  211. for (i = 0; i < NoOfSubFrames; i++)
  212. {
  213. if (i < 2)
  214. subframesize = SubFrameSize - 1;
  215. else
  216. subframesize = SubFrameSize;
  217. Interpol(lspi, OldlspD, lsp_ex, i, ORDER);
  218. /* Convert lsp to PC */
  219. lsp2a(pci, lspi);
  220. /* Bandwidth expansion after frame erasure only if LPCflag is set */
  221. if (bit_rate == 4)
  222. {
  223. if (LPCflag && fer_counter == 2)
  224. weight(pci, pci, P75, ORDER);
  225. }
  226. Interpol_delay(delayi, &pdelayD, &delay, i);
  227. /* Un-Pack bits */
  228. /* ACB delay gain */
  229. BitUnpack(&idxppg, (UINT16 *) PackedWords, 3, PackWdsPtr);
  230. /* FCB shape index 1 */
  231. if (bit_rate == 4)
  232. {
  233. BitUnpack(SScratch, (UINT16 *) PackedWords, 8, PackWdsPtr);
  234. fcbIndexVector[0] = *SScratch;
  235. BitUnpack(SScratch, (UINT16 *) PackedWords, 8, PackWdsPtr);
  236. fcbIndexVector[1] = *SScratch;
  237. BitUnpack(SScratch, (UINT16 *) PackedWords, 8, PackWdsPtr);
  238. fcbIndexVector[2] = *SScratch;
  239. BitUnpack(SScratch, (UINT16 *) PackedWords, 11, PackWdsPtr);
  240. fcbIndexVector[3] = *SScratch;
  241. /* FCB gain index */
  242. BitUnpack(&idxcbg, (UINT16 *) PackedWords, 5, PackWdsPtr);
  243. }
  244. else
  245. {
  246. BitUnpack(&idxcb, (UINT16 *) PackedWords, 10, PackWdsPtr);
  247. /* FCB gain index */
  248. BitUnpack(&idxcbg, (UINT16 *) PackedWords, 4, PackWdsPtr);
  249. }
  250. /* Compute adaptive codebook contribution */
  251. {
  252. sum_acb = ppvq[idxppg];
  253. ave_acb_gain = add(ave_acb_gain, mult_r(sum_acb, 10923)); /* (10923=1/NoOfSubFrames) */
  254. }
  255. acb_excitation(PitchMemoryD + ACBMemSize, sum_acb, delayi,
  256. PitchMemoryD, subframesize);
  257. /* Compute fixed codebook contribution */
  258. ave_fcb_gain = add(ave_fcb_gain, mult_r(gnvq[idxcbg], 1365)); /* (1365=(1/3)/8) */
  259. /* Compute fixed codebook contribution */
  260. if ((bit_rate == 4)) /* full rate */
  261. dec8_35(fcbIndexVector, Scratch, subframesize);
  262. if ((bit_rate == 3)) /* half rate */
  263. dec3_10(idxcb, Scratch, subframesize);
  264. {
  265. if (sum_acb > 14744)
  266. sum_acb = 14744;
  267. if (sum_acb < 3276)
  268. sum_acb = 3276;
  269. sum_acb = shl(sum_acb, 1);
  270. /* get intrpolated delay for this subframe */
  271. /* n = extract_h(L_add(L_shr(L_add(delayi[1], delayi[0]), 1), 32768)); */
  272. /* n = extract_h(L_add(L_shr(L_add(delayi[1], delayi[0]), 1), 0x00008000L)); */
  273. n = round32(L_shr(L_add(delayi[1], delayi[0]), 1));
  274. if (n > subframesize)
  275. n = 200;
  276. /* To scale down by 8 to offset (sum1=gnvq[]),
  277. * which has been scaled up by 8 */
  278. for (j = 0; j < SubFrameSize + 6; j++)
  279. Scratch[j] = shl(Scratch[j], 11);
  280. pit_shrp(Scratch, n, sum_acb, subframesize);
  281. sum1 = gnvq[idxcbg];
  282. for (j = 0; j < subframesize; j++)
  283. PitchMemoryD[j + ACBMemSize] = add(PitchMemoryD[j + ACBMemSize], mult_r(sum1, Scratch[j]));
  284. }
  285. {
  286. FadeScale = add(FadeScale, P20);
  287. }
  288. for (j = 0; j < ACBMemSize; j++)
  289. PitchMemoryD[j] = PitchMemoryD[j + subframesize];
  290. /* Synthesis of decoder output signal and postfilter output signal */
  291. iir(DECspeech, PitchMemoryD + ACBMemSize, pci, SynMemory, ORDER, subframesize);
  292. /* Postfilter */
  293. if (post_filter)
  294. {
  295. if (bit_rate == 4)
  296. apf(DECspeech, pci, DECspeechPF, L_shr(L_add(delayi[0], delayi[1]), 1), ALPHA, BETA, U, AGC, LTGAIN, ORDER, subframesize, bit_rate);
  297. else
  298. apf(DECspeech, pci, DECspeechPF, L_shr(L_add(delayi[0], delayi[1]), 1), HALF_ALPHA, BETA, HALF_U, AGC, LTGAIN, ORDER, subframesize, bit_rate);
  299. }
  300. else
  301. {
  302. for (j = 0; j < subframesize; j++)
  303. DECspeechPF[j] = DECspeech[j];
  304. }
  305. /* Write p.f. decoder output and variables to files */
  306. for (j = 0; j < subframesize; j++){
  307. *foutP++ = shl(DECspeechPF[j],1); /* adjust scaling */
  308. }
  309. }
  310. pdelayD = delay;
  311. } /* Ends case for rate=4kbps or 8kbps */
  312. /* update decoder varaibles */
  313. for (i = 0; i < ORDER; i++)
  314. OldlspD[i] = lsp_ex[i];
  315. lastrateD = bit_rate;
  316. decode_fcnt++;
  317. last_fer_flag = fer_flag;
  318. }
  319. void Fix_delay_contour(INT32 *delayi)
  320. {
  321. register int i, j;
  322. INT16 subframesize;
  323. /* Fix delay countour of previous erased frame */
  324. j = sub(idxppg, 16);
  325. pdelayD_back = pdelayD;
  326. pdelayD = sub(delay, j);
  327. if (abs(sub(pdelayD, pdelayD_back)) > 15)
  328. pdelayD_back = pdelayD;
  329. for (i = 0; i < ACBMemSize; i++)
  330. PitchMemoryD[i] = PitchMemoryD_back[i];
  331. #if 1
  332. for (i = 0; i < NoOfSubFrames; i++)
  333. {
  334. if (i < 2)
  335. subframesize = SubFrameSize - 1;
  336. else
  337. subframesize = SubFrameSize;
  338. /* Interpolate delay */
  339. Interpol_delay(delayi, &pdelayD_back, &pdelayD, i);
  340. /* Compute adaptive codebook contribution */
  341. acb_excitation(PitchMemoryD + ACBMemSize, ave_acb_gain, delayi,
  342. PitchMemoryD, subframesize);
  343. for (j = 0; j < ACBMemSize; j++)
  344. PitchMemoryD[j] = PitchMemoryD[j + subframesize];
  345. }
  346. #endif
  347. }