1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include "libcook/cook.h"
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
;
48 uint32_t packet_count
;
49 int scrambling_unit_size
, num_units
;
50 size_t resume_offset
= ci
->id3
->offset
;
54 DEBUGF("codec init failed\n");
57 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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
));
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
);
84 DEBUGF("failed to initialize cook decoder\n");
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
);
98 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
100 /* The main decoder loop */
104 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
105 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
107 DEBUGF("rm_get_packet failed\n");
111 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
114 if (ci
->stop_codec
|| ci
->new_track
)
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
) {
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) {
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
);
142 DEBUGF("rm_get_packet failed\n");
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
;
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
);
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");
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
);
182 if (ci
->request_next_track())