Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
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
10namespace {
11
12using PixelFormat = QVideoFrameFormat::PixelFormat;
13using ColorRange = QVideoFrameFormat::ColorRange;
14
15// clang-format off
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 },
29
30 // The cases with kCMVideoCodecType_JPEG/kCMVideoCodecType_JPEG_OpenDML as cv pixel format should be investigated.
31 // Matching kCMVideoCodecType_JPEG_OpenDML to ColorRange_Full is a little hack to distinguish between
32 // kCMVideoCodecType_JPEG and kCMVideoCodecType_JPEG_OpenDML.
33 { kCMVideoCodecType_JPEG, PixelFormat::Format_Jpeg, ColorRange::ColorRange_Unknown },
34 { kCMVideoCodecType_JPEG_OpenDML, PixelFormat::Format_Jpeg, ColorRange::ColorRange_Full }
35};
36// clang-format on
37
38template<typename Type, typename... Args>
39Type findInPixelFormatMap(Type defaultValue, Args... args)
40{
41 auto checkElement = [&](const auto &element) {
42 return ((args == std::get<Args>(element)) && ...);
43 };
44
45 auto found = std::find_if(std::begin(PixelFormatMap), std::end(PixelFormatMap), checkElement);
46 return found == std::end(PixelFormatMap) ? defaultValue : std::get<Type>(*found);
47}
48
49}
50
52{
53 return findInPixelFormatMap(ColorRange::ColorRange_Unknown, cvPixelFormat);
54}
55
57{
58 return findInPixelFormatMap(PixelFormat::Format_Invalid, cvPixelFormat);
59}
60
61CvPixelFormat QAVFHelpers::toCVPixelFormat(PixelFormat pixelFmt, ColorRange colorRange)
62{
63 return findInPixelFormatMap(CvPixelFormatInvalid, pixelFmt, colorRange);
64}
65
67{
68 auto cvPixelFormat = CVPixelBufferGetPixelFormatType(buffer);
69 auto pixelFormat = fromCVPixelFormat(cvPixelFormat);
70 if (openGL) {
71 if (cvPixelFormat == kCVPixelFormatType_32BGRA)
73 else
74 qWarning() << "Accelerated macOS OpenGL video supports BGRA only, got CV pixel format"
75 << cvPixelFormat;
76 }
77
78 size_t width = CVPixelBufferGetWidth(buffer);
79 size_t height = CVPixelBufferGetHeight(buffer);
80
82
85
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)) {
96 }
97 }
98 }
99
100 if (CFStringRef cTransfer = reinterpret_cast<CFStringRef>(
101 CVBufferGetAttachment(buffer, kCVImageBufferTransferFunctionKey, nullptr))) {
102
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));
112 double g;
113 CFNumberGetValue(gamma, kCFNumberFloat32Type, &g);
114 // These are best fit values given what we have in our enum
115 if (g < 0.8)
116 ; // unknown
117 else if (g < 1.5)
119 else if (g < 2.1)
121 else if (g < 2.5)
123 else if (g < 3.2)
125 }
126 if (@available(macOS 10.12, iOS 11.0, *)) {
127 if (CFEqual(cTransfer, kCVImageBufferTransferFunction_ITU_R_2020))
129 }
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)) {
135 }
136 }
137 }
138
139 format.setColorRange(colorRangeForCVPixelFormat(cvPixelFormat));
140 format.setColorSpace(colorSpace);
141 format.setColorTransfer(colorTransfer);
142 return format;
143}
\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.
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)
unsigned CvPixelFormat
constexpr CvPixelFormat CvPixelFormatInvalid
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei height
GLenum GLuint buffer
GLint GLsizei width
GLboolean GLboolean g
GLint GLsizei GLsizei GLenum format
QJSValueList args
Definition moc.h:23