Team Fortress 2 Source Code as on 22/4/2020
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.

381 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include <windows.h>
  3. #include <mmreg.h>
  4. #include "../toollib/toollib.h"
  5. #include "tier1/strtools.h"
  6. #include "resample.h"
  7. #define clamp(a,b,c) ( (a) > (c) ? (c) : ( (a) < (b) ? (b) : (a) ) )
  8. const int NUM_COEFFS = 7;
  9. static const float g_ResampleCoefficients[NUM_COEFFS] =
  10. {
  11. 0.0457281f, 0.168088f, 0.332501f, 0.504486f, 0.663202f, 0.803781f, 0.933856f
  12. };
  13. // generates 1 output sample for 2 input samples
  14. inline float DecimateSamplePair(float input0, float input1, const float pCoefficients[7], float xState[2], float yState[7] )
  15. {
  16. float tmp_0 = xState[0];
  17. float tmp_1 = xState[1];
  18. xState[0] = input0;
  19. xState[1] = input1;
  20. input0 = (input0 - yState[0]) * pCoefficients[0] + tmp_0;
  21. input1 = (input1 - yState[1]) * pCoefficients[1] + tmp_1;
  22. tmp_0 = yState[0];
  23. tmp_1 = yState[1];
  24. yState[0] = input0;
  25. yState[1] = input1;
  26. input0 = (input0 - yState[2]) * pCoefficients[2] + tmp_0;
  27. input1 = (input1 - yState[3]) * pCoefficients[3] + tmp_1;
  28. tmp_0 = yState[2];
  29. tmp_1 = yState[3];
  30. yState[2] = input0;
  31. yState[3] = input1;
  32. input0 = (input0 - yState[4]) * pCoefficients[4] + tmp_0;
  33. input1 = (input1 - yState[5]) * pCoefficients[5] + tmp_1;
  34. tmp_0 = yState[4];
  35. yState[4] = input0;
  36. yState[5] = input1;
  37. input0 = (input0 - yState[6]) * pCoefficients[6] + tmp_0;
  38. yState[6] = input0;
  39. return (input0 + input1);
  40. }
  41. static void ExtractFloatSamples( float *pOut, const short *pInputBuffer, int sampleCount, int stride )
  42. {
  43. for ( int i = 0; i < sampleCount; i++ )
  44. {
  45. pOut[i] = pInputBuffer[0] * 1.0f / 32768.0f;
  46. pInputBuffer += stride;
  47. }
  48. }
  49. static void ExtractShortSamples( short *pOut, const float *pInputBuffer, float scale, int sampleCount, int stride )
  50. {
  51. for ( int i = 0; i < sampleCount; i++ )
  52. {
  53. int sampleOut = (int)(pInputBuffer[i] * scale);
  54. sampleOut = clamp( sampleOut, -32768, 32767 );
  55. pOut[0] = (short)(sampleOut);
  56. pOut += stride;
  57. }
  58. }
  59. struct decimatestate_t
  60. {
  61. float xState[2];
  62. float yState[7];
  63. };
  64. void DecimateSampleBlock( float *pInOut, int sampleCount )
  65. {
  66. decimatestate_t block;
  67. int outCount = sampleCount >> 1;
  68. int pos = 0;
  69. memset( &block, 0, sizeof(block) );
  70. do
  71. {
  72. float input0 = pInOut[pos*2+0];
  73. float input1 = pInOut[pos*2+1];
  74. pInOut[pos] = DecimateSamplePair( input0, input1, g_ResampleCoefficients, block.xState, block.yState );
  75. pos++;
  76. } while( pos < outCount );
  77. }
  78. void DecimateSampleRateBy2_16( const short *pInputBuffer, short *pOutputBuffer, int sampleCount, int channelCount )
  79. {
  80. float *pTmpBuf = new float[sampleCount];
  81. for ( int i = 0; i < channelCount; i++ )
  82. {
  83. ExtractFloatSamples( pTmpBuf, pInputBuffer+i, sampleCount, channelCount );
  84. DecimateSampleBlock( pTmpBuf, sampleCount );
  85. ExtractShortSamples( pOutputBuffer+i, pTmpBuf, 0.5f * 32768.0f, sampleCount>>1, channelCount );
  86. }
  87. delete [] pTmpBuf;
  88. }
  89. struct adpcmstate_t
  90. {
  91. const ADPCMWAVEFORMAT *pFormat;
  92. const ADPCMCOEFSET *pCoefficients;
  93. int blockSize;
  94. };
  95. static int error_sign_lut[] = { 0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1 };
  96. static int error_coefficients_lut[] = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 };
  97. void ParseADPCM( adpcmstate_t &out, const byte *pFormatChunk )
  98. {
  99. out.pFormat = (const ADPCMWAVEFORMAT *)pFormatChunk;
  100. if ( out.pFormat )
  101. {
  102. out.pCoefficients = out.pFormat->aCoef;
  103. // number of bytes for samples
  104. out.blockSize = ((out.pFormat->wSamplesPerBlock - 2) * out.pFormat->wfx.nChannels ) / 2;
  105. // size of channel header
  106. out.blockSize += 7 * out.pFormat->wfx.nChannels;
  107. }
  108. }
  109. void DecompressBlockMono( const adpcmstate_t &state, short *pOut, const char *pIn, int count )
  110. {
  111. int pred = *pIn++;
  112. int co1 = state.pCoefficients[pred].iCoef1;
  113. int co2 = state.pCoefficients[pred].iCoef2;
  114. // read initial delta
  115. int delta = *((short *)pIn);
  116. pIn += 2;
  117. // read initial samples for prediction
  118. int samp1 = *((short *)pIn);
  119. pIn += 2;
  120. int samp2 = *((short *)pIn);
  121. pIn += 2;
  122. // write out the initial samples (stored in reverse order)
  123. *pOut++ = (short)samp2;
  124. *pOut++ = (short)samp1;
  125. // subtract the 2 samples in the header
  126. count -= 2;
  127. // this is a toggle to read nibbles, first nibble is high
  128. int high = 1;
  129. int error, sample=0;
  130. // now process the block
  131. while ( count )
  132. {
  133. // read the error nibble from the input stream
  134. if ( high )
  135. {
  136. sample = (unsigned char) (*pIn++);
  137. // high nibble
  138. error = sample >> 4;
  139. // cache low nibble for next read
  140. sample = sample & 0xf;
  141. // Next read is from cache, not stream
  142. high = 0;
  143. }
  144. else
  145. {
  146. // stored in previous read (low nibble)
  147. error = sample;
  148. // next read is from stream
  149. high = 1;
  150. }
  151. // convert to signed with LUT
  152. int errorSign = error_sign_lut[error];
  153. // interpolate the new sample
  154. int predSample = (samp1 * co1) + (samp2 * co2);
  155. // coefficients are fixed point 8-bit, so shift back to 16-bit integer
  156. predSample >>= 8;
  157. // Add in current error estimate
  158. predSample += (errorSign * delta);
  159. // Correct error estimate
  160. delta = (delta * error_coefficients_lut[error]) >> 8;
  161. // Clamp error estimate
  162. if ( delta < 16 )
  163. delta = 16;
  164. // clamp
  165. if ( predSample > 32767L )
  166. predSample = 32767L;
  167. else if ( predSample < -32768L )
  168. predSample = -32768L;
  169. // output
  170. *pOut++ = (short)predSample;
  171. // move samples over
  172. samp2 = samp1;
  173. samp1 = predSample;
  174. count--;
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Decode a single block of stereo ADPCM audio
  179. // Input : *pOut - 16-bit output buffer
  180. // *pIn - ADPCM encoded block data
  181. // count - number of sample pairs to decode
  182. //-----------------------------------------------------------------------------
  183. void DecompressBlockStereo( const adpcmstate_t &state, short *pOut, const char *pIn, int count )
  184. {
  185. int pred[2], co1[2], co2[2];
  186. int i;
  187. for ( i = 0; i < 2; i++ )
  188. {
  189. pred[i] = *pIn++;
  190. co1[i] = state.pCoefficients[pred[i]].iCoef1;
  191. co2[i] = state.pCoefficients[pred[i]].iCoef2;
  192. }
  193. int delta[2], samp1[2], samp2[2];
  194. for ( i = 0; i < 2; i++, pIn += 2 )
  195. {
  196. // read initial delta
  197. delta[i] = *((short *)pIn);
  198. }
  199. // read initial samples for prediction
  200. for ( i = 0; i < 2; i++, pIn += 2 )
  201. {
  202. samp1[i] = *((short *)pIn);
  203. }
  204. for ( i = 0; i < 2; i++, pIn += 2 )
  205. {
  206. samp2[i] = *((short *)pIn);
  207. }
  208. // write out the initial samples (stored in reverse order)
  209. *pOut++ = (short)samp2[0]; // left
  210. *pOut++ = (short)samp2[1]; // right
  211. *pOut++ = (short)samp1[0]; // left
  212. *pOut++ = (short)samp1[1]; // right
  213. // subtract the 2 samples in the header
  214. count -= 2;
  215. // this is a toggle to read nibbles, first nibble is high
  216. int high = 1;
  217. int error, sample=0;
  218. // now process the block
  219. while ( count )
  220. {
  221. for ( i = 0; i < 2; i++ )
  222. {
  223. // read the error nibble from the input stream
  224. if ( high )
  225. {
  226. sample = (unsigned char) (*pIn++);
  227. // high nibble
  228. error = sample >> 4;
  229. // cache low nibble for next read
  230. sample = sample & 0xf;
  231. // Next read is from cache, not stream
  232. high = 0;
  233. }
  234. else
  235. {
  236. // stored in previous read (low nibble)
  237. error = sample;
  238. // next read is from stream
  239. high = 1;
  240. }
  241. // convert to signed with LUT
  242. int errorSign = error_sign_lut[error];
  243. // interpolate the new sample
  244. int predSample = (samp1[i] * co1[i]) + (samp2[i] * co2[i]);
  245. // coefficients are fixed point 8-bit, so shift back to 16-bit integer
  246. predSample >>= 8;
  247. // Add in current error estimate
  248. predSample += (errorSign * delta[i]);
  249. // Correct error estimate
  250. delta[i] = (delta[i] * error_coefficients_lut[error]) >> 8;
  251. // Clamp error estimate
  252. if ( delta[i] < 16 )
  253. delta[i] = 16;
  254. // clamp
  255. if ( predSample > 32767L )
  256. predSample = 32767L;
  257. else if ( predSample < -32768L )
  258. predSample = -32768L;
  259. // output
  260. *pOut++ = (short)predSample;
  261. // move samples over
  262. samp2[i] = samp1[i];
  263. samp1[i] = predSample;
  264. }
  265. count--;
  266. }
  267. }
  268. int ADPCMSampleCountShortBlock( const adpcmstate_t &state, int shortBlockSize )
  269. {
  270. if ( shortBlockSize < 8 )
  271. return 0;
  272. int sampleCount = state.pFormat->wSamplesPerBlock;
  273. // short block?, fixup sample count (2 samples per byte, divided by number of channels per sample set)
  274. sampleCount -= ((state.blockSize - shortBlockSize) * 2) / state.pFormat->wfx.nChannels;
  275. return sampleCount;
  276. }
  277. int ADPCMSampleCount( const byte *pFormatChunk, const byte *pDataChunk, int dataSize )
  278. {
  279. adpcmstate_t state;
  280. ParseADPCM( state, pFormatChunk );
  281. int numBlocks = dataSize / state.blockSize;
  282. int mod = dataSize % state.blockSize;
  283. return numBlocks * state.pFormat->wSamplesPerBlock + ADPCMSampleCountShortBlock(state, mod);
  284. }
  285. void DecompressADPCMSamples( const byte *pFormatChunk, const byte *pDataChunk, int dataSize, short *pOutputBuffer )
  286. {
  287. adpcmstate_t state;
  288. ParseADPCM( state, pFormatChunk );
  289. while ( dataSize > 0 )
  290. {
  291. int block = dataSize;
  292. int sampleCount = state.pFormat->wSamplesPerBlock;
  293. if ( block > state.blockSize )
  294. {
  295. block = state.blockSize;
  296. }
  297. else
  298. {
  299. sampleCount = ADPCMSampleCountShortBlock( state, block );
  300. }
  301. if ( state.pFormat->wfx.nChannels == 1 )
  302. {
  303. DecompressBlockMono( state, pOutputBuffer, (const char *)pDataChunk, sampleCount );
  304. }
  305. else
  306. {
  307. DecompressBlockStereo( state, pOutputBuffer, (const char *)pDataChunk, sampleCount );
  308. }
  309. pOutputBuffer += sampleCount * state.pFormat->wfx.nChannels;
  310. dataSize -= block;
  311. pDataChunk += block;
  312. }
  313. }
  314. void Convert8To16( const byte *pInputBuffer, short *pOutputBuffer, int sampleCount, int channelCount )
  315. {
  316. for ( int i = 0; i < sampleCount*channelCount; i++ )
  317. {
  318. unsigned short signedSample = (byte)((int)((unsigned)pInputBuffer[i]) - 128);
  319. pOutputBuffer[i] = (short) (signedSample | (signedSample<<8));
  320. }
  321. }