Ejemplo n.º 1
0
        private void CreateVideoStream(int width, int height, int stride, int rate, int scale)
        {
            VfwApi.AVISTREAMINFO sinfo = new VfwApi.AVISTREAMINFO();
            sinfo.fccType               = (int)VfwApi.streamtypeVIDEO;
            sinfo.fccHandler            = (int)VfwApi.mmioFOURCC('D', 'I', 'B', ' ');
            sinfo.dwFlags               = 0;
            sinfo.dwCaps                = 0;
            sinfo.wPriority             = 0;
            sinfo.wLanguage             = 0;
            sinfo.dwScale               = scale;
            sinfo.dwRate                = rate;
            sinfo.dwStart               = 0;
            sinfo.dwLength              = 0;
            sinfo.dwInitialFrames       = 0;
            sinfo.dwSuggestedBufferSize = height * stride;
            sinfo.dwQuality             = -1;
            sinfo.dwSampleSize          = 0;
            sinfo.rcFrame               = new VfwApi.RECT();
            sinfo.rcFrame.top           = 0;
            sinfo.rcFrame.left          = 0;
            sinfo.rcFrame.bottom        = (uint)height;
            sinfo.rcFrame.right         = (uint)width;
            sinfo.dwEditCount           = 0;
            sinfo.dwFormatChangeCount   = 0;
            sinfo.szName                = new ushort[64];

            int hr = VfwApi.AVIFileCreateStream(pfile_, ref pavi_, ref sinfo);

            if (hr != 0)
            {
                throw new VfwException("AVIFileCreateStream", hr);
            }
        }
Ejemplo n.º 2
0
        private void CreateAudioStream(int sample, int length, int sample_size)
        {
            VfwApi.AVISTREAMINFO sinfo = new VfwApi.AVISTREAMINFO();
            sinfo.fccType               = (int)VfwApi.streamtypeAUDIO;
            sinfo.fccHandler            = 0;
            sinfo.dwFlags               = 0;
            sinfo.dwCaps                = 0;
            sinfo.wPriority             = 0;
            sinfo.wLanguage             = 0;
            sinfo.dwScale               = 1;
            sinfo.dwRate                = sample;
            sinfo.dwStart               = 0;
            sinfo.dwLength              = length;
            sinfo.dwInitialFrames       = 0;
            sinfo.dwSuggestedBufferSize = 0;
            sinfo.dwQuality             = 0;
            sinfo.dwSampleSize          = sample_size;
            sinfo.rcFrame               = new VfwApi.RECT();
            sinfo.rcFrame.top           = 0;
            sinfo.rcFrame.left          = 0;
            sinfo.rcFrame.bottom        = 0;
            sinfo.rcFrame.right         = 0;
            sinfo.dwEditCount           = 0;
            sinfo.dwFormatChangeCount   = 0;
            sinfo.szName                = new ushort[64];

            int hr = VfwApi.AVIFileCreateStream(pfile_, ref pavi_audio_, ref sinfo);

            if (hr != 0)
            {
                throw new VfwException("AVIFileCreateStream", hr);
            }
        }
Ejemplo n.º 3
0
        public void GetBitmapHeader()
        {
            IntPtr p = VfwApi.AVIStreamGetFrame(pgf_, 0);

            if (p == IntPtr.Zero)
            {
                throw new VfwException("先頭のフレーム取得に失敗しました。");
            }
            byte[] header = new byte[40];
            Marshal.Copy(p, header, 0, header.Length);
            MemoryStream ms     = new MemoryStream(header);
            BinaryReader reader = new BinaryReader(ms);

            reader.ReadInt32();
            width_  = reader.ReadInt32();
            height_ = reader.ReadInt32();
            if (height_ < 0)
            {
                is_flip_ = false;
                height_  = -height_;
            }
            reader.ReadInt16();
            bit_count_ = reader.ReadUInt16();
            if (bit_count_ != 24 && bit_count_ != 32)
            {
                throw new VfwException("24ビットと32ビットカラー以外には対応していません。");
            }
            ms.Close();
        }
Ejemplo n.º 4
0
 protected static void DeInitialize()
 {
     if (is_initialized_)
     {
         VfwApi.AVIFileExit();
         is_initialized_ = false;
     }
 }
