1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_dct4_q15.c
4 * Description: Processing function of DCT4 & IDCT4 Q15
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
32 * @addtogroup DCT4_IDCT4
37 * @brief Processing function for the Q15 DCT4/IDCT4.
38 * @param[in] *S points to an instance of the Q15 DCT4 structure.
39 * @param[in] *pState points to state buffer.
40 * @param[in,out] *pInlineBuffer points to the in-place input and output buffer.
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.
46 * The input and output formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
48 * \image html dct4FormatsQ15Table.gif
52 const arm_dct4_instance_q15
* S
,
54 q15_t
* pInlineBuffer
)
56 uint32_t i
; /* Loop counter */
57 q15_t
*weights
= S
->pTwiddle
; /* Pointer to the Weights table */
58 q15_t
*cosFact
= S
->pCosFactor
; /* Pointer to the cos factors table */
59 q15_t
*pS1
, *pS2
, *pbuff
; /* Temporary pointers for input buffer and pState buffer */
60 q15_t in
; /* Temporary variable */
63 /* DCT4 computation involves DCT2 (which is calculated using RFFT)
64 * along with some pre-processing and post-processing.
65 * Computational procedure is explained as follows:
66 * (a) Pre-processing involves multiplying input with cos factor,
67 * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
69 * r(n) -- output of preprocessing
70 * u(n) -- input to preprocessing(actual Source buffer)
71 * (b) Calculation of DCT2 using FFT is divided into three steps:
72 * Step1: Re-ordering of even and odd elements of input.
73 * Step2: Calculating FFT of the re-ordered input.
74 * Step3: Taking the real part of the product of FFT output and weights.
75 * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
76 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
78 * Y4 -- DCT4 output, Y2 -- DCT2 output
79 * (d) Multiplying the output with the normalizing factor sqrt(2/N).
82 /*-------- Pre-processing ------------*/
83 /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
84 arm_mult_q15(pInlineBuffer
, cosFact
, pInlineBuffer
, S
->N
);
85 arm_shift_q15(pInlineBuffer
, 1, pInlineBuffer
, S
->N
);
87 /* ----------------------------------------------------------------
88 * Step1: Re-ordering of even and odd elements as
89 * pState[i] = pInlineBuffer[2*i] and
90 * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
91 ---------------------------------------------------------------------*/
93 /* pS1 initialized to pState */
96 /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
97 pS2
= pState
+ (S
->N
- 1U);
99 /* pbuff initialized to input buffer */
100 pbuff
= pInlineBuffer
;
103 #if defined (ARM_MATH_DSP)
105 /* Run the below code for Cortex-M4 and Cortex-M3 */
107 /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
108 i
= (uint32_t) S
->Nby2
>> 2U;
110 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
111 ** a second loop below computes the remaining 1 to 3 samples. */
114 /* Re-ordering of even and odd elements */
115 /* pState[i] = pInlineBuffer[2*i] */
117 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
129 /* Decrement the loop counter */
133 /* pbuff initialized to input buffer */
134 pbuff
= pInlineBuffer
;
136 /* pS1 initialized to pState */
139 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
140 i
= (uint32_t) S
->N
>> 2U;
142 /* Processing with loop unrolling 4 times as N is always multiple of 4.
143 * Compute 4 outputs at a time */
146 /* Writing the re-ordered output back to inplace input buffer */
152 /* Decrement the loop counter */
157 /* ---------------------------------------------------------
158 * Step2: Calculate RFFT for N-point input
159 * ---------------------------------------------------------- */
160 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
161 arm_rfft_q15(S
->pRfft
, pInlineBuffer
, pState
);
163 /*----------------------------------------------------------------------
164 * Step3: Multiply the FFT output with the weights.
165 *----------------------------------------------------------------------*/
166 arm_cmplx_mult_cmplx_q15(pState
, weights
, pState
, S
->N
);
168 /* The output of complex multiplication is in 3.13 format.
169 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
170 arm_shift_q15(pState
, 2, pState
, S
->N
* 2);
172 /* ----------- Post-processing ---------- */
173 /* DCT-IV can be obtained from DCT-II by the equation,
174 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
175 * Hence, Y4(0) = Y2(0)/2 */
176 /* Getting only real part from the output and Converting to DCT-IV */
178 /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
179 i
= ((uint32_t) S
->N
- 1U) >> 2U;
181 /* pbuff initialized to input buffer. */
182 pbuff
= pInlineBuffer
;
184 /* pS1 initialized to pState */
187 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
189 /* input buffer acts as inplace, so output values are stored in the input itself. */
192 /* pState pointer is incremented twice as the real values are located alternatively in the array */
195 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
196 ** a second loop below computes the remaining 1 to 3 samples. */
199 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
200 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
203 /* points to the next real value */
218 /* Decrement the loop counter */
222 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
223 ** No loop unrolling is used. */
224 i
= ((uint32_t) S
->N
- 1U) % 0x4U
;
228 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
229 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
232 /* points to the next real value */
235 /* Decrement the loop counter */
240 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
242 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
243 i
= (uint32_t) S
->N
>> 2U;
245 /* pbuff initialized to the pInlineBuffer(now contains the output values) */
246 pbuff
= pInlineBuffer
;
248 /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
251 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
253 *pbuff
++ = ((q15_t
) (((q31_t
) in
* S
->normalize
) >> 15));
256 *pbuff
++ = ((q15_t
) (((q31_t
) in
* S
->normalize
) >> 15));
259 *pbuff
++ = ((q15_t
) (((q31_t
) in
* S
->normalize
) >> 15));
262 *pbuff
++ = ((q15_t
) (((q31_t
) in
* S
->normalize
) >> 15));
264 /* Decrement the loop counter */
271 /* Run the below code for Cortex-M0 */
273 /* Initializing the loop counter to N/2 */
274 i
= (uint32_t) S
->Nby2
;
278 /* Re-ordering of even and odd elements */
279 /* pState[i] = pInlineBuffer[2*i] */
281 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
284 /* Decrement the loop counter */
288 /* pbuff initialized to input buffer */
289 pbuff
= pInlineBuffer
;
291 /* pS1 initialized to pState */
294 /* Initializing the loop counter */
299 /* Writing the re-ordered output back to inplace input buffer */
302 /* Decrement the loop counter */
307 /* ---------------------------------------------------------
308 * Step2: Calculate RFFT for N-point input
309 * ---------------------------------------------------------- */
310 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
311 arm_rfft_q15(S
->pRfft
, pInlineBuffer
, pState
);
313 /*----------------------------------------------------------------------
314 * Step3: Multiply the FFT output with the weights.
315 *----------------------------------------------------------------------*/
316 arm_cmplx_mult_cmplx_q15(pState
, weights
, pState
, S
->N
);
318 /* The output of complex multiplication is in 3.13 format.
319 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
320 arm_shift_q15(pState
, 2, pState
, S
->N
* 2);
322 /* ----------- Post-processing ---------- */
323 /* DCT-IV can be obtained from DCT-II by the equation,
324 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
325 * Hence, Y4(0) = Y2(0)/2 */
326 /* Getting only real part from the output and Converting to DCT-IV */
328 /* Initializing the loop counter */
329 i
= ((uint32_t) S
->N
- 1U);
331 /* pbuff initialized to input buffer. */
332 pbuff
= pInlineBuffer
;
334 /* pS1 initialized to pState */
337 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
339 /* input buffer acts as inplace, so output values are stored in the input itself. */
342 /* pState pointer is incremented twice as the real values are located alternatively in the array */
347 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
348 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
351 /* points to the next real value */
354 /* Decrement the loop counter */
358 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
360 /* Initializing the loop counter */
363 /* pbuff initialized to the pInlineBuffer(now contains the output values) */
364 pbuff
= pInlineBuffer
;
368 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
370 *pbuff
++ = ((q15_t
) (((q31_t
) in
* S
->normalize
) >> 15));
372 /* Decrement the loop counter */
376 #endif /* #if defined (ARM_MATH_DSP) */
381 * @} end of DCT4_IDCT4 group