FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_cabac.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 649 670 96.9%
Functions: 55 56 98.2%
Branches: 489 523 93.5%

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 "hevc_data.h"
29 #include "hevc.h"
30 #include "hevcdec.h"
31
32 #define CABAC_MAX_BIN 31
33
34 /**
35 * number of bin by SyntaxElement.
36 */
37 static const int8_t num_bins_in_se[] = {
38 1, // sao_merge_flag
39 1, // sao_type_idx
40 0, // sao_eo_class
41 0, // sao_band_position
42 0, // sao_offset_abs
43 0, // sao_offset_sign
44 0, // end_of_slice_flag
45 3, // split_coding_unit_flag
46 1, // cu_transquant_bypass_flag
47 3, // skip_flag
48 3, // cu_qp_delta
49 1, // pred_mode
50 4, // part_mode
51 0, // pcm_flag
52 1, // prev_intra_luma_pred_mode
53 0, // mpm_idx
54 0, // rem_intra_luma_pred_mode
55 2, // intra_chroma_pred_mode
56 1, // merge_flag
57 1, // merge_idx
58 5, // inter_pred_idc
59 2, // ref_idx_l0
60 2, // ref_idx_l1
61 2, // abs_mvd_greater0_flag
62 2, // abs_mvd_greater1_flag
63 0, // abs_mvd_minus2
64 0, // mvd_sign_flag
65 1, // mvp_lx_flag
66 1, // no_residual_data_flag
67 3, // split_transform_flag
68 2, // cbf_luma
69 5, // cbf_cb, cbf_cr
70 2, // transform_skip_flag[][]
71 2, // explicit_rdpcm_flag[][]
72 2, // explicit_rdpcm_dir_flag[][]
73 18, // last_significant_coeff_x_prefix
74 18, // last_significant_coeff_y_prefix
75 0, // last_significant_coeff_x_suffix
76 0, // last_significant_coeff_y_suffix
77 4, // significant_coeff_group_flag
78 44, // significant_coeff_flag
79 24, // coeff_abs_level_greater1_flag
80 6, // coeff_abs_level_greater2_flag
81 0, // coeff_abs_level_remaining
82 0, // coeff_sign_flag
83 8, // log2_res_scale_abs
84 2, // res_scale_sign_flag
85 1, // cu_chroma_qp_offset_flag
86 1, // cu_chroma_qp_offset_idx
87 };
88
89 /**
90 * Offset to ctxIdx 0 in init_values and states, indexed by SyntaxElement.
91 */
92 static const int elem_offset[sizeof(num_bins_in_se)] = {
93 0, // sao_merge_flag
94 1, // sao_type_idx
95 2, // sao_eo_class
96 2, // sao_band_position
97 2, // sao_offset_abs
98 2, // sao_offset_sign
99 2, // end_of_slice_flag
100 2, // split_coding_unit_flag
101 5, // cu_transquant_bypass_flag
102 6, // skip_flag
103 9, // cu_qp_delta
104 12, // pred_mode
105 13, // part_mode
106 17, // pcm_flag
107 17, // prev_intra_luma_pred_mode
108 18, // mpm_idx
109 18, // rem_intra_luma_pred_mode
110 18, // intra_chroma_pred_mode
111 20, // merge_flag
112 21, // merge_idx
113 22, // inter_pred_idc
114 27, // ref_idx_l0
115 29, // ref_idx_l1
116 31, // abs_mvd_greater0_flag
117 33, // abs_mvd_greater1_flag
118 35, // abs_mvd_minus2
119 35, // mvd_sign_flag
120 35, // mvp_lx_flag
121 36, // no_residual_data_flag
122 37, // split_transform_flag
123 40, // cbf_luma
124 42, // cbf_cb, cbf_cr
125 47, // transform_skip_flag[][]
126 49, // explicit_rdpcm_flag[][]
127 51, // explicit_rdpcm_dir_flag[][]
128 53, // last_significant_coeff_x_prefix
129 71, // last_significant_coeff_y_prefix
130 89, // last_significant_coeff_x_suffix
131 89, // last_significant_coeff_y_suffix
132 89, // significant_coeff_group_flag
133 93, // significant_coeff_flag
134 137, // coeff_abs_level_greater1_flag
135 161, // coeff_abs_level_greater2_flag
136 167, // coeff_abs_level_remaining
137 167, // coeff_sign_flag
138 167, // log2_res_scale_abs
139 175, // res_scale_sign_flag
140 177, // cu_chroma_qp_offset_flag
141 178, // cu_chroma_qp_offset_idx
142 };
143
144 #define CNU 154
145 /**
146 * Indexed by init_type
147 */
148 static const uint8_t init_values[3][HEVC_CONTEXTS] = {
149 { // sao_merge_flag
150 153,
151 // sao_type_idx
152 200,
153 // split_coding_unit_flag
154 139, 141, 157,
155 // cu_transquant_bypass_flag
156 154,
157 // skip_flag
158 CNU, CNU, CNU,
159 // cu_qp_delta
160 154, 154, 154,
161 // pred_mode
162 CNU,
163 // part_mode
164 184, CNU, CNU, CNU,
165 // prev_intra_luma_pred_mode
166 184,
167 // intra_chroma_pred_mode
168 63, 139,
169 // merge_flag
170 CNU,
171 // merge_idx
172 CNU,
173 // inter_pred_idc
174 CNU, CNU, CNU, CNU, CNU,
175 // ref_idx_l0
176 CNU, CNU,
177 // ref_idx_l1
178 CNU, CNU,
179 // abs_mvd_greater1_flag
180 CNU, CNU,
181 // abs_mvd_greater1_flag
182 CNU, CNU,
183 // mvp_lx_flag
184 CNU,
185 // no_residual_data_flag
186 CNU,
187 // split_transform_flag
188 153, 138, 138,
189 // cbf_luma
190 111, 141,
191 // cbf_cb, cbf_cr
192 94, 138, 182, 154, 154,
193 // transform_skip_flag
194 139, 139,
195 // explicit_rdpcm_flag
196 139, 139,
197 // explicit_rdpcm_dir_flag
198 139, 139,
199 // last_significant_coeff_x_prefix
200 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
201 79, 108, 123, 63,
202 // last_significant_coeff_y_prefix
203 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
204 79, 108, 123, 63,
205 // significant_coeff_group_flag
206 91, 171, 134, 141,
207 // significant_coeff_flag
208 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
209 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
210 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
211 141, 111,
212 // coeff_abs_level_greater1_flag
213 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
214 122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
215 // coeff_abs_level_greater2_flag
216 138, 153, 136, 167, 152, 152,
217 // log2_res_scale_abs
218 154, 154, 154, 154, 154, 154, 154, 154,
219 // res_scale_sign_flag
220 154, 154,
221 // cu_chroma_qp_offset_flag
222 154,
223 // cu_chroma_qp_offset_idx
224 154,
225 },
226 { // sao_merge_flag
227 153,
228 // sao_type_idx
229 185,
230 // split_coding_unit_flag
231 107, 139, 126,
232 // cu_transquant_bypass_flag
233 154,
234 // skip_flag
235 197, 185, 201,
236 // cu_qp_delta
237 154, 154, 154,
238 // pred_mode
239 149,
240 // part_mode
241 154, 139, 154, 154,
242 // prev_intra_luma_pred_mode
243 154,
244 // intra_chroma_pred_mode
245 152, 139,
246 // merge_flag
247 110,
248 // merge_idx
249 122,
250 // inter_pred_idc
251 95, 79, 63, 31, 31,
252 // ref_idx_l0
253 153, 153,
254 // ref_idx_l1
255 153, 153,
256 // abs_mvd_greater1_flag
257 140, 198,
258 // abs_mvd_greater1_flag
259 140, 198,
260 // mvp_lx_flag
261 168,
262 // no_residual_data_flag
263 79,
264 // split_transform_flag
265 124, 138, 94,
266 // cbf_luma
267 153, 111,
268 // cbf_cb, cbf_cr
269 149, 107, 167, 154, 154,
270 // transform_skip_flag
271 139, 139,
272 // explicit_rdpcm_flag
273 139, 139,
274 // explicit_rdpcm_dir_flag
275 139, 139,
276 // last_significant_coeff_x_prefix
277 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
278 94, 108, 123, 108,
279 // last_significant_coeff_y_prefix
280 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
281 94, 108, 123, 108,
282 // significant_coeff_group_flag
283 121, 140, 61, 154,
284 // significant_coeff_flag
285 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
286 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
287 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
288 140, 140,
289 // coeff_abs_level_greater1_flag
290 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
291 136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
292 // coeff_abs_level_greater2_flag
293 107, 167, 91, 122, 107, 167,
294 // log2_res_scale_abs
295 154, 154, 154, 154, 154, 154, 154, 154,
296 // res_scale_sign_flag
297 154, 154,
298 // cu_chroma_qp_offset_flag
299 154,
300 // cu_chroma_qp_offset_idx
301 154,
302 },
303 { // sao_merge_flag
304 153,
305 // sao_type_idx
306 160,
307 // split_coding_unit_flag
308 107, 139, 126,
309 // cu_transquant_bypass_flag
310 154,
311 // skip_flag
312 197, 185, 201,
313 // cu_qp_delta
314 154, 154, 154,
315 // pred_mode
316 134,
317 // part_mode
318 154, 139, 154, 154,
319 // prev_intra_luma_pred_mode
320 183,
321 // intra_chroma_pred_mode
322 152, 139,
323 // merge_flag
324 154,
325 // merge_idx
326 137,
327 // inter_pred_idc
328 95, 79, 63, 31, 31,
329 // ref_idx_l0
330 153, 153,
331 // ref_idx_l1
332 153, 153,
333 // abs_mvd_greater1_flag
334 169, 198,
335 // abs_mvd_greater1_flag
336 169, 198,
337 // mvp_lx_flag
338 168,
339 // no_residual_data_flag
340 79,
341 // split_transform_flag
342 224, 167, 122,
343 // cbf_luma
344 153, 111,
345 // cbf_cb, cbf_cr
346 149, 92, 167, 154, 154,
347 // transform_skip_flag
348 139, 139,
349 // explicit_rdpcm_flag
350 139, 139,
351 // explicit_rdpcm_dir_flag
352 139, 139,
353 // last_significant_coeff_x_prefix
354 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
355 79, 108, 123, 93,
356 // last_significant_coeff_y_prefix
357 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
358 79, 108, 123, 93,
359 // significant_coeff_group_flag
360 121, 140, 61, 154,
361 // significant_coeff_flag
362 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
363 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
364 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
365 140, 140,
366 // coeff_abs_level_greater1_flag
367 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
368 136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
369 // coeff_abs_level_greater2_flag
370 107, 167, 91, 107, 107, 167,
371 // log2_res_scale_abs
372 154, 154, 154, 154, 154, 154, 154, 154,
373 // res_scale_sign_flag
374 154, 154,
375 // cu_chroma_qp_offset_flag
376 154,
377 // cu_chroma_qp_offset_idx
378 154,
379 },
380 };
381
382 static const uint8_t scan_1x1[1] = {
383 0,
384 };
385
386 static const uint8_t horiz_scan2x2_x[4] = {
387 0, 1, 0, 1,
388 };
389
390 static const uint8_t horiz_scan2x2_y[4] = {
391 0, 0, 1, 1
392 };
393
394 static const uint8_t horiz_scan4x4_x[16] = {
395 0, 1, 2, 3,
396 0, 1, 2, 3,
397 0, 1, 2, 3,
398 0, 1, 2, 3,
399 };
400
401 static const uint8_t horiz_scan4x4_y[16] = {
402 0, 0, 0, 0,
403 1, 1, 1, 1,
404 2, 2, 2, 2,
405 3, 3, 3, 3,
406 };
407
408 static const uint8_t horiz_scan8x8_inv[8][8] = {
409 { 0, 1, 2, 3, 16, 17, 18, 19, },
410 { 4, 5, 6, 7, 20, 21, 22, 23, },
411 { 8, 9, 10, 11, 24, 25, 26, 27, },
412 { 12, 13, 14, 15, 28, 29, 30, 31, },
413 { 32, 33, 34, 35, 48, 49, 50, 51, },
414 { 36, 37, 38, 39, 52, 53, 54, 55, },
415 { 40, 41, 42, 43, 56, 57, 58, 59, },
416 { 44, 45, 46, 47, 60, 61, 62, 63, },
417 };
418
419 static const uint8_t diag_scan2x2_x[4] = {
420 0, 0, 1, 1,
421 };
422
423 static const uint8_t diag_scan2x2_y[4] = {
424 0, 1, 0, 1,
425 };
426
427 static const uint8_t diag_scan2x2_inv[2][2] = {
428 { 0, 2, },
429 { 1, 3, },
430 };
431
432 static const uint8_t diag_scan4x4_inv[4][4] = {
433 { 0, 2, 5, 9, },
434 { 1, 4, 8, 12, },
435 { 3, 7, 11, 14, },
436 { 6, 10, 13, 15, },
437 };
438
439 static const uint8_t diag_scan8x8_inv[8][8] = {
440 { 0, 2, 5, 9, 14, 20, 27, 35, },
441 { 1, 4, 8, 13, 19, 26, 34, 42, },
442 { 3, 7, 12, 18, 25, 33, 41, 48, },
443 { 6, 11, 17, 24, 32, 40, 47, 53, },
444 { 10, 16, 23, 31, 39, 46, 52, 57, },
445 { 15, 22, 30, 38, 45, 51, 56, 60, },
446 { 21, 29, 37, 44, 50, 55, 59, 62, },
447 { 28, 36, 43, 49, 54, 58, 61, 63, },
448 };
449
450 1414295 void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts)
451 {
452 1414295 const HEVCContext *const s = lc->parent;
453
454
2/2
✓ Branch 0 taken 102424 times.
✓ Branch 1 taken 1311871 times.
1414295 if (s->ps.pps->entropy_coding_sync_enabled_flag &&
455
2/2
✓ Branch 0 taken 97674 times.
✓ Branch 1 taken 4750 times.
102424 (ctb_addr_ts % s->ps.sps->ctb_width == 2 ||
456
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 96890 times.
97674 (s->ps.sps->ctb_width == 2 &&
457
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 392 times.
784 ctb_addr_ts % s->ps.sps->ctb_width == 0))) {
458 5142 memcpy(lc->common_cabac_state->state, lc->cabac_state, HEVC_CONTEXTS);
459
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 5094 times.
5142 if (s->ps.sps->persistent_rice_adaptation_enabled_flag) {
460 48 memcpy(lc->common_cabac_state->stat_coeff, lc->stat_coeff, HEVC_STAT_COEFFS);
461 }
462 }
463 1414295 }
464
465 4127 static void load_states(HEVCLocalContext *lc, const HEVCContext *s)
466 {
467 4127 memcpy(lc->cabac_state, lc->common_cabac_state->state, HEVC_CONTEXTS);
468
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4083 times.
4127 if (s->ps.sps->persistent_rice_adaptation_enabled_flag) {
469 44 memcpy(lc->stat_coeff, lc->common_cabac_state->stat_coeff, HEVC_STAT_COEFFS);
470 }
471 4127 }
472
473 7792 static int cabac_reinit(HEVCLocalContext *lc)
474 {
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7792 times.
7792 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
476 }
477
478 27504 static int cabac_init_decoder(HEVCLocalContext *lc)
479 {
480 27504 GetBitContext *gb = &lc->gb;
481 27504 skip_bits(gb, 1);
482 27504 align_get_bits(gb);
483 82512 return ff_init_cabac_decoder(&lc->cc,
484 27504 gb->buffer + get_bits_count(gb) / 8,
485 27504 (get_bits_left(gb) + 7) / 8);
486 }
487
488 24785 static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
489 {
490 24785 int init_type = 2 - s->sh.slice_type;
491 int i;
492
493
4/4
✓ Branch 0 taken 6489 times.
✓ Branch 1 taken 18296 times.
✓ Branch 2 taken 6346 times.
✓ Branch 3 taken 143 times.
24785 if (s->sh.cabac_init_flag && s->sh.slice_type != HEVC_SLICE_I)
494 6346 init_type ^= 3;
495
496
2/2
✓ Branch 0 taken 4932215 times.
✓ Branch 1 taken 24785 times.
4957000 for (i = 0; i < HEVC_CONTEXTS; i++) {
497 4932215 int init_value = init_values[init_type][i];
498 4932215 int m = (init_value >> 4) * 5 - 45;
499 4932215 int n = ((init_value & 15) << 3) - 16;
500 4932215 int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
501
502 4932215 pre ^= pre >> 31;
503
2/2
✓ Branch 0 taken 506353 times.
✓ Branch 1 taken 4425862 times.
4932215 if (pre > 124)
504 506353 pre = 124 + (pre & 1);
505 4932215 lc->cabac_state[i] = pre;
506 }
507
508
2/2
✓ Branch 0 taken 99140 times.
✓ Branch 1 taken 24785 times.
123925 for (i = 0; i < 4; i++)
509 99140 lc->stat_coeff[i] = 0;
510 24785 }
511
512 1414296 int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts)
513 {
514 1414296 const HEVCContext *const s = lc->parent;
515
516
2/2
✓ Branch 0 taken 27504 times.
✓ Branch 1 taken 1386792 times.
1414296 if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
517 27504 int ret = cabac_init_decoder(lc);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27504 times.
27504 if (ret < 0)
519 return ret;
520
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 19557 times.
27504 if (s->sh.dependent_slice_segment_flag == 0 ||
521
2/2
✓ Branch 0 taken 876 times.
✓ Branch 1 taken 7071 times.
7947 (s->ps.pps->tiles_enabled_flag &&
522
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 567 times.
876 s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]))
523 19866 cabac_init_state(lc, s);
524
525
2/2
✓ Branch 0 taken 17989 times.
✓ Branch 1 taken 9515 times.
27504 if (!s->sh.first_slice_in_pic_flag &&
526
2/2
✓ Branch 0 taken 3853 times.
✓ Branch 1 taken 14136 times.
17989 s->ps.pps->entropy_coding_sync_enabled_flag) {
527
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 2217 times.
3853 if (ctb_addr_ts % s->ps.sps->ctb_width == 0) {
528
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1544 times.
1636 if (s->ps.sps->ctb_width == 1)
529 92 cabac_init_state(lc, s);
530
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 382 times.
1544 else if (s->sh.dependent_slice_segment_flag == 1)
531 1162 load_states(lc, s);
532 }
533 }
534 } else {
535
2/2
✓ Branch 0 taken 287199 times.
✓ Branch 1 taken 1099593 times.
1386792 if (s->ps.pps->tiles_enabled_flag &&
536
2/2
✓ Branch 0 taken 4625 times.
✓ Branch 1 taken 282574 times.
287199 s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
537 int ret;
538
1/2
✓ Branch 0 taken 4625 times.
✗ Branch 1 not taken.
4625 if (s->threads_number == 1)
539 4625 ret = cabac_reinit(lc);
540 else {
541 ret = cabac_init_decoder(lc);
542 }
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4625 times.
4625 if (ret < 0)
544 return ret;
545 4625 cabac_init_state(lc, s);
546 }
547
2/2
✓ Branch 0 taken 97840 times.
✓ Branch 1 taken 1288952 times.
1386792 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
548
2/2
✓ Branch 0 taken 3167 times.
✓ Branch 1 taken 94673 times.
97840 if (ctb_addr_ts % s->ps.sps->ctb_width == 0) {
549 int ret;
550 3167 get_cabac_terminate(&lc->cc);
551
1/2
✓ Branch 0 taken 3167 times.
✗ Branch 1 not taken.
3167 if (s->threads_number == 1)
552 3167 ret = cabac_reinit(lc);
553 else {
554 ret = cabac_init_decoder(lc);
555 }
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3167 times.
3167 if (ret < 0)
557 return ret;
558
559
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 2965 times.
3167 if (s->ps.sps->ctb_width == 1)
560 202 cabac_init_state(lc, s);
561 else
562 2965 load_states(lc, s);
563 }
564 }
565 }
566 1414296 return 0;
567 }
568
569 #define GET_CABAC(ctx) get_cabac(&lc->cc, &lc->cabac_state[ctx])
570
571 816622 int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc)
572 {
573 816622 return GET_CABAC(elem_offset[SAO_MERGE_FLAG]);
574 }
575
576 374343 int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
577 {
578
2/2
✓ Branch 1 taken 211593 times.
✓ Branch 2 taken 162750 times.
374343 if (!GET_CABAC(elem_offset[SAO_TYPE_IDX]))
579 211593 return 0;
580
581
2/2
✓ Branch 1 taken 53980 times.
✓ Branch 2 taken 108770 times.
162750 if (!get_cabac_bypass(&lc->cc))
582 53980 return SAO_BAND;
583 108770 return SAO_EDGE;
584 }
585
586 77956 int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc)
587 {
588 int i;
589 77956 int value = get_cabac_bypass(&lc->cc);
590
591
2/2
✓ Branch 0 taken 311824 times.
✓ Branch 1 taken 77956 times.
389780 for (i = 0; i < 4; i++)
592 311824 value = (value << 1) | get_cabac_bypass(&lc->cc);
593 77956 return value;
594 }
595
596 870776 int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc)
597 {
598 870776 int i = 0;
599 870776 int length = (1 << (FFMIN(lc->parent->ps.sps->bit_depth, 10) - 5)) - 1;
600
601
4/4
✓ Branch 0 taken 2460892 times.
✓ Branch 1 taken 127355 times.
✓ Branch 3 taken 1717471 times.
✓ Branch 4 taken 743421 times.
2588247 while (i < length && get_cabac_bypass(&lc->cc))
602 1717471 i++;
603 870776 return i;
604 }
605
606 243162 int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc)
607 {
608 243162 return get_cabac_bypass(&lc->cc);
609 }
610
611 108770 int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
612 {
613 108770 int ret = get_cabac_bypass(&lc->cc) << 1;
614 108770 ret |= get_cabac_bypass(&lc->cc);
615 108770 return ret;
616 }
617
618 1414295 int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc)
619 {
620 1414295 return get_cabac_terminate(&lc->cc);
621 }
622
623 200139 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCLocalContext *lc)
624 {
625 200139 return GET_CABAC(elem_offset[CU_TRANSQUANT_BYPASS_FLAG]);
626 }
627
628 8091541 int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, int x0, int y0, int x_cb, int y_cb)
629 {
630 8091541 const HEVCContext *const s = lc->parent;
631 8091541 int min_cb_width = s->ps.sps->min_cb_width;
632 8091541 int inc = 0;
633 8091541 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
634 8091541 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
635
636
4/4
✓ Branch 0 taken 736905 times.
✓ Branch 1 taken 7354636 times.
✓ Branch 2 taken 532201 times.
✓ Branch 3 taken 204704 times.
8091541 if (lc->ctb_left_flag || x0b)
637 7886837 inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
638
4/4
✓ Branch 0 taken 1679412 times.
✓ Branch 1 taken 6412129 times.
✓ Branch 2 taken 1222321 times.
✓ Branch 3 taken 457091 times.
8091541 if (lc->ctb_up_flag || y0b)
639 7634450 inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1);
640
641 8091541 return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
642 }
643
644 609973 int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc)
645 {
646 609973 int prefix_val = 0;
647 609973 int suffix_val = 0;
648 609973 int inc = 0;
649
650
4/4
✓ Branch 0 taken 1483159 times.
✓ Branch 1 taken 106104 times.
✓ Branch 3 taken 979290 times.
✓ Branch 4 taken 503869 times.
1589263 while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
651 979290 prefix_val++;
652 979290 inc = 1;
653 }
654
2/2
✓ Branch 0 taken 106104 times.
✓ Branch 1 taken 503869 times.
609973 if (prefix_val >= 5) {
655 106104 int k = 0;
656
3/4
✓ Branch 0 taken 360262 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 254158 times.
✓ Branch 4 taken 106104 times.
360262 while (k < 7 && get_cabac_bypass(&lc->cc)) {
657 254158 suffix_val += 1 << k;
658 254158 k++;
659 }
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106104 times.
106104 if (k == 7) {
661 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
662 return AVERROR_INVALIDDATA;
663 }
664
665
2/2
✓ Branch 0 taken 254158 times.
✓ Branch 1 taken 106104 times.
360262 while (k--)
666 254158 suffix_val += get_cabac_bypass(&lc->cc) << k;
667 }
668 609973 return prefix_val + suffix_val;
669 }
670
671 370572 int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc)
672 {
673 370572 return get_cabac_bypass(&lc->cc);
674 }
675
676 266275 int ff_hevc_cu_chroma_qp_offset_flag(HEVCLocalContext *lc)
677 {
678 266275 return GET_CABAC(elem_offset[CU_CHROMA_QP_OFFSET_FLAG]);
679 }
680
681 int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc)
682 {
683 int c_max= FFMAX(5, lc->parent->ps.pps->chroma_qp_offset_list_len_minus1);
684 int i = 0;
685
686 while (i < c_max && GET_CABAC(elem_offset[CU_CHROMA_QP_OFFSET_IDX]))
687 i++;
688
689 return i;
690 }
691
692 4199420 int ff_hevc_pred_mode_decode(HEVCLocalContext *lc)
693 {
694 4199420 return GET_CABAC(elem_offset[PRED_MODE_FLAG]);
695 }
696
697 7487571 int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, int ct_depth, int x0, int y0)
698 {
699 7487571 const HEVCContext *const s = lc->parent;
700 7487571 const HEVCSPS *const sps = s->ps.sps;
701 7487571 int inc = 0, depth_left = 0, depth_top = 0;
702 7487571 int x0b = av_mod_uintp2(x0, sps->log2_ctb_size);
703 7487571 int y0b = av_mod_uintp2(y0, sps->log2_ctb_size);
704 7487571 int x_cb = x0 >> sps->log2_min_cb_size;
705 7487571 int y_cb = y0 >> sps->log2_min_cb_size;
706
707
4/4
✓ Branch 0 taken 694983 times.
✓ Branch 1 taken 6792588 times.
✓ Branch 2 taken 396864 times.
✓ Branch 3 taken 298119 times.
7487571 if (lc->ctb_left_flag || x0b)
708 7189452 depth_left = s->tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1];
709
4/4
✓ Branch 0 taken 1491457 times.
✓ Branch 1 taken 5996114 times.
✓ Branch 2 taken 839611 times.
✓ Branch 3 taken 651846 times.
7487571 if (lc->ctb_up_flag || y0b)
710 6835725 depth_top = s->tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb];
711
712 7487571 inc += (depth_left > ct_depth);
713 7487571 inc += (depth_top > ct_depth);
714
715 7487571 return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc);
716 }
717
718 6169097 int ff_hevc_part_mode_decode(HEVCLocalContext *lc, int log2_cb_size)
719 {
720
2/2
✓ Branch 1 taken 3595028 times.
✓ Branch 2 taken 2574069 times.
6169097 if (GET_CABAC(elem_offset[PART_MODE])) // 1
721 3595028 return PART_2Nx2N;
722
2/2
✓ Branch 0 taken 1943519 times.
✓ Branch 1 taken 630550 times.
2574069 if (log2_cb_size == lc->parent->ps.sps->log2_min_cb_size) {
723
2/2
✓ Branch 0 taken 1194329 times.
✓ Branch 1 taken 749190 times.
1943519 if (lc->cu.pred_mode == MODE_INTRA) // 0
724 1194329 return PART_NxN;
725
2/2
✓ Branch 1 taken 308106 times.
✓ Branch 2 taken 441084 times.
749190 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
726 308106 return PART_2NxN;
727
2/2
✓ Branch 0 taken 223043 times.
✓ Branch 1 taken 218041 times.
441084 if (log2_cb_size == 3) // 00
728 223043 return PART_Nx2N;
729
2/2
✓ Branch 1 taken 112405 times.
✓ Branch 2 taken 105636 times.
218041 if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001
730 112405 return PART_Nx2N;
731 105636 return PART_NxN; // 000
732 }
733
734
2/2
✓ Branch 0 taken 65275 times.
✓ Branch 1 taken 565275 times.
630550 if (!lc->parent->ps.sps->amp_enabled_flag) {
735
2/2
✓ Branch 1 taken 29519 times.
✓ Branch 2 taken 35756 times.
65275 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
736 29519 return PART_2NxN;
737 35756 return PART_Nx2N;
738 }
739
740
2/2
✓ Branch 1 taken 227404 times.
✓ Branch 2 taken 337871 times.
565275 if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX
741
2/2
✓ Branch 1 taken 108414 times.
✓ Branch 2 taken 118990 times.
227404 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011
742 108414 return PART_2NxN;
743
2/2
✓ Branch 1 taken 55211 times.
✓ Branch 2 taken 63779 times.
118990 if (get_cabac_bypass(&lc->cc)) // 0101
744 55211 return PART_2NxnD;
745 63779 return PART_2NxnU; // 0100
746 }
747
748
2/2
✓ Branch 1 taken 164546 times.
✓ Branch 2 taken 173325 times.
337871 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001
749 164546 return PART_Nx2N;
750
2/2
✓ Branch 1 taken 80098 times.
✓ Branch 2 taken 93227 times.
173325 if (get_cabac_bypass(&lc->cc)) // 0001
751 80098 return PART_nRx2N;
752 93227 return PART_nLx2N; // 0000
753 }
754
755 153183 int ff_hevc_pcm_flag_decode(HEVCLocalContext *lc)
756 {
757 153183 return get_cabac_terminate(&lc->cc);
758 }
759
760 7543986 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc)
761 {
762 7543986 return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]);
763 }
764
765 4524034 int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc)
766 {
767 4524034 int i = 0;
768
4/4
✓ Branch 0 taken 6739049 times.
✓ Branch 1 taken 939398 times.
✓ Branch 3 taken 3154413 times.
✓ Branch 4 taken 3584636 times.
7678447 while (i < 2 && get_cabac_bypass(&lc->cc))
769 3154413 i++;
770 4524034 return i;
771 }
772
773 3019952 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc)
774 {
775 int i;
776 3019952 int value = get_cabac_bypass(&lc->cc);
777
778
2/2
✓ Branch 0 taken 12079808 times.
✓ Branch 1 taken 3019952 times.
15099760 for (i = 0; i < 4; i++)
779 12079808 value = (value << 1) | get_cabac_bypass(&lc->cc);
780 3019952 return value;
781 }
782
783 4062360 int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc)
784 {
785 int ret;
786
2/2
✓ Branch 1 taken 3032579 times.
✓ Branch 2 taken 1029781 times.
4062360 if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE]))
787 3032579 return 4;
788
789 1029781 ret = get_cabac_bypass(&lc->cc) << 1;
790 1029781 ret |= get_cabac_bypass(&lc->cc);
791 1029781 return ret;
792 }
793
794 6150690 int ff_hevc_merge_idx_decode(HEVCLocalContext *lc)
795 {
796 6150690 int i = GET_CABAC(elem_offset[MERGE_IDX]);
797
798
2/2
✓ Branch 0 taken 2508967 times.
✓ Branch 1 taken 3641723 times.
6150690 if (i != 0) {
799
4/4
✓ Branch 0 taken 3861778 times.
✓ Branch 1 taken 298278 times.
✓ Branch 3 taken 1651089 times.
✓ Branch 4 taken 2210689 times.
4160056 while (i < lc->parent->sh.max_num_merge_cand-1 && get_cabac_bypass(&lc->cc))
800 1651089 i++;
801 }
802 6150690 return i;
803 }
804
805 4718920 int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
806 {
807 4718920 return GET_CABAC(elem_offset[MERGE_FLAG]);
808 }
809
810 1811787 int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH)
811 {
812
2/2
✓ Branch 0 taken 239847 times.
✓ Branch 1 taken 1571940 times.
1811787 if (nPbW + nPbH == 12)
813 239847 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
814
2/2
✓ Branch 1 taken 404149 times.
✓ Branch 2 taken 1167791 times.
1571940 if (GET_CABAC(elem_offset[INTER_PRED_IDC] + lc->ct_depth))
815 404149 return PRED_BI;
816
817 1167791 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
818 }
819
820 2787647 int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
821 {
822 2787647 int i = 0;
823 2787647 int max = num_ref_idx_lx - 1;
824 2787647 int max_ctx = FFMIN(max, 2);
825
826
4/4
✓ Branch 0 taken 2779079 times.
✓ Branch 1 taken 950798 times.
✓ Branch 3 taken 942230 times.
✓ Branch 4 taken 1836849 times.
3729877 while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i))
827 942230 i++;
828
2/2
✓ Branch 0 taken 195868 times.
✓ Branch 1 taken 2591779 times.
2787647 if (i == 2) {
829
4/4
✓ Branch 0 taken 160433 times.
✓ Branch 1 taken 120844 times.
✓ Branch 3 taken 85409 times.
✓ Branch 4 taken 75024 times.
281277 while (i < max && get_cabac_bypass(&lc->cc))
830 85409 i++;
831 }
832
833 2787647 return i;
834 }
835
836 2787647 int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
837 {
838 2787647 return GET_CABAC(elem_offset[MVP_LX_FLAG]);
839 }
840
841 2351853 int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc)
842 {
843 2351853 return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]);
844 }
845
846 5259706 static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc)
847 {
848 5259706 return GET_CABAC(elem_offset[ABS_MVD_GREATER0_FLAG]);
849 }
850
851 3599183 static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc)
852 {
853 3599183 return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1);
854 }
855
856 2584028 static av_always_inline int mvd_decode(HEVCLocalContext *lc)
857 {
858 2584028 int ret = 2;
859 2584028 int k = 1;
860
861
3/4
✓ Branch 0 taken 6329966 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3745938 times.
✓ Branch 4 taken 2584028 times.
6329966 while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc)) {
862 3745938 ret += 1U << k;
863 3745938 k++;
864 }
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2584028 times.
2584028 if (k == CABAC_MAX_BIN) {
866 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
867 return 0;
868 }
869
2/2
✓ Branch 0 taken 6329966 times.
✓ Branch 1 taken 2584028 times.
8913994 while (k--)
870 6329966 ret += get_cabac_bypass(&lc->cc) << k;
871 2584028 return get_cabac_bypass_sign(&lc->cc, -ret);
872 }
873
874 1015155 static av_always_inline int mvd_sign_flag_decode(HEVCLocalContext *lc)
875 {
876 1015155 return get_cabac_bypass_sign(&lc->cc, -1);
877 }
878
879 6855308 int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
880 {
881 6855308 return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
882 }
883
884 15116915 int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth)
885 {
886 15116915 return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
887 }
888
889 14610550 int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth)
890 {
891 14610550 return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
892 }
893
894 6506260 static int hevc_transform_skip_flag_decode(HEVCLocalContext *lc, int c_idx)
895 {
896 6506260 return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx);
897 }
898
899 35234 static int explicit_rdpcm_flag_decode(HEVCLocalContext *lc, int c_idx)
900 {
901 35234 return GET_CABAC(elem_offset[EXPLICIT_RDPCM_FLAG] + !!c_idx);
902 }
903
904 3090 static int explicit_rdpcm_dir_flag_decode(HEVCLocalContext *lc, int c_idx)
905 {
906 3090 return GET_CABAC(elem_offset[EXPLICIT_RDPCM_DIR_FLAG] + !!c_idx);
907 }
908
909 384850 int ff_hevc_log2_res_scale_abs(HEVCLocalContext *lc, int idx)
910 {
911 384850 int i =0;
912
913
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(elem_offset[LOG2_RES_SCALE_ABS] + 4 * idx + i))
914 837742 i++;
915
916 384850 return i;
917 }
918
919 235843 int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx)
920 {
921 235843 return GET_CABAC(elem_offset[RES_SCALE_SIGN_FLAG] + idx);
922 }
923
924 14145585 static av_always_inline void last_significant_coeff_xy_prefix_decode(HEVCLocalContext *lc, int c_idx,
925 int log2_size, int *last_scx_prefix, int *last_scy_prefix)
926 {
927 14145585 int i = 0;
928 14145585 int max = (log2_size << 1) - 1;
929 int ctx_offset, ctx_shift;
930
931
2/2
✓ Branch 0 taken 9694148 times.
✓ Branch 1 taken 4451437 times.
14145585 if (!c_idx) {
932 9694148 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2);
933 9694148 ctx_shift = (log2_size + 1) >> 2;
934 } else {
935 4451437 ctx_offset = 15;
936 4451437 ctx_shift = log2_size - 2;
937 }
938
4/4
✓ Branch 0 taken 34622175 times.
✓ Branch 1 taken 2099209 times.
✓ Branch 2 taken 22575799 times.
✓ Branch 3 taken 12046376 times.
71343559 while (i < max &&
939 34622175 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_X_PREFIX] + (i >> ctx_shift) + ctx_offset))
940 22575799 i++;
941 14145585 *last_scx_prefix = i;
942
943 14145585 i = 0;
944
4/4
✓ Branch 0 taken 30020356 times.
✓ Branch 1 taken 1644005 times.
✓ Branch 2 taken 17518776 times.
✓ Branch 3 taken 12501580 times.
61684717 while (i < max &&
945 30020356 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_Y_PREFIX] + (i >> ctx_shift) + ctx_offset))
946 17518776 i++;
947 14145585 *last_scy_prefix = i;
948 14145585 }
949
950 3079371 static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc,
951 int last_significant_coeff_prefix)
952 {
953 int i;
954 3079371 int length = (last_significant_coeff_prefix >> 1) - 1;
955 3079371 int value = get_cabac_bypass(&lc->cc);
956
957
2/2
✓ Branch 0 taken 862135 times.
✓ Branch 1 taken 3079371 times.
3941506 for (i = 1; i < length; i++)
958 862135 value = (value << 1) | get_cabac_bypass(&lc->cc);
959 3079371 return value;
960 }
961
962 8265020 static av_always_inline int significant_coeff_group_flag_decode(HEVCLocalContext *lc, int c_idx, int ctx_cg)
963 {
964 int inc;
965
966
2/2
✓ Branch 0 taken 1336741 times.
✓ Branch 1 taken 6928279 times.
8265020 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
967
968 8265020 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
969 }
970 169348688 static av_always_inline int significant_coeff_flag_decode(HEVCLocalContext *lc, int x_c, int y_c,
971 int offset, const uint8_t *ctx_idx_map)
972 {
973 169348688 int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
974 169348688 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
975 }
976
977 18394335 static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset)
978 {
979 18394335 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + offset);
980 }
981
982 90442731 static av_always_inline int coeff_abs_level_greater1_flag_decode(HEVCLocalContext *lc, int c_idx, int inc)
983 {
984
985
2/2
✓ Branch 0 taken 21327645 times.
✓ Branch 1 taken 69115086 times.
90442731 if (c_idx > 0)
986 21327645 inc += 16;
987
988 90442731 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
989 }
990
991 10068953 static av_always_inline int coeff_abs_level_greater2_flag_decode(HEVCLocalContext *lc, int c_idx, int inc)
992 {
993
2/2
✓ Branch 0 taken 2130160 times.
✓ Branch 1 taken 7938793 times.
10068953 if (c_idx > 0)
994 2130160 inc += 4;
995
996 10068953 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
997 }
998
999 40707768 static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param)
1000 {
1001 40707768 int prefix = 0;
1002 40707768 int suffix = 0;
1003 int last_coeff_abs_level_remaining;
1004 int i;
1005
1006
3/4
✓ Branch 0 taken 89347342 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 48639574 times.
✓ Branch 4 taken 40707768 times.
89347342 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc))
1007 48639574 prefix++;
1008
1009
2/2
✓ Branch 0 taken 34775036 times.
✓ Branch 1 taken 5932732 times.
40707768 if (prefix < 3) {
1010
2/2
✓ Branch 0 taken 36987268 times.
✓ Branch 1 taken 34775036 times.
71762304 for (i = 0; i < rc_rice_param; i++)
1011 36987268 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1012 34775036 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1013 } else {
1014 5932732 int prefix_minus3 = prefix - 3;
1015
1016
2/4
✓ Branch 0 taken 5932732 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5932732 times.
5932732 if (prefix == CABAC_MAX_BIN || prefix_minus3 + rc_rice_param > 16 + 6) {
1017 av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
1018 return 0;
1019 }
1020
1021
2/2
✓ Branch 0 taken 19437994 times.
✓ Branch 1 taken 5932732 times.
25370726 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1022 19437994 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1023 5932732 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1024 5932732 << rc_rice_param) + suffix;
1025 }
1026 40707768 return last_coeff_abs_level_remaining;
1027 }
1028
1029 21577991 static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb)
1030 {
1031 int i;
1032 21577991 int ret = 0;
1033
1034
2/2
✓ Branch 0 taken 97410569 times.
✓ Branch 1 taken 21577991 times.
118988560 for (i = 0; i < nb; i++)
1035 97410569 ret = (ret << 1) | get_cabac_bypass(&lc->cc);
1036 21577991 return ret;
1037 }
1038
1039 14145585 void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, int x0, int y0,
1040 int log2_trafo_size, enum ScanType scan_idx,
1041 int c_idx)
1042 {
1043 #define GET_COORD(offset, n) \
1044 do { \
1045 x_c = (x_cg << 2) + scan_x_off[n]; \
1046 y_c = (y_cg << 2) + scan_y_off[n]; \
1047 } while (0)
1048 14145585 const HEVCContext *const s = lc->parent;
1049 14145585 int transform_skip_flag = 0;
1050
1051 int last_significant_coeff_x, last_significant_coeff_y;
1052 int last_scan_pos;
1053 int n_end;
1054 14145585 int num_coeff = 0;
1055 14145585 int greater1_ctx = 1;
1056
1057 int num_last_subset;
1058 int x_cg_last_sig, y_cg_last_sig;
1059
1060 const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1061
1062 14145585 ptrdiff_t stride = s->frame->linesize[c_idx];
1063 14145585 int hshift = s->ps.sps->hshift[c_idx];
1064 14145585 int vshift = s->ps.sps->vshift[c_idx];
1065 14145585 uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1066 14145585 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1067
2/2
✓ Branch 0 taken 4451437 times.
✓ Branch 1 taken 9694148 times.
14145585 int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer);
1068 14145585 uint8_t significant_coeff_group_flag[8][8] = {{0}};
1069 14145585 int explicit_rdpcm_flag = 0;
1070 int explicit_rdpcm_dir_flag;
1071
1072 14145585 int trafo_size = 1 << log2_trafo_size;
1073 int i;
1074 int qp,shift,add,scale,scale_m;
1075 static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
1076 14145585 const uint8_t *scale_matrix = NULL;
1077 uint8_t dc_scale;
1078
2/2
✓ Branch 0 taken 9694148 times.
✓ Branch 1 taken 4451437 times.
14145585 int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode :
1079 lc->tu.intra_pred_mode_c;
1080
1081 14145585 memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1082
1083 // Derive QP for dequant
1084
2/2
✓ Branch 0 taken 13707219 times.
✓ Branch 1 taken 438366 times.
14145585 if (!lc->cu.cu_transquant_bypass_flag) {
1085 static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1086 static const uint8_t rem6[51 + 4 * 6 + 1] = {
1087 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1088 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1089 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1090 4, 5, 0, 1, 2, 3, 4, 5, 0, 1
1091 };
1092
1093 static const uint8_t div6[51 + 4 * 6 + 1] = {
1094 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1095 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1096 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1097 10, 10, 11, 11, 11, 11, 11, 11, 12, 12
1098 };
1099 13707219 int qp_y = lc->qp_y;
1100
1101
2/2
✓ Branch 0 taken 11257982 times.
✓ Branch 1 taken 2449237 times.
13707219 if (s->ps.pps->transform_skip_enabled_flag &&
1102
2/2
✓ Branch 0 taken 6506260 times.
✓ Branch 1 taken 4751722 times.
11257982 log2_trafo_size <= s->ps.pps->log2_max_transform_skip_block_size) {
1103 6506260 transform_skip_flag = hevc_transform_skip_flag_decode(lc, c_idx);
1104 }
1105
1106
2/2
✓ Branch 0 taken 9418900 times.
✓ Branch 1 taken 4288319 times.
13707219 if (c_idx == 0) {
1107 9418900 qp = qp_y + s->ps.sps->qp_bd_offset;
1108 } else {
1109 int qp_i, offset;
1110
1111
2/2
✓ Branch 0 taken 2046698 times.
✓ Branch 1 taken 2241621 times.
4288319 if (c_idx == 1)
1112 2046698 offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset +
1113 2046698 lc->tu.cu_qp_offset_cb;
1114 else
1115 2241621 offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset +
1116 2241621 lc->tu.cu_qp_offset_cr;
1117
1118 4288319 qp_i = av_clip(qp_y + offset, - s->ps.sps->qp_bd_offset, 57);
1119
2/2
✓ Branch 0 taken 2422214 times.
✓ Branch 1 taken 1866105 times.
4288319 if (s->ps.sps->chroma_format_idc == 1) {
1120
2/2
✓ Branch 0 taken 738077 times.
✓ Branch 1 taken 1684137 times.
2422214 if (qp_i < 30)
1121 738077 qp = qp_i;
1122
2/2
✓ Branch 0 taken 9279 times.
✓ Branch 1 taken 1674858 times.
1684137 else if (qp_i > 43)
1123 9279 qp = qp_i - 6;
1124 else
1125 1674858 qp = qp_c[qp_i - 30];
1126 } else {
1127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1866105 times.
1866105 if (qp_i > 51)
1128 qp = 51;
1129 else
1130 1866105 qp = qp_i;
1131 }
1132
1133 4288319 qp += s->ps.sps->qp_bd_offset;
1134 }
1135
1136 13707219 shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
1137 13707219 add = 1 << (shift-1);
1138 13707219 scale = level_scale[rem6[qp]] << (div6[qp]);
1139 13707219 scale_m = 16; // default when no custom scaling lists.
1140 13707219 dc_scale = 16;
1141
1142
6/6
✓ Branch 0 taken 484014 times.
✓ Branch 1 taken 13223205 times.
✓ Branch 2 taken 26526 times.
✓ Branch 3 taken 457488 times.
✓ Branch 4 taken 14292 times.
✓ Branch 5 taken 12234 times.
13707219 if (s->ps.sps->scaling_list_enable_flag && !(transform_skip_flag && log2_trafo_size > 2)) {
1143 943560 const ScalingList *sl = s->ps.pps->scaling_list_data_present_flag ?
1144
2/2
✓ Branch 0 taken 357638 times.
✓ Branch 1 taken 114142 times.
471780 &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
1145 471780 int matrix_id = lc->cu.pred_mode != MODE_INTRA;
1146
1147 471780 matrix_id = 3 * matrix_id + c_idx;
1148
1149 471780 scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
1150
2/2
✓ Branch 0 taken 42637 times.
✓ Branch 1 taken 429143 times.
471780 if (log2_trafo_size >= 4)
1151 42637 dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
1152 }
1153 } else {
1154 438366 shift = 0;
1155 438366 add = 0;
1156 438366 scale = 0;
1157 438366 dc_scale = 0;
1158 }
1159
1160
6/6
✓ Branch 0 taken 4620679 times.
✓ Branch 1 taken 9524906 times.
✓ Branch 2 taken 225511 times.
✓ Branch 3 taken 4395168 times.
✓ Branch 4 taken 190277 times.
✓ Branch 5 taken 35234 times.
14145585 if (lc->cu.pred_mode == MODE_INTER && s->ps.sps->explicit_rdpcm_enabled_flag &&
1161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190277 times.
190277 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1162 35234 explicit_rdpcm_flag = explicit_rdpcm_flag_decode(lc, c_idx);
1163
2/2
✓ Branch 0 taken 3090 times.
✓ Branch 1 taken 32144 times.
35234 if (explicit_rdpcm_flag) {
1164 3090 explicit_rdpcm_dir_flag = explicit_rdpcm_dir_flag_decode(lc, c_idx);
1165 }
1166 }
1167
1168 14145585 last_significant_coeff_xy_prefix_decode(lc, c_idx, log2_trafo_size,
1169 &last_significant_coeff_x, &last_significant_coeff_y);
1170
1171
2/2
✓ Branch 0 taken 1801417 times.
✓ Branch 1 taken 12344168 times.
14145585 if (last_significant_coeff_x > 3) {
1172 1801417 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x);
1173 1801417 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1174 1801417 (2 + (last_significant_coeff_x & 1)) +
1175 suffix;
1176 }
1177
1178
2/2
✓ Branch 0 taken 1277954 times.
✓ Branch 1 taken 12867631 times.
14145585 if (last_significant_coeff_y > 3) {
1179 1277954 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y);
1180 1277954 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1181 1277954 (2 + (last_significant_coeff_y & 1)) +
1182 suffix;
1183 }
1184
1185
2/2
✓ Branch 0 taken 1814967 times.
✓ Branch 1 taken 12330618 times.
14145585 if (scan_idx == SCAN_VERT)
1186 1814967 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1187
1188 14145585 x_cg_last_sig = last_significant_coeff_x >> 2;
1189 14145585 y_cg_last_sig = last_significant_coeff_y >> 2;
1190
1191
3/3
✓ Branch 0 taken 10301225 times.
✓ Branch 1 taken 2029393 times.
✓ Branch 2 taken 1814967 times.
14145585 switch (scan_idx) {
1192 10301225 case SCAN_DIAG: {
1193 10301225 int last_x_c = last_significant_coeff_x & 3;
1194 10301225 int last_y_c = last_significant_coeff_y & 3;
1195
1196 10301225 scan_x_off = ff_hevc_diag_scan4x4_x;
1197 10301225 scan_y_off = ff_hevc_diag_scan4x4_y;
1198 10301225 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1199
2/2
✓ Branch 0 taken 5246857 times.
✓ Branch 1 taken 5054368 times.
10301225 if (trafo_size == 4) {
1200 5246857 scan_x_cg = scan_1x1;
1201 5246857 scan_y_cg = scan_1x1;
1202
2/2
✓ Branch 0 taken 2776887 times.
✓ Branch 1 taken 2277481 times.
5054368 } else if (trafo_size == 8) {
1203 2776887 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1204 2776887 scan_x_cg = diag_scan2x2_x;
1205 2776887 scan_y_cg = diag_scan2x2_y;
1206
2/2
✓ Branch 0 taken 1688464 times.
✓ Branch 1 taken 589017 times.
2277481 } else if (trafo_size == 16) {
1207 1688464 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1208 1688464 scan_x_cg = ff_hevc_diag_scan4x4_x;
1209 1688464 scan_y_cg = ff_hevc_diag_scan4x4_y;
1210 } else { // trafo_size == 32
1211 589017 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1212 589017 scan_x_cg = ff_hevc_diag_scan8x8_x;
1213 589017 scan_y_cg = ff_hevc_diag_scan8x8_y;
1214 }
1215 10301225 break;
1216 }
1217 2029393 case SCAN_HORIZ:
1218 2029393 scan_x_cg = horiz_scan2x2_x;
1219 2029393 scan_y_cg = horiz_scan2x2_y;
1220 2029393 scan_x_off = horiz_scan4x4_x;
1221 2029393 scan_y_off = horiz_scan4x4_y;
1222 2029393 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1223 2029393 break;
1224 1814967 default: //SCAN_VERT
1225 1814967 scan_x_cg = horiz_scan2x2_y;
1226 1814967 scan_y_cg = horiz_scan2x2_x;
1227 1814967 scan_x_off = horiz_scan4x4_y;
1228 1814967 scan_y_off = horiz_scan4x4_x;
1229 1814967 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1230 1814967 break;
1231 }
1232 14145585 num_coeff++;
1233 14145585 num_last_subset = (num_coeff - 1) >> 4;
1234
1235
2/2
✓ Branch 0 taken 24779408 times.
✓ Branch 1 taken 14145585 times.
38924993 for (i = num_last_subset; i >= 0; i--) {
1236 int n, m;
1237 int x_cg, y_cg, x_c, y_c, pos;
1238 24779408 int implicit_non_zero_coeff = 0;
1239 int64_t trans_coeff_level;
1240 24779408 int prev_sig = 0;
1241 24779408 int offset = i << 4;
1242 24779408 int rice_init = 0;
1243
1244 uint8_t significant_coeff_flag_idx[16];
1245 24779408 uint8_t nb_significant_coeff_flag = 0;
1246
1247 24779408 x_cg = scan_x_cg[i];
1248 24779408 y_cg = scan_y_cg[i];
1249
1250
4/4
✓ Branch 0 taken 10633823 times.
✓ Branch 1 taken 14145585 times.
✓ Branch 2 taken 8265020 times.
✓ Branch 3 taken 2368803 times.
24779408 if ((i < num_last_subset) && (i > 0)) {
1251 8265020 int ctx_cg = 0;
1252
2/2
✓ Branch 0 taken 7555953 times.
✓ Branch 1 taken 709067 times.
8265020 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1253 7555953 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1254
2/2
✓ Branch 0 taken 6973033 times.
✓ Branch 1 taken 1291987 times.
8265020 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1255 6973033 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1256
1257 8265020 significant_coeff_group_flag[x_cg][y_cg] =
1258 8265020 significant_coeff_group_flag_decode(lc, c_idx, ctx_cg);
1259 8265020 implicit_non_zero_coeff = 1;
1260 } else {
1261 16514388 significant_coeff_group_flag[x_cg][y_cg] =
1262
5/6
✓ Branch 0 taken 14819984 times.
✓ Branch 1 taken 1694404 times.
✓ Branch 2 taken 674399 times.
✓ Branch 3 taken 14145585 times.
✓ Branch 4 taken 2368803 times.
✗ Branch 5 not taken.
18883191 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1263
1/2
✓ Branch 0 taken 2368803 times.
✗ Branch 1 not taken.
2368803 (x_cg == 0 && y_cg == 0));
1264 }
1265
1266 24779408 last_scan_pos = num_coeff - offset - 1;
1267
1268
2/2
✓ Branch 0 taken 14145585 times.
✓ Branch 1 taken 10633823 times.
24779408 if (i == num_last_subset) {
1269 14145585 n_end = last_scan_pos - 1;
1270 14145585 significant_coeff_flag_idx[0] = last_scan_pos;
1271 14145585 nb_significant_coeff_flag = 1;
1272 } else {
1273 10633823 n_end = 15;
1274 }
1275
1276
2/2
✓ Branch 0 taken 14868154 times.
✓ Branch 1 taken 9911254 times.
24779408 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1277 14868154 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1278
2/2
✓ Branch 0 taken 14441855 times.
✓ Branch 1 taken 10337553 times.
24779408 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1279
2/2
✓ Branch 0 taken 5137659 times.
✓ Branch 1 taken 9304196 times.
14441855 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1280
1281
4/4
✓ Branch 0 taken 21663555 times.
✓ Branch 1 taken 3115853 times.
✓ Branch 2 taken 18464866 times.
✓ Branch 3 taken 3198689 times.
24779408 if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) {
1282 static const uint8_t ctx_idx_map[] = {
1283 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8, // log2_trafo_size == 2
1284 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0
1285 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 1
1286 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, // prev_sig == 2
1287 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // default
1288 };
1289 const uint8_t *ctx_idx_map_p;
1290 18464866 int scf_offset = 0;
1291
4/4
✓ Branch 0 taken 1017071 times.
✓ Branch 1 taken 17447795 times.
✓ Branch 2 taken 886760 times.
✓ Branch 3 taken 130311 times.
18464866 if (s->ps.sps->transform_skip_context_enabled_flag &&
1292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 886760 times.
886760 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1293 130311 ctx_idx_map_p = &ctx_idx_map[4 * 16];
1294
2/2
✓ Branch 0 taken 61311 times.
✓ Branch 1 taken 69000 times.
130311 if (c_idx == 0) {
1295 61311 scf_offset = 40;
1296 } else {
1297 69000 scf_offset = 14 + 27;
1298 }
1299 } else {
1300
2/2
✓ Branch 0 taken 4404644 times.
✓ Branch 1 taken 13929911 times.
18334555 if (c_idx != 0)
1301 4404644 scf_offset = 27;
1302
2/2
✓ Branch 0 taken 6335516 times.
✓ Branch 1 taken 11999039 times.
18334555 if (log2_trafo_size == 2) {
1303 6335516 ctx_idx_map_p = &ctx_idx_map[0];
1304 } else {
1305 11999039 ctx_idx_map_p = &ctx_idx_map[(prev_sig + 1) << 4];
1306
2/2
✓ Branch 0 taken 9611782 times.
✓ Branch 1 taken 2387257 times.
11999039 if (c_idx == 0) {
1307
4/4
✓ Branch 0 taken 5803573 times.
✓ Branch 1 taken 3808209 times.
✓ Branch 2 taken 2015231 times.
✓ Branch 3 taken 3788342 times.
9611782 if ((x_cg > 0 || y_cg > 0))
1308 5823440 scf_offset += 3;
1309
2/2
✓ Branch 0 taken 3603791 times.
✓ Branch 1 taken 6007991 times.
9611782 if (log2_trafo_size == 3) {
1310
2/2
✓ Branch 0 taken 2501915 times.
✓ Branch 1 taken 1101876 times.
3603791 scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15;
1311 } else {
1312 6007991 scf_offset += 21;
1313 }
1314 } else {
1315
2/2
✓ Branch 0 taken 1135109 times.
✓ Branch 1 taken 1252148 times.
2387257 if (log2_trafo_size == 3)
1316 1135109 scf_offset += 9;
1317 else
1318 1252148 scf_offset += 12;
1319 }
1320 }
1321 }
1322
2/2
✓ Branch 0 taken 169348688 times.
✓ Branch 1 taken 18464866 times.
187813554 for (n = n_end; n > 0; n--) {
1323 169348688 x_c = scan_x_off[n];
1324 169348688 y_c = scan_y_off[n];
1325
2/2
✓ Branch 1 taken 81616805 times.
✓ Branch 2 taken 87731883 times.
169348688 if (significant_coeff_flag_decode(lc, x_c, y_c, scf_offset, ctx_idx_map_p)) {
1326 81616805 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1327 81616805 nb_significant_coeff_flag++;
1328 81616805 implicit_non_zero_coeff = 0;
1329 }
1330 }
1331
2/2
✓ Branch 0 taken 18394335 times.
✓ Branch 1 taken 70531 times.
18464866 if (implicit_non_zero_coeff == 0) {
1332
4/4
✓ Branch 0 taken 1014653 times.
✓ Branch 1 taken 17379682 times.
✓ Branch 2 taken 884736 times.
✓ Branch 3 taken 129917 times.
18394335 if (s->ps.sps->transform_skip_context_enabled_flag &&
1333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 884736 times.
884736 (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1334
2/2
✓ Branch 0 taken 61056 times.
✓ Branch 1 taken 68861 times.
129917 if (c_idx == 0) {
1335 61056 scf_offset = 42;
1336 } else {
1337 68861 scf_offset = 16 + 27;
1338 }
1339 } else {
1340
2/2
✓ Branch 0 taken 11190782 times.
✓ Branch 1 taken 7073636 times.
18264418 if (i == 0) {
1341
2/2
✓ Branch 0 taken 8106471 times.
✓ Branch 1 taken 3084311 times.
11190782 if (c_idx == 0)
1342 8106471 scf_offset = 0;
1343 else
1344 3084311 scf_offset = 27;
1345 } else {
1346 7073636 scf_offset = 2 + scf_offset;
1347 }
1348 }
1349
2/2
✓ Branch 1 taken 12399999 times.
✓ Branch 2 taken 5994336 times.
18394335 if (significant_coeff_flag_decode_0(lc, c_idx, scf_offset) == 1) {
1350 12399999 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1351 12399999 nb_significant_coeff_flag++;
1352 }
1353 } else {
1354 70531 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1355 70531 nb_significant_coeff_flag++;
1356 }
1357 }
1358
1359 24779408 n_end = nb_significant_coeff_flag;
1360
1361
1362
2/2
✓ Branch 0 taken 21577991 times.
✓ Branch 1 taken 3201417 times.
24779408 if (n_end) {
1363 int first_nz_pos_in_cg;
1364 int last_nz_pos_in_cg;
1365 21577991 int c_rice_param = 0;
1366 21577991 int first_greater1_coeff_idx = -1;
1367 uint8_t coeff_abs_level_greater1_flag[8];
1368 uint16_t coeff_sign_flag;
1369 21577991 int sum_abs = 0;
1370 int sign_hidden;
1371 int sb_type;
1372
1373
1374 // initialize first elem of coeff_bas_level_greater1_flag
1375
4/4
✓ Branch 0 taken 7517970 times.
✓ Branch 1 taken 14060021 times.
✓ Branch 2 taken 6107887 times.
✓ Branch 3 taken 1410083 times.
21577991 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1376
1377
2/2
✓ Branch 0 taken 1166629 times.
✓ Branch 1 taken 20411362 times.
21577991 if (s->ps.sps->persistent_rice_adaptation_enabled_flag) {
1378
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)
1379
2/2
✓ Branch 0 taken 365016 times.
✓ Branch 1 taken 682024 times.
1047040 sb_type = 2 * (c_idx == 0 ? 1 : 0);
1380 else
1381
2/2
✓ Branch 0 taken 56576 times.
✓ Branch 1 taken 63013 times.
119589 sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1;
1382 1166629 c_rice_param = lc->stat_coeff[sb_type] / 4;
1383 }
1384
1385
4/4
✓ Branch 0 taken 7432406 times.
✓ Branch 1 taken 14145585 times.
✓ Branch 2 taken 3314201 times.
✓ Branch 3 taken 4118205 times.
21577991 if (!(i == num_last_subset) && greater1_ctx == 0)
1386 3314201 ctx_set++;
1387 21577991 greater1_ctx = 1;
1388 21577991 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1389
1390
2/2
✓ Branch 0 taken 90442731 times.
✓ Branch 1 taken 21577991 times.
112020722 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1391 90442731 int inc = (ctx_set << 2) + greater1_ctx;
1392 90442731 coeff_abs_level_greater1_flag[m] =
1393 90442731 coeff_abs_level_greater1_flag_decode(lc, c_idx, inc);
1394
2/2
✓ Branch 0 taken 29794520 times.
✓ Branch 1 taken 60648211 times.
90442731 if (coeff_abs_level_greater1_flag[m]) {
1395 29794520 greater1_ctx = 0;
1396
2/2
✓ Branch 0 taken 10068953 times.
✓ Branch 1 taken 19725567 times.
29794520 if (first_greater1_coeff_idx == -1)
1397 10068953 first_greater1_coeff_idx = m;
1398
4/4
✓ Branch 0 taken 48092510 times.
✓ Branch 1 taken 12555701 times.
✓ Branch 2 taken 29622596 times.
✓ Branch 3 taken 18469914 times.
60648211 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1399 29622596 greater1_ctx++;
1400 }
1401 }
1402 21577991 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1403
1404
2/2
✓ Branch 0 taken 20874504 times.
✓ Branch 1 taken 703487 times.
21577991 if (lc->cu.cu_transquant_bypass_flag ||
1405
2/2
✓ Branch 0 taken 13468707 times.
✓ Branch 1 taken 7405797 times.
20874504 (lc->cu.pred_mode == MODE_INTRA &&
1406
6/6
✓ Branch 0 taken 902776 times.
✓ Branch 1 taken 12565931 times.
✓ Branch 2 taken 92431 times.
✓ Branch 3 taken 810345 times.
✓ Branch 4 taken 47376 times.
✓ Branch 5 taken 45055 times.
13468707 s->ps.sps->implicit_rdpcm_enabled_flag && transform_skip_flag &&
1407
4/4
✓ Branch 0 taken 28377 times.
✓ Branch 1 taken 18999 times.
✓ Branch 2 taken 5999 times.
✓ Branch 3 taken 20804451 times.
20829449 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1408 explicit_rdpcm_flag)
1409 773540 sign_hidden = 0;
1410 else
1411 20804451 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1412
1413
2/2
✓ Branch 0 taken 10068953 times.
✓ Branch 1 taken 11509038 times.
21577991 if (first_greater1_coeff_idx != -1) {
1414 10068953 coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(lc, c_idx, ctx_set);
1415 }
1416
4/4
✓ Branch 0 taken 19222514 times.
✓ Branch 1 taken 2355477 times.
✓ Branch 2 taken 8400163 times.
✓ Branch 3 taken 10822351 times.
21577991 if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden ) {
1417 10755640 coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1418 } else {
1419 10822351 coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1420 }
1421
1422
2/2
✓ Branch 0 taken 108232920 times.
✓ Branch 1 taken 21577991 times.
129810911 for (m = 0; m < n_end; m++) {
1423 108232920 n = significant_coeff_flag_idx[m];
1424 108232920 GET_COORD(offset, n);
1425
2/2
✓ Branch 0 taken 90442731 times.
✓ Branch 1 taken 17790189 times.
108232920 if (m < 8) {
1426 90442731 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1427
4/4
✓ Branch 0 taken 10068953 times.
✓ Branch 1 taken 80373778 times.
✓ Branch 2 taken 22917579 times.
✓ Branch 3 taken 67525152 times.
90442731 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1428 22917579 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1429
1430 22917579 trans_coeff_level += last_coeff_abs_level_remaining;
1431
2/2
✓ Branch 0 taken 5953316 times.
✓ Branch 1 taken 16964263 times.
22917579 if (trans_coeff_level > (3 << c_rice_param))
1432
2/2
✓ Branch 0 taken 197734 times.
✓ Branch 1 taken 5755582 times.
5953316 c_rice_param = s->ps.sps->persistent_rice_adaptation_enabled_flag ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1433
4/4
✓ Branch 0 taken 1114125 times.
✓ Branch 1 taken 21803454 times.
✓ Branch 2 taken 315677 times.
✓ Branch 3 taken 798448 times.
22917579 if (s->ps.sps->persistent_rice_adaptation_enabled_flag && !rice_init) {
1434 315677 int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1435
2/2
✓ Branch 0 taken 37785 times.
✓ Branch 1 taken 277892 times.
315677 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1436 37785 lc->stat_coeff[sb_type]++;
1437
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))
1438
2/2
✓ Branch 0 taken 37744 times.
✓ Branch 1 taken 119323 times.
157067 if (lc->stat_coeff[sb_type] > 0)
1439 37744 lc->stat_coeff[sb_type]--;
1440 315677 rice_init = 1;
1441 }
1442 }
1443 } else {
1444 17790189 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1445
1446 17790189 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1447
2/2
✓ Branch 0 taken 2222205 times.
✓ Branch 1 taken 15567984 times.
17790189 if (trans_coeff_level > (3 << c_rice_param))
1448
2/2
✓ Branch 0 taken 89299 times.
✓ Branch 1 taken 2132906 times.
2222205 c_rice_param = s->ps.sps->persistent_rice_adaptation_enabled_flag ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1449
4/4
✓ Branch 0 taken 938151 times.
✓ Branch 1 taken 16852038 times.
✓ Branch 2 taken 36551 times.
✓ Branch 3 taken 901600 times.
17790189 if (s->ps.sps->persistent_rice_adaptation_enabled_flag && !rice_init) {
1450 36551 int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1451
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 35207 times.
36551 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1452 1344 lc->stat_coeff[sb_type]++;
1453
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))
1454
2/2
✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 23297 times.
24604 if (lc->stat_coeff[sb_type] > 0)
1455 1307 lc->stat_coeff[sb_type]--;
1456 36551 rice_init = 1;
1457 }
1458 }
1459
4/4
✓ Branch 0 taken 100343691 times.
✓ Branch 1 taken 7889229 times.
✓ Branch 2 taken 78325144 times.
✓ Branch 3 taken 22018547 times.
108232920 if (s->ps.pps->sign_data_hiding_flag && sign_hidden) {
1460 78325144 sum_abs += trans_coeff_level;
1461
4/4
✓ Branch 0 taken 10822351 times.
✓ Branch 1 taken 67502793 times.
✓ Branch 2 taken 5403489 times.
✓ Branch 3 taken 5418862 times.
78325144 if (n == first_nz_pos_in_cg && (sum_abs&1))
1462 5403489 trans_coeff_level = -trans_coeff_level;
1463 }
1464
2/2
✓ Branch 0 taken 49212189 times.
✓ Branch 1 taken 59020731 times.
108232920 if (coeff_sign_flag >> 15)
1465 49212189 trans_coeff_level = -trans_coeff_level;
1466 108232920 coeff_sign_flag <<= 1;
1467
2/2
✓ Branch 0 taken 99548534 times.
✓ Branch 1 taken 8684386 times.
108232920 if(!lc->cu.cu_transquant_bypass_flag) {
1468
6/6
✓ Branch 0 taken 2778647 times.
✓ Branch 1 taken 96769887 times.
✓ Branch 2 taken 190401 times.
✓ Branch 3 taken 2588246 times.
✓ Branch 4 taken 30375 times.
✓ Branch 5 taken 160026 times.
99548534 if (s->ps.sps->scaling_list_enable_flag && !(transform_skip_flag && log2_trafo_size > 2)) {
1469
6/6
✓ Branch 0 taken 822457 times.
✓ Branch 1 taken 1796164 times.
✓ Branch 2 taken 369421 times.
✓ Branch 3 taken 453036 times.
✓ Branch 4 taken 338164 times.
✓ Branch 5 taken 31257 times.
2618621 if(y_c || x_c || log2_trafo_size < 4) {
1470
4/4
✓ Branch 0 taken 650713 times.
✓ Branch 1 taken 814838 times.
✓ Branch 2 taken 298445 times.
✓ Branch 3 taken 823368 times.
2587364 switch(log2_trafo_size) {
1471 650713 case 3: pos = (y_c << 3) + x_c; break;
1472 814838 case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1473 298445 case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1474 823368 default: pos = (y_c << 2) + x_c; break;
1475 }
1476 2587364 scale_m = scale_matrix[pos];
1477 } else {
1478 31257 scale_m = dc_scale;
1479 }
1480 }
1481 99548534 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1482
2/2
✓ Branch 0 taken 50244860 times.
✓ Branch 1 taken 49303674 times.
99548534 if(trans_coeff_level < 0) {
1483
2/2
✓ Branch 0 taken 162681 times.
✓ Branch 1 taken 50082179 times.
50244860 if((~trans_coeff_level) & 0xFffffffffff8000)
1484 162681 trans_coeff_level = -32768;
1485 } else {
1486
2/2
✓ Branch 0 taken 162295 times.
✓ Branch 1 taken 49141379 times.
49303674 if(trans_coeff_level & 0xffffffffffff8000)
1487 162295 trans_coeff_level = 32767;
1488 }
1489 }
1490 108232920 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1491 }
1492 }
1493 }
1494
1495
2/2
✓ Branch 0 taken 438366 times.
✓ Branch 1 taken 13707219 times.
14145585 if (lc->cu.cu_transquant_bypass_flag) {
1496
2/6
✓ Branch 0 taken 438366 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438366 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
438366 if (explicit_rdpcm_flag || (s->ps.sps->implicit_rdpcm_enabled_flag &&
1497 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1498 int mode = s->ps.sps->implicit_rdpcm_enabled_flag ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag;
1499
1500 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1501 }
1502 } else {
1503
2/2
✓ Branch 0 taken 432141 times.
✓ Branch 1 taken 13275078 times.
13707219 if (transform_skip_flag) {
1504
2/2
✓ Branch 0 taken 54609 times.
✓ Branch 1 taken 21520 times.
76129 int rot = s->ps.sps->transform_skip_rotation_enabled_flag &&
1505
2/2
✓ Branch 0 taken 76129 times.
✓ Branch 1 taken 356012 times.
508270 log2_trafo_size == 2 &&
1506
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 33783 times.
54609 lc->cu.pred_mode == MODE_INTRA;
1507
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 411315 times.
432141 if (rot) {
1508
2/2
✓ Branch 0 taken 166608 times.
✓ Branch 1 taken 20826 times.
187434 for (i = 0; i < 8; i++)
1509 166608 FFSWAP(int16_t, coeffs[i], coeffs[16 - i - 1]);
1510 }
1511
1512 432141 s->hevcdsp.dequant(coeffs, log2_trafo_size);
1513
1514
4/4
✓ Branch 0 taken 429051 times.
✓ Branch 1 taken 3090 times.
✓ Branch 2 taken 73039 times.
✓ Branch 3 taken 356012 times.
432141 if (explicit_rdpcm_flag || (s->ps.sps->implicit_rdpcm_enabled_flag &&
1515
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 &&
1516
2/2
✓ Branch 0 taken 8263 times.
✓ Branch 1 taken 16922 times.
25185 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1517
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);
1518
1519 27063 s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1520 }
1521
6/6
✓ Branch 0 taken 9021280 times.
✓ Branch 1 taken 4253798 times.
✓ Branch 2 taken 6095272 times.
✓ Branch 3 taken 2926008 times.
✓ Branch 4 taken 3571093 times.
✓ Branch 5 taken 2524179 times.
13275078 } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) {
1522 3571093 s->hevcdsp.transform_4x4_luma(coeffs);
1523 } else {
1524 9703985 int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1525
2/2
✓ Branch 0 taken 2179306 times.
✓ Branch 1 taken 7524679 times.
9703985 if (max_xy == 0)
1526 2179306 s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1527 else {
1528 7524679 int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1529
2/2
✓ Branch 0 taken 5211416 times.
✓ Branch 1 taken 2313263 times.
7524679 if (max_xy < 4)
1530 5211416 col_limit = FFMIN(4, col_limit);
1531
2/2
✓ Branch 0 taken 1727764 times.
✓ Branch 1 taken 585499 times.
2313263 else if (max_xy < 8)
1532 1727764 col_limit = FFMIN(8, col_limit);
1533
2/2
✓ Branch 0 taken 250803 times.
✓ Branch 1 taken 334696 times.
585499 else if (max_xy < 12)
1534 250803 col_limit = FFMIN(24, col_limit);
1535 7524679 s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1536 }
1537 }
1538 }
1539
2/2
✓ Branch 0 taken 269268 times.
✓ Branch 1 taken 13876317 times.
14145585 if (lc->tu.cross_pf) {
1540 269268 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1541
1542
2/2
✓ Branch 0 taken 20252880 times.
✓ Branch 1 taken 269268 times.
20522148 for (i = 0; i < (trafo_size * trafo_size); i++) {
1543 20252880 coeffs[i] = coeffs[i] + ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1544 }
1545 }
1546 14145585 s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride);
1547 14145585 }
1548
1549 2629853 void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
1550 {
1551 2629853 int x = abs_mvd_greater0_flag_decode(lc);
1552 2629853 int y = abs_mvd_greater0_flag_decode(lc);
1553
1554
2/2
✓ Branch 0 taken 1835839 times.
✓ Branch 1 taken 794014 times.
2629853 if (x)
1555 1835839 x += abs_mvd_greater1_flag_decode(lc);
1556
2/2
✓ Branch 0 taken 1763344 times.
✓ Branch 1 taken 866509 times.
2629853 if (y)
1557 1763344 y += abs_mvd_greater1_flag_decode(lc);
1558
1559
3/4
✓ Branch 0 taken 1333671 times.
✓ Branch 1 taken 502168 times.
✓ Branch 2 taken 794014 times.
✗ Branch 3 not taken.
2629853 switch (x) {
1560 1333671 case 2: lc->pu.mvd.x = mvd_decode(lc); break;
1561 502168 case 1: lc->pu.mvd.x = mvd_sign_flag_decode(lc); break;
1562 794014 case 0: lc->pu.mvd.x = 0; break;
1563 }
1564
1565
3/4
✓ Branch 0 taken 1250357 times.
✓ Branch 1 taken 512987 times.
✓ Branch 2 taken 866509 times.
✗ Branch 3 not taken.
2629853 switch (y) {
1566 1250357 case 2: lc->pu.mvd.y = mvd_decode(lc); break;
1567 512987 case 1: lc->pu.mvd.y = mvd_sign_flag_decode(lc); break;
1568 866509 case 0: lc->pu.mvd.y = 0; break;
1569 }
1570 2629853 }
1571
1572