1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * 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.
10 */
11
12 #include <assert.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdbool.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "config/aom_config.h"
21 #include "config/av1_rtcd.h"
22
23 #include "aom_dsp/aom_dsp_common.h"
24 #include "aom_dsp/flow_estimation/corner_detect.h"
25 #include "aom_ports/mem.h"
26 #include "av1/common/common.h"
27 #include "av1/common/resize.h"
28
29 #include "config/aom_dsp_rtcd.h"
30 #include "config/aom_scale_rtcd.h"
31
32 // Filters for interpolation (0.5-band) - note this also filters integer pels.
33 static const InterpKernel filteredinterp_filters500[(1 << RS_SUBPEL_BITS)] = {
34 { -3, 0, 35, 64, 35, 0, -3, 0 }, { -3, 0, 34, 64, 36, 0, -3, 0 },
35 { -3, -1, 34, 64, 36, 1, -3, 0 }, { -3, -1, 33, 64, 37, 1, -3, 0 },
36 { -3, -1, 32, 64, 38, 1, -3, 0 }, { -3, -1, 31, 64, 39, 1, -3, 0 },
37 { -3, -1, 31, 63, 39, 2, -3, 0 }, { -2, -2, 30, 63, 40, 2, -3, 0 },
38 { -2, -2, 29, 63, 41, 2, -3, 0 }, { -2, -2, 29, 63, 41, 3, -4, 0 },
39 { -2, -2, 28, 63, 42, 3, -4, 0 }, { -2, -2, 27, 63, 43, 3, -4, 0 },
40 { -2, -3, 27, 63, 43, 4, -4, 0 }, { -2, -3, 26, 62, 44, 5, -4, 0 },
41 { -2, -3, 25, 62, 45, 5, -4, 0 }, { -2, -3, 25, 62, 45, 5, -4, 0 },
42 { -2, -3, 24, 62, 46, 5, -4, 0 }, { -2, -3, 23, 61, 47, 6, -4, 0 },
43 { -2, -3, 23, 61, 47, 6, -4, 0 }, { -2, -3, 22, 61, 48, 7, -4, -1 },
44 { -2, -3, 21, 60, 49, 7, -4, 0 }, { -1, -4, 20, 60, 49, 8, -4, 0 },
45 { -1, -4, 20, 60, 50, 8, -4, -1 }, { -1, -4, 19, 59, 51, 9, -4, -1 },
46 { -1, -4, 19, 59, 51, 9, -4, -1 }, { -1, -4, 18, 58, 52, 10, -4, -1 },
47 { -1, -4, 17, 58, 52, 11, -4, -1 }, { -1, -4, 16, 58, 53, 11, -4, -1 },
48 { -1, -4, 16, 57, 53, 12, -4, -1 }, { -1, -4, 15, 57, 54, 12, -4, -1 },
49 { -1, -4, 15, 56, 54, 13, -4, -1 }, { -1, -4, 14, 56, 55, 13, -4, -1 },
50 { -1, -4, 14, 55, 55, 14, -4, -1 }, { -1, -4, 13, 55, 56, 14, -4, -1 },
51 { -1, -4, 13, 54, 56, 15, -4, -1 }, { -1, -4, 12, 54, 57, 15, -4, -1 },
52 { -1, -4, 12, 53, 57, 16, -4, -1 }, { -1, -4, 11, 53, 58, 16, -4, -1 },
53 { -1, -4, 11, 52, 58, 17, -4, -1 }, { -1, -4, 10, 52, 58, 18, -4, -1 },
54 { -1, -4, 9, 51, 59, 19, -4, -1 }, { -1, -4, 9, 51, 59, 19, -4, -1 },
55 { -1, -4, 8, 50, 60, 20, -4, -1 }, { 0, -4, 8, 49, 60, 20, -4, -1 },
56 { 0, -4, 7, 49, 60, 21, -3, -2 }, { -1, -4, 7, 48, 61, 22, -3, -2 },
57 { 0, -4, 6, 47, 61, 23, -3, -2 }, { 0, -4, 6, 47, 61, 23, -3, -2 },
58 { 0, -4, 5, 46, 62, 24, -3, -2 }, { 0, -4, 5, 45, 62, 25, -3, -2 },
59 { 0, -4, 5, 45, 62, 25, -3, -2 }, { 0, -4, 5, 44, 62, 26, -3, -2 },
60 { 0, -4, 4, 43, 63, 27, -3, -2 }, { 0, -4, 3, 43, 63, 27, -2, -2 },
61 { 0, -4, 3, 42, 63, 28, -2, -2 }, { 0, -4, 3, 41, 63, 29, -2, -2 },
62 { 0, -3, 2, 41, 63, 29, -2, -2 }, { 0, -3, 2, 40, 63, 30, -2, -2 },
63 { 0, -3, 2, 39, 63, 31, -1, -3 }, { 0, -3, 1, 39, 64, 31, -1, -3 },
64 { 0, -3, 1, 38, 64, 32, -1, -3 }, { 0, -3, 1, 37, 64, 33, -1, -3 },
65 { 0, -3, 1, 36, 64, 34, -1, -3 }, { 0, -3, 0, 36, 64, 34, 0, -3 },
66 };
67
68 // Filters for interpolation (0.625-band) - note this also filters integer pels.
69 static const InterpKernel filteredinterp_filters625[(1 << RS_SUBPEL_BITS)] = {
70 { -1, -8, 33, 80, 33, -8, -1, 0 }, { -1, -8, 31, 80, 34, -8, -1, 1 },
71 { -1, -8, 30, 80, 35, -8, -1, 1 }, { -1, -8, 29, 80, 36, -7, -2, 1 },
72 { -1, -8, 28, 80, 37, -7, -2, 1 }, { -1, -8, 27, 80, 38, -7, -2, 1 },
73 { 0, -8, 26, 79, 39, -7, -2, 1 }, { 0, -8, 25, 79, 40, -7, -2, 1 },
74 { 0, -8, 24, 79, 41, -7, -2, 1 }, { 0, -8, 23, 78, 42, -6, -2, 1 },
75 { 0, -8, 22, 78, 43, -6, -2, 1 }, { 0, -8, 21, 78, 44, -6, -2, 1 },
76 { 0, -8, 20, 78, 45, -5, -3, 1 }, { 0, -8, 19, 77, 47, -5, -3, 1 },
77 { 0, -8, 18, 77, 48, -5, -3, 1 }, { 0, -8, 17, 77, 49, -5, -3, 1 },
78 { 0, -8, 16, 76, 50, -4, -3, 1 }, { 0, -8, 15, 76, 51, -4, -3, 1 },
79 { 0, -8, 15, 75, 52, -3, -4, 1 }, { 0, -7, 14, 74, 53, -3, -4, 1 },
80 { 0, -7, 13, 74, 54, -3, -4, 1 }, { 0, -7, 12, 73, 55, -2, -4, 1 },
81 { 0, -7, 11, 73, 56, -2, -4, 1 }, { 0, -7, 10, 72, 57, -1, -4, 1 },
82 { 1, -7, 10, 71, 58, -1, -5, 1 }, { 0, -7, 9, 71, 59, 0, -5, 1 },
83 { 1, -7, 8, 70, 60, 0, -5, 1 }, { 1, -7, 7, 69, 61, 1, -5, 1 },
84 { 1, -6, 6, 68, 62, 1, -5, 1 }, { 0, -6, 6, 68, 62, 2, -5, 1 },
85 { 1, -6, 5, 67, 63, 2, -5, 1 }, { 1, -6, 5, 66, 64, 3, -6, 1 },
86 { 1, -6, 4, 65, 65, 4, -6, 1 }, { 1, -6, 3, 64, 66, 5, -6, 1 },
87 { 1, -5, 2, 63, 67, 5, -6, 1 }, { 1, -5, 2, 62, 68, 6, -6, 0 },
88 { 1, -5, 1, 62, 68, 6, -6, 1 }, { 1, -5, 1, 61, 69, 7, -7, 1 },
89 { 1, -5, 0, 60, 70, 8, -7, 1 }, { 1, -5, 0, 59, 71, 9, -7, 0 },
90 { 1, -5, -1, 58, 71, 10, -7, 1 }, { 1, -4, -1, 57, 72, 10, -7, 0 },
91 { 1, -4, -2, 56, 73, 11, -7, 0 }, { 1, -4, -2, 55, 73, 12, -7, 0 },
92 { 1, -4, -3, 54, 74, 13, -7, 0 }, { 1, -4, -3, 53, 74, 14, -7, 0 },
93 { 1, -4, -3, 52, 75, 15, -8, 0 }, { 1, -3, -4, 51, 76, 15, -8, 0 },
94 { 1, -3, -4, 50, 76, 16, -8, 0 }, { 1, -3, -5, 49, 77, 17, -8, 0 },
95 { 1, -3, -5, 48, 77, 18, -8, 0 }, { 1, -3, -5, 47, 77, 19, -8, 0 },
96 { 1, -3, -5, 45, 78, 20, -8, 0 }, { 1, -2, -6, 44, 78, 21, -8, 0 },
97 { 1, -2, -6, 43, 78, 22, -8, 0 }, { 1, -2, -6, 42, 78, 23, -8, 0 },
98 { 1, -2, -7, 41, 79, 24, -8, 0 }, { 1, -2, -7, 40, 79, 25, -8, 0 },
99 { 1, -2, -7, 39, 79, 26, -8, 0 }, { 1, -2, -7, 38, 80, 27, -8, -1 },
100 { 1, -2, -7, 37, 80, 28, -8, -1 }, { 1, -2, -7, 36, 80, 29, -8, -1 },
101 { 1, -1, -8, 35, 80, 30, -8, -1 }, { 1, -1, -8, 34, 80, 31, -8, -1 },
102 };
103
104 // Filters for interpolation (0.75-band) - note this also filters integer pels.
105 static const InterpKernel filteredinterp_filters750[(1 << RS_SUBPEL_BITS)] = {
106 { 2, -11, 25, 96, 25, -11, 2, 0 }, { 2, -11, 24, 96, 26, -11, 2, 0 },
107 { 2, -11, 22, 96, 28, -11, 2, 0 }, { 2, -10, 21, 96, 29, -12, 2, 0 },
108 { 2, -10, 19, 96, 31, -12, 2, 0 }, { 2, -10, 18, 95, 32, -11, 2, 0 },
109 { 2, -10, 17, 95, 34, -12, 2, 0 }, { 2, -9, 15, 95, 35, -12, 2, 0 },
110 { 2, -9, 14, 94, 37, -12, 2, 0 }, { 2, -9, 13, 94, 38, -12, 2, 0 },
111 { 2, -8, 12, 93, 40, -12, 1, 0 }, { 2, -8, 11, 93, 41, -12, 1, 0 },
112 { 2, -8, 9, 92, 43, -12, 1, 1 }, { 2, -8, 8, 92, 44, -12, 1, 1 },
113 { 2, -7, 7, 91, 46, -12, 1, 0 }, { 2, -7, 6, 90, 47, -12, 1, 1 },
114 { 2, -7, 5, 90, 49, -12, 1, 0 }, { 2, -6, 4, 89, 50, -12, 1, 0 },
115 { 2, -6, 3, 88, 52, -12, 0, 1 }, { 2, -6, 2, 87, 54, -12, 0, 1 },
116 { 2, -5, 1, 86, 55, -12, 0, 1 }, { 2, -5, 0, 85, 57, -12, 0, 1 },
117 { 2, -5, -1, 84, 58, -11, 0, 1 }, { 2, -5, -2, 83, 60, -11, 0, 1 },
118 { 2, -4, -2, 82, 61, -11, -1, 1 }, { 1, -4, -3, 81, 63, -10, -1, 1 },
119 { 2, -4, -4, 80, 64, -10, -1, 1 }, { 1, -4, -4, 79, 66, -10, -1, 1 },
120 { 1, -3, -5, 77, 67, -9, -1, 1 }, { 1, -3, -6, 76, 69, -9, -1, 1 },
121 { 1, -3, -6, 75, 70, -8, -2, 1 }, { 1, -2, -7, 74, 71, -8, -2, 1 },
122 { 1, -2, -7, 72, 72, -7, -2, 1 }, { 1, -2, -8, 71, 74, -7, -2, 1 },
123 { 1, -2, -8, 70, 75, -6, -3, 1 }, { 1, -1, -9, 69, 76, -6, -3, 1 },
124 { 1, -1, -9, 67, 77, -5, -3, 1 }, { 1, -1, -10, 66, 79, -4, -4, 1 },
125 { 1, -1, -10, 64, 80, -4, -4, 2 }, { 1, -1, -10, 63, 81, -3, -4, 1 },
126 { 1, -1, -11, 61, 82, -2, -4, 2 }, { 1, 0, -11, 60, 83, -2, -5, 2 },
127 { 1, 0, -11, 58, 84, -1, -5, 2 }, { 1, 0, -12, 57, 85, 0, -5, 2 },
128 { 1, 0, -12, 55, 86, 1, -5, 2 }, { 1, 0, -12, 54, 87, 2, -6, 2 },
129 { 1, 0, -12, 52, 88, 3, -6, 2 }, { 0, 1, -12, 50, 89, 4, -6, 2 },
130 { 0, 1, -12, 49, 90, 5, -7, 2 }, { 1, 1, -12, 47, 90, 6, -7, 2 },
131 { 0, 1, -12, 46, 91, 7, -7, 2 }, { 1, 1, -12, 44, 92, 8, -8, 2 },
132 { 1, 1, -12, 43, 92, 9, -8, 2 }, { 0, 1, -12, 41, 93, 11, -8, 2 },
133 { 0, 1, -12, 40, 93, 12, -8, 2 }, { 0, 2, -12, 38, 94, 13, -9, 2 },
134 { 0, 2, -12, 37, 94, 14, -9, 2 }, { 0, 2, -12, 35, 95, 15, -9, 2 },
135 { 0, 2, -12, 34, 95, 17, -10, 2 }, { 0, 2, -11, 32, 95, 18, -10, 2 },
136 { 0, 2, -12, 31, 96, 19, -10, 2 }, { 0, 2, -12, 29, 96, 21, -10, 2 },
137 { 0, 2, -11, 28, 96, 22, -11, 2 }, { 0, 2, -11, 26, 96, 24, -11, 2 },
138 };
139
140 // Filters for interpolation (0.875-band) - note this also filters integer pels.
141 static const InterpKernel filteredinterp_filters875[(1 << RS_SUBPEL_BITS)] = {
142 { 3, -8, 13, 112, 13, -8, 3, 0 }, { 2, -7, 12, 112, 15, -8, 3, -1 },
143 { 3, -7, 10, 112, 17, -9, 3, -1 }, { 2, -6, 8, 112, 19, -9, 3, -1 },
144 { 2, -6, 7, 112, 21, -10, 3, -1 }, { 2, -5, 6, 111, 22, -10, 3, -1 },
145 { 2, -5, 4, 111, 24, -10, 3, -1 }, { 2, -4, 3, 110, 26, -11, 3, -1 },
146 { 2, -4, 1, 110, 28, -11, 3, -1 }, { 2, -4, 0, 109, 30, -12, 4, -1 },
147 { 1, -3, -1, 108, 32, -12, 4, -1 }, { 1, -3, -2, 108, 34, -13, 4, -1 },
148 { 1, -2, -4, 107, 36, -13, 4, -1 }, { 1, -2, -5, 106, 38, -13, 4, -1 },
149 { 1, -1, -6, 105, 40, -14, 4, -1 }, { 1, -1, -7, 104, 42, -14, 4, -1 },
150 { 1, -1, -7, 103, 44, -15, 4, -1 }, { 1, 0, -8, 101, 46, -15, 4, -1 },
151 { 1, 0, -9, 100, 48, -15, 4, -1 }, { 1, 0, -10, 99, 50, -15, 4, -1 },
152 { 1, 1, -11, 97, 53, -16, 4, -1 }, { 0, 1, -11, 96, 55, -16, 4, -1 },
153 { 0, 1, -12, 95, 57, -16, 4, -1 }, { 0, 2, -13, 93, 59, -16, 4, -1 },
154 { 0, 2, -13, 91, 61, -16, 4, -1 }, { 0, 2, -14, 90, 63, -16, 4, -1 },
155 { 0, 2, -14, 88, 65, -16, 4, -1 }, { 0, 2, -15, 86, 67, -16, 4, 0 },
156 { 0, 3, -15, 84, 69, -17, 4, 0 }, { 0, 3, -16, 83, 71, -17, 4, 0 },
157 { 0, 3, -16, 81, 73, -16, 3, 0 }, { 0, 3, -16, 79, 75, -16, 3, 0 },
158 { 0, 3, -16, 77, 77, -16, 3, 0 }, { 0, 3, -16, 75, 79, -16, 3, 0 },
159 { 0, 3, -16, 73, 81, -16, 3, 0 }, { 0, 4, -17, 71, 83, -16, 3, 0 },
160 { 0, 4, -17, 69, 84, -15, 3, 0 }, { 0, 4, -16, 67, 86, -15, 2, 0 },
161 { -1, 4, -16, 65, 88, -14, 2, 0 }, { -1, 4, -16, 63, 90, -14, 2, 0 },
162 { -1, 4, -16, 61, 91, -13, 2, 0 }, { -1, 4, -16, 59, 93, -13, 2, 0 },
163 { -1, 4, -16, 57, 95, -12, 1, 0 }, { -1, 4, -16, 55, 96, -11, 1, 0 },
164 { -1, 4, -16, 53, 97, -11, 1, 1 }, { -1, 4, -15, 50, 99, -10, 0, 1 },
165 { -1, 4, -15, 48, 100, -9, 0, 1 }, { -1, 4, -15, 46, 101, -8, 0, 1 },
166 { -1, 4, -15, 44, 103, -7, -1, 1 }, { -1, 4, -14, 42, 104, -7, -1, 1 },
167 { -1, 4, -14, 40, 105, -6, -1, 1 }, { -1, 4, -13, 38, 106, -5, -2, 1 },
168 { -1, 4, -13, 36, 107, -4, -2, 1 }, { -1, 4, -13, 34, 108, -2, -3, 1 },
169 { -1, 4, -12, 32, 108, -1, -3, 1 }, { -1, 4, -12, 30, 109, 0, -4, 2 },
170 { -1, 3, -11, 28, 110, 1, -4, 2 }, { -1, 3, -11, 26, 110, 3, -4, 2 },
171 { -1, 3, -10, 24, 111, 4, -5, 2 }, { -1, 3, -10, 22, 111, 6, -5, 2 },
172 { -1, 3, -10, 21, 112, 7, -6, 2 }, { -1, 3, -9, 19, 112, 8, -6, 2 },
173 { -1, 3, -9, 17, 112, 10, -7, 3 }, { -1, 3, -8, 15, 112, 12, -7, 2 },
174 };
175
176 const int16_t av1_resize_filter_normative[(
177 1 << RS_SUBPEL_BITS)][UPSCALE_NORMATIVE_TAPS] = {
178 #if UPSCALE_NORMATIVE_TAPS == 8
179 { 0, 0, 0, 128, 0, 0, 0, 0 }, { 0, 0, -1, 128, 2, -1, 0, 0 },
180 { 0, 1, -3, 127, 4, -2, 1, 0 }, { 0, 1, -4, 127, 6, -3, 1, 0 },
181 { 0, 2, -6, 126, 8, -3, 1, 0 }, { 0, 2, -7, 125, 11, -4, 1, 0 },
182 { -1, 2, -8, 125, 13, -5, 2, 0 }, { -1, 3, -9, 124, 15, -6, 2, 0 },
183 { -1, 3, -10, 123, 18, -6, 2, -1 }, { -1, 3, -11, 122, 20, -7, 3, -1 },
184 { -1, 4, -12, 121, 22, -8, 3, -1 }, { -1, 4, -13, 120, 25, -9, 3, -1 },
185 { -1, 4, -14, 118, 28, -9, 3, -1 }, { -1, 4, -15, 117, 30, -10, 4, -1 },
186 { -1, 5, -16, 116, 32, -11, 4, -1 }, { -1, 5, -16, 114, 35, -12, 4, -1 },
187 { -1, 5, -17, 112, 38, -12, 4, -1 }, { -1, 5, -18, 111, 40, -13, 5, -1 },
188 { -1, 5, -18, 109, 43, -14, 5, -1 }, { -1, 6, -19, 107, 45, -14, 5, -1 },
189 { -1, 6, -19, 105, 48, -15, 5, -1 }, { -1, 6, -19, 103, 51, -16, 5, -1 },
190 { -1, 6, -20, 101, 53, -16, 6, -1 }, { -1, 6, -20, 99, 56, -17, 6, -1 },
191 { -1, 6, -20, 97, 58, -17, 6, -1 }, { -1, 6, -20, 95, 61, -18, 6, -1 },
192 { -2, 7, -20, 93, 64, -18, 6, -2 }, { -2, 7, -20, 91, 66, -19, 6, -1 },
193 { -2, 7, -20, 88, 69, -19, 6, -1 }, { -2, 7, -20, 86, 71, -19, 6, -1 },
194 { -2, 7, -20, 84, 74, -20, 7, -2 }, { -2, 7, -20, 81, 76, -20, 7, -1 },
195 { -2, 7, -20, 79, 79, -20, 7, -2 }, { -1, 7, -20, 76, 81, -20, 7, -2 },
196 { -2, 7, -20, 74, 84, -20, 7, -2 }, { -1, 6, -19, 71, 86, -20, 7, -2 },
197 { -1, 6, -19, 69, 88, -20, 7, -2 }, { -1, 6, -19, 66, 91, -20, 7, -2 },
198 { -2, 6, -18, 64, 93, -20, 7, -2 }, { -1, 6, -18, 61, 95, -20, 6, -1 },
199 { -1, 6, -17, 58, 97, -20, 6, -1 }, { -1, 6, -17, 56, 99, -20, 6, -1 },
200 { -1, 6, -16, 53, 101, -20, 6, -1 }, { -1, 5, -16, 51, 103, -19, 6, -1 },
201 { -1, 5, -15, 48, 105, -19, 6, -1 }, { -1, 5, -14, 45, 107, -19, 6, -1 },
202 { -1, 5, -14, 43, 109, -18, 5, -1 }, { -1, 5, -13, 40, 111, -18, 5, -1 },
203 { -1, 4, -12, 38, 112, -17, 5, -1 }, { -1, 4, -12, 35, 114, -16, 5, -1 },
204 { -1, 4, -11, 32, 116, -16, 5, -1 }, { -1, 4, -10, 30, 117, -15, 4, -1 },
205 { -1, 3, -9, 28, 118, -14, 4, -1 }, { -1, 3, -9, 25, 120, -13, 4, -1 },
206 { -1, 3, -8, 22, 121, -12, 4, -1 }, { -1, 3, -7, 20, 122, -11, 3, -1 },
207 { -1, 2, -6, 18, 123, -10, 3, -1 }, { 0, 2, -6, 15, 124, -9, 3, -1 },
208 { 0, 2, -5, 13, 125, -8, 2, -1 }, { 0, 1, -4, 11, 125, -7, 2, 0 },
209 { 0, 1, -3, 8, 126, -6, 2, 0 }, { 0, 1, -3, 6, 127, -4, 1, 0 },
210 { 0, 1, -2, 4, 127, -3, 1, 0 }, { 0, 0, -1, 2, 128, -1, 0, 0 },
211 #else
212 #error "Invalid value of UPSCALE_NORMATIVE_TAPS"
213 #endif // UPSCALE_NORMATIVE_TAPS == 8
214 };
215
216 // Filters for interpolation (full-band) - no filtering for integer pixels
217 #define filteredinterp_filters1000 av1_resize_filter_normative
218
choose_interp_filter(int in_length,int out_length)219 static const InterpKernel *choose_interp_filter(int in_length, int out_length) {
220 int out_length16 = out_length * 16;
221 if (out_length16 >= in_length * 16)
222 return filteredinterp_filters1000;
223 else if (out_length16 >= in_length * 13)
224 return filteredinterp_filters875;
225 else if (out_length16 >= in_length * 11)
226 return filteredinterp_filters750;
227 else if (out_length16 >= in_length * 9)
228 return filteredinterp_filters625;
229 else
230 return filteredinterp_filters500;
231 }
232
interpolate_core(const uint8_t * const input,int in_length,uint8_t * output,int out_length,const int16_t * interp_filters,int interp_taps)233 static void interpolate_core(const uint8_t *const input, int in_length,
234 uint8_t *output, int out_length,
235 const int16_t *interp_filters, int interp_taps) {
236 const int32_t delta =
237 (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) /
238 out_length;
239 const int32_t offset =
240 in_length > out_length
241 ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
242 out_length / 2) /
243 out_length
244 : -(((int32_t)(out_length - in_length)
245 << (RS_SCALE_SUBPEL_BITS - 1)) +
246 out_length / 2) /
247 out_length;
248 uint8_t *optr = output;
249 int x, x1, x2, sum, k, int_pel, sub_pel;
250 int32_t y;
251
252 x = 0;
253 y = offset + RS_SCALE_EXTRA_OFF;
254 while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) {
255 x++;
256 y += delta;
257 }
258 x1 = x;
259 x = out_length - 1;
260 y = delta * x + offset + RS_SCALE_EXTRA_OFF;
261 while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >=
262 in_length) {
263 x--;
264 y -= delta;
265 }
266 x2 = x;
267 if (x1 > x2) {
268 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length;
269 ++x, y += delta) {
270 int_pel = y >> RS_SCALE_SUBPEL_BITS;
271 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
272 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
273 sum = 0;
274 for (k = 0; k < interp_taps; ++k) {
275 const int pk = int_pel - interp_taps / 2 + 1 + k;
276 sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)];
277 }
278 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
279 }
280 } else {
281 // Initial part.
282 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) {
283 int_pel = y >> RS_SCALE_SUBPEL_BITS;
284 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
285 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
286 sum = 0;
287 for (k = 0; k < interp_taps; ++k)
288 sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)];
289 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
290 }
291 // Middle part.
292 for (; x <= x2; ++x, y += delta) {
293 int_pel = y >> RS_SCALE_SUBPEL_BITS;
294 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
295 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
296 sum = 0;
297 for (k = 0; k < interp_taps; ++k)
298 sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k];
299 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
300 }
301 // End part.
302 for (; x < out_length; ++x, y += delta) {
303 int_pel = y >> RS_SCALE_SUBPEL_BITS;
304 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
305 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
306 sum = 0;
307 for (k = 0; k < interp_taps; ++k)
308 sum += filter[k] *
309 input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)];
310 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
311 }
312 }
313 }
314
interpolate(const uint8_t * const input,int in_length,uint8_t * output,int out_length)315 static void interpolate(const uint8_t *const input, int in_length,
316 uint8_t *output, int out_length) {
317 const InterpKernel *interp_filters =
318 choose_interp_filter(in_length, out_length);
319
320 interpolate_core(input, in_length, output, out_length, &interp_filters[0][0],
321 SUBPEL_TAPS);
322 }
323
av1_get_upscale_convolve_step(int in_length,int out_length)324 int32_t av1_get_upscale_convolve_step(int in_length, int out_length) {
325 return ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length;
326 }
327
get_upscale_convolve_x0(int in_length,int out_length,int32_t x_step_qn)328 static int32_t get_upscale_convolve_x0(int in_length, int out_length,
329 int32_t x_step_qn) {
330 const int err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS);
331 const int32_t x0 =
332 (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
333 out_length / 2) /
334 out_length +
335 RS_SCALE_EXTRA_OFF - err / 2;
336 return (int32_t)((uint32_t)x0 & RS_SCALE_SUBPEL_MASK);
337 }
338
down2_symeven(const uint8_t * const input,int length,uint8_t * output,int start_offset)339 void down2_symeven(const uint8_t *const input, int length, uint8_t *output,
340 int start_offset) {
341 // Actual filter len = 2 * filter_len_half.
342 const int16_t *filter = av1_down2_symeven_half_filter;
343 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
344 int i, j;
345 uint8_t *optr = output;
346 int l1 = filter_len_half;
347 int l2 = (length - filter_len_half);
348 l1 += (l1 & 1);
349 l2 += (l2 & 1);
350 if (l1 > l2) {
351 // Short input length.
352 for (i = start_offset; i < length; i += 2) {
353 int sum = (1 << (FILTER_BITS - 1));
354 for (j = 0; j < filter_len_half; ++j) {
355 sum +=
356 (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + 1 + j, length - 1)]) *
357 filter[j];
358 }
359 sum >>= FILTER_BITS;
360 *optr++ = clip_pixel(sum);
361 }
362 } else {
363 // Initial part.
364 for (i = start_offset; i < l1; i += 2) {
365 int sum = (1 << (FILTER_BITS - 1));
366 for (j = 0; j < filter_len_half; ++j) {
367 sum += (input[AOMMAX(i - j, 0)] + input[i + 1 + j]) * filter[j];
368 }
369 sum >>= FILTER_BITS;
370 *optr++ = clip_pixel(sum);
371 }
372 // Middle part.
373 for (; i < l2; i += 2) {
374 int sum = (1 << (FILTER_BITS - 1));
375 for (j = 0; j < filter_len_half; ++j) {
376 sum += (input[i - j] + input[i + 1 + j]) * filter[j];
377 }
378 sum >>= FILTER_BITS;
379 *optr++ = clip_pixel(sum);
380 }
381 // End part.
382 for (; i < length; i += 2) {
383 int sum = (1 << (FILTER_BITS - 1));
384 for (j = 0; j < filter_len_half; ++j) {
385 sum +=
386 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
387 }
388 sum >>= FILTER_BITS;
389 *optr++ = clip_pixel(sum);
390 }
391 }
392 }
393
down2_symodd(const uint8_t * const input,int length,uint8_t * output)394 static void down2_symodd(const uint8_t *const input, int length,
395 uint8_t *output) {
396 // Actual filter len = 2 * filter_len_half - 1.
397 const int16_t *filter = av1_down2_symodd_half_filter;
398 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
399 int i, j;
400 uint8_t *optr = output;
401 int l1 = filter_len_half - 1;
402 int l2 = (length - filter_len_half + 1);
403 l1 += (l1 & 1);
404 l2 += (l2 & 1);
405 if (l1 > l2) {
406 // Short input length.
407 for (i = 0; i < length; i += 2) {
408 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
409 for (j = 1; j < filter_len_half; ++j) {
410 sum += (input[(i - j < 0 ? 0 : i - j)] +
411 input[(i + j >= length ? length - 1 : i + j)]) *
412 filter[j];
413 }
414 sum >>= FILTER_BITS;
415 *optr++ = clip_pixel(sum);
416 }
417 } else {
418 // Initial part.
419 for (i = 0; i < l1; i += 2) {
420 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
421 for (j = 1; j < filter_len_half; ++j) {
422 sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
423 }
424 sum >>= FILTER_BITS;
425 *optr++ = clip_pixel(sum);
426 }
427 // Middle part.
428 for (; i < l2; i += 2) {
429 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
430 for (j = 1; j < filter_len_half; ++j) {
431 sum += (input[i - j] + input[i + j]) * filter[j];
432 }
433 sum >>= FILTER_BITS;
434 *optr++ = clip_pixel(sum);
435 }
436 // End part.
437 for (; i < length; i += 2) {
438 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
439 for (j = 1; j < filter_len_half; ++j) {
440 sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
441 filter[j];
442 }
443 sum >>= FILTER_BITS;
444 *optr++ = clip_pixel(sum);
445 }
446 }
447 }
448
get_down2_length(int length,int steps)449 static int get_down2_length(int length, int steps) {
450 for (int s = 0; s < steps; ++s) length = (length + 1) >> 1;
451 return length;
452 }
453
get_down2_steps(int in_length,int out_length)454 static int get_down2_steps(int in_length, int out_length) {
455 int steps = 0;
456 int proj_in_length;
457 while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
458 ++steps;
459 in_length = proj_in_length;
460 if (in_length == 1) {
461 // Special case: we break because any further calls to get_down2_length()
462 // with be with length == 1, which return 1, resulting in an infinite
463 // loop.
464 break;
465 }
466 }
467 return steps;
468 }
469
resize_multistep(const uint8_t * const input,int length,uint8_t * output,int olength,uint8_t * otmp)470 static void resize_multistep(const uint8_t *const input, int length,
471 uint8_t *output, int olength, uint8_t *otmp) {
472 if (length == olength) {
473 memcpy(output, input, sizeof(output[0]) * length);
474 return;
475 }
476 const int steps = get_down2_steps(length, olength);
477
478 if (steps > 0) {
479 uint8_t *out = NULL;
480 int filteredlength = length;
481
482 assert(otmp != NULL);
483 uint8_t *otmp2 = otmp + get_down2_length(length, 1);
484 for (int s = 0; s < steps; ++s) {
485 const int proj_filteredlength = get_down2_length(filteredlength, 1);
486 const uint8_t *const in = (s == 0 ? input : out);
487 if (s == steps - 1 && proj_filteredlength == olength)
488 out = output;
489 else
490 out = (s & 1 ? otmp2 : otmp);
491 if (filteredlength & 1)
492 down2_symodd(in, filteredlength, out);
493 else
494 down2_symeven(in, filteredlength, out, 0);
495 filteredlength = proj_filteredlength;
496 }
497 if (filteredlength != olength) {
498 interpolate(out, filteredlength, output, olength);
499 }
500 } else {
501 interpolate(input, length, output, olength);
502 }
503 }
504
fill_col_to_arr(uint8_t * img,int stride,int len,uint8_t * arr)505 static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
506 int i;
507 uint8_t *iptr = img;
508 uint8_t *aptr = arr;
509 for (i = 0; i < len; ++i, iptr += stride) {
510 *aptr++ = *iptr;
511 }
512 }
513
fill_arr_to_col(uint8_t * img,int stride,int len,uint8_t * arr)514 static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
515 int i;
516 uint8_t *iptr = img;
517 uint8_t *aptr = arr;
518 for (i = 0; i < len; ++i, iptr += stride) {
519 *iptr = *aptr++;
520 }
521 }
522
av1_resize_vert_dir_c(uint8_t * intbuf,uint8_t * output,int out_stride,int height,int height2,int width2,int start_col)523 bool av1_resize_vert_dir_c(uint8_t *intbuf, uint8_t *output, int out_stride,
524 int height, int height2, int width2, int start_col) {
525 bool mem_status = true;
526 uint8_t *arrbuf = (uint8_t *)aom_malloc(sizeof(*arrbuf) * height);
527 uint8_t *arrbuf2 = (uint8_t *)aom_malloc(sizeof(*arrbuf2) * height2);
528 if (arrbuf == NULL || arrbuf2 == NULL) {
529 mem_status = false;
530 goto Error;
531 }
532
533 for (int i = start_col; i < width2; ++i) {
534 fill_col_to_arr(intbuf + i, width2, height, arrbuf);
535 down2_symeven(arrbuf, height, arrbuf2, 0);
536 fill_arr_to_col(output + i, out_stride, height2, arrbuf2);
537 }
538
539 Error:
540 aom_free(arrbuf);
541 aom_free(arrbuf2);
542 return mem_status;
543 }
544
av1_resize_horz_dir_c(const uint8_t * const input,int in_stride,uint8_t * intbuf,int height,int filtered_length,int width2)545 void av1_resize_horz_dir_c(const uint8_t *const input, int in_stride,
546 uint8_t *intbuf, int height, int filtered_length,
547 int width2) {
548 for (int i = 0; i < height; ++i)
549 down2_symeven(input + in_stride * i, filtered_length, intbuf + width2 * i,
550 0);
551 }
552
av1_resize_plane_to_half(const uint8_t * const input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride)553 bool av1_resize_plane_to_half(const uint8_t *const input, int height, int width,
554 int in_stride, uint8_t *output, int height2,
555 int width2, int out_stride) {
556 uint8_t *intbuf = (uint8_t *)aom_malloc(sizeof(*intbuf) * width2 * height);
557 if (intbuf == NULL) {
558 return false;
559 }
560
561 // Resize in the horizontal direction
562 av1_resize_horz_dir(input, in_stride, intbuf, height, width, width2);
563 // Resize in the vertical direction
564 bool mem_status = av1_resize_vert_dir(intbuf, output, out_stride, height,
565 height2, width2, 0 /*start_col*/);
566 aom_free(intbuf);
567 return mem_status;
568 }
569
570 // Check if both the output width and height are half of input width and
571 // height respectively.
should_resize_by_half(int height,int width,int height2,int width2)572 bool should_resize_by_half(int height, int width, int height2, int width2) {
573 const bool is_width_by_2 = get_down2_length(width, 1) == width2;
574 const bool is_height_by_2 = get_down2_length(height, 1) == height2;
575 return (is_width_by_2 && is_height_by_2);
576 }
577
av1_resize_plane(const uint8_t * input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride)578 bool av1_resize_plane(const uint8_t *input, int height, int width,
579 int in_stride, uint8_t *output, int height2, int width2,
580 int out_stride) {
581 int i;
582 bool mem_status = true;
583 uint8_t *intbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * width2 * height);
584 uint8_t *tmpbuf =
585 (uint8_t *)aom_malloc(sizeof(uint8_t) * AOMMAX(width, height));
586 uint8_t *arrbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * height);
587 uint8_t *arrbuf2 = (uint8_t *)aom_malloc(sizeof(uint8_t) * height2);
588 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL) {
589 mem_status = false;
590 goto Error;
591 }
592 assert(width > 0);
593 assert(height > 0);
594 assert(width2 > 0);
595 assert(height2 > 0);
596 for (i = 0; i < height; ++i)
597 resize_multistep(input + in_stride * i, width, intbuf + width2 * i, width2,
598 tmpbuf);
599 for (i = 0; i < width2; ++i) {
600 fill_col_to_arr(intbuf + i, width2, height, arrbuf);
601 resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf);
602 fill_arr_to_col(output + i, out_stride, height2, arrbuf2);
603 }
604
605 Error:
606 aom_free(intbuf);
607 aom_free(tmpbuf);
608 aom_free(arrbuf);
609 aom_free(arrbuf2);
610 return mem_status;
611 }
612
upscale_normative_rect(const uint8_t * const input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride,int x_step_qn,int x0_qn,int pad_left,int pad_right)613 static bool upscale_normative_rect(const uint8_t *const input, int height,
614 int width, int in_stride, uint8_t *output,
615 int height2, int width2, int out_stride,
616 int x_step_qn, int x0_qn, int pad_left,
617 int pad_right) {
618 assert(width > 0);
619 assert(height > 0);
620 assert(width2 > 0);
621 assert(height2 > 0);
622 assert(height2 == height);
623
624 // Extend the left/right pixels of the tile column if needed
625 // (either because we can't sample from other tiles, or because we're at
626 // a frame edge).
627 // Save the overwritten pixels into tmp_left and tmp_right.
628 // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra
629 // column of border pixels compared to what we'd naively think.
630 const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1;
631 uint8_t *tmp_left =
632 NULL; // Silence spurious "may be used uninitialized" warnings
633 uint8_t *tmp_right = NULL;
634 uint8_t *const in_tl = (uint8_t *)(input - border_cols); // Cast off 'const'
635 uint8_t *const in_tr = (uint8_t *)(input + width);
636 if (pad_left) {
637 tmp_left = (uint8_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height);
638 if (!tmp_left) return false;
639 for (int i = 0; i < height; i++) {
640 memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_cols);
641 memset(in_tl + i * in_stride, input[i * in_stride], border_cols);
642 }
643 }
644 if (pad_right) {
645 tmp_right =
646 (uint8_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height);
647 if (!tmp_right) {
648 aom_free(tmp_left);
649 return false;
650 }
651 for (int i = 0; i < height; i++) {
652 memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_cols);
653 memset(in_tr + i * in_stride, input[i * in_stride + width - 1],
654 border_cols);
655 }
656 }
657
658 av1_convolve_horiz_rs(input - 1, in_stride, output, out_stride, width2,
659 height2, &av1_resize_filter_normative[0][0], x0_qn,
660 x_step_qn);
661
662 // Restore the left/right border pixels
663 if (pad_left) {
664 for (int i = 0; i < height; i++) {
665 memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_cols);
666 }
667 aom_free(tmp_left);
668 }
669 if (pad_right) {
670 for (int i = 0; i < height; i++) {
671 memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_cols);
672 }
673 aom_free(tmp_right);
674 }
675 return true;
676 }
677
678 #if CONFIG_AV1_HIGHBITDEPTH
highbd_interpolate_core(const uint16_t * const input,int in_length,uint16_t * output,int out_length,int bd,const int16_t * interp_filters,int interp_taps)679 static void highbd_interpolate_core(const uint16_t *const input, int in_length,
680 uint16_t *output, int out_length, int bd,
681 const int16_t *interp_filters,
682 int interp_taps) {
683 const int32_t delta =
684 (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) /
685 out_length;
686 const int32_t offset =
687 in_length > out_length
688 ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
689 out_length / 2) /
690 out_length
691 : -(((int32_t)(out_length - in_length)
692 << (RS_SCALE_SUBPEL_BITS - 1)) +
693 out_length / 2) /
694 out_length;
695 uint16_t *optr = output;
696 int x, x1, x2, sum, k, int_pel, sub_pel;
697 int32_t y;
698
699 x = 0;
700 y = offset + RS_SCALE_EXTRA_OFF;
701 while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) {
702 x++;
703 y += delta;
704 }
705 x1 = x;
706 x = out_length - 1;
707 y = delta * x + offset + RS_SCALE_EXTRA_OFF;
708 while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >=
709 in_length) {
710 x--;
711 y -= delta;
712 }
713 x2 = x;
714 if (x1 > x2) {
715 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length;
716 ++x, y += delta) {
717 int_pel = y >> RS_SCALE_SUBPEL_BITS;
718 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
719 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
720 sum = 0;
721 for (k = 0; k < interp_taps; ++k) {
722 const int pk = int_pel - interp_taps / 2 + 1 + k;
723 sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)];
724 }
725 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
726 }
727 } else {
728 // Initial part.
729 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) {
730 int_pel = y >> RS_SCALE_SUBPEL_BITS;
731 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
732 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
733 sum = 0;
734 for (k = 0; k < interp_taps; ++k)
735 sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)];
736 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
737 }
738 // Middle part.
739 for (; x <= x2; ++x, y += delta) {
740 int_pel = y >> RS_SCALE_SUBPEL_BITS;
741 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
742 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
743 sum = 0;
744 for (k = 0; k < interp_taps; ++k)
745 sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k];
746 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
747 }
748 // End part.
749 for (; x < out_length; ++x, y += delta) {
750 int_pel = y >> RS_SCALE_SUBPEL_BITS;
751 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
752 const int16_t *filter = &interp_filters[sub_pel * interp_taps];
753 sum = 0;
754 for (k = 0; k < interp_taps; ++k)
755 sum += filter[k] *
756 input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)];
757 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
758 }
759 }
760 }
761
highbd_interpolate(const uint16_t * const input,int in_length,uint16_t * output,int out_length,int bd)762 static void highbd_interpolate(const uint16_t *const input, int in_length,
763 uint16_t *output, int out_length, int bd) {
764 const InterpKernel *interp_filters =
765 choose_interp_filter(in_length, out_length);
766
767 highbd_interpolate_core(input, in_length, output, out_length, bd,
768 &interp_filters[0][0], SUBPEL_TAPS);
769 }
770
highbd_down2_symeven(const uint16_t * const input,int length,uint16_t * output,int bd)771 static void highbd_down2_symeven(const uint16_t *const input, int length,
772 uint16_t *output, int bd) {
773 // Actual filter len = 2 * filter_len_half.
774 static const int16_t *filter = av1_down2_symeven_half_filter;
775 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
776 int i, j;
777 uint16_t *optr = output;
778 int l1 = filter_len_half;
779 int l2 = (length - filter_len_half);
780 l1 += (l1 & 1);
781 l2 += (l2 & 1);
782 if (l1 > l2) {
783 // Short input length.
784 for (i = 0; i < length; i += 2) {
785 int sum = (1 << (FILTER_BITS - 1));
786 for (j = 0; j < filter_len_half; ++j) {
787 sum +=
788 (input[AOMMAX(0, i - j)] + input[AOMMIN(i + 1 + j, length - 1)]) *
789 filter[j];
790 }
791 sum >>= FILTER_BITS;
792 *optr++ = clip_pixel_highbd(sum, bd);
793 }
794 } else {
795 // Initial part.
796 for (i = 0; i < l1; i += 2) {
797 int sum = (1 << (FILTER_BITS - 1));
798 for (j = 0; j < filter_len_half; ++j) {
799 sum += (input[AOMMAX(0, i - j)] + input[i + 1 + j]) * filter[j];
800 }
801 sum >>= FILTER_BITS;
802 *optr++ = clip_pixel_highbd(sum, bd);
803 }
804 // Middle part.
805 for (; i < l2; i += 2) {
806 int sum = (1 << (FILTER_BITS - 1));
807 for (j = 0; j < filter_len_half; ++j) {
808 sum += (input[i - j] + input[i + 1 + j]) * filter[j];
809 }
810 sum >>= FILTER_BITS;
811 *optr++ = clip_pixel_highbd(sum, bd);
812 }
813 // End part.
814 for (; i < length; i += 2) {
815 int sum = (1 << (FILTER_BITS - 1));
816 for (j = 0; j < filter_len_half; ++j) {
817 sum +=
818 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
819 }
820 sum >>= FILTER_BITS;
821 *optr++ = clip_pixel_highbd(sum, bd);
822 }
823 }
824 }
825
highbd_down2_symodd(const uint16_t * const input,int length,uint16_t * output,int bd)826 static void highbd_down2_symodd(const uint16_t *const input, int length,
827 uint16_t *output, int bd) {
828 // Actual filter len = 2 * filter_len_half - 1.
829 static const int16_t *filter = av1_down2_symodd_half_filter;
830 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
831 int i, j;
832 uint16_t *optr = output;
833 int l1 = filter_len_half - 1;
834 int l2 = (length - filter_len_half + 1);
835 l1 += (l1 & 1);
836 l2 += (l2 & 1);
837 if (l1 > l2) {
838 // Short input length.
839 for (i = 0; i < length; i += 2) {
840 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
841 for (j = 1; j < filter_len_half; ++j) {
842 sum += (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + j, length - 1)]) *
843 filter[j];
844 }
845 sum >>= FILTER_BITS;
846 *optr++ = clip_pixel_highbd(sum, bd);
847 }
848 } else {
849 // Initial part.
850 for (i = 0; i < l1; i += 2) {
851 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
852 for (j = 1; j < filter_len_half; ++j) {
853 sum += (input[AOMMAX(i - j, 0)] + input[i + j]) * filter[j];
854 }
855 sum >>= FILTER_BITS;
856 *optr++ = clip_pixel_highbd(sum, bd);
857 }
858 // Middle part.
859 for (; i < l2; i += 2) {
860 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
861 for (j = 1; j < filter_len_half; ++j) {
862 sum += (input[i - j] + input[i + j]) * filter[j];
863 }
864 sum >>= FILTER_BITS;
865 *optr++ = clip_pixel_highbd(sum, bd);
866 }
867 // End part.
868 for (; i < length; i += 2) {
869 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
870 for (j = 1; j < filter_len_half; ++j) {
871 sum += (input[i - j] + input[AOMMIN(i + j, length - 1)]) * filter[j];
872 }
873 sum >>= FILTER_BITS;
874 *optr++ = clip_pixel_highbd(sum, bd);
875 }
876 }
877 }
878
highbd_resize_multistep(const uint16_t * const input,int length,uint16_t * output,int olength,uint16_t * otmp,int bd)879 static void highbd_resize_multistep(const uint16_t *const input, int length,
880 uint16_t *output, int olength,
881 uint16_t *otmp, int bd) {
882 if (length == olength) {
883 memcpy(output, input, sizeof(output[0]) * length);
884 return;
885 }
886 const int steps = get_down2_steps(length, olength);
887
888 if (steps > 0) {
889 uint16_t *out = NULL;
890 int filteredlength = length;
891
892 assert(otmp != NULL);
893 uint16_t *otmp2 = otmp + get_down2_length(length, 1);
894 for (int s = 0; s < steps; ++s) {
895 const int proj_filteredlength = get_down2_length(filteredlength, 1);
896 const uint16_t *const in = (s == 0 ? input : out);
897 if (s == steps - 1 && proj_filteredlength == olength)
898 out = output;
899 else
900 out = (s & 1 ? otmp2 : otmp);
901 if (filteredlength & 1)
902 highbd_down2_symodd(in, filteredlength, out, bd);
903 else
904 highbd_down2_symeven(in, filteredlength, out, bd);
905 filteredlength = proj_filteredlength;
906 }
907 if (filteredlength != olength) {
908 highbd_interpolate(out, filteredlength, output, olength, bd);
909 }
910 } else {
911 highbd_interpolate(input, length, output, olength, bd);
912 }
913 }
914
highbd_fill_col_to_arr(uint16_t * img,int stride,int len,uint16_t * arr)915 static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
916 uint16_t *arr) {
917 int i;
918 uint16_t *iptr = img;
919 uint16_t *aptr = arr;
920 for (i = 0; i < len; ++i, iptr += stride) {
921 *aptr++ = *iptr;
922 }
923 }
924
highbd_fill_arr_to_col(uint16_t * img,int stride,int len,uint16_t * arr)925 static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
926 uint16_t *arr) {
927 int i;
928 uint16_t *iptr = img;
929 uint16_t *aptr = arr;
930 for (i = 0; i < len; ++i, iptr += stride) {
931 *iptr = *aptr++;
932 }
933 }
934
av1_highbd_resize_plane(const uint8_t * input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride,int bd)935 void av1_highbd_resize_plane(const uint8_t *input, int height, int width,
936 int in_stride, uint8_t *output, int height2,
937 int width2, int out_stride, int bd) {
938 int i;
939 uint16_t *intbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * width2 * height);
940 uint16_t *tmpbuf =
941 (uint16_t *)aom_malloc(sizeof(uint16_t) * AOMMAX(width, height));
942 uint16_t *arrbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * height);
943 uint16_t *arrbuf2 = (uint16_t *)aom_malloc(sizeof(uint16_t) * height2);
944 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
945 goto Error;
946 for (i = 0; i < height; ++i) {
947 highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
948 intbuf + width2 * i, width2, tmpbuf, bd);
949 }
950 for (i = 0; i < width2; ++i) {
951 highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
952 highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
953 highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
954 arrbuf2);
955 }
956
957 Error:
958 aom_free(intbuf);
959 aom_free(tmpbuf);
960 aom_free(arrbuf);
961 aom_free(arrbuf2);
962 }
963
highbd_upscale_normative_rect(const uint8_t * const input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride,int x_step_qn,int x0_qn,int pad_left,int pad_right,int bd)964 static bool highbd_upscale_normative_rect(const uint8_t *const input,
965 int height, int width, int in_stride,
966 uint8_t *output, int height2,
967 int width2, int out_stride,
968 int x_step_qn, int x0_qn,
969 int pad_left, int pad_right, int bd) {
970 assert(width > 0);
971 assert(height > 0);
972 assert(width2 > 0);
973 assert(height2 > 0);
974 assert(height2 == height);
975
976 // Extend the left/right pixels of the tile column if needed
977 // (either because we can't sample from other tiles, or because we're at
978 // a frame edge).
979 // Save the overwritten pixels into tmp_left and tmp_right.
980 // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra
981 // column of border pixels compared to what we'd naively think.
982 const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1;
983 const int border_size = border_cols * sizeof(uint16_t);
984 uint16_t *tmp_left =
985 NULL; // Silence spurious "may be used uninitialized" warnings
986 uint16_t *tmp_right = NULL;
987 uint16_t *const input16 = CONVERT_TO_SHORTPTR(input);
988 uint16_t *const in_tl = input16 - border_cols;
989 uint16_t *const in_tr = input16 + width;
990 if (pad_left) {
991 tmp_left = (uint16_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height);
992 if (!tmp_left) return false;
993 for (int i = 0; i < height; i++) {
994 memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_size);
995 aom_memset16(in_tl + i * in_stride, input16[i * in_stride], border_cols);
996 }
997 }
998 if (pad_right) {
999 tmp_right =
1000 (uint16_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height);
1001 if (!tmp_right) {
1002 aom_free(tmp_left);
1003 return false;
1004 }
1005 for (int i = 0; i < height; i++) {
1006 memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_size);
1007 aom_memset16(in_tr + i * in_stride, input16[i * in_stride + width - 1],
1008 border_cols);
1009 }
1010 }
1011
1012 av1_highbd_convolve_horiz_rs(CONVERT_TO_SHORTPTR(input - 1), in_stride,
1013 CONVERT_TO_SHORTPTR(output), out_stride, width2,
1014 height2, &av1_resize_filter_normative[0][0],
1015 x0_qn, x_step_qn, bd);
1016
1017 // Restore the left/right border pixels
1018 if (pad_left) {
1019 for (int i = 0; i < height; i++) {
1020 memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_size);
1021 }
1022 aom_free(tmp_left);
1023 }
1024 if (pad_right) {
1025 for (int i = 0; i < height; i++) {
1026 memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_size);
1027 }
1028 aom_free(tmp_right);
1029 }
1030 return true;
1031 }
1032 #endif // CONFIG_AV1_HIGHBITDEPTH
1033
av1_resize_and_extend_frame_c(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,const InterpFilter filter,const int phase_scaler,const int num_planes)1034 void av1_resize_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
1035 YV12_BUFFER_CONFIG *dst,
1036 const InterpFilter filter,
1037 const int phase_scaler,
1038 const int num_planes) {
1039 assert(filter == BILINEAR || filter == EIGHTTAP_SMOOTH ||
1040 filter == EIGHTTAP_REGULAR);
1041 const InterpKernel *const kernel =
1042 (const InterpKernel *)av1_interp_filter_params_list[filter].filter_ptr;
1043
1044 for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) {
1045 const int is_uv = i > 0;
1046 const int src_w = src->crop_widths[is_uv];
1047 const int src_h = src->crop_heights[is_uv];
1048 const uint8_t *src_buffer = src->buffers[i];
1049 const int src_stride = src->strides[is_uv];
1050 const int dst_w = dst->crop_widths[is_uv];
1051 const int dst_h = dst->crop_heights[is_uv];
1052 uint8_t *dst_buffer = dst->buffers[i];
1053 const int dst_stride = dst->strides[is_uv];
1054 for (int y = 0; y < dst_h; y += 16) {
1055 const int y_q4 =
1056 src_h == dst_h ? 0 : y * 16 * src_h / dst_h + phase_scaler;
1057 for (int x = 0; x < dst_w; x += 16) {
1058 const int x_q4 =
1059 src_w == dst_w ? 0 : x * 16 * src_w / dst_w + phase_scaler;
1060 const uint8_t *src_ptr =
1061 src_buffer + y * src_h / dst_h * src_stride + x * src_w / dst_w;
1062 uint8_t *dst_ptr = dst_buffer + y * dst_stride + x;
1063
1064 // Width and height of the actual working area.
1065 const int work_w = AOMMIN(16, dst_w - x);
1066 const int work_h = AOMMIN(16, dst_h - y);
1067 // SIMD versions of aom_scaled_2d() have some trouble handling
1068 // nonstandard sizes, so fall back on the C version to handle borders.
1069 if (work_w != 16 || work_h != 16) {
1070 aom_scaled_2d_c(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
1071 x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
1072 16 * src_h / dst_h, work_w, work_h);
1073 } else {
1074 aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
1075 x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
1076 16 * src_h / dst_h, 16, 16);
1077 }
1078 }
1079 }
1080 }
1081 aom_extend_frame_borders(dst, num_planes);
1082 }
1083
av1_resize_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int bd,int num_planes)1084 bool av1_resize_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1085 YV12_BUFFER_CONFIG *dst, int bd,
1086 int num_planes) {
1087 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with aom_image_t
1088
1089 // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
1090 // the static analysis warnings.
1091 for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) {
1092 const int is_uv = i > 0;
1093 #if CONFIG_AV1_HIGHBITDEPTH
1094 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
1095 av1_highbd_resize_plane(src->buffers[i], src->crop_heights[is_uv],
1096 src->crop_widths[is_uv], src->strides[is_uv],
1097 dst->buffers[i], dst->crop_heights[is_uv],
1098 dst->crop_widths[is_uv], dst->strides[is_uv], bd);
1099 } else if (!av1_resize_plane(src->buffers[i], src->crop_heights[is_uv],
1100 src->crop_widths[is_uv], src->strides[is_uv],
1101 dst->buffers[i], dst->crop_heights[is_uv],
1102 dst->crop_widths[is_uv],
1103 dst->strides[is_uv])) {
1104 return false;
1105 }
1106 #else
1107 (void)bd;
1108 if (!av1_resize_plane(src->buffers[i], src->crop_heights[is_uv],
1109 src->crop_widths[is_uv], src->strides[is_uv],
1110 dst->buffers[i], dst->crop_heights[is_uv],
1111 dst->crop_widths[is_uv], dst->strides[is_uv]))
1112 return false;
1113 #endif
1114 }
1115 aom_extend_frame_borders(dst, num_planes);
1116 return true;
1117 }
1118
av1_upscale_normative_rows(const AV1_COMMON * cm,const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int plane,int rows)1119 void av1_upscale_normative_rows(const AV1_COMMON *cm, const uint8_t *src,
1120 int src_stride, uint8_t *dst, int dst_stride,
1121 int plane, int rows) {
1122 const int is_uv = (plane > 0);
1123 const int ss_x = is_uv && cm->seq_params->subsampling_x;
1124 const int downscaled_plane_width = ROUND_POWER_OF_TWO(cm->width, ss_x);
1125 const int upscaled_plane_width =
1126 ROUND_POWER_OF_TWO(cm->superres_upscaled_width, ss_x);
1127 const int superres_denom = cm->superres_scale_denominator;
1128
1129 TileInfo tile_col;
1130 const int32_t x_step_qn = av1_get_upscale_convolve_step(
1131 downscaled_plane_width, upscaled_plane_width);
1132 int32_t x0_qn = get_upscale_convolve_x0(downscaled_plane_width,
1133 upscaled_plane_width, x_step_qn);
1134
1135 for (int j = 0; j < cm->tiles.cols; j++) {
1136 av1_tile_set_col(&tile_col, cm, j);
1137 // Determine the limits of this tile column in both the source
1138 // and destination images.
1139 // Note: The actual location which we start sampling from is
1140 // (downscaled_x0 - 1 + (x0_qn/2^14)), and this quantity increases
1141 // by exactly dst_width * (x_step_qn/2^14) pixels each iteration.
1142 const int downscaled_x0 = tile_col.mi_col_start << (MI_SIZE_LOG2 - ss_x);
1143 const int downscaled_x1 = tile_col.mi_col_end << (MI_SIZE_LOG2 - ss_x);
1144 const int src_width = downscaled_x1 - downscaled_x0;
1145
1146 const int upscaled_x0 = (downscaled_x0 * superres_denom) / SCALE_NUMERATOR;
1147 int upscaled_x1;
1148 if (j == cm->tiles.cols - 1) {
1149 // Note that we can't just use AOMMIN here - due to rounding,
1150 // (downscaled_x1 * superres_denom) / SCALE_NUMERATOR may be less than
1151 // upscaled_plane_width.
1152 upscaled_x1 = upscaled_plane_width;
1153 } else {
1154 upscaled_x1 = (downscaled_x1 * superres_denom) / SCALE_NUMERATOR;
1155 }
1156
1157 const uint8_t *const src_ptr = src + downscaled_x0;
1158 uint8_t *const dst_ptr = dst + upscaled_x0;
1159 const int dst_width = upscaled_x1 - upscaled_x0;
1160
1161 const int pad_left = (j == 0);
1162 const int pad_right = (j == cm->tiles.cols - 1);
1163
1164 bool success;
1165 #if CONFIG_AV1_HIGHBITDEPTH
1166 if (cm->seq_params->use_highbitdepth)
1167 success = highbd_upscale_normative_rect(
1168 src_ptr, rows, src_width, src_stride, dst_ptr, rows, dst_width,
1169 dst_stride, x_step_qn, x0_qn, pad_left, pad_right,
1170 cm->seq_params->bit_depth);
1171 else
1172 success = upscale_normative_rect(src_ptr, rows, src_width, src_stride,
1173 dst_ptr, rows, dst_width, dst_stride,
1174 x_step_qn, x0_qn, pad_left, pad_right);
1175 #else
1176 success = upscale_normative_rect(src_ptr, rows, src_width, src_stride,
1177 dst_ptr, rows, dst_width, dst_stride,
1178 x_step_qn, x0_qn, pad_left, pad_right);
1179 #endif
1180 if (!success) {
1181 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1182 "Error upscaling frame");
1183 }
1184 // Update the fractional pixel offset to prepare for the next tile column.
1185 x0_qn += (dst_width * x_step_qn) - (src_width << RS_SCALE_SUBPEL_BITS);
1186 }
1187 }
1188
upscale_normative_and_extend_frame(const AV1_COMMON * cm,const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst)1189 static void upscale_normative_and_extend_frame(const AV1_COMMON *cm,
1190 const YV12_BUFFER_CONFIG *src,
1191 YV12_BUFFER_CONFIG *dst) {
1192 const int num_planes = av1_num_planes(cm);
1193 for (int i = 0; i < num_planes; ++i) {
1194 const int is_uv = (i > 0);
1195 av1_upscale_normative_rows(cm, src->buffers[i], src->strides[is_uv],
1196 dst->buffers[i], dst->strides[is_uv], i,
1197 src->crop_heights[is_uv]);
1198 }
1199
1200 aom_extend_frame_borders(dst, num_planes);
1201 }
1202
av1_realloc_and_scale_if_required(AV1_COMMON * cm,YV12_BUFFER_CONFIG * unscaled,YV12_BUFFER_CONFIG * scaled,const InterpFilter filter,const int phase,const bool use_optimized_scaler,const bool for_psnr,const int border_in_pixels,const bool alloc_pyramid)1203 YV12_BUFFER_CONFIG *av1_realloc_and_scale_if_required(
1204 AV1_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
1205 const InterpFilter filter, const int phase, const bool use_optimized_scaler,
1206 const bool for_psnr, const int border_in_pixels, const bool alloc_pyramid) {
1207 // If scaling is performed for the sole purpose of calculating PSNR, then our
1208 // target dimensions are superres upscaled width/height. Otherwise our target
1209 // dimensions are coded width/height.
1210 const int scaled_width = for_psnr ? cm->superres_upscaled_width : cm->width;
1211 const int scaled_height =
1212 for_psnr ? cm->superres_upscaled_height : cm->height;
1213 const bool scaling_required = (scaled_width != unscaled->y_crop_width) ||
1214 (scaled_height != unscaled->y_crop_height);
1215
1216 if (scaling_required) {
1217 const int num_planes = av1_num_planes(cm);
1218 const SequenceHeader *seq_params = cm->seq_params;
1219
1220 // Reallocate the frame buffer based on the target dimensions when scaling
1221 // is required.
1222 if (aom_realloc_frame_buffer(
1223 scaled, scaled_width, scaled_height, seq_params->subsampling_x,
1224 seq_params->subsampling_y, seq_params->use_highbitdepth,
1225 border_in_pixels, cm->features.byte_alignment, NULL, NULL, NULL,
1226 alloc_pyramid, 0))
1227 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1228 "Failed to allocate scaled buffer");
1229
1230 bool has_optimized_scaler = av1_has_optimized_scaler(
1231 unscaled->y_crop_width, unscaled->y_crop_height, scaled_width,
1232 scaled_height);
1233 if (num_planes > 1) {
1234 has_optimized_scaler = has_optimized_scaler &&
1235 av1_has_optimized_scaler(unscaled->uv_crop_width,
1236 unscaled->uv_crop_height,
1237 scaled->uv_crop_width,
1238 scaled->uv_crop_height);
1239 }
1240
1241 #if CONFIG_AV1_HIGHBITDEPTH
1242 if (use_optimized_scaler && has_optimized_scaler &&
1243 cm->seq_params->bit_depth == AOM_BITS_8) {
1244 av1_resize_and_extend_frame(unscaled, scaled, filter, phase, num_planes);
1245 } else {
1246 if (!av1_resize_and_extend_frame_nonnormative(
1247 unscaled, scaled, (int)cm->seq_params->bit_depth, num_planes))
1248 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1249 "Failed to allocate buffers during resize");
1250 }
1251 #else
1252 if (use_optimized_scaler && has_optimized_scaler) {
1253 av1_resize_and_extend_frame(unscaled, scaled, filter, phase, num_planes);
1254 } else {
1255 if (!av1_resize_and_extend_frame_nonnormative(
1256 unscaled, scaled, (int)cm->seq_params->bit_depth, num_planes))
1257 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1258 "Failed to allocate buffers during resize");
1259 }
1260 #endif
1261 return scaled;
1262 }
1263 return unscaled;
1264 }
1265
1266 // Calculates the scaled dimension given the original dimension and the scale
1267 // denominator.
calculate_scaled_size_helper(int * dim,int denom)1268 static void calculate_scaled_size_helper(int *dim, int denom) {
1269 if (denom != SCALE_NUMERATOR) {
1270 // We need to ensure the constraint in "Appendix A" of the spec:
1271 // * FrameWidth is greater than or equal to 16
1272 // * FrameHeight is greater than or equal to 16
1273 // For this, we clamp the downscaled dimension to at least 16. One
1274 // exception: if original dimension itself was < 16, then we keep the
1275 // downscaled dimension to be same as the original, to ensure that resizing
1276 // is valid.
1277 const int min_dim = AOMMIN(16, *dim);
1278 // Use this version if we need *dim to be even
1279 // *width = (*width * SCALE_NUMERATOR + denom) / (2 * denom);
1280 // *width <<= 1;
1281 *dim = (*dim * SCALE_NUMERATOR + denom / 2) / (denom);
1282 *dim = AOMMAX(*dim, min_dim);
1283 }
1284 }
1285
av1_calculate_scaled_size(int * width,int * height,int resize_denom)1286 void av1_calculate_scaled_size(int *width, int *height, int resize_denom) {
1287 calculate_scaled_size_helper(width, resize_denom);
1288 calculate_scaled_size_helper(height, resize_denom);
1289 }
1290
av1_calculate_scaled_superres_size(int * width,int * height,int superres_denom)1291 void av1_calculate_scaled_superres_size(int *width, int *height,
1292 int superres_denom) {
1293 (void)height;
1294 calculate_scaled_size_helper(width, superres_denom);
1295 }
1296
av1_calculate_unscaled_superres_size(int * width,int * height,int denom)1297 void av1_calculate_unscaled_superres_size(int *width, int *height, int denom) {
1298 if (denom != SCALE_NUMERATOR) {
1299 // Note: av1_calculate_scaled_superres_size() rounds *up* after division
1300 // when the resulting dimensions are odd. So here, we round *down*.
1301 *width = *width * denom / SCALE_NUMERATOR;
1302 (void)height;
1303 }
1304 }
1305
1306 // Copy only the config data from 'src' to 'dst'.
copy_buffer_config(const YV12_BUFFER_CONFIG * const src,YV12_BUFFER_CONFIG * const dst)1307 static void copy_buffer_config(const YV12_BUFFER_CONFIG *const src,
1308 YV12_BUFFER_CONFIG *const dst) {
1309 dst->bit_depth = src->bit_depth;
1310 dst->color_primaries = src->color_primaries;
1311 dst->transfer_characteristics = src->transfer_characteristics;
1312 dst->matrix_coefficients = src->matrix_coefficients;
1313 dst->monochrome = src->monochrome;
1314 dst->chroma_sample_position = src->chroma_sample_position;
1315 dst->color_range = src->color_range;
1316 }
1317
1318 // TODO(afergs): Look for in-place upscaling
1319 // TODO(afergs): aom_ vs av1_ functions? Which can I use?
1320 // Upscale decoded image.
av1_superres_upscale(AV1_COMMON * cm,BufferPool * const pool,bool alloc_pyramid)1321 void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool,
1322 bool alloc_pyramid) {
1323 const int num_planes = av1_num_planes(cm);
1324 if (!av1_superres_scaled(cm)) return;
1325 const SequenceHeader *const seq_params = cm->seq_params;
1326 const int byte_alignment = cm->features.byte_alignment;
1327
1328 YV12_BUFFER_CONFIG copy_buffer;
1329 memset(©_buffer, 0, sizeof(copy_buffer));
1330
1331 YV12_BUFFER_CONFIG *const frame_to_show = &cm->cur_frame->buf;
1332
1333 const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, 3);
1334 if (aom_alloc_frame_buffer(
1335 ©_buffer, aligned_width, cm->height, seq_params->subsampling_x,
1336 seq_params->subsampling_y, seq_params->use_highbitdepth,
1337 AOM_BORDER_IN_PIXELS, byte_alignment, false, 0))
1338 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1339 "Failed to allocate copy buffer for superres upscaling");
1340
1341 // Copy function assumes the frames are the same size.
1342 // Note that it does not copy YV12_BUFFER_CONFIG config data.
1343 aom_yv12_copy_frame(frame_to_show, ©_buffer, num_planes);
1344
1345 assert(copy_buffer.y_crop_width == aligned_width);
1346 assert(copy_buffer.y_crop_height == cm->height);
1347
1348 // Realloc the current frame buffer at a higher resolution in place.
1349 if (pool != NULL) {
1350 // Use callbacks if on the decoder.
1351 aom_codec_frame_buffer_t *fb = &cm->cur_frame->raw_frame_buffer;
1352 aom_release_frame_buffer_cb_fn_t release_fb_cb = pool->release_fb_cb;
1353 aom_get_frame_buffer_cb_fn_t cb = pool->get_fb_cb;
1354 void *cb_priv = pool->cb_priv;
1355
1356 lock_buffer_pool(pool);
1357 // Realloc with callback does not release the frame buffer - release first.
1358 if (release_fb_cb(cb_priv, fb)) {
1359 unlock_buffer_pool(pool);
1360 aom_internal_error(
1361 cm->error, AOM_CODEC_MEM_ERROR,
1362 "Failed to free current frame buffer before superres upscaling");
1363 }
1364 // aom_realloc_frame_buffer() leaves config data for frame_to_show intact
1365 if (aom_realloc_frame_buffer(
1366 frame_to_show, cm->superres_upscaled_width,
1367 cm->superres_upscaled_height, seq_params->subsampling_x,
1368 seq_params->subsampling_y, seq_params->use_highbitdepth,
1369 AOM_BORDER_IN_PIXELS, byte_alignment, fb, cb, cb_priv,
1370 alloc_pyramid, 0)) {
1371 unlock_buffer_pool(pool);
1372 aom_internal_error(
1373 cm->error, AOM_CODEC_MEM_ERROR,
1374 "Failed to allocate current frame buffer for superres upscaling");
1375 }
1376 unlock_buffer_pool(pool);
1377 } else {
1378 // Make a copy of the config data for frame_to_show in copy_buffer
1379 copy_buffer_config(frame_to_show, ©_buffer);
1380
1381 // Don't use callbacks on the encoder.
1382 // aom_alloc_frame_buffer() clears the config data for frame_to_show
1383 if (aom_alloc_frame_buffer(
1384 frame_to_show, cm->superres_upscaled_width,
1385 cm->superres_upscaled_height, seq_params->subsampling_x,
1386 seq_params->subsampling_y, seq_params->use_highbitdepth,
1387 AOM_BORDER_IN_PIXELS, byte_alignment, alloc_pyramid, 0))
1388 aom_internal_error(
1389 cm->error, AOM_CODEC_MEM_ERROR,
1390 "Failed to reallocate current frame buffer for superres upscaling");
1391
1392 // Restore config data back to frame_to_show
1393 copy_buffer_config(©_buffer, frame_to_show);
1394 }
1395 // TODO(afergs): verify frame_to_show is correct after realloc
1396 // encoder:
1397 // decoder:
1398
1399 assert(frame_to_show->y_crop_width == cm->superres_upscaled_width);
1400 assert(frame_to_show->y_crop_height == cm->superres_upscaled_height);
1401
1402 // Scale up and back into frame_to_show.
1403 assert(frame_to_show->y_crop_width != cm->width);
1404 upscale_normative_and_extend_frame(cm, ©_buffer, frame_to_show);
1405
1406 // Free the copy buffer
1407 aom_free_frame_buffer(©_buffer);
1408 }
1409