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.

382 lines
17KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. void (*ff_idct)(DCTELEM *block);
  24. void (*get_pixels)(DCTELEM *block, const UINT8 *pixels, int line_size);
  25. void (*put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  26. void (*add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  27. op_pixels_abs_func pix_abs16x16;
  28. op_pixels_abs_func pix_abs16x16_x2;
  29. op_pixels_abs_func pix_abs16x16_y2;
  30. op_pixels_abs_func pix_abs16x16_xy2;
  31. static UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
  32. UINT32 squareTbl[512];
  33. void get_pixels_c(DCTELEM *block, const UINT8 *pixels, int line_size)
  34. {
  35. DCTELEM *p;
  36. const UINT8 *pix;
  37. int i;
  38. /* read the pixels */
  39. p = block;
  40. pix = pixels;
  41. for(i=0;i<8;i++) {
  42. p[0] = pix[0];
  43. p[1] = pix[1];
  44. p[2] = pix[2];
  45. p[3] = pix[3];
  46. p[4] = pix[4];
  47. p[5] = pix[5];
  48. p[6] = pix[6];
  49. p[7] = pix[7];
  50. pix += line_size;
  51. p += 8;
  52. }
  53. }
  54. void put_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
  55. {
  56. const DCTELEM *p;
  57. UINT8 *pix;
  58. int i;
  59. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  60. /* read the pixels */
  61. p = block;
  62. pix = pixels;
  63. for(i=0;i<8;i++) {
  64. pix[0] = cm[p[0]];
  65. pix[1] = cm[p[1]];
  66. pix[2] = cm[p[2]];
  67. pix[3] = cm[p[3]];
  68. pix[4] = cm[p[4]];
  69. pix[5] = cm[p[5]];
  70. pix[6] = cm[p[6]];
  71. pix[7] = cm[p[7]];
  72. pix += line_size;
  73. p += 8;
  74. }
  75. }
  76. void add_pixels_clamped_c(const DCTELEM *block, UINT8 *pixels, int line_size)
  77. {
  78. const DCTELEM *p;
  79. UINT8 *pix;
  80. int i;
  81. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  82. /* read the pixels */
  83. p = block;
  84. pix = pixels;
  85. for(i=0;i<8;i++) {
  86. pix[0] = cm[pix[0] + p[0]];
  87. pix[1] = cm[pix[1] + p[1]];
  88. pix[2] = cm[pix[2] + p[2]];
  89. pix[3] = cm[pix[3] + p[3]];
  90. pix[4] = cm[pix[4] + p[4]];
  91. pix[5] = cm[pix[5] + p[5]];
  92. pix[6] = cm[pix[6] + p[6]];
  93. pix[7] = cm[pix[7] + p[7]];
  94. pix += line_size;
  95. p += 8;
  96. }
  97. }
  98. #define PIXOP(BTYPE, OPNAME, OP, INCR) \
  99. \
  100. static void OPNAME ## _pixels(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  101. { \
  102. BTYPE *p; \
  103. const UINT8 *pix; \
  104. \
  105. p = block; \
  106. pix = pixels; \
  107. do { \
  108. OP(p[0], pix[0]); \
  109. OP(p[1], pix[1]); \
  110. OP(p[2], pix[2]); \
  111. OP(p[3], pix[3]); \
  112. OP(p[4], pix[4]); \
  113. OP(p[5], pix[5]); \
  114. OP(p[6], pix[6]); \
  115. OP(p[7], pix[7]); \
  116. pix += line_size; \
  117. p += INCR; \
  118. } while (--h);; \
  119. } \
  120. \
  121. static void OPNAME ## _pixels_x2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  122. { \
  123. BTYPE *p; \
  124. const UINT8 *pix; \
  125. \
  126. p = block; \
  127. pix = pixels; \
  128. do { \
  129. OP(p[0], avg2(pix[0], pix[1])); \
  130. OP(p[1], avg2(pix[1], pix[2])); \
  131. OP(p[2], avg2(pix[2], pix[3])); \
  132. OP(p[3], avg2(pix[3], pix[4])); \
  133. OP(p[4], avg2(pix[4], pix[5])); \
  134. OP(p[5], avg2(pix[5], pix[6])); \
  135. OP(p[6], avg2(pix[6], pix[7])); \
  136. OP(p[7], avg2(pix[7], pix[8])); \
  137. pix += line_size; \
  138. p += INCR; \
  139. } while (--h); \
  140. } \
  141. \
  142. static void OPNAME ## _pixels_y2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  143. { \
  144. BTYPE *p; \
  145. const UINT8 *pix; \
  146. const UINT8 *pix1; \
  147. \
  148. p = block; \
  149. pix = pixels; \
  150. pix1 = pixels + line_size; \
  151. do { \
  152. OP(p[0], avg2(pix[0], pix1[0])); \
  153. OP(p[1], avg2(pix[1], pix1[1])); \
  154. OP(p[2], avg2(pix[2], pix1[2])); \
  155. OP(p[3], avg2(pix[3], pix1[3])); \
  156. OP(p[4], avg2(pix[4], pix1[4])); \
  157. OP(p[5], avg2(pix[5], pix1[5])); \
  158. OP(p[6], avg2(pix[6], pix1[6])); \
  159. OP(p[7], avg2(pix[7], pix1[7])); \
  160. pix += line_size; \
  161. pix1 += line_size; \
  162. p += INCR; \
  163. } while(--h); \
  164. } \
  165. \
  166. static void OPNAME ## _pixels_xy2(BTYPE *block, const UINT8 *pixels, int line_size, int h) \
  167. { \
  168. BTYPE *p; \
  169. const UINT8 *pix; \
  170. const UINT8 *pix1; \
  171. \
  172. p = block; \
  173. pix = pixels; \
  174. pix1 = pixels + line_size; \
  175. do { \
  176. OP(p[0], avg4(pix[0], pix[1], pix1[0], pix1[1])); \
  177. OP(p[1], avg4(pix[1], pix[2], pix1[1], pix1[2])); \
  178. OP(p[2], avg4(pix[2], pix[3], pix1[2], pix1[3])); \
  179. OP(p[3], avg4(pix[3], pix[4], pix1[3], pix1[4])); \
  180. OP(p[4], avg4(pix[4], pix[5], pix1[4], pix1[5])); \
  181. OP(p[5], avg4(pix[5], pix[6], pix1[5], pix1[6])); \
  182. OP(p[6], avg4(pix[6], pix[7], pix1[6], pix1[7])); \
  183. OP(p[7], avg4(pix[7], pix[8], pix1[7], pix1[8])); \
  184. pix += line_size; \
  185. pix1 += line_size; \
  186. p += INCR; \
  187. } while(--h); \
  188. } \
  189. \
  190. void (*OPNAME ## _pixels_tab[4])(BTYPE *block, const UINT8 *pixels, int line_size, int h) = { \
  191. OPNAME ## _pixels, \
  192. OPNAME ## _pixels_x2, \
  193. OPNAME ## _pixels_y2, \
  194. OPNAME ## _pixels_xy2, \
  195. };
  196. /* rounding primitives */
  197. #define avg2(a,b) ((a+b+1)>>1)
  198. #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
  199. #define op_put(a, b) a = b
  200. #define op_avg(a, b) a = avg2(a, b)
  201. #define op_sub(a, b) a -= b
  202. PIXOP(UINT8, put, op_put, line_size)
  203. PIXOP(UINT8, avg, op_avg, line_size)
  204. PIXOP(DCTELEM, sub, op_sub, 8)
  205. /* not rounding primitives */
  206. #undef avg2
  207. #undef avg4
  208. #define avg2(a,b) ((a+b)>>1)
  209. #define avg4(a,b,c,d) ((a+b+c+d+1)>>2)
  210. PIXOP(UINT8, put_no_rnd, op_put, line_size)
  211. PIXOP(UINT8, avg_no_rnd, op_avg, line_size)
  212. /* motion estimation */
  213. #undef avg2
  214. #undef avg4
  215. #define avg2(a,b) ((a+b+1)>>1)
  216. #define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
  217. int pix_abs16x16_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
  218. {
  219. int s, i;
  220. s = 0;
  221. for(i=0;i<h;i++) {
  222. s += abs(pix1[0] - pix2[0]);
  223. s += abs(pix1[1] - pix2[1]);
  224. s += abs(pix1[2] - pix2[2]);
  225. s += abs(pix1[3] - pix2[3]);
  226. s += abs(pix1[4] - pix2[4]);
  227. s += abs(pix1[5] - pix2[5]);
  228. s += abs(pix1[6] - pix2[6]);
  229. s += abs(pix1[7] - pix2[7]);
  230. s += abs(pix1[8] - pix2[8]);
  231. s += abs(pix1[9] - pix2[9]);
  232. s += abs(pix1[10] - pix2[10]);
  233. s += abs(pix1[11] - pix2[11]);
  234. s += abs(pix1[12] - pix2[12]);
  235. s += abs(pix1[13] - pix2[13]);
  236. s += abs(pix1[14] - pix2[14]);
  237. s += abs(pix1[15] - pix2[15]);
  238. pix1 += line_size;
  239. pix2 += line_size;
  240. }
  241. return s;
  242. }
  243. int pix_abs16x16_x2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
  244. {
  245. int s, i;
  246. s = 0;
  247. for(i=0;i<h;i++) {
  248. s += abs(pix1[0] - avg2(pix2[0], pix2[1]));
  249. s += abs(pix1[1] - avg2(pix2[1], pix2[2]));
  250. s += abs(pix1[2] - avg2(pix2[2], pix2[3]));
  251. s += abs(pix1[3] - avg2(pix2[3], pix2[4]));
  252. s += abs(pix1[4] - avg2(pix2[4], pix2[5]));
  253. s += abs(pix1[5] - avg2(pix2[5], pix2[6]));
  254. s += abs(pix1[6] - avg2(pix2[6], pix2[7]));
  255. s += abs(pix1[7] - avg2(pix2[7], pix2[8]));
  256. s += abs(pix1[8] - avg2(pix2[8], pix2[9]));
  257. s += abs(pix1[9] - avg2(pix2[9], pix2[10]));
  258. s += abs(pix1[10] - avg2(pix2[10], pix2[11]));
  259. s += abs(pix1[11] - avg2(pix2[11], pix2[12]));
  260. s += abs(pix1[12] - avg2(pix2[12], pix2[13]));
  261. s += abs(pix1[13] - avg2(pix2[13], pix2[14]));
  262. s += abs(pix1[14] - avg2(pix2[14], pix2[15]));
  263. s += abs(pix1[15] - avg2(pix2[15], pix2[16]));
  264. pix1 += line_size;
  265. pix2 += line_size;
  266. }
  267. return s;
  268. }
  269. int pix_abs16x16_y2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
  270. {
  271. int s, i;
  272. UINT8 *pix3 = pix2 + line_size;
  273. s = 0;
  274. for(i=0;i<h;i++) {
  275. s += abs(pix1[0] - avg2(pix2[0], pix3[0]));
  276. s += abs(pix1[1] - avg2(pix2[1], pix3[1]));
  277. s += abs(pix1[2] - avg2(pix2[2], pix3[2]));
  278. s += abs(pix1[3] - avg2(pix2[3], pix3[3]));
  279. s += abs(pix1[4] - avg2(pix2[4], pix3[4]));
  280. s += abs(pix1[5] - avg2(pix2[5], pix3[5]));
  281. s += abs(pix1[6] - avg2(pix2[6], pix3[6]));
  282. s += abs(pix1[7] - avg2(pix2[7], pix3[7]));
  283. s += abs(pix1[8] - avg2(pix2[8], pix3[8]));
  284. s += abs(pix1[9] - avg2(pix2[9], pix3[9]));
  285. s += abs(pix1[10] - avg2(pix2[10], pix3[10]));
  286. s += abs(pix1[11] - avg2(pix2[11], pix3[11]));
  287. s += abs(pix1[12] - avg2(pix2[12], pix3[12]));
  288. s += abs(pix1[13] - avg2(pix2[13], pix3[13]));
  289. s += abs(pix1[14] - avg2(pix2[14], pix3[14]));
  290. s += abs(pix1[15] - avg2(pix2[15], pix3[15]));
  291. pix1 += line_size;
  292. pix2 += line_size;
  293. pix3 += line_size;
  294. }
  295. return s;
  296. }
  297. int pix_abs16x16_xy2_c(UINT8 *pix1, UINT8 *pix2, int line_size, int h)
  298. {
  299. int s, i;
  300. UINT8 *pix3 = pix2 + line_size;
  301. s = 0;
  302. for(i=0;i<h;i++) {
  303. s += abs(pix1[0] - avg4(pix2[0], pix2[1], pix3[0], pix3[1]));
  304. s += abs(pix1[1] - avg4(pix2[1], pix2[2], pix3[1], pix3[2]));
  305. s += abs(pix1[2] - avg4(pix2[2], pix2[3], pix3[2], pix3[3]));
  306. s += abs(pix1[3] - avg4(pix2[3], pix2[4], pix3[3], pix3[4]));
  307. s += abs(pix1[4] - avg4(pix2[4], pix2[5], pix3[4], pix3[5]));
  308. s += abs(pix1[5] - avg4(pix2[5], pix2[6], pix3[5], pix3[6]));
  309. s += abs(pix1[6] - avg4(pix2[6], pix2[7], pix3[6], pix3[7]));
  310. s += abs(pix1[7] - avg4(pix2[7], pix2[8], pix3[7], pix3[8]));
  311. s += abs(pix1[8] - avg4(pix2[8], pix2[9], pix3[8], pix3[9]));
  312. s += abs(pix1[9] - avg4(pix2[9], pix2[10], pix3[9], pix3[10]));
  313. s += abs(pix1[10] - avg4(pix2[10], pix2[11], pix3[10], pix3[11]));
  314. s += abs(pix1[11] - avg4(pix2[11], pix2[12], pix3[11], pix3[12]));
  315. s += abs(pix1[12] - avg4(pix2[12], pix2[13], pix3[12], pix3[13]));
  316. s += abs(pix1[13] - avg4(pix2[13], pix2[14], pix3[13], pix3[14]));
  317. s += abs(pix1[14] - avg4(pix2[14], pix2[15], pix3[14], pix3[15]));
  318. s += abs(pix1[15] - avg4(pix2[15], pix2[16], pix3[15], pix3[16]));
  319. pix1 += line_size;
  320. pix2 += line_size;
  321. pix3 += line_size;
  322. }
  323. return s;
  324. }
  325. void dsputil_init(void)
  326. {
  327. int i;
  328. for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
  329. for(i=0;i<MAX_NEG_CROP;i++) {
  330. cropTbl[i] = 0;
  331. cropTbl[i + MAX_NEG_CROP + 256] = 255;
  332. }
  333. for(i=0;i<512;i++) {
  334. squareTbl[i] = (i - 256) * (i - 256);
  335. }
  336. ff_idct = j_rev_dct;
  337. get_pixels = get_pixels_c;
  338. put_pixels_clamped = put_pixels_clamped_c;
  339. add_pixels_clamped = add_pixels_clamped_c;
  340. pix_abs16x16 = pix_abs16x16_c;
  341. pix_abs16x16_x2 = pix_abs16x16_x2_c;
  342. pix_abs16x16_y2 = pix_abs16x16_y2_c;
  343. pix_abs16x16_xy2 = pix_abs16x16_xy2_c;
  344. av_fdct = jpeg_fdct_ifast;
  345. #ifdef HAVE_MMX
  346. dsputil_init_mmx();
  347. #endif
  348. }