Skip to content

Commit

Permalink
Capture from device and file with HW acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce authored and deadprogram committed Sep 16, 2024
1 parent 1dc4376 commit 1dce353
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
21 changes: 21 additions & 0 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ bool VideoCapture_OpenWithAPI(VideoCapture v, const char* uri, int apiPreference
return v->open(uri, apiPreference);
}

bool VideoCapture_OpenWithAPIParams(VideoCapture v, const char* uri, int apiPreference, int *paramsv, int paramsc) {
std::vector< int > params;

for( int i = 0; i< paramsc; i++) {
params.push_back(paramsv[i]);
}

return v->open(uri, apiPreference, params);
}

bool VideoCapture_OpenDevice(VideoCapture v, int device) {
return v->open(device);
}
Expand All @@ -25,6 +35,17 @@ bool VideoCapture_OpenDeviceWithAPI(VideoCapture v, int device, int apiPreferenc
return v->open(device, apiPreference);
}

bool VideoCapture_OpenDeviceWithAPIParams(VideoCapture v, int device, int apiPreference, int *paramsv, int paramsc) {
std::vector< int > params;

for( int i = 0; i< paramsc; i++) {
params.push_back(paramsv[i]);
}

return v->open(device, apiPreference, params);
}


void VideoCapture_Set(VideoCapture v, int prop, double param) {
v->set(prop, param);
}
Expand Down
57 changes: 56 additions & 1 deletion videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ const (

// VideoCaptureBitrate displays the video bitrate in kbits/s. Read-only property.
VideoCaptureBitrate VideoCaptureProperties = 47

// VideoCaptureHWAcceleration Hardware acceleration type.
VideoCaptureHWAcceleration VideoCaptureProperties = 50

// VideoCaptureHWDevice Hardware device index (select GPU if multiple available).
VideoCaptureHWDevice VideoCaptureProperties = 51
)

// VideoCapture is a wrapper around the OpenCV VideoCapture class.
Expand Down Expand Up @@ -320,6 +326,21 @@ func VideoCaptureFileWithAPI(uri string, apiPreference VideoCaptureAPI) (vc *Vid
return
}

// VideoCaptureFileWithAPIParams opens a VideoCapture from a file and prepares
// to start capturing. It returns error if it fails to open the file stored in uri path.
func VideoCaptureFileWithAPIParams(uri string, apiPreference VideoCaptureAPI, params []VideoCaptureProperties) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}

cURI := C.CString(uri)
defer C.free(unsafe.Pointer(cURI))

if !C.VideoCapture_OpenWithAPIParams(vc.p, cURI, C.int(apiPreference), (*C.int)(unsafe.Pointer(&params[0])), C.int(len(params))) {
err = fmt.Errorf("Error opening file: %s with api backend: %d", uri, apiPreference)
}

return
}

// VideoCaptureDevice opens a VideoCapture from a device and prepares
// to start capturing. It returns error if it fails to open the video device.
func VideoCaptureDevice(device int) (vc *VideoCapture, err error) {
Expand All @@ -332,7 +353,7 @@ func VideoCaptureDevice(device int) (vc *VideoCapture, err error) {
return
}

// VideoCaptureDevice opens a VideoCapture from a device with the api preference.
// VideoCaptureDeviceWithAPI opens a VideoCapture from a device with the api preference.
// It returns error if it fails to open the video device.
func VideoCaptureDeviceWithAPI(device int, apiPreference VideoCaptureAPI) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}
Expand All @@ -344,6 +365,18 @@ func VideoCaptureDeviceWithAPI(device int, apiPreference VideoCaptureAPI) (vc *V
return
}

