Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
avfvideobuffer.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qavfhelpers_p.h>
7#include <QtMultimedia/private/qvideotexturehelper_p.h>
8#include <QtGui/qopenglcontext.h>
9#include <QtGui/rhi/qrhi.h>
10#include <QtCore/qloggingcategory.h>
11
12#include <CoreVideo/CVMetalTexture.h>
13#include <CoreVideo/CVMetalTextureCache.h>
14
15#import <AVFoundation/AVFoundation.h>
16#import <Metal/Metal.h>
17
18QT_USE_NAMESPACE
19
20Q_STATIC_LOGGING_CATEGORY(qLcVideoBuffer, "qt.multimedia.darwin.videobuffer")
21
22AVFVideoBuffer::AVFVideoBuffer(AVFVideoSinkInterface *sink, QCFType<CVImageBufferRef> buffer)
23 : QHwVideoBuffer(sink->rhi() ? QVideoFrame::RhiTextureHandle : QVideoFrame::NoHandle),
24#if QT_CONFIG(opengl)
25 sink(sink),
26#endif
27 m_buffer(std::move(buffer))
28{
29 const bool rhiIsOpenGL = sink && sink->rhi() && sink->rhi()->backend() == QRhi::OpenGLES2;
30 m_format = QAVFHelpers::videoFormatForImageBuffer(m_buffer, rhiIsOpenGL);
31
32 if (sink && sink->rhi() && sink->rhi()->backend() == QRhi::Metal)
33 metalCache = sink->cvMetalTextureCache;
34}
35
37{
38 Q_ASSERT(m_mode == QVideoFrame::NotMapped);
39}
40
41AVFVideoBuffer::MapData AVFVideoBuffer::map(QVideoFrame::MapMode mode)
42{
43 MapData mapData;
44
45 if (m_mode == QVideoFrame::NotMapped) {
46 CVPixelBufferLockBaseAddress(m_buffer, mode == QVideoFrame::ReadOnly
47 ? kCVPixelBufferLock_ReadOnly
48 : 0);
49 m_mode = mode;
50 }
51
52 mapData.planeCount = CVPixelBufferGetPlaneCount(m_buffer);
53 Q_ASSERT(mapData.planeCount <= 3);
54
55 if (!mapData.planeCount) {
56 // single plane
57 mapData.bytesPerLine[0] = CVPixelBufferGetBytesPerRow(m_buffer);
58 mapData.data[0] = static_cast<uchar*>(CVPixelBufferGetBaseAddress(m_buffer));
59 mapData.dataSize[0] = CVPixelBufferGetDataSize(m_buffer);
60 mapData.planeCount = mapData.data[0] ? 1 : 0;
61 return mapData;
62 }
63
64 // For a bi-planar or tri-planar format we have to set the parameters correctly:
65 for (int i = 0; i < mapData.planeCount; ++i) {
66 mapData.bytesPerLine[i] = CVPixelBufferGetBytesPerRowOfPlane(m_buffer, i);
67 mapData.dataSize[i] = mapData.bytesPerLine[i]*CVPixelBufferGetHeightOfPlane(m_buffer, i);
68 mapData.data[i] = static_cast<uchar*>(CVPixelBufferGetBaseAddressOfPlane(m_buffer, i));
69 }
70
71 return mapData;
72}
73
75{
76 if (m_mode != QVideoFrame::NotMapped) {
77 CVPixelBufferUnlockBaseAddress(m_buffer, m_mode == QVideoFrame::ReadOnly
78 ? kCVPixelBufferLock_ReadOnly
79 : 0);
80 m_mode = QVideoFrame::NotMapped;
81 }
82}
83
84static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
85{
86 switch (f) {
87 default:
88 case QRhiTexture::UnknownFormat:
89 return MTLPixelFormatInvalid;
90 case QRhiTexture::RGBA8:
91 return MTLPixelFormatRGBA8Unorm;
92 case QRhiTexture::BGRA8:
93 return MTLPixelFormatBGRA8Unorm;
94 case QRhiTexture::R8:
95 case QRhiTexture::RED_OR_ALPHA8:
96 return MTLPixelFormatR8Unorm;
97 case QRhiTexture::RG8:
98 return MTLPixelFormatRG8Unorm;
99 case QRhiTexture::R16:
100 return MTLPixelFormatR16Unorm;
101 case QRhiTexture::RG16:
102 return MTLPixelFormatRG16Unorm;
103 case QRhiTexture::RGBA16F:
104 return MTLPixelFormatRGBA16Float;
105 case QRhiTexture::RGBA32F:
106 return MTLPixelFormatRGBA32Float;
107 case QRhiTexture::R16F:
108 return MTLPixelFormatR16Float;
109 case QRhiTexture::R32F:
110 return MTLPixelFormatR32Float;
111 }
112}
113
114
116{
117 auto *textureDescription = QVideoTextureHelper::textureDescription(m_format.pixelFormat());
118 int bufferPlanes = CVPixelBufferGetPlaneCount(m_buffer);
119 if (plane > 0 && plane >= bufferPlanes)
120 return 0;
121
122 if (rhi.backend() == QRhi::Metal) {
123 if (!cvMetalTexture[plane]) {
124 size_t width = CVPixelBufferGetWidth(m_buffer);
125 size_t height = CVPixelBufferGetHeight(m_buffer);
126 QSize planeSize = textureDescription->rhiPlaneSize(QSize(width, height), plane, &rhi);
127
128 if (!metalCache)
129 return 0;
130
131 // Create a CoreVideo pixel buffer backed Metal texture image from the texture cache.
132 const auto pixelFormat = rhiTextureFormatToMetalFormat(textureDescription->rhiTextureFormat(plane, &rhi));
133 if (pixelFormat != MTLPixelFormatInvalid) {
134 // Passing invalid pixel format makes Metal API validation
135 // to crash (and also makes no sense at all).
136 auto ret = CVMetalTextureCacheCreateTextureFromImage(
137 kCFAllocatorDefault,
138 metalCache,
139 m_buffer, nil,
140 pixelFormat,
141 planeSize.width(), planeSize.height(),
142 plane,
143 &cvMetalTexture[plane]);
144
145 if (ret != kCVReturnSuccess)
146 qCWarning(qLcVideoBuffer) << "texture creation failed" << ret;
147 } else {
148 qCWarning(qLcVideoBuffer) << "requested invalid pixel format:"
149 << textureDescription->rhiTextureFormat(plane, &rhi);
150 }
151 }
152
153 return cvMetalTexture[plane] ? quint64(CVMetalTextureGetTexture(cvMetalTexture[plane])) : 0;
154 } else if (rhi.backend() == QRhi::OpenGLES2) {
155#if QT_CONFIG(opengl)
156#ifdef Q_OS_MACOS
157 CVOpenGLTextureCacheFlush(sink->cvOpenGLTextureCache, 0);
158 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
159 const CVReturn cvret = CVOpenGLTextureCacheCreateTextureFromImage(
160 kCFAllocatorDefault,
161 sink->cvOpenGLTextureCache,
162 m_buffer,
163 nil,
164 &cvOpenGLTexture);
165 if (cvret != kCVReturnSuccess)
166 qCWarning(qLcVideoBuffer) << "OpenGL texture creation failed" << cvret;
167
168 Q_ASSERT(CVOpenGLTextureGetTarget(cvOpenGLTexture) == GL_TEXTURE_RECTANGLE);
169 // Get an OpenGL texture name from the CVPixelBuffer-backed OpenGL texture image.
170 return CVOpenGLTextureGetName(cvOpenGLTexture);
171#endif
172#ifdef Q_OS_IOS
173 CVOpenGLESTextureCacheFlush(sink->cvOpenGLESTextureCache, 0);
174 // Create a CVPixelBuffer-backed OpenGL texture image from the texture cache.
175 const CVReturn cvret = CVOpenGLESTextureCacheCreateTextureFromImage(
176 kCFAllocatorDefault,
177 sink->cvOpenGLESTextureCache,
178 m_buffer,
179 nil,
180 GL_TEXTURE_2D,
181 GL_RGBA,
182 CVPixelBufferGetWidth(m_buffer),
183 CVPixelBufferGetHeight(m_buffer),
184 GL_RGBA,
185 GL_UNSIGNED_BYTE,
186 0,
187 &cvOpenGLESTexture);
188 if (cvret != kCVReturnSuccess)
189 qCWarning(qLcVideoBuffer) << "OpenGL ES texture creation failed" << cvret;
190
191 // Get an OpenGL texture name from the CVPixelBuffer-backed OpenGL texture image.
192 return CVOpenGLESTextureGetName(cvOpenGLESTexture);
193#endif
194#endif
195 }
196 return 0;
197}
static MTLPixelFormat rhiTextureFormatToMetalFormat(QRhiTexture::Format f)
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
~AVFVideoBuffer() override
quint64 textureHandle(QRhi &, int plane) override
void unmap() override
Releases the memory mapped by the map() function.