diff --git a/videoio.cpp b/videoio.cpp index 2fca2ac4..4be600e1 100644 --- a/videoio.cpp +++ b/videoio.cpp @@ -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); } @@ -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); } diff --git a/videoio.go b/videoio.go index c9b319aa..c14089b3 100644 --- a/videoio.go +++ b/videoio.go @@ -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. @@ -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(¶ms[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) { @@ -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()} @@ -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(¶ms[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) @@ -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: @@ -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") + } +} diff --git a/videoio.h b/videoio.h index 743c026f..2dd1c109 100644 --- a/videoio.h +++ b/videoio.h @@ -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); diff --git a/videoio_test.go b/videoio_test.go index 0a3501d6..34002bc1 100644 --- a/videoio_test.go +++ b/videoio_test.go @@ -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()