Refactor aac decoder as preparation for upcoming m4a changes. The aac decoder does...
[kugel-rb.git] / apps / codecs / aac.c
blob849d87bedf347cf3219b18fad7095c37d9fe725a
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 real_t gb_time_buffer[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
34 static real_t gb_fb_intermed[2][GB_BUF_SIZE] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
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 demux_res_t demux_res;
47 stream_t input_stream;
48 uint32_t sound_samples_done;
49 uint32_t elapsed_time;
50 int file_offset;
51 int framelength;
52 int lead_trim = 0;
53 int needed_bufsize;
54 unsigned int i;
55 unsigned char* buffer;
56 NeAACDecFrameInfo frame_info;
57 NeAACDecHandle decoder;
58 int err;
59 uint32_t s = 0;
60 uint32_t sbr_fac = 1;
61 unsigned char c = 0;
62 void *ret;
64 /* Generic codec initialisation */
65 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
66 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
68 next_track:
69 err = CODEC_OK;
71 /* Clean and initialize decoder structures */
72 memset(&demux_res , 0, sizeof(demux_res));
73 if (codec_init()) {
74 LOGF("FAAD: Codec init error\n");
75 err = CODEC_ERROR;
76 goto exit;
79 if (codec_wait_taginfo() != 0)
80 goto done;
82 file_offset = ci->id3->offset;
84 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
85 codec_set_replaygain(ci->id3);
87 stream_create(&input_stream,ci);
89 /* if qtmovie_read returns successfully, the stream is up to
90 * the movie data, which can be used directly by the decoder */
91 if (!qtmovie_read(&input_stream, &demux_res)) {
92 LOGF("FAAD: File init error\n");
93 err = CODEC_ERROR;
94 goto done;
97 /* initialise the sound converter */
98 decoder = NeAACDecOpen();
100 if (!decoder) {
101 LOGF("FAAD: Decode open error\n");
102 err = CODEC_ERROR;
103 goto done;
106 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
107 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
108 NeAACDecSetConfiguration(decoder, conf);
110 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
111 if (err) {
112 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
113 err = CODEC_ERROR;
114 goto done;
117 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
118 * be called after NeAACDecOpen(). */
119 /* A buffer of framelength or 2*frameLenght size must be allocated for
120 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
121 * we do not use the IRAM buffer and keep faad's internal allocation (see
122 * specrec.c). */
123 needed_bufsize = decoder->frameLength;
124 #ifdef SBR_DEC
125 if ((decoder->sbr_present_flag == 1) || (decoder->forceUpSampling == 1))
127 needed_bufsize *= 2;
129 #endif
130 if (needed_bufsize <= GB_BUF_SIZE)
132 decoder->time_out[0] = &gb_time_buffer[0][0];
133 decoder->time_out[1] = &gb_time_buffer[1][0];
135 /* A buffer of with frameLength elements must be allocated for fb_intermed.
136 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
137 * internal allocation (see specrec.c). */
138 needed_bufsize = decoder->frameLength;
139 if (needed_bufsize <= GB_BUF_SIZE)
141 decoder->fb_intermed[0] = &gb_fb_intermed[0][0];
142 decoder->fb_intermed[1] = &gb_fb_intermed[1][0];
145 #ifdef SBR_DEC
146 /* Check for need of special handling for seek/resume and elapsed time. */
147 if (ci->id3->needs_upsampling_correction) {
148 sbr_fac = 2;
149 } else {
150 sbr_fac = 1;
152 #endif
154 ci->id3->frequency = s;
156 i = 0;
158 if (file_offset > 0) {
159 /* Resume the desired (byte) position. Important: When resuming SBR
160 * upsampling files the resulting sound_samples_done must be expanded
161 * by a factor of 2. This is done via using sbr_fac. */
162 if (alac_seek_raw(&demux_res, &input_stream, file_offset,
163 &sound_samples_done, (int*) &i)) {
164 sound_samples_done *= sbr_fac;
165 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
166 ci->set_elapsed(elapsed_time);
167 } else {
168 sound_samples_done = 0;
170 } else {
171 sound_samples_done = 0;
174 if (i == 0)
176 lead_trim = ci->id3->lead_trim;
179 /* The main decoding loop */
180 while (i < demux_res.num_sample_byte_sizes) {
181 ci->yield();
183 if (ci->stop_codec || ci->new_track) {
184 break;
187 /* Deal with any pending seek requests */
188 if (ci->seek_time) {
189 /* Seek to the desired time position. Important: When seeking in SBR
190 * upsampling files the seek_time must be divided by 2 when calling
191 * alac_seek and the resulting sound_samples_done must be expanded
192 * by a factor 2. This is done via using sbr_fac. */
193 if (alac_seek(&demux_res, &input_stream,
194 ((ci->seek_time-1)/10/sbr_fac)*(ci->id3->frequency/100),
195 &sound_samples_done, (int*) &i)) {
196 sound_samples_done *= sbr_fac;
197 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
198 ci->set_elapsed(elapsed_time);
200 if (i == 0)
202 lead_trim = ci->id3->lead_trim;
205 ci->seek_complete();
208 /* There can be gaps between chunks, so skip ahead if needed. It
209 * doesn't seem to happen much, but it probably means that a
210 * "proper" file can have chunks out of order. Why one would want
211 * that an good question (but files with gaps do exist, so who
212 * knows?), so we don't support that - for now, at least.
214 file_offset = get_sample_offset(&demux_res, i);
216 if (file_offset > ci->curpos)
218 ci->advance_buffer(file_offset - ci->curpos);
220 else if (file_offset == 0)
222 LOGF("AAC: get_sample_offset error\n");
223 err = CODEC_ERROR;
224 goto done;
227 /* Request the required number of bytes from the input buffer */
228 buffer=ci->request_buffer(&n, demux_res.sample_byte_size[i]);
230 /* Decode one block - returned samples will be host-endian */
231 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
233 /* NeAACDecDecode may sometimes return NULL without setting error. */
234 if (ret == NULL || frame_info.error > 0) {
235 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
236 err = CODEC_ERROR;
237 goto done;
240 /* Advance codec buffer (no need to call set_offset because of this) */
241 ci->advance_buffer(frame_info.bytesconsumed);
243 /* Output the audio */
244 ci->yield();
246 /* Gather number of samples for the decoded frame. */
247 framelength = (frame_info.samples >> 1) - lead_trim;
249 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
251 framelength -= ci->id3->tail_trim;
254 if (framelength > 0)
256 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
257 &decoder->time_out[1][lead_trim],
258 framelength);
261 if (lead_trim > 0)
263 /* frame_info.samples can be 0 for the first frame */
264 lead_trim -= (i > 0 || frame_info.samples)
265 ? (frame_info.samples >> 1) : (uint32_t)framelength;
267 if (lead_trim < 0 || ci->id3->lead_trim == 0)
269 lead_trim = 0;
273 /* Update the elapsed-time indicator */
274 sound_samples_done += framelength;
275 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
276 ci->set_elapsed(elapsed_time);
277 i++;
280 done:
281 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
283 if (ci->request_next_track())
284 goto next_track;
286 exit:
287 return err;