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 "libalac/decomp.h"
28 static int32_t outputbuffer
[ALAC_MAX_CHANNELS
][ALAC_BLOCKSIZE
] IBSS_ATTR
;
30 /* this is the codec entry point */
31 enum codec_status
codec_main(void)
34 demux_res_t demux_res
;
35 stream_t input_stream
;
38 uint32_t sample_duration
;
39 uint32_t sample_byte_size
;
42 unsigned char* buffer
;
46 /* Generic codec initialisation */
47 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
48 ci
->configure(DSP_SET_SAMPLE_DEPTH
, ALAC_OUTPUT_DEPTH
-1);
52 /* Clean and initialize decoder structures */
53 memset(&demux_res
, 0, sizeof(demux_res
));
55 LOGF("ALAC: Error initialising codec\n");
60 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
63 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
64 codec_set_replaygain(ci
->id3
);
66 stream_create(&input_stream
,ci
);
68 /* Read from ci->id3->offset before calling qtmovie_read. */
69 samplesdone
= (uint32_t)(((uint64_t)(ci
->id3
->offset
) * ci
->id3
->frequency
) /
70 (ci
->id3
->bitrate
*128));
72 /* if qtmovie_read returns successfully, the stream is up to
73 * the movie data, which can be used directly by the decoder */
74 if (!qtmovie_read(&input_stream
, &demux_res
)) {
75 LOGF("ALAC: Error initialising file\n");
80 /* initialise the sound converter */
81 create_alac(demux_res
.sound_sample_size
, demux_res
.num_channels
,&alac
);
82 alac_set_info(&alac
, demux_res
.codecdata
);
84 /* Set i for first frame, seek to desired sample position for resuming. */
86 if (samplesdone
> 0) {
87 if (alac_seek(&demux_res
, &input_stream
, samplesdone
,
88 &samplesdone
, (int*) &i
)) {
89 elapsedtime
= (samplesdone
* 10) / (ci
->id3
->frequency
/ 100);
90 ci
->set_elapsed(elapsedtime
);
96 /* The main decoding loop */
97 while (i
< demux_res
.num_sample_byte_sizes
) {
99 if (ci
->stop_codec
|| ci
->new_track
) {
103 /* Deal with any pending seek requests */
105 if (alac_seek(&demux_res
, &input_stream
,
106 ((ci
->seek_time
-1)/10) * (ci
->id3
->frequency
/100),
107 &samplesdone
, (int *)&i
)) {
108 elapsedtime
=(samplesdone
*10)/(ci
->id3
->frequency
/100);
109 ci
->set_elapsed(elapsedtime
);
114 /* Lookup the length (in samples and bytes) of block i */
115 if (!get_sample_info(&demux_res
, i
, &sample_duration
,
116 &sample_byte_size
)) {
117 LOGF("ALAC: Error in get_sample_info\n");
118 retval
= CODEC_ERROR
;
122 /* Request the required number of bytes from the input buffer */
124 buffer
=ci
->request_buffer(&n
,sample_byte_size
);
125 if (n
!=sample_byte_size
) {
126 retval
= CODEC_ERROR
;
130 /* Decode one block - returned samples will be host-endian */
132 samplesdecoded
=alac_decode_frame(&alac
, buffer
, outputbuffer
, ci
->yield
);
134 /* Advance codec buffer n bytes */
135 ci
->advance_buffer(n
);
137 /* Output the audio */
139 ci
->pcmbuf_insert(outputbuffer
[0], outputbuffer
[1], samplesdecoded
);
141 /* Update the elapsed-time indicator */
142 samplesdone
+=sample_duration
;
143 elapsedtime
=(samplesdone
*10)/(ci
->id3
->frequency
/100);
144 ci
->set_elapsed(elapsedtime
);
151 LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone
);
153 if (ci
->request_next_track())