Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / filebaseulaw.C
blob92a3cb3783613cbcf822e7b02e62f0371687d009
1 #include "assets.h"
2 #include "byteorder.h"
3 #include "file.h"
4 #include "filebase.h"
5 #include "sizes.h"
7 // ======================================= ulaw codecs
9 float FileBase::ulawtofloat(char ulaw)
11 //printf("%f\n", ulawtofloat_ptr[ulaw]);
12         return ulawtofloat_ptr[(unsigned char)ulaw];
15 char FileBase::floattoulaw(float value)
17         return floattoulaw_ptr[(int)(value * 32767)];
20 // turn off the trap as per the MIL-STD
21 #undef ZEROTRAP
22 // define the add-in bias for 16 bit samples
23 #define uBIAS 0x84
24 #define uCLIP 32635
26 int FileBase::generate_ulaw_tables()
28         int i;
29         float value;
31         if(!ulawtofloat_table)
32         {
33         static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
34         int sign, exponent, mantissa, sample;
35                 unsigned char ulawbyte;
37                 ulawtofloat_table = new float[256];
38                 ulawtofloat_ptr = ulawtofloat_table;
39                 for(i = 0; i < 256; i++)
40                 {
41                         ulawbyte = (unsigned char)i;
42                 ulawbyte = ~ ulawbyte;
43                 sign = ( ulawbyte & 0x80 );
44                 exponent = ( ulawbyte >> 4 ) & 0x07;
45                 mantissa = ulawbyte & 0x0F;
46                 sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
47                 if ( sign != 0 ) sample = -sample;
49                         ulawtofloat_ptr[(int)i] = (float)sample / 32768;
50                 }
51         }
53         if(!floattoulaw_table)
54         {
55         int sign, exponent, mantissa;
56         unsigned char ulawbyte;
57                 int sample;
58         int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
59                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
60                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
61                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
62                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
63                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
64                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
65                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
66                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
67                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
68                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
69                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
70                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
71                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
72                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
73                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
75                 floattoulaw_table = new unsigned char[65536];
76                 floattoulaw_ptr = floattoulaw_table + 32768;
78                 for(i = -32768; i < 32768; i++)
79                 {
80                         sample = i;
81 // Get the sample into sign-magnitude.
82                 sign = (sample >> 8) & 0x80;            // set aside the sign
83                 if ( sign != 0 ) sample = -sample;              // get magnitude
84                 if ( sample > uCLIP ) sample = uCLIP;           // clip the magnitude
86 // Convert from 16 bit linear to ulaw.
87                 sample = sample + uBIAS;
88                     exponent = exp_lut[( sample >> 7 ) & 0xFF];
89                     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
90                     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
91 #ifdef ZEROTRAP
92                     if ( ulawbyte == 0 ) ulawbyte = 0x02;       /* optional CCITT trap */
93 #endif
95                     floattoulaw_ptr[i] = ulawbyte;
96                 }
97         }
98         return 0;
101 int FileBase::delete_ulaw_tables()
103         if(floattoulaw_table) delete [] floattoulaw_table;
104         if(ulawtofloat_table) delete [] ulawtofloat_table;
105         floattoulaw_table = 0;
106         ulawtofloat_table = 0;
107         return 0;
110 //      Local Variables:
111 //      mode: C++
112 //      c-file-style: "linux"
113 //      End: