SIPSorcery.VP9 10.0.20-pre

Prefix Reserved
This is a prerelease version of SIPSorcery.VP9.
dotnet add package SIPSorcery.VP9 --version 10.0.20-pre
                    
NuGet\Install-Package SIPSorcery.VP9 -Version 10.0.20-pre
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SIPSorcery.VP9" Version="10.0.20-pre" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SIPSorcery.VP9" Version="10.0.20-pre" />
                    
Directory.Packages.props
<PackageReference Include="SIPSorcery.VP9" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SIPSorcery.VP9 --version 10.0.20-pre
                    
#r "nuget: SIPSorcery.VP9, 10.0.20-pre"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SIPSorcery.VP9@10.0.20-pre
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SIPSorcery.VP9&version=10.0.20-pre&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SIPSorcery.VP9&version=10.0.20-pre&prerelease
                    
Install as a Cake Tool

SIPSorcery.VP9

A pure C# VP9 video codec for the SIPSorcery real-time communications library, ported from the WebM project's libvpx (v1.14.0, commit d1186e5b1). No native binaries, no PInvoke.

VP9 is royalty free: libvpx is BSD licensed and comes with Google's patent grant, reproduced here in LIBVPX_LICENSE and LIBVPX_PATENTS.

Status

The decoder and the realtime encoder work, byte for byte as libvpx does, apart from two deliberate changes to the encoder (see Changes from libvpx). The port is checked against a C-only build of the same libvpx commit with the same configuration and the same changes (see thirdparty/libvpx-vp9-dotnet-port).

