av/stream/adpcm/adpcm.go

278 lines
7.1 KiB
Go
Raw Normal View History

/*
NAME
adpcm.go
DESCRIPTION
adpcm.go contains functions for encoding/compressing pcm into adpcm and decoding/decompressing back to pcm.
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
adpcm.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
/*
Original IMA/DVI ADPCM specification: (http://www.cs.columbia.edu/~hgs/audio/dvi/IMA_ADPCM.pdf).
Reference algorithms for ADPCM compression and decompression are in part 6.
*/
package adpcm
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.
// 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
index int16
}
// Decoder is used to decode from ADPCM to PCM data.
// pred, 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
index int16
step int16
}
// table of index changes (see spec)
var indexTable = []int16{
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
}
// quantize step size table (see spec)
var stepTable = []int16{
7, 8, 9, 10, 11, 12, 13, 14,
16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66,
73, 80, 88, 97, 107, 118, 130, 143,
157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767,
}
// NewEncoder retuns a new ADPCM encoder.
func NewEncoder(dst *bytes.Buffer) *Encoder {
e := Encoder{
dest: dst,
}
return &e
}
// NewDecoder retuns a new ADPCM decoder.
func NewDecoder(dst *bytes.Buffer) *Decoder {
d := Decoder{
step: stepTable[0],
dest: dst,
}
return &d
}
// 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 {
delta := sample - e.pred
var nibble byte
// set sign bit and find absolute value of difference
if delta < 0 {
nibble = 8
delta = -delta
}
step := stepTable[e.index]
diff := step >> 3
var mask byte = 4
for i := 0; i < 3; i++ {
if delta > step {
nibble |= mask
delta -= step
diff += step
}
mask >>= 1
step >>= 1
}
// adjust predicted sample based on calculated difference
if nibble&8 != 0 {
e.pred = capAdd16(e.pred, -diff)
} else {
e.pred = capAdd16(e.pred, diff)
}
// check for underflow and overflow
if e.pred < math.MinInt16 {
e.pred = math.MinInt16
} else if e.pred > math.MaxInt16 {
e.pred = math.MaxInt16
}
e.index += indexTable[nibble&7]
// check for underflow and overflow
if e.index < 0 {
e.index = 0
} else if e.index > int16(len(stepTable)-1) {
e.index = int16(len(stepTable) - 1)
}
return nibble
}
// decodeSample takes a byte, the last 4 bits of which contain a single
// 4 bit ADPCM nibble, and returns a 16 bit decoded PCM sample
func (d *Decoder) decodeSample(nibble byte) int16 {
// calculate difference
var diff int16
if nibble&4 != 0 {
diff += d.step
}
if nibble&2 != 0 {
diff += d.step >> 1
}
if nibble&1 != 0 {
diff += d.step >> 2
}
diff += d.step >> 3
// account for sign bit
if nibble&8 != 0 {
diff = -diff
}
// adjust predicted sample based on calculated difference
d.pred = capAdd16(d.pred, diff)
// adjust index into step size lookup table using nibble
d.index += indexTable[nibble]
// check for overflow and underflow
if d.index < 0 {
d.index = 0
} else if d.index > int16(len(stepTable)-1) {
d.index = int16(len(stepTable) - 1)
}
// find new quantizer step size
d.step = stepTable[d.index]
return d.pred
}
// 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)
}
}
func (e *Encoder) calcHead(sample []byte) error {
// check that we are given 1 16-bit sample (2 bytes)
sampSize := 2
if len(sample) != sampSize {
return fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), sampSize)
}
intSample := int16(binary.LittleEndian.Uint16(sample))
e.encodeSample(intSample)
e.dest.Write(sample)
e.dest.WriteByte(byte(uint16(e.index)))
e.dest.WriteByte(byte(0x00))
return nil
}
// EncodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
// It returns a byte slice containing encoded (compressed) ADPCM nibbles (each byte contains two nibbles).
func (e *Encoder) EncodeBlock(block []byte) error {
bSize := 1010
if len(block) != bSize {
return fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), bSize)
}
err := e.calcHead(block[0:2])
if err != nil {
return err
}
for i := 2; i < len(block); i++ {
if (i+1)%4 == 0 {
sample2 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1])))
sample := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3])))
e.dest.WriteByte(byte((sample << 4) | sample2))
}
}
return nil
}
// DecodeBlock takes a slice of 256 bytes, each byte should contain two ADPCM encoded nibbles.
// It returns a byte slice containing the resulting decoded (uncompressed) 16-bit PCM samples.
func (d *Decoder) DecodeBlock(block []byte) ([]byte, error) {
bSize := 256
if len(block) != bSize {
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize)
}
var result []byte
d.pred = int16(binary.LittleEndian.Uint16(block[0:2]))
d.index = int16(block[2])
d.step = stepTable[d.index]
result = append(result, block[0:2]...)
for i := 4; i < len(block); i++ {
originalSample := block[i]
secondSample := byte(originalSample >> 4)
firstSample := byte((secondSample << 4) ^ originalSample)
firstBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(firstSample)))
result = append(result, firstBytes...)
secondBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(secondSample)))
result = append(result, secondBytes...)
}
return result, nil
}