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 static ALIGN real_t gb_time_buffer
[2][GB_BUF_SIZE
] IBSS_ATTR_FAAD_LARGE_IRAM
;
34 static 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);
72 /* Clean and initialize decoder structures */
73 memset(&demux_res
, 0, sizeof(demux_res
));
75 LOGF("FAAD: Codec init error\n");
80 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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 /* if qtmovie_read returns successfully, the stream is up to
91 * the movie data, which can be used directly by the decoder */
92 if (!qtmovie_read(&input_stream
, &demux_res
)) {
93 LOGF("FAAD: File init error\n");
98 /* initialise the sound converter */
99 decoder
= NeAACDecOpen();
102 LOGF("FAAD: Decode open error\n");
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
);
113 LOGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
118 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
119 * be called after NeAACDecOpen(). */
120 /* A buffer of framelength or 2*frameLenght size must be allocated for
121 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
122 * we do not use the IRAM buffer and keep faad's internal allocation (see
124 needed_bufsize
= decoder
->frameLength
;
126 if ((decoder
->sbr_present_flag
== 1) || (decoder
->forceUpSampling
== 1))
131 if (needed_bufsize
<= GB_BUF_SIZE
)
133 decoder
->time_out
[0] = &gb_time_buffer
[0][0];
134 decoder
->time_out
[1] = &gb_time_buffer
[1][0];
136 /* A buffer of with frameLength elements must be allocated for fb_intermed.
137 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
138 * internal allocation (see specrec.c). */
139 needed_bufsize
= decoder
->frameLength
;
140 if (needed_bufsize
<= GB_BUF_SIZE
)
142 decoder
->fb_intermed
[0] = &gb_fb_intermed
[0][0];
143 decoder
->fb_intermed
[1] = &gb_fb_intermed
[1][0];
146 ci
->id3
->frequency
= s
;
150 if (file_offset
> 0) {
151 if (alac_seek_raw(&demux_res
, &input_stream
, file_offset
,
152 &sound_samples_done
, (int*) &i
)) {
153 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
154 ci
->set_elapsed(elapsed_time
);
156 sound_samples_done
= 0;
159 sound_samples_done
= 0;
164 lead_trim
= ci
->id3
->lead_trim
;
167 /* The main decoding loop */
168 while (i
< demux_res
.num_sample_byte_sizes
) {
171 if (ci
->stop_codec
|| ci
->new_track
) {
175 /* Deal with any pending seek requests */
177 if (alac_seek(&demux_res
, &input_stream
,
178 ((ci
->seek_time
-1)/10)*(ci
->id3
->frequency
/100),
179 &sound_samples_done
, (int*) &i
)) {
180 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
181 ci
->set_elapsed(elapsed_time
);
185 lead_trim
= ci
->id3
->lead_trim
;
191 /* Lookup the length (in samples and bytes) of block i */
192 if (!get_sample_info(&demux_res
, i
, &sample_duration
,
193 &sample_byte_size
)) {
194 LOGF("AAC: get_sample_info error\n");
199 /* There can be gaps between chunks, so skip ahead if needed. It
200 * doesn't seem to happen much, but it probably means that a
201 * "proper" file can have chunks out of order. Why one would want
202 * that an good question (but files with gaps do exist, so who
203 * knows?), so we don't support that - for now, at least.
205 file_offset
= get_sample_offset(&demux_res
, i
);
207 if (file_offset
> ci
->curpos
)
209 ci
->advance_buffer(file_offset
- ci
->curpos
);
211 else if (file_offset
== 0)
213 LOGF("AAC: get_sample_offset error\n");
218 /* Request the required number of bytes from the input buffer */
219 buffer
=ci
->request_buffer(&n
,sample_byte_size
);
221 /* Decode one block - returned samples will be host-endian */
222 ret
= NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
224 /* NeAACDecDecode may sometimes return NULL without setting error. */
225 if (ret
== NULL
|| frame_info
.error
> 0) {
226 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
231 /* Advance codec buffer (no need to call set_offset because of this) */
232 ci
->advance_buffer(n
);
234 /* Output the audio */
237 framelength
= (frame_info
.samples
>> 1) - lead_trim
;
239 if (i
== demux_res
.num_sample_byte_sizes
- 1 && framelength
> 0)
241 /* Currently limited to at most one frame of tail_trim.
242 * Seems to be enough.
244 if (ci
->id3
->tail_trim
== 0
245 && sample_duration
< (frame_info
.samples
>> 1))
247 /* Subtract lead_trim just in case we decode a file with
248 * only one audio frame with actual data.
250 framelength
= sample_duration
- lead_trim
;
254 framelength
-= ci
->id3
->tail_trim
;
260 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
261 &decoder
->time_out
[1][lead_trim
],
267 /* frame_info.samples can be 0 for the first frame */
268 lead_trim
-= (i
> 0 || frame_info
.samples
)
269 ? (frame_info
.samples
>> 1) : sample_duration
;
271 if (lead_trim
< 0 || ci
->id3
->lead_trim
== 0)
277 /* Update the elapsed-time indicator */
278 sound_samples_done
+= sample_duration
;
279 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
280 ci
->set_elapsed(elapsed_time
);
287 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done
);
289 if (ci
->request_next_track())