Exemplo n.º 1
0
        /// <summary>Create a new stream</summary>
        private void CreateStreamWithoutFormat()
        {
            int    scale = 1;
            double rate  = this.frameRate;

            this.GetRateAndScale(ref rate, ref scale);
            Avi.AVISTREAMINFO strhdr = new Avi.AVISTREAMINFO();
            strhdr.fccType               = Avi.mmioStringToFOURCC("vids", 0);
            strhdr.fccHandler            = Avi.mmioStringToFOURCC("CVID", 0);
            strhdr.dwFlags               = 0;
            strhdr.dwCaps                = 0;
            strhdr.wPriority             = 0;
            strhdr.wLanguage             = 0;
            strhdr.dwScale               = (int)scale;
            strhdr.dwRate                = (int)rate; // Frames per Second
            strhdr.dwStart               = 0;
            strhdr.dwLength              = 0;
            strhdr.dwInitialFrames       = 0;
            strhdr.dwSuggestedBufferSize = this.frameSize; //height_ * stride_;
            strhdr.dwQuality             = -1;             //default
            strhdr.dwSampleSize          = 0;
            strhdr.rcFrame.top           = 0;
            strhdr.rcFrame.left          = 0;
            strhdr.rcFrame.bottom        = (uint)this.height;
            strhdr.rcFrame.right         = (uint)this.width;
            strhdr.dwEditCount           = 0;
            strhdr.dwFormatChangeCount   = 0;
            strhdr.szName                = new UInt16[64];
            int result = Avi.AVIFileCreateStream(this.aviFile, out this.aviStream, ref strhdr);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>Create a compressed stream from an uncompressed stream</summary>
        private void CreateCompressedStream()
        {
            //display the compression options dialog...
            Avi.AVICOMPRESSOPTIONS_CLASS options = new Avi.AVICOMPRESSOPTIONS_CLASS();
            options.fccType  = (uint)Avi.streamtypeVIDEO;
            options.lpParms  = IntPtr.Zero;
            options.lpFormat = IntPtr.Zero;
            Avi.AVISaveOptions(IntPtr.Zero, Avi.ICMF_CHOOSE_KEYFRAME | Avi.ICMF_CHOOSE_DATARATE, 1, ref this.aviStream,
                               ref options);
            Avi.AVISaveOptionsFree(1, ref options);
            //..or set static options

            /*Avi.AVICOMPRESSOPTIONS opts = new Avi.AVICOMPRESSOPTIONS();
             *          opts.fccType         = (UInt32)Avi.mmioStringToFOURCC("vids", 0);
             *          opts.fccHandler      = (UInt32)Avi.mmioStringToFOURCC("CVID", 0);
             *          opts.dwKeyFrameEvery = 0;
             *          opts.dwQuality       = 0;  // 0 .. 10000
             *          opts.dwFlags         = 0;  // AVICOMRPESSF_KEYFRAMES = 4
             *          opts.dwBytesPerSecond= 0;
             *          opts.lpFormat        = new IntPtr(0);
             *          opts.cbFormat        = 0;
             *          opts.lpParms         = new IntPtr(0);
             *          opts.cbParms         = 0;
             *          opts.dwInterleaveEvery = 0;*/
            //get the compressed stream
            this.compressOptions = options.ToStruct();
            int result = Avi.AVIMakeCompressedStream(out this.compressedStream, this.aviStream, ref this.compressOptions,
                                                     0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIMakeCompressedStream: " + result.ToString());
            }
            this.SetFormat(this.compressedStream);
        }
Exemplo n.º 3
0
        /// <summary>Add one frame to a new stream</summary>
        /// <param name="bmp"></param>
        /// <remarks>
        /// This works only with uncompressed streams,
        /// and compressed streams that have not been saved yet.
        /// Use DecompressToNewFile to edit saved compressed streams.
        /// </remarks>
        public void AddFrame(Bitmap bmp)
        {
            try
            {
                bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

                BitmapData bmpDat = bmp.LockBits(
                    new Rectangle(
                        0, 0, bmp.Width, bmp.Height),
                    ImageLockMode.ReadOnly, bmp.PixelFormat);

                int result = Avi.AVIStreamWrite(writeCompressed ? compressedStream : aviStream,
                                                countFrames, 1,
                                                bmpDat.Scan0,
                                                (Int32)(bmpDat.Stride * bmpDat.Height),
                                                0, 0, 0);

                if (result != 0)
                {
                    throw new Exception("Exception in VideoStreamWrite: " + result.ToString());
                }

                bmp.UnlockBits(bmpDat);

                countFrames++;
            }
            catch (Exception exp)
            {
                Debug.Write(exp.ToString());
            }
        }
Exemplo n.º 4
0
        /// <summary>Prepare for decompressing frames</summary>
        /// <remarks>
        /// This method has to be called before GetBitmap and ExportBitmap.
        /// Release ressources with GetFrameClose.
        /// </remarks>
        public void GetFrameOpen()
        {
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            //Open frames

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            bih.biBitCount      = countBitsPerPixel;
            bih.biClrImportant  = 0;
            bih.biClrUsed       = 0;
            bih.biCompression   = 0;
            bih.biHeight        = (Int32)streamInfo.rcFrame.bottom;
            bih.biWidth         = (Int32)streamInfo.rcFrame.right;
            bih.biPlanes        = 1;
            bih.biSize          = Marshal.SizeOf(bih);
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;

            getFrameObject = Avi.AVIStreamGetFrameOpen(aviStream, ref bih);

            if (getFrameObject == 0)
            {
                throw new Exception("Exception in VideoStreamGetFrameOpen!");
            }
        }
Exemplo n.º 5
0
        /// <summary>Open or create an AVI file</summary>
        /// <param name="fileName">Name of the AVI file</param>
        /// <param name="open">true: Open the file; false: Create or overwrite the file</param>
        public AviManager(String fileName, bool open)
        {
            Avi.AVIFileInit();
            int result;

            if (open)
            {
                //open existing file

                result = Avi.AVIFileOpen(
                    ref aviFile, fileName,
                    Avi.OF_READWRITE, 0);
            }
            else
            {
                //create empty file

                result = Avi.AVIFileOpen(
                    ref aviFile, fileName,
                    Avi.OF_WRITE | Avi.OF_CREATE, 0);
            }

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileOpen: " + result.ToString());
            }
        }
