diff --git a/cmd/adpcm/decode-pcm/decode-pcm.go b/cmd/adpcm/decode-pcm/decode-pcm.go index 81b2d969..64973e3f 100644 --- a/cmd/adpcm/decode-pcm/decode-pcm.go +++ b/cmd/adpcm/decode-pcm/decode-pcm.go @@ -1,3 +1,30 @@ +/* +NAME + decode-pcm.go + +DESCRIPTION + See Readme.md + +AUTHOR + Trek Hopton + +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 import ( @@ -16,11 +43,13 @@ func main() { flag.StringVar(&inPath, "in", "encoded.adpcm", "file path of input") flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data") flag.Parse() + //read adpcm comp, err := ioutil.ReadFile(inPath) if err != nil { panic(err) } + //decode adpcm var decoded []byte start := 0 @@ -35,6 +64,7 @@ func main() { start = i + 1 } } + // save pcm to file err = ioutil.WriteFile(outPath, decoded, 0644) if err != nil { diff --git a/cmd/adpcm/encode-pcm/encode-pcm.go b/cmd/adpcm/encode-pcm/encode-pcm.go index f8812dd3..47ba6e07 100644 --- a/cmd/adpcm/encode-pcm/encode-pcm.go +++ b/cmd/adpcm/encode-pcm/encode-pcm.go @@ -1,3 +1,30 @@ +/* +NAME + encode-pcm.go + +DESCRIPTION + See Readme.md + +AUTHOR + Trek Hopton + +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 import ( @@ -13,6 +40,7 @@ func main() { flag.StringVar(&inPath, "in", "data.pcm", "file path of input data") flag.StringVar(&adpcmPath, "out", "encoded.adpcm", "file path of output") flag.Parse() + //read pcm pcm, err := ioutil.ReadFile(inPath) if err != nil { @@ -35,6 +63,7 @@ func main() { start = i + 1 } } + // save adpcm to file err = ioutil.WriteFile(adpcmPath, comp, 0644) if err != nil { diff --git a/stream/adpcm/adpcm.go b/stream/adpcm/adpcm.go index 61259de0..b4963efb 100644 --- a/stream/adpcm/adpcm.go +++ b/stream/adpcm/adpcm.go @@ -1,3 +1,35 @@ +/* +NAME + adpcm.go + +DESCRIPTION + See Readme.md + +AUTHOR + Trek Hopton + +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 import ( @@ -6,13 +38,13 @@ import ( "math" ) -// table of index changes +// 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 +// 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, @@ -39,7 +71,6 @@ var ( // encodeSample takes a single 16 bit PCM sample and // returns a byte of which the last 4 bits are an encoded ADPCM nibble func encodeSample(sample int16) byte { - delta := sample - encPred 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 // 4 bit ADPCM nibble, and returns a 16 bit decoded PCM sample func decodeSample(nibble byte) int16 { - - // compute predicted sample estimate (decPred): - // calculate difference var diff int16 if nibble&4 != 0 { @@ -108,30 +136,37 @@ func decodeSample(nibble byte) int16 { diff += decStep >> 2 } diff += decStep >> 3 + // account for sign bit if nibble&8 != 0 { diff = -diff } - // adjust predicted sample based on calculated difference - decPred += diff - // check for overflow and underflow - if decPred > math.MaxInt16 { - decPred = math.MaxInt16 - } else if decPred < math.MinInt16 { - decPred = math.MinInt16 + // adjust predicted sample based on calculated difference, check for overflow + if diff > 0 { + if decPred > math.MaxInt16-diff { + decPred = math.MaxInt16 + } 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 decIndex += indexTable[nibble] + // check for overflow and underflow if decIndex < 0 { decIndex = 0 } else if decIndex > int16(len(stepTable)-1) { decIndex = int16(len(stepTable) - 1) } + // find new quantizer step size decStep = stepTable[decIndex] @@ -139,7 +174,6 @@ func decodeSample(nibble byte) int16 { } func calcHead(sample []byte) ([]byte, error) { - // check that we are given 1 16-bit sample (2 bytes) sampSize := 2 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). // It returns a byte slice containing encoded (compressed) ADPCM nibbles (each byte contains two nibbles). func EncodeBlock(block []byte) ([]byte, error) { - bSize := 1010 if 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. // It returns a byte slice containing the resulting decoded (uncompressed) 16-bit PCM samples. func 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) @@ -211,7 +243,6 @@ func DecodeBlock(block []byte) ([]byte, error) { secondBytes := make([]byte, 2) binary.LittleEndian.PutUint16(secondBytes, uint16(decodeSample(secondSample))) result = append(result, secondBytes...) - } return result, nil diff --git a/stream/adpcm/adpcm_test.go b/stream/adpcm/adpcm_test.go index c83732c3..4d40f3e9 100644 --- a/stream/adpcm/adpcm_test.go +++ b/stream/adpcm/adpcm_test.go @@ -1,3 +1,30 @@ +/* +NAME + adpcm_test.go + +DESCRIPTION + See Readme.md + +AUTHOR + Trek Hopton + +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 import ( @@ -14,6 +41,7 @@ func TestEncodeBlock(t *testing.T) { if err != nil { t.Errorf("Unable to read input PCM file: %v", err) } + //encode adpcm var comp []byte start := 0 @@ -23,13 +51,13 @@ func TestEncodeBlock(t *testing.T) { encBlock, err := EncodeBlock(block) if err != nil { - //todo: use correct logging of error t.Errorf("Unable to encode block: %v", err) } comp = append(comp, encBlock...) start = i + 1 } } + //read expected adpcm file exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm") if err != nil { @@ -49,6 +77,7 @@ func TestDecodeBlock(t *testing.T) { if err != nil { t.Errorf("Unable to read input ADPCM file: %v", err) } + //decode adpcm var decoded []byte start := 0 @@ -63,6 +92,7 @@ func TestDecodeBlock(t *testing.T) { start = i + 1 } } + //read expected pcm file exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm") if err != nil {