Debian Codegraph @

Samples/2_Concepts_and_Techniques/dct8x8/dct8x8_kernel_quantization.cuh

raw
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_quantization.cu
31 * \brief Contains unoptimized quantization routines. Device code.
32 *
33 * This code implements CUDA versions of quantization of Discrete Cosine
34 * Transform coefficients with 8x8 blocks for float and short arrays.
35 */
36
37 #pragma once
38 #include <cooperative_groups.h>
39
40 namespace cg = cooperative_groups;
41 #include "Common.h"
42
43 /**
44 * JPEG quality=0_of_12 quantization matrix
45 */
46 __constant__ short Q[] = {
47 32, 33, 51, 81, 66, 39, 34, 17,
48 33, 36, 48, 47, 28, 23, 12, 12,
49 51, 48, 47, 28, 23, 12, 12, 12,
50 81, 47, 28, 23, 12, 12, 12, 12,
51 66, 28, 23, 12, 12, 12, 12, 12,
52 39, 23, 12, 12, 12, 12, 12, 12,
53 34, 12, 12, 12, 12, 12, 12, 12,
54 17, 12, 12, 12, 12, 12, 12, 12
55 };
56
57 /**
58 **************************************************************************
59 * Performs in-place quantization of given DCT coefficients plane using
60 * predefined quantization matrices (for floats plane). Unoptimized.
61 *
62 * \param SrcDst [IN/OUT] - DCT coefficients plane
63 * \param Stride [IN] - Stride of SrcDst
64 *
65 * \return None
66 */
67 __global__ void CUDAkernelQuantizationFloat(float *SrcDst, int Stride) {
68 // Block index
69 int bx = blockIdx.x;
70 int by = blockIdx.y;
71
72 // Thread index (current coefficient)
73 int tx = threadIdx.x;
74 int ty = threadIdx.y;
75
76 // copy current coefficient to the local variable
77 float curCoef =
78 SrcDst[(by * BLOCK_SIZE + ty) * Stride + (bx * BLOCK_SIZE + tx)];
79 float curQuant = (float)Q[ty * BLOCK_SIZE + tx];
80
81 // quantize the current coefficient
82 float quantized = roundf(curCoef / curQuant);
83 curCoef = quantized * curQuant;
84
85 // copy quantized coefficient back to the DCT-plane
86 SrcDst[(by * BLOCK_SIZE + ty) * Stride + (bx * BLOCK_SIZE + tx)] = curCoef;
87 }
88
89 /**
90 **************************************************************************
91 * Performs in-place quantization of given DCT coefficients plane using
92 * predefined quantization matrices (for shorts plane). Unoptimized.
93 *
94 * \param SrcDst [IN/OUT] - DCT coefficients plane
95 * \param Stride [IN] - Stride of SrcDst
96 *
97 * \return None
98 */
99 __global__ void CUDAkernelQuantizationShort(short *SrcDst, int Stride) {
100 // Handle to thread block group
101 cg::thread_block cta = cg::this_thread_block();
102 // Block index
103 int bx = blockIdx.x;
104 int by = blockIdx.y;
105
106 // Thread index (current coefficient)
107 int tx = threadIdx.x;
108 int ty = threadIdx.y;
109
110 // copy current coefficient to the local variable
111 short curCoef =
112 SrcDst[(by * BLOCK_SIZE + ty) * Stride + (bx * BLOCK_SIZE + tx)];
113 short curQuant = Q[ty * BLOCK_SIZE + tx];
114
115 // quantize the current coefficient
116 if (curCoef < 0) {
117 curCoef = -curCoef;
118 curCoef += curQuant >> 1;
119 curCoef /= curQuant;
120 curCoef = -curCoef;
121 } else {
122 curCoef += curQuant >> 1;
123 curCoef /= curQuant;
124 }
125
126 cg::sync(cta);
127
128 curCoef = curCoef * curQuant;
129
130 // copy quantized coefficient back to the DCT-plane
131 SrcDst[(by * BLOCK_SIZE + ty) * Stride + (bx * BLOCK_SIZE + tx)] = curCoef;
132 }