Rename printf to prevent naming conflict. Also change comment to conform with Rockbox...
[kugel-rb.git] / apps / codecs / aac.c
blob4bc6ffb2f9f9c01b87b9dabe7d5b4e6190695f6a
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 ALIGN real_t gb_time_buffer[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM;
34 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 if (codec_init()) {
73 LOGF("FAAD: Codec init error\n");
74 err = CODEC_ERROR;
75 goto exit;
78 while (!*ci->taginfo_ready && !ci->stop_codec)
79 ci->sleep(1);
81 file_offset = ci->id3->offset;
83 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
84 codec_set_replaygain(ci->id3);
86 stream_create(&input_stream,ci);
88 /* if qtmovie_read returns successfully, the stream is up to
89 * the movie data, which can be used directly by the decoder */
90 if (!qtmovie_read(&input_stream, &demux_res)) {
91 LOGF("FAAD: File init error\n");
92 err = CODEC_ERROR;
93 goto done;
96 /* initialise the sound converter */
97 decoder = NeAACDecOpen();
99 if (!decoder) {
100 LOGF("FAAD: Decode open error\n");
101 err = CODEC_ERROR;
102 goto done;
105 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
106 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
107 NeAACDecSetConfiguration(decoder, conf);
109 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
110 if (err) {
111 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
112 err = CODEC_ERROR;
113 goto done;
116 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
117 * be called after NeAACDecOpen(). */
118 /* A buffer of framelength or 2*frameLenght size must be allocated for
119 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
120 * we do not use the IRAM buffer and keep faad's internal allocation (see
121 * specrec.c). */
122 needed_bufsize = decoder->frameLength;
123 #ifdef SBR_DEC
124 if ((decoder->sbr_present_flag == 1) || (decoder->forceUpSampling == 1))
126 needed_bufsize *= 2;
128 #endif
129 if (needed_bufsize <= GB_BUF_SIZE)
131 decoder->time_out[0] = &gb_time_buffer[0][0];
132 decoder->time_out[1] = &gb_time_buffer[1][0];
134 /* A buffer of with frameLength elements must be allocated for fb_intermed.
135 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
136 * internal allocation (see specrec.c). */
137 needed_bufsize = decoder->frameLength;
138 if (needed_bufsize <= GB_BUF_SIZE)
140 decoder->fb_intermed[0] = &gb_fb_intermed[0][0];
141 decoder->fb_intermed[1] = &gb_fb_intermed[1][0];
144 ci->id3->frequency = s;
146 i = 0;
148 if (file_offset > 0) {
149 if (alac_seek_raw(&demux_res, &input_stream, file_offset,
150 &sound_samples_done, (int*) &i)) {
151 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
152 ci->set_elapsed(elapsed_time);
153 } else {
154 sound_samples_done = 0;
156 } else {
157 sound_samples_done = 0;
160 if (i == 0)
162 lead_trim = ci->id3->lead_trim;
165 /* The main decoding loop */
166 while (i < demux_res.num_sample_byte_sizes) {
167 ci->yield();
169 if (ci->stop_codec || ci->new_track) {
170 break;
173 /* Deal with any pending seek requests */
174 if (ci->seek_time) {
175 if (alac_seek(&demux_res, &input_stream,
176 ((ci->seek_time-1)/10)*(ci->id3->frequency/100),
177 &sound_samples_done, (int*) &i)) {
178 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
179 ci->set_elapsed(elapsed_time);
181 if (i == 0)
183 lead_trim = ci->id3->lead_trim;
186 ci->seek_complete();
189 /* Lookup the length (in samples and bytes) of block i */
190 if (!get_sample_info(&demux_res, i, &sample_duration,
191 &sample_byte_size)) {
192 LOGF("AAC: get_sample_info error\n");
193 err = CODEC_ERROR;
194 goto done;
197 /* There can be gaps between chunks, so skip ahead if needed. It
198 * doesn't seem to happen much, but it probably means that a
199 * "proper" file can have chunks out of order. Why one would want
200 * that an good question (but files with gaps do exist, so who
201 * knows?), so we don't support that - for now, at least.
203 file_offset = get_sample_offset(&demux_res, i);
205 if (file_offset > ci->curpos)
207 ci->advance_buffer(file_offset - ci->curpos);
209 else if (file_offset == 0)
211 LOGF("AAC: get_sample_offset error\n");
212 err = CODEC_ERROR;
213 goto done;
216 /* Request the required number of bytes from the input buffer */
217 buffer=ci->request_buffer(&n,sample_byte_size);
219 /* Decode one block - returned samples will be host-endian */
220 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
222 /* NeAACDecDecode may sometimes return NULL without setting error. */
223 if (ret == NULL || frame_info.error > 0) {
224 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
225 err = CODEC_ERROR;
226 goto done;
229 /* Advance codec buffer (no need to call set_offset because of this) */
230 ci->advance_buffer(n);
232 /* Output the audio */
233 ci->yield();
235 framelength = (frame_info.samples >> 1) - lead_trim;
237 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
239 /* Currently limited to at most one frame of tail_trim.
240 * Seems to be enough.
242 if (ci->id3->tail_trim == 0
243 && sample_duration < (frame_info.samples >> 1))
245 /* Subtract lead_trim just in case we decode a file with
246 * only one audio frame with actual data.
248 framelength = sample_duration - lead_trim;
250 else
252 framelength -= ci->id3->tail_trim;
256 if (framelength > 0)
258 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
259 &decoder->time_out[1][lead_trim],
260 framelength);
263 if (lead_trim > 0)
265 /* frame_info.samples can be 0 for the first frame */
266 lead_trim -= (i > 0 || frame_info.samples)
267 ? (frame_info.samples >> 1) : sample_duration;
269 if (lead_trim < 0 || ci->id3->lead_trim == 0)
271 lead_trim = 0;
275 /* Update the elapsed-time indicator */
276 sound_samples_done += sample_duration;
277 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
278 ci->set_elapsed(elapsed_time);
279 i++;
282 err = CODEC_OK;
284 done:
285 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
287 if (ci->request_next_track())
288 goto next_track;
290 exit:
291 return err;