Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
CrendKing committed Aug 15, 2020
1 parent d25bf00 commit 3fce017
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
27 changes: 14 additions & 13 deletions avisynth_filter/src/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ auto CAviSynthFilter::Transform(IMediaSample *pIn, IMediaSample *pOut) -> HRESUL
Log("late: %10i timePerFrame: %lli streamTime: %10lli streamFrameNb: %4lli sampleTime: %10lli sampleFrameNb: %4i",
m_itrLate, _timePerFrame, static_cast<REFERENCE_TIME>(streamTime), static_cast<REFERENCE_TIME>(streamTime) / _timePerFrame, inputStartTime, inSampleFrameNb);

if (_reloadAvsFile && !reloadedAvsForTypeChange) {
ReloadAviSynth(m_pInput->CurrentMediaType());
}

if (_reloadAvsFile || reloadedAvsForTypeChange) {
if (_reloadAvsFile) {
if (!reloadedAvsForTypeChange) {
ReloadAviSynth(m_pInput->CurrentMediaType());
}
_deliveryFrameNb = inSampleFrameNb;
_reloadAvsFile = false;
}

const REFERENCE_TIME gcMinTime = (static_cast<REFERENCE_TIME>(_deliveryFrameNb) - _bufferBack) * _timePerFrame;
Expand Down Expand Up @@ -501,8 +501,9 @@ auto CAviSynthFilter::GenerateMediaType(int definition, const AM_MEDIA_TYPE *tem

if (SUCCEEDED(CheckVideoInfo2Type(newMediaType))) {
VIDEOINFOHEADER2 *newVih2 = reinterpret_cast<VIDEOINFOHEADER2 *>(newMediaType->pbFormat);
newVih2->dwPictAspectRatioX = _avsScriptVideoInfo.width;
newVih2->dwPictAspectRatioY = _avsScriptVideoInfo.height;
const int gcd = std::gcd(_avsScriptVideoInfo.width, _avsScriptVideoInfo.height);
newVih2->dwPictAspectRatioX = _avsScriptVideoInfo.width / gcd;
newVih2->dwPictAspectRatioY = _avsScriptVideoInfo.height / gcd;
newBmi = &newVih2->bmiHeader;
} else {
newBmi = &newVih->bmiHeader;
Expand Down Expand Up @@ -534,7 +535,7 @@ auto CAviSynthFilter::GenerateMediaType(int definition, const AM_MEDIA_TYPE *tem
* Whenever output type is change, only take the new width for stride size.
* Whenever the output type is effectively changed, return true for notifying downstream the change.
*
* returns pair of bools: <received_new_input_type, notify_new_output_type>
* returns pair of bools: <reloaded_avs_for_type_change, notify_new_output_type>
*/
auto CAviSynthFilter::HandleMediaTypeChange(IMediaSample *pIn, IMediaSample *pOut) -> std::pair<bool, bool> {
bool reloadedAvsForTypeChange = false;
Expand Down Expand Up @@ -563,8 +564,8 @@ auto CAviSynthFilter::HandleMediaTypeChange(IMediaSample *pIn, IMediaSample *pOu

if (hasNewInputType || hasNewOutputType) {
ReloadAviSynth(m_pInput->CurrentMediaType());
replaceOutputType = GenerateMediaType(Format::LookupAvsType(_avsScriptVideoInfo.pixel_type)[0], &m_pInput->CurrentMediaType());
reloadedAvsForTypeChange = true;
replaceOutputType = GenerateMediaType(Format::LookupAvsType(_avsScriptVideoInfo.pixel_type)[0], &m_pInput->CurrentMediaType());
}

if (hasNewOutputType) {
Expand All @@ -580,16 +581,17 @@ auto CAviSynthFilter::HandleMediaTypeChange(IMediaSample *pIn, IMediaSample *pOu
}

if (replaceOutputType != nullptr) {
m_pOutput->SetMediaType(reinterpret_cast<CMediaType *>(replaceOutputType));
DeleteMediaType(replaceOutputType);

const Format::VideoFormat replaceOutputFormat = Format::GetVideoFormat(m_pOutput->CurrentMediaType());

if (_outputFormat != replaceOutputFormat) {
Log("new output type: definition %i, width %5i, height %5i, codec %#10x",
replaceOutputFormat.definition, replaceOutputFormat.bmi.biWidth, replaceOutputFormat.bmi.biHeight, replaceOutputFormat.bmi.biCompression);
_outputFormat = replaceOutputFormat;
m_pOutput->SetMediaType(reinterpret_cast<CMediaType *>(replaceOutputType));
notifyNewOutputType = true;
}

DeleteMediaType(replaceOutputType);
}

return { reloadedAvsForTypeChange, notifyNewOutputType };
Expand Down Expand Up @@ -683,7 +685,6 @@ auto CAviSynthFilter::ReloadAviSynth(const AM_MEDIA_TYPE &mediaType) -> bool {
_avsScriptClip = invokeResult.AsClip();
_avsScriptVideoInfo = _avsScriptClip->GetVideoInfo();
_timePerFrame = llMulDiv(_avsScriptVideoInfo.fps_denominator, UNITS, _avsScriptVideoInfo.fps_numerator, 0);
_reloadAvsFile = false;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion avisynth_filter/src/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CAviSynthFilter
auto LoadSettings() -> void;
auto GetInputDefinition(const AM_MEDIA_TYPE *mediaType) const -> int;
auto GenerateMediaType(int definition, const AM_MEDIA_TYPE *templateMediaType) const -> AM_MEDIA_TYPE *;
auto HandleMediaTypeChange(IMediaSample *pIn, IMediaSample *pOut) ->std::pair<bool, bool>;
auto HandleMediaTypeChange(IMediaSample *pIn, IMediaSample *pOut) -> std::pair<bool, bool>;
auto DeletePinTypes() -> void;
auto CreateAviSynth() -> void;
auto ReloadAviSynth(const AM_MEDIA_TYPE &mediaType) -> bool;
Expand Down
1 change: 1 addition & 0 deletions avisynth_filter/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mutex>
#include <string>
#include <thread>
#include <numeric>
#include <unordered_map>
#include <vector>

Expand Down

0 comments on commit 3fce017

Please sign in to comment.