Coverage Report

Created: 2022-08-24 06:17

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