Skip to content

Commit

Permalink
bits: rename WriteBits into WriteBitsUnsafe (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 20, 2024
1 parent 9cff2d7 commit a1df134
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
7 changes: 7 additions & 0 deletions pkg/bits/write.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package bits

// WriteBits writes N bits.
//
// Deprecated: replaced by WriteBitsUnsafe.
func WriteBits(buf []byte, pos *int, bits uint64, n int) {
WriteBitsUnsafe(buf, pos, bits, n)
}

// WriteBitsUnsafe writes N bits.
func WriteBitsUnsafe(buf []byte, pos *int, bits uint64, n int) {
res := 8 - (*pos & 0x07)
if n < res {
buf[*pos>>0x03] |= byte(bits << (res - n))
Expand Down
12 changes: 6 additions & 6 deletions pkg/bits/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"github.com/stretchr/testify/require"
)

func TestWriteBits(t *testing.T) {
func TestWriteBitsUnsafe(t *testing.T) {
buf := make([]byte, 6)
pos := 0
WriteBits(buf, &pos, uint64(0x2a), 6)
WriteBits(buf, &pos, uint64(0x0c), 6)
WriteBits(buf, &pos, uint64(0x1f), 6)
WriteBits(buf, &pos, uint64(0x5a), 8)
WriteBits(buf, &pos, uint64(0xaaec4), 20)
WriteBitsUnsafe(buf, &pos, uint64(0x2a), 6)
WriteBitsUnsafe(buf, &pos, uint64(0x0c), 6)
WriteBitsUnsafe(buf, &pos, uint64(0x1f), 6)
WriteBitsUnsafe(buf, &pos, uint64(0x5a), 8)
WriteBitsUnsafe(buf, &pos, uint64(0xaaec4), 20)
require.Equal(t, []byte{0xA8, 0xC7, 0xD6, 0xAA, 0xBB, 0x10}, buf)
}
30 changes: 15 additions & 15 deletions pkg/codecs/mpeg4audio/audio_specific_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,17 @@ func (c AudioSpecificConfig) Marshal() ([]byte, error) {

func (c AudioSpecificConfig) marshalTo(buf []byte, pos *int) error {
if c.ExtensionType == ObjectTypeSBR || c.ExtensionType == ObjectTypePS {
bits.WriteBits(buf, pos, uint64(c.ExtensionType), 5)
bits.WriteBitsUnsafe(buf, pos, uint64(c.ExtensionType), 5)
} else {
bits.WriteBits(buf, pos, uint64(c.Type), 5)
bits.WriteBitsUnsafe(buf, pos, uint64(c.Type), 5)
}

sampleRateIndex, ok := reverseSampleRates[c.SampleRate]
if !ok {
bits.WriteBits(buf, pos, uint64(15), 4)
bits.WriteBits(buf, pos, uint64(c.SampleRate), 24)
bits.WriteBitsUnsafe(buf, pos, uint64(15), 4)
bits.WriteBitsUnsafe(buf, pos, uint64(c.SampleRate), 24)
} else {
bits.WriteBits(buf, pos, uint64(sampleRateIndex), 4)
bits.WriteBitsUnsafe(buf, pos, uint64(sampleRateIndex), 4)
}

var channelConfig int
Expand All @@ -226,33 +226,33 @@ func (c AudioSpecificConfig) marshalTo(buf []byte, pos *int) error {
default:
return fmt.Errorf("invalid channel count (%d)", c.ChannelCount)
}
bits.WriteBits(buf, pos, uint64(channelConfig), 4)
bits.WriteBitsUnsafe(buf, pos, uint64(channelConfig), 4)

if c.ExtensionType == ObjectTypeSBR || c.ExtensionType == ObjectTypePS {
sampleRateIndex, ok := reverseSampleRates[c.ExtensionSampleRate]
if !ok {
bits.WriteBits(buf, pos, uint64(0x0F), 4)
bits.WriteBits(buf, pos, uint64(c.ExtensionSampleRate), 24)
bits.WriteBitsUnsafe(buf, pos, uint64(0x0F), 4)
bits.WriteBitsUnsafe(buf, pos, uint64(c.ExtensionSampleRate), 24)
} else {
bits.WriteBits(buf, pos, uint64(sampleRateIndex), 4)
bits.WriteBitsUnsafe(buf, pos, uint64(sampleRateIndex), 4)
}
bits.WriteBits(buf, pos, uint64(c.Type), 5)
bits.WriteBitsUnsafe(buf, pos, uint64(c.Type), 5)
}

if c.FrameLengthFlag {
bits.WriteBits(buf, pos, 1, 1)
bits.WriteBitsUnsafe(buf, pos, 1, 1)
} else {
bits.WriteBits(buf, pos, 0, 1)
bits.WriteBitsUnsafe(buf, pos, 0, 1)
}

if c.DependsOnCoreCoder {
bits.WriteBits(buf, pos, 1, 1)
bits.WriteBitsUnsafe(buf, pos, 1, 1)
} else {
bits.WriteBits(buf, pos, 0, 1)
bits.WriteBitsUnsafe(buf, pos, 0, 1)
}

if c.DependsOnCoreCoder {
bits.WriteBits(buf, pos, uint64(c.CoreCoderDelay), 14)
bits.WriteBitsUnsafe(buf, pos, uint64(c.CoreCoderDelay), 14)
}

*pos++ // extensionFlag
Expand Down
44 changes: 22 additions & 22 deletions pkg/codecs/mpeg4audio/stream_mux_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,20 @@ func (c StreamMuxConfig) Marshal() ([]byte, error) {
buf := make([]byte, c.marshalSize())
pos := 0

bits.WriteBits(buf, &pos, 0, 1) // audioMuxVersion
bits.WriteBits(buf, &pos, 1, 1) // allStreamsSameTimeFraming
bits.WriteBits(buf, &pos, uint64(c.NumSubFrames), 6)
bits.WriteBits(buf, &pos, uint64(len(c.Programs)-1), 4)
bits.WriteBitsUnsafe(buf, &pos, 0, 1) // audioMuxVersion
bits.WriteBitsUnsafe(buf, &pos, 1, 1) // allStreamsSameTimeFraming
bits.WriteBitsUnsafe(buf, &pos, uint64(c.NumSubFrames), 6)
bits.WriteBitsUnsafe(buf, &pos, uint64(len(c.Programs)-1), 4)

for prog, p := range c.Programs {
bits.WriteBits(buf, &pos, uint64(len(p.Layers)-1), 3)
bits.WriteBitsUnsafe(buf, &pos, uint64(len(p.Layers)-1), 3)

for lay, l := range p.Layers {
if prog != 0 || lay != 0 {
if l.AudioSpecificConfig != nil {
bits.WriteBits(buf, &pos, 0, 1)
bits.WriteBitsUnsafe(buf, &pos, 0, 1)
} else {
bits.WriteBits(buf, &pos, 1, 1)
bits.WriteBitsUnsafe(buf, &pos, 1, 1)
}
}

Expand All @@ -263,30 +263,30 @@ func (c StreamMuxConfig) Marshal() ([]byte, error) {
}
}

bits.WriteBits(buf, &pos, uint64(l.FrameLengthType), 3)
bits.WriteBitsUnsafe(buf, &pos, uint64(l.FrameLengthType), 3)

switch l.FrameLengthType {
case 0:
bits.WriteBits(buf, &pos, uint64(l.LatmBufferFullness), 8)
bits.WriteBitsUnsafe(buf, &pos, uint64(l.LatmBufferFullness), 8)

case 1:
bits.WriteBits(buf, &pos, uint64(l.FrameLength), 9)
bits.WriteBitsUnsafe(buf, &pos, uint64(l.FrameLength), 9)

case 4, 5, 3:
bits.WriteBits(buf, &pos, uint64(l.CELPframeLengthTableIndex), 6)
bits.WriteBitsUnsafe(buf, &pos, uint64(l.CELPframeLengthTableIndex), 6)

case 6, 7:
if l.HVXCframeLengthTableIndex {
bits.WriteBits(buf, &pos, 1, 1)
bits.WriteBitsUnsafe(buf, &pos, 1, 1)
} else {
bits.WriteBits(buf, &pos, 0, 1)
bits.WriteBitsUnsafe(buf, &pos, 0, 1)
}
}
}
}

if c.OtherDataPresent {
bits.WriteBits(buf, &pos, 1, 1)
bits.WriteBitsUnsafe(buf, &pos, 1, 1)

var lenBytes []byte
tmp := c.OtherDataLenBits
Expand All @@ -303,21 +303,21 @@ func (c StreamMuxConfig) Marshal() ([]byte, error) {
}

for i := len(lenBytes) - 1; i > 0; i-- {
bits.WriteBits(buf, &pos, 1, 1)
bits.WriteBits(buf, &pos, uint64(lenBytes[i]), 8)
bits.WriteBitsUnsafe(buf, &pos, 1, 1)
bits.WriteBitsUnsafe(buf, &pos, uint64(lenBytes[i]), 8)
}

bits.WriteBits(buf, &pos, 0, 1)
bits.WriteBits(buf, &pos, uint64(lenBytes[0]), 8)
bits.WriteBitsUnsafe(buf, &pos, 0, 1)
bits.WriteBitsUnsafe(buf, &pos, uint64(lenBytes[0]), 8)
} else {
bits.WriteBits(buf, &pos, 0, 1)
bits.WriteBitsUnsafe(buf, &pos, 0, 1)
}

if c.CRCCheckPresent {
bits.WriteBits(buf, &pos, 1, 1)
bits.WriteBits(buf, &pos, uint64(c.CRCCheckSum), 8)
bits.WriteBitsUnsafe(buf, &pos, 1, 1)
bits.WriteBitsUnsafe(buf, &pos, uint64(c.CRCCheckSum), 8)
} else {
bits.WriteBits(buf, &pos, 0, 1)
bits.WriteBitsUnsafe(buf, &pos, 0, 1)
}

return buf, nil
Expand Down

0 comments on commit a1df134

Please sign in to comment.