Make repeat mode/shuffle work in the sbs also
[maemo-rb.git] / apps / codecs / alac.c
blob3721f04f1fa434c29fd5f00474ccadb35615e2aa
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 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)
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(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
48 ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);
50 next_track:
51 retval = CODEC_OK;
53 /* Clean and initialize decoder structures */
54 memset(&demux_res , 0, sizeof(demux_res));
55 if (codec_init()) {
56 LOGF("ALAC: Error initialising codec\n");
57 retval = CODEC_ERROR;
58 goto exit;
61 if (codec_wait_taginfo() != 0)
62 goto done;
64 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
65 codec_set_replaygain(ci->id3);
67 stream_create(&input_stream,ci);
69 /* Read from ci->id3->offset before calling qtmovie_read. */
70 samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) /
71 (ci->id3->bitrate*128));
73 /* if qtmovie_read returns successfully, the stream is up to
74 * the movie data, which can be used directly by the decoder */
75 if (!qtmovie_read(&input_stream, &demux_res)) {
76 LOGF("ALAC: Error initialising file\n");
77 retval = CODEC_ERROR;
78 goto done;
81 /* initialise the sound converter */
82 create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac);
83 alac_set_info(&alac, demux_res.codecdata);
85 /* Set i for first frame, seek to desired sample position for resuming. */
86 i=0;
87 if (samplesdone > 0) {
88 if (alac_seek(&demux_res, &input_stream, samplesdone,
89 &samplesdone, (int*) &i)) {
90 elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100);
91 ci->set_elapsed(elapsedtime);
92 } else {
93 samplesdone = 0;
97 /* The main decoding loop */
98 while (i < demux_res.num_sample_byte_sizes) {
99 ci->yield();
100 if (ci->stop_codec || ci->new_track) {
101 break;
104 /* Deal with any pending seek requests */
105 if (ci->seek_time) {
106 if (alac_seek(&demux_res, &input_stream,
107 ((ci->seek_time-1)/10) * (ci->id3->frequency/100),
108 &samplesdone, (int *)&i)) {
109 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
110 ci->set_elapsed(elapsedtime);
112 ci->seek_complete();
115 /* Lookup the length (in samples and bytes) of block i */
116 if (!get_sample_info(&demux_res, i, &sample_duration,
117 &sample_byte_size)) {
118 LOGF("ALAC: Error in get_sample_info\n");
119 retval = CODEC_ERROR;
120 goto done;
123 /* Request the required number of bytes from the input buffer */
125 buffer=ci->request_buffer(&n,sample_byte_size);
126 if (n!=sample_byte_size) {
127 retval = CODEC_ERROR;
128 goto done;
131 /* Decode one block - returned samples will be host-endian */
132 ci->yield();
133 samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield);
135 /* Advance codec buffer n bytes */
136 ci->advance_buffer(n);
138 /* Output the audio */
139 ci->yield();
140 ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);
142 /* Update the elapsed-time indicator */
143 samplesdone+=sample_duration;
144 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
145 ci->set_elapsed(elapsedtime);
147 i++;
150 done:
151 LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone);
153 if (ci->request_next_track())
154 goto next_track;
156 exit:
157 return retval;