blob: b7811024697cf72f4537f888db1255b03b2b90ed [file] [log] [blame]
Dave Chapmancea6d0c2005-10-31 20:56:29 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Dave Chapmancea6d0c2005-10-31 20:56:29 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "codeclib.h"
23#include "libm4a/m4a.h"
24#include "libfaad/common.h"
25#include "libfaad/structs.h"
26#include "libfaad/decoder.h"
27
Jens Arnoldb8749fd2006-01-18 00:05:14 +000028CODEC_HEADER
29
Dave Chapmancea6d0c2005-10-31 20:56:29 +000030/* this is the codec entry point */
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000031enum codec_status codec_main(void)
Dave Chapmancea6d0c2005-10-31 20:56:29 +000032{
Magnus Holmgren9896fd12006-10-11 17:02:23 +000033 /* 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).
38 */
Dave Chapmanb08a9422005-10-31 23:34:14 +000039 size_t n;
40 static demux_res_t demux_res;
41 stream_t input_stream;
Magnus Holmgren9896fd12006-10-11 17:02:23 +000042 uint32_t sound_samples_done;
43 uint32_t elapsed_time;
Dave Chapmanb08a9422005-10-31 23:34:14 +000044 uint32_t sample_duration;
45 uint32_t sample_byte_size;
Magnus Holmgren9896fd12006-10-11 17:02:23 +000046 int file_offset;
Magnus Holmgrenc3206a42007-06-16 13:00:52 +000047 int framelength;
48 int lead_trim = 0;
Dave Chapmanb08a9422005-10-31 23:34:14 +000049 unsigned int i;
50 unsigned char* buffer;
Magnus Holmgren9896fd12006-10-11 17:02:23 +000051 static NeAACDecFrameInfo frame_info;
52 NeAACDecHandle decoder;
Dave Chapmanb08a9422005-10-31 23:34:14 +000053 int err;
Magnus Holmgren9896fd12006-10-11 17:02:23 +000054 uint32_t s = 0;
55 unsigned char c = 0;
Dave Chapmancea6d0c2005-10-31 20:56:29 +000056
Dave Chapmanb08a9422005-10-31 23:34:14 +000057 /* Generic codec initialisation */
Michael Sevakis97f369a2007-02-10 16:34:16 +000058 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
Dave Chapmancea6d0c2005-10-31 20:56:29 +000059
Michael Sevakis97f369a2007-02-10 16:34:16 +000060 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
61 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
Dave Chapmancea6d0c2005-10-31 20:56:29 +000062
Brandon Lowebadcc62006-04-15 02:03:11 +000063next_track:
Magnus Holmgren9896fd12006-10-11 17:02:23 +000064 err = CODEC_OK;
Dave Chapmancea6d0c2005-10-31 20:56:29 +000065
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000066 if (codec_init()) {
Magnus Holmgren9896fd12006-10-11 17:02:23 +000067 LOGF("FAAD: Codec init error\n");
Brandon Low1060e442006-01-18 20:22:03 +000068 err = CODEC_ERROR;
69 goto exit;
Dave Chapmancea6d0c2005-10-31 20:56:29 +000070 }
71
Magnus Holmgren9f09a392006-08-23 13:10:48 +000072 while (!*ci->taginfo_ready && !ci->stop_codec)
73 ci->sleep(1);
Dave Chapmanb08a9422005-10-31 23:34:14 +000074
Magnus Holmgren9896fd12006-10-11 17:02:23 +000075 sound_samples_done = ci->id3->offset;
Magnus Holmgren9f09a392006-08-23 13:10:48 +000076
Michael Sevakis97f369a2007-02-10 16:34:16 +000077 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
Tomasz Malesinski80da8b12006-11-26 18:31:41 +000078 codec_set_replaygain(ci->id3);
Dave Chapmanb08a9422005-10-31 23:34:14 +000079
80 stream_create(&input_stream,ci);
81
82 /* if qtmovie_read returns successfully, the stream is up to
83 * the movie data, which can be used directly by the decoder */
84 if (!qtmovie_read(&input_stream, &demux_res)) {
Magnus Holmgren9896fd12006-10-11 17:02:23 +000085 LOGF("FAAD: File init error\n");
Brandon Low1060e442006-01-18 20:22:03 +000086 err = CODEC_ERROR;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +000087 goto done;
Dave Chapmanb08a9422005-10-31 23:34:14 +000088 }
89
90 /* initialise the sound converter */
Magnus Holmgren9896fd12006-10-11 17:02:23 +000091 decoder = NeAACDecOpen();
Dave Chapmanb08a9422005-10-31 23:34:14 +000092
Magnus Holmgren9896fd12006-10-11 17:02:23 +000093 if (!decoder) {
94 LOGF("FAAD: Decode open error\n");
Brandon Low1060e442006-01-18 20:22:03 +000095 err = CODEC_ERROR;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +000096 goto done;
Dave Chapmanb08a9422005-10-31 23:34:14 +000097 }
98
Magnus Holmgren9896fd12006-10-11 17:02:23 +000099 NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
Thom Johansen0263ece2005-11-02 19:43:52 +0000100 conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000101 NeAACDecSetConfiguration(decoder, conf);
Dave Chapmanb08a9422005-10-31 23:34:14 +0000102
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000103 err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
Dave Chapmanb08a9422005-10-31 23:34:14 +0000104 if (err) {
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000105 LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
Brandon Low1060e442006-01-18 20:22:03 +0000106 err = CODEC_ERROR;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000107 goto done;
Dave Chapmanb08a9422005-10-31 23:34:14 +0000108 }
109
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000110 ci->id3->frequency = s;
Dave Chapmanb08a9422005-10-31 23:34:14 +0000111
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000112 i = 0;
Magnus Holmgren9f09a392006-08-23 13:10:48 +0000113
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000114 if (sound_samples_done > 0) {
115 if (alac_seek_raw(&demux_res, &input_stream, sound_samples_done,
116 &sound_samples_done, (int*) &i)) {
117 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
118 ci->set_elapsed(elapsed_time);
Magnus Holmgren9f09a392006-08-23 13:10:48 +0000119 } else {
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000120 sound_samples_done = 0;
Magnus Holmgren9f09a392006-08-23 13:10:48 +0000121 }
122 }
Magnus Holmgrenc3206a42007-06-16 13:00:52 +0000123
124 if (i == 0)
125 {
126 lead_trim = ci->id3->lead_trim;
127 }
Magnus Holmgren9f09a392006-08-23 13:10:48 +0000128
Dave Chapmanb08a9422005-10-31 23:34:14 +0000129 /* The main decoding loop */
130 while (i < demux_res.num_sample_byte_sizes) {
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000131 ci->yield();
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000132
Brandon Lowebadcc62006-04-15 02:03:11 +0000133 if (ci->stop_codec || ci->new_track) {
Dave Chapmanb08a9422005-10-31 23:34:14 +0000134 break;
135 }
136
137 /* Deal with any pending seek requests */
138 if (ci->seek_time) {
Daniel Stenberg76667e22005-12-02 08:42:48 +0000139 if (alac_seek(&demux_res, &input_stream,
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000140 ((ci->seek_time-1)/10)*(ci->id3->frequency/100),
141 &sound_samples_done, (int*) &i)) {
142 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
143 ci->set_elapsed(elapsed_time);
Magnus Holmgrenc3206a42007-06-16 13:00:52 +0000144
145 if (i == 0)
146 {
147 lead_trim = ci->id3->lead_trim;
148 }
Dave Chapmanb08a9422005-10-31 23:34:14 +0000149 }
Dave Chapman5006d152005-11-02 00:09:42 +0000150 ci->seek_complete();
Dave Chapmanb08a9422005-10-31 23:34:14 +0000151 }
152
153 /* Lookup the length (in samples and bytes) of block i */
154 if (!get_sample_info(&demux_res, i, &sample_duration,
155 &sample_byte_size)) {
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000156 LOGF("AAC: get_sample_info error\n");
Brandon Low1060e442006-01-18 20:22:03 +0000157 err = CODEC_ERROR;
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000158 goto done;
Dave Chapmanb08a9422005-10-31 23:34:14 +0000159 }
160
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000161 /* There can be gaps between chunks, so skip ahead if needed. It
162 * doesn't seem to happen much, but it probably means that a
163 * "proper" file can have chunks out of order. Why one would want
164 * that an good question (but files with gaps do exist, so who
165 * knows?), so we don't support that - for now, at least.
166 */
167 file_offset = get_sample_offset(&demux_res, i);
168
169 if (file_offset > ci->curpos)
170 {
171 ci->advance_buffer(file_offset - ci->curpos);
172 }
Magnus Holmgrenfc1efc72007-01-30 21:42:36 +0000173 else if (file_offset == 0)
174 {
175 LOGF("AAC: get_sample_offset error\n");
176 err = CODEC_ERROR;
177 goto done;
178 }
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000179
Dave Chapmanb08a9422005-10-31 23:34:14 +0000180 /* Request the required number of bytes from the input buffer */
Brandon Lowc76904b2006-03-24 14:02:27 +0000181 buffer=ci->request_buffer(&n,sample_byte_size);
Dave Chapmanb08a9422005-10-31 23:34:14 +0000182
183 /* Decode one block - returned samples will be host-endian */
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000184 NeAACDecDecode(decoder, &frame_info, buffer, n);
185 /* Ignore return value, we access samples in the decoder struct
186 * directly.
187 */
188 if (frame_info.error > 0) {
189 LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
190 err = CODEC_ERROR;
191 goto done;
Dave Chapmanb08a9422005-10-31 23:34:14 +0000192 }
193
Dave Chapmanb08a9422005-10-31 23:34:14 +0000194 /* Advance codec buffer */
195 ci->advance_buffer(n);
196
197 /* Output the audio */
Tomasz Malesinski80da8b12006-11-26 18:31:41 +0000198 ci->yield();
Magnus Holmgrenc3206a42007-06-16 13:00:52 +0000199
200 framelength = (frame_info.samples >> 1) - lead_trim;
201
202 if (i == demux_res.num_sample_byte_sizes - 1 && framelength > 0)
203 {
204 /* Currently limited to at most one frame of tail_trim.
205 * Seems to be enough.
206 */
207 if (ci->id3->tail_trim == 0
208 && sample_duration < (frame_info.samples >> 1))
209 {
210 /* Subtract lead_trim just in case we decode a file with
211 * only one audio frame with actual data.
212 */
213 framelength = sample_duration - lead_trim;
214 }
215 else
216 {
217 framelength -= ci->id3->tail_trim;
218 }
219 }
220
221 if (framelength > 0)
222 {
223 ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
224 &decoder->time_out[1][lead_trim],
225 framelength);
226 }
227
228 if (lead_trim > 0)
229 {
230 /* frame_info.samples can be 0 for the first frame */
231 lead_trim -= (i > 0 || frame_info.samples)
232 ? (frame_info.samples >> 1) : sample_duration;
233
234 if (lead_trim < 0 || ci->id3->lead_trim == 0)
235 {
236 lead_trim = 0;
237 }
238 }
Dave Chapmanb08a9422005-10-31 23:34:14 +0000239
240 /* Update the elapsed-time indicator */
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000241 sound_samples_done += sample_duration;
242 elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
243 ci->set_elapsed(elapsed_time);
Dave Chapmanb08a9422005-10-31 23:34:14 +0000244
245 /* Keep track of current position - for resuming */
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000246 ci->set_offset(elapsed_time);
Dave Chapmanb08a9422005-10-31 23:34:14 +0000247
248 i++;
Dave Chapmancea6d0c2005-10-31 20:56:29 +0000249 }
Magnus Holmgren9896fd12006-10-11 17:02:23 +0000250
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000251 err = CODEC_OK;
Dave Chapmancea6d0c2005-10-31 20:56:29 +0000252
Brandon Lowf3bc1ef2006-04-22 14:40:13 +0000253done:
Michael Sevakis33545312007-03-20 13:53:23 +0000254 LOGF("AAC: Decoded %lu samples\n", sound_samples_done);
Dave Chapmancea6d0c2005-10-31 20:56:29 +0000255
Dave Chapmanb08a9422005-10-31 23:34:14 +0000256 if (ci->request_next_track())
257 goto next_track;
Dave Chapmancea6d0c2005-10-31 20:56:29 +0000258
Brandon Low1060e442006-01-18 20:22:03 +0000259exit:
260 return err;
Dave Chapmancea6d0c2005-10-31 20:56:29 +0000261}