Force alignment of various data structures in libmad. Speeds up Gigabeat S decoding...
[kugel-rb.git] / apps / codecs / aac.c
blobdbc4782505c4f90ef27d34860ccd63930570c5dc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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 "libm4a/m4a.h"
24 #include "libfaad/common.h"
25 #include "libfaad/structs.h"
26 #include "libfaad/decoder.h"
28 CODEC_HEADER
30 /* Global buffers to be used in the mdct synthesis. This way the arrays can
31 * be moved to IRAM for some targets */
32 #define GB_BUF_SIZE 1024
33 static ALIGN real_t gb_time_buffer[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM;
34 static ALIGN real_t gb_fb_intermed[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM;
36 /* this is the codec entry point */
37 enum codec_status codec_main(void)
39 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
40 * a bit confusing. Files with sound are split up in chunks, where
41 * each chunk contains one or more samples. Each sample in turn
42 * contains a number of "sound samples" (the kind you refer to with
43 * the sampling frequency).
45 size_t n;
46 static demux_res_t demux_res;
47 stream_t input_stream;
48 uint32_t sound_samples_done;
49 uint32_t elapsed_time;
50 uint32_t sample_duration;
51 uint32_t sample_byte_size;
52 int file_offset;
53 int framelength;
54 int lead_trim = 0;
55 int needed_bufsize;
56 unsigned int i;
57 unsigned char* buffer;
58 static NeAACDecFrameInfo frame_info;
59 NeAACDecHandle decoder;
60 int err;
61 uint32_t s = 0;
62 unsigned char c = 0;
63 void *ret;
65 /* Generic codec initialisation */
66 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
67 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
69 next_track:
70 err = CODEC_OK;
72 /* Clean and initialize decoder structures */
73 memset(&demux_res , 0, sizeof(demux_res));
74 if (codec_init()) {
75 LOGF("FAAD: Codec init error\n");
76 err = CODEC_ERROR;
77 goto exit;
80 while (!*ci->taginfo_ready && !ci->stop_codec)
81 ci->sleep(1);
83 file_offset = ci->id3->offset;
85 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
86 codec_set_replaygain(ci->id3);
88 stream_create(&input_stream,ci);
90 /* if qtmovie_read returns successfully, the stream is up to
91 * the movie data, which can be used directly by the decoder */
92 if (!qtmovie_read(&input_stream, &demux_res)) {
93 LOGF("FAAD: File init error\n");
94 err = CODEC_ERROR;
95 goto done;
98 /* initialise the sound converter */
99 decoder = NeAACDecOpen();
101 if (!decoder) {
102 LOGF("FAAD: Decode open error\n");
103 err = CODEC_ERROR;
104 goto done;
107 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
108 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
109 NeAACDecSetConfiguration(decoder, conf);
111 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
112 if (err) {
113 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
114 err = CODEC_ERROR;
115 goto done;
118 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
119 * be called after NeAACDecOpen(). */
120 /* A buffer of framelength or 2*frameLenght size must be allocated for
121 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
122 * we do not use the IRAM buffer and keep faad's internal allocation (see
123 * specrec.c). */
124 needed_bufsize = decoder->frameLength;
125 #ifdef SBR_DEC
126 if ((decoder->sbr_present_flag == 1) || (decoder->forceUpSampling == 1))
128 needed_bufsize *= 2;
130 #endif
131 if (needed_bufsize <= GB_BUF_SIZE)
133 decoder->time_out[0] = &gb_time_buffer[0][0];
134 decoder->time_out[1] = &gb_time_buffer[1][0];
136 /* A buffer of with frameLength elements must be allocated for fb_intermed.
137 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
138 * internal allocation (see specrec.c). */
139 needed_bufsize = decoder->frameLength;
140 if (needed_bufsize <= GB_BUF_SIZE)
142 decoder->fb_intermed[0] = &gb_fb_intermed[0][0];
143 decoder->fb_intermed[1] = &gb_fb_intermed[1][0];
146 ci->id3->frequency = s;
148 i = 0;
150 if (file_offset > 0) {
151 if (alac_seek_raw(&demux_res, &input_stream, file_offset,
152 &sound_samples_done, (int*) &i)) {
153 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
154 ci->set_elapsed(elapsed_time);
155 } else {
156 sound_samples_done = 0;
158 } else {
159 sound_samples_done = 0;
162 if (i == 0)
164 lead_trim = ci->id3->lead_trim;
167 /* The main decoding loop */
168 while (i < demux_res.num_sample_byte_sizes) {
169 ci->yield();
171 if (ci->stop_codec || ci->new_track) {
172 break;
175 /* Deal with any pending seek requests */
176 if (ci->seek_time) {
177 if (alac_seek(&demux_res, &input_stream,
178 ((ci->seek_time-1)/10)*(ci->id3->frequency/100),
179 &sound_samples_done, (int*) &i)) {
180 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
181 ci->set_elapsed(elapsed_time);
183 if (i == 0)
185 lead_trim = ci->id3->lead_trim;
188 ci->seek_complete();
191 /* Lookup the length (in samples and bytes) of block i */
192 if (!get_sample_info(&demux_res, i, &sample_duration,
193 &sample_byte_size)) {
194 LOGF("AAC: get_sample_info error\n");
195 err = CODEC_ERROR;
196 goto done;
199 /* There can be gaps between chunks, so skip ahead if needed. It
200 * doesn't seem to happen much, but it probably means that a
201 * "proper" file can have chunks out of order. Why one would want
202 * that an good question (but files with gaps do exist, so who
203 * knows?), so we don't support that - for now, at least.
205 file_offset = get_sample_offset(&demux_res, i);
207 if (file_offset > ci->curpos)
209 ci->advance_buffer(file_offset - ci->curpos);
211 else if (file_offset == 0)
213 LOGF("AAC: get_sample_offset error\n");
214 err = CODEC_ERROR;
215 goto done;
218 /* Request the required number of bytes from the input buffer */
219 buffer=ci->request_buffer(&n,sample_byte_size);
221 /* Decode one block - returned samples will be host-endian */
222 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
224 /* NeAACDecDecode may sometimes return NULL without setting error. */
225 if (ret == NULL || frame_info.error > 0) {
226 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
227 err = CODEC_ERROR;
228 goto done;
231 /* Advance codec buffer (no need to call set_offset because of this) */
232 ci->advance_buffer(n);
234 /* Output the audio */
235 ci->yield();
237 framelength = (frame_info.samples >> 1) - lead_trim;
239 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
241 /* Currently limited to at most one frame of tail_trim.
242 * Seems to be enough.
244 if (ci->id3->tail_trim == 0
245 && sample_duration < (frame_info.samples >> 1))
247 /* Subtract lead_trim just in case we decode a file with
248 * only one audio frame with actual data.
250 framelength = sample_duration - lead_trim;
252 else
254 framelength -= ci->id3->tail_trim;
258 if (framelength > 0)
260 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
261 &decoder->time_out[1][lead_trim],
262 framelength);
265 if (lead_trim > 0)
267 /* frame_info.samples can be 0 for the first frame */
268 lead_trim -= (i > 0 || frame_info.samples)
269 ? (frame_info.samples >> 1) : sample_duration;
271 if (lead_trim < 0 || ci->id3->lead_trim == 0)
273 lead_trim = 0;
277 /* Update the elapsed-time indicator */
278 sound_samples_done += sample_duration;
279 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
280 ci->set_elapsed(elapsed_time);
281 i++;
284 err = CODEC_OK;
286 done:
287 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
289 if (ci->request_next_track())
290 goto next_track;
292 exit:
293 return err;