synaptics: add a small delay in syn_get_data() for PB Vibe. Touchpad doesn't work...
[kugel-rb.git] / apps / codecs / aac.c
blob0f38ea114593d3cd30aef3facd0dacbe66b251b3
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 /* this is the codec entry point */
31 enum codec_status codec_main(void)
33 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
34 * a bit confusing. Files with sound are split up in chunks, where
35 * each chunk contains one or more samples. Each sample in turn
36 * contains a number of "sound samples" (the kind you refer to with
37 * the sampling frequency).
39 size_t n;
40 static demux_res_t demux_res;
41 stream_t input_stream;
42 uint32_t sound_samples_done;
43 uint32_t elapsed_time;
44 uint32_t sample_duration;
45 uint32_t sample_byte_size;
46 int file_offset;
47 int framelength;
48 int lead_trim = 0;
49 unsigned int i;
50 unsigned char* buffer;
51 static NeAACDecFrameInfo frame_info;
52 NeAACDecHandle decoder;
53 int err;
54 uint32_t s = 0;
55 unsigned char c = 0;
56 void *ret;
58 /* Generic codec initialisation */
59 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
60 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
62 next_track:
63 err = CODEC_OK;
65 if (codec_init()) {
66 LOGF("FAAD: Codec init error\n");
67 err = CODEC_ERROR;
68 goto exit;
71 while (!*ci->taginfo_ready && !ci->stop_codec)
72 ci->sleep(1);
74 file_offset = ci->id3->offset;
76 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
77 codec_set_replaygain(ci->id3);
79 stream_create(&input_stream,ci);
81 /* if qtmovie_read returns successfully, the stream is up to
82 * the movie data, which can be used directly by the decoder */
83 if (!qtmovie_read(&input_stream, &demux_res)) {
84 LOGF("FAAD: File init error\n");
85 err = CODEC_ERROR;
86 goto done;
89 /* initialise the sound converter */
90 decoder = NeAACDecOpen();
92 if (!decoder) {
93 LOGF("FAAD: Decode open error\n");
94 err = CODEC_ERROR;
95 goto done;
98 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
99 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
100 NeAACDecSetConfiguration(decoder, conf);
102 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
103 if (err) {
104 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
105 err = CODEC_ERROR;
106 goto done;
109 ci->id3->frequency = s;
111 i = 0;
113 if (file_offset > 0) {
114 if (alac_seek_raw(&demux_res, &input_stream, file_offset,
115 &sound_samples_done, (int*) &i)) {
116 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
117 ci->set_elapsed(elapsed_time);
118 } else {
119 sound_samples_done = 0;
121 } else {
122 sound_samples_done = 0;
125 if (i == 0)
127 lead_trim = ci->id3->lead_trim;
130 /* The main decoding loop */
131 while (i < demux_res.num_sample_byte_sizes) {
132 ci->yield();
134 if (ci->stop_codec || ci->new_track) {
135 break;
138 /* Deal with any pending seek requests */
139 if (ci->seek_time) {
140 if (alac_seek(&demux_res, &input_stream,
141 ((ci->seek_time-1)/10)*(ci->id3->frequency/100),
142 &sound_samples_done, (int*) &i)) {
143 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
144 ci->set_elapsed(elapsed_time);
146 if (i == 0)
148 lead_trim = ci->id3->lead_trim;
151 ci->seek_complete();
154 /* Lookup the length (in samples and bytes) of block i */
155 if (!get_sample_info(&demux_res, i, &sample_duration,
156 &sample_byte_size)) {
157 LOGF("AAC: get_sample_info error\n");
158 err = CODEC_ERROR;
159 goto done;
162 /* There can be gaps between chunks, so skip ahead if needed. It
163 * doesn't seem to happen much, but it probably means that a
164 * "proper" file can have chunks out of order. Why one would want
165 * that an good question (but files with gaps do exist, so who
166 * knows?), so we don't support that - for now, at least.
168 file_offset = get_sample_offset(&demux_res, i);
170 if (file_offset > ci->curpos)
172 ci->advance_buffer(file_offset - ci->curpos);
174 else if (file_offset == 0)
176 LOGF("AAC: get_sample_offset error\n");
177 err = CODEC_ERROR;
178 goto done;
181 /* Request the required number of bytes from the input buffer */
182 buffer=ci->request_buffer(&n,sample_byte_size);
184 /* Decode one block - returned samples will be host-endian */
185 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
187 /* NeAACDecDecode may sometimes return NULL without setting error. */
188 if (ret == NULL || frame_info.error > 0) {
189 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
190 err = CODEC_ERROR;
191 goto done;
194 /* Advance codec buffer (no need to call set_offset because of this) */
195 ci->advance_buffer(n);
197 /* Output the audio */
198 ci->yield();
200 framelength = (frame_info.samples >> 1) - lead_trim;
202 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
204 /* Currently limited to at most one frame of tail_trim.
205 * Seems to be enough.
207 if (ci->id3->tail_trim == 0
208 && sample_duration < (frame_info.samples >> 1))
210 /* Subtract lead_trim just in case we decode a file with
211 * only one audio frame with actual data.
213 framelength = sample_duration - lead_trim;
215 else
217 framelength -= ci->id3->tail_trim;
221 if (framelength > 0)
223 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
224 &decoder->time_out[1][lead_trim],
225 framelength);
228 if (lead_trim > 0)
230 /* frame_info.samples can be 0 for the first frame */
231 lead_trim -= (i > 0 || frame_info.samples)
232 ? (frame_info.samples >> 1) : sample_duration;
234 if (lead_trim < 0 || ci->id3->lead_trim == 0)
236 lead_trim = 0;
240 /* Update the elapsed-time indicator */
241 sound_samples_done += sample_duration;
242 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
243 ci->set_elapsed(elapsed_time);
244 i++;
247 err = CODEC_OK;
249 done:
250 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
252 if (ci->request_next_track())
253 goto next_track;
255 exit:
256 return err;