title: What is the most performant way to render unmanaged video frames in WPF?
body: <p>I'm using FFmpeg library to receive and decode H.264/MPEG-TS over UDP with <strong>minimal</strong> latency (something MediaElement can't handle).</p>
  
  <p>On a dedicated FFmpeg thread, I'm pulling PixelFormats.Bgr32 video frames for display. I've already tried InteropBitmap:</p>
  
  <pre><code>_section = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, PAGE_READWRITE, 0, size, null);
  _buffer = MapViewOfFile(_section, FILE_MAP_ALL_ACCESS, 0, 0, size);
  Dispatcher.Invoke((Action)delegate()
  {
      _interopBitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(_section, width, height, PixelFormats.Bgr32, (int)size / height, 0);
      this.Source = _interopBitmap;
  });
  </code></pre>
  
  <p>And then per frame update:</p>
  
  <pre><code>Dispatcher.Invoke((Action)delegate()
  {
      _interopBitmap.Invalidate();
  });
  </code></pre>
  
  <p>But performance is quite bad (skipping frames, high CPU usage etc).</p>
  
  <p>I've also tried WriteableBitmap: FFmpeg is placing frames in _writeableBitmap.BackBuffer and per frame update:</p>
  
  <pre><code>Dispatcher.Invoke((Action)delegate()
  {
      _writeableBitmap.Lock();
  });
  try
  {
      ret = FFmpegInvoke.sws_scale(...);
  }
  finally
  {
      Dispatcher.Invoke((Action)delegate()
      {
          _writeableBitmap.AddDirtyRect(_rect);
          _writeableBitmap.Unlock();
      });
  }
  </code></pre>
  
  <p>Experiencing almost the same performance issues (tested with various DispatcherPriority).</p>
  
  <p>Any help will be greatly appreciated.</p>