Path State
Decoder (profiles 0 and 1, 8-bit) Done. All 49 corpus streams (651 frames) decode to the C decoder's MD5 on every frame: libvpx's good-quality, best, two-pass, alt-ref and realtime encoders, 4:2:0, 4:2:2, 4:4:0 and 4:4:4, lossless, odd sizes down to 1x1, tiles, resizing, error resilient and frame parallel modes, temporal and spatial layers; the same on 4 threads. Chromium's live VP9 decodes.
Encoder (libvpx's realtime one-pass mode) Done. Every packet of 50 scripted encodes (891 packets) matches the C encoder's bytes: speeds 5 to 9, CBR, VBR, CQ and fixed Q, cyclic refresh and no AQ, screen content, key frame requests and intervals, bitrate and size changes, dynamic resize, frame dropping, error resilience, tiles, lossless, profile 1 (4:2:2, 4:4:0, 4:4:4), 2 and 3 temporal layers and 2 or 3 spatial layers, runs of 60 to 120 frames, sizes up to 1920x1080, flat cells swapping black and white, and row based multithreading on 3 to 8 threads. Every packet decodes with the ported decoder. Threading tile columns alone, it writes the packets it does on one thread; with row based multithreading, the same packets on any number of threads from 2. Chromium decodes its stream live.
Kernels The 7032 kernel fixtures (transforms, intra predictors, loop filters, convolutions, quantisers, SAD and variance, averages and hadamards) match the C kernels' outputs, and each SIMD kernel matches its C kernel on random and edge-case inputs.

Not ported, because the realtime configuration leaves them out: libvpx's good-quality and two-pass encoding, the alt-ref and temporal filters, high bit depth (10 and 12 bit) and the postprocessor. libvpx's own SIMD code (intrinsics and assembly) isn't ported either: the port has C# SIMD kernels of its own (see Speed).

Changes from libvpx

Two things libvpx's encoder does are changed, on purpose. The changes are made in the C (the port's tools/fixes.py), and the reference build the tests compare against has them too.

  • Blocks whose changes cancel. In CBR, libvpx's realtime mode decision for blocks of 32x32 and up (model_rd_for_sb_y_large in vp9_pickmode.c) takes a transform block's DC as zero when it's under the quantiser's threshold, or when the residual over the whole block sums to nearly nothing (|| sse == var). When two flat areas of one block change by equal and opposite amounts, as a barcode's cells do when one turns white and the other black, the sum cancels: the block is coded as skipped and keeps the previous frame's pixels. Chromium's VP9 encoder does the same. The port tests each transform block's DC alone, so those blocks are coded, at the same bitrate and speed.
  • The motion search from a scaled reference. For a reference of another size (the frames after a size change, and a spatial layer's reference to the layer below), libvpx skips the step that picks which candidate vector starts a block's motion search (vp9_mv_pred), and starts from whatever the last block to take that step left. With row based multithreading that depends on which thread encoded which rows, so the frame after a resize came out one of two ways from run to run. The port starts from the nearest vector, as the step does when it finds nothing better.

Without threads, every other encoder script gives the same bytes as upstream libvpx.

Using the decoder

VP9NetVideoEndPoint is a SIPSorcery video sink. Give it the frames an RTCPeerConnection assembles and it raises BGR pictures:

var videoSink = new VP9NetVideoEndPoint();
videoSink.OnVideoSinkDecodedSample += (bgr, width, height, stride, format) => { /* display */ };

var pc = new RTCPeerConnection();
pc.addTrack(new MediaStreamTrack(videoSink.GetVideoSinkFormats(), MediaStreamStatusEnum.RecvOnly));
pc.OnVideoFrameReceived += videoSink.GotVideoFrame;
pc.OnVideoFormatsNegotiated += formats => videoSink.SetVideoSinkFormat(formats.First());

Handle OnKeyFrameRequired by sending the sender an RTCP PLI. The sink raises it when a frame doesn't decode, which is what happens when it joins a stream after its key frame or a frame was lost: libvpx then refuses frames until the next key frame, and browsers send one only when asked. The WebRTCClientCodecs example does all of this when its page picks vp9.net.

VP9Codec is the IVideoEncoder underneath, for decoding without an endpoint. DecodeVideo takes one VP9 frame (a superframe, for layered streams) and returns the picture it shows as I420, BGR or RGB. DecodeVideoFaster returns the same in a buffer the codec re-uses. Profile 1 streams have their chroma point sampled down to 4:2:0.

Using the encoder

VP9Codec is also the encoder, so it can go into any SIPSorcery video source that takes an IVideoEncoder:

var vp9Codec = new VP9Codec(targetBitrateKbps: 1000);
var testPattern = new VideoTestPatternSource(vp9Codec);

var pc = new RTCPeerConnection();
pc.addTrack(new MediaStreamTrack(testPattern.GetVideoSourceFormats(), MediaStreamStatusEnum.SendOnly));
testPattern.OnVideoSourceEncodedSample += pc.SendVideo;
pc.OnVideoFormatsNegotiated += formats => testPattern.SetVideoSourceFormat(formats.First());

It encodes I420, BGRA, RGBA, BGR and RGB frames to one VP9 frame each, set up as libwebrtc sets up libvpx for a single layer: realtime one-pass CBR, cyclic refresh AQ, no frame dropping, profile 0. The constructor takes the target bitrate, the speed (libvpx's cpu-used, 5 to 9, 7 by default as libwebrtc uses for most sizes), the most frames between key frames and the threads (see Speed). TargetBitrateKbps can change at any time. A new frame size starts a new stream with a key frame. SIPSorcery passes no timestamps, so frames are timed by when they arrive, and the rate control follows the source's real frame rate.

SIPSorcery doesn't answer a receiver's key frame request on its own: call ForceKeyFrame when RTCPeerConnection.OnReceiveReport brings an RTCP PLI or FIR. The WebRTCGetStartedCodecs example does this when VP9 from vp9.net is picked on its page.

The underlying libvpx API is ported too (vpx_codec_enc_init_ver, vpx_codec_encode, every realtime control including the SVC ones, vpx_codec_decode, ...), internal for now: the tests drive it directly.

Speed

The hot kernels have SIMD forms (simd/, C# Vector128: SSE2 on x64, AdvSimd on Arm64, with SSSE3 and AVX2 where they help): the convolutions, SAD, variance and sub-pixel variance, the quantisers, the loop filters, the encoder's averages and hadamards, the 4x4 and 8x8 transforms and the intra predictors. They give exactly what the C kernels give. The SIPSorcery.VP9.DisableSimd AppContext switch turns them off.

libvpx's worker threads run on threads too. The encoder's threads share out the superblock rows of each tile (row based multithreading, as libwebrtc has it), each row a superblock or two behind the one above. They write the tiles of the bitstream side by side, and filter the frame's superblock rows on every thread. With threads to spare (8 at 1080p) the encoder writes the bitstream while it filters the frame. The decoder decodes a stream's tile columns side by side and filters on its threads.

1080p at speed 7 on an i9-10900 (10 cores, 20 logical processors): camera-like frames (a textured background panning at a sub-pixel rate, a block crossing it and sensor noise) at about 3 Mbit/s, decoded from a stream the encoder wrote on the same threads (up to 4) (benchmarks/SIPSorcery.VP9). The best of several runs; on this machine, a desktop in use, runs of the same set-up varied by up to a third.

A frame C kernels, 1 thread SIMD, 1 thread 2 threads 4 threads 8 threads 16 threads
Encode 315 ms 42.0 ms 25.8 ms 13.7 ms (73 fps) 10.9 ms (92 fps) 9.9 ms (101 fps)
Decode 62.3 ms 9.7 ms 5.6 ms 3.2 ms

With 4 threads, row based multithreading and a thread per tile column (4 at 1080p) are about as fast as each other. Row based multithreading is what lets more threads than tile columns help. 720p encodes in 20.7 ms on one thread and 6.8 ms on 4; 2560x1440 in 14.7 ms on 16.

VP9Codec picks the encoder's threads by frame size unless told: 2 from 640x360, 4 from 1280x720, 8 from 1920x1080 and 16 from 2560x1440, no more than the processors, else 1. It turns row based multithreading on with more than one thread, and asks for a tile column a thread, as many as the width allows (columns are at least 256 pixels wide: 4 at 720p and 1080p, 8 from 1440p). libwebrtc stops at 4 threads for native libvpx. The port's threads are slower than libvpx's, and the library is mostly for client machines, where the one encode's latency counts. More than 8 threads at 1080p is a little faster for much more processor time. The decoder gets up to 4 threads, as many as a stream has tile columns. The first few frames are slow (up to a few hundred ms each) while the JIT compiles the encoder.

Live, in TranscodeLoop with a browser's 1080p30 VP9 test card in and out, VP9Codec decoded a frame in 13 to 14 ms (to BGR) and encoded one in 18 to 20 ms (p95 under 23 ms) on 4 threads, measured before row based multithreading and the newer kernels, so it keeps up. With the change for blocks whose changes cancel, page B reads the test card's strip in every frame, and 40 s runs at 1080p (VP8 or VP9 in) and a 30 s run at 720p pass (encode p50 14.8 ms at 1080p, 9.7 ms at 720p). Before it, page B couldn't read tens to hundreds of frames a run, as with Chromium's own libvpx encoder set up the same way.

Port conventions

  • File for file. Each .cs file ports one libvpx source file (and its header) and keeps its name, and the ported code keeps libvpx's function and type names, so it can be read side by side with the C.
  • Generated, then patched. The C is pruned to the port's configuration and translated by the tools in thirdparty/libvpx-vp9-dotnet-port/refharness/tools; every fix is made in the tools, so the whole port regenerates from the C. Only VP9Codec, VP9NetVideoEndPoint and the files under vpx_ports that stand in for the C runtime are written by hand.
  • Native memory. Codec state is unmanaged structs in native memory (vpx_mem), reached through pointers as upstream does, so the GC never moves or scans it. Dispose VP9Codec to free it.
  • C semantics. Integer promotion, wrapping arithmetic and double operation order follow the C exactly, which is what keeps the encoder's rate control and mode decisions byte-exact.

Tests

  • test/SIPSorcery.VP9.UnitTest: the kernel fixtures, and the SIMD kernels against the C kernels.
  • test/SIPSorcery.VP9.TestVectors: the decoder corpus, the encoder scripts (byte-exact and decodable, with leak checks), tile column threads against one thread and row based multithreading on 3 to 8 threads against 2, VP9Codec's encode and decode surface, and blocks whose changes cancel (StaleBlockTests).
dotnet test SIPSorcery.VP9.slnf -c Release

They pass in Debug too (about 4.5 minutes rather than 40 s), where libvpx's own asserts are live as Debug.Asserts, along with the bounds checks on the inline arrays.

License

The port is BSD 3-Clause, as SIPSorcery is. The ported code is also covered by libvpx's BSD 3-Clause LICENSE and its PATENTS grant, in LIBVPX_LICENSE and LIBVPX_PATENTS.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.20-pre 61 11/29/2026

-v10.0.20-pre: Initial release.