0

I'm using ffmpeg to decode H.264 streams. I have a pointer to image data in memory and i can create a WritableBitmap using this code:

     var _rmlive = new WriteableBitmap(e.Width, e.Hieght, 1, 1, PixelFormats.Bgr24, null);
_rmlive.WritePixels(_rectLive, e.PointerToBuffer, _arraySize, e.Stride);

and i can get a Bitmap object out of the pointer using this code:

byte[] inBytes = new byte[size];
        Marshal.Copy(ptr, inBytes, 0, (int)size);
            var b = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            var BoundsRect = new System.Drawing.Rectangle(0, 0, width, height);
            BitmapData bmpData = b.LockBits(BoundsRect,
                                            ImageLockMode.WriteOnly,
                                            b.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = bmpData.Stride * b.Height;
            try
            {
                Marshal.Copy(inBytes, 0, ptr, bytes);
            }
            catch (Exception e)
            {
            }
            finally
            {
                b.UnlockBits(bmpData);
            }

then i can render and display it in WPF but it consumes a lot of CPU resources (specially for full HD frames). the question is it is possible to render/display the image directly from the pointer without creating Bitmap/WritableBitmap in c# / WPF?

  • You need something that can quickly draw onto some surface, preferably using hardware support. Look into OpenGL, DirectX or GDI+. – CodeCaster Feb 08 '18 at 15:00
  • can you provider some code samples? – Mohamed Al-Hosary Feb 08 '18 at 15:01
  • No, sorry, all of those have plenty of documentation on the web. – CodeCaster Feb 08 '18 at 15:02
  • thank you for your support @CodeCaster – Mohamed Al-Hosary Feb 08 '18 at 15:04
  • If you care about performance, stop using ffmpeg. It's not an appropriate streaming video solution, at least not on Windows (maybe on *nix where interprocess I/O is more streamlined...not sure there). Use one of the built-in video streaming APIs, so all of the handling is contained to your process. Note that WPF doesn't have built-in support for streaming video, but you can cobble it together. E.g. https://stackoverflow.com/questions/7117589/using-mediaelement-to-play-video-from-stream, or use `HttpServer` to host the stream (so `MediaPlayer` has a URL to work with). – Peter Duniho Feb 09 '18 at 02:26
  • Most efficient, of course, would be to use `DirectShow`. It's a little more work, but hosting DirectX rendering in WPF isn't too bad, and DirectShow itself also isn't that hard. – Peter Duniho Feb 09 '18 at 02:27
  • @PeterDuniho: because i don't have a URL, i have only a frame by frame byte[] H.264 encoded frame, so i use ffmpeg to decode in memory array. i can't find an implementation without using a URL in DirectShow. Do you have any sample for DirectShow? – Mohamed Al-Hosary Feb 09 '18 at 07:56
  • There are lots of DirectShow samples around. If you can't find them, your first step should be to learn how to use a web search engine. As for _"because i don't have a URL"_ -- my point about using `HttpServer` is that _you_ create the URL, to point to _your_ implementation that uses `HttpServer` to stream from the URL that _you_ created. Then you _do_ have a URL. – Peter Duniho Feb 09 '18 at 08:16

0 Answers0