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 static ATRAC3Context q IBSS_ATTR
;
36 /* this is the codec entry point */
37 enum codec_status
codec_main(enum codec_entry_call_reason reason
)
44 /* this is called for each file to process */
45 enum codec_status
codec_run(void)
47 static size_t buff_size
;
48 int datasize
, res
, frame_counter
, total_frames
, seek_frame_offset
;
53 enum codec_command_action action
= CODEC_ACTION_NULL
;
56 DEBUGF("codec init failed\n");
60 resume_offset
= ci
->id3
->offset
;
62 codec_set_replaygain(ci
->id3
);
63 ci
->memset(&q
,0,sizeof(ATRAC3Context
));
65 ci
->configure(DSP_SET_FREQUENCY
, ci
->id3
->frequency
);
66 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
67 ci
->configure(DSP_SET_STEREO_MODE
, ci
->id3
->channels
== 1 ?
68 STEREO_MONO
: STEREO_NONINTERLEAVED
);
72 res
= atrac3_decode_init(&q
, ci
->id3
);
74 DEBUGF("failed to initialize OMA atrac decoder\n");
78 total_frames
= (ci
->id3
->filesize
- ci
->id3
->first_frame_offset
) / FRAMESIZE
;
81 /* check for a mid-track resume and force a seek time accordingly */
82 if(resume_offset
> ci
->id3
->first_frame_offset
) {
83 resume_offset
-= ci
->id3
->first_frame_offset
;
84 /* calculate resume_offset in frames */
85 resume_offset
= (int)resume_offset
/ FRAMESIZE
;
86 param
= (int)resume_offset
* ((FRAMESIZE
* 8)/BITRATE
);
87 action
= CODEC_ACTION_SEEK_TIME
;
91 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
94 /* The main decoder loop */
95 while(frame_counter
< total_frames
)
97 if (action
== CODEC_ACTION_NULL
)
98 action
= ci
->get_command(¶m
);
100 if (action
== CODEC_ACTION_HALT
)
103 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
105 if (action
== CODEC_ACTION_SEEK_TIME
) {
106 /* Do not allow seeking beyond the file's length */
107 if ((unsigned) param
> ci
->id3
->length
) {
108 ci
->set_elapsed(ci
->id3
->length
);
113 /* Seek to the start of the track */
117 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
119 action
= CODEC_ACTION_NULL
;
123 seek_frame_offset
= (param
* BITRATE
) / (8 * FRAMESIZE
);
124 frame_counter
= seek_frame_offset
;
125 ci
->seek_buffer(ci
->id3
->first_frame_offset
+ seek_frame_offset
* FRAMESIZE
);
126 bit_buffer
= (uint8_t *) ci
->request_buffer(&buff_size
, FRAMESIZE
);
128 ci
->set_elapsed(elapsed
);
132 action
= CODEC_ACTION_NULL
;
134 res
= atrac3_decode_frame(FRAMESIZE
, &q
, &datasize
, bit_buffer
, FRAMESIZE
);
136 if(res
!= (int)FRAMESIZE
) {
137 DEBUGF("codec error\n");
142 ci
->pcmbuf_insert(q
.outSamples
, q
.outSamples
+ 1024,
143 q
.samples_per_frame
/ ci
->id3
->channels
);
145 elapsed
+= (FRAMESIZE
* 8) / BITRATE
;
146 ci
->set_elapsed(elapsed
);
148 ci
->advance_buffer(FRAMESIZE
);