Ejemplo n.º 5
0
 protected static void Initialize()
 {
     if (!is_initialized_)
     {
         VfwApi.AVIFileInit();
         is_initialized_ = true;
     }
 }
Ejemplo n.º 6
0
        public VfwAviWriter(string filename, int rate, int scale)
        {
            Initialize();
            int hr = VfwApi.AVIFileOpen(ref pfile_, filename, VfwApi.OF_CREATE | VfwApi.OF_SHARE_DENY_WRITE, IntPtr.Zero);

            if (hr != 0)
            {
                throw new VfwException("AVIFileOpen", hr);
            }
            rate_  = rate;
            scale_ = scale;
        }
Ejemplo n.º 7
0
        public void AddWave(string wave_filename)
        {
            byte[]       wave_data = File.ReadAllBytes(wave_filename);
            MemoryStream header    = new MemoryStream();

            header.Write(wave_data, 0, 36);
            header.Seek(0, SeekOrigin.Begin);
            BinaryReader reader = new BinaryReader(header);

            reader.ReadBytes(20);

            IntPtr wave_ptr      = Marshal.AllocHGlobal(wave_data.Length);
            IntPtr wave_data_ptr = new IntPtr(wave_ptr.ToInt32() + 44);

            try
            {
                Marshal.Copy(wave_data, 0, wave_ptr, wave_data.Length);

                VfwApi.PCMWAVEFORMAT pwformat = new VfwApi.PCMWAVEFORMAT();
                pwformat.wFormatTag      = reader.ReadInt16();
                pwformat.nChannels       = reader.ReadInt16();
                pwformat.nSamplesPerSec  = reader.ReadInt32();
                pwformat.nAvgBytesPerSec = reader.ReadInt32();
                pwformat.nBlockAlign     = reader.ReadInt16();
                pwformat.wBitsPerSample  = reader.ReadInt16();

                int sample_size = pwformat.nChannels * pwformat.wBitsPerSample / 8;

                CreateAudioStream(pwformat.nSamplesPerSec, (wave_data.Length - 44) / sample_size, sample_size);

                int hr = VfwApi.AVIStreamSetFormat(pavi_audio_, 0, ref pwformat, Marshal.SizeOf(pwformat));
                if (hr != 0)
                {
                    throw new VfwException("AVIStreamSetFormat", hr);
                }

                hr = VfwApi.AVIStreamWrite(pavi_audio_, 0, wave_data.Length - 44, wave_data_ptr,
                                           wave_data.Length - 44, 16, IntPtr.Zero, IntPtr.Zero);
                if (hr != 0)
                {
                    throw new VfwException("AVIStreamWrite", hr);
                }

                VfwApi.AVIStreamRelease(pavi_audio_);
                pavi_audio_ = IntPtr.Zero;
            }
            finally
            {
                Marshal.FreeHGlobal(wave_ptr);
            }
        }
Ejemplo n.º 8
0
 public void Close()
 {
     if (pavi_audio_ != IntPtr.Zero)
     {
         VfwApi.AVIStreamRelease(pavi_audio_);
         pavi_audio_ = IntPtr.Zero;
     }
     if (pavi_ != IntPtr.Zero)
     {
         VfwApi.AVIStreamRelease(pavi_);
         pavi_ = IntPtr.Zero;
     }
     if (pfile_ != IntPtr.Zero)
     {
         VfwApi.AVIFileRelease(pfile_);
         pfile_ = IntPtr.Zero;
     }
     DeInitialize();
 }
Ejemplo n.º 9
0
        private void SetVideoFormat(int width, int height, int stride, int bit_count)
        {
            VfwApi.BITMAPINFOHEADER binfo = new VfwApi.BITMAPINFOHEADER();
            binfo.biSize          = Marshal.SizeOf(binfo);
            binfo.biWidth         = width;
            binfo.biHeight        = height;
            binfo.biPlanes        = 1;
            binfo.biBitCount      = (short)bit_count;
            binfo.biCompression   = 0;
            binfo.biSizeImage     = height * stride;
            binfo.biXPelsPerMeter = 0;
            binfo.biYPelsPerMeter = 0;
            binfo.biClrUsed       = 0;
            binfo.biClrImportant  = 0;

            int hr = VfwApi.AVIStreamSetFormat(pavi_, 0, ref binfo, binfo.biSize);

            if (hr != 0)
            {
                throw new VfwException("AVIStreamSetFormat", hr);
            }
        }
Ejemplo n.º 10
0
        public Bitmap GetBitmap(int frame_number)
        {
            IntPtr p = VfwApi.AVIStreamGetFrame(pgf_, frame_start_ + frame_number);

            if (p == IntPtr.Zero)
            {
                throw new VfwException("AVIStreamGetFrame", 0);
            }
            p = new IntPtr(p.ToInt32() + 40);
            int stride = (width_ * bit_count_ / 8 + 3) & ~3;

            byte[] buffer = new byte[stride * height_ + 54];
            Marshal.Copy(p, buffer, 0, buffer.Length);
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            Bitmap   bitmap = new Bitmap(width_, height_, stride, BitCountToPixelFormat(bit_count_), handle.AddrOfPinnedObject());

            handle.Free();
            if (is_flip_)
            {
                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            }
            return(bitmap);
        }
Ejemplo n.º 11
0
 public void Close()
 {
     if (pgf_ != IntPtr.Zero)
     {
         int hr = VfwApi.AVIStreamGetFrameClose(pgf_);
         if (hr != 0)
         {
             throw new VfwException("AVIStreamGetFrameClose", hr);
         }
         pgf_ = IntPtr.Zero;
     }
     if (pavi_ != IntPtr.Zero)
     {
         VfwApi.AVIStreamRelease(pavi_);
         pavi_ = IntPtr.Zero;
     }
     if (pfile_ != IntPtr.Zero)
     {
         VfwApi.AVIFileRelease(pfile_);
         pfile_ = IntPtr.Zero;
     }
     DeInitialize();
 }
Ejemplo n.º 12
0
        public void Open(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException();
            }
            Initialize();
            int hr = VfwApi.AVIFileOpen(ref pfile_, filename, VfwApi.OF_READ | VfwApi.OF_SHARE_DENY_NONE, IntPtr.Zero);

            if (hr != 0)
            {
                throw new VfwException("AVIFileOpen", hr);
            }
            hr = VfwApi.AVIFileGetStream(pfile_, ref pavi_, 0, 0);
            if (hr != 0)
            {
                throw new VfwException("AVIFileGetStream", hr);
            }
            frame_start_  = VfwApi.AVIStreamStart(pavi_);
            frame_length_ = VfwApi.AVIStreamLength(pavi_);

            VfwApi.AVISTREAMINFO psi = new VfwApi.AVISTREAMINFO();
            hr = VfwApi.AVIStreamInfo(pavi_, ref psi, Marshal.SizeOf(new VfwApi.AVISTREAMINFO()));
            if (hr != 0)
            {
                throw new VfwException("AVIStreamInfo", hr);
            }
            rate_  = psi.dwRate;
            scale_ = psi.dwScale;

            pgf_ = VfwApi.AVIStreamGetFrameOpen(pavi_, IntPtr.Zero);
            if (pgf_ == IntPtr.Zero)
            {
                throw new VfwException("AVIStreamGetFrameOpen", 0);
            }
            GetBitmapHeader();
        }
Ejemplo n.º 13
0
        // 引数に与えた bitmap は上下反転される
        public void AddFrame(Bitmap bitmap)
        {
            if (is_first)
            {
                int bit_count = PixelFormatToBitCount(bitmap.PixelFormat);
                width_        = bitmap.Width;
                height_       = bitmap.Height;
                pixel_format_ = bitmap.PixelFormat;
                stride_       = (width_ * bit_count / 8 + 3) & ~3;
                CreateVideoStream(width_, height_, stride_, rate_, scale_);
                SetVideoFormat(width_, height_, stride_, bit_count);
                is_first = false;
            }
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            BitmapData bmp_data = bitmap.LockBits(new Rectangle(0, 0, width_, height_), ImageLockMode.ReadOnly, pixel_format_);
            int        hr       = VfwApi.AVIStreamWrite(pavi_, current_frame_, 1, bmp_data.Scan0, stride_ * height_, 16, IntPtr.Zero, IntPtr.Zero);

            if (hr != 0)
            {
                throw new VfwException("AVIStreamWrite", hr);
            }
            bitmap.UnlockBits(bmp_data);
            ++current_frame_;
        }