2013-07-01 05:15:43 +02:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2008-03-06 20:20:25 +01:00
|
|
|
//
|
2013-07-01 05:15:43 +02:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2008-03-06 20:20:25 +01:00
|
|
|
//
|
2013-07-01 05:15:43 +02:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2008-03-06 20:20:25 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-03-06 20:20:25 +01:00
|
|
|
#include "video_provider_cache.h"
|
|
|
|
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2012-01-18 21:08:42 +01:00
|
|
|
#include "video_frame.h"
|
|
|
|
|
2012-02-08 00:16:41 +01:00
|
|
|
#include <algorithm>
|
2013-12-12 04:36:29 +01:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
|
|
|
#if BOOST_VERSION <= 105200
|
|
|
|
// Compilation fails without this with boost 1.52. I have no idea why.
|
|
|
|
static bool operator==(VideoFrame const& a, VideoFrame const& b) {
|
|
|
|
return a.width == b.width
|
|
|
|
&& a.height == b.height
|
|
|
|
&& a.pitch == b.pitch
|
|
|
|
&& a.flipped == b.flipped
|
|
|
|
&& a.data == b.data;
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-08 00:16:41 +01:00
|
|
|
|
|
|
|
/// A video frame and its frame number
|
2014-03-13 02:39:07 +01:00
|
|
|
struct CachedFrame final : public VideoFrame {
|
2012-02-08 00:16:41 +01:00
|
|
|
int frame_number;
|
2013-07-01 05:15:43 +02:00
|
|
|
|
|
|
|
CachedFrame(int frame_number, VideoFrame const& frame)
|
|
|
|
: VideoFrame(frame.data.data(), frame.width, frame.height, frame.pitch, frame.flipped)
|
|
|
|
, frame_number(frame_number)
|
|
|
|
{
|
|
|
|
}
|
2010-07-08 06:29:04 +02:00
|
|
|
};
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
VideoProviderCache::VideoProviderCache(std::unique_ptr<VideoProvider> parent)
|
2013-06-08 06:19:40 +02:00
|
|
|
: master(std::move(parent))
|
2012-02-08 00:16:41 +01:00
|
|
|
, max_cache_size(OPT_GET("Provider/Video/Cache/Size")->GetInt() << 20) // convert MB to bytes
|
2010-07-08 06:29:04 +02:00
|
|
|
{
|
2008-03-06 20:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoProviderCache::~VideoProviderCache() {
|
|
|
|
}
|
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
std::shared_ptr<VideoFrame> VideoProviderCache::GetFrame(int n) {
|
2012-02-08 00:16:41 +01:00
|
|
|
size_t total_size = 0;
|
|
|
|
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto cur = cache.begin(); cur != cache.end(); ++cur) {
|
2012-02-08 00:16:41 +01:00
|
|
|
if (cur->frame_number == n) {
|
2014-02-02 18:14:33 +01:00
|
|
|
#if BOOST_VERSION <= 105200
|
|
|
|
// Until boost 1.52, boost::container::list incorrectly asserted
|
|
|
|
// that this != &other, so do an extra splice through an empty list
|
|
|
|
decltype(cache) temp;
|
|
|
|
temp.splice(temp.begin(), cache, cur);
|
|
|
|
cache.splice(cache.begin(), temp, temp.begin());
|
|
|
|
#else
|
2013-07-01 05:15:43 +02:00
|
|
|
cache.splice(cache.begin(), cache, cur); // Move to front
|
2014-02-02 18:14:33 +01:00
|
|
|
#endif
|
2013-07-01 05:15:43 +02:00
|
|
|
return std::make_shared<VideoFrame>(cache.front());
|
2008-03-06 20:20:25 +01:00
|
|
|
}
|
2012-02-08 00:16:41 +01:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
total_size += cur->data.size();
|
2008-03-06 20:20:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
auto frame = master->GetFrame(n);
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
if (total_size >= max_cache_size)
|
2012-02-08 00:16:41 +01:00
|
|
|
cache.pop_back();
|
2013-07-01 05:15:43 +02:00
|
|
|
cache.emplace_front(n, *frame);
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2013-07-01 05:15:43 +02:00
|
|
|
return frame;
|
2009-11-29 19:59:21 +01:00
|
|
|
}
|