arm_dct4_q31.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_dct4_q31.c
  4. * Description: Processing function of DCT4 & IDCT4 Q31
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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. #include "arm_math.h"
  29. /**
  30. * @addtogroup DCT4_IDCT4
  31. * @{
  32. */
  33. /**
  34. * @brief Processing function for the Q31 DCT4/IDCT4.
  35. * @param[in] *S points to an instance of the Q31 DCT4 structure.
  36. * @param[in] *pState points to state buffer.
  37. * @param[in,out] *pInlineBuffer points to the in-place input and output buffer.
  38. * @return none.
  39. * \par Input an output formats:
  40. * Input samples need to be downscaled by 1 bit to avoid saturations in the Q31 DCT process,
  41. * as the conversion from DCT2 to DCT4 involves one subtraction.
  42. * Internally inputs are downscaled in the RFFT process function to avoid overflows.
  43. * Number of bits downscaled, depends on the size of the transform.
  44. * The input and output formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
  45. *
  46. * \image html dct4FormatsQ31Table.gif
  47. */
  48. void arm_dct4_q31(
  49. const arm_dct4_instance_q31 * S,
  50. q31_t * pState,
  51. q31_t * pInlineBuffer)
  52. {
  53. uint16_t i; /* Loop counter */
  54. q31_t *weights = S->pTwiddle; /* Pointer to the Weights table */
  55. q31_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
  56. q31_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
  57. q31_t in; /* Temporary variable */
  58. /* DCT4 computation involves DCT2 (which is calculated using RFFT)
  59. * along with some pre-processing and post-processing.
  60. * Computational procedure is explained as follows:
  61. * (a) Pre-processing involves multiplying input with cos factor,
  62. * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
  63. * where,
  64. * r(n) -- output of preprocessing
  65. * u(n) -- input to preprocessing(actual Source buffer)
  66. * (b) Calculation of DCT2 using FFT is divided into three steps:
  67. * Step1: Re-ordering of even and odd elements of input.
  68. * Step2: Calculating FFT of the re-ordered input.
  69. * Step3: Taking the real part of the product of FFT output and weights.
  70. * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
  71. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  72. * where,
  73. * Y4 -- DCT4 output, Y2 -- DCT2 output
  74. * (d) Multiplying the output with the normalizing factor sqrt(2/N).
  75. */
  76. /*-------- Pre-processing ------------*/
  77. /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
  78. arm_mult_q31(pInlineBuffer, cosFact, pInlineBuffer, S->N);
  79. arm_shift_q31(pInlineBuffer, 1, pInlineBuffer, S->N);
  80. /* ----------------------------------------------------------------
  81. * Step1: Re-ordering of even and odd elements as
  82. * pState[i] = pInlineBuffer[2*i] and
  83. * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
  84. ---------------------------------------------------------------------*/
  85. /* pS1 initialized to pState */
  86. pS1 = pState;
  87. /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
  88. pS2 = pState + (S->N - 1U);
  89. /* pbuff initialized to input buffer */
  90. pbuff = pInlineBuffer;
  91. #if defined (ARM_MATH_DSP)
  92. /* Run the below code for Cortex-M4 and Cortex-M3 */
  93. /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
  94. i = S->Nby2 >> 2U;
  95. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  96. ** a second loop below computes the remaining 1 to 3 samples. */
  97. do
  98. {
  99. /* Re-ordering of even and odd elements */
  100. /* pState[i] = pInlineBuffer[2*i] */
  101. *pS1++ = *pbuff++;
  102. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  103. *pS2-- = *pbuff++;
  104. *pS1++ = *pbuff++;
  105. *pS2-- = *pbuff++;
  106. *pS1++ = *pbuff++;
  107. *pS2-- = *pbuff++;
  108. *pS1++ = *pbuff++;
  109. *pS2-- = *pbuff++;
  110. /* Decrement the loop counter */
  111. i--;
  112. } while (i > 0U);
  113. /* pbuff initialized to input buffer */
  114. pbuff = pInlineBuffer;
  115. /* pS1 initialized to pState */
  116. pS1 = pState;
  117. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  118. i = S->N >> 2U;
  119. /* Processing with loop unrolling 4 times as N is always multiple of 4.
  120. * Compute 4 outputs at a time */
  121. do
  122. {
  123. /* Writing the re-ordered output back to inplace input buffer */
  124. *pbuff++ = *pS1++;
  125. *pbuff++ = *pS1++;
  126. *pbuff++ = *pS1++;
  127. *pbuff++ = *pS1++;
  128. /* Decrement the loop counter */
  129. i--;
  130. } while (i > 0U);
  131. /* ---------------------------------------------------------
  132. * Step2: Calculate RFFT for N-point input
  133. * ---------------------------------------------------------- */
  134. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  135. arm_rfft_q31(S->pRfft, pInlineBuffer, pState);
  136. /*----------------------------------------------------------------------
  137. * Step3: Multiply the FFT output with the weights.
  138. *----------------------------------------------------------------------*/
  139. arm_cmplx_mult_cmplx_q31(pState, weights, pState, S->N);
  140. /* The output of complex multiplication is in 3.29 format.
  141. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.31 format by shifting left by 2 bits. */
  142. arm_shift_q31(pState, 2, pState, S->N * 2);
  143. /* ----------- Post-processing ---------- */
  144. /* DCT-IV can be obtained from DCT-II by the equation,
  145. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  146. * Hence, Y4(0) = Y2(0)/2 */
  147. /* Getting only real part from the output and Converting to DCT-IV */
  148. /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
  149. i = (S->N - 1U) >> 2U;
  150. /* pbuff initialized to input buffer. */
  151. pbuff = pInlineBuffer;
  152. /* pS1 initialized to pState */
  153. pS1 = pState;
  154. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  155. in = *pS1++ >> 1U;
  156. /* input buffer acts as inplace, so output values are stored in the input itself. */
  157. *pbuff++ = in;
  158. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  159. pS1++;
  160. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  161. ** a second loop below computes the remaining 1 to 3 samples. */
  162. do
  163. {
  164. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  165. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  166. in = *pS1++ - in;
  167. *pbuff++ = in;
  168. /* points to the next real value */
  169. pS1++;
  170. in = *pS1++ - in;
  171. *pbuff++ = in;
  172. pS1++;
  173. in = *pS1++ - in;
  174. *pbuff++ = in;
  175. pS1++;
  176. in = *pS1++ - in;
  177. *pbuff++ = in;
  178. pS1++;
  179. /* Decrement the loop counter */
  180. i--;
  181. } while (i > 0U);
  182. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  183. ** No loop unrolling is used. */
  184. i = (S->N - 1U) % 0x4U;
  185. while (i > 0U)
  186. {
  187. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  188. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  189. in = *pS1++ - in;
  190. *pbuff++ = in;
  191. /* points to the next real value */
  192. pS1++;
  193. /* Decrement the loop counter */
  194. i--;
  195. }
  196. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  197. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  198. i = S->N >> 2U;
  199. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  200. pbuff = pInlineBuffer;
  201. /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
  202. do
  203. {
  204. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  205. in = *pbuff;
  206. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  207. in = *pbuff;
  208. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  209. in = *pbuff;
  210. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  211. in = *pbuff;
  212. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  213. /* Decrement the loop counter */
  214. i--;
  215. } while (i > 0U);
  216. #else
  217. /* Run the below code for Cortex-M0 */
  218. /* Initializing the loop counter to N/2 */
  219. i = S->Nby2;
  220. do
  221. {
  222. /* Re-ordering of even and odd elements */
  223. /* pState[i] = pInlineBuffer[2*i] */
  224. *pS1++ = *pbuff++;
  225. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  226. *pS2-- = *pbuff++;
  227. /* Decrement the loop counter */
  228. i--;
  229. } while (i > 0U);
  230. /* pbuff initialized to input buffer */
  231. pbuff = pInlineBuffer;
  232. /* pS1 initialized to pState */
  233. pS1 = pState;
  234. /* Initializing the loop counter */
  235. i = S->N;
  236. do
  237. {
  238. /* Writing the re-ordered output back to inplace input buffer */
  239. *pbuff++ = *pS1++;
  240. /* Decrement the loop counter */
  241. i--;
  242. } while (i > 0U);
  243. /* ---------------------------------------------------------
  244. * Step2: Calculate RFFT for N-point input
  245. * ---------------------------------------------------------- */
  246. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  247. arm_rfft_q31(S->pRfft, pInlineBuffer, pState);
  248. /*----------------------------------------------------------------------
  249. * Step3: Multiply the FFT output with the weights.
  250. *----------------------------------------------------------------------*/
  251. arm_cmplx_mult_cmplx_q31(pState, weights, pState, S->N);
  252. /* The output of complex multiplication is in 3.29 format.
  253. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.31 format by shifting left by 2 bits. */
  254. arm_shift_q31(pState, 2, pState, S->N * 2);
  255. /* ----------- Post-processing ---------- */
  256. /* DCT-IV can be obtained from DCT-II by the equation,
  257. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  258. * Hence, Y4(0) = Y2(0)/2 */
  259. /* Getting only real part from the output and Converting to DCT-IV */
  260. /* pbuff initialized to input buffer. */
  261. pbuff = pInlineBuffer;
  262. /* pS1 initialized to pState */
  263. pS1 = pState;
  264. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  265. in = *pS1++ >> 1U;
  266. /* input buffer acts as inplace, so output values are stored in the input itself. */
  267. *pbuff++ = in;
  268. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  269. pS1++;
  270. /* Initializing the loop counter */
  271. i = (S->N - 1U);
  272. while (i > 0U)
  273. {
  274. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  275. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  276. in = *pS1++ - in;
  277. *pbuff++ = in;
  278. /* points to the next real value */
  279. pS1++;
  280. /* Decrement the loop counter */
  281. i--;
  282. }
  283. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  284. /* Initializing the loop counter */
  285. i = S->N;
  286. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  287. pbuff = pInlineBuffer;
  288. do
  289. {
  290. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  291. in = *pbuff;
  292. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  293. /* Decrement the loop counter */
  294. i--;
  295. } while (i > 0U);
  296. #endif /* #if defined (ARM_MATH_DSP) */
  297. }
  298. /**
  299. * @} end of DCT4_IDCT4 group
  300. */