all repos — mgba @ d33f1d13926f4aa2afebad31775222cdd278411c

mGBA Game Boy Advance Emulator

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
10using namespace QGBA;
11
12VideoDumper::VideoDumper(QObject* parent)
13	: QAbstractVideoSurface(parent)
14{
15}
16
17bool VideoDumper::present(const QVideoFrame& frame) {
18	QVideoFrame mappedFrame(frame);
19	if (!mappedFrame.map(QAbstractVideoBuffer::ReadOnly)) {
20		return false;
21	}
22	QVideoFrame::PixelFormat vFormat = mappedFrame.pixelFormat();
23	QImage::Format format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
24	bool swap = false;
25	if (format == QImage::Format_Invalid) {
26		if (vFormat < QVideoFrame::Format_BGRA5658_Premultiplied) {
27			vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32);
28			format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
29			if (format == QImage::Format_ARGB32) {
30				format = QImage::Format_RGBA8888;
31			} else if (format == QImage::Format_ARGB32_Premultiplied) {
32				format = QImage::Format_RGBA8888_Premultiplied;
33			}
34			swap = true;
35		} else {
36			return false;
37		}
38	}
39	uchar* bits = mappedFrame.bits();
40	QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);
41	if (swap) {
42		image = image.rgbSwapped();
43	} else {
44#ifdef Q_OS_WIN
45		// Qt's DirectShow plug-in is pretty dang buggy
46		image = image.mirrored();
47#else
48		image = image.copy(); // Create a deep copy of the bits
49#endif
50	}
51	mappedFrame.unmap();
52	emit imageAvailable(image);
53	return true;
54}
55
56QList<QVideoFrame::PixelFormat> VideoDumper::supportedPixelFormats(QAbstractVideoBuffer::HandleType) const {
57	QList<QVideoFrame::PixelFormat> list;
58	list.append(QVideoFrame::Format_RGB32);
59	list.append(QVideoFrame::Format_ARGB32);
60	list.append(QVideoFrame::Format_RGB24);
61	list.append(QVideoFrame::Format_ARGB32_Premultiplied);
62	list.append(QVideoFrame::Format_RGB565);
63	list.append(QVideoFrame::Format_RGB555);
64	list.append(QVideoFrame::Format_BGR32);
65	list.append(QVideoFrame::Format_BGRA32);
66	list.append(QVideoFrame::Format_BGR24);
67	list.append(QVideoFrame::Format_BGRA32_Premultiplied);
68	list.append(QVideoFrame::Format_BGR565);
69	list.append(QVideoFrame::Format_BGR555);
70	return list;
71}