mirror of https://bitbucket.org/ausocean/av.git
adpcm: added overflow checks, improved initialization, naming
This commit is contained in:
parent
b412b75fc6
commit
d06388cfe9
|
@ -36,23 +36,24 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// encoder is used to encode to ADPCM from PCM data.
|
||||
// pred and index hold state that persists between calls to encodeSample and calcHead.
|
||||
// est and index hold state that persists between calls to encodeSample and calcHead.
|
||||
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the encoded ADPCM data is written to.
|
||||
type encoder struct {
|
||||
dest *bytes.Buffer
|
||||
pred int16
|
||||
est int16
|
||||
index int16
|
||||
}
|
||||
|
||||
// decoder is used to decode from ADPCM to PCM data.
|
||||
// pred, index, and step hold state that persists between calls to decodeSample.
|
||||
// est, index, and step hold state that persists between calls to decodeSample.
|
||||
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the decoded PCM data is written to.
|
||||
type decoder struct {
|
||||
dest *bytes.Buffer
|
||||
pred int16
|
||||
est int16
|
||||
index int16
|
||||
step int16
|
||||
}
|
||||
|
@ -98,7 +99,6 @@ func NewEncoder(dst *bytes.Buffer) *encoder {
|
|||
// NewDecoder retuns a new ADPCM decoder.
|
||||
func NewDecoder(dst *bytes.Buffer) *decoder {
|
||||
d := decoder{
|
||||
step: stepTable[0],
|
||||
dest: dst,
|
||||
}
|
||||
return &d
|
||||
|
@ -107,8 +107,8 @@ func NewDecoder(dst *bytes.Buffer) *decoder {
|
|||
// encodeSample takes a single 16 bit PCM sample and
|
||||
// returns a byte of which the last 4 bits are an encoded ADPCM nibble.
|
||||
func (e *encoder) encodeSample(sample int16) byte {
|
||||
// Find difference of actual sample from encoder's prediction.
|
||||
delta := sample - e.pred
|
||||
// Find difference between the sample and the previous estimation.
|
||||
delta := capAdd16(sample, -e.est)
|
||||
|
||||
// Create and set sign bit for nibble and find absolute value of difference.
|
||||
var nib byte
|
||||
|
@ -124,20 +124,20 @@ func (e *encoder) encodeSample(sample int16) byte {
|
|||
for i := 0; i < 3; i++ {
|
||||
if delta > step {
|
||||
nib |= mask
|
||||
delta -= step
|
||||
diff += step
|
||||
delta = capAdd16(delta, -step)
|
||||
diff = capAdd16(diff, step)
|
||||
}
|
||||
mask >>= 1
|
||||
step >>= 1
|
||||
}
|
||||
|
||||
// Adjust predicted sample based on calculated difference.
|
||||
if nib&8 != 0 {
|
||||
e.pred -= diff
|
||||
} else {
|
||||
e.pred += diff
|
||||
diff = -diff
|
||||
}
|
||||
|
||||
// Adjust estimated sample based on calculated difference.
|
||||
e.est = capAdd16(e.est, diff)
|
||||
|
||||
e.index += indexTable[nib&7]
|
||||
|
||||
// Check for underflow and overflow.
|
||||
|
@ -156,23 +156,23 @@ func (d *decoder) decodeSample(nibble byte) int16 {
|
|||
// Calculate difference.
|
||||
var diff int16
|
||||
if nibble&4 != 0 {
|
||||
diff += d.step
|
||||
diff = capAdd16(diff, d.step)
|
||||
}
|
||||
if nibble&2 != 0 {
|
||||
diff += d.step >> 1
|
||||
diff = capAdd16(diff, d.step>>1)
|
||||
}
|
||||
if nibble&1 != 0 {
|
||||
diff += d.step >> 2
|
||||
diff = capAdd16(diff, d.step>>2)
|
||||
}
|
||||
diff += d.step >> 3
|
||||
diff = capAdd16(diff, d.step>>3)
|
||||
|
||||
// Account for sign bit.
|
||||
if nibble&8 != 0 {
|
||||
diff = -diff
|
||||
}
|
||||
|
||||
// Adjust predicted sample based on calculated difference.
|
||||
d.pred += diff
|
||||
// Adjust estimated sample based on calculated difference.
|
||||
d.est = capAdd16(d.est, diff)
|
||||
|
||||
// Adjust index into step size lookup table using nibble.
|
||||
d.index += indexTable[nibble]
|
||||
|
@ -187,7 +187,20 @@ func (d *decoder) decodeSample(nibble byte) int16 {
|
|||
// Find new quantizer step size.
|
||||
d.step = stepTable[d.index]
|
||||
|
||||
return d.pred
|
||||
return d.est
|
||||
}
|
||||
|
||||
// capAdd16 adds two int16s together and caps at max/min int16 instead of overflowing
|
||||
func capAdd16(a, b int16) int16 {
|
||||
c := int32(a) + int32(b)
|
||||
switch {
|
||||
case c < math.MinInt16:
|
||||
return math.MinInt16
|
||||
case c > math.MaxInt16:
|
||||
return math.MaxInt16
|
||||
default:
|
||||
return int16(c)
|
||||
}
|
||||
}
|
||||
|
||||
// calcHead sets the state for the encoder by running the first sample through
|
||||
|
@ -200,15 +213,12 @@ func (e *encoder) calcHead(sample []byte, pad bool) (int, error) {
|
|||
return 0, fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), sampSize)
|
||||
}
|
||||
|
||||
intSample := int16(binary.LittleEndian.Uint16(sample))
|
||||
e.encodeSample(intSample)
|
||||
|
||||
n, err := e.dest.Write(sample)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
err = e.dest.WriteByte(byte(uint16(e.index)))
|
||||
err = e.dest.WriteByte(byte(int16(e.index)))
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
@ -226,78 +236,23 @@ func (e *encoder) calcHead(sample []byte, pad bool) (int, error) {
|
|||
return n, nil
|
||||
}
|
||||
|
||||
// encodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
|
||||
// It writes encoded (compressed) bytes (each byte containing two ADPCM nibbles) to the encoder's io.Writer (dest).
|
||||
// The number of bytes written is returned along with any errors.
|
||||
// Note: nibbles are output in little endian order, eg. n1n0 n3n2 n5n4...
|
||||
// Note: first 4 bytes are for initializing the decoder before decoding a block.
|
||||
// - First two bytes contain the first 16-bit sample uncompressed.
|
||||
// - Third byte is the decoder's starting index for the block, the fourth is padding and ignored.
|
||||
func (e *encoder) encodeBlock(block []byte) (int, error) {
|
||||
if len(block) != PcmBS {
|
||||
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), PcmBS)
|
||||
}
|
||||
// init initializes the encoder's estimation to the first uncompressed sample and the index to
|
||||
// point to a suitable quantizer step size.
|
||||
func (e *encoder) init(samps []byte) {
|
||||
int1 := int16(binary.LittleEndian.Uint16(samps[0:2]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samps[2:4]))
|
||||
e.est = int1
|
||||
|
||||
n, err := e.calcHead(block[0:2], false)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
for i := 3; i < PcmBS; i += 4 {
|
||||
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1])))
|
||||
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3])))
|
||||
err = e.dest.WriteByte(byte((nib2 << 4) | nib1))
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
n++
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// decodeBlock takes a slice of 256 bytes, each byte after the first 4 should contain two ADPCM encoded nibbles.
|
||||
// It writes the resulting decoded (decompressed) 16-bit PCM samples to the decoder's io.Writer (dest).
|
||||
// The number of bytes written is returned along with any errors.
|
||||
func (d *decoder) decodeBlock(block []byte) (int, error) {
|
||||
if len(block) != AdpcmBS {
|
||||
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), AdpcmBS)
|
||||
}
|
||||
|
||||
// Initialize decoder with first 4 bytes of the block.
|
||||
d.pred = int16(binary.LittleEndian.Uint16(block[0:2]))
|
||||
d.index = int16(block[2])
|
||||
d.step = stepTable[d.index]
|
||||
n, err := d.dest.Write(block[0:2])
|
||||
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.
|
||||
for i := 4; i < AdpcmBS; i++ {
|
||||
twoNibs := block[i]
|
||||
nib2 := byte(twoNibs >> 4)
|
||||
nib1 := byte((nib2 << 4) ^ twoNibs)
|
||||
|
||||
firstBytes := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
|
||||
_n, err := d.dest.Write(firstBytes)
|
||||
n += _n
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
secondBytes := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
|
||||
_n, err = d.dest.Write(secondBytes)
|
||||
n += _n
|
||||
if err != nil {
|
||||
return n, err
|
||||
halfDiff := math.Abs(math.Abs(float64(int1)) - math.Abs(float64(int2))/2.0)
|
||||
closest := math.Abs(float64(stepTable[0]) - halfDiff)
|
||||
var cInd int16
|
||||
for i, step := range stepTable {
|
||||
if math.Abs(float64(step)-halfDiff) < closest {
|
||||
closest = math.Abs(float64(step) - halfDiff)
|
||||
cInd = int16(i)
|
||||
}
|
||||
}
|
||||
|
||||
return n, nil
|
||||
e.index = cInd
|
||||
}
|
||||
|
||||
// Write takes a slice of bytes of arbitrary length representing pcm and encodes in into adpcm.
|
||||
|
@ -311,6 +266,7 @@ func (e *encoder) Write(inPcm []byte) (int, error) {
|
|||
pad = true
|
||||
}
|
||||
|
||||
e.init(inPcm[0:4])
|
||||
n, err := e.calcHead(inPcm[0:2], pad)
|
||||
if err != nil {
|
||||
return n, err
|
||||
|
@ -344,7 +300,7 @@ func (e *encoder) Write(inPcm []byte) (int, error) {
|
|||
// The number of bytes written out is returned along with any error that occured.
|
||||
func (d *decoder) Write(inAdpcm []byte) (int, error) {
|
||||
// Initialize decoder with first 4 bytes of the inAdpcm.
|
||||
d.pred = int16(binary.LittleEndian.Uint16(inAdpcm[0:2]))
|
||||
d.est = int16(binary.LittleEndian.Uint16(inAdpcm[0:2]))
|
||||
d.index = int16(inAdpcm[2])
|
||||
d.step = stepTable[d.index]
|
||||
n, err := d.dest.Write(inAdpcm[0:2])
|
||||
|
|
Loading…
Reference in New Issue