Table of Contents

Class ConfigurationParser

Namespace
VAST.Codecs.H264
Assembly
VAST.Common.dll

Provides parsing functionality for H.264/AVC configuration data (SPS/PPS).

public class ConfigurationParser
Inheritance
ConfigurationParser
Inherited Members

Constructors

ConfigurationParser()

Initializes a new instance of the ConfigurationParser class.

public ConfigurationParser()

Properties

AspectRatio

Gets the sample aspect ratio (SAR) from VUI parameters, or 1:1 if not specified.

public Rational AspectRatio { get; }

Property Value

Rational

Bitrate

Gets the bitrate in bits per second from HRD parameters, or 0 if not available or inconsistent.

public int Bitrate { get; }

Property Value

int

FrameRate

Gets the frame rate as a rational number, derived from VUI timing information.

public Rational FrameRate { get; }

Property Value

Rational

Height

Gets the video height in pixels, calculated from SPS parameters with cropping applied.

public int Height { get; }

Property Value

int

Level

Gets the H.264 level indicator (level_idc) from the SPS.

public int Level { get; }

Property Value

int

MaxBitrate

Gets the maximum bitrate in bits per second based on the H.264 level, or an estimated value if level is unknown.

public int MaxBitrate { get; }

Property Value

int

Profile

Gets the H.264 profile indicator (profile_idc) from the SPS.

public int Profile { get; }

Property Value

int

ProfileLevel

Gets the combined profile and level indicator as a 32-bit value encoding profile_idc, constraint flags, and level_idc.

public int ProfileLevel { get; }

Property Value

int

ReorderingQueueDepth

Gets the frame reordering queue depth (max_num_reorder_frames) from VUI parameters or calculated from level constraints.

public int ReorderingQueueDepth { get; }

Property Value

int

Width

Gets the video width in pixels, calculated from SPS parameters with cropping applied.

public int Width { get; }

Property Value

int

Methods

ConvertCodecPrivateDataToAnnexB(MediaType)

Converts codec private data from AVCDecoderConfigurationRecord format to Annex B format if not already in Annex B format.

public static void ConvertCodecPrivateDataToAnnexB(MediaType mt)

Parameters

mt MediaType

The media type containing the codec private data to convert.

ConvertCodecPrivateDataToAvc(MediaType)

Converts codec private data from Annex B format to AVCDecoderConfigurationRecord format if not already in AVC format.

public static void ConvertCodecPrivateDataToAvc(MediaType mt)

Parameters

mt MediaType

The media type containing the codec private data to convert.

ConvertToAnnexBBitstream(VersatileBuffer, VersatileBuffer, int)

Converts an AVC bitstream to Annex B format.

public static void ConvertToAnnexBBitstream(VersatileBuffer inputPacket, VersatileBuffer outputPacket, int nalUnitSize)

Parameters

inputPacket VersatileBuffer

The input packet in AVC format.

outputPacket VersatileBuffer

The output buffer to write the Annex B bitstream to.

nalUnitSize int

The NAL unit length field size in the input bitstream (typically 4).

ConvertToAnnexBBitstream(VersatileBuffer, VersatileBuffer, int, byte[])

Converts input packet in AVC bitstream format to output packet in Annex B bitstream format inserting parameter sets if necessary

public static void ConvertToAnnexBBitstream(VersatileBuffer inputPacket, VersatileBuffer outputPacket, int nalUnitSize, byte[] parameterSets)

Parameters

inputPacket VersatileBuffer

Input packet in AVC bitstream format

outputPacket VersatileBuffer

Output packet in Annex B bitstream format

nalUnitSize int

NAL unit size of the input bitstream

parameterSets byte[]

Parameter sets to insert into output bitstream if not present in input bitstream. Must be in Annex B format!

ConvertToAvcBitstream(byte[], int, int, VersatileBuffer)

Converts an Annex B bitstream to AVC bitstream format (NAL unit length prefixes).

public static void ConvertToAvcBitstream(byte[] data, int offset, int length, VersatileBuffer outputPacket)

Parameters

data byte[]

The input data in Annex B format.

offset int

The offset within the input data.

length int

The number of bytes to convert.

outputPacket VersatileBuffer

The output buffer to write the AVC bitstream to.

DetectFrameType(VersatileBuffer, out bool, out bool)

Detects the frame type of an H.264 video frame in an Annex B bitstream.

public static void DetectFrameType(VersatileBuffer buffer, out bool isKeyframe, out bool isCleanPoint)

Parameters

buffer VersatileBuffer

The buffer containing the Annex B bitstream.

isKeyframe bool

When this method returns, indicates whether the frame is an IDR (keyframe).

isCleanPoint bool

When this method returns, indicates whether the frame is a clean point (I-slice or IDR).

FindNalUnit(byte[], int, int, NalUnitTypes, out int)

Finds a NAL unit of the specified type in an Annex B bitstream.

public static int FindNalUnit(byte[] buffer, int offset, int length, NalUnitTypes nalUnit, out int startCodeSize)

Parameters

buffer byte[]

The buffer to search.

offset int

The offset to start searching from.

length int

