2
* $Id: pa_converters.c 1339 2008-02-15 07:50:33Z rossb $
3
* Portable Audio I/O Library sample conversion mechanism
5
* Based on the Open Source API proposed by Ross Bencina
6
* Copyright (c) 1999-2002 Phil Burk, Ross Bencina
8
* Permission is hereby granted, free of charge, to any person obtaining
9
* a copy of this software and associated documentation files
10
* (the "Software"), to deal in the Software without restriction,
11
* including without limitation the rights to use, copy, modify, merge,
12
* publish, distribute, sublicense, and/or sell copies of the Software,
13
* and to permit persons to whom the Software is furnished to do so,
14
* subject to the following conditions:
16
* The above copyright notice and this permission notice shall be
17
* included in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
* The text above constitutes the entire PortAudio license; however,
30
* the PortAudio community also makes the following non-binding requests:
32
* Any person wishing to distribute modifications to the Software is
33
* requested to send the modifications to the original developer so that
34
* they can be incorporated into the canonical version. It is also
35
* requested that these non-binding requests be included along with the
42
@brief Conversion function implementations.
44
If the C9x function lrintf() is available, define PA_USE_C99_LRINTF to use it
46
@todo Consider whether functions which dither but don't clip should exist,
47
V18 automatically enabled clipping whenever dithering was selected. Perhaps
48
we should do the same.
50
@todo implement the converters marked IMPLEMENT ME: Float32_To_UInt8_Dither,
51
Float32_To_UInt8_Clip, Float32_To_UInt8_DitherClip, Int32_To_Int24_Dither,
52
Int32_To_UInt8_Dither, Int24_To_Int16_Dither, Int24_To_Int8_Dither,
53
Int24_To_UInt8_Dither, Int16_To_Int8_Dither, Int16_To_UInt8_Dither,
55
@todo review the converters marked REVIEW: Float32_To_Int32,
56
Float32_To_Int32_Dither, Float32_To_Int32_Clip, Float32_To_Int32_DitherClip,
57
Int32_To_Int16_Dither, Int32_To_Int8_Dither, Int16_To_Int32
61
#include "pa_converters.h"
62
#include "pa_dither.h"
63
#include "pa_endianness.h"
67
PaSampleFormat PaUtil_SelectClosestAvailableFormat(
68
PaSampleFormat availableFormats, PaSampleFormat format )
70
PaSampleFormat result;
72
format &= ~paNonInterleaved;
73
availableFormats &= ~paNonInterleaved;
75
if( (format & availableFormats) == 0 )
77
/* NOTE: this code depends on the sample format constants being in
78
descending order of quality - ie best quality is 0
79
FIXME: should write an assert which checks that all of the
80
known constants conform to that requirement.
85
/* scan for better formats */
91
while( (result & availableFormats) == 0 && result != 0 );
99
/* scan for worse formats */
105
while( (result & availableFormats) == 0 && result != paCustomFormat );
107
if( (result & availableFormats) == 0 )
108
result = paSampleFormatNotSupported;
118
/* -------------------------------------------------------------------------- */
120
#define PA_SELECT_FORMAT_( format, float32, int32, int24, int16, int8, uint8 ) \
121
switch( format & ~paNonInterleaved ){ \
137
/* -------------------------------------------------------------------------- */
139
#define PA_SELECT_CONVERTER_DITHER_CLIP_( flags, source, destination ) \
140
if( flags & paClipOff ){ /* no clip */ \
141
if( flags & paDitherOff ){ /* no dither */ \
142
return paConverters. source ## _To_ ## destination; \
143
}else{ /* dither */ \
144
return paConverters. source ## _To_ ## destination ## _Dither; \
147
if( flags & paDitherOff ){ /* no dither */ \
148
return paConverters. source ## _To_ ## destination ## _Clip; \
149
}else{ /* dither */ \
150
return paConverters. source ## _To_ ## destination ## _DitherClip; \
154
/* -------------------------------------------------------------------------- */
156
#define PA_SELECT_CONVERTER_DITHER_( flags, source, destination ) \
157
if( flags & paDitherOff ){ /* no dither */ \
158
return paConverters. source ## _To_ ## destination; \
159
}else{ /* dither */ \
160
return paConverters. source ## _To_ ## destination ## _Dither; \
163
/* -------------------------------------------------------------------------- */
165
#define PA_USE_CONVERTER_( source, destination )\
166
return paConverters. source ## _To_ ## destination;
168
/* -------------------------------------------------------------------------- */
170
#define PA_UNITY_CONVERSION_( wordlength )\
171
return paConverters. Copy_ ## wordlength ## _To_ ## wordlength;
173
/* -------------------------------------------------------------------------- */
175
PaUtilConverter* PaUtil_SelectConverter( PaSampleFormat sourceFormat,
176
PaSampleFormat destinationFormat, PaStreamFlags flags )
178
PA_SELECT_FORMAT_( sourceFormat,
180
PA_SELECT_FORMAT_( destinationFormat,
181
/* paFloat32: */ PA_UNITY_CONVERSION_( 32 ),
182
/* paInt32: */ PA_SELECT_CONVERTER_DITHER_CLIP_( flags, Float32, Int32 ),
183
/* paInt24: */ PA_SELECT_CONVERTER_DITHER_CLIP_( flags, Float32, Int24 ),
184
/* paInt16: */ PA_SELECT_CONVERTER_DITHER_CLIP_( flags, Float32, Int16 ),
185
/* paInt8: */ PA_SELECT_CONVERTER_DITHER_CLIP_( flags, Float32, Int8 ),
186
/* paUInt8: */ PA_SELECT_CONVERTER_DITHER_CLIP_( flags, Float32, UInt8 )
189
PA_SELECT_FORMAT_( destinationFormat,
190
/* paFloat32: */ PA_USE_CONVERTER_( Int32, Float32 ),
191
/* paInt32: */ PA_UNITY_CONVERSION_( 32 ),
192
/* paInt24: */ PA_SELECT_CONVERTER_DITHER_( flags, Int32, Int24 ),
193
/* paInt16: */ PA_SELECT_CONVERTER_DITHER_( flags, Int32, Int16 ),
194
/* paInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int32, Int8 ),
195
/* paUInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int32, UInt8 )
198
PA_SELECT_FORMAT_( destinationFormat,
199
/* paFloat32: */ PA_USE_CONVERTER_( Int24, Float32 ),
200
/* paInt32: */ PA_USE_CONVERTER_( Int24, Int32 ),
201
/* paInt24: */ PA_UNITY_CONVERSION_( 24 ),
202
/* paInt16: */ PA_SELECT_CONVERTER_DITHER_( flags, Int24, Int16 ),
203
/* paInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int24, Int8 ),
204
/* paUInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int24, UInt8 )
207
PA_SELECT_FORMAT_( destinationFormat,
208
/* paFloat32: */ PA_USE_CONVERTER_( Int16, Float32 ),
209
/* paInt32: */ PA_USE_CONVERTER_( Int16, Int32 ),
210
/* paInt24: */ PA_USE_CONVERTER_( Int16, Int24 ),
211
/* paInt16: */ PA_UNITY_CONVERSION_( 16 ),
212
/* paInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int16, Int8 ),
213
/* paUInt8: */ PA_SELECT_CONVERTER_DITHER_( flags, Int16, UInt8 )
216
PA_SELECT_FORMAT_( destinationFormat,
217
/* paFloat32: */ PA_USE_CONVERTER_( Int8, Float32 ),
218
/* paInt32: */ PA_USE_CONVERTER_( Int8, Int32 ),
219
/* paInt24: */ PA_USE_CONVERTER_( Int8, Int24 ),
220
/* paInt16: */ PA_USE_CONVERTER_( Int8, Int16 ),
221
/* paInt8: */ PA_UNITY_CONVERSION_( 8 ),
222
/* paUInt8: */ PA_USE_CONVERTER_( Int8, UInt8 )
225
PA_SELECT_FORMAT_( destinationFormat,
226
/* paFloat32: */ PA_USE_CONVERTER_( UInt8, Float32 ),
227
/* paInt32: */ PA_USE_CONVERTER_( UInt8, Int32 ),
228
/* paInt24: */ PA_USE_CONVERTER_( UInt8, Int24 ),
229
/* paInt16: */ PA_USE_CONVERTER_( UInt8, Int16 ),
230
/* paInt8: */ PA_USE_CONVERTER_( UInt8, Int8 ),
231
/* paUInt8: */ PA_UNITY_CONVERSION_( 8 )
236
/* -------------------------------------------------------------------------- */
238
#ifdef PA_NO_STANDARD_CONVERTERS
240
/* -------------------------------------------------------------------------- */
242
PaUtilConverterTable paConverters = {
243
0, /* PaUtilConverter *Float32_To_Int32; */
244
0, /* PaUtilConverter *Float32_To_Int32_Dither; */
245
0, /* PaUtilConverter *Float32_To_Int32_Clip; */
246
0, /* PaUtilConverter *Float32_To_Int32_DitherClip; */
248
0, /* PaUtilConverter *Float32_To_Int24; */
249
0, /* PaUtilConverter *Float32_To_Int24_Dither; */
250
0, /* PaUtilConverter *Float32_To_Int24_Clip; */
251
0, /* PaUtilConverter *Float32_To_Int24_DitherClip; */
253
0, /* PaUtilConverter *Float32_To_Int16; */
254
0, /* PaUtilConverter *Float32_To_Int16_Dither; */
255
0, /* PaUtilConverter *Float32_To_Int16_Clip; */
256
0, /* PaUtilConverter *Float32_To_Int16_DitherClip; */
258
0, /* PaUtilConverter *Float32_To_Int8; */
259
0, /* PaUtilConverter *Float32_To_Int8_Dither; */
260
0, /* PaUtilConverter *Float32_To_Int8_Clip; */
261
0, /* PaUtilConverter *Float32_To_Int8_DitherClip; */
263
0, /* PaUtilConverter *Float32_To_UInt8; */
264
0, /* PaUtilConverter *Float32_To_UInt8_Dither; */
265
0, /* PaUtilConverter *Float32_To_UInt8_Clip; */
266
0, /* PaUtilConverter *Float32_To_UInt8_DitherClip; */
268
0, /* PaUtilConverter *Int32_To_Float32; */
269
0, /* PaUtilConverter *Int32_To_Int24; */
270
0, /* PaUtilConverter *Int32_To_Int24_Dither; */
271
0, /* PaUtilConverter *Int32_To_Int16; */
272
0, /* PaUtilConverter *Int32_To_Int16_Dither; */
273
0, /* PaUtilConverter *Int32_To_Int8; */
274
0, /* PaUtilConverter *Int32_To_Int8_Dither; */
275
0, /* PaUtilConverter *Int32_To_UInt8; */
276
0, /* PaUtilConverter *Int32_To_UInt8_Dither; */
278
0, /* PaUtilConverter *Int24_To_Float32; */
279
0, /* PaUtilConverter *Int24_To_Int32; */
280
0, /* PaUtilConverter *Int24_To_Int16; */
281
0, /* PaUtilConverter *Int24_To_Int16_Dither; */
282
0, /* PaUtilConverter *Int24_To_Int8; */
283
0, /* PaUtilConverter *Int24_To_Int8_Dither; */
284
0, /* PaUtilConverter *Int24_To_UInt8; */
285
0, /* PaUtilConverter *Int24_To_UInt8_Dither; */
287
0, /* PaUtilConverter *Int16_To_Float32; */
288
0, /* PaUtilConverter *Int16_To_Int32; */
289
0, /* PaUtilConverter *Int16_To_Int24; */
290
0, /* PaUtilConverter *Int16_To_Int8; */
291
0, /* PaUtilConverter *Int16_To_Int8_Dither; */
292
0, /* PaUtilConverter *Int16_To_UInt8; */
293
0, /* PaUtilConverter *Int16_To_UInt8_Dither; */
295
0, /* PaUtilConverter *Int8_To_Float32; */
296
0, /* PaUtilConverter *Int8_To_Int32; */
297
0, /* PaUtilConverter *Int8_To_Int24 */
298
0, /* PaUtilConverter *Int8_To_Int16; */
299
0, /* PaUtilConverter *Int8_To_UInt8; */
301
0, /* PaUtilConverter *UInt8_To_Float32; */
302
0, /* PaUtilConverter *UInt8_To_Int32; */
303
0, /* PaUtilConverter *UInt8_To_Int24; */
304
0, /* PaUtilConverter *UInt8_To_Int16; */
305
0, /* PaUtilConverter *UInt8_To_Int8; */
307
0, /* PaUtilConverter *Copy_8_To_8; */
308
0, /* PaUtilConverter *Copy_16_To_16; */
309
0, /* PaUtilConverter *Copy_24_To_24; */
310
0 /* PaUtilConverter *Copy_32_To_32; */
313
/* -------------------------------------------------------------------------- */
315
#else /* PA_NO_STANDARD_CONVERTERS is not defined */
317
/* -------------------------------------------------------------------------- */
319
#define PA_CLIP_( val, min, max )\
320
{ val = ((val) < (min)) ? (min) : (((val) > (max)) ? (max) : (val)); }
323
static const float const_1_div_128_ = 1.0f / 128.0f; /* 8 bit multiplier */
325
static const float const_1_div_32768_ = 1.0f / 32768.f; /* 16 bit multiplier */
327
static const double const_1_div_2147483648_ = 1.0 / 2147483648.0; /* 32 bit multiplier */
329
/* -------------------------------------------------------------------------- */
331
static void Float32_To_Int32(
332
void *destinationBuffer, signed int destinationStride,
333
void *sourceBuffer, signed int sourceStride,
334
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
336
float *src = (float*)sourceBuffer;
337
PaInt32 *dest = (PaInt32*)destinationBuffer;
338
(void)ditherGenerator; /* unused parameter */
343
#ifdef PA_USE_C99_LRINTF
344
float scaled = *src * 0x7FFFFFFF;
345
*dest = lrintf(scaled-0.5f);
347
double scaled = *src * 0x7FFFFFFF;
348
*dest = (PaInt32) scaled;
352
dest += destinationStride;
356
/* -------------------------------------------------------------------------- */
358
static void Float32_To_Int32_Dither(
359
void *destinationBuffer, signed int destinationStride,
360
void *sourceBuffer, signed int sourceStride,
361
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
363
float *src = (float*)sourceBuffer;
364
PaInt32 *dest = (PaInt32*)destinationBuffer;
369
#ifdef PA_USE_C99_LRINTF
370
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
371
/* use smaller scaler to prevent overflow when we add the dither */
372
float dithered = ((float)*src * (2147483646.0f)) + dither;
373
*dest = lrintf(dithered - 0.5f);
375
double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
376
/* use smaller scaler to prevent overflow when we add the dither */
377
double dithered = ((double)*src * (2147483646.0)) + dither;
378
*dest = (PaInt32) dithered;
381
dest += destinationStride;
385
/* -------------------------------------------------------------------------- */
387
static void Float32_To_Int32_Clip(
388
void *destinationBuffer, signed int destinationStride,
389
void *sourceBuffer, signed int sourceStride,
390
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
392
float *src = (float*)sourceBuffer;
393
PaInt32 *dest = (PaInt32*)destinationBuffer;
394
(void) ditherGenerator; /* unused parameter */
399
#ifdef PA_USE_C99_LRINTF
400
float scaled = *src * 0x7FFFFFFF;
401
PA_CLIP_( scaled, -2147483648.f, 2147483647.f );
402
*dest = lrintf(scaled-0.5f);
404
double scaled = *src * 0x7FFFFFFF;
405
PA_CLIP_( scaled, -2147483648., 2147483647. );
406
*dest = (PaInt32) scaled;
410
dest += destinationStride;
414
/* -------------------------------------------------------------------------- */
416
static void Float32_To_Int32_DitherClip(
417
void *destinationBuffer, signed int destinationStride,
418
void *sourceBuffer, signed int sourceStride,
419
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
421
float *src = (float*)sourceBuffer;
422
PaInt32 *dest = (PaInt32*)destinationBuffer;
427
#ifdef PA_USE_C99_LRINTF
428
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
429
/* use smaller scaler to prevent overflow when we add the dither */
430
float dithered = ((float)*src * (2147483646.0f)) + dither;
431
PA_CLIP_( dithered, -2147483648.f, 2147483647.f );
432
*dest = lrintf(dithered-0.5f);
434
double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
435
/* use smaller scaler to prevent overflow when we add the dither */
436
double dithered = ((double)*src * (2147483646.0)) + dither;
437
PA_CLIP_( dithered, -2147483648., 2147483647. );
438
*dest = (PaInt32) dithered;
442
dest += destinationStride;
446
/* -------------------------------------------------------------------------- */
448
static void Float32_To_Int24(
449
void *destinationBuffer, signed int destinationStride,
450
void *sourceBuffer, signed int sourceStride,
451
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
453
float *src = (float*)sourceBuffer;
454
unsigned char *dest = (unsigned char*)destinationBuffer;
457
(void) ditherGenerator; /* unused parameter */
461
/* convert to 32 bit and drop the low 8 bits */
462
double scaled = *src * 0x7FFFFFFF;
463
temp = (PaInt32) scaled;
465
#if defined(PA_LITTLE_ENDIAN)
466
dest[0] = (unsigned char)(temp >> 8);
467
dest[1] = (unsigned char)(temp >> 16);
468
dest[2] = (unsigned char)(temp >> 24);
469
#elif defined(PA_BIG_ENDIAN)
470
dest[0] = (unsigned char)(temp >> 24);
471
dest[1] = (unsigned char)(temp >> 16);
472
dest[2] = (unsigned char)(temp >> 8);
476
dest += destinationStride * 3;
480
/* -------------------------------------------------------------------------- */
482
static void Float32_To_Int24_Dither(
483
void *destinationBuffer, signed int destinationStride,
484
void *sourceBuffer, signed int sourceStride,
485
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
487
float *src = (float*)sourceBuffer;
488
unsigned char *dest = (unsigned char*)destinationBuffer;
493
/* convert to 32 bit and drop the low 8 bits */
495
double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
496
/* use smaller scaler to prevent overflow when we add the dither */
497
double dithered = ((double)*src * (2147483646.0)) + dither;
499
temp = (PaInt32) dithered;
501
#if defined(PA_LITTLE_ENDIAN)
502
dest[0] = (unsigned char)(temp >> 8);
503
dest[1] = (unsigned char)(temp >> 16);
504
dest[2] = (unsigned char)(temp >> 24);
505
#elif defined(PA_BIG_ENDIAN)
506
dest[0] = (unsigned char)(temp >> 24);
507
dest[1] = (unsigned char)(temp >> 16);
508
dest[2] = (unsigned char)(temp >> 8);
512
dest += destinationStride * 3;
516
/* -------------------------------------------------------------------------- */
518
static void Float32_To_Int24_Clip(
519
void *destinationBuffer, signed int destinationStride,
520
void *sourceBuffer, signed int sourceStride,
521
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
523
float *src = (float*)sourceBuffer;
524
unsigned char *dest = (unsigned char*)destinationBuffer;
527
(void) ditherGenerator; /* unused parameter */
531
/* convert to 32 bit and drop the low 8 bits */
532
double scaled = *src * 0x7FFFFFFF;
533
PA_CLIP_( scaled, -2147483648., 2147483647. );
534
temp = (PaInt32) scaled;
536
#if defined(PA_LITTLE_ENDIAN)
537
dest[0] = (unsigned char)(temp >> 8);
538
dest[1] = (unsigned char)(temp >> 16);
539
dest[2] = (unsigned char)(temp >> 24);
540
#elif defined(PA_BIG_ENDIAN)
541
dest[0] = (unsigned char)(temp >> 24);
542
dest[1] = (unsigned char)(temp >> 16);
543
dest[2] = (unsigned char)(temp >> 8);
547
dest += destinationStride * 3;
551
/* -------------------------------------------------------------------------- */
553
static void Float32_To_Int24_DitherClip(
554
void *destinationBuffer, signed int destinationStride,
555
void *sourceBuffer, signed int sourceStride,
556
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
558
float *src = (float*)sourceBuffer;
559
unsigned char *dest = (unsigned char*)destinationBuffer;
564
/* convert to 32 bit and drop the low 8 bits */
566
double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
567
/* use smaller scaler to prevent overflow when we add the dither */
568
double dithered = ((double)*src * (2147483646.0)) + dither;
569
PA_CLIP_( dithered, -2147483648., 2147483647. );
571
temp = (PaInt32) dithered;
573
#if defined(PA_LITTLE_ENDIAN)
574
dest[0] = (unsigned char)(temp >> 8);
575
dest[1] = (unsigned char)(temp >> 16);
576
dest[2] = (unsigned char)(temp >> 24);
577
#elif defined(PA_BIG_ENDIAN)
578
dest[0] = (unsigned char)(temp >> 24);
579
dest[1] = (unsigned char)(temp >> 16);
580
dest[2] = (unsigned char)(temp >> 8);
584
dest += destinationStride * 3;
588
/* -------------------------------------------------------------------------- */
590
static void Float32_To_Int16(
591
void *destinationBuffer, signed int destinationStride,
592
void *sourceBuffer, signed int sourceStride,
593
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
595
float *src = (float*)sourceBuffer;
596
PaInt16 *dest = (PaInt16*)destinationBuffer;
597
(void)ditherGenerator; /* unused parameter */
601
#ifdef PA_USE_C99_LRINTF
602
float tempf = (*src * (32767.0f)) ;
603
*dest = lrintf(tempf-0.5f);
605
short samp = (short) (*src * (32767.0f));
610
dest += destinationStride;
614
/* -------------------------------------------------------------------------- */
616
static void Float32_To_Int16_Dither(
617
void *destinationBuffer, signed int destinationStride,
618
void *sourceBuffer, signed int sourceStride,
619
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
621
float *src = (float*)sourceBuffer;
622
PaInt16 *dest = (PaInt16*)destinationBuffer;
627
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
628
/* use smaller scaler to prevent overflow when we add the dither */
629
float dithered = (*src * (32766.0f)) + dither;
631
#ifdef PA_USE_C99_LRINTF
632
*dest = lrintf(dithered-0.5f);
634
*dest = (PaInt16) dithered;
638
dest += destinationStride;
642
/* -------------------------------------------------------------------------- */
644
static void Float32_To_Int16_Clip(
645
void *destinationBuffer, signed int destinationStride,
646
void *sourceBuffer, signed int sourceStride,
647
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
649
float *src = (float*)sourceBuffer;
650
PaInt16 *dest = (PaInt16*)destinationBuffer;
651
(void)ditherGenerator; /* unused parameter */
655
#ifdef PA_USE_C99_LRINTF
656
long samp = lrintf((*src * (32767.0f)) -0.5f);
658
long samp = (PaInt32) (*src * (32767.0f));
660
PA_CLIP_( samp, -0x8000, 0x7FFF );
661
*dest = (PaInt16) samp;
664
dest += destinationStride;
668
/* -------------------------------------------------------------------------- */
670
static void Float32_To_Int16_DitherClip(
671
void *destinationBuffer, signed int destinationStride,
672
void *sourceBuffer, signed int sourceStride,
673
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
675
float *src = (float*)sourceBuffer;
676
PaInt16 *dest = (PaInt16*)destinationBuffer;
677
(void)ditherGenerator; /* unused parameter */
682
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
683
/* use smaller scaler to prevent overflow when we add the dither */
684
float dithered = (*src * (32766.0f)) + dither;
685
PaInt32 samp = (PaInt32) dithered;
686
PA_CLIP_( samp, -0x8000, 0x7FFF );
687
#ifdef PA_USE_C99_LRINTF
688
*dest = lrintf(samp-0.5f);
690
*dest = (PaInt16) samp;
694
dest += destinationStride;
698
/* -------------------------------------------------------------------------- */
700
static void Float32_To_Int8(
701
void *destinationBuffer, signed int destinationStride,
702
void *sourceBuffer, signed int sourceStride,
703
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
705
float *src = (float*)sourceBuffer;
706
signed char *dest = (signed char*)destinationBuffer;
707
(void)ditherGenerator; /* unused parameter */
711
signed char samp = (signed char) (*src * (127.0f));
715
dest += destinationStride;
719
/* -------------------------------------------------------------------------- */
721
static void Float32_To_Int8_Dither(
722
void *destinationBuffer, signed int destinationStride,
723
void *sourceBuffer, signed int sourceStride,
724
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
726
float *src = (float*)sourceBuffer;
727
signed char *dest = (signed char*)destinationBuffer;
728
(void)ditherGenerator; /* unused parameter */
732
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
733
/* use smaller scaler to prevent overflow when we add the dither */
734
float dithered = (*src * (126.0f)) + dither;
735
PaInt32 samp = (PaInt32) dithered;
736
*dest = (signed char) samp;
739
dest += destinationStride;
743
/* -------------------------------------------------------------------------- */
745
static void Float32_To_Int8_Clip(
746
void *destinationBuffer, signed int destinationStride,
747
void *sourceBuffer, signed int sourceStride,
748
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
750
float *src = (float*)sourceBuffer;
751
signed char *dest = (signed char*)destinationBuffer;
752
(void)ditherGenerator; /* unused parameter */
756
PaInt32 samp = (PaInt32)(*src * (127.0f));
757
PA_CLIP_( samp, -0x80, 0x7F );
758
*dest = (signed char) samp;
761
dest += destinationStride;
765
/* -------------------------------------------------------------------------- */
767
static void Float32_To_Int8_DitherClip(
768
void *destinationBuffer, signed int destinationStride,
769
void *sourceBuffer, signed int sourceStride,
770
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
772
float *src = (float*)sourceBuffer;
773
signed char *dest = (signed char*)destinationBuffer;
774
(void)ditherGenerator; /* unused parameter */
778
float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
779
/* use smaller scaler to prevent overflow when we add the dither */
780
float dithered = (*src * (126.0f)) + dither;
781
PaInt32 samp = (PaInt32) dithered;
782
PA_CLIP_( samp, -0x80, 0x7F );
783
*dest = (signed char) samp;
786
dest += destinationStride;
790
/* -------------------------------------------------------------------------- */
792
static void Float32_To_UInt8(
793
void *destinationBuffer, signed int destinationStride,
794
void *sourceBuffer, signed int sourceStride,
795
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
797
float *src = (float*)sourceBuffer;
798
unsigned char *dest = (unsigned char*)destinationBuffer;
799
(void)ditherGenerator; /* unused parameter */
803
unsigned char samp = (unsigned char)(128 + ((unsigned char) (*src * (127.0f))));
807
dest += destinationStride;
811
/* -------------------------------------------------------------------------- */
813
static void Float32_To_UInt8_Dither(
814
void *destinationBuffer, signed int destinationStride,
815
void *sourceBuffer, signed int sourceStride,
816
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
818
float *src = (float*)sourceBuffer;
819
unsigned char *dest = (unsigned char*)destinationBuffer;
820
(void)ditherGenerator; /* unused parameter */
827
dest += destinationStride;
831
/* -------------------------------------------------------------------------- */
833
static void Float32_To_UInt8_Clip(
834
void *destinationBuffer, signed int destinationStride,
835
void *sourceBuffer, signed int sourceStride,
836
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
838
float *src = (float*)sourceBuffer;
839
unsigned char *dest = (unsigned char*)destinationBuffer;
840
(void)ditherGenerator; /* unused parameter */
847
dest += destinationStride;
851
/* -------------------------------------------------------------------------- */
853
static void Float32_To_UInt8_DitherClip(
854
void *destinationBuffer, signed int destinationStride,
855
void *sourceBuffer, signed int sourceStride,
856
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
858
float *src = (float*)sourceBuffer;
859
unsigned char *dest = (unsigned char*)destinationBuffer;
860
(void)ditherGenerator; /* unused parameter */
867
dest += destinationStride;
871
/* -------------------------------------------------------------------------- */
873
static void Int32_To_Float32(
874
void *destinationBuffer, signed int destinationStride,
875
void *sourceBuffer, signed int sourceStride,
876
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
878
PaInt32 *src = (PaInt32*)sourceBuffer;
879
float *dest = (float*)destinationBuffer;
880
(void)ditherGenerator; /* unused parameter */
884
*dest = (float) ((double)*src * const_1_div_2147483648_);
887
dest += destinationStride;
891
/* -------------------------------------------------------------------------- */
893
static void Int32_To_Int24(
894
void *destinationBuffer, signed int destinationStride,
895
void *sourceBuffer, signed int sourceStride,
896
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
898
PaInt32 *src = (PaInt32*)sourceBuffer;
899
unsigned char *dest = (unsigned char*)destinationBuffer;
900
(void) ditherGenerator; /* unused parameter */
905
#if defined(PA_LITTLE_ENDIAN)
906
dest[0] = (unsigned char)(*src >> 8);
907
dest[1] = (unsigned char)(*src >> 16);
908
dest[2] = (unsigned char)(*src >> 24);
909
#elif defined(PA_BIG_ENDIAN)
910
dest[0] = (unsigned char)(*src >> 24);
911
dest[1] = (unsigned char)(*src >> 16);
912
dest[2] = (unsigned char)(*src >> 8);
915
dest += destinationStride * 3;
919
/* -------------------------------------------------------------------------- */
921
static void Int32_To_Int24_Dither(
922
void *destinationBuffer, signed int destinationStride,
923
void *sourceBuffer, signed int sourceStride,
924
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
926
(void) destinationBuffer; /* unused parameters */
927
(void) destinationStride; /* unused parameters */
928
(void) sourceBuffer; /* unused parameters */
929
(void) sourceStride; /* unused parameters */
930
(void) count; /* unused parameters */
931
(void) ditherGenerator; /* unused parameters */
935
/* -------------------------------------------------------------------------- */
937
static void Int32_To_Int16(
938
void *destinationBuffer, signed int destinationStride,
939
void *sourceBuffer, signed int sourceStride,
940
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
942
PaInt32 *src = (PaInt32*)sourceBuffer;
943
PaInt16 *dest = (PaInt16*)destinationBuffer;
944
(void)ditherGenerator; /* unused parameter */
948
*dest = (PaInt16) ((*src) >> 16);
951
dest += destinationStride;
955
/* -------------------------------------------------------------------------- */
957
static void Int32_To_Int16_Dither(
958
void *destinationBuffer, signed int destinationStride,
959
void *sourceBuffer, signed int sourceStride,
960
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
962
PaInt32 *src = (PaInt32*)sourceBuffer;
963
PaInt16 *dest = (PaInt16*)destinationBuffer;
969
dither = PaUtil_Generate16BitTriangularDither( ditherGenerator );
970
*dest = (PaInt16) ((((*src)>>1) + dither) >> 15);
973
dest += destinationStride;
977
/* -------------------------------------------------------------------------- */
979
static void Int32_To_Int8(
980
void *destinationBuffer, signed int destinationStride,
981
void *sourceBuffer, signed int sourceStride,
982
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
984
PaInt32 *src = (PaInt32*)sourceBuffer;
985
signed char *dest = (signed char*)destinationBuffer;
986
(void)ditherGenerator; /* unused parameter */
990
*dest = (signed char) ((*src) >> 24);
993
dest += destinationStride;
997
/* -------------------------------------------------------------------------- */
999
static void Int32_To_Int8_Dither(
1000
void *destinationBuffer, signed int destinationStride,
1001
void *sourceBuffer, signed int sourceStride,
1002
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1004
PaInt32 *src = (PaInt32*)sourceBuffer;
1005
signed char *dest = (signed char*)destinationBuffer;
1011
dither = PaUtil_Generate16BitTriangularDither( ditherGenerator );
1012
*dest = (signed char) ((((*src)>>1) + dither) >> 23);
1014
src += sourceStride;
1015
dest += destinationStride;
1019
/* -------------------------------------------------------------------------- */
1021
static void Int32_To_UInt8(
1022
void *destinationBuffer, signed int destinationStride,
1023
void *sourceBuffer, signed int sourceStride,
1024
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1026
PaInt32 *src = (PaInt32*)sourceBuffer;
1027
unsigned char *dest = (unsigned char*)destinationBuffer;
1028
(void)ditherGenerator; /* unused parameter */
1032
(*dest) = (unsigned char)(((*src) >> 24) + 128);
1034
src += sourceStride;
1035
dest += destinationStride;
1039
/* -------------------------------------------------------------------------- */
1041
static void Int32_To_UInt8_Dither(
1042
void *destinationBuffer, signed int destinationStride,
1043
void *sourceBuffer, signed int sourceStride,
1044
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1046
PaInt32 *src = (PaInt32*)sourceBuffer;
1047
unsigned char *dest = (unsigned char*)destinationBuffer;
1048
(void)ditherGenerator; /* unused parameter */
1054
src += sourceStride;
1055
dest += destinationStride;
1059
/* -------------------------------------------------------------------------- */
1061
static void Int24_To_Float32(
1062
void *destinationBuffer, signed int destinationStride,
1063
void *sourceBuffer, signed int sourceStride,
1064
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1066
unsigned char *src = (unsigned char*)sourceBuffer;
1067
float *dest = (float*)destinationBuffer;
1070
(void) ditherGenerator; /* unused parameter */
1075
#if defined(PA_LITTLE_ENDIAN)
1076
temp = (((long)src[0]) << 8);
1077
temp = temp | (((long)src[1]) << 16);
1078
temp = temp | (((long)src[2]) << 24);
1079
#elif defined(PA_BIG_ENDIAN)
1080
temp = (((long)src[0]) << 24);
1081
temp = temp | (((long)src[1]) << 16);
1082
temp = temp | (((long)src[2]) << 8);
1085
*dest = (float) ((double)temp * const_1_div_2147483648_);
1087
src += sourceStride * 3;
1088
dest += destinationStride;
1092
/* -------------------------------------------------------------------------- */
1094
static void Int24_To_Int32(
1095
void *destinationBuffer, signed int destinationStride,
1096
void *sourceBuffer, signed int sourceStride,
1097
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1099
unsigned char *src = (unsigned char*)sourceBuffer;
1100
PaInt32 *dest = (PaInt32*) destinationBuffer;
1103
(void) ditherGenerator; /* unused parameter */
1108
#if defined(PA_LITTLE_ENDIAN)
1109
temp = (((long)src[0]) << 8);
1110
temp = temp | (((long)src[1]) << 16);
1111
temp = temp | (((long)src[2]) << 24);
1112
#elif defined(PA_BIG_ENDIAN)
1113
temp = (((long)src[0]) << 24);
1114
temp = temp | (((long)src[1]) << 16);
1115
temp = temp | (((long)src[2]) << 8);
1120
src += sourceStride * 3;
1121
dest += destinationStride;
1125
/* -------------------------------------------------------------------------- */
1127
static void Int24_To_Int16(
1128
void *destinationBuffer, signed int destinationStride,
1129
void *sourceBuffer, signed int sourceStride,
1130
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1132
unsigned char *src = (unsigned char*)sourceBuffer;
1133
PaInt16 *dest = (PaInt16*)destinationBuffer;
1137
(void) ditherGenerator; /* unused parameter */
1142
#if defined(PA_LITTLE_ENDIAN)
1143
/* src[0] is discarded */
1144
temp = (((PaInt16)src[1]));
1145
temp = temp | (PaInt16)(((PaInt16)src[2]) << 8);
1146
#elif defined(PA_BIG_ENDIAN)
1147
/* src[2] is discarded */
1148
temp = (PaInt16)(((PaInt16)src[0]) << 8);
1149
temp = temp | (((PaInt16)src[1]));
1154
src += sourceStride * 3;
1155
dest += destinationStride;
1159
/* -------------------------------------------------------------------------- */
1161
static void Int24_To_Int16_Dither(
1162
void *destinationBuffer, signed int destinationStride,
1163
void *sourceBuffer, signed int sourceStride,
1164
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1166
(void) destinationBuffer; /* unused parameters */
1167
(void) destinationStride; /* unused parameters */
1168
(void) sourceBuffer; /* unused parameters */
1169
(void) sourceStride; /* unused parameters */
1170
(void) count; /* unused parameters */
1171
(void) ditherGenerator; /* unused parameters */
1175
/* -------------------------------------------------------------------------- */
1177
static void Int24_To_Int8(
1178
void *destinationBuffer, signed int destinationStride,
1179
void *sourceBuffer, signed int sourceStride,
1180
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1182
unsigned char *src = (unsigned char*)sourceBuffer;
1183
signed char *dest = (signed char*)destinationBuffer;
1185
(void) ditherGenerator; /* unused parameter */
1190
#if defined(PA_LITTLE_ENDIAN)
1191
/* src[0] is discarded */
1192
/* src[1] is discarded */
1194
#elif defined(PA_BIG_ENDIAN)
1195
/* src[2] is discarded */
1196
/* src[1] is discarded */
1200
src += sourceStride * 3;
1201
dest += destinationStride;
1205
/* -------------------------------------------------------------------------- */
1207
static void Int24_To_Int8_Dither(
1208
void *destinationBuffer, signed int destinationStride,
1209
void *sourceBuffer, signed int sourceStride,
1210
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1212
(void) destinationBuffer; /* unused parameters */
1213
(void) destinationStride; /* unused parameters */
1214
(void) sourceBuffer; /* unused parameters */
1215
(void) sourceStride; /* unused parameters */
1216
(void) count; /* unused parameters */
1217
(void) ditherGenerator; /* unused parameters */
1221
/* -------------------------------------------------------------------------- */
1223
static void Int24_To_UInt8(
1224
void *destinationBuffer, signed int destinationStride,
1225
void *sourceBuffer, signed int sourceStride,
1226
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1228
unsigned char *src = (unsigned char*)sourceBuffer;
1229
unsigned char *dest = (unsigned char*)destinationBuffer;
1231
(void) ditherGenerator; /* unused parameter */
1236
#if defined(PA_LITTLE_ENDIAN)
1237
/* src[0] is discarded */
1238
/* src[1] is discarded */
1239
*dest = (unsigned char)(src[2] + 128);
1240
#elif defined(PA_BIG_ENDIAN)
1241
*dest = (unsigned char)(src[0] + 128);
1242
/* src[1] is discarded */
1243
/* src[2] is discarded */
1246
src += sourceStride * 3;
1247
dest += destinationStride;
1251
/* -------------------------------------------------------------------------- */
1253
static void Int24_To_UInt8_Dither(
1254
void *destinationBuffer, signed int destinationStride,
1255
void *sourceBuffer, signed int sourceStride,
1256
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1258
(void) destinationBuffer; /* unused parameters */
1259
(void) destinationStride; /* unused parameters */
1260
(void) sourceBuffer; /* unused parameters */
1261
(void) sourceStride; /* unused parameters */
1262
(void) count; /* unused parameters */
1263
(void) ditherGenerator; /* unused parameters */
1267
/* -------------------------------------------------------------------------- */
1269
static void Int16_To_Float32(
1270
void *destinationBuffer, signed int destinationStride,
1271
void *sourceBuffer, signed int sourceStride,
1272
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1274
PaInt16 *src = (PaInt16*)sourceBuffer;
1275
float *dest = (float*)destinationBuffer;
1276
(void)ditherGenerator; /* unused parameter */
1280
float samp = *src * const_1_div_32768_; /* FIXME: i'm concerned about this being asymetrical with float->int16 -rb */
1283
src += sourceStride;
1284
dest += destinationStride;
1288
/* -------------------------------------------------------------------------- */
1290
static void Int16_To_Int32(
1291
void *destinationBuffer, signed int destinationStride,
1292
void *sourceBuffer, signed int sourceStride,
1293
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1295
PaInt16 *src = (PaInt16*)sourceBuffer;
1296
PaInt32 *dest = (PaInt32*)destinationBuffer;
1297
(void)ditherGenerator; /* unused parameter */
1301
/* REVIEW: we should consider something like
1302
(*src << 16) | (*src & 0xFFFF)
1307
src += sourceStride;
1308
dest += destinationStride;
1312
/* -------------------------------------------------------------------------- */
1314
static void Int16_To_Int24(
1315
void *destinationBuffer, signed int destinationStride,
1316
void *sourceBuffer, signed int sourceStride,
1317
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1319
PaInt16 *src = (PaInt16*) sourceBuffer;
1320
unsigned char *dest = (unsigned char*)destinationBuffer;
1323
(void) ditherGenerator; /* unused parameter */
1329
#if defined(PA_LITTLE_ENDIAN)
1331
dest[1] = (unsigned char)(temp);
1332
dest[2] = (unsigned char)(temp >> 8);
1333
#elif defined(PA_BIG_ENDIAN)
1334
dest[0] = (unsigned char)(temp >> 8);
1335
dest[1] = (unsigned char)(temp);
1339
src += sourceStride;
1340
dest += destinationStride * 3;
1344
/* -------------------------------------------------------------------------- */
1346
static void Int16_To_Int8(
1347
void *destinationBuffer, signed int destinationStride,
1348
void *sourceBuffer, signed int sourceStride,
1349
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1351
PaInt16 *src = (PaInt16*)sourceBuffer;
1352
signed char *dest = (signed char*)destinationBuffer;
1353
(void)ditherGenerator; /* unused parameter */
1357
(*dest) = (signed char)((*src) >> 8);
1359
src += sourceStride;
1360
dest += destinationStride;
1364
/* -------------------------------------------------------------------------- */
1366
static void Int16_To_Int8_Dither(
1367
void *destinationBuffer, signed int destinationStride,
1368
void *sourceBuffer, signed int sourceStride,
1369
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1371
PaInt16 *src = (PaInt16*)sourceBuffer;
1372
signed char *dest = (signed char*)destinationBuffer;
1373
(void)ditherGenerator; /* unused parameter */
1379
src += sourceStride;
1380
dest += destinationStride;
1384
/* -------------------------------------------------------------------------- */
1386
static void Int16_To_UInt8(
1387
void *destinationBuffer, signed int destinationStride,
1388
void *sourceBuffer, signed int sourceStride,
1389
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1391
PaInt16 *src = (PaInt16*)sourceBuffer;
1392
unsigned char *dest = (unsigned char*)destinationBuffer;
1393
(void)ditherGenerator; /* unused parameter */
1397
(*dest) = (unsigned char)(((*src) >> 8) + 128);
1399
src += sourceStride;
1400
dest += destinationStride;
1404
/* -------------------------------------------------------------------------- */
1406
static void Int16_To_UInt8_Dither(
1407
void *destinationBuffer, signed int destinationStride,
1408
void *sourceBuffer, signed int sourceStride,
1409
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1411
PaInt16 *src = (PaInt16*)sourceBuffer;
1412
unsigned char *dest = (unsigned char*)destinationBuffer;
1413
(void)ditherGenerator; /* unused parameter */
1419
src += sourceStride;
1420
dest += destinationStride;
1424
/* -------------------------------------------------------------------------- */
1426
static void Int8_To_Float32(
1427
void *destinationBuffer, signed int destinationStride,
1428
void *sourceBuffer, signed int sourceStride,
1429
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1431
signed char *src = (signed char*)sourceBuffer;
1432
float *dest = (float*)destinationBuffer;
1433
(void)ditherGenerator; /* unused parameter */
1437
float samp = *src * const_1_div_128_;
1440
src += sourceStride;
1441
dest += destinationStride;
1445
/* -------------------------------------------------------------------------- */
1447
static void Int8_To_Int32(
1448
void *destinationBuffer, signed int destinationStride,
1449
void *sourceBuffer, signed int sourceStride,
1450
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1452
signed char *src = (signed char*)sourceBuffer;
1453
PaInt32 *dest = (PaInt32*)destinationBuffer;
1454
(void)ditherGenerator; /* unused parameter */
1458
(*dest) = (*src) << 24;
1460
src += sourceStride;
1461
dest += destinationStride;
1465
/* -------------------------------------------------------------------------- */
1467
static void Int8_To_Int24(
1468
void *destinationBuffer, signed int destinationStride,
1469
void *sourceBuffer, signed int sourceStride,
1470
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1472
signed char *src = (signed char*)sourceBuffer;
1473
unsigned char *dest = (unsigned char*)destinationBuffer;
1474
(void)ditherGenerator; /* unused parameter */
1479
#if defined(PA_LITTLE_ENDIAN)
1483
#elif defined(PA_BIG_ENDIAN)
1489
src += sourceStride;
1490
dest += destinationStride * 3;
1494
/* -------------------------------------------------------------------------- */
1496
static void Int8_To_Int16(
1497
void *destinationBuffer, signed int destinationStride,
1498
void *sourceBuffer, signed int sourceStride,
1499
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1501
signed char *src = (signed char*)sourceBuffer;
1502
PaInt16 *dest = (PaInt16*)destinationBuffer;
1503
(void)ditherGenerator; /* unused parameter */
1507
(*dest) = (PaInt16)((*src) << 8);
1509
src += sourceStride;
1510
dest += destinationStride;
1514
/* -------------------------------------------------------------------------- */
1516
static void Int8_To_UInt8(
1517
void *destinationBuffer, signed int destinationStride,
1518
void *sourceBuffer, signed int sourceStride,
1519
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1521
signed char *src = (signed char*)sourceBuffer;
1522
unsigned char *dest = (unsigned char*)destinationBuffer;
1523
(void)ditherGenerator; /* unused parameter */
1527
(*dest) = (unsigned char)(*src + 128);
1529
src += sourceStride;
1530
dest += destinationStride;
1534
/* -------------------------------------------------------------------------- */
1536
static void UInt8_To_Float32(
1537
void *destinationBuffer, signed int destinationStride,
1538
void *sourceBuffer, signed int sourceStride,
1539
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1541
unsigned char *src = (unsigned char*)sourceBuffer;
1542
float *dest = (float*)destinationBuffer;
1543
(void)ditherGenerator; /* unused parameter */
1547
float samp = (*src - 128) * const_1_div_128_;
1550
src += sourceStride;
1551
dest += destinationStride;
1555
/* -------------------------------------------------------------------------- */
1557
static void UInt8_To_Int32(
1558
void *destinationBuffer, signed int destinationStride,
1559
void *sourceBuffer, signed int sourceStride,
1560
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1562
unsigned char *src = (unsigned char*)sourceBuffer;
1563
PaInt32 *dest = (PaInt32*)destinationBuffer;
1564
(void)ditherGenerator; /* unused parameter */
1568
(*dest) = (*src - 128) << 24;
1570
src += sourceStride;
1571
dest += destinationStride;
1575
/* -------------------------------------------------------------------------- */
1577
static void UInt8_To_Int24(
1578
void *destinationBuffer, signed int destinationStride,
1579
void *sourceBuffer, signed int sourceStride,
1580
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1582
unsigned char *src = (unsigned char*)sourceBuffer;
1583
unsigned char *dest = (unsigned char*)destinationBuffer;
1584
(void) ditherGenerator; /* unused parameters */
1589
#if defined(PA_LITTLE_ENDIAN)
1592
dest[2] = (unsigned char)(*src - 128);
1593
#elif defined(PA_BIG_ENDIAN)
1594
dest[0] = (unsigned char)(*src - 128);
1599
src += sourceStride;
1600
dest += destinationStride * 3;
1604
/* -------------------------------------------------------------------------- */
1606
static void UInt8_To_Int16(
1607
void *destinationBuffer, signed int destinationStride,
1608
void *sourceBuffer, signed int sourceStride,
1609
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1611
unsigned char *src = (unsigned char*)sourceBuffer;
1612
PaInt16 *dest = (PaInt16*)destinationBuffer;
1613
(void)ditherGenerator; /* unused parameter */
1617
(*dest) = (PaInt16)((*src - 128) << 8);
1619
src += sourceStride;
1620
dest += destinationStride;
1624
/* -------------------------------------------------------------------------- */
1626
static void UInt8_To_Int8(
1627
void *destinationBuffer, signed int destinationStride,
1628
void *sourceBuffer, signed int sourceStride,
1629
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1631
unsigned char *src = (unsigned char*)sourceBuffer;
1632
signed char *dest = (signed char*)destinationBuffer;
1633
(void)ditherGenerator; /* unused parameter */
1637
(*dest) = (signed char)(*src - 128);
1639
src += sourceStride;
1640
dest += destinationStride;
1644
/* -------------------------------------------------------------------------- */
1646
static void Copy_8_To_8(
1647
void *destinationBuffer, signed int destinationStride,
1648
void *sourceBuffer, signed int sourceStride,
1649
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1651
unsigned char *src = (unsigned char*)sourceBuffer;
1652
unsigned char *dest = (unsigned char*)destinationBuffer;
1654
(void) ditherGenerator; /* unused parameter */
1660
src += sourceStride;
1661
dest += destinationStride;
1665
/* -------------------------------------------------------------------------- */
1667
static void Copy_16_To_16(
1668
void *destinationBuffer, signed int destinationStride,
1669
void *sourceBuffer, signed int sourceStride,
1670
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1672
PaUint16 *src = (PaUint16 *)sourceBuffer;
1673
PaUint16 *dest = (PaUint16 *)destinationBuffer;
1675
(void) ditherGenerator; /* unused parameter */
1681
src += sourceStride;
1682
dest += destinationStride;
1686
/* -------------------------------------------------------------------------- */
1688
static void Copy_24_To_24(
1689
void *destinationBuffer, signed int destinationStride,
1690
void *sourceBuffer, signed int sourceStride,
1691
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1693
unsigned char *src = (unsigned char*)sourceBuffer;
1694
unsigned char *dest = (unsigned char*)destinationBuffer;
1696
(void) ditherGenerator; /* unused parameter */
1704
src += sourceStride * 3;
1705
dest += destinationStride * 3;
1709
/* -------------------------------------------------------------------------- */
1711
static void Copy_32_To_32(
1712
void *destinationBuffer, signed int destinationStride,
1713
void *sourceBuffer, signed int sourceStride,
1714
unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator )
1716
PaUint32 *dest = (PaUint32 *)destinationBuffer;
1717
PaUint32 *src = (PaUint32 *)sourceBuffer;
1719
(void) ditherGenerator; /* unused parameter */
1725
src += sourceStride;
1726
dest += destinationStride;
1730
/* -------------------------------------------------------------------------- */
1732
PaUtilConverterTable paConverters = {
1733
Float32_To_Int32, /* PaUtilConverter *Float32_To_Int32; */
1734
Float32_To_Int32_Dither, /* PaUtilConverter *Float32_To_Int32_Dither; */
1735
Float32_To_Int32_Clip, /* PaUtilConverter *Float32_To_Int32_Clip; */
1736
Float32_To_Int32_DitherClip, /* PaUtilConverter *Float32_To_Int32_DitherClip; */
1738
Float32_To_Int24, /* PaUtilConverter *Float32_To_Int24; */
1739
Float32_To_Int24_Dither, /* PaUtilConverter *Float32_To_Int24_Dither; */
1740
Float32_To_Int24_Clip, /* PaUtilConverter *Float32_To_Int24_Clip; */
1741
Float32_To_Int24_DitherClip, /* PaUtilConverter *Float32_To_Int24_DitherClip; */
1743
Float32_To_Int16, /* PaUtilConverter *Float32_To_Int16; */
1744
Float32_To_Int16_Dither, /* PaUtilConverter *Float32_To_Int16_Dither; */
1745
Float32_To_Int16_Clip, /* PaUtilConverter *Float32_To_Int16_Clip; */
1746
Float32_To_Int16_DitherClip, /* PaUtilConverter *Float32_To_Int16_DitherClip; */
1748
Float32_To_Int8, /* PaUtilConverter *Float32_To_Int8; */
1749
Float32_To_Int8_Dither, /* PaUtilConverter *Float32_To_Int8_Dither; */
1750
Float32_To_Int8_Clip, /* PaUtilConverter *Float32_To_Int8_Clip; */
1751
Float32_To_Int8_DitherClip, /* PaUtilConverter *Float32_To_Int8_DitherClip; */
1753
Float32_To_UInt8, /* PaUtilConverter *Float32_To_UInt8; */
1754
Float32_To_UInt8_Dither, /* PaUtilConverter *Float32_To_UInt8_Dither; */
1755
Float32_To_UInt8_Clip, /* PaUtilConverter *Float32_To_UInt8_Clip; */
1756
Float32_To_UInt8_DitherClip, /* PaUtilConverter *Float32_To_UInt8_DitherClip; */
1758
Int32_To_Float32, /* PaUtilConverter *Int32_To_Float32; */
1759
Int32_To_Int24, /* PaUtilConverter *Int32_To_Int24; */
1760
Int32_To_Int24_Dither, /* PaUtilConverter *Int32_To_Int24_Dither; */
1761
Int32_To_Int16, /* PaUtilConverter *Int32_To_Int16; */
1762
Int32_To_Int16_Dither, /* PaUtilConverter *Int32_To_Int16_Dither; */
1763
Int32_To_Int8, /* PaUtilConverter *Int32_To_Int8; */
1764
Int32_To_Int8_Dither, /* PaUtilConverter *Int32_To_Int8_Dither; */
1765
Int32_To_UInt8, /* PaUtilConverter *Int32_To_UInt8; */
1766
Int32_To_UInt8_Dither, /* PaUtilConverter *Int32_To_UInt8_Dither; */
1768
Int24_To_Float32, /* PaUtilConverter *Int24_To_Float32; */
1769
Int24_To_Int32, /* PaUtilConverter *Int24_To_Int32; */
1770
Int24_To_Int16, /* PaUtilConverter *Int24_To_Int16; */
1771
Int24_To_Int16_Dither, /* PaUtilConverter *Int24_To_Int16_Dither; */
1772
Int24_To_Int8, /* PaUtilConverter *Int24_To_Int8; */
1773
Int24_To_Int8_Dither, /* PaUtilConverter *Int24_To_Int8_Dither; */
1774
Int24_To_UInt8, /* PaUtilConverter *Int24_To_UInt8; */
1775
Int24_To_UInt8_Dither, /* PaUtilConverter *Int24_To_UInt8_Dither; */
1777
Int16_To_Float32, /* PaUtilConverter *Int16_To_Float32; */
1778
Int16_To_Int32, /* PaUtilConverter *Int16_To_Int32; */
1779
Int16_To_Int24, /* PaUtilConverter *Int16_To_Int24; */
1780
Int16_To_Int8, /* PaUtilConverter *Int16_To_Int8; */
1781
Int16_To_Int8_Dither, /* PaUtilConverter *Int16_To_Int8_Dither; */
1782
Int16_To_UInt8, /* PaUtilConverter *Int16_To_UInt8; */
1783
Int16_To_UInt8_Dither, /* PaUtilConverter *Int16_To_UInt8_Dither; */
1785
Int8_To_Float32, /* PaUtilConverter *Int8_To_Float32; */
1786
Int8_To_Int32, /* PaUtilConverter *Int8_To_Int32; */
1787
Int8_To_Int24, /* PaUtilConverter *Int8_To_Int24 */
1788
Int8_To_Int16, /* PaUtilConverter *Int8_To_Int16; */
1789
Int8_To_UInt8, /* PaUtilConverter *Int8_To_UInt8; */
1791
UInt8_To_Float32, /* PaUtilConverter *UInt8_To_Float32; */
1792
UInt8_To_Int32, /* PaUtilConverter *UInt8_To_Int32; */
1793
UInt8_To_Int24, /* PaUtilConverter *UInt8_To_Int24; */
1794
UInt8_To_Int16, /* PaUtilConverter *UInt8_To_Int16; */
1795
UInt8_To_Int8, /* PaUtilConverter *UInt8_To_Int8; */
1797
Copy_8_To_8, /* PaUtilConverter *Copy_8_To_8; */
1798
Copy_16_To_16, /* PaUtilConverter *Copy_16_To_16; */
1799
Copy_24_To_24, /* PaUtilConverter *Copy_24_To_24; */
1800
Copy_32_To_32 /* PaUtilConverter *Copy_32_To_32; */
1803
/* -------------------------------------------------------------------------- */
1805
#endif /* PA_NO_STANDARD_CONVERTERS */
1807
/* -------------------------------------------------------------------------- */
1809
PaUtilZeroer* PaUtil_SelectZeroer( PaSampleFormat destinationFormat )
1811
switch( destinationFormat & ~paNonInterleaved ){
1813
return paZeroers.Zero32;
1815
return paZeroers.Zero32;
1817
return paZeroers.Zero24;
1819
return paZeroers.Zero16;
1821
return paZeroers.Zero8;
1823
return paZeroers.ZeroU8;
1828
/* -------------------------------------------------------------------------- */
1830
#ifdef PA_NO_STANDARD_ZEROERS
1832
/* -------------------------------------------------------------------------- */
1834
PaUtilZeroerTable paZeroers = {
1835
0, /* PaUtilZeroer *ZeroU8; */
1836
0, /* PaUtilZeroer *Zero8; */
1837
0, /* PaUtilZeroer *Zero16; */
1838
0, /* PaUtilZeroer *Zero24; */
1839
0, /* PaUtilZeroer *Zero32; */
1842
/* -------------------------------------------------------------------------- */
1844
#else /* PA_NO_STANDARD_ZEROERS is not defined */
1846
/* -------------------------------------------------------------------------- */
1848
static void ZeroU8( void *destinationBuffer, signed int destinationStride,
1849
unsigned int count )
1851
unsigned char *dest = (unsigned char*)destinationBuffer;
1857
dest += destinationStride;
1861
/* -------------------------------------------------------------------------- */
1863
static void Zero8( void *destinationBuffer, signed int destinationStride,
1864
unsigned int count )
1866
unsigned char *dest = (unsigned char*)destinationBuffer;
1872
dest += destinationStride;
1876
/* -------------------------------------------------------------------------- */
1878
static void Zero16( void *destinationBuffer, signed int destinationStride,
1879
unsigned int count )
1881
PaUint16 *dest = (PaUint16 *)destinationBuffer;
1887
dest += destinationStride;
1891
/* -------------------------------------------------------------------------- */
1893
static void Zero24( void *destinationBuffer, signed int destinationStride,
1894
unsigned int count )
1896
unsigned char *dest = (unsigned char*)destinationBuffer;
1904
dest += destinationStride * 3;
1908
/* -------------------------------------------------------------------------- */
1910
static void Zero32( void *destinationBuffer, signed int destinationStride,
1911
unsigned int count )
1913
PaUint32 *dest = (PaUint32 *)destinationBuffer;
1919
dest += destinationStride;
1923
/* -------------------------------------------------------------------------- */
1925
PaUtilZeroerTable paZeroers = {
1926
ZeroU8, /* PaUtilZeroer *ZeroU8; */
1927
Zero8, /* PaUtilZeroer *Zero8; */
1928
Zero16, /* PaUtilZeroer *Zero16; */
1929
Zero24, /* PaUtilZeroer *Zero24; */
1930
Zero32, /* PaUtilZeroer *Zero32; */
1933
/* -------------------------------------------------------------------------- */
1935
#endif /* PA_NO_STANDARD_ZEROERS */