144 94, 138, 182, 154, 154,
152 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
155 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
160 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
161 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
162 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
165 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
166 122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
168 138, 153, 136, 167, 152, 152,
170 154, 154, 154, 154, 154, 154, 154, 154,
221 149, 107, 167, 154, 154,
229 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
232 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
237 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
238 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
239 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
242 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
243 136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
245 107, 167, 91, 122, 107, 167,
247 154, 154, 154, 154, 154, 154, 154, 154,
298 149, 92, 167, 154, 154,
306 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
309 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
314 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
315 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
316 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
319 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
320 136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
322 107, 167, 91, 107, 107, 167,
324 154, 154, 154, 154, 154, 154, 154, 154,
1077 int log2_trafo_size,
enum ScanType scan_idx,
1080#define GET_COORD(offset, n) \
1082 x_c = (x_cg << 2) + scan_x_off[n]; \
1083 y_c = (y_cg << 2) + scan_y_off[n]; \
1087 int transform_skip_flag = 0;
1089 int last_significant_coeff_x, last_significant_coeff_y;
1093 int greater1_ctx = 1;
1095 int num_last_subset;
1096 int max_xy, col_limit, clear_rows;
1097 int x_cg_last_sig, y_cg_last_sig;
1099 const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1101 ptrdiff_t
stride =
s->cur_frame->f->linesize[c_idx];
1102 int hshift =
sps->hshift[c_idx];
1103 int vshift =
sps->vshift[c_idx];
1104 uint8_t *
dst = &
s->cur_frame->f->data[c_idx][(y0 >> vshift) *
stride +
1105 ((x0 >> hshift) <<
sps->pixel_shift)];
1107 uint8_t significant_coeff_group_flag[8][8] = {{0}};
1108 int explicit_rdpcm_flag = 0;
1109 int explicit_rdpcm_dir_flag;
1111 int trafo_size = 1 << log2_trafo_size;
1114 static const uint8_t
level_scale[] = { 40, 45, 51, 57, 64, 72 };
1115 const uint8_t *scale_matrix =
NULL;
1122 static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1123 static const uint8_t
rem6[51 + 4 * 6 + 1] = {
1124 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1125 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1126 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1127 4, 5, 0, 1, 2, 3, 4, 5, 0, 1
1130 static const uint8_t
div6[51 + 4 * 6 + 1] = {
1131 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1132 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1133 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1134 10, 10, 11, 11, 11, 11, 11, 11, 12, 12
1138 if (
pps->transform_skip_enabled_flag &&
1139 log2_trafo_size <= pps->log2_max_transform_skip_block_size) {
1144 qp = qp_y +
sps->qp_bd_offset;
1156 if (
sps->chroma_format_idc == 1) {
1162 qp = qp_c[qp_i - 30];
1170 qp +=
sps->qp_bd_offset;
1173 shift =
sps->bit_depth + log2_trafo_size - 5;
1174 add = 1 << (
shift-1);
1179 if (
sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) {
1181 &
pps->scaling_list : &
sps->scaling_list;
1184 matrix_id = 3 * matrix_id + c_idx;
1186 scale_matrix = sl->
sl[log2_trafo_size - 2][matrix_id];
1187 if (log2_trafo_size >= 4)
1188 dc_scale = sl->
sl_dc[log2_trafo_size - 4][matrix_id];
1200 if (explicit_rdpcm_flag) {
1206 &last_significant_coeff_x, &last_significant_coeff_y);
1208 if (last_significant_coeff_x > 3) {
1210 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1211 (2 + (last_significant_coeff_x & 1)) +
1215 if (last_significant_coeff_y > 3) {
1217 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1218 (2 + (last_significant_coeff_y & 1)) +
1223 FFSWAP(
int, last_significant_coeff_x, last_significant_coeff_y);
1225 x_cg_last_sig = last_significant_coeff_x >> 2;
1226 y_cg_last_sig = last_significant_coeff_y >> 2;
1230 int last_x_c = last_significant_coeff_x & 3;
1231 int last_y_c = last_significant_coeff_y & 3;
1236 if (trafo_size == 4) {
1239 }
else if (trafo_size == 8) {
1243 }
else if (trafo_size == 16) {
1259 num_coeff =
horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1266 num_coeff =
horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1270 num_last_subset = (num_coeff - 1) >> 4;
1272 max_xy =
FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1273 col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1275 col_limit =
FFMIN(4, col_limit);
1276 else if (max_xy < 8)
1277 col_limit =
FFMIN(8, col_limit);
1278 else if (max_xy < 12)
1279 col_limit =
FFMIN(24, col_limit);
1282 clear_rows = trafo_size;
1289 memset(coeffs, 0, clear_rows * trafo_size *
sizeof(int16_t));
1291 for (
i = num_last_subset;
i >= 0;
i--) {
1293 int x_cg, y_cg, x_c, y_c,
pos;
1294 int implicit_non_zero_coeff = 0;
1300 uint8_t significant_coeff_flag_idx[16];
1301 uint8_t nb_significant_coeff_flag = 0;
1303 x_cg = scan_x_cg[
i];
1304 y_cg = scan_y_cg[
i];
1306 if ((
i < num_last_subset) && (
i > 0)) {
1308 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1309 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1310 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1311 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1313 significant_coeff_group_flag[x_cg][y_cg] =
1315 implicit_non_zero_coeff = 1;
1317 significant_coeff_group_flag[x_cg][y_cg] =
1318 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1319 (x_cg == 0 && y_cg == 0));
1322 last_scan_pos = num_coeff -
offset - 1;
1324 if (
i == num_last_subset) {
1325 n_end = last_scan_pos - 1;
1326 significant_coeff_flag_idx[0] = last_scan_pos;
1327 nb_significant_coeff_flag = 1;
1332 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1333 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1334 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1335 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1337 if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) {
1339 static const uint8_t ctx_idx_map[3][5 * 16] = {
1341 0, 2, 1, 6, 3, 4, 7, 6, 4, 5, 7, 8, 5, 8, 8, 8,
1342 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1343 2, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0,
1344 2, 2, 1, 2, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0,
1345 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1348 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8,
1349 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1350 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1351 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0,
1352 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1355 0, 2, 6, 7, 1, 3, 6, 7, 4, 4, 8, 8, 5, 5, 8, 8,
1356 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1357 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0,
1358 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1359 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1362 const uint8_t *ctx_idx_map_p;
1365 if (
sps->transform_skip_context_enabled &&
1367 ctx_idx_map_p = &ctx_idx_map[scan_idx][4 * 16];
1371 scf_offset = 14 + 27;
1376 if (log2_trafo_size == 2) {
1377 ctx_idx_map_p = &ctx_idx_map[scan_idx][0];
1379 ctx_idx_map_p = &ctx_idx_map[scan_idx][(prev_sig + 1) << 4];
1381 if ((x_cg > 0 || y_cg > 0))
1383 if (log2_trafo_size == 3) {
1384 scf_offset += (scan_idx ==
SCAN_DIAG) ? 9 : 15;
1389 if (log2_trafo_size == 3)
1397 nb0 = nb_significant_coeff_flag;
1398 for (n = n_end; n > 0; n--) {
1400 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1401 nb_significant_coeff_flag +=
sig;
1403 if (nb_significant_coeff_flag != nb0)
1404 implicit_non_zero_coeff = 0;
1405 if (implicit_non_zero_coeff == 0) {
1406 if (
sps->transform_skip_context_enabled &&
1411 scf_offset = 16 + 27;
1420 scf_offset = 2 + scf_offset;
1423 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1424 nb_significant_coeff_flag +=
1427 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1428 nb_significant_coeff_flag++;
1432 n_end = nb_significant_coeff_flag;
1436 int first_nz_pos_in_cg;
1437 int last_nz_pos_in_cg;
1438 int c_rice_param = 0;
1439 int first_greater1_coeff_idx = -1;
1440 uint8_t coeff_abs_level_greater1_flag[8];
1441 uint16_t coeff_sign_flag;
1449 int ctx_set = (
i > 0 && c_idx == 0) ? 2 : 0;
1451 if (
sps->persistent_rice_adaptation_enabled) {
1453 sb_type = 2 * (c_idx == 0 ? 1 : 0);
1455 sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1;
1459 if (!(
i == num_last_subset) && greater1_ctx == 0)
1462 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1465 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1466 int inc = (ctx_set << 2) + greater1_ctx;
1468 coeff_abs_level_greater1_flag[m] =
flag;
1469 gt1_mask |=
flag << m;
1470 greater1_ctx = (greater1_ctx + (greater1_ctx - 1U < 2)) & (
flag - 1);
1473 first_greater1_coeff_idx =
ff_ctz(gt1_mask);
1474 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1478 sps->implicit_rdpcm_enabled && transform_skip_flag &&
1479 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1480 explicit_rdpcm_flag)
1483 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1485 if (first_greater1_coeff_idx != -1) {
1488 if (!
pps->sign_data_hiding_flag || !sign_hidden ) {
1489 coeff_sign_flag =
coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1491 coeff_sign_flag =
coeff_sign_flag_decode(lc, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1494 for (m = 0; m < n_end; m++) {
1497 n = significant_coeff_flag_idx[m];
1500 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1501 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1504 trans_coeff_level += last_coeff_abs_level_remaining;
1505 if (trans_coeff_level > (3 << c_rice_param))
1506 c_rice_param =
sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 :
FFMIN(c_rice_param + 1, 4);
1507 if (
sps->persistent_rice_adaptation_enabled && !rice_init) {
1509 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1511 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1520 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1521 if (trans_coeff_level > (3 << c_rice_param))
1522 c_rice_param =
sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 :
FFMIN(c_rice_param + 1, 4);
1523 if (
sps->persistent_rice_adaptation_enabled && !rice_init) {
1525 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1527 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1533 if (
pps->sign_data_hiding_flag && sign_hidden) {
1534 sum_abs += trans_coeff_level;
1535 if (n == first_nz_pos_in_cg && (sum_abs&1))
1536 trans_coeff_level = -trans_coeff_level;
1539 sign = -(
int64_t)(coeff_sign_flag >> 15);
1540 trans_coeff_level = (trans_coeff_level ^ sign) - sign;
1541 coeff_sign_flag <<= 1;
1543 if (
sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) {
1544 if(y_c || x_c || log2_trafo_size < 4) {
1545 switch(log2_trafo_size) {
1546 case 3:
pos = (y_c << 3) + x_c;
break;
1547 case 4:
pos = ((y_c >> 1) << 3) + (x_c >> 1);
break;
1548 case 5:
pos = ((y_c >> 2) << 3) + (x_c >> 2);
break;
1549 default:
pos = (y_c << 2) + x_c;
break;
1551 scale_m = scale_matrix[
pos];
1557 trans_coeff_level =
FFMAX(
FFMIN(trans_coeff_level, 32767), -32768);
1559 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1565 if (explicit_rdpcm_flag || (
sps->implicit_rdpcm_enabled &&
1566 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1567 int mode =
sps->implicit_rdpcm_enabled ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag;
1569 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size,
mode);
1572 if (transform_skip_flag) {
1573 int rot =
sps->transform_skip_rotation_enabled &&
1574 log2_trafo_size == 2 &&
1577 for (
i = 0;
i < 8;
i++)
1578 FFSWAP(int16_t, coeffs[
i], coeffs[16 -
i - 1]);
1581 s->hevcdsp.dequant(coeffs, log2_trafo_size);
1583 if (explicit_rdpcm_flag || (
sps->implicit_rdpcm_enabled &&
1585 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1586 int mode = explicit_rdpcm_flag ? explicit_rdpcm_dir_flag : (pred_mode_intra == 26);
1588 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size,
mode);
1591 s->hevcdsp.transform_4x4_luma(coeffs);
1594 s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1596 s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1602 for (
i = 0;
i < (trafo_size * trafo_size);
i++) {
1606 s->hevcdsp.add_residual[log2_trafo_size-2](
dst, coeffs,
stride);