Merge "documentation: minor cosmetics"
[libvpx.git] / vp8 / common / filter.c
blob6e364a94d4838bc7fb6ccfce9a697550068abe5e
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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 */
12 #include <stdlib.h>
13 #include "filter.h"
14 #include "vpx_ports/mem.h"
16 DECLARE_ALIGNED(16, const short, vp8_bilinear_filters[8][2]) =
18 { 128, 0 },
19 { 112, 16 },
20 { 96, 32 },
21 { 80, 48 },
22 { 64, 64 },
23 { 48, 80 },
24 { 32, 96 },
25 { 16, 112 }
28 DECLARE_ALIGNED(16, const short, vp8_sub_pel_filters[8][6]) =
31 { 0, 0, 128, 0, 0, 0 }, /* note that 1/8 pel positions are just as per alpha -0.5 bicubic */
32 { 0, -6, 123, 12, -1, 0 },
33 { 2, -11, 108, 36, -8, 1 }, /* New 1/4 pel 6 tap filter */
34 { 0, -9, 93, 50, -6, 0 },
35 { 3, -16, 77, 77, -16, 3 }, /* New 1/2 pel 6 tap filter */
36 { 0, -6, 50, 93, -9, 0 },
37 { 1, -8, 36, 108, -11, 2 }, /* New 1/4 pel 6 tap filter */
38 { 0, -1, 12, 123, -6, 0 },
41 void vp8_filter_block2d_first_pass
43 unsigned char *src_ptr,
44 int *output_ptr,
45 unsigned int src_pixels_per_line,
46 unsigned int pixel_step,
47 unsigned int output_height,
48 unsigned int output_width,
49 const short *vp8_filter
52 unsigned int i, j;
53 int Temp;
55 for (i = 0; i < output_height; i++)
57 for (j = 0; j < output_width; j++)
59 Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
60 ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
61 ((int)src_ptr[0] * vp8_filter[2]) +
62 ((int)src_ptr[pixel_step] * vp8_filter[3]) +
63 ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
64 ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
65 (VP8_FILTER_WEIGHT >> 1); /* Rounding */
67 /* Normalize back to 0-255 */
68 Temp = Temp >> VP8_FILTER_SHIFT;
70 if (Temp < 0)
71 Temp = 0;
72 else if (Temp > 255)
73 Temp = 255;
75 output_ptr[j] = Temp;
76 src_ptr++;
79 /* Next row... */
80 src_ptr += src_pixels_per_line - output_width;
81 output_ptr += output_width;
85 void vp8_filter_block2d_second_pass
87 int *src_ptr,
88 unsigned char *output_ptr,
89 int output_pitch,
90 unsigned int src_pixels_per_line,
91 unsigned int pixel_step,
92 unsigned int output_height,
93 unsigned int output_width,
94 const short *vp8_filter
97 unsigned int i, j;
98 int Temp;
100 for (i = 0; i < output_height; i++)
102 for (j = 0; j < output_width; j++)
104 /* Apply filter */
105 Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
106 ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
107 ((int)src_ptr[0] * vp8_filter[2]) +
108 ((int)src_ptr[pixel_step] * vp8_filter[3]) +
109 ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
110 ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
111 (VP8_FILTER_WEIGHT >> 1); /* Rounding */
113 /* Normalize back to 0-255 */
114 Temp = Temp >> VP8_FILTER_SHIFT;
116 if (Temp < 0)
117 Temp = 0;
118 else if (Temp > 255)
119 Temp = 255;
121 output_ptr[j] = (unsigned char)Temp;
122 src_ptr++;
125 /* Start next row */
126 src_ptr += src_pixels_per_line - output_width;
127 output_ptr += output_pitch;
132 void vp8_filter_block2d
134 unsigned char *src_ptr,
135 unsigned char *output_ptr,
136 unsigned int src_pixels_per_line,
137 int output_pitch,
138 const short *HFilter,
139 const short *VFilter
142 int FData[9*4]; /* Temp data buffer used in filtering */
144 /* First filter 1-D horizontally... */
145 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 4, HFilter);
147 /* then filter verticaly... */
148 vp8_filter_block2d_second_pass(FData + 8, output_ptr, output_pitch, 4, 4, 4, 4, VFilter);
152 void vp8_block_variation_c
154 unsigned char *src_ptr,
155 int src_pixels_per_line,
156 int *HVar,
157 int *VVar
160 int i, j;
161 unsigned char *Ptr = src_ptr;
163 for (i = 0; i < 4; i++)
165 for (j = 0; j < 4; j++)
167 *HVar += abs((int)Ptr[j] - (int)Ptr[j+1]);
168 *VVar += abs((int)Ptr[j] - (int)Ptr[j+src_pixels_per_line]);
171 Ptr += src_pixels_per_line;
178 void vp8_sixtap_predict_c
180 unsigned char *src_ptr,
181 int src_pixels_per_line,
182 int xoffset,
183 int yoffset,
184 unsigned char *dst_ptr,
185 int dst_pitch
188 const short *HFilter;
189 const short *VFilter;
191 HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
192 VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
194 vp8_filter_block2d(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter);
196 void vp8_sixtap_predict8x8_c
198 unsigned char *src_ptr,
199 int src_pixels_per_line,
200 int xoffset,
201 int yoffset,
202 unsigned char *dst_ptr,
203 int dst_pitch
206 const short *HFilter;
207 const short *VFilter;
208 int FData[13*16]; /* Temp data buffer used in filtering */
210 HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
211 VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
213 /* First filter 1-D horizontally... */
214 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 13, 8, HFilter);
217 /* then filter verticaly... */
218 vp8_filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 8, 8, VFilter);
222 void vp8_sixtap_predict8x4_c
224 unsigned char *src_ptr,
225 int src_pixels_per_line,
226 int xoffset,
227 int yoffset,
228 unsigned char *dst_ptr,
229 int dst_pitch
232 const short *HFilter;
233 const short *VFilter;
234 int FData[13*16]; /* Temp data buffer used in filtering */
236 HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
237 VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
239 /* First filter 1-D horizontally... */
240 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 8, HFilter);
243 /* then filter verticaly... */
244 vp8_filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 4, 8, VFilter);
248 void vp8_sixtap_predict16x16_c
250 unsigned char *src_ptr,
251 int src_pixels_per_line,
252 int xoffset,
253 int yoffset,
254 unsigned char *dst_ptr,
255 int dst_pitch
258 const short *HFilter;
259 const short *VFilter;
260 int FData[21*24]; /* Temp data buffer used in filtering */
263 HFilter = vp8_sub_pel_filters[xoffset]; /* 6 tap */
264 VFilter = vp8_sub_pel_filters[yoffset]; /* 6 tap */
266 /* First filter 1-D horizontally... */
267 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 21, 16, HFilter);
269 /* then filter verticaly... */
270 vp8_filter_block2d_second_pass(FData + 32, dst_ptr, dst_pitch, 16, 16, 16, 16, VFilter);
275 /****************************************************************************
277 * ROUTINE : filter_block2d_bil_first_pass
279 * INPUTS : UINT8 *src_ptr : Pointer to source block.
280 * UINT32 src_stride : Stride of source block.
281 * UINT32 height : Block height.
282 * UINT32 width : Block width.
283 * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
285 * OUTPUTS : INT32 *dst_ptr : Pointer to filtered block.
287 * RETURNS : void
289 * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block
290 * in the horizontal direction to produce the filtered output
291 * block. Used to implement first-pass of 2-D separable filter.
293 * SPECIAL NOTES : Produces INT32 output to retain precision for next pass.
294 * Two filter taps should sum to VP8_FILTER_WEIGHT.
296 ****************************************************************************/
297 void vp8_filter_block2d_bil_first_pass
299 unsigned char *src_ptr,
300 unsigned short *dst_ptr,
301 unsigned int src_stride,
302 unsigned int height,
303 unsigned int width,
304 const short *vp8_filter
307 unsigned int i, j;
309 for (i = 0; i < height; i++)
311 for (j = 0; j < width; j++)
313 /* Apply bilinear filter */
314 dst_ptr[j] = (((int)src_ptr[0] * vp8_filter[0]) +
315 ((int)src_ptr[1] * vp8_filter[1]) +
316 (VP8_FILTER_WEIGHT / 2)) >> VP8_FILTER_SHIFT;
317 src_ptr++;
320 /* Next row... */
321 src_ptr += src_stride - width;
322 dst_ptr += width;
326 /****************************************************************************
328 * ROUTINE : filter_block2d_bil_second_pass
330 * INPUTS : INT32 *src_ptr : Pointer to source block.
331 * UINT32 dst_pitch : Destination block pitch.
332 * UINT32 height : Block height.
333 * UINT32 width : Block width.
334 * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
336 * OUTPUTS : UINT16 *dst_ptr : Pointer to filtered block.
338 * RETURNS : void
340 * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block
341 * in the vertical direction to produce the filtered output
342 * block. Used to implement second-pass of 2-D separable filter.
344 * SPECIAL NOTES : Requires 32-bit input as produced by filter_block2d_bil_first_pass.
345 * Two filter taps should sum to VP8_FILTER_WEIGHT.
347 ****************************************************************************/
348 void vp8_filter_block2d_bil_second_pass
350 unsigned short *src_ptr,
351 unsigned char *dst_ptr,
352 int dst_pitch,
353 unsigned int height,
354 unsigned int width,
355 const short *vp8_filter
358 unsigned int i, j;
359 int Temp;
361 for (i = 0; i < height; i++)
363 for (j = 0; j < width; j++)
365 /* Apply filter */
366 Temp = ((int)src_ptr[0] * vp8_filter[0]) +
367 ((int)src_ptr[width] * vp8_filter[1]) +
368 (VP8_FILTER_WEIGHT / 2);
369 dst_ptr[j] = (unsigned int)(Temp >> VP8_FILTER_SHIFT);
370 src_ptr++;
373 /* Next row... */
374 dst_ptr += dst_pitch;
379 /****************************************************************************
381 * ROUTINE : filter_block2d_bil
383 * INPUTS : UINT8 *src_ptr : Pointer to source block.
384 * UINT32 src_pitch : Stride of source block.
385 * UINT32 dst_pitch : Stride of destination block.
386 * INT32 *HFilter : Array of 2 horizontal filter taps.
387 * INT32 *VFilter : Array of 2 vertical filter taps.
388 * INT32 Width : Block width
389 * INT32 Height : Block height
391 * OUTPUTS : UINT16 *dst_ptr : Pointer to filtered block.
393 * RETURNS : void
395 * FUNCTION : 2-D filters an input block by applying a 2-tap
396 * bi-linear filter horizontally followed by a 2-tap
397 * bi-linear filter vertically on the result.
399 * SPECIAL NOTES : The largest block size can be handled here is 16x16
401 ****************************************************************************/
402 void vp8_filter_block2d_bil
404 unsigned char *src_ptr,
405 unsigned char *dst_ptr,
406 unsigned int src_pitch,
407 unsigned int dst_pitch,
408 const short *HFilter,
409 const short *VFilter,
410 int Width,
411 int Height
415 unsigned short FData[17*16]; /* Temp data buffer used in filtering */
417 /* First filter 1-D horizontally... */
418 vp8_filter_block2d_bil_first_pass(src_ptr, FData, src_pitch, Height + 1, Width, HFilter);
420 /* then 1-D vertically... */
421 vp8_filter_block2d_bil_second_pass(FData, dst_ptr, dst_pitch, Height, Width, VFilter);
425 void vp8_bilinear_predict4x4_c
427 unsigned char *src_ptr,
428 int src_pixels_per_line,
429 int xoffset,
430 int yoffset,
431 unsigned char *dst_ptr,
432 int dst_pitch
435 const short *HFilter;
436 const short *VFilter;
438 HFilter = vp8_bilinear_filters[xoffset];
439 VFilter = vp8_bilinear_filters[yoffset];
440 #if 0
442 int i;
443 unsigned char temp1[16];
444 unsigned char temp2[16];
446 bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
447 vp8_filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
449 for (i = 0; i < 16; i++)
451 if (temp1[i] != temp2[i])
453 bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
454 vp8_filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
458 #endif
459 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 4, 4);
463 void vp8_bilinear_predict8x8_c
465 unsigned char *src_ptr,
466 int src_pixels_per_line,
467 int xoffset,
468 int yoffset,
469 unsigned char *dst_ptr,
470 int dst_pitch
473 const short *HFilter;
474 const short *VFilter;
476 HFilter = vp8_bilinear_filters[xoffset];
477 VFilter = vp8_bilinear_filters[yoffset];
479 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 8);
483 void vp8_bilinear_predict8x4_c
485 unsigned char *src_ptr,
486 int src_pixels_per_line,
487 int xoffset,
488 int yoffset,
489 unsigned char *dst_ptr,
490 int dst_pitch
493 const short *HFilter;
494 const short *VFilter;
496 HFilter = vp8_bilinear_filters[xoffset];
497 VFilter = vp8_bilinear_filters[yoffset];
499 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 4);
503 void vp8_bilinear_predict16x16_c
505 unsigned char *src_ptr,
506 int src_pixels_per_line,
507 int xoffset,
508 int yoffset,
509 unsigned char *dst_ptr,
510 int dst_pitch
513 const short *HFilter;
514 const short *VFilter;
516 HFilter = vp8_bilinear_filters[xoffset];
517 VFilter = vp8_bilinear_filters[yoffset];
519 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 16, 16);