-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRBFilter_SSE2.cpp
780 lines (623 loc) · 23 KB
/
RBFilter_SSE2.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#include "stdafx.h"
#include "RBFilter_SSE2.h"
#include <math.h>
#include <malloc.h>
#include <new>
#include <emmintrin.h>
#include <tmmintrin.h>
#include <thread>
#define MAX_RANGE_TABLE_SIZE 255
#define ALIGN_SIZE 16
// only 1 of following 2 should be defined
#define EDGE_COLOR_USE_MAXIMUM
//#define EDGE_COLOR_USE_ADDITION
// if EDGE_COLOR_USE_MAXIMUM is defined, then edge color detection works by calculating
// maximum difference among 3 components (RGB) of 2 colors, which tends to result in lower differences (since only largest among 3 is selected)
// if EDGE_COLOR_USE_ADDITION is defined, then edge color detection works by calculating
// sum of all 3 components, while enforcing 255 maximum. This method is much more sensitive to small differences
#if defined(EDGE_COLOR_USE_MAXIMUM) && defined(EDGE_COLOR_USE_ADDITION)
#error Only 1 of those can be defined
#endif
#if !defined(EDGE_COLOR_USE_MAXIMUM) && !defined(EDGE_COLOR_USE_ADDITION)
#error 1 of those must be defined
#endif
CRBFilterSSE2::CRBFilterSSE2()
{
m_range_table = new float[MAX_RANGE_TABLE_SIZE + 1];
memset(m_range_table, 0, (MAX_RANGE_TABLE_SIZE + 1) * sizeof(float));
}
CRBFilterSSE2::~CRBFilterSSE2()
{
release();
delete[] m_range_table;
}
bool CRBFilterSSE2::initialize(int width, int height, int thread_count, bool pipelined)
{
// basic sanity check, not strict
if (width < 16 || width > 10000)
return false;
if (height < 2 || height > 10000)
return false;
if (thread_count < 1 || thread_count > RBF_MAX_THREADS)
return false;
release();
// round width up to nearest ALIGN_SIZE * thread_count
int round_up = (ALIGN_SIZE / 4) * thread_count;
if (width % round_up)
{
width += round_up - width % round_up;
}
m_reserved_width = width;
m_reserved_height = height;
m_thread_count = thread_count;
m_stage_buffer[0] = (unsigned char*)_aligned_malloc(m_reserved_width * m_reserved_height * 4, ALIGN_SIZE);
if (!m_stage_buffer[0])
return false;
if (pipelined)
{
for (int i = 1; i < STAGE_BUFFER_COUNT; i++)
{
m_stage_buffer[i] = (unsigned char*)_aligned_malloc(m_reserved_width * m_reserved_height * 4, ALIGN_SIZE);
if (!m_stage_buffer[i])
return false;
}
}
m_h_line_cache = new (std::nothrow) float*[m_thread_count];
if (!m_h_line_cache)
return false;
// zero just in case
for (int i = 0; i < m_thread_count; i++)
m_h_line_cache[i] = nullptr;
for (int i = 0; i < m_thread_count; i++)
{
m_h_line_cache[i] = (float*)_aligned_malloc(m_reserved_width * 12 * sizeof(float) , ALIGN_SIZE);
if (!m_h_line_cache[i])
return false;
}
// if (m_pipelined)
{
m_v_line_cache = new (std::nothrow) float*[m_thread_count];
if (!m_v_line_cache)
return false;
for (int i = 0; i < m_thread_count; i++)
m_v_line_cache[i] = nullptr;
for (int i = 0; i < m_thread_count; i++)
{
m_v_line_cache[i] = (float*)_aligned_malloc((m_reserved_width * 8 * sizeof(float)) / m_thread_count, ALIGN_SIZE);
if (!m_v_line_cache[i])
return false;
}
}
return true;
}
void CRBFilterSSE2::release()
{
for (int i = 0; i < STAGE_BUFFER_COUNT; i++)
{
if (m_stage_buffer[i])
{
_aligned_free(m_stage_buffer[i]);
m_stage_buffer[i] = nullptr;
}
}
if (m_h_line_cache)
{
for (int i = 0; i < m_thread_count; i++)
{
if (m_h_line_cache[i])
_aligned_free(m_h_line_cache[i]);
}
delete[] m_h_line_cache;
m_h_line_cache = nullptr;
}
// if (m_pipelined)
{
for (int i = 0; i < m_thread_count; i++)
{
if (m_v_line_cache[i])
_aligned_free(m_v_line_cache[i]);
}
delete[] m_v_line_cache;
}
m_v_line_cache = nullptr;
m_reserved_width = 0;
m_reserved_height = 0;
m_thread_count = 0;
m_pipelined = false;
m_filter_counter = 0;
}
void CRBFilterSSE2::setSigma(float sigma_spatial, float sigma_range)
{
if (m_sigma_spatial != sigma_spatial || m_sigma_range != sigma_range)
{
m_sigma_spatial = sigma_spatial;
m_sigma_range = sigma_range;
double alpha_f = (exp(-sqrt(2.0) / (sigma_spatial * 255.0)));
m_inv_alpha_f = (float)(1.0 - alpha_f);
double inv_sigma_range = 1.0 / (sigma_range * MAX_RANGE_TABLE_SIZE);
{
double ii = 0.f;
for (int i = 0; i <= MAX_RANGE_TABLE_SIZE; i++, ii -= 1.0)
{
m_range_table[i] = (float)(alpha_f * exp(ii * inv_sigma_range));
}
}
}
}
// example of edge color difference calculation from original implementation
// idea is to fit maximum edge color difference as single number in 0-255 range
// colors are added then 2 components are scaled 4x while 1 complement is scaled 2x
// this means 1 of the components is more dominant
//int getDiffFactor(const unsigned char* color1, const unsigned char* color2)
//{
// int c1 = abs(color1[0] - color2[0]);
// int c2 = abs(color1[1] - color2[1]);
// int c3 = abs(color1[2] - color2[2]);
//
// return ((c1 + c3) >> 2) + (c2 >> 1);
//}
inline void getDiffFactor3x(__m128i pix4, __m128i pix4p, __m128i* diff4x)
{
static __m128i byte_mask = _mm_set1_epi32(255);
// get absolute difference for each component per pixel
__m128i diff = _mm_sub_epi8(_mm_max_epu8(pix4, pix4p), _mm_min_epu8(pix4, pix4p));
#ifdef EDGE_COLOR_USE_MAXIMUM
// get maximum of 3 components
__m128i diff_shift1 = _mm_srli_epi32(diff, 8); // 2nd component
diff = _mm_max_epu8(diff, diff_shift1);
diff_shift1 = _mm_srli_epi32(diff_shift1, 8); // 3rd component
diff = _mm_max_epu8(diff, diff_shift1);
// skip alpha component
diff = _mm_and_si128(diff, byte_mask); // zero out all but 1st byte
#endif
#ifdef EDGE_COLOR_USE_ADDITION
// add all component differences and saturate
__m128i diff_shift1 = _mm_srli_epi32(diff, 8); // 2nd component
diff = _mm_adds_epu8(diff, diff_shift1);
diff_shift1 = _mm_srli_epi32(diff_shift1, 8); // 3rd component
diff = _mm_adds_epu8(diff, diff_shift1);
diff = _mm_and_si128(diff, byte_mask); // zero out all but 1st byte
#endif
_mm_store_si128(diff4x, diff);
}
void CRBFilterSSE2::horizontalFilter(int thread_index, const unsigned char* img_src, unsigned char* img_dst, int width, int height, int pitch)
{
int height_segment = height / m_thread_count;
int buffer_offset = thread_index * height_segment * pitch;
img_src += buffer_offset;
img_dst += buffer_offset;
if (thread_index + 1 == m_thread_count) // last segment should account for uneven height
height_segment += height % m_thread_count;
float* line_cache = m_h_line_cache[thread_index];
const float* range_table = m_range_table;
__m128 inv_alpha = _mm_set_ps1(m_inv_alpha_f);
__m128 half_value = _mm_set_ps1(0.5f);
__m128i mask_pack = _mm_setr_epi8(0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
__m128i mask_unpack = _mm_setr_epi8(12, -1, -1, -1, 13, -1, -1, -1, 14, -1, -1, -1, 15, -1, -1, -1);
// used to store maximum difference between 2 pixels
__declspec(align(16)) long color_diff[4];
for (int y = 0; y < height_segment; y++)
{
//////////////////////
// right to left pass, results of this pass get stored in 'line_cache'
{
int pixels_left = width - 1;
// get end of line buffer
float* line_buffer = line_cache + pixels_left * 12;
///////
// handle last pixel in row separately as special case
{
const unsigned char* last_src = img_src + (y + 1) * pitch - 4;
// result color
line_buffer[8] = (float)last_src[0];
line_buffer[9] = (float)last_src[1];
line_buffer[10] = (float)last_src[2];
line_buffer[11] = (float)last_src[3];
// premultiplied source
// caching pre-multiplied allows saving 1 multiply operation in 2nd pass loop, not a big difference
line_buffer[4] = m_inv_alpha_f * line_buffer[8];
line_buffer[5] = m_inv_alpha_f * line_buffer[9];
line_buffer[6] = m_inv_alpha_f * line_buffer[10];
line_buffer[7] = m_inv_alpha_f * line_buffer[11];
}
// "previous" pixel color
__m128 pixel_prev = _mm_load_ps(line_buffer + 8);
// "previous" pixel factor
__m128 alpha_f_prev4 = _mm_set_ps1(1.f);
///////
// handle most middle pixels in 16 byte intervals using xmm registers
// process 4x pixels at a time
int buffer_inc = y * pitch + (pixels_left - 1) * 4 - 16;
const __m128i* src_4xCur = (const __m128i*)(img_src + buffer_inc);
const __m128i* src_4xPrev = (const __m128i*)(img_src + buffer_inc + 4);
while (pixels_left > 0) // outer loop 4x pixel
{
// load 4x pixel, may read backward past start of buffer, but it's OK since that extra data won't be used
__m128i pix4 = _mm_loadu_si128(src_4xCur--);
__m128i pix4p = _mm_loadu_si128(src_4xPrev--);
// get color differences
getDiffFactor3x(pix4, pix4p, (__m128i*)color_diff);
for (int i = 3; i >= 0 && pixels_left-- > 0; i--) // inner loop
{
float alpha_f = range_table[color_diff[i]];
__m128 alpha_f_4x = _mm_set_ps1(alpha_f);
// cache weights for next filter pass
line_buffer -= 12;
_mm_store_ps(line_buffer, alpha_f_4x);
// color factor
alpha_f_prev4 = _mm_mul_ps(alpha_f_prev4, alpha_f_4x);
alpha_f_prev4 = _mm_add_ps(alpha_f_prev4, inv_alpha);
// unpack current source pixel
__m128i pix1 = _mm_shuffle_epi8(pix4, mask_unpack); // extracts 1 pixel components from BYTE to DWORD
pix4 = _mm_slli_si128(pix4, 4); // shift left so next loop unpacks next pixel data
__m128 pixel_F = _mm_cvtepi32_ps(pix1); // convert to floats
// apply color filter
pixel_F = _mm_mul_ps(pixel_F, inv_alpha);
_mm_store_ps(line_buffer + 4, pixel_F); // cache pre-multiplied source color for next filter pass
alpha_f_4x = _mm_mul_ps(pixel_prev, alpha_f_4x);
pixel_F = _mm_add_ps(pixel_F, alpha_f_4x);
// store current color as previous for next cycle
pixel_prev = pixel_F;
// calculate final color
pixel_F = _mm_div_ps(pixel_F, alpha_f_prev4);
// cache filtered color for next filter pass
_mm_store_ps(line_buffer + 8, pixel_F);
}
}
}
//////////////////////
// left to right pass
{
int pixels_left = width - 1;
// process 4x pixels at a time
int buffer_inc = y * pitch;
const __m128i* src_4xCur = (const __m128i*)(img_src + buffer_inc + 4); // shifted by 1 pixel
const __m128i* src_4xPrev = (const __m128i*)(img_src + buffer_inc);
// use float type only to enable 4 byte write using MOVSS
float* out_result = (float*)(img_dst + buffer_inc + 4); // start at 2nd pixel from left
const float* line_buffer = line_cache;
///////
// handle first pixel in row separately as special case
{
unsigned char* first_dst = img_dst + buffer_inc;
// average new pixel with one already in output
// source color was pre-multipled, so get original
float inv_factor = 1.f / m_inv_alpha_f;
first_dst[0] = (unsigned char)((line_buffer[4] * inv_factor + line_buffer[8]) * 0.5f);
first_dst[1] = (unsigned char)((line_buffer[5] * inv_factor + line_buffer[9]) * 0.5f);
first_dst[2] = (unsigned char)((line_buffer[6] * inv_factor + line_buffer[10]) * 0.5f);
first_dst[3] = (unsigned char)((line_buffer[7] * inv_factor + line_buffer[11]) * 0.5f);
}
// initialize "previous pixel" with 4 components of last row pixel
__m128 pixel_prev = _mm_load_ps(line_buffer + 8);
line_buffer += 12;
__m128 alpha_f_prev4 = _mm_set_ps1(1.f);
///////
// handle most pixels in 16 byte intervals using xmm registers
while (pixels_left > 0) // outer loop 4x pixel
{
for (int i = 0; i <= 3 && pixels_left-- > 0; i++) // inner loop
{
// load cached factor
__m128 alpha_f_4x = _mm_load_ps(line_buffer);
line_buffer += 12;
// color factor
alpha_f_prev4 = _mm_mul_ps(alpha_f_prev4, alpha_f_4x);
alpha_f_prev4 = _mm_add_ps(alpha_f_prev4, inv_alpha);
// load current source pixel, pre-multiplied
__m128 pixel_F = _mm_load_ps(line_buffer + 4);
// apply color filter
alpha_f_4x = _mm_mul_ps(pixel_prev, alpha_f_4x);
pixel_F = _mm_add_ps(pixel_F, alpha_f_4x);
// store current color as previous for next cycle
pixel_prev = pixel_F;
// calculate final color
pixel_F = _mm_div_ps(pixel_F, alpha_f_prev4);
// average this result with result from previous pass
__m128 prev_pix4 = _mm_load_ps(line_buffer + 8);
pixel_F = _mm_add_ps(pixel_F, prev_pix4);
pixel_F = _mm_mul_ps(pixel_F, half_value);
// pack float pixel into byte pixel
__m128i pixB = _mm_cvtps_epi32(pixel_F); // convert to integer
pixB = _mm_shuffle_epi8(pixB, mask_pack);
_mm_store_ss(out_result++, _mm_castsi128_ps(pixB));
}
}
}
}
}
void CRBFilterSSE2::verticalFilter(int thread_index, const unsigned char* img_src, unsigned char* img_dst, int width, int height, int pitch)
{
int width_segment = width / m_thread_count;
// make sure width segments round to 16 byte boundary except for last one
width_segment -= width_segment % 4;
int start_offset = width_segment * thread_index;
if (thread_index == m_thread_count - 1) // last one
width_segment = width - start_offset;
int width4 = width_segment / 4;
// adjust img buffer starting positions
img_src += start_offset * 4;
img_dst += start_offset * 4;
float* line_cache = m_v_line_cache[thread_index];
const float* range_table = m_range_table;
__m128 inv_alpha = _mm_set_ps1(m_inv_alpha_f);
__m128 half_value = _mm_set_ps1(0.5f);
__m128i mask_pack = _mm_setr_epi8(0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
__m128i mask_unpack = _mm_setr_epi8(0, -1, -1, -1, 1, -1, -1, -1, 2, -1, -1, -1, 3, -1, -1, -1);
// used to store maximum difference between 2 pixels
__declspec(align(16)) long color_diff[4];
/////////////////
// Bottom to top pass first
{
// last line processed separately since no previous
{
unsigned char* dst_line = img_dst + (height - 1) * pitch;
const unsigned char* src_line = img_src + (height - 1) * pitch;
float* line_buffer = line_cache;
memcpy(dst_line, src_line, width_segment * 4); // copy last line
// initialize line cache
for (int x = 0; x < width_segment; x++)
{
// set factor to 1
line_buffer[0] = 1.f;
line_buffer[1] = 1.f;
line_buffer[2] = 1.f;
line_buffer[3] = 1.f;
// set result color
line_buffer[4] = (float)src_line[0];
line_buffer[5] = (float)src_line[1];
line_buffer[6] = (float)src_line[2];
line_buffer[7] = (float)src_line[3];
src_line += 4;
line_buffer += 8;
}
}
// process other lines
for (int y = height - 2; y >= 0; y--)
{
float* dst_line = (float*)(img_dst + y * pitch);
float* line_buffer = line_cache;
__m128i* src_4xCur = (__m128i*)(img_src + y * pitch);
__m128i* src_4xPrev = (__m128i*)(img_src + (y + 1) * pitch);
int pixels_left = width_segment;
while (pixels_left > 0)
{
// may read past end of buffer, but that data won't be used
__m128i pix4 = _mm_loadu_si128(src_4xCur++); // load 4x pixel
__m128i pix4p = _mm_loadu_si128(src_4xPrev++);
// get color differences
getDiffFactor3x(pix4, pix4p, (__m128i*)color_diff);
for (int i = 0; i < 4 && pixels_left-- > 0; i++) // inner loop
{
float alpha_f = range_table[color_diff[i]];
__m128 alpha_f_4x = _mm_set_ps1(alpha_f);
// load previous line color factor
__m128 alpha_f_prev4 = _mm_load_ps(line_buffer);
// load previous line color
__m128 pixel_prev = _mm_load_ps(line_buffer + 4);
// color factor
alpha_f_prev4 = _mm_mul_ps(alpha_f_prev4, alpha_f_4x);
alpha_f_prev4 = _mm_add_ps(alpha_f_prev4, inv_alpha);
// unpack current source pixel
__m128i pix1 = _mm_shuffle_epi8(pix4, mask_unpack);
pix4 = _mm_srli_si128(pix4, 4); // shift right
__m128 pixel_F = _mm_cvtepi32_ps(pix1); // convert to floats
// apply color filter
pixel_F = _mm_mul_ps(pixel_F, inv_alpha);
alpha_f_4x = _mm_mul_ps(pixel_prev, alpha_f_4x);
pixel_F = _mm_add_ps(pixel_F, alpha_f_4x);
// store current factor and color as previous for next cycle
_mm_store_ps(line_buffer, alpha_f_prev4);
_mm_store_ps(line_buffer + 4, pixel_F);
line_buffer += 8;
// calculate final color
pixel_F = _mm_div_ps(pixel_F, alpha_f_prev4);
// pack float pixel into byte pixel
__m128i pixB = _mm_cvtps_epi32(pixel_F); // convert to integer
pixB = _mm_shuffle_epi8(pixB, mask_pack);
_mm_store_ss(dst_line++, _mm_castsi128_ps(pixB));
}
}
}
}
/////////////////
// Top to bottom pass last
{
mask_pack = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 4, 8, 12);
// first line handled separately since no previous
{
unsigned char* dst_line = img_dst;
const unsigned char* src_line = img_src;
float* line_buffer = line_cache;
for (int x = 0; x < width_segment; x++)
{
// average ccurrent destanation color with current source
dst_line[0] = (dst_line[0] + src_line[0]) / 2;
dst_line[1] = (dst_line[1] + src_line[1]) / 2;
dst_line[2] = (dst_line[2] + src_line[2]) / 2;
dst_line[3] = (dst_line[3] + src_line[3]) / 2;
// set factor to 1
line_buffer[0] = 1.f;
line_buffer[1] = 1.f;
line_buffer[2] = 1.f;
line_buffer[3] = 1.f;
// set result color
line_buffer[4] = (float)src_line[0];
line_buffer[5] = (float)src_line[1];
line_buffer[6] = (float)src_line[2];
line_buffer[7] = (float)src_line[3];
dst_line += 4;
src_line += 4;
line_buffer += 8;
}
}
// process other lines
for (int y = 1; y < height; y++)
{
// const unsigned char* src_line = img_src + y * pitch;
float* line_buffer = line_cache;
__m128i* src_4xCur = (__m128i*)(img_src + y * pitch);
__m128i* src_4xPrev = (__m128i*)(img_src + (y - 1) * pitch);
__m128i* dst_4x = (__m128i*)(img_dst + y * pitch);
for (int x = 0; x < width4; x++)
{
// get color difference
__m128i pix4 = _mm_loadu_si128(src_4xCur++); // load 4x pixel
__m128i pix4p = _mm_loadu_si128(src_4xPrev++);
// get color differences
getDiffFactor3x(pix4, pix4p, (__m128i*)color_diff);
__m128i out_pix4;
for (int i = 0; i < 4; i++) // inner loop
{
float alpha_f = range_table[color_diff[i]];
__m128 alpha_f_4x = _mm_set_ps1(alpha_f);
// load previous line color factor
__m128 alpha_f_prev4 = _mm_load_ps(line_buffer);
// load previous line color
__m128 pixel_prev = _mm_load_ps(line_buffer + 4);
// color factor
// alpha_f_prev = m_inv_alpha_f + alpha_f * alpha_f_prev;
alpha_f_prev4 = _mm_mul_ps(alpha_f_prev4, alpha_f_4x);
alpha_f_prev4 = _mm_add_ps(alpha_f_prev4, inv_alpha);
// unpack current source pixel
__m128i pix1 = _mm_shuffle_epi8(pix4, mask_unpack);
pix4 = _mm_srli_si128(pix4, 4); // shift right
__m128 pixel_F = _mm_cvtepi32_ps(pix1); // convert to floats
// apply color filter
pixel_F = _mm_mul_ps(pixel_F, inv_alpha);
alpha_f_4x = _mm_mul_ps(pixel_prev, alpha_f_4x);
pixel_F = _mm_add_ps(pixel_F, alpha_f_4x);
// store current factor and color as previous for next cycle
_mm_store_ps(line_buffer, alpha_f_prev4);
_mm_store_ps(line_buffer + 4, pixel_F);
line_buffer += 8;
// calculate final color
pixel_F = _mm_div_ps(pixel_F, alpha_f_prev4);
// pack float pixel into byte pixel
__m128i pixB = _mm_cvtps_epi32(pixel_F); // convert to integer
pixB = _mm_shuffle_epi8(pixB, mask_pack);
out_pix4 = _mm_srli_si128(out_pix4, 4); // shift
out_pix4 = _mm_or_si128(out_pix4, pixB);
}
// average result 4x pixel with what is already in destination
__m128i dst4 = _mm_loadu_si128(dst_4x);
out_pix4 = _mm_avg_epu8(out_pix4, dst4);
_mm_storeu_si128(dst_4x++, out_pix4); // store 4x pixel
}
// have to handle leftover 1-3 pixels if last width segment isn't divisble by 4
if (width_segment % 4)
{
// this should be avoided by having image buffers with pitch divisible by 16
}
}
}
}
bool CRBFilterSSE2::filter(unsigned char* out_data, const unsigned char* in_data, int width, int height, int pitch)
{
// basic error checking
if (!m_stage_buffer[0])
return false;
if (width < 16 || width > m_reserved_width)
return false;
if (height < 16 || height > m_reserved_height)
return false;
if (pitch < width * 4)
return false;
if (!out_data || !in_data)
return false;
if (m_inv_alpha_f == 0.f)
return false;
int thread_count_adjusted = m_thread_count - 1;
//////////////////////////////////////////////
// horizontal filter divided in threads
for (int i = 0; i < thread_count_adjusted; i++)
{
m_horizontal_tasks[i] = std::async(std::launch::async, &CRBFilterSSE2::horizontalFilter, this, i, in_data, m_stage_buffer[0], width, height, pitch);
}
// use this thread for last segment
horizontalFilter(thread_count_adjusted, in_data, m_stage_buffer[0], width, height, pitch);
// wait for result
for (int i = 0; i < thread_count_adjusted; i++)
{
m_horizontal_tasks[i].get();
}
/////////////////////////////////////////////
// vertical filter divided in threads
for (int i = 0; i < thread_count_adjusted; i++)
{
m_vertical_tasks[i] = std::async(std::launch::async, &CRBFilterSSE2::verticalFilter, this, i, m_stage_buffer[0], out_data, width, height, pitch);
}
// use this thread for last segment
verticalFilter(thread_count_adjusted, m_stage_buffer[0], out_data, width, height, pitch);
// wait for result
for (int i = 0; i < thread_count_adjusted; i++)
{
m_vertical_tasks[i].get();
}
return true;
}
bool CRBFilterSSE2::filterPipePush(unsigned char* out_data, const unsigned char* in_data, int width, int height, int pitch)
{
// basic error checking
if (!m_stage_buffer[0])
return false;
if (width < 16 || width > m_reserved_width)
return false;
if (height < 16 || height > m_reserved_height)
return false;
if (pitch < width * 4)
return false;
if (m_inv_alpha_f == 0.f)
return false;
m_image_width = width;
m_image_height = height;
m_image_pitch = pitch;
// block until last frame finished 1st stage
for (int i = 0; i < m_thread_count; i++)
{
if (m_horizontal_tasks[i].valid())
m_horizontal_tasks[i].get();
}
int previous_stage_index = (m_filter_counter - 1) % STAGE_BUFFER_COUNT;
int current_stage_index = m_filter_counter % STAGE_BUFFER_COUNT;
m_filter_counter++;
m_out_buffer[current_stage_index] = out_data;
// start new horizontal stage
if (in_data)
{
// start first stage for current frame
for (int i = 0; i < m_thread_count; i++)
{
m_horizontal_tasks[i] = std::async(std::launch::async, &CRBFilterSSE2::horizontalFilter, this, i, in_data, m_stage_buffer[current_stage_index], width, height, pitch);
}
}
// block until last frame finished 2nd stage
for (int i = 0; i < m_thread_count; i++)
{
if (m_vertical_tasks[i].valid())
m_vertical_tasks[i].get();
}
// start new vertical stage based on result of previous stage
if (previous_stage_index >= 0 && m_out_buffer[previous_stage_index])
{
// start first stage for current frame
for (int i = 0; i < m_thread_count; i++)
{
m_vertical_tasks[i] = std::async(std::launch::async, &CRBFilterSSE2::verticalFilter, this, i, m_stage_buffer[previous_stage_index], m_out_buffer[previous_stage_index], width, height, pitch);
}
}
return true;
}
void CRBFilterSSE2::filterPipeFlush()
{
filterPipePush(nullptr, nullptr, m_image_width, m_image_height, m_image_pitch);
if (m_filter_counter > 0)
{
for (int i = 0; i < m_thread_count; i++)
{
if(m_vertical_tasks[i].valid())
m_vertical_tasks[i].get();
}
}
m_filter_counter = 0;
}