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.

425 lines
18KB

  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 FUNCC(OPNAME ## _no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  127. FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
  128. }\
  129. \
  130. static inline void FUNCC(OPNAME ## _pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  131. FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
  132. }\
  133. \
  134. static inline void FUNCC(OPNAME ## _no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  135. FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
  136. }\
  137. \
  138. static inline void FUNCC(OPNAME ## _pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  139. FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
  140. }\
  141. \
  142. 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,\
  143. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  144. /* FIXME HIGH BIT DEPTH */\
  145. int i;\
  146. for(i=0; i<h; i++){\
  147. uint32_t a, b, c, d, l0, l1, h0, h1;\
  148. a= AV_RN32(&src1[i*src_stride1]);\
  149. b= AV_RN32(&src2[i*src_stride2]);\
  150. c= AV_RN32(&src3[i*src_stride3]);\
  151. d= AV_RN32(&src4[i*src_stride4]);\
  152. l0= (a&0x03030303UL)\
  153. + (b&0x03030303UL)\
  154. + 0x02020202UL;\
  155. h0= ((a&0xFCFCFCFCUL)>>2)\
  156. + ((b&0xFCFCFCFCUL)>>2);\
  157. l1= (c&0x03030303UL)\
  158. + (d&0x03030303UL);\
  159. h1= ((c&0xFCFCFCFCUL)>>2)\
  160. + ((d&0xFCFCFCFCUL)>>2);\
  161. OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  162. a= AV_RN32(&src1[i*src_stride1+4]);\
  163. b= AV_RN32(&src2[i*src_stride2+4]);\
  164. c= AV_RN32(&src3[i*src_stride3+4]);\
  165. d= AV_RN32(&src4[i*src_stride4+4]);\
  166. l0= (a&0x03030303UL)\
  167. + (b&0x03030303UL)\
  168. + 0x02020202UL;\
  169. h0= ((a&0xFCFCFCFCUL)>>2)\
  170. + ((b&0xFCFCFCFCUL)>>2);\
  171. l1= (c&0x03030303UL)\
  172. + (d&0x03030303UL);\
  173. h1= ((c&0xFCFCFCFCUL)>>2)\
  174. + ((d&0xFCFCFCFCUL)>>2);\
  175. OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  176. }\
  177. }\
  178. \
  179. static inline void FUNCC(OPNAME ## _pixels4_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  180. FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
  181. }\
  182. \
  183. static inline void FUNCC(OPNAME ## _pixels4_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  184. FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
  185. }\
  186. \
  187. static inline void FUNCC(OPNAME ## _pixels2_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  188. FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
  189. }\
  190. \
  191. static inline void FUNCC(OPNAME ## _pixels2_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
  192. FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
  193. }\
  194. \
  195. 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,\
  196. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  197. /* FIXME HIGH BIT DEPTH*/\
  198. int i;\
  199. for(i=0; i<h; i++){\
  200. uint32_t a, b, c, d, l0, l1, h0, h1;\
  201. a= AV_RN32(&src1[i*src_stride1]);\
  202. b= AV_RN32(&src2[i*src_stride2]);\
  203. c= AV_RN32(&src3[i*src_stride3]);\
  204. d= AV_RN32(&src4[i*src_stride4]);\
  205. l0= (a&0x03030303UL)\
  206. + (b&0x03030303UL)\
  207. + 0x01010101UL;\
  208. h0= ((a&0xFCFCFCFCUL)>>2)\
  209. + ((b&0xFCFCFCFCUL)>>2);\
  210. l1= (c&0x03030303UL)\
  211. + (d&0x03030303UL);\
  212. h1= ((c&0xFCFCFCFCUL)>>2)\
  213. + ((d&0xFCFCFCFCUL)>>2);\
  214. OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  215. a= AV_RN32(&src1[i*src_stride1+4]);\
  216. b= AV_RN32(&src2[i*src_stride2+4]);\
  217. c= AV_RN32(&src3[i*src_stride3+4]);\
  218. d= AV_RN32(&src4[i*src_stride4+4]);\
  219. l0= (a&0x03030303UL)\
  220. + (b&0x03030303UL)\
  221. + 0x01010101UL;\
  222. h0= ((a&0xFCFCFCFCUL)>>2)\
  223. + ((b&0xFCFCFCFCUL)>>2);\
  224. l1= (c&0x03030303UL)\
  225. + (d&0x03030303UL);\
  226. h1= ((c&0xFCFCFCFCUL)>>2)\
  227. + ((d&0xFCFCFCFCUL)>>2);\
  228. OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  229. }\
  230. }\
  231. 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,\
  232. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  233. FUNC(OPNAME ## _pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  234. 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);\
  235. }\
  236. 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,\
  237. int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
  238. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
  239. 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);\
  240. }\
  241. \
  242. static inline void FUNCC(OPNAME ## _pixels2_xy2)(uint8_t *_block, const uint8_t *_pixels, ptrdiff_t line_size, int h)\
  243. {\
  244. int i, a0, b0, a1, b1;\
  245. pixel *block = (pixel*)_block;\
  246. const pixel *pixels = (const pixel*)_pixels;\
  247. line_size /= sizeof(pixel);\
  248. a0= pixels[0];\
  249. b0= pixels[1] + 2;\
  250. a0 += b0;\
  251. b0 += pixels[2];\
  252. \
  253. pixels+=line_size;\
  254. for(i=0; i<h; i+=2){\
  255. a1= pixels[0];\
  256. b1= pixels[1];\
  257. a1 += b1;\
  258. b1 += pixels[2];\
  259. \
  260. block[0]= (a1+a0)>>2; /* FIXME non put */\
  261. block[1]= (b1+b0)>>2;\
  262. \
  263. pixels+=line_size;\
  264. block +=line_size;\
  265. \
  266. a0= pixels[0];\
  267. b0= pixels[1] + 2;\
  268. a0 += b0;\
  269. b0 += pixels[2];\
  270. \
  271. block[0]= (a1+a0)>>2;\
  272. block[1]= (b1+b0)>>2;\
  273. pixels+=line_size;\
  274. block +=line_size;\
  275. }\
  276. }\
  277. \
  278. static inline void FUNCC(OPNAME ## _pixels4_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
  279. {\
  280. /* FIXME HIGH BIT DEPTH */\
  281. int i;\
  282. const uint32_t a= AV_RN32(pixels );\
  283. const uint32_t b= AV_RN32(pixels+1);\
  284. uint32_t l0= (a&0x03030303UL)\
  285. + (b&0x03030303UL)\
  286. + 0x02020202UL;\
  287. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  288. + ((b&0xFCFCFCFCUL)>>2);\
  289. uint32_t l1,h1;\
  290. \
  291. pixels+=line_size;\
  292. for(i=0; i<h; i+=2){\
  293. uint32_t a= AV_RN32(pixels );\
  294. uint32_t b= AV_RN32(pixels+1);\
  295. l1= (a&0x03030303UL)\
  296. + (b&0x03030303UL);\
  297. h1= ((a&0xFCFCFCFCUL)>>2)\
  298. + ((b&0xFCFCFCFCUL)>>2);\
  299. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  300. pixels+=line_size;\
  301. block +=line_size;\
  302. a= AV_RN32(pixels );\
  303. b= AV_RN32(pixels+1);\
  304. l0= (a&0x03030303UL)\
  305. + (b&0x03030303UL)\
  306. + 0x02020202UL;\
  307. h0= ((a&0xFCFCFCFCUL)>>2)\
  308. + ((b&0xFCFCFCFCUL)>>2);\
  309. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  310. pixels+=line_size;\
  311. block +=line_size;\
  312. }\
  313. }\
  314. \
  315. static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
  316. {\
  317. /* FIXME HIGH BIT DEPTH */\
  318. int j;\
  319. for(j=0; j<2; j++){\
  320. int i;\
  321. const uint32_t a= AV_RN32(pixels );\
  322. const uint32_t b= AV_RN32(pixels+1);\
  323. uint32_t l0= (a&0x03030303UL)\
  324. + (b&0x03030303UL)\
  325. + 0x02020202UL;\
  326. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  327. + ((b&0xFCFCFCFCUL)>>2);\
  328. uint32_t l1,h1;\
  329. \
  330. pixels+=line_size;\
  331. for(i=0; i<h; i+=2){\
  332. uint32_t a= AV_RN32(pixels );\
  333. uint32_t b= AV_RN32(pixels+1);\
  334. l1= (a&0x03030303UL)\
  335. + (b&0x03030303UL);\
  336. h1= ((a&0xFCFCFCFCUL)>>2)\
  337. + ((b&0xFCFCFCFCUL)>>2);\
  338. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  339. pixels+=line_size;\
  340. block +=line_size;\
  341. a= AV_RN32(pixels );\
  342. b= AV_RN32(pixels+1);\
  343. l0= (a&0x03030303UL)\
  344. + (b&0x03030303UL)\
  345. + 0x02020202UL;\
  346. h0= ((a&0xFCFCFCFCUL)>>2)\
  347. + ((b&0xFCFCFCFCUL)>>2);\
  348. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  349. pixels+=line_size;\
  350. block +=line_size;\
  351. }\
  352. pixels+=4-line_size*(h+1);\
  353. block +=4-line_size*h;\
  354. }\
  355. }\
  356. \
  357. static inline void FUNCC(OPNAME ## _no_rnd_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
  358. {\
  359. /* FIXME HIGH BIT DEPTH */\
  360. int j;\
  361. for(j=0; j<2; j++){\
  362. int i;\
  363. const uint32_t a= AV_RN32(pixels );\
  364. const uint32_t b= AV_RN32(pixels+1);\
  365. uint32_t l0= (a&0x03030303UL)\
  366. + (b&0x03030303UL)\
  367. + 0x01010101UL;\
  368. uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
  369. + ((b&0xFCFCFCFCUL)>>2);\
  370. uint32_t l1,h1;\
  371. \
  372. pixels+=line_size;\
  373. for(i=0; i<h; i+=2){\
  374. uint32_t a= AV_RN32(pixels );\
  375. uint32_t b= AV_RN32(pixels+1);\
  376. l1= (a&0x03030303UL)\
  377. + (b&0x03030303UL);\
  378. h1= ((a&0xFCFCFCFCUL)>>2)\
  379. + ((b&0xFCFCFCFCUL)>>2);\
  380. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  381. pixels+=line_size;\
  382. block +=line_size;\
  383. a= AV_RN32(pixels );\
  384. b= AV_RN32(pixels+1);\
  385. l0= (a&0x03030303UL)\
  386. + (b&0x03030303UL)\
  387. + 0x01010101UL;\
  388. h0= ((a&0xFCFCFCFCUL)>>2)\
  389. + ((b&0xFCFCFCFCUL)>>2);\
  390. OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
  391. pixels+=line_size;\
  392. block +=line_size;\
  393. }\
  394. pixels+=4-line_size*(h+1);\
  395. block +=4-line_size*h;\
  396. }\
  397. }\
  398. \
  399. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_x2) , FUNCC(OPNAME ## _pixels8_x2) , 8*sizeof(pixel))\
  400. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_y2) , FUNCC(OPNAME ## _pixels8_y2) , 8*sizeof(pixel))\
  401. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), FUNCC(OPNAME ## _pixels8_xy2), 8*sizeof(pixel))\
  402. av_unused CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16) , FUNCC(OPNAME ## _pixels8) , 8*sizeof(pixel))\
  403. CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_x2) , FUNCC(OPNAME ## _no_rnd_pixels8_x2) , 8*sizeof(pixel))\
  404. CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_y2) , FUNCC(OPNAME ## _no_rnd_pixels8_y2) , 8*sizeof(pixel))\
  405. CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_xy2), FUNCC(OPNAME ## _no_rnd_pixels8_xy2), 8*sizeof(pixel))\
  406. #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
  407. #define op_put(a, b) a = b
  408. #if BIT_DEPTH == 8
  409. #define put_no_rnd_pixels8_8_c put_pixels8_8_c
  410. PIXOP2(avg, op_avg)
  411. PIXOP2(put, op_put)
  412. #endif
  413. #undef op_avg
  414. #undef op_put