1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Dave Chapman
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 ****************************************************************************/
23 #include <codecs/demac/libdemac/demac.h>
27 #define BLOCKS_PER_LOOP 1024
28 #define MAX_CHANNELS 2
29 #define MAX_BYTESPERSAMPLE 3
31 /* Monkey's Audio files have one seekpoint per frame. The framesize
32 varies between 73728 and 1179648 samples.
34 At the smallest framesize, 30000 frames would be 50155 seconds of
35 audio - almost 14 hours. This should be enough for any file a user
36 would want to play in Rockbox, given the 2GB FAT filesize (and 4GB
37 seektable entry size) limit.
39 This means the seektable is 120000 bytes, but we have a lot of
40 spare room in the codec buffer - the APE codec itself is small.
43 #define MAX_SEEKPOINTS 30000
44 static uint32_t seektablebuf
[MAX_SEEKPOINTS
];
46 #define INPUT_CHUNKSIZE (32*1024)
48 /* 4608*4 = 18432 bytes per channel */
49 static int32_t decoded0
[BLOCKS_PER_LOOP
] IBSS_ATTR
;
50 static int32_t decoded1
[BLOCKS_PER_LOOP
] IBSS_ATTR
;
52 #define MAX_SUPPORTED_SEEKTABLE_SIZE 5000
55 /* Given an ape_ctx and a sample to seek to, return the file position
56 to the frame containing that sample, and the number of samples to
60 bool ape_calc_seekpos(struct ape_ctx_t
* ape_ctx
,
64 uint32_t* samplestoskip
)
68 n
= new_sample
/ ape_ctx
->blocksperframe
;
69 if (n
>= ape_ctx
->numseekpoints
)
71 /* We don't have a seekpoint for that frame */
76 *filepos
= ape_ctx
->seektable
[n
];
77 *samplestoskip
= new_sample
- (n
* ape_ctx
->blocksperframe
);
82 /* The resume offset is a value in bytes - we need to
83 turn it into a frame number and samplestoskip value */
85 void ape_resume(struct ape_ctx_t
* ape_ctx
, size_t resume_offset
,
86 uint32_t* currentframe
, uint32_t* samplesdone
,
87 uint32_t* samplestoskip
, int* firstbyte
)
97 while ((*currentframe
< ape_ctx
->totalframes
) &&
98 (*currentframe
< ape_ctx
->numseekpoints
) &&
99 (resume_offset
> ape_ctx
->seektable
[*currentframe
]))
102 *samplesdone
+= ape_ctx
->blocksperframe
;
105 if ((*currentframe
> 0) &&
106 (ape_ctx
->seektable
[*currentframe
] > resume_offset
)) {
108 *samplesdone
-= ape_ctx
->blocksperframe
;
111 newfilepos
= ape_ctx
->seektable
[*currentframe
];
113 /* APE's bytestream is weird... */
114 *firstbyte
= 3 - (newfilepos
& 3);
117 ci
->seek_buffer(newfilepos
);
119 /* We estimate where we were in the current frame, based on the
121 if (*currentframe
< (ape_ctx
->totalframes
- 1)) {
122 framesize
= ape_ctx
->seektable
[*currentframe
+1] - ape_ctx
->seektable
[*currentframe
];
123 offset
= resume_offset
- ape_ctx
->seektable
[*currentframe
];
125 *samplestoskip
= (offset
* ape_ctx
->blocksperframe
) / framesize
;
129 /* this is the codec entry point */
130 enum codec_status
codec_main(void)
132 struct ape_ctx_t ape_ctx
;
133 uint32_t samplesdone
;
134 uint32_t elapsedtime
;
138 uint32_t currentframe
;
140 uint32_t samplestoskip
;
143 unsigned char* inbuffer
;
144 uint32_t blockstodecode
;
147 size_t resume_offset
;
149 /* Generic codec initialisation */
150 ci
->configure(CODEC_SET_FILEBUF_WATERMARK
, 1024*512);
152 ci
->configure(DSP_SET_SAMPLE_DEPTH
, APE_OUTPUT_DEPTH
-1);
158 /* Remember the resume position - when the codec is opened, the
159 playback engine will reset it. */
160 resume_offset
= ci
->id3
->offset
;
163 LOGF("APE: Error initialising codec\n");
164 retval
= CODEC_ERROR
;
168 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
171 inbuffer
= ci
->request_buffer(&bytesleft
, INPUT_CHUNKSIZE
);
173 /* Read the file headers to populate the ape_ctx struct */
174 if (ape_parseheaderbuf(inbuffer
,&ape_ctx
) < 0) {
175 LOGF("APE: Error reading header\n");
176 retval
= CODEC_ERROR
;
180 /* Initialise the seektable for this file */
181 ape_ctx
.seektable
= seektablebuf
;
182 ape_ctx
.numseekpoints
= MIN(MAX_SEEKPOINTS
,ape_ctx
.numseekpoints
);
184 ci
->advance_buffer(ape_ctx
.seektablefilepos
);
186 /* The seektable may be bigger than the guard buffer (32KB), so we
188 ci
->read_filebuf(ape_ctx
.seektable
, ape_ctx
.numseekpoints
* sizeof(uint32_t));
190 #ifdef ROCKBOX_BIG_ENDIAN
191 /* Byte-swap the little-endian seekpoints */
195 for(i
= 0; i
< ape_ctx
.numseekpoints
; i
++)
196 ape_ctx
.seektable
[i
] = swap32(ape_ctx
.seektable
[i
]);
200 /* Now advance the file position to the first frame */
201 ci
->advance_buffer(ape_ctx
.firstframe
-
202 (ape_ctx
.seektablefilepos
+
203 ape_ctx
.numseekpoints
* sizeof(uint32_t)));
205 ci
->configure(DSP_SWITCH_FREQUENCY
, ape_ctx
.samplerate
);
206 ci
->configure(DSP_SET_STEREO_MODE
, ape_ctx
.channels
== 1 ?
207 STEREO_MONO
: STEREO_NONINTERLEAVED
);
208 codec_set_replaygain(ci
->id3
);
210 /* The main decoding loop */
213 /* The resume offset is a value in bytes - we need to
214 turn it into a frame number and samplestoskip value */
216 ape_resume(&ape_ctx
, resume_offset
,
217 ¤tframe
, &samplesdone
, &samplestoskip
, &firstbyte
);
222 firstbyte
= 3; /* Take account of the little-endian 32-bit byte ordering */
225 /* Initialise the buffer */
226 inbuffer
= ci
->request_buffer(&bytesleft
, INPUT_CHUNKSIZE
);
228 /* The main decoding loop - we decode the frames a small chunk at a time */
229 while (currentframe
< ape_ctx
.totalframes
)
232 /* Calculate how many blocks there are in this frame */
233 if (currentframe
== (ape_ctx
.totalframes
- 1))
234 nblocks
= ape_ctx
.finalframeblocks
;
236 nblocks
= ape_ctx
.blocksperframe
;
238 ape_ctx
.currentframeblocks
= nblocks
;
240 /* Initialise the frame decoder */
241 init_frame_decoder(&ape_ctx
, inbuffer
, &firstbyte
, &bytesconsumed
);
243 ci
->advance_buffer(bytesconsumed
);
244 inbuffer
= ci
->request_buffer(&bytesleft
, INPUT_CHUNKSIZE
);
246 /* Decode the frame a chunk at a time */
250 if (ci
->stop_codec
|| ci
->new_track
) {
254 /* Deal with any pending seek requests */
257 if (ape_calc_seekpos(&ape_ctx
,
258 ((ci
->seek_time
-1)/10) * (ci
->id3
->frequency
/100),
263 samplesdone
= currentframe
* ape_ctx
.blocksperframe
;
265 /* APE's bytestream is weird... */
266 firstbyte
= 3 - (newfilepos
& 3);
269 ci
->seek_buffer(newfilepos
);
270 inbuffer
= ci
->request_buffer(&bytesleft
, INPUT_CHUNKSIZE
);
273 goto frame_start
; /* Sorry... */
278 blockstodecode
= MIN(BLOCKS_PER_LOOP
, nblocks
);
280 if ((res
= decode_chunk(&ape_ctx
, inbuffer
, &firstbyte
,
283 blockstodecode
)) < 0)
285 /* Frame decoding error, abort */
286 LOGF("APE: Frame %d, error %d\n",currentframe
,res
);
287 retval
= CODEC_ERROR
;
293 if (samplestoskip
> 0) {
294 if (samplestoskip
< blockstodecode
) {
295 ci
->pcmbuf_insert(decoded0
+ samplestoskip
,
296 decoded1
+ samplestoskip
,
297 blockstodecode
- samplestoskip
);
300 samplestoskip
-= blockstodecode
;
303 ci
->pcmbuf_insert(decoded0
, decoded1
, blockstodecode
);
306 samplesdone
+= blockstodecode
;
308 if (!samplestoskip
) {
309 /* Update the elapsed-time indicator */
310 elapsedtime
= (samplesdone
*10)/(ape_ctx
.samplerate
/100);
311 ci
->set_elapsed(elapsedtime
);
314 ci
->advance_buffer(bytesconsumed
);
315 inbuffer
= ci
->request_buffer(&bytesleft
, INPUT_CHUNKSIZE
);
317 /* Decrement the block count */
318 nblocks
-= blockstodecode
;
327 LOGF("APE: Decoded %ld samples\n",samplesdone
);
329 if (ci
->request_next_track())