You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
7.5KB

  1. /*
  2. * Simple IDCT (Alpha optimized)
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * based upon some outcommented C code from mpeg2dec (idct_mmx.c
  7. * written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
  8. *
  9. * Alpha optimizations by Måns Rullgård <mans@mansr.com>
  10. * and Falk Hueffner <falk@debian.org>
  11. *
  12. * This file is part of FFmpeg.
  13. *
  14. * FFmpeg is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * FFmpeg is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with FFmpeg; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include "libavcodec/dsputil.h"
  29. #include "dsputil_alpha.h"
  30. #include "asm.h"
  31. // cos(i * M_PI / 16) * sqrt(2) * (1 << 14)
  32. // W4 is actually exactly 16384, but using 16383 works around
  33. // accumulating rounding errors for some encoders
  34. #define W1 ((int_fast32_t) 22725)
  35. #define W2 ((int_fast32_t) 21407)
  36. #define W3 ((int_fast32_t) 19266)
  37. #define W4 ((int_fast32_t) 16383)
  38. #define W5 ((int_fast32_t) 12873)
  39. #define W6 ((int_fast32_t) 8867)
  40. #define W7 ((int_fast32_t) 4520)
  41. #define ROW_SHIFT 11
  42. #define COL_SHIFT 20
  43. /* 0: all entries 0, 1: only first entry nonzero, 2: otherwise */
  44. static inline int idct_row(DCTELEM *row)
  45. {
  46. int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3, t;
  47. uint64_t l, r, t2;
  48. l = ldq(row);
  49. r = ldq(row + 4);
  50. if (l == 0 && r == 0)
  51. return 0;
  52. a0 = W4 * sextw(l) + (1 << (ROW_SHIFT - 1));
  53. if (((l & ~0xffffUL) | r) == 0) {
  54. a0 >>= ROW_SHIFT;
  55. t2 = (uint16_t) a0;
  56. t2 |= t2 << 16;
  57. t2 |= t2 << 32;
  58. stq(t2, row);
  59. stq(t2, row + 4);
  60. return 1;
  61. }
  62. a1 = a0;
  63. a2 = a0;
  64. a3 = a0;
  65. t = extwl(l, 4); /* row[2] */
  66. if (t != 0) {
  67. t = sextw(t);
  68. a0 += W2 * t;
  69. a1 += W6 * t;
  70. a2 -= W6 * t;
  71. a3 -= W2 * t;
  72. }
  73. t = extwl(r, 0); /* row[4] */
  74. if (t != 0) {
  75. t = sextw(t);
  76. a0 += W4 * t;
  77. a1 -= W4 * t;
  78. a2 -= W4 * t;
  79. a3 += W4 * t;
  80. }
  81. t = extwl(r, 4); /* row[6] */
  82. if (t != 0) {
  83. t = sextw(t);
  84. a0 += W6 * t;
  85. a1 -= W2 * t;
  86. a2 += W2 * t;
  87. a3 -= W6 * t;
  88. }
  89. t = extwl(l, 2); /* row[1] */
  90. if (t != 0) {
  91. t = sextw(t);
  92. b0 = W1 * t;
  93. b1 = W3 * t;
  94. b2 = W5 * t;
  95. b3 = W7 * t;
  96. } else {
  97. b0 = 0;
  98. b1 = 0;
  99. b2 = 0;
  100. b3 = 0;
  101. }
  102. t = extwl(l, 6); /* row[3] */
  103. if (t) {
  104. t = sextw(t);
  105. b0 += W3 * t;
  106. b1 -= W7 * t;
  107. b2 -= W1 * t;
  108. b3 -= W5 * t;
  109. }
  110. t = extwl(r, 2); /* row[5] */
  111. if (t) {
  112. t = sextw(t);
  113. b0 += W5 * t;
  114. b1 -= W1 * t;
  115. b2 += W7 * t;
  116. b3 += W3 * t;
  117. }
  118. t = extwl(r, 6); /* row[7] */
  119. if (t) {
  120. t = sextw(t);
  121. b0 += W7 * t;
  122. b1 -= W5 * t;
  123. b2 += W3 * t;
  124. b3 -= W1 * t;
  125. }
  126. row[0] = (a0 + b0) >> ROW_SHIFT;
  127. row[1] = (a1 + b1) >> ROW_SHIFT;
  128. row[2] = (a2 + b2) >> ROW_SHIFT;
  129. row[3] = (a3 + b3) >> ROW_SHIFT;
  130. row[4] = (a3 - b3) >> ROW_SHIFT;
  131. row[5] = (a2 - b2) >> ROW_SHIFT;
  132. row[6] = (a1 - b1) >> ROW_SHIFT;
  133. row[7] = (a0 - b0) >> ROW_SHIFT;
  134. return 2;
  135. }
  136. static inline void idct_col(DCTELEM *col)
  137. {
  138. int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
  139. col[0] += (1 << (COL_SHIFT - 1)) / W4;
  140. a0 = W4 * col[8 * 0];
  141. a1 = W4 * col[8 * 0];
  142. a2 = W4 * col[8 * 0];
  143. a3 = W4 * col[8 * 0];
  144. if (col[8 * 2]) {
  145. a0 += W2 * col[8 * 2];
  146. a1 += W6 * col[8 * 2];
  147. a2 -= W6 * col[8 * 2];
  148. a3 -= W2 * col[8 * 2];
  149. }
  150. if (col[8 * 4]) {
  151. a0 += W4 * col[8 * 4];
  152. a1 -= W4 * col[8 * 4];
  153. a2 -= W4 * col[8 * 4];
  154. a3 += W4 * col[8 * 4];
  155. }
  156. if (col[8 * 6]) {
  157. a0 += W6 * col[8 * 6];
  158. a1 -= W2 * col[8 * 6];
  159. a2 += W2 * col[8 * 6];
  160. a3 -= W6 * col[8 * 6];
  161. }
  162. if (col[8 * 1]) {
  163. b0 = W1 * col[8 * 1];
  164. b1 = W3 * col[8 * 1];
  165. b2 = W5 * col[8 * 1];
  166. b3 = W7 * col[8 * 1];
  167. } else {
  168. b0 = 0;
  169. b1 = 0;
  170. b2 = 0;
  171. b3 = 0;
  172. }
  173. if (col[8 * 3]) {
  174. b0 += W3 * col[8 * 3];
  175. b1 -= W7 * col[8 * 3];
  176. b2 -= W1 * col[8 * 3];
  177. b3 -= W5 * col[8 * 3];
  178. }
  179. if (col[8 * 5]) {
  180. b0 += W5 * col[8 * 5];
  181. b1 -= W1 * col[8 * 5];
  182. b2 += W7 * col[8 * 5];
  183. b3 += W3 * col[8 * 5];
  184. }
  185. if (col[8 * 7]) {
  186. b0 += W7 * col[8 * 7];
  187. b1 -= W5 * col[8 * 7];
  188. b2 += W3 * col[8 * 7];
  189. b3 -= W1 * col[8 * 7];
  190. }
  191. col[8 * 0] = (a0 + b0) >> COL_SHIFT;
  192. col[8 * 7] = (a0 - b0) >> COL_SHIFT;
  193. col[8 * 1] = (a1 + b1) >> COL_SHIFT;
  194. col[8 * 6] = (a1 - b1) >> COL_SHIFT;
  195. col[8 * 2] = (a2 + b2) >> COL_SHIFT;
  196. col[8 * 5] = (a2 - b2) >> COL_SHIFT;
  197. col[8 * 3] = (a3 + b3) >> COL_SHIFT;
  198. col[8 * 4] = (a3 - b3) >> COL_SHIFT;
  199. }
  200. /* If all rows but the first one are zero after row transformation,
  201. all rows will be identical after column transformation. */
  202. static inline void idct_col2(DCTELEM *col)
  203. {
  204. int i;
  205. uint64_t l, r;
  206. for (i = 0; i < 8; ++i) {
  207. int_fast32_t a0 = col[i] + (1 << (COL_SHIFT - 1)) / W4;
  208. a0 *= W4;
  209. col[i] = a0 >> COL_SHIFT;
  210. }
  211. l = ldq(col + 0 * 4); r = ldq(col + 1 * 4);
  212. stq(l, col + 2 * 4); stq(r, col + 3 * 4);
  213. stq(l, col + 4 * 4); stq(r, col + 5 * 4);
  214. stq(l, col + 6 * 4); stq(r, col + 7 * 4);
  215. stq(l, col + 8 * 4); stq(r, col + 9 * 4);
  216. stq(l, col + 10 * 4); stq(r, col + 11 * 4);
  217. stq(l, col + 12 * 4); stq(r, col + 13 * 4);
  218. stq(l, col + 14 * 4); stq(r, col + 15 * 4);
  219. }
  220. void ff_simple_idct_axp(DCTELEM *block)
  221. {
  222. int i;
  223. int rowsZero = 1; /* all rows except row 0 zero */
  224. int rowsConstant = 1; /* all rows consist of a constant value */
  225. for (i = 0; i < 8; i++) {
  226. int sparseness = idct_row(block + 8 * i);
  227. if (i > 0 && sparseness > 0)
  228. rowsZero = 0;
  229. if (sparseness == 2)
  230. rowsConstant = 0;
  231. }
  232. if (rowsZero) {
  233. idct_col2(block);
  234. } else if (rowsConstant) {
  235. idct_col(block);
  236. for (i = 0; i < 8; i += 2) {
  237. uint64_t v = (uint16_t) block[0];
  238. uint64_t w = (uint16_t) block[8];
  239. v |= v << 16;
  240. w |= w << 16;
  241. v |= v << 32;
  242. w |= w << 32;
  243. stq(v, block + 0 * 4);
  244. stq(v, block + 1 * 4);
  245. stq(w, block + 2 * 4);
  246. stq(w, block + 3 * 4);
  247. block += 4 * 4;
  248. }
  249. } else {
  250. for (i = 0; i < 8; i++)
  251. idct_col(block + i);
  252. }
  253. }
  254. void ff_simple_idct_put_axp(uint8_t *dest, int line_size, DCTELEM *block)
  255. {
  256. ff_simple_idct_axp(block);
  257. put_pixels_clamped_axp_p(block, dest, line_size);
  258. }
  259. void ff_simple_idct_add_axp(uint8_t *dest, int line_size, DCTELEM *block)
  260. {
  261. ff_simple_idct_axp(block);
  262. add_pixels_clamped_axp_p(block, dest, line_size);
  263. }