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"
31 static RMContext rmctx
;
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
;
48 uint32_t packet_count
;
49 int scrambling_unit_size
, num_units
;
54 DEBUGF("codec init failed\n");
58 if (codec_wait_taginfo() != 0)
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
));
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
);
87 DEBUGF("failed to initialize cook decoder\n");
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
);
101 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
103 /* The main decoder loop */
107 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
108 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
110 DEBUGF("rm_get_packet failed\n");
114 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
117 if (ci
->stop_codec
|| ci
->new_track
)
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
) {
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) {
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
);
145 DEBUGF("rm_get_packet failed\n");
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
;
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
);
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");
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
);
185 if (ci
->request_next_track())