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.

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