1 #include "All.h" 2 #include "Prepare.h" 3 4 5 const uint32 CRC32_TABLE[256] = {0u,1996959894u,3993919788u,2567524794u, 6 124634137u,1886057615u,3915621685u,2657392035u,249268274u,2044508324u, 7 3772115230u,2547177864u,162941995u,2125561021u,3887607047u,2428444049u, 8 498536548u,1789927666u,4089016648u,2227061214u,450548861u,1843258603u, 9 4107580753u,2211677639u,325883990u,1684777152u,4251122042u,2321926636u, 10 335633487u,1661365465u,4195302755u,2366115317u,997073096u,1281953886u, 11 3579855332u,2724688242u,1006888145u,1258607687u,3524101629u,2768942443u, 12 901097722u,1119000684u,3686517206u,2898065728u,853044451u,1172266101u, 13 3705015759u,2882616665u,651767980u,1373503546u,3369554304u,3218104598u, 14 565507253u,1454621731u,3485111705u,3099436303u,671266974u,1594198024u, 15 3322730930u,2970347812u,795835527u,1483230225u,3244367275u,3060149565u, 16 1994146192u,31158534u,2563907772u,4023717930u,1907459465u,112637215u, 17 2680153253u,3904427059u,2013776290u,251722036u,2517215374u,3775830040u, 18 2137656763u,141376813u,2439277719u,3865271297u,1802195444u,476864866u, 19 2238001368u,4066508878u,1812370925u,453092731u,2181625025u,4111451223u, 20 1706088902u,314042704u,2344532202u,4240017532u,1658658271u,366619977u, 21 2362670323u,4224994405u,1303535960u,984961486u,2747007092u,3569037538u, 22 1256170817u,1037604311u,2765210733u,3554079995u,1131014506u,879679996u, 23 2909243462u,3663771856u,1141124467u,855842277u,2852801631u,3708648649u, 24 1342533948u,654459306u,3188396048u,3373015174u,1466479909u,544179635u, 25 3110523913u,3462522015u,1591671054u,702138776u,2966460450u,3352799412u, 26 1504918807u,783551873u,3082640443u,3233442989u,3988292384u,2596254646u, 27 62317068u,1957810842u,3939845945u,2647816111u,81470997u,1943803523u, 28 3814918930u,2489596804u,225274430u,2053790376u,3826175755u,2466906013u, 29 167816743u,2097651377u,4027552580u,2265490386u,503444072u,1762050814u, 30 4150417245u,2154129355u,426522225u,1852507879u,4275313526u,2312317920u, 31 282753626u,1742555852u,4189708143u,2394877945u,397917763u,1622183637u, 32 3604390888u,2714866558u,953729732u,1340076626u,3518719985u,2797360999u, 33 1068828381u,1219638859u,3624741850u,2936675148u,906185462u,1090812512u, 34 3747672003u,2825379669u,829329135u,1181335161u,3412177804u,3160834842u, 35 628085408u,1382605366u,3423369109u,3138078467u,570562233u,1426400815u, 36 3317316542u,2998733608u,733239954u,1555261956u,3268935591u,3050360625u, 37 752459403u,1541320221u,2607071920u,3965973030u,1969922972u,40735498u, 38 2617837225u,3943577151u,1913087877u,83908371u,2512341634u,3803740692u, 39 2075208622u,213261112u,2463272603u,3855990285u,2094854071u,198958881u, 40 2262029012u,4057260610u,1759359992u,534414190u,2176718541u,4139329115u, 41 1873836001u,414664567u,2282248934u,4279200368u,1711684554u,285281116u, 42 2405801727u,4167216745u,1634467795u,376229701u,2685067896u,3608007406u, 43 1308918612u,956543938u,2808555105u,3495958263u,1231636301u,1047427035u, 44 2932959818u,3654703836u,1088359270u,936918000u,2847714899u,3736837829u, 45 1202900863u,817233897u,3183342108u,3401237130u,1404277552u,615818150u, 46 3134207493u,3453421203u,1423857449u,601450431u,3009837614u,3294710456u, 47 1567103746u,711928724u,3020668471u,3272380065u,1510334235u,755167117u}; 48 49 50 int 51 CPrepare::Prepare(const unsigned char* pRawData, int nBytes, 52 const WAVEFORMATEX* pWaveFormatEx, int* pOutputX, int* pOutputY, 53 unsigned int* pCRC, int* pSpecialCodes, int* pPeakLevel) 54 { 55 // error check the parameters 56 if (pRawData == NULL || pWaveFormatEx == NULL) 57 return ERROR_BAD_PARAMETER; 58 59 // initialize the pointers that got passed in 60 *pCRC = 0xFFFFFFFF; 61 *pSpecialCodes = 0; 62 63 // variables 64 uint32 CRC = 0xFFFFFFFF; 65 const int nTotalBlocks = nBytes / pWaveFormatEx->nBlockAlign; 66 int R,L; 67 68 // the prepare code 69 if (pWaveFormatEx->wBitsPerSample == 8) { 70 if (pWaveFormatEx->nChannels == 2) { 71 for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; 72 nBlockIndex++) { 73 74 R = (int) (*((unsigned char *) pRawData) - 128); 75 L = (int) (*((unsigned char *) (pRawData + 1)) - 128); 76 77 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 78 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 79 80 // check the peak 81 if (labs(L) > *pPeakLevel) 82 *pPeakLevel = labs(L); 83 if (labs(R) > *pPeakLevel) 84 *pPeakLevel = labs(R); 85 86 // convert to x,y 87 pOutputY[nBlockIndex] = L - R; 88 pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2); 89 } 90 } else if (pWaveFormatEx->nChannels == 1) { 91 for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; 92 nBlockIndex++) { 93 94 R = (int) (*((unsigned char *) pRawData) - 128); 95 96 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 97 98 // check the peak 99 if (labs(R) > *pPeakLevel) 100 *pPeakLevel = labs(R); 101 102 // convert to x,y 103 pOutputX[nBlockIndex] = R; 104 } 105 } 106 } else if (pWaveFormatEx->wBitsPerSample == 24) { 107 if (pWaveFormatEx->nChannels == 2) { 108 for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) { 109 uint32 nTemp = 0; 110 111 nTemp |= (*pRawData << 0); 112 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 113 114 nTemp |= (*pRawData << 8); 115 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 116 117 nTemp |= (*pRawData << 16); 118 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 119 120 if (nTemp & 0x800000) 121 R = (int) (nTemp & 0x7FFFFF) - 0x800000; 122 else 123 R = (int) (nTemp & 0x7FFFFF); 124 125 nTemp = 0; 126 127 nTemp |= (*pRawData << 0); 128 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 129 130 nTemp |= (*pRawData << 8); 131 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 132 133 nTemp |= (*pRawData << 16); 134 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 135 136 if (nTemp & 0x800000) 137 L = (int) (nTemp & 0x7FFFFF) - 0x800000; 138 else 139 L = (int) (nTemp & 0x7FFFFF); 140 141 // check the peak 142 if (labs(L) > *pPeakLevel) 143 *pPeakLevel = labs(L); 144 if (labs(R) > *pPeakLevel) 145 *pPeakLevel = labs(R); 146 147 // convert to x,y 148 pOutputY[nBlockIndex] = L - R; 149 pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2); 150 } 151 } else if (pWaveFormatEx->nChannels == 1) { 152 for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; 153 nBlockIndex++) { 154 155 uint32 nTemp = 0; 156 157 nTemp |= (*pRawData << 0); 158 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 159 160 nTemp |= (*pRawData << 8); 161 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 162 163 nTemp |= (*pRawData << 16); 164 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 165 166 if (nTemp & 0x800000) 167 R = (int) (nTemp & 0x7FFFFF) - 0x800000; 168 else 169 R = (int) (nTemp & 0x7FFFFF); 170 171 // check the peak 172 if (labs(R) > *pPeakLevel) 173 *pPeakLevel = labs(R); 174 175 // convert to x,y 176 pOutputX[nBlockIndex] = R; 177 } 178 } 179 } else { 180 if (pWaveFormatEx->nChannels == 2) { 181 int LPeak = 0; 182 int RPeak = 0; 183 int nBlockIndex = 0; 184 for (nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) { 185 R = (int) *((int16 *) pRawData); 186 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 187 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 188 189 L = (int) *((int16 *) pRawData); 190 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 191 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 192 193 // check the peak 194 if (labs(L) > LPeak) 195 LPeak = labs(L); 196 if (labs(R) > RPeak) 197 RPeak = labs(R); 198 199 // convert to x,y 200 pOutputY[nBlockIndex] = L - R; 201 pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2); 202 } 203 204 if (LPeak == 0) 205 *pSpecialCodes |= SPECIAL_FRAME_LEFT_SILENCE; 206 if (RPeak == 0) 207 *pSpecialCodes |= SPECIAL_FRAME_RIGHT_SILENCE; 208 if (max(LPeak, RPeak) > *pPeakLevel) 209 *pPeakLevel = max(LPeak, RPeak); 210 211 // check for pseudo-stereo files 212 nBlockIndex = 0; 213 while (pOutputY[nBlockIndex++] == 0) { 214 if (nBlockIndex == (nBytes / 4)) { 215 *pSpecialCodes |= SPECIAL_FRAME_PSEUDO_STEREO; 216 break; 217 } 218 } 219 } else if (pWaveFormatEx->nChannels == 1) { 220 int nPeak = 0; 221 for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; 222 nBlockIndex++) { 223 224 R = (int) *((int16 *) pRawData); 225 226 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 227 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *pRawData++]; 228 229 // check the peak 230 if (labs(R) > nPeak) 231 nPeak = labs(R); 232 233 //convert to x,y 234 pOutputX[nBlockIndex] = R; 235 } 236 237 if (nPeak > *pPeakLevel) 238 *pPeakLevel = nPeak; 239 240 if (nPeak == 0) 241 *pSpecialCodes |= SPECIAL_FRAME_MONO_SILENCE; 242 } 243 } 244 245 CRC = CRC ^ 0xFFFFFFFF; 246 247 // add the special code 248 CRC >>= 1; 249 250 if (*pSpecialCodes != 0) 251 CRC |= (1 << 31); 252 253 *pCRC = CRC; 254 255 return ERROR_SUCCESS; 256 } 257 258 259 void 260 CPrepare::Unprepare(int X, int Y, const WAVEFORMATEX* pWaveFormatEx, 261 unsigned char* pOutput, unsigned int* pCRC) 262 { 263 #define CALCULATE_CRC_BYTE *pCRC = (*pCRC >> 8) ^ CRC32_TABLE[(*pCRC & 0xFF) ^ *pOutput++]; 264 265 // decompress and convert from (x,y) -> (l,r) 266 // sort of long and ugly.... sorry 267 268 if (pWaveFormatEx->nChannels == 2) { 269 if (pWaveFormatEx->wBitsPerSample == 16) { 270 // get the right and left values 271 int nR = X - (Y / 2); 272 int nL = nR + Y; 273 274 // error check (for overflows) 275 if ((nR < -32768) || (nR > 32767) || (nL < -32768) || (nL > 32767)) 276 throw(-1); 277 278 *(int16 *) pOutput = (int16) nR; 279 CALCULATE_CRC_BYTE 280 CALCULATE_CRC_BYTE 281 282 *(int16 *) pOutput = (int16) nL; 283 CALCULATE_CRC_BYTE 284 CALCULATE_CRC_BYTE 285 } else if (pWaveFormatEx->wBitsPerSample == 8) { 286 unsigned char R = (X - (Y / 2) + 128); 287 *pOutput = R; 288 CALCULATE_CRC_BYTE 289 *pOutput = (unsigned char) (R + Y); 290 CALCULATE_CRC_BYTE 291 } else if (pWaveFormatEx->wBitsPerSample == 24) { 292 int32 RV, LV; 293 294 RV = X - (Y / 2); 295 LV = RV + Y; 296 297 uint32 nTemp = 0; 298 if (RV < 0) 299 nTemp = ((uint32) (RV + 0x800000)) | 0x800000; 300 else 301 nTemp = (uint32) RV; 302 303 *pOutput = (unsigned char) ((nTemp >> 0) & 0xFF); 304 CALCULATE_CRC_BYTE 305 *pOutput = (unsigned char) ((nTemp >> 8) & 0xFF); 306 CALCULATE_CRC_BYTE 307 *pOutput = (unsigned char) ((nTemp >> 16) & 0xFF); 308 CALCULATE_CRC_BYTE 309 310 nTemp = 0; 311 if (LV < 0) 312 nTemp = ((uint32) (LV + 0x800000)) | 0x800000; 313 else 314 nTemp = (uint32) LV; 315 316 *pOutput = (unsigned char) ((nTemp >> 0) & 0xFF); 317 CALCULATE_CRC_BYTE 318 319 *pOutput = (unsigned char) ((nTemp >> 8) & 0xFF); 320 CALCULATE_CRC_BYTE 321 322 *pOutput = (unsigned char) ((nTemp >> 16) & 0xFF); 323 CALCULATE_CRC_BYTE 324 } 325 } else if (pWaveFormatEx->nChannels == 1) { 326 if (pWaveFormatEx->wBitsPerSample == 16) { 327 int16 R = X; 328 329 *(int16 *) pOutput = (int16) R; 330 CALCULATE_CRC_BYTE 331 CALCULATE_CRC_BYTE 332 } else if (pWaveFormatEx->wBitsPerSample == 8) { 333 unsigned char R = X + 128; 334 *pOutput = R; 335 CALCULATE_CRC_BYTE 336 } else if (pWaveFormatEx->wBitsPerSample == 24) { 337 int32 RV = X; 338 uint32 nTemp = 0; 339 340 if (RV < 0) 341 nTemp = ((uint32) (RV + 0x800000)) | 0x800000; 342 else 343 nTemp = (uint32) RV; 344 345 *pOutput = (unsigned char) ((nTemp >> 0) & 0xFF); 346 CALCULATE_CRC_BYTE 347 *pOutput = (unsigned char) ((nTemp >> 8) & 0xFF); 348 CALCULATE_CRC_BYTE 349 *pOutput = (unsigned char) ((nTemp >> 16) & 0xFF); 350 CALCULATE_CRC_BYTE 351 } 352 } 353 } 354 355 356 #ifdef BACKWARDS_COMPATIBILITY 357 int 358 CPrepare::UnprepareOld(int* pInputX, int* pInputY, int nBlocks, 359 const WAVEFORMATEX* pWaveFormatEx, unsigned char* pRawData, 360 unsigned int* pCRC, int* pSpecialCodes, int nFileVersion) 361 { 362 // the CRC that will be figured during decompression 363 uint32 CRC = 0xFFFFFFFF; 364 365 // decompress and convert from (x,y) -> (l,r) 366 // sort of int and ugly.... sorry 367 if (pWaveFormatEx->nChannels == 2) { 368 // convert the x,y data to raw data 369 if (pWaveFormatEx->wBitsPerSample == 16) { 370 int16 R; 371 unsigned char *Buffer = &pRawData[0]; 372 int *pX = pInputX; 373 int *pY = pInputY; 374 375 for (; pX < &pInputX[nBlocks]; pX++, pY++) { 376 R = *pX - (*pY / 2); 377 378 *(int16 *) Buffer = (int16) R; 379 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 380 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 381 382 *(int16 *) Buffer = (int16) R + *pY; 383 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 384 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 385 } 386 } else if (pWaveFormatEx->wBitsPerSample == 8) { 387 unsigned char *R = (unsigned char *) &pRawData[0]; 388 unsigned char *L = (unsigned char *) &pRawData[1]; 389 390 if (nFileVersion > 3830) { 391 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++, 392 L+=2, R+=2) { 393 394 *R = (unsigned char) (pInputX[SampleIndex] 395 - (pInputY[SampleIndex] / 2) + 128); 396 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *R]; 397 *L = (unsigned char) (*R + pInputY[SampleIndex]); 398 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *L]; 399 } 400 } else { 401 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++, 402 L+=2, R+=2) { 403 404 *R = (unsigned char) (pInputX[SampleIndex] 405 - (pInputY[SampleIndex] / 2)); 406 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *R]; 407 *L = (unsigned char) (*R + pInputY[SampleIndex]); 408 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *L]; 409 } 410 } 411 } else if (pWaveFormatEx->wBitsPerSample == 24) { 412 unsigned char *Buffer = (unsigned char *) &pRawData[0]; 413 int32 RV, LV; 414 415 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) { 416 RV = pInputX[SampleIndex] - (pInputY[SampleIndex] / 2); 417 LV = RV + pInputY[SampleIndex]; 418 419 uint32 nTemp = 0; 420 if (RV < 0) 421 nTemp = ((uint32) (RV + 0x800000)) | 0x800000; 422 else 423 nTemp = (uint32) RV; 424 425 *Buffer = (unsigned char) ((nTemp >> 0) & 0xFF); 426 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 427 428 *Buffer = (unsigned char) ((nTemp >> 8) & 0xFF); 429 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 430 431 *Buffer = (unsigned char) ((nTemp >> 16) & 0xFF); 432 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 433 434 nTemp = 0; 435 if (LV < 0) 436 nTemp = ((uint32) (LV + 0x800000)) | 0x800000; 437 else 438 nTemp = (uint32) LV; 439 440 *Buffer = (unsigned char) ((nTemp >> 0) & 0xFF); 441 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 442 443 *Buffer = (unsigned char) ((nTemp >> 8) & 0xFF); 444 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 445 446 *Buffer = (unsigned char) ((nTemp >> 16) & 0xFF); 447 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 448 } 449 } 450 } else if (pWaveFormatEx->nChannels == 1) { 451 // convert to raw data 452 if (pWaveFormatEx->wBitsPerSample == 8) { 453 unsigned char *R = (unsigned char *) &pRawData[0]; 454 455 if (nFileVersion > 3830) { 456 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++, 457 R++) { 458 459 *R = pInputX[SampleIndex] + 128; 460 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *R]; 461 } 462 } else { 463 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++, 464 R++) { 465 466 *R = (unsigned char) (pInputX[SampleIndex]); 467 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *R]; 468 } 469 } 470 } else if (pWaveFormatEx->wBitsPerSample == 24) { 471 unsigned char *Buffer = (unsigned char *) &pRawData[0]; 472 int32 RV; 473 for (int SampleIndex = 0; SampleIndex<nBlocks; SampleIndex++) { 474 RV = pInputX[SampleIndex]; 475 476 uint32 nTemp = 0; 477 if (RV < 0) 478 nTemp = ((uint32) (RV + 0x800000)) | 0x800000; 479 else 480 nTemp = (uint32) RV; 481 482 *Buffer = (unsigned char) ((nTemp >> 0) & 0xFF); 483 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 484 485 *Buffer = (unsigned char) ((nTemp >> 8) & 0xFF); 486 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 487 488 *Buffer = (unsigned char) ((nTemp >> 16) & 0xFF); 489 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 490 } 491 } else { 492 unsigned char *Buffer = &pRawData[0]; 493 494 for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) { 495 *(int16 *) Buffer = (int16) (pInputX[SampleIndex]); 496 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 497 CRC = (CRC >> 8) ^ CRC32_TABLE[(CRC & 0xFF) ^ *Buffer++]; 498 } 499 } 500 } 501 502 CRC = CRC ^ 0xFFFFFFFF; 503 504 *pCRC = CRC; 505 506 return 0; 507 } 508 #endif // #ifdef BACKWARDS_COMPATIBILITY 509