Explicitely say 'minutes' when speaking the battery time, fixes FS#11932.
[kugel-rb.git] / apps / codecs / aac.c
blob34239864d19d86867879b1bde8fa934da2edc6d2
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 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 NeAACDecFrameInfo frame_info;
59 NeAACDecHandle decoder;
60 int err;
61 uint32_t s = 0;
62 uint32_t sbr_fac = 1;
63 unsigned char c = 0;
64 void *ret;
66 /* Generic codec initialisation */
67 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
68 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
70 next_track:
71 err = CODEC_OK;
73 /* Clean and initialize decoder structures */
74 memset(&demux_res , 0, sizeof(demux_res));
75 if (codec_init()) {
76 LOGF("FAAD: Codec init error\n");
77 err = CODEC_ERROR;
78 goto exit;
81 while (!*ci->taginfo_ready && !ci->stop_codec)
82 ci->sleep(1);
84 file_offset = ci->id3->offset;
86 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
87 codec_set_replaygain(ci->id3);
89 stream_create(&input_stream,ci);
91 /* if qtmovie_read returns successfully, the stream is up to
92 * the movie data, which can be used directly by the decoder */
93 if (!qtmovie_read(&input_stream, &demux_res)) {
94 LOGF("FAAD: File init error\n");
95 err = CODEC_ERROR;
96 goto done;
99 /* initialise the sound converter */
100 decoder = NeAACDecOpen();
102 if (!decoder) {
103 LOGF("FAAD: Decode open error\n");
104 err = CODEC_ERROR;
105 goto done;
108 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
109 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
110 NeAACDecSetConfiguration(decoder, conf);
112 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
113 if (err) {
114 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
115 err = CODEC_ERROR;
116 goto done;
119 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
120 * be called after NeAACDecOpen(). */
121 /* A buffer of framelength or 2*frameLenght size must be allocated for
122 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
123 * we do not use the IRAM buffer and keep faad's internal allocation (see
124 * specrec.c). */
125 needed_bufsize = decoder->frameLength;
126 #ifdef SBR_DEC
127 if ((decoder->sbr_present_flag == 1) || (decoder->forceUpSampling == 1))
129 needed_bufsize *= 2;
131 #endif
132 if (needed_bufsize <= GB_BUF_SIZE)
134 decoder->time_out[0] = &gb_time_buffer[0][0];
135 decoder->time_out[1] = &gb_time_buffer[1][0];
137 /* A buffer of with frameLength elements must be allocated for fb_intermed.
138 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
139 * internal allocation (see specrec.c). */
140 needed_bufsize = decoder->frameLength;
141 if (needed_bufsize <= GB_BUF_SIZE)
143 decoder->fb_intermed[0] = &gb_fb_intermed[0][0];
144 decoder->fb_intermed[1] = &gb_fb_intermed[1][0];
147 #ifdef SBR_DEC
148 /* Check for need of special handling for seek/resume and elapsed time. */
149 if (ci->id3->needs_upsampling_correction) {
150 sbr_fac = 2;
151 } else {
152 sbr_fac = 1;
154 #endif
156 ci->id3->frequency = s;
158 i = 0;
160 if (file_offset > 0) {
161 /* Resume the desired (byte) position. Important: When resuming SBR
162 * upsampling files the resulting sound_samples_done must be expanded
163 * by a factor of 2. This is done via using sbr_fac. */
164 if (alac_seek_raw(&demux_res, &input_stream, file_offset,
165 &sound_samples_done, (int*) &i)) {
166 sound_samples_done *= sbr_fac;
167 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
168 ci->set_elapsed(elapsed_time);
169 } else {
170 sound_samples_done = 0;
172 } else {
173 sound_samples_done = 0;
176 if (i == 0)
178 lead_trim = ci->id3->lead_trim;
181 /* The main decoding loop */
182 while (i < demux_res.num_sample_byte_sizes) {
183 ci->yield();
185 if (ci->stop_codec || ci->new_track) {
186 break;
189 /* Deal with any pending seek requests */
190 if (ci->seek_time) {
191 /* Seek to the desired time position. Important: When seeking in SBR
192 * upsampling files the seek_time must be divided by 2 when calling
193 * alac_seek and the resulting sound_samples_done must be expanded
194 * by a factor 2. This is done via using sbr_fac. */
195 if (alac_seek(&demux_res, &input_stream,
196 ((ci->seek_time-1)/10/sbr_fac)*(ci->id3->frequency/100),
197 &sound_samples_done, (int*) &i)) {
198 sound_samples_done *= sbr_fac;
199 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
200 ci->set_elapsed(elapsed_time);
202 if (i == 0)
204 lead_trim = ci->id3->lead_trim;
207 ci->seek_complete();
210 /* Lookup the length (in samples and bytes) of block i */
211 if (!get_sample_info(&demux_res, i, &sample_duration,
212 &sample_byte_size)) {
213 LOGF("AAC: get_sample_info error\n");
214 err = CODEC_ERROR;
215 goto done;
218 /* There can be gaps between chunks, so skip ahead if needed. It
219 * doesn't seem to happen much, but it probably means that a
220 * "proper" file can have chunks out of order. Why one would want
221 * that an good question (but files with gaps do exist, so who
222 * knows?), so we don't support that - for now, at least.
224 file_offset = get_sample_offset(&demux_res, i);
226 if (file_offset > ci->curpos)
228 ci->advance_buffer(file_offset - ci->curpos);
230 else if (file_offset == 0)
232 LOGF("AAC: get_sample_offset error\n");
233 err = CODEC_ERROR;
234 goto done;
237 /* Request the required number of bytes from the input buffer */
238 buffer=ci->request_buffer(&n,sample_byte_size);
240 /* Decode one block - returned samples will be host-endian */
241 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
243 /* NeAACDecDecode may sometimes return NULL without setting error. */
244 if (ret == NULL || frame_info.error > 0) {
245 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
246 err = CODEC_ERROR;
247 goto done;
250 /* Advance codec buffer (no need to call set_offset because of this) */
251 ci->advance_buffer(n);
253 /* Output the audio */
254 ci->yield();
256 /* Ensure correct sample_duration is used. For SBR upsampling files
257 * sample_duration is only half the size of real output frame size. */
258 sample_duration *= sbr_fac;
260 framelength = (frame_info.samples >> 1) - lead_trim;
262 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
264 /* Currently limited to at most one frame of tail_trim.
265 * Seems to be enough.
267 if (ci->id3->tail_trim == 0
268 && sample_duration < (frame_info.samples >> 1))
270 /* Subtract lead_trim just in case we decode a file with
271 * only one audio frame with actual data.
273 framelength = sample_duration - lead_trim;
275 else
277 framelength -= ci->id3->tail_trim;
281 if (framelength > 0)
283 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
284 &decoder->time_out[1][lead_trim],
285 framelength);
288 if (lead_trim > 0)
290 /* frame_info.samples can be 0 for the first frame */
291 lead_trim -= (i > 0 || frame_info.samples)
292 ? (frame_info.samples >> 1) : (uint32_t)framelength;
294 if (lead_trim < 0 || ci->id3->lead_trim == 0)
296 lead_trim = 0;
300 /* Update the elapsed-time indicator */
301 sound_samples_done += framelength;
302 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
303 ci->set_elapsed(elapsed_time);
304 i++;
307 err = CODEC_OK;
309 done:
310 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
312 if (ci->request_next_track())
313 goto next_track;
315 exit:
316 return err;