Skip to content

Commit

Permalink
Add src_width and src_height params for the first plane (#2)
Browse files Browse the repository at this point in the history
* Add src_width and src_height params
for the first plane only

* Fix formatting

---------

Co-authored-by: Setsugennoao <[email protected]>
  • Loading branch information
random-patch-method and Setsugennoao authored Jun 24, 2024
1 parent d7d6ad2 commit 77b4d3e
Showing 1 changed file with 81 additions and 10 deletions.
91 changes: 81 additions & 10 deletions Source/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct DpidData {
int dst_w, dst_h;
float lambda[3];
float src_left[3], src_top[3];
float src_width[3], src_height[3];
bool process[3];
bool read_chromaloc;
};
Expand Down Expand Up @@ -40,10 +41,10 @@ static void dpidProcess(const T * VS_RESTRICT srcp, int src_stride,
const T * VS_RESTRICT downp, int down_stride,
T * VS_RESTRICT dstp, int dst_stride,
int src_w, int src_h, int dst_w, int dst_h, float lambda,
float src_left, float src_top) {
float src_left, float src_top, float src_width, float src_height) {

const float scale_x = static_cast<float>(src_w) / dst_w;
const float scale_y = static_cast<float>(src_h) / dst_h;
const float scale_x = src_width / dst_w;
const float scale_y = src_height / dst_h;

for (int outer_y = 0; outer_y < dst_h; ++outer_y) {
for (int outer_x = 0; outer_x < dst_w; ++outer_x) {
Expand Down Expand Up @@ -128,7 +129,18 @@ static const VSFrame *VS_CC dpidGetframe(int n, int activationReason, void *inst
const int dst_w = vsapi->getFrameWidth(src2, plane);
const int dst_h = vsapi->getFrameHeight(src2, plane);

const float hSubS = plane == 0 ? 1.0f : static_cast<float>(1 << fi->subSamplingW);
const float vSubS = plane == 0 ? 1.0f : static_cast<float>(1 << fi->subSamplingH);

float src_left, src_top;

float src_width = d->src_width[plane] / hSubS;
if (src_width == 0.0f)
src_width = static_cast<float>(src_w);
float src_height = d->src_height[plane] / vSubS;
if (src_height == 0.0f)
src_height = static_cast<float>(src_h);

if (plane != 0 && d->read_chromaloc) {
int chromaLocation;

Expand All @@ -141,15 +153,13 @@ static const VSFrame *VS_CC dpidGetframe(int n, int activationReason, void *inst
}
}

const float hSubS = static_cast<float>(1 << fi->subSamplingW);
const float hCPlace = (chromaLocation == 0 || chromaLocation == 2 || chromaLocation == 4)
? (0.5f - hSubS / 2) : 0.f;
const float hScale = static_cast<float>(dst_w) / static_cast<float>(src_w);
const float hScale = static_cast<float>(dst_w) / src_width;

const float vSubS = static_cast<float>(1 << fi->subSamplingH);
const float vCPlace = (chromaLocation == 2 || chromaLocation == 3)
? (0.5f - vSubS / 2) : ((chromaLocation == 4 || chromaLocation == 5) ? (vSubS / 2 - 0.5f) : 0.f);
const float vScale = static_cast<float>(dst_h) / static_cast<float>(src_h);
const float vScale = static_cast<float>(dst_h) / src_height;

src_left = ((d->src_left[plane] - hCPlace) * hScale + hCPlace) / hScale / hSubS;
src_top = ((d->src_top[plane] - vCPlace) * vScale + vCPlace) / vScale / vSubS;
Expand All @@ -166,14 +176,14 @@ static const VSFrame *VS_CC dpidGetframe(int n, int activationReason, void *inst
reinterpret_cast<const uint8_t *>(src2p), src2_stride,
reinterpret_cast<uint8_t *>(dstp), dst_stride,
src_w, src_h, dst_w, dst_h, d->lambda[plane],
src_left, src_top);
src_left, src_top, src_width, src_height);
} else if (fi->bytesPerSample == 2) {
dpidProcess(
reinterpret_cast<const uint16_t *>(src1p), src1_stride,
reinterpret_cast<const uint16_t *>(src2p), src2_stride,
reinterpret_cast<uint16_t *>(dstp), dst_stride,
src_w, src_h, dst_w, dst_h, d->lambda[plane],
src_left, src_top);
src_left, src_top, src_width, src_height);
}
} else if (fi->sampleType == stFloat) {
if (fi->bytesPerSample == 4) {
Expand All @@ -182,7 +192,7 @@ static const VSFrame *VS_CC dpidGetframe(int n, int activationReason, void *inst
reinterpret_cast<const float *>(src2p), src2_stride,
reinterpret_cast<float *>(dstp), dst_stride,
src_w, src_h, dst_w, dst_h, d->lambda[plane],
src_left, src_top);
src_left, src_top, src_width, src_height);
}
}
}
Expand Down Expand Up @@ -265,6 +275,14 @@ static void VS_CC dpidRawCreate(const VSMap *in, VSMap *out, void *userData, VSC
if (numSrcTop > vi->format.numPlanes)
throw std::string{"more \"src_top\" given than there are planes"};

const int numSrcWidth = vsapi->mapNumElements(in, "src_width");
if (numSrcWidth > vi->format.numPlanes)
throw std::string{"more \"src_width\" given than there are planes"};

const int numSrcHeight = vsapi->mapNumElements(in, "src_height");
if (numSrcHeight > vi->format.numPlanes)
throw std::string{"more \"src_height\" given than there are planes"};

for (int i = 0; i < 3; i++) {
if (i < numSrcLeft)
d->src_left[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_left", i, nullptr));
Expand All @@ -279,9 +297,28 @@ static void VS_CC dpidRawCreate(const VSMap *in, VSMap *out, void *userData, VSC
d->src_top[0] = 0.0f;
else
d->src_top[i] = d->src_top[i-1];

if (i < numSrcWidth) {
d->src_width[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_width", i, nullptr));
if (d->src_width[i] < 0.0f)
throw std::string{"active window set by \"src_width\" must be positive"};
} else if (i == 0)
d->src_width[0] = 0.0f;
else
d->src_width[i] = d->src_width[i - 1];

if (i < numSrcHeight) {
d->src_height[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_height", i, nullptr));
if (d->src_height[i] < 0.0f)
throw std::string{"active window set by \"src_height\" must be positive"};
} else if (i == 0)
d->src_height[0] = 0.0f;
else
d->src_height[i] = d->src_height[i - 1];
}

d->read_chromaloc = static_cast<bool>(vsapi->mapGetInt(in, "read_chromaloc", 0, &err));

if (err) {
d->read_chromaloc = true;
}
Expand Down Expand Up @@ -365,6 +402,14 @@ static void VS_CC dpidCreate(const VSMap *in, VSMap *out, void *userData, VSCore
if (numSrcTop > vi->format.numPlanes)
throw std::string{"more \"src_top\" given than there are planes"};

const int numSrcWidth = vsapi->mapNumElements(in, "src_width");
if (numSrcWidth > vi->format.numPlanes)
throw std::string{"more \"src_width\" given than there are planes"};

const int numSrcHeight = vsapi->mapNumElements(in, "src_height");
if (numSrcHeight > vi->format.numPlanes)
throw std::string{"more \"src_height\" given than there are planes"};

for (int i = 0; i < 3; i++) {
if (i < numSrcLeft)
d->src_left[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_left", i, nullptr));
Expand All @@ -379,6 +424,24 @@ static void VS_CC dpidCreate(const VSMap *in, VSMap *out, void *userData, VSCore
d->src_top[0] = 0.0f;
else
d->src_top[i] = d->src_top[i-1];

if (i < numSrcWidth) {
d->src_width[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_width", i, nullptr));
if (d->src_width[i] < 0.0f)
throw std::string{"active window set by \"src_width\" must be positive"};
} else if (i == 0)
d->src_width[0] = 0.0f;
else
d->src_width[i] = d->src_width[i - 1];

if (i < numSrcHeight) {
d->src_height[i] = static_cast<float>(vsapi->mapGetFloat(in, "src_height", i, nullptr));
if (d->src_height[i] < 0.0f)
throw std::string{"active window set by \"src_height\" must be positive"};
} else if (i == 0)
d->src_height[0] = 0.0f;
else
d->src_height[i] = d->src_height[i - 1];
}

d->read_chromaloc = static_cast<bool>(vsapi->mapGetInt(in, "read_chromaloc", 0, &err));
Expand All @@ -393,6 +456,10 @@ static void VS_CC dpidCreate(const VSMap *in, VSMap *out, void *userData, VSCore
vsapi->mapSetInt(vtmp1, "height", d->dst_h, maReplace);
vsapi->mapSetFloat(vtmp1, "src_left", d->src_left[0], maReplace);
vsapi->mapSetFloat(vtmp1, "src_top", d->src_top[0], maReplace);
if (d->src_width[0] != 0.0f)
vsapi->mapSetFloat(vtmp1, "src_width", d->src_width[0], maReplace);
if (d->src_height[0] != 0.0f)
vsapi->mapSetFloat(vtmp1, "src_height", d->src_height[0], maReplace);

VSMap * vtmp2 = vsapi->invoke(vsapi->getPluginByNamespace("resize", core), "Bilinear", vtmp1);
if (vsapi->mapGetError(vtmp2)) {
Expand Down Expand Up @@ -433,6 +500,8 @@ VS_EXTERNAL_API(void) VapourSynthPluginInit2(VSPlugin *plugin, const VSPLUGINAPI
"lambda:float[]:opt;"
"src_left:float[]:opt;"
"src_top:float[]:opt;"
"src_width:float[]:opt;"
"src_height:float[]:opt;"
"read_chromaloc:int:opt;"
"planes:int[]:opt;",
"clip:vnode;", dpidRawCreate, 0, plugin);
Expand All @@ -444,6 +513,8 @@ VS_EXTERNAL_API(void) VapourSynthPluginInit2(VSPlugin *plugin, const VSPLUGINAPI
"lambda:float[]:opt;"
"src_left:float[]:opt;"
"src_top:float[]:opt;"
"src_width:float[]:opt;"
"src_height:float[]:opt;"
"read_chromaloc:int:opt;",
"clip:vnode;", dpidCreate, 0, plugin);
}

0 comments on commit 77b4d3e

Please sign in to comment.