Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / cook.c
blob30255d4fd7e50f89972c3683664ea24c98f7917a
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 RMContext rmctx;
32 RMPacket pkt;
33 COOKContext q IBSS_ATTR;
34 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 = ci->id3->offset;
52 next_track:
53 if (codec_init()) {
54 DEBUGF("codec init failed\n");
55 return CODEC_ERROR;
57 while (!*ci->taginfo_ready && !ci->stop_codec)
58 ci->sleep(1);
60 codec_set_replaygain(ci->id3);
61 ci->memset(&rmctx,0,sizeof(RMContext));
62 ci->memset(&pkt,0,sizeof(RMPacket));
63 ci->memset(&q,0,sizeof(COOKContext));
65 init_rm(&rmctx);
67 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
68 /* cook's sample representation is 21.11
69 * DSP_SET_SAMPLE_DEPTH = 11 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 26 */
70 ci->configure(DSP_SET_SAMPLE_DEPTH, 26);
71 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
72 STEREO_MONO : STEREO_NONINTERLEAVED);
74 packet_count = rmctx.nb_packets;
75 rmctx.audio_framesize = rmctx.block_align;
76 rmctx.block_align = rmctx.sub_packet_size;
77 fs = rmctx.audio_framesize;
78 sps= rmctx.block_align;
79 h = rmctx.sub_packet_h;
80 scrambling_unit_size = h*fs;
82 res =cook_decode_init(&rmctx, &q);
83 if(res < 0) {
84 DEBUGF("failed to initialize cook decoder\n");
85 return CODEC_ERROR;
88 /* check for a mid-track resume and force a seek time accordingly */
89 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
90 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
91 num_units = (int)resume_offset / scrambling_unit_size;
92 /* put number of subpackets to skip in resume_offset */
93 resume_offset /= (sps + PACKET_HEADER_SIZE);
94 ci->seek_time = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
97 ci->set_elapsed(0);
98 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
100 /* The main decoder loop */
101 seek_start :
102 while(packet_count)
104 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
105 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
106 if(consumed < 0) {
107 DEBUGF("rm_get_packet failed\n");
108 return CODEC_ERROR;
111 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
113 ci->yield();
114 if (ci->stop_codec || ci->new_track)
115 goto done;
117 if (ci->seek_time) {
118 ci->set_elapsed(ci->seek_time);
120 /* Do not allow seeking beyond the file's length */
121 if ((unsigned) ci->seek_time > ci->id3->length) {
122 ci->seek_complete();
123 goto done;
126 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
127 packet_count = rmctx.nb_packets;
128 rmctx.audio_pkt_cnt = 0;
129 rmctx.frame_number = 0;
131 /* Seek to the start of the track */
132 if (ci->seek_time == 1) {
133 ci->set_elapsed(0);
134 ci->seek_complete();
135 goto seek_start;
137 num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
138 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
139 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
140 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
141 if(consumed < 0) {
142 DEBUGF("rm_get_packet failed\n");
143 return CODEC_ERROR;
145 packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
146 rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate));
147 while(rmctx.audiotimestamp > (unsigned) ci->seek_time) {
148 rmctx.audio_pkt_cnt = 0;
149 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
150 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
151 consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
152 packet_count += rmctx.audio_pkt_cnt;
153 num_units--;
155 time_offset = ci->seek_time - rmctx.audiotimestamp;
156 i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
157 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
158 ci->seek_complete();
160 res = cook_decode_frame(&rmctx,&q, rm_outbuf, &datasize, pkt.frames[i], rmctx.block_align);
161 rmctx.frame_number++;
163 /* skip the first two frames; no valid audio */
164 if(rmctx.frame_number < 3) continue;
166 if(res != rmctx.block_align) {
167 DEBUGF("codec error\n");
168 return CODEC_ERROR;
171 ci->pcmbuf_insert(rm_outbuf,
172 rm_outbuf+q.samples_per_channel,
173 q.samples_per_channel);
174 ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
176 packet_count -= rmctx.audio_pkt_cnt;
177 rmctx.audio_pkt_cnt = 0;
178 ci->advance_buffer(consumed);
181 done :
182 if (ci->request_next_track())
183 goto next_track;
185 return CODEC_OK;