coremedia

package
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatDescriptorMagic            uint32 = 0x66647363 //fdsc - csdf
	MediaTypeVideo                   uint32 = 0x76696465 //vide - ediv
	MediaTypeSound                   uint32 = 0x736F756E //nuos - soun
	MediaTypeMagic                   uint32 = 0x6D646961 //mdia - aidm
	VideoDimensionMagic              uint32 = 0x7664696D //vdim - midv
	CodecMagic                       uint32 = 0x636F6463 //codc - cdoc
	CodecAvc1                        uint32 = 0x61766331 //avc1 - 1cva
	ExtensionMagic                   uint32 = 0x6578746E //extn - ntxe
	AudioStreamBasicDescriptionMagic uint32 = 0x61736264 //asdb - dbsa
)

Those are the markers found in the hex dumps. For convenience I have added the ASCII representation as a comment in normal byte order and reverse byteorder (so you can find them in the hex dumps) Note: I have just guessed what the names could be from the marker ascii, I could be wrong ;-)

View Source
const (
	KCMTimeFlagsValid                 uint32 = 0x0
	KCMTimeFlagsHasBeenRounded        uint32 = 0x1
	KCMTimeFlagsPositiveInfinity      uint32 = 0x2
	KCMTimeFlagsNegativeInfinity      uint32 = 0x4
	KCMTimeFlagsIndefinite            uint32 = 0x8
	KCMTimeFlagsImpliedValueFlagsMask        = KCMTimeFlagsPositiveInfinity | KCMTimeFlagsNegativeInfinity | KCMTimeFlagsIndefinite
	CMTimeLengthInBytes               int    = 24
)

Constants for the CMTime struct

View Source
const (
	KeyValuePairMagic uint32 = 0x6B657976 //keyv - vyek
	StringKey         uint32 = 0x7374726B //strk - krts
	IntKey            uint32 = 0x6964786B //idxk - kxdi
	BooleanValueMagic uint32 = 0x62756C76 //bulv - vlub
	DictionaryMagic   uint32 = 0x64696374 //dict - tcid
	DataValueMagic    uint32 = 0x64617476 //datv - vtad
	StringValueMagic  uint32 = 0x73747276 //strv - vrts
)

Dictionary related magic marker constants

View Source
const AudioFormatIDLpcm uint32 = 0x6C70636D

AudioFormatIDLpcm is the CoreMedia MediaID for LPCM

View Source
const NanoSecondScale = 1000000000

NanoSecondScale is the default system clock scale where 1/NanoSecondScale == 1 Nanosecond.

Variables

This section is empty.

Functions

func CalculateSkew

func CalculateSkew(startTimeClock1 CMTime, endTimeClock1 CMTime, startTimeClock2 CMTime, endTimeClock2 CMTime) float64

CalculateSkew calculates the deviation between the frequencies of two given clocks by using time diffs and returns a skew value float64 scaled to match the second clock.

func GetNaluDetails

func GetNaluDetails(nalu []byte) string

GetNaluDetails creates a string containing length and type of a h264-nalu.

func GetWavHeaderBytes

func GetWavHeaderBytes(length int) ([]byte, error)

GetWavHeaderBytes creates a byte slice containing a valid wav header using the supplied length.

func SerializeStringKeyDict

func SerializeStringKeyDict(stringKeyDict StringKeyDict) []byte

SerializeStringKeyDict serializes a StringKeyDict into a []byte

func Table

func Table() []string

Table Returns a table containing all h264 nalu types

func WriteWavHeader

func WriteWavHeader(length int, wavFile *os.File) error

WriteWavHeader creates a wave file header using the given length and writes it at the BEGINNING of the wavFile. Please make sure that the file has enough zero bytes before the audio data.

Types

type AVCustomWriter

type AVCustomWriter struct {
	// contains filtered or unexported fields
}

func NewAVCustomWriter

func NewAVCustomWriter(stream io.Writer) AVCustomWriter

func (AVCustomWriter) Consume

func (avfw AVCustomWriter) Consume(buf CMSampleBuffer) error

Consume writes PPS and SPS as well as sample bufs into a annex b .h264 file and audio samples into a wav file

func (AVCustomWriter) Stop

func (avfw AVCustomWriter) Stop()

Nothing currently

type AVFileWriter

type AVFileWriter struct {
	// contains filtered or unexported fields
}

AVFileWriter writes nalus into a file using 0x00000001 as a separator (h264 ANNEX B) and raw pcm audio into a wav file Note that you will have to call WriteWavHeader() on the audiofile when you are done to write a wav header and get a valid file.

func NewAVFileWriter

func NewAVFileWriter(h264FileWriter io.Writer, wavFileWriter io.Writer) AVFileWriter

NewAVFileWriter binary writes nalus in annex b format to the given writer and audio buffers into a wav file. Note that you will have to call WriteWavHeader() on the audiofile when you are done to write a wav header and get a valid file.

func NewAVFileWriterAudioOnly

