examples/Qt/common/GLBufferQVideoSurface.cpp
/*
QVideoSurfaceFormat GLBufferQVideoSurface::nearestFormat(const QVideoSurfaceFormat & inFmt) const	{
	return inFmt;
}
*/
QVideoSurfaceFormat GLBufferQVideoSurface::nearestFormat(const QVideoSurfaceFormat & inFmt) const       {
return inFmt;
}
QVideoSurfaceFormat GLBufferQVideoSurface :: nearestFormat ( const QVideoSurfaceFormat & inFmt ) const { return inFmt ; }
bool GLBufferQVideoSurface::present(const QVideoFrame & inFrame)	{
	if (uploader == nullptr)	{
		cout << "ERR: no uploader, " << __PRETTY_FUNCTION__ << endl;
		return false;
	}
	GLBufferPoolRef		bp = yuvSwizzleScene->privatePool();
	if (bp == nullptr)	{
		cout << "ERR: no global buffer pool, " << __PRETTY_FUNCTION__ << endl;
		return false;
	}
	GLBufferRef		cpuBuffer = CreateCPUBufferForQVideoFrame(const_cast<QVideoFrame*>(&inFrame), bp);
	if (cpuBuffer == nullptr)	{
		cout << "ERR: cant make CPU buffer, " << __PRETTY_FUNCTION__ << endl;
		return false;
	}
	GLBufferRef		freshTexture = nullptr;
	GLBufferRef		texToEmit = nullptr;
	switch(cpuBuffer->desc.pixelFormat)	{
	case GLBuffer::PF_YCbCr_422:
		uploader->setSwapBytes(false);
		if (!native2vuySupport)	{
			GLBufferRef		fauxCPUBuffer = GLBufferCopy(cpuBuffer);
			fauxCPUBuffer->size = { ceil(fauxCPUBuffer->size.width/2.), fauxCPUBuffer->size.height };
			fauxCPUBuffer->backingSize = { ceil(fauxCPUBuffer->size.width/2.), fauxCPUBuffer->size.height };
			fauxCPUBuffer->srcRect = { 0, 0, ceil(fauxCPUBuffer->srcRect.size.width/2.), fauxCPUBuffer->size.height };
			fauxCPUBuffer->desc.internalFormat = GLBuffer::IF_RGBA8;
			fauxCPUBuffer->desc.pixelFormat = GLBuffer::PF_BGRA;
			fauxCPUBuffer->desc.pixelType = GLBuffer::PT_UByte;
			freshTexture = uploader->streamCPUToTex(fauxCPUBuffer,false);
		}
		else	{
			freshTexture = uploader->streamCPUToTex(cpuBuffer,false);
		}
		if (freshTexture != nullptr)	{
			if (!native2vuySupport)	{
				yuvSwizzleScene->setFilterInputBuffer(freshTexture);
				Size			tmpSize(freshTexture->size.width*2.0, freshTexture->size.height);
				GLBufferRef		swizzledTexture = yuvSwizzleScene->createAndRenderABuffer(tmpSize);
				if (swizzledTexture != nullptr)
					lastUploadedFrame = swizzledTexture;
			}
			else	{
				lastUploadedFrame = freshTexture;
			}
		}
		break;
	case GLBuffer::PF_RGBA:	
		{
			uploader->setSwapBytes(false);
			cpuBuffer->desc.internalFormat = GLBuffer::IF_RGBA8;
			cpuBuffer->desc.pixelFormat = GLBuffer::PF_BGRA;
			cpuBuffer->desc.pixelType = GLBuffer::PT_UByte;
			freshTexture = uploader->streamCPUToTex(cpuBuffer,false);
			if (freshTexture != nullptr)	{
				lastUploadedFrame = freshTexture;
			}
			else	{
			}
		}
		break;
	case GLBuffer::PF_BGRA:
		{
#if defined(Q_OS_WIN)
			uploader->setSwapBytes(false);
#elif defined(Q_OS_MAC)
			uploader->setSwapBytes(true);
#endif
			cpuBuffer->desc.internalFormat = GLBuffer::IF_RGBA8;
			cpuBuffer->desc.pixelFormat = GLBuffer::PF_BGRA;
			cpuBuffer->desc.pixelType = GLBuffer::PT_UByte;
			freshTexture = uploader->streamCPUToTex(cpuBuffer,false);
			if (freshTexture != nullptr)	{
				lastUploadedFrame = freshTexture;
			}
			else	{
			}
		}
		break;
	default:
		qDebug() << "ERR: unhandled case in " << __PRETTY_FUNCTION__;
		break;
	}
	if (freshTexture != nullptr)	{
		emit frameProduced(lastUploadedFrame);
	}
	return true;
}
bool GLBufferQVideoSurface :: present ( const QVideoFrame & inFrame ) { if ( uploader == nullptr ) { cout << " " << __PRETTY_FUNCTION__ << endl ; return false ; } GLBufferPoolRef bp = yuvSwizzleScene -> privatePool ( ) ; if ( bp == nullptr ) { cout << " " << __PRETTY_FUNCTION__ << endl ; return false ; } GLBufferRef cpuBuffer = CreateCPUBufferForQVideoFrame ( const_cast < QVideoFrame * > ( & inFrame ) , bp ) ; if ( cpuBuffer == nullptr ) { cout << " " << __PRETTY_FUNCTION__ << endl ; return false ; } GLBufferRef freshTexture = nullptr ; GLBufferRef texToEmit = nullptr ; switch ( cpuBuffer -> desc . pixelFormat ) { case GLBuffer :: PF_YCbCr_422 : uploader -> setSwapBytes ( false ) ; if ( ! native2vuySupport ) { GLBufferRef fauxCPUBuffer = GLBufferCopy ( cpuBuffer ) ; fauxCPUBuffer -> size = { ceil ( fauxCPUBuffer -> size . width / 2. ) , fauxCPUBuffer -> size . height } ; fauxCPUBuffer -> backingSize = { ceil ( fauxCPUBuffer -> size . width / 2. ) , fauxCPUBuffer -> size . height } ; fauxCPUBuffer -> srcRect = { 0 , 0 , ceil ( fauxCPUBuffer -> srcRect . size . width / 2. ) , fauxCPUBuffer -> size . height } ; fauxCPUBuffer -> desc . internalFormat = GLBuffer :: IF_RGBA8 ; fauxCPUBuffer -> desc . pixelFormat = GLBuffer :: PF_BGRA ; fauxCPUBuffer -> desc . pixelType = GLBuffer :: PT_UByte ; freshTexture = uploader -> streamCPUToTex ( fauxCPUBuffer , false ) ; } else { freshTexture = uploader -> streamCPUToTex ( cpuBuffer , false ) ; } if ( freshTexture != nullptr ) { if ( ! native2vuySupport ) { yuvSwizzleScene -> setFilterInputBuffer ( freshTexture ) ; Size tmpSize ( freshTexture -> size . width * 2.0 , freshTexture -> size . height ) ; GLBufferRef swizzledTexture = yuvSwizzleScene -> createAndRenderABuffer ( tmpSize ) ; if ( swizzledTexture != nullptr ) lastUploadedFrame = swizzledTexture ; } else { lastUploadedFrame = freshTexture ; } } break ; case GLBuffer :: PF_RGBA : { uploader -> setSwapBytes ( false ) ; cpuBuffer -> desc . internalFormat = GLBuffer :: IF_RGBA8 ; cpuBuffer -> desc . pixelFormat = GLBuffer :: PF_BGRA ; cpuBuffer -> desc . pixelType = GLBuffer :: PT_UByte ; freshTexture = uploader -> streamCPUToTex ( cpuBuffer , false ) ; if ( freshTexture != nullptr ) { lastUploadedFrame = freshTexture ; } else { } } break ; case GLBuffer :: PF_BGRA : { #if defined ( Q_OS_WIN ) uploader -> setSwapBytes ( false ) ; #elif defined ( Q_OS_MAC ) uploader -> setSwapBytes ( true ) ; #endif cpuBuffer -> desc . internalFormat = GLBuffer :: IF_RGBA8 ; cpuBuffer -> desc . pixelFormat = GLBuffer :: PF_BGRA ; cpuBuffer -> desc . pixelType = GLBuffer :: PT_UByte ; freshTexture = uploader -> streamCPUToTex ( cpuBuffer , false ) ; if ( freshTexture != nullptr ) { lastUploadedFrame = freshTexture ; } else { } } break ; default : qDebug ( ) << " " << __PRETTY_FUNCTION__ ; break ; } if ( freshTexture != nullptr ) { emit frameProduced ( lastUploadedFrame ) ; } return true ; }
QVideoSurfaceFormat GLBufferQVideoSurface::nearestFormat(const QVideoSurfaceFormat & inFmt) const       {
return inFmt;
}
QVideoSurfaceFormat GLBufferQVideoSurface :: nearestFormat ( const QVideoSurfaceFormat & inFmt ) const { return inFmt ; }
//qDebug() << __PRETTY_FUNCTION__ << "... " << inFrame; //auto			bp = GetGlobalBufferPool(); //cout << "\tcpuBuffer is " << *cpuBuffer << ", pixel format is " << cpuBuffer->desc.pixelFormat << endl; //cout << "\tPF_BGRA is " << GLBuffer::PF_BGRA << ", PF_RGBA is " << GLBuffer::PF_RGBA << ", PF_YCbCr_422 is " << GLBuffer::PF_YCbCr_422 << endl; //qDebug() << "\tYUV frame..."; //	if we're uploading the YCbCr tex as RGBA and then swizzling it on the GPU, adjust the cpu buffer accordingly //yuvSwizzleScene->setSize(Size(freshTexture->size.width*2.0, freshTexture->size.height)); //	not really RGBA- Qt vends xRGB, which we upload as BGRA and then swizzle to RGBA in a shader //qDebug() << "\tRGB frame..."; //GLBufferRef		fauxCPUBuffer = GLBufferCopy(cpuBuffer); //fauxCPUBuffer->desc.internalFormat = GLBuffer::IF_RGBA8; //fauxCPUBuffer->desc.pixelFormat = GLBuffer::PF_BGRA; //fauxCPUBuffer->desc.pixelType = GLBuffer::PT_UByte; //cout << "\tfreshTexture is " << *freshTexture << endl; //cout << "\tcpuBuffer flipped is " << cpuBuffer->flipped << ", freshTexture flipped is " << freshTexture->flipped << endl; //bgrSwizzleScene->setFilterInputBuffer(freshTexture); //GLBufferRef		swizzledTexture = bgrSwizzleScene->createAndRenderABuffer(freshTexture->size); //cout << "\tswizzledTexture is " << *swizzledTexture << endl; //cout << "\tfreshTexture was null\n"; //qDebug() << "\tBGR frame..."; //GLBufferRef		fauxCPUBuffer = GLBufferCopy(cpuBuffer); //fauxCPUBuffer->desc.internalFormat = GLBuffer::IF_RGBA8; //fauxCPUBuffer->desc.pixelFormat = GLBuffer::PF_BGRA; //fauxCPUBuffer->desc.pixelType = GLBuffer::PT_UByte; //cout << "\tfreshTexture is " << *freshTexture << endl; //cout << "\tcpuBuffer flipped is " << cpuBuffer->flipped << ", freshTexture flipped is " << freshTexture->flipped << endl; //bgrSwizzleScene->setFilterInputBuffer(freshTexture); //GLBufferRef		swizzledTexture = bgrSwizzleScene->createAndRenderABuffer(freshTexture->size); //cout << "\tswizzledTexture is " << *swizzledTexture << endl; //cout << "\tfreshTexture was null\n";