diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/vox.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/vox.c')
-rw-r--r-- | lib/rbcodec/codecs/vox.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/vox.c b/lib/rbcodec/codecs/vox.c new file mode 100644 index 0000000000..279d003162 --- /dev/null +++ b/lib/rbcodec/codecs/vox.c | |||
@@ -0,0 +1,201 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Yoshihisa Uchida | ||
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 "codecs/libpcm/support_formats.h" | ||
24 | |||
25 | CODEC_HEADER | ||
26 | |||
27 | /* vox codec (Dialogic telephony file formats) */ | ||
28 | |||
29 | #define PCM_SAMPLE_SIZE (2048) | ||
30 | |||
31 | static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR; | ||
32 | |||
33 | static struct pcm_format format; | ||
34 | static uint32_t bytesdone; | ||
35 | |||
36 | static uint8_t *read_buffer(size_t *realsize) | ||
37 | { | ||
38 | uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize); | ||
39 | if (bytesdone + (*realsize) > format.numbytes) | ||
40 | *realsize = format.numbytes - bytesdone; | ||
41 | bytesdone += *realsize; | ||
42 | ci->advance_buffer(*realsize); | ||
43 | return buffer; | ||
44 | } | ||
45 | |||
46 | /* this is the codec entry point */ | ||
47 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
48 | { | ||
49 | if (reason == CODEC_LOAD) { | ||
50 | /* Generic codec initialisation */ | ||
51 | ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1); | ||
52 | } | ||
53 | |||
54 | return CODEC_OK; | ||
55 | } | ||
56 | |||
57 | /* this is called for each file to process */ | ||
58 | enum codec_status codec_run(void) | ||
59 | { | ||
60 | uint32_t decodedsamples; | ||
61 | size_t n; | ||
62 | int bufcount; | ||
63 | int endofstream; | ||
64 | uint8_t *voxbuf; | ||
65 | off_t firstblockposn = 0; /* position of the first block in file */ | ||
66 | const struct pcm_codec *codec; | ||
67 | intptr_t param; | ||
68 | |||
69 | if (codec_init()) { | ||
70 | DEBUGF("codec_init() error\n"); | ||
71 | return CODEC_ERROR; | ||
72 | } | ||
73 | |||
74 | codec_set_replaygain(ci->id3); | ||
75 | |||
76 | /* Need to save offset for later use (cleared indirectly by advance_buffer) */ | ||
77 | bytesdone = ci->id3->offset; | ||
78 | ci->seek_buffer(0); | ||
79 | |||
80 | ci->memset(&format, 0, sizeof(struct pcm_format)); | ||
81 | |||
82 | /* set format */ | ||
83 | format.channels = 1; | ||
84 | format.bitspersample = 4; | ||
85 | format.numbytes = ci->id3->filesize; | ||
86 | format.blockalign = 1; | ||
87 | |||
88 | /* advance to first WAVE chunk */ | ||
89 | firstblockposn = 0; | ||
90 | decodedsamples = 0; | ||
91 | ci->advance_buffer(firstblockposn); | ||
92 | |||
93 | /* | ||
94 | * get codec | ||
95 | * supports dialogic oki adpcm only | ||
96 | */ | ||
97 | codec = get_dialogic_oki_adpcm_codec(); | ||
98 | if (!codec) | ||
99 | { | ||
100 | DEBUGF("CODEC_ERROR: dialogic oki adpcm codec does not load.\n"); | ||
101 | return CODEC_ERROR; | ||
102 | } | ||
103 | |||
104 | if (!codec->set_format(&format)) { | ||
105 | return CODEC_ERROR; | ||
106 | } | ||
107 | |||
108 | if (format.numbytes == 0) { | ||
109 | DEBUGF("CODEC_ERROR: data size is 0\n"); | ||
110 | return CODEC_ERROR; | ||
111 | } | ||
112 | |||
113 | /* check chunksize */ | ||
114 | if (format.chunksize * 2 > PCM_SAMPLE_SIZE) | ||
115 | format.chunksize = PCM_SAMPLE_SIZE / 2; | ||
116 | if (format.chunksize == 0) | ||
117 | { | ||
118 | DEBUGF("CODEC_ERROR: chunksize is 0\n"); | ||
119 | return CODEC_ERROR; | ||
120 | } | ||
121 | |||
122 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); | ||
123 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
124 | |||
125 | /* make sure we're at the correct offset */ | ||
126 | if (bytesdone > (uint32_t) firstblockposn) { | ||
127 | /* Round down to previous block */ | ||
128 | struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn, | ||
129 | PCM_SEEK_POS, &read_buffer); | ||
130 | |||
131 | if (newpos->pos > format.numbytes) { | ||
132 | return CODEC_OK; | ||
133 | } | ||
134 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
135 | { | ||
136 | bytesdone = newpos->pos; | ||
137 | decodedsamples = newpos->samples; | ||
138 | } | ||
139 | } else { | ||
140 | /* already where we need to be */ | ||
141 | bytesdone = 0; | ||
142 | } | ||
143 | |||
144 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
145 | |||
146 | /* The main decoder loop */ | ||
147 | endofstream = 0; | ||
148 | |||
149 | while (!endofstream) { | ||
150 | enum codec_command_action action = ci->get_command(¶m); | ||
151 | |||
152 | if (action == CODEC_ACTION_HALT) | ||
153 | break; | ||
154 | |||
155 | if (action == CODEC_ACTION_SEEK_TIME) { | ||
156 | struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME, | ||
157 | &read_buffer); | ||
158 | |||
159 | if (newpos->pos > format.numbytes) | ||
160 | { | ||
161 | ci->set_elapsed(ci->id3->length); | ||
162 | ci->seek_complete(); | ||
163 | break; | ||
164 | } | ||
165 | |||
166 | if (ci->seek_buffer(firstblockposn + newpos->pos)) | ||
167 | { | ||
168 | bytesdone = newpos->pos; | ||
169 | decodedsamples = newpos->samples; | ||
170 | } | ||
171 | |||
172 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
173 | ci->seek_complete(); | ||
174 | } | ||
175 | |||
176 | voxbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); | ||
177 | if (n == 0) | ||
178 | break; /* End of stream */ | ||
179 | if (bytesdone + n > format.numbytes) { | ||
180 | n = format.numbytes - bytesdone; | ||
181 | endofstream = 1; | ||
182 | } | ||
183 | |||
184 | if (codec->decode(voxbuf, n, samples, &bufcount) == CODEC_ERROR) | ||
185 | { | ||
186 | DEBUGF("codec error\n"); | ||
187 | return CODEC_ERROR; | ||
188 | } | ||
189 | |||
190 | ci->pcmbuf_insert(samples, NULL, bufcount); | ||
191 | ci->advance_buffer(n); | ||
192 | bytesdone += n; | ||
193 | decodedsamples += bufcount; | ||
194 | |||
195 | if (bytesdone >= format.numbytes) | ||
196 | endofstream = 1; | ||
197 | ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency); | ||
198 | } | ||
199 | |||
200 | return CODEC_OK; | ||
201 | } | ||