FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / adx.c
blob0e50054753e99c80d80558c7b2d5074f23472030
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 ****************************************************************************/
21 #include "codeclib.h"
22 #include "inttypes.h"
23 #include "math.h"
24 #include "lib/fixedpoint.h"
26 CODEC_HEADER
28 /* Maximum number of bytes to process in one iteration */
29 #define WAV_CHUNK_SIZE (1024*2)
31 /* Number of times to loop looped tracks when repeat is disabled */
32 #define LOOP_TIMES 2
34 /* Length of fade-out for looped tracks (milliseconds) */
35 #define FADE_LENGTH 10000L
37 /* Default high pass filter cutoff frequency is 500 Hz.
38 * Others can be set, but the default is nearly always used,
39 * and there is no way to determine if another was used, anyway.
41 const long cutoff = 500;
43 static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
45 /* this is the codec entry point */
46 enum codec_status codec_main(void)
48 int channels;
49 int sampleswritten, i;
50 uint8_t *buf;
51 int32_t ch1_1, ch1_2, ch2_1, ch2_2; /* ADPCM history */
52 size_t n;
53 int endofstream; /* end of stream flag */
54 uint32_t avgbytespersec;
55 int looping; /* looping flag */
56 int loop_count; /* number of loops done so far */
57 int fade_count; /* countdown for fadeout */
58 int fade_frames; /* length of fade in frames */
59 off_t start_adr, end_adr; /* loop points */
60 off_t chanstart, bufoff;
61 /*long coef1=0x7298L,coef2=-0x3350L;*/
62 long coef1, coef2;
64 /* Generic codec initialisation */
65 /* we only render 16 bits */
66 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
68 next_track:
69 DEBUGF("ADX: next_track\n");
70 if (codec_init()) {
71 return CODEC_ERROR;
73 DEBUGF("ADX: after init\n");
75 /* init history */
76 ch1_1=ch1_2=ch2_1=ch2_2=0;
78 /* wait for track info to load */
79 while (!*ci->taginfo_ready && !ci->stop_codec)
80 ci->sleep(1);
82 codec_set_replaygain(ci->id3);
84 /* Get header */
85 DEBUGF("ADX: request initial buffer\n");
86 ci->seek_buffer(0);
87 buf = ci->request_buffer(&n, 0x38);
88 if (!buf || n < 0x38) {
89 return CODEC_ERROR;
91 bufoff = 0;
92 DEBUGF("ADX: read size = %lx\n",(unsigned long)n);
94 /* Get file header for starting offset, channel count */
96 chanstart = ((buf[2] << 8) | buf[3]) + 4;
97 channels = buf[7];
99 /* useful for seeking and reporting current playback position */
100 avgbytespersec = ci->id3->frequency * 18 * channels / 32;
101 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec);
103 /* calculate filter coefficients */
106 * A simple table of these coefficients would be nice, but
107 * some very odd frequencies are used and if I'm going to
108 * interpolate I might as well just go all the way and
109 * calclate them precisely.
110 * Speed is not an issue as this only needs to be done once per file.
113 const int64_t big28 = 0x10000000LL;
114 const int64_t big32 = 0x100000000LL;
115 int64_t frequency = ci->id3->frequency;
116 int64_t phasemultiple = cutoff*big32/frequency;
118 long z;
119 int64_t a;
120 const int64_t b = (M_SQRT2*big28)-big28;
121 int64_t c;
122 int64_t d;
124 fp_sincos((unsigned long)phasemultiple,&z);
126 a = (M_SQRT2*big28)-(z*big28/LONG_MAX);
129 * In the long passed to fsqrt there are only 4 nonfractional bits,
130 * which is sufficient here, but this is the only reason why I don't
131 * use 32 fractional bits everywhere.
133 d = fp_sqrt((a+b)*(a-b)/big28,28);
134 c = (a-d)*big28/b;
136 coef1 = (c*8192) >> 28;
137 coef2 = (c*c/big28*-4096) >> 28;
138 DEBUGF("ADX: samprate=%ld ",(long)frequency);
139 DEBUGF("coef1 %04x ",(unsigned int)(coef1*4));
140 DEBUGF("coef2 %04x\n",(unsigned int)(coef2*-4));
143 /* Get loop data */
145 looping = 0; start_adr = 0; end_adr = 0;
146 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
147 /* Soul Calibur 2 style (type 03) */
148 DEBUGF("ADX: type 03 found\n");
149 /* check if header is too small for loop data */
150 if (chanstart-6 < 0x2c) looping=0;
151 else {
152 looping = (buf[0x18]) ||
153 (buf[0x19]) ||
154 (buf[0x1a]) ||
155 (buf[0x1b]);
156 end_adr = (buf[0x28]<<24) |
157 (buf[0x29]<<16) |
158 (buf[0x2a]<<8) |
159 (buf[0x2b]);
161 start_adr = (
162 (buf[0x1c]<<24) |
163 (buf[0x1d]<<16) |
164 (buf[0x1e]<<8) |
165 (buf[0x1f])
166 )/32*channels*18+chanstart;
168 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
169 /* Standard (type 04) */
170 DEBUGF("ADX: type 04 found\n");
171 /* check if header is too small for loop data */
172 if (chanstart-6 < 0x38) looping=0;
173 else {
174 looping = (buf[0x24]) ||
175 (buf[0x25]) ||
176 (buf[0x26]) ||
177 (buf[0x27]);
178 end_adr = (buf[0x34]<<24) |
179 (buf[0x35]<<16) |
180 (buf[0x36]<<8) |
181 buf[0x37];
182 start_adr = (
183 (buf[0x28]<<24) |
184 (buf[0x29]<<16) |
185 (buf[0x2a]<<8) |
186 (buf[0x2b])
187 )/32*channels*18+chanstart;
189 } else {
190 DEBUGF("ADX: error, couldn't determine ADX type\n");
191 return CODEC_ERROR;
194 if (looping) {
195 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr,end_adr);
196 } else {
197 DEBUGF("ADX: not looped\n");
200 /* advance to first frame */
201 DEBUGF("ADX: first frame at %lx\n",chanstart);
202 bufoff = chanstart;
204 /* get in position */
205 ci->seek_buffer(bufoff);
208 /* setup pcm buffer format */
209 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
210 if (channels == 2) {
211 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
212 } else if (channels == 1) {
213 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
214 } else {
215 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
216 return CODEC_ERROR;
219 endofstream = 0;
220 loop_count = 0;
221 fade_count = -1; /* disable fade */
222 fade_frames = 1;
224 /* The main decoder loop */
226 while (!endofstream) {
227 ci->yield();
228 if (ci->stop_codec || ci->new_track) {
229 break;
232 /* do we need to loop? */
233 if (bufoff > end_adr-18*channels && looping) {
234 DEBUGF("ADX: loop!\n");
235 /* check for endless looping */
236 if (ci->global_settings->repeat_mode==REPEAT_ONE) {
237 loop_count=0;
238 fade_count = -1; /* disable fade */
239 } else {
240 /* otherwise start fade after LOOP_TIMES loops */
241 loop_count++;
242 if (loop_count >= LOOP_TIMES && fade_count < 0) {
243 /* frames to fade over */
244 fade_frames = FADE_LENGTH*ci->id3->frequency/32/1000;
245 /* volume relative to fade_frames */
246 fade_count = fade_frames;
247 DEBUGF("ADX: fade_frames = %d\n",fade_frames);
250 bufoff = start_adr;
251 ci->seek_buffer(bufoff);
254 /* do we need to seek? */
255 if (ci->seek_time) {
256 uint32_t newpos;
258 DEBUGF("ADX: seek to %ldms\n",ci->seek_time);
260 endofstream = 0;
261 loop_count = 0;
262 fade_count = -1; /* disable fade */
263 fade_frames = 1;
265 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
266 / (1000LL*18*channels))*(18*channels);
267 bufoff = chanstart + newpos;
268 while (bufoff > end_adr-18*channels) {
269 bufoff-=end_adr-start_adr;
270 loop_count++;
272 ci->seek_buffer(bufoff);
273 ci->seek_complete();
276 if (bufoff>ci->filesize-channels*18) break; /* End of stream */
278 sampleswritten=0;
280 while (
281 /* Is there data left in the file? */
282 (bufoff <= ci->filesize-(18*channels)) &&
283 /* Is there space in the output buffer? */
284 (sampleswritten <= WAV_CHUNK_SIZE-(32*channels)) &&
285 /* Should we be looping? */
286 ((!looping) || bufoff <= end_adr-18*channels))
288 /* decode first/only channel */
289 int32_t scale;
290 int32_t ch1_0, d;
292 /* fetch a frame */
293 buf = ci->request_buffer(&n, 18);
295 if (!buf || n!=18) {
296 DEBUGF("ADX: couldn't get buffer at %lx\n",
297 bufoff);
298 return CODEC_ERROR;
301 scale = ((buf[0] << 8) | (buf[1])) +1;
303 for (i = 2; i < 18; i++)
305 d = (buf[i] >> 4) & 15;
306 if (d & 8) d-= 16;
307 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
308 if (ch1_0 > 32767) ch1_0 = 32767;
309 else if (ch1_0 < -32768) ch1_0 = -32768;
310 samples[sampleswritten] = ch1_0;
311 sampleswritten+=channels;
312 ch1_2 = ch1_1; ch1_1 = ch1_0;
314 d = buf[i] & 15;
315 if (d & 8) d -= 16;
316 ch1_0 = d*scale + ((coef1*ch1_1 + coef2*ch1_2) >> 12);
317 if (ch1_0 > 32767) ch1_0 = 32767;
318 else if (ch1_0 < -32768) ch1_0 = -32768;
319 samples[sampleswritten] = ch1_0;
320 sampleswritten+=channels;
321 ch1_2 = ch1_1; ch1_1 = ch1_0;
323 bufoff+=18;
324 ci->advance_buffer(18);
326 if (channels == 2) {
327 /* decode second channel */
328 int32_t scale;
329 int32_t ch2_0, d;
331 buf = ci->request_buffer(&n, 18);
333 if (!buf || n!=18) {
334 DEBUGF("ADX: couldn't get buffer at %lx\n",
335 bufoff);
336 return CODEC_ERROR;
339 scale = ((buf[0] << 8)|(buf[1]))+1;
341 sampleswritten-=63;
343 for (i = 2; i < 18; i++)
345 d = (buf[i] >> 4) & 15;
346 if (d & 8) d-= 16;
347 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
348 if (ch2_0 > 32767) ch2_0 = 32767;
349 else if (ch2_0 < -32768) ch2_0 = -32768;
350 samples[sampleswritten] = ch2_0;
351 sampleswritten+=2;
352 ch2_2 = ch2_1; ch2_1 = ch2_0;
354 d = buf[i] & 15;
355 if (d & 8) d -= 16;
356 ch2_0 = d*scale + ((coef1*ch2_1 + coef2*ch2_2) >> 12);
357 if (ch2_0 > 32767) ch2_0 = 32767;
358 else if (ch2_0 < -32768) ch2_0 = -32768;
359 samples[sampleswritten] = ch2_0;
360 sampleswritten+=2;
361 ch2_2 = ch2_1; ch2_1 = ch2_0;
363 bufoff+=18;
364 ci->advance_buffer(18);
365 sampleswritten--; /* go back to first channel's next sample */
368 if (fade_count>0) {
369 fade_count--;
370 for (i=0;i<(channels==1?32:64);i++) samples[sampleswritten-i-1]=
371 ((int32_t)samples[sampleswritten-i-1])*fade_count/fade_frames;
372 if (fade_count==0) {endofstream=1; break;}
376 if (channels == 2)
377 sampleswritten >>= 1; /* make samples/channel */
379 ci->pcmbuf_insert(samples, NULL, sampleswritten);
381 ci->set_elapsed(
382 ((end_adr-start_adr)*loop_count + bufoff-chanstart)*
383 1000LL/avgbytespersec);
386 if (ci->request_next_track())
387 goto next_track;
389 return CODEC_OK;