FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_cabac.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 649 670 96.9%
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 1404493 void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts)
451 {
452 1404493 const HEVCContext *const s = lc->parent;
453
454
2/2
✓ Branch 0 taken 102312 times.
✓ Branch 1 taken 1302181 times.
1404493 if (s->ps.pps->entropy_coding_sync_enabled_flag &&
455
2/2
✓ Branch 0 taken 97578 times.
✓ Branch 1 taken 4734 times.
102312 (ctb_addr_ts % s->ps.sps->ctb_width == 2 ||
456
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 96794 times.
97578 (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 5126 memcpy(lc->common_cabac_state->state, lc->cabac_state, HEVC_CONTEXTS);
459
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 5078 times.
5126 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 1404493 }
464
465 4115 static void load_states(HEVCLocalContext *lc, const HEVCContext *s)
466 {
467 4115 memcpy(lc->cabac_state, lc->common_cabac_state->state, HEVC_CONTEXTS);
468
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4071 times.
4115 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 4115 }
472
473 7780 static int cabac_reinit(HEVCLocalContext *lc)
474 {
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7780 times.
7780 return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0;
476 }
477
478 27481 static int cabac_init_decoder(HEVCLocalContext *lc)
479 {
480 27481 GetBitContext *gb = &lc->gb;
481 27481 skip_bits(gb, 1);
482 27481 align_get_bits(gb);
483 82443 return ff_init_cabac_decoder(&lc->cc,
484 27481 gb->buffer + get_bits_count(gb) / 8,
485 27481 (get_bits_left(gb) + 7) / 8);
486 }
487
488 24762 static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
489 {
490 24762 int init_type = 2 - s->sh.slice_type;
491 int i;
492
493
4/4
✓ Branch 0 taken 6488 times.
✓ Branch 1 taken 18274 times.
✓ Branch 2 taken 6345 times.
✓ Branch 3 taken 143 times.
24762 if (s->sh.cabac_init_flag && s->sh.slice_type != HEVC_SLICE_I)
494 6345 init_type ^= 3;
495
496
2/2
✓ Branch 0 taken 4927638 times.
✓ Branch 1 taken 24762 times.
4952400 for (i = 0; i < HEVC_CONTEXTS; i++) {
497 4927638 int init_value = init_values[init_type][i];
498 4927638 int m = (init_value >> 4) * 5 - 45;
499 4927638 int n = ((init_value & 15) << 3) - 16;
500 4927638 int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
501
502 4927638 pre ^= pre >> 31;
503
2/2
✓ Branch 0 taken 505880 times.
✓ Branch 1 taken 4421758 times.
4927638 if (pre > 124)
504 505880 pre = 124 + (pre & 1);
505 4927638 lc->cabac_state[i] = pre;
506 }
507
508
2/2
✓ Branch 0 taken 99048 times.
✓ Branch 1 taken 24762 times.
123810 for (i = 0; i < 4; i++)
509 99048 lc->stat_coeff[i] = 0;
510 24762 }
511
512 1404494 int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts)
513 {
514 1404494 const HEVCContext *const s = lc->parent;
515
516
2/2
✓ Branch 0 taken 27481 times.
✓ Branch 1 taken 1377013 times.
1404494 if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
517 27481 int ret = cabac_init_decoder(lc);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27481 times.
27481 if (ret < 0)
519 return ret;
520
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 19534 times.
27481 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 19843 cabac_init_state(lc, s);
524
525
2/2
✓ Branch 0 taken 17989 times.
✓ Branch 1 taken 9492 times.
27481 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 1089814 times.
1377013 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 97732 times.
✓ Branch 1 taken 1279281 times.
1377013 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
548
2/2
✓ Branch 0 taken 3155 times.
✓ Branch 1 taken 94577 times.
97732 if (ctb_addr_ts % s->ps.sps->ctb_width == 0) {
549 int ret;
550 3155 get_cabac_terminate(&lc->cc);
551
1/2
✓ Branch 0 taken 3155 times.
✗ Branch 1 not taken.
3155 if (s->threads_number == 1)
552 3155 ret = cabac_reinit(lc);
553 else {
554 ret = cabac_init_decoder(lc);
555 }
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3155 times.
3155 if (ret < 0)
557 return ret;
558
559
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 2953 times.
3155 if (s->ps.sps->ctb_width == 1)
560 202 cabac_init_state(lc, s);
561 else
562 2953 load_states(lc, s);
563 }
564 }
565 }
566 1404494 return 0;
567 }
568
569 #define GET_CABAC(ctx) get_cabac(&lc->cc, &lc->cabac_state[ctx])
570
571 800227 int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc)
572 {
573 800227 return GET_CABAC(elem_offset[SAO_MERGE_FLAG]);
574 }
575
576 361259 int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
577 {
578
2/2
✓ Branch 1 taken 199089 times.
✓ Branch 2 taken 162170 times.
361259 if (!GET_CABAC(elem_offset[SAO_TYPE_IDX]))
579 199089 return 0;
580
581
2/2
✓ Branch 1 taken 53862 times.
✓ Branch 2 taken 108308 times.
162170 if (!get_cabac_bypass(&lc->cc))
582 53862 return SAO_BAND;
583 108308 return SAO_EDGE;
584 }
585
586 77817 int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc)
587 {
588 int i;
589 77817 int value = get_cabac_bypass(&lc->cc);
590
591
2/2
✓ Branch 0 taken 311268 times.
✓ Branch 1 taken 77817 times.
389085 for (i = 0; i < 4; i++)
592 311268 value = (value << 1) | get_cabac_bypass(&lc->cc);
593 77817 return value;
594 }
595
596 867552 int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc)
597 {
598 867552 int i = 0;
599 867552 int length = (1 << (FFMIN(lc->parent->ps.sps->bit_depth, 10) - 5)) - 1;
600
601
4/4
✓ Branch 0 taken 2455695 times.
✓ Branch 1 taken 127355 times.
✓ Branch 3 taken 1715498 times.
✓ Branch 4 taken 740197 times.
2583050 while (i < length && get_cabac_bypass(&lc->cc))
602 1715498 i++;
603 867552 return i;
604 }
605
606 242918 int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc)
607 {
608 242918 return get_cabac_bypass(&lc->cc);
609 }
610
611 108308 int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
612 {
613 108308 int ret = get_cabac_bypass(&lc->cc) << 1;
614 108308 ret |= get_cabac_bypass(&lc->cc);
615 108308 return ret;
616 }
617
618 1404493 int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc)
619 {
620 1404493 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 8062573 int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, int x0, int y0, int x_cb, int y_cb)
629 {
630 8062573 const HEVCContext *const s = lc->parent;
631 8062573 int min_cb_width = s->ps.sps->min_cb_width;
632 8062573 int inc = 0;
633 8062573 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
634 8062573 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
635
636
4/4
✓ Branch 0 taken 735981 times.
✓ Branch 1 taken 7326592 times.
✓ Branch 2 taken 531679 times.
✓ Branch 3 taken 204302 times.
8062573 if (lc->ctb_left_flag || x0b)
637 7858271 inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
638
4/4
✓ Branch 0 taken 1678173 times.
✓ Branch 1 taken 6384400 times.
✓ Branch 2 taken 1221730 times.
✓ Branch 3 taken 456443 times.
8062573 if (lc->ctb_up_flag || y0b)
639 7606130 inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1);
640
641 8062573 return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
642 }
643
644 588011 int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc)
645 {
646 588011 int prefix_val = 0;
647 588011 int suffix_val = 0;
648 588011 int inc = 0;
649
650
4/4
✓ Branch 0 taken 1457778 times.
✓ Branch 1 taken 106000 times.
✓ Branch 3 taken 975767 times.
✓ Branch 4 taken 482011 times.
1563778 while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
651 975767 prefix_val++;
652 975767 inc = 1;
653 }
654
2/2
✓ Branch 0 taken 106000 times.
✓ Branch 1 taken 482011 times.
588011 if (prefix_val >= 5) {
655 106000 int k = 0;
656
3/4
✓ Branch 0 taken 360086 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 254086 times.
✓ Branch 4 taken 106000 times.
360086 while (k < 7 && get_cabac_bypass(&lc->cc)) {
657 254086 suffix_val += 1 << k;
658 254086 k++;
659 }
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106000 times.
106000 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 254086 times.
✓ Branch 1 taken 106000 times.
360086 while (k--)
666 254086 suffix_val += get_cabac_bypass(&lc->cc) << k;
667 }
668 588011 return prefix_val + suffix_val;
669 }
670
671 368706 int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc)
672 {
673 368706 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 4187183 int ff_hevc_pred_mode_decode(HEVCLocalContext *lc)
693 {
694 4187183 return GET_CABAC(elem_offset[PRED_MODE_FLAG]);
695 }
696
697 7449707 int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, int ct_depth, int x0, int y0)
698 {
699 7449707 const HEVCContext *const s = lc->parent;
700 7449707 const HEVCSPS *const sps = s->ps.sps;
701 7449707 int inc = 0, depth_left = 0, depth_top = 0;
702 7449707 int x0b = av_mod_uintp2(x0, sps->log2_ctb_size);
703 7449707 int y0b = av_mod_uintp2(y0, sps->log2_ctb_size);
704 7449707 int x_cb = x0 >> sps->log2_min_cb_size;
705 7449707 int y_cb = y0 >> sps->log2_min_cb_size;
706
707
4/4
✓ Branch 0 taken 693521 times.
✓ Branch 1 taken 6756186 times.
✓ Branch 2 taken 396086 times.
✓ Branch 3 taken 297435 times.
7449707 if (lc->ctb_left_flag || x0b)
708 7152272 depth_left = s->tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1];
709
4/4
✓ Branch 0 taken 1489027 times.
✓ Branch 1 taken 5960680 times.
✓ Branch 2 taken 838387 times.
✓ Branch 3 taken 650640 times.
7449707 if (lc->ctb_up_flag || y0b)
710 6799067 depth_top = s->tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb];
711
712 7449707 inc += (depth_left > ct_depth);
713 7449707 inc += (depth_top > ct_depth);
714
715 7449707 return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc);
716 }
717
718 6152496 int ff_hevc_part_mode_decode(HEVCLocalContext *lc, int log2_cb_size)
719 {
720
2/2
✓ Branch 1 taken 3580246 times.
✓ Branch 2 taken 2572250 times.
6152496 if (GET_CABAC(elem_offset[PART_MODE])) // 1
721 3580246 return PART_2Nx2N;
722
2/2
✓ Branch 0 taken 1941700 times.
✓ Branch 1 taken 630550 times.
2572250 if (log2_cb_size == lc->parent->ps.sps->log2_min_cb_size) {
723
2/2
✓ Branch 0 taken 1192511 times.
✓ Branch 1 taken 749189 times.
1941700 if (lc->cu.pred_mode == MODE_INTRA) // 0
724 1192511 return PART_NxN;
725
2/2
✓ Branch 1 taken 308106 times.
✓ Branch 2 taken 441083 times.
749189 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
726 308106 return PART_2NxN;
727
2/2
✓ Branch 0 taken 223042 times.
✓ Branch 1 taken 218041 times.
441083 if (log2_cb_size == 3) // 00
728 223042 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 7516767 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc)
761 {
762 7516767 return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]);
763 }
764
765 4504443 int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc)
766 {
767 4504443 int i = 0;
768
4/4
✓ Branch 0 taken 6709015 times.
✓ Branch 1 taken 936265 times.
✓ Branch 3 taken 3140837 times.
✓ Branch 4 taken 3568178 times.
7645280 while (i < 2 && get_cabac_bypass(&lc->cc))
769 3140837 i++;
770 4504443 return i;
771 }
772
773 3012324 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc)
774 {
775 int i;
776 3012324 int value = get_cabac_bypass(&lc->cc);
777
778
2/2
✓ Branch 0 taken 12049296 times.
✓ Branch 1 taken 3012324 times.
15061620 for (i = 0; i < 4; i++)
779 12049296 value = (value << 1) | get_cabac_bypass(&lc->cc);
780 3012324 return value;
781 }
782
783 4040595 int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc)
784 {
785 int ret;
786
2/2
✓ Branch 1 taken 3011904 times.
✓ Branch 2 taken 1028691 times.
4040595 if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE]))
787 3011904 return 4;
788
789 1028691 ret = get_cabac_bypass(&lc->cc) << 1;
790 1028691 ret |= get_cabac_bypass(&lc->cc);
791 1028691 return ret;
792 }
793
794 6130846 int ff_hevc_merge_idx_decode(HEVCLocalContext *lc)
795 {
796 6130846 int i = GET_CABAC(elem_offset[MERGE_IDX]);
797
798
2/2
✓ Branch 0 taken 2505968 times.
✓ Branch 1 taken 3624878 times.
6130846 if (i != 0) {
799
4/4
✓ Branch 0 taken 3858747 times.
✓ Branch 1 taken 297802 times.
✓ Branch 3 taken 1650581 times.
✓ Branch 4 taken 2208166 times.
4156549 while (i < lc->parent->sh.max_num_merge_cand-1 && get_cabac_bypass(&lc->cc))
800 1650581 i++;
801 }
802 6130846 return i;
803 }
804
805 4712445 int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
806 {
807 4712445 return GET_CABAC(elem_offset[MERGE_FLAG]);
808 }
809
810 1811353 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 1571506 times.
1811353 if (nPbW + nPbH == 12)
813 239847 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
814
2/2
✓ Branch 1 taken 404002 times.
✓ Branch 2 taken 1167504 times.
1571506 if (GET_CABAC(elem_offset[INTER_PRED_IDC] + lc->ct_depth))
815 404002 return PRED_BI;
816
817 1167504 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
818 }
819
820 2784138 int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
821 {
822 2784138 int i = 0;
823 2784138 int max = num_ref_idx_lx - 1;
824 2784138 int max_ctx = FFMIN(max, 2);
825
826
4/4
✓ Branch 0 taken 2779079 times.
✓ Branch 1 taken 947289 times.
✓ Branch 3 taken 942230 times.
✓ Branch 4 taken 1836849 times.
3726368 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 2588270 times.
2784138 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 2784138 return i;
834 }
835
836 2784138 int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
837 {
838 2784138 return GET_CABAC(elem_offset[MVP_LX_FLAG]);
839 }
840
841 2348491 int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc)
842 {
843 2348491 return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]);
844 }
845
846 5252688 static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc)
847 {
848 5252688 return GET_CABAC(elem_offset[ABS_MVD_GREATER0_FLAG]);
849 }
850
851 3593568 static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc)
852 {
853 3593568 return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1);
854 }
855
856 2578708 static av_always_inline int mvd_decode(HEVCLocalContext *lc)
857 {
858 2578708 int ret = 2;
859 2578708 int k = 1;
860
861
3/4
✓ Branch 0 taken 6315397 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3736689 times.
✓ Branch 4 taken 2578708 times.
6315397 while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc)) {
862 3736689 ret += 1U << k;
863 3736689 k++;
864 }
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2578708 times.
2578708 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 6315397 times.
✓ Branch 1 taken 2578708 times.
8894105 while (k--)
870 6315397 ret += get_cabac_bypass(&lc->cc) << k;
871 2578708 return get_cabac_bypass_sign(&lc->cc, -ret);
872 }
873
874 1014860 static av_always_inline int mvd_sign_flag_decode(HEVCLocalContext *lc)
875 {
876 1014860 return get_cabac_bypass_sign(&lc->cc, -1);
877 }
878
879 6830912 int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
880 {
881 6830912 return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
882 }
883
884 15063749 int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth)
885 {
886 15063749 return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
887 }
888
889 14579240 int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth)
890 {
891 14579240 return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
892 }
893
894 6499076 static int hevc_transform_skip_flag_decode(HEVCLocalContext *lc, int c_idx)
895 {
896 6499076 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 14110549 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 14110549 int i = 0;
928 14110549 int max = (log2_size << 1) - 1;
929 int ctx_offset, ctx_shift;
930
931
2/2
✓ Branch 0 taken 9667874 times.
✓ Branch 1 taken 4442675 times.
14110549 if (!c_idx) {
932 9667874 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2);
933 9667874 ctx_shift = (log2_size + 1) >> 2;
934 } else {
935 4442675 ctx_offset = 15;
936 4442675 ctx_shift = log2_size - 2;
937 }
938
4/4
✓ Branch 0 taken 34527700 times.
✓ Branch 1 taken 2096500 times.
✓ Branch 2 taken 22513651 times.
✓ Branch 3 taken 12014049 times.
71151900 while (i < max &&
939 34527700 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_X_PREFIX] + (i >> ctx_shift) + ctx_offset))
940 22513651 i++;
941 14110549 *last_scx_prefix = i;
942
943 14110549 i = 0;
944
4/4
✓ Branch 0 taken 29937417 times.
✓ Branch 1 taken 1642649 times.
✓ Branch 2 taken 17469517 times.
✓ Branch 3 taken 12467900 times.
61517483 while (i < max &&
945 29937417 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_Y_PREFIX] + (i >> ctx_shift) + ctx_offset))
946 17469517 i++;
947 14110549 *last_scy_prefix = i;
948 14110549 }
949
950 3066321 static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc,
951 int last_significant_coeff_prefix)
952 {
953 int i;
954 3066321 int length = (last_significant_coeff_prefix >> 1) - 1;
955 3066321 int value = get_cabac_bypass(&lc->cc);
956
957
2/2
✓ Branch 0 taken 857794 times.
✓ Branch 1 taken 3066321 times.
3924115 for (i = 1; i < length; i++)
958 857794 value = (value << 1) | get_cabac_bypass(&lc->cc);
959 3066321 return value;
960 }
961
962 8228026 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 1333005 times.
✓ Branch 1 taken 6895021 times.
8228026 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
967
968 8228026 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
969 }
970 168860508 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 168860508 int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
974 168860508 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
975 }
976
977 18345356 static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset)
978 {
979 18345356 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + offset);
980 }
981
982 90288914 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 21314772 times.
✓ Branch 1 taken 68974142 times.
90288914 if (c_idx > 0)
986 21314772 inc += 16;
987
988 90288914 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
989 }
990
991 10055852 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 2129141 times.
✓ Branch 1 taken 7926711 times.
10055852 if (c_idx > 0)
994 2129141 inc += 4;
995
996 10055852 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
997 }
998
999 40687442 static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param)
1000 {
1001 40687442 int prefix = 0;
1002 40687442 int suffix = 0;
1003 int last_coeff_abs_level_remaining;
1004 int i;
1005
1006
3/4
✓ Branch 0 taken 89306454 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 48619012 times.
✓ Branch 4 taken 40687442 times.
89306454 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc))
1007 48619012 prefix++;
1008
1009
2/2
✓ Branch 0 taken 34757581 times.
✓ Branch 1 taken 5929861 times.
40687442 if (prefix < 3) {
1010
2/2
✓ Branch 0 taken 36980904 times.
✓ Branch 1 taken 34757581 times.
71738485 for (i = 0; i < rc_rice_param; i++)
1011 36980904 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1012 34757581 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1013 } else {
1014 5929861 int prefix_minus3 = prefix - 3;
1015
1016
2/4
✓ Branch 0 taken 5929861 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5929861 times.
5929861 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 19434265 times.
✓ Branch 1 taken 5929861 times.
25364126 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1022 19434265 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1023 5929861 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1024 5929861 << rc_rice_param) + suffix;
1025 }
1026 40687442 return last_coeff_abs_level_remaining;
1027 }
1028
1029 21518729 static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb)
1030 {
1031 int i;
1032 21518729 int ret = 0;
1033
1034
2/2
✓ Branch 0 taken 97256272 times.
✓ Branch 1 taken 21518729 times.
118775001 for (i = 0; i < nb; i++)
1035 97256272 ret = (ret << 1) | get_cabac_bypass(&lc->cc);
1036 21518729 return ret;
1037 }
1038
1039 14110549 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 14110549 const HEVCContext *const s = lc->parent;
1049 14110549 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 14110549 int num_coeff = 0;
1055 14110549 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 14110549 ptrdiff_t stride = s->frame->linesize[c_idx];
1063 14110549 int hshift = s->ps.sps->hshift[c_idx];
1064 14110549 int vshift = s->ps.sps->vshift[c_idx];
1065 14110549 uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1066 14110549 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1067
2/2
✓ Branch 0 taken 4442675 times.
✓ Branch 1 taken 9667874 times.
14110549 int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer);
1068 14110549 uint8_t significant_coeff_group_flag[8][8] = {{0}};
1069 14110549 int explicit_rdpcm_flag = 0;
1070 int explicit_rdpcm_dir_flag;
1071
1072 14110549 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 14110549 const uint8_t *scale_matrix = NULL;
1077 uint8_t dc_scale;
1078
2/2
✓ Branch 0 taken 9667874 times.
✓ Branch 1 taken 4442675 times.
14110549 int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode :
1079 lc->tu.intra_pred_mode_c;
1080
1081 14110549 memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1082
1083 // Derive QP for dequant
1084
2/2
✓ Branch 0 taken 13672183 times.
✓ Branch 1 taken 438366 times.
14110549 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 13672183 int qp_y = lc->qp_y;
1100
1101
2/2
✓ Branch 0 taken 11248602 times.
✓ Branch 1 taken 2423581 times.
13672183 if (s->ps.pps->transform_skip_enabled_flag &&
1102
2/2
✓ Branch 0 taken 6499076 times.
✓ Branch 1 taken 4749526 times.
11248602 log2_trafo_size <= s->ps.pps->log2_max_transform_skip_block_size) {
1103 6499076 transform_skip_flag = hevc_transform_skip_flag_decode(lc, c_idx);
1104 }
1105
1106
2/2
✓ Branch 0 taken 9392626 times.
✓ Branch 1 taken 4279557 times.
13672183 if (c_idx == 0) {
1107 9392626 qp = qp_y + s->ps.sps->qp_bd_offset;
1108 } else {
1109 int qp_i, offset;
1110
1111
2/2
✓ Branch 0 taken 2041941 times.
✓ Branch 1 taken 2237616 times.
4279557 if (c_idx == 1)
1112 2041941 offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset +
1113 2041941 lc->tu.cu_qp_offset_cb;
1114 else
1115 2237616 offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset +
1116 2237616 lc->tu.cu_qp_offset_cr;
1117
1118 4279557 qp_i = av_clip(qp_y + offset, - s->ps.sps->qp_bd_offset, 57);
1119
2/2
✓ Branch 0 taken 2413452 times.
✓ Branch 1 taken 1866105 times.
4279557 if (s->ps.sps->chroma_format_idc == 1) {
1120
2/2
✓ Branch 0 taken 729892 times.
✓ Branch 1 taken 1683560 times.
2413452 if (qp_i < 30)
1121 729892 qp = qp_i;
1122
2/2
✓ Branch 0 taken 9230 times.
✓ Branch 1 taken 1674330 times.
1683560 else if (qp_i > 43)
1123 9230 qp = qp_i - 6;
1124 else
1125 1674330 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 4279557 qp += s->ps.sps->qp_bd_offset;
1134 }
1135
1136 13672183 shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
1137 13672183 add = 1 << (shift-1);
1138 13672183 scale = level_scale[rem6[qp]] << (div6[qp]);
1139 13672183 scale_m = 16; // default when no custom scaling lists.
1140 13672183 dc_scale = 16;
1141
1142
6/6
✓ Branch 0 taken 484014 times.
✓ Branch 1 taken 13188169 times.
✓ Branch 2 taken 26526 times.
✓ Branch 3 taken 457488 times.
✓ Branch 4 taken 14292 times.
✓ Branch 5 taken 12234 times.
13672183 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 4616047 times.
✓ Branch 1 taken 9494502 times.
✓ Branch 2 taken 225511 times.
✓ Branch 3 taken 4390536 times.
✓ Branch 4 taken 190277 times.
✓ Branch 5 taken 35234 times.
14110549 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 14110549 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 1794019 times.
✓ Branch 1 taken 12316530 times.
14110549 if (last_significant_coeff_x > 3) {
1172 1794019 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x);
1173 1794019 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1174 1794019 (2 + (last_significant_coeff_x & 1)) +
1175 suffix;
1176 }
1177
1178
2/2
✓ Branch 0 taken 1272302 times.
✓ Branch 1 taken 12838247 times.
14110549 if (last_significant_coeff_y > 3) {
1179 1272302 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y);
1180 1272302 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1181 1272302 (2 + (last_significant_coeff_y & 1)) +
1182 suffix;
1183 }
1184
1185
2/2
✓ Branch 0 taken 1811135 times.
✓ Branch 1 taken 12299414 times.
14110549 if (scan_idx == SCAN_VERT)
1186 1811135 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1187
1188 14110549 x_cg_last_sig = last_significant_coeff_x >> 2;
1189 14110549 y_cg_last_sig = last_significant_coeff_y >> 2;
1190
1191
3/3
✓ Branch 0 taken 10274106 times.
✓ Branch 1 taken 2025308 times.
✓ Branch 2 taken 1811135 times.
14110549 switch (scan_idx) {
1192 10274106 case SCAN_DIAG: {
1193 10274106 int last_x_c = last_significant_coeff_x & 3;
1194 10274106 int last_y_c = last_significant_coeff_y & 3;
1195
1196 10274106 scan_x_off = ff_hevc_diag_scan4x4_x;
1197 10274106 scan_y_off = ff_hevc_diag_scan4x4_y;
1198 10274106 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1199
2/2
✓ Branch 0 taken 5243050 times.
✓ Branch 1 taken 5031056 times.
10274106 if (trafo_size == 4) {
1200 5243050 scan_x_cg = scan_1x1;
1201 5243050 scan_y_cg = scan_1x1;
1202
2/2
✓ Branch 0 taken 2770421 times.
✓ Branch 1 taken 2260635 times.
5031056 } else if (trafo_size == 8) {
1203 2770421 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1204 2770421 scan_x_cg = diag_scan2x2_x;
1205 2770421 scan_y_cg = diag_scan2x2_y;
1206
2/2
✓ Branch 0 taken 1677245 times.
✓ Branch 1 taken 583390 times.
2260635 } else if (trafo_size == 16) {
1207 1677245 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1208 1677245 scan_x_cg = ff_hevc_diag_scan4x4_x;
1209 1677245 scan_y_cg = ff_hevc_diag_scan4x4_y;
1210 } else { // trafo_size == 32
1211 583390 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1212 583390 scan_x_cg = ff_hevc_diag_scan8x8_x;
1213 583390 scan_y_cg = ff_hevc_diag_scan8x8_y;
1214 }
1215 10274106 break;
1216 }
1217 2025308 case SCAN_HORIZ:
1218 2025308 scan_x_cg = horiz_scan2x2_x;
1219 2025308 scan_y_cg = horiz_scan2x2_y;
1220 2025308 scan_x_off = horiz_scan4x4_x;
1221 2025308 scan_y_off = horiz_scan4x4_y;
1222 2025308 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1223 2025308 break;
1224 1811135 default: //SCAN_VERT
1225 1811135 scan_x_cg = horiz_scan2x2_y;
1226 1811135 scan_y_cg = horiz_scan2x2_x;
1227 1811135 scan_x_off = horiz_scan4x4_y;
1228 1811135 scan_y_off = horiz_scan4x4_x;
1229 1811135 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1230 1811135 break;
1231 }
1232 14110549 num_coeff++;
1233 14110549 num_last_subset = (num_coeff - 1) >> 4;
1234
1235
2/2
✓ Branch 0 taken 24697687 times.
✓ Branch 1 taken 14110549 times.
38808236 for (i = num_last_subset; i >= 0; i--) {
1236 int n, m;
1237 int x_cg, y_cg, x_c, y_c, pos;
1238 24697687 int implicit_non_zero_coeff = 0;
1239 int64_t trans_coeff_level;
1240 24697687 int prev_sig = 0;
1241 24697687 int offset = i << 4;
1242 24697687 int rice_init = 0;
1243
1244 uint8_t significant_coeff_flag_idx[16];
1245 24697687 uint8_t nb_significant_coeff_flag = 0;
1246
1247 24697687 x_cg = scan_x_cg[i];
1248 24697687 y_cg = scan_y_cg[i];
1249
1250
4/4
✓ Branch 0 taken 10587138 times.
✓ Branch 1 taken 14110549 times.
✓ Branch 2 taken 8228026 times.
✓ Branch 3 taken 2359112 times.
24697687 if ((i < num_last_subset) && (i > 0)) {
1251 8228026 int ctx_cg = 0;
1252
2/2
✓ Branch 0 taken 7519896 times.
✓ Branch 1 taken 708130 times.
8228026 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1253 7519896 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1254
2/2
✓ Branch 0 taken 6937386 times.
✓ Branch 1 taken 1290640 times.
8228026 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1255 6937386 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1256
1257 8228026 significant_coeff_group_flag[x_cg][y_cg] =
1258 8228026 significant_coeff_group_flag_decode(lc, c_idx, ctx_cg);
1259 8228026 implicit_non_zero_coeff = 1;
1260 } else {
1261 16469661 significant_coeff_group_flag[x_cg][y_cg] =
1262
5/6
✓ Branch 0 taken 14782123 times.
✓ Branch 1 taken 1687538 times.
✓ Branch 2 taken 671574 times.
✓ Branch 3 taken 14110549 times.
✓ Branch 4 taken 2359112 times.
✗ Branch 5 not taken.
18828773 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1263
1/2
✓ Branch 0 taken 2359112 times.
✗ Branch 1 not taken.
2359112 (x_cg == 0 && y_cg == 0));
1264 }
1265
1266 24697687 last_scan_pos = num_coeff - offset - 1;
1267
1268
2/2
✓ Branch 0 taken 14110549 times.
✓ Branch 1 taken 10587138 times.
24697687 if (i == num_last_subset) {
1269 14110549 n_end = last_scan_pos - 1;
1270 14110549 significant_coeff_flag_idx[0] = last_scan_pos;
1271 14110549 nb_significant_coeff_flag = 1;
1272 } else {
1273 10587138 n_end = 15;
1274 }
1275
1276
2/2
✓ Branch 0 taken 14797225 times.
✓ Branch 1 taken 9900462 times.
24697687 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1277 14797225 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1278
2/2
✓ Branch 0 taken 14371638 times.
✓ Branch 1 taken 10326049 times.
24697687 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1279
2/2
✓ Branch 0 taken 5120560 times.
✓ Branch 1 taken 9251078 times.
14371638 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1280
1281
4/4
✓ Branch 0 taken 21603368 times.
✓ Branch 1 taken 3094319 times.
✓ Branch 2 taken 18415089 times.
✓ Branch 3 taken 3188279 times.
24697687 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 18415089 int scf_offset = 0;
1291
4/4
✓ Branch 0 taken 1017071 times.
✓ Branch 1 taken 17398018 times.
✓ Branch 2 taken 886760 times.
✓ Branch 3 taken 130311 times.
18415089 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 4400666 times.
✓ Branch 1 taken 13884112 times.
18284778 if (c_idx != 0)
1301 4400666 scf_offset = 27;
1302
2/2
✓ Branch 0 taken 6328987 times.
✓ Branch 1 taken 11955791 times.
18284778 if (log2_trafo_size == 2) {
1303 6328987 ctx_idx_map_p = &ctx_idx_map[0];
1304 } else {
1305 11955791 ctx_idx_map_p = &ctx_idx_map[(prev_sig + 1) << 4];
1306
2/2
✓ Branch 0 taken 9570921 times.
✓ Branch 1 taken 2384870 times.
11955791 if (c_idx == 0) {
1307
4/4
✓ Branch 0 taken 5777142 times.
✓ Branch 1 taken 3793779 times.
✓ Branch 2 taken 2006712 times.
✓ Branch 3 taken 3770430 times.
9570921 if ((x_cg > 0 || y_cg > 0))
1308 5800491 scf_offset += 3;
1309
2/2
✓ Branch 0 taken 3596825 times.
✓ Branch 1 taken 5974096 times.
9570921 if (log2_trafo_size == 3) {
1310
2/2
✓ Branch 0 taken 2498315 times.
✓ Branch 1 taken 1098510 times.
3596825 scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15;
1311 } else {
1312 5974096 scf_offset += 21;
1313 }
1314 } else {
1315
2/2
✓ Branch 0 taken 1134288 times.
✓ Branch 1 taken 1250582 times.
2384870 if (log2_trafo_size == 3)
1316 1134288 scf_offset += 9;
1317 else
1318 1250582 scf_offset += 12;
1319 }
1320 }
1321 }
1322
2/2
✓ Branch 0 taken 168860508 times.
✓ Branch 1 taken 18415089 times.
187275597 for (n = n_end; n > 0; n--) {
1323 168860508 x_c = scan_x_off[n];
1324 168860508 y_c = scan_y_off[n];
1325
2/2
✓ Branch 1 taken 81514899 times.
✓ Branch 2 taken 87345609 times.
168860508 if (significant_coeff_flag_decode(lc, x_c, y_c, scf_offset, ctx_idx_map_p)) {
1326 81514899 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1327 81514899 nb_significant_coeff_flag++;
1328 81514899 implicit_non_zero_coeff = 0;
1329 }
1330 }
1331
2/2
✓ Branch 0 taken 18345356 times.
✓ Branch 1 taken 69733 times.
18415089 if (implicit_non_zero_coeff == 0) {
1332
4/4
✓ Branch 0 taken 1014653 times.
✓ Branch 1 taken 17330703 times.
✓ Branch 2 taken 884736 times.
✓ Branch 3 taken 129917 times.
18345356 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 11164293 times.
✓ Branch 1 taken 7051146 times.
18215439 if (i == 0) {
1341
2/2
✓ Branch 0 taken 8083621 times.
✓ Branch 1 taken 3080672 times.
11164293 if (c_idx == 0)
1342 8083621 scf_offset = 0;
1343 else
1344 3080672 scf_offset = 27;
1345 } else {
1346 7051146 scf_offset = 2 + scf_offset;
1347 }
1348 }
1349
2/2
✓ Branch 1 taken 12378702 times.
✓ Branch 2 taken 5966654 times.
18345356 if (significant_coeff_flag_decode_0(lc, c_idx, scf_offset) == 1) {
1350 12378702 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1351 12378702 nb_significant_coeff_flag++;
1352 }
1353 } else {
1354 69733 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1355 69733 nb_significant_coeff_flag++;
1356 }
1357 }
1358
1359 24697687 n_end = nb_significant_coeff_flag;
1360
1361
1362
2/2
✓ Branch 0 taken 21518729 times.
✓ Branch 1 taken 3178958 times.
24697687 if (n_end) {
1363 int first_nz_pos_in_cg;
1364 int last_nz_pos_in_cg;
1365 21518729 int c_rice_param = 0;
1366 21518729 int first_greater1_coeff_idx = -1;
1367 uint8_t coeff_abs_level_greater1_flag[8];
1368 uint16_t coeff_sign_flag;
1369 21518729 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 7492819 times.
✓ Branch 1 taken 14025910 times.
✓ Branch 2 taken 6083146 times.
✓ Branch 3 taken 1409673 times.
21518729 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1376
1377
2/2
✓ Branch 0 taken 1166629 times.
✓ Branch 1 taken 20352100 times.
21518729 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 7408180 times.
✓ Branch 1 taken 14110549 times.
✓ Branch 2 taken 3312702 times.
✓ Branch 3 taken 4095478 times.
21518729 if (!(i == num_last_subset) && greater1_ctx == 0)
1386 3312702 ctx_set++;
1387 21518729 greater1_ctx = 1;
1388 21518729 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1389
1390
2/2
✓ Branch 0 taken 90288914 times.
✓ Branch 1 taken 21518729 times.
111807643 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1391 90288914 int inc = (ctx_set << 2) + greater1_ctx;
1392 90288914 coeff_abs_level_greater1_flag[m] =
1393 90288914 coeff_abs_level_greater1_flag_decode(lc, c_idx, inc);
1394
2/2
✓ Branch 0 taken 29769448 times.
✓ Branch 1 taken 60519466 times.
90288914 if (coeff_abs_level_greater1_flag[m]) {
1395 29769448 greater1_ctx = 0;
1396
2/2
✓ Branch 0 taken 10055852 times.
✓ Branch 1 taken 19713596 times.
29769448 if (first_greater1_coeff_idx == -1)
1397 10055852 first_greater1_coeff_idx = m;
1398
4/4
✓ Branch 0 taken 47974271 times.
✓ Branch 1 taken 12545195 times.
✓ Branch 2 taken 29536505 times.
✓ Branch 3 taken 18437766 times.
60519466 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1399 29536505 greater1_ctx++;
1400 }
1401 }
1402 21518729 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1403
1404
2/2
✓ Branch 0 taken 20815242 times.
✓ Branch 1 taken 703487 times.
21518729 if (lc->cu.cu_transquant_bypass_flag ||
1405
2/2
✓ Branch 0 taken 13416507 times.
✓ Branch 1 taken 7398735 times.
20815242 (lc->cu.pred_mode == MODE_INTRA &&
1406
6/6
✓ Branch 0 taken 902776 times.
✓ Branch 1 taken 12513731 times.
✓ Branch 2 taken 92431 times.
✓ Branch 3 taken 810345 times.
✓ Branch 4 taken 47376 times.
✓ Branch 5 taken 45055 times.
13416507 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 20745189 times.
20770187 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1408 explicit_rdpcm_flag)
1409 773540 sign_hidden = 0;
1410 else
1411 20745189 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1412
1413
2/2
✓ Branch 0 taken 10055852 times.
✓ Branch 1 taken 11462877 times.
21518729 if (first_greater1_coeff_idx != -1) {
1414 10055852 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 19210866 times.
✓ Branch 1 taken 2307863 times.
✓ Branch 2 taken 8393255 times.
✓ Branch 3 taken 10817611 times.
21518729 if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden ) {
1417 10701118 coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1418 } else {
1419 10817611 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 108073883 times.
✓ Branch 1 taken 21518729 times.
129592612 for (m = 0; m < n_end; m++) {
1423 108073883 n = significant_coeff_flag_idx[m];
1424 108073883 GET_COORD(offset, n);
1425
2/2
✓ Branch 0 taken 90288914 times.
✓ Branch 1 taken 17784969 times.
108073883 if (m < 8) {
1426 90288914 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1427
4/4
✓ Branch 0 taken 10055852 times.
✓ Branch 1 taken 80233062 times.
✓ Branch 2 taken 22902473 times.
✓ Branch 3 taken 67386441 times.
90288914 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1428 22902473 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1429
1430 22902473 trans_coeff_level += last_coeff_abs_level_remaining;
1431
2/2
✓ Branch 0 taken 5949534 times.
✓ Branch 1 taken 16952939 times.
22902473 if (trans_coeff_level > (3 << c_rice_param))
1432
2/2
✓ Branch 0 taken 197734 times.
✓ Branch 1 taken 5751800 times.
5949534 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 21788348 times.
✓ Branch 2 taken 315677 times.
✓ Branch 3 taken 798448 times.
22902473 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 17784969 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1445
1446 17784969 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1447
2/2
✓ Branch 0 taken 2221070 times.
✓ Branch 1 taken 15563899 times.
17784969 if (trans_coeff_level > (3 << c_rice_param))
1448
2/2
✓ Branch 0 taken 89299 times.
✓ Branch 1 taken 2131771 times.
2221070 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 16846818 times.
✓ Branch 2 taken 36551 times.
✓ Branch 3 taken 901600 times.
17784969 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 100305911 times.
✓ Branch 1 taken 7767972 times.
✓ Branch 2 taken 78298676 times.
✓ Branch 3 taken 22007235 times.
108073883 if (s->ps.pps->sign_data_hiding_flag && sign_hidden) {
1460 78298676 sum_abs += trans_coeff_level;
1461
4/4
✓ Branch 0 taken 10817611 times.
✓ Branch 1 taken 67481065 times.
✓ Branch 2 taken 5401093 times.
✓ Branch 3 taken 5416518 times.
78298676 if (n == first_nz_pos_in_cg && (sum_abs&1))
1462 5401093 trans_coeff_level = -trans_coeff_level;
1463 }
1464
2/2
✓ Branch 0 taken 49133959 times.
✓ Branch 1 taken 58939924 times.
108073883 if (coeff_sign_flag >> 15)
1465 49133959 trans_coeff_level = -trans_coeff_level;
1466 108073883 coeff_sign_flag <<= 1;
1467
2/2
✓ Branch 0 taken 99389497 times.
✓ Branch 1 taken 8684386 times.
108073883 if(!lc->cu.cu_transquant_bypass_flag) {
1468
6/6
✓ Branch 0 taken 2778647 times.
✓ Branch 1 taken 96610850 times.
✓ Branch 2 taken 190401 times.
✓ Branch 3 taken 2588246 times.
✓ Branch 4 taken 30375 times.
✓ Branch 5 taken 160026 times.
99389497 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 99389497 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1482
2/2
✓ Branch 0 taken 50164234 times.
✓ Branch 1 taken 49225263 times.
99389497 if(trans_coeff_level < 0) {
1483
2/2
✓ Branch 0 taken 162681 times.
✓ Branch 1 taken 50001553 times.
50164234 if((~trans_coeff_level) & 0xFffffffffff8000)
1484 162681 trans_coeff_level = -32768;
1485 } else {
1486
2/2
✓ Branch 0 taken 162295 times.
✓ Branch 1 taken 49062968 times.
49225263 if(trans_coeff_level & 0xffffffffffff8000)
1487 162295 trans_coeff_level = 32767;
1488 }
1489 }
1490 108073883 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 13672183 times.
14110549 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 431869 times.
✓ Branch 1 taken 13240314 times.
13672183 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 355740 times.
507998 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 411043 times.
431869 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 431869 s->hevcdsp.dequant(coeffs, log2_trafo_size);
1513
1514
4/4
✓ Branch 0 taken 428779 times.
✓ Branch 1 taken 3090 times.
✓ Branch 2 taken 73039 times.
✓ Branch 3 taken 355740 times.
431869 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 8991148 times.
✓ Branch 1 taken 4249166 times.
✓ Branch 2 taken 6073349 times.
✓ Branch 3 taken 2917799 times.
✓ Branch 4 taken 3565525 times.
✓ Branch 5 taken 2507824 times.
13240314 } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) {
1522 3565525 s->hevcdsp.transform_4x4_luma(coeffs);
1523 } else {
1524 9674789 int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1525
2/2
✓ Branch 0 taken 2171649 times.
✓ Branch 1 taken 7503140 times.
9674789 if (max_xy == 0)
1526 2171649 s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1527 else {
1528 7503140 int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1529
2/2
✓ Branch 0 taken 5199568 times.
✓ Branch 1 taken 2303572 times.
7503140 if (max_xy < 4)
1530 5199568 col_limit = FFMIN(4, col_limit);
1531
2/2
✓ Branch 0 taken 1721175 times.
✓ Branch 1 taken 582397 times.
2303572 else if (max_xy < 8)
1532 1721175 col_limit = FFMIN(8, col_limit);
1533
2/2
✓ Branch 0 taken 249127 times.
✓ Branch 1 taken 333270 times.
582397 else if (max_xy < 12)
1534 249127 col_limit = FFMIN(24, col_limit);
1535 7503140 s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1536 }
1537 }
1538 }
1539
2/2
✓ Branch 0 taken 269268 times.
✓ Branch 1 taken 13841281 times.
14110549 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 14110549 s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride);
1547 14110549 }
1548
1549 2626344 void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
1550 {
1551 2626344 int x = abs_mvd_greater0_flag_decode(lc);
1552 2626344 int y = abs_mvd_greater0_flag_decode(lc);
1553
1554
2/2
✓ Branch 0 taken 1832999 times.
✓ Branch 1 taken 793345 times.
2626344 if (x)
1555 1832999 x += abs_mvd_greater1_flag_decode(lc);
1556
2/2
✓ Branch 0 taken 1760569 times.
✓ Branch 1 taken 865775 times.
2626344 if (y)
1557 1760569 y += abs_mvd_greater1_flag_decode(lc);
1558
1559
3/4
✓ Branch 0 taken 1330963 times.
✓ Branch 1 taken 502036 times.
✓ Branch 2 taken 793345 times.
✗ Branch 3 not taken.
2626344 switch (x) {
1560 1330963 case 2: lc->pu.mvd.x = mvd_decode(lc); break;
1561 502036 case 1: lc->pu.mvd.x = mvd_sign_flag_decode(lc); break;
1562 793345 case 0: lc->pu.mvd.x = 0; break;
1563 }
1564
1565
3/4
✓ Branch 0 taken 1247745 times.
✓ Branch 1 taken 512824 times.
✓ Branch 2 taken 865775 times.
✗ Branch 3 not taken.
2626344 switch (y) {
1566 1247745 case 2: lc->pu.mvd.y = mvd_decode(lc); break;
1567 512824 case 1: lc->pu.mvd.y = mvd_sign_flag_decode(lc); break;
1568 865775 case 0: lc->pu.mvd.y = 0; break;
1569 }
1570 2626344 }
1571
1572