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 ATRAC3Context q IBSS_ATTR
;
36 /* this is the codec entry point */
37 enum codec_status
codec_main(void)
39 static size_t buff_size
;
40 int datasize
, res
, frame_counter
, total_frames
, seek_frame_offset
;
43 size_t resume_offset
= ci
->id3
->offset
;
47 DEBUGF("codec init failed\n");
50 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
53 codec_set_replaygain(ci
->id3
);
54 ci
->memset(&q
,0,sizeof(ATRAC3Context
));
56 ci
->configure(DSP_SET_FREQUENCY
, ci
->id3
->frequency
);
57 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
58 ci
->configure(DSP_SET_STEREO_MODE
, ci
->id3
->channels
== 1 ?
59 STEREO_MONO
: STEREO_NONINTERLEAVED
);
61 res
=atrac3_decode_init(&q
, ci
->id3
);
63 DEBUGF("failed to initialize atrac decoder\n");
67 /* check for a mid-track resume and force a seek time accordingly */
68 if(resume_offset
> ci
->id3
->first_frame_offset
) {
69 resume_offset
-= ci
->id3
->first_frame_offset
;
70 /* calculate resume_offset in frames */
71 resume_offset
= (int)resume_offset
/ FRAMESIZE
;
72 ci
->seek_time
= (int)resume_offset
* ((FRAMESIZE
* 8)/BITRATE
);
74 total_frames
= (ci
->id3
->filesize
- ci
->id3
->first_frame_offset
) / FRAMESIZE
;
79 ci
->advance_buffer(ci
->id3
->first_frame_offset
);
81 /* The main decoder loop */
83 while(frame_counter
< total_frames
)
85 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
88 if (ci
->stop_codec
|| ci
->new_track
)
92 ci
->set_elapsed(ci
->seek_time
);
94 /* Do not allow seeking beyond the file's length */
95 if ((unsigned) ci
->seek_time
> ci
->id3
->length
) {
100 /* Seek to the start of the track */
101 if (ci
->seek_time
== 1) {
104 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
108 seek_frame_offset
= (ci
->seek_time
* BITRATE
) / (8 * FRAMESIZE
);
109 frame_counter
= seek_frame_offset
;
110 ci
->seek_buffer(ci
->id3
->first_frame_offset
+ seek_frame_offset
* FRAMESIZE
);
111 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
112 elapsed
= ci
->seek_time
;
114 ci
->set_elapsed(elapsed
);
118 res
= atrac3_decode_frame(FRAMESIZE
, &q
, &datasize
, bit_buffer
, FRAMESIZE
);
120 if(res
!= (int)FRAMESIZE
) {
121 DEBUGF("codec error\n");
126 ci
->pcmbuf_insert(q
.outSamples
, q
.outSamples
+ 1024, q
.samples_per_frame
/ ci
->id3
->channels
);
128 elapsed
+= (FRAMESIZE
* 8) / BITRATE
;
129 ci
->set_elapsed(elapsed
);
131 ci
->advance_buffer(FRAMESIZE
);
136 if (ci
->request_next_track())