github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/g711/decode.go (about) 1 // Package g711 contains utilities to work with the G711 codec. 2 package g711 3 4 var mulawTable = func() [256]uint16 { 5 var ret [256]uint16 6 for i := 0; i < 256; i++ { 7 v := ^i 8 9 tmp := (((uint16(v) & 0x0F) << 3) + 0x84) << ((v & 0x70) >> 4) 10 11 if (v & 0x80) != 0 { 12 ret[i] = 0x84 - tmp 13 } else { 14 ret[i] = tmp - 0x84 15 } 16 } 17 return ret 18 }() 19 20 var alawTable = func() [256]uint16 { 21 var ret [256]uint16 22 for i := 0; i < 256; i++ { 23 v := i ^ 0x55 24 25 t := uint16(v) & 0x0F 26 seg := (uint16(v) & 0x70) >> 4 27 28 if seg != 0 { 29 t = (t*2 + 1 + 32) << (seg + 2) 30 } else { 31 t = (t*2 + 1) << 3 32 } 33 34 if (v & 0x80) != 0 { 35 ret[i] = t 36 } else { 37 ret[i] = -t 38 } 39 } 40 return ret 41 }() 42 43 // DecodeMulaw decodes 8-bit G711 samples (MU-law) into 16-bit LPCM samples. 44 func DecodeMulaw(in []byte) []byte { 45 out := make([]byte, len(in)*2) 46 for i, sample := range in { 47 out[i*2] = uint8(mulawTable[sample] >> 8) 48 out[(i*2)+1] = uint8(mulawTable[sample]) 49 } 50 return out 51 } 52 53 // DecodeAlaw decodes 8-bit G711 samples (A-law) into 16-bit LPCM samples. 54 func DecodeAlaw(in []byte) []byte { 55 out := make([]byte, len(in)*2) 56 for i, sample := range in { 57 out[i*2] = uint8(alawTable[sample] >> 8) 58 out[(i*2)+1] = uint8(alawTable[sample]) 59 } 60 return out 61 }