2019-08-25 10:44:06 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
parse.go provides H.264 NAL unit parsing utilities for the extraction of
|
|
|
|
syntax elements.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2017-2018 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
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package h264
|
|
|
|
|
2019-09-11 03:17:37 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/codec/h264/h264dec"
|
|
|
|
)
|
2019-08-25 10:44:06 +03:00
|
|
|
|
|
|
|
var errNotEnoughBytes = errors.New("not enough bytes to read")
|
|
|
|
|
|
|
|
// NALType returns the NAL type of the given NAL unit bytes. The given NAL unit
|
|
|
|
// may be in byte stream or packet format.
|
2019-09-11 03:17:37 +03:00
|
|
|
// NB: access unit delimiters are skipped.
|
2019-08-25 10:44:06 +03:00
|
|
|
func NALType(n []byte) (int, error) {
|
|
|
|
sc := frameScanner{buf: n}
|
2019-09-11 03:17:37 +03:00
|
|
|
for {
|
|
|
|
b, ok := sc.readByte()
|
2019-08-25 10:44:06 +03:00
|
|
|
if !ok {
|
|
|
|
return 0, errNotEnoughBytes
|
|
|
|
}
|
2019-09-11 03:17:37 +03:00
|
|
|
for i := 1; b == 0x00 && i != 4; i++ {
|
|
|
|
b, ok = sc.readByte()
|
|
|
|
if !ok {
|
|
|
|
return 0, errNotEnoughBytes
|
|
|
|
}
|
|
|
|
if b != 0x01 || (i != 2 && i != 3) {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-25 10:44:06 +03:00
|
|
|
|
2019-09-11 03:17:37 +03:00
|
|
|
b, ok = sc.readByte()
|
|
|
|
if !ok {
|
|
|
|
return 0, errNotEnoughBytes
|
|
|
|
}
|
|
|
|
nalType := int(b & 0x1f)
|
|
|
|
if nalType != h264dec.NALTypeAccessUnitDelimiter {
|
|
|
|
return nalType, nil
|
|
|
|
}
|
2019-08-25 10:44:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type frameScanner struct {
|
|
|
|
off int
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *frameScanner) readByte() (b byte, ok bool) {
|
|
|
|
if s.off >= len(s.buf) {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
b = s.buf[s.off]
|
|
|
|
s.off++
|
|
|
|
return b, true
|
|
|
|
}
|
2022-02-23 02:56:47 +03:00
|
|
|
|
2022-03-04 09:39:50 +03:00
|
|
|
// Trim will trim down a given byte stream of video data so that a key frame appears first.
|
2022-02-23 02:56:47 +03:00
|
|
|
func Trim(n []byte) ([]byte, error) {
|
|
|
|
sc := frameScanner{buf: n}
|
|
|
|
for {
|
|
|
|
b, ok := sc.readByte()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNotEnoughBytes
|
|
|
|
}
|
|
|
|
for i := 1; b == 0x00 && i != 4; i++ {
|
|
|
|
b, ok = sc.readByte()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNotEnoughBytes
|
|
|
|
}
|
|
|
|
if b != 0x01 || (i != 2 && i != 3) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok = sc.readByte()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNotEnoughBytes
|
|
|
|
}
|
|
|
|
nalType := int(b & 0x1f)
|
|
|
|
if nalType == 7 {
|
|
|
|
sc.off = sc.off - 4
|
|
|
|
return sc.buf[sc.off:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|