Skip to content

Commit

Permalink
add g711 utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 28, 2023
1 parent cb3003e commit 9641ec4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ Definitions and functions shared between [gortsplib](https://github.com/bluenvir
|ISO 13818-2, Generic Coding of Moving Pictures and Associated Audio information, Part 2, Video|codecs / MPEG-1/2 Video|
|ISO 14496-2, Coding of audio-visual objects, Part 2, Visual|codecs / MPEG-4 Video|
|[ITU-T Rec. T-871, JPEG File Interchange Format](https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.871-201105-I!!PDF-E&type=items)|codecs / JPEG|
|[ITU-T Rec. H.264 (08/2021)](https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-H.264-202108-I!!PDF-E&type=items)|codecs / H264|
|[ITU-T Rec. H.265 (08/2021)](https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-H.265-202108-I!!PDF-E&type=items)|codecs / H265|
|[ITU-T Rec. H.264 (08/2021)](https://www.itu.int/rec/T-REC-H.264)|codecs / H264|
|[ITU-T Rec. H.265 (08/2021)](https://www.itu.int/rec/T-REC-H.265)|codecs / H265|
|[VP9 Bitstream & Decoding Process Specification v0.6](https://storage.googleapis.com/downloads.webmproject.org/docs/vp9/vp9-bitstream-specification-v0.6-20160331-draft.pdf)|codecs / VP9|
|[AV1 Bitstream & Decoding Process](https://aomediacodec.github.io/av1-spec/av1-spec.pdf)|codecs / AV1|
|[ITU-T Rec. G.711 (11/88)](https://www.itu.int/rec/T-REC-G.711)|codecs / G711|
|ISO 11172-3, Coding of moving pictures and associated audio|codecs / MPEG-1/2 Audio|
|ISO 13818-3, Generic Coding of Moving Pictures and Associated Audio information, Part 3, Audio|codecs / MPEG-1/2 Audio|
|ISO 14496-3, Coding of audio-visual objects, Part 3, Audio|codecs / MPEG-4 Audio|
Expand Down
61 changes: 61 additions & 0 deletions pkg/codecs/g711/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Package g711 contains utilities to work with the G711 codec.
package g711

var mulawTable = func() [256]uint16 {
var ret [256]uint16
for i := 0; i < 256; i++ {
v := ^i

tmp := (((uint16(v) & 0x0F) << 3) + 0x84) << ((v & 0x70) >> 4)

if (v & 0x80) != 0 {
ret[i] = 0x84 - tmp
} else {
ret[i] = tmp - 0x84
}
}
return ret
}()

var alawTable = func() [256]uint16 {
var ret [256]uint16
for i := 0; i < 256; i++ {
v := i ^ 0x55

t := uint16(v) & 0x0F
seg := (uint16(v) & 0x70) >> 4

if seg != 0 {
t = (t*2 + 1 + 32) << (seg + 2)
} else {
t = (t*2 + 1) << 3
}

if (v & 0x80) != 0 {
ret[i] = t
} else {
ret[i] = -t
}
}
return ret
}()

// DecodeMulaw decodes 8-bit G711 samples (MU-law) into 16-bit LPCM samples.
func DecodeMulaw(in []byte) []byte {
out := make([]byte, len(in)*2)
for i, sample := range in {
out[i*2] = uint8(mulawTable[sample] >> 8)
out[(i*2)+1] = uint8(mulawTable[sample])
}
return out
}

// DecodeAlaw decodes 8-bit G711 samples (A-law) into 16-bit LPCM samples.
func DecodeAlaw(in []byte) []byte {
out := make([]byte, len(in)*2)
for i, sample := range in {
out[i*2] = uint8(alawTable[sample] >> 8)
out[(i*2)+1] = uint8(alawTable[sample])
}
return out
}
27 changes: 27 additions & 0 deletions pkg/codecs/g711/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g711

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDecodeMuLaw(t *testing.T) {
require.Equal(t,
DecodeMulaw([]byte{1, 2, 3, 255, 254, 253}),
[]byte{
0x86, 0x84, 0x8a, 0x84, 0x8e, 0x84, 0x00, 0x00,
0x00, 0x08, 0x00, 0x10,
},
)
}

func TestDecodeALaw(t *testing.T) {
require.Equal(t,
DecodeAlaw([]byte{1, 2, 3, 255, 254, 253}),
[]byte{
0xeb, 0x80, 0xe8, 0x80, 0xe9, 0x80, 0x03, 0x50,
0x03, 0x70, 0x03, 0x10,
},
)
}

0 comments on commit 9641ec4

Please sign in to comment.