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.
11 #include "./vp9_rtcd.h"
13 #include "vpx_ports/mem.h"
14 #include "vpx/vpx_integer.h"
16 #include "vp9/common/vp9_common.h"
17 #include "vp9/common/vp9_filter.h"
19 #include "vp9/encoder/vp9_variance.h"
21 void variance(const uint8_t *a, int a_stride,
22 const uint8_t *b, int b_stride,
23 int w, int h, unsigned int *sse, int *sum) {
29 for (i = 0; i < h; i++) {
30 for (j = 0; j < w; j++) {
31 const int diff = a[j] - b[j];
41 // Applies a 1-D 2-tap bi-linear filter to the source block in either horizontal
42 // or vertical direction to produce the filtered output block. Used to implement
43 // first-pass of 2-D separable filter.
45 // Produces int32_t output to retain precision for next pass. Two filter taps
46 // should sum to VP9_FILTER_WEIGHT. pixel_step defines whether the filter is
47 // applied horizontally (pixel_step=1) or vertically (pixel_step=stride). It
48 // defines the offset required to move from one input to the next.
49 static void var_filter_block2d_bil_first_pass(const uint8_t *src_ptr,
51 unsigned int src_pixels_per_line,
53 unsigned int output_height,
54 unsigned int output_width,
55 const int16_t *vp9_filter) {
58 for (i = 0; i < output_height; i++) {
59 for (j = 0; j < output_width; j++) {
60 output_ptr[j] = ROUND_POWER_OF_TWO((int)src_ptr[0] * vp9_filter[0] +
61 (int)src_ptr[pixel_step] * vp9_filter[1],
68 src_ptr += src_pixels_per_line - output_width;
69 output_ptr += output_width;
73 // Applies a 1-D 2-tap bi-linear filter to the source block in either horizontal
74 // or vertical direction to produce the filtered output block. Used to implement
75 // second-pass of 2-D separable filter.
77 // Requires 32-bit input as produced by filter_block2d_bil_first_pass. Two
78 // filter taps should sum to VP9_FILTER_WEIGHT. pixel_step defines whether the
79 // filter is applied horizontally (pixel_step=1) or vertically (pixel_step=
80 // stride). It defines the offset required to move from one input to the next.
81 static void var_filter_block2d_bil_second_pass(const uint16_t *src_ptr,
83 unsigned int src_pixels_per_line,
84 unsigned int pixel_step,
85 unsigned int output_height,
86 unsigned int output_width,
87 const int16_t *vp9_filter) {
90 for (i = 0; i < output_height; i++) {
91 for (j = 0; j < output_width; j++) {
92 output_ptr[j] = ROUND_POWER_OF_TWO((int)src_ptr[0] * vp9_filter[0] +
93 (int)src_ptr[pixel_step] * vp9_filter[1],
98 src_ptr += src_pixels_per_line - output_width;
99 output_ptr += output_width;
103 unsigned int vp9_get_mb_ss_c(const int16_t *src_ptr) {
104 unsigned int i, sum = 0;
106 for (i = 0; i < 256; ++i) {
107 sum += src_ptr[i] * src_ptr[i];
114 unsigned int vp9_variance##W##x##H##_c(const uint8_t *a, int a_stride, \
115 const uint8_t *b, int b_stride, \
116 unsigned int *sse) { \
118 variance(a, a_stride, b, b_stride, W, H, sse, &sum); \
119 return *sse - (((int64_t)sum * sum) / (W * H)); \
122 #define SUBPIX_VAR(W, H) \
123 unsigned int vp9_sub_pixel_variance##W##x##H##_c( \
124 const uint8_t *src, int src_stride, \
125 int xoffset, int yoffset, \
126 const uint8_t *dst, int dst_stride, \
127 unsigned int *sse) { \
128 uint16_t fdata3[(H + 1) * W]; \
129 uint8_t temp2[H * W]; \
131 var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, W, \
132 BILINEAR_FILTERS_2TAP(xoffset)); \
133 var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
134 BILINEAR_FILTERS_2TAP(yoffset)); \
136 return vp9_variance##W##x##H##_c(temp2, W, dst, dst_stride, sse); \
139 #define SUBPIX_AVG_VAR(W, H) \
140 unsigned int vp9_sub_pixel_avg_variance##W##x##H##_c( \
141 const uint8_t *src, int src_stride, \
142 int xoffset, int yoffset, \
143 const uint8_t *dst, int dst_stride, \
145 const uint8_t *second_pred) { \
146 uint16_t fdata3[(H + 1) * W]; \
147 uint8_t temp2[H * W]; \
148 DECLARE_ALIGNED_ARRAY(16, uint8_t, temp3, H * W); \
150 var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, W, \
151 BILINEAR_FILTERS_2TAP(xoffset)); \
152 var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
153 BILINEAR_FILTERS_2TAP(yoffset)); \
155 vp9_comp_avg_pred(temp3, second_pred, W, H, temp2, W); \
157 return vp9_variance##W##x##H##_c(temp3, W, dst, dst_stride, sse); \
160 void vp9_get16x16var_c(const uint8_t *src_ptr, int source_stride,
161 const uint8_t *ref_ptr, int ref_stride,
162 unsigned int *sse, int *sum) {
163 variance(src_ptr, source_stride, ref_ptr, ref_stride, 16, 16, sse, sum);
166 void vp9_get8x8var_c(const uint8_t *src_ptr, int source_stride,
167 const uint8_t *ref_ptr, int ref_stride,
168 unsigned int *sse, int *sum) {
169 variance(src_ptr, source_stride, ref_ptr, ref_stride, 8, 8, sse, sum);
172 unsigned int vp9_mse16x16_c(const uint8_t *src, int src_stride,
173 const uint8_t *ref, int ref_stride,
176 variance(src, src_stride, ref, ref_stride, 16, 16, sse, &sum);
180 unsigned int vp9_mse16x8_c(const uint8_t *src, int src_stride,
181 const uint8_t *ref, int ref_stride,
184 variance(src, src_stride, ref, ref_stride, 16, 8, sse, &sum);
188 unsigned int vp9_mse8x16_c(const uint8_t *src, int src_stride,
189 const uint8_t *ref, int ref_stride,
192 variance(src, src_stride, ref, ref_stride, 8, 16, sse, &sum);
196 unsigned int vp9_mse8x8_c(const uint8_t *src, int src_stride,
197 const uint8_t *ref, int ref_stride,
200 variance(src, src_stride, ref, ref_stride, 8, 8, sse, &sum);
222 SUBPIX_AVG_VAR(8, 16)
226 SUBPIX_AVG_VAR(16, 8)
230 SUBPIX_AVG_VAR(16, 16)
234 SUBPIX_AVG_VAR(16, 32)
238 SUBPIX_AVG_VAR(32, 16)
242 SUBPIX_AVG_VAR(32, 32)
246 SUBPIX_AVG_VAR(32, 64)
250 SUBPIX_AVG_VAR(64, 32)
254 SUBPIX_AVG_VAR(64, 64)
256 void vp9_comp_avg_pred(uint8_t *comp_pred, const uint8_t *pred, int width,
257 int height, const uint8_t *ref, int ref_stride) {
260 for (i = 0; i < height; i++) {
261 for (j = 0; j < width; j++) {
262 const int tmp = pred[j] + ref[j];
263 comp_pred[j] = ROUND_POWER_OF_TWO(tmp, 1);
271 #if CONFIG_VP9_HIGHBITDEPTH
272 void high_variance64(const uint8_t *a8, int a_stride,
273 const uint8_t *b8, int b_stride,
274 int w, int h, uint64_t *sse,
278 uint16_t *a = CONVERT_TO_SHORTPTR(a8);
279 uint16_t *b = CONVERT_TO_SHORTPTR(b8);
283 for (i = 0; i < h; i++) {
284 for (j = 0; j < w; j++) {
285 const int diff = a[j] - b[j];
294 void high_variance(const uint8_t *a8, int a_stride,
295 const uint8_t *b8, int b_stride,
296 int w, int h, unsigned int *sse,
298 uint64_t sse_long = 0;
299 uint64_t sum_long = 0;
300 high_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long, &sum_long);
305 void high_10_variance(const uint8_t *a8, int a_stride,
306 const uint8_t *b8, int b_stride,
307 int w, int h, unsigned int *sse,
309 uint64_t sse_long = 0;
310 uint64_t sum_long = 0;
311 high_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long, &sum_long);
312 *sum = ROUND_POWER_OF_TWO(sum_long, 2);
313 *sse = ROUND_POWER_OF_TWO(sse_long, 4);
316 void high_12_variance(const uint8_t *a8, int a_stride,
317 const uint8_t *b8, int b_stride,
318 int w, int h, unsigned int *sse,
320 uint64_t sse_long = 0;
321 uint64_t sum_long = 0;
322 high_variance64(a8, a_stride, b8, b_stride, w, h, &sse_long, &sum_long);
323 *sum = ROUND_POWER_OF_TWO(sum_long, 4);
324 *sse = ROUND_POWER_OF_TWO(sse_long, 8);
327 static void high_var_filter_block2d_bil_first_pass(
328 const uint8_t *src_ptr8,
329 uint16_t *output_ptr,
330 unsigned int src_pixels_per_line,
332 unsigned int output_height,
333 unsigned int output_width,
334 const int16_t *vp9_filter) {
336 uint16_t *src_ptr = CONVERT_TO_SHORTPTR(src_ptr8);
337 for (i = 0; i < output_height; i++) {
338 for (j = 0; j < output_width; j++) {
340 ROUND_POWER_OF_TWO((int)src_ptr[0] * vp9_filter[0] +
341 (int)src_ptr[pixel_step] * vp9_filter[1],
348 src_ptr += src_pixels_per_line - output_width;
349 output_ptr += output_width;
353 static void high_var_filter_block2d_bil_second_pass(
354 const uint16_t *src_ptr,
355 uint16_t *output_ptr,
356 unsigned int src_pixels_per_line,
357 unsigned int pixel_step,
358 unsigned int output_height,
359 unsigned int output_width,
360 const int16_t *vp9_filter) {
363 for (i = 0; i < output_height; i++) {
364 for (j = 0; j < output_width; j++) {
366 ROUND_POWER_OF_TWO((int)src_ptr[0] * vp9_filter[0] +
367 (int)src_ptr[pixel_step] * vp9_filter[1],
372 src_ptr += src_pixels_per_line - output_width;
373 output_ptr += output_width;
377 #define HIGH_VAR(W, H) \
378 unsigned int vp9_high_variance##W##x##H##_c(const uint8_t *a, int a_stride, \
379 const uint8_t *b, int b_stride, \
380 unsigned int *sse) { \
382 high_variance(a, a_stride, b, b_stride, W, H, sse, &sum); \
383 return *sse - (((int64_t)sum * sum) / (W * H)); \
386 unsigned int vp9_high_10_variance##W##x##H##_c(const uint8_t *a, int a_stride, \
387 const uint8_t *b, int b_stride, \
388 unsigned int *sse) { \
390 high_10_variance(a, a_stride, b, b_stride, W, H, sse, &sum); \
391 return *sse - (((int64_t)sum * sum) / (W * H)); \
394 unsigned int vp9_high_12_variance##W##x##H##_c(const uint8_t *a, int a_stride, \
395 const uint8_t *b, int b_stride, \
396 unsigned int *sse) { \
398 high_12_variance(a, a_stride, b, b_stride, W, H, sse, &sum); \
399 return *sse - (((int64_t)sum * sum) / (W * H)); \
402 #define HIGH_SUBPIX_VAR(W, H) \
403 unsigned int vp9_high_sub_pixel_variance##W##x##H##_c( \
404 const uint8_t *src, int src_stride, \
405 int xoffset, int yoffset, \
406 const uint8_t *dst, int dst_stride, \
407 unsigned int *sse) { \
408 uint16_t fdata3[(H + 1) * W]; \
409 uint16_t temp2[H * W]; \
411 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
412 W, BILINEAR_FILTERS_2TAP(xoffset)); \
413 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
414 BILINEAR_FILTERS_2TAP(yoffset)); \
416 return vp9_high_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp2), W, dst, \
420 unsigned int vp9_high_10_sub_pixel_variance##W##x##H##_c( \
421 const uint8_t *src, int src_stride, \
422 int xoffset, int yoffset, \
423 const uint8_t *dst, int dst_stride, \
424 unsigned int *sse) { \
425 uint16_t fdata3[(H + 1) * W]; \
426 uint16_t temp2[H * W]; \
428 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
429 W, BILINEAR_FILTERS_2TAP(xoffset)); \
430 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
431 BILINEAR_FILTERS_2TAP(yoffset)); \
433 return vp9_high_10_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp2), W, dst, \
437 unsigned int vp9_high_12_sub_pixel_variance##W##x##H##_c( \
438 const uint8_t *src, int src_stride, \
439 int xoffset, int yoffset, \
440 const uint8_t *dst, int dst_stride, \
441 unsigned int *sse) { \
442 uint16_t fdata3[(H + 1) * W]; \
443 uint16_t temp2[H * W]; \
445 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
446 W, BILINEAR_FILTERS_2TAP(xoffset)); \
447 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
448 BILINEAR_FILTERS_2TAP(yoffset)); \
450 return vp9_high_12_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp2), W, dst, \
454 #define HIGH_SUBPIX_AVG_VAR(W, H) \
455 unsigned int vp9_high_sub_pixel_avg_variance##W##x##H##_c( \
456 const uint8_t *src, int src_stride, \
457 int xoffset, int yoffset, \
458 const uint8_t *dst, int dst_stride, \
460 const uint8_t *second_pred) { \
461 uint16_t fdata3[(H + 1) * W]; \
462 uint16_t temp2[H * W]; \
463 DECLARE_ALIGNED_ARRAY(16, uint16_t, temp3, H * W); \
465 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
466 W, BILINEAR_FILTERS_2TAP(xoffset)); \
467 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
468 BILINEAR_FILTERS_2TAP(yoffset)); \
470 vp9_high_comp_avg_pred(temp3, second_pred, W, H, CONVERT_TO_BYTEPTR(temp2), \
473 return vp9_high_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp3), W, dst, \
477 unsigned int vp9_high_10_sub_pixel_avg_variance##W##x##H##_c( \
478 const uint8_t *src, int src_stride, \
479 int xoffset, int yoffset, \
480 const uint8_t *dst, int dst_stride, \
482 const uint8_t *second_pred) { \
483 uint16_t fdata3[(H + 1) * W]; \
484 uint16_t temp2[H * W]; \
485 DECLARE_ALIGNED_ARRAY(16, uint16_t, temp3, H * W); \
487 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
488 W, BILINEAR_FILTERS_2TAP(xoffset)); \
489 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
490 BILINEAR_FILTERS_2TAP(yoffset)); \
492 vp9_high_comp_avg_pred(temp3, second_pred, W, H, CONVERT_TO_BYTEPTR(temp2), \
495 return vp9_high_10_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp3), W, dst, \
499 unsigned int vp9_high_12_sub_pixel_avg_variance##W##x##H##_c( \
500 const uint8_t *src, int src_stride, \
501 int xoffset, int yoffset, \
502 const uint8_t *dst, int dst_stride, \
504 const uint8_t *second_pred) { \
505 uint16_t fdata3[(H + 1) * W]; \
506 uint16_t temp2[H * W]; \
507 DECLARE_ALIGNED_ARRAY(16, uint16_t, temp3, H * W); \
509 high_var_filter_block2d_bil_first_pass(src, fdata3, src_stride, 1, H + 1, \
510 W, BILINEAR_FILTERS_2TAP(xoffset)); \
511 high_var_filter_block2d_bil_second_pass(fdata3, temp2, W, W, H, W, \
512 BILINEAR_FILTERS_2TAP(yoffset)); \
514 vp9_high_comp_avg_pred(temp3, second_pred, W, H, CONVERT_TO_BYTEPTR(temp2), \
517 return vp9_high_12_variance##W##x##H##_c(CONVERT_TO_BYTEPTR(temp3), W, dst, \
521 #define HIGH_GET_VAR(S) \
522 void vp9_high_get##S##x##S##var_c(const uint8_t *src, int src_stride, \
523 const uint8_t *ref, int ref_stride, \
524 unsigned int *sse, int *sum) { \
525 high_variance(src, src_stride, ref, ref_stride, S, S, sse, sum); \
528 void vp9_high_10_get##S##x##S##var_c(const uint8_t *src, int src_stride, \
529 const uint8_t *ref, int ref_stride, \
530 unsigned int *sse, int *sum) { \
531 high_10_variance(src, src_stride, ref, ref_stride, S, S, sse, sum); \
534 void vp9_high_12_get##S##x##S##var_c(const uint8_t *src, int src_stride, \
535 const uint8_t *ref, int ref_stride, \
536 unsigned int *sse, int *sum) { \
537 high_12_variance(src, src_stride, ref, ref_stride, S, S, sse, sum); \
540 #define HIGH_MSE(W, H) \
541 unsigned int vp9_high_mse##W##x##H##_c(const uint8_t *src, int src_stride, \
542 const uint8_t *ref, int ref_stride, \
543 unsigned int *sse) { \
545 high_variance(src, src_stride, ref, ref_stride, W, H, sse, &sum); \
549 unsigned int vp9_high_10_mse##W##x##H##_c(const uint8_t *src, int src_stride, \
550 const uint8_t *ref, int ref_stride, \
551 unsigned int *sse) { \
553 high_10_variance(src, src_stride, ref, ref_stride, W, H, sse, &sum); \
557 unsigned int vp9_high_12_mse##W##x##H##_c(const uint8_t *src, int src_stride, \
558 const uint8_t *ref, int ref_stride, \
559 unsigned int *sse) { \
561 high_12_variance(src, src_stride, ref, ref_stride, W, H, sse, &sum); \
574 HIGH_SUBPIX_VAR(4, 4)
575 HIGH_SUBPIX_AVG_VAR(4, 4)
578 HIGH_SUBPIX_VAR(4, 8)
579 HIGH_SUBPIX_AVG_VAR(4, 8)
582 HIGH_SUBPIX_VAR(8, 4)
583 HIGH_SUBPIX_AVG_VAR(8, 4)
586 HIGH_SUBPIX_VAR(8, 8)
587 HIGH_SUBPIX_AVG_VAR(8, 8)
590 HIGH_SUBPIX_VAR(8, 16)
591 HIGH_SUBPIX_AVG_VAR(8, 16)
594 HIGH_SUBPIX_VAR(16, 8)
595 HIGH_SUBPIX_AVG_VAR(16, 8)
598 HIGH_SUBPIX_VAR(16, 16)
599 HIGH_SUBPIX_AVG_VAR(16, 16)
602 HIGH_SUBPIX_VAR(16, 32)
603 HIGH_SUBPIX_AVG_VAR(16, 32)
606 HIGH_SUBPIX_VAR(32, 16)
607 HIGH_SUBPIX_AVG_VAR(32, 16)
610 HIGH_SUBPIX_VAR(32, 32)
611 HIGH_SUBPIX_AVG_VAR(32, 32)
614 HIGH_SUBPIX_VAR(32, 64)
615 HIGH_SUBPIX_AVG_VAR(32, 64)
618 HIGH_SUBPIX_VAR(64, 32)
619 HIGH_SUBPIX_AVG_VAR(64, 32)
622 HIGH_SUBPIX_VAR(64, 64)
623 HIGH_SUBPIX_AVG_VAR(64, 64)
625 void vp9_high_comp_avg_pred(uint16_t *comp_pred, const uint8_t *pred8,
626 int width, int height, const uint8_t *ref8,
629 uint16_t *pred = CONVERT_TO_SHORTPTR(pred8);
630 uint16_t *ref = CONVERT_TO_SHORTPTR(ref8);
631 for (i = 0; i < height; i++) {
632 for (j = 0; j < width; j++) {
633 const int tmp = pred[j] + ref[j];
634 comp_pred[j] = ROUND_POWER_OF_TWO(tmp, 1);
641 #endif // CONFIG_VP9_HIGHBITDEPTH