From b5b9cb031d0b1b3c802fde2f33e27ecf7d166778 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 13 Jul 2009 10:06:19 +0000 Subject: Modified the code for seeking to speed it up a bit. Instead of searching the file for the required timestamp, we seek directly to a calcualted position. If we go to a point beyond the desired seek_time, we seek back in blocks (normally one or two blocks to the back) to get the required block. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21836 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/cook.c | 70 ++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'apps') diff --git a/apps/codecs/cook.c b/apps/codecs/cook.c index 5efb797fdd..fe818be905 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++; -- cgit v1.2.3