69QVideoFrameFormat
QAVFHelpers::videoFormatForImageBuffer(CVImageBufferRef buffer,
bool openGL)
71 auto cvPixelFormat = CVPixelBufferGetPixelFormatType(buffer);
72 auto pixelFormat = fromCVPixelFormat(cvPixelFormat);
74 if (cvPixelFormat == kCVPixelFormatType_32BGRA)
75 pixelFormat = QVideoFrameFormat::Format_SamplerRect;
77 qWarning() <<
"Accelerated macOS OpenGL video supports BGRA only, got CV pixel format"
81 size_t width = CVPixelBufferGetWidth(buffer);
82 size_t height = CVPixelBufferGetHeight(buffer);
84 QVideoFrameFormat format(QSize(width, height), pixelFormat);
86 auto colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
87 auto colorTransfer = QVideoFrameFormat::ColorTransfer_Unknown;
90 QCFString(CVBufferCopyAttachment(buffer, kCVImageBufferYCbCrMatrixKey,
nullptr))) {
91 if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_709_2)) {
92 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
93 }
else if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_601_4)
94 || CFEqual(cSpace, kCVImageBufferYCbCrMatrix_SMPTE_240M_1995)) {
95 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
96 }
else if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_2020)) {
97 colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
101 if (
auto cTransfer = QCFString(
102 CVBufferCopyAttachment(buffer, kCVImageBufferTransferFunctionKey,
nullptr))) {
104 using QCFNumber = QCFType<CFNumberRef>;
106 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_709_2)) {
107 colorTransfer = QVideoFrameFormat::ColorTransfer_BT709;
108 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_240M_1995)) {
109 colorTransfer = QVideoFrameFormat::ColorTransfer_BT601;
110 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_sRGB)) {
111 colorTransfer = QVideoFrameFormat::ColorTransfer_Gamma22;
112 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_UseGamma)) {
114 QCFNumber(CVBufferCopyAttachment(buffer, kCVImageBufferGammaLevelKey,
nullptr));
116 CFNumberGetValue(gamma, kCFNumberFloat32Type, &g);
121 colorTransfer = QVideoFrameFormat::ColorTransfer_Linear;
123 colorTransfer = QVideoFrameFormat::ColorTransfer_BT709;
125 colorTransfer = QVideoFrameFormat::ColorTransfer_Gamma22;
127 colorTransfer = QVideoFrameFormat::ColorTransfer_Gamma28;
128 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2020)) {
129 colorTransfer = QVideoFrameFormat::ColorTransfer_BT709;
130 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2100_HLG)) {
131 colorTransfer = QVideoFrameFormat::ColorTransfer_STD_B67;
132 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ)) {
133 colorTransfer = QVideoFrameFormat::ColorTransfer_ST2084;
137 format.setColorRange(colorRangeForCVPixelFormat(cvPixelFormat));
138 format.setColorSpace(colorSpace);
139 format.setColorTransfer(colorTransfer);