562f01513e98e3c4ab493f178f446da1c84211b4
2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
19 /*===========================================================================
20 ** Delta Word Variable Width
22 ** This decoder and encoder were implemented using information found in this
23 ** document : http://home.swbell.net/rubywand/R011SNDFMTS.TXT
25 ** According to the document, the algorithm "was invented 1991 by Magnus
26 ** Lidstrom and is copyright 1993 by NuEdge Development".
41 { int dwm_maxsize
, bit_width
, max_delta
, span
;
43 int bit_count
, bits
, last_delta_width
, last_sample
;
46 unsigned char buffer
[256] ;
50 /*============================================================================================
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
) ;
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
) ;
63 static sf_count_t
dwvw_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
64 static int dwvw_close (SF_PRIVATE
*psf
) ;
66 static int dwvw_decode_data (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int *ptr
, int len
) ;
67 static int dwvw_decode_load_bits (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int bit_count
) ;
69 static int dwvw_encode_data (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, const int *ptr
, int len
) ;
70 static void dwvw_encode_store_bits (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int data
, int new_bits
) ;
71 static void dwvw_read_reset (DWVW_PRIVATE
*pdwvw
) ;
73 /*============================================================================================
74 ** DWVW initialisation function.
78 dwvw_init (SF_PRIVATE
*psf
, int bitwidth
)
79 { DWVW_PRIVATE
*pdwvw
;
81 if (psf
->codec_data
!= NULL
)
82 { psf_log_printf (psf
, "*** psf->codec_data is not NULL.\n") ;
87 return SFE_DWVW_BAD_BITWIDTH
;
89 if (psf
->file
.mode
== SFM_RDWR
)
90 return SFE_BAD_MODE_RW
;
92 if ((pdwvw
= calloc (1, sizeof (DWVW_PRIVATE
))) == NULL
)
93 return SFE_MALLOC_FAILED
;
95 psf
->codec_data
= (void*) pdwvw
;
97 pdwvw
->bit_width
= bitwidth
;
98 pdwvw
->dwm_maxsize
= bitwidth
/ 2 ;
99 pdwvw
->max_delta
= 1 << (bitwidth
- 1) ;
100 pdwvw
->span
= 1 << bitwidth
;
102 dwvw_read_reset (pdwvw
) ;
104 if (psf
->file
.mode
== SFM_READ
)
105 { psf
->read_short
= dwvw_read_s
;
106 psf
->read_int
= dwvw_read_i
;
107 psf
->read_float
= dwvw_read_f
;
108 psf
->read_double
= dwvw_read_d
;
111 if (psf
->file
.mode
== SFM_WRITE
)
112 { psf
->write_short
= dwvw_write_s
;
113 psf
->write_int
= dwvw_write_i
;
114 psf
->write_float
= dwvw_write_f
;
115 psf
->write_double
= dwvw_write_d
;
118 psf
->codec_close
= dwvw_close
;
119 psf
->seek
= dwvw_seek
;
121 /* FIXME : This is bogus. */
122 psf
->sf
.frames
= SF_COUNT_MAX
;
123 psf
->datalength
= psf
->sf
.frames
;
124 /* EMXIF : This is bogus. */
129 /*--------------------------------------------------------------------------------------------
133 dwvw_close (SF_PRIVATE
*psf
)
134 { DWVW_PRIVATE
*pdwvw
;
136 if (psf
->codec_data
== NULL
)
138 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
140 if (psf
->file
.mode
== SFM_WRITE
)
141 { static int last_values
[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
143 /* Write 8 zero samples to fully flush output. */
144 dwvw_encode_data (psf
, pdwvw
, last_values
, 12) ;
146 /* Write the last buffer worth of data to disk. */
147 psf_fwrite (pdwvw
->b
.buffer
, 1, pdwvw
->b
.index
, psf
) ;
149 if (psf
->write_header
)
150 psf
->write_header (psf
, SF_TRUE
) ;
157 dwvw_seek (SF_PRIVATE
*psf
, int UNUSED (mode
), sf_count_t offset
)
158 { DWVW_PRIVATE
*pdwvw
;
160 if (! psf
->codec_data
)
161 { psf
->error
= SFE_INTERNAL
;
162 return PSF_SEEK_ERROR
;
165 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
168 { psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
169 dwvw_read_reset (pdwvw
) ;
173 psf
->error
= SFE_BAD_SEEK
;
174 return PSF_SEEK_ERROR
;
178 /*==============================================================================
182 dwvw_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
183 { DWVW_PRIVATE
*pdwvw
;
185 int k
, bufferlen
, readcount
= 0, count
;
186 sf_count_t total
= 0 ;
188 if (! psf
->codec_data
)
190 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
193 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
195 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
196 count
= dwvw_decode_data (psf
, pdwvw
, iptr
, readcount
) ;
197 for (k
= 0 ; k
< readcount
; k
++)
198 ptr
[total
+ k
] = iptr
[k
] >> 16 ;
202 if (count
!= readcount
)
210 dwvw_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
211 { DWVW_PRIVATE
*pdwvw
;
212 int readcount
, count
;
213 sf_count_t total
= 0 ;
215 if (! psf
->codec_data
)
217 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
220 { readcount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
222 count
= dwvw_decode_data (psf
, pdwvw
, ptr
, readcount
) ;
227 if (count
!= readcount
)
235 dwvw_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
236 { DWVW_PRIVATE
*pdwvw
;
238 int k
, bufferlen
, readcount
= 0, count
;
239 sf_count_t total
= 0 ;
242 if (! psf
->codec_data
)
244 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
246 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80000000) : 1.0 ;
249 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
251 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
252 count
= dwvw_decode_data (psf
, pdwvw
, iptr
, readcount
) ;
253 for (k
= 0 ; k
< readcount
; k
++)
254 ptr
[total
+ k
] = normfact
* (float) (iptr
[k
]) ;
258 if (count
!= readcount
)
266 dwvw_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
267 { DWVW_PRIVATE
*pdwvw
;
269 int k
, bufferlen
, readcount
= 0, count
;
270 sf_count_t total
= 0 ;
273 if (! psf
->codec_data
)
275 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
277 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80000000) : 1.0 ;
280 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
282 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
283 count
= dwvw_decode_data (psf
, pdwvw
, iptr
, readcount
) ;
284 for (k
= 0 ; k
< readcount
; k
++)
285 ptr
[total
+ k
] = normfact
* (double) (iptr
[k
]) ;
289 if (count
!= readcount
)
297 dwvw_decode_data (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int *ptr
, int len
)
299 int delta_width_modifier
, delta_width
, delta_negative
, delta
, sample
;
301 /* Restore state from last decode call. */
302 delta_width
= pdwvw
->last_delta_width
;
303 sample
= pdwvw
->last_sample
;
305 for (count
= 0 ; count
< len
; count
++)
306 { /* If bit_count parameter is zero get the delta_width_modifier. */
307 delta_width_modifier
= dwvw_decode_load_bits (psf
, pdwvw
, -1) ;
309 /* Check for end of input bit stream. Break loop if end. */
310 if (delta_width_modifier
< 0)
313 if (delta_width_modifier
&& dwvw_decode_load_bits (psf
, pdwvw
, 1))
314 delta_width_modifier
= - delta_width_modifier
;
316 /* Calculate the current word width. */
317 delta_width
= (delta_width
+ delta_width_modifier
+ pdwvw
->bit_width
) % pdwvw
->bit_width
;
319 /* Load the delta. */
322 { delta
= dwvw_decode_load_bits (psf
, pdwvw
, delta_width
- 1) | (1 << (delta_width
- 1)) ;
323 delta_negative
= dwvw_decode_load_bits (psf
, pdwvw
, 1) ;
324 if (delta
== pdwvw
->max_delta
- 1)
325 delta
+= dwvw_decode_load_bits (psf
, pdwvw
, 1) ;
330 /* Calculate the sample */
333 if (sample
>= pdwvw
->max_delta
)
334 sample
-= pdwvw
->span
;
335 else if (sample
< - pdwvw
->max_delta
)
336 sample
+= pdwvw
->span
;
338 /* Store the sample justifying to the most significant bit. */
339 ptr
[count
] = sample
<< (32 - pdwvw
->bit_width
) ;
341 if (pdwvw
->b
.end
== 0 && pdwvw
->bit_count
== 0)
345 pdwvw
->last_delta_width
= delta_width
;
346 pdwvw
->last_sample
= sample
;
348 pdwvw
->samplecount
+= count
;
351 } /* dwvw_decode_data */
354 dwvw_decode_load_bits (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int bit_count
)
355 { int output
= 0, get_dwm
= SF_FALSE
;
358 ** Depending on the value of parameter bit_count, either get the
359 ** required number of bits (ie bit_count > 0) or the
360 ** delta_width_modifier (otherwise).
364 { get_dwm
= SF_TRUE
;
365 /* modify bit_count to ensure we have enought bits for finding dwm. */
366 bit_count
= pdwvw
->dwm_maxsize
;
369 /* Load bits in bit reseviour. */
370 while (pdwvw
->bit_count
< bit_count
)
371 { if (pdwvw
->b
.index
>= pdwvw
->b
.end
)
372 { pdwvw
->b
.end
= psf_fread (pdwvw
->b
.buffer
, 1, sizeof (pdwvw
->b
.buffer
), psf
) ;
376 /* Check for end of input stream. */
377 if (bit_count
< 8 && pdwvw
->b
.end
== 0)
380 pdwvw
->bits
= (pdwvw
->bits
<< 8) ;
382 if (pdwvw
->b
.index
< pdwvw
->b
.end
)
383 { pdwvw
->bits
|= pdwvw
->b
.buffer
[pdwvw
->b
.index
] ;
386 pdwvw
->bit_count
+= 8 ;
389 /* If asked to get bits do so. */
391 { output
= (pdwvw
->bits
>> (pdwvw
->bit_count
- bit_count
)) & ((1 << bit_count
) - 1) ;
392 pdwvw
->bit_count
-= bit_count
;
396 /* Otherwise must have been asked to get delta_width_modifier. */
397 while (output
< (pdwvw
->dwm_maxsize
))
398 { pdwvw
->bit_count
-= 1 ;
399 if (pdwvw
->bits
& (1 << pdwvw
->bit_count
))
405 } /* dwvw_decode_load_bits */
408 dwvw_read_reset (DWVW_PRIVATE
*pdwvw
)
409 { pdwvw
->samplecount
= 0 ;
412 pdwvw
->bit_count
= 0 ;
414 pdwvw
->last_delta_width
= 0 ;
415 pdwvw
->last_sample
= 0 ;
416 } /* dwvw_read_reset */
419 dwvw_encode_store_bits (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, int data
, int new_bits
)
422 /* Shift the bits into the resevoir. */
423 pdwvw
->bits
= (pdwvw
->bits
<< new_bits
) | (data
& ((1 << new_bits
) - 1)) ;
424 pdwvw
->bit_count
+= new_bits
;
426 /* Transfer bit to buffer. */
427 while (pdwvw
->bit_count
>= 8)
428 { byte
= pdwvw
->bits
>> (pdwvw
->bit_count
- 8) ;
429 pdwvw
->bit_count
-= 8 ;
430 pdwvw
->b
.buffer
[pdwvw
->b
.index
] = byte
& 0xFF ;
434 if (pdwvw
->b
.index
> SIGNED_SIZEOF (pdwvw
->b
.buffer
) - 4)
435 { psf_fwrite (pdwvw
->b
.buffer
, 1, pdwvw
->b
.index
, psf
) ;
440 } /* dwvw_encode_store_bits */
443 /* Debigging routine. */
445 dump_bits (DWVW_PRIVATE
*pdwvw
)
448 for (k
= 0 ; k
< 10 && k
< pdwvw
->b
.index
; k
++)
451 { putchar (mask
& pdwvw
->b
.buffer
[k
] ? '1' : '0') ;
457 for (k
= pdwvw
->bit_count
- 1 ; k
>= 0 ; k
--)
458 putchar (pdwvw
->bits
& (1 << k
) ? '1' : '0') ;
464 #define HIGHEST_BIT(x,count) \
474 dwvw_encode_data (SF_PRIVATE
*psf
, DWVW_PRIVATE
*pdwvw
, const int *ptr
, int len
)
476 int delta_width_modifier
, delta
, delta_negative
, delta_width
, extra_bit
;
478 for (count
= 0 ; count
< len
; count
++)
479 { delta
= (ptr
[count
] >> (32 - pdwvw
->bit_width
)) - pdwvw
->last_sample
;
481 /* Calculate extra_bit if needed. */
484 if (delta
< -pdwvw
->max_delta
)
485 delta
= pdwvw
->max_delta
+ (delta
% pdwvw
->max_delta
) ;
486 else if (delta
== -pdwvw
->max_delta
)
489 delta
= pdwvw
->max_delta
- 1 ;
491 else if (delta
> pdwvw
->max_delta
)
492 { delta_negative
= 1 ;
493 delta
= pdwvw
->span
- delta
;
494 delta
= abs (delta
) ;
496 else if (delta
== pdwvw
->max_delta
)
498 delta
= pdwvw
->max_delta
- 1 ;
501 { delta_negative
= 1 ;
502 delta
= abs (delta
) ;
505 if (delta
== pdwvw
->max_delta
- 1 && extra_bit
== -1)
508 /* Find width in bits of delta */
509 HIGHEST_BIT (delta
, delta_width
) ;
511 /* Calculate the delta_width_modifier */
512 delta_width_modifier
= (delta_width
- pdwvw
->last_delta_width
) % pdwvw
->bit_width
;
513 if (delta_width_modifier
> pdwvw
->dwm_maxsize
)
514 delta_width_modifier
-= pdwvw
->bit_width
;
515 if (delta_width_modifier
< -pdwvw
->dwm_maxsize
)
516 delta_width_modifier
+= pdwvw
->bit_width
;
518 /* Write delta_width_modifier zeros, followed by terminating '1'. */
519 dwvw_encode_store_bits (psf
, pdwvw
, 0, abs (delta_width_modifier
)) ;
520 if (abs (delta_width_modifier
) != pdwvw
->dwm_maxsize
)
521 dwvw_encode_store_bits (psf
, pdwvw
, 1, 1) ;
523 /* Write delta_width_modifier sign. */
524 if (delta_width_modifier
< 0)
525 dwvw_encode_store_bits (psf
, pdwvw
, 1, 1) ;
526 if (delta_width_modifier
> 0)
527 dwvw_encode_store_bits (psf
, pdwvw
, 0, 1) ;
529 /* Write delta and delta sign bit. */
531 { dwvw_encode_store_bits (psf
, pdwvw
, delta
, abs (delta_width
) - 1) ;
532 dwvw_encode_store_bits (psf
, pdwvw
, (delta_negative
? 1 : 0), 1) ;
535 /* Write extra bit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
537 dwvw_encode_store_bits (psf
, pdwvw
, extra_bit
, 1) ;
539 pdwvw
->last_sample
= ptr
[count
] >> (32 - pdwvw
->bit_width
) ;
540 pdwvw
->last_delta_width
= delta_width
;
543 pdwvw
->samplecount
+= count
;
546 } /* dwvw_encode_data */
549 dwvw_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
550 { DWVW_PRIVATE
*pdwvw
;
552 int k
, bufferlen
, writecount
= 0, count
;
553 sf_count_t total
= 0 ;
555 if (! psf
->codec_data
)
557 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
560 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
562 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
563 for (k
= 0 ; k
< writecount
; k
++)
564 iptr
[k
] = ptr
[total
+ k
] << 16 ;
565 count
= dwvw_encode_data (psf
, pdwvw
, iptr
, writecount
) ;
569 if (count
!= writecount
)
577 dwvw_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
578 { DWVW_PRIVATE
*pdwvw
;
579 int writecount
, count
;
580 sf_count_t total
= 0 ;
582 if (! psf
->codec_data
)
584 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
587 { writecount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
589 count
= dwvw_encode_data (psf
, pdwvw
, ptr
, writecount
) ;
594 if (count
!= writecount
)
602 dwvw_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
603 { DWVW_PRIVATE
*pdwvw
;
605 int k
, bufferlen
, writecount
= 0, count
;
606 sf_count_t total
= 0 ;
609 if (! psf
->codec_data
)
611 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
613 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
616 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
618 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
619 for (k
= 0 ; k
< writecount
; k
++)
620 iptr
[k
] = lrintf (normfact
* ptr
[total
+ k
]) ;
621 count
= dwvw_encode_data (psf
, pdwvw
, iptr
, writecount
) ;
625 if (count
!= writecount
)
633 dwvw_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
634 { DWVW_PRIVATE
*pdwvw
;
636 int k
, bufferlen
, writecount
= 0, count
;
637 sf_count_t total
= 0 ;
640 if (! psf
->codec_data
)
642 pdwvw
= (DWVW_PRIVATE
*) psf
->codec_data
;
644 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
647 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
649 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
650 for (k
= 0 ; k
< writecount
; k
++)
651 iptr
[k
] = lrint (normfact
* ptr
[total
+ k
]) ;
652 count
= dwvw_encode_data (psf
, pdwvw
, iptr
, writecount
) ;
656 if (count
!= writecount
)