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