Exemplo n.º 6
0
        /// <summary>Initialize a VideoStream for an existing stream</summary>
        /// <param name="aviFile">The file that contains the stream</param>
        /// <param name="aviStream">An IAVISTREAM from [aviFile]</param>
        public VideoStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            //Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            //int size = Marshal.SizeOf(bih);
            Avi.BITMAPINFO bih  = new Avi.BITMAPINFO();
            int            size = Marshal.SizeOf(bih.bmiHeader);

            Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);

            if (bih.bmiHeader.biBitCount < 24)
            {
                size = Marshal.SizeOf(bih.bmiHeader) + Avi.PALETTE_SIZE;
                Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);
                CopyPalette(bih.bmiColors);
            }

            this.frameRate         = (float)streamInfo.dwRate / (float)streamInfo.dwScale;
            this.width             = (int)streamInfo.rcFrame.right;
            this.height            = (int)streamInfo.rcFrame.bottom;
            this.frameSize         = bih.bmiHeader.biSizeImage;
            this.countBitsPerPixel = bih.bmiHeader.biBitCount;
            this.firstFrame        = Avi.AVIStreamStart(aviStream.ToInt32());
            this.countFrames       = Avi.AVIStreamLength(aviStream.ToInt32());
        }
Exemplo n.º 7
0
        /// <summary>Add one frame to a new stream</summary>
        /// <param name="bmp"></param>
        /// <remarks>
        /// This works only with uncompressed streams,
        /// and compressed streams that have not been saved yet.
        /// Use DecompressToNewFile to edit saved compressed streams.
        /// </remarks>
        public void AddFrame(Bitmap bmp)
        {
            //bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

            // NEW 2012-11-10
            if (countFrames == 0)
            {
                CopyPalette(bmp.Palette);
                SetFormat(writeCompressed ? compressedStream : StreamPointer, countFrames);
            }

            BitmapData bmpDat = bmp.LockBits(
                new Rectangle(
                    0, 0, bmp.Width, bmp.Height),
                ImageLockMode.ReadOnly, bmp.PixelFormat);

            int result = Avi.AVIStreamWrite(writeCompressed ? compressedStream : StreamPointer,
                                            countFrames, 1,
                                            bmpDat.Scan0,
                                            (Int32)(bmpDat.Stride * bmpDat.Height),
                                            0, 0, 0);

            if (result != 0)
            {
                throw new Exception("Exception in VideoStreamWrite: " + result.ToString());
            }

            bmp.UnlockBits(bmpDat);

            countFrames++;
        }
