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(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
;
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
;
110 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
112 /* The main decoder loop */
116 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
117 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
119 DEBUGF("rm_get_packet failed\n");
123 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
125 if (action
== CODEC_ACTION_NULL
)
126 action
= ci
->get_command(¶m
);
128 if (action
== CODEC_ACTION_HALT
)
131 if (action
== CODEC_ACTION_SEEK_TIME
) {
132 /* Do not allow seeking beyond the file's length */
133 if ((unsigned) param
> ci
->id3
->length
) {
134 ci
->set_elapsed(ci
->id3
->length
);
139 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
140 packet_count
= rmctx
.nb_packets
;
141 rmctx
.audio_pkt_cnt
= 0;
142 rmctx
.frame_number
= 0;
144 /* Seek to the start of the track */
148 action
= CODEC_ACTION_NULL
;
151 num_units
= (param
/(sps
*1000*8/rmctx
.bit_rate
))/(h
*(fs
/sps
));
152 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* num_units
);
153 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
154 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
156 DEBUGF("rm_get_packet failed\n");
160 packet_count
= rmctx
.nb_packets
- rmctx
.audio_pkt_cnt
* num_units
;
161 rmctx
.frame_number
= (param
/(sps
*1000*8/rmctx
.bit_rate
));
162 while(rmctx
.audiotimestamp
> (unsigned) param
) {
163 rmctx
.audio_pkt_cnt
= 0;
164 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* (num_units
-1));
165 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
166 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
167 packet_count
+= rmctx
.audio_pkt_cnt
;
170 time_offset
= param
- rmctx
.audiotimestamp
;
171 i
= (time_offset
/((sps
* 8 * 1000)/rmctx
.bit_rate
));
172 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
176 action
= CODEC_ACTION_NULL
;
178 res
= cook_decode_frame(&rmctx
,&q
, rm_outbuf
, &datasize
, pkt
.frames
[i
], rmctx
.block_align
);
179 rmctx
.frame_number
++;
181 /* skip the first two frames; no valid audio */
182 if(rmctx
.frame_number
< 3) continue;
184 if(res
!= rmctx
.block_align
) {
185 DEBUGF("codec error\n");
189 ci
->pcmbuf_insert(rm_outbuf
,
190 rm_outbuf
+q
.samples_per_channel
,
191 q
.samples_per_channel
);
192 ci
->set_elapsed(rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
);
194 packet_count
-= rmctx
.audio_pkt_cnt
;
195 rmctx
.audio_pkt_cnt
= 0;
196 ci
->advance_buffer(consumed
);