Class FFCodec2Skia
Decodes video frames from a media source directly into SKBitmap instances.
Implements
Inherited Members
Namespace: FFmpeg.Skia
Assembly: FFmpeg.Skia.dll
Syntax
public sealed class FFCodec2Skia : IDisposable
Remarks
FFCodec2Skia provides an API similar to SKCodec, but uses FFmpeg
for decoding instead of Skia's built-in image codecs. Frames can be decoded into newly
allocated bitmaps or into an existing SKBitmap supplied by the caller.
If the source pixel format is directly supported by Skia, pixel data is copied without conversion whenever possible. Otherwise, an internal FFmpeg.Images.SwsContext is used to convert the frame into the requested Skia color type. The scaler is kept between frames.
Instances are not thread-safe: call the decoding and seeking methods from one thread at a time.
Properties
View SourceDisplaySize
Gets the size of the decoded images (Info) as displayed, i.e. with Rotation applied (width and height are swapped for 90 and 270 degrees).
Declaration
public SKSizeI DisplaySize { get; }
Property Value
| Type | Description |
|---|---|
| SKSizeI |
Duration
Gets the duration of the video stream.
Declaration
public TimeSpan Duration { get; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
Remarks
If the stream does not store its duration, the duration of the whole media is used; Zero if that is unknown too (e.g. live streams).
FrameRate
Gets the nominal frame rate of the video stream.
Declaration
public Rational FrameRate { get; }
Property Value
| Type | Description |
|---|---|
| Rational |
Remarks
The value is taken from the underlying FFmpeg codec context and may differ from the effective playback rate for variable-frame-rate content.
Frames
Gets the total number of frames reported by the video stream.
Declaration
public long Frames { get; }
Property Value
| Type | Description |
|---|---|
| long |
Remarks
Some container formats do not store the total frame count. In such cases, this value may be zero or estimated by FFmpeg.
Info
Gets the image information describing decoded frames.
Declaration
public SKImageInfo Info { get; }
Property Value
| Type | Description |
|---|---|
| SKImageInfo |
Remarks
The image information specifies the dimensions and color type of the SKBitmap instances produced by this decoder. Its alpha type is Unpremul for color types with alpha (FFmpeg uses straight alpha), otherwise Opaque.
Rotation
Gets the clockwise rotation in degrees (0, 90, 180 or 270) the video must be displayed with, from the stream's display matrix (e.g. a video recorded with a phone held upright).
Declaration
public int Rotation { get; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
The decoded images are not rotated; draw them with DrawRotated(SKCanvas, SKBitmap, SKRect, int, SKSamplingOptions, SKPaint?).
SampleAspectRatio
Gets the sample (pixel) aspect ratio of the video; 1:1 for square pixels.
Declaration
public Rational SampleAspectRatio { get; }
Property Value
| Type | Description |
|---|---|
| Rational |
Remarks
If no width was requested, Info already has the width of square pixels (e.g. 720x480 with 32:27 is decoded as 853x480).
Methods
View SourceCreate(IOContext, DeviceType)
Opens a media source from an FFmpeg.IO.IOContext and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(IOContext io, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| IOContext | io | The custom I/O context used to read the media data. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(IOContext, SKImageInfo, DeviceType)
Opens a media source from an FFmpeg.IO.IOContext and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(IOContext io, SKImageInfo info, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| IOContext | io | The custom I/O context used to read the media data. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(IOContext, SKImageInfo, IDictionary<string, string>, DeviceType)
Opens a media source from an FFmpeg.IO.IOContext and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(IOContext io, SKImageInfo info, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| IOContext | io | The custom I/O context used to read the media data. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(IOContext, IDictionary<string, string>, DeviceType)
Opens a media source from an FFmpeg.IO.IOContext and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(IOContext io, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| IOContext | io | The custom I/O context used to read the media data. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(Stream, DeviceType)
Opens a media source and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(Stream stream, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(Stream, SKImageInfo, DeviceType)
Opens a media source from a Stream and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(Stream stream, SKImageInfo info, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream containing the media data. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(Stream, SKImageInfo, IDictionary<string, string>, DeviceType)
Opens a media source from a Stream and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(Stream stream, SKImageInfo info, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream containing the media data. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(Stream, IDictionary<string, string>, DeviceType)
Opens a media source from a Stream and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(Stream stream, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The stream containing the media data. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(string, DeviceType)
Opens a media source and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(string url, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | The path or URL of the media source to open. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(string, SKImageInfo, DeviceType)
Opens a media source and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(string url, SKImageInfo info, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | The path or URL of the media source to open. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(string, SKImageInfo, IDictionary<string, string>, DeviceType)
Opens a media source and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(string url, SKImageInfo info, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | The path or URL of the media source to open. |
| SKImageInfo | info | Specifies the dimensions and SKColorType of the decoded images. Frames are scaled and converted as necessary to match this image information. A width or height of 0 keeps the size of the video (with square pixels, see SampleAspectRatio), Unknown picks a color type. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Create(string, IDictionary<string, string>, DeviceType)
Opens a media source and creates a new FFCodec2Skia instance for decoding video frames.
Declaration
public static FFCodec2Skia? Create(string url, IDictionary<string, string> codecOptions, DeviceType deviceType = DeviceType.None)
Parameters
| Type | Name | Description |
|---|---|---|
| string | url | The path or URL of the media source to open. |
| IDictionary<string, string> | codecOptions | A collection of codec and format options passed to FFmpeg when opening the media source. The available options depend on the selected demuxer and decoder. |
| DeviceType | deviceType | Specifies the hardware acceleration device to use for decoding. Use FFmpeg.HW.DeviceType.None for software decoding. |
Returns
| Type | Description |
|---|---|
| FFCodec2Skia | A new FFCodec2Skia instance. |
Remarks
The best available video stream is selected automatically. All other streams are discarded during decoding. If the requested image size or color type differs from the source video, frames are converted using FFmpeg's software scaler before being returned.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The media does not contain a video stream, or |
| FFmpegException | The media could not be opened. |
Dispose()
Declaration
public void Dispose()
NextImage(out FFCodecFrameInfo)
Decodes the next video frame into a newly allocated SKBitmap.
Declaration
public SKBitmap? NextImage(out FFCodecFrameInfo frameInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| FFCodecFrameInfo | frameInfo | Receives timing information describing the decoded frame. |
Returns
| Type | Description |
|---|---|
| SKBitmap | A decoded SKBitmap with the size and color type of Info, or null if the end of the video stream has been reached. |
Remarks
The returned bitmap must be disposed by the caller when it is no longer needed.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The decoder has already been disposed. |
| FFmpegException | Reading or decoding failed. |
NextImage(SKBitmap, out FFCodecFrameInfo)
Decodes the next video frame into an existing SKBitmap.
Declaration
public bool NextImage(SKBitmap skImage, out FFCodecFrameInfo frameInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| SKBitmap | skImage | The destination bitmap that receives the decoded frame. If it has no pixels, they are allocated as described by Info. |
| FFCodecFrameInfo | frameInfo | Receives timing information describing the decoded frame. |
Returns
| Type | Description |
|---|---|
| bool | true if a frame was decoded successfully; otherwise, false if the end of the video stream has been reached. |
Remarks
When the bitmap already matches the decoded frame dimensions and color type, the pixel data is copied directly.
Otherwise, the frame is scaled and converted to the bitmap's size and color type.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The decoder has already been disposed. |
| NotSupportedException | The bitmap's color type has no equivalent FFmpeg pixel format. |
| FFmpegException | Reading or decoding failed. |
Restart()
Restarts decoding from the beginning of the video stream.
Declaration
public AVResult32 Restart()
Returns
| Type | Description |
|---|---|
| AVResult32 | An FFmpeg.Utils.AVResult32 indicating whether the operation succeeded. |
Seek(long)
Seeks to the specified frame number.
Declaration
public AVResult32 Seek(long frame)
Parameters
| Type | Name | Description |
|---|---|---|
| long | frame | The zero-based frame index. |
Returns
| Type | Description |
|---|---|
| AVResult32 | An FFmpeg.Utils.AVResult32 indicating whether the seek operation succeeded. |
Remarks
The frame index is converted into a time with FrameRate (and the start time of the stream), which is exact for constant-frame-rate video. If the frame rate is unknown, FFmpeg's frame based seeking is used, which only few formats support.
Seek(TimeSpan)
Seeks to the specified playback position.
Declaration
public AVResult32 Seek(TimeSpan time)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | time | The target playback position, in the timestamps of the stream (see TimeStamp). |
Returns
| Type | Description |
|---|---|
| AVResult32 | An FFmpeg.Utils.AVResult32 indicating whether the seek operation succeeded. |
Remarks
FFmpeg seeks to the key frame before time; the frames in between are decoded but skipped,
so the next call to NextImage(out FFCodecFrameInfo) or
NextImage(SKBitmap, out FFCodecFrameInfo) returns the frame that is visible at
time.