Exemplo n.º 8
0
        /// <summary>Apply a format to a new stream</summary>
        /// <param name="aviStream">The IAVISTREAM</param>
        /// <remarks>
        /// The format must be set before the first frame can be written,
        /// and it cannot be changed later.
        /// </remarks>
        private void SetFormat(IntPtr aviStream, int writePosition)
        {
            Avi.BITMAPINFO bi = new Avi.BITMAPINFO();
            bi.bmiHeader.biWidth     = width;
            bi.bmiHeader.biHeight    = height;
            bi.bmiHeader.biPlanes    = 1;
            bi.bmiHeader.biBitCount  = countBitsPerPixel;
            bi.bmiHeader.biSizeImage = frameSize;
            bi.bmiHeader.biSize      = Marshal.SizeOf(bi.bmiHeader);

            if (countBitsPerPixel < 24)
            {
                bi.bmiHeader.biClrUsed      = this.palette.Length;
                bi.bmiHeader.biClrImportant = this.palette.Length;
                bi.bmiColors = new Avi.RGBQUAD[this.palette.Length];
                this.palette.CopyTo(bi.bmiColors, 0);
                bi.bmiHeader.biSize += bi.bmiColors.Length * Avi.RGBQUAD_SIZE;
            }

            int result = Avi.AVIStreamSetFormat(aviStream, writePosition, ref bi, bi.bmiHeader.biSize);

            if (result != 0)
            {
                throw new Exception("Error in VideoStreamSetFormat: " + result.ToString("X"));
            }
        }
Exemplo n.º 9
0
        /*
         *      /// <summary>Export a frame into a bitmap</summary>
         *      /// <param name="position">Position of the frame</param>
         *      public Bitmap GetBitmap(int position){
         *              if(position > countFrames){
         *                      throw new Exception("Invalid frame position: "+position);
         *              }
         *
         *  Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);
         *
         *  //Decompress the frame and return a pointer to the DIB
         *  int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);
         *              //Copy the bitmap header into a managed struct
         *              Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
         *              bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), bih.GetType());
         *
         *              if(bih.biSizeImage < 1){
         *                      throw new Exception("Exception in VideoStreamGetFrame");
         *              }
         *
         *              //copy the image
         *
         *              byte[] bitmapData;
         *              int address = dib + Marshal.SizeOf(bih);
         *              if(bih.biBitCount < 16){
         *                      //copy palette and pixels
         *                      bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
         *              }else{
         *                      //copy only pixels
         *                      bitmapData = new byte[bih.biSizeImage];
         *              }
         *
         *  Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);
         *
         *              //copy bitmap info
         *              byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
         *              IntPtr ptr;
         *              ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
         *              Marshal.StructureToPtr(bih, ptr, false);
         *              address = ptr.ToInt32();
         *  Marshal.Copy(new IntPtr(address), bitmapInfo, 0, bitmapInfo.Length);
         *
         *  Marshal.FreeHGlobal(ptr);
         *
         *              //create file header
         *              Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
         *              bfh.bfType = Avi.BMP_MAGIC_COOKIE;
         *              bfh.bfSize = (Int32)(55 + bih.biSizeImage); //size of file as written to disk
         *              bfh.bfReserved1 = 0;
         *              bfh.bfReserved2 = 0;
         *              bfh.bfOffBits = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
         *              if(bih.biBitCount < 16){
         *                      //There is a palette between header and pixel data
         *                      bfh.bfOffBits += Avi.PALETTE_SIZE;
         *              }
         *
         *              //write a bitmap stream
         *              BinaryWriter bw = new BinaryWriter( new MemoryStream() );
         *
         *              //write header
         *              bw.Write(bfh.bfType);
         *              bw.Write(bfh.bfSize);
         *              bw.Write(bfh.bfReserved1);
         *              bw.Write(bfh.bfReserved2);
         *              bw.Write(bfh.bfOffBits);
         *              //write bitmap info
         *              bw.Write(bitmapInfo);
         *              //write bitmap data
         *              bw.Write(bitmapData);
         *
         *              Bitmap bmp = (Bitmap)Image.FromStream(bw.BaseStream);
         *              Bitmap saveableBitmap = new Bitmap(bmp.Width, bmp.Height);
         *              Graphics g = Graphics.FromImage(saveableBitmap);
         *              g.DrawImage(bmp, 0,0);
         *              g.Dispose();
         *              bmp.Dispose();
         *
         *              bw.Close();
         *              return saveableBitmap;
         *      }*/

        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            // Get the pointer to the BITMAPINFOHEADER
            int    firstFrame = Avi.AVIStreamStart(aviStream.ToInt32());
            IntPtr bitmapPtr  = new IntPtr(Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position));

            // Copy into a BITMAPINFOHEADER structure
            Avi.BITMAPINFOHEADER bitmapInfo = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(bitmapPtr, typeof(Avi.BITMAPINFOHEADER));

            // Find the offset of the raw image data
            IntPtr imagePtr = new IntPtr(bitmapPtr.ToInt32() + bitmapInfo.biSize);

            // Determine the stride
            int stride = bitmapInfo.biSizeImage / bitmapInfo.biHeight;

            // Determine the pixel format
            PixelFormat pixelFormat = ConvertBitCountToPixelFormat(bitmapInfo.biBitCount);

            // Create a bitmap from the raw image data
            Bitmap bmp = new Bitmap(bitmapInfo.biWidth, bitmapInfo.biHeight, stride, pixelFormat, imagePtr);

            // Flip the bitmap vertically
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

            return(bmp);
        }
