Class CodecContext
Represents a codec context used for encoding or decoding video and audio streams using FFmpeg.
This class wraps the FFmpeg.AutoGen._AVCodecContext structure from the FFmpeg library, providing an interface for configuring and interacting with codec parameters for both encoding and decoding operations.
Implements
Inherited Members
Namespace: FFmpeg.Codecs
Assembly: FFmpeg.dll
Syntax
public sealed class CodecContext : OptionQueryableBase, ILoggingContext, IAudioDecoder, IAudioEncoder, IVideoDecoder, IDecoderContext, IVideoEncoder, IEncoderContext, IDisposable, IOptionQueryable, IAVPointer<_AVCodecContext>
Properties
View SourceAlphaMode
Gets or sets how the alpha channel is represented in relation to the color components.
Declaration
public AlphaMode AlphaMode { get; set; }
Property Value
| Type | Description |
|---|---|
| AlphaMode |
Remarks
Premultiplied indicates that the color components are already multiplied by the alpha value, while Straight indicates that the alpha channel is stored independently of the color components.
AudioServiceType
Gets or sets the audio service type, which indicates the intended usage of the audio stream (e.g., main audio, commentary, or karaoke).
Encoding: Notify by the user to specify the type of audio service for encoding.
Decoding: This field is set by libavcodec during decoding and cannot be set by the user.
Declaration
public AudioServiceType AudioServiceType { get; set; }
Property Value
| Type | Description |
|---|---|
| AudioServiceType |
BitRate
Gets or sets the target bit rate in bits per second, which influences the compression level. The bit rate directly affects the quality and size of the encoded media.
Encoding: Notify by the user. Not used in constant quantizer encoding.
Decoding: Notify by the user. May be overwritten by libavcodec if this information is available in the stream.
Declaration
public long BitRate { get; set; }
Property Value
| Type | Description |
|---|---|
| long |
BitRateTolerance
Gets or sets the tolerance for bit rate deviations during encoding.
Encoding: Defines how much the bitstream is allowed to deviate from the target bit rate. This applies to both constant bit rate (CBR) and variable bit rate (VBR) encoding.
Decoding: This field is not used during decoding.
Declaration
public int BitRateTolerance { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
ChannelLayout
Gets or sets the channel layout for the audio stream, such as stereo or surround sound.
Encoding: Must be set by the user to specify the desired channel layout for encoding.
Decoding: May be set by the user if known, but the decoder may override it based on the actual stream data.
Declaration
public ChannelLayout_ref ChannelLayout { get; }
Property Value
| Type | Description |
|---|---|
| ChannelLayout_ref |
ChromaSampleLocation
Gets or sets the chroma sample location, which defines the position of the chroma samples relative to the luma samples in the video.
Encoding: Must be set by the user to specify the chroma sample location for encoding.
Decoding: Notify by libavcodec during decoding to correctly align chroma samples.
Declaration
public ChromaLocation ChromaSampleLocation { get; set; }
Property Value
| Type | Description |
|---|---|
| ChromaLocation |
Codec
Gets the codec associated with the context. Represents the specific codec being used for encoding or decoding.
Declaration
public Codec Codec { get; }
Property Value
| Type | Description |
|---|---|
| Codec |
CodecID
Gets or sets the codec ID, which represents the specific codec used. This identifies the codec being used, such as H.264 or AAC.
Declaration
public CodecID CodecID { get; }
Property Value
| Type | Description |
|---|---|
| CodecID |
CodecTag
Gets or sets the codec tag, an additional codec identifier used to work around some encoder bugs. The codec tag is a 32-bit identifier used to map codec information, especially when containers store this data in formats that exceed 32 bits. It is commonly represented as a FourCC code.
Encoding: Notify by the user; if not set, a default based on CodecID will be used.
Decoding: Notify by the user; during initialization, libavcodec will convert the value to uppercase.
Declaration
public FourCC CodecTag { get; set; }
Property Value
| Type | Description |
|---|---|
| FourCC |
CodecType
Gets or sets the general type of the codec (e.g., video, audio). This property indicates whether the codec is handling audio, video, or other media types.
Declaration
public MediaType CodecType { get; }
Property Value
| Type | Description |
|---|---|
| MediaType |
CodedHeight
Gets or sets the coded height of the video frame, which may be larger than the actual display height. This value represents the height of the frame in the bitstream, which may differ from the decoded output height due to cropping or other optimizations.
Encoding: Unused.
Decoding: May be set by the user before opening the decoder if known. The decoder may overwrite this value during parsing.
Declaration
public int CodedHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
CodedWidth
Gets or sets the coded width of the video frame, which may be larger than the actual display width. This value represents the width of the frame in the bitstream, which may differ from the decoded output width due to cropping or other optimizations.
Encoding: Unused.
Decoding: May be set by the user before opening the decoder if known. The decoder may overwrite this value during parsing.
Declaration
public int CodedWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
ColorPrimaries
Gets or sets the chromaticity coordinates of the source primaries, which define the red, green, and blue components of the video.
Encoding: Must be set by the user to define the chromaticity coordinates for encoding.
Decoding: Notify by libavcodec during decoding to interpret the color information correctly.
Declaration
public ColorPrimaries ColorPrimaries { get; set; }
Property Value
| Type | Description |
|---|---|
| ColorPrimaries |
ColorRange
Gets or sets the color range of the video, which defines the range of values for the color components (MPEG vs JPEG YUV range).
Encoding: Can be set by the user to override the default output color range. If not specified, libavcodec sets the color range based on the output format.
Decoding: Notify by libavcodec, but may be set by the user to propagate the color range to other components reading from the decoder context.
Declaration
public ColorRange ColorRange { get; set; }
Property Value
| Type | Description |
|---|---|
| ColorRange |
ColorSpace
Gets or sets the YUV colorspace type of the video, which defines how the color components are organized.
Encoding: Must be set by the user to specify the YUV colorspace for encoding.
Decoding: Notify by libavcodec during decoding to interpret the colorspace of the video stream.
Declaration
public ColorSpace ColorSpace { get; set; }
Property Value
| Type | Description |
|---|---|
| ColorSpace |
ColorTransferCharacteristic
Gets or sets the color transfer characteristic, which is used for gamma correction and defines how colors are transformed.
Encoding: Must be set by the user to specify the color transfer characteristics for encoding.
Decoding: Notify by libavcodec during decoding to handle color transformation properly.
Declaration
public ColorTransferCharacteristic ColorTransferCharacteristic { get; set; }
Property Value
| Type | Description |
|---|---|
| ColorTransferCharacteristic |
CompressionLevel
Gets or sets the compression level for encoding, affecting the tradeoff between speed and quality.
Encoding: Notify by the user to control the level of compression. Higher values may result in slower encoding but potentially better quality.
Decoding: This field is not used during decoding.
Declaration
public int CompressionLevel { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Delay
Gets the delay in frames between the input to the encoder and the output of the decoder. For video, this represents the number of frames delayed between the encoded input and the decoded output. For audio decoding, this is the number of samples that the decoder needs to output before its output is valid.
Encoding: Notify by libavcodec.
Decoding: Notify by libavcodec.
Declaration
public int Delay { get; }
Property Value
| Type | Description |
|---|---|
| int |
ExtraData
Gets or sets the extra data, typically codec-specific initialization data required for decoding or encoding. Certain codecs use extradata to store auxiliary information, such as Huffman tables for MJPEG, additional flags for RV10, or global headers for MPEG-4.
Encoding: The data is set, allocated, and freed by libavcodec.
Decoding: The data must be set, allocated, and freed by the user. The allocated memory should be AV_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual extradata to prevent buffer overflows when reading the bitstream.
Declaration
public ReadOnlySpan<byte> ExtraData { get; set; }
Property Value
| Type | Description |
|---|---|
| ReadOnlySpan<byte> |
FieldOrder
Gets or sets the field order for interlaced video, which defines whether the top or bottom field is displayed first.
Encoding: Notify by libavcodec during encoding to ensure proper field order in the output stream.
Decoding: Notify by the user if known, to correctly interpret the field order of the input stream.
Declaration
public FieldOrder FieldOrder { get; set; }
Property Value
| Type | Description |
|---|---|
| FieldOrder |
Flags
Gets or sets codec flags that determine various encoding or decoding settings.
The flags are split between general codec flags (AV_CODEC_FLAG_) and extended flags (AV_CODEC_FLAG2_).
Encoding: Notify by the user.
Decoding: Notify by the user.
Declaration
public CodecFlags Flags { get; set; }
Property Value
| Type | Description |
|---|---|
| CodecFlags |
FrameRate
Gets or sets the frame rate of the video stream, in frames per second.
Encoding: Used to signal the constant frame rate (CFR) content to the encoder.
Decoding: May be set by the decoder for codecs that store a frame rate value in the bitstream. If unknown, this value will be { 0, 1 }.
Declaration
public Rational FrameRate { get; set; }
Property Value
| Type | Description |
|---|---|
| Rational |
FrameSize
Gets or sets the frame size for audio encoding/decoding, which represents the number of samples per channel in each frame.
Encoding: Notify by libavcodec during encoding to define the number of samples per frame. Each frame submitted must have exactly this number of samples, except the last frame.
Decoding: Some decoders may set this field to indicate a constant frame size.
Declaration
public int FrameSize { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
GOPSize
Gets or sets the number of frames in a Group of Pictures (GOP).
Encoding: Notify by the user to specify the number of frames in each GOP. If set to 0, the encoding will be intra-only (I-frames only).
Decoding: This field is unused.
Declaration
public int GOPSize { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
GlobalQuality
Gets or sets the global quality factor for codecs that do not adjust quality on a per-frame basis.
Encoding: This value is used to set the global quality for encoding, which is proportional to the MPEG quantization scale (qscale).
Decoding: This field is not used during decoding.
Declaration
public int GlobalQuality { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
HardwareDeviceContext
Gets the hardware device context pointer, which represents the device context used for hardware acceleration.
Encoding: Not used.
Decoding: Access this property to interact with the hardware device context when using hardware acceleration.
Declaration
public DeviceContext_ref HardwareDeviceContext { get; }
Property Value
| Type | Description |
|---|---|
| DeviceContext_ref |
Height
Gets or sets the height of the video frame in pixels. This field must be set by the user for encoding and may be set for decoding if known.
Encoding: Must be set by the user.
Decoding: May be set by the user before opening the decoder if known, e.g., from the container. The decoder may overwrite this value during parsing.
Declaration
public int Height { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
IsOpen
Gets a value indicating whether the codec context is open.
Declaration
public bool IsOpen { get; }
Property Value
| Type | Description |
|---|---|
| bool |
MinGOPSize
Gets or sets the minimum size of the Group of Pictures (GOP).
Encoding: Notify by the user to specify the minimum number of frames between keyframes (i.e., I-frames).
Decoding: This field is unused.
Declaration
public int MinGOPSize { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
PacketTimeBase
Gets or sets the packet time base, which is used for expressing packet timestamps (pkt_dts/pts) and AVPacket.dts/pts. This defines the time base in which the packet timestamps are calculated.
Encoding: Unused.
Decoding: Notify by the user.
Declaration
public Rational PacketTimeBase { get; set; }
Property Value
| Type | Description |
|---|---|
| Rational |
Pass1StatsOutput
Gets the pass1 encoding statistics output buffer as a read-only span of bytes.
This buffer contains encoding statistics collected during the first pass of encoding.
This property is set by libavcodec and cannot be modified by the user.
Declaration
public ReadOnlySpan<byte> Pass1StatsOutput { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlySpan<byte> |
Pass2StatsInput
Gets or sets the pass2 encoding statistics input buffer as a read-only span of bytes.
This buffer should contain the concatenated statistics from the pass1 output, used for the second pass of encoding.
This property can be set by the user for pass2 encoding. If the buffer is being replaced, the old buffer is freed.
Declaration
public ReadOnlySpan<byte> Pass2StatsInput { get; set; }
Property Value
| Type | Description |
|---|---|
| ReadOnlySpan<byte> |
PixelFormat
Gets or sets the pixel format of the video, which defines the format of the pixels (e.g., YUV, RGB).
Encoding: Must be set by the user to define the pixel format for encoding.
Decoding: May be set by the user if known from the headers. However, it can be overridden by the decoder during parsing.
Declaration
public PixelFormat PixelFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| PixelFormat |
Pointer
Gets the pointer to the underlying FFmpeg.AutoGen._AVCodecContext structure.
Declaration
protected override void* Pointer { get; }
Property Value
| Type | Description |
|---|---|
| void* |
Overrides
View SourceProfile
Gets or sets the codec profile, which defines the level of compression and quality for the codec.
Encoding: Notify by the user to specify the desired codec profile (e.g., main, high, baseline for H.264).
Decoding: Notify by libavcodec during decoding to reflect the profile used in the encoded stream.
Declaration
public Profile Profile { get; set; }
Property Value
| Type | Description |
|---|---|
| Profile |
ReferenceFrames
Gets or sets the number of reference frames used for inter-frame compression.
Encoding: Must be set by the user to specify the number of reference frames for encoding.
Decoding: Notify by libavcodec during decoding to manage reference frame usage.
Declaration
public int ReferenceFrames { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
SampleAspectRatio
Gets or sets the sample aspect ratio, which represents the width of a pixel divided by its height. A sample aspect ratio of 0 means the value is unknown. For some video standards, the numerator and denominator must be relatively prime and less than 256.
Encoding: Must be set by the user to specify the sample aspect ratio for encoding.
Decoding: Notify by libavcodec to provide the sample aspect ratio for decoding.
Declaration
public Rational SampleAspectRatio { get; set; }
Property Value
| Type | Description |
|---|---|
| Rational |
SampleFormat
Gets or sets the audio sample format, which defines how audio samples are stored (e.g., signed 16-bit, float).
Encoding: Must be set by the user to define the sample format for the audio stream being encoded.
Decoding: Notify by libavcodec based on the audio stream format during decoding.
Declaration
public SampleFormat SampleFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| SampleFormat |
SampleRate
Gets or sets the audio sample rate in samples per second (Hz).
Encoding: Must be set by the user to specify the sample rate of the audio stream being encoded.
Decoding: May be set by the user if known, but could be overridden by libavcodec based on the audio stream format.
Declaration
public int SampleRate { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
SoftwarePixelFormat
Gets the software pixel format, which represents the nominal unaccelerated pixel format used during decoding.
Encoding: Unused.
Decoding: Notify by libavcodec before calling the format selection function (get_format).
Declaration
public PixelFormat SoftwarePixelFormat { get; }
Property Value
| Type | Description |
|---|---|
| PixelFormat |
ThreadCount
Gets or sets the number of threads used for encoding or decoding operations.
Encoding: Notify by the user to determine the number of threads for parallel encoding tasks.
Decoding: Notify by the user to determine the number of threads for parallel decoding tasks.
Declaration
public int ThreadCount { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
TimeBase
Gets or sets the time base, representing the unit of time in terms of frame duration. This is the fundamental unit used to express timestamps in the media stream (e.g., 1/25 for 25 fps).
Encoding: The time base is unused.
Decoding: Notify by the user to define the base time unit for timestamps.
Declaration
public Rational TimeBase { get; set; }
Property Value
| Type | Description |
|---|---|
| Rational |
Width
Gets or sets the width of the video frame in pixels. This field must be set by the user for encoding and may be set for decoding if known.
Encoding: Must be set by the user.
Decoding: May be set by the user before opening the decoder if known, e.g., from the container. The decoder may overwrite this value during parsing.
Declaration
public int Width { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
View SourceAllocate(Codec?)
Allocates a new CodecContext instance for the specified codec.
Declaration
public static CodecContext Allocate(Codec? codec)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec for which to allocate the context. If null, a context is allocated without a specific codec. |
Returns
| Type | Description |
|---|---|
| CodecContext | A new CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if the codec context cannot be allocated due to insufficient memory. |
CopyParameters(CodecContext)
Copies codec parameters from the current codec context to another CodecContext.
Declaration
public void CopyParameters(CodecContext dst)
Parameters
| Type | Name | Description |
|---|---|---|
| CodecContext | dst | The destination CodecContext to which the codec parameters will be copied. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if there is insufficient memory to create the temporary ICodecParameters object. |
| FFmpegException | Thrown if the FFmpeg function to copy parameters returns an error. |
Decode(AVPacket?, AVFrame)
Decodes an encoded packet and returns the next available decoded frame.
Declaration
public AVResult32 Decode(AVPacket? packet, AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVPacket | packet | The packet to submit to the decoder, or null to signal the end of the input stream and enter draining mode. |
| AVFrame | frame | Receives the decoded frame if one is available. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
Remarks
This convenience method combines SendPacket(AVPacket?) and ReceiveFrame(AVFrame) into a single operation.
Passing null for packet flushes the
decoder. Continue calling this method with null until
EndOfFile is returned, indicating that all buffered
frames have been retrieved.
Under normal circumstances this method never returns TryAgain. If the decoder reports this condition while accepting a packet, an FFmpegException is thrown instead. Applications that need to handle TryAgain explicitly should use SendPacket(AVPacket?) and ReceiveFrame(AVFrame) directly.
DecodeSubtitle(AVPacket, Subtitle)
Declaration
public AVResult32 DecodeSubtitle(AVPacket packet, Subtitle subtitle)
Parameters
| Type | Name | Description |
|---|---|---|
| AVPacket | packet | The AVPacket containing the encoded subtitle data. |
| Subtitle | subtitle | The decoded subtitle output as a Subtitle. |
Returns
| Type | Description |
|---|---|
| AVResult32 | The result of the decoding operation as AVResult32. This result indicates whether the operation was successful or if an error occurred. |
Remarks
This method is specific to subtitle codecs. If the codec type is not set to Subtitle, the operation will throw a NotSupportedException.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if the codec is not of type subtitle. |
Dispose()
Declaration
public void Dispose()
DrainDecoder()
Enters draining mode for the decoder at the end of a stream, ensuring that any internally buffered frames are processed. This is required when no more input packets are available for decoding, but the codec may still have frames to output.
Declaration
public AVResult32 DrainDecoder()
Returns
| Type | Description |
|---|---|
| AVResult32 |
Remarks
The draining process for decoding works as follows:
- Instead of passing a valid packet, call SendPacket(AVPacket?) with a null packet, which enters draining mode.
- Then, call ReceiveFrame(AVFrame) repeatedly to retrieve any remaining decoded frames until
AVERROR_EOFis returned, indicating that all buffered frames have been processed. - Before decoding can be resumed, call FlushBuffers() to clear the codec's internal buffers and reset its state.
During draining mode, ReceiveFrame(AVFrame) will not return AVERROR(EAGAIN) unless the draining mode was not properly initiated.
DrainEncoder()
Enters draining mode for the encoder at the end of a stream, ensuring that any internally buffered packets are processed. This is required when no more input frames are available for encoding, but the codec may still have frames to output.
Declaration
public AVResult32 DrainEncoder()
Returns
| Type | Description |
|---|---|
| AVResult32 |
Remarks
The draining process for encoding works as follows:
- Instead of passing a valid frame, call SendFrame(AVFrame?) with a null frame, which enters draining mode.
- Then, call ReceivePacket(AVPacket) repeatedly to retrieve any remaining encoded packets until
AVERROR_EOFis returned, indicating that all buffered packets have been processed. - Before encoding can be resumed, call FlushBuffers() to clear the codec's internal buffers and reset its state.
During draining mode, ReceivePacket(AVPacket) will not return AVERROR(EAGAIN) unless the draining mode was not properly initiated.
Encode(AVFrame?, AVPacket)
Encodes a frame and returns the next available encoded packet.
Declaration
public AVResult32 Encode(AVFrame? frame, AVPacket packet)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The frame to submit to the encoder, or null to signal the end of the input stream and enter draining mode. |
| AVPacket | packet | Receives the encoded packet if one is available. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
Remarks
This convenience method combines SendFrame(AVFrame?) and ReceivePacket(AVPacket) into a single operation.
Passing null for frame flushes the
encoder. Continue calling this method with null until
EndOfFile is returned, indicating that all buffered
packets have been retrieved.
Under normal circumstances this method never returns TryAgain. If the encoder reports this condition while accepting a frame, an FFmpegException is thrown instead. Applications that need to handle TryAgain explicitly should use SendFrame(AVFrame?) and ReceivePacket(AVPacket) directly.
EncodeSubtitle(AVPacket, Subtitle)
Encodes a subtitle into the provided AVPacket and returns the result.
Declaration
public AVResult32 EncodeSubtitle(AVPacket packet, Subtitle subtitle)
Parameters
| Type | Name | Description |
|---|---|---|
| AVPacket | packet | The AVPacket that will contain the encoded subtitle data. This method will modify and return a fresh packet with the encoded data. |
| Subtitle | subtitle | The Subtitle to encode into the packet. |
Returns
| Type | Description |
|---|---|
| AVResult32 | The result of the encoding operation as AVResult32. A successful result indicates that the subtitle was encoded into the packet successfully. |
Remarks
After encoding the subtitle, the returned AVPacket will be freshly created. Important settings, such as StreamIndex, must be set accordingly after the encoding process completes. The AVPacket will have a maximum size of 1MB (1024 * 1024 bytes).
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if the codec is not of type subtitle. |
~CodecContext()
Finalizer for CodecContext to ensure resources are released.
This finalizer calls the FFmpeg.Codecs.CodecContext.Dispose(System.Boolean) method with disposing set to false.
It is called by the garbage collector if the object is not explicitly disposed.
Declaration
protected ~CodecContext()
FlushBuffers()
Resets the internal codec state and flushes internal buffers.
This method should be called, for example, when seeking or when switching to a different stream. It clears any cached data and prepares the codec for new data.
Declaration
public void FlushBuffers()
GetCodecParameters()
Retrieves the codec parameters from the current codec context.
Declaration
public CodecParameters GetCodecParameters()
Returns
| Type | Description |
|---|---|
| CodecParameters | An ICodecParameters object containing the codec parameters retrieved from the context. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if there is insufficient memory to allocate the ICodecParameters object. |
| FFmpegException | Thrown if the FFmpeg function to retrieve parameters returns an error. |
Open(Codec)
Opens a codec and initializes a CodecContext for it.
Declaration
public static CodecContext Open(Codec codec)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec. |
Open(Codec, ICodecParameters?)
Opens a codec, sets codec parameters, and initializes a CodecContext for it.
Declaration
public static CodecContext Open(Codec codec, ICodecParameters? codecParams)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| ICodecParameters | codecParams | The codec parameters to set. Can be null. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec or setting parameters. |
Open(Codec, ICodecParameters?, DeviceType)
Opens a codec, sets codec parameters, and initializes a CodecContext for it with hardware acceleration support.
Declaration
public static CodecContext Open(Codec codec, ICodecParameters? codecParams, DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| ICodecParameters | codecParams | The codec parameters to set. Can be null. |
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec, setting parameters, or configuring hardware acceleration. |
Open(Codec, AVDictionary?)
Opens a codec and initializes a CodecContext for it with additional options from a dictionary.
Declaration
public static CodecContext Open(Codec codec, AVDictionary? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| AVDictionary | dictionary | A dictionary of additional codec options. Can be null. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec. |
Open(Codec, AVDictionary?, DeviceType)
Opens a codec, sets hardware device type, and initializes a CodecContext for it with additional options from a dictionary.
Declaration
public static CodecContext Open(Codec codec, AVDictionary? dictionary, DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| AVDictionary | dictionary | A dictionary of additional codec options. Can be null. |
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec or setting hardware device type. |
Open(Codec, AVMultiDictionary?)
Opens a codec and initializes a CodecContext for it with additional options from a multi-dictionary.
Declaration
public static CodecContext Open(Codec codec, AVMultiDictionary? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| AVMultiDictionary | dictionary | A multi-dictionary of additional codec options. Can be null. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec. |
Open(Codec, AVMultiDictionary?, DeviceType)
Opens a codec, sets hardware device type, and initializes a CodecContext for it with additional options from a multi-dictionary.
Declaration
public static CodecContext Open(Codec codec, AVMultiDictionary? dictionary, DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| AVMultiDictionary | dictionary | A multi-dictionary of additional codec options. Can be null. |
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec or setting hardware device type. |
Open(Codec, DeviceType)
Opens a codec and initializes a CodecContext for it with hardware acceleration support.
Declaration
public static CodecContext Open(Codec codec, DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec or setting hardware device type. |
Open(Codec, IDictionary<string, string>?)
Opens a codec and initializes a CodecContext for it with additional options from a dictionary of string key-value pairs.
Declaration
public static CodecContext Open(Codec codec, IDictionary<string, string>? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| IDictionary<string, string> | dictionary | A dictionary of additional codec options. Can be null. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec. |
Open(Codec, IDictionary<string, string>?, DeviceType)
Opens a codec, sets hardware device type, and initializes a CodecContext for it with additional options from a dictionary of string key-value pairs.
Declaration
public static CodecContext Open(Codec codec, IDictionary<string, string>? dictionary, DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec | codec | The codec to open. |
| IDictionary<string, string> | dictionary | A dictionary of additional codec options. Can be null. |
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| CodecContext | An open CodecContext instance. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an error occurs while opening the codec or setting hardware device type. |
Open(Codec?)
Opens the codec context with the specified codec.
Declaration
public AVResult32 Open(Codec? codec)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec to be used to open the codec context. If null, the context is opened with the currently set codec. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
Open(Codec?, ICodecParameters?)
Opens the codec context with the specified codec and codec parameters.
Declaration
public AVResult32 Open(Codec? codec, ICodecParameters? codecParameters)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec to be used to open the codec context. If null, the context is opened with the currently set codec. |
| ICodecParameters | codecParameters | Optional ICodecParameters to set before opening the codec. If null, only the codec is used. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
Open(Codec?, AVDictionary?)
Opens the codec context with the specified codec and optional dictionary of codec options.
Declaration
public AVResult32 Open(Codec? codec, AVDictionary? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec to be used to open the codec context. If null, the context is opened with the currently set codec. |
| AVDictionary | dictionary | An optional AVDictionary containing codec-specific options. If null, defaults are used. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
Open(Codec?, AVMultiDictionary?)
Opens the codec context with the specified codec and optional multi-dictionary of codec options.
Declaration
public AVResult32 Open(Codec? codec, AVMultiDictionary? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec to be used to open the codec context. If null, the context is opened with the currently set codec. |
| AVMultiDictionary | dictionary | An optional AVMultiDictionary containing codec-specific options. If null, defaults are used. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
Open(Codec?, IDictionary<string, string>?)
Opens the codec context with the specified codec and an IDictionary<TKey, TValue> of codec options.
Declaration
public AVResult32 Open(Codec? codec, IDictionary<string, string>? dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Codec? | codec | The codec to be used to open the codec context. If null, the context is opened with the currently set codec. |
| IDictionary<string, string> | dictionary | An optional IDictionary<TKey, TValue> containing codec-specific options. If null, defaults are used. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
OpenEncoder(AVStream)
Creates and opens an encoder configured from the specified stream.
Declaration
public static CodecContext OpenEncoder(AVStream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| AVStream | stream | The stream whose codec identifier, codec parameters, time base, and frame rate are used to configure the encoder. |
Returns
| Type | Description |
|---|---|
| CodecContext | An opened CodecContext configured for encoding the specified stream. |
Exceptions
| Type | Condition |
|---|---|
| FFmpegException | Thrown if the encoder cannot be found, allocated, configured, or opened. |
ReceiveFrame(AVFrame)
Receives a frame from the codec and transfers it to the provided AVFrame.
If the codec context uses hardware acceleration, the frame will be transferred from hardware memory (GPU) to system memory (RAM).
Declaration
public AVResult32 ReceiveFrame(AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The AVFrame to receive from the codec. This frame will be populated with the decoded data. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
ReceiveHWFrame(AVFrame)
Receives a hardware-accelerated frame from the codec.
If the frame is in RAM, this method behaves the same as ReceiveFrame(AVFrame). If the frame is in GPU memory, this method will return the frame without transferring it to RAM first.
Declaration
public AVResult32 ReceiveHWFrame(AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The AVFrame to receive from the codec. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
ReceivePacket(AVPacket)
Receives a packet from the codec after encoding or decoding.
Declaration
public AVResult32 ReceivePacket(AVPacket packet)
Parameters
| Type | Name | Description |
|---|---|---|
| AVPacket | packet | The AVPacket to receive from the codec. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
SendFrame(AVFrame?)
Sends a frame to the codec for encoding or decoding.
Declaration
public AVResult32 SendFrame(AVFrame? frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The AVFrame to send to the codec. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
SendPacket(AVPacket?)
Sends a packet to the codec for encoding or decoding.
Declaration
public AVResult32 SendPacket(AVPacket? packet)
Parameters
| Type | Name | Description |
|---|---|---|
| AVPacket | packet | The AVPacket to send to the codec. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. Check for errors using |
SetCodecParameters(ICodecParameters)
Sets the codec parameters for the current codec context using the specified parameters.
Declaration
public void SetCodecParameters(ICodecParameters parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| ICodecParameters | parameters | An ICodecParameters object containing the codec parameters to be set in the context. |
Exceptions
| Type | Condition |
|---|---|
| FFmpegException | Thrown if the FFmpeg function to set parameters returns an error. |
SetHWDeviceType(DeviceType)
Sets the hardware device type for the codec context.
This method configures the hardware acceleration device type to be used by the codec context. It must be set before opening the codec. If the codec context is already open, an exception will be thrown.
Declaration
public AVResult32 SetHWDeviceType(DeviceType deviceType)
Parameters
| Type | Name | Description |
|---|---|---|
| DeviceType | deviceType | The type of hardware device to use for acceleration. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating the result of the operation. |
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if the codec context is already open when attempting to set the hardware device type. |
ToString()
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string |