Skip to content

Commit

Permalink
Don't use CFR mode if forced framerate is same as the source file
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Feb 27, 2024
1 parent 30b9747 commit 1f96be3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
34 changes: 20 additions & 14 deletions src/avisynth/avssources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ static AVPixelFormat CSNameToPIXFMT(const char *CSName, AVPixelFormat Default, b
}

AvisynthVideoSource::AvisynthVideoSource(const char *SourceFile, int Track, FFMS_Index *Index,
int FPSNum, int FPSDen, int Threads, int SeekMode, int RFFMode,
int AFPSNum, int AFPSDen, int Threads, int SeekMode, int RFFMode,
int ResizeToWidth, int ResizeToHeight, const char *ResizerName,
const char *ConvertToFormatName, const char *VarPrefix, IScriptEnvironment* Env)
: FPSNum(FPSNum)
, FPSDen(FPSDen)
: FPSNum(AFPSNum)
, FPSDen(AFPSDen)
, RFFMode(RFFMode)
, VarPrefix(VarPrefix) {
VI = {};
Expand Down Expand Up @@ -227,19 +227,25 @@ AvisynthVideoSource::AvisynthVideoSource(const char *SourceFile, int Track, FFMS
FieldList.resize(VI.num_frames);
}
} else {
VI.fps_denominator = VP->FPSDenominator;
VI.fps_numerator = VP->FPSNumerator;
VI.num_frames = VP->NumFrames;
if (FPSNum > 0 && FPSDen > 0) {
VI.fps_denominator = FPSDen;
VI.fps_numerator = FPSNum;
if (VP->NumFrames > 1) {
VI.num_frames = static_cast<int>((VP->LastTime - VP->FirstTime) * (1 + 1. / (VP->NumFrames - 1)) * FPSNum / FPSDen + 0.5);
if (VI.num_frames < 1) VI.num_frames = 1;
vsh::reduceRational(&FPSNum, &FPSDen);
if (VI.fps_denominator != FPSDen || VI.fps_numerator != FPSNum) {
VI.fps_denominator = FPSDen;
VI.fps_numerator = FPSNum;
if (VP->NumFrames > 1) {
VI.num_frames = static_cast<int>((VP->LastTime - VP->FirstTime) * (1 + 1. / (VP->NumFrames - 1)) * FPSNum / FPSDen + 0.5);
if (VI.num_frames < 1)
VI.num_frames = 1;
} else {
VI.num_frames = 1;
}
} else {
VI.num_frames = 1;
FPSNum = 0;
FPSDen = 0;
}
} else {
VI.fps_denominator = VP->FPSDenominator;
VI.fps_numerator = VP->FPSNumerator;
VI.num_frames = VP->NumFrames;
}
}

Expand Down Expand Up @@ -584,7 +590,7 @@ PVideoFrame AvisynthVideoSource::GetFrame(int n, IScriptEnvironment *Env) {
num = 1;
int64_t DurNum = TB->Num * num;
int64_t DurDen = TB->Den * 1000;
vsh::muldivRational(&DurNum, &DurDen, 1, 1);
vsh::reduceRational(&DurNum, &DurDen);
Env->propSetInt(props, "_DurationNum", DurNum, 0);
Env->propSetInt(props, "_DurationDen", DurDen, 0);
Env->propSetFloat(props, "_AbsoluteTime", ((static_cast<double>(TB->Num) / 1000) * FFMS_GetFrameInfo(T, n)->PTS) / TB->Den, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/avisynth/avssources.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class AvisynthVideoSource : public IClip {
VideoInfo VI;
bool HighBitDepth;
FFMS_VideoSource *V;
int FPSNum;
int FPSDen;
int64_t FPSNum;
int64_t FPSDen;
int RFFMode;
std::vector<FrameFields> FieldList;
const char *VarPrefix;
Expand Down
31 changes: 18 additions & 13 deletions src/vapoursynth/vapoursource4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const VSFrame *VS_CC VSVideoSource4::GetVSFrame(int n, VSCore *core, const VSAPI
num = 1;
int64_t DurNum = TB->Num * num;
int64_t DurDen = TB->Den * 1000;
vsh::muldivRational(&DurNum, &DurDen, 1, 1);
vsh::reduceRational(&DurNum, &DurDen);
vsapi->mapSetInt(Props, "_DurationNum", DurNum, maReplace);
vsapi->mapSetInt(Props, "_DurationDen", DurDen, maReplace);
vsapi->mapSetFloat(Props, "_AbsoluteTime", ((static_cast<double>(TB->Num) / 1000) * FFMS_GetFrameInfo(T, n)->PTS) / TB->Den, maReplace);
Expand Down Expand Up @@ -274,22 +274,27 @@ VSVideoSource4::VSVideoSource4(const char *SourceFile, int Track, FFMS_Index *In

const FFMS_VideoProperties *VP = FFMS_GetVideoProperties(V);

VI[0].fpsDen = VP->FPSDenominator;
VI[0].fpsNum = VP->FPSNumerator;
VI[0].numFrames = VP->NumFrames;
vsh::reduceRational(&VI[0].fpsNum, &VI[0].fpsDen);

if (FPSNum > 0 && FPSDen > 0) {
vsh::muldivRational(&FPSNum, &FPSDen, 1, 1);
VI[0].fpsDen = FPSDen;
VI[0].fpsNum = FPSNum;
if (VP->NumFrames > 1) {
VI[0].numFrames = static_cast<int>((VP->LastTime - VP->FirstTime) * (1 + 1. / (VP->NumFrames - 1)) * FPSNum / FPSDen + 0.5);
if (VI[0].numFrames < 1)
vsh::reduceRational(&FPSNum, &FPSDen);
if (VI[0].fpsDen != FPSDen || VI[0].fpsNum != FPSNum) {
VI[0].fpsDen = FPSDen;
VI[0].fpsNum = FPSNum;
if (VP->NumFrames > 1) {
VI[0].numFrames = static_cast<int>((VP->LastTime - VP->FirstTime) * (1 + 1. / (VP->NumFrames - 1)) * FPSNum / FPSDen + 0.5);
if (VI[0].numFrames < 1)
VI[0].numFrames = 1;
} else {
VI[0].numFrames = 1;
}
} else {
VI[0].numFrames = 1;
FPSNum = 0;
FPSDen = 0;
}
} else {
VI[0].fpsDen = VP->FPSDenominator;
VI[0].fpsNum = VP->FPSNumerator;
VI[0].numFrames = VP->NumFrames;
vsh::muldivRational(&VI[0].fpsNum, &VI[0].fpsDen, 1, 1);
}

if (OutputAlpha) {
Expand Down

0 comments on commit 1f96be3

Please sign in to comment.