2 ** Copyright (C) 2003-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.
27 /*============================================================================
28 ** Rule number 1 is to only apply dither when going from a larger bitwidth
29 ** to a smaller bitwidth. This can happen on both read and write.
31 ** Need to apply dither on all conversions marked X below.
36 ** | short int float double
37 ** --------+-----------------------------------------------
39 ** u 16 bit | none X X X
40 ** t 24 bit | none X X X
41 ** p 32 bit | none none X X
42 ** u float | none none none none
43 ** t double | none none none none
48 ** O | 8 bit 16 bit 24 bit 32 bit float double
49 ** u --------+-------------------------------------------------
50 ** t short | none none X X X X
51 ** p int | none none none X X X
52 ** u float | none none none none none none
53 ** t double | none none none none none none
56 #define SFE_DITHER_BAD_PTR 666
57 #define SFE_DITHER_BAD_TYPE 667
60 { int read_short_dither_bits
, read_int_dither_bits
;
61 int write_short_dither_bits
, write_int_dither_bits
;
62 double read_float_dither_scale
, read_double_dither_bits
;
63 double write_float_dither_scale
, write_double_dither_bits
;
65 sf_count_t (*read_short
) (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
66 sf_count_t (*read_int
) (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
67 sf_count_t (*read_float
) (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
68 sf_count_t (*read_double
) (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
70 sf_count_t (*write_short
) (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
71 sf_count_t (*write_int
) (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
72 sf_count_t (*write_float
) (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
73 sf_count_t (*write_double
) (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
75 double buffer
[SF_BUFFER_LEN
/ sizeof (double)] ;
78 static sf_count_t
dither_read_short (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
79 static sf_count_t
dither_read_int (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
81 static sf_count_t
dither_write_short (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
82 static sf_count_t
dither_write_int (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
83 static sf_count_t
dither_write_float (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
84 static sf_count_t
dither_write_double (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
87 dither_init (SF_PRIVATE
*psf
, int mode
)
88 { DITHER_DATA
*pdither
;
90 pdither
= psf
->dither
; /* This may be NULL. */
92 /* Turn off dither on read. */
93 if (mode
== SFM_READ
&& psf
->read_dither
.type
== SFD_NO_DITHER
)
94 { if (pdither
== NULL
)
95 return 0 ; /* Dither is already off, so just return. */
97 if (pdither
->read_short
)
98 psf
->read_short
= pdither
->read_short
;
99 if (pdither
->read_int
)
100 psf
->read_int
= pdither
->read_int
;
101 if (pdither
->read_float
)
102 psf
->read_float
= pdither
->read_float
;
103 if (pdither
->read_double
)
104 psf
->read_double
= pdither
->read_double
;
108 /* Turn off dither on write. */
109 if (mode
== SFM_WRITE
&& psf
->write_dither
.type
== SFD_NO_DITHER
)
110 { if (pdither
== NULL
)
111 return 0 ; /* Dither is already off, so just return. */
113 if (pdither
->write_short
)
114 psf
->write_short
= pdither
->write_short
;
115 if (pdither
->write_int
)
116 psf
->write_int
= pdither
->write_int
;
117 if (pdither
->write_float
)
118 psf
->write_float
= pdither
->write_float
;
119 if (pdither
->write_double
)
120 psf
->write_double
= pdither
->write_double
;
124 /* Turn on dither on read if asked. */
125 if (mode
== SFM_READ
&& psf
->read_dither
.type
!= 0)
126 { if (pdither
== NULL
)
127 pdither
= psf
->dither
= calloc (1, sizeof (DITHER_DATA
)) ;
129 return SFE_MALLOC_FAILED
;
131 switch (SF_CODEC (psf
->sf
.format
))
132 { case SF_FORMAT_DOUBLE
:
133 case SF_FORMAT_FLOAT
:
134 pdither
->read_int
= psf
->read_int
;
135 psf
->read_int
= dither_read_int
;
138 case SF_FORMAT_PCM_32
:
139 case SF_FORMAT_PCM_24
:
140 case SF_FORMAT_PCM_16
:
141 case SF_FORMAT_PCM_S8
:
142 case SF_FORMAT_PCM_U8
:
143 pdither
->read_short
= psf
->read_short
;
144 psf
->read_short
= dither_read_short
;
151 /* Turn on dither on write if asked. */
152 if (mode
== SFM_WRITE
&& psf
->write_dither
.type
!= 0)
153 { if (pdither
== NULL
)
154 pdither
= psf
->dither
= calloc (1, sizeof (DITHER_DATA
)) ;
156 return SFE_MALLOC_FAILED
;
158 switch (SF_CODEC (psf
->sf
.format
))
159 { case SF_FORMAT_DOUBLE
:
160 case SF_FORMAT_FLOAT
:
161 pdither
->write_int
= psf
->write_int
;
162 psf
->write_int
= dither_write_int
;
165 case SF_FORMAT_PCM_32
:
166 case SF_FORMAT_PCM_24
:
167 case SF_FORMAT_PCM_16
:
168 case SF_FORMAT_PCM_S8
:
169 case SF_FORMAT_PCM_U8
:
175 pdither
->write_short
= psf
->write_short
;
176 psf
->write_short
= dither_write_short
;
178 pdither
->write_int
= psf
->write_int
;
179 psf
->write_int
= dither_write_int
;
181 pdither
->write_float
= psf
->write_float
;
182 psf
->write_float
= dither_write_float
;
184 pdither
->write_double
= psf
->write_double
;
185 psf
->write_double
= dither_write_double
;
191 /*==============================================================================
194 static void dither_short (const short *in
, short *out
, int frames
, int channels
) ;
195 static void dither_int (const int *in
, int *out
, int frames
, int channels
) ;
197 static void dither_float (const float *in
, float *out
, int frames
, int channels
) ;
198 static void dither_double (const double *in
, double *out
, int frames
, int channels
) ;
201 dither_read_short (SF_PRIVATE
* UNUSED (psf
), short * UNUSED (ptr
), sf_count_t len
)
204 } /* dither_read_short */
207 dither_read_int (SF_PRIVATE
* UNUSED (psf
), int * UNUSED (ptr
), sf_count_t len
)
210 } /* dither_read_int */
212 /*------------------------------------------------------------------------------
216 dither_write_short (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
217 { DITHER_DATA
*pdither
;
218 int bufferlen
, writecount
, thiswrite
;
219 sf_count_t total
= 0 ;
221 if ((pdither
= psf
->dither
) == NULL
)
222 { psf
->error
= SFE_DITHER_BAD_PTR
;
226 switch (SF_CODEC (psf
->sf
.format
))
227 { case SF_FORMAT_PCM_S8
:
228 case SF_FORMAT_PCM_U8
:
229 case SF_FORMAT_DPCM_8
:
233 return pdither
->write_short (psf
, ptr
, len
) ;
236 bufferlen
= sizeof (pdither
->buffer
) / sizeof (short) ;
239 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
240 writecount
/= psf
->sf
.channels
;
241 writecount
*= psf
->sf
.channels
;
243 dither_short (ptr
, (short*) pdither
->buffer
, writecount
/ psf
->sf
.channels
, psf
->sf
.channels
) ;
245 thiswrite
= pdither
->write_short (psf
, (short*) pdither
->buffer
, writecount
) ;
248 if (thiswrite
< writecount
)
253 } /* dither_write_short */
256 dither_write_int (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
257 { DITHER_DATA
*pdither
;
258 int bufferlen
, writecount
, thiswrite
;
259 sf_count_t total
= 0 ;
261 if ((pdither
= psf
->dither
) == NULL
)
262 { psf
->error
= SFE_DITHER_BAD_PTR
;
266 switch (SF_CODEC (psf
->sf
.format
))
267 { case SF_FORMAT_PCM_S8
:
268 case SF_FORMAT_PCM_U8
:
269 case SF_FORMAT_PCM_16
:
270 case SF_FORMAT_PCM_24
:
273 case SF_FORMAT_DPCM_8
:
274 case SF_FORMAT_DPCM_16
:
278 return pdither
->write_int (psf
, ptr
, len
) ;
282 bufferlen
= sizeof (pdither
->buffer
) / sizeof (int) ;
285 { writecount
= (len
>= bufferlen
) ? bufferlen
: (int) len
;
286 writecount
/= psf
->sf
.channels
;
287 writecount
*= psf
->sf
.channels
;
289 dither_int (ptr
, (int*) pdither
->buffer
, writecount
/ psf
->sf
.channels
, psf
->sf
.channels
) ;
291 thiswrite
= pdither
->write_int (psf
, (int*) pdither
->buffer
, writecount
) ;
294 if (thiswrite
< writecount
)
299 } /* dither_write_int */
302 dither_write_float (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
303 { DITHER_DATA
*pdither
;
304 int bufferlen
, writecount
, thiswrite
;
305 sf_count_t total
= 0 ;
307 if ((pdither
= psf
->dither
) == NULL
)
308 { psf
->error
= SFE_DITHER_BAD_PTR
;
312 switch (SF_CODEC (psf
->sf
.format
))
313 { case SF_FORMAT_PCM_S8
:
314 case SF_FORMAT_PCM_U8
:
315 case SF_FORMAT_PCM_16
:
316 case SF_FORMAT_PCM_24
:
319 case SF_FORMAT_DPCM_8
:
320 case SF_FORMAT_DPCM_16
:
324 return pdither
->write_float (psf
, ptr
, len
) ;
327 bufferlen
= sizeof (pdither
->buffer
) / sizeof (float) ;
330 { writecount
= (len
>= bufferlen
) ? bufferlen
: (float) len
;
331 writecount
/= psf
->sf
.channels
;
332 writecount
*= psf
->sf
.channels
;
334 dither_float (ptr
, (float*) pdither
->buffer
, writecount
/ psf
->sf
.channels
, psf
->sf
.channels
) ;
336 thiswrite
= pdither
->write_float (psf
, (float*) pdither
->buffer
, writecount
) ;
339 if (thiswrite
< writecount
)
344 } /* dither_write_float */
347 dither_write_double (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
348 { DITHER_DATA
*pdither
;
349 int bufferlen
, writecount
, thiswrite
;
350 sf_count_t total
= 0 ;
352 if ((pdither
= psf
->dither
) == NULL
)
353 { psf
->error
= SFE_DITHER_BAD_PTR
;
357 switch (SF_CODEC (psf
->sf
.format
))
358 { case SF_FORMAT_PCM_S8
:
359 case SF_FORMAT_PCM_U8
:
360 case SF_FORMAT_PCM_16
:
361 case SF_FORMAT_PCM_24
:
364 case SF_FORMAT_DPCM_8
:
365 case SF_FORMAT_DPCM_16
:
369 return pdither
->write_double (psf
, ptr
, len
) ;
373 bufferlen
= sizeof (pdither
->buffer
) / sizeof (double) ;
376 { writecount
= (len
>= bufferlen
) ? bufferlen
: (double) len
;
377 writecount
/= psf
->sf
.channels
;
378 writecount
*= psf
->sf
.channels
;
380 dither_double (ptr
, (double*) pdither
->buffer
, writecount
/ psf
->sf
.channels
, psf
->sf
.channels
) ;
382 thiswrite
= pdither
->write_double (psf
, (double*) pdither
->buffer
, writecount
) ;
385 if (thiswrite
< writecount
)
390 } /* dither_write_double */
392 /*==============================================================================
396 dither_short (const short *in
, short *out
, int frames
, int channels
)
399 for (ch
= 0 ; ch
< channels
; ch
++)
400 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
406 dither_int (const int *in
, int *out
, int frames
, int channels
)
409 for (ch
= 0 ; ch
< channels
; ch
++)
410 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
416 dither_float (const float *in
, float *out
, int frames
, int channels
)
419 for (ch
= 0 ; ch
< channels
; ch
++)
420 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
426 dither_double (const double *in
, double *out
, int frames
, int channels
)
429 for (ch
= 0 ; ch
< channels
; ch
++)
430 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
433 } /* dither_double */
435 /*==============================================================================
440 ** Not made public because this (maybe) requires storage of state information.
442 ** Also maybe need separate state info for each channel!!!!
446 DO_NOT_USE_sf_dither_short (const SF_DITHER_INFO
*dither
, const short *in
, short *out
, int frames
, int channels
)
450 return SFE_DITHER_BAD_PTR
;
452 switch (dither
->type
& SFD_TYPEMASK
)
454 case SFD_TRIANGULAR_PDF
:
455 for (ch
= 0 ; ch
< channels
; ch
++)
456 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
461 return SFE_DITHER_BAD_TYPE
;
465 } /* DO_NOT_USE_sf_dither_short */
468 DO_NOT_USE_sf_dither_int (const SF_DITHER_INFO
*dither
, const int *in
, int *out
, int frames
, int channels
)
472 return SFE_DITHER_BAD_PTR
;
474 switch (dither
->type
& SFD_TYPEMASK
)
476 case SFD_TRIANGULAR_PDF
:
477 for (ch
= 0 ; ch
< channels
; ch
++)
478 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
483 return SFE_DITHER_BAD_TYPE
;
487 } /* DO_NOT_USE_sf_dither_int */
490 DO_NOT_USE_sf_dither_float (const SF_DITHER_INFO
*dither
, const float *in
, float *out
, int frames
, int channels
)
494 return SFE_DITHER_BAD_PTR
;
496 switch (dither
->type
& SFD_TYPEMASK
)
498 case SFD_TRIANGULAR_PDF
:
499 for (ch
= 0 ; ch
< channels
; ch
++)
500 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
505 return SFE_DITHER_BAD_TYPE
;
509 } /* DO_NOT_USE_sf_dither_float */
512 DO_NOT_USE_sf_dither_double (const SF_DITHER_INFO
*dither
, const double *in
, double *out
, int frames
, int channels
)
516 return SFE_DITHER_BAD_PTR
;
518 switch (dither
->type
& SFD_TYPEMASK
)
520 case SFD_TRIANGULAR_PDF
:
521 for (ch
= 0 ; ch
< channels
; ch
++)
522 for (k
= ch
; k
< channels
* frames
; k
+= channels
)
527 return SFE_DITHER_BAD_TYPE
;
531 } /* DO_NOT_USE_sf_dither_double */