// Open an AVI file public void Open(string fname) { // close previous file Close(); // open file if (Win32.AVIFileOpen(out file, fname, Win32.OpenFileMode.ShareDenyWrite, IntPtr.Zero) != 0) { throw new ApplicationException("Failed opening file"); } // get first video stream if (Win32.AVIFileGetStream(file, out stream, Win32.mmioFOURCC("vids"), 0) != 0) { throw new ApplicationException("Failed getting video stream"); } // get stream info Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO(); Win32.AVIStreamInfo(stream, ref info, Marshal.SizeOf(info)); width = info.rcFrame.right; height = info.rcFrame.bottom; position = info.dwStart; start = info.dwStart; length = info.dwLength; rate = (float)info.dwRate / (float)info.dwScale; codec = Win32.decode_mmioFOURCC(info.fccHandler); // prepare decompressor Win32.BITMAPINFOHEADER bih = new Win32.BITMAPINFOHEADER(); bih.biSize = Marshal.SizeOf(bih.GetType()); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = 0; // BI_RGB // get frame open object if ((getFrame = Win32.AVIStreamGetFrameOpen(stream, ref bih)) == IntPtr.Zero) { bih.biHeight = -height; if ((getFrame = Win32.AVIStreamGetFrameOpen(stream, ref bih)) == IntPtr.Zero) { throw new ApplicationException("Failed initializing decompressor"); } } }
// Create new AVI file public void Open(string fname, int width, int height) { // close previous file Close(); // calculate stride stride = width * 3; int r = stride % 4; if (r != 0) { stride += (4 - r); } // create new file if (Win32.AVIFileOpen(out file, fname, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero) != 0) { throw new ApplicationException("Failed opening file"); } this.width = width; this.height = height; // describe new stream Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO(); info.fccType = Win32.mmioFOURCC("vids"); info.fccHandler = Win32.mmioFOURCC(codec); info.dwScale = 1; info.dwRate = rate; info.dwSuggestedBufferSize = stride * height; // create stream if (Win32.AVIFileCreateStream(file, out stream, ref info) != 0) { throw new ApplicationException("Failed creating stream"); } // describe compression options Win32.AVICOMPRESSOPTIONS opts = new Win32.AVICOMPRESSOPTIONS(); opts.fccHandler = Win32.mmioFOURCC(codec); opts.dwQuality = quality; // // Win32.AVISaveOptions(stream, ref opts, IntPtr.Zero); // create compressed stream if (Win32.AVIMakeCompressedStream(out streamCompressed, stream, ref opts, IntPtr.Zero) != 0) { throw new ApplicationException("Failed creating compressed stream"); } // describe frame format Win32.BITMAPINFOHEADER bih = new Win32.BITMAPINFOHEADER(); bih.biSize = Marshal.SizeOf(bih.GetType()); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biSizeImage = 0; bih.biCompression = 0; // BI_RGB // set frame format if (Win32.AVIStreamSetFormat(streamCompressed, 0, ref bih, Marshal.SizeOf(bih.GetType())) != 0) { throw new ApplicationException("Failed creating compressed stream"); } // alloc unmanaged memory for frame buf = Marshal.AllocHGlobal(stride * height); position = 0; }