Merge "vp8_rd_pick_best_mbsegmentation code restructure"
[libvpx.git] / vp8 / common / filter_c.c
blob399a847d54ce9c4752a4f202817478d02d71b689
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>
14 #define BLOCK_HEIGHT_WIDTH 4
15 #define VP8_FILTER_WEIGHT 128
16 #define VP8_FILTER_SHIFT 7
19 static const int bilinear_filters[8][2] =
21 { 128, 0 },
22 { 112, 16 },
23 { 96, 32 },
24 { 80, 48 },
25 { 64, 64 },
26 { 48, 80 },
27 { 32, 96 },
28 { 16, 112 }
32 static const short sub_pel_filters[8][6] =
35 { 0, 0, 128, 0, 0, 0 }, /* note that 1/8 pel positions are just as per alpha -0.5 bicubic */
36 { 0, -6, 123, 12, -1, 0 },
37 { 2, -11, 108, 36, -8, 1 }, /* New 1/4 pel 6 tap filter */
38 { 0, -9, 93, 50, -6, 0 },
39 { 3, -16, 77, 77, -16, 3 }, /* New 1/2 pel 6 tap filter */
40 { 0, -6, 50, 93, -9, 0 },
41 { 1, -8, 36, 108, -11, 2 }, /* New 1/4 pel 6 tap filter */
42 { 0, -1, 12, 123, -6, 0 },
48 void vp8_filter_block2d_first_pass
50 unsigned char *src_ptr,
51 int *output_ptr,
52 unsigned int src_pixels_per_line,
53 unsigned int pixel_step,
54 unsigned int output_height,
55 unsigned int output_width,
56 const short *vp8_filter
59 unsigned int i, j;
60 int Temp;
62 for (i = 0; i < output_height; i++)
64 for (j = 0; j < output_width; j++)
66 Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
67 ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
68 ((int)src_ptr[0] * vp8_filter[2]) +
69 ((int)src_ptr[pixel_step] * vp8_filter[3]) +
70 ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
71 ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
72 (VP8_FILTER_WEIGHT >> 1); /* Rounding */
74 /* Normalize back to 0-255 */
75 Temp = Temp >> VP8_FILTER_SHIFT;
77 if (Temp < 0)
78 Temp = 0;
79 else if (Temp > 255)
80 Temp = 255;
82 output_ptr[j] = Temp;
83 src_ptr++;
86 /* Next row... */
87 src_ptr += src_pixels_per_line - output_width;
88 output_ptr += output_width;
92 void vp8_filter_block2d_second_pass
94 int *src_ptr,
95 unsigned char *output_ptr,
96 int output_pitch,
97 unsigned int src_pixels_per_line,
98 unsigned int pixel_step,
99 unsigned int output_height,
100 unsigned int output_width,
101 const short *vp8_filter
104 unsigned int i, j;
105 int Temp;
107 for (i = 0; i < output_height; i++)
109 for (j = 0; j < output_width; j++)
111 /* Apply filter */
112 Temp = ((int)src_ptr[-2 * (int)pixel_step] * vp8_filter[0]) +
113 ((int)src_ptr[-1 * (int)pixel_step] * vp8_filter[1]) +
114 ((int)src_ptr[0] * vp8_filter[2]) +
115 ((int)src_ptr[pixel_step] * vp8_filter[3]) +
116 ((int)src_ptr[2*pixel_step] * vp8_filter[4]) +
117 ((int)src_ptr[3*pixel_step] * vp8_filter[5]) +
118 (VP8_FILTER_WEIGHT >> 1); /* Rounding */
120 /* Normalize back to 0-255 */
121 Temp = Temp >> VP8_FILTER_SHIFT;
123 if (Temp < 0)
124 Temp = 0;
125 else if (Temp > 255)
126 Temp = 255;
128 output_ptr[j] = (unsigned char)Temp;
129 src_ptr++;
132 /* Start next row */
133 src_ptr += src_pixels_per_line - output_width;
134 output_ptr += output_pitch;
139 void vp8_filter_block2d
141 unsigned char *src_ptr,
142 unsigned char *output_ptr,
143 unsigned int src_pixels_per_line,
144 int output_pitch,
145 const short *HFilter,
146 const short *VFilter
149 int FData[9*4]; /* Temp data bufffer used in filtering */
151 /* First filter 1-D horizontally... */
152 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 4, HFilter);
154 /* then filter verticaly... */
155 vp8_filter_block2d_second_pass(FData + 8, output_ptr, output_pitch, 4, 4, 4, 4, VFilter);
159 void vp8_block_variation_c
161 unsigned char *src_ptr,
162 int src_pixels_per_line,
163 int *HVar,
164 int *VVar
167 int i, j;
168 unsigned char *Ptr = src_ptr;
170 for (i = 0; i < 4; i++)
172 for (j = 0; j < 4; j++)
174 *HVar += abs((int)Ptr[j] - (int)Ptr[j+1]);
175 *VVar += abs((int)Ptr[j] - (int)Ptr[j+src_pixels_per_line]);
178 Ptr += src_pixels_per_line;
185 void vp8_sixtap_predict_c
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
195 const short *HFilter;
196 const short *VFilter;
198 HFilter = sub_pel_filters[xoffset]; /* 6 tap */
199 VFilter = sub_pel_filters[yoffset]; /* 6 tap */
201 vp8_filter_block2d(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter);
203 void vp8_sixtap_predict8x8_c
205 unsigned char *src_ptr,
206 int src_pixels_per_line,
207 int xoffset,
208 int yoffset,
209 unsigned char *dst_ptr,
210 int dst_pitch
213 const short *HFilter;
214 const short *VFilter;
215 int FData[13*16]; /* Temp data bufffer used in filtering */
217 HFilter = sub_pel_filters[xoffset]; /* 6 tap */
218 VFilter = sub_pel_filters[yoffset]; /* 6 tap */
220 /* First filter 1-D horizontally... */
221 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 13, 8, HFilter);
224 /* then filter verticaly... */
225 vp8_filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 8, 8, VFilter);
229 void vp8_sixtap_predict8x4_c
231 unsigned char *src_ptr,
232 int src_pixels_per_line,
233 int xoffset,
234 int yoffset,
235 unsigned char *dst_ptr,
236 int dst_pitch
239 const short *HFilter;
240 const short *VFilter;
241 int FData[13*16]; /* Temp data bufffer used in filtering */
243 HFilter = sub_pel_filters[xoffset]; /* 6 tap */
244 VFilter = sub_pel_filters[yoffset]; /* 6 tap */
246 /* First filter 1-D horizontally... */
247 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 9, 8, HFilter);
250 /* then filter verticaly... */
251 vp8_filter_block2d_second_pass(FData + 16, dst_ptr, dst_pitch, 8, 8, 4, 8, VFilter);
255 void vp8_sixtap_predict16x16_c
257 unsigned char *src_ptr,
258 int src_pixels_per_line,
259 int xoffset,
260 int yoffset,
261 unsigned char *dst_ptr,
262 int dst_pitch
265 const short *HFilter;
266 const short *VFilter;
267 int FData[21*24]; /* Temp data bufffer used in filtering */
270 HFilter = sub_pel_filters[xoffset]; /* 6 tap */
271 VFilter = sub_pel_filters[yoffset]; /* 6 tap */
273 /* First filter 1-D horizontally... */
274 vp8_filter_block2d_first_pass(src_ptr - (2 * src_pixels_per_line), FData, src_pixels_per_line, 1, 21, 16, HFilter);
276 /* then filter verticaly... */
277 vp8_filter_block2d_second_pass(FData + 32, dst_ptr, dst_pitch, 16, 16, 16, 16, VFilter);
282 /****************************************************************************
284 * ROUTINE : filter_block2d_bil_first_pass
286 * INPUTS : UINT8 *src_ptr : Pointer to source block.
287 * UINT32 src_pixels_per_line : Stride of input block.
288 * UINT32 pixel_step : Offset between filter input samples (see notes).
289 * UINT32 output_height : Input block height.
290 * UINT32 output_width : Input block width.
291 * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
293 * OUTPUTS : INT32 *output_ptr : Pointer to filtered block.
295 * RETURNS : void
297 * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block in
298 * either horizontal or vertical direction to produce the
299 * filtered output block. Used to implement first-pass
300 * of 2-D separable filter.
302 * SPECIAL NOTES : Produces INT32 output to retain precision for next pass.
303 * Two filter taps should sum to VP8_FILTER_WEIGHT.
304 * pixel_step defines whether the filter is applied
305 * horizontally (pixel_step=1) or vertically (pixel_step=stride).
306 * It defines the offset required to move from one input
307 * to the next.
309 ****************************************************************************/
310 void vp8_filter_block2d_bil_first_pass
312 unsigned char *src_ptr,
313 unsigned short *output_ptr,
314 unsigned int src_pixels_per_line,
315 int pixel_step,
316 unsigned int output_height,
317 unsigned int output_width,
318 const int *vp8_filter
321 unsigned int i, j;
323 for (i = 0; i < output_height; i++)
325 for (j = 0; j < output_width; j++)
327 /* Apply bilinear filter */
328 output_ptr[j] = (((int)src_ptr[0] * vp8_filter[0]) +
329 ((int)src_ptr[pixel_step] * vp8_filter[1]) +
330 (VP8_FILTER_WEIGHT / 2)) >> VP8_FILTER_SHIFT;
331 src_ptr++;
334 /* Next row... */
335 src_ptr += src_pixels_per_line - output_width;
336 output_ptr += output_width;
340 /****************************************************************************
342 * ROUTINE : filter_block2d_bil_second_pass
344 * INPUTS : INT32 *src_ptr : Pointer to source block.
345 * UINT32 src_pixels_per_line : Stride of input block.
346 * UINT32 pixel_step : Offset between filter input samples (see notes).
347 * UINT32 output_height : Input block height.
348 * UINT32 output_width : Input block width.
349 * INT32 *vp8_filter : Array of 2 bi-linear filter taps.
351 * OUTPUTS : UINT16 *output_ptr : Pointer to filtered block.
353 * RETURNS : void
355 * FUNCTION : Applies a 1-D 2-tap bi-linear filter to the source block in
356 * either horizontal or vertical direction to produce the
357 * filtered output block. Used to implement second-pass
358 * of 2-D separable filter.
360 * SPECIAL NOTES : Requires 32-bit input as produced by filter_block2d_bil_first_pass.
361 * Two filter taps should sum to VP8_FILTER_WEIGHT.
362 * pixel_step defines whether the filter is applied
363 * horizontally (pixel_step=1) or vertically (pixel_step=stride).
364 * It defines the offset required to move from one input
365 * to the next.
367 ****************************************************************************/
368 void vp8_filter_block2d_bil_second_pass
370 unsigned short *src_ptr,
371 unsigned char *output_ptr,
372 int output_pitch,
373 unsigned int src_pixels_per_line,
374 unsigned int pixel_step,
375 unsigned int output_height,
376 unsigned int output_width,
377 const int *vp8_filter
380 unsigned int i, j;
381 int Temp;
383 for (i = 0; i < output_height; i++)
385 for (j = 0; j < output_width; j++)
387 /* Apply filter */
388 Temp = ((int)src_ptr[0] * vp8_filter[0]) +
389 ((int)src_ptr[pixel_step] * vp8_filter[1]) +
390 (VP8_FILTER_WEIGHT / 2);
391 output_ptr[j] = (unsigned int)(Temp >> VP8_FILTER_SHIFT);
392 src_ptr++;
395 /* Next row... */
396 src_ptr += src_pixels_per_line - output_width;
397 output_ptr += output_pitch;
402 /****************************************************************************
404 * ROUTINE : filter_block2d_bil
406 * INPUTS : UINT8 *src_ptr : Pointer to source block.
407 * UINT32 src_pixels_per_line : Stride of input block.
408 * INT32 *HFilter : Array of 2 horizontal filter taps.
409 * INT32 *VFilter : Array of 2 vertical filter taps.
411 * OUTPUTS : UINT16 *output_ptr : Pointer to filtered block.
413 * RETURNS : void
415 * FUNCTION : 2-D filters an input block by applying a 2-tap
416 * bi-linear filter horizontally followed by a 2-tap
417 * bi-linear filter vertically on the result.
419 * SPECIAL NOTES : The largest block size can be handled here is 16x16
421 ****************************************************************************/
422 void vp8_filter_block2d_bil
424 unsigned char *src_ptr,
425 unsigned char *output_ptr,
426 unsigned int src_pixels_per_line,
427 unsigned int dst_pitch,
428 const int *HFilter,
429 const int *VFilter,
430 int Width,
431 int Height
435 unsigned short FData[17*16]; /* Temp data bufffer used in filtering */
437 /* First filter 1-D horizontally... */
438 vp8_filter_block2d_bil_first_pass(src_ptr, FData, src_pixels_per_line, 1, Height + 1, Width, HFilter);
440 /* then 1-D vertically... */
441 vp8_filter_block2d_bil_second_pass(FData, output_ptr, dst_pitch, Width, Width, Height, Width, VFilter);
445 void vp8_bilinear_predict4x4_c
447 unsigned char *src_ptr,
448 int src_pixels_per_line,
449 int xoffset,
450 int yoffset,
451 unsigned char *dst_ptr,
452 int dst_pitch
455 const int *HFilter;
456 const int *VFilter;
458 HFilter = bilinear_filters[xoffset];
459 VFilter = bilinear_filters[yoffset];
460 #if 0
462 int i;
463 unsigned char temp1[16];
464 unsigned char temp2[16];
466 bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
467 vp8_filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
469 for (i = 0; i < 16; i++)
471 if (temp1[i] != temp2[i])
473 bilinear_predict4x4_mmx(src_ptr, src_pixels_per_line, xoffset, yoffset, temp1, 4);
474 vp8_filter_block2d_bil(src_ptr, temp2, src_pixels_per_line, 4, HFilter, VFilter, 4, 4);
478 #endif
479 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 4, 4);
483 void vp8_bilinear_predict8x8_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 int *HFilter;
494 const int *VFilter;
496 HFilter = bilinear_filters[xoffset];
497 VFilter = bilinear_filters[yoffset];
499 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 8);
503 void vp8_bilinear_predict8x4_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 int *HFilter;
514 const int *VFilter;
516 HFilter = bilinear_filters[xoffset];
517 VFilter = bilinear_filters[yoffset];
519 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 8, 4);
523 void vp8_bilinear_predict16x16_c
525 unsigned char *src_ptr,
526 int src_pixels_per_line,
527 int xoffset,
528 int yoffset,
529 unsigned char *dst_ptr,
530 int dst_pitch
533 const int *HFilter;
534 const int *VFilter;
536 HFilter = bilinear_filters[xoffset];
537 VFilter = bilinear_filters[yoffset];
539 vp8_filter_block2d_bil(src_ptr, dst_ptr, src_pixels_per_line, dst_pitch, HFilter, VFilter, 16, 16);