| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using LiveKit.Proto; |
| | | 6 | | using LiveKit.Rtc.Internal; |
| | | 7 | | |
| | | 8 | | namespace LiveKit.Rtc |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a video frame with associated metadata and pixel data. |
| | | 12 | | /// </summary> |
| | | 13 | | public class VideoFrame |
| | | 14 | | { |
| | | 15 | | private readonly int _width; |
| | | 16 | | private readonly int _height; |
| | | 17 | | private readonly Proto.VideoBufferType _type; |
| | | 18 | | private readonly byte[] _data; |
| | | 19 | | private readonly Dictionary<string, object> _userdata; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new VideoFrame instance. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="width">The width of the video frame in pixels.</param> |
| | | 25 | | /// <param name="height">The height of the video frame in pixels.</param> |
| | | 26 | | /// <param name="type">The format type of the video frame data.</param> |
| | | 27 | | /// <param name="data">The raw pixel data for the video frame.</param> |
| | | 28 | | /// <param name="userdata">Optional user data dictionary.</param> |
| | 2001 | 29 | | public VideoFrame( |
| | 2001 | 30 | | int width, |
| | 2001 | 31 | | int height, |
| | 2001 | 32 | | Proto.VideoBufferType type, |
| | 2001 | 33 | | byte[] data, |
| | 2001 | 34 | | Dictionary<string, object>? userdata = null |
| | 2001 | 35 | | ) |
| | 2001 | 36 | | { |
| | 2001 | 37 | | int requiredSize = GetBufferSize(type, width, height); |
| | 2001 | 38 | | if (data.Length < requiredSize) |
| | 0 | 39 | | { |
| | 0 | 40 | | throw new ArgumentException( |
| | 0 | 41 | | $"Data length ({data.Length}) must be >= required size ({requiredSize}) for {type} format", |
| | 0 | 42 | | nameof(data) |
| | 0 | 43 | | ); |
| | | 44 | | } |
| | | 45 | | |
| | 2001 | 46 | | _width = width; |
| | 2001 | 47 | | _height = height; |
| | 2001 | 48 | | _type = type; |
| | 2001 | 49 | | _data = data; |
| | 2001 | 50 | | _userdata = userdata ?? new Dictionary<string, object>(); |
| | 2001 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Creates a new empty VideoFrame with the specified parameters. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="width">Width in pixels.</param> |
| | | 57 | | /// <param name="height">Height in pixels.</param> |
| | | 58 | | /// <param name="type">Pixel format of the buffer.</param> |
| | | 59 | | /// <returns>A frame of that size and format with a zeroed buffer.</returns> |
| | | 60 | | public static VideoFrame Create(int width, int height, VideoBufferType type) |
| | 720 | 61 | | { |
| | 720 | 62 | | int size = GetBufferSize(type, width, height); |
| | 720 | 63 | | var data = new byte[size]; |
| | 720 | 64 | | return new VideoFrame(width, height, type, data); |
| | 720 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a VideoFrame from an owned FFI buffer. |
| | | 69 | | /// </summary> |
| | | 70 | | internal static VideoFrame FromOwnedInfo(OwnedVideoBuffer ownedInfo) |
| | 930 | 71 | | { |
| | | 72 | | // Own the native buffer first, so it is released even if the copy below throws |
| | 930 | 73 | | using var handle = FfiHandle.FromId(ownedInfo.Handle.Id); |
| | | 74 | | |
| | 930 | 75 | | var info = ownedInfo.Info; |
| | 930 | 76 | | int dataLen = GetBufferSize( |
| | 930 | 77 | | (VideoBufferType)info.Type, |
| | 930 | 78 | | (int)info.Width, |
| | 930 | 79 | | (int)info.Height |
| | 930 | 80 | | ); |
| | | 81 | | |
| | | 82 | | // Copy the pixels from native memory. This runs on the FFI callback thread, so it |
| | | 83 | | // must be a block copy, not a byte loop (a 1080p frame is 3 MB). |
| | 930 | 84 | | var data = new byte[dataLen]; |
| | 930 | 85 | | if (dataLen > 0) |
| | 930 | 86 | | { |
| | | 87 | | unsafe |
| | 930 | 88 | | { |
| | 930 | 89 | | fixed (byte* destination = data) |
| | 930 | 90 | | { |
| | 930 | 91 | | Buffer.MemoryCopy((void*)info.DataPtr, destination, dataLen, dataLen); |
| | 930 | 92 | | } |
| | 930 | 93 | | } |
| | 930 | 94 | | } |
| | | 95 | | |
| | 930 | 96 | | return new VideoFrame( |
| | 930 | 97 | | (int)info.Width, |
| | 930 | 98 | | (int)info.Height, |
| | 930 | 99 | | (VideoBufferType)info.Type, |
| | 930 | 100 | | data |
| | 930 | 101 | | ); |
| | 930 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Pins the pixels while a synchronous FFI operation consumes the buffer. |
| | | 106 | | /// The callback must not retain the buffer or plane pointers after returning. |
| | | 107 | | /// </summary> |
| | | 108 | | internal TResult WithPinnedBuffer<TResult>(Func<VideoBufferInfo, TResult> callback) |
| | 3780 | 109 | | { |
| | | 110 | | unsafe |
| | 3780 | 111 | | { |
| | 3780 | 112 | | fixed (byte* ptr = _data) |
| | 3780 | 113 | | { |
| | 3780 | 114 | | var info = new VideoBufferInfo |
| | 3780 | 115 | | { |
| | 3780 | 116 | | Width = (uint)_width, |
| | 3780 | 117 | | Height = (uint)_height, |
| | 3780 | 118 | | Type = (LiveKit.Proto.VideoBufferType)_type, |
| | 3780 | 119 | | DataPtr = (ulong)ptr, |
| | 3780 | 120 | | Stride = (uint)GetStride(_type, _width), |
| | 3780 | 121 | | }; |
| | | 122 | | |
| | | 123 | | // Add component infos |
| | 3780 | 124 | | var components = GetPlaneInfos((ulong)ptr, _type, _width, _height); |
| | 29902 | 125 | | foreach (var component in components) |
| | 9281 | 126 | | { |
| | 9281 | 127 | | info.Components.Add(component); |
| | 9281 | 128 | | } |
| | | 129 | | |
| | 3780 | 130 | | return callback(info); |
| | | 131 | | } |
| | | 132 | | } |
| | 3779 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary>Gets the width of the video frame in pixels.</summary> |
| | 327 | 136 | | public int Width => _width; |
| | | 137 | | |
| | | 138 | | /// <summary>Gets the height of the video frame in pixels.</summary> |
| | 327 | 139 | | public int Height => _height; |
| | | 140 | | |
| | | 141 | | /// <summary>Gets the format type of the video frame.</summary> |
| | 15 | 142 | | public VideoBufferType Type => _type; |
| | | 143 | | |
| | | 144 | | /// <summary>Gets the raw pixel data.</summary> |
| | 0 | 145 | | public ReadOnlySpan<byte> Data => _data; |
| | | 146 | | |
| | | 147 | | /// <summary>Gets the raw pixel data as a mutable span.</summary> |
| | 61 | 148 | | public Span<byte> DataMutable => _data; |
| | | 149 | | |
| | | 150 | | /// <summary>Gets the raw pixel data as a byte array.</summary> |
| | 280855 | 151 | | public byte[] DataBytes => _data; |
| | | 152 | | |
| | | 153 | | /// <summary>Gets the user data associated with the frame.</summary> |
| | 0 | 154 | | public Dictionary<string, object> Userdata => _userdata; |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets a specific plane from the video frame data. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="planeIndex">The index of the plane (0-based).</param> |
| | | 160 | | /// <returns>The plane data, or null if the index is out of bounds.</returns> |
| | | 161 | | public byte[]? GetPlane(int planeIndex) |
| | 62 | 162 | | { |
| | 62 | 163 | | var planeInfos = GetPlaneInfosLocal(_type, _width, _height); |
| | 62 | 164 | | if (planeIndex >= planeInfos.Count) |
| | 0 | 165 | | return null; |
| | | 166 | | |
| | 62 | 167 | | var planeInfo = planeInfos[planeIndex]; |
| | 62 | 168 | | var planeData = new byte[planeInfo.Size]; |
| | 62 | 169 | | Array.Copy(_data, planeInfo.Offset, planeData, 0, planeInfo.Size); |
| | 62 | 170 | | return planeData; |
| | 62 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Converts the video frame to a different format type. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="targetType">The target format type.</param> |
| | | 177 | | /// <param name="flipY">If true, flip the frame vertically.</param> |
| | | 178 | | /// <returns>A new VideoFrame in the target format.</returns> |
| | | 179 | | public VideoFrame Convert(VideoBufferType targetType, bool flipY = false) |
| | 852 | 180 | | { |
| | | 181 | | // The FFI reads the pixels synchronously, before SendRequest returns and the result |
| | | 182 | | // is an independent native buffer, so the pin only needs to cover the request. |
| | 852 | 183 | | var response = WithPinnedBuffer(buffer => |
| | 852 | 184 | | { |
| | 852 | 185 | | // Unlike capture (and libwebrtc), the FFI's I010 converter reads each plane as a u16 |
| | 852 | 186 | | // slice of size / 2 samples and requires samples >= stride * rows, so it expects |
| | 852 | 187 | | // strides in samples rather than bytes. |
| | 852 | 188 | | if (_type == VideoBufferType.I010) |
| | 2 | 189 | | { |
| | 18 | 190 | | foreach (var component in buffer.Components) |
| | 6 | 191 | | component.Stride /= 2; |
| | 2 | 192 | | } |
| | 852 | 193 | | |
| | 852 | 194 | | return FfiClient.Instance.SendRequest( |
| | 852 | 195 | | new FfiRequest |
| | 852 | 196 | | { |
| | 852 | 197 | | VideoConvert = new VideoConvertRequest |
| | 852 | 198 | | { |
| | 852 | 199 | | FlipY = flipY, |
| | 852 | 200 | | DstType = (LiveKit.Proto.VideoBufferType)targetType, |
| | 852 | 201 | | Buffer = buffer, |
| | 852 | 202 | | }, |
| | 852 | 203 | | } |
| | 852 | 204 | | ); |
| | 1704 | 205 | | }); |
| | | 206 | | |
| | 852 | 207 | | if (!string.IsNullOrEmpty(response.VideoConvert?.Error)) |
| | 0 | 208 | | { |
| | 0 | 209 | | throw new InvalidOperationException( |
| | 0 | 210 | | $"Video conversion failed: {response.VideoConvert.Error}" |
| | 0 | 211 | | ); |
| | | 212 | | } |
| | | 213 | | |
| | 852 | 214 | | return FromOwnedInfo(response.VideoConvert!.Buffer); |
| | 852 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Creates a copy of this video frame. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <returns>A deep copy of the frame, with its own pixel buffer.</returns> |
| | | 221 | | public VideoFrame Clone() |
| | 0 | 222 | | { |
| | 0 | 223 | | var dataCopy = new byte[_data.Length]; |
| | 0 | 224 | | Array.Copy(_data, dataCopy, _data.Length); |
| | 0 | 225 | | return new VideoFrame( |
| | 0 | 226 | | _width, |
| | 0 | 227 | | _height, |
| | 0 | 228 | | _type, |
| | 0 | 229 | | dataCopy, |
| | 0 | 230 | | new Dictionary<string, object>(_userdata) |
| | 0 | 231 | | ); |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <inheritdoc /> |
| | | 235 | | public override string ToString() |
| | 0 | 236 | | { |
| | 0 | 237 | | return $"VideoFrame(width={_width}, height={_height}, type={_type})"; |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | #region Static Helpers |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Gets the buffer size in bytes for a video buffer type. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <param name="type">Pixel format of the buffer.</param> |
| | | 246 | | /// <param name="width">Width in pixels.</param> |
| | | 247 | | /// <param name="height">Height in pixels.</param> |
| | | 248 | | /// <returns>Number of bytes a frame of that format and size occupies.</returns> |
| | | 249 | | public static int GetBufferSize(Proto.VideoBufferType type, int width, int height) |
| | 3651 | 250 | | { |
| | 3651 | 251 | | switch (type) |
| | | 252 | | { |
| | | 253 | | case Proto.VideoBufferType.Argb: |
| | | 254 | | case Proto.VideoBufferType.Abgr: |
| | | 255 | | case Proto.VideoBufferType.Rgba: |
| | | 256 | | case Proto.VideoBufferType.Bgra: |
| | 1471 | 257 | | return width * height * 4; |
| | | 258 | | |
| | | 259 | | case Proto.VideoBufferType.Rgb24: |
| | 0 | 260 | | return width * height * 3; |
| | | 261 | | |
| | | 262 | | case Proto.VideoBufferType.I420: |
| | 2134 | 263 | | { |
| | 2134 | 264 | | int chromaWidth = (width + 1) / 2; |
| | 2134 | 265 | | int chromaHeight = (height + 1) / 2; |
| | 2134 | 266 | | return width * height + chromaWidth * chromaHeight * 2; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | case VideoBufferType.I420A: |
| | 10 | 270 | | { |
| | 10 | 271 | | int chromaWidth = (width + 1) / 2; |
| | 10 | 272 | | int chromaHeight = (height + 1) / 2; |
| | 10 | 273 | | return width * height * 2 + chromaWidth * chromaHeight * 2; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | case VideoBufferType.I422: |
| | 10 | 277 | | { |
| | 10 | 278 | | int chromaWidth = (width + 1) / 2; |
| | 10 | 279 | | return width * height + chromaWidth * height * 2; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | case VideoBufferType.I444: |
| | 10 | 283 | | return width * height * 3; |
| | | 284 | | |
| | | 285 | | case VideoBufferType.I010: |
| | 10 | 286 | | { |
| | 10 | 287 | | int chromaWidth = (width + 1) / 2; |
| | 10 | 288 | | int chromaHeight = (height + 1) / 2; |
| | 10 | 289 | | return width * height * 2 + chromaWidth * chromaHeight * 4; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | case VideoBufferType.Nv12: |
| | 6 | 293 | | { |
| | 6 | 294 | | int chromaWidth = (width + 1) / 2; |
| | 6 | 295 | | int chromaHeight = (height + 1) / 2; |
| | 6 | 296 | | return width * height + chromaWidth * chromaHeight * 2; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | default: |
| | 0 | 300 | | throw new ArgumentException($"Unsupported video buffer type: {type}"); |
| | | 301 | | } |
| | 3651 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Gets the stride for a video buffer type. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <param name="type">Pixel format of the buffer.</param> |
| | | 308 | | /// <param name="width">Width in pixels.</param> |
| | | 309 | | /// <returns>Number of bytes per row of the first plane.</returns> |
| | | 310 | | public static int GetStride(VideoBufferType type, int width) |
| | 3780 | 311 | | { |
| | 3780 | 312 | | switch (type) |
| | | 313 | | { |
| | | 314 | | case VideoBufferType.Argb: |
| | | 315 | | case VideoBufferType.Abgr: |
| | | 316 | | case VideoBufferType.Rgba: |
| | | 317 | | case VideoBufferType.Bgra: |
| | 687 | 318 | | return width * 4; |
| | | 319 | | |
| | | 320 | | case VideoBufferType.Rgb24: |
| | 0 | 321 | | return width * 3; |
| | | 322 | | |
| | | 323 | | default: |
| | 3093 | 324 | | return 0; // Planar formats don't have a single stride |
| | | 325 | | } |
| | 3780 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Describes the planes of a buffer starting at <paramref name="addr"/> the way the FFI |
| | | 330 | | /// expects them. Derived from <see cref="GetPlaneInfosLocal"/> so both always agree. |
| | | 331 | | /// Packed formats have no planes. |
| | | 332 | | /// </summary> |
| | | 333 | | private static List<VideoBufferInfo.Types.ComponentInfo> GetPlaneInfos( |
| | | 334 | | ulong addr, |
| | | 335 | | VideoBufferType type, |
| | | 336 | | int width, |
| | | 337 | | int height |
| | | 338 | | ) |
| | 3780 | 339 | | { |
| | 3780 | 340 | | var result = new List<VideoBufferInfo.Types.ComponentInfo>(); |
| | 29902 | 341 | | foreach (var plane in GetPlaneInfosLocal(type, width, height)) |
| | 9281 | 342 | | { |
| | 9281 | 343 | | result.Add( |
| | 9281 | 344 | | new VideoBufferInfo.Types.ComponentInfo |
| | 9281 | 345 | | { |
| | 9281 | 346 | | DataPtr = addr + (ulong)plane.Offset, |
| | 9281 | 347 | | Stride = (uint)plane.Stride, |
| | 9281 | 348 | | Size = (uint)plane.Size, |
| | 9281 | 349 | | } |
| | 9281 | 350 | | ); |
| | 9281 | 351 | | } |
| | 3780 | 352 | | return result; |
| | 3780 | 353 | | } |
| | | 354 | | |
| | | 355 | | private struct PlaneInfo |
| | | 356 | | { |
| | | 357 | | public int Offset; |
| | | 358 | | public int Size; |
| | | 359 | | public int Stride; |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | private static List<PlaneInfo> GetPlaneInfosLocal( |
| | | 363 | | VideoBufferType type, |
| | | 364 | | int width, |
| | | 365 | | int height |
| | | 366 | | ) |
| | 3842 | 367 | | { |
| | 3842 | 368 | | var result = new List<PlaneInfo>(); |
| | | 369 | | |
| | 3842 | 370 | | switch (type) |
| | | 371 | | { |
| | | 372 | | case VideoBufferType.I420: |
| | 3102 | 373 | | { |
| | 3102 | 374 | | int chromaWidth = (width + 1) / 2; |
| | 3102 | 375 | | int chromaHeight = (height + 1) / 2; |
| | | 376 | | |
| | 3102 | 377 | | int ySize = width * height; |
| | 3102 | 378 | | int uvSize = chromaWidth * chromaHeight; |
| | | 379 | | |
| | 3102 | 380 | | result.Add( |
| | 3102 | 381 | | new PlaneInfo |
| | 3102 | 382 | | { |
| | 3102 | 383 | | Offset = 0, |
| | 3102 | 384 | | Size = ySize, |
| | 3102 | 385 | | Stride = width, |
| | 3102 | 386 | | } |
| | 3102 | 387 | | ); |
| | 3102 | 388 | | result.Add( |
| | 3102 | 389 | | new PlaneInfo |
| | 3102 | 390 | | { |
| | 3102 | 391 | | Offset = ySize, |
| | 3102 | 392 | | Size = uvSize, |
| | 3102 | 393 | | Stride = chromaWidth, |
| | 3102 | 394 | | } |
| | 3102 | 395 | | ); |
| | 3102 | 396 | | result.Add( |
| | 3102 | 397 | | new PlaneInfo |
| | 3102 | 398 | | { |
| | 3102 | 399 | | Offset = ySize + uvSize, |
| | 3102 | 400 | | Size = uvSize, |
| | 3102 | 401 | | Stride = chromaWidth, |
| | 3102 | 402 | | } |
| | 3102 | 403 | | ); |
| | 3102 | 404 | | break; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | case VideoBufferType.I420A: |
| | 13 | 408 | | { |
| | 13 | 409 | | int chromaWidth = (width + 1) / 2; |
| | 13 | 410 | | int chromaHeight = (height + 1) / 2; |
| | | 411 | | |
| | 13 | 412 | | int ySize = width * height; |
| | 13 | 413 | | int uvSize = chromaWidth * chromaHeight; |
| | | 414 | | |
| | 13 | 415 | | result.Add( |
| | 13 | 416 | | new PlaneInfo |
| | 13 | 417 | | { |
| | 13 | 418 | | Offset = 0, |
| | 13 | 419 | | Size = ySize, |
| | 13 | 420 | | Stride = width, |
| | 13 | 421 | | } |
| | 13 | 422 | | ); |
| | 13 | 423 | | result.Add( |
| | 13 | 424 | | new PlaneInfo |
| | 13 | 425 | | { |
| | 13 | 426 | | Offset = ySize, |
| | 13 | 427 | | Size = uvSize, |
| | 13 | 428 | | Stride = chromaWidth, |
| | 13 | 429 | | } |
| | 13 | 430 | | ); |
| | 13 | 431 | | result.Add( |
| | 13 | 432 | | new PlaneInfo |
| | 13 | 433 | | { |
| | 13 | 434 | | Offset = ySize + uvSize, |
| | 13 | 435 | | Size = uvSize, |
| | 13 | 436 | | Stride = chromaWidth, |
| | 13 | 437 | | } |
| | 13 | 438 | | ); |
| | 13 | 439 | | result.Add( |
| | 13 | 440 | | new PlaneInfo |
| | 13 | 441 | | { |
| | 13 | 442 | | Offset = ySize + uvSize * 2, |
| | 13 | 443 | | Size = ySize, |
| | 13 | 444 | | Stride = width, |
| | 13 | 445 | | } |
| | 13 | 446 | | ); |
| | 13 | 447 | | break; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | case VideoBufferType.I422: |
| | 11 | 451 | | { |
| | 11 | 452 | | int chromaWidth = (width + 1) / 2; |
| | | 453 | | |
| | 11 | 454 | | int ySize = width * height; |
| | 11 | 455 | | int uvSize = chromaWidth * height; |
| | | 456 | | |
| | 11 | 457 | | result.Add( |
| | 11 | 458 | | new PlaneInfo |
| | 11 | 459 | | { |
| | 11 | 460 | | Offset = 0, |
| | 11 | 461 | | Size = ySize, |
| | 11 | 462 | | Stride = width, |
| | 11 | 463 | | } |
| | 11 | 464 | | ); |
| | 11 | 465 | | result.Add( |
| | 11 | 466 | | new PlaneInfo |
| | 11 | 467 | | { |
| | 11 | 468 | | Offset = ySize, |
| | 11 | 469 | | Size = uvSize, |
| | 11 | 470 | | Stride = chromaWidth, |
| | 11 | 471 | | } |
| | 11 | 472 | | ); |
| | 11 | 473 | | result.Add( |
| | 11 | 474 | | new PlaneInfo |
| | 11 | 475 | | { |
| | 11 | 476 | | Offset = ySize + uvSize, |
| | 11 | 477 | | Size = uvSize, |
| | 11 | 478 | | Stride = chromaWidth, |
| | 11 | 479 | | } |
| | 11 | 480 | | ); |
| | 11 | 481 | | break; |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | case VideoBufferType.I444: |
| | 11 | 485 | | { |
| | 11 | 486 | | int planeSize = width * height; |
| | 11 | 487 | | result.Add( |
| | 11 | 488 | | new PlaneInfo |
| | 11 | 489 | | { |
| | 11 | 490 | | Offset = 0, |
| | 11 | 491 | | Size = planeSize, |
| | 11 | 492 | | Stride = width, |
| | 11 | 493 | | } |
| | 11 | 494 | | ); |
| | 11 | 495 | | result.Add( |
| | 11 | 496 | | new PlaneInfo |
| | 11 | 497 | | { |
| | 11 | 498 | | Offset = planeSize, |
| | 11 | 499 | | Size = planeSize, |
| | 11 | 500 | | Stride = width, |
| | 11 | 501 | | } |
| | 11 | 502 | | ); |
| | 11 | 503 | | result.Add( |
| | 11 | 504 | | new PlaneInfo |
| | 11 | 505 | | { |
| | 11 | 506 | | Offset = planeSize * 2, |
| | 11 | 507 | | Size = planeSize, |
| | 11 | 508 | | Stride = width, |
| | 11 | 509 | | } |
| | 11 | 510 | | ); |
| | 11 | 511 | | break; |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | case VideoBufferType.I010: |
| | 11 | 515 | | { |
| | | 516 | | // 16-bit samples: strides and sizes are in bytes. |
| | 11 | 517 | | int chromaWidth = (width + 1) / 2; |
| | 11 | 518 | | int chromaHeight = (height + 1) / 2; |
| | | 519 | | |
| | 11 | 520 | | int ySize = width * height * 2; |
| | 11 | 521 | | int uvSize = chromaWidth * chromaHeight * 2; |
| | | 522 | | |
| | 11 | 523 | | result.Add( |
| | 11 | 524 | | new PlaneInfo |
| | 11 | 525 | | { |
| | 11 | 526 | | Offset = 0, |
| | 11 | 527 | | Size = ySize, |
| | 11 | 528 | | Stride = width * 2, |
| | 11 | 529 | | } |
| | 11 | 530 | | ); |
| | 11 | 531 | | result.Add( |
| | 11 | 532 | | new PlaneInfo |
| | 11 | 533 | | { |
| | 11 | 534 | | Offset = ySize, |
| | 11 | 535 | | Size = uvSize, |
| | 11 | 536 | | Stride = chromaWidth * 2, |
| | 11 | 537 | | } |
| | 11 | 538 | | ); |
| | 11 | 539 | | result.Add( |
| | 11 | 540 | | new PlaneInfo |
| | 11 | 541 | | { |
| | 11 | 542 | | Offset = ySize + uvSize, |
| | 11 | 543 | | Size = uvSize, |
| | 11 | 544 | | Stride = chromaWidth * 2, |
| | 11 | 545 | | } |
| | 11 | 546 | | ); |
| | 11 | 547 | | break; |
| | | 548 | | } |
| | | 549 | | |
| | | 550 | | case VideoBufferType.Nv12: |
| | 7 | 551 | | { |
| | 7 | 552 | | int chromaWidth = (width + 1) / 2; |
| | 7 | 553 | | int chromaHeight = (height + 1) / 2; |
| | | 554 | | |
| | 7 | 555 | | int ySize = width * height; |
| | 7 | 556 | | int uvSize = chromaWidth * chromaHeight * 2; |
| | | 557 | | |
| | 7 | 558 | | result.Add( |
| | 7 | 559 | | new PlaneInfo |
| | 7 | 560 | | { |
| | 7 | 561 | | Offset = 0, |
| | 7 | 562 | | Size = ySize, |
| | 7 | 563 | | Stride = width, |
| | 7 | 564 | | } |
| | 7 | 565 | | ); |
| | 7 | 566 | | result.Add( |
| | 7 | 567 | | new PlaneInfo |
| | 7 | 568 | | { |
| | 7 | 569 | | Offset = ySize, |
| | 7 | 570 | | Size = uvSize, |
| | 7 | 571 | | Stride = chromaWidth * 2, |
| | 7 | 572 | | } |
| | 7 | 573 | | ); |
| | 7 | 574 | | break; |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | 3842 | 578 | | return result; |
| | 3842 | 579 | | } |
| | | 580 | | |
| | | 581 | | #endregion |
| | | 582 | | } |
| | | 583 | | } |