audio/mp3: Fix Get_Main_Dat to return error

This commit is contained in:
Hajime Hoshi 2017-06-17 02:50:38 +09:00
parent 73190062da
commit 61c76a3749
3 changed files with 38 additions and 27 deletions

View File

@ -33,8 +33,7 @@ var mpeg1_scalefac_sizes = [16][2]int{
{2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3},
} }
//export Read_Main_L3 func readMainL3() error {
func Read_Main_L3() C.int {
/* Number of channels(1 for mono and 2 for stereo) */ /* Number of channels(1 for mono and 2 for stereo) */
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 {
@ -48,8 +47,7 @@ func Read_Main_L3() C.int {
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", framesize) return fmt.Errorf("mp3: framesize = %d", 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
@ -67,8 +65,9 @@ func Read_Main_L3() C.int {
* frames that should be used. This buffer is later accessed by the * frames that should be used. This buffer is later accessed by the
* getMainBits function in the same way as the side info is. * getMainBits function in the same way as the side info is.
*/ */
if getMainData(main_data_size, int(C.g_side_info.main_data_begin)) != C.OK { if err := getMainData(main_data_size, int(C.g_side_info.main_data_begin)); err != nil {
return C.ERROR /* This could be due to not enough data in reservoir */ /* This could be due to not enough data in reservoir */
return err
} }
for gr := 0; gr < 2; gr++ { for gr := 0; gr < 2; gr++ {
for ch := 0; ch < nch; ch++ { for ch := 0; ch < nch; ch++ {
@ -151,13 +150,12 @@ func Read_Main_L3() C.int {
} }
/* Read Huffman coded data. Skip stuffing bits. */ /* Read Huffman coded data. Skip stuffing bits. */
if err := readHuffman(part_2_start, gr, ch); err != nil { if err := readHuffman(part_2_start, gr, ch); err != nil {
g_error = err return err
return C.ERROR
} }
} }
} }
/* The ancillary data is stored here,but we ignore it. */ /* The ancillary data is stored here,but we ignore it. */
return C.OK return nil
} }
type mainDataBytes struct { type mainDataBytes struct {
@ -175,20 +173,28 @@ type mainDataBytes struct {
var theMainDataBytes mainDataBytes var theMainDataBytes mainDataBytes
func getMainData(size int, begin int) int { func getMainData(size int, begin int) error {
if size > 1500 { if size > 1500 {
g_error = fmt.Errorf("size = %d", size) g_error = fmt.Errorf("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 {
// No,there is not,so we skip decoding this frame,but we have to // No,there is not, so we skip decoding this frame, but we have to
// read the main_data bits from the bitstream in case they are needed // read the main_data bits from the bitstream in case they are needed
// for decoding the next frame. // for decoding the next frame.
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 = err for n < size && err == nil {
return C.ERROR nn, err2 := getBytes(buf)
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return fmt.Errorf("mp3: unexpected EOF at getMainData")
}
return err
} }
copy(theMainDataBytes.vec[theMainDataBytes.top:], buf[:n]) copy(theMainDataBytes.vec[theMainDataBytes.top:], buf[:n])
/* Set up pointers */ /* Set up pointers */
@ -196,7 +202,7 @@ func getMainData(size int, begin int) int {
theMainDataBytes.pos = 0 theMainDataBytes.pos = 0
theMainDataBytes.idx = 0 theMainDataBytes.idx = 0
theMainDataBytes.top += size theMainDataBytes.top += size
return C.ERROR return fmt.Errorf("mp3: not enought frame data; read next frame?")
} }
/* Copy data from previous frames */ /* Copy data from previous frames */
for i := 0; i < begin; i++ { for i := 0; i < begin; i++ {
@ -204,10 +210,18 @@ func getMainData(size int, begin int) int {
} }
/* Read the main_data from file */ /* Read the main_data from file */
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 = err for n < size && err == nil {
return C.ERROR nn, err2 := getBytes(buf)
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return fmt.Errorf("mp3: unexpected EOF at getMainData")
}
return err
} }
copy(theMainDataBytes.vec[begin:], buf[:n]) copy(theMainDataBytes.vec[begin:], buf[:n])
/* Set up pointers */ /* Set up pointers */
@ -215,7 +229,7 @@ func getMainData(size int, begin int) int {
theMainDataBytes.pos = 0 theMainDataBytes.pos = 0
theMainDataBytes.idx = 0 theMainDataBytes.idx = 0
theMainDataBytes.top = begin + size theMainDataBytes.top = begin + size
return C.OK return nil
} }
func getMainBit() int { func getMainBit() int {

View File

@ -91,9 +91,7 @@ size_t writeToWriter(void* data, int size);
unsigned Get_Main_Pos(void); unsigned Get_Main_Pos(void);
int Set_Main_Pos(unsigned bit_pos); int Set_Main_Pos(unsigned bit_pos);
int Read_Main_L3(void);
int Read_Audio_L3(void); int Read_Audio_L3(void);
int Read_Header(void);
void L3_Requantize(unsigned gr,unsigned ch); void L3_Requantize(unsigned gr,unsigned ch);
void L3_Reorder(unsigned gr,unsigned ch); void L3_Reorder(unsigned gr,unsigned ch);

View File

@ -64,9 +64,8 @@ func readFrame() error {
/* If there's not enough main data in the bit reservoir, /* If there's not enough main data in the bit reservoir,
* signal to calling function so that decoding isn't done! */ * signal to calling function so that decoding isn't done! */
/* Get main data(scalefactors and Huffman coded frequency data) */ /* Get main data(scalefactors and Huffman coded frequency data) */
if Read_Main_L3() != C.OK { if err := readMainL3(); err != nil {
// TODO: Fix this return err
return fmt.Errorf("mp3: Read_Main_L3")
} }
} else { } else {
return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer) return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer)