Example #1
0
        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());
            }
        }
Example #2
0
 public void GetFrameClose()
 {
     if (getFrameObject != 0)
     {
         Avi.AVIStreamGetFrameClose(getFrameObject);
         getFrameObject = 0;
     }
 }
Example #3
0
        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);
        }
Example #4
0
 public virtual void Close()
 {
     if (writeCompressed)
     {
         Avi.AVIStreamRelease(compressedStream);
     }
     Avi.AVIStreamRelease(StreamPointer);
 }
Example #5
0
        public void Close()
        {
            foreach (AviStream stream in streams)
            {
                stream.Close();
            }

            Avi.AVIFileRelease(aviFile);
            Avi.AVIFileExit();
        }
Example #6
0
        public AudioStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;

            int size = Marshal.SizeOf(waveFormat);

            Avi.AVIStreamReadFormat(aviStream, 0, ref waveFormat, ref size);
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);
        }
Example #7
0
        private Avi.AVISTREAMINFO GetStreamInfo(IntPtr aviStream)
        {
            Avi.AVISTREAMINFO streamInfo = new Avi.AVISTREAMINFO();
            int result = Avi.AVIStreamInfo(StreamPointer, ref streamInfo, Marshal.SizeOf(streamInfo));

            if (result != 0)
            {
                throw new Exception("Exception in VideoStreamInfo: " + result.ToString());
            }
            return(streamInfo);
        }
Example #8
0
        public void SetInfo(Avi.AVISTREAMINFO info)
        {
            int result = Avi.EditStreamSetInfo(editableStream, ref info, Marshal.SizeOf(info));

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

            frameRate = info.dwRate / info.dwScale;
        }
Example #9
0
        public void Paste(IntPtr sourceStream, int copyPosition, int pastePosition, int length)
        {
            int pastedLength = 0;
            int result       = Avi.EditStreamPaste(editableStream, ref pastePosition, ref pastedLength, sourceStream, copyPosition, length);

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

            countFrames += pastedLength;
        }
Example #10
0
        public IntPtr Copy(int start, int length)
        {
            IntPtr copyPointer = IntPtr.Zero;
            int    result      = Avi.EditStreamCopy(editableStream, ref start, ref length, ref copyPointer);

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

            return(copyPointer);
        }
Example #11
0
        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);
        }
Example #12
0
        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);
        }
Example #13
0
        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);
        }
Example #14
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);
        }
Example #15
0
        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)
            {
                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);
        }
Example #16
0
        public override void ExportStream(String fileName)
        {
            Avi.AVICOMPRESSOPTIONS_CLASS opts = new Avi.AVICOMPRESSOPTIONS_CLASS();
            opts.fccType           = (UInt32)Avi.mmioStringToFOURCC("auds", 0);
            opts.fccHandler        = (UInt32)Avi.mmioStringToFOURCC("CAUD", 0);
            opts.dwKeyFrameEvery   = 0;
            opts.dwQuality         = 0;
            opts.dwFlags           = 0;
            opts.dwBytesPerSecond  = 0;
            opts.lpFormat          = new IntPtr(0);
            opts.cbFormat          = 0;
            opts.lpParms           = new IntPtr(0);
            opts.cbParms           = 0;
            opts.dwInterleaveEvery = 0;

            Avi.AVISaveV(fileName, 0, 0, 1, ref aviStream, ref opts);
        }
Example #17
0
        public IntPtr GetStreamData(ref Avi.AVISTREAMINFO streamInfo, ref Avi.PCMWAVEFORMAT format, ref int streamLength)
        {
            streamInfo = GetStreamInfo();

            format       = GetFormat();
            streamLength = Avi.AVIStreamLength(aviStream.ToInt32()) * streamInfo.dwSampleSize;
            IntPtr waveData = Marshal.AllocHGlobal(streamLength);

            int result = Avi.AVIStreamRead(aviStream, 0, streamLength, waveData, streamLength, 0, 0);

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

            return(waveData);
        }
Example #18
0
        private void SetFormat(IntPtr aviStream)
        {
            Avi.BITMAPINFOHEADER bi = new Avi.BITMAPINFOHEADER();
            bi.biSize      = Marshal.SizeOf(bi);
            bi.biWidth     = width;
            bi.biHeight    = height;
            bi.biPlanes    = 1;
            bi.biBitCount  = countBitsPerPixel;
            bi.biSizeImage = frameSize;

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

            if (result != 0)
            {
                throw new Exception("Error in VideoStreamSetFormat: " + result.ToString());
            }
        }
