Updated our source code header to explicitly mention that we are GPL v2 or
[Rockbox.git] / apps / codecs / wavpack.c
blobc93e2e0dcdd448d14299dfd0ba031cbbaf6606af
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Bryant
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 "libwavpack/wavpack.h"
25 CODEC_HEADER
27 #define BUFFER_SIZE 4096
29 static int32_t temp_buffer [BUFFER_SIZE] IBSS_ATTR;
31 static int32_t read_callback (void *buffer, int32_t bytes)
33 int32_t retval = ci->read_filebuf (buffer, bytes);
34 ci->id3->offset = ci->curpos;
35 return retval;
38 /* this is the codec entry point */
39 enum codec_status codec_main(void)
41 WavpackContext *wpc;
42 char error [80];
43 int bps, nchans, sr_100;
44 int retval;
46 /* Generic codec initialisation */
47 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
49 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
51 next_track:
53 if (codec_init()) {
54 retval = CODEC_ERROR;
55 goto exit;
58 while (!*ci->taginfo_ready && !ci->stop_codec)
59 ci->sleep(1);
61 /* Create a decoder instance */
62 wpc = WavpackOpenFileInput (read_callback, error);
64 if (!wpc) {
65 retval = CODEC_ERROR;
66 goto done;
69 ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc));
70 codec_set_replaygain(ci->id3);
71 bps = WavpackGetBytesPerSample (wpc);
72 nchans = WavpackGetReducedChannels (wpc);
73 ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO);
74 sr_100 = ci->id3->frequency / 100;
76 ci->set_elapsed (0);
78 /* The main decoder loop */
80 while (1) {
81 int32_t nsamples;
83 if (ci->seek_time && ci->taginfo_ready && ci->id3->length) {
84 ci->seek_time--;
85 int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
86 int n, d, skip;
88 if (ci->seek_time > curpos_ms) {
89 n = ci->seek_time - curpos_ms;
90 d = ci->id3->length - curpos_ms;
91 skip = (int)((int64_t)(ci->filesize - ci->curpos) * n / d);
92 ci->seek_buffer (ci->curpos + skip);
94 else {
95 n = curpos_ms - ci->seek_time;
96 d = curpos_ms;
97 skip = (int)((int64_t) ci->curpos * n / d);
98 ci->seek_buffer (ci->curpos - skip);
101 wpc = WavpackOpenFileInput (read_callback, error);
102 ci->seek_complete();
104 if (!wpc)
105 break;
107 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
108 ci->yield ();
111 nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
113 if (!nsamples || ci->stop_codec || ci->new_track)
114 break;
116 ci->yield ();
118 if (ci->stop_codec || ci->new_track)
119 break;
121 ci->pcmbuf_insert (temp_buffer, NULL, nsamples);
123 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
124 ci->yield ();
126 retval = CODEC_OK;
128 done:
129 if (ci->request_next_track())
130 goto next_track;
132 exit:
133 return retval;