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 real_t gb_time_buffer
[2][GB_BUF_SIZE
] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR
;
34 static real_t gb_fb_intermed
[2][GB_BUF_SIZE
] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR
;
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 demux_res_t demux_res
;
47 stream_t input_stream
;
48 uint32_t sound_samples_done
;
49 uint32_t elapsed_time
;
55 unsigned char* buffer
;
56 NeAACDecFrameInfo frame_info
;
57 NeAACDecHandle decoder
;
64 /* Generic codec initialisation */
65 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
66 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
71 /* Clean and initialize decoder structures */
72 memset(&demux_res
, 0, sizeof(demux_res
));
74 LOGF("FAAD: Codec init error\n");
79 if (codec_wait_taginfo() != 0)
82 file_offset
= ci
->id3
->offset
;
84 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
85 codec_set_replaygain(ci
->id3
);
87 stream_create(&input_stream
,ci
);
89 /* if qtmovie_read returns successfully, the stream is up to
90 * the movie data, which can be used directly by the decoder */
91 if (!qtmovie_read(&input_stream
, &demux_res
)) {
92 LOGF("FAAD: File init error\n");
97 /* initialise the sound converter */
98 decoder
= NeAACDecOpen();
101 LOGF("FAAD: Decode open error\n");
106 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
107 conf
->outputFormat
= FAAD_FMT_24BIT
; /* irrelevant, we don't convert */
108 NeAACDecSetConfiguration(decoder
, conf
);
110 err
= NeAACDecInit2(decoder
, demux_res
.codecdata
, demux_res
.codecdata_len
, &s
, &c
);
112 LOGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
117 /* Set pointer to be able to use IRAM an to avoid alloc in decoder. Must
118 * be called after NeAACDecOpen(). */
119 /* A buffer of framelength or 2*frameLenght size must be allocated for
120 * time_out. If frameLength is too big or SBR/forceUpSampling is active,
121 * we do not use the IRAM buffer and keep faad's internal allocation (see
123 needed_bufsize
= decoder
->frameLength
;
125 if ((decoder
->sbr_present_flag
== 1) || (decoder
->forceUpSampling
== 1))
130 if (needed_bufsize
<= GB_BUF_SIZE
)
132 decoder
->time_out
[0] = &gb_time_buffer
[0][0];
133 decoder
->time_out
[1] = &gb_time_buffer
[1][0];
135 /* A buffer of with frameLength elements must be allocated for fb_intermed.
136 * If frameLength is too big, we do not use the IRAM buffer and keep faad's
137 * internal allocation (see specrec.c). */
138 needed_bufsize
= decoder
->frameLength
;
139 if (needed_bufsize
<= GB_BUF_SIZE
)
141 decoder
->fb_intermed
[0] = &gb_fb_intermed
[0][0];
142 decoder
->fb_intermed
[1] = &gb_fb_intermed
[1][0];
146 /* Check for need of special handling for seek/resume and elapsed time. */
147 if (ci
->id3
->needs_upsampling_correction
) {
154 ci
->id3
->frequency
= s
;
158 if (file_offset
> 0) {
159 /* Resume the desired (byte) position. Important: When resuming SBR
160 * upsampling files the resulting sound_samples_done must be expanded
161 * by a factor of 2. This is done via using sbr_fac. */
162 if (alac_seek_raw(&demux_res
, &input_stream
, file_offset
,
163 &sound_samples_done
, (int*) &i
)) {
164 sound_samples_done
*= sbr_fac
;
165 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
166 ci
->set_elapsed(elapsed_time
);
168 sound_samples_done
= 0;
171 sound_samples_done
= 0;
176 lead_trim
= ci
->id3
->lead_trim
;
179 /* The main decoding loop */
180 while (i
< demux_res
.num_sample_byte_sizes
) {
183 if (ci
->stop_codec
|| ci
->new_track
) {
187 /* Deal with any pending seek requests */
189 /* Seek to the desired time position. Important: When seeking in SBR
190 * upsampling files the seek_time must be divided by 2 when calling
191 * alac_seek and the resulting sound_samples_done must be expanded
192 * by a factor 2. This is done via using sbr_fac. */
193 if (alac_seek(&demux_res
, &input_stream
,
194 ((ci
->seek_time
-1)/10/sbr_fac
)*(ci
->id3
->frequency
/100),
195 &sound_samples_done
, (int*) &i
)) {
196 sound_samples_done
*= sbr_fac
;
197 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
198 ci
->set_elapsed(elapsed_time
);
202 lead_trim
= ci
->id3
->lead_trim
;
208 /* There can be gaps between chunks, so skip ahead if needed. It
209 * doesn't seem to happen much, but it probably means that a
210 * "proper" file can have chunks out of order. Why one would want
211 * that an good question (but files with gaps do exist, so who
212 * knows?), so we don't support that - for now, at least.
214 file_offset
= get_sample_offset(&demux_res
, i
);
216 if (file_offset
> ci
->curpos
)
218 ci
->advance_buffer(file_offset
- ci
->curpos
);
220 else if (file_offset
== 0)
222 LOGF("AAC: get_sample_offset error\n");
227 /* Request the required number of bytes from the input buffer */
228 buffer
=ci
->request_buffer(&n
, demux_res
.sample_byte_size
[i
]);
230 /* Decode one block - returned samples will be host-endian */
231 ret
= NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
233 /* NeAACDecDecode may sometimes return NULL without setting error. */
234 if (ret
== NULL
|| frame_info
.error
> 0) {
235 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
240 /* Advance codec buffer (no need to call set_offset because of this) */
241 ci
->advance_buffer(frame_info
.bytesconsumed
);
243 /* Output the audio */
246 /* Gather number of samples for the decoded frame. */
247 framelength
= (frame_info
.samples
>> 1) - lead_trim
;
249 if (i
== demux_res
.num_sample_byte_sizes
- 1 && framelength
> 0)
251 framelength
-= ci
->id3
->tail_trim
;
256 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
257 &decoder
->time_out
[1][lead_trim
],
263 /* frame_info.samples can be 0 for the first frame */
264 lead_trim
-= (i
> 0 || frame_info
.samples
)
265 ? (frame_info
.samples
>> 1) : (uint32_t)framelength
;
267 if (lead_trim
< 0 || ci
->id3
->lead_trim
== 0)
273 /* Update the elapsed-time indicator */
274 sound_samples_done
+= framelength
;
275 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
276 ci
->set_elapsed(elapsed_time
);
281 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done
);
283 if (ci
->request_next_track())