mirror of
				https://github.com/aler9/gortsplib
				synced 2025-10-31 18:42:40 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			234 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			234 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package formats
 | |
| 
 | |
| import (
 | |
| 	"encoding/base64"
 | |
| 	"fmt"
 | |
| 	"strconv"
 | |
| 	"sync"
 | |
| 
 | |
| 	"github.com/bluenviron/mediacommon/pkg/codecs/h265"
 | |
| 	"github.com/pion/rtp"
 | |
| 
 | |
| 	"github.com/bluenviron/gortsplib/v3/pkg/formats/rtph265"
 | |
| )
 | |
| 
 | |
| // check whether a RTP/H265 packet is random access, without decoding the packet.
 | |
| func rtpH265IsRandomAccess(pkt *rtp.Packet) bool {
 | |
| 	if len(pkt.Payload) == 0 {
 | |
| 		return false
 | |
| 	}
 | |
| 
 | |
| 	typ := h265.NALUType((pkt.Payload[0] >> 1) & 0b111111)
 | |
| 
 | |
| 	switch typ {
 | |
| 	case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT:
 | |
| 		return true
 | |
| 
 | |
| 	case h265.NALUType_AggregationUnit:
 | |
| 		payload := pkt.Payload[2:]
 | |
| 
 | |
| 		for len(payload) > 0 {
 | |
| 			if len(payload) < 2 {
 | |
| 				return false
 | |
| 			}
 | |
| 
 | |
| 			size := uint16(payload[0])<<8 | uint16(payload[1])
 | |
| 			payload = payload[2:]
 | |
| 
 | |
| 			if size == 0 || int(size) > len(payload) {
 | |
| 				return false
 | |
| 			}
 | |
| 
 | |
| 			var nalu []byte
 | |
| 			nalu, payload = payload[:size], payload[size:]
 | |
| 
 | |
| 			typ = h265.NALUType((nalu[0] >> 1) & 0b111111)
 | |
| 			if typ == h265.NALUType_IDR_W_RADL || typ == h265.NALUType_IDR_N_LP || typ == h265.NALUType_CRA_NUT {
 | |
| 				return true
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 	case h265.NALUType_FragmentationUnit:
 | |
| 		if len(pkt.Payload) < 3 {
 | |
| 			return false
 | |
| 		}
 | |
| 
 | |
| 		start := pkt.Payload[1] >> 7
 | |
| 		if start != 1 {
 | |
| 			return false
 | |
| 		}
 | |
| 
 | |
| 		typ := h265.NALUType(pkt.Payload[2] & 0b111111)
 | |
| 		if typ == h265.NALUType_IDR_W_RADL || typ == h265.NALUType_IDR_N_LP || typ == h265.NALUType_CRA_NUT {
 | |
| 			return true
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return false
 | |
| }
 | |
| 
 | |
| // H265 is a RTP format for the H265 codec.
 | |
| // Specification: https://datatracker.ietf.org/doc/html/rfc7798
 | |
| type H265 struct {
 | |
| 	PayloadTyp uint8
 | |
| 	VPS        []byte
 | |
| 	SPS        []byte
 | |
| 	PPS        []byte
 | |
| 	MaxDONDiff int
 | |
| 
 | |
| 	mutex sync.RWMutex
 | |
| }
 | |
| 
 | |
| func (f *H265) unmarshal(payloadType uint8, _ string, _ string, _ string, fmtp map[string]string) error {
 | |
| 	f.PayloadTyp = payloadType
 | |
| 
 | |
| 	for key, val := range fmtp {
 | |
| 		switch key {
 | |
| 		case "sprop-vps":
 | |
| 			var err error
 | |
| 			f.VPS, err = base64.StdEncoding.DecodeString(val)
 | |
| 			if err != nil {
 | |
| 				return fmt.Errorf("invalid sprop-vps (%v)", fmtp)
 | |
| 			}
 | |
| 
 | |
| 		case "sprop-sps":
 | |
| 			var err error
 | |
| 			f.SPS, err = base64.StdEncoding.DecodeString(val)
 | |
| 			if err != nil {
 | |
| 				return fmt.Errorf("invalid sprop-sps (%v)", fmtp)
 | |
| 			}
 | |
| 
 | |
| 		case "sprop-pps":
 | |
| 			var err error
 | |
| 			f.PPS, err = base64.StdEncoding.DecodeString(val)
 | |
| 			if err != nil {
 | |
| 				return fmt.Errorf("invalid sprop-pps (%v)", fmtp)
 | |
| 			}
 | |
| 
 | |
| 		case "sprop-max-don-diff":
 | |
| 			tmp, err := strconv.ParseUint(val, 10, 31)
 | |
| 			if err != nil {
 | |
| 				return fmt.Errorf("invalid sprop-max-don-diff (%v)", fmtp)
 | |
| 			}
 | |
| 			f.MaxDONDiff = int(tmp)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // Codec implements Format.
 | |
| func (f *H265) Codec() string {
 | |
| 	return "H265"
 | |
| }
 | |
| 
 | |
| // String implements Format.
 | |
| //
 | |
| // Deprecated: replaced by Codec().
 | |
| func (f *H265) String() string {
 | |
| 	return f.Codec()
 | |
| }
 | |
| 
 | |
| // ClockRate implements Format.
 | |
| func (f *H265) ClockRate() int {
 | |
| 	return 90000
 | |
| }
 | |
| 
 | |
| // PayloadType implements Format.
 | |
| func (f *H265) PayloadType() uint8 {
 | |
| 	return f.PayloadTyp
 | |
| }
 | |
| 
 | |
| // RTPMap implements Format.
 | |
| func (f *H265) RTPMap() string {
 | |
| 	return "H265/90000"
 | |
| }
 | |
| 
 | |
| // FMTP implements Format.
 | |
| func (f *H265) FMTP() map[string]string {
 | |
| 	f.mutex.RLock()
 | |
| 	defer f.mutex.RUnlock()
 | |
| 
 | |
| 	fmtp := make(map[string]string)
 | |
| 	if f.VPS != nil {
 | |
| 		fmtp["sprop-vps"] = base64.StdEncoding.EncodeToString(f.VPS)
 | |
| 	}
 | |
| 	if f.SPS != nil {
 | |
| 		fmtp["sprop-sps"] = base64.StdEncoding.EncodeToString(f.SPS)
 | |
| 	}
 | |
| 	if f.PPS != nil {
 | |
| 		fmtp["sprop-pps"] = base64.StdEncoding.EncodeToString(f.PPS)
 | |
| 	}
 | |
| 	if f.MaxDONDiff != 0 {
 | |
| 		fmtp["sprop-max-don-diff"] = strconv.FormatInt(int64(f.MaxDONDiff), 10)
 | |
| 	}
 | |
| 
 | |
| 	return fmtp
 | |
| }
 | |
| 
 | |
| // PTSEqualsDTS implements Format.
 | |
| func (f *H265) PTSEqualsDTS(pkt *rtp.Packet) bool {
 | |
| 	return rtpH265IsRandomAccess(pkt)
 | |
| }
 | |
| 
 | |
| // CreateDecoder creates a decoder able to decode the content of the format.
 | |
| //
 | |
| // Deprecated: this has been replaced by CreateDecoder2() that can also return an error.
 | |
| func (f *H265) CreateDecoder() *rtph265.Decoder {
 | |
| 	d, _ := f.CreateDecoder2()
 | |
| 	return d
 | |
| }
 | |
| 
 | |
| // CreateDecoder2 creates a decoder able to decode the content of the format.
 | |
| func (f *H265) CreateDecoder2() (*rtph265.Decoder, error) {
 | |
| 	d := &rtph265.Decoder{
 | |
| 		MaxDONDiff: f.MaxDONDiff,
 | |
| 	}
 | |
| 
 | |
| 	err := d.Init()
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return d, nil
 | |
| }
 | |
| 
 | |
| // CreateEncoder creates an encoder able to encode the content of the format.
 | |
| //
 | |
| // Deprecated: this has been replaced by CreateEncoder2() that can also return an error.
 | |
| func (f *H265) CreateEncoder() *rtph265.Encoder {
 | |
| 	e, _ := f.CreateEncoder2()
 | |
| 	return e
 | |
| }
 | |
| 
 | |
| // CreateEncoder2 creates an encoder able to encode the content of the format.
 | |
| func (f *H265) CreateEncoder2() (*rtph265.Encoder, error) {
 | |
| 	e := &rtph265.Encoder{
 | |
| 		PayloadType: f.PayloadTyp,
 | |
| 		MaxDONDiff:  f.MaxDONDiff,
 | |
| 	}
 | |
| 
 | |
| 	err := e.Init()
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return e, nil
 | |
| }
 | |
| 
 | |
| // SafeSetParams sets the codec parameters.
 | |
| func (f *H265) SafeSetParams(vps []byte, sps []byte, pps []byte) {
 | |
| 	f.mutex.Lock()
 | |
| 	defer f.mutex.Unlock()
 | |
| 	f.VPS = vps
 | |
| 	f.SPS = sps
 | |
| 	f.PPS = pps
 | |
| }
 | |
| 
 | |
| // SafeParams returns the codec parameters.
 | |
| func (f *H265) SafeParams() ([]byte, []byte, []byte) {
 | |
| 	f.mutex.RLock()
 | |
| 	defer f.mutex.RUnlock()
 | |
| 	return f.VPS, f.SPS, f.PPS
 | |
| }
 | 
