• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 #include <string.h>
14 
15 #include "config/aom_config.h"
16 #include "config/aom_dsp_rtcd.h"
17 
18 #include "aom/aom_integer.h"
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_dsp/aom_filter.h"
21 #include "aom_ports/mem.h"
22 
horz_scalar_product(const uint8_t * a,const int16_t * b)23 static INLINE int horz_scalar_product(const uint8_t *a, const int16_t *b) {
24   int sum = 0;
25   for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
26   return sum;
27 }
28 
vert_scalar_product(const uint8_t * a,ptrdiff_t a_stride,const int16_t * b)29 static INLINE int vert_scalar_product(const uint8_t *a, ptrdiff_t a_stride,
30                                       const int16_t *b) {
31   int sum = 0;
32   for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
33   return sum;
34 }
35 
convolve_horiz(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const InterpKernel * x_filters,int x0_q4,int x_step_q4,int w,int h)36 static void convolve_horiz(const uint8_t *src, ptrdiff_t src_stride,
37                            uint8_t *dst, ptrdiff_t dst_stride,
38                            const InterpKernel *x_filters, int x0_q4,
39                            int x_step_q4, int w, int h) {
40   src -= SUBPEL_TAPS / 2 - 1;
41   for (int y = 0; y < h; ++y) {
42     int x_q4 = x0_q4;
43     for (int x = 0; x < w; ++x) {
44       const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
45       const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
46       const int sum = horz_scalar_product(src_x, x_filter);
47       dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
48       x_q4 += x_step_q4;
49     }
50     src += src_stride;
51     dst += dst_stride;
52   }
53 }
54 
convolve_vert(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const InterpKernel * y_filters,int y0_q4,int y_step_q4,int w,int h)55 static void convolve_vert(const uint8_t *src, ptrdiff_t src_stride,
56                           uint8_t *dst, ptrdiff_t dst_stride,
57                           const InterpKernel *y_filters, int y0_q4,
58                           int y_step_q4, int w, int h) {
59   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
60 
61   for (int x = 0; x < w; ++x) {
62     int y_q4 = y0_q4;
63     for (int y = 0; y < h; ++y) {
64       const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
65       const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
66       const int sum = vert_scalar_product(src_y, src_stride, y_filter);
67       dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
68       y_q4 += y_step_q4;
69     }
70     ++src;
71     ++dst;
72   }
73 }
74 
get_filter_base(const int16_t * filter)75 static const InterpKernel *get_filter_base(const int16_t *filter) {
76   // NOTE: This assumes that the filter table is 256-byte aligned.
77   return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
78 }
79 
get_filter_offset(const int16_t * f,const InterpKernel * base)80 static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
81   return (int)((const InterpKernel *)(intptr_t)f - base);
82 }
83 
aom_convolve8_horiz_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h)84 void aom_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
85                            uint8_t *dst, ptrdiff_t dst_stride,
86                            const int16_t *filter_x, int x_step_q4,
87                            const int16_t *filter_y, int y_step_q4, int w,
88                            int h) {
89   const InterpKernel *const filters_x = get_filter_base(filter_x);
90   const int x0_q4 = get_filter_offset(filter_x, filters_x);
91 
92   (void)filter_y;
93   (void)y_step_q4;
94 
95   convolve_horiz(src, src_stride, dst, dst_stride, filters_x, x0_q4, x_step_q4,
96                  w, h);
97 }
98 
aom_convolve8_vert_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h)99 void aom_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
100                           uint8_t *dst, ptrdiff_t dst_stride,
101                           const int16_t *filter_x, int x_step_q4,
102                           const int16_t *filter_y, int y_step_q4, int w,
103                           int h) {
104   const InterpKernel *const filters_y = get_filter_base(filter_y);
105   const int y0_q4 = get_filter_offset(filter_y, filters_y);
106 
107   (void)filter_x;
108   (void)x_step_q4;
109 
110   convolve_vert(src, src_stride, dst, dst_stride, filters_y, y0_q4, y_step_q4,
111                 w, h);
112 }
113 
aom_convolve8_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const InterpKernel * filter,int x0_q4,int x_step_q4,int y0_q4,int y_step_q4,int w,int h)114 void aom_convolve8_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
115                      ptrdiff_t dst_stride, const InterpKernel *filter,
116                      int x0_q4, int x_step_q4, int y0_q4, int y_step_q4, int w,
117                      int h) {
118   // Note: Fixed size intermediate buffer, temp, places limits on parameters.
119   // 2d filtering proceeds in 2 steps:
120   //   (1) Interpolate horizontally into an intermediate buffer, temp.
121   //   (2) Interpolate temp vertically to derive the sub-pixel result.
122   // Deriving the maximum number of rows in the temp buffer (135):
123   // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
124   // --Largest block size is 64x64 pixels.
125   // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
126   //   original frame (in 1/16th pixel units).
127   // --Must round-up because block may be located at sub-pixel position.
128   // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
129   // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
130   // When calling in frame scaling function, the smallest scaling factor is x1/4
131   // ==> y_step_q4 = 64. Since w and h are at most 16, the temp buffer is still
132   // big enough.
133   uint8_t temp[64 * 135];
134   const int intermediate_height =
135       (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
136 
137   assert(w <= 64);
138   assert(h <= 64);
139   assert(y_step_q4 <= 32 || (y_step_q4 <= 64 && h <= 32));
140   assert(x_step_q4 <= 64);
141 
142   convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1), src_stride, temp, 64,
143                  filter, x0_q4, x_step_q4, w, intermediate_height);
144   convolve_vert(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst, dst_stride, filter,
145                 y0_q4, y_step_q4, w, h);
146 }
147 
aom_scaled_2d_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const InterpKernel * filter,int x0_q4,int x_step_q4,int y0_q4,int y_step_q4,int w,int h)148 void aom_scaled_2d_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
149                      ptrdiff_t dst_stride, const InterpKernel *filter,
150                      int x0_q4, int x_step_q4, int y0_q4, int y_step_q4, int w,
151                      int h) {
152   aom_convolve8_c(src, src_stride, dst, dst_stride, filter, x0_q4, x_step_q4,
153                   y0_q4, y_step_q4, w, h);
154 }
155 
aom_convolve_copy_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,int w,int h)156 void aom_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
157                          ptrdiff_t dst_stride, int w, int h) {
158   for (int r = h; r > 0; --r) {
159     memmove(dst, src, w);
160     src += src_stride;
161     dst += dst_stride;
162   }
163 }
164 
165 #if CONFIG_AV1_HIGHBITDEPTH
highbd_vert_scalar_product(const uint16_t * a,ptrdiff_t a_stride,const int16_t * b)166 static INLINE int highbd_vert_scalar_product(const uint16_t *a,
167                                              ptrdiff_t a_stride,
168                                              const int16_t *b) {
169   int sum = 0;
170   for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
171   return sum;
172 }
173 
highbd_horz_scalar_product(const uint16_t * a,const int16_t * b)174 static INLINE int highbd_horz_scalar_product(const uint16_t *a,
175                                              const int16_t *b) {
176   int sum = 0;
177   for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
178   return sum;
179 }
180 
highbd_convolve_horiz(const uint8_t * src8,ptrdiff_t src_stride,uint8_t * dst8,ptrdiff_t dst_stride,const InterpKernel * x_filters,int x0_q4,int x_step_q4,int w,int h,int bd)181 static void highbd_convolve_horiz(const uint8_t *src8, ptrdiff_t src_stride,
182                                   uint8_t *dst8, ptrdiff_t dst_stride,
183                                   const InterpKernel *x_filters, int x0_q4,
184                                   int x_step_q4, int w, int h, int bd) {
185   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
186   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
187   src -= SUBPEL_TAPS / 2 - 1;
188   for (int y = 0; y < h; ++y) {
189     int x_q4 = x0_q4;
190     for (int x = 0; x < w; ++x) {
191       const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
192       const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
193       const int sum = highbd_horz_scalar_product(src_x, x_filter);
194       dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
195       x_q4 += x_step_q4;
196     }
197     src += src_stride;
198     dst += dst_stride;
199   }
200 }
201 
highbd_convolve_vert(const uint8_t * src8,ptrdiff_t src_stride,uint8_t * dst8,ptrdiff_t dst_stride,const InterpKernel * y_filters,int y0_q4,int y_step_q4,int w,int h,int bd)202 static void highbd_convolve_vert(const uint8_t *src8, ptrdiff_t src_stride,
203                                  uint8_t *dst8, ptrdiff_t dst_stride,
204                                  const InterpKernel *y_filters, int y0_q4,
205                                  int y_step_q4, int w, int h, int bd) {
206   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
207   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
208   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
209   for (int x = 0; x < w; ++x) {
210     int y_q4 = y0_q4;
211     for (int y = 0; y < h; ++y) {
212       const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
213       const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
214       const int sum = highbd_vert_scalar_product(src_y, src_stride, y_filter);
215       dst[y * dst_stride] =
216           clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
217       y_q4 += y_step_q4;
218     }
219     ++src;
220     ++dst;
221   }
222 }
223 
aom_highbd_convolve8_horiz_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h,int bd)224 void aom_highbd_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
225                                   uint8_t *dst, ptrdiff_t dst_stride,
226                                   const int16_t *filter_x, int x_step_q4,
227                                   const int16_t *filter_y, int y_step_q4, int w,
228                                   int h, int bd) {
229   const InterpKernel *const filters_x = get_filter_base(filter_x);
230   const int x0_q4 = get_filter_offset(filter_x, filters_x);
231   (void)filter_y;
232   (void)y_step_q4;
233 
234   highbd_convolve_horiz(src, src_stride, dst, dst_stride, filters_x, x0_q4,
235                         x_step_q4, w, h, bd);
236 }
237 
aom_highbd_convolve8_vert_c(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h,int bd)238 void aom_highbd_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
239                                  uint8_t *dst, ptrdiff_t dst_stride,
240                                  const int16_t *filter_x, int x_step_q4,
241                                  const int16_t *filter_y, int y_step_q4, int w,
242                                  int h, int bd) {
243   const InterpKernel *const filters_y = get_filter_base(filter_y);
244   const int y0_q4 = get_filter_offset(filter_y, filters_y);
245   (void)filter_x;
246   (void)x_step_q4;
247 
248   highbd_convolve_vert(src, src_stride, dst, dst_stride, filters_y, y0_q4,
249                        y_step_q4, w, h, bd);
250 }
251 
aom_highbd_convolve_copy_c(const uint16_t * src,ptrdiff_t src_stride,uint16_t * dst,ptrdiff_t dst_stride,int w,int h)252 void aom_highbd_convolve_copy_c(const uint16_t *src, ptrdiff_t src_stride,
253                                 uint16_t *dst, ptrdiff_t dst_stride, int w,
254                                 int h) {
255   for (int y = 0; y < h; ++y) {
256     memmove(dst, src, w * sizeof(src[0]));
257     src += src_stride;
258     dst += dst_stride;
259   }
260 }
261 #endif  // CONFIG_AV1_HIGHBITDEPTH
262