-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All the reverse-engineering work comes from AntonioND [1]. A new video backend API has been added to grab video images. A corresponding implementation using OpenCV is also part of this commit, but other implementation should be possible (GStreamer for instance ?). With the OpenCV backend, the video device selection can be done using the Core parameter: [Core] GbCameraVideoDevice=<my_device> Where <my_device> can be either an integer which represent the device number (0 for default) or a string which specify the video device path. Tested with 64DD Mario Talent Studio (Japan), a transfer pak plugged in the first controller with a Japanese GameBoy camera. Also since the core currently requires a cart ROM (even if should strictly be required) I used Perfect Dark (Japan) to allow using the Transfer Pak. This is a core/ui limitation not related to this PR. [1] https://github.com/AntonioND/gbcam-rev-engineer
- Loading branch information
Showing
9 changed files
with
649 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* Mupen64plus - video_backend.h * | ||
* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * | ||
* Copyright (C) 2017 Bobby Smiles * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program; if not, write to the * | ||
* Free Software Foundation, Inc., * | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * | ||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
#ifndef M64P_BACKENDS_API_VIDEO_BACKEND_H | ||
#define M64P_BACKENDS_API_VIDEO_BACKEND_H | ||
|
||
#include "api/m64p_types.h" | ||
|
||
struct video_input_backend_interface | ||
{ | ||
/* Open a video stream with following properties (width, height) | ||
* Returns M64ERR_SUCCESS on success. | ||
*/ | ||
m64p_error (*open)(void* vin, unsigned int width, unsigned int height); | ||
|
||
/* Close a previsouly opened video stream. | ||
*/ | ||
void (*close)(void* vin); | ||
|
||
/* Grab a BGR image on an open stream | ||
* Returns M64ERR_SUCCESS on success. | ||
*/ | ||
m64p_error (*grab_image)(void* vin, void* data); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* Mupen64plus - opencv_video_backend.h * | ||
* Mupen64Plus homepage: https://mupen64plus.org/ * | ||
* Copyright (C) 2017 Bobby Smiles * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program; if not, write to the * | ||
* Free Software Foundation, Inc., * | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * | ||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
|
||
#include <opencv2/core.hpp> | ||
#include <opencv2/imgproc.hpp> | ||
#include <opencv2/videoio.hpp> | ||
#include <opencv2/highgui.hpp> | ||
|
||
#include <cstdlib> | ||
|
||
extern "C" | ||
{ | ||
#include "backends/opencv_video_backend.h" | ||
#include "backends/api/video_backend.h" | ||
|
||
#define M64P_CORE_PROTOTYPES 1 | ||
#include "api/callbacks.h" | ||
#include "api/m64p_types.h" | ||
#include "main/util.h" | ||
|
||
/* Implements video_input_backend. | ||
* Needs device field to be set before calling. | ||
*/ | ||
static m64p_error opencv_video_open(void* vin, unsigned int width, unsigned int height) | ||
{ | ||
try { | ||
int dev_num; | ||
struct opencv_video_backend* b = static_cast<struct opencv_video_backend*>(vin); | ||
|
||
/* allocate memory for cv::VideoCapture */ | ||
cv::VideoCapture* cap = static_cast<cv::VideoCapture*>(std::malloc(sizeof(cv::VideoCapture))); | ||
if (cap == NULL) { | ||
DebugMessage(M64MSG_ERROR, "Failed to allocated memory for video device %s", b->device); | ||
return M64ERR_NO_MEMORY; | ||
} | ||
|
||
/* placement new to call cv::VideoCapture constructor */ | ||
new(cap)cv::VideoCapture(); | ||
|
||
|
||
/* open device (we support both device number or path */ | ||
if (string_to_int(b->device, &dev_num)) { | ||
cap->open(dev_num); | ||
} | ||
else { | ||
cap->open(b->device); | ||
} | ||
|
||
if (!cap->isOpened()) { | ||
DebugMessage(M64MSG_ERROR, "Failed to open video device %s", b->device); | ||
std::free(cap); | ||
return M64ERR_SYSTEM_FAIL; | ||
} | ||
|
||
/* TODO: adapt capture resolution to the desired resolution */ | ||
|
||
DebugMessage(M64MSG_INFO, "Video successfully opened: %s", b->device); | ||
|
||
b->cap = cap; | ||
b->width = width; | ||
b->height = height; | ||
|
||
return M64ERR_SUCCESS; | ||
} | ||
/* C++ exception must not cross C-API boundaries */ | ||
catch(...) { return M64ERR_INTERNAL; } | ||
} | ||
|
||
static void opencv_video_close(void* vin) | ||
{ | ||
try { | ||
struct opencv_video_backend* b = static_cast<struct opencv_video_backend*>(vin); | ||
|
||
if (b->cap != NULL) { | ||
/* explicit call to cv::VideoCapture destructor */ | ||
static_cast<cv::VideoCapture*>(b->cap)->~VideoCapture(); | ||
|
||
/* free allocated memory */ | ||
std::free(b->cap); | ||
b->cap = NULL; | ||
} | ||
|
||
if (b->device != NULL) { | ||
std::free(b->device); | ||
b->device = NULL; | ||
} | ||
|
||
DebugMessage(M64MSG_INFO, "Video closed"); | ||
} | ||
/* C++ exception must not cross C-API boundaries */ | ||
catch(...) { return; } | ||
} | ||
|
||
static m64p_error opencv_grab_image(void* vin, void* data) | ||
{ | ||
try { | ||
struct opencv_video_backend* b = static_cast<struct opencv_video_backend*>(vin); | ||
cv::VideoCapture* cap = static_cast<cv::VideoCapture*>(b->cap); | ||
|
||
/* read next frame */ | ||
cv::Mat frame; | ||
if (cap == NULL || !cap->read(frame)) { | ||
DebugMessage(M64MSG_ERROR, "Failed to grab frame !"); | ||
return M64ERR_SYSTEM_FAIL; | ||
} | ||
|
||
/* resize image to desired resolution */ | ||
cv::Mat output = cv::Mat(b->height, b->width, CV_8UC3, data); | ||
cv::resize(frame, output, output.size(), 0, 0, cv::INTER_AREA); | ||
|
||
return M64ERR_SUCCESS; | ||
} | ||
/* C++ exception must not cross C-API boundaries */ | ||
catch(...) { return M64ERR_INTERNAL; } | ||
} | ||
|
||
|
||
#if 0 | ||
void cv_imshow(const char* name, unsigned int width, unsigned int height, int channels, void* data) | ||
{ | ||
try { | ||
int type = (channels == 1) ? CV_8UC1 : CV_8UC3; | ||
|
||
cv::Mat frame = cv::Mat(height, width, type, data); | ||
cv::imshow(name, frame); | ||
cv::waitKey(1); | ||
} | ||
/* C++ exception must not cross C-API boundaries */ | ||
catch(...) { return; } | ||
} | ||
#endif | ||
|
||
const struct video_input_backend_interface g_iopencv_video_input_backend = | ||
{ | ||
opencv_video_open, | ||
opencv_video_close, | ||
opencv_grab_image | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
* Mupen64plus - opencv_video_backend.h * | ||
* Mupen64Plus homepage: https://mupen64plus.org/ * | ||
* Copyright (C) 2017 Bobby Smiles * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program; if not, write to the * | ||
* Free Software Foundation, Inc., * | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * | ||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
#ifndef M64P_BACKENDS_OPENCV_VIDEO_BACKEND_H | ||
#define M64P_BACKENDS_OPENCV_VIDEO_BACKEND_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "backends/api/video_backend.h" | ||
|
||
struct opencv_video_backend | ||
{ | ||
char* device; | ||
unsigned int width; | ||
unsigned int height; | ||
|
||
/* using void* to avoid leaking C++ stuff in this header */ | ||
void* cap; | ||
}; | ||
|
||
#if 0 | ||
void cv_imshow(const char* name, unsigned int width, unsigned int height, int channels, void* data); | ||
#endif | ||
|
||
extern const struct video_input_backend_interface g_iopencv_video_input_backend; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
Oops, something went wrong.