ADPCM: pre-allocating memory for entire known length of output byte slice

This commit is contained in:
Trek H 2019-02-13 16:53:25 +10:30
parent 43c6027888
commit 7d3829d19f
3 changed files with 48 additions and 47 deletions

View File

@ -29,6 +29,7 @@ package main
import ( import (
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"bitbucket.org/ausocean/av/stream/adpcm" "bitbucket.org/ausocean/av/stream/adpcm"
@ -39,30 +40,30 @@ import (
func main() { func main() {
var inPath string var inPath string
var outPath string var outPath string
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)
} }
fmt.Println("Read", len(comp), "bytes from file", inPath)
//decode adpcm // decode adpcm
var decoded []byte inBSize := 256
start := 0 numBlocks := int(len(comp) / inBSize)
for i := 0; i < len(comp); i++ { outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4
if i%256 == 255 { decoded := make([]byte, 0, outBSize*numBlocks)
block := comp[start : i+1] for i, start := inBSize-1, 0; i < len(comp); i += inBSize {
decBlock, err := adpcm.DecodeBlock(block) block := comp[start : i+1]
if err != nil { decBlock, err := adpcm.DecodeBlock(block)
panic(err) if err != nil {
} panic(err)
decoded = append(decoded, decBlock...)
start = i + 1
} }
decoded = append(decoded, decBlock...)
start = i + 1
} }
// save pcm to file // save pcm to file
@ -70,4 +71,5 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath)
} }

View File

@ -29,11 +29,14 @@ package main
import ( import (
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"bitbucket.org/ausocean/av/stream/adpcm" "bitbucket.org/ausocean/av/stream/adpcm"
) )
// This program accepts an input pcm file and outputs an encoded adpcm file.
// Input and output file names can be specified as arguments.
func main() { func main() {
var inPath string var inPath string
var adpcmPath string var adpcmPath string
@ -46,22 +49,21 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Read", len(pcm), "bytes from file", inPath)
//encode adpcm //encode adpcm
var comp []byte inBSize := 1010
start := 0 numBlocks := int(len(pcm) / inBSize)
bSize := 1010 outBSize := int(float32(inBSize/4) + float32(3.5)) // compression is 4:1 and 3.5 bytes of info are added to each block
for i := 0; i < len(pcm); i++ { comp := make([]byte, 0, outBSize*numBlocks)
if i%bSize == bSize-1 { for i, start := inBSize-1, 0; i < len(pcm); i += inBSize {
block := pcm[start : i+1] block := pcm[start : i+1]
encBlock, err := adpcm.EncodeBlock(block)
encBlock, err := adpcm.EncodeBlock(block) if err != nil {
if err != nil { panic(err)
panic(err)
}
comp = append(comp, encBlock...)
start = i + 1
} }
comp = append(comp, encBlock...)
start = i + 1
} }
// save adpcm to file // save adpcm to file
@ -69,4 +71,5 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath)
} }

View File

@ -44,18 +44,16 @@ func TestEncodeBlock(t *testing.T) {
//encode adpcm //encode adpcm
var comp []byte var comp []byte
start := 0 bSize := 1010
for i := 0; i < len(pcm); i++ { for i, start := bSize-1, 0; i < len(pcm); i += bSize {
if i%1010 == 1009 { block := pcm[start : i+1]
block := pcm[start : i+1]
encBlock, err := EncodeBlock(block) encBlock, err := EncodeBlock(block)
if err != nil { if err != nil {
t.Errorf("Unable to encode block: %v", err) t.Errorf("Unable to encode block: %v", err)
}
comp = append(comp, encBlock...)
start = i + 1
} }
comp = append(comp, encBlock...)
start = i + 1
} }
//read expected adpcm file //read expected adpcm file
@ -80,17 +78,15 @@ func TestDecodeBlock(t *testing.T) {
//decode adpcm //decode adpcm
var decoded []byte var decoded []byte
start := 0 bSize := 256
for i := 0; i < len(comp); i++ { for i, start := bSize-1, 0; i < len(comp); i += bSize {
if i%256 == 255 { block := comp[start : i+1]
block := comp[start : i+1] decBlock, err := DecodeBlock(block)
decBlock, err := DecodeBlock(block) if err != nil {
if err != nil { t.Errorf("Unable to decode block: %v", err)
t.Errorf("Unable to decode block: %v", err)
}
decoded = append(decoded, decBlock...)
start = i + 1
} }
decoded = append(decoded, decBlock...)
start = i + 1
} }
//read expected pcm file //read expected pcm file