Exemplo n.º 10
0
        /// <summary>Add an existing wave audio stream to the file</summary>
        /// <param name="newStream">The stream to add</param>
        public void AddAudioStream(AudioStream newStream)
        {
            Avi.AVISTREAMINFO streamInfo   = new Avi.AVISTREAMINFO();
            Avi.PCMWAVEFORMAT streamFormat = new Avi.PCMWAVEFORMAT();
            int    streamLength            = 0;
            IntPtr waveData = newStream.GetStreamData(ref streamInfo, ref streamFormat, ref streamLength);

            IntPtr aviStream;
            int    result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref streamInfo);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }

            result = Avi.AVIStreamSetFormat(aviStream, 0, ref streamFormat, Marshal.SizeOf(streamFormat));
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamSetFormat: " + result.ToString());
            }

            result = Avi.AVIStreamWrite(aviStream, 0, streamLength, waveData, streamLength, Avi.AVIIF_KEYFRAME, 0, 0);
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamWrite: " + result.ToString());
            }

            result = Avi.AVIStreamRelease(aviStream);
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamRelease: " + result.ToString());
            }
        }
Exemplo n.º 11
0
 /// <summary>Close the stream</summary>
 public virtual void Close()
 {
     if (this.writeCompressed)
     {
         Avi.AVIStreamRelease(this.compressedStream);
     }
     Avi.AVIStreamRelease(this.StreamPointer);
 }
        /// <summary>Read the stream's format information</summary>
        /// <returns>PCMWAVEFORMAT</returns>
        public Avi.PCMWAVEFORMAT GetFormat()
        {
            Avi.PCMWAVEFORMAT format = new Avi.PCMWAVEFORMAT();
            int size   = Marshal.SizeOf(format);
            int result = Avi.AVIStreamReadFormat(aviStream, 0, ref format, ref size);

            return(format);
        }
Exemplo n.º 13
0
 /// <summary>Free ressources that have been used by GetFrameOpen</summary>
 public void GetFrameClose()
 {
     if (getFrameObject != 0)
     {
         Avi.AVIStreamGetFrameClose(getFrameObject);
         getFrameObject = 0;
     }
 }
Exemplo n.º 14
0
 public virtual void Close()
 {
     if (writeCompressed)
     {
         Avi.AVIStreamRelease(compressedStream);
     }
     Avi.AVIStreamRelease(aviStream);
 }
Exemplo n.º 15
0
        /// <summary>Initialize an AudioStream for an existing stream</summary>
        /// <param name="aviFile">The file that contains the stream</param>
        /// <param name="aviStream">An IAVISTREAM from [aviFile]</param>
        public AudioStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;
            int size = Marshal.SizeOf(this.waveFormat);

            Avi.AVIStreamReadFormat(aviStream, 0, ref this.waveFormat, ref size);
            Avi.AVISTREAMINFO streamInfo = this.GetStreamInfo(aviStream);
        }
Exemplo n.º 16
0
 /// <summary>Release all ressources</summary>
 public void Close()
 {
     foreach (AviStream stream in this.streams)
     {
         stream.Close();
     }
     Avi.AVIFileRelease(this.aviFile);
     Avi.AVIFileExit();
 }
Exemplo n.º 17
0
        /// <summary>Change the AviStreamInfo values and update the frame rate</summary>
        /// <param name="info"></param>
        public void SetInfo(Avi.AVISTREAMINFO info)
        {
            int result = Avi.EditStreamSetInfo(this.editableStream, ref info, Marshal.SizeOf(info));

            if (result != 0)
            {
                throw new Exception("Exception in SetInfo: " + result.ToString());
            }
            this.frameRate = info.dwRate / info.dwScale;
        }
Exemplo n.º 18
0
        /// <summary>Prepare for decompressing frames</summary>
        /// <remarks>
        /// This method has to be called before GetBitmap and ExportBitmap.
        /// Release ressources with GetFrameClose.
        /// </remarks>
        public void GetFrameOpen()
        {
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            //Open frames

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            //  bih.biBitCount = countBitsPerPixel;
            bih.biBitCount      = 24;
            bih.biClrImportant  = 0;
            bih.biClrUsed       = 0;
            bih.biCompression   = 0;
            bih.biPlanes        = 1;
            bih.biSize          = Marshal.SizeOf(bih);
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;

            // Corrections by M. Covington:
            // If these are pre-set, interlaced video is not handled correctly.
            // Better to give zeroes and let Windows fill them in.
            bih.biHeight = 0;            // was (Int32)streamInfo.rcFrame.bottom;
            bih.biWidth  = 0;            // was (Int32)streamInfo.rcFrame.right;

            // Corrections by M. Covington:
            // Validate the bit count, because some AVI files give a bit count
            // that is not one of the allowed values in a BitmapInfoHeader.
            // Here 0 means for Windows to figure it out from other information.
            if (bih.biBitCount > 24)
            {
                bih.biBitCount = 32;
            }
            else if (bih.biBitCount > 16)
            {
                bih.biBitCount = 24;
            }
            else if (bih.biBitCount > 8)
            {
                bih.biBitCount = 16;
            }
            else if (bih.biBitCount > 4)
            {
                bih.biBitCount = 8;
            }
            else if (bih.biBitCount > 0)
            {
                bih.biBitCount = 4;
            }

            getFrameObject = Avi.AVIStreamGetFrameOpen(StreamPointer, ref bih);

            if (getFrameObject == 0)
            {
                throw new Exception("Exception in VideoStreamGetFrameOpen!");
            }
        }
