Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt6 Porting guidance: construct QVideoFrame from QImage



  • it took me a long time to figure this out so i thought i'd share, so this is searchable in the future:

    if anyone knows how to do this without performing the extra copy, i'd love to know. (ie: move the bits directly from the QImage into video memory, without going through the staging area. eg: am i just missing a QVideoFrame API that i don't know about?

    static
    QVideoFrame		QVideoFrame_fromImage(const QImage& image)
    {
    	QVideoFrameFormat	frameFormat(
    		image.size(),
    		QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
    	QVideoFrame		vidFrame(frameFormat); vidFrame.map(QVideoFrame::WriteOnly);
    	qsizetype		image_rowbytesI(image.bytesPerLine());
    	qsizetype		frame_rowbytesI(vidFrame.bytesPerLine());
    	const uchar*		imageBitsP(image.bits());
    	uchar*			frameBitsP(vidFrame.bits());
    	int			maxRowY(image.size().height());
    
    	CF_ASSERT(image_rowbytesI <= frame_rowbytesI);
    
    	for (int rowY = 0; rowY < maxRowY; ++rowY) {
    		std::copy(imageBitsP, imageBitsP + image_rowbytesI, frameBitsP);
    		imageBitsP += image_rowbytesI;
    		frameBitsP += frame_rowbytesI;
    	}
    
    	vidFrame.unmap();
    	return vidFrame;
    }
    


  • the correct answer is posted above, but a question remains: is this the most efficient method?



  • the correct answer is posted above, but a question remains: is this the most efficient method?


Log in to reply