audio/mp3: Make Get_Byte a Go function

This commit is contained in:
Hajime Hoshi 2017-06-17 01:36:58 +09:00
parent bdb87b0257
commit b08897a4e5
3 changed files with 47 additions and 22 deletions

View File

@ -39,14 +39,28 @@ var (
//export Read_CRC //export Read_CRC
func Read_CRC() C.int { func Read_CRC() C.int {
if Get_Byte() == eof || Get_Byte() == eof { buf := make([]int, 2)
return C.FALSE n := 0
var err error
for n < 2 && err == nil {
nn, err2 := getBytes(buf[n:])
n += nn
err = err2
}
if err == io.EOF {
if n < 2 {
return C.ERROR
}
return C.OK
}
if err != nil {
g_error = err
return C.ERROR
} }
return C.OK return C.OK
} }
//export Get_Byte func getByte() (uint8, error) {
func Get_Byte() C.unsigned {
for len(readerCache) == 0 && !readerEOF { for len(readerCache) == 0 && !readerEOF {
buf := make([]uint8, 4096) buf := make([]uint8, 4096)
n, err := reader.Read(buf) n, err := reader.Read(buf)
@ -55,26 +69,26 @@ func Get_Byte() C.unsigned {
if err == io.EOF { if err == io.EOF {
readerEOF = true readerEOF = true
} else { } else {
panic(err) return 0, err
} }
} }
} }
if len(readerCache) == 0 { if len(readerCache) == 0 {
return eof return 0, io.EOF
} }
b := readerCache[0] b := readerCache[0]
readerCache = readerCache[1:] readerCache = readerCache[1:]
readerPos++ readerPos++
return C.unsigned(b) return b, nil
} }
func getBytes(buf []int) (int, error) { func getBytes(buf []int) (int, error) {
for i := range buf { for i := range buf {
v := Get_Byte() v, err := getByte()
if v == eof { buf[i] = int(v)
if err == io.EOF {
return i, io.EOF return i, io.EOF
} }
buf[i] = int(v)
} }
return len(buf), nil return len(buf), nil
} }

View File

@ -84,8 +84,6 @@ typedef struct { /* Scale factor band indices,for long and short windows */
} }
t_sf_band_indices; t_sf_band_indices;
unsigned Get_Byte(void);
unsigned Get_Bytes(unsigned num, unsigned* data_vec);
unsigned Get_Filepos(void); unsigned Get_Filepos(void);
int Decode_L3(void); int Decode_L3(void);
int Read_Frame(void); int Read_Frame(void);

View File

@ -25,6 +25,7 @@ import "C"
import ( import (
"fmt" "fmt"
"io"
) )
func isHeader(header uint32) bool { func isHeader(header uint32) bool {
@ -46,14 +47,24 @@ func isHeader(header uint32) bool {
//export Read_Header //export Read_Header
func Read_Header() C.int { func Read_Header() C.int {
/* Get the next four bytes from the bitstream */ /* Get the next four bytes from the bitstream */
b1 := uint32(Get_Byte()) buf := make([]int, 4)
b2 := uint32(Get_Byte()) n := 0
b3 := uint32(Get_Byte()) var err error
b4 := uint32(Get_Byte()) for n < 4 && err == nil {
/* If we got an End Of File condition we're done */ nn, err2 := getBytes(buf[n:])
if (b1 == eof) || (b2 == eof) || (b3 == eof) || (b4 == eof) { n += nn
err = err2
}
if n < 4 {
if err != io.EOF {
g_error = err
}
return C.ERROR return C.ERROR
} }
b1 := uint32(buf[0])
b2 := uint32(buf[1])
b3 := uint32(buf[2])
b4 := uint32(buf[3])
header := (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0) header := (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
for !isHeader(uint32(header)) { for !isHeader(uint32(header)) {
/* No,so scan the bitstream one byte at a time until we find it or EOF */ /* No,so scan the bitstream one byte at a time until we find it or EOF */
@ -62,12 +73,14 @@ func Read_Header() C.int {
b2 = b3 b2 = b3
b3 = b4 b3 = b4
/* Get one new byte from the bitstream */ /* Get one new byte from the bitstream */
b4 = uint32(Get_Byte()) b, err := getByte()
/* If we got an End Of File condition we're done */ if err != nil {
if b4 == eof { if err != io.EOF {
g_error = err
}
return C.ERROR return C.ERROR
} }
/* Make up the new header */ b4 = uint32(b)
header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0) header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
} /* while... */ } /* while... */
/* If we get here we've found the sync word,and can decode the header /* If we get here we've found the sync word,and can decode the header