2 * Copyright (c) 2013 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.
13 #include "./vpx_config.h"
14 #include "./vp9_rtcd.h"
15 #include "vp9/common/vp9_common.h"
16 #include "vp9/common/vp9_convolve.h"
17 #include "vp9/common/vp9_filter.h"
18 #include "vpx/vpx_integer.h"
19 #include "vpx_ports/mem.h"
21 static void convolve_horiz(const uint8_t *src, ptrdiff_t src_stride,
22 uint8_t *dst, ptrdiff_t dst_stride,
23 const InterpKernel *x_filters,
24 int x0_q4, int x_step_q4, int w, int h) {
26 src -= SUBPEL_TAPS / 2 - 1;
27 for (y = 0; y < h; ++y) {
29 for (x = 0; x < w; ++x) {
30 const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
31 const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
33 for (k = 0; k < SUBPEL_TAPS; ++k)
34 sum += src_x[k] * x_filter[k];
35 dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
43 static void convolve_avg_horiz(const uint8_t *src, ptrdiff_t src_stride,
44 uint8_t *dst, ptrdiff_t dst_stride,
45 const InterpKernel *x_filters,
46 int x0_q4, int x_step_q4, int w, int h) {
48 src -= SUBPEL_TAPS / 2 - 1;
49 for (y = 0; y < h; ++y) {
51 for (x = 0; x < w; ++x) {
52 const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
53 const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
55 for (k = 0; k < SUBPEL_TAPS; ++k)
56 sum += src_x[k] * x_filter[k];
57 dst[x] = ROUND_POWER_OF_TWO(dst[x] +
58 clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)), 1);
66 static void convolve_vert(const uint8_t *src, ptrdiff_t src_stride,
67 uint8_t *dst, ptrdiff_t dst_stride,
68 const InterpKernel *y_filters,
69 int y0_q4, int y_step_q4, int w, int h) {
71 src -= src_stride * (SUBPEL_TAPS / 2 - 1);
73 for (x = 0; x < w; ++x) {
75 for (y = 0; y < h; ++y) {
76 const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
77 const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
79 for (k = 0; k < SUBPEL_TAPS; ++k)
80 sum += src_y[k * src_stride] * y_filter[k];
81 dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
89 static void convolve_avg_vert(const uint8_t *src, ptrdiff_t src_stride,
90 uint8_t *dst, ptrdiff_t dst_stride,
91 const InterpKernel *y_filters,
92 int y0_q4, int y_step_q4, int w, int h) {
94 src -= src_stride * (SUBPEL_TAPS / 2 - 1);
96 for (x = 0; x < w; ++x) {
98 for (y = 0; y < h; ++y) {
99 const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
100 const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
102 for (k = 0; k < SUBPEL_TAPS; ++k)
103 sum += src_y[k * src_stride] * y_filter[k];
104 dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] +
105 clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)), 1);
113 static void convolve(const uint8_t *src, ptrdiff_t src_stride,
114 uint8_t *dst, ptrdiff_t dst_stride,
115 const InterpKernel *const x_filters,
116 int x0_q4, int x_step_q4,
117 const InterpKernel *const y_filters,
118 int y0_q4, int y_step_q4,
120 // Note: Fixed size intermediate buffer, temp, places limits on parameters.
121 // 2d filtering proceeds in 2 steps:
122 // (1) Interpolate horizontally into an intermediate buffer, temp.
123 // (2) Interpolate temp vertically to derive the sub-pixel result.
124 // Deriving the maximum number of rows in the temp buffer (135):
125 // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
126 // --Largest block size is 64x64 pixels.
127 // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
128 // original frame (in 1/16th pixel units).
129 // --Must round-up because block may be located at sub-pixel position.
130 // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
131 // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
132 uint8_t temp[135 * 64];
133 int intermediate_height =
134 (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
138 assert(y_step_q4 <= 32);
139 assert(x_step_q4 <= 32);
141 convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1), src_stride, temp, 64,
142 x_filters, x0_q4, x_step_q4, w, intermediate_height);
143 convolve_vert(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst, dst_stride,
144 y_filters, y0_q4, y_step_q4, w, h);
147 static const InterpKernel *get_filter_base(const int16_t *filter) {
148 // NOTE: This assumes that the filter table is 256-byte aligned.
149 // TODO(agrange) Modify to make independent of table alignment.
150 return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
153 static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
154 return (int)((const InterpKernel *)(intptr_t)f - base);
157 void vp9_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
158 uint8_t *dst, ptrdiff_t dst_stride,
159 const int16_t *filter_x, int x_step_q4,
160 const int16_t *filter_y, int y_step_q4,
162 const InterpKernel *const filters_x = get_filter_base(filter_x);
163 const int x0_q4 = get_filter_offset(filter_x, filters_x);
168 convolve_horiz(src, src_stride, dst, dst_stride, filters_x,
169 x0_q4, x_step_q4, w, h);
172 void vp9_convolve8_avg_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
173 uint8_t *dst, ptrdiff_t dst_stride,
174 const int16_t *filter_x, int x_step_q4,
175 const int16_t *filter_y, int y_step_q4,
177 const InterpKernel *const filters_x = get_filter_base(filter_x);
178 const int x0_q4 = get_filter_offset(filter_x, filters_x);
183 convolve_avg_horiz(src, src_stride, dst, dst_stride, filters_x,
184 x0_q4, x_step_q4, w, h);
187 void vp9_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
188 uint8_t *dst, ptrdiff_t dst_stride,
189 const int16_t *filter_x, int x_step_q4,
190 const int16_t *filter_y, int y_step_q4,
192 const InterpKernel *const filters_y = get_filter_base(filter_y);
193 const int y0_q4 = get_filter_offset(filter_y, filters_y);
198 convolve_vert(src, src_stride, dst, dst_stride, filters_y,
199 y0_q4, y_step_q4, w, h);
202 void vp9_convolve8_avg_vert_c(const uint8_t *src, ptrdiff_t src_stride,
203 uint8_t *dst, ptrdiff_t dst_stride,
204 const int16_t *filter_x, int x_step_q4,
205 const int16_t *filter_y, int y_step_q4,
207 const InterpKernel *const filters_y = get_filter_base(filter_y);
208 const int y0_q4 = get_filter_offset(filter_y, filters_y);
213 convolve_avg_vert(src, src_stride, dst, dst_stride, filters_y,
214 y0_q4, y_step_q4, w, h);
217 void vp9_convolve8_c(const uint8_t *src, ptrdiff_t src_stride,
218 uint8_t *dst, ptrdiff_t dst_stride,
219 const int16_t *filter_x, int x_step_q4,
220 const int16_t *filter_y, int y_step_q4,
222 const InterpKernel *const filters_x = get_filter_base(filter_x);
223 const int x0_q4 = get_filter_offset(filter_x, filters_x);
225 const InterpKernel *const filters_y = get_filter_base(filter_y);
226 const int y0_q4 = get_filter_offset(filter_y, filters_y);
228 convolve(src, src_stride, dst, dst_stride,
229 filters_x, x0_q4, x_step_q4,
230 filters_y, y0_q4, y_step_q4, w, h);
233 void vp9_convolve8_avg_c(const uint8_t *src, ptrdiff_t src_stride,
234 uint8_t *dst, ptrdiff_t dst_stride,
235 const int16_t *filter_x, int x_step_q4,
236 const int16_t *filter_y, int y_step_q4,
238 /* Fixed size intermediate buffer places limits on parameters. */
239 DECLARE_ALIGNED_ARRAY(16, uint8_t, temp, 64 * 64);
243 vp9_convolve8_c(src, src_stride, temp, 64,
244 filter_x, x_step_q4, filter_y, y_step_q4, w, h);
245 vp9_convolve_avg_c(temp, 64, dst, dst_stride, NULL, 0, NULL, 0, w, h);
248 void vp9_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride,
249 uint8_t *dst, ptrdiff_t dst_stride,
250 const int16_t *filter_x, int filter_x_stride,
251 const int16_t *filter_y, int filter_y_stride,
255 (void)filter_x; (void)filter_x_stride;
256 (void)filter_y; (void)filter_y_stride;
258 for (r = h; r > 0; --r) {
259 vpx_memcpy(dst, src, w);
265 void vp9_convolve_avg_c(const uint8_t *src, ptrdiff_t src_stride,
266 uint8_t *dst, ptrdiff_t dst_stride,
267 const int16_t *filter_x, int filter_x_stride,
268 const int16_t *filter_y, int filter_y_stride,
272 (void)filter_x; (void)filter_x_stride;
273 (void)filter_y; (void)filter_y_stride;
275 for (y = 0; y < h; ++y) {
276 for (x = 0; x < w; ++x)
277 dst[x] = ROUND_POWER_OF_TWO(dst[x] + src[x], 1);
284 #if CONFIG_VP9_HIGHBITDEPTH
285 static void highbd_convolve_horiz(const uint8_t *src8, ptrdiff_t src_stride,
286 uint8_t *dst8, ptrdiff_t dst_stride,
287 const InterpKernel *x_filters,
288 int x0_q4, int x_step_q4,
289 int w, int h, int bd) {
291 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
292 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
293 src -= SUBPEL_TAPS / 2 - 1;
294 for (y = 0; y < h; ++y) {
296 for (x = 0; x < w; ++x) {
297 const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
298 const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
300 for (k = 0; k < SUBPEL_TAPS; ++k)
301 sum += src_x[k] * x_filter[k];
302 dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
310 static void highbd_convolve_avg_horiz(const uint8_t *src8, ptrdiff_t src_stride,
311 uint8_t *dst8, ptrdiff_t dst_stride,
312 const InterpKernel *x_filters,
313 int x0_q4, int x_step_q4,
314 int w, int h, int bd) {
316 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
317 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
318 src -= SUBPEL_TAPS / 2 - 1;
319 for (y = 0; y < h; ++y) {
321 for (x = 0; x < w; ++x) {
322 const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
323 const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
325 for (k = 0; k < SUBPEL_TAPS; ++k)
326 sum += src_x[k] * x_filter[k];
327 dst[x] = ROUND_POWER_OF_TWO(dst[x] +
328 clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd), 1);
336 static void highbd_convolve_vert(const uint8_t *src8, ptrdiff_t src_stride,
337 uint8_t *dst8, ptrdiff_t dst_stride,
338 const InterpKernel *y_filters,
339 int y0_q4, int y_step_q4, int w, int h,
342 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
343 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
344 src -= src_stride * (SUBPEL_TAPS / 2 - 1);
345 for (x = 0; x < w; ++x) {
347 for (y = 0; y < h; ++y) {
348 const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
349 const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
351 for (k = 0; k < SUBPEL_TAPS; ++k)
352 sum += src_y[k * src_stride] * y_filter[k];
353 dst[y * dst_stride] = clip_pixel_highbd(
354 ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
362 static void highbd_convolve_avg_vert(const uint8_t *src8, ptrdiff_t src_stride,
363 uint8_t *dst8, ptrdiff_t dst_stride,
364 const InterpKernel *y_filters,
365 int y0_q4, int y_step_q4, int w, int h,
368 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
369 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
370 src -= src_stride * (SUBPEL_TAPS / 2 - 1);
371 for (x = 0; x < w; ++x) {
373 for (y = 0; y < h; ++y) {
374 const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
375 const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
377 for (k = 0; k < SUBPEL_TAPS; ++k)
378 sum += src_y[k * src_stride] * y_filter[k];
379 dst[y * dst_stride] = ROUND_POWER_OF_TWO(dst[y * dst_stride] +
380 clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd), 1);
388 static void highbd_convolve(const uint8_t *src, ptrdiff_t src_stride,
389 uint8_t *dst, ptrdiff_t dst_stride,
390 const InterpKernel *const x_filters,
391 int x0_q4, int x_step_q4,
392 const InterpKernel *const y_filters,
393 int y0_q4, int y_step_q4,
394 int w, int h, int bd) {
395 // Note: Fixed size intermediate buffer, temp, places limits on parameters.
396 // 2d filtering proceeds in 2 steps:
397 // (1) Interpolate horizontally into an intermediate buffer, temp.
398 // (2) Interpolate temp vertically to derive the sub-pixel result.
399 // Deriving the maximum number of rows in the temp buffer (135):
400 // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
401 // --Largest block size is 64x64 pixels.
402 // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
403 // original frame (in 1/16th pixel units).
404 // --Must round-up because block may be located at sub-pixel position.
405 // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
406 // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
407 uint16_t temp[64 * 135];
408 int intermediate_height =
409 (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
413 assert(y_step_q4 <= 32);
414 assert(x_step_q4 <= 32);
416 highbd_convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1),
417 src_stride, CONVERT_TO_BYTEPTR(temp), 64,
418 x_filters, x0_q4, x_step_q4, w,
419 intermediate_height, bd);
420 highbd_convolve_vert(CONVERT_TO_BYTEPTR(temp) + 64 * (SUBPEL_TAPS / 2 - 1),
421 64, dst, dst_stride, y_filters, y0_q4, y_step_q4,
426 void vp9_highbd_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
427 uint8_t *dst, ptrdiff_t dst_stride,
428 const int16_t *filter_x, int x_step_q4,
429 const int16_t *filter_y, int y_step_q4,
430 int w, int h, int bd) {
431 const InterpKernel *const filters_x = get_filter_base(filter_x);
432 const int x0_q4 = get_filter_offset(filter_x, filters_x);
436 highbd_convolve_horiz(src, src_stride, dst, dst_stride, filters_x,
437 x0_q4, x_step_q4, w, h, bd);
440 void vp9_highbd_convolve8_avg_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
441 uint8_t *dst, ptrdiff_t dst_stride,
442 const int16_t *filter_x, int x_step_q4,
443 const int16_t *filter_y, int y_step_q4,
444 int w, int h, int bd) {
445 const InterpKernel *const filters_x = get_filter_base(filter_x);
446 const int x0_q4 = get_filter_offset(filter_x, filters_x);
450 highbd_convolve_avg_horiz(src, src_stride, dst, dst_stride, filters_x,
451 x0_q4, x_step_q4, w, h, bd);
454 void vp9_highbd_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
455 uint8_t *dst, ptrdiff_t dst_stride,
456 const int16_t *filter_x, int x_step_q4,
457 const int16_t *filter_y, int y_step_q4,
458 int w, int h, int bd) {
459 const InterpKernel *const filters_y = get_filter_base(filter_y);
460 const int y0_q4 = get_filter_offset(filter_y, filters_y);
464 highbd_convolve_vert(src, src_stride, dst, dst_stride, filters_y,
465 y0_q4, y_step_q4, w, h, bd);
468 void vp9_highbd_convolve8_avg_vert_c(const uint8_t *src, ptrdiff_t src_stride,
469 uint8_t *dst, ptrdiff_t dst_stride,
470 const int16_t *filter_x, int x_step_q4,
471 const int16_t *filter_y, int y_step_q4,
472 int w, int h, int bd) {
473 const InterpKernel *const filters_y = get_filter_base(filter_y);
474 const int y0_q4 = get_filter_offset(filter_y, filters_y);
478 highbd_convolve_avg_vert(src, src_stride, dst, dst_stride, filters_y,
479 y0_q4, y_step_q4, w, h, bd);
482 void vp9_highbd_convolve8_c(const uint8_t *src, ptrdiff_t src_stride,
483 uint8_t *dst, ptrdiff_t dst_stride,
484 const int16_t *filter_x, int x_step_q4,
485 const int16_t *filter_y, int y_step_q4,
486 int w, int h, int bd) {
487 const InterpKernel *const filters_x = get_filter_base(filter_x);
488 const int x0_q4 = get_filter_offset(filter_x, filters_x);
490 const InterpKernel *const filters_y = get_filter_base(filter_y);
491 const int y0_q4 = get_filter_offset(filter_y, filters_y);
493 highbd_convolve(src, src_stride, dst, dst_stride,
494 filters_x, x0_q4, x_step_q4,
495 filters_y, y0_q4, y_step_q4, w, h, bd);
498 void vp9_highbd_convolve8_avg_c(const uint8_t *src, ptrdiff_t src_stride,
499 uint8_t *dst, ptrdiff_t dst_stride,
500 const int16_t *filter_x, int x_step_q4,
501 const int16_t *filter_y, int y_step_q4,
502 int w, int h, int bd) {
503 // Fixed size intermediate buffer places limits on parameters.
504 DECLARE_ALIGNED_ARRAY(16, uint16_t, temp, 64 * 64);
508 vp9_highbd_convolve8_c(src, src_stride, CONVERT_TO_BYTEPTR(temp), 64,
509 filter_x, x_step_q4, filter_y, y_step_q4, w, h, bd);
510 vp9_highbd_convolve_avg_c(CONVERT_TO_BYTEPTR(temp), 64, dst, dst_stride,
511 NULL, 0, NULL, 0, w, h, bd);
514 void vp9_highbd_convolve_copy_c(const uint8_t *src8, ptrdiff_t src_stride,
515 uint8_t *dst8, ptrdiff_t dst_stride,
516 const int16_t *filter_x, int filter_x_stride,
517 const int16_t *filter_y, int filter_y_stride,
518 int w, int h, int bd) {
520 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
521 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
524 (void)filter_x_stride;
525 (void)filter_y_stride;
528 for (r = h; r > 0; --r) {
529 vpx_memcpy(dst, src, w * sizeof(uint16_t));
535 void vp9_highbd_convolve_avg_c(const uint8_t *src8, ptrdiff_t src_stride,
536 uint8_t *dst8, ptrdiff_t dst_stride,
537 const int16_t *filter_x, int filter_x_stride,
538 const int16_t *filter_y, int filter_y_stride,
539 int w, int h, int bd) {
541 uint16_t *src = CONVERT_TO_SHORTPTR(src8);
542 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
545 (void)filter_x_stride;
546 (void)filter_y_stride;
549 for (y = 0; y < h; ++y) {
550 for (x = 0; x < w; ++x) {
551 dst[x] = ROUND_POWER_OF_TWO(dst[x] + src[x], 1);