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