The number of bytes to search.

nalUnit NalUnitTypes

The NAL unit type to find.

startCodeSize int

The detected start code size of the found NAL unit.

Returns

int

The position of the found NAL unit, or -1 if not found.

FindNextStartCode(byte[], int, int, out int)

Finds next start code in Annex B bitstream

public static int FindNextStartCode(byte[] buffer, int offset, int length, out int startCodeSize)

Parameters

buffer byte[]

Buffer to search

offset int

Search offset position

length int

Length of the remaining(!) data size after the offset

startCodeSize int

Detected start code size

Returns

int

If found then start code position. Otherwise -1

FindNextStartCode(VersatileBuffer, int, int, out int)

Finds next start code in Annex B bitstream within a VersatileBuffer.

public static int FindNextStartCode(VersatileBuffer buffer, int offset, int length, out int startCodeSize)

Parameters

buffer VersatileBuffer

VersatileBuffer to search.

offset int

Search offset position.

length int

Length of the remaining data size after the offset.

startCodeSize int

Detected start code size (3 or 4 bytes).

Returns

int

If found, the start code position; otherwise, -1.

FindSpsPps(byte[], out byte[], out byte[])

Finds and extracts SPS and PPS NAL units from codec private data (AVC or Annex B format).

public static bool FindSpsPps(byte[] buffer, out byte[] sps, out byte[] pps)

Parameters

buffer byte[]

The buffer containing the codec private data.

sps byte[]

When this method returns, contains the SPS NAL unit data, or null if not found.

pps byte[]

When this method returns, contains the PPS NAL unit data, or null if not found.

Returns

bool

True if the buffer was parsed successfully; otherwise, false.

FindSpsPps(byte[], int, int, out byte[], out byte[])

Finds and extracts SPS and PPS NAL units from a portion of codec private data (AVC or Annex B format).

public static bool FindSpsPps(byte[] buffer, int offset, int length, out byte[] sps, out byte[] pps)

Parameters

buffer byte[]

The buffer containing the codec private data.

offset int

The offset within the buffer to start searching.

length int

The number of bytes to search.

sps byte[]

When this method returns, contains the SPS NAL unit data, or null if not found.

pps byte[]

When this method returns, contains the PPS NAL unit data, or null if not found.

Returns

bool

True if the buffer was parsed successfully; otherwise, false.

FindSpsPps(VersatileBuffer, out byte[], out byte[])

Finds and extracts SPS and PPS NAL units from codec private data in a VersatileBuffer (AVC or Annex B format).

public static bool FindSpsPps(VersatileBuffer buffer, out byte[] sps, out byte[] pps)

Parameters

buffer VersatileBuffer

The buffer containing the codec private data.

sps byte[]

When this method returns, contains the SPS NAL unit data, or null if not found.

pps byte[]

When this method returns, contains the PPS NAL unit data, or null if not found.

Returns

bool

True if the buffer was parsed successfully; otherwise, false.

FindVideoStart(VersatileBuffer, int, int, int, out int)

Finds start of actual video data depending on dropCode parameter.

public static int FindVideoStart(VersatileBuffer buffer, int offset, int length, int dropCode, out int startCodeSize)

Parameters

buffer VersatileBuffer

Frame bitstream

offset int

Offset in the input buffer

length int

Length of the input buffer

dropCode int

Drop code defines which NAL units will be dropped: 0 - AUD, SPS, PPS and SEI NALs are dropped 1 - AUD, SPS, and PPS are dropped 2 - only AUD is dropped

startCodeSize int

Start code size of the first video NAL

Returns

int

Position of actual video data

GenerateAnnexBCodecPrivateData(MediaType, byte[])

Generates Annex B format codec private data from an Annex B bitstream.

public static void GenerateAnnexBCodecPrivateData(MediaType mt, byte[] buffer)

Parameters

mt MediaType

The media type to populate with codec private data.

buffer byte[]

The buffer containing the Annex B bitstream with SPS and PPS.

GenerateAnnexBCodecPrivateData(MediaType, byte[], byte[])

Generates Annex B format codec private data from SPS and PPS NAL units.

public static void GenerateAnnexBCodecPrivateData(MediaType mt, byte[] sps, byte[] pps)

Parameters

mt MediaType

The media type to populate with codec private data.

sps byte[]

The SPS NAL unit data (including NAL unit header).

pps byte[]

The PPS NAL unit data (including NAL unit header).

GenerateCodecPrivateData(MediaType, byte[])

Generates AVCDecoderConfigurationRecord format codec private data from an Annex B bitstream.

public static void GenerateCodecPrivateData(MediaType mt, byte[] buffer)

Parameters

mt MediaType

The media type to populate with codec private data.

buffer byte[]

The buffer containing the Annex B bitstream with SPS and PPS.

GenerateCodecPrivateData(MediaType, byte[], byte[])

Generates AVCDecoderConfigurationRecord format codec private data from SPS and PPS NAL units.

public static void GenerateCodecPrivateData(MediaType mt, byte[] sps, byte[] pps)

Parameters

mt MediaType

The media type to populate with codec private data.

sps byte[]

