| 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_kernel2.cu |
| 31 |
* \brief Contains 2nd kernel implementations of DCT and IDCT routines, used in |
| 32 |
* 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 done using floating point representation. |
| 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 |
|
| 47 |
#include "Common.h" |
| 48 |
|
| 49 |
// Used in forward and inverse DCT |
| 50 |
#define C_a 1.387039845322148f //!< a = (2^0.5) * cos( pi / 16); |
| 51 |
#define C_b 1.306562964876377f //!< b = (2^0.5) * cos( pi / 8); |
| 52 |
#define C_c 1.175875602419359f //!< c = (2^0.5) * cos(3 * pi / 16); |
| 53 |
#define C_d 0.785694958387102f //!< d = (2^0.5) * cos(5 * pi / 16); |
| 54 |
#define C_e 0.541196100146197f //!< e = (2^0.5) * cos(3 * pi / 8); |
| 55 |
#define C_f 0.275899379282943f //!< f = (2^0.5) * cos(7 * pi / 16); |
| 56 |
|
| 57 |
/** |
| 58 |
* Normalization constant that is used in forward and inverse DCT |
| 59 |
*/ |
| 60 |
#define C_norm 0.3535533905932737f // 1 / (8^0.5) |
| 61 |
|
| 62 |
/** |
| 63 |
* Width of data block (2nd kernel) |
| 64 |
*/ |
| 65 |
#define KER2_BLOCK_WIDTH 32 |
| 66 |
|
| 67 |
/** |
| 68 |
* Height of data block (2nd kernel) |
| 69 |
*/ |
| 70 |
#define KER2_BLOCK_HEIGHT 16 |
| 71 |
|
| 72 |
/** |
| 73 |
* LOG2 of width of data block (2nd kernel) |
| 74 |
*/ |
| 75 |
#define KER2_BW_LOG2 5 |
| 76 |
|
| 77 |
/** |
| 78 |
* LOG2 of height of data block (2nd kernel) |
| 79 |
*/ |
| 80 |
#define KER2_BH_LOG2 4 |
| 81 |
|
| 82 |
/** |
| 83 |
* Stride of shared memory buffer (2nd kernel) |
| 84 |
*/ |
| 85 |
#define KER2_SMEMBLOCK_STRIDE (KER2_BLOCK_WIDTH + 1) |
| 86 |
|
| 87 |
/** |
| 88 |
************************************************************************** |
| 89 |
* Performs in-place DCT of vector of 8 elements. |
| 90 |
* |
| 91 |
* \param Vect0 [IN/OUT] - Pointer to the first element of vector |
| 92 |
* \param Step [IN/OUT] - Value to add to ptr to access other elements |
| 93 |
* |
| 94 |
* \return None |
| 95 |
*/ |
| 96 |
__device__ void CUDAsubroutineInplaceDCTvector(float *Vect0, int Step) { |
| 97 |
float *Vect1 = Vect0 + Step; |
| 98 |
float *Vect2 = Vect1 + Step; |
| 99 |
float *Vect3 = Vect2 + Step; |
| 100 |
float *Vect4 = Vect3 + Step; |
| 101 |
float *Vect5 = Vect4 + Step; |
| 102 |
float *Vect6 = Vect5 + Step; |
| 103 |
float *Vect7 = Vect6 + Step; |
| 104 |
|
| 105 |
float X07P = (*Vect0) + (*Vect7); |
| 106 |
float X16P = (*Vect1) + (*Vect6); |
| 107 |
float X25P = (*Vect2) + (*Vect5); |
| 108 |
float X34P = (*Vect3) + (*Vect4); |
| 109 |
|
| 110 |
float X07M = (*Vect0) - (*Vect7); |
| 111 |
float X61M = (*Vect6) - (*Vect1); |
| 112 |
float X25M = (*Vect2) - (*Vect5); |
| 113 |
float X43M = (*Vect4) - (*Vect3); |
| 114 |
|
| 115 |
float X07P34PP = X07P + X34P; |
| 116 |
float X07P34PM = X07P - X34P; |
| 117 |
float X16P25PP = X16P + X25P; |
| 118 |
float X16P25PM = X16P - X25P; |
| 119 |
|
| 120 |
(*Vect0) = C_norm * (X07P34PP + X16P25PP); |
| 121 |
(*Vect2) = C_norm * (C_b * X07P34PM + C_e * X16P25PM); |
| 122 |
(*Vect4) = C_norm * (X07P34PP - X16P25PP); |
| 123 |
(*Vect6) = C_norm * (C_e * X07P34PM - C_b * X16P25PM); |
| 124 |
|
| 125 |
(*Vect1) = C_norm * (C_a * X07M - C_c * X61M + C_d * X25M - C_f * X43M); |
| 126 |
(*Vect3) = C_norm * (C_c * X07M + C_f * X61M - C_a * X25M + C_d * X43M); |
| 127 |
(*Vect5) = C_norm * (C_d * X07M + C_a * X61M + C_f * X25M - C_c * X43M); |
| 128 |
(*Vect7) = C_norm * (C_f * X07M + C_d * X61M + C_c * X25M + C_a * X43M); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
************************************************************************** |
| 133 |
* Performs in-place IDCT of vector of 8 elements. |
| 134 |
* |
| 135 |
* \param Vect0 [IN/OUT] - Pointer to the first element of vector |
| 136 |
* \param Step [IN/OUT] - Value to add to ptr to access other elements |
| 137 |
* |
| 138 |
* \return None |
| 139 |
*/ |
| 140 |
__device__ void CUDAsubroutineInplaceIDCTvector(float *Vect0, int Step) { |
| 141 |
float *Vect1 = Vect0 + Step; |
| 142 |
float *Vect2 = Vect1 + Step; |
| 143 |
float *Vect3 = Vect2 + Step; |
| 144 |
float *Vect4 = Vect3 + Step; |
| 145 |
float *Vect5 = Vect4 + Step; |
| 146 |
float *Vect6 = Vect5 + Step; |
| 147 |
float *Vect7 = Vect6 + Step; |
| 148 |
|
| 149 |
float Y04P = (*Vect0) + (*Vect4); |
| 150 |
float Y2b6eP = C_b * (*Vect2) + C_e * (*Vect6); |
| 151 |
|
| 152 |
float Y04P2b6ePP = Y04P + Y2b6eP; |
| 153 |
float Y04P2b6ePM = Y04P - Y2b6eP; |
| 154 |
float Y7f1aP3c5dPP = |
| 155 |
C_f * (*Vect7) + C_a * (*Vect1) + C_c * (*Vect3) + C_d * (*Vect5); |
| 156 |
float Y7a1fM3d5cMP = |
| 157 |
C_a * (*Vect7) - C_f * (*Vect1) + C_d * (*Vect3) - C_c * (*Vect5); |
| 158 |
|
| 159 |
float Y04M = (*Vect0) - (*Vect4); |
| 160 |
float Y2e6bM = C_e * (*Vect2) - C_b * (*Vect6); |
| 161 |
|
| 162 |
float Y04M2e6bMP = Y04M + Y2e6bM; |
| 163 |
float Y04M2e6bMM = Y04M - Y2e6bM; |
| 164 |
float Y1c7dM3f5aPM = |
| 165 |
C_c * (*Vect1) - C_d * (*Vect7) - C_f * (*Vect3) - C_a * (*Vect5); |
| 166 |
float Y1d7cP3a5fMM = |
| 167 |
C_d * (*Vect1) + C_c * (*Vect7) - C_a * (*Vect3) + C_f * (*Vect5); |
| 168 |
|
| 169 |
(*Vect0) = C_norm * (Y04P2b6ePP + Y7f1aP3c5dPP); |
| 170 |
(*Vect7) = C_norm * (Y04P2b6ePP - Y7f1aP3c5dPP); |
| 171 |
(*Vect4) = C_norm * (Y04P2b6ePM + Y7a1fM3d5cMP); |
| 172 |
(*Vect3) = C_norm * (Y04P2b6ePM - Y7a1fM3d5cMP); |
| 173 |
|
| 174 |
(*Vect1) = C_norm * (Y04M2e6bMP + Y1c7dM3f5aPM); |
| 175 |
(*Vect5) = C_norm * (Y04M2e6bMM - Y1d7cP3a5fMM); |
| 176 |
(*Vect2) = C_norm * (Y04M2e6bMM + Y1d7cP3a5fMM); |
| 177 |
(*Vect6) = C_norm * (Y04M2e6bMP - Y1c7dM3f5aPM); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
************************************************************************** |
| 182 |
* Performs 8x8 block-wise Forward Discrete Cosine Transform of the given |
| 183 |
* image plane and outputs result to the array of coefficients. 2nd |
| 184 |
*implementation. |
| 185 |
* This kernel is designed to process image by blocks of blocks8x8 that |
| 186 |
* utilizes maximum warps capacity, assuming that it is enough of 8 threads |
| 187 |
* per block8x8. |
| 188 |
* |
| 189 |
* \param SrcDst [OUT] - Coefficients plane |
| 190 |
* \param ImgStride [IN] - Stride of SrcDst |
| 191 |
* |
| 192 |
* \return None |
| 193 |
*/ |
| 194 |
|
| 195 |
__global__ void CUDAkernel2DCT(float *dst, float *src, int ImgStride) { |
| 196 |
// Handle to thread block group |
| 197 |
cg::thread_block cta = cg::this_thread_block(); |
| 198 |
|
| 199 |
__shared__ float block[KER2_BLOCK_HEIGHT * KER2_SMEMBLOCK_STRIDE]; |
| 200 |
|
| 201 |
int OffsThreadInRow = threadIdx.y * BLOCK_SIZE + threadIdx.x; |
| 202 |
int OffsThreadInCol = threadIdx.z * BLOCK_SIZE; |
| 203 |
src += FMUL(blockIdx.y * KER2_BLOCK_HEIGHT + OffsThreadInCol, ImgStride) + |
| 204 |
blockIdx.x * KER2_BLOCK_WIDTH + OffsThreadInRow; |
| 205 |
dst += FMUL(blockIdx.y * KER2_BLOCK_HEIGHT + OffsThreadInCol, ImgStride) + |
| 206 |
blockIdx.x * KER2_BLOCK_WIDTH + OffsThreadInRow; |
| 207 |
float *bl_ptr = |
| 208 |
block + OffsThreadInCol * KER2_SMEMBLOCK_STRIDE + OffsThreadInRow; |
| 209 |
|
| 210 |
#pragma unroll |
| 211 |
|
| 212 |
for (unsigned int i = 0; i < BLOCK_SIZE; i++) |
| 213 |
bl_ptr[i * KER2_SMEMBLOCK_STRIDE] = src[i * ImgStride]; |
| 214 |
|
| 215 |
cg::sync(cta); |
| 216 |
// process rows |
| 217 |
CUDAsubroutineInplaceDCTvector( |
| 218 |
block + (OffsThreadInCol + threadIdx.x) * KER2_SMEMBLOCK_STRIDE + |
| 219 |
OffsThreadInRow - threadIdx.x, |
| 220 |
1); |
| 221 |
|
| 222 |
cg::sync(cta); |
| 223 |
// process columns |
| 224 |
CUDAsubroutineInplaceDCTvector(bl_ptr, KER2_SMEMBLOCK_STRIDE); |
| 225 |
|
| 226 |
cg::sync(cta); |
| 227 |
for (unsigned int i = 0; i < BLOCK_SIZE; i++) |
| 228 |
dst[i * ImgStride] = bl_ptr[i * KER2_SMEMBLOCK_STRIDE]; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
************************************************************************** |
| 233 |
* Performs 8x8 block-wise Inverse Discrete Cosine Transform of the given |
| 234 |
* coefficients plane and outputs result to the image. 2nd implementation. |
| 235 |
* This kernel is designed to process image by blocks of blocks8x8 that |
| 236 |
* utilizes maximum warps capacity, assuming that it is enough of 8 threads |
| 237 |
* per block8x8. |
| 238 |
* |
| 239 |
* \param SrcDst [OUT] - Coefficients plane |
| 240 |
* \param ImgStride [IN] - Stride of SrcDst |
| 241 |
* |
| 242 |
* \return None |
| 243 |
*/ |
| 244 |
|
| 245 |
__global__ void CUDAkernel2IDCT(float *dst, float *src, int ImgStride) { |
| 246 |
// Handle to thread block group |
| 247 |
cg::thread_block cta = cg::this_thread_block(); |
| 248 |
|
| 249 |
__shared__ float block[KER2_BLOCK_HEIGHT * KER2_SMEMBLOCK_STRIDE]; |
| 250 |
|
| 251 |
int OffsThreadInRow = threadIdx.y * BLOCK_SIZE + threadIdx.x; |
| 252 |
int OffsThreadInCol = threadIdx.z * BLOCK_SIZE; |
| 253 |
src += FMUL(blockIdx.y * KER2_BLOCK_HEIGHT + OffsThreadInCol, ImgStride) + |
| 254 |
blockIdx.x * KER2_BLOCK_WIDTH + OffsThreadInRow; |
| 255 |
dst += FMUL(blockIdx.y * KER2_BLOCK_HEIGHT + OffsThreadInCol, ImgStride) + |
| 256 |
blockIdx.x * KER2_BLOCK_WIDTH + OffsThreadInRow; |
| 257 |
float *bl_ptr = |
| 258 |
block + OffsThreadInCol * KER2_SMEMBLOCK_STRIDE + OffsThreadInRow; |
| 259 |
|
| 260 |
#pragma unroll |
| 261 |
|
| 262 |
for (unsigned int i = 0; i < BLOCK_SIZE; i++) |
| 263 |
bl_ptr[i * KER2_SMEMBLOCK_STRIDE] = src[i * ImgStride]; |
| 264 |
|
| 265 |
cg::sync(cta); |
| 266 |
// process rows |
| 267 |
CUDAsubroutineInplaceIDCTvector( |
| 268 |
block + (OffsThreadInCol + threadIdx.x) * KER2_SMEMBLOCK_STRIDE + |
| 269 |
OffsThreadInRow - threadIdx.x, |
| 270 |
1); |
| 271 |
|
| 272 |
cg::sync(cta); |
| 273 |
// process columns |
| 274 |
CUDAsubroutineInplaceIDCTvector(bl_ptr, KER2_SMEMBLOCK_STRIDE); |
| 275 |
|
| 276 |
cg::sync(cta); |
| 277 |
|
| 278 |
for (unsigned int i = 0; i < BLOCK_SIZE; i++) |
| 279 |
dst[i * ImgStride] = bl_ptr[i * KER2_SMEMBLOCK_STRIDE]; |
| 280 |
} |