2019-02-13 04:23:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
adpcm.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-02-25 04:25:13 +03:00
|
|
|
adpcm.go contains functions for encoding/compressing pcm into adpcm and decoding/decompressing back to pcm.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
|
|
|
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
|
2019-02-27 09:16:15 +03:00
|
|
|
for more details.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-27 09:16:15 +03:00
|
|
|
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).
|
2019-02-13 04:23:06 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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-02-11 04:41:26 +03:00
|
|
|
package adpcm
|
|
|
|
|
|
|
|
import (
|
2019-02-28 05:35:45 +03:00
|
|
|
"bytes"
|
2019-02-11 04:41:26 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// encoder is used to encode to ADPCM from PCM data.
|
2019-02-28 04:23:16 +03:00
|
|
|
// pred and index hold state that persists between calls to encodeSample and calcHead.
|
2019-02-28 05:35:45 +03:00
|
|
|
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the encoded ADPCM data is written to.
|
2019-03-15 09:52:19 +03:00
|
|
|
type encoder struct {
|
2019-02-28 05:35:45 +03:00
|
|
|
dest *bytes.Buffer
|
2019-02-28 04:23:16 +03:00
|
|
|
pred int16
|
|
|
|
index int16
|
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// decoder is used to decode from ADPCM to PCM data.
|
2019-02-28 04:23:16 +03:00
|
|
|
// pred, index, and step hold state that persists between calls to decodeSample.
|
2019-02-28 05:35:45 +03:00
|
|
|
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the decoded PCM data is written to.
|
2019-03-15 09:52:19 +03:00
|
|
|
type decoder struct {
|
2019-02-28 05:35:45 +03:00
|
|
|
dest *bytes.Buffer
|
2019-02-28 04:23:16 +03:00
|
|
|
pred int16
|
|
|
|
index int16
|
|
|
|
step int16
|
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// PcmBS is the size of the blocks that an encoder uses.
|
|
|
|
// 'encodeBlock' will encode PcmBS bytes at a time and the output will be AdpcmBS bytes long.
|
2019-03-15 08:17:08 +03:00
|
|
|
const PcmBS = 1010
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// AdpcmBS is the size of the blocks that a decoder uses.
|
|
|
|
// 'decodeBlock' will decode AdpcmBS bytes at a time and the output will be PcmBS bytes long.
|
2019-03-15 08:17:08 +03:00
|
|
|
const AdpcmBS = 256
|
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Table of index changes (see spec).
|
2019-02-11 04:41:26 +03:00
|
|
|
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).
|
2019-02-11 04:41:26 +03:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-02-28 04:23:16 +03:00
|
|
|
// NewEncoder retuns a new ADPCM encoder.
|
2019-03-15 09:52:19 +03:00
|
|
|
func NewEncoder(dst *bytes.Buffer) *encoder {
|
|
|
|
e := encoder{
|
2019-02-28 04:23:16 +03:00
|
|
|
dest: dst,
|
|
|
|
}
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDecoder retuns a new ADPCM decoder.
|
2019-03-15 09:52:19 +03:00
|
|
|
func NewDecoder(dst *bytes.Buffer) *decoder {
|
|
|
|
d := decoder{
|
2019-02-28 04:23:16 +03:00
|
|
|
step: stepTable[0],
|
|
|
|
dest: dst,
|
|
|
|
}
|
|
|
|
return &d
|
|
|
|
}
|
2019-02-11 04:41:26 +03:00
|
|
|
|
|
|
|
// 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.
|
2019-03-15 09:52:19 +03:00
|
|
|
func (e *encoder) encodeSample(sample int16) byte {
|
2019-03-07 08:38:00 +03:00
|
|
|
// Find difference of actual sample from encoder's prediction.
|
2019-02-28 04:23:16 +03:00
|
|
|
delta := sample - e.pred
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Create and set sign bit for nibble and find absolute value of difference.
|
2019-03-01 07:44:09 +03:00
|
|
|
var nib byte
|
2019-02-11 04:41:26 +03:00
|
|
|
if delta < 0 {
|
2019-03-01 07:44:09 +03:00
|
|
|
nib = 8
|
2019-02-11 04:41:26 +03:00
|
|
|
delta = -delta
|
|
|
|
}
|
|
|
|
|
2019-02-28 04:23:16 +03:00
|
|
|
step := stepTable[e.index]
|
2019-02-11 04:41:26 +03:00
|
|
|
diff := step >> 3
|
|
|
|
var mask byte = 4
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if delta > step {
|
2019-03-01 07:44:09 +03:00
|
|
|
nib |= mask
|
2019-02-11 04:41:26 +03:00
|
|
|
delta -= step
|
|
|
|
diff += step
|
|
|
|
}
|
|
|
|
mask >>= 1
|
|
|
|
step >>= 1
|
|
|
|
}
|
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Adjust predicted sample based on calculated difference.
|
2019-03-01 07:44:09 +03:00
|
|
|
if nib&8 != 0 {
|
2019-03-07 06:33:51 +03:00
|
|
|
e.pred -= diff
|
2019-02-11 04:41:26 +03:00
|
|
|
} else {
|
2019-03-07 06:33:51 +03:00
|
|
|
e.pred += diff
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 07:44:09 +03:00
|
|
|
e.index += indexTable[nib&7]
|
2019-03-15 09:52:19 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Check for underflow and overflow.
|
2019-02-28 04:23:16 +03:00
|
|
|
if e.index < 0 {
|
|
|
|
e.index = 0
|
|
|
|
} else if e.index > int16(len(stepTable)-1) {
|
|
|
|
e.index = int16(len(stepTable) - 1)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 07:44:09 +03:00
|
|
|
return nib
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeSample takes a byte, the last 4 bits of which contain a single
|
2019-03-07 08:38:00 +03:00
|
|
|
// 4 bit ADPCM nibble, and returns a 16 bit decoded PCM sample.
|
2019-03-15 09:52:19 +03:00
|
|
|
func (d *decoder) decodeSample(nibble byte) int16 {
|
2019-03-07 08:38:00 +03:00
|
|
|
// Calculate difference.
|
2019-02-11 04:41:26 +03:00
|
|
|
var diff int16
|
|
|
|
if nibble&4 != 0 {
|
2019-02-28 04:23:16 +03:00
|
|
|
diff += d.step
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
if nibble&2 != 0 {
|
2019-02-28 04:23:16 +03:00
|
|
|
diff += d.step >> 1
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
if nibble&1 != 0 {
|
2019-02-28 04:23:16 +03:00
|
|
|
diff += d.step >> 2
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
2019-02-28 04:23:16 +03:00
|
|
|
diff += d.step >> 3
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Account for sign bit.
|
2019-02-11 04:41:26 +03:00
|
|
|
if nibble&8 != 0 {
|
|
|
|
diff = -diff
|
|
|
|
}
|
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Adjust predicted sample based on calculated difference.
|
2019-03-07 06:33:51 +03:00
|
|
|
d.pred += diff
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Adjust index into step size lookup table using nibble.
|
2019-02-28 04:23:16 +03:00
|
|
|
d.index += indexTable[nibble]
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Check for overflow and underflow.
|
2019-02-28 04:23:16 +03:00
|
|
|
if d.index < 0 {
|
|
|
|
d.index = 0
|
|
|
|
} else if d.index > int16(len(stepTable)-1) {
|
|
|
|
d.index = int16(len(stepTable) - 1)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Find new quantizer step size.
|
2019-02-28 04:23:16 +03:00
|
|
|
d.step = stepTable[d.index]
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-02-28 04:23:16 +03:00
|
|
|
return d.pred
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 07:44:09 +03:00
|
|
|
// calcHead sets the state for the encoder by running the first sample through
|
2019-03-15 10:24:24 +03:00
|
|
|
// the encoder, and writing the first sample to the encoder's io.Writer (dest).
|
|
|
|
// It returns the number of bytes written to the encoder's io.Writer (dest) along with any errors.
|
2019-03-15 09:52:19 +03:00
|
|
|
func (e *encoder) calcHead(sample []byte) (int, error) {
|
2019-03-07 08:38:00 +03:00
|
|
|
// Check that we are given 1 16-bit sample (2 bytes).
|
2019-03-15 08:17:08 +03:00
|
|
|
const sampSize = 2
|
2019-02-12 07:19:16 +03:00
|
|
|
if len(sample) != sampSize {
|
2019-03-15 08:17:08 +03:00
|
|
|
return 0, fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), sampSize)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
intSample := int16(binary.LittleEndian.Uint16(sample))
|
2019-02-28 04:23:16 +03:00
|
|
|
e.encodeSample(intSample)
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
n, err := e.dest.Write(sample)
|
2019-03-15 08:17:08 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 08:17:08 +03:00
|
|
|
}
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-15 08:17:08 +03:00
|
|
|
err = e.dest.WriteByte(byte(uint16(e.index)))
|
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 08:17:08 +03:00
|
|
|
}
|
2019-03-15 10:24:24 +03:00
|
|
|
n++
|
2019-03-15 08:17:08 +03:00
|
|
|
|
|
|
|
err = e.dest.WriteByte(byte(0x00))
|
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 08:17:08 +03:00
|
|
|
}
|
2019-03-15 10:24:24 +03:00
|
|
|
n++
|
|
|
|
return n, nil
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// encodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
|
2019-03-15 10:24:24 +03:00
|
|
|
// 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.
|
2019-03-07 08:38:00 +03:00
|
|
|
// 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.
|
2019-03-15 09:52:19 +03:00
|
|
|
func (e *encoder) encodeBlock(block []byte) (int, error) {
|
2019-03-15 08:17:08 +03:00
|
|
|
if len(block) != PcmBS {
|
2019-03-15 10:24:24 +03:00
|
|
|
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), PcmBS)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
n, err := e.calcHead(block[0:2])
|
2019-02-11 04:41:26 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 08:17:08 +03:00
|
|
|
for i := 3; i < PcmBS; i += 4 {
|
2019-03-01 07:44:09 +03:00
|
|
|
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1])))
|
|
|
|
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3])))
|
2019-03-15 08:17:08 +03:00
|
|
|
err = e.dest.WriteByte(byte((nib2 << 4) | nib1))
|
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 08:17:08 +03:00
|
|
|
}
|
2019-03-15 10:24:24 +03:00
|
|
|
n++
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, nil
|
2019-03-15 08:17:08 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// decodeBlock takes a slice of 256 bytes, each byte after the first 4 should contain two ADPCM encoded nibbles.
|
2019-03-15 10:24:24 +03:00
|
|
|
// 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.
|
2019-03-15 09:52:19 +03:00
|
|
|
func (d *decoder) decodeBlock(block []byte) (int, error) {
|
2019-03-15 08:17:08 +03:00
|
|
|
if len(block) != AdpcmBS {
|
2019-03-15 10:24:24 +03:00
|
|
|
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), AdpcmBS)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Initialize decoder with first 4 bytes of the block.
|
2019-02-28 04:23:16 +03:00
|
|
|
d.pred = int16(binary.LittleEndian.Uint16(block[0:2]))
|
|
|
|
d.index = int16(block[2])
|
|
|
|
d.step = stepTable[d.index]
|
2019-03-15 10:24:24 +03:00
|
|
|
n, err := d.dest.Write(block[0:2])
|
2019-03-15 09:37:22 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// 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.
|
2019-03-15 08:17:08 +03:00
|
|
|
for i := 4; i < AdpcmBS; i++ {
|
2019-03-01 07:44:09 +03:00
|
|
|
twoNibs := block[i]
|
|
|
|
nib2 := byte(twoNibs >> 4)
|
|
|
|
nib1 := byte((nib2 << 4) ^ twoNibs)
|
2019-02-11 04:41:26 +03:00
|
|
|
|
|
|
|
firstBytes := make([]byte, 2)
|
2019-03-01 07:44:09 +03:00
|
|
|
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
|
2019-03-15 10:24:24 +03:00
|
|
|
_n, err := d.dest.Write(firstBytes)
|
|
|
|
n += _n
|
2019-03-15 09:37:22 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
2019-02-11 04:41:26 +03:00
|
|
|
|
|
|
|
secondBytes := make([]byte, 2)
|
2019-03-01 07:44:09 +03:00
|
|
|
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
|
2019-03-15 10:24:24 +03:00
|
|
|
_n, err = d.dest.Write(secondBytes)
|
|
|
|
n += _n
|
2019-03-15 09:37:22 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, nil
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// Write takes a slice of bytes of arbitrary length representing pcm and encodes in into adpcm.
|
|
|
|
// It writes its output to the encoder's dest.
|
|
|
|
// The number of bytes written out is returned along with any error that occured.
|
|
|
|
func (e *encoder) Write(inPcm []byte) (int, error) {
|
2019-03-15 09:37:22 +03:00
|
|
|
numBlocks := len(inPcm) / PcmBS
|
2019-03-15 10:24:24 +03:00
|
|
|
n := 0
|
2019-03-15 09:37:22 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := inPcm[PcmBS*i : PcmBS*(i+1)]
|
2019-03-15 10:24:24 +03:00
|
|
|
_n, err := e.encodeBlock(block)
|
|
|
|
n += _n
|
2019-03-15 09:37:22 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, nil
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:52:19 +03:00
|
|
|
// Write takes a slice of bytes of arbitrary length representing adpcm and decodes in into pcm.
|
|
|
|
// It writes its output to the decoder's dest.
|
|
|
|
// The number of bytes written out is returned along with any error that occured.
|
|
|
|
func (d *decoder) Write(inAdpcm []byte) (int, error) {
|
2019-03-15 09:37:22 +03:00
|
|
|
numBlocks := len(inAdpcm) / AdpcmBS
|
2019-03-15 10:24:24 +03:00
|
|
|
n := 0
|
2019-03-15 09:37:22 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := inAdpcm[AdpcmBS*i : AdpcmBS*(i+1)]
|
2019-03-15 10:24:24 +03:00
|
|
|
_n, err := d.decodeBlock(block)
|
|
|
|
n += _n
|
2019-03-15 09:37:22 +03:00
|
|
|
if err != nil {
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, err
|
2019-03-15 09:37:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 10:24:24 +03:00
|
|
|
return n, nil
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|