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 /* 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
33 #define FAAD_BYTE_BUFFER_SIZE (2048-12)
35 /* this is the codec entry point */
36 enum codec_status
codec_main(void)
38 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
39 * a bit confusing. Files with sound are split up in chunks, where
40 * each chunk contains one or more samples. Each sample in turn
41 * contains a number of "sound samples" (the kind you refer to with
42 * the sampling frequency).
45 demux_res_t demux_res
;
46 stream_t input_stream
;
47 uint32_t sound_samples_done
;
48 uint32_t elapsed_time
;
53 unsigned char* buffer
;
54 NeAACDecFrameInfo frame_info
;
55 NeAACDecHandle decoder
;
57 uint32_t seek_idx
= 0;
63 /* Generic codec initialisation */
64 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
65 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
70 /* Clean and initialize decoder structures */
71 memset(&demux_res
, 0, sizeof(demux_res
));
73 LOGF("FAAD: Codec init error\n");
78 if (codec_wait_taginfo() != 0)
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
);
117 /* Check for need of special handling for seek/resume and elapsed time. */
118 if (ci
->id3
->needs_upsampling_correction
) {
125 ci
->id3
->frequency
= s
;
129 if (file_offset
> 0) {
130 /* Resume the desired (byte) position. Important: When resuming SBR
131 * upsampling files the resulting sound_samples_done must be expanded
132 * by a factor of 2. This is done via using sbr_fac. */
133 if (m4a_seek_raw(&demux_res
, &input_stream
, file_offset
,
134 &sound_samples_done
, (int*) &i
)) {
135 sound_samples_done
*= sbr_fac
;
136 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
137 ci
->set_elapsed(elapsed_time
);
139 sound_samples_done
= 0;
141 NeAACDecPostSeekReset(decoder
, i
);
143 sound_samples_done
= 0;
148 lead_trim
= ci
->id3
->lead_trim
;
151 /* The main decoding loop */
152 while (i
< demux_res
.num_sample_byte_sizes
) {
155 if (ci
->stop_codec
|| ci
->new_track
) {
159 /* Deal with any pending seek requests */
161 /* Seek to the desired time position. Important: When seeking in SBR
162 * upsampling files the seek_time must be divided by 2 when calling
163 * m4a_seek and the resulting sound_samples_done must be expanded
164 * by a factor 2. This is done via using sbr_fac. */
165 if (m4a_seek(&demux_res
, &input_stream
,
166 ((ci
->seek_time
-1)/10/sbr_fac
)*(ci
->id3
->frequency
/100),
167 &sound_samples_done
, (int*) &i
)) {
168 sound_samples_done
*= sbr_fac
;
169 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
170 ci
->set_elapsed(elapsed_time
);
175 lead_trim
= ci
->id3
->lead_trim
;
178 NeAACDecPostSeekReset(decoder
, i
);
182 /* There can be gaps between chunks, so skip ahead if needed. It
183 * doesn't seem to happen much, but it probably means that a
184 * "proper" file can have chunks out of order. Why one would want
185 * that an good question (but files with gaps do exist, so who
186 * knows?), so we don't support that - for now, at least.
188 file_offset
= m4a_check_sample_offset(&demux_res
, i
, &seek_idx
);
190 if (file_offset
> ci
->curpos
)
192 ci
->advance_buffer(file_offset
- ci
->curpos
);
194 else if (file_offset
== 0)
196 LOGF("AAC: get_sample_offset error\n");
201 /* Request the required number of bytes from the input buffer */
202 buffer
=ci
->request_buffer(&n
, FAAD_BYTE_BUFFER_SIZE
);
204 /* Decode one block - returned samples will be host-endian */
205 ret
= NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
207 /* NeAACDecDecode may sometimes return NULL without setting error. */
208 if (ret
== NULL
|| frame_info
.error
> 0) {
209 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
214 /* Advance codec buffer (no need to call set_offset because of this) */
215 ci
->advance_buffer(frame_info
.bytesconsumed
);
217 /* Output the audio */
220 /* Gather number of samples for the decoded frame. */
221 framelength
= (frame_info
.samples
>> 1) - lead_trim
;
223 if (i
== demux_res
.num_sample_byte_sizes
- 1 && framelength
> 0)
225 framelength
-= ci
->id3
->tail_trim
;
230 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
231 &decoder
->time_out
[1][lead_trim
],
237 /* frame_info.samples can be 0 for the first frame */
238 lead_trim
-= (i
> 0 || frame_info
.samples
)
239 ? (frame_info
.samples
>> 1) : (uint32_t)framelength
;
241 if (lead_trim
< 0 || ci
->id3
->lead_trim
== 0)
247 /* Update the elapsed-time indicator */
248 sound_samples_done
+= framelength
;
249 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
250 ci
->set_elapsed(elapsed_time
);
255 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done
);
257 if (ci
->request_next_track())