Skip to content

Commit

Permalink
Make clamp a static inline function.
Browse files Browse the repository at this point in the history
At best, this is two instructions on x86-64, so the cost of the function
call is significant. It's a templated standard library function in
C++17, so make it match the standard interface.

Also remove atan2deg, which should also be static inline but isn't used.

About 1% speedup.
  • Loading branch information
atsampson committed Jun 30, 2019
1 parent 276a2e0 commit b59f64b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 40 deletions.
18 changes: 2 additions & 16 deletions tools/ld-comb-ntsc/comb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ void Comb::split2D(FrameBuffer *frameBuffer)
kn /= 2;

qreal p_2drange = 45 * irescale;
kp = clamp(1 - (kp / p_2drange), 0, 1);
kn = clamp(1 - (kn / p_2drange), 0, 1);
kp = clamp(1 - (kp / p_2drange), 0.0, 1.0);
kn = clamp(1 - (kn / p_2drange), 0.0, 1.0);

qreal sc = 1.0;

Expand Down Expand Up @@ -563,17 +563,3 @@ void Comb::adjustY(FrameBuffer *frameBuffer, YiqBuffer &yiqBuffer)
}
}
}

qreal Comb::clamp(qreal v, qreal low, qreal high)
{
if (v < low) return low;
else if (v > high) return high;
else return v;
}

qreal Comb::atan2deg(qreal y, qreal x)
{
qreal rv = static_cast<double>(atan2(static_cast<long double>(y), x) * (180 / M_PIl));
if (rv < 0) rv += 360;
return rv;
}
3 changes: 0 additions & 3 deletions tools/ld-comb-ntsc/comb.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ class Comb
QByteArray yiqToRgbFrame(YiqBuffer yiqBuffer, qreal burstLevel);
void overlayOpticalFlowMap(FrameBuffer frameBuffer, QByteArray &rgbOutputFrame);
void adjustY(FrameBuffer *frameBuffer, YiqBuffer &yiqBuffer);

qreal clamp(qreal v, qreal low, qreal high);
qreal atan2deg(qreal y, qreal x);
};

#endif // COMB_H
10 changes: 1 addition & 9 deletions tools/ld-comb-ntsc/opticalflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void OpticalFlow::denseOpticalFlow(YiqBuffer yiqBuffer, QVector<qreal> &kValues)
// in the X direction than the y
qreal velocity = calculateDistance(static_cast<qreal>(flowatxy.y), static_cast<qreal>(flowatxy.x) * 2);

kValues[(910 * y) + x] = clamp(velocity, 0, 1);
kValues[(910 * y) + x] = clamp(velocity, 0.0, 1.0);
}
}
} else kValues.fill(1);
Expand Down Expand Up @@ -89,14 +89,6 @@ cv::Mat OpticalFlow::convertYtoMat(YiqBuffer yiqBuffer)
return cv::Mat(525, 910, CV_16UC1, frame);
}

// Method to clamp a value within a low and high range
qreal OpticalFlow::clamp(qreal v, qreal low, qreal high)
{
if (v < low) return low;
else if (v > high) return high;
else return v;
}

// This method calculates the distance between points where x is the difference between the x-coordinates
// and y is the difference between the y coordinates
qreal OpticalFlow::calculateDistance(qreal yDifference, qreal xDifference)
Expand Down
2 changes: 1 addition & 1 deletion tools/ld-comb-ntsc/opticalflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <opencv2/core/core.hpp>
#include <opencv2/video/tracking.hpp>

#include "rgb.h"
#include "yiqbuffer.h"

class OpticalFlow
Expand All @@ -54,7 +55,6 @@ class OpticalFlow
qint32 framesProcessed;

cv::Mat convertYtoMat(YiqBuffer yiqBuffer);
qreal clamp(qreal v, qreal low, qreal high);
qreal calculateDistance(qreal yDifference, qreal xDifference);
};

Expand Down
13 changes: 3 additions & 10 deletions tools/ld-comb-ntsc/rgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,13 @@ void RGB::conv(YIQ _y, qreal colourBurstMedian)
g = y - (0.272 * i) - (0.647 * q);
b = y - (1.107 * i) + (1.704 * q);

r = clamp(r, 0, 65535);
g = clamp(g, 0, 65535);
b = clamp(b, 0, 65535);
r = clamp(r, 0.0, 65535.0);
g = clamp(g, 0.0, 65535.0);
b = clamp(b, 0.0, 65535.0);
}

// Private methods ----------------------------------------------------------------------------------------------------

double RGB::clamp(double v, double low, double high)
{
if (v < low) return low;
else if (v > high) return high;
else return v;
}

double RGB::scaleY(double level)
{
// Scale Y to according to the black to white interval
Expand Down
11 changes: 10 additions & 1 deletion tools/ld-comb-ntsc/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ class RGB
bool whitePoint75;
bool blackAndWhite;

double clamp(double v, double low, double high);
double scaleY(double level);
};

// Clamp a value to within a fixed range.
// (Equivalent to C++17's std::clamp.)
template <typename T>
static inline const T& clamp(const T& v, const T& low, const T& high)
{
if (v < low) return low;
else if (v > high) return high;
else return v;
}

#endif // RGB_H

0 comments on commit b59f64b

Please sign in to comment.