The SPS NAL unit data (including NAL unit header).

pps byte[]

The PPS NAL unit data (including NAL unit header).

GenerateCodecPrivateData(MediaType, byte[], int, int)

Generates AVCDecoderConfigurationRecord format codec private data from a portion of an Annex B bitstream.

public static void GenerateCodecPrivateData(MediaType mt, byte[] buffer, int offset, int length)

Parameters

mt MediaType

The media type to populate with codec private data.

buffer byte[]

The buffer containing the Annex B bitstream with SPS and PPS.

offset int

The offset within the buffer to start searching.

length int

The number of bytes to search.

GenerateCodecPrivateData(MediaType, VersatileBuffer)

Generates AVCDecoderConfigurationRecord format codec private data from an Annex B bitstream in a VersatileBuffer.

public static void GenerateCodecPrivateData(MediaType mt, VersatileBuffer buffer)

Parameters

mt MediaType

The media type to populate with codec private data.

buffer VersatileBuffer

The buffer containing the Annex B bitstream with SPS and PPS.

HaveSpsPps(VersatileBuffer, int, int)

Checks whether an Annex B bitstream contains SPS or PPS NAL units.

public static bool HaveSpsPps(VersatileBuffer buffer, int offset, int length)

Parameters

buffer VersatileBuffer

The buffer containing the Annex B bitstream.

offset int

The offset within the buffer to start searching.

length int

The number of bytes to search.

Returns

bool

True if SPS or PPS NAL units are found; otherwise, false.

IsISlice(VersatileBuffer, int, int)

Determines whether the slice data at the specified position is an I-slice or SI-slice.

public static bool IsISlice(VersatileBuffer buffer, int offset, int length)

Parameters

buffer VersatileBuffer

The buffer containing slice data (starting after NAL unit header).

offset int

The offset within the buffer.

length int

The number of bytes available.

Returns

bool

True if the slice is an I-slice or SI-slice; otherwise, false.

Parse(byte[])

Parses H.264/AVC configuration data from a byte buffer. Automatically detects the format (Annex B, AVC decoder configuration record, or single NAL unit).

public int Parse(byte[] buffer)

Parameters

buffer byte[]

The buffer containing the configuration data.

Returns

int

1 on success, -1 on failure.

ParsePps(byte[], int, int)

Parses a Picture Parameter Set (PPS) NAL unit from the specified buffer.

public ConfigurationParser.Pps ParsePps(byte[] buffer, int offset, int length)

Parameters

buffer byte[]

The buffer containing the PPS data (without NAL unit header byte).

offset int

The offset within the buffer to start parsing.

length int

The number of bytes to parse.

Returns

ConfigurationParser.Pps

The parsed PPS object, or null if parsing failed.

ParseSps(byte[], int, int)

Parses a Sequence Parameter Set (SPS) NAL unit from the specified buffer.

public ConfigurationParser.Sps ParseSps(byte[] buffer, int offset, int length)

Parameters

buffer byte[]

The buffer containing the SPS data (without NAL unit header byte).

offset int

The offset within the buffer to start parsing.

length int

The number of bytes to parse.

Returns

ConfigurationParser.Sps

The parsed SPS object, or null if parsing failed.

PrintNalUnits(byte[], int, int)

Creates a diagnostic string listing all NAL units in an Annex B bitstream with their positions, types, and sizes.

public static string PrintNalUnits(byte[] buffer, int offset, int length)

Parameters

buffer byte[]

The buffer containing the Annex B bitstream.

offset int

The offset within the buffer to start searching.

length int

The number of bytes to search.

Returns

string

A comma-separated string describing each NAL unit, or an empty string if no NAL units were found.

RefineAnnexBBitstream(VersatileBuffer, VersatileBuffer, byte[])

Refines an Annex B bitstream by normalizing start codes to 4 bytes and optionally inserting parameter sets.

public static void RefineAnnexBBitstream(VersatileBuffer inputPacket, VersatileBuffer outputPacket, byte[] parameterSets = null)

Parameters

inputPacket VersatileBuffer

The input packet in Annex B format.

outputPacket VersatileBuffer

The output buffer to write the refined bitstream to.

parameterSets byte[]

Optional parameter sets in Annex B format to insert if not present in the input.

RemoveForbiddenSequenceEmulation(byte[], int, int)

Removes emulation prevention bytes (0x03) from RBSP data to restore the original SODB.

public static byte[] RemoveForbiddenSequenceEmulation(byte[] sourceBuffer, int offset, int length)

Parameters

sourceBuffer byte[]

The buffer containing the RBSP data with emulation prevention bytes.

offset int

The offset within the buffer to start processing.

length int

The number of bytes to process.

Returns

byte[]

A new byte array with emulation prevention bytes removed.

TryGetReorderingQueueDepth(out int)

Attempts to get the frame reordering queue depth.

public bool TryGetReorderingQueueDepth(out int reorderingQueueDepth)

Parameters

reorderingQueueDepth int

When this method returns, contains the reordering queue depth.

Returns

bool

True if the value was explicitly specified in the bitstream; false if it was calculated from level constraints.