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 "libatrac/atrac3.h"
31 static RMContext rmctx
;
33 static ATRAC3Context q IBSS_ATTR
;
35 static void init_rm(RMContext
*rmctx
)
37 /* initialize the RMContext */
38 memcpy(rmctx
, (void*)(( (intptr_t)ci
->id3
->id3v2buf
+ 3 ) &~ 3), sizeof(RMContext
));
40 /* and atrac3 expects extadata in id3v2buf, so we shall give it that */
41 memcpy(ci
->id3
->id3v2buf
, (char*)rmctx
->codec_extradata
, rmctx
->extradata_size
*sizeof(char));
44 /* this is the codec entry point */
45 enum codec_status
codec_main(void)
47 static size_t buff_size
;
48 int datasize
, res
, consumed
, i
, time_offset
;
51 uint32_t packet_count
;
52 int scrambling_unit_size
, num_units
, elapsed
= 0;
58 DEBUGF("codec init failed\n");
62 if (codec_wait_taginfo() != 0)
65 resume_offset
= ci
->id3
->offset
;
67 codec_set_replaygain(ci
->id3
);
68 ci
->memset(&rmctx
,0,sizeof(RMContext
));
69 ci
->memset(&pkt
,0,sizeof(RMPacket
));
70 ci
->memset(&q
,0,sizeof(ATRAC3Context
));
74 ci
->configure(DSP_SET_FREQUENCY
, ci
->id3
->frequency
);
75 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
76 ci
->configure(DSP_SET_STEREO_MODE
, rmctx
.nb_channels
== 1 ?
77 STEREO_MONO
: STEREO_NONINTERLEAVED
);
79 packet_count
= rmctx
.nb_packets
;
80 rmctx
.audio_framesize
= rmctx
.block_align
;
81 rmctx
.block_align
= rmctx
.sub_packet_size
;
82 fs
= rmctx
.audio_framesize
;
83 sps
= rmctx
.block_align
;
84 h
= rmctx
.sub_packet_h
;
85 scrambling_unit_size
= h
*fs
;
87 res
=atrac3_decode_init(&q
, ci
->id3
);
89 DEBUGF("failed to initialize RM atrac decoder\n");
93 /* check for a mid-track resume and force a seek time accordingly */
94 if(resume_offset
> rmctx
.data_offset
+ DATA_HEADER_SIZE
) {
95 resume_offset
-= rmctx
.data_offset
+ DATA_HEADER_SIZE
;
96 num_units
= (int)resume_offset
/ scrambling_unit_size
;
97 /* put number of subpackets to skip in resume_offset */
98 resume_offset
/= (sps
+ PACKET_HEADER_SIZE
);
99 ci
->seek_time
= (int)resume_offset
* ((sps
* 8 * 1000)/rmctx
.bit_rate
);
103 ci
->advance_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
105 /* The main decoder loop */
107 while((unsigned)elapsed
< rmctx
.duration
)
109 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
110 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
111 if(consumed
< 0 && playback_on
!= 0) {
112 if(playback_on
== -1) {
113 /* Error only if packet-parsing failed and playback hadn't started */
114 DEBUGF("rm_get_packet failed\n");
121 for(i
= 0; i
< rmctx
.audio_pkt_cnt
*(fs
/sps
) ; i
++)
124 if (ci
->stop_codec
|| ci
->new_track
)
128 ci
->set_elapsed(ci
->seek_time
);
130 /* Do not allow seeking beyond the file's length */
131 if ((unsigned) ci
->seek_time
> ci
->id3
->length
) {
136 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
);
137 packet_count
= rmctx
.nb_packets
;
138 rmctx
.audio_pkt_cnt
= 0;
139 rmctx
.frame_number
= 0;
141 /* Seek to the start of the track */
142 if (ci
->seek_time
== 1) {
147 num_units
= ((ci
->seek_time
)/(sps
*1000*8/rmctx
.bit_rate
))/(h
*(fs
/sps
));
148 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* num_units
);
149 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
150 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
151 if(consumed
< 0 && playback_on
!= 0) {
152 if(playback_on
== -1) {
153 /* Error only if packet-parsing failed and playback hadn't started */
154 DEBUGF("rm_get_packet failed\n");
161 packet_count
= rmctx
.nb_packets
- rmctx
.audio_pkt_cnt
* num_units
;
162 rmctx
.frame_number
= ((ci
->seek_time
)/(sps
*1000*8/rmctx
.bit_rate
));
163 while(rmctx
.audiotimestamp
> (unsigned) ci
->seek_time
) {
164 rmctx
.audio_pkt_cnt
= 0;
165 ci
->seek_buffer(rmctx
.data_offset
+ DATA_HEADER_SIZE
+ consumed
* (num_units
-1));
166 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, scrambling_unit_size
);
167 consumed
= rm_get_packet(&bit_buffer
, &rmctx
, &pkt
);
168 packet_count
+= rmctx
.audio_pkt_cnt
;
171 time_offset
= ci
->seek_time
- rmctx
.audiotimestamp
;
172 i
= (time_offset
/((sps
* 8 * 1000)/rmctx
.bit_rate
));
173 elapsed
= rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
;
174 ci
->set_elapsed(elapsed
);
178 res
= atrac3_decode_frame(rmctx
.block_align
, &q
, &datasize
, pkt
.frames
[i
], rmctx
.block_align
);
179 else /* indicates that there are no remaining frames */
182 if(res
!= rmctx
.block_align
) {
183 DEBUGF("codec error\n");
188 ci
->pcmbuf_insert(q
.outSamples
, q
.outSamples
+ 1024, q
.samples_per_frame
/ rmctx
.nb_channels
);
190 elapsed
= rmctx
.audiotimestamp
+(1000*8*sps
/rmctx
.bit_rate
)*i
;
191 ci
->set_elapsed(elapsed
);
192 rmctx
.frame_number
++;
194 packet_count
-= rmctx
.audio_pkt_cnt
;
195 rmctx
.audio_pkt_cnt
= 0;
196 ci
->advance_buffer(consumed
);
200 if (ci
->request_next_track())