audio/mp3: Make readHeader to return *mpeg1FrameHeader

This commit is contained in:
Hajime Hoshi 2017-06-18 17:44:04 +09:00
parent 1c779b6017
commit 940c3e65a2
2 changed files with 35 additions and 32 deletions

View File

@ -50,9 +50,11 @@ func (f *frame) readNextFrame() (*frame, error) {
nf.store = nf.prev.store nf.store = nf.prev.store
nf.v_vec = nf.prev.v_vec nf.v_vec = nf.prev.v_vec
} }
if err := nf.readHeader(); err != nil { h, err := readHeader()
if err != nil {
return nil, err return nil, err
} }
nf.header = h
// Get CRC word if present // Get CRC word if present
if nf.header.protection_bit == 0 { if nf.header.protection_bit == 0 {
if err := readCRC(); err != nil { if err := readCRC(); err != nil {
@ -63,7 +65,7 @@ func (f *frame) readNextFrame() (*frame, error) {
return nil, fmt.Errorf("mp3: only layer3 (want %d; got %d) is supported!", mpeg1Layer3, nf.header.layer) return nil, fmt.Errorf("mp3: only layer3 (want %d; got %d) is supported!", mpeg1Layer3, nf.header.layer)
} }
// Get side info // Get side info
s, err := readSideInfo(&nf.header) s, err := readSideInfo(nf.header)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -93,7 +95,7 @@ func isHeader(header uint32) bool {
return true return true
} }
func (f *frame) readHeader() error { func readHeader() (*mpeg1FrameHeader, error) {
// Get the next four bytes from the bitstream // Get the next four bytes from the bitstream
buf := make([]int, 4) buf := make([]int, 4)
n := 0 n := 0
@ -106,11 +108,11 @@ func (f *frame) readHeader() error {
if n < 4 { if n < 4 {
if err == io.EOF { if err == io.EOF {
if n == 0 { if n == 0 {
return eof return nil, eof
} }
return fmt.Errorf("mp3: unexpected EOF at readHeader") return nil, fmt.Errorf("mp3: unexpected EOF at readHeader")
} }
return err return nil, err
} }
b1 := uint32(buf[0]) b1 := uint32(buf[0])
b2 := uint32(buf[1]) b2 := uint32(buf[1])
@ -127,9 +129,9 @@ func (f *frame) readHeader() error {
b, err := getByte() b, err := getByte()
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
return fmt.Errorf("mp3: unexpected EOF at readHeader") return nil, fmt.Errorf("mp3: unexpected EOF at readHeader")
} }
return err return nil, err
} }
b4 = uint32(b) b4 = uint32(b)
header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0) header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
@ -137,40 +139,41 @@ func (f *frame) readHeader() error {
// If we get here we've found the sync word,and can decode the header // If we get here we've found the sync word,and can decode the header
// which is in the low 20 bits of the 32-bit sync+header word. // which is in the low 20 bits of the 32-bit sync+header word.
// Decode the header // Decode the header
f.header.id = int((header & 0x00180000) >> 19) h := &mpeg1FrameHeader{}
f.header.layer = mpeg1Layer((header & 0x00060000) >> 17) h.id = int((header & 0x00180000) >> 19)
f.header.protection_bit = int((header & 0x00010000) >> 16) h.layer = mpeg1Layer((header & 0x00060000) >> 17)
f.header.bitrate_index = int((header & 0x0000f000) >> 12) h.protection_bit = int((header & 0x00010000) >> 16)
f.header.sampling_frequency = int((header & 0x00000c00) >> 10) h.bitrate_index = int((header & 0x0000f000) >> 12)
f.header.padding_bit = int((header & 0x00000200) >> 9) h.sampling_frequency = int((header & 0x00000c00) >> 10)
f.header.private_bit = int((header & 0x00000100) >> 8) h.padding_bit = int((header & 0x00000200) >> 9)
f.header.mode = mpeg1Mode((header & 0x000000c0) >> 6) h.private_bit = int((header & 0x00000100) >> 8)
f.header.mode_extension = int((header & 0x00000030) >> 4) h.mode = mpeg1Mode((header & 0x000000c0) >> 6)
f.header.copyright = int((header & 0x00000008) >> 3) h.mode_extension = int((header & 0x00000030) >> 4)
f.header.original_or_copy = int((header & 0x00000004) >> 2) h.copyright = int((header & 0x00000008) >> 3)
f.header.emphasis = int((header & 0x00000003) >> 0) h.original_or_copy = int((header & 0x00000004) >> 2)
h.emphasis = int((header & 0x00000003) >> 0)
// Check for invalid values and impossible combinations // Check for invalid values and impossible combinations
if f.header.id != 3 { if h.id != 3 {
return fmt.Errorf("mp3: ID must be 3. Header word is 0x%08x at file pos %d", return nil, fmt.Errorf("mp3: ID must be 3. Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if f.header.bitrate_index == 0 { if h.bitrate_index == 0 {
return fmt.Errorf("mp3: Free bitrate format NIY! Header word is 0x%08x at file pos %d", return nil, fmt.Errorf("mp3: Free bitrate format NIY! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if f.header.bitrate_index == 15 { if h.bitrate_index == 15 {
return fmt.Errorf("mp3: bitrate_index = 15 is invalid! Header word is 0x%08x at file pos %d", return nil, fmt.Errorf("mp3: bitrate_index = 15 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if f.header.sampling_frequency == 3 { if h.sampling_frequency == 3 {
return fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d", return nil, fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if f.header.layer == mpeg1LayerReserved { if h.layer == mpeg1LayerReserved {
return fmt.Errorf("mp3: layer = %d is invalid! Header word is 0x%08x at file pos %d", return nil, fmt.Errorf("mp3: layer = %d is invalid! Header word is 0x%08x at file pos %d",
mpeg1LayerReserved, header, getFilepos()) mpeg1LayerReserved, header, getFilepos())
} }
return nil return h, nil
} }
func (f *frame) readHuffman(part_2_start, gr, ch int) error { func (f *frame) readHuffman(part_2_start, gr, ch int) error {

View File

@ -86,7 +86,7 @@ type mpeg1MainData struct {
type frame struct { type frame struct {
prev *frame prev *frame
header mpeg1FrameHeader header *mpeg1FrameHeader
sideInfo *mpeg1SideInfo sideInfo *mpeg1SideInfo
mainData mpeg1MainData mainData mpeg1MainData