2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-11-06 18:39:59 +00:00
|
|
|
#include "dive.h"
|
|
|
|
#include "metrics.h"
|
|
|
|
#include "divelist.h"
|
|
|
|
#include "qthelper.h"
|
|
|
|
#include "imagedownloader.h"
|
2018-07-10 13:04:35 +00:00
|
|
|
#include "videoframeextractor.h"
|
2018-03-10 13:15:50 +00:00
|
|
|
#include "qt-models/divepicturemodel.h"
|
|
|
|
#include "metadata.h"
|
2015-11-06 18:39:59 +00:00
|
|
|
#include <unistd.h>
|
2016-04-30 22:00:29 +00:00
|
|
|
#include <QString>
|
2018-03-30 05:57:30 +00:00
|
|
|
#include <QImageReader>
|
2018-03-10 13:15:50 +00:00
|
|
|
#include <QDataStream>
|
2018-05-09 21:08:44 +00:00
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QPainter>
|
2015-11-06 18:39:59 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2018-03-11 11:58:55 +00:00
|
|
|
// Note: this is a global instead of a function-local variable on purpose.
|
|
|
|
// We don't want this to be generated in a different thread context if
|
|
|
|
// ImageDownloader::instance() is called from a worker thread.
|
|
|
|
static ImageDownloader imageDownloader;
|
|
|
|
ImageDownloader *ImageDownloader::instance()
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-03-11 11:58:55 +00:00
|
|
|
return &imageDownloader;
|
2016-03-15 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 11:58:55 +00:00
|
|
|
ImageDownloader::ImageDownloader()
|
2018-02-08 21:45:55 +00:00
|
|
|
{
|
2018-03-11 11:58:55 +00:00
|
|
|
connect(&manager, &QNetworkAccessManager::finished, this, &ImageDownloader::saveImage);
|
2018-02-08 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 10:50:34 +00:00
|
|
|
void ImageDownloader::load(QUrl url, QString filename)
|
2018-02-08 21:45:55 +00:00
|
|
|
{
|
2018-03-11 11:58:55 +00:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
request.setAttribute(QNetworkRequest::User, filename);
|
|
|
|
manager.get(request);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 11:58:55 +00:00
|
|
|
void ImageDownloader::saveImage(QNetworkReply *reply)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-03-11 11:58:55 +00:00
|
|
|
QString filename = reply->request().attribute(QNetworkRequest::User).toString();
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2018-05-23 16:24:08 +00:00
|
|
|
emit failed(filename);
|
2018-03-11 11:58:55 +00:00
|
|
|
} else {
|
|
|
|
QByteArray imageData = reply->readAll();
|
2018-06-02 16:03:03 +00:00
|
|
|
if (imageData.isEmpty()) {
|
|
|
|
emit failed(filename);
|
|
|
|
} else {
|
|
|
|
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(path);
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(filename.toUtf8());
|
|
|
|
QFile imageFile(path.append("/").append(hash.result().toHex()));
|
|
|
|
if (imageFile.open(QIODevice::WriteOnly)) {
|
|
|
|
qDebug() << "Write image to" << imageFile.fileName();
|
|
|
|
QDataStream stream(&imageFile);
|
|
|
|
stream.writeRawData(imageData.data(), imageData.length());
|
|
|
|
imageFile.waitForBytesWritten(-1);
|
|
|
|
imageFile.close();
|
|
|
|
learnPictureFilename(filename, imageFile.fileName());
|
|
|
|
}
|
|
|
|
emit loaded(filename);
|
2018-03-11 11:58:55 +00:00
|
|
|
}
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2016-03-15 21:45:17 +00:00
|
|
|
|
2018-03-11 11:58:55 +00:00
|
|
|
reply->deleteLater();
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 19:24:44 +00:00
|
|
|
static bool hasVideoFileExtension(const QString &filename)
|
2018-05-15 18:47:35 +00:00
|
|
|
{
|
2018-07-08 19:24:44 +00:00
|
|
|
for (const QString &ext: videoExtensionsList)
|
|
|
|
if (filename.endsWith(ext, Qt::CaseInsensitive))
|
|
|
|
return true;
|
|
|
|
return false;
|
2018-05-15 18:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch a picture from the given filename and determine its type (picture of video).
|
|
|
|
// If this is a non-remote file, fetch it from disk. Remote files are fetched from the
|
|
|
|
// net in a background thread. In such a case, the output-type is set to MEDIATYPE_STILL_LOADING.
|
2018-06-20 19:41:18 +00:00
|
|
|
// If the input-flag "tryDownload" is set to false, no download attempt is made. This is to
|
|
|
|
// prevent infinite loops, where failed image downloads would be repeated ad infinitum.
|
2018-05-15 18:47:35 +00:00
|
|
|
// Returns: fetched image, type
|
|
|
|
Thumbnailer::Thumbnail Thumbnailer::fetchImage(const QString &filename, const QString &originalFilename, bool tryDownload)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-06-20 19:41:18 +00:00
|
|
|
QUrl url = QUrl::fromUserInput(filename);
|
|
|
|
if (url.isLocalFile()) {
|
2018-07-08 19:24:44 +00:00
|
|
|
// We try to determine the type first by peeking into the file.
|
2018-05-15 18:47:35 +00:00
|
|
|
QString filename = url.toLocalFile();
|
2018-07-08 19:24:44 +00:00
|
|
|
metadata md;
|
|
|
|
mediatype_t type = get_metadata(qPrintable(filename), &md);
|
|
|
|
|
|
|
|
// For io error or video, return early with the appropriate dummy-icon.
|
2018-07-15 15:56:18 +00:00
|
|
|
if (type == MEDIATYPE_IO_ERROR)
|
|
|
|
return { failImage, MEDIATYPE_IO_ERROR, 0 };
|
|
|
|
else if (type == MEDIATYPE_VIDEO)
|
2018-07-10 13:04:35 +00:00
|
|
|
return fetchVideoThumbnail(filename, originalFilename, md.duration);
|
2018-07-08 19:24:44 +00:00
|
|
|
|
|
|
|
// Try if Qt can parse this image. If it does, use this as a thumbnail.
|
2018-05-15 18:47:35 +00:00
|
|
|
QImage thumb(filename);
|
2018-07-13 20:55:35 +00:00
|
|
|
if (!thumb.isNull()) {
|
2018-07-15 15:56:18 +00:00
|
|
|
int size = maxThumbnailSize();
|
|
|
|
thumb = thumb.scaled(size, size, Qt::KeepAspectRatio);
|
|
|
|
return addPictureThumbnailToCache(originalFilename, thumb);
|
2018-07-13 20:55:35 +00:00
|
|
|
}
|
2018-07-08 19:24:44 +00:00
|
|
|
|
|
|
|
// Neither our code, nor Qt could determine the type of this object from looking at the data.
|
2018-07-13 20:55:35 +00:00
|
|
|
// Try to check for a video-file extension. Since we couldn't parse the video file,
|
|
|
|
// we pass 0 as the duration.
|
2018-07-15 15:56:18 +00:00
|
|
|
if (hasVideoFileExtension(filename))
|
2018-07-10 13:04:35 +00:00
|
|
|
return fetchVideoThumbnail(filename, originalFilename, {0} );
|
2018-07-08 19:24:44 +00:00
|
|
|
|
|
|
|
// Give up: we simply couldn't determine what this thing is.
|
2018-07-13 20:55:35 +00:00
|
|
|
// But since we managed to read this file, mark this file in the cache as unknown.
|
2018-07-15 15:56:18 +00:00
|
|
|
return addUnknownThumbnailToCache(originalFilename);
|
2018-05-15 22:17:04 +00:00
|
|
|
} else if (tryDownload) {
|
2018-06-20 19:41:18 +00:00
|
|
|
// This has to be done in UI main thread, because QNetworkManager refuses
|
|
|
|
// to treat requests from other threads. invokeMethod() is Qt's way of calling a
|
|
|
|
// function in a different thread, namely the thread the called object is associated to.
|
|
|
|
QMetaObject::invokeMethod(ImageDownloader::instance(), "load", Qt::AutoConnection, Q_ARG(QUrl, url), Q_ARG(QString, originalFilename));
|
2018-07-15 15:56:18 +00:00
|
|
|
return { QImage(), MEDIATYPE_STILL_LOADING, 0 };
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-07-15 15:56:18 +00:00
|
|
|
return { QImage(), MEDIATYPE_IO_ERROR, 0 };
|
2018-06-20 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch a picture based on its original filename. If there is a translated filename (obtained either
|
|
|
|
// by the find-moved-picture functionality or the filename of the local cache of a remote picture),
|
|
|
|
// try that first. If fetching from the translated filename fails, this could mean that the image
|
|
|
|
// was downloaded previously, but for some reason the cached picture was lost. Therefore, in such a
|
|
|
|
// case, try the canonical filename. If that likewise fails, give up. For input and output parameters
|
|
|
|
// see fetchImage() above.
|
2018-05-15 18:47:35 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::getHashedImage(const QString &filename, bool tryDownload)
|
2018-06-20 19:41:18 +00:00
|
|
|
{
|
|
|
|
QString localFilename = localFilePath(filename);
|
|
|
|
|
2018-07-08 19:24:44 +00:00
|
|
|
// If there is a translated filename, try that first.
|
|
|
|
// Note that we set the default type to io-error, so that if we didn't try
|
|
|
|
// the local filename first, we will load the file from the canonical filename.
|
2018-07-15 15:56:18 +00:00
|
|
|
Thumbnail thumbnail { QImage(), MEDIATYPE_IO_ERROR, 0 };
|
2018-06-20 19:41:18 +00:00
|
|
|
if (localFilename != filename)
|
2018-05-15 18:47:35 +00:00
|
|
|
thumbnail = fetchImage(localFilename, filename, tryDownload);
|
2018-06-20 19:41:18 +00:00
|
|
|
|
2018-07-08 19:24:44 +00:00
|
|
|
// If fetching from the local filename failed (or we didn't even try),
|
|
|
|
// use the canonical filename. This might for example happen if we downloaded
|
|
|
|
// a file, but for some reason lost the cached file.
|
|
|
|
if (thumbnail.type == MEDIATYPE_IO_ERROR)
|
2018-05-15 18:47:35 +00:00
|
|
|
thumbnail = fetchImage(filename, filename, tryDownload);
|
2018-06-20 19:41:18 +00:00
|
|
|
|
2018-07-08 19:24:44 +00:00
|
|
|
if (thumbnail.type == MEDIATYPE_IO_ERROR)
|
2018-06-20 19:41:18 +00:00
|
|
|
qInfo() << "Error loading image" << filename << "[local:" << localFilename << "]";
|
2018-05-15 18:47:35 +00:00
|
|
|
return thumbnail;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-03-10 13:15:50 +00:00
|
|
|
|
2018-05-09 21:08:44 +00:00
|
|
|
static QImage renderIcon(const char *id, int size)
|
|
|
|
{
|
2018-05-20 20:35:29 +00:00
|
|
|
QImage res(size, size, QImage::Format_RGB32);
|
|
|
|
res.fill(Qt::white);
|
2018-05-09 21:08:44 +00:00
|
|
|
QSvgRenderer svg{QString(id)};
|
|
|
|
QPainter painter(&res);
|
|
|
|
svg.render(&painter);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
// As renderIcon, but render to a fixed width and scale height accordingly
|
|
|
|
// and have a transparent background.
|
|
|
|
static QImage renderIconWidth(const char *id, int size)
|
|
|
|
{
|
|
|
|
QSvgRenderer svg{QString(id)};
|
|
|
|
QSize svgSize = svg.defaultSize();
|
|
|
|
QImage res(size, size * svgSize.height() / svgSize.width(), QImage::Format_ARGB32);
|
|
|
|
QPainter painter(&res);
|
|
|
|
svg.render(&painter);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:08:44 +00:00
|
|
|
Thumbnailer::Thumbnailer() : failImage(renderIcon(":filter-close", maxThumbnailSize())), // TODO: Don't misuse filter close icon
|
2018-05-15 18:47:35 +00:00
|
|
|
dummyImage(renderIcon(":camera-icon", maxThumbnailSize())),
|
2018-07-08 19:24:44 +00:00
|
|
|
videoImage(renderIcon(":video-icon", maxThumbnailSize())),
|
2018-07-10 13:04:35 +00:00
|
|
|
videoOverlayImage(renderIconWidth(":video-overlay", maxThumbnailSize())),
|
2018-07-08 19:24:44 +00:00
|
|
|
unknownImage(renderIcon(":unknown-icon", maxThumbnailSize()))
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
// Currently, we only process one image at a time. Stefan Fuchs reported problems when
|
|
|
|
// calculating multiple thumbnails at once and this hopefully helps.
|
|
|
|
pool.setMaxThreadCount(1);
|
2018-03-11 11:58:55 +00:00
|
|
|
connect(ImageDownloader::instance(), &ImageDownloader::loaded, this, &Thumbnailer::imageDownloaded);
|
|
|
|
connect(ImageDownloader::instance(), &ImageDownloader::failed, this, &Thumbnailer::imageDownloadFailed);
|
2018-07-10 13:04:35 +00:00
|
|
|
connect(VideoFrameExtractor::instance(), &VideoFrameExtractor::extracted, this, &Thumbnailer::frameExtracted);
|
|
|
|
connect(VideoFrameExtractor::instance(), &VideoFrameExtractor::failed, this, &Thumbnailer::frameExtractionFailed);
|
|
|
|
connect(VideoFrameExtractor::instance(), &VideoFrameExtractor::failed, this, &Thumbnailer::frameExtractionInvalid);
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Thumbnailer *Thumbnailer::instance()
|
|
|
|
{
|
|
|
|
static Thumbnailer self;
|
|
|
|
return &self;
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:55:35 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::getPictureThumbnailFromStream(QDataStream &stream)
|
|
|
|
{
|
|
|
|
QImage res;
|
|
|
|
stream >> res;
|
|
|
|
return { res, MEDIATYPE_PICTURE, 0 };
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
void Thumbnailer::markVideoThumbnail(QImage &img)
|
|
|
|
{
|
|
|
|
QSize size = img.size();
|
|
|
|
QImage marker = videoOverlayImage.scaledToWidth(size.width());
|
|
|
|
marker = marker.copy(0, (marker.size().height() - size.height()) / 2, size.width(), size.height());
|
|
|
|
QPainter painter(&img);
|
|
|
|
painter.drawImage(0, 0, marker);
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(duration_t)
|
|
|
|
Thumbnailer::Thumbnail Thumbnailer::getVideoThumbnailFromStream(QDataStream &stream, const QString &filename)
|
2018-07-13 20:55:35 +00:00
|
|
|
{
|
|
|
|
quint32 duration, numPics;
|
|
|
|
stream >> duration >> numPics;
|
|
|
|
|
|
|
|
// If reading did not succeed, schedule for recalculation - this thumbnail might
|
|
|
|
// have been written by an older version, which couldn't extract the duration.
|
2018-07-15 15:56:18 +00:00
|
|
|
// Likewise test the duration and number of pictures for sanity (no videos longer than 10 h,
|
|
|
|
// no more than 10000 pictures).
|
|
|
|
if (stream.status() != QDataStream::Ok || duration > 36000 || numPics > 10000)
|
2018-07-13 20:55:35 +00:00
|
|
|
return { QImage(), MEDIATYPE_VIDEO, 0 };
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
// If the file didn't contain an image, but user turned on thumbnail extraction, schedule thumbnail
|
|
|
|
// for extraction. TODO: save failure to extract thumbnails to disk so that thumbnailing
|
|
|
|
// is not repeated ad-nauseum for broken images.
|
|
|
|
if (numPics == 0 && prefs.extract_video_thumbnails) {
|
|
|
|
QMetaObject::invokeMethod(VideoFrameExtractor::instance(), "extract", Qt::AutoConnection,
|
|
|
|
Q_ARG(QString, filename), Q_ARG(QString, filename), Q_ARG(duration_t, duration_t{(int32_t)duration}));
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:55:35 +00:00
|
|
|
// Currently, we support only one picture
|
|
|
|
QImage res;
|
|
|
|
if (numPics > 0) {
|
|
|
|
quint32 offset;
|
|
|
|
stream >> offset >> res;
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
if (res.isNull())
|
|
|
|
res = videoImage; // No picture -> show dummy-icon
|
|
|
|
else
|
|
|
|
markVideoThumbnail(res); // We got an image -> place our video marker on top of it
|
|
|
|
|
|
|
|
return { res, MEDIATYPE_VIDEO, (int32_t)duration };
|
2018-07-13 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch a thumbnail from cache.
|
|
|
|
// If Thumbnail::QImage is null, the thumbnail is scheduled for recreation.
|
2018-05-15 18:47:35 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::getThumbnailFromCache(const QString &picture_filename)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
if (filename.isEmpty())
|
2018-07-15 15:56:18 +00:00
|
|
|
return { QImage(), MEDIATYPE_UNKNOWN, 0 };
|
2018-03-10 13:15:50 +00:00
|
|
|
QFile file(filename);
|
2018-05-27 13:42:22 +00:00
|
|
|
|
|
|
|
if (prefs.auto_recalculate_thumbnails) {
|
|
|
|
// Check if thumbnails is older than the (local) image file
|
|
|
|
QString filenameLocal = localFilePath(qPrintable(picture_filename));
|
|
|
|
QFileInfo pictureInfo(filenameLocal);
|
|
|
|
QFileInfo thumbnailInfo(file);
|
|
|
|
if (pictureInfo.exists() && thumbnailInfo.exists()) {
|
|
|
|
QDateTime pictureTime = pictureInfo.lastModified();
|
|
|
|
QDateTime thumbnailTime = thumbnailInfo.lastModified();
|
|
|
|
if (pictureTime.isValid() && thumbnailTime.isValid() && thumbnailTime < pictureTime) {
|
|
|
|
// Both files exist, have valid timestamps and thumbnail was calculated before picture.
|
|
|
|
// Return an empty thumbnail to signal recalculation of the thumbnail
|
2018-07-13 20:55:35 +00:00
|
|
|
return { QImage(), MEDIATYPE_UNKNOWN, 0 };
|
2018-05-27 13:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:15:50 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2018-07-13 20:55:35 +00:00
|
|
|
return { QImage(), MEDIATYPE_UNKNOWN, 0 };
|
2018-03-10 13:15:50 +00:00
|
|
|
QDataStream stream(&file);
|
|
|
|
|
|
|
|
// Each thumbnail file is composed of a media-type and an image file.
|
|
|
|
quint32 type;
|
|
|
|
QImage res;
|
|
|
|
stream >> type;
|
2018-05-15 18:47:35 +00:00
|
|
|
|
2018-07-13 20:55:35 +00:00
|
|
|
switch (type) {
|
|
|
|
case MEDIATYPE_PICTURE: return getPictureThumbnailFromStream(stream);
|
2018-07-10 13:04:35 +00:00
|
|
|
case MEDIATYPE_VIDEO: return getVideoThumbnailFromStream(stream, picture_filename);
|
2018-07-13 20:55:35 +00:00
|
|
|
case MEDIATYPE_UNKNOWN: return { unknownImage, MEDIATYPE_UNKNOWN, 0 };
|
|
|
|
default: return { QImage(), MEDIATYPE_UNKNOWN, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::addVideoThumbnailToCache(const QString &picture_filename, duration_t duration,
|
|
|
|
const QImage &image, duration_t position)
|
2018-07-13 20:55:35 +00:00
|
|
|
{
|
|
|
|
// The format of video thumbnails:
|
|
|
|
// uint32 MEDIATYPE_VIDEO
|
|
|
|
// uint32 duration of video in seconds
|
|
|
|
// uint32 number of pictures (0 = we didn't manage to extract a picture)
|
|
|
|
// for each picture:
|
|
|
|
// uint32 offset in msec from begining of video
|
|
|
|
// QImage frame
|
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
QSaveFile file(filename);
|
2018-07-15 15:56:18 +00:00
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream stream(&file);
|
2018-05-15 18:47:35 +00:00
|
|
|
|
2018-07-15 15:56:18 +00:00
|
|
|
stream << (quint32)MEDIATYPE_VIDEO;
|
|
|
|
stream << (quint32)duration.seconds;
|
2018-07-10 13:04:35 +00:00
|
|
|
|
|
|
|
if (image.isNull()) {
|
|
|
|
// No image provided
|
|
|
|
stream << (quint32)0;
|
|
|
|
} else {
|
|
|
|
// Currently, we support at most one image
|
|
|
|
stream << (quint32)1;
|
|
|
|
stream << (quint32)position.seconds;
|
|
|
|
stream << image;
|
|
|
|
}
|
|
|
|
|
2018-07-15 15:56:18 +00:00
|
|
|
file.commit();
|
|
|
|
}
|
|
|
|
return { videoImage, MEDIATYPE_VIDEO, duration };
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::fetchVideoThumbnail(const QString &filename, const QString &originalFilename, duration_t duration)
|
|
|
|
{
|
|
|
|
if (prefs.extract_video_thumbnails) {
|
|
|
|
// Video-thumbnailing is enabled. Fetch thumbnail in background thread and in the meanwhile
|
|
|
|
// return a dummy image.
|
|
|
|
QMetaObject::invokeMethod(VideoFrameExtractor::instance(), "extract", Qt::AutoConnection,
|
|
|
|
Q_ARG(QString, originalFilename), Q_ARG(QString, filename), Q_ARG(duration_t, duration));
|
|
|
|
return { videoImage, MEDIATYPE_VIDEO, duration };
|
|
|
|
} else {
|
|
|
|
// Video-thumbnailing is disabled. Write a thumbnail without picture.
|
|
|
|
return addVideoThumbnailToCache(originalFilename, duration, QImage(), {0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 15:56:18 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::addPictureThumbnailToCache(const QString &picture_filename, const QImage &thumbnail)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
2018-07-13 20:55:35 +00:00
|
|
|
// The format of a picture-thumbnail is very simple:
|
|
|
|
// uint32 MEDIATYPE_PICTURE
|
|
|
|
// QImage thumbnail
|
2018-03-10 13:15:50 +00:00
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
QSaveFile file(filename);
|
2018-07-15 15:56:18 +00:00
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream stream(&file);
|
2018-03-10 13:15:50 +00:00
|
|
|
|
2018-07-15 15:56:18 +00:00
|
|
|
stream << (quint32)MEDIATYPE_PICTURE;
|
|
|
|
stream << thumbnail;
|
|
|
|
file.commit();
|
|
|
|
}
|
|
|
|
return { thumbnail, MEDIATYPE_PICTURE, 0 };
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 15:56:18 +00:00
|
|
|
Thumbnailer::Thumbnail Thumbnailer::addUnknownThumbnailToCache(const QString &picture_filename)
|
2018-07-13 20:55:35 +00:00
|
|
|
{
|
|
|
|
QString filename = thumbnailFileName(picture_filename);
|
|
|
|
QSaveFile file(filename);
|
2018-07-15 15:56:18 +00:00
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream stream(&file);
|
|
|
|
stream << (quint32)MEDIATYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
return { unknownImage, MEDIATYPE_UNKNOWN, 0 };
|
2018-07-13 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:04:35 +00:00
|
|
|
void Thumbnailer::frameExtracted(QString filename, QImage thumbnail, duration_t duration, duration_t offset)
|
|
|
|
{
|
|
|
|
if (thumbnail.isNull()) {
|
|
|
|
frameExtractionFailed(filename, duration);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
int size = maxThumbnailSize();
|
|
|
|
thumbnail = thumbnail.scaled(size, size, Qt::KeepAspectRatio);
|
|
|
|
markVideoThumbnail(thumbnail);
|
|
|
|
addVideoThumbnailToCache(filename, duration, thumbnail, offset);
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
workingOn.remove(filename);
|
|
|
|
emit thumbnailChanged(filename, thumbnail, duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If frame extraction failed, don't show an error image, because we don't want
|
|
|
|
// to penalize users that haven't installed ffmpe. Simply remove this item from
|
|
|
|
// the work-queue.
|
|
|
|
void Thumbnailer::frameExtractionFailed(QString filename, duration_t duration)
|
|
|
|
{
|
|
|
|
// Frame extraction failed, but this was due to ffmpeg not starting
|
|
|
|
// add to the thumbnail cache as a video image with unknown thumbnail.
|
|
|
|
addVideoThumbnailToCache(filename, duration, QImage(), { 0 });
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thumbnailer::frameExtractionInvalid(QString filename, duration_t)
|
|
|
|
{
|
|
|
|
// Frame extraction failed because ffmpeg could not parse the file.
|
|
|
|
// For now, let's mark this as an unknown file. The user may want
|
|
|
|
// to recalculate thumbnails with an updated ffmpeg binary..?
|
|
|
|
addUnknownThumbnailToCache(filename);
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
2018-05-27 12:03:29 +00:00
|
|
|
void Thumbnailer::recalculate(QString filename)
|
|
|
|
{
|
2018-05-15 18:47:35 +00:00
|
|
|
Thumbnail thumbnail = getHashedImage(filename, true);
|
2018-05-27 12:03:29 +00:00
|
|
|
|
|
|
|
// If we couldn't load the image from disk -> leave old thumbnail.
|
|
|
|
// The case "load from web" is a bit inconsistent: it will call into processItem() later
|
|
|
|
// and therefore a "broken" image symbol may be shown.
|
2018-07-08 19:24:44 +00:00
|
|
|
if (thumbnail.type == MEDIATYPE_STILL_LOADING || thumbnail.type == MEDIATYPE_IO_ERROR)
|
2018-05-27 12:03:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
QMutexLocker l(&lock);
|
2018-07-15 15:56:18 +00:00
|
|
|
emit thumbnailChanged(filename, thumbnail.img, thumbnail.duration);
|
2018-05-27 12:03:29 +00:00
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:17:04 +00:00
|
|
|
void Thumbnailer::processItem(QString filename, bool tryDownload)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
2018-05-15 18:47:35 +00:00
|
|
|
Thumbnail thumbnail = getThumbnailFromCache(filename);
|
2018-03-10 13:15:50 +00:00
|
|
|
|
2018-05-15 18:47:35 +00:00
|
|
|
if (thumbnail.img.isNull()) {
|
|
|
|
thumbnail = getHashedImage(filename, tryDownload);
|
|
|
|
if (thumbnail.type == MEDIATYPE_STILL_LOADING)
|
2018-03-11 11:58:55 +00:00
|
|
|
return;
|
|
|
|
|
2018-05-15 18:47:35 +00:00
|
|
|
if (thumbnail.img.isNull()) {
|
|
|
|
thumbnail.img = failImage;
|
2018-03-10 13:15:50 +00:00
|
|
|
} else {
|
2018-03-11 09:19:08 +00:00
|
|
|
int size = maxThumbnailSize();
|
2018-05-15 18:47:35 +00:00
|
|
|
thumbnail.img = thumbnail.img.scaled(size, size, Qt::KeepAspectRatio);
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexLocker l(&lock);
|
2018-07-15 15:56:18 +00:00
|
|
|
emit thumbnailChanged(filename, thumbnail.img, thumbnail.duration);
|
2018-03-10 13:15:50 +00:00
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
2018-03-11 11:58:55 +00:00
|
|
|
void Thumbnailer::imageDownloaded(QString filename)
|
|
|
|
{
|
2018-06-10 14:39:47 +00:00
|
|
|
// Image was downloaded -> try thumbnailing again.
|
2018-03-11 11:58:55 +00:00
|
|
|
QMutexLocker l(&lock);
|
2018-05-15 22:17:04 +00:00
|
|
|
workingOn[filename] = QtConcurrent::run(&pool, [this, filename]() { processItem(filename, false); });
|
2018-03-11 11:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thumbnailer::imageDownloadFailed(QString filename)
|
|
|
|
{
|
2018-07-15 15:56:18 +00:00
|
|
|
emit thumbnailChanged(filename, failImage, duration_t{ 0 });
|
2018-03-11 11:58:55 +00:00
|
|
|
QMutexLocker l(&lock);
|
|
|
|
workingOn.remove(filename);
|
|
|
|
}
|
|
|
|
|
2018-06-30 09:36:37 +00:00
|
|
|
QImage Thumbnailer::fetchThumbnail(const QString &filename)
|
2018-03-10 13:15:50 +00:00
|
|
|
{
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
|
|
|
|
// We are not currently fetching this thumbnail - add it to the list.
|
|
|
|
if (!workingOn.contains(filename)) {
|
|
|
|
workingOn.insert(filename,
|
2018-05-15 22:17:04 +00:00
|
|
|
QtConcurrent::run(&pool, [this, filename]() { processItem(filename, true); }));
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
2018-03-11 09:19:08 +00:00
|
|
|
return dummyImage;
|
2018-03-10 13:15:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 12:03:29 +00:00
|
|
|
void Thumbnailer::calculateThumbnails(const QVector<QString> &filenames)
|
|
|
|
{
|
|
|
|
QMutexLocker l(&lock);
|
|
|
|
for (const QString &filename: filenames) {
|
|
|
|
if (!workingOn.contains(filename)) {
|
|
|
|
workingOn.insert(filename,
|
|
|
|
QtConcurrent::run(&pool, [this, filename]() { recalculate(filename); }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:15:50 +00:00
|
|
|
void Thumbnailer::clearWorkQueue()
|
|
|
|
{
|
2018-07-10 13:04:35 +00:00
|
|
|
// We also want to clear the working-queue of the video-frame-extractor so that
|
|
|
|
// we don't get thumbnails that we don't care about.
|
|
|
|
VideoFrameExtractor::instance()->clearWorkQueue();
|
|
|
|
|
2018-03-10 13:15:50 +00:00
|
|
|
QMutexLocker l(&lock);
|
|
|
|
for (auto it = workingOn.begin(); it != workingOn.end(); ++it)
|
|
|
|
it->cancel();
|
|
|
|
workingOn.clear();
|
|
|
|
}
|
2018-03-11 09:19:08 +00:00
|
|
|
|
|
|
|
static const int maxZoom = 3; // Maximum zoom: thrice of standard size
|
|
|
|
|
|
|
|
int Thumbnailer::defaultThumbnailSize()
|
|
|
|
{
|
|
|
|
return defaultIconMetrics().sz_pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Thumbnailer::maxThumbnailSize()
|
|
|
|
{
|
|
|
|
return defaultThumbnailSize() * maxZoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Thumbnailer::thumbnailSize(double zoomLevel)
|
|
|
|
{
|
|
|
|
// Calculate size of thumbnails. The standard size is defaultIconMetrics().sz_pic.
|
|
|
|
// We use exponential scaling so that the central point is the standard
|
|
|
|
// size and the minimum and maximum extreme points are a third respectively
|
|
|
|
// three times the standard size.
|
|
|
|
// Naturally, these three zoom levels are then represented by
|
|
|
|
// -1.0 (minimum), 0 (standard) and 1.0 (maximum). The actual size is
|
|
|
|
// calculated as standard_size*3.0^zoomLevel.
|
|
|
|
return static_cast<int>(round(defaultThumbnailSize() * pow(maxZoom, zoomLevel)));
|
|
|
|
}
|