| 1 |
/* |
| 2 |
* This file is part of Bino, a 3D video player. |
| 3 |
* |
| 4 |
* Copyright (C) 2024 |
| 5 |
* Martin Lambers <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <QStandardPaths> |
| 22 |
#include <QDir> |
| 23 |
#include <QTemporaryFile> |
| 24 |
#include <QSharedPointer> |
| 25 |
#include <QImage> |
| 26 |
#include <cstring> |
| 27 |
|
| 28 |
#include "urlloader.hpp" |
| 29 |
#include "tools.hpp" |
| 30 |
#include "log.hpp" |
| 31 |
#include "digestiblemedia.hpp" |
| 32 |
|
| 33 |
|
| 34 |
/* We convert JPEGs to temporary PPMs here, for the following reasons: |
| 35 |
* - QtMultimedia tries to decode JPEGs with hardware acceleration, which fails |
| 36 |
* when the image dimensions (or other properties) are not within the |
| 37 |
* constraints of the hardware decoder, which is optimized for video. |
| 38 |
* This happens often with both the GStreamer and FFmpeg backends. |
| 39 |
* Unfortunately there does not seem to be a fallback to software decoding. |
| 40 |
* - MPO files can contain multiple JPEG images. In the only relevant use case, |
| 41 |
* they contain a left and a right JPEG with a lot of junk in between (probably |
| 42 |
* called metadata by some standard). These files typically cannot be read |
| 43 |
* reliably by either QtMultimedia backend. So we read both JPEGs manually |
| 44 |
* from the MPO and stack them on top of each other (top-bottom format). |
| 45 |
* - The destination image format is PPM because it is fast to write (no |
| 46 |
* compression) and the media backend will not be tempted to try hardware |
| 47 |
* accelerated decoding on PPMs. |
| 48 |
*/ |
| 49 |
|
| 50 |
QUrl digestibleMediaUrl(const QUrl& url) |
| 51 |
{ |
| 52 |
static QMap<QUrl, QSharedPointer<QTemporaryFile>> cache; |
| 53 |
|
| 54 |
// check if we need conversion |
| 55 |
QString extension = getExtension(url); |
| 56 |
bool needsConversion = (extension == "jpg" || extension == "jpeg" || extension == "jps" || extension == "mpo"); |
| 57 |
if (!needsConversion) { |
| 58 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1 needs no conversion").arg(url.toString()))); |
| 59 |
return url; |
| 60 |
} |
| 61 |
|
| 62 |
// check if we have it cached |
| 63 |
QSharedPointer<QTemporaryFile> tempFile = nullptr; |
| 64 |
auto it = cache.find(url); |
| 65 |
if (it != cache.end()) { |
| 66 |
tempFile = it.value(); |
| 67 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1 is in cache: %2").arg(url.toString()).arg(tempFile->fileName()))); |
| 68 |
} |
| 69 |
|
| 70 |
// build temporary file if it is not in cache yet |
| 71 |
if (!tempFile) { |
| 72 |
UrlLoader loader(url); |
| 73 |
const QByteArray& data = loader.load(); |
| 74 |
if (data.size() == 0) { |
| 75 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: cannot download").arg(url.toString()))); |
| 76 |
return url; |
| 77 |
} |
| 78 |
|
| 79 |
QImage img; |
| 80 |
if (!img.loadFromData(data, "JPG")) { |
| 81 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: cannot load JPEG").arg(url.toString()))); |
| 82 |
return url; |
| 83 |
} |
| 84 |
|
| 85 |
if (url.fileName().endsWith("mpo", Qt::CaseInsensitive)) { |
| 86 |
unsigned char jpegMarker[4] = { 0xff, 0xd8, 0xff, 0xe1 }; |
| 87 |
QByteArrayView jpegMarkerView(jpegMarker, 4); |
| 88 |
qsizetype nextJpeg = data.indexOf(jpegMarkerView, 4); |
| 89 |
if (nextJpeg <= 0) { |
| 90 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: no second jpeg marker found").arg(url.toString()))); |
| 91 |
} else { |
| 92 |
QImage imgRight; |
| 93 |
if (!imgRight.loadFromData(QByteArrayView(data.data() + nextJpeg, data.size() - nextJpeg), "JPG")) { |
| 94 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: cannot load second jpeg").arg(url.toString()))); |
| 95 |
} else { |
| 96 |
if (img.format() != imgRight.format() || img.size() != imgRight.size()) { |
| 97 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: second jpeg is incompatible").arg(url.toString()))); |
| 98 |
} else { |
| 99 |
QImage combinedImg(img.width(), 2 * img.height(), img.format()); |
| 100 |
for (int i = 0; i < img.height(); i++) { |
| 101 |
std::memcpy(combinedImg.scanLine(i), img.constScanLine(i), img.bytesPerLine()); |
| 102 |
} |
| 103 |
for (int i = 0; i < img.height(); i++) { |
| 104 |
std::memcpy(combinedImg.scanLine(img.height() + i), imgRight.constScanLine(i), imgRight.bytesPerLine()); |
| 105 |
} |
| 106 |
img = combinedImg; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
QString cacheDirName = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); |
| 113 |
QDir cacheDir(cacheDirName); |
| 114 |
if (!cacheDir.mkpath(cacheDirName)) { |
| 115 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: cannot create cache directory %2").arg(url.toString()).arg(cacheDirName))); |
| 116 |
return url; |
| 117 |
} |
| 118 |
QString tmpl = cacheDirName + '/' + QString("bino-XXXXXX") + QString(".ppm"); |
| 119 |
tempFile.reset(new QTemporaryFile(tmpl)); |
| 120 |
if (!img.save(tempFile.get(), "PPM")) { |
| 121 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1: cannot save to %2").arg(url.toString()).arg(tempFile->fileName()))); |
| 122 |
return url; |
| 123 |
} |
| 124 |
|
| 125 |
LOG_DEBUG("%s", qPrintable(QString("digestibleMediaUrl: %1 is saved in %2").arg(url.toString()).arg(tempFile->fileName()))); |
| 126 |
cache.insert(url, tempFile); |
| 127 |
} |
| 128 |
|
| 129 |
return QUrl::fromLocalFile(tempFile->fileName()); |
| 130 |
} |