Improved FPS test plugin: * Better precision for low frame rates (take extra ticks...
[Rockbox.git] / apps / codecs / aac.c
blobd4f051c09c9e847c4b00eff5159c1a2e4189aa41
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include "libm4a/m4a.h"
22 #include "libfaad/common.h"
23 #include "libfaad/structs.h"
24 #include "libfaad/decoder.h"
26 CODEC_HEADER
28 /* this is the codec entry point */
29 enum codec_status codec_main(void)
31 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
32 * a bit confusing. Files with sound are split up in chunks, where
33 * each chunk contains one or more samples. Each sample in turn
34 * contains a number of "sound samples" (the kind you refer to with
35 * the sampling frequency).
37 size_t n;
38 static demux_res_t demux_res;
39 stream_t input_stream;
40 uint32_t sound_samples_done;
41 uint32_t elapsed_time;
42 uint32_t sample_duration;
43 uint32_t sample_byte_size;
44 int file_offset;
45 int framelength;
46 int lead_trim = 0;
47 unsigned int i;
48 unsigned char* buffer;
49 static NeAACDecFrameInfo frame_info;
50 NeAACDecHandle decoder;
51 int err;
52 uint32_t s = 0;
53 unsigned char c = 0;
55 /* Generic codec initialisation */
56 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*16);
57 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
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 sound_samples_done = 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 (sound_samples_done > 0) {
114 if (alac_seek_raw(&demux_res, &input_stream, sound_samples_done,
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;
123 if (i == 0)
125 lead_trim = ci->id3->lead_trim;
128 /* The main decoding loop */
129 while (i < demux_res.num_sample_byte_sizes) {
130 ci->yield();
132 if (ci->stop_codec || ci->new_track) {
133 break;
136 /* Deal with any pending seek requests */
137 if (ci->seek_time) {
138 if (alac_seek(&demux_res, &input_stream,
139 ((ci->seek_time-1)/10)*(ci->id3->frequency/100),
140 &sound_samples_done, (int*) &i)) {
141 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
142 ci->set_elapsed(elapsed_time);
144 if (i == 0)
146 lead_trim = ci->id3->lead_trim;
149 ci->seek_complete();
152 /* Lookup the length (in samples and bytes) of block i */
153 if (!get_sample_info(&demux_res, i, &sample_duration,
154 &sample_byte_size)) {
155 LOGF("AAC: get_sample_info error\n");
156 err = CODEC_ERROR;
157 goto done;
160 /* There can be gaps between chunks, so skip ahead if needed. It
161 * doesn't seem to happen much, but it probably means that a
162 * "proper" file can have chunks out of order. Why one would want
163 * that an good question (but files with gaps do exist, so who
164 * knows?), so we don't support that - for now, at least.
166 file_offset = get_sample_offset(&demux_res, i);
168 if (file_offset > ci->curpos)
170 ci->advance_buffer(file_offset - ci->curpos);
172 else if (file_offset == 0)
174 LOGF("AAC: get_sample_offset error\n");
175 err = CODEC_ERROR;
176 goto done;
179 /* Request the required number of bytes from the input buffer */
180 buffer=ci->request_buffer(&n,sample_byte_size);
182 /* Decode one block - returned samples will be host-endian */
183 NeAACDecDecode(decoder, &frame_info, buffer, n);
184 /* Ignore return value, we access samples in the decoder struct
185 * directly.
187 if (frame_info.error > 0) {
188 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
189 err = CODEC_ERROR;
190 goto done;
193 /* Advance codec buffer */
194 ci->advance_buffer(n);
196 /* Output the audio */
197 ci->yield();
199 framelength = (frame_info.samples >> 1) - lead_trim;
201 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
203 /* Currently limited to at most one frame of tail_trim.
204 * Seems to be enough.
206 if (ci->id3->tail_trim == 0
207 && sample_duration < (frame_info.samples >> 1))
209 /* Subtract lead_trim just in case we decode a file with
210 * only one audio frame with actual data.
212 framelength = sample_duration - lead_trim;
214 else
216 framelength -= ci->id3->tail_trim;
220 if (framelength > 0)
222 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
223 &decoder->time_out[1][lead_trim],
224 framelength);
227 if (lead_trim > 0)
229 /* frame_info.samples can be 0 for the first frame */
230 lead_trim -= (i > 0 || frame_info.samples)
231 ? (frame_info.samples >> 1) : sample_duration;
233 if (lead_trim < 0 || ci->id3->lead_trim == 0)
235 lead_trim = 0;
239 /* Update the elapsed-time indicator */
240 sound_samples_done += sample_duration;
241 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
242 ci->set_elapsed(elapsed_time);
244 /* Keep track of current position - for resuming */
245 ci->set_offset(elapsed_time);
247 i++;
250 err = CODEC_OK;
252 done:
253 LOGF("AAC: Decoded %lu samples\n", sound_samples_done);
255 if (ci->request_next_track())
256 goto next_track;
258 exit:
259 return err;