Class ConfigurationParser
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
Bitrate
Gets the bitrate in bits per second from HRD parameters, or 0 if not available or inconsistent.
public int Bitrate { get; }
Property Value
FrameRate
Gets the frame rate as a rational number, derived from VUI timing information.
public Rational FrameRate { get; }
Property Value
Height
Gets the video height in pixels, calculated from SPS parameters with cropping applied.
public int Height { get; }
Property Value
Level
Gets the H.264 level indicator (level_idc) from the SPS.
public int Level { get; }
Property Value
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
Profile
Gets the H.264 profile indicator (profile_idc) from the SPS.
public int Profile { get; }
Property Value
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
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
Width
Gets the video width in pixels, calculated from SPS parameters with cropping applied.
public int Width { get; }
Property Value
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
mtMediaTypeThe 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
mtMediaTypeThe 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
inputPacketVersatileBufferThe input packet in AVC format.
outputPacketVersatileBufferThe output buffer to write the Annex B bitstream to.
nalUnitSizeintThe 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
inputPacketVersatileBufferInput packet in AVC bitstream format
outputPacketVersatileBufferOutput packet in Annex B bitstream format
nalUnitSizeintNAL unit size of the input bitstream
parameterSetsbyte[]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
databyte[]The input data in Annex B format.
offsetintThe offset within the input data.
lengthintThe number of bytes to convert.
outputPacketVersatileBufferThe 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
bufferVersatileBufferThe buffer containing the Annex B bitstream.
isKeyframeboolWhen this method returns, indicates whether the frame is an IDR (keyframe).
isCleanPointboolWhen 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
bufferbyte[]The buffer to search.
offsetintThe offset to start searching from.
lengthintThe number of bytes to search.
nalUnitNalUnitTypesThe NAL unit type to find.
startCodeSizeintThe 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
bufferbyte[]Buffer to search
offsetintSearch offset position
lengthintLength of the remaining(!) data size after the offset
startCodeSizeintDetected 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
bufferVersatileBufferVersatileBuffer to search.
offsetintSearch offset position.
lengthintLength of the remaining data size after the offset.
startCodeSizeintDetected 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
bufferbyte[]The buffer containing the codec private data.
spsbyte[]When this method returns, contains the SPS NAL unit data, or null if not found.
ppsbyte[]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
bufferbyte[]The buffer containing the codec private data.
offsetintThe offset within the buffer to start searching.
lengthintThe number of bytes to search.
spsbyte[]When this method returns, contains the SPS NAL unit data, or null if not found.
ppsbyte[]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
bufferVersatileBufferThe buffer containing the codec private data.
spsbyte[]When this method returns, contains the SPS NAL unit data, or null if not found.
ppsbyte[]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
bufferVersatileBufferFrame bitstream
offsetintOffset in the input buffer
lengthintLength of the input buffer
dropCodeintDrop 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
startCodeSizeintStart 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
mtMediaTypeThe media type to populate with codec private data.
bufferbyte[]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
mtMediaTypeThe media type to populate with codec private data.
spsbyte[]The SPS NAL unit data (including NAL unit header).
ppsbyte[]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
mtMediaTypeThe media type to populate with codec private data.
bufferbyte[]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
mtMediaTypeThe media type to populate with codec private data.
spsbyte[]The SPS NAL unit data (including NAL unit header).
ppsbyte[]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
mtMediaTypeThe media type to populate with codec private data.
bufferbyte[]The buffer containing the Annex B bitstream with SPS and PPS.
offsetintThe offset within the buffer to start searching.
lengthintThe 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
mtMediaTypeThe media type to populate with codec private data.
bufferVersatileBufferThe 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
bufferVersatileBufferThe buffer containing the Annex B bitstream.
offsetintThe offset within the buffer to start searching.
lengthintThe 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
bufferVersatileBufferThe buffer containing slice data (starting after NAL unit header).
offsetintThe offset within the buffer.
lengthintThe 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
bufferbyte[]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
bufferbyte[]The buffer containing the PPS data (without NAL unit header byte).
offsetintThe offset within the buffer to start parsing.
lengthintThe 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
bufferbyte[]The buffer containing the SPS data (without NAL unit header byte).
offsetintThe offset within the buffer to start parsing.
lengthintThe 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
bufferbyte[]The buffer containing the Annex B bitstream.
offsetintThe offset within the buffer to start searching.
lengthintThe 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
inputPacketVersatileBufferThe input packet in Annex B format.
outputPacketVersatileBufferThe output buffer to write the refined bitstream to.
parameterSetsbyte[]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
sourceBufferbyte[]The buffer containing the RBSP data with emulation prevention bytes.
offsetintThe offset within the buffer to start processing.
lengthintThe 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
reorderingQueueDepthintWhen 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.