1
Vote

Uncompressed AVI creates vertical jitter

description

When I create an uncompressed AVI there is a vertical jitter (I think) of about 1 pixel. It could be my code but I'm not seeing where it's creating any kind of offset. It seems to do it in both Media Player Classic HD and Windows Media Player so I think it's encoded into the file rather than an artifact of playback.

Here's what I'm using in a console application. The bitmap this refers to is one I created in MS Paint and saved to my D drive. I'm testing it with a single bitmap for now until I later add a file collection to make a slideshow.
        private static byte[] GetFrameData(Bitmap bmp)
        {
            var frameData = new byte[bmp.Width * bmp.Height * 4];
            var bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
            Marshal.Copy(bits.Scan0, frameData, 0, frameData.Length);
            bmp.UnlockBits(bits);
            return frameData;
        }

        private static Bitmap GetFrameBitmap()
        {
            Image img = Image.FromFile(@"D:\temp.bmp");
            Bitmap bmp = new Bitmap(img);
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return bmp;
        }

        static void CreateAVI()
        {
            int lengthInSeconds = 5;
            int fps = 30;
            var writer = new AviWriter(@"D:\test.avi")
            {
                FramesPerSecond = fps,
                EmitIndex1 = true
            };
            Bitmap bmp = GetFrameBitmap();
            var stream = writer.AddVideoStream();
            stream.Width = bmp.Width;
            stream.Height = bmp.Height;
            stream.Codec = KnownFourCCs.Codecs.Uncompressed;
            stream.BitsPerPixel = BitsPerPixel.Bpp32;
            int totalFrames = lengthInSeconds * fps;
            for (int i = 0; i < totalFrames; i++)
            {
                bmp = GetFrameBitmap();
                byte[] frameData = GetFrameData(bmp);
                stream.WriteFrame(true, frameData, 0, frameData.Length);
                Console.WriteLine(i.ToString());
                bmp.Dispose();
            }
            writer.Close();
            Console.WriteLine("Done.");
            Console.ReadLine();
        }

file attachments

comments

robault wrote Oct 5, 2016 at 8:24 PM

Hey, I also want to mention this is a really cool project. I wouldn't be able to build mine without it. Thanks for making it!

--Robert

baSSiLL wrote Oct 5, 2016 at 9:19 PM

I've checked your code with several sizes of bitmaps and did not notice any issues on playback.
Can you provide a bitmap you're testing with? Or a screencast of the jitter?

Although it should not matter for 32-bits format, it is safer to use BitmapData.Stride property to determine the byte size of bitmap line rather than multiply Width by bytes per pixel.

robault wrote Oct 6, 2016 at 12:23 AM

Here's the bitmap I'm using.

robault wrote Oct 6, 2016 at 12:28 AM

ACK! You know what! On a hunch after your last comment I uninstalled the Shark codec pack and it runs just fine. Must have been some decoder setting that was applied during/after install. There's no jitter anymore. This stuff is still pretty new to me so I'll have to be more careful about what I'm doing. Thanks for the quick response.