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 #define FRAMESIZE ci->id3->bytesperframe
32 #define BITRATE ci->id3->bitrate
34 /* The codec has nothing to do with RM, it just uses an RMContext struct to *
35 * store the data needs to be passed to the decoder initializing function. */
37 ATRAC3Context q IBSS_ATTR
;
39 static void init_rm(RMContext
*rmctx
, struct mp3entry
*id3
)
41 rmctx
->sample_rate
= id3
->frequency
;
42 rmctx
->nb_channels
= 2;
43 rmctx
->bit_rate
= id3
->bitrate
;
44 rmctx
->block_align
= id3
->bytesperframe
;
46 /* 14-byte extra-data was faked in the metadata parser so that *
47 * the ATRAC3 decoder would parse it as WAV format extra-data. */
48 rmctx
->extradata_size
= 14;
49 memcpy(rmctx
->codec_extradata
, &id3
->id3v1buf
[0][0], 14);
52 /* this is the codec entry point */
53 enum codec_status
codec_main(void)
55 static size_t buff_size
;
56 int datasize
, res
, frame_counter
, total_frames
, seek_frame_offset
;
59 size_t resume_offset
= ci
->id3
->offset
;
63 DEBUGF("codec init failed\n");
66 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
69 codec_set_replaygain(ci
->id3
);
70 ci
->memset(&rmctx
,0,sizeof(RMContext
));
71 ci
->memset(&q
,0,sizeof(ATRAC3Context
));
73 init_rm(&rmctx
, ci
->id3
);
75 ci
->configure(DSP_SET_FREQUENCY
, ci
->id3
->frequency
);
76 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
77 ci
->configure(DSP_SET_STEREO_MODE
, rmctx
.nb_channels
== 1 ?
78 STEREO_MONO
: STEREO_NONINTERLEAVED
);
80 res
=atrac3_decode_init(&q
, &rmctx
);
82 DEBUGF("failed to initialize atrac decoder\n");
86 /* check for a mid-track resume and force a seek time accordingly */
87 if(resume_offset
> ci
->id3
->first_frame_offset
) {
88 resume_offset
-= ci
->id3
->first_frame_offset
;
89 /* calculate resume_offset in frames */
90 resume_offset
= (int)resume_offset
/ FRAMESIZE
;
91 ci
->seek_time
= (int)resume_offset
* ((FRAMESIZE
* 8)/BITRATE
);
93 total_frames
= (ci
->id3
->filesize
- ci
->id3
->first_frame_offset
) / FRAMESIZE
;
97 ci
->advance_buffer(ci
->id3
->first_frame_offset
);
99 /* The main decoder loop */
101 while(frame_counter
< total_frames
)
103 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
106 if (ci
->stop_codec
|| ci
->new_track
)
110 ci
->set_elapsed(ci
->seek_time
);
112 /* Do not allow seeking beyond the file's length */
113 if ((unsigned) ci
->seek_time
> ci
->id3
->length
) {
118 /* Seek to the start of the track */
119 if (ci
->seek_time
== 1) {
122 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
126 seek_frame_offset
= (ci
->seek_time
* BITRATE
) / (8 * FRAMESIZE
);
127 frame_counter
= seek_frame_offset
;
128 ci
->seek_buffer(ci
->id3
->first_frame_offset
+ seek_frame_offset
* FRAMESIZE
);
129 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
130 elapsed
= ci
->seek_time
;
132 ci
->set_elapsed(elapsed
);
136 res
= atrac3_decode_frame(&rmctx
, &q
, &datasize
, bit_buffer
, FRAMESIZE
);
138 if(res
!= (int)FRAMESIZE
) {
139 DEBUGF("codec error\n");
144 ci
->pcmbuf_insert(q
.outSamples
, q
.outSamples
+ 1024, q
.samples_per_frame
/ rmctx
.nb_channels
);
146 elapsed
+= (FRAMESIZE
* 8) / BITRATE
;
147 ci
->set_elapsed(elapsed
);
149 ci
->advance_buffer(FRAMESIZE
);
154 if (ci
->request_next_track())