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.

272 lines
12KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * DSP utils
  27. */
  28. #include "bit_depth_template.c"
  29. /* draw the edges of width 'w' of an image of size width, height */
  30. //FIXME check that this is ok for mpeg4 interlaced
  31. static void FUNCC(draw_edges)(uint8_t *_buf, int _wrap, int width, int height, int w, int h, int sides)
  32. {
  33. pixel *buf = (pixel*)_buf;
  34. int wrap = _wrap / sizeof(pixel);
  35. pixel *ptr, *last_line;
  36. int i;
  37. /* left and right */
  38. ptr = buf;
  39. for(i=0;i<height;i++) {
  40. #if BIT_DEPTH > 8
  41. int j;
  42. for (j = 0; j < w; j++) {
  43. ptr[j-w] = ptr[0];
  44. ptr[j+width] = ptr[width-1];
  45. }
  46. #else
  47. memset(ptr - w, ptr[0], w);
  48. memset(ptr + width, ptr[width-1], w);
  49. #endif
  50. ptr += wrap;
  51. }
  52. /* top and bottom + corners */
  53. buf -= w;
  54. last_line = buf + (height - 1) * wrap;
  55. if (sides & EDGE_TOP)
  56. for(i = 0; i < h; i++)
  57. memcpy(buf - (i + 1) * wrap, buf, (width + w + w) * sizeof(pixel)); // top
  58. if (sides & EDGE_BOTTOM)
  59. for (i = 0; i < h; i++)
  60. memcpy(last_line + (i + 1) * wrap, last_line, (width + w + w) * sizeof(pixel)); // bottom
  61. }
  62. #define DCTELEM_FUNCS(dctcoef, suffix) \
  63. static void FUNCC(get_pixels ## suffix)(int16_t *restrict _block, \
  64. const uint8_t *_pixels, \
  65. int line_size) \
  66. { \
  67. const pixel *pixels = (const pixel *) _pixels; \
  68. dctcoef *restrict block = (dctcoef *) _block; \
  69. int i; \
  70. \
  71. /* read the pixels */ \
  72. for(i=0;i<8;i++) { \
  73. block[0] = pixels[0]; \
  74. block[1] = pixels[1]; \
  75. block[2] = pixels[2]; \
  76. block[3] = pixels[3]; \
  77. block[4] = pixels[4]; \
  78. block[5] = pixels[5]; \
  79. block[6] = pixels[6]; \
  80. block[7] = pixels[7]; \
  81. pixels += line_size / sizeof(pixel); \
  82. block += 8; \
  83. } \
  84. } \
  85. \
  86. static void FUNCC(clear_block ## suffix)(int16_t *block) \
  87. { \
  88. memset(block, 0, sizeof(dctcoef)*64); \
  89. } \
  90. \
  91. /** \
  92. * memset(blocks, 0, sizeof(int16_t)*6*64) \
  93. */ \
  94. static void FUNCC(clear_blocks ## suffix)(int16_t *blocks) \
  95. { \
  96. memset(blocks, 0, sizeof(dctcoef)*6*64); \
  97. }
  98. DCTELEM_FUNCS(int16_t, _16)
  99. #if BIT_DEPTH > 8
  100. DCTELEM_FUNCS(dctcoef, _32)
  101. #endif
  102. #if BIT_DEPTH == 8
  103. #include "hpel_template.c"
  104. #endif
  105. #define PIXOP2(OPNAME, OP) \
  106. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
  107. int src_stride1, int src_stride2, int h){\
  108. int i;\
  109. for(i=0; i<h; i++){\
  110. pixel4 a,b;\
  111. a= AV_RN4P(&src1[i*src_stride1 ]);\
  112. b= AV_RN4P(&src2[i*src_stride2 ]);\
  113. OP(*((pixel4*)&dst[i*dst_stride ]), no_rnd_avg_pixel4(a, b));\
  114. a= AV_RN4P(&src1[i*src_stride1+4*sizeof(pixel)]);\
  115. b= AV_RN4P(&src2[i*src_stride2+4*sizeof(pixel)]);\
  116. OP(*((pixel4*)&dst[i*dst_stride+4*sizeof(pixel)]), no_rnd_avg_pixel4(a, b));\
  117. }\
  118. }\
  119. \
  120. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
  121. int src_stride1, int src_stride2, int h){\
  122. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst , src1 , src2 , dst_stride, src_stride1, src_stride2, h);\
  123. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, h);\
  124. }\
  125. \
  126. static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
  127. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  128. /* FIXME HIGH BIT DEPTH */\
  129. int i;\
  130. for(i=0; i<h; i++){\
  131. uint32_t a, b, c, d, l0, l1, h0, h1;\
  132. a= AV_RN32(&src1[i*src_stride1]);\
  133. b= AV_RN32(&src2[i*src_stride2]);\
  134. c= AV_RN32(&src3[i*src_stride3]);\
  135. d= AV_RN32(&src4[i*src_stride4]);\
  136. l0= (a&0x03030303UL)\
  137. + (b&0x03030303UL)\
  138. + 0x02020202UL;\
  139. h0= ((a&0xFCFCFCFCUL)>>2)\
  140. + ((b&0xFCFCFCFCUL)>>2);\
  141. l1= (c&0x03030303UL)\
  142. + (d&0x03030303UL);\
  143. h1= ((c&0xFCFCFCFCUL)>>2)\
  144. + ((d&0xFCFCFCFCUL)>>2);\
  145. OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  146. a= AV_RN32(&src1[i*src_stride1+4]);\
  147. b= AV_RN32(&src2[i*src_stride2+4]);\
  148. c= AV_RN32(&src3[i*src_stride3+4]);\
  149. d= AV_RN32(&src4[i*src_stride4+4]);\
  150. l0= (a&0x03030303UL)\
  151. + (b&0x03030303UL)\
  152. + 0x02020202UL;\
  153. h0= ((a&0xFCFCFCFCUL)>>2)\
  154. + ((b&0xFCFCFCFCUL)>>2);\
  155. l1= (c&0x03030303UL)\
  156. + (d&0x03030303UL);\
  157. h1= ((c&0xFCFCFCFCUL)>>2)\
  158. + ((d&0xFCFCFCFCUL)>>2);\
  159. OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  160. }\
  161. }\
  162. \
  163. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
  164. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  165. /* FIXME HIGH BIT DEPTH*/\
  166. int i;\
  167. for(i=0; i<h; i++){\
  168. uint32_t a, b, c, d, l0, l1, h0, h1;\
  169. a= AV_RN32(&src1[i*src_stride1]);\
  170. b= AV_RN32(&src2[i*src_stride2]);\
  171. c= AV_RN32(&src3[i*src_stride3]);\
  172. d= AV_RN32(&src4[i*src_stride4]);\
  173. l0= (a&0x03030303UL)\
  174. + (b&0x03030303UL)\
  175. + 0x01010101UL;\
  176. h0= ((a&0xFCFCFCFCUL)>>2)\
  177. + ((b&0xFCFCFCFCUL)>>2);\
  178. l1= (c&0x03030303UL)\
  179. + (d&0x03030303UL);\
  180. h1= ((c&0xFCFCFCFCUL)>>2)\
  181. + ((d&0xFCFCFCFCUL)>>2);\
  182. OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  183. a= AV_RN32(&src1[i*src_stride1+4]);\
  184. b= AV_RN32(&src2[i*src_stride2+4]);\
  185. c= AV_RN32(&src3[i*src_stride3+4]);\
  186. d= AV_RN32(&src4[i*src_stride4+4]);\
  187. l0= (a&0x03030303UL)\
  188. + (b&0x03030303UL)\
  189. + 0x01010101UL;\
  190. h0= ((a&0xFCFCFCFCUL)>>2)\
  191. + ((b&0xFCFCFCFCUL)>>2);\
  192. l1= (c&0x03030303UL)\
  193. + (d&0x03030303UL);\
  194. h1= ((c&0xFCFCFCFCUL)>>2)\
  195. + ((d&0xFCFCFCFCUL)>>2);\
  196. OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  197. }\
  198. }\
  199. static inline void FUNC(OPNAME ## _pixels16_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
  200. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  201. FUNC(OPNAME ## _pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  202. FUNC(OPNAME ## _pixels8_l4)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), src3+8*sizeof(pixel), src4+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  203. }\
  204. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
  205. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  206. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  207. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), src3+8*sizeof(pixel), src4+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  208. }\
  209. \
  210. static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
  211. {\
  212. /* FIXME HIGH BIT DEPTH */\
  213. int j;\
  214. for(j=0; j<2; j++){\
  215. int i;\
  216. const uint32_t a= AV_RN32(pixels );\
  217. const uint32_t b= AV_RN32(pixels+1);\
  218. uint32_t l0= (a&0x03030303UL)\
  219. + (b&0x03030303UL)\
  220. + 0x02020202UL;\
  221. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  222. + ((b&0xFCFCFCFCUL)>>2);\
  223. uint32_t l1,h1;\
  224. \
  225. pixels+=line_size;\
  226. for(i=0; i<h; i+=2){\
  227. uint32_t a= AV_RN32(pixels );\
  228. uint32_t b= AV_RN32(pixels+1);\
  229. l1= (a&0x03030303UL)\
  230. + (b&0x03030303UL);\
  231. h1= ((a&0xFCFCFCFCUL)>>2)\
  232. + ((b&0xFCFCFCFCUL)>>2);\
  233. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  234. pixels+=line_size;\
  235. block +=line_size;\
  236. a= AV_RN32(pixels );\
  237. b= AV_RN32(pixels+1);\
  238. l0= (a&0x03030303UL)\
  239. + (b&0x03030303UL)\
  240. + 0x02020202UL;\
  241. h0= ((a&0xFCFCFCFCUL)>>2)\
  242. + ((b&0xFCFCFCFCUL)>>2);\
  243. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  244. pixels+=line_size;\
  245. block +=line_size;\
  246. }\
  247. pixels+=4-line_size*(h+1);\
  248. block +=4-line_size*h;\
  249. }\
  250. }\
  251. \
  252. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), FUNCC(OPNAME ## _pixels8_xy2), 8*sizeof(pixel))\
  253. #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
  254. #define op_put(a, b) a = b
  255. #if BIT_DEPTH == 8
  256. #define put_no_rnd_pixels8_8_c put_pixels8_8_c
  257. PIXOP2(avg, op_avg)
  258. PIXOP2(put, op_put)
  259. #endif
  260. #undef op_avg
  261. #undef op_put