From 02f13a519504ee5ca27a7c0904369cdba7120860 Mon Sep 17 00:00:00 2001 From: Markus Tzoe Date: Wed, 19 Apr 2017 11:02:24 +0800 Subject: [PATCH] minor change: struct rename --- codec.go | 31 ++++++++++++++++ const.go | 2 +- decoder.go | 34 ++++++++--------- encoder.go | 34 ++++++++--------- fifo.go | 101 +++++++++++++++++++++++++++++---------------------- fifo_test.go | 8 ++-- filter.go | 33 +++++++++++++---- tta.go | 63 -------------------------------- 8 files changed, 153 insertions(+), 153 deletions(-) create mode 100644 codec.go delete mode 100644 tta.go diff --git a/codec.go b/codec.go new file mode 100644 index 0000000..7e47863 --- /dev/null +++ b/codec.go @@ -0,0 +1,31 @@ +package tta + +type Info struct { + format uint32 // audio format + nch uint32 // number of channels + bps uint32 // bits per sample + sps uint32 // samplerate (sps) + samples uint32 // data length in samples +} + +type adapter struct { + k0 uint32 + k1 uint32 + sum0 uint32 + sum1 uint32 +} + +func (a *adapter) init(k0, k1 uint32) { + a.k0 = k0 + a.k1 = k1 + a.sum0 = shift16[k0] + a.sum1 = shift16[k1] +} + +type codec struct { + filter Filter + adapter adapter + prev int32 +} + +type Callback func(uint32, uint32, uint32) diff --git a/const.go b/const.go index 6731505..0c000df 100644 --- a/const.go +++ b/const.go @@ -208,4 +208,4 @@ var crc64TableHigh = [256]uint32{ 0x5dedc41a, 0x1f1d25f1, 0xd80c07cd, 0x9afce626, } -var fltSet = [3]int32{10, 9, 10} +var shifts = [3]uint32{10, 9, 10} diff --git a/decoder.go b/decoder.go index 3c71d65..67125ec 100644 --- a/decoder.go +++ b/decoder.go @@ -6,10 +6,10 @@ import ( ) type Decoder struct { - codec [maxNCH]ttaCodec // 1 per channel - channels int // number of channels/codecs - data [8]byte // codec initialization data - fifo ttaFifo + codecs [maxNCH]codec // 1 per channel + channels int // number of channels/codecs + data [8]byte // codec initialization data + fifo fifo passwordSet bool // password protection flag seekAllowed bool // seek table flag seekTable []uint64 // the playing position table @@ -83,12 +83,12 @@ func (d *Decoder) ProcessStream(out []byte, cb Callback) int32 { i := 0 outClone := out[:] for d.fpos < d.flen && len(outClone) > 0 { - value = d.fifo.getValue(&d.codec[i].rice) + value = d.fifo.getValue(&d.codecs[i].adapter) // decompress stage 1: adaptive hybrid filter - d.codec[i].filter.Decode(&value) + d.codecs[i].filter.Decode(&value) // decompress stage 2: fixed order 1 prediction - value += ((d.codec[i].prev * ((1 << 5) - 1)) >> 5) - d.codec[i].prev = value + value += ((d.codecs[i].prev * ((1 << 5) - 1)) >> 5) + d.codecs[i].prev = value cache[i] = value if i < d.channels-1 { i++ @@ -148,12 +148,12 @@ func (d *Decoder) ProcessFrame(inSize uint32, out []byte) int32 { var ret int32 outClone := out[:] for d.fifo.count < inSize && len(outClone) > 0 { - value = d.fifo.getValue(&d.codec[i].rice) + value = d.fifo.getValue(&d.codecs[i].adapter) // decompress stage 1: adaptive hybrid filter - d.codec[i].filter.Decode(&value) + d.codecs[i].filter.Decode(&value) // decompress stage 2: fixed order 1 prediction - value += ((d.codec[i].prev * ((1 << 5) - 1)) >> 5) - d.codec[i].prev = value + value += ((d.codecs[i].prev * ((1 << 5) - 1)) >> 5) + d.codecs[i].prev = value cache[i] = value if i < d.channels-1 { i++ @@ -219,7 +219,7 @@ func (d *Decoder) frameInit(frame uint32, seekNeeded bool) (err error) { if frame >= d.frames { return } - shift := fltSet[d.depth-1] + shift := shifts[d.depth-1] d.fnum = frame if seekNeeded && d.seekAllowed { pos := d.seekTable[d.fnum] @@ -237,12 +237,12 @@ func (d *Decoder) frameInit(frame uint32, seekNeeded bool) (err error) { } for i := 0; i < d.channels; i++ { if sseEnabled { - d.codec[i].filter = NewSSEFilter(d.data, shift) + d.codecs[i].filter = NewSSEFilter(d.data, shift) } else { - d.codec[i].filter = NewCompatibleFilter(d.data, shift) + d.codecs[i].filter = NewCompatibleFilter(d.data, shift) } - d.codec[i].rice.init(10, 10) - d.codec[i].prev = 0 + d.codecs[i].adapter.init(10, 10) + d.codecs[i].prev = 0 } d.fpos = 0 d.fifo.reset() diff --git a/encoder.go b/encoder.go index 4c3ea21..28683ae 100644 --- a/encoder.go +++ b/encoder.go @@ -7,10 +7,10 @@ import ( ) type Encoder struct { - codec [maxNCH]ttaCodec // 1 per channel - channels int // number of channels/codecs - data [8]byte // codec initialization data - fifo ttaFifo + codecs [maxNCH]codec // 1 per channel + channels int // number of channels/codecs + data [8]byte // codec initialization data + fifo fifo seekTable []uint64 // the playing position table format uint32 // tta data format rate uint32 // bitrate (kbps) @@ -115,11 +115,11 @@ func (e *Encoder) ProcessStream(in []byte, cb Callback) { } // compress stage 1: fixed order 1 prediction tmp = curr - curr -= ((e.codec[i].prev * ((1 << 5) - 1)) >> 5) - e.codec[i].prev = tmp + curr -= ((e.codecs[i].prev * ((1 << 5) - 1)) >> 5) + e.codecs[i].prev = tmp // compress stage 2: adaptive hybrid filter - e.codec[i].filter.Encode(&curr) - e.fifo.putValue(&e.codec[i].rice, curr) + e.codecs[i].filter.Encode(&curr) + e.fifo.putValue(&e.codecs[i].adapter, curr) if i < e.channels-1 { i++ } else { @@ -174,11 +174,11 @@ func (e *Encoder) ProcessFrame(in []byte) { } // compress stage 1: fixed order 1 prediction tmp = curr - curr -= ((e.codec[i].prev * ((1 << 5) - 1)) >> 5) - e.codec[i].prev = tmp + curr -= ((e.codecs[i].prev * ((1 << 5) - 1)) >> 5) + e.codecs[i].prev = tmp // compress stage 2: adaptive hybrid filter - e.codec[i].filter.Encode(&curr) - e.fifo.putValue(&e.codec[i].rice, curr) + e.codecs[i].filter.Encode(&curr) + e.fifo.putValue(&e.codecs[i].adapter, curr) if i < e.channels-1 { i++ } else { @@ -222,7 +222,7 @@ func (e *Encoder) frameInit(frame uint32) (err error) { if frame >= e.frames { return } - shift := fltSet[e.depth-1] + shift := shifts[e.depth-1] e.fnum = frame if e.fnum == e.frames-1 { e.flen = e.flenLast @@ -232,12 +232,12 @@ func (e *Encoder) frameInit(frame uint32) (err error) { // init entropy encoder for i := 0; i < e.channels; i++ { if sseEnabled { - e.codec[i].filter = NewSSEFilter(e.data, shift) + e.codecs[i].filter = NewSSEFilter(e.data, shift) } else { - e.codec[i].filter = NewCompatibleFilter(e.data, shift) + e.codecs[i].filter = NewCompatibleFilter(e.data, shift) } - e.codec[i].rice.init(10, 10) - e.codec[i].prev = 0 + e.codecs[i].adapter.init(10, 10) + e.codecs[i].prev = 0 } e.fpos = 0 e.fifo.reset() diff --git a/fifo.go b/fifo.go index f5acf30..84292b4 100644 --- a/fifo.go +++ b/fifo.go @@ -1,6 +1,21 @@ package tta -func (s *ttaFifo) readByte() (v byte) { +import ( + "io" +) + +type fifo struct { + buffer [fifoBufferSize]byte + pos int32 + end int32 + bcount uint32 // count of bits in cache + bcache uint32 // bit cache + crc uint32 + count uint32 + io io.ReadWriteSeeker +} + +func (s *fifo) readByte() (v byte) { if s.pos >= s.end { v, _ := s.io.Read(s.buffer[:]) // FIXME: handle this error s.end = int32(v) @@ -13,14 +28,14 @@ func (s *ttaFifo) readByte() (v byte) { return } -func (s *ttaFifo) readUint16() (v uint16) { +func (s *fifo) readUint16() (v uint16) { v = 0 v |= uint16(s.readByte()) v |= uint16(s.readByte()) << 8 return } -func (s *ttaFifo) readUint32() (v uint32) { +func (s *fifo) readUint32() (v uint32) { v = 0 v |= uint32(s.readByte()) v |= uint32(s.readByte()) << 8 @@ -29,30 +44,30 @@ func (s *ttaFifo) readUint32() (v uint32) { return } -func (s *ttaFifo) readCrc32() bool { +func (s *fifo) readCrc32() bool { crc := s.crc ^ 0xFFFFFFFF return crc == s.readUint32() } -func (s *ttaFifo) readStart() { +func (s *fifo) readStart() { s.pos = s.end } -func (s *ttaFifo) reset() { +func (s *fifo) reset() { s.crc = 0xFFFFFFFF s.bcache = 0 s.bcount = 0 s.count = 0 } -func (s *ttaFifo) readSkipBytes(size uint32) { +func (s *fifo) readSkipBytes(size uint32) { for size > 0 { size-- s.readByte() } } -func (s *ttaFifo) skipId3v2() (size uint32) { +func (s *fifo) skipId3v2() (size uint32) { s.reset() if 'I' != s.readByte() || 'D' != s.readByte() || '3' != s.readByte() { s.pos = 0 @@ -71,7 +86,7 @@ func (s *ttaFifo) skipId3v2() (size uint32) { return } -func (s *ttaFifo) getValue(rice *ttaAdapt) (value int32) { +func (s *fifo) getValue(ad *adapter) (value int32) { if s.bcache^bitMask[s.bcount] == 0 { value += int32(s.bcount) s.bcache = uint32(s.readByte()) @@ -93,11 +108,11 @@ func (s *ttaFifo) getValue(rice *ttaAdapt) (value int32) { var level, k, tmp uint32 if value != 0 { level = 1 - k = rice.k1 + k = ad.k1 value-- } else { level = 0 - k = rice.k0 + k = ad.k0 } if k != 0 { for s.bcount < k { @@ -111,20 +126,20 @@ func (s *ttaFifo) getValue(rice *ttaAdapt) (value int32) { s.bcache &= bitMask[s.bcount] } if level != 0 { - rice.sum1 += uint32(value) - (rice.sum1 >> 4) - if rice.k1 > 0 && rice.sum1 < shift16[rice.k1] { - rice.k1-- - } else if rice.sum1 > shift16[rice.k1+1] { - rice.k1++ + ad.sum1 += uint32(value) - (ad.sum1 >> 4) + if ad.k1 > 0 && ad.sum1 < shift16[ad.k1] { + ad.k1-- + } else if ad.sum1 > shift16[ad.k1+1] { + ad.k1++ } - value += int32(bitShift[rice.k0]) + value += int32(bitShift[ad.k0]) } - rice.sum0 += uint32(value) - (rice.sum0 >> 4) - if rice.k0 > 0 && rice.sum0 < shift16[rice.k0] { - rice.k0-- - } else if rice.sum0 > shift16[rice.k0+1] { - rice.k0++ + ad.sum0 += uint32(value) - (ad.sum0 >> 4) + if ad.k0 > 0 && ad.sum0 < shift16[ad.k0] { + ad.k0-- + } else if ad.sum0 > shift16[ad.k0+1] { + ad.k0++ } // ((x & 1)?((x + 1) >> 1):(-x >> 1)) if value&1 != 0 { @@ -135,11 +150,11 @@ func (s *ttaFifo) getValue(rice *ttaAdapt) (value int32) { return } -func (s *ttaFifo) writeStart() { +func (s *fifo) writeStart() { s.pos = 0 } -func (s *ttaFifo) writeDone() error { +func (s *fifo) writeDone() error { if s.pos > 0 { if n, err := s.io.Write(s.buffer[:s.pos]); err != nil || n != int(s.pos) { return errWrite @@ -149,7 +164,7 @@ func (s *ttaFifo) writeDone() error { return nil } -func (s *ttaFifo) writeByte(v byte) error { +func (s *fifo) writeByte(v byte) error { if s.pos == fifoBufferSize { if n, err := s.io.Write(s.buffer[:]); err != nil || n != fifoBufferSize { return errWrite @@ -163,7 +178,7 @@ func (s *ttaFifo) writeByte(v byte) error { return nil } -func (s *ttaFifo) writeUint16(v uint16) error { +func (s *fifo) writeUint16(v uint16) error { if err := s.writeByte(byte(v)); err != nil { return err } @@ -173,7 +188,7 @@ func (s *ttaFifo) writeUint16(v uint16) error { return nil } -func (s *ttaFifo) writeUint32(v uint32) error { +func (s *fifo) writeUint32(v uint32) error { if err := s.writeByte(byte(v)); err != nil { return err } @@ -186,11 +201,11 @@ func (s *ttaFifo) writeUint32(v uint32) error { return s.writeByte(byte(v >> 24)) } -func (s *ttaFifo) writeCrc32() error { +func (s *fifo) writeCrc32() error { return s.writeUint32(s.crc ^ 0xFFFFFFFF) } -func (s *ttaFifo) writeSkipBytes(size uint32) error { +func (s *fifo) writeSkipBytes(size uint32) error { for size > 0 { if err := s.writeByte(0); err != nil { return err @@ -200,7 +215,7 @@ func (s *ttaFifo) writeSkipBytes(size uint32) error { return nil } -func (s *ttaFifo) putValue(rice *ttaAdapt, value int32) { +func (s *fifo) putValue(ad *adapter, value int32) { var k, unary, outval uint32 if value > 0 { outval = (uint32(value) << 1) - 1 @@ -208,22 +223,22 @@ func (s *ttaFifo) putValue(rice *ttaAdapt, value int32) { outval = uint32(-value) << 1 } // encode Rice unsigned - k = rice.k0 - rice.sum0 += outval - (rice.sum0 >> 4) - if rice.k0 > 0 && rice.sum0 < shift16[rice.k0] { - rice.k0-- - } else if rice.sum0 > shift16[rice.k0+1] { - rice.k0++ + k = ad.k0 + ad.sum0 += outval - (ad.sum0 >> 4) + if ad.k0 > 0 && ad.sum0 < shift16[ad.k0] { + ad.k0-- + } else if ad.sum0 > shift16[ad.k0+1] { + ad.k0++ } if outval >= bitShift[k] { outval -= bitShift[k] - k = rice.k1 - rice.sum1 += outval - (rice.sum1 >> 4) - if rice.k1 > 0 && rice.sum1 < shift16[rice.k1] { - rice.k1-- - } else if rice.sum1 > shift16[rice.k1+1] { - rice.k1++ + k = ad.k1 + ad.sum1 += outval - (ad.sum1 >> 4) + if ad.k1 > 0 && ad.sum1 < shift16[ad.k1] { + ad.k1-- + } else if ad.sum1 > shift16[ad.k1+1] { + ad.k1++ } unary = 1 + (outval >> k) } else { @@ -260,7 +275,7 @@ func (s *ttaFifo) putValue(rice *ttaAdapt, value int32) { } } -func (s *ttaFifo) flushBitCache() { +func (s *fifo) flushBitCache() { for s.bcount > 0 { s.writeByte(byte(s.bcache)) s.bcache >>= 8 diff --git a/fifo_test.go b/fifo_test.go index 4855402..790d1de 100644 --- a/fifo_test.go +++ b/fifo_test.go @@ -6,7 +6,7 @@ import ( func TestReadByte(t *testing.T) { t.Parallel() - fifo := ttaFifo{} + fifo := fifo{} for i := 0; i < fifoBufferSize; i++ { fifo.buffer[i] = byte(i) } @@ -33,7 +33,7 @@ func TestReadByte(t *testing.T) { func TestReadUint16(t *testing.T) { t.Parallel() - fifo := ttaFifo{} + fifo := fifo{} for i := 0; i < fifoBufferSize; i++ { fifo.buffer[i] = byte(i) } @@ -62,7 +62,7 @@ func TestReadUint16(t *testing.T) { func TestReadUint32(t *testing.T) { t.Parallel() - fifo := ttaFifo{} + fifo := fifo{} for i := 0; i < fifoBufferSize; i++ { fifo.buffer[i] = byte(i) } @@ -91,7 +91,7 @@ func TestReadUint32(t *testing.T) { func TestWriteByte(t *testing.T) { t.Parallel() - fifo := ttaFifo{} + fifo := fifo{} fifo.pos = 0 fifo.end = fifoBufferSize for i := 0; i < fifoBufferSize; i++ { diff --git a/filter.go b/filter.go index ab10288..85bb052 100644 --- a/filter.go +++ b/filter.go @@ -2,8 +2,25 @@ package tta // TODO: SSE4 optimization -func NewCompatibleFilter(data [8]byte, shift int32) Filter { - t := ttaFilterCompat{} +type Filter interface { + Decode(*int32) + Encode(*int32) +} + +type filterCompat struct { + index int32 + error int32 + round int32 + shift uint32 + qm [8]int32 + dx [24]int32 + dl [24]int32 +} + +type filterSse filterCompat + +func NewCompatibleFilter(data [8]byte, shift uint32) Filter { + t := filterCompat{} t.shift = shift t.round = 1 << uint32(shift-1) t.qm[0] = int32(int8(data[0])) @@ -17,7 +34,7 @@ func NewCompatibleFilter(data [8]byte, shift int32) Filter { return &t } -func (t *ttaFilterCompat) Decode(in *int32) { +func (t *filterCompat) Decode(in *int32) { pa := t.dl[:] pb := t.qm[:] pm := t.dx[:] @@ -67,7 +84,7 @@ func (t *ttaFilterCompat) Decode(in *int32) { pa[4] += pa[5] } -func (t *ttaFilterCompat) Encode(in *int32) { +func (t *filterCompat) Encode(in *int32) { pa := t.dl[:] pb := t.qm[:] pm := t.dx[:] @@ -120,8 +137,8 @@ func (t *ttaFilterCompat) Encode(in *int32) { t.error = *in } -func NewSSEFilter(data [8]byte, shift int32) Filter { - t := ttaFilterSse{} +func NewSSEFilter(data [8]byte, shift uint32) Filter { + t := filterSse{} t.shift = shift t.round = 1 << uint32(shift-1) t.qm[0] = int32(int8(data[0])) @@ -135,8 +152,8 @@ func NewSSEFilter(data [8]byte, shift int32) Filter { return &t } -func (t *ttaFilterSse) Decode(in *int32) { +func (t *filterSse) Decode(in *int32) { } -func (t *ttaFilterSse) Encode(in *int32) { +func (t *filterSse) Encode(in *int32) { } diff --git a/tta.go b/tta.go deleted file mode 100644 index cbd341d..0000000 --- a/tta.go +++ /dev/null @@ -1,63 +0,0 @@ -package tta - -import ( - "io" -) - -type Info struct { - format uint32 // audio format - nch uint32 // number of channels - bps uint32 // bits per sample - sps uint32 // samplerate (sps) - samples uint32 // data length in samples -} - -type Filter interface { - Decode(*int32) - Encode(*int32) -} - -type ttaFilterCompat struct { - index int32 - error int32 - round int32 - shift int32 - qm [8]int32 - dx [24]int32 - dl [24]int32 -} - -type ttaFilterSse ttaFilterCompat - -type ttaAdapt struct { - k0 uint32 - k1 uint32 - sum0 uint32 - sum1 uint32 -} - -func (rice *ttaAdapt) init(k0, k1 uint32) { - rice.k0 = k0 - rice.k1 = k1 - rice.sum0 = shift16[k0] - rice.sum1 = shift16[k1] -} - -type ttaCodec struct { - filter Filter - rice ttaAdapt - prev int32 -} - -type ttaFifo struct { - buffer [fifoBufferSize]byte - pos int32 - end int32 - bcount uint32 // count of bits in cache - bcache uint32 // bit cache - crc uint32 - count uint32 - io io.ReadWriteSeeker -} - -type Callback func(uint32, uint32, uint32)