blob: 3625263d10fe45b04d9a941c509dea64d0c0b325 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <limits.h>
14#include <math.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
Alex Converse64d7ef62017-03-22 18:09:16 -070019#include "./aom_config.h"
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020020#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Sebastien Alaiwan71e87842017-04-12 16:03:28 +020022#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "aom_ports/mem.h"
Fergus Simpson090bb602017-04-07 12:37:53 -070024#include "aom_scale/aom_scale.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "av1/common/common.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070026#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027
Fergus Simpson090bb602017-04-07 12:37:53 -070028#include "./aom_scale_rtcd.h"
29
Yaowu Xuc27fc142016-08-22 16:08:15 -070030#define FILTER_BITS 7
31
32#define INTERP_TAPS 8
Fergus Simpson090bb602017-04-07 12:37:53 -070033#define SUBPEL_BITS_RS 5
34#define SUBPEL_MASK_RS ((1 << SUBPEL_BITS_RS) - 1)
Yaowu Xuc27fc142016-08-22 16:08:15 -070035#define INTERP_PRECISION_BITS 32
36
37typedef int16_t interp_kernel[INTERP_TAPS];
38
39// Filters for interpolation (0.5-band) - note this also filters integer pels.
Fergus Simpson090bb602017-04-07 12:37:53 -070040static const interp_kernel filteredinterp_filters500[(1 << SUBPEL_BITS_RS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -070041 { -3, 0, 35, 64, 35, 0, -3, 0 }, { -3, -1, 34, 64, 36, 1, -3, 0 },
42 { -3, -1, 32, 64, 38, 1, -3, 0 }, { -2, -2, 31, 63, 39, 2, -3, 0 },
43 { -2, -2, 29, 63, 41, 2, -3, 0 }, { -2, -2, 28, 63, 42, 3, -4, 0 },
44 { -2, -3, 27, 63, 43, 4, -4, 0 }, { -2, -3, 25, 62, 45, 5, -4, 0 },
45 { -2, -3, 24, 62, 46, 5, -4, 0 }, { -2, -3, 23, 61, 47, 6, -4, 0 },
46 { -2, -3, 21, 60, 49, 7, -4, 0 }, { -1, -4, 20, 60, 50, 8, -4, -1 },
47 { -1, -4, 19, 59, 51, 9, -4, -1 }, { -1, -4, 17, 58, 52, 10, -4, 0 },
48 { -1, -4, 16, 57, 53, 12, -4, -1 }, { -1, -4, 15, 56, 54, 13, -4, -1 },
49 { -1, -4, 14, 55, 55, 14, -4, -1 }, { -1, -4, 13, 54, 56, 15, -4, -1 },
50 { -1, -4, 12, 53, 57, 16, -4, -1 }, { 0, -4, 10, 52, 58, 17, -4, -1 },
51 { -1, -4, 9, 51, 59, 19, -4, -1 }, { -1, -4, 8, 50, 60, 20, -4, -1 },
52 { 0, -4, 7, 49, 60, 21, -3, -2 }, { 0, -4, 6, 47, 61, 23, -3, -2 },
53 { 0, -4, 5, 46, 62, 24, -3, -2 }, { 0, -4, 5, 45, 62, 25, -3, -2 },
54 { 0, -4, 4, 43, 63, 27, -3, -2 }, { 0, -4, 3, 42, 63, 28, -2, -2 },
55 { 0, -3, 2, 41, 63, 29, -2, -2 }, { 0, -3, 2, 39, 63, 31, -2, -2 },
56 { 0, -3, 1, 38, 64, 32, -1, -3 }, { 0, -3, 1, 36, 64, 34, -1, -3 }
57};
58
59// Filters for interpolation (0.625-band) - note this also filters integer pels.
Fergus Simpson090bb602017-04-07 12:37:53 -070060static const interp_kernel filteredinterp_filters625[(1 << SUBPEL_BITS_RS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -070061 { -1, -8, 33, 80, 33, -8, -1, 0 }, { -1, -8, 30, 80, 35, -8, -1, 1 },
62 { -1, -8, 28, 80, 37, -7, -2, 1 }, { 0, -8, 26, 79, 39, -7, -2, 1 },
63 { 0, -8, 24, 79, 41, -7, -2, 1 }, { 0, -8, 22, 78, 43, -6, -2, 1 },
64 { 0, -8, 20, 78, 45, -5, -3, 1 }, { 0, -8, 18, 77, 48, -5, -3, 1 },
65 { 0, -8, 16, 76, 50, -4, -3, 1 }, { 0, -8, 15, 75, 52, -3, -4, 1 },
66 { 0, -7, 13, 74, 54, -3, -4, 1 }, { 0, -7, 11, 73, 56, -2, -4, 1 },
67 { 0, -7, 10, 71, 58, -1, -4, 1 }, { 1, -7, 8, 70, 60, 0, -5, 1 },
68 { 1, -6, 6, 68, 62, 1, -5, 1 }, { 1, -6, 5, 67, 63, 2, -5, 1 },
69 { 1, -6, 4, 65, 65, 4, -6, 1 }, { 1, -5, 2, 63, 67, 5, -6, 1 },
70 { 1, -5, 1, 62, 68, 6, -6, 1 }, { 1, -5, 0, 60, 70, 8, -7, 1 },
71 { 1, -4, -1, 58, 71, 10, -7, 0 }, { 1, -4, -2, 56, 73, 11, -7, 0 },
72 { 1, -4, -3, 54, 74, 13, -7, 0 }, { 1, -4, -3, 52, 75, 15, -8, 0 },
73 { 1, -3, -4, 50, 76, 16, -8, 0 }, { 1, -3, -5, 48, 77, 18, -8, 0 },
74 { 1, -3, -5, 45, 78, 20, -8, 0 }, { 1, -2, -6, 43, 78, 22, -8, 0 },
75 { 1, -2, -7, 41, 79, 24, -8, 0 }, { 1, -2, -7, 39, 79, 26, -8, 0 },
76 { 1, -2, -7, 37, 80, 28, -8, -1 }, { 1, -1, -8, 35, 80, 30, -8, -1 },
77};
78
79// Filters for interpolation (0.75-band) - note this also filters integer pels.
Fergus Simpson090bb602017-04-07 12:37:53 -070080static const interp_kernel filteredinterp_filters750[(1 << SUBPEL_BITS_RS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -070081 { 2, -11, 25, 96, 25, -11, 2, 0 }, { 2, -11, 22, 96, 28, -11, 2, 0 },
82 { 2, -10, 19, 95, 31, -11, 2, 0 }, { 2, -10, 17, 95, 34, -12, 2, 0 },
83 { 2, -9, 14, 94, 37, -12, 2, 0 }, { 2, -8, 12, 93, 40, -12, 1, 0 },
84 { 2, -8, 9, 92, 43, -12, 1, 1 }, { 2, -7, 7, 91, 46, -12, 1, 0 },
85 { 2, -7, 5, 90, 49, -12, 1, 0 }, { 2, -6, 3, 88, 52, -12, 0, 1 },
86 { 2, -5, 1, 86, 55, -12, 0, 1 }, { 2, -5, -1, 84, 58, -11, 0, 1 },
87 { 2, -4, -2, 82, 61, -11, -1, 1 }, { 2, -4, -4, 80, 64, -10, -1, 1 },
88 { 1, -3, -5, 77, 67, -9, -1, 1 }, { 1, -3, -6, 75, 70, -8, -2, 1 },
89 { 1, -2, -7, 72, 72, -7, -2, 1 }, { 1, -2, -8, 70, 75, -6, -3, 1 },
90 { 1, -1, -9, 67, 77, -5, -3, 1 }, { 1, -1, -10, 64, 80, -4, -4, 2 },
91 { 1, -1, -11, 61, 82, -2, -4, 2 }, { 1, 0, -11, 58, 84, -1, -5, 2 },
92 { 1, 0, -12, 55, 86, 1, -5, 2 }, { 1, 0, -12, 52, 88, 3, -6, 2 },
93 { 0, 1, -12, 49, 90, 5, -7, 2 }, { 0, 1, -12, 46, 91, 7, -7, 2 },
94 { 1, 1, -12, 43, 92, 9, -8, 2 }, { 0, 1, -12, 40, 93, 12, -8, 2 },
95 { 0, 2, -12, 37, 94, 14, -9, 2 }, { 0, 2, -12, 34, 95, 17, -10, 2 },
96 { 0, 2, -11, 31, 95, 19, -10, 2 }, { 0, 2, -11, 28, 96, 22, -11, 2 }
97};
98
99// Filters for interpolation (0.875-band) - note this also filters integer pels.
Fergus Simpson090bb602017-04-07 12:37:53 -0700100static const interp_kernel filteredinterp_filters875[(1 << SUBPEL_BITS_RS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 { 3, -8, 13, 112, 13, -8, 3, 0 }, { 3, -7, 10, 112, 17, -9, 3, -1 },
102 { 2, -6, 7, 111, 21, -9, 3, -1 }, { 2, -5, 4, 111, 24, -10, 3, -1 },
103 { 2, -4, 1, 110, 28, -11, 3, -1 }, { 1, -3, -1, 108, 32, -12, 4, -1 },
104 { 1, -2, -3, 106, 36, -13, 4, -1 }, { 1, -1, -6, 105, 40, -14, 4, -1 },
105 { 1, -1, -7, 102, 44, -14, 4, -1 }, { 1, 0, -9, 100, 48, -15, 4, -1 },
106 { 1, 1, -11, 97, 53, -16, 4, -1 }, { 0, 1, -12, 95, 57, -16, 4, -1 },
107 { 0, 2, -13, 91, 61, -16, 4, -1 }, { 0, 2, -14, 88, 65, -16, 4, -1 },
108 { 0, 3, -15, 84, 69, -17, 4, 0 }, { 0, 3, -16, 81, 73, -16, 3, 0 },
109 { 0, 3, -16, 77, 77, -16, 3, 0 }, { 0, 3, -16, 73, 81, -16, 3, 0 },
110 { 0, 4, -17, 69, 84, -15, 3, 0 }, { -1, 4, -16, 65, 88, -14, 2, 0 },
111 { -1, 4, -16, 61, 91, -13, 2, 0 }, { -1, 4, -16, 57, 95, -12, 1, 0 },
112 { -1, 4, -16, 53, 97, -11, 1, 1 }, { -1, 4, -15, 48, 100, -9, 0, 1 },
113 { -1, 4, -14, 44, 102, -7, -1, 1 }, { -1, 4, -14, 40, 105, -6, -1, 1 },
114 { -1, 4, -13, 36, 106, -3, -2, 1 }, { -1, 4, -12, 32, 108, -1, -3, 1 },
115 { -1, 3, -11, 28, 110, 1, -4, 2 }, { -1, 3, -10, 24, 111, 4, -5, 2 },
116 { -1, 3, -9, 21, 111, 7, -6, 2 }, { -1, 3, -9, 17, 112, 10, -7, 3 }
117};
118
119// Filters for interpolation (full-band) - no filtering for integer pixels
Fergus Simpson090bb602017-04-07 12:37:53 -0700120static const interp_kernel filteredinterp_filters1000[(1 << SUBPEL_BITS_RS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 { 0, 0, 0, 128, 0, 0, 0, 0 }, { 0, 1, -3, 128, 3, -1, 0, 0 },
122 { -1, 2, -6, 127, 7, -2, 1, 0 }, { -1, 3, -9, 126, 12, -4, 1, 0 },
123 { -1, 4, -12, 125, 16, -5, 1, 0 }, { -1, 4, -14, 123, 20, -6, 2, 0 },
124 { -1, 5, -15, 120, 25, -8, 2, 0 }, { -1, 5, -17, 118, 30, -9, 3, -1 },
125 { -1, 6, -18, 114, 35, -10, 3, -1 }, { -1, 6, -19, 111, 41, -12, 3, -1 },
126 { -1, 6, -20, 107, 46, -13, 4, -1 }, { -1, 6, -21, 103, 52, -14, 4, -1 },
127 { -1, 6, -21, 99, 57, -16, 5, -1 }, { -1, 6, -21, 94, 63, -17, 5, -1 },
128 { -1, 6, -20, 89, 68, -18, 5, -1 }, { -1, 6, -20, 84, 73, -19, 6, -1 },
129 { -1, 6, -20, 79, 79, -20, 6, -1 }, { -1, 6, -19, 73, 84, -20, 6, -1 },
130 { -1, 5, -18, 68, 89, -20, 6, -1 }, { -1, 5, -17, 63, 94, -21, 6, -1 },
131 { -1, 5, -16, 57, 99, -21, 6, -1 }, { -1, 4, -14, 52, 103, -21, 6, -1 },
132 { -1, 4, -13, 46, 107, -20, 6, -1 }, { -1, 3, -12, 41, 111, -19, 6, -1 },
133 { -1, 3, -10, 35, 114, -18, 6, -1 }, { -1, 3, -9, 30, 118, -17, 5, -1 },
134 { 0, 2, -8, 25, 120, -15, 5, -1 }, { 0, 2, -6, 20, 123, -14, 4, -1 },
135 { 0, 1, -5, 16, 125, -12, 4, -1 }, { 0, 1, -4, 12, 126, -9, 3, -1 },
136 { 0, 1, -2, 7, 127, -6, 2, -1 }, { 0, 0, -1, 3, 128, -3, 1, 0 }
137};
138
139// Filters for factor of 2 downsampling.
Yaowu Xuf883b422016-08-30 14:01:10 -0700140static const int16_t av1_down2_symeven_half_filter[] = { 56, 12, -3, -1 };
141static const int16_t av1_down2_symodd_half_filter[] = { 64, 35, 0, -3 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142
143static const interp_kernel *choose_interp_filter(int inlength, int outlength) {
144 int outlength16 = outlength * 16;
145 if (outlength16 >= inlength * 16)
146 return filteredinterp_filters1000;
147 else if (outlength16 >= inlength * 13)
148 return filteredinterp_filters875;
149 else if (outlength16 >= inlength * 11)
150 return filteredinterp_filters750;
151 else if (outlength16 >= inlength * 9)
152 return filteredinterp_filters625;
153 else
154 return filteredinterp_filters500;
155}
156
157static void interpolate(const uint8_t *const input, int inlength,
158 uint8_t *output, int outlength) {
159 const int64_t delta =
160 (((uint64_t)inlength << 32) + outlength / 2) / outlength;
161 const int64_t offset =
162 inlength > outlength
163 ? (((int64_t)(inlength - outlength) << 31) + outlength / 2) /
164 outlength
165 : -(((int64_t)(outlength - inlength) << 31) + outlength / 2) /
166 outlength;
167 uint8_t *optr = output;
168 int x, x1, x2, sum, k, int_pel, sub_pel;
169 int64_t y;
170
171 const interp_kernel *interp_filters =
172 choose_interp_filter(inlength, outlength);
173
174 x = 0;
175 y = offset;
176 while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
177 x++;
178 y += delta;
179 }
180 x1 = x;
181 x = outlength - 1;
182 y = delta * x + offset;
183 while ((y >> INTERP_PRECISION_BITS) + (int64_t)(INTERP_TAPS / 2) >=
184 inlength) {
185 x--;
186 y -= delta;
187 }
188 x2 = x;
189 if (x1 > x2) {
190 for (x = 0, y = offset; x < outlength; ++x, y += delta) {
191 const int16_t *filter;
192 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700193 sub_pel =
194 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 filter = interp_filters[sub_pel];
196 sum = 0;
197 for (k = 0; k < INTERP_TAPS; ++k) {
198 const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200199 sum += filter[k] * input[AOMMAX(AOMMIN(pk, inlength - 1), 0)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200 }
201 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
202 }
203 } else {
204 // Initial part.
205 for (x = 0, y = offset; x < x1; ++x, y += delta) {
206 const int16_t *filter;
207 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700208 sub_pel =
209 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 filter = interp_filters[sub_pel];
211 sum = 0;
212 for (k = 0; k < INTERP_TAPS; ++k)
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200213 sum += filter[k] * input[AOMMAX(int_pel - INTERP_TAPS / 2 + 1 + k, 0)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
215 }
216 // Middle part.
217 for (; x <= x2; ++x, y += delta) {
218 const int16_t *filter;
219 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700220 sub_pel =
221 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 filter = interp_filters[sub_pel];
223 sum = 0;
224 for (k = 0; k < INTERP_TAPS; ++k)
225 sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
226 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
227 }
228 // End part.
229 for (; x < outlength; ++x, y += delta) {
230 const int16_t *filter;
231 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700232 sub_pel =
233 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234 filter = interp_filters[sub_pel];
235 sum = 0;
236 for (k = 0; k < INTERP_TAPS; ++k)
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200237 sum += filter[k] *
238 input[AOMMIN(int_pel - INTERP_TAPS / 2 + 1 + k, inlength - 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700239 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
240 }
241 }
242}
243
Sebastien Alaiwan92400ab2017-03-14 10:39:29 +0100244#ifndef __clang_analyzer__
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245static void down2_symeven(const uint8_t *const input, int length,
246 uint8_t *output) {
247 // Actual filter len = 2 * filter_len_half.
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 const int16_t *filter = av1_down2_symeven_half_filter;
249 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 int i, j;
251 uint8_t *optr = output;
252 int l1 = filter_len_half;
253 int l2 = (length - filter_len_half);
254 l1 += (l1 & 1);
255 l2 += (l2 & 1);
256 if (l1 > l2) {
257 // Short input length.
258 for (i = 0; i < length; i += 2) {
259 int sum = (1 << (FILTER_BITS - 1));
260 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200261 sum +=
262 (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + 1 + j, length - 1)]) *
263 filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264 }
265 sum >>= FILTER_BITS;
266 *optr++ = clip_pixel(sum);
267 }
268 } else {
269 // Initial part.
270 for (i = 0; i < l1; i += 2) {
271 int sum = (1 << (FILTER_BITS - 1));
272 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200273 sum += (input[AOMMAX(i - j, 0)] + input[i + 1 + j]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 }
275 sum >>= FILTER_BITS;
276 *optr++ = clip_pixel(sum);
277 }
278 // Middle part.
279 for (; i < l2; i += 2) {
280 int sum = (1 << (FILTER_BITS - 1));
281 for (j = 0; j < filter_len_half; ++j) {
282 sum += (input[i - j] + input[i + 1 + j]) * filter[j];
283 }
284 sum >>= FILTER_BITS;
285 *optr++ = clip_pixel(sum);
286 }
287 // End part.
288 for (; i < length; i += 2) {
289 int sum = (1 << (FILTER_BITS - 1));
290 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200291 sum +=
292 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293 }
294 sum >>= FILTER_BITS;
295 *optr++ = clip_pixel(sum);
296 }
297 }
298}
Sebastien Alaiwan92400ab2017-03-14 10:39:29 +0100299#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300
301static void down2_symodd(const uint8_t *const input, int length,
302 uint8_t *output) {
303 // Actual filter len = 2 * filter_len_half - 1.
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 const int16_t *filter = av1_down2_symodd_half_filter;
305 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 int i, j;
307 uint8_t *optr = output;
308 int l1 = filter_len_half - 1;
309 int l2 = (length - filter_len_half + 1);
310 l1 += (l1 & 1);
311 l2 += (l2 & 1);
312 if (l1 > l2) {
313 // Short input length.
314 for (i = 0; i < length; i += 2) {
315 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
316 for (j = 1; j < filter_len_half; ++j) {
317 sum += (input[(i - j < 0 ? 0 : i - j)] +
318 input[(i + j >= length ? length - 1 : i + j)]) *
319 filter[j];
320 }
321 sum >>= FILTER_BITS;
322 *optr++ = clip_pixel(sum);
323 }
324 } else {
325 // Initial part.
326 for (i = 0; i < l1; i += 2) {
327 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
328 for (j = 1; j < filter_len_half; ++j) {
329 sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
330 }
331 sum >>= FILTER_BITS;
332 *optr++ = clip_pixel(sum);
333 }
334 // Middle part.
335 for (; i < l2; i += 2) {
336 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
337 for (j = 1; j < filter_len_half; ++j) {
338 sum += (input[i - j] + input[i + j]) * filter[j];
339 }
340 sum >>= FILTER_BITS;
341 *optr++ = clip_pixel(sum);
342 }
343 // End part.
344 for (; i < length; i += 2) {
345 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
346 for (j = 1; j < filter_len_half; ++j) {
347 sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
348 filter[j];
349 }
350 sum >>= FILTER_BITS;
351 *optr++ = clip_pixel(sum);
352 }
353 }
354}
355
356static int get_down2_length(int length, int steps) {
357 int s;
358 for (s = 0; s < steps; ++s) length = (length + 1) >> 1;
359 return length;
360}
361
362static int get_down2_steps(int in_length, int out_length) {
363 int steps = 0;
364 int proj_in_length;
365 while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
366 ++steps;
367 in_length = proj_in_length;
368 }
369 return steps;
370}
371
372static void resize_multistep(const uint8_t *const input, int length,
373 uint8_t *output, int olength, uint8_t *otmp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 if (length == olength) {
375 memcpy(output, input, sizeof(output[0]) * length);
376 return;
377 }
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200378 const int steps = get_down2_steps(length, olength);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379
380 if (steps > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 uint8_t *out = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 int filteredlength = length;
383
384 assert(otmp != NULL);
Sebastien Alaiwane4ba6aa2017-05-04 14:18:30 +0200385 uint8_t *otmp2 = otmp + get_down2_length(length, 1);
386 for (int s = 0; s < steps; ++s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387 const int proj_filteredlength = get_down2_length(filteredlength, 1);
388 const uint8_t *const in = (s == 0 ? input : out);
389 if (s == steps - 1 && proj_filteredlength == olength)
390 out = output;
391 else
392 out = (s & 1 ? otmp2 : otmp);
393 if (filteredlength & 1)
394 down2_symodd(in, filteredlength, out);
395 else
396 down2_symeven(in, filteredlength, out);
397 filteredlength = proj_filteredlength;
398 }
399 if (filteredlength != olength) {
400 interpolate(out, filteredlength, output, olength);
401 }
402 } else {
403 interpolate(input, length, output, olength);
404 }
405}
406
407static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
408 int i;
409 uint8_t *iptr = img;
410 uint8_t *aptr = arr;
411 for (i = 0; i < len; ++i, iptr += stride) {
412 *aptr++ = *iptr;
413 }
414}
415
416static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
417 int i;
418 uint8_t *iptr = img;
419 uint8_t *aptr = arr;
420 for (i = 0; i < len; ++i, iptr += stride) {
421 *iptr = *aptr++;
422 }
423}
424
Yaowu Xuf883b422016-08-30 14:01:10 -0700425void av1_resize_plane(const uint8_t *const input, int height, int width,
426 int in_stride, uint8_t *output, int height2, int width2,
427 int out_stride) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 int i;
429 uint8_t *intbuf = (uint8_t *)malloc(sizeof(uint8_t) * width2 * height);
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200430 uint8_t *tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) * AOMMAX(width, height));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431 uint8_t *arrbuf = (uint8_t *)malloc(sizeof(uint8_t) * height);
432 uint8_t *arrbuf2 = (uint8_t *)malloc(sizeof(uint8_t) * height2);
433 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
434 goto Error;
435 assert(width > 0);
436 assert(height > 0);
437 assert(width2 > 0);
438 assert(height2 > 0);
439 for (i = 0; i < height; ++i)
440 resize_multistep(input + in_stride * i, width, intbuf + width2 * i, width2,
441 tmpbuf);
442 for (i = 0; i < width2; ++i) {
443 fill_col_to_arr(intbuf + i, width2, height, arrbuf);
444 resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf);
445 fill_arr_to_col(output + i, out_stride, height2, arrbuf2);
446 }
447
448Error:
449 free(intbuf);
450 free(tmpbuf);
451 free(arrbuf);
452 free(arrbuf2);
453}
454
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200455#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456static void highbd_interpolate(const uint16_t *const input, int inlength,
457 uint16_t *output, int outlength, int bd) {
458 const int64_t delta =
459 (((uint64_t)inlength << 32) + outlength / 2) / outlength;
460 const int64_t offset =
461 inlength > outlength
462 ? (((int64_t)(inlength - outlength) << 31) + outlength / 2) /
463 outlength
464 : -(((int64_t)(outlength - inlength) << 31) + outlength / 2) /
465 outlength;
466 uint16_t *optr = output;
467 int x, x1, x2, sum, k, int_pel, sub_pel;
468 int64_t y;
469
470 const interp_kernel *interp_filters =
471 choose_interp_filter(inlength, outlength);
472
473 x = 0;
474 y = offset;
475 while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
476 x++;
477 y += delta;
478 }
479 x1 = x;
480 x = outlength - 1;
481 y = delta * x + offset;
482 while ((y >> INTERP_PRECISION_BITS) + (int64_t)(INTERP_TAPS / 2) >=
483 inlength) {
484 x--;
485 y -= delta;
486 }
487 x2 = x;
488 if (x1 > x2) {
489 for (x = 0, y = offset; x < outlength; ++x, y += delta) {
490 const int16_t *filter;
491 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700492 sub_pel =
493 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700494 filter = interp_filters[sub_pel];
495 sum = 0;
496 for (k = 0; k < INTERP_TAPS; ++k) {
497 const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200498 sum += filter[k] * input[AOMMAX(AOMMIN(pk, inlength - 1), 0)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499 }
500 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
501 }
502 } else {
503 // Initial part.
504 for (x = 0, y = offset; x < x1; ++x, y += delta) {
505 const int16_t *filter;
506 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700507 sub_pel =
508 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509 filter = interp_filters[sub_pel];
510 sum = 0;
511 for (k = 0; k < INTERP_TAPS; ++k)
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200512 sum += filter[k] * input[AOMMAX(int_pel - INTERP_TAPS / 2 + 1 + k, 0)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
514 }
515 // Middle part.
516 for (; x <= x2; ++x, y += delta) {
517 const int16_t *filter;
518 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700519 sub_pel =
520 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 filter = interp_filters[sub_pel];
522 sum = 0;
523 for (k = 0; k < INTERP_TAPS; ++k)
524 sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
525 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
526 }
527 // End part.
528 for (; x < outlength; ++x, y += delta) {
529 const int16_t *filter;
530 int_pel = y >> INTERP_PRECISION_BITS;
Fergus Simpson090bb602017-04-07 12:37:53 -0700531 sub_pel =
532 (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS_RS)) & SUBPEL_MASK_RS;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 filter = interp_filters[sub_pel];
534 sum = 0;
535 for (k = 0; k < INTERP_TAPS; ++k)
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200536 sum += filter[k] *
537 input[AOMMIN(int_pel - INTERP_TAPS / 2 + 1 + k, inlength - 1)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
539 }
540 }
541}
Sebastien Alaiwan92400ab2017-03-14 10:39:29 +0100542#ifndef __clang_analyzer__
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543static void highbd_down2_symeven(const uint16_t *const input, int length,
544 uint16_t *output, int bd) {
545 // Actual filter len = 2 * filter_len_half.
Yaowu Xuf883b422016-08-30 14:01:10 -0700546 static const int16_t *filter = av1_down2_symeven_half_filter;
547 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548 int i, j;
549 uint16_t *optr = output;
550 int l1 = filter_len_half;
551 int l2 = (length - filter_len_half);
552 l1 += (l1 & 1);
553 l2 += (l2 & 1);
554 if (l1 > l2) {
555 // Short input length.
556 for (i = 0; i < length; i += 2) {
557 int sum = (1 << (FILTER_BITS - 1));
558 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200559 sum +=
560 (input[AOMMAX(0, i - j)] + input[AOMMIN(i + 1 + j, length - 1)]) *
561 filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 }
563 sum >>= FILTER_BITS;
564 *optr++ = clip_pixel_highbd(sum, bd);
565 }
566 } else {
567 // Initial part.
568 for (i = 0; i < l1; i += 2) {
569 int sum = (1 << (FILTER_BITS - 1));
570 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200571 sum += (input[AOMMAX(0, i - j)] + input[i + 1 + j]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572 }
573 sum >>= FILTER_BITS;
574 *optr++ = clip_pixel_highbd(sum, bd);
575 }
576 // Middle part.
577 for (; i < l2; i += 2) {
578 int sum = (1 << (FILTER_BITS - 1));
579 for (j = 0; j < filter_len_half; ++j) {
580 sum += (input[i - j] + input[i + 1 + j]) * filter[j];
581 }
582 sum >>= FILTER_BITS;
583 *optr++ = clip_pixel_highbd(sum, bd);
584 }
585 // End part.
586 for (; i < length; i += 2) {
587 int sum = (1 << (FILTER_BITS - 1));
588 for (j = 0; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200589 sum +=
590 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591 }
592 sum >>= FILTER_BITS;
593 *optr++ = clip_pixel_highbd(sum, bd);
594 }
595 }
596}
597
598static void highbd_down2_symodd(const uint16_t *const input, int length,
599 uint16_t *output, int bd) {
600 // Actual filter len = 2 * filter_len_half - 1.
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 static const int16_t *filter = av1_down2_symodd_half_filter;
602 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 int i, j;
604 uint16_t *optr = output;
605 int l1 = filter_len_half - 1;
606 int l2 = (length - filter_len_half + 1);
607 l1 += (l1 & 1);
608 l2 += (l2 & 1);
609 if (l1 > l2) {
610 // Short input length.
611 for (i = 0; i < length; i += 2) {
612 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
613 for (j = 1; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200614 sum += (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + j, length - 1)]) *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615 filter[j];
616 }
617 sum >>= FILTER_BITS;
618 *optr++ = clip_pixel_highbd(sum, bd);
619 }
620 } else {
621 // Initial part.
622 for (i = 0; i < l1; i += 2) {
623 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
624 for (j = 1; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200625 sum += (input[AOMMAX(i - j, 0)] + input[i + j]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 }
627 sum >>= FILTER_BITS;
628 *optr++ = clip_pixel_highbd(sum, bd);
629 }
630 // Middle part.
631 for (; i < l2; i += 2) {
632 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
633 for (j = 1; j < filter_len_half; ++j) {
634 sum += (input[i - j] + input[i + j]) * filter[j];
635 }
636 sum >>= FILTER_BITS;
637 *optr++ = clip_pixel_highbd(sum, bd);
638 }
639 // End part.
640 for (; i < length; i += 2) {
641 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
642 for (j = 1; j < filter_len_half; ++j) {
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200643 sum += (input[i - j] + input[AOMMIN(i + j, length - 1)]) * filter[j];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700644 }
645 sum >>= FILTER_BITS;
646 *optr++ = clip_pixel_highbd(sum, bd);
647 }
648 }
649}
Sebastien Alaiwan92400ab2017-03-14 10:39:29 +0100650#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651
652static void highbd_resize_multistep(const uint16_t *const input, int length,
653 uint16_t *output, int olength,
654 uint16_t *otmp, int bd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 if (length == olength) {
656 memcpy(output, input, sizeof(output[0]) * length);
657 return;
658 }
Sebastien Alaiwane4ba6aa2017-05-04 14:18:30 +0200659 const int steps = get_down2_steps(length, olength);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660
661 if (steps > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662 uint16_t *out = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663 int filteredlength = length;
664
665 assert(otmp != NULL);
Sebastien Alaiwane4ba6aa2017-05-04 14:18:30 +0200666 uint16_t *otmp2 = otmp + get_down2_length(length, 1);
667 for (int s = 0; s < steps; ++s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 const int proj_filteredlength = get_down2_length(filteredlength, 1);
669 const uint16_t *const in = (s == 0 ? input : out);
670 if (s == steps - 1 && proj_filteredlength == olength)
671 out = output;
672 else
673 out = (s & 1 ? otmp2 : otmp);
674 if (filteredlength & 1)
675 highbd_down2_symodd(in, filteredlength, out, bd);
676 else
677 highbd_down2_symeven(in, filteredlength, out, bd);
678 filteredlength = proj_filteredlength;
679 }
680 if (filteredlength != olength) {
681 highbd_interpolate(out, filteredlength, output, olength, bd);
682 }
683 } else {
684 highbd_interpolate(input, length, output, olength, bd);
685 }
686}
687
688static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
689 uint16_t *arr) {
690 int i;
691 uint16_t *iptr = img;
692 uint16_t *aptr = arr;
693 for (i = 0; i < len; ++i, iptr += stride) {
694 *aptr++ = *iptr;
695 }
696}
697
698static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
699 uint16_t *arr) {
700 int i;
701 uint16_t *iptr = img;
702 uint16_t *aptr = arr;
703 for (i = 0; i < len; ++i, iptr += stride) {
704 *iptr = *aptr++;
705 }
706}
707
Yaowu Xuf883b422016-08-30 14:01:10 -0700708void av1_highbd_resize_plane(const uint8_t *const input, int height, int width,
709 int in_stride, uint8_t *output, int height2,
710 int width2, int out_stride, int bd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711 int i;
712 uint16_t *intbuf = (uint16_t *)malloc(sizeof(uint16_t) * width2 * height);
713 uint16_t *tmpbuf =
Sebastien Alaiwan0b95a6e2017-05-04 13:03:35 +0200714 (uint16_t *)malloc(sizeof(uint16_t) * AOMMAX(width, height));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715 uint16_t *arrbuf = (uint16_t *)malloc(sizeof(uint16_t) * height);
716 uint16_t *arrbuf2 = (uint16_t *)malloc(sizeof(uint16_t) * height2);
717 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
718 goto Error;
719 for (i = 0; i < height; ++i) {
720 highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
721 intbuf + width2 * i, width2, tmpbuf, bd);
722 }
723 for (i = 0; i < width2; ++i) {
724 highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
725 highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
726 highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
727 arrbuf2);
728 }
729
730Error:
731 free(intbuf);
732 free(tmpbuf);
733 free(arrbuf);
734 free(arrbuf2);
735}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200736#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737
Yaowu Xuf883b422016-08-30 14:01:10 -0700738void av1_resize_frame420(const uint8_t *const y, int y_stride,
739 const uint8_t *const u, const uint8_t *const v,
740 int uv_stride, int height, int width, uint8_t *oy,
741 int oy_stride, uint8_t *ou, uint8_t *ov,
742 int ouv_stride, int oheight, int owidth) {
743 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
744 av1_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
745 owidth / 2, ouv_stride);
746 av1_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
747 owidth / 2, ouv_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748}
749
Yaowu Xuf883b422016-08-30 14:01:10 -0700750void av1_resize_frame422(const uint8_t *const y, int y_stride,
751 const uint8_t *const u, const uint8_t *const v,
752 int uv_stride, int height, int width, uint8_t *oy,
753 int oy_stride, uint8_t *ou, uint8_t *ov,
754 int ouv_stride, int oheight, int owidth) {
755 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
756 av1_resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2,
757 ouv_stride);
758 av1_resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2,
759 ouv_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760}
761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762void av1_resize_frame444(const uint8_t *const y, int y_stride,
763 const uint8_t *const u, const uint8_t *const v,
764 int uv_stride, int height, int width, uint8_t *oy,
765 int oy_stride, uint8_t *ou, uint8_t *ov,
766 int ouv_stride, int oheight, int owidth) {
767 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
768 av1_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
769 ouv_stride);
770 av1_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
771 ouv_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772}
773
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200774#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700775void av1_highbd_resize_frame420(const uint8_t *const y, int y_stride,
776 const uint8_t *const u, const uint8_t *const v,
777 int uv_stride, int height, int width,
778 uint8_t *oy, int oy_stride, uint8_t *ou,
779 uint8_t *ov, int ouv_stride, int oheight,
780 int owidth, int bd) {
781 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
782 oy_stride, bd);
783 av1_highbd_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
784 owidth / 2, ouv_stride, bd);
785 av1_highbd_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
786 owidth / 2, ouv_stride, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787}
788
Yaowu Xuf883b422016-08-30 14:01:10 -0700789void av1_highbd_resize_frame422(const uint8_t *const y, int y_stride,
790 const uint8_t *const u, const uint8_t *const v,
791 int uv_stride, int height, int width,
792 uint8_t *oy, int oy_stride, uint8_t *ou,
793 uint8_t *ov, int ouv_stride, int oheight,
794 int owidth, int bd) {
795 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
796 oy_stride, bd);
797 av1_highbd_resize_plane(u, height, width / 2, uv_stride, ou, oheight,
798 owidth / 2, ouv_stride, bd);
799 av1_highbd_resize_plane(v, height, width / 2, uv_stride, ov, oheight,
800 owidth / 2, ouv_stride, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801}
802
Yaowu Xuf883b422016-08-30 14:01:10 -0700803void av1_highbd_resize_frame444(const uint8_t *const y, int y_stride,
804 const uint8_t *const u, const uint8_t *const v,
805 int uv_stride, int height, int width,
806 uint8_t *oy, int oy_stride, uint8_t *ou,
807 uint8_t *ov, int ouv_stride, int oheight,
808 int owidth, int bd) {
809 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
810 oy_stride, bd);
811 av1_highbd_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
812 ouv_stride, bd);
813 av1_highbd_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
814 ouv_stride, bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700815}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200816#endif // CONFIG_HIGHBITDEPTH
Fergus Simpson090bb602017-04-07 12:37:53 -0700817
818#if CONFIG_HIGHBITDEPTH
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700819void av1_resize_and_extend_frame(const YV12_BUFFER_CONFIG *src,
820 YV12_BUFFER_CONFIG *dst, int bd) {
Fergus Simpson090bb602017-04-07 12:37:53 -0700821#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700822void av1_resize_and_extend_frame(const YV12_BUFFER_CONFIG *src,
823 YV12_BUFFER_CONFIG *dst) {
Fergus Simpson090bb602017-04-07 12:37:53 -0700824#endif // CONFIG_HIGHBITDEPTH
825 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with aom_image_t
826 int i;
827 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
828 src->v_buffer };
829 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
830 const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
831 src->uv_crop_width };
832 const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
833 src->uv_crop_height };
834 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
835 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
836 const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
837 dst->uv_crop_width };
838 const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
839 dst->uv_crop_height };
840
841 for (i = 0; i < MAX_MB_PLANE; ++i) {
842#if CONFIG_HIGHBITDEPTH
843 if (src->flags & YV12_FLAG_HIGHBITDEPTH)
844 av1_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
845 src_strides[i], dsts[i], dst_heights[i],
846 dst_widths[i], dst_strides[i], bd);
847 else
848#endif // CONFIG_HIGHBITDEPTH
849 av1_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
850 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
851 }
852 aom_extend_frame_borders(dst);
853}
854
855YV12_BUFFER_CONFIG *av1_scale_if_required_fast(AV1_COMMON *cm,
856 YV12_BUFFER_CONFIG *unscaled,
857 YV12_BUFFER_CONFIG *scaled) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700858 if (cm->width != unscaled->y_crop_width ||
859 cm->height != unscaled->y_crop_height) {
Fergus Simpson090bb602017-04-07 12:37:53 -0700860 // For 2x2 scaling down.
861 aom_scale_frame(unscaled, scaled, unscaled->y_buffer, 9, 2, 1, 2, 1, 0);
862 aom_extend_frame_borders(scaled);
863 return scaled;
864 } else {
865 return unscaled;
866 }
867}
868
869YV12_BUFFER_CONFIG *av1_scale_if_required(AV1_COMMON *cm,
870 YV12_BUFFER_CONFIG *unscaled,
871 YV12_BUFFER_CONFIG *scaled) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700872 if (cm->width != unscaled->y_crop_width ||
873 cm->height != unscaled->y_crop_height) {
Fergus Simpson090bb602017-04-07 12:37:53 -0700874#if CONFIG_HIGHBITDEPTH
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700875 av1_resize_and_extend_frame(unscaled, scaled, (int)cm->bit_depth);
Fergus Simpson090bb602017-04-07 12:37:53 -0700876#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700877 av1_resize_and_extend_frame(unscaled, scaled);
Fergus Simpson090bb602017-04-07 12:37:53 -0700878#endif // CONFIG_HIGHBITDEPTH
879 return scaled;
880 } else {
881 return unscaled;
882 }
883}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700884
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700885void av1_calculate_scaled_size(int *width, int *height, int num, int den) {
886 *width = *width * num / den;
887 *height = *height * num / den;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700888}
889
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700890#if CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700891// TODO(afergs): Look for in-place upscaling
892// TODO(afergs): aom_ vs av1_ functions? Which can I use?
893// Upscale decoded image.
894void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool) {
895 if (av1_superres_unscaled(cm)) return;
896
897 YV12_BUFFER_CONFIG copy_buffer;
898 memset(&copy_buffer, 0, sizeof(copy_buffer));
899
900 YV12_BUFFER_CONFIG *const frame_to_show = get_frame_new_buffer(cm);
901
902 if (aom_alloc_frame_buffer(&copy_buffer, cm->width, cm->height,
903 cm->subsampling_x, cm->subsampling_y,
David Barker26762352017-06-13 14:11:39 +0100904#if CONFIG_HIGHBITDEPTH
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700905 cm->use_highbitdepth,
906#endif // CONFIG_HIGHBITDEPTH
907 AOM_BORDER_IN_PIXELS, cm->byte_alignment))
908 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
909 "Failed to allocate copy buffer for superres upscaling");
910
911 // Copy function assumes the frames are the same size, doesn't copy bit_depth.
912 aom_yv12_copy_frame(frame_to_show, &copy_buffer);
913 copy_buffer.bit_depth = frame_to_show->bit_depth;
914 assert(copy_buffer.y_crop_width == cm->width);
915 assert(copy_buffer.y_crop_height == cm->height);
916
917 // Realloc the current frame buffer at a higher resolution in place.
918 if (pool != NULL) {
919 // Use callbacks if on the decoder.
920 aom_codec_frame_buffer_t *fb =
921 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer;
922 aom_release_frame_buffer_cb_fn_t release_fb_cb = pool->release_fb_cb;
923 aom_get_frame_buffer_cb_fn_t cb = pool->get_fb_cb;
924 void *cb_priv = pool->cb_priv;
925
926 // Realloc with callback does not release the frame buffer - release first.
927 if (release_fb_cb(cb_priv, fb))
928 aom_internal_error(
929 &cm->error, AOM_CODEC_MEM_ERROR,
930 "Failed to free current frame buffer before superres upscaling");
931
932 if (aom_realloc_frame_buffer(
933 frame_to_show, cm->superres_upscaled_width,
934 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
David Barker26762352017-06-13 14:11:39 +0100935#if CONFIG_HIGHBITDEPTH
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700936 cm->use_highbitdepth,
937#endif // CONFIG_HIGHBITDEPTH
938 AOM_BORDER_IN_PIXELS, cm->byte_alignment, fb, cb, cb_priv))
939 aom_internal_error(
940 &cm->error, AOM_CODEC_MEM_ERROR,
941 "Failed to allocate current frame buffer for superres upscaling");
942 } else {
943 // Don't use callbacks on the encoder.
944 if (aom_alloc_frame_buffer(frame_to_show, cm->superres_upscaled_width,
945 cm->superres_upscaled_height, cm->subsampling_x,
946 cm->subsampling_y,
David Barker26762352017-06-13 14:11:39 +0100947#if CONFIG_HIGHBITDEPTH
Fergus Simpsond2bcbb52017-05-22 23:15:05 -0700948 cm->use_highbitdepth,
949#endif // CONFIG_HIGHBITDEPTH
950 AOM_BORDER_IN_PIXELS, cm->byte_alignment))
951 aom_internal_error(
952 &cm->error, AOM_CODEC_MEM_ERROR,
953 "Failed to reallocate current frame buffer for superres upscaling");
954 }
955 // TODO(afergs): verify frame_to_show is correct after realloc
956 // encoder:
957 // decoder:
958 frame_to_show->bit_depth = copy_buffer.bit_depth;
959 assert(frame_to_show->y_crop_width == cm->superres_upscaled_width);
960 assert(frame_to_show->y_crop_height == cm->superres_upscaled_height);
961
962 // Scale up and back into frame_to_show.
963 assert(frame_to_show->y_crop_width != cm->width);
964 assert(frame_to_show->y_crop_height != cm->height);
965#if CONFIG_HIGHBITDEPTH
966 av1_resize_and_extend_frame(&copy_buffer, frame_to_show, (int)cm->bit_depth);
967#else
968 av1_resize_and_extend_frame(&copy_buffer, frame_to_show);
969#endif // CONFIG_HIGHBITDEPTH
970
971 // Free the copy buffer
972 aom_free_frame_buffer(&copy_buffer);
973}
974#endif // CONFIG_FRAME_SUPERRES