Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_resize.c
1 /*
2  *  Copyright (c) 2014 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 #include <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "vp9/common/vp9_common.h"
19 #include "vp9/encoder/vp9_resize.h"
20
21 #define FILTER_BITS               7
22
23 #define INTERP_TAPS               8
24 #define SUBPEL_BITS               5
25 #define SUBPEL_MASK               ((1 << SUBPEL_BITS) - 1)
26 #define INTERP_PRECISION_BITS     32
27
28 typedef int16_t interp_kernel[INTERP_TAPS];
29
30 // Filters for interpolation (0.5-band) - note this also filters integer pels.
31 const interp_kernel vp9_filteredinterp_filters500[(1 << SUBPEL_BITS)] = {
32   {-3,  0, 35, 64, 35,  0, -3, 0},
33   {-3, -1, 34, 64, 36,  1, -3, 0},
34   {-3, -1, 32, 64, 38,  1, -3, 0},
35   {-2, -2, 31, 63, 39,  2, -3, 0},
36   {-2, -2, 29, 63, 41,  2, -3, 0},
37   {-2, -2, 28, 63, 42,  3, -4, 0},
38   {-2, -3, 27, 63, 43,  4, -4, 0},
39   {-2, -3, 25, 62, 45,  5, -4, 0},
40   {-2, -3, 24, 62, 46,  5, -4, 0},
41   {-2, -3, 23, 61, 47,  6, -4, 0},
42   {-2, -3, 21, 60, 49,  7, -4, 0},
43   {-1, -4, 20, 60, 50,  8, -4, -1},
44   {-1, -4, 19, 59, 51,  9, -4, -1},
45   {-1, -4, 17, 58, 52, 10, -4, 0},
46   {-1, -4, 16, 57, 53, 12, -4, -1},
47   {-1, -4, 15, 56, 54, 13, -4, -1},
48   {-1, -4, 14, 55, 55, 14, -4, -1},
49   {-1, -4, 13, 54, 56, 15, -4, -1},
50   {-1, -4, 12, 53, 57, 16, -4, -1},
51   {0, -4, 10, 52, 58, 17, -4, -1},
52   {-1, -4,  9, 51, 59, 19, -4, -1},
53   {-1, -4,  8, 50, 60, 20, -4, -1},
54   {0, -4,  7, 49, 60, 21, -3, -2},
55   {0, -4,  6, 47, 61, 23, -3, -2},
56   {0, -4,  5, 46, 62, 24, -3, -2},
57   {0, -4,  5, 45, 62, 25, -3, -2},
58   {0, -4,  4, 43, 63, 27, -3, -2},
59   {0, -4,  3, 42, 63, 28, -2, -2},
60   {0, -3,  2, 41, 63, 29, -2, -2},
61   {0, -3,  2, 39, 63, 31, -2, -2},
62   {0, -3,  1, 38, 64, 32, -1, -3},
63   {0, -3,  1, 36, 64, 34, -1, -3}
64 };
65
66 // Filters for interpolation (0.625-band) - note this also filters integer pels.
67 const interp_kernel vp9_filteredinterp_filters625[(1 << SUBPEL_BITS)] = {
68   {-1, -8, 33, 80, 33, -8, -1, 0},
69   {-1, -8, 30, 80, 35, -8, -1, 1},
70   {-1, -8, 28, 80, 37, -7, -2, 1},
71   {0, -8, 26, 79, 39, -7, -2, 1},
72   {0, -8, 24, 79, 41, -7, -2, 1},
73   {0, -8, 22, 78, 43, -6, -2, 1},
74   {0, -8, 20, 78, 45, -5, -3, 1},
75   {0, -8, 18, 77, 48, -5, -3, 1},
76   {0, -8, 16, 76, 50, -4, -3, 1},
77   {0, -8, 15, 75, 52, -3, -4, 1},
78   {0, -7, 13, 74, 54, -3, -4, 1},
79   {0, -7, 11, 73, 56, -2, -4, 1},
80   {0, -7, 10, 71, 58, -1, -4, 1},
81   {1, -7,  8, 70, 60,  0, -5, 1},
82   {1, -6,  6, 68, 62,  1, -5, 1},
83   {1, -6,  5, 67, 63,  2, -5, 1},
84   {1, -6,  4, 65, 65,  4, -6, 1},
85   {1, -5,  2, 63, 67,  5, -6, 1},
86   {1, -5,  1, 62, 68,  6, -6, 1},
87   {1, -5,  0, 60, 70,  8, -7, 1},
88   {1, -4, -1, 58, 71, 10, -7, 0},
89   {1, -4, -2, 56, 73, 11, -7, 0},
90   {1, -4, -3, 54, 74, 13, -7, 0},
91   {1, -4, -3, 52, 75, 15, -8, 0},
92   {1, -3, -4, 50, 76, 16, -8, 0},
93   {1, -3, -5, 48, 77, 18, -8, 0},
94   {1, -3, -5, 45, 78, 20, -8, 0},
95   {1, -2, -6, 43, 78, 22, -8, 0},
96   {1, -2, -7, 41, 79, 24, -8, 0},
97   {1, -2, -7, 39, 79, 26, -8, 0},
98   {1, -2, -7, 37, 80, 28, -8, -1},
99   {1, -1, -8, 35, 80, 30, -8, -1},
100 };
101
102 // Filters for interpolation (0.75-band) - note this also filters integer pels.
103 const interp_kernel vp9_filteredinterp_filters750[(1 << SUBPEL_BITS)] = {
104   {2, -11,  25,  96,  25, -11,   2, 0},
105   {2, -11,  22,  96,  28, -11,   2, 0},
106   {2, -10,  19,  95,  31, -11,   2, 0},
107   {2, -10,  17,  95,  34, -12,   2, 0},
108   {2,  -9,  14,  94,  37, -12,   2, 0},
109   {2,  -8,  12,  93,  40, -12,   1, 0},
110   {2,  -8,   9,  92,  43, -12,   1, 1},
111   {2,  -7,   7,  91,  46, -12,   1, 0},
112   {2,  -7,   5,  90,  49, -12,   1, 0},
113   {2,  -6,   3,  88,  52, -12,   0, 1},
114   {2,  -5,   1,  86,  55, -12,   0, 1},
115   {2,  -5,  -1,  84,  58, -11,   0, 1},
116   {2,  -4,  -2,  82,  61, -11,  -1, 1},
117   {2,  -4,  -4,  80,  64, -10,  -1, 1},
118   {1, -3, -5, 77, 67, -9, -1, 1},
119   {1, -3, -6, 75, 70, -8, -2, 1},
120   {1, -2, -7, 72, 72, -7, -2, 1},
121   {1, -2, -8, 70, 75, -6, -3, 1},
122   {1, -1, -9, 67, 77, -5, -3, 1},
123   {1,  -1, -10,  64,  80,  -4,  -4, 2},
124   {1,  -1, -11,  61,  82,  -2,  -4, 2},
125   {1,   0, -11,  58,  84,  -1,  -5, 2},
126   {1,   0, -12,  55,  86,   1,  -5, 2},
127   {1,   0, -12,  52,  88,   3,  -6, 2},
128   {0,   1, -12,  49,  90,   5,  -7, 2},
129   {0,   1, -12,  46,  91,   7,  -7, 2},
130   {1,   1, -12,  43,  92,   9,  -8, 2},
131   {0,   1, -12,  40,  93,  12,  -8, 2},
132   {0,   2, -12,  37,  94,  14,  -9, 2},
133   {0,   2, -12,  34,  95,  17, -10, 2},
134   {0,   2, -11,  31,  95,  19, -10, 2},
135   {0,   2, -11,  28,  96,  22, -11, 2}
136 };
137
138 // Filters for interpolation (0.875-band) - note this also filters integer pels.
139 const interp_kernel vp9_filteredinterp_filters875[(1 << SUBPEL_BITS)] = {
140   {3,  -8,  13, 112,  13,  -8,   3, 0},
141   {3,  -7,  10, 112,  17,  -9,   3, -1},
142   {2,  -6,   7, 111,  21,  -9,   3, -1},
143   {2,  -5,   4, 111,  24, -10,   3, -1},
144   {2,  -4,   1, 110,  28, -11,   3, -1},
145   {1,  -3,  -1, 108,  32, -12,   4, -1},
146   {1,  -2,  -3, 106,  36, -13,   4, -1},
147   {1,  -1,  -6, 105,  40, -14,   4, -1},
148   {1,  -1,  -7, 102,  44, -14,   4, -1},
149   {1,   0,  -9, 100,  48, -15,   4, -1},
150   {1,   1, -11,  97,  53, -16,   4, -1},
151   {0,   1, -12,  95,  57, -16,   4, -1},
152   {0,   2, -13,  91,  61, -16,   4, -1},
153   {0,   2, -14,  88,  65, -16,   4, -1},
154   {0,   3, -15,  84,  69, -17,   4, 0},
155   {0,   3, -16,  81,  73, -16,   3, 0},
156   {0,   3, -16,  77,  77, -16,   3, 0},
157   {0,   3, -16,  73,  81, -16,   3, 0},
158   {0,   4, -17,  69,  84, -15,   3, 0},
159   {-1,   4, -16,  65,  88, -14,   2, 0},
160   {-1,   4, -16,  61,  91, -13,   2, 0},
161   {-1,   4, -16,  57,  95, -12,   1, 0},
162   {-1,   4, -16,  53,  97, -11,   1, 1},
163   {-1,   4, -15,  48, 100,  -9,   0, 1},
164   {-1,   4, -14,  44, 102,  -7,  -1, 1},
165   {-1,   4, -14,  40, 105,  -6,  -1, 1},
166   {-1,   4, -13,  36, 106,  -3,  -2, 1},
167   {-1,   4, -12,  32, 108,  -1,  -3, 1},
168   {-1,   3, -11,  28, 110,   1,  -4, 2},
169   {-1,   3, -10,  24, 111,   4,  -5, 2},
170   {-1,   3,  -9,  21, 111,   7,  -6, 2},
171   {-1,   3,  -9,  17, 112,  10,  -7, 3}
172 };
173
174 // Filters for interpolation (full-band) - no filtering for integer pixels
175 const interp_kernel vp9_filteredinterp_filters1000[(1 << SUBPEL_BITS)] = {
176   {0,   0,   0, 128,   0,   0,   0, 0},
177   {0,   1,  -3, 128,   3,  -1,   0, 0},
178   {-1,   2,  -6, 127,   7,  -2,   1, 0},
179   {-1,   3,  -9, 126,  12,  -4,   1, 0},
180   {-1,   4, -12, 125,  16,  -5,   1, 0},
181   {-1,   4, -14, 123,  20,  -6,   2, 0},
182   {-1,   5, -15, 120,  25,  -8,   2, 0},
183   {-1,   5, -17, 118,  30,  -9,   3, -1},
184   {-1,   6, -18, 114,  35, -10,   3, -1},
185   {-1,   6, -19, 111,  41, -12,   3, -1},
186   {-1,   6, -20, 107,  46, -13,   4, -1},
187   {-1,   6, -21, 103,  52, -14,   4, -1},
188   {-1,   6, -21,  99,  57, -16,   5, -1},
189   {-1,   6, -21,  94,  63, -17,   5, -1},
190   {-1,   6, -20,  89,  68, -18,   5, -1},
191   {-1,   6, -20,  84,  73, -19,   6, -1},
192   {-1,   6, -20,  79,  79, -20,   6, -1},
193   {-1,   6, -19,  73,  84, -20,   6, -1},
194   {-1,   5, -18,  68,  89, -20,   6, -1},
195   {-1,   5, -17,  63,  94, -21,   6, -1},
196   {-1,   5, -16,  57,  99, -21,   6, -1},
197   {-1,   4, -14,  52, 103, -21,   6, -1},
198   {-1,   4, -13,  46, 107, -20,   6, -1},
199   {-1,   3, -12,  41, 111, -19,   6, -1},
200   {-1,   3, -10,  35, 114, -18,   6, -1},
201   {-1,   3,  -9,  30, 118, -17,   5, -1},
202   {0,   2,  -8,  25, 120, -15,   5, -1},
203   {0,   2,  -6,  20, 123, -14,   4, -1},
204   {0,   1,  -5,  16, 125, -12,   4, -1},
205   {0,   1,  -4,  12, 126,  -9,   3, -1},
206   {0,   1,  -2,   7, 127,  -6,   2, -1},
207   {0,   0,  -1,   3, 128,  -3,   1, 0}
208 };
209
210 // Filters for factor of 2 downsampling.
211 static const int16_t vp9_down2_symeven_half_filter[] = {56, 12, -3, -1};
212 static const int16_t vp9_down2_symodd_half_filter[] = {64, 35, 0, -3};
213
214 static const interp_kernel *choose_interp_filter(int inlength, int outlength) {
215   int outlength16 = outlength * 16;
216   if (outlength16 >= inlength * 16)
217     return vp9_filteredinterp_filters1000;
218   else if (outlength16 >= inlength * 13)
219     return vp9_filteredinterp_filters875;
220   else if (outlength16 >= inlength * 11)
221     return vp9_filteredinterp_filters750;
222   else if (outlength16 >= inlength * 9)
223     return vp9_filteredinterp_filters625;
224   else
225     return vp9_filteredinterp_filters500;
226 }
227
228 static void interpolate(const uint8_t *const input, int inlength,
229                         uint8_t *output, int outlength) {
230   const int64_t delta = (((uint64_t)inlength << 32) + outlength / 2) /
231       outlength;
232   const int64_t offset = inlength > outlength ?
233       (((int64_t)(inlength - outlength) << 31) + outlength / 2) / outlength :
234       -(((int64_t)(outlength - inlength) << 31) + outlength / 2) / outlength;
235   uint8_t *optr = output;
236   int x, x1, x2, sum, k, int_pel, sub_pel;
237   int64_t y;
238
239   const interp_kernel *interp_filters =
240       choose_interp_filter(inlength, outlength);
241
242   x = 0;
243   y = offset;
244   while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
245     x++;
246     y += delta;
247   }
248   x1 = x;
249   x = outlength - 1;
250   y = delta * x + offset;
251   while ((y >> INTERP_PRECISION_BITS) +
252          (int64_t)(INTERP_TAPS / 2) >= inlength) {
253     x--;
254     y -= delta;
255   }
256   x2 = x;
257   if (x1 > x2) {
258     for (x = 0, y = offset; x < outlength; ++x, y += delta) {
259       const int16_t *filter;
260       int_pel = y >> INTERP_PRECISION_BITS;
261       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
262       filter = interp_filters[sub_pel];
263       sum = 0;
264       for (k = 0; k < INTERP_TAPS; ++k) {
265         const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
266         sum += filter[k] * input[(pk < 0 ? 0 :
267                                   (pk >= inlength ? inlength - 1 : pk))];
268       }
269       *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
270     }
271   } else {
272     // Initial part.
273     for (x = 0, y = offset; x < x1; ++x, y += delta) {
274       const int16_t *filter;
275       int_pel = y >> INTERP_PRECISION_BITS;
276       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
277       filter = interp_filters[sub_pel];
278       sum = 0;
279       for (k = 0; k < INTERP_TAPS; ++k)
280         sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0 ?
281                                   0 :
282                                   int_pel - INTERP_TAPS / 2 + 1 + k)];
283       *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
284     }
285     // Middle part.
286     for (; x <= x2; ++x, y += delta) {
287       const int16_t *filter;
288       int_pel = y >> INTERP_PRECISION_BITS;
289       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
290       filter = interp_filters[sub_pel];
291       sum = 0;
292       for (k = 0; k < INTERP_TAPS; ++k)
293         sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
294       *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
295     }
296     // End part.
297     for (; x < outlength; ++x, y += delta) {
298       const int16_t *filter;
299       int_pel = y >> INTERP_PRECISION_BITS;
300       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
301       filter = interp_filters[sub_pel];
302       sum = 0;
303       for (k = 0; k < INTERP_TAPS; ++k)
304         sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >=
305                                   inlength ?  inlength - 1 :
306                                   int_pel - INTERP_TAPS / 2 + 1 + k)];
307       *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
308     }
309   }
310 }
311
312 static void down2_symeven(const uint8_t *const input, int length,
313                           uint8_t *output) {
314   // Actual filter len = 2 * filter_len_half.
315   const int16_t *filter = vp9_down2_symeven_half_filter;
316   const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
317   int i, j;
318   uint8_t *optr = output;
319   int l1 = filter_len_half;
320   int l2 = (length - filter_len_half);
321   l1 += (l1 & 1);
322   l2 += (l2 & 1);
323   if (l1 > l2) {
324     // Short input length.
325     for (i = 0; i < length; i += 2) {
326       int sum = (1 << (FILTER_BITS - 1));
327       for (j = 0; j < filter_len_half; ++j) {
328         sum += (input[(i - j < 0 ? 0 : i - j)] +
329                 input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
330             filter[j];
331       }
332       sum >>= FILTER_BITS;
333       *optr++ = clip_pixel(sum);
334     }
335   } else {
336     // Initial part.
337     for (i = 0; i < l1; i += 2) {
338       int sum = (1 << (FILTER_BITS - 1));
339       for (j = 0; j < filter_len_half; ++j) {
340         sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
341       }
342       sum >>= FILTER_BITS;
343       *optr++ = clip_pixel(sum);
344     }
345     // Middle part.
346     for (; i < l2; i += 2) {
347       int sum = (1 << (FILTER_BITS - 1));
348       for (j = 0; j < filter_len_half; ++j) {
349         sum += (input[i - j] + input[i + 1 + j]) * filter[j];
350       }
351       sum >>= FILTER_BITS;
352       *optr++ = clip_pixel(sum);
353     }
354     // End part.
355     for (; i < length; i += 2) {
356       int sum = (1 << (FILTER_BITS - 1));
357       for (j = 0; j < filter_len_half; ++j) {
358         sum += (input[i - j] +
359                 input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
360             filter[j];
361       }
362       sum >>= FILTER_BITS;
363       *optr++ = clip_pixel(sum);
364     }
365   }
366 }
367
368 static void down2_symodd(const uint8_t *const input, int length,
369                          uint8_t *output) {
370   // Actual filter len = 2 * filter_len_half - 1.
371   const int16_t *filter = vp9_down2_symodd_half_filter;
372   const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
373   int i, j;
374   uint8_t *optr = output;
375   int l1 = filter_len_half - 1;
376   int l2 = (length - filter_len_half + 1);
377   l1 += (l1 & 1);
378   l2 += (l2 & 1);
379   if (l1 > l2) {
380     // Short input length.
381     for (i = 0; i < length; i += 2) {
382       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
383       for (j = 1; j < filter_len_half; ++j) {
384         sum += (input[(i - j < 0 ? 0 : i - j)] +
385                 input[(i + j >= length ? length - 1 : i + j)]) *
386             filter[j];
387       }
388       sum >>= FILTER_BITS;
389       *optr++ = clip_pixel(sum);
390     }
391   } else {
392     // Initial part.
393     for (i = 0; i < l1; i += 2) {
394       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
395       for (j = 1; j < filter_len_half; ++j) {
396         sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
397       }
398       sum >>= FILTER_BITS;
399       *optr++ = clip_pixel(sum);
400     }
401     // Middle part.
402     for (; i < l2; i += 2) {
403       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
404       for (j = 1; j < filter_len_half; ++j) {
405         sum += (input[i - j] + input[i + j]) * filter[j];
406       }
407       sum >>= FILTER_BITS;
408       *optr++ = clip_pixel(sum);
409     }
410     // End part.
411     for (; i < length; i += 2) {
412       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
413       for (j = 1; j < filter_len_half; ++j) {
414         sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
415             filter[j];
416       }
417       sum >>= FILTER_BITS;
418       *optr++ = clip_pixel(sum);
419     }
420   }
421 }
422
423 static int get_down2_length(int length, int steps) {
424   int s;
425   for (s = 0; s < steps; ++s)
426     length = (length + 1) >> 1;
427   return length;
428 }
429
430 int get_down2_steps(int in_length, int out_length) {
431   int steps = 0;
432   int proj_in_length;
433   while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
434     ++steps;
435     in_length = proj_in_length;
436   }
437   return steps;
438 }
439
440 static void resize_multistep(const uint8_t *const input,
441                              int length,
442                              uint8_t *output,
443                              int olength,
444                              uint8_t *buf) {
445   int steps;
446   if (length == olength) {
447     memcpy(output, input, sizeof(uint8_t) * length);
448     return;
449   }
450   steps = get_down2_steps(length, olength);
451
452   if (steps > 0) {
453     int s;
454     uint8_t *out = NULL;
455     uint8_t *tmpbuf = NULL;
456     uint8_t *otmp, *otmp2;
457     int filteredlength = length;
458     if (!tmpbuf) {
459       tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) * length);
460       otmp = tmpbuf;
461     } else {
462       otmp = buf;
463     }
464     otmp2 = otmp + get_down2_length(length, 1);
465     for (s = 0; s < steps; ++s) {
466       const int proj_filteredlength = get_down2_length(filteredlength, 1);
467       const uint8_t *const in = (s == 0 ? input : out);
468       if (s == steps - 1 && proj_filteredlength == olength)
469         out = output;
470       else
471         out = (s & 1 ? otmp2 : otmp);
472       if (filteredlength & 1)
473         down2_symodd(in, filteredlength, out);
474       else
475         down2_symeven(in, filteredlength, out);
476       filteredlength = proj_filteredlength;
477     }
478     if (filteredlength != olength) {
479       interpolate(out, filteredlength, output, olength);
480     }
481     if (tmpbuf)
482       free(tmpbuf);
483   } else {
484     interpolate(input, length, output, olength);
485   }
486 }
487
488 static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
489   int i;
490   uint8_t *iptr = img;
491   uint8_t *aptr = arr;
492   for (i = 0; i < len; ++i, iptr += stride) {
493     *aptr++ = *iptr;
494   }
495 }
496
497 static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
498   int i;
499   uint8_t *iptr = img;
500   uint8_t *aptr = arr;
501   for (i = 0; i < len; ++i, iptr += stride) {
502     *iptr = *aptr++;
503   }
504 }
505
506 void vp9_resize_plane(const uint8_t *const input,
507                       int height,
508                       int width,
509                       int in_stride,
510                       uint8_t *output,
511                       int height2,
512                       int width2,
513                       int out_stride) {
514   int i;
515   uint8_t *intbuf = (uint8_t *)malloc(sizeof(uint8_t) * width2 * height);
516   uint8_t *tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) *
517                                       (width < height ? height : width));
518   uint8_t *arrbuf = (uint8_t *)malloc(sizeof(uint8_t) * (height + height2));
519   for (i = 0; i < height; ++i)
520     resize_multistep(input + in_stride * i, width,
521                         intbuf + width2 * i, width2, tmpbuf);
522   for (i = 0; i < width2; ++i) {
523     fill_col_to_arr(intbuf + i, width2, height, arrbuf);
524     resize_multistep(arrbuf, height, arrbuf + height, height2, tmpbuf);
525     fill_arr_to_col(output + i, out_stride, height2, arrbuf + height);
526   }
527   free(intbuf);
528   free(tmpbuf);
529   free(arrbuf);
530 }
531
532 #if CONFIG_VP9_HIGHBITDEPTH
533 static void highbd_interpolate(const uint16_t *const input, int inlength,
534                                uint16_t *output, int outlength, int bd) {
535   const int64_t delta =
536       (((uint64_t)inlength << 32) + outlength / 2) / outlength;
537   const int64_t offset = inlength > outlength ?
538       (((int64_t)(inlength - outlength) << 31) + outlength / 2) / outlength :
539       -(((int64_t)(outlength - inlength) << 31) + outlength / 2) / outlength;
540   uint16_t *optr = output;
541   int x, x1, x2, sum, k, int_pel, sub_pel;
542   int64_t y;
543
544   const interp_kernel *interp_filters =
545       choose_interp_filter(inlength, outlength);
546
547   x = 0;
548   y = offset;
549   while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
550     x++;
551     y += delta;
552   }
553   x1 = x;
554   x = outlength - 1;
555   y = delta * x + offset;
556   while ((y >> INTERP_PRECISION_BITS) +
557          (int64_t)(INTERP_TAPS / 2) >= inlength) {
558     x--;
559     y -= delta;
560   }
561   x2 = x;
562   if (x1 > x2) {
563     for (x = 0, y = offset; x < outlength; ++x, y += delta) {
564       const int16_t *filter;
565       int_pel = y >> INTERP_PRECISION_BITS;
566       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
567       filter = interp_filters[sub_pel];
568       sum = 0;
569       for (k = 0; k < INTERP_TAPS; ++k) {
570         const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
571         sum += filter[k] *
572             input[(pk < 0 ? 0 : (pk >= inlength ? inlength - 1 : pk))];
573       }
574       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
575     }
576   } else {
577     // Initial part.
578     for (x = 0, y = offset; x < x1; ++x, y += delta) {
579       const int16_t *filter;
580       int_pel = y >> INTERP_PRECISION_BITS;
581       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
582       filter = interp_filters[sub_pel];
583       sum = 0;
584       for (k = 0; k < INTERP_TAPS; ++k)
585         sum += filter[k] *
586             input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0 ?
587                    0 : int_pel - INTERP_TAPS / 2 + 1 + k)];
588       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
589     }
590     // Middle part.
591     for (; x <= x2; ++x, y += delta) {
592       const int16_t *filter;
593       int_pel = y >> INTERP_PRECISION_BITS;
594       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
595       filter = interp_filters[sub_pel];
596       sum = 0;
597       for (k = 0; k < INTERP_TAPS; ++k)
598         sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
599       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
600     }
601     // End part.
602     for (; x < outlength; ++x, y += delta) {
603       const int16_t *filter;
604       int_pel = y >> INTERP_PRECISION_BITS;
605       sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
606       filter = interp_filters[sub_pel];
607       sum = 0;
608       for (k = 0; k < INTERP_TAPS; ++k)
609         sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >=
610                                   inlength ?  inlength - 1 :
611                                   int_pel - INTERP_TAPS / 2 + 1 + k)];
612       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
613     }
614   }
615 }
616
617 static void highbd_down2_symeven(const uint16_t *const input, int length,
618                                  uint16_t *output, int bd) {
619   // Actual filter len = 2 * filter_len_half.
620   static const int16_t *filter = vp9_down2_symeven_half_filter;
621   const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
622   int i, j;
623   uint16_t *optr = output;
624   int l1 = filter_len_half;
625   int l2 = (length - filter_len_half);
626   l1 += (l1 & 1);
627   l2 += (l2 & 1);
628   if (l1 > l2) {
629     // Short input length.
630     for (i = 0; i < length; i += 2) {
631       int sum = (1 << (FILTER_BITS - 1));
632       for (j = 0; j < filter_len_half; ++j) {
633         sum += (input[(i - j < 0 ? 0 : i - j)] +
634                 input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
635             filter[j];
636       }
637       sum >>= FILTER_BITS;
638       *optr++ = clip_pixel_highbd(sum, bd);
639     }
640   } else {
641     // Initial part.
642     for (i = 0; i < l1; i += 2) {
643       int sum = (1 << (FILTER_BITS - 1));
644       for (j = 0; j < filter_len_half; ++j) {
645         sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
646       }
647       sum >>= FILTER_BITS;
648       *optr++ = clip_pixel_highbd(sum, bd);
649     }
650     // Middle part.
651     for (; i < l2; i += 2) {
652       int sum = (1 << (FILTER_BITS - 1));
653       for (j = 0; j < filter_len_half; ++j) {
654         sum += (input[i - j] + input[i + 1 + j]) * filter[j];
655       }
656       sum >>= FILTER_BITS;
657       *optr++ = clip_pixel_highbd(sum, bd);
658     }
659     // End part.
660     for (; i < length; i += 2) {
661       int sum = (1 << (FILTER_BITS - 1));
662       for (j = 0; j < filter_len_half; ++j) {
663         sum += (input[i - j] +
664                 input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
665             filter[j];
666       }
667       sum >>= FILTER_BITS;
668       *optr++ = clip_pixel_highbd(sum, bd);
669     }
670   }
671 }
672
673 static void highbd_down2_symodd(const uint16_t *const input, int length,
674                               uint16_t *output, int bd) {
675   // Actual filter len = 2 * filter_len_half - 1.
676   static const int16_t *filter = vp9_down2_symodd_half_filter;
677   const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
678   int i, j;
679   uint16_t *optr = output;
680   int l1 = filter_len_half - 1;
681   int l2 = (length - filter_len_half + 1);
682   l1 += (l1 & 1);
683   l2 += (l2 & 1);
684   if (l1 > l2) {
685     // Short input length.
686     for (i = 0; i < length; i += 2) {
687       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
688       for (j = 1; j < filter_len_half; ++j) {
689         sum += (input[(i - j < 0 ? 0 : i - j)] +
690                 input[(i + j >= length ? length - 1 : i + j)]) *
691             filter[j];
692       }
693       sum >>= FILTER_BITS;
694       *optr++ = clip_pixel_highbd(sum, bd);
695     }
696   } else {
697     // Initial part.
698     for (i = 0; i < l1; i += 2) {
699       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
700       for (j = 1; j < filter_len_half; ++j) {
701         sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
702       }
703       sum >>= FILTER_BITS;
704       *optr++ = clip_pixel_highbd(sum, bd);
705     }
706     // Middle part.
707     for (; i < l2; i += 2) {
708       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
709       for (j = 1; j < filter_len_half; ++j) {
710         sum += (input[i - j] + input[i + j]) * filter[j];
711       }
712       sum >>= FILTER_BITS;
713       *optr++ = clip_pixel_highbd(sum, bd);
714     }
715     // End part.
716     for (; i < length; i += 2) {
717       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
718       for (j = 1; j < filter_len_half; ++j) {
719         sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
720             filter[j];
721       }
722       sum >>= FILTER_BITS;
723       *optr++ = clip_pixel_highbd(sum, bd);
724     }
725   }
726 }
727
728 static void highbd_resize_multistep(const uint16_t *const input,
729                                     int length,
730                                     uint16_t *output,
731                                     int olength,
732                                     uint16_t *buf,
733                                     int bd) {
734   int steps;
735   if (length == olength) {
736     memcpy(output, input, sizeof(uint16_t) * length);
737     return;
738   }
739   steps = get_down2_steps(length, olength);
740
741   if (steps > 0) {
742     int s;
743     uint16_t *out = NULL;
744     uint16_t *tmpbuf = NULL;
745     uint16_t *otmp, *otmp2;
746     int filteredlength = length;
747     if (!tmpbuf) {
748       tmpbuf = (uint16_t *)malloc(sizeof(uint16_t) * length);
749       otmp = tmpbuf;
750     } else {
751       otmp = buf;
752     }
753     otmp2 = otmp + get_down2_length(length, 1);
754     for (s = 0; s < steps; ++s) {
755       const int proj_filteredlength = get_down2_length(filteredlength, 1);
756       const uint16_t *const in = (s == 0 ? input : out);
757       if (s == steps - 1 && proj_filteredlength == olength)
758         out = output;
759       else
760         out = (s & 1 ? otmp2 : otmp);
761       if (filteredlength & 1)
762         highbd_down2_symodd(in, filteredlength, out, bd);
763       else
764         highbd_down2_symeven(in, filteredlength, out, bd);
765       filteredlength = proj_filteredlength;
766     }
767     if (filteredlength != olength) {
768       highbd_interpolate(out, filteredlength, output, olength, bd);
769     }
770     if (tmpbuf)
771       free(tmpbuf);
772   } else {
773     highbd_interpolate(input, length, output, olength, bd);
774   }
775 }
776
777 static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
778                                    uint16_t *arr) {
779   int i;
780   uint16_t *iptr = img;
781   uint16_t *aptr = arr;
782   for (i = 0; i < len; ++i, iptr += stride) {
783     *aptr++ = *iptr;
784   }
785 }
786
787 static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
788                                    uint16_t *arr) {
789   int i;
790   uint16_t *iptr = img;
791   uint16_t *aptr = arr;
792   for (i = 0; i < len; ++i, iptr += stride) {
793     *iptr = *aptr++;
794   }
795 }
796
797 void vp9_highbd_resize_plane(const uint8_t *const input,
798                              int height,
799                              int width,
800                              int in_stride,
801                              uint8_t *output,
802                              int height2,
803                              int width2,
804                              int out_stride,
805                              int bd) {
806   int i;
807   uint16_t *intbuf = (uint16_t *)malloc(sizeof(uint16_t) * width2 * height);
808   uint16_t *tmpbuf = (uint16_t *)malloc(sizeof(uint16_t) *
809                                         (width < height ? height : width));
810   uint16_t *arrbuf = (uint16_t *)malloc(sizeof(uint16_t) * (height + height2));
811   for (i = 0; i < height; ++i) {
812     highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
813                             intbuf + width2 * i, width2, tmpbuf, bd);
814   }
815   for (i = 0; i < width2; ++i) {
816     highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
817     highbd_resize_multistep(arrbuf, height, arrbuf + height, height2, tmpbuf,
818                             bd);
819     highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
820                            arrbuf + height);
821   }
822   free(intbuf);
823   free(tmpbuf);
824   free(arrbuf);
825 }
826 #endif  // CONFIG_VP9_HIGHBITDEPTH
827
828 void vp9_resize_frame420(const uint8_t *const y,
829                          int y_stride,
830                          const uint8_t *const u, const uint8_t *const v,
831                          int uv_stride,
832                          int height, int width,
833                          uint8_t *oy, int oy_stride,
834                          uint8_t *ou, uint8_t *ov, int ouv_stride,
835                          int oheight, int owidth) {
836   vp9_resize_plane(y, height, width, y_stride,
837                    oy, oheight, owidth, oy_stride);
838   vp9_resize_plane(u, height / 2, width / 2, uv_stride,
839                    ou, oheight / 2, owidth / 2, ouv_stride);
840   vp9_resize_plane(v, height / 2, width / 2, uv_stride,
841                    ov, oheight / 2, owidth / 2, ouv_stride);
842 }
843
844 void vp9_resize_frame422(const uint8_t *const y, int y_stride,
845                          const uint8_t *const u, const uint8_t *const v,
846                          int uv_stride,
847                          int height, int width,
848                          uint8_t *oy, int oy_stride,
849                          uint8_t *ou, uint8_t *ov, int ouv_stride,
850                          int oheight, int owidth) {
851   vp9_resize_plane(y, height, width, y_stride,
852                    oy, oheight, owidth, oy_stride);
853   vp9_resize_plane(u, height, width / 2, uv_stride,
854                    ou, oheight, owidth / 2, ouv_stride);
855   vp9_resize_plane(v, height, width / 2, uv_stride,
856                    ov, oheight, owidth / 2, ouv_stride);
857 }
858
859 void vp9_resize_frame444(const uint8_t *const y, int y_stride,
860                          const uint8_t *const u, const uint8_t *const v,
861                          int uv_stride,
862                          int height, int width,
863                          uint8_t *oy, int oy_stride,
864                          uint8_t *ou, uint8_t *ov, int ouv_stride,
865                          int oheight, int owidth) {
866   vp9_resize_plane(y, height, width, y_stride,
867                    oy, oheight, owidth, oy_stride);
868   vp9_resize_plane(u, height, width, uv_stride,
869                    ou, oheight, owidth, ouv_stride);
870   vp9_resize_plane(v, height, width, uv_stride,
871                    ov, oheight, owidth, ouv_stride);
872 }
873
874 #if CONFIG_VP9_HIGHBITDEPTH
875 void vp9_highbd_resize_frame420(const uint8_t *const y,
876                                 int y_stride,
877                                 const uint8_t *const u, const uint8_t *const v,
878                                 int uv_stride,
879                                 int height, int width,
880                                 uint8_t *oy, int oy_stride,
881                                 uint8_t *ou, uint8_t *ov, int ouv_stride,
882                                 int oheight, int owidth, int bd) {
883   vp9_highbd_resize_plane(y, height, width, y_stride,
884                           oy, oheight, owidth, oy_stride, bd);
885   vp9_highbd_resize_plane(u, height / 2, width / 2, uv_stride,
886                           ou, oheight / 2, owidth / 2, ouv_stride, bd);
887   vp9_highbd_resize_plane(v, height / 2, width / 2, uv_stride,
888                           ov, oheight / 2, owidth / 2, ouv_stride, bd);
889 }
890
891 void vp9_highbd_resize_frame422(const uint8_t *const y, int y_stride,
892                                 const uint8_t *const u, const uint8_t *const v,
893                                 int uv_stride,
894                                 int height, int width,
895                                 uint8_t *oy, int oy_stride,
896                                 uint8_t *ou, uint8_t *ov, int ouv_stride,
897                                 int oheight, int owidth, int bd) {
898   vp9_highbd_resize_plane(y, height, width, y_stride,
899                           oy, oheight, owidth, oy_stride, bd);
900   vp9_highbd_resize_plane(u, height, width / 2, uv_stride,
901                           ou, oheight, owidth / 2, ouv_stride, bd);
902   vp9_highbd_resize_plane(v, height, width / 2, uv_stride,
903                           ov, oheight, owidth / 2, ouv_stride, bd);
904 }
905
906 void vp9_highbd_resize_frame444(const uint8_t *const y, int y_stride,
907                                 const uint8_t *const u, const uint8_t *const v,
908                                 int uv_stride,
909                                 int height, int width,
910                                 uint8_t *oy, int oy_stride,
911                                 uint8_t *ou, uint8_t *ov, int ouv_stride,
912                                 int oheight, int owidth, int bd) {
913   vp9_highbd_resize_plane(y, height, width, y_stride,
914                           oy, oheight, owidth, oy_stride, bd);
915   vp9_highbd_resize_plane(u, height, width, uv_stride,
916                           ou, oheight, owidth, ouv_stride, bd);
917   vp9_highbd_resize_plane(v, height, width, uv_stride,
918                           ov, oheight, owidth, ouv_stride, bd);
919 }
920 #endif  // CONFIG_VP9_HIGHBITDEPTH