src/platform/qt/VideoDumper.cpp (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "VideoDumper.h"
7
8#include <QImage>
9#include <QVideoSurfaceFormat>
10
11using namespace QGBA;
12
13VideoDumper::VideoDumper(QObject* parent)
14 : QAbstractVideoSurface(parent)
15{
16}
17
18bool VideoDumper::present(const QVideoFrame& frame) {
19 QVideoFrame mappedFrame(frame);
20 if (!mappedFrame.map(QAbstractVideoBuffer::ReadOnly)) {
21 return false;
22 }
23 QVideoFrame::PixelFormat vFormat = mappedFrame.pixelFormat();
24 QImage::Format format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
25 bool swap = false;
26 if (format == QImage::Format_Invalid) {
27 if (vFormat < QVideoFrame::Format_BGRA5658_Premultiplied) {
28 vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32);
29 format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
30 if (format == QImage::Format_ARGB32) {
31 format = QImage::Format_RGBA8888;
32 } else if (format == QImage::Format_ARGB32_Premultiplied) {
33 format = QImage::Format_RGBA8888_Premultiplied;
34 }
35 swap = true;
36 } else {
37 return false;
38 }
39 }
40 uchar* bits = mappedFrame.bits();
41 QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);
42 if (swap) {
43 image = image.rgbSwapped();
44 } else if (surfaceFormat().scanLineDirection() != QVideoSurfaceFormat::BottomToTop) {
45 image = image.copy(); // Create a deep copy of the bits
46 }
47 if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
48 image = image.mirrored();
49 }
50 mappedFrame.unmap();
51 emit imageAvailable(image);
52 return true;
53}
54
55QList<QVideoFrame::PixelFormat> VideoDumper::supportedPixelFormats(QAbstractVideoBuffer::HandleType) const {
56 QList<QVideoFrame::PixelFormat> list;
57 list.append(QVideoFrame::Format_RGB32);
58 list.append(QVideoFrame::Format_ARGB32);
59 list.append(QVideoFrame::Format_RGB24);
60 list.append(QVideoFrame::Format_ARGB32_Premultiplied);
61 list.append(QVideoFrame::Format_RGB565);
62 list.append(QVideoFrame::Format_RGB555);
63 list.append(QVideoFrame::Format_BGR32);
64 list.append(QVideoFrame::Format_BGRA32);
65 list.append(QVideoFrame::Format_BGR24);
66 list.append(QVideoFrame::Format_BGRA32_Premultiplied);
67 list.append(QVideoFrame::Format_BGR565);
68 list.append(QVideoFrame::Format_BGR555);
69 return list;
70}