Skip to content

Commit

Permalink
Increase extraSourceBuffer faster in case of drought
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
CrendKing committed Jul 2, 2021
1 parent 567f713 commit e574b1a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
1 change: 0 additions & 1 deletion avisynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FrameHandler {
auto BeginFlush() -> void;
auto EndFlush(const std::function<void ()> &interim) -> void;
auto StartWorker() -> void;
auto TerminalFlush() -> void;
auto GetInputBufferSize() const -> int;
constexpr auto GetSourceFrameNb() const -> int { return _nextSourceFrameNb; }
constexpr auto GetOutputFrameNb() const -> int { return _nextOutputFrameNb; }
Expand Down
2 changes: 1 addition & 1 deletion filter_common/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static const GUID MEDIASUBTYPE_YV24 = FOURCCMap('
// interface version 7 = AviSynth+ 3.5
static constexpr const int MINIMUM_AVISYNTH_PLUS_INTERFACE_VERSION = 7;

static constexpr const double EXTRA_SOURCE_BUFFER_CHANGE_THRESHOLD = 0.05;
static constexpr const double EXTRA_SOURCE_BUFFER_CHANGE_THRESHOLD = 0.1;
static constexpr const int MAXIMUM_EXTRA_SOURCE_BUFFER = 14;

/*
Expand Down
22 changes: 7 additions & 15 deletions filter_common/src/frame_handler_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ FrameHandler::FrameHandler(CSynthFilter &filter)
FrameHandler::~FrameHandler() {
if (_workerThread.joinable()) {
_isStopping = true;
TerminalFlush();

BeginFlush();
EndFlush([]() -> void {
MainFrameServer::GetInstance().StopScript();
});

_workerThread.join();
}
}
Expand All @@ -29,25 +34,12 @@ auto FrameHandler::StartWorker() -> void {
}
}

auto FrameHandler::TerminalFlush() -> void {
BeginFlush();
EndFlush([]() -> void {
/*
* Stop the script after worker threads are paused and before flushing is done so that no new frame request (GetSourceFrame()) happens.
* And since _isFlushing is still on, existing frame request should also just drain instead of block.
*
* If no stop here, since AddInputSample() no longer adds frame, existing GetSourceFrame() calls will stuck forever.
*/
MainFrameServer::GetInstance().StopScript();
});
}

auto FrameHandler::UpdateExtraSourceBuffer() -> void {
if (const int sourceAvgFps = MainFrameServer::GetInstance().GetSourceAvgFrameRate();
_currentInputFrameRate > 0 && _nextSourceFrameNb % (sourceAvgFps / FRAME_RATE_SCALE_FACTOR) == 0) {
if (const double ratio = static_cast<double>(_currentInputFrameRate) / sourceAvgFps;
ratio < 1 - EXTRA_SOURCE_BUFFER_CHANGE_THRESHOLD) {
_extraSourceBuffer = min(_extraSourceBuffer, MAXIMUM_EXTRA_SOURCE_BUFFER) + 1;
_extraSourceBuffer = min(_extraSourceBuffer, MAXIMUM_EXTRA_SOURCE_BUFFER) + 2;
} else if (ratio > 1 + EXTRA_SOURCE_BUFFER_CHANGE_THRESHOLD) {
_extraSourceBuffer = max(_extraSourceBuffer, 1) - 1;
}
Expand Down
6 changes: 5 additions & 1 deletion filter_common/src/input_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ auto CSynthFilterInputPin::Active() -> HRESULT {

auto CSynthFilterInputPin::Inactive() -> HRESULT {
_filter._isReadyToReceive = false;
_filter.frameHandler->TerminalFlush();

_filter.frameHandler->BeginFlush();
_filter.frameHandler->EndFlush([this]() -> void {
MainFrameServer::GetInstance().ReloadScript(_filter.m_pInput->CurrentMediaType(), true);
});

return __super::Inactive();
}
Expand Down
2 changes: 1 addition & 1 deletion filter_common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace SynthFilter {
#define FILTER_NAME_WIDE WIDEN(FILTER_NAME_BASE)
#define FILTER_VERSION_MAJOR 1
#define FILTER_VERSION_MINOR 1
#define FILTER_VERSION_PATCH 4
#define FILTER_VERSION_PATCH 5
#define FILTER_VERSION_STRING STRINGIZE(FILTER_VERSION_MAJOR) "." STRINGIZE(FILTER_VERSION_MINOR) "." STRINGIZE(FILTER_VERSION_PATCH) FILTER_VARIANT " # " FILTER_GIT_HASH
}
4 changes: 3 additions & 1 deletion vapoursynth_filter/src/frame_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ auto FrameHandler::PrepareOutputSample(ATL::CComPtr<IMediaSample> &sample, int o
Format::WriteSample(_filter._outputVideoFormat, outputFrame, outputBuffer);

if (const ATL::CComQIPtr<IMediaSideData> outputSampleSideData(sample); outputSampleSideData != nullptr) {
_sourceFrames[sourceFrameNb].hdrSideData->WriteTo(outputSampleSideData);
const decltype(_sourceFrames)::const_iterator iter = _sourceFrames.find(sourceFrameNb);
ASSERT(iter != _sourceFrames.end());
iter->second.hdrSideData->WriteTo(outputSampleSideData);
}

RefreshOutputFrameRates(outputFrameNb);
Expand Down
1 change: 0 additions & 1 deletion vapoursynth_filter/src/frame_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FrameHandler {
auto BeginFlush() -> void;
auto EndFlush(const std::function<void ()> &interim) -> void;
auto StartWorker() -> void;
auto TerminalFlush() -> void;
auto GetInputBufferSize() const -> int;
constexpr auto GetSourceFrameNb() const -> int { return _nextSourceFrameNb; }
constexpr auto GetOutputFrameNb() const -> int { return _nextOutputFrameNb; }
Expand Down

0 comments on commit e574b1a

Please sign in to comment.