// Constructor public AVIWriter() { Win32.AVIFileInit(); }
// 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; }