|
Imports Silverlight5_SampleVB1.cAVIDefs
Imports System.Runtime.InteropServices
Public Class cAVI
#Region "Local variables"
' <summary>
' Pointer to the AVI File in memory
' </summary>
Private pAVIFile As Int32
' <summary>
' Pointer to the AVI Stream in memory
' </summary>
Private pAVIStream As Int32
' <summary>
' Structure representing the Bitmap Information
' </summary>
Private bih As BITMAPINFOHEADER
' <summary>
' Pointer to Video Frames Object in Memory
' </summary>
Private pGetFrameObj As Int32
' <summary>
' Pointer to a DIB
' </summary>
Private pDIB As Int32
' <summary>
' Number of Scanlines in a Video Frame
' </summary>
Private numScans As Int16
' <summary>
' Byte Array for a single Sample (RGB format, hence * 3 bytes for each pixel)
' </summary>
Private RGB_Sample As Byte() '= New Byte(_frameHeight * _frameWidth * 3)
' <summary>
' Number of bytes of each pixel (4 bytes - RGBA)
' </summary>
Private Const _framePixelSize As Integer = 4
' <summary>
' Size in bytes of the stream (same as the frame size in bytes)
' </summary>
Private _frameStreamSize As Integer '= _count' <summary>
' <summary>
' Size in bytes for each Sample of type RGBA (4 bytes per pixel)
' </summary>
Private _count As Integer '= _frameHeight * _frameWidth * _framePixelSize
Private Const streamtypeVIDEO As Int32 = 1935960438
Private Const BI_RGB As Int32 = 0
Private streamInfo As AVISTREAM_INFO = New AVISTREAM_INFO()
Private firstFrame As Int32
Private numFrames As Integer
Private _speed As Long
Private _frameWidth As Int32
Private _frameHeight As Int32
Private timeexpended As Int32, frametime As Int32, initialtime As Int32
Private timelapse As Integer, initcapture As Integer
Private aviOpen As Boolean = False
Private j As Int32 = 0
Private pixelPos As Int32
Private Const RGBByteCount As Int16 = 3 ' represents RGB total bytes per pixel
Private Const RGBAByteCount As Int16 = 4 ' represents RGBA total bytes per pixel
#End Region
' Opens and prepares an AVI video
' Takes in a a string representing the full path to the AVI Video
Public Function openVideo(filename As String) As Boolean
' Return a Pointer of the File in Memory assigned to pAVIFile
Dim res As Int32 = AVIFileOpen(pAVIFile, filename, 32, 0)
' Return a Pointer of the Video Stream in Memory assigned to pAVIStream
res = AVIFileGetStream(pAVIFile, pAVIStream, streamtypeVIDEO, 0)
' Return the First Video Frame Number
firstFrame = AVIStreamStart(pAVIStream)
' Return the Total Number of Frames in the Stream
numFrames = AVIStreamLength(pAVIStream)
' Return the Video Stream Information
res = AVIStreamInfo(pAVIStream, streamInfo, Marshal.SizeOf(streamInfo))
_speed = streamInfo.dwRate 'fps
With bih
.biBitCount = 24
.biClrImportant = 0
.biClrUsed = 0
.biCompression = BI_RGB
.biHeight = streamInfo.rcFrame.bottom - streamInfo.rcFrame.top
.biPlanes = 1
.biSize = 40
.biWidth = streamInfo.rcFrame.right - streamInfo.rcFrame.left
.biXPelsPerMeter = 0
.biYPelsPerMeter = 0
.biSizeImage = (((.biWidth * 3) + 3) And &HFFFC) * .biHeight
End With
_frameWidth = bih.biWidth
_frameHeight = bih.biHeight
_count = _frameHeight * _frameWidth * _framePixelSize
_frameStreamSize = _count
numScans = IIf(_frameHeight > _frameWidth, _frameHeight, _frameWidth)
' Retrieve the Frames Object from the stream byte by supplying the Stream and the format we expect
pGetFrameObj = AVIStreamGetFrameOpen(pAVIStream, bih)
' Resize our actual Samples in Bytes
ReDim RGB_Sample(bih.biSizeImage - 1)
frametime = 0
timelapse = 30
initcapture = 1
aviOpen = True
Return True
End Function
' Prepares and extracts the Bytes for a specific Video Frame (without the Info Header)
' outRGBSample if passed in by reference, to be modified and assigned a Sample in Bytes
Public Sub returnFrameBytes(ByRef outRGBASample() As Byte)
If initcapture = 0 Then
timeexpended = timeGetTime() - initialtime
Else
frametime = 0
timeexpended = 0
initialtime = timeGetTime()
End If
If initcapture = 0 Then
If timelapse > 1000 Then
frametime = frametime + 1
Else
frametime = (timeexpended / 1000) * (1000 / _speed) 'timelapse)
If frametime >= numFrames Then
frametime = 0
timelapse = 30
initcapture = 1
End If
End If
Else
initcapture = 0
End If
Try
' Get the requested Frame from the Stream - frame number "frametime" provided.
' Return a Pointer to the DIB
pDIB = AVIStreamGetFrame(pGetFrameObj, frametime)
Catch ex As Exception
End Try
If pDIB <> 0 Then
Marshal.Copy(pDIB + Marshal.SizeOf(bih), RGB_Sample, 0, bih.biSizeImage)
If Not RGB_Sample Is Nothing Then
For verticalCount As Integer = RGB_Sample.Length - 1 To 0 Step _frameWidth * RGBByteCount * -1
For horizontalCount As Integer = 0 To _frameWidth - 1
' Calculate the next pixel position from the original Sample
' based on the outer loop, it is calculated from bottom-right
pixelPos = verticalCount - (_frameWidth * RGBByteCount) + (horizontalCount * RGBByteCount) + 1
'MessageBox.Show(RGB_Sample(pixelPos))
'System.Diagnostics.Debug.WriteLine(RGB_Sample(pixelPos))
'System.Diagnostics.Debug.WriteLine(RGBA_Sample(pixelPos))
outRGBASample(j) = RGB_Sample(pixelPos)
outRGBASample(j + 1) = RGB_Sample(pixelPos + 1)
outRGBASample(j + 2) = RGB_Sample(pixelPos + 2)
' Assign 1 byte for the Alpha Channel
outRGBASample(j + 3) = 255 '&FF
'jump 4 bytes for the RGBA byte counter
j += RGBAByteCount
Next
Next
End If
j = 0
End If
End Sub
Public Sub closeStream()
' Do some AVI Clean up here
If pGetFrameObj <> 0 Then
Call AVIStreamGetFrameClose(pGetFrameObj) '
End If
If pAVIStream <> 0 Then
Call AVIStreamRelease(pAVIStream) ' Close video stream
End If
If pAVIFile <> 0 Then
Call AVIFileRelease(pAVIFile) ' Close the AVI file
End If
AVIFileExit()
aviOpen = False
End Sub
#Region "Properties"
Public Property playSpeed() As Int32
Get
Return _speed
End Get
Set(value As Int32)
_speed = value
End Set
End Property
Public ReadOnly Property startFrame() As Int32
Get
Return firstFrame
End Get
End Property
Public ReadOnly Property totalFrames() As Int32
Get
Return numFrames
End Get
End Property
Public ReadOnly Property frameWidth() As Int32
Get
Return _frameWidth
End Get
End Property
Public ReadOnly Property frameHeight() As Int32
Get
Return _frameHeight
End Get
End Property
Public ReadOnly Property frameSize() As Integer
Get
Return _frameStreamSize
End Get
End Property
Public ReadOnly Property IsAVIOpen() As Boolean
Get
Return aviOpen
End Get
End Property
#End Region
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been a Developer for many years. I have worked as Senior Developer in Kenya, Canada and Saudi Arabia. I enjoy coding and I am looking forward to more challenges with new Technologies
I am currently IT Manager at Norconsult Telematics, Saudi Arabia.