1 /*****************************************************************************
2 * Copyright (C) 2014 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
25 #include "primitives.h"
26 #include "scalinglist.h"
29 // file-anonymous namespace
31 /* Strings for scaling list file parsing */
32 const char MatrixType
[4][6][20] =
63 const char MatrixType_DC
[4][12][22] =
71 "INTRA16X16_CHROMAU_DC",
72 "INTRA16X16_CHROMAV_DC",
74 "INTER16X16_CHROMAU_DC",
75 "INTER16X16_CHROMAV_DC"
83 int quantTSDefault4x4
[16] =
91 int quantIntraDefault8x8
[64] =
93 16, 16, 16, 16, 17, 18, 21, 24,
94 16, 16, 16, 16, 17, 19, 22, 25,
95 16, 16, 17, 18, 20, 22, 25, 29,
96 16, 16, 18, 21, 24, 27, 31, 36,
97 17, 17, 20, 24, 30, 35, 41, 47,
98 18, 19, 22, 27, 35, 44, 54, 65,
99 21, 22, 25, 31, 41, 54, 70, 88,
100 24, 25, 29, 36, 47, 65, 88, 115
103 int quantInterDefault8x8
[64] =
105 16, 16, 16, 16, 17, 18, 20, 24,
106 16, 16, 16, 17, 18, 20, 24, 25,
107 16, 16, 17, 18, 20, 24, 25, 28,
108 16, 17, 18, 20, 24, 25, 28, 33,
109 17, 18, 20, 24, 25, 28, 33, 41,
110 18, 20, 24, 25, 28, 33, 41, 54,
111 20, 24, 25, 28, 33, 41, 54, 71,
112 24, 25, 28, 33, 41, 54, 71, 91
120 const int ScalingList::s_numCoefPerSize
[NUM_SIZES
] = { 16, 64, 256, 1024 };
121 const int32_t ScalingList::s_quantScales
[NUM_REM
] = { 26214, 23302, 20560, 18396, 16384, 14564 };
122 const int32_t ScalingList::s_invQuantScales
[NUM_REM
] = { 40, 45, 51, 57, 64, 72 };
124 ScalingList::ScalingList()
126 memset(m_quantCoef
, 0, sizeof(m_quantCoef
));
127 memset(m_dequantCoef
, 0, sizeof(m_dequantCoef
));
128 memset(m_scalingListCoef
, 0, sizeof(m_scalingListCoef
));
131 bool ScalingList::init()
134 for (int sizeId
= 0; sizeId
< NUM_SIZES
; sizeId
++)
136 for (int listId
= 0; listId
< NUM_LISTS
; listId
++)
138 m_scalingListCoef
[sizeId
][listId
] = X265_MALLOC(int32_t, X265_MIN(MAX_MATRIX_COEF_NUM
, s_numCoefPerSize
[sizeId
]));
139 ok
&= !!m_scalingListCoef
[sizeId
][listId
];
140 for (int rem
= 0; rem
< NUM_REM
; rem
++)
142 m_quantCoef
[sizeId
][listId
][rem
] = X265_MALLOC(int32_t, s_numCoefPerSize
[sizeId
]);
143 m_dequantCoef
[sizeId
][listId
][rem
] = X265_MALLOC(int32_t, s_numCoefPerSize
[sizeId
]);
144 ok
&= m_quantCoef
[sizeId
][listId
][rem
] && m_dequantCoef
[sizeId
][listId
][rem
];
151 ScalingList::~ScalingList()
153 for (int sizeId
= 0; sizeId
< NUM_SIZES
; sizeId
++)
155 for (int listId
= 0; listId
< NUM_LISTS
; listId
++)
157 X265_FREE(m_scalingListCoef
[sizeId
][listId
]);
158 for (int rem
= 0; rem
< NUM_REM
; rem
++)
160 X265_FREE(m_quantCoef
[sizeId
][listId
][rem
]);
161 X265_FREE(m_dequantCoef
[sizeId
][listId
][rem
]);
167 /* returns predicted list index if a match is found, else -1 */
168 int ScalingList::checkPredMode(int size
, int list
) const
170 for (int predList
= list
; predList
>= 0; predList
--)
173 if (size
< BLOCK_16x16
&& m_scalingListDC
[size
][list
] != m_scalingListDC
[size
][predList
])
176 // check value of matrix
177 if (!memcmp(m_scalingListCoef
[size
][list
],
178 list
== predList
? getScalingListDefaultAddress(size
, predList
) : m_scalingListCoef
[size
][predList
],
179 sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM
, s_numCoefPerSize
[size
])))
186 /* check if use default quantization matrix
187 * returns true if default quantization matrix is used in all sizes */
188 bool ScalingList::checkDefaultScalingList() const
190 int defaultCounter
= 0;
192 for (int s
= 0; s
< NUM_SIZES
; s
++)
193 for (int l
= 0; l
< NUM_LISTS
; l
++)
194 if (!memcmp(m_scalingListCoef
[s
][l
], getScalingListDefaultAddress(s
, l
),
195 sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM
, s_numCoefPerSize
[s
])) &&
196 ((s
< BLOCK_16x16
) || (m_scalingListDC
[s
][l
] == 16)))
199 return defaultCounter
!= (NUM_LISTS
* NUM_SIZES
- 4); // -4 for 32x32
202 /* get address of default quantization matrix */
203 const int32_t* ScalingList::getScalingListDefaultAddress(int sizeId
, int listId
) const
208 return quantTSDefault4x4
;
210 return (listId
< 3) ? quantIntraDefault8x8
: quantInterDefault8x8
;
212 return (listId
< 3) ? quantIntraDefault8x8
: quantInterDefault8x8
;
214 return (listId
< 1) ? quantIntraDefault8x8
: quantInterDefault8x8
;
219 X265_CHECK(0, "invalid scaling list size\n");
223 void ScalingList::processDefaultMarix(int sizeId
, int listId
)
225 ::memcpy(m_scalingListCoef
[sizeId
][listId
], getScalingListDefaultAddress(sizeId
, listId
), sizeof(int) * X265_MIN(MAX_MATRIX_COEF_NUM
, s_numCoefPerSize
[sizeId
]));
226 m_scalingListDC
[sizeId
][listId
] = SCALING_LIST_DC
;
229 void ScalingList::setDefaultScalingList()
231 for (int sizeId
= 0; sizeId
< NUM_SIZES
; sizeId
++)
232 for (int listId
= 0; listId
< NUM_LISTS
; listId
++)
233 processDefaultMarix(sizeId
, listId
);
235 m_bDataPresent
= false;
238 bool ScalingList::parseScalingList(const char* filename
)
240 FILE *fp
= fopen(filename
, "r");
243 x265_log(NULL
, X265_LOG_ERROR
, "can't open scaling list file %s\n", filename
);
250 for (int sizeIdc
= 0; sizeIdc
< NUM_SIZES
; sizeIdc
++)
252 int size
= X265_MIN(MAX_MATRIX_COEF_NUM
, s_numCoefPerSize
[sizeIdc
]);
253 for (int listIdc
= 0; listIdc
< NUM_LISTS
; listIdc
++)
255 src
= m_scalingListCoef
[sizeIdc
][listIdc
];
260 char *ret
= fgets(line
, 1024, fp
);
261 if (!ret
|| (!strstr(line
, MatrixType
[sizeIdc
][listIdc
]) && feof(fp
)))
263 x265_log(NULL
, X265_LOG_ERROR
, "can't read matrix from %s\n", filename
);
267 while (!strstr(line
, MatrixType
[sizeIdc
][listIdc
]));
269 for (int i
= 0; i
< size
; i
++)
272 if (fscanf(fp
, "%d,", &data
) != 1)
274 x265_log(NULL
, X265_LOG_ERROR
, "can't read matrix from %s\n", filename
);
280 // set DC value for default matrix check
281 m_scalingListDC
[sizeIdc
][listIdc
] = src
[0];
283 if (sizeIdc
> BLOCK_8x8
)
288 char *ret
= fgets(line
, 1024, fp
);
289 if (!ret
|| (!strstr(line
, MatrixType_DC
[sizeIdc
][listIdc
]) && feof(fp
)))
291 x265_log(NULL
, X265_LOG_ERROR
, "can't read DC from %s\n", filename
);
295 while (!strstr(line
, MatrixType_DC
[sizeIdc
][listIdc
]));
298 if (fscanf(fp
, "%d,", &data
) != 1)
300 x265_log(NULL
, X265_LOG_ERROR
, "can't read matrix from %s\n", filename
);
304 // overwrite DC value when size of matrix is larger than 16x16
305 m_scalingListDC
[sizeIdc
][listIdc
] = data
;
313 m_bDataPresent
= !checkDefaultScalingList();
318 /** set quantized matrix coefficient for encode */
319 void ScalingList::setupQuantMatrices()
321 for (int size
= 0; size
< NUM_SIZES
; size
++)
323 int width
= 1 << (size
+ 2);
324 int ratio
= width
/ X265_MIN(MAX_MATRIX_SIZE_NUM
, width
);
325 int stride
= X265_MIN(MAX_MATRIX_SIZE_NUM
, width
);
326 int count
= s_numCoefPerSize
[size
];
328 for (int list
= 0; list
< NUM_LISTS
; list
++)
330 int32_t *coeff
= m_scalingListCoef
[size
][list
];
331 int32_t dc
= m_scalingListDC
[size
][list
];
333 for (int rem
= 0; rem
< NUM_REM
; rem
++)
335 int32_t *quantCoeff
= m_quantCoef
[size
][list
][rem
];
336 int32_t *dequantCoeff
= m_dequantCoef
[size
][list
][rem
];
340 processScalingListEnc(coeff
, quantCoeff
, s_quantScales
[rem
] << 4, width
, width
, ratio
, stride
, dc
);
341 processScalingListDec(coeff
, dequantCoeff
, s_invQuantScales
[rem
], width
, width
, ratio
, stride
, dc
);
345 /* flat quant and dequant coefficients */
346 for (int i
= 0; i
< count
; i
++)
348 quantCoeff
[i
] = s_quantScales
[rem
];
349 dequantCoeff
[i
] = s_invQuantScales
[rem
];
357 void ScalingList::processScalingListEnc(int32_t *coeff
, int32_t *quantcoeff
, int32_t quantScales
, int height
, int width
,
358 int ratio
, int stride
, int32_t dc
)
360 for (int j
= 0; j
< height
; j
++)
361 for (int i
= 0; i
< width
; i
++)
362 quantcoeff
[j
* width
+ i
] = quantScales
/ coeff
[stride
* (j
/ ratio
) + i
/ ratio
];
365 quantcoeff
[0] = quantScales
/ dc
;
368 void ScalingList::processScalingListDec(int32_t *coeff
, int32_t *dequantcoeff
, int32_t invQuantScales
, int height
, int width
,
369 int ratio
, int stride
, int32_t dc
)
371 for (int j
= 0; j
< height
; j
++)
372 for (int i
= 0; i
< width
; i
++)
373 dequantcoeff
[j
* width
+ i
] = invQuantScales
* coeff
[stride
* (j
/ ratio
) + i
/ ratio
];
376 dequantcoeff
[0] = invQuantScales
* dc
;