tdesktop/Telegram/SourceFiles/lottie/lottie_cache.cpp

417 lines
10 KiB
C++
Raw Normal View History

2019-06-26 14:18:00 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "lottie/lottie_cache.h"
#include "lottie/lottie_frame_renderer.h"
2019-06-26 15:04:38 +00:00
#include "ffmpeg/ffmpeg_utility.h"
2019-06-26 14:18:00 +00:00
#include "base/bytes.h"
#include <QDataStream>
2019-06-27 09:57:48 +00:00
#include <lz4.h>
#include <range/v3/numeric/accumulate.hpp>
2019-06-26 14:18:00 +00:00
namespace Lottie {
namespace {
constexpr auto kAlignStorage = 16;
bool UncompressToRaw(AlignedStorage &to, bytes::const_span from) {
if (from.empty() || from.size() > to.rawSize()) {
return false;
} else if (from.size() == to.rawSize()) {
memcpy(to.raw(), from.data(), from.size());
return true;
}
2019-06-27 09:57:48 +00:00
const auto result = LZ4_decompress_safe(
reinterpret_cast<const char*>(from.data()),
static_cast<char*>(to.raw()),
from.size(),
to.rawSize());
return (result == to.rawSize());
2019-06-26 14:18:00 +00:00
}
2019-06-26 17:14:46 +00:00
void CompressFromRaw(QByteArray &to, const AlignedStorage &from) {
2019-06-27 09:57:48 +00:00
const auto size = from.rawSize();
const auto max = LZ4_compressBound(size);
to.reserve(max);
to.resize(max);
const auto compressed = LZ4_compress_default(
static_cast<const char*>(from.raw()),
to.data(),
size,
to.size());
Assert(compressed > 0);
if (compressed >= size) {
to.resize(size);
memcpy(to.data(), from.raw(), size);
} else {
to.resize(compressed);
}
2019-06-26 17:14:46 +00:00
}
void Decode(QImage &to, AlignedStorage &from, const QSize &fromSize) {
from.copyRawToAligned();
2019-06-26 15:04:38 +00:00
if (!FFmpeg::GoodStorageForFrame(to, fromSize)) {
to = FFmpeg::CreateFrameStorage(fromSize);
}
2019-06-26 14:18:00 +00:00
auto fromBytes = static_cast<const char*>(from.aligned());
auto toBytes = to.bits();
const auto fromPerLine = from.bytesPerLine();
const auto toPerLine = to.bytesPerLine();
for (auto i = 0; i != to.height(); ++i) {
memcpy(toBytes, fromBytes, to.width() * 4);
fromBytes += fromPerLine;
toBytes += toPerLine;
}
}
2019-06-26 17:14:46 +00:00
void Encode(AlignedStorage &to, const QImage &from, const QSize &toSize) {
auto fromBytes = from.bits();
auto toBytes = static_cast<char*>(to.aligned());
const auto fromPerLine = from.bytesPerLine();
const auto toPerLine = to.bytesPerLine();
for (auto i = 0; i != to.lines(); ++i) {
memcpy(toBytes, fromBytes, from.width() * 4);
fromBytes += fromPerLine;
toBytes += toPerLine;
}
to.copyAlignedToRaw();
}
void Xor(AlignedStorage &to, const AlignedStorage &from) {
Expects(to.rawSize() == from.rawSize());
using Block = std::conditional_t<
sizeof(void*) == sizeof(uint64),
uint64,
uint32>;
constexpr auto kBlockSize = sizeof(Block);
const auto amount = from.rawSize();
const auto fromBytes = reinterpret_cast<const uchar*>(from.raw());
const auto toBytes = reinterpret_cast<uchar*>(to.raw());
const auto skip = reinterpret_cast<quintptr>(toBytes) % kBlockSize;
const auto blocks = (amount - skip) / kBlockSize;
for (auto i = 0; i != skip; ++i) {
toBytes[i] ^= fromBytes[i];
}
const auto fromBlocks = reinterpret_cast<const Block*>(fromBytes + skip);
const auto toBlocks = reinterpret_cast<Block*>(toBytes + skip);
for (auto i = 0; i != blocks; ++i) {
toBlocks[i] ^= fromBlocks[i];
}
const auto left = amount - skip - (blocks * kBlockSize);
for (auto i = amount - left; i != amount; ++i) {
toBytes[i] ^= fromBytes[i];
}
}
2019-06-26 14:18:00 +00:00
} // namespace
void AlignedStorage::allocate(int packedBytesPerLine, int lines) {
Expects(packedBytesPerLine >= 0);
Expects(lines >= 0);
_packedBytesPerLine = packedBytesPerLine;
_lines = lines;
reallocate();
}
void AlignedStorage::reallocate() {
const auto perLine = bytesPerLine();
const auto total = perLine * _lines;
_buffer = QByteArray(total + kAlignStorage - 1, Qt::Uninitialized);
_raw = (perLine != _packedBytesPerLine)
? QByteArray(_packedBytesPerLine * _lines, Qt::Uninitialized)
: QByteArray();
}
int AlignedStorage::lines() const {
return _lines;
}
int AlignedStorage::rawSize() const {
return _lines * _packedBytesPerLine;
}
void *AlignedStorage::raw() {
return (bytesPerLine() == _packedBytesPerLine) ? aligned() : _raw.data();
}
const void *AlignedStorage::raw() const {
return (bytesPerLine() == _packedBytesPerLine) ? aligned() : _raw.data();
}
int AlignedStorage::bytesPerLine() const {
return kAlignStorage
* ((_packedBytesPerLine + kAlignStorage - 1) / kAlignStorage);
}
void *AlignedStorage::aligned() {
const auto result = reinterpret_cast<quintptr>(_buffer.data());
return reinterpret_cast<void*>(kAlignStorage
* ((result + kAlignStorage - 1) / kAlignStorage));
}
const void *AlignedStorage::aligned() const {
const auto result = reinterpret_cast<quintptr>(_buffer.data());
return reinterpret_cast<void*>(kAlignStorage
* ((result + kAlignStorage - 1) / kAlignStorage));
}
void AlignedStorage::copyRawToAligned() {
const auto fromPerLine = _packedBytesPerLine;
const auto toPerLine = bytesPerLine();
if (fromPerLine == toPerLine) {
return;
}
2019-06-26 17:14:46 +00:00
auto from = static_cast<const char*>(raw());
2019-06-26 14:18:00 +00:00
auto to = static_cast<char*>(aligned());
for (auto i = 0; i != _lines; ++i) {
2019-06-26 17:14:46 +00:00
memcpy(to, from, fromPerLine);
2019-06-26 14:18:00 +00:00
from += fromPerLine;
to += toPerLine;
}
}
void AlignedStorage::copyAlignedToRaw() {
const auto fromPerLine = bytesPerLine();
const auto toPerLine = _packedBytesPerLine;
if (fromPerLine == toPerLine) {
return;
}
2019-06-26 17:14:46 +00:00
auto from = static_cast<const char*>(aligned());
2019-06-26 14:18:00 +00:00
auto to = static_cast<char*>(raw());
for (auto i = 0; i != _lines; ++i) {
2019-06-26 17:14:46 +00:00
memcpy(to, from, toPerLine);
2019-06-26 14:18:00 +00:00
from += fromPerLine;
to += toPerLine;
}
}
2019-06-26 17:14:46 +00:00
CacheState::CacheState(const QByteArray &data, const FrameRequest &request)
2019-06-26 14:18:00 +00:00
: _data(data) {
2019-06-26 17:14:46 +00:00
if (!readHeader(request)) {
2019-06-26 14:18:00 +00:00
_framesReady = 0;
2019-06-26 17:14:46 +00:00
_data = QByteArray();
2019-06-26 14:18:00 +00:00
}
}
2019-06-26 17:14:46 +00:00
void CacheState::init(
QSize original,
int frameRate,
int framesCount,
const FrameRequest &request) {
_size = request.size(original);
_original = original;
_frameRate = frameRate;
_framesCount = framesCount;
_framesReady = 0;
prepareBuffers();
}
2019-06-26 14:18:00 +00:00
int CacheState::frameRate() const {
return _frameRate;
}
int CacheState::framesReady() const {
return _framesReady;
}
int CacheState::framesCount() const {
return _framesCount;
}
2019-06-26 17:14:46 +00:00
QSize CacheState::originalSize() const {
return _original;
}
bool CacheState::readHeader(const FrameRequest &request) {
2019-06-26 14:18:00 +00:00
if (_data.isEmpty()) {
return false;
}
QDataStream stream(&_data, QIODevice::ReadOnly);
2019-06-26 17:14:46 +00:00
auto encoder = quint8(0);
2019-06-26 14:18:00 +00:00
stream >> encoder;
if (static_cast<Encoder>(encoder) != Encoder::YUV420A4_LZ4) {
return false;
}
auto size = QSize();
auto original = QSize();
auto frameRate = qint32(0);
auto framesCount = qint32(0);
auto framesReady = qint32(0);
stream
>> size
>> original
>> frameRate
>> framesCount
>> framesReady;
if (stream.status() != QDataStream::Ok
|| original.isEmpty()
|| (original.width() > kMaxSize)
|| (original.height() > kMaxSize)
|| (frameRate <= 0)
|| (frameRate > kMaxFrameRate)
|| (framesCount <= 0)
|| (framesCount > kMaxFramesCount)
|| (framesReady <= 0)
|| (framesReady > framesCount)
2019-06-26 17:14:46 +00:00
|| request.size(original) != size) {
2019-06-26 14:18:00 +00:00
return false;
}
_size = size;
_original = original;
_frameRate = frameRate;
_framesCount = framesCount;
_framesReady = framesReady;
prepareBuffers();
2019-06-26 17:14:46 +00:00
return renderFrame(_firstFrame, request, 0);
}
QImage CacheState::takeFirstFrame() {
return std::move(_firstFrame);
}
bool CacheState::renderFrame(
QImage &to,
const FrameRequest &request,
int index) {
Expects(index >= _framesReady
|| index == _offsetFrameIndex
|| index == 0);
if (index >= _framesReady) {
return false;
} else if (request.size(_original) != _size) {
return false;
} else if (index == 0) {
2019-06-27 09:57:48 +00:00
_offset = headerSize();
2019-06-26 17:14:46 +00:00
_offsetFrameIndex = 0;
}
if (!readCompressedDelta()) {
_framesReady = 0;
_data = QByteArray();
2019-06-26 14:18:00 +00:00
return false;
}
2019-06-26 17:14:46 +00:00
if (index == 0) {
std::swap(_uncompressed, _previous);
} else {
Xor(_previous, _uncompressed);
}
Decode(to, _previous, _size);
2019-06-26 14:18:00 +00:00
return true;
}
2019-06-26 17:14:46 +00:00
void CacheState::appendFrame(
const QImage &frame,
const FrameRequest &request,
int index) {
if (request.size(_original) != _size) {
_framesReady = 0;
_data = QByteArray();
}
if (index != _framesReady) {
return;
}
if (index == 0) {
_size = request.size(_original);
2019-06-27 09:57:48 +00:00
_compressedFrames.reserve(_framesCount);
2019-06-26 17:14:46 +00:00
prepareBuffers();
}
Encode(_uncompressed, frame, _size);
if (index == 0) {
writeCompressedDelta();
std::swap(_uncompressed, _previous);
} else {
std::swap(_uncompressed, _previous);
Xor(_uncompressed, _previous);
writeCompressedDelta();
}
2019-06-27 09:57:48 +00:00
if (++_framesReady == _framesCount) {
finalizeEncoding();
}
}
void CacheState::finalizeEncoding() {
const auto size = (_data.isEmpty() ? headerSize() : _data.size())
+ (_compressedFrames.size() * sizeof(qint32))
+ ranges::accumulate(
_compressedFrames,
0,
std::plus(),
&QByteArray::size);
if (_data.isEmpty()) {
_data.reserve(size);
writeHeader();
}
const auto offset = _data.size();
_data.resize(size);
auto to = _data.data() + offset;
for (const auto &block : _compressedFrames) {
const auto amount = qint32(block.size());
memcpy(to, &amount, sizeof(qint32));
to += sizeof(qint32);
memcpy(to, block.data(), amount);
to += amount;
}
_compressedFrames.clear();
_compressedFrames.shrink_to_fit();
_compressBuffer = QByteArray();
}
int CacheState::headerSize() const {
return 8 * sizeof(qint32);
2019-06-26 14:18:00 +00:00
}
2019-06-26 17:14:46 +00:00
void CacheState::writeHeader() {
Expects(_data.isEmpty());
QDataStream stream(&_data, QIODevice::WriteOnly);
stream
2019-06-27 09:57:48 +00:00
<< static_cast<qint32>(Encoder::YUV420A4_LZ4)
2019-06-26 17:14:46 +00:00
<< _size
<< _original
<< qint32(_frameRate)
<< qint32(_framesCount)
2019-06-27 09:57:48 +00:00
<< qint32(_framesReady);
2019-06-26 14:18:00 +00:00
}
2019-06-26 17:14:46 +00:00
void CacheState::writeCompressedDelta() {
2019-06-27 09:57:48 +00:00
CompressFromRaw(_compressBuffer, _uncompressed);
_compressedFrames.push_back(_compressBuffer);
_compressedFrames.back().detach();
2019-06-26 17:14:46 +00:00
}
void CacheState::prepareBuffers() {
_uncompressed.allocate(_size.width() * 4, _size.height());
_previous.allocate(_size.width() * 4, _size.height());
2019-06-26 14:18:00 +00:00
}
2019-06-26 17:14:46 +00:00
bool CacheState::readCompressedDelta() {
2019-06-26 14:18:00 +00:00
auto length = qint32(0);
2019-06-26 17:14:46 +00:00
const auto part = bytes::make_span(_data).subspan(_offset);
2019-06-26 14:18:00 +00:00
if (part.size() < sizeof(length)) {
return false;
}
2019-06-26 17:14:46 +00:00
bytes::copy(
bytes::object_as_span(&length),
part.subspan(0, sizeof(length)));
2019-06-26 14:18:00 +00:00
const auto bytes = part.subspan(sizeof(length));
2019-06-26 17:14:46 +00:00
_offset += sizeof(length) + length;
++_offsetFrameIndex;
2019-06-26 14:18:00 +00:00
return (length <= bytes.size())
? UncompressToRaw(_uncompressed, bytes.subspan(0, length))
: false;
}
} // namespace Lottie