Exemplo n.º 19
0
        /// <summary>Create a compressed stream from an uncompressed stream</summary>
        private void CreateCompressedStream(Avi.AVICOMPRESSOPTIONS options)
        {
            int result = Avi.AVIMakeCompressedStream(out this.compressedStream, this.aviStream, ref options, 0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIMakeCompressedStream: " + result.ToString());
            }
            this.compressOptions = options;
            this.SetFormat(this.compressedStream);
        }
        /// <summary>Read the stream's header information</summary>
        /// <param name="aviStream">The IAVISTREAM to read from</param>
        /// <returns>AVISTREAMINFO</returns>
        private Avi.AVISTREAMINFO GetStreamInfo(IntPtr aviStream)
        {
            Avi.AVISTREAMINFO streamInfo = new Avi.AVISTREAMINFO();
            int result = Avi.AVIStreamInfo(aviStream, ref streamInfo, Marshal.SizeOf(streamInfo));

            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamInfo: " + result.ToString());
            }
            return(streamInfo);
        }
Exemplo n.º 21
0
        /// <summary>Copy a number of frames into a temporary stream</summary>
        /// <param name="start">First frame to copy</param>
        /// <param name="length">Count of frames to copy</param>
        /// <returns>Pointer to the unmanaged temporary stream</returns>
        public IntPtr Copy(int start, int length)
        {
            IntPtr copyPointer = IntPtr.Zero;
            int    result      = Avi.EditStreamCopy(this.editableStream, ref start, ref length, ref copyPointer);

            if (result != 0)
            {
                throw new Exception("Exception in Copy: " + result.ToString());
            }
            return(copyPointer);
        }
Exemplo n.º 22
0
        /// <summary>Move a number of frames into a temporary stream</summary>
        /// <param name="start">First frame to cut</param>
        /// <param name="length">Count of frames to cut</param>
        /// <returns>Pointer to the unmanaged temporary stream</returns>
        public IntPtr Cut(int start, int length)
        {
            IntPtr copyPointer = IntPtr.Zero;
            int    result      = Avi.EditStreamCut(this.editableStream, ref start, ref length, ref copyPointer);

            if (result != 0)
            {
                throw new Exception("Exception in Cut: " + result.ToString());
            }
            this.countFrames -= length;
            return(copyPointer);
        }
Exemplo n.º 23
0
        /// <summary>Paste a number of frames from another video stream into this stream</summary>
        /// <param name="sourceStream">Pointer to the unmanaged stream to copy from</param>
        /// <param name="copyPosition">Index of the first frame to copy</param>
        /// <param name="pastePosition">Where to paste the copied frames</param>
        /// <param name="length">Count of frames to paste</param>
        public void Paste(IntPtr sourceStream, int copyPosition, int pastePosition, int length)
        {
            int pastedLength = 0;
            int result       = Avi.EditStreamPaste(this.editableStream, ref pastePosition, ref pastedLength, sourceStream,
                                                   copyPosition, length);

            if (result != 0)
            {
                throw new Exception("Exception in Paste: " + result.ToString());
            }
            this.countFrames += pastedLength;
        }
Exemplo n.º 24
0
        /// <summary>Create an editable stream from an uneditable stream</summary>
        /// <param name="stream">uneditable stream</param>
        public EditableVideoStream(VideoStream stream) : base(stream.FrameSize, stream.FrameRate, stream.Width, stream.Height, stream.CountBitsPerPixel, stream.CountFrames, stream.CompressOptions, stream.WriteCompressed)
        {
            Avi.AVIFileInit();
            int result = Avi.CreateEditableStream(ref editableStream, stream.StreamPointer);

            if (result != 0)
            {
                throw new Exception("Exception in CreateEditableStream: " + result.ToString());
            }

            SetInfo(stream.StreamInfo);
        }
