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.

3542 lines
110 KiB

  1. //==========================================================================;
  2. //
  3. // msadpcm.c
  4. //
  5. // Copyright (c) 1992-1994 Microsoft Corporation
  6. //
  7. // Description:
  8. //
  9. //
  10. // History:
  11. //
  12. //==========================================================================;
  13. #include <windows.h>
  14. #include <windowsx.h>
  15. #include <mmsystem.h>
  16. #include <mmreg.h>
  17. #include <msacm.h>
  18. #include <msacmdrv.h>
  19. #include "codec.h"
  20. #include "msadpcm.h"
  21. #include "debug.h"
  22. //
  23. // these are in dec386.asm for Win 16
  24. //
  25. // _gaiCoef1 dw 256, 512, 0, 192, 240, 460, 392
  26. // _gaiCoef2 dw 0, -256, 0, 64, 0, -208, -232
  27. //
  28. // _gaiP4 dw 230, 230, 230, 230, 307, 409, 512, 614
  29. // dw 768, 614, 512, 409, 307, 230, 230, 230
  30. //
  31. #ifdef WIN32
  32. const int gaiCoef1[]= {256, 512, 0, 192, 240, 460, 392};
  33. const int gaiCoef2[]= { 0, -256, 0, 64, 0, -208, -232};
  34. const int gaiP4[] = {230, 230, 230, 230, 307, 409, 512, 614,
  35. 768, 614, 512, 409, 307, 230, 230, 230};
  36. #endif
  37. #ifndef INLINE
  38. #define INLINE __inline
  39. #endif
  40. //--------------------------------------------------------------------------;
  41. //
  42. // DWORD pcmM08BytesToSamples
  43. // DWORD pcmM16BytesToSamples
  44. // DWORD pcmS08BytesToSamples
  45. // DWORD pcmS16BytesToSamples
  46. //
  47. // Description:
  48. // These functions return the number of samples in a buffer of PCM
  49. // of the specified format. For efficiency, it is declared INLINE.
  50. // Note that, depending on the optimization flags, it may not
  51. // actually be implemented as INLINE. Optimizing for speed (-Oxwt)
  52. // will generally obey the INLINE specification.
  53. //
  54. // Arguments:
  55. // DWORD cb: The length of the buffer, in bytes.
  56. //
  57. // Return (DWORD): The length of the buffer in samples.
  58. //
  59. //--------------------------------------------------------------------------;
  60. INLINE DWORD pcmM08BytesToSamples( DWORD cb )
  61. {
  62. return cb;
  63. }
  64. INLINE DWORD pcmM16BytesToSamples( DWORD cb )
  65. {
  66. return cb / ((DWORD)2);
  67. }
  68. INLINE DWORD pcmS08BytesToSamples( DWORD cb )
  69. {
  70. return cb / ((DWORD)2);
  71. }
  72. INLINE DWORD pcmS16BytesToSamples( DWORD cb )
  73. {
  74. return cb / ((DWORD)4);
  75. }
  76. //--------------------------------------------------------------------------;
  77. //
  78. // int pcmRead08
  79. // int pcmRead16
  80. // int pcmRead16Unaligned
  81. //
  82. // Description:
  83. // These functions read an 8 or 16 bit PCM value from the specified
  84. // buffer. Note that the buffers are either HUGE or UNALIGNED in all
  85. // cases. However, if a single 16-bit value crosses a segment boundary
  86. // in Win16, then pcmRead16 will wrap around; use pcmRead16Unaligned
  87. // instead.
  88. //
  89. // Arguments:
  90. // HPBYTE pb: Pointer to the input buffer.
  91. //
  92. // Return (int): The PCM value converted to 16-bit format.
  93. //
  94. //--------------------------------------------------------------------------;
  95. INLINE int pcmRead08( HPBYTE pb )
  96. {
  97. return ( (int)*pb - 128 ) << 8;
  98. }
  99. INLINE int pcmRead16( HPBYTE pb )
  100. {
  101. return (int)*(short HUGE_T *)pb;
  102. }
  103. #ifdef WIN32
  104. #define pcmRead16Unaligned pcmRead16
  105. #else
  106. INLINE int pcmRead16Unaligned( HPBYTE pb )
  107. {
  108. return (int)(short)( ((WORD)*pb) | (((WORD)*(pb+1))<<8) );
  109. }
  110. #endif
  111. //--------------------------------------------------------------------------;
  112. //
  113. // void pcmWrite08
  114. // void pcmWrite16
  115. // void pcmWrite16Unaligned
  116. //
  117. // Description:
  118. // These functions write a PCM sample (a 16-bit integer) into the
  119. // specified buffer in the appropriate format. Note that the buffers
  120. // are either HUGE or UNALIGNED. However, if a single 16-bit value is
  121. // written across a segment boundary, then pcmWrite16 will not handle
  122. // it correctly; us pcmWrite16Unaligned instead.
  123. //
  124. // Arguments:
  125. // HPBYTE pb: Pointer to the output buffer.
  126. // int iSamp: The sample.
  127. //
  128. // Return (int): The PCM value converted to 16-bit format.
  129. //
  130. //--------------------------------------------------------------------------;
  131. INLINE void pcmWrite08( HPBYTE pb, int iSamp )
  132. {
  133. *pb = (BYTE)((iSamp >> 8) + 128);
  134. }
  135. INLINE void pcmWrite16( HPBYTE pb, int iSamp )
  136. {
  137. *(short HUGE_T *)pb = (short)iSamp;
  138. }
  139. #ifdef WIN32
  140. #define pcmWrite16Unaligned pcmWrite16
  141. #else
  142. INLINE void pcmWrite16Unaligned( HPBYTE pb, int iSamp )
  143. {
  144. *pb = (BYTE)( iSamp&0x00FF );
  145. *(pb+1) = (BYTE)( iSamp>>8 );
  146. }
  147. #endif
  148. //--------------------------------------------------------------------------;
  149. //
  150. // int adpcmCalcDelta
  151. //
  152. // Description:
  153. // This function computes the next Adaptive Scale Factor (ASF) value
  154. // based on the the current ASF and the current encoded sample.
  155. //
  156. // Arguments:
  157. // int iEnc: The current encoded sample (as a signed integer).
  158. // int iDelta: The current ASF.
  159. //
  160. // Return (int): The next ASF.
  161. //
  162. //--------------------------------------------------------------------------;
  163. INLINE int adpcmCalcDelta
  164. (
  165. int iEnc,
  166. int iDelta
  167. )
  168. {
  169. int iNewDelta;
  170. iNewDelta = (int)((gaiP4[iEnc&OUTPUT4MASK] * (long)iDelta) >> PSCALE);
  171. if( iNewDelta < DELTA4MIN )
  172. iNewDelta = DELTA4MIN;
  173. return iNewDelta;
  174. }
  175. //--------------------------------------------------------------------------;
  176. //
  177. // long adpcmCalcPrediction
  178. //
  179. // Description:
  180. // This function calculates the predicted sample value based on the
  181. // previous two samples and the current coefficients.
  182. //
  183. // Arguments:
  184. // int iSamp1: The previous decoded sample.
  185. // int iCoef1: The coefficient for iSamp1.
  186. // int iSamp2: The decoded sample before iSamp1.
  187. // int iCoef2: The coefficient for iSamp2.
  188. //
  189. // Return (long): The predicted sample.
  190. //
  191. //--------------------------------------------------------------------------;
  192. INLINE long adpcmCalcPrediction
  193. (
  194. int iSamp1,
  195. int iCoef1,
  196. int iSamp2,
  197. int iCoef2
  198. )
  199. {
  200. return ((long)iSamp1 * iCoef1 + (long)iSamp2 * iCoef2) >> CSCALE;
  201. }
  202. //--------------------------------------------------------------------------;
  203. //
  204. // int adpcmDecodeSample
  205. //
  206. // Description:
  207. // This function decodes a single 4-bit encoded ADPCM sample. There
  208. // are three steps:
  209. //
  210. // 1. Sign-extend the 4-bit iInput.
  211. //
  212. // 2. predict the next sample using the previous two
  213. // samples and the predictor coefficients:
  214. //
  215. // Prediction = (iSamp1 * aiCoef1 + iSamp2 * iCoef2) / 256;
  216. //
  217. // 3. reconstruct the original PCM sample using the encoded
  218. // sample (iInput), the Adaptive Scale Factor (aiDelta)
  219. // and the prediction value computed in step 1 above.
  220. //
  221. // Sample = (iInput * iDelta) + Prediction;
  222. //
  223. // Arguments:
  224. // int iSamp1: The previous decoded sample.
  225. // int iCoef1: The coefficient for iSamp1.
  226. // int iSamp2: The decoded sample before iSamp1.
  227. // int iCoef2: The coefficient for iSamp2.
  228. // int iInput: The current encoded sample (lower 4 bits).
  229. // int iDelta: The current ASF.
  230. //
  231. // Return (int): The decoded sample.
  232. //
  233. //--------------------------------------------------------------------------;
  234. INLINE int adpcmDecodeSample
  235. (
  236. int iSamp1,
  237. int iCoef1,
  238. int iSamp2,
  239. int iCoef2,
  240. int iInput,
  241. int iDelta
  242. )
  243. {
  244. long lSamp;
  245. iInput = (int)( ((signed char)(iInput<<4)) >> 4 );
  246. lSamp = ((long)iInput * iDelta) +
  247. adpcmCalcPrediction(iSamp1,iCoef1,iSamp2,iCoef2);
  248. if (lSamp > 32767)
  249. lSamp = 32767;
  250. else if (lSamp < -32768)
  251. lSamp = -32768;
  252. return (int)lSamp;
  253. }
  254. //--------------------------------------------------------------------------;
  255. //
  256. // int adpcmEncode4Bit_FirstDelta
  257. //
  258. // Description:
  259. //
  260. //
  261. // Arguments:
  262. //
  263. //
  264. // Return (short FNLOCAL):
  265. //
  266. //
  267. // History:
  268. // 1/27/93 cjp [curtisp]
  269. //
  270. //--------------------------------------------------------------------------;
  271. INLINE int FNLOCAL adpcmEncode4Bit_FirstDelta
  272. (
  273. int iCoef1,
  274. int iCoef2,
  275. int iP5,
  276. int iP4,
  277. int iP3,
  278. int iP2,
  279. int iP1
  280. )
  281. {
  282. long lTotal;
  283. int iRtn;
  284. long lTemp;
  285. //
  286. // use average of 3 predictions
  287. //
  288. lTemp = (((long)iP5 * iCoef2) + ((long)iP4 * iCoef1)) >> CSCALE;
  289. lTotal = (lTemp > iP3) ? (lTemp - iP3) : (iP3 - lTemp);
  290. lTemp = (((long)iP4 * iCoef2) + ((long)iP3 * iCoef1)) >> CSCALE;
  291. lTotal += (lTemp > iP2) ? (lTemp - iP2) : (iP2 - lTemp);
  292. lTemp = (((long)iP3 * iCoef2) + ((long)iP2 * iCoef1)) >> CSCALE;
  293. lTotal += (lTemp > iP1) ? (lTemp - iP1) : (iP1 - lTemp);
  294. //
  295. // optimal iDelta is 1/4 of prediction error
  296. //
  297. iRtn = (int)(lTotal / 12);
  298. if (iRtn < DELTA4MIN)
  299. iRtn = DELTA4MIN;
  300. return (iRtn);
  301. } // adpcmEncode4Bit_FirstDelta()
  302. //==========================================================================;
  303. //
  304. // NON-REALTIME ENCODE ROUTINES
  305. //
  306. //==========================================================================;
  307. //--------------------------------------------------------------------------;
  308. //
  309. // DWORD adpcmEncode4Bit_M08_FullPass
  310. // DWORD adpcmEncode4Bit_M16_FullPass
  311. // DWORD adpcmEncode4Bit_S08_FullPass
  312. // DWORD adpcmEncode4Bit_S16_FullPass
  313. //
  314. // Description:
  315. // These functions encode a buffer of data from PCM to MS ADPCM in the
  316. // specified format. These functions use a fullpass algorithm which
  317. // tries each set of coefficients in order to determine which set
  318. // produces the smallest coding error. The appropriate function is
  319. // called once for each ACMDM_STREAM_CONVERT message received.
  320. //
  321. //
  322. // Arguments:
  323. //
  324. //
  325. // Return (DWORD): The number of bytes used in the destination buffer.
  326. //
  327. //--------------------------------------------------------------------------;
  328. #define ENCODE_DELTA_LOOKAHEAD 5
  329. DWORD FNGLOBAL adpcmEncode4Bit_M08_FullPass
  330. (
  331. HPBYTE pbSrc,
  332. DWORD cbSrcLength,
  333. HPBYTE pbDst,
  334. UINT nBlockAlignment,
  335. UINT cSamplesPerBlock,
  336. UINT nNumCoef,
  337. LPADPCMCOEFSET lpCoefSet
  338. )
  339. {
  340. HPBYTE pbDstStart;
  341. HPBYTE pbSrcThisBlock;
  342. DWORD cSrcSamples;
  343. UINT cBlockSamples;
  344. int aiSamples[ENCODE_DELTA_LOOKAHEAD];
  345. int aiFirstDelta[MSADPCM_MAX_COEFFICIENTS];
  346. DWORD adwTotalError[MSADPCM_MAX_COEFFICIENTS];
  347. int iCoef1;
  348. int iCoef2;
  349. int iSamp1;
  350. int iSamp2;
  351. int iDelta;
  352. int iOutput1;
  353. int iOutput2;
  354. int iBestPredictor;
  355. int iSample;
  356. long lSamp;
  357. long lError;
  358. long lPrediction;
  359. DWORD dw;
  360. UINT i,n;
  361. pbDstStart = pbDst;
  362. cSrcSamples = pcmM08BytesToSamples(cbSrcLength);
  363. //
  364. // step through each block of PCM data and encode it to 4 bit ADPCM
  365. //
  366. while( 0 != cSrcSamples )
  367. {
  368. //
  369. // determine how much data we should encode for this block--this
  370. // will be cSamplesPerBlock until we hit the last chunk of PCM
  371. // data that will not fill a complete block. so on the last block
  372. // we only encode that amount of data remaining...
  373. //
  374. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  375. cSrcSamples -= cBlockSamples;
  376. //
  377. // We need the first ENCODE_DELTA_LOOKAHEAD samples in order to
  378. // calculate the first iDelta value. Therefore we put these samples
  379. // into a more accessible array: aiSamples[]. Note: if we don't
  380. // have ENCODE_DELTA_LOOKAHEAD samples, we pretend that the samples
  381. // that we don't have are actually zeros. This is important not
  382. // only for the iDelta calculation, but also for the case where
  383. // there is only 1 sample to encode ... in this case, there is not
  384. // really enough data to complete the ADPCM block header, but since
  385. // the two delay samples for the block header will be taken from
  386. // the aiSamples[] array, iSamp1 [the second sample] will be taken
  387. // as zero and there will no problem.
  388. //
  389. pbSrcThisBlock = pbSrc;
  390. for (n = 0; n < ENCODE_DELTA_LOOKAHEAD; n++)
  391. {
  392. if( n < cBlockSamples )
  393. aiSamples[n] = pcmRead08(pbSrcThisBlock++);
  394. else
  395. aiSamples[n] = 0;
  396. }
  397. //
  398. // find the optimal predictor for each channel: to do this, we
  399. // must step through and encode using each coefficient set (one
  400. // at a time) and determine which one has the least error from
  401. // the original data. the one with the least error is then used
  402. // for the final encode (the 8th pass done below).
  403. //
  404. // NOTE: keeping the encoded data of the one that has the least
  405. // error at all times is an obvious optimization that should be
  406. // done. in this way, we only need to do 7 passes instead of 8.
  407. //
  408. for (i = 0; i < MSADPCM_MAX_COEFFICIENTS; i++)
  409. {
  410. //
  411. // Reset source pointer to the beginning of the block.
  412. //
  413. pbSrcThisBlock = pbSrc;
  414. //
  415. // Reset variables for this pass.
  416. //
  417. adwTotalError[i] = 0L;
  418. iCoef1 = lpCoefSet[i].iCoef1;
  419. iCoef2 = lpCoefSet[i].iCoef2;
  420. //
  421. // We need to choose the first iDelta--to do this, we need
  422. // to look at the first few samples.
  423. //
  424. iDelta = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  425. aiSamples[0], aiSamples[1], aiSamples[2],
  426. aiSamples[3], aiSamples[4]);
  427. aiFirstDelta[i] = iDelta;
  428. //
  429. // Set up first two samples - these have already been converted
  430. // to 16-bit values in aiSamples[], but make sure to increment
  431. // pbSrcThisBlock so that it keeps in sync.
  432. //
  433. iSamp1 = aiSamples[1];
  434. iSamp2 = aiSamples[0];
  435. pbSrcThisBlock += 2*sizeof(BYTE);
  436. //
  437. // now encode the rest of the PCM data in this block--note
  438. // we start 2 samples ahead because the first two samples are
  439. // simply copied into the ADPCM block header...
  440. //
  441. for (n = 2; n < cBlockSamples; n++)
  442. {
  443. //
  444. // calculate the prediction based on the previous two
  445. // samples
  446. //
  447. lPrediction = adpcmCalcPrediction( iSamp1, iCoef1,
  448. iSamp2, iCoef2 );
  449. //
  450. // Grab the next sample to encode.
  451. //
  452. iSample = pcmRead08(pbSrcThisBlock++);
  453. //
  454. // encode it
  455. //
  456. lError = (long)iSample - lPrediction;
  457. iOutput1 = (int)(lError / iDelta);
  458. if (iOutput1 > OUTPUT4MAX)
  459. iOutput1 = OUTPUT4MAX;
  460. else if (iOutput1 < OUTPUT4MIN)
  461. iOutput1 = OUTPUT4MIN;
  462. lSamp = lPrediction + ((long)iDelta * iOutput1);
  463. if (lSamp > 32767)
  464. lSamp = 32767;
  465. else if (lSamp < -32768)
  466. lSamp = -32768;
  467. //
  468. // compute the next iDelta
  469. //
  470. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  471. //
  472. // Save updated delay samples.
  473. //
  474. iSamp2 = iSamp1;
  475. iSamp1 = (int)lSamp;
  476. //
  477. // keep a running status on the error for the current
  478. // coefficient pair for this channel
  479. //
  480. lError = lSamp - iSample;
  481. adwTotalError[i] += (lError * lError) >> 7;
  482. }
  483. }
  484. //
  485. // WHEW! we have now made 7 passes over the data and calculated
  486. // the error for each--so it's time to find the one that produced
  487. // the lowest error and use that predictor.
  488. //
  489. iBestPredictor = 0;
  490. dw = adwTotalError[0];
  491. for (i = 1; i < MSADPCM_MAX_COEFFICIENTS; i++)
  492. {
  493. if (adwTotalError[i] < dw)
  494. {
  495. iBestPredictor = i;
  496. dw = adwTotalError[i];
  497. }
  498. }
  499. iCoef1 = lpCoefSet[iBestPredictor].iCoef1;
  500. iCoef2 = lpCoefSet[iBestPredictor].iCoef2;
  501. //
  502. // grab first iDelta from our precomputed first deltas that we
  503. // calculated above
  504. //
  505. iDelta = aiFirstDelta[iBestPredictor];
  506. //
  507. // Set up first two samples - these have already been converted
  508. // to 16-bit values in aiSamples[], but make sure to increment
  509. // pbSrc so that it keeps in sync.
  510. //
  511. iSamp1 = aiSamples[1];
  512. iSamp2 = aiSamples[0];
  513. pbSrc += 2*sizeof(BYTE);
  514. ASSERT( cBlockSamples != 1 );
  515. cBlockSamples -= 2;
  516. //
  517. // write the block header for the encoded data
  518. //
  519. // the block header is composed of the following data:
  520. // 1 byte predictor per channel
  521. // 2 byte delta per channel
  522. // 2 byte first delayed sample per channel
  523. // 2 byte second delayed sample per channel
  524. //
  525. *pbDst++ = (BYTE)iBestPredictor;
  526. pcmWrite16Unaligned(pbDst,iDelta);
  527. pbDst += sizeof(short);
  528. pcmWrite16Unaligned(pbDst,iSamp1);
  529. pbDst += sizeof(short);
  530. pcmWrite16Unaligned(pbDst,iSamp2);
  531. pbDst += sizeof(short);
  532. //
  533. // We have written the header for this block--now write the data
  534. // chunk (which consists of a bunch of encoded nibbles).
  535. //
  536. while( cBlockSamples>0 )
  537. {
  538. //
  539. // Sample 1.
  540. //
  541. iSample = pcmRead08(pbSrc++);
  542. cBlockSamples--;
  543. //
  544. // calculate the prediction based on the previous two samples
  545. //
  546. lPrediction = adpcmCalcPrediction(iSamp1,iCoef1,iSamp2,iCoef2);
  547. //
  548. // encode the sample
  549. //
  550. lError = (long)iSample - lPrediction;
  551. iOutput1 = (int)(lError / iDelta);
  552. if (iOutput1 > OUTPUT4MAX)
  553. iOutput1 = OUTPUT4MAX;
  554. else if (iOutput1 < OUTPUT4MIN)
  555. iOutput1 = OUTPUT4MIN;
  556. lSamp = lPrediction + ((long)iDelta * iOutput1);
  557. if (lSamp > 32767)
  558. lSamp = 32767;
  559. else if (lSamp < -32768)
  560. lSamp = -32768;
  561. //
  562. // compute the next iDelta
  563. //
  564. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  565. //
  566. // Save updated delay samples.
  567. //
  568. iSamp2 = iSamp1;
  569. iSamp1 = (int)lSamp;
  570. //
  571. // Sample 2.
  572. //
  573. if( cBlockSamples>0 ) {
  574. iSample = pcmRead08(pbSrc++);
  575. cBlockSamples--;
  576. //
  577. // calculate the prediction based on the previous two samples
  578. //
  579. lPrediction = adpcmCalcPrediction(iSamp1,iCoef1,iSamp2,iCoef2);
  580. //
  581. // encode the sample
  582. //
  583. lError = (long)iSample - lPrediction;
  584. iOutput2 = (int)(lError / iDelta);
  585. if (iOutput2 > OUTPUT4MAX)
  586. iOutput2 = OUTPUT4MAX;
  587. else if (iOutput2 < OUTPUT4MIN)
  588. iOutput2 = OUTPUT4MIN;
  589. lSamp = lPrediction + ((long)iDelta * iOutput2);
  590. if (lSamp > 32767)
  591. lSamp = 32767;
  592. else if (lSamp < -32768)
  593. lSamp = -32768;
  594. //
  595. // compute the next iDelta
  596. //
  597. iDelta = adpcmCalcDelta(iOutput2,iDelta);
  598. //
  599. // Save updated delay samples.
  600. //
  601. iSamp2 = iSamp1;
  602. iSamp1 = (int)lSamp;
  603. } else {
  604. iOutput2 = 0;
  605. }
  606. //
  607. // Write out the encoded byte.
  608. //
  609. *pbDst++ = (BYTE)( ((iOutput1&OUTPUT4MASK)<<4) |
  610. (iOutput2&OUTPUT4MASK) );
  611. }
  612. }
  613. //
  614. // We return the number of bytes used in the destination. This is
  615. // simply the difference in bytes from where we started.
  616. //
  617. return (DWORD)(pbDst - pbDstStart);
  618. } // adpcmEncode4Bit_M08_FullPass()
  619. //--------------------------------------------------------------------------;
  620. //--------------------------------------------------------------------------;
  621. DWORD FNGLOBAL adpcmEncode4Bit_M16_FullPass
  622. (
  623. HPBYTE pbSrc,
  624. DWORD cbSrcLength,
  625. HPBYTE pbDst,
  626. UINT nBlockAlignment,
  627. UINT cSamplesPerBlock,
  628. UINT nNumCoef,
  629. LPADPCMCOEFSET lpCoefSet
  630. )
  631. {
  632. HPBYTE pbDstStart;
  633. HPBYTE pbSrcThisBlock;
  634. DWORD cSrcSamples;
  635. UINT cBlockSamples;
  636. int aiSamples[ENCODE_DELTA_LOOKAHEAD];
  637. int aiFirstDelta[MSADPCM_MAX_COEFFICIENTS];
  638. DWORD adwTotalError[MSADPCM_MAX_COEFFICIENTS];
  639. int iCoef1;
  640. int iCoef2;
  641. int iSamp1;
  642. int iSamp2;
  643. int iDelta;
  644. int iOutput1;
  645. int iOutput2;
  646. int iBestPredictor;
  647. int iSample;
  648. long lSamp;
  649. long lError;
  650. long lPrediction;
  651. DWORD dw;
  652. UINT i,n;
  653. pbDstStart = pbDst;
  654. cSrcSamples = pcmM16BytesToSamples(cbSrcLength);
  655. //
  656. // step through each block of PCM data and encode it to 4 bit ADPCM
  657. //
  658. while( 0 != cSrcSamples )
  659. {
  660. //
  661. // determine how much data we should encode for this block--this
  662. // will be cSamplesPerBlock until we hit the last chunk of PCM
  663. // data that will not fill a complete block. so on the last block
  664. // we only encode that amount of data remaining...
  665. //
  666. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  667. cSrcSamples -= cBlockSamples;
  668. //
  669. // We need the first ENCODE_DELTA_LOOKAHEAD samples in order to
  670. // calculate the first iDelta value. Therefore we put these samples
  671. // into a more accessible array: aiSamples[]. Note: if we don't
  672. // have ENCODE_DELTA_LOOKAHEAD samples, we pretend that the samples
  673. // that we don't have are actually zeros. This is important not
  674. // only for the iDelta calculation, but also for the case where
  675. // there is only 1 sample to encode ... in this case, there is not
  676. // really enough data to complete the ADPCM block header, but since
  677. // the two delay samples for the block header will be taken from
  678. // the aiSamples[] array, iSamp1 [the second sample] will be taken
  679. // as zero and there will no problem.
  680. //
  681. pbSrcThisBlock = pbSrc;
  682. for (n = 0; n < ENCODE_DELTA_LOOKAHEAD; n++)
  683. {
  684. if( n < cBlockSamples )
  685. {
  686. aiSamples[n] = pcmRead16(pbSrcThisBlock);
  687. pbSrcThisBlock += sizeof(short);
  688. }
  689. else
  690. aiSamples[n] = 0;
  691. }
  692. //
  693. // find the optimal predictor for each channel: to do this, we
  694. // must step through and encode using each coefficient set (one
  695. // at a time) and determine which one has the least error from
  696. // the original data. the one with the least error is then used
  697. // for the final encode (the 8th pass done below).
  698. //
  699. // NOTE: keeping the encoded data of the one that has the least
  700. // error at all times is an obvious optimization that should be
  701. // done. in this way, we only need to do 7 passes instead of 8.
  702. //
  703. for (i = 0; i < MSADPCM_MAX_COEFFICIENTS; i++)
  704. {
  705. //
  706. // Reset source pointer to the beginning of the block.
  707. //
  708. pbSrcThisBlock = pbSrc;
  709. //
  710. // Reset variables for this pass.
  711. //
  712. adwTotalError[i] = 0L;
  713. iCoef1 = lpCoefSet[i].iCoef1;
  714. iCoef2 = lpCoefSet[i].iCoef2;
  715. //
  716. // We need to choose the first iDelta--to do this, we need
  717. // to look at the first few samples.
  718. //
  719. iDelta = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  720. aiSamples[0], aiSamples[1], aiSamples[2],
  721. aiSamples[3], aiSamples[4]);
  722. aiFirstDelta[i] = iDelta;
  723. //
  724. // Set up first two samples - these have already been converted
  725. // to 16-bit values in aiSamples[], but make sure to increment
  726. // pbSrcThisBlock so that it keeps in sync.
  727. //
  728. iSamp1 = aiSamples[1];
  729. iSamp2 = aiSamples[0];
  730. pbSrcThisBlock += 2*sizeof(short);
  731. //
  732. // now encode the rest of the PCM data in this block--note
  733. // we start 2 samples ahead because the first two samples are
  734. // simply copied into the ADPCM block header...
  735. //
  736. for (n = 2; n < cBlockSamples; n++)
  737. {
  738. //
  739. // calculate the prediction based on the previous two
  740. // samples
  741. //
  742. lPrediction = adpcmCalcPrediction( iSamp1, iCoef1,
  743. iSamp2, iCoef2 );
  744. //
  745. // Grab the next sample to encode.
  746. //
  747. iSample = pcmRead16(pbSrcThisBlock);
  748. pbSrcThisBlock += sizeof(short);
  749. //
  750. // encode it
  751. //
  752. lError = (long)iSample - lPrediction;
  753. iOutput1 = (int)(lError / iDelta);
  754. if (iOutput1 > OUTPUT4MAX)
  755. iOutput1 = OUTPUT4MAX;
  756. else if (iOutput1 < OUTPUT4MIN)
  757. iOutput1 = OUTPUT4MIN;
  758. lSamp = lPrediction + ((long)iDelta * iOutput1);
  759. if (lSamp > 32767)
  760. lSamp = 32767;
  761. else if (lSamp < -32768)
  762. lSamp = -32768;
  763. //
  764. // compute the next iDelta
  765. //
  766. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  767. //
  768. // Save updated delay samples.
  769. //
  770. iSamp2 = iSamp1;
  771. iSamp1 = (int)lSamp;
  772. //
  773. // keep a running status on the error for the current
  774. // coefficient pair for this channel
  775. //
  776. lError = lSamp - iSample;
  777. adwTotalError[i] += (lError * lError) >> 7;
  778. }
  779. }
  780. //
  781. // WHEW! we have now made 7 passes over the data and calculated
  782. // the error for each--so it's time to find the one that produced
  783. // the lowest error and use that predictor.
  784. //
  785. iBestPredictor = 0;
  786. dw = adwTotalError[0];
  787. for (i = 1; i < MSADPCM_MAX_COEFFICIENTS; i++)
  788. {
  789. if (adwTotalError[i] < dw)
  790. {
  791. iBestPredictor = i;
  792. dw = adwTotalError[i];
  793. }
  794. }
  795. iCoef1 = lpCoefSet[iBestPredictor].iCoef1;
  796. iCoef2 = lpCoefSet[iBestPredictor].iCoef2;
  797. //
  798. // grab first iDelta from our precomputed first deltas that we
  799. // calculated above
  800. //
  801. iDelta = aiFirstDelta[iBestPredictor];
  802. //
  803. // Set up first two samples - these have already been converted
  804. // to 16-bit values in aiSamples[], but make sure to increment
  805. // pbSrc so that it keeps in sync.
  806. //
  807. iSamp1 = aiSamples[1];
  808. iSamp2 = aiSamples[0];
  809. pbSrc += 2*sizeof(short);
  810. ASSERT( cBlockSamples != 1 );
  811. cBlockSamples -= 2;
  812. //
  813. // write the block header for the encoded data
  814. //
  815. // the block header is composed of the following data:
  816. // 1 byte predictor per channel
  817. // 2 byte delta per channel
  818. // 2 byte first delayed sample per channel
  819. // 2 byte second delayed sample per channel
  820. //
  821. *pbDst++ = (BYTE)iBestPredictor;
  822. pcmWrite16Unaligned(pbDst,iDelta);
  823. pbDst += sizeof(short);
  824. pcmWrite16Unaligned(pbDst,iSamp1);
  825. pbDst += sizeof(short);
  826. pcmWrite16Unaligned(pbDst,iSamp2);
  827. pbDst += sizeof(short);
  828. //
  829. // We have written the header for this block--now write the data
  830. // chunk (which consists of a bunch of encoded nibbles).
  831. //
  832. while( cBlockSamples>0 )
  833. {
  834. //
  835. // Sample 1.
  836. //
  837. iSample = pcmRead16(pbSrc);
  838. pbSrc += sizeof(short);
  839. cBlockSamples--;
  840. //
  841. // calculate the prediction based on the previous two samples
  842. //
  843. lPrediction = adpcmCalcPrediction(iSamp1,iCoef1,iSamp2,iCoef2);
  844. //
  845. // encode the sample
  846. //
  847. lError = (long)iSample - lPrediction;
  848. iOutput1 = (int)(lError / iDelta);
  849. if (iOutput1 > OUTPUT4MAX)
  850. iOutput1 = OUTPUT4MAX;
  851. else if (iOutput1 < OUTPUT4MIN)
  852. iOutput1 = OUTPUT4MIN;
  853. lSamp = lPrediction + ((long)iDelta * iOutput1);
  854. if (lSamp > 32767)
  855. lSamp = 32767;
  856. else if (lSamp < -32768)
  857. lSamp = -32768;
  858. //
  859. // compute the next iDelta
  860. //
  861. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  862. //
  863. // Save updated delay samples.
  864. //
  865. iSamp2 = iSamp1;
  866. iSamp1 = (int)lSamp;
  867. //
  868. // Sample 2.
  869. //
  870. if( cBlockSamples>0 ) {
  871. iSample = pcmRead16(pbSrc);
  872. pbSrc += sizeof(short);
  873. cBlockSamples--;
  874. //
  875. // calculate the prediction based on the previous two samples
  876. //
  877. lPrediction = adpcmCalcPrediction(iSamp1,iCoef1,iSamp2,iCoef2);
  878. //
  879. // encode the sample
  880. //
  881. lError = (long)iSample - lPrediction;
  882. iOutput2 = (int)(lError / iDelta);
  883. if (iOutput2 > OUTPUT4MAX)
  884. iOutput2 = OUTPUT4MAX;
  885. else if (iOutput2 < OUTPUT4MIN)
  886. iOutput2 = OUTPUT4MIN;
  887. lSamp = lPrediction + ((long)iDelta * iOutput2);
  888. if (lSamp > 32767)
  889. lSamp = 32767;
  890. else if (lSamp < -32768)
  891. lSamp = -32768;
  892. //
  893. // compute the next iDelta
  894. //
  895. iDelta = adpcmCalcDelta(iOutput2,iDelta);
  896. //
  897. // Save updated delay samples.
  898. //
  899. iSamp2 = iSamp1;
  900. iSamp1 = (int)lSamp;
  901. } else {
  902. iOutput2 = 0;
  903. }
  904. //
  905. // Write out the encoded byte.
  906. //
  907. *pbDst++ = (BYTE)( ((iOutput1&OUTPUT4MASK)<<4) |
  908. (iOutput2&OUTPUT4MASK) );
  909. }
  910. }
  911. //
  912. // We return the number of bytes used in the destination. This is
  913. // simply the difference in bytes from where we started.
  914. //
  915. return (DWORD)(pbDst - pbDstStart);
  916. } // adpcmEncode4Bit_M16_FullPass()
  917. //--------------------------------------------------------------------------;
  918. //--------------------------------------------------------------------------;
  919. DWORD FNGLOBAL adpcmEncode4Bit_S08_FullPass
  920. (
  921. HPBYTE pbSrc,
  922. DWORD cbSrcLength,
  923. HPBYTE pbDst,
  924. UINT nBlockAlignment,
  925. UINT cSamplesPerBlock,
  926. UINT nNumCoef,
  927. LPADPCMCOEFSET lpCoefSet
  928. )
  929. {
  930. HPBYTE pbDstStart;
  931. HPBYTE pbSrcThisBlock;
  932. DWORD cSrcSamples;
  933. UINT cBlockSamples;
  934. int aiSamplesL[ENCODE_DELTA_LOOKAHEAD];
  935. int aiSamplesR[ENCODE_DELTA_LOOKAHEAD];
  936. int aiFirstDeltaL[MSADPCM_MAX_COEFFICIENTS];
  937. int aiFirstDeltaR[MSADPCM_MAX_COEFFICIENTS];
  938. DWORD adwTotalErrorL[MSADPCM_MAX_COEFFICIENTS];
  939. DWORD adwTotalErrorR[MSADPCM_MAX_COEFFICIENTS];
  940. int iCoef1;
  941. int iCoef2;
  942. int iCoef1L;
  943. int iCoef2L;
  944. int iSamp1L;
  945. int iSamp2L;
  946. int iDeltaL;
  947. int iOutputL;
  948. int iBestPredictorL;
  949. int iCoef1R;
  950. int iCoef2R;
  951. int iSamp1R;
  952. int iSamp2R;
  953. int iDeltaR;
  954. int iOutputR;
  955. int iBestPredictorR;
  956. int iSample;
  957. long lSamp;
  958. long lError;
  959. long lPrediction;
  960. DWORD dwL, dwR;
  961. UINT i,n;
  962. pbDstStart = pbDst;
  963. cSrcSamples = pcmS08BytesToSamples(cbSrcLength);
  964. //
  965. // step through each block of PCM data and encode it to 4 bit ADPCM
  966. //
  967. while( 0 != cSrcSamples )
  968. {
  969. //
  970. // determine how much data we should encode for this block--this
  971. // will be cSamplesPerBlock until we hit the last chunk of PCM
  972. // data that will not fill a complete block. so on the last block
  973. // we only encode that amount of data remaining...
  974. //
  975. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  976. cSrcSamples -= cBlockSamples;
  977. //
  978. // We need the first ENCODE_DELTA_LOOKAHEAD samples in order to
  979. // calculate the first iDelta value. Therefore we put these samples
  980. // into a more accessible array: aiSamples[]. Note: if we don't
  981. // have ENCODE_DELTA_LOOKAHEAD samples, we pretend that the samples
  982. // that we don't have are actually zeros. This is important not
  983. // only for the iDelta calculation, but also for the case where
  984. // there is only 1 sample to encode ... in this case, there is not
  985. // really enough data to complete the ADPCM block header, but since
  986. // the two delay samples for the block header will be taken from
  987. // the aiSamples[] array, iSamp1 [the second sample] will be taken
  988. // as zero and there will no problem.
  989. //
  990. pbSrcThisBlock = pbSrc;
  991. for (n = 0; n < ENCODE_DELTA_LOOKAHEAD; n++)
  992. {
  993. if( n < cBlockSamples )
  994. {
  995. aiSamplesL[n] = pcmRead08(pbSrcThisBlock++);
  996. aiSamplesR[n] = pcmRead08(pbSrcThisBlock++);
  997. }
  998. else
  999. {
  1000. aiSamplesL[n] = 0;
  1001. aiSamplesR[n] = 0;
  1002. }
  1003. }
  1004. //
  1005. // find the optimal predictor for each channel: to do this, we
  1006. // must step through and encode using each coefficient set (one
  1007. // at a time) and determine which one has the least error from
  1008. // the original data. the one with the least error is then used
  1009. // for the final encode (the 8th pass done below).
  1010. //
  1011. // NOTE: keeping the encoded data of the one that has the least
  1012. // error at all times is an obvious optimization that should be
  1013. // done. in this way, we only need to do 7 passes instead of 8.
  1014. //
  1015. for (i = 0; i < MSADPCM_MAX_COEFFICIENTS; i++)
  1016. {
  1017. //
  1018. // Reset source pointer to the beginning of the block.
  1019. //
  1020. pbSrcThisBlock = pbSrc;
  1021. //
  1022. // Reset variables for this pass (coefs are the same for L, R).
  1023. //
  1024. adwTotalErrorL[i] = 0L;
  1025. adwTotalErrorR[i] = 0L;
  1026. iCoef1 = lpCoefSet[i].iCoef1;
  1027. iCoef2 = lpCoefSet[i].iCoef2;
  1028. //
  1029. // We need to choose the first iDelta--to do this, we need
  1030. // to look at the first few samples.
  1031. //
  1032. iDeltaL = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  1033. aiSamplesL[0], aiSamplesL[1], aiSamplesL[2],
  1034. aiSamplesL[3], aiSamplesL[4]);
  1035. iDeltaR = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  1036. aiSamplesR[0], aiSamplesR[1], aiSamplesR[2],
  1037. aiSamplesR[3], aiSamplesR[4]);
  1038. aiFirstDeltaL[i] = iDeltaL;
  1039. aiFirstDeltaR[i] = iDeltaR;
  1040. //
  1041. // Set up first two samples - these have already been converted
  1042. // to 16-bit values in aiSamples[], but make sure to increment
  1043. // pbSrcThisBlock so that it keeps in sync.
  1044. //
  1045. iSamp1L = aiSamplesL[1];
  1046. iSamp1R = aiSamplesR[1];
  1047. iSamp2L = aiSamplesL[0];
  1048. iSamp2R = aiSamplesR[0];
  1049. pbSrcThisBlock += 2*sizeof(BYTE) * 2; // Last 2 = # of channels.
  1050. //
  1051. // now encode the rest of the PCM data in this block--note
  1052. // we start 2 samples ahead because the first two samples are
  1053. // simply copied into the ADPCM block header...
  1054. //
  1055. for (n = 2; n < cBlockSamples; n++)
  1056. {
  1057. //
  1058. // LEFT channel.
  1059. //
  1060. //
  1061. // calculate the prediction based on the previous two
  1062. // samples
  1063. //
  1064. lPrediction = adpcmCalcPrediction( iSamp1L, iCoef1,
  1065. iSamp2L, iCoef2 );
  1066. //
  1067. // Grab the next sample to encode.
  1068. //
  1069. iSample = pcmRead08(pbSrcThisBlock++);
  1070. //
  1071. // encode it
  1072. //
  1073. lError = (long)iSample - lPrediction;
  1074. iOutputL = (int)(lError / iDeltaL);
  1075. if (iOutputL > OUTPUT4MAX)
  1076. iOutputL = OUTPUT4MAX;
  1077. else if (iOutputL < OUTPUT4MIN)
  1078. iOutputL = OUTPUT4MIN;
  1079. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  1080. if (lSamp > 32767)
  1081. lSamp = 32767;
  1082. else if (lSamp < -32768)
  1083. lSamp = -32768;
  1084. //
  1085. // compute the next iDelta
  1086. //
  1087. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  1088. //
  1089. // Save updated delay samples.
  1090. //
  1091. iSamp2L = iSamp1L;
  1092. iSamp1L = (int)lSamp;
  1093. //
  1094. // keep a running status on the error for the current
  1095. // coefficient pair for this channel
  1096. //
  1097. lError = lSamp - iSample;
  1098. adwTotalErrorL[i] += (lError * lError) >> 7;
  1099. //
  1100. // RIGHT channel.
  1101. //
  1102. //
  1103. // calculate the prediction based on the previous two
  1104. // samples
  1105. //
  1106. lPrediction = adpcmCalcPrediction( iSamp1R, iCoef1,
  1107. iSamp2R, iCoef2 );
  1108. //
  1109. // Grab the next sample to encode.
  1110. //
  1111. iSample = pcmRead08(pbSrcThisBlock++);
  1112. //
  1113. // encode it
  1114. //
  1115. lError = (long)iSample - lPrediction;
  1116. iOutputR = (int)(lError / iDeltaR);
  1117. if (iOutputR > OUTPUT4MAX)
  1118. iOutputR = OUTPUT4MAX;
  1119. else if (iOutputR < OUTPUT4MIN)
  1120. iOutputR = OUTPUT4MIN;
  1121. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  1122. if (lSamp > 32767)
  1123. lSamp = 32767;
  1124. else if (lSamp < -32768)
  1125. lSamp = -32768;
  1126. //
  1127. // compute the next iDelta
  1128. //
  1129. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  1130. //
  1131. // Save updated delay samples.
  1132. //
  1133. iSamp2R = iSamp1R;
  1134. iSamp1R = (int)lSamp;
  1135. //
  1136. // keep a running status on the error for the current
  1137. // coefficient pair for this channel
  1138. //
  1139. lError = lSamp - iSample;
  1140. adwTotalErrorR[i] += (lError * lError) >> 7;
  1141. }
  1142. }
  1143. //
  1144. // WHEW! we have now made 7 passes over the data and calculated
  1145. // the error for each--so it's time to find the one that produced
  1146. // the lowest error and use that predictor.
  1147. //
  1148. iBestPredictorL = 0;
  1149. iBestPredictorR = 0;
  1150. dwL = adwTotalErrorL[0];
  1151. dwR = adwTotalErrorR[0];
  1152. for (i = 1; i < MSADPCM_MAX_COEFFICIENTS; i++)
  1153. {
  1154. if (adwTotalErrorL[i] < dwL)
  1155. {
  1156. iBestPredictorL = i;
  1157. dwL = adwTotalErrorL[i];
  1158. }
  1159. if (adwTotalErrorR[i] < dwR)
  1160. {
  1161. iBestPredictorR = i;
  1162. dwR = adwTotalErrorR[i];
  1163. }
  1164. }
  1165. iCoef1L = lpCoefSet[iBestPredictorL].iCoef1;
  1166. iCoef1R = lpCoefSet[iBestPredictorR].iCoef1;
  1167. iCoef2L = lpCoefSet[iBestPredictorL].iCoef2;
  1168. iCoef2R = lpCoefSet[iBestPredictorR].iCoef2;
  1169. //
  1170. // grab first iDelta from our precomputed first deltas that we
  1171. // calculated above
  1172. //
  1173. iDeltaL = aiFirstDeltaL[iBestPredictorL];
  1174. iDeltaR = aiFirstDeltaR[iBestPredictorR];
  1175. //
  1176. // Set up first two samples - these have already been converted
  1177. // to 16-bit values in aiSamples[], but make sure to increment
  1178. // pbSrc so that it keeps in sync.
  1179. //
  1180. iSamp1L = aiSamplesL[1];
  1181. iSamp1R = aiSamplesR[1];
  1182. iSamp2L = aiSamplesL[0];
  1183. iSamp2R = aiSamplesR[0];
  1184. pbSrc += 2*sizeof(BYTE) * 2; // Last 2 = # of channels.
  1185. ASSERT( cBlockSamples != 1 );
  1186. cBlockSamples -= 2;
  1187. //
  1188. // write the block header for the encoded data
  1189. //
  1190. // the block header is composed of the following data:
  1191. // 1 byte predictor per channel
  1192. // 2 byte delta per channel
  1193. // 2 byte first delayed sample per channel
  1194. // 2 byte second delayed sample per channel
  1195. //
  1196. *pbDst++ = (BYTE)iBestPredictorL;
  1197. *pbDst++ = (BYTE)iBestPredictorR;
  1198. pcmWrite16Unaligned(pbDst,iDeltaL);
  1199. pbDst += sizeof(short);
  1200. pcmWrite16Unaligned(pbDst,iDeltaR);
  1201. pbDst += sizeof(short);
  1202. pcmWrite16Unaligned(pbDst,iSamp1L);
  1203. pbDst += sizeof(short);
  1204. pcmWrite16Unaligned(pbDst,iSamp1R);
  1205. pbDst += sizeof(short);
  1206. pcmWrite16Unaligned(pbDst,iSamp2L);
  1207. pbDst += sizeof(short);
  1208. pcmWrite16Unaligned(pbDst,iSamp2R);
  1209. pbDst += sizeof(short);
  1210. //
  1211. // We have written the header for this block--now write the data
  1212. // chunk (which consists of a bunch of encoded nibbles).
  1213. //
  1214. while( cBlockSamples-- )
  1215. {
  1216. //
  1217. // LEFT channel.
  1218. //
  1219. iSample = pcmRead08(pbSrc++);
  1220. //
  1221. // calculate the prediction based on the previous two samples
  1222. //
  1223. lPrediction = adpcmCalcPrediction(iSamp1L,iCoef1L,iSamp2L,iCoef2L);
  1224. //
  1225. // encode the sample
  1226. //
  1227. lError = (long)iSample - lPrediction;
  1228. iOutputL = (int)(lError / iDeltaL);
  1229. if (iOutputL > OUTPUT4MAX)
  1230. iOutputL = OUTPUT4MAX;
  1231. else if (iOutputL < OUTPUT4MIN)
  1232. iOutputL = OUTPUT4MIN;
  1233. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  1234. if (lSamp > 32767)
  1235. lSamp = 32767;
  1236. else if (lSamp < -32768)
  1237. lSamp = -32768;
  1238. //
  1239. // compute the next iDelta
  1240. //
  1241. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  1242. //
  1243. // Save updated delay samples.
  1244. //
  1245. iSamp2L = iSamp1L;
  1246. iSamp1L = (int)lSamp;
  1247. //
  1248. // RIGHT channel.
  1249. //
  1250. iSample = pcmRead08(pbSrc++);
  1251. //
  1252. // calculate the prediction based on the previous two samples
  1253. //
  1254. lPrediction = adpcmCalcPrediction(iSamp1R,iCoef1R,iSamp2R,iCoef2R);
  1255. //
  1256. // encode the sample
  1257. //
  1258. lError = (long)iSample - lPrediction;
  1259. iOutputR = (int)(lError / iDeltaR);
  1260. if (iOutputR > OUTPUT4MAX)
  1261. iOutputR = OUTPUT4MAX;
  1262. else if (iOutputR < OUTPUT4MIN)
  1263. iOutputR = OUTPUT4MIN;
  1264. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  1265. if (lSamp > 32767)
  1266. lSamp = 32767;
  1267. else if (lSamp < -32768)
  1268. lSamp = -32768;
  1269. //
  1270. // compute the next iDelta
  1271. //
  1272. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  1273. //
  1274. // Save updated delay samples.
  1275. //
  1276. iSamp2R = iSamp1R;
  1277. iSamp1R = (int)lSamp;
  1278. //
  1279. // Write out the encoded byte.
  1280. //
  1281. *pbDst++ = (BYTE)( ((iOutputL&OUTPUT4MASK)<<4) |
  1282. (iOutputR&OUTPUT4MASK) );
  1283. }
  1284. }
  1285. //
  1286. // We return the number of bytes used in the destination. This is
  1287. // simply the difference in bytes from where we started.
  1288. //
  1289. return (DWORD)(pbDst - pbDstStart);
  1290. } // adpcmEncode4Bit_S08_FullPass()
  1291. //--------------------------------------------------------------------------;
  1292. //--------------------------------------------------------------------------;
  1293. DWORD FNGLOBAL adpcmEncode4Bit_S16_FullPass
  1294. (
  1295. HPBYTE pbSrc,
  1296. DWORD cbSrcLength,
  1297. HPBYTE pbDst,
  1298. UINT nBlockAlignment,
  1299. UINT cSamplesPerBlock,
  1300. UINT nNumCoef,
  1301. LPADPCMCOEFSET lpCoefSet
  1302. )
  1303. {
  1304. HPBYTE pbDstStart;
  1305. HPBYTE pbSrcThisBlock;
  1306. DWORD cSrcSamples;
  1307. UINT cBlockSamples;
  1308. int aiSamplesL[ENCODE_DELTA_LOOKAHEAD];
  1309. int aiSamplesR[ENCODE_DELTA_LOOKAHEAD];
  1310. int aiFirstDeltaL[MSADPCM_MAX_COEFFICIENTS];
  1311. int aiFirstDeltaR[MSADPCM_MAX_COEFFICIENTS];
  1312. DWORD adwTotalErrorL[MSADPCM_MAX_COEFFICIENTS];
  1313. DWORD adwTotalErrorR[MSADPCM_MAX_COEFFICIENTS];
  1314. int iCoef1;
  1315. int iCoef2;
  1316. int iCoef1L;
  1317. int iCoef2L;
  1318. int iSamp1L;
  1319. int iSamp2L;
  1320. int iDeltaL;
  1321. int iOutputL;
  1322. int iBestPredictorL;
  1323. int iCoef1R;
  1324. int iCoef2R;
  1325. int iSamp1R;
  1326. int iSamp2R;
  1327. int iDeltaR;
  1328. int iOutputR;
  1329. int iBestPredictorR;
  1330. int iSample;
  1331. long lSamp;
  1332. long lError;
  1333. long lPrediction;
  1334. DWORD dwL, dwR;
  1335. UINT i,n;
  1336. pbDstStart = pbDst;
  1337. cSrcSamples = pcmS16BytesToSamples(cbSrcLength);
  1338. //
  1339. // step through each block of PCM data and encode it to 4 bit ADPCM
  1340. //
  1341. while( 0 != cSrcSamples )
  1342. {
  1343. //
  1344. // determine how much data we should encode for this block--this
  1345. // will be cSamplesPerBlock until we hit the last chunk of PCM
  1346. // data that will not fill a complete block. so on the last block
  1347. // we only encode that amount of data remaining...
  1348. //
  1349. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  1350. cSrcSamples -= cBlockSamples;
  1351. //
  1352. // We need the first ENCODE_DELTA_LOOKAHEAD samples in order to
  1353. // calculate the first iDelta value. Therefore we put these samples
  1354. // into a more accessible array: aiSamples[]. Note: if we don't
  1355. // have ENCODE_DELTA_LOOKAHEAD samples, we pretend that the samples
  1356. // that we don't have are actually zeros. This is important not
  1357. // only for the iDelta calculation, but also for the case where
  1358. // there is only 1 sample to encode ... in this case, there is not
  1359. // really enough data to complete the ADPCM block header, but since
  1360. // the two delay samples for the block header will be taken from
  1361. // the aiSamples[] array, iSamp1 [the second sample] will be taken
  1362. // as zero and there will no problem.
  1363. //
  1364. pbSrcThisBlock = pbSrc;
  1365. for (n = 0; n < ENCODE_DELTA_LOOKAHEAD; n++)
  1366. {
  1367. if( n < cBlockSamples )
  1368. {
  1369. aiSamplesL[n] = pcmRead16(pbSrcThisBlock);
  1370. pbSrcThisBlock += sizeof(short);
  1371. aiSamplesR[n] = pcmRead16(pbSrcThisBlock);
  1372. pbSrcThisBlock += sizeof(short);
  1373. }
  1374. else
  1375. {
  1376. aiSamplesL[n] = 0;
  1377. aiSamplesR[n] = 0;
  1378. }
  1379. }
  1380. //
  1381. // find the optimal predictor for each channel: to do this, we
  1382. // must step through and encode using each coefficient set (one
  1383. // at a time) and determine which one has the least error from
  1384. // the original data. the one with the least error is then used
  1385. // for the final encode (the 8th pass done below).
  1386. //
  1387. // NOTE: keeping the encoded data of the one that has the least
  1388. // error at all times is an obvious optimization that should be
  1389. // done. in this way, we only need to do 7 passes instead of 8.
  1390. //
  1391. for (i = 0; i < MSADPCM_MAX_COEFFICIENTS; i++)
  1392. {
  1393. //
  1394. // Reset source pointer to the beginning of the block.
  1395. //
  1396. pbSrcThisBlock = pbSrc;
  1397. //
  1398. // Reset variables for this pass (coefs are the same for L, R).
  1399. //
  1400. adwTotalErrorL[i] = 0L;
  1401. adwTotalErrorR[i] = 0L;
  1402. iCoef1 = lpCoefSet[i].iCoef1;
  1403. iCoef2 = lpCoefSet[i].iCoef2;
  1404. //
  1405. // We need to choose the first iDelta--to do this, we need
  1406. // to look at the first few samples.
  1407. //
  1408. iDeltaL = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  1409. aiSamplesL[0], aiSamplesL[1], aiSamplesL[2],
  1410. aiSamplesL[3], aiSamplesL[4]);
  1411. iDeltaR = adpcmEncode4Bit_FirstDelta(iCoef1, iCoef2,
  1412. aiSamplesR[0], aiSamplesR[1], aiSamplesR[2],
  1413. aiSamplesR[3], aiSamplesR[4]);
  1414. aiFirstDeltaL[i] = iDeltaL;
  1415. aiFirstDeltaR[i] = iDeltaR;
  1416. //
  1417. // Set up first two samples - these have already been converted
  1418. // to 16-bit values in aiSamples[], but make sure to increment
  1419. // pbSrcThisBlock so that it keeps in sync.
  1420. //
  1421. iSamp1L = aiSamplesL[1];
  1422. iSamp1R = aiSamplesR[1];
  1423. iSamp2L = aiSamplesL[0];
  1424. iSamp2R = aiSamplesR[0];
  1425. pbSrcThisBlock += 2*sizeof(short) * 2; // Last 2 = # of channels.
  1426. //
  1427. // now encode the rest of the PCM data in this block--note
  1428. // we start 2 samples ahead because the first two samples are
  1429. // simply copied into the ADPCM block header...
  1430. //
  1431. for (n = 2; n < cBlockSamples; n++)
  1432. {
  1433. //
  1434. // LEFT channel.
  1435. //
  1436. //
  1437. // calculate the prediction based on the previous two
  1438. // samples
  1439. //
  1440. lPrediction = adpcmCalcPrediction( iSamp1L, iCoef1,
  1441. iSamp2L, iCoef2 );
  1442. //
  1443. // Grab the next sample to encode.
  1444. //
  1445. iSample = pcmRead16(pbSrcThisBlock);
  1446. pbSrcThisBlock += sizeof(short);
  1447. //
  1448. // encode it
  1449. //
  1450. lError = (long)iSample - lPrediction;
  1451. iOutputL = (int)(lError / iDeltaL);
  1452. if (iOutputL > OUTPUT4MAX)
  1453. iOutputL = OUTPUT4MAX;
  1454. else if (iOutputL < OUTPUT4MIN)
  1455. iOutputL = OUTPUT4MIN;
  1456. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  1457. if (lSamp > 32767)
  1458. lSamp = 32767;
  1459. else if (lSamp < -32768)
  1460. lSamp = -32768;
  1461. //
  1462. // compute the next iDelta
  1463. //
  1464. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  1465. //
  1466. // Save updated delay samples.
  1467. //
  1468. iSamp2L = iSamp1L;
  1469. iSamp1L = (int)lSamp;
  1470. //
  1471. // keep a running status on the error for the current
  1472. // coefficient pair for this channel
  1473. //
  1474. lError = lSamp - iSample;
  1475. adwTotalErrorL[i] += (lError * lError) >> 7;
  1476. //
  1477. // RIGHT channel.
  1478. //
  1479. //
  1480. // calculate the prediction based on the previous two
  1481. // samples
  1482. //
  1483. lPrediction = adpcmCalcPrediction( iSamp1R, iCoef1,
  1484. iSamp2R, iCoef2 );
  1485. //
  1486. // Grab the next sample to encode.
  1487. //
  1488. iSample = pcmRead16(pbSrcThisBlock);
  1489. pbSrcThisBlock += sizeof(short);
  1490. //
  1491. // encode it
  1492. //
  1493. lError = (long)iSample - lPrediction;
  1494. iOutputR = (int)(lError / iDeltaR);
  1495. if (iOutputR > OUTPUT4MAX)
  1496. iOutputR = OUTPUT4MAX;
  1497. else if (iOutputR < OUTPUT4MIN)
  1498. iOutputR = OUTPUT4MIN;
  1499. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  1500. if (lSamp > 32767)
  1501. lSamp = 32767;
  1502. else if (lSamp < -32768)
  1503. lSamp = -32768;
  1504. //
  1505. // compute the next iDelta
  1506. //
  1507. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  1508. //
  1509. // Save updated delay samples.
  1510. //
  1511. iSamp2R = iSamp1R;
  1512. iSamp1R = (int)lSamp;
  1513. //
  1514. // keep a running status on the error for the current
  1515. // coefficient pair for this channel
  1516. //
  1517. lError = lSamp - iSample;
  1518. adwTotalErrorR[i] += (lError * lError) >> 7;
  1519. }
  1520. }
  1521. //
  1522. // WHEW! we have now made 7 passes over the data and calculated
  1523. // the error for each--so it's time to find the one that produced
  1524. // the lowest error and use that predictor.
  1525. //
  1526. iBestPredictorL = 0;
  1527. iBestPredictorR = 0;
  1528. dwL = adwTotalErrorL[0];
  1529. dwR = adwTotalErrorR[0];
  1530. for (i = 1; i < MSADPCM_MAX_COEFFICIENTS; i++)
  1531. {
  1532. if (adwTotalErrorL[i] < dwL)
  1533. {
  1534. iBestPredictorL = i;
  1535. dwL = adwTotalErrorL[i];
  1536. }
  1537. if (adwTotalErrorR[i] < dwR)
  1538. {
  1539. iBestPredictorR = i;
  1540. dwR = adwTotalErrorR[i];
  1541. }
  1542. }
  1543. iCoef1L = lpCoefSet[iBestPredictorL].iCoef1;
  1544. iCoef1R = lpCoefSet[iBestPredictorR].iCoef1;
  1545. iCoef2L = lpCoefSet[iBestPredictorL].iCoef2;
  1546. iCoef2R = lpCoefSet[iBestPredictorR].iCoef2;
  1547. //
  1548. // grab first iDelta from our precomputed first deltas that we
  1549. // calculated above
  1550. //
  1551. iDeltaL = aiFirstDeltaL[iBestPredictorL];
  1552. iDeltaR = aiFirstDeltaR[iBestPredictorR];
  1553. //
  1554. // Set up first two samples - these have already been converted
  1555. // to 16-bit values in aiSamples[], but make sure to increment
  1556. // pbSrc so that it keeps in sync.
  1557. //
  1558. iSamp1L = aiSamplesL[1];
  1559. iSamp1R = aiSamplesR[1];
  1560. iSamp2L = aiSamplesL[0];
  1561. iSamp2R = aiSamplesR[0];
  1562. pbSrc += 2*sizeof(short) * 2; // Last 2 = # of channels.
  1563. ASSERT( cBlockSamples != 1 );
  1564. cBlockSamples -= 2;
  1565. //
  1566. // write the block header for the encoded data
  1567. //
  1568. // the block header is composed of the following data:
  1569. // 1 byte predictor per channel
  1570. // 2 byte delta per channel
  1571. // 2 byte first delayed sample per channel
  1572. // 2 byte second delayed sample per channel
  1573. //
  1574. *pbDst++ = (BYTE)iBestPredictorL;
  1575. *pbDst++ = (BYTE)iBestPredictorR;
  1576. pcmWrite16Unaligned(pbDst,iDeltaL);
  1577. pbDst += sizeof(short);
  1578. pcmWrite16Unaligned(pbDst,iDeltaR);
  1579. pbDst += sizeof(short);
  1580. pcmWrite16Unaligned(pbDst,iSamp1L);
  1581. pbDst += sizeof(short);
  1582. pcmWrite16Unaligned(pbDst,iSamp1R);
  1583. pbDst += sizeof(short);
  1584. pcmWrite16Unaligned(pbDst,iSamp2L);
  1585. pbDst += sizeof(short);
  1586. pcmWrite16Unaligned(pbDst,iSamp2R);
  1587. pbDst += sizeof(short);
  1588. //
  1589. // We have written the header for this block--now write the data
  1590. // chunk (which consists of a bunch of encoded nibbles).
  1591. //
  1592. while( cBlockSamples-- )
  1593. {
  1594. //
  1595. // LEFT channel.
  1596. //
  1597. iSample = pcmRead16(pbSrc);
  1598. pbSrc += sizeof(short);
  1599. //
  1600. // calculate the prediction based on the previous two samples
  1601. //
  1602. lPrediction = adpcmCalcPrediction(iSamp1L,iCoef1L,iSamp2L,iCoef2L);
  1603. //
  1604. // encode the sample
  1605. //
  1606. lError = (long)iSample - lPrediction;
  1607. iOutputL = (int)(lError / iDeltaL);
  1608. if (iOutputL > OUTPUT4MAX)
  1609. iOutputL = OUTPUT4MAX;
  1610. else if (iOutputL < OUTPUT4MIN)
  1611. iOutputL = OUTPUT4MIN;
  1612. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  1613. if (lSamp > 32767)
  1614. lSamp = 32767;
  1615. else if (lSamp < -32768)
  1616. lSamp = -32768;
  1617. //
  1618. // compute the next iDelta
  1619. //
  1620. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  1621. //
  1622. // Save updated delay samples.
  1623. //
  1624. iSamp2L = iSamp1L;
  1625. iSamp1L = (int)lSamp;
  1626. //
  1627. // RIGHT channel.
  1628. //
  1629. iSample = pcmRead16(pbSrc);
  1630. pbSrc += sizeof(short);
  1631. //
  1632. // calculate the prediction based on the previous two samples
  1633. //
  1634. lPrediction = adpcmCalcPrediction(iSamp1R,iCoef1R,iSamp2R,iCoef2R);
  1635. //
  1636. // encode the sample
  1637. //
  1638. lError = (long)iSample - lPrediction;
  1639. iOutputR = (int)(lError / iDeltaR);
  1640. if (iOutputR > OUTPUT4MAX)
  1641. iOutputR = OUTPUT4MAX;
  1642. else if (iOutputR < OUTPUT4MIN)
  1643. iOutputR = OUTPUT4MIN;
  1644. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  1645. if (lSamp > 32767)
  1646. lSamp = 32767;
  1647. else if (lSamp < -32768)
  1648. lSamp = -32768;
  1649. //
  1650. // compute the next iDelta
  1651. //
  1652. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  1653. //
  1654. // Save updated delay samples.
  1655. //
  1656. iSamp2R = iSamp1R;
  1657. iSamp1R = (int)lSamp;
  1658. //
  1659. // Write out the encoded byte.
  1660. //
  1661. *pbDst++ = (BYTE)( ((iOutputL&OUTPUT4MASK)<<4) |
  1662. (iOutputR&OUTPUT4MASK) );
  1663. }
  1664. }
  1665. //
  1666. // We return the number of bytes used in the destination. This is
  1667. // simply the difference in bytes from where we started.
  1668. //
  1669. return (DWORD)(pbDst - pbDstStart);
  1670. } // adpcmEncode4Bit_S16_FullPass()
  1671. //==========================================================================;
  1672. //
  1673. // The code below this point is only compiled into WIN32 builds. Win16
  1674. // builds will call 386 assembler routines instead; see the routine
  1675. // acmdStreamOpen() in codec.c for more details.
  1676. //
  1677. //==========================================================================;
  1678. #ifdef WIN32
  1679. //==========================================================================;
  1680. //
  1681. // REALTIME ENCODE ROUTINES
  1682. //
  1683. //==========================================================================;
  1684. //--------------------------------------------------------------------------;
  1685. //
  1686. // DWORD FNGLOBAL adpcmEncode4Bit_M08_OnePass
  1687. // DWORD FNGLOBAL adpcmEncode4Bit_M16_OnePass
  1688. // DWORD FNGLOBAL adpcmEncode4Bit_S08_OnePass
  1689. // DWORD FNGLOBAL adpcmEncode4Bit_S16_OnePass
  1690. //
  1691. // Description:
  1692. //
  1693. //
  1694. // Arguments:
  1695. //
  1696. //
  1697. // Return (DWORD FNGLOBAL):
  1698. //
  1699. //
  1700. // History:
  1701. // 1/27/93 cjp [curtisp]
  1702. // 3/03/94 rmh [bobhed]
  1703. //
  1704. //--------------------------------------------------------------------------;
  1705. DWORD FNGLOBAL adpcmEncode4Bit_M08_OnePass
  1706. (
  1707. HPBYTE pbSrc,
  1708. DWORD cbSrcLength,
  1709. HPBYTE pbDst,
  1710. UINT nBlockAlignment,
  1711. UINT cSamplesPerBlock,
  1712. UINT nNumCoef,
  1713. LPADPCMCOEFSET lpCoefSet
  1714. )
  1715. {
  1716. HPBYTE pbDstStart;
  1717. DWORD cSrcSamples;
  1718. UINT cBlockSamples;
  1719. int iSamp1;
  1720. int iSamp2;
  1721. int iDelta;
  1722. int iOutput1;
  1723. int iOutput2;
  1724. int iSample;
  1725. long lSamp;
  1726. long lError;
  1727. long lPrediction;
  1728. pbDstStart = pbDst;
  1729. cSrcSamples = pcmM08BytesToSamples(cbSrcLength);
  1730. //
  1731. // step through each block of PCM data and encode it to 4 bit ADPCM
  1732. //
  1733. while( 0 != cSrcSamples )
  1734. {
  1735. //
  1736. // determine how much data we should encode for this block--this
  1737. // will be cSamplesPerBlock until we hit the last chunk of PCM
  1738. // data that will not fill a complete block. so on the last block
  1739. // we only encode that amount of data remaining...
  1740. //
  1741. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  1742. cSrcSamples -= cBlockSamples;
  1743. //
  1744. // write the block header for the encoded data
  1745. //
  1746. // the block header is composed of the following data:
  1747. // 1 byte predictor per channel
  1748. // 2 byte delta per channel
  1749. // 2 byte first delayed sample per channel
  1750. // 2 byte second delayed sample per channel
  1751. //
  1752. *pbDst++ = (BYTE)1;
  1753. iDelta = DELTA4START;
  1754. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDelta.
  1755. pbDst += sizeof(short);
  1756. //
  1757. // Note that iSamp2 comes before iSamp1. If we only have one
  1758. // sample, then set iSamp1 to zero.
  1759. //
  1760. iSamp2 = pcmRead08(pbSrc++);
  1761. if( --cBlockSamples > 0 ) {
  1762. iSamp1 = pcmRead08(pbSrc++);
  1763. cBlockSamples--;
  1764. } else {
  1765. iSamp1 = 0;
  1766. }
  1767. pcmWrite16Unaligned(pbDst,iSamp1);
  1768. pbDst += sizeof(short);
  1769. pcmWrite16Unaligned(pbDst,iSamp2);
  1770. pbDst += sizeof(short);
  1771. //
  1772. // We have written the header for this block--now write the data
  1773. // chunk (which consists of a bunch of encoded nibbles).
  1774. //
  1775. while( cBlockSamples>0 )
  1776. {
  1777. //
  1778. // Sample 1.
  1779. //
  1780. iSample = pcmRead08(pbSrc++);
  1781. cBlockSamples--;
  1782. //
  1783. // calculate the prediction based on the previous two samples
  1784. //
  1785. lPrediction = ((long)iSamp1<<1) - iSamp2;
  1786. //
  1787. // encode the sample
  1788. //
  1789. lError = (long)iSample - lPrediction;
  1790. iOutput1 = (int)(lError / iDelta);
  1791. if (iOutput1 > OUTPUT4MAX)
  1792. iOutput1 = OUTPUT4MAX;
  1793. else if (iOutput1 < OUTPUT4MIN)
  1794. iOutput1 = OUTPUT4MIN;
  1795. lSamp = lPrediction + ((long)iDelta * iOutput1);
  1796. if (lSamp > 32767)
  1797. lSamp = 32767;
  1798. else if (lSamp < -32768)
  1799. lSamp = -32768;
  1800. //
  1801. // compute the next iDelta
  1802. //
  1803. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  1804. //
  1805. // Save updated delay samples.
  1806. //
  1807. iSamp2 = iSamp1;
  1808. iSamp1 = (int)lSamp;
  1809. //
  1810. // Sample 2.
  1811. //
  1812. if( cBlockSamples>0 ) {
  1813. iSample = pcmRead08(pbSrc++);
  1814. cBlockSamples--;
  1815. //
  1816. // calculate the prediction based on the previous two samples
  1817. //
  1818. lPrediction = ((long)iSamp1<<1) - iSamp2;
  1819. //
  1820. // encode the sample
  1821. //
  1822. lError = (long)iSample - lPrediction;
  1823. iOutput2 = (int)(lError / iDelta);
  1824. if (iOutput2 > OUTPUT4MAX)
  1825. iOutput2 = OUTPUT4MAX;
  1826. else if (iOutput2 < OUTPUT4MIN)
  1827. iOutput2 = OUTPUT4MIN;
  1828. lSamp = lPrediction + ((long)iDelta * iOutput2);
  1829. if (lSamp > 32767)
  1830. lSamp = 32767;
  1831. else if (lSamp < -32768)
  1832. lSamp = -32768;
  1833. //
  1834. // compute the next iDelta
  1835. //
  1836. iDelta = adpcmCalcDelta(iOutput2,iDelta);
  1837. //
  1838. // Save updated delay samples.
  1839. //
  1840. iSamp2 = iSamp1;
  1841. iSamp1 = (int)lSamp;
  1842. } else {
  1843. iOutput2 = 0;
  1844. }
  1845. //
  1846. // Write out the encoded byte.
  1847. //
  1848. *pbDst++ = (BYTE)( ((iOutput1&OUTPUT4MASK)<<4) |
  1849. (iOutput2&OUTPUT4MASK) );
  1850. }
  1851. }
  1852. //
  1853. // We return the number of bytes used in the destination. This is
  1854. // simply the difference in bytes from where we started.
  1855. //
  1856. return (DWORD)(pbDst - pbDstStart);
  1857. } // adpcmEncode4Bit_M08_OnePass()
  1858. //--------------------------------------------------------------------------;
  1859. //--------------------------------------------------------------------------;
  1860. DWORD FNGLOBAL adpcmEncode4Bit_M16_OnePass
  1861. (
  1862. HPBYTE pbSrc,
  1863. DWORD cbSrcLength,
  1864. HPBYTE pbDst,
  1865. UINT nBlockAlignment,
  1866. UINT cSamplesPerBlock,
  1867. UINT nNumCoef,
  1868. LPADPCMCOEFSET lpCoefSet
  1869. )
  1870. {
  1871. HPBYTE pbDstStart;
  1872. DWORD cSrcSamples;
  1873. UINT cBlockSamples;
  1874. int iSamp1;
  1875. int iSamp2;
  1876. int iDelta;
  1877. int iOutput1;
  1878. int iOutput2;
  1879. int iSample;
  1880. long lSamp;
  1881. long lError;
  1882. long lPrediction;
  1883. pbDstStart = pbDst;
  1884. cSrcSamples = pcmM16BytesToSamples(cbSrcLength);
  1885. //
  1886. // step through each block of PCM data and encode it to 4 bit ADPCM
  1887. //
  1888. while( 0 != cSrcSamples )
  1889. {
  1890. //
  1891. // determine how much data we should encode for this block--this
  1892. // will be cSamplesPerBlock until we hit the last chunk of PCM
  1893. // data that will not fill a complete block. so on the last block
  1894. // we only encode that amount of data remaining...
  1895. //
  1896. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  1897. cSrcSamples -= cBlockSamples;
  1898. //
  1899. // write the block header for the encoded data
  1900. //
  1901. // the block header is composed of the following data:
  1902. // 1 byte predictor per channel
  1903. // 2 byte delta per channel
  1904. // 2 byte first delayed sample per channel
  1905. // 2 byte second delayed sample per channel
  1906. //
  1907. *pbDst++ = (BYTE)1;
  1908. iDelta = DELTA4START;
  1909. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDelta;
  1910. pbDst += sizeof(short);
  1911. //
  1912. // Note that iSamp2 comes before iSamp1. If we only have one
  1913. // sample, then set iSamp1 to zero.
  1914. //
  1915. iSamp2 = pcmRead16(pbSrc);
  1916. pbSrc += sizeof(short);
  1917. if( --cBlockSamples > 0 ) {
  1918. iSamp1 = pcmRead16(pbSrc);
  1919. pbSrc += sizeof(short);
  1920. cBlockSamples--;
  1921. } else {
  1922. iSamp1 = 0;
  1923. }
  1924. pcmWrite16Unaligned(pbDst,iSamp1);
  1925. pbDst += sizeof(short);
  1926. pcmWrite16Unaligned(pbDst,iSamp2);
  1927. pbDst += sizeof(short);
  1928. //
  1929. // We have written the header for this block--now write the data
  1930. // chunk (which consists of a bunch of encoded nibbles).
  1931. //
  1932. while( cBlockSamples>0 )
  1933. {
  1934. //
  1935. // Sample 1.
  1936. //
  1937. iSample = pcmRead16(pbSrc);
  1938. pbSrc += sizeof(short);
  1939. cBlockSamples--;
  1940. //
  1941. // calculate the prediction based on the previous two samples
  1942. //
  1943. lPrediction = ((long)iSamp1<<1) - iSamp2;
  1944. //
  1945. // encode the sample
  1946. //
  1947. lError = (long)iSample - lPrediction;
  1948. iOutput1 = (int)(lError / iDelta);
  1949. if (iOutput1 > OUTPUT4MAX)
  1950. iOutput1 = OUTPUT4MAX;
  1951. else if (iOutput1 < OUTPUT4MIN)
  1952. iOutput1 = OUTPUT4MIN;
  1953. lSamp = lPrediction + ((long)iDelta * iOutput1);
  1954. if (lSamp > 32767)
  1955. lSamp = 32767;
  1956. else if (lSamp < -32768)
  1957. lSamp = -32768;
  1958. //
  1959. // compute the next iDelta
  1960. //
  1961. iDelta = adpcmCalcDelta(iOutput1,iDelta);
  1962. //
  1963. // Save updated delay samples.
  1964. //
  1965. iSamp2 = iSamp1;
  1966. iSamp1 = (int)lSamp;
  1967. //
  1968. // Sample 2.
  1969. //
  1970. if( cBlockSamples>0 ) {
  1971. iSample = pcmRead16(pbSrc);
  1972. pbSrc += sizeof(short);
  1973. cBlockSamples--;
  1974. //
  1975. // calculate the prediction based on the previous two samples
  1976. //
  1977. lPrediction = ((long)iSamp1<<1) - iSamp2;
  1978. //
  1979. // encode the sample
  1980. //
  1981. lError = (long)iSample - lPrediction;
  1982. iOutput2 = (int)(lError / iDelta);
  1983. if (iOutput2 > OUTPUT4MAX)
  1984. iOutput2 = OUTPUT4MAX;
  1985. else if (iOutput2 < OUTPUT4MIN)
  1986. iOutput2 = OUTPUT4MIN;
  1987. lSamp = lPrediction + ((long)iDelta * iOutput2);
  1988. if (lSamp > 32767)
  1989. lSamp = 32767;
  1990. else if (lSamp < -32768)
  1991. lSamp = -32768;
  1992. //
  1993. // compute the next iDelta
  1994. //
  1995. iDelta = adpcmCalcDelta(iOutput2,iDelta);
  1996. //
  1997. // Save updated delay samples.
  1998. //
  1999. iSamp2 = iSamp1;
  2000. iSamp1 = (int)lSamp;
  2001. } else {
  2002. iOutput2 = 0;
  2003. }
  2004. //
  2005. // Write out the encoded byte.
  2006. //
  2007. *pbDst++ = (BYTE)( ((iOutput1&OUTPUT4MASK)<<4) |
  2008. (iOutput2&OUTPUT4MASK) );
  2009. }
  2010. }
  2011. //
  2012. // We return the number of bytes used in the destination. This is
  2013. // simply the difference in bytes from where we started.
  2014. //
  2015. return (DWORD)(pbDst - pbDstStart);
  2016. } // adpcmEncode4Bit_M16_OnePass()
  2017. //--------------------------------------------------------------------------;
  2018. //--------------------------------------------------------------------------;
  2019. DWORD FNGLOBAL adpcmEncode4Bit_S08_OnePass
  2020. (
  2021. HPBYTE pbSrc,
  2022. DWORD cbSrcLength,
  2023. HPBYTE pbDst,
  2024. UINT nBlockAlignment,
  2025. UINT cSamplesPerBlock,
  2026. UINT nNumCoef,
  2027. LPADPCMCOEFSET lpCoefSet
  2028. )
  2029. {
  2030. HPBYTE pbDstStart;
  2031. DWORD cSrcSamples;
  2032. UINT cBlockSamples;
  2033. int iSamp1L;
  2034. int iSamp2L;
  2035. int iDeltaL;
  2036. int iOutputL;
  2037. int iSamp1R;
  2038. int iSamp2R;
  2039. int iDeltaR;
  2040. int iOutputR;
  2041. int iSample;
  2042. long lSamp;
  2043. long lError;
  2044. long lPrediction;
  2045. pbDstStart = pbDst;
  2046. cSrcSamples = pcmS08BytesToSamples(cbSrcLength);
  2047. //
  2048. // step through each block of PCM data and encode it to 4 bit ADPCM
  2049. //
  2050. while( 0 != cSrcSamples )
  2051. {
  2052. //
  2053. // determine how much data we should encode for this block--this
  2054. // will be cSamplesPerBlock until we hit the last chunk of PCM
  2055. // data that will not fill a complete block. so on the last block
  2056. // we only encode that amount of data remaining...
  2057. //
  2058. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  2059. cSrcSamples -= cBlockSamples;
  2060. //
  2061. // write the block header for the encoded data
  2062. //
  2063. // the block header is composed of the following data:
  2064. // 1 byte predictor per channel
  2065. // 2 byte delta per channel
  2066. // 2 byte first delayed sample per channel
  2067. // 2 byte second delayed sample per channel
  2068. //
  2069. *pbDst++ = (BYTE)1;
  2070. *pbDst++ = (BYTE)1;
  2071. iDeltaL = DELTA4START;
  2072. iDeltaR = DELTA4START;
  2073. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDeltaL.
  2074. pbDst += sizeof(short);
  2075. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDeltaR.
  2076. pbDst += sizeof(short);
  2077. //
  2078. // Note that iSamp2 comes before iSamp1. If we only have one
  2079. // sample, then set iSamp1 to zero.
  2080. //
  2081. iSamp2L = pcmRead08(pbSrc++);
  2082. iSamp2R = pcmRead08(pbSrc++);
  2083. if( --cBlockSamples > 0 ) {
  2084. iSamp1L = pcmRead08(pbSrc++);
  2085. iSamp1R = pcmRead08(pbSrc++);
  2086. cBlockSamples--;
  2087. } else {
  2088. iSamp1L = 0;
  2089. iSamp1R = 0;
  2090. }
  2091. pcmWrite16Unaligned(pbDst,iSamp1L);
  2092. pbDst += sizeof(short);
  2093. pcmWrite16Unaligned(pbDst,iSamp1R);
  2094. pbDst += sizeof(short);
  2095. pcmWrite16Unaligned(pbDst,iSamp2L);
  2096. pbDst += sizeof(short);
  2097. pcmWrite16Unaligned(pbDst,iSamp2R);
  2098. pbDst += sizeof(short);
  2099. //
  2100. // We have written the header for this block--now write the data
  2101. // chunk (which consists of a bunch of encoded nibbles).
  2102. //
  2103. while( cBlockSamples-- )
  2104. {
  2105. //
  2106. // LEFT channel.
  2107. //
  2108. iSample = pcmRead08(pbSrc++);
  2109. //
  2110. // calculate the prediction based on the previous two samples
  2111. //
  2112. lPrediction = ((long)iSamp1L<<1) - iSamp2L;
  2113. //
  2114. // encode the sample
  2115. //
  2116. lError = (long)iSample - lPrediction;
  2117. iOutputL = (int)(lError / iDeltaL);
  2118. if (iOutputL > OUTPUT4MAX)
  2119. iOutputL = OUTPUT4MAX;
  2120. else if (iOutputL < OUTPUT4MIN)
  2121. iOutputL = OUTPUT4MIN;
  2122. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  2123. if (lSamp > 32767)
  2124. lSamp = 32767;
  2125. else if (lSamp < -32768)
  2126. lSamp = -32768;
  2127. //
  2128. // compute the next iDelta
  2129. //
  2130. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  2131. //
  2132. // Save updated delay samples.
  2133. //
  2134. iSamp2L = iSamp1L;
  2135. iSamp1L = (int)lSamp;
  2136. //
  2137. // RIGHT channel.
  2138. //
  2139. iSample = pcmRead08(pbSrc++);
  2140. //
  2141. // calculate the prediction based on the previous two samples
  2142. //
  2143. lPrediction = ((long)iSamp1R<<1) - iSamp2R;
  2144. //
  2145. // encode the sample
  2146. //
  2147. lError = (long)iSample - lPrediction;
  2148. iOutputR = (int)(lError / iDeltaR);
  2149. if (iOutputR > OUTPUT4MAX)
  2150. iOutputR = OUTPUT4MAX;
  2151. else if (iOutputR < OUTPUT4MIN)
  2152. iOutputR = OUTPUT4MIN;
  2153. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  2154. if (lSamp > 32767)
  2155. lSamp = 32767;
  2156. else if (lSamp < -32768)
  2157. lSamp = -32768;
  2158. //
  2159. // compute the next iDelta
  2160. //
  2161. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  2162. //
  2163. // Save updated delay samples.
  2164. //
  2165. iSamp2R = iSamp1R;
  2166. iSamp1R = (int)lSamp;
  2167. //
  2168. // Write out the encoded byte.
  2169. //
  2170. *pbDst++ = (BYTE)( ((iOutputL&OUTPUT4MASK)<<4) |
  2171. (iOutputR&OUTPUT4MASK) );
  2172. }
  2173. }
  2174. //
  2175. // We return the number of bytes used in the destination. This is
  2176. // simply the difference in bytes from where we started.
  2177. //
  2178. return (DWORD)(pbDst - pbDstStart);
  2179. } // adpcmEncode4Bit_S08_OnePass()
  2180. //--------------------------------------------------------------------------;
  2181. //--------------------------------------------------------------------------;
  2182. DWORD FNGLOBAL adpcmEncode4Bit_S16_OnePass
  2183. (
  2184. HPBYTE pbSrc,
  2185. DWORD cbSrcLength,
  2186. HPBYTE pbDst,
  2187. UINT nBlockAlignment,
  2188. UINT cSamplesPerBlock,
  2189. UINT nNumCoef,
  2190. LPADPCMCOEFSET lpCoefSet
  2191. )
  2192. {
  2193. HPBYTE pbDstStart;
  2194. DWORD cSrcSamples;
  2195. UINT cBlockSamples;
  2196. int iSamp1L;
  2197. int iSamp2L;
  2198. int iDeltaL;
  2199. int iOutputL;
  2200. int iSamp1R;
  2201. int iSamp2R;
  2202. int iDeltaR;
  2203. int iOutputR;
  2204. int iSample;
  2205. long lSamp;
  2206. long lError;
  2207. long lPrediction;
  2208. pbDstStart = pbDst;
  2209. cSrcSamples = pcmS16BytesToSamples(cbSrcLength);
  2210. //
  2211. // step through each block of PCM data and encode it to 4 bit ADPCM
  2212. //
  2213. while( 0 != cSrcSamples )
  2214. {
  2215. //
  2216. // determine how much data we should encode for this block--this
  2217. // will be cSamplesPerBlock until we hit the last chunk of PCM
  2218. // data that will not fill a complete block. so on the last block
  2219. // we only encode that amount of data remaining...
  2220. //
  2221. cBlockSamples = (UINT)min(cSrcSamples, cSamplesPerBlock);
  2222. cSrcSamples -= cBlockSamples;
  2223. //
  2224. // write the block header for the encoded data
  2225. //
  2226. // the block header is composed of the following data:
  2227. // 1 byte predictor per channel
  2228. // 2 byte delta per channel
  2229. // 2 byte first delayed sample per channel
  2230. // 2 byte second delayed sample per channel
  2231. //
  2232. *pbDst++ = (BYTE)1;
  2233. *pbDst++ = (BYTE)1;
  2234. iDeltaL = DELTA4START;
  2235. iDeltaR = DELTA4START;
  2236. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDeltaL.
  2237. pbDst += sizeof(short);
  2238. pcmWrite16Unaligned(pbDst,DELTA4START); // Same as iDeltaR.
  2239. pbDst += sizeof(short);
  2240. //
  2241. // Note that iSamp2 comes before iSamp1. If we only have one
  2242. // sample, then set iSamp1 to zero.
  2243. //
  2244. iSamp2L = pcmRead16(pbSrc);
  2245. pbSrc += sizeof(short);
  2246. iSamp2R = pcmRead16(pbSrc);
  2247. pbSrc += sizeof(short);
  2248. if( --cBlockSamples > 0 ) {
  2249. iSamp1L = pcmRead16(pbSrc);
  2250. pbSrc += sizeof(short);
  2251. iSamp1R = pcmRead16(pbSrc);
  2252. pbSrc += sizeof(short);
  2253. cBlockSamples--;
  2254. } else {
  2255. iSamp1L = 0;
  2256. iSamp1R = 0;
  2257. }
  2258. pcmWrite16Unaligned(pbDst,iSamp1L);
  2259. pbDst += sizeof(short);
  2260. pcmWrite16Unaligned(pbDst,iSamp1R);
  2261. pbDst += sizeof(short);
  2262. pcmWrite16Unaligned(pbDst,iSamp2L);
  2263. pbDst += sizeof(short);
  2264. pcmWrite16Unaligned(pbDst,iSamp2R);
  2265. pbDst += sizeof(short);
  2266. //
  2267. // We have written the header for this block--now write the data
  2268. // chunk (which consists of a bunch of encoded nibbles).
  2269. //
  2270. while( cBlockSamples-- )
  2271. {
  2272. //
  2273. // LEFT channel.
  2274. //
  2275. iSample = pcmRead16(pbSrc);
  2276. pbSrc += sizeof(short);
  2277. //
  2278. // calculate the prediction based on the previous two samples
  2279. //
  2280. lPrediction = ((long)iSamp1L<<1) - iSamp2L;
  2281. //
  2282. // encode the sample
  2283. //
  2284. lError = (long)iSample - lPrediction;
  2285. iOutputL = (int)(lError / iDeltaL);
  2286. if (iOutputL > OUTPUT4MAX)
  2287. iOutputL = OUTPUT4MAX;
  2288. else if (iOutputL < OUTPUT4MIN)
  2289. iOutputL = OUTPUT4MIN;
  2290. lSamp = lPrediction + ((long)iDeltaL * iOutputL);
  2291. if (lSamp > 32767)
  2292. lSamp = 32767;
  2293. else if (lSamp < -32768)
  2294. lSamp = -32768;
  2295. //
  2296. // compute the next iDelta
  2297. //
  2298. iDeltaL = adpcmCalcDelta(iOutputL,iDeltaL);
  2299. //
  2300. // Save updated delay samples.
  2301. //
  2302. iSamp2L = iSamp1L;
  2303. iSamp1L = (int)lSamp;
  2304. //
  2305. // RIGHT channel.
  2306. //
  2307. iSample = pcmRead16(pbSrc);
  2308. pbSrc += sizeof(short);
  2309. //
  2310. // calculate the prediction based on the previous two samples
  2311. //
  2312. lPrediction = ((long)iSamp1R<<1) - iSamp2R;
  2313. //
  2314. // encode the sample
  2315. //
  2316. lError = (long)iSample - lPrediction;
  2317. iOutputR = (int)(lError / iDeltaR);
  2318. if (iOutputR > OUTPUT4MAX)
  2319. iOutputR = OUTPUT4MAX;
  2320. else if (iOutputR < OUTPUT4MIN)
  2321. iOutputR = OUTPUT4MIN;
  2322. lSamp = lPrediction + ((long)iDeltaR * iOutputR);
  2323. if (lSamp > 32767)
  2324. lSamp = 32767;
  2325. else if (lSamp < -32768)
  2326. lSamp = -32768;
  2327. //
  2328. // compute the next iDelta
  2329. //
  2330. iDeltaR = adpcmCalcDelta(iOutputR,iDeltaR);
  2331. //
  2332. // Save updated delay samples.
  2333. //
  2334. iSamp2R = iSamp1R;
  2335. iSamp1R = (int)lSamp;
  2336. //
  2337. // Write out the encoded byte.
  2338. //
  2339. *pbDst++ = (BYTE)( ((iOutputL&OUTPUT4MASK)<<4) |
  2340. (iOutputR&OUTPUT4MASK) );
  2341. }
  2342. }
  2343. //
  2344. // We return the number of bytes used in the destination. This is
  2345. // simply the difference in bytes from where we started.
  2346. //
  2347. return (DWORD)(pbDst - pbDstStart);
  2348. } // adpcmEncode4Bit_S16_OnePass()
  2349. //==========================================================================;
  2350. //
  2351. // DECODE ROUTINES
  2352. //
  2353. //==========================================================================;
  2354. //--------------------------------------------------------------------------;
  2355. //
  2356. // DWORD adpcmDecode4Bit_M08
  2357. // DWORD adpcmDecode4Bit_M16
  2358. // DWORD adpcmDecode4Bit_S08
  2359. // DWORD adpcmDecode4Bit_S16
  2360. //
  2361. // Description:
  2362. // These functions decode a buffer of data from MS ADPCM to PCM in the
  2363. // specified format. The appropriate function is called once for each
  2364. // ACMDM_STREAM_CONVERT message received.
  2365. //
  2366. //
  2367. // Arguments:
  2368. //
  2369. //
  2370. // Return (DWORD): The number of bytes used in the destination buffer.
  2371. //
  2372. //--------------------------------------------------------------------------;
  2373. DWORD FNGLOBAL adpcmDecode4Bit_M08
  2374. (
  2375. HPBYTE pbSrc,
  2376. DWORD cbSrcLength,
  2377. HPBYTE pbDst,
  2378. UINT nBlockAlignment,
  2379. UINT cSamplesPerBlock,
  2380. UINT nNumCoef,
  2381. LPADPCMCOEFSET lpCoefSet
  2382. )
  2383. {
  2384. HPBYTE pbDstStart;
  2385. UINT cbHeader;
  2386. UINT cbBlockLength;
  2387. UINT nPredictor;
  2388. BYTE bSample;
  2389. int iInput;
  2390. int iSamp;
  2391. int iSamp1;
  2392. int iSamp2;
  2393. int iCoef1;
  2394. int iCoef2;
  2395. int iDelta;
  2396. pbDstStart = pbDst;
  2397. cbHeader = MSADPCM_HEADER_LENGTH * 1; // 1 = number of channels.
  2398. //
  2399. //
  2400. //
  2401. while( cbSrcLength >= cbHeader )
  2402. {
  2403. //
  2404. // We have at least enough data to read a full block header.
  2405. //
  2406. // the header looks like this:
  2407. // 1 byte predictor per channel (determines coefficients).
  2408. // 2 byte delta per channel
  2409. // 2 byte first sample per channel
  2410. // 2 byte second sample per channel
  2411. //
  2412. // this gives us (7 * bChannels) bytes of header information. note
  2413. // that as long as there is _at least_ (7 * bChannels) of header
  2414. // info, we will grab the two samples from the header. We figure
  2415. // out how much data we have in the rest of the block, ie. whether
  2416. // we have a full block or not. That way we don't have to test
  2417. // each sample to see if we have run out of data.
  2418. //
  2419. cbBlockLength = (UINT)min(cbSrcLength,nBlockAlignment);
  2420. cbSrcLength -= cbBlockLength;
  2421. cbBlockLength -= cbHeader;
  2422. //
  2423. // Process the block header.
  2424. //
  2425. nPredictor = (UINT)(BYTE)(*pbSrc++);
  2426. if( nPredictor >= nNumCoef )
  2427. {
  2428. //
  2429. // the predictor is out of range--this is considered a
  2430. // fatal error with the ADPCM data, so we fail by returning
  2431. // zero bytes decoded
  2432. //
  2433. return 0;
  2434. }
  2435. iCoef1 = lpCoefSet[nPredictor].iCoef1;
  2436. iCoef2 = lpCoefSet[nPredictor].iCoef2;
  2437. iDelta = pcmRead16Unaligned(pbSrc);
  2438. pbSrc += sizeof(short);
  2439. iSamp1 = pcmRead16Unaligned(pbSrc);
  2440. pbSrc += sizeof(short);
  2441. iSamp2 = pcmRead16Unaligned(pbSrc);
  2442. pbSrc += sizeof(short);
  2443. //
  2444. // write out first 2 samples.
  2445. //
  2446. // NOTE: the samples are written to the destination PCM buffer
  2447. // in the _reverse_ order that they are in the header block:
  2448. // remember that iSamp2 is the _previous_ sample to iSamp1.
  2449. //
  2450. pcmWrite08(pbDst,iSamp2);
  2451. pcmWrite08(pbDst,iSamp1);
  2452. //
  2453. // we now need to decode the 'data' section of the ADPCM block.
  2454. // this consists of packed 4 bit nibbles. The high-order nibble
  2455. // contains the first sample; the low-order nibble contains the
  2456. // second sample.
  2457. //
  2458. while( cbBlockLength-- )
  2459. {
  2460. bSample = *pbSrc++;
  2461. //
  2462. // Sample 1.
  2463. //
  2464. iInput = (int)(((signed char)bSample) >> 4); //Sign-extend.
  2465. iSamp = adpcmDecodeSample( iSamp1,iCoef1,
  2466. iSamp2,iCoef2,
  2467. iInput,iDelta );
  2468. iDelta = adpcmCalcDelta( iInput,iDelta );
  2469. pcmWrite08(pbDst++,iSamp);
  2470. //
  2471. // ripple our previous samples down making the new iSamp1
  2472. // equal to the sample we just decoded
  2473. //
  2474. iSamp2 = iSamp1;
  2475. iSamp1 = iSamp;
  2476. //
  2477. // Sample 2.
  2478. //
  2479. iInput = (int)(((signed char)(bSample<<4)) >> 4); //Sign-extend.
  2480. iSamp = adpcmDecodeSample( iSamp1,iCoef1,
  2481. iSamp2,iCoef2,
  2482. iInput,iDelta );
  2483. iDelta = adpcmCalcDelta( iInput,iDelta );
  2484. pcmWrite08(pbDst++,iSamp);
  2485. //
  2486. // ripple our previous samples down making the new iSamp1
  2487. // equal to the sample we just decoded
  2488. //
  2489. iSamp2 = iSamp1;
  2490. iSamp1 = iSamp;
  2491. }
  2492. }
  2493. //
  2494. // We return the number of bytes used in the destination. This is
  2495. // simply the difference in bytes from where we started.
  2496. //
  2497. return (DWORD)(pbDst - pbDstStart);
  2498. } // adpcmDecode4Bit_M08()
  2499. //--------------------------------------------------------------------------;
  2500. //--------------------------------------------------------------------------;
  2501. DWORD FNGLOBAL adpcmDecode4Bit_M16
  2502. (
  2503. HPBYTE pbSrc,
  2504. DWORD cbSrcLength,
  2505. HPBYTE pbDst,
  2506. UINT nBlockAlignment,
  2507. UINT cSamplesPerBlock,
  2508. UINT nNumCoef,
  2509. LPADPCMCOEFSET lpCoefSet
  2510. )
  2511. {
  2512. HPBYTE pbDstStart;
  2513. UINT cbHeader;
  2514. UINT cbBlockLength;
  2515. UINT nPredictor;
  2516. BYTE bSample;
  2517. int iInput;
  2518. int iSamp;
  2519. int iSamp1;
  2520. int iSamp2;
  2521. int iCoef1;
  2522. int iCoef2;
  2523. int iDelta;
  2524. pbDstStart = pbDst;
  2525. cbHeader = MSADPCM_HEADER_LENGTH * 1; // 1 = number of channels.
  2526. //
  2527. //
  2528. //
  2529. while( cbSrcLength >= cbHeader )
  2530. {
  2531. //
  2532. // We have at least enough data to read a full block header.
  2533. //
  2534. // the header looks like this:
  2535. // 1 byte predictor per channel (determines coefficients).
  2536. // 2 byte delta per channel
  2537. // 2 byte first sample per channel
  2538. // 2 byte second sample per channel
  2539. //
  2540. // this gives us (7 * bChannels) bytes of header information. note
  2541. // that as long as there is _at least_ (7 * bChannels) of header
  2542. // info, we will grab the two samples from the header. We figure
  2543. // out how much data we have in the rest of the block, ie. whether
  2544. // we have a full block or not. That way we don't have to test
  2545. // each sample to see if we have run out of data.
  2546. //
  2547. cbBlockLength = (UINT)min(cbSrcLength,nBlockAlignment);
  2548. cbSrcLength -= cbBlockLength;
  2549. cbBlockLength -= cbHeader;
  2550. //
  2551. // Process the block header.
  2552. //
  2553. nPredictor = (UINT)(BYTE)(*pbSrc++);
  2554. if( nPredictor >= nNumCoef )
  2555. {
  2556. //
  2557. // the predictor is out of range--this is considered a
  2558. // fatal error with the ADPCM data, so we fail by returning
  2559. // zero bytes decoded
  2560. //
  2561. return 0;
  2562. }
  2563. iCoef1 = lpCoefSet[nPredictor].iCoef1;
  2564. iCoef2 = lpCoefSet[nPredictor].iCoef2;
  2565. iDelta = pcmRead16Unaligned(pbSrc);
  2566. pbSrc += sizeof(short);
  2567. iSamp1 = pcmRead16Unaligned(pbSrc);
  2568. pbSrc += sizeof(short);
  2569. iSamp2 = pcmRead16Unaligned(pbSrc);
  2570. pbSrc += sizeof(short);
  2571. //
  2572. // write out first 2 samples.
  2573. //
  2574. // NOTE: the samples are written to the destination PCM buffer
  2575. // in the _reverse_ order that they are in the header block:
  2576. // remember that iSamp2 is the _previous_ sample to iSamp1.
  2577. //
  2578. pcmWrite16(pbDst,iSamp2);
  2579. pbDst += sizeof(short);
  2580. pcmWrite16(pbDst,iSamp1);
  2581. pbDst += sizeof(short);
  2582. //
  2583. // we now need to decode the 'data' section of the ADPCM block.
  2584. // this consists of packed 4 bit nibbles. The high-order nibble
  2585. // contains the first sample; the low-order nibble contains the
  2586. // second sample.
  2587. //
  2588. while( cbBlockLength-- )
  2589. {
  2590. bSample = *pbSrc++;
  2591. //
  2592. // Sample 1.
  2593. //
  2594. iInput = (int)(((signed char)bSample) >> 4); //Sign-extend.
  2595. iSamp = adpcmDecodeSample( iSamp1,iCoef1,
  2596. iSamp2,iCoef2,
  2597. iInput,iDelta );
  2598. iDelta = adpcmCalcDelta( iInput,iDelta );
  2599. pcmWrite16(pbDst,iSamp);
  2600. pbDst += sizeof(short);
  2601. //
  2602. // ripple our previous samples down making the new iSamp1
  2603. // equal to the sample we just decoded
  2604. //
  2605. iSamp2 = iSamp1;
  2606. iSamp1 = iSamp;
  2607. //
  2608. // Sample 2.
  2609. //
  2610. iInput = (int)(((signed char)(bSample<<4)) >> 4); //Sign-extend.
  2611. iSamp = adpcmDecodeSample( iSamp1,iCoef1,
  2612. iSamp2,iCoef2,
  2613. iInput,iDelta );
  2614. iDelta = adpcmCalcDelta( iInput,iDelta );
  2615. pcmWrite16(pbDst,iSamp);
  2616. pbDst += sizeof(short);
  2617. //
  2618. // ripple our previous samples down making the new iSamp1
  2619. // equal to the sample we just decoded
  2620. //
  2621. iSamp2 = iSamp1;
  2622. iSamp1 = iSamp;
  2623. }
  2624. }
  2625. //
  2626. // We return the number of bytes used in the destination. This is
  2627. // simply the difference in bytes from where we started.
  2628. //
  2629. return (DWORD)(pbDst - pbDstStart);
  2630. } // adpcmDecode4Bit_M16()
  2631. //--------------------------------------------------------------------------;
  2632. //--------------------------------------------------------------------------;
  2633. DWORD FNGLOBAL adpcmDecode4Bit_S08
  2634. (
  2635. HPBYTE pbSrc,
  2636. DWORD cbSrcLength,
  2637. HPBYTE pbDst,
  2638. UINT nBlockAlignment,
  2639. UINT cSamplesPerBlock,
  2640. UINT nNumCoef,
  2641. LPADPCMCOEFSET lpCoefSet
  2642. )
  2643. {
  2644. HPBYTE pbDstStart;
  2645. UINT cbHeader;
  2646. UINT cbBlockLength;
  2647. UINT nPredictor;
  2648. BYTE bSample;
  2649. int iInput;
  2650. int iSamp;
  2651. int iSamp1L;
  2652. int iSamp2L;
  2653. int iCoef1L;
  2654. int iCoef2L;
  2655. int iDeltaL;
  2656. int iSamp1R;
  2657. int iSamp2R;
  2658. int iCoef1R;
  2659. int iCoef2R;
  2660. int iDeltaR;
  2661. pbDstStart = pbDst;
  2662. cbHeader = MSADPCM_HEADER_LENGTH * 2; // 2 = number of channels.
  2663. //
  2664. //
  2665. //
  2666. while( cbSrcLength >= cbHeader )
  2667. {
  2668. //
  2669. // We have at least enough data to read a full block header.
  2670. //
  2671. // the header looks like this:
  2672. // 1 byte predictor per channel (determines coefficients).
  2673. // 2 byte delta per channel
  2674. // 2 byte first sample per channel
  2675. // 2 byte second sample per channel
  2676. //
  2677. // this gives us (7 * bChannels) bytes of header information. note
  2678. // that as long as there is _at least_ (7 * bChannels) of header
  2679. // info, we will grab the two samples from the header. We figure
  2680. // out how much data we have in the rest of the block, ie. whether
  2681. // we have a full block or not. That way we don't have to test
  2682. // each sample to see if we have run out of data.
  2683. //
  2684. cbBlockLength = (UINT)min(cbSrcLength,nBlockAlignment);
  2685. cbSrcLength -= cbBlockLength;
  2686. cbBlockLength -= cbHeader;
  2687. //
  2688. // Process the block header.
  2689. //
  2690. nPredictor = (UINT)(BYTE)(*pbSrc++); // Left.
  2691. if( nPredictor >= nNumCoef )
  2692. {
  2693. //
  2694. // the predictor is out of range--this is considered a
  2695. // fatal error with the ADPCM data, so we fail by returning
  2696. // zero bytes decoded
  2697. //
  2698. return 0;
  2699. }
  2700. iCoef1L = lpCoefSet[nPredictor].iCoef1;
  2701. iCoef2L = lpCoefSet[nPredictor].iCoef2;
  2702. nPredictor = (UINT)(BYTE)(*pbSrc++); // Right.
  2703. if( nPredictor >= nNumCoef )
  2704. {
  2705. //
  2706. // the predictor is out of range--this is considered a
  2707. // fatal error with the ADPCM data, so we fail by returning
  2708. // zero bytes decoded
  2709. //
  2710. return 0;
  2711. }
  2712. iCoef1R = lpCoefSet[nPredictor].iCoef1;
  2713. iCoef2R = lpCoefSet[nPredictor].iCoef2;
  2714. iDeltaL = pcmRead16Unaligned(pbSrc); // Left.
  2715. pbSrc += sizeof(short);
  2716. iDeltaR = pcmRead16Unaligned(pbSrc); // Right.
  2717. pbSrc += sizeof(short);
  2718. iSamp1L = pcmRead16Unaligned(pbSrc); // Left.
  2719. pbSrc += sizeof(short);
  2720. iSamp1R = pcmRead16Unaligned(pbSrc); // Right.
  2721. pbSrc += sizeof(short);
  2722. iSamp2L = pcmRead16Unaligned(pbSrc); // Left.
  2723. pbSrc += sizeof(short);
  2724. iSamp2R = pcmRead16Unaligned(pbSrc); // Right.
  2725. pbSrc += sizeof(short);
  2726. //
  2727. // write out first 2 samples (per channel).
  2728. //
  2729. // NOTE: the samples are written to the destination PCM buffer
  2730. // in the _reverse_ order that they are in the header block:
  2731. // remember that iSamp2 is the _previous_ sample to iSamp1.
  2732. //
  2733. pcmWrite08(pbDst++,iSamp2L);
  2734. pcmWrite08(pbDst++,iSamp2R);
  2735. pcmWrite08(pbDst++,iSamp1L);
  2736. pcmWrite08(pbDst++,iSamp1R);
  2737. //
  2738. // we now need to decode the 'data' section of the ADPCM block.
  2739. // this consists of packed 4 bit nibbles. The high-order nibble
  2740. // contains the left sample; the low-order nibble contains the
  2741. // right sample.
  2742. //
  2743. while( cbBlockLength-- )
  2744. {
  2745. bSample = *pbSrc++;
  2746. //
  2747. // Left sample.
  2748. //
  2749. iInput = (int)(((signed char)bSample) >> 4); //Sign-extend.
  2750. iSamp = adpcmDecodeSample( iSamp1L,iCoef1L,
  2751. iSamp2L,iCoef2L,
  2752. iInput,iDeltaL );
  2753. iDeltaL = adpcmCalcDelta( iInput,iDeltaL );
  2754. pcmWrite08(pbDst++,iSamp);
  2755. //
  2756. // ripple our previous samples down making the new iSamp1
  2757. // equal to the sample we just decoded
  2758. //
  2759. iSamp2L = iSamp1L;
  2760. iSamp1L = iSamp;
  2761. //
  2762. // Right sample.
  2763. //
  2764. iInput = (int)(((signed char)(bSample<<4)) >> 4); //Sign-extend.
  2765. iSamp = adpcmDecodeSample( iSamp1R,iCoef1R,
  2766. iSamp2R,iCoef2R,
  2767. iInput,iDeltaR );
  2768. iDeltaR = adpcmCalcDelta( iInput,iDeltaR );
  2769. pcmWrite08(pbDst++,iSamp);
  2770. //
  2771. // ripple our previous samples down making the new iSamp1
  2772. // equal to the sample we just decoded
  2773. //
  2774. iSamp2R = iSamp1R;
  2775. iSamp1R = iSamp;
  2776. }
  2777. }
  2778. //
  2779. // We return the number of bytes used in the destination. This is
  2780. // simply the difference in bytes from where we started.
  2781. //
  2782. return (DWORD)(pbDst - pbDstStart);
  2783. } // adpcmDecode4Bit_S08()
  2784. //--------------------------------------------------------------------------;
  2785. //--------------------------------------------------------------------------;
  2786. DWORD FNGLOBAL adpcmDecode4Bit_S16
  2787. (
  2788. HPBYTE pbSrc,
  2789. DWORD cbSrcLength,
  2790. HPBYTE pbDst,
  2791. UINT nBlockAlignment,
  2792. UINT cSamplesPerBlock,
  2793. UINT nNumCoef,
  2794. LPADPCMCOEFSET lpCoefSet
  2795. )
  2796. {
  2797. HPBYTE pbDstStart;
  2798. UINT cbHeader;
  2799. UINT cbBlockLength;
  2800. UINT nPredictor;
  2801. BYTE bSample;
  2802. int iInput;
  2803. int iSamp;
  2804. int iSamp1L;
  2805. int iSamp2L;
  2806. int iCoef1L;
  2807. int iCoef2L;
  2808. int iDeltaL;
  2809. int iSamp1R;
  2810. int iSamp2R;
  2811. int iCoef1R;
  2812. int iCoef2R;
  2813. int iDeltaR;
  2814. pbDstStart = pbDst;
  2815. cbHeader = MSADPCM_HEADER_LENGTH * 2; // 2 = number of channels.
  2816. //
  2817. //
  2818. //
  2819. while( cbSrcLength >= cbHeader )
  2820. {
  2821. //
  2822. // We have at least enough data to read a full block header.
  2823. //
  2824. // the header looks like this:
  2825. // 1 byte predictor per channel (determines coefficients).
  2826. // 2 byte delta per channel
  2827. // 2 byte first sample per channel
  2828. // 2 byte second sample per channel
  2829. //
  2830. // this gives us (7 * bChannels) bytes of header information. note
  2831. // that as long as there is _at least_ (7 * bChannels) of header
  2832. // info, we will grab the two samples from the header. We figure
  2833. // out how much data we have in the rest of the block, ie. whether
  2834. // we have a full block or not. That way we don't have to test
  2835. // each sample to see if we have run out of data.
  2836. //
  2837. cbBlockLength = (UINT)min(cbSrcLength,nBlockAlignment);
  2838. cbSrcLength -= cbBlockLength;
  2839. cbBlockLength -= cbHeader;
  2840. //
  2841. // Process the block header.
  2842. //
  2843. nPredictor = (UINT)(BYTE)(*pbSrc++); // Left.
  2844. if( nPredictor >= nNumCoef )
  2845. {
  2846. //
  2847. // the predictor is out of range--this is considered a
  2848. // fatal error with the ADPCM data, so we fail by returning
  2849. // zero bytes decoded
  2850. //
  2851. return 0;
  2852. }
  2853. iCoef1L = lpCoefSet[nPredictor].iCoef1;
  2854. iCoef2L = lpCoefSet[nPredictor].iCoef2;
  2855. nPredictor = (UINT)(BYTE)(*pbSrc++); // Right.
  2856. if( nPredictor >= nNumCoef )
  2857. {
  2858. //
  2859. // the predictor is out of range--this is considered a
  2860. // fatal error with the ADPCM data, so we fail by returning
  2861. // zero bytes decoded
  2862. //
  2863. return 0;
  2864. }
  2865. iCoef1R = lpCoefSet[nPredictor].iCoef1;
  2866. iCoef2R = lpCoefSet[nPredictor].iCoef2;
  2867. iDeltaL = pcmRead16Unaligned(pbSrc); // Left.
  2868. pbSrc += sizeof(short);
  2869. iDeltaR = pcmRead16Unaligned(pbSrc); // Right.
  2870. pbSrc += sizeof(short);
  2871. iSamp1L = pcmRead16Unaligned(pbSrc); // Left.
  2872. pbSrc += sizeof(short);
  2873. iSamp1R = pcmRead16Unaligned(pbSrc); // Right.
  2874. pbSrc += sizeof(short);
  2875. iSamp2L = pcmRead16Unaligned(pbSrc); // Left.
  2876. pbSrc += sizeof(short);
  2877. iSamp2R = pcmRead16Unaligned(pbSrc); // Right.
  2878. pbSrc += sizeof(short);
  2879. //
  2880. // write out first 2 samples (per channel).
  2881. //
  2882. // NOTE: the samples are written to the destination PCM buffer
  2883. // in the _reverse_ order that they are in the header block:
  2884. // remember that iSamp2 is the _previous_ sample to iSamp1.
  2885. //
  2886. pcmWrite16(pbDst,iSamp2L);
  2887. pbDst += sizeof(short);
  2888. pcmWrite16(pbDst,iSamp2R);
  2889. pbDst += sizeof(short);
  2890. pcmWrite16(pbDst,iSamp1L);
  2891. pbDst += sizeof(short);
  2892. pcmWrite16(pbDst,iSamp1R);
  2893. pbDst += sizeof(short);
  2894. //
  2895. // we now need to decode the 'data' section of the ADPCM block.
  2896. // this consists of packed 4 bit nibbles. The high-order nibble
  2897. // contains the left sample; the low-order nibble contains the
  2898. // right sample.
  2899. //
  2900. while( cbBlockLength-- )
  2901. {
  2902. bSample = *pbSrc++;
  2903. //
  2904. // Left sample.
  2905. //
  2906. iInput = (int)(((signed char)bSample) >> 4); //Sign-extend.
  2907. iSamp = adpcmDecodeSample( iSamp1L,iCoef1L,
  2908. iSamp2L,iCoef2L,
  2909. iInput,iDeltaL );
  2910. iDeltaL = adpcmCalcDelta( iInput,iDeltaL );
  2911. pcmWrite16(pbDst,iSamp);
  2912. pbDst += sizeof(short);
  2913. //
  2914. // ripple our previous samples down making the new iSamp1
  2915. // equal to the sample we just decoded
  2916. //
  2917. iSamp2L = iSamp1L;
  2918. iSamp1L = iSamp;
  2919. //
  2920. // Right sample.
  2921. //
  2922. iInput = (int)(((signed char)(bSample<<4)) >> 4); //Sign-extend.
  2923. iSamp = adpcmDecodeSample( iSamp1R,iCoef1R,
  2924. iSamp2R,iCoef2R,
  2925. iInput,iDeltaR );
  2926. iDeltaR = adpcmCalcDelta( iInput,iDeltaR );
  2927. pcmWrite16(pbDst,iSamp);
  2928. pbDst += sizeof(short);
  2929. //
  2930. // ripple our previous samples down making the new iSamp1
  2931. // equal to the sample we just decoded
  2932. //
  2933. iSamp2R = iSamp1R;
  2934. iSamp1R = iSamp;
  2935. }
  2936. }
  2937. //
  2938. // We return the number of bytes used in the destination. This is
  2939. // simply the difference in bytes from where we started.
  2940. //
  2941. return (DWORD)(pbDst - pbDstStart);
  2942. } // adpcmDecode4Bit_S16()
  2943. #endif