Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qavfhelpers.mm
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include <qavfhelpers_p.h>
4#include <CoreMedia/CMFormatDescription.h>
5#include <CoreVideo/CoreVideo.h>
6#include <qdebug.h>
7
8#import <CoreVideo/CoreVideo.h>
9
11{
12 switch (avPixelFormat) {
13 case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
14 case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
15 case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
17 case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
18 case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
19 case kCVPixelFormatType_422YpCbCr8:
20 case kCVPixelFormatType_422YpCbCr8_yuvs:
22 default:
24 }
25}
26
28{
29 switch (avPixelFormat) {
30 case kCVPixelFormatType_32ARGB:
32 case kCVPixelFormatType_32BGRA:
34 case kCVPixelFormatType_420YpCbCr8Planar:
35 case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
37 case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
38 case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
40 case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
41 case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
43 case kCVPixelFormatType_422YpCbCr8:
45 case kCVPixelFormatType_422YpCbCr8_yuvs:
47 case kCVPixelFormatType_OneComponent8:
51
52 case kCMVideoCodecType_JPEG:
53 case kCMVideoCodecType_JPEG_OpenDML:
55 default:
57 }
58}
59
61{
62 switch (qtFormat) {
64 conv = kCVPixelFormatType_32ARGB;
65 break;
67 conv = kCVPixelFormatType_32BGRA;
68 break;
70 conv = kCVPixelFormatType_420YpCbCr8PlanarFullRange;
71 break;
73 conv = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
74 break;
76 conv = kCVPixelFormatType_420YpCbCr10BiPlanarFullRange;
77 break;
79 conv = kCVPixelFormatType_422YpCbCr8;
80 break;
82 conv = kCVPixelFormatType_422YpCbCr8_yuvs;
83 break;
85 conv = kCVPixelFormatType_OneComponent8;
86 break;
89 break;
90 default:
91 return false;
92 }
93
94 return true;
95}
96
98{
99 auto avPixelFormat = CVPixelBufferGetPixelFormatType(buffer);
100 auto pixelFormat = fromCVPixelFormat(avPixelFormat);
101 if (openGL) {
102 if (avPixelFormat == kCVPixelFormatType_32BGRA)
104 else
105 qWarning() << "Accelerated macOS OpenGL video supports BGRA only, got CV pixel format" << avPixelFormat;
106 }
107
108 size_t width = CVPixelBufferGetWidth(buffer);
109 size_t height = CVPixelBufferGetHeight(buffer);
110
111 QVideoFrameFormat format(QSize(width, height), pixelFormat);
112
114 auto colorTransfer = QVideoFrameFormat::ColorTransfer_Unknown;
115
116 if (CFStringRef cSpace = reinterpret_cast<CFStringRef>(
117 CVBufferGetAttachment(buffer, kCVImageBufferYCbCrMatrixKey, nullptr))) {
118 if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_709_2)) {
120 } else if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_601_4)
121 || CFEqual(cSpace, kCVImageBufferYCbCrMatrix_SMPTE_240M_1995)) {
123 } else if (@available(macOS 10.11, iOS 9.0, *)) {
124 if (CFEqual(cSpace, kCVImageBufferYCbCrMatrix_ITU_R_2020)) {
126 }
127 }
128 }
129
130 if (CFStringRef cTransfer = reinterpret_cast<CFStringRef>(
131 CVBufferGetAttachment(buffer, kCVImageBufferTransferFunctionKey, nullptr))) {
132
133 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_709_2)) {
135 } else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_240M_1995)) {
137 } else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_sRGB)) {
139 } else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_UseGamma)) {
140 auto gamma = reinterpret_cast<CFNumberRef>(
141 CVBufferGetAttachment(buffer, kCVImageBufferGammaLevelKey, nullptr));
142 double g;
143 CFNumberGetValue(gamma, kCFNumberFloat32Type, &g);
144 // These are best fit values given what we have in our enum
145 if (g < 0.8)
146 ; // unknown
147 else if (g < 1.5)
149 else if (g < 2.1)
151 else if (g < 2.5)
153 else if (g < 3.2)
155 }
156 if (@available(macOS 10.12, iOS 11.0, *)) {
157 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2020))
159 }
160 if (@available(macOS 10.12, iOS 11.0, *)) {
161 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2100_HLG)) {
163 } else if (CFEqual(cTransfer, kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ)) {
165 }
166 }
167 }
168
169 format.setColorRange(colorRangeForPixelFormat(avPixelFormat));
170 format.setColorSpace(colorSpace);
171 format.setColorTransfer(colorTransfer);
172 return format;
173}
\inmodule QtCore
Definition qsize.h:25
The QVideoFrameFormat class specifies the stream format of a video presentation surface.
PixelFormat
Enumerates video data types.
ColorRange
Describes the color range used by the video data.
bool toCVPixelFormat(QVideoFrameFormat::PixelFormat qtFormat, unsigned &conv)
QVideoFrameFormat::PixelFormat fromCVPixelFormat(unsigned avPixelFormat)
QVideoFrameFormat videoFormatForImageBuffer(CVImageBufferRef buffer, bool openGL=false)
static QVideoFrameFormat::ColorRange colorRangeForPixelFormat(unsigned avPixelFormat)
@ q_kCVPixelFormatType_OneComponent16
#define qWarning
Definition qlogging.h:162
GLint GLsizei GLsizei height
GLenum GLuint buffer
GLint GLsizei width
GLboolean GLboolean g
GLint GLsizei GLsizei GLenum format