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 vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32);
27 format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
28 if (format == QImage::Format_ARGB32) {
29 format = QImage::Format_RGBA8888;
30 } else if (format == QImage::Format_ARGB32_Premultiplied) {
31 format = QImage::Format_RGBA8888_Premultiplied;
32 }
33 swap = true;
34 }
35 uchar* bits = mappedFrame.bits();
36 QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);
37 if (swap) {
38 image = image.rgbSwapped();
39 } else {
40#ifdef Q_OS_WIN
41 // Qt's DirectShow plug-in is pretty dang buggy
42 image = image.mirrored();
43#else
44 image = image.copy(); // Create a deep copy of the bits
45#endif
46 }
47 mappedFrame.unmap();
48 emit imageAvailable(image);
49 return true;
50}
51
52QList<QVideoFrame::PixelFormat> VideoDumper::supportedPixelFormats(QAbstractVideoBuffer::HandleType) const {
53 QList<QVideoFrame::PixelFormat> list;
54 list.append(QVideoFrame::Format_RGB32);
55 list.append(QVideoFrame::Format_ARGB32);
56 list.append(QVideoFrame::Format_RGB24);
57 list.append(QVideoFrame::Format_ARGB32_Premultiplied);
58 list.append(QVideoFrame::Format_RGB565);
59 list.append(QVideoFrame::Format_RGB555);
60 list.append(QVideoFrame::Format_BGR32);
61 list.append(QVideoFrame::Format_BGRA32);
62 list.append(QVideoFrame::Format_BGR24);
63 list.append(QVideoFrame::Format_BGRA32_Premultiplied);
64 list.append(QVideoFrame::Format_BGR565);
65 list.append(QVideoFrame::Format_BGR555);
66 return list;
67}