Rework of libfaad in several areas. Allow removal of malloc with a new define FAAD_ST...
[kugel-rb.git] / apps / codecs / aac.c
blob5638dc49a9ec8d0f9745cb26c8e979d185233b8b
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 /* The maximum buffer size handled by faad. 12 bytes are required by libfaad
31 * as headroom (see libfaad/bits.c). FAAD_BYTE_BUFFER_SIZE bytes are buffered
32 * for each frame. */
33 #define FAAD_BYTE_BUFFER_SIZE (2048-12)
35 /* this is the codec entry point */
36 enum codec_status codec_main(void)
38 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
39 * a bit confusing. Files with sound are split up in chunks, where
40 * each chunk contains one or more samples. Each sample in turn
41 * contains a number of "sound samples" (the kind you refer to with
42 * the sampling frequency).
44 size_t n;
45 demux_res_t demux_res;
46 stream_t input_stream;
47 uint32_t sound_samples_done;
48 uint32_t elapsed_time;
49 int file_offset;
50 int framelength;
51 int lead_trim = 0;
52 unsigned int i;
53 unsigned char* buffer;
54 NeAACDecFrameInfo frame_info;
55 NeAACDecHandle decoder;
56 int err;
57 uint32_t seek_idx = 0;
58 uint32_t s = 0;
59 uint32_t sbr_fac = 1;
60 unsigned char c = 0;
61 void *ret;
63 /* Generic codec initialisation */
64 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
65 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
67 next_track:
68 err = CODEC_OK;
70 /* Clean and initialize decoder structures */
71 memset(&demux_res , 0, sizeof(demux_res));
72 if (codec_init()) {
73 LOGF("FAAD: Codec init error\n");
74 err = CODEC_ERROR;
75 goto exit;
78 if (codec_wait_taginfo() != 0)
79 goto done;
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 #ifdef SBR_DEC
117 /* Check for need of special handling for seek/resume and elapsed time. */
118 if (ci->id3->needs_upsampling_correction) {
119 sbr_fac = 2;
120 } else {
121 sbr_fac = 1;
123 #endif
125 ci->id3->frequency = s;
127 i = 0;
129 if (file_offset > 0) {
130 /* Resume the desired (byte) position. Important: When resuming SBR
131 * upsampling files the resulting sound_samples_done must be expanded
132 * by a factor of 2. This is done via using sbr_fac. */
133 if (m4a_seek_raw(&demux_res, &input_stream, file_offset,
134 &sound_samples_done, (int*) &i)) {
135 sound_samples_done *= sbr_fac;
136 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
137 ci->set_elapsed(elapsed_time);
138 } else {
139 sound_samples_done = 0;
141 NeAACDecPostSeekReset(decoder, i);
142 } else {
143 sound_samples_done = 0;
146 if (i == 0)
148 lead_trim = ci->id3->lead_trim;
151 /* The main decoding loop */
152 while (i < demux_res.num_sample_byte_sizes) {
153 ci->yield();
155 if (ci->stop_codec || ci->new_track) {
156 break;
159 /* Deal with any pending seek requests */
160 if (ci->seek_time) {
161 /* Seek to the desired time position. Important: When seeking in SBR
162 * upsampling files the seek_time must be divided by 2 when calling
163 * m4a_seek and the resulting sound_samples_done must be expanded
164 * by a factor 2. This is done via using sbr_fac. */
165 if (m4a_seek(&demux_res, &input_stream,
166 ((ci->seek_time-1)/10/sbr_fac)*(ci->id3->frequency/100),
167 &sound_samples_done, (int*) &i)) {
168 sound_samples_done *= sbr_fac;
169 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
170 ci->set_elapsed(elapsed_time);
171 seek_idx = 0;
173 if (i == 0)
175 lead_trim = ci->id3->lead_trim;
178 NeAACDecPostSeekReset(decoder, i);
179 ci->seek_complete();
182 /* There can be gaps between chunks, so skip ahead if needed. It
183 * doesn't seem to happen much, but it probably means that a
184 * "proper" file can have chunks out of order. Why one would want
185 * that an good question (but files with gaps do exist, so who
186 * knows?), so we don't support that - for now, at least.
188 file_offset = m4a_check_sample_offset(&demux_res, i, &seek_idx);
190 if (file_offset > ci->curpos)
192 ci->advance_buffer(file_offset - ci->curpos);
194 else if (file_offset == 0)
196 LOGF("AAC: get_sample_offset error\n");
197 err = CODEC_ERROR;
198 goto done;
201 /* Request the required number of bytes from the input buffer */
202 buffer=ci->request_buffer(&n, FAAD_BYTE_BUFFER_SIZE);
204 /* Decode one block - returned samples will be host-endian */
205 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
207 /* NeAACDecDecode may sometimes return NULL without setting error. */
208 if (ret == NULL || frame_info.error > 0) {
209 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
210 err = CODEC_ERROR;
211 goto done;
214 /* Advance codec buffer (no need to call set_offset because of this) */
215 ci->advance_buffer(frame_info.bytesconsumed);
217 /* Output the audio */
218 ci->yield();
220 /* Gather number of samples for the decoded frame. */
221 framelength = (frame_info.samples >> 1) - lead_trim;
223 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
225 framelength -= ci->id3->tail_trim;
228 if (framelength > 0)
230 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
231 &decoder->time_out[1][lead_trim],
232 framelength);
235 if (lead_trim > 0)
237 /* frame_info.samples can be 0 for the first frame */
238 lead_trim -= (i > 0 || frame_info.samples)
239 ? (frame_info.samples >> 1) : (uint32_t)framelength;
241 if (lead_trim < 0 || ci->id3->lead_trim == 0)
243 lead_trim = 0;
247 /* Update the elapsed-time indicator */
248 sound_samples_done += framelength;
249 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
250 ci->set_elapsed(elapsed_time);
251 i++;
254 done:
255 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
257 if (ci->request_next_track())
258 goto next_track;
260 exit:
261 return err;