/media/libvpx/vp8/common/filter.c

http://github.com/zpao/v8monkey · C · 494 lines · 328 code · 71 blank · 95 comment · 17 complexity · aa70ab04d16a462cf33152898e64000a MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdlib.h>
  11. #include "filter.h"
  12. #include "vpx_ports/mem.h"
  13. DECLARE_ALIGNED(16, const short, vp8_bilinear_filters[8][2]) =
  14. {
  15. { 128, 0 },
  16. { 112, 16 },
  17. { 96, 32 },
  18. { 80, 48 },
  19. { 64, 64 },
  20. { 48, 80 },
  21. { 32, 96 },
  22. { 16, 112 }
  23. };
  24. DECLARE_ALIGNED(16, const short, vp8_sub_pel_filters[8][6]) =
  25. {
  26. { 0, 0, 128, 0, 0, 0 }, /* note that 1/8 pel positions are just as per alpha -0.5 bicubic */
  27. { 0, -6, 123, 12, -1, 0 },
  28. { 2, -11, 108, 36, -8, 1 }, /* New 1/4 pel 6 tap filter */
  29. { 0, -9, 93, 50, -6, 0 },
  30. { 3, -16, 77, 77, -16, 3 }, /* New 1/2 pel 6 tap filter */
  31. { 0, -6, 50, 93, -9, 0 },
  32. { 1, -8, 36, 108, -11, 2 }, /* New 1/4 pel 6 tap filter */
  33. { 0, -1, 12, 123, -6, 0 },
  34. };
  35. static void filter_block2d_first_pass
  36. (
  37. unsigned char *src_ptr,
  38. int *output_ptr,
  39. unsigned int src_pixels_per_line,
  40. unsigned int pixel_step,
  41. unsigned int output_height,
  42. unsigned int output_width,
  43. const short *vp8_filter
  44. )
  45. {
  46. unsigned int i, j;
  47. int Temp;
  48. for (i = 0; i < output_height; i++)
  49. {
  50. for (j = 0; j < output_width; j++)
  51. {
  52. Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
  53. ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
  54. ((int)src_ptr[0] * vp8_filter[2]) +
  55. ((int)src_ptr[pixel_step] * vp8_filter[3]) +
  56. ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
  57. ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
  58. (VP8_FILTER_WEIGHT >> 1); /* Rounding */
  59. /* Normalize back to 0-255 */
  60. Temp = Temp >> VP8_FILTER_SHIFT;
  61. if (Temp < 0)
  62. Temp = 0;
  63. else if (Temp > 255)
  64. Temp = 255;
  65. output_ptr[j] = Temp;
  66. src_ptr++;
  67. }
  68. /* Next row... */
  69. src_ptr += src_pixels_per_line - output_width;
  70. output_ptr += output_width;
  71. }
  72. }
  73. static void filter_block2d_second_pass
  74. (
  75. int *src_ptr,
  76. unsigned char *output_ptr,
  77. int output_pitch,
  78. unsigned int src_pixels_per_line,
  79. unsigned int pixel_step,
  80. unsigned int output_height,
  81. unsigned int output_width,
  82. const short *vp8_filter
  83. )
  84. {
  85. unsigned int i, j;
  86. int Temp;
  87. for (i = 0; i < output_height; i++)
  88. {
  89. for (j = 0; j < output_width; j++)
  90. {
  91. /* Apply filter */
  92. Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
  93. ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
  94. ((int)src_ptr[0] * vp8_filter[2]) +
  95. ((int)src_ptr[pixel_step] * vp8_filter[3]) +
  96. ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
  97. ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
  98. (VP8_FILTER_WEIGHT >> 1); /* Rounding */
  99. /* Normalize back to 0-255 */
  100. Temp = Temp >> VP8_FILTER_SHIFT;
  101. if (Temp < 0)
  102. Temp = 0;
  103. else if (Temp > 255)
  104. Temp = 255;
  105. output_ptr[j] = (unsigned char)Temp;
  106. src_ptr++;
  107. }
  108. /* Start next row */
  109. src_ptr += src_pixels_per_line - output_width;
  110. output_ptr += output_pitch;
  111. }
  112. }
  113. static void filter_block2d
  114. (
  115. unsigned char *src_ptr,
  116. unsigned char *output_ptr,
  117. unsigned int src_pixels_per_line,
  118. int output_pitch,
  119. const short *HFilter,
  120. const short *VFilter
  121. )
  122. {
  123. int FData[9*4]; /* Temp data buffer used in filtering */
  124. /* First filter 1-D horizontally... */
  125. filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 4, HFilter);
  126. /* then filter verticaly... */
  127. filter_block2d_second_pass(FData + 8, output_ptr, output_pitch, 4, 4, 4, 4, VFilter);
  128. }
  129. void vp8_sixtap_predict_c
  130. (
  131. unsigned char *src_ptr,
  132. int src_pixels_per_line,
  133. int xoffset,
  134. int yoffset,
  135. unsigned char *dst_ptr,
  136. int dst_pitch
  137. )
  138. {
  139. const short *HFilter;
  140. const short *VFilter;
  141. HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
  142. VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
  143. filter_block2d(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter);
  144. }
  145. void vp8_sixtap_predict8x8_c
  146. (
  147. unsigned char *src_ptr,
  148. int src_pixels_per_line,
  149. int xoffset,
  150. int yoffset,
  151. unsigned char *dst_ptr,
  152. int dst_pitch
  153. )
  154. {
  155. const short *HFilter;
  156. const short *VFilter;
  157. int FData[13*16]; /* Temp data buffer used in filtering */
  158. HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
  159. VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
  160. /* First filter 1-D horizontally... */
  161. filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 13, 8, HFilter);
  162. /* then filter verticaly... */
  163. filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 8, 8, VFilter);
  164. }
  165. void vp8_sixtap_predict8x4_c
  166. (
  167. unsigned char *src_ptr,
  168. int src_pixels_per_line,
  169. int xoffset,
  170. int yoffset,
  171. unsigned char *dst_ptr,
  172. int dst_pitch
  173. )
  174. {
  175. const short *HFilter;
  176. const short *VFilter;
  177. int FData[13*16]; /* Temp data buffer used in filtering */
  178. HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
  179. VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
  180. /* First filter 1-D horizontally... */
  181. filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 8, HFilter);
  182. /* then filter verticaly... */
  183. filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 4, 8, VFilter);
  184. }
  185. void vp8_sixtap_predict16x16_c
  186. (
  187. unsigned char *src_ptr,
  188. int src_pixels_per_line,
  189. int xoffset,
  190. int yoffset,
  191. unsigned char *dst_ptr,
  192. int dst_pitch
  193. )
  194. {
  195. const short *HFilter;
  196. const short *VFilter;
  197. int FData[21*24]; /* Temp data buffer used in filtering */
  198. HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
  199. VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
  200. /* First filter 1-D horizontally... */
  201. filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 21, 16, HFilter);
  202. /* then filter verticaly... */
  203. filter_block2d_second_pass(FData + 32, dst_ptr, dst_pitch, 16, 16, 16, 16, VFilter);
  204. }
  205. /****************************************************************************
  206. *
  207. * ROUTINE : filter_block2d_bil_first_pass
  208. *
  209. * INPUTS : UINT8 *src_ptr : Pointer to source block.
  210. * UINT32 src_stride : Stride of source block.
  211. * UINT32 height : Block height.
  212. * UINT32 width : Block width.
  213. * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
  214. *
  215. * OUTPUTS : INT32 *dst_ptr : Pointer to filtered block.
  216. *
  217. * RETURNS : void
  218. *
  219. * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block
  220. * in the horizontal direction to produce the filtered output
  221. * block. Used to implement first-pass of 2-D separable filter.
  222. *
  223. * SPECIAL NOTES : Produces INT32 output to retain precision for next pass.
  224. * Two filter taps should sum to VP8_FILTER_WEIGHT.
  225. *
  226. ****************************************************************************/
  227. static void filter_block2d_bil_first_pass
  228. (
  229. unsigned char *src_ptr,
  230. unsigned short *dst_ptr,
  231. unsigned int src_stride,
  232. unsigned int height,
  233. unsigned int width,
  234. const short *vp8_filter
  235. )
  236. {
  237. unsigned int i, j;
  238. for (i = 0; i < height; i++)
  239. {
  240. for (j = 0; j < width; j++)
  241. {
  242. /* Apply bilinear filter */
  243. dst_ptr[j] = (((int)src_ptr[0] * vp8_filter[0]) +
  244. ((int)src_ptr[1] * vp8_filter[1]) +
  245. (VP8_FILTER_WEIGHT / 2)) >> VP8_FILTER_SHIFT;
  246. src_ptr++;
  247. }
  248. /* Next row... */
  249. src_ptr += src_stride - width;
  250. dst_ptr += width;
  251. }
  252. }
  253. /****************************************************************************
  254. *
  255. * ROUTINE : filter_block2d_bil_second_pass
  256. *
  257. * INPUTS : INT32 *src_ptr : Pointer to source block.
  258. * UINT32 dst_pitch : Destination block pitch.
  259. * UINT32 height : Block height.
  260. * UINT32 width : Block width.
  261. * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
  262. *
  263. * OUTPUTS : UINT16 *dst_ptr : Pointer to filtered block.
  264. *
  265. * RETURNS : void
  266. *
  267. * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block
  268. * in the vertical direction to produce the filtered output
  269. * block. Used to implement second-pass of 2-D separable filter.
  270. *
  271. * SPECIAL NOTES : Requires 32-bit input as produced by filter_block2d_bil_first_pass.
  272. * Two filter taps should sum to VP8_FILTER_WEIGHT.
  273. *
  274. ****************************************************************************/
  275. static void filter_block2d_bil_second_pass
  276. (
  277. unsigned short *src_ptr,
  278. unsigned char *dst_ptr,
  279. int dst_pitch,
  280. unsigned int height,
  281. unsigned int width,
  282. const short *vp8_filter
  283. )
  284. {
  285. unsigned int i, j;
  286. int Temp;
  287. for (i = 0; i < height; i++)
  288. {
  289. for (j = 0; j < width; j++)
  290. {
  291. /* Apply filter */
  292. Temp = ((int)src_ptr[0] * vp8_filter[0]) +
  293. ((int)src_ptr[width] * vp8_filter[1]) +
  294. (VP8_FILTER_WEIGHT / 2);
  295. dst_ptr[j] = (unsigned int)(Temp >> VP8_FILTER_SHIFT);
  296. src_ptr++;
  297. }
  298. /* Next row... */
  299. dst_ptr += dst_pitch;
  300. }
  301. }
  302. /****************************************************************************
  303. *
  304. * ROUTINE : filter_block2d_bil
  305. *
  306. * INPUTS : UINT8 *src_ptr : Pointer to source block.
  307. * UINT32 src_pitch : Stride of source block.
  308. * UINT32 dst_pitch : Stride of destination block.
  309. * INT32 *HFilter : Array of 2 horizontal filter taps.
  310. * INT32 *VFilter : Array of 2 vertical filter taps.
  311. * INT32 Width : Block width
  312. * INT32 Height : Block height
  313. *
  314. * OUTPUTS : UINT16 *dst_ptr : Pointer to filtered block.
  315. *
  316. * RETURNS : void
  317. *
  318. * FUNCTION : 2-D filters an input block by applying a 2-tap
  319. * bi-linear filter horizontally followed by a 2-tap
  320. * bi-linear filter vertically on the result.
  321. *
  322. * SPECIAL NOTES : The largest block size can be handled here is 16x16
  323. *
  324. ****************************************************************************/
  325. static void filter_block2d_bil
  326. (
  327. unsigned char *src_ptr,
  328. unsigned char *dst_ptr,
  329. unsigned int src_pitch,
  330. unsigned int dst_pitch,
  331. const short *HFilter,
  332. const short *VFilter,
  333. int Width,
  334. int Height
  335. )
  336. {
  337. unsigned short FData[17*16]; /* Temp data buffer used in filtering */
  338. /* First filter 1-D horizontally... */
  339. filter_block2d_bil_first_pass(src_ptr, FData, src_pitch, Height + 1, Width, HFilter);
  340. /* then 1-D vertically... */
  341. filter_block2d_bil_second_pass(FData, dst_ptr, dst_pitch, Height, Width, VFilter);
  342. }
  343. void vp8_bilinear_predict4x4_c
  344. (
  345. unsigned char *src_ptr,
  346. int src_pixels_per_line,
  347. int xoffset,
  348. int yoffset,
  349. unsigned char *dst_ptr,
  350. int dst_pitch
  351. )
  352. {
  353. const short *HFilter;
  354. const short *VFilter;
  355. HFilter = vp8_bilinear_filters[xoffset];
  356. VFilter = vp8_bilinear_filters[yoffset];
  357. #if 0
  358. {
  359. int i;
  360. unsigned char temp1[16];
  361. unsigned char temp2[16];
  362. bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
  363. filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
  364. for (i = 0; i < 16; i++)
  365. {
  366. if (temp1[i] != temp2[i])
  367. {
  368. bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
  369. filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
  370. }
  371. }
  372. }
  373. #endif
  374. filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 4, 4);
  375. }
  376. void vp8_bilinear_predict8x8_c
  377. (
  378. unsigned char *src_ptr,
  379. int src_pixels_per_line,
  380. int xoffset,
  381. int yoffset,
  382. unsigned char *dst_ptr,
  383. int dst_pitch
  384. )
  385. {
  386. const short *HFilter;
  387. const short *VFilter;
  388. HFilter = vp8_bilinear_filters[xoffset];
  389. VFilter = vp8_bilinear_filters[yoffset];
  390. filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 8);
  391. }
  392. void vp8_bilinear_predict8x4_c
  393. (
  394. unsigned char *src_ptr,
  395. int src_pixels_per_line,
  396. int xoffset,
  397. int yoffset,
  398. unsigned char *dst_ptr,
  399. int dst_pitch
  400. )
  401. {
  402. const short *HFilter;
  403. const short *VFilter;
  404. HFilter = vp8_bilinear_filters[xoffset];
  405. VFilter = vp8_bilinear_filters[yoffset];
  406. filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 4);
  407. }
  408. void vp8_bilinear_predict16x16_c
  409. (
  410. unsigned char *src_ptr,
  411. int src_pixels_per_line,
  412. int xoffset,
  413. int yoffset,
  414. unsigned char *dst_ptr,
  415. int dst_pitch
  416. )
  417. {
  418. const short *HFilter;
  419. const short *VFilter;
  420. HFilter = vp8_bilinear_filters[xoffset];
  421. VFilter = vp8_bilinear_filters[yoffset];
  422. filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 16, 16);
  423. }