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/
|
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
#include "include/aegisub/video_provider.h"
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2012-01-18 21:08:42 +01:00
|
|
|
#include "video_frame.h"
|
|
|
|
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2014-03-24 15:05:01 +01:00
|
|
|
|
|
|
|
#include <list>
|
2012-02-08 00:16:41 +01:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
namespace {
|
2012-02-08 00:16:41 +01:00
|
|
|
/// A video frame and its frame number
|
2014-06-12 23:34:21 +02:00
|
|
|
struct CachedFrame {
|
|
|
|
VideoFrame frame;
|
2012-02-08 00:16:41 +01:00
|
|
|
int frame_number;
|
2013-07-01 05:15:43 +02:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
CachedFrame(VideoFrame const& frame, int frame_number)
|
|
|
|
: frame(frame), frame_number(frame_number) { }
|
|
|
|
|
|
|
|
CachedFrame(CachedFrame const&) = delete;
|
2010-07-08 06:29:04 +02:00
|
|
|
};
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
/// @class VideoProviderCache
|
|
|
|
/// @brief A wrapper around a video provider which provides LRU caching
|
|
|
|
class VideoProviderCache final : public VideoProvider {
|
|
|
|
/// The source provider to get frames from
|
|
|
|
std::unique_ptr<VideoProvider> master;
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
/// @brief Maximum size of the cache in bytes
|
|
|
|
///
|
|
|
|
/// Note that this is a soft limit. The cache stops allocating new frames
|
|
|
|
/// once it has exceeded the limit, but it never tries to shrink
|
2014-06-12 23:34:21 +02:00
|
|
|
const size_t max_cache_size = OPT_GET("Provider/Video/Cache/Size")->GetInt() << 20; // convert MB to bytes
|
2014-03-24 15:05:01 +01:00
|
|
|
|
|
|
|
/// Cache of video frames with the most recently used ones at the front
|
|
|
|
std::list<CachedFrame> cache;
|
|
|
|
|
|
|
|
public:
|
2014-06-12 23:34:21 +02:00
|
|
|
VideoProviderCache(std::unique_ptr<VideoProvider> master) : master(std::move(master)) { }
|
2014-03-24 15:05:01 +01:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
void GetFrame(int n, VideoFrame &frame) override;
|
2014-03-24 15:05:01 +01:00
|
|
|
|
2014-05-20 04:21:50 +02:00
|
|
|
void SetColorSpace(std::string const& m) override {
|
|
|
|
cache.clear();
|
|
|
|
return master->SetColorSpace(m);
|
|
|
|
}
|
|
|
|
|
2014-03-24 15:05:01 +01:00
|
|
|
int GetFrameCount() const override { return master->GetFrameCount(); }
|
|
|
|
int GetWidth() const override { return master->GetWidth(); }
|
|
|
|
int GetHeight() const override { return master->GetHeight(); }
|
|
|
|
double GetDAR() const override { return master->GetDAR(); }
|
|
|
|
agi::vfr::Framerate GetFPS() const override { return master->GetFPS(); }
|
|
|
|
std::vector<int> GetKeyFrames() const override { return master->GetKeyFrames(); }
|
|
|
|
std::string GetWarning() const override { return master->GetWarning(); }
|
|
|
|
std::string GetDecoderName() const override { return master->GetDecoderName(); }
|
|
|
|
std::string GetColorSpace() const override { return master->GetColorSpace(); }
|
2014-05-20 01:47:54 +02:00
|
|
|
std::string GetRealColorSpace() const override { return master->GetRealColorSpace(); }
|
2014-05-23 15:34:52 +02:00
|
|
|
bool ShouldSetVideoProperties() const override { return master->ShouldSetVideoProperties(); }
|
|
|
|
bool HasAudio() const override { return master->HasAudio(); }
|
2014-03-24 15:05:01 +01:00
|
|
|
};
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
void VideoProviderCache::GetFrame(int n, VideoFrame &out) {
|
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) {
|
2013-07-01 05:15:43 +02:00
|
|
|
cache.splice(cache.begin(), cache, cur); // Move to front
|
2014-06-12 23:34:21 +02:00
|
|
|
out = cache.front().frame;
|
|
|
|
return;
|
2008-03-06 20:20:25 +01:00
|
|
|
}
|
2012-02-08 00:16:41 +01:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
total_size += cur->frame.data.size();
|
2008-03-06 20:20:25 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
master->GetFrame(n, out);
|
2008-03-06 20:20:25 +01:00
|
|
|
|
2014-06-12 23:34:21 +02:00
|
|
|
if (total_size >= max_cache_size) {
|
|
|
|
cache.splice(cache.begin(), cache, --cache.end()); // Move last to front
|
|
|
|
cache.front().frame_number = n;
|
|
|
|
cache.front().frame = out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cache.emplace_front(out, n);
|
2009-11-29 19:59:21 +01:00
|
|
|
}
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<VideoProvider> CreateCacheVideoProvider(std::unique_ptr<VideoProvider> parent) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<VideoProviderCache>(std::move(parent));
|
2014-03-24 15:05:01 +01:00
|
|
|
}
|