func NewAVFileWriterAudioOnly(wavFileWriter io.Writer) AVFileWriter

func (AVFileWriter) Consume

func (avfw AVFileWriter) Consume(buf CMSampleBuffer) error

Consume writes PPS and SPS as well as sample bufs into a annex b .h264 file and audio samples into a wav file Note that you will have to call WriteWavHeader() on the audiofile when you are done to write a wav header and get a valid file.

func (AVFileWriter) Stop

func (avfw AVFileWriter) Stop()

Nothing currently

type AudioStreamBasicDescription

type AudioStreamBasicDescription struct {
	SampleRate       float64
	FormatID         uint32
	FormatFlags      uint32
	BytesPerPacket   uint32
	FramesPerPacket  uint32
	BytesPerFrame    uint32
	ChannelsPerFrame uint32
	BitsPerChannel   uint32
	Reserved         uint32
}

AudioStreamBasicDescription represents the struct found here: https://github.com/nu774/MSResampler/blob/master/CoreAudio/CoreAudioTypes.h

func DefaultAudioStreamBasicDescription

func DefaultAudioStreamBasicDescription() AudioStreamBasicDescription

DefaultAudioStreamBasicDescription creates a LPCM AudioStreamBasicDescription with default values I grabbed from the hex dump

func NewAudioStreamBasicDescriptionFromBytes

func NewAudioStreamBasicDescriptionFromBytes(data []byte) (AudioStreamBasicDescription, error)

NewAudioStreamBasicDescriptionFromBytes reads AudioStreamBasicDescription from bytes

func (AudioStreamBasicDescription) SerializeAudioStreamBasicDescription

func (adsb AudioStreamBasicDescription) SerializeAudioStreamBasicDescription(adsbBytes []byte)

SerializeAudioStreamBasicDescription puts an AudioStreamBasicDescription into the given byte array

func (AudioStreamBasicDescription) String

func (adsb AudioStreamBasicDescription) String() string

type CMClock

type CMClock struct {
	ID        uint64
	TimeScale uint32
	// contains filtered or unexported fields
}

CMClock represents a monotonic Clock that will start counting when created

func NewCMClockWithHostTime

func NewCMClockWithHostTime(ID uint64) CMClock

NewCMClockWithHostTime creates a new Clock with the given ID with a nanosecond scale. Calls to GetTime will measure the time difference since the clock was created.

func NewCMClockWithHostTimeAndScale

func NewCMClockWithHostTimeAndScale(ID uint64, timeScale uint32) CMClock

NewCMClockWithHostTimeAndScale creates a new CMClock with given ID and a custom timeScale

func (CMClock) GetTime

func (c CMClock) GetTime() CMTime

GetTime returns a CMTime that gives the time passed since the clock started. This is monotonic and does NOT use wallclock time.

type CMItemCount

type CMItemCount = int

CMItemCount is a simple typedef to int to be a bit closer to MacOS/iOS

type CMSampleBuffer

type CMSampleBuffer struct {
	OutputPresentationTimestamp CMTime
	FormatDescription           FormatDescriptor
	HasFormatDescription        bool
	NumSamples                  CMItemCount          //nsmp
	SampleTimingInfoArray       []CMSampleTimingInfo //stia
	SampleData                  []byte
	SampleSizes                 []int
	Attachments                 IndexKeyDict //satt
	Sary                        IndexKeyDict //sary
	MediaType                   uint32
}

CMSampleBuffer represents the CoreMedia class used to exchange AV SampleData and contains meta information like timestamps or optional FormatDescriptors

func NewCMSampleBufferFromBytes

func NewCMSampleBufferFromBytes(data []byte, mediaType uint32) (CMSampleBuffer, error)

NewCMSampleBufferFromBytes parses a CMSampleBuffer from a []byte assuming it begins with a 4 byte length and the 4byte magic int "sbuf"

func NewCMSampleBufferFromBytesAudio

func NewCMSampleBufferFromBytesAudio(data []byte) (CMSampleBuffer, error)

NewCMSampleBufferFromBytesAudio parses a CMSampleBuffer containing audio data.

func NewCMSampleBufferFromBytesVideo

func NewCMSampleBufferFromBytesVideo(data []byte) (CMSampleBuffer, error)

NewCMSampleBufferFromBytesVideo parses a CMSampleBuffer containing audio video.

func (CMSampleBuffer) HasSampleData

func (buffer CMSampleBuffer) HasSampleData() bool

func (CMSampleBuffer) String

func (buffer CMSampleBuffer) String() string

type CMSampleTimingInfo

type CMSampleTimingInfo struct {
	Duration CMTime /*! @field duration
	The duration of the sample. If a single struct applies to
	each of the samples, they all will have this duration. */
	PresentationTimeStamp CMTime /*! @field presentationTimeStamp
	The time at which the sample will be presented. If a single
	struct applies to each of the samples, this is the presentationTime of the
	first sample. The presentationTime of subsequent samples will be derived by
	repeatedly adding the sample duration. */
	DecodeTimeStamp CMTime /*! @field decodeTimeStamp
	The time at which the sample will be decoded. If the samples
	are in presentation order, this must be set to kCMTimeInvalid. */
}

