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