Merge pull request #23469 from gottagofaster236:use_nv12_for_obs_camera

This commit is contained in:
Alexander Alekhin 2023-04-10 13:39:15 +00:00
commit f9ce3f4b91

View File

@ -186,6 +186,7 @@ DEFINE_GUID(MEDIASUBTYPE_YV12,0x32315659,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,
DEFINE_GUID(MEDIASUBTYPE_YVU9,0x39555659,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71); DEFINE_GUID(MEDIASUBTYPE_YVU9,0x39555659,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(MEDIASUBTYPE_YVYU,0x55595659,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71); DEFINE_GUID(MEDIASUBTYPE_YVYU,0x55595659,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(MEDIASUBTYPE_MJPG,0x47504A4D, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); // MGB DEFINE_GUID(MEDIASUBTYPE_MJPG,0x47504A4D, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); // MGB
DEFINE_GUID(MEDIASUBTYPE_NV12,0x3231564e,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(MEDIATYPE_Interleaved,0x73766169,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71); DEFINE_GUID(MEDIATYPE_Interleaved,0x73766169,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(MEDIATYPE_Video,0x73646976,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71); DEFINE_GUID(MEDIATYPE_Video,0x73646976,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(PIN_CATEGORY_CAPTURE,0xfb6c4281,0x0353,0x11d1,0x90,0x5f,0x00,0x00,0xc0,0xcc,0x16,0xba); DEFINE_GUID(PIN_CATEGORY_CAPTURE,0xfb6c4281,0x0353,0x11d1,0x90,0x5f,0x00,0x00,0xc0,0xcc,0x16,0xba);
@ -338,7 +339,7 @@ static void DebugPrintOut(const char *format, ...)
//videoInput defines //videoInput defines
#define VI_VERSION 0.1995 #define VI_VERSION 0.1995
#define VI_MAX_CAMERAS 20 #define VI_MAX_CAMERAS 20
#define VI_NUM_TYPES 22 //MGB #define VI_NUM_TYPES 23 //MGB
#define VI_NUM_FORMATS 18 //DON'T TOUCH #define VI_NUM_FORMATS 18 //DON'T TOUCH
//defines for setPhyCon - tuner is not as well supported as composite and s-video //defines for setPhyCon - tuner is not as well supported as composite and s-video
@ -826,11 +827,14 @@ void videoDevice::setSize(int w, int h){
{ {
videoSize = w * h; videoSize = w * h;
} }
else if (pAmMediaType->subtype == MEDIASUBTYPE_Y16 || else if (pAmMediaType->subtype == MEDIASUBTYPE_Y16)
pAmMediaType->subtype == MEDIASUBTYPE_YUY2)
{ {
videoSize = w * h * 2; videoSize = w * h * 2;
} }
else if (pAmMediaType->subtype == MEDIASUBTYPE_NV12)
{
videoSize = w * h * 3 / 2;
}
else else
{ {
videoSize = w * h * 3; videoSize = w * h * 3;
@ -1109,6 +1113,7 @@ videoInput::videoInput(){
mediaSubtypes[19] = MEDIASUBTYPE_I420; mediaSubtypes[19] = MEDIASUBTYPE_I420;
mediaSubtypes[20] = MEDIASUBTYPE_BY8; mediaSubtypes[20] = MEDIASUBTYPE_BY8;
mediaSubtypes[21] = MEDIASUBTYPE_Y16; mediaSubtypes[21] = MEDIASUBTYPE_Y16;
mediaSubtypes[22] = MEDIASUBTYPE_NV12;
//The video formats we support //The video formats we support
formatTypes[VI_NTSC_M] = AnalogVideo_NTSC_M; formatTypes[VI_NTSC_M] = AnalogVideo_NTSC_M;
@ -1575,11 +1580,11 @@ bool videoInput::getPixels(int id, unsigned char * dstBuffer, bool flipRedAndBlu
processPixels(src, dst, width, height, flipRedAndBlue, flipImage, 2); processPixels(src, dst, width, height, flipRedAndBlue, flipImage, 2);
} }
} }
else if (VDList[id]->pAmMediaType->subtype == MEDIASUBTYPE_YUY2) else if (VDList[id]->pAmMediaType->subtype == MEDIASUBTYPE_NV12)
{ {
cv::Mat srcMat(height, width, CV_8UC2, src); cv::Mat srcMat(height * 3 / 2, width, CV_8UC1, src);
cv::Mat dstMat(height, width, CV_8UC3, dst); cv::Mat dstMat(height, width, CV_8UC3, dst);
cv::cvtColor(srcMat, dstMat, cv::COLOR_YUV2BGR_YUY2); cv::cvtColor(srcMat, dstMat, cv::COLOR_YUV2BGR_NV12);
} }
else else
{ {
@ -2331,6 +2336,7 @@ void videoInput::getMediaSubtypeAsString(GUID type, char * typeAsString){
else if(type == MEDIASUBTYPE_I420) snprintf(tmpStr, sizeof(tmpStr), "I420"); else if(type == MEDIASUBTYPE_I420) snprintf(tmpStr, sizeof(tmpStr), "I420");
else if (type == MEDIASUBTYPE_BY8) snprintf(tmpStr, sizeof(tmpStr), "BY8"); else if (type == MEDIASUBTYPE_BY8) snprintf(tmpStr, sizeof(tmpStr), "BY8");
else if (type == MEDIASUBTYPE_Y16) snprintf(tmpStr, sizeof(tmpStr), "Y16"); else if (type == MEDIASUBTYPE_Y16) snprintf(tmpStr, sizeof(tmpStr), "Y16");
else if (type == MEDIASUBTYPE_NV12) snprintf(tmpStr, sizeof(tmpStr), "NV12");
else snprintf(tmpStr, sizeof(tmpStr), "OTHER"); else snprintf(tmpStr, sizeof(tmpStr), "OTHER");
memcpy(typeAsString, tmpStr, sizeof(char)*8); memcpy(typeAsString, tmpStr, sizeof(char)*8);
@ -2768,9 +2774,9 @@ int videoInput::start(int deviceID, videoDevice *VD){
if (strcmp("OBS Virtual Camera", VD->nDeviceName) == 0) if (strcmp("OBS Virtual Camera", VD->nDeviceName) == 0)
{ {
// OBS Virtual Camera always returns S_OK on SetFormat(), even if it doesn't support // OBS Virtual Camera always returns S_OK on SetFormat(), even if it doesn't support
// the actual format. So we have to choose a format that it supports manually, e.g. YUY2. // the actual format. So we have to choose a format that it supports manually, e.g. NV12.
// https://github.com/opencv/opencv/issues/19746#issuecomment-1383056787 // https://github.com/opencv/opencv/issues/19746#issuecomment-1383056787
VD->tryVideoType = MEDIASUBTYPE_YUY2; VD->tryVideoType = MEDIASUBTYPE_NV12;
} }
char guidStr[8]; char guidStr[8];
@ -2885,7 +2891,7 @@ int videoInput::start(int deviceID, videoDevice *VD){
// Disable format conversion if using 8/16-bit data (e-Con systems) // Disable format conversion if using 8/16-bit data (e-Con systems)
if (checkSingleByteFormat(VD->pAmMediaType->subtype) || if (checkSingleByteFormat(VD->pAmMediaType->subtype) ||
(VD->pAmMediaType->subtype == MEDIASUBTYPE_Y16 || VD->pAmMediaType->subtype == MEDIASUBTYPE_YUY2)) (VD->pAmMediaType->subtype == MEDIASUBTYPE_Y16 || VD->pAmMediaType->subtype == MEDIASUBTYPE_NV12))
{ {
DebugPrintOut("SETUP: Not converting frames to RGB.\n"); DebugPrintOut("SETUP: Not converting frames to RGB.\n");
mt.subtype = VD->pAmMediaType->subtype; mt.subtype = VD->pAmMediaType->subtype;