Class SwrContext
Represents a managed wrapper for the FFmpeg SwrContext, used for audio resampling, format conversion, and channel layout remapping.
Inherited Members
Namespace: FFmpeg.Audio
Assembly: FFmpeg.dll
Syntax
public class SwrContext : OptionQueryableBase, IOptionQueryable, ILoggingContext, IDisposable, IAVPointer<_SwrContext>
Constructors
View SourceSwrContext()
Initializes a new empty SwrContext instance. Configuration should be done manually via property setters or Config(AVFrame, AVFrame).
Declaration
public SwrContext()
SwrContext(IChannelLayout, SampleFormat, int, IChannelLayout, SampleFormat, int)
Initializes a new instance of the SwrContext class with full configuration parameters. Automatically initializes the underlying SwrContext.
Declaration
public SwrContext(IChannelLayout srcLayout, SampleFormat srcFormat, int srcSampleRate, IChannelLayout dstLayout, SampleFormat dstFormat, int dstSampleRate)
Parameters
| Type | Name | Description |
|---|---|---|
| IChannelLayout | srcLayout | Source channel layout. |
| SampleFormat | srcFormat | Source sample format. |
| int | srcSampleRate | Source sample rate. |
| IChannelLayout | dstLayout | Destination channel layout. |
| SampleFormat | dstFormat | Destination sample format. |
| int | dstSampleRate | Destination sample rate. |
SwrContext(AVFrame, AVFrame)
Initializes a new SwrContext based on two AVFrame formats.
Declaration
public SwrContext(AVFrame srcFormat, AVFrame dstFormat)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | srcFormat | The source AVFrame for format detection. |
| AVFrame | dstFormat | The destination AVFrame for format detection. |
Properties
View SourceBytesPerSampleDestination
Gets the number of bytes per audio sample for the current destination format.
Declaration
public int BytesPerSampleDestination { get; }
Property Value
| Type | Description |
|---|---|
| int |
BytesPerSampleSource
Gets the number of bytes per audio sample for the current source format.
Declaration
public int BytesPerSampleSource { get; }
Property Value
| Type | Description |
|---|---|
| int |
DestinationFormat
Gets or sets the destination sample format. Setting this value will reset the internal SwrContext state.
Declaration
public SampleFormat DestinationFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| SampleFormat |
DestinationLayout
Gets or sets the destination channel layout. Setting this value will reset the internal SwrContext state.
Declaration
public ChannelLayout DestinationLayout { get; set; }
Property Value
| Type | Description |
|---|---|
| ChannelLayout |
DestinationSampleRate
Gets or sets the destination sample rate. Setting this value will reset the internal SwrContext state.
Declaration
public int DestinationSampleRate { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
IsInitialized
Gets a value indicating whether the SwrContext is initialized and ready for conversion.
Declaration
public bool IsInitialized { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Pointer
Gets the native pointer to the internal SwrContext structure.
Declaration
protected override void* Pointer { get; }
Property Value
| Type | Description |
|---|---|
| void* |
Overrides
View SourceSourceFormat
Gets or sets the source sample format (e.g., AV_SAMPLE_FMT_FLTP). Setting this value will reset the internal SwrContext state.
Declaration
public SampleFormat SourceFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| SampleFormat |
SourceLayout
Gets or sets the source channel layout (e.g., stereo, mono). Setting this value will reset the internal SwrContext state.
Declaration
public ChannelLayout SourceLayout { get; set; }
Property Value
| Type | Description |
|---|---|
| ChannelLayout |
SourceSampleRate
Gets or sets the source sample rate (e.g., 44100 Hz). Setting this value will reset the internal SwrContext state.
Declaration
public int SourceSampleRate { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
View SourceBytesPerSample(SampleFormat)
Gets the number of bytes per sample for a specified audio sample format.
Declaration
public static int BytesPerSample(SampleFormat format)
Parameters
| Type | Name | Description |
|---|---|---|
| SampleFormat | format | The sample format to query. |
Returns
| Type | Description |
|---|---|
| int | The number of bytes per sample. |
Clear()
Drops all output samples currently stored in the internal SwrContext.
Declaration
public void Clear()
Remarks
This method clears any buffered output samples that have not yet been retrieved or converted, effectively resetting the output state of the resampler.
Config(IChannelLayout, SampleFormat, int, IChannelLayout, SampleFormat, int)
Configures the SwrContext with explicit channel layouts, sample formats, and sample rates.
Declaration
public AVResult32 Config(IChannelLayout srcLayout, SampleFormat srcFormat, int srcSampleRate, IChannelLayout dstLayout, SampleFormat dstFormat, int dstSampleRate)
Parameters
| Type | Name | Description |
|---|---|---|
| IChannelLayout | srcLayout | Source channel layout. |
| SampleFormat | srcFormat | Source sample format. |
| int | srcSampleRate | Source sample rate. |
| IChannelLayout | dstLayout | Destination channel layout. |
| SampleFormat | dstFormat | Destination sample format. |
| int | dstSampleRate | Destination sample rate. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating success or failure. |
Remarks
This method closes any existing context configuration, reallocates the context, and sets new parameters.
Note: You do not need to call Init() manually if you use Convert(AVFrame?, AVFrame?) afterwards,
as it will initialize the context if necessary.
Config(AVFrame, AVFrame)
Configures the SwrContext using source and destination AVFrame formats.
Declaration
public AVResult32 Config(AVFrame src, AVFrame dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | Source AVFrame. |
| AVFrame | dst | Destination AVFrame. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating success or failure. |
Remarks
This method updates the internal configuration using frame metadata.
Note: You do not need to call Init() manually if you use Convert(AVFrame?, AVFrame?) afterwards,
as it will initialize the context if necessary.
Convert(AudioBuffer, AVFrame?)
Converts audio from an AudioBuffer source to an AVFrame destination.
Declaration
public AVResult32 Convert(AudioBuffer src, AVFrame? dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBuffer | src | Source buffer. |
| AVFrame | dst | Destination frame. Can be |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Convert(AudioBuffer, int, AudioBuffer, int)
Converts audio from a source AudioBuffer to a destination AudioBuffer, starting at specified sample indices.
Declaration
public AVResult32 Convert(AudioBuffer src, int srcSampleIndex, AudioBuffer dst, int dstSampleIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBuffer | src | Source buffer. Can be |
| int | srcSampleIndex | Sample index to begin reading from in the source buffer. |
| AudioBuffer | dst | Destination buffer. Can be |
| int | dstSampleIndex | Sample index to begin writing to in the destination buffer. |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
This method will automatically call Init() if the context is not initialized.
If both buffers are null, the method throws.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if both |
Convert(AudioBuffer, int, AVFrame?)
Converts audio from an AudioBuffer source to an AVFrame destination.
Declaration
public AVResult32 Convert(AudioBuffer src, int srcSampleIndex, AVFrame? dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBuffer | src | Source buffer. |
| int | srcSampleIndex | Sample index to begin reading from in the source buffer. |
| AVFrame | dst | Destination frame. Can be |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
This method will automatically call Init() if needed.
Convert(AVFrame?, AudioBuffer)
Converts audio from an AVFrame to an AudioBuffer, writing to the end of the buffer.
Declaration
public AVResult32 Convert(AVFrame? src, AudioBuffer dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | Source frame. Can be |
| AudioBuffer | dst | Destination audio buffer. |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Convert(AVFrame?, AudioBuffer, int)
Converts audio from an AVFrame source to an AudioBuffer destination, starting at the specified sample index.
Declaration
public AVResult32 Convert(AVFrame? src, AudioBuffer dst, int dstSampleIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame, or |
| AudioBuffer | dst | The destination buffer. |
| int | dstSampleIndex | The starting index in the destination buffer to write to. |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
This method will automatically call Init() if the context is not initialized. If conversion fails, check both the return value and initialization state.
Convert(AVFrame?, AVFrame?)
Declaration
public AVResult32 Convert(AVFrame? src, AVFrame? dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame. Can be |
| AVFrame | dst | The destination frame. Can be |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
This method will automatically call Init() if the context is not yet initialized. If conversion fails, it is recommended to verify whether the context was initialized correctly.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if |
Convert(AVFrame?, nint, int)
Converts an AVFrame to a raw audio buffer represented by a pointer.
Declaration
public AVResult32 Convert(AVFrame? src, nint dst, int outSamplesPerChannel)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source AVFrame. Can be |
| nint | dst | Pointer to the destination buffer. |
| int | outSamplesPerChannel | The number of output samples per channel that |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
This method supports packed or planar mono output only. If the output format is planar, the destination layout must be mono (1 channel). Automatically calls Init() if not yet initialized.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if the source layout, format, or sample rate does not match expectations, or if planar output is requested with non-mono layout. |
Convert(AVFrame?, Span<byte>)
Declaration
public AVResult32 Convert(AVFrame? src, Span<byte> dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source AVFrame. Can be |
| Span<byte> | dst | A span representing the output buffer. |
Returns
| Type | Description |
|---|---|
| AVResult32 | On success, returns the number of output samples produced. On error, returns an AVResult32 error code. |
Remarks
Supports packed or planar mono output. Automatically calculates how many output samples fit into dst.
Automatically calls Init() if not yet initialized.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if the source layout, format, or sample rate does not match the configuration, or if planar output is used with more than one channel. |
Dispose()
Releases unmanaged resources associated with the SwrContext.
Declaration
public void Dispose()
~SwrContext()
Finalizer to ensure unmanaged resources are freed if Dispose() is not called.
Declaration
protected ~SwrContext()
GetOutputSampleCount()
Gets the number of output samples that can be produced from the currently queued input samples.
Declaration
public AVResult32 GetOutputSampleCount()
Returns
| Type | Description |
|---|---|
| AVResult32 | The number of available output samples, or an error code. |
GetOutputSampleCount(int)
Gets the number of output samples that will be generated from a given number of input samples.
Declaration
public AVResult32 GetOutputSampleCount(int inSamples)
Parameters
| Type | Name | Description |
|---|---|---|
| int | inSamples | The number of input samples per channel. |
Returns
| Type | Description |
|---|---|
| AVResult32 | The corresponding number of output samples, or an error code. |
Init()
Initializes the SwrContext after configuration. Must be called before conversion if using the default constructor.
Declaration
public AVResult32 Init()
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 indicating success or error. |