Coverage Report

Created: 2025-06-13 07:07

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