libavcodec/hevc/cabac.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * HEVC CABAC decoding | ||
| 3 | * | ||
| 4 | * Copyright (C) 2012 - 2013 Guillaume Martres | ||
| 5 | * Copyright (C) 2012 - 2013 Gildas Cocherel | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "libavutil/attributes.h" | ||
| 25 | #include "libavutil/common.h" | ||
| 26 | |||
| 27 | #include "libavcodec/cabac_functions.h" | ||
| 28 | #include "data.h" | ||
| 29 | #include "hevc.h" | ||
| 30 | #include "hevcdec.h" | ||
| 31 | |||
| 32 | #define CABAC_MAX_BIN 31 | ||
| 33 | |||
| 34 | // ELEM(NAME, NUM_BINS) | ||
| 35 | #define CABAC_ELEMS(ELEM) \ | ||
| 36 | ELEM(SAO_MERGE_FLAG, 1) \ | ||
| 37 | ELEM(SAO_TYPE_IDX, 1) \ | ||
| 38 | ELEM(SAO_EO_CLASS, 0) \ | ||
| 39 | ELEM(SAO_BAND_POSITION, 0) \ | ||
| 40 | ELEM(SAO_OFFSET_ABS, 0) \ | ||
| 41 | ELEM(SAO_OFFSET_SIGN, 0) \ | ||
| 42 | ELEM(END_OF_SLICE_FLAG, 0) \ | ||
| 43 | ELEM(SPLIT_CODING_UNIT_FLAG, 3) \ | ||
| 44 | ELEM(CU_TRANSQUANT_BYPASS_FLAG, 1) \ | ||
| 45 | ELEM(SKIP_FLAG, 3) \ | ||
| 46 | ELEM(CU_QP_DELTA, 3) \ | ||
| 47 | ELEM(PRED_MODE_FLAG, 1) \ | ||
| 48 | ELEM(PART_MODE, 4) \ | ||
| 49 | ELEM(PCM_FLAG, 0) \ | ||
| 50 | ELEM(PREV_INTRA_LUMA_PRED_FLAG, 1) \ | ||
| 51 | ELEM(MPM_IDX, 0) \ | ||
| 52 | ELEM(REM_INTRA_LUMA_PRED_MODE, 0) \ | ||
| 53 | ELEM(INTRA_CHROMA_PRED_MODE, 2) \ | ||
| 54 | ELEM(MERGE_FLAG, 1) \ | ||
| 55 | ELEM(MERGE_IDX, 1) \ | ||
| 56 | ELEM(INTER_PRED_IDC, 5) \ | ||
| 57 | ELEM(REF_IDX_L0, 2) \ | ||
| 58 | ELEM(REF_IDX_L1, 2) \ | ||
| 59 | ELEM(ABS_MVD_GREATER0_FLAG, 2) \ | ||
| 60 | ELEM(ABS_MVD_GREATER1_FLAG, 2) \ | ||
| 61 | ELEM(ABS_MVD_MINUS2, 0) \ | ||
| 62 | ELEM(MVD_SIGN_FLAG, 0) \ | ||
| 63 | ELEM(MVP_LX_FLAG, 1) \ | ||
| 64 | ELEM(NO_RESIDUAL_DATA_FLAG, 1) \ | ||
| 65 | ELEM(SPLIT_TRANSFORM_FLAG, 3) \ | ||
| 66 | ELEM(CBF_LUMA, 2) \ | ||
| 67 | ELEM(CBF_CB_CR, 5) \ | ||
| 68 | ELEM(TRANSFORM_SKIP_FLAG, 2) \ | ||
| 69 | ELEM(EXPLICIT_RDPCM_FLAG, 2) \ | ||
| 70 | ELEM(EXPLICIT_RDPCM_DIR_FLAG, 2) \ | ||
| 71 | ELEM(LAST_SIGNIFICANT_COEFF_X_PREFIX, 18) \ | ||
| 72 | ELEM(LAST_SIGNIFICANT_COEFF_Y_PREFIX, 18) \ | ||
| 73 | ELEM(LAST_SIGNIFICANT_COEFF_X_SUFFIX, 0) \ | ||
| 74 | ELEM(LAST_SIGNIFICANT_COEFF_Y_SUFFIX, 0) \ | ||
| 75 | ELEM(SIGNIFICANT_COEFF_GROUP_FLAG, 4) \ | ||
| 76 | ELEM(SIGNIFICANT_COEFF_FLAG, 44) \ | ||
| 77 | ELEM(COEFF_ABS_LEVEL_GREATER1_FLAG, 24) \ | ||
| 78 | ELEM(COEFF_ABS_LEVEL_GREATER2_FLAG, 6) \ | ||
| 79 | ELEM(COEFF_ABS_LEVEL_REMAINING, 0) \ | ||
| 80 | ELEM(COEFF_SIGN_FLAG, 0) \ | ||
| 81 | ELEM(LOG2_RES_SCALE_ABS, 8) \ | ||
| 82 | ELEM(RES_SCALE_SIGN_FLAG, 2) \ | ||
| 83 | ELEM(CU_CHROMA_QP_OFFSET_FLAG, 1) \ | ||
| 84 | ELEM(CU_CHROMA_QP_OFFSET_IDX, 1) \ | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Offset to ctxIdx 0 in init_values and states. | ||
| 88 | */ | ||
| 89 | enum { | ||
| 90 | #define OFFSET(NAME, NUM_BINS) \ | ||
| 91 | NAME ## _OFFSET, \ | ||
| 92 | NAME ## _END = NAME ## _OFFSET + NUM_BINS - 1, | ||
| 93 | CABAC_ELEMS(OFFSET) | ||
| 94 | }; | ||
| 95 | |||
| 96 | #define CNU 154 | ||
| 97 | /** | ||
| 98 | * Indexed by init_type | ||
| 99 | */ | ||
| 100 | static const uint8_t init_values[3][HEVC_CONTEXTS] = { | ||
| 101 | { // sao_merge_flag | ||
| 102 | 153, | ||
| 103 | // sao_type_idx | ||
| 104 | 200, | ||
| 105 | // split_coding_unit_flag | ||
| 106 | 139, 141, 157, | ||
| 107 | // cu_transquant_bypass_flag | ||
| 108 | 154, | ||
| 109 | // skip_flag | ||
| 110 | CNU, CNU, CNU, | ||
| 111 | // cu_qp_delta | ||
| 112 | 154, 154, 154, | ||
| 113 | // pred_mode | ||
| 114 | CNU, | ||
| 115 | // part_mode | ||
| 116 | 184, CNU, CNU, CNU, | ||
| 117 | // prev_intra_luma_pred_mode | ||
| 118 | 184, | ||
| 119 | // intra_chroma_pred_mode | ||
| 120 | 63, 139, | ||
| 121 | // merge_flag | ||
| 122 | CNU, | ||
| 123 | // merge_idx | ||
| 124 | CNU, | ||
| 125 | // inter_pred_idc | ||
| 126 | CNU, CNU, CNU, CNU, CNU, | ||
| 127 | // ref_idx_l0 | ||
| 128 | CNU, CNU, | ||
| 129 | // ref_idx_l1 | ||
| 130 | CNU, CNU, | ||
| 131 | // abs_mvd_greater1_flag | ||
| 132 | CNU, CNU, | ||
| 133 | // abs_mvd_greater1_flag | ||
| 134 | CNU, CNU, | ||
| 135 | // mvp_lx_flag | ||
| 136 | CNU, | ||
| 137 | // no_residual_data_flag | ||
| 138 | CNU, | ||
| 139 | // split_transform_flag | ||
| 140 | 153, 138, 138, | ||
| 141 | // cbf_luma | ||
| 142 | 111, 141, | ||
| 143 | // cbf_cb, cbf_cr | ||
| 144 | 94, 138, 182, 154, 154, | ||
| 145 | // transform_skip_flag | ||
| 146 | 139, 139, | ||
| 147 | // explicit_rdpcm_flag | ||
| 148 | 139, 139, | ||
| 149 | // explicit_rdpcm_dir_flag | ||
| 150 | 139, 139, | ||
| 151 | // last_significant_coeff_x_prefix | ||
| 152 | 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, | ||
| 153 | 79, 108, 123, 63, | ||
| 154 | // last_significant_coeff_y_prefix | ||
| 155 | 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, | ||
| 156 | 79, 108, 123, 63, | ||
| 157 | // significant_coeff_group_flag | ||
| 158 | 91, 171, 134, 141, | ||
| 159 | // significant_coeff_flag | ||
| 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, | ||
| 163 | 141, 111, | ||
| 164 | // coeff_abs_level_greater1_flag | ||
| 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, | ||
| 167 | // coeff_abs_level_greater2_flag | ||
| 168 | 138, 153, 136, 167, 152, 152, | ||
| 169 | // log2_res_scale_abs | ||
| 170 | 154, 154, 154, 154, 154, 154, 154, 154, | ||
| 171 | // res_scale_sign_flag | ||
| 172 | 154, 154, | ||
| 173 | // cu_chroma_qp_offset_flag | ||
| 174 | 154, | ||
| 175 | // cu_chroma_qp_offset_idx | ||
| 176 | 154, | ||
| 177 | }, | ||
| 178 | { // sao_merge_flag | ||
| 179 | 153, | ||
| 180 | // sao_type_idx | ||
| 181 | 185, | ||
| 182 | // split_coding_unit_flag | ||
| 183 | 107, 139, 126, | ||
| 184 | // cu_transquant_bypass_flag | ||
| 185 | 154, | ||
| 186 | // skip_flag | ||
| 187 | 197, 185, 201, | ||
| 188 | // cu_qp_delta | ||
| 189 | 154, 154, 154, | ||
| 190 | // pred_mode | ||
| 191 | 149, | ||
| 192 | // part_mode | ||
| 193 | 154, 139, 154, 154, | ||
| 194 | // prev_intra_luma_pred_mode | ||
| 195 | 154, | ||
| 196 | // intra_chroma_pred_mode | ||
| 197 | 152, 139, | ||
| 198 | // merge_flag | ||
| 199 | 110, | ||
| 200 | // merge_idx | ||
| 201 | 122, | ||
| 202 | // inter_pred_idc | ||
| 203 | 95, 79, 63, 31, 31, | ||
| 204 | // ref_idx_l0 | ||
| 205 | 153, 153, | ||
| 206 | // ref_idx_l1 | ||
| 207 | 153, 153, | ||
| 208 | // abs_mvd_greater1_flag | ||
| 209 | 140, 198, | ||
| 210 | // abs_mvd_greater1_flag | ||
| 211 | 140, 198, | ||
| 212 | // mvp_lx_flag | ||
| 213 | 168, | ||
| 214 | // no_residual_data_flag | ||
| 215 | 79, | ||
| 216 | // split_transform_flag | ||
| 217 | 124, 138, 94, | ||
| 218 | // cbf_luma | ||
| 219 | 153, 111, | ||
| 220 | // cbf_cb, cbf_cr | ||
| 221 | 149, 107, 167, 154, 154, | ||
| 222 | // transform_skip_flag | ||
| 223 | 139, 139, | ||
| 224 | // explicit_rdpcm_flag | ||
| 225 | 139, 139, | ||
| 226 | // explicit_rdpcm_dir_flag | ||
| 227 | 139, 139, | ||
| 228 | // last_significant_coeff_x_prefix | ||
| 229 | 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, | ||
| 230 | 94, 108, 123, 108, | ||
| 231 | // last_significant_coeff_y_prefix | ||
| 232 | 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, | ||
| 233 | 94, 108, 123, 108, | ||
| 234 | // significant_coeff_group_flag | ||
| 235 | 121, 140, 61, 154, | ||
| 236 | // significant_coeff_flag | ||
| 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, | ||
| 240 | 140, 140, | ||
| 241 | // coeff_abs_level_greater1_flag | ||
| 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, | ||
| 244 | // coeff_abs_level_greater2_flag | ||
| 245 | 107, 167, 91, 122, 107, 167, | ||
| 246 | // log2_res_scale_abs | ||
| 247 | 154, 154, 154, 154, 154, 154, 154, 154, | ||
| 248 | // res_scale_sign_flag | ||
| 249 | 154, 154, | ||
| 250 | // cu_chroma_qp_offset_flag | ||
| 251 | 154, | ||
| 252 | // cu_chroma_qp_offset_idx | ||
| 253 | 154, | ||
| 254 | }, | ||
| 255 | { // sao_merge_flag | ||
| 256 | 153, | ||
| 257 | // sao_type_idx | ||
| 258 | 160, | ||
| 259 | // split_coding_unit_flag | ||
| 260 | 107, 139, 126, | ||
| 261 | // cu_transquant_bypass_flag | ||
| 262 | 154, | ||
| 263 | // skip_flag | ||
| 264 | 197, 185, 201, | ||
| 265 | // cu_qp_delta | ||
| 266 | 154, 154, 154, | ||
| 267 | // pred_mode | ||
| 268 | 134, | ||
| 269 | // part_mode | ||
| 270 | 154, 139, 154, 154, | ||
| 271 | // prev_intra_luma_pred_mode | ||
| 272 | 183, | ||
| 273 | // intra_chroma_pred_mode | ||
| 274 | 152, 139, | ||
| 275 | // merge_flag | ||
| 276 | 154, | ||
| 277 | // merge_idx | ||
| 278 | 137, | ||
| 279 | // inter_pred_idc | ||
| 280 | 95, 79, 63, 31, 31, | ||
| 281 | // ref_idx_l0 | ||
| 282 | 153, 153, | ||
| 283 | // ref_idx_l1 | ||
| 284 | 153, 153, | ||
| 285 | // abs_mvd_greater1_flag | ||
| 286 | 169, 198, | ||
| 287 | // abs_mvd_greater1_flag | ||
| 288 | 169, 198, | ||
| 289 | // mvp_lx_flag | ||
| 290 | 168, | ||
| 291 | // no_residual_data_flag | ||
| 292 | 79, | ||
| 293 | // split_transform_flag | ||
| 294 | 224, 167, 122, | ||
| 295 | // cbf_luma | ||
| 296 | 153, 111, | ||
| 297 | // cbf_cb, cbf_cr | ||
| 298 | 149, 92, 167, 154, 154, | ||
| 299 | // transform_skip_flag | ||
| 300 | 139, 139, | ||
| 301 | // explicit_rdpcm_flag | ||
| 302 | 139, 139, | ||
| 303 | // explicit_rdpcm_dir_flag | ||
| 304 | 139, 139, | ||
| 305 | // last_significant_coeff_x_prefix | ||
| 306 | 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, | ||
| 307 | 79, 108, 123, 93, | ||
| 308 | // last_significant_coeff_y_prefix | ||
| 309 | 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, | ||
| 310 | 79, 108, 123, 93, | ||
| 311 | // significant_coeff_group_flag | ||
| 312 | 121, 140, 61, 154, | ||
| 313 | // significant_coeff_flag | ||
| 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, | ||
| 317 | 140, 140, | ||
| 318 | // coeff_abs_level_greater1_flag | ||
| 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, | ||
| 321 | // coeff_abs_level_greater2_flag | ||
| 322 | 107, 167, 91, 107, 107, 167, | ||
| 323 | // log2_res_scale_abs | ||
| 324 | 154, 154, 154, 154, 154, 154, 154, 154, | ||
| 325 | // res_scale_sign_flag | ||
| 326 | 154, 154, | ||
| 327 | // cu_chroma_qp_offset_flag | ||
| 328 | 154, | ||
| 329 | // cu_chroma_qp_offset_idx | ||
| 330 | 154, | ||
| 331 | }, | ||
| 332 | }; | ||
| 333 | |||
| 334 | static const uint8_t scan_1x1[1] = { | ||
| 335 | 0, | ||
| 336 | }; | ||
| 337 | |||
| 338 | static const uint8_t horiz_scan2x2_x[4] = { | ||
| 339 | 0, 1, 0, 1, | ||
| 340 | }; | ||
| 341 | |||
| 342 | static const uint8_t horiz_scan2x2_y[4] = { | ||
| 343 | 0, 0, 1, 1 | ||
| 344 | }; | ||
| 345 | |||
| 346 | static const uint8_t horiz_scan4x4_x[16] = { | ||
| 347 | 0, 1, 2, 3, | ||
| 348 | 0, 1, 2, 3, | ||
| 349 | 0, 1, 2, 3, | ||
| 350 | 0, 1, 2, 3, | ||
| 351 | }; | ||
| 352 | |||
| 353 | static const uint8_t horiz_scan4x4_y[16] = { | ||
| 354 | 0, 0, 0, 0, | ||
| 355 | 1, 1, 1, 1, | ||
| 356 | 2, 2, 2, 2, | ||
| 357 | 3, 3, 3, 3, | ||
| 358 | }; | ||
| 359 | |||
| 360 | static const uint8_t horiz_scan8x8_inv[8][8] = { | ||
| 361 | { 0, 1, 2, 3, 16, 17, 18, 19, }, | ||
| 362 | { 4, 5, 6, 7, 20, 21, 22, 23, }, | ||
| 363 | { 8, 9, 10, 11, 24, 25, 26, 27, }, | ||
| 364 | { 12, 13, 14, 15, 28, 29, 30, 31, }, | ||
| 365 | { 32, 33, 34, 35, 48, 49, 50, 51, }, | ||
| 366 | { 36, 37, 38, 39, 52, 53, 54, 55, }, | ||
| 367 | { 40, 41, 42, 43, 56, 57, 58, 59, }, | ||
| 368 | { 44, 45, 46, 47, 60, 61, 62, 63, }, | ||
| 369 | }; | ||
| 370 | |||
| 371 | static const uint8_t diag_scan2x2_x[4] = { | ||
| 372 | 0, 0, 1, 1, | ||
| 373 | }; | ||
| 374 | |||
| 375 | static const uint8_t diag_scan2x2_y[4] = { | ||
| 376 | 0, 1, 0, 1, | ||
| 377 | }; | ||
| 378 | |||
| 379 | static const uint8_t diag_scan2x2_inv[2][2] = { | ||
| 380 | { 0, 2, }, | ||
| 381 | { 1, 3, }, | ||
| 382 | }; | ||
| 383 | |||
| 384 | static const uint8_t diag_scan4x4_inv[4][4] = { | ||
| 385 | { 0, 2, 5, 9, }, | ||
| 386 | { 1, 4, 8, 12, }, | ||
| 387 | { 3, 7, 11, 14, }, | ||
| 388 | { 6, 10, 13, 15, }, | ||
| 389 | }; | ||
| 390 | |||
| 391 | static const uint8_t diag_scan8x8_inv[8][8] = { | ||
| 392 | { 0, 2, 5, 9, 14, 20, 27, 35, }, | ||
| 393 | { 1, 4, 8, 13, 19, 26, 34, 42, }, | ||
| 394 | { 3, 7, 12, 18, 25, 33, 41, 48, }, | ||
| 395 | { 6, 11, 17, 24, 32, 40, 47, 53, }, | ||
| 396 | { 10, 16, 23, 31, 39, 46, 52, 57, }, | ||
| 397 | { 15, 22, 30, 38, 45, 51, 56, 60, }, | ||
| 398 | { 21, 29, 37, 44, 50, 55, 59, 62, }, | ||
| 399 | { 28, 36, 43, 49, 54, 58, 61, 63, }, | ||
| 400 | }; | ||
| 401 | |||
| 402 | 2044225 | void ff_hevc_save_states(HEVCLocalContext *lc, const HEVCPPS *pps, | |
| 403 | int ctb_addr_ts) | ||
| 404 | { | ||
| 405 | 2044225 | const HEVCSPS *const sps = pps->sps; | |
| 406 | 2044225 | if (pps->entropy_coding_sync_enabled_flag && | |
| 407 | 168556 | (ctb_addr_ts % sps->ctb_width == 2 || | |
| 408 | 161382 | (sps->ctb_width == 2 && | |
| 409 | 768 | ctb_addr_ts % sps->ctb_width == 0))) { | |
| 410 | 7558 | memcpy(lc->common_cabac_state->state, lc->sub.cabac_state, HEVC_CONTEXTS); | |
| 411 | 7558 | if (sps->persistent_rice_adaptation_enabled) { | |
| 412 | 36 | memcpy(lc->common_cabac_state->stat_coeff, lc->sub.stat_coeff, HEVC_STAT_COEFFS); | |
| 413 | } | ||
| 414 | } | ||
| 415 | 2044225 | } | |
| 416 | |||
| 417 | 6283 | static void load_states(HEVCLocalContext *lc, const HEVCSPS *sps) | |
| 418 | { | ||
| 419 | 6283 | memcpy(lc->sub.cabac_state, lc->common_cabac_state->state, HEVC_CONTEXTS); | |
| 420 | 6283 | if (sps->persistent_rice_adaptation_enabled) { | |
| 421 | 33 | memcpy(lc->sub.stat_coeff, lc->common_cabac_state->stat_coeff, HEVC_STAT_COEFFS); | |
| 422 | } | ||
| 423 | 6283 | } | |
| 424 | |||
| 425 | 10886 | static int cabac_reinit(HEVCLocalContext *lc) | |
| 426 | { | ||
| 427 | 10886 | return skip_bytes(&lc->sub.cc, 0) == NULL ? AVERROR_INVALIDDATA : 0; | |
| 428 | } | ||
| 429 | |||
| 430 | 33514 | static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s) | |
| 431 | { | ||
| 432 | 33514 | int init_type = 2 - lc->sh->slice_type; | |
| 433 | int i; | ||
| 434 | |||
| 435 | 33514 | if (lc->sh->cabac_init_flag && lc->sh->slice_type != HEVC_SLICE_I) | |
| 436 | 6503 | init_type ^= 3; | |
| 437 | |||
| 438 | 6702800 | for (i = 0; i < HEVC_CONTEXTS; i++) { | |
| 439 | 6669286 | int init_value = init_values[init_type][i]; | |
| 440 | 6669286 | int m = (init_value >> 4) * 5 - 45; | |
| 441 | 6669286 | int n = ((init_value & 15) << 3) - 16; | |
| 442 | 6669286 | int pre = 2 * (((m * av_clip(lc->sh->slice_qp, 0, 51)) >> 4) + n) - 127; | |
| 443 | |||
| 444 | 6669286 | pre ^= pre >> 31; | |
| 445 | 6669286 | if (pre > 124) | |
| 446 | 681723 | pre = 124 + (pre & 1); | |
| 447 | 6669286 | lc->sub.cabac_state[i] = pre; | |
| 448 | } | ||
| 449 | |||
| 450 | 167570 | for (i = 0; i < 4; i++) | |
| 451 | 134056 | lc->sub.stat_coeff[i] = 0; | |
| 452 | 33514 | } | |
| 453 | |||
| 454 | /* Open CABAC on this substream's data, or continue byte-aligned in it if it | ||
| 455 | * is already open (one slice read from a single buffer). */ | ||
| 456 | 15974 | static int substream_continue(HEVCLocalContext *lc) | |
| 457 | { | ||
| 458 | 15974 | HEVCSubstream *const sub = &lc->sub; | |
| 459 | int ret; | ||
| 460 | |||
| 461 | 15974 | if (sub->opened) | |
| 462 | 10886 | return cabac_reinit(lc); | |
| 463 | |||
| 464 | 5088 | ret = ff_init_cabac_decoder(&sub->cc, sub->data, sub->size); | |
| 465 | 5088 | if (ret < 0) | |
| 466 | ✗ | return ret; | |
| 467 | 5088 | sub->opened = 1; | |
| 468 | |||
| 469 | 5088 | return 0; | |
| 470 | } | ||
| 471 | |||
| 472 | 2096786 | int ff_hevc_cabac_init(HEVCLocalContext *lc, const HEVCPPS *pps, | |
| 473 | int ctb_addr_ts) | ||
| 474 | { | ||
| 475 | 2096786 | const HEVCContext *const s = lc->parent; | |
| 476 | 2096786 | const HEVCSPS *const sps = pps->sps; | |
| 477 | 2096786 | HEVCSubstream *const sub = &lc->sub; | |
| 478 | |||
| 479 | 2096786 | if (ctb_addr_ts == pps->ctb_addr_rs_to_ts[lc->sh->slice_ctb_addr_rs]) { | |
| 480 | /* First CTB of a slice segment. For a dependent slice segment the | ||
| 481 | * data still belongs to the new segment. */ | ||
| 482 | 30081 | int ret = ff_init_cabac_decoder(&sub->cc, sub->data, sub->size); | |
| 483 | 30081 | if (ret < 0) | |
| 484 | ✗ | return ret; | |
| 485 | 30081 | sub->opened = 1; | |
| 486 | 30081 | if (lc->sh->dependent_slice_segment_flag == 0 || | |
| 487 | 7794 | (pps->tiles_enabled_flag && | |
| 488 | 844 | pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1])) | |
| 489 | 22585 | cabac_init_state(lc, s); | |
| 490 | |||
| 491 | 30081 | if (!lc->sh->first_slice_in_pic_flag && | |
| 492 | 19381 | pps->entropy_coding_sync_enabled_flag) { | |
| 493 | 3820 | if (ctb_addr_ts % sps->ctb_width == 0) { | |
| 494 | 1620 | if (sps->ctb_width == 1) | |
| 495 | 92 | cabac_init_state(lc, s); | |
| 496 | 1528 | else if (lc->sh->dependent_slice_segment_flag == 1) | |
| 497 | 1146 | load_states(lc, sps); | |
| 498 | } | ||
| 499 | } | ||
| 500 | } else { | ||
| 501 | 2066705 | if (pps->tiles_enabled_flag && | |
| 502 | 805264 | pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) { | |
| 503 | 10641 | int ret = substream_continue(lc); | |
| 504 | 10641 | if (ret < 0) | |
| 505 | ✗ | return ret; | |
| 506 | 10641 | cabac_init_state(lc, s); | |
| 507 | } | ||
| 508 | 2066705 | if (pps->entropy_coding_sync_enabled_flag) { | |
| 509 | 163747 | if (ctb_addr_ts % sps->ctb_width == 0) { | |
| 510 | int ret; | ||
| 511 | /* The end of row terminate bit is read only when continuing */ | ||
| 512 | 5333 | if (sub->opened) | |
| 513 | 5333 | get_cabac_terminate(&sub->cc); | |
| 514 | 5333 | ret = substream_continue(lc); | |
| 515 | 5333 | if (ret < 0) | |
| 516 | ✗ | return ret; | |
| 517 | |||
| 518 | 5333 | if (sps->ctb_width == 1) | |
| 519 | 196 | cabac_init_state(lc, s); | |
| 520 | else | ||
| 521 | 5137 | load_states(lc, sps); | |
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | 2096786 | return 0; | |
| 526 | } | ||
| 527 | |||
| 528 | #define GET_CABAC(ctx) get_cabac(&lc->sub.cc, &lc->sub.cabac_state[ctx]) | ||
| 529 | |||
| 530 | 1430254 | int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc) | |
| 531 | { | ||
| 532 | 1430254 | return GET_CABAC(SAO_MERGE_FLAG_OFFSET); | |
| 533 | } | ||
| 534 | |||
| 535 | 561241 | int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc) | |
| 536 | { | ||
| 537 | 561241 | if (!GET_CABAC(SAO_TYPE_IDX_OFFSET)) | |
| 538 | 390750 | return 0; | |
| 539 | |||
| 540 | 170491 | if (!get_cabac_bypass(&lc->sub.cc)) | |
| 541 | 50549 | return SAO_BAND; | |
| 542 | 119942 | return SAO_EDGE; | |
| 543 | } | ||
| 544 | |||
| 545 | 72038 | int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc) | |
| 546 | { | ||
| 547 | int i; | ||
| 548 | 72038 | int value = get_cabac_bypass(&lc->sub.cc); | |
| 549 | |||
| 550 | 360190 | for (i = 0; i < 4; i++) | |
| 551 | 288152 | value = (value << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 552 | 72038 | return value; | |
| 553 | } | ||
| 554 | |||
| 555 | 875768 | int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc, int bit_depth) | |
| 556 | { | ||
| 557 | 875768 | int i = 0; | |
| 558 | 875768 | int length = (1 << (FFMIN(bit_depth, 10) - 5)) - 1; | |
| 559 | |||
| 560 | 2528251 | while (i < length && get_cabac_bypass(&lc->sub.cc)) | |
| 561 | 1652483 | i++; | |
| 562 | 875768 | return i; | |
| 563 | } | ||
| 564 | |||
| 565 | 223924 | int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc) | |
| 566 | { | ||
| 567 | 223924 | return get_cabac_bypass(&lc->sub.cc); | |
| 568 | } | ||
| 569 | |||
| 570 | 119942 | int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc) | |
| 571 | { | ||
| 572 | 119942 | int ret = get_cabac_bypass(&lc->sub.cc) << 1; | |
| 573 | 119942 | ret |= get_cabac_bypass(&lc->sub.cc); | |
| 574 | 119942 | return ret; | |
| 575 | } | ||
| 576 | |||
| 577 | 2096785 | int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc) | |
| 578 | { | ||
| 579 | 2096785 | return get_cabac_terminate(&lc->sub.cc); | |
| 580 | } | ||
| 581 | |||
| 582 | 180080 | int ff_hevc_cu_transquant_bypass_flag_decode(HEVCLocalContext *lc) | |
| 583 | { | ||
| 584 | 180080 | return GET_CABAC(CU_TRANSQUANT_BYPASS_FLAG_OFFSET); | |
| 585 | } | ||
| 586 | |||
| 587 | 10722676 | int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, uint8_t *skip_flag, | |
| 588 | int x0, int y0, int x_cb, int y_cb, int min_cb_width) | ||
| 589 | { | ||
| 590 | 10722676 | int inc = 0; | |
| 591 | |||
| 592 | 10722676 | if (lc->ctb_left_flag || x0) | |
| 593 | 10430274 | inc = !!SAMPLE_CTB(skip_flag, x_cb - 1, y_cb); | |
| 594 | 10722676 | if (lc->ctb_up_flag || y0) | |
| 595 | 10154986 | inc += !!SAMPLE_CTB(skip_flag, x_cb, y_cb - 1); | |
| 596 | |||
| 597 | 10722676 | return GET_CABAC(SKIP_FLAG_OFFSET + inc); | |
| 598 | } | ||
| 599 | |||
| 600 | 920312 | int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc) | |
| 601 | { | ||
| 602 | 920312 | int prefix_val = 0; | |
| 603 | 920312 | int suffix_val = 0; | |
| 604 | 920312 | int inc = 0; | |
| 605 | |||
| 606 | 2204365 | while (prefix_val < 5 && GET_CABAC(CU_QP_DELTA_OFFSET + inc)) { | |
| 607 | 1284053 | prefix_val++; | |
| 608 | 1284053 | inc = 1; | |
| 609 | } | ||
| 610 | 920312 | if (prefix_val >= 5) { | |
| 611 | 141978 | int k = 0; | |
| 612 | 438128 | while (k < 7 && get_cabac_bypass(&lc->sub.cc)) { | |
| 613 | 296150 | suffix_val += 1 << k; | |
| 614 | 296150 | k++; | |
| 615 | } | ||
| 616 | 141978 | if (k == 7) { | |
| 617 | ✗ | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k); | |
| 618 | ✗ | return AVERROR_INVALIDDATA; | |
| 619 | } | ||
| 620 | |||
| 621 | 438128 | while (k--) | |
| 622 | 296150 | suffix_val += get_cabac_bypass(&lc->sub.cc) << k; | |
| 623 | } | ||
| 624 | 920312 | return prefix_val + suffix_val; | |
| 625 | } | ||
| 626 | |||
| 627 | 485098 | int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc) | |
| 628 | { | ||
| 629 | 485098 | return get_cabac_bypass(&lc->sub.cc); | |
| 630 | } | ||
| 631 | |||
| 632 | 228078 | int ff_hevc_cu_chroma_qp_offset_flag(HEVCLocalContext *lc) | |
| 633 | { | ||
| 634 | 228078 | return GET_CABAC(CU_CHROMA_QP_OFFSET_FLAG_OFFSET); | |
| 635 | } | ||
| 636 | |||
| 637 | ✗ | int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc, int chroma_qp_offset_list_len_minus1) | |
| 638 | { | ||
| 639 | ✗ | int c_max= FFMAX(5, chroma_qp_offset_list_len_minus1); | |
| 640 | ✗ | int i = 0; | |
| 641 | |||
| 642 | ✗ | while (i < c_max && GET_CABAC(CU_CHROMA_QP_OFFSET_IDX_OFFSET)) | |
| 643 | ✗ | i++; | |
| 644 | |||
| 645 | ✗ | return i; | |
| 646 | } | ||
| 647 | |||
| 648 | 5581896 | int ff_hevc_pred_mode_decode(HEVCLocalContext *lc) | |
| 649 | { | ||
| 650 | 5581896 | return GET_CABAC(PRED_MODE_FLAG_OFFSET); | |
| 651 | } | ||
| 652 | |||
| 653 | 9482901 | int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, uint8_t *tab_ct_depth, | |
| 654 | const HEVCSPS *sps, | ||
| 655 | int ct_depth, int x0, int y0) | ||
| 656 | { | ||
| 657 | 9482901 | int inc = 0, depth_left = 0, depth_top = 0; | |
| 658 | 9482901 | int x0b = av_zero_extend(x0, sps->log2_ctb_size); | |
| 659 | 9482901 | int y0b = av_zero_extend(y0, sps->log2_ctb_size); | |
| 660 | 9482901 | int x_cb = x0 >> sps->log2_min_cb_size; | |
| 661 | 9482901 | int y_cb = y0 >> sps->log2_min_cb_size; | |
| 662 | |||
| 663 | 9482901 | if (lc->ctb_left_flag || x0b) | |
| 664 | 9087226 | depth_left = tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1]; | |
| 665 | 9482901 | if (lc->ctb_up_flag || y0b) | |
| 666 | 8702791 | depth_top = tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb]; | |
| 667 | |||
| 668 | 9482901 | inc += (depth_left > ct_depth); | |
| 669 | 9482901 | inc += (depth_top > ct_depth); | |
| 670 | |||
| 671 | 9482901 | return GET_CABAC(SPLIT_CODING_UNIT_FLAG_OFFSET + inc); | |
| 672 | } | ||
| 673 | |||
| 674 | 7103755 | int ff_hevc_part_mode_decode(HEVCLocalContext *lc, const HEVCSPS *sps, int log2_cb_size) | |
| 675 | { | ||
| 676 | 7103755 | if (GET_CABAC(PART_MODE_OFFSET)) // 1 | |
| 677 | 4471903 | return PART_2Nx2N; | |
| 678 | 2631852 | if (log2_cb_size == sps->log2_min_cb_size) { | |
| 679 | 1845642 | if (lc->cu.pred_mode == MODE_INTRA) // 0 | |
| 680 | 1070260 | return PART_NxN; | |
| 681 | 775382 | if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01 | |
| 682 | 317827 | return PART_2NxN; | |
| 683 | 457555 | if (log2_cb_size == 3) // 00 | |
| 684 | 233413 | return PART_Nx2N; | |
| 685 | 224142 | if (GET_CABAC(PART_MODE_OFFSET + 2)) // 001 | |
| 686 | 115987 | return PART_Nx2N; | |
| 687 | 108155 | return PART_NxN; // 000 | |
| 688 | } | ||
| 689 | |||
| 690 | 786210 | if (!sps->amp_enabled) { | |
| 691 | 180593 | if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01 | |
| 692 | 88775 | return PART_2NxN; | |
| 693 | 91818 | return PART_Nx2N; | |
| 694 | } | ||
| 695 | |||
| 696 | 605617 | if (GET_CABAC(PART_MODE_OFFSET + 1)) { // 01X, 01XX | |
| 697 | 241563 | if (GET_CABAC(PART_MODE_OFFSET + 3)) // 011 | |
| 698 | 115990 | return PART_2NxN; | |
| 699 | 125573 | if (get_cabac_bypass(&lc->sub.cc)) // 0101 | |
| 700 | 58179 | return PART_2NxnD; | |
| 701 | 67394 | return PART_2NxnU; // 0100 | |
| 702 | } | ||
| 703 | |||
| 704 | 364054 | if (GET_CABAC(PART_MODE_OFFSET + 3)) // 001 | |
| 705 | 177818 | return PART_Nx2N; | |
| 706 | 186236 | if (get_cabac_bypass(&lc->sub.cc)) // 0001 | |
| 707 | 85890 | return PART_nRx2N; | |
| 708 | 100346 | return PART_nLx2N; // 0000 | |
| 709 | } | ||
| 710 | |||
| 711 | 126478 | int ff_hevc_pcm_flag_decode(HEVCLocalContext *lc) | |
| 712 | { | ||
| 713 | 126478 | return get_cabac_terminate(&lc->sub.cc); | |
| 714 | } | ||
| 715 | |||
| 716 | 6956540 | int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc) | |
| 717 | { | ||
| 718 | 6956540 | return GET_CABAC(PREV_INTRA_LUMA_PRED_FLAG_OFFSET); | |
| 719 | } | ||
| 720 | |||
| 721 | 4139582 | int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc) | |
| 722 | { | ||
| 723 | 4139582 | int i = 0; | |
| 724 | 7047693 | while (i < 2 && get_cabac_bypass(&lc->sub.cc)) | |
| 725 | 2908111 | i++; | |
| 726 | 4139582 | return i; | |
| 727 | } | ||
| 728 | |||
| 729 | 2816958 | int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc) | |
| 730 | { | ||
| 731 | int i; | ||
| 732 | 2816958 | int value = get_cabac_bypass(&lc->sub.cc); | |
| 733 | |||
| 734 | 14084790 | for (i = 0; i < 4; i++) | |
| 735 | 11267832 | value = (value << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 736 | 2816958 | return value; | |
| 737 | } | ||
| 738 | |||
| 739 | 3802033 | int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc) | |
| 740 | { | ||
| 741 | int ret; | ||
| 742 | 3802033 | if (!GET_CABAC(INTRA_CHROMA_PRED_MODE_OFFSET)) | |
| 743 | 2911444 | return 4; | |
| 744 | |||
| 745 | 890589 | ret = get_cabac_bypass(&lc->sub.cc) << 1; | |
| 746 | 890589 | ret |= get_cabac_bypass(&lc->sub.cc); | |
| 747 | 890589 | return ret; | |
| 748 | } | ||
| 749 | |||
| 750 | 8001708 | int ff_hevc_merge_idx_decode(HEVCLocalContext *lc) | |
| 751 | { | ||
| 752 | 8001708 | int i = GET_CABAC(MERGE_IDX_OFFSET); | |
| 753 | |||
| 754 | 8001708 | if (i != 0) { | |
| 755 | 5017219 | while (i < lc->sh->max_num_merge_cand-1 && get_cabac_bypass(&lc->sub.cc)) | |
| 756 | 1850810 | i++; | |
| 757 | } | ||
| 758 | 8001708 | return i; | |
| 759 | } | ||
| 760 | |||
| 761 | 6055589 | int ff_hevc_merge_flag_decode(HEVCLocalContext *lc) | |
| 762 | { | ||
| 763 | 6055589 | return GET_CABAC(MERGE_FLAG_OFFSET); | |
| 764 | } | ||
| 765 | |||
| 766 | 1923691 | int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH) | |
| 767 | { | ||
| 768 | 1923691 | if (nPbW + nPbH == 12) | |
| 769 | 248743 | return GET_CABAC(INTER_PRED_IDC_OFFSET + 4); | |
| 770 | 1674948 | if (GET_CABAC(INTER_PRED_IDC_OFFSET + lc->ct_depth)) | |
| 771 | 425087 | return PRED_BI; | |
| 772 | |||
| 773 | 1249861 | return GET_CABAC(INTER_PRED_IDC_OFFSET + 4); | |
| 774 | } | ||
| 775 | |||
| 776 | 3542895 | int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx) | |
| 777 | { | ||
| 778 | 3542895 | int i = 0; | |
| 779 | 3542895 | int max = num_ref_idx_lx - 1; | |
| 780 | 3542895 | int max_ctx = FFMIN(max, 2); | |
| 781 | |||
| 782 | 4643190 | while (i < max_ctx && GET_CABAC(REF_IDX_L0_OFFSET + i)) | |
| 783 | 1100295 | i++; | |
| 784 | 3542895 | if (i == 2) { | |
| 785 | 290210 | while (i < max && get_cabac_bypass(&lc->sub.cc)) | |
| 786 | 86597 | i++; | |
| 787 | } | ||
| 788 | |||
| 789 | 3542895 | return i; | |
| 790 | } | ||
| 791 | |||
| 792 | 3542895 | int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc) | |
| 793 | { | ||
| 794 | 3542895 | return GET_CABAC(MVP_LX_FLAG_OFFSET); | |
| 795 | } | ||
| 796 | |||
| 797 | 3069183 | int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc) | |
| 798 | { | ||
| 799 | 3069183 | return GET_CABAC(NO_RESIDUAL_DATA_FLAG_OFFSET); | |
| 800 | } | ||
| 801 | |||
| 802 | 6758580 | static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc) | |
| 803 | { | ||
| 804 | 6758580 | return GET_CABAC(ABS_MVD_GREATER0_FLAG_OFFSET); | |
| 805 | } | ||
| 806 | |||
| 807 | 4542630 | static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc) | |
| 808 | { | ||
| 809 | 4542630 | return GET_CABAC(ABS_MVD_GREATER1_FLAG_OFFSET + 1); | |
| 810 | } | ||
| 811 | |||
| 812 | 3112746 | static av_always_inline int mvd_decode(HEVCLocalContext *lc) | |
| 813 | { | ||
| 814 | 3112746 | int ret = 2; | |
| 815 | 3112746 | int k = 1; | |
| 816 | |||
| 817 | 7472048 | while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->sub.cc)) { | |
| 818 | 4359302 | ret += 1U << k; | |
| 819 | 4359302 | k++; | |
| 820 | } | ||
| 821 | 3112746 | if (k == CABAC_MAX_BIN) { | |
| 822 | ✗ | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k); | |
| 823 | ✗ | return 0; | |
| 824 | } | ||
| 825 | 10584794 | while (k--) | |
| 826 | 7472048 | ret += get_cabac_bypass(&lc->sub.cc) << k; | |
| 827 | 3112746 | return get_cabac_bypass_sign(&lc->sub.cc, -ret); | |
| 828 | } | ||
| 829 | |||
| 830 | 1429884 | static av_always_inline int mvd_sign_flag_decode(HEVCLocalContext *lc) | |
| 831 | { | ||
| 832 | 1429884 | return get_cabac_bypass_sign(&lc->sub.cc, -1); | |
| 833 | } | ||
| 834 | |||
| 835 | 7166603 | int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size) | |
| 836 | { | ||
| 837 | 7166603 | return GET_CABAC(SPLIT_TRANSFORM_FLAG_OFFSET + 5 - log2_trafo_size); | |
| 838 | } | ||
| 839 | |||
| 840 | 15960662 | int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth) | |
| 841 | { | ||
| 842 | 15960662 | return GET_CABAC(CBF_CB_CR_OFFSET + trafo_depth); | |
| 843 | } | ||
| 844 | |||
| 845 | 14966542 | int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth) | |
| 846 | { | ||
| 847 | 14966542 | return GET_CABAC(CBF_LUMA_OFFSET + !trafo_depth); | |
| 848 | } | ||
| 849 | |||
| 850 | 5701370 | static int hevc_transform_skip_flag_decode(HEVCLocalContext *lc, int c_idx) | |
| 851 | { | ||
| 852 | 5701370 | return GET_CABAC(TRANSFORM_SKIP_FLAG_OFFSET + !!c_idx); | |
| 853 | } | ||
| 854 | |||
| 855 | 35234 | static int explicit_rdpcm_flag_decode(HEVCLocalContext *lc, int c_idx) | |
| 856 | { | ||
| 857 | 35234 | return GET_CABAC(EXPLICIT_RDPCM_FLAG_OFFSET + !!c_idx); | |
| 858 | } | ||
| 859 | |||
| 860 | 3090 | static int explicit_rdpcm_dir_flag_decode(HEVCLocalContext *lc, int c_idx) | |
| 861 | { | ||
| 862 | 3090 | return GET_CABAC(EXPLICIT_RDPCM_DIR_FLAG_OFFSET + !!c_idx); | |
| 863 | } | ||
| 864 | |||
| 865 | 251428 | int ff_hevc_log2_res_scale_abs(HEVCLocalContext *lc, int idx) | |
| 866 | { | ||
| 867 | 251428 | int i =0; | |
| 868 | |||
| 869 | 752961 | while (i < 4 && GET_CABAC(LOG2_RES_SCALE_ABS_OFFSET + 4 * idx + i)) | |
| 870 | 501533 | i++; | |
| 871 | |||
| 872 | 251428 | return i; | |
| 873 | } | ||
| 874 | |||
| 875 | 142161 | int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx) | |
| 876 | { | ||
| 877 | 142161 | return GET_CABAC(RES_SCALE_SIGN_FLAG_OFFSET + idx); | |
| 878 | } | ||
| 879 | |||
| 880 | 14292833 | static av_always_inline void last_significant_coeff_xy_prefix_decode(HEVCLocalContext *lc, int c_idx, | |
| 881 | int log2_size, int *last_scx_prefix, int *last_scy_prefix) | ||
| 882 | { | ||
| 883 | 14292833 | int i = 0; | |
| 884 | 14292833 | int max = (log2_size << 1) - 1; | |
| 885 | int ctx_offset, ctx_shift; | ||
| 886 | |||
| 887 | 14292833 | if (!c_idx) { | |
| 888 | 10136428 | ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2); | |
| 889 | 10136428 | ctx_shift = (log2_size + 1) >> 2; | |
| 890 | } else { | ||
| 891 | 4156405 | ctx_offset = 15; | |
| 892 | 4156405 | ctx_shift = log2_size - 2; | |
| 893 | } | ||
| 894 | 71396834 | while (i < max && | |
| 895 | 34757311 | GET_CABAC(LAST_SIGNIFICANT_COEFF_X_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset)) | |
| 896 | 22346690 | i++; | |
| 897 | 14292833 | *last_scx_prefix = i; | |
| 898 | |||
| 899 | 14292833 | i = 0; | |
| 900 | 62644527 | while (i < max && | |
| 901 | 30558668 | GET_CABAC(LAST_SIGNIFICANT_COEFF_Y_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset)) | |
| 902 | 17793026 | i++; | |
| 903 | 14292833 | *last_scy_prefix = i; | |
| 904 | 14292833 | } | |
| 905 | |||
| 906 | 3264097 | static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc, | |
| 907 | int last_significant_coeff_prefix) | ||
| 908 | { | ||
| 909 | int i; | ||
| 910 | 3264097 | int length = (last_significant_coeff_prefix >> 1) - 1; | |
| 911 | 3264097 | int value = get_cabac_bypass(&lc->sub.cc); | |
| 912 | |||
| 913 | 4142386 | for (i = 1; i < length; i++) | |
| 914 | 878289 | value = (value << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 915 | 3264097 | return value; | |
| 916 | } | ||
| 917 | |||
| 918 | 8388333 | static av_always_inline int significant_coeff_group_flag_decode(HEVCLocalContext *lc, int c_idx, int ctx_cg) | |
| 919 | { | ||
| 920 | int inc; | ||
| 921 | |||
| 922 | 8388333 | inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0); | |
| 923 | |||
| 924 | 8388333 | return GET_CABAC(SIGNIFICANT_COEFF_GROUP_FLAG_OFFSET + inc); | |
| 925 | } | ||
| 926 | 170110216 | static av_always_inline int significant_coeff_flag_decode(HEVCLocalContext *lc, int n, | |
| 927 | int offset, const uint8_t *ctx_idx_map) | ||
| 928 | { | ||
| 929 | 170110216 | int inc = ctx_idx_map[n] + offset; | |
| 930 | 170110216 | return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + inc); | |
| 931 | } | ||
| 932 | |||
| 933 | 18389127 | static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset) | |
| 934 | { | ||
| 935 | 18389127 | return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + offset); | |
| 936 | } | ||
| 937 | |||
| 938 | 86532352 | static av_always_inline int coeff_abs_level_greater1_flag_decode(HEVCLocalContext *lc, int c_idx, int inc) | |
| 939 | { | ||
| 940 | |||
| 941 | 86532352 | if (c_idx > 0) | |
| 942 | 18371855 | inc += 16; | |
| 943 | |||
| 944 | 86532352 | return GET_CABAC(COEFF_ABS_LEVEL_GREATER1_FLAG_OFFSET + inc); | |
| 945 | } | ||
| 946 | |||
| 947 | 9392820 | static av_always_inline int coeff_abs_level_greater2_flag_decode(HEVCLocalContext *lc, int c_idx, int inc) | |
| 948 | { | ||
| 949 | 9392820 | if (c_idx > 0) | |
| 950 | 1798205 | inc += 4; | |
| 951 | |||
| 952 | 9392820 | return GET_CABAC(COEFF_ABS_LEVEL_GREATER2_FLAG_OFFSET + inc); | |
| 953 | } | ||
| 954 | |||
| 955 | /* a run of bypass bins is the quotient of the offset by range << 17 */ | ||
| 956 | 17297167 | static int cabac_bypass_bits(CABACContext *c, int n) | |
| 957 | { | ||
| 958 | 17297167 | int avail = CABAC_BITS - ff_ctz(c->low); | |
| 959 | 17297167 | uint64_t x, r = (uint64_t)c->range << (CABAC_BITS + 1); | |
| 960 | 17297167 | const uint8_t *bytestream = c->bytestream; | |
| 961 | |||
| 962 | 17297167 | if (n < avail) { | |
| 963 | 10800501 | x = (uint64_t)c->low << n; | |
| 964 | } else { | ||
| 965 | 6496666 | int fill = (bytestream[0] << 9) + (bytestream[1] << 1) - CABAC_MASK; | |
| 966 | |||
| 967 | 6496666 | x = (uint64_t)((((int64_t)c->low << avail) + fill)) << (n - avail); | |
| 968 | #if !UNCHECKED_BITSTREAM_READER | ||
| 969 | 6496666 | if (bytestream < c->bytestream_end) | |
| 970 | #endif | ||
| 971 | 6496466 | bytestream += CABAC_BITS / 8; | |
| 972 | } | ||
| 973 | |||
| 974 | 17297167 | if (x < r << n) { | |
| 975 | 17297167 | c->low = x % r; | |
| 976 | 17297167 | c->bytestream = bytestream; | |
| 977 | 17297167 | return x / r; | |
| 978 | } else { | ||
| 979 | ✗ | int ret = 0; | |
| 980 | |||
| 981 | ✗ | for (int i = 0; i < n; i++) | |
| 982 | ✗ | ret = (ret << 1) | get_cabac_bypass(c); | |
| 983 | ✗ | return ret; | |
| 984 | } | ||
| 985 | } | ||
| 986 | |||
| 987 | /* leading ones of the quotient, consumed only up to the first zero */ | ||
| 988 | 36908098 | static int cabac_unary_prefix(CABACContext *c, int max) | |
| 989 | { | ||
| 990 | 36908098 | int prefix = 0; | |
| 991 | |||
| 992 | 42229674 | while (prefix < max) { | |
| 993 | 42229674 | int avail = CABAC_BITS - ff_ctz(c->low); | |
| 994 | 42229674 | int n = FFMIN(avail - 1, max - prefix); | |
| 995 | 42229674 | uint64_t r = (uint64_t)c->range << (CABAC_BITS + 1); | |
| 996 | 42229674 | uint64_t x = (uint64_t)c->low << n; | |
| 997 | int q, t; | ||
| 998 | |||
| 999 | 42229674 | if (n < 1 || x >= r << n) { | |
| 1000 | 4968749 | if (!get_cabac_bypass(c)) | |
| 1001 | 2307340 | return prefix; | |
| 1002 | 2661409 | prefix++; | |
| 1003 | 2661409 | continue; | |
| 1004 | } | ||
| 1005 | |||
| 1006 | 37260925 | q = x / r; | |
| 1007 | 37260925 | t = ~q & ((1 << n) - 1); | |
| 1008 | 37260925 | if (t) { | |
| 1009 | 34600758 | int p = n - 1 - av_log2(t); | |
| 1010 | 34600758 | int k = n - (p + 1); | |
| 1011 | |||
| 1012 | 34600758 | c->low = (x >> k) - ((uint64_t)(q >> k) * r); | |
| 1013 | 34600758 | return prefix + p; | |
| 1014 | } | ||
| 1015 | 2660167 | c->low = x - (uint64_t)q * r; | |
| 1016 | 2660167 | prefix += n; | |
| 1017 | } | ||
| 1018 | ✗ | return prefix; | |
| 1019 | } | ||
| 1020 | |||
| 1021 | 36908098 | static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param) | |
| 1022 | { | ||
| 1023 | int prefix; | ||
| 1024 | 36908098 | int suffix = 0; | |
| 1025 | int last_coeff_abs_level_remaining; | ||
| 1026 | int i; | ||
| 1027 | |||
| 1028 | 36908098 | prefix = cabac_unary_prefix(&lc->sub.cc, CABAC_MAX_BIN); | |
| 1029 | |||
| 1030 | 36908098 | if (prefix < 3) { | |
| 1031 | 31865466 | if (rc_rice_param > 2) | |
| 1032 | 3411495 | suffix = cabac_bypass_bits(&lc->sub.cc, rc_rice_param); | |
| 1033 | else | ||
| 1034 | 47950166 | for (i = 0; i < rc_rice_param; i++) | |
| 1035 | 19496195 | suffix = (suffix << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 1036 | 31865466 | last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix; | |
| 1037 | } else { | ||
| 1038 | 5042632 | int prefix_minus3 = prefix - 3; | |
| 1039 | int k; | ||
| 1040 | |||
| 1041 | 5042632 | if (prefix == CABAC_MAX_BIN || prefix_minus3 + rc_rice_param > 16 + 6) { | |
| 1042 | ✗ | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix); | |
| 1043 | ✗ | return 0; | |
| 1044 | } | ||
| 1045 | |||
| 1046 | 5042632 | k = prefix_minus3 + rc_rice_param; | |
| 1047 | 5042632 | if (k > 16) { | |
| 1048 | ✗ | suffix = cabac_bypass_bits(&lc->sub.cc, 16) << (k - 16); | |
| 1049 | ✗ | suffix |= cabac_bypass_bits(&lc->sub.cc, k - 16); | |
| 1050 | 5042632 | } else if (k > 2) { | |
| 1051 | 2177892 | suffix = cabac_bypass_bits(&lc->sub.cc, k); | |
| 1052 | } else { | ||
| 1053 | 5670159 | for (i = 0; i < k; i++) | |
| 1054 | 2805419 | suffix = (suffix << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 1055 | } | ||
| 1056 | 5042632 | last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1) | |
| 1057 | 5042632 | << rc_rice_param) + suffix; | |
| 1058 | } | ||
| 1059 | 36908098 | return last_coeff_abs_level_remaining; | |
| 1060 | } | ||
| 1061 | |||
| 1062 | 21796811 | static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb) | |
| 1063 | { | ||
| 1064 | int i; | ||
| 1065 | 21796811 | int ret = 0; | |
| 1066 | |||
| 1067 | 21796811 | if (nb > 2) | |
| 1068 | 11707780 | return cabac_bypass_bits(&lc->sub.cc, nb); | |
| 1069 | |||
| 1070 | 24283301 | for (i = 0; i < nb; i++) | |
| 1071 | 14194270 | ret = (ret << 1) | get_cabac_bypass(&lc->sub.cc); | |
| 1072 | 10089031 | return ret; | |
| 1073 | } | ||
| 1074 | |||
| 1075 | 14292833 | void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, const HEVCPPS *pps, | |
| 1076 | int x0, int y0, | ||
| 1077 | int log2_trafo_size, enum ScanType scan_idx, | ||
| 1078 | int c_idx) | ||
| 1079 | { | ||
| 1080 | #define GET_COORD(offset, n) \ | ||
| 1081 | do { \ | ||
| 1082 | x_c = (x_cg << 2) + scan_x_off[n]; \ | ||
| 1083 | y_c = (y_cg << 2) + scan_y_off[n]; \ | ||
| 1084 | } while (0) | ||
| 1085 | 14292833 | const HEVCContext *const s = lc->parent; | |
| 1086 | 14292833 | const HEVCSPS *const sps = pps->sps; | |
| 1087 | 14292833 | int transform_skip_flag = 0; | |
| 1088 | |||
| 1089 | int last_significant_coeff_x, last_significant_coeff_y; | ||
| 1090 | int last_scan_pos; | ||
| 1091 | int n_end; | ||
| 1092 | 14292833 | int num_coeff = 0; | |
| 1093 | 14292833 | int greater1_ctx = 1; | |
| 1094 | |||
| 1095 | int num_last_subset; | ||
| 1096 | int max_xy, col_limit, clear_rows; | ||
| 1097 | int x_cg_last_sig, y_cg_last_sig; | ||
| 1098 | |||
| 1099 | const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off; | ||
| 1100 | |||
| 1101 | 14292833 | ptrdiff_t stride = s->cur_frame->f->linesize[c_idx]; | |
| 1102 | 14292833 | int hshift = sps->hshift[c_idx]; | |
| 1103 | 14292833 | int vshift = sps->vshift[c_idx]; | |
| 1104 | 14292833 | uint8_t *dst = &s->cur_frame->f->data[c_idx][(y0 >> vshift) * stride + | |
| 1105 | 14292833 | ((x0 >> hshift) << sps->pixel_shift)]; | |
| 1106 | 14292833 | int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer); | |
| 1107 | 14292833 | uint8_t significant_coeff_group_flag[8][8] = {{0}}; | |
| 1108 | 14292833 | int explicit_rdpcm_flag = 0; | |
| 1109 | int explicit_rdpcm_dir_flag; | ||
| 1110 | |||
| 1111 | 14292833 | int trafo_size = 1 << log2_trafo_size; | |
| 1112 | int i; | ||
| 1113 | int qp,shift,add,scale,scale_m; | ||
| 1114 | static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 }; | ||
| 1115 | 14292833 | const uint8_t *scale_matrix = NULL; | |
| 1116 | uint8_t dc_scale; | ||
| 1117 | 14292833 | int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode : | |
| 1118 | lc->tu.intra_pred_mode_c; | ||
| 1119 | |||
| 1120 | // Derive QP for dequant | ||
| 1121 | 14292833 | if (!lc->cu.cu_transquant_bypass_flag) { | |
| 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 | ||
| 1128 | }; | ||
| 1129 | |||
| 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 | ||
| 1135 | }; | ||
| 1136 | 13868635 | int qp_y = lc->sub.qp_y; | |
| 1137 | |||
| 1138 | 13868635 | if (pps->transform_skip_enabled_flag && | |
| 1139 | 10307969 | log2_trafo_size <= pps->log2_max_transform_skip_block_size) { | |
| 1140 | 5701370 | transform_skip_flag = hevc_transform_skip_flag_decode(lc, c_idx); | |
| 1141 | } | ||
| 1142 | |||
| 1143 | 13868635 | if (c_idx == 0) { | |
| 1144 | 9875633 | qp = qp_y + sps->qp_bd_offset; | |
| 1145 | } else { | ||
| 1146 | int qp_i, offset; | ||
| 1147 | |||
| 1148 | 3993002 | if (c_idx == 1) | |
| 1149 | 1919632 | offset = pps->cb_qp_offset + lc->sh->slice_cb_qp_offset + | |
| 1150 | 1919632 | lc->sub.cu_qp_offset_cb; | |
| 1151 | else | ||
| 1152 | 2073370 | offset = pps->cr_qp_offset + lc->sh->slice_cr_qp_offset + | |
| 1153 | 2073370 | lc->sub.cu_qp_offset_cr; | |
| 1154 | |||
| 1155 | 3993002 | qp_i = av_clip(qp_y + offset, - sps->qp_bd_offset, 57); | |
| 1156 | 3993002 | if (sps->chroma_format_idc == 1) { | |
| 1157 | 2596690 | if (qp_i < 30) | |
| 1158 | 953576 | qp = qp_i; | |
| 1159 | 1643114 | else if (qp_i > 43) | |
| 1160 | 11213 | qp = qp_i - 6; | |
| 1161 | else | ||
| 1162 | 1631901 | qp = qp_c[qp_i - 30]; | |
| 1163 | } else { | ||
| 1164 | 1396312 | if (qp_i > 51) | |
| 1165 | ✗ | qp = 51; | |
| 1166 | else | ||
| 1167 | 1396312 | qp = qp_i; | |
| 1168 | } | ||
| 1169 | |||
| 1170 | 3993002 | qp += sps->qp_bd_offset; | |
| 1171 | } | ||
| 1172 | |||
| 1173 | 13868635 | shift = sps->bit_depth + log2_trafo_size - 5; | |
| 1174 | 13868635 | add = 1 << (shift-1); | |
| 1175 | 13868635 | scale = level_scale[rem6[qp]] << (div6[qp]); | |
| 1176 | 13868635 | scale_m = 16; // default when no custom scaling lists. | |
| 1177 | 13868635 | dc_scale = 16; | |
| 1178 | |||
| 1179 | 13868635 | if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) { | |
| 1180 | 605014 | const ScalingList *sl = pps->scaling_list_data_present_flag ? | |
| 1181 | 605014 | &pps->scaling_list : &sps->scaling_list; | |
| 1182 | 605014 | int matrix_id = lc->cu.pred_mode != MODE_INTRA; | |
| 1183 | |||
| 1184 | 605014 | matrix_id = 3 * matrix_id + c_idx; | |
| 1185 | |||
| 1186 | 605014 | scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id]; | |
| 1187 | 605014 | if (log2_trafo_size >= 4) | |
| 1188 | 112450 | dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id]; | |
| 1189 | } | ||
| 1190 | } else { | ||
| 1191 | 424198 | shift = 0; | |
| 1192 | 424198 | add = 0; | |
| 1193 | 424198 | scale = 0; | |
| 1194 | 424198 | dc_scale = 0; | |
| 1195 | } | ||
| 1196 | |||
| 1197 | 14292833 | if (lc->cu.pred_mode == MODE_INTER && sps->explicit_rdpcm_enabled && | |
| 1198 | 190277 | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { | |
| 1199 | 35234 | explicit_rdpcm_flag = explicit_rdpcm_flag_decode(lc, c_idx); | |
| 1200 | 35234 | if (explicit_rdpcm_flag) { | |
| 1201 | 3090 | explicit_rdpcm_dir_flag = explicit_rdpcm_dir_flag_decode(lc, c_idx); | |
| 1202 | } | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | 14292833 | last_significant_coeff_xy_prefix_decode(lc, c_idx, log2_trafo_size, | |
| 1206 | &last_significant_coeff_x, &last_significant_coeff_y); | ||
| 1207 | |||
| 1208 | 14292833 | if (last_significant_coeff_x > 3) { | |
| 1209 | 1874770 | int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x); | |
| 1210 | 1874770 | last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) * | |
| 1211 | 1874770 | (2 + (last_significant_coeff_x & 1)) + | |
| 1212 | suffix; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | 14292833 | if (last_significant_coeff_y > 3) { | |
| 1216 | 1389327 | int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y); | |
| 1217 | 1389327 | last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) * | |
| 1218 | 1389327 | (2 + (last_significant_coeff_y & 1)) + | |
| 1219 | suffix; | ||
| 1220 | } | ||
| 1221 | |||
| 1222 | 14292833 | if (scan_idx == SCAN_VERT) | |
| 1223 | 1518700 | FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y); | |
| 1224 | |||
| 1225 | 14292833 | x_cg_last_sig = last_significant_coeff_x >> 2; | |
| 1226 | 14292833 | y_cg_last_sig = last_significant_coeff_y >> 2; | |
| 1227 | |||
| 1228 | 14292833 | switch (scan_idx) { | |
| 1229 | 10952396 | case SCAN_DIAG: { | |
| 1230 | 10952396 | int last_x_c = last_significant_coeff_x & 3; | |
| 1231 | 10952396 | int last_y_c = last_significant_coeff_y & 3; | |
| 1232 | |||
| 1233 | 10952396 | scan_x_off = ff_hevc_diag_scan4x4_x; | |
| 1234 | 10952396 | scan_y_off = ff_hevc_diag_scan4x4_y; | |
| 1235 | 10952396 | num_coeff = diag_scan4x4_inv[last_y_c][last_x_c]; | |
| 1236 | 10952396 | if (trafo_size == 4) { | |
| 1237 | 4913250 | scan_x_cg = scan_1x1; | |
| 1238 | 4913250 | scan_y_cg = scan_1x1; | |
| 1239 | 6039146 | } else if (trafo_size == 8) { | |
| 1240 | 3115410 | num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4; | |
| 1241 | 3115410 | scan_x_cg = diag_scan2x2_x; | |
| 1242 | 3115410 | scan_y_cg = diag_scan2x2_y; | |
| 1243 | 2923736 | } else if (trafo_size == 16) { | |
| 1244 | 2119172 | num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4; | |
| 1245 | 2119172 | scan_x_cg = ff_hevc_diag_scan4x4_x; | |
| 1246 | 2119172 | scan_y_cg = ff_hevc_diag_scan4x4_y; | |
| 1247 | } else { // trafo_size == 32 | ||
| 1248 | 804564 | num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4; | |
| 1249 | 804564 | scan_x_cg = ff_hevc_diag_scan8x8_x; | |
| 1250 | 804564 | scan_y_cg = ff_hevc_diag_scan8x8_y; | |
| 1251 | } | ||
| 1252 | 10952396 | break; | |
| 1253 | } | ||
| 1254 | 1821737 | case SCAN_HORIZ: | |
| 1255 | 1821737 | scan_x_cg = horiz_scan2x2_x; | |
| 1256 | 1821737 | scan_y_cg = horiz_scan2x2_y; | |
| 1257 | 1821737 | scan_x_off = horiz_scan4x4_x; | |
| 1258 | 1821737 | scan_y_off = horiz_scan4x4_y; | |
| 1259 | 1821737 | num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x]; | |
| 1260 | 1821737 | break; | |
| 1261 | 1518700 | default: //SCAN_VERT | |
| 1262 | 1518700 | scan_x_cg = horiz_scan2x2_y; | |
| 1263 | 1518700 | scan_y_cg = horiz_scan2x2_x; | |
| 1264 | 1518700 | scan_x_off = horiz_scan4x4_y; | |
| 1265 | 1518700 | scan_y_off = horiz_scan4x4_x; | |
| 1266 | 1518700 | num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y]; | |
| 1267 | 1518700 | break; | |
| 1268 | } | ||
| 1269 | 14292833 | num_coeff++; | |
| 1270 | 14292833 | num_last_subset = (num_coeff - 1) >> 4; | |
| 1271 | |||
| 1272 | 14292833 | max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y); | |
| 1273 | 14292833 | col_limit = last_significant_coeff_x + last_significant_coeff_y + 4; | |
| 1274 | 14292833 | if (max_xy < 4) | |
| 1275 | 11759299 | col_limit = FFMIN(4, col_limit); | |
| 1276 | 2533534 | else if (max_xy < 8) | |
| 1277 | 1906850 | col_limit = FFMIN(8, col_limit); | |
| 1278 | 626684 | else if (max_xy < 12) | |
| 1279 | 286500 | col_limit = FFMIN(24, col_limit); | |
| 1280 | |||
| 1281 | // idct_dc reads coeffs[0] only and writes the whole block: no clear needed | ||
| 1282 | 14292833 | clear_rows = trafo_size; | |
| 1283 | 14292833 | if (!lc->cu.cu_transquant_bypass_flag && !transform_skip_flag && | |
| 1284 | 13473926 | !(lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)) { | |
| 1285 | 10301147 | if (max_xy == 0) | |
| 1286 | 2494563 | clear_rows = 0; | |
| 1287 | } | ||
| 1288 | 14292833 | if (clear_rows) | |
| 1289 | 11798270 | memset(coeffs, 0, clear_rows * trafo_size * sizeof(int16_t)); | |
| 1290 | |||
| 1291 | 39507533 | for (i = num_last_subset; i >= 0; i--) { | |
| 1292 | int n, m; | ||
| 1293 | int x_cg, y_cg, x_c, y_c, pos; | ||
| 1294 | 25214700 | int implicit_non_zero_coeff = 0; | |
| 1295 | int64_t trans_coeff_level; | ||
| 1296 | 25214700 | int prev_sig = 0; | |
| 1297 | 25214700 | int offset = i << 4; | |
| 1298 | 25214700 | int rice_init = 0; | |
| 1299 | |||
| 1300 | uint8_t significant_coeff_flag_idx[16]; | ||
| 1301 | 25214700 | uint8_t nb_significant_coeff_flag = 0; | |
| 1302 | |||
| 1303 | 25214700 | x_cg = scan_x_cg[i]; | |
| 1304 | 25214700 | y_cg = scan_y_cg[i]; | |
| 1305 | |||
| 1306 | 25214700 | if ((i < num_last_subset) && (i > 0)) { | |
| 1307 | 8388333 | int ctx_cg = 0; | |
| 1308 | 8388333 | if (x_cg < (1 << (log2_trafo_size - 2)) - 1) | |
| 1309 | 7715540 | ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg]; | |
| 1310 | 8388333 | if (y_cg < (1 << (log2_trafo_size - 2)) - 1) | |
| 1311 | 7091952 | ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1]; | |
| 1312 | |||
| 1313 | 8388333 | significant_coeff_group_flag[x_cg][y_cg] = | |
| 1314 | 8388333 | significant_coeff_group_flag_decode(lc, c_idx, ctx_cg); | |
| 1315 | 8388333 | implicit_non_zero_coeff = 1; | |
| 1316 | } else { | ||
| 1317 | 16826367 | significant_coeff_group_flag[x_cg][y_cg] = | |
| 1318 | 19359901 | ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) || | |
| 1319 | 2533534 | (x_cg == 0 && y_cg == 0)); | |
| 1320 | } | ||
| 1321 | |||
| 1322 | 25214700 | last_scan_pos = num_coeff - offset - 1; | |
| 1323 | |||
| 1324 | 25214700 | if (i == num_last_subset) { | |
| 1325 | 14292833 | n_end = last_scan_pos - 1; | |
| 1326 | 14292833 | significant_coeff_flag_idx[0] = last_scan_pos; | |
| 1327 | 14292833 | nb_significant_coeff_flag = 1; | |
| 1328 | } else { | ||
| 1329 | 10921867 | n_end = 15; | |
| 1330 | } | ||
| 1331 | |||
| 1332 | 25214700 | if (x_cg < ((1 << log2_trafo_size) - 1) >> 2) | |
| 1333 | 16109627 | prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg]; | |
| 1334 | 25214700 | if (y_cg < ((1 << log2_trafo_size) - 1) >> 2) | |
| 1335 | 15623208 | prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1); | |
| 1336 | |||
| 1337 | 25214700 | if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) { | |
| 1338 | // ctx_idx_map composed with the intra-CG scan, indexed by scan position | ||
| 1339 | static const uint8_t ctx_idx_map[3][5 * 16] = { | ||
| 1340 | { // SCAN_DIAG | ||
| 1341 | 0, 2, 1, 6, 3, 4, 7, 6, 4, 5, 7, 8, 5, 8, 8, 8, // log2_trafo_size == 2 | ||
| 1342 | 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0 | ||
| 1343 | 2, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, // prev_sig == 1 | ||
| 1344 | 2, 2, 1, 2, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, // prev_sig == 2 | ||
| 1345 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // default | ||
| 1346 | }, | ||
| 1347 | { // SCAN_HORIZ | ||
| 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, | ||
| 1353 | }, | ||
| 1354 | { // SCAN_VERT | ||
| 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, | ||
| 1360 | }, | ||
| 1361 | }; | ||
| 1362 | const uint8_t *ctx_idx_map_p; | ||
| 1363 | 18470864 | int scf_offset = 0; | |
| 1364 | int nb0; | ||
| 1365 | 18470864 | if (sps->transform_skip_context_enabled && | |
| 1366 | 592618 | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { | |
| 1367 | 94450 | ctx_idx_map_p = &ctx_idx_map[scan_idx][4 * 16]; | |
| 1368 | 94450 | if (c_idx == 0) { | |
| 1369 | 41170 | scf_offset = 40; | |
| 1370 | } else { | ||
| 1371 | 53280 | scf_offset = 14 + 27; | |
| 1372 | } | ||
| 1373 | } else { | ||
| 1374 | 18376414 | if (c_idx != 0) | |
| 1375 | 3812420 | scf_offset = 27; | |
| 1376 | 18376414 | if (log2_trafo_size == 2) { | |
| 1377 | 5707705 | ctx_idx_map_p = &ctx_idx_map[scan_idx][0]; | |
| 1378 | } else { | ||
| 1379 | 12668709 | ctx_idx_map_p = &ctx_idx_map[scan_idx][(prev_sig + 1) << 4]; | |
| 1380 | 12668709 | if (c_idx == 0) { | |
| 1381 | 10519775 | if ((x_cg > 0 || y_cg > 0)) | |
| 1382 | 6109013 | scf_offset += 3; | |
| 1383 | 10519775 | if (log2_trafo_size == 3) { | |
| 1384 | 3842128 | scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15; | |
| 1385 | } else { | ||
| 1386 | 6677647 | scf_offset += 21; | |
| 1387 | } | ||
| 1388 | } else { | ||
| 1389 | 2148934 | if (log2_trafo_size == 3) | |
| 1390 | 997694 | scf_offset += 9; | |
| 1391 | else | ||
| 1392 | 1151240 | scf_offset += 12; | |
| 1393 | } | ||
| 1394 | } | ||
| 1395 | } | ||
| 1396 | // the flag is a fresh bin: store always, advance conditionally | ||
| 1397 | 18470864 | nb0 = nb_significant_coeff_flag; | |
| 1398 | 188581080 | for (n = n_end; n > 0; n--) { | |
| 1399 | 170110216 | int sig = significant_coeff_flag_decode(lc, n, scf_offset, ctx_idx_map_p); | |
| 1400 | 170110216 | significant_coeff_flag_idx[nb_significant_coeff_flag] = n; | |
| 1401 | 170110216 | nb_significant_coeff_flag += sig; | |
| 1402 | } | ||
| 1403 | 18470864 | if (nb_significant_coeff_flag != nb0) | |
| 1404 | 14776659 | implicit_non_zero_coeff = 0; | |
| 1405 | 18470864 | if (implicit_non_zero_coeff == 0) { | |
| 1406 | 18389127 | if (sps->transform_skip_context_enabled && | |
| 1407 | 591196 | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { | |
| 1408 | 94165 | if (c_idx == 0) { | |
| 1409 | 40977 | scf_offset = 42; | |
| 1410 | } else { | ||
| 1411 | 53188 | scf_offset = 16 + 27; | |
| 1412 | } | ||
| 1413 | } else { | ||
| 1414 | 18294962 | if (i == 0) { | |
| 1415 | 11113030 | if (c_idx == 0) | |
| 1416 | 8454981 | scf_offset = 0; | |
| 1417 | else | ||
| 1418 | 2658049 | scf_offset = 27; | |
| 1419 | } else { | ||
| 1420 | 7181932 | scf_offset = 2 + scf_offset; | |
| 1421 | } | ||
| 1422 | } | ||
| 1423 | 18389127 | significant_coeff_flag_idx[nb_significant_coeff_flag] = 0; | |
| 1424 | 18389127 | nb_significant_coeff_flag += | |
| 1425 | 18389127 | significant_coeff_flag_decode_0(lc, c_idx, scf_offset); | |
| 1426 | } else { | ||
| 1427 | 81737 | significant_coeff_flag_idx[nb_significant_coeff_flag] = 0; | |
| 1428 | 81737 | nb_significant_coeff_flag++; | |
| 1429 | } | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | 25214700 | n_end = nb_significant_coeff_flag; | |
| 1433 | |||
| 1434 | |||
| 1435 | 25214700 | if (n_end) { | |
| 1436 | int first_nz_pos_in_cg; | ||
| 1437 | int last_nz_pos_in_cg; | ||
| 1438 | 21796811 | int c_rice_param = 0; | |
| 1439 | 21796811 | int first_greater1_coeff_idx = -1; | |
| 1440 | uint8_t coeff_abs_level_greater1_flag[8]; | ||
| 1441 | uint16_t coeff_sign_flag; | ||
| 1442 | 21796811 | int sum_abs = 0; | |
| 1443 | int sign_hidden; | ||
| 1444 | int sb_type; | ||
| 1445 | 21796811 | int gt1_mask = 0; | |
| 1446 | |||
| 1447 | |||
| 1448 | // initialize first elem of coeff_bas_level_greater1_flag | ||
| 1449 | 21796811 | int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0; | |
| 1450 | |||
| 1451 | 21796811 | if (sps->persistent_rice_adaptation_enabled) { | |
| 1452 | 769625 | if (!transform_skip_flag && !lc->cu.cu_transquant_bypass_flag) | |
| 1453 | 685062 | sb_type = 2 * (c_idx == 0 ? 1 : 0); | |
| 1454 | else | ||
| 1455 | 84563 | sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1; | |
| 1456 | 769625 | c_rice_param = lc->sub.stat_coeff[sb_type] / 4; | |
| 1457 | } | ||
| 1458 | |||
| 1459 | 21796811 | if (!(i == num_last_subset) && greater1_ctx == 0) | |
| 1460 | 3068711 | ctx_set++; | |
| 1461 | 21796811 | greater1_ctx = 1; | |
| 1462 | 21796811 | last_nz_pos_in_cg = significant_coeff_flag_idx[0]; | |
| 1463 | |||
| 1464 | // fresh bins: branchless context update, first set index from the mask | ||
| 1465 | 108329163 | for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) { | |
| 1466 | 86532352 | int inc = (ctx_set << 2) + greater1_ctx; | |
| 1467 | 86532352 | int flag = coeff_abs_level_greater1_flag_decode(lc, c_idx, inc); | |
| 1468 | 86532352 | coeff_abs_level_greater1_flag[m] = flag; | |
| 1469 | 86532352 | gt1_mask |= flag << m; | |
| 1470 | 86532352 | greater1_ctx = (greater1_ctx + (greater1_ctx - 1U < 2)) & (flag - 1); | |
| 1471 | } | ||
| 1472 | 21796811 | if (gt1_mask) | |
| 1473 | 9392820 | first_greater1_coeff_idx = ff_ctz(gt1_mask); | |
| 1474 | 21796811 | first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1]; | |
| 1475 | |||
| 1476 | 21796811 | if (lc->cu.cu_transquant_bypass_flag || | |
| 1477 | 21077869 | (lc->cu.pred_mode == MODE_INTRA && | |
| 1478 | 11508968 | sps->implicit_rdpcm_enabled && transform_skip_flag && | |
| 1479 | 21050075 | (pred_mode_intra == 10 || pred_mode_intra == 26 )) || | |
| 1480 | explicit_rdpcm_flag) | ||
| 1481 | 765433 | sign_hidden = 0; | |
| 1482 | else | ||
| 1483 | 21031378 | sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4); | |
| 1484 | |||
| 1485 | 21796811 | if (first_greater1_coeff_idx != -1) { | |
| 1486 | 9392820 | coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(lc, c_idx, ctx_set); | |
| 1487 | } | ||
| 1488 | 21796811 | if (!pps->sign_data_hiding_flag || !sign_hidden ) { | |
| 1489 | 12072878 | coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag); | |
| 1490 | } else { | ||
| 1491 | 9723933 | coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1)); | |
| 1492 | } | ||
| 1493 | |||
| 1494 | 124358168 | for (m = 0; m < n_end; m++) { | |
| 1495 | int64_t sign; | ||
| 1496 | |||
| 1497 | 102561357 | n = significant_coeff_flag_idx[m]; | |
| 1498 | 102561357 | GET_COORD(offset, n); | |
| 1499 | 102561357 | if (m < 8) { | |
| 1500 | 86532352 | trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m]; | |
| 1501 | 86532352 | if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) { | |
| 1502 | 20879093 | int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param); | |
| 1503 | |||
| 1504 | 20879093 | trans_coeff_level += last_coeff_abs_level_remaining; | |
| 1505 | 20879093 | if (trans_coeff_level > (3 << c_rice_param)) | |
| 1506 | 5280154 | c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4); | |
| 1507 | 20879093 | if (sps->persistent_rice_adaptation_enabled && !rice_init) { | |
| 1508 | 182004 | int c_rice_p_init = lc->sub.stat_coeff[sb_type] / 4; | |
| 1509 | 182004 | if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init)) | |
| 1510 | 20309 | lc->sub.stat_coeff[sb_type]++; | |
| 1511 | 161695 | else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init)) | |
| 1512 | 93774 | if (lc->sub.stat_coeff[sb_type] > 0) | |
| 1513 | 20285 | lc->sub.stat_coeff[sb_type]--; | |
| 1514 | 182004 | rice_init = 1; | |
| 1515 | } | ||
| 1516 | } | ||
| 1517 | } else { | ||
| 1518 | 16029005 | int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param); | |
| 1519 | |||
| 1520 | 16029005 | trans_coeff_level = 1 + last_coeff_abs_level_remaining; | |
| 1521 | 16029005 | if (trans_coeff_level > (3 << c_rice_param)) | |
| 1522 | 1830372 | c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4); | |
| 1523 | 16029005 | if (sps->persistent_rice_adaptation_enabled && !rice_init) { | |
| 1524 | 22670 | int c_rice_p_init = lc->sub.stat_coeff[sb_type] / 4; | |
| 1525 | 22670 | if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init)) | |
| 1526 | 785 | lc->sub.stat_coeff[sb_type]++; | |
| 1527 | 21885 | else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init)) | |
| 1528 | 15387 | if (lc->sub.stat_coeff[sb_type] > 0) | |
| 1529 | 744 | lc->sub.stat_coeff[sb_type]--; | |
| 1530 | 22670 | rice_init = 1; | |
| 1531 | } | ||
| 1532 | } | ||
| 1533 | 102561357 | if (pps->sign_data_hiding_flag && sign_hidden) { | |
| 1534 | 69875836 | sum_abs += trans_coeff_level; | |
| 1535 | 69875836 | if (n == first_nz_pos_in_cg && (sum_abs&1)) | |
| 1536 | 4844351 | trans_coeff_level = -trans_coeff_level; | |
| 1537 | } | ||
| 1538 | // equiprobable sign: apply it branchless | ||
| 1539 | 102561357 | sign = -(int64_t)(coeff_sign_flag >> 15); | |
| 1540 | 102561357 | trans_coeff_level = (trans_coeff_level ^ sign) - sign; | |
| 1541 | 102561357 | coeff_sign_flag <<= 1; | |
| 1542 | 102561357 | if(!lc->cu.cu_transquant_bypass_flag) { | |
| 1543 | 93733974 | if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) { | |
| 1544 | 3157153 | if(y_c || x_c || log2_trafo_size < 4) { | |
| 1545 | 3073518 | switch(log2_trafo_size) { | |
| 1546 | 731538 | case 3: pos = (y_c << 3) + x_c; break; | |
| 1547 | 926236 | case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break; | |
| 1548 | 413079 | case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break; | |
| 1549 | 1002665 | default: pos = (y_c << 2) + x_c; break; | |
| 1550 | } | ||
| 1551 | 3073518 | scale_m = scale_matrix[pos]; | |
| 1552 | } else { | ||
| 1553 | 83635 | scale_m = dc_scale; | |
| 1554 | } | ||
| 1555 | } | ||
| 1556 | 93733974 | trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift; | |
| 1557 | 93733974 | trans_coeff_level = FFMAX(FFMIN(trans_coeff_level, 32767), -32768); | |
| 1558 | } | ||
| 1559 | 102561357 | coeffs[y_c * trafo_size + x_c] = trans_coeff_level; | |
| 1560 | } | ||
| 1561 | } | ||
| 1562 | } | ||
| 1563 | |||
| 1564 | 14292833 | if (lc->cu.cu_transquant_bypass_flag) { | |
| 1565 | 424198 | 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; | |
| 1568 | |||
| 1569 | ✗ | s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode); | |
| 1570 | } | ||
| 1571 | } else { | ||
| 1572 | 13868635 | if (transform_skip_flag) { | |
| 1573 | 59846 | int rot = sps->transform_skip_rotation_enabled && | |
| 1574 | 454555 | log2_trafo_size == 2 && | |
| 1575 | 45633 | lc->cu.pred_mode == MODE_INTRA; | |
| 1576 | 394709 | if (rot) { | |
| 1577 | 106650 | for (i = 0; i < 8; i++) | |
| 1578 | 94800 | FFSWAP(int16_t, coeffs[i], coeffs[16 - i - 1]); | |
| 1579 | } | ||
| 1580 | |||
| 1581 | 394709 | s->hevcdsp.dequant(coeffs, log2_trafo_size); | |
| 1582 | |||
| 1583 | 394709 | if (explicit_rdpcm_flag || (sps->implicit_rdpcm_enabled && | |
| 1584 | 56756 | lc->cu.pred_mode == MODE_INTRA && | |
| 1585 | 15060 | (pred_mode_intra == 10 || pred_mode_intra == 26))) { | |
| 1586 | 17944 | int mode = explicit_rdpcm_flag ? explicit_rdpcm_dir_flag : (pred_mode_intra == 26); | |
| 1587 | |||
| 1588 | 17944 | s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode); | |
| 1589 | } | ||
| 1590 | 13473926 | } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) { | |
| 1591 | 3172779 | s->hevcdsp.transform_4x4_luma(coeffs); | |
| 1592 | } else { | ||
| 1593 | 10301147 | if (max_xy == 0) | |
| 1594 | 2494563 | s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs); | |
| 1595 | else | ||
| 1596 | 7806584 | s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit); | |
| 1597 | } | ||
| 1598 | } | ||
| 1599 | 14292833 | if (lc->tu.cross_pf) { | |
| 1600 | 171429 | int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer; | |
| 1601 | |||
| 1602 | 13711509 | for (i = 0; i < (trafo_size * trafo_size); i++) { | |
| 1603 | 13540080 | coeffs[i] = coeffs[i] + ((lc->tu.res_scale_val * coeffs_y[i]) >> 3); | |
| 1604 | } | ||
| 1605 | } | ||
| 1606 | 14292833 | s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride); | |
| 1607 | 14292833 | } | |
| 1608 | |||
| 1609 | 3379290 | void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size) | |
| 1610 | { | ||
| 1611 | 3379290 | int x = abs_mvd_greater0_flag_decode(lc); | |
| 1612 | 3379290 | int y = abs_mvd_greater0_flag_decode(lc); | |
| 1613 | |||
| 1614 | 3379290 | if (x) | |
| 1615 | 2318699 | x += abs_mvd_greater1_flag_decode(lc); | |
| 1616 | 3379290 | if (y) | |
| 1617 | 2223931 | y += abs_mvd_greater1_flag_decode(lc); | |
| 1618 | |||
| 1619 | 3379290 | switch (x) { | |
| 1620 | 1611646 | case 2: lc->pu.mvd.x = mvd_decode(lc); break; | |
| 1621 | 707053 | case 1: lc->pu.mvd.x = mvd_sign_flag_decode(lc); break; | |
| 1622 | 1060591 | case 0: lc->pu.mvd.x = 0; break; | |
| 1623 | } | ||
| 1624 | |||
| 1625 | 3379290 | switch (y) { | |
| 1626 | 1501100 | case 2: lc->pu.mvd.y = mvd_decode(lc); break; | |
| 1627 | 722831 | case 1: lc->pu.mvd.y = mvd_sign_flag_decode(lc); break; | |
| 1628 | 1155359 | case 0: lc->pu.mvd.y = 0; break; | |
| 1629 | } | ||
| 1630 | 3379290 | } | |
| 1631 |