/src/libsndfile/src/dwvw.c
Line | Count | Source |
1 | | /* |
2 | | ** Copyright (C) 2002-2014 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 | | /*=========================================================================== |
20 | | ** Delta Word Variable Width |
21 | | ** |
22 | | ** This decoder and encoder were implemented using information found in this |
23 | | ** document : http://home.swbell.net/rubywand/R011SNDFMTS.TXT |
24 | | ** |
25 | | ** According to the document, the algorithm "was invented 1991 by Magnus |
26 | | ** Lidstrom and is copyright 1993 by NuEdge Development". |
27 | | */ |
28 | | |
29 | | #include "sfconfig.h" |
30 | | |
31 | | #include <stdio.h> |
32 | | #include <stdlib.h> |
33 | | #include <string.h> |
34 | | #include <math.h> |
35 | | |
36 | | #include "sndfile.h" |
37 | | #include "sfendian.h" |
38 | | #include "common.h" |
39 | | |
40 | | typedef struct |
41 | | { int bit_width, dwm_maxsize, max_delta, span ; |
42 | | int samplecount ; |
43 | | int bit_count, bits, last_delta_width, last_sample ; |
44 | | struct |
45 | | { int index, end ; |
46 | | unsigned char buffer [256] ; |
47 | | } b ; |
48 | | } DWVW_PRIVATE ; |
49 | | |
50 | | /*============================================================================================ |
51 | | */ |
52 | | |
53 | | static sf_count_t dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; |
54 | | static sf_count_t dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ; |
55 | | static sf_count_t dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ; |
56 | | static sf_count_t dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ; |
57 | | |
58 | | static sf_count_t dwvw_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; |
59 | | static sf_count_t dwvw_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; |
60 | | static sf_count_t dwvw_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; |
61 | | static sf_count_t dwvw_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; |
62 | | |
63 | | static sf_count_t dwvw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; |
64 | | static int dwvw_close (SF_PRIVATE *psf) ; |
65 | | static int dwvw_byterate (SF_PRIVATE *psf) ; |
66 | | |
67 | | static int dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ; |
68 | | static int dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) ; |
69 | | |
70 | | static int dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, const int *ptr, int len) ; |
71 | | static void dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits) ; |
72 | | static void dwvw_read_reset (DWVW_PRIVATE *pdwvw) ; |
73 | | |
74 | | /*============================================================================================ |
75 | | ** DWVW initialisation function. |
76 | | */ |
77 | | |
78 | | int |
79 | | dwvw_init (SF_PRIVATE *psf, int bitwidth) |
80 | 453 | { DWVW_PRIVATE *pdwvw ; |
81 | | |
82 | 453 | if (psf->codec_data != NULL) |
83 | 0 | { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ; |
84 | 0 | return SFE_INTERNAL ; |
85 | 453 | } ; |
86 | | |
87 | 453 | if (bitwidth > 24) |
88 | 0 | return SFE_DWVW_BAD_BITWIDTH ; |
89 | | |
90 | 453 | if (psf->file.mode == SFM_RDWR) |
91 | 0 | return SFE_BAD_MODE_RW ; |
92 | | |
93 | 453 | if ((pdwvw = calloc (1, sizeof (DWVW_PRIVATE))) == NULL) |
94 | 0 | return SFE_MALLOC_FAILED ; |
95 | | |
96 | 453 | psf->codec_data = (void*) pdwvw ; |
97 | 453 | pdwvw->bit_width = bitwidth ; |
98 | 453 | dwvw_read_reset (pdwvw) ; |
99 | | |
100 | 453 | if (psf->file.mode == SFM_READ) |
101 | 453 | { psf->read_short = dwvw_read_s ; |
102 | 453 | psf->read_int = dwvw_read_i ; |
103 | 453 | psf->read_float = dwvw_read_f ; |
104 | 453 | psf->read_double = dwvw_read_d ; |
105 | 453 | } ; |
106 | | |
107 | 453 | if (psf->file.mode == SFM_WRITE) |
108 | 0 | { psf->write_short = dwvw_write_s ; |
109 | 0 | psf->write_int = dwvw_write_i ; |
110 | 0 | psf->write_float = dwvw_write_f ; |
111 | 0 | psf->write_double = dwvw_write_d ; |
112 | 0 | } ; |
113 | | |
114 | 453 | psf->codec_close = dwvw_close ; |
115 | 453 | psf->seek = dwvw_seek ; |
116 | 453 | psf->byterate = dwvw_byterate ; |
117 | | |
118 | 453 | if (psf->file.mode == SFM_READ) |
119 | 453 | { psf->sf.frames = psf_decode_frame_count (psf) ; |
120 | 453 | dwvw_read_reset (pdwvw) ; |
121 | 453 | } ; |
122 | | |
123 | 453 | return 0 ; |
124 | 453 | } /* dwvw_init */ |
125 | | |
126 | | /*-------------------------------------------------------------------------------------------- |
127 | | */ |
128 | | |
129 | | static int |
130 | | dwvw_close (SF_PRIVATE *psf) |
131 | 453 | { DWVW_PRIVATE *pdwvw ; |
132 | | |
133 | 453 | if (psf->codec_data == NULL) |
134 | 0 | return 0 ; |
135 | 453 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
136 | | |
137 | 453 | if (psf->file.mode == SFM_WRITE) |
138 | 0 | { static int last_values [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ; |
139 | | |
140 | | /* Write 8 zero samples to fully flush output. */ |
141 | 0 | dwvw_encode_data (psf, pdwvw, last_values, 12) ; |
142 | | |
143 | | /* Write the last buffer worth of data to disk. */ |
144 | 0 | psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ; |
145 | |
|
146 | 0 | if (psf->write_header) |
147 | 0 | psf->write_header (psf, SF_TRUE) ; |
148 | 0 | } ; |
149 | | |
150 | 453 | return 0 ; |
151 | 453 | } /* dwvw_close */ |
152 | | |
153 | | static sf_count_t |
154 | | dwvw_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset) |
155 | 0 | { DWVW_PRIVATE *pdwvw ; |
156 | |
|
157 | 0 | if (! psf->codec_data) |
158 | 0 | { psf->error = SFE_INTERNAL ; |
159 | 0 | return PSF_SEEK_ERROR ; |
160 | 0 | } ; |
161 | |
|
162 | 0 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
163 | |
|
164 | 0 | if (offset == 0) |
165 | 0 | { psf_fseek (psf, psf->dataoffset, SEEK_SET) ; |
166 | 0 | dwvw_read_reset (pdwvw) ; |
167 | 0 | return 0 ; |
168 | 0 | } ; |
169 | |
|
170 | 0 | psf->error = SFE_BAD_SEEK ; |
171 | 0 | return PSF_SEEK_ERROR ; |
172 | 0 | } /* dwvw_seek */ |
173 | | |
174 | | static int |
175 | | dwvw_byterate (SF_PRIVATE *psf) |
176 | 0 | { |
177 | 0 | if (psf->file.mode == SFM_READ) |
178 | 0 | return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ; |
179 | | |
180 | 0 | return -1 ; |
181 | 0 | } /* dwvw_byterate */ |
182 | | |
183 | | /*============================================================================== |
184 | | */ |
185 | | |
186 | | static sf_count_t |
187 | | dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) |
188 | 173 | { DWVW_PRIVATE *pdwvw ; |
189 | 173 | BUF_UNION ubuf ; |
190 | 173 | int *iptr ; |
191 | 173 | int k, bufferlen, readcount = 0, count ; |
192 | 173 | sf_count_t total = 0 ; |
193 | | |
194 | 173 | if (! psf->codec_data) |
195 | 0 | return 0 ; |
196 | 173 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
197 | | |
198 | 173 | iptr = ubuf.ibuf ; |
199 | 173 | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
200 | 817 | while (len > 0) |
201 | 704 | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
202 | 704 | count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; |
203 | 1.25M | for (k = 0 ; k < readcount ; k++) |
204 | 1.25M | ptr [total + k] = iptr [k] >> 16 ; |
205 | | |
206 | 704 | total += count ; |
207 | 704 | len -= readcount ; |
208 | 704 | if (count != readcount) |
209 | 60 | break ; |
210 | 704 | } ; |
211 | | |
212 | 173 | return total ; |
213 | 173 | } /* dwvw_read_s */ |
214 | | |
215 | | static sf_count_t |
216 | | dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) |
217 | 8.35k | { DWVW_PRIVATE *pdwvw ; |
218 | 8.35k | int readcount, count ; |
219 | 8.35k | sf_count_t total = 0 ; |
220 | | |
221 | 8.35k | if (! psf->codec_data) |
222 | 0 | return 0 ; |
223 | 8.35k | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
224 | | |
225 | 15.9k | while (len > 0) |
226 | 8.35k | { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ; |
227 | | |
228 | 8.35k | count = dwvw_decode_data (psf, pdwvw, ptr, readcount) ; |
229 | | |
230 | 8.35k | total += count ; |
231 | 8.35k | len -= count ; |
232 | | |
233 | 8.35k | if (count != readcount) |
234 | 781 | break ; |
235 | 8.35k | } ; |
236 | | |
237 | 8.35k | return total ; |
238 | 8.35k | } /* dwvw_read_i */ |
239 | | |
240 | | static sf_count_t |
241 | | dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) |
242 | 3.59k | { DWVW_PRIVATE *pdwvw ; |
243 | 3.59k | BUF_UNION ubuf ; |
244 | 3.59k | int *iptr ; |
245 | 3.59k | int k, bufferlen, readcount = 0, count ; |
246 | 3.59k | sf_count_t total = 0 ; |
247 | 3.59k | float normfact ; |
248 | | |
249 | 3.59k | if (! psf->codec_data) |
250 | 0 | return 0 ; |
251 | 3.59k | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
252 | | |
253 | 3.59k | normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ; |
254 | | |
255 | 3.59k | iptr = ubuf.ibuf ; |
256 | 3.59k | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
257 | 7.17k | while (len > 0) |
258 | 3.59k | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
259 | 3.59k | count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; |
260 | 410k | for (k = 0 ; k < readcount ; k++) |
261 | 407k | ptr [total + k] = normfact * (float) (iptr [k]) ; |
262 | | |
263 | 3.59k | total += count ; |
264 | 3.59k | len -= readcount ; |
265 | 3.59k | if (count != readcount) |
266 | 10 | break ; |
267 | 3.59k | } ; |
268 | | |
269 | 3.59k | return total ; |
270 | 3.59k | } /* dwvw_read_f */ |
271 | | |
272 | | static sf_count_t |
273 | | dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) |
274 | 214 | { DWVW_PRIVATE *pdwvw ; |
275 | 214 | BUF_UNION ubuf ; |
276 | 214 | int *iptr ; |
277 | 214 | int k, bufferlen, readcount = 0, count ; |
278 | 214 | sf_count_t total = 0 ; |
279 | 214 | double normfact ; |
280 | | |
281 | 214 | if (! psf->codec_data) |
282 | 0 | return 0 ; |
283 | 214 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
284 | | |
285 | 214 | normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ; |
286 | | |
287 | 214 | iptr = ubuf.ibuf ; |
288 | 214 | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
289 | 956 | while (len > 0) |
290 | 795 | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
291 | 795 | count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ; |
292 | 1.32M | for (k = 0 ; k < readcount ; k++) |
293 | 1.31M | ptr [total + k] = normfact * (double) (iptr [k]) ; |
294 | | |
295 | 795 | total += count ; |
296 | 795 | len -= readcount ; |
297 | 795 | if (count != readcount) |
298 | 53 | break ; |
299 | 795 | } ; |
300 | | |
301 | 214 | return total ; |
302 | 214 | } /* dwvw_read_d */ |
303 | | |
304 | | static int |
305 | | dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) |
306 | 13.4k | { int count ; |
307 | 13.4k | int delta_width_modifier, delta_width, delta_negative, delta, sample ; |
308 | | |
309 | | /* Restore state from last decode call. */ |
310 | 13.4k | delta_width = pdwvw->last_delta_width ; |
311 | 13.4k | sample = pdwvw->last_sample ; |
312 | | |
313 | 21.0M | for (count = 0 ; count < len ; count++) |
314 | 21.0M | { /* If bit_count parameter is zero get the delta_width_modifier. */ |
315 | 21.0M | delta_width_modifier = dwvw_decode_load_bits (psf, pdwvw, -1) ; |
316 | | |
317 | | /* Check for end of input bit stream. Break loop if end. */ |
318 | 21.0M | if (delta_width_modifier < 0 || (pdwvw->b.end == 0 && count == 0)) |
319 | 602 | break ; |
320 | | |
321 | 21.0M | if (delta_width_modifier && dwvw_decode_load_bits (psf, pdwvw, 1)) |
322 | 843k | delta_width_modifier = - delta_width_modifier ; |
323 | | |
324 | | /* Calculate the current word width. */ |
325 | 21.0M | delta_width = (delta_width + delta_width_modifier + pdwvw->bit_width) % pdwvw->bit_width ; |
326 | | |
327 | | /* Load the delta. */ |
328 | 21.0M | delta = 0 ; |
329 | 21.0M | if (delta_width) |
330 | 8.10M | { delta = dwvw_decode_load_bits (psf, pdwvw, delta_width - 1) | (1 << (delta_width - 1)) ; |
331 | 8.10M | delta_negative = dwvw_decode_load_bits (psf, pdwvw, 1) ; |
332 | 8.10M | if (delta == pdwvw->max_delta - 1) |
333 | 230k | delta += dwvw_decode_load_bits (psf, pdwvw, 1) ; |
334 | 8.10M | if (delta_negative) |
335 | 4.24M | delta = -delta ; |
336 | 8.10M | } ; |
337 | | |
338 | | /* Calculate the sample */ |
339 | 21.0M | sample += delta ; |
340 | | |
341 | 21.0M | if (sample >= pdwvw->max_delta) |
342 | 274k | sample -= pdwvw->span ; |
343 | 20.7M | else if (sample < - pdwvw->max_delta) |
344 | 305k | sample += pdwvw->span ; |
345 | | |
346 | | /* Store the sample justifying to the most significant bit. */ |
347 | 21.0M | ptr [count] = arith_shift_left (sample, 32 - pdwvw->bit_width) ; |
348 | | |
349 | 21.0M | if (pdwvw->b.end == 0 && pdwvw->bit_count == 0) |
350 | 302 | break ; |
351 | 21.0M | } ; |
352 | | |
353 | 13.4k | pdwvw->last_delta_width = delta_width ; |
354 | 13.4k | pdwvw->last_sample = sample ; |
355 | | |
356 | 13.4k | pdwvw->samplecount += count ; |
357 | | |
358 | 13.4k | return count ; |
359 | 13.4k | } /* dwvw_decode_data */ |
360 | | |
361 | | static int |
362 | | dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) |
363 | 41.6M | { int output = 0, get_dwm = SF_FALSE ; |
364 | | |
365 | | /* |
366 | | ** Depending on the value of parameter bit_count, either get the |
367 | | ** required number of bits (ie bit_count > 0) or the |
368 | | ** delta_width_modifier (otherwise). |
369 | | */ |
370 | | |
371 | 41.6M | if (bit_count < 0) |
372 | 21.0M | { get_dwm = SF_TRUE ; |
373 | | /* modify bit_count to ensure we have enough bits for finding dwm. */ |
374 | 21.0M | bit_count = pdwvw->dwm_maxsize ; |
375 | 21.0M | } ; |
376 | | |
377 | | /* Load bits in bit reseviour. */ |
378 | 50.6M | while (pdwvw->bit_count < bit_count) |
379 | 9.02M | { if (pdwvw->b.index >= pdwvw->b.end) |
380 | 478k | { pdwvw->b.end = (int) psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ; |
381 | 478k | pdwvw->b.index = 0 ; |
382 | 478k | } ; |
383 | | |
384 | | /* Check for end of input stream. */ |
385 | 9.02M | if (bit_count < 8 && pdwvw->b.end == 0) |
386 | 52.9k | return -1 ; |
387 | | |
388 | 8.97M | pdwvw->bits = arith_shift_left (pdwvw->bits, 8) ; |
389 | | |
390 | 8.97M | if (pdwvw->b.index < pdwvw->b.end) |
391 | 8.58M | { pdwvw->bits |= pdwvw->b.buffer [pdwvw->b.index] ; |
392 | 8.58M | pdwvw->b.index ++ ; |
393 | 8.58M | } ; |
394 | 8.97M | pdwvw->bit_count += 8 ; |
395 | 41.5M | } ; |
396 | | |
397 | | /* If asked to get bits do so. */ |
398 | 41.5M | if (! get_dwm) |
399 | 20.5M | { output = (pdwvw->bits >> (pdwvw->bit_count - bit_count)) & ((1 << bit_count) - 1) ; |
400 | 20.5M | pdwvw->bit_count -= bit_count ; |
401 | 20.5M | return output ; |
402 | 21.0M | } ; |
403 | | |
404 | | /* Otherwise must have been asked to get delta_width_modifier. */ |
405 | 35.5M | while (output < (pdwvw->dwm_maxsize)) |
406 | 32.8M | { pdwvw->bit_count -= 1 ; |
407 | 32.8M | if (pdwvw->bits & (1 << pdwvw->bit_count)) |
408 | 18.2M | break ; |
409 | 14.5M | output += 1 ; |
410 | 14.5M | } ; |
411 | | |
412 | 21.0M | return output ; |
413 | 41.5M | } /* dwvw_decode_load_bits */ |
414 | | |
415 | | static void |
416 | | dwvw_read_reset (DWVW_PRIVATE *pdwvw) |
417 | 906 | { int bitwidth = pdwvw->bit_width ; |
418 | | |
419 | 906 | memset (pdwvw, 0, sizeof (DWVW_PRIVATE)) ; |
420 | | |
421 | 906 | pdwvw->bit_width = bitwidth ; |
422 | 906 | pdwvw->dwm_maxsize = bitwidth / 2 ; |
423 | 906 | pdwvw->max_delta = 1 << (bitwidth - 1) ; |
424 | 906 | pdwvw->span = 1 << bitwidth ; |
425 | 906 | } /* dwvw_read_reset */ |
426 | | |
427 | | static void |
428 | | dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits) |
429 | 0 | { int byte ; |
430 | | |
431 | | /* Shift the bits into the reservoir. */ |
432 | 0 | pdwvw->bits = arith_shift_left (pdwvw->bits, new_bits) | (data & (arith_shift_left (1, new_bits) - 1)) ; |
433 | 0 | pdwvw->bit_count += new_bits ; |
434 | | |
435 | | /* Transfer bit to buffer. */ |
436 | 0 | while (pdwvw->bit_count >= 8) |
437 | 0 | { byte = pdwvw->bits >> (pdwvw->bit_count - 8) ; |
438 | 0 | pdwvw->bit_count -= 8 ; |
439 | 0 | pdwvw->b.buffer [pdwvw->b.index] = byte & 0xFF ; |
440 | 0 | pdwvw->b.index ++ ; |
441 | 0 | } ; |
442 | |
|
443 | 0 | if (pdwvw->b.index > SIGNED_SIZEOF (pdwvw->b.buffer) - 4) |
444 | 0 | { psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ; |
445 | 0 | pdwvw->b.index = 0 ; |
446 | 0 | } ; |
447 | |
|
448 | 0 | return ; |
449 | 0 | } /* dwvw_encode_store_bits */ |
450 | | |
451 | | #if 0 |
452 | | /* Debigging routine. */ |
453 | | static void |
454 | | dump_bits (DWVW_PRIVATE *pdwvw) |
455 | | { int k, mask ; |
456 | | |
457 | | for (k = 0 ; k < 10 && k < pdwvw->b.index ; k++) |
458 | | { mask = 0x80 ; |
459 | | while (mask) |
460 | | { putchar (mask & pdwvw->b.buffer [k] ? '1' : '0') ; |
461 | | mask >>= 1 ; |
462 | | } ; |
463 | | putchar (' ') ; |
464 | | } |
465 | | |
466 | | for (k = pdwvw->bit_count - 1 ; k >= 0 ; k --) |
467 | | putchar (pdwvw->bits & (1 << k) ? '1' : '0') ; |
468 | | |
469 | | putchar ('\n') ; |
470 | | } /* dump_bits */ |
471 | | #endif |
472 | | |
473 | | #define HIGHEST_BIT(x, count) \ |
474 | 0 | { int y = x ; \ |
475 | 0 | (count) = 0 ; \ |
476 | 0 | while (y) \ |
477 | 0 | { (count) ++ ; \ |
478 | 0 | y >>= 1 ; \ |
479 | 0 | } ; \ |
480 | 0 | } ; |
481 | | |
482 | | static int |
483 | | dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, const int *ptr, int len) |
484 | 0 | { int count ; |
485 | 0 | int delta_width_modifier, delta, delta_negative, delta_width, extra_bit ; |
486 | |
|
487 | 0 | for (count = 0 ; count < len ; count++) |
488 | 0 | { delta = (ptr [count] >> (32 - pdwvw->bit_width)) - pdwvw->last_sample ; |
489 | | |
490 | | /* Calculate extra_bit if needed. */ |
491 | 0 | extra_bit = -1 ; |
492 | 0 | delta_negative = 0 ; |
493 | 0 | if (delta < -pdwvw->max_delta) |
494 | 0 | delta = pdwvw->max_delta + (delta % pdwvw->max_delta) ; |
495 | 0 | else if (delta == -pdwvw->max_delta) |
496 | 0 | { extra_bit = 1 ; |
497 | 0 | delta_negative = 1 ; |
498 | 0 | delta = pdwvw->max_delta - 1 ; |
499 | 0 | } |
500 | 0 | else if (delta > pdwvw->max_delta) |
501 | 0 | { delta_negative = 1 ; |
502 | 0 | delta = pdwvw->span - delta ; |
503 | 0 | delta = abs (delta) ; |
504 | 0 | } |
505 | 0 | else if (delta == pdwvw->max_delta) |
506 | 0 | { extra_bit = 1 ; |
507 | 0 | delta = pdwvw->max_delta - 1 ; |
508 | 0 | } |
509 | 0 | else if (delta < 0) |
510 | 0 | { delta_negative = 1 ; |
511 | 0 | delta = abs (delta) ; |
512 | 0 | } ; |
513 | |
|
514 | 0 | if (delta == pdwvw->max_delta - 1 && extra_bit == -1) |
515 | 0 | extra_bit = 0 ; |
516 | | |
517 | | /* Find width in bits of delta */ |
518 | 0 | HIGHEST_BIT (delta, delta_width) ; |
519 | | |
520 | | /* Calculate the delta_width_modifier */ |
521 | 0 | delta_width_modifier = (delta_width - pdwvw->last_delta_width) % pdwvw->bit_width ; |
522 | 0 | if (delta_width_modifier > pdwvw->dwm_maxsize) |
523 | 0 | delta_width_modifier -= pdwvw->bit_width ; |
524 | 0 | if (delta_width_modifier < -pdwvw->dwm_maxsize) |
525 | 0 | delta_width_modifier += pdwvw->bit_width ; |
526 | | |
527 | | /* Write delta_width_modifier zeros, followed by terminating '1'. */ |
528 | 0 | dwvw_encode_store_bits (psf, pdwvw, 0, abs (delta_width_modifier)) ; |
529 | 0 | if (abs (delta_width_modifier) != pdwvw->dwm_maxsize) |
530 | 0 | dwvw_encode_store_bits (psf, pdwvw, 1, 1) ; |
531 | | |
532 | | /* Write delta_width_modifier sign. */ |
533 | 0 | if (delta_width_modifier < 0) |
534 | 0 | dwvw_encode_store_bits (psf, pdwvw, 1, 1) ; |
535 | 0 | if (delta_width_modifier > 0) |
536 | 0 | dwvw_encode_store_bits (psf, pdwvw, 0, 1) ; |
537 | | |
538 | | /* Write delta and delta sign bit. */ |
539 | 0 | if (delta_width) |
540 | 0 | { dwvw_encode_store_bits (psf, pdwvw, delta, abs (delta_width) - 1) ; |
541 | 0 | dwvw_encode_store_bits (psf, pdwvw, (delta_negative ? 1 : 0), 1) ; |
542 | 0 | } ; |
543 | | |
544 | | /* Write extra bit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ |
545 | 0 | if (extra_bit >= 0) |
546 | 0 | dwvw_encode_store_bits (psf, pdwvw, extra_bit, 1) ; |
547 | |
|
548 | 0 | pdwvw->last_sample = ptr [count] >> (32 - pdwvw->bit_width) ; |
549 | 0 | pdwvw->last_delta_width = delta_width ; |
550 | 0 | } ; |
551 | |
|
552 | 0 | pdwvw->samplecount += count ; |
553 | |
|
554 | 0 | return count ; |
555 | 0 | } /* dwvw_encode_data */ |
556 | | |
557 | | static sf_count_t |
558 | | dwvw_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) |
559 | 0 | { DWVW_PRIVATE *pdwvw ; |
560 | 0 | BUF_UNION ubuf ; |
561 | 0 | int *iptr ; |
562 | 0 | int k, bufferlen, writecount = 0, count ; |
563 | 0 | sf_count_t total = 0 ; |
564 | |
|
565 | 0 | if (! psf->codec_data) |
566 | 0 | return 0 ; |
567 | 0 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
568 | |
|
569 | 0 | iptr = ubuf.ibuf ; |
570 | 0 | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
571 | 0 | while (len > 0) |
572 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
573 | 0 | for (k = 0 ; k < writecount ; k++) |
574 | 0 | iptr [k] = arith_shift_left (ptr [total + k], 16) ; |
575 | 0 | count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; |
576 | |
|
577 | 0 | total += count ; |
578 | 0 | len -= writecount ; |
579 | 0 | if (count != writecount) |
580 | 0 | break ; |
581 | 0 | } ; |
582 | |
|
583 | 0 | return total ; |
584 | 0 | } /* dwvw_write_s */ |
585 | | |
586 | | static sf_count_t |
587 | | dwvw_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) |
588 | 0 | { DWVW_PRIVATE *pdwvw ; |
589 | 0 | int writecount, count ; |
590 | 0 | sf_count_t total = 0 ; |
591 | |
|
592 | 0 | if (! psf->codec_data) |
593 | 0 | return 0 ; |
594 | 0 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
595 | |
|
596 | 0 | while (len > 0) |
597 | 0 | { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; |
598 | |
|
599 | 0 | count = dwvw_encode_data (psf, pdwvw, ptr, writecount) ; |
600 | |
|
601 | 0 | total += count ; |
602 | 0 | len -= count ; |
603 | |
|
604 | 0 | if (count != writecount) |
605 | 0 | break ; |
606 | 0 | } ; |
607 | |
|
608 | 0 | return total ; |
609 | 0 | } /* dwvw_write_i */ |
610 | | |
611 | | static sf_count_t |
612 | | dwvw_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) |
613 | 0 | { DWVW_PRIVATE *pdwvw ; |
614 | 0 | BUF_UNION ubuf ; |
615 | 0 | int *iptr ; |
616 | 0 | int k, bufferlen, writecount = 0, count ; |
617 | 0 | sf_count_t total = 0 ; |
618 | 0 | float normfact ; |
619 | |
|
620 | 0 | if (! psf->codec_data) |
621 | 0 | return 0 ; |
622 | 0 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
623 | |
|
624 | 0 | normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ; |
625 | |
|
626 | 0 | iptr = ubuf.ibuf ; |
627 | 0 | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
628 | 0 | while (len > 0) |
629 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
630 | 0 | for (k = 0 ; k < writecount ; k++) |
631 | 0 | iptr [k] = psf_lrintf (normfact * ptr [total + k]) ; |
632 | 0 | count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; |
633 | |
|
634 | 0 | total += count ; |
635 | 0 | len -= writecount ; |
636 | 0 | if (count != writecount) |
637 | 0 | break ; |
638 | 0 | } ; |
639 | |
|
640 | 0 | return total ; |
641 | 0 | } /* dwvw_write_f */ |
642 | | |
643 | | static sf_count_t |
644 | | dwvw_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) |
645 | 0 | { DWVW_PRIVATE *pdwvw ; |
646 | 0 | BUF_UNION ubuf ; |
647 | 0 | int *iptr ; |
648 | 0 | int k, bufferlen, writecount = 0, count ; |
649 | 0 | sf_count_t total = 0 ; |
650 | 0 | double normfact ; |
651 | |
|
652 | 0 | if (! psf->codec_data) |
653 | 0 | return 0 ; |
654 | 0 | pdwvw = (DWVW_PRIVATE*) psf->codec_data ; |
655 | |
|
656 | 0 | normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ; |
657 | |
|
658 | 0 | iptr = ubuf.ibuf ; |
659 | 0 | bufferlen = ARRAY_LEN (ubuf.ibuf) ; |
660 | 0 | while (len > 0) |
661 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
662 | 0 | for (k = 0 ; k < writecount ; k++) |
663 | 0 | iptr [k] = psf_lrint (normfact * ptr [total + k]) ; |
664 | 0 | count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ; |
665 | |
|
666 | 0 | total += count ; |
667 | 0 | len -= writecount ; |
668 | 0 | if (count != writecount) |
669 | 0 | break ; |
670 | 0 | } ; |
671 | |
|
672 | 0 | return total ; |
673 | 0 | } /* dwvw_write_d */ |
674 | | |