KPipewire

pwhelpers.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "pwhelpers.h"
8#include "logging.h"
9
10QImage::Format SpaToQImageFormat(quint32 format)
11{
12 switch (format) {
13 case SPA_VIDEO_FORMAT_BGRx:
14 case SPA_VIDEO_FORMAT_BGRA:
15 return QImage::Format_RGBA8888_Premultiplied; // Handled in SpaBufferToQImage
16 case SPA_VIDEO_FORMAT_ABGR:
17 case SPA_VIDEO_FORMAT_xBGR:
18 return QImage::Format_ARGB32; // Handled in SpaBufferToQImage
19 case SPA_VIDEO_FORMAT_BGR:
21 case SPA_VIDEO_FORMAT_RGBx:
23 case SPA_VIDEO_FORMAT_RGB:
25 case SPA_VIDEO_FORMAT_RGBA:
27 case SPA_VIDEO_FORMAT_GRAY8:
29 default:
30 qCWarning(PIPEWIRE_LOGGING) << "cannot convert spa format to QImage" << format;
32 }
33}
34
35QImage PWHelpers::SpaBufferToQImage(const uchar *data, int width, int height, qsizetype bytesPerLine, spa_video_format format, PipeWireFrameCleanupFunction *c)
36{
37 c->ref();
38 switch (format) {
39 case SPA_VIDEO_FORMAT_BGRx:
40 case SPA_VIDEO_FORMAT_BGRA:
41 case SPA_VIDEO_FORMAT_xBGR:
42 case SPA_VIDEO_FORMAT_ABGR: {
43 // This is needed because QImage does not support BGRA
44 // This is obviously a much slower path, it makes sense to avoid it as much as possible
45 return QImage(data, width, height, bytesPerLine, SpaToQImageFormat(format), &PipeWireFrameCleanupFunction::unref, c).rgbSwapped();
46 }
47 case SPA_VIDEO_FORMAT_GRAY8:
48 case SPA_VIDEO_FORMAT_RGBx:
49 case SPA_VIDEO_FORMAT_RGB:
50 case SPA_VIDEO_FORMAT_RGBA:
51 default:
52 return QImage(data, width, height, bytesPerLine, SpaToQImageFormat(format), &PipeWireFrameCleanupFunction::unref, c);
53 }
54}
55
56QImage PipeWireFrameData::toImage() const
57{
58 return PWHelpers::SpaBufferToQImage(static_cast<uchar *>(data), size.width(), size.height(), stride, format, cleanup);
59}
60
61std::shared_ptr<PipeWireFrameData> PipeWireFrameData::copy() const
62{
63 const uint bufferSize = size.height() * stride * 4;
64 auto newMap = malloc(bufferSize);
65 memcpy(newMap, data, bufferSize);
66 return std::make_shared<PipeWireFrameData>(format, newMap, size, stride, new PipeWireFrameCleanupFunction([newMap] {
67 free(newMap);
68 }));
69}
The to track the lifetime of a pipewire frame.
Definition pwhelpers.h:25
QImage rgbSwapped() &&
int height() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.