Adjusted flac packetizer cache
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b47447874a
commit
b42a1fee89
|
@ -14,6 +14,7 @@ func NewFLACPacketizer(reader io.Reader) *FLACPacketizer {
|
|||
}
|
||||
|
||||
type FLACHeaderPacket struct {
|
||||
Capture [4]byte
|
||||
MinimumBlockSize uint16
|
||||
MaximumBlockSize uint16
|
||||
MinimumFrameSize uint32
|
||||
|
@ -40,9 +41,9 @@ func (p *FLACHeaderPacket) GetEndSampleNumber() int64 {
|
|||
}
|
||||
|
||||
func (p *FLACHeaderPacket) GetData() []byte {
|
||||
data := bytes.NewBuffer([]byte{})
|
||||
data := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||
buf := bitio.NewWriter(data)
|
||||
if _, err := buf.Write([]byte("fLaC")); err != nil {
|
||||
if _, err := buf.Write(p.Capture[:]); err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, b := range p.MetadataBlocks {
|
||||
|
@ -57,7 +58,7 @@ func (p *FLACHeaderPacket) GetData() []byte {
|
|||
}
|
||||
}
|
||||
|
||||
buf.Align()
|
||||
_ = buf.Close()
|
||||
|
||||
return data.Bytes()
|
||||
}
|
||||
|
@ -86,7 +87,11 @@ func readFlacMetadataBlock(br *bitio.Reader) (block byte, data []byte) {
|
|||
}
|
||||
|
||||
func readFlacHeader(br *bitio.Reader) *FLACHeaderPacket {
|
||||
if n, err := br.ReadBits(32); err != nil || n != 0x664C6143 {
|
||||
hdr := &FLACHeaderPacket{
|
||||
buf: make([]byte, 0, 1024*1024),
|
||||
}
|
||||
|
||||
if _, err := br.Read(hdr.Capture[:]); err != nil || bytes.Compare(hdr.Capture[:], []byte("fLaC")) != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -94,9 +99,6 @@ func readFlacHeader(br *bitio.Reader) *FLACHeaderPacket {
|
|||
if (block&0x7F) != 0 || len(data) < ((16+16+24+24+20+3+5+36+128)/8) { //StreamInfo
|
||||
return nil
|
||||
}
|
||||
hdr := &FLACHeaderPacket{
|
||||
buf: make([]byte, 0, 1024*1024),
|
||||
}
|
||||
|
||||
hdr.MetadataBlocks = append(hdr.MetadataBlocks, append([]byte{block}, data...))
|
||||
|
||||
|
@ -456,7 +458,10 @@ func readFlacFrame(hdr *FLACHeaderPacket, br *bitio.Reader) *FLACPacket {
|
|||
}
|
||||
}
|
||||
|
||||
p.FrameData = buf.Bytes()
|
||||
_ = bw.Close()
|
||||
|
||||
p.FrameData = make([]byte, buf.Len())
|
||||
copy(p.FrameData, buf.Bytes())
|
||||
|
||||
if p.CRC16, err = br.ReadBits(16); err != nil {
|
||||
return nil
|
||||
|
@ -645,7 +650,7 @@ func (p *FLACPacket) KeepMode() KeepMode {
|
|||
|
||||
func (p *FLACPacket) GetStartSampleNumber() int64 {
|
||||
if p.BlockingStrategy == 0 { //fixed-blocksize
|
||||
return int64(p.Number * uint64(p.getBlockSize()))
|
||||
return int64(p.Number * uint64(p.hdr.MinimumBlockSize))
|
||||
}
|
||||
return int64(p.Number)
|
||||
}
|
||||
|
@ -769,10 +774,6 @@ func (p *FLACPacket) getBitsPerSample() uint8 {
|
|||
}
|
||||
|
||||
func (p *FLACPacket) GetDataOffset(offset int64) []byte {
|
||||
if offset == 0 && len(p.buf) > 0 {
|
||||
return p.buf
|
||||
}
|
||||
|
||||
//TODO: reserve proper amount
|
||||
buf := bytes.NewBuffer(make([]byte, 0, len(p.FrameData)+64))
|
||||
bw := bitio.NewWriter(buf)
|
||||
|
@ -805,7 +806,7 @@ func (p *FLACPacket) GetDataOffset(offset int64) []byte {
|
|||
return nil
|
||||
}
|
||||
} else {
|
||||
number += offset / int64(p.getBlockSize())
|
||||
number += offset / int64(p.hdr.MinimumBlockSize)
|
||||
|
||||
if number < 0 {
|
||||
return nil
|
||||
|
@ -928,10 +929,8 @@ func (p *FLACPacket) GetDataOffset(offset int64) []byte {
|
|||
|
||||
_ = bw.WriteBits(uint64(flacCrc16(buf.Bytes())), 16)
|
||||
|
||||
if offset == 0 {
|
||||
p.buf = buf.Bytes()
|
||||
return p.buf
|
||||
}
|
||||
_ = bw.Close()
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue