Redo my previous segfault fix in a better way.
[Rockbox.git] / apps / codecs / adx.c
blobf558bae135c64f0d958152eb938ca7a11c4dd3ad
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2006-2007 Adam Gashlin (hcs)
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
18 #include "codeclib.h"
19 #include "inttypes.h"
21 CODEC_HEADER
23 /* Maximum number of bytes to process in one iteration */
24 #define WAV_CHUNK_SIZE (1024*2)
26 /* Volume for ADX decoder */
27 #define BASE_VOL 0x2000
29 /* Number of times to loop looped tracks when repeat is disabled */
30 #define LOOP_TIMES 2
32 /* Length of fade-out for looped tracks (milliseconds) */
33 #define FADE_LENGTH 10000L
35 static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
37 /* this is the codec entry point */
38 enum codec_status codec_main(void)
40 int channels;
41 int sampleswritten, i;
42 uint8_t *buf;
43 int32_t ch1_1, ch1_2, ch2_1, ch2_2; /* ADPCM history */
44 size_t n;
45 int endofstream; /* end of stream flag */
46 uint32_t avgbytespersec;
47 int looping; /* looping flag */
48 int loop_count; /* number of loops done so far */
49 int fade_count; /* countdown for fadeout */
50 int fade_frames; /* length of fade in frames */
51 off_t start_adr, end_adr; /* loop points */
52 off_t chanstart, bufoff;
54 /* Generic codec initialisation */
55 /* we only render 16 bits */
56 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
58 next_track:
59 DEBUGF("ADX: next_track\n");
60 if (codec_init()) {
61 return CODEC_ERROR;
63 DEBUGF("ADX: after init\n");
65 /* init history */
66 ch1_1=ch1_2=ch2_1=ch2_2=0;
68 /* wait for track info to load */
69 while (!*ci->taginfo_ready && !ci->stop_codec)
70 ci->sleep(1);
72 codec_set_replaygain(ci->id3);
74 /* Get header */
75 DEBUGF("ADX: request initial buffer\n");
76 ci->seek_buffer(0);
77 buf = ci->request_buffer(&n, 0x38);
78 if (!buf || n < 0x38) {
79 return CODEC_ERROR;
81 bufoff = 0;
82 DEBUGF("ADX: read size = %lx\n",(unsigned long)n);
84 /* Get file header for starting offset, channel count */
86 chanstart = ((buf[2] << 8) | buf[3]) + 4;
87 channels = buf[7];
89 /* useful for seeking and reporting current playback position */
90 avgbytespersec = ci->id3->frequency * 18 * channels / 32;
91 DEBUGF("avgbytespersec=%ld\n",(unsigned long)avgbytespersec);
93 /* Get loop data */
95 looping = 0; start_adr = 0; end_adr = 0;
96 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
97 /* Soul Calibur 2 style (type 03) */
98 DEBUGF("ADX: type 03 found\n");
99 /* check if header is too small for loop data */
100 if (chanstart-6 < 0x2c) looping=0;
101 else {
102 looping = (buf[0x18]) ||
103 (buf[0x19]) ||
104 (buf[0x1a]) ||
105 (buf[0x1b]);
106 end_adr = (buf[0x28]<<24) |
107 (buf[0x29]<<16) |
108 (buf[0x2a]<<8) |
109 (buf[0x2b]);
111 start_adr = (
112 (buf[0x1c]<<24) |
113 (buf[0x1d]<<16) |
114 (buf[0x1e]<<8) |
115 (buf[0x1f])
116 )/32*channels*18+chanstart;
118 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
119 /* Standard (type 04) */
120 DEBUGF("ADX: type 04 found\n");
121 /* check if header is too small for loop data */
122 if (chanstart-6 < 0x38) looping=0;
123 else {
124 looping = (buf[0x24]) ||
125 (buf[0x25]) ||
126 (buf[0x26]) ||
127 (buf[0x27]);
128 end_adr = (buf[0x34]<<24) |
129 (buf[0x35]<<16) |
130 (buf[0x36]<<8) |
131 buf[0x37];
132 start_adr = (
133 (buf[0x28]<<24) |
134 (buf[0x29]<<16) |
135 (buf[0x2a]<<8) |
136 (buf[0x2b])
137 )/32*channels*18+chanstart;
139 } else {
140 DEBUGF("ADX: error, couldn't determine ADX type\n");
141 return CODEC_ERROR;
144 if (looping) {
145 DEBUGF("ADX: looped, start: %lx end: %lx\n",start_adr,end_adr);
146 } else {
147 DEBUGF("ADX: not looped\n");
150 /* advance to first frame */
151 DEBUGF("ADX: first frame at %lx\n",chanstart);
152 bufoff = chanstart;
154 /* get in position */
155 ci->seek_buffer(bufoff);
158 /* setup pcm buffer format */
159 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
160 if (channels == 2) {
161 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
162 } else if (channels == 1) {
163 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
164 } else {
165 DEBUGF("ADX CODEC_ERROR: more than 2 channels\n");
166 return CODEC_ERROR;
169 endofstream = 0;
170 loop_count = 0;
171 fade_count = -1; /* disable fade */
172 fade_frames = 1;
174 /* The main decoder loop */
176 while (!endofstream) {
177 ci->yield();
178 if (ci->stop_codec || ci->new_track) {
179 break;
182 /* do we need to loop? */
183 if (bufoff > end_adr-18*channels && looping) {
184 DEBUGF("ADX: loop!\n");
185 /* check for endless looping */
186 if (ci->global_settings->repeat_mode==REPEAT_ONE) {
187 loop_count=0;
188 fade_count = -1; /* disable fade */
189 } else {
190 /* otherwise start fade after LOOP_TIMES loops */
191 loop_count++;
192 if (loop_count >= LOOP_TIMES && fade_count < 0) {
193 /* frames to fade over */
194 fade_frames = FADE_LENGTH*ci->id3->frequency/32/1000;
195 /* volume relative to fade_frames */
196 fade_count = fade_frames;
197 DEBUGF("ADX: fade_frames = %d\n",fade_frames);
200 bufoff = start_adr;
201 ci->seek_buffer(bufoff);
204 /* do we need to seek? */
205 if (ci->seek_time) {
206 uint32_t newpos;
208 DEBUGF("ADX: seek to %ldms\n",ci->seek_time);
210 endofstream = 0;
211 loop_count = 0;
212 fade_count = -1; /* disable fade */
213 fade_frames = 1;
215 newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
216 / (1000LL*18*channels))*(18*channels);
217 bufoff = chanstart + newpos;
218 while (bufoff > end_adr-18*channels) {
219 bufoff-=end_adr-start_adr;
220 loop_count++;
222 ci->seek_buffer(bufoff);
223 ci->seek_complete();
226 if (bufoff>ci->filesize-channels*18) break; /* End of stream */
228 sampleswritten=0;
230 while (
231 /* Is there data left in the file? */
232 (bufoff <= ci->filesize-(18*channels)) &&
233 /* Is there space in the output buffer? */
234 (sampleswritten <= WAV_CHUNK_SIZE-(32*channels)) &&
235 /* Should we be looping? */
236 ((!looping) || bufoff <= end_adr-18*channels))
238 /* decode first/only channel */
239 int32_t scale;
240 int32_t ch1_0, d;
242 /* fetch a frame */
243 buf = ci->request_buffer(&n, 18);
245 if (!buf || n!=18) {
246 DEBUGF("ADX: couldn't get buffer at %lx\n",
247 bufoff);
248 return CODEC_ERROR;
251 scale = (((buf[0] << 8) | (buf[1])) +1) * BASE_VOL;
253 for (i = 2; i < 18; i++)
255 d = (buf[i] >> 4) & 15;
256 if (d & 8) d-= 16;
257 ch1_0 = (d*scale + 0x7298L*ch1_1 - 0x3350L*ch1_2) >> 14;
258 if (ch1_0 > 32767) ch1_0 = 32767;
259 else if (ch1_0 < -32768) ch1_0 = -32768;
260 samples[sampleswritten] = ch1_0;
261 sampleswritten+=channels;
262 ch1_2 = ch1_1; ch1_1 = ch1_0;
264 d = buf[i] & 15;
265 if (d & 8) d -= 16;
266 ch1_0 = (d*scale + 0x7298L*ch1_1 - 0x3350L*ch1_2) >> 14;
267 if (ch1_0 > 32767) ch1_0 = 32767;
268 else if (ch1_0 < -32768) ch1_0 = -32768;
269 samples[sampleswritten] = ch1_0;
270 sampleswritten+=channels;
271 ch1_2 = ch1_1; ch1_1 = ch1_0;
273 bufoff+=18;
274 ci->advance_buffer(18);
276 if (channels == 2) {
277 /* decode second channel */
278 int32_t scale;
279 int32_t ch2_0, d;
281 buf = ci->request_buffer(&n, 18);
283 if (!buf || n!=18) {
284 DEBUGF("ADX: couldn't get buffer at %lx\n",
285 bufoff);
286 return CODEC_ERROR;
289 scale = (((buf[0] << 8)|(buf[1]))+1)*BASE_VOL;
291 sampleswritten-=63;
293 for (i = 2; i < 18; i++)
295 d = (buf[i] >> 4) & 15;
296 if (d & 8) d-= 16;
297 ch2_0 = (d*scale + 0x7298L*ch2_1 - 0x3350L*ch2_2) >> 14;
298 if (ch2_0 > 32767) ch2_0 = 32767;
299 else if (ch2_0 < -32768) ch2_0 = -32768;
300 samples[sampleswritten] = ch2_0;
301 sampleswritten+=2;
302 ch2_2 = ch2_1; ch2_1 = ch2_0;
304 d = buf[i] & 15;
305 if (d & 8) d -= 16;
306 ch2_0 = (d*scale + 0x7298L*ch2_1 - 0x3350L*ch2_2) >> 14;
307 if (ch2_0 > 32767) ch2_0 = 32767;
308 else if (ch2_0 < -32768) ch2_0 = -32768;
309 samples[sampleswritten] = ch2_0;
310 sampleswritten+=2;
311 ch2_2 = ch2_1; ch2_1 = ch2_0;
313 bufoff+=18;
314 ci->advance_buffer(18);
315 sampleswritten--; /* go back to first channel's next sample */
318 if (fade_count>0) {
319 fade_count--;
320 for (i=0;i<(channels==1?32:64);i++) samples[sampleswritten-i-1]=
321 ((int32_t)samples[sampleswritten-i-1])*fade_count/fade_frames;
322 if (fade_count==0) {endofstream=1; break;}
326 if (channels == 2)
327 sampleswritten >>= 1; /* make samples/channel */
329 ci->pcmbuf_insert(samples, NULL, sampleswritten);
331 ci->set_elapsed(
332 ((end_adr-start_adr)*loop_count + bufoff-chanstart)*
333 1000LL/avgbytespersec);
336 if (ci->request_next_track())
337 goto next_track;
339 return CODEC_OK;