Esempio n. 1
0
        /// <summary>
        /// Open AVI file.
        /// </summary>
        /// 
        /// <param name="fileName">AVI file name to open.</param>
        /// 
        /// <remarks><para>The method opens a video file and prepares the stream and decoder for
        /// reading video frames with the help of <see cref="GetNextFrame"/> method.</para>
        /// </remarks>
        /// 
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while opening the video file. See exception message.</exception>
        ///
        /// 
        public void Open( string fileName )
        {
            // close previous file
            Close( );

            bool success = false;

            try
            {
                lock ( sync )
                {
                    // open AVI file
                    if ( Win32.AVIFileOpen( out file, fileName, Win32.OpenFileMode.ShareDenyWrite, IntPtr.Zero ) != 0 )
                        throw new System.IO.IOException( "Failed opening the specified AVI file." );

                    // get first video stream
                    if ( Win32.AVIFileGetStream( file, out stream, Win32.mmioFOURCC( "vids" ), 0 ) != 0 )
                        throw new VideoException( "Failed getting video stream." );

                    // get stream info
                    Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO( );
                    Win32.AVIStreamInfo( stream, ref info, Marshal.SizeOf( info ) );

                    width    = info.rectFrame.right;
                    height   = info.rectFrame.bottom;
                    position = info.start;
                    start    = info.start;
                    length   = info.length;
                    rate     = (float) info.rate / (float) info.scale;
                    codec    = Win32.decode_mmioFOURCC( info.handler );

                    // prepare decompressor
                    Win32.BITMAPINFOHEADER bitmapInfoHeader = new Win32.BITMAPINFOHEADER( );

                    bitmapInfoHeader.size        = Marshal.SizeOf( bitmapInfoHeader.GetType( ) );
                    bitmapInfoHeader.width       = width;
                    bitmapInfoHeader.height      = height;
                    bitmapInfoHeader.planes      = 1;
                    bitmapInfoHeader.bitCount    = 24;
                    bitmapInfoHeader.compression = 0; // BI_RGB

                    // get frame object
                    if ( ( getFrame = Win32.AVIStreamGetFrameOpen( stream, ref bitmapInfoHeader ) ) == IntPtr.Zero )
                    {
                        bitmapInfoHeader.height = -height;

                        if ( ( getFrame = Win32.AVIStreamGetFrameOpen( stream, ref bitmapInfoHeader ) ) == IntPtr.Zero )
                            throw new VideoException( "Failed initializing decompressor." );
                    }

                    success = true;
                }
            }
            finally
            {
                if ( !success )
                {
                    Close( );
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        /// 
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        /// 
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        /// 
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while creating new video file. See exception message.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory for internal buffer.</exception>
        /// <exception cref="ArgumentException">Video file resolution must be a multiple of two.</exception>
        /// 
        public void Open( string fileName, int width, int height )
		{
			// close previous file
			Close( );

           // check width and height
            if ( ( ( width & 1 ) != 0 ) || ( ( height & 1 ) != 0 ) )
            {
	            throw new ArgumentException( "Video file resolution must be a multiple of two." );
            }

            bool success = false;

            try
            {
                lock ( sync )
                {
                    // calculate stride
                    stride = width * 3;
                    if ( ( stride % 4 ) != 0 )
                        stride += ( 4 - stride % 4 );

                    // create new file
                    if ( Win32.AVIFileOpen( out file, fileName, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero ) != 0 )
                        throw new System.IO.IOException( "Failed opening the specified file." );

                    this.width = width;
                    this.height = height;

                    // describe new stream
                    Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO( );

                    info.type    = Win32.mmioFOURCC( "vids" );
                    info.handler = Win32.mmioFOURCC( codec );
                    info.scale   = 1;
                    info.rate    = rate;
                    info.suggestedBufferSize = stride * height;

                    // create stream
                    if ( Win32.AVIFileCreateStream( file, out stream, ref info ) != 0 )
                        throw new VideoException( "Failed creating stream." );

                    // describe compression options
                    Win32.AVICOMPRESSOPTIONS options = new Win32.AVICOMPRESSOPTIONS( );

                    options.handler = Win32.mmioFOURCC( codec );
                    options.quality = quality;

                    // uncomment if video settings dialog is required to show
                    // Win32.AVISaveOptions( stream, ref options );

                    // create compressed stream
                    if ( Win32.AVIMakeCompressedStream( out streamCompressed, stream, ref options, IntPtr.Zero ) != 0 )
                        throw new VideoException( "Failed creating compressed stream." );

                    // describe frame format
                    Win32.BITMAPINFOHEADER bitmapInfoHeader = new Win32.BITMAPINFOHEADER( );

                    bitmapInfoHeader.size        = Marshal.SizeOf( bitmapInfoHeader.GetType( ) );
                    bitmapInfoHeader.width       = width;
                    bitmapInfoHeader.height      = height;
                    bitmapInfoHeader.planes      = 1;
                    bitmapInfoHeader.bitCount    = 24;
                    bitmapInfoHeader.sizeImage   = 0;
                    bitmapInfoHeader.compression = 0; // BI_RGB

                    // set frame format
                    if ( Win32.AVIStreamSetFormat( streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf( bitmapInfoHeader.GetType( ) ) ) != 0 )
                        throw new VideoException( "Failed setting format of the compressed stream." );

                    // alloc unmanaged memory for frame
                    buffer = Marshal.AllocHGlobal( stride * height );

                    if ( buffer == IntPtr.Zero )
                    {
                        throw new OutOfMemoryException( "Insufficient memory for internal buffer." );
                    }

                    position = 0;
                    success = true;
                }
            }
            finally
            {
                if ( !success )
                {
                    Close( );
                }
            }
		}
Esempio n. 3
0
		// 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); // != 0)
			{
				// throw new ApplicationException("Failed creating compressed options");
			}
			
			// 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;
		}
Esempio n. 4
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        /// 
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        /// 
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        /// 
        /// <exception cref="ApplicationException">Failure of opening video files (the exception message
        /// specifies the issues).</exception>
        /// 
        public void Open( string fileName, int width, int height )
		{
			// close previous file
			Close( );

            lock ( this )
            {
                // calculate stride
                stride = width * 3;
                if ( ( stride % 4 ) != 0 )
                    stride += ( 4 - stride % 4 );

                // create new file
                if ( Win32.AVIFileOpen( out file, fileName, 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.type = Win32.mmioFOURCC( "vids" );
                info.handler = Win32.mmioFOURCC( codec );
                info.scale = 1;
                info.rate = rate;
                info.suggestedBufferSize = stride * height;

                // create stream
                if ( Win32.AVIFileCreateStream( file, out stream, ref info ) != 0 )
                    throw new ApplicationException( "Failed creating stream" );

                // describe compression options
                Win32.AVICOMPRESSOPTIONS options = new Win32.AVICOMPRESSOPTIONS( );

                options.handler = Win32.mmioFOURCC( codec );
                options.quality = quality;

                // uncomment if video settings dialog is required to show
                // Win32.AVISaveOptions( stream, ref options );

                // create compressed stream
                if ( Win32.AVIMakeCompressedStream( out streamCompressed, stream, ref options, IntPtr.Zero ) != 0 )
                    throw new ApplicationException( "Failed creating compressed stream" );

                // describe frame format
                Win32.BITMAPINFOHEADER bitmapInfoHeader = new Win32.BITMAPINFOHEADER( );

                bitmapInfoHeader.size = Marshal.SizeOf( bitmapInfoHeader.GetType( ) );
                bitmapInfoHeader.width = width;
                bitmapInfoHeader.height = height;
                bitmapInfoHeader.planes = 1;
                bitmapInfoHeader.bitCount = 24;
                bitmapInfoHeader.sizeImage = 0;
                bitmapInfoHeader.compression = 0; // BI_RGB

                // set frame format
                if ( Win32.AVIStreamSetFormat( streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf( bitmapInfoHeader.GetType( ) ) ) != 0 )
                    throw new ApplicationException( "Failed creating compressed stream" );

                // alloc unmanaged memory for frame
                buffer = Marshal.AllocHGlobal( stride * height );

                if ( buffer == IntPtr.Zero )
                    throw new ApplicationException( "Insufficient memory for internal buffer" );

                position = 0;
            }
		}
Esempio n. 5
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        /// 
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        /// 
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        /// 
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while creating new video file. See exception message.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory for internal buffer.</exception>
        /// <exception cref="ArgumentException">Video file resolution must be a multiple of two.</exception>
        /// 
        public void Open( string fileName, int width, int height )
        {
            // close previous file
            Close( );

               // check width and height
            if ( ( ( width & 1 ) != 0 ) || ( ( height & 1 ) != 0 ) )
            {
                throw new ArgumentException( "Video file resolution must be a multiple of two." );
            }

            bool success = false;

            // ------------------------ new: Append .avi (and remove it after closing) if filename doesnt end with .avi ---
            //                               (if filename doesnt end with .avi AVIWrite can't write the file)

            ofileName = fileName;
            string nfileName = fileName.ToLower(); // to work with all versions of .avi, .AVI, .Avi, ...
            if (!nfileName.EndsWith(".avi"))
            {

                fileName += ".avi";
                fileNameChanged = true;

                FileInfo nfi = new FileInfo(fileName);

                // if the new filename also exists: throw exception
                if (nfi.Exists)
                {
                    fileNameChanged = false;
                    throw new System.IO.IOException("Intermediate file exists already.");
                };
            }

            //-------------------------------------------------------------------------------------------------------------

            // ------------------------ new: delete file if it exists (AVIWRiter does not overwrite any file) -------------

            FileInfo fi = new FileInfo(ofileName);
            if (fi.Exists) { File.Delete(ofileName); };
            //-------------------------------------------------------------------------------------------------------------

            try
            {
                lock ( sync )
                {
                    // calculate stride
                    stride = width * 3;
                    if ( ( stride % 4 ) != 0 )
                        stride += ( 4 - stride % 4 );

                    // create new file
                    if ( Win32.AVIFileOpen( out file, fileName, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero ) != 0 )
                        throw new System.IO.IOException( "Failed opening the specified file." );

                    this.width = width;
                    this.height = height;

                    // describe new stream
                    Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO( );

                    info.type    = Win32.mmioFOURCC( "vids" );
                    info.handler = Win32.mmioFOURCC( codec );
                    info.scale   = 1;
                    info.rate    = rate;
                    info.suggestedBufferSize = stride * height;

                    // create stream
                    if ( Win32.AVIFileCreateStream( file, out stream, ref info ) != 0 )
                        throw new VideoException( "Failed creating stream." );

                    // describe compression options
                    Win32.AVICOMPRESSOPTIONS options = new Win32.AVICOMPRESSOPTIONS( );

                    options.handler = Win32.mmioFOURCC( codec );
                    options.quality = quality;

                    // uncomment if video settings dialog is required to show
                    // Win32.AVISaveOptions( stream, ref options );

                    // create compressed stream
                    if ( Win32.AVIMakeCompressedStream( out streamCompressed, stream, ref options, IntPtr.Zero ) != 0 )
                        throw new VideoException( "Failed creating compressed stream." );

                    // describe frame format
                    Win32.BITMAPINFOHEADER bitmapInfoHeader = new Win32.BITMAPINFOHEADER( );

                    bitmapInfoHeader.size        = Marshal.SizeOf( bitmapInfoHeader.GetType( ) );
                    bitmapInfoHeader.width       = width;
                    bitmapInfoHeader.height      = height;
                    bitmapInfoHeader.planes      = 1;
                    bitmapInfoHeader.bitCount    = 24;
                    bitmapInfoHeader.sizeImage   = 0;
                    bitmapInfoHeader.compression = 0; // BI_RGB

                    // set frame format
                    if ( Win32.AVIStreamSetFormat( streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf( bitmapInfoHeader.GetType( ) ) ) != 0 )
                        throw new VideoException( "Failed setting format of the compressed stream." );

                    // alloc unmanaged memory for frame
                    buffer = Marshal.AllocHGlobal( stride * height );

                    if ( buffer == IntPtr.Zero )
                    {
                        throw new OutOfMemoryException( "Insufficient memory for internal buffer." );
                    }

                    position = 0;
                    success = true;
                }
            }
            finally
            {
                if ( !success )
                {
                    Close( );
                }
            }
        }
Esempio n. 6
0
		// 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");
			}
		}