libvisiontransfer  6.1.0
bitconversions.cpp
1 /*******************************************************************************
2  * Copyright (c) 2018 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include "visiontransfer/bitconversions.h"
16 #include "visiontransfer/exceptions.h"
17 
18 // SIMD Headers
19 #ifdef __AVX2__
20 # include <immintrin.h>
21 #elif __SSE4_1__
22 # include <smmintrin.h>
23 #elif __SSE2__
24 # include <emmintrin.h>
25 #endif
26 
27 #ifdef __ARM_NEON
28 #include <arm_neon.h>
29 #endif
30 
31 void BitConversions::decode12BitPacked(int startRow, int stopRow, const unsigned char* src,
32  unsigned char* dst, int srcStride, int dstStride, int rowWidth) {
33 
34  const unsigned char* dispStart = src;
35 
36 # ifdef __SSE4_1__
37  if(rowWidth % 32 == 0) {
38  if(srcStride % 16 == 0 && reinterpret_cast<size_t>(src) % 16 == 0) {
39  decode12BitPackedSSE4<true>(startRow, stopRow, dispStart,
40  rowWidth, reinterpret_cast<unsigned short*>(dst), srcStride, dstStride);
41  } else {
42  decode12BitPackedSSE4<false>(startRow, stopRow, dispStart,
43  rowWidth, reinterpret_cast<unsigned short*>(dst), srcStride, dstStride);
44  }
45 
46  } else // We use fallback implementation if the image width is not dividable by 32
47 # endif
48 # if defined(__ARM_NEON) && defined(__ARM_ARCH_ISA_A64)
49  if(rowWidth % 32 == 0) {
50  if(srcStride % 16 == 0 && reinterpret_cast<size_t>(src) % 16 == 0) {
51  decode12BitPackedNEON<true>(startRow, stopRow, dispStart,
52  rowWidth, reinterpret_cast<unsigned short*>(dst), srcStride, dstStride);
53  } else {
54  decode12BitPackedNEON<false>(startRow, stopRow, dispStart,
55  rowWidth, reinterpret_cast<unsigned short*>(dst), srcStride, dstStride);
56  }
57 
58  } else // We use fallback implementation if the image width is not dividable by 32
59 # endif
60  {
61  decode12BitPackedFallback(startRow, stopRow, dispStart, rowWidth,
62  reinterpret_cast<unsigned short*>(dst), srcStride, dstStride);
63  }
64 }
65 
66 #ifdef __SSE4_1__
67 template <bool alignedLoad>
68 void BitConversions::decode12BitPackedSSE4(int startRow, int stopRow, const unsigned char* dispStart,
69  int width, unsigned short* dst, int srcStride, int dstStride) {
70  if(width % 32 != 0) {
71  throw ProtocolException("Image width must be a multiple of 32!");
72  }
73 
74  // SSE optimized code
75  unsigned char* outPos = &reinterpret_cast<unsigned char*>(dst)[startRow*dstStride];
76  int outRowPadding = dstStride - 2*width;
77 
78  constexpr char ff = (char)0xff; // to prevent warnings
79  const __m128i shuffleMask1a = _mm_set_epi8(11, 10, 10, 9, 8, 7, 7, 6, 5, 4, 4, 3, 2, 1, 1, 0);
80  const __m128i shuffleMask1b = _mm_set_epi8(ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 15, 14, 13, 13, 12);
81 
82  const __m128i shuffleMask2a = _mm_set_epi8(7, 6, 6, 5, 4, 3, 3, 2, 1, 0, 0, ff, ff, ff, ff, ff);
83  const __m128i shuffleMask2b = _mm_set_epi8(ff, ff, ff, ff, ff, 15, 15, 14, 13, 12, 12, 11, 10, 9, 9, 8);
84 
85  const __m128i shuffleMask3a = _mm_set_epi8(3, 2, 2, 1, 0, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff);
86  const __m128i shuffleMask3b = _mm_set_epi8(15, 14, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 5, 5, 4);
87 
88  const __m128i shiftMultiplyMask = _mm_set_epi16(1, 16, 1, 16, 1, 16, 1, 16);
89 
90  const __m128i blendMask1 = _mm_set_epi8(ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, 0, 0, 0, 0, 0);
91  const __m128i blendMask2 = _mm_set_epi8(ff, ff, ff, ff, ff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
92 
93  int dispRowWidth = width * 3/2;
94 
95  for(int y = startRow; y<stopRow; y++) {
96  const unsigned char* rowPos = &dispStart[y*srcStride];
97  const unsigned char* rowEnd = &dispStart[y*srcStride + dispRowWidth];
98 
99  while(rowPos < rowEnd) {
100  // Load 16 pixels
101  // AA BA BB CC DC DD EE FE FF ...
102  __m128i rowPixels1, rowPixels2, rowPixels3;
103  if(alignedLoad) {
104  rowPixels1 = _mm_load_si128(reinterpret_cast<const __m128i*>(rowPos));
105  rowPos += 16;
106 
107  rowPixels2 = _mm_load_si128(reinterpret_cast<const __m128i*>(rowPos));
108  rowPos += 16;
109 
110  rowPixels3 = _mm_load_si128(reinterpret_cast<const __m128i*>(rowPos));
111  rowPos += 16;
112  } else {
113  rowPixels1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(rowPos));
114  rowPos += 16;
115 
116  rowPixels2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(rowPos));
117  rowPos += 16;
118 
119  rowPixels3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(rowPos));
120  rowPos += 16;
121  }
122 
123  // Duplicate bytes with shared data
124  // BAAA BBBA DCCC DDDC FEEE FFFE (example without endianess swap!)
125  __m128i part1 = _mm_shuffle_epi8(rowPixels1, shuffleMask1a);
126  __m128i part2a = _mm_shuffle_epi8(rowPixels1, shuffleMask1b);
127  __m128i part2b = _mm_shuffle_epi8(rowPixels2, shuffleMask2a);
128  __m128i part3a = _mm_shuffle_epi8(rowPixels2, shuffleMask2b);
129  __m128i part3b = _mm_shuffle_epi8(rowPixels3, shuffleMask3a);
130  __m128i part4 = _mm_shuffle_epi8(rowPixels3, shuffleMask3b);
131 
132  __m128i part2 = _mm_blendv_epi8(part2a, part2b, blendMask1);
133  __m128i part3 = _mm_blendv_epi8(part3a, part3b, blendMask2);
134 
135  // Shift left through multiplication
136  // AAA0 BBBA CCC0 DDDC EEE0 FFFE
137  __m128i shift1a = _mm_mullo_epi16(part1, shiftMultiplyMask);
138  __m128i shift2a = _mm_mullo_epi16(part2, shiftMultiplyMask);
139  __m128i shift3a = _mm_mullo_epi16(part3, shiftMultiplyMask);
140  __m128i shift4a = _mm_mullo_epi16(part4, shiftMultiplyMask);
141 
142  // Shift right again
143  // 0AAA 0BBB 0CCC 0DDD 0EEE 0FFF ...
144  __m128i shift1b = _mm_srli_epi16(shift1a, 4);
145  __m128i shift2b = _mm_srli_epi16(shift2a, 4);
146  __m128i shift3b = _mm_srli_epi16(shift3a, 4);
147  __m128i shift4b = _mm_srli_epi16(shift4a, 4);
148 
149  _mm_storeu_si128(reinterpret_cast<__m128i*>(outPos), shift1b);
150  outPos += 16;
151  _mm_storeu_si128(reinterpret_cast<__m128i*>(outPos), shift2b);
152  outPos += 16;
153  _mm_storeu_si128(reinterpret_cast<__m128i*>(outPos), shift3b);
154  outPos += 16;
155  _mm_storeu_si128(reinterpret_cast<__m128i*>(outPos), shift4b);
156  outPos += 16;
157  }
158 
159  outPos += outRowPadding;
160  }
161 }
162 #endif
163 
164 #if defined(__ARM_NEON) && defined(__ARM_ARCH_ISA_A64)
165 #define TX(y,x) ((x + y*16)/3 + ((x + y*16)%3)*16)
166 
167 template <bool alignedLoad>
168 void BitConversions::decode12BitPackedNEON(int startRow, int stopRow, const unsigned char* dispStart,
169  int width, unsigned short* dst, int srcStride, int dstStride) {
170  if(width % 32 != 0) {
171  throw ProtocolException("Image width must be a multiple of 32!");
172  }
173 
174  // ARM NEON A64 optimized code
175  unsigned char* outPos = &reinterpret_cast<unsigned char*>(dst)[startRow*dstStride];
176  int outRowPadding = dstStride - 2*width;
177 
178  // Shuffle mask already performs endianess swapping
179  const uint8x16_t shuffleMask1 = {TX(0,0), TX(0,1), TX(0,1), TX(0,2), TX(0,3), TX(0,4),
180  TX(0,4), TX(0,5), TX(0,6), TX(0,7), TX(0,7), TX(0,8), TX(0,9), TX(0,10), TX(0,10), TX(0,11)};
181  const uint8x16_t shuffleMask2 = {TX(0,12), TX(0,13), TX(0,13), TX(0,14), TX(0,15), TX(1,0),
182  TX(1,0), TX(1,1), TX(1,2), TX(1,3), TX(1,3), TX(1,4), TX(1,5), TX(1,6), TX(1,6), TX(1,7)};
183  const uint8x16_t shuffleMask3 = {TX(1,8), TX(1,9), TX(1,9), TX(1,10), TX(1,11), TX(1,12),
184  TX(1,12), TX(1,13), TX(1,14), TX(1,15), TX(1,15), TX(2,0), TX(2,1), TX(2,2), TX(2,2), TX(2,3)};
185  const uint8x16_t shuffleMask4 = {TX(2,4), TX(2,5), TX(2,5), TX(2,6), TX(2,7), TX(2,8),
186  TX(2,8), TX(2,9), TX(2,10), TX(2,11), TX(2,11), TX(2,12), TX(2,13), TX(2,14), TX(2,14), TX(2,15)};
187 
188  const int16x8_t shiftMask = {4, 0, 4, 0, 4, 0, 4, 0};
189 
190  int dispRowWidth = width * 3/2;
191 
192  for(int y = startRow; y<stopRow; y++) {
193  const unsigned char* rowPos = &dispStart[y*srcStride];
194  const unsigned char* rowEnd = &dispStart[y*srcStride + dispRowWidth];
195 
196  while(rowPos < rowEnd) {
197  // Load 16 pixels
198  // AA BA BB CC DC DD EE FE FF
199  uint8x16x3_t rowPixels;
200  if(alignedLoad) {
201  rowPixels = vld3q_u8(reinterpret_cast<const uint8_t*>(
202  __builtin_assume_aligned(rowPos, 16)));
203  } else {
204  rowPixels = vld3q_u8(reinterpret_cast<const uint8_t*>(rowPos));
205  }
206  rowPos += 48;
207 
208  // Duplicate bytes with shared data
209  // BAAA BBBA DCCC DDDC FEEE FFFE (example without endianess swap!)
210  uint8x16_t part1 = vqtbl3q_u8(rowPixels, shuffleMask1);
211  uint8x16_t part2 = vqtbl3q_u8(rowPixels, shuffleMask2);
212  uint8x16_t part3 = vqtbl3q_u8(rowPixels, shuffleMask3);
213  uint8x16_t part4 = vqtbl3q_u8(rowPixels, shuffleMask4);
214 
215  // Shift left
216  // AAA0 BBBA CCC0 DDDC EEE0 FFFE
217  uint16x8_t shift1a = vshlq_u16(vreinterpretq_u16_u8(part1), shiftMask);
218  uint16x8_t shift2a = vshlq_u16(vreinterpretq_u16_u8(part2), shiftMask);
219  uint16x8_t shift3a = vshlq_u16(vreinterpretq_u16_u8(part3), shiftMask);
220  uint16x8_t shift4a = vshlq_u16(vreinterpretq_u16_u8(part4), shiftMask);
221 
222  // Shift right again
223  // 0AAA 0BBB 0CCC 0DDD 0EEE 0FFF ...
224  uint16x8_t shift1b = vshrq_n_u16(shift1a, 4);
225  uint16x8_t shift2b = vshrq_n_u16(shift2a, 4);
226  uint16x8_t shift3b = vshrq_n_u16(shift3a, 4);
227  uint16x8_t shift4b = vshrq_n_u16(shift4a, 4);
228 
229  vst1q_u16(reinterpret_cast<uint16_t*>(outPos), shift1b);
230  outPos += 16;
231  vst1q_u16(reinterpret_cast<uint16_t*>(outPos), shift2b);
232  outPos += 16;
233  vst1q_u16(reinterpret_cast<uint16_t*>(outPos), shift3b);
234  outPos += 16;
235  vst1q_u16(reinterpret_cast<uint16_t*>(outPos), shift4b);
236  outPos += 16;
237  }
238 
239  outPos += outRowPadding;
240  }
241 }
242 #endif
243 
244 void BitConversions::decode12BitPackedFallback(int startRow, int stopRow, const unsigned char* dispStart,
245  int width, unsigned short* dst, int srcStride, int dstStride) {
246 
247  int dstStrideShort = dstStride/2;
248 
249  // Non-SSE version
250  for(int y = startRow; y < stopRow; y++) {
251  const unsigned char* srcPtr = &dispStart[y*srcStride];
252  unsigned short* dstPtr = &dst[y*dstStrideShort];
253  unsigned short* dstEndPtr = dstPtr + width;
254 
255  while(dstPtr != dstEndPtr) {
256  *dstPtr = static_cast<unsigned short>(*srcPtr);
257  srcPtr++;
258  *dstPtr |= static_cast<unsigned short>(*srcPtr & 0x0f) << 8;
259  dstPtr++;
260 
261  *dstPtr = static_cast<unsigned short>(*srcPtr) >> 4;
262  srcPtr++;
263  *dstPtr |= static_cast<unsigned short>(*srcPtr) << 4;
264  srcPtr++;
265  dstPtr++;
266  }
267  }
268 }
269 
270 void BitConversions::encode12BitPacked(int startRow, int stopRow, const unsigned char* src,
271  unsigned char* dst, int srcStride, int dstStride, int rowWidth) {
272  const unsigned short* srcShort = reinterpret_cast<const unsigned short*>(src);
273  int srcStrideShort = srcStride/2;
274 
275  // SSE/NEON optimization is not yet available
276  for(int y = startRow; y < stopRow; y++) {
277  const unsigned short* srcPtr = &srcShort[y*srcStrideShort];
278  const unsigned short* srcEndPtr = srcPtr + rowWidth;
279  unsigned char* dstPtr = &dst[y*dstStride];
280 
281  while(srcPtr != srcEndPtr) {
282  *dstPtr = static_cast<unsigned char>(*srcPtr);
283  dstPtr++;
284  *dstPtr = static_cast<unsigned char>(*srcPtr >> 8) & 0x0f;
285  srcPtr++;
286 
287  *dstPtr |= static_cast<unsigned char>(*srcPtr) << 4;
288  dstPtr++;
289  *dstPtr = static_cast<unsigned char>(*srcPtr >> 4);
290  srcPtr++;
291  dstPtr++;
292  }
293  }
294 }
Exception class that is used for all protocol exceptions.
Definition: exceptions.h:23
Nerian Vision Technologies