Refactor alac decoder as preparation for upcoming m4a changes. The alac decoder does...
[kugel-rb.git] / apps / codecs / alac.c
blob428fc59836201009c7cc29a31357a7376e649107
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 int samplesdecoded;
39 unsigned int i;
40 unsigned char* buffer;
41 alac_file alac;
42 int retval;
44 /* Generic codec initialisation */
45 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
46 ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);
48 next_track:
49 retval = CODEC_OK;
51 /* Clean and initialize decoder structures */
52 memset(&demux_res , 0, sizeof(demux_res));
53 if (codec_init()) {
54 LOGF("ALAC: Error initialising codec\n");
55 retval = CODEC_ERROR;
56 goto exit;
59 if (codec_wait_taginfo() != 0)
60 goto done;
62 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
63 codec_set_replaygain(ci->id3);
65 stream_create(&input_stream,ci);
67 /* Read from ci->id3->offset before calling qtmovie_read. */
68 samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) /
69 (ci->id3->bitrate*128));
71 /* if qtmovie_read returns successfully, the stream is up to
72 * the movie data, which can be used directly by the decoder */
73 if (!qtmovie_read(&input_stream, &demux_res)) {
74 LOGF("ALAC: Error initialising file\n");
75 retval = CODEC_ERROR;
76 goto done;
79 /* initialise the sound converter */
80 create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac);
81 alac_set_info(&alac, demux_res.codecdata);
83 /* Set i for first frame, seek to desired sample position for resuming. */
84 i=0;
85 if (samplesdone > 0) {
86 if (alac_seek(&demux_res, &input_stream, samplesdone,
87 &samplesdone, (int*) &i)) {
88 elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100);
89 ci->set_elapsed(elapsedtime);
90 } else {
91 samplesdone = 0;
95 /* The main decoding loop */
96 while (i < demux_res.num_sample_byte_sizes) {
97 ci->yield();
98 if (ci->stop_codec || ci->new_track) {
99 break;
102 /* Deal with any pending seek requests */
103 if (ci->seek_time) {
104 if (alac_seek(&demux_res, &input_stream,
105 ((ci->seek_time-1)/10) * (ci->id3->frequency/100),
106 &samplesdone, (int *)&i)) {
107 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
108 ci->set_elapsed(elapsedtime);
110 ci->seek_complete();
113 /* Request the required number of bytes from the input buffer */
114 buffer=ci->request_buffer(&n, demux_res.sample_byte_size[i]);
115 if (n!=demux_res.sample_byte_size[i]) {
116 retval = CODEC_ERROR;
117 goto done;
120 /* Decode one block - returned samples will be host-endian */
121 ci->yield();
122 samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield);
124 /* Advance codec buffer by amount of consumed bytes */
125 ci->advance_buffer(alac.bytes_consumed);
127 /* Output the audio */
128 ci->yield();
129 ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);
131 /* Update the elapsed-time indicator */
132 samplesdone+=samplesdecoded;
133 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
134 ci->set_elapsed(elapsedtime);
136 i++;
139 done:
140 LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone);
142 if (ci->request_next_track())
143 goto next_track;
145 exit:
146 return retval;