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 IBSS_ATTR_COOK_LARGE_IRAM
;
32 static RMPacket pkt IBSS_ATTR_COOK_LARGE_IRAM
;
33 static COOKContext q IBSS_ATTR
;
34 static int32_t rm_outbuf
[2048] IBSS_ATTR_COOK_LARGE_IRAM MEM_ALIGN_ATTR
;
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(enum codec_entry_call_reason reason
)
49 /* this is called for each file to process */
50 enum codec_status
codec_run(void)
52 static size_t buff_size
;
53 int datasize
, res
, consumed
, i
, time_offset
;
56 uint32_t packet_count
;
57 int scrambling_unit_size
, num_units
;
60 enum codec_command_action action
= CODEC_ACTION_NULL
;
63 DEBUGF("codec init failed\n");
67 resume_offset
= ci
->id3
->offset
;
69 codec_set_replaygain(ci
->id3
);
70 ci
->memset(&rmctx
,0,sizeof(RMContext
));
71 ci
->memset(&pkt
,0,sizeof(RMPacket
));
72 ci
->memset(&q
,0,sizeof(COOKContext
));
78 ci
->configure(DSP_SET_FREQUENCY
, ci
->id3
->frequency
);
79 /* cook's sample representation is 21.11
80 * DSP_SET_SAMPLE_DEPTH = 11 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 26 */
81 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 26);
82 ci
->configure(DSP_SET_STEREO_MODE
, rmctx
.nb_channels
== 1 ?
83 STEREO_MONO
: STEREO_NONINTERLEAVED
);
85 packet_count
= rmctx
.nb_packets
;
86 rmctx
.audio_framesize
= rmctx
.block_align
;
87 rmctx
.block_align
= rmctx
.sub_packet_size
;
88 fs
= rmctx
.audio_framesize
;
89 sps
= rmctx
.block_align
;
90 h
= rmctx
.sub_packet_h
;
91 scrambling_unit_size
= h
* (fs
+ PACKET_HEADER_SIZE
);
93 res
=cook_decode_init(&rmctx
, &q
);
95 DEBUGF("failed to initialize cook decoder\n");
99 /* check for a mid-track resume and force a seek time accordingly */
100 if(resume_offset
> rmctx
.data_offset
+ DATA_HEADER_SIZE
) {
101 resume_offset
-= rmctx
.data_offset
+ DATA_HEADER_SIZE
;
102 num_units
= (int)resume_offset
/ scrambling_unit_size
;
103 /* put number of subpackets to skip in resume_offset */
104 resume_offset
/= (sps
+ PACKET_HEADER_SIZE
);
105 param
= (int)resume_offset
* ((sps
* 8 * 1000)/rmctx
.bit_rate
);
106 action
= CODEC_ACTION_SEEK_TIME
;
112 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
114 /* The main decoder loop */
118 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
119 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
121 DEBUGF("rm_get_packet failed\n");
125 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
127 if (action
== CODEC_ACTION_NULL
)
128 action
= ci
->get_command(¶m
);
130 if (action
== CODEC_ACTION_HALT
)
133 if (action
== CODEC_ACTION_SEEK_TIME
) {
134 /* Do not allow seeking beyond the file's length */
135 if ((unsigned) param
> ci
->id3
->length
) {
136 ci
->set_elapsed(ci
->id3
->length
);
141 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
142 packet_count
= rmctx
.nb_packets
;
143 rmctx
.audio_pkt_cnt
= 0;
144 rmctx
.frame_number
= 0;
146 /* Seek to the start of the track */
150 action
= CODEC_ACTION_NULL
;
153 num_units
= (param
/(sps
*1000*8/rmctx
.bit_rate
))/(h
*(fs
/sps
));
154 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* num_units
);
155 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
156 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
158 DEBUGF("rm_get_packet failed\n");
162 packet_count
= rmctx
.nb_packets
- rmctx
.audio_pkt_cnt
* num_units
;
163 rmctx
.frame_number
= (param
/(sps
*1000*8/rmctx
.bit_rate
));
164 while(rmctx
.audiotimestamp
> (unsigned) param
) {
165 rmctx
.audio_pkt_cnt
= 0;
166 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* (num_units
-1));
167 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
168 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
169 packet_count
+= rmctx
.audio_pkt_cnt
;
172 time_offset
= param
- rmctx
.audiotimestamp
;
173 i
= (time_offset
/((sps
* 8 * 1000)/rmctx
.bit_rate
));
174 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
178 action
= CODEC_ACTION_NULL
;
180 res
= cook_decode_frame(&rmctx
,&q
, rm_outbuf
, &datasize
, pkt
.frames
[i
], rmctx
.block_align
);
181 rmctx
.frame_number
++;
183 /* skip the first two frames; no valid audio */
184 if(rmctx
.frame_number
< 3) continue;
186 if(res
!= rmctx
.block_align
) {
187 DEBUGF("codec error\n");
191 ci
->pcmbuf_insert(rm_outbuf
,
192 rm_outbuf
+q
.samples_per_channel
,
193 q
.samples_per_channel
);
194 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
196 packet_count
-= rmctx
.audio_pkt_cnt
;
197 rmctx
.audio_pkt_cnt
= 0;
198 ci
->advance_buffer(consumed
);