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 
choose_interp_filter(int inlength,int outlength)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 
interpolate(const uint8_t * const input,int inlength,uint8_t * output,int outlength)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 
down2_symeven(const uint8_t * const input,int length,uint8_t * output)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   static 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 
down2_symodd(const uint8_t * const input,int length,uint8_t * output)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   static 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 
get_down2_length(int length,int steps)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 
get_down2_steps(int in_length,int out_length)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 
resize_multistep(const uint8_t * const input,int length,uint8_t * output,int olength,uint8_t * buf)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 
fill_col_to_arr(uint8_t * img,int stride,int len,uint8_t * arr)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 
fill_arr_to_col(uint8_t * img,int stride,int len,uint8_t * arr)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 
vp9_resize_plane(const uint8_t * const input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride)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 
vp9_resize_frame420(const uint8_t * const y,int y_stride,const uint8_t * const u,const uint8_t * const v,int uv_stride,int height,int width,uint8_t * oy,int oy_stride,uint8_t * ou,uint8_t * ov,int ouv_stride,int oheight,int owidth)532 void vp9_resize_frame420(const uint8_t *const y,
533                          int y_stride,
534                          const uint8_t *const u, const uint8_t *const v,
535                          int uv_stride,
536                          int height, int width,
537                          uint8_t *oy, int oy_stride,
538                          uint8_t *ou, uint8_t *ov, int ouv_stride,
539                          int oheight, int owidth) {
540   vp9_resize_plane(y, height, width, y_stride,
541                    oy, oheight, owidth, oy_stride);
542   vp9_resize_plane(u, height / 2, width / 2, uv_stride,
543                    ou, oheight / 2, owidth / 2, ouv_stride);
544   vp9_resize_plane(v, height / 2, width / 2, uv_stride,
545                    ov, oheight / 2, owidth / 2, ouv_stride);
546 }
547 
vp9_resize_frame422(const uint8_t * const y,int y_stride,const uint8_t * const u,const uint8_t * const v,int uv_stride,int height,int width,uint8_t * oy,int oy_stride,uint8_t * ou,uint8_t * ov,int ouv_stride,int oheight,int owidth)548 void vp9_resize_frame422(const uint8_t *const y, int y_stride,
549                          const uint8_t *const u, const uint8_t *const v,
550                          int uv_stride,
551                          int height, int width,
552                          uint8_t *oy, int oy_stride,
553                          uint8_t *ou, uint8_t *ov, int ouv_stride,
554                          int oheight, int owidth) {
555   vp9_resize_plane(y, height, width, y_stride,
556                    oy, oheight, owidth, oy_stride);
557   vp9_resize_plane(u, height, width / 2, uv_stride,
558                    ou, oheight, owidth / 2, ouv_stride);
559   vp9_resize_plane(v, height, width / 2, uv_stride,
560                    ov, oheight, owidth / 2, ouv_stride);
561 }
562 
vp9_resize_frame444(const uint8_t * const y,int y_stride,const uint8_t * const u,const uint8_t * const v,int uv_stride,int height,int width,uint8_t * oy,int oy_stride,uint8_t * ou,uint8_t * ov,int ouv_stride,int oheight,int owidth)563 void vp9_resize_frame444(const uint8_t *const y, int y_stride,
564                          const uint8_t *const u, const uint8_t *const v,
565                          int uv_stride,
566                          int height, int width,
567                          uint8_t *oy, int oy_stride,
568                          uint8_t *ou, uint8_t *ov, int ouv_stride,
569                          int oheight, int owidth) {
570   vp9_resize_plane(y, height, width, y_stride,
571                    oy, oheight, owidth, oy_stride);
572   vp9_resize_plane(u, height, width, uv_stride,
573                    ou, oheight, owidth, ouv_stride);
574   vp9_resize_plane(v, height, width, uv_stride,
575                    ov, oheight, owidth, ouv_stride);
576 }
577