Show / Hide Table of Contents

Class SwrContext

Represents a managed wrapper for the FFmpeg SwrContext, used for audio resampling, format conversion, and channel layout remapping.

Inheritance
object
OptionQueryableBase
SwrContext
Implements
IOptionQueryable
ILoggingContext
IDisposable
IAVPointer<_SwrContext>
Inherited Members
OptionQueryableBase.ResetOptions()
OptionQueryableBase.GetOptions(bool)
OptionQueryableBase.FindOption(string, bool)
OptionQueryableBase.SetOption(string, string, bool)
OptionQueryableBase.SetOption(string, long, bool)
OptionQueryableBase.SetOption(string, bool, bool)
OptionQueryableBase.SetOption(string, double, bool)
OptionQueryableBase.SetOption(string, Rational, bool)
OptionQueryableBase.SetOption(string, ReadOnlySpan<byte>, bool)
OptionQueryableBase.SetOption(string, ReadOnlyMemory<byte>, bool)
OptionQueryableBase.SetOption(string, int, int, bool)
OptionQueryableBase.SetOption(string, (int Width, int Height), bool)
OptionQueryableBase.SetOption(string, PixelFormat, bool)
OptionQueryableBase.SetOption(string, SampleFormat, bool)
OptionQueryableBase.SetOption(string, IDictionary<string, string>, bool)
OptionQueryableBase.SetOption(string, ILookup<string, string>, bool)
OptionQueryableBase.SetOption(IDictionary<string, string>, bool)
OptionQueryableBase.SetOption(string, ref ILookup<string, string>, bool)
OptionQueryableBase.SetOption<T>(string, T, bool)
OptionQueryableBase.SetOption(string, object, bool)
OptionQueryableBase.SetOption(Option, string, bool)
OptionQueryableBase.SetOption(Option, bool, bool)
OptionQueryableBase.SetOption(Option, long, bool)
OptionQueryableBase.SetOption(Option, double, bool)
OptionQueryableBase.SetOption(Option, Rational, bool)
OptionQueryableBase.SetOption(Option, ReadOnlySpan<byte>, bool)
OptionQueryableBase.SetOption(Option, ReadOnlyMemory<byte>, bool)
OptionQueryableBase.SetOption(Option, int, int, bool)
OptionQueryableBase.SetOption(Option, (int Width, int Height), bool)
OptionQueryableBase.SetOption(Option, PixelFormat, bool)
OptionQueryableBase.SetOption(Option, SampleFormat, bool)
OptionQueryableBase.SetOption(Option, IDictionary<string, string>, bool)
OptionQueryableBase.SetOption(Option, ILookup<string, string>, bool)
OptionQueryableBase.SetOption<T>(Option, T, bool)
OptionQueryableBase.SetOption(Option, object, bool)
OptionQueryableBase.TryGetOption(string, out string, bool)
OptionQueryableBase.TryGetOption(string, out long, bool)
OptionQueryableBase.TryGetOption<T>(string, out T, bool)
OptionQueryableBase.TryGetOption(string, out ulong, bool)
OptionQueryableBase.TryGetOption(string, out int, bool)
OptionQueryableBase.TryGetOption(string, out bool, bool)
OptionQueryableBase.TryGetOption(string, out uint, bool)
OptionQueryableBase.TryGetOption(string, out double, bool)
OptionQueryableBase.TryGetOption(string, out Rational, bool)
OptionQueryableBase.TryGetOption(string, out PixelFormat, bool)
OptionQueryableBase.TryGetOption(string, out SampleFormat, bool)
OptionQueryableBase.TryGetOption(string, out IDictionary<string, string>, bool)
OptionQueryableBase.TryGetOption(string, out Dictionary<string, string>, bool)
OptionQueryableBase.TryGetOption(string, out AVDictionary, bool)
OptionQueryableBase.TryGetOption(string, out ILookup<string, string>, bool)
OptionQueryableBase.TryGetOption(string, out AVMultiDictionary, bool)
OptionQueryableBase.TryGetOption(string, out int, out int, bool)
OptionQueryableBase.TryGetOption(string, out (int Width, int Height), bool)
OptionQueryableBase.TryGetOption(string, Span<byte>, bool)
OptionQueryableBase.TryGetOption(string, out byte[], bool)
OptionQueryableBase.TryGetOption(string, out Memory<byte>, bool)
OptionQueryableBase.TryGetOption(string, out ReadOnlyMemory<byte>, bool)
OptionQueryableBase.TryGetOption(string, out object, bool)
OptionQueryableBase.TryGetOption(Option, out string, bool)
OptionQueryableBase.TryGetOption(Option, out long, bool)
OptionQueryableBase.TryGetOption<T>(Option, out T, bool)
OptionQueryableBase.TryGetOption(Option, out ulong, bool)
OptionQueryableBase.TryGetOption(Option, out int, bool)
OptionQueryableBase.TryGetOption(Option, out bool, bool)
OptionQueryableBase.TryGetOption(Option, out uint, bool)
OptionQueryableBase.TryGetOption(Option, out double, bool)
OptionQueryableBase.TryGetOption(Option, out Rational, bool)
OptionQueryableBase.TryGetOption(Option, out PixelFormat, bool)
OptionQueryableBase.TryGetOption(Option, out SampleFormat, bool)
OptionQueryableBase.TryGetOption(Option, out IDictionary<string, string>, bool)
OptionQueryableBase.TryGetOption(Option, out Dictionary<string, string>, bool)
OptionQueryableBase.TryGetOption(Option, out AVDictionary, bool)
OptionQueryableBase.TryGetOption(Option, out ILookup<string, string>, bool)
OptionQueryableBase.TryGetOption(Option, out AVMultiDictionary, bool)
OptionQueryableBase.TryGetOption(Option, out int, out int, bool)
OptionQueryableBase.TryGetOption(Option, out (int Width, int Height), bool)
OptionQueryableBase.TryGetOption(Option, out byte[], bool)
OptionQueryableBase.TryGetOption(Option, out Memory<byte>, bool)
OptionQueryableBase.TryGetOption(Option, out ReadOnlyMemory<byte>, bool)
OptionQueryableBase.TryGetOption(Option, Span<byte>, bool)
OptionQueryableBase.TryGetOption(Option, out object, bool)
OptionQueryableBase.SetOption(string, ChannelLayout, bool)
OptionQueryableBase.SetOption(Option, ChannelLayout, bool)
OptionQueryableBase.TryGetOption(string, out ChannelLayout, bool)
OptionQueryableBase.TryGetOption(Option, out ChannelLayout, bool)
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: FFmpeg.Audio
Assembly: FFmpeg.dll
Syntax
public class SwrContext : OptionQueryableBase, IOptionQueryable, ILoggingContext, IDisposable, IAVPointer<_SwrContext>

