Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2421 lines
59 KiB

  1. //==========================================================================;
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright (c) 1993-1999 Microsoft Corporation
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. // gsm610.c
  13. //
  14. // Description:
  15. // This file contains encode and decode routines for the
  16. // GSM 06.10 standard.
  17. //
  18. //==========================================================================;
  19. #include <windows.h>
  20. #include <windowsx.h>
  21. #include <mmsystem.h>
  22. #include <mmreg.h>
  23. #include <msacm.h>
  24. #include <msacmdrv.h>
  25. #include "codec.h"
  26. #include "gsm610.h"
  27. #include "debug.h"
  28. typedef BYTE HUGE *HPBYTE;
  29. #ifdef WIN32
  30. typedef WORD UNALIGNED *HPWORD;
  31. #else
  32. typedef WORD HUGE *HPWORD;
  33. #endif
  34. //**************************************************************************
  35. /*
  36. This source module has the following structure.
  37. Section 1:
  38. Highest level functions. These functions are called from outside
  39. this module.
  40. Section 2:
  41. Encoding support functions. These functions support
  42. the encoding process.
  43. Section 3:
  44. Decoding support functions. These functions support
  45. the decoding process.
  46. Section 4:
  47. Math functions used by any of the above functions.
  48. Most of the encode and decode support routines are direct implementations of
  49. the pseudocode algorithms described in the GSM 6.10 specification. Some
  50. changes were made where necessary or where optimization was obvious or
  51. necessary.
  52. Most variables are named as in the GSM 6.10 spec, departing from the common
  53. hungarian notation. This facilitates referencing the specification when
  54. studying this implementation.
  55. Some of the functions are conditionally compiled per the definition of
  56. the WIN32 and _X86_ symbol. These functions have analogous alternate
  57. implementations in 80386 assembler (in GSM61016.ASM and GSM61032.ASM) for
  58. the purposes of execution speed. The 'C' implementations of these functions
  59. are left intact for portability and can also be referenced when studying the
  60. assembler implementations. Symbols accessed in/from GSM610xx.ASM are
  61. declared with the EXTERN_C linkage macro.
  62. */
  63. //**************************************************************************
  64. //-----------------------------------------------------------------------
  65. //-----------------------------------------------------------------------
  66. //
  67. // Typedefs
  68. //
  69. //-----------------------------------------------------------------------
  70. //-----------------------------------------------------------------------
  71. #ifndef LPSHORT
  72. typedef SHORT FAR *LPSHORT;
  73. #endif
  74. //
  75. // XM is an RPE sequence containing 13 samples. There is one
  76. // RPE sequence per sub-frame. This is typedefed in order to
  77. // facilitate passing the array thru function calls.
  78. //
  79. typedef SHORT XM[13];
  80. //-----------------------------------------------------------------------
  81. //-----------------------------------------------------------------------
  82. //
  83. // Macros
  84. //
  85. //-----------------------------------------------------------------------
  86. //-----------------------------------------------------------------------
  87. #define BITSHIFTLEFT(x,c) ( ((c)>=0) ? ((x)<<(c)) : ((x)>>(-(c))) )
  88. #define BITSHIFTRIGHT(x,c) ( ((c)>=0) ? ((x)>>(c)) : ((x)<<(-(c))) )
  89. //-----------------------------------------------------------------------
  90. //-----------------------------------------------------------------------
  91. //
  92. // function protos
  93. //
  94. //-----------------------------------------------------------------------
  95. //-----------------------------------------------------------------------
  96. //
  97. //
  98. // Math function protos
  99. //
  100. __inline SHORT add(SHORT var1, SHORT var2);
  101. __inline SHORT sub(SHORT var1, SHORT var2);
  102. __inline SHORT mult(SHORT var1, SHORT var2);
  103. __inline SHORT mult_r(SHORT var1, SHORT var2);
  104. __inline SHORT gabs(SHORT var1);
  105. __inline SHORT gdiv(SHORT var1, SHORT var2);
  106. __inline LONG l_mult(SHORT var1, SHORT var2);
  107. __inline LONG l_add(LONG l_var1, LONG l_var2);
  108. __inline LONG l_sub(LONG l_var1, LONG l_var2);
  109. __inline SHORT norm(LONG l_var1);
  110. __inline LONG IsNeg(LONG x);
  111. //
  112. // helper functions
  113. //
  114. __inline SHORT Convert8To16BitPCM(BYTE);
  115. __inline BYTE Convert16To8BitPCM(SHORT);
  116. //
  117. //
  118. // encode functions
  119. //
  120. void encodePreproc
  121. ( PSTREAMINSTANCE psi,
  122. LPSHORT sop,
  123. LPSHORT s );
  124. void encodeLPCAnalysis
  125. ( PSTREAMINSTANCE psi,
  126. LPSHORT s,
  127. LPSHORT LARc );
  128. void encodeLPCFilter
  129. ( PSTREAMINSTANCE psi,
  130. LPSHORT LARc,
  131. LPSHORT s,
  132. LPSHORT d );
  133. EXTERN_C void encodeLTPAnalysis
  134. ( PSTREAMINSTANCE psi,
  135. LPSHORT d,
  136. LPSHORT pNc,
  137. LPSHORT pbc );
  138. void encodeLTPFilter
  139. ( PSTREAMINSTANCE psi,
  140. SHORT bc,
  141. SHORT Nc,
  142. LPSHORT d,
  143. LPSHORT e,
  144. LPSHORT dpp );
  145. void encodeRPE
  146. ( PSTREAMINSTANCE psi,
  147. LPSHORT e,
  148. LPSHORT pMc,
  149. LPSHORT pxmaxc,
  150. LPSHORT xMc,
  151. LPSHORT ep );
  152. void encodeUpdate
  153. ( PSTREAMINSTANCE psi,
  154. LPSHORT ep,
  155. LPSHORT dpp );
  156. void PackFrame0
  157. ( BYTE FAR ab[],
  158. SHORT FAR LAR[],
  159. SHORT FAR N[],
  160. SHORT FAR b[],
  161. SHORT FAR M[],
  162. SHORT FAR Xmax[],
  163. XM FAR X[] );
  164. void PackFrame1
  165. ( BYTE FAR ab[],
  166. SHORT FAR LAR[],
  167. SHORT FAR N[],
  168. SHORT FAR b[],
  169. SHORT FAR M[],
  170. SHORT FAR Xmax[],
  171. XM FAR X[] );
  172. //
  173. //
  174. // decode functions
  175. //
  176. void decodeRPE
  177. ( PSTREAMINSTANCE psi,
  178. SHORT Mcr,
  179. SHORT xmaxcr,
  180. LPSHORT xMcr,
  181. LPSHORT erp );
  182. EXTERN_C void decodeLTP
  183. ( PSTREAMINSTANCE psi,
  184. SHORT bcr,
  185. SHORT Ncr,
  186. LPSHORT erp );
  187. void decodeLPC
  188. ( PSTREAMINSTANCE psi,
  189. LPSHORT LARcr,
  190. LPSHORT wt,
  191. LPSHORT sr );
  192. EXTERN_C void decodePostproc
  193. ( PSTREAMINSTANCE psi,
  194. LPSHORT sr,
  195. LPSHORT srop );
  196. void UnpackFrame0
  197. ( BYTE FAR ab[],
  198. SHORT FAR LAR[],
  199. SHORT FAR N[],
  200. SHORT FAR b[],
  201. SHORT FAR M[],
  202. SHORT FAR Xmax[],
  203. XM FAR X[] );
  204. void UnpackFrame1
  205. ( BYTE FAR ab[],
  206. SHORT FAR LAR[],
  207. SHORT FAR N[],
  208. SHORT FAR b[],
  209. SHORT FAR M[],
  210. SHORT FAR Xmax[],
  211. XM FAR X[] );
  212. //---------------------------------------------------------------------
  213. //---------------------------------------------------------------------
  214. //
  215. // Functions
  216. //
  217. //---------------------------------------------------------------------
  218. //---------------------------------------------------------------------
  219. //---------------------------------------------------------------------
  220. //
  221. // gsm610Reset(PSTREAMINSTANCE psi)
  222. //
  223. // Description:
  224. // Resets the gsm610-specific stream instance data for
  225. // the encode/decode routines
  226. //
  227. // Arguments:
  228. // PSTREAMINSTANCE psi
  229. // Pointer to stream instance structure
  230. //
  231. // Return value:
  232. // void
  233. // No return value
  234. //
  235. //---------------------------------------------------------------------
  236. void FNGLOBAL gsm610Reset(PSTREAMINSTANCE psi)
  237. {
  238. // For our gsm610 codec, almost all our instance data resets to 0
  239. UINT i;
  240. for (i=0; i<SIZEOF_ARRAY(psi->dp); i++) psi->dp[i] = 0;
  241. for (i=0; i<SIZEOF_ARRAY(psi->drp); i++) psi->drp[i] = 0;
  242. psi->z1 = 0;
  243. psi->l_z2 = 0;
  244. psi->mp = 0;
  245. for (i=0; i<SIZEOF_ARRAY(psi->OldLARpp); i++) psi->OldLARpp[i] = 0;
  246. for (i=0; i<SIZEOF_ARRAY(psi->u); i++) psi->u[i] = 0;
  247. psi->nrp = 40; // The only non-zero init
  248. for (i=0; i<SIZEOF_ARRAY(psi->OldLARrpp); i++) psi->OldLARrpp[i] = 0;
  249. psi->msr = 0;
  250. for (i=0; i<SIZEOF_ARRAY(psi->v); i++) psi->v[i] = 0;
  251. return;
  252. }
  253. //--------------------------------------------------------------------------;
  254. //
  255. // LRESULT gsm610Encode
  256. //
  257. // Description:
  258. // This function handles the ACMDM_STREAM_CONVERT message. This is the
  259. // whole purpose of writing an ACM driver--to convert data. This message
  260. // is sent after a stream has been opened (the driver receives and
  261. // succeeds the ACMDM_STREAM_OPEN message).
  262. //
  263. // Arguments:
  264. // LPACMDRVSTREAMINSTANCE padsi: Pointer to instance data for the
  265. // conversion stream. This structure was allocated by the ACM and
  266. // filled with the most common instance data needed for conversions.
  267. // The information in this structure is exactly the same as it was
  268. // during the ACMDM_STREAM_OPEN message--so it is not necessary
  269. // to re-verify the information referenced by this structure.
  270. //
  271. // LPACMDRVSTREAMHEADER padsh: Pointer to stream header structure
  272. // that defines the source data and destination buffer to convert.
  273. //
  274. // Return (LRESULT):
  275. // The return value is zero (MMSYSERR_NOERROR) if this function
  276. // succeeds with no errors. The return value is a non-zero error code
  277. // if the function fails.
  278. //
  279. //--------------------------------------------------------------------------;
  280. LRESULT FNGLOBAL gsm610Encode
  281. (
  282. LPACMDRVSTREAMINSTANCE padsi,
  283. LPACMDRVSTREAMHEADER padsh
  284. )
  285. {
  286. #if (GSM610_FRAMESPERMONOBLOCK != 2)
  287. #error THIS WAS WRITTEN FOR 2 FRAMES PER BLOCK!!!
  288. #endif
  289. #if (GSM610_MAXCHANNELS > 1)
  290. #error THIS WAS WRITTEN FOR MONO ONLY!!!
  291. #endif
  292. PSTREAMINSTANCE psi;
  293. DWORD cbSrcLen;
  294. BOOL fBlockAlign;
  295. DWORD cb;
  296. DWORD dwcSamples; // dw count of samples
  297. DWORD cBlocks;
  298. UINT i;
  299. HPBYTE hpbSrc, hpbDst;
  300. SHORT sop[GSM610_SAMPLESPERFRAME];
  301. SHORT s[GSM610_SAMPLESPERFRAME];
  302. SHORT d[GSM610_SAMPLESPERFRAME];
  303. SHORT e[GSM610_SAMPLESPERSUBFRAME];
  304. SHORT dpp[GSM610_SAMPLESPERSUBFRAME];
  305. SHORT ep[GSM610_SAMPLESPERSUBFRAME];
  306. // The GSM610 stream data:
  307. SHORT LARc[9]; // LARc[1..8] (one array per frame)
  308. SHORT Nc[GSM610_NUMSUBFRAMES]; // Nc (one per sub-frame)
  309. SHORT bc[GSM610_NUMSUBFRAMES]; // bc (one per sub-frame)
  310. SHORT Mc[GSM610_NUMSUBFRAMES]; // Mc (one per sub-frame)
  311. SHORT xmaxc[GSM610_NUMSUBFRAMES]; // Xmaxc (one per sub-frame)
  312. XM xMc[GSM610_NUMSUBFRAMES]; // xMc (one sequence per sub-frame)
  313. // Temp buffer to hold a block (two frames) of packed stream data
  314. BYTE abBlock[ GSM610_BYTESPERMONOBLOCK ];
  315. UINT nFrame;
  316. UINT cSamples;
  317. #ifdef DEBUG
  318. // ProfSetup(1000,0);
  319. // ProfStart();
  320. #endif
  321. psi = (PSTREAMINSTANCE)padsi->dwDriver;
  322. //
  323. // If this is flagged as the first block of a conversion
  324. // then reset the stream instance data.
  325. //
  326. if (0 != (ACM_STREAMCONVERTF_START & padsh->fdwConvert))
  327. {
  328. gsm610Reset(psi);
  329. }
  330. fBlockAlign = (0 != (ACM_STREAMCONVERTF_BLOCKALIGN & padsh->fdwConvert));
  331. //
  332. // -= encode PCM to GSM 6.10 =-
  333. //
  334. //
  335. //
  336. dwcSamples = PCM_BYTESTOSAMPLES(((LPPCMWAVEFORMAT)(padsi->pwfxSrc)), padsh->cbSrcLength);
  337. cBlocks = dwcSamples / GSM610_SAMPLESPERMONOBLOCK;
  338. if (!fBlockAlign)
  339. {
  340. //
  341. // Add on another block to hold the fragment of
  342. // data at the end of our source data.
  343. //
  344. if (0 != dwcSamples % GSM610_SAMPLESPERMONOBLOCK)
  345. cBlocks++;
  346. }
  347. //
  348. //
  349. //
  350. cb = cBlocks * GSM610_BLOCKALIGNMENT(padsi->pwfxDst);
  351. if (cb > padsh->cbDstLength)
  352. {
  353. return (ACMERR_NOTPOSSIBLE);
  354. }
  355. padsh->cbDstLengthUsed = cb;
  356. if (fBlockAlign)
  357. {
  358. dwcSamples = cBlocks * GSM610_SAMPLESPERMONOBLOCK;
  359. cb = PCM_SAMPLESTOBYTES(((LPPCMWAVEFORMAT)(padsi->pwfxSrc)), dwcSamples);
  360. }
  361. else
  362. {
  363. cb = padsh->cbSrcLength;
  364. }
  365. padsh->cbSrcLengthUsed = cb;
  366. //
  367. //
  368. //
  369. cbSrcLen = padsh->cbSrcLengthUsed;
  370. // Setup huge pointers to our src and dst buffers
  371. hpbSrc = (HPBYTE)padsh->pbSrc;
  372. hpbDst = (HPBYTE)padsh->pbDst;
  373. // Loop thru entire source buffer
  374. while (cbSrcLen)
  375. {
  376. // Process source buffer as two full GSM610 frames
  377. for (nFrame=0; nFrame < 2; nFrame++)
  378. {
  379. //
  380. // the src contains 8- or 16-bit PCM. currently we only
  381. // handle mono conversions.
  382. //
  383. //
  384. // we will fill sop[] with one frame of 16-bit PCM samples
  385. //
  386. //
  387. // copy min( cSrcSamplesLeft, GSM610_SAMPLESPERFRAME ) samples
  388. // to array sop[].
  389. //
  390. dwcSamples = PCM_BYTESTOSAMPLES(((LPPCMWAVEFORMAT)(padsi->pwfxSrc)), cbSrcLen);
  391. cSamples = (int) min(dwcSamples, (DWORD) GSM610_SAMPLESPERFRAME);
  392. if (padsi->pwfxSrc->wBitsPerSample == 16)
  393. {
  394. // copy 16-bit samples from hpbSrc to sop
  395. for (i=0; i < cSamples; i++)
  396. {
  397. sop[i] = *( ((HPWORD)hpbSrc)++ );
  398. }
  399. }
  400. else
  401. {
  402. // copy 8-bit samples from hpbSrc to 16-bit samples in sop
  403. for (i=0; i < cSamples; i++)
  404. {
  405. sop[i] = Convert8To16BitPCM(*hpbSrc++);
  406. }
  407. }
  408. cbSrcLen -= PCM_SAMPLESTOBYTES(((LPPCMWAVEFORMAT)(padsi->pwfxSrc)), cSamples);
  409. // fill out sop[] with silence if necessary.
  410. for ( ; i < GSM610_SAMPLESPERFRAME; i++)
  411. {
  412. sop[i] = 0;
  413. }
  414. //
  415. // Encode a frame of data
  416. //
  417. encodePreproc(psi, sop, s);
  418. encodeLPCAnalysis(psi, s, LARc);
  419. encodeLPCFilter(psi, LARc, s, d);
  420. // For each of four sub-frames
  421. for (i=0; i<4; i++)
  422. {
  423. encodeLTPAnalysis(psi, &d[i*40], &Nc[i], &bc[i]);
  424. encodeLTPFilter(psi, bc[i], Nc[i], &d[i*40], e, dpp);
  425. encodeRPE(psi, e, &Mc[i], &xmaxc[i], xMc[i], ep);
  426. encodeUpdate(psi, ep, dpp);
  427. }
  428. //
  429. // Pack the data and store in dst buffer
  430. //
  431. if (nFrame == 0)
  432. PackFrame0(abBlock, LARc, Nc, bc, Mc, xmaxc, xMc);
  433. else
  434. {
  435. PackFrame1(abBlock, LARc, Nc, bc, Mc, xmaxc, xMc);
  436. for (i=0; i<GSM610_BYTESPERMONOBLOCK; i++)
  437. *(hpbDst++) = abBlock[i];
  438. }
  439. } // for (nFrame...
  440. }
  441. #ifdef DEBUG
  442. // ProfStop();
  443. #endif
  444. return (MMSYSERR_NOERROR);
  445. }
  446. //--------------------------------------------------------------------------;
  447. //
  448. // LRESULT gsm610Decode
  449. //
  450. // Description:
  451. // This function handles the ACMDM_STREAM_CONVERT message. This is the
  452. // whole purpose of writing an ACM driver--to convert data. This message
  453. // is sent after a stream has been opened (the driver receives and
  454. // succeeds the ACMDM_STREAM_OPEN message).
  455. //
  456. // Arguments:
  457. // LPACMDRVSTREAMINSTANCE padsi: Pointer to instance data for the
  458. // conversion stream. This structure was allocated by the ACM and
  459. // filled with the most common instance data needed for conversions.
  460. // The information in this structure is exactly the same as it was
  461. // during the ACMDM_STREAM_OPEN message--so it is not necessary
  462. // to re-verify the information referenced by this structure.
  463. //
  464. // LPACMDRVSTREAMHEADER padsh: Pointer to stream header structure
  465. // that defines the source data and destination buffer to convert.
  466. //
  467. // Return (LRESULT):
  468. // The return value is zero (MMSYSERR_NOERROR) if this function
  469. // succeeds with no errors. The return value is a non-zero error code
  470. // if the function fails.
  471. //
  472. //--------------------------------------------------------------------------;
  473. LRESULT FNGLOBAL gsm610Decode
  474. (
  475. LPACMDRVSTREAMINSTANCE padsi,
  476. LPACMDRVSTREAMHEADER padsh
  477. )
  478. {
  479. #if (GSM610_FRAMESPERMONOBLOCK != 2)
  480. #error THIS WAS WRITTEN FOR 2 FRAMES PER BLOCK!!!
  481. #endif
  482. #if (GSM610_MAXCHANNELS > 1)
  483. #error THIS WAS WRITTEN FOR MONO ONLY!!!
  484. #endif
  485. PSTREAMINSTANCE psi;
  486. DWORD cbSrcLen;
  487. BOOL fBlockAlign;
  488. DWORD cb;
  489. DWORD dwcSamples;
  490. DWORD cBlocks;
  491. HPBYTE hpbSrc, hpbDst;
  492. SHORT erp[GSM610_SAMPLESPERSUBFRAME];
  493. SHORT wt[GSM610_SAMPLESPERFRAME];
  494. SHORT sr[GSM610_SAMPLESPERFRAME];
  495. SHORT srop[GSM610_SAMPLESPERFRAME];
  496. // The GSM610 stream data:
  497. SHORT LARcr[9]; // LARc[1..8] (one array per frame)
  498. SHORT Ncr[GSM610_NUMSUBFRAMES]; // Nc (one per sub-frame)
  499. SHORT bcr[GSM610_NUMSUBFRAMES]; // bc (one per sub-frame)
  500. SHORT Mcr[GSM610_NUMSUBFRAMES]; // Mc (one per sub-frame)
  501. SHORT xmaxcr[GSM610_NUMSUBFRAMES]; // Xmaxc (one per sub-frame)
  502. XM xMcr[GSM610_NUMSUBFRAMES]; // xMc (one sequence per sub-frame)
  503. UINT i,j;
  504. UINT nFrame;
  505. // Temp buffer to hold a block (two frames) of packed stream data
  506. BYTE abBlock[ GSM610_BYTESPERMONOBLOCK ];
  507. #ifdef DEBUG
  508. // ProfStart();
  509. #endif
  510. psi = (PSTREAMINSTANCE)padsi->dwDriver;
  511. // If this is flagged as the first block of a conversion
  512. // then reset the stream instance data.
  513. if (0 != (ACM_STREAMCONVERTF_START & padsh->fdwConvert))
  514. {
  515. gsm610Reset(psi);
  516. }
  517. fBlockAlign = (0 != (ACM_STREAMCONVERTF_BLOCKALIGN & padsh->fdwConvert));
  518. //
  519. // -= decode GSM 6.10 to PCM =-
  520. //
  521. //
  522. cb = padsh->cbSrcLength;
  523. cBlocks = cb / GSM610_BLOCKALIGNMENT(padsi->pwfxSrc);
  524. if (0L == cBlocks)
  525. {
  526. padsh->cbSrcLengthUsed = cb;
  527. padsh->cbDstLengthUsed = 0L;
  528. return (MMSYSERR_NOERROR);
  529. }
  530. //
  531. // Compute bytes we will use in destination buffer. Carefull! Look
  532. // out for overflow in our calculations!
  533. //
  534. if ((0xFFFFFFFFL / GSM610_SAMPLESPERMONOBLOCK) < cBlocks)
  535. return (ACMERR_NOTPOSSIBLE);
  536. dwcSamples = cBlocks * GSM610_SAMPLESPERMONOBLOCK;
  537. if (PCM_BYTESTOSAMPLES(((LPPCMWAVEFORMAT)(padsi->pwfxDst)), 0xFFFFFFFFL) < dwcSamples)
  538. return (ACMERR_NOTPOSSIBLE);
  539. cb = PCM_SAMPLESTOBYTES(((LPPCMWAVEFORMAT)(padsi->pwfxDst)), dwcSamples);
  540. if (cb > padsh->cbDstLength)
  541. {
  542. return (ACMERR_NOTPOSSIBLE);
  543. }
  544. padsh->cbDstLengthUsed = cb;
  545. padsh->cbSrcLengthUsed = cBlocks * GSM610_BLOCKALIGNMENT(padsi->pwfxSrc);
  546. //
  547. //
  548. //
  549. cbSrcLen = padsh->cbSrcLengthUsed;
  550. // Setup huge pointers to our src and dst buffers
  551. hpbSrc = (HPBYTE)padsh->pbSrc;
  552. hpbDst = (HPBYTE)padsh->pbDst;
  553. // while at least another full block of coded data
  554. while (cbSrcLen >= GSM610_BYTESPERMONOBLOCK)
  555. {
  556. // copy a block of data from stream buffer to our temp buffer
  557. for (i=0; i<GSM610_BYTESPERMONOBLOCK; i++) abBlock[i] = *(hpbSrc++);
  558. cbSrcLen -= GSM610_BYTESPERMONOBLOCK;
  559. // for each of the two frames in the block
  560. for (nFrame=0; nFrame < 2; nFrame++)
  561. {
  562. // Unpack data from stream
  563. if (nFrame == 0)
  564. UnpackFrame0(abBlock, LARcr, Ncr, bcr, Mcr, xmaxcr, xMcr);
  565. else
  566. UnpackFrame1(abBlock, LARcr, Ncr, bcr, Mcr, xmaxcr, xMcr);
  567. for (i=0; i<4; i++) // for each of 4 sub-blocks
  568. {
  569. // reconstruct the long term residual signal erp[0..39]
  570. // from Mcr, xmaxcr, and xMcr
  571. decodeRPE(psi, Mcr[i], xmaxcr[i], xMcr[i], erp);
  572. // reconstruct the short term residual signal drp[0..39]
  573. // and also update drp[-120..-1]
  574. decodeLTP(psi, bcr[i], Ncr[i], erp);
  575. // accumulate the four sub-blocks of reconstructed short
  576. // term residual signal drp[0..39] into wt[0..159]
  577. for (j=0; j<40; j++) wt[(i*40) + j] = psi->drp[120+j];
  578. }
  579. // reconstruct the signal s
  580. decodeLPC(psi, LARcr, wt, sr);
  581. // post-process the signal s
  582. decodePostproc(psi, sr, srop);
  583. //
  584. // write decoded 16-bit PCM to dst. our dst format
  585. // may be 8- or 16-bit PCM.
  586. //
  587. if (padsi->pwfxDst->wBitsPerSample == 16)
  588. {
  589. // copy 16-bit samples from srop to hpbDst
  590. for (j=0; j < GSM610_SAMPLESPERFRAME; j++)
  591. {
  592. *( ((HPWORD)hpbDst)++ ) = srop[j];
  593. }
  594. }
  595. else
  596. {
  597. // copy 16-bit samples from srop to 8-bit samples in hpbDst
  598. for (j=0; j < GSM610_SAMPLESPERFRAME; j++)
  599. {
  600. *(hpbDst++) = Convert16To8BitPCM(srop[j]);
  601. }
  602. }
  603. } // for (nFrame...
  604. }
  605. #ifdef DEBUG
  606. // ProfStop();
  607. #endif
  608. return (MMSYSERR_NOERROR);
  609. }
  610. //=====================================================================
  611. //=====================================================================
  612. //
  613. // Encode routines
  614. //
  615. //=====================================================================
  616. //=====================================================================
  617. //---------------------------------------------------------------------
  618. //--------------------------------------------------------------------
  619. //
  620. // Function protos
  621. //
  622. //---------------------------------------------------------------------
  623. //---------------------------------------------------------------------
  624. EXTERN_C void CompACF(LPSHORT s, LPLONG l_ACF);
  625. void Compr(PSTREAMINSTANCE psi, LPLONG l_ACF, LPSHORT r);
  626. void CompLAR(PSTREAMINSTANCE psi, LPSHORT r, LPSHORT LAR);
  627. void CompLARc(PSTREAMINSTANCE psi, LPSHORT LAR, LPSHORT LARc);
  628. void CompLARpp(PSTREAMINSTANCE psi, LPSHORT LARc, LPSHORT LARpp);
  629. void CompLARp(PSTREAMINSTANCE psi, LPSHORT LARpp, LPSHORT LARp1, LPSHORT LARp2, LPSHORT LARp3, LPSHORT LARp4);
  630. void Comprp(PSTREAMINSTANCE psi, LPSHORT LARp, LPSHORT rp);
  631. EXTERN_C void Compd(PSTREAMINSTANCE psi, LPSHORT rp, LPSHORT s, LPSHORT d, UINT k_start, UINT k_end);
  632. void WeightingFilter(PSTREAMINSTANCE psi, LPSHORT e, LPSHORT x);
  633. void RPEGridSelect(PSTREAMINSTANCE psi, LPSHORT x, LPSHORT pMc, LPSHORT xM);
  634. void APCMQuantize(PSTREAMINSTANCE psi, LPSHORT xM, LPSHORT pxmaxc, LPSHORT xMc, LPSHORT pexp, LPSHORT pmant);
  635. void APCMInvQuantize(PSTREAMINSTANCE psi, SHORT exp, SHORT mant, LPSHORT xMc, LPSHORT xMp);
  636. void RPEGridPosition(PSTREAMINSTANCE psi, SHORT Mc, LPSHORT xMp, LPSHORT ep);
  637. //---------------------------------------------------------------------
  638. //---------------------------------------------------------------------
  639. //
  640. // Global constant data
  641. //
  642. //---------------------------------------------------------------------
  643. //---------------------------------------------------------------------
  644. const SHORT BCODE A[9] = {
  645. 0, // not used
  646. 20480, 20480, 20480, 20480, 13964, 15360, 8534, 9036 };
  647. const SHORT BCODE B[9] = {
  648. 0, // not used
  649. 0, 0, 2048, -2560, 94, -1792, -341, -1144 };
  650. const SHORT BCODE MIC[9] = {
  651. 0, // not used
  652. -32, -32, -16, -16, -8, -8, -4, -4 };
  653. const SHORT BCODE MAC[9] = {
  654. 0, // not used
  655. 31, 31, 15, 15, 7, 7, 3, 3 };
  656. const SHORT BCODE INVA[9] = {
  657. 0, // unused
  658. 13107, 13107, 13107, 13107, 19223, 17476, 31454, 29708 };
  659. EXTERN_C const SHORT BCODE DLB[4] = { 6554, 16384, 26214, 32767 };
  660. EXTERN_C const SHORT BCODE QLB[4] = { 3277, 11469, 21299, 32767 };
  661. const SHORT BCODE H[11] = { -134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134 };
  662. const SHORT BCODE NRFAC[8] = { 29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384 };
  663. const SHORT BCODE FAC[8] = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 };
  664. //---------------------------------------------------------------------
  665. //---------------------------------------------------------------------
  666. //
  667. // Procedures
  668. //
  669. //---------------------------------------------------------------------
  670. //---------------------------------------------------------------------
  671. //---------------------------------------------------------------------
  672. //
  673. // PackFrame0
  674. //
  675. //---------------------------------------------------------------------
  676. void PackFrame0
  677. (
  678. BYTE FAR ab[],
  679. SHORT FAR LAR[],
  680. SHORT FAR N[],
  681. SHORT FAR b[],
  682. SHORT FAR M[],
  683. SHORT FAR Xmax[],
  684. XM FAR X[]
  685. )
  686. {
  687. int i;
  688. // Pack the LAR[1..8] into the first 4.5 bytes
  689. ab[0] = ((LAR[1] ) & 0x3F) | ((LAR[2] << 6) & 0xC0);
  690. ab[1] = ((LAR[2] >> 2) & 0x0F) | ((LAR[3] << 4) & 0xF0);
  691. ab[2] = ((LAR[3] >> 4) & 0x01) | ((LAR[4] << 1) & 0x3E) | ((LAR[5] << 6) & 0xC0);
  692. ab[3] = ((LAR[5] >> 2) & 0x03) | ((LAR[6] << 2) & 0x3C) | ((LAR[7] << 6) & 0xC0);
  693. ab[4] = ((LAR[7] >> 2) & 0x01) | ((LAR[8] << 1) & 0x0E);
  694. // Pack N, b, M, Xmax, and X for each of the 4 sub-frames
  695. for (i=0; i<4; i++)
  696. {
  697. ab[4+i*7+0] |= ((N[i] << 4) & 0xF0);
  698. ab[4+i*7+1] = ((N[i] >> 4) & 0x07) | ((b[i] << 3) & 0x18) | ((M[i] << 5) & 0x60) | ((Xmax[i] << 7) & 0x80);
  699. ab[4+i*7+2] = ((Xmax[i] >> 1) & 0x1F) | ((X[i][0] << 5) & 0xE0);
  700. ab[4+i*7+3] = (X[i][1] & 0x07) | ((X[i][2] << 3) & 0x38) | ((X[i][3] << 6) & 0xC0);
  701. ab[4+i*7+4] = ((X[i][3] >> 2) & 0x01) | ((X[i][4] << 1) & 0x0E) | ((X[i][5] << 4) & 0x70) | ((X[i][6] << 7) & 0x80);
  702. ab[4+i*7+5] = ((X[i][6] >> 1) & 0x03) | ((X[i][7] << 2) & 0x1C) | ((X[i][8] << 5) & 0xE0);
  703. ab[4+i*7+6] = (X[i][9] & 0x07) | ((X[i][10] << 3) & 0x38) | ((X[i][11] << 6) & 0xC0);
  704. ab[4+i*7+7] = ((X[i][11] >> 2) & 0x01) | ((X[i][12] << 1) & 0x0E);
  705. }
  706. return;
  707. }
  708. //---------------------------------------------------------------------
  709. //
  710. // PackFrame1
  711. //
  712. //---------------------------------------------------------------------
  713. void PackFrame1
  714. (
  715. BYTE FAR ab[],
  716. SHORT FAR LAR[],
  717. SHORT FAR N[],
  718. SHORT FAR b[],
  719. SHORT FAR M[],
  720. SHORT FAR Xmax[],
  721. XM FAR X[]
  722. )
  723. {
  724. int i;
  725. // Pack the LAR[1..8] into the first 4.5 bytes, starting with the
  726. // more significant nibble of the first byte.
  727. ab[32] |= ((LAR[1] << 4) & 0xF0);
  728. ab[33] = ((LAR[1] >> 4) & 0x03) | ((LAR[2] << 2) & 0xFC);
  729. ab[34] = ((LAR[3] ) & 0x1F) | ((LAR[4] << 5) & 0xE0);
  730. ab[35] = ((LAR[4] >> 3) & 0x03) | ((LAR[5] << 2) & 0x3C) | ((LAR[6] << 6) & 0xC0);
  731. ab[36] = ((LAR[6] >> 2) & 0x03) | ((LAR[7] << 2) & 0x1C) | ((LAR[8] << 5) & 0xE0);
  732. // Pack N, b, M, Xmax, and X for each of the 4 sub-frames
  733. for (i=0; i<4; i++)
  734. {
  735. ab[37+i*7+0] = (N[i] & 0x7F) | ((b[i] << 7) & 0x80);
  736. ab[37+i*7+1] = ((b[i] >> 1) & 0x01) | ((M[i] << 1) & 0x06) | ((Xmax[i] << 3) & 0xF8);
  737. ab[37+i*7+2] = ((Xmax[i] >> 5) & 0x01) | ((X[i][0] << 1) & 0x0E) | ((X[i][1] << 4) & 0x70) | ((X[i][2] << 7) & 0x80);
  738. ab[37+i*7+3] = ((X[i][2] >> 1) & 0x03) | ((X[i][3] << 2) & 0x1C) | ((X[i][4] << 5) & 0xE0);
  739. ab[37+i*7+4] = ((X[i][5] ) & 0x07) | ((X[i][6] << 3) & 0x38) | ((X[i][7] << 6) & 0xC0);
  740. ab[37+i*7+5] = ((X[i][7] >> 2) & 0x01) | ((X[i][8] << 1) & 0x0E) | ((X[i][9] << 4) & 0x70) | ((X[i][10] << 7) & 0x80);
  741. ab[37+i*7+6] = ((X[i][10] >> 1) & 0x03) | ((X[i][11] << 2) & 0x1C) | ((X[i][12] << 5) & 0xE0);
  742. }
  743. return;
  744. }
  745. //---------------------------------------------------------------------
  746. //
  747. // encodePreproc()
  748. //
  749. //---------------------------------------------------------------------
  750. void encodePreproc(PSTREAMINSTANCE psi, LPSHORT sop, LPSHORT s)
  751. {
  752. SHORT so[160];
  753. SHORT sof[160];
  754. UINT k;
  755. SHORT s1;
  756. SHORT temp;
  757. SHORT msp, lsp;
  758. LONG l_s2;
  759. // downscale
  760. for (k=0; k<160; k++)
  761. {
  762. so[k] = sop[k] >> 3;
  763. so[k] = so[k] << 2;
  764. }
  765. // offset compensation
  766. for (k=0; k<160; k++)
  767. {
  768. // Compute the non-recursive part
  769. s1 = sub(so[k], psi->z1);
  770. psi->z1 = so[k];
  771. // compute the recursive part
  772. l_s2 = s1;
  773. l_s2 = l_s2 << 15;
  774. // execution of 31 by 16 bits multiplication
  775. msp = (SHORT) (psi->l_z2 >> 15);
  776. lsp = (SHORT) l_sub(psi->l_z2, ( ((LONG)msp) << 15));
  777. temp = mult_r(lsp, 32735);
  778. l_s2 = l_add(l_s2, temp);
  779. psi->l_z2 = l_add(l_mult(msp, 32735) >> 1, l_s2);
  780. // compute sof[k] with rounding
  781. sof[k] = (SHORT) (l_add(psi->l_z2, 16384) >> 15);
  782. }
  783. // preemphasis
  784. for (k=0; k<160; k++)
  785. {
  786. s[k] = add(sof[k], mult_r(psi->mp, -28180));
  787. psi->mp = sof[k];
  788. }
  789. return;
  790. }
  791. //---------------------------------------------------------------------
  792. //
  793. // encodeLPCAnalysis()
  794. //
  795. //---------------------------------------------------------------------
  796. void encodeLPCAnalysis(PSTREAMINSTANCE psi, LPSHORT s, LPSHORT LARc)
  797. {
  798. LONG l_ACF[9];
  799. SHORT r[9];
  800. SHORT LAR[9];
  801. CompACF(s, l_ACF);
  802. Compr(psi, l_ACF, r);
  803. CompLAR(psi, r, LAR);
  804. CompLARc(psi, LAR, LARc);
  805. return;
  806. }
  807. //---------------------------------------------------------------------
  808. //
  809. // CompACF()
  810. //
  811. //---------------------------------------------------------------------
  812. void CompACF(LPSHORT s, LPLONG l_ACF)
  813. {
  814. SHORT smax, temp, scalauto;
  815. UINT i, k;
  816. //
  817. // Dynamic scaling of array s[0..159]
  818. //
  819. // Search for the maximum
  820. smax = 0;
  821. for (k=0; k<160; k++)
  822. {
  823. temp = gabs(s[k]);
  824. if (temp > smax) smax = temp;
  825. }
  826. // Computation of the scaling factor
  827. if (smax == 0) scalauto = 0;
  828. else scalauto = sub( 4, norm( ((LONG)smax)<<16 ) );
  829. // Scaling of the array s
  830. if (scalauto > 0)
  831. {
  832. temp = BITSHIFTRIGHT(16384, sub(scalauto,1));
  833. for (k=0; k<160; k++)
  834. {
  835. // s[k] = mult_r(s[k], temp);
  836. s[k] = HIWORD( ( (((LONG)s[k])<<(15-scalauto)) + 0x4000L ) << 1 );
  837. }
  838. }
  839. //
  840. // Compute the l_ACF[..]
  841. //
  842. for (k=0; k<9; k++)
  843. {
  844. l_ACF[k] = 0;
  845. for (i=k; i<160; i++)
  846. {
  847. l_ACF[k] = l_add(l_ACF[k], l_mult(s[i], s[i-k]));
  848. }
  849. }
  850. //
  851. // Rescaling of array s
  852. //
  853. if (scalauto > 0)
  854. {
  855. for (k=0; k<160; k++)
  856. {
  857. // We don't need the BITSHIFTLEFT macro
  858. // cuz we know scalauto>0 due to above test
  859. s[k] = s[k] << scalauto;
  860. }
  861. }
  862. //
  863. //
  864. //
  865. return;
  866. }
  867. //---------------------------------------------------------------------
  868. //
  869. // Compr()
  870. //
  871. //---------------------------------------------------------------------
  872. void Compr(PSTREAMINSTANCE psi, LPLONG l_ACF, LPSHORT r)
  873. {
  874. UINT i, k, m, n;
  875. SHORT temp, ACF[9];
  876. SHORT K[9], P[9]; // K[2..8], P[0..8]
  877. //
  878. // Schur recursion with 16 bits arithmetic
  879. //
  880. if (l_ACF[0] == 0)
  881. {
  882. for (i=1; i<=8; i++)
  883. {
  884. r[i] = 0;
  885. }
  886. return;
  887. }
  888. temp = norm(l_ACF[0]);
  889. for (k=0; k<=8; k++)
  890. {
  891. ACF[k] = (SHORT) ((BITSHIFTLEFT(l_ACF[k], temp)) >> 16);
  892. }
  893. //
  894. // Init array P and K for the recursion
  895. //
  896. for (i=1; i<=7; i++)
  897. {
  898. K[9-i] = ACF[i];
  899. }
  900. for (i=0; i<=8; i++)
  901. {
  902. P[i] = ACF[i];
  903. }
  904. //
  905. // Compute reflection coefficients
  906. //
  907. for (n=1; n<=8; n++)
  908. {
  909. if (P[0] < gabs(P[1]))
  910. {
  911. for (i=n; i<=8; i++)
  912. {
  913. r[i] = 0;
  914. }
  915. return;
  916. }
  917. r[n] = gdiv(gabs(P[1]),P[0]);
  918. if (P[1] > 0) r[n] = sub(0,r[n]);
  919. // Here's the real exit from this for loop
  920. if (n==8) return;
  921. // Schur recursion
  922. P[0] = add(P[0], mult_r(P[1], r[n]));
  923. for (m=1; m<=8-n; m++)
  924. {
  925. P[m] = add( P[m+1], mult_r(K[9-m],r[n]) );
  926. K[9-m] = add( K[9-m], mult_r(P[m+1], r[n]) );
  927. }
  928. }
  929. }
  930. //---------------------------------------------------------------------
  931. //
  932. // CompLAR()
  933. //
  934. //---------------------------------------------------------------------
  935. void CompLAR(PSTREAMINSTANCE psi, LPSHORT r, LPSHORT LAR)
  936. {
  937. UINT i;
  938. SHORT temp;
  939. //
  940. // Computation of LAR[1..8] from r[1..8]
  941. //
  942. for (i=1; i<=8; i++)
  943. {
  944. temp = gabs(r[i]);
  945. if (temp < 22118)
  946. {
  947. temp = temp >> 1;
  948. }
  949. else if (temp < 31130)
  950. {
  951. temp = sub(temp, 11059);
  952. }
  953. else
  954. {
  955. temp = sub(temp, 26112) << 2;
  956. }
  957. LAR[i] = temp;
  958. if (r[i] < 0)
  959. {
  960. LAR[i] = sub(0, LAR[i]);
  961. }
  962. }
  963. return;
  964. }
  965. //---------------------------------------------------------------------
  966. //
  967. // CompLARc()
  968. //
  969. //---------------------------------------------------------------------
  970. void CompLARc(PSTREAMINSTANCE psi, LPSHORT LAR, LPSHORT LARc)
  971. {
  972. UINT i;
  973. SHORT temp;
  974. for (i=1; i<=8; i++)
  975. {
  976. temp = mult(A[i], LAR[i]);
  977. temp = add(temp, B[i]);
  978. temp = add(temp, 256);
  979. LARc[i] = temp >> 9;
  980. // Check if LARc[i] between MIN and MAX
  981. if (LARc[i] > MAC[i]) LARc[i] = MAC[i];
  982. if (LARc[i] < MIC[i]) LARc[i] = MIC[i];
  983. // This is used to make all LARc positive
  984. LARc[i] = sub(LARc[i], MIC[i]);
  985. }
  986. return;
  987. }
  988. //---------------------------------------------------------------------
  989. //
  990. // encodeLPCFilter()
  991. //
  992. //---------------------------------------------------------------------
  993. void encodeLPCFilter(PSTREAMINSTANCE psi, LPSHORT LARc, LPSHORT s, LPSHORT d)
  994. {
  995. SHORT LARpp[9]; // array [1..8]
  996. SHORT LARp1[9], LARp2[9], LARp3[9], LARp4[9]; // array [1..8]
  997. SHORT rp[9]; // array [1..8]
  998. CompLARpp(psi, LARc, LARpp);
  999. CompLARp(psi, LARpp, LARp1, LARp2, LARp3, LARp4);
  1000. Comprp(psi, LARp1, rp);
  1001. Compd(psi, (LPSHORT)rp, s, d, 0, 12);
  1002. Comprp(psi, LARp2, rp);
  1003. Compd(psi, (LPSHORT)rp, s, d, 13, 26);
  1004. Comprp(psi, LARp3, rp);
  1005. Compd(psi, (LPSHORT)rp, s, d, 27, 39);
  1006. Comprp(psi, LARp4, rp);
  1007. Compd(psi, (LPSHORT)rp, s, d, 40, 159);
  1008. return;
  1009. }
  1010. //---------------------------------------------------------------------
  1011. //
  1012. // CompLARpp()
  1013. //
  1014. //---------------------------------------------------------------------
  1015. void CompLARpp(PSTREAMINSTANCE psi, LPSHORT LARc, LPSHORT LARpp)
  1016. {
  1017. UINT i;
  1018. SHORT temp1, temp2;
  1019. for (i=1; i<=8; i++)
  1020. {
  1021. temp1 = add(LARc[i], MIC[i]) << 10;
  1022. temp2 = B[i] << 1;
  1023. temp1 = sub(temp1,temp2);
  1024. temp1 = mult_r(INVA[i], temp1);
  1025. LARpp[i] = add(temp1, temp1);
  1026. }
  1027. return;
  1028. }
  1029. //---------------------------------------------------------------------
  1030. //
  1031. // CompLARp()
  1032. //
  1033. //---------------------------------------------------------------------
  1034. void CompLARp(PSTREAMINSTANCE psi, LPSHORT LARpp, LPSHORT LARp1, LPSHORT LARp2, LPSHORT LARp3, LPSHORT LARp4)
  1035. {
  1036. UINT i;
  1037. for (i=1; i<=8; i++)
  1038. {
  1039. LARp1[i] = add( (SHORT)(psi->OldLARpp[i] >> 2), (SHORT)(LARpp[i] >> 2) );
  1040. LARp1[i] = add( LARp1[i], (SHORT)(psi->OldLARpp[i] >> 1) );
  1041. LARp2[i] = add( (SHORT)(psi->OldLARpp[i] >> 1), (SHORT)(LARpp[i] >> 1) );
  1042. LARp3[i] = add( (SHORT)(psi->OldLARpp[i] >> 2), (SHORT)(LARpp[i] >> 2) );
  1043. LARp3[i] = add( LARp3[i], (SHORT)(LARpp[i] >> 1) );
  1044. LARp4[i] = LARpp[i];
  1045. }
  1046. for (i=1; i<=8; i++)
  1047. {
  1048. psi->OldLARpp[i] = LARpp[i];
  1049. }
  1050. return;
  1051. }
  1052. //---------------------------------------------------------------------
  1053. //
  1054. // Comprp()
  1055. //
  1056. //---------------------------------------------------------------------
  1057. void Comprp(PSTREAMINSTANCE psi, LPSHORT LARp, LPSHORT rp)
  1058. {
  1059. UINT i;
  1060. SHORT temp;
  1061. for (i=1; i<=8; i++)
  1062. {
  1063. temp = gabs(LARp[i]);
  1064. if (temp < 11059)
  1065. {
  1066. temp = temp << 1;
  1067. }
  1068. else if (temp < 20070)
  1069. {
  1070. temp = add(temp, 11059);
  1071. }
  1072. else
  1073. {
  1074. temp = add((SHORT)(temp>>2), 26112);
  1075. }
  1076. rp[i] = temp;
  1077. if (LARp[i] < 0)
  1078. {
  1079. rp[i] = sub(0,rp[i]);
  1080. }
  1081. }
  1082. return;
  1083. }
  1084. //---------------------------------------------------------------------
  1085. //
  1086. // Compd()
  1087. //
  1088. //---------------------------------------------------------------------
  1089. void Compd(PSTREAMINSTANCE psi, LPSHORT rp, LPSHORT s, LPSHORT d, UINT k_start, UINT k_end)
  1090. {
  1091. UINT k, i;
  1092. SHORT sav;
  1093. SHORT di;
  1094. SHORT temp;
  1095. for (k=k_start; k<=k_end; k++)
  1096. {
  1097. di = s[k];
  1098. sav = di;
  1099. for (i=1; i<=8; i++)
  1100. {
  1101. temp = add( psi->u[i-1], mult_r(rp[i],di) );
  1102. di = add( di, mult_r(rp[i], psi->u[i-1]) );
  1103. psi->u[i-1] = sav;
  1104. sav = temp;
  1105. }
  1106. d[k] = di;
  1107. }
  1108. return;
  1109. }
  1110. //---------------------------------------------------------------------
  1111. //
  1112. // encodeLTPAnalysis()
  1113. //
  1114. //---------------------------------------------------------------------
  1115. void encodeLTPAnalysis(PSTREAMINSTANCE psi, LPSHORT d, LPSHORT pNc, LPSHORT pbc)
  1116. {
  1117. SHORT dmax;
  1118. SHORT temp;
  1119. SHORT scal;
  1120. SHORT wt[40];
  1121. SHORT lambda;
  1122. LONG l_max, l_power;
  1123. SHORT R, S;
  1124. SHORT Nc;
  1125. int k; // k must be int, not UINT!
  1126. Nc = *pNc;
  1127. // Search of the optimum scaling of d[0..39]
  1128. dmax = 0;
  1129. for (k=39; k>=0; k--)
  1130. {
  1131. temp = gabs( d[k] );
  1132. if (temp > dmax) dmax = temp;
  1133. }
  1134. temp = 0;
  1135. if (dmax == 0) scal = 0;
  1136. else temp = norm( ((LONG)dmax) << 16);
  1137. if (temp > 6) scal = 0;
  1138. else scal = sub(6,temp);
  1139. // Init of working array wt[0..39]
  1140. ASSERT( scal >= 0 );
  1141. for (k=39; k>=0; k--)
  1142. {
  1143. wt[k] = d[k] >> scal;
  1144. }
  1145. // Search for max cross-correlation and coding of LTP lag
  1146. l_max = 0;
  1147. Nc = 40;
  1148. for (lambda=40; lambda<=120; lambda++)
  1149. {
  1150. register LONG l_result = 0;
  1151. for (k=39; k>=0; k--)
  1152. {
  1153. l_result += (LONG)(wt[k]) * (LONG)(psi->dp[120-lambda+k]);
  1154. }
  1155. if (l_result > l_max)
  1156. {
  1157. Nc = lambda;
  1158. l_max = l_result;
  1159. }
  1160. }
  1161. l_max <<= 1; // This operation should be on l_result as part of the
  1162. // multiply/add, but for efficiency we shift it all
  1163. // the way out of the loops.
  1164. // Rescaling of l_max
  1165. ASSERT( sub(6,scal) >= 0 );
  1166. l_max = l_max >> sub(6,scal);
  1167. // Compute the power of the reconstructed short term residual
  1168. // signal dp[..].
  1169. l_power = 0;
  1170. {
  1171. SHORT s;
  1172. for (k=39; k>=0; k--)
  1173. {
  1174. s = psi->dp[120-Nc+k] >> 3;
  1175. l_power += s*s; // This sum can never overflow!!!
  1176. }
  1177. ASSERT( l_power >= 0 );
  1178. if( l_power >= 1073741824 ) { // 2**30
  1179. l_power = 2147483647; // 2**31 - 1
  1180. } else {
  1181. l_power <<= 1; // This shift is normally part of l_mult().
  1182. }
  1183. }
  1184. *pNc = Nc;
  1185. // Normalization of l_max and l_power
  1186. if (l_max <= 0)
  1187. {
  1188. *pbc = 0;
  1189. return;
  1190. }
  1191. if (l_max >= l_power)
  1192. {
  1193. *pbc = 3;
  1194. return;
  1195. }
  1196. temp = norm(l_power);
  1197. ASSERT( temp >= 0 );
  1198. R = (SHORT) ((l_max<<temp) >> 16);
  1199. S = (SHORT) ((l_power<<temp) >> 16);
  1200. // Codeing of the LTP gain
  1201. for ( *pbc=0; *pbc<=2; (*pbc)++ )
  1202. {
  1203. if (R <= mult(S, DLB[*pbc]))
  1204. {
  1205. return;
  1206. }
  1207. }
  1208. *pbc = 3;
  1209. return;
  1210. }
  1211. //---------------------------------------------------------------------
  1212. //
  1213. // encodeLTPFilter()
  1214. //
  1215. //---------------------------------------------------------------------
  1216. void encodeLTPFilter(PSTREAMINSTANCE psi, SHORT bc, SHORT Nc, LPSHORT d, LPSHORT e, LPSHORT dpp)
  1217. {
  1218. SHORT bp;
  1219. UINT k;
  1220. // Decoding of the coded LTP gain
  1221. bp = QLB[bc];
  1222. // Calculating the array e[0..39] and the array dpp[0..39]
  1223. for (k=0; k<=39; k++)
  1224. {
  1225. dpp[k] = mult_r(bp, psi->dp[120+k-Nc]);
  1226. e[k] = sub(d[k], dpp[k]);
  1227. }
  1228. return;
  1229. }
  1230. //---------------------------------------------------------------------
  1231. //
  1232. // encodeRPE()
  1233. //
  1234. //---------------------------------------------------------------------
  1235. void encodeRPE(PSTREAMINSTANCE psi, LPSHORT e, LPSHORT pMc, LPSHORT pxmaxc, LPSHORT xMc, LPSHORT ep)
  1236. {
  1237. SHORT x[40];
  1238. SHORT xM[13];
  1239. SHORT exp, mant;
  1240. SHORT xMp[13];
  1241. WeightingFilter(psi, e, x);
  1242. RPEGridSelect(psi, x, pMc, xM);
  1243. APCMQuantize(psi, xM, pxmaxc, xMc, &exp, &mant);
  1244. APCMInvQuantize(psi, exp, mant, xMc, xMp);
  1245. RPEGridPosition(psi, *pMc, xMp, ep);
  1246. return;
  1247. }
  1248. //---------------------------------------------------------------------
  1249. //
  1250. // WeightingFilter()
  1251. //
  1252. //---------------------------------------------------------------------
  1253. void WeightingFilter(PSTREAMINSTANCE psi, LPSHORT e, LPSHORT x)
  1254. {
  1255. UINT i, k;
  1256. LONG l_result, l_temp;
  1257. SHORT wt[50];
  1258. // Initialization of a temporary working array wt[0..49]
  1259. for (k= 0; k<= 4; k++) wt[k] = 0;
  1260. for (k= 5; k<=44; k++) wt[k] = e[k-5];
  1261. for (k=45; k<=49; k++) wt[k] = 0;
  1262. // Compute the signal x[0..39]
  1263. for (k=0; k<=39; k++)
  1264. {
  1265. l_result = 8192; // rounding of the output of the filter
  1266. for (i=0; i<=10; i++)
  1267. {
  1268. l_temp = l_mult(wt[k+i], H[i]);
  1269. l_result = l_add(l_result, l_temp);
  1270. }
  1271. l_result = l_add(l_result, l_result); // scaling x2
  1272. l_result = l_add(l_result, l_result); // scaling x4
  1273. x[k] = (SHORT) (l_result >> 16);
  1274. }
  1275. return;
  1276. }
  1277. //---------------------------------------------------------------------
  1278. //
  1279. // RPEGridSelect()
  1280. //
  1281. //---------------------------------------------------------------------
  1282. void RPEGridSelect(PSTREAMINSTANCE psi, LPSHORT x, LPSHORT pMc, LPSHORT xM)
  1283. {
  1284. UINT m, i;
  1285. LONG l_EM;
  1286. SHORT temp1;
  1287. LONG l_result, l_temp;
  1288. // the signal x[0..39] is used to select the RPE grid which is
  1289. // represented by Mc
  1290. l_EM = 0;
  1291. *pMc = 0;
  1292. for (m=0; m<=3; m++)
  1293. {
  1294. l_result = 0;
  1295. for (i=0; i<=12; i++)
  1296. {
  1297. temp1 = x[m+(3*i)] >> 2;
  1298. l_temp = l_mult(temp1, temp1);
  1299. l_result = l_add(l_temp, l_result);
  1300. }
  1301. if (l_result > l_EM)
  1302. {
  1303. *pMc = (SHORT)m;
  1304. l_EM = l_result;
  1305. }
  1306. }
  1307. // down-sampling by a factor of 3 to get the selected xM[0..12]
  1308. // RPE sequence
  1309. for (i=0; i<=12; i++)
  1310. {
  1311. xM[i] = x[*pMc + (3*i)];
  1312. }
  1313. return;
  1314. }
  1315. //---------------------------------------------------------------------
  1316. //
  1317. // APCMQuantize()
  1318. //
  1319. //---------------------------------------------------------------------
  1320. void APCMQuantize(PSTREAMINSTANCE psi, LPSHORT xM, LPSHORT pxmaxc, LPSHORT xMc, LPSHORT pexp, LPSHORT pmant)
  1321. {
  1322. UINT i;
  1323. SHORT xmax;
  1324. SHORT temp;
  1325. SHORT itest;
  1326. SHORT temp1, temp2;
  1327. // find the maximum absolute value xmax or xM[0..12]
  1328. xmax = 0;
  1329. for (i=0; i<=12; i++)
  1330. {
  1331. temp = gabs(xM[i]);
  1332. if (temp > xmax) xmax = temp;
  1333. }
  1334. // quantizing and coding of xmax to get xmaxc
  1335. *pexp = 0;
  1336. temp = xmax >> 9;
  1337. itest = 0;
  1338. for (i=0; i<=5; i++)
  1339. {
  1340. if (temp <=0) itest = 1;
  1341. temp = temp >> 1;
  1342. if (itest == 0) *pexp = add(*pexp,1);
  1343. }
  1344. temp = add(*pexp,5);
  1345. *pxmaxc = add( (SHORT)BITSHIFTRIGHT(xmax,temp), (SHORT)(*pexp << 3) );
  1346. //
  1347. // quantizing and coding of the xM[0..12] RPE sequence to get
  1348. // the xMc[0..12]
  1349. //
  1350. // compute exponent and mantissa of the decoded version of xmaxc
  1351. *pexp = 0;
  1352. if (*pxmaxc > 15) *pexp = sub((SHORT)(*pxmaxc >> 3),1);
  1353. *pmant = sub(*pxmaxc,(SHORT)(*pexp<<3));
  1354. // normalize mantissa 0 <= mant <= 7
  1355. if (*pmant==0)
  1356. {
  1357. *pexp = -4;
  1358. *pmant = 15;
  1359. }
  1360. else
  1361. {
  1362. itest = 0;
  1363. for (i=0; i<=2; i++)
  1364. {
  1365. if (*pmant > 7) itest = 1;
  1366. if (itest == 0) *pmant = add((SHORT)(*pmant << 1),1);
  1367. if (itest == 0) *pexp = sub(*pexp,1);
  1368. }
  1369. }
  1370. *pmant = sub(*pmant,8);
  1371. // direct computation of xMc[0..12] using table
  1372. temp1 = sub(6,*pexp); // normalization by the exponent
  1373. temp2 = NRFAC[*pmant]; // see table (inverse mantissa)
  1374. for (i=0; i<=12; i++)
  1375. {
  1376. temp = BITSHIFTLEFT(xM[i], temp1);
  1377. temp = mult( temp, temp2 );
  1378. xMc[i] = add( (SHORT)(temp >> 12), 4 ); // makes all xMc[i] positive
  1379. }
  1380. return;
  1381. }
  1382. //---------------------------------------------------------------------
  1383. //
  1384. // APCMInvQuantize()
  1385. //
  1386. //---------------------------------------------------------------------
  1387. void APCMInvQuantize(PSTREAMINSTANCE psi, SHORT exp, SHORT mant, LPSHORT xMc, LPSHORT xMp)
  1388. {
  1389. SHORT temp1, temp2, temp3, temp;
  1390. UINT i;
  1391. temp1 = FAC[mant];
  1392. temp2 = sub(6,exp);
  1393. temp3 = BITSHIFTLEFT(1, sub(temp2,1));
  1394. for (i=0; i<=12; i++)
  1395. {
  1396. temp = sub( (SHORT)(xMc[i] << 1), 7); // restores sign of xMc[i]
  1397. temp = temp << 12;
  1398. temp = mult_r(temp1, temp);
  1399. temp = add(temp, temp3);
  1400. xMp[i] = BITSHIFTRIGHT(temp,temp2);
  1401. }
  1402. return;
  1403. }
  1404. //---------------------------------------------------------------------
  1405. //
  1406. // RPEGridPosition(SHORT Mc, LPSHORT xMp, LPSHORT ep)
  1407. //
  1408. //---------------------------------------------------------------------
  1409. void RPEGridPosition(PSTREAMINSTANCE psi, SHORT Mc, LPSHORT xMp, LPSHORT ep)
  1410. {
  1411. UINT k, i;
  1412. for (k=0; k<=39; k++)
  1413. {
  1414. ep[k] = 0;
  1415. }
  1416. for (i=0; i<=12; i++)
  1417. {
  1418. ep[Mc + (3*i)] = xMp[i];
  1419. }
  1420. return;
  1421. }
  1422. //---------------------------------------------------------------------
  1423. //
  1424. // encodeUpdate()
  1425. //
  1426. //---------------------------------------------------------------------
  1427. void encodeUpdate(PSTREAMINSTANCE psi, LPSHORT ep, LPSHORT dpp)
  1428. {
  1429. UINT k;
  1430. for (k=0; k<=79; k++)
  1431. psi->dp[120-120+k] = psi->dp[120-80+k];
  1432. for (k=0; k<=39; k++)
  1433. psi->dp[120-40+k] = add(ep[k], dpp[k]);
  1434. return;
  1435. }
  1436. //=====================================================================
  1437. //=====================================================================
  1438. //
  1439. // Decode routines
  1440. //
  1441. //=====================================================================
  1442. //=====================================================================
  1443. //---------------------------------------------------------------------
  1444. //---------------------------------------------------------------------
  1445. //
  1446. // Function protos
  1447. //
  1448. //---------------------------------------------------------------------
  1449. //---------------------------------------------------------------------
  1450. EXTERN_C void Compsr(PSTREAMINSTANCE psi, LPSHORT wt, LPSHORT rrp, UINT k_start, UINT k_end, LPSHORT sr);
  1451. //---------------------------------------------------------------------
  1452. //---------------------------------------------------------------------
  1453. //
  1454. // Procedures
  1455. //
  1456. //---------------------------------------------------------------------
  1457. //---------------------------------------------------------------------
  1458. //---------------------------------------------------------------------
  1459. //
  1460. // UnpackFrame0
  1461. //
  1462. //---------------------------------------------------------------------
  1463. void UnpackFrame0
  1464. (
  1465. BYTE FAR ab[],
  1466. SHORT FAR LAR[],
  1467. SHORT FAR N[],
  1468. SHORT FAR b[],
  1469. SHORT FAR M[],
  1470. SHORT FAR Xmax[],
  1471. XM FAR X[]
  1472. )
  1473. {
  1474. UINT i;
  1475. // Unpack the LAR[1..8] from the first 4.5 bytes
  1476. LAR[1] = (ab[0] & 0x3F);
  1477. LAR[2] = ((ab[0] & 0xC0) >> 6) | ((ab[1] & 0x0F) << 2);
  1478. LAR[3] = ((ab[1] & 0xF0) >> 4) | ((ab[2] & 0x01) << 4);
  1479. LAR[4] = ((ab[2] & 0x3E) >> 1);
  1480. LAR[5] = ((ab[2] & 0xC0) >> 6) | ((ab[3] & 0x03) << 2);
  1481. LAR[6] = ((ab[3] & 0x3C) >> 2);
  1482. LAR[7] = ((ab[3] & 0xC0) >> 6) | ((ab[4] & 0x01) << 2);
  1483. LAR[8] = ((ab[4] & 0x0E) >> 1);
  1484. // Unpack N, b, M, Xmax, and X for each of the four sub-frames
  1485. for (i=0; i<4; i++)
  1486. {
  1487. // A convenient macro for getting bytes out of the array for
  1488. // construction of the subframe parameters
  1489. #define sfb(x) (ab[4+i*7+x])
  1490. N[i] = ((sfb(0) & 0xF0) >> 4) | ((sfb(1) & 0x07) << 4);
  1491. b[i] = ((sfb(1) & 0x18) >> 3);
  1492. M[i] = ((sfb(1) & 0x60) >> 5);
  1493. Xmax[i] = ((sfb(1) & 0x80) >> 7) | ((sfb(2) & 0x1F) << 1);
  1494. X[i][0] = ((sfb(2) & 0xE0) >> 5);
  1495. X[i][1] = (sfb(3) & 0x07);
  1496. X[i][2] = ((sfb(3) & 0x3C) >> 3);
  1497. X[i][3] = ((sfb(3) & 0xC0) >> 6) | ((sfb(4) & 0x01) << 2);
  1498. X[i][4] = ((sfb(4) & 0x0E) >> 1);
  1499. X[i][5] = ((sfb(4) & 0x70) >> 4);
  1500. X[i][6] = ((sfb(4) & 0x80) >> 7) | ((sfb(5) & 0x03) << 1);
  1501. X[i][7] = ((sfb(5) & 0x1C) >> 2);
  1502. X[i][8] = ((sfb(5) & 0xE0) >> 5);
  1503. X[i][9] = (sfb(6) & 0x07);
  1504. X[i][10] = ((sfb(6) & 0x38) >> 3);
  1505. X[i][11] = ((sfb(6) & 0xC0) >> 6) | ((sfb(7) & 0x01) << 2);
  1506. X[i][12] = ((sfb(7) & 0x0E) >> 1);
  1507. #undef sfb
  1508. }
  1509. return;
  1510. }
  1511. //---------------------------------------------------------------------
  1512. //
  1513. // UnpackFrame1
  1514. //
  1515. //---------------------------------------------------------------------
  1516. void UnpackFrame1
  1517. (
  1518. BYTE FAR ab[],
  1519. SHORT FAR LAR[],
  1520. SHORT FAR N[],
  1521. SHORT FAR b[],
  1522. SHORT FAR M[],
  1523. SHORT FAR Xmax[],
  1524. XM FAR X[]
  1525. )
  1526. {
  1527. UINT i;
  1528. // Unpack the LAR[1..8] from the first 4.5 bytes
  1529. LAR[1] = ((ab[32] & 0xF0) >> 4) | ((ab[33] & 0x03) << 4);
  1530. LAR[2] = ((ab[33] & 0xFC) >> 2);
  1531. LAR[3] = ((ab[34] & 0x1F) );
  1532. LAR[4] = ((ab[34] & 0xE0) >> 5) | ((ab[35] & 0x03) << 3);
  1533. LAR[5] = ((ab[35] & 0x3C) >> 2);
  1534. LAR[6] = ((ab[35] & 0xC0) >> 6) | ((ab[36] & 0x03) << 2);
  1535. LAR[7] = ((ab[36] & 0x1C) >> 2);
  1536. LAR[8] = ((ab[36] & 0xE0) >> 5);
  1537. // Unpack N, b, M, Xmax, and X for each of the four sub-frames
  1538. for (i=0; i<4; i++)
  1539. {
  1540. // A convenient macro for getting bytes out of the array for
  1541. // construction of the subframe parameters
  1542. #define sfb(x) (ab[37+i*7+x])
  1543. N[i] = sfb(0) & 0x7F;
  1544. b[i] = ((sfb(0) & 0x80) >> 7) | ((sfb(1) & 0x01) << 1);
  1545. M[i] = ((sfb(1) & 0x06) >> 1);
  1546. Xmax[i] = ((sfb(1) & 0xF8) >> 3) | ((sfb(2) & 0x01) << 5);
  1547. X[i][0] = ((sfb(2) & 0x0E) >> 1);
  1548. X[i][1] = ((sfb(2) & 0x70) >> 4);
  1549. X[i][2] = ((sfb(2) & 0x80) >> 7) | ((sfb(3) & 0x03) << 1);
  1550. X[i][3] = ((sfb(3) & 0x1C) >> 2);
  1551. X[i][4] = ((sfb(3) & 0xE0) >> 5);
  1552. X[i][5] = ((sfb(4) & 0x07) );
  1553. X[i][6] = ((sfb(4) & 0x38) >> 3);
  1554. X[i][7] = ((sfb(4) & 0xC0) >> 6) | ((sfb(5) & 0x01) << 2);
  1555. X[i][8] = ((sfb(5) & 0x0E) >> 1);
  1556. X[i][9] = ((sfb(5) & 0x70) >> 4);
  1557. X[i][10] = ((sfb(5) & 0x80) >> 7) | ((sfb(6) & 0x03) << 1);
  1558. X[i][11] = ((sfb(6) & 0x1C) >> 2);
  1559. X[i][12] = ((sfb(6) & 0xE0) >> 5);
  1560. #undef sfb
  1561. }
  1562. return;
  1563. }
  1564. //---------------------------------------------------------------------
  1565. //
  1566. // decodeRPE()
  1567. //
  1568. //---------------------------------------------------------------------
  1569. void decodeRPE(PSTREAMINSTANCE psi, SHORT Mcr, SHORT xmaxcr, LPSHORT xMcr, LPSHORT erp)
  1570. {
  1571. SHORT exp, mant;
  1572. SHORT itest;
  1573. UINT i;
  1574. SHORT temp1, temp2, temp3, temp;
  1575. SHORT xMrp[13];
  1576. UINT k;
  1577. // compute the exponent and mantissa of the decoded
  1578. // version of xmaxcr
  1579. exp = 0;
  1580. if (xmaxcr > 15) exp = sub( (SHORT)(xmaxcr >> 3), 1 );
  1581. mant = sub( xmaxcr, (SHORT)(exp << 3) );
  1582. // normalize the mantissa 0 <= mant <= 7
  1583. if (mant == 0)
  1584. {
  1585. exp = -4;
  1586. mant = 15;
  1587. }
  1588. else
  1589. {
  1590. itest = 0;
  1591. for (i=0; i<=2; i++)
  1592. {
  1593. if (mant > 7) itest = 1;
  1594. if (itest == 0) mant = add((SHORT)(mant << 1),1);
  1595. if (itest == 0) exp = sub(exp,1);
  1596. }
  1597. }
  1598. mant = sub(mant, 8);
  1599. // APCM inverse quantization
  1600. temp1 = FAC[mant];
  1601. temp2 = sub(6,exp);
  1602. temp3 = BITSHIFTLEFT(1, sub(temp2, 1));
  1603. for (i=0; i<=12; i++)
  1604. {
  1605. temp = sub( (SHORT)(xMcr[i] << 1), 7 );
  1606. temp = temp << 12;
  1607. temp = mult_r(temp1, temp);
  1608. temp = add(temp, temp3);
  1609. xMrp[i] = BITSHIFTRIGHT(temp, temp2);
  1610. }
  1611. // RPE grid positioning
  1612. for (k=0; k<=39; k++) erp[k] = 0;
  1613. for (i=0; i<=12; i++) erp[Mcr + (3*i)] = xMrp[i];
  1614. //
  1615. return;
  1616. }
  1617. //---------------------------------------------------------------------
  1618. //
  1619. // decodeLTP()
  1620. //
  1621. //---------------------------------------------------------------------
  1622. void decodeLTP(PSTREAMINSTANCE psi, SHORT bcr, SHORT Ncr, LPSHORT erp)
  1623. {
  1624. SHORT Nr;
  1625. SHORT brp;
  1626. UINT k;
  1627. SHORT drpp;
  1628. // check limits of Nr
  1629. Nr = Ncr;
  1630. if (Ncr < 40) Nr = psi->nrp;
  1631. if (Ncr > 120) Nr = psi->nrp;
  1632. psi->nrp = Nr;
  1633. // decoding of the LTP gain bcr
  1634. brp = QLB[bcr];
  1635. // computation of the reconstructed short term residual
  1636. // signal drp[0..39]
  1637. for (k=0; k<=39; k++)
  1638. {
  1639. drpp = mult_r( brp, psi->drp[120+k-Nr] );
  1640. psi->drp[120+k] = add( erp[k], drpp );
  1641. }
  1642. // update of the reconstructed short term residual
  1643. // signal drp[-1..-120]
  1644. for (k=0; k<=119; k++)
  1645. {
  1646. psi->drp[120-120+k] = psi->drp[120-80+k];
  1647. }
  1648. return;
  1649. }
  1650. //---------------------------------------------------------------------
  1651. //
  1652. // decodeLPC
  1653. //
  1654. //---------------------------------------------------------------------
  1655. void decodeLPC
  1656. (
  1657. PSTREAMINSTANCE psi, // instance data
  1658. LPSHORT LARcr, // received coded Log.-Area Ratios [1..8]
  1659. LPSHORT wt, // accumulated drp signal [0..159]
  1660. LPSHORT sr // reconstructed s [0..159]
  1661. )
  1662. {
  1663. UINT i;
  1664. SHORT LARrpp[9]; // LARrpp[1..8], decoded LARcr
  1665. SHORT LARrp[9]; // LARrp[1..9], interpolated LARrpp
  1666. SHORT rrp[9]; // rrp[1..8], reflection coefficients
  1667. SHORT temp1, temp2;
  1668. //
  1669. // decoding of the coded log area ratios to get LARrpp[1..8]
  1670. //
  1671. // compute LARrpp[1..8]
  1672. for (i=1; i<=8; i++)
  1673. {
  1674. temp1 = add( LARcr[i], MIC[i] ) << 10;
  1675. temp2 = B[i] << 1;
  1676. temp1 = sub( temp1, temp2);
  1677. temp1 = mult_r( INVA[i], temp1 );
  1678. LARrpp[i] = add( temp1, temp1 );
  1679. }
  1680. //
  1681. // for k_start=0 to k_end=12
  1682. //
  1683. // interpolation of LARrpp[1..8] to get LARrp[1..8]
  1684. for (i=1; i<=8; i++)
  1685. {
  1686. // for k_start=0 to k_end=12
  1687. LARrp[i] = add( (SHORT)(psi->OldLARrpp[i] >> 2), (SHORT)(LARrpp[i] >> 2) );
  1688. LARrp[i] = add( LARrp[i], (SHORT)(psi->OldLARrpp[i] >> 1) );
  1689. }
  1690. // computation of reflection coefficients rrp[1..8]
  1691. Comprp(psi, LARrp, rrp);
  1692. // short term synthesis filtering
  1693. Compsr(psi, wt, rrp, 0, 12, sr);
  1694. //
  1695. // for k_start=13 to k_end=26
  1696. //
  1697. // interpolation of LARrpp[1..8] to get LARrp[1..8]
  1698. for (i=1; i<=8; i++)
  1699. {
  1700. // for k_start=13 to k_end=26
  1701. LARrp[i] = add( (SHORT)(psi->OldLARrpp[i] >> 1), (SHORT)(LARrpp[i] >> 1) );
  1702. }
  1703. // computation of reflection coefficients rrp[1..8]
  1704. Comprp(psi, LARrp, rrp);
  1705. // short term synthesis filtering
  1706. Compsr(psi, wt, rrp, 13, 26, sr);
  1707. //
  1708. // for k_start=27 to k_end=39
  1709. //
  1710. // interpolation of LARrpp[1..8] to get LARrp[1..8]
  1711. for (i=1; i<=8; i++)
  1712. {
  1713. // for k_start=27 to k_end=39
  1714. LARrp[i] = add( (SHORT)(psi->OldLARrpp[i] >> 2), (SHORT)(LARrpp[i] >> 2) );
  1715. LARrp[i] = add( LARrp[i], (SHORT)(LARrpp[i] >> 1) );
  1716. }
  1717. // computation of reflection coefficients rrp[1..8]
  1718. Comprp(psi, LARrp, rrp);
  1719. // short term synthesis filtering
  1720. Compsr(psi, wt, rrp, 27, 39, sr);
  1721. //
  1722. // for k_start=40 to k_end=159
  1723. //
  1724. // interpolation of LARrpp[1..8] to get LARrp[1..8]
  1725. for (i=1; i<=8; i++)
  1726. {
  1727. // for k_start=40 to k_end=159
  1728. LARrp[i] = LARrpp[i];
  1729. }
  1730. // computation of reflection coefficients rrp[1..8]
  1731. Comprp(psi, LARrp, rrp);
  1732. // short term synthesis filtering
  1733. Compsr(psi, wt, rrp, 40, 159, sr);
  1734. //
  1735. // update oldLARrpp[1..8]
  1736. //
  1737. for (i=1; i<=8; i++)
  1738. {
  1739. psi->OldLARrpp[i] = LARrpp[i];
  1740. }
  1741. return;
  1742. }
  1743. //---------------------------------------------------------------------
  1744. //
  1745. // decodePostproc()
  1746. //
  1747. //---------------------------------------------------------------------
  1748. void decodePostproc(PSTREAMINSTANCE psi, LPSHORT sr, LPSHORT srop)
  1749. {
  1750. UINT k;
  1751. // deemphasis filtering
  1752. for (k=0; k<=159; k++)
  1753. {
  1754. srop[k] = psi->msr = add(sr[k], mult_r(psi->msr, 28180));
  1755. // upscaling and truncation of the output signal
  1756. srop[k] = (add(srop[k], srop[k])) & 0xFFF8;
  1757. }
  1758. return;
  1759. }
  1760. //---------------------------------------------------------------------
  1761. //
  1762. // Compsr()
  1763. //
  1764. //---------------------------------------------------------------------
  1765. void Compsr(PSTREAMINSTANCE psi, LPSHORT wt, LPSHORT rrp, UINT k_start, UINT k_end, LPSHORT sr)
  1766. {
  1767. UINT i, k;
  1768. SHORT sri;
  1769. for (k=k_start; k<=k_end; k++)
  1770. {
  1771. sri = wt[k];
  1772. for (i=1; i<=8; i++)
  1773. {
  1774. sri = sub( sri, mult_r(rrp[9-i], psi->v[8-i]) );
  1775. psi->v[9-i] = add( psi->v[8-i], mult_r( rrp[9-i], sri ) );
  1776. }
  1777. sr[k] = sri;
  1778. psi->v[0] = sri;
  1779. }
  1780. return;
  1781. }
  1782. //=====================================================================
  1783. //=====================================================================
  1784. //
  1785. // Math and helper routines
  1786. //
  1787. //=====================================================================
  1788. //=====================================================================
  1789. //
  1790. // The 8-/16-bit PCM conversion routines are implemented as seperate
  1791. // functions to allow easy modification if we someday wish to do
  1792. // something more sophisticated that simple truncation... They are
  1793. // prototyped as inline so there should be no performance penalty.
  1794. //
  1795. //
  1796. SHORT Convert8To16BitPCM(BYTE bPCM8)
  1797. {
  1798. return ( ((SHORT)bPCM8) - 0x80 ) << 8;
  1799. }
  1800. BYTE Convert16To8BitPCM(SHORT iPCM16)
  1801. {
  1802. return (BYTE)((iPCM16 >> 8) + 0x80);
  1803. }
  1804. SHORT add(SHORT var1, SHORT var2)
  1805. {
  1806. LONG sum;
  1807. sum = (LONG) var1 + (LONG) var2;
  1808. if (sum < -32768L) return -32768;
  1809. if (sum > 32767L) return 32767;
  1810. return (SHORT) sum;
  1811. }
  1812. SHORT sub(SHORT var1, SHORT var2)
  1813. {
  1814. LONG diff;
  1815. diff = (LONG) var1 - (LONG) var2;
  1816. if (diff < -32768L) return -32768;
  1817. if (diff > 32767L) return 32767;
  1818. return (SHORT) diff;
  1819. }
  1820. SHORT mult(SHORT var1, SHORT var2)
  1821. {
  1822. LONG product;
  1823. product = (LONG) var1 * (LONG) var2;
  1824. if (product >= 0x40000000) product=0x3FFFFFFF;
  1825. return ( (SHORT) HIWORD((DWORD)(product<<1)) );
  1826. }
  1827. SHORT mult_r(SHORT var1, SHORT var2)
  1828. {
  1829. LONG product;
  1830. product = ((LONG) var1 * (LONG) var2) + 16384L;
  1831. if (product >= 0x40000000) product=0x3FFFFFFF;
  1832. return ( (SHORT) HIWORD((DWORD)(product<<1)) );
  1833. }
  1834. SHORT gabs(SHORT var1)
  1835. {
  1836. if (var1 >= 0) return var1;
  1837. if (var1 == -32768) return 32767;
  1838. return -var1;
  1839. }
  1840. SHORT gdiv(SHORT num, SHORT denum)
  1841. {
  1842. UINT k;
  1843. LONG l_num, l_denum;
  1844. SHORT div;
  1845. l_num = num;
  1846. l_denum = denum;
  1847. div = 0;
  1848. for (k=0; k<15; k++)
  1849. {
  1850. div = div << 1;
  1851. l_num = l_num << 1;
  1852. if (l_num >= l_denum)
  1853. {
  1854. l_num = l_sub(l_num, l_denum);
  1855. div = add(div,1);
  1856. }
  1857. }
  1858. return div;
  1859. }
  1860. LONG l_mult(SHORT var1, SHORT var2)
  1861. {
  1862. LONG product;
  1863. product = (LONG) var1 * (LONG) var2;
  1864. return product << 1;
  1865. }
  1866. LONG l_add(LONG l_var1, LONG l_var2)
  1867. {
  1868. LONG l_sum;
  1869. // perform long addition
  1870. l_sum = l_var1 + l_var2;
  1871. // check for under or overflow
  1872. if (IsNeg(l_var1))
  1873. {
  1874. if (IsNeg(l_var2) && !IsNeg(l_sum))
  1875. {
  1876. return 0x80000000;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. if (!IsNeg(l_var2) && IsNeg(l_sum))
  1882. {
  1883. return 0x7FFFFFFF;
  1884. }
  1885. }
  1886. return l_sum;
  1887. }
  1888. LONG l_sub(LONG l_var1, LONG l_var2)
  1889. {
  1890. LONG l_diff;
  1891. // perform subtraction
  1892. l_diff = l_var1 - l_var2;
  1893. // check for underflow
  1894. if ( (l_var1<0) && (l_var2>0) && (l_diff>0) ) l_diff=0x80000000;
  1895. // check for overflow
  1896. if ( (l_var1>0) && (l_var2<0) && (l_diff<0) ) l_diff=0x7FFFFFFF;
  1897. return l_diff;
  1898. }
  1899. SHORT norm(LONG l_var)
  1900. {
  1901. UINT i;
  1902. i=0;
  1903. if (l_var > 0)
  1904. {
  1905. while (l_var < 1073741824)
  1906. {
  1907. i++;
  1908. l_var = l_var << 1;
  1909. }
  1910. }
  1911. else if (l_var < 0)
  1912. {
  1913. while (l_var > -1073741824)
  1914. {
  1915. i++;
  1916. l_var = l_var << 1;
  1917. }
  1918. }
  1919. return (SHORT)i;
  1920. }
  1921. LONG IsNeg(LONG x)
  1922. {
  1923. return(x & 0x80000000);
  1924. }