e921fb88a1acf454c39d52d7a4b1f65e42d52f7f
[platform/upstream/gst-plugins-good.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 #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
29 #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
30 #define CLIP 32635
31
32 void
33 mulaw_encode(gint16* in, guint8* out, gint numsamples)
34 {
35     static gint16 exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
36                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
37                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
38                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
39                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
40                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
44                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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     gint16 sign, exponent, mantissa,i;
52     gint16 sample;
53     guint8 ulawbyte;
54
55     for(i=0;i<numsamples;i++) {
56       sample=in[i];
57       /** get the sample into sign-magnitude **/
58       sign = (sample >> 8) & 0x80;        /* set aside the sign */
59       if (sign != 0) sample = -sample;    /* get magnitude */
60       if (sample > CLIP) sample = CLIP;   /* clip the magnitude */
61       /** convert from 16 bit linear to ulaw **/
62       sample = sample + BIAS;
63       exponent = exp_lut[(sample>>7) & 0xFF];
64       mantissa = (sample >> (exponent+3)) & 0x0F;
65       ulawbyte = ~(sign | (exponent << 4) | mantissa);
66 #ifdef ZEROTRAP
67       if (ulawbyte == 0 ) ulawbyte = 0x02;  /* optional CCITT trap */
68 #endif
69       out[i]=ulawbyte;
70     }
71 }
72
73 /*
74  * This routine converts from ulaw to 16 bit linear
75  * 29 September 1989
76  *
77  * Craig Reese: IDA/Supercomputing Research Center
78  *
79  * References:
80  * 1) CCITT Recommendation G.711  (very difficult to follow)
81  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
82  *     for Analog-to_Digital Conversion Techniques,"
83  *     17 February 1987
84  *
85  * Input: 8 bit ulaw sample
86  * Output: signed 16 bit linear sample
87  */
88
89 void
90 mulaw_decode(guint8* in,gint16* out,gint numsamples)
91 {
92     static gint16 exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
93     gint16 sign, exponent, mantissa;
94     guint8 ulawbyte;
95     gint16 linear,i;
96     for(i=0;i<numsamples;i++) {
97       ulawbyte=in[i];
98       ulawbyte = ~ulawbyte;
99       sign = (ulawbyte & 0x80);
100       exponent = (ulawbyte >> 4) & 0x07;
101       mantissa = ulawbyte & 0x0F;
102       linear = exp_lut[exponent] + (mantissa << (exponent+3));
103       if (sign != 0) linear = -linear;
104       out[i]=linear;
105     }
106 }