Skip to content
This repository was archived by the owner on Jun 28, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ PublishScripts/
*.nuget.props
*.nuget.targets

# NuGet executable
.nuget/nuget.exe

# Microsoft Azure Build Output
csx/
*.build.csdef
Expand Down
20 changes: 19 additions & 1 deletion src/VpxVideoEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public byte[] EncodeVideo(int width, int height, byte[] sample, VideoPixelFormat
}
else
{
int stride = pixelFormat == VideoPixelFormatsEnum.Bgra ? width * 4 : width * 3;
int stride = GetStride(width, pixelFormat);
var i420Buffer = PixelConverter.ToI420(width, height, stride, sample, pixelFormat);
encodedBuffer = _vp8Encoder.Encode(i420Buffer, vpxmd.VpxImgFmt.VPX_IMG_FMT_I420, _forceKeyFrame);
}
Expand Down Expand Up @@ -135,6 +135,24 @@ public IEnumerable<VideoSample> DecodeVideo(byte[] frame, VideoPixelFormatsEnum
}
}

private static int RoundUp(int value, int to) => (value + to - 1) / to * to;

private static int BytesPerPixel(VideoPixelFormatsEnum pixelFormat) => pixelFormat switch
{
VideoPixelFormatsEnum.Bgra => 4,
VideoPixelFormatsEnum.Rgba => 4,
VideoPixelFormatsEnum.Rgb => 3,
VideoPixelFormatsEnum.Bgr => 3,
_ => throw new ArgumentException($"Unsupported pixel format for bytes-per-pixel calculation: {pixelFormat}", nameof(pixelFormat))
};

/// <summary>
/// Calculates the row stride in bytes for the given width and pixel format,
/// rounding up to the nearest 4-byte boundary as required by most bitmap formats.
/// </summary>
private static int GetStride(int width, VideoPixelFormatsEnum pixelFormat) =>
RoundUp(value: width * BytesPerPixel(pixelFormat), to: 4);

public void Dispose()
{
_vp8Encoder?.Dispose();
Expand Down