Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / law / mulaw-conversion.c
1 /*
2  * This routine converts from linear to ulaw
3  * 29 September 1989
4  *
5  * Craig Reese: IDA/Supercomputing Research Center
6  * Joe Campbell: Department of Defense
7  *
8  * References:
9  * 1) CCITT Recommendation G.711  (very difficult to follow)
10  * 2) "A New Digital Technique for Implementation of Any 
11  *     Continuous PCM Companding Law," Villeret, Michel,
12  *     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
13  *     1973, pg. 11.12-11.17
14  * 3) MIL-STD-188-113,"Interoperability and Performance Standards
15  *     for Analog-to_Digital Conversion Techniques,"
16  *     17 February 1987
17  *
18  * Input: Signed 16 bit linear sample
19  * Output: 8 bit ulaw sample
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27
28 #include "mulaw-conversion.h"
29
30 #undef ZEROTRAP                 /* turn on the trap as per the MIL-STD */
31 #define BIAS 0x84               /* define the add-in bias for 16 bit samples */
32 #define CLIP 32635
33
34 void
35 mulaw_encode (gint16 * in, guint8 * out, gint numsamples)
36 {
37   static gint16 exp_lut[256] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
38     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
39     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
40     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
41     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
42     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
43     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
44     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
45     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
46     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
47     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
48     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
49     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
51     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
52     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
53   };
54   gint16 sign, exponent, mantissa, i;
55   gint16 sample;
56   guint8 ulawbyte;
57
58   for (i = 0; i < numsamples; i++) {
59     sample = in[i];
60       /** get the sample into sign-magnitude **/
61     sign = (sample >> 8) & 0x80;        /* set aside the sign */
62     if (sign != 0) {
63       sample = -sample;         /* get magnitude */
64     }
65     /* sample can be zero because we can overflow in the inversion,
66      * checking against the unsigned version solves this */
67     if (((guint16) sample) > CLIP)
68       sample = CLIP;            /* clip the magnitude */
69
70       /** convert from 16 bit linear to ulaw **/
71     sample = sample + BIAS;
72     exponent = exp_lut[(sample >> 7) & 0xFF];
73     mantissa = (sample >> (exponent + 3)) & 0x0F;
74     ulawbyte = ~(sign | (exponent << 4) | mantissa);
75 #ifdef ZEROTRAP
76     if (ulawbyte == 0)
77       ulawbyte = 0x02;          /* optional CCITT trap */
78 #endif
79     out[i] = ulawbyte;
80   }
81 }
82
83 /*
84  * This routine converts from ulaw to 16 bit linear
85  * 29 September 1989
86  *
87  * Craig Reese: IDA/Supercomputing Research Center
88  *
89  * References:
90  * 1) CCITT Recommendation G.711  (very difficult to follow)
91  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
92  *     for Analog-to_Digital Conversion Techniques,"
93  *     17 February 1987
94  *
95  * Input: 8 bit ulaw sample
96  * Output: signed 16 bit linear sample
97  */
98
99 void
100 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
101 {
102   static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
103   gint16 sign, exponent, mantissa;
104   guint8 ulawbyte;
105   gint16 linear, i;
106
107   for (i = 0; i < numsamples; i++) {
108     ulawbyte = in[i];
109     ulawbyte = ~ulawbyte;
110     sign = (ulawbyte & 0x80);
111     exponent = (ulawbyte >> 4) & 0x07;
112     mantissa = ulawbyte & 0x0F;
113     linear = exp_lut[exponent] + (mantissa << (exponent + 3));
114     if (sign != 0)
115       linear = -linear;
116     out[i] = linear;
117   }
118 }