autodetection: convert path to native separators before displaying it.
[Rockbox.git] / apps / codecs / adx.c
blobc265f0cd56fd839624d97e6b0b486a83d81d870c
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 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "codeclib.h"
20 #include "inttypes.h"
21 #include "math.h"
23 CODEC_HEADER
25 /* Maximum number of bytes to process in one iteration */
26 #define WAV_CHUNK_SIZE (1024*2)
28 /* Number of times to loop looped tracks when repeat is disabled */
29 #define LOOP_TIMES 2
31 /* Length of fade-out for looped tracks (milliseconds) */
32 #define FADE_LENGTH 10000L
34 /* Default high pass filter cutoff frequency is 500 Hz.
35 * Others can be set, but the default is nearly always used,
36 * and there is no way to determine if another was used, anyway.
38 const long cutoff = 500;
40 static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
42 /* fixed point stuff from apps/plugins/lib/fixedpoint.c */
44 /* Inverse gain of circular cordic rotation in s0.31 format. */
45 static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
47 /* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
48 static const unsigned long atan_table[] = {
49 0x1fffffff, /* +0.785398163 (or pi/4) */
50 0x12e4051d, /* +0.463647609 */
51 0x09fb385b, /* +0.244978663 */
52 0x051111d4, /* +0.124354995 */
53 0x028b0d43, /* +0.062418810 */
54 0x0145d7e1, /* +0.031239833 */
55 0x00a2f61e, /* +0.015623729 */
56 0x00517c55, /* +0.007812341 */
57 0x0028be53, /* +0.003906230 */
58 0x00145f2e, /* +0.001953123 */
59 0x000a2f98, /* +0.000976562 */
60 0x000517cc, /* +0.000488281 */
61 0x00028be6, /* +0.000244141 */
62 0x000145f3, /* +0.000122070 */
63 0x0000a2f9, /* +0.000061035 */
64 0x0000517c, /* +0.000030518 */
65 0x000028be, /* +0.000015259 */
66 0x0000145f, /* +0.000007629 */
67 0x00000a2f, /* +0.000003815 */
68 0x00000517, /* +0.000001907 */
69 0x0000028b, /* +0.000000954 */
70 0x00000145, /* +0.000000477 */
71 0x000000a2, /* +0.000000238 */
72 0x00000051, /* +0.000000119 */
73 0x00000028, /* +0.000000060 */
74 0x00000014, /* +0.000000030 */
75 0x0000000a, /* +0.000000015 */
76 0x00000005, /* +0.000000007 */
77 0x00000002, /* +0.000000004 */
78 0x00000001, /* +0.000000002 */
79 0x00000000, /* +0.000000001 */
80 0x00000000, /* +0.000000000 */
83 /**
84 * Implements sin and cos using CORDIC rotation.
86 * @param phase has range from 0 to 0xffffffff, representing 0 and
87 * 2*pi respectively.
88 * @param cos return address for cos
89 * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
90 * representing -1 and 1 respectively.
92 static long fsincos(unsigned long phase, long *cos)
94 int32_t x, x1, y, y1;
95 unsigned long z, z1;
96 int i;
98 /* Setup initial vector */
99 x = cordic_circular_gain;
100 y = 0;
101 z = phase;
103 /* The phase has to be somewhere between 0..pi for this to work right */
104 if (z < 0xffffffff / 4) {
105 /* z in first quadrant, z += pi/2 to correct */
106 x = -x;
107 z += 0xffffffff / 4;
108 } else if (z < 3 * (0xffffffff / 4)) {
109 /* z in third quadrant, z -= pi/2 to correct */
110 z -= 0xffffffff / 4;
111 } else {
112 /* z in fourth quadrant, z -= 3pi/2 to correct */
113 x = -x;
114 z -= 3 * (0xffffffff / 4);
117 /* Each iteration adds roughly 1-bit of extra precision */
118 for (i = 0; i < 31; i++) {
119 x1 = x >> i;
120 y1 = y >> i;
121 z1 = atan_table[i];
123 /* Decided which direction to rotate vector. Pivot point is pi/2 */
124 if (z >= 0xffffffff / 4) {
125 x -= y1;
126 y += x1;
127 z -= z1;
128 } else {
129 x += y1;
130 y -= x1;
131 z += z1;
135 if (cos)
136 *cos = x;
138 return y;
142 * Fixed point square root via Newton-Raphson.
143 * @param a square root argument.
144 * @param fracbits specifies number of fractional bits in argument.
145 * @return Square root of argument in same fixed point format as input.
147 static long fsqrt(long a, unsigned int fracbits)
149 long b = a/2 + (1 << fracbits); /* initial approximation */
150 unsigned n;
151 const unsigned iterations = 8; /* bumped up from 4 as it wasn't
152 nearly enough for 28 fractional bits */
154 for (n = 0; n < iterations; ++n)
155 b = (b + (long)(((long long)(a) << fracbits)/b))/2;
157 return b;
160 /* this is the codec entry point */
161 enum codec_status codec_main(void)
163 int channels;
164 int sampleswritten, i;
165 uint8_t *buf;
166 int32_t ch1_1, ch1_2, ch2_1, ch2_2; /* ADPCM history */
167 size_t n;
168 int endofstream; /* end of stream flag */
169 uint32_t avgbytespersec;
170 int looping; /* looping flag */
171 int loop_count; /* number of loops done so far */
172 int fade_count; /* countdown for fadeout */
173 int fade_frames; /* length of fade in frames */
174 off_t start_adr, end_adr; /* loop points */
175 off_t chanstart, bufoff;
176 /*long coef1=0x7298L,coef2=-0x3350L;*/
177 long coef1, coef2;
179 /* Generic codec initialisation */
180 /* we only render 16 bits */
181 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
183 next_track:
184 DEBUGF("ADX: next_track\n");
185 if (codec_init()) {
186 return CODEC_ERROR;
188 DEBUGF("ADX: after init\n");
190 /* init history */
191 ch1_1=ch1_2=ch2_1=ch2_2=0;
193 /* wait for track info to load */
194 while (!*ci->taginfo_ready && !ci->stop_codec)
195 ci->sleep(1);
197 codec_set_replaygain(ci->id3);
199 /* Get header */
200 DEBUGF("ADX: request initial buffer\n");
201 ci->seek_buffer(0);
202 buf = ci->request_buffer(&n, 0x38);
203 if (!buf || n < 0x38) {
204 return CODEC_ERROR;
206 bufoff = 0;
207 DEBUGF("ADX: read size = %lx\n",(unsigned long)n);
209 /* Get file header for starting offset, channel count */
211 chanstart = ((buf[2] << 8) | buf[3]) + 4;
212 channels = buf[7];
214 /* useful for seeking and reporting current playback position */
215 avgbytespersec = ci->id3->frequency * 18 * channels / 32;
216 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec);
218 /* calculate filter coefficients */
221 * A simple table of these coefficients would be nice, but
222 * some very odd frequencies are used and if I'm going to
223 * interpolate I might as well just go all the way and
224 * calclate them precisely.
225 * Speed is not an issue as this only needs to be done once per file.
228 const int64_t big28 = 0x10000000LL;
229 const int64_t big32 = 0x100000000LL;
230 int64_t frequency = ci->id3->frequency;
231 int64_t phasemultiple = cutoff*big32/frequency;
233 long z;
234 int64_t a;
235 const int64_t b = (M_SQRT2*big28)-big28;
236 int64_t c;
237 int64_t d;
239 fsincos((unsigned long)phasemultiple,&z);
241 a = (M_SQRT2*big28)-(z*big28/LONG_MAX);
244 * In the long passed to fsqrt there are only 4 nonfractional bits,
245 * which is sufficient here, but this is the only reason why I don't
246 * use 32 fractional bits everywhere.
248 d = fsqrt((a+b)*(a-b)/big28,28);
249 c = (a-d)*big28/b;
251 coef1 = (c*8192) >> 28;
252 coef2 = (c*c/big28*-4096) >> 28;
253 DEBUGF("ADX: samprate=%ld ",(long)frequency);
254 DEBUGF("coef1 %04x ",(unsigned int)(coef1*4));
255 DEBUGF("coef2 %04x\n",(unsigned int)(coef2*-4));
258 /* Get loop data */
260 looping = 0; start_adr = 0; end_adr = 0;
261 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
262 /* Soul Calibur 2 style (type 03) */
263 DEBUGF("ADX: type 03 found\n");
264 /* check if header is too small for loop data */
265 if (chanstart-6 < 0x2c) looping=0;
266 else {
267 looping = (buf[0x18]) ||
268 (buf[0x19]) ||
269 (buf[0x1a]) ||
270 (buf[0x1b]);
271 end_adr = (buf[0x28]<<24) |
272 (buf[0x29]<<16) |
273 (buf[0x2a]<<8) |
274 (buf[0x2b]);
276 start_adr = (
277 (buf[0x1c]<<24) |
278 (buf[0x1d]<<16) |
279 (buf[0x1e]<<8) |
280 (buf[0x1f])
281 )/32*channels*18+chanstart;
283 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
284 /* Standard (type 04) */
285 DEBUGF("ADX: type 04 found\n");
286 /* check if header is too small for loop data */
287 if (chanstart-6 < 0x38) looping=0;
288 else {
289 looping = (buf[0x24]) ||
290 (buf[0x25]) ||
291 (buf[0x26]) ||
292 (buf[0x27]);
293 end_adr = (buf[0x34]<<24) |
294 (buf[0x35]<<16) |
295 (buf[0x36]<<8) |
296 buf[0x37];
297 start_adr = (
298 (buf[0x28]<<24) |
299 (buf[0x29]<<16) |
300 (buf[0x2a]<<8) |
301 (buf[0x2b])
302 )/32*channels*18+chanstart;
304 } else {
305 DEBUGF("ADX: error, couldn't determine ADX type\n");
306 return CODEC_ERROR;
309 if (looping) {
310 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr,end_adr);
311 } else {
312 DEBUGF("ADX: not looped\n");
315 /* advance to first frame */
316 DEBUGF("ADX: first frame at %lx\n",chanstart);
317 bufoff = chanstart;
319 /* get in position */
320 ci->seek_buffer(bufoff);
323 /* setup pcm buffer format */
324 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
325 if (channels == 2) {
326 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
327 } else if (channels == 1) {
328 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
329 } else {
330 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
331 return CODEC_ERROR;
334 endofstream = 0;
335 loop_count = 0;
336 fade_count = -1; /* disable fade */
337 fade_frames = 1;
339 /* The main decoder loop */
341 while (!endofstream) {
342 ci->yield();
343 if (ci->stop_codec || ci->new_track) {
344 break;
347 /* do we need to loop? */
348 if (bufoff > end_adr-18*channels && looping) {
349 DEBUGF("ADX: loop!\n");
350 /* check for endless looping */
351 if (ci->global_settings->repeat_mode==REPEAT_ONE) {
352 loop_count=0;
353 fade_count = -1; /* disable fade */
354 } else {
355 /* otherwise start fade after LOOP_TIMES loops */
356 loop_count++;
357 if (loop_count >= LOOP_TIMES && fade_count < 0) {
358 /* frames to fade over */
359 fade_frames = FADE_LENGTH*ci->id3->frequency/32/1000;
360 /* volume relative to fade_frames */
361 fade_count = fade_frames;
362 DEBUGF("ADX: fade_frames = %d\n",fade_frames);
365 bufoff = start_adr;
366 ci->seek_buffer(bufoff);
369 /* do we need to seek? */
370 if (ci->seek_time) {
371 uint32_t newpos;
373 DEBUGF("ADX: seek to %ldms\n",ci->seek_time);
375 endofstream = 0;
376 loop_count = 0;
377 fade_count = -1; /* disable fade */
378 fade_frames = 1;
380 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
381 / (1000LL*18*channels))*(18*channels);
382 bufoff = chanstart + newpos;
383 while (bufoff > end_adr-18*channels) {
384 bufoff-=end_adr-start_adr;
385 loop_count++;
387 ci->seek_buffer(bufoff);
388 ci->seek_complete();
391 if (bufoff>ci->filesize-channels*18) break; /* End of stream */
393 sampleswritten=0;
395 while (
396 /* Is there data left in the file? */
397 (bufoff <= ci->filesize-(18*channels)) &&
398 /* Is there space in the output buffer? */
399 (sampleswritten <= WAV_CHUNK_SIZE-(32*channels)) &&
400 /* Should we be looping? */
401 ((!looping) || bufoff <= end_adr-18*channels))
403 /* decode first/only channel */
404 int32_t scale;
405 int32_t ch1_0, d;
407 /* fetch a frame */
408 buf = ci->request_buffer(&n, 18);
410 if (!buf || n!=18) {
411 DEBUGF("ADX: couldn't get buffer at %lx\n",
412 bufoff);
413 return CODEC_ERROR;
416 scale = ((buf[0] << 8) | (buf[1])) +1;
418 for (i = 2; i < 18; i++)
420 d = (buf[i] >> 4) & 15;
421 if (d & 8) d-= 16;
422 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
423 if (ch1_0 > 32767) ch1_0 = 32767;
424 else if (ch1_0 < -32768) ch1_0 = -32768;
425 samples[sampleswritten] = ch1_0;
426 sampleswritten+=channels;
427 ch1_2 = ch1_1; ch1_1 = ch1_0;
429 d = buf[i] & 15;
430 if (d & 8) d -= 16;
431 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
432 if (ch1_0 > 32767) ch1_0 = 32767;
433 else if (ch1_0 < -32768) ch1_0 = -32768;
434 samples[sampleswritten] = ch1_0;
435 sampleswritten+=channels;
436 ch1_2 = ch1_1; ch1_1 = ch1_0;
438 bufoff+=18;
439 ci->advance_buffer(18);
441 if (channels == 2) {
442 /* decode second channel */
443 int32_t scale;
444 int32_t ch2_0, d;
446 buf = ci->request_buffer(&n, 18);
448 if (!buf || n!=18) {
449 DEBUGF("ADX: couldn't get buffer at %lx\n",
450 bufoff);
451 return CODEC_ERROR;
454 scale = ((buf[0] << 8)|(buf[1]))+1;
456 sampleswritten-=63;
458 for (i = 2; i < 18; i++)
460 d = (buf[i] >> 4) & 15;
461 if (d & 8) d-= 16;
462 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
463 if (ch2_0 > 32767) ch2_0 = 32767;
464 else if (ch2_0 < -32768) ch2_0 = -32768;
465 samples[sampleswritten] = ch2_0;
466 sampleswritten+=2;
467 ch2_2 = ch2_1; ch2_1 = ch2_0;
469 d = buf[i] & 15;
470 if (d & 8) d -= 16;
471 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
472 if (ch2_0 > 32767) ch2_0 = 32767;
473 else if (ch2_0 < -32768) ch2_0 = -32768;
474 samples[sampleswritten] = ch2_0;
475 sampleswritten+=2;
476 ch2_2 = ch2_1; ch2_1 = ch2_0;
478 bufoff+=18;
479 ci->advance_buffer(18);
480 sampleswritten--; /* go back to first channel's next sample */
483 if (fade_count>0) {
484 fade_count--;
485 for (i=0;i<(channels==1?32:64);i++) samples[sampleswritten-i-1]=
486 ((int32_t)samples[sampleswritten-i-1])*fade_count/fade_frames;
487 if (fade_count==0) {endofstream=1; break;}
491 if (channels == 2)
492 sampleswritten >>= 1; /* make samples/channel */
494 ci->pcmbuf_insert(samples, NULL, sampleswritten);
496 ci->set_elapsed(
497 ((end_adr-start_adr)*loop_count + bufoff-chanstart)*
498 1000LL/avgbytespersec);
501 if (ci->request_next_track())
502 goto next_track;
504 return CODEC_OK;