File: | libs/libsndfile/src/ms_adpcm.c |
Location: | line 558, column 18 |
Description: | Assigned value is garbage or undefined |
1 | /* | |||
2 | ** Copyright (C) 1999-2013 Erik de Castro Lopo <erikd@mega-nerd.com> | |||
3 | ** | |||
4 | ** This program is free software; you can redistribute it and/or modify | |||
5 | ** it under the terms of the GNU Lesser General Public License as published by | |||
6 | ** the Free Software Foundation; either version 2.1 of the License, or | |||
7 | ** (at your option) any later version. | |||
8 | ** | |||
9 | ** This program is distributed in the hope that it will be useful, | |||
10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
12 | ** GNU Lesser General Public License for more details. | |||
13 | ** | |||
14 | ** You should have received a copy of the GNU Lesser General Public License | |||
15 | ** along with this program; if not, write to the Free Software | |||
16 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |||
17 | */ | |||
18 | ||||
19 | #include "sfconfig.h" | |||
20 | ||||
21 | #include <stdio.h> | |||
22 | #include <stdlib.h> | |||
23 | #include <string.h> | |||
24 | #include <math.h> | |||
25 | ||||
26 | #include "sndfile.h" | |||
27 | #include "sfendian.h" | |||
28 | #include "common.h" | |||
29 | #include "wav_w64.h" | |||
30 | ||||
31 | /* These required here because we write the header in this file. */ | |||
32 | ||||
33 | #define RIFF_MARKER(((uint32_t) (('R') | (('I') << 8) | (('F') << 16 ) | (((uint32_t) ('F')) << 24)))) (MAKE_MARKER ('R', 'I', 'F', 'F')((uint32_t) (('R') | (('I') << 8) | (('F') << 16) | (((uint32_t) ('F')) << 24)))) | |||
34 | #define WAVE_MARKER(((uint32_t) (('W') | (('A') << 8) | (('V') << 16 ) | (((uint32_t) ('E')) << 24)))) (MAKE_MARKER ('W', 'A', 'V', 'E')((uint32_t) (('W') | (('A') << 8) | (('V') << 16) | (((uint32_t) ('E')) << 24)))) | |||
35 | #define fmt_MARKER(((uint32_t) (('f') | (('m') << 8) | (('t') << 16 ) | (((uint32_t) (' ')) << 24)))) (MAKE_MARKER ('f', 'm', 't', ' ')((uint32_t) (('f') | (('m') << 8) | (('t') << 16) | (((uint32_t) (' ')) << 24)))) | |||
36 | #define fact_MARKER(((uint32_t) (('f') | (('a') << 8) | (('c') << 16 ) | (((uint32_t) ('t')) << 24)))) (MAKE_MARKER ('f', 'a', 'c', 't')((uint32_t) (('f') | (('a') << 8) | (('c') << 16) | (((uint32_t) ('t')) << 24)))) | |||
37 | #define data_MARKER(((uint32_t) (('d') | (('a') << 8) | (('t') << 16 ) | (((uint32_t) ('a')) << 24)))) (MAKE_MARKER ('d', 'a', 't', 'a')((uint32_t) (('d') | (('a') << 8) | (('t') << 16) | (((uint32_t) ('a')) << 24)))) | |||
38 | ||||
39 | #define WAVE_FORMAT_MS_ADPCM0x0002 0x0002 | |||
40 | ||||
41 | typedef struct | |||
42 | { int channels, blocksize, samplesperblock, blocks, dataremaining ; | |||
43 | int blockcount ; | |||
44 | sf_count_t samplecount ; | |||
45 | short *samples ; | |||
46 | unsigned char *block ; | |||
47 | short dummydata [] ; /* ISO C99 struct flexible array. */ | |||
48 | } MSADPCM_PRIVATE ; | |||
49 | ||||
50 | /*============================================================================================ | |||
51 | ** MS ADPCM static data and functions. | |||
52 | */ | |||
53 | ||||
54 | static int AdaptationTable [] = | |||
55 | { 230, 230, 230, 230, 307, 409, 512, 614, | |||
56 | 768, 614, 512, 409, 307, 230, 230, 230 | |||
57 | } ; | |||
58 | ||||
59 | /* TODO : The first 7 coef's are are always hardcode and must | |||
60 | appear in the actual WAVE file. They should be read in | |||
61 | in case a sound program added extras to the list. */ | |||
62 | ||||
63 | static int AdaptCoeff1 [MSADPCM_ADAPT_COEFF_COUNT7] = | |||
64 | { 256, 512, 0, 192, 240, 460, 392 | |||
65 | } ; | |||
66 | ||||
67 | static int AdaptCoeff2 [MSADPCM_ADAPT_COEFF_COUNT7] = | |||
68 | { 0, -256, 0, 64, 0, -208, -232 | |||
69 | } ; | |||
70 | ||||
71 | /*============================================================================================ | |||
72 | ** MS ADPCM Block Layout. | |||
73 | ** ====================== | |||
74 | ** Block is usually 256, 512 or 1024 bytes depending on sample rate. | |||
75 | ** For a mono file, the block is laid out as follows: | |||
76 | ** byte purpose | |||
77 | ** 0 block predictor [0..6] | |||
78 | ** 1,2 initial idelta (positive) | |||
79 | ** 3,4 sample 1 | |||
80 | ** 5,6 sample 0 | |||
81 | ** 7..n packed bytecodes | |||
82 | ** | |||
83 | ** For a stereo file, the block is laid out as follows: | |||
84 | ** byte purpose | |||
85 | ** 0 block predictor [0..6] for left channel | |||
86 | ** 1 block predictor [0..6] for right channel | |||
87 | ** 2,3 initial idelta (positive) for left channel | |||
88 | ** 4,5 initial idelta (positive) for right channel | |||
89 | ** 6,7 sample 1 for left channel | |||
90 | ** 8,9 sample 1 for right channel | |||
91 | ** 10,11 sample 0 for left channel | |||
92 | ** 12,13 sample 0 for right channel | |||
93 | ** 14..n packed bytecodes | |||
94 | */ | |||
95 | ||||
96 | /*============================================================================================ | |||
97 | ** Static functions. | |||
98 | */ | |||
99 | ||||
100 | static int msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ; | |||
101 | static sf_count_t msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ; | |||
102 | ||||
103 | static int msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ; | |||
104 | static sf_count_t msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, const short *ptr, int len) ; | |||
105 | ||||
106 | static sf_count_t msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; | |||
107 | static sf_count_t msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ; | |||
108 | static sf_count_t msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ; | |||
109 | static sf_count_t msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ; | |||
110 | ||||
111 | static sf_count_t msadpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; | |||
112 | static sf_count_t msadpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; | |||
113 | static sf_count_t msadpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; | |||
114 | static sf_count_t msadpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; | |||
115 | ||||
116 | static sf_count_t msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; | |||
117 | static int msadpcm_close (SF_PRIVATE *psf) ; | |||
118 | ||||
119 | static void choose_predictor (unsigned int channels, short *data, int *bpred, int *idelta) ; | |||
120 | ||||
121 | /*============================================================================================ | |||
122 | ** MS ADPCM Read Functions. | |||
123 | */ | |||
124 | ||||
125 | int | |||
126 | wav_w64_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) | |||
127 | { MSADPCM_PRIVATE *pms ; | |||
128 | unsigned int pmssize ; | |||
129 | int count ; | |||
130 | ||||
131 | if (psf->codec_data != NULL((void*)0)) | |||
132 | { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ; | |||
133 | return SFE_INTERNAL ; | |||
134 | } ; | |||
135 | ||||
136 | if (psf->file.mode == SFM_WRITE) | |||
137 | samplesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ; | |||
138 | ||||
139 | pmssize = sizeof (MSADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ; | |||
140 | ||||
141 | if (! (psf->codec_data = calloc (1, pmssize))) | |||
142 | return SFE_MALLOC_FAILED ; | |||
143 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
144 | ||||
145 | pms->samples = pms->dummydata ; | |||
146 | pms->block = (unsigned char*) (pms->dummydata + psf->sf.channels * samplesperblock) ; | |||
147 | ||||
148 | pms->channels = psf->sf.channels ; | |||
149 | pms->blocksize = blockalign ; | |||
150 | pms->samplesperblock = samplesperblock ; | |||
151 | ||||
152 | if (pms->blocksize == 0) | |||
153 | { psf_log_printf (psf, "*** Error : pms->blocksize should not be zero.\n") ; | |||
154 | return SFE_INTERNAL ; | |||
155 | } ; | |||
156 | ||||
157 | if (psf->file.mode == SFM_READ) | |||
158 | { pms->dataremaining = psf->datalength ; | |||
159 | ||||
160 | if (psf->datalength % pms->blocksize) | |||
161 | pms->blocks = psf->datalength / pms->blocksize + 1 ; | |||
162 | else | |||
163 | pms->blocks = psf->datalength / pms->blocksize ; | |||
164 | ||||
165 | count = 2 * (pms->blocksize - 6 * pms->channels) / pms->channels ; | |||
166 | if (pms->samplesperblock != count) | |||
167 | { psf_log_printf (psf, "*** Error : samplesperblock should be %d.\n", count) ; | |||
168 | return SFE_INTERNAL ; | |||
169 | } ; | |||
170 | ||||
171 | psf->sf.frames = (psf->datalength / pms->blocksize) * pms->samplesperblock ; | |||
172 | ||||
173 | psf_log_printf (psf, " bpred idelta\n") ; | |||
174 | ||||
175 | msadpcm_decode_block (psf, pms) ; | |||
176 | ||||
177 | psf->read_short = msadpcm_read_s ; | |||
178 | psf->read_int = msadpcm_read_i ; | |||
179 | psf->read_float = msadpcm_read_f ; | |||
180 | psf->read_double = msadpcm_read_d ; | |||
181 | } ; | |||
182 | ||||
183 | if (psf->file.mode == SFM_WRITE) | |||
184 | { pms->samples = pms->dummydata ; | |||
185 | ||||
186 | pms->samplecount = 0 ; | |||
187 | ||||
188 | psf->write_short = msadpcm_write_s ; | |||
189 | psf->write_int = msadpcm_write_i ; | |||
190 | psf->write_float = msadpcm_write_f ; | |||
191 | psf->write_double = msadpcm_write_d ; | |||
192 | } ; | |||
193 | ||||
194 | psf->codec_close = msadpcm_close ; | |||
195 | psf->seek = msadpcm_seek ; | |||
196 | ||||
197 | return 0 ; | |||
198 | } /* wav_w64_msadpcm_init */ | |||
199 | ||||
200 | static int | |||
201 | msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) | |||
202 | { int chan, k, blockindx, sampleindx ; | |||
203 | short bytecode, bpred [2], chan_idelta [2] ; | |||
204 | ||||
205 | int predict ; | |||
206 | int current ; | |||
207 | int idelta ; | |||
208 | ||||
209 | pms->blockcount ++ ; | |||
210 | pms->samplecount = 0 ; | |||
211 | ||||
212 | if (pms->blockcount > pms->blocks) | |||
213 | { memset (pms->samples, 0, pms->samplesperblock * pms->channels) ; | |||
214 | return 1 ; | |||
215 | } ; | |||
216 | ||||
217 | if ((k = psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) | |||
218 | psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pms->blocksize) ; | |||
219 | ||||
220 | /* Read and check the block header. */ | |||
221 | ||||
222 | if (pms->channels == 1) | |||
223 | { bpred [0] = pms->block [0] ; | |||
224 | ||||
225 | if (bpred [0] >= 7) | |||
226 | psf_log_printf (psf, "MS ADPCM synchronisation error (%d).\n", bpred [0]) ; | |||
227 | ||||
228 | chan_idelta [0] = pms->block [1] | (pms->block [2] << 8) ; | |||
229 | chan_idelta [1] = 0 ; | |||
230 | ||||
231 | psf_log_printf (psf, "(%d) (%d)\n", bpred [0], chan_idelta [0]) ; | |||
232 | ||||
233 | pms->samples [1] = pms->block [3] | (pms->block [4] << 8) ; | |||
234 | pms->samples [0] = pms->block [5] | (pms->block [6] << 8) ; | |||
235 | blockindx = 7 ; | |||
236 | } | |||
237 | else | |||
238 | { bpred [0] = pms->block [0] ; | |||
239 | bpred [1] = pms->block [1] ; | |||
240 | ||||
241 | if (bpred [0] >= 7 || bpred [1] >= 7) | |||
242 | psf_log_printf (psf, "MS ADPCM synchronisation error (%d %d).\n", bpred [0], bpred [1]) ; | |||
243 | ||||
244 | chan_idelta [0] = pms->block [2] | (pms->block [3] << 8) ; | |||
245 | chan_idelta [1] = pms->block [4] | (pms->block [5] << 8) ; | |||
246 | ||||
247 | psf_log_printf (psf, "(%d, %d) (%d, %d)\n", bpred [0], bpred [1], chan_idelta [0], chan_idelta [1]) ; | |||
248 | ||||
249 | pms->samples [2] = pms->block [6] | (pms->block [7] << 8) ; | |||
250 | pms->samples [3] = pms->block [8] | (pms->block [9] << 8) ; | |||
251 | ||||
252 | pms->samples [0] = pms->block [10] | (pms->block [11] << 8) ; | |||
253 | pms->samples [1] = pms->block [12] | (pms->block [13] << 8) ; | |||
254 | ||||
255 | blockindx = 14 ; | |||
256 | } ; | |||
257 | ||||
258 | /*-------------------------------------------------------- | |||
259 | This was left over from a time when calculations were done | |||
260 | as ints rather than shorts. Keep this around as a reminder | |||
261 | in case I ever find a file which decodes incorrectly. | |||
262 | ||||
263 | if (chan_idelta [0] & 0x8000) | |||
264 | chan_idelta [0] -= 0x10000 ; | |||
265 | if (chan_idelta [1] & 0x8000) | |||
266 | chan_idelta [1] -= 0x10000 ; | |||
267 | --------------------------------------------------------*/ | |||
268 | ||||
269 | /* Pull apart the packed 4 bit samples and store them in their | |||
270 | ** correct sample positions. | |||
271 | */ | |||
272 | ||||
273 | sampleindx = 2 * pms->channels ; | |||
274 | while (blockindx < pms->blocksize) | |||
275 | { bytecode = pms->block [blockindx++] ; | |||
276 | pms->samples [sampleindx++] = (bytecode >> 4) & 0x0F ; | |||
277 | pms->samples [sampleindx++] = bytecode & 0x0F ; | |||
278 | } ; | |||
279 | ||||
280 | /* Decode the encoded 4 bit samples. */ | |||
281 | ||||
282 | for (k = 2 * pms->channels ; k < (pms->samplesperblock * pms->channels) ; k ++) | |||
283 | { chan = (pms->channels > 1) ? (k % 2) : 0 ; | |||
284 | ||||
285 | bytecode = pms->samples [k] & 0xF ; | |||
286 | ||||
287 | /* Compute next Adaptive Scale Factor (ASF) */ | |||
288 | idelta = chan_idelta [chan] ; | |||
289 | chan_idelta [chan] = (AdaptationTable [bytecode] * idelta) >> 8 ; /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */ | |||
290 | if (chan_idelta [chan] < 16) | |||
291 | chan_idelta [chan] = 16 ; | |||
292 | if (bytecode & 0x8) | |||
293 | bytecode -= 0x10 ; | |||
294 | ||||
295 | predict = ((pms->samples [k - pms->channels] * AdaptCoeff1 [bpred [chan]]) | |||
296 | + (pms->samples [k - 2 * pms->channels] * AdaptCoeff2 [bpred [chan]])) >> 8 ; /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */ | |||
297 | current = (bytecode * idelta) + predict ; | |||
298 | ||||
299 | if (current > 32767) | |||
300 | current = 32767 ; | |||
301 | else if (current < -32768) | |||
302 | current = -32768 ; | |||
303 | ||||
304 | pms->samples [k] = current ; | |||
305 | } ; | |||
306 | ||||
307 | return 1 ; | |||
308 | } /* msadpcm_decode_block */ | |||
309 | ||||
310 | static sf_count_t | |||
311 | msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) | |||
312 | { int count, total = 0, indx = 0 ; | |||
313 | ||||
314 | while (indx < len) | |||
315 | { if (pms->blockcount >= pms->blocks && pms->samplecount >= pms->samplesperblock) | |||
316 | { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ; | |||
317 | return total ; | |||
318 | } ; | |||
319 | ||||
320 | if (pms->samplecount >= pms->samplesperblock) | |||
321 | msadpcm_decode_block (psf, pms) ; | |||
322 | ||||
323 | count = (pms->samplesperblock - pms->samplecount) * pms->channels ; | |||
324 | count = (len - indx > count) ? count : len - indx ; | |||
325 | ||||
326 | memcpy (&(ptr [indx]), &(pms->samples [pms->samplecount * pms->channels]), count * sizeof (short)) ; | |||
327 | indx += count ; | |||
328 | pms->samplecount += count / pms->channels ; | |||
329 | total = indx ; | |||
330 | } ; | |||
331 | ||||
332 | return total ; | |||
333 | } /* msadpcm_read_block */ | |||
334 | ||||
335 | static sf_count_t | |||
336 | msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) | |||
337 | { MSADPCM_PRIVATE *pms ; | |||
338 | int readcount, count ; | |||
339 | sf_count_t total = 0 ; | |||
340 | ||||
341 | if (! psf->codec_data) | |||
342 | return 0 ; | |||
343 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
344 | ||||
345 | while (len > 0) | |||
346 | { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ; | |||
347 | ||||
348 | count = msadpcm_read_block (psf, pms, ptr, readcount) ; | |||
349 | ||||
350 | total += count ; | |||
351 | len -= count ; | |||
352 | if (count != readcount) | |||
353 | break ; | |||
354 | } ; | |||
355 | ||||
356 | return total ; | |||
357 | } /* msadpcm_read_s */ | |||
358 | ||||
359 | static sf_count_t | |||
360 | msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) | |||
361 | { MSADPCM_PRIVATE *pms ; | |||
362 | BUF_UNION ubuf ; | |||
363 | short *sptr ; | |||
364 | int k, bufferlen, readcount = 0, count ; | |||
365 | sf_count_t total = 0 ; | |||
366 | ||||
367 | if (! psf->codec_data) | |||
368 | return 0 ; | |||
369 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
370 | ||||
371 | sptr = ubuf.sbuf ; | |||
372 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
373 | while (len > 0) | |||
374 | { readcount = (len >= bufferlen) ? bufferlen : len ; | |||
375 | count = msadpcm_read_block (psf, pms, sptr, readcount) ; | |||
376 | for (k = 0 ; k < readcount ; k++) | |||
377 | ptr [total + k] = sptr [k] << 16 ; | |||
378 | total += count ; | |||
379 | len -= readcount ; | |||
380 | if (count != readcount) | |||
381 | break ; | |||
382 | } ; | |||
383 | return total ; | |||
384 | } /* msadpcm_read_i */ | |||
385 | ||||
386 | static sf_count_t | |||
387 | msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) | |||
388 | { MSADPCM_PRIVATE *pms ; | |||
389 | BUF_UNION ubuf ; | |||
390 | short *sptr ; | |||
391 | int k, bufferlen, readcount = 0, count ; | |||
392 | sf_count_t total = 0 ; | |||
393 | float normfact ; | |||
394 | ||||
395 | if (! psf->codec_data) | |||
396 | return 0 ; | |||
397 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
398 | ||||
399 | normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ; | |||
400 | sptr = ubuf.sbuf ; | |||
401 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
402 | while (len > 0) | |||
403 | { readcount = (len >= bufferlen) ? bufferlen : len ; | |||
404 | count = msadpcm_read_block (psf, pms, sptr, readcount) ; | |||
405 | for (k = 0 ; k < readcount ; k++) | |||
406 | ptr [total + k] = normfact * (float) (sptr [k]) ; | |||
407 | total += count ; | |||
408 | len -= readcount ; | |||
409 | if (count != readcount) | |||
410 | break ; | |||
411 | } ; | |||
412 | return total ; | |||
413 | } /* msadpcm_read_f */ | |||
414 | ||||
415 | static sf_count_t | |||
416 | msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) | |||
417 | { MSADPCM_PRIVATE *pms ; | |||
418 | BUF_UNION ubuf ; | |||
419 | short *sptr ; | |||
420 | int k, bufferlen, readcount = 0, count ; | |||
421 | sf_count_t total = 0 ; | |||
422 | double normfact ; | |||
423 | ||||
424 | normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ; | |||
425 | ||||
426 | if (! psf->codec_data) | |||
427 | return 0 ; | |||
428 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
429 | ||||
430 | sptr = ubuf.sbuf ; | |||
431 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
432 | while (len > 0) | |||
433 | { readcount = (len >= bufferlen) ? bufferlen : len ; | |||
434 | count = msadpcm_read_block (psf, pms, sptr, readcount) ; | |||
435 | for (k = 0 ; k < readcount ; k++) | |||
436 | ptr [total + k] = normfact * (double) (sptr [k]) ; | |||
437 | total += count ; | |||
438 | len -= readcount ; | |||
439 | if (count != readcount) | |||
440 | break ; | |||
441 | } ; | |||
442 | return total ; | |||
443 | } /* msadpcm_read_d */ | |||
444 | ||||
445 | static sf_count_t | |||
446 | msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) | |||
447 | { MSADPCM_PRIVATE *pms ; | |||
448 | int newblock, newsample ; | |||
449 | ||||
450 | if (! psf->codec_data) | |||
451 | return 0 ; | |||
452 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
453 | ||||
454 | if (psf->datalength < 0 || psf->dataoffset < 0) | |||
455 | { psf->error = SFE_BAD_SEEK ; | |||
456 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||
457 | } ; | |||
458 | ||||
459 | if (offset == 0) | |||
460 | { psf_fseek (psf, psf->dataoffset, SEEK_SET0) ; | |||
461 | pms->blockcount = 0 ; | |||
462 | msadpcm_decode_block (psf, pms) ; | |||
463 | pms->samplecount = 0 ; | |||
464 | return 0 ; | |||
465 | } ; | |||
466 | ||||
467 | if (offset < 0 || offset > pms->blocks * pms->samplesperblock) | |||
468 | { psf->error = SFE_BAD_SEEK ; | |||
469 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||
470 | } ; | |||
471 | ||||
472 | newblock = offset / pms->samplesperblock ; | |||
473 | newsample = offset % pms->samplesperblock ; | |||
474 | ||||
475 | if (mode == SFM_READ) | |||
476 | { psf_fseek (psf, psf->dataoffset + newblock * pms->blocksize, SEEK_SET0) ; | |||
477 | pms->blockcount = newblock ; | |||
478 | msadpcm_decode_block (psf, pms) ; | |||
479 | pms->samplecount = newsample ; | |||
480 | } | |||
481 | else | |||
482 | { /* What to do about write??? */ | |||
483 | psf->error = SFE_BAD_SEEK ; | |||
484 | return PSF_SEEK_ERROR((sf_count_t) -1) ; | |||
485 | } ; | |||
486 | ||||
487 | return newblock * pms->samplesperblock + newsample ; | |||
488 | } /* msadpcm_seek */ | |||
489 | ||||
490 | /*========================================================================================== | |||
491 | ** MS ADPCM Write Functions. | |||
492 | */ | |||
493 | ||||
494 | void | |||
495 | msadpcm_write_adapt_coeffs (SF_PRIVATE *psf) | |||
496 | { int k ; | |||
497 | ||||
498 | for (k = 0 ; k < MSADPCM_ADAPT_COEFF_COUNT7 ; k++) | |||
499 | psf_binheader_writef (psf, "22", AdaptCoeff1 [k], AdaptCoeff2 [k]) ; | |||
500 | } /* msadpcm_write_adapt_coeffs */ | |||
501 | ||||
502 | /*========================================================================================== | |||
503 | */ | |||
504 | ||||
505 | static int | |||
506 | msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) | |||
507 | { unsigned int blockindx ; | |||
508 | unsigned char byte ; | |||
509 | int chan, k, predict, bpred [2], idelta [2], errordelta, newsamp ; | |||
510 | ||||
511 | choose_predictor (pms->channels, pms->samples, bpred, idelta) ; | |||
512 | ||||
513 | /* Write the block header. */ | |||
514 | ||||
515 | if (pms->channels == 1) | |||
516 | { pms->block [0] = bpred [0] ; | |||
517 | pms->block [1] = idelta [0] & 0xFF ; | |||
518 | pms->block [2] = idelta [0] >> 8 ; | |||
519 | pms->block [3] = pms->samples [1] & 0xFF ; | |||
520 | pms->block [4] = pms->samples [1] >> 8 ; | |||
521 | pms->block [5] = pms->samples [0] & 0xFF ; | |||
522 | pms->block [6] = pms->samples [0] >> 8 ; | |||
523 | ||||
524 | blockindx = 7 ; | |||
525 | byte = 0 ; | |||
526 | ||||
527 | /* Encode the samples as 4 bit. */ | |||
528 | ||||
529 | for (k = 2 ; k < pms->samplesperblock ; k++) | |||
530 | { predict = (pms->samples [k-1] * AdaptCoeff1 [bpred [0]] + pms->samples [k-2] * AdaptCoeff2 [bpred [0]]) >> 8 ; | |||
531 | errordelta = (pms->samples [k] - predict) / idelta [0] ; | |||
532 | if (errordelta < -8) | |||
533 | errordelta = -8 ; | |||
534 | else if (errordelta > 7) | |||
535 | errordelta = 7 ; | |||
536 | newsamp = predict + (idelta [0] * errordelta) ; | |||
537 | if (newsamp > 32767) | |||
538 | newsamp = 32767 ; | |||
539 | else if (newsamp < -32768) | |||
540 | newsamp = -32768 ; | |||
541 | if (errordelta < 0) | |||
542 | errordelta += 0x10 ; | |||
543 | ||||
544 | byte = (byte << 4) | (errordelta & 0xF) ; | |||
545 | if (k % 2) | |||
546 | { pms->block [blockindx++] = byte ; | |||
547 | byte = 0 ; | |||
548 | } ; | |||
549 | ||||
550 | idelta [0] = (idelta [0] * AdaptationTable [errordelta]) >> 8 ; | |||
551 | if (idelta [0] < 16) | |||
552 | idelta [0] = 16 ; | |||
553 | pms->samples [k] = newsamp ; | |||
554 | } ; | |||
555 | } | |||
556 | else | |||
557 | { /* Stereo file. */ | |||
558 | pms->block [0] = bpred [0] ; | |||
| ||||
559 | pms->block [1] = bpred [1] ; | |||
560 | ||||
561 | pms->block [2] = idelta [0] & 0xFF ; | |||
562 | pms->block [3] = idelta [0] >> 8 ; | |||
563 | pms->block [4] = idelta [1] & 0xFF ; | |||
564 | pms->block [5] = idelta [1] >> 8 ; | |||
565 | ||||
566 | pms->block [6] = pms->samples [2] & 0xFF ; | |||
567 | pms->block [7] = pms->samples [2] >> 8 ; | |||
568 | pms->block [8] = pms->samples [3] & 0xFF ; | |||
569 | pms->block [9] = pms->samples [3] >> 8 ; | |||
570 | ||||
571 | pms->block [10] = pms->samples [0] & 0xFF ; | |||
572 | pms->block [11] = pms->samples [0] >> 8 ; | |||
573 | pms->block [12] = pms->samples [1] & 0xFF ; | |||
574 | pms->block [13] = pms->samples [1] >> 8 ; | |||
575 | ||||
576 | blockindx = 14 ; | |||
577 | byte = 0 ; | |||
578 | chan = 1 ; | |||
579 | ||||
580 | for (k = 4 ; k < 2 * pms->samplesperblock ; k++) | |||
581 | { chan = k & 1 ; | |||
582 | ||||
583 | predict = (pms->samples [k-2] * AdaptCoeff1 [bpred [chan]] + pms->samples [k-4] * AdaptCoeff2 [bpred [chan]]) >> 8 ; | |||
584 | errordelta = (pms->samples [k] - predict) / idelta [chan] ; | |||
585 | ||||
586 | ||||
587 | if (errordelta < -8) | |||
588 | errordelta = -8 ; | |||
589 | else if (errordelta > 7) | |||
590 | errordelta = 7 ; | |||
591 | newsamp = predict + (idelta [chan] * errordelta) ; | |||
592 | if (newsamp > 32767) | |||
593 | newsamp = 32767 ; | |||
594 | else if (newsamp < -32768) | |||
595 | newsamp = -32768 ; | |||
596 | if (errordelta < 0) | |||
597 | errordelta += 0x10 ; | |||
598 | ||||
599 | byte = (byte << 4) | (errordelta & 0xF) ; | |||
600 | ||||
601 | if (chan) | |||
602 | { pms->block [blockindx++] = byte ; | |||
603 | byte = 0 ; | |||
604 | } ; | |||
605 | ||||
606 | idelta [chan] = (idelta [chan] * AdaptationTable [errordelta]) >> 8 ; | |||
607 | if (idelta [chan] < 16) | |||
608 | idelta [chan] = 16 ; | |||
609 | pms->samples [k] = newsamp ; | |||
610 | } ; | |||
611 | } ; | |||
612 | ||||
613 | /* Write the block to disk. */ | |||
614 | ||||
615 | if ((k = psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize) | |||
616 | psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pms->blocksize) ; | |||
617 | ||||
618 | memset (pms->samples, 0, pms->samplesperblock * sizeof (short)) ; | |||
619 | ||||
620 | pms->blockcount ++ ; | |||
621 | pms->samplecount = 0 ; | |||
622 | ||||
623 | return 1 ; | |||
624 | } /* msadpcm_encode_block */ | |||
625 | ||||
626 | static sf_count_t | |||
627 | msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, const short *ptr, int len) | |||
628 | { int count, total = 0, indx = 0 ; | |||
629 | ||||
630 | while (indx < len) | |||
631 | { count = (pms->samplesperblock - pms->samplecount) * pms->channels ; | |||
632 | ||||
633 | if (count > len - indx) | |||
634 | count = len - indx ; | |||
635 | ||||
636 | memcpy (&(pms->samples [pms->samplecount * pms->channels]), &(ptr [total]), count * sizeof (short)) ; | |||
637 | indx += count ; | |||
638 | pms->samplecount += count / pms->channels ; | |||
639 | total = indx ; | |||
640 | ||||
641 | if (pms->samplecount >= pms->samplesperblock) | |||
642 | msadpcm_encode_block (psf, pms) ; | |||
643 | } ; | |||
644 | ||||
645 | return total ; | |||
646 | } /* msadpcm_write_block */ | |||
647 | ||||
648 | static sf_count_t | |||
649 | msadpcm_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) | |||
650 | { MSADPCM_PRIVATE *pms ; | |||
651 | int writecount, count ; | |||
652 | sf_count_t total = 0 ; | |||
653 | ||||
654 | if (! psf->codec_data) | |||
655 | return 0 ; | |||
656 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
657 | ||||
658 | while (len > 0) | |||
659 | { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; | |||
660 | ||||
661 | count = msadpcm_write_block (psf, pms, ptr, writecount) ; | |||
662 | ||||
663 | total += count ; | |||
664 | len -= count ; | |||
665 | if (count != writecount) | |||
666 | break ; | |||
667 | } ; | |||
668 | ||||
669 | return total ; | |||
670 | } /* msadpcm_write_s */ | |||
671 | ||||
672 | static sf_count_t | |||
673 | msadpcm_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) | |||
674 | { MSADPCM_PRIVATE *pms ; | |||
675 | BUF_UNION ubuf ; | |||
676 | short *sptr ; | |||
677 | int k, bufferlen, writecount, count ; | |||
678 | sf_count_t total = 0 ; | |||
679 | ||||
680 | if (! psf->codec_data) | |||
681 | return 0 ; | |||
682 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
683 | ||||
684 | sptr = ubuf.sbuf ; | |||
685 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
686 | while (len > 0) | |||
687 | { writecount = (len >= bufferlen) ? bufferlen : len ; | |||
688 | for (k = 0 ; k < writecount ; k++) | |||
689 | sptr [k] = ptr [total + k] >> 16 ; | |||
690 | count = msadpcm_write_block (psf, pms, sptr, writecount) ; | |||
691 | total += count ; | |||
692 | len -= writecount ; | |||
693 | if (count != writecount) | |||
694 | break ; | |||
695 | } ; | |||
696 | return total ; | |||
697 | } /* msadpcm_write_i */ | |||
698 | ||||
699 | static sf_count_t | |||
700 | msadpcm_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) | |||
701 | { MSADPCM_PRIVATE *pms ; | |||
702 | BUF_UNION ubuf ; | |||
703 | short *sptr ; | |||
704 | int k, bufferlen, writecount, count ; | |||
705 | sf_count_t total = 0 ; | |||
706 | float normfact ; | |||
707 | ||||
708 | if (! psf->codec_data) | |||
709 | return 0 ; | |||
710 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
711 | ||||
712 | normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; | |||
713 | ||||
714 | sptr = ubuf.sbuf ; | |||
715 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
716 | while (len > 0) | |||
717 | { writecount = (len >= bufferlen) ? bufferlen : len ; | |||
718 | for (k = 0 ; k < writecount ; k++) | |||
719 | sptr [k] = lrintf (normfact * ptr [total + k]) ; | |||
720 | count = msadpcm_write_block (psf, pms, sptr, writecount) ; | |||
721 | total += count ; | |||
722 | len -= writecount ; | |||
723 | if (count != writecount) | |||
724 | break ; | |||
725 | } ; | |||
726 | return total ; | |||
727 | } /* msadpcm_write_f */ | |||
728 | ||||
729 | static sf_count_t | |||
730 | msadpcm_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) | |||
731 | { MSADPCM_PRIVATE *pms ; | |||
732 | BUF_UNION ubuf ; | |||
733 | short *sptr ; | |||
734 | int k, bufferlen, writecount, count ; | |||
735 | sf_count_t total = 0 ; | |||
736 | double normfact ; | |||
737 | ||||
738 | normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; | |||
739 | ||||
740 | if (! psf->codec_data) | |||
741 | return 0 ; | |||
742 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
743 | ||||
744 | sptr = ubuf.sbuf ; | |||
745 | bufferlen = ARRAY_LEN (ubuf.sbuf)((int) (sizeof (ubuf.sbuf) / sizeof ((ubuf.sbuf) [0]))) ; | |||
746 | while (len > 0) | |||
747 | { writecount = (len >= bufferlen) ? bufferlen : len ; | |||
748 | for (k = 0 ; k < writecount ; k++) | |||
749 | sptr [k] = lrint (normfact * ptr [total + k]) ; | |||
750 | count = msadpcm_write_block (psf, pms, sptr, writecount) ; | |||
751 | total += count ; | |||
752 | len -= writecount ; | |||
753 | if (count != writecount) | |||
754 | break ; | |||
755 | } ; | |||
756 | return total ; | |||
757 | } /* msadpcm_write_d */ | |||
758 | ||||
759 | /*======================================================================================== | |||
760 | */ | |||
761 | ||||
762 | static int | |||
763 | msadpcm_close (SF_PRIVATE *psf) | |||
764 | { MSADPCM_PRIVATE *pms ; | |||
765 | ||||
766 | pms = (MSADPCM_PRIVATE*) psf->codec_data ; | |||
767 | ||||
768 | if (psf->file.mode == SFM_WRITE) | |||
| ||||
769 | { /* Now we know static int for certain the length of the file we can | |||
770 | ** re-write the header. | |||
771 | */ | |||
772 | ||||
773 | if (pms->samplecount && pms->samplecount < pms->samplesperblock) | |||
774 | msadpcm_encode_block (psf, pms) ; | |||
775 | } ; | |||
776 | ||||
777 | return 0 ; | |||
778 | } /* msadpcm_close */ | |||
779 | ||||
780 | /*======================================================================================== | |||
781 | ** Static functions. | |||
782 | */ | |||
783 | ||||
784 | /*---------------------------------------------------------------------------------------- | |||
785 | ** Choosing the block predictor. | |||
786 | ** Each block requires a predictor and an idelta for each channel. | |||
787 | ** The predictor is in the range [0..6] which is an indx into the two AdaptCoeff tables. | |||
788 | ** The predictor is chosen by trying all of the possible predictors on a small set of | |||
789 | ** samples at the beginning of the block. The predictor with the smallest average | |||
790 | ** abs (idelta) is chosen as the best predictor for this block. | |||
791 | ** The value of idelta is chosen to to give a 4 bit code value of +/- 4 (approx. half the | |||
792 | ** max. code value). If the average abs (idelta) is zero, the sixth predictor is chosen. | |||
793 | ** If the value of idelta is less then 16 it is set to 16. | |||
794 | ** | |||
795 | ** Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose best predictor) | |||
796 | ** value of 3. The best possible results would be obtained by using all the samples to | |||
797 | ** choose the predictor. | |||
798 | */ | |||
799 | ||||
800 | #define IDELTA_COUNT3 3 | |||
801 | ||||
802 | static void | |||
803 | choose_predictor (unsigned int channels, short *data, int *block_pred, int *idelta) | |||
804 | { unsigned int chan, k, bpred, idelta_sum, best_bpred, best_idelta ; | |||
805 | ||||
806 | for (chan = 0 ; chan < channels ; chan++) | |||
807 | { best_bpred = best_idelta = 0 ; | |||
808 | ||||
809 | for (bpred = 0 ; bpred < 7 ; bpred++) | |||
810 | { idelta_sum = 0 ; | |||
811 | for (k = 2 ; k < 2 + IDELTA_COUNT3 ; k++) | |||
812 | idelta_sum += abs (data [k * channels] - ((data [(k - 1) * channels] * AdaptCoeff1 [bpred] + data [(k - 2) * channels] * AdaptCoeff2 [bpred]) >> 8)) ; | |||
813 | idelta_sum /= (4 * IDELTA_COUNT3) ; | |||
814 | ||||
815 | if (bpred == 0 || idelta_sum < best_idelta) | |||
816 | { best_bpred = bpred ; | |||
817 | best_idelta = idelta_sum ; | |||
818 | } ; | |||
819 | ||||
820 | if (! idelta_sum) | |||
821 | { best_bpred = bpred ; | |||
822 | best_idelta = 16 ; | |||
823 | break ; | |||
824 | } ; | |||
825 | ||||
826 | } ; /* for bpred ... */ | |||
827 | if (best_idelta < 16) | |||
828 | best_idelta = 16 ; | |||
829 | ||||
830 | block_pred [chan] = best_bpred ; | |||
831 | idelta [chan] = best_idelta ; | |||
832 | } ; | |||
833 | ||||
834 | return ; | |||
835 | } /* choose_predictor */ | |||
836 |