7#include <gst/video/gstvideoaffinetransformationmeta.h>
8#include <gst/video/gstvideometa.h>
9#include <gst/video/video-hdr.h>
12#include <QtCore/QtNumeric>
13#include <QtMultimedia/QAbstractVideoBuffer>
14#include <QtMultimedia/QVideoFrame>
21#if defined(QGC_HAS_ANY_GPU_PATH)
22#include <private/qhwvideobuffer_p.h>
30 case GST_VIDEO_COLOR_MATRIX_BT601:
31 return QVideoFrameFormat::ColorSpace_BT601;
32 case GST_VIDEO_COLOR_MATRIX_BT709:
33 return QVideoFrameFormat::ColorSpace_BT709;
34 case GST_VIDEO_COLOR_MATRIX_BT2020:
35 return QVideoFrameFormat::ColorSpace_BT2020;
36 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
38 return QVideoFrameFormat::ColorSpace_AdobeRgb;
42 return QVideoFrameFormat::ColorSpace_Undefined;
50 case GST_VIDEO_TRANSFER_BT601:
51 return QVideoFrameFormat::ColorTransfer_BT601;
52 case GST_VIDEO_TRANSFER_BT2020_10:
53 case GST_VIDEO_TRANSFER_BT2020_12:
54 case GST_VIDEO_TRANSFER_BT709:
55 return QVideoFrameFormat::ColorTransfer_BT709;
56 case GST_VIDEO_TRANSFER_GAMMA20:
57 return QVideoFrameFormat::ColorTransfer_BT709;
59 case GST_VIDEO_TRANSFER_SMPTE240M:
60 case GST_VIDEO_TRANSFER_GAMMA22:
61 case GST_VIDEO_TRANSFER_SRGB:
62 case GST_VIDEO_TRANSFER_ADOBERGB:
63 return QVideoFrameFormat::ColorTransfer_Gamma22;
64 case GST_VIDEO_TRANSFER_GAMMA18:
65 return QVideoFrameFormat::ColorTransfer_BT709;
66 case GST_VIDEO_TRANSFER_GAMMA28:
67 return QVideoFrameFormat::ColorTransfer_Gamma28;
68 case GST_VIDEO_TRANSFER_GAMMA10:
69 return QVideoFrameFormat::ColorTransfer_Linear;
70 case GST_VIDEO_TRANSFER_SMPTE2084:
71 return QVideoFrameFormat::ColorTransfer_ST2084;
72 case GST_VIDEO_TRANSFER_ARIB_STD_B67:
73 return QVideoFrameFormat::ColorTransfer_STD_B67;
76 return QVideoFrameFormat::ColorTransfer_Unknown;
87 return QVideoFrameFormat::Format_Invalid;
93 case GST_VIDEO_COLOR_RANGE_0_255:
94 return QVideoFrameFormat::ColorRange_Full;
95 case GST_VIDEO_COLOR_RANGE_16_235:
96 return QVideoFrameFormat::ColorRange_Video;
98 return QVideoFrameFormat::ColorRange_Unknown;
106 GstVideoOrientationMethod method;
107 QtVideo::Rotation rotation;
111 static constexpr Orientation kOrientations[] = {
112 {GST_VIDEO_ORIENTATION_IDENTITY, QtVideo::Rotation::None,
false},
113 {GST_VIDEO_ORIENTATION_90R, QtVideo::Rotation::Clockwise90,
false},
114 {GST_VIDEO_ORIENTATION_180, QtVideo::Rotation::Clockwise180,
false},
115 {GST_VIDEO_ORIENTATION_90L, QtVideo::Rotation::Clockwise270,
false},
116 {GST_VIDEO_ORIENTATION_HORIZ, QtVideo::Rotation::None,
true},
117 {GST_VIDEO_ORIENTATION_VERT, QtVideo::Rotation::Clockwise180,
true},
118 {GST_VIDEO_ORIENTATION_UL_LR, QtVideo::Rotation::Clockwise90,
true},
119 {GST_VIDEO_ORIENTATION_UR_LL, QtVideo::Rotation::Clockwise270,
true},
121 const Orientation* known = std::ranges::find(kOrientations, method, &Orientation::method);
122 if (known == std::end(kOrientations)) {
123 static std::atomic<bool> s_warnedUnhandled{
false};
124 if (!s_warnedUnhandled.exchange(
true, std::memory_order_relaxed)) {
125 qCWarning(GstQgcFrameMapLog) <<
"Unhandled GstVideoOrientationMethod" << method <<
"— treating as identity";
127 known = std::begin(kOrientations);
129 frame.setRotation(known->rotation);
130 frame.setMirrored(known->mirrored);
135 if (streamOrientation !=
static_cast<int>(GST_VIDEO_ORIENTATION_IDENTITY)) {
138 if (GST_BUFFER_PTS_IS_VALID(buffer)) {
140 frame.setStartTime(GST_BUFFER_PTS(buffer) / GST_USECOND);
141 if (GST_BUFFER_DURATION_IS_VALID(buffer)) {
142 frame.setEndTime((GST_BUFFER_PTS(buffer) + GST_BUFFER_DURATION(buffer)) / GST_USECOND);
149 const GstVideoColorimetry& colorimetry = GST_VIDEO_INFO_COLORIMETRY(&info);
150 QVideoFrameFormat::ColorSpace colorSpace =
toQtColorSpace(colorimetry.matrix);
153 if (colorSpace == QVideoFrameFormat::ColorSpace_Undefined) {
154 const int height = GST_VIDEO_INFO_HEIGHT(&info);
156 colorSpace = (height > 576) ? QVideoFrameFormat::ColorSpace_BT709 : QVideoFrameFormat::ColorSpace_BT601;
159 format.setColorSpace(colorSpace);
161 QVideoFrameFormat::ColorRange range =
toQtColorRange(colorimetry.range);
164 const bool knownYuvMatrix =
165 (colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_BT601) || (colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_BT709) ||
166 (colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_BT2020) ||
167 (colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_SMPTE240M) || (colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_FCC);
168 if (range == QVideoFrameFormat::ColorRange_Unknown && knownYuvMatrix) {
169 range = QVideoFrameFormat::ColorRange_Video;
171 format.setColorRange(range);
174 GstVideoContentLightLevel cll;
175 bool clipApplied =
false;
176 if (caps && gst_video_content_light_level_from_caps(&cll, caps) && cll.max_content_light_level > 0) {
177 format.setMaxLuminance(
static_cast<float>(cll.max_content_light_level));
181 GstVideoMasteringDisplayInfo masteringInfo;
182 if (caps && gst_video_mastering_display_info_from_caps(&masteringInfo, caps)) {
184 const double maxLuminance =
static_cast<double>(masteringInfo.max_display_mastering_luminance) / 10000.0;
185 if (maxLuminance > 0.0) {
186 format.setMaxLuminance(
static_cast<float>(maxLuminance));
198bool applyAffineMatrix(
FrameGeometry& geometry,
const gfloat* matrix)
200 if (!qFuzzyIsNull(matrix[1]) || !qFuzzyIsNull(matrix[4]) || !qFuzzyIsNull(matrix[3]) || !qFuzzyIsNull(matrix[7]) ||
201 !qFuzzyCompare(matrix[15], 1.0F) || qFuzzyIsNull(matrix[0]) || qFuzzyIsNull(matrix[5])) {
204 const double scaleX = matrix[0];
205 const double scaleY = matrix[5];
206 const double ax = 1.0 / scaleX;
207 const double bx = -matrix[12] / scaleX;
208 const double ay = 1.0 / scaleY;
209 const double by = 1.0 - ((1.0 - matrix[13]) / scaleY);
213 constexpr double kMargin = 1e-3;
214 const QRect picture = geometry.
picture;
215 const auto span = [](
double from,
double to,
int origin,
int extent) {
216 const int first = origin +
static_cast<int>(std::floor((std::min(from, to) * extent) + kMargin));
217 const int end = origin +
static_cast<int>(std::ceil((std::max(from, to) * extent) - kMargin));
218 return std::pair(std::clamp(first, origin, origin + extent), std::clamp(end, origin, origin + extent));
220 const auto [left, right] = span(bx, bx + ax, picture.x(), picture.width());
221 const auto [top, bottom] = span(by, by + ay, picture.y(), picture.height());
222 if ((right <= left) || (bottom <= top)) {
225 geometry.
picture = QRect(left, top, right - left, bottom - top);
235 const QSize capsSize(GST_VIDEO_INFO_WIDTH(&capsInfo), GST_VIDEO_INFO_HEIGHT(&capsInfo));
236 FrameGeometry geometry{.frameSize = capsSize, .picture = QRect(QPoint(0, 0), capsSize), .pictureSize = capsSize};
238 if (
const GstVideoMeta*
const meta = gst_buffer_get_video_meta(buffer);
239 meta && (meta->width >=
static_cast<guint
>(capsSize.width())) &&
240 (meta->height >=
static_cast<guint
>(capsSize.height()))) {
241 geometry.
frameSize = QSize(
static_cast<int>(meta->width),
static_cast<int>(meta->height));
243 if (
const GstVideoCropMeta*
const crop = gst_buffer_get_video_crop_meta(buffer)) {
244 geometry.
picture = QRect(
static_cast<int>(crop->x),
static_cast<int>(crop->y),
static_cast<int>(crop->width),
245 static_cast<int>(crop->height));
248 if (
const GstVideoAffineTransformationMeta*
const affine = gst_buffer_get_video_affine_transformation_meta(buffer);
249 affine && !applyAffineMatrix(geometry, affine->matrix)) {
250 static std::atomic<bool> s_warnedUnapplied{
false};
251 if (!s_warnedUnapplied.exchange(
true, std::memory_order_relaxed)) {
252 qCWarning(GstQgcFrameMapLog) <<
"GstVideoAffineTransformationMeta is no viewport with flips (a scale, a "
253 "translation, mirroring) — not applied";
266 int degrees =
static_cast<int>(frame.rotation());
267 bool mirrored = frame.mirrored();
270 mirrored = !mirrored;
274 frame.setRotation(
static_cast<QtVideo::Rotation
>(degrees % 360));
275 frame.setMirrored(mirrored);
284 format.setViewport(geometry.
picture);
294#if defined(QGC_HAS_ANY_GPU_PATH)
296 if (hwContext.gpuEnabled) {
297 if (
auto hwBuf =
makeHwVideoBuffer(buffer, caps, info, format, hwContext, matchedPath, pathCache)) {
298 out.
frame = QVideoFrame(std::move(hwBuf));
312 out.
frame = QVideoFrame(std::move(cpuBuf));
HwVideoBufferPath
GPU zero-copy path a frame is imported through; None means the frame takes the CPU path.
std::unique_ptr< QHwVideoBuffer > makeHwVideoBuffer(GstBuffer *buffer, GstCaps *caps, const GstVideoInfo &info, QVideoFrameFormat format, const HwVideoBufferContext &context, HwVideoBufferPath &matchedPath, HwResolvedPathCache *cache)
QVideoFrameFormat::PixelFormat toQtPixelFormat(GstVideoFormat fmt)
QVideoFrameFormat::ColorTransfer toQtColorTransfer(GstVideoTransferFunction transfer)
void applyColorimetry(QVideoFrameFormat &format, const GstVideoInfo &info, GstCaps *caps)
FrameGeometry frameGeometry(GstBuffer *buffer, const GstVideoInfo &capsInfo)
QVideoFrameFormat withFrameGeometry(QVideoFrameFormat format, const FrameGeometry &geometry)
format for frames of geometry: its frame size, with the picture as the viewport.
void applyFlips(QVideoFrame &frame, const FrameGeometry &geometry)
Flips frame's picture as geometry tells, ahead of the rotation and mirroring the frame already has.
QVideoFrameFormat::ColorSpace toQtColorSpace(GstVideoColorMatrix matrix)
Sample-to-frame helpers for qgcqvideosink's show_frame; pure functions, streaming-thread safe.
void applyOrientationAndTiming(QVideoFrame &frame, GstBuffer *buffer, int streamOrientation)
The stream's orientation + PTS/duration timing; streamOrientation is GstVideoOrientationMethod cast t...
MappedFrame mapSampleToFrame(GstBuffer *buffer, GstCaps *caps, const GstVideoInfo &info, const QVideoFrameFormat &format, const HwVideoBufferContext &hwContext, const CpuVideoFramePool::PinnedFrameCounter &pinnedCpuFrames, HwResolvedPathCache *pathCache)
void applyOrientationToFrame(QVideoFrame &frame, GstVideoOrientationMethod method)
Apply rotation + mirror flags derived from GstVideoOrientationMethod.
QVideoFrameFormat::ColorRange toQtColorRange(GstVideoColorRange range)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static std::unique_ptr< QAbstractVideoBuffer > wrapZeroCopy(GstBuffer *buffer, const GstVideoInfo &info, const QVideoFrameFormat &format, const PinnedFrameCounter &pinned)
std::shared_ptr< std::atomic< int > > PinnedFrameCounter
Live zero-copy frames of one sink; shared so frames Qt still holds after the sink is gone can release...
static QVideoFrame copyFromBuffer(GstBuffer *buffer, const GstVideoInfo &videoInfo, const QVideoFrameFormat &format)
bool hasCpuLayout(GstCaps *caps)
constexpr VideoFormatEntry kVideoFormatTable[]
HwVideoBufferPath gpuPath
None for a CPU frame, and for one that failed to map.
bool demoted
A GPU path was requested for this stream but this frame fell back to CPU.
bool noCpuLayout
Dropped instead of read as linear rows, which would show it scrambled.