Exemplo n.º 25
0
        /// <summary>Copy the stream into a new file</summary>
        /// <param name="fileName">Name of the new file</param>
        public override void ExportStream(String fileName)
        {
            Avi.AVICOMPRESSOPTIONS_CLASS opts = new Avi.AVICOMPRESSOPTIONS_CLASS();
            opts.fccType  = (uint)Avi.streamtypeVIDEO;
            opts.lpParms  = IntPtr.Zero;
            opts.lpFormat = IntPtr.Zero;
            IntPtr streamPointer = StreamPointer;

            Avi.AVISaveOptions(IntPtr.Zero, Avi.ICMF_CHOOSE_KEYFRAME | Avi.ICMF_CHOOSE_DATARATE, 1, ref streamPointer, ref opts);
            Avi.AVISaveOptionsFree(1, ref opts);

            Avi.AVISaveV(fileName, 0, 0, 1, ref aviStream, ref opts);
        }
Exemplo n.º 26
0
        public static void MakeFileFromStream(String fileName, AviStream stream)
        {
            IntPtr newFile       = IntPtr.Zero;
            IntPtr streamPointer = stream.StreamPointer;

            Avi.AVICOMPRESSOPTIONS_CLASS opts = new Avi.AVICOMPRESSOPTIONS_CLASS();
            opts.fccType  = (uint)Avi.streamtypeVIDEO;
            opts.lpParms  = IntPtr.Zero;
            opts.lpFormat = IntPtr.Zero;
            Avi.AVISaveOptions(IntPtr.Zero, Avi.ICMF_CHOOSE_KEYFRAME | Avi.ICMF_CHOOSE_DATARATE, 1, ref streamPointer, ref opts);
            Avi.AVISaveOptionsFree(1, ref opts);

            Avi.AVISaveV(fileName, 0, 0, 1, ref streamPointer, ref opts);
        }
Exemplo n.º 27
0
        /// <summary>Add an existing wave audio stream to the file</summary>
        /// <param name="newStream">The stream to add</param>
        /// <param name="startAtFrameIndex">
        /// The index of the video frame at which the sound is going to start.
        /// '0' inserts the sound at the beginning of the video.
        /// </param>
        public void AddAudioStream(AudioStream newStream, int startAtFrameIndex)
        {
            Avi.AVISTREAMINFO streamInfo   = new Avi.AVISTREAMINFO();
            Avi.PCMWAVEFORMAT streamFormat = new Avi.PCMWAVEFORMAT();
            int streamLength = 0;

            IntPtr rawData  = newStream.GetStreamData(ref streamInfo, ref streamFormat, ref streamLength);
            IntPtr waveData = rawData;

            if (startAtFrameIndex > 0)
            {
                //not supported
                //streamInfo.dwStart = startAtFrameIndex;

                double framesPerSecond  = GetVideoStream().FrameRate;
                double samplesPerSecond = newStream.CountSamplesPerSecond;
                double startAtSecond    = startAtFrameIndex / framesPerSecond;
                int    startAtSample    = (int)(samplesPerSecond * startAtSecond);

                waveData = InsertSilence(startAtSample - 1, waveData, streamLength, ref streamInfo);
            }

            IntPtr aviStream;
            int    result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref streamInfo);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }

            result = Avi.AVIStreamSetFormat(aviStream, 0, ref streamFormat, Marshal.SizeOf(streamFormat));
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamSetFormat: " + result.ToString());
            }

            result = Avi.AVIStreamWrite(aviStream, 0, streamLength, waveData, streamLength, Avi.AVIIF_KEYFRAME, 0, 0);
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamWrite: " + result.ToString());
            }

            result = Avi.AVIStreamRelease(aviStream);
            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamRelease: " + result.ToString());
            }

            Marshal.FreeHGlobal(waveData);
        }
Exemplo n.º 28
0
        /// <summary>Get the first video stream - usually there is only one video stream</summary>
        /// <returns>VideoStream object for the stream</returns>
        public VideoStream GetVideoStream()
        {
            IntPtr aviStream;
            int    result = Avi.AVIFileGetStream(this.aviFile, out aviStream, Avi.streamtypeVIDEO, 0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileGetStream: " + result.ToString());
            }
            VideoStream stream = new VideoStream(this.aviFile, aviStream);

            this.streams.Add(stream);
            return(stream);
        }
Exemplo n.º 29
0
        /// <summary>Getthe first wave audio stream</summary>
        /// <returns>AudioStream object for the stream</returns>
        public AudioStream GetWaveStream()
        {
            IntPtr aviStream;
            int    result = Avi.AVIFileGetStream(this.aviFile, out aviStream, Avi.streamtypeAUDIO, 0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileGetStream: " + result.ToString());
            }
            AudioStream stream = new AudioStream(this.aviFile, aviStream);

            this.streams.Add(stream);
            return(stream);
        }
