1 /* Extended Module Player
2 * Copyright (C) 1996-2018 Claudio Matsuoka and Hipolito Carraro Jr
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include "common.h"
24 #include "loader.h"
25
26 #ifndef LIBXMP_CORE_PLAYER
27
28 /*
29 * From the Audio File Formats (version 2.5)
30 * Submitted-by: Guido van Rossum <guido@cwi.nl>
31 * Last-modified: 27-Aug-1992
32 *
33 * The Acorn Archimedes uses a variation on U-LAW with the bit order
34 * reversed and the sign bit in bit 0. Being a 'minority' architecture,
35 * Arc owners are quite adept at converting sound/image formats from
36 * other machines, and it is unlikely that you'll ever encounter sound in
37 * one of the Arc's own formats (there are several).
38 */
39 static const int8 vdic_table[128] = {
40 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0,
41 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0,
42 /* 16 */ 0, 0, 0, 0, 0, 0, 0, 0,
43 /* 24 */ 1, 1, 1, 1, 1, 1, 1, 1,
44 /* 32 */ 1, 1, 1, 1, 2, 2, 2, 2,
45 /* 40 */ 2, 2, 2, 2, 3, 3, 3, 3,
46 /* 48 */ 3, 3, 4, 4, 4, 4, 5, 5,
47 /* 56 */ 5, 5, 6, 6, 6, 6, 7, 7,
48 /* 64 */ 7, 8, 8, 9, 9, 10, 10, 11,
49 /* 72 */ 11, 12, 12, 13, 13, 14, 14, 15,
50 /* 80 */ 15, 16, 17, 18, 19, 20, 21, 22,
51 /* 88 */ 23, 24, 25, 26, 27, 28, 29, 30,
52 /* 96 */ 31, 33, 34, 36, 38, 40, 42, 44,
53 /* 104 */ 46, 48, 50, 52, 54, 56, 58, 60,
54 /* 112 */ 62, 65, 68, 72, 77, 80, 84, 91,
55 /* 120 */ 95, 98, 103, 109, 114, 120, 126, 127
56 };
57
58
59 /* Convert 7 bit samples to 8 bit */
convert_7bit_to_8bit(uint8 * p,int l)60 static void convert_7bit_to_8bit(uint8 *p, int l)
61 {
62 for (; l--; p++) {
63 *p <<= 1;
64 }
65 }
66
67 /* Convert Archimedes VIDC samples to linear */
convert_vidc_to_linear(uint8 * p,int l)68 static void convert_vidc_to_linear(uint8 *p, int l)
69 {
70 int i;
71 uint8 x;
72
73 for (i = 0; i < l; i++) {
74 x = p[i];
75 p[i] = vdic_table[x >> 1];
76 if (x & 0x01)
77 p[i] *= -1;
78 }
79 }
80
adpcm4_decoder(uint8 * inp,uint8 * outp,char * tab,int len)81 static void adpcm4_decoder(uint8 *inp, uint8 *outp, char *tab, int len)
82 {
83 char delta = 0;
84 uint8 b0, b1;
85 int i;
86
87 len = (len + 1) / 2;
88
89 for (i = 0; i < len; i++) {
90 b0 = *inp;
91 b1 = *inp++ >> 4;
92 delta += tab[b0 & 0x0f];
93 *outp++ = delta;
94 delta += tab[b1 & 0x0f];
95 *outp++ = delta;
96 }
97 }
98
99 #endif
100
101 /* Convert differential to absolute sample data */
convert_delta(uint8 * p,int l,int r)102 static void convert_delta(uint8 *p, int l, int r)
103 {
104 uint16 *w = (uint16 *)p;
105 uint16 abs = 0;
106
107 if (r) {
108 for (; l--;) {
109 abs = *w + abs;
110 *w++ = abs;
111 }
112 } else {
113 for (; l--;) {
114 abs = *p + abs;
115 *p++ = (uint8) abs;
116 }
117 }
118 }
119
120 /* Convert signed to unsigned sample data */
convert_signal(uint8 * p,int l,int r)121 static void convert_signal(uint8 *p, int l, int r)
122 {
123 uint16 *w = (uint16 *)p;
124
125 if (r) {
126 for (; l--; w++)
127 *w += 0x8000;
128 } else {
129 for (; l--; p++)
130 *p += (char)0x80; /* cast needed by MSVC++ */
131 }
132 }
133
134 /* Convert little-endian 16 bit samples to big-endian */
convert_endian(uint8 * p,int l)135 static void convert_endian(uint8 *p, int l)
136 {
137 uint8 b;
138 int i;
139
140 for (i = 0; i < l; i++) {
141 b = p[0];
142 p[0] = p[1];
143 p[1] = b;
144 p += 2;
145 }
146 }
147
148 #if 0
149 /* Downmix stereo samples to mono */
150 static void convert_stereo_to_mono(uint8 *p, int l, int r)
151 {
152 int16 *b = (int16 *)p;
153 int i;
154
155 if (r) {
156 l /= 2;
157 for (i = 0; i < l; i++)
158 b[i] = (b[i * 2] + b[i * 2 + 1]) / 2;
159 } else {
160 for (i = 0; i < l; i++)
161 p[i] = (p[i * 2] + p[i * 2 + 1]) / 2;
162 }
163 }
164 #endif
165
unroll_loop(struct xmp_sample * xxs)166 static void unroll_loop(struct xmp_sample *xxs)
167 {
168 int8 *s8;
169 int16 *s16;
170 int start, loop_size;
171 int i;
172
173 s16 = (int16 *)xxs->data;
174 s8 = (int8 *)xxs->data;
175
176 if (xxs->len > xxs->lpe) {
177 start = xxs->lpe;
178 } else {
179 start = xxs->len;
180 }
181
182 loop_size = xxs->lpe - xxs->lps;
183
184 if (xxs->flg & XMP_SAMPLE_16BIT) {
185 s16 += start;
186 for (i = 0; i < loop_size; i++) {
187 *(s16 + i) = *(s16 - i - 1);
188 }
189 } else {
190 s8 += start;
191 for (i = 0; i < loop_size; i++) {
192 *(s8 + i) = *(s8 - i - 1);
193 }
194 }
195 }
196
197
libxmp_load_sample(struct module_data * m,HIO_HANDLE * f,int flags,struct xmp_sample * xxs,const void * buffer)198 int libxmp_load_sample(struct module_data *m, HIO_HANDLE *f, int flags, struct xmp_sample *xxs, const void *buffer)
199 {
200 int bytelen, extralen, unroll_extralen, i;
201
202 #ifndef LIBXMP_CORE_PLAYER
203 /* Adlib FM patches */
204 if (flags & SAMPLE_FLAG_ADLIB) {
205 return 0;
206 }
207 #endif
208
209 /* Empty or invalid samples
210 */
211 if (xxs->len <= 0) {
212 return 0;
213 }
214
215 /* Skip sample loading
216 * FIXME: fails for ADPCM samples
217 *
218 * + Sanity check: skip huge samples (likely corrupt module)
219 */
220 if (xxs->len > MAX_SAMPLE_SIZE || (m && m->smpctl & XMP_SMPCTL_SKIP)) {
221 if (~flags & SAMPLE_FLAG_NOLOAD) {
222 /* coverity[check_return] */
223 hio_seek(f, xxs->len, SEEK_CUR);
224 }
225 return 0;
226 }
227
228 /* Loop parameters sanity check
229 */
230 if (xxs->lps < 0) {
231 xxs->lps = 0;
232 }
233 if (xxs->lpe > xxs->len) {
234 xxs->lpe = xxs->len;
235 }
236 if (xxs->lps >= xxs->len || xxs->lps >= xxs->lpe) {
237 xxs->lps = xxs->lpe = 0;
238 xxs->flg &= ~(XMP_SAMPLE_LOOP | XMP_SAMPLE_LOOP_BIDIR);
239 }
240
241 /* Patches with samples
242 * Allocate extra sample for interpolation.
243 */
244 bytelen = xxs->len;
245 extralen = 4;
246 unroll_extralen = 0;
247
248 /* Disable birectional loop flag if sample is not looped
249 */
250 if (xxs->flg & XMP_SAMPLE_LOOP_BIDIR) {
251 if (~xxs->flg & XMP_SAMPLE_LOOP)
252 xxs->flg &= ~XMP_SAMPLE_LOOP_BIDIR;
253 }
254 /* Unroll bidirectional loops
255 */
256 if (xxs->flg & XMP_SAMPLE_LOOP_BIDIR) {
257 unroll_extralen = (xxs->lpe - xxs->lps) -
258 (xxs->len - xxs->lpe);
259
260 if (unroll_extralen < 0) {
261 unroll_extralen = 0;
262 }
263 }
264
265 if (xxs->flg & XMP_SAMPLE_16BIT) {
266 bytelen *= 2;
267 extralen *= 2;
268 unroll_extralen *= 2;
269 }
270
271 /* add guard bytes before the buffer for higher order interpolation */
272 xxs->data = (unsigned char *)malloc(bytelen + extralen + unroll_extralen + 4);
273 if (xxs->data == NULL) {
274 goto err;
275 }
276
277 *(uint32 *)xxs->data = 0;
278 xxs->data += 4;
279
280 if (flags & SAMPLE_FLAG_NOLOAD) {
281 memcpy(xxs->data, buffer, bytelen);
282 } else
283 #ifndef LIBXMP_CORE_PLAYER
284 if (flags & SAMPLE_FLAG_ADPCM) {
285 int x2 = (bytelen + 1) >> 1;
286 char table[16];
287
288 if (hio_read(table, 1, 16, f) != 16) {
289 goto err2;
290 }
291 if (hio_read(xxs->data + x2, 1, x2, f) != x2) {
292 goto err2;
293 }
294 adpcm4_decoder((uint8 *)xxs->data + x2,
295 (uint8 *)xxs->data, table, bytelen);
296 } else
297 #endif
298 {
299 int x = hio_read(xxs->data, 1, bytelen, f);
300 if (x != bytelen) {
301 D_(D_WARN "short read (%d) in sample load", x - bytelen);
302 memset(xxs->data + x, 0, bytelen - x);
303 }
304 }
305
306 #ifndef LIBXMP_CORE_PLAYER
307 if (flags & SAMPLE_FLAG_7BIT) {
308 convert_7bit_to_8bit(xxs->data, xxs->len);
309 }
310 #endif
311
312 /* Fix endianism if needed */
313 if (xxs->flg & XMP_SAMPLE_16BIT) {
314 #ifdef WORDS_BIGENDIAN
315 if (~flags & SAMPLE_FLAG_BIGEND)
316 convert_endian(xxs->data, xxs->len);
317 #else
318 if (flags & SAMPLE_FLAG_BIGEND)
319 convert_endian(xxs->data, xxs->len);
320 #endif
321 }
322
323 /* Convert delta samples */
324 if (flags & SAMPLE_FLAG_DIFF) {
325 convert_delta(xxs->data, xxs->len, xxs->flg & XMP_SAMPLE_16BIT);
326 } else if (flags & SAMPLE_FLAG_8BDIFF) {
327 int len = xxs->len;
328 if (xxs->flg & XMP_SAMPLE_16BIT) {
329 len *= 2;
330 }
331 convert_delta(xxs->data, len, 0);
332 }
333
334 /* Convert samples to signed */
335 if (flags & SAMPLE_FLAG_UNS) {
336 convert_signal(xxs->data, xxs->len,
337 xxs->flg & XMP_SAMPLE_16BIT);
338 }
339
340 #if 0
341 /* Downmix stereo samples */
342 if (flags & SAMPLE_FLAG_STEREO) {
343 convert_stereo_to_mono(xxs->data, xxs->len,
344 xxs->flg & XMP_SAMPLE_16BIT);
345 xxs->len /= 2;
346 }
347 #endif
348
349 #ifndef LIBXMP_CORE_PLAYER
350 if (flags & SAMPLE_FLAG_VIDC) {
351 convert_vidc_to_linear(xxs->data, xxs->len);
352 }
353 #endif
354
355 /* Check for full loop samples */
356 if (flags & SAMPLE_FLAG_FULLREP) {
357 if (xxs->lps == 0 && xxs->len > xxs->lpe)
358 xxs->flg |= XMP_SAMPLE_LOOP_FULL;
359 }
360
361 /* Unroll bidirectional loops */
362 if (xxs->flg & XMP_SAMPLE_LOOP_BIDIR) {
363 unroll_loop(xxs);
364 bytelen += unroll_extralen;
365 }
366
367 /* Add extra samples at end */
368 if (xxs->flg & XMP_SAMPLE_16BIT) {
369 for (i = 0; i < 8; i++) {
370 xxs->data[bytelen + i] = xxs->data[bytelen - 2 + i];
371 }
372 } else {
373 for (i = 0; i < 4; i++) {
374 xxs->data[bytelen + i] = xxs->data[bytelen - 1 + i];
375 }
376 }
377
378 /* Add extra samples at start */
379 if (xxs->flg & XMP_SAMPLE_16BIT) {
380 xxs->data[-2] = xxs->data[0];
381 xxs->data[-1] = xxs->data[1];
382 } else {
383 xxs->data[-1] = xxs->data[0];
384 }
385
386 /* Fix sample at loop */
387 if (xxs->flg & XMP_SAMPLE_LOOP) {
388 int lpe = xxs->lpe;
389 int lps = xxs->lps;
390
391 if (xxs->flg & XMP_SAMPLE_LOOP_BIDIR) {
392 lpe += lpe - lps;
393 }
394
395 if (xxs->flg & XMP_SAMPLE_16BIT) {
396 lpe <<= 1;
397 lps <<= 1;
398 for (i = 0; i < 8; i++) {
399 xxs->data[lpe + i] = xxs->data[lps + i];
400 }
401 } else {
402 for (i = 0; i < 4; i++) {
403 xxs->data[lpe + i] = xxs->data[lps + i];
404 }
405 }
406 }
407
408 return 0;
409
410 #ifndef LIBXMP_CORE_PLAYER
411 err2:
412 free(xxs->data - 4);
413 xxs->data = NULL; /* prevent double free in PCM load error */
414 #endif
415 err:
416 return -1;
417 }
418