The manual should reflect that some synthesizer codecs are switched off for low memor...
[kugel-rb.git] / apps / codecs / aac.c
blob52e08c7b56c965a067bd247a9a62713429761e02
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(enum codec_entry_call_reason reason)
38 if (reason == CODEC_LOAD) {
39 /* Generic codec initialisation */
40 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
41 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
44 return CODEC_OK;
47 /* this is called for each file to process */
48 enum codec_status codec_run(void)
50 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
51 * a bit confusing. Files with sound are split up in chunks, where
52 * each chunk contains one or more samples. Each sample in turn
53 * contains a number of "sound samples" (the kind you refer to with
54 * the sampling frequency).
56 size_t n;
57 demux_res_t demux_res;
58 stream_t input_stream;
59 uint32_t sound_samples_done;
60 uint32_t elapsed_time;
61 int file_offset;
62 int framelength;
63 int lead_trim = 0;
64 unsigned int frame_samples;
65 unsigned int i;
66 unsigned char* buffer;
67 NeAACDecFrameInfo frame_info;
68 NeAACDecHandle decoder;
69 int err;
70 uint32_t seek_idx = 0;
71 uint32_t s = 0;
72 uint32_t sbr_fac = 1;
73 unsigned char c = 0;
74 void *ret;
75 intptr_t param;
76 bool empty_first_frame = false;
78 /* Clean and initialize decoder structures */
79 memset(&demux_res , 0, sizeof(demux_res));
80 if (codec_init()) {
81 LOGF("FAAD: Codec init error\n");
82 return CODEC_ERROR;
85 file_offset = ci->id3->offset;
87 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
88 codec_set_replaygain(ci->id3);
90 stream_create(&input_stream,ci);
92 ci->seek_buffer(ci->id3->first_frame_offset);
94 /* if qtmovie_read returns successfully, the stream is up to
95 * the movie data, which can be used directly by the decoder */
96 if (!qtmovie_read(&input_stream, &demux_res)) {
97 LOGF("FAAD: File init error\n");
98 return CODEC_ERROR;
101 /* initialise the sound converter */
102 decoder = NeAACDecOpen();
104 if (!decoder) {
105 LOGF("FAAD: Decode open error\n");
106 return CODEC_ERROR;
109 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
110 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
111 NeAACDecSetConfiguration(decoder, conf);
113 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
114 if (err) {
115 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
116 return CODEC_ERROR;
119 #ifdef SBR_DEC
120 /* Check for need of special handling for seek/resume and elapsed time. */
121 if (ci->id3->needs_upsampling_correction) {
122 sbr_fac = 2;
123 } else {
124 sbr_fac = 1;
126 #endif
128 i = 0;
130 if (file_offset > 0) {
131 /* Resume the desired (byte) position. Important: When resuming SBR
132 * upsampling files the resulting sound_samples_done must be expanded
133 * by a factor of 2. This is done via using sbr_fac. */
134 if (m4a_seek_raw(&demux_res, &input_stream, file_offset,
135 &sound_samples_done, (int*) &i)) {
136 sound_samples_done *= sbr_fac;
137 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
138 ci->set_elapsed(elapsed_time);
139 } else {
140 sound_samples_done = 0;
142 NeAACDecPostSeekReset(decoder, i);
143 } else {
144 sound_samples_done = 0;
147 if (i == 0)
149 lead_trim = ci->id3->lead_trim;
152 /* The main decoding loop */
153 while (i < demux_res.num_sample_byte_sizes) {
154 enum codec_command_action action = ci->get_command(&param);
156 if (action == CODEC_ACTION_HALT)
157 break;
159 /* Deal with any pending seek requests */
160 if (action == CODEC_ACTION_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 (param/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 return CODEC_ERROR;
200 /* Request the required number of bytes from the input buffer */
201 buffer=ci->request_buffer(&n, FAAD_BYTE_BUFFER_SIZE);
203 /* Decode one block - returned samples will be host-endian */
204 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
206 /* NeAACDecDecode may sometimes return NULL without setting error. */
207 if (ret == NULL || frame_info.error > 0) {
208 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
209 return CODEC_ERROR;
212 /* Advance codec buffer (no need to call set_offset because of this) */
213 ci->advance_buffer(frame_info.bytesconsumed);
215 /* Output the audio */
216 ci->yield();
218 frame_samples = frame_info.samples >> 1;
220 if (empty_first_frame)
222 /* Remove the first frame from lead_trim, under the assumption
223 * that it had the same size as this frame
225 empty_first_frame = false;
226 lead_trim -= frame_samples;
228 if (lead_trim < 0)
230 lead_trim = 0;
234 /* Gather number of samples for the decoded frame. */
235 framelength = frame_samples - lead_trim;
237 if (i == demux_res.num_sample_byte_sizes - 1)
239 // Size of the last frame
240 const uint32_t sample_duration = (demux_res.num_time_to_samples > 0) ?
241 demux_res.time_to_sample[demux_res.num_time_to_samples - 1].sample_duration :
242 frame_samples;
244 /* Currently limited to at most one frame of tail_trim.
245 * Seems to be enough.
247 if (ci->id3->tail_trim == 0 && sample_duration < frame_samples)
249 /* Subtract lead_trim just in case we decode a file with only
250 * one audio frame with actual data (lead_trim is usually zero
251 * here).
253 framelength = sample_duration - lead_trim;
255 else
257 framelength -= ci->id3->tail_trim;
261 if (framelength > 0)
263 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
264 &decoder->time_out[1][lead_trim],
265 framelength);
266 sound_samples_done += framelength;
267 /* Update the elapsed-time indicator */
268 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
269 ci->set_elapsed(elapsed_time);
272 if (lead_trim > 0)
274 /* frame_info.samples can be 0 for frame 0. We still want to
275 * remove it from lead_trim, so do that during frame 1.
277 if (0 == i && 0 == frame_info.samples)
279 empty_first_frame = true;
282 lead_trim -= frame_samples;
284 if (lead_trim < 0)
286 lead_trim = 0;
290 ++i;
293 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
294 return CODEC_OK;