This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 460
/
genx_mc.cpp
285 lines (274 loc) · 9.49 KB
/
genx_mc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2012-2020 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../include/genx_me_common.h"
#include "../include/genx_blend_mc.h"
#include "../include/genx_sd_common.h"
extern "C" _GENX_MAIN_
void McP16_4MV_1SURF_WITH_CHR(
SurfaceIndex SURF_CONTROL,
SurfaceIndex SURF_REF1,
SurfaceIndex SURF_MV16x16_1,
SurfaceIndex SURF_SRC,
SurfaceIndex SURF_OUT,
uint start_xy,
uint scene_nums
)
{
vector<ushort, 2>
start_mbXY = start_xy;
vector<uint, 1>
scene_numbers = scene_nums;
uchar
scnFref = scene_numbers.format<uchar>()[0],
scnSrc = scene_numbers.format<uchar>()[1];
uint
mbX = get_thread_origin_x() + start_mbXY.format<ushort>()[0],
mbY = get_thread_origin_y() + start_mbXY.format<ushort>()[1],
x = mbX << 3,
y = mbY << 3;
vector<uchar, 96>
control;
read(SURF_CONTROL, 0, control);
ushort
width = control.format<ushort>()[30],
height = control.format<ushort>()[31],
th = control.format<short>()[32],
sTh = control.format<short>()[35];
matrix<short, 2, 4>
mv8_g4 = 0,
mv = 0;
matrix<uchar, 12, 12>
srcCh = 0,
preFil = 0;
matrix<uchar, 8, 8>
src = 0,
out = 0,
out2 = 0,
fil = 0;
matrix<uchar, 4, 8>
och = 0,
scm = 0;
vector<float, 2>
RsCsT = 0.0f;
short
nsc = scnFref == scnSrc;
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x - 2, y - 2, srcCh);
if (th > 0)
{
RsCsT = Genx_RsCs_aprox_8x8Block(srcCh.select<8, 1, 8, 1>(2, 2));
//Reference generation
mv8_g4 = MV_Neighborhood_read(SURF_MV16x16_1, width, height, mbX, mbY);
out = OMC_Ref_Generation(SURF_REF1, width, height, mbX, mbY, mv8_g4);
mv = (mv8_g4 * mv8_g4) / 16 * nsc;
int size = cm_sum<int>(mv);
int simFactor1 = 0;
if (nsc)
{
simFactor1 = mergeStrengthCalculator(out, srcCh, RsCsT, nsc, th, size);
srcCh.select<8, 1, 8, 1>(2, 2) = mergeBlocksRef(srcCh, out, simFactor1);
}
fil = srcCh.select<8, 1, 8, 1>(2, 2);
och = SpatialDenoiser_8x8_NV12_Chroma(SURF_SRC, srcCh, mbX, mbY, sTh);
}
else
{
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x, y, fil);
read_plane(SURF_SRC, GENX_SURFACE_UV_PLANE, x, y >> 1, och);
}
write_plane(SURF_OUT, GENX_SURFACE_Y_PLANE, x, y, fil);
write_plane(SURF_OUT, GENX_SURFACE_UV_PLANE, x, y >> 1, och);
}
#define TEST 1
extern "C" _GENX_MAIN_
void McP16_4MV_2SURF_WITH_CHR(
SurfaceIndex SURF_CONTROL,
SurfaceIndex SURF_REF1,
SurfaceIndex SURF_MV16x16_1,
SurfaceIndex SURF_REF2,
SurfaceIndex SURF_MV16x16_2,
SurfaceIndex SURF_SRC,
SurfaceIndex SURF_OUT,
uint start_xy,
uint scene_nums
)
{
vector<ushort, 2>
start_mbXY = start_xy;
vector<uint,1>
scene_numbers = scene_nums;
uchar
scnFref = scene_numbers.format<uchar>()[0],
scnBref = scene_numbers.format<uchar>()[2],
scnSrc = scene_numbers.format<uchar>()[1],
run = 0;
uint
mbX = get_thread_origin_x() + start_mbXY.format<ushort>()[0],
mbY = get_thread_origin_y() + start_mbXY.format<ushort>()[1],
x = mbX << 3,
y = mbY << 3;
vector<uchar, 96>
control;
read(SURF_CONTROL, 0, control);
ushort
width = control.format<ushort>()[30],
height = control.format<ushort>()[31],
th = control.format<short>()[32],
sTh = control.format<short>()[35];
matrix<short,2,4>
mv8_g4 = 0,
mv = 0;
#if TEST
matrix<uchar, 12, 16>
srcCh = 0,
preFil = 0;
#else
matrix<uchar, 12, 12>
srcCh = 0,
preFil = 0;
#endif
matrix<uchar, 8, 8>
src = 0,
out = 0,
out2 = 0,
out3 = 0,
out4 = 0,
fil = 0;
matrix<uchar, 4, 8>
och = 0;
matrix<uchar, 4, 8>
scm = 0;
vector<float, 2>
RsCsT = 0.0f,
RsCsT1 = 0.0f,
RsCsT2 = 0.0f;
short
dif1 = scnFref == scnSrc,
dif2 = scnSrc == scnBref,
dift = !dif1 + !dif2;
read_plane(SURF_SRC, GENX_SURFACE_UV_PLANE, x, y >> 1, och);
if (th > 0)
{
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x - 2, y - 2, srcCh);
RsCsT = Genx_RsCs_aprox_8x8Block(srcCh.select<8, 1, 8, 1>(2, 2));
//First reference
mv8_g4 = MV_Neighborhood_read(SURF_MV16x16_1, width, height, mbX, mbY);
out = OMC_Ref_Generation(SURF_REF1, width, height, mbX, mbY, mv8_g4);
mv = (mv8_g4 * mv8_g4) / 16 * dif1;
int size1 = cm_sum<int>(mv);
//Second reference
mv8_g4 = MV_Neighborhood_read(SURF_MV16x16_2, width, height, mbX, mbY);
out2 = OMC_Ref_Generation(SURF_REF2, width, height, mbX, mbY, mv8_g4);
mv = (mv8_g4 * mv8_g4) / 16 * dif2;
int size2 = cm_sum<int>(mv);
int size = ((size1 * dif1) + (size2 * dif2));
if (dif1 + dif2)
size /= (dif1 + dif2);
if (size >= DISTANCETH || dift)
{
int simFactor1 = mergeStrengthCalculator(out, srcCh, RsCsT, dif1, th, size1);
int simFactor2 = mergeStrengthCalculator(out2, srcCh, RsCsT, dif2, th, size2);
fil = mergeBlocks2Ref(srcCh, out, simFactor1, out2, simFactor2);
}
else
{
out = MedianIdx_8x8_3ref(out, srcCh.select<8, 1, 8, 1>(2, 2), out2);
int simFactor1 = mergeStrengthCalculator(out, srcCh, RsCsT, true, th, size);
fil = mergeBlocksRef(srcCh, out, simFactor1);
}
}
else
{
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x, y, fil);
}
write_plane(SURF_OUT, GENX_SURFACE_Y_PLANE, x, y, fil);
write_plane(SURF_OUT, GENX_SURFACE_UV_PLANE, x, y >> 1, och);
}
extern "C" _GENX_MAIN_
void MC_MERGE4(
SurfaceIndex SURF_REF1,
SurfaceIndex SURF_REF2,
uint start_xy
)
{
vector<ushort, 2>
start_mbXY = start_xy;
uint
mbX = get_thread_origin_x() + start_mbXY.format<ushort>()[0],
mbY = get_thread_origin_y() + start_mbXY.format<ushort>()[1],
x = mbX << 4,
y = mbY << 4;
matrix<uchar, 16, 16>
ref1 = 0,
ref2 = 0;
read_plane(SURF_REF1, GENX_SURFACE_Y_PLANE, x, y, ref1);
read_plane(SURF_REF2, GENX_SURFACE_Y_PLANE, x, y, ref2);
ref1 = (ref1 + ref2 + 1) >> 1;
write_plane(SURF_REF1, GENX_SURFACE_Y_PLANE, x, y, ref1);
}
#define VAR_SC_DATA_SIZE 8 //2 * size of float in bytes
extern "C" _GENX_MAIN_
void MC_VAR_SC_CALC(
SurfaceIndex SURF_SRC,
SurfaceIndex SURF_NOISE,
uint start_xy
)
{
vector<ushort, 2>
start_mbXY = start_xy;
int
mbX = get_thread_origin_x() + start_mbXY.format<ushort>()[0],
mbY = get_thread_origin_y() + start_mbXY.format<ushort>()[1],
x = mbX * 16,
y = mbY * 16;
matrix<uchar, 17, 32>
src = 0;
matrix<short, 16, 16>
tmp = 0;
matrix<float, 1, 2>
var_sc = 0.0f;
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x - 1, y - 1, src.select<8, 1, 32, 1>(0, 0));
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x - 1, y + 7, src.select<8, 1, 32, 1>(8, 0));
read_plane(SURF_SRC, GENX_SURFACE_Y_PLANE, x - 1, y + 15, src.select<1, 1, 32, 1>(16, 0));
matrix<ushort, 4, 4>
rs4x4,
cs4x4;
matrix<short, 16, 16>
tmpRs = src.select<16, 1, 16, 1>(0, 1) - src.select<16, 1, 16, 1>(1, 1),
tmpCs = src.select<16, 1, 16, 1>(1, 0) - src.select<16, 1, 16, 1>(1, 1);
#pragma unroll
for (uchar i = 0; i < 4; i++)
{
#pragma unroll
for (uchar j = 0; j < 4; j++)
{
rs4x4[i][j] = cm_shr<ushort>(cm_sum<uint>(cm_mul<ushort>(tmpRs.select<4, 1, 4, 1>(i << 2, j << 2), tmpRs.select<4, 1, 4, 1>(i << 2, j << 2))), 4, SAT);
cs4x4[i][j] = cm_shr<ushort>(cm_sum<uint>(cm_mul<ushort>(tmpCs.select<4, 1, 4, 1>(i << 2, j << 2), tmpCs.select<4, 1, 4, 1>(i << 2, j << 2))), 4, SAT);
}
}
float
average = cm_sum<float>(src.select<16, 1, 16, 1>(1, 1)) / 256.0f,
square = cm_sum<float>(cm_mul<ushort>(src.select<16, 1, 16, 1>(1, 1), src.select<16, 1, 16, 1>(1, 1))) / 256.0f;
var_sc(0, 0) = (square - average * average);//Variance value
float
RsFull = cm_sum<float>(rs4x4),
CsFull = cm_sum<float>(cs4x4);
var_sc(0, 1) = (RsFull + CsFull) / 16.0f; //RsCs value
write(SURF_NOISE, mbX * VAR_SC_DATA_SIZE, mbY, var_sc);
}