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
;
35 static void init_rm(RMContext
*rmctx
)
37 memcpy(rmctx
, (void*)(( (intptr_t)ci
->id3
->id3v2buf
+ 3 ) &~ 3), sizeof(RMContext
));
40 /* this is the codec entry point */
41 enum codec_status
codec_main(void)
43 static size_t buff_size
;
44 int datasize
, res
, consumed
, i
, time_offset
;
46 int16_t outbuf
[2048] __attribute__((aligned(32)));
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 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 16);
69 ci
->configure(DSP_SET_STEREO_MODE
, rmctx
.nb_channels
== 1 ?
70 STEREO_MONO
: STEREO_INTERLEAVED
);
72 packet_count
= rmctx
.nb_packets
;
73 rmctx
.audio_framesize
= rmctx
.block_align
;
74 rmctx
.block_align
= rmctx
.sub_packet_size
;
75 fs
= rmctx
.audio_framesize
;
76 sps
= rmctx
.block_align
;
77 h
= rmctx
.sub_packet_h
;
78 scrambling_unit_size
= h
*fs
;
80 res
=cook_decode_init(&rmctx
, &q
);
82 DEBUGF("failed to initialize cook decoder\n");
86 /* check for a mid-track resume and force a seek time accordingly */
87 if(resume_offset
> rmctx
.data_offset
+ DATA_HEADER_SIZE
) {
88 resume_offset
-= rmctx
.data_offset
+ DATA_HEADER_SIZE
;
89 num_units
= (int)resume_offset
/ scrambling_unit_size
;
90 /* put number of subpackets to skip in resume_offset */
91 resume_offset
/= (sps
+ PACKET_HEADER_SIZE
);
92 ci
->seek_time
= (int)resume_offset
* ((sps
* 8 * 1000)/rmctx
.bit_rate
);
96 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
98 /* The main decoder loop */
102 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
103 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
105 DEBUGF("rm_get_packet failed\n");
109 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
112 if (ci
->stop_codec
|| ci
->new_track
)
116 ci
->set_elapsed(ci
->seek_time
);
118 /* Do not allow seeking beyond the file's length */
119 if ((unsigned) ci
->seek_time
> ci
->id3
->length
) {
124 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
125 packet_count
= rmctx
.nb_packets
;
126 rmctx
.audio_pkt_cnt
= 0;
127 rmctx
.frame_number
= 0;
129 /* Seek to the start of the track */
130 if (ci
->seek_time
== 1) {
135 num_units
= ((ci
->seek_time
)/(sps
*1000*8/rmctx
.bit_rate
))/(h
*(fs
/sps
));
136 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* num_units
);
137 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
138 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
140 DEBUGF("rm_get_packet failed\n");
143 packet_count
= rmctx
.nb_packets
- rmctx
.audio_pkt_cnt
* num_units
;
144 rmctx
.frame_number
= ((ci
->seek_time
)/(sps
*1000*8/rmctx
.bit_rate
));
145 while(rmctx
.audiotimestamp
> (unsigned) ci
->seek_time
) {
146 rmctx
.audio_pkt_cnt
= 0;
147 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* (num_units
-1));
148 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
149 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
150 packet_count
+= rmctx
.audio_pkt_cnt
;
153 time_offset
= ci
->seek_time
- rmctx
.audiotimestamp
;
154 i
= (time_offset
/((sps
* 8 * 1000)/rmctx
.bit_rate
));
155 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
158 res
= cook_decode_frame(&rmctx
,&q
, outbuf
, &datasize
, pkt
.frames
[i
], rmctx
.block_align
);
159 rmctx
.frame_number
++;
161 /* skip the first two frames; no valid audio */
162 if(rmctx
.frame_number
< 3) continue;
164 if(res
!= rmctx
.block_align
) {
165 DEBUGF("codec error\n");
169 ci
->pcmbuf_insert(outbuf
, NULL
, q
.samples_per_frame
/ rmctx
.nb_channels
);
170 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
172 packet_count
-= rmctx
.audio_pkt_cnt
;
173 rmctx
.audio_pkt_cnt
= 0;
174 ci
->advance_buffer(consumed
);
178 if (ci
->request_next_track())