ebiten/audio/mp3/maindata.go

278 lines
8.3 KiB
Go
Raw Normal View History

2017-06-12 19:25:27 +02:00
// 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
import (
"fmt"
2017-06-16 17:34:54 +02:00
"io"
2017-06-12 19:25:27 +02:00
)
2017-06-14 03:56:42 +02:00
var mpeg1_scalefac_sizes = [16][2]int{
{0, 0}, {0, 1}, {0, 2}, {0, 3}, {3, 0}, {1, 1}, {1, 2}, {1, 3},
{2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3},
}
2017-06-17 11:42:43 +02:00
func (f *frame) readMainL3() error {
2017-06-14 03:56:42 +02:00
/* Number of channels(1 for mono and 2 for stereo) */
nch := 2
2017-06-17 11:42:43 +02:00
if f.header.mode == mpeg1ModeSingleChannel {
2017-06-14 03:56:42 +02:00
nch = 1
}
/* Calculate header audio data size */
framesize := (144*
2017-06-17 11:42:43 +02:00
g_mpeg1_bitrates[f.header.layer][f.header.bitrate_index])/
g_sampling_frequency[f.header.sampling_frequency] +
int(f.header.padding_bit)
2017-06-14 03:56:42 +02:00
if framesize > 2000 {
return fmt.Errorf("mp3: framesize = %d", framesize)
2017-06-14 03:56:42 +02:00
}
/* Sideinfo is 17 bytes for one channel and 32 bytes for two */
sideinfo_size := 32
if nch == 1 {
sideinfo_size = 17
}
/* Main data size is the rest of the frame,including ancillary data */
main_data_size := framesize - sideinfo_size - 4 /* sync+header */
/* CRC is 2 bytes */
2017-06-17 11:42:43 +02:00
if f.header.protection_bit == 0 {
2017-06-14 03:56:42 +02:00
main_data_size -= 2
}
/* Assemble main data buffer with data from this frame and the previous
* two frames. main_data_begin indicates how many bytes from previous
* frames that should be used. This buffer is later accessed by the
* getMainBits function in the same way as the side info is.
*/
2017-06-17 11:42:43 +02:00
if err := getMainData(main_data_size, int(f.sideInfo.main_data_begin)); err != nil {
/* This could be due to not enough data in reservoir */
return err
2017-06-14 03:56:42 +02:00
}
for gr := 0; gr < 2; gr++ {
for ch := 0; ch < nch; ch++ {
2017-06-16 23:48:27 +02:00
part_2_start := getMainPos()
2017-06-14 03:56:42 +02:00
/* Number of bits in the bitstream for the bands */
2017-06-17 11:42:43 +02:00
slen1 := mpeg1_scalefac_sizes[f.sideInfo.scalefac_compress[gr][ch]][0]
slen2 := mpeg1_scalefac_sizes[f.sideInfo.scalefac_compress[gr][ch]][1]
if (f.sideInfo.win_switch_flag[gr][ch] != 0) && (f.sideInfo.block_type[gr][ch] == 2) {
if f.sideInfo.mixed_block_flag[gr][ch] != 0 {
2017-06-14 03:56:42 +02:00
for sfb := 0; sfb < 8; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[gr][ch][sfb] = getMainBits(slen1)
2017-06-14 03:56:42 +02:00
}
for sfb := 3; sfb < 12; sfb++ {
/*slen1 for band 3-5,slen2 for 6-11*/
nbits := slen2
if sfb < 6 {
nbits = slen1
}
for win := 0; win < 3; win++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_s[gr][ch][sfb][win] = getMainBits(nbits)
2017-06-14 03:56:42 +02:00
}
}
} else {
for sfb := 0; sfb < 12; sfb++ {
/*slen1 for band 3-5,slen2 for 6-11*/
nbits := slen2
if sfb < 6 {
nbits = slen1
}
for win := 0; win < 3; win++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_s[gr][ch][sfb][win] = getMainBits(nbits)
2017-06-14 03:56:42 +02:00
}
}
}
} else { /* block_type == 0 if winswitch == 0 */
/* Scale factor bands 0-5 */
2017-06-17 11:42:43 +02:00
if (f.sideInfo.scfsi[ch][0] == 0) || (gr == 0) {
2017-06-14 03:56:42 +02:00
for sfb := 0; sfb < 6; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[gr][ch][sfb] = getMainBits(slen1)
2017-06-14 03:56:42 +02:00
}
2017-06-17 11:42:43 +02:00
} else if (f.sideInfo.scfsi[ch][0] == 1) && (gr == 1) {
2017-06-14 03:56:42 +02:00
/* Copy scalefactors from granule 0 to granule 1 */
for sfb := 0; sfb < 6; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[1][ch][sfb] = f.mainData.scalefac_l[0][ch][sfb]
2017-06-14 03:56:42 +02:00
}
}
/* Scale factor bands 6-10 */
2017-06-17 11:42:43 +02:00
if (f.sideInfo.scfsi[ch][1] == 0) || (gr == 0) {
2017-06-14 03:56:42 +02:00
for sfb := 6; sfb < 11; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[gr][ch][sfb] = getMainBits(slen1)
2017-06-14 03:56:42 +02:00
}
2017-06-17 11:42:43 +02:00
} else if (f.sideInfo.scfsi[ch][1] == 1) && (gr == 1) {
2017-06-14 03:56:42 +02:00
/* Copy scalefactors from granule 0 to granule 1 */
for sfb := 6; sfb < 11; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[1][ch][sfb] = f.mainData.scalefac_l[0][ch][sfb]
2017-06-14 03:56:42 +02:00
}
}
/* Scale factor bands 11-15 */
2017-06-17 11:42:43 +02:00
if (f.sideInfo.scfsi[ch][2] == 0) || (gr == 0) {
2017-06-14 03:56:42 +02:00
for sfb := 11; sfb < 16; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[gr][ch][sfb] = getMainBits(slen2)
2017-06-14 03:56:42 +02:00
}
2017-06-17 11:42:43 +02:00
} else if (f.sideInfo.scfsi[ch][2] == 1) && (gr == 1) {
2017-06-14 03:56:42 +02:00
/* Copy scalefactors from granule 0 to granule 1 */
for sfb := 11; sfb < 16; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[1][ch][sfb] = f.mainData.scalefac_l[0][ch][sfb]
2017-06-14 03:56:42 +02:00
}
}
/* Scale factor bands 16-20 */
2017-06-17 11:42:43 +02:00
if (f.sideInfo.scfsi[ch][3] == 0) || (gr == 0) {
2017-06-14 03:56:42 +02:00
for sfb := 16; sfb < 21; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[gr][ch][sfb] = getMainBits(slen2)
2017-06-14 03:56:42 +02:00
}
2017-06-17 11:42:43 +02:00
} else if (f.sideInfo.scfsi[ch][3] == 1) && (gr == 1) {
2017-06-14 03:56:42 +02:00
/* Copy scalefactors from granule 0 to granule 1 */
for sfb := 16; sfb < 21; sfb++ {
2017-06-17 11:42:43 +02:00
f.mainData.scalefac_l[1][ch][sfb] = f.mainData.scalefac_l[0][ch][sfb]
2017-06-14 03:56:42 +02:00
}
}
}
/* Read Huffman coded data. Skip stuffing bits. */
2017-06-17 11:42:43 +02:00
if err := f.readHuffman(part_2_start, gr, ch); err != nil {
return err
2017-06-16 16:41:13 +02:00
}
2017-06-14 03:56:42 +02:00
}
}
/* The ancillary data is stored here,but we ignore it. */
return nil
2017-06-14 03:56:42 +02:00
}
2017-06-13 19:22:11 +02:00
type mainDataBytes struct {
2017-06-12 19:25:27 +02:00
// 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
}
2017-06-13 19:22:11 +02:00
var theMainDataBytes mainDataBytes
2017-06-12 19:25:27 +02:00
func getMainData(size int, begin int) error {
2017-06-12 19:25:27 +02:00
if size > 1500 {
2017-06-16 20:01:39 +02:00
return fmt.Errorf("mp3: size = %d", size)
2017-06-12 19:25:27 +02:00
}
// Check that there's data available from previous frames if needed
2017-06-13 19:22:11 +02:00
if int(begin) > theMainDataBytes.top {
// No,there is not, so we skip decoding this frame, but we have to
2017-06-12 19:25:27 +02:00
// read the main_data bits from the bitstream in case they are needed
// for decoding the next frame.
2017-06-16 17:34:54 +02:00
buf := make([]int, size)
n := 0
var err error
for n < size && err == nil {
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
2017-06-12 19:25:27 +02:00
}
2017-06-16 17:34:54 +02:00
copy(theMainDataBytes.vec[theMainDataBytes.top:], buf[:n])
2017-06-12 19:25:27 +02:00
/* Set up pointers */
2017-06-13 19:22:11 +02:00
theMainDataBytes.ptr = theMainDataBytes.vec[0:]
theMainDataBytes.pos = 0
theMainDataBytes.idx = 0
2017-06-14 03:56:42 +02:00
theMainDataBytes.top += size
2017-06-16 20:01:39 +02:00
// TODO: Define a special error and enable to continue the next frame.
return fmt.Errorf("mp3: frame can't be decoded")
2017-06-12 19:25:27 +02:00
}
/* Copy data from previous frames */
2017-06-14 03:56:42 +02:00
for i := 0; i < begin; i++ {
theMainDataBytes.vec[i] = theMainDataBytes.vec[theMainDataBytes.top-begin+i]
2017-06-12 19:25:27 +02:00
}
/* Read the main_data from file */
2017-06-16 17:34:54 +02:00
buf := make([]int, size)
n := 0
var err error
for n < size && err == nil {
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
2017-06-12 19:25:27 +02:00
}
2017-06-16 17:34:54 +02:00
copy(theMainDataBytes.vec[begin:], buf[:n])
2017-06-12 19:25:27 +02:00
/* Set up pointers */
2017-06-13 19:22:11 +02:00
theMainDataBytes.ptr = theMainDataBytes.vec[0:]
theMainDataBytes.pos = 0
theMainDataBytes.idx = 0
2017-06-14 03:56:42 +02:00
theMainDataBytes.top = begin + size
return nil
2017-06-12 19:25:27 +02:00
}
2017-06-14 03:56:42 +02:00
func getMainBit() int {
2017-06-13 19:22:11 +02:00
tmp := uint(theMainDataBytes.ptr[0]) >> (7 - uint(theMainDataBytes.idx))
2017-06-12 19:25:27 +02:00
tmp &= 0x01
2017-06-13 19:22:11 +02:00
theMainDataBytes.ptr = theMainDataBytes.ptr[(theMainDataBytes.idx+1)>>3:]
theMainDataBytes.pos += (theMainDataBytes.idx + 1) >> 3
theMainDataBytes.idx = (theMainDataBytes.idx + 1) & 0x07
2017-06-14 03:56:42 +02:00
return int(tmp)
2017-06-12 19:25:27 +02:00
}
2017-06-14 03:56:42 +02:00
func getMainBits(num int) int {
2017-06-12 19:25:27 +02:00
if num == 0 {
return 0
}
/* Form a word of the next four bytes */
b := make([]int, 4)
for i := range b {
2017-06-13 19:22:11 +02:00
if len(theMainDataBytes.ptr) > i {
b[i] = theMainDataBytes.ptr[i]
2017-06-12 19:25:27 +02:00
}
}
tmp := (uint32(b[0]) << 24) | (uint32(b[1]) << 16) | (uint32(b[2]) << 8) | (uint32(b[3]) << 0)
/* Remove bits already used */
2017-06-13 19:22:11 +02:00
tmp = tmp << uint(theMainDataBytes.idx)
2017-06-12 19:25:27 +02:00
/* Remove bits after the desired bits */
2017-06-14 03:56:42 +02:00
tmp = tmp >> (32 - uint(num))
2017-06-12 19:25:27 +02:00
/* Update pointers */
2017-06-13 19:22:11 +02:00
theMainDataBytes.ptr = theMainDataBytes.ptr[(theMainDataBytes.idx+int(num))>>3:]
2017-06-14 03:56:42 +02:00
theMainDataBytes.pos += (theMainDataBytes.idx + num) >> 3
theMainDataBytes.idx = (theMainDataBytes.idx + num) & 0x07
2017-06-12 19:25:27 +02:00
/* Done */
2017-06-14 03:56:42 +02:00
return int(tmp)
2017-06-12 19:25:27 +02:00
}
2017-06-16 23:48:27 +02:00
func getMainPos() int {
2017-06-13 19:22:11 +02:00
pos := theMainDataBytes.pos
pos *= 8 /* Multiply by 8 to get number of bits */
pos += theMainDataBytes.idx /* Add current bit index */
2017-06-16 23:48:27 +02:00
return pos
2017-06-12 19:25:27 +02:00
}
2017-06-16 23:48:27 +02:00
func setMainPos(bit_pos int) {
2017-06-13 19:22:11 +02:00
theMainDataBytes.ptr = theMainDataBytes.vec[bit_pos>>3:]
theMainDataBytes.pos = int(bit_pos) >> 3
theMainDataBytes.idx = int(bit_pos) & 0x7
2017-06-12 19:25:27 +02:00
}