-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
45 lines (36 loc) · 1 KB
/
decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package go_g729
//#cgo CFLAGS: -I bcg729
//#cgo LDFLAGS: -L bcg729 -lbcg729 -lm
//#include "bcg729/decoder.h"
import "C"
import "unsafe"
type Decoder struct{
dec *C.bcg729DecoderChannelContextStruct
}
func NewDecoder() *Decoder{
return &Decoder{C.initBcg729DecoderChannel()}
}
func (thiz *Decoder)Destroy(){
C.closeBcg729DecoderChannel(thiz.dec)
}
func (thiz *Decoder)Decode(data []byte) []byte{
decoded := make([]byte, 160)
cDecoded := (*C.short)(unsafe.Pointer(&decoded[0]))
var cEncoded *C.uint8_t
if len(data) > 0 {
cEncoded = (*C.uint8_t)(unsafe.Pointer(&data[0]))
}else{
cEncoded = (*C.uint8_t)(C.NULL)
}
if len(data) == 0{
C.bcg729Decoder(thiz.dec, cEncoded, 0, 1, 0, 0, cDecoded)
return decoded
}
if len(data) < 8 {
//TODO: should consume 2 bytes every time decode in loop? or just decode once
C.bcg729Decoder(thiz.dec, cEncoded, C.uint8_t(len(data)), 0, 1, 0, cDecoded)
return decoded
}
C.bcg729Decoder(thiz.dec, cEncoded, C.uint8_t(len(data)), 0, 0, 0, cDecoded)
return decoded
}