rework set default transtion, nested proxy edl fixes, doubleclick proxy media fix...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filebaseulaw.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "assets.h"
23 #include "byteorder.h"
24 #include "file.h"
25 #include "filebase.h"
26 #include "sizes.h"
27
28 // ======================================= ulaw codecs
29
30 float FileBase::ulawtofloat(char ulaw)
31 {
32 //printf("%f\n", ulawtofloat_ptr[ulaw]);
33         return ulawtofloat_ptr[(unsigned char)ulaw];
34 }
35
36 char FileBase::floattoulaw(float value)
37 {
38         return floattoulaw_ptr[(int)(value * 32767)];
39 }
40
41 // turn off the trap as per the MIL-STD
42 #undef ZEROTRAP
43 // define the add-in bias for 16 bit samples
44 #define uBIAS 0x84
45 #define uCLIP 32635
46
47 int FileBase::generate_ulaw_tables()
48 {
49         if( !ulawtofloat_table )
50         {
51                 static int exp_lut[8] = {
52                         0, 132, 396, 924, 1980, 4092, 8316, 16764
53                 };
54                 int sign, exponent, mantissa, sample;
55                 unsigned char ulawbyte;
56
57                 ulawtofloat_table = new float[256];
58                 ulawtofloat_ptr = ulawtofloat_table;
59                 for(int i = 0; i < 256; i++)
60                 {
61                         ulawbyte = (unsigned char)i;
62                         ulawbyte = ~ ulawbyte;
63                         sign = ( ulawbyte & 0x80 );
64                         exponent = ( ulawbyte >> 4 ) & 0x07;
65                         mantissa = ulawbyte & 0x0F;
66                         sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
67                         if ( sign != 0 ) sample = -sample;
68                         ulawtofloat_ptr[(int)i] = (float)sample / 32768;
69                 }
70         }
71
72         if( !floattoulaw_table )
73         {
74                 int sign, exponent, mantissa;
75                 unsigned char ulawbyte;
76                 int sample;
77                 int exp_lut[256] = {
78                         0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
79                         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
80                         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
81                         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
82                         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
83                         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
84                         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
85                         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
86                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
87                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
88                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
89                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
90                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
91                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
92                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
93                         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
94                 };
95
96                 floattoulaw_table = new unsigned char[65536];
97                 floattoulaw_ptr = floattoulaw_table + 32768;
98
99                 for(int i = -32768; i < 32768; i++)
100                 {
101                         sample = i;
102 // Get the sample into sign-magnitude.
103                         sign = (sample >> 8) & 0x80;            // set aside the sign
104                         if ( sign != 0 ) sample = -sample;              // get magnitude
105                         if ( sample > uCLIP ) sample = uCLIP;           // clip the magnitude
106 // Convert from 16 bit linear to ulaw.
107                         sample = sample + uBIAS;
108                         exponent = exp_lut[( sample >> 7 ) & 0xFF];
109                         mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
110                         ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
111 #ifdef ZEROTRAP
112                         if ( ulawbyte == 0 ) ulawbyte = 0x02;   /* optional CCITT trap */
113 #endif
114                         floattoulaw_ptr[i] = ulawbyte;
115                 }
116         }
117         return 0;
118 }
119
120 int FileBase::delete_ulaw_tables()
121 {
122         if(floattoulaw_table) delete [] floattoulaw_table;
123         if(ulawtofloat_table) delete [] ulawtofloat_table;
124         floattoulaw_table = 0;
125         ulawtofloat_table = 0;
126         return 0;
127 }