av1.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package codec
  2. import (
  3. "errors"
  4. "io"
  5. )
  6. var (
  7. ErrInvalidMarker = errors.New("invalid marker value found in AV1CodecConfigurationRecord")
  8. ErrInvalidVersion = errors.New("unsupported AV1CodecConfigurationRecord version")
  9. ErrNonZeroReservedBits = errors.New("non-zero reserved bits found in AV1CodecConfigurationRecord")
  10. )
  11. const (
  12. AV1_OBU_SEQUENCE_HEADER = 1
  13. AV1_OBU_TEMPORAL_DELIMITER = 2
  14. AV1_OBU_FRAME_HEADER = 3
  15. AV1_OBU_TILE_GROUP = 4
  16. AV1_OBU_METADATA = 5
  17. AV1_OBU_FRAME = 6
  18. AV1_OBU_REDUNDANT_FRAME_HEADER = 7
  19. AV1_OBU_TILE_LIST = 8
  20. AV1_OBU_PADDING = 15
  21. )
  22. type AV1CodecConfigurationRecord struct {
  23. Version byte
  24. SeqProfile byte
  25. SeqLevelIdx0 byte
  26. SeqTier0 byte
  27. HighBitdepth byte
  28. TwelveBit byte
  29. MonoChrome byte
  30. ChromaSubsamplingX byte
  31. ChromaSubsamplingY byte
  32. ChromaSamplePosition byte
  33. InitialPresentationDelayPresent byte
  34. InitialPresentationDelayMinusOne byte
  35. ConfigOBUs []byte
  36. }
  37. func (p *AV1CodecConfigurationRecord) Unmarshal(data []byte) (n int, err error) {
  38. l := len(data)
  39. if l < 4 {
  40. err = io.ErrShortWrite
  41. return
  42. }
  43. Marker := data[0] >> 7
  44. if Marker != 1 {
  45. return 0, ErrInvalidMarker
  46. }
  47. p.Version = data[0] & 0x7F
  48. if p.Version != 1 {
  49. return 1, ErrInvalidVersion
  50. }
  51. p.SeqProfile = data[1] >> 5
  52. p.SeqLevelIdx0 = data[1] & 0x1F
  53. p.SeqTier0 = data[2] >> 7
  54. p.HighBitdepth = (data[2] >> 6) & 0x01
  55. p.TwelveBit = (data[2] >> 5) & 0x01
  56. p.MonoChrome = (data[2] >> 4) & 0x01
  57. p.ChromaSubsamplingX = (data[2] >> 3) & 0x01
  58. p.ChromaSubsamplingY = (data[2] >> 2) & 0x01
  59. p.ChromaSamplePosition = data[2] & 0x03
  60. if data[3]>>5 != 0 {
  61. return 3, ErrNonZeroReservedBits
  62. }
  63. p.InitialPresentationDelayPresent = (data[3] >> 4) & 0x01
  64. if p.InitialPresentationDelayPresent == 1 {
  65. p.InitialPresentationDelayMinusOne = data[3] & 0x0F
  66. } else {
  67. if data[3]&0x0F != 0 {
  68. return 3, ErrNonZeroReservedBits
  69. }
  70. p.InitialPresentationDelayMinusOne = 0
  71. }
  72. if l > 4 {
  73. p.ConfigOBUs = data[4:]
  74. }
  75. return l, nil
  76. }