mirror of
https://github.com/wader/fq.git
synced 2024-12-27 15:42:07 +03:00
decode: Move io helper into *D
This commit is contained in:
parent
6fba1a8125
commit
7af191d974
@ -70,7 +70,7 @@ func flacDecode(d *decode.D, in interface{}) interface{} {
|
||||
frameStreamSamplesBuf := ffo.SamplesBuf[0 : samplesInFrame*uint64(ffo.Channels*ffo.BitsPerSample/8)]
|
||||
framesNDecodedSamples += ffo.Samples
|
||||
|
||||
decode.MustCopy(d, md5Samples, bytes.NewReader(frameStreamSamplesBuf))
|
||||
d.MustCopy(md5Samples, bytes.NewReader(frameStreamSamplesBuf))
|
||||
streamDecodedSamples += ffo.Samples
|
||||
|
||||
// reuse buffer if possible
|
||||
|
@ -337,7 +337,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
|
||||
})
|
||||
|
||||
headerCRC := &crc.CRC{Bits: 8, Table: crc.ATM8Table}
|
||||
decode.MustCopy(d, headerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart))
|
||||
d.MustCopy(headerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart))
|
||||
d.FieldRawLen("crc", 8, d.ValidateBitBuf(headerCRC.Sum(nil)), d.RawHex)
|
||||
})
|
||||
|
||||
@ -587,7 +587,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
|
||||
d.FieldU("byte_align", d.ByteAlignBits(), d.AssertU(0))
|
||||
// <16> CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0) of everything before the crc, back to and including the frame header sync code
|
||||
footerCRC := &crc.CRC{Bits: 16, Table: crc.ANSI16Table}
|
||||
decode.MustCopy(d, footerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart))
|
||||
d.MustCopy(footerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart))
|
||||
d.FieldRawLen("footer_crc", 16, d.ValidateBitBuf(footerCRC.Sum(nil)), d.RawHex)
|
||||
|
||||
streamSamples := len(channelSamples[0])
|
||||
|
@ -92,7 +92,7 @@ func gifDecode(d *decode.D, in interface{}) interface{} {
|
||||
d.FieldU8("terminator")
|
||||
seenTerminator = true
|
||||
}
|
||||
decode.MustCopy(d, dataBytes, b.Copy())
|
||||
d.MustCopy(dataBytes, b.Copy())
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -105,7 +105,7 @@ func gzDecode(d *decode.D, in interface{}) interface{} {
|
||||
case delfateMethod:
|
||||
deflateR := flate.NewReader(compressedBB)
|
||||
uncompressed := &bytes.Buffer{}
|
||||
if _, err := decode.Copy(d, io.MultiWriter(uncompressed, crc32W), deflateR); err != nil {
|
||||
if _, err := d.Copy(io.MultiWriter(uncompressed, crc32W), deflateR); err != nil {
|
||||
d.Fatalf(err.Error())
|
||||
}
|
||||
uncompressedBB := bitio.NewBufferFromBytes(uncompressed.Bytes(), -1)
|
||||
|
@ -532,7 +532,7 @@ func decodeFrame(d *decode.D, version int) uint64 {
|
||||
if unsyncFlag {
|
||||
// TODO: DecodeFn
|
||||
// TODO: unknown after frame decode
|
||||
unsyncedBb := decode.MustNewBitBufFromReader(d, unsyncReader{Reader: d.BitBufRange(d.Pos(), int64(dataSize)*8)})
|
||||
unsyncedBb := d.MustNewBitBufFromReader(unsyncReader{Reader: d.BitBufRange(d.Pos(), int64(dataSize)*8)})
|
||||
d.FieldFormatBitBuf("unsync", unsyncedBb, decode.FormatFn(func(d *decode.D, in interface{}) interface{} {
|
||||
if fn, ok := frames[idNormalized]; ok {
|
||||
fn(d)
|
||||
|
@ -100,7 +100,7 @@ func avcNALUDecode(d *decode.D, in interface{}) interface{} {
|
||||
d.FieldBool("forbidden_zero_bit")
|
||||
d.FieldU2("nal_ref_idc")
|
||||
nalType := d.FieldU5("nal_unit_type", d.MapUToScalar(avcNALNames))
|
||||
unescapedBb := decode.MustNewBitBufFromReader(d, decode.NALUnescapeReader{Reader: d.BitBufRange(d.Pos(), d.BitsLeft())})
|
||||
unescapedBb := d.MustNewBitBufFromReader(decode.NALUnescapeReader{Reader: d.BitBufRange(d.Pos(), d.BitsLeft())})
|
||||
|
||||
switch nalType {
|
||||
case avcNALCodedSliceNonIDR,
|
||||
|
@ -71,7 +71,7 @@ func hevcNALUDecode(d *decode.D, in interface{}) interface{} {
|
||||
nalType := d.FieldU6("nal_unit_type", d.MapUToStrSym(hevcNALNames))
|
||||
d.FieldU6("nuh_layer_id")
|
||||
d.FieldU3("nuh_temporal_id_plus1")
|
||||
unescapedBb := decode.MustNewBitBufFromReader(d, decode.NALUnescapeReader{Reader: d.BitBufRange(d.Pos(), d.BitsLeft())})
|
||||
unescapedBb := d.MustNewBitBufFromReader(decode.NALUnescapeReader{Reader: d.BitBufRange(d.Pos(), d.BitsLeft())})
|
||||
|
||||
_ = unescapedBb
|
||||
_ = nalType
|
||||
|
@ -385,8 +385,8 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
|
||||
|
||||
crcHash := &crc.CRC{Bits: 16, Current: 0xffff, Table: crc.ANSI16Table}
|
||||
// 2 bytes after sync and some other fields + all of side info
|
||||
decode.MustCopy(d, crcHash, d.BitBufRange(2*8, 2*8))
|
||||
decode.MustCopy(d, crcHash, d.BitBufRange(6*8, sideInfoBytes*8))
|
||||
d.MustCopy(crcHash, d.BitBufRange(2*8, 2*8))
|
||||
d.MustCopy(crcHash, d.BitBufRange(6*8, sideInfoBytes*8))
|
||||
|
||||
if crcValue != nil {
|
||||
_ = crcValue.ScalarFn(d.ValidateBitBuf(crcHash.Sum(nil)))
|
||||
|
@ -48,9 +48,9 @@ func pageDecode(d *decode.D, in interface{}) interface{} {
|
||||
|
||||
pageChecksumValue := d.FieldGet("page_checksum")
|
||||
pageCRC := &crc.CRC{Bits: 32, Table: crc.Poly04c11db7Table}
|
||||
decode.MustCopy(d, pageCRC, d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos)) // header before checksum
|
||||
decode.MustCopy(d, pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits
|
||||
decode.MustCopy(d, pageCRC, d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop())) // rest of page
|
||||
d.MustCopy(pageCRC, d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos)) // header before checksum
|
||||
d.MustCopy(pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits
|
||||
d.MustCopy(pageCRC, d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop())) // rest of page
|
||||
_ = pageChecksumValue.ScalarFn(d.ValidateBitBuf(bitio.ReverseBytes(pageCRC.Sum(nil))))
|
||||
|
||||
return p
|
||||
|
@ -113,7 +113,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
|
||||
|
||||
switch compressionMethod {
|
||||
case compressionDeflate:
|
||||
d.FieldFormatReaderLen("uncompressed", int64(dataLen), zlib.NewReader, decode.FormatFn(func(d *decode.D, in interface{}) interface{} {
|
||||
d.FieldFormatReaderLen("uncompressed", dataLen, zlib.NewReader, decode.FormatFn(func(d *decode.D, in interface{}) interface{} {
|
||||
d.FieldUTF8("text", int(d.BitsLeft()/8))
|
||||
return nil
|
||||
}))
|
||||
@ -180,7 +180,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
|
||||
})
|
||||
|
||||
chunkCRC := crc32.NewIEEE()
|
||||
decode.MustCopy(d, chunkCRC, d.BitBufRange(crcStartPos, d.Pos()-crcStartPos))
|
||||
d.MustCopy(chunkCRC, d.BitBufRange(crcStartPos, d.Pos()-crcStartPos))
|
||||
d.FieldRawLen("crc", 32, d.ValidateBitBuf(chunkCRC.Sum(nil)), d.RawHex)
|
||||
})
|
||||
|
||||
|
@ -184,6 +184,20 @@ func (d *D) Copy(r io.Writer, w io.Reader) (int64, error) {
|
||||
return io.CopyBuffer(r, w, buf)
|
||||
}
|
||||
|
||||
func (d *D) MustCopy(r io.Writer, w io.Reader) int64 {
|
||||
n, err := d.Copy(r, w)
|
||||
if err != nil {
|
||||
panic(IOError{Err: err, Op: "MustCopyBuffer"})
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (d *D) MustNewBitBufFromReader(r io.Reader) *bitio.Buffer {
|
||||
b := &bytes.Buffer{}
|
||||
d.MustCopy(b, r)
|
||||
return bitio.NewBufferFromBytes(b.Bytes(), -1)
|
||||
}
|
||||
|
||||
func (d *D) SharedReadBuf(n int) []byte {
|
||||
if d.readBuf == nil {
|
||||
d.readBuf = new([]byte)
|
||||
|
@ -1,32 +1,9 @@
|
||||
package decode
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/wader/fq/pkg/bitio"
|
||||
)
|
||||
|
||||
func Copy(d *D, r io.Writer, w io.Reader) (int64, error) {
|
||||
// TODO: what size? now same as io.Copy
|
||||
buf := d.SharedReadBuf(32 * 1024)
|
||||
return io.CopyBuffer(r, w, buf)
|
||||
}
|
||||
|
||||
func MustCopy(d *D, r io.Writer, w io.Reader) int64 {
|
||||
n, err := Copy(d, r, w)
|
||||
if err != nil {
|
||||
panic(IOError{Err: err, Op: "MustCopyBuffer"})
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func MustNewBitBufFromReader(d *D, r io.Reader) *bitio.Buffer {
|
||||
b := &bytes.Buffer{}
|
||||
MustCopy(d, b, r)
|
||||
return bitio.NewBufferFromBytes(b.Bytes(), -1)
|
||||
}
|
||||
|
||||
// TODO: move?
|
||||
// TODO: make generic replace reader? share with id3v2 unsync?
|
||||
type NALUnescapeReader struct {
|
||||
|
Loading…
Reference in New Issue
Block a user