Coverage Report

Created: 2023-06-07 06:31

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