Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_convolve.c
1 /*
2  *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3  *
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  */
10
11 #include <assert.h>
12
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"
20
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) {
25   int x, y;
26   src -= SUBPEL_TAPS / 2 - 1;
27   for (y = 0; y < h; ++y) {
28     int x_q4 = x0_q4;
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];
32       int k, sum = 0;
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));
36       x_q4 += x_step_q4;
37     }
38     src += src_stride;
39     dst += dst_stride;
40   }
41 }
42
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) {
47   int x, y;
48   src -= SUBPEL_TAPS / 2 - 1;
49   for (y = 0; y < h; ++y) {
50     int x_q4 = x0_q4;
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];
54       int k, sum = 0;
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);
59       x_q4 += x_step_q4;
60     }
61     src += src_stride;
62     dst += dst_stride;
63   }
64 }
65
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) {
70   int x, y;
71   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
72
73   for (x = 0; x < w; ++x) {
74     int y_q4 = y0_q4;
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];
78       int k, sum = 0;
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));
82       y_q4 += y_step_q4;
83     }
84     ++src;
85     ++dst;
86   }
87 }
88
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) {
93   int x, y;
94   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
95
96   for (x = 0; x < w; ++x) {
97     int y_q4 = y0_q4;
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];
101       int k, sum = 0;
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);
106       y_q4 += y_step_q4;
107     }
108     ++src;
109     ++dst;
110   }
111 }
112
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,
119                      int w, int h) {
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 = (((h - 1) * y_step_q4 + 15) >> 4) + SUBPEL_TAPS;
134
135   assert(w <= 64);
136   assert(h <= 64);
137   assert(y_step_q4 <= 32);
138   assert(x_step_q4 <= 32);
139
140   if (intermediate_height < h)
141     intermediate_height = h;
142
143   convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1), src_stride, temp, 64,
144                  x_filters, x0_q4, x_step_q4, w, intermediate_height);
145   convolve_vert(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst, dst_stride,
146                 y_filters, y0_q4, y_step_q4, w, h);
147 }
148
149 static const InterpKernel *get_filter_base(const int16_t *filter) {
150   // NOTE: This assumes that the filter table is 256-byte aligned.
151   // TODO(agrange) Modify to make independent of table alignment.
152   return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
153 }
154
155 static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
156   return (int)((const InterpKernel *)(intptr_t)f - base);
157 }
158
159 void vp9_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
160                            uint8_t *dst, ptrdiff_t dst_stride,
161                            const int16_t *filter_x, int x_step_q4,
162                            const int16_t *filter_y, int y_step_q4,
163                            int w, int h) {
164   const InterpKernel *const filters_x = get_filter_base(filter_x);
165   const int x0_q4 = get_filter_offset(filter_x, filters_x);
166
167   (void)filter_y;
168   (void)y_step_q4;
169
170   convolve_horiz(src, src_stride, dst, dst_stride, filters_x,
171                  x0_q4, x_step_q4, w, h);
172 }
173
174 void vp9_convolve8_avg_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
175                                uint8_t *dst, ptrdiff_t dst_stride,
176                                const int16_t *filter_x, int x_step_q4,
177                                const int16_t *filter_y, int y_step_q4,
178                                int w, int h) {
179   const InterpKernel *const filters_x = get_filter_base(filter_x);
180   const int x0_q4 = get_filter_offset(filter_x, filters_x);
181
182   (void)filter_y;
183   (void)y_step_q4;
184
185   convolve_avg_horiz(src, src_stride, dst, dst_stride, filters_x,
186                      x0_q4, x_step_q4, w, h);
187 }
188
189 void vp9_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
190                           uint8_t *dst, ptrdiff_t dst_stride,
191                           const int16_t *filter_x, int x_step_q4,
192                           const int16_t *filter_y, int y_step_q4,
193                           int w, int h) {
194   const InterpKernel *const filters_y = get_filter_base(filter_y);
195   const int y0_q4 = get_filter_offset(filter_y, filters_y);
196
197   (void)filter_x;
198   (void)x_step_q4;
199
200   convolve_vert(src, src_stride, dst, dst_stride, filters_y,
201                 y0_q4, y_step_q4, w, h);
202 }
203
204 void vp9_convolve8_avg_vert_c(const uint8_t *src, ptrdiff_t src_stride,
205                               uint8_t *dst, ptrdiff_t dst_stride,
206                               const int16_t *filter_x, int x_step_q4,
207                               const int16_t *filter_y, int y_step_q4,
208                               int w, int h) {
209   const InterpKernel *const filters_y = get_filter_base(filter_y);
210   const int y0_q4 = get_filter_offset(filter_y, filters_y);
211
212   (void)filter_x;
213   (void)x_step_q4;
214
215   convolve_avg_vert(src, src_stride, dst, dst_stride, filters_y,
216                     y0_q4, y_step_q4, w, h);
217 }
218
219 void vp9_convolve8_c(const uint8_t *src, ptrdiff_t src_stride,
220                      uint8_t *dst, ptrdiff_t dst_stride,
221                      const int16_t *filter_x, int x_step_q4,
222                      const int16_t *filter_y, int y_step_q4,
223                      int w, int h) {
224   const InterpKernel *const filters_x = get_filter_base(filter_x);
225   const int x0_q4 = get_filter_offset(filter_x, filters_x);
226
227   const InterpKernel *const filters_y = get_filter_base(filter_y);
228   const int y0_q4 = get_filter_offset(filter_y, filters_y);
229
230   convolve(src, src_stride, dst, dst_stride,
231            filters_x, x0_q4, x_step_q4,
232            filters_y, y0_q4, y_step_q4, w, h);
233 }
234
235 void vp9_convolve8_avg_c(const uint8_t *src, ptrdiff_t src_stride,
236                          uint8_t *dst, ptrdiff_t dst_stride,
237                          const int16_t *filter_x, int x_step_q4,
238                          const int16_t *filter_y, int y_step_q4,
239                          int w, int h) {
240   /* Fixed size intermediate buffer places limits on parameters. */
241   DECLARE_ALIGNED_ARRAY(16, uint8_t, temp, 64 * 64);
242   assert(w <= 64);
243   assert(h <= 64);
244
245   vp9_convolve8_c(src, src_stride, temp, 64,
246                   filter_x, x_step_q4, filter_y, y_step_q4, w, h);
247   vp9_convolve_avg_c(temp, 64, dst, dst_stride, NULL, 0, NULL, 0, w, h);
248 }
249
250 void vp9_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride,
251                          uint8_t *dst, ptrdiff_t dst_stride,
252                          const int16_t *filter_x, int filter_x_stride,
253                          const int16_t *filter_y, int filter_y_stride,
254                          int w, int h) {
255   int r;
256
257   (void)filter_x;  (void)filter_x_stride;
258   (void)filter_y;  (void)filter_y_stride;
259
260   for (r = h; r > 0; --r) {
261     vpx_memcpy(dst, src, w);
262     src += src_stride;
263     dst += dst_stride;
264   }
265 }
266
267 void vp9_convolve_avg_c(const uint8_t *src, ptrdiff_t src_stride,
268                         uint8_t *dst, ptrdiff_t dst_stride,
269                         const int16_t *filter_x, int filter_x_stride,
270                         const int16_t *filter_y, int filter_y_stride,
271                         int w, int h) {
272   int x, y;
273
274   (void)filter_x;  (void)filter_x_stride;
275   (void)filter_y;  (void)filter_y_stride;
276
277   for (y = 0; y < h; ++y) {
278     for (x = 0; x < w; ++x)
279       dst[x] = ROUND_POWER_OF_TWO(dst[x] + src[x], 1);
280
281     src += src_stride;
282     dst += dst_stride;
283   }
284 }