Class SwsContext
Represents a context for scaling and converting image frames between different sizes and pixel formats. This class provides an interface for configuring the source and destination image properties and performing the conversion using the FFmpeg scaling library.
Inherited Members
Namespace: FFmpeg.Images
Assembly: FFmpeg.dll
Syntax
public sealed class SwsContext : OptionQueryableBase, IOptionQueryable, ILoggingContext, IDisposable, IAVPointer<_SwsContext>
Constructors
View SourceSwsContext(int, int, PixelFormat, int, int, PixelFormat, SwsAlgorithm)
Initializes a new SwsContext with a fixed source and destination image configuration.
Declaration
public SwsContext(int srcW, int srcH, PixelFormat srcFormat, int dstW, int dstH, PixelFormat dstFormat, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| int | srcW | The width of the source image, in pixels. |
| int | srcH | The height of the source image, in pixels. |
| PixelFormat | srcFormat | The pixel format of the source image. |
| int | dstW | The width of the destination image, in pixels. |
| int | dstH | The height of the destination image, in pixels. |
| PixelFormat | dstFormat | The pixel format of the destination image. |
| SwsAlgorithm | algorithm | The scaling algorithm and associated parameters to use. |
Remarks
Use this constructor when converting between image buffers or image planes where the source and destination dimensions and pixel formats are known in advance. The source and destination properties of the created context are fixed for its lifetime and cannot be changed after construction.
If you intend to convert between AVFrame instances using Convert(AVFrame, AVFrame), prefer creating the context with Allocate() instead. An allocated context automatically configures itself to match the source and destination frames when scaling.
Properties
View SourceAlgorithm
Gets the scaling algorithm used by this context.
Declaration
public SwsAlgorithm Algorithm { get; }
Property Value
| Type | Description |
|---|---|
| SwsAlgorithm |
Remarks
For contexts created with Allocate(), the algorithm is currently fixed to the default algorithm selected during context initialization.
DestinationFormat
Gets the pixel format of the destination image.
Declaration
public PixelFormat DestinationFormat { get; }
Property Value
| Type | Description |
|---|---|
| PixelFormat |
DestinationHeight
Gets the height of the destination image in pixels.
Declaration
public int DestinationHeight { get; }
Property Value
| Type | Description |
|---|---|
| int |
DestinationWidth
Gets the width of the destination image in pixels.
Declaration
public int DestinationWidth { get; }
Property Value
| Type | Description |
|---|---|
| int |
Pointer
Provides a pointer to the underlying FFmpeg scaling context.
Declaration
protected override void* Pointer { get; }
Property Value
| Type | Description |
|---|---|
| void* |
Overrides
View SourceSourceFormat
Gets the pixel format of the source image.
Declaration
public PixelFormat SourceFormat { get; }
Property Value
| Type | Description |
|---|---|
| PixelFormat |
SourceHeight
Gets the height of the source image in pixels.
Declaration
public int SourceHeight { get; }
Property Value
| Type | Description |
|---|---|
| int |
SourceWidth
Gets the width of the source image in pixels.
Declaration
public int SourceWidth { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
View SourceAllocate()
Allocates an unconfigured SwsContext.
Declaration
public static SwsContext Allocate()
Returns
| Type | Description |
|---|---|
| SwsContext | A new, unconfigured SwsContext suitable for frame-to-frame scaling. |
Remarks
This is the preferred way to create a context when using Convert(AVFrame, AVFrame). The context is automatically configured from the source and destination frames each time it is used, updating the source and destination properties as needed.
Unlike SwsContext(int, int, PixelFormat, int, int, PixelFormat, SwsAlgorithm), the image dimensions and pixel formats are not fixed when the context is created.
The scaling algorithm cannot currently be specified when using this creation method.
CheckContext(SwsContext?, CodecContext, CodecContext)
Ensures that a scaling context matches the specified source and destination codec contexts.
Declaration
public static SwsContext CheckContext(SwsContext? context, CodecContext src, CodecContext dst)
Parameters
| Type | Name | Description |
|---|---|---|
| SwsContext | context | The existing scaling context, or null. |
| CodecContext | src | The source codec context. |
| CodecContext | dst | The destination codec context. |
Returns
| Type | Description |
|---|---|
| SwsContext | A compatible SwsContext. If the supplied context is not compatible, it is disposed and replaced with a newly created instance. |
Remarks
This helper is intended for applications that repeatedly convert frames whose dimensions or pixel formats may change during playback or encoding.
CheckContext(SwsContext?, CodecContext, AVFrame)
Ensures that a scaling context matches the specified source codec context and destination frame.
Declaration
public static SwsContext CheckContext(SwsContext? context, CodecContext src, AVFrame dst)
Parameters
| Type | Name | Description |
|---|---|---|
| SwsContext | context | The existing scaling context, or null. |
| CodecContext | src | The source codec context. |
| AVFrame | dst | The destination codec context. |
Returns
| Type | Description |
|---|---|
| SwsContext | A compatible SwsContext. If the supplied context is not compatible, it is disposed and replaced with a newly created instance. |
Remarks
This helper is intended for applications that repeatedly convert frames whose dimensions or pixel formats may change during playback or encoding.
CheckContext(SwsContext?, AVFrame, CodecContext)
Ensures that a scaling context matches the specified source and destination codec contexts.
Declaration
public static SwsContext CheckContext(SwsContext? context, AVFrame src, CodecContext dst)
Parameters
| Type | Name | Description |
|---|---|---|
| SwsContext | context | The existing scaling context, or null. |
| AVFrame | src | The source codec context. |
| CodecContext | dst | The destination codec context. |
Returns
| Type | Description |
|---|---|
| SwsContext | A compatible SwsContext. If the supplied context is not compatible, it is disposed and replaced with a newly created instance. |
Remarks
This helper is intended for applications that repeatedly convert frames whose dimensions or pixel formats may change during playback or encoding.
CheckContext(SwsContext?, AVFrame, AVFrame)
Ensures that a scaling context matches the specified source and destination frames.
Declaration
public static SwsContext CheckContext(SwsContext? context, AVFrame src, AVFrame dst)
Parameters
| Type | Name | Description |
|---|---|---|
| SwsContext | context | The existing scaling context, or null. |
| AVFrame | src | The source codec context. |
| AVFrame | dst | The destination codec context. |
Returns
| Type | Description |
|---|---|
| SwsContext | A compatible SwsContext. If the supplied context is not compatible, it is disposed and replaced with a newly created instance. |
Remarks
This helper is intended for applications that repeatedly convert frames whose dimensions or pixel formats may change during playback or encoding.
Convert(Image, Image)
Declaration
public AVResult32 Convert(Image src, Image dst)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | src | The source image to be converted. |
| Image | dst | The destination image where the converted data will be stored. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(Image, Image, SwsAlgorithm)
Declaration
public static AVResult32 Convert(Image src, Image dst, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | src | The source image to be converted. |
| Image | dst | The destination image where the converted data will be stored. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(Image, AVFrame)
Declaration
public AVResult32 Convert(Image src, AVFrame dst)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | src | The source image to be converted. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(Image, AVFrame, SwsAlgorithm)
Declaration
public static AVResult32 Convert(Image src, AVFrame dst, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | src | The source image to be converted. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, Image)
Declaration
public AVResult32 Convert(AVFrame src, Image dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| Image | dst | The destination image where the converted data will be stored. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, Image, SwsAlgorithm)
Declaration
public static AVResult32 Convert(AVFrame src, Image dst, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| Image | dst | The destination image where the converted data will be stored. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, AVFrame)
Declaration
public AVResult32 Convert(AVFrame src, AVFrame dst)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, AVFrame, SwsAlgorithm)
Declaration
public static AVResult32 Convert(AVFrame src, AVFrame dst, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
| SwsAlgorithm | algorithm | The algorithm used for scaling |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, nint, ImageInfo, SwsAlgorithm)
Converts the source AVFrame to the destination buffer, described by ImageInfo, using the specified scaling algorithm.
Declaration
public static AVResult32 Convert(AVFrame src, nint dst, ImageInfo dstInfo, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| nint | dst | Pointer to the destination buffer. |
| ImageInfo | dstInfo | Information describing the destination image. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, nint, int)
Converts the source AVFrame to a destination buffer at a specified memory location.
Declaration
public AVResult32 Convert(AVFrame src, nint dst, int dstAlign = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| nint | dst | The destination buffer as a memory pointer. |
| int | dstAlign | Optional memory alignment for the destination buffer, default is 1. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, ReadOnlySpan<nint>, ReadOnlySpan<int>)
Converts pixel data from the specified source AVFrame into the destination image planes using the current scaling context.
Declaration
public AVResult32 Convert(AVFrame frame, ReadOnlySpan<nint> dstPlanes, ReadOnlySpan<int> dstStride)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source frame. Its pixel format must match SourceFormat. |
| ReadOnlySpan<nint> | dstPlanes | The destination image planes as an array of pointers. The array must contain at least as many elements as required by DestinationFormat. |
| ReadOnlySpan<int> | dstStride | The line size, in bytes, for each destination plane. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(AVFrame, Span<byte>, int)
Declaration
public AVResult32 Convert(AVFrame src, Span<byte> dst, int dstAlign = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | src | The source frame to be converted. |
| Span<byte> | dst | The destination buffer as a Span<T>. |
| int | dstAlign | Optional memory alignment for the destination buffer, default is 1. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(nint, ImageInfo, AVFrame, SwsAlgorithm)
Converts the source buffer, described by ImageInfo, to the destination AVFrame using the specified scaling algorithm.
Declaration
public static AVResult32 Convert(nint src, ImageInfo srcInfo, AVFrame dst, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | src | Pointer to the source buffer. |
| ImageInfo | srcInfo | Information describing the source image. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(nint, ImageInfo, nint, ImageInfo)
Converts the source buffer, described by ImageInfo, to the destination buffer, also described by ImageInfo.
Declaration
public AVResult32 Convert(nint src, ImageInfo srcInfo, nint dst, ImageInfo dstInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | src | Pointer to the source buffer. |
| ImageInfo | srcInfo | The information describing the source image. |
| nint | dst | Pointer to the destination buffer. |
| ImageInfo | dstInfo | The information describing the destination image. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(nint, ImageInfo, nint, ImageInfo, SwsAlgorithm)
Converts the source buffer, described by ImageInfo, to the destination buffer, also described by ImageInfo, using the specified scaling algorithm.
Declaration
public static AVResult32 Convert(nint src, ImageInfo srcInfo, nint dst, ImageInfo dstInfo, SwsAlgorithm algorithm)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | src | Pointer to the source buffer. |
| ImageInfo | srcInfo | Information describing the source image. |
| nint | dst | Pointer to the destination buffer. |
| ImageInfo | dstInfo | Information describing the destination image. |
| SwsAlgorithm | algorithm | The scaling algorithm to use for the conversion. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(nint, AVFrame, int)
Converts a source buffer from a specified memory location to the destination AVFrame.
Declaration
public AVResult32 Convert(nint src, AVFrame dst, int srcAlign = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | src | The source buffer as a memory pointer. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
| int | srcAlign | Optional memory alignment for the source buffer, default is 1. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(nint, nint, int, int)
Converts the source buffer to the destination buffer, both specified by pointers, with optional alignments for source and destination.
Declaration
public AVResult32 Convert(nint src, nint dst, int srcAlign = 1, int dstAlign = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| nint | src | Pointer to the source buffer. |
| nint | dst | Pointer to the destination buffer. |
| int | srcAlign | Optional memory alignment for the source buffer, default is 1. |
| int | dstAlign | Optional memory alignment for the destination buffer, default is 1. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(ReadOnlySpan<nint>, ReadOnlySpan<int>, AVFrame)
Converts pixel data from the specified source image planes into the destination AVFrame using the current scaling context.
Declaration
public AVResult32 Convert(ReadOnlySpan<nint> srcPlanes, ReadOnlySpan<int> srcStride, AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<nint> | srcPlanes | The source image planes as an array of pointers. The array must contain at least as many elements as required by SourceFormat. |
| ReadOnlySpan<int> | srcStride | The line size, in bytes, for each source plane. |
| AVFrame | frame | The destination frame. Its pixel format must match DestinationFormat. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(ReadOnlySpan<nint>, ReadOnlySpan<int>, ReadOnlySpan<nint>, ReadOnlySpan<int>)
Converts the source and destination planes using the current scaling context.
Declaration
public AVResult32 Convert(ReadOnlySpan<nint> srcPlanes, ReadOnlySpan<int> srcStride, ReadOnlySpan<nint> dstPlanes, ReadOnlySpan<int> dstStride)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<nint> | srcPlanes | The source planes as an array of pointers. |
| ReadOnlySpan<int> | srcStride | The line size of each source plane. |
| ReadOnlySpan<nint> | dstPlanes | The destination planes as an array of pointers. |
| ReadOnlySpan<int> | dstStride | The line size of each destination plane. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceConvert(Span<byte>, AVFrame, int)
Declaration
public AVResult32 Convert(Span<byte> src, AVFrame dst, int srcAlign = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<byte> | src | The source buffer as a Span<T>. |
| AVFrame | dst | The destination frame where the converted data will be stored. |
| int | srcAlign | Optional memory alignment for the source buffer, default is 1. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An AVResult32 value indicating success or failure of the conversion operation. |
See Also
View SourceDispose()
Releases the unmanaged resources associated with this scaling context.
Declaration
public void Dispose()
Remarks
After calling this method, the context can no longer be used for image conversion.
~SwsContext()
Ensures the unmanaged data gets released.
Declaration
protected ~SwsContext()
GetBufferSize(int, int, PixelFormat, int)
Calculates the buffer size required for an image with the specified width, height, and pixel format.
Declaration
public static int GetBufferSize(int width, int height, PixelFormat fmt, int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | The width of the image in pixels. |
| int | height | The height of the image in pixels. |
| PixelFormat | fmt | The pixel format of the image. |
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the buffer required to store the image. |
GetDestinationBufferSize(int)
Calculates the buffer size required for the destination image based on its dimensions, format, and alignment.
Declaration
public int GetDestinationBufferSize(int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the buffer required to store the destination image. |
See Also
View SourceGetDestinationLineSize(int, int)
Gets the size of a single line (row) in the destination image for a given plane, considering the specified alignment.
Declaration
public int GetDestinationLineSize(int plane, int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | plane | The plane index of the image. |
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the line in bytes. |
See Also
View SourceGetLineSize(int, PixelFormat, int, int)
Gets the size of a single line (row) in an image with the specified width, pixel format, and plane, considering the given alignment.
Declaration
public static int GetLineSize(int width, PixelFormat fmt, int plane, int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | The width of the image in pixels. |
| PixelFormat | fmt | The pixel format of the image. |
| int | plane | The plane index of the image. |
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the line in bytes. |
GetSourceBufferSize(int)
Calculates the buffer size required for the source image based on its dimensions, format, and alignment.
Declaration
public int GetSourceBufferSize(int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the buffer required to store the source image. |
See Also
View SourceGetSourceLineSize(int, int)
Gets the size of a single line (row) in the source image for a given plane, considering the specified alignment.
Declaration
public int GetSourceLineSize(int plane, int align = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | plane | The plane index of the image (e.g., 0 for Y plane in YUV format). |
| int | align | Optional memory alignment, default is 1. |
Returns
| Type | Description |
|---|---|
| int | The size of the line in bytes. |