arm_dct4_q15.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_q15.c
  9. *
  10. * Description: Processing function of DCT4 & IDCT4 Q15.
  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 Q15 DCT4/IDCT4.
  47. * @param[in] *S points to an instance of the Q15 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. *
  52. * \par Input an output formats:
  53. * Internally inputs are downscaled in the RFFT process function to avoid overflows.
  54. * Number of bits downscaled, depends on the size of the transform.
  55. * The input and output formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
  56. *
  57. * \image html dct4FormatsQ15Table.gif
  58. */
  59. void arm_dct4_q15(
  60. const arm_dct4_instance_q15 * S,
  61. q15_t * pState,
  62. q15_t * pInlineBuffer)
  63. {
  64. uint32_t i; /* Loop counter */
  65. q15_t *weights = S->pTwiddle; /* Pointer to the Weights table */
  66. q15_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
  67. q15_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
  68. q15_t in; /* Temporary variable */
  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. /*-------- Pre-processing ------------*/
  88. /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
  89. arm_mult_q15(pInlineBuffer, cosFact, pInlineBuffer, S->N);
  90. arm_shift_q15(pInlineBuffer, 1, pInlineBuffer, S->N);
  91. /* ----------------------------------------------------------------
  92. * Step1: Re-ordering of even and odd elements as
  93. * pState[i] = pInlineBuffer[2*i] and
  94. * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
  95. ---------------------------------------------------------------------*/
  96. /* pS1 initialized to pState */
  97. pS1 = pState;
  98. /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
  99. pS2 = pState + (S->N - 1u);
  100. /* pbuff initialized to input buffer */
  101. pbuff = pInlineBuffer;
  102. #ifndef ARM_MATH_CM0_FAMILY
  103. /* Run the below code for Cortex-M4 and Cortex-M3 */
  104. /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
  105. i = (uint32_t) S->Nby2 >> 2u;
  106. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  107. ** a second loop below computes the remaining 1 to 3 samples. */
  108. do
  109. {
  110. /* Re-ordering of even and odd elements */
  111. /* pState[i] = pInlineBuffer[2*i] */
  112. *pS1++ = *pbuff++;
  113. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  114. *pS2-- = *pbuff++;
  115. *pS1++ = *pbuff++;
  116. *pS2-- = *pbuff++;
  117. *pS1++ = *pbuff++;
  118. *pS2-- = *pbuff++;
  119. *pS1++ = *pbuff++;
  120. *pS2-- = *pbuff++;
  121. /* Decrement the loop counter */
  122. i--;
  123. } while(i > 0u);
  124. /* pbuff initialized to input buffer */
  125. pbuff = pInlineBuffer;
  126. /* pS1 initialized to pState */
  127. pS1 = pState;
  128. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  129. i = (uint32_t) S->N >> 2u;
  130. /* Processing with loop unrolling 4 times as N is always multiple of 4.
  131. * Compute 4 outputs at a time */
  132. do
  133. {
  134. /* Writing the re-ordered output back to inplace input buffer */
  135. *pbuff++ = *pS1++;
  136. *pbuff++ = *pS1++;
  137. *pbuff++ = *pS1++;
  138. *pbuff++ = *pS1++;
  139. /* Decrement the loop counter */
  140. i--;
  141. } while(i > 0u);
  142. /* ---------------------------------------------------------
  143. * Step2: Calculate RFFT for N-point input
  144. * ---------------------------------------------------------- */
  145. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  146. arm_rfft_q15(S->pRfft, pInlineBuffer, pState);
  147. /*----------------------------------------------------------------------
  148. * Step3: Multiply the FFT output with the weights.
  149. *----------------------------------------------------------------------*/
  150. arm_cmplx_mult_cmplx_q15(pState, weights, pState, S->N);
  151. /* The output of complex multiplication is in 3.13 format.
  152. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
  153. arm_shift_q15(pState, 2, pState, S->N * 2);
  154. /* ----------- Post-processing ---------- */
  155. /* DCT-IV can be obtained from DCT-II by the equation,
  156. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  157. * Hence, Y4(0) = Y2(0)/2 */
  158. /* Getting only real part from the output and Converting to DCT-IV */
  159. /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
  160. i = ((uint32_t) S->N - 1u) >> 2u;
  161. /* pbuff initialized to input buffer. */
  162. pbuff = pInlineBuffer;
  163. /* pS1 initialized to pState */
  164. pS1 = pState;
  165. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  166. in = *pS1++ >> 1u;
  167. /* input buffer acts as inplace, so output values are stored in the input itself. */
  168. *pbuff++ = in;
  169. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  170. pS1++;
  171. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  172. ** a second loop below computes the remaining 1 to 3 samples. */
  173. do
  174. {
  175. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  176. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  177. in = *pS1++ - in;
  178. *pbuff++ = in;
  179. /* points to the next real value */
  180. pS1++;
  181. in = *pS1++ - in;
  182. *pbuff++ = in;
  183. pS1++;
  184. in = *pS1++ - in;
  185. *pbuff++ = in;
  186. pS1++;
  187. in = *pS1++ - in;
  188. *pbuff++ = in;
  189. pS1++;
  190. /* Decrement the loop counter */
  191. i--;
  192. } while(i > 0u);
  193. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  194. ** No loop unrolling is used. */
  195. i = ((uint32_t) S->N - 1u) % 0x4u;
  196. while(i > 0u)
  197. {
  198. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  199. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  200. in = *pS1++ - in;
  201. *pbuff++ = in;
  202. /* points to the next real value */
  203. pS1++;
  204. /* Decrement the loop counter */
  205. i--;
  206. }
  207. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  208. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  209. i = (uint32_t) S->N >> 2u;
  210. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  211. pbuff = pInlineBuffer;
  212. /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
  213. do
  214. {
  215. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  216. in = *pbuff;
  217. *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
  218. in = *pbuff;
  219. *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
  220. in = *pbuff;
  221. *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
  222. in = *pbuff;
  223. *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
  224. /* Decrement the loop counter */
  225. i--;
  226. } while(i > 0u);
  227. #else
  228. /* Run the below code for Cortex-M0 */
  229. /* Initializing the loop counter to N/2 */
  230. i = (uint32_t) S->Nby2;
  231. do
  232. {
  233. /* Re-ordering of even and odd elements */
  234. /* pState[i] = pInlineBuffer[2*i] */
  235. *pS1++ = *pbuff++;
  236. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  237. *pS2-- = *pbuff++;
  238. /* Decrement the loop counter */
  239. i--;
  240. } while(i > 0u);
  241. /* pbuff initialized to input buffer */
  242. pbuff = pInlineBuffer;
  243. /* pS1 initialized to pState */
  244. pS1 = pState;
  245. /* Initializing the loop counter */
  246. i = (uint32_t) S->N;
  247. do
  248. {
  249. /* Writing the re-ordered output back to inplace input buffer */
  250. *pbuff++ = *pS1++;
  251. /* Decrement the loop counter */
  252. i--;
  253. } while(i > 0u);
  254. /* ---------------------------------------------------------
  255. * Step2: Calculate RFFT for N-point input
  256. * ---------------------------------------------------------- */
  257. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  258. arm_rfft_q15(S->pRfft, pInlineBuffer, pState);
  259. /*----------------------------------------------------------------------
  260. * Step3: Multiply the FFT output with the weights.
  261. *----------------------------------------------------------------------*/
  262. arm_cmplx_mult_cmplx_q15(pState, weights, pState, S->N);
  263. /* The output of complex multiplication is in 3.13 format.
  264. * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
  265. arm_shift_q15(pState, 2, pState, S->N * 2);
  266. /* ----------- Post-processing ---------- */
  267. /* DCT-IV can be obtained from DCT-II by the equation,
  268. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  269. * Hence, Y4(0) = Y2(0)/2 */
  270. /* Getting only real part from the output and Converting to DCT-IV */
  271. /* Initializing the loop counter */
  272. i = ((uint32_t) S->N - 1u);
  273. /* pbuff initialized to input buffer. */
  274. pbuff = pInlineBuffer;
  275. /* pS1 initialized to pState */
  276. pS1 = pState;
  277. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  278. in = *pS1++ >> 1u;
  279. /* input buffer acts as inplace, so output values are stored in the input itself. */
  280. *pbuff++ = in;
  281. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  282. pS1++;
  283. do
  284. {
  285. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  286. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  287. in = *pS1++ - in;
  288. *pbuff++ = in;
  289. /* points to the next real value */
  290. pS1++;
  291. /* Decrement the loop counter */
  292. i--;
  293. } while(i > 0u);
  294. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  295. /* Initializing the loop counter */
  296. i = (uint32_t) S->N;
  297. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  298. pbuff = pInlineBuffer;
  299. do
  300. {
  301. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  302. in = *pbuff;
  303. *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
  304. /* Decrement the loop counter */
  305. i--;
  306. } while(i > 0u);
  307. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  308. }
  309. /**
  310. * @} end of DCT4_IDCT4 group
  311. */