Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / raac.c
blob93972f557b9694bcfb56bd4f445faeeb9c11f2eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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 #include "libfaad/output.h"
29 CODEC_HEADER
31 /* Global buffers to be used in the mdct synthesis. This way the arrays can
32 * be moved to IRAM for some targets */
33 #define GB_BUF_SIZE 1024
34 ALIGN real_t gb_time_buffer[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM;
35 ALIGN real_t gb_fb_intermed[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM;
38 static void init_rm(RMContext *rmctx)
40 memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
43 RMContext rmctx;
44 RMPacket pkt;
45 /* this is the codec entry point */
46 enum codec_status codec_main(void)
48 static NeAACDecFrameInfo frame_info;
49 NeAACDecHandle decoder;
50 size_t n;
51 void *ret;
52 int needed_bufsize;
53 unsigned int i;
54 unsigned char* buffer;
55 int err, consumed, pkt_offset, skipped = 0;
56 uint32_t s = 0; /* sample rate */
57 unsigned char c = 0; /* channels */
58 int playback_on = -1;
59 size_t resume_offset = ci->id3->offset;
61 /* Generic codec initialisation */
62 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
63 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
65 next_track:
66 err = CODEC_OK;
68 if (codec_init()) {
69 DEBUGF("FAAD: Codec init error\n");
70 return CODEC_ERROR;
73 while (!*ci->taginfo_ready && !ci->stop_codec)
74 ci->sleep(1);
76 ci->memset(&rmctx,0,sizeof(RMContext));
77 ci->memset(&pkt,0,sizeof(RMPacket));
78 init_rm(&rmctx);
79 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
80 codec_set_replaygain(ci->id3);
82 /* initialise the sound converter */
83 decoder = NeAACDecOpen();
85 if (!decoder) {
86 DEBUGF("FAAD: Decode open error\n");
87 err = CODEC_ERROR;
88 goto done;
90 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
91 conf->outputFormat = FAAD_FMT_16BIT; /* irrelevant, we don't convert */
92 NeAACDecSetConfiguration(decoder, conf);
94 decoder->config.defObjectType = rmctx.codec_extradata[0];
95 decoder->config.defSampleRate = rmctx.sample_rate;
96 err = NeAACDecInit(decoder, NULL, 0, &s, &c);
98 if (err) {
99 DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
100 err = CODEC_ERROR;
101 goto done;
104 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
105 * be called after NeAACDecOpen(). */
106 /* A buffer of framelength or 2*frameLenght size must be allocated for
107 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
108 * we do not use the IRAM buffer and keep faad's internal allocation (see
109 * specrec.c). */
110 needed_bufsize = decoder->frameLength;
111 #ifdef SBR_DEC
112 if ((decoder->sbr_present_flag == 1) || (decoder->forceUpSampling == 1))
114 needed_bufsize *= 2;
116 #endif
117 if (needed_bufsize <= GB_BUF_SIZE)
119 decoder->time_out[0] = &gb_time_buffer[0][0];
120 decoder->time_out[1] = &gb_time_buffer[1][0];
122 /* A buffer of with frameLength elements must be allocated for fb_intermed.
123 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
124 * internal allocation (see specrec.c). */
125 needed_bufsize = decoder->frameLength;
126 if (needed_bufsize <= GB_BUF_SIZE)
128 decoder->fb_intermed[0] = &gb_fb_intermed[0][0];
129 decoder->fb_intermed[1] = &gb_fb_intermed[1][0];
132 /* check for a mid-track resume and force a seek time accordingly */
133 if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
134 resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
135 /* put number of subpackets to skip in resume_offset */
136 resume_offset /= (rmctx.block_align + PACKET_HEADER_SIZE);
137 ci->seek_time = (int)resume_offset * ((rmctx.block_align * 8 * 1000)/rmctx.bit_rate);
140 ci->id3->frequency = s;
141 ci->set_elapsed(0);
142 ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
144 /* The main decoding loop */
145 seek_start:
146 while (1) {
147 ci->yield();
148 if (ci->stop_codec || ci->new_track) {
149 break;
152 if (ci->seek_time) {
154 /* Do not allow seeking beyond the file's length */
155 if ((unsigned) ci->seek_time > ci->id3->length) {
156 ci->seek_complete();
157 goto done;
160 ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
162 /* Seek to the start of the track */
163 if (ci->seek_time == 1) {
164 ci->set_elapsed(0);
165 ci->seek_complete();
166 goto seek_start;
169 skipped = 0;
170 while(1) {
171 buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
172 pkt_offset = skipped - pkt.length;
173 consumed = rm_get_packet(&buffer, &rmctx, &pkt);
174 if(consumed < 0 && playback_on != 0) {
175 if(playback_on == -1) {
176 /* Error only if packet-parsing failed and playback hadn't started */
177 DEBUGF("rm_get_packet failed\n");
178 return CODEC_ERROR;
180 else
181 goto done;
183 skipped += pkt.length;
184 if(pkt.timestamp > (unsigned)ci->seek_time) break;
185 ci->advance_buffer(pkt.length);
187 ci->seek_buffer(pkt_offset + rmctx.data_offset + DATA_HEADER_SIZE);
188 buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
189 ci->seek_complete();
192 /* Request the required number of bytes from the input buffer */
193 buffer=ci->request_buffer(&n,rmctx.audio_framesize + 1000);
194 consumed = rm_get_packet(&buffer, &rmctx, &pkt);
196 if(consumed < 0 && playback_on != 0) {
197 if(playback_on == -1) {
198 /* Error only if packet-parsing failed and playback hadn't started */
199 DEBUGF("rm_get_packet failed\n");
200 return CODEC_ERROR;
202 else
203 goto done;
206 playback_on = 1;
207 if (pkt.timestamp >= ci->id3->length)
208 goto done;
209 /* Decode one block - returned samples will be host-endian */
210 for(i = 0; i < rmctx.sub_packet_cnt; i++) {
211 ret = NeAACDecDecode(decoder, &frame_info, buffer, rmctx.sub_packet_lengths[i]);
212 buffer += rmctx.sub_packet_lengths[i];
213 if (frame_info.error > 0) {
214 DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
215 err = CODEC_ERROR;
216 goto exit;
218 ci->pcmbuf_insert(decoder->time_out[0],
219 decoder->time_out[1],
220 decoder->frameLength);
221 ci->set_elapsed(pkt.timestamp);
224 ci->advance_buffer(pkt.length);
227 err = CODEC_OK;
229 done:
230 if (ci->request_next_track())
231 goto next_track;
233 exit:
234 return err;