1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "libm4a/m4a.h"
24 #include "libfaad/common.h"
25 #include "libfaad/structs.h"
26 #include "libfaad/decoder.h"
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 ALIGN real_t gb_time_buffer
[2][GB_BUF_SIZE
] IBSS_ATTR_FAAD_LARGE_IRAM
;
34 ALIGN real_t gb_fb_intermed
[2][GB_BUF_SIZE
] IBSS_ATTR_FAAD_LARGE_IRAM
;
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).
46 static 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
;
57 unsigned char* buffer
;
58 static NeAACDecFrameInfo frame_info
;
59 NeAACDecHandle decoder
;
65 /* Generic codec initialisation */
66 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
67 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
73 LOGF("FAAD: Codec init error\n");
78 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
81 file_offset
= ci
->id3
->offset
;
83 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
84 codec_set_replaygain(ci
->id3
);
86 stream_create(&input_stream
,ci
);
88 /* if qtmovie_read returns successfully, the stream is up to
89 * the movie data, which can be used directly by the decoder */
90 if (!qtmovie_read(&input_stream
, &demux_res
)) {
91 LOGF("FAAD: File init error\n");
96 /* initialise the sound converter */
97 decoder
= NeAACDecOpen();
100 LOGF("FAAD: Decode open error\n");
105 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
106 conf
->outputFormat
= FAAD_FMT_24BIT
; /* irrelevant, we don't convert */
107 NeAACDecSetConfiguration(decoder
, conf
);
109 err
= NeAACDecInit2(decoder
, demux_res
.codecdata
, demux_res
.codecdata_len
, &s
, &c
);
111 LOGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
116 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
117 * be called after NeAACDecOpen(). */
118 /* A buffer of framelength or 2*frameLenght size must be allocated for
119 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
120 * we do not use the IRAM buffer and keep faad's internal allocation (see
122 needed_bufsize
= decoder
->frameLength
;
124 if ((decoder
->sbr_present_flag
== 1) || (decoder
->forceUpSampling
== 1))
129 if (needed_bufsize
<= GB_BUF_SIZE
)
131 decoder
->time_out
[0] = &gb_time_buffer
[0][0];
132 decoder
->time_out
[1] = &gb_time_buffer
[1][0];
134 /* A buffer of with frameLength elements must be allocated for fb_intermed.
135 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
136 * internal allocation (see specrec.c). */
137 needed_bufsize
= decoder
->frameLength
;
138 if (needed_bufsize
<= GB_BUF_SIZE
)
140 decoder
->fb_intermed
[0] = &gb_fb_intermed
[0][0];
141 decoder
->fb_intermed
[1] = &gb_fb_intermed
[1][0];
144 ci
->id3
->frequency
= s
;
148 if (file_offset
> 0) {
149 if (alac_seek_raw(&demux_res
, &input_stream
, file_offset
,
150 &sound_samples_done
, (int*) &i
)) {
151 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
152 ci
->set_elapsed(elapsed_time
);
154 sound_samples_done
= 0;
157 sound_samples_done
= 0;
162 lead_trim
= ci
->id3
->lead_trim
;
165 /* The main decoding loop */
166 while (i
< demux_res
.num_sample_byte_sizes
) {
169 if (ci
->stop_codec
|| ci
->new_track
) {
173 /* Deal with any pending seek requests */
175 if (alac_seek(&demux_res
, &input_stream
,
176 ((ci
->seek_time
-1)/10)*(ci
->id3
->frequency
/100),
177 &sound_samples_done
, (int*) &i
)) {
178 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
179 ci
->set_elapsed(elapsed_time
);
183 lead_trim
= ci
->id3
->lead_trim
;
189 /* Lookup the length (in samples and bytes) of block i */
190 if (!get_sample_info(&demux_res
, i
, &sample_duration
,
191 &sample_byte_size
)) {
192 LOGF("AAC: get_sample_info error\n");
197 /* There can be gaps between chunks, so skip ahead if needed. It
198 * doesn't seem to happen much, but it probably means that a
199 * "proper" file can have chunks out of order. Why one would want
200 * that an good question (but files with gaps do exist, so who
201 * knows?), so we don't support that - for now, at least.
203 file_offset
= get_sample_offset(&demux_res
, i
);
205 if (file_offset
> ci
->curpos
)
207 ci
->advance_buffer(file_offset
- ci
->curpos
);
209 else if (file_offset
== 0)
211 LOGF("AAC: get_sample_offset error\n");
216 /* Request the required number of bytes from the input buffer */
217 buffer
=ci
->request_buffer(&n
,sample_byte_size
);
219 /* Decode one block - returned samples will be host-endian */
220 ret
= NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
222 /* NeAACDecDecode may sometimes return NULL without setting error. */
223 if (ret
== NULL
|| frame_info
.error
> 0) {
224 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
229 /* Advance codec buffer (no need to call set_offset because of this) */
230 ci
->advance_buffer(n
);
232 /* Output the audio */
235 framelength
= (frame_info
.samples
>> 1) - lead_trim
;
237 if (i
== demux_res
.num_sample_byte_sizes
- 1 && framelength
> 0)
239 /* Currently limited to at most one frame of tail_trim.
240 * Seems to be enough.
242 if (ci
->id3
->tail_trim
== 0
243 && sample_duration
< (frame_info
.samples
>> 1))
245 /* Subtract lead_trim just in case we decode a file with
246 * only one audio frame with actual data.
248 framelength
= sample_duration
- lead_trim
;
252 framelength
-= ci
->id3
->tail_trim
;
258 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
259 &decoder
->time_out
[1][lead_trim
],
265 /* frame_info.samples can be 0 for the first frame */
266 lead_trim
-= (i
> 0 || frame_info
.samples
)
267 ? (frame_info
.samples
>> 1) : sample_duration
;
269 if (lead_trim
< 0 || ci
->id3
->lead_trim
== 0)
275 /* Update the elapsed-time indicator */
276 sound_samples_done
+= sample_duration
;
277 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
278 ci
->set_elapsed(elapsed_time
);
285 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done
);
287 if (ci
->request_next_track())