Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / adx.c
blobdd5bba16e7ae4644f4db6168f9570031494785dd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2006-2008 Adam Gashlin (hcs)
10 * Copyright (C) 2006 Jens Arnold
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <limits.h>
23 #include "codeclib.h"
24 #include "inttypes.h"
25 #include "math.h"
26 #include "lib/fixedpoint.h"
28 CODEC_HEADER
30 /* Maximum number of bytes to process in one iteration */
31 #define WAV_CHUNK_SIZE (1024*2)
33 /* Number of times to loop looped tracks when repeat is disabled */
34 #define LOOP_TIMES 2
36 /* Length of fade-out for looped tracks (milliseconds) */
37 #define FADE_LENGTH 10000L
39 /* Default high pass filter cutoff frequency is 500 Hz.
40 * Others can be set, but the default is nearly always used,
41 * and there is no way to determine if another was used, anyway.
43 const long cutoff = 500;
45 static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
47 /* this is the codec entry point */
48 enum codec_status codec_main(void)
50 int channels;
51 int sampleswritten, i;
52 uint8_t *buf;
53 int32_t ch1_1, ch1_2, ch2_1, ch2_2; /* ADPCM history */
54 size_t n;
55 int endofstream; /* end of stream flag */
56 uint32_t avgbytespersec;
57 int looping; /* looping flag */
58 int loop_count; /* number of loops done so far */
59 int fade_count; /* countdown for fadeout */
60 int fade_frames; /* length of fade in frames */
61 off_t start_adr, end_adr; /* loop points */
62 off_t chanstart, bufoff;
63 /*long coef1=0x7298L,coef2=-0x3350L;*/
64 long coef1, coef2;
66 /* Generic codec initialisation */
67 /* we only render 16 bits */
68 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
70 next_track:
71 DEBUGF("ADX: next_track\n");
72 if (codec_init()) {
73 return CODEC_ERROR;
75 DEBUGF("ADX: after init\n");
77 /* init history */
78 ch1_1=ch1_2=ch2_1=ch2_2=0;
80 /* wait for track info to load */
81 while (!*ci->taginfo_ready && !ci->stop_codec)
82 ci->sleep(1);
84 codec_set_replaygain(ci->id3);
86 /* Get header */
87 DEBUGF("ADX: request initial buffer\n");
88 ci->seek_buffer(0);
89 buf = ci->request_buffer(&n, 0x38);
90 if (!buf || n < 0x38) {
91 return CODEC_ERROR;
93 bufoff = 0;
94 DEBUGF("ADX: read size = %lx\n",(unsigned long)n);
96 /* Get file header for starting offset, channel count */
98 chanstart = ((buf[2] << 8) | buf[3]) + 4;
99 channels = buf[7];
101 /* useful for seeking and reporting current playback position */
102 avgbytespersec = ci->id3->frequency * 18 * channels / 32;
103 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec);
105 /* calculate filter coefficients */
108 * A simple table of these coefficients would be nice, but
109 * some very odd frequencies are used and if I'm going to
110 * interpolate I might as well just go all the way and
111 * calclate them precisely.
112 * Speed is not an issue as this only needs to be done once per file.
115 const int64_t big28 = 0x10000000LL;
116 const int64_t big32 = 0x100000000LL;
117 int64_t frequency = ci->id3->frequency;
118 int64_t phasemultiple = cutoff*big32/frequency;
120 long z;
121 int64_t a;
122 const int64_t b = (M_SQRT2*big28)-big28;
123 int64_t c;
124 int64_t d;
126 fp_sincos((unsigned long)phasemultiple,&z);
128 a = (M_SQRT2*big28)-(z*big28/LONG_MAX);
131 * In the long passed to fsqrt there are only 4 nonfractional bits,
132 * which is sufficient here, but this is the only reason why I don't
133 * use 32 fractional bits everywhere.
135 d = fp_sqrt((a+b)*(a-b)/big28,28);
136 c = (a-d)*big28/b;
138 coef1 = (c*8192) >> 28;
139 coef2 = (c*c/big28*-4096) >> 28;
140 DEBUGF("ADX: samprate=%ld ",(long)frequency);
141 DEBUGF("coef1 %04x ",(unsigned int)(coef1*4));
142 DEBUGF("coef2 %04x\n",(unsigned int)(coef2*-4));
145 /* Get loop data */
147 looping = 0; start_adr = 0; end_adr = 0;
148 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
149 /* Soul Calibur 2 style (type 03) */
150 DEBUGF("ADX: type 03 found\n");
151 /* check if header is too small for loop data */
152 if (chanstart-6 < 0x2c) looping=0;
153 else {
154 looping = (buf[0x18]) ||
155 (buf[0x19]) ||
156 (buf[0x1a]) ||
157 (buf[0x1b]);
158 end_adr = (buf[0x28]<<24) |
159 (buf[0x29]<<16) |
160 (buf[0x2a]<<8) |
161 (buf[0x2b]);
163 start_adr = (
164 (buf[0x1c]<<24) |
165 (buf[0x1d]<<16) |
166 (buf[0x1e]<<8) |
167 (buf[0x1f])
168 )/32*channels*18+chanstart;
170 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
171 /* Standard (type 04) */
172 DEBUGF("ADX: type 04 found\n");
173 /* check if header is too small for loop data */
174 if (chanstart-6 < 0x38) looping=0;
175 else {
176 looping = (buf[0x24]) ||
177 (buf[0x25]) ||
178 (buf[0x26]) ||
179 (buf[0x27]);
180 end_adr = (buf[0x34]<<24) |
181 (buf[0x35]<<16) |
182 (buf[0x36]<<8) |
183 buf[0x37];
184 start_adr = (
185 (buf[0x28]<<24) |
186 (buf[0x29]<<16) |
187 (buf[0x2a]<<8) |
188 (buf[0x2b])
189 )/32*channels*18+chanstart;
191 } else {
192 DEBUGF("ADX: error, couldn't determine ADX type\n");
193 return CODEC_ERROR;
196 if (looping) {
197 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr,end_adr);
198 } else {
199 DEBUGF("ADX: not looped\n");
202 /* advance to first frame */
203 DEBUGF("ADX: first frame at %lx\n",chanstart);
204 bufoff = chanstart;
206 /* get in position */
207 ci->seek_buffer(bufoff);
210 /* setup pcm buffer format */
211 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
212 if (channels == 2) {
213 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
214 } else if (channels == 1) {
215 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
216 } else {
217 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
218 return CODEC_ERROR;
221 endofstream = 0;
222 loop_count = 0;
223 fade_count = -1; /* disable fade */
224 fade_frames = 1;
226 /* The main decoder loop */
228 while (!endofstream) {
229 ci->yield();
230 if (ci->stop_codec || ci->new_track) {
231 break;
234 /* do we need to loop? */
235 if (bufoff > end_adr-18*channels && looping) {
236 DEBUGF("ADX: loop!\n");
237 /* check for endless looping */
238 if (ci->global_settings->repeat_mode==REPEAT_ONE) {
239 loop_count=0;
240 fade_count = -1; /* disable fade */
241 } else {
242 /* otherwise start fade after LOOP_TIMES loops */
243 loop_count++;
244 if (loop_count >= LOOP_TIMES && fade_count < 0) {
245 /* frames to fade over */
246 fade_frames = FADE_LENGTH*ci->id3->frequency/32/1000;
247 /* volume relative to fade_frames */
248 fade_count = fade_frames;
249 DEBUGF("ADX: fade_frames = %d\n",fade_frames);
252 bufoff = start_adr;
253 ci->seek_buffer(bufoff);
256 /* do we need to seek? */
257 if (ci->seek_time) {
258 uint32_t newpos;
260 DEBUGF("ADX: seek to %ldms\n",ci->seek_time);
262 endofstream = 0;
263 loop_count = 0;
264 fade_count = -1; /* disable fade */
265 fade_frames = 1;
267 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
268 / (1000LL*18*channels))*(18*channels);
269 bufoff = chanstart + newpos;
270 while (bufoff > end_adr-18*channels) {
271 bufoff-=end_adr-start_adr;
272 loop_count++;
274 ci->seek_buffer(bufoff);
275 ci->seek_complete();
278 if (bufoff>ci->filesize-channels*18) break; /* End of stream */
280 sampleswritten=0;
282 while (
283 /* Is there data left in the file? */
284 (bufoff <= ci->filesize-(18*channels)) &&
285 /* Is there space in the output buffer? */
286 (sampleswritten <= WAV_CHUNK_SIZE-(32*channels)) &&
287 /* Should we be looping? */
288 ((!looping) || bufoff <= end_adr-18*channels))
290 /* decode first/only channel */
291 int32_t scale;
292 int32_t ch1_0, d;
294 /* fetch a frame */
295 buf = ci->request_buffer(&n, 18);
297 if (!buf || n!=18) {
298 DEBUGF("ADX: couldn't get buffer at %lx\n",
299 bufoff);
300 return CODEC_ERROR;
303 scale = ((buf[0] << 8) | (buf[1])) +1;
305 for (i = 2; i < 18; i++)
307 d = (buf[i] >> 4) & 15;
308 if (d & 8) d-= 16;
309 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
310 if (ch1_0 > 32767) ch1_0 = 32767;
311 else if (ch1_0 < -32768) ch1_0 = -32768;
312 samples[sampleswritten] = ch1_0;
313 sampleswritten+=channels;
314 ch1_2 = ch1_1; ch1_1 = ch1_0;
316 d = buf[i] & 15;
317 if (d & 8) d -= 16;
318 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
319 if (ch1_0 > 32767) ch1_0 = 32767;
320 else if (ch1_0 < -32768) ch1_0 = -32768;
321 samples[sampleswritten] = ch1_0;
322 sampleswritten+=channels;
323 ch1_2 = ch1_1; ch1_1 = ch1_0;
325 bufoff+=18;
326 ci->advance_buffer(18);
328 if (channels == 2) {
329 /* decode second channel */
330 int32_t scale;
331 int32_t ch2_0, d;
333 buf = ci->request_buffer(&n, 18);
335 if (!buf || n!=18) {
336 DEBUGF("ADX: couldn't get buffer at %lx\n",
337 bufoff);
338 return CODEC_ERROR;
341 scale = ((buf[0] << 8)|(buf[1]))+1;
343 sampleswritten-=63;
345 for (i = 2; i < 18; i++)
347 d = (buf[i] >> 4) & 15;
348 if (d & 8) d-= 16;
349 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
350 if (ch2_0 > 32767) ch2_0 = 32767;
351 else if (ch2_0 < -32768) ch2_0 = -32768;
352 samples[sampleswritten] = ch2_0;
353 sampleswritten+=2;
354 ch2_2 = ch2_1; ch2_1 = ch2_0;
356 d = buf[i] & 15;
357 if (d & 8) d -= 16;
358 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
359 if (ch2_0 > 32767) ch2_0 = 32767;
360 else if (ch2_0 < -32768) ch2_0 = -32768;
361 samples[sampleswritten] = ch2_0;
362 sampleswritten+=2;
363 ch2_2 = ch2_1; ch2_1 = ch2_0;
365 bufoff+=18;
366 ci->advance_buffer(18);
367 sampleswritten--; /* go back to first channel's next sample */
370 if (fade_count>0) {
371 fade_count--;
372 for (i=0;i<(channels==1?32:64);i++) samples[sampleswritten-i-1]=
373 ((int32_t)samples[sampleswritten-i-1])*fade_count/fade_frames;
374 if (fade_count==0) {endofstream=1; break;}
378 if (channels == 2)
379 sampleswritten >>= 1; /* make samples/channel */
381 ci->pcmbuf_insert(samples, NULL, sampleswritten);
383 ci->set_elapsed(
384 ((end_adr-start_adr)*loop_count + bufoff-chanstart)*
385 1000LL/avgbytespersec);
388 if (ci->request_next_track())
389 goto next_track;
391 return CODEC_OK;