2 * Copyright (c) 2014 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.
18 #include "vp9/common/vp9_common.h"
19 #include "vp9/encoder/vp9_resize.h"
25 #define SUBPEL_MASK ((1 << SUBPEL_BITS) - 1)
26 #define INTERP_PRECISION_BITS 32
28 typedef int16_t interp_kernel[INTERP_TAPS];
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}
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},
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}
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}
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}
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};
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;
225 return vp9_filteredinterp_filters500;
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) /
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;
239 const interp_kernel *interp_filters =
240 choose_interp_filter(inlength, outlength);
244 while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
250 y = delta * x + offset;
251 while ((y >> INTERP_PRECISION_BITS) +
252 (int64_t)(INTERP_TAPS / 2) >= inlength) {
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];
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))];
269 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
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];
279 for (k = 0; k < INTERP_TAPS; ++k)
280 sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0 ?
282 int_pel - INTERP_TAPS / 2 + 1 + k)];
283 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
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];
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));
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];
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));
312 static void down2_symeven(const uint8_t *const input, int length,
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;
318 uint8_t *optr = output;
319 int l1 = filter_len_half;
320 int l2 = (length - filter_len_half);
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)]) *
333 *optr++ = clip_pixel(sum);
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];
343 *optr++ = clip_pixel(sum);
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];
352 *optr++ = clip_pixel(sum);
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)]) *
363 *optr++ = clip_pixel(sum);
368 static void down2_symodd(const uint8_t *const input, int length,
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;
374 uint8_t *optr = output;
375 int l1 = filter_len_half - 1;
376 int l2 = (length - filter_len_half + 1);
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)]) *
389 *optr++ = clip_pixel(sum);
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];
399 *optr++ = clip_pixel(sum);
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];
408 *optr++ = clip_pixel(sum);
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)]) *
418 *optr++ = clip_pixel(sum);
423 static int get_down2_length(int length, int steps) {
425 for (s = 0; s < steps; ++s)
426 length = (length + 1) >> 1;
430 int get_down2_steps(int in_length, int out_length) {
433 while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
435 in_length = proj_in_length;
440 static void resize_multistep(const uint8_t *const input,
446 if (length == olength) {
447 memcpy(output, input, sizeof(uint8_t) * length);
450 steps = get_down2_steps(length, olength);
455 uint8_t *tmpbuf = NULL;
456 uint8_t *otmp, *otmp2;
457 int filteredlength = length;
459 tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) * length);
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)
471 out = (s & 1 ? otmp2 : otmp);
472 if (filteredlength & 1)
473 down2_symodd(in, filteredlength, out);
475 down2_symeven(in, filteredlength, out);
476 filteredlength = proj_filteredlength;
478 if (filteredlength != olength) {
479 interpolate(out, filteredlength, output, olength);
484 interpolate(input, length, output, olength);
488 static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
492 for (i = 0; i < len; ++i, iptr += stride) {
497 static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
501 for (i = 0; i < len; ++i, iptr += stride) {
506 void vp9_resize_plane(const uint8_t *const input,
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);
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;
544 const interp_kernel *interp_filters =
545 choose_interp_filter(inlength, outlength);
549 while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
555 y = delta * x + offset;
556 while ((y >> INTERP_PRECISION_BITS) +
557 (int64_t)(INTERP_TAPS / 2) >= inlength) {
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];
569 for (k = 0; k < INTERP_TAPS; ++k) {
570 const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
572 input[(pk < 0 ? 0 : (pk >= inlength ? inlength - 1 : pk))];
574 *optr++ = clip_pixel_high(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
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];
584 for (k = 0; k < INTERP_TAPS; ++k)
586 input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0 ?
587 0 : int_pel - INTERP_TAPS / 2 + 1 + k)];
588 *optr++ = clip_pixel_high(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
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];
597 for (k = 0; k < INTERP_TAPS; ++k)
598 sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
599 *optr++ = clip_pixel_high(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
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];
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_high(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
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;
623 uint16_t *optr = output;
624 int l1 = filter_len_half;
625 int l2 = (length - filter_len_half);
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)]) *
638 *optr++ = clip_pixel_high(sum, bd);
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];
648 *optr++ = clip_pixel_high(sum, bd);
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];
657 *optr++ = clip_pixel_high(sum, bd);
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)]) *
668 *optr++ = clip_pixel_high(sum, bd);
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;
679 uint16_t *optr = output;
680 int l1 = filter_len_half - 1;
681 int l2 = (length - filter_len_half + 1);
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)]) *
694 *optr++ = clip_pixel_high(sum, bd);
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];
704 *optr++ = clip_pixel_high(sum, bd);
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];
713 *optr++ = clip_pixel_high(sum, bd);
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)]) *
723 *optr++ = clip_pixel_high(sum, bd);
728 static void highbd_resize_multistep(const uint16_t *const input,
735 if (length == olength) {
736 memcpy(output, input, sizeof(uint16_t) * length);
739 steps = get_down2_steps(length, olength);
743 uint16_t *out = NULL;
744 uint16_t *tmpbuf = NULL;
745 uint16_t *otmp, *otmp2;
746 int filteredlength = length;
748 tmpbuf = (uint16_t *)malloc(sizeof(uint16_t) * length);
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)
760 out = (s & 1 ? otmp2 : otmp);
761 if (filteredlength & 1)
762 highbd_down2_symodd(in, filteredlength, out, bd);
764 highbd_down2_symeven(in, filteredlength, out, bd);
765 filteredlength = proj_filteredlength;
767 if (filteredlength != olength) {
768 highbd_interpolate(out, filteredlength, output, olength, bd);
773 highbd_interpolate(input, length, output, olength, bd);
777 static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
780 uint16_t *iptr = img;
781 uint16_t *aptr = arr;
782 for (i = 0; i < len; ++i, iptr += stride) {
787 static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
790 uint16_t *iptr = img;
791 uint16_t *aptr = arr;
792 for (i = 0; i < len; ++i, iptr += stride) {
797 void vp9_highbd_resize_plane(const uint8_t *const input,
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);
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,
819 highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
826 #endif // CONFIG_VP9_HIGHBITDEPTH
828 void vp9_resize_frame420(const uint8_t *const y,
830 const uint8_t *const u, const uint8_t *const v,
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);
844 void vp9_resize_frame422(const uint8_t *const y, int y_stride,
845 const uint8_t *const u, const uint8_t *const v,
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);
859 void vp9_resize_frame444(const uint8_t *const y, int y_stride,
860 const uint8_t *const u, const uint8_t *const v,
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);
874 #if CONFIG_VP9_HIGHBITDEPTH
875 void vp9_highbd_resize_frame420(const uint8_t *const y,
877 const uint8_t *const u, const uint8_t *const v,
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);
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,
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);
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,
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);
920 #endif // CONFIG_VP9_HIGHBITDEPTH