av/codec/adpcm/adpcm.go

356 lines
10 KiB
Go
Raw Normal View History

/*
NAME
adpcm.go
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.
*/
2019-05-28 20:27:17 +03:00
// Package adpcm provides functions to transcode between PCM and ADPCM.
package adpcm
import (
"encoding/binary"
"fmt"
"io"
"math"
)
2019-05-06 11:26:34 +03:00
const (
2019-05-28 20:27:17 +03:00
byteDepth = 2 // We are working with 16-bit samples. TODO(Trek): make configurable.
initSamps = 2 // Number of samples used to initialise the encoder.
2019-05-16 18:28:40 +03:00
initBytes = initSamps * byteDepth
2019-05-28 20:27:17 +03:00
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.
2019-05-16 18:28:40 +03:00
bytesPerEnc = samplesPerEnc * byteDepth
2019-05-28 20:27:17 +03:00
compFact = 4 // In general ADPCM compresses by a factor of 4.
2019-05-06 11:26:34 +03:00
)
2019-03-07 08:38:00 +03:00
// 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,
}
2019-03-07 08:38:00 +03:00
// 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,
}
// Encoder is used to encode to ADPCM from PCM data.
type Encoder struct {
2019-05-28 20:27:17 +03:00
// dst is the destination for ADPCM-encoded data.
dst io.Writer
2019-05-28 20:27:17 +03:00
est int16 // Estimation of sample based on quantised ADPCM nibble.
idx int16 // Index to step used for estimation.
}
// Decoder is used to decode from ADPCM to PCM data.
type Decoder struct {
2019-05-28 20:27:17 +03:00
// dst is the destination for PCM-encoded data.
dst io.Writer
2019-05-28 20:27:17 +03:00
est int16 // Estimation of sample based on quantised ADPCM nibble.
idx int16 // Index to step used for estimation.
step int16
}
// NewEncoder retuns a new ADPCM Encoder.
func NewEncoder(dst io.Writer) *Encoder {
2019-05-28 20:27:17 +03:00
return &Encoder{dst: dst}
}
// encodeSample takes a single 16 bit PCM sample and
2019-03-07 08:38:00 +03:00
// returns a byte of which the last 4 bits are an encoded ADPCM nibble.
func (e *Encoder) encodeSample(sample int16) byte {
// Find difference between the sample and the previous estimation.
delta := capAdd16(sample, -e.est)
2019-03-07 08:38:00 +03:00
// Create and set sign bit for nibble and find absolute value of difference.
var nib byte
if delta < 0 {
nib = 8
delta = -delta
}
2019-05-28 20:27:17 +03:00
step := stepTable[e.idx]
diff := step >> 3
var mask byte = 4
for i := 0; i < 3; i++ {
if delta > step {
nib |= mask
delta = capAdd16(delta, -step)
diff = capAdd16(diff, step)
}
mask >>= 1
step >>= 1
}
if nib&8 != 0 {
diff = -diff
}
// Adjust estimated sample based on calculated difference.
e.est = capAdd16(e.est, diff)
2019-05-28 20:27:17 +03:00
e.idx += indexTable[nib&7]
2019-03-07 08:38:00 +03:00
// Check for underflow and overflow.
2019-05-28 20:27:17 +03:00
if e.idx < 0 {
e.idx = 0
} else if e.idx > int16(len(stepTable)-1) {
e.idx = int16(len(stepTable) - 1)
}
return nib
}
// calcHead sets the state for the Encoder by running the first sample through
// the Encoder, and writing the first sample to the Encoder's io.Writer (dst).
2019-05-28 20:27:17 +03:00
// It returns the number of bytes written to the Encoder's destination and the first error encountered.
func (e *Encoder) calcHead(sample []byte, pad bool) (int, error) {
2019-05-06 11:26:34 +03:00
// Check that we are given 1 sample.
if len(sample) != byteDepth {
return 0, fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), byteDepth)
}
2019-05-06 11:26:34 +03:00
n, err := e.dst.Write(sample)
if err != nil {
return n, err
}
2019-05-28 20:27:17 +03:00
_n, err := e.dst.Write([]byte{byte(int16(e.idx))})
if err != nil {
return n, err
}
n += _n
if pad {
_n, err = e.dst.Write([]byte{0x01})
} else {
_n, err = e.dst.Write([]byte{0x00})
}
n += _n
if err != nil {
return n, err
}
return n, nil
}
// init initializes the Encoder's estimation to the first uncompressed sample and the index to
// point to a suitable quantizer step size.
2019-05-06 11:26:34 +03:00
// 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) {
2019-05-16 18:28:40 +03:00
int1 := int16(binary.LittleEndian.Uint16(samples[:byteDepth]))
int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initBytes]))
e.est = int1
2019-05-28 20:27:17 +03:00
halfDiff := math.Abs(math.Abs(float64(int1)) - math.Abs(float64(int2))/2)
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)
}
}
2019-05-28 20:27:17 +03:00
e.idx = cInd
}
2019-05-06 11:26:34 +03:00
// Write takes a slice of bytes of arbitrary length representing pcm and encodes it into adpcm.
// It writes its output to the Encoder's dst.
// The number of bytes written out is returned along with any error that occured.
func (e *Encoder) Write(b []byte) (int, error) {
// Check that pcm has enough data to initialize Decoder.
2019-05-16 18:28:40 +03:00
pcmLen := len(b)
2019-05-06 11:26:34 +03:00
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.
pad := false
2019-05-06 11:26:34 +03:00
if (pcmLen-byteDepth)%bytesPerEnc != 0 {
pad = true
}
2019-05-16 18:28:40 +03:00
e.init(b[:initBytes])
n, err := e.calcHead(b[:byteDepth], pad)
if err != nil {
return n, err
}
2019-04-26 17:24:05 +03:00
// Skip the first sample and start at the end of the first two samples, then every two samples encode them into a byte of adpcm.
2019-05-06 11:26:34 +03:00
for i := byteDepth; i+bytesPerEnc-1 < pcmLen; i += bytesPerEnc {
2019-05-16 18:28:40 +03:00
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i : i+byteDepth])))
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i+byteDepth : i+bytesPerEnc])))
_n, err := e.dst.Write([]byte{byte((nib2 << 4) | nib1)})
n += _n
if err != nil {
return n, err
}
}
2019-05-06 11:26:34 +03:00
// If we've reached the end of the pcm data and there's a sample left over,
// compress it to a nibble and leave the first half of the byte padded with 0s.
if pad {
2019-05-16 18:28:40 +03:00
nib := e.encodeSample(int16(binary.LittleEndian.Uint16(b[pcmLen-byteDepth : pcmLen])))
_n, err := e.dst.Write([]byte{nib})
n += _n
if err != nil {
return n, err
}
}
return n, nil
}
// NewDecoder retuns a new ADPCM Decoder.
func NewDecoder(dst io.Writer) *Decoder {
2019-05-28 20:27:17 +03:00
return &Decoder{dst: dst}
}
// 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 = capAdd16(diff, d.step)
}
if nibble&2 != 0 {
diff = capAdd16(diff, d.step>>1)
}
if nibble&1 != 0 {
diff = capAdd16(diff, d.step>>2)
}
diff = capAdd16(diff, d.step>>3)
// Account for sign bit.
if nibble&8 != 0 {
diff = -diff
}
// Adjust estimated sample based on calculated difference.
d.est = capAdd16(d.est, diff)
// Adjust index into step size lookup table using nibble.
2019-05-28 20:27:17 +03:00
d.idx += indexTable[nibble]
// Check for overflow and underflow.
2019-05-28 20:27:17 +03:00
if d.idx < 0 {
d.idx = 0
} else if d.idx > int16(len(stepTable)-1) {
d.idx = int16(len(stepTable) - 1)
}
// Find new quantizer step size.
2019-05-28 20:27:17 +03:00
d.step = stepTable[d.idx]
return d.est
}
2019-05-06 11:26:34 +03:00
// Write takes a slice of bytes of arbitrary length representing adpcm and decodes it into pcm.
// 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) {
// Initialize Decoder with first 4 bytes of b.
2019-05-16 18:28:40 +03:00
d.est = int16(binary.LittleEndian.Uint16(b[:byteDepth]))
2019-05-28 20:27:17 +03:00
d.idx = int16(b[byteDepth])
d.step = stepTable[d.idx]
2019-05-16 18:28:40 +03:00
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 (Adpcm[3]), only decode up until the last byte, then decode that separately.
2019-05-16 18:28:40 +03:00
for i := headBytes; i < len(b)-int(b[3]); i++ {
twoNibs := b[i]
nib2 := byte(twoNibs >> 4)
nib1 := byte((nib2 << 4) ^ twoNibs)
2019-05-06 11:26:34 +03:00
firstBytes := make([]byte, byteDepth)
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
2019-05-06 11:26:34 +03:00
_n, err := d.dst.Write(firstBytes)
n += _n
if err != nil {
return n, err
}
2019-05-06 11:26:34 +03:00
secondBytes := make([]byte, byteDepth)
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
2019-05-06 11:26:34 +03:00
_n, err = d.dst.Write(secondBytes)
n += _n
if err != nil {
return n, err
}
}
2019-05-16 18:28:40 +03:00
if b[3] == 0x01 {
padNib := b[len(b)-1]
2019-05-06 11:26:34 +03:00
samp := make([]byte, byteDepth)
binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib)))
2019-05-06 11:26:34 +03:00
_n, err := d.dst.Write(samp)
n += _n
if err != nil {
return n, err
}
}
return n, nil
}
// 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)
}
}
2019-05-28 20:27:17 +03:00
// EncBytes will return the number of adpcm bytes that will be generated when encoding the given amount of pcm bytes (n).
func EncBytes(n int) int {
// For 'n' pcm bytes, 1 sample is left uncompressed, the rest is compressed by a factor of 4
// 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.
2019-05-28 20:27:17 +03:00
if n%bytesPerEnc == 0 {
return (n-byteDepth)/compFact + headBytes + 1
}
2019-05-28 20:27:17 +03:00
return (n-byteDepth)/compFact + headBytes
}