util.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package rtcp
  4. // getPadding Returns the padding required to make the length a multiple of 4
  5. func getPadding(packetLen int) int {
  6. if packetLen%4 == 0 {
  7. return 0
  8. }
  9. return 4 - (packetLen % 4)
  10. }
  11. // setNBitsOfUint16 will truncate the value to size, left-shift to startIndex position and set
  12. func setNBitsOfUint16(src, size, startIndex, val uint16) (uint16, error) {
  13. if startIndex+size > 16 {
  14. return 0, errInvalidSizeOrStartIndex
  15. }
  16. // truncate val to size bits
  17. val &= (1 << size) - 1
  18. return src | (val << (16 - size - startIndex)), nil
  19. }
  20. // appendBit32 will left-shift and append n bits of val
  21. func appendNBitsToUint32(src, n, val uint32) uint32 {
  22. return (src << n) | (val & (0xFFFFFFFF >> (32 - n)))
  23. }
  24. // getNBit get n bits from 1 byte, begin with a position
  25. func getNBitsFromByte(b byte, begin, n uint16) uint16 {
  26. endShift := 8 - (begin + n)
  27. mask := (0xFF >> begin) & uint8(0xFF<<endShift)
  28. return uint16(b&mask) >> endShift
  29. }
  30. // get24BitFromBytes get 24bits from `[3]byte` slice
  31. func get24BitsFromBytes(b []byte) uint32 {
  32. return uint32(b[0])<<16 + uint32(b[1])<<8 + uint32(b[2])
  33. }