CMSampleTimingInfo is a simple struct containing 3 CMtimes: Duration, PresentationTimeStamp and DecodeTimeStamp

func (CMSampleTimingInfo) String

func (info CMSampleTimingInfo) String() string

type CMTime

type CMTime struct {
	CMTimeValue uint64 /*! @field value The value of the CMTime. value/timescale = seconds. */
	CMTimeScale uint32 /*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */
	CMTimeFlags uint32 /*! @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc. */
	CMTimeEpoch uint64 /*! @field epoch Differentiates between equal timestamps that are actually different because
	of looping, multi-item sequencing, etc.
	Will be used during comparison: greater epochs happen after lesser ones.
	Additions/subtraction is only possible within a single epoch,
	however, since epoch length may be unknown/variable. */
}

CMTime is taken from https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.8.sdk/System/Library/Frameworks/CoreMedia.framework/Versions/A/Headers/CMTime.h

func NewCMTimeFromBytes

func NewCMTimeFromBytes(data []byte) (CMTime, error)

NewCMTimeFromBytes reads a CMTime struct directly from the given byte slice

func (CMTime) GetTimeForScale

func (time CMTime) GetTimeForScale(newScaleToUse CMTime) float64

GetTimeForScale calculates a float64 TimeValue by rescaling this CMTime to the CMTimeScale of the given CMTime

func (CMTime) Seconds

func (time CMTime) Seconds() uint64

Seconds returns CMTimeValue/CMTimeScale and 0 when all values are 0

func (CMTime) Serialize

func (time CMTime) Serialize(target []byte) error

Serialize serializes this CMTime into a given byte slice that needs to be at least of CMTimeLengthInBytes length

func (CMTime) String

func (time CMTime) String() string

type FormatDescriptor

type FormatDescriptor struct {
	MediaType            uint32
	VideoDimensionWidth  uint32
	VideoDimensionHeight uint32
	Codec                uint32
	Extensions           IndexKeyDict
	//PPS contains bytes of the Picture Parameter Set h264 NALu
	PPS []byte
	//SPS contains bytes of the Picture Parameter Set h264 NALu
	SPS                         []byte
	AudioStreamBasicDescription AudioStreamBasicDescription
}

FormatDescriptor is actually a CMFormatDescription https://developer.apple.com/documentation/coremedia/cmformatdescription https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.9.sdk/System/Library/Frameworks/CoreMedia.framework/Versions/A/Headers/CMFormatDescription.h

func NewFormatDescriptorFromBytes

func NewFormatDescriptorFromBytes(data []byte) (FormatDescriptor, error)

NewFormatDescriptorFromBytes parses a CMFormatDescription from bytes

func (FormatDescriptor) String

func (fdsc FormatDescriptor) String() string

type IndexKeyDict

type IndexKeyDict struct {
	Entries []IndexKeyEntry
}

IndexKeyDict a dictionary that uses uint16 as keys with an array of IndexKeyEntry

func NewIndexDictFromBytes

func NewIndexDictFromBytes(data []byte) (IndexKeyDict, error)

NewIndexDictFromBytes creates a new dictionary assuming the byte array starts with the 4 byte length of the dictionary followed by "dict" as the magic marker

func NewIndexDictFromBytesWithCustomMarker

func NewIndexDictFromBytesWithCustomMarker(data []byte, magic uint32) (IndexKeyDict, error)

NewIndexDictFromBytesWithCustomMarker creates a new dictionary assuming the byte array starts with the 4 byte length of the dictionary followed by magic as the magic marker

func (IndexKeyDict) String

func (dt IndexKeyDict) String() string

type IndexKeyEntry

type IndexKeyEntry struct {
	Key   uint16
	Value interface{}
}

IndexKeyEntry is a pair of a uint16 key and an arbitrary value.

type StringKeyDict

type StringKeyDict struct {
	Entries []StringKeyEntry
}

StringKeyDict a dictionary that uses strings as keys with an array of StringKeyEntry

func NewStringDictFromBytes

func NewStringDictFromBytes(data []byte) (StringKeyDict, error)

NewStringDictFromBytes creates a new dictionary assuming the byte array starts with the 4 byte length of the dictionary followed by "dict" as the magic marker

func (StringKeyDict) String

func (dt StringKeyDict) String() string

type StringKeyEntry

type StringKeyEntry struct {
	Key   string
	Value interface{}
}

StringKeyEntry a pair of a string key and an arbitrary value

func ParseKeyValueEntry

func ParseKeyValueEntry(data []byte) (StringKeyEntry, error)

ParseKeyValueEntry parses a byte array into a StringKeyEntry assuming the array starts with a 4 byte length followed by the "keyv" magic

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL