summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/raac.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/raac.c')
-rw-r--r--lib/rbcodec/codecs/raac.c212
1 files changed, 212 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/raac.c b/lib/rbcodec/codecs/raac.c
new file mode 100644
index 0000000000..0ef7e715bc
--- /dev/null
+++ b/lib/rbcodec/codecs/raac.c
@@ -0,0 +1,212 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 Mohamed Tarek
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "codeclib.h"
23#include "librm/rm.h"
24#include "libfaad/common.h"
25#include "libfaad/structs.h"
26#include "libfaad/decoder.h"
27/* rockbox: not used
28#include "libfaad/output.h"
29*/
30
31CODEC_HEADER
32
33static void init_rm(RMContext *rmctx)
34{
35 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
36}
37
38static RMContext rmctx;
39static RMPacket pkt;
40
41/* this is the codec entry point */
42enum codec_status codec_main(enum codec_entry_call_reason reason)
43{
44 if (reason == CODEC_LOAD) {
45 /* Generic codec initialisation */
46 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
47 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
48 }
49
50 return CODEC_OK;
51}
52
53/* this is called for each file to process */
54enum codec_status codec_run(void)
55{
56 static NeAACDecFrameInfo frame_info;
57 NeAACDecHandle decoder;
58 size_t n;
59 unsigned int i;
60 unsigned char* buffer;
61 int err, consumed, pkt_offset, skipped = 0;
62 uint32_t s = 0; /* sample rate */
63 unsigned char c = 0; /* channels */
64 int playback_on = -1;
65 size_t resume_offset;
66 intptr_t param;
67 enum codec_command_action action = CODEC_ACTION_NULL;
68
69 if (codec_init()) {
70 DEBUGF("FAAD: Codec init error\n");
71 return CODEC_ERROR;
72 }
73
74 resume_offset = ci->id3->offset;
75
76 ci->memset(&rmctx,0,sizeof(RMContext));
77 ci->memset(&pkt,0,sizeof(RMPacket));
78
79 ci->seek_buffer(0);
80
81 init_rm(&rmctx);
82 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
83 codec_set_replaygain(ci->id3);
84
85 /* initialise the sound converter */
86 decoder = NeAACDecOpen();
87
88 if (!decoder) {
89 DEBUGF("FAAD: Decode open error\n");
90 return CODEC_ERROR;
91 }
92
93 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
94 conf->outputFormat = FAAD_FMT_16BIT; /* irrelevant, we don't convert */
95 NeAACDecSetConfiguration(decoder, conf);
96
97 decoder->config.defObjectType = rmctx.codec_extradata[0];
98 decoder->config.defSampleRate = rmctx.sample_rate;
99 err = NeAACDecInit(decoder, NULL, 0, &s, &c);
100
101 if (err) {
102 DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
103 return CODEC_ERROR;
104 }
105
106 /* check for a mid-track resume and force a seek time accordingly */
107 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
108 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
109 /* put number of subpackets to skip in resume_offset */
110 resume_offset /= (rmctx.block_align + PACKET_HEADER_SIZE);
111 param = (int)resume_offset * ((rmctx.block_align * 8 * 1000)/rmctx.bit_rate);
112 action = CODEC_ACTION_SEEK_TIME;
113 }
114 ci->set_elapsed(0);
115 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
116
117 /* The main decoding loop */
118 while (1) {
119 if (action == CODEC_ACTION_NULL)
120 action = ci->get_command(&param);
121
122 if (action == CODEC_ACTION_HALT)
123 break;
124
125 if (action == CODEC_ACTION_SEEK_TIME) {
126 /* Do not allow seeking beyond the file's length */
127 if ((unsigned) param > ci->id3->length) {
128 ci->set_elapsed(ci->id3->length);
129 ci->seek_complete();
130 break;
131 }
132
133 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
134
135 /* Seek to the start of the track */
136 if (param == 0) {
137 ci->set_elapsed(0);
138 ci->seek_complete();
139 action = CODEC_ACTION_NULL;
140 continue;
141 }
142
143 skipped = 0;
144 while(1) {
145 buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
146 pkt_offset = skipped - pkt.length;
147 consumed = rm_get_packet(&buffer, &rmctx, &pkt);
148 if(consumed < 0 && playback_on != 0) {
149 if(playback_on == -1) {
150 /* Error only if packet-parsing failed and playback hadn't started */
151 DEBUGF("rm_get_packet failed\n");
152 ci->seek_complete();
153 return CODEC_ERROR;
154 }
155 else {
156 ci->seek_complete();
157 return CODEC_OK;
158 }
159 }
160 skipped += pkt.length;
161
162 if(pkt.timestamp > (unsigned)param)
163 break;
164
165 ci->advance_buffer(pkt.length);
166 }
167 ci->seek_buffer(pkt_offset + rmctx.data_offset + DATA_HEADER_SIZE);
168 buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
169 NeAACDecPostSeekReset(decoder, decoder->frame);
170 ci->set_elapsed(pkt.timestamp);
171 ci->seek_complete();
172 }
173
174 action = CODEC_ACTION_NULL;
175
176 /* Request the required number of bytes from the input buffer */
177 buffer=ci->request_buffer(&n,rmctx.audio_framesize + 1000);
178 consumed = rm_get_packet(&buffer, &rmctx, &pkt);
179
180 if(consumed < 0 && playback_on != 0) {
181 if(playback_on == -1) {
182 /* Error only if packet-parsing failed and playback hadn't started */
183 DEBUGF("rm_get_packet failed\n");
184 return CODEC_ERROR;
185 }
186 else
187 break;
188 }
189
190 playback_on = 1;
191 if (pkt.timestamp >= ci->id3->length)
192 break;
193
194 /* Decode one block - returned samples will be host-endian */
195 for(i = 0; i < rmctx.sub_packet_cnt; i++) {
196 NeAACDecDecode(decoder, &frame_info, buffer, rmctx.sub_packet_lengths[i]);
197 buffer += rmctx.sub_packet_lengths[i];
198 if (frame_info.error > 0) {
199 DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
200 return CODEC_ERROR;
201 }
202 ci->pcmbuf_insert(decoder->time_out[0],
203 decoder->time_out[1],
204 decoder->frameLength);
205 ci->set_elapsed(pkt.timestamp);
206 }
207
208 ci->advance_buffer(pkt.length);
209 }
210
211 return CODEC_OK;
212}