// VideoCaptureDeviceWithAPIParams opens a VideoCapture from a device with the api preference.
// It returns error if it fails to open the video device.
func VideoCaptureDeviceWithAPIParams(device int, apiPreference VideoCaptureAPI, params []VideoCaptureProperties) (vc *VideoCapture, err error) {
vc = &VideoCapture{p: C.VideoCapture_New()}

if !C.VideoCapture_OpenDeviceWithAPIParams(vc.p, C.int(device), C.int(apiPreference), (*C.int)(unsafe.Pointer(&params[0])), C.int(len(params))) {
err = fmt.Errorf("Error opening device: %d with api backend: %d", device, apiPreference)
}

return
}

// Close VideoCapture object.
func (v *VideoCapture) Close() error {
C.VideoCapture_Close(v.p)
Expand Down Expand Up @@ -491,6 +524,9 @@ func OpenVideoCapture(v interface{}) (*VideoCapture, error) {
}
}

// OpenVideoCaptureWithAPI return VideoCapture specified by device ID if v is a
// number. Return VideoCapture created from video file, URL, or GStreamer
// pipeline if v is a string.
func OpenVideoCaptureWithAPI(v interface{}, apiPreference VideoCaptureAPI) (*VideoCapture, error) {
switch vv := v.(type) {
case int:
Expand All @@ -505,3 +541,22 @@ func OpenVideoCaptureWithAPI(v interface{}, apiPreference VideoCaptureAPI) (*Vid
return nil, errors.New("argument must be int or string")
}
}

// OpenVideoCaptureWithAPIParams return VideoCapture specified by device ID if v is a
// number. Return VideoCapture created from video file, URL, or GStreamer
// pipeline if v is a string.
func OpenVideoCaptureWithAPIParams(v interface{}, apiPreference VideoCaptureAPI, params []VideoCaptureProperties) (*VideoCapture, error) {
switch vv := v.(type) {
case int:
return VideoCaptureDeviceWithAPIParams(vv, apiPreference, params)
case string:
id, err := strconv.Atoi(vv)
if err == nil {
return VideoCaptureDeviceWithAPIParams(id, apiPreference, params)
}
//TODO: params with files
return VideoCaptureFileWithAPIParams(vv, apiPreference, params)
default:
return nil, errors.New("argument must be int or string")
}
}
2 changes: 2 additions & 0 deletions videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ VideoCapture VideoCapture_New();
void VideoCapture_Close(VideoCapture v);
bool VideoCapture_Open(VideoCapture v, const char* uri);
bool VideoCapture_OpenWithAPI(VideoCapture v, const char* uri, int apiPreference);
bool VideoCapture_OpenWithAPIParams(VideoCapture v, const char* uri, int apiPreference, int *paramsv, int paramsc);
bool VideoCapture_OpenDevice(VideoCapture v, int device);
bool VideoCapture_OpenDeviceWithAPI(VideoCapture v, int device, int apiPreference);
bool VideoCapture_OpenDeviceWithAPIParams(VideoCapture v, int device, int apiPreference, int *paramsv, int paramsc);
void VideoCapture_Set(VideoCapture v, int prop, double param);
double VideoCapture_Get(VideoCapture v, int prop);
int VideoCapture_IsOpened(VideoCapture v);
Expand Down
12 changes: 12 additions & 0 deletions videoio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ func TestVideoCaptureWithAPI(t *testing.T) {
})
}

func TestVideoCaptureWithAPIParams(t *testing.T) {
vc, _ := OpenVideoCaptureWithAPIParams(0, VideoCaptureAny, []VideoCaptureProperties{VideoCaptureHWAcceleration, 1, VideoCaptureHWDevice, 0})
defer vc.Close()

}

func TestVideoCaptureFileWithAPIParams(t *testing.T) {
vc, _ := OpenVideoCaptureWithAPIParams("images/small.mp4", VideoCaptureAny, []VideoCaptureProperties{VideoCaptureHWAcceleration, 1, VideoCaptureHWDevice, 0})
defer vc.Close()

}

func TestVideoCaptureFile(t *testing.T) {
vc, err := VideoCaptureFile("images/small.mp4")
defer vc.Close()
Expand Down

0 comments on commit 1dce353

Please sign in to comment.