mirror of https://bitbucket.org/ausocean/av.git
ADPCM: pre-allocating memory for entire known length of output byte slice
This commit is contained in:
parent
43c6027888
commit
7d3829d19f
|
@ -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,7 +40,6 @@ 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()
|
||||||
|
@ -49,12 +49,14 @@ func main() {
|
||||||
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)
|
||||||
|
for i, start := inBSize-1, 0; i < len(comp); i += inBSize {
|
||||||
block := comp[start : i+1]
|
block := comp[start : i+1]
|
||||||
decBlock, err := adpcm.DecodeBlock(block)
|
decBlock, err := adpcm.DecodeBlock(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -63,11 +65,11 @@ func main() {
|
||||||
decoded = append(decoded, decBlock...)
|
decoded = append(decoded, decBlock...)
|
||||||
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 {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println("Decoded and wrote", len(decoded), "bytes to file", outPath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,15 +49,15 @@ 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)
|
||||||
|
@ -62,11 +65,11 @@ func main() {
|
||||||
comp = append(comp, encBlock...)
|
comp = append(comp, encBlock...)
|
||||||
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 {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,9 +44,8 @@ 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)
|
||||||
|
@ -56,7 +55,6 @@ func TestEncodeBlock(t *testing.T) {
|
||||||
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")
|
||||||
|
@ -80,9 +78,8 @@ 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 {
|
||||||
|
@ -91,7 +88,6 @@ func TestDecodeBlock(t *testing.T) {
|
||||||
decoded = append(decoded, decBlock...)
|
decoded = append(decoded, decBlock...)
|
||||||
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")
|
||||||
|
|
Loading…
Reference in New Issue