/src/ffmpeg/libavcodec/hevc/cabac.c
Line | Count | Source |
1 | | /* |
2 | | * HEVC CABAC decoding |
3 | | * |
4 | | * Copyright (C) 2012 - 2013 Guillaume Martres |
5 | | * Copyright (C) 2012 - 2013 Gildas Cocherel |
6 | | * |
7 | | * This file is part of FFmpeg. |
8 | | * |
9 | | * FFmpeg is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * FFmpeg is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with FFmpeg; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | | */ |
23 | | |
24 | | #include "libavutil/attributes.h" |
25 | | #include "libavutil/common.h" |
26 | | |
27 | | #include "libavcodec/cabac_functions.h" |
28 | | #include "data.h" |
29 | | #include "hevc.h" |
30 | | #include "hevcdec.h" |
31 | | |
32 | 554M | #define CABAC_MAX_BIN 31 |
33 | | |
34 | | // ELEM(NAME, NUM_BINS) |
35 | | #define CABAC_ELEMS(ELEM) \ |
36 | | ELEM(SAO_MERGE_FLAG, 1) \ |
37 | | ELEM(SAO_TYPE_IDX, 1) \ |
38 | | ELEM(SAO_EO_CLASS, 0) \ |
39 | | ELEM(SAO_BAND_POSITION, 0) \ |
40 | | ELEM(SAO_OFFSET_ABS, 0) \ |
41 | | ELEM(SAO_OFFSET_SIGN, 0) \ |
42 | | ELEM(END_OF_SLICE_FLAG, 0) \ |
43 | | ELEM(SPLIT_CODING_UNIT_FLAG, 3) \ |
44 | | ELEM(CU_TRANSQUANT_BYPASS_FLAG, 1) \ |
45 | | ELEM(SKIP_FLAG, 3) \ |
46 | | ELEM(CU_QP_DELTA, 3) \ |
47 | | ELEM(PRED_MODE_FLAG, 1) \ |
48 | | ELEM(PART_MODE, 4) \ |
49 | | ELEM(PCM_FLAG, 0) \ |
50 | | ELEM(PREV_INTRA_LUMA_PRED_FLAG, 1) \ |
51 | | ELEM(MPM_IDX, 0) \ |
52 | | ELEM(REM_INTRA_LUMA_PRED_MODE, 0) \ |
53 | | ELEM(INTRA_CHROMA_PRED_MODE, 2) \ |
54 | | ELEM(MERGE_FLAG, 1) \ |
55 | | ELEM(MERGE_IDX, 1) \ |
56 | | ELEM(INTER_PRED_IDC, 5) \ |
57 | | ELEM(REF_IDX_L0, 2) \ |
58 | | ELEM(REF_IDX_L1, 2) \ |
59 | | ELEM(ABS_MVD_GREATER0_FLAG, 2) \ |
60 | | ELEM(ABS_MVD_GREATER1_FLAG, 2) \ |
61 | | ELEM(ABS_MVD_MINUS2, 0) \ |
62 | | ELEM(MVD_SIGN_FLAG, 0) \ |
63 | | ELEM(MVP_LX_FLAG, 1) \ |
64 | | ELEM(NO_RESIDUAL_DATA_FLAG, 1) \ |
65 | | ELEM(SPLIT_TRANSFORM_FLAG, 3) \ |
66 | | ELEM(CBF_LUMA, 2) \ |
67 | | ELEM(CBF_CB_CR, 5) \ |
68 | | ELEM(TRANSFORM_SKIP_FLAG, 2) \ |
69 | | ELEM(EXPLICIT_RDPCM_FLAG, 2) \ |
70 | | ELEM(EXPLICIT_RDPCM_DIR_FLAG, 2) \ |
71 | | ELEM(LAST_SIGNIFICANT_COEFF_X_PREFIX, 18) \ |
72 | | ELEM(LAST_SIGNIFICANT_COEFF_Y_PREFIX, 18) \ |
73 | | ELEM(LAST_SIGNIFICANT_COEFF_X_SUFFIX, 0) \ |
74 | | ELEM(LAST_SIGNIFICANT_COEFF_Y_SUFFIX, 0) \ |
75 | | ELEM(SIGNIFICANT_COEFF_GROUP_FLAG, 4) \ |
76 | | ELEM(SIGNIFICANT_COEFF_FLAG, 44) \ |
77 | | ELEM(COEFF_ABS_LEVEL_GREATER1_FLAG, 24) \ |
78 | | ELEM(COEFF_ABS_LEVEL_GREATER2_FLAG, 6) \ |
79 | | ELEM(COEFF_ABS_LEVEL_REMAINING, 0) \ |
80 | | ELEM(COEFF_SIGN_FLAG, 0) \ |
81 | | ELEM(LOG2_RES_SCALE_ABS, 8) \ |
82 | | ELEM(RES_SCALE_SIGN_FLAG, 2) \ |
83 | | ELEM(CU_CHROMA_QP_OFFSET_FLAG, 1) \ |
84 | | ELEM(CU_CHROMA_QP_OFFSET_IDX, 1) \ |
85 | | |
86 | | /** |
87 | | * Offset to ctxIdx 0 in init_values and states. |
88 | | */ |
89 | | enum { |
90 | | #define OFFSET(NAME, NUM_BINS) \ |
91 | | NAME ## _OFFSET, \ |
92 | | NAME ## _END = NAME ## _OFFSET + NUM_BINS - 1, |
93 | | CABAC_ELEMS(OFFSET) |
94 | | }; |
95 | | |
96 | | #define CNU 154 |
97 | | /** |
98 | | * Indexed by init_type |
99 | | */ |
100 | | static const uint8_t init_values[3][HEVC_CONTEXTS] = { |
101 | | { // sao_merge_flag |
102 | | 153, |
103 | | // sao_type_idx |
104 | | 200, |
105 | | // split_coding_unit_flag |
106 | | 139, 141, 157, |
107 | | // cu_transquant_bypass_flag |
108 | | 154, |
109 | | // skip_flag |
110 | | CNU, CNU, CNU, |
111 | | // cu_qp_delta |
112 | | 154, 154, 154, |
113 | | // pred_mode |
114 | | CNU, |
115 | | // part_mode |
116 | | 184, CNU, CNU, CNU, |
117 | | // prev_intra_luma_pred_mode |
118 | | 184, |
119 | | // intra_chroma_pred_mode |
120 | | 63, 139, |
121 | | // merge_flag |
122 | | CNU, |
123 | | // merge_idx |
124 | | CNU, |
125 | | // inter_pred_idc |
126 | | CNU, CNU, CNU, CNU, CNU, |
127 | | // ref_idx_l0 |
128 | | CNU, CNU, |
129 | | // ref_idx_l1 |
130 | | CNU, CNU, |
131 | | // abs_mvd_greater1_flag |
132 | | CNU, CNU, |
133 | | // abs_mvd_greater1_flag |
134 | | CNU, CNU, |
135 | | // mvp_lx_flag |
136 | | CNU, |
137 | | // no_residual_data_flag |
138 | | CNU, |
139 | | // split_transform_flag |
140 | | 153, 138, 138, |
141 | | // cbf_luma |
142 | | 111, 141, |
143 | | // cbf_cb, cbf_cr |
144 | | 94, 138, 182, 154, 154, |
145 | | // transform_skip_flag |
146 | | 139, 139, |
147 | | // explicit_rdpcm_flag |
148 | | 139, 139, |
149 | | // explicit_rdpcm_dir_flag |
150 | | 139, 139, |
151 | | // last_significant_coeff_x_prefix |
152 | | 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, |
153 | | 79, 108, 123, 63, |
154 | | // last_significant_coeff_y_prefix |
155 | | 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, |
156 | | 79, 108, 123, 63, |
157 | | // significant_coeff_group_flag |
158 | | 91, 171, 134, 141, |
159 | | // significant_coeff_flag |
160 | | 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153, |
161 | | 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140, |
162 | | 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111, |
163 | | 141, 111, |
164 | | // coeff_abs_level_greater1_flag |
165 | | 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107, |
166 | | 122, 152, 140, 179, 166, 182, 140, 227, 122, 197, |
167 | | // coeff_abs_level_greater2_flag |
168 | | 138, 153, 136, 167, 152, 152, |
169 | | // log2_res_scale_abs |
170 | | 154, 154, 154, 154, 154, 154, 154, 154, |
171 | | // res_scale_sign_flag |
172 | | 154, 154, |
173 | | // cu_chroma_qp_offset_flag |
174 | | 154, |
175 | | // cu_chroma_qp_offset_idx |
176 | | 154, |
177 | | }, |
178 | | { // sao_merge_flag |
179 | | 153, |
180 | | // sao_type_idx |
181 | | 185, |
182 | | // split_coding_unit_flag |
183 | | 107, 139, 126, |
184 | | // cu_transquant_bypass_flag |
185 | | 154, |
186 | | // skip_flag |
187 | | 197, 185, 201, |
188 | | // cu_qp_delta |
189 | | 154, 154, 154, |
190 | | // pred_mode |
191 | | 149, |
192 | | // part_mode |
193 | | 154, 139, 154, 154, |
194 | | // prev_intra_luma_pred_mode |
195 | | 154, |
196 | | // intra_chroma_pred_mode |
197 | | 152, 139, |
198 | | // merge_flag |
199 | | 110, |
200 | | // merge_idx |
201 | | 122, |
202 | | // inter_pred_idc |
203 | | 95, 79, 63, 31, 31, |
204 | | // ref_idx_l0 |
205 | | 153, 153, |
206 | | // ref_idx_l1 |
207 | | 153, 153, |
208 | | // abs_mvd_greater1_flag |
209 | | 140, 198, |
210 | | // abs_mvd_greater1_flag |
211 | | 140, 198, |
212 | | // mvp_lx_flag |
213 | | 168, |
214 | | // no_residual_data_flag |
215 | | 79, |
216 | | // split_transform_flag |
217 | | 124, 138, 94, |
218 | | // cbf_luma |
219 | | 153, 111, |
220 | | // cbf_cb, cbf_cr |
221 | | 149, 107, 167, 154, 154, |
222 | | // transform_skip_flag |
223 | | 139, 139, |
224 | | // explicit_rdpcm_flag |
225 | | 139, 139, |
226 | | // explicit_rdpcm_dir_flag |
227 | | 139, 139, |
228 | | // last_significant_coeff_x_prefix |
229 | | 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, |
230 | | 94, 108, 123, 108, |
231 | | // last_significant_coeff_y_prefix |
232 | | 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, |
233 | | 94, 108, 123, 108, |
234 | | // significant_coeff_group_flag |
235 | | 121, 140, 61, 154, |
236 | | // significant_coeff_flag |
237 | | 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153, |
238 | | 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, |
239 | | 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140, |
240 | | 140, 140, |
241 | | // coeff_abs_level_greater1_flag |
242 | | 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121, |
243 | | 136, 137, 169, 194, 166, 167, 154, 167, 137, 182, |
244 | | // coeff_abs_level_greater2_flag |
245 | | 107, 167, 91, 122, 107, 167, |
246 | | // log2_res_scale_abs |
247 | | 154, 154, 154, 154, 154, 154, 154, 154, |
248 | | // res_scale_sign_flag |
249 | | 154, 154, |
250 | | // cu_chroma_qp_offset_flag |
251 | | 154, |
252 | | // cu_chroma_qp_offset_idx |
253 | | 154, |
254 | | }, |
255 | | { // sao_merge_flag |
256 | | 153, |
257 | | // sao_type_idx |
258 | | 160, |
259 | | // split_coding_unit_flag |
260 | | 107, 139, 126, |
261 | | // cu_transquant_bypass_flag |
262 | | 154, |
263 | | // skip_flag |
264 | | 197, 185, 201, |
265 | | // cu_qp_delta |
266 | | 154, 154, 154, |
267 | | // pred_mode |
268 | | 134, |
269 | | // part_mode |
270 | | 154, 139, 154, 154, |
271 | | // prev_intra_luma_pred_mode |
272 | | 183, |
273 | | // intra_chroma_pred_mode |
274 | | 152, 139, |
275 | | // merge_flag |
276 | | 154, |
277 | | // merge_idx |
278 | | 137, |
279 | | // inter_pred_idc |
280 | | 95, 79, 63, 31, 31, |
281 | | // ref_idx_l0 |
282 | | 153, 153, |
283 | | // ref_idx_l1 |
284 | | 153, 153, |
285 | | // abs_mvd_greater1_flag |
286 | | 169, 198, |
287 | | // abs_mvd_greater1_flag |
288 | | 169, 198, |
289 | | // mvp_lx_flag |
290 | | 168, |
291 | | // no_residual_data_flag |
292 | | 79, |
293 | | // split_transform_flag |
294 | | 224, 167, 122, |
295 | | // cbf_luma |
296 | | 153, 111, |
297 | | // cbf_cb, cbf_cr |
298 | | 149, 92, 167, 154, 154, |
299 | | // transform_skip_flag |
300 | | 139, 139, |
301 | | // explicit_rdpcm_flag |
302 | | 139, 139, |
303 | | // explicit_rdpcm_dir_flag |
304 | | 139, 139, |
305 | | // last_significant_coeff_x_prefix |
306 | | 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, |
307 | | 79, 108, 123, 93, |
308 | | // last_significant_coeff_y_prefix |
309 | | 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, |
310 | | 79, 108, 123, 93, |
311 | | // significant_coeff_group_flag |
312 | | 121, 140, 61, 154, |
313 | | // significant_coeff_flag |
314 | | 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153, |
315 | | 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, |
316 | | 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140, |
317 | | 140, 140, |
318 | | // coeff_abs_level_greater1_flag |
319 | | 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121, |
320 | | 136, 122, 169, 208, 166, 167, 154, 152, 167, 182, |
321 | | // coeff_abs_level_greater2_flag |
322 | | 107, 167, 91, 107, 107, 167, |
323 | | // log2_res_scale_abs |
324 | | 154, 154, 154, 154, 154, 154, 154, 154, |
325 | | // res_scale_sign_flag |
326 | | 154, 154, |
327 | | // cu_chroma_qp_offset_flag |
328 | | 154, |
329 | | // cu_chroma_qp_offset_idx |
330 | | 154, |
331 | | }, |
332 | | }; |
333 | | |
334 | | static const uint8_t scan_1x1[1] = { |
335 | | 0, |
336 | | }; |
337 | | |
338 | | static const uint8_t horiz_scan2x2_x[4] = { |
339 | | 0, 1, 0, 1, |
340 | | }; |
341 | | |
342 | | static const uint8_t horiz_scan2x2_y[4] = { |
343 | | 0, 0, 1, 1 |
344 | | }; |
345 | | |
346 | | static const uint8_t horiz_scan4x4_x[16] = { |
347 | | 0, 1, 2, 3, |
348 | | 0, 1, 2, 3, |
349 | | 0, 1, 2, 3, |
350 | | 0, 1, 2, 3, |
351 | | }; |
352 | | |
353 | | static const uint8_t horiz_scan4x4_y[16] = { |
354 | | 0, 0, 0, 0, |
355 | | 1, 1, 1, 1, |
356 | | 2, 2, 2, 2, |
357 | | 3, 3, 3, 3, |
358 | | }; |
359 | | |
360 | | static const uint8_t horiz_scan8x8_inv[8][8] = { |
361 | | { 0, 1, 2, 3, 16, 17, 18, 19, }, |
362 | | { 4, 5, 6, 7, 20, 21, 22, 23, }, |
363 | | { 8, 9, 10, 11, 24, 25, 26, 27, }, |
364 | | { 12, 13, 14, 15, 28, 29, 30, 31, }, |
365 | | { 32, 33, 34, 35, 48, 49, 50, 51, }, |
366 | | { 36, 37, 38, 39, 52, 53, 54, 55, }, |
367 | | { 40, 41, 42, 43, 56, 57, 58, 59, }, |
368 | | { 44, 45, 46, 47, 60, 61, 62, 63, }, |
369 | | }; |
370 | | |
371 | | static const uint8_t diag_scan2x2_x[4] = { |
372 | | 0, 0, 1, 1, |
373 | | }; |
374 | | |
375 | | static const uint8_t diag_scan2x2_y[4] = { |
376 | | 0, 1, 0, 1, |
377 | | }; |
378 | | |
379 | | static const uint8_t diag_scan2x2_inv[2][2] = { |
380 | | { 0, 2, }, |
381 | | { 1, 3, }, |
382 | | }; |
383 | | |
384 | | static const uint8_t diag_scan4x4_inv[4][4] = { |
385 | | { 0, 2, 5, 9, }, |
386 | | { 1, 4, 8, 12, }, |
387 | | { 3, 7, 11, 14, }, |
388 | | { 6, 10, 13, 15, }, |
389 | | }; |
390 | | |
391 | | static const uint8_t diag_scan8x8_inv[8][8] = { |
392 | | { 0, 2, 5, 9, 14, 20, 27, 35, }, |
393 | | { 1, 4, 8, 13, 19, 26, 34, 42, }, |
394 | | { 3, 7, 12, 18, 25, 33, 41, 48, }, |
395 | | { 6, 11, 17, 24, 32, 40, 47, 53, }, |
396 | | { 10, 16, 23, 31, 39, 46, 52, 57, }, |
397 | | { 15, 22, 30, 38, 45, 51, 56, 60, }, |
398 | | { 21, 29, 37, 44, 50, 55, 59, 62, }, |
399 | | { 28, 36, 43, 49, 54, 58, 61, 63, }, |
400 | | }; |
401 | | |
402 | | void ff_hevc_save_states(HEVCLocalContext *lc, const HEVCPPS *pps, |
403 | | int ctb_addr_ts) |
404 | 4.01M | { |
405 | 4.01M | const HEVCSPS *const sps = pps->sps; |
406 | 4.01M | if (pps->entropy_coding_sync_enabled_flag && |
407 | 182k | (ctb_addr_ts % sps->ctb_width == 2 || |
408 | 165k | (sps->ctb_width == 2 && |
409 | 22.8k | ctb_addr_ts % sps->ctb_width == 0))) { |
410 | 22.8k | memcpy(lc->common_cabac_state->state, lc->cabac_state, HEVC_CONTEXTS); |
411 | 22.8k | if (sps->persistent_rice_adaptation_enabled) { |
412 | 3.23k | memcpy(lc->common_cabac_state->stat_coeff, lc->stat_coeff, HEVC_STAT_COEFFS); |
413 | 3.23k | } |
414 | 22.8k | } |
415 | 4.01M | } |
416 | | |
417 | | static void load_states(HEVCLocalContext *lc, const HEVCSPS *sps) |
418 | 13.4k | { |
419 | 13.4k | memcpy(lc->cabac_state, lc->common_cabac_state->state, HEVC_CONTEXTS); |
420 | 13.4k | if (sps->persistent_rice_adaptation_enabled) { |
421 | 1.93k | memcpy(lc->stat_coeff, lc->common_cabac_state->stat_coeff, HEVC_STAT_COEFFS); |
422 | 1.93k | } |
423 | 13.4k | } |
424 | | |
425 | | static int cabac_reinit(HEVCLocalContext *lc) |
426 | 35.9k | { |
427 | 35.9k | return skip_bytes(&lc->cc, 0) == NULL ? AVERROR_INVALIDDATA : 0; |
428 | 35.9k | } |
429 | | |
430 | | static void cabac_init_state(HEVCLocalContext *lc, const HEVCContext *s) |
431 | 66.3k | { |
432 | 66.3k | int init_type = 2 - s->sh.slice_type; |
433 | 66.3k | int i; |
434 | | |
435 | 66.3k | if (s->sh.cabac_init_flag && s->sh.slice_type != HEVC_SLICE_I) |
436 | 7.53k | init_type ^= 3; |
437 | | |
438 | 13.2M | for (i = 0; i < HEVC_CONTEXTS; i++) { |
439 | 13.1M | int init_value = init_values[init_type][i]; |
440 | 13.1M | int m = (init_value >> 4) * 5 - 45; |
441 | 13.1M | int n = ((init_value & 15) << 3) - 16; |
442 | 13.1M | int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127; |
443 | | |
444 | 13.1M | pre ^= pre >> 31; |
445 | 13.1M | if (pre > 124) |
446 | 1.33M | pre = 124 + (pre & 1); |
447 | 13.1M | lc->cabac_state[i] = pre; |
448 | 13.1M | } |
449 | | |
450 | 331k | for (i = 0; i < 4; i++) |
451 | 265k | lc->stat_coeff[i] = 0; |
452 | 66.3k | } |
453 | | |
454 | | int ff_hevc_cabac_init(HEVCLocalContext *lc, const HEVCPPS *pps, |
455 | | int ctb_addr_ts, const uint8_t *data, size_t size, |
456 | | int is_wpp) |
457 | 4.02M | { |
458 | 4.02M | const HEVCContext *const s = lc->parent; |
459 | 4.02M | const HEVCSPS *const sps = pps->sps; |
460 | | |
461 | 4.02M | if (ctb_addr_ts == pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) { |
462 | 73.9k | int ret = ff_init_cabac_decoder(&lc->cc, data, size); |
463 | 73.9k | if (ret < 0) |
464 | 464 | return ret; |
465 | 73.4k | if (s->sh.dependent_slice_segment_flag == 0 || |
466 | 19.3k | (pps->tiles_enabled_flag && |
467 | 4.75k | pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1])) |
468 | 54.2k | cabac_init_state(lc, s); |
469 | | |
470 | 73.4k | if (!s->sh.first_slice_in_pic_flag && |
471 | 23.2k | pps->entropy_coding_sync_enabled_flag) { |
472 | 2.69k | if (ctb_addr_ts % sps->ctb_width == 0) { |
473 | 866 | if (sps->ctb_width == 1) |
474 | 0 | cabac_init_state(lc, s); |
475 | 866 | else if (s->sh.dependent_slice_segment_flag == 1) |
476 | 651 | load_states(lc, sps); |
477 | 866 | } |
478 | 2.69k | } |
479 | 3.95M | } else { |
480 | 3.95M | if (pps->tiles_enabled_flag && |
481 | 1.43M | pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) { |
482 | 14.5k | int ret; |
483 | 14.5k | if (!is_wpp) |
484 | 14.5k | ret = cabac_reinit(lc); |
485 | 0 | else { |
486 | 0 | ret = ff_init_cabac_decoder(&lc->cc, data, size); |
487 | 0 | } |
488 | 14.5k | if (ret < 0) |
489 | 2.76k | return ret; |
490 | 11.7k | cabac_init_state(lc, s); |
491 | 11.7k | } |
492 | 3.95M | if (pps->entropy_coding_sync_enabled_flag) { |
493 | 179k | if (ctb_addr_ts % sps->ctb_width == 0) { |
494 | 21.3k | int ret; |
495 | 21.3k | get_cabac_terminate(&lc->cc); |
496 | 21.3k | if (!is_wpp) |
497 | 21.3k | ret = cabac_reinit(lc); |
498 | 0 | else { |
499 | 0 | ret = ff_init_cabac_decoder(&lc->cc, data, size); |
500 | 0 | } |
501 | 21.3k | if (ret < 0) |
502 | 8.39k | return ret; |
503 | | |
504 | 12.9k | if (sps->ctb_width == 1) |
505 | 204 | cabac_init_state(lc, s); |
506 | 12.7k | else |
507 | 12.7k | load_states(lc, sps); |
508 | 12.9k | } |
509 | 179k | } |
510 | 3.95M | } |
511 | 4.01M | return 0; |
512 | 4.02M | } |
513 | | |
514 | 4.99G | #define GET_CABAC(ctx) get_cabac(&lc->cc, &lc->cabac_state[ctx]) |
515 | | |
516 | | int ff_hevc_sao_merge_flag_decode(HEVCLocalContext *lc) |
517 | 4.93M | { |
518 | 4.93M | return GET_CABAC(SAO_MERGE_FLAG_OFFSET); |
519 | 4.93M | } |
520 | | |
521 | | int ff_hevc_sao_type_idx_decode(HEVCLocalContext *lc) |
522 | 4.57M | { |
523 | 4.57M | if (!GET_CABAC(SAO_TYPE_IDX_OFFSET)) |
524 | 620k | return 0; |
525 | | |
526 | 3.95M | if (!get_cabac_bypass(&lc->cc)) |
527 | 3.52M | return SAO_BAND; |
528 | 427k | return SAO_EDGE; |
529 | 3.95M | } |
530 | | |
531 | | int ff_hevc_sao_band_position_decode(HEVCLocalContext *lc) |
532 | 5.26M | { |
533 | 5.26M | int i; |
534 | 5.26M | int value = get_cabac_bypass(&lc->cc); |
535 | | |
536 | 26.3M | for (i = 0; i < 4; i++) |
537 | 21.0M | value = (value << 1) | get_cabac_bypass(&lc->cc); |
538 | 5.26M | return value; |
539 | 5.26M | } |
540 | | |
541 | | int ff_hevc_sao_offset_abs_decode(HEVCLocalContext *lc, int bit_depth) |
542 | 23.6M | { |
543 | 23.6M | int i = 0; |
544 | 23.6M | int length = (1 << (FFMIN(bit_depth, 10) - 5)) - 1; |
545 | | |
546 | 28.7M | while (i < length && get_cabac_bypass(&lc->cc)) |
547 | 5.16M | i++; |
548 | 23.6M | return i; |
549 | 23.6M | } |
550 | | |
551 | | int ff_hevc_sao_offset_sign_decode(HEVCLocalContext *lc) |
552 | 1.33M | { |
553 | 1.33M | return get_cabac_bypass(&lc->cc); |
554 | 1.33M | } |
555 | | |
556 | | int ff_hevc_sao_eo_class_decode(HEVCLocalContext *lc) |
557 | 427k | { |
558 | 427k | int ret = get_cabac_bypass(&lc->cc) << 1; |
559 | 427k | ret |= get_cabac_bypass(&lc->cc); |
560 | 427k | return ret; |
561 | 427k | } |
562 | | |
563 | | int ff_hevc_end_of_slice_flag_decode(HEVCLocalContext *lc) |
564 | 4.01M | { |
565 | 4.01M | return get_cabac_terminate(&lc->cc); |
566 | 4.01M | } |
567 | | |
568 | | int ff_hevc_cu_transquant_bypass_flag_decode(HEVCLocalContext *lc) |
569 | 7.73M | { |
570 | 7.73M | return GET_CABAC(CU_TRANSQUANT_BYPASS_FLAG_OFFSET); |
571 | 7.73M | } |
572 | | |
573 | | int ff_hevc_skip_flag_decode(HEVCLocalContext *lc, uint8_t *skip_flag, |
574 | | int x0, int y0, int x_cb, int y_cb, int min_cb_width) |
575 | 6.79M | { |
576 | 6.79M | int inc = 0; |
577 | | |
578 | 6.79M | if (lc->ctb_left_flag || x0) |
579 | 6.65M | inc = !!SAMPLE_CTB(skip_flag, x_cb - 1, y_cb); |
580 | 6.79M | if (lc->ctb_up_flag || y0) |
581 | 6.53M | inc += !!SAMPLE_CTB(skip_flag, x_cb, y_cb - 1); |
582 | | |
583 | 6.79M | return GET_CABAC(SKIP_FLAG_OFFSET + inc); |
584 | 6.79M | } |
585 | | |
586 | | int ff_hevc_cu_qp_delta_abs(HEVCLocalContext *lc) |
587 | 4.85M | { |
588 | 4.85M | int prefix_val = 0; |
589 | 4.85M | int suffix_val = 0; |
590 | 4.85M | int inc = 0; |
591 | | |
592 | 14.1M | while (prefix_val < 5 && GET_CABAC(CU_QP_DELTA_OFFSET + inc)) { |
593 | 9.32M | prefix_val++; |
594 | 9.32M | inc = 1; |
595 | 9.32M | } |
596 | 4.85M | if (prefix_val >= 5) { |
597 | 1.51M | int k = 0; |
598 | 1.63M | while (k < 7 && get_cabac_bypass(&lc->cc)) { |
599 | 120k | suffix_val += 1 << k; |
600 | 120k | k++; |
601 | 120k | } |
602 | 1.51M | if (k == 7) { |
603 | 426 | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k); |
604 | 426 | return AVERROR_INVALIDDATA; |
605 | 426 | } |
606 | | |
607 | 1.62M | while (k--) |
608 | 117k | suffix_val += get_cabac_bypass(&lc->cc) << k; |
609 | 1.51M | } |
610 | 4.85M | return prefix_val + suffix_val; |
611 | 4.85M | } |
612 | | |
613 | | int ff_hevc_cu_qp_delta_sign_flag(HEVCLocalContext *lc) |
614 | 2.96M | { |
615 | 2.96M | return get_cabac_bypass(&lc->cc); |
616 | 2.96M | } |
617 | | |
618 | | int ff_hevc_cu_chroma_qp_offset_flag(HEVCLocalContext *lc) |
619 | 1.06M | { |
620 | 1.06M | return GET_CABAC(CU_CHROMA_QP_OFFSET_FLAG_OFFSET); |
621 | 1.06M | } |
622 | | |
623 | | int ff_hevc_cu_chroma_qp_offset_idx(HEVCLocalContext *lc, int chroma_qp_offset_list_len_minus1) |
624 | 4.12k | { |
625 | 4.12k | int c_max= FFMAX(5, chroma_qp_offset_list_len_minus1); |
626 | 4.12k | int i = 0; |
627 | | |
628 | 12.9k | while (i < c_max && GET_CABAC(CU_CHROMA_QP_OFFSET_IDX_OFFSET)) |
629 | 8.84k | i++; |
630 | | |
631 | 4.12k | return i; |
632 | 4.12k | } |
633 | | |
634 | | int ff_hevc_pred_mode_decode(HEVCLocalContext *lc) |
635 | 2.98M | { |
636 | 2.98M | return GET_CABAC(PRED_MODE_FLAG_OFFSET); |
637 | 2.98M | } |
638 | | |
639 | | int ff_hevc_split_coding_unit_flag_decode(HEVCLocalContext *lc, uint8_t *tab_ct_depth, |
640 | | const HEVCSPS *sps, |
641 | | int ct_depth, int x0, int y0) |
642 | 22.4M | { |
643 | 22.4M | int inc = 0, depth_left = 0, depth_top = 0; |
644 | 22.4M | int x0b = av_zero_extend(x0, sps->log2_ctb_size); |
645 | 22.4M | int y0b = av_zero_extend(y0, sps->log2_ctb_size); |
646 | 22.4M | int x_cb = x0 >> sps->log2_min_cb_size; |
647 | 22.4M | int y_cb = y0 >> sps->log2_min_cb_size; |
648 | | |
649 | 22.4M | if (lc->ctb_left_flag || x0b) |
650 | 21.6M | depth_left = tab_ct_depth[(y_cb) * sps->min_cb_width + x_cb - 1]; |
651 | 22.4M | if (lc->ctb_up_flag || y0b) |
652 | 21.0M | depth_top = tab_ct_depth[(y_cb - 1) * sps->min_cb_width + x_cb]; |
653 | | |
654 | 22.4M | inc += (depth_left > ct_depth); |
655 | 22.4M | inc += (depth_top > ct_depth); |
656 | | |
657 | 22.4M | return GET_CABAC(SPLIT_CODING_UNIT_FLAG_OFFSET + inc); |
658 | 22.4M | } |
659 | | |
660 | | int ff_hevc_part_mode_decode(HEVCLocalContext *lc, const HEVCSPS *sps, int log2_cb_size) |
661 | 50.6M | { |
662 | 50.6M | if (GET_CABAC(PART_MODE_OFFSET)) // 1 |
663 | 33.9M | return PART_2Nx2N; |
664 | 16.6M | if (log2_cb_size == sps->log2_min_cb_size) { |
665 | 16.4M | if (lc->cu.pred_mode == MODE_INTRA) // 0 |
666 | 15.5M | return PART_NxN; |
667 | 880k | if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01 |
668 | 345k | return PART_2NxN; |
669 | 535k | if (log2_cb_size == 3) // 00 |
670 | 522k | return PART_Nx2N; |
671 | 12.3k | if (GET_CABAC(PART_MODE_OFFSET + 2)) // 001 |
672 | 6.97k | return PART_Nx2N; |
673 | 5.40k | return PART_NxN; // 000 |
674 | 12.3k | } |
675 | | |
676 | 238k | if (!sps->amp_enabled) { |
677 | 36.1k | if (GET_CABAC(PART_MODE_OFFSET + 1)) // 01 |
678 | 18.5k | return PART_2NxN; |
679 | 17.6k | return PART_Nx2N; |
680 | 36.1k | } |
681 | | |
682 | 202k | if (GET_CABAC(PART_MODE_OFFSET + 1)) { // 01X, 01XX |
683 | 85.1k | if (GET_CABAC(PART_MODE_OFFSET + 3)) // 011 |
684 | 44.7k | return PART_2NxN; |
685 | 40.3k | if (get_cabac_bypass(&lc->cc)) // 0101 |
686 | 14.8k | return PART_2NxnD; |
687 | 25.5k | return PART_2NxnU; // 0100 |
688 | 40.3k | } |
689 | | |
690 | 117k | if (GET_CABAC(PART_MODE_OFFSET + 3)) // 001 |
691 | 53.8k | return PART_Nx2N; |
692 | 63.8k | if (get_cabac_bypass(&lc->cc)) // 0001 |
693 | 22.9k | return PART_nRx2N; |
694 | 40.8k | return PART_nLx2N; // 0000 |
695 | 63.8k | } |
696 | | |
697 | | int ff_hevc_pcm_flag_decode(HEVCLocalContext *lc) |
698 | 3.72M | { |
699 | 3.72M | return get_cabac_terminate(&lc->cc); |
700 | 3.72M | } |
701 | | |
702 | | int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCLocalContext *lc) |
703 | 98.5M | { |
704 | 98.5M | return GET_CABAC(PREV_INTRA_LUMA_PRED_FLAG_OFFSET); |
705 | 98.5M | } |
706 | | |
707 | | int ff_hevc_mpm_idx_decode(HEVCLocalContext *lc) |
708 | 54.9M | { |
709 | 54.9M | int i = 0; |
710 | 69.2M | while (i < 2 && get_cabac_bypass(&lc->cc)) |
711 | 14.2M | i++; |
712 | 54.9M | return i; |
713 | 54.9M | } |
714 | | |
715 | | int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCLocalContext *lc) |
716 | 43.5M | { |
717 | 43.5M | int i; |
718 | 43.5M | int value = get_cabac_bypass(&lc->cc); |
719 | | |
720 | 217M | for (i = 0; i < 4; i++) |
721 | 174M | value = (value << 1) | get_cabac_bypass(&lc->cc); |
722 | 43.5M | return value; |
723 | 43.5M | } |
724 | | |
725 | | int ff_hevc_intra_chroma_pred_mode_decode(HEVCLocalContext *lc) |
726 | 57.9M | { |
727 | 57.9M | int ret; |
728 | 57.9M | if (!GET_CABAC(INTRA_CHROMA_PRED_MODE_OFFSET)) |
729 | 47.5M | return 4; |
730 | | |
731 | 10.3M | ret = get_cabac_bypass(&lc->cc) << 1; |
732 | 10.3M | ret |= get_cabac_bypass(&lc->cc); |
733 | 10.3M | return ret; |
734 | 57.9M | } |
735 | | |
736 | | int ff_hevc_merge_idx_decode(HEVCLocalContext *lc) |
737 | 5.68M | { |
738 | 5.68M | int i = GET_CABAC(MERGE_IDX_OFFSET); |
739 | | |
740 | 5.68M | if (i != 0) { |
741 | 1.89M | while (i < lc->parent->sh.max_num_merge_cand-1 && get_cabac_bypass(&lc->cc)) |
742 | 650k | i++; |
743 | 1.24M | } |
744 | 5.68M | return i; |
745 | 5.68M | } |
746 | | |
747 | | int ff_hevc_merge_flag_decode(HEVCLocalContext *lc) |
748 | 3.42M | { |
749 | 3.42M | return GET_CABAC(MERGE_FLAG_OFFSET); |
750 | 3.42M | } |
751 | | |
752 | | int ff_hevc_inter_pred_idc_decode(HEVCLocalContext *lc, int nPbW, int nPbH) |
753 | 1.21M | { |
754 | 1.21M | if (nPbW + nPbH == 12) |
755 | 605k | return GET_CABAC(INTER_PRED_IDC_OFFSET + 4); |
756 | 605k | if (GET_CABAC(INTER_PRED_IDC_OFFSET + lc->ct_depth)) |
757 | 214k | return PRED_BI; |
758 | | |
759 | 391k | return GET_CABAC(INTER_PRED_IDC_OFFSET + 4); |
760 | 605k | } |
761 | | |
762 | | int ff_hevc_ref_idx_lx_decode(HEVCLocalContext *lc, int num_ref_idx_lx) |
763 | 1.59M | { |
764 | 1.59M | int i = 0; |
765 | 1.59M | int max = num_ref_idx_lx - 1; |
766 | 1.59M | int max_ctx = FFMIN(max, 2); |
767 | | |
768 | 1.83M | while (i < max_ctx && GET_CABAC(REF_IDX_L0_OFFSET + i)) |
769 | 238k | i++; |
770 | 1.59M | if (i == 2) { |
771 | 60.5k | while (i < max && get_cabac_bypass(&lc->cc)) |
772 | 15.1k | i++; |
773 | 45.4k | } |
774 | | |
775 | 1.59M | return i; |
776 | 1.59M | } |
777 | | |
778 | | int ff_hevc_mvp_lx_flag_decode(HEVCLocalContext *lc) |
779 | 1.59M | { |
780 | 1.59M | return GET_CABAC(MVP_LX_FLAG_OFFSET); |
781 | 1.59M | } |
782 | | |
783 | | int ff_hevc_no_residual_syntax_flag_decode(HEVCLocalContext *lc) |
784 | 1.61M | { |
785 | 1.61M | return GET_CABAC(NO_RESIDUAL_DATA_FLAG_OFFSET); |
786 | 1.61M | } |
787 | | |
788 | | static av_always_inline int abs_mvd_greater0_flag_decode(HEVCLocalContext *lc) |
789 | 2.97M | { |
790 | 2.97M | return GET_CABAC(ABS_MVD_GREATER0_FLAG_OFFSET); |
791 | 2.97M | } |
792 | | |
793 | | static av_always_inline int abs_mvd_greater1_flag_decode(HEVCLocalContext *lc) |
794 | 1.60M | { |
795 | 1.60M | return GET_CABAC(ABS_MVD_GREATER1_FLAG_OFFSET + 1); |
796 | 1.60M | } |
797 | | |
798 | | static av_always_inline int mvd_decode(HEVCLocalContext *lc) |
799 | 609k | { |
800 | 609k | int ret = 2; |
801 | 609k | int k = 1; |
802 | | |
803 | 1.38M | while (k < CABAC_MAX_BIN && get_cabac_bypass(&lc->cc)) { |
804 | 778k | ret += 1U << k; |
805 | 778k | k++; |
806 | 778k | } |
807 | 609k | if (k == CABAC_MAX_BIN) { |
808 | 120 | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k); |
809 | 120 | return 0; |
810 | 120 | } |
811 | 1.99M | while (k--) |
812 | 1.38M | ret += get_cabac_bypass(&lc->cc) << k; |
813 | 609k | return get_cabac_bypass_sign(&lc->cc, -ret); |
814 | 609k | } |
815 | | |
816 | | static av_always_inline int mvd_sign_flag_decode(HEVCLocalContext *lc) |
817 | 993k | { |
818 | 993k | return get_cabac_bypass_sign(&lc->cc, -1); |
819 | 993k | } |
820 | | |
821 | | int ff_hevc_split_transform_flag_decode(HEVCLocalContext *lc, int log2_trafo_size) |
822 | 31.0M | { |
823 | 31.0M | return GET_CABAC(SPLIT_TRANSFORM_FLAG_OFFSET + 5 - log2_trafo_size); |
824 | 31.0M | } |
825 | | |
826 | | int ff_hevc_cbf_cb_cr_decode(HEVCLocalContext *lc, int trafo_depth) |
827 | 152M | { |
828 | 152M | return GET_CABAC(CBF_CB_CR_OFFSET + trafo_depth); |
829 | 152M | } |
830 | | |
831 | | int ff_hevc_cbf_luma_decode(HEVCLocalContext *lc, int trafo_depth) |
832 | 118M | { |
833 | 118M | return GET_CABAC(CBF_LUMA_OFFSET + !trafo_depth); |
834 | 118M | } |
835 | | |
836 | | static int hevc_transform_skip_flag_decode(HEVCLocalContext *lc, int c_idx) |
837 | 57.7M | { |
838 | 57.7M | return GET_CABAC(TRANSFORM_SKIP_FLAG_OFFSET + !!c_idx); |
839 | 57.7M | } |
840 | | |
841 | | static int explicit_rdpcm_flag_decode(HEVCLocalContext *lc, int c_idx) |
842 | 60.3k | { |
843 | 60.3k | return GET_CABAC(EXPLICIT_RDPCM_FLAG_OFFSET + !!c_idx); |
844 | 60.3k | } |
845 | | |
846 | | static int explicit_rdpcm_dir_flag_decode(HEVCLocalContext *lc, int c_idx) |
847 | 26.0k | { |
848 | 26.0k | return GET_CABAC(EXPLICIT_RDPCM_DIR_FLAG_OFFSET + !!c_idx); |
849 | 26.0k | } |
850 | | |
851 | | int ff_hevc_log2_res_scale_abs(HEVCLocalContext *lc, int idx) |
852 | 12.5M | { |
853 | 12.5M | int i =0; |
854 | | |
855 | 29.2M | while (i < 4 && GET_CABAC(LOG2_RES_SCALE_ABS_OFFSET + 4 * idx + i)) |
856 | 16.7M | i++; |
857 | | |
858 | 12.5M | return i; |
859 | 12.5M | } |
860 | | |
861 | | int ff_hevc_res_scale_sign_flag(HEVCLocalContext *lc, int idx) |
862 | 7.00M | { |
863 | 7.00M | return GET_CABAC(RES_SCALE_SIGN_FLAG_OFFSET + idx); |
864 | 7.00M | } |
865 | | |
866 | | static av_always_inline void last_significant_coeff_xy_prefix_decode(HEVCLocalContext *lc, int c_idx, |
867 | | int log2_size, int *last_scx_prefix, int *last_scy_prefix) |
868 | 138M | { |
869 | 138M | int i = 0; |
870 | 138M | int max = (log2_size << 1) - 1; |
871 | 138M | int ctx_offset, ctx_shift; |
872 | | |
873 | 138M | if (!c_idx) { |
874 | 93.8M | ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2); |
875 | 93.8M | ctx_shift = (log2_size + 1) >> 2; |
876 | 93.8M | } else { |
877 | 44.4M | ctx_offset = 15; |
878 | 44.4M | ctx_shift = log2_size - 2; |
879 | 44.4M | } |
880 | 374M | while (i < max && |
881 | 347M | GET_CABAC(LAST_SIGNIFICANT_COEFF_X_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset)) |
882 | 236M | i++; |
883 | 138M | *last_scx_prefix = i; |
884 | | |
885 | 138M | i = 0; |
886 | 372M | while (i < max && |
887 | 346M | GET_CABAC(LAST_SIGNIFICANT_COEFF_Y_PREFIX_OFFSET + (i >> ctx_shift) + ctx_offset)) |
888 | 234M | i++; |
889 | 138M | *last_scy_prefix = i; |
890 | 138M | } |
891 | | |
892 | | static av_always_inline int last_significant_coeff_suffix_decode(HEVCLocalContext *lc, |
893 | | int last_significant_coeff_prefix) |
894 | 49.5M | { |
895 | 49.5M | int i; |
896 | 49.5M | int length = (last_significant_coeff_prefix >> 1) - 1; |
897 | 49.5M | int value = get_cabac_bypass(&lc->cc); |
898 | | |
899 | 58.9M | for (i = 1; i < length; i++) |
900 | 9.34M | value = (value << 1) | get_cabac_bypass(&lc->cc); |
901 | 49.5M | return value; |
902 | 49.5M | } |
903 | | |
904 | | static av_always_inline int significant_coeff_group_flag_decode(HEVCLocalContext *lc, int c_idx, int ctx_cg) |
905 | 113M | { |
906 | 113M | int inc; |
907 | | |
908 | 113M | inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0); |
909 | | |
910 | 113M | return GET_CABAC(SIGNIFICANT_COEFF_GROUP_FLAG_OFFSET + inc); |
911 | 113M | } |
912 | | static av_always_inline int significant_coeff_flag_decode(HEVCLocalContext *lc, int n, |
913 | | int offset, const uint8_t *ctx_idx_map) |
914 | 2.23G | { |
915 | 2.23G | int inc = ctx_idx_map[n] + offset; |
916 | 2.23G | return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + inc); |
917 | 2.23G | } |
918 | | |
919 | | static av_always_inline int significant_coeff_flag_decode_0(HEVCLocalContext *lc, int c_idx, int offset) |
920 | 181M | { |
921 | 181M | return GET_CABAC(SIGNIFICANT_COEFF_FLAG_OFFSET + offset); |
922 | 181M | } |
923 | | |
924 | | static av_always_inline int coeff_abs_level_greater1_flag_decode(HEVCLocalContext *lc, int c_idx, int inc) |
925 | 1.05G | { |
926 | | |
927 | 1.05G | if (c_idx > 0) |
928 | 90.9M | inc += 16; |
929 | | |
930 | 1.05G | return GET_CABAC(COEFF_ABS_LEVEL_GREATER1_FLAG_OFFSET + inc); |
931 | 1.05G | } |
932 | | |
933 | | static av_always_inline int coeff_abs_level_greater2_flag_decode(HEVCLocalContext *lc, int c_idx, int inc) |
934 | 39.5M | { |
935 | 39.5M | if (c_idx > 0) |
936 | 10.9M | inc += 4; |
937 | | |
938 | 39.5M | return GET_CABAC(COEFF_ABS_LEVEL_GREATER2_FLAG_OFFSET + inc); |
939 | 39.5M | } |
940 | | |
941 | | /* a run of bypass bins is the quotient of the offset by range << 17 */ |
942 | | static int cabac_bypass_bits(CABACContext *c, int n) |
943 | 155M | { |
944 | 155M | int avail = CABAC_BITS - ff_ctz(c->low); |
945 | 155M | uint64_t x, r = (uint64_t)c->range << (CABAC_BITS + 1); |
946 | 155M | const uint8_t *bytestream = c->bytestream; |
947 | | |
948 | 155M | if (n < avail) { |
949 | 73.4M | x = (uint64_t)c->low << n; |
950 | 81.7M | } else { |
951 | 81.7M | int fill = (bytestream[0] << 9) + (bytestream[1] << 1) - CABAC_MASK; |
952 | | |
953 | 81.7M | x = (uint64_t)((((int64_t)c->low << avail) + fill)) << (n - avail); |
954 | 81.7M | #if !UNCHECKED_BITSTREAM_READER |
955 | 81.7M | if (bytestream < c->bytestream_end) |
956 | 928k | #endif |
957 | 928k | bytestream += CABAC_BITS / 8; |
958 | 81.7M | } |
959 | | |
960 | 155M | if (x < r << n) { |
961 | 155M | c->low = x % r; |
962 | 155M | c->bytestream = bytestream; |
963 | 155M | return x / r; |
964 | 155M | } else { |
965 | 0 | int ret = 0; |
966 | |
|
967 | 0 | for (int i = 0; i < n; i++) |
968 | 0 | ret = (ret << 1) | get_cabac_bypass(c); |
969 | 0 | return ret; |
970 | 0 | } |
971 | 155M | } |
972 | | |
973 | | /* leading ones of the quotient, consumed only up to the first zero */ |
974 | | static int cabac_unary_prefix(CABACContext *c, int max) |
975 | 532M | { |
976 | 532M | int prefix = 0; |
977 | | |
978 | 542M | while (prefix < max) { |
979 | 542M | int avail = CABAC_BITS - ff_ctz(c->low); |
980 | 542M | int n = FFMIN(avail - 1, max - prefix); |
981 | 542M | uint64_t r = (uint64_t)c->range << (CABAC_BITS + 1); |
982 | 542M | uint64_t x = (uint64_t)c->low << n; |
983 | 542M | int q, t; |
984 | | |
985 | 542M | if (n < 1 || x >= r << n) { |
986 | 37.8M | if (!get_cabac_bypass(c)) |
987 | 33.3M | return prefix; |
988 | 4.54M | prefix++; |
989 | 4.54M | continue; |
990 | 37.8M | } |
991 | | |
992 | 504M | q = x / r; |
993 | 504M | t = ~q & ((1 << n) - 1); |
994 | 504M | if (t) { |
995 | 499M | int p = n - 1 - av_log2(t); |
996 | 499M | int k = n - (p + 1); |
997 | | |
998 | 499M | c->low = (x >> k) - ((uint64_t)(q >> k) * r); |
999 | 499M | return prefix + p; |
1000 | 499M | } |
1001 | 4.56M | c->low = x - (uint64_t)q * r; |
1002 | 4.56M | prefix += n; |
1003 | 4.56M | } |
1004 | 22.7k | return prefix; |
1005 | 532M | } |
1006 | | |
1007 | | static av_always_inline int coeff_abs_level_remaining_decode(HEVCLocalContext *lc, int rc_rice_param) |
1008 | 532M | { |
1009 | 532M | int prefix; |
1010 | 532M | int suffix = 0; |
1011 | 532M | int last_coeff_abs_level_remaining; |
1012 | 532M | int i; |
1013 | | |
1014 | 532M | prefix = cabac_unary_prefix(&lc->cc, CABAC_MAX_BIN); |
1015 | | |
1016 | 532M | if (prefix < 3) { |
1017 | 523M | if (rc_rice_param > 2) |
1018 | 2.32M | suffix = cabac_bypass_bits(&lc->cc, rc_rice_param); |
1019 | 521M | else |
1020 | 550M | for (i = 0; i < rc_rice_param; i++) |
1021 | 29.3M | suffix = (suffix << 1) | get_cabac_bypass(&lc->cc); |
1022 | 523M | last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix; |
1023 | 523M | } else { |
1024 | 9.01M | int prefix_minus3 = prefix - 3; |
1025 | 9.01M | int k; |
1026 | | |
1027 | 9.01M | if (prefix == CABAC_MAX_BIN || prefix_minus3 + rc_rice_param > 16 + 6) { |
1028 | 23.3k | av_log(lc->logctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix); |
1029 | 23.3k | return 0; |
1030 | 23.3k | } |
1031 | | |
1032 | 8.98M | k = prefix_minus3 + rc_rice_param; |
1033 | 8.98M | if (k > 16) { |
1034 | 923 | suffix = cabac_bypass_bits(&lc->cc, 16) << (k - 16); |
1035 | 923 | suffix |= cabac_bypass_bits(&lc->cc, k - 16); |
1036 | 8.98M | } else if (k > 2) { |
1037 | 1.97M | suffix = cabac_bypass_bits(&lc->cc, k); |
1038 | 7.01M | } else { |
1039 | 13.0M | for (i = 0; i < k; i++) |
1040 | 6.03M | suffix = (suffix << 1) | get_cabac_bypass(&lc->cc); |
1041 | 7.01M | } |
1042 | 8.98M | last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1) |
1043 | 8.98M | << rc_rice_param) + suffix; |
1044 | 8.98M | } |
1045 | 532M | return last_coeff_abs_level_remaining; |
1046 | 532M | } |
1047 | | |
1048 | | static av_always_inline int coeff_sign_flag_decode(HEVCLocalContext *lc, uint8_t nb) |
1049 | 250M | { |
1050 | 250M | int i; |
1051 | 250M | int ret = 0; |
1052 | | |
1053 | 250M | if (nb > 2) |
1054 | 150M | return cabac_bypass_bits(&lc->cc, nb); |
1055 | | |
1056 | 217M | for (i = 0; i < nb; i++) |
1057 | 118M | ret = (ret << 1) | get_cabac_bypass(&lc->cc); |
1058 | 99.2M | return ret; |
1059 | 250M | } |
1060 | | |
1061 | | void ff_hevc_hls_residual_coding(HEVCLocalContext *lc, const HEVCPPS *pps, |
1062 | | int x0, int y0, |
1063 | | int log2_trafo_size, enum ScanType scan_idx, |
1064 | | int c_idx) |
1065 | 138M | { |
1066 | 138M | #define GET_COORD(offset, n) \ |
1067 | 1.50G | do { \ |
1068 | 1.50G | x_c = (x_cg << 2) + scan_x_off[n]; \ |
1069 | 1.50G | y_c = (y_cg << 2) + scan_y_off[n]; \ |
1070 | 1.50G | } while (0) |
1071 | 138M | const HEVCContext *const s = lc->parent; |
1072 | 138M | const HEVCSPS *const sps = pps->sps; |
1073 | 138M | int transform_skip_flag = 0; |
1074 | | |
1075 | 138M | int last_significant_coeff_x, last_significant_coeff_y; |
1076 | 138M | int last_scan_pos; |
1077 | 138M | int n_end; |
1078 | 138M | int num_coeff = 0; |
1079 | 138M | int greater1_ctx = 1; |
1080 | | |
1081 | 138M | int num_last_subset; |
1082 | 138M | int max_xy, col_limit, clear_rows; |
1083 | 138M | int x_cg_last_sig, y_cg_last_sig; |
1084 | | |
1085 | 138M | const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off; |
1086 | | |
1087 | 138M | ptrdiff_t stride = s->cur_frame->f->linesize[c_idx]; |
1088 | 138M | int hshift = sps->hshift[c_idx]; |
1089 | 138M | int vshift = sps->vshift[c_idx]; |
1090 | 138M | uint8_t *dst = &s->cur_frame->f->data[c_idx][(y0 >> vshift) * stride + |
1091 | 138M | ((x0 >> hshift) << sps->pixel_shift)]; |
1092 | 138M | int16_t *coeffs = (int16_t*)(c_idx ? lc->edge_emu_buffer2 : lc->edge_emu_buffer); |
1093 | 138M | uint8_t significant_coeff_group_flag[8][8] = {{0}}; |
1094 | 138M | int explicit_rdpcm_flag = 0; |
1095 | 138M | int explicit_rdpcm_dir_flag; |
1096 | | |
1097 | 138M | int trafo_size = 1 << log2_trafo_size; |
1098 | 138M | int i; |
1099 | 138M | int qp,shift,add,scale,scale_m; |
1100 | 138M | static const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 }; |
1101 | 138M | const uint8_t *scale_matrix = NULL; |
1102 | 138M | uint8_t dc_scale; |
1103 | 138M | int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode : |
1104 | 138M | lc->tu.intra_pred_mode_c; |
1105 | | |
1106 | | // Derive QP for dequant |
1107 | 138M | if (!lc->cu.cu_transquant_bypass_flag) { |
1108 | 131M | static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 }; |
1109 | 131M | static const uint8_t rem6[51 + 4 * 6 + 1] = { |
1110 | 131M | 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, |
1111 | 131M | 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, |
1112 | 131M | 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, |
1113 | 131M | 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 |
1114 | 131M | }; |
1115 | | |
1116 | 131M | static const uint8_t div6[51 + 4 * 6 + 1] = { |
1117 | 131M | 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, |
1118 | 131M | 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, |
1119 | 131M | 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, |
1120 | 131M | 10, 10, 11, 11, 11, 11, 11, 11, 12, 12 |
1121 | 131M | }; |
1122 | 131M | int qp_y = lc->qp_y; |
1123 | | |
1124 | 131M | if (pps->transform_skip_enabled_flag && |
1125 | 86.3M | log2_trafo_size <= pps->log2_max_transform_skip_block_size) { |
1126 | 57.7M | transform_skip_flag = hevc_transform_skip_flag_decode(lc, c_idx); |
1127 | 57.7M | } |
1128 | | |
1129 | 131M | if (c_idx == 0) { |
1130 | 89.0M | qp = qp_y + sps->qp_bd_offset; |
1131 | 89.0M | } else { |
1132 | 42.2M | int qp_i, offset; |
1133 | | |
1134 | 42.2M | if (c_idx == 1) |
1135 | 21.1M | offset = pps->cb_qp_offset + s->sh.slice_cb_qp_offset + |
1136 | 21.1M | lc->tu.cu_qp_offset_cb; |
1137 | 21.1M | else |
1138 | 21.1M | offset = pps->cr_qp_offset + s->sh.slice_cr_qp_offset + |
1139 | 21.1M | lc->tu.cu_qp_offset_cr; |
1140 | | |
1141 | 42.2M | qp_i = av_clip(qp_y + offset, - sps->qp_bd_offset, 57); |
1142 | 42.2M | if (sps->chroma_format_idc == 1) { |
1143 | 17.4M | if (qp_i < 30) |
1144 | 8.42M | qp = qp_i; |
1145 | 9.04M | else if (qp_i > 43) |
1146 | 1.19M | qp = qp_i - 6; |
1147 | 7.85M | else |
1148 | 7.85M | qp = qp_c[qp_i - 30]; |
1149 | 24.7M | } else { |
1150 | 24.7M | if (qp_i > 51) |
1151 | 640k | qp = 51; |
1152 | 24.1M | else |
1153 | 24.1M | qp = qp_i; |
1154 | 24.7M | } |
1155 | | |
1156 | 42.2M | qp += sps->qp_bd_offset; |
1157 | 42.2M | } |
1158 | | |
1159 | 131M | shift = sps->bit_depth + log2_trafo_size - 5; |
1160 | 131M | add = 1 << (shift-1); |
1161 | 131M | scale = level_scale[rem6[qp]] << (div6[qp]); |
1162 | 131M | scale_m = 16; // default when no custom scaling lists. |
1163 | 131M | dc_scale = 16; |
1164 | | |
1165 | 131M | if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) { |
1166 | 8.23M | const ScalingList *sl = pps->scaling_list_data_present_flag ? |
1167 | 7.93M | &pps->scaling_list : &sps->scaling_list; |
1168 | 8.23M | int matrix_id = lc->cu.pred_mode != MODE_INTRA; |
1169 | | |
1170 | 8.23M | matrix_id = 3 * matrix_id + c_idx; |
1171 | | |
1172 | 8.23M | scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id]; |
1173 | 8.23M | if (log2_trafo_size >= 4) |
1174 | 837k | dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id]; |
1175 | 8.23M | } |
1176 | 131M | } else { |
1177 | 7.03M | shift = 0; |
1178 | 7.03M | add = 0; |
1179 | 7.03M | scale = 0; |
1180 | 7.03M | dc_scale = 0; |
1181 | 7.03M | } |
1182 | | |
1183 | 138M | if (lc->cu.pred_mode == MODE_INTER && sps->explicit_rdpcm_enabled && |
1184 | 186k | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { |
1185 | 60.3k | explicit_rdpcm_flag = explicit_rdpcm_flag_decode(lc, c_idx); |
1186 | 60.3k | if (explicit_rdpcm_flag) { |
1187 | 26.0k | explicit_rdpcm_dir_flag = explicit_rdpcm_dir_flag_decode(lc, c_idx); |
1188 | 26.0k | } |
1189 | 60.3k | } |
1190 | | |
1191 | 138M | last_significant_coeff_xy_prefix_decode(lc, c_idx, log2_trafo_size, |
1192 | 138M | &last_significant_coeff_x, &last_significant_coeff_y); |
1193 | | |
1194 | 138M | if (last_significant_coeff_x > 3) { |
1195 | 24.9M | int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_x); |
1196 | 24.9M | last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) * |
1197 | 24.9M | (2 + (last_significant_coeff_x & 1)) + |
1198 | 24.9M | suffix; |
1199 | 24.9M | } |
1200 | | |
1201 | 138M | if (last_significant_coeff_y > 3) { |
1202 | 24.6M | int suffix = last_significant_coeff_suffix_decode(lc, last_significant_coeff_y); |
1203 | 24.6M | last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) * |
1204 | 24.6M | (2 + (last_significant_coeff_y & 1)) + |
1205 | 24.6M | suffix; |
1206 | 24.6M | } |
1207 | | |
1208 | 138M | if (scan_idx == SCAN_VERT) |
1209 | 7.86M | FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y); |
1210 | | |
1211 | 138M | x_cg_last_sig = last_significant_coeff_x >> 2; |
1212 | 138M | y_cg_last_sig = last_significant_coeff_y >> 2; |
1213 | | |
1214 | 138M | switch (scan_idx) { |
1215 | 115M | case SCAN_DIAG: { |
1216 | 115M | int last_x_c = last_significant_coeff_x & 3; |
1217 | 115M | int last_y_c = last_significant_coeff_y & 3; |
1218 | | |
1219 | 115M | scan_x_off = ff_hevc_diag_scan4x4_x; |
1220 | 115M | scan_y_off = ff_hevc_diag_scan4x4_y; |
1221 | 115M | num_coeff = diag_scan4x4_inv[last_y_c][last_x_c]; |
1222 | 115M | if (trafo_size == 4) { |
1223 | 79.0M | scan_x_cg = scan_1x1; |
1224 | 79.0M | scan_y_cg = scan_1x1; |
1225 | 79.0M | } else if (trafo_size == 8) { |
1226 | 30.1M | num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4; |
1227 | 30.1M | scan_x_cg = diag_scan2x2_x; |
1228 | 30.1M | scan_y_cg = diag_scan2x2_y; |
1229 | 30.1M | } else if (trafo_size == 16) { |
1230 | 4.62M | num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4; |
1231 | 4.62M | scan_x_cg = ff_hevc_diag_scan4x4_x; |
1232 | 4.62M | scan_y_cg = ff_hevc_diag_scan4x4_y; |
1233 | 4.62M | } else { // trafo_size == 32 |
1234 | 1.88M | num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4; |
1235 | 1.88M | scan_x_cg = ff_hevc_diag_scan8x8_x; |
1236 | 1.88M | scan_y_cg = ff_hevc_diag_scan8x8_y; |
1237 | 1.88M | } |
1238 | 115M | break; |
1239 | 0 | } |
1240 | 14.7M | case SCAN_HORIZ: |
1241 | 14.7M | scan_x_cg = horiz_scan2x2_x; |
1242 | 14.7M | scan_y_cg = horiz_scan2x2_y; |
1243 | 14.7M | scan_x_off = horiz_scan4x4_x; |
1244 | 14.7M | scan_y_off = horiz_scan4x4_y; |
1245 | 14.7M | num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x]; |
1246 | 14.7M | break; |
1247 | 7.86M | default: //SCAN_VERT |
1248 | 7.86M | scan_x_cg = horiz_scan2x2_y; |
1249 | 7.86M | scan_y_cg = horiz_scan2x2_x; |
1250 | 7.86M | scan_x_off = horiz_scan4x4_y; |
1251 | 7.86M | scan_y_off = horiz_scan4x4_x; |
1252 | 7.86M | num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y]; |
1253 | 7.86M | break; |
1254 | 138M | } |
1255 | 138M | num_coeff++; |
1256 | 138M | num_last_subset = (num_coeff - 1) >> 4; |
1257 | | |
1258 | 138M | max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y); |
1259 | 138M | col_limit = last_significant_coeff_x + last_significant_coeff_y + 4; |
1260 | 138M | if (max_xy < 4) |
1261 | 109M | col_limit = FFMIN(4, col_limit); |
1262 | 29.2M | else if (max_xy < 8) |
1263 | 25.2M | col_limit = FFMIN(8, col_limit); |
1264 | 3.96M | else if (max_xy < 12) |
1265 | 1.72M | col_limit = FFMIN(24, col_limit); |
1266 | | |
1267 | | // idct_dc reads coeffs[0] only and writes the whole block: no clear needed |
1268 | 138M | clear_rows = trafo_size; |
1269 | 138M | if (!lc->cu.cu_transquant_bypass_flag && !transform_skip_flag && |
1270 | 103M | !(lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)) { |
1271 | 64.7M | if (max_xy == 0) |
1272 | 21.3M | clear_rows = 0; |
1273 | 64.7M | } |
1274 | 138M | if (clear_rows) |
1275 | 116M | memset(coeffs, 0, clear_rows * trafo_size * sizeof(int16_t)); |
1276 | | |
1277 | 418M | for (i = num_last_subset; i >= 0; i--) { |
1278 | 280M | int n, m; |
1279 | 280M | int x_cg, y_cg, x_c, y_c, pos; |
1280 | 280M | int implicit_non_zero_coeff = 0; |
1281 | 280M | int64_t trans_coeff_level; |
1282 | 280M | int prev_sig = 0; |
1283 | 280M | int offset = i << 4; |
1284 | 280M | int rice_init = 0; |
1285 | | |
1286 | 280M | uint8_t significant_coeff_flag_idx[16]; |
1287 | 280M | uint8_t nb_significant_coeff_flag = 0; |
1288 | | |
1289 | 280M | x_cg = scan_x_cg[i]; |
1290 | 280M | y_cg = scan_y_cg[i]; |
1291 | | |
1292 | 280M | if ((i < num_last_subset) && (i > 0)) { |
1293 | 113M | int ctx_cg = 0; |
1294 | 113M | if (x_cg < (1 << (log2_trafo_size - 2)) - 1) |
1295 | 90.6M | ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg]; |
1296 | 113M | if (y_cg < (1 << (log2_trafo_size - 2)) - 1) |
1297 | 84.9M | ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1]; |
1298 | | |
1299 | 113M | significant_coeff_group_flag[x_cg][y_cg] = |
1300 | 113M | significant_coeff_group_flag_decode(lc, c_idx, ctx_cg); |
1301 | 113M | implicit_non_zero_coeff = 1; |
1302 | 167M | } else { |
1303 | 167M | significant_coeff_group_flag[x_cg][y_cg] = |
1304 | 167M | ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) || |
1305 | 29.2M | (x_cg == 0 && y_cg == 0)); |
1306 | 167M | } |
1307 | | |
1308 | 280M | last_scan_pos = num_coeff - offset - 1; |
1309 | | |
1310 | 280M | if (i == num_last_subset) { |
1311 | 138M | n_end = last_scan_pos - 1; |
1312 | 138M | significant_coeff_flag_idx[0] = last_scan_pos; |
1313 | 138M | nb_significant_coeff_flag = 1; |
1314 | 142M | } else { |
1315 | 142M | n_end = 15; |
1316 | 142M | } |
1317 | | |
1318 | 280M | if (x_cg < ((1 << log2_trafo_size) - 1) >> 2) |
1319 | 138M | prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg]; |
1320 | 280M | if (y_cg < ((1 << log2_trafo_size) - 1) >> 2) |
1321 | 133M | prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1); |
1322 | | |
1323 | 280M | if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) { |
1324 | | // ctx_idx_map composed with the intra-CG scan, indexed by scan position |
1325 | 191M | static const uint8_t ctx_idx_map[3][5 * 16] = { |
1326 | 191M | { // SCAN_DIAG |
1327 | 191M | 0, 2, 1, 6, 3, 4, 7, 6, 4, 5, 7, 8, 5, 8, 8, 8, // log2_trafo_size == 2 |
1328 | 191M | 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0 |
1329 | 191M | 2, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, // prev_sig == 1 |
1330 | 191M | 2, 2, 1, 2, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, // prev_sig == 2 |
1331 | 191M | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // default |
1332 | 191M | }, |
1333 | 191M | { // SCAN_HORIZ |
1334 | 191M | 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8, |
1335 | 191M | 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
1336 | 191M | 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
1337 | 191M | 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, |
1338 | 191M | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
1339 | 191M | }, |
1340 | 191M | { // SCAN_VERT |
1341 | 191M | 0, 2, 6, 7, 1, 3, 6, 7, 4, 4, 8, 8, 5, 5, 8, 8, |
1342 | 191M | 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
1343 | 191M | 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, |
1344 | 191M | 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
1345 | 191M | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
1346 | 191M | }, |
1347 | 191M | }; |
1348 | 191M | const uint8_t *ctx_idx_map_p; |
1349 | 191M | int scf_offset = 0; |
1350 | 191M | int nb0; |
1351 | 191M | if (sps->transform_skip_context_enabled && |
1352 | 60.2M | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { |
1353 | 10.4M | ctx_idx_map_p = &ctx_idx_map[scan_idx][4 * 16]; |
1354 | 10.4M | if (c_idx == 0) { |
1355 | 7.21M | scf_offset = 40; |
1356 | 7.21M | } else { |
1357 | 3.19M | scf_offset = 14 + 27; |
1358 | 3.19M | } |
1359 | 181M | } else { |
1360 | 181M | if (c_idx != 0) |
1361 | 19.7M | scf_offset = 27; |
1362 | 181M | if (log2_trafo_size == 2) { |
1363 | 60.9M | ctx_idx_map_p = &ctx_idx_map[scan_idx][0]; |
1364 | 120M | } else { |
1365 | 120M | ctx_idx_map_p = &ctx_idx_map[scan_idx][(prev_sig + 1) << 4]; |
1366 | 120M | if (c_idx == 0) { |
1367 | 116M | if ((x_cg > 0 || y_cg > 0)) |
1368 | 85.3M | scf_offset += 3; |
1369 | 116M | if (log2_trafo_size == 3) { |
1370 | 63.3M | scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15; |
1371 | 63.3M | } else { |
1372 | 53.3M | scf_offset += 21; |
1373 | 53.3M | } |
1374 | 116M | } else { |
1375 | 3.53M | if (log2_trafo_size == 3) |
1376 | 2.40M | scf_offset += 9; |
1377 | 1.12M | else |
1378 | 1.12M | scf_offset += 12; |
1379 | 3.53M | } |
1380 | 120M | } |
1381 | 181M | } |
1382 | | // the flag is a fresh bin: store always, advance conditionally |
1383 | 191M | nb0 = nb_significant_coeff_flag; |
1384 | 2.42G | for (n = n_end; n > 0; n--) { |
1385 | 2.23G | int sig = significant_coeff_flag_decode(lc, n, scf_offset, ctx_idx_map_p); |
1386 | 2.23G | significant_coeff_flag_idx[nb_significant_coeff_flag] = n; |
1387 | 2.23G | nb_significant_coeff_flag += sig; |
1388 | 2.23G | } |
1389 | 191M | if (nb_significant_coeff_flag != nb0) |
1390 | 160M | implicit_non_zero_coeff = 0; |
1391 | 191M | if (implicit_non_zero_coeff == 0) { |
1392 | 181M | if (sps->transform_skip_context_enabled && |
1393 | 57.9M | (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) { |
1394 | 10.2M | if (c_idx == 0) { |
1395 | 7.02M | scf_offset = 42; |
1396 | 7.02M | } else { |
1397 | 3.18M | scf_offset = 16 + 27; |
1398 | 3.18M | } |
1399 | 171M | } else { |
1400 | 171M | if (i == 0) { |
1401 | 94.8M | if (c_idx == 0) |
1402 | 76.1M | scf_offset = 0; |
1403 | 18.7M | else |
1404 | 18.7M | scf_offset = 27; |
1405 | 94.8M | } else { |
1406 | 76.6M | scf_offset = 2 + scf_offset; |
1407 | 76.6M | } |
1408 | 171M | } |
1409 | 181M | significant_coeff_flag_idx[nb_significant_coeff_flag] = 0; |
1410 | 181M | nb_significant_coeff_flag += |
1411 | 181M | significant_coeff_flag_decode_0(lc, c_idx, scf_offset); |
1412 | 181M | } else { |
1413 | 9.91M | significant_coeff_flag_idx[nb_significant_coeff_flag] = 0; |
1414 | 9.91M | nb_significant_coeff_flag++; |
1415 | 9.91M | } |
1416 | 191M | } |
1417 | | |
1418 | 280M | n_end = nb_significant_coeff_flag; |
1419 | | |
1420 | | |
1421 | 280M | if (n_end) { |
1422 | 250M | int first_nz_pos_in_cg; |
1423 | 250M | int last_nz_pos_in_cg; |
1424 | 250M | int c_rice_param = 0; |
1425 | 250M | int first_greater1_coeff_idx = -1; |
1426 | 250M | uint8_t coeff_abs_level_greater1_flag[8]; |
1427 | 250M | uint16_t coeff_sign_flag; |
1428 | 250M | int sum_abs = 0; |
1429 | 250M | int sign_hidden; |
1430 | 250M | int sb_type; |
1431 | 250M | int gt1_mask = 0; |
1432 | | |
1433 | | |
1434 | | // initialize first elem of coeff_bas_level_greater1_flag |
1435 | 250M | int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0; |
1436 | | |
1437 | 250M | if (sps->persistent_rice_adaptation_enabled) { |
1438 | 80.4M | if (!transform_skip_flag && !lc->cu.cu_transquant_bypass_flag) |
1439 | 67.4M | sb_type = 2 * (c_idx == 0 ? 1 : 0); |
1440 | 13.0M | else |
1441 | 13.0M | sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1; |
1442 | 80.4M | c_rice_param = lc->stat_coeff[sb_type] / 4; |
1443 | 80.4M | } |
1444 | | |
1445 | 250M | if (!(i == num_last_subset) && greater1_ctx == 0) |
1446 | 5.48M | ctx_set++; |
1447 | 250M | greater1_ctx = 1; |
1448 | 250M | last_nz_pos_in_cg = significant_coeff_flag_idx[0]; |
1449 | | |
1450 | | // fresh bins: branchless context update, first set index from the mask |
1451 | 1.30G | for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) { |
1452 | 1.05G | int inc = (ctx_set << 2) + greater1_ctx; |
1453 | 1.05G | int flag = coeff_abs_level_greater1_flag_decode(lc, c_idx, inc); |
1454 | 1.05G | coeff_abs_level_greater1_flag[m] = flag; |
1455 | 1.05G | gt1_mask |= flag << m; |
1456 | 1.05G | greater1_ctx = (greater1_ctx + (greater1_ctx - 1U < 2)) & (flag - 1); |
1457 | 1.05G | } |
1458 | 250M | if (gt1_mask) |
1459 | 39.5M | first_greater1_coeff_idx = ff_ctz(gt1_mask); |
1460 | 250M | first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1]; |
1461 | | |
1462 | 250M | if (lc->cu.cu_transquant_bypass_flag || |
1463 | 235M | (lc->cu.pred_mode == MODE_INTRA && |
1464 | 231M | sps->implicit_rdpcm_enabled && transform_skip_flag && |
1465 | 11.5M | (pred_mode_intra == 10 || pred_mode_intra == 26 )) || |
1466 | 233M | explicit_rdpcm_flag) |
1467 | 16.4M | sign_hidden = 0; |
1468 | 233M | else |
1469 | 233M | sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4); |
1470 | | |
1471 | 250M | if (first_greater1_coeff_idx != -1) { |
1472 | 39.5M | coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(lc, c_idx, ctx_set); |
1473 | 39.5M | } |
1474 | 250M | if (!pps->sign_data_hiding_flag || !sign_hidden ) { |
1475 | 152M | coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag); |
1476 | 152M | } else { |
1477 | 97.6M | coeff_sign_flag = coeff_sign_flag_decode(lc, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1)); |
1478 | 97.6M | } |
1479 | | |
1480 | 1.75G | for (m = 0; m < n_end; m++) { |
1481 | 1.50G | int64_t sign; |
1482 | | |
1483 | 1.50G | n = significant_coeff_flag_idx[m]; |
1484 | 1.50G | GET_COORD(offset, n); |
1485 | 1.50G | if (m < 8) { |
1486 | 1.05G | trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m]; |
1487 | 1.05G | if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) { |
1488 | 75.6M | int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param); |
1489 | | |
1490 | 75.6M | trans_coeff_level += last_coeff_abs_level_remaining; |
1491 | 75.6M | if (trans_coeff_level > (3 << c_rice_param)) |
1492 | 13.2M | c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4); |
1493 | 75.6M | if (sps->persistent_rice_adaptation_enabled && !rice_init) { |
1494 | 8.54M | int c_rice_p_init = lc->stat_coeff[sb_type] / 4; |
1495 | 8.54M | if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init)) |
1496 | 856k | lc->stat_coeff[sb_type]++; |
1497 | 7.68M | else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init)) |
1498 | 4.95M | if (lc->stat_coeff[sb_type] > 0) |
1499 | 853k | lc->stat_coeff[sb_type]--; |
1500 | 8.54M | rice_init = 1; |
1501 | 8.54M | } |
1502 | 75.6M | } |
1503 | 1.05G | } else { |
1504 | 457M | int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(lc, c_rice_param); |
1505 | | |
1506 | 457M | trans_coeff_level = 1 + last_coeff_abs_level_remaining; |
1507 | 457M | if (trans_coeff_level > (3 << c_rice_param)) |
1508 | 3.14M | c_rice_param = sps->persistent_rice_adaptation_enabled ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4); |
1509 | 457M | if (sps->persistent_rice_adaptation_enabled && !rice_init) { |
1510 | 17.6M | int c_rice_p_init = lc->stat_coeff[sb_type] / 4; |
1511 | 17.6M | if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init)) |
1512 | 89.1k | lc->stat_coeff[sb_type]++; |
1513 | 17.5M | else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init)) |
1514 | 17.2M | if (lc->stat_coeff[sb_type] > 0) |
1515 | 89.2k | lc->stat_coeff[sb_type]--; |
1516 | 17.6M | rice_init = 1; |
1517 | 17.6M | } |
1518 | 457M | } |
1519 | 1.50G | if (pps->sign_data_hiding_flag && sign_hidden) { |
1520 | 833M | sum_abs += trans_coeff_level; |
1521 | 833M | if (n == first_nz_pos_in_cg && (sum_abs&1)) |
1522 | 17.5M | trans_coeff_level = -trans_coeff_level; |
1523 | 833M | } |
1524 | | // equiprobable sign: apply it branchless |
1525 | 1.50G | sign = -(int64_t)(coeff_sign_flag >> 15); |
1526 | 1.50G | trans_coeff_level = (trans_coeff_level ^ sign) - sign; |
1527 | 1.50G | coeff_sign_flag <<= 1; |
1528 | 1.50G | if(!lc->cu.cu_transquant_bypass_flag) { |
1529 | 1.40G | if (sps->scaling_list_enabled && !(transform_skip_flag && log2_trafo_size > 2)) { |
1530 | 144M | if(y_c || x_c || log2_trafo_size < 4) { |
1531 | 143M | switch(log2_trafo_size) { |
1532 | 25.1M | case 3: pos = (y_c << 3) + x_c; break; |
1533 | 92.4M | case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break; |
1534 | 2.76M | case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break; |
1535 | 22.9M | default: pos = (y_c << 2) + x_c; break; |
1536 | 143M | } |
1537 | 143M | scale_m = scale_matrix[pos]; |
1538 | 143M | } else { |
1539 | 808k | scale_m = dc_scale; |
1540 | 808k | } |
1541 | 144M | } |
1542 | 1.40G | trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift; |
1543 | 1.40G | trans_coeff_level = FFMAX(FFMIN(trans_coeff_level, 32767), -32768); |
1544 | 1.40G | } |
1545 | 1.50G | coeffs[y_c * trafo_size + x_c] = trans_coeff_level; |
1546 | 1.50G | } |
1547 | 250M | } |
1548 | 280M | } |
1549 | | |
1550 | 138M | if (lc->cu.cu_transquant_bypass_flag) { |
1551 | 7.03M | if (explicit_rdpcm_flag || (sps->implicit_rdpcm_enabled && |
1552 | 409k | (pred_mode_intra == 10 || pred_mode_intra == 26))) { |
1553 | 36.6k | int mode = sps->implicit_rdpcm_enabled ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag; |
1554 | | |
1555 | 36.6k | s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode); |
1556 | 36.6k | } |
1557 | 131M | } else { |
1558 | 131M | if (transform_skip_flag) { |
1559 | 27.9M | int rot = sps->transform_skip_rotation_enabled && |
1560 | 11.3M | log2_trafo_size == 2 && |
1561 | 11.1M | lc->cu.pred_mode == MODE_INTRA; |
1562 | 27.9M | if (rot) { |
1563 | 100M | for (i = 0; i < 8; i++) |
1564 | 88.9M | FFSWAP(int16_t, coeffs[i], coeffs[16 - i - 1]); |
1565 | 11.1M | } |
1566 | | |
1567 | 27.9M | s->hevcdsp.dequant(coeffs, log2_trafo_size); |
1568 | | |
1569 | 27.9M | if (explicit_rdpcm_flag || (sps->implicit_rdpcm_enabled && |
1570 | 11.2M | lc->cu.pred_mode == MODE_INTRA && |
1571 | 11.1M | (pred_mode_intra == 10 || pred_mode_intra == 26))) { |
1572 | 1.43M | int mode = explicit_rdpcm_flag ? explicit_rdpcm_dir_flag : (pred_mode_intra == 26); |
1573 | | |
1574 | 1.43M | s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode); |
1575 | 1.43M | } |
1576 | 103M | } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) { |
1577 | 38.5M | s->hevcdsp.transform_4x4_luma(coeffs); |
1578 | 64.7M | } else { |
1579 | 64.7M | if (max_xy == 0) |
1580 | 21.3M | s->hevcdsp.idct_dc[log2_trafo_size - 2](coeffs); |
1581 | 43.4M | else |
1582 | 43.4M | s->hevcdsp.idct[log2_trafo_size - 2](coeffs, col_limit); |
1583 | 64.7M | } |
1584 | 131M | } |
1585 | 138M | if (lc->tu.cross_pf) { |
1586 | 2.45M | int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer; |
1587 | | |
1588 | 150M | for (i = 0; i < (trafo_size * trafo_size); i++) { |
1589 | 147M | coeffs[i] = coeffs[i] + ((lc->tu.res_scale_val * coeffs_y[i]) >> 3); |
1590 | 147M | } |
1591 | 2.45M | } |
1592 | 138M | s->hevcdsp.add_residual[log2_trafo_size-2](dst, coeffs, stride); |
1593 | 138M | } |
1594 | | |
1595 | | void ff_hevc_hls_mvd_coding(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size) |
1596 | 1.48M | { |
1597 | 1.48M | int x = abs_mvd_greater0_flag_decode(lc); |
1598 | 1.48M | int y = abs_mvd_greater0_flag_decode(lc); |
1599 | | |
1600 | 1.48M | if (x) |
1601 | 803k | x += abs_mvd_greater1_flag_decode(lc); |
1602 | 1.48M | if (y) |
1603 | 799k | y += abs_mvd_greater1_flag_decode(lc); |
1604 | | |
1605 | 1.48M | switch (x) { |
1606 | 306k | case 2: lc->pu.mvd.x = mvd_decode(lc); break; |
1607 | 497k | case 1: lc->pu.mvd.x = mvd_sign_flag_decode(lc); break; |
1608 | 684k | case 0: lc->pu.mvd.x = 0; break; |
1609 | 1.48M | } |
1610 | | |
1611 | 1.48M | switch (y) { |
1612 | 303k | case 2: lc->pu.mvd.y = mvd_decode(lc); break; |
1613 | 495k | case 1: lc->pu.mvd.y = mvd_sign_flag_decode(lc); break; |
1614 | 689k | case 0: lc->pu.mvd.y = 0; break; |
1615 | 1.48M | } |
1616 | 1.48M | } |