Example #19
0
        public void GetFrameOpen()
        {
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

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

            bih.biHeight = 0;
            bih.biWidth  = 0;

            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!");
            }
        }
Example #20
0
        public VideoStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;

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

            Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            this.frameRate         = (float)streamInfo.dwRate / (float)streamInfo.dwScale;
            this.width             = (int)streamInfo.rcFrame.right;
            this.height            = (int)streamInfo.rcFrame.bottom;
            this.frameSize         = bih.biSizeImage;
            this.countBitsPerPixel = bih.biBitCount;
            this.firstFrame        = Avi.AVIStreamStart(aviStream.ToInt32());
            this.countFrames       = Avi.AVIStreamLength(aviStream.ToInt32());
        }
Example #21
0
        public VideoStream GetVideoStream()
        {
            IntPtr aviStream;

            int result = Avi.AVIFileGetStream(
                aviFile,
                out aviStream,
                Avi.streamtypeVIDEO, 0);

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

            VideoStream stream = new VideoStream(aviFile, aviStream);

            streams.Add(stream);
            return(stream);
        }
Example #22
0
        public AudioStream GetWaveStream()
        {
            IntPtr aviStream;

            int result = Avi.AVIFileGetStream(
                aviFile,
                out aviStream,
                Avi.streamtypeAUDIO, 0);

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

            AudioStream stream = new AudioStream(aviFile, aviStream);

            streams.Add(stream);
            return(stream);
        }
Example #23
0
        private void CreateCompressedStream()
        {
            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 aviStream, ref options);
            Avi.AVISaveOptionsFree(1, ref options);

            this.compressOptions = options.ToStruct();
            int result = Avi.AVIMakeCompressedStream(out compressedStream, aviStream, ref compressOptions, 0);

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

            SetFormat(compressedStream);
        }
Example #24
0
        public AviManager(String fileName, bool open)
        {
            Avi.AVIFileInit();
            int result;

            if (open)
            {
                result = Avi.AVIFileOpen(
                    ref aviFile, fileName,
                    Avi.OF_READWRITE, 0);
            }
            else
            {
                result = Avi.AVIFileOpen(
                    ref aviFile, fileName,
                    Avi.OF_WRITE | Avi.OF_CREATE, 0);
            }

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileOpen: " + result.ToString());
            }
        }
Example #25
0
        private void CreateStreamWithoutFormat()
        {
            int    scale = 1;
            double rate  = frameRate;

            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;
            strhdr.dwStart               = 0;
            strhdr.dwLength              = 0;
            strhdr.dwInitialFrames       = 0;
            strhdr.dwSuggestedBufferSize = frameSize;
            strhdr.dwQuality             = -1;
            strhdr.dwSampleSize          = 0;
            strhdr.rcFrame.top           = 0;
            strhdr.rcFrame.left          = 0;
            strhdr.rcFrame.bottom        = (uint)height;
            strhdr.rcFrame.right         = (uint)width;
            strhdr.dwEditCount           = 0;
            strhdr.dwFormatChangeCount   = 0;
            strhdr.szName                = new UInt16[64];

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

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }
        }
Example #26
0
        public void AddFrame(Bitmap bmp)
        {
            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 : 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++;
        }
Example #27
0
 public override void Close()
 {
     base.Close();
     Avi.AVIFileExit();
 }
Example #28
0
        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

            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");
            }

            byte[] bitmapData;
            int    address = dib + Marshal.SizeOf(bih);

            if (bih.biBitCount < 16)
            {
                bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
            }
            else
            {
                bitmapData = new byte[bih.biSizeImage];
            }

            Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);

            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);

            Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
            bfh.bfType      = Avi.BMP_MAGIC_COOKIE;
            bfh.bfSize      = (Int32)(55 + bih.biSizeImage);
            bfh.bfReserved1 = 0;
            bfh.bfReserved2 = 0;
            bfh.bfOffBits   = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
            if (bih.biBitCount < 16)
            {
                bfh.bfOffBits += Avi.PALETTE_SIZE;
            }

            BinaryWriter bw = new BinaryWriter(new MemoryStream());

            bw.Write(bfh.bfType);
            bw.Write(bfh.bfSize);
            bw.Write(bfh.bfReserved1);
            bw.Write(bfh.bfReserved2);
            bw.Write(bfh.bfOffBits);
            bw.Write(bitmapInfo);
            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);
        }