2005-06-22 19:41:30 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-06-22 19:41:30 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
#if NUM_CORES > 1 && !defined(MPEGPLAYER)
|
2008-09-20 22:06:12 +00:00
|
|
|
#define MPA_SYNTH_ON_COP
|
|
|
|
#endif
|
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static struct mad_stream stream IBSS_ATTR;
|
|
|
|
static struct mad_frame frame IBSS_ATTR;
|
|
|
|
static struct mad_synth synth IBSS_ATTR;
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2008-09-20 22:06:12 +00:00
|
|
|
#ifdef MPA_SYNTH_ON_COP
|
2010-07-25 22:24:53 +00:00
|
|
|
static volatile short die IBSS_ATTR = 0; /*thread should die*/
|
2008-09-20 22:06:12 +00:00
|
|
|
|
|
|
|
#if (CONFIG_CPU == PP5024) || (CONFIG_CPU == PP5022)
|
2010-07-25 22:24:53 +00:00
|
|
|
static mad_fixed_t sbsample_prev[2][36][32] IBSS_ATTR;
|
2008-09-20 22:06:12 +00:00
|
|
|
#else
|
2010-07-25 22:24:53 +00:00
|
|
|
static mad_fixed_t sbsample_prev[2][36][32] SHAREDBSS_ATTR;
|
2008-09-20 22:06:12 +00:00
|
|
|
#endif
|
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static struct semaphore synth_done_sem IBSS_ATTR;
|
|
|
|
static struct semaphore synth_pending_sem IBSS_ATTR;
|
2008-09-20 22:06:12 +00:00
|
|
|
#endif
|
|
|
|
|
2005-06-22 21:18:05 +00:00
|
|
|
#define INPUT_CHUNK_SIZE 8192
|
2005-06-22 19:41:30 +00:00
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
|
|
|
|
static mad_fixed_t sbsample[2][36][32] IBSS_ATTR;
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static 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? */
|
2010-07-25 22:24:53 +00:00
|
|
|
static int mpeg_latency[3] = { 0, 481, 529 };
|
|
|
|
static int mpeg_framesize[3] = {384, 1152, 1152};
|
2005-10-12 16:25:59 +00:00
|
|
|
|
2008-12-29 19:49:48 +00:00
|
|
|
static void init_mad(void)
|
2006-01-22 10:25:07 +00:00
|
|
|
{
|
|
|
|
ci->memset(&stream, 0, sizeof(struct mad_stream));
|
2010-06-27 18:11:07 +00:00
|
|
|
ci->memset(&frame , 0, sizeof(struct mad_frame));
|
|
|
|
ci->memset(&synth , 0, sizeof(struct mad_synth));
|
2008-09-22 01:29:50 +00:00
|
|
|
|
2008-09-20 22:06:12 +00:00
|
|
|
#ifdef MPA_SYNTH_ON_COP
|
|
|
|
frame.sbsample_prev = &sbsample_prev;
|
2010-06-27 18:11:07 +00:00
|
|
|
frame.sbsample = &sbsample;
|
2008-09-20 22:06:12 +00:00
|
|
|
#else
|
|
|
|
frame.sbsample_prev = &sbsample;
|
2010-06-27 18:11:07 +00:00
|
|
|
frame.sbsample = &sbsample;
|
2008-09-20 22:06:12 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-27 18:11:07 +00:00
|
|
|
/* We do this so libmad doesn't try to call codec_calloc(). This needs to
|
|
|
|
* be called before mad_stream_init(), mad_frame_inti() and
|
|
|
|
* mad_synth_init(). */
|
|
|
|
frame.overlap = &mad_frame_overlap;
|
|
|
|
stream.main_data = &mad_main_data;
|
|
|
|
|
|
|
|
/* Call mad initialization. Those will zero the arrays frame.overlap,
|
|
|
|
* frame.sbsample and frame.sbsample_prev. Therefore there is no need to
|
|
|
|
* zero them here. */
|
2006-01-22 10:25:07 +00:00
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_frame_init(&frame);
|
|
|
|
mad_synth_init(&synth);
|
|
|
|
}
|
|
|
|
|
2008-12-29 19:49:48 +00:00
|
|
|
static int get_file_pos(int newtime)
|
2008-03-29 06:36:53 +00:00
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
struct mp3entry *id3 = ci->id3;
|
|
|
|
|
|
|
|
if (id3->vbr) {
|
2009-04-20 19:10:27 +00:00
|
|
|
/* Convert newtime and id3->length to seconds to
|
|
|
|
* avoid overflow */
|
|
|
|
unsigned int newtime_s = newtime/1000;
|
|
|
|
unsigned int length_s = id3->length/1000;
|
|
|
|
|
2008-03-29 06:36:53 +00:00
|
|
|
if (id3->has_toc) {
|
|
|
|
/* Use the TOC to find the new position */
|
|
|
|
unsigned int percent, remainder;
|
|
|
|
int curtoc, nexttoc, plen;
|
|
|
|
|
2009-04-20 19:10:27 +00:00
|
|
|
percent = (newtime_s*100) / length_s;
|
2008-03-29 06:36:53 +00:00
|
|
|
if (percent > 99)
|
|
|
|
percent = 99;
|
|
|
|
|
|
|
|
curtoc = id3->toc[percent];
|
|
|
|
|
|
|
|
if (percent < 99) {
|
|
|
|
nexttoc = id3->toc[percent+1];
|
|
|
|
} else {
|
|
|
|
nexttoc = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = (id3->filesize/256)*curtoc;
|
|
|
|
|
|
|
|
/* Use the remainder to get a more accurate position */
|
2009-04-20 19:10:27 +00:00
|
|
|
remainder = (newtime_s*100) % length_s;
|
|
|
|
remainder = (remainder*100) / length_s;
|
2008-03-29 06:36:53 +00:00
|
|
|
plen = (nexttoc - curtoc)*(id3->filesize/256);
|
|
|
|
pos += (plen/100)*remainder;
|
|
|
|
} else {
|
|
|
|
/* No TOC exists, estimate the new position */
|
2009-04-20 19:10:27 +00:00
|
|
|
pos = (id3->filesize / length_s) * newtime_s;
|
2008-03-29 06:36:53 +00:00
|
|
|
}
|
|
|
|
} else if (id3->bitrate) {
|
|
|
|
pos = newtime * (id3->bitrate / 8);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't seek right to the end of the file so that we can
|
|
|
|
transition properly to the next song */
|
|
|
|
if (pos >= (int)(id3->filesize - id3->id3v1len))
|
|
|
|
pos = id3->filesize - id3->id3v1len - 1;
|
|
|
|
|
2009-08-03 20:28:16 +00:00
|
|
|
/* id3->filesize excludes id3->first_frame_offset, so add it now */
|
|
|
|
pos += id3->first_frame_offset;
|
|
|
|
|
2008-03-29 06:36:53 +00:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2008-04-03 12:13:03 +00:00
|
|
|
static void set_elapsed(struct mp3entry* id3)
|
|
|
|
{
|
|
|
|
unsigned long offset = id3->offset > id3->first_frame_offset ?
|
|
|
|
id3->offset - id3->first_frame_offset : 0;
|
|
|
|
|
|
|
|
if ( id3->vbr ) {
|
|
|
|
if ( id3->has_toc ) {
|
|
|
|
/* calculate elapsed time using TOC */
|
|
|
|
int i;
|
|
|
|
unsigned int remainder, plen, relpos, nextpos;
|
|
|
|
|
|
|
|
/* find wich percent we're at */
|
|
|
|
for (i=0; i<100; i++ )
|
|
|
|
if ( offset < id3->toc[i] * (id3->filesize / 256) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
i--;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
relpos = id3->toc[i];
|
|
|
|
|
|
|
|
if (i < 99)
|
|
|
|
nextpos = id3->toc[i+1];
|
|
|
|
else
|
|
|
|
nextpos = 256;
|
|
|
|
|
|
|
|
remainder = offset - (relpos * (id3->filesize / 256));
|
|
|
|
|
|
|
|
/* set time for this percent (divide before multiply to prevent
|
|
|
|
overflow on long files. loss of precision is negligible on
|
|
|
|
short files) */
|
|
|
|
id3->elapsed = i * (id3->length / 100);
|
|
|
|
|
|
|
|
/* calculate remainder time */
|
|
|
|
plen = (nextpos - relpos) * (id3->filesize / 256);
|
|
|
|
id3->elapsed += (((remainder * 100) / plen) *
|
|
|
|
(id3->length / 10000));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no TOC exists. set a rough estimate using average bitrate */
|
|
|
|
int tpk = id3->length /
|
|
|
|
((id3->filesize - id3->first_frame_offset - id3->id3v1len) /
|
|
|
|
1024);
|
|
|
|
id3->elapsed = offset / 1024 * tpk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* constant bitrate, use exact calculation */
|
|
|
|
if (id3->bitrate != 0)
|
|
|
|
id3->elapsed = offset / (id3->bitrate / 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-20 22:06:12 +00:00
|
|
|
#ifdef MPA_SYNTH_ON_COP
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the synthesis filter on the COProcessor
|
|
|
|
*/
|
|
|
|
|
2010-06-10 19:02:27 +00:00
|
|
|
static int mad_synth_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)] IBSS_ATTR;
|
2008-09-20 22:06:12 +00:00
|
|
|
|
|
|
|
static const unsigned char * const mad_synth_thread_name = "mp3dec";
|
2008-12-10 08:57:10 +00:00
|
|
|
static unsigned int mad_synth_thread_id = 0;
|
2008-09-20 22:06:12 +00:00
|
|
|
|
|
|
|
|
2008-09-23 09:23:32 +00:00
|
|
|
static void mad_synth_thread(void)
|
|
|
|
{
|
|
|
|
while(1) {
|
2008-09-23 08:09:04 +00:00
|
|
|
ci->semaphore_release(&synth_done_sem);
|
2008-09-20 22:06:12 +00:00
|
|
|
ci->semaphore_wait(&synth_pending_sem);
|
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
if(die)
|
|
|
|
break;
|
|
|
|
|
2008-09-20 22:06:12 +00:00
|
|
|
mad_synth_frame(&synth, &frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
/* wait for the synth thread to go idle which indicates a PCM frame has been
|
|
|
|
* synthesized */
|
2008-09-23 09:23:32 +00:00
|
|
|
static inline void mad_synth_thread_wait_pcm(void)
|
|
|
|
{
|
2008-09-20 22:06:12 +00:00
|
|
|
ci->semaphore_wait(&synth_done_sem);
|
|
|
|
}
|
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
/* increment the done semaphore - used after a wait for idle to preserve the
|
|
|
|
* semaphore count */
|
2008-09-23 09:23:32 +00:00
|
|
|
static inline void mad_synth_thread_unwait_pcm(void)
|
|
|
|
{
|
2008-09-23 08:09:04 +00:00
|
|
|
ci->semaphore_release(&synth_done_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* after synth thread has gone idle - switch decoded frames and commence
|
|
|
|
* synthesis on it */
|
2008-09-23 09:23:32 +00:00
|
|
|
static void mad_synth_thread_ready(void)
|
|
|
|
{
|
2008-09-20 22:06:12 +00:00
|
|
|
mad_fixed_t (*temp)[2][36][32];
|
2008-09-23 08:09:04 +00:00
|
|
|
|
|
|
|
/*circular buffer that holds 2 frames' samples*/
|
|
|
|
temp=frame.sbsample;
|
|
|
|
frame.sbsample = frame.sbsample_prev;
|
|
|
|
frame.sbsample_prev=temp;
|
|
|
|
|
|
|
|
ci->semaphore_release(&synth_pending_sem);
|
2008-09-20 22:06:12 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 09:23:32 +00:00
|
|
|
static bool mad_synth_thread_create(void)
|
|
|
|
{
|
2008-09-23 08:09:04 +00:00
|
|
|
ci->semaphore_init(&synth_done_sem, 1, 0);
|
|
|
|
ci->semaphore_init(&synth_pending_sem, 1, 0);
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
mad_synth_thread_id = ci->create_thread(mad_synth_thread,
|
2008-09-20 22:06:12 +00:00
|
|
|
mad_synth_thread_stack,
|
2008-09-23 08:09:04 +00:00
|
|
|
sizeof(mad_synth_thread_stack), 0,
|
|
|
|
mad_synth_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_PLAYBACK)
|
|
|
|
IF_COP(, COP));
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
if (mad_synth_thread_id == 0)
|
2008-09-23 08:09:04 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-23 09:23:32 +00:00
|
|
|
static void mad_synth_thread_quit(void)
|
|
|
|
{
|
2008-09-23 08:09:04 +00:00
|
|
|
/*mop up COP thread*/
|
|
|
|
die=1;
|
|
|
|
ci->semaphore_release(&synth_pending_sem);
|
2008-12-10 08:57:10 +00:00
|
|
|
ci->thread_wait(mad_synth_thread_id);
|
2009-02-11 12:55:51 +00:00
|
|
|
ci->cpucache_invalidate();
|
2008-09-20 22:06:12 +00:00
|
|
|
}
|
|
|
|
#else
|
2008-09-23 09:23:32 +00:00
|
|
|
static inline void mad_synth_thread_ready(void)
|
|
|
|
{
|
2010-02-07 18:38:47 +00:00
|
|
|
mad_synth_frame(&synth, &frame);
|
2008-09-20 22:06:12 +00:00
|
|
|
}
|
2008-09-23 09:23:32 +00:00
|
|
|
|
|
|
|
static inline bool mad_synth_thread_create(void)
|
|
|
|
{
|
2010-02-07 18:38:47 +00:00
|
|
|
return true;
|
2008-09-20 22:06:12 +00:00
|
|
|
}
|
2008-09-23 09:23:32 +00:00
|
|
|
|
|
|
|
static inline void mad_synth_thread_quit(void)
|
|
|
|
{
|
2008-09-23 08:09:04 +00:00
|
|
|
}
|
2008-09-23 09:23:32 +00:00
|
|
|
|
|
|
|
static inline void mad_synth_thread_wait_pcm(void)
|
|
|
|
{
|
2008-09-23 08:09:04 +00:00
|
|
|
}
|
2008-09-23 09:23:32 +00:00
|
|
|
|
|
|
|
static inline void mad_synth_thread_unwait_pcm(void)
|
|
|
|
{
|
2008-09-20 22:06:12 +00:00
|
|
|
}
|
2008-09-23 09:23:32 +00:00
|
|
|
#endif /* MPA_SYNTH_ON_COP */
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2006-11-26 18:31:41 +00:00
|
|
|
enum codec_status codec_main(void)
|
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
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init())
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
|
|
|
|
/* Create a decoder instance */
|
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
|
2008-09-23 08:09:04 +00:00
|
|
|
|
|
|
|
/*does nothing on 1 processor systems except return true*/
|
|
|
|
if(!mad_synth_thread_create())
|
|
|
|
return CODEC_ERROR;
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
next_track:
|
2008-09-20 22:06:12 +00:00
|
|
|
|
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
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, 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
|
|
|
|
2008-04-03 12:13:03 +00:00
|
|
|
if (ci->id3->offset) {
|
2007-11-05 18:15:52 +00:00
|
|
|
ci->seek_buffer(ci->id3->offset);
|
2008-04-03 12:13:03 +00:00
|
|
|
set_elapsed(ci->id3);
|
|
|
|
}
|
2007-11-05 18:15:52 +00:00
|
|
|
else
|
|
|
|
ci->seek_buffer(ci->id3->first_frame_offset);
|
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;
|
2008-09-22 01:29:50 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
if (ci->seek_time) {
|
|
|
|
int newpos;
|
2006-01-20 22:02:44 +00:00
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
/*make sure the synth thread is idle before seeking - MT only*/
|
|
|
|
mad_synth_thread_wait_pcm();
|
|
|
|
mad_synth_thread_unwait_pcm();
|
|
|
|
|
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 {
|
2008-03-29 06:36:53 +00:00
|
|
|
newpos = get_file_pos(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)) {
|
2010-08-01 16:08:25 +00:00
|
|
|
if (stream.error == MAD_ERROR_BUFLEN) {
|
2005-10-12 16:25:59 +00:00
|
|
|
/* 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. */
|
2008-09-23 08:09:04 +00:00
|
|
|
mad_synth_thread_wait_pcm();
|
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
if (framelength > 0) {
|
2008-09-20 22:06:12 +00:00
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
/* In case of a mono file, the second array will be ignored. */
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(&synth.pcm.samples[0][samples_to_skip],
|
|
|
|
&synth.pcm.samples[1][samples_to_skip],
|
|
|
|
framelength);
|
2006-05-20 09:57:55 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
2008-09-23 08:09:04 +00:00
|
|
|
|
|
|
|
/* Initiate PCM synthesis on the COP (MT) or perform it here (ST) */
|
2008-09-20 22:06:12 +00:00
|
|
|
mad_synth_thread_ready();
|
2006-05-20 09:57:55 +00:00
|
|
|
|
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;
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, current_frequency);
|
2005-11-02 19:02:25 +00:00
|
|
|
}
|
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) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
2005-07-02 16:52:30 +00:00
|
|
|
current_stereo_mode = STEREO_NONINTERLEAVED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (current_stereo_mode != STEREO_MONO) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
2005-07-02 16:52:30 +00:00
|
|
|
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;
|
2009-08-03 20:28:16 +00:00
|
|
|
ci->set_elapsed((samplesdone * 1000) / current_frequency);
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|
2006-05-20 09:57:55 +00:00
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
/* wait for synth idle - MT only*/
|
|
|
|
mad_synth_thread_wait_pcm();
|
|
|
|
mad_synth_thread_unwait_pcm();
|
|
|
|
|
2006-05-20 09:57:55 +00:00
|
|
|
/* Finish the remaining decoded frame.
|
|
|
|
Cut the required samples from the end. */
|
2008-09-20 22:06:12 +00:00
|
|
|
if (framelength > stop_skip){
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(synth.pcm.samples[0], synth.pcm.samples[1],
|
|
|
|
framelength - stop_skip);
|
2008-09-23 08:09:04 +00:00
|
|
|
}
|
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
|
|
|
|
2008-09-23 08:09:04 +00:00
|
|
|
/*mop up COP thread - MT only*/
|
|
|
|
mad_synth_thread_quit();
|
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
return status;
|
2005-06-22 19:41:30 +00:00
|
|
|
}
|