Make repeat mode/shuffle work in the sbs also
[maemo-rb.git] / apps / codecs / cook.c
blob015618986c6272db6f6090ec03d1ca9bd24410e5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Mohamed Tarek
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <string.h>
24 #include "logf.h"
25 #include "codeclib.h"
26 #include "inttypes.h"
27 #include "libcook/cook.h"
29 CODEC_HEADER
31 static RMContext rmctx;
32 static RMPacket pkt;
33 static COOKContext q IBSS_ATTR;
34 static int32_t rm_outbuf[2048];
36 static void init_rm(RMContext *rmctx)
38 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
41 /* this is the codec entry point */
42 enum codec_status codec_main(void)
44 static size_t buff_size;
45 int datasize, res, consumed, i, time_offset;
46 uint8_t *bit_buffer;
47 uint16_t fs,sps,h;
48 uint32_t packet_count;
49 int scrambling_unit_size, num_units;
50 size_t resume_offset;
52 next_track:
53 if (codec_init()) {
54 DEBUGF("codec init failed\n");
55 return CODEC_ERROR;
58 if (codec_wait_taginfo() != 0)
59 goto done;
61 resume_offset = ci->id3->offset;
63 codec_set_replaygain(ci->id3);
64 ci->memset(&rmctx,0,sizeof(RMContext));
65 ci->memset(&pkt,0,sizeof(RMPacket));
66 ci->memset(&q,0,sizeof(COOKContext));
68 init_rm(&rmctx);
70 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
71 /* cook's sample representation is 21.11
72 * DSP_SET_SAMPLE_DEPTH = 11 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 26 */
73 ci->configure(DSP_SET_SAMPLE_DEPTH, 26);
74 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
75 STEREO_MONO : STEREO_NONINTERLEAVED);
77 packet_count = rmctx.nb_packets;
78 rmctx.audio_framesize = rmctx.block_align;
79 rmctx.block_align = rmctx.sub_packet_size;
80 fs = rmctx.audio_framesize;
81 sps= rmctx.block_align;
82 h = rmctx.sub_packet_h;
83 scrambling_unit_size = h*fs;
85 res =cook_decode_init(&rmctx, &q);
86 if(res < 0) {
87 DEBUGF("failed to initialize cook decoder\n");
88 return CODEC_ERROR;
91 /* check for a mid-track resume and force a seek time accordingly */
92 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
93 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
94 num_units = (int)resume_offset / scrambling_unit_size;
95 /* put number of subpackets to skip in resume_offset */
96 resume_offset /= (sps + PACKET_HEADER_SIZE);
97 ci->seek_time = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
100 ci->set_elapsed(0);
101 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
103 /* The main decoder loop */
104 seek_start :
105 while(packet_count)
107 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
108 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
109 if(consumed < 0) {
110 DEBUGF("rm_get_packet failed\n");
111 return CODEC_ERROR;
114 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
116 ci->yield();
117 if (ci->stop_codec || ci->new_track)
118 goto done;
120 if (ci->seek_time) {
121 ci->set_elapsed(ci->seek_time);
123 /* Do not allow seeking beyond the file's length */
124 if ((unsigned) ci->seek_time > ci->id3->length) {
125 ci->seek_complete();
126 goto done;
129 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
130 packet_count = rmctx.nb_packets;
131 rmctx.audio_pkt_cnt = 0;
132 rmctx.frame_number = 0;
134 /* Seek to the start of the track */
135 if (ci->seek_time == 1) {
136 ci->set_elapsed(0);
137 ci->seek_complete();
138 goto seek_start;
140 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
141 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
142 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
143 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
144 if(consumed < 0) {
145 DEBUGF("rm_get_packet failed\n");
146 return CODEC_ERROR;
148 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
149 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
150 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
151 rmctx.audio_pkt_cnt = 0;
152 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
153 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
154 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
155 packet_count += rmctx.audio_pkt_cnt;
156 num_units--;
158 time_offset = ci->seek_time - rmctx.audiotimestamp;
159 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
160 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
161 ci->seek_complete();
163 res = cook_decode_frame(&rmctx,&q, rm_outbuf, &datasize, pkt.frames[i], rmctx.block_align);
164 rmctx.frame_number++;
166 /* skip the first two frames; no valid audio */
167 if(rmctx.frame_number < 3) continue;
169 if(res != rmctx.block_align) {
170 DEBUGF("codec error\n");
171 return CODEC_ERROR;
174 ci->pcmbuf_insert(rm_outbuf,
175 rm_outbuf+q.samples_per_channel,
176 q.samples_per_channel);
177 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
179 packet_count -= rmctx.audio_pkt_cnt;
180 rmctx.audio_pkt_cnt = 0;
181 ci->advance_buffer(consumed);
184 done :
185 if (ci->request_next_track())
186 goto next_track;
188 return CODEC_OK;