github.com/cnotch/ipchub@v1.1.0/utils/h264or5.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package utils
     6  
     7  // RemoveH264or5EmulationBytes A general routine for making a copy of a (H.264 or H.265) NAL unit, removing 'emulation' bytes from the copy
     8  // copy from live555
     9  func RemoveH264or5EmulationBytes(from []byte) []byte {
    10  	to := make([]byte, len(from))
    11  	toMaxSize := len(to)
    12  	fromSize := len(from)
    13  	toSize := 0
    14  	i := 0
    15  	for i < fromSize && toSize+1 < toMaxSize {
    16  		if i+2 < fromSize && from[i] == 0 && from[i+1] == 0 && from[i+2] == 3 {
    17  			to[toSize] = 0
    18  			to[toSize+1] = 0
    19  			toSize += 2
    20  			i += 3
    21  		} else {
    22  			to[toSize] = from[i]
    23  			toSize++
    24  			i++
    25  		}
    26  	}
    27  
    28  	// 如果剩余最后一个字节,拷贝它
    29  	if i < fromSize && toSize < toMaxSize {
    30  		to[toSize] = from[i]
    31  		toSize++
    32  		i++
    33  	}
    34  
    35  	return to[:toSize]
    36  	// return bytes.Replace(from, []byte{0, 0, 3}, []byte{0, 0}, -1)
    37  }