AUDZAP.CPP 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /****************************************************************************
  19. *
  20. * C O N F I D E N T I A L --- W E S T W O O D S T U D I O S
  21. *
  22. *----------------------------------------------------------------------------
  23. *
  24. * FILE
  25. * Audzap.c
  26. *
  27. * DESCRIPTION
  28. * Lossy audio compression. (32-Bit protected mode)
  29. *
  30. * PROGRAMMER
  31. * Joe L. Bostic
  32. *
  33. * DATE
  34. * January 26, 1995
  35. *
  36. *----------------------------------------------------------------------------
  37. *
  38. * PUBLIC
  39. * AudioZap - Compress audio sample data.
  40. *
  41. ****************************************************************************/
  42. #include <stdio.h>
  43. #include <mem.h>
  44. #include "compress.h"
  45. /*---------------------------------------------------------------------------
  46. * PRIVATE DECLARATIONS
  47. *-------------------------------------------------------------------------*/
  48. #define MIN(a,b) (((a)<(b)) ? (a) : (b))
  49. #define MAX(a,b) (((a)>(b)) ? (a) : (b))
  50. typedef enum {
  51. CODE_2BIT, /* Bit packed 2 bit delta. */
  52. CODE_4BIT, /* Nibble packed 4 bit delta. */
  53. CODE_RAW, /* Raw sample. */
  54. CODE_SILENCE /* Run of silence. */
  55. } SCodeType;
  56. char _2bitencode[5] = {
  57. 0,1,2,3
  58. };
  59. long _2bitdecode[4] = {
  60. -2,-1,0,1
  61. };
  62. char _4bitencode[19] = {
  63. 0,1,2,2,3,4,5,6,7,(8),
  64. 8,9,10,11,12,13,13,14,15
  65. };
  66. long _4bitdecode[16] = {
  67. -9,-8,-6,-5,-4,-3,-2,
  68. -1,0,1,2,3,4,5,6,8
  69. };
  70. /****************************************************************************
  71. *
  72. * NAME
  73. * AudioZap - Compress audio sample data.
  74. *
  75. * SYNOPSIS
  76. * Size = AudioZap(Source, Dest, Size)
  77. *
  78. * long AudioZap(void *, void *, long);
  79. *
  80. * FUNCTION
  81. * NOTE - If the compressed size is equal to or greater than the original
  82. * size then the data could not be compressed and the uncompressed data
  83. * should be written.
  84. *
  85. * INPUTS
  86. * Source - Pointer to buffer containing audio sample data.
  87. * Dest - Pointer to buffer to put encoded data.
  88. * Size - Number of bytes to compress.
  89. *
  90. * RESULT
  91. * Size - Size in bytes of encoded data.
  92. *
  93. ****************************************************************************/
  94. long AudioZap(void *source, void *dest, long size)
  95. {
  96. unsigned char *s = (unsigned char *)source;
  97. unsigned char *d = (unsigned char *)dest;
  98. long delta;
  99. unsigned long previous = 0x80;
  100. long outcount = 0;
  101. unsigned char *s4;
  102. unsigned long p4;
  103. long max4;
  104. unsigned char *lastraw = 0;
  105. long osize = size;
  106. unsigned long i;
  107. unsigned long dd;
  108. /* Reduce very small amplitudes to silence. Usually a rather large
  109. * portion of a sample is hovering around the silence value. This is
  110. * due, in part, to the artifacting of the sample process. These low
  111. * amplitudes are not audible.
  112. */
  113. max4 = size;
  114. s = (unsigned char *)source;
  115. while (size > 0 && outcount < osize) {
  116. /* First check for runs of zero deltas. If a run of at least
  117. * any can be found, then output it.
  118. */
  119. s4 = s;
  120. max4 = MIN(63 + 1, size);
  121. for (i = 0; i < max4; i++) {
  122. if (previous != *s4++)
  123. break;
  124. }
  125. /* When there is a code transition, terminate any run of raw
  126. * samples.
  127. */
  128. if (i > 2) {
  129. lastraw = 0;
  130. *d++ = ((i - 1)|(CODE_SILENCE << 6));
  131. outcount++;
  132. s += i;
  133. size -= i;
  134. continue;
  135. }
  136. /* If there are fewer than 4 samples remaining, then using delta
  137. * compression is inefficient. Just drop into the raw routine
  138. */
  139. if (size > 4) {
  140. s4 = s;
  141. p4 = previous;
  142. /* Find out the number of lossless 2 bit deltas available. These
  143. * deltas are always present in quads. The compressed code is
  144. * the delta quad count followed by the deltas in bit packed bytes.
  145. */
  146. max4 = MIN(64L * 4L + 4L + 4L, size);
  147. for (i = 0; i < max4; i++) {
  148. delta = (((int)*s4++) - p4);
  149. if ((delta < -2) || (delta > 1)) {
  150. break;
  151. }
  152. p4 += _2bitdecode[_2bitencode[delta + 2]];
  153. if (((signed)p4) < 0) {
  154. p4 = 0;
  155. }
  156. if (((signed)p4) > 255) {
  157. p4 = 255;
  158. }
  159. }
  160. i >>= 2; // Delta 2 always occur in quads -- force this.
  161. /* If there is the minimum benificial number of delta 2s available,
  162. * then compress them.
  163. */
  164. if (i) {
  165. /* When there is a code transition, terminate any run of raw
  166. * samples.
  167. */
  168. lastraw = 0;
  169. /* Output the delta 4 pair count. This is the number of pairs
  170. * minus the 'free' two pairs already assumed to be there.
  171. */
  172. i = MIN(i, (63 + 1));
  173. *d++ = ((i - 1)|(CODE_2BIT << 6));
  174. outcount++;
  175. for (dd = 0; dd < i; dd++) {
  176. int delta1, delta2, delta3, delta4;
  177. delta1 = _2bitencode[((((int)*s++) - previous) + 2)];
  178. previous += _2bitdecode[delta1];
  179. if (((signed)previous) < 0) {
  180. previous = 0;
  181. }
  182. if (((signed)previous) > 255) {
  183. previous = 255;
  184. }
  185. size--;
  186. delta2 = _2bitencode[((((int)*s++) - previous) + 2)];
  187. previous += _2bitdecode[delta2];
  188. if (((signed)previous) < 0) {
  189. previous = 0;
  190. }
  191. if (((signed)previous) > 255) {
  192. previous = 255;
  193. }
  194. size--;
  195. delta3 = _2bitencode[((((int)*s++) - previous) + 2)];
  196. previous += _2bitdecode[delta3];
  197. if (((signed)previous) < 0) {
  198. previous = 0;
  199. }
  200. if (((signed)previous) > 255) {
  201. previous = 255;
  202. }
  203. size--;
  204. delta4 = _2bitencode[((((int)*s++) - previous) + 2)];
  205. previous += _2bitdecode[delta4];
  206. if (((signed)previous) < 0) {
  207. previous = 0;
  208. }
  209. if (((signed)previous) > 255) {
  210. previous = 255;
  211. }
  212. size--;
  213. *d++ = ((delta4 << 6)|(delta3 << 4)|(delta2 << 2)|delta1);
  214. outcount++;
  215. }
  216. continue;
  217. } else {
  218. s4 = s;
  219. p4 = previous;
  220. /* Find out the number of lossless 4 bit deltas follow. These
  221. * deltas are always present in pairs. The compressed code is
  222. * the delta pair count followed by the deltas in nibble packed
  223. * bytes.
  224. */
  225. max4 = MIN(64L * 2L + 4L + 4L, size);
  226. for (i = 0; i < max4; i++) {
  227. delta = (((int)*s4++) - p4);
  228. if (delta < -9 || delta >= 9) {
  229. break;
  230. }
  231. p4 += _4bitdecode[_4bitencode[(delta + 9)]];
  232. if (((signed)p4) < 0) {
  233. p4 = 0;
  234. }
  235. if (((signed)p4) > 255) {
  236. p4 = 255;
  237. }
  238. }
  239. i >>= 1; // Delta 4 always occur in pairs -- force this.
  240. /* If there is the minimum benificial number of delta 4s available,
  241. * then compress them.
  242. */
  243. if (i) {
  244. /* When there is a code transition, terminate any run of raw
  245. * samples.
  246. */
  247. lastraw = 0;
  248. /* Output the delta 4 pair count. This is the number of pairs
  249. * minus the 'free' two pairs already assumed to be there.
  250. */
  251. i = MIN(i, (63 + 1));
  252. *d++ = ((i - 1)|(CODE_4BIT << 6));
  253. outcount++;
  254. for (dd = 0; dd < i; dd++) {
  255. int delta1, delta2;
  256. delta1 = _4bitencode[((((int)*s++) - previous) + 9)];
  257. previous += _4bitdecode[delta1];
  258. if (((signed)previous) < 0) {
  259. previous = 0;
  260. }
  261. if (((signed)previous) > 255) {
  262. previous = 255;
  263. }
  264. size--;
  265. delta2 = _4bitencode[((((int)*s++) - previous) + 9)];
  266. previous += _4bitdecode[delta2];
  267. if (((signed)previous) < 0) {
  268. previous = 0;
  269. }
  270. if (((signed)previous) > 255) {
  271. previous = 255;
  272. }
  273. size--;
  274. *d++ = ((delta2 << 4)|(delta1 & 0x0F));
  275. outcount++;
  276. }
  277. continue;
  278. }
  279. }
  280. }
  281. /* Raw output since deltas were unsuccessful. If this is a run
  282. * of raw output, then merely tack it onto the run rather than
  283. * create a new code sequence.
  284. */
  285. if (lastraw) {
  286. *lastraw = ((*lastraw) + 1);
  287. /* There is only so much a run code can accomodate. If the limit
  288. * has been reached, then terminate this code. A new one will be
  289. * created if necessary.
  290. */
  291. if ((*lastraw & 0x1F) == 0x1F) {
  292. lastraw = 0;
  293. }
  294. } else {
  295. /* If there is no current raw dump of samples, then check to see if
  296. * this sample can fit into a 5 bit delta. If it can, then store
  297. * it as such as a parasite to the "raw" code. This will save a byte
  298. * for any stray 5 bit deltas that happen along. It is expected that
  299. * this is more frequent than 6 or more bit deltas that would necessitate
  300. * the use of the RAW code.
  301. */
  302. delta = (((int)*s) - previous);
  303. if ((delta >= -16) && (delta <= 15)) {
  304. lastraw = 0;
  305. *d++ = ((CODE_RAW << 6)|0x20|(delta & 0x1F));
  306. outcount++;
  307. previous = *s++;
  308. size--;
  309. continue;
  310. } else {
  311. lastraw = d;
  312. *d++ = (CODE_RAW << 6);
  313. outcount++;
  314. }
  315. }
  316. *d++ = previous = *s++;
  317. size--;
  318. outcount++;
  319. }
  320. /* Check to see if the compression process actually resulted in smaller
  321. * data size. In some cases, the 'compressed' data is actually larger. In
  322. * this case, just output the raw frame. If the compressed and actual frame
  323. * size match, then it is presumed that no compression occurs.
  324. */
  325. if (outcount >= osize) {
  326. memcpy(dest, source, (size_t)osize);
  327. outcount = osize;
  328. }
  329. return(outcount);
  330. }