mirror of https://bitbucket.org/ausocean/av.git
ADPCM: fixed spacing and overflow check
This commit is contained in:
parent
790dfaba7e
commit
c30b3de7c3
|
@ -1,3 +1,30 @@
|
||||||
|
/*
|
||||||
|
NAME
|
||||||
|
decode-pcm.go
|
||||||
|
|
||||||
|
DESCRIPTION
|
||||||
|
See Readme.md
|
||||||
|
|
||||||
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
decode-pcm.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
|
||||||
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -16,11 +43,13 @@ func main() {
|
||||||
flag.StringVar(&inPath, "in", "encoded.adpcm", "file path of input")
|
flag.StringVar(&inPath, "in", "encoded.adpcm", "file path of input")
|
||||||
flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data")
|
flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
//read adpcm
|
//read adpcm
|
||||||
comp, err := ioutil.ReadFile(inPath)
|
comp, err := ioutil.ReadFile(inPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//decode adpcm
|
//decode adpcm
|
||||||
var decoded []byte
|
var decoded []byte
|
||||||
start := 0
|
start := 0
|
||||||
|
@ -35,6 +64,7 @@ func main() {
|
||||||
start = i + 1
|
start = i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save pcm to file
|
// save pcm to file
|
||||||
err = ioutil.WriteFile(outPath, decoded, 0644)
|
err = ioutil.WriteFile(outPath, decoded, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,3 +1,30 @@
|
||||||
|
/*
|
||||||
|
NAME
|
||||||
|
encode-pcm.go
|
||||||
|
|
||||||
|
DESCRIPTION
|
||||||
|
See Readme.md
|
||||||
|
|
||||||
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
encode-pcm.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
|
||||||
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -13,6 +40,7 @@ func main() {
|
||||||
flag.StringVar(&inPath, "in", "data.pcm", "file path of input data")
|
flag.StringVar(&inPath, "in", "data.pcm", "file path of input data")
|
||||||
flag.StringVar(&adpcmPath, "out", "encoded.adpcm", "file path of output")
|
flag.StringVar(&adpcmPath, "out", "encoded.adpcm", "file path of output")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
//read pcm
|
//read pcm
|
||||||
pcm, err := ioutil.ReadFile(inPath)
|
pcm, err := ioutil.ReadFile(inPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,6 +63,7 @@ func main() {
|
||||||
start = i + 1
|
start = i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save adpcm to file
|
// save adpcm to file
|
||||||
err = ioutil.WriteFile(adpcmPath, comp, 0644)
|
err = ioutil.WriteFile(adpcmPath, comp, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,3 +1,35 @@
|
||||||
|
/*
|
||||||
|
NAME
|
||||||
|
adpcm.go
|
||||||
|
|
||||||
|
DESCRIPTION
|
||||||
|
See Readme.md
|
||||||
|
|
||||||
|
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
|
||||||
|
along with revid 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
|
package adpcm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -6,13 +38,13 @@ import (
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// table of index changes
|
// table of index changes (see spec)
|
||||||
var indexTable = []int16{
|
var indexTable = []int16{
|
||||||
-1, -1, -1, -1, 2, 4, 6, 8,
|
-1, -1, -1, -1, 2, 4, 6, 8,
|
||||||
-1, -1, -1, -1, 2, 4, 6, 8,
|
-1, -1, -1, -1, 2, 4, 6, 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
// quantize step size table
|
// quantize step size table (see spec)
|
||||||
var stepTable = []int16{
|
var stepTable = []int16{
|
||||||
7, 8, 9, 10, 11, 12, 13, 14,
|
7, 8, 9, 10, 11, 12, 13, 14,
|
||||||
16, 17, 19, 21, 23, 25, 28, 31,
|
16, 17, 19, 21, 23, 25, 28, 31,
|
||||||
|
@ -39,7 +71,6 @@ var (
|
||||||
// encodeSample takes a single 16 bit PCM sample and
|
// encodeSample takes a single 16 bit PCM sample and
|
||||||
// returns a byte of which the last 4 bits are an encoded ADPCM nibble
|
// returns a byte of which the last 4 bits are an encoded ADPCM nibble
|
||||||
func encodeSample(sample int16) byte {
|
func encodeSample(sample int16) byte {
|
||||||
|
|
||||||
delta := sample - encPred
|
delta := sample - encPred
|
||||||
|
|
||||||
var nibble byte
|
var nibble byte
|
||||||
|
@ -93,9 +124,6 @@ func encodeSample(sample int16) byte {
|
||||||
// decodeSample takes a byte, the last 4 bits of which contain a single
|
// 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
|
// 4 bit ADPCM nibble, and returns a 16 bit decoded PCM sample
|
||||||
func decodeSample(nibble byte) int16 {
|
func decodeSample(nibble byte) int16 {
|
||||||
|
|
||||||
// compute predicted sample estimate (decPred):
|
|
||||||
|
|
||||||
// calculate difference
|
// calculate difference
|
||||||
var diff int16
|
var diff int16
|
||||||
if nibble&4 != 0 {
|
if nibble&4 != 0 {
|
||||||
|
@ -108,30 +136,37 @@ func decodeSample(nibble byte) int16 {
|
||||||
diff += decStep >> 2
|
diff += decStep >> 2
|
||||||
}
|
}
|
||||||
diff += decStep >> 3
|
diff += decStep >> 3
|
||||||
|
|
||||||
// account for sign bit
|
// account for sign bit
|
||||||
if nibble&8 != 0 {
|
if nibble&8 != 0 {
|
||||||
diff = -diff
|
diff = -diff
|
||||||
}
|
}
|
||||||
// adjust predicted sample based on calculated difference
|
|
||||||
decPred += diff
|
|
||||||
|
|
||||||
// check for overflow and underflow
|
// adjust predicted sample based on calculated difference, check for overflow
|
||||||
if decPred > math.MaxInt16 {
|
if diff > 0 {
|
||||||
decPred = math.MaxInt16
|
if decPred > math.MaxInt16-diff {
|
||||||
} else if decPred < math.MinInt16 {
|
decPred = math.MaxInt16
|
||||||
decPred = math.MinInt16
|
} else {
|
||||||
|
decPred += diff
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if decPred < math.MaxInt16-diff {
|
||||||
|
decPred = math.MaxInt16
|
||||||
|
} else {
|
||||||
|
decPred += diff
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute new step size:
|
|
||||||
|
|
||||||
// adjust index into step size lookup table using nibble
|
// adjust index into step size lookup table using nibble
|
||||||
decIndex += indexTable[nibble]
|
decIndex += indexTable[nibble]
|
||||||
|
|
||||||
// check for overflow and underflow
|
// check for overflow and underflow
|
||||||
if decIndex < 0 {
|
if decIndex < 0 {
|
||||||
decIndex = 0
|
decIndex = 0
|
||||||
} else if decIndex > int16(len(stepTable)-1) {
|
} else if decIndex > int16(len(stepTable)-1) {
|
||||||
decIndex = int16(len(stepTable) - 1)
|
decIndex = int16(len(stepTable) - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// find new quantizer step size
|
// find new quantizer step size
|
||||||
decStep = stepTable[decIndex]
|
decStep = stepTable[decIndex]
|
||||||
|
|
||||||
|
@ -139,7 +174,6 @@ func decodeSample(nibble byte) int16 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcHead(sample []byte) ([]byte, error) {
|
func calcHead(sample []byte) ([]byte, error) {
|
||||||
|
|
||||||
// check that we are given 1 16-bit sample (2 bytes)
|
// check that we are given 1 16-bit sample (2 bytes)
|
||||||
sampSize := 2
|
sampSize := 2
|
||||||
if len(sample) != sampSize {
|
if len(sample) != sampSize {
|
||||||
|
@ -161,7 +195,6 @@ func calcHead(sample []byte) ([]byte, error) {
|
||||||
// EncodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
|
// 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).
|
// It returns a byte slice containing encoded (compressed) ADPCM nibbles (each byte contains two nibbles).
|
||||||
func EncodeBlock(block []byte) ([]byte, error) {
|
func EncodeBlock(block []byte) ([]byte, error) {
|
||||||
|
|
||||||
bSize := 1010
|
bSize := 1010
|
||||||
if len(block) != bSize {
|
if len(block) != bSize {
|
||||||
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), bSize)
|
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), bSize)
|
||||||
|
@ -187,7 +220,6 @@ func EncodeBlock(block []byte) ([]byte, error) {
|
||||||
// DecodeBlock takes a slice of 256 bytes, each byte should contain two ADPCM encoded nibbles.
|
// 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.
|
// It returns a byte slice containing the resulting decoded (uncompressed) 16-bit PCM samples.
|
||||||
func DecodeBlock(block []byte) ([]byte, error) {
|
func DecodeBlock(block []byte) ([]byte, error) {
|
||||||
|
|
||||||
bSize := 256
|
bSize := 256
|
||||||
if len(block) != bSize {
|
if len(block) != bSize {
|
||||||
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize)
|
return nil, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize)
|
||||||
|
@ -211,7 +243,6 @@ func DecodeBlock(block []byte) ([]byte, error) {
|
||||||
secondBytes := make([]byte, 2)
|
secondBytes := make([]byte, 2)
|
||||||
binary.LittleEndian.PutUint16(secondBytes, uint16(decodeSample(secondSample)))
|
binary.LittleEndian.PutUint16(secondBytes, uint16(decodeSample(secondSample)))
|
||||||
result = append(result, secondBytes...)
|
result = append(result, secondBytes...)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|
|
@ -1,3 +1,30 @@
|
||||||
|
/*
|
||||||
|
NAME
|
||||||
|
adpcm_test.go
|
||||||
|
|
||||||
|
DESCRIPTION
|
||||||
|
See Readme.md
|
||||||
|
|
||||||
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
adpcm_test.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
|
||||||
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
|
*/
|
||||||
|
|
||||||
package adpcm
|
package adpcm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,6 +41,7 @@ func TestEncodeBlock(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to read input PCM file: %v", err)
|
t.Errorf("Unable to read input PCM file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//encode adpcm
|
//encode adpcm
|
||||||
var comp []byte
|
var comp []byte
|
||||||
start := 0
|
start := 0
|
||||||
|
@ -23,13 +51,13 @@ func TestEncodeBlock(t *testing.T) {
|
||||||
|
|
||||||
encBlock, err := EncodeBlock(block)
|
encBlock, err := EncodeBlock(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//todo: use correct logging of error
|
|
||||||
t.Errorf("Unable to encode block: %v", err)
|
t.Errorf("Unable to encode block: %v", err)
|
||||||
}
|
}
|
||||||
comp = append(comp, encBlock...)
|
comp = append(comp, encBlock...)
|
||||||
start = i + 1
|
start = i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//read expected adpcm file
|
//read expected adpcm file
|
||||||
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm")
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -49,6 +77,7 @@ func TestDecodeBlock(t *testing.T) {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
//decode adpcm
|
//decode adpcm
|
||||||
var decoded []byte
|
var decoded []byte
|
||||||
start := 0
|
start := 0
|
||||||
|
@ -63,6 +92,7 @@ func TestDecodeBlock(t *testing.T) {
|
||||||
start = i + 1
|
start = i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//read expected pcm file
|
//read expected pcm file
|
||||||
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm")
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue