Video capture issues

Just starting out? Need help? Post your questions and find answers here.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Video capture issues

Post by Caronte3D »

chris319 wrote: Fri May 12, 2023 9:25 am What version of PB are you using?
Latest beta.
chris319 wrote: Fri May 12, 2023 9:25 am Can you see your camera using another program?
Yes I can see my camera with all programs (even with the previous example you posted here):
viewtopic.php?p=598200#p598200
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Video capture issues

Post by chris319 »

It works for me. I am not using a beta version of pb.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Video capture issues

Post by chris319 »

Latest version:

Code: Select all

;WORKS WITH ESCAPI
;LAST MODIFIED 5/12/2023

InitKeyboard()
InitSprite()

;MAKE LUT
factor.f = 255/Pow(255,gamma.f)
Dim gammaTable.a(257)
For cnt = 0 To 255
gammaTable.a(cnt) = cnt
r=(Pow(rf.f,gamma.f) * factor.f) + PEDESTAL.f
Next

result = ExamineDesktops(): Global V_RES = DesktopHeight(0): Global H_RES = DesktopWidth(0)
SCRNH = V_RES: SCRNW = H_RES

Global fullScreen
Global recordingNow

Global clip = #True
;Global whiteClip = 235 ;100 IRE
;Global whiteClip = 239 ;102 IRE
Global whiteClip = 246 ;105 IRE
;Global whiteClip = 254 ;108 IRE

std$ = "SDR"
;"HLG" OR "SDR" OR "HDR"

Enumeration
#GAINGAD = 1;GAIN GADGET
#GAMMA;GAMMA GADGET
#PEDESTAL;PEDESTAL GADGET
#GAINTEXT
#PEDTEXT
#GAMMATEXT
EndEnumeration

;#UNITY = 0.5
#KR = 0.2126:#KG = 0.7152:#KB = 0.0722 ;REC.709

;PRESETS
gain.f = 1.2
gamma.f = 0.95: factor.f = 255/Pow(255,gamma)
pedestal.f = -4

IncludeFile "escapi.pbi"

Global HEIGHT = (V_RES / 2)-40
Global WIDTH = HEIGHT * 16 / 9
;Global WIDTH = (H_RES / 4)-40
Global WIDTHM1 = WIDTH - 1
Global HEIGHTM1 = V_RES - 1

Procedure MakeGraticule_720(std$)
StartDrawing(ImageOutput(3))  
DrawingMode(#PB_2DDrawing_Default)
;BLACK
lum = 16
LineXY(68,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff);BLACK
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
DrawText(445,(640 - lum*2)-45, "0%", $ffffff)

;lum = 246 ;105 IRE
;LineXY(68,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff);BLACK
;LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)

If std$ = "HLG" Or std$ = "HDR"
lum = 397 / 4 ;HLG
LineXY(36,(255 - lum) + 52, WIDTH-45,(255 - lum) + 52,$00ffff)
DrawText(600,(255 - lum) + 42,"38", $ffffff)
;DrawText(2,(255 - lum) + 44,"397", $ffffff)
DrawText(6,(255 - lum) + 42, "18%", $ffffff)

ElseIf std$ = "SDR"
  
  lum = 104; 40 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "40%", $ffffff)
  
  lum = 82; 30 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "30%", $ffffff)
  
  lum = 38; 10 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "10%", $ffffff)
  
  lum = 169; 70 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "70%", $ffffff)
  
  lum = 213; 90 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "90%", $ffffff)

  lum = 60; 20 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45 , "20%", $ffffff)
  
  lum = 147 ;60 IRE
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(445,(640 - lum*2)-45, "60%", $ffffff)

lum = 191; 80 IRE
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
DrawText(445,(640 - lum*2)-45, "80%", $ffffff)

lum = 126; 50 IRE
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
DrawText(445,(640 - lum*2)-45, "50%", $ffffff)
EndIf

;HLG 90% REFLECTANCE
If std$ = "HLG" Or std$ = "HDR"
;lum = 235
;LineXY(36,(640 - lum*2), WIDTH-45,(640 - lum*2),$00ffff)
;DrawText(600,(510 - lum) + 84, "100", $ffffff)
;DrawText(6,(510 - lum) + 84,"90%", $ffffff)

ElseIf std$ = "SDR"
lum = 235 ;90% REFLECTANCE
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45, $00ffff)
DrawText(445,(640 - lum*2)-45, "100%", $ffffff)

EndIf

;DrawText(6, 90, "REFL.", $ffffff)
;DrawText(600, 90, "IRE", $ffffff)
StopDrawing()

EndProcedure

Procedure Graticule_1080(std$)
DrawingMode(#PB_2DDrawing_Default)
;BLACK
lum = 16
LineXY(68,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff);BLACK
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
DrawText(6,(440 - lum*2)-45, "0", $ffffff)
;DrawText(6,(640 - lum*2)-45, " 0%", $ffffff)

;GAMMA
If std$ = "HLG" Or std$ = "HDR"
lum = 397 / 4 ;HLG
LineXY(36,(255 - lum) + 52, WIDTH-45,(255 - lum) + 52,$00ffff)
DrawText(600,(255 - lum) + 42,"38", $ffffff)
;DrawText(2,(255 - lum) + 44,"397", $ffffff)
DrawText(6,(255 - lum) + 42, "18%", $ffffff)

ElseIf std$ = "SDR"
  
  lum = 104
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(600,(640 - lum*2)-45 , "40", $ffffff)
  DrawText(6,(640 - lum*2)-45, "18%", $ffffff)
  
  lum = 82
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(600,(640 - lum*2)-45, "30", $ffffff)
  
  lum = 126
  LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45,$00ffff)
  DrawText(600,(640 - lum*2)-45, "50", $ffffff)
  
EndIf

;WHITE
;HLG 90% REFLECTANCE
If std$ = "HLG" Or std$ = "HDR"
;lum = 235
;LineXY(36,(640 - lum*2), WIDTH-45,(640 - lum*2),$00ffff)
;DrawText(500,(510 - lum) + 84, "100", $ffffff)
;DrawText(6,(510 - lum) + 84,"90%", $ffffff)

ElseIf std$ = "SDR"
lum = 235 ;90% REFLECTANCE
LineXY(36,(640 - lum*2)-45, WIDTH-45,(640 - lum*2)-45, $00ffff)
DrawText(600,(640 - lum*2)-45, "100", $ffffff)
DrawText(6,(640 - lum*2)-45, "90%", $ffffff)

EndIf

DrawText(6, 90, "REFL.", $ffffff)
DrawText(600, 90, "IRE", $ffffff)

EndProcedure

LoadFont(1,"arial",13,#PB_Font_Bold);FOR SCOPE GRATICULE
LoadFont(2,"arial",21);FOR GADGETS

device = 0
count = setupESCAPI()

    If count = 0
      MessageRequester("Error", "Unable to initialize ESCAPI.  Use 32-bit version")
      End
    EndIf

    ;name$ = Space(255)
    getCaptureDeviceName(device, @name$, 255)

    bufSize = H_RES * V_RES * 4
    scp.SimpleCapParams
    scp\mWidth = H_RES
    scp\mHeight = V_RES
    scp\mTargetBuf = AllocateMemory(bufSize)

If initCapture(device, @scp) = #False
 MessageRequester("Error", "Unable to initialize webcam.")
 End
EndIf
     
image1 = CreateImage(1, (H_RES-290)/2, V_RES - 66, 24) ;SCOPE
image2 = CreateImage(2, H_RES,V_RES, 24);CAMERA
image3 = CreateImage(3, (H_RES-290)/2, V_RES - 66, 24):MakeGraticule_720(std$) ;GRATICULE

OpenWindow(2,0,0, H_RES, V_RES,"Monitor", #PB_Window_BorderLess) ;FULL-SCREEN WINDOW
AddKeyboardShortcut(2,#PB_Shortcut_F11,#PB_Shortcut_F11) ;F11 FOR FULL SCREEN
AddKeyboardShortcut(2,#PB_Shortcut_Space,#PB_Shortcut_Space) ;SPACE BAR TO STOP/START RECORDING
AddKeyboardShortcut(2, #PB_Shortcut_Control|#PB_Shortcut_Q, 113);CTL Q TO QUIT
AddKeyboardShortcut(2, #PB_Shortcut_Escape,27);ESC TO QUIT
OpenWindowedScreen(WindowID(2),0,0,H_RES,V_RES,0,0,0,#PB_Screen_NoSynchronization)
;CanvasGadget(101,0,0,H_RES,V_RES,#PB_Canvas_Container)
HideWindow(2,#True)

;MAIN WINDOW
OpenWindow(1,72,0, WIDTH*2, V_RES - 66,"Waveform", #PB_Window_SystemMenu)
AddKeyboardShortcut(1,#PB_Shortcut_F11,#PB_Shortcut_F11) ;F11 FOR FULL SCREEN
AddKeyboardShortcut(1,#PB_Shortcut_Space,#PB_Shortcut_Space) ;SPACE BAR TO STOP/START RECORDING
AddKeyboardShortcut(1, #PB_Shortcut_Control|#PB_Shortcut_Q, 113);CTL Q TO QUIT
AddKeyboardShortcut(1, #PB_Shortcut_Escape,27);ESC TO QUIT
;OpenWindowedScreen(WindowID(1),0,0,H_RES,V_RES,0,0,0,#PB_Screen_WaitSynchronization)

If V_RES = 720
SpinGadget(#GAINGAD, WIDTH * 1.5, 15,200,48,0,100)
SpinGadget(#GAMMA,WIDTH * 1.5, 65,200,48,0,100)
SpinGadget(#PEDESTAL,WIDTH * 1.5, 115,200,48,0,100)
SetGadgetFont(#GAINGAD,FontID(2))
SetGadgetFont(#PEDESTAL,FontID(2))
SetGadgetFont(#GAMMA,FontID(2))

SetGadgetText(#GAINGAD,StrD(gain,2))
SetGadgetText(#GAMMA,StrD(gamma,2))
SetGadgetText(#PEDESTAL,StrD(pedestal,2))

TextGadget(#GAINTEXT,WIDTH * 1.1,20,200,48,"GAIN")
TextGadget(#PEDTEXT,WIDTH * 1.1,70,200,48,"GAMMA")
TextGadget(#GAMMATEXT,WIDTH * 1.1,120,200,48,"PEDESTAL")

TextGadget(11,WIDTH * 1.1,180,600,48,"WHITE: 100%")
TextGadget(12,WIDTH * 1.1,220,600,48,"18% GRAY: 40%")
TextGadget(13,WIDTH * 1.1,260,600,48,"BLACK: 0%")

SetGadgetFont(#GAINTEXT,FontID(2))
SetGadgetFont(#PEDTEXT,FontID(2))
SetGadgetFont(#GAMMATEXT,FontID(2))
SetGadgetFont(11,FontID(2))
SetGadgetFont(12,FontID(2))
SetGadgetFont(13,FontID(2))

ElseIf V_RES = 1080
EndIf

;setCaptureProperty(DEVICE,#CAPTURE_ZOOM,0,#VideoProcAmp_Flags_Manual)
;setCaptureProperty(DEVICE,#CAPTURE_BRIGHTNESS,0.5,#VideoProcAmp_Flags_Manual); INITIALIZE TO UNITY

Quit = #False

Repeat ;MAIN LOOP
doCapture(device)

Delay(1) ;1 FRAME ~ 16 MILLISECONDS
event = WindowEvent()

If event = #PB_Event_CloseWindow
       Quit = #True
       Break
ElseIf event = #PB_Event_Gadget

If EventGadget() = #GAINGAD
If EventType() = #PB_EventType_Up:gain + 0.01
ElseIf EventType() = #PB_EventType_Down:gain - 0.01
SetGadgetText(#GAINGAD,StrD(gain,2))
EndIf

ElseIf EventGadget() = #GAMMA
If EventType() = #PB_EventType_Up:gamma - 0.05
ElseIf EventType() = #PB_EventType_Down:gamma + 0.05
SetGadgetText(#GAMMA,StrD(gamma,2))
factor.f = 255/Pow(255,gamma)
EndIf

ElseIf EventGadget() = #PEDESTAL
If EventType() = #PB_EventType_Up:pedestal + 1
ElseIf EventType() = #PB_EventType_Down:pedestal - 1
SetGadgetText(#PEDESTAL,StrD(pedestal,2))
EndIf
EndIf

ElseIf event = #PB_Event_Menu
menuItem = EventMenu()

If menuitem = #PB_Shortcut_F11
If fullScreen <> #False
  fullScreen = #False:ShowCursor_(#True)
  HideWindow(2,#True)
SetActiveWindow(1)
Else
fullScreen = #True:ShowCursor_(#False)
HideWindow(2,#False)
SetActiveWindow(2)
EndIf

ElseIf menuitem = #PB_Shortcut_Space
  Debug "space bar"

EndIf

EndIf

;SCOPE
If fullScreen = #False
StartDrawing(ImageOutput(1))

DrawImage(ImageID(3),0,0);DRAW GRATICULE
;ElseIf V_RES = 1080: Graticule_1080(std$)
;EndIf

*readBufr = scp\mTargetBuf

;DRAW SCOPE
For y = 0 To HEIGHTM1
For x = 0 To WIDTHM1
r = (PeekA(*readBufr+2))
g = (PeekA(*readBufr+1))
b = (PeekA(*readBufr))

;Goto skip

rf.f = r * gain
gf.f = g * gain
bf.f = b * gain

 r=(Pow(rf,gamma) * factor) + PEDESTAL
 If clip = #True And r > whiteClip:r=whiteClip:EndIf;clip at 105 IRE
 If r < 16:r=16:EndIf;clip
 
 g=(Pow(gf,gamma) * factor) + PEDESTAL
 If clip = #True And g > whiteClip:g=whiteClip:EndIf;clip at 105 IRE
 If g < 16:g=16:EndIf;clip
 
 b=(Pow(bf,gamma) * factor) + PEDESTAL
 If clip = #True And b > whiteClip:b=whiteClip:EndIf;clip at 105 IRE
 If b < 16:b=16:EndIf;clip

;DRAW SCOPE
lum.f = (r*#KR) + (g*#KG) + (b*#KB)
;lum = 235;CALIBRATE

Plot(x/2*0.75+70, (640 - lum*2)-45, $00cc00)

*readBufr + 4
Next
hm1 - pitch
Next

StopDrawing()
skip:

;DRAW CAMERA

*readBufr = scp\mTargetBuf

;DRAW CAMERA
StartDrawing(ImageOutput(2))
For y = 0 To V_RES - 1: For x = 0 To H_RES - 1
    
r = (PeekA(*readBufr+2))
g = (PeekA(*readBufr+1))
b = (PeekA(*readBufr))

rf.f = r * gain
gf.f = g * gain
bf.f = b * gain

r=(Pow(rf,gamma) * factor) + PEDESTAL
If clip = #True And r > whiteClip:r=whiteClip:EndIf;clip at 105 IRE
If r < 16:r=16:EndIf;clip

g=(Pow(gf,gamma) * factor) + PEDESTAL
If clip = #True And g > whiteClip:g=whiteClip:EndIf;clip at 105 IRE
If g < 16:g=16:EndIf;clip

b=(Pow(bf,gamma) * factor) + PEDESTAL
If clip = #True And b > whiteClip:b=whiteClip:EndIf;clip at 105 IRE
If b < 16:b=16:EndIf;clip

Plot(x, y, RGB(r,g,b));MONITOR IS SLIGHTLY UNDERSCANNED

*readBufr + 4

Next
hm1 - pitch
Next

;LineXY(0,568 * 9/16,568,568*9/16,#Green)
;Debug WIDTH 568
StopDrawing()

Else ;FULLSCREEN = TRUE
*readBufr = scp\mTargetBuf
StartDrawing(ImageOutput(2))

For y = 0 To V_RES - 1: For x = 0 To H_RES - 1
    
r = (PeekA(*readBufr+2))
g = (PeekA(*readBufr+1))
b = (PeekA(*readBufr))

rf.f = r * gain
gf.f = g * gain
bf.f = b * gain

r=(Pow(rf,gamma) * factor) + PEDESTAL
If clip = #True And r > whiteClip:r=whiteClip:EndIf;clip at 105 IRE
If r < 16:r=16:EndIf;clip

g=(Pow(gf,gamma) * factor) + PEDESTAL
If clip = #True And g > whiteClip:g=whiteClip:EndIf;clip at 105 IRE
If g < 16:g=16:EndIf;clip

b=(Pow(bf,gamma) * factor) + PEDESTAL
If clip = #True And b > whiteClip:b=whiteClip:EndIf;clip at 105 IRE
If b < 16:b=16:EndIf;clip

Plot(x, y, RGB(r,g,b));MONITOR IS SLIGHTLY UNDERSCANNED

*readBufr + 4

Next
hm1 - pitch
Next

StopDrawing()

EndIf

If fullScreen = #False
StartDrawing(WindowOutput(1))
DrawImage(ImageID(1),0,0);DRAW SCOPE TO SCREEN
;If V_RES = 720:DrawImage(ImageID(2),568,300,1280,360):EndIf;DRAW CAMERA TO SCREEN
If V_RES = 720:DrawImage(ImageID(2),496,294,640,360):EndIf;DRAW CAMERA TO SCREEN

StopDrawing()

Else ;FULL SCREEN
StartDrawing(WindowOutput(2))
;StartDrawing(CanvasOutput(101))

DrawImage(ImageID(2),0,0);DRAW CAMERA TO CANVAS GADGET
StopDrawing()
;FlipBuffers()
EndIf

StopDrawing()

Until quit <> #False

Delay(500)
;StopDrawing()
deinitCapture(device)
FreeImage(1):FreeImage(2):FreeImage(3)
FreeMemory(scp\mTargetBuf)

CloseWindow(1)
End
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Video capture issues

Post by chris319 »

I have fixed the bottleneck that was giving me a slow frame rate. I made a lookup table for gamma and eliminated the floating point. Will post the source code later.
Post Reply