michael@0: /* michael@0: * Copyright (c) 2013 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "./vpx_config.h" michael@0: #include "./vp9_rtcd.h" michael@0: #include "vp9/common/vp9_common.h" michael@0: #include "vp9/common/vp9_convolve.h" michael@0: #include "vp9/common/vp9_filter.h" michael@0: #include "vpx/vpx_integer.h" michael@0: #include "vpx_ports/mem.h" michael@0: michael@0: static void convolve_horiz_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x0, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h, int taps) { michael@0: int x, y, k; michael@0: michael@0: /* NOTE: This assumes that the filter table is 256-byte aligned. */ michael@0: /* TODO(agrange) Modify to make independent of table alignment. */ michael@0: const int16_t *const filter_x_base = michael@0: (const int16_t *)(((intptr_t)filter_x0) & ~(intptr_t)0xff); michael@0: michael@0: /* Adjust base pointer address for this source line */ michael@0: src -= taps / 2 - 1; michael@0: michael@0: for (y = 0; y < h; ++y) { michael@0: /* Initial phase offset */ michael@0: int x_q4 = (int)(filter_x0 - filter_x_base) / taps; michael@0: michael@0: for (x = 0; x < w; ++x) { michael@0: /* Per-pixel src offset */ michael@0: const int src_x = x_q4 >> SUBPEL_BITS; michael@0: int sum = 0; michael@0: michael@0: /* Pointer to filter to use */ michael@0: const int16_t *const filter_x = filter_x_base + michael@0: (x_q4 & SUBPEL_MASK) * taps; michael@0: michael@0: for (k = 0; k < taps; ++k) michael@0: sum += src[src_x + k] * filter_x[k]; michael@0: michael@0: dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); michael@0: michael@0: /* Move to the next source pixel */ michael@0: x_q4 += x_step_q4; michael@0: } michael@0: src += src_stride; michael@0: dst += dst_stride; michael@0: } michael@0: } michael@0: michael@0: static void convolve_avg_horiz_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x0, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h, int taps) { michael@0: int x, y, k; michael@0: michael@0: /* NOTE: This assumes that the filter table is 256-byte aligned. */ michael@0: /* TODO(agrange) Modify to make independent of table alignment. */ michael@0: const int16_t *const filter_x_base = michael@0: (const int16_t *)(((intptr_t)filter_x0) & ~(intptr_t)0xff); michael@0: michael@0: /* Adjust base pointer address for this source line */ michael@0: src -= taps / 2 - 1; michael@0: michael@0: for (y = 0; y < h; ++y) { michael@0: /* Initial phase offset */ michael@0: int x_q4 = (int)(filter_x0 - filter_x_base) / taps; michael@0: michael@0: for (x = 0; x < w; ++x) { michael@0: /* Per-pixel src offset */ michael@0: const int src_x = x_q4 >> SUBPEL_BITS; michael@0: int sum = 0; michael@0: michael@0: /* Pointer to filter to use */ michael@0: const int16_t *const filter_x = filter_x_base + michael@0: (x_q4 & SUBPEL_MASK) * taps; michael@0: michael@0: for (k = 0; k < taps; ++k) michael@0: sum += src[src_x + k] * filter_x[k]; michael@0: michael@0: dst[x] = ROUND_POWER_OF_TWO(dst[x] + michael@0: clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)), 1); michael@0: michael@0: /* Move to the next source pixel */ michael@0: x_q4 += x_step_q4; michael@0: } michael@0: src += src_stride; michael@0: dst += dst_stride; michael@0: } michael@0: } michael@0: michael@0: static void convolve_vert_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y0, int y_step_q4, michael@0: int w, int h, int taps) { michael@0: int x, y, k; michael@0: michael@0: /* NOTE: This assumes that the filter table is 256-byte aligned. */ michael@0: /* TODO(agrange) Modify to make independent of table alignment. */ michael@0: const int16_t *const filter_y_base = michael@0: (const int16_t *)(((intptr_t)filter_y0) & ~(intptr_t)0xff); michael@0: michael@0: /* Adjust base pointer address for this source column */ michael@0: src -= src_stride * (taps / 2 - 1); michael@0: michael@0: for (x = 0; x < w; ++x) { michael@0: /* Initial phase offset */ michael@0: int y_q4 = (int)(filter_y0 - filter_y_base) / taps; michael@0: michael@0: for (y = 0; y < h; ++y) { michael@0: /* Per-pixel src offset */ michael@0: const int src_y = y_q4 >> SUBPEL_BITS; michael@0: int sum = 0; michael@0: michael@0: /* Pointer to filter to use */ michael@0: const int16_t *const filter_y = filter_y_base + michael@0: (y_q4 & SUBPEL_MASK) * taps; michael@0: michael@0: for (k = 0; k < taps; ++k) michael@0: sum += src[(src_y + k) * src_stride] * filter_y[k]; michael@0: michael@0: dst[y * dst_stride] = michael@0: clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); michael@0: michael@0: /* Move to the next source pixel */ michael@0: y_q4 += y_step_q4; michael@0: } michael@0: ++src; michael@0: ++dst; michael@0: } michael@0: } michael@0: michael@0: static void convolve_avg_vert_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y0, int y_step_q4, michael@0: int w, int h, int taps) { michael@0: int x, y, k; michael@0: michael@0: /* NOTE: This assumes that the filter table is 256-byte aligned. */ michael@0: /* TODO(agrange) Modify to make independent of table alignment. */ michael@0: const int16_t *const filter_y_base = michael@0: (const int16_t *)(((intptr_t)filter_y0) & ~(intptr_t)0xff); michael@0: michael@0: /* Adjust base pointer address for this source column */ michael@0: src -= src_stride * (taps / 2 - 1); michael@0: michael@0: for (x = 0; x < w; ++x) { michael@0: /* Initial phase offset */ michael@0: int y_q4 = (int)(filter_y0 - filter_y_base) / taps; michael@0: michael@0: for (y = 0; y < h; ++y) { michael@0: /* Per-pixel src offset */ michael@0: const int src_y = y_q4 >> SUBPEL_BITS; michael@0: int sum = 0; michael@0: michael@0: /* Pointer to filter to use */ michael@0: const int16_t *const filter_y = filter_y_base + michael@0: (y_q4 & SUBPEL_MASK) * taps; michael@0: michael@0: for (k = 0; k < taps; ++k) michael@0: sum += src[(src_y + k) * src_stride] * filter_y[k]; michael@0: michael@0: dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] + michael@0: clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)), 1); michael@0: michael@0: /* Move to the next source pixel */ michael@0: y_q4 += y_step_q4; michael@0: } michael@0: ++src; michael@0: ++dst; michael@0: } michael@0: } michael@0: michael@0: static void convolve_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h, int taps) { michael@0: /* Fixed size intermediate buffer places limits on parameters. michael@0: * Maximum intermediate_height is 324, for y_step_q4 == 80, michael@0: * h == 64, taps == 8. michael@0: * y_step_q4 of 80 allows for 1/10 scale for 5 layer svc michael@0: */ michael@0: uint8_t temp[64 * 324]; michael@0: int intermediate_height = (((h - 1) * y_step_q4 + 15) >> 4) + taps; michael@0: michael@0: assert(w <= 64); michael@0: assert(h <= 64); michael@0: assert(taps <= 8); michael@0: assert(y_step_q4 <= 80); michael@0: assert(x_step_q4 <= 80); michael@0: michael@0: if (intermediate_height < h) michael@0: intermediate_height = h; michael@0: michael@0: convolve_horiz_c(src - src_stride * (taps / 2 - 1), src_stride, temp, 64, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, michael@0: intermediate_height, taps); michael@0: convolve_vert_c(temp + 64 * (taps / 2 - 1), 64, dst, dst_stride, filter_x, michael@0: x_step_q4, filter_y, y_step_q4, w, h, taps); michael@0: } michael@0: michael@0: void vp9_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: convolve_horiz_c(src, src_stride, dst, dst_stride, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h, 8); michael@0: } michael@0: michael@0: void vp9_convolve8_avg_horiz_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: convolve_avg_horiz_c(src, src_stride, dst, dst_stride, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h, 8); michael@0: } michael@0: michael@0: void vp9_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: convolve_vert_c(src, src_stride, dst, dst_stride, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h, 8); michael@0: } michael@0: michael@0: void vp9_convolve8_avg_vert_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: convolve_avg_vert_c(src, src_stride, dst, dst_stride, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h, 8); michael@0: } michael@0: michael@0: void vp9_convolve8_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: convolve_c(src, src_stride, dst, dst_stride, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h, 8); michael@0: } michael@0: michael@0: void vp9_convolve8_avg_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int x_step_q4, michael@0: const int16_t *filter_y, int y_step_q4, michael@0: int w, int h) { michael@0: /* Fixed size intermediate buffer places limits on parameters. */ michael@0: DECLARE_ALIGNED_ARRAY(16, uint8_t, temp, 64 * 64); michael@0: assert(w <= 64); michael@0: assert(h <= 64); michael@0: michael@0: vp9_convolve8(src, src_stride, temp, 64, michael@0: filter_x, x_step_q4, filter_y, y_step_q4, w, h); michael@0: vp9_convolve_avg(temp, 64, dst, dst_stride, NULL, 0, NULL, 0, w, h); michael@0: } michael@0: michael@0: void vp9_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int filter_x_stride, michael@0: const int16_t *filter_y, int filter_y_stride, michael@0: int w, int h) { michael@0: int r; michael@0: michael@0: for (r = h; r > 0; --r) { michael@0: vpx_memcpy(dst, src, w); michael@0: src += src_stride; michael@0: dst += dst_stride; michael@0: } michael@0: } michael@0: michael@0: void vp9_convolve_avg_c(const uint8_t *src, ptrdiff_t src_stride, michael@0: uint8_t *dst, ptrdiff_t dst_stride, michael@0: const int16_t *filter_x, int filter_x_stride, michael@0: const int16_t *filter_y, int filter_y_stride, michael@0: int w, int h) { michael@0: int x, y; michael@0: michael@0: for (y = 0; y < h; ++y) { michael@0: for (x = 0; x < w; ++x) michael@0: dst[x] = ROUND_POWER_OF_TWO(dst[x] + src[x], 1); michael@0: michael@0: src += src_stride; michael@0: dst += dst_stride; michael@0: } michael@0: }