mirror of
https://github.com/aler9/gortsplib
synced 2025-10-10 17:40:28 +08:00
h264: improve performance
This commit is contained in:
@@ -65,14 +65,23 @@ outer:
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func annexBEncodeSize(nalus [][]byte) int {
|
||||||
|
n := 0
|
||||||
|
for _, nalu := range nalus {
|
||||||
|
n += 4 + len(nalu)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// AnnexBEncode encodes NALUs into the Annex-B stream format.
|
// AnnexBEncode encodes NALUs into the Annex-B stream format.
|
||||||
func AnnexBEncode(nalus [][]byte) ([]byte, error) {
|
func AnnexBEncode(nalus [][]byte) ([]byte, error) {
|
||||||
var ret []byte
|
buf := make([]byte, annexBEncodeSize(nalus))
|
||||||
|
pos := 0
|
||||||
|
|
||||||
for _, nalu := range nalus {
|
for _, nalu := range nalus {
|
||||||
ret = append(ret, []byte{0x00, 0x00, 0x00, 0x01}...)
|
pos += copy(buf[pos:], []byte{0x00, 0x00, 0x00, 0x01})
|
||||||
ret = append(ret, nalu...)
|
pos += copy(buf[pos:], nalu)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
@@ -32,24 +32,25 @@ func AVCCDecode(byts []byte) ([][]byte, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func avccEncodeSize(nalus [][]byte) int {
|
||||||
|
n := 0
|
||||||
|
for _, nalu := range nalus {
|
||||||
|
n += 4 + len(nalu)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// AVCCEncode encodes NALUs into the AVCC stream format.
|
// AVCCEncode encodes NALUs into the AVCC stream format.
|
||||||
func AVCCEncode(nalus [][]byte) ([]byte, error) {
|
func AVCCEncode(nalus [][]byte) ([]byte, error) {
|
||||||
le := 0
|
buf := make([]byte, avccEncodeSize(nalus))
|
||||||
for _, nalu := range nalus {
|
|
||||||
le += 4 + len(nalu)
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make([]byte, le)
|
|
||||||
pos := 0
|
pos := 0
|
||||||
|
|
||||||
for _, nalu := range nalus {
|
for _, nalu := range nalus {
|
||||||
ln := len(nalu)
|
binary.BigEndian.PutUint32(buf[pos:], uint32(len(nalu)))
|
||||||
binary.BigEndian.PutUint32(ret[pos:], uint32(ln))
|
|
||||||
pos += 4
|
pos += 4
|
||||||
|
|
||||||
copy(ret[pos:], nalu)
|
pos += copy(buf[pos:], nalu)
|
||||||
pos += ln
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user