Using Frames from the Multimedia Toolkit with the Rest of LEADTOOLS OCX (Visual Basic)

The following is an overview of the process:

1.

ltmmConvertCtrl and ltmmCaptureCtrl controls can pass you each sample using the ltmmMediaSample Interface. For simplicity, we will be using the ltmmConvertCtrl control from now on, but similar steps would be used with the ltmmCaptureCtrl control.

2.

You should create an ltmmMediaType object and set its:

 

a.

Type to be video (using the ltmmMediaType.Type property)

 

b.

Subtype to uncompressed RGB24 or 8-bit (using the ltmmMediaType.Subtype property) because this is the only type of data LEADTOOLS can use

3.

Create an ltmmSampleTarget object.

4.

Call ltmmSampleTarget.SetAcceptedMediaTypeto set the output sample type. You can also free the ltmmMediaType object since it is not needed anymore.

5.

Specify that the output from ltmmConvertCtrl control will be the target sample grabber by setting ltmmConvertCtrl.TargetObject to the newly created ltmmSampleTarget object.

6.

Make the convert control create its graph to know the size of the output samples. You can set ltmmConvertCtrl.SourceFile to tell it to start converting from a multimedia file. Call ltmmConvertCtrl::StartConvert to force the filter to build the filter graph and update the output type.

7.

Get the ltmmMediaType object which will be used by the sample grabber control by calling ltmmSampleTarget.GetConnectedMediaType. Get the dimensions of the samples using ltmmMediaType.Format. For video samples, the format is described by the VIDEOINFOHEADER structure, which you will find in the sample.

8.

Use the width, height and bits per pixel from the media type and create a user bitmap by calling CreateUserBitmap. You can pass NULL for vData if you don’t have the data yet.

9.

For each sample:

 

a.

Retrieve a sample using ltmmSampleTarget.GetSample.

 

b.

Get direct access to the sample data using ltmmMediaSample.Buffer.

 

c.

Call SetBitmapDataPointer to set the ActiveX bitmap data pointer to the sample data

 

d.

Use the LEAD bitmap. (This is what we have been working for!)

 

e.

Release the sample by assigning Nothing to ltmmSampleTarget.

 

Note: The ActiveX bitmap should not be used after releasing the sample. You should call SetBitmapDataPointer before this, because the LEADTOOLS control might attempt to access the bitmap data on its own (to repaint, for example)!

10.

Stop the conversion by calling ltmmConvertCtrl.StopConvert and free any memory (other than the bitmap data) allocated for the ActiveX bitmap using the Bitmap property.

Example:

 

' declarations
Private Type BITMAPINFOHEADER '40 bytes
        biSize As Long
        biWidth As Long
        biHeight As Long
        biPlanes As Integer
        biBitCount As Integer
        biCompression As Long
        biSizeImage As Long
        biXPelsPerMeter As Long
        biYPelsPerMeter As Long
        biClrUsed As Long
        biClrImportant As Long
End Type

Private Type RECT    ' 16 bytes
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Type REFERENCE_TIME  ' 8 bytes
    lowpart As Long
    highpart As Long
End Type

Private Type VIDEOINFOHEADER ' 88 bytes
    rcSource As RECT
    rcTarget As RECT
    dwBitRate As Long
    dwBitErrorRate As Long
    AvgTimePerFrame As REFERENCE_TIME
    bmiHeader As BITMAPINFOHEADER
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'
' Copy VideoInfoHeader
' copys an array to a video info header
' vih - destination video info header
' viharr - source array
'
Private Sub CopyVideoInfoHeader(vih As VIDEOINFOHEADER, viharr() As Byte)
    CopyMemory vih, viharr(0), 88
End Sub
'
' AviToTIFF
' Creates a multipage TIFF file from an AVI file
' pszAviFile - source file name
' pszOutputFile - the name of the output TIF file
' nMaxFrames - maximum number of pages (frames) to append to the file
'
Sub AviToTIFF(strAviFile As String, strOutputFile As String, MaxFrames As Integer)

    Dim SampleTarget As ltmmSampleTarget
    Dim MediaType As ltmmMediaType
    Dim MediaSample As ltmmMediaSample
    Dim vih As VIDEOINFOHEADER
    Dim vBuffer As Variant
    
    ' create sample target
    Set SampleTarget = New ltmmSampleTarget
    
    ' create the media type object
    Set MediaType = New ltmmMediaType
    
    
    ' set type to MEDIATYPE_Video
    MediaType.Type = "{73646976-0000-0010-8000-00AA00389B71}"
    
    ' set subtype to MEDIASUBTYPE_RGB24
    MediaType.Subtype = "{e436eb7d-524f-11ce-9f53-0020af0ba770}"
    
    '  set the accepted media type
    SampleTarget.SetAcceptedMediaType MediaType
    
    
    ' get it back for debugging
    Set MediaType = SampleTarget.GetAcceptedMediaType()
    
    
    '  set the convert input file name
    ltmmConvertCtrl1.SourceFile = strAviFile
    
    '  set the convert object target
    ltmmConvertCtrl1.TargetObject = SampleTarget
    
    ' start the conversion
    ltmmConvertCtrl1.StartConvert

    ' get the connected media type
    Set MediaType = SampleTarget.GetConnectedMediaType()

    ' get the VIDEOINFOHEADER
    CopyVideoInfoHeader vih, MediaType.Format

    ' create the user bitmap in the LEAD control
    LEAD1.ScaleMode = 3  ' use pixels

    LEAD1.CreateUserBitmap vih.bmiHeader.biWidth, vih.bmiHeader.biHeight, vih.bmiHeader.biBitCount, Empty, 0

    ' todo: copy the palette, if necessary

    ' disable the repaint to get a faster convert
    ' you might want to do this if you are trying to do a save while capturing
    LEAD1.AutoRepaint = False

    For n = 0 To (MaxFrames - 1)
        ' fetch a sample
        On Error GoTo Done
        Set MediaSample = SampleTarget.GetSample(1000)

        ' get the sample data
        vBuffer = MediaSample.Buffer

        ' set the control pointer without doing a copy
        LEAD1.SetBitmapDataPointer vBuffer, vih.bmiHeader.biSizeImage

        LEAD1.Save strOutputFile, FILE_LEAD1JTIF, 0, 25, SAVE_APPEND

        ' clear the sample data
        vBuffer = Empty
        Set MediaSample = Nothing

        ' the control data pointer is not valid anymore
        LEAD1.SetBitmapDataPointer Empty, 0
    Next
Done:
    ' clear the data pointer
    LEAD1.SetBitmapDataPointer Empty, 0

    ' stop
    ltmmConvertCtrl1.StopConvert
End Sub

Private Sub Form_Load()
    AviToTIFF "c:\count.avi", "e:\i\count.tif", 10
End Sub