Handle mid-track resumes in RM.
[kugel-rb.git] / apps / codecs / cook.c
blob5f91d244ff92d897752a0595e9a337685ca5e7f3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Copyright (C) 2009 Mohamed Tarek
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <string.h>
22 #include "logf.h"
23 #include "codeclib.h"
24 #include "inttypes.h"
25 #include "libcook/cook.h"
27 CODEC_HEADER
29 RMContext rmctx;
30 RMPacket pkt;
31 COOKContext q IBSS_ATTR;
33 static void init_rm(RMContext *rmctx)
35 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
38 /* this is the codec entry point */
39 enum codec_status codec_main(void)
41 static size_t buff_size;
42 int datasize, res, consumed, i, time_offset;
43 uint8_t *bit_buffer;
44 int16_t outbuf[2048] __attribute__((aligned(32)));
45 uint16_t fs,sps,h;
46 uint32_t packet_count;
47 int scrambling_unit_size, num_units;
48 size_t resume_offset = ci->id3->offset;
50 next_track:
51 if (codec_init()) {
52 DEBUGF("codec init failed\n");
53 return CODEC_ERROR;
55 while (!*ci->taginfo_ready && !ci->stop_codec)
56 ci->sleep(1);
58 codec_set_replaygain(ci->id3);
59 ci->memset(&rmctx,0,sizeof(RMContext));
60 ci->memset(&pkt,0,sizeof(RMPacket));
61 ci->memset(&q,0,sizeof(COOKContext));
63 init_rm(&rmctx);
65 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
66 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
67 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
68 STEREO_MONO : STEREO_INTERLEAVED);
70 packet_count = rmctx.nb_packets;
71 rmctx.audio_framesize = rmctx.block_align;
72 rmctx.block_align = rmctx.sub_packet_size;
73 fs = rmctx.audio_framesize;
74 sps= rmctx.block_align;
75 h = rmctx.sub_packet_h;
76 scrambling_unit_size = h*fs;
78 res =cook_decode_init(&rmctx, &q);
79 if(res < 0) {
80 DEBUGF("failed to initialize cook decoder\n");
81 return CODEC_ERROR;
84 /* check for a mid-track resume and force a seek time accordingly */
85 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
86 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
87 num_units = (int)resume_offset / scrambling_unit_size;
88 /* put number of subpackets to skip in resume_offset */
89 resume_offset /= (sps + PACKET_HEADER_SIZE);
90 ci->seek_time = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
93 ci->set_elapsed(0);
94 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
96 /* The main decoder loop */
97 seek_start :
98 while(packet_count)
100 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
101 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
102 if(consumed < 0) {
103 DEBUGF("rm_get_packet failed\n");
104 return CODEC_ERROR;
107 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
109 ci->yield();
110 if (ci->stop_codec || ci->new_track)
111 goto done;
113 if (ci->seek_time) {
114 ci->set_elapsed(ci->seek_time);
116 /* Do not allow seeking beyond the file's length */
117 if ((unsigned) ci->seek_time > ci->id3->length) {
118 ci->seek_complete();
119 goto done;
122 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
123 packet_count = rmctx.nb_packets;
124 rmctx.audio_pkt_cnt = 0;
125 rmctx.frame_number = 0;
127 /* Seek to the start of the track */
128 if (ci->seek_time == 1) {
129 ci->set_elapsed(0);
130 ci->seek_complete();
131 goto seek_start;
133 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
134 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
135 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
136 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
137 if(consumed < 0) {
138 DEBUGF("rm_get_packet failed\n");
139 return CODEC_ERROR;
141 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
142 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
143 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
144 rmctx.audio_pkt_cnt = 0;
145 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
146 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
147 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
148 packet_count += rmctx.audio_pkt_cnt;
149 num_units--;
151 time_offset = ci->seek_time - rmctx.audiotimestamp;
152 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
153 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
154 ci->seek_complete();
156 res = cook_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align);
157 rmctx.frame_number++;
159 /* skip the first two frames; no valid audio */
160 if(rmctx.frame_number < 3) continue;
162 if(res != rmctx.block_align) {
163 DEBUGF("codec error\n");
164 return CODEC_ERROR;
167 ci->pcmbuf_insert(outbuf, NULL, q.samples_per_frame / rmctx.nb_channels);
168 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
170 packet_count -= rmctx.audio_pkt_cnt;
171 rmctx.audio_pkt_cnt = 0;
172 ci->advance_buffer(consumed);
175 done :
176 if (ci->request_next_track())
177 goto next_track;
179 return CODEC_OK;