summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/wavpack.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/wavpack.c')
-rw-r--r--lib/rbcodec/codecs/wavpack.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/wavpack.c b/lib/rbcodec/codecs/wavpack.c
new file mode 100644
index 0000000000..4d42391fc1
--- /dev/null
+++ b/lib/rbcodec/codecs/wavpack.c
@@ -0,0 +1,127 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 David Bryant
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "codeclib.h"
23#include "libwavpack/wavpack.h"
24
25CODEC_HEADER
26
27#define BUFFER_SIZE 4096
28
29static int32_t temp_buffer [BUFFER_SIZE] IBSS_ATTR;
30
31static int32_t read_callback (void *buffer, int32_t bytes)
32{
33 int32_t retval = ci->read_filebuf (buffer, bytes);
34 ci->set_offset(ci->curpos);
35 return retval;
36}
37
38/* this is the codec entry point */
39enum codec_status codec_main(enum codec_entry_call_reason reason)
40{
41 if (reason == CODEC_LOAD) {
42 /* Generic codec initialisation */
43 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
44 }
45
46 return CODEC_OK;
47}
48
49/* this is called for each file to process */
50enum codec_status codec_run(void)
51{
52 WavpackContext *wpc;
53 char error [80];
54 /* rockbox: comment 'set but unused' variables
55 int bps;
56 */
57 int nchans, sr_100;
58 intptr_t param;
59
60 if (codec_init())
61 return CODEC_ERROR;
62
63 ci->seek_buffer (ci->id3->offset);
64
65 /* Create a decoder instance */
66 wpc = WavpackOpenFileInput (read_callback, error);
67
68 if (!wpc)
69 return CODEC_ERROR;
70
71 ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc));
72 codec_set_replaygain(ci->id3);
73 /* bps = WavpackGetBytesPerSample (wpc); */
74 nchans = WavpackGetReducedChannels (wpc);
75 ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO);
76 sr_100 = ci->id3->frequency / 100;
77
78 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
79
80 /* The main decoder loop */
81
82 while (1) {
83 int32_t nsamples;
84 enum codec_command_action action = ci->get_command(&param);
85
86 if (action == CODEC_ACTION_HALT)
87 break;
88
89 if (action == CODEC_ACTION_SEEK_TIME) {
90 int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
91 int n, d, skip;
92
93 if (param > curpos_ms) {
94 n = param - curpos_ms;
95 d = ci->id3->length - curpos_ms;
96 skip = (int)((int64_t)(ci->filesize - ci->curpos) * n / d);
97 ci->seek_buffer (ci->curpos + skip);
98 }
99 else if (curpos_ms != 0) {
100 n = curpos_ms - param;
101 d = curpos_ms;
102 skip = (int)((int64_t) ci->curpos * n / d);
103 ci->seek_buffer (ci->curpos - skip);
104 }
105
106 wpc = WavpackOpenFileInput (read_callback, error);
107 if (!wpc)
108 {
109 ci->seek_complete();
110 break;
111 }
112
113 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
114 ci->seek_complete();
115 }
116
117 nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
118
119 if (!nsamples)
120 break;
121
122 ci->pcmbuf_insert (temp_buffer, NULL, nsamples);
123 ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
124 }
125
126 return CODEC_OK;
127}