2 * wave.c -- this file contains decompression methods used by Storm.dll
3 * to decompress wave files.
5 * Copyright (C) 2003 Maik Broemme <mbroemme@plusserver.de>
7 * This source was adepted from the C++ version of wave.cpp included
8 * in stormlib. The C++ version belongs to the following authors,
10 * Ladislav Zezula <ladik.zezula.net>
11 * Tom Amigo <tomamigo@apexmail.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 /* Tables necessary dor decompression */
31 static unsigned long wave_table_1503f120
[] = {
32 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000006,
33 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007,
34 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007,
35 0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000006, 0xFFFFFFFF, 0x00000008
38 static unsigned long wave_table_1503f1a0
[] = {
39 0x00000007, 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E,
40 0x00000010, 0x00000011, 0x00000013, 0x00000015, 0x00000017, 0x00000019, 0x0000001C, 0x0000001F,
41 0x00000022, 0x00000025, 0x00000029, 0x0000002D, 0x00000032, 0x00000037, 0x0000003C, 0x00000042,
42 0x00000049, 0x00000050, 0x00000058, 0x00000061, 0x0000006B, 0x00000076, 0x00000082, 0x0000008F,
43 0x0000009D, 0x000000AD, 0x000000BE, 0x000000D1, 0x000000E6, 0x000000FD, 0x00000117, 0x00000133,
44 0x00000151, 0x00000173, 0x00000198, 0x000001C1, 0x000001EE, 0x00000220, 0x00000256, 0x00000292,
45 0x000002D4, 0x0000031C, 0x0000036C, 0x000003C3, 0x00000424, 0x0000048E, 0x00000502, 0x00000583,
46 0x00000610, 0x000006AB, 0x00000756, 0x00000812, 0x000008E0, 0x000009C3, 0x00000ABD, 0x00000BD0,
47 0x00000CFF, 0x00000E4C, 0x00000FBA, 0x0000114C, 0x00001307, 0x000014EE, 0x00001706, 0x00001954,
48 0x00001BDC, 0x00001EA5, 0x000021B6, 0x00002515, 0x000028CA, 0x00002CDF, 0x0000315B, 0x0000364B,
49 0x00003BB9, 0x000041B2, 0x00004844, 0x00004F7E, 0x00005771, 0x0000602F, 0x000069CE, 0x00007462,
54 * Decompress a wave file, mono or stereo
58 int libmpq_wave_decompress(unsigned char *out_buf
, int out_length
, unsigned char *in_buf
, int in_length
, int channels
) {
61 unsigned char *in_end
= in_buf
+ in_length
; /* End on input buffer */
74 for (count
= 0; count
< channels
; count
++) {
76 temp
= *(short *)in
.pw
++;
77 nr_array2
[count
] = temp
;
79 return out
.pb
- out_buf
;
81 *out
.pw
++ = (unsigned short)temp
;
85 while (in
.pb
< in_end
) {
86 unsigned char one_byte
= *in
.pb
++;
88 index
= (index
== 0) ? 1 : 0;
92 * Get one byte from input buffer
95 if (one_byte
& 0x80) {
97 switch(one_byte
& 0x7F) {
98 case 0: /* 15007B8E */
99 if (nr_array1
[index
] != 0) {
102 if (out_length
< 2) {
105 *out
.pw
++ = (unsigned short)nr_array2
[index
];
108 case 1: /* 15007B72 */
109 nr_array1
[index
] += 8; /* EBX also */
110 if (nr_array1
[index
] > 0x58) {
111 nr_array1
[index
] = 0x58;
114 index
= (index
== 0) ? 1 : 0;
120 nr_array1
[index
] -= 8;
121 if (nr_array1
[index
] < 0) {
122 nr_array1
[index
] = 0;
127 index
= (index
== 0) ? 1 : 0;
131 unsigned long temp1
= wave_table_1503f1a0
[nr_array1
[index
]]; /* EDI */
132 unsigned long temp2
= temp1
>> in_buf
[1]; /* ESI */
133 long temp3
= nr_array2
[index
]; /* ECX */
134 if (one_byte
& 0x01) { /* EBX = one_byte */
135 temp2
+= (temp1
>> 0);
137 if (one_byte
& 0x02) {
138 temp2
+= (temp1
>> 1);
140 if (one_byte
& 0x04) {
141 temp2
+= (temp1
>> 2);
143 if (one_byte
& 0x08) {
144 temp2
+= (temp1
>> 3);
146 if (one_byte
& 0x10) {
147 temp2
+= (temp1
>> 4);
149 if (one_byte
& 0x20) {
150 temp2
+= (temp1
>> 5);
152 if(one_byte
& 0x40) {
154 if (temp3
<= (long)0xFFFF8000) {
155 temp3
= (long)0xFFFF8000;
159 if (temp3
>= 0x7FFF) {
163 nr_array2
[index
] = temp3
;
164 if (out_length
< 2) {
168 temp2
= nr_array1
[index
];
170 *out
.pw
++ = (unsigned short)temp3
;
172 temp2
+= wave_table_1503f120
[one_byte
];
173 nr_array1
[index
] = temp2
;
175 if (nr_array1
[index
] < 0) {
176 nr_array1
[index
] = 0;
178 if (nr_array1
[index
] > 0x58) {
179 nr_array1
[index
] = 0x58;
184 return (out
.pb
- out_buf
);