Constructors

View Source

SwrContext()

Initializes a new empty SwrContext instance. Configuration should be done manually via property setters or Config(AVFrame, AVFrame).

Declaration
public SwrContext()
View Source

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.

View Source

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 Source

BytesPerSampleDestination

Gets the number of bytes per audio sample for the current destination format.

Declaration
public int BytesPerSampleDestination { get; }
Property Value
Type Description
int
View Source

BytesPerSampleSource

Gets the number of bytes per audio sample for the current source format.

Declaration
public int BytesPerSampleSource { get; }
Property Value
Type Description
int
View Source

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
View Source

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
View Source

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
View Source

IsInitialized

Gets a value indicating whether the SwrContext is initialized and ready for conversion.

Declaration
public bool IsInitialized { get; }
Property Value
Type Description
bool
View Source

Pointer

Gets the native pointer to the internal SwrContext structure.

Declaration
protected override void* Pointer { get; }
Property Value
Type Description
void*
Overrides
OptionQueryableBase.Pointer
View Source

SourceFormat

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
View Source

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
View Source

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 Source

BytesPerSample(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.

View Source

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.

View Source

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.

View Source

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.

View Source

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 null.

Returns
Type Description
AVResult32

On success, returns the number of output samples produced. On error, returns an AVResult32 error code.

View Source

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 null.

int srcSampleIndex

Sample index to begin reading from in the source buffer.

AudioBuffer dst

Destination buffer. Can be null.

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 src and dst are null.

View Source

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 null.

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.

View Source

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 null to flush the resampler.

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.

View Source

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 null to flush the resampler.

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.

View Source

Convert(AVFrame?, AVFrame?)

Converts audio data from a source AVFrame to a destination AVFrame.

Declaration
public AVResult32 Convert(AVFrame? src, AVFrame? dst)
Parameters
Type Name Description
AVFrame src

The source frame. Can be null.

AVFrame dst

The destination frame. Can be null.

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 src and dst refer to the same instance.

View Source

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 null to flush the resampler.

nint dst

Pointer to the destination buffer.

int outSamplesPerChannel

The number of output samples per channel that dst can hold.

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.

View Source

Convert(AVFrame?, Span<byte>)

Converts an AVFrame to a raw audio buffer provided as a Span<T>.

Declaration
public AVResult32 Convert(AVFrame? src, Span<byte> dst)
Parameters
Type Name Description
AVFrame src

The source AVFrame. Can be null.

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.

View Source

Dispose()

Releases unmanaged resources associated with the SwrContext.

Declaration
public void Dispose()
View Source

~SwrContext()

Finalizer to ensure unmanaged resources are freed if Dispose() is not called.

Declaration
protected ~SwrContext()
View Source

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.

View Source

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.

View Source

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.

Implements

IOptionQueryable
ILoggingContext
IDisposable
IAVPointer<T>

Extension Methods

LoggingContextExtensions.Debug(ILoggingContext, string)
LoggingContextExtensions.Error(ILoggingContext, string)
LoggingContextExtensions.Fatal(ILoggingContext, string)
LoggingContextExtensions.Info(ILoggingContext, string)
LoggingContextExtensions.Log(ILoggingContext, LogLevel, string)
LoggingContextExtensions.Panic(ILoggingContext, string)
LoggingContextExtensions.Trace(ILoggingContext, string)
LoggingContextExtensions.Verbose(ILoggingContext, string)
LoggingContextExtensions.Warning(ILoggingContext, string)
  • View Source
In this article
Back to top Generated by DocFX