| 1 |
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted provided that the following conditions |
| 5 |
* are met: |
| 6 |
* * Redistributions of source code must retain the above copyright |
| 7 |
* notice, this list of conditions and the following disclaimer. |
| 8 |
* * Redistributions in binary form must reproduce the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer in the |
| 10 |
* documentation and/or other materials provided with the distribution. |
| 11 |
* * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 |
* contributors may be used to endorse or promote products derived |
| 13 |
* from this software without specific prior written permission. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/** |
| 29 |
************************************************************************** |
| 30 |
* \file dct8x8_kernel_short.cu |
| 31 |
* \brief Contains kernel implementations of DCT and IDCT routines for 16-bit |
| 32 |
* integers, used in JPEG internal data processing. Optimized device code. |
| 33 |
* |
| 34 |
* This code implements traditional approach to forward and inverse Discrete |
| 35 |
* Cosine Transform to blocks of image pixels (of 8x8 size), as in JPEG standard. |
| 36 |
* The data processing is performed using short data type. |
| 37 |
* The routine that performs quantization of coefficients can be found in |
| 38 |
* dct8x8_kernel_quantization.cu file. |
| 39 |
*/ |
| 40 |
|
| 41 |
#pragma once |
| 42 |
|
| 43 |
#include <cooperative_groups.h> |
| 44 |
|
| 45 |
namespace cg = cooperative_groups; |
| 46 |
#include "Common.h" |
| 47 |
|
| 48 |
/** |
| 49 |
* Width of data block (short kernel) |
| 50 |
*/ |
| 51 |
#define KERS_BLOCK_WIDTH 32 |
| 52 |
|
| 53 |
/** |
| 54 |
* Height of data block (short kernel) |
| 55 |
*/ |
| 56 |
#define KERS_BLOCK_HEIGHT 32 |
| 57 |
|
| 58 |
/** |
| 59 |
* LOG2 of width of data block (short kernel) |
| 60 |
*/ |
| 61 |
#define KERS_BW_LOG2 5 |
| 62 |
|
| 63 |
/** |
| 64 |
* LOG2 of height of data block (short kernel) |
| 65 |
*/ |
| 66 |
#define KERS_BH_LOG2 5 |
| 67 |
|
| 68 |
/** |
| 69 |
* Stride of shared memory buffer (short kernel) |
| 70 |
*/ |
| 71 |
#define KERS_SMEMBLOCK_STRIDE (KERS_BLOCK_WIDTH + 2) |
| 72 |
|
| 73 |
/** |
| 74 |
* Half of data block width (short kernel) |
| 75 |
*/ |
| 76 |
#define KERS_BLOCK_WIDTH_HALF (KERS_BLOCK_WIDTH / 2) |
| 77 |
|
| 78 |
#define SIN_1_4 0x5A82 |
| 79 |
#define COS_1_4 0x5A82 |
| 80 |
#define SIN_1_8 0x30FC |
| 81 |
#define COS_1_8 0x7642 |
| 82 |
|
| 83 |
#define OSIN_1_16 0x063E |
| 84 |
#define OSIN_3_16 0x11C7 |
| 85 |
#define OSIN_5_16 0x1A9B |
| 86 |
#define OSIN_7_16 0x1F63 |
| 87 |
|
| 88 |
#define OCOS_1_16 0x1F63 |
| 89 |
#define OCOS_3_16 0x1A9B |
| 90 |
#define OCOS_5_16 0x11C7 |
| 91 |
#define OCOS_7_16 0x063E |
| 92 |
|
| 93 |
/** |
| 94 |
* Package of 2 shorts into 1 int - designed to perform i/o by integers to avoid |
| 95 |
* bank conflicts |
| 96 |
*/ |
| 97 |
union PackedShorts { |
| 98 |
struct __align__(8) { |
| 99 |
short hShort1; |
| 100 |
short hShort2; |
| 101 |
}; |
| 102 |
unsigned int hInt; |
| 103 |
}; |
| 104 |
|
| 105 |
/** |
| 106 |
* Converts fixed point value to short value |
| 107 |
*/ |
| 108 |
__device__ inline short unfixh(int x) { return (short)((x + 0x8000) >> 16); } |
| 109 |
|
| 110 |
/** |
| 111 |
* Converts fixed point value to short value |
| 112 |
*/ |
| 113 |
__device__ inline int unfixo(int x) { return (x + 0x1000) >> 13; } |
| 114 |
|
| 115 |
/** |
| 116 |
************************************************************************** |
| 117 |
* Performs in-place DCT of vector of 8 elements (used to access columns in |
| 118 |
*shared memory). |
| 119 |
* |
| 120 |
* \param SrcDst [IN/OUT] - Pointer to the first element of vector |
| 121 |
* \param Stride [IN] - Value to add to ptr to access other elements |
| 122 |
* |
| 123 |
* \return None |
| 124 |
*/ |
| 125 |
__device__ void CUDAshortInplaceDCT(short *SrcDst, int Stride) { |
| 126 |
int in0, in1, in2, in3, in4, in5, in6, in7; |
| 127 |
int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; |
| 128 |
int tmp10, tmp11, tmp12, tmp13; |
| 129 |
int tmp14, tmp15, tmp16, tmp17; |
| 130 |
int tmp25, tmp26; |
| 131 |
|
| 132 |
int DoubleStride = Stride << 1; |
| 133 |
|
| 134 |
short *DstPtr = SrcDst; |
| 135 |
in0 = *DstPtr; |
| 136 |
DstPtr += Stride; |
| 137 |
in1 = *DstPtr; |
| 138 |
DstPtr += Stride; |
| 139 |
in2 = *DstPtr; |
| 140 |
DstPtr += Stride; |
| 141 |
in3 = *DstPtr; |
| 142 |
DstPtr += Stride; |
| 143 |
in4 = *DstPtr; |
| 144 |
DstPtr += Stride; |
| 145 |
in5 = *DstPtr; |
| 146 |
DstPtr += Stride; |
| 147 |
in6 = *DstPtr; |
| 148 |
DstPtr += Stride; |
| 149 |
in7 = *DstPtr; |
| 150 |
|
| 151 |
tmp0 = in7 + in0; |
| 152 |
tmp1 = in6 + in1; |
| 153 |
tmp2 = in5 + in2; |
| 154 |
tmp3 = in4 + in3; |
| 155 |
tmp4 = in3 - in4; |
| 156 |
tmp5 = in2 - in5; |
| 157 |
tmp6 = in1 - in6; |
| 158 |
tmp7 = in0 - in7; |
| 159 |
|
| 160 |
tmp10 = tmp3 + tmp0; |
| 161 |
tmp11 = tmp2 + tmp1; |
| 162 |
tmp12 = tmp1 - tmp2; |
| 163 |
tmp13 = tmp0 - tmp3; |
| 164 |
|
| 165 |
tmp16 = unfixo(FMUL(tmp6 + tmp5, SIN_1_4)); |
| 166 |
tmp15 = unfixo(FMUL(tmp6 - tmp5, COS_1_4)); |
| 167 |
|
| 168 |
tmp4 <<= 2; |
| 169 |
tmp7 <<= 2; |
| 170 |
|
| 171 |
tmp14 = tmp4 + tmp15; |
| 172 |
tmp25 = tmp4 - tmp15; |
| 173 |
tmp26 = tmp7 - tmp16; |
| 174 |
tmp17 = tmp7 + tmp16; |
| 175 |
|
| 176 |
DstPtr = SrcDst; |
| 177 |
*DstPtr = unfixh(FMUL(tmp10 + tmp11, SIN_1_4)); |
| 178 |
DstPtr += DoubleStride; |
| 179 |
*DstPtr = unfixh(FMUL(tmp13, COS_1_8) + FMUL(tmp12, SIN_1_8)); |
| 180 |
DstPtr += DoubleStride; |
| 181 |
*DstPtr = unfixh(FMUL(tmp10 - tmp11, COS_1_4)); |
| 182 |
DstPtr += DoubleStride; |
| 183 |
*DstPtr = unfixh(FMUL(tmp13, SIN_1_8) - FMUL(tmp12, COS_1_8)); |
| 184 |
|
| 185 |
DstPtr = SrcDst + Stride; |
| 186 |
*DstPtr = unfixh(FMUL(tmp17, OCOS_1_16) + FMUL(tmp14, OSIN_1_16)); |
| 187 |
DstPtr += DoubleStride; |
| 188 |
*DstPtr = unfixh(FMUL(tmp26, OCOS_3_16) - FMUL(tmp25, OSIN_3_16)); |
| 189 |
DstPtr += DoubleStride; |
| 190 |
*DstPtr = unfixh(FMUL(tmp26, OCOS_5_16) + FMUL(tmp25, OSIN_5_16)); |
| 191 |
DstPtr += DoubleStride; |
| 192 |
*DstPtr = unfixh(FMUL(tmp17, OCOS_7_16) - FMUL(tmp14, OSIN_7_16)); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
************************************************************************** |
| 197 |
* Performs in-place DCT of vector of 8 elements (used to access rows in shared |
| 198 |
*memory). |
| 199 |
* |
| 200 |
* \param V8 [IN/OUT] - Pointer to the first two elements of vector |
| 201 |
* |
| 202 |
* \return None |
| 203 |
*/ |
| 204 |
__device__ void CUDAshortInplaceDCT(unsigned int *V8) { |
| 205 |
int in0, in1, in2, in3, in4, in5, in6, in7; |
| 206 |
int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; |
| 207 |
int tmp10, tmp11, tmp12, tmp13; |
| 208 |
int tmp14, tmp15, tmp16, tmp17; |
| 209 |
int tmp25, tmp26; |
| 210 |
PackedShorts sh0, sh1, sh2, sh3; |
| 211 |
|
| 212 |
sh0.hInt = V8[0]; |
| 213 |
sh1.hInt = V8[1]; |
| 214 |
sh2.hInt = V8[2]; |
| 215 |
sh3.hInt = V8[3]; |
| 216 |
in0 = sh0.hShort1; |
| 217 |
in1 = sh0.hShort2; |
| 218 |
in2 = sh1.hShort1; |
| 219 |
in3 = sh1.hShort2; |
| 220 |
in4 = sh2.hShort1; |
| 221 |
in5 = sh2.hShort2; |
| 222 |
in6 = sh3.hShort1; |
| 223 |
in7 = sh3.hShort2; |
| 224 |
|
| 225 |
tmp0 = in7 + in0; |
| 226 |
tmp1 = in6 + in1; |
| 227 |
tmp2 = in5 + in2; |
| 228 |
tmp3 = in4 + in3; |
| 229 |
tmp4 = in3 - in4; |
| 230 |
tmp5 = in2 - in5; |
| 231 |
tmp6 = in1 - in6; |
| 232 |
tmp7 = in0 - in7; |
| 233 |
|
| 234 |
tmp10 = tmp3 + tmp0; |
| 235 |
tmp11 = tmp2 + tmp1; |
| 236 |
tmp12 = tmp1 - tmp2; |
| 237 |
tmp13 = tmp0 - tmp3; |
| 238 |
|
| 239 |
sh0.hShort1 = unfixh(FMUL(tmp10 + tmp11, SIN_1_4)); |
| 240 |
sh2.hShort1 = unfixh(FMUL(tmp10 - tmp11, COS_1_4)); |
| 241 |
|
| 242 |
sh1.hShort1 = unfixh(FMUL(tmp13, COS_1_8) + FMUL(tmp12, SIN_1_8)); |
| 243 |
sh3.hShort1 = unfixh(FMUL(tmp13, SIN_1_8) - FMUL(tmp12, COS_1_8)); |
| 244 |
|
| 245 |
tmp16 = unfixo(FMUL(tmp6 + tmp5, SIN_1_4)); |
| 246 |
tmp15 = unfixo(FMUL(tmp6 - tmp5, COS_1_4)); |
| 247 |
|
| 248 |
tmp4 <<= 2; |
| 249 |
tmp7 <<= 2; |
| 250 |
|
| 251 |
tmp14 = tmp4 + tmp15; |
| 252 |
tmp25 = tmp4 - tmp15; |
| 253 |
tmp26 = tmp7 - tmp16; |
| 254 |
tmp17 = tmp7 + tmp16; |
| 255 |
|
| 256 |
sh0.hShort2 = unfixh(FMUL(tmp17, OCOS_1_16) + FMUL(tmp14, OSIN_1_16)); |
| 257 |
sh3.hShort2 = unfixh(FMUL(tmp17, OCOS_7_16) - FMUL(tmp14, OSIN_7_16)); |
| 258 |
sh2.hShort2 = unfixh(FMUL(tmp26, OCOS_5_16) + FMUL(tmp25, OSIN_5_16)); |
| 259 |
sh1.hShort2 = unfixh(FMUL(tmp26, OCOS_3_16) - FMUL(tmp25, OSIN_3_16)); |
| 260 |
|
| 261 |
V8[0] = sh0.hInt; |
| 262 |
V8[1] = sh1.hInt; |
| 263 |
V8[2] = sh2.hInt; |
| 264 |
V8[3] = sh3.hInt; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
************************************************************************** |
| 269 |
* Performs in-place IDCT of vector of 8 elements (used to access columns in |
| 270 |
*shared memory). |
| 271 |
* |
| 272 |
* \param SrcDst [IN/OUT] - Pointer to the first element of vector |
| 273 |
* \param Stride [IN] - Value to add to ptr to access other elements |
| 274 |
* |
| 275 |
* \return None |
| 276 |
*/ |
| 277 |
__device__ void CUDAshortInplaceIDCT(short *SrcDst, int Stride) { |
| 278 |
int in0, in1, in2, in3, in4, in5, in6, in7; |
| 279 |
int tmp10, tmp11, tmp12, tmp13; |
| 280 |
int tmp20, tmp21, tmp22, tmp23; |
| 281 |
int tmp30, tmp31; |
| 282 |
int tmp40, tmp41, tmp42, tmp43; |
| 283 |
int tmp50, tmp51, tmp52, tmp53; |
| 284 |
|
| 285 |
short *DstPtr = SrcDst; |
| 286 |
in0 = *DstPtr; |
| 287 |
DstPtr += Stride; |
| 288 |
in1 = *DstPtr; |
| 289 |
DstPtr += Stride; |
| 290 |
in2 = *DstPtr; |
| 291 |
DstPtr += Stride; |
| 292 |
in3 = *DstPtr; |
| 293 |
DstPtr += Stride; |
| 294 |
in4 = *DstPtr; |
| 295 |
DstPtr += Stride; |
| 296 |
in5 = *DstPtr; |
| 297 |
DstPtr += Stride; |
| 298 |
in6 = *DstPtr; |
| 299 |
DstPtr += Stride; |
| 300 |
in7 = *DstPtr; |
| 301 |
|
| 302 |
tmp10 = FMUL(in0 + in4, COS_1_4); |
| 303 |
tmp11 = FMUL(in0 - in4, COS_1_4); |
| 304 |
tmp12 = FMUL(in2, SIN_1_8) - FMUL(in6, COS_1_8); |
| 305 |
tmp13 = FMUL(in6, SIN_1_8) + FMUL(in2, COS_1_8); |
| 306 |
|
| 307 |
tmp20 = tmp10 + tmp13; |
| 308 |
tmp21 = tmp11 + tmp12; |
| 309 |
tmp22 = tmp11 - tmp12; |
| 310 |
tmp23 = tmp10 - tmp13; |
| 311 |
|
| 312 |
tmp30 = unfixo(FMUL(in3 + in5, COS_1_4)); |
| 313 |
tmp31 = unfixo(FMUL(in3 - in5, COS_1_4)); |
| 314 |
|
| 315 |
in1 <<= 2; |
| 316 |
in7 <<= 2; |
| 317 |
|
| 318 |
tmp40 = in1 + tmp30; |
| 319 |
tmp41 = in7 + tmp31; |
| 320 |
tmp42 = in1 - tmp30; |
| 321 |
tmp43 = in7 - tmp31; |
| 322 |
|
| 323 |
tmp50 = FMUL(tmp40, OCOS_1_16) + FMUL(tmp41, OSIN_1_16); |
| 324 |
tmp51 = FMUL(tmp40, OSIN_1_16) - FMUL(tmp41, OCOS_1_16); |
| 325 |
tmp52 = FMUL(tmp42, OCOS_5_16) + FMUL(tmp43, OSIN_5_16); |
| 326 |
tmp53 = FMUL(tmp42, OSIN_5_16) - FMUL(tmp43, OCOS_5_16); |
| 327 |
|
| 328 |
DstPtr = SrcDst; |
| 329 |
*DstPtr = unfixh(tmp20 + tmp50); |
| 330 |
DstPtr += Stride; |
| 331 |
*DstPtr = unfixh(tmp21 + tmp53); |
| 332 |
DstPtr += Stride; |
| 333 |
*DstPtr = unfixh(tmp22 + tmp52); |
| 334 |
DstPtr += Stride; |
| 335 |
*DstPtr = unfixh(tmp23 + tmp51); |
| 336 |
DstPtr += Stride; |
| 337 |
*DstPtr = unfixh(tmp23 - tmp51); |
| 338 |
DstPtr += Stride; |
| 339 |
*DstPtr = unfixh(tmp22 - tmp52); |
| 340 |
DstPtr += Stride; |
| 341 |
*DstPtr = unfixh(tmp21 - tmp53); |
| 342 |
DstPtr += Stride; |
| 343 |
*DstPtr = unfixh(tmp20 - tmp50); |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
************************************************************************** |
| 348 |
* Performs in-place IDCT of vector of 8 elements (used to access rows in shared |
| 349 |
*memory). |
| 350 |
* |
| 351 |
* \param V8 [IN/OUT] - Pointer to the first two elements of vector |
| 352 |
* |
| 353 |
* \return None |
| 354 |
*/ |
| 355 |
__device__ void CUDAshortInplaceIDCT(unsigned int *V8) { |
| 356 |
int in0, in1, in2, in3, in4, in5, in6, in7; |
| 357 |
int tmp10, tmp11, tmp12, tmp13; |
| 358 |
int tmp20, tmp21, tmp22, tmp23; |
| 359 |
int tmp30, tmp31; |
| 360 |
int tmp40, tmp41, tmp42, tmp43; |
| 361 |
int tmp50, tmp51, tmp52, tmp53; |
| 362 |
PackedShorts sh0, sh1, sh2, sh3; |
| 363 |
|
| 364 |
sh0.hInt = V8[0]; |
| 365 |
sh1.hInt = V8[1]; |
| 366 |
sh2.hInt = V8[2]; |
| 367 |
sh3.hInt = V8[3]; |
| 368 |
in0 = sh0.hShort1; |
| 369 |
in1 = sh0.hShort2; |
| 370 |
in2 = sh1.hShort1; |
| 371 |
in3 = sh1.hShort2; |
| 372 |
in4 = sh2.hShort1; |
| 373 |
in5 = sh2.hShort2; |
| 374 |
in6 = sh3.hShort1; |
| 375 |
in7 = sh3.hShort2; |
| 376 |
|
| 377 |
tmp10 = FMUL(in0 + in4, COS_1_4); |
| 378 |
tmp11 = FMUL(in0 - in4, COS_1_4); |
| 379 |
tmp12 = FMUL(in2, SIN_1_8) - FMUL(in6, COS_1_8); |
| 380 |
tmp13 = FMUL(in6, SIN_1_8) + FMUL(in2, COS_1_8); |
| 381 |
|
| 382 |
tmp20 = tmp10 + tmp13; |
| 383 |
tmp21 = tmp11 + tmp12; |
| 384 |
tmp22 = tmp11 - tmp12; |
| 385 |
tmp23 = tmp10 - tmp13; |
| 386 |
|
| 387 |
tmp30 = unfixo(FMUL(in3 + in5, COS_1_4)); |
| 388 |
tmp31 = unfixo(FMUL(in3 - in5, COS_1_4)); |
| 389 |
|
| 390 |
in1 <<= 2; |
| 391 |
in7 <<= 2; |
| 392 |
|
| 393 |
tmp40 = in1 + tmp30; |
| 394 |
tmp41 = in7 + tmp31; |
| 395 |
tmp42 = in1 - tmp30; |
| 396 |
tmp43 = in7 - tmp31; |
| 397 |
|
| 398 |
tmp50 = FMUL(tmp40, OCOS_1_16) + FMUL(tmp41, OSIN_1_16); |
| 399 |
tmp51 = FMUL(tmp40, OSIN_1_16) - FMUL(tmp41, OCOS_1_16); |
| 400 |
tmp52 = FMUL(tmp42, OCOS_5_16) + FMUL(tmp43, OSIN_5_16); |
| 401 |
tmp53 = FMUL(tmp42, OSIN_5_16) - FMUL(tmp43, OCOS_5_16); |
| 402 |
|
| 403 |
sh0.hShort1 = unfixh(tmp20 + tmp50); |
| 404 |
sh0.hShort2 = unfixh(tmp21 + tmp53); |
| 405 |
sh1.hShort1 = unfixh(tmp22 + tmp52); |
| 406 |
sh1.hShort2 = unfixh(tmp23 + tmp51); |
| 407 |
sh2.hShort1 = unfixh(tmp23 - tmp51); |
| 408 |
sh2.hShort2 = unfixh(tmp22 - tmp52); |
| 409 |
sh3.hShort1 = unfixh(tmp21 - tmp53); |
| 410 |
sh3.hShort2 = unfixh(tmp20 - tmp50); |
| 411 |
|
| 412 |
V8[0] = sh0.hInt; |
| 413 |
V8[1] = sh1.hInt; |
| 414 |
V8[2] = sh2.hInt; |
| 415 |
V8[3] = sh3.hInt; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
************************************************************************** |
| 420 |
* Performs 8x8 block-wise Forward Discrete Cosine Transform of the given |
| 421 |
* image plane and outputs result to the array of coefficients. Short |
| 422 |
*implementation. |
| 423 |
* This kernel is designed to process image by blocks of blocks8x8 that |
| 424 |
* utilize maximum warps capacity, assuming that it is enough of 8 threads |
| 425 |
* per block8x8. |
| 426 |
* |
| 427 |
* \param SrcDst [OUT] - Coefficients plane |
| 428 |
* \param ImgStride [IN] - Stride of SrcDst |
| 429 |
* |
| 430 |
* \return None |
| 431 |
*/ |
| 432 |
|
| 433 |
#define IMAD(a, b, c) (((a) * (b)) + (c)) |
| 434 |
#define IMUL(a, b) ((a) * (b)) |
| 435 |
|
| 436 |
__global__ void CUDAkernelShortDCT(short *SrcDst, int ImgStride) { |
| 437 |
// Handle to thread block group |
| 438 |
cg::thread_block cta = cg::this_thread_block(); |
| 439 |
__shared__ short block[KERS_BLOCK_HEIGHT * KERS_SMEMBLOCK_STRIDE]; |
| 440 |
int OffsThreadInRow = FMUL(threadIdx.y, BLOCK_SIZE) + threadIdx.x; |
| 441 |
int OffsThreadInCol = FMUL(threadIdx.z, BLOCK_SIZE); |
| 442 |
int OffsThrRowPermuted = |
| 443 |
(OffsThreadInRow & 0xFFFFFFE0) | |
| 444 |
((OffsThreadInRow << 1) | (OffsThreadInRow >> 4) & 0x1) & 0x1F; |
| 445 |
|
| 446 |
SrcDst += |
| 447 |
IMAD(IMAD(blockIdx.y, KERS_BLOCK_HEIGHT, OffsThreadInCol), ImgStride, |
| 448 |
IMAD(blockIdx.x, KERS_BLOCK_WIDTH, OffsThreadInRow * 2)); |
| 449 |
short *bl_ptr = |
| 450 |
block + IMAD(OffsThreadInCol, KERS_SMEMBLOCK_STRIDE, OffsThreadInRow * 2); |
| 451 |
|
| 452 |
// load data to shared memory (only first half of threads in each row performs |
| 453 |
// data moving (each thread moves 2 shorts) |
| 454 |
if (OffsThreadInRow < KERS_BLOCK_WIDTH_HALF) { |
| 455 |
#pragma unroll |
| 456 |
|
| 457 |
for (int i = 0; i < BLOCK_SIZE; i++) |
| 458 |
((int *)bl_ptr)[i * (KERS_SMEMBLOCK_STRIDE / 2)] = |
| 459 |
((int *)SrcDst)[i * (ImgStride / 2)]; |
| 460 |
} |
| 461 |
|
| 462 |
cg::sync(cta); |
| 463 |
CUDAshortInplaceDCT( |
| 464 |
block + OffsThreadInCol * KERS_SMEMBLOCK_STRIDE + OffsThrRowPermuted, |
| 465 |
KERS_SMEMBLOCK_STRIDE); |
| 466 |
cg::sync(cta); |
| 467 |
CUDAshortInplaceDCT((unsigned int *)(block + |
| 468 |
OffsThreadInRow * KERS_SMEMBLOCK_STRIDE + |
| 469 |
OffsThreadInCol)); |
| 470 |
cg::sync(cta); |
| 471 |
|
| 472 |
// store data to global memory (only first half of threads in each row |
| 473 |
// performs data moving (each thread moves 2 shorts) |
| 474 |
if (OffsThreadInRow < KERS_BLOCK_WIDTH_HALF) { |
| 475 |
#pragma unroll |
| 476 |
|
| 477 |
for (int i = 0; i < BLOCK_SIZE; i++) |
| 478 |
((int *)SrcDst)[i * (ImgStride / 2)] = |
| 479 |
((int *)bl_ptr)[i * (KERS_SMEMBLOCK_STRIDE / 2)]; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
************************************************************************** |
| 485 |
* Performs 8x8 block-wise Inverse Discrete Cosine Transform of the given |
| 486 |
* image plane and outputs result to the array of coefficients. Short |
| 487 |
*implementation. |
| 488 |
* This kernel is designed to process image by blocks of blocks8x8 that |
| 489 |
* utilize maximum warps capacity, assuming that it is enough of 8 threads |
| 490 |
* per block8x8. |
| 491 |
* |
| 492 |
* \param SrcDst [OUT] - Coefficients plane |
| 493 |
* \param ImgStride [IN] - Stride of SrcDst |
| 494 |
* |
| 495 |
* \return None |
| 496 |
*/ |
| 497 |
|
| 498 |
__global__ void CUDAkernelShortIDCT(short *SrcDst, int ImgStride) { |
| 499 |
// Handle to thread block group |
| 500 |
cg::thread_block cta = cg::this_thread_block(); |
| 501 |
__shared__ short block[KERS_BLOCK_HEIGHT * KERS_SMEMBLOCK_STRIDE]; |
| 502 |
|
| 503 |
int OffsThreadInRow = IMAD(threadIdx.y, BLOCK_SIZE, threadIdx.x); |
| 504 |
int OffsThreadInCol = IMUL(threadIdx.z, BLOCK_SIZE); |
| 505 |
int OffsThrRowPermuted = |
| 506 |
(OffsThreadInRow & 0xFFFFFFE0) | |
| 507 |
((OffsThreadInRow << 1) | (OffsThreadInRow >> 4) & 0x1) & 0x1F; |
| 508 |
|
| 509 |
SrcDst += |
| 510 |
IMAD(IMAD(blockIdx.y, KERS_BLOCK_HEIGHT, OffsThreadInCol), ImgStride, |
| 511 |
IMAD(blockIdx.x, KERS_BLOCK_WIDTH, OffsThreadInRow * 2)); |
| 512 |
short *bl_ptr = |
| 513 |
block + IMAD(OffsThreadInCol, KERS_SMEMBLOCK_STRIDE, OffsThreadInRow * 2); |
| 514 |
|
| 515 |
// load data to shared memory (only first half of threads in each row performs |
| 516 |
// data moving (each thread moves 2 shorts) |
| 517 |
if (OffsThreadInRow < KERS_BLOCK_WIDTH_HALF) { |
| 518 |
#pragma unroll |
| 519 |
|
| 520 |
for (int i = 0; i < BLOCK_SIZE; i++) |
| 521 |
((int *)bl_ptr)[i * (KERS_SMEMBLOCK_STRIDE / 2)] = |
| 522 |
((int *)SrcDst)[i * (ImgStride / 2)]; |
| 523 |
} |
| 524 |
|
| 525 |
cg::sync(cta); |
| 526 |
CUDAshortInplaceIDCT( |
| 527 |
block + OffsThreadInCol * KERS_SMEMBLOCK_STRIDE + OffsThrRowPermuted, |
| 528 |
KERS_SMEMBLOCK_STRIDE); |
| 529 |
cg::sync(cta); |
| 530 |
CUDAshortInplaceIDCT( |
| 531 |
(unsigned int *)(block + OffsThreadInRow * KERS_SMEMBLOCK_STRIDE + |
| 532 |
OffsThreadInCol)); |
| 533 |
cg::sync(cta); |
| 534 |
|
| 535 |
// store data to global memory (only first half of threads in each row |
| 536 |
// performs data moving (each thread moves 2 shorts) |
| 537 |
if (OffsThreadInRow < KERS_BLOCK_WIDTH_HALF) { |
| 538 |
#pragma unroll |
| 539 |
|
| 540 |
for (int i = 0; i < BLOCK_SIZE; i++) |
| 541 |
((int *)SrcDst)[i * (ImgStride / 2)] = |
| 542 |
((int *)bl_ptr)[i * (KERS_SMEMBLOCK_STRIDE / 2)]; |
| 543 |
} |
| 544 |
} |