Forward seeking fixed. Some comments added.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6700 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Miika Pekkarinen 2005-06-13 15:26:53 +00:00
parent 7dad7d3a6a
commit d94cba6d0f
3 changed files with 58 additions and 24 deletions

View file

@ -125,19 +125,19 @@ static volatile int buf_widx;
#define MAX_TRACK 10 #define MAX_TRACK 10
struct track_info { struct track_info {
struct mp3entry id3; struct mp3entry id3; /* TAG metadata */
struct mp3info mp3data; struct mp3info mp3data; /* MP3 metadata */
char *codecbuf; char *codecbuf; /* Pointer to codec buffer */
size_t codecsize; size_t codecsize; /* Codec length in bytes */
int codectype; int codectype; /* Codec type (example AFMT_MPA_L3) */
volatile char *filebuf; off_t filerem; /* Remaining bytes of file NOT in buffer */
off_t filerem; off_t filesize; /* File total length */
off_t filesize; off_t filepos; /* Read position of file for next buffer fill */
off_t filepos; off_t start_pos; /* Position to first bytes of file in buffer */
volatile int available; volatile int available; /* Available bytes to read from buffer */
bool taginfo_ready; bool taginfo_ready; /* Is metadata read */
int playlist_offset; int playlist_offset; /* File location in playlist */
}; };
/* Track information (count in file buffer, read/write indexes for /* Track information (count in file buffer, read/write indexes for
@ -286,7 +286,11 @@ void codec_advance_buffer_callback(size_t amount)
if ((int)amount > cur_ti->available) { if ((int)amount > cur_ti->available) {
codecbufused = 0; codecbufused = 0;
buf_ridx = buf_widx; buf_ridx = 0;
buf_widx = 0;
cur_ti->start_pos += amount;
amount -= cur_ti->available;
ci.curpos += cur_ti->available;
cur_ti->available = 0; cur_ti->available = 0;
while ((int)amount < cur_ti->available && !ci.stop_codec) while ((int)amount < cur_ti->available && !ci.stop_codec)
yield(); yield();
@ -343,7 +347,8 @@ bool codec_seek_buffer_callback(off_t newpos)
if (ci.curpos - difference < 0) if (ci.curpos - difference < 0)
difference = ci.curpos; difference = ci.curpos;
if (codecbufused + difference > codecbuflen) { if (ci.curpos - difference < cur_ti->start_pos) {
logf("Seek failed (reload song)");
/* We need to reload the song. FIX THIS! */ /* We need to reload the song. FIX THIS! */
return false; return false;
} }
@ -473,8 +478,9 @@ void audio_fill_file_buffer(void)
fill_bytesleft -= rc; fill_bytesleft -= rc;
} }
tracks[track_widx].filerem -= i;
codecbufused += i; codecbufused += i;
tracks[track_widx].filerem -= i;
tracks[track_widx].start_pos = tracks[track_widx].filepos;
tracks[track_widx].filepos += i; tracks[track_widx].filepos += i;
logf("Done:%d", tracks[track_widx].available); logf("Done:%d", tracks[track_widx].available);
if (tracks[track_widx].filerem == 0) { if (tracks[track_widx].filerem == 0) {
@ -653,7 +659,8 @@ bool audio_load_track(int offset, bool start_play, int peek_offset)
close(fd); close(fd);
return false; return false;
} }
tracks[track_widx].filebuf = &codecbuf[buf_widx]; // tracks[track_widx].filebuf = &codecbuf[buf_widx];
tracks[track_widx].start_pos = 0;
//logf("%s", trackname); //logf("%s", trackname);
logf("Buffering track:%d/%d", track_widx, track_ridx); logf("Buffering track:%d/%d", track_widx, track_ridx);
@ -681,6 +688,7 @@ bool audio_load_track(int offset, bool start_play, int peek_offset)
tracks[track_widx].filepos = offset; tracks[track_widx].filepos = offset;
tracks[track_widx].filerem = tracks[track_widx].filesize - offset; tracks[track_widx].filerem = tracks[track_widx].filesize - offset;
ci.curpos = offset; ci.curpos = offset;
tracks[track_widx].start_pos = offset;
} else { } else {
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
} }
@ -943,7 +951,7 @@ bool audio_load_track(int offset, bool start_play, int peek_offset)
track_changed = true; track_changed = true;
track_count++; track_count++;
i = tracks[track_widx].filepos; i = tracks[track_widx].start_pos;
while (i < size) { while (i < size) {
/* Give codecs some processing time to prevent glitches. */ /* Give codecs some processing time to prevent glitches. */
yield_codecs(); yield_codecs();

View file

@ -20,6 +20,7 @@
#ifndef _AUDIO_H #ifndef _AUDIO_H
#define _AUDIO_H #define _AUDIO_H
/* Supported file types. */
#define AFMT_MPA_L1 0x0001 // MPEG Audio layer 1 #define AFMT_MPA_L1 0x0001 // MPEG Audio layer 1
#define AFMT_MPA_L2 0x0002 // MPEG Audio layer 2 #define AFMT_MPA_L2 0x0002 // MPEG Audio layer 2
#define AFMT_MPA_L3 0x0004 // MPEG Audio layer 3 #define AFMT_MPA_L3 0x0004 // MPEG Audio layer 3
@ -36,6 +37,7 @@
#define AFMT_UNKNOWN 0x1000 // Unknown file format #define AFMT_UNKNOWN 0x1000 // Unknown file format
#define AFMT_WAVPACK 0x2000 // WavPack #define AFMT_WAVPACK 0x2000 // WavPack
/* File buffer configuration keys. */
#define CODEC_SET_FILEBUF_WATERMARK 1 #define CODEC_SET_FILEBUF_WATERMARK 1
#define CODEC_SET_FILEBUF_CHUNKSIZE 2 #define CODEC_SET_FILEBUF_CHUNKSIZE 2
#define CODEC_SET_FILEBUF_LIMIT 3 #define CODEC_SET_FILEBUF_LIMIT 3
@ -43,32 +45,56 @@
/* Not yet implemented. */ /* Not yet implemented. */
#define CODEC_SET_AUDIOBUF_WATERMARK 4 #define CODEC_SET_AUDIOBUF_WATERMARK 4
/* Codec Interface */
struct codec_api { struct codec_api {
off_t filesize; off_t filesize; /* Total file length */
off_t curpos; off_t curpos; /* Current buffer position */
size_t bitspersampe;
/* For gapless mp3 */ /* For gapless mp3 */
struct mp3entry *id3; struct mp3entry *id3; /* TAG metadata pointer */
struct mp3info *mp3data; struct mp3info *mp3data; /* MP3 metadata pointer */
bool *taginfo_ready; bool *taginfo_ready; /* Is metadata read */
/* Codec should periodically check if stop_codec is set to true.
In case it's, codec must return with PLUGIN_OK status immediately. */
bool stop_codec; bool stop_codec;
/* Codec should periodically check if reload_codec is set to true.
In case it's, codec should reload itself without exiting. */
bool reload_codec; bool reload_codec;
/* If seek_time != 0, codec should seek to that song position (in ms)
if codec supports seeking. */
int seek_time; int seek_time;
/* Returns buffer to malloc array. Only codeclib should need this. */
void* (*get_codec_memory)(size_t *size); void* (*get_codec_memory)(size_t *size);
/* Insert PCM data into audio buffer for playback. Playback will start
automatically. */
bool (*audiobuffer_insert)(char *data, size_t length); bool (*audiobuffer_insert)(char *data, size_t length);
/* Set song position in WPS (value in ms). */
void (*set_elapsed)(unsigned int value); void (*set_elapsed)(unsigned int value);
/* Read next <size> amount bytes from file buffer to <ptr>.
Will return number of bytes read or 0 if end of file. */
size_t (*read_filebuf)(void *ptr, size_t size); size_t (*read_filebuf)(void *ptr, size_t size);
/* Request pointer to file buffer which can be used to read
<realsize> amount of data. <reqsize> tells the buffer system
how much data it should try to allocate. If <realsize> is 0,
end of file is reached. */
void* (*request_buffer)(size_t *realsize, size_t reqsize); void* (*request_buffer)(size_t *realsize, size_t reqsize);
/* Advance file buffer position by <amount> amount of bytes. */
void (*advance_buffer)(size_t amount); void (*advance_buffer)(size_t amount);
/* Advance file buffer to a pointer location inside file buffer. */
void (*advance_buffer_loc)(void *ptr); void (*advance_buffer_loc)(void *ptr);
/* Seek file buffer to position <newpos> beginning of file. */
bool (*seek_buffer)(off_t newpos); bool (*seek_buffer)(off_t newpos);
/* Calculate mp3 seek position from given time data in ms. */
off_t (*mp3_get_filepos)(int newtime); off_t (*mp3_get_filepos)(int newtime);
/* Request file change from file buffer. Returns true is next
track is available and changed. If return value is false,
codec should exit immediately with PLUGIN_OK status. */
bool (*request_next_track)(void); bool (*request_next_track)(void);
/* Configure different codec buffer parameters. */
void (*configure)(int setting, void *value); void (*configure)(int setting, void *value);
}; };

View file

@ -305,12 +305,12 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parm)
sample_loc = ci->seek_time/1000 * ci->id3->frequency; sample_loc = ci->seek_time/1000 * ci->id3->frequency;
newpos = ci->mp3_get_filepos(ci->seek_time-1); newpos = ci->mp3_get_filepos(ci->seek_time-1);
if (ci->seek_buffer(newpos)) { if (ci->seek_buffer(newpos)) {
ci->seek_time = 0;
if (sample_loc >= samplecount + samplesdone) if (sample_loc >= samplecount + samplesdone)
break ; break ;
samplecount += samplesdone - sample_loc; samplecount += samplesdone - sample_loc;
samplesdone = sample_loc; samplesdone = sample_loc;
} }
ci->seek_time = 0;
} }
/* Lock buffers */ /* Lock buffers */