| 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_kernel1.cu |
| 31 |
* \brief Contains 1st CUDA implementations of DCT, IDCT and quantization |
| 32 |
*routines, |
| 33 |
* used in JPEG internal data processing. Device code. |
| 34 |
* |
| 35 |
* This code implements first CUDA versions of forward and inverse Discrete |
| 36 |
*Cosine |
| 37 |
* Transform to blocks of image pixels (of 8x8 size), as in JPEG standard. The |
| 38 |
*data |
| 39 |
* processing is done using floating point representation. |
| 40 |
* The routine that performs quantization of coefficients can be found in |
| 41 |
* dct8x8_kernel_quantization.cu file. |
| 42 |
*/ |
| 43 |
#pragma once |
| 44 |
#include <cooperative_groups.h> |
| 45 |
|
| 46 |
namespace cg = cooperative_groups; |
| 47 |
#include "Common.h" |
| 48 |
|
| 49 |
/** |
| 50 |
* This unitary matrix performs discrete cosine transform of rows of the matrix |
| 51 |
* to the left |
| 52 |
*/ |
| 53 |
__constant__ float DCTv8matrix[] = { |
| 54 |
0.3535533905932738f, 0.4903926402016152f, 0.4619397662556434f, 0.4157348061512726f, 0.3535533905932738f, 0.2777851165098011f, 0.1913417161825449f, 0.0975451610080642f, |
| 55 |
0.3535533905932738f, 0.4157348061512726f, 0.1913417161825449f, -0.0975451610080641f, -0.3535533905932737f, -0.4903926402016152f, -0.4619397662556434f, -0.2777851165098011f, |
| 56 |
0.3535533905932738f, 0.2777851165098011f, -0.1913417161825449f, -0.4903926402016152f, -0.3535533905932738f, 0.0975451610080642f, 0.4619397662556433f, 0.4157348061512727f, |
| 57 |
0.3535533905932738f, 0.0975451610080642f, -0.4619397662556434f, -0.2777851165098011f, 0.3535533905932737f, 0.4157348061512727f, -0.1913417161825450f, -0.4903926402016153f, |
| 58 |
0.3535533905932738f, -0.0975451610080641f, -0.4619397662556434f, 0.2777851165098009f, 0.3535533905932738f, -0.4157348061512726f, -0.1913417161825453f, 0.4903926402016152f, |
| 59 |
0.3535533905932738f, -0.2777851165098010f, -0.1913417161825452f, 0.4903926402016153f, -0.3535533905932733f, -0.0975451610080649f, 0.4619397662556437f, -0.4157348061512720f, |
| 60 |
0.3535533905932738f, -0.4157348061512727f, 0.1913417161825450f, 0.0975451610080640f, -0.3535533905932736f, 0.4903926402016152f, -0.4619397662556435f, 0.2777851165098022f, |
| 61 |
0.3535533905932738f, -0.4903926402016152f, 0.4619397662556433f, -0.4157348061512721f, 0.3535533905932733f, -0.2777851165098008f, 0.1913417161825431f, -0.0975451610080625f |
| 62 |
}; |
| 63 |
|
| 64 |
// Temporary blocks |
| 65 |
__shared__ float CurBlockLocal1[BLOCK_SIZE2]; |
| 66 |
__shared__ float CurBlockLocal2[BLOCK_SIZE2]; |
| 67 |
|
| 68 |
/** |
| 69 |
************************************************************************** |
| 70 |
* Performs 1st implementation of 8x8 block-wise Forward Discrete Cosine |
| 71 |
*Transform of the given |
| 72 |
* image plane and outputs result to the array of coefficients. |
| 73 |
* |
| 74 |
* \param Dst [OUT] - Coefficients plane |
| 75 |
* \param ImgWidth [IN] - Stride of Dst |
| 76 |
* \param OffsetXBlocks [IN] - Offset along X in blocks from which to perform |
| 77 |
*processing |
| 78 |
* \param OffsetYBlocks [IN] - Offset along Y in blocks from which to perform |
| 79 |
*processing |
| 80 |
* |
| 81 |
* \return None |
| 82 |
*/ |
| 83 |
__global__ void CUDAkernel1DCT(float *Dst, int ImgWidth, int OffsetXBlocks, |
| 84 |
int OffsetYBlocks, cudaTextureObject_t TexSrc) { |
| 85 |
// Handle to thread block group |
| 86 |
cg::thread_block cta = cg::this_thread_block(); |
| 87 |
// Block index |
| 88 |
const int bx = blockIdx.x + OffsetXBlocks; |
| 89 |
const int by = blockIdx.y + OffsetYBlocks; |
| 90 |
|
| 91 |
// Thread index (current coefficient) |
| 92 |
const int tx = threadIdx.x; |
| 93 |
const int ty = threadIdx.y; |
| 94 |
|
| 95 |
// Texture coordinates |
| 96 |
const float tex_x = (float)((bx << BLOCK_SIZE_LOG2) + tx) + 0.5f; |
| 97 |
const float tex_y = (float)((by << BLOCK_SIZE_LOG2) + ty) + 0.5f; |
| 98 |
|
| 99 |
// copy current image pixel to the first block |
| 100 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = |
| 101 |
tex2D<float>(TexSrc, tex_x, tex_y); |
| 102 |
|
| 103 |
// synchronize threads to make sure the block is copied |
| 104 |
cg::sync(cta); |
| 105 |
|
| 106 |
// calculate the multiplication of DCTv8matrixT * A and place it in the second |
| 107 |
// block |
| 108 |
float curelem = 0; |
| 109 |
int DCTv8matrixIndex = 0 * BLOCK_SIZE + ty; |
| 110 |
int CurBlockLocal1Index = 0 * BLOCK_SIZE + tx; |
| 111 |
#pragma unroll |
| 112 |
|
| 113 |
for (int i = 0; i < BLOCK_SIZE; i++) { |
| 114 |
curelem += |
| 115 |
DCTv8matrix[DCTv8matrixIndex] * CurBlockLocal1[CurBlockLocal1Index]; |
| 116 |
DCTv8matrixIndex += BLOCK_SIZE; |
| 117 |
CurBlockLocal1Index += BLOCK_SIZE; |
| 118 |
} |
| 119 |
|
| 120 |
CurBlockLocal2[(ty << BLOCK_SIZE_LOG2) + tx] = curelem; |
| 121 |
|
| 122 |
// synchronize threads to make sure the first 2 matrices are multiplied and |
| 123 |
// the result is stored in the second block |
| 124 |
cg::sync(cta); |
| 125 |
|
| 126 |
// calculate the multiplication of (DCTv8matrixT * A) * DCTv8matrix and place |
| 127 |
// it in the first block |
| 128 |
curelem = 0; |
| 129 |
int CurBlockLocal2Index = (ty << BLOCK_SIZE_LOG2) + 0; |
| 130 |
DCTv8matrixIndex = 0 * BLOCK_SIZE + tx; |
| 131 |
#pragma unroll |
| 132 |
|
| 133 |
for (int i = 0; i < BLOCK_SIZE; i++) { |
| 134 |
curelem += |
| 135 |
CurBlockLocal2[CurBlockLocal2Index] * DCTv8matrix[DCTv8matrixIndex]; |
| 136 |
CurBlockLocal2Index += 1; |
| 137 |
DCTv8matrixIndex += BLOCK_SIZE; |
| 138 |
} |
| 139 |
|
| 140 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = curelem; |
| 141 |
|
| 142 |
// synchronize threads to make sure the matrices are multiplied and the result |
| 143 |
// is stored back in the first block |
| 144 |
cg::sync(cta); |
| 145 |
|
| 146 |
// copy current coefficient to its place in the result array |
| 147 |
Dst[FMUL(((by << BLOCK_SIZE_LOG2) + ty), ImgWidth) + |
| 148 |
((bx << BLOCK_SIZE_LOG2) + tx)] = |
| 149 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx]; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
************************************************************************** |
| 154 |
* Performs 1st implementation of 8x8 block-wise Inverse Discrete Cosine |
| 155 |
*Transform of the given |
| 156 |
* DCT coefficients plane and outputs result to the image array |
| 157 |
* |
| 158 |
* \param Dst [OUT] - Image plane |
| 159 |
* \param ImgWidth [IN] - Stride of Dst |
| 160 |
* \param OffsetXBlocks [IN] - Offset along X in blocks from which to perform |
| 161 |
*processing |
| 162 |
* \param OffsetYBlocks [IN] - Offset along Y in blocks from which to perform |
| 163 |
*processing |
| 164 |
* |
| 165 |
* \return None |
| 166 |
*/ |
| 167 |
__global__ void CUDAkernel1IDCT(float *Dst, int ImgWidth, int OffsetXBlocks, |
| 168 |
int OffsetYBlocks, cudaTextureObject_t TexSrc) { |
| 169 |
// Handle to thread block group |
| 170 |
cg::thread_block cta = cg::this_thread_block(); |
| 171 |
// Block index |
| 172 |
int bx = blockIdx.x + OffsetXBlocks; |
| 173 |
int by = blockIdx.y + OffsetYBlocks; |
| 174 |
|
| 175 |
// Thread index (current image pixel) |
| 176 |
int tx = threadIdx.x; |
| 177 |
int ty = threadIdx.y; |
| 178 |
|
| 179 |
// Texture coordinates |
| 180 |
const float tex_x = (float)((bx << BLOCK_SIZE_LOG2) + tx) + 0.5f; |
| 181 |
const float tex_y = (float)((by << BLOCK_SIZE_LOG2) + ty) + 0.5f; |
| 182 |
|
| 183 |
// copy current image pixel to the first block |
| 184 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = |
| 185 |
tex2D<float>(TexSrc, tex_x, tex_y); |
| 186 |
|
| 187 |
// synchronize threads to make sure the block is copied |
| 188 |
cg::sync(cta); |
| 189 |
|
| 190 |
// calculate the multiplication of DCTv8matrix * A and place it in the second |
| 191 |
// block |
| 192 |
float curelem = 0; |
| 193 |
int DCTv8matrixIndex = (ty << BLOCK_SIZE_LOG2) + 0; |
| 194 |
int CurBlockLocal1Index = 0 * BLOCK_SIZE + tx; |
| 195 |
#pragma unroll |
| 196 |
|
| 197 |
for (int i = 0; i < BLOCK_SIZE; i++) { |
| 198 |
curelem += |
| 199 |
DCTv8matrix[DCTv8matrixIndex] * CurBlockLocal1[CurBlockLocal1Index]; |
| 200 |
DCTv8matrixIndex += 1; |
| 201 |
CurBlockLocal1Index += BLOCK_SIZE; |
| 202 |
} |
| 203 |
|
| 204 |
CurBlockLocal2[(ty << BLOCK_SIZE_LOG2) + tx] = curelem; |
| 205 |
|
| 206 |
// synchronize threads to make sure the first 2 matrices are multiplied and |
| 207 |
// the result is stored in the second block |
| 208 |
cg::sync(cta); |
| 209 |
|
| 210 |
// calculate the multiplication of (DCTv8matrix * A) * DCTv8matrixT and place |
| 211 |
// it in the first block |
| 212 |
curelem = 0; |
| 213 |
int CurBlockLocal2Index = (ty << BLOCK_SIZE_LOG2) + 0; |
| 214 |
DCTv8matrixIndex = (tx << BLOCK_SIZE_LOG2) + 0; |
| 215 |
#pragma unroll |
| 216 |
|
| 217 |
for (int i = 0; i < BLOCK_SIZE; i++) { |
| 218 |
curelem += |
| 219 |
CurBlockLocal2[CurBlockLocal2Index] * DCTv8matrix[DCTv8matrixIndex]; |
| 220 |
CurBlockLocal2Index += 1; |
| 221 |
DCTv8matrixIndex += 1; |
| 222 |
} |
| 223 |
|
| 224 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = curelem; |
| 225 |
|
| 226 |
// synchronize threads to make sure the matrices are multiplied and the result |
| 227 |
// is stored back in the first block |
| 228 |
cg::sync(cta); |
| 229 |
|
| 230 |
// copy current coefficient to its place in the result array |
| 231 |
Dst[FMUL(((by << BLOCK_SIZE_LOG2) + ty), ImgWidth) + |
| 232 |
((bx << BLOCK_SIZE_LOG2) + tx)] = |
| 233 |
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx]; |
| 234 |
} |