av/nal/NalUnit.go

102 lines
2.4 KiB
Go

/*
NAME
PES.go -
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
PES.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package nal
type NALUnit interface {
ToByteSlice() []byte
GetType() byte
}
type NALSpsPps struct {
Data []byte
}
type NALFragment struct {
ThreeNUBs byte
FragmentType byte
Start bool
End bool
Reserved bool
FiveNUBs byte
Data []byte
}
func GetNalType(unit []byte) byte {
return unit[0] & 0x1F
}
/*
First byte: [ 3 NAL UNIT BITS | 5 FRAGMENT TYPE BITS]
Second byte: [ START BIT | END BIT | RESERVED BIT | 5 NAL UNIT BITS]
Other bytes: [... VIDEO FRAGMENT DATA...]
*/
func ParseNALFragment(unit []byte) (u *NALFragment) {
u = new(NALFragment)
u.ThreeNUBs = (unit[0] & 0xE0) >> 5
u.FragmentType = unit[0] & 0x1F
u.Start = (unit[1] & 0x80) != 0
u.End = (unit[1] & 0x40) != 0
u.Reserved = (unit[1] & 0x20) != 0
u.FiveNUBs = unit[1] & 0x1F
u.Data = make([]byte,len(unit[2:]))
copy(u.Data[:],unit[2:])
return
}
func ParseNALSpsPps(unit []byte)(u *NALSpsPps){
u = new(NALSpsPps)
u.Data = make([]byte,len(unit))
copy(u.Data[:],unit[:])
return
}
func (u *NALFragment) ToByteSlice() (output []byte) {
output = make([]byte, 2+len(u.Data))
output[0] = ( u.ThreeNUBs << 5 ) | u.FragmentType
output[1] = boolToByte( u.Start ) << 7 |
boolToByte( u.End ) << 6 |
boolToByte( u.Reserved ) << 5 |
u.FiveNUBs
copy(output[2:],u.Data)
return
}
func (u *NALFragment) GetType() byte {
return GetNalType(u.ToByteSlice())
}
func (u *NALSpsPps) GetType() byte {
return GetNalType(u.ToByteSlice())
}
func (u *NALSpsPps) ToByteSlice() (output []byte){
output = make([]byte,len(u.Data))
output = u.Data
return
}