Repository navigation
Expand file tree
/
Copy pathimagescaler.cpp
More file actions
279 lines (252 loc) · 11.6 KB
/
Copy pathimagescaler.cpp
File metadata and controls
279 lines (252 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Skin flatPlus: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "./imagescaler.h"
#include <vdr/tools.h>
#include <array>
#include <cstdlib>
#include <cmath>
ImageScaler::ImageScaler() :
m_memory(nullptr),
m_hor_filters(nullptr),
m_ver_filters(nullptr),
m_buffer(nullptr),
m_dst_image(nullptr),
m_dst_stride(0),
m_dst_width(0),
m_dst_height(0),
m_src_width(0),
m_src_height(0),
m_src_x(0),
m_src_y(0),
m_dst_x(0),
m_dst_y(0) {
}
ImageScaler::~ImageScaler() = default; // No need for manual memory management
/**
* Computes the normalized sinc function, sin(x)/x.
*
* For small values of x, a Taylor series approximation is used to avoid
* division by zero and improve numerical stability.
*
* @param x The input value for which the sinc function is calculated.
* @return The value of sin(x)/x, or an approximation for small x.
*/
inline static float sincf(float x) {
// Use Taylor expansion for small x; accurate enough for filter design
if (fabsf(x) < 0.05f)
return 1.0f - (1.0f / 6.0f) * x * x;
return sin(x) / x;
}
/**
* Calculates horizontal and vertical filter coefficients for image scaling
*
* The image is upscaled by a factor of dst_size/src_size, which is a
* rational number. The filter coefficients are calculated using the
* sinc-low pass filter and a cosine window. The filter coefficients are
* normalized to sum up to 2048.
*
* The function stores the filter coefficients in the array filters, which
* has size dst_size. The coefficients are stored in the order
* h0, h1, h2, h3 for each filter. The offset of the first unused pixel is
* stored in the member m_offset of the Filter struct.
*
* The function also sets an end marker in the last element of the array
* filters, which is used to detect the end of the array.
*
* @param filters pointer to an array of Filter structs
* @param dst_size destination image size
* @param src_size source image size
*/
static void CalculateFilters(ImageScaler::Filter *filters, int dst_size, int src_size) {
const float fc {(dst_size >= src_size) ? 1.0f : (static_cast<float>(dst_size) / src_size)};
static constexpr float kPi {3.14159265359f}; // kPi constant
static constexpr float kScaleFactor {2048.0};
const int d {2 * dst_size}; // Sample position denominator
int e {0}, offset {0}; // Init outside of loop
float sub_offset {0.0f}, norm {0.0f}, t {0.0f};
std::array<float, 4> h_arr {0.0f, 0.0f, 0.0f, 0.0f};
for (int i {0}; i < dst_size; ++i) {
e = (2 * i + 1) * src_size - dst_size; // Sample position enumerator
offset = e / d; // Truncated sample position
// Exact sample position is (float) e/d = offset + sub_offset
/* const float */ // sub_offset = (static_cast<float>(e - offset * d) / static_cast<float>(d));
sub_offset = (e - offset * d) * (1.0f / d);
// Calculate filter coefficients
for (unsigned int j {0}; j < 4; ++j) {
t = kPi * (sub_offset + (1 - j));
h_arr[j] = sincf(fc * t) * cosf(0.25f * t); // Sinc-low pass and cos-window
}
// Clamp so we don't sample out of bounds
while (offset < 1) {
h_arr[0] += h_arr[1];
h_arr[1] = h_arr[2];
h_arr[2] = h_arr[3];
h_arr[3] = 0.0f;
++offset;
}
while (offset + 3 > src_size) {
h_arr[3] += h_arr[2];
h_arr[2] = h_arr[1];
h_arr[1] = h_arr[0];
h_arr[0] = 0.0f;
--offset;
}
// Coefficients are normalized to sum up to 2048
norm = kScaleFactor / (h_arr[0] + h_arr[1] + h_arr[2] + h_arr[3]);
--offset; // Offset of fist used pixel
filters[i].m_offset = offset + 4; // Store offset of first unused pixel
for (unsigned int j {0}; j < 4; ++j) {
t = norm * h_arr[j];
filters[i].m_coeff[(offset + j) & 3] =
static_cast<int16_t>((t > 0.0f) ? (t + 0.5f) : (t - 0.5f)); // Consider ring buffer index permutations
}
}
// Set end marker
filters[dst_size].m_offset = (unsigned int) -1;
}
/**
* Sets the parameters for the image scaling operation.
*
* This function initializes the destination image buffer, stride, and dimensions,
* as well as the source image dimensions. It recalculates the horizontal and vertical
* filter coefficients if the image dimensions have changed. The function uses a
* unique_ptr for managing memory allocation required for filter coefficients and
* temporary buffer storage.
*
* @param dst_image Pointer to the destination image buffer.
* @param dst_stride The stride (row length in memory) of the destination image.
* @param dst_width The width of the destination image.
* @param dst_height The height of the destination image.
* @param src_width The width of the source image.
* @param src_height The height of the source image.
*/
void ImageScaler::SetImageParameters(unsigned int *dst_image, unsigned int dst_stride, unsigned int dst_width,
unsigned int dst_height, unsigned int src_width, unsigned int src_height) {
m_src_x = 0;
m_src_y = 0;
m_dst_x = 0;
m_dst_y = 0;
m_dst_image = dst_image;
m_dst_stride = dst_stride;
// If image dimensions do not change we can keep the old filter coefficients
if ((src_width == m_src_width) && (src_height == m_src_height) && (dst_width == m_dst_width) &&
(dst_height == m_dst_height))
return;
m_dst_width = dst_width;
m_dst_height = dst_height;
m_src_width = src_width;
m_src_height = src_height;
// Narrowing conversion
const std::size_t hor_filters_size =
(m_dst_width + 1) * sizeof(Filter); // Reserve one extra position for end marker
const std::size_t ver_filters_size = (m_dst_height + 1) * sizeof(Filter);
const std::size_t buffer_size = 4 * m_dst_width * sizeof(TmpPixel);
// Use a std::unique_ptr to manage memory
m_memory = std::make_unique<char[]>(hor_filters_size + ver_filters_size + buffer_size);
m_hor_filters = reinterpret_cast<Filter *>(m_memory.get());
m_ver_filters = reinterpret_cast<Filter *>(m_memory.get() + hor_filters_size);
m_buffer = reinterpret_cast<TmpPixel *>(m_memory.get() + hor_filters_size + ver_filters_size);
CalculateFilters(m_hor_filters, m_dst_width , m_src_width);
CalculateFilters(m_ver_filters, m_dst_height, m_src_height);
}
// Branchless shift and clamp [0,255] (for up to 31-bit signed input).
// This uses bitwise operations for performance.
inline static unsigned int shift_clamp(int x) {
// This is a branchless version of:
// x = (x + 2^21) >> 22;
// return std::clamp(x, 0, 255);
static constexpr int kShiftAmount {22}; // 22
static constexpr int kRounding {1 << (kShiftAmount - 1)}; // 2^21
x = (x + kRounding) >> kShiftAmount;
// Branchless [0,255] clamp for signed int (Common C idiom, fast)
x &= -(x >= 0); // Clamp <0 to 0 (if x<0, becomes 0)
x -= (x > 255) * (x - 255); // Oversaturate above 255 down to 255
return static_cast<unsigned int>(x);
}
/**
* Packs a TmpPixel into a single 32-bit unsigned integer.
*
* This function takes a TmpPixel, representing a pixel with four color components,
* and packs it into a single 32-bit unsigned integer. The color components are
* shifted into place and clamped to the range 0..255 to avoid overflows.
*
* @param pixel The TmpPixel to pack.
* @return A 32-bit unsigned integer representing the packed pixel.
*/
inline unsigned int ImageScaler::PackPixel(int c0, int c1, int c2, int c3) {
return shift_clamp(c0) |
(shift_clamp(c1) << 8) |
(shift_clamp(c2) << 16) |
(shift_clamp(c3) << 24);
}
/**
* Processes the next source line for vertical image scaling
*
* Applies vertical filtering coefficients to calculate output pixels
* using a 4-tap filter. Each output pixel is computed as weighted sum
* of 4 input pixels using pre-calculated coefficients.
*
* The filter coefficients (h0-h3) are optimized for minimal ringing
* while preserving sharpness.
*/
void ImageScaler::NextSourceLine() {
m_dst_x = 0;
m_src_x = 0;
++m_src_y;
if (m_dst_y > m_dst_height) {
esyslog("ImageScaler::NextSourceLine: Buffer overrun detected! m_dst_y (%d) > m_dst_height (%d)", m_dst_y,
m_dst_height);
return; // Protect against buffer overrun
}
int16_t h0 {0}, h1 {0}, h2 {0}, h3 {0}; // Init outside of loop
// Filter coefficients (Read once for each vertical pass)
while (m_ver_filters[m_dst_y].m_offset == m_src_y) {
h0 = m_ver_filters[m_dst_y].m_coeff[0];
h1 = m_ver_filters[m_dst_y].m_coeff[1];
h2 = m_ver_filters[m_dst_y].m_coeff[2];
h3 = m_ver_filters[m_dst_y].m_coeff[3];
const TmpPixel *src {m_buffer}; // Pointer to the buffer containing filtered input lines
unsigned int *dst {m_dst_image + m_dst_stride * m_dst_y}; // Pointer to the destination image line
unsigned int i {0};
// Unroll 4 pixels at a time for speed
for (; i + 3 < m_dst_width; i += 4) {
// Calculate four pixels at a time using direct component math to avoid temporary objects.
const int c00 {src[0][0] * h0 + src[1][0] * h1 + src[2][0] * h2 + src[3][0] * h3};
const int c01 {src[0][1] * h0 + src[1][1] * h1 + src[2][1] * h2 + src[3][1] * h3};
const int c02 {src[0][2] * h0 + src[1][2] * h1 + src[2][2] * h2 + src[3][2] * h3};
const int c03 {src[0][3] * h0 + src[1][3] * h1 + src[2][3] * h2 + src[3][3] * h3};
const int c10 {src[4][0] * h0 + src[5][0] * h1 + src[6][0] * h2 + src[7][0] * h3};
const int c11 {src[4][1] * h0 + src[5][1] * h1 + src[6][1] * h2 + src[7][1] * h3};
const int c12 {src[4][2] * h0 + src[5][2] * h1 + src[6][2] * h2 + src[7][2] * h3};
const int c13 {src[4][3] * h0 + src[5][3] * h1 + src[6][3] * h2 + src[7][3] * h3};
const int c20 {src[8][0] * h0 + src[9][0] * h1 + src[10][0] * h2 + src[11][0] * h3};
const int c21 {src[8][1] * h0 + src[9][1] * h1 + src[10][1] * h2 + src[11][1] * h3};
const int c22 {src[8][2] * h0 + src[9][2] * h1 + src[10][2] * h2 + src[11][2] * h3};
const int c23 {src[8][3] * h0 + src[9][3] * h1 + src[10][3] * h2 + src[11][3] * h3};
const int c30 {src[12][0] * h0 + src[13][0] * h1 + src[14][0] * h2 + src[15][0] * h3};
const int c31 {src[12][1] * h0 + src[13][1] * h1 + src[14][1] * h2 + src[15][1] * h3};
const int c32 {src[12][2] * h0 + src[13][2] * h1 + src[14][2] * h2 + src[15][2] * h3};
const int c33 {src[12][3] * h0 + src[13][3] * h1 + src[14][3] * h2 + src[15][3] * h3};
dst[i] = PackPixel(c00, c01, c02, c03);
dst[i+1] = PackPixel(c10, c11, c12, c13);
dst[i+2] = PackPixel(c20, c21, c22, c23);
dst[i+3] = PackPixel(c30, c31, c32, c33);
src += 16;
}
// If num pixels is not a multiple of 4, finish the remaining ones
for (; i < m_dst_width; ++i) {
const int c0 {src[0][0] * h0 + src[1][0] * h1 + src[2][0] * h2 + src[3][0] * h3};
const int c1 {src[0][1] * h0 + src[1][1] * h1 + src[2][1] * h2 + src[3][1] * h3};
const int c2 {src[0][2] * h0 + src[1][2] * h1 + src[2][2] * h2 + src[3][2] * h3};
const int c3 {src[0][3] * h0 + src[1][3] * h1 + src[2][3] * h2 + src[3][3] * h3};
dst[i] = PackPixel(c0, c1, c2, c3);
src += 4;
}
++m_dst_y;
}
}