ADPCM.CPP 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "function.h"
  19. extern "C" {
  20. #include "soscomp.h"
  21. #include "itable.cpp"
  22. #include "dtable.cpp"
  23. void sosCODECInitStream(_SOS_COMPRESS_INFO* info)
  24. {
  25. info->dwSampleIndex = 0;
  26. info->dwPredicted = 0;
  27. }
  28. unsigned long sosCODECDecompressData(_SOS_COMPRESS_INFO* info, unsigned long numbytes)
  29. {
  30. unsigned long token;
  31. long sample;
  32. unsigned int fastindex;
  33. unsigned char *inbuff;
  34. unsigned short *outbuff;
  35. inbuff = (unsigned char *)info->lpSource;
  36. outbuff = (unsigned short *)info->lpDest;
  37. // Preload variables before the big loop
  38. fastindex = (unsigned int)info->dwSampleIndex;
  39. sample = info->dwPredicted;
  40. if (!numbytes)
  41. goto SkipLoop;
  42. do {
  43. // First nibble
  44. token = *inbuff++;
  45. fastindex += token & 0x0f;
  46. sample += DiffTable[fastindex];
  47. fastindex = IndexTable[fastindex];
  48. if (sample > 32767L)
  49. sample = 32767L;
  50. if (sample < -32768L)
  51. sample = -32768L;
  52. *outbuff++ = (unsigned short)sample;
  53. // Second nibble
  54. fastindex += token >> 4;
  55. sample += DiffTable[fastindex];
  56. fastindex = IndexTable[fastindex];
  57. if (sample > 32767L)
  58. sample = 32767L;
  59. if (sample < -32768L)
  60. sample = -32768L;
  61. *outbuff++ = (unsigned short)sample;
  62. } while(--numbytes);
  63. SkipLoop:
  64. // Put local vars back
  65. info->dwSampleIndex = (unsigned long)fastindex;
  66. info->dwPredicted = sample;
  67. return(numbytes << 2);
  68. }
  69. }