summaryrefslogtreecommitdiff
path: root/apps/codecs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs')
-rw-r--r--apps/codecs/cook.c70
1 files changed, 34 insertions, 36 deletions
diff --git a/apps/codecs/cook.c b/apps/codecs/cook.c
index 5efb797..fe818be 100644
--- a/apps/codecs/cook.c
+++ b/apps/codecs/cook.c
@@ -41,12 +41,12 @@ static void init_rm(RMContext *rmctx)
enum codec_status codec_main(void)
{
static size_t buff_size;
- int datasize, res, consumed, i, j, time_offset;
+ int datasize, res, consumed, i, time_offset;
uint8_t *bit_buffer;
int16_t outbuf[2048] __attribute__((aligned(32)));
uint16_t fs,sps,h;
uint32_t packet_count;
- int scrambling_unit_size, flag_seek_block = 0;
+ int scrambling_unit_size, num_units;
next_track:
if (codec_init()) {
@@ -102,51 +102,49 @@ seek_start :
if (ci->stop_codec || ci->new_track)
goto done;
- if (ci->seek_time) {
+ if (ci->seek_time) {
+ ci->set_elapsed(ci->seek_time);
+
+ /* Do not allow seeking beyond the file's length */
+ if ((unsigned) ci->seek_time > ci->id3->length) {
+ ci->seek_complete();
+ goto done;
+ }
+
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
packet_count = rmctx.nb_packets;
rmctx.audio_pkt_cnt = 0;
rmctx.frame_number = 0;
+
+ /* Seek to the start of the track */
if (ci->seek_time == 1) {
ci->set_elapsed(0);
ci->seek_complete();
goto seek_start;
- }
- j = 0;
- while(1) {
+ }
+ num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
+ ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
+ bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
+ consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
+ if(consumed < 0) {
+ DEBUGF("rm_get_packet failed\n");
+ return CODEC_ERROR;
+ }
+ packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
+ rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
+ while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
rmctx.audio_pkt_cnt = 0;
- bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
- consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
- if(consumed < 0) {
- DEBUGF("rm_get_packet failed\n");
- return CODEC_ERROR;
- }
- if(rmctx.audiotimestamp < (unsigned) ci->seek_time) {
- ci->advance_buffer(consumed);
- packet_count -= rmctx.audio_pkt_cnt;
- rmctx.frame_number += h*(fs/sps);
- j+= consumed;
- }
- if(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
- flag_seek_block = 1;
- rmctx.audio_pkt_cnt = 0;
- break;
- }
- }
-
- if(flag_seek_block) {
- ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + j - consumed);
- bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
- consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
+ ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
+ bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
+ consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
packet_count += rmctx.audio_pkt_cnt;
- rmctx.frame_number -= h*(fs/sps);
- time_offset = ci->seek_time - rmctx.audiotimestamp;
- i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
- flag_seek_block = 0;
- ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
- ci->seek_complete();
+ num_units--;
}
- }
+ time_offset = ci->seek_time - rmctx.audiotimestamp;
+ i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
+ ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
+ ci->seek_complete();
+ }
res = cook_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align);
rmctx.frame_number++;