Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / aac.c
blobcd81b5a58421ebcc86e0c41a01ddb1c9ce351210
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 i;
65 unsigned char* buffer;
66 NeAACDecFrameInfo frame_info;
67 NeAACDecHandle decoder;
68 int err;
69 uint32_t seek_idx = 0;
70 uint32_t s = 0;
71 uint32_t sbr_fac = 1;
72 unsigned char c = 0;
73 void *ret;
74 intptr_t param;
76 /* Clean and initialize decoder structures */
77 memset(&demux_res , 0, sizeof(demux_res));
78 if (codec_init()) {
79 LOGF("FAAD: Codec init error\n");
80 return CODEC_ERROR;
83 file_offset = ci->id3->offset;
85 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
86 codec_set_replaygain(ci->id3);
88 stream_create(&input_stream,ci);
90 ci->seek_buffer(ci->id3->first_frame_offset);
92 /* if qtmovie_read returns successfully, the stream is up to
93 * the movie data, which can be used directly by the decoder */
94 if (!qtmovie_read(&input_stream, &demux_res)) {
95 LOGF("FAAD: File init error\n");
96 return CODEC_ERROR;
99 /* initialise the sound converter */
100 decoder = NeAACDecOpen();
102 if (!decoder) {
103 LOGF("FAAD: Decode open error\n");
104 return CODEC_ERROR;
107 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
108 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
109 NeAACDecSetConfiguration(decoder, conf);
111 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
112 if (err) {
113 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
114 return CODEC_ERROR;
117 #ifdef SBR_DEC
118 /* Check for need of special handling for seek/resume and elapsed time. */
119 if (ci->id3->needs_upsampling_correction) {
120 sbr_fac = 2;
121 } else {
122 sbr_fac = 1;
124 #endif
126 i = 0;
128 if (file_offset > 0) {
129 /* Resume the desired (byte) position. Important: When resuming SBR
130 * upsampling files the resulting sound_samples_done must be expanded
131 * by a factor of 2. This is done via using sbr_fac. */
132 if (m4a_seek_raw(&demux_res, &input_stream, file_offset,
133 &sound_samples_done, (int*) &i)) {
134 sound_samples_done *= sbr_fac;
135 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
136 ci->set_elapsed(elapsed_time);
137 } else {
138 sound_samples_done = 0;
140 NeAACDecPostSeekReset(decoder, i);
141 } else {
142 sound_samples_done = 0;
145 if (i == 0)
147 lead_trim = ci->id3->lead_trim;
150 /* The main decoding loop */
151 while (i < demux_res.num_sample_byte_sizes) {
152 enum codec_command_action action = ci->get_command(&param);
154 if (action == CODEC_ACTION_HALT)
155 break;
157 /* Deal with any pending seek requests */
158 if (action == CODEC_ACTION_SEEK_TIME) {
159 /* Seek to the desired time position. Important: When seeking in SBR
160 * upsampling files the seek_time must be divided by 2 when calling
161 * m4a_seek and the resulting sound_samples_done must be expanded
162 * by a factor 2. This is done via using sbr_fac. */
163 if (m4a_seek(&demux_res, &input_stream,
164 (param/10/sbr_fac)*(ci->id3->frequency/100),
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 seek_idx = 0;
171 if (i == 0)
173 lead_trim = ci->id3->lead_trim;
176 NeAACDecPostSeekReset(decoder, i);
177 ci->seek_complete();
180 /* There can be gaps between chunks, so skip ahead if needed. It
181 * doesn't seem to happen much, but it probably means that a
182 * "proper" file can have chunks out of order. Why one would want
183 * that an good question (but files with gaps do exist, so who
184 * knows?), so we don't support that - for now, at least.
186 file_offset = m4a_check_sample_offset(&demux_res, i, &seek_idx);
188 if (file_offset > ci->curpos)
190 ci->advance_buffer(file_offset - ci->curpos);
192 else if (file_offset == 0)
194 LOGF("AAC: get_sample_offset error\n");
195 return CODEC_ERROR;
198 /* Request the required number of bytes from the input buffer */
199 buffer=ci->request_buffer(&n, FAAD_BYTE_BUFFER_SIZE);
201 /* Decode one block - returned samples will be host-endian */
202 ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
204 /* NeAACDecDecode may sometimes return NULL without setting error. */
205 if (ret == NULL || frame_info.error > 0) {
206 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
207 return CODEC_ERROR;
210 /* Advance codec buffer (no need to call set_offset because of this) */
211 ci->advance_buffer(frame_info.bytesconsumed);
213 /* Output the audio */
214 ci->yield();
216 /* Gather number of samples for the decoded frame. */
217 framelength = (frame_info.samples >> 1) - lead_trim;
219 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
221 framelength -= ci->id3->tail_trim;
224 if (framelength > 0)
226 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
227 &decoder->time_out[1][lead_trim],
228 framelength);
231 if (lead_trim > 0)
233 /* frame_info.samples can be 0 for the first frame */
234 lead_trim -= (i > 0 || frame_info.samples)
235 ? (frame_info.samples >> 1) : (uint32_t)framelength;
237 if (lead_trim < 0 || ci->id3->lead_trim == 0)
239 lead_trim = 0;
243 /* Update the elapsed-time indicator */
244 sound_samples_done += framelength;
245 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
246 ci->set_elapsed(elapsed_time);
247 i++;
250 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
251 return CODEC_OK;