From 5dd91f6c83aae20e102768f0c5a083e1f6c916a1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 13 Jun 2017 02:25:27 +0900 Subject: [PATCH] audio/mp3: Move main_data_* to Go --- audio/mp3/decode_notjs.go | 13 ---- audio/mp3/maindata.go | 137 ++++++++++++++++++++++++++++++++++++++ audio/mp3/pdmp3.c | 121 +-------------------------------- audio/mp3/pdmp3.h | 6 ++ 4 files changed, 145 insertions(+), 132 deletions(-) create mode 100644 audio/mp3/maindata.go diff --git a/audio/mp3/decode_notjs.go b/audio/mp3/decode_notjs.go index 1b9f5e68e..808ea5455 100644 --- a/audio/mp3/decode_notjs.go +++ b/audio/mp3/decode_notjs.go @@ -60,19 +60,6 @@ func Get_Byte() C.unsigned { return C.unsigned(b) } -//export Get_Bytes -func Get_Bytes(num C.unsigned, data *C.unsigned) C.unsigned { - s := C.unsigned(0) - for i := 0; i < int(num); i++ { - v := Get_Byte() - if v == eof { - return eof - } - *(*C.unsigned)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(i)*unsafe.Sizeof(s))) = v - } - return C.OK -} - func getBytes(num int) ([]int, error) { r := make([]int, num) for i := 0; i < num; i++ { diff --git a/audio/mp3/maindata.go b/audio/mp3/maindata.go new file mode 100644 index 000000000..7d263cac7 --- /dev/null +++ b/audio/mp3/maindata.go @@ -0,0 +1,137 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !js + +package mp3 + +// #include "pdmp3.h" +import "C" + +import ( + "fmt" +) + +/* Bit reservoir for main data */ +type mainData struct { + // Large static data + vec [2 * 1024]int + // Pointer into the reservoir + ptr []int + // Index into the current byte(0-7) + idx int + // Number of bytes in reservoir(0-1024) + top int + + pos int +} + +var theMainData mainData + +//export Get_Main_Data +func Get_Main_Data(size C.unsigned, begin C.unsigned) C.int { + if size > 1500 { + g_error = fmt.Errorf("size = %d", size) + } + /* Check that there's data available from previous frames if needed */ + if int(begin) > theMainData.top { + // 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 + // for decoding the next frame. + b, err := getBytes(int(size)) + if err != nil { + g_error = err + return C.ERROR + } + copy(theMainData.vec[theMainData.top:], b) + /* Set up pointers */ + theMainData.ptr = theMainData.vec[0:] + theMainData.pos = 0 + theMainData.idx = 0 + theMainData.top += int(size) + return C.ERROR + } + /* Copy data from previous frames */ + for i := 0; i < int(begin); i++ { + theMainData.vec[i] = theMainData.vec[theMainData.top-int(begin)+i] + } + /* Read the main_data from file */ + b, err := getBytes(int(size)) + if err != nil { + g_error = err + return C.ERROR + } + copy(theMainData.vec[begin:], b) + /* Set up pointers */ + theMainData.ptr = theMainData.vec[0:] + theMainData.pos = 0 + theMainData.idx = 0 + theMainData.top = int(begin) + int(size) + return C.OK +} + +//export Get_Main_Bit +func Get_Main_Bit() C.unsigned { + tmp := uint(theMainData.ptr[0]) >> (7 - uint(theMainData.idx)) + tmp &= 0x01 + theMainData.ptr = theMainData.ptr[(theMainData.idx+1)>>3:] + theMainData.pos += (theMainData.idx + 1) >> 3 + theMainData.idx = (theMainData.idx + 1) & 0x07 + return C.unsigned(tmp) +} + +//export Get_Main_Bits +func Get_Main_Bits(num C.unsigned) C.unsigned { + if num == 0 { + return 0 + } + /* Form a word of the next four bytes */ + b := make([]int, 4) + for i := range b { + if len(theMainData.ptr) > i { + b[i] = theMainData.ptr[i] + } + } + tmp := (uint32(b[0]) << 24) | (uint32(b[1]) << 16) | (uint32(b[2]) << 8) | (uint32(b[3]) << 0) + + /* Remove bits already used */ + tmp = tmp << uint(theMainData.idx) + + /* Remove bits after the desired bits */ + tmp = tmp >> (32 - num) + + /* Update pointers */ + theMainData.ptr = theMainData.ptr[(theMainData.idx+int(num))>>3:] + theMainData.pos += (theMainData.idx + int(num)) >> 3 + theMainData.idx = (theMainData.idx + int(num)) & 0x07 + + /* Done */ + return C.unsigned(tmp) +} + +//export Get_Main_Pos +func Get_Main_Pos() C.unsigned { + pos := theMainData.pos + pos *= 8 /* Multiply by 8 to get number of bits */ + pos += theMainData.idx /* Add current bit index */ + return C.unsigned(pos) +} + +//export Set_Main_Pos +func Set_Main_Pos(bit_pos C.unsigned) C.int { + theMainData.ptr = theMainData.vec[bit_pos>>3:] + theMainData.pos = int(bit_pos) >> 3 + theMainData.idx = int(bit_pos) & 0x7 + return C.OK +} diff --git a/audio/mp3/pdmp3.c b/audio/mp3/pdmp3.c index 386c23777..3073f0780 100644 --- a/audio/mp3/pdmp3.c +++ b/audio/mp3/pdmp3.c @@ -109,14 +109,10 @@ static void dmp_samples(t_mpeg1_main_data *md,int gr,int ch,int type); #define dmp_samples(...) do{}while(0) #endif -static int Get_Main_Data(unsigned main_data_size,unsigned main_data_begin); static int Read_Audio_L3(void); static int Read_CRC(void); static int Read_Header(void) ; static int Read_Main_L3(void); -static int Set_Main_Pos(unsigned bit_pos); - -static unsigned Get_Main_Pos(void); static void audio_write(unsigned *samples,unsigned nsamples,int sample_rate); static void audio_write_raw(unsigned *samples,unsigned nsamples); @@ -456,12 +452,7 @@ static const float ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037 }; -static unsigned hsynth_init = 1,synth_init = 1, - /* Bit reservoir for main data */ - g_main_data_vec[2*1024],/* Large static data */ - *g_main_data_ptr,/* Pointer into the reservoir */ - g_main_data_idx,/* Index into the current byte(0-7) */ - g_main_data_top = 0;/* Number of bytes in reservoir(0-1024) */ +static unsigned hsynth_init = 1,synth_init = 1; /* Scale factor band indices * @@ -659,43 +650,6 @@ int Decode_L3(void){ return(OK); /* Done */ } -/** Description: This function assembles the main data buffer with data from -* this frame and the previous two frames into a local buffer -* used by the Get_Main_Bits function. -* Parameters: main_data_begin indicates how many bytes from previous -* frames that should be used. main_data_size indicates the number -* of data bytes in this frame. -* Return value: Status -* Author: Krister Lagerström(krister@kmlager.com) **/ -static int Get_Main_Data(unsigned main_data_size,unsigned main_data_begin){ - int i,start_pos; - - if(main_data_size > 1500) ERR("main_data_size = %d\n",main_data_size); - /* Check that there's data available from previous frames if needed */ - if(main_data_begin > g_main_data_top) { - /* 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 - * for decoding the next frame. */ - (void) Get_Bytes(main_data_size,&(g_main_data_vec[g_main_data_top])); - /* Set up pointers */ - g_main_data_ptr = &(g_main_data_vec[0]); - g_main_data_idx = 0; - g_main_data_top += main_data_size; - return(ERROR); /* This frame cannot be decoded! */ - } - for(i = 0; i < main_data_begin; i++) { /* Copy data from previous frames */ - g_main_data_vec[i] = g_main_data_vec[g_main_data_top - main_data_begin + i]; - } - start_pos = Get_Filepos(); - /* Read the main_data from file */ - (void) Get_Bytes(main_data_size,&(g_main_data_vec[main_data_begin])); - /* Set up pointers */ - g_main_data_ptr = &(g_main_data_vec[0]); - g_main_data_idx = 0; - g_main_data_top = main_data_begin + main_data_size; - return(OK); /* Done */ -} - /**Description: Reads audio and main data from bitstream into a buffer. main * data is taken from this frame and up to 2 previous frames. * Parameters: None @@ -1013,77 +967,6 @@ static int Read_Main_L3(void){ return(OK); /* Done */ } -/**Description: sets position of next bit to be read from main data bitstream. -* Parameters: Bit position. 0 = start,8 = start of byte 1,etc. -* Return value: OK or ERROR if bit_pos is past end of main data for this frame. -* Author: Krister Lagerström(krister@kmlager.com) **/ -static int Set_Main_Pos(unsigned bit_pos){ - - g_main_data_ptr = &(g_main_data_vec[bit_pos >> 3]); - g_main_data_idx = bit_pos & 0x7; - - return(OK); - -} - -/**Description: gets one bit from the local buffer which contains main_data. -* Parameters: None -* Return value: The bit is returned in the LSB of the return value. -* Author: Krister Lagerström(krister@kmlager.com) **/ -unsigned Get_Main_Bit(void){ - unsigned tmp; - - tmp = g_main_data_ptr[0] >>(7 - g_main_data_idx); - tmp &= 0x01; - g_main_data_ptr +=(g_main_data_idx + 1) >> 3; - g_main_data_idx =(g_main_data_idx + 1) & 0x07; - return(tmp); /* Done */ -} - -/**Description: reads 'number_of_bits' from local buffer containing main_data. -* Parameters: number_of_bits to read(max 24) -* Return value: The bits are returned in the LSB of the return value. -* -******************************************************************************/ -unsigned Get_Main_Bits(unsigned number_of_bits){ - unsigned tmp; - - - if(number_of_bits == 0) return(0); - - /* Form a word of the next four bytes */ - tmp =(g_main_data_ptr[0] << 24) |(g_main_data_ptr[1] << 16) | - (g_main_data_ptr[2] << 8) |(g_main_data_ptr[3] << 0); - - /* Remove bits already used */ - tmp = tmp << g_main_data_idx; - - /* Remove bits after the desired bits */ - tmp = tmp >>(32 - number_of_bits); - - /* Update pointers */ - g_main_data_ptr +=(g_main_data_idx + number_of_bits) >> 3; - g_main_data_idx =(g_main_data_idx + number_of_bits) & 0x07; - - /* Done */ - return(tmp); - -} - -/**Description: returns pos. of next bit to be read from main data bitstream. -* Parameters: None -* Return value: Bit position. -* Author: Krister Lagerström(krister@kmlager.com) **/ -static unsigned Get_Main_Pos(void){ - unsigned pos; - - pos =((size_t) g_main_data_ptr) -((size_t) &(g_main_data_vec[0])); - pos /= 4; /* Divide by four to get number of bytes */ - pos *= 8; /* Multiply by 8 to get number of bits */ - pos += g_main_data_idx; /* Add current bit index */ - return(pos); -} - /**Description: TBD * Parameters: TBD * Return value: TBD @@ -1099,7 +982,7 @@ static void Error(const char *s,int e){ * Author: Krister Lagerström(krister@kmlager.com) **/ static void Decode_L3_Init_Song(void){ hsynth_init = synth_init = 1; - g_main_data_top = 0; /* Clear bit reservoir */ + //g_main_data_top = 0; /* Clear bit reservoir */ } /**Description: Does inverse modified DCT and windowing. diff --git a/audio/mp3/pdmp3.h b/audio/mp3/pdmp3.h index a2199cfbf..35b47057d 100644 --- a/audio/mp3/pdmp3.h +++ b/audio/mp3/pdmp3.h @@ -21,8 +21,14 @@ int Decode_L3(void); int Read_Frame(void); size_t writeToWriter(void* data, int size); +int Get_Main_Data(unsigned main_data_size,unsigned main_data_begin); unsigned Get_Main_Bit(void); unsigned Get_Main_Bits(unsigned number_of_bits); +unsigned Get_Main_Pos(void); +int Set_Main_Pos(unsigned bit_pos); +unsigned Get_Main_Bit(void); +unsigned Get_Main_Bits(unsigned number_of_bits); + void Get_Sideinfo(unsigned sideinfo_size); unsigned Get_Side_Bits(unsigned number_of_bits);