Debian Codegraph @

Samples/2_Concepts_and_Techniques/dct8x8/DCT8x8_Gold.cpp

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_Gold.cpp
31 * \brief Contains DCT, IDCT and quantization routines, used in JPEG internal
32 * data processing. Host code.
33 *
34 * This sample implements forward and inverse Discrete Cosine Transform to blocks
35 * of image pixels (of 8x8 size), as in JPEG standard. The data processing is
36 *done
37 * using floating point representation.
38 * The routine that performs quantization of coefficients is also included.
39 */
40
41 #include "Common.h"
42 #include "BmpUtil.h"
43
44 /**
45 * This unitary matrix performs DCT of rows of the matrix to the left
46 */
47 const float DCTv8matrix[BLOCK_SIZE2] = {
48 0.3535533905932738f, 0.4903926402016152f, 0.4619397662556434f, 0.4157348061512726f, 0.3535533905932738f, 0.2777851165098011f, 0.1913417161825449f, 0.0975451610080642f,
49 0.3535533905932738f, 0.4157348061512726f, 0.1913417161825449f, -0.0975451610080641f, -0.3535533905932737f, -0.4903926402016152f, -0.4619397662556434f, -0.2777851165098011f,
50 0.3535533905932738f, 0.2777851165098011f, -0.1913417161825449f, -0.4903926402016152f, -0.3535533905932738f, 0.0975451610080642f, 0.4619397662556433f, 0.4157348061512727f,
51 0.3535533905932738f, 0.0975451610080642f, -0.4619397662556434f, -0.2777851165098011f, 0.3535533905932737f, 0.4157348061512727f, -0.1913417161825450f, -0.4903926402016153f,
52 0.3535533905932738f, -0.0975451610080641f, -0.4619397662556434f, 0.2777851165098009f, 0.3535533905932738f, -0.4157348061512726f, -0.1913417161825453f, 0.4903926402016152f,
53 0.3535533905932738f, -0.2777851165098010f, -0.1913417161825452f, 0.4903926402016153f, -0.3535533905932733f, -0.0975451610080649f, 0.4619397662556437f, -0.4157348061512720f,
54 0.3535533905932738f, -0.4157348061512727f, 0.1913417161825450f, 0.0975451610080640f, -0.3535533905932736f, 0.4903926402016152f, -0.4619397662556435f, 0.2777851165098022f,
55 0.3535533905932738f, -0.4903926402016152f, 0.4619397662556433f, -0.4157348061512721f, 0.3535533905932733f, -0.2777851165098008f, 0.1913417161825431f, -0.0975451610080625f
56 };
57
58 /**
59 * This unitary matrix performs DCT of columns of the matrix to the right
60 */
61 const float DCTv8matrixT[BLOCK_SIZE2] = {
62 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f, 0.3535533905932738f,
63 0.4903926402016152f, 0.4157348061512726f, 0.2777851165098011f, 0.0975451610080642f, -0.0975451610080641f, -0.2777851165098010f, -0.4157348061512727f, -0.4903926402016152f,
64 0.4619397662556434f, 0.1913417161825449f, -0.1913417161825449f, -0.4619397662556434f, -0.4619397662556434f, -0.1913417161825452f, 0.1913417161825450f, 0.4619397662556433f,
65 0.4157348061512726f, -0.0975451610080641f, -0.4903926402016152f, -0.2777851165098011f, 0.2777851165098009f, 0.4903926402016153f, 0.0975451610080640f, -0.4157348061512721f,
66 0.3535533905932738f, -0.3535533905932737f, -0.3535533905932738f, 0.3535533905932737f, 0.3535533905932738f, -0.3535533905932733f, -0.3535533905932736f, 0.3535533905932733f,
67 0.2777851165098011f, -0.4903926402016152f, 0.0975451610080642f, 0.4157348061512727f, -0.4157348061512726f, -0.0975451610080649f, 0.4903926402016152f, -0.2777851165098008f,
68 0.1913417161825449f, -0.4619397662556434f, 0.4619397662556433f, -0.1913417161825450f, -0.1913417161825453f, 0.4619397662556437f, -0.4619397662556435f, 0.1913417161825431f,
69 0.0975451610080642f, -0.2777851165098011f, 0.4157348061512727f, -0.4903926402016153f, 0.4903926402016152f, -0.4157348061512720f, 0.2777851165098022f, -0.0975451610080625f
70 };
71
72 /**
73 * JPEG quality=0_of_12 quantization matrix
74 */
75 float Q[BLOCK_SIZE2] = {
76 32.f, 33.f, 51.f, 81.f, 66.f, 39.f, 34.f, 17.f,
77 33.f, 36.f, 48.f, 47.f, 28.f, 23.f, 12.f, 12.f,
78 51.f, 48.f, 47.f, 28.f, 23.f, 12.f, 12.f, 12.f,
79 81.f, 47.f, 28.f, 23.f, 12.f, 12.f, 12.f, 12.f,
80 66.f, 28.f, 23.f, 12.f, 12.f, 12.f, 12.f, 12.f,
81 39.f, 23.f, 12.f, 12.f, 12.f, 12.f, 12.f, 12.f,
82 34.f, 12.f, 12.f, 12.f, 12.f, 12.f, 12.f, 12.f,
83 17.f, 12.f, 12.f, 12.f, 12.f, 12.f, 12.f, 12.f
84 };
85
86 /**
87 **************************************************************************
88 * Performs multiplication of two 8x8 matrices
89 *
90 * \param M1 [IN] - Pointer to the first matrix
91 * \param M1Stride [IN] - Stride of the first matrix
92 * \param M2 [IN] - Pointer to the second matrix
93 * \param M2Stride [IN] - Stride of the second matrix
94 * \param Mres [OUT] - Pointer to the result matrix
95 * \param MresStride [IN] - Stride of the result matrix
96 *
97 * \return None
98 */
99 void mult8x8(const float *M1, int M1Stride, const float *M2, int M2Stride,
100 float *Mres, int MresStride) {
101 for (int i = 0; i < BLOCK_SIZE; i++) {
102 for (int j = 0; j < BLOCK_SIZE; j++) {
103 float accumul = 0;
104
105 for (int k = 0; k < BLOCK_SIZE; k++) {
106 accumul += M1[i * M1Stride + k] * M2[k * M2Stride + j];
107 }
108
109 Mres[i * MresStride + j] = accumul;
110 }
111 }
112 }
113
114 /**
115 **************************************************************************
116 * Performs 8x8 block-wise Forward Discrete Cosine Transform of the given
117 * image plane and outputs result to the plane of coefficients.
118 * 1st version.
119 *
120 * \param fSrc [IN] - Source image plane
121 * \param fDst [OUT] - Destination coefficients plane
122 * \param Stride [IN] - Stride of both planes
123 * \param Size [IN] - Size of planes
124 *
125 * \return None
126 */
127 extern "C" void computeDCT8x8Gold1(const float *fSrc, float *fDst, int Stride,
128 ROI Size) {
129 float tmpblock[BLOCK_SIZE2];
130
131 // perform block wise DCT
132 // DCT(A) = DCTv8matrixT * A * DCTv8matrix
133 for (int i = 0; i + BLOCK_SIZE - 1 < Size.height; i += BLOCK_SIZE) {
134 for (int j = 0; j + BLOCK_SIZE - 1 < Size.width; j += BLOCK_SIZE) {
135 // tmpblock = DCTv8matrixT * A
136 mult8x8(DCTv8matrixT, BLOCK_SIZE, fSrc + i * Stride + j, Stride, tmpblock,
137 BLOCK_SIZE);
138 // DCT(A) = tmpblock * DCTv8matrix
139 mult8x8(tmpblock, BLOCK_SIZE, DCTv8matrix, BLOCK_SIZE,
140 fDst + i * Stride + j, Stride);
141 }
142 }
143 }
144
145 /**
146 **************************************************************************
147 * Performs 8x8 block-wise Inverse Discrete Cosine Transform of the given
148 * coefficients plane and outputs result to the image plane.
149 * 1st version.
150 *
151 * \param fSrc [IN] - Source coefficients plane
152 * \param fDst [OUT] - Destination image plane
153 * \param Stride [IN] - Stride of both planes
154 * \param Size [IN] - Size of planes
155 *
156 * \return None
157 */
158 extern "C" void computeIDCT8x8Gold1(const float *fSrc, float *fDst, int Stride,
159 ROI Size) {
160 float tmpblock[BLOCK_SIZE2];
161
162 // perform block wise IDCT
163 // IDCT(A) = DCTv8matrix * A * DCTv8matrixT
164 for (int i = 0; i + BLOCK_SIZE - 1 < Size.height; i += BLOCK_SIZE) {
165 for (int j = 0; j + BLOCK_SIZE - 1 < Size.width; j += BLOCK_SIZE) {
166 // tmpblock = DCTv8matrix * A
167 mult8x8(DCTv8matrix, BLOCK_SIZE, fSrc + i * Stride + j, Stride, tmpblock,
168 BLOCK_SIZE);
169 // DCT(A) = tmpblock * DCTv8matrixT;
170 mult8x8(tmpblock, BLOCK_SIZE, DCTv8matrixT, BLOCK_SIZE,
171 fDst + i * Stride + j, Stride);
172 }
173 }
174 }
175
176 /**
177 **************************************************************************
178 * Performs in-place quantization of given coefficients plane using
179 * predefined quantization matrices (float elements)
180 *
181 * \param fSrcDst [IN/OUT] - Coefficients plane
182 * \param Stride [IN] - Stride of SrcDst
183 * \param Size [IN] - Size of the plane
184 *
185 * \return None
186 */
187 extern "C" void quantizeGoldFloat(float *fSrcDst, int Stride, ROI Size) {
188 // perform block wise in-place quantization using Q
189 // Q(A) = round(A ./ Q) .* Q;
190 for (int i = 0; i < Size.height; i++) {
191 for (int j = 0; j < Size.width; j++) {
192 int qx = j % BLOCK_SIZE;
193 int qy = i % BLOCK_SIZE;
194 float quantized =
195 round_f(fSrcDst[i * Stride + j] / Q[(qy << BLOCK_SIZE_LOG2) + qx]);
196 fSrcDst[i * Stride + j] = quantized * Q[(qy << BLOCK_SIZE_LOG2) + qx];
197 }
198 }
199 }
200
201 /**
202 **************************************************************************
203 * Performs in-place quantization of given coefficients plane using
204 * predefined quantization matrices (short elements)
205 *
206 * \param fSrcDst [IN/OUT] - Coefficients plane
207 * \param Stride [IN] - Stride of SrcDst
208 * \param Size [IN] - Size of the plane
209 *
210 * \return None
211 */
212 void quantizeGoldShort(short *fSrcDst, int Stride, ROI Size) {
213 // perform block wise in-place quantization using Q
214 // Q(A) = round(A ./ Q) .* Q;
215 for (int i = 0; i < Size.height; i++) {
216 for (int j = 0; j < Size.width; j++) {
217 int qx = j % BLOCK_SIZE;
218 int qy = i % BLOCK_SIZE;
219 short temp = fSrcDst[i * Stride + j];
220 short quant = (short)(Q[(qy << BLOCK_SIZE_LOG2) + qx]);
221
222 if (temp < 0) {
223 temp = -temp;
224 temp += quant >> 1;
225 temp /= quant;
226 temp = -temp;
227 } else {
228 temp += quant >> 1;
229 temp /= quant;
230 }
231
232 fSrcDst[i * Stride + j] = temp * quant;
233 }
234 }
235 }
236
237 // Used in forward and inverse DCT.
238 float C_a = 1.387039845322148f; //!< a = (2^0.5) * cos( pi / 16);
239 float C_b = 1.306562964876377f; //!< b = (2^0.5) * cos( pi / 8);
240 float C_c = 1.175875602419359f; //!< c = (2^0.5) * cos(3 * pi / 16);
241 float C_d = 0.785694958387102f; //!< d = (2^0.5) * cos(5 * pi / 16);
242 float C_e = 0.541196100146197f; //!< e = (2^0.5) * cos(3 * pi / 8);
243 float C_f = 0.275899379282943f; //!< f = (2^0.5) * cos(7 * pi / 16);
244
245 /**
246 * Normalization constant that is used in forward and inverse DCT
247 */
248 float C_norm = 0.3535533905932737f; // 1 / (8^0.5)
249
250 /**
251 **************************************************************************
252 * Performs DCT of vector of 8 elements.
253 *
254 * \param FirstIn [IN] - Pointer to the first element of input vector
255 * \param StepIn [IN] - Value to add to ptr to access other input
256 *elements
257 * \param FirstOut [OUT] - Pointer to the first element of output vector
258 * \param StepOut [IN] - Value to add to ptr to access other output
259 *elements
260 *
261 * \return None
262 */
263 void SubroutineDCTvector(float *FirstIn, int StepIn, float *FirstOut,
264 int StepOut) {
265 float X07P = FirstIn[0 * StepIn] + FirstIn[7 * StepIn];
266 float X16P = FirstIn[1 * StepIn] + FirstIn[6 * StepIn];
267 float X25P = FirstIn[2 * StepIn] + FirstIn[5 * StepIn];
268 float X34P = FirstIn[3 * StepIn] + FirstIn[4 * StepIn];
269
270 float X07M = FirstIn[0 * StepIn] - FirstIn[7 * StepIn];
271 float X61M = FirstIn[6 * StepIn] - FirstIn[1 * StepIn];
272 float X25M = FirstIn[2 * StepIn] - FirstIn[5 * StepIn];
273 float X43M = FirstIn[4 * StepIn] - FirstIn[3 * StepIn];
274
275 float X07P34PP = X07P + X34P;
276 float X07P34PM = X07P - X34P;
277 float X16P25PP = X16P + X25P;
278 float X16P25PM = X16P - X25P;
279
280 FirstOut[0 * StepOut] = C_norm * (X07P34PP + X16P25PP);
281 FirstOut[2 * StepOut] = C_norm * (C_b * X07P34PM + C_e * X16P25PM);
282 FirstOut[4 * StepOut] = C_norm * (X07P34PP - X16P25PP);
283 FirstOut[6 * StepOut] = C_norm * (C_e * X07P34PM - C_b * X16P25PM);
284
285 FirstOut[1 * StepOut] =
286 C_norm * (C_a * X07M - C_c * X61M + C_d * X25M - C_f * X43M);
287 FirstOut[3 * StepOut] =
288 C_norm * (C_c * X07M + C_f * X61M - C_a * X25M + C_d * X43M);
289 FirstOut[5 * StepOut] =
290 C_norm * (C_d * X07M + C_a * X61M + C_f * X25M - C_c * X43M);
291 FirstOut[7 * StepOut] =
292 C_norm * (C_f * X07M + C_d * X61M + C_c * X25M + C_a * X43M);
293 }
294
295 /**
296 **************************************************************************
297 * Performs IDCT of vector of 8 elements.
298 *
299 * \param FirstIn [IN] - Pointer to the first element of input vector
300 * \param StepIn [IN] - Value to add to ptr to access other input
301 *elements
302 * \param FirstOut [OUT] - Pointer to the first element of output vector
303 * \param StepOut [IN] - Value to add to ptr to access other output
304 *elements
305 *
306 * \return None
307 */
308 void SubroutineIDCTvector(float *FirstIn, int StepIn, float *FirstOut,
309 int StepOut) {
310 float Y04P = FirstIn[0 * StepIn] + FirstIn[4 * StepIn];
311 float Y2b6eP = C_b * FirstIn[2 * StepIn] + C_e * FirstIn[6 * StepIn];
312
313 float Y04P2b6ePP = Y04P + Y2b6eP;
314 float Y04P2b6ePM = Y04P - Y2b6eP;
315 float Y7f1aP3c5dPP = C_f * FirstIn[7 * StepIn] + C_a * FirstIn[1 * StepIn] +
316 C_c * FirstIn[3 * StepIn] + C_d * FirstIn[5 * StepIn];
317 float Y7a1fM3d5cMP = C_a * FirstIn[7 * StepIn] - C_f * FirstIn[1 * StepIn] +
318 C_d * FirstIn[3 * StepIn] - C_c * FirstIn[5 * StepIn];
319
320 float Y04M = FirstIn[0 * StepIn] - FirstIn[4 * StepIn];
321 float Y2e6bM = C_e * FirstIn[2 * StepIn] - C_b * FirstIn[6 * StepIn];
322
323 float Y04M2e6bMP = Y04M + Y2e6bM;
324 float Y04M2e6bMM = Y04M - Y2e6bM;
325 float Y1c7dM3f5aPM = C_c * FirstIn[1 * StepIn] - C_d * FirstIn[7 * StepIn] -
326 C_f * FirstIn[3 * StepIn] - C_a * FirstIn[5 * StepIn];
327 float Y1d7cP3a5fMM = C_d * FirstIn[1 * StepIn] + C_c * FirstIn[7 * StepIn] -
328 C_a * FirstIn[3 * StepIn] + C_f * FirstIn[5 * StepIn];
329
330 FirstOut[0 * StepOut] = C_norm * (Y04P2b6ePP + Y7f1aP3c5dPP);
331 FirstOut[7 * StepOut] = C_norm * (Y04P2b6ePP - Y7f1aP3c5dPP);
332 FirstOut[4 * StepOut] = C_norm * (Y04P2b6ePM + Y7a1fM3d5cMP);
333 FirstOut[3 * StepOut] = C_norm * (Y04P2b6ePM - Y7a1fM3d5cMP);
334
335 FirstOut[1 * StepOut] = C_norm * (Y04M2e6bMP + Y1c7dM3f5aPM);
336 FirstOut[5 * StepOut] = C_norm * (Y04M2e6bMM - Y1d7cP3a5fMM);
337 FirstOut[2 * StepOut] = C_norm * (Y04M2e6bMM + Y1d7cP3a5fMM);
338 FirstOut[6 * StepOut] = C_norm * (Y04M2e6bMP - Y1c7dM3f5aPM);
339 }
340
341 /**
342 **************************************************************************
343 * Performs 8x8 block-wise Forward Discrete Cosine Transform of the given
344 * image plane and outputs result to the plane of coefficients.
345 * 2nd version.
346 *
347 * \param fSrc [IN] - Source image plane
348 * \param fDst [OUT] - Destination coefficients plane
349 * \param Stride [IN] - Stride of both planes
350 * \param Size [IN] - Size of planes
351 *
352 * \return None
353 */
354 extern "C" void computeDCT8x8Gold2(const float *fSrc, float *fDst, int Stride,
355 ROI Size) {
356 for (int i = 0; i + BLOCK_SIZE - 1 < Size.height; i += BLOCK_SIZE) {
357 for (int j = 0; j + BLOCK_SIZE - 1 < Size.width; j += BLOCK_SIZE) {
358 // process rows
359 for (int k = 0; k < BLOCK_SIZE; k++) {
360 SubroutineDCTvector((float *)fSrc + (i + k) * Stride + j, 1,
361 fDst + (i + k) * Stride + j, 1);
362 }
363
364 // process columns
365 for (int k = 0; k < BLOCK_SIZE; k++) {
366 SubroutineDCTvector(fDst + i * Stride + (j + k), Stride,
367 fDst + i * Stride + (j + k), Stride);
368 }
369 }
370 }
371 }
372
373 /**
374 **************************************************************************
375 * Performs 8x8 block-wise Inverse Discrete Cosine Transform of the given
376 * coefficients plane and outputs result to the image plane
377 * 2nd version.
378 *
379 * \param fSrc [IN] - Source coefficients plane
380 * \param fDst [OUT] - Destination image plane
381 * \param Stride [IN] - Stride of both planes
382 * \param Size [IN] - Size of planes
383 *
384 * \return None
385 */
386 extern "C" void computeIDCT8x8Gold2(const float *fSrc, float *fDst, int Stride,
387 ROI Size) {
388 for (int i = 0; i + BLOCK_SIZE - 1 < Size.height; i += BLOCK_SIZE) {
389 for (int j = 0; j + BLOCK_SIZE - 1 < Size.width; j += BLOCK_SIZE) {
390 // process rows
391 for (int k = 0; k < BLOCK_SIZE; k++) {
392 SubroutineIDCTvector((float *)fSrc + (i + k) * Stride + j, 1,
393 fDst + (i + k) * Stride + j, 1);
394 }
395
396 // process columns
397 for (int k = 0; k < BLOCK_SIZE; k++) {
398 SubroutineIDCTvector(fDst + i * Stride + (j + k), Stride,
399 fDst + i * Stride + (j + k), Stride);
400 }
401 }
402 }
403 }