mulawdec: fix integer overrun
[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 #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;
55   gint16 sample;
56   guint8 ulawbyte;
57   gint i;
58
59   for (i = 0; i < numsamples; i++) {
60     sample = in[i];
61       /** get the sample into sign-magnitude **/
62     sign = (sample >> 8) & 0x80;        /* set aside the sign */
63     if (sign != 0) {
64       sample = -sample;         /* get magnitude */
65     }
66     /* sample can be zero because we can overflow in the inversion,
67      * checking against the unsigned version solves this */
68     if (((guint16) sample) > CLIP)
69       sample = CLIP;            /* clip the magnitude */
70
71       /** convert from 16 bit linear to ulaw **/
72     sample = sample + BIAS;
73     exponent = exp_lut[(sample >> 7) & 0xFF];
74     mantissa = (sample >> (exponent + 3)) & 0x0F;
75     ulawbyte = ~(sign | (exponent << 4) | mantissa);
76 #ifdef ZEROTRAP
77     if (ulawbyte == 0)
78       ulawbyte = 0x02;          /* optional CCITT trap */
79 #endif
80     out[i] = ulawbyte;
81   }
82 }
83
84 /*
85  * This routine converts from ulaw to 16 bit linear
86  * 29 September 1989
87  *
88  * Craig Reese: IDA/Supercomputing Research Center
89  *
90  * References:
91  * 1) CCITT Recommendation G.711  (very difficult to follow)
92  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
93  *     for Analog-to_Digital Conversion Techniques,"
94  *     17 February 1987
95  *
96  * Input: 8 bit ulaw sample
97  * Output: signed 16 bit linear sample
98  */
99
100 void
101 mulaw_decode (guint8 * in, gint16 * out, gint numsamples)
102 {
103   static gint16 exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
104   gint16 sign, exponent, mantissa;
105   guint8 ulawbyte;
106   gint16 linear;
107   gint i;
108
109   for (i = 0; i < numsamples; i++) {
110     ulawbyte = in[i];
111     ulawbyte = ~ulawbyte;
112     sign = (ulawbyte & 0x80);
113     exponent = (ulawbyte >> 4) & 0x07;
114     mantissa = ulawbyte & 0x0F;
115     linear = exp_lut[exponent] + (mantissa << (exponent + 3));
116     if (sign != 0)
117       linear = -linear;
118     out[i] = linear;
119   }
120 }