diff --git a/audio/mp3/decode_notjs.go b/audio/mp3/decode_notjs.go index 89234360a..2253ff379 100644 --- a/audio/mp3/decode_notjs.go +++ b/audio/mp3/decode_notjs.go @@ -20,7 +20,6 @@ package mp3 import "C" import ( - "errors" "io" "unsafe" ) @@ -88,8 +87,6 @@ func writeToWriter(data unsafe.Pointer, size C.int) C.size_t { return C.size_t(n) } -var g_error error - func decode(r io.Reader, w io.Writer) error { reader = r writer = w @@ -102,10 +99,7 @@ func decode(r io.Reader, w io.Writer) error { if Get_Filepos() == eof { break } - if err != nil { - return err - } - return errors.New("mp3: not enough maindata to decode frame") + return err } - return g_error + return nil } diff --git a/audio/mp3/maindata.go b/audio/mp3/maindata.go index d84b05e67..04f804c92 100644 --- a/audio/mp3/maindata.go +++ b/audio/mp3/maindata.go @@ -175,7 +175,7 @@ var theMainDataBytes mainDataBytes func getMainData(size int, begin int) error { 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 if int(begin) > theMainDataBytes.top { @@ -202,7 +202,8 @@ func getMainData(size int, begin int) error { theMainDataBytes.pos = 0 theMainDataBytes.idx = 0 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 */ for i := 0; i < begin; i++ { diff --git a/audio/mp3/read.go b/audio/mp3/read.go index 97903eebc..56ea27268 100644 --- a/audio/mp3/read.go +++ b/audio/mp3/read.go @@ -53,23 +53,26 @@ func readFrame() error { if err := readHeader(); err != nil { return err } - /* Get CRC word if present */ + // Get CRC word if present if C.g_frame_header.protection_bit == 0 { if err := readCRC(); err != nil { return err } } - if C.g_frame_header.layer == 3 { /* Get audio data */ - Read_Audio_L3() /* Get side info */ - /* If there's not enough main data in the bit reservoir, - * 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 - } - } else { + + if C.g_frame_header.layer != 3 { return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer) } + // Get side info + if err := readAudioL3(); err != nil { + return err + } + // If there's not enough main data in the bit reservoir, + // 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 } diff --git a/audio/mp3/sideinfo.go b/audio/mp3/sideinfo.go index 7ed3fafad..03ce9fcaa 100644 --- a/audio/mp3/sideinfo.go +++ b/audio/mp3/sideinfo.go @@ -44,8 +44,7 @@ var g_mpeg1_bitrates = [3][15]int{ var g_sampling_frequency = [3]int{44100, 48000, 32000} -//export Read_Audio_L3 -func Read_Audio_L3() C.int { +func readAudioL3() error { nch := 2 if C.g_frame_header.mode == C.mpeg1_mode_single_channel { nch = 1 @@ -56,8 +55,7 @@ func Read_Audio_L3() C.int { g_sampling_frequency[C.g_frame_header.sampling_frequency] + int(C.g_frame_header.padding_bit) if framesize > 2000 { - g_error = fmt.Errorf("mp3: framesize = %d\n", framesize) - return C.ERROR + return fmt.Errorf("mp3: framesize = %d\n", framesize) } /* Sideinfo is 17 bytes for one channel and 32 bytes for two */ sideinfo_size := 32 @@ -71,9 +69,8 @@ func Read_Audio_L3() C.int { main_data_size -= 2 } /* Read sideinfo from bitstream into buffer used by getSideBits() */ - getSideinfo(sideinfo_size) - if Get_Filepos() == eof { - return C.ERROR + if err := getSideinfo(sideinfo_size); err != nil { + return err } /* Parse audio 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)) } } - return C.OK + return nil } // A sideInfo is a bit reservoir for side info @@ -138,16 +135,25 @@ type sideInfo struct { var theSideInfo sideInfo -func getSideinfo(size int) { +func getSideinfo(size int) error { buf := make([]int, size) - n, err := getBytes(buf) - if err != nil && err != io.EOF { - g_error = fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v", + n := 0 + var err error + 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) - return } theSideInfo.vec = buf[:n] theSideInfo.idx = 0 + return nil } func getSideBits(num int) int {