arm_dct4_q31.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_dct4_q31.c
  9. *
  10. * Description: Processing function of DCT4 & IDCT4 Q31.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @addtogroup DCT4_IDCT4
  43. * @{
  44. */
  45. /**
  46. * @brief Processing function for the Q31 DCT4/IDCT4.
  47. * @param[in] *S points to an instance of the Q31 DCT4 structure.
  48. * @param[in] *pState points to state buffer.
  49. * @param[in,out] *pInlineBuffer points to the in-place input and output buffer.
  50. * @return none.
  51. * \par Input an output formats:
  52. * Input samples need to be downscaled by 1 bit to avoid saturations in the Q31 DCT process,
  53. * as the conversion from DCT2 to DCT4 involves one subtraction.
  54. * Internally inputs are downscaled in the RFFT process function to avoid overflows.
  55. * Number of bits downscaled, depends on the size of the transform.
  56. * The input and output formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
  57. *
  58. * \image html dct4FormatsQ31Table.gif
  59. */
  60. void arm_dct4_q31(
  61. const arm_dct4_instance_q31 * S,
  62. q31_t * pState,
  63. q31_t * pInlineBuffer)
  64. {
  65. uint16_t i; /* Loop counter */
  66. q31_t *weights = S->pTwiddle; /* Pointer to the Weights table */
  67. q31_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
  68. q31_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
  69. q31_t in; /* Temporary variable */
  70. /* DCT4 computation involves DCT2 (which is calculated using RFFT)
  71. * along with some pre-processing and post-processing.
  72. * Computational procedure is explained as follows:
  73. * (a) Pre-processing involves multiplying input with cos factor,
  74. * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
  75. * where,
  76. * r(n) -- output of preprocessing
  77. * u(n) -- input to preprocessing(actual Source buffer)
  78. * (b) Calculation of DCT2 using FFT is divided into three steps:
  79. * Step1: Re-ordering of even and odd elements of input.
  80. * Step2: Calculating FFT of the re-ordered input.
  81. * Step3: Taking the real part of the product of FFT output and weights.
  82. * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
  83. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  84. * where,
  85. * Y4 -- DCT4 output, Y2 -- DCT2 output
  86. * (d) Multiplying the output with the normalizing factor sqrt(2/N).
  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_q31(pInlineBuffer, cosFact, pInlineBuffer, S->N);
  91. arm_shift_q31(pInlineBuffer, 1, pInlineBuffer, S->N);
  92. /* ----------------------------------------------------------------
  93. * Step1: Re-ordering of even and odd elements as
  94. * pState[i] = pInlineBuffer[2*i] and
  95. * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
  96. ---------------------------------------------------------------------*/
  97. /* pS1 initialized to pState */
  98. pS1 = pState;
  99. /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
  100. pS2 = pState + (S->N - 1u);
  101. /* pbuff initialized to input buffer */
  102. pbuff = pInlineBuffer;
  103. #ifndef ARM_MATH_CM0_FAMILY
  104. /* Run the below code for Cortex-M4 and Cortex-M3 */
  105. /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
  106. i = S->Nby2 >> 2u;
  107. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  108. ** a second loop below computes the remaining 1 to 3 samples. */
  109. do
  110. {
  111. /* Re-ordering of even and odd elements */
  112. /* pState[i] = pInlineBuffer[2*i] */
  113. *pS1++ = *pbuff++;
  114. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  115. *pS2-- = *pbuff++;
  116. *pS1++ = *pbuff++;
  117. *pS2-- = *pbuff++;
  118. *pS1++ = *pbuff++;
  119. *pS2-- = *pbuff++;
  120. *pS1++ = *pbuff++;
  121. *pS2-- = *pbuff++;
  122. /* Decrement the loop counter */
  123. i--;
  124. } while(i > 0u);
  125. /* pbuff initialized to input buffer */
  126. pbuff = pInlineBuffer;
  127. /* pS1 initialized to pState */
  128. pS1 = pState;
  129. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  130. i = S->N >> 2u;
  131. /* Processing with loop unrolling 4 times as N is always multiple of 4.
  132. * Compute 4 outputs at a time */
  133. do
  134. {
  135. /* Writing the re-ordered output back to inplace input buffer */
  136. *pbuff++ = *pS1++;
  137. *pbuff++ = *pS1++;
  138. *pbuff++ = *pS1++;
  139. *pbuff++ = *pS1++;
  140. /* Decrement the loop counter */
  141. i--;
  142. } while(i > 0u);
  143. /* ---------------------------------------------------------
  144. * Step2: Calculate RFFT for N-point input
  145. * ---------------------------------------------------------- */
  146. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  147. arm_rfft_q31(S->pRfft, pInlineBuffer, pState);
  148. /*----------------------------------------------------------------------
  149. * Step3: Multiply the FFT output with the weights.
  150. *----------------------------------------------------------------------*/
  151. arm_cmplx_mult_cmplx_q31(pState, weights, pState, S->N);
  152. /* The output of complex multiplication is in 3.29 format.
  153. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.31 format by shifting left by 2 bits. */
  154. arm_shift_q31(pState, 2, pState, S->N * 2);
  155. /* ----------- Post-processing ---------- */
  156. /* DCT-IV can be obtained from DCT-II by the equation,
  157. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  158. * Hence, Y4(0) = Y2(0)/2 */
  159. /* Getting only real part from the output and Converting to DCT-IV */
  160. /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
  161. i = (S->N - 1u) >> 2u;
  162. /* pbuff initialized to input buffer. */
  163. pbuff = pInlineBuffer;
  164. /* pS1 initialized to pState */
  165. pS1 = pState;
  166. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  167. in = *pS1++ >> 1u;
  168. /* input buffer acts as inplace, so output values are stored in the input itself. */
  169. *pbuff++ = in;
  170. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  171. pS1++;
  172. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  173. ** a second loop below computes the remaining 1 to 3 samples. */
  174. do
  175. {
  176. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  177. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  178. in = *pS1++ - in;
  179. *pbuff++ = in;
  180. /* points to the next real value */
  181. pS1++;
  182. in = *pS1++ - in;
  183. *pbuff++ = in;
  184. pS1++;
  185. in = *pS1++ - in;
  186. *pbuff++ = in;
  187. pS1++;
  188. in = *pS1++ - in;
  189. *pbuff++ = in;
  190. pS1++;
  191. /* Decrement the loop counter */
  192. i--;
  193. } while(i > 0u);
  194. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  195. ** No loop unrolling is used. */
  196. i = (S->N - 1u) % 0x4u;
  197. while(i > 0u)
  198. {
  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 */
  201. in = *pS1++ - in;
  202. *pbuff++ = in;
  203. /* points to the next real value */
  204. pS1++;
  205. /* Decrement the loop counter */
  206. i--;
  207. }
  208. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  209. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  210. i = S->N >> 2u;
  211. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  212. pbuff = pInlineBuffer;
  213. /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
  214. do
  215. {
  216. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  217. in = *pbuff;
  218. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  219. in = *pbuff;
  220. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  221. in = *pbuff;
  222. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  223. in = *pbuff;
  224. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  225. /* Decrement the loop counter */
  226. i--;
  227. } while(i > 0u);
  228. #else
  229. /* Run the below code for Cortex-M0 */
  230. /* Initializing the loop counter to N/2 */
  231. i = S->Nby2;
  232. do
  233. {
  234. /* Re-ordering of even and odd elements */
  235. /* pState[i] = pInlineBuffer[2*i] */
  236. *pS1++ = *pbuff++;
  237. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  238. *pS2-- = *pbuff++;
  239. /* Decrement the loop counter */
  240. i--;
  241. } while(i > 0u);
  242. /* pbuff initialized to input buffer */
  243. pbuff = pInlineBuffer;
  244. /* pS1 initialized to pState */
  245. pS1 = pState;
  246. /* Initializing the loop counter */
  247. i = S->N;
  248. do
  249. {
  250. /* Writing the re-ordered output back to inplace input buffer */
  251. *pbuff++ = *pS1++;
  252. /* Decrement the loop counter */
  253. i--;
  254. } while(i > 0u);
  255. /* ---------------------------------------------------------
  256. * Step2: Calculate RFFT for N-point input
  257. * ---------------------------------------------------------- */
  258. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  259. arm_rfft_q31(S->pRfft, pInlineBuffer, pState);
  260. /*----------------------------------------------------------------------
  261. * Step3: Multiply the FFT output with the weights.
  262. *----------------------------------------------------------------------*/
  263. arm_cmplx_mult_cmplx_q31(pState, weights, pState, S->N);
  264. /* The output of complex multiplication is in 3.29 format.
  265. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.31 format by shifting left by 2 bits. */
  266. arm_shift_q31(pState, 2, pState, S->N * 2);
  267. /* ----------- Post-processing ---------- */
  268. /* DCT-IV can be obtained from DCT-II by the equation,
  269. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  270. * Hence, Y4(0) = Y2(0)/2 */
  271. /* Getting only real part from the output and Converting to DCT-IV */
  272. /* pbuff initialized to input buffer. */
  273. pbuff = pInlineBuffer;
  274. /* pS1 initialized to pState */
  275. pS1 = pState;
  276. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  277. in = *pS1++ >> 1u;
  278. /* input buffer acts as inplace, so output values are stored in the input itself. */
  279. *pbuff++ = in;
  280. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  281. pS1++;
  282. /* Initializing the loop counter */
  283. i = (S->N - 1u);
  284. while(i > 0u)
  285. {
  286. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  287. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  288. in = *pS1++ - in;
  289. *pbuff++ = in;
  290. /* points to the next real value */
  291. pS1++;
  292. /* Decrement the loop counter */
  293. i--;
  294. }
  295. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  296. /* Initializing the loop counter */
  297. i = S->N;
  298. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  299. pbuff = pInlineBuffer;
  300. do
  301. {
  302. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  303. in = *pbuff;
  304. *pbuff++ = ((q31_t) (((q63_t) in * S->normalize) >> 31));
  305. /* Decrement the loop counter */
  306. i--;
  307. } while(i > 0u);
  308. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  309. }
  310. /**
  311. * @} end of DCT4_IDCT4 group
  312. */