none
PlanarImage returned by Runtime.VideoFrameReady corrupted (possible race condition) RRS feed

  • Question

  • Hi,

    Using C#, I have stumbled upon a problem when processing the PlanarImage returned via Runtime.VideoFrameReady. It seems the returned PlanarImage is updated by the Runtime even after returning the PlanarImage to the user - i.e. a race condition occurs when the processor is under heavy load, yielding a corrupted image.

    The following code based on System.Drawing.Bitmap yields a corrupted bitmap when the processor is under heavy load:

            private static Bitmap PlanarImageToBitmap(PlanarImage planarImage)
            {
                var width = planarImage.Width;
                var height = planarImage.Height;
                Bitmap bitmap;
                unsafe
                {
                    fixed (byte* p = planarImage.Bits)
                    {
                        var pixelPointer = (IntPtr) p;
                        using (
                            var tempBitmap = new Bitmap(width, height, width*planarImage.BytesPerPixel, PixelFormat.Format32bppRgb,
                                                        pixelPointer))
                            bitmap = (Bitmap) tempBitmap.Clone();
                    }
                }
                return bitmap;
            }
    
    

     

    whereas the following WPF-dependent code (BitmapSource) yields a valid color image even when stressing the processor:

            private static Bitmap PlanarImageToBitmap(PlanarImage planarImage)
            {
                var source = BitmapSource.Create(planarImage.Width, planarImage.Height, 96, 96, PixelFormats.Bgr32, null,
                                                 planarImage.Bits, planarImage.Width * planarImage.BytesPerPixel);
                var bitmap = new Bitmap(source.PixelWidth, source.PixelHeight, PixelFormat.Format32bppRgb);
                var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
                bitmap.UnlockBits(data);
                return bitmap;
            }
    
    

     

    Comments?

    Are you able to reproduce the problem?

     

    Best regards,

    Jesper

    Friday, October 21, 2011 2:13 PM

All replies

  • So, are you saying that your "unsafe" code works fine under normal load, but then fails sporadically when under heavy load? What is the rate of failure/corruption under heavy load, and what do you mean by "heavy load". I'm asking, because if the rate of failure under certain conditions is actually 100%, it's probably not a load issue, but something else going on.

    Also, why do you use "unsafe" code if the WPF-based code you posted works just fine? Are you hoping to improve performance? If so, did you measure actual performance improvements with the code above?

    Eddy


    I'm here to help
    Friday, October 21, 2011 6:11 PM