Coverage Report

Created: 2024-06-18 06:48

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