forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dct8x8_kernel1.cuh
234 lines (202 loc) · 9.38 KB
/
dct8x8_kernel1.cuh
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
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
**************************************************************************
* \file dct8x8_kernel1.cu
* \brief Contains 1st CUDA implementations of DCT, IDCT and quantization
*routines,
* used in JPEG internal data processing. Device code.
*
* This code implements first CUDA versions of forward and inverse Discrete
*Cosine
* Transform to blocks of image pixels (of 8x8 size), as in JPEG standard. The
*data
* processing is done using floating point representation.
* The routine that performs quantization of coefficients can be found in
* dct8x8_kernel_quantization.cu file.
*/
#pragma once
#include <cooperative_groups.h>
namespace cg = cooperative_groups;
#include "Common.h"
/**
* This unitary matrix performs discrete cosine transform of rows of the matrix
* to the left
*/
__constant__ float DCTv8matrix[] = {
0.3535533905932738f, 0.4903926402016152f, 0.4619397662556434f, 0.4157348061512726f, 0.3535533905932738f, 0.2777851165098011f, 0.1913417161825449f, 0.0975451610080642f,
0.3535533905932738f, 0.4157348061512726f, 0.1913417161825449f, -0.0975451610080641f, -0.3535533905932737f, -0.4903926402016152f, -0.4619397662556434f, -0.2777851165098011f,
0.3535533905932738f, 0.2777851165098011f, -0.1913417161825449f, -0.4903926402016152f, -0.3535533905932738f, 0.0975451610080642f, 0.4619397662556433f, 0.4157348061512727f,
0.3535533905932738f, 0.0975451610080642f, -0.4619397662556434f, -0.2777851165098011f, 0.3535533905932737f, 0.4157348061512727f, -0.1913417161825450f, -0.4903926402016153f,
0.3535533905932738f, -0.0975451610080641f, -0.4619397662556434f, 0.2777851165098009f, 0.3535533905932738f, -0.4157348061512726f, -0.1913417161825453f, 0.4903926402016152f,
0.3535533905932738f, -0.2777851165098010f, -0.1913417161825452f, 0.4903926402016153f, -0.3535533905932733f, -0.0975451610080649f, 0.4619397662556437f, -0.4157348061512720f,
0.3535533905932738f, -0.4157348061512727f, 0.1913417161825450f, 0.0975451610080640f, -0.3535533905932736f, 0.4903926402016152f, -0.4619397662556435f, 0.2777851165098022f,
0.3535533905932738f, -0.4903926402016152f, 0.4619397662556433f, -0.4157348061512721f, 0.3535533905932733f, -0.2777851165098008f, 0.1913417161825431f, -0.0975451610080625f
};
// Temporary blocks
__shared__ float CurBlockLocal1[BLOCK_SIZE2];
__shared__ float CurBlockLocal2[BLOCK_SIZE2];
/**
**************************************************************************
* Performs 1st implementation of 8x8 block-wise Forward Discrete Cosine
*Transform of the given
* image plane and outputs result to the array of coefficients.
*
* \param Dst [OUT] - Coefficients plane
* \param ImgWidth [IN] - Stride of Dst
* \param OffsetXBlocks [IN] - Offset along X in blocks from which to perform
*processing
* \param OffsetYBlocks [IN] - Offset along Y in blocks from which to perform
*processing
*
* \return None
*/
__global__ void CUDAkernel1DCT(float *Dst, int ImgWidth, int OffsetXBlocks,
int OffsetYBlocks, cudaTextureObject_t TexSrc) {
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
// Block index
const int bx = blockIdx.x + OffsetXBlocks;
const int by = blockIdx.y + OffsetYBlocks;
// Thread index (current coefficient)
const int tx = threadIdx.x;
const int ty = threadIdx.y;
// Texture coordinates
const float tex_x = (float)((bx << BLOCK_SIZE_LOG2) + tx) + 0.5f;
const float tex_y = (float)((by << BLOCK_SIZE_LOG2) + ty) + 0.5f;
// copy current image pixel to the first block
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] =
tex2D<float>(TexSrc, tex_x, tex_y);
// synchronize threads to make sure the block is copied
cg::sync(cta);
// calculate the multiplication of DCTv8matrixT * A and place it in the second
// block
float curelem = 0;
int DCTv8matrixIndex = 0 * BLOCK_SIZE + ty;
int CurBlockLocal1Index = 0 * BLOCK_SIZE + tx;
#pragma unroll
for (int i = 0; i < BLOCK_SIZE; i++) {
curelem +=
DCTv8matrix[DCTv8matrixIndex] * CurBlockLocal1[CurBlockLocal1Index];
DCTv8matrixIndex += BLOCK_SIZE;
CurBlockLocal1Index += BLOCK_SIZE;
}
CurBlockLocal2[(ty << BLOCK_SIZE_LOG2) + tx] = curelem;
// synchronize threads to make sure the first 2 matrices are multiplied and
// the result is stored in the second block
cg::sync(cta);
// calculate the multiplication of (DCTv8matrixT * A) * DCTv8matrix and place
// it in the first block
curelem = 0;
int CurBlockLocal2Index = (ty << BLOCK_SIZE_LOG2) + 0;
DCTv8matrixIndex = 0 * BLOCK_SIZE + tx;
#pragma unroll
for (int i = 0; i < BLOCK_SIZE; i++) {
curelem +=
CurBlockLocal2[CurBlockLocal2Index] * DCTv8matrix[DCTv8matrixIndex];
CurBlockLocal2Index += 1;
DCTv8matrixIndex += BLOCK_SIZE;
}
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = curelem;
// synchronize threads to make sure the matrices are multiplied and the result
// is stored back in the first block
cg::sync(cta);
// copy current coefficient to its place in the result array
Dst[FMUL(((by << BLOCK_SIZE_LOG2) + ty), ImgWidth) +
((bx << BLOCK_SIZE_LOG2) + tx)] =
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx];
}
/**
**************************************************************************
* Performs 1st implementation of 8x8 block-wise Inverse Discrete Cosine
*Transform of the given
* DCT coefficients plane and outputs result to the image array
*
* \param Dst [OUT] - Image plane
* \param ImgWidth [IN] - Stride of Dst
* \param OffsetXBlocks [IN] - Offset along X in blocks from which to perform
*processing
* \param OffsetYBlocks [IN] - Offset along Y in blocks from which to perform
*processing
*
* \return None
*/
__global__ void CUDAkernel1IDCT(float *Dst, int ImgWidth, int OffsetXBlocks,
int OffsetYBlocks, cudaTextureObject_t TexSrc) {
// Handle to thread block group
cg::thread_block cta = cg::this_thread_block();
// Block index
int bx = blockIdx.x + OffsetXBlocks;
int by = blockIdx.y + OffsetYBlocks;
// Thread index (current image pixel)
int tx = threadIdx.x;
int ty = threadIdx.y;
// Texture coordinates
const float tex_x = (float)((bx << BLOCK_SIZE_LOG2) + tx) + 0.5f;
const float tex_y = (float)((by << BLOCK_SIZE_LOG2) + ty) + 0.5f;
// copy current image pixel to the first block
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] =
tex2D<float>(TexSrc, tex_x, tex_y);
// synchronize threads to make sure the block is copied
cg::sync(cta);
// calculate the multiplication of DCTv8matrix * A and place it in the second
// block
float curelem = 0;
int DCTv8matrixIndex = (ty << BLOCK_SIZE_LOG2) + 0;
int CurBlockLocal1Index = 0 * BLOCK_SIZE + tx;
#pragma unroll
for (int i = 0; i < BLOCK_SIZE; i++) {
curelem +=
DCTv8matrix[DCTv8matrixIndex] * CurBlockLocal1[CurBlockLocal1Index];
DCTv8matrixIndex += 1;
CurBlockLocal1Index += BLOCK_SIZE;
}
CurBlockLocal2[(ty << BLOCK_SIZE_LOG2) + tx] = curelem;
// synchronize threads to make sure the first 2 matrices are multiplied and
// the result is stored in the second block
cg::sync(cta);
// calculate the multiplication of (DCTv8matrix * A) * DCTv8matrixT and place
// it in the first block
curelem = 0;
int CurBlockLocal2Index = (ty << BLOCK_SIZE_LOG2) + 0;
DCTv8matrixIndex = (tx << BLOCK_SIZE_LOG2) + 0;
#pragma unroll
for (int i = 0; i < BLOCK_SIZE; i++) {
curelem +=
CurBlockLocal2[CurBlockLocal2Index] * DCTv8matrix[DCTv8matrixIndex];
CurBlockLocal2Index += 1;
DCTv8matrixIndex += 1;
}
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx] = curelem;
// synchronize threads to make sure the matrices are multiplied and the result
// is stored back in the first block
cg::sync(cta);
// copy current coefficient to its place in the result array
Dst[FMUL(((by << BLOCK_SIZE_LOG2) + ty), ImgWidth) +
((bx << BLOCK_SIZE_LOG2) + tx)] =
CurBlockLocal1[(ty << BLOCK_SIZE_LOG2) + tx];
}