Merged in adpcm-chunks (pull request #237)

Added adpcm chunk length and ability to decode consecutive chunks

Approved-by: kortschak <dan@kortschak.io>
This commit is contained in:
Trek Hopton 2019-08-28 03:30:02 +00:00
commit f537da41f2
2 changed files with 69 additions and 46 deletions

View File

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

View File

@ -51,7 +51,7 @@ func TestEncodeBlock(t *testing.T) {
} }
// Read expected adpcm file. // Read expected adpcm file.
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded_8kHz_adpcm_test.adpcm") exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded_8kHz_adpcm_test2.adpcm")
if err != nil { if err != nil {
t.Errorf("Unable to read expected ADPCM file: %v", err) t.Errorf("Unable to read expected ADPCM file: %v", err)
} }
@ -65,7 +65,7 @@ func TestEncodeBlock(t *testing.T) {
// resulting PCM with the expected decoded PCM. // resulting PCM with the expected decoded PCM.
func TestDecodeBlock(t *testing.T) { func TestDecodeBlock(t *testing.T) {
// Read adpcm. // Read adpcm.
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded_8kHz_adpcm_test.adpcm") comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded_8kHz_adpcm_test2.adpcm")
if err != nil { if err != nil {
t.Errorf("Unable to read input ADPCM file: %v", err) t.Errorf("Unable to read input ADPCM file: %v", err)
} }
@ -79,7 +79,7 @@ func TestDecodeBlock(t *testing.T) {
} }
// Read expected pcm file. // Read expected pcm file.
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded_8kHz_adpcm_test.pcm") exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded_8kHz_adpcm_test2.pcm")
if err != nil { if err != nil {
t.Errorf("Unable to read expected PCM file: %v", err) t.Errorf("Unable to read expected PCM file: %v", err)
} }