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