/src/x265/source/common/scalinglist.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Steve Borho <steve@borho.org> |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
19 | | * |
20 | | * This program is also available under a commercial proprietary license. |
21 | | * For more information, contact us at license @ x265.com. |
22 | | *****************************************************************************/ |
23 | | |
24 | | #include "common.h" |
25 | | #include "primitives.h" |
26 | | #include "scalinglist.h" |
27 | | |
28 | | namespace { |
29 | | // file-anonymous namespace |
30 | | |
31 | | /* Strings for scaling list file parsing */ |
32 | | |
33 | | static int quantTSDefault4x4[16] = |
34 | | { |
35 | | 16, 16, 16, 16, |
36 | | 16, 16, 16, 16, |
37 | | 16, 16, 16, 16, |
38 | | 16, 16, 16, 16 |
39 | | }; |
40 | | |
41 | | static int quantIntraDefault8x8[64] = |
42 | | { |
43 | | 16, 16, 16, 16, 17, 18, 21, 24, |
44 | | 16, 16, 16, 16, 17, 19, 22, 25, |
45 | | 16, 16, 17, 18, 20, 22, 25, 29, |
46 | | 16, 16, 18, 21, 24, 27, 31, 36, |
47 | | 17, 17, 20, 24, 30, 35, 41, 47, |
48 | | 18, 19, 22, 27, 35, 44, 54, 65, |
49 | | 21, 22, 25, 31, 41, 54, 70, 88, |
50 | | 24, 25, 29, 36, 47, 65, 88, 115 |
51 | | }; |
52 | | |
53 | | static int quantInterDefault8x8[64] = |
54 | | { |
55 | | 16, 16, 16, 16, 17, 18, 20, 24, |
56 | | 16, 16, 16, 17, 18, 20, 24, 25, |
57 | | 16, 16, 17, 18, 20, 24, 25, 28, |
58 | | 16, 17, 18, 20, 24, 25, 28, 33, |
59 | | 17, 18, 20, 24, 25, 28, 33, 41, |
60 | | 18, 20, 24, 25, 28, 33, 41, 54, |
61 | | 20, 24, 25, 28, 33, 41, 54, 71, |
62 | | 24, 25, 28, 33, 41, 54, 71, 91 |
63 | | }; |
64 | | |
65 | | } |
66 | | |
67 | | namespace X265_NS { |
68 | | // private namespace |
69 | | const char ScalingList::MatrixType[4][6][20] = |
70 | | { |
71 | | { |
72 | | "INTRA4X4_LUMA", |
73 | | "INTRA4X4_CHROMAU", |
74 | | "INTRA4X4_CHROMAV", |
75 | | "INTER4X4_LUMA", |
76 | | "INTER4X4_CHROMAU", |
77 | | "INTER4X4_CHROMAV" |
78 | | }, |
79 | | { |
80 | | "INTRA8X8_LUMA", |
81 | | "INTRA8X8_CHROMAU", |
82 | | "INTRA8X8_CHROMAV", |
83 | | "INTER8X8_LUMA", |
84 | | "INTER8X8_CHROMAU", |
85 | | "INTER8X8_CHROMAV" |
86 | | }, |
87 | | { |
88 | | "INTRA16X16_LUMA", |
89 | | "INTRA16X16_CHROMAU", |
90 | | "INTRA16X16_CHROMAV", |
91 | | "INTER16X16_LUMA", |
92 | | "INTER16X16_CHROMAU", |
93 | | "INTER16X16_CHROMAV" |
94 | | }, |
95 | | { |
96 | | "INTRA32X32_LUMA", |
97 | | "", |
98 | | "", |
99 | | "INTER32X32_LUMA", |
100 | | "", |
101 | | "", |
102 | | }, |
103 | | }; |
104 | | const char ScalingList::MatrixType_DC[4][12][22] = |
105 | | { |
106 | | { |
107 | | }, |
108 | | { |
109 | | }, |
110 | | { |
111 | | "INTRA16X16_LUMA_DC", |
112 | | "INTRA16X16_CHROMAU_DC", |
113 | | "INTRA16X16_CHROMAV_DC", |
114 | | "INTER16X16_LUMA_DC", |
115 | | "INTER16X16_CHROMAU_DC", |
116 | | "INTER16X16_CHROMAV_DC" |
117 | | }, |
118 | | { |
119 | | "INTRA32X32_LUMA_DC", |
120 | | "", |
121 | | "", |
122 | | "INTER32X32_LUMA_DC", |
123 | | "", |
124 | | "", |
125 | | }, |
126 | | }; |
127 | | |
128 | | const int ScalingList::s_numCoefPerSize[NUM_SIZES] = { 16, 64, 256, 1024 }; |
129 | | const int32_t ScalingList::s_quantScales[NUM_REM] = { 26214, 23302, 20560, 18396, 16384, 14564 }; |
130 | | const int32_t ScalingList::s_invQuantScales[NUM_REM] = { 40, 45, 51, 57, 64, 72 }; |
131 | | |
132 | | ScalingList::ScalingList() |
133 | 698 | { |
134 | 698 | memset(m_quantCoef, 0, sizeof(m_quantCoef)); |
135 | 698 | memset(m_dequantCoef, 0, sizeof(m_dequantCoef)); |
136 | 698 | memset(m_scalingListCoef, 0, sizeof(m_scalingListCoef)); |
137 | 698 | } |
138 | | |
139 | | bool ScalingList::init() |
140 | 698 | { |
141 | 698 | bool ok = true; |
142 | 3.49k | for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++) |
143 | 2.79k | { |
144 | 19.5k | for (int listId = 0; listId < NUM_LISTS; listId++) |
145 | 16.7k | { |
146 | 16.7k | m_scalingListCoef[sizeId][listId] = X265_MALLOC(int32_t, X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeId])); |
147 | 16.7k | ok &= !!m_scalingListCoef[sizeId][listId]; |
148 | 117k | for (int rem = 0; rem < NUM_REM; rem++) |
149 | 100k | { |
150 | 100k | m_quantCoef[sizeId][listId][rem] = X265_MALLOC(int32_t, s_numCoefPerSize[sizeId]); |
151 | 100k | m_dequantCoef[sizeId][listId][rem] = X265_MALLOC(int32_t, s_numCoefPerSize[sizeId]); |
152 | 100k | ok &= m_quantCoef[sizeId][listId][rem] && m_dequantCoef[sizeId][listId][rem]; |
153 | 100k | } |
154 | 16.7k | } |
155 | 2.79k | } |
156 | 698 | return ok; |
157 | 698 | } |
158 | | |
159 | | ScalingList::~ScalingList() |
160 | 698 | { |
161 | 3.49k | for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++) |
162 | 2.79k | { |
163 | 19.5k | for (int listId = 0; listId < NUM_LISTS; listId++) |
164 | 16.7k | { |
165 | 16.7k | X265_FREE(m_scalingListCoef[sizeId][listId]); |
166 | 117k | for (int rem = 0; rem < NUM_REM; rem++) |
167 | 100k | { |
168 | 100k | X265_FREE(m_quantCoef[sizeId][listId][rem]); |
169 | 100k | X265_FREE(m_dequantCoef[sizeId][listId][rem]); |
170 | 100k | } |
171 | 16.7k | } |
172 | 2.79k | } |
173 | 698 | } |
174 | | |
175 | | /* returns predicted list index if a match is found, else -1 */ |
176 | | int ScalingList::checkPredMode(int size, int list) const |
177 | 0 | { |
178 | 0 | for (int predList = list; predList >= 0; predList--) |
179 | 0 | { |
180 | | // check DC value |
181 | 0 | if (size < BLOCK_16x16 && m_scalingListDC[size][list] != m_scalingListDC[size][predList]) |
182 | 0 | continue; |
183 | | |
184 | | // check value of matrix |
185 | 0 | if (!memcmp(m_scalingListCoef[size][list], |
186 | 0 | list == predList ? getScalingListDefaultAddress(size, predList) : m_scalingListCoef[size][predList], |
187 | 0 | sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[size]))) |
188 | 0 | return predList; |
189 | 0 | } |
190 | | |
191 | 0 | return -1; |
192 | 0 | } |
193 | | |
194 | | /* check if use default quantization matrix |
195 | | * returns true if default quantization matrix is used in all sizes */ |
196 | | bool ScalingList::checkDefaultScalingList() const |
197 | 0 | { |
198 | 0 | int defaultCounter = 0; |
199 | |
|
200 | 0 | for (int s = 0; s < NUM_SIZES; s++) |
201 | 0 | for (int l = 0; l < NUM_LISTS; l++) |
202 | 0 | if (!memcmp(m_scalingListCoef[s][l], getScalingListDefaultAddress(s, l), |
203 | 0 | sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[s])) && |
204 | 0 | ((s < BLOCK_16x16) || (m_scalingListDC[s][l] == 16))) |
205 | 0 | defaultCounter++; |
206 | |
|
207 | 0 | return defaultCounter != (NUM_LISTS * NUM_SIZES - 4); // -4 for 32x32 |
208 | 0 | } |
209 | | |
210 | | /* get address of default quantization matrix */ |
211 | | const int32_t* ScalingList::getScalingListDefaultAddress(int sizeId, int listId) const |
212 | 0 | { |
213 | 0 | switch (sizeId) |
214 | 0 | { |
215 | 0 | case BLOCK_4x4: |
216 | 0 | return quantTSDefault4x4; |
217 | 0 | case BLOCK_8x8: |
218 | 0 | return (listId < 3) ? quantIntraDefault8x8 : quantInterDefault8x8; |
219 | 0 | case BLOCK_16x16: |
220 | 0 | return (listId < 3) ? quantIntraDefault8x8 : quantInterDefault8x8; |
221 | 0 | case BLOCK_32x32: |
222 | 0 | return (listId < 1) ? quantIntraDefault8x8 : quantInterDefault8x8; |
223 | 0 | default: |
224 | 0 | break; |
225 | 0 | } |
226 | | |
227 | 0 | X265_CHECK(0, "invalid scaling list size\n"); |
228 | 0 | return NULL; |
229 | 0 | } |
230 | | |
231 | | void ScalingList::processDefaultMarix(int sizeId, int listId) |
232 | 0 | { |
233 | 0 | memcpy(m_scalingListCoef[sizeId][listId], getScalingListDefaultAddress(sizeId, listId), sizeof(int) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeId])); |
234 | 0 | m_scalingListDC[sizeId][listId] = SCALING_LIST_DC; |
235 | 0 | } |
236 | | |
237 | | void ScalingList::setDefaultScalingList() |
238 | 0 | { |
239 | 0 | for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++) |
240 | 0 | for (int listId = 0; listId < NUM_LISTS; listId++) |
241 | 0 | processDefaultMarix(sizeId, listId); |
242 | 0 | m_bEnabled = true; |
243 | 0 | m_bDataPresent = false; |
244 | 0 | } |
245 | | |
246 | | bool ScalingList::parseScalingList(const char* filename) |
247 | 0 | { |
248 | 0 | FILE *fp = x265_fopen(filename, "r"); |
249 | 0 | if (!fp) |
250 | 0 | { |
251 | 0 | x265_log_file(NULL, X265_LOG_ERROR, "can't open scaling list file %s\n", filename); |
252 | 0 | return true; |
253 | 0 | } |
254 | | |
255 | 0 | char line[1024]; |
256 | 0 | int32_t *src = NULL; |
257 | 0 | fseek(fp, 0, 0); |
258 | |
|
259 | 0 | for (int sizeIdc = 0; sizeIdc < NUM_SIZES; sizeIdc++) |
260 | 0 | { |
261 | 0 | int size = X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeIdc]); |
262 | 0 | for (int listIdc = 0; listIdc < NUM_LISTS; listIdc += (sizeIdc == 3) ? 3 : 1) |
263 | 0 | { |
264 | 0 | src = m_scalingListCoef[sizeIdc][listIdc]; |
265 | |
|
266 | 0 | do |
267 | 0 | { |
268 | 0 | char *ret = fgets(line, 1024, fp); |
269 | 0 | if (!ret || (!strstr(line, MatrixType[sizeIdc][listIdc]) && feof(fp))) |
270 | 0 | { |
271 | 0 | x265_log_file(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename); |
272 | 0 | return true; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | while (!strstr(line, MatrixType[sizeIdc][listIdc])); |
276 | | |
277 | 0 | for (int i = 0; i < size; i++) |
278 | 0 | { |
279 | 0 | int data; |
280 | 0 | if (fscanf(fp, "%d,", &data) != 1) |
281 | 0 | { |
282 | 0 | x265_log_file(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename); |
283 | 0 | return true; |
284 | 0 | } |
285 | 0 | src[i] = data; |
286 | 0 | } |
287 | | |
288 | | // set DC value for default matrix check |
289 | 0 | m_scalingListDC[sizeIdc][listIdc] = src[0]; |
290 | |
|
291 | 0 | if (sizeIdc > BLOCK_8x8) |
292 | 0 | { |
293 | 0 | do |
294 | 0 | { |
295 | 0 | char *ret = fgets(line, 1024, fp); |
296 | 0 | if (!ret || (!strstr(line, MatrixType_DC[sizeIdc][listIdc]) && feof(fp))) |
297 | 0 | { |
298 | 0 | x265_log_file(NULL, X265_LOG_ERROR, "can't read DC from %s\n", filename); |
299 | 0 | return true; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | while (!strstr(line, MatrixType_DC[sizeIdc][listIdc])); |
303 | | |
304 | 0 | int data; |
305 | 0 | if (fscanf(fp, "%d,", &data) != 1) |
306 | 0 | { |
307 | 0 | x265_log_file(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename); |
308 | 0 | return true; |
309 | 0 | } |
310 | | |
311 | | // overwrite DC value when size of matrix is larger than 16x16 |
312 | 0 | m_scalingListDC[sizeIdc][listIdc] = data; |
313 | 0 | } |
314 | 0 | } |
315 | 0 | if (sizeIdc == 3) |
316 | 0 | { |
317 | 0 | for (int listIdc = 1; listIdc < NUM_LISTS; listIdc++) |
318 | 0 | { |
319 | 0 | if (listIdc % 3 != 0) |
320 | 0 | { |
321 | 0 | src = m_scalingListCoef[sizeIdc][listIdc]; |
322 | 0 | const int *srcNextSmallerSize = m_scalingListCoef[sizeIdc - 1][listIdc]; |
323 | 0 | for (int i = 0; i < size; i++) |
324 | 0 | { |
325 | 0 | src[i] = srcNextSmallerSize[i]; |
326 | 0 | } |
327 | 0 | m_scalingListDC[sizeIdc][listIdc] = m_scalingListDC[sizeIdc - 1][listIdc]; |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | 0 | fclose(fp); |
334 | |
|
335 | 0 | m_bEnabled = true; |
336 | 0 | m_bDataPresent = true; |
337 | |
|
338 | 0 | return false; |
339 | 0 | } |
340 | | |
341 | | /** set quantized matrix coefficient for encode */ |
342 | | void ScalingList::setupQuantMatrices(int internalCsp) |
343 | 698 | { |
344 | 3.49k | for (int size = 0; size < NUM_SIZES; size++) |
345 | 2.79k | { |
346 | 2.79k | int width = 1 << (size + 2); |
347 | 2.79k | int ratio = width / X265_MIN(MAX_MATRIX_SIZE_NUM, width); |
348 | 2.79k | int stride = X265_MIN(MAX_MATRIX_SIZE_NUM, width); |
349 | 2.79k | int count = s_numCoefPerSize[size]; |
350 | | |
351 | 19.5k | for (int list = 0; list < NUM_LISTS; list++) |
352 | 16.7k | { |
353 | 16.7k | int32_t *coeff = m_scalingListCoef[size][list]; |
354 | 16.7k | int32_t dc = m_scalingListDC[size][list]; |
355 | | |
356 | 117k | for (int rem = 0; rem < NUM_REM; rem++) |
357 | 100k | { |
358 | 100k | int32_t *quantCoeff = m_quantCoef[size][list][rem]; |
359 | 100k | int32_t *dequantCoeff = m_dequantCoef[size][list][rem]; |
360 | | |
361 | 100k | if (m_bEnabled) |
362 | 0 | { |
363 | 0 | if (internalCsp == X265_CSP_I444) |
364 | 0 | { |
365 | 0 | for (int i = 0; i < 64; i++) |
366 | 0 | { |
367 | 0 | m_scalingListCoef[BLOCK_32x32][1][i] = m_scalingListCoef[BLOCK_16x16][1][i]; |
368 | 0 | m_scalingListCoef[BLOCK_32x32][2][i] = m_scalingListCoef[BLOCK_16x16][2][i]; |
369 | 0 | m_scalingListCoef[BLOCK_32x32][4][i] = m_scalingListCoef[BLOCK_16x16][4][i]; |
370 | 0 | m_scalingListCoef[BLOCK_32x32][5][i] = m_scalingListCoef[BLOCK_16x16][5][i]; |
371 | 0 | } |
372 | |
|
373 | 0 | m_scalingListDC[BLOCK_32x32][1] = m_scalingListDC[BLOCK_16x16][1]; |
374 | 0 | m_scalingListDC[BLOCK_32x32][2] = m_scalingListDC[BLOCK_16x16][2]; |
375 | 0 | m_scalingListDC[BLOCK_32x32][4] = m_scalingListDC[BLOCK_16x16][4]; |
376 | 0 | m_scalingListDC[BLOCK_32x32][5] = m_scalingListDC[BLOCK_16x16][5]; |
377 | 0 | } |
378 | 0 | processScalingListEnc(coeff, quantCoeff, s_quantScales[rem] << 4, width, width, ratio, stride, dc); |
379 | 0 | processScalingListDec(coeff, dequantCoeff, s_invQuantScales[rem], width, width, ratio, stride, dc); |
380 | 0 | } |
381 | 100k | else |
382 | 100k | { |
383 | | /* flat quant and dequant coefficients */ |
384 | 34.2M | for (int i = 0; i < count; i++) |
385 | 34.1M | { |
386 | 34.1M | quantCoeff[i] = s_quantScales[rem]; |
387 | 34.1M | dequantCoeff[i] = s_invQuantScales[rem]; |
388 | 34.1M | } |
389 | 100k | } |
390 | 100k | } |
391 | 16.7k | } |
392 | 2.79k | } |
393 | 698 | } |
394 | | |
395 | | void ScalingList::processScalingListEnc(int32_t *coeff, int32_t *quantcoeff, int32_t quantScales, int height, int width, |
396 | | int ratio, int stride, int32_t dc) |
397 | 0 | { |
398 | 0 | for (int j = 0; j < height; j++) |
399 | 0 | for (int i = 0; i < width; i++) |
400 | 0 | quantcoeff[j * width + i] = quantScales / coeff[stride * (j / ratio) + i / ratio]; |
401 | |
|
402 | 0 | if (ratio > 1) |
403 | 0 | quantcoeff[0] = quantScales / dc; |
404 | 0 | } |
405 | | |
406 | | void ScalingList::processScalingListDec(int32_t *coeff, int32_t *dequantcoeff, int32_t invQuantScales, int height, int width, |
407 | | int ratio, int stride, int32_t dc) |
408 | 0 | { |
409 | 0 | for (int j = 0; j < height; j++) |
410 | 0 | for (int i = 0; i < width; i++) |
411 | 0 | dequantcoeff[j * width + i] = invQuantScales * coeff[stride * (j / ratio) + i / ratio]; |
412 | |
|
413 | 0 | if (ratio > 1) |
414 | 0 | dequantcoeff[0] = invQuantScales * dc; |
415 | 0 | } |
416 | | |
417 | | } |