Example #1
0
        public static VideoCompressor[] GetAll(bool reload)
        {
            if (compressors != null && !reload)
            {
                // Compressors are cached so if a new one gets installed, apps need restart to get update data
                return(compressors.ToArray());
            }
            compressors = new List <VideoCompressor>();
            Avi32Interop.ICINFO ici = new Avi32Interop.ICINFO();
            ici.Init();
            int hr = int.MaxValue;

            for (uint i = 0; hr != 0; i++)
            {
                // Get info for compressor #i
                ici.biSize = (uint)Marshal.SizeOf(ici);
                hr         = Avi32Interop.ICInfo(Avi32Interop.ICTYPE_VIDEO, i, ref ici);
                try {
                    // Create compressor using fccHandler
                    VideoCompressor compressor = new VideoCompressor(ici.fccHandler);
                    compressors.Add(compressor);
                }
                catch (AviException) {
                }
            }
            return(compressors.ToArray());
        }
Example #2
0
        public override bool Equals(object obj)
        {
            VideoCompressor compressor = obj as VideoCompressor;

            if (compressor == null)
            {
                return(false);
            }
            return(this.fccHandler.Equals(compressor.fccHandler));
        }
Example #3
0
        private void SetupVideo(DisplayFormat videoFormat, VideoCompressor compressor, int fps)
        {
            int colorDepth = Bitmap.GetPixelFormatSize(videoFormat.PixelFormat);
            int width      = videoFormat.Width;
            int height     = videoFormat.Height;
            // Calculate pitch
            int bytesPerPixel = colorDepth / 8;
            int pitch         = width * bytesPerPixel;
            int pitch_factor  = 4;

            if (pitch % pitch_factor != 0)
            {
                pitch = pitch + pitch_factor - pitch % pitch_factor;
            }
            // Create AVI Stream
            Avi32Interop.AVISTREAMINFO asf = new Avi32Interop.AVISTREAMINFO();
            asf.dwRate = fps;
            asf.dwSuggestedBufferSize = pitch * height * bytesPerPixel;
            asf.dwScale = 1;
            asf.fccType = Avi32Interop.streamtypeVIDEO;
            asf.szName  = null;
            asf.rcFrame = new Avi32Interop.RECT(0, 0, width, height);
            int hr = Avi32Interop.AVIFileCreateStream(this.pAviFile, out this.pVideoStream, ref asf);

            if (hr != 0)
            {
                throw new AviException("AVIFileCreateStream", hr);
            }
            // Set stream format
            Avi32Interop.BITMAPINFOHEADER bih = new Avi32Interop.BITMAPINFOHEADER();
            bih.biBitCount    = (ushort)colorDepth;
            bih.biCompression = 0; // BI_RGB
            bih.biHeight      = videoFormat.Height;
            bih.biPlanes      = 1;
            bih.biSize        = (uint)Marshal.SizeOf(bih);
            bih.biSizeImage   = (uint)(pitch * height * (colorDepth / 8));
            bih.biWidth       = videoFormat.Width;
            if (compressor != null && !compressor.Equals(VideoCompressor.None))
            {
                // Setup compressor
                this.SetupVideoCompressor(compressor);
                hr = Avi32Interop.AVIStreamSetFormat(this.pAviCompressedStream, 0, ref bih, Marshal.SizeOf(bih));
            }
            else
            {
                hr = Avi32Interop.AVIStreamSetFormat(this.pVideoStream, 0, ref bih, Marshal.SizeOf(bih));
            }
            if (hr != 0)
            {
                throw new AviException("AVIStreamSetFormat", hr);
            }
        }
Example #4
0
        private void SetupVideoCompressor(VideoCompressor compressor)
        {
            Avi32Interop.AVICOMPRESSOPTIONS compressorOptions = new Avi32Interop.AVICOMPRESSOPTIONS();
            uint fccHandler = compressor.FccHandler;

            compressorOptions.fccType    = Avi32Interop.ICTYPE_VIDEO;
            compressorOptions.fccHandler = fccHandler;
            compressorOptions.dwQuality  = (uint)compressor.Quality;
            // Open compressor
            IntPtr hic = Avi32Interop.ICOpen(Avi32Interop.ICTYPE_VIDEO, fccHandler, Avi32Interop.ICMODE_QUERY);

            if (hic == IntPtr.Zero)
            {
                int errorCode = Marshal.GetLastWin32Error();
                throw new AviException(errorCode, string.Format("ICOpen failed, error code = 0x{0:X8}.", errorCode));
            }
            // Get number of bytes required for params
            uint   cbParams = (uint)Avi32Interop.ICGetState(hic, IntPtr.Zero, 0);
            IntPtr pParams  = Marshal.AllocHGlobal((int)cbParams);

            try {
                // Get params
                int retval = Avi32Interop.ICGetState(hic, pParams, cbParams);
                compressorOptions.cbParms = cbParams;
                // If the Xvid Video Codec is selected, hack params to hide status window!
                if (string.Equals(compressor.FccHandlerString, xvidCodec))
                {
                    ModifyXvidParams(pParams, (int)cbParams);
                }
                compressorOptions.lpParms = pParams;
                compressorOptions.dwFlags = Avi32Interop.AVICOMPRESSF_VALID;
                // Close compressor
                Avi32Interop.ICClose(hic);
                // Make compressed stream
                int hr = Avi32Interop.AVIMakeCompressedStream(out this.pAviCompressedStream, this.pVideoStream,
                                                              ref compressorOptions, 0);
                if (hr != 0)
                {
                    throw new AviException(hr, string.Format("AVIMakeCompressedStream failed, error code = 0x{0:X8}.",
                                                             hr));
                }
            }
            finally {
                if (pParams != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pParams);
                }
            }
        }
Example #5
0
        public static VideoCompressor Create(string fccHandler)
        {
            if (string.IsNullOrEmpty(fccHandler))
            {
                throw new ArgumentNullException("fccHandler");
            }
            if (fccHandler.Length != 4)
            {
                throw new ArgumentException("fccHandler");
            }
            uint            ffcHandler = Avi32Interop.mmioFOURCC(fccHandler[0], fccHandler[1], fccHandler[2], fccHandler[3]);
            VideoCompressor compressor = new VideoCompressor(ffcHandler);

            return(compressor);
        }
Example #6
0
        public void Open(string fileName, DisplayFormat videoFormat, int fps, VideoCompressor compressor,
                         SoundFormat audioFormat, AcmEncoder audioEncoder)
        {
            if (this.opened)
            {
                throw new InvalidOperationException();
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            this.video = videoFormat != null;
            this.audio = audioFormat != null;
            if (!this.audio && !this.video)
            {
                // There is nothing to do!
                throw new InvalidOperationException();
            }
            // Open AVI File
            int hr = Avi32Interop.AVIFileOpen(out this.pAviFile, fileName, Avi32Interop.OF_CREATE, IntPtr.Zero);

            if (hr != 0)
            {
                throw new AviException("AVIFileOpen", hr);
            }
            try {
                if (this.video)
                {
                    this.SetupVideo(videoFormat, compressor, fps);
                }
                if (this.audio)
                {
                    this.SetupAudio(audioFormat, audioEncoder);
                }
                this.opened = true;
            }
            finally {
                if (!this.opened)
                {
                    this.Close();
                }
            }
        }
Example #7
0
 public static VideoCompressor[] GetAll(bool reload)
 {
     if (compressors != null && !reload) {
     // Compressors are cached so if a new one gets installed, apps need restart to get update data
     return compressors.ToArray();
      }
      compressors = new List<VideoCompressor>();
      Avi32Interop.ICINFO ici = new Avi32Interop.ICINFO();
      ici.Init();
      int hr = int.MaxValue;
      for (uint i = 0; hr != 0; i++) {
     // Get info for compressor #i
     ici.biSize = (uint)Marshal.SizeOf(ici);
     hr = Avi32Interop.ICInfo(Avi32Interop.ICTYPE_VIDEO, i, ref ici);
     try {
        // Create compressor using fccHandler
        VideoCompressor compressor = new VideoCompressor(ici.fccHandler);
        compressors.Add(compressor);
     }
     catch (AviException) {
     }
      }
      return compressors.ToArray();
 }
Example #8
0
 public static VideoCompressor Create(string fccHandler)
 {
     if (string.IsNullOrEmpty(fccHandler)) {
     throw new ArgumentNullException("fccHandler");
      }
      if (fccHandler.Length != 4) {
     throw new ArgumentException("fccHandler");
      }
      uint ffcHandler = Avi32Interop.mmioFOURCC(fccHandler[0], fccHandler[1], fccHandler[2], fccHandler[3]);
      VideoCompressor compressor = new VideoCompressor(ffcHandler);
      return compressor;
 }
Example #9
0
 private void SetupVideoCompressor(VideoCompressor compressor)
 {
     Avi32Interop.AVICOMPRESSOPTIONS compressorOptions = new Avi32Interop.AVICOMPRESSOPTIONS();
      uint fccHandler = compressor.FccHandler;
      compressorOptions.fccType = Avi32Interop.ICTYPE_VIDEO;
      compressorOptions.fccHandler = fccHandler;
      compressorOptions.dwQuality = (uint)compressor.Quality;
      // Open compressor
      IntPtr hic = Avi32Interop.ICOpen(Avi32Interop.ICTYPE_VIDEO, fccHandler, Avi32Interop.ICMODE_QUERY);
      if (hic == IntPtr.Zero) {
     int errorCode = Marshal.GetLastWin32Error();
     throw new AviException(errorCode, string.Format("ICOpen failed, error code = 0x{0:X8}.", errorCode));
      }
      // Get number of bytes required for params
      uint cbParams = (uint)Avi32Interop.ICGetState(hic, IntPtr.Zero, 0);
      IntPtr pParams = Marshal.AllocHGlobal((int)cbParams);
      try {
     // Get params
     int retval = Avi32Interop.ICGetState(hic, pParams, cbParams);
     compressorOptions.cbParms = cbParams;
     // If the Xvid Video Codec is selected, hack params to hide status window!
     if (string.Equals(compressor.FccHandlerString, xvidCodec)) {
        ModifyXvidParams(pParams, (int)cbParams);
     }
     compressorOptions.lpParms = pParams;
     compressorOptions.dwFlags = Avi32Interop.AVICOMPRESSF_VALID;
     // Close compressor
     Avi32Interop.ICClose(hic);
     // Make compressed stream
     int hr = Avi32Interop.AVIMakeCompressedStream(out this.pAviCompressedStream, this.pVideoStream,
                                                   ref compressorOptions, 0);
     if (hr != 0) {
        throw new AviException(hr, string.Format("AVIMakeCompressedStream failed, error code = 0x{0:X8}.",
                                                 hr));
     }
      }
      finally {
     if (pParams != IntPtr.Zero) {
        Marshal.FreeHGlobal(pParams);
     }
      }
 }
Example #10
0
 private void SetupVideo(DisplayFormat videoFormat, VideoCompressor compressor, int fps)
 {
     int colorDepth = Bitmap.GetPixelFormatSize(videoFormat.PixelFormat);
      int width = videoFormat.Width;
      int height = videoFormat.Height;
      // Calculate pitch
      int bytesPerPixel = colorDepth / 8;
      int pitch = width * bytesPerPixel;
      int pitch_factor = 4;
      if (pitch % pitch_factor != 0) {
     pitch = pitch + pitch_factor - pitch % pitch_factor;
      }
      // Create AVI Stream
      Avi32Interop.AVISTREAMINFO asf = new Avi32Interop.AVISTREAMINFO();
      asf.dwRate = fps;
      asf.dwSuggestedBufferSize = pitch * height * bytesPerPixel;
      asf.dwScale = 1;
      asf.fccType = Avi32Interop.streamtypeVIDEO;
      asf.szName = null;
      asf.rcFrame = new Avi32Interop.RECT(0, 0, width, height);
      int hr = Avi32Interop.AVIFileCreateStream(this.pAviFile, out this.pVideoStream, ref asf);
      if (hr != 0) {
     throw new AviException("AVIFileCreateStream", hr);
      }
      // Set stream format
      Avi32Interop.BITMAPINFOHEADER bih = new Avi32Interop.BITMAPINFOHEADER();
      bih.biBitCount = (ushort)colorDepth;
      bih.biCompression = 0; // BI_RGB
      bih.biHeight = videoFormat.Height;
      bih.biPlanes = 1;
      bih.biSize = (uint)Marshal.SizeOf(bih);
      bih.biSizeImage = (uint)(pitch * height * (colorDepth / 8));
      bih.biWidth = videoFormat.Width;
      if (compressor != null && !compressor.Equals(VideoCompressor.None)) {
     // Setup compressor
     this.SetupVideoCompressor(compressor);
     hr = Avi32Interop.AVIStreamSetFormat(this.pAviCompressedStream, 0, ref bih, Marshal.SizeOf(bih));
      }
      else {
     hr = Avi32Interop.AVIStreamSetFormat(this.pVideoStream, 0, ref bih, Marshal.SizeOf(bih));
      }
      if (hr != 0) {
     throw new AviException("AVIStreamSetFormat", hr);
      }
 }
Example #11
0
 public void Open(string fileName, DisplayFormat videoFormat, int fps, VideoCompressor compressor, 
                SoundFormat audioFormat, AcmEncoder audioEncoder)
 {
     if (this.opened) {
     throw new InvalidOperationException();
      }
      if (string.IsNullOrEmpty(fileName)) {
     throw new ArgumentNullException("fileName");
      }
      this.video = videoFormat != null;
      this.audio = audioFormat != null;
      if (!this.audio && !this.video) {
     // There is nothing to do!
     throw new InvalidOperationException();
      }
      // Open AVI File
      int hr = Avi32Interop.AVIFileOpen(out this.pAviFile, fileName, Avi32Interop.OF_CREATE, IntPtr.Zero);
      if (hr != 0) {
     throw new AviException("AVIFileOpen", hr);
      }
      try {
     if (this.video) {
       this.SetupVideo(videoFormat, compressor, fps);
     }
     if (this.audio) {
        this.SetupAudio(audioFormat, audioEncoder);
     }
     this.opened = true;
      }
      finally {
     if (!this.opened) {
        this.Close();
     }
      }
 }