Class Extensions
Provides extension methods for interoperating between FFmpeg image types and SkiaSharp.
Inherited Members
Namespace: FFmpeg.Skia
Assembly: FFmpeg.Skia.dll
Syntax
public static class Extensions
Remarks
This class contains helper methods for converting between FFmpeg FFmpeg.Images.PixelFormat values and SkiaSharp SKColorType values, as well as methods for converting image data between FFmpeg.Utils.AVFrame, SKImage, and SKBitmap.
When the source and destination pixel formats are compatible, image data is copied directly without conversion. Otherwise, pixel format conversion and image scaling are performed using FFmpeg.Images.SwsContext.
Methods prefixed with To create independent copies of the image data,
while methods prefixed with As create Skia objects that share the
underlying pixel buffer with a new reference to the FFmpeg.Utils.AVFrame, avoiding an
additional memory copy whenever possible.
FFmpeg stores straight (unpremultiplied) alpha. Skia images and bitmaps created by these methods use Unpremul for color types with alpha and Opaque otherwise. When copying from or into premultiplied Skia images, the alpha is converted.
The cropping values of a frame (FFmpeg.Utils.AVFrame.CropLeft, ...) are applied: only the visible region is converted.
The DrawFrame extensions of SKCanvas draw frames without copying when Skia supports their
pixel format (AsSKImage(AVFrame)) and convert them otherwise. The display rotation of a video is not
applied by any of these methods; use DrawRotated to draw with it.
Properties
View SourceFFSupportedColorTypes
Gets the SKColorType values that have a direct equivalent FFmpeg FFmpeg.Images.PixelFormat.
Declaration
public static SKColorType[] FFSupportedColorTypes { get; }
Property Value
| Type | Description |
|---|---|
| SKColorType[] |
Remarks
The order of the color types corresponds to SKSupportedPixelFormats.
SKSupportedPixelFormats
Gets the FFmpeg pixel formats that can be represented directly by SKColorType without requiring an intermediate conversion.
Declaration
public static PixelFormat[] SKSupportedPixelFormats { get; }
Property Value
| Type | Description |
|---|---|
| PixelFormat[] |
Remarks
The order of the formats reflects the preferred formats used when selecting the closest compatible Skia color type.
Methods
View SourceAsSKBitmap(AVFrame)
Creates an SKBitmap that shares the pixel buffer of the supplied FFmpeg.Utils.AVFrame whenever possible.
Declaration
public static SKBitmap AsSKBitmap(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
Returns
| Type | Description |
|---|---|
| SKBitmap | An SKBitmap representing the visible region of the frame. |
Remarks
If the frame uses a pixel format that is directly supported by Skia, no pixel data is copied. Instead, a new reference to the frame is created and the returned bitmap points into its buffer. The buffer may be shared with other frames (e.g. the decoder's), so treat the bitmap as read-only.
The frame reference remains alive until the returned SKBitmap is disposed or reset.
If the frame's pixel format is not supported by Skia, this method falls back to ToSKBitmap(AVFrame), which performs a pixel format conversion and copies the image data.
AsSKImage(AVFrame)
Creates an SKImage that shares the pixel buffer of the supplied FFmpeg.Utils.AVFrame whenever possible.
Declaration
public static SKImage AsSKImage(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
Returns
| Type | Description |
|---|---|
| SKImage | An SKImage representing the visible region of the frame. |
Remarks
If the frame uses a pixel format that is directly supported by Skia, no pixel data is copied. Instead, a new reference to the frame is created and the returned image points into its buffer.
The frame reference remains alive until the returned SKImage is disposed.
If the frame's pixel format is not supported by Skia, this method falls back to ToSKImage(AVFrame), which performs a pixel format conversion and copies the image data.
CopyTo(AVFrame, SKBitmap)
Copies the contents of an FFmpeg.Utils.AVFrame into an existing SKBitmap.
Declaration
public static void CopyTo(this AVFrame frame, SKBitmap bitmap)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
| SKBitmap | bitmap | The destination bitmap. |
Remarks
If the bitmap has no pixels, it is allocated with the visible size of the frame and the frame's color type, or the closest 8 bit color type if Skia does not support the frame's pixel format.
If the frame and bitmap have matching dimensions and compatible pixel formats, the pixel data is copied directly. Otherwise, the frame is scaled and/or converted using FFmpeg.Images.SwsContext before being written to the bitmap.
NotifyPixelsChanged() is called automatically after the bitmap has been updated.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The bitmap's color type has no equivalent FFmpeg pixel format. |
CopyTo(SKBitmap, AVFrame)
Copies the contents of an SKBitmap into an existing FFmpeg.Utils.AVFrame.
Declaration
public static void CopyTo(this SKBitmap bitmap, AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| SKBitmap | bitmap | The source bitmap. |
| AVFrame | frame | The destination video frame. |
Remarks
If the destination frame has not been initialized, its dimensions and pixel format are set to match the bitmap before the copy operation.
If the destination frame has already been initialized, its dimensions and pixel format are preserved. The bitmap is scaled and/or converted as necessary before being written to the frame.
If the destination frame does not already own a buffer, one is allocated automatically. If its buffer is shared with other frames, it is made writable first.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The bitmap's SKColorType has no equivalent FFmpeg pixel format. |
CopyTo(SKImage, AVFrame)
Copies the contents of an SKImage into an existing FFmpeg.Utils.AVFrame.
Declaration
public static void CopyTo(this SKImage image, AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| SKImage | image | The source image. |
| AVFrame | frame | The destination video frame. |
Remarks
If the destination frame has not been initialized, its dimensions and pixel format are set to match the image before the copy operation.
If the destination frame has already been initialized, its dimensions and pixel format are preserved. The image is scaled and/or converted as necessary before being written to the frame.
If the destination frame does not already own a buffer, one is allocated automatically. If its buffer is shared with other frames, it is made writable first.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The image's SKColorType has no equivalent FFmpeg pixel format, or the image's pixel data cannot be accessed (for example, because it is GPU-backed). |
CroppedRect(AVFrame)
Gets the cropping rectangle of the frame as an SKRectI.
Declaration
public static SKRectI CroppedRect(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame |
Returns
| Type | Description |
|---|---|
| SKRectI | An SKRectI describing the visible region of the frame after applying the cropping values stored in the frame. |
DrawFrame(SKCanvas, AVFrame, SKPoint, SKSamplingOptions, SKPaint?)
Draws an FFmpeg.Utils.AVFrame onto the canvas at the specified position.
Declaration
public static void DrawFrame(this SKCanvas canvas, AVFrame frame, SKPoint p, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| AVFrame | frame | The source video frame to draw. |
| SKPoint | p | The position at which to draw the frame. |
| SKSamplingOptions | sampling | The sampling options used when the frame is scaled. |
| SKPaint | paint | The optional paint used to draw the frame. |
DrawFrame(SKCanvas, AVFrame, SKRect, SKRect, SKSamplingOptions, SKPaint?)
Draws a portion of an FFmpeg.Utils.AVFrame into a destination rectangle.
Declaration
public static void DrawFrame(this SKCanvas canvas, AVFrame frame, SKRect source, SKRect dest, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| AVFrame | frame | The source video frame to draw. |
| SKRect | source | The portion of the frame to draw. |
| SKRect | dest | The destination rectangle onto which the source region is drawn. |
| SKSamplingOptions | sampling | The sampling options used when the frame is scaled. |
| SKPaint | paint | The optional paint used to draw the frame. |
DrawFrame(SKCanvas, AVFrame, SKRect, SKSamplingOptions, SKPaint?)
Draws an FFmpeg.Utils.AVFrame into the specified destination rectangle.
Declaration
public static void DrawFrame(this SKCanvas canvas, AVFrame frame, SKRect dest, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| AVFrame | frame | The source video frame to draw. |
| SKRect | dest | The destination rectangle onto which the frame is drawn. |
| SKSamplingOptions | sampling | The sampling options used when the frame is scaled. |
| SKPaint | paint | The optional paint used to draw the frame. |
DrawFrame(SKCanvas, AVFrame, float, float, SKSamplingOptions, SKPaint?)
Draws an FFmpeg.Utils.AVFrame onto the canvas at the specified coordinates.
Declaration
public static void DrawFrame(this SKCanvas canvas, AVFrame frame, float x, float y, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| AVFrame | frame | The source video frame to draw. |
| float | x | The x-coordinate at which to draw the frame. |
| float | y | The y-coordinate at which to draw the frame. |
| SKSamplingOptions | sampling | The sampling options used when the frame is scaled. |
| SKPaint | paint | The optional paint used to draw the frame. |
DrawRotated(SKCanvas, SKBitmap, SKRect, int, SKSamplingOptions, SKPaint?)
Draws a bitmap rotated clockwise by rotation degrees so that it fills
dest, e.g. a video frame with its display rotation
(Rotation, Rotation, Rotation).
Declaration
public static void DrawRotated(this SKCanvas canvas, SKBitmap bitmap, SKRect dest, int rotation, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| SKBitmap | bitmap | The bitmap to draw. |
| SKRect | dest | The destination rectangle, in display orientation (for 90 and 270 degrees its width corresponds to the bitmap's height). |
| int | rotation | The clockwise rotation in degrees; multiples of 90. |
| SKSamplingOptions | sampling | The sampling options used when the bitmap is scaled. |
| SKPaint | paint | The optional paint used to draw the bitmap. |
DrawRotated(SKCanvas, SKImage, SKRect, int, SKSamplingOptions, SKPaint?)
Draws an image rotated clockwise by rotation degrees so that it fills
dest.
Declaration
public static void DrawRotated(this SKCanvas canvas, SKImage image, SKRect dest, int rotation, SKSamplingOptions sampling, SKPaint? paint = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SKCanvas | canvas | |
| SKImage | image | |
| SKRect | dest | The destination rectangle, in display orientation (for 90 and 270 degrees its width corresponds to the bitmap's height). |
| int | rotation | The clockwise rotation in degrees; multiples of 90. |
| SKSamplingOptions | sampling | The sampling options used when the bitmap is scaled. |
| SKPaint | paint | The optional paint used to draw the bitmap. |
GetDisplayRotation(AVStream)
Gets the rotation a video stream must be displayed with, from its display matrix (e.g. the orientation of a phone video).
Declaration
public static int GetDisplayRotation(this AVStream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| AVStream | stream |
Returns
| Type | Description |
|---|---|
| int | The clockwise rotation in degrees: 0, 90, 180 or 270. 0 if the stream has no display matrix. |
Remarks
Mirroring in the display matrix is ignored. Use DrawRotated(SKCanvas, SKBitmap, SKRect, int, SKSamplingOptions, SKPaint?) to draw a frame with the rotation.
GetDisplayRotation(AVFrame)
Gets the rotation a frame must be displayed with, from its display matrix side data.
Declaration
public static int GetDisplayRotation(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame |
Returns
| Type | Description |
|---|---|
| int | The clockwise rotation in degrees: 0, 90, 180 or 270. 0 if the frame has no display matrix (the stream may still have one, see GetDisplayRotation(AVStream)). |
GetSampleAspectRatio(AVStream)
Gets the sample (pixel) aspect ratio of a video stream: the container's value if it has one, otherwise the
codec's (like av_guess_sample_aspect_ratio).
Declaration
public static Rational GetSampleAspectRatio(this AVStream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| AVStream | stream |
Returns
| Type | Description |
|---|---|
| Rational | The width of a pixel relative to its height; 1:1 if unknown. |
HasFFmpegEquivalent(SKColorType)
Determines whether the specified Skia color type has a direct equivalent FFmpeg pixel format.
Declaration
public static bool HasFFmpegEquivalent(this SKColorType pixelFormat)
Parameters
| Type | Name | Description |
|---|---|---|
| SKColorType | pixelFormat | The Skia color type to test. |
Returns
| Type | Description |
|---|---|
| bool | true if the color type can be converted directly to a FFmpeg.Images.PixelFormat; otherwise, false. |
See Also
View SourceHasSkiaEquivalent(PixelFormat)
Determines whether the specified FFmpeg pixel format can be represented directly by Skia.
Declaration
public static bool HasSkiaEquivalent(this PixelFormat pixFmt)
Parameters
| Type | Name | Description |
|---|---|---|
| PixelFormat | pixFmt | The FFmpeg pixel format to test. |
Returns
| Type | Description |
|---|---|
| bool | true if the pixel format has a direct SKColorType equivalent; otherwise, false. |
See Also
View SourceReference(SKBitmap, AVFrame)
Replaces the pixel data of this SKBitmap with the supplied FFmpeg.Utils.AVFrame, sharing the frame's pixel buffer whenever possible.
Declaration
public static void Reference(this SKBitmap bmp, AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| SKBitmap | bmp | The destination bitmap. |
| AVFrame | frame | The source video frame. |
Remarks
The bitmap's previous pixels are released first.
If the frame uses a pixel format that is directly supported by Skia, no pixel data is copied. Instead, a new reference to the frame is created and the bitmap points into its buffer (see AsSKBitmap(AVFrame)). The frame reference remains alive until the bitmap is reset or disposed.
If the frame's pixel format is not supported by Skia, the frame is converted into newly allocated pixels as in CopyTo(AVFrame, SKBitmap).
ToAVFrame(SKBitmap)
Creates a new FFmpeg.Utils.AVFrame from an SKBitmap.
Declaration
public static AVFrame ToAVFrame(this SKBitmap image)
Parameters
| Type | Name | Description |
|---|---|---|
| SKBitmap | image | The source bitmap. |
Returns
| Type | Description |
|---|---|
| AVFrame | A newly allocated frame containing the bitmap data. |
Remarks
The resulting frame uses the FFmpeg pixel format corresponding to the bitmap's SKColorType. Premultiplied alpha is converted to straight alpha.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The bitmap's color type has no equivalent FFmpeg pixel format. |
ToAVFrame(SKBitmap, PixelFormat)
Creates a new FFmpeg.Utils.AVFrame from an SKBitmap using the specified pixel format.
Declaration
public static AVFrame ToAVFrame(this SKBitmap image, PixelFormat targetFormat = PixelFormat.None)
Parameters
| Type | Name | Description |
|---|---|---|
| SKBitmap | image | The source bitmap. |
| PixelFormat | targetFormat | The desired FFmpeg pixel format. Specify FFmpeg.Images.PixelFormat.None to preserve the bitmap's native format. |
Returns
| Type | Description |
|---|---|
| AVFrame | A newly allocated frame containing the converted bitmap. |
Remarks
If the requested pixel format already matches the bitmap's color type, no conversion is performed.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The bitmap's color type has no equivalent FFmpeg pixel format. |
ToAVFrame(SKImage)
Creates a new FFmpeg.Utils.AVFrame from an SKImage.
Declaration
public static AVFrame ToAVFrame(this SKImage image)
Parameters
| Type | Name | Description |
|---|---|---|
| SKImage | image | The source image. |
Returns
| Type | Description |
|---|---|
| AVFrame | A newly allocated frame containing the image data. |
Remarks
The resulting frame uses the FFmpeg pixel format corresponding to the
image's SKColorType. Lazily decoded images (e.g. from SKImage.FromEncodedData)
are decoded first. Premultiplied alpha is converted to straight alpha.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The image's color type has no equivalent FFmpeg pixel format, or the image's pixels cannot be read (for example, because it is GPU-backed). |
ToAVFrame(SKImage, PixelFormat)
Creates a new FFmpeg.Utils.AVFrame from an SKImage using the specified pixel format.
Declaration
public static AVFrame ToAVFrame(this SKImage image, PixelFormat targetFormat = PixelFormat.None)
Parameters
| Type | Name | Description |
|---|---|---|
| SKImage | image | The source image. |
| PixelFormat | targetFormat | The desired FFmpeg pixel format. Specify FFmpeg.Images.PixelFormat.None to preserve the image's native format. |
Returns
| Type | Description |
|---|---|
| AVFrame | A newly allocated frame containing the converted image. |
Remarks
If the requested pixel format already matches the image's color type, no conversion is performed.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The image's color type has no equivalent FFmpeg pixel format, or the image's pixels cannot be read (for example, because it is GPU-backed). |
ToBestSkiaColorType(PixelFormat)
Converts an FFmpeg FFmpeg.Images.PixelFormat to the most appropriate SKColorType.
Declaration
public static SKColorType ToBestSkiaColorType(this PixelFormat pixelFormat)
Parameters
| Type | Name | Description |
|---|---|---|
| PixelFormat | pixelFormat | The FFmpeg pixel format to convert. |
Returns
| Type | Description |
|---|---|
| SKColorType | The matching SKColorType if one exists; otherwise, the color type corresponding to the closest supported FFmpeg pixel format. |
Remarks
If the specified pixel format is not directly supported by Skia, the closest compatible format is selected using FindBestPixelFormat(PixelFormat, params ReadOnlySpan<PixelFormat>).
See Also
View SourceToPixelFormat(SKColorType)
Converts a supported SKColorType to its corresponding FFmpeg FFmpeg.Images.PixelFormat.
Declaration
public static PixelFormat ToPixelFormat(this SKColorType pixelFormat)
Parameters
| Type | Name | Description |
|---|---|---|
| SKColorType | pixelFormat | The Skia color type to convert. |
Returns
| Type | Description |
|---|---|
| PixelFormat | The corresponding FFmpeg pixel format. |
Remarks
Only color types listed in FFSupportedColorTypes are supported.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the specified SKColorType has no direct equivalent FFmpeg pixel format. |
ToSKBitmap(AVFrame)
Creates a new SKBitmap from an FFmpeg.Utils.AVFrame.
Declaration
public static SKBitmap ToSKBitmap(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
Returns
| Type | Description |
|---|---|
| SKBitmap | A new SKBitmap containing the visible region of the frame. |
Remarks
If the frame's pixel format is directly supported by Skia, the pixel data is copied without conversion.
Otherwise, the frame is converted to PlatformColorType (or Rgba8888) using FFmpeg.Images.SwsContext.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The frame does not contain valid image dimensions. |
ToSKBitmap(AVFrame, SKColorType)
Creates a new SKBitmap from an FFmpeg.Utils.AVFrame using the specified Skia color type.
Declaration
public static SKBitmap ToSKBitmap(this AVFrame frame, SKColorType colorType = SKColorType.Unknown)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
| SKColorType | colorType | The desired color type of the resulting bitmap. If Unknown is specified, the color type is chosen as in ToSKBitmap(AVFrame). |
Returns
| Type | Description |
|---|---|
| SKBitmap | A new SKBitmap containing the visible region of the frame. |
Remarks
If the requested color type already matches the frame's pixel format, no pixel format conversion is performed.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The frame does not contain valid image dimensions. |
| NotSupportedException | The specified color type has no equivalent FFmpeg pixel format. |
ToSKImage(AVFrame)
Creates a new SKImage from an FFmpeg.Utils.AVFrame.
Declaration
public static SKImage ToSKImage(this AVFrame frame)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
Returns
| Type | Description |
|---|---|
| SKImage | A new SKImage containing the visible region of the frame. |
Remarks
If the frame's pixel format is directly supported by Skia, the pixel data is copied without conversion.
Otherwise, the frame is converted to a compatible Skia color type using FFmpeg.Images.SwsContext.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The frame does not contain valid image dimensions. |
ToSKImage(AVFrame, SKColorType)
Creates a new SKImage from an FFmpeg.Utils.AVFrame using the specified Skia color type.
Declaration
public static SKImage ToSKImage(this AVFrame frame, SKColorType colorType = SKColorType.Unknown)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | frame | The source video frame. |
| SKColorType | colorType | The desired color type of the resulting image. If Unknown is specified, the color type is chosen as in ToSKImage(AVFrame). |
Returns
| Type | Description |
|---|---|
| SKImage | A new SKImage containing the visible region of the frame. |
Remarks
If the requested color type already matches the frame's pixel format, no pixel format conversion is performed.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The frame does not contain valid image dimensions. |
| NotSupportedException | The specified color type cannot be converted to an FFmpeg pixel format. |
ToSkiaColorType(PixelFormat)
Converts a supported FFmpeg FFmpeg.Images.PixelFormat to its corresponding SKColorType.
Declaration
public static SKColorType ToSkiaColorType(this PixelFormat pixelFormat)
Parameters
| Type | Name | Description |
|---|---|---|
| PixelFormat | pixelFormat | The FFmpeg pixel format to convert. |
Returns
| Type | Description |
|---|---|
| SKColorType | The corresponding Skia color type. |
Remarks
Only pixel formats listed in SKSupportedPixelFormats are supported.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the specified FFmpeg.Images.PixelFormat has no direct SKColorType equivalent. |