Fixes encoding

This commit is contained in:
zyxar 2015-08-18 10:26:11 +08:00
parent 351b7f600b
commit cdd071c67f
4 changed files with 61 additions and 20 deletions

View file

@ -67,12 +67,12 @@ func Compress(infile, outfile *os.File, passwd string, cb Callback) (err error)
if buf_size >= data_size {
buf_size = data_size
}
if read_len, err = infile.Read(buffer[:buf_size]); err != nil || read_len == 0 {
if read_len, err = infile.Read(buffer[:buf_size]); err != nil || read_len != int(buf_size) {
err = TTA_READ_ERROR
return
}
encoder.ProcessStream(buffer[:read_len], cb)
data_size -= uint32(read_len)
encoder.ProcessStream(buffer[:buf_size], cb)
data_size -= buf_size
}
encoder.Close()
return
@ -93,14 +93,20 @@ func (this *Encoder) ProcessStream(in []byte, cb Callback) {
in = in[this.depth:]
tmp = next << this.shift_bits
i := 0
for len(in) > 0 {
index := 0
for {
curr = next
next = read_buffer(in, this.depth)
in = in[this.depth:]
tmp = next << this.shift_bits
if index < len(in) {
next = read_buffer(in[index:], this.depth)
tmp = next << this.shift_bits
} else {
next = 0
tmp = 0
}
index += int(this.depth)
// transform data
if this.channels > 1 {
if i < this.channels {
if i < this.channels-1 {
res = next - curr
curr = res
} else {
@ -131,6 +137,9 @@ func (this *Encoder) ProcessStream(in []byte, cb Callback) {
}
this.frame_init(this.fnum)
}
if index >= int(this.depth)+len(in) {
break
}
}
}
@ -143,14 +152,20 @@ func (this *Encoder) ProcessFrame(in []byte) {
in = in[this.depth:]
tmp = next << this.shift_bits
i := 0
for len(in) > 0 {
index := 0
for {
curr = next
next = read_buffer(in, this.depth)
in = in[this.depth:]
tmp = next << this.shift_bits
if index < len(in) {
next = read_buffer(in[index:], this.depth)
tmp = next << this.shift_bits
} else {
next = 0
tmp = 0
}
index += int(this.depth)
// transform data
if this.channels > 1 {
if i < this.channels {
if i < this.channels-1 {
res = next - curr
curr = res
} else {
@ -176,6 +191,9 @@ func (this *Encoder) ProcessFrame(in []byte) {
this.rate = (this.fifo.count << 3) / 1070
break
}
if index >= int(this.depth)+len(in) {
break
}
}
}

10
fifo.go
View file

@ -161,18 +161,18 @@ func (s *tta_fifo) write_start() {
}
func (s *tta_fifo) write_done() error {
buffer_size := s.pos
if buffer_size > 0 {
if n, err := s.io.Write(s.buffer[:]); err != nil || int32(n) != buffer_size {
if s.pos > 0 {
if n, err := s.io.Write(s.buffer[:s.pos]); err != nil || n != int(s.pos) {
return TTA_WRITE_ERROR
}
s.pos = 0
}
return nil
}
func (s *tta_fifo) write_byte(v byte) error {
if s.pos == int32(len(s.buffer)) {
if n, err := s.io.Write(s.buffer[:]); err != nil || int32(n) != TTA_FIFO_BUFFER_SIZE {
if s.pos == TTA_FIFO_BUFFER_SIZE {
if n, err := s.io.Write(s.buffer[:]); err != nil || n != TTA_FIFO_BUFFER_SIZE {
return TTA_WRITE_ERROR
}
s.pos = 0

View file

@ -85,3 +85,26 @@ func TestReadUint32(t *testing.T) {
t.Error("read_uint32 fail @ pos")
}
}
func TestWriteByte(t *testing.T) {
fifo := tta_fifo{}
fifo.pos = 0
fifo.end = TTA_FIFO_BUFFER_SIZE
for i := 0; i < TTA_FIFO_BUFFER_SIZE; i++ {
if err := fifo.write_byte(byte(i)); err != nil {
t.Errorf("write_byte fail @ %d, %v\n", i, err)
}
if fifo.count != uint32(i+1) {
t.Errorf("write_byte fail @ count - %d\n", i)
}
if fifo.pos != int32(i+1) {
t.Errorf("write_byte fail @ pos - %d\n", i)
}
}
if fifo.count != TTA_FIFO_BUFFER_SIZE {
t.Error("write_byte fail @ count")
}
if fifo.pos != TTA_FIFO_BUFFER_SIZE {
t.Error("write_byte fail @ pos")
}
}

View file

@ -57,9 +57,9 @@ func write_buffer(src int32, p []byte, depth uint32) {
func read_buffer(p []byte, depth uint32) (v int32) {
switch depth {
case 2:
v = int32(binary.LittleEndian.Uint16(p))
v = int32(int16(binary.LittleEndian.Uint16(p)))
case 1:
v = int32(p[0])
v = int32(int8(p[0]))
default:
v = int32(binary.LittleEndian.Uint32(p))
}