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
|
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* RemoteFX Codec Library - SSE2 Optimizations
*
* Copyright 2011 Stephen Erisman
* Copyright 2011 Norbert Federa <norbert.federa@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <freerdp/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/sysinfo.h>
#include <xmmintrin.h>
#include <emmintrin.h>
#include "rfx_types.h"
#include "rfx_sse2.h"
#ifdef _MSC_VER
#define __attribute__(...)
#endif
#define CACHE_LINE_BYTES 64
#ifndef __clang__
#define ATTRIBUTES __gnu_inline__, __always_inline__, __artificial__
#else
#define ATTRIBUTES __gnu_inline__, __always_inline__
#endif
#define _mm_between_epi16(_val, _min, _max) \
do \
{ \
_val = _mm_min_epi16(_max, _mm_max_epi16(_val, _min)); \
} while (0)
static __inline void __attribute__((ATTRIBUTES)) _mm_prefetch_buffer(char* buffer, int num_bytes)
{
__m128i* buf = (__m128i*)buffer;
for (unsigned int i = 0; i < (num_bytes / sizeof(__m128i));
i += (CACHE_LINE_BYTES / sizeof(__m128i)))
{
_mm_prefetch((char*)(&buf[i]), _MM_HINT_NTA);
}
}
/* rfx_decode_ycbcr_to_rgb_sse2 code now resides in the primitives library. */
/* rfx_encode_rgb_to_ycbcr_sse2 code now resides in the primitives library. */
static __inline void __attribute__((ATTRIBUTES))
rfx_quantization_decode_block_sse2(INT16* buffer, const size_t buffer_size, const UINT32 factor)
{
__m128i a;
__m128i* ptr = (__m128i*)buffer;
__m128i* buf_end = (__m128i*)(buffer + buffer_size);
if (factor == 0)
return;
do
{
a = _mm_load_si128(ptr);
a = _mm_slli_epi16(a, factor);
_mm_store_si128(ptr, a);
ptr++;
} while (ptr < buf_end);
}
static void rfx_quantization_decode_sse2(INT16* buffer, const UINT32* WINPR_RESTRICT quantVals)
{
WINPR_ASSERT(buffer);
WINPR_ASSERT(quantVals);
_mm_prefetch_buffer((char*)buffer, 4096 * sizeof(INT16));
rfx_quantization_decode_block_sse2(&buffer[0], 1024, quantVals[8] - 1); /* HL1 */
rfx_quantization_decode_block_sse2(&buffer[1024], 1024, quantVals[7] - 1); /* LH1 */
rfx_quantization_decode_block_sse2(&buffer[2048], 1024, quantVals[9] - 1); /* HH1 */
rfx_quantization_decode_block_sse2(&buffer[3072], 256, quantVals[5] - 1); /* HL2 */
rfx_quantization_decode_block_sse2(&buffer[3328], 256, quantVals[4] - 1); /* LH2 */
rfx_quantization_decode_block_sse2(&buffer[3584], 256, quantVals[6] - 1); /* HH2 */
rfx_quantization_decode_block_sse2(&buffer[3840], 64, quantVals[2] - 1); /* HL3 */
rfx_quantization_decode_block_sse2(&buffer[3904], 64, quantVals[1] - 1); /* LH3 */
rfx_quantization_decode_block_sse2(&buffer[3968], 64, quantVals[3] - 1); /* HH3 */
rfx_quantization_decode_block_sse2(&buffer[4032], 64, quantVals[0] - 1); /* LL3 */
}
static __inline void __attribute__((ATTRIBUTES))
rfx_quantization_encode_block_sse2(INT16* buffer, const int buffer_size, const UINT32 factor)
{
__m128i a;
__m128i* ptr = (__m128i*)buffer;
__m128i* buf_end = (__m128i*)(buffer + buffer_size);
__m128i half;
if (factor == 0)
return;
half = _mm_set1_epi16(1 << (factor - 1));
do
{
a = _mm_load_si128(ptr);
a = _mm_add_epi16(a, half);
a = _mm_srai_epi16(a, factor);
_mm_store_si128(ptr, a);
ptr++;
} while (ptr < buf_end);
}
static void rfx_quantization_encode_sse2(INT16* buffer,
const UINT32* WINPR_RESTRICT quantization_values)
{
WINPR_ASSERT(buffer);
WINPR_ASSERT(quantization_values);
_mm_prefetch_buffer((char*)buffer, 4096 * sizeof(INT16));
rfx_quantization_encode_block_sse2(buffer, 1024, quantization_values[8] - 6); /* HL1 */
rfx_quantization_encode_block_sse2(buffer + 1024, 1024, quantization_values[7] - 6); /* LH1 */
rfx_quantization_encode_block_sse2(buffer + 2048, 1024, quantization_values[9] - 6); /* HH1 */
rfx_quantization_encode_block_sse2(buffer + 3072, 256, quantization_values[5] - 6); /* HL2 */
rfx_quantization_encode_block_sse2(buffer + 3328, 256, quantization_values[4] - 6); /* LH2 */
rfx_quantization_encode_block_sse2(buffer + 3584, 256, quantization_values[6] - 6); /* HH2 */
rfx_quantization_encode_block_sse2(buffer + 3840, 64, quantization_values[2] - 6); /* HL3 */
rfx_quantization_encode_block_sse2(buffer + 3904, 64, quantization_values[1] - 6); /* LH3 */
rfx_quantization_encode_block_sse2(buffer + 3968, 64, quantization_values[3] - 6); /* HH3 */
rfx_quantization_encode_block_sse2(buffer + 4032, 64, quantization_values[0] - 6); /* LL3 */
rfx_quantization_encode_block_sse2(buffer, 4096, 5);
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_decode_block_horiz_sse2(INT16* l, INT16* h, INT16* dst, int subband_width)
{
INT16* l_ptr = l;
INT16* h_ptr = h;
INT16* dst_ptr = dst;
int first = 0;
int last = 0;
__m128i l_n;
__m128i h_n;
__m128i h_n_m;
__m128i tmp_n;
__m128i dst_n;
__m128i dst_n_p;
__m128i dst1;
__m128i dst2;
for (int y = 0; y < subband_width; y++)
{
/* Even coefficients */
for (int n = 0; n < subband_width; n += 8)
{
/* dst[2n] = l[n] - ((h[n-1] + h[n] + 1) >> 1); */
l_n = _mm_load_si128((__m128i*)l_ptr);
h_n = _mm_load_si128((__m128i*)h_ptr);
h_n_m = _mm_loadu_si128((__m128i*)(h_ptr - 1));
if (n == 0)
{
first = _mm_extract_epi16(h_n_m, 1);
h_n_m = _mm_insert_epi16(h_n_m, first, 0);
}
tmp_n = _mm_add_epi16(h_n, h_n_m);
tmp_n = _mm_add_epi16(tmp_n, _mm_set1_epi16(1));
tmp_n = _mm_srai_epi16(tmp_n, 1);
dst_n = _mm_sub_epi16(l_n, tmp_n);
_mm_store_si128((__m128i*)l_ptr, dst_n);
l_ptr += 8;
h_ptr += 8;
}
l_ptr -= subband_width;
h_ptr -= subband_width;
/* Odd coefficients */
for (int n = 0; n < subband_width; n += 8)
{
/* dst[2n + 1] = (h[n] << 1) + ((dst[2n] + dst[2n + 2]) >> 1); */
h_n = _mm_load_si128((__m128i*)h_ptr);
h_n = _mm_slli_epi16(h_n, 1);
dst_n = _mm_load_si128((__m128i*)(l_ptr));
dst_n_p = _mm_loadu_si128((__m128i*)(l_ptr + 1));
if (n == subband_width - 8)
{
last = _mm_extract_epi16(dst_n_p, 6);
dst_n_p = _mm_insert_epi16(dst_n_p, last, 7);
}
tmp_n = _mm_add_epi16(dst_n_p, dst_n);
tmp_n = _mm_srai_epi16(tmp_n, 1);
tmp_n = _mm_add_epi16(tmp_n, h_n);
dst1 = _mm_unpacklo_epi16(dst_n, tmp_n);
dst2 = _mm_unpackhi_epi16(dst_n, tmp_n);
_mm_store_si128((__m128i*)dst_ptr, dst1);
_mm_store_si128((__m128i*)(dst_ptr + 8), dst2);
l_ptr += 8;
h_ptr += 8;
dst_ptr += 16;
}
}
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_decode_block_vert_sse2(INT16* l, INT16* h, INT16* dst, int subband_width)
{
INT16* l_ptr = l;
INT16* h_ptr = h;
INT16* dst_ptr = dst;
__m128i l_n;
__m128i h_n;
__m128i tmp_n;
__m128i h_n_m;
__m128i dst_n;
__m128i dst_n_m;
__m128i dst_n_p;
int total_width = subband_width + subband_width;
/* Even coefficients */
for (int n = 0; n < subband_width; n++)
{
for (int x = 0; x < total_width; x += 8)
{
/* dst[2n] = l[n] - ((h[n-1] + h[n] + 1) >> 1); */
l_n = _mm_load_si128((__m128i*)l_ptr);
h_n = _mm_load_si128((__m128i*)h_ptr);
tmp_n = _mm_add_epi16(h_n, _mm_set1_epi16(1));
if (n == 0)
tmp_n = _mm_add_epi16(tmp_n, h_n);
else
{
h_n_m = _mm_loadu_si128((__m128i*)(h_ptr - total_width));
tmp_n = _mm_add_epi16(tmp_n, h_n_m);
}
tmp_n = _mm_srai_epi16(tmp_n, 1);
dst_n = _mm_sub_epi16(l_n, tmp_n);
_mm_store_si128((__m128i*)dst_ptr, dst_n);
l_ptr += 8;
h_ptr += 8;
dst_ptr += 8;
}
dst_ptr += total_width;
}
h_ptr = h;
dst_ptr = dst + total_width;
/* Odd coefficients */
for (int n = 0; n < subband_width; n++)
{
for (int x = 0; x < total_width; x += 8)
{
/* dst[2n + 1] = (h[n] << 1) + ((dst[2n] + dst[2n + 2]) >> 1); */
h_n = _mm_load_si128((__m128i*)h_ptr);
dst_n_m = _mm_load_si128((__m128i*)(dst_ptr - total_width));
h_n = _mm_slli_epi16(h_n, 1);
tmp_n = dst_n_m;
if (n == subband_width - 1)
tmp_n = _mm_add_epi16(tmp_n, dst_n_m);
else
{
dst_n_p = _mm_loadu_si128((__m128i*)(dst_ptr + total_width));
tmp_n = _mm_add_epi16(tmp_n, dst_n_p);
}
tmp_n = _mm_srai_epi16(tmp_n, 1);
dst_n = _mm_add_epi16(tmp_n, h_n);
_mm_store_si128((__m128i*)dst_ptr, dst_n);
h_ptr += 8;
dst_ptr += 8;
}
dst_ptr += total_width;
}
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_decode_block_sse2(INT16* buffer, INT16* idwt, int subband_width)
{
INT16* hl = NULL;
INT16* lh = NULL;
INT16* hh = NULL;
INT16* ll = NULL;
INT16* l_dst = NULL;
INT16* h_dst = NULL;
_mm_prefetch_buffer((char*)idwt, subband_width * 4 * sizeof(INT16));
/* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt.
*/
/* The 4 sub-bands are stored in HL(0), LH(1), HH(2), LL(3) order. */
/* The lower part L uses LL(3) and HL(0). */
/* The higher part H uses LH(1) and HH(2). */
ll = buffer + subband_width * subband_width * 3;
hl = buffer;
l_dst = idwt;
rfx_dwt_2d_decode_block_horiz_sse2(ll, hl, l_dst, subband_width);
lh = buffer + subband_width * subband_width;
hh = buffer + subband_width * subband_width * 2;
h_dst = idwt + subband_width * subband_width * 2;
rfx_dwt_2d_decode_block_horiz_sse2(lh, hh, h_dst, subband_width);
/* Inverse DWT in vertical direction, results are stored in original buffer. */
rfx_dwt_2d_decode_block_vert_sse2(l_dst, h_dst, buffer, subband_width);
}
static void rfx_dwt_2d_decode_sse2(INT16* buffer, INT16* dwt_buffer)
{
WINPR_ASSERT(buffer);
WINPR_ASSERT(dwt_buffer);
_mm_prefetch_buffer((char*)buffer, 4096 * sizeof(INT16));
rfx_dwt_2d_decode_block_sse2(&buffer[3840], dwt_buffer, 8);
rfx_dwt_2d_decode_block_sse2(&buffer[3072], dwt_buffer, 16);
rfx_dwt_2d_decode_block_sse2(&buffer[0], dwt_buffer, 32);
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_encode_block_vert_sse2(INT16* src, INT16* l, INT16* h, int subband_width)
{
int total_width = 0;
__m128i src_2n;
__m128i src_2n_1;
__m128i src_2n_2;
__m128i h_n;
__m128i h_n_m;
__m128i l_n;
total_width = subband_width << 1;
for (int n = 0; n < subband_width; n++)
{
for (int x = 0; x < total_width; x += 8)
{
src_2n = _mm_load_si128((__m128i*)src);
src_2n_1 = _mm_load_si128((__m128i*)(src + total_width));
if (n < subband_width - 1)
src_2n_2 = _mm_load_si128((__m128i*)(src + 2 * total_width));
else
src_2n_2 = src_2n;
/* h[n] = (src[2n + 1] - ((src[2n] + src[2n + 2]) >> 1)) >> 1 */
h_n = _mm_add_epi16(src_2n, src_2n_2);
h_n = _mm_srai_epi16(h_n, 1);
h_n = _mm_sub_epi16(src_2n_1, h_n);
h_n = _mm_srai_epi16(h_n, 1);
_mm_store_si128((__m128i*)h, h_n);
if (n == 0)
h_n_m = h_n;
else
h_n_m = _mm_load_si128((__m128i*)(h - total_width));
/* l[n] = src[2n] + ((h[n - 1] + h[n]) >> 1) */
l_n = _mm_add_epi16(h_n_m, h_n);
l_n = _mm_srai_epi16(l_n, 1);
l_n = _mm_add_epi16(l_n, src_2n);
_mm_store_si128((__m128i*)l, l_n);
src += 8;
l += 8;
h += 8;
}
src += total_width;
}
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_encode_block_horiz_sse2(INT16* src, INT16* l, INT16* h, int subband_width)
{
int first = 0;
__m128i src_2n;
__m128i src_2n_1;
__m128i src_2n_2;
__m128i h_n;
__m128i h_n_m;
__m128i l_n;
for (int y = 0; y < subband_width; y++)
{
for (int n = 0; n < subband_width; n += 8)
{
/* The following 3 Set operations consumes more than half of the total DWT processing
* time! */
src_2n =
_mm_set_epi16(src[14], src[12], src[10], src[8], src[6], src[4], src[2], src[0]);
src_2n_1 =
_mm_set_epi16(src[15], src[13], src[11], src[9], src[7], src[5], src[3], src[1]);
src_2n_2 = _mm_set_epi16(n == subband_width - 8 ? src[14] : src[16], src[14], src[12],
src[10], src[8], src[6], src[4], src[2]);
/* h[n] = (src[2n + 1] - ((src[2n] + src[2n + 2]) >> 1)) >> 1 */
h_n = _mm_add_epi16(src_2n, src_2n_2);
h_n = _mm_srai_epi16(h_n, 1);
h_n = _mm_sub_epi16(src_2n_1, h_n);
h_n = _mm_srai_epi16(h_n, 1);
_mm_store_si128((__m128i*)h, h_n);
h_n_m = _mm_loadu_si128((__m128i*)(h - 1));
if (n == 0)
{
first = _mm_extract_epi16(h_n_m, 1);
h_n_m = _mm_insert_epi16(h_n_m, first, 0);
}
/* l[n] = src[2n] + ((h[n - 1] + h[n]) >> 1) */
l_n = _mm_add_epi16(h_n_m, h_n);
l_n = _mm_srai_epi16(l_n, 1);
l_n = _mm_add_epi16(l_n, src_2n);
_mm_store_si128((__m128i*)l, l_n);
src += 16;
l += 8;
h += 8;
}
}
}
static __inline void __attribute__((ATTRIBUTES))
rfx_dwt_2d_encode_block_sse2(INT16* buffer, INT16* dwt, int subband_width)
{
INT16* hl = NULL;
INT16* lh = NULL;
INT16* hh = NULL;
INT16* ll = NULL;
INT16* l_src = NULL;
INT16* h_src = NULL;
_mm_prefetch_buffer((char*)dwt, subband_width * 4 * sizeof(INT16));
/* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */
l_src = dwt;
h_src = dwt + subband_width * subband_width * 2;
rfx_dwt_2d_encode_block_vert_sse2(buffer, l_src, h_src, subband_width);
/* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order,
* stored in original buffer. */
/* The lower part L generates LL(3) and HL(0). */
/* The higher part H generates LH(1) and HH(2). */
ll = buffer + subband_width * subband_width * 3;
hl = buffer;
lh = buffer + subband_width * subband_width;
hh = buffer + subband_width * subband_width * 2;
rfx_dwt_2d_encode_block_horiz_sse2(l_src, ll, hl, subband_width);
rfx_dwt_2d_encode_block_horiz_sse2(h_src, lh, hh, subband_width);
}
static void rfx_dwt_2d_encode_sse2(INT16* buffer, INT16* dwt_buffer)
{
WINPR_ASSERT(buffer);
WINPR_ASSERT(dwt_buffer);
_mm_prefetch_buffer((char*)buffer, 4096 * sizeof(INT16));
rfx_dwt_2d_encode_block_sse2(buffer, dwt_buffer, 32);
rfx_dwt_2d_encode_block_sse2(buffer + 3072, dwt_buffer, 16);
rfx_dwt_2d_encode_block_sse2(buffer + 3840, dwt_buffer, 8);
}
void rfx_init_sse2(RFX_CONTEXT* context)
{
if (!IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE))
return;
PROFILER_RENAME(context->priv->prof_rfx_quantization_decode, "rfx_quantization_decode_sse2")
PROFILER_RENAME(context->priv->prof_rfx_quantization_encode, "rfx_quantization_encode_sse2")
PROFILER_RENAME(context->priv->prof_rfx_dwt_2d_decode, "rfx_dwt_2d_decode_sse2")
PROFILER_RENAME(context->priv->prof_rfx_dwt_2d_encode, "rfx_dwt_2d_encode_sse2")
context->quantization_decode = rfx_quantization_decode_sse2;
context->quantization_encode = rfx_quantization_encode_sse2;
context->dwt_2d_decode = rfx_dwt_2d_decode_sse2;
context->dwt_2d_encode = rfx_dwt_2d_encode_sse2;
}
|