FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/cabac.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 638 659 96.8%
Functions: 54 55 98.2%
Branches: 497 531 93.6%

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 "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 1596779 void ff_hevc_save_states(HEVCLocalContext *lc, const HEVCPPS *pps,
403 int ctb_addr_ts)
404 {
405 1596779 const HEVCSPS *const sps = pps->sps;
406
2/2
✓ Branch 0 taken 167314 times.
✓ Branch 1 taken 1429465 times.
1596779 if (pps->entropy_coding_sync_enabled_flag &&
407
2/2
✓ Branch 0 taken 160629 times.
✓ Branch 1 taken 6685 times.
167314 (ctb_addr_ts % sps->ctb_width == 2 ||
408
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 159845 times.
160629 (sps->ctb_width == 2 &&
409
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 392 times.
784 ctb_addr_ts % sps->ctb_width == 0))) {
410 7077 memcpy(lc->common_cabac_state->state, lc->cabac_state, HEVC_CONTEXTS);
411
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7029 times.
7077 if (sps->persistent_rice_adaptation_enabled) {
412 48 memcpy(lc->common_cabac_state->stat_coeff, lc->stat_coeff, HEVC_STAT_COEFFS);
413 }
414 }
415 1596779 }
416
417 5909 static void load_states(HEVCLocalContext *lc, const HEVCSPS *sps)
418 {
419 5909 memcpy(lc->cabac_state, lc->common_cabac_state->state, HEVC_CONTEXTS);
420
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5865 times.
5909 if (sps->persistent_rice_adaptation_enabled) {
421 44 memcpy(lc->stat_coeff, lc->common_cabac_state->stat_coeff, HEVC_STAT_COEFFS);
422 }
423 5909 }
424
425 9574 static int cabac_reinit(HEVCLocalContext *lc)
426 {
427
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9574 times.
9574 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
428 }
429
430 25560 static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
431 {
432 25560 int init_type = 2 - s->sh.slice_type;
433 int i;
434
435
4/4
✓ Branch 0 taken 6606 times.
✓ Branch 1 taken 18954 times.
✓ Branch 2 taken 6462 times.
✓ Branch 3 taken 144 times.
25560 if (s->sh.cabac_init_flag && s->sh.slice_type != HEVC_SLICE_I)
436 6462 init_type ^= 3;
437
438
2/2
✓ Branch 0 taken 5086440 times.
✓ Branch 1 taken 25560 times.
5112000 for (i = 0; i < HEVC_CONTEXTS; i++) {
439 5086440 int init_value = init_values[init_type][i];
440 5086440 int m = (init_value >> 4) * 5 - 45;
441 5086440 int n = ((init_value & 15) << 3) - 16;
442 5086440 int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
443
444 5086440 pre ^= pre >> 31;
445
2/2
✓ Branch 0 taken 522236 times.
✓ Branch 1 taken 4564204 times.
5086440 if (pre > 124)
446 522236 pre = 124 + (pre & 1);
447 5086440 lc->cabac_state[i] = pre;
448 }
449
450
2/2
✓ Branch 0 taken 102240 times.
✓ Branch 1 taken 25560 times.
127800 for (i = 0; i < 4; i++)
451 102240 lc->stat_coeff[i] = 0;
452 25560 }
453
454 1596780 int ff_hevc_cabac_init(HEVCLocalContext *lc, const HEVCPPS *pps,
455 int ctb_addr_ts, const uint8_t *data, size_t size,
456 int is_wpp)
457 {
458 1596780 const HEVCContext *const s = lc->parent;
459 1596780 const HEVCSPS *const sps = pps->sps;
460
461
2/2
✓ Branch 0 taken 28279 times.
✓ Branch 1 taken 1568501 times.
1596780 if (ctb_addr_ts == pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
462 28279 int ret = ff_init_cabac_decoder(&lc->cc, data, size);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28279 times.
28279 if (ret < 0)
464 return ret;
465
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 20332 times.
28279 if (s->sh.dependent_slice_segment_flag == 0 ||
466
2/2
✓ Branch 0 taken 876 times.
✓ Branch 1 taken 7071 times.
7947 (pps->tiles_enabled_flag &&
467
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 567 times.
876 pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]))
468 20641 cabac_init_state(lc, s);
469
470
2/2
✓ Branch 0 taken 17996 times.
✓ Branch 1 taken 10283 times.
28279 if (!s->sh.first_slice_in_pic_flag &&
471
2/2
✓ Branch 0 taken 3853 times.
✓ Branch 1 taken 14143 times.
17996 pps->entropy_coding_sync_enabled_flag) {
472
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 2217 times.
3853 if (ctb_addr_ts % sps->ctb_width == 0) {
473
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1544 times.
1636 if (sps->ctb_width == 1)
474 92 cabac_init_state(lc, s);
475
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 382 times.
1544 else if (s->sh.dependent_slice_segment_flag == 1)
476 1162 load_states(lc, sps);
477 }
478 }
479 } else {
480
2/2
✓ Branch 0 taken 290871 times.
✓ Branch 1 taken 1277630 times.
1568501 if (pps->tiles_enabled_flag &&
481
2/2
✓ Branch 0 taken 4625 times.
✓ Branch 1 taken 286246 times.
290871 pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) {
482 int ret;
483
1/2
✓ Branch 0 taken 4625 times.
✗ Branch 1 not taken.
4625 if (!is_wpp)
484 4625 ret = cabac_reinit(lc);
485 else {
486 ret = ff_init_cabac_decoder(&lc->cc, data, size);
487 }
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4625 times.
4625 if (ret < 0)
489 return ret;
490 4625 cabac_init_state(lc, s);
491 }
492
2/2
✓ Branch 0 taken 162577 times.
✓ Branch 1 taken 1405924 times.
1568501 if (pps->entropy_coding_sync_enabled_flag) {
493
2/2
✓ Branch 0 taken 4949 times.
✓ Branch 1 taken 157628 times.
162577 if (ctb_addr_ts % sps->ctb_width == 0) {
494 int ret;
495 4949 get_cabac_terminate(&lc->cc);
496
1/2
✓ Branch 0 taken 4949 times.
✗ Branch 1 not taken.
4949 if (!is_wpp)
497 4949 ret = cabac_reinit(lc);
498 else {
499 ret = ff_init_cabac_decoder(&lc->cc, data, size);
500 }
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4949 times.
4949 if (ret < 0)
502 return ret;
503
504
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 4747 times.
4949 if (sps->ctb_width == 1)
505 202 cabac_init_state(lc, s);
506 else
507 4747 load_states(lc, sps);
508 }
509 }
510 }
511 1596780 return 0;
512 }
513
514 #define GET_CABAC(ctx) get_cabac(&lc->cc, &lc->cabac_state[ctx])
515
516 1030594 int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc)
517 {
518 1030594 return GET_CABAC(SAO_MERGE_FLAG_OFFSET);
519 }
520
521 520925 int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
522 {
523
2/2
✓ Branch 1 taken 347580 times.
✓ Branch 2 taken 173345 times.
520925 if (!GET_CABAC(SAO_TYPE_IDX_OFFSET))
524 347580 return 0;
525
526
2/2
✓ Branch 1 taken 55467 times.
✓ Branch 2 taken 117878 times.
173345 if (!get_cabac_bypass(&lc->cc))
527 55467 return SAO_BAND;
528 117878 return SAO_EDGE;
529 }
530
531 79988 int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc)
532 {
533 int i;
534 79988 int value = get_cabac_bypass(&lc->cc);
535
536
2/2
✓ Branch 0 taken 319952 times.
✓ Branch 1 taken 79988 times.
399940 for (i = 0; i < 4; i++)
537 319952 value = (value << 1) | get_cabac_bypass(&lc->cc);
538 79988 return value;
539 }
540
541 923876 int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc, int bit_depth)
542 {
543 923876 int i = 0;
544 923876 int length = (1 << (FFMIN(bit_depth, 10) - 5)) - 1;
545
546
4/4
✓ Branch 0 taken 2560518 times.
✓ Branch 1 taken 127946 times.
✓ Branch 3 taken 1764588 times.
✓ Branch 4 taken 795930 times.
2688464 while (i < length && get_cabac_bypass(&lc->cc))
547 1764588 i++;
548 923876 return i;
549 }
550
551 247932 int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc)
552 {
553 247932 return get_cabac_bypass(&lc->cc);
554 }
555
556 117878 int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
557 {
558 117878 int ret = get_cabac_bypass(&lc->cc) << 1;
559 117878 ret |= get_cabac_bypass(&lc->cc);
560 117878 return ret;
561 }
562
563 1596779 int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc)
564 {
565 1596779 return get_cabac_terminate(&lc->cc);
566 }
567
568 211788 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCLocalContext *lc)
569 {
570 211788 return GET_CABAC(CU_TRANSQUANT_BYPASS_FLAG_OFFSET);
571 }
572
573 8656190 int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, uint8_t *skip_flag,
574 int x0, int y0, int x_cb, int y_cb, int min_cb_width)
575 {
576 8656190 int inc = 0;
577
578
4/4
✓ Branch 0 taken 770731 times.
✓ Branch 1 taken 7885459 times.
✓ Branch 2 taken 553042 times.
✓ Branch 3 taken 217689 times.
8656190 if (lc->ctb_left_flag || x0)
579 8438501 inc = !!SAMPLE_CTB(skip_flag, x_cb - 1, y_cb);
580
4/4
✓ Branch 0 taken 1701811 times.
✓ Branch 1 taken 6954379 times.
✓ Branch 2 taken 1231418 times.
✓ Branch 3 taken 470393 times.
8656190 if (lc->ctb_up_flag || y0)
581 8185797 inc += !!SAMPLE_CTB(skip_flag, x_cb, y_cb - 1);
582
583 8656190 return GET_CABAC(SKIP_FLAG_OFFSET + inc);
584 }
585
586 710974 int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc)
587 {
588 710974 int prefix_val = 0;
589 710974 int suffix_val = 0;
590 710974 int inc = 0;
591
592
4/4
✓ Branch 0 taken 1624442 times.
✓ Branch 1 taken 108968 times.
✓ Branch 3 taken 1022436 times.
✓ Branch 4 taken 602006 times.
1733410 while (prefix_val < 5 && GET_CABAC(CU_QP_DELTA_OFFSET + inc)) {
593 1022436 prefix_val++;
594 1022436 inc = 1;
595 }
596
2/2
✓ Branch 0 taken 108968 times.
✓ Branch 1 taken 602006 times.
710974 if (prefix_val >= 5) {
597 108968 int k = 0;
598
3/4
✓ Branch 0 taken 366683 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 257715 times.
✓ Branch 4 taken 108968 times.
366683 while (k < 7 && get_cabac_bypass(&lc->cc)) {
599 257715 suffix_val += 1 << k;
600 257715 k++;
601 }
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108968 times.
108968 if (k == 7) {
603 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
604 return AVERROR_INVALIDDATA;
605 }
606
607
2/2
✓ Branch 0 taken 257715 times.
✓ Branch 1 taken 108968 times.
366683 while (k--)
608 257715 suffix_val += get_cabac_bypass(&lc->cc) << k;
609 }
610 710974 return prefix_val + suffix_val;
611 }
612
613 391160 int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc)
614 {
615 391160 return get_cabac_bypass(&lc->cc);
616 }
617
618 266275 int ff_hevc_cu_chroma_qp_offset_flag(HEVCLocalContext *lc)
619 {
620 266275 return GET_CABAC(CU_CHROMA_QP_OFFSET_FLAG_OFFSET);
621 }
622
623 int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc, int chroma_qp_offset_list_len_minus1)
624 {
625 int c_max= FFMAX(5, chroma_qp_offset_list_len_minus1);
626 int i = 0;
627
628 while (i < c_max && GET_CABAC(CU_CHROMA_QP_OFFSET_IDX_OFFSET))
629 i++;
630
631 return i;
632 }
633
634 4545598 int ff_hevc_pred_mode_decode(HEVCLocalContext *lc)
635 {
636 4545598 return GET_CABAC(PRED_MODE_FLAG_OFFSET);
637 }
638
639 8165934 int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, uint8_t *tab_ct_depth,
640 const HEVCSPS *sps,
641 int ct_depth, int x0, int y0)
642 {
643 8165934 int inc = 0, depth_left = 0, depth_top = 0;
644 8165934 int x0b = av_zero_extend(x0, sps->log2_ctb_size);
645 8165934 int y0b = av_zero_extend(y0, sps->log2_ctb_size);
646 8165934 int x_cb = x0 >> sps->log2_min_cb_size;
647 8165934 int y_cb = y0 >> sps->log2_min_cb_size;
648
649
4/4
✓ Branch 0 taken 735878 times.
✓ Branch 1 taken 7430056 times.
✓ Branch 2 taken 416721 times.
✓ Branch 3 taken 319157 times.
8165934 if (lc->ctb_left_flag || x0b)
650 7846777 depth_left = tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1];
651
4/4
✓ Branch 0 taken 1517604 times.
✓ Branch 1 taken 6648330 times.
✓ Branch 2 taken 847254 times.
✓ Branch 3 taken 670350 times.
8165934 if (lc->ctb_up_flag || y0b)
652 7495584 depth_top = tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb];
653
654 8165934 inc += (depth_left > ct_depth);
655 8165934 inc += (depth_top > ct_depth);
656
657 8165934 return GET_CABAC(SPLIT_CODING_UNIT_FLAG_OFFSET + inc);
658 }
659
660 6679812 int ff_hevc_part_mode_decode(HEVCLocalContext *lc, const HEVCSPS *sps, int log2_cb_size)
661 {
662
2/2
✓ Branch 1 taken 3949141 times.
✓ Branch 2 taken 2730671 times.
6679812 if (GET_CABAC(PART_MODE_OFFSET)) // 1
663 3949141 return PART_2Nx2N;
664
2/2
✓ Branch 0 taken 2056729 times.
✓ Branch 1 taken 673942 times.
2730671 if (log2_cb_size == sps->log2_min_cb_size) {
665
2/2
✓ Branch 0 taken 1283790 times.
✓ Branch 1 taken 772939 times.
2056729 if (lc->cu.pred_mode == MODE_INTRA) // 0
666 1283790 return PART_NxN;
667
2/2
✓ Branch 1 taken 316731 times.
✓ Branch 2 taken 456208 times.
772939 if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01
668 316731 return PART_2NxN;
669
2/2
✓ Branch 0 taken 232935 times.
✓ Branch 1 taken 223273 times.
456208 if (log2_cb_size == 3) // 00
670 232935 return PART_Nx2N;
671
2/2
✓ Branch 1 taken 115501 times.
✓ Branch 2 taken 107772 times.
223273 if (GET_CABAC(PART_MODE_OFFSET + 2)) // 001
672 115501 return PART_Nx2N;
673 107772 return PART_NxN; // 000
674 }
675
676
2/2
✓ Branch 0 taken 69538 times.
✓ Branch 1 taken 604404 times.
673942 if (!sps->amp_enabled) {
677
2/2
✓ Branch 1 taken 30136 times.
✓ Branch 2 taken 39402 times.
69538 if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01
678 30136 return PART_2NxN;
679 39402 return PART_Nx2N;
680 }
681
682
2/2
✓ Branch 1 taken 241002 times.
✓ Branch 2 taken 363402 times.
604404 if (GET_CABAC(PART_MODE_OFFSET + 1)) { // 01X, 01XX
683
2/2
✓ Branch 1 taken 115674 times.
✓ Branch 2 taken 125328 times.
241002 if (GET_CABAC(PART_MODE_OFFSET + 3)) // 011
684 115674 return PART_2NxN;
685
2/2
✓ Branch 1 taken 58064 times.
✓ Branch 2 taken 67264 times.
125328 if (get_cabac_bypass(&lc->cc)) // 0101
686 58064 return PART_2NxnD;
687 67264 return PART_2NxnU; // 0100
688 }
689
690
2/2
✓ Branch 1 taken 177449 times.
✓ Branch 2 taken 185953 times.
363402 if (GET_CABAC(PART_MODE_OFFSET + 3)) // 001
691 177449 return PART_Nx2N;
692
2/2
✓ Branch 1 taken 85773 times.
✓ Branch 2 taken 100180 times.
185953 if (get_cabac_bypass(&lc->cc)) // 0001
693 85773 return PART_nRx2N;
694 100180 return PART_nLx2N; // 0000
695 }
696
697 153183 int ff_hevc_pcm_flag_decode(HEVCLocalContext *lc)
698 {
699 153183 return get_cabac_terminate(&lc->cc);
700 }
701
702 8197062 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc)
703 {
704 8197062 return GET_CABAC(PREV_INTRA_LUMA_PRED_FLAG_OFFSET);
705 }
706
707 4921640 int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc)
708 {
709 4921640 int i = 0;
710
4/4
✓ Branch 0 taken 7330854 times.
✓ Branch 1 taken 1026794 times.
✓ Branch 3 taken 3436008 times.
✓ Branch 4 taken 3894846 times.
8357648 while (i < 2 && get_cabac_bypass(&lc->cc))
711 3436008 i++;
712 4921640 return i;
713 }
714
715 3275422 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc)
716 {
717 int i;
718 3275422 int value = get_cabac_bypass(&lc->cc);
719
720
2/2
✓ Branch 0 taken 13101688 times.
✓ Branch 1 taken 3275422 times.
16377110 for (i = 0; i < 4; i++)
721 13101688 value = (value << 1) | get_cabac_bypass(&lc->cc);
722 3275422 return value;
723 }
724
725 4447053 int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc)
726 {
727 int ret;
728
2/2
✓ Branch 1 taken 3343075 times.
✓ Branch 2 taken 1103978 times.
4447053 if (!GET_CABAC(INTRA_CHROMA_PRED_MODE_OFFSET))
729 3343075 return 4;
730
731 1103978 ret = get_cabac_bypass(&lc->cc) << 1;
732 1103978 ret |= get_cabac_bypass(&lc->cc);
733 1103978 return ret;
734 }
735
736 6542463 int ff_hevc_merge_idx_decode(HEVCLocalContext *lc)
737 {
738 6542463 int i = GET_CABAC(MERGE_IDX_OFFSET);
739
740
2/2
✓ Branch 0 taken 2632841 times.
✓ Branch 1 taken 3909622 times.
6542463 if (i != 0) {
741
4/4
✓ Branch 0 taken 4039035 times.
✓ Branch 1 taken 312517 times.
✓ Branch 3 taken 1718711 times.
✓ Branch 4 taken 2320324 times.
4351552 while (i < lc->parent->sh.max_num_merge_cand-1 && get_cabac_bypass(&lc->cc))
742 1718711 i++;
743 }
744 6542463 return i;
745 }
746
747 5048690 int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
748 {
749 5048690 return GET_CABAC(MERGE_FLAG_OFFSET);
750 }
751
752 1919531 int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH)
753 {
754
2/2
✓ Branch 0 taken 248638 times.
✓ Branch 1 taken 1670893 times.
1919531 if (nPbW + nPbH == 12)
755 248638 return GET_CABAC(INTER_PRED_IDC_OFFSET + 4);
756
2/2
✓ Branch 1 taken 424492 times.
✓ Branch 2 taken 1246401 times.
1670893 if (GET_CABAC(INTER_PRED_IDC_OFFSET + lc->ct_depth))
757 424492 return PRED_BI;
758
759 1246401 return GET_CABAC(INTER_PRED_IDC_OFFSET + 4);
760 }
761
762 2964458 int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
763 {
764 2964458 int i = 0;
765 2964458 int max = num_ref_idx_lx - 1;
766 2964458 int max_ctx = FFMIN(max, 2);
767
768
4/4
✓ Branch 0 taken 2894468 times.
✓ Branch 1 taken 1053388 times.
✓ Branch 3 taken 983398 times.
✓ Branch 4 taken 1911070 times.
3947856 while (i < max_ctx && GET_CABAC(REF_IDX_L0_OFFSET + i))
769 983398 i++;
770
2/2
✓ Branch 0 taken 203411 times.
✓ Branch 1 taken 2761047 times.
2964458 if (i == 2) {
771
4/4
✓ Branch 0 taken 162576 times.
✓ Branch 1 taken 127432 times.
✓ Branch 3 taken 86597 times.
✓ Branch 4 taken 75979 times.
290008 while (i < max && get_cabac_bypass(&lc->cc))
772 86597 i++;
773 }
774
775 2964458 return i;
776 }
777
778 2964458 int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
779 {
780 2964458 return GET_CABAC(MVP_LX_FLAG_OFFSET);
781 }
782
783 2507651 int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc)
784 {
785 2507651 return GET_CABAC(NO_RESIDUAL_DATA_FLAG_OFFSET);
786 }
787
788 5601706 static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc)
789 {
790 5601706 return GET_CABAC(ABS_MVD_GREATER0_FLAG_OFFSET);
791 }
792
793 3850702 static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc)
794 {
795 3850702 return GET_CABAC(ABS_MVD_GREATER1_FLAG_OFFSET + 1);
796 }
797
798 2762681 static av_always_inline int mvd_decode(HEVCLocalContext *lc)
799 {
800 2762681 int ret = 2;
801 2762681 int k = 1;
802
803
3/4
✓ Branch 0 taken 6773990 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4011309 times.
✓ Branch 4 taken 2762681 times.
6773990 while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc)) {
804 4011309 ret += 1U << k;
805 4011309 k++;
806 }
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2762681 times.
2762681 if (k == CABAC_MAX_BIN) {
808 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
809 return 0;
810 }
811
2/2
✓ Branch 0 taken 6773990 times.
✓ Branch 1 taken 2762681 times.
9536671 while (k--)
812 6773990 ret += get_cabac_bypass(&lc->cc) << k;
813 2762681 return get_cabac_bypass_sign(&lc->cc, -ret);
814 }
815
816 1088021 static av_always_inline int mvd_sign_flag_decode(HEVCLocalContext *lc)
817 {
818 1088021 return get_cabac_bypass_sign(&lc->cc, -1);
819 }
820
821 7591133 int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
822 {
823 7591133 return GET_CABAC(SPLIT_TRANSFORM_FLAG_OFFSET + 5 - log2_trafo_size);
824 }
825
826 16436987 int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth)
827 {
828 16436987 return GET_CABAC(CBF_CB_CR_OFFSET + trafo_depth);
829 }
830
831 15745910 int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth)
832 {
833
2/2
✓ Branch 0 taken 2958749 times.
✓ Branch 1 taken 12787161 times.
15745910 return GET_CABAC(CBF_LUMA_OFFSET + !trafo_depth);
834 }
835
836 6809164 static int hevc_transform_skip_flag_decode(HEVCLocalContext *lc, int c_idx)
837 {
838
2/2
✓ Branch 0 taken 2323956 times.
✓ Branch 1 taken 4485208 times.
6809164 return GET_CABAC(TRANSFORM_SKIP_FLAG_OFFSET + !!c_idx);
839 }
840
841 35234 static int explicit_rdpcm_flag_decode(HEVCLocalContext *lc, int c_idx)
842 {
843
2/2
✓ Branch 0 taken 29253 times.
✓ Branch 1 taken 5981 times.
35234 return GET_CABAC(EXPLICIT_RDPCM_FLAG_OFFSET + !!c_idx);
844 }
845
846 3090 static int explicit_rdpcm_dir_flag_decode(HEVCLocalContext *lc, int c_idx)
847 {
848
2/2
✓ Branch 0 taken 1813 times.
✓ Branch 1 taken 1277 times.
3090 return GET_CABAC(EXPLICIT_RDPCM_DIR_FLAG_OFFSET + !!c_idx);
849 }
850
851 384850 int ff_hevc_log2_res_scale_abs(HEVCLocalContext *lc, int idx)
852 {
853 384850 int i =0;
854
855
4/4
✓ Branch 0 taken 1076510 times.
✓ Branch 1 taken 146082 times.
✓ Branch 3 taken 837742 times.
✓ Branch 4 taken 238768 times.
1222592 while (i < 4 && GET_CABAC(LOG2_RES_SCALE_ABS_OFFSET + 4 * idx + i))
856 837742 i++;
857
858 384850 return i;
859 }
860
861 235843 int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx)
862 {
863 235843 return GET_CABAC(RES_SCALE_SIGN_FLAG_OFFSET + idx);
864 }
865
866 15306399 static av_always_inline void last_significant_coeff_xy_prefix_decode(HEVCLocalContext *lc, int c_idx,
867 int log2_size, int *last_scx_prefix, int *last_scy_prefix)
868 {
869 15306399 int i = 0;
870 15306399 int max = (log2_size << 1) - 1;
871 int ctx_offset, ctx_shift;
872
873
2/2
✓ Branch 0 taken 10552869 times.
✓ Branch 1 taken 4753530 times.
15306399 if (!c_idx) {
874 10552869 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2);
875 10552869 ctx_shift = (log2_size + 1) >> 2;
876 } else {
877 4753530 ctx_offset = 15;
878 4753530 ctx_shift = log2_size - 2;
879 }
880
4/4
✓ Branch 0 taken 37104903 times.
✓ Branch 1 taken 2224493 times.
✓ Branch 2 taken 24022997 times.
✓ Branch 3 taken 13081906 times.
76434299 while (i < max &&
881 37104903 GET_CABAC(LAST_SIGNIFICANT_COEFF_X_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset))
882 24022997 i++;
883 15306399 *last_scx_prefix = i;
884
885 15306399 i = 0;
886
4/4
✓ Branch 0 taken 32205749 times.
✓ Branch 1 taken 1751158 times.
✓ Branch 2 taken 18650508 times.
✓ Branch 3 taken 13555241 times.
66162656 while (i < max &&
887 32205749 GET_CABAC(LAST_SIGNIFICANT_COEFF_Y_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset))
888 18650508 i++;
889 15306399 *last_scy_prefix = i;
890 15306399 }
891
892 3237202 static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc,
893 int last_significant_coeff_prefix)
894 {
895 int i;
896 3237202 int length = (last_significant_coeff_prefix >> 1) - 1;
897 3237202 int value = get_cabac_bypass(&lc->cc);
898
899
2/2
✓ Branch 0 taken 899278 times.
✓ Branch 1 taken 3237202 times.
4136480 for (i = 1; i < length; i++)
900 899278 value = (value << 1) | get_cabac_bypass(&lc->cc);
901 3237202 return value;
902 }
903
904 8612523 static av_always_inline int significant_coeff_group_flag_decode(HEVCLocalContext *lc, int c_idx, int ctx_cg)
905 {
906 int inc;
907
908
2/2
✓ Branch 0 taken 1352903 times.
✓ Branch 1 taken 7259620 times.
8612523 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
909
910 8612523 return GET_CABAC(SIGNIFICANT_COEFF_GROUP_FLAG_OFFSET + inc);
911 }
912 177917390 static av_always_inline int significant_coeff_flag_decode(HEVCLocalContext *lc, int x_c, int y_c,
913 int offset, const uint8_t *ctx_idx_map)
914 {
915 177917390 int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
916 177917390 return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + inc);
917 }
918
919 19486073 static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset)
920 {
921 19486073 return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + offset);
922 }
923
924 95638921 static av_always_inline int coeff_abs_level_greater1_flag_decode(HEVCLocalContext *lc, int c_idx, int inc)
925 {
926
927
2/2
✓ Branch 0 taken 22097804 times.
✓ Branch 1 taken 73541117 times.
95638921 if (c_idx > 0)
928 22097804 inc += 16;
929
930 95638921 return GET_CABAC(COEFF_ABS_LEVEL_GREATER1_FLAG_OFFSET + inc);
931 }
932
933 10708960 static av_always_inline int coeff_abs_level_greater2_flag_decode(HEVCLocalContext *lc, int c_idx, int inc)
934 {
935
2/2
✓ Branch 0 taken 2210748 times.
✓ Branch 1 taken 8498212 times.
10708960 if (c_idx > 0)
936 2210748 inc += 4;
937
938 10708960 return GET_CABAC(COEFF_ABS_LEVEL_GREATER2_FLAG_OFFSET + inc);
939 }
940
941 42772789 static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param)
942 {
943 42772789 int prefix = 0;
944 42772789 int suffix = 0;
945 int last_coeff_abs_level_remaining;
946 int i;
947
948
3/4
✓ Branch 0 taken 93409920 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 50637131 times.
✓ Branch 4 taken 42772789 times.
93409920 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc))
949 50637131 prefix++;
950
951
2/2
✓ Branch 0 taken 36579232 times.
✓ Branch 1 taken 6193557 times.
42772789 if (prefix < 3) {
952
2/2
✓ Branch 0 taken 38346485 times.
✓ Branch 1 taken 36579232 times.
74925717 for (i = 0; i < rc_rice_param; i++)
953 38346485 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
954 36579232 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
955 } else {
956 6193557 int prefix_minus3 = prefix - 3;
957
958
2/4
✓ Branch 0 taken 6193557 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6193557 times.
6193557 if (prefix == CABAC_MAX_BIN || prefix_minus3 + rc_rice_param > 16 + 6) {
959 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
960 return 0;
961 }
962
963
2/2
✓ Branch 0 taken 19842983 times.
✓ Branch 1 taken 6193557 times.
26036540 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
964 19842983 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
965 6193557 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
966 6193557 << rc_rice_param) + suffix;
967 }
968 42772789 return last_coeff_abs_level_remaining;
969 }
970
971 23042623 static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb)
972 {
973 int i;
974 23042623 int ret = 0;
975
976
2/2
✓ Branch 0 taken 103035575 times.
✓ Branch 1 taken 23042623 times.
126078198 for (i = 0; i < nb; i++)
977 103035575 ret = (ret << 1) | get_cabac_bypass(&lc->cc);
978 23042623 return ret;
979 }
980
981 15306399 void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, const HEVCPPS *pps,
982 int x0, int y0,
983 int log2_trafo_size, enum ScanType scan_idx,
984 int c_idx)
985 {
986 #define GET_COORD(offset, n) \
987 do { \
988 x_c = (x_cg << 2) + scan_x_off[n]; \
989 y_c = (y_cg << 2) + scan_y_off[n]; \
990 } while (0)
991 15306399 const HEVCContext *const s = lc->parent;
992 15306399 const HEVCSPS *const sps = pps->sps;
993 15306399 int transform_skip_flag = 0;
994
995 int last_significant_coeff_x, last_significant_coeff_y;
996 int last_scan_pos;
997 int n_end;
998 15306399 int num_coeff = 0;
999 15306399 int greater1_ctx = 1;
1000
1001 int num_last_subset;
1002 int x_cg_last_sig, y_cg_last_sig;
1003
1004 const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1005
1006 15306399 ptrdiff_t stride = s->cur_frame->f->linesize[c_idx];
1007 15306399 int hshift = sps->hshift[c_idx];
1008 15306399 int vshift = sps->vshift[c_idx];
1009 15306399 uint8_t *dst = &s->cur_frame->f->data[c_idx][(y0 >> vshift) * stride +
1010 15306399 ((x0 >> hshift) << sps->pixel_shift)];
1011
2/2
✓ Branch 0 taken 4753530 times.
✓ Branch 1 taken 10552869 times.
15306399 int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer);
1012 15306399 uint8_t significant_coeff_group_flag[8][8] = {{0}};
1013 15306399 int explicit_rdpcm_flag = 0;
1014 int explicit_rdpcm_dir_flag;
1015
1016 15306399 int trafo_size = 1 << log2_trafo_size;
1017 int i;
1018 int qp,shift,add,scale,scale_m;
1019 static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
1020 15306399 const uint8_t *scale_matrix = NULL;
1021 uint8_t dc_scale;
1022
2/2
✓ Branch 0 taken 10552869 times.
✓ Branch 1 taken 4753530 times.
15306399 int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode :
1023 lc->tu.intra_pred_mode_c;
1024
1025 15306399 memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1026
1027 // Derive QP for dequant
1028
2/2
✓ Branch 0 taken 14808887 times.
✓ Branch 1 taken 497512 times.
15306399 if (!lc->cu.cu_transquant_bypass_flag) {
1029 static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1030 static const uint8_t rem6[51 + 4 * 6 + 1] = {
1031 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1032 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1033 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1034 4, 5, 0, 1, 2, 3, 4, 5, 0, 1
1035 };
1036
1037 static const uint8_t div6[51 + 4 * 6 + 1] = {
1038 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1039 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1040 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1041 10, 10, 11, 11, 11, 11, 11, 11, 12, 12
1042 };
1043 14808887 int qp_y = lc->qp_y;
1044
1045
2/2
✓ Branch 0 taken 12075138 times.
✓ Branch 1 taken 2733749 times.
14808887 if (pps->transform_skip_enabled_flag &&
1046
2/2
✓ Branch 0 taken 6809164 times.
✓ Branch 1 taken 5265974 times.
12075138 log2_trafo_size <= pps->log2_max_transform_skip_block_size) {
1047 6809164 transform_skip_flag = hevc_transform_skip_flag_decode(lc, c_idx);
1048 }
1049
1050
2/2
✓ Branch 0 taken 10242454 times.
✓ Branch 1 taken 4566433 times.
14808887 if (c_idx == 0) {
1051 10242454 qp = qp_y + sps->qp_bd_offset;
1052 } else {
1053 int qp_i, offset;
1054
1055
2/2
✓ Branch 0 taken 2166915 times.
✓ Branch 1 taken 2399518 times.
4566433 if (c_idx == 1)
1056 2166915 offset = pps->cb_qp_offset + s->sh.slice_cb_qp_offset +
1057 2166915 lc->tu.cu_qp_offset_cb;
1058 else
1059 2399518 offset = pps->cr_qp_offset + s->sh.slice_cr_qp_offset +
1060 2399518 lc->tu.cu_qp_offset_cr;
1061
1062 4566433 qp_i = av_clip(qp_y + offset, - sps->qp_bd_offset, 57);
1063
2/2
✓ Branch 0 taken 2700328 times.
✓ Branch 1 taken 1866105 times.
4566433 if (sps->chroma_format_idc == 1) {
1064
2/2
✓ Branch 0 taken 877590 times.
✓ Branch 1 taken 1822738 times.
2700328 if (qp_i < 30)
1065 877590 qp = qp_i;
1066
2/2
✓ Branch 0 taken 13523 times.
✓ Branch 1 taken 1809215 times.
1822738 else if (qp_i > 43)
1067 13523 qp = qp_i - 6;
1068 else
1069 1809215 qp = qp_c[qp_i - 30];
1070 } else {
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1866105 times.
1866105 if (qp_i > 51)
1072 qp = 51;
1073 else
1074 1866105 qp = qp_i;
1075 }
1076
1077 4566433 qp += sps->qp_bd_offset;
1078 }
1079
1080 14808887 shift = sps->bit_depth + log2_trafo_size - 5;
1081 14808887 add = 1 << (shift-1);
1082 14808887 scale = level_scale[rem6[qp]] << (div6[qp]);
1083 14808887 scale_m = 16; // default when no custom scaling lists.
1084 14808887 dc_scale = 16;
1085
1086
6/6
✓ Branch 0 taken 676908 times.
✓ Branch 1 taken 14131979 times.
✓ Branch 2 taken 26526 times.
✓ Branch 3 taken 650382 times.
✓ Branch 4 taken 14292 times.
✓ Branch 5 taken 12234 times.
14808887 if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) {
1087 1329348 const ScalingList *sl = pps->scaling_list_data_present_flag ?
1088
2/2
✓ Branch 0 taken 357638 times.
✓ Branch 1 taken 307036 times.
664674 &pps->scaling_list : &sps->scaling_list;
1089 664674 int matrix_id = lc->cu.pred_mode != MODE_INTRA;
1090
1091 664674 matrix_id = 3 * matrix_id + c_idx;
1092
1093 664674 scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
1094
2/2
✓ Branch 0 taken 111752 times.
✓ Branch 1 taken 552922 times.
664674 if (log2_trafo_size >= 4)
1095 111752 dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
1096 }
1097 } else {
1098 497512 shift = 0;
1099 497512 add = 0;
1100 497512 scale = 0;
1101 497512 dc_scale = 0;
1102 }
1103
1104
6/6
✓ Branch 0 taken 4984083 times.
✓ Branch 1 taken 10322316 times.
✓ Branch 2 taken 225511 times.
✓ Branch 3 taken 4758572 times.
✓ Branch 4 taken 190277 times.
✓ Branch 5 taken 35234 times.
15306399 if (lc->cu.pred_mode == MODE_INTER && sps->explicit_rdpcm_enabled &&
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190277 times.
190277 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1106 35234 explicit_rdpcm_flag = explicit_rdpcm_flag_decode(lc, c_idx);
1107
2/2
✓ Branch 0 taken 3090 times.
✓ Branch 1 taken 32144 times.
35234 if (explicit_rdpcm_flag) {
1108 3090 explicit_rdpcm_dir_flag = explicit_rdpcm_dir_flag_decode(lc, c_idx);
1109 }
1110 }
1111
1112 15306399 last_significant_coeff_xy_prefix_decode(lc, c_idx, log2_trafo_size,
1113 &last_significant_coeff_x, &last_significant_coeff_y);
1114
1115
2/2
✓ Branch 0 taken 1895014 times.
✓ Branch 1 taken 13411385 times.
15306399 if (last_significant_coeff_x > 3) {
1116 1895014 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x);
1117 1895014 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1118 1895014 (2 + (last_significant_coeff_x & 1)) +
1119 suffix;
1120 }
1121
1122
2/2
✓ Branch 0 taken 1342188 times.
✓ Branch 1 taken 13964211 times.
15306399 if (last_significant_coeff_y > 3) {
1123 1342188 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y);
1124 1342188 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1125 1342188 (2 + (last_significant_coeff_y & 1)) +
1126 suffix;
1127 }
1128
1129
2/2
✓ Branch 0 taken 1934389 times.
✓ Branch 1 taken 13372010 times.
15306399 if (scan_idx == SCAN_VERT)
1130 1934389 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1131
1132 15306399 x_cg_last_sig = last_significant_coeff_x >> 2;
1133 15306399 y_cg_last_sig = last_significant_coeff_y >> 2;
1134
1135
3/3
✓ Branch 0 taken 11177931 times.
✓ Branch 1 taken 2194079 times.
✓ Branch 2 taken 1934389 times.
15306399 switch (scan_idx) {
1136 11177931 case SCAN_DIAG: {
1137 11177931 int last_x_c = last_significant_coeff_x & 3;
1138 11177931 int last_y_c = last_significant_coeff_y & 3;
1139
1140 11177931 scan_x_off = ff_hevc_diag_scan4x4_x;
1141 11177931 scan_y_off = ff_hevc_diag_scan4x4_y;
1142 11177931 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1143
2/2
✓ Branch 0 taken 5554419 times.
✓ Branch 1 taken 5623512 times.
11177931 if (trafo_size == 4) {
1144 5554419 scan_x_cg = scan_1x1;
1145 5554419 scan_y_cg = scan_1x1;
1146
2/2
✓ Branch 0 taken 2982066 times.
✓ Branch 1 taken 2641446 times.
5623512 } else if (trafo_size == 8) {
1147 2982066 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1148 2982066 scan_x_cg = diag_scan2x2_x;
1149 2982066 scan_y_cg = diag_scan2x2_y;
1150
2/2
✓ Branch 0 taken 1906497 times.
✓ Branch 1 taken 734949 times.
2641446 } else if (trafo_size == 16) {
1151 1906497 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1152 1906497 scan_x_cg = ff_hevc_diag_scan4x4_x;
1153 1906497 scan_y_cg = ff_hevc_diag_scan4x4_y;
1154 } else { // trafo_size == 32
1155 734949 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1156 734949 scan_x_cg = ff_hevc_diag_scan8x8_x;
1157 734949 scan_y_cg = ff_hevc_diag_scan8x8_y;
1158 }
1159 11177931 break;
1160 }
1161 2194079 case SCAN_HORIZ:
1162 2194079 scan_x_cg = horiz_scan2x2_x;
1163 2194079 scan_y_cg = horiz_scan2x2_y;
1164 2194079 scan_x_off = horiz_scan4x4_x;
1165 2194079 scan_y_off = horiz_scan4x4_y;
1166 2194079 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1167 2194079 break;
1168 1934389 default: //SCAN_VERT
1169 1934389 scan_x_cg = horiz_scan2x2_y;
1170 1934389 scan_y_cg = horiz_scan2x2_x;
1171 1934389 scan_x_off = horiz_scan4x4_y;
1172 1934389 scan_y_off = horiz_scan4x4_x;
1173 1934389 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1174 1934389 break;
1175 }
1176 15306399 num_coeff++;
1177 15306399 num_last_subset = (num_coeff - 1) >> 4;
1178
1179
2/2
✓ Branch 0 taken 26421626 times.
✓ Branch 1 taken 15306399 times.
41728025 for (i = num_last_subset; i >= 0; i--) {
1180 int n, m;
1181 int x_cg, y_cg, x_c, y_c, pos;
1182 26421626 int implicit_non_zero_coeff = 0;
1183 int64_t trans_coeff_level;
1184 26421626 int prev_sig = 0;
1185 26421626 int offset = i << 4;
1186 26421626 int rice_init = 0;
1187
1188 uint8_t significant_coeff_flag_idx[16];
1189 26421626 uint8_t nb_significant_coeff_flag = 0;
1190
1191 26421626 x_cg = scan_x_cg[i];
1192 26421626 y_cg = scan_y_cg[i];
1193
1194
4/4
✓ Branch 0 taken 11115227 times.
✓ Branch 1 taken 15306399 times.
✓ Branch 2 taken 8612523 times.
✓ Branch 3 taken 2502704 times.
26421626 if ((i < num_last_subset) && (i > 0)) {
1195 8612523 int ctx_cg = 0;
1196
2/2
✓ Branch 0 taken 7885237 times.
✓ Branch 1 taken 727286 times.
8612523 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1197 7885237 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1198
2/2
✓ Branch 0 taken 7284109 times.
✓ Branch 1 taken 1328414 times.
8612523 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1199 7284109 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1200
1201 8612523 significant_coeff_group_flag[x_cg][y_cg] =
1202 8612523 significant_coeff_group_flag_decode(lc, c_idx, ctx_cg);
1203 8612523 implicit_non_zero_coeff = 1;
1204 } else {
1205 17809103 significant_coeff_group_flag[x_cg][y_cg] =
1206
5/6
✓ Branch 0 taken 16025316 times.
✓ Branch 1 taken 1783787 times.
✓ Branch 2 taken 718917 times.
✓ Branch 3 taken 15306399 times.
✓ Branch 4 taken 2502704 times.
✗ Branch 5 not taken.
20311807 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1207
1/2
✓ Branch 0 taken 2502704 times.
✗ Branch 1 not taken.
2502704 (x_cg == 0 && y_cg == 0));
1208 }
1209
1210 26421626 last_scan_pos = num_coeff - offset - 1;
1211
1212
2/2
✓ Branch 0 taken 15306399 times.
✓ Branch 1 taken 11115227 times.
26421626 if (i == num_last_subset) {
1213 15306399 n_end = last_scan_pos - 1;
1214 15306399 significant_coeff_flag_idx[0] = last_scan_pos;
1215 15306399 nb_significant_coeff_flag = 1;
1216 } else {
1217 11115227 n_end = 15;
1218 }
1219
1220
2/2
✓ Branch 0 taken 15948770 times.
✓ Branch 1 taken 10472856 times.
26421626 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1221 15948770 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1222
2/2
✓ Branch 0 taken 15504983 times.
✓ Branch 1 taken 10916643 times.
26421626 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1223
2/2
✓ Branch 0 taken 5326807 times.
✓ Branch 1 taken 10178176 times.
15504983 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1224
1225
4/4
✓ Branch 0 taken 23132203 times.
✓ Branch 1 taken 3289423 times.
✓ Branch 2 taken 19561096 times.
✓ Branch 3 taken 3571107 times.
26421626 if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) {
1226 static const uint8_t ctx_idx_map[] = {
1227 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8, // log2_trafo_size == 2
1228 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0
1229 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 1
1230 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, // prev_sig == 2
1231 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // default
1232 };
1233 const uint8_t *ctx_idx_map_p;
1234 19561096 int scf_offset = 0;
1235
4/4
✓ Branch 0 taken 1017071 times.
✓ Branch 1 taken 18544025 times.
✓ Branch 2 taken 886760 times.
✓ Branch 3 taken 130311 times.
19561096 if (sps->transform_skip_context_enabled &&
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 886760 times.
886760 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1237 130311 ctx_idx_map_p = &ctx_idx_map[4 * 16];
1238
2/2
✓ Branch 0 taken 61311 times.
✓ Branch 1 taken 69000 times.
130311 if (c_idx == 0) {
1239 61311 scf_offset = 40;
1240 } else {
1241 69000 scf_offset = 14 + 27;
1242 }
1243 } else {
1244
2/2
✓ Branch 0 taken 4569150 times.
✓ Branch 1 taken 14861635 times.
19430785 if (c_idx != 0)
1245 4569150 scf_offset = 27;
1246
2/2
✓ Branch 0 taken 6708316 times.
✓ Branch 1 taken 12722469 times.
19430785 if (log2_trafo_size == 2) {
1247 6708316 ctx_idx_map_p = &ctx_idx_map[0];
1248 } else {
1249 12722469 ctx_idx_map_p = &ctx_idx_map[(prev_sig + 1) << 4];
1250
2/2
✓ Branch 0 taken 10265028 times.
✓ Branch 1 taken 2457441 times.
12722469 if (c_idx == 0) {
1251
4/4
✓ Branch 0 taken 6292631 times.
✓ Branch 1 taken 3972397 times.
✓ Branch 2 taken 2119151 times.
✓ Branch 3 taken 4173480 times.
10265028 if ((x_cg > 0 || y_cg > 0))
1252 6091548 scf_offset += 3;
1253
2/2
✓ Branch 0 taken 3827194 times.
✓ Branch 1 taken 6437834 times.
10265028 if (log2_trafo_size == 3) {
1254
2/2
✓ Branch 0 taken 2640711 times.
✓ Branch 1 taken 1186483 times.
3827194 scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15;
1255 } else {
1256 6437834 scf_offset += 21;
1257 }
1258 } else {
1259
2/2
✓ Branch 0 taken 1174487 times.
✓ Branch 1 taken 1282954 times.
2457441 if (log2_trafo_size == 3)
1260 1174487 scf_offset += 9;
1261 else
1262 1282954 scf_offset += 12;
1263 }
1264 }
1265 }
1266
2/2
✓ Branch 0 taken 177917390 times.
✓ Branch 1 taken 19561096 times.
197478486 for (n = n_end; n > 0; n--) {
1267 177917390 x_c = scan_x_off[n];
1268 177917390 y_c = scan_y_off[n];
1269
2/2
✓ Branch 1 taken 85720245 times.
✓ Branch 2 taken 92197145 times.
177917390 if (significant_coeff_flag_decode(lc, x_c, y_c, scf_offset, ctx_idx_map_p)) {
1270 85720245 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1271 85720245 nb_significant_coeff_flag++;
1272 85720245 implicit_non_zero_coeff = 0;
1273 }
1274 }
1275
2/2
✓ Branch 0 taken 19486073 times.
✓ Branch 1 taken 75023 times.
19561096 if (implicit_non_zero_coeff == 0) {
1276
4/4
✓ Branch 0 taken 1014653 times.
✓ Branch 1 taken 18471420 times.
✓ Branch 2 taken 884736 times.
✓ Branch 3 taken 129917 times.
19486073 if (sps->transform_skip_context_enabled &&
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 884736 times.
884736 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1278
2/2
✓ Branch 0 taken 61056 times.
✓ Branch 1 taken 68861 times.
129917 if (c_idx == 0) {
1279 61056 scf_offset = 42;
1280 } else {
1281 68861 scf_offset = 16 + 27;
1282 }
1283 } else {
1284
2/2
✓ Branch 0 taken 12001723 times.
✓ Branch 1 taken 7354433 times.
19356156 if (i == 0) {
1285
2/2
✓ Branch 0 taken 8770087 times.
✓ Branch 1 taken 3231636 times.
12001723 if (c_idx == 0)
1286 8770087 scf_offset = 0;
1287 else
1288 3231636 scf_offset = 27;
1289 } else {
1290 7354433 scf_offset = 2 + scf_offset;
1291 }
1292 }
1293
2/2
✓ Branch 1 taken 13176181 times.
✓ Branch 2 taken 6309892 times.
19486073 if (significant_coeff_flag_decode_0(lc, c_idx, scf_offset) == 1) {
1294 13176181 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1295 13176181 nb_significant_coeff_flag++;
1296 }
1297 } else {
1298 75023 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1299 75023 nb_significant_coeff_flag++;
1300 }
1301 }
1302
1303 26421626 n_end = nb_significant_coeff_flag;
1304
1305
1306
2/2
✓ Branch 0 taken 23042623 times.
✓ Branch 1 taken 3379003 times.
26421626 if (n_end) {
1307 int first_nz_pos_in_cg;
1308 int last_nz_pos_in_cg;
1309 23042623 int c_rice_param = 0;
1310 23042623 int first_greater1_coeff_idx = -1;
1311 uint8_t coeff_abs_level_greater1_flag[8];
1312 uint16_t coeff_sign_flag;
1313 23042623 int sum_abs = 0;
1314 int sign_hidden;
1315 int sb_type;
1316
1317
1318 // initialize first elem of coeff_bas_level_greater1_flag
1319
4/4
✓ Branch 0 taken 7825804 times.
✓ Branch 1 taken 15216819 times.
✓ Branch 2 taken 6396543 times.
✓ Branch 3 taken 1429261 times.
23042623 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1320
1321
2/2
✓ Branch 0 taken 1166629 times.
✓ Branch 1 taken 21875994 times.
23042623 if (sps->persistent_rice_adaptation_enabled) {
1322
3/4
✓ Branch 0 taken 1047040 times.
✓ Branch 1 taken 119589 times.
✓ Branch 2 taken 1047040 times.
✗ Branch 3 not taken.
1166629 if (!transform_skip_flag && !lc->cu.cu_transquant_bypass_flag)
1323
2/2
✓ Branch 0 taken 365016 times.
✓ Branch 1 taken 682024 times.
1047040 sb_type = 2 * (c_idx == 0 ? 1 : 0);
1324 else
1325
2/2
✓ Branch 0 taken 56576 times.
✓ Branch 1 taken 63013 times.
119589 sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1;
1326 1166629 c_rice_param = lc->stat_coeff[sb_type] / 4;
1327 }
1328
1329
4/4
✓ Branch 0 taken 7736224 times.
✓ Branch 1 taken 15306399 times.
✓ Branch 2 taken 3426781 times.
✓ Branch 3 taken 4309443 times.
23042623 if (!(i == num_last_subset) && greater1_ctx == 0)
1330 3426781 ctx_set++;
1331 23042623 greater1_ctx = 1;
1332 23042623 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1333
1334
2/2
✓ Branch 0 taken 95638921 times.
✓ Branch 1 taken 23042623 times.
118681544 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1335 95638921 int inc = (ctx_set << 2) + greater1_ctx;
1336 95638921 coeff_abs_level_greater1_flag[m] =
1337 95638921 coeff_abs_level_greater1_flag_decode(lc, c_idx, inc);
1338
2/2
✓ Branch 0 taken 31462343 times.
✓ Branch 1 taken 64176578 times.
95638921 if (coeff_abs_level_greater1_flag[m]) {
1339 31462343 greater1_ctx = 0;
1340
2/2
✓ Branch 0 taken 10708960 times.
✓ Branch 1 taken 20753383 times.
31462343 if (first_greater1_coeff_idx == -1)
1341 10708960 first_greater1_coeff_idx = m;
1342
4/4
✓ Branch 0 taken 50905149 times.
✓ Branch 1 taken 13271429 times.
✓ Branch 2 taken 31574482 times.
✓ Branch 3 taken 19330667 times.
64176578 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1343 31574482 greater1_ctx++;
1344 }
1345 }
1346 23042623 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1347
1348
2/2
✓ Branch 0 taken 22246076 times.
✓ Branch 1 taken 796547 times.
23042623 if (lc->cu.cu_transquant_bypass_flag ||
1349
2/2
✓ Branch 0 taken 14426022 times.
✓ Branch 1 taken 7820054 times.
22246076 (lc->cu.pred_mode == MODE_INTRA &&
1350
6/6
✓ Branch 0 taken 902776 times.
✓ Branch 1 taken 13523246 times.
✓ Branch 2 taken 92431 times.
✓ Branch 3 taken 810345 times.
✓ Branch 4 taken 47376 times.
✓ Branch 5 taken 45055 times.
14426022 sps->implicit_rdpcm_enabled && transform_skip_flag &&
1351
4/4
✓ Branch 0 taken 28377 times.
✓ Branch 1 taken 18999 times.
✓ Branch 2 taken 5999 times.
✓ Branch 3 taken 22176023 times.
22201021 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1352 explicit_rdpcm_flag)
1353 866600 sign_hidden = 0;
1354 else
1355 22176023 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1356
1357
2/2
✓ Branch 0 taken 10708960 times.
✓ Branch 1 taken 12333663 times.
23042623 if (first_greater1_coeff_idx != -1) {
1358 10708960 coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(lc, c_idx, ctx_set);
1359 }
1360
4/4
✓ Branch 0 taken 20349574 times.
✓ Branch 1 taken 2693049 times.
✓ Branch 2 taken 9107301 times.
✓ Branch 3 taken 11242273 times.
23042623 if (!pps->sign_data_hiding_flag || !sign_hidden ) {
1361 11800350 coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1362 } else {
1363 11242273 coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1364 }
1365
1366
2/2
✓ Branch 0 taken 114277848 times.
✓ Branch 1 taken 23042623 times.
137320471 for (m = 0; m < n_end; m++) {
1367 114277848 n = significant_coeff_flag_idx[m];
1368 114277848 GET_COORD(offset, n);
1369
2/2
✓ Branch 0 taken 95638921 times.
✓ Branch 1 taken 18638927 times.
114277848 if (m < 8) {
1370 95638921 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1371
4/4
✓ Branch 0 taken 10708960 times.
✓ Branch 1 taken 84929961 times.
✓ Branch 2 taken 24133862 times.
✓ Branch 3 taken 71505059 times.
95638921 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1372 24133862 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1373
1374 24133862 trans_coeff_level += last_coeff_abs_level_remaining;
1375
2/2
✓ Branch 0 taken 6268528 times.
✓ Branch 1 taken 17865334 times.
24133862 if (trans_coeff_level > (3 << c_rice_param))
1376
2/2
✓ Branch 0 taken 197734 times.
✓ Branch 1 taken 6070794 times.
6268528 c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1377
4/4
✓ Branch 0 taken 1114125 times.
✓ Branch 1 taken 23019737 times.
✓ Branch 2 taken 315677 times.
✓ Branch 3 taken 798448 times.
24133862 if (sps->persistent_rice_adaptation_enabled && !rice_init) {
1378 315677 int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1379
2/2
✓ Branch 0 taken 37785 times.
✓ Branch 1 taken 277892 times.
315677 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1380 37785 lc->stat_coeff[sb_type]++;
1381
2/2
✓ Branch 0 taken 157067 times.
✓ Branch 1 taken 120825 times.
277892 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1382
2/2
✓ Branch 0 taken 37744 times.
✓ Branch 1 taken 119323 times.
157067 if (lc->stat_coeff[sb_type] > 0)
1383 37744 lc->stat_coeff[sb_type]--;
1384 315677 rice_init = 1;
1385 }
1386 }
1387 } else {
1388 18638927 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1389
1390 18638927 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1391
2/2
✓ Branch 0 taken 2312658 times.
✓ Branch 1 taken 16326269 times.
18638927 if (trans_coeff_level > (3 << c_rice_param))
1392
2/2
✓ Branch 0 taken 89299 times.
✓ Branch 1 taken 2223359 times.
2312658 c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1393
4/4
✓ Branch 0 taken 938151 times.
✓ Branch 1 taken 17700776 times.
✓ Branch 2 taken 36551 times.
✓ Branch 3 taken 901600 times.
18638927 if (sps->persistent_rice_adaptation_enabled && !rice_init) {
1394 36551 int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1395
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 35207 times.
36551 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1396 1344 lc->stat_coeff[sb_type]++;
1397
2/2
✓ Branch 0 taken 24604 times.
✓ Branch 1 taken 10603 times.
35207 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1398
2/2
✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 23297 times.
24604 if (lc->stat_coeff[sb_type] > 0)
1399 1307 lc->stat_coeff[sb_type]--;
1400 36551 rice_init = 1;
1401 }
1402 }
1403
4/4
✓ Branch 0 taken 105177944 times.
✓ Branch 1 taken 9099904 times.
✓ Branch 2 taken 81071301 times.
✓ Branch 3 taken 24106643 times.
114277848 if (pps->sign_data_hiding_flag && sign_hidden) {
1404 81071301 sum_abs += trans_coeff_level;
1405
4/4
✓ Branch 0 taken 11242273 times.
✓ Branch 1 taken 69829028 times.
✓ Branch 2 taken 5609814 times.
✓ Branch 3 taken 5632459 times.
81071301 if (n == first_nz_pos_in_cg && (sum_abs&1))
1406 5609814 trans_coeff_level = -trans_coeff_level;
1407 }
1408
2/2
✓ Branch 0 taken 52062460 times.
✓ Branch 1 taken 62215388 times.
114277848 if (coeff_sign_flag >> 15)
1409 52062460 trans_coeff_level = -trans_coeff_level;
1410 114277848 coeff_sign_flag <<= 1;
1411
2/2
✓ Branch 0 taken 104464496 times.
✓ Branch 1 taken 9813352 times.
114277848 if(!lc->cu.cu_transquant_bypass_flag) {
1412
6/6
✓ Branch 0 taken 3590462 times.
✓ Branch 1 taken 100874034 times.
✓ Branch 2 taken 190401 times.
✓ Branch 3 taken 3400061 times.
✓ Branch 4 taken 30375 times.
✓ Branch 5 taken 160026 times.
104464496 if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) {
1413
6/6
✓ Branch 0 taken 1101785 times.
✓ Branch 1 taken 2328651 times.
✓ Branch 2 taken 497066 times.
✓ Branch 3 taken 604719 times.
✓ Branch 4 taken 414177 times.
✓ Branch 5 taken 82889 times.
3430436 if(y_c || x_c || log2_trafo_size < 4) {
1414
4/4
✓ Branch 0 taken 803764 times.
✓ Branch 1 taken 958920 times.
✓ Branch 2 taken 425429 times.
✓ Branch 3 taken 1159434 times.
3347547 switch(log2_trafo_size) {
1415 803764 case 3: pos = (y_c << 3) + x_c; break;
1416 958920 case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1417 425429 case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1418 1159434 default: pos = (y_c << 2) + x_c; break;
1419 }
1420 3347547 scale_m = scale_matrix[pos];
1421 } else {
1422 82889 scale_m = dc_scale;
1423 }
1424 }
1425 104464496 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1426
2/2
✓ Branch 0 taken 52734646 times.
✓ Branch 1 taken 51729850 times.
104464496 if(trans_coeff_level < 0) {
1427
2/2
✓ Branch 0 taken 162681 times.
✓ Branch 1 taken 52571965 times.
52734646 if((~trans_coeff_level) & 0xFffffffffff8000)
1428 162681 trans_coeff_level = -32768;
1429 } else {
1430
2/2
✓ Branch 0 taken 162295 times.
✓ Branch 1 taken 51567555 times.
51729850 if(trans_coeff_level & 0xffffffffffff8000)
1431 162295 trans_coeff_level = 32767;
1432 }
1433 }
1434 114277848 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1435 }
1436 }
1437 }
1438
1439
2/2
✓ Branch 0 taken 497512 times.
✓ Branch 1 taken 14808887 times.
15306399 if (lc->cu.cu_transquant_bypass_flag) {
1440
2/6
✓ Branch 0 taken 497512 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 497512 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
497512 if (explicit_rdpcm_flag || (sps->implicit_rdpcm_enabled &&
1441 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1442 int mode = sps->implicit_rdpcm_enabled ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag;
1443
1444 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1445 }
1446 } else {
1447
2/2
✓ Branch 0 taken 440018 times.
✓ Branch 1 taken 14368869 times.
14808887 if (transform_skip_flag) {
1448
2/2
✓ Branch 0 taken 54609 times.
✓ Branch 1 taken 21520 times.
76129 int rot = sps->transform_skip_rotation_enabled &&
1449
2/2
✓ Branch 0 taken 76129 times.
✓ Branch 1 taken 363889 times.
516147 log2_trafo_size == 2 &&
1450
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 33783 times.
54609 lc->cu.pred_mode == MODE_INTRA;
1451
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 419192 times.
440018 if (rot) {
1452
2/2
✓ Branch 0 taken 166608 times.
✓ Branch 1 taken 20826 times.
187434 for (i = 0; i < 8; i++)
1453 166608 FFSWAP(int16_t, coeffs[i], coeffs[16 - i - 1]);
1454 }
1455
1456 440018 s->hevcdsp.dequant(coeffs, log2_trafo_size);
1457
1458
4/4
✓ Branch 0 taken 436928 times.
✓ Branch 1 taken 3090 times.
✓ Branch 2 taken 73039 times.
✓ Branch 3 taken 363889 times.
440018 if (explicit_rdpcm_flag || (sps->implicit_rdpcm_enabled &&
1459
4/4
✓ Branch 0 taken 40895 times.
✓ Branch 1 taken 32144 times.
✓ Branch 2 taken 25185 times.
✓ Branch 3 taken 15710 times.
73039 lc->cu.pred_mode == MODE_INTRA &&
1460
2/2
✓ Branch 0 taken 8263 times.
✓ Branch 1 taken 16922 times.
25185 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1461
2/2
✓ Branch 0 taken 23973 times.
✓ Branch 1 taken 3090 times.
27063 int mode = explicit_rdpcm_flag ? explicit_rdpcm_dir_flag : (pred_mode_intra == 26);
1462
1463 27063 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1464 }
1465
6/6
✓ Branch 0 taken 9780756 times.
✓ Branch 1 taken 4588113 times.
✓ Branch 2 taken 6651106 times.
✓ Branch 3 taken 3129650 times.
✓ Branch 4 taken 3829756 times.
✓ Branch 5 taken 2821350 times.
14368869 } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) {
1466 3829756 s->hevcdsp.transform_4x4_luma(coeffs);
1467 } else {
1468 10539113 int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1469
2/2
✓ Branch 0 taken 2467349 times.
✓ Branch 1 taken 8071764 times.
10539113 if (max_xy == 0)
1470 2467349 s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1471 else {
1472 8071764 int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1473
2/2
✓ Branch 0 taken 5630959 times.
✓ Branch 1 taken 2440805 times.
8071764 if (max_xy < 4)
1474 5630959 col_limit = FFMIN(4, col_limit);
1475
2/2
✓ Branch 0 taken 1826056 times.
✓ Branch 1 taken 614749 times.
2440805 else if (max_xy < 8)
1476 1826056 col_limit = FFMIN(8, col_limit);
1477
2/2
✓ Branch 0 taken 264702 times.
✓ Branch 1 taken 350047 times.
614749 else if (max_xy < 12)
1478 264702 col_limit = FFMIN(24, col_limit);
1479 8071764 s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1480 }
1481 }
1482 }
1483
2/2
✓ Branch 0 taken 269268 times.
✓ Branch 1 taken 15037131 times.
15306399 if (lc->tu.cross_pf) {
1484 269268 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1485
1486
2/2
✓ Branch 0 taken 20252880 times.
✓ Branch 1 taken 269268 times.
20522148 for (i = 0; i < (trafo_size * trafo_size); i++) {
1487 20252880 coeffs[i] = coeffs[i] + ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1488 }
1489 }
1490 15306399 s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride);
1491 15306399 }
1492
1493 2800853 void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
1494 {
1495 2800853 int x = abs_mvd_greater0_flag_decode(lc);
1496 2800853 int y = abs_mvd_greater0_flag_decode(lc);
1497
1498
2/2
✓ Branch 0 taken 1966431 times.
✓ Branch 1 taken 834422 times.
2800853 if (x)
1499 1966431 x += abs_mvd_greater1_flag_decode(lc);
1500
2/2
✓ Branch 0 taken 1884271 times.
✓ Branch 1 taken 916582 times.
2800853 if (y)
1501 1884271 y += abs_mvd_greater1_flag_decode(lc);
1502
1503
3/4
✓ Branch 0 taken 1428271 times.
✓ Branch 1 taken 538160 times.
✓ Branch 2 taken 834422 times.
✗ Branch 3 not taken.
2800853 switch (x) {
1504 1428271 case 2: lc->pu.mvd.x = mvd_decode(lc); break;
1505 538160 case 1: lc->pu.mvd.x = mvd_sign_flag_decode(lc); break;
1506 834422 case 0: lc->pu.mvd.x = 0; break;
1507 }
1508
1509
3/4
✓ Branch 0 taken 1334410 times.
✓ Branch 1 taken 549861 times.
✓ Branch 2 taken 916582 times.
✗ Branch 3 not taken.
2800853 switch (y) {
1510 1334410 case 2: lc->pu.mvd.y = mvd_decode(lc); break;
1511 549861 case 1: lc->pu.mvd.y = mvd_sign_flag_decode(lc); break;
1512 916582 case 0: lc->pu.mvd.y = 0; break;
1513 }
1514 2800853 }
1515
1516