FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_cabac.c
Date: 2023-03-22 23:59:29
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 1413673 void ff_hevc_save_states(HEVCLocalContext *lc, int ctb_addr_ts)
451 {
452 1413673 const HEVCContext *const s = lc->parent;
453
454
2/2
✓ Branch 0 taken 102312 times.
✓ Branch 1 taken 1311361 times.
1413673 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 1413673 }
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 27499 static int cabac_init_decoder(HEVCLocalContext *lc)
479 {
480 27499 GetBitContext *gb = &lc->gb;
481 27499 skip_bits(gb, 1);
482 27499 align_get_bits(gb);
483 82497 return ff_init_cabac_decoder(&lc->cc,
484 27499 gb->buffer + get_bits_count(gb) / 8,
485 27499 (get_bits_left(gb) + 7) / 8);
486 }
487
488 24780 static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s)
489 {
490 24780 int init_type = 2 - s->sh.slice_type;
491 int i;
492
493
4/4
✓ Branch 0 taken 6488 times.
✓ Branch 1 taken 18292 times.
✓ Branch 2 taken 6345 times.
✓ Branch 3 taken 143 times.
24780 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 4931220 times.
✓ Branch 1 taken 24780 times.
4956000 for (i = 0; i < HEVC_CONTEXTS; i++) {
497 4931220 int init_value = init_values[init_type][i];
498 4931220 int m = (init_value >> 4) * 5 - 45;
499 4931220 int n = ((init_value & 15) << 3) - 16;
500 4931220 int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
501
502 4931220 pre ^= pre >> 31;
503
2/2
✓ Branch 0 taken 506252 times.
✓ Branch 1 taken 4424968 times.
4931220 if (pre > 124)
504 506252 pre = 124 + (pre & 1);
505 4931220 lc->cabac_state[i] = pre;
506 }
507
508
2/2
✓ Branch 0 taken 99120 times.
✓ Branch 1 taken 24780 times.
123900 for (i = 0; i < 4; i++)
509 99120 lc->stat_coeff[i] = 0;
510 24780 }
511
512 1413674 int ff_hevc_cabac_init(HEVCLocalContext *lc, int ctb_addr_ts)
513 {
514 1413674 const HEVCContext *const s = lc->parent;
515
516
2/2
✓ Branch 0 taken 27499 times.
✓ Branch 1 taken 1386175 times.
1413674 if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
517 27499 int ret = cabac_init_decoder(lc);
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27499 times.
27499 if (ret < 0)
519 return ret;
520
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 19552 times.
27499 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 19861 cabac_init_state(lc, s);
524
525
2/2
✓ Branch 0 taken 17989 times.
✓ Branch 1 taken 9510 times.
27499 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 1098976 times.
1386175 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 1288443 times.
1386175 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 1413674 return 0;
567 }
568
569 #define GET_CABAC(ctx) get_cabac(&lc->cc, &lc->cabac_state[ctx])
570
571 815804 int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc)
572 {
573 815804 return GET_CABAC(elem_offset[SAO_MERGE_FLAG]);
574 }
575
576 374123 int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc)
577 {
578
2/2
✓ Branch 1 taken 211484 times.
✓ Branch 2 taken 162639 times.
374123 if (!GET_CABAC(elem_offset[SAO_TYPE_IDX]))
579 211484 return 0;
580
581
2/2
✓ Branch 1 taken 53947 times.
✓ Branch 2 taken 108692 times.
162639 if (!get_cabac_bypass(&lc->cc))
582 53947 return SAO_BAND;
583 108692 return SAO_EDGE;
584 }
585
586 77904 int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc)
587 {
588 int i;
589 77904 int value = get_cabac_bypass(&lc->cc);
590
591
2/2
✓ Branch 0 taken 311616 times.
✓ Branch 1 taken 77904 times.
389520 for (i = 0; i < 4; i++)
592 311616 value = (value << 1) | get_cabac_bypass(&lc->cc);
593 77904 return value;
594 }
595
596 870224 int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc)
597 {
598 870224 int i = 0;
599 870224 int length = (1 << (FFMIN(lc->parent->ps.sps->bit_depth, 10) - 5)) - 1;
600
601
4/4
✓ Branch 0 taken 2459475 times.
✓ Branch 1 taken 127355 times.
✓ Branch 3 taken 1716606 times.
✓ Branch 4 taken 742869 times.
2586830 while (i < length && get_cabac_bypass(&lc->cc))
602 1716606 i++;
603 870224 return i;
604 }
605
606 243063 int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc)
607 {
608 243063 return get_cabac_bypass(&lc->cc);
609 }
610
611 108692 int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc)
612 {
613 108692 int ret = get_cabac_bypass(&lc->cc) << 1;
614 108692 ret |= get_cabac_bypass(&lc->cc);
615 108692 return ret;
616 }
617
618 1413673 int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc)
619 {
620 1413673 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 8087416 int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, int x0, int y0, int x_cb, int y_cb)
629 {
630 8087416 const HEVCContext *const s = lc->parent;
631 8087416 int min_cb_width = s->ps.sps->min_cb_width;
632 8087416 int inc = 0;
633 8087416 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
634 8087416 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
635
636
4/4
✓ Branch 0 taken 736797 times.
✓ Branch 1 taken 7350619 times.
✓ Branch 2 taken 532126 times.
✓ Branch 3 taken 204671 times.
8087416 if (lc->ctb_left_flag || x0b)
637 7882745 inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
638
4/4
✓ Branch 0 taken 1679232 times.
✓ Branch 1 taken 6408184 times.
✓ Branch 2 taken 1222201 times.
✓ Branch 3 taken 457031 times.
8087416 if (lc->ctb_up_flag || y0b)
639 7630385 inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1);
640
641 8087416 return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
642 }
643
644 605368 int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc)
645 {
646 605368 int prefix_val = 0;
647 605368 int suffix_val = 0;
648 605368 int inc = 0;
649
650
4/4
✓ Branch 0 taken 1476886 times.
✓ Branch 1 taken 106004 times.
✓ Branch 3 taken 977522 times.
✓ Branch 4 taken 499364 times.
1582890 while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
651 977522 prefix_val++;
652 977522 inc = 1;
653 }
654
2/2
✓ Branch 0 taken 106004 times.
✓ Branch 1 taken 499364 times.
605368 if (prefix_val >= 5) {
655 106004 int k = 0;
656
3/4
✓ Branch 0 taken 360090 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 254086 times.
✓ Branch 4 taken 106004 times.
360090 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 106004 times.
106004 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 106004 times.
360090 while (k--)
666 254086 suffix_val += get_cabac_bypass(&lc->cc) << k;
667 }
668 605368 return prefix_val + suffix_val;
669 }
670
671 369825 int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc)
672 {
673 369825 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 4197485 int ff_hevc_pred_mode_decode(HEVCLocalContext *lc)
693 {
694 4197485 return GET_CABAC(elem_offset[PRED_MODE_FLAG]);
695 }
696
697 7482355 int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, int ct_depth, int x0, int y0)
698 {
699 7482355 const HEVCContext *const s = lc->parent;
700 7482355 const HEVCSPS *const sps = s->ps.sps;
701 7482355 int inc = 0, depth_left = 0, depth_top = 0;
702 7482355 int x0b = av_mod_uintp2(x0, sps->log2_ctb_size);
703 7482355 int y0b = av_mod_uintp2(y0, sps->log2_ctb_size);
704 7482355 int x_cb = x0 >> sps->log2_min_cb_size;
705 7482355 int y_cb = y0 >> sps->log2_min_cb_size;
706
707
4/4
✓ Branch 0 taken 694601 times.
✓ Branch 1 taken 6787754 times.
✓ Branch 2 taken 396612 times.
✓ Branch 3 taken 297989 times.
7482355 if (lc->ctb_left_flag || x0b)
708 7184366 depth_left = s->tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1];
709
4/4
✓ Branch 0 taken 1490715 times.
✓ Branch 1 taken 5991640 times.
✓ Branch 2 taken 839131 times.
✓ Branch 3 taken 651584 times.
7482355 if (lc->ctb_up_flag || y0b)
710 6830771 depth_top = s->tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb];
711
712 7482355 inc += (depth_left > ct_depth);
713 7482355 inc += (depth_top > ct_depth);
714
715 7482355 return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc);
716 }
717
718 6163910 int ff_hevc_part_mode_decode(HEVCLocalContext *lc, int log2_cb_size)
719 {
720
2/2
✓ Branch 1 taken 3591533 times.
✓ Branch 2 taken 2572377 times.
6163910 if (GET_CABAC(elem_offset[PART_MODE])) // 1
721 3591533 return PART_2Nx2N;
722
2/2
✓ Branch 0 taken 1941827 times.
✓ Branch 1 taken 630550 times.
2572377 if (log2_cb_size == lc->parent->ps.sps->log2_min_cb_size) {
723
2/2
✓ Branch 0 taken 1192637 times.
✓ Branch 1 taken 749190 times.
1941827 if (lc->cu.pred_mode == MODE_INTRA) // 0
724 1192637 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 7534538 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc)
761 {
762 7534538 return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]);
763 }
764
765 4518151 int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc)
766 {
767 4518151 int i = 0;
768
4/4
✓ Branch 0 taken 6730180 times.
✓ Branch 1 taken 938249 times.
✓ Branch 3 taken 3150278 times.
✓ Branch 4 taken 3579902 times.
7668429 while (i < 2 && get_cabac_bypass(&lc->cc))
769 3150278 i++;
770 4518151 return i;
771 }
772
773 3016387 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc)
774 {
775 int i;
776 3016387 int value = get_cabac_bypass(&lc->cc);
777
778
2/2
✓ Branch 0 taken 12065548 times.
✓ Branch 1 taken 3016387 times.
15081935 for (i = 0; i < 4; i++)
779 12065548 value = (value << 1) | get_cabac_bypass(&lc->cc);
780 3016387 return value;
781 }
782
783 4057988 int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc)
784 {
785 int ret;
786
2/2
✓ Branch 1 taken 3029297 times.
✓ Branch 2 taken 1028691 times.
4057988 if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE]))
787 3029297 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 6147543 int ff_hevc_merge_idx_decode(HEVCLocalContext *lc)
795 {
796 6147543 int i = GET_CABAC(elem_offset[MERGE_IDX]);
797
798
2/2
✓ Branch 0 taken 2508492 times.
✓ Branch 1 taken 3639051 times.
6147543 if (i != 0) {
799
4/4
✓ Branch 0 taken 3861778 times.
✓ Branch 1 taken 297803 times.
✓ Branch 3 taken 1651089 times.
✓ Branch 4 taken 2210689 times.
4159581 while (i < lc->parent->sh.max_num_merge_cand-1 && get_cabac_bypass(&lc->cc))
800 1651089 i++;
801 }
802 6147543 return i;
803 }
804
805 4717553 int ff_hevc_merge_flag_decode(HEVCLocalContext *lc)
806 {
807 4717553 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 2787237 int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx)
821 {
822 2787237 int i = 0;
823 2787237 int max = num_ref_idx_lx - 1;
824 2787237 int max_ctx = FFMIN(max, 2);
825
826
4/4
✓ Branch 0 taken 2779079 times.
✓ Branch 1 taken 950388 times.
✓ Branch 3 taken 942230 times.
✓ Branch 4 taken 1836849 times.
3729467 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 2591369 times.
2787237 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 2787237 return i;
834 }
835
836 2787237 int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc)
837 {
838 2787237 return GET_CABAC(elem_offset[MVP_LX_FLAG]);
839 }
840
841 2351443 int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc)
842 {
843 2351443 return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]);
844 }
845
846 5258886 static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc)
847 {
848 5258886 return GET_CABAC(elem_offset[ABS_MVD_GREATER0_FLAG]);
849 }
850
851 3598549 static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc)
852 {
853 3598549 return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1);
854 }
855
856 2583689 static av_always_inline int mvd_decode(HEVCLocalContext *lc)
857 {
858 2583689 int ret = 2;
859 2583689 int k = 1;
860
861
3/4
✓ Branch 0 taken 6329274 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3745585 times.
✓ Branch 4 taken 2583689 times.
6329274 while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc)) {
862 3745585 ret += 1U << k;
863 3745585 k++;
864 }
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2583689 times.
2583689 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 6329274 times.
✓ Branch 1 taken 2583689 times.
8912963 while (k--)
870 6329274 ret += get_cabac_bypass(&lc->cc) << k;
871 2583689 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 6852640 int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size)
880 {
881 6852640 return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
882 }
883
884 15105201 int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth)
885 {
886 15105201 return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
887 }
888
889 14599211 int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth)
890 {
891 14599211 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 14134046 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 14134046 int i = 0;
928 14134046 int max = (log2_size << 1) - 1;
929 int ctx_offset, ctx_shift;
930
931
2/2
✓ Branch 0 taken 9685184 times.
✓ Branch 1 taken 4448862 times.
14134046 if (!c_idx) {
932 9685184 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2);
933 9685184 ctx_shift = (log2_size + 1) >> 2;
934 } else {
935 4448862 ctx_offset = 15;
936 4448862 ctx_shift = log2_size - 2;
937 }
938
4/4
✓ Branch 0 taken 34594002 times.
✓ Branch 1 taken 2096823 times.
✓ Branch 2 taken 22556779 times.
✓ Branch 3 taken 12037223 times.
71284827 while (i < max &&
939 34594002 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_X_PREFIX] + (i >> ctx_shift) + ctx_offset))
940 22556779 i++;
941 14134046 *last_scx_prefix = i;
942
943 14134046 i = 0;
944
4/4
✓ Branch 0 taken 29996719 times.
✓ Branch 1 taken 1642936 times.
✓ Branch 2 taken 17505609 times.
✓ Branch 3 taken 12491110 times.
61636374 while (i < max &&
945 29996719 GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_Y_PREFIX] + (i >> ctx_shift) + ctx_offset))
946 17505609 i++;
947 14134046 *last_scy_prefix = i;
948 14134046 }
949
950 3077057 static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc,
951 int last_significant_coeff_prefix)
952 {
953 int i;
954 3077057 int length = (last_significant_coeff_prefix >> 1) - 1;
955 3077057 int value = get_cabac_bypass(&lc->cc);
956
957
2/2
✓ Branch 0 taken 861371 times.
✓ Branch 1 taken 3077057 times.
3938428 for (i = 1; i < length; i++)
958 861371 value = (value << 1) | get_cabac_bypass(&lc->cc);
959 3077057 return value;
960 }
961
962 8258520 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 1336696 times.
✓ Branch 1 taken 6921824 times.
8258520 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
967
968 8258520 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
969 }
970 169244146 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 169244146 int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
974 169244146 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
975 }
976
977 18381074 static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset)
978 {
979 18381074 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + offset);
980 }
981
982 90399896 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 21322656 times.
✓ Branch 1 taken 69077240 times.
90399896 if (c_idx > 0)
986 21322656 inc += 16;
987
988 90399896 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
989 }
990
991 10064472 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 2129699 times.
✓ Branch 1 taken 7934773 times.
10064472 if (c_idx > 0)
994 2129699 inc += 4;
995
996 10064472 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
997 }
998
999 40701437 static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param)
1000 {
1001 40701437 int prefix = 0;
1002 40701437 int suffix = 0;
1003 int last_coeff_abs_level_remaining;
1004 int i;
1005
1006
3/4
✓ Branch 0 taken 89335890 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 48634453 times.
✓ Branch 4 taken 40701437 times.
89335890 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc))
1007 48634453 prefix++;
1008
1009
2/2
✓ Branch 0 taken 34769321 times.
✓ Branch 1 taken 5932116 times.
40701437 if (prefix < 3) {
1010
2/2
✓ Branch 0 taken 36985957 times.
✓ Branch 1 taken 34769321 times.
71755278 for (i = 0; i < rc_rice_param; i++)
1011 36985957 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1012 34769321 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1013 } else {
1014 5932116 int prefix_minus3 = prefix - 3;
1015
1016
2/4
✓ Branch 0 taken 5932116 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5932116 times.
5932116 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 19437467 times.
✓ Branch 1 taken 5932116 times.
25369583 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1022 19437467 suffix = (suffix << 1) | get_cabac_bypass(&lc->cc);
1023 5932116 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1024 5932116 << rc_rice_param) + suffix;
1025 }
1026 40701437 return last_coeff_abs_level_remaining;
1027 }
1028
1029 21562567 static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb)
1030 {
1031 int i;
1032 21562567 int ret = 0;
1033
1034
2/2
✓ Branch 0 taken 97370763 times.
✓ Branch 1 taken 21562567 times.
118933330 for (i = 0; i < nb; i++)
1035 97370763 ret = (ret << 1) | get_cabac_bypass(&lc->cc);
1036 21562567 return ret;
1037 }
1038
1039 14134046 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 14134046 const HEVCContext *const s = lc->parent;
1049 14134046 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 14134046 int num_coeff = 0;
1055 14134046 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 14134046 ptrdiff_t stride = s->frame->linesize[c_idx];
1063 14134046 int hshift = s->ps.sps->hshift[c_idx];
1064 14134046 int vshift = s->ps.sps->vshift[c_idx];
1065 14134046 uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1066 14134046 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1067
2/2
✓ Branch 0 taken 4448862 times.
✓ Branch 1 taken 9685184 times.
14134046 int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer);
1068 14134046 uint8_t significant_coeff_group_flag[8][8] = {{0}};
1069 14134046 int explicit_rdpcm_flag = 0;
1070 int explicit_rdpcm_dir_flag;
1071
1072 14134046 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 14134046 const uint8_t *scale_matrix = NULL;
1077 uint8_t dc_scale;
1078
2/2
✓ Branch 0 taken 9685184 times.
✓ Branch 1 taken 4448862 times.
14134046 int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode :
1079 lc->tu.intra_pred_mode_c;
1080
1081 14134046 memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1082
1083 // Derive QP for dequant
1084
2/2
✓ Branch 0 taken 13695680 times.
✓ Branch 1 taken 438366 times.
14134046 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 13695680 int qp_y = lc->qp_y;
1100
1101
2/2
✓ Branch 0 taken 11248602 times.
✓ Branch 1 taken 2447078 times.
13695680 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 9409936 times.
✓ Branch 1 taken 4285744 times.
13695680 if (c_idx == 0) {
1107 9409936 qp = qp_y + s->ps.sps->qp_bd_offset;
1108 } else {
1109 int qp_i, offset;
1110
1111
2/2
✓ Branch 0 taken 2045411 times.
✓ Branch 1 taken 2240333 times.
4285744 if (c_idx == 1)
1112 2045411 offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset +
1113 2045411 lc->tu.cu_qp_offset_cb;
1114 else
1115 2240333 offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset +
1116 2240333 lc->tu.cu_qp_offset_cr;
1117
1118 4285744 qp_i = av_clip(qp_y + offset, - s->ps.sps->qp_bd_offset, 57);
1119
2/2
✓ Branch 0 taken 2419639 times.
✓ Branch 1 taken 1866105 times.
4285744 if (s->ps.sps->chroma_format_idc == 1) {
1120
2/2
✓ Branch 0 taken 735765 times.
✓ Branch 1 taken 1683874 times.
2419639 if (qp_i < 30)
1121 735765 qp = qp_i;
1122
2/2
✓ Branch 0 taken 9230 times.
✓ Branch 1 taken 1674644 times.
1683874 else if (qp_i > 43)
1123 9230 qp = qp_i - 6;
1124 else
1125 1674644 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 4285744 qp += s->ps.sps->qp_bd_offset;
1134 }
1135
1136 13695680 shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
1137 13695680 add = 1 << (shift-1);
1138 13695680 scale = level_scale[rem6[qp]] << (div6[qp]);
1139 13695680 scale_m = 16; // default when no custom scaling lists.
1140 13695680 dc_scale = 16;
1141
1142
6/6
✓ Branch 0 taken 484014 times.
✓ Branch 1 taken 13211666 times.
✓ Branch 2 taken 26526 times.
✓ Branch 3 taken 457488 times.
✓ Branch 4 taken 14292 times.
✓ Branch 5 taken 12234 times.
13695680 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 4619226 times.
✓ Branch 1 taken 9514820 times.
✓ Branch 2 taken 225511 times.
✓ Branch 3 taken 4393715 times.
✓ Branch 4 taken 190277 times.
✓ Branch 5 taken 35234 times.
14134046 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 14134046 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 1800103 times.
✓ Branch 1 taken 12333943 times.
14134046 if (last_significant_coeff_x > 3) {
1172 1800103 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x);
1173 1800103 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1174 1800103 (2 + (last_significant_coeff_x & 1)) +
1175 suffix;
1176 }
1177
1178
2/2
✓ Branch 0 taken 1276954 times.
✓ Branch 1 taken 12857092 times.
14134046 if (last_significant_coeff_y > 3) {
1179 1276954 int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y);
1180 1276954 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1181 1276954 (2 + (last_significant_coeff_y & 1)) +
1182 suffix;
1183 }
1184
1185
2/2
✓ Branch 0 taken 1811708 times.
✓ Branch 1 taken 12322338 times.
14134046 if (scan_idx == SCAN_VERT)
1186 1811708 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1187
1188 14134046 x_cg_last_sig = last_significant_coeff_x >> 2;
1189 14134046 y_cg_last_sig = last_significant_coeff_y >> 2;
1190
1191
3/3
✓ Branch 0 taken 10295776 times.
✓ Branch 1 taken 2026562 times.
✓ Branch 2 taken 1811708 times.
14134046 switch (scan_idx) {
1192 10295776 case SCAN_DIAG: {
1193 10295776 int last_x_c = last_significant_coeff_x & 3;
1194 10295776 int last_y_c = last_significant_coeff_y & 3;
1195
1196 10295776 scan_x_off = ff_hevc_diag_scan4x4_x;
1197 10295776 scan_y_off = ff_hevc_diag_scan4x4_y;
1198 10295776 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1199
2/2
✓ Branch 0 taken 5243929 times.
✓ Branch 1 taken 5051847 times.
10295776 if (trafo_size == 4) {
1200 5243929 scan_x_cg = scan_1x1;
1201 5243929 scan_y_cg = scan_1x1;
1202
2/2
✓ Branch 0 taken 2775780 times.
✓ Branch 1 taken 2276067 times.
5051847 } else if (trafo_size == 8) {
1203 2775780 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1204 2775780 scan_x_cg = diag_scan2x2_x;
1205 2775780 scan_y_cg = diag_scan2x2_y;
1206
2/2
✓ Branch 0 taken 1687427 times.
✓ Branch 1 taken 588640 times.
2276067 } else if (trafo_size == 16) {
1207 1687427 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1208 1687427 scan_x_cg = ff_hevc_diag_scan4x4_x;
1209 1687427 scan_y_cg = ff_hevc_diag_scan4x4_y;
1210 } else { // trafo_size == 32
1211 588640 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1212 588640 scan_x_cg = ff_hevc_diag_scan8x8_x;
1213 588640 scan_y_cg = ff_hevc_diag_scan8x8_y;
1214 }
1215 10295776 break;
1216 }
1217 2026562 case SCAN_HORIZ:
1218 2026562 scan_x_cg = horiz_scan2x2_x;
1219 2026562 scan_y_cg = horiz_scan2x2_y;
1220 2026562 scan_x_off = horiz_scan4x4_x;
1221 2026562 scan_y_off = horiz_scan4x4_y;
1222 2026562 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1223 2026562 break;
1224 1811708 default: //SCAN_VERT
1225 1811708 scan_x_cg = horiz_scan2x2_y;
1226 1811708 scan_y_cg = horiz_scan2x2_x;
1227 1811708 scan_x_off = horiz_scan4x4_y;
1228 1811708 scan_y_off = horiz_scan4x4_x;
1229 1811708 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1230 1811708 break;
1231 }
1232 14134046 num_coeff++;
1233 14134046 num_last_subset = (num_coeff - 1) >> 4;
1234
1235
2/2
✓ Branch 0 taken 24759342 times.
✓ Branch 1 taken 14134046 times.
38893388 for (i = num_last_subset; i >= 0; i--) {
1236 int n, m;
1237 int x_cg, y_cg, x_c, y_c, pos;
1238 24759342 int implicit_non_zero_coeff = 0;
1239 int64_t trans_coeff_level;
1240 24759342 int prev_sig = 0;
1241 24759342 int offset = i << 4;
1242 24759342 int rice_init = 0;
1243
1244 uint8_t significant_coeff_flag_idx[16];
1245 24759342 uint8_t nb_significant_coeff_flag = 0;
1246
1247 24759342 x_cg = scan_x_cg[i];
1248 24759342 y_cg = scan_y_cg[i];
1249
1250
4/4
✓ Branch 0 taken 10625296 times.
✓ Branch 1 taken 14134046 times.
✓ Branch 2 taken 8258520 times.
✓ Branch 3 taken 2366776 times.
24759342 if ((i < num_last_subset) && (i > 0)) {
1251 8258520 int ctx_cg = 0;
1252
2/2
✓ Branch 0 taken 7549570 times.
✓ Branch 1 taken 708950 times.
8258520 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1253 7549570 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1254
2/2
✓ Branch 0 taken 6966929 times.
✓ Branch 1 taken 1291591 times.
8258520 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1255 6966929 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1256
1257 8258520 significant_coeff_group_flag[x_cg][y_cg] =
1258 8258520 significant_coeff_group_flag_decode(lc, c_idx, ctx_cg);
1259 8258520 implicit_non_zero_coeff = 1;
1260 } else {
1261 16500822 significant_coeff_group_flag[x_cg][y_cg] =
1262
5/6
✓ Branch 0 taken 14807247 times.
✓ Branch 1 taken 1693575 times.
✓ Branch 2 taken 673201 times.
✓ Branch 3 taken 14134046 times.
✓ Branch 4 taken 2366776 times.
✗ Branch 5 not taken.
18867598 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1263
1/2
✓ Branch 0 taken 2366776 times.
✗ Branch 1 not taken.
2366776 (x_cg == 0 && y_cg == 0));
1264 }
1265
1266 24759342 last_scan_pos = num_coeff - offset - 1;
1267
1268
2/2
✓ Branch 0 taken 14134046 times.
✓ Branch 1 taken 10625296 times.
24759342 if (i == num_last_subset) {
1269 14134046 n_end = last_scan_pos - 1;
1270 14134046 significant_coeff_flag_idx[0] = last_scan_pos;
1271 14134046 nb_significant_coeff_flag = 1;
1272 } else {
1273 10625296 n_end = 15;
1274 }
1275
1276
2/2
✓ Branch 0 taken 14856333 times.
✓ Branch 1 taken 9903009 times.
24759342 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1277 14856333 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1278
2/2
✓ Branch 0 taken 14430737 times.
✓ Branch 1 taken 10328605 times.
24759342 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1279
2/2
✓ Branch 0 taken 5134399 times.
✓ Branch 1 taken 9296338 times.
14430737 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1280
1281
4/4
✓ Branch 0 taken 21647676 times.
✓ Branch 1 taken 3111666 times.
✓ Branch 2 taken 18451495 times.
✓ Branch 3 taken 3196181 times.
24759342 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 18451495 int scf_offset = 0;
1291
4/4
✓ Branch 0 taken 1017071 times.
✓ Branch 1 taken 17434424 times.
✓ Branch 2 taken 886760 times.
✓ Branch 3 taken 130311 times.
18451495 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 4403002 times.
✓ Branch 1 taken 13918182 times.
18321184 if (c_idx != 0)
1301 4403002 scf_offset = 27;
1302
2/2
✓ Branch 0 taken 6329296 times.
✓ Branch 1 taken 11991888 times.
18321184 if (log2_trafo_size == 2) {
1303 6329296 ctx_idx_map_p = &ctx_idx_map[0];
1304 } else {
1305 11991888 ctx_idx_map_p = &ctx_idx_map[(prev_sig + 1) << 4];
1306
2/2
✓ Branch 0 taken 9604781 times.
✓ Branch 1 taken 2387107 times.
11991888 if (c_idx == 0) {
1307
4/4
✓ Branch 0 taken 5798399 times.
✓ Branch 1 taken 3806382 times.
✓ Branch 2 taken 2013204 times.
✓ Branch 3 taken 3785195 times.
9604781 if ((x_cg > 0 || y_cg > 0))
1308 5819586 scf_offset += 3;
1309
2/2
✓ Branch 0 taken 3600668 times.
✓ Branch 1 taken 6004113 times.
9604781 if (log2_trafo_size == 3) {
1310
2/2
✓ Branch 0 taken 2500716 times.
✓ Branch 1 taken 1099952 times.
3600668 scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15;
1311 } else {
1312 6004113 scf_offset += 21;
1313 }
1314 } else {
1315
2/2
✓ Branch 0 taken 1135043 times.
✓ Branch 1 taken 1252064 times.
2387107 if (log2_trafo_size == 3)
1316 1135043 scf_offset += 9;
1317 else
1318 1252064 scf_offset += 12;
1319 }
1320 }
1321 }
1322
2/2
✓ Branch 0 taken 169244146 times.
✓ Branch 1 taken 18451495 times.
187695641 for (n = n_end; n > 0; n--) {
1323 169244146 x_c = scan_x_off[n];
1324 169244146 y_c = scan_y_off[n];
1325
2/2
✓ Branch 1 taken 81590269 times.
✓ Branch 2 taken 87653877 times.
169244146 if (significant_coeff_flag_decode(lc, x_c, y_c, scf_offset, ctx_idx_map_p)) {
1326 81590269 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1327 81590269 nb_significant_coeff_flag++;
1328 81590269 implicit_non_zero_coeff = 0;
1329 }
1330 }
1331
2/2
✓ Branch 0 taken 18381074 times.
✓ Branch 1 taken 70421 times.
18451495 if (implicit_non_zero_coeff == 0) {
1332
4/4
✓ Branch 0 taken 1014653 times.
✓ Branch 1 taken 17366421 times.
✓ Branch 2 taken 884736 times.
✓ Branch 3 taken 129917 times.
18381074 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 11181286 times.
✓ Branch 1 taken 7069871 times.
18251157 if (i == 0) {
1341
2/2
✓ Branch 0 taken 8098596 times.
✓ Branch 1 taken 3082690 times.
11181286 if (c_idx == 0)
1342 8098596 scf_offset = 0;
1343 else
1344 3082690 scf_offset = 27;
1345 } else {
1346 7069871 scf_offset = 2 + scf_offset;
1347 }
1348 }
1349
2/2
✓ Branch 1 taken 12393638 times.
✓ Branch 2 taken 5987436 times.
18381074 if (significant_coeff_flag_decode_0(lc, c_idx, scf_offset) == 1) {
1350 12393638 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1351 12393638 nb_significant_coeff_flag++;
1352 }
1353 } else {
1354 70421 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1355 70421 nb_significant_coeff_flag++;
1356 }
1357 }
1358
1359 24759342 n_end = nb_significant_coeff_flag;
1360
1361
1362
2/2
✓ Branch 0 taken 21562567 times.
✓ Branch 1 taken 3196775 times.
24759342 if (n_end) {
1363 int first_nz_pos_in_cg;
1364 int last_nz_pos_in_cg;
1365 21562567 int c_rice_param = 0;
1366 21562567 int first_greater1_coeff_idx = -1;
1367 uint8_t coeff_abs_level_greater1_flag[8];
1368 uint16_t coeff_sign_flag;
1369 21562567 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 7513630 times.
✓ Branch 1 taken 14048937 times.
✓ Branch 2 taken 6103588 times.
✓ Branch 3 taken 1410042 times.
21562567 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1376
1377
2/2
✓ Branch 0 taken 1166629 times.
✓ Branch 1 taken 20395938 times.
21562567 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 7428521 times.
✓ Branch 1 taken 14134046 times.
✓ Branch 2 taken 3313621 times.
✓ Branch 3 taken 4114900 times.
21562567 if (!(i == num_last_subset) && greater1_ctx == 0)
1386 3313621 ctx_set++;
1387 21562567 greater1_ctx = 1;
1388 21562567 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1389
1390
2/2
✓ Branch 0 taken 90399896 times.
✓ Branch 1 taken 21562567 times.
111962463 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1391 90399896 int inc = (ctx_set << 2) + greater1_ctx;
1392 90399896 coeff_abs_level_greater1_flag[m] =
1393 90399896 coeff_abs_level_greater1_flag_decode(lc, c_idx, inc);
1394
2/2
✓ Branch 0 taken 29786469 times.
✓ Branch 1 taken 60613427 times.
90399896 if (coeff_abs_level_greater1_flag[m]) {
1395 29786469 greater1_ctx = 0;
1396
2/2
✓ Branch 0 taken 10064472 times.
✓ Branch 1 taken 19721997 times.
29786469 if (first_greater1_coeff_idx == -1)
1397 10064472 first_greater1_coeff_idx = m;
1398
4/4
✓ Branch 0 taken 48061891 times.
✓ Branch 1 taken 12551536 times.
✓ Branch 2 taken 29600257 times.
✓ Branch 3 taken 18461634 times.
60613427 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1399 29600257 greater1_ctx++;
1400 }
1401 }
1402 21562567 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1403
1404
2/2
✓ Branch 0 taken 20859080 times.
✓ Branch 1 taken 703487 times.
21562567 if (lc->cu.cu_transquant_bypass_flag ||
1405
2/2
✓ Branch 0 taken 13456178 times.
✓ Branch 1 taken 7402902 times.
20859080 (lc->cu.pred_mode == MODE_INTRA &&
1406
6/6
✓ Branch 0 taken 902776 times.
✓ Branch 1 taken 12553402 times.
✓ Branch 2 taken 92431 times.
✓ Branch 3 taken 810345 times.
✓ Branch 4 taken 47376 times.
✓ Branch 5 taken 45055 times.
13456178 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 20789027 times.
20814025 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1408 explicit_rdpcm_flag)
1409 773540 sign_hidden = 0;
1410 else
1411 20789027 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1412
1413
2/2
✓ Branch 0 taken 10064472 times.
✓ Branch 1 taken 11498095 times.
21562567 if (first_greater1_coeff_idx != -1) {
1414 10064472 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 2351701 times.
✓ Branch 2 taken 8393255 times.
✓ Branch 3 taken 10817611 times.
21562567 if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden ) {
1417 10744956 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 108188374 times.
✓ Branch 1 taken 21562567 times.
129750941 for (m = 0; m < n_end; m++) {
1423 108188374 n = significant_coeff_flag_idx[m];
1424 108188374 GET_COORD(offset, n);
1425
2/2
✓ Branch 0 taken 90399896 times.
✓ Branch 1 taken 17788478 times.
108188374 if (m < 8) {
1426 90399896 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1427
4/4
✓ Branch 0 taken 10064472 times.
✓ Branch 1 taken 80335424 times.
✓ Branch 2 taken 22912959 times.
✓ Branch 3 taken 67486937 times.
90399896 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1428 22912959 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1429
1430 22912959 trans_coeff_level += last_coeff_abs_level_remaining;
1431
2/2
✓ Branch 0 taken 5952341 times.
✓ Branch 1 taken 16960618 times.
22912959 if (trans_coeff_level > (3 << c_rice_param))
1432
2/2
✓ Branch 0 taken 197734 times.
✓ Branch 1 taken 5754607 times.
5952341 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 21798834 times.
✓ Branch 2 taken 315677 times.
✓ Branch 3 taken 798448 times.
22912959 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 17788478 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param);
1445
1446 17788478 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1447
2/2
✓ Branch 0 taken 2221972 times.
✓ Branch 1 taken 15566506 times.
17788478 if (trans_coeff_level > (3 << c_rice_param))
1448
2/2
✓ Branch 0 taken 89299 times.
✓ Branch 1 taken 2132673 times.
2221972 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 16850327 times.
✓ Branch 2 taken 36551 times.
✓ Branch 3 taken 901600 times.
17788478 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 7882463 times.
✓ Branch 2 taken 78298676 times.
✓ Branch 3 taken 22007235 times.
108188374 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 49192294 times.
✓ Branch 1 taken 58996080 times.
108188374 if (coeff_sign_flag >> 15)
1465 49192294 trans_coeff_level = -trans_coeff_level;
1466 108188374 coeff_sign_flag <<= 1;
1467
2/2
✓ Branch 0 taken 99503988 times.
✓ Branch 1 taken 8684386 times.
108188374 if(!lc->cu.cu_transquant_bypass_flag) {
1468
6/6
✓ Branch 0 taken 2778647 times.
✓ Branch 1 taken 96725341 times.
✓ Branch 2 taken 190401 times.
✓ Branch 3 taken 2588246 times.
✓ Branch 4 taken 30375 times.
✓ Branch 5 taken 160026 times.
99503988 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 99503988 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1482
2/2
✓ Branch 0 taken 50222569 times.
✓ Branch 1 taken 49281419 times.
99503988 if(trans_coeff_level < 0) {
1483
2/2
✓ Branch 0 taken 162681 times.
✓ Branch 1 taken 50059888 times.
50222569 if((~trans_coeff_level) & 0xFffffffffff8000)
1484 162681 trans_coeff_level = -32768;
1485 } else {
1486
2/2
✓ Branch 0 taken 162295 times.
✓ Branch 1 taken 49119124 times.
49281419 if(trans_coeff_level & 0xffffffffffff8000)
1487 162295 trans_coeff_level = 32767;
1488 }
1489 }
1490 108188374 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 13695680 times.
14134046 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 13263811 times.
13695680 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 9011466 times.
✓ Branch 1 taken 4252345 times.
✓ Branch 2 taken 6087792 times.
✓ Branch 3 taken 2923674 times.
✓ Branch 4 taken 3565819 times.
✓ Branch 5 taken 2521973 times.
13263811 } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) {
1522 3565819 s->hevcdsp.transform_4x4_luma(coeffs);
1523 } else {
1524 9697992 int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1525
2/2
✓ Branch 0 taken 2178069 times.
✓ Branch 1 taken 7519923 times.
9697992 if (max_xy == 0)
1526 2178069 s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs);
1527 else {
1528 7519923 int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1529
2/2
✓ Branch 0 taken 5208687 times.
✓ Branch 1 taken 2311236 times.
7519923 if (max_xy < 4)
1530 5208687 col_limit = FFMIN(4, col_limit);
1531
2/2
✓ Branch 0 taken 1726244 times.
✓ Branch 1 taken 584992 times.
2311236 else if (max_xy < 8)
1532 1726244 col_limit = FFMIN(8, col_limit);
1533
2/2
✓ Branch 0 taken 250643 times.
✓ Branch 1 taken 334349 times.
584992 else if (max_xy < 12)
1534 250643 col_limit = FFMIN(24, col_limit);
1535 7519923 s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit);
1536 }
1537 }
1538 }
1539
2/2
✓ Branch 0 taken 269268 times.
✓ Branch 1 taken 13864778 times.
14134046 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 14134046 s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride);
1547 14134046 }
1548
1549 2629443 void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
1550 {
1551 2629443 int x = abs_mvd_greater0_flag_decode(lc);
1552 2629443 int y = abs_mvd_greater0_flag_decode(lc);
1553
1554
2/2
✓ Branch 0 taken 1835512 times.
✓ Branch 1 taken 793931 times.
2629443 if (x)
1555 1835512 x += abs_mvd_greater1_flag_decode(lc);
1556
2/2
✓ Branch 0 taken 1763037 times.
✓ Branch 1 taken 866406 times.
2629443 if (y)
1557 1763037 y += abs_mvd_greater1_flag_decode(lc);
1558
1559
3/4
✓ Branch 0 taken 1333476 times.
✓ Branch 1 taken 502036 times.
✓ Branch 2 taken 793931 times.
✗ Branch 3 not taken.
2629443 switch (x) {
1560 1333476 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 793931 case 0: lc->pu.mvd.x = 0; break;
1563 }
1564
1565
3/4
✓ Branch 0 taken 1250213 times.
✓ Branch 1 taken 512824 times.
✓ Branch 2 taken 866406 times.
✗ Branch 3 not taken.
2629443 switch (y) {
1566 1250213 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 866406 case 0: lc->pu.mvd.y = 0; break;
1569 }
1570 2629443 }
1571
1572