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 /* this is the codec entry point */
31 enum codec_status
codec_main(void)
33 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
34 * a bit confusing. Files with sound are split up in chunks, where
35 * each chunk contains one or more samples. Each sample in turn
36 * contains a number of "sound samples" (the kind you refer to with
37 * the sampling frequency).
40 static demux_res_t demux_res
;
41 stream_t input_stream
;
42 uint32_t sound_samples_done
;
43 uint32_t elapsed_time
;
44 uint32_t sample_duration
;
45 uint32_t sample_byte_size
;
50 unsigned char* buffer
;
51 static NeAACDecFrameInfo frame_info
;
52 NeAACDecHandle decoder
;
57 /* Generic codec initialisation */
58 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
59 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
65 LOGF("FAAD: Codec init error\n");
70 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
73 sound_samples_done
= ci
->id3
->offset
;
75 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
76 codec_set_replaygain(ci
->id3
);
78 stream_create(&input_stream
,ci
);
80 /* if qtmovie_read returns successfully, the stream is up to
81 * the movie data, which can be used directly by the decoder */
82 if (!qtmovie_read(&input_stream
, &demux_res
)) {
83 LOGF("FAAD: File init error\n");
88 /* initialise the sound converter */
89 decoder
= NeAACDecOpen();
92 LOGF("FAAD: Decode open error\n");
97 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
98 conf
->outputFormat
= FAAD_FMT_24BIT
; /* irrelevant, we don't convert */
99 NeAACDecSetConfiguration(decoder
, conf
);
101 err
= NeAACDecInit2(decoder
, demux_res
.codecdata
, demux_res
.codecdata_len
, &s
, &c
);
103 LOGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
108 ci
->id3
->frequency
= s
;
112 if (sound_samples_done
> 0) {
113 if (alac_seek_raw(&demux_res
, &input_stream
, sound_samples_done
,
114 &sound_samples_done
, (int*) &i
)) {
115 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
116 ci
->set_elapsed(elapsed_time
);
118 sound_samples_done
= 0;
124 lead_trim
= ci
->id3
->lead_trim
;
127 /* The main decoding loop */
128 while (i
< demux_res
.num_sample_byte_sizes
) {
131 if (ci
->stop_codec
|| ci
->new_track
) {
135 /* Deal with any pending seek requests */
137 if (alac_seek(&demux_res
, &input_stream
,
138 ((ci
->seek_time
-1)/10)*(ci
->id3
->frequency
/100),
139 &sound_samples_done
, (int*) &i
)) {
140 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
141 ci
->set_elapsed(elapsed_time
);
145 lead_trim
= ci
->id3
->lead_trim
;
151 /* Lookup the length (in samples and bytes) of block i */
152 if (!get_sample_info(&demux_res
, i
, &sample_duration
,
153 &sample_byte_size
)) {
154 LOGF("AAC: get_sample_info error\n");
159 /* There can be gaps between chunks, so skip ahead if needed. It
160 * doesn't seem to happen much, but it probably means that a
161 * "proper" file can have chunks out of order. Why one would want
162 * that an good question (but files with gaps do exist, so who
163 * knows?), so we don't support that - for now, at least.
165 file_offset
= get_sample_offset(&demux_res
, i
);
167 if (file_offset
> ci
->curpos
)
169 ci
->advance_buffer(file_offset
- ci
->curpos
);
171 else if (file_offset
== 0)
173 LOGF("AAC: get_sample_offset error\n");
178 /* Request the required number of bytes from the input buffer */
179 buffer
=ci
->request_buffer(&n
,sample_byte_size
);
181 /* Decode one block - returned samples will be host-endian */
182 NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
183 /* Ignore return value, we access samples in the decoder struct
186 if (frame_info
.error
> 0) {
187 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
192 /* Advance codec buffer */
193 ci
->advance_buffer(n
);
195 /* Output the audio */
198 framelength
= (frame_info
.samples
>> 1) - lead_trim
;
200 if (i
== demux_res
.num_sample_byte_sizes
- 1 && framelength
> 0)
202 /* Currently limited to at most one frame of tail_trim.
203 * Seems to be enough.
205 if (ci
->id3
->tail_trim
== 0
206 && sample_duration
< (frame_info
.samples
>> 1))
208 /* Subtract lead_trim just in case we decode a file with
209 * only one audio frame with actual data.
211 framelength
= sample_duration
- lead_trim
;
215 framelength
-= ci
->id3
->tail_trim
;
221 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
222 &decoder
->time_out
[1][lead_trim
],
228 /* frame_info.samples can be 0 for the first frame */
229 lead_trim
-= (i
> 0 || frame_info
.samples
)
230 ? (frame_info
.samples
>> 1) : sample_duration
;
232 if (lead_trim
< 0 || ci
->id3
->lead_trim
== 0)
238 /* Update the elapsed-time indicator */
239 sound_samples_done
+= sample_duration
;
240 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
241 ci
->set_elapsed(elapsed_time
);
243 /* Keep track of current position - for resuming */
244 ci
->set_offset(elapsed_time
);
252 LOGF("AAC: Decoded %lu samples\n", sound_samples_done
);
254 if (ci
->request_next_track())