Exemplo n.º 30
0
        /// <summary>Returns all data needed to copy the stream</summary>
        /// <remarks>Do not forget to call Marshal.FreeHGlobal and release the raw data pointer</remarks>
        /// <param name="streamInfo">Receives the header information</param>
        /// <param name="format">Receives the format</param>
        /// <param name="streamLength">Receives the length of the stream</param>
        /// <returns>Pointer to the wave data</returns>
        public IntPtr GetStreamData(ref Avi.AVISTREAMINFO streamInfo, ref Avi.PCMWAVEFORMAT format, ref int streamLength)
        {
            streamInfo = this.GetStreamInfo();
            format     = this.GetFormat();
            //length in bytes = length in samples * length of a sample
            streamLength = Avi.AVIStreamLength(this.aviStream.ToInt32()) * streamInfo.dwSampleSize;
            IntPtr waveData = Marshal.AllocHGlobal(streamLength);
            int    result   = Avi.AVIStreamRead(this.aviStream, 0, streamLength, waveData, streamLength, 0, 0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamRead: " + result.ToString());
            }
            return(waveData);
        }
		/// <summary>Returns all data needed to copy the stream</summary>
		/// <remarks>Do not forget to call Marshal.FreeHGlobal and release the raw data pointer</remarks>
		/// <param name="streamInfo">Receives the header information</param>
		/// <param name="format">Receives the format</param>
		/// <param name="streamLength">Receives the length of the stream</param>
		/// <returns>Pointer to the wave data</returns>
		public IntPtr GetStreamData(ref Avi.AVISTREAMINFO streamInfo, ref Avi.PCMWAVEFORMAT format, ref int streamLength){
			streamInfo = GetStreamInfo();
			
			format = GetFormat();
			//length in bytes = length in samples * length of a sample
            streamLength = Avi.NativeMethods.AVIStreamLength(aviStream.ToInt32()) * streamInfo.dwSampleSize;
			IntPtr waveData = Marshal.AllocHGlobal(streamLength);

            int result = Avi.NativeMethods.AVIStreamRead(aviStream, 0, streamLength, waveData, streamLength, 0, 0);
			if(result != 0){
				throw new Exception("Exception in AVIStreamRead: "+result.ToString());
			}
			
			return waveData;
		}
Exemplo n.º 32
0
 /// <summary>Initialize a new VideoStream and add the first frame</summary>
 /// <param name="aviFile">The file that contains the stream</param>
 /// <param name="writeCompressed">true: create a compressed stream before adding frames</param>
 /// <param name="frameRate">Frames per second</param>
 /// <param name="firstFrame">Image to write into the stream as the first frame</param>
 public VideoStream(int aviFile, Avi.AVICOMPRESSOPTIONS compressOptions, double frameRate, Bitmap firstFrame)
 {
     Initialize(aviFile, true, frameRate, firstFrame);
     CreateStream(compressOptions);
     AddFrame(firstFrame);
 }
Exemplo n.º 33
0
 /// <summary>Create a new stream</summary>
 private void CreateStream(Avi.AVICOMPRESSOPTIONS options)
 {
     CreateStreamWithoutFormat();
     CreateCompressedStream(options);
 }
Exemplo n.º 34
0
        /// <summary>Create a compressed stream from an uncompressed stream</summary>
        private void CreateCompressedStream(Avi.AVICOMPRESSOPTIONS options)
        {
            int result = Avi.AVIMakeCompressedStream(out compressedStream, aviStream, ref options, 0);
            if (result != 0) {
                throw new Exception("Exception in AVIMakeCompressedStream: " + result.ToString());
            }

            this.compressOptions = options;

            SetFormat(compressedStream);
        }
Exemplo n.º 35
0
 /// <summary>Copy all properties from one VideoStream to another one</summary>
 /// <remarks>Used by EditableVideoStream</remarks>
 /// <param name="frameSize"></param><param name="frameRate"></param>
 /// <param name="width"></param><param name="height"></param>
 /// <param name="countBitsPerPixel"></param>
 /// <param name="countFrames"></param><param name="compressOptions"></param>
 internal VideoStream(int frameSize, double frameRate, int width, int height, Int16 countBitsPerPixel, int countFrames, Avi.AVICOMPRESSOPTIONS compressOptions, bool writeCompressed)
 {
     this.frameSize = frameSize;
     this.frameRate = frameRate;
     this.width = width;
     this.height = height;
     this.countBitsPerPixel = countBitsPerPixel;
     this.countFrames = countFrames;
     this.compressOptions = compressOptions;
     this.writeCompressed = writeCompressed;
     this.firstFrame = 0;
 }
Exemplo n.º 36
0
        public IntPtr GetRawBitmap(int position, out Avi.BITMAPINFOHEADER bih, out int dataLength)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }
            if (getFrameObject == 0)
            {
                throw new Exception("GetFrameOpen has not been called yet");
            }

            int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);
            bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), typeof(Avi.BITMAPINFOHEADER));

            if(bih.biSizeImage < 1){
                throw new Exception("Exception in VideoStreamGetFrame");
            }

            dataLength = bih.biSizeImage;
            if (bih.biBitCount < 16)
            {
                dataLength += Avi.PALETTE_SIZE;
            }
            return new IntPtr(dib + SIZE_BIH);
        }
Exemplo n.º 37
0
        /// <summary>Add an existing wave audio stream to the file</summary>
        /// <param name="waveData">The new stream's data</param>
        /// <param name="streamInfo">Header info for the new stream</param>
        /// <param name="streamFormat">The new stream' format info</param>
        /// <param name="streamLength">Length of the new stream</param>
        public void AddAudioStream(IntPtr waveData, Avi.AVISTREAMINFO streamInfo, Avi.PCMWAVEFORMAT streamFormat, int streamLength) {
            IntPtr aviStream;
            int result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref streamInfo);
            if (result != 0) {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }

            result = Avi.AVIStreamSetFormat(aviStream, 0, ref streamFormat, Marshal.SizeOf(streamFormat));
            if (result != 0) {
                throw new Exception("Exception in AVIStreamSetFormat: " + result.ToString());
            }

            result = Avi.AVIStreamWrite(aviStream, 0, streamLength, waveData, streamLength, Avi.AVIIF_KEYFRAME, 0, 0);
            if (result != 0) {
                throw new Exception("Exception in AVIStreamWrite: " + result.ToString());
            }

            result = Avi.AVIStreamRelease(aviStream);
            if (result != 0) {
                throw new Exception("Exception in AVIStreamRelease: " + result.ToString());
            }
        }
Exemplo n.º 38
0
        private IntPtr InsertSilence(int countSilentSamples, IntPtr waveData, int lengthWave, ref Avi.AVISTREAMINFO streamInfo) {
            //initialize silence
            int lengthSilence = countSilentSamples * streamInfo.dwSampleSize;
            byte[] silence = new byte[lengthSilence];

            //initialize new sound
            int lengthNewStream = lengthSilence + lengthWave;
            IntPtr newWaveData = Marshal.AllocHGlobal(lengthNewStream);

            //copy silence
            Marshal.Copy(silence, 0, newWaveData, lengthSilence);

            //copy sound
            byte[] sound = new byte[lengthWave];
            Marshal.Copy(waveData, sound, 0, lengthWave);
            IntPtr startOfSound = new IntPtr(newWaveData.ToInt32() + lengthSilence);
            Marshal.Copy(sound, 0, startOfSound, lengthWave);

            Marshal.FreeHGlobal(newWaveData);

            streamInfo.dwLength = lengthNewStream;
            return newWaveData;
        }
Exemplo n.º 39
0
 /// <summary>Add an empty video stream to the file</summary>
 /// <remarks>Compresses the stream without showing the codecs dialog</remarks>
 /// <param name="compressOptions">Compression options</param>
 /// <param name="frameRate">Frames per second</param>
 /// <param name="firstFrame">Image to write into the stream as the first frame</param>
 /// <returns>VideoStream object for the new stream</returns>
 public VideoStream AddVideoStream(Avi.AVICOMPRESSOPTIONS compressOptions, double frameRate, Bitmap firstFrame) {
     VideoStream stream = new VideoStream(aviFile, compressOptions, frameRate, firstFrame);
     streams.Add(stream);
     return stream;
 }
Exemplo n.º 40
0
        /// <summary>Copy a palette</summary>
        /// <param name="template">Original palette</param>
        private void CopyPalette(Avi.RGBQUAD[] template)
        {
            this.palette = new Avi.RGBQUAD[template.Length];

            for (int n = 0; n < this.palette.Length; n++)
            {
                if (n < template.Length)
                {
                    this.palette[n].rgbRed = template[n].rgbRed;
                    this.palette[n].rgbGreen = template[n].rgbGreen;
                    this.palette[n].rgbBlue = template[n].rgbBlue;
                }
                else
                {
                    this.palette[n].rgbRed = 0;
                    this.palette[n].rgbGreen = 0;
                    this.palette[n].rgbBlue = 0;
                }
            }
        }
 /// <summary>Change the AviStreamInfo values and update the frame rate</summary>
 /// <param name="info"></param>
 public void SetInfo(Avi.AVISTREAMINFO info) {
     int result = Avi.NativeMethods.EditStreamSetInfo(editableStream, ref info, Marshal.SizeOf(info));
     if (result != 0) {
         throw new Exception("Exception in SetInfo: " + result.ToString());
     }
 
     frameRate = info.dwRate / info.dwScale;
 }