SHARE
TWEET

Untitled

a guest Jan 26th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. file := "test.mp4"
  2. video_bitrate := 2000000
  3. video_fps := 25
  4. duration := 5
  5. capture_cursor := true
  6. audiodevice := "CABLE Output (VB-Audio Virtual Cable)"
  7.  
  8. ; SourceReader_MF_LOW_LATENCY := "turnOff"
  9. ; SinkWriter_MF_LOW_LATENCY := "turnOff"
  10. ; SinkWriter_MF_SINK_WRITER_DISABLE_THROTTLING := "turnOff"
  11.  
  12. setbatchlines -1
  13. Global pSinkWriter, SourceReader, audioStreamIndex, Flush, IMFSourceReaderCallback, IMFTransform, pTransform, MFT_OUTPUT_DATA_BUFFER, cbOutBytes
  14. d3d := Direct3DCreate9(D3D_SDK_VERSION := 32)
  15. if !d3d
  16. {
  17.     MsgBox, 16, Error, Direct3DCreate9 failed.
  18.     ExitApp
  19. }
  20. VarSetCapacity(D3DDISPLAYMODE, 16, 0)
  21. IDirect3D9_GetAdapterDisplayMode(d3d, D3DADAPTER_DEFAULT := 0, &D3DDISPLAYMODE)
  22. Windowed := true
  23. BackBufferCount := 1
  24. height := NumGet(D3DDISPLAYMODE, 4, "uint")
  25. width := NumGet(D3DDISPLAYMODE, 0, "uint")
  26. SwapEffect := 1   ; D3DSWAPEFFECT_DISCARD
  27. hDeviceWindow := 0
  28. VarSetCapacity(D3DPRESENT_PARAMETERS, 48+2*A_PtrSize, 0)
  29. NumPut(width, D3DPRESENT_PARAMETERS, 0)
  30. NumPut(height, D3DPRESENT_PARAMETERS, 4)
  31. NumPut(BackBufferCount, D3DPRESENT_PARAMETERS, 12)
  32. NumPut(SwapEffect, D3DPRESENT_PARAMETERS, 24)
  33. NumPut(hDeviceWindow, D3DPRESENT_PARAMETERS, 24+A_PtrSize)
  34. NumPut(Windowed, D3DPRESENT_PARAMETERS, 24+2*A_PtrSize)
  35. IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT := 0, D3DDEVTYPE_HAL := 1, 0, D3DCREATE_SOFTWARE_VERTEXPROCESSING := 0x00000020, &D3DPRESENT_PARAMETERS, device)
  36. IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, D3DFMT_A8R8G8B8 := 21, D3DPOOL_SYSTEMMEM := 2, surface, 0)
  37. Release(d3d)
  38. d3d := ""
  39.  
  40. LOAD_DLL_Mf_Mfplat_Mfreadwrite()
  41. MFStartup(version := 2, MFSTARTUP_FULL := 0)
  42. if (ShowAllAudioDevicesNames = "")
  43. {
  44.    VarSetCapacity(MFT_REGISTER_TYPE_INFO, 32, 0)
  45.    DllCall("RtlMoveMemory", "ptr", &MFT_REGISTER_TYPE_INFO, "ptr", MF_GUID(GUID, "MFMediaType_Video"), "ptr", 16)
  46.    DllCall("RtlMoveMemory", "ptr", &MFT_REGISTER_TYPE_INFO + 16, "ptr", MF_GUID(GUID, "MFVideoFormat_H264"), "ptr", 16)
  47.    hardware_encoder := MFTEnumEx(MF_GUID(GUID, "MFT_CATEGORY_VIDEO_ENCODER"), MFT_ENUM_FLAG_HARDWARE := 0x04|MFT_ENUM_FLAG_SORTANDFILTER := 0x40, 0, &MFT_REGISTER_TYPE_INFO)
  48.    msgbox % "hardware-encoder - " ((hardware_encoder != "") ? hardware_encoder : "none")
  49.    if (x1 != "")
  50.    {
  51.       checkCoordinates(x1, x2, y1, y2, hardware_encoder)
  52.       width := x2-x1
  53.       height := y2-y1
  54.       VarSetCapacity(RECT, 16, 0)
  55.       NumPut(x1, RECT, 0, "int")
  56.       NumPut(y1, RECT, 4, "int")
  57.       NumPut(x2, RECT, 8, "int")
  58.       NumPut(y2, RECT, 12, "int")
  59.    }
  60.    if (hardware_encoder != "")
  61.    {
  62.       MFCreateAttributes(pMFAttributes, 4)
  63.       IMFAttributes_SetUINT32(pMFAttributes, MF_GUID(GUID, "MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS"), true)
  64.    }
  65.    else
  66.       MFCreateAttributes(pMFAttributes, 3)
  67.    IMFAttributes_SetGUID(pMFAttributes, MF_GUID(GUID, "MF_TRANSCODE_CONTAINERTYPE"), MF_GUID(GUID1, "MFTranscodeContainerType_MPEG4"))
  68.    if (SinkWriter_MF_SINK_WRITER_DISABLE_THROTTLING != "turnOff")
  69.       IMFAttributes_SetUINT32(pMFAttributes, MF_GUID(GUID, "MF_SINK_WRITER_DISABLE_THROTTLING"), true)
  70.    if (SinkWriter_MF_LOW_LATENCY != "turnOff")
  71.       IMFAttributes_SetUINT32(pMFAttributes, MF_GUID(GUID, "MF_LOW_LATENCY"), true)
  72.    MFCreateSinkWriterFromURL(file, 0, pMFAttributes, pSinkWriter)
  73.    Release(pMFAttributes)
  74.    pMFAttributes := ""
  75.  
  76.    loop 2   ; 1 - input, 2 - output
  77.    {
  78.       MFCreateMediaType(pMediaType%A_Index%)
  79.       IMFAttributes_SetGUID(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_MAJOR_TYPE"), MF_GUID(GUID1, "MFMediaType_Video"))
  80.       if (A_Index = 1)
  81.       {
  82.          if (A_OSVersion = "WIN_7") or !InStr(hardware_encoder, "NVIDIA") or ((x1 != "") and (CaptureCoordinatesWithCPU = true))
  83.             IMFAttributes_SetGUID(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, "MFVideoFormat_RGB32"))
  84.          else
  85.             IMFAttributes_SetGUID(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, "MFVideoFormat_ARGB32"))
  86.       }
  87.       else
  88.       {
  89.          IMFAttributes_SetGUID(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, "MFVideoFormat_H264"))
  90.          IMFAttributes_SetUINT32(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_AVG_BITRATE"), video_bitrate)
  91.       }
  92.       IMFAttributes_SetUINT32(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_INTERLACE_MODE"), MFVideoInterlace_Progressive := 2)
  93.       IMFAttributes_SetUINT64(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_FRAME_SIZE"), (width<<32)|height)
  94.       IMFAttributes_SetUINT64(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_FRAME_RATE"), (video_fps<<32)|1)
  95.       IMFAttributes_SetUINT64(pMediaType%A_Index%, MF_GUID(GUID, "MF_MT_PIXEL_ASPECT_RATIO"), (1<<32)|1)
  96.    }
  97.    IMFSinkWriter_AddStream(pSinkWriter, pMediaType2, videoStreamIndex)
  98.    IMFSinkWriter_SetInputMediaType(pSinkWriter, videoStreamIndex, pMediaType1, 0)
  99.    Release(pMediaType1)
  100.    Release(pMediaType2)
  101.    pMediaType1 := pMediaType2 := ""
  102. }
  103.  
  104. if (audiodevice != "") or (ShowAllAudioDevicesNames != "")
  105. {
  106.    NUM_CHANNELSMax := BITS_PER_SAMPLEMax := SAMPLES_PER_SECONDMax := BYTES_PER_SECONDMax := StreamNumberAudio := TypeNumberAudio := AudioSources := ""
  107.    IMFSourceReaderCallback := IMFSourceReaderCallback_new()
  108.    MFCreateAttributes(pMFAttributes, 1)
  109.    IMFAttributes_SetGUID(pMFAttributes, MF_GUID(GUID, "MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE"), MF_GUID(GUID1, "MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID"))
  110.    MFEnumDeviceSources(pMFAttributes, pppSourceActivate, pcSourceActivate)
  111.    Loop % pcSourceActivate
  112.    {
  113.       IMFActivate := NumGet(pppSourceActivate + (A_Index - 1)*A_PtrSize)
  114.       devicename := IMFActivate_GetAllocatedString(IMFActivate, MF_GUID(GUID, "MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME"))
  115.       if ShowAllAudioDevicesNames
  116.          Audiodevicenames .= devicename "`n"
  117.       else if (devicename = Audiodevice)
  118.          IMFActivate_ActivateObject(IMFActivate, IMFMediaSource := "{279a808d-aec7-40c8-9c6b-a6b492c78a66}", MediaSourceAudio)
  119.       Release(IMFActivate)
  120.       IMFActivate := ""
  121.    }
  122.    DllCall("ole32\CoTaskMemFree", "ptr", pppSourceActivate)
  123.    Release(pMFAttributes)
  124.    pMFAttributes := ""
  125.    if ShowAllAudioDevicesNames
  126.    {
  127.       if (Audiodevicenames = "")
  128.          Audiodevicenames .= "None`n"
  129.       msgbox % clipboard := "Audio:`n" Audiodevicenames
  130.       ExitApp
  131.    }
  132.    if (MediaSourceAudio = "")
  133.    {
  134.       msgbox % "Cannot find Audio device - """ Audiodevice """"
  135.       ExitApp
  136.    }
  137.    if (hardware_encoder != "")
  138.    {
  139.       MFCreateAttributes(pMFAttributes, 3)
  140.       IMFAttributes_SetUINT32(pMFAttributes, MF_GUID(GUID, "MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS"), true)
  141.    }
  142.    else
  143.       MFCreateAttributes(pMFAttributes, 2)
  144.    if (SourceReader_MF_LOW_LATENCY != "turnOff")
  145.       IMFAttributes_SetUINT32(pMFAttributes, MF_GUID(GUID, "MF_LOW_LATENCY"), true)
  146.    IMFAttributes_SetUnknown(pMFAttributes, MF_GUID(GUID, "MF_SOURCE_READER_ASYNC_CALLBACK"), IMFSourceReaderCallback)
  147.    MFCreateSourceReaderFromMediaSource(MediaSourceAudio, pMFAttributes, SourceReader)
  148.    loop
  149.    {
  150.       n := A_Index - 1
  151.       if (IMFSourceReader_GetNativeMediaType(SourceReader, n, 0, ppMediaType) = "MF_E_INVALIDSTREAMNUMBER")
  152.          break
  153.       Release(ppMediaType)
  154.       ppMediaType := ""
  155.       loop
  156.       {
  157.          k := A_Index - 1
  158.          if (IMFSourceReader_GetNativeMediaType(SourceReader, n, k, ppMediaType) = "MF_E_NO_MORE_TYPES")
  159.             break
  160.          IMFAttributes_GetGUID(ppMediaType, MF_GUID(GUID, "MF_MT_MAJOR_TYPE"), pguidValue)
  161.          if (MemoryDifference(&pguidValue, MF_GUID(GUID, "MFMediaType_Audio"), 16) = 0)
  162.          {
  163.             IMFAttributes_GetGUID(ppMediaType, MF_GUID(GUID, "MF_MT_SUBTYPE"), pguidValueSubType)
  164.             AudioSources .= Format("0x{:x}", NumGet(pguidValueSubType, 0, "int")) "`n"
  165.             if (MemoryDifference(&pguidValueSubType, MF_GUID(GUID, "MFAudioFormat_PCM"), 16) = 0) or (MemoryDifference(&pguidValueSubType, MF_GUID(GUID, "MFAudioFormat_Float"), 16) = 0)
  166.             {
  167.                if (IMFAttributes_GetUINT32(ppMediaType, MF_GUID(GUID, "MF_MT_AUDIO_AVG_BYTES_PER_SECOND"), BYTES_PER_SECOND) = "MF_E_ATTRIBUTENOTFOUND")
  168.                   BYTES_PER_SECOND := 0
  169.                if (IMFAttributes_GetUINT32(ppMediaType, MF_GUID(GUID, "MF_MT_AUDIO_BITS_PER_SAMPLE"), BITS_PER_SAMPLE) = "MF_E_ATTRIBUTENOTFOUND")
  170.                   BITS_PER_SAMPLE := 0
  171.                if (IMFAttributes_GetUINT32(ppMediaType, MF_GUID(GUID, "MF_MT_AUDIO_SAMPLES_PER_SECOND"), SAMPLES_PER_SECOND) = "MF_E_ATTRIBUTENOTFOUND")
  172.                   SAMPLES_PER_SECOND := 0
  173.                if (IMFAttributes_GetUINT32(ppMediaType, MF_GUID(GUID, "MF_MT_AUDIO_NUM_CHANNELS"), NUM_CHANNELS) = "MF_E_ATTRIBUTENOTFOUND")
  174.                   NUM_CHANNELS := 0
  175.                if (InStr(A_OSVersion, "W") and (((NUM_CHANNELSMax != 1) and (NUM_CHANNELSMax != 2) and (NUM_CHANNELS > 0)) or ((NUM_CHANNELSMax = 1) and (NUM_CHANNELS = 2)))) or (!InStr(A_OSVersion, "W") and (((NUM_CHANNELSMax < 2) and (NUM_CHANNELS > NUM_CHANNELSMax)) or ((NUM_CHANNELSMax = 2) and (NUM_CHANNELS = 6)) or ((NUM_CHANNELSMax > 2) and (NUM_CHANNELSMax != 6) and ((NUM_CHANNELS = 2) or (NUM_CHANNELS = 6))))) or ((NUM_CHANNELS = NUM_CHANNELSMax) and (BITS_PER_SAMPLEMax != 16) and ((BITS_PER_SAMPLE = 16) or (BITS_PER_SAMPLE > BITS_PER_SAMPLEMax))) or ((NUM_CHANNELS = NUM_CHANNELSMax) and (BITS_PER_SAMPLE = BITS_PER_SAMPLEMax) and (SAMPLES_PER_SECONDMax != 44100) and (SAMPLES_PER_SECONDMax != 48000) and ((SAMPLES_PER_SECOND = 44100) or (SAMPLES_PER_SECOND > SAMPLES_PER_SECONDMax))) or ((SAMPLES_PER_SECONDMax != 48000) and (SAMPLES_PER_SECOND = 48000)) or ((NUM_CHANNELS = NUM_CHANNELSMax) and (BITS_PER_SAMPLE = BITS_PER_SAMPLEMax) and (SAMPLES_PER_SECOND = SAMPLES_PER_SECONDMax) and (BYTES_PER_SECOND > BYTES_PER_SECONDMax))
  176.                {
  177.                   if (MemoryDifference(&pguidValueSubType, MF_GUID(GUID, "MFAudioFormat_PCM"), 16) = 0)
  178.                      MFAudioFormat := "MFAudioFormat_PCM"
  179.                   else
  180.                      MFAudioFormat := "MFAudioFormat_Float"
  181.                   NUM_CHANNELSMax := NUM_CHANNELS
  182.                   BITS_PER_SAMPLEMax := BITS_PER_SAMPLE
  183.                   SAMPLES_PER_SECONDMax := SAMPLES_PER_SECOND
  184.                   BYTES_PER_SECONDMax := BYTES_PER_SECOND
  185.                   StreamNumberAudio := n
  186.                   TypeNumberAudio := k
  187.                }
  188.             }
  189.          }
  190.          Release(ppMediaType)
  191.          ppMediaType := ""
  192.       }
  193.    }
  194.    if (StreamNumberAudio = "")
  195.    {
  196.       Sort, AudioSources, U
  197.       msgbox % "Current AudioSources not supported:`n" AudioSources
  198.       ExitApp
  199.    }
  200.    IMFSourceReader_SetStreamSelection(SourceReader, MF_SOURCE_READER_ALL_STREAMS := 0xFFFFFFFE, false)
  201.    IMFSourceReader_SetStreamSelection(SourceReader, StreamNumberAudio, true)
  202.    Release(pMFAttributes)
  203.    pMFAttributes := ""
  204.    if (NUM_CHANNELSMax = 0)
  205.       NUM_CHANNELS := 1
  206.    else if (NUM_CHANNELSMax > 2) and (NUM_CHANNELSMax < 6)
  207.       NUM_CHANNELS := 2
  208.    else if (NUM_CHANNELSMax >= 6)
  209.    {
  210.       if !InStr(A_OSVersion, "W")
  211.          NUM_CHANNELS := 6
  212.       else
  213.          NUM_CHANNELS := 2
  214.    }
  215.    if (SAMPLES_PER_SECONDMax < 44100)
  216.       SAMPLES_PER_SECOND := 44100
  217.    else if (SAMPLES_PER_SECONDMax > 44100)
  218.       SAMPLES_PER_SECOND := 48000
  219.    loop 2   ; 1 - input, 2 - output
  220.    {
  221.       MFCreateMediaType(pMediaTypeAudio%A_Index%)
  222.       IMFAttributes_SetGUID(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_MAJOR_TYPE"), MF_GUID(GUID1, "MFMediaType_Audio"))
  223.       if (A_Index = 1)
  224.          IMFAttributes_SetGUID(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, "MFAudioFormat_PCM"))
  225.       else
  226.       {
  227.          IMFAttributes_SetGUID(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, "MFAudioFormat_AAC"))
  228.          IMFAttributes_SetUINT32(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_AVG_BYTES_PER_SECOND"), 20000)
  229.       }
  230.       IMFAttributes_SetUINT32(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_BITS_PER_SAMPLE"), 16)
  231.       IMFAttributes_SetUINT32(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_SAMPLES_PER_SECOND"), SAMPLES_PER_SECOND)
  232.       IMFAttributes_SetUINT32(pMediaTypeAudio%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_NUM_CHANNELS"), NUM_CHANNELS)
  233.    }
  234.    IMFSinkWriter_AddStream(pSinkWriter, pMediaTypeAudio2, audioStreamIndex)
  235.    IMFSinkWriter_SetInputMediaType(pSinkWriter, audioStreamIndex, pMediaTypeAudio1, 0)
  236.    if (IMFSourceReader_SetCurrentMediaType(SourceReader, StreamNumberAudio, 0, pMediaTypeAudio1) = "MF_E_TOPO_CODEC_NOT_FOUND")
  237.    {
  238.       LOAD_DLL_Resampledmo_Mfaacenc()
  239.       IMFTransform := 1
  240.       cbOutBytes := 100000
  241.       loop 2   ; 1 - input, 2 - output
  242.       {
  243.          MFCreateMediaType(pMedia%A_Index%)
  244.          IMFAttributes_SetGUID(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_MAJOR_TYPE"), MF_GUID(GUID1, "MFMediaType_Audio"))
  245.          IMFAttributes_SetGUID(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_SUBTYPE"), MF_GUID(GUID1, MFAudioFormat))
  246.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_BITS_PER_SAMPLE"), BITS_PER_SAMPLEMax)
  247.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_NUM_CHANNELS"), NUM_CHANNELSMax)
  248.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_SAMPLES_PER_SECOND"), SAMPLES_PER_SECONDMax)
  249.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_BLOCK_ALIGNMENT"), NUM_CHANNELSMax*BITS_PER_SAMPLEMax//8)
  250.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_AUDIO_AVG_BYTES_PER_SECOND"), SAMPLES_PER_SECONDMax*NUM_CHANNELSMax*BITS_PER_SAMPLEMax//8)
  251.          IMFAttributes_SetUINT32(pMedia%A_Index%, MF_GUID(GUID, "MF_MT_ALL_SAMPLES_INDEPENDENT"), true)
  252.          if (A_Index = 1)
  253.             MFAudioFormat := "MFAudioFormat_PCM", BITS_PER_SAMPLEMax := 16, NUM_CHANNELSMax := NUM_CHANNELS, SAMPLES_PER_SECONDMax := SAMPLES_PER_SECOND
  254.       }
  255.       spTransformUnk := ComObjCreate(CLSID_CResamplerMediaObject := "{f447b69e-1884-4a7e-8055-346f74d6edb3}", IID_IUnknown := "{00000000-0000-0000-C000-000000000046}")
  256.       pTransform := ComObjQuery(spTransformUnk, IID_IMFTransform := "{bf94c121-5b05-4e6f-8000-ba598961414d}")
  257.       spResamplerProps := ComObjQuery(spTransformUnk, IID_IWMResamplerProps := "{E7E9984F-F09F-4da4-903F-6E2E0EFE56B5}")
  258.       IWMResamplerProps_SetHalfFilterLength(spResamplerProps, 60)
  259.       IMFTransform_SetInputType(pTransform, 0, pMedia1, 0)
  260.       IMFTransform_SetOutputType(pTransform, 0, pMedia2, 0)
  261.       IMFTransform_GetInputStatus(pTransform, 0, mftStatus)
  262.       if (mftStatus != 1)   ; MFT_INPUT_STATUS_ACCEPT_DATA
  263.       {
  264.          msgbox IMFTransform_GetInputStatus not accept data
  265.          ExitApp
  266.       }
  267.       IMFTransform_ProcessMessage(pTransform, MFT_MESSAGE_COMMAND_FLUSH := 0, 0)
  268.       IMFTransform_ProcessMessage(pTransform, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING := 0x10000000, 0)
  269.       IMFTransform_ProcessMessage(pTransform, MFT_MESSAGE_NOTIFY_START_OF_STREAM := 0x10000003, 0)
  270.       Release(pMedia1)
  271.       Release(pMedia2)
  272.       pMedia1 := pMedia2 := ""
  273.    }
  274.    Release(pMediaTypeAudio1)
  275.    Release(pMediaTypeAudio2)
  276.    pMediaTypeAudio1 := pMediaTypeAudio2 := ""
  277. }
  278. IMFSinkWriter_BeginWriting(pSinkWriter)
  279. video_frame_duration := 10000000/video_fps
  280. video_frame_count := duration*video_fps
  281. cbWidth := 4 * width
  282. cbBuffer := cbWidth * height
  283. rtStart := 0
  284. fps := 1000/video_fps
  285.  
  286. loop
  287. {
  288.    if (A_Index = 1)
  289.       timeElapsed := A_TickCount
  290.    else
  291.    {
  292.       loop
  293.       {
  294.          if (A_TickCount - timeElapsed >= fps)
  295.          {
  296.             timeElapsed+=fps
  297.             break
  298.          }
  299.          sleep 10
  300.       }
  301.    }
  302.  
  303.    IDirect3DDevice9_GetFrontBufferData(device, 0, surface)
  304.    if (capture_cursor = true)
  305.    {
  306.       VarSetCapacity(CURSORINFO, cbSize := 16 + A_PtrSize, 0)
  307.       NumPut(cbSize, CURSORINFO, 0, "uint")
  308.       if DllCall("GetCursorInfo", "ptr", &CURSORINFO) and (NumGet(CURSORINFO, 4, "uint") = 1)   ; CURSOR_SHOWING
  309.       {
  310.          hCursor := NumGet(CURSORINFO, 8)
  311.          xCursor := NumGet(CURSORINFO, 8 + A_PtrSize, "int")
  312.          yCursor := NumGet(CURSORINFO, 12 + A_PtrSize, "int")
  313.          VarSetCapacity(ICONINFO, 8 + A_PtrSize*3, 0)
  314.          DllCall("GetIconInfo", "ptr", hCursor, "ptr", &ICONINFO)
  315.          xHotspot := NumGet(ICONINFO, 4, "uint")
  316.          yHotspot := NumGet(ICONINFO, 8, "uint")
  317.          hbmMask  := NumGet(ICONINFO, 8 + A_PtrSize)
  318.          hbmColor := NumGet(ICONINFO, 8 + A_PtrSize*2)
  319.          IDirect3DSurface9_GetDC(surface, hdc)
  320.          DllCall("DrawIconEx", "ptr", hdc, "int", xCursor - xHotspot, "int", yCursor - yHotspot, "ptr", hCursor, "int", 0, "int", 0, "uint", 0, "ptr", 0, "uint", DI_NORMAL := 0x0003 | DI_DEFAULTSIZE := 0x0008)
  321.          if hbmMask
  322.             DllCall("DeleteObject", "ptr", hbmMask)
  323.          if hbmColor
  324.             DllCall("DeleteObject", "ptr", hbmColor)
  325.          hbmMask := hbmColor := ""
  326.          IDirect3DSurface9_ReleaseDC(surface, hdc)
  327.       }
  328.    }
  329.    VarSetCapacity(D3DLOCKED_RECT, A_PtrSize*2, 0)
  330.    if (x1 = "")
  331.       IDirect3DSurface9_LockRect(surface, &D3DLOCKED_RECT, 0, 0)
  332.    else
  333.       IDirect3DSurface9_LockRect(surface, &D3DLOCKED_RECT, &RECT, 0)
  334.    pitch := NumGet(D3DLOCKED_RECT, 0, "int")
  335.    pBits := NumGet(D3DLOCKED_RECT, A_PtrSize, "ptr")
  336.  
  337.    MFCreateMemoryBuffer(cbBuffer, pBuffer)
  338.    IMFMediaBuffer_Lock(pBuffer, pData, 0, 0)
  339.    if (A_OSVersion = "WIN_7") or !InStr(hardware_encoder, "NVIDIA") or ((x1 != "") and (CaptureCoordinatesWithCPU = true)) or (Rotate = true)
  340.       MFCopyImage(pData, cbWidth, pBits+(height-1)*pitch, pitch*-1, cbWidth, height)
  341.    else
  342.       MFCopyImage(pData, cbWidth, pBits, pitch, cbWidth, height)
  343.    IMFMediaBuffer_Unlock(pBuffer)
  344.    IMFMediaBuffer_SetCurrentLength(pBuffer, cbBuffer)
  345.    MFCreateSample(pSample)
  346.    IMFSample_AddBuffer(pSample, pBuffer)
  347.    IMFSample_SetSampleTime(pSample, rtStart)
  348.    IMFSample_SetSampleDuration(pSample, video_frame_duration)
  349.    IMFSinkWriter_WriteSample(pSinkWriter, streamIndex, pSample)
  350.  
  351.    IDirect3DSurface9_UnlockRect(surface)
  352.    Release(pSample)
  353.    Release(pBuffer)
  354.    pSample := pBuffer := ""
  355.    if (A_Index = 1) and (audiodevice != "")
  356.       IMFSourceReader_ReadSample(SourceReader, MF_SOURCE_READER_ANY_STREAM := 0xFFFFFFFE, 0, 0, 0, 0, 0)
  357.    if (A_Index = video_frame_count)
  358.    {
  359.       if (audiodevice != "")
  360.       {
  361.          flush := 1
  362.          loop
  363.          {
  364.             if (flush = "")
  365.                break
  366.             sleep 50
  367.          }
  368.       }
  369.       break
  370.    }
  371.    rtStart += video_frame_duration
  372. }
  373. IMFSinkWriter_Finalize(pSinkWriter)
  374. if (audiodevice != "")
  375. {
  376.    if (IMFTransform = 1)
  377.    {
  378.       Release(spResamplerProps)
  379.       Release(pTransform)
  380.       Release(spTransformUnk)
  381.       spResamplerProps := pTransform := spTransformUnk := IMFTransform := ""
  382.    }
  383.    Release(MediaSourceAudio)
  384.    MediaSourceAudio := ""
  385. }
  386. Release(pSinkWriter)
  387. Release(surface)
  388. Release(device)
  389. pSinkWriter := surface := device := ""
  390. MFShutdown()
  391. msgbox done
  392. ExitApp
  393.  
  394.  
  395.  
  396.  
  397.  
  398. Direct3DCreate9(SDKVersion) {
  399.     if !DllCall("GetModuleHandle","str","d3d9")
  400.         DllCall("LoadLibrary","str","d3d9")
  401.     return DllCall("d3d9\Direct3DCreate9", "uint", SDKVersion)
  402. }
  403.  
  404. IDirect3D9_GetAdapterDisplayMode(this,Adapter,pMode)
  405. {
  406.    hr := DllCall(NumGet(NumGet(this+0)+8*A_PtrSize),"ptr",this,"uint",Adapter,"ptr",pMode)
  407.    if hr or ErrorLevel
  408.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  409. }
  410.  
  411. IDirect3D9_CreateDevice(this,Adapter,DeviceType,hFocusWindow,BehaviorFlags,pPresentationParameters,ByRef ppReturnedDeviceInterface)
  412. {
  413.    hr := DllCall(NumGet(NumGet(this+0)+16*A_PtrSize),"ptr",this,"uint",Adapter,"uint",DeviceType,"ptr",hFocusWindow,"uint",BehaviorFlags,"ptr",pPresentationParameters,"ptr*",ppReturnedDeviceInterface)
  414.    if hr or ErrorLevel
  415.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  416. }
  417.  
  418. IDirect3DDevice9_GetFrontBufferData(this,iSwapChain,pDestSurface)
  419. {
  420.    hr := DllCall(NumGet(NumGet(this+0)+33*A_PtrSize),"ptr",this,"uint",iSwapChain,"ptr",pDestSurface)
  421.    if hr or ErrorLevel
  422.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  423. }
  424.  
  425. IDirect3DDevice9_CreateOffscreenPlainSurface(this,Width,Height,Format,Pool,ByRef ppSurface,pSharedHandle)
  426. {
  427.    hr := DllCall(NumGet(NumGet(this+0)+36*A_PtrSize),"ptr",this,"uint",Width,"uint",Height,"uint",Format,"uint",Pool,"ptr*",ppSurface,"ptr",pSharedHandle)
  428.    if hr or ErrorLevel
  429.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  430. }
  431.  
  432. IDirect3DSurface9_LockRect(this,pLockedRect,pRect,Flags)
  433. {
  434.    hr := DllCall(NumGet(NumGet(this+0)+13*A_PtrSize),"ptr",this,"ptr",pLockedRect,"ptr",pRect,"uint",Flags)
  435.    if hr or ErrorLevel
  436.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  437. }
  438.  
  439. IDirect3DSurface9_UnlockRect(this)
  440. {
  441.    hr := DllCall(NumGet(NumGet(this+0)+14*A_PtrSize),"ptr",this)
  442.    if hr or ErrorLevel
  443.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  444. }
  445.  
  446. IDirect3DSurface9_GetDC(this, ByRef phdc)
  447. {
  448.    hr := DllCall(NumGet(NumGet(this+0)+15*A_PtrSize),"ptr",this,"ptr*",phdc)
  449.    if hr or ErrorLevel
  450.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  451. }
  452.  
  453. IDirect3DSurface9_ReleaseDC(this,phdc)
  454. {
  455.    hr := DllCall(NumGet(NumGet(this+0)+16*A_PtrSize),"ptr",this,"ptr",phdc)
  456.    if hr or ErrorLevel
  457.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  458. }
  459.  
  460.  
  461.  
  462. LOAD_DLL_Mf_Mfplat_Mfreadwrite()
  463. {
  464.    if !DllCall("GetModuleHandle","str","Mf")
  465.       DllCall("LoadLibrary","Str", "Mf.dll", "ptr")
  466.    if !DllCall("GetModuleHandle","str","Mfplat")
  467.       DllCall("LoadLibrary","Str", "Mfplat.dll", "ptr")
  468.    if !DllCall("GetModuleHandle","str","Mfreadwrite")
  469.       DllCall("LoadLibrary","Str", "Mfreadwrite.dll", "ptr")
  470. }
  471.  
  472. MFStartup(version, dwFlags)
  473. {
  474.    hr := DllCall("Mfplat.dll\MFStartup", "uint", version, "uint", dwFlags)
  475.    if hr or ErrorLevel
  476.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  477. }
  478.  
  479. MFShutdown()
  480. {
  481.    hr := DllCall("Mfplat.dll\MFShutdown")
  482.    if hr or ErrorLevel
  483.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  484. }
  485.  
  486. MFTEnumEx(guidCategory, Flags, pInputType, pOutputType)
  487. {
  488.    if (A_PtrSize = 8)
  489.       hr := DllCall("Mfplat\MFTEnumEx", "ptr", guidCategory, "uint", Flags, "ptr", pInputType, "ptr", pOutputType, "ptr*", pppMFTActivate, "uint*", pnumMFTActivate)
  490.    else
  491.       hr := DllCall("Mfplat\MFTEnumEx", "uint64", NumGet(guidCategory+0, 0, "uint64"), "uint64", NumGet(guidCategory+0, 8, "uint64"), "uint", Flags, "ptr", pInputType, "ptr", pOutputType, "ptr*", pppMFTActivate, "uint*", pnumMFTActivate)
  492.    loop % pnumMFTActivate
  493.    {
  494.       IMFActivate := NumGet(pppMFTActivate + (A_Index - 1)*A_PtrSize)
  495.       if (A_Index = 1)
  496.          hardware_encoder := IMFActivate_GetAllocatedString(IMFActivate, MF_GUID(GUID, "MFT_FRIENDLY_NAME_Attribute"))
  497.       Release(IMFActivate)
  498.    }
  499.    DllCall("ole32\CoTaskMemFree", "ptr", pppMFTActivate)
  500.    return hardware_encoder
  501. }
  502.  
  503. MFCreateSinkWriterFromURL(pwszOutputURL, pByteStream, pAttributes, ByRef ppSinkWriter)
  504. {
  505.    hr := DllCall("Mfreadwrite.dll\MFCreateSinkWriterFromURL", "str", pwszOutputURL, "ptr", pByteStream, "ptr", pAttributes, "ptr*", ppSinkWriter)
  506.    if hr or ErrorLevel
  507.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  508. }
  509.  
  510. MFCreateSourceReaderFromMediaSource(pMediaSource, pAttributes, ByRef ppSourceReader)
  511. {
  512.    hr := DllCall("Mfreadwrite.dll\MFCreateSourceReaderFromMediaSource", "ptr", pMediaSource, "ptr", pAttributes, "ptr*", ppSourceReader)
  513.    if hr or ErrorLevel
  514.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  515. }
  516.  
  517. MFCreateMediaType(ByRef ppMFType)
  518. {
  519.    hr := DllCall("Mfplat.dll\MFCreateMediaType", "ptr*", ppMFType)
  520.    if hr or ErrorLevel
  521.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  522. }
  523.  
  524. MFCreateAttributes(ByRef ppMFAttributes, cInitialSize)
  525. {
  526.    hr := DllCall("Mfplat.dll\MFCreateAttributes", "ptr*", ppMFAttributes, "uint", cInitialSize)
  527.    if hr or ErrorLevel
  528.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  529. }
  530.  
  531. MFCreateSample(ByRef ppIMFSample)
  532. {
  533.    hr := DllCall("Mfplat.dll\MFCreateSample", "ptr*", ppIMFSample)
  534.    if hr or ErrorLevel
  535.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  536. }
  537.  
  538. MFCreateMemoryBuffer(cbMaxLength, ByRef ppBuffer)
  539. {
  540.    hr := DllCall("Mfplat.dll\MFCreateMemoryBuffer", "uint", cbMaxLength, "ptr*", ppBuffer)
  541.    if hr or ErrorLevel
  542.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  543. }
  544.  
  545. MFCopyImage(pDest, lDestStride, pSrc, lSrcStride, dwWidthInBytes, dwLines)
  546. {
  547.    hr := DllCall("Mfplat.dll\MFCopyImage", "ptr", pDest, "int", lDestStride, "ptr", pSrc, "int", lSrcStride, "uint", dwWidthInBytes, "uint", dwLines)
  548.    if hr or ErrorLevel
  549.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  550. }
  551.  
  552. MFEnumDeviceSources(pAttributes, ByRef pppSourceActivate, ByRef pcSourceActivate)
  553. {
  554.    hr := DllCall("Mf.dll\MFEnumDeviceSources", "ptr", pAttributes, "ptr*", pppSourceActivate, "uint*", pcSourceActivate)
  555.    if hr or ErrorLevel
  556.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  557. }
  558.  
  559. MFCreateCollection(ByRef ppIMFCollection)
  560. {
  561.    hr := DllCall("Mfplat.dll\MFCreateCollection", "ptr*", ppIMFCollection)
  562.    if hr or ErrorLevel
  563.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  564. }
  565.  
  566. MFCreateAggregateSource(pSourceCollection, ByRef ppAggSource)
  567. {
  568.    hr := DllCall("Mf.dll\MFCreateAggregateSource", "ptr", pSourceCollection, "ptr*", ppAggSource)
  569.    if hr or ErrorLevel
  570.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  571. }
  572.  
  573. IMFSourceReader_SetCurrentMediaType(this, dwStreamIndex, pdwReserved, pMediaType)
  574. {
  575.    hr := DllCall(NumGet(NumGet(this+0)+7*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "uint", pdwReserved, "ptr", pMediaType)
  576.    if hr or ErrorLevel
  577.    {
  578.       if (hr&=0xFFFFFFFF) = 0xC00D5212   ; MF_E_TOPO_CODEC_NOT_FOUND
  579.          return "MF_E_TOPO_CODEC_NOT_FOUND"
  580.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  581.    }
  582. }
  583.  
  584. IMFSourceReader_SetStreamSelection(this, dwStreamIndex, fSelected)
  585. {
  586.    hr := DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "int", fSelected)
  587.    if hr or ErrorLevel
  588.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  589. }
  590.  
  591. IMFSourceReader_GetNativeMediaType(this, dwStreamIndex, dwMediaTypeIndex, ByRef ppMediaType)
  592. {
  593.    hr := DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "uint", dwMediaTypeIndex, "ptr*", ppMediaType)
  594.    if hr or ErrorLevel
  595.    {
  596.       if (hr&=0xFFFFFFFF) = 0xC00D36B3   ; MF_E_INVALIDSTREAMNUMBER
  597.          return "MF_E_INVALIDSTREAMNUMBER"
  598.       if (hr&=0xFFFFFFFF) = 0xC00D36B9   ; MF_E_NO_MORE_TYPES
  599.          return "MF_E_NO_MORE_TYPES"
  600.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  601.    }
  602. }
  603.  
  604. IMFSourceReader_ReadSample(this, dwStreamIndex, dwControlFlags, pdwActualStreamIndex, pdwStreamFlags, pllTimestamp, ppSample)
  605. {
  606.    hr := DllCall(NumGet(NumGet(this+0)+9*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "uint", dwControlFlags, "uint", pdwActualStreamIndex, "uint", pdwStreamFlags, "int", pllTimestamp, "ptr", ppSample)
  607.    if hr or ErrorLevel
  608.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  609. }
  610.  
  611. IMFSourceReader_Flush(this, dwStreamIndex)
  612. {
  613.    hr := DllCall(NumGet(NumGet(this+0)+10*A_PtrSize), "ptr", this, "uint", dwStreamIndex)
  614.    if hr or ErrorLevel
  615.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  616. }
  617.  
  618. IMFAttributes_GetGUID(this, guidKey, ByRef pguidValue)
  619. {
  620.    VarSetCapacity(pguidValue, 16, 0)
  621.    hr := DllCall(NumGet(NumGet(this+0)+10*A_PtrSize), "ptr", this, "ptr", guidKey, "ptr", &pguidValue)
  622.    if hr or ErrorLevel
  623.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  624.    return &pguidValue
  625. }
  626.  
  627. IMFAttributes_GetUINT64(this, guidKey, ByRef punValue)
  628. {
  629.    hr := DllCall(NumGet(NumGet(this+0)+8*A_PtrSize), "ptr", this, "ptr", guidKey, "uint64*", punValue)
  630.    if hr or ErrorLevel
  631.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  632. }
  633.  
  634. IMFAttributes_GetUINT32(this, guidKey, ByRef punValue)
  635. {
  636.    hr := DllCall(NumGet(NumGet(this+0)+7*A_PtrSize), "ptr", this, "ptr", guidKey, "uint*", punValue)
  637.    if hr or ErrorLevel
  638.    {
  639.       if (hr&=0xFFFFFFFF) = 0xC00D36E6   ; MF_E_ATTRIBUTENOTFOUND
  640.          return "MF_E_ATTRIBUTENOTFOUND"
  641.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  642.    }
  643. }
  644.  
  645. IMFAttributes_SetUINT32(this, guidKey, unValue)
  646. {
  647.    hr := DllCall(NumGet(NumGet(this+0)+21*A_PtrSize), "ptr", this, "ptr", guidKey, "uint", unValue)
  648.    if hr or ErrorLevel
  649.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  650. }
  651.  
  652. IMFAttributes_SetUINT64(this, guidKey, unValue)
  653. {
  654.    hr := DllCall(NumGet(NumGet(this+0)+22*A_PtrSize), "ptr", this, "ptr", guidKey, "uint64", unValue)
  655.    if hr or ErrorLevel
  656.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  657. }
  658.  
  659. IMFAttributes_SetGUID(this, guidKey, guidValue)
  660. {
  661.    hr := DllCall(NumGet(NumGet(this+0)+24*A_PtrSize), "ptr", this, "ptr", guidKey, "ptr", guidValue)
  662.    if hr or ErrorLevel
  663.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  664. }
  665.  
  666. IMFAttributes_SetUnknown(this, guidKey, pUnknown)
  667. {
  668.    hr := DllCall(NumGet(NumGet(this+0)+27*A_PtrSize), "ptr", this, "ptr", guidKey, "ptr", pUnknown)
  669.    if hr or ErrorLevel
  670.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  671. }
  672.  
  673. IMFActivate_GetAllocatedString(this, guidKey)
  674. {
  675.    hr := DllCall(NumGet(NumGet(this+0)+13*A_PtrSize), "ptr", this, "ptr", guidKey, "ptr*", ppwszValue, "uint*", pcchLength)
  676.    if hr or ErrorLevel
  677.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  678.    AllocatedString := StrGet(ppwszValue, pcchLength, "UTF-16")
  679.    DllCall("ole32\CoTaskMemFree", "ptr", ppwszValue)
  680.    return AllocatedString
  681. }
  682.  
  683. IMFActivate_ActivateObject(this, riid, ByRef ppv)
  684. {
  685.    GUID(riid, riid)
  686.    hr := DllCall(NumGet(NumGet(this+0)+33*A_PtrSize), "ptr", this, "ptr", &riid, "ptr*", ppv)
  687.    if hr or ErrorLevel
  688.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  689. }
  690.  
  691. IMFSinkWriter_SendStreamTick(this, dwStreamIndex, llTimestamp)
  692. {
  693.    hr := DllCall(NumGet(NumGet(this+0)+7*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "int64", llTimestamp)
  694.    if hr or ErrorLevel
  695.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  696. }
  697.  
  698. IMFSinkWriter_AddStream(this, pMediaTypeOut, ByRef pdwStreamIndex)
  699. {
  700.    hr := DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "ptr", pMediaTypeOut, "ptr*", pdwStreamIndex)
  701.    if hr or ErrorLevel
  702.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  703. }
  704.  
  705. IMFSinkWriter_SetInputMediaType(this, dwStreamIndex, pInputMediaType, pEncodingParameters)
  706. {
  707.    hr := DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "ptr", pInputMediaType, "ptr", pEncodingParameters)
  708.    if hr or ErrorLevel
  709.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  710. }
  711.  
  712. IMFSinkWriter_BeginWriting(this)
  713. {
  714.    hr := DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this)
  715.    if hr or ErrorLevel
  716.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  717. }
  718.  
  719. IMFSinkWriter_WriteSample(this, dwStreamIndex, pSample)
  720. {
  721.    hr := DllCall(NumGet(NumGet(this+0)+6*A_PtrSize), "ptr", this, "uint", dwStreamIndex, "ptr", pSample)
  722.    if hr or ErrorLevel
  723.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  724. }
  725.  
  726. IMFSinkWriter_Finalize(this)
  727. {
  728.    hr := DllCall(NumGet(NumGet(this+0)+11*A_PtrSize), "ptr", this)
  729.    if hr or ErrorLevel
  730.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  731. }
  732.  
  733. IMFMediaBuffer_Lock(this, ByRef ppbBuffer, ByRef pcbMaxLength, ByRef pcbCurrentLength)
  734. {
  735.    hr := DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "ptr*", ppbBuffer, "uint*", pcbMaxLength, "uint*", pcbCurrentLength)
  736.    if hr or ErrorLevel
  737.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  738. }
  739.  
  740. IMFMediaBuffer_Unlock(this)
  741. {
  742.    hr := DllCall(NumGet(NumGet(this+0)+4*A_PtrSize), "ptr", this)
  743.    if hr or ErrorLevel
  744.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  745. }
  746.  
  747. IMFMediaBuffer_SetCurrentLength(this, cbCurrentLength)
  748. {
  749.    hr := DllCall(NumGet(NumGet(this+0)+6*A_PtrSize), "ptr", this, "uint", cbCurrentLength)
  750.    if hr or ErrorLevel
  751.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  752. }
  753.  
  754. IMFMediaSource_Shutdown(this)
  755. {
  756.    hr := DllCall(NumGet(NumGet(this+0)+12*A_PtrSize), "ptr", this)
  757.    if hr or ErrorLevel
  758.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  759. }
  760.  
  761. IMFSample_AddBuffer(this, pBuffer)
  762. {
  763.    hr := DllCall(NumGet(NumGet(this+0)+42*A_PtrSize), "ptr", this, "ptr", pBuffer)
  764.    if hr or ErrorLevel
  765.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  766. }
  767.  
  768. IMFSample_SetSampleTime(this, hnsSampleTime)
  769. {
  770.    hr := DllCall(NumGet(NumGet(this+0)+36*A_PtrSize), "ptr", this, "int64", hnsSampleTime)
  771.    if hr or ErrorLevel
  772.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  773. }
  774.  
  775. IMFSample_GetSampleDuration(this, ByRef phnsSampleDuration)
  776. {
  777.    hr := DllCall(NumGet(NumGet(this+0)+37*A_PtrSize), "ptr", this, "int64*", phnsSampleDuration)
  778.    if hr or ErrorLevel
  779.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  780. }
  781.  
  782. IMFSample_SetSampleDuration(this, hnsSampleDuration)
  783. {
  784.    hr := DllCall(NumGet(NumGet(this+0)+38*A_PtrSize), "ptr", this, "int64", hnsSampleDuration)
  785.    if hr or ErrorLevel
  786.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  787. }
  788.  
  789. IMFCollection_AddElement(this, pUnkElement)
  790. {
  791.    hr := DllCall(NumGet(NumGet(this+0)+5*A_PtrSize), "ptr", this, "ptr", pUnkElement)
  792.    if hr or ErrorLevel
  793.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  794. }
  795.  
  796. LOAD_DLL_Resampledmo_Mfaacenc()
  797. {
  798.    if !DllCall("GetModuleHandle","str","Resampledmo")
  799.       DllCall("LoadLibrary","Str", "Resampledmo.dll", "ptr")
  800.    if !DllCall("GetModuleHandle","str","Mfaacenc")
  801.       DllCall("LoadLibrary","Str", "Mfaacenc.dll", "ptr")
  802. }
  803.  
  804. IMFTransform_GetInputStatus(this, dwInputStreamID, ByRef pdwFlags)
  805. {
  806.    hr := DllCall(NumGet(NumGet(this+0)+19*A_PtrSize), "ptr", this, "uint", dwInputStreamID, "uint*", pdwFlags)
  807.    if hr or ErrorLevel
  808.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  809. }
  810.  
  811. IMFTransform_ProcessMessage(this, eMessage, ulParam)
  812. {
  813.    hr := DllCall(NumGet(NumGet(this+0)+23*A_PtrSize), "ptr", this, "uint", eMessage, "uint", ulParam)
  814.    if hr or ErrorLevel
  815.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  816. }
  817.  
  818. IMFTransform_ProcessOutput(this, dwFlags, cOutputBufferCount, pOutputSamples, ByRef pdwStatus)
  819. {
  820.    hr := DllCall(NumGet(NumGet(this+0)+25*A_PtrSize), "ptr", this, "uint", dwFlags, "uint", cOutputBufferCount, "ptr", pOutputSamples, "uint*", pdwStatus)
  821.    if hr or ErrorLevel
  822.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  823. }
  824.  
  825. IMFTransform_ProcessInput(this, dwInputStreamID, pSample, dwFlags)
  826. {
  827.    hr := DllCall(NumGet(NumGet(this+0)+24*A_PtrSize), "ptr", this, "uint", dwInputStreamID, "ptr", pSample, "uint", dwFlags)
  828.    if hr or ErrorLevel
  829.    {
  830.       if (hr&=0xFFFFFFFF) = 0xC00D36B2   ; MF_E_INVALIDREQUEST
  831.          return "MF_E_INVALIDREQUEST"
  832.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  833.    }
  834. }
  835.  
  836. IMFTransform_GetOutputStreamInfo(this, dwInputStreamID, pStreamInfo)
  837. {
  838.    hr := DllCall(NumGet(NumGet(this+0)+7*A_PtrSize), "ptr", this, "uint", dwInputStreamID, "ptr", pStreamInfo)
  839.    if hr or ErrorLevel
  840.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  841. }
  842.  
  843. IMFTransform_SetOutputType(this, dwInputStreamID, pType, dwFlags)
  844. {
  845.    hr := DllCall(NumGet(NumGet(this+0)+16*A_PtrSize), "ptr", this, "uint", dwInputStreamID, "ptr", pType, "uint", dwFlags)
  846.    if hr or ErrorLevel
  847.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  848. }
  849.  
  850. IMFTransform_SetInputType(this, dwInputStreamID, pType, dwFlags)
  851. {
  852.    hr := DllCall(NumGet(NumGet(this+0)+15*A_PtrSize), "ptr", this, "uint", dwInputStreamID, "ptr", pType, "uint", dwFlags)
  853.    if hr or ErrorLevel
  854.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  855. }
  856.  
  857. IWMResamplerProps_SetHalfFilterLength(this, lhalfFilterLen)
  858. {
  859.    hr := DllCall(NumGet(NumGet(this+0)+3*A_PtrSize), "ptr", this, "int", lhalfFilterLen)
  860.    if hr or ErrorLevel
  861.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  862. }
  863.  
  864. MF_GUID(ByRef GUID, name)
  865. {
  866.    static init:=1, _:={}
  867.    if init
  868.    {
  869.       init:=0
  870.       _.MF_MT_MAJOR_TYPE := [0x48eba18e, 0xf8c9, 0x4687, 0xbf, 0x11, 0x0a, 0x74, 0xc9, 0xf9, 0x6a, 0x8f]
  871.       _.MF_MT_SUBTYPE := [0xf7e34c9a, 0x42e8, 0x4714, 0xb7, 0x4b, 0xcb, 0x29, 0xd7, 0x2c, 0x35, 0xe5]
  872.       _.MF_MT_AVG_BITRATE := [0x20332624, 0xfb0d, 0x4d9e, 0xbd, 0x0d, 0xcb, 0xf6, 0x78, 0x6c, 0x10, 0x2e]
  873.       _.MF_MT_INTERLACE_MODE := [0xe2724bb8, 0xe676, 0x4806, 0xb4, 0xb2, 0xa8, 0xd6, 0xef, 0xb4, 0x4c, 0xcd]
  874.       _.MF_MT_FRAME_SIZE := [0x1652c33d, 0xd6b2, 0x4012, 0xb8, 0x34, 0x72, 0x03, 0x08, 0x49, 0xa3, 0x7d]
  875.       _.MF_MT_FRAME_RATE := [0xc459a2e8, 0x3d2c, 0x4e44, 0xb1, 0x32, 0xfe, 0xe5, 0x15, 0x6c, 0x7b, 0xb0]
  876.       _.MF_MT_PIXEL_ASPECT_RATIO := [0xc6376a1e, 0x8d0a, 0x4027, 0xbe, 0x45, 0x6d, 0x9a, 0x0a, 0xd3, 0x9b, 0xb6]
  877.       _.MF_MT_AUDIO_AVG_BYTES_PER_SECOND := [0x1aab75c8, 0xcfef, 0x451c, 0xab, 0x95, 0xac, 0x03, 0x4b, 0x8e, 0x17, 0x31]
  878.       _.MF_MT_AUDIO_BLOCK_ALIGNMENT := [0x322de230, 0x9eeb, 0x43bd, 0xab, 0x7a, 0xff, 0x41, 0x22, 0x51, 0x54, 0x1d]
  879.       _.MF_MT_AUDIO_SAMPLES_PER_SECOND := [0x5faeeae7, 0x0290, 0x4c31, 0x9e, 0x8a, 0xc5, 0x34, 0xf6, 0x8d, 0x9d, 0xba]
  880.       _.MF_MT_AUDIO_BITS_PER_SAMPLE := [0xf2deb57f, 0x40fa, 0x4764, 0xaa, 0x33, 0xed, 0x4f, 0x2d, 0x1f, 0xf6, 0x69]
  881.       _.MF_MT_AUDIO_NUM_CHANNELS := [0x37e48bf5, 0x645e, 0x4c5b, 0x89, 0xde, 0xad, 0xa9, 0xe2, 0x9b, 0x69, 0x6a]
  882.       _.MFT_CATEGORY_VIDEO_ENCODER := [0xf79eac7d, 0xe545, 0x4387, 0xbd, 0xee, 0xd6, 0x47, 0xd7, 0xbd, 0xe4, 0x2a]
  883.       _.MF_TRANSCODE_CONTAINERTYPE := [0x150ff23f, 0x4abc, 0x478b, 0xac, 0x4f, 0xe1, 0x91, 0x6f, 0xba, 0x1c, 0xca]
  884.       _.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS := [0xa634a91c, 0x822b, 0x41b9, 0xa4, 0x94, 0x4d, 0xe4, 0x64, 0x36, 0x12, 0xb0]
  885.       _.MFTranscodeContainerType_MPEG4 := [0xdc6cd05d, 0xb9d0, 0x40ef, 0xbd, 0x35, 0xfa, 0x62, 0x2c, 0x1a, 0xb2, 0x8a]
  886.       _.MFT_FRIENDLY_NAME_Attribute := [0x314ffbae, 0x5b41, 0x4c95, 0x9c, 0x19, 0x4e, 0x7d, 0x58, 0x6f, 0xac, 0xe3]
  887.       _.MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME := [0x60d0e559, 0x52f8, 0x4fa2, 0xbb, 0xce, 0xac, 0xdb, 0x34, 0xa8, 0xec, 0x1]
  888.       _.MF_SINK_WRITER_DISABLE_THROTTLING := [0x08b845d8, 0x2b74, 0x4afe, 0x9d, 0x53, 0xbe, 0x16, 0xd2, 0xd5, 0xae, 0x4f]
  889.       _.MF_LOW_LATENCY := [0x9c27891a, 0xed7a, 0x40e1, 0x88, 0xe8, 0xb2, 0x27, 0x27, 0xa0, 0x24, 0xee]
  890.       _.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE := [0xc60ac5fe, 0x252a, 0x478f, 0xa0, 0xef, 0xbc, 0x8f, 0xa5, 0xf7, 0xca, 0xd3]
  891.       _.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID := [0x14dd9a1c, 0x7cff, 0x41be, 0xb1, 0xb9, 0xba, 0x1a, 0xc6, 0xec, 0xb5, 0x71]
  892.       _.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID := [0x8ac3587a, 0x4ae7, 0x42d8, 0x99, 0xe0, 0x0a, 0x60, 0x13, 0xee, 0xf9, 0x0f]
  893.       _.MF_SOURCE_READER_DISCONNECT_MEDIASOURCE_ON_SHUTDOWN := [0x56b67165, 0x219e, 0x456d, 0xa2, 0x2e, 0x2d, 0x30, 0x04, 0xc7, 0xfe, 0x56]
  894.       _.MF_MT_ALL_SAMPLES_INDEPENDENT := [0xc9173739, 0x5e56, 0x461c, 0xb7, 0x13, 0x46, 0xfb, 0x99, 0x5c, 0xb9, 0x5f]
  895.       _.MF_SOURCE_READER_ASYNC_CALLBACK := [0x1e3dbeac, 0xbb43, 0x4c35, 0xb5, 0x07, 0xcd, 0x64, 0x44, 0x64, 0xc9, 0x65]
  896.       _.MFSampleExtension_Discontinuity := [0x9cdf01d9, 0xa0f0, 0x43ba, 0xb0, 0x77, 0xea, 0xa0, 0x6c, 0xbd, 0x72, 0x8a]
  897.       _.MFMediaType_Video := [0x73646976, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  898.       _.MFMediaType_Audio := [0x73647561, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71]
  899.       _.MFAudioFormat_AAC := [0x1610, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  900.       _.MFAudioFormat_Float := [0x0003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  901.       _.MFAudioFormat_PCM := [0x0001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  902.       _.MFVideoFormat_H264 := [0x34363248, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]   ; FCC("H264") = 0x34363248
  903.       _.MFVideoFormat_RGB32 := [0x00000016, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  904.       _.MFVideoFormat_ARGB32 := [0x00000015, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  905.       _.MFVideoFormat_I420 := [0x30323449, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  906.       _.MFVideoFormat_IYUV := [0x56555949, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  907.       _.MFVideoFormat_NV12 := [0x3231564E, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  908.       _.MFVideoFormat_YUY2 := [0x32595559, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  909.       _.MFVideoFormat_YV12 := [0x32315659, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  910.       _.MFVideoFormat_RGB24 := [0x00000014, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]
  911.    }
  912.    if _.haskey(name)
  913.    {
  914.       p := _[name]
  915.       VarSetCapacity(GUID,16)
  916.       ,NumPut(p.1+(p.2<<32)+(p.3<<48),GUID,0,"int64")
  917.       ,NumPut(p.4+(p.5<<8)+(p.6<<16)+(p.7<<24)+(p.8<<32)+(p.9<<40)+(p.10<<48)+(p.11<<56),GUID,8,"int64")
  918.       return &GUID
  919.    }
  920.    else return name
  921. }
  922.  
  923. GUID(ByRef GUID, sGUID)
  924. {
  925.     VarSetCapacity(GUID, 16, 0)
  926.     return DllCall("ole32\CLSIDFromString", "WStr", sGUID, "Ptr", &GUID) >= 0 ? &GUID : ""
  927. }
  928.  
  929. FCC(var)
  930. {
  931.    c := StrSplit(var)
  932.    msgbox % clipboard := Format("{:#x}",((Asc(c[1])&255)+((Asc(c[2])&255)<<8)+((Asc(c[3])&255)<<16)+((Asc(c[4])&255)<<24)))
  933. }
  934.  
  935. Release(this)
  936. {
  937.    DllCall(NumGet(NumGet(this+0)+2*A_PtrSize), "ptr", this)
  938.    if ErrorLevel
  939.       _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
  940. }
  941.  
  942. MemoryDifference(ptr1, ptr2, num)
  943. {
  944.    return DllCall("msvcrt\memcmp", "ptr", ptr1, "ptr", ptr2, "int", num)
  945. }
  946.  
  947. _Error(val)
  948. {
  949.    msgbox % val
  950.    ExitApp
  951. }
  952.  
  953. checkCoordinates(ByRef start1, ByRef end1, ByRef start2, ByRef end2, hardware_encoder:="")
  954. {
  955.    if (A_OSVersion = "WIN_7") or !InStr(hardware_encoder, "NVIDIA") or ((x1 != "") and (CaptureCoordinatesWithCPU = true))
  956.       min1 := min2 := 33
  957.    else
  958.       min1 := 33, min2 := 17
  959.    max1 := A_ScreenWidth, max2 := A_ScreenHeight
  960.    loop 2
  961.    {
  962.       if (end%A_Index% - start%A_Index% < min%A_Index%)
  963.          end%A_Index% := start%A_Index% + min%A_Index%
  964.       if ((A_OSVersion = "WIN_7") or !InStr(hardware_encoder, "NVIDIA") or ((x1 != "") and (CaptureCoordinatesWithCPU = true))) and (mod(end%A_Index% - start%A_Index%, 2) != 0)
  965.          end%A_Index%++
  966.       if (end%A_Index% > max%A_Index%)
  967.       {
  968.          start%A_Index% += max%A_Index%-end%A_Index%
  969.          end%A_Index% := max%A_Index%
  970.       }
  971.    }
  972. }
  973.  
  974.  
  975.  
  976. IMFSourceReaderCallback_new() {
  977.    static VTBL := [ "QueryInterface"
  978.                   , "AddRef"
  979.                   , "Release"
  980.                   , "OnReadSample" A_PtrSize
  981.                   , "OnFlush"
  982.                   , "OnEvent" ]
  983.                  
  984.         , heapSize := A_PtrSize*10
  985.         , heapOffset := A_PtrSize*9
  986.        
  987.         , flags := (HEAP_GENERATE_EXCEPTIONS := 0x4) | (HEAP_NO_SERIALIZE := 0x1)
  988.         , HEAP_ZERO_MEMORY := 0x8
  989.    
  990.    hHeap := DllCall("HeapCreate", "UInt", flags, "Ptr", 0, "Ptr", 0, "Ptr")
  991.    addr := IMFSourceReaderCallback := DllCall("HeapAlloc", "Ptr", hHeap, "UInt", HEAP_ZERO_MEMORY, "Ptr", heapSize, "Ptr")
  992.    addr := NumPut(addr + A_PtrSize, addr + 0)
  993.    for k, v in VTBL
  994.       addr := NumPut( RegisterSyncCallback("IMFSourceReaderCallback_" . v), addr + 0 )
  995.    NumPut(hHeap, IMFSourceReaderCallback + heapOffset)
  996.    Return IMFSourceReaderCallback
  997. }
  998.  
  999. IMFSourceReaderCallback_QueryInterface(this, riid, ppvObject)
  1000. {
  1001.    static IID_IUnknown, IID_IMFSourceReaderCallback
  1002.    if (!VarSetCapacity(IID_IUnknown))
  1003.    {
  1004.       VarSetCapacity(IID_IUnknown, 16), VarSetCapacity(IID_IMFSourceReaderCallback, 16)
  1005.       DllCall("ole32\CLSIDFromString", "WStr", "{00000000-0000-0000-C000-000000000046}", "Ptr", &IID_IUnknown)
  1006.       DllCall("ole32\CLSIDFromString", "WStr", "{deec8d99-fa1d-4d82-84c2-2c8969944867}", "Ptr", &IID_IMFSourceReaderCallback)
  1007.    }
  1008.    if (DllCall("ole32\IsEqualGUID", "Ptr", riid, "Ptr", &IID_IMFSourceReaderCallback) || DllCall("ole32\IsEqualGUID", "Ptr", riid, "Ptr", &IID_IUnknown))
  1009.    {
  1010.       NumPut(this, ppvObject+0, "Ptr")
  1011.       IMFSourceReaderCallback_AddRef(this)
  1012.       return 0 ; S_OK
  1013.    }
  1014.    NumPut(0, ppvObject+0, "Ptr")
  1015.    return 0x80004002 ; E_NOINTERFACE
  1016. }
  1017.  
  1018. IMFSourceReaderCallback_AddRef(this) {
  1019.    static refOffset := A_PtrSize*8
  1020.    NumPut(refCount := NumGet(this + refOffset, "UInt") + 1, this + refOffset, "UInt")
  1021.    Return refCount
  1022. }
  1023.  
  1024. IMFSourceReaderCallback_Release(this) {
  1025.    static refOffset := A_PtrSize*8
  1026.         , heapOffset := A_PtrSize*9
  1027.    NumPut(refCount := NumGet(this + refOffset, "UInt") - 1, this + refOffset, "UInt")
  1028.    if (refCount = 0) {
  1029.       hHeap := NumGet(this + heapOffset)
  1030.       DllCall("HeapDestroy", "Ptr", hHeap)
  1031.    }
  1032.    Return refCount
  1033. }
  1034.  
  1035. /*
  1036.     RegisterSyncCallback
  1037.  
  1038.     A replacement for RegisterCallback for use with APIs that will call
  1039.     the callback on the wrong thread.  Synchronizes with the script's main
  1040.     thread via a window message.
  1041.  
  1042.     This version tries to emulate RegisterCallback as much as possible
  1043.     without using RegisterCallback, so shares most of its limitations,
  1044.     and some enhancements that could be made are not.
  1045.  
  1046.     Other differences from v1 RegisterCallback:
  1047.       - Variadic mode can't be emulated exactly, so is not supported.
  1048.       - A_EventInfo can't be set in v1, so is not supported.
  1049.       - Fast mode is not supported (the option is ignored).
  1050.       - ByRef parameters are allowed (but ByRef is ignored).
  1051.       - Throws instead of returning "" on failure.
  1052. */
  1053. RegisterSyncCallback(FunctionName, Options:="", ParamCount:="")
  1054. {
  1055.     if !(fn := Func(FunctionName)) || fn.IsBuiltIn
  1056.         throw Exception("Bad function", -1, FunctionName)
  1057.     if (ParamCount == "")
  1058.         ParamCount := fn.MinParams
  1059.     if (ParamCount > fn.MaxParams && !fn.IsVariadic || ParamCount+0 < fn.MinParams)
  1060.         throw Exception("Bad param count", -1, ParamCount)
  1061.  
  1062.     static sHwnd := 0, sMsg, sSendMessageW
  1063.     if !sHwnd
  1064.     {
  1065.         Gui RegisterSyncCallback: +Parent%A_ScriptHwnd% +hwndsHwnd
  1066.         OnMessage(sMsg := 0x8000, Func("RegisterSyncCallback_Msg"))
  1067.         sSendMessageW := DllCall("GetProcAddress", "ptr", DllCall("GetModuleHandle", "str", "user32.dll", "ptr"), "astr", "SendMessageW", "ptr")
  1068.     }
  1069.  
  1070.     if !(pcb := DllCall("GlobalAlloc", "uint", 0, "ptr", 96, "ptr"))
  1071.         throw
  1072.     DllCall("VirtualProtect", "ptr", pcb, "ptr", 96, "uint", 0x40, "uint*", 0)
  1073.  
  1074.     p := pcb
  1075.     if (A_PtrSize = 8)
  1076.     {
  1077.         /*
  1078.         48 89 4c 24 08  ; mov [rsp+8], rcx
  1079.         48 89 54'24 10  ; mov [rsp+16], rdx
  1080.         4c 89 44 24 18  ; mov [rsp+24], r8
  1081.         4c'89 4c 24 20  ; mov [rsp+32], r9
  1082.         48 83 ec 28'    ; sub rsp, 40
  1083.         4c 8d 44 24 30  ; lea r8, [rsp+48]  (arg 3, &params)
  1084.         49 b9 ..        ; mov r9, .. (arg 4, operand to follow)
  1085.         */
  1086.         p := NumPut(0x54894808244c8948, p+0)
  1087.         p := NumPut(0x4c182444894c1024, p+0)
  1088.         p := NumPut(0x28ec834820244c89, p+0)
  1089.         p := NumPut(  0xb9493024448d4c, p+0) - 1
  1090.         lParamPtr := p, p += 8
  1091.  
  1092.         p := NumPut(0xba, p+0, "char") ; mov edx, nmsg
  1093.         p := NumPut(sMsg, p+0, "int")
  1094.         p := NumPut(0xb9, p+0, "char") ; mov ecx, hwnd
  1095.         p := NumPut(sHwnd, p+0, "int")
  1096.         p := NumPut(0xb848, p+0, "short") ; mov rax, SendMessageW
  1097.         p := NumPut(sSendMessageW, p+0)
  1098.         /*
  1099.         ff d0        ; call rax
  1100.         48 83 c4 28  ; add rsp, 40
  1101.         c3           ; ret
  1102.         */
  1103.         p := NumPut(0x00c328c48348d0ff, p+0)
  1104.     }
  1105.     else ;(A_PtrSize = 4)
  1106.     {
  1107.         p := NumPut(0x68, p+0, "char")      ; push ... (lParam data)
  1108.         lParamPtr := p, p += 4
  1109.         p := NumPut(0x0824448d, p+0, "int") ; lea eax, [esp+8]
  1110.         p := NumPut(0x50, p+0, "char")      ; push eax
  1111.         p := NumPut(0x68, p+0, "char")      ; push nmsg
  1112.         p := NumPut(sMsg, p+0, "int")
  1113.         p := NumPut(0x68, p+0, "char")      ; push hwnd
  1114.         p := NumPut(sHwnd, p+0, "int")
  1115.         p := NumPut(0xb8, p+0, "char")      ; mov eax, &SendMessageW
  1116.         p := NumPut(sSendMessageW, p+0, "int")
  1117.         p := NumPut(0xd0ff, p+0, "short")   ; call eax
  1118.         p := NumPut(0xc2, p+0, "char")      ; ret argsize
  1119.         p := NumPut((InStr(Options, "C") ? 0 : ParamCount*4), p+0, "short")
  1120.     }
  1121.     NumPut(p, lParamPtr+0) ; To be passed as lParam.
  1122.     p := NumPut(&fn, p+0)
  1123.     p := NumPut(ParamCount, p+0, "int")
  1124.     return pcb
  1125. }
  1126.  
  1127. RegisterSyncCallback_Msg(wParam, lParam)
  1128. {
  1129.     if (A_Gui != "RegisterSyncCallback")
  1130.         return
  1131.     fn := Object(NumGet(lParam + 0))
  1132.     paramCount := NumGet(lParam + A_PtrSize, "int")
  1133.     params := []
  1134.     Loop % paramCount
  1135.         params.Push(NumGet(wParam + A_PtrSize * (A_Index-1)))
  1136.     return %fn%(params*)
  1137. }
  1138.  
  1139. IMFSourceReaderCallback_OnReadSample4(this_, hrStatus, dwStreamIndex, dwStreamFlags, llTimestamp, llTimestamp1, pSample)
  1140. {
  1141.    Static audioStart, gapAudio
  1142.    critical
  1143.    if hrStatus
  1144.       _Error(A_ThisFunc " error: " hrStatus "`nErrorLevel: " ErrorLevel)
  1145.    llTimestamp |= (llTimestamp1 << 32)
  1146.    if (pSample != 0)
  1147.    {
  1148.       if (gapAudio = 1)
  1149.       {
  1150.          IMFAttributes_SetUINT32(pSample, MF_GUID(GUID, "MFSampleExtension_Discontinuity"), true)
  1151.          gapAudio := ""
  1152.       }
  1153.       if (audioStart = "")
  1154.          audioStart := llTimestamp
  1155.       IMFSample_SetSampleTime(pSample, llTimestamp - audioStart)
  1156.       if (IMFTransform = 1)
  1157.       {
  1158.          IMFSample_GetSampleDuration(pSample, llSampleDuration)
  1159.          loop
  1160.          {
  1161.             if (IMFTransform_ProcessInput(pTransform, 0, pSample, 0) != "MF_E_INVALIDREQUEST")
  1162.                break
  1163.             msgbox error
  1164.          }
  1165.          MFCreateSample(pSample1)
  1166.          VarSetCapacity(MFT_OUTPUT_DATA_BUFFER, 4*A_PtrSize, 0)
  1167.          NumPut(pSample1, MFT_OUTPUT_DATA_BUFFER, A_PtrSize, "ptr")
  1168.          MFCreateMemoryBuffer(cbOutBytes, pBuffer)
  1169.          IMFSample_AddBuffer(pSample1, pBuffer)
  1170.          IMFTransform_ProcessOutput(pTransform, 0, 1, &MFT_OUTPUT_DATA_BUFFER, processOutputStatus)
  1171.          IMFSample_SetSampleTime(pSample1, llTimestamp - audioStart)
  1172.          IMFSample_SetSampleDuration(pSample1, llSampleDuration)
  1173.          IMFSinkWriter_WriteSample(pSinkWriter, audioStreamIndex, pSample1)
  1174.          Release(pSample1)
  1175.          Release(pBuffer)
  1176.          pSample1 := pBuffer := ""
  1177.       }
  1178.       else
  1179.          IMFSinkWriter_WriteSample(pSinkWriter, audioStreamIndex, pSample)
  1180.    }
  1181.    else if (dwStreamFlags & MF_SOURCE_READERF_STREAMTICK := 256) and (audioStart != "")
  1182.    {
  1183.       IMFSinkWriter_SendStreamTick(pSinkWriter, audioStreamIndex, llTimestamp - audioStart)
  1184.       gapAudio := 1
  1185.    }
  1186.    if (flush = "")
  1187.       IMFSourceReader_ReadSample(SourceReader, MF_SOURCE_READER_ANY_STREAM := 0xFFFFFFFE, 0, 0, 0, 0, 0)
  1188.    else
  1189.    {
  1190.       Release(IMFSourceReaderCallback)
  1191.       Release(SourceReader)
  1192.       SourceReader := IMFSourceReaderCallback := flush := audioStart := gapAudio := ""
  1193.    }
  1194.    return
  1195. }
  1196.  
  1197. IMFSourceReaderCallback_OnReadSample8(this_, hrStatus, dwStreamIndex, dwStreamFlags, llTimestamp, pSample)
  1198. {
  1199.    Static audioStart, gapAudio
  1200.    critical
  1201.    if hrStatus
  1202.       _Error(A_ThisFunc " error: " hrStatus "`nErrorLevel: " ErrorLevel)
  1203.    if (pSample != 0)
  1204.    {
  1205.       if (gapAudio = 1)
  1206.       {
  1207.          IMFAttributes_SetUINT32(pSample, MF_GUID(GUID, "MFSampleExtension_Discontinuity"), true)
  1208.          gapAudio := ""
  1209.       }
  1210.       if (audioStart = "")
  1211.          audioStart := llTimestamp
  1212.       IMFSample_SetSampleTime(pSample, llTimestamp - audioStart)
  1213.       if (IMFTransform = 1)
  1214.       {
  1215.          IMFSample_GetSampleDuration(pSample, llSampleDuration)
  1216.          loop
  1217.          {
  1218.             if (IMFTransform_ProcessInput(pTransform, 0, pSample, 0) != "MF_E_INVALIDREQUEST")
  1219.                break
  1220.             msgbox error
  1221.          }
  1222.          MFCreateSample(pSample1)
  1223.          VarSetCapacity(MFT_OUTPUT_DATA_BUFFER, 4*A_PtrSize, 0)
  1224.          NumPut(pSample1, MFT_OUTPUT_DATA_BUFFER, A_PtrSize, "ptr")
  1225.          MFCreateMemoryBuffer(cbOutBytes, pBuffer)
  1226.          IMFSample_AddBuffer(pSample1, pBuffer)
  1227.          IMFTransform_ProcessOutput(pTransform, 0, 1, &MFT_OUTPUT_DATA_BUFFER, processOutputStatus)
  1228.          IMFSample_SetSampleTime(pSample1, llTimestamp - audioStart)
  1229.          IMFSample_SetSampleDuration(pSample1, llSampleDuration)
  1230.          IMFSinkWriter_WriteSample(pSinkWriter, audioStreamIndex, pSample1)
  1231.          Release(pSample1)
  1232.          Release(pBuffer)
  1233.          pSample1 := pBuffer := ""
  1234.       }
  1235.       else
  1236.          IMFSinkWriter_WriteSample(pSinkWriter, audioStreamIndex, pSample)
  1237.    }
  1238.    else if (dwStreamFlags & MF_SOURCE_READERF_STREAMTICK := 256) and (audioStart != "")
  1239.    {
  1240.       IMFSinkWriter_SendStreamTick(pSinkWriter, audioStreamIndex, llTimestamp - audioStart)
  1241.       gapAudio := 1
  1242.    }
  1243.    if (flush = "")
  1244.       IMFSourceReader_ReadSample(SourceReader, MF_SOURCE_READER_ANY_STREAM := 0xFFFFFFFE, 0, 0, 0, 0, 0)
  1245.    else
  1246.    {
  1247.       Release(IMFSourceReaderCallback)
  1248.       Release(SourceReader)
  1249.       SourceReader := IMFSourceReaderCallback := flush := audioStart := gapAudio := ""
  1250.    }
  1251.    return
  1252. }
  1253.  
  1254. IMFSourceReaderCallback_OnFlush(this_, dwStreamIndex)
  1255. {
  1256.    return
  1257. }
  1258.  
  1259. IMFSourceReaderCallback_OnEvent(this_)
  1260. {
  1261.    return
  1262. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top