Added 'keywords' and 'eol-style' properties.
[kugel-rb.git] / apps / codecs / alac.c
blob367be148241cb72f88974a82fdfdae87a5985a31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "codeclib.h"
23 #include "libm4a/m4a.h"
24 #include "libalac/decomp.h"
26 CODEC_HEADER
28 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR;
30 /* this is the codec entry point */
31 enum codec_status codec_main(void)
33 size_t n;
34 demux_res_t demux_res;
35 stream_t input_stream;
36 uint32_t samplesdone;
37 uint32_t elapsedtime;
38 uint32_t sample_duration;
39 uint32_t sample_byte_size;
40 int samplesdecoded;
41 unsigned int i;
42 unsigned char* buffer;
43 alac_file alac;
44 int retval;
46 /* Generic codec initialisation */
47 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
49 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
50 ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);
52 next_track:
54 if (codec_init()) {
55 LOGF("ALAC: Error initialising codec\n");
56 retval = CODEC_ERROR;
57 goto exit;
60 while (!*ci->taginfo_ready && !ci->stop_codec)
61 ci->sleep(1);
63 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
64 codec_set_replaygain(ci->id3);
66 stream_create(&input_stream,ci);
68 /* if qtmovie_read returns successfully, the stream is up to
69 * the movie data, which can be used directly by the decoder */
70 if (!qtmovie_read(&input_stream, &demux_res)) {
71 LOGF("ALAC: Error initialising file\n");
72 retval = CODEC_ERROR;
73 goto done;
76 /* initialise the sound converter */
77 create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac);
78 alac_set_info(&alac, demux_res.codecdata);
80 i=0;
81 samplesdone=0;
82 /* The main decoding loop */
83 while (i < demux_res.num_sample_byte_sizes) {
84 ci->yield();
85 if (ci->stop_codec || ci->new_track) {
86 break;
89 /* Deal with any pending seek requests */
90 if (ci->seek_time) {
91 if (alac_seek(&demux_res, &input_stream,
92 ((ci->seek_time-1)/10) * (ci->id3->frequency/100),
93 &samplesdone, (int *)&i)) {
94 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
95 ci->set_elapsed(elapsedtime);
97 ci->seek_complete();
100 /* Lookup the length (in samples and bytes) of block i */
101 if (!get_sample_info(&demux_res, i, &sample_duration,
102 &sample_byte_size)) {
103 LOGF("ALAC: Error in get_sample_info\n");
104 retval = CODEC_ERROR;
105 goto done;
108 /* Request the required number of bytes from the input buffer */
110 buffer=ci->request_buffer(&n,sample_byte_size);
111 if (n!=sample_byte_size) {
112 retval = CODEC_ERROR;
113 goto done;
116 /* Decode one block - returned samples will be host-endian */
117 ci->yield();
118 samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield);
120 /* Advance codec buffer n bytes */
121 ci->advance_buffer(n);
123 /* Output the audio */
124 ci->yield();
125 ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);
127 /* Update the elapsed-time indicator */
128 samplesdone+=sample_duration;
129 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
130 ci->set_elapsed(elapsedtime);
132 /* Keep track of current position - for resuming */
133 ci->set_offset(elapsedtime);
135 i++;
137 retval = CODEC_OK;
139 done:
140 LOGF("ALAC: Decoded %ld samples\n",samplesdone);
142 if (ci->request_next_track())
143 goto next_track;
145 exit:
146 return retval;