ADPCM.CPP 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. #include "function.h"
  15. extern "C" {
  16. #include "soscomp.h"
  17. #include "itable.cpp"
  18. #include "dtable.cpp"
  19. void sosCODECInitStream(_SOS_COMPRESS_INFO* info)
  20. {
  21. info->dwSampleIndex = 0;
  22. info->dwPredicted = 0;
  23. }
  24. unsigned long sosCODECDecompressData(_SOS_COMPRESS_INFO* info, unsigned long numbytes)
  25. {
  26. unsigned long token;
  27. long sample;
  28. unsigned int fastindex;
  29. unsigned char *inbuff;
  30. unsigned short *outbuff;
  31. inbuff = (unsigned char *)info->lpSource;
  32. outbuff = (unsigned short *)info->lpDest;
  33. // Preload variables before the big loop
  34. fastindex = (unsigned int)info->dwSampleIndex;
  35. sample = info->dwPredicted;
  36. if (!numbytes)
  37. goto SkipLoop;
  38. do {
  39. // First nibble
  40. token = *inbuff++;
  41. fastindex += token & 0x0f;
  42. sample += DiffTable[fastindex];
  43. fastindex = IndexTable[fastindex];
  44. if (sample > 32767L)
  45. sample = 32767L;
  46. if (sample < -32768L)
  47. sample = -32768L;
  48. *outbuff++ = (unsigned short)sample;
  49. // Second nibble
  50. fastindex += token >> 4;
  51. sample += DiffTable[fastindex];
  52. fastindex = IndexTable[fastindex];
  53. if (sample > 32767L)
  54. sample = 32767L;
  55. if (sample < -32768L)
  56. sample = -32768L;
  57. *outbuff++ = (unsigned short)sample;
  58. } while(--numbytes);
  59. SkipLoop:
  60. // Put local vars back
  61. info->dwSampleIndex = (unsigned long)fastindex;
  62. info->dwPredicted = sample;
  63. return(numbytes << 2);
  64. }
  65. }