4#include <CoreMedia/CMFormatDescription.h>
5#include <CoreVideo/CoreVideo.h>
8#import <CoreVideo/CoreVideo.h>
16constexpr std::tuple<CvPixelFormat, PixelFormat, ColorRange> PixelFormatMap[] = {
17 { kCVPixelFormatType_32ARGB, PixelFormat::Format_ARGB8888, ColorRange::ColorRange_Unknown },
18 { kCVPixelFormatType_32BGRA, PixelFormat::Format_BGRA8888, ColorRange::ColorRange_Unknown },
19 { kCVPixelFormatType_420YpCbCr8Planar, PixelFormat::Format_YUV420P, ColorRange::ColorRange_Unknown },
20 { kCVPixelFormatType_420YpCbCr8PlanarFullRange, PixelFormat::Format_YUV420P, ColorRange::ColorRange_Full },
21 { kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, PixelFormat::Format_NV12, ColorRange::ColorRange_Video },
22 { kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, PixelFormat::Format_NV12, ColorRange::ColorRange_Full },
23 { kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, PixelFormat::Format_P010, ColorRange::ColorRange_Video },
24 { kCVPixelFormatType_420YpCbCr10BiPlanarFullRange, PixelFormat::Format_P010, ColorRange::ColorRange_Full },
25 { kCVPixelFormatType_422YpCbCr8, PixelFormat::Format_UYVY, ColorRange::ColorRange_Video },
26 { kCVPixelFormatType_422YpCbCr8_yuvs, PixelFormat::Format_YUYV, ColorRange::ColorRange_Video },
27 { kCVPixelFormatType_OneComponent8, PixelFormat::Format_Y8, ColorRange::ColorRange_Unknown },
28 { kCVPixelFormatType_OneComponent16, PixelFormat::Format_Y16, ColorRange::ColorRange_Unknown },
33 { kCMVideoCodecType_JPEG, PixelFormat::Format_Jpeg, ColorRange::ColorRange_Unknown },
34 { kCMVideoCodecType_JPEG_OpenDML, PixelFormat::Format_Jpeg, ColorRange::ColorRange_Full }
38template<
typename Type,
typename... Args>
39Type findInPixelFormatMap(
Type defaultValue, Args...
args)
41 auto checkElement = [&](
const auto &element) {
42 return ((
args == std::get<Args>(element)) && ...);
45 auto found = std::find_if(std::begin(PixelFormatMap), std::end(PixelFormatMap), checkElement);
46 return found == std::end(PixelFormatMap) ? defaultValue : std::get<Type>(*found);
53 return findInPixelFormatMap(ColorRange::ColorRange_Unknown, cvPixelFormat);
58 return findInPixelFormatMap(PixelFormat::Format_Invalid, cvPixelFormat);
68 auto cvPixelFormat = CVPixelBufferGetPixelFormatType(
buffer);
71 if (cvPixelFormat == kCVPixelFormatType_32BGRA)
74 qWarning() <<
"Accelerated macOS OpenGL video supports BGRA only, got CV pixel format"
86 if (CFStringRef cSpace =
reinterpret_cast<CFStringRef
>(
87 CVBufferGetAttachment(
buffer, kCVImageBufferYCbCrMatrixKey,
nullptr))) {
88 if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_709_2)) {
90 }
else if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_601_4)
91 || CFEqual(cSpace, kCVImageBufferYCbCrMatrix_SMPTE_240M_1995)) {
93 }
else if (@available(macOS 10.11,
iOS 9.0, *)) {
94 if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_2020)) {
100 if (CFStringRef cTransfer =
reinterpret_cast<CFStringRef
>(
101 CVBufferGetAttachment(
buffer, kCVImageBufferTransferFunctionKey,
nullptr))) {
103 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_709_2)) {
105 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_240M_1995)) {
107 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_sRGB)) {
109 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_UseGamma)) {
110 auto gamma =
reinterpret_cast<CFNumberRef
>(
111 CVBufferGetAttachment(
buffer, kCVImageBufferGammaLevelKey,
nullptr));
113 CFNumberGetValue(gamma, kCFNumberFloat32Type, &
g);
126 if (@available(macOS 10.12,
iOS 11.0, *)) {
127 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2020))
130 if (@available(macOS 10.12,
iOS 11.0, *)) {
131 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2100_HLG)) {
133 }
else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ)) {
140 format.setColorSpace(colorSpace);
141 format.setColorTransfer(colorTransfer);
QVideoFrameFormat::ColorRange colorRangeForCVPixelFormat(CvPixelFormat cvPixelFormat)
QVideoFrameFormat::PixelFormat fromCVPixelFormat(CvPixelFormat cvPixelFormat)
CvPixelFormat toCVPixelFormat(QVideoFrameFormat::PixelFormat pixFmt, QVideoFrameFormat::ColorRange colorRange)
QVideoFrameFormat videoFormatForImageBuffer(CVImageBufferRef buffer, bool openGL=false)
constexpr CvPixelFormat CvPixelFormatInvalid
GLint GLsizei GLsizei height
GLint GLsizei GLsizei GLenum format