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(enum codec_entry_call_reason reason
)
38 if (reason
== CODEC_LOAD
) {
39 /* Generic codec initialisation */
40 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
41 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 29);
47 /* this is called for each file to process */
48 enum codec_status
codec_run(void)
50 /* Note that when dealing with QuickTime/MPEG4 files, terminology is
51 * a bit confusing. Files with sound are split up in chunks, where
52 * each chunk contains one or more samples. Each sample in turn
53 * contains a number of "sound samples" (the kind you refer to with
54 * the sampling frequency).
57 demux_res_t demux_res
;
58 stream_t input_stream
;
59 uint32_t sound_samples_done
;
60 uint32_t elapsed_time
;
64 unsigned int frame_samples
;
66 unsigned char* buffer
;
67 NeAACDecFrameInfo frame_info
;
68 NeAACDecHandle decoder
;
70 uint32_t seek_idx
= 0;
76 bool empty_first_frame
= false;
78 /* Clean and initialize decoder structures */
79 memset(&demux_res
, 0, sizeof(demux_res
));
81 LOGF("FAAD: Codec init error\n");
85 file_offset
= ci
->id3
->offset
;
87 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
88 codec_set_replaygain(ci
->id3
);
90 stream_create(&input_stream
,ci
);
92 ci
->seek_buffer(ci
->id3
->first_frame_offset
);
94 /* if qtmovie_read returns successfully, the stream is up to
95 * the movie data, which can be used directly by the decoder */
96 if (!qtmovie_read(&input_stream
, &demux_res
)) {
97 LOGF("FAAD: File init error\n");
101 /* initialise the sound converter */
102 decoder
= NeAACDecOpen();
105 LOGF("FAAD: Decode open error\n");
109 NeAACDecConfigurationPtr conf
= NeAACDecGetCurrentConfiguration(decoder
);
110 conf
->outputFormat
= FAAD_FMT_24BIT
; /* irrelevant, we don't convert */
111 NeAACDecSetConfiguration(decoder
, conf
);
113 err
= NeAACDecInit2(decoder
, demux_res
.codecdata
, demux_res
.codecdata_len
, &s
, &c
);
115 LOGF("FAAD: DecInit: %d, %d\n", err
, decoder
->object_type
);
120 /* Check for need of special handling for seek/resume and elapsed time. */
121 if (ci
->id3
->needs_upsampling_correction
) {
130 if (file_offset
> 0) {
131 /* Resume the desired (byte) position. Important: When resuming SBR
132 * upsampling files the resulting sound_samples_done must be expanded
133 * by a factor of 2. This is done via using sbr_fac. */
134 if (m4a_seek_raw(&demux_res
, &input_stream
, file_offset
,
135 &sound_samples_done
, (int*) &i
)) {
136 sound_samples_done
*= sbr_fac
;
137 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
138 ci
->set_elapsed(elapsed_time
);
140 sound_samples_done
= 0;
142 NeAACDecPostSeekReset(decoder
, i
);
144 sound_samples_done
= 0;
149 lead_trim
= ci
->id3
->lead_trim
;
152 /* The main decoding loop */
153 while (i
< demux_res
.num_sample_byte_sizes
) {
154 enum codec_command_action action
= ci
->get_command(¶m
);
156 if (action
== CODEC_ACTION_HALT
)
159 /* Deal with any pending seek requests */
160 if (action
== CODEC_ACTION_SEEK_TIME
) {
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 (param
/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");
200 /* Request the required number of bytes from the input buffer */
201 buffer
=ci
->request_buffer(&n
, FAAD_BYTE_BUFFER_SIZE
);
203 /* Decode one block - returned samples will be host-endian */
204 ret
= NeAACDecDecode(decoder
, &frame_info
, buffer
, n
);
206 /* NeAACDecDecode may sometimes return NULL without setting error. */
207 if (ret
== NULL
|| frame_info
.error
> 0) {
208 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info
.error
));
212 /* Advance codec buffer (no need to call set_offset because of this) */
213 ci
->advance_buffer(frame_info
.bytesconsumed
);
215 /* Output the audio */
218 frame_samples
= frame_info
.samples
>> 1;
220 if (empty_first_frame
)
222 /* Remove the first frame from lead_trim, under the assumption
223 * that it had the same size as this frame
225 empty_first_frame
= false;
226 lead_trim
-= frame_samples
;
234 /* Gather number of samples for the decoded frame. */
235 framelength
= frame_samples
- lead_trim
;
237 if (i
== demux_res
.num_sample_byte_sizes
- 1)
239 // Size of the last frame
240 const uint32_t sample_duration
= (demux_res
.num_time_to_samples
> 0) ?
241 demux_res
.time_to_sample
[demux_res
.num_time_to_samples
- 1].sample_duration
:
244 /* Currently limited to at most one frame of tail_trim.
245 * Seems to be enough.
247 if (ci
->id3
->tail_trim
== 0 && sample_duration
< frame_samples
)
249 /* Subtract lead_trim just in case we decode a file with only
250 * one audio frame with actual data (lead_trim is usually zero
253 framelength
= sample_duration
- lead_trim
;
257 framelength
-= ci
->id3
->tail_trim
;
263 ci
->pcmbuf_insert(&decoder
->time_out
[0][lead_trim
],
264 &decoder
->time_out
[1][lead_trim
],
266 sound_samples_done
+= framelength
;
267 /* Update the elapsed-time indicator */
268 elapsed_time
= (sound_samples_done
* 10) / (ci
->id3
->frequency
/ 100);
269 ci
->set_elapsed(elapsed_time
);
274 /* frame_info.samples can be 0 for frame 0. We still want to
275 * remove it from lead_trim, so do that during frame 1.
277 if (0 == i
&& 0 == frame_info
.samples
)
279 empty_first_frame
= true;
282 lead_trim
-= frame_samples
;
293 LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done
);