lottie_cache.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "lottie/details/lottie_cache.h"
  8. #include "lottie/details/lottie_frame_renderer.h"
  9. #include "ffmpeg/ffmpeg_utility.h"
  10. #include "base/bytes.h"
  11. #include "base/assertion.h"
  12. #include <QDataStream>
  13. #include <QIODevice>
  14. #include <range/v3/numeric/accumulate.hpp>
  15. namespace Lottie {
  16. namespace {
  17. // Must not exceed max database allowed entry size.
  18. constexpr auto kMaxCacheSize = 10 * 1024 * 1024;
  19. } // namespace
  20. Cache::Cache(
  21. const QByteArray &data,
  22. const FrameRequest &request,
  23. FnMut<void(QByteArray &&cached)> put)
  24. : _data(data)
  25. , _put(std::move(put)) {
  26. if (!readHeader(request)) {
  27. _framesReady = 0;
  28. _framesInData = 0;
  29. _data = QByteArray();
  30. }
  31. }
  32. Cache::Cache(Cache &&) = default;
  33. Cache &Cache::operator=(Cache&&) = default;
  34. Cache::~Cache() {
  35. finalizeEncoding();
  36. }
  37. void Cache::init(
  38. QSize original,
  39. int frameRate,
  40. int framesCount,
  41. const FrameRequest &request) {
  42. _size = request.size(original, sizeRounding());
  43. _original = original;
  44. _frameRate = frameRate;
  45. _framesCount = framesCount;
  46. _framesReady = 0;
  47. _framesInData = 0;
  48. prepareBuffers();
  49. }
  50. int Cache::sizeRounding() const {
  51. return 8;
  52. }
  53. int Cache::frameRate() const {
  54. return _frameRate;
  55. }
  56. int Cache::framesReady() const {
  57. return _framesReady;
  58. }
  59. int Cache::framesCount() const {
  60. return _framesCount;
  61. }
  62. QSize Cache::originalSize() const {
  63. return _original;
  64. }
  65. bool Cache::readHeader(const FrameRequest &request) {
  66. if (_data.isEmpty()) {
  67. return false;
  68. }
  69. QDataStream stream(&_data, QIODevice::ReadOnly);
  70. auto encoder = qint32(0);
  71. stream >> encoder;
  72. if (static_cast<Encoder>(encoder) != Encoder::YUV420A4_LZ4) {
  73. return false;
  74. }
  75. auto size = QSize();
  76. auto original = QSize();
  77. auto frameRate = qint32(0);
  78. auto framesCount = qint32(0);
  79. auto framesReady = qint32(0);
  80. stream
  81. >> size
  82. >> original
  83. >> frameRate
  84. >> framesCount
  85. >> framesReady;
  86. if (stream.status() != QDataStream::Ok
  87. || original.isEmpty()
  88. || (original.width() > kMaxSize)
  89. || (original.height() > kMaxSize)
  90. || (frameRate <= 0)
  91. || (frameRate > kNormalFrameRate && frameRate != kMaxFrameRate)
  92. || (framesCount <= 0)
  93. || (framesCount > kMaxFramesCount)
  94. || (framesReady <= 0)
  95. || (framesReady > framesCount)
  96. || request.size(original, sizeRounding()) != size) {
  97. return false;
  98. }
  99. _encoder = static_cast<Encoder>(encoder);
  100. _size = size;
  101. _original = original;
  102. _frameRate = frameRate;
  103. _framesCount = framesCount;
  104. _framesReady = framesReady;
  105. _framesInData = framesReady;
  106. prepareBuffers();
  107. return (renderFrame(_firstFrame, request, 0) == FrameRenderResult::Ok);
  108. }
  109. QImage Cache::takeFirstFrame() {
  110. return std::move(_firstFrame);
  111. }
  112. FrameRenderResult Cache::renderFrame(
  113. QImage &to,
  114. const FrameRequest &request,
  115. int index) {
  116. const auto result = renderFrame(_readContext, to, request, index);
  117. if (result == FrameRenderResult::Ok) {
  118. if (index + 1 == _framesReady && _data.size() > _readContext.offset) {
  119. _data.resize(_readContext.offset);
  120. }
  121. } else if (result == FrameRenderResult::BadCacheSize) {
  122. _framesReady = 0;
  123. _framesInData = 0;
  124. _data.clear();
  125. }
  126. return result;
  127. }
  128. FrameRenderResult Cache::renderFrame(
  129. CacheReadContext &context,
  130. QImage &to,
  131. const FrameRequest &request,
  132. int index) const {
  133. Expects(index >= _framesReady
  134. || index == context.offsetFrameIndex
  135. || index == 0);
  136. Expects(index >= _framesReady || context.ready());
  137. if (index >= _framesReady) {
  138. return FrameRenderResult::NotReady;
  139. } else if (request.size(_original, sizeRounding()) != _size) {
  140. return FrameRenderResult::BadCacheSize;
  141. } else if (index == 0) {
  142. context.offsetFrameIndex = 0;
  143. context.offset = headerSize();
  144. }
  145. const auto [ok, xored] = readCompressedFrame(context);
  146. if (!ok || (xored && index == 0)) {
  147. return FrameRenderResult::Failed;
  148. }
  149. if (xored) {
  150. Xor(context.previous, context.uncompressed);
  151. } else {
  152. std::swap(context.uncompressed, context.previous);
  153. }
  154. Decode(to, context.previous, _size, context.decodeContext);
  155. return FrameRenderResult::Ok;
  156. }
  157. void Cache::appendFrame(
  158. const QImage &frame,
  159. const FrameRequest &request,
  160. int index) {
  161. if (request.size(_original, sizeRounding()) != _size) {
  162. _framesReady = 0;
  163. _framesInData = 0;
  164. _data = QByteArray();
  165. }
  166. if (index != _framesReady) {
  167. return;
  168. } else if (index == 0) {
  169. _size = request.size(_original, sizeRounding());
  170. _encode = EncodeFields();
  171. _encode.compressedFrames.reserve(_framesCount);
  172. prepareBuffers();
  173. }
  174. Assert(frame.size() == _size);
  175. Assert(_readContext.ready());
  176. Encode(_readContext.uncompressed, frame, _encode.cache, _encode.context);
  177. CompressAndSwapFrame(
  178. _encode.compressBuffer,
  179. (index != 0) ? &_encode.xorCompressBuffer : nullptr,
  180. _readContext.uncompressed,
  181. _readContext.previous);
  182. const auto compressed = _encode.compressBuffer;
  183. const auto nowSize = (_data.isEmpty() ? headerSize() : _data.size())
  184. + _encode.totalSize;
  185. const auto totalSize = nowSize + compressed.size();
  186. if (nowSize <= kMaxCacheSize && totalSize > kMaxCacheSize) {
  187. // Write to cache while we still can.
  188. finalizeEncoding();
  189. }
  190. _encode.totalSize += compressed.size();
  191. _encode.compressedFrames.push_back(compressed);
  192. _encode.compressedFrames.back().detach();
  193. ++_readContext.offsetFrameIndex;
  194. _readContext.offset += compressed.size();
  195. if (++_framesReady == _framesCount) {
  196. finalizeEncoding();
  197. }
  198. }
  199. void Cache::finalizeEncoding() {
  200. if (_encode.compressedFrames.empty() || !_put) {
  201. return;
  202. }
  203. const auto size = (_data.isEmpty() ? headerSize() : _data.size())
  204. + _encode.totalSize;
  205. if (_data.isEmpty()) {
  206. _data.reserve(size);
  207. writeHeader();
  208. } else {
  209. updateFramesReadyCount();
  210. }
  211. const auto offset = _data.size();
  212. _data.resize(size);
  213. auto to = _data.data() + offset;
  214. for (const auto &block : _encode.compressedFrames) {
  215. const auto amount = qint32(block.size());
  216. memcpy(to, block.data(), amount);
  217. to += amount;
  218. }
  219. _framesInData = _framesReady;
  220. if (_data.size() <= kMaxCacheSize) {
  221. _put(QByteArray(_data));
  222. }
  223. _encode = EncodeFields();
  224. }
  225. int Cache::headerSize() const {
  226. return 8 * sizeof(qint32);
  227. }
  228. void Cache::writeHeader() {
  229. Expects(_data.isEmpty());
  230. QDataStream stream(&_data, QIODevice::WriteOnly);
  231. stream
  232. << static_cast<qint32>(_encoder)
  233. << _size
  234. << _original
  235. << qint32(_frameRate)
  236. << qint32(_framesCount)
  237. << qint32(_framesReady);
  238. }
  239. void Cache::updateFramesReadyCount() {
  240. Expects(_data.size() >= headerSize());
  241. QDataStream stream(&_data, QIODevice::ReadWrite);
  242. stream.device()->seek(headerSize() - sizeof(qint32));
  243. stream << qint32(_framesReady);
  244. }
  245. void Cache::prepareBuffers() {
  246. prepareBuffers(_readContext);
  247. }
  248. void Cache::prepareBuffers(CacheReadContext &context) const {
  249. if (_size.isEmpty()) {
  250. return;
  251. }
  252. // 12 bit per pixel in YUV420P.
  253. const auto bytesPerLine = _size.width();
  254. context.offset = headerSize();
  255. context.offsetFrameIndex = 0;
  256. context.uncompressed.allocate(bytesPerLine, _size.height());
  257. context.previous.allocate(bytesPerLine, _size.height());
  258. }
  259. void Cache::keepUpContext(CacheReadContext &context) const {
  260. Expects(!context.ready()
  261. || context.previous.size() == _readContext.previous.size());
  262. Expects(!context.ready()
  263. || context.uncompressed.size() == _readContext.uncompressed.size());
  264. Expects(&context != &_readContext);
  265. if (!context.ready()) {
  266. prepareBuffers(context);
  267. }
  268. context.offset = _readContext.offset;
  269. context.offsetFrameIndex = _readContext.offsetFrameIndex;
  270. memcpy(
  271. context.previous.data(),
  272. _readContext.previous.data(),
  273. _readContext.previous.size());
  274. memcpy(
  275. context.uncompressed.data(),
  276. _readContext.uncompressed.data(),
  277. _readContext.uncompressed.size());
  278. }
  279. Cache::ReadResult Cache::readCompressedFrame(
  280. CacheReadContext &context) const {
  281. Expects(context.ready());
  282. auto length = qint32(0);
  283. const auto part = [&] {
  284. if (context.offsetFrameIndex >= _framesInData) {
  285. // One reader is still accumulating compressed frames,
  286. // while second reader already started reading after the first.
  287. const auto readyIndex = context.offsetFrameIndex - _framesInData;
  288. Assert(readyIndex < _encode.compressedFrames.size());
  289. return bytes::make_span(_encode.compressedFrames[readyIndex]);
  290. } else if (_data.size() > context.offset) {
  291. return bytes::make_span(_data).subspan(context.offset);
  292. } else {
  293. return bytes::const_span();
  294. }
  295. }();
  296. if (part.size() < sizeof(length)) {
  297. return { false };
  298. }
  299. bytes::copy(
  300. bytes::object_as_span(&length),
  301. part.subspan(0, sizeof(length)));
  302. const auto bytes = part.subspan(sizeof(length));
  303. const auto xored = (length < 0);
  304. if (xored) {
  305. length = -length;
  306. }
  307. const auto ok = (length <= bytes.size())
  308. ? UncompressToRaw(context.uncompressed, bytes.subspan(0, length))
  309. : false;
  310. if (ok) {
  311. context.offset += sizeof(length) + length;
  312. ++context.offsetFrameIndex;
  313. }
  314. return { ok, xored };
  315. }
  316. } // namespace Lottie