2005-06-22 19:41:30 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
2005-10-12 16:25:59 +00:00
|
|
|
#include <codecs/libmad/mad.h>
|
2005-08-16 18:26:41 +00:00
|
|
|
#include <inttypes.h>
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2005-10-27 11:32:02 +00:00
|
|
|
struct mad_stream stream IBSS_ATTR;
|
|
|
|
struct mad_frame frame IBSS_ATTR;
|
|
|
|
struct mad_synth synth IBSS_ATTR;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
|
|
/* The following function is used inside libmad - let's hope it's never
|
|
|
|
called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void abort(void) {
|
|
|
|
}
|
|
|
|
|
2005-06-22 21:18:05 +00:00
|
|
|
#define INPUT_CHUNK_SIZE 8192
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2005-10-27 11:32:02 +00:00
|
|
|
mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
|
|
|
|
unsigned char mad_main_data[MAD_BUFFER_MDLEN] IBSS_ATTR;
|
2005-06-22 19:41:30 +00:00
|
|
|
/* TODO: what latency does layer 1 have? */
|
|
|
|
int mpeg_latency[3] = { 0, 481, 529 };
|
2006-05-20 09:57:55 +00:00
|
|
|
int mpeg_framesize[3] = {384, 1152, 1152};
|
2005-10-12 16:25:59 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
#ifdef USE_IRAM
|
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
2006-01-08 22:50:14 +00:00
|
|
|
extern char iedata[];
|
|
|
|
extern char iend[];
|
2005-06-22 19:41:30 +00:00
|
|
|
#endif
|
|
|
|
|
2005-08-10 22:59:06 +00:00
|
|
|
struct codec_api *ci;
|
|
|
|
|
2006-01-22 10:25:07 +00:00
|
|
|
void init_mad(void)
|
|
|
|
{
|
|
|
|
ci->memset(&stream, 0, sizeof(struct mad_stream));
|
|
|
|
ci->memset(&frame, 0, sizeof(struct mad_frame));
|
|
|
|
ci->memset(&synth, 0, sizeof(struct mad_synth));
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2006-01-22 10:25:07 +00:00
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_frame_init(&frame);
|
|
|
|
mad_synth_init(&synth);
|
|
|
|
|
|
|
|
/* We do this so libmad doesn't try to call codec_calloc() */
|
|
|
|
ci->memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
|
|
|
|
frame.overlap = &mad_frame_overlap;
|
|
|
|
stream.main_data = &mad_main_data;
|
|
|
|
}
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2005-10-12 16:25:59 +00:00
|
|
|
enum codec_status codec_start(struct codec_api *api)
|
2005-06-22 19:41:30 +00:00
|
|
|
{
|
2006-04-22 14:40:13 +00:00
|
|
|
int status;
|
2006-03-24 14:02:27 +00:00
|
|
|
size_t size;
|
2005-06-22 19:41:30 +00:00
|
|
|
int file_end;
|
2005-11-02 19:02:25 +00:00
|
|
|
int samples_to_skip; /* samples to skip in total for this file (at start) */
|
2005-10-12 16:25:59 +00:00
|
|
|
char *inputbuffer;
|
2006-05-20 09:57:55 +00:00
|
|
|
int64_t samplesdone;
|
|
|
|
int stop_skip, start_skip;
|
|
|
|
int current_stereo_mode = -1;
|
|
|
|
unsigned long current_frequency = 0;
|
|
|
|
int framelength;
|
|
|
|
int padding = MAD_BUFFER_GUARD; /* to help mad decode the last frame */
|
2005-08-10 22:59:06 +00:00
|
|
|
|
|
|
|
ci = api;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
|
|
#ifdef USE_IRAM
|
2005-06-22 21:18:05 +00:00
|
|
|
ci->memcpy(iramstart, iramcopy, iramend - iramstart);
|
2006-01-08 22:50:14 +00:00
|
|
|
ci->memset(iedata, 0, iend - iedata);
|
2005-06-22 19:41:30 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
if (codec_init(api))
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
|
|
|
|
/* Create a decoder instance */
|
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
ci->configure(DSP_DITHER, (bool *)false);
|
2005-11-02 19:15:25 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(MAD_F_FRACBITS));
|
2005-06-26 19:41:29 +00:00
|
|
|
ci->configure(DSP_SET_CLIP_MIN, (int *)-MAD_F_ONE);
|
|
|
|
ci->configure(DSP_SET_CLIP_MAX, (int *)(MAD_F_ONE - 1));
|
2005-10-12 16:25:59 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16));
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
next_track:
|
2006-04-22 14:40:13 +00:00
|
|
|
status = CODEC_OK;
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
/* Reinitializing seems to be necessary to avoid playback quircks when seeking. */
|
2006-01-22 10:25:07 +00:00
|
|
|
init_mad();
|
2005-10-12 16:25:59 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
file_end = 0;
|
2005-07-10 20:37:36 +00:00
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
2005-07-11 18:47:47 +00:00
|
|
|
ci->sleep(1);
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-06-26 19:41:29 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, (int *)ci->id3->frequency);
|
2005-08-10 22:59:06 +00:00
|
|
|
current_frequency = ci->id3->frequency;
|
2005-07-27 11:54:33 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
ci->request_buffer(&size, ci->id3->first_frame_offset);
|
|
|
|
ci->advance_buffer(size);
|
2005-07-05 19:55:40 +00:00
|
|
|
|
|
|
|
if (ci->id3->lead_trim >= 0 && ci->id3->tail_trim >= 0) {
|
|
|
|
stop_skip = ci->id3->tail_trim - mpeg_latency[ci->id3->layer];
|
2005-06-22 19:41:30 +00:00
|
|
|
if (stop_skip < 0) stop_skip = 0;
|
2005-07-05 19:55:40 +00:00
|
|
|
start_skip = ci->id3->lead_trim + mpeg_latency[ci->id3->layer];
|
2005-06-22 19:41:30 +00:00
|
|
|
} else {
|
|
|
|
stop_skip = 0;
|
|
|
|
/* We want to skip this amount anyway */
|
2005-07-05 19:55:40 +00:00
|
|
|
start_skip = mpeg_latency[ci->id3->layer];
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
2005-07-27 11:54:33 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
/* Libmad will not decode the last frame without 8 bytes of extra padding
|
|
|
|
in the buffer. So, we can trick libmad into not decoding the last frame
|
|
|
|
if we are to skip it entirely and then cut the appropriate samples from
|
|
|
|
final frame that we did decode. Note, if all tags (ID3, APE) are not
|
|
|
|
properly stripped from the end of the file, this trick will not work. */
|
|
|
|
if (stop_skip >= mpeg_framesize[ci->id3->layer]) {
|
|
|
|
padding = 0;
|
|
|
|
stop_skip -= mpeg_framesize[ci->id3->layer];
|
|
|
|
} else {
|
|
|
|
padding = MAD_BUFFER_GUARD;
|
|
|
|
}
|
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
samplesdone = ((int64_t)ci->id3->elapsed) * current_frequency / 1000;
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
/* Don't skip any samples unless we start at the beginning. */
|
|
|
|
if (samplesdone > 0)
|
|
|
|
samples_to_skip = 0;
|
|
|
|
else
|
|
|
|
samples_to_skip = start_skip;
|
|
|
|
|
|
|
|
framelength = 0;
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* This is the decoding loop. */
|
|
|
|
while (1) {
|
2005-06-22 21:18:05 +00:00
|
|
|
ci->yield();
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track)
|
2005-10-12 16:25:59 +00:00
|
|
|
break;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
|
|
|
if (ci->seek_time) {
|
|
|
|
int newpos;
|
2006-01-20 22:02:44 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
samplesdone = ((int64_t)(ci->seek_time-1))*current_frequency/1000;
|
|
|
|
|
|
|
|
if (ci->seek_time-1 == 0) {
|
2006-05-01 11:54:04 +00:00
|
|
|
newpos = ci->id3->first_frame_offset;
|
2006-05-20 09:57:55 +00:00
|
|
|
samples_to_skip = start_skip;
|
|
|
|
} else {
|
2006-05-01 11:54:04 +00:00
|
|
|
newpos = ci->mp3_get_filepos(ci->seek_time-1);
|
2006-05-20 09:57:55 +00:00
|
|
|
samples_to_skip = 0;
|
|
|
|
}
|
2005-08-10 22:59:06 +00:00
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
if (!ci->seek_buffer(newpos))
|
2006-04-22 14:40:13 +00:00
|
|
|
break;
|
2005-08-21 18:12:31 +00:00
|
|
|
ci->seek_complete();
|
2006-01-22 10:25:07 +00:00
|
|
|
init_mad();
|
2006-05-20 09:57:55 +00:00
|
|
|
framelength = 0;
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock buffers */
|
2005-10-12 16:25:59 +00:00
|
|
|
if (stream.error == 0) {
|
|
|
|
inputbuffer = ci->request_buffer(&size, INPUT_CHUNK_SIZE);
|
|
|
|
if (size == 0 || inputbuffer == NULL)
|
|
|
|
break;
|
2006-04-22 15:13:53 +00:00
|
|
|
mad_stream_buffer(&stream, (unsigned char *)inputbuffer,
|
2006-05-20 09:57:55 +00:00
|
|
|
size + padding);
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-11-02 19:02:25 +00:00
|
|
|
if (mad_frame_decode(&frame, &stream)) {
|
2005-10-12 16:25:59 +00:00
|
|
|
if (stream.error == MAD_FLAG_INCOMPLETE
|
|
|
|
|| stream.error == MAD_ERROR_BUFLEN) {
|
|
|
|
/* This makes the codec support partially corrupted files */
|
2005-06-22 19:41:30 +00:00
|
|
|
if (file_end == 30)
|
2005-10-12 16:25:59 +00:00
|
|
|
break;
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* Fill the buffer */
|
2006-04-22 14:48:05 +00:00
|
|
|
if (stream.next_frame)
|
2005-12-22 21:53:21 +00:00
|
|
|
ci->advance_buffer_loc((void *)stream.next_frame);
|
|
|
|
else
|
|
|
|
ci->advance_buffer(size);
|
2005-10-12 16:25:59 +00:00
|
|
|
stream.error = 0;
|
2005-06-22 19:41:30 +00:00
|
|
|
file_end++;
|
2005-10-12 16:25:59 +00:00
|
|
|
continue;
|
|
|
|
} else if (MAD_RECOVERABLE(stream.error)) {
|
2005-06-22 19:41:30 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2005-11-02 19:02:25 +00:00
|
|
|
/* Some other unrecoverable error */
|
2006-01-18 20:22:03 +00:00
|
|
|
status = CODEC_ERROR;
|
2005-06-22 19:41:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-10-12 16:25:59 +00:00
|
|
|
break;
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-11-02 19:02:25 +00:00
|
|
|
file_end = 0;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
/* Do the pcmbuf insert here. Note, this is the PREVIOUS frame's pcm
|
|
|
|
data (not the one just decoded above). When we exit the decoding
|
|
|
|
loop we will need to process the final frame that was decoded. */
|
|
|
|
if (framelength > 0) {
|
|
|
|
/* In case of a mono file, the second array will be ignored. */
|
|
|
|
ci->pcmbuf_insert_split(&synth.pcm.samples[0][samples_to_skip],
|
|
|
|
&synth.pcm.samples[1][samples_to_skip],
|
|
|
|
framelength * 4);
|
|
|
|
|
|
|
|
/* Only skip samples for the first frame added. */
|
2005-11-02 19:02:25 +00:00
|
|
|
samples_to_skip = 0;
|
2005-08-13 08:02:38 +00:00
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
mad_synth_frame(&synth, &frame);
|
|
|
|
|
2005-11-02 19:02:25 +00:00
|
|
|
/* Check if sample rate and stereo settings changed in this frame. */
|
|
|
|
if (frame.header.samplerate != current_frequency) {
|
|
|
|
current_frequency = frame.header.samplerate;
|
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, (int *)current_frequency);
|
|
|
|
}
|
2005-10-12 16:25:59 +00:00
|
|
|
if (MAD_NCHANNELS(&frame.header) == 2) {
|
2005-07-02 16:52:30 +00:00
|
|
|
if (current_stereo_mode != STEREO_NONINTERLEAVED) {
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_NONINTERLEAVED);
|
|
|
|
current_stereo_mode = STEREO_NONINTERLEAVED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (current_stereo_mode != STEREO_MONO) {
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO);
|
|
|
|
current_stereo_mode = STEREO_MONO;
|
|
|
|
}
|
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
if (stream.next_frame)
|
|
|
|
ci->advance_buffer_loc((void *)stream.next_frame);
|
2005-07-07 07:15:05 +00:00
|
|
|
else
|
|
|
|
ci->advance_buffer(size);
|
2005-08-13 08:02:38 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
framelength = synth.pcm.length - samples_to_skip;
|
|
|
|
if (framelength < 0) {
|
|
|
|
framelength = 0;
|
|
|
|
samples_to_skip -= synth.pcm.length;
|
|
|
|
}
|
|
|
|
|
2005-08-13 08:02:38 +00:00
|
|
|
samplesdone += framelength;
|
2005-08-16 18:26:41 +00:00
|
|
|
ci->set_elapsed(samplesdone / (current_frequency / 1000));
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
/* Finish the remaining decoded frame.
|
|
|
|
Cut the required samples from the end. */
|
|
|
|
if (framelength > stop_skip)
|
|
|
|
ci->pcmbuf_insert_split(synth.pcm.samples[0], synth.pcm.samples[1],
|
|
|
|
(framelength - stop_skip) * 4);
|
|
|
|
|
2005-10-12 16:25:59 +00:00
|
|
|
stream.error = 0;
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
return status;
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|