audio/mp3: Remove g_error

This commit is contained in:
Hajime Hoshi 2017-06-17 03:01:39 +09:00
parent 61c76a3749
commit 1c86a90c8e
4 changed files with 37 additions and 33 deletions

View File

@ -20,7 +20,6 @@ package mp3
import "C" import "C"
import ( import (
"errors"
"io" "io"
"unsafe" "unsafe"
) )
@ -88,8 +87,6 @@ func writeToWriter(data unsafe.Pointer, size C.int) C.size_t {
return C.size_t(n) return C.size_t(n)
} }
var g_error error
func decode(r io.Reader, w io.Writer) error { func decode(r io.Reader, w io.Writer) error {
reader = r reader = r
writer = w writer = w
@ -102,10 +99,7 @@ func decode(r io.Reader, w io.Writer) error {
if Get_Filepos() == eof { if Get_Filepos() == eof {
break break
} }
if err != nil {
return err return err
} }
return errors.New("mp3: not enough maindata to decode frame") return nil
}
return g_error
} }

View File

@ -175,7 +175,7 @@ var theMainDataBytes mainDataBytes
func getMainData(size int, begin int) error { func getMainData(size int, begin int) error {
if size > 1500 { if size > 1500 {
g_error = fmt.Errorf("size = %d", size) return fmt.Errorf("mp3: size = %d", size)
} }
// Check that there's data available from previous frames if needed // Check that there's data available from previous frames if needed
if int(begin) > theMainDataBytes.top { if int(begin) > theMainDataBytes.top {
@ -202,7 +202,8 @@ func getMainData(size int, begin int) error {
theMainDataBytes.pos = 0 theMainDataBytes.pos = 0
theMainDataBytes.idx = 0 theMainDataBytes.idx = 0
theMainDataBytes.top += size theMainDataBytes.top += size
return fmt.Errorf("mp3: not enought frame data; read next frame?") // TODO: Define a special error and enable to continue the next frame.
return fmt.Errorf("mp3: frame can't be decoded")
} }
/* Copy data from previous frames */ /* Copy data from previous frames */
for i := 0; i < begin; i++ { for i := 0; i < begin; i++ {

View File

@ -53,22 +53,25 @@ func readFrame() error {
if err := readHeader(); err != nil { if err := readHeader(); err != nil {
return err return err
} }
/* Get CRC word if present */ // Get CRC word if present
if C.g_frame_header.protection_bit == 0 { if C.g_frame_header.protection_bit == 0 {
if err := readCRC(); err != nil { if err := readCRC(); err != nil {
return err return err
} }
} }
if C.g_frame_header.layer == 3 { /* Get audio data */
Read_Audio_L3() /* Get side info */ if C.g_frame_header.layer != 3 {
/* If there's not enough main data in the bit reservoir, return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer)
* signal to calling function so that decoding isn't done! */ }
/* Get main data(scalefactors and Huffman coded frequency data) */ // Get side info
if err := readMainL3(); err != nil { if err := readAudioL3(); err != nil {
return err return err
} }
} else { // If there's not enough main data in the bit reservoir,
return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer) // signal to calling function so that decoding isn't done!
// Get main data(scalefactors and Huffman coded frequency data)
if err := readMainL3(); err != nil {
return err
} }
return nil return nil
} }

View File

@ -44,8 +44,7 @@ var g_mpeg1_bitrates = [3][15]int{
var g_sampling_frequency = [3]int{44100, 48000, 32000} var g_sampling_frequency = [3]int{44100, 48000, 32000}
//export Read_Audio_L3 func readAudioL3() error {
func Read_Audio_L3() C.int {
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if C.g_frame_header.mode == C.mpeg1_mode_single_channel {
nch = 1 nch = 1
@ -56,8 +55,7 @@ func Read_Audio_L3() C.int {
g_sampling_frequency[C.g_frame_header.sampling_frequency] + g_sampling_frequency[C.g_frame_header.sampling_frequency] +
int(C.g_frame_header.padding_bit) int(C.g_frame_header.padding_bit)
if framesize > 2000 { if framesize > 2000 {
g_error = fmt.Errorf("mp3: framesize = %d\n", framesize) return fmt.Errorf("mp3: framesize = %d\n", framesize)
return C.ERROR
} }
/* Sideinfo is 17 bytes for one channel and 32 bytes for two */ /* Sideinfo is 17 bytes for one channel and 32 bytes for two */
sideinfo_size := 32 sideinfo_size := 32
@ -71,9 +69,8 @@ func Read_Audio_L3() C.int {
main_data_size -= 2 main_data_size -= 2
} }
/* Read sideinfo from bitstream into buffer used by getSideBits() */ /* Read sideinfo from bitstream into buffer used by getSideBits() */
getSideinfo(sideinfo_size) if err := getSideinfo(sideinfo_size); err != nil {
if Get_Filepos() == eof { return err
return C.ERROR
} }
/* Parse audio data */ /* Parse audio data */
/* Pointer to where we should start reading main data */ /* Pointer to where we should start reading main data */
@ -127,7 +124,7 @@ func Read_Audio_L3() C.int {
C.g_side_info.count1table_select[gr][ch] = C.uint(getSideBits(1)) C.g_side_info.count1table_select[gr][ch] = C.uint(getSideBits(1))
} }
} }
return C.OK return nil
} }
// A sideInfo is a bit reservoir for side info // A sideInfo is a bit reservoir for side info
@ -138,16 +135,25 @@ type sideInfo struct {
var theSideInfo sideInfo var theSideInfo sideInfo
func getSideinfo(size int) { func getSideinfo(size int) error {
buf := make([]int, size) buf := make([]int, size)
n, err := getBytes(buf) n := 0
if err != nil && err != io.EOF { var err error
g_error = fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v", for n < size && err == nil {
nn, err2 := getBytes(buf[n:])
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return fmt.Errorf("mp3: unexpected EOF at getSideinfo")
}
return fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v",
size, Get_Filepos(), err) size, Get_Filepos(), err)
return
} }
theSideInfo.vec = buf[:n] theSideInfo.vec = buf[:n]
theSideInfo.idx = 0 theSideInfo.idx = 0
return nil
} }
func getSideBits(num int) int { func getSideBits(num int) int {