2017-12-20 09:47:28 +03:00
|
|
|
/*
|
|
|
|
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 packets
|
|
|
|
|
2017-12-21 05:45:50 +03:00
|
|
|
type NALUnit interface {
|
|
|
|
ToByteSlice() []byte
|
|
|
|
GetType() byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type NALSpsPps struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type NALFragment struct {
|
2017-12-20 09:47:28 +03:00
|
|
|
ThreeNUBs byte
|
|
|
|
FragmentType byte
|
|
|
|
Start bool
|
|
|
|
End bool
|
|
|
|
Reserved bool
|
|
|
|
FiveNUBs byte
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2017-12-21 05:45:50 +03:00
|
|
|
|
|
|
|
func GetNalType(unit []byte) byte {
|
|
|
|
return unit[0] & 0x1F
|
|
|
|
}
|
|
|
|
|
2017-12-20 09:47:28 +03:00
|
|
|
/*
|
|
|
|
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...]
|
|
|
|
*/
|
2017-12-21 05:45:50 +03:00
|
|
|
func ParseNALFragment(unit []byte) (u *NALFragment) {
|
|
|
|
u = new(NALFragment)
|
|
|
|
u.ThreeNUBs = (unit[0] & 0xE0) >> 5
|
2017-12-20 09:47:28 +03:00
|
|
|
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 = unit[2:]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-21 05:45:50 +03:00
|
|
|
func ParseNALSpsPps(unit []byte)(u *NALSpsPps){
|
|
|
|
u = new(NALSpsPps)
|
|
|
|
u.Data = make([]byte,len(unit))
|
|
|
|
u.Data = unit
|
|
|
|
return
|
2017-12-20 09:47:28 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 05:45:50 +03:00
|
|
|
func (u *NALFragment) ToByteSlice() (output []byte) {
|
2017-12-20 09:47:28 +03:00
|
|
|
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
|
|
|
|
}
|
2017-12-21 05:45:50 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|