mirror of https://bitbucket.org/ausocean/av.git
adpcm: removed adpcm.go changes from this branch
This commit is contained in:
parent
6bef4aeefc
commit
4ac968654f
|
@ -40,11 +40,10 @@ import (
|
|||
const (
|
||||
byteDepth = 2 // We are working with 16-bit samples. TODO(Trek): make configurable.
|
||||
initSamps = 2 // Number of samples used to initialise the encoder.
|
||||
initSize = initSamps * byteDepth
|
||||
headSize = 8 // Number of bytes in the header of ADPCM.
|
||||
initBytes = initSamps * byteDepth
|
||||
headBytes = 4 // Number of bytes in the header of ADPCM.
|
||||
samplesPerEnc = 2 // Number of sample encoded at a time eg. 2 16-bit samples get encoded into 1 byte.
|
||||
bytesPerEnc = samplesPerEnc * byteDepth
|
||||
chunkLenSize = 4 // Size of the chunk length in bytes, chunk length is a 32 bit number.
|
||||
compFact = 4 // In general ADPCM compresses by a factor of 4.
|
||||
)
|
||||
|
||||
|
@ -177,7 +176,7 @@ func (e *Encoder) calcHead(sample []byte, pad bool) (int, error) {
|
|||
// The suitable step size is the closest step size in the stepTable to half the absolute difference of the first two samples.
|
||||
func (e *Encoder) init(samples []byte) {
|
||||
int1 := int16(binary.LittleEndian.Uint16(samples[:byteDepth]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initSize]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initBytes]))
|
||||
e.est = int1
|
||||
|
||||
halfDiff := math.Abs(math.Abs(float64(int1)) - math.Abs(float64(int2))/2)
|
||||
|
@ -198,8 +197,8 @@ func (e *Encoder) init(samples []byte) {
|
|||
func (e *Encoder) Write(b []byte) (int, error) {
|
||||
// Check that pcm has enough data to initialize Decoder.
|
||||
pcmLen := len(b)
|
||||
if pcmLen < initSize {
|
||||
return 0, fmt.Errorf("length of given byte array must be >= %v", initSize)
|
||||
if pcmLen < initBytes {
|
||||
return 0, fmt.Errorf("length of given byte array must be >= %v", initBytes)
|
||||
}
|
||||
|
||||
// Determine if there will be a byte that won't contain two full nibbles and will need padding.
|
||||
|
@ -208,18 +207,8 @@ func (e *Encoder) Write(b []byte) (int, error) {
|
|||
pad = true
|
||||
}
|
||||
|
||||
// Write the first 4 bytes of the adpcm chunk, which represent its length, ie. the number of bytes following the chunk length.
|
||||
chunkLen := EncBytes(pcmLen)
|
||||
chunkLenBytes := make([]byte, chunkLenSize)
|
||||
binary.LittleEndian.PutUint32(chunkLenBytes, uint32(chunkLen))
|
||||
n, err := e.dst.Write(chunkLenBytes)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
e.init(b[:initSize])
|
||||
_n, err := e.calcHead(b[:byteDepth], pad)
|
||||
n += _n
|
||||
e.init(b[:initBytes])
|
||||
n, err := e.calcHead(b[:byteDepth], pad)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
@ -295,30 +284,19 @@ func (d *Decoder) decodeSample(nibble byte) int16 {
|
|||
// It writes its output to the Decoder's dst.
|
||||
// The number of bytes written out is returned along with any error that occured.
|
||||
func (d *Decoder) Write(b []byte) (int, error) {
|
||||
// Iterate over each chunk and decode it.
|
||||
var n int
|
||||
var chunkLen int
|
||||
for off := 0; off+headSize <= len(b); off += chunkLen {
|
||||
// Read length of chunk and check if whole chunk exists.
|
||||
chunkLen = int(binary.LittleEndian.Uint32(b[off : off+chunkLenSize]))
|
||||
if off+chunkLen > len(b) {
|
||||
break
|
||||
}
|
||||
|
||||
// Initialize Decoder with header of b.
|
||||
d.est = int16(binary.LittleEndian.Uint16(b[off+chunkLenSize : off+chunkLenSize+byteDepth]))
|
||||
d.idx = int16(b[off+chunkLenSize+byteDepth])
|
||||
// Initialize Decoder with first 4 bytes of b.
|
||||
d.est = int16(binary.LittleEndian.Uint16(b[:byteDepth]))
|
||||
d.idx = int16(b[byteDepth])
|
||||
d.step = stepTable[d.idx]
|
||||
_n, err := d.dst.Write(b[off+chunkLenSize : off+chunkLenSize+byteDepth])
|
||||
n += _n
|
||||
n, err := d.dst.Write(b[:byteDepth])
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
// For each byte, seperate it into two nibbles (each nibble is a compressed sample),
|
||||
// then decode each nibble and output the resulting 16-bit samples.
|
||||
// If padding flag is true only decode up until the last byte, then decode that separately.
|
||||
for i := off + headSize; i < off+chunkLen-int(b[off+chunkLenSize+3]); i++ {
|
||||
// If padding flag is true (Adpcm[3]), only decode up until the last byte, then decode that separately.
|
||||
for i := headBytes; i < len(b)-int(b[3]); i++ {
|
||||
twoNibs := b[i]
|
||||
nib2 := byte(twoNibs >> 4)
|
||||
nib1 := byte((nib2 << 4) ^ twoNibs)
|
||||
|
@ -339,8 +317,8 @@ func (d *Decoder) Write(b []byte) (int, error) {
|
|||
return n, err
|
||||
}
|
||||
}
|
||||
if b[off+chunkLenSize+3] == 0x01 {
|
||||
padNib := b[off+chunkLen-1]
|
||||
if b[3] == 0x01 {
|
||||
padNib := b[len(b)-1]
|
||||
samp := make([]byte, byteDepth)
|
||||
binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib)))
|
||||
_n, err := d.dst.Write(samp)
|
||||
|
@ -349,7 +327,6 @@ func (d *Decoder) Write(b []byte) (int, error) {
|
|||
return n, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
|
@ -372,7 +349,7 @@ func EncBytes(n int) int {
|
|||
// and a start index and padding-flag byte are added.
|
||||
// Also if there are an even number of samples, there will be half a byte of padding added to the last byte.
|
||||
if n%bytesPerEnc == 0 {
|
||||
return (n-byteDepth)/compFact + headSize + 1
|
||||
return (n-byteDepth)/compFact + headBytes + 1
|
||||
}
|
||||
return (n-byteDepth)/compFact + headSize
|
||||
return (n-byteDepth)/compFact + headBytes
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue