github.com/bluenviron/mediacommon@v1.9.3/pkg/formats/fmp4/part_sample.go (about)

     1  package fmp4
     2  
     3  import (
     4  	"github.com/bluenviron/mediacommon/pkg/codecs/av1"
     5  	"github.com/bluenviron/mediacommon/pkg/codecs/h264"
     6  )
     7  
     8  // PartSample is a sample of a PartTrack.
     9  type PartSample struct {
    10  	Duration        uint32
    11  	PTSOffset       int32
    12  	IsNonSyncSample bool
    13  	Payload         []byte
    14  }
    15  
    16  // NewPartSampleAV1 creates a sample with AV1 data.
    17  func NewPartSampleAV1(sequenceHeaderPresent bool, tu [][]byte) (*PartSample, error) {
    18  	bs, err := av1.BitstreamMarshal(tu)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	return &PartSample{
    24  		IsNonSyncSample: !sequenceHeaderPresent,
    25  		Payload:         bs,
    26  	}, nil
    27  }
    28  
    29  // NewPartSampleH26x creates a sample with H26x data.
    30  func NewPartSampleH26x(ptsOffset int32, randomAccessPresent bool, au [][]byte) (*PartSample, error) {
    31  	avcc, err := h264.AVCCMarshal(au)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return &PartSample{
    37  		PTSOffset:       ptsOffset,
    38  		IsNonSyncSample: !randomAccessPresent,
    39  		Payload:         avcc,
    40  	}, nil
    41  }
    42  
    43  // GetAV1 gets AV1 data from the sample.
    44  func (ps PartSample) GetAV1() ([][]byte, error) {
    45  	tu, err := av1.BitstreamUnmarshal(ps.Payload, true)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return tu, nil
    51  }
    52  
    53  // GetH26x gets H26x data from the sample.
    54  func (ps PartSample) GetH26x() ([][]byte, error) {
    55  	au, err := h264.AVCCUnmarshal(ps.Payload)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return au, nil
    61  }