1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_dct4_q15.c
4 * Description: Processing function of DCT4 & IDCT4 Q15
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29 #include "dsp/transform_functions.h"
30
31 /**
32 @addtogroup DCT4Q15
33 @{
34 */
35
36 /**
37 @brief Processing function for the Q15 DCT4/IDCT4.
38 @deprecated Do not use this function. It will be removed in future versions.
39 @param[in] S points to an instance of the Q15 DCT4 structure.
40 @param[in] pState points to state buffer.
41 @param[in,out] pInlineBuffer points to the in-place input and output buffer.
42
43 @par Input an output formats
44 Internally inputs are downscaled in the RFFT process function to avoid overflows.
45 Number of bits downscaled, depends on the size of the transform. The input and output
46 formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
47
48 | DCT Size | Input format | Output format | Number of bits to upscale |
49 | --------: | ------------: | ------------: | ------------------------: |
50 | 2048 | 1.15 | 11.5 | 10 |
51 | 512 | 1.15 | 9.7 | 8 |
52 | 128 | 1.15 | 7.9 | 6 |
53
54
55 */
56
arm_dct4_q15(const arm_dct4_instance_q15 * S,q15_t * pState,q15_t * pInlineBuffer)57 void arm_dct4_q15(
58 const arm_dct4_instance_q15 * S,
59 q15_t * pState,
60 q15_t * pInlineBuffer)
61 {
62 const q15_t *weights = S->pTwiddle; /* Pointer to the Weights table */
63 const q15_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
64 q15_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
65 q15_t in; /* Temporary variable */
66 uint32_t i; /* Loop counter */
67
68
69 /* DCT4 computation involves DCT2 (which is calculated using RFFT)
70 * along with some pre-processing and post-processing.
71 * Computational procedure is explained as follows:
72 * (a) Pre-processing involves multiplying input with cos factor,
73 * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
74 * where,
75 * r(n) -- output of preprocessing
76 * u(n) -- input to preprocessing(actual Source buffer)
77 * (b) Calculation of DCT2 using FFT is divided into three steps:
78 * Step1: Re-ordering of even and odd elements of input.
79 * Step2: Calculating FFT of the re-ordered input.
80 * Step3: Taking the real part of the product of FFT output and weights.
81 * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
82 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
83 * where,
84 * Y4 -- DCT4 output, Y2 -- DCT2 output
85 * (d) Multiplying the output with the normalizing factor sqrt(2/N).
86 */
87
88 /*-------- Pre-processing ------------*/
89 /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
90 arm_mult_q15 (pInlineBuffer, cosFact, pInlineBuffer, S->N);
91 arm_shift_q15 (pInlineBuffer, 1, pInlineBuffer, S->N);
92
93 /* ----------------------------------------------------------------
94 * Step1: Re-ordering of even and odd elements as
95 * pState[i] = pInlineBuffer[2*i] and
96 * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
97 ---------------------------------------------------------------------*/
98
99 /* pS1 initialized to pState */
100 pS1 = pState;
101
102 /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
103 pS2 = pState + (S->N - 1U);
104
105 /* pbuff initialized to input buffer */
106 pbuff = pInlineBuffer;
107
108
109 #if defined (ARM_MATH_LOOPUNROLL)
110
111 /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
112 i = S->Nby2 >> 2U;
113
114 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
115 ** a second loop below computes the remaining 1 to 3 samples. */
116 do
117 {
118 /* Re-ordering of even and odd elements */
119 /* pState[i] = pInlineBuffer[2*i] */
120 *pS1++ = *pbuff++;
121 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
122 *pS2-- = *pbuff++;
123
124 *pS1++ = *pbuff++;
125 *pS2-- = *pbuff++;
126
127 *pS1++ = *pbuff++;
128 *pS2-- = *pbuff++;
129
130 *pS1++ = *pbuff++;
131 *pS2-- = *pbuff++;
132
133 /* Decrement loop counter */
134 i--;
135 } while (i > 0U);
136
137 /* pbuff initialized to input buffer */
138 pbuff = pInlineBuffer;
139
140 /* pS1 initialized to pState */
141 pS1 = pState;
142
143 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
144 i = S->N >> 2U;
145
146 /* Processing with loop unrolling 4 times as N is always multiple of 4.
147 * Compute 4 outputs at a time */
148 do
149 {
150 /* Writing the re-ordered output back to inplace input buffer */
151 *pbuff++ = *pS1++;
152 *pbuff++ = *pS1++;
153 *pbuff++ = *pS1++;
154 *pbuff++ = *pS1++;
155
156 /* Decrement the loop counter */
157 i--;
158 } while (i > 0U);
159
160
161 /* ---------------------------------------------------------
162 * Step2: Calculate RFFT for N-point input
163 * ---------------------------------------------------------- */
164 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
165 arm_rfft_q15 (S->pRfft, pInlineBuffer, pState);
166
167 /*----------------------------------------------------------------------
168 * Step3: Multiply the FFT output with the weights.
169 *----------------------------------------------------------------------*/
170 arm_cmplx_mult_cmplx_q15 (pState, weights, pState, S->N);
171
172 /* The output of complex multiplication is in 3.13 format.
173 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
174 arm_shift_q15 (pState, 2, pState, S->N * 2);
175
176 /* ----------- Post-processing ---------- */
177 /* DCT-IV can be obtained from DCT-II by the equation,
178 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
179 * Hence, Y4(0) = Y2(0)/2 */
180 /* Getting only real part from the output and Converting to DCT-IV */
181
182 /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
183 i = (S->N - 1U) >> 2U;
184
185 /* pbuff initialized to input buffer. */
186 pbuff = pInlineBuffer;
187
188 /* pS1 initialized to pState */
189 pS1 = pState;
190
191 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
192 in = *pS1++ >> 1U;
193 /* input buffer acts as inplace, so output values are stored in the input itself. */
194 *pbuff++ = in;
195
196 /* pState pointer is incremented twice as the real values are located alternatively in the array */
197 pS1++;
198
199 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
200 ** a second loop below computes the remaining 1 to 3 samples. */
201 do
202 {
203 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
204 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
205 in = *pS1++ - in;
206 *pbuff++ = in;
207 /* points to the next real value */
208 pS1++;
209
210 in = *pS1++ - in;
211 *pbuff++ = in;
212 pS1++;
213
214 in = *pS1++ - in;
215 *pbuff++ = in;
216 pS1++;
217
218 in = *pS1++ - in;
219 *pbuff++ = in;
220 pS1++;
221
222 /* Decrement the loop counter */
223 i--;
224 } while (i > 0U);
225
226 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
227 ** No loop unrolling is used. */
228 i = (S->N - 1U) % 0x4U;
229
230 while (i > 0U)
231 {
232 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
233 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
234 in = *pS1++ - in;
235 *pbuff++ = in;
236
237 /* points to the next real value */
238 pS1++;
239
240 /* Decrement loop counter */
241 i--;
242 }
243
244
245 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
246
247 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
248 i = S->N >> 2U;
249
250 /* pbuff initialized to the pInlineBuffer(now contains the output values) */
251 pbuff = pInlineBuffer;
252
253 /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
254 do
255 {
256 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
257 in = *pbuff;
258 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
259
260 in = *pbuff;
261 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
262
263 in = *pbuff;
264 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
265
266 in = *pbuff;
267 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
268
269 /* Decrement loop counter */
270 i--;
271 } while (i > 0U);
272
273
274 #else
275
276 /* Initializing the loop counter to N/2 */
277 i = S->Nby2;
278
279 do
280 {
281 /* Re-ordering of even and odd elements */
282 /* pState[i] = pInlineBuffer[2*i] */
283 *pS1++ = *pbuff++;
284 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
285 *pS2-- = *pbuff++;
286
287 /* Decrement the loop counter */
288 i--;
289 } while (i > 0U);
290
291 /* pbuff initialized to input buffer */
292 pbuff = pInlineBuffer;
293
294 /* pS1 initialized to pState */
295 pS1 = pState;
296
297 /* Initializing the loop counter */
298 i = S->N;
299
300 do
301 {
302 /* Writing the re-ordered output back to inplace input buffer */
303 *pbuff++ = *pS1++;
304
305 /* Decrement the loop counter */
306 i--;
307 } while (i > 0U);
308
309
310 /* ---------------------------------------------------------
311 * Step2: Calculate RFFT for N-point input
312 * ---------------------------------------------------------- */
313 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
314 arm_rfft_q15 (S->pRfft, pInlineBuffer, pState);
315
316 /*----------------------------------------------------------------------
317 * Step3: Multiply the FFT output with the weights.
318 *----------------------------------------------------------------------*/
319 arm_cmplx_mult_cmplx_q15 (pState, weights, pState, S->N);
320
321 /* The output of complex multiplication is in 3.13 format.
322 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
323 arm_shift_q15 (pState, 2, pState, S->N * 2);
324
325 /* ----------- Post-processing ---------- */
326 /* DCT-IV can be obtained from DCT-II by the equation,
327 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
328 * Hence, Y4(0) = Y2(0)/2 */
329 /* Getting only real part from the output and Converting to DCT-IV */
330
331 /* pbuff initialized to input buffer. */
332 pbuff = pInlineBuffer;
333
334 /* pS1 initialized to pState */
335 pS1 = pState;
336
337 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
338 in = *pS1++ >> 1U;
339 /* input buffer acts as inplace, so output values are stored in the input itself. */
340 *pbuff++ = in;
341
342 /* pState pointer is incremented twice as the real values are located alternatively in the array */
343 pS1++;
344
345 /* Initializing the loop counter */
346 i = (S->N - 1U);
347
348 do
349 {
350 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
351 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
352 in = *pS1++ - in;
353 *pbuff++ = in;
354
355 /* points to the next real value */
356 pS1++;
357
358 /* Decrement loop counter */
359 i--;
360 } while (i > 0U);
361
362 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
363
364 /* Initializing loop counter */
365 i = S->N;
366
367 /* pbuff initialized to the pInlineBuffer (now contains the output values) */
368 pbuff = pInlineBuffer;
369
370 do
371 {
372 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
373 in = *pbuff;
374 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
375
376 /* Decrement loop counter */
377 i--;
378
379 } while (i > 0U);
380
381 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
382
383 }
384
385 /**
386 @} end of DCT4Q15 group
387 */
388