Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / alac.c
blob82bda264c3ed0f52324255b49aa40ac6909bf766
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 /* The maximum buffer size handled. This amount of bytes is buffered for each
29 * frame. */
30 #define ALAC_BYTE_BUFFER_SIZE 32768
32 static int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR;
34 /* this is the codec entry point */
35 enum codec_status codec_main(enum codec_entry_call_reason reason)
37 if (reason == CODEC_LOAD) {
38 /* Generic codec initialisation */
39 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
40 ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);
43 return CODEC_OK;
46 /* this is called for each file to process */
47 enum codec_status codec_run(void)
49 size_t n;
50 demux_res_t demux_res;
51 stream_t input_stream;
52 uint32_t samplesdone;
53 uint32_t elapsedtime = 0;
54 int samplesdecoded;
55 unsigned int i;
56 unsigned char* buffer;
57 alac_file alac;
58 intptr_t param;
60 /* Clean and initialize decoder structures */
61 memset(&demux_res , 0, sizeof(demux_res));
62 if (codec_init()) {
63 LOGF("ALAC: Error initialising codec\n");
64 return CODEC_ERROR;
67 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
68 codec_set_replaygain(ci->id3);
70 ci->seek_buffer(0);
72 stream_create(&input_stream,ci);
74 /* Read from ci->id3->offset before calling qtmovie_read. */
75 samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) /
76 (ci->id3->bitrate*128));
78 /* if qtmovie_read returns successfully, the stream is up to
79 * the movie data, which can be used directly by the decoder */
80 if (!qtmovie_read(&input_stream, &demux_res)) {
81 LOGF("ALAC: Error initialising file\n");
82 return CODEC_ERROR;
85 /* initialise the sound converter */
86 create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac);
87 alac_set_info(&alac, demux_res.codecdata);
89 /* Set i for first frame, seek to desired sample position for resuming. */
90 i=0;
91 if (samplesdone > 0) {
92 if (m4a_seek(&demux_res, &input_stream, samplesdone,
93 &samplesdone, (int*) &i)) {
94 elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100);
95 ci->set_elapsed(elapsedtime);
96 } else {
97 samplesdone = 0;
101 /* The main decoding loop */
102 while (i < demux_res.num_sample_byte_sizes) {
103 enum codec_command_action action = ci->get_command(&param);
105 if (action == CODEC_ACTION_HALT)
106 break;
108 /* Request the required number of bytes from the input buffer */
109 buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE);
111 /* Deal with any pending seek requests */
112 if (action == CODEC_ACTION_SEEK_TIME) {
113 if (m4a_seek(&demux_res, &input_stream,
114 (param/10) * (ci->id3->frequency/100),
115 &samplesdone, (int *)&i)) {
116 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
118 ci->set_elapsed(elapsedtime);
119 ci->seek_complete();
122 /* Request the required number of bytes from the input buffer */
123 buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE);
125 /* Decode one block - returned samples will be host-endian */
126 samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield);
127 ci->yield();
129 /* Advance codec buffer by amount of consumed bytes */
130 ci->advance_buffer(alac.bytes_consumed);
132 /* Output the audio */
133 ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);
135 /* Update the elapsed-time indicator */
136 samplesdone+=samplesdecoded;
137 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
138 ci->set_elapsed(elapsedtime);
140 i++;
143 LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone);
144 return CODEC_OK;