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(const uint8_t * const input,int in_length,uint8_t * output,int out_length)316 static void interpolate(const uint8_t *const input, int in_length,
317                         uint8_t *output, int out_length) {
318   const InterpKernel *interp_filters =
319       choose_interp_filter(in_length, out_length);
320 
321   interpolate_core(input, in_length, output, out_length, &interp_filters[0][0],
322                    SUBPEL_TAPS);
323 }
324 
av1_get_upscale_convolve_step(int in_length,int out_length)325 int32_t av1_get_upscale_convolve_step(int in_length, int out_length) {
326   return ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length;
327 }
328 
get_upscale_convolve_x0(int in_length,int out_length,int32_t x_step_qn)329 static int32_t get_upscale_convolve_x0(int in_length, int out_length,
330                                        int32_t x_step_qn) {
331   const int err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS);
332   const int32_t x0 =
333       (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
334        out_length / 2) /
335           out_length +
336       RS_SCALE_EXTRA_OFF - err / 2;
337   return (int32_t)((uint32_t)x0 & RS_SCALE_SUBPEL_MASK);
338 }
339 
340 #ifndef __clang_analyzer__
down2_symeven(const uint8_t * const input,int length,uint8_t * output)341 static void down2_symeven(const uint8_t *const input, int length,
342                           uint8_t *output) {
343   // Actual filter len = 2 * filter_len_half.
344   const int16_t *filter = av1_down2_symeven_half_filter;
345   const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
346   int i, j;
347   uint8_t *optr = output;
348   int l1 = filter_len_half;
349   int l2 = (length - filter_len_half);
350   l1 += (l1 & 1);
351   l2 += (l2 & 1);
352   if (l1 > l2) {
353     // Short input length.
354     for (i = 0; i < length; i += 2) {
355       int sum = (1 << (FILTER_BITS - 1));
356       for (j = 0; j < filter_len_half; ++j) {
357         sum +=
358             (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + 1 + j, length - 1)]) *
359             filter[j];
360       }
361       sum >>= FILTER_BITS;
362       *optr++ = clip_pixel(sum);
363     }
364   } else {
365     // Initial part.
366     for (i = 0; i < l1; i += 2) {
367       int sum = (1 << (FILTER_BITS - 1));
368       for (j = 0; j < filter_len_half; ++j) {
369         sum += (input[AOMMAX(i - j, 0)] + input[i + 1 + j]) * filter[j];
370       }
371       sum >>= FILTER_BITS;
372       *optr++ = clip_pixel(sum);
373     }
374     // Middle part.
375     for (; i < l2; i += 2) {
376       int sum = (1 << (FILTER_BITS - 1));
377       for (j = 0; j < filter_len_half; ++j) {
378         sum += (input[i - j] + input[i + 1 + j]) * filter[j];
379       }
380       sum >>= FILTER_BITS;
381       *optr++ = clip_pixel(sum);
382     }
383     // End part.
384     for (; i < length; i += 2) {
385       int sum = (1 << (FILTER_BITS - 1));
386       for (j = 0; j < filter_len_half; ++j) {
387         sum +=
388             (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
389       }
390       sum >>= FILTER_BITS;
391       *optr++ = clip_pixel(sum);
392     }
393   }
394 }
395 #endif
396 
down2_symodd(const uint8_t * const input,int length,uint8_t * output)397 static void down2_symodd(const uint8_t *const input, int length,
398                          uint8_t *output) {
399   // Actual filter len = 2 * filter_len_half - 1.
400   const int16_t *filter = av1_down2_symodd_half_filter;
401   const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
402   int i, j;
403   uint8_t *optr = output;
404   int l1 = filter_len_half - 1;
405   int l2 = (length - filter_len_half + 1);
406   l1 += (l1 & 1);
407   l2 += (l2 & 1);
408   if (l1 > l2) {
409     // Short input length.
410     for (i = 0; i < length; i += 2) {
411       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
412       for (j = 1; j < filter_len_half; ++j) {
413         sum += (input[(i - j < 0 ? 0 : i - j)] +
414                 input[(i + j >= length ? length - 1 : i + j)]) *
415                filter[j];
416       }
417       sum >>= FILTER_BITS;
418       *optr++ = clip_pixel(sum);
419     }
420   } else {
421     // Initial part.
422     for (i = 0; i < l1; i += 2) {
423       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
424       for (j = 1; j < filter_len_half; ++j) {
425         sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
426       }
427       sum >>= FILTER_BITS;
428       *optr++ = clip_pixel(sum);
429     }
430     // Middle part.
431     for (; i < l2; i += 2) {
432       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
433       for (j = 1; j < filter_len_half; ++j) {
434         sum += (input[i - j] + input[i + j]) * filter[j];
435       }
436       sum >>= FILTER_BITS;
437       *optr++ = clip_pixel(sum);
438     }
439     // End part.
440     for (; i < length; i += 2) {
441       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
442       for (j = 1; j < filter_len_half; ++j) {
443         sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
444                filter[j];
445       }
446       sum >>= FILTER_BITS;
447       *optr++ = clip_pixel(sum);
448     }
449   }
450 }
451 
get_down2_length(int length,int steps)452 static int get_down2_length(int length, int steps) {
453   for (int s = 0; s < steps; ++s) length = (length + 1) >> 1;
454   return length;
455 }
456 
get_down2_steps(int in_length,int out_length)457 static int get_down2_steps(int in_length, int out_length) {
458   int steps = 0;
459   int proj_in_length;
460   while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
461     ++steps;
462     in_length = proj_in_length;
463     if (in_length == 1) {
464       // Special case: we break because any further calls to get_down2_length()
465       // with be with length == 1, which return 1, resulting in an infinite
466       // loop.
467       break;
468     }
469   }
470   return steps;
471 }
472 
resize_multistep(const uint8_t * const input,int length,uint8_t * output,int olength,uint8_t * otmp)473 static void resize_multistep(const uint8_t *const input, int length,
474                              uint8_t *output, int olength, uint8_t *otmp) {
475   if (length == olength) {
476     memcpy(output, input, sizeof(output[0]) * length);
477     return;
478   }
479   const int steps = get_down2_steps(length, olength);
480 
481   if (steps > 0) {
482     uint8_t *out = NULL;
483     int filteredlength = length;
484 
485     assert(otmp != NULL);
486     uint8_t *otmp2 = otmp + get_down2_length(length, 1);
487     for (int s = 0; s < steps; ++s) {
488       const int proj_filteredlength = get_down2_length(filteredlength, 1);
489       const uint8_t *const in = (s == 0 ? input : out);
490       if (s == steps - 1 && proj_filteredlength == olength)
491         out = output;
492       else
493         out = (s & 1 ? otmp2 : otmp);
494       if (filteredlength & 1)
495         down2_symodd(in, filteredlength, out);
496       else
497         down2_symeven(in, filteredlength, out);
498       filteredlength = proj_filteredlength;
499     }
500     if (filteredlength != olength) {
501       interpolate(out, filteredlength, output, olength);
502     }
503   } else {
504     interpolate(input, length, output, olength);
505   }
506 }
507 
fill_col_to_arr(uint8_t * img,int stride,int len,uint8_t * arr)508 static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
509   int i;
510   uint8_t *iptr = img;
511   uint8_t *aptr = arr;
512   for (i = 0; i < len; ++i, iptr += stride) {
513     *aptr++ = *iptr;
514   }
515 }
516 
fill_arr_to_col(uint8_t * img,int stride,int len,uint8_t * arr)517 static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
518   int i;
519   uint8_t *iptr = img;
520   uint8_t *aptr = arr;
521   for (i = 0; i < len; ++i, iptr += stride) {
522     *iptr = *aptr++;
523   }
524 }
525 
resize_plane(const uint8_t * const input,int height,int width,int in_stride,uint8_t * output,int height2,int width2,int out_stride)526 static void resize_plane(const uint8_t *const input, int height, int width,
527                          int in_stride, uint8_t *output, int height2,
528                          int width2, int out_stride) {
529   int i;
530   uint8_t *intbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * width2 * height);
531   uint8_t *tmpbuf =
532       (uint8_t *)aom_malloc(sizeof(uint8_t) * AOMMAX(width, height));
533   uint8_t *arrbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * height);
534   uint8_t *arrbuf2 = (uint8_t *)aom_malloc(sizeof(uint8_t) * height2);
535   if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
536     goto Error;
537   assert(width > 0);
538   assert(height > 0);
539   assert(width2 > 0);
540   assert(height2 > 0);
541   for (i = 0; i < height; ++i)
542     resize_multistep(input + in_stride * i, width, intbuf + width2 * i, width2,
543                      tmpbuf);
544   for (i = 0; i < width2; ++i) {
545     fill_col_to_arr(intbuf + i, width2, height, arrbuf);
546     resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf);
547     fill_arr_to_col(output + i, out_stride, height2, arrbuf2);
548   }
549 
550 Error:
551   aom_free(intbuf);
552   aom_free(tmpbuf);
553   aom_free(arrbuf);
554   aom_free(arrbuf2);
555 }
556 
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)557 static void upscale_normative_rect(const uint8_t *const input, int height,
558                                    int width, int in_stride, uint8_t *output,
559                                    int height2, int width2, int out_stride,
560                                    int x_step_qn, int x0_qn, int pad_left,
561                                    int pad_right) {
562   assert(width > 0);
563   assert(height > 0);
564   assert(width2 > 0);
565   assert(height2 > 0);
566   assert(height2 == height);
567 
568   // Extend the left/right pixels of the tile column if needed
569   // (either because we can't sample from other tiles, or because we're at
570   // a frame edge).
571   // Save the overwritten pixels into tmp_left and tmp_right.
572   // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra
573   // column of border pixels compared to what we'd naively think.
574   const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1;
575   uint8_t *tmp_left =
576       NULL;  // Silence spurious "may be used uninitialized" warnings
577   uint8_t *tmp_right = NULL;
578   uint8_t *const in_tl = (uint8_t *)(input - border_cols);  // Cast off 'const'
579   uint8_t *const in_tr = (uint8_t *)(input + width);
580   if (pad_left) {
581     tmp_left = (uint8_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height);
582     for (int i = 0; i < height; i++) {
583       memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_cols);
584       memset(in_tl + i * in_stride, input[i * in_stride], border_cols);
585     }
586   }
587   if (pad_right) {
588     tmp_right =
589         (uint8_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height);
590     for (int i = 0; i < height; i++) {
591       memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_cols);
592       memset(in_tr + i * in_stride, input[i * in_stride + width - 1],
593              border_cols);
594     }
595   }
596 
597   av1_convolve_horiz_rs(input - 1, in_stride, output, out_stride, width2,
598                         height2, &av1_resize_filter_normative[0][0], x0_qn,
599                         x_step_qn);
600 
601   // Restore the left/right border pixels
602   if (pad_left) {
603     for (int i = 0; i < height; i++) {
604       memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_cols);
605     }
606     aom_free(tmp_left);
607   }
608   if (pad_right) {
609     for (int i = 0; i < height; i++) {
610       memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_cols);
611     }
612     aom_free(tmp_right);
613   }
614 }
615 
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)616 static void highbd_interpolate_core(const uint16_t *const input, int in_length,
617                                     uint16_t *output, int out_length, int bd,
618                                     const int16_t *interp_filters,
619                                     int interp_taps) {
620   const int32_t delta =
621       (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) /
622       out_length;
623   const int32_t offset =
624       in_length > out_length
625           ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
626              out_length / 2) /
627                 out_length
628           : -(((int32_t)(out_length - in_length)
629                << (RS_SCALE_SUBPEL_BITS - 1)) +
630               out_length / 2) /
631                 out_length;
632   uint16_t *optr = output;
633   int x, x1, x2, sum, k, int_pel, sub_pel;
634   int32_t y;
635 
636   x = 0;
637   y = offset + RS_SCALE_EXTRA_OFF;
638   while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) {
639     x++;
640     y += delta;
641   }
642   x1 = x;
643   x = out_length - 1;
644   y = delta * x + offset + RS_SCALE_EXTRA_OFF;
645   while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >=
646          in_length) {
647     x--;
648     y -= delta;
649   }
650   x2 = x;
651   if (x1 > x2) {
652     for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length;
653          ++x, y += delta) {
654       int_pel = y >> RS_SCALE_SUBPEL_BITS;
655       sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
656       const int16_t *filter = &interp_filters[sub_pel * interp_taps];
657       sum = 0;
658       for (k = 0; k < interp_taps; ++k) {
659         const int pk = int_pel - interp_taps / 2 + 1 + k;
660         sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)];
661       }
662       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
663     }
664   } else {
665     // Initial part.
666     for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) {
667       int_pel = y >> RS_SCALE_SUBPEL_BITS;
668       sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
669       const int16_t *filter = &interp_filters[sub_pel * interp_taps];
670       sum = 0;
671       for (k = 0; k < interp_taps; ++k)
672         sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)];
673       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
674     }
675     // Middle part.
676     for (; x <= x2; ++x, y += delta) {
677       int_pel = y >> RS_SCALE_SUBPEL_BITS;
678       sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
679       const int16_t *filter = &interp_filters[sub_pel * interp_taps];
680       sum = 0;
681       for (k = 0; k < interp_taps; ++k)
682         sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k];
683       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
684     }
685     // End part.
686     for (; x < out_length; ++x, y += delta) {
687       int_pel = y >> RS_SCALE_SUBPEL_BITS;
688       sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
689       const int16_t *filter = &interp_filters[sub_pel * interp_taps];
690       sum = 0;
691       for (k = 0; k < interp_taps; ++k)
692         sum += filter[k] *
693                input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)];
694       *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
695     }
696   }
697 }
698 
highbd_interpolate(const uint16_t * const input,int in_length,uint16_t * output,int out_length,int bd)699 static void highbd_interpolate(const uint16_t *const input, int in_length,
700                                uint16_t *output, int out_length, int bd) {
701   const InterpKernel *interp_filters =
702       choose_interp_filter(in_length, out_length);
703 
704   highbd_interpolate_core(input, in_length, output, out_length, bd,
705                           &interp_filters[0][0], SUBPEL_TAPS);
706 }
707 
708 #ifndef __clang_analyzer__
highbd_down2_symeven(const uint16_t * const input,int length,uint16_t * output,int bd)709 static void highbd_down2_symeven(const uint16_t *const input, int length,
710                                  uint16_t *output, int bd) {
711   // Actual filter len = 2 * filter_len_half.
712   static const int16_t *filter = av1_down2_symeven_half_filter;
713   const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
714   int i, j;
715   uint16_t *optr = output;
716   int l1 = filter_len_half;
717   int l2 = (length - filter_len_half);
718   l1 += (l1 & 1);
719   l2 += (l2 & 1);
720   if (l1 > l2) {
721     // Short input length.
722     for (i = 0; i < length; i += 2) {
723       int sum = (1 << (FILTER_BITS - 1));
724       for (j = 0; j < filter_len_half; ++j) {
725         sum +=
726             (input[AOMMAX(0, i - j)] + input[AOMMIN(i + 1 + j, length - 1)]) *
727             filter[j];
728       }
729       sum >>= FILTER_BITS;
730       *optr++ = clip_pixel_highbd(sum, bd);
731     }
732   } else {
733     // Initial part.
734     for (i = 0; i < l1; i += 2) {
735       int sum = (1 << (FILTER_BITS - 1));
736       for (j = 0; j < filter_len_half; ++j) {
737         sum += (input[AOMMAX(0, i - j)] + input[i + 1 + j]) * filter[j];
738       }
739       sum >>= FILTER_BITS;
740       *optr++ = clip_pixel_highbd(sum, bd);
741     }
742     // Middle part.
743     for (; i < l2; i += 2) {
744       int sum = (1 << (FILTER_BITS - 1));
745       for (j = 0; j < filter_len_half; ++j) {
746         sum += (input[i - j] + input[i + 1 + j]) * filter[j];
747       }
748       sum >>= FILTER_BITS;
749       *optr++ = clip_pixel_highbd(sum, bd);
750     }
751     // End part.
752     for (; i < length; i += 2) {
753       int sum = (1 << (FILTER_BITS - 1));
754       for (j = 0; j < filter_len_half; ++j) {
755         sum +=
756             (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
757       }
758       sum >>= FILTER_BITS;
759       *optr++ = clip_pixel_highbd(sum, bd);
760     }
761   }
762 }
763 
highbd_down2_symodd(const uint16_t * const input,int length,uint16_t * output,int bd)764 static void highbd_down2_symodd(const uint16_t *const input, int length,
765                                 uint16_t *output, int bd) {
766   // Actual filter len = 2 * filter_len_half - 1.
767   static const int16_t *filter = av1_down2_symodd_half_filter;
768   const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
769   int i, j;
770   uint16_t *optr = output;
771   int l1 = filter_len_half - 1;
772   int l2 = (length - filter_len_half + 1);
773   l1 += (l1 & 1);
774   l2 += (l2 & 1);
775   if (l1 > l2) {
776     // Short input length.
777     for (i = 0; i < length; i += 2) {
778       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
779       for (j = 1; j < filter_len_half; ++j) {
780         sum += (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + j, length - 1)]) *
781                filter[j];
782       }
783       sum >>= FILTER_BITS;
784       *optr++ = clip_pixel_highbd(sum, bd);
785     }
786   } else {
787     // Initial part.
788     for (i = 0; i < l1; i += 2) {
789       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
790       for (j = 1; j < filter_len_half; ++j) {
791         sum += (input[AOMMAX(i - j, 0)] + input[i + j]) * filter[j];
792       }
793       sum >>= FILTER_BITS;
794       *optr++ = clip_pixel_highbd(sum, bd);
795     }
796     // Middle part.
797     for (; i < l2; i += 2) {
798       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
799       for (j = 1; j < filter_len_half; ++j) {
800         sum += (input[i - j] + input[i + j]) * filter[j];
801       }
802       sum >>= FILTER_BITS;
803       *optr++ = clip_pixel_highbd(sum, bd);
804     }
805     // End part.
806     for (; i < length; i += 2) {
807       int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
808       for (j = 1; j < filter_len_half; ++j) {
809         sum += (input[i - j] + input[AOMMIN(i + j, length - 1)]) * filter[j];
810       }
811       sum >>= FILTER_BITS;
812       *optr++ = clip_pixel_highbd(sum, bd);
813     }
814   }
815 }
816 #endif
817 
highbd_resize_multistep(const uint16_t * const input,int length,uint16_t * output,int olength,uint16_t * otmp,int bd)818 static void highbd_resize_multistep(const uint16_t *const input, int length,
819                                     uint16_t *output, int olength,
820                                     uint16_t *otmp, int bd) {
821   if (length == olength) {
822     memcpy(output, input, sizeof(output[0]) * length);
823     return;
824   }
825   const int steps = get_down2_steps(length, olength);
826 
827   if (steps > 0) {
828     uint16_t *out = NULL;
829     int filteredlength = length;
830 
831     assert(otmp != NULL);
832     uint16_t *otmp2 = otmp + get_down2_length(length, 1);
833     for (int s = 0; s < steps; ++s) {
834       const int proj_filteredlength = get_down2_length(filteredlength, 1);
835       const uint16_t *const in = (s == 0 ? input : out);
836       if (s == steps - 1 && proj_filteredlength == olength)
837         out = output;
838       else
839         out = (s & 1 ? otmp2 : otmp);
840       if (filteredlength & 1)
841         highbd_down2_symodd(in, filteredlength, out, bd);
842       else
843         highbd_down2_symeven(in, filteredlength, out, bd);
844       filteredlength = proj_filteredlength;
845     }
846     if (filteredlength != olength) {
847       highbd_interpolate(out, filteredlength, output, olength, bd);
848     }
849   } else {
850     highbd_interpolate(input, length, output, olength, bd);
851   }
852 }
853 
highbd_fill_col_to_arr(uint16_t * img,int stride,int len,uint16_t * arr)854 static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
855                                    uint16_t *arr) {
856   int i;
857   uint16_t *iptr = img;
858   uint16_t *aptr = arr;
859   for (i = 0; i < len; ++i, iptr += stride) {
860     *aptr++ = *iptr;
861   }
862 }
863 
highbd_fill_arr_to_col(uint16_t * img,int stride,int len,uint16_t * arr)864 static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
865                                    uint16_t *arr) {
866   int i;
867   uint16_t *iptr = img;
868   uint16_t *aptr = arr;
869   for (i = 0; i < len; ++i, iptr += stride) {
870     *iptr = *aptr++;
871   }
872 }
873 
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)874 static void highbd_resize_plane(const uint8_t *const input, int height,
875                                 int width, int in_stride, uint8_t *output,
876                                 int height2, int width2, int out_stride,
877                                 int bd) {
878   int i;
879   uint16_t *intbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * width2 * height);
880   uint16_t *tmpbuf =
881       (uint16_t *)aom_malloc(sizeof(uint16_t) * AOMMAX(width, height));
882   uint16_t *arrbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * height);
883   uint16_t *arrbuf2 = (uint16_t *)aom_malloc(sizeof(uint16_t) * height2);
884   if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
885     goto Error;
886   for (i = 0; i < height; ++i) {
887     highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
888                             intbuf + width2 * i, width2, tmpbuf, bd);
889   }
890   for (i = 0; i < width2; ++i) {
891     highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
892     highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
893     highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
894                            arrbuf2);
895   }
896 
897 Error:
898   aom_free(intbuf);
899   aom_free(tmpbuf);
900   aom_free(arrbuf);
901   aom_free(arrbuf2);
902 }
903 
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)904 static void highbd_upscale_normative_rect(const uint8_t *const input,
905                                           int height, int width, int in_stride,
906                                           uint8_t *output, int height2,
907                                           int width2, int out_stride,
908                                           int x_step_qn, int x0_qn,
909                                           int pad_left, int pad_right, int bd) {
910   assert(width > 0);
911   assert(height > 0);
912   assert(width2 > 0);
913   assert(height2 > 0);
914   assert(height2 == height);
915 
916   // Extend the left/right pixels of the tile column if needed
917   // (either because we can't sample from other tiles, or because we're at
918   // a frame edge).
919   // Save the overwritten pixels into tmp_left and tmp_right.
920   // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra
921   // column of border pixels compared to what we'd naively think.
922   const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1;
923   const int border_size = border_cols * sizeof(uint16_t);
924   uint16_t *tmp_left =
925       NULL;  // Silence spurious "may be used uninitialized" warnings
926   uint16_t *tmp_right = NULL;
927   uint16_t *const input16 = CONVERT_TO_SHORTPTR(input);
928   uint16_t *const in_tl = input16 - border_cols;
929   uint16_t *const in_tr = input16 + width;
930   if (pad_left) {
931     tmp_left = (uint16_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height);
932     for (int i = 0; i < height; i++) {
933       memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_size);
934       aom_memset16(in_tl + i * in_stride, input16[i * in_stride], border_cols);
935     }
936   }
937   if (pad_right) {
938     tmp_right =
939         (uint16_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height);
940     for (int i = 0; i < height; i++) {
941       memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_size);
942       aom_memset16(in_tr + i * in_stride, input16[i * in_stride + width - 1],
943                    border_cols);
944     }
945   }
946 
947   av1_highbd_convolve_horiz_rs(CONVERT_TO_SHORTPTR(input - 1), in_stride,
948                                CONVERT_TO_SHORTPTR(output), out_stride, width2,
949                                height2, &av1_resize_filter_normative[0][0],
950                                x0_qn, x_step_qn, bd);
951 
952   // Restore the left/right border pixels
953   if (pad_left) {
954     for (int i = 0; i < height; i++) {
955       memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_size);
956     }
957     aom_free(tmp_left);
958   }
959   if (pad_right) {
960     for (int i = 0; i < height; i++) {
961       memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_size);
962     }
963     aom_free(tmp_right);
964   }
965 }
966 
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)967 void av1_resize_frame420(const uint8_t *const y, int y_stride,
968                          const uint8_t *const u, const uint8_t *const v,
969                          int uv_stride, int height, int width, uint8_t *oy,
970                          int oy_stride, uint8_t *ou, uint8_t *ov,
971                          int ouv_stride, int oheight, int owidth) {
972   resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
973   resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2, owidth / 2,
974                ouv_stride);
975   resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2, owidth / 2,
976                ouv_stride);
977 }
978 
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)979 void av1_resize_frame422(const uint8_t *const y, int y_stride,
980                          const uint8_t *const u, const uint8_t *const v,
981                          int uv_stride, int height, int width, uint8_t *oy,
982                          int oy_stride, uint8_t *ou, uint8_t *ov,
983                          int ouv_stride, int oheight, int owidth) {
984   resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
985   resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2,
986                ouv_stride);
987   resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2,
988                ouv_stride);
989 }
990 
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)991 void av1_resize_frame444(const uint8_t *const y, int y_stride,
992                          const uint8_t *const u, const uint8_t *const v,
993                          int uv_stride, int height, int width, uint8_t *oy,
994                          int oy_stride, uint8_t *ou, uint8_t *ov,
995                          int ouv_stride, int oheight, int owidth) {
996   resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
997   resize_plane(u, height, width, uv_stride, ou, oheight, owidth, ouv_stride);
998   resize_plane(v, height, width, uv_stride, ov, oheight, owidth, ouv_stride);
999 }
1000 
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)1001 void av1_highbd_resize_frame420(const uint8_t *const y, int y_stride,
1002                                 const uint8_t *const u, const uint8_t *const v,
1003                                 int uv_stride, int height, int width,
1004                                 uint8_t *oy, int oy_stride, uint8_t *ou,
1005                                 uint8_t *ov, int ouv_stride, int oheight,
1006                                 int owidth, int bd) {
1007   highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
1008                       oy_stride, bd);
1009   highbd_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
1010                       owidth / 2, ouv_stride, bd);
1011   highbd_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
1012                       owidth / 2, ouv_stride, bd);
1013 }
1014 
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)1015 void av1_highbd_resize_frame422(const uint8_t *const y, int y_stride,
1016                                 const uint8_t *const u, const uint8_t *const v,
1017                                 int uv_stride, int height, int width,
1018                                 uint8_t *oy, int oy_stride, uint8_t *ou,
1019                                 uint8_t *ov, int ouv_stride, int oheight,
1020                                 int owidth, int bd) {
1021   highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
1022                       oy_stride, bd);
1023   highbd_resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2,
1024                       ouv_stride, bd);
1025   highbd_resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2,
1026                       ouv_stride, bd);
1027 }
1028 
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)1029 void av1_highbd_resize_frame444(const uint8_t *const y, int y_stride,
1030                                 const uint8_t *const u, const uint8_t *const v,
1031                                 int uv_stride, int height, int width,
1032                                 uint8_t *oy, int oy_stride, uint8_t *ou,
1033                                 uint8_t *ov, int ouv_stride, int oheight,
1034                                 int owidth, int bd) {
1035   highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
1036                       oy_stride, bd);
1037   highbd_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
1038                       ouv_stride, bd);
1039   highbd_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
1040                       ouv_stride, bd);
1041 }
1042 
av1_resize_and_extend_frame(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int bd,const int num_planes)1043 void av1_resize_and_extend_frame(const YV12_BUFFER_CONFIG *src,
1044                                  YV12_BUFFER_CONFIG *dst, int bd,
1045                                  const int num_planes) {
1046   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with aom_image_t
1047 
1048   // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
1049   // the static analysis warnings.
1050   for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) {
1051     const int is_uv = i > 0;
1052     if (src->flags & YV12_FLAG_HIGHBITDEPTH)
1053       highbd_resize_plane(src->buffers[i], src->crop_heights[is_uv],
1054                           src->crop_widths[is_uv], src->strides[is_uv],
1055                           dst->buffers[i], dst->crop_heights[is_uv],
1056                           dst->crop_widths[is_uv], dst->strides[is_uv], bd);
1057     else
1058       resize_plane(src->buffers[i], src->crop_heights[is_uv],
1059                    src->crop_widths[is_uv], src->strides[is_uv],
1060                    dst->buffers[i], dst->crop_heights[is_uv],
1061                    dst->crop_widths[is_uv], dst->strides[is_uv]);
1062   }
1063   aom_extend_frame_borders(dst, num_planes);
1064 }
1065 
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)1066 void av1_upscale_normative_rows(const AV1_COMMON *cm, const uint8_t *src,
1067                                 int src_stride, uint8_t *dst, int dst_stride,
1068                                 int plane, int rows) {
1069   const int is_uv = (plane > 0);
1070   const int ss_x = is_uv && cm->seq_params.subsampling_x;
1071   const int downscaled_plane_width = ROUND_POWER_OF_TWO(cm->width, ss_x);
1072   const int upscaled_plane_width =
1073       ROUND_POWER_OF_TWO(cm->superres_upscaled_width, ss_x);
1074   const int superres_denom = cm->superres_scale_denominator;
1075 
1076   TileInfo tile_col;
1077   const int32_t x_step_qn = av1_get_upscale_convolve_step(
1078       downscaled_plane_width, upscaled_plane_width);
1079   int32_t x0_qn = get_upscale_convolve_x0(downscaled_plane_width,
1080                                           upscaled_plane_width, x_step_qn);
1081 
1082   for (int j = 0; j < cm->tile_cols; j++) {
1083     av1_tile_set_col(&tile_col, cm, j);
1084     // Determine the limits of this tile column in both the source
1085     // and destination images.
1086     // Note: The actual location which we start sampling from is
1087     // (downscaled_x0 - 1 + (x0_qn/2^14)), and this quantity increases
1088     // by exactly dst_width * (x_step_qn/2^14) pixels each iteration.
1089     const int downscaled_x0 = tile_col.mi_col_start << (MI_SIZE_LOG2 - ss_x);
1090     const int downscaled_x1 = tile_col.mi_col_end << (MI_SIZE_LOG2 - ss_x);
1091     const int src_width = downscaled_x1 - downscaled_x0;
1092 
1093     const int upscaled_x0 = (downscaled_x0 * superres_denom) / SCALE_NUMERATOR;
1094     int upscaled_x1;
1095     if (j == cm->tile_cols - 1) {
1096       // Note that we can't just use AOMMIN here - due to rounding,
1097       // (downscaled_x1 * superres_denom) / SCALE_NUMERATOR may be less than
1098       // upscaled_plane_width.
1099       upscaled_x1 = upscaled_plane_width;
1100     } else {
1101       upscaled_x1 = (downscaled_x1 * superres_denom) / SCALE_NUMERATOR;
1102     }
1103 
1104     const uint8_t *const src_ptr = src + downscaled_x0;
1105     uint8_t *const dst_ptr = dst + upscaled_x0;
1106     const int dst_width = upscaled_x1 - upscaled_x0;
1107 
1108     const int pad_left = (j == 0);
1109     const int pad_right = (j == cm->tile_cols - 1);
1110 
1111     if (cm->seq_params.use_highbitdepth)
1112       highbd_upscale_normative_rect(src_ptr, rows, src_width, src_stride,
1113                                     dst_ptr, rows, dst_width, dst_stride,
1114                                     x_step_qn, x0_qn, pad_left, pad_right,
1115                                     cm->seq_params.bit_depth);
1116     else
1117       upscale_normative_rect(src_ptr, rows, src_width, src_stride, dst_ptr,
1118                              rows, dst_width, dst_stride, x_step_qn, x0_qn,
1119                              pad_left, pad_right);
1120 
1121     // Update the fractional pixel offset to prepare for the next tile column.
1122     x0_qn += (dst_width * x_step_qn) - (src_width << RS_SCALE_SUBPEL_BITS);
1123   }
1124 }
1125 
av1_upscale_normative_and_extend_frame(const AV1_COMMON * cm,const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst)1126 void av1_upscale_normative_and_extend_frame(const AV1_COMMON *cm,
1127                                             const YV12_BUFFER_CONFIG *src,
1128                                             YV12_BUFFER_CONFIG *dst) {
1129   const int num_planes = av1_num_planes(cm);
1130   for (int i = 0; i < num_planes; ++i) {
1131     const int is_uv = (i > 0);
1132     av1_upscale_normative_rows(cm, src->buffers[i], src->strides[is_uv],
1133                                dst->buffers[i], dst->strides[is_uv], i,
1134                                src->crop_heights[is_uv]);
1135   }
1136 
1137   aom_extend_frame_borders(dst, num_planes);
1138 }
1139 
av1_scale_if_required(AV1_COMMON * cm,YV12_BUFFER_CONFIG * unscaled,YV12_BUFFER_CONFIG * scaled)1140 YV12_BUFFER_CONFIG *av1_scale_if_required(AV1_COMMON *cm,
1141                                           YV12_BUFFER_CONFIG *unscaled,
1142                                           YV12_BUFFER_CONFIG *scaled) {
1143   const int num_planes = av1_num_planes(cm);
1144   if (cm->width != unscaled->y_crop_width ||
1145       cm->height != unscaled->y_crop_height) {
1146     av1_resize_and_extend_frame(unscaled, scaled, (int)cm->seq_params.bit_depth,
1147                                 num_planes);
1148     return scaled;
1149   } else {
1150     return unscaled;
1151   }
1152 }
1153 
1154 // Calculates the scaled dimension given the original dimension and the scale
1155 // denominator.
calculate_scaled_size_helper(int * dim,int denom)1156 static void calculate_scaled_size_helper(int *dim, int denom) {
1157   if (denom != SCALE_NUMERATOR) {
1158     // Use this version if we need *dim to be even
1159     // *width = (*width * SCALE_NUMERATOR + denom) / (2 * denom);
1160     // *width <<= 1;
1161     *dim = (*dim * SCALE_NUMERATOR + denom / 2) / (denom);
1162   }
1163 }
1164 
av1_calculate_scaled_size(int * width,int * height,int resize_denom)1165 void av1_calculate_scaled_size(int *width, int *height, int resize_denom) {
1166   calculate_scaled_size_helper(width, resize_denom);
1167   calculate_scaled_size_helper(height, resize_denom);
1168 }
1169 
av1_calculate_scaled_superres_size(int * width,int * height,int superres_denom)1170 void av1_calculate_scaled_superres_size(int *width, int *height,
1171                                         int superres_denom) {
1172   (void)height;
1173   calculate_scaled_size_helper(width, superres_denom);
1174 }
1175 
av1_calculate_unscaled_superres_size(int * width,int * height,int denom)1176 void av1_calculate_unscaled_superres_size(int *width, int *height, int denom) {
1177   if (denom != SCALE_NUMERATOR) {
1178     // Note: av1_calculate_scaled_superres_size() rounds *up* after division
1179     // when the resulting dimensions are odd. So here, we round *down*.
1180     *width = *width * denom / SCALE_NUMERATOR;
1181     (void)height;
1182   }
1183 }
1184 
1185 // Copy only the config data from 'src' to 'dst'.
copy_buffer_config(const YV12_BUFFER_CONFIG * const src,YV12_BUFFER_CONFIG * const dst)1186 static void copy_buffer_config(const YV12_BUFFER_CONFIG *const src,
1187                                YV12_BUFFER_CONFIG *const dst) {
1188   dst->bit_depth = src->bit_depth;
1189   dst->color_primaries = src->color_primaries;
1190   dst->transfer_characteristics = src->transfer_characteristics;
1191   dst->matrix_coefficients = src->matrix_coefficients;
1192   dst->monochrome = src->monochrome;
1193   dst->chroma_sample_position = src->chroma_sample_position;
1194   dst->color_range = src->color_range;
1195 }
1196 
1197 // TODO(afergs): Look for in-place upscaling
1198 // TODO(afergs): aom_ vs av1_ functions? Which can I use?
1199 // Upscale decoded image.
av1_superres_upscale(AV1_COMMON * cm,BufferPool * const pool)1200 void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool) {
1201   const int num_planes = av1_num_planes(cm);
1202   if (!av1_superres_scaled(cm)) return;
1203   const SequenceHeader *const seq_params = &cm->seq_params;
1204 
1205   YV12_BUFFER_CONFIG copy_buffer;
1206   memset(&copy_buffer, 0, sizeof(copy_buffer));
1207 
1208   YV12_BUFFER_CONFIG *const frame_to_show = get_frame_new_buffer(cm);
1209 
1210   const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, 3);
1211   if (aom_alloc_frame_buffer(
1212           &copy_buffer, aligned_width, cm->height, seq_params->subsampling_x,
1213           seq_params->subsampling_y, seq_params->use_highbitdepth,
1214           AOM_BORDER_IN_PIXELS, cm->byte_alignment))
1215     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
1216                        "Failed to allocate copy buffer for superres upscaling");
1217 
1218   // Copy function assumes the frames are the same size.
1219   // Note that it does not copy YV12_BUFFER_CONFIG config data.
1220   aom_yv12_copy_frame(frame_to_show, &copy_buffer, num_planes);
1221 
1222   assert(copy_buffer.y_crop_width == aligned_width);
1223   assert(copy_buffer.y_crop_height == cm->height);
1224 
1225   // Realloc the current frame buffer at a higher resolution in place.
1226   if (pool != NULL) {
1227     // Use callbacks if on the decoder.
1228     aom_codec_frame_buffer_t *fb =
1229         &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer;
1230     aom_release_frame_buffer_cb_fn_t release_fb_cb = pool->release_fb_cb;
1231     aom_get_frame_buffer_cb_fn_t cb = pool->get_fb_cb;
1232     void *cb_priv = pool->cb_priv;
1233 
1234     // Realloc with callback does not release the frame buffer - release first.
1235     if (release_fb_cb(cb_priv, fb))
1236       aom_internal_error(
1237           &cm->error, AOM_CODEC_MEM_ERROR,
1238           "Failed to free current frame buffer before superres upscaling");
1239 
1240     // aom_realloc_frame_buffer() leaves config data for frame_to_show intact
1241     if (aom_realloc_frame_buffer(
1242             frame_to_show, cm->superres_upscaled_width,
1243             cm->superres_upscaled_height, seq_params->subsampling_x,
1244             seq_params->subsampling_y, seq_params->use_highbitdepth,
1245             AOM_BORDER_IN_PIXELS, cm->byte_alignment, fb, cb, cb_priv))
1246       aom_internal_error(
1247           &cm->error, AOM_CODEC_MEM_ERROR,
1248           "Failed to allocate current frame buffer for superres upscaling");
1249   } else {
1250     // Make a copy of the config data for frame_to_show in copy_buffer
1251     copy_buffer_config(frame_to_show, &copy_buffer);
1252 
1253     // Don't use callbacks on the encoder.
1254     // aom_alloc_frame_buffer() clears the config data for frame_to_show
1255     if (aom_alloc_frame_buffer(
1256             frame_to_show, cm->superres_upscaled_width,
1257             cm->superres_upscaled_height, seq_params->subsampling_x,
1258             seq_params->subsampling_y, seq_params->use_highbitdepth,
1259             AOM_BORDER_IN_PIXELS, cm->byte_alignment))
1260       aom_internal_error(
1261           &cm->error, AOM_CODEC_MEM_ERROR,
1262           "Failed to reallocate current frame buffer for superres upscaling");
1263 
1264     // Restore config data back to frame_to_show
1265     copy_buffer_config(&copy_buffer, frame_to_show);
1266   }
1267   // TODO(afergs): verify frame_to_show is correct after realloc
1268   //               encoder:
1269   //               decoder:
1270 
1271   assert(frame_to_show->y_crop_width == cm->superres_upscaled_width);
1272   assert(frame_to_show->y_crop_height == cm->superres_upscaled_height);
1273 
1274   // Scale up and back into frame_to_show.
1275   assert(frame_to_show->y_crop_width != cm->width);
1276   av1_upscale_normative_and_extend_frame(cm, &copy_buffer, frame_to_show);
1277 
1278   // Free the copy buffer
1279   aom_free_frame_buffer(&copy_buffer);
1280 }
1281