FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hevc_cabac.c
Go to the documentation of this file.
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.h"
29 
30 #define CABAC_MAX_BIN 31
31 
32 /**
33  * number of bin by SyntaxElement.
34  */
35 av_unused static const int8_t num_bins_in_se[] = {
36  1, // sao_merge_flag
37  1, // sao_type_idx
38  0, // sao_eo_class
39  0, // sao_band_position
40  0, // sao_offset_abs
41  0, // sao_offset_sign
42  0, // end_of_slice_flag
43  3, // split_coding_unit_flag
44  1, // cu_transquant_bypass_flag
45  3, // skip_flag
46  3, // cu_qp_delta
47  1, // pred_mode
48  4, // part_mode
49  0, // pcm_flag
50  1, // prev_intra_luma_pred_mode
51  0, // mpm_idx
52  0, // rem_intra_luma_pred_mode
53  2, // intra_chroma_pred_mode
54  1, // merge_flag
55  1, // merge_idx
56  5, // inter_pred_idc
57  2, // ref_idx_l0
58  2, // ref_idx_l1
59  2, // abs_mvd_greater0_flag
60  2, // abs_mvd_greater1_flag
61  0, // abs_mvd_minus2
62  0, // mvd_sign_flag
63  1, // mvp_lx_flag
64  1, // no_residual_data_flag
65  3, // split_transform_flag
66  2, // cbf_luma
67  4, // cbf_cb, cbf_cr
68  2, // transform_skip_flag[][]
69  2, // explicit_rdpcm_flag[][]
70  2, // explicit_rdpcm_dir_flag[][]
71  18, // last_significant_coeff_x_prefix
72  18, // last_significant_coeff_y_prefix
73  0, // last_significant_coeff_x_suffix
74  0, // last_significant_coeff_y_suffix
75  4, // significant_coeff_group_flag
76  44, // significant_coeff_flag
77  24, // coeff_abs_level_greater1_flag
78  6, // coeff_abs_level_greater2_flag
79  0, // coeff_abs_level_remaining
80  0, // coeff_sign_flag
81  8, // log2_res_scale_abs
82  2, // res_scale_sign_flag
83  1, // cu_chroma_qp_offset_flag
84  1, // cu_chroma_qp_offset_idx
85 };
86 
87 /**
88  * Offset to ctxIdx 0 in init_values and states, indexed by SyntaxElement.
89  */
90 static const int elem_offset[sizeof(num_bins_in_se)] = {
91  0, // sao_merge_flag
92  1, // sao_type_idx
93  2, // sao_eo_class
94  2, // sao_band_position
95  2, // sao_offset_abs
96  2, // sao_offset_sign
97  2, // end_of_slice_flag
98  2, // split_coding_unit_flag
99  5, // cu_transquant_bypass_flag
100  6, // skip_flag
101  9, // cu_qp_delta
102  12, // pred_mode
103  13, // part_mode
104  17, // pcm_flag
105  17, // prev_intra_luma_pred_mode
106  18, // mpm_idx
107  18, // rem_intra_luma_pred_mode
108  18, // intra_chroma_pred_mode
109  20, // merge_flag
110  21, // merge_idx
111  22, // inter_pred_idc
112  27, // ref_idx_l0
113  29, // ref_idx_l1
114  31, // abs_mvd_greater0_flag
115  33, // abs_mvd_greater1_flag
116  35, // abs_mvd_minus2
117  35, // mvd_sign_flag
118  35, // mvp_lx_flag
119  36, // no_residual_data_flag
120  37, // split_transform_flag
121  40, // cbf_luma
122  42, // cbf_cb, cbf_cr
123  46, // transform_skip_flag[][]
124  48, // explicit_rdpcm_flag[][]
125  50, // explicit_rdpcm_dir_flag[][]
126  52, // last_significant_coeff_x_prefix
127  70, // last_significant_coeff_y_prefix
128  88, // last_significant_coeff_x_suffix
129  88, // last_significant_coeff_y_suffix
130  88, // significant_coeff_group_flag
131  92, // significant_coeff_flag
132  136, // coeff_abs_level_greater1_flag
133  160, // coeff_abs_level_greater2_flag
134  166, // coeff_abs_level_remaining
135  166, // coeff_sign_flag
136  166, // log2_res_scale_abs
137  174, // res_scale_sign_flag
138  176, // cu_chroma_qp_offset_flag
139  177, // cu_chroma_qp_offset_idx
140 };
141 
142 #define CNU 154
143 /**
144  * Indexed by init_type
145  */
146 static const uint8_t init_values[3][HEVC_CONTEXTS] = {
147  { // sao_merge_flag
148  153,
149  // sao_type_idx
150  200,
151  // split_coding_unit_flag
152  139, 141, 157,
153  // cu_transquant_bypass_flag
154  154,
155  // skip_flag
156  CNU, CNU, CNU,
157  // cu_qp_delta
158  154, 154, 154,
159  // pred_mode
160  CNU,
161  // part_mode
162  184, CNU, CNU, CNU,
163  // prev_intra_luma_pred_mode
164  184,
165  // intra_chroma_pred_mode
166  63, 139,
167  // merge_flag
168  CNU,
169  // merge_idx
170  CNU,
171  // inter_pred_idc
172  CNU, CNU, CNU, CNU, CNU,
173  // ref_idx_l0
174  CNU, CNU,
175  // ref_idx_l1
176  CNU, CNU,
177  // abs_mvd_greater1_flag
178  CNU, CNU,
179  // abs_mvd_greater1_flag
180  CNU, CNU,
181  // mvp_lx_flag
182  CNU,
183  // no_residual_data_flag
184  CNU,
185  // split_transform_flag
186  153, 138, 138,
187  // cbf_luma
188  111, 141,
189  // cbf_cb, cbf_cr
190  94, 138, 182, 154,
191  // transform_skip_flag
192  139, 139,
193  // explicit_rdpcm_flag
194  139, 139,
195  // explicit_rdpcm_dir_flag
196  139, 139,
197  // last_significant_coeff_x_prefix
198  110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
199  79, 108, 123, 63,
200  // last_significant_coeff_y_prefix
201  110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
202  79, 108, 123, 63,
203  // significant_coeff_group_flag
204  91, 171, 134, 141,
205  // significant_coeff_flag
206  111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
207  125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
208  139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
209  141, 111,
210  // coeff_abs_level_greater1_flag
211  140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
212  122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
213  // coeff_abs_level_greater2_flag
214  138, 153, 136, 167, 152, 152,
215  // log2_res_scale_abs
216  154, 154, 154, 154, 154, 154, 154, 154,
217  // res_scale_sign_flag
218  154, 154,
219  // cu_chroma_qp_offset_flag
220  154,
221  // cu_chroma_qp_offset_idx
222  154,
223  },
224  { // sao_merge_flag
225  153,
226  // sao_type_idx
227  185,
228  // split_coding_unit_flag
229  107, 139, 126,
230  // cu_transquant_bypass_flag
231  154,
232  // skip_flag
233  197, 185, 201,
234  // cu_qp_delta
235  154, 154, 154,
236  // pred_mode
237  149,
238  // part_mode
239  154, 139, 154, 154,
240  // prev_intra_luma_pred_mode
241  154,
242  // intra_chroma_pred_mode
243  152, 139,
244  // merge_flag
245  110,
246  // merge_idx
247  122,
248  // inter_pred_idc
249  95, 79, 63, 31, 31,
250  // ref_idx_l0
251  153, 153,
252  // ref_idx_l1
253  153, 153,
254  // abs_mvd_greater1_flag
255  140, 198,
256  // abs_mvd_greater1_flag
257  140, 198,
258  // mvp_lx_flag
259  168,
260  // no_residual_data_flag
261  79,
262  // split_transform_flag
263  124, 138, 94,
264  // cbf_luma
265  153, 111,
266  // cbf_cb, cbf_cr
267  149, 107, 167, 154,
268  // transform_skip_flag
269  139, 139,
270  // explicit_rdpcm_flag
271  139, 139,
272  // explicit_rdpcm_dir_flag
273  139, 139,
274  // last_significant_coeff_x_prefix
275  125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
276  94, 108, 123, 108,
277  // last_significant_coeff_y_prefix
278  125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
279  94, 108, 123, 108,
280  // significant_coeff_group_flag
281  121, 140, 61, 154,
282  // significant_coeff_flag
283  155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
284  154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
285  153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
286  140, 140,
287  // coeff_abs_level_greater1_flag
288  154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
289  136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
290  // coeff_abs_level_greater2_flag
291  107, 167, 91, 122, 107, 167,
292  // log2_res_scale_abs
293  154, 154, 154, 154, 154, 154, 154, 154,
294  // res_scale_sign_flag
295  154, 154,
296  // cu_chroma_qp_offset_flag
297  154,
298  // cu_chroma_qp_offset_idx
299  154,
300  },
301  { // sao_merge_flag
302  153,
303  // sao_type_idx
304  160,
305  // split_coding_unit_flag
306  107, 139, 126,
307  // cu_transquant_bypass_flag
308  154,
309  // skip_flag
310  197, 185, 201,
311  // cu_qp_delta
312  154, 154, 154,
313  // pred_mode
314  134,
315  // part_mode
316  154, 139, 154, 154,
317  // prev_intra_luma_pred_mode
318  183,
319  // intra_chroma_pred_mode
320  152, 139,
321  // merge_flag
322  154,
323  // merge_idx
324  137,
325  // inter_pred_idc
326  95, 79, 63, 31, 31,
327  // ref_idx_l0
328  153, 153,
329  // ref_idx_l1
330  153, 153,
331  // abs_mvd_greater1_flag
332  169, 198,
333  // abs_mvd_greater1_flag
334  169, 198,
335  // mvp_lx_flag
336  168,
337  // no_residual_data_flag
338  79,
339  // split_transform_flag
340  224, 167, 122,
341  // cbf_luma
342  153, 111,
343  // cbf_cb, cbf_cr
344  149, 92, 167, 154,
345  // transform_skip_flag
346  139, 139,
347  // explicit_rdpcm_flag
348  139, 139,
349  // explicit_rdpcm_dir_flag
350  139, 139,
351  // last_significant_coeff_x_prefix
352  125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
353  79, 108, 123, 93,
354  // last_significant_coeff_y_prefix
355  125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
356  79, 108, 123, 93,
357  // significant_coeff_group_flag
358  121, 140, 61, 154,
359  // significant_coeff_flag
360  170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
361  154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
362  153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
363  140, 140,
364  // coeff_abs_level_greater1_flag
365  154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
366  136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
367  // coeff_abs_level_greater2_flag
368  107, 167, 91, 107, 107, 167,
369  // log2_res_scale_abs
370  154, 154, 154, 154, 154, 154, 154, 154,
371  // res_scale_sign_flag
372  154, 154,
373  // cu_chroma_qp_offset_flag
374  154,
375  // cu_chroma_qp_offset_idx
376  154,
377  },
378 };
379 
380 static const uint8_t scan_1x1[1] = {
381  0,
382 };
383 
384 static const uint8_t horiz_scan2x2_x[4] = {
385  0, 1, 0, 1,
386 };
387 
388 static const uint8_t horiz_scan2x2_y[4] = {
389  0, 0, 1, 1
390 };
391 
392 static const uint8_t horiz_scan4x4_x[16] = {
393  0, 1, 2, 3,
394  0, 1, 2, 3,
395  0, 1, 2, 3,
396  0, 1, 2, 3,
397 };
398 
399 static const uint8_t horiz_scan4x4_y[16] = {
400  0, 0, 0, 0,
401  1, 1, 1, 1,
402  2, 2, 2, 2,
403  3, 3, 3, 3,
404 };
405 
406 static const uint8_t horiz_scan8x8_inv[8][8] = {
407  { 0, 1, 2, 3, 16, 17, 18, 19, },
408  { 4, 5, 6, 7, 20, 21, 22, 23, },
409  { 8, 9, 10, 11, 24, 25, 26, 27, },
410  { 12, 13, 14, 15, 28, 29, 30, 31, },
411  { 32, 33, 34, 35, 48, 49, 50, 51, },
412  { 36, 37, 38, 39, 52, 53, 54, 55, },
413  { 40, 41, 42, 43, 56, 57, 58, 59, },
414  { 44, 45, 46, 47, 60, 61, 62, 63, },
415 };
416 
417 static const uint8_t diag_scan2x2_x[4] = {
418  0, 0, 1, 1,
419 };
420 
421 static const uint8_t diag_scan2x2_y[4] = {
422  0, 1, 0, 1,
423 };
424 
425 static const uint8_t diag_scan2x2_inv[2][2] = {
426  { 0, 2, },
427  { 1, 3, },
428 };
429 
431  0, 0, 1, 0,
432  1, 2, 0, 1,
433  2, 3, 1, 2,
434  3, 2, 3, 3,
435 };
436 
438  0, 1, 0, 2,
439  1, 0, 3, 2,
440  1, 0, 3, 2,
441  1, 3, 2, 3,
442 };
443 
444 static const uint8_t diag_scan4x4_inv[4][4] = {
445  { 0, 2, 5, 9, },
446  { 1, 4, 8, 12, },
447  { 3, 7, 11, 14, },
448  { 6, 10, 13, 15, },
449 };
450 
452  0, 0, 1, 0,
453  1, 2, 0, 1,
454  2, 3, 0, 1,
455  2, 3, 4, 0,
456  1, 2, 3, 4,
457  5, 0, 1, 2,
458  3, 4, 5, 6,
459  0, 1, 2, 3,
460  4, 5, 6, 7,
461  1, 2, 3, 4,
462  5, 6, 7, 2,
463  3, 4, 5, 6,
464  7, 3, 4, 5,
465  6, 7, 4, 5,
466  6, 7, 5, 6,
467  7, 6, 7, 7,
468 };
469 
471  0, 1, 0, 2,
472  1, 0, 3, 2,
473  1, 0, 4, 3,
474  2, 1, 0, 5,
475  4, 3, 2, 1,
476  0, 6, 5, 4,
477  3, 2, 1, 0,
478  7, 6, 5, 4,
479  3, 2, 1, 0,
480  7, 6, 5, 4,
481  3, 2, 1, 7,
482  6, 5, 4, 3,
483  2, 7, 6, 5,
484  4, 3, 7, 6,
485  5, 4, 7, 6,
486  5, 7, 6, 7,
487 };
488 
489 static const uint8_t diag_scan8x8_inv[8][8] = {
490  { 0, 2, 5, 9, 14, 20, 27, 35, },
491  { 1, 4, 8, 13, 19, 26, 34, 42, },
492  { 3, 7, 12, 18, 25, 33, 41, 48, },
493  { 6, 11, 17, 24, 32, 40, 47, 53, },
494  { 10, 16, 23, 31, 39, 46, 52, 57, },
495  { 15, 22, 30, 38, 45, 51, 56, 60, },
496  { 21, 29, 37, 44, 50, 55, 59, 62, },
497  { 28, 36, 43, 49, 54, 58, 61, 63, },
498 };
499 
500 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
501 {
503  (ctb_addr_ts % s->sps->ctb_width == 2 ||
504  (s->sps->ctb_width == 2 &&
505  ctb_addr_ts % s->sps->ctb_width == 0))) {
506  memcpy(s->cabac_state, s->HEVClc->cabac_state, HEVC_CONTEXTS);
507  }
508 }
509 
510 static void load_states(HEVCContext *s)
511 {
512  memcpy(s->HEVClc->cabac_state, s->cabac_state, HEVC_CONTEXTS);
513 }
514 
516 {
517  skip_bytes(&lc->cc, 0);
518 }
519 
521 {
522  GetBitContext *gb = &s->HEVClc->gb;
523  skip_bits(gb, 1);
524  align_get_bits(gb);
526  gb->buffer + get_bits_count(gb) / 8,
527  (get_bits_left(gb) + 7) / 8);
528 }
529 
531 {
532  int init_type = 2 - s->sh.slice_type;
533  int i;
534 
535  if (s->sh.cabac_init_flag && s->sh.slice_type != I_SLICE)
536  init_type ^= 3;
537 
538  for (i = 0; i < HEVC_CONTEXTS; i++) {
539  int init_value = init_values[init_type][i];
540  int m = (init_value >> 4) * 5 - 45;
541  int n = ((init_value & 15) << 3) - 16;
542  int pre = 2 * (((m * av_clip(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
543 
544  pre ^= pre >> 31;
545  if (pre > 124)
546  pre = 124 + (pre & 1);
547  s->HEVClc->cabac_state[i] = pre;
548  }
549 
550  for (i = 0; i < 4; i++)
551  s->HEVClc->stat_coeff[i] = 0;
552 }
553 
554 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
555 {
556  if (ctb_addr_ts == s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
558  if (s->sh.dependent_slice_segment_flag == 0 ||
559  (s->pps->tiles_enabled_flag &&
560  s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]))
561  cabac_init_state(s);
562 
563  if (!s->sh.first_slice_in_pic_flag &&
565  if (ctb_addr_ts % s->sps->ctb_width == 0) {
566  if (s->sps->ctb_width == 1)
567  cabac_init_state(s);
568  else if (s->sh.dependent_slice_segment_flag == 1)
569  load_states(s);
570  }
571  }
572  } else {
573  if (s->pps->tiles_enabled_flag &&
574  s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
575  if (s->threads_number == 1)
576  cabac_reinit(s->HEVClc);
577  else
579  cabac_init_state(s);
580  }
582  if (ctb_addr_ts % s->sps->ctb_width == 0) {
584  if (s->threads_number == 1)
585  cabac_reinit(s->HEVClc);
586  else
588 
589  if (s->sps->ctb_width == 1)
590  cabac_init_state(s);
591  else
592  load_states(s);
593  }
594  }
595  }
596 }
597 
598 #define GET_CABAC(ctx) get_cabac(&s->HEVClc->cc, &s->HEVClc->cabac_state[ctx])
599 
601 {
603 }
604 
606 {
608  return 0;
609 
610  if (!get_cabac_bypass(&s->HEVClc->cc))
611  return SAO_BAND;
612  return SAO_EDGE;
613 }
614 
616 {
617  int i;
618  int value = get_cabac_bypass(&s->HEVClc->cc);
619 
620  for (i = 0; i < 4; i++)
621  value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
622  return value;
623 }
624 
626 {
627  int i = 0;
628  int length = (1 << (FFMIN(s->sps->bit_depth, 10) - 5)) - 1;
629 
630  while (i < length && get_cabac_bypass(&s->HEVClc->cc))
631  i++;
632  return i;
633 }
634 
636 {
637  return get_cabac_bypass(&s->HEVClc->cc);
638 }
639 
641 {
642  int ret = get_cabac_bypass(&s->HEVClc->cc) << 1;
643  ret |= get_cabac_bypass(&s->HEVClc->cc);
644  return ret;
645 }
646 
648 {
649  return get_cabac_terminate(&s->HEVClc->cc);
650 }
651 
653 {
655 }
656 
657 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
658 {
659  int min_cb_width = s->sps->min_cb_width;
660  int inc = 0;
661  int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
662  int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
663 
664  if (s->HEVClc->ctb_left_flag || x0b)
665  inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
666  if (s->HEVClc->ctb_up_flag || y0b)
667  inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1);
668 
669  return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
670 }
671 
673 {
674  int prefix_val = 0;
675  int suffix_val = 0;
676  int inc = 0;
677 
678  while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
679  prefix_val++;
680  inc = 1;
681  }
682  if (prefix_val >= 5) {
683  int k = 0;
684  while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc)) {
685  suffix_val += 1 << k;
686  k++;
687  }
688  if (k == CABAC_MAX_BIN)
689  av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
690 
691  while (k--)
692  suffix_val += get_cabac_bypass(&s->HEVClc->cc) << k;
693  }
694  return prefix_val + suffix_val;
695 }
696 
698 {
699  return get_cabac_bypass(&s->HEVClc->cc);
700 }
701 
703 {
705 }
706 
708 {
709  int c_max= FFMAX(5, s->pps->chroma_qp_offset_list_len_minus1);
710  int i = 0;
711 
712  while (i < c_max && GET_CABAC(elem_offset[CU_CHROMA_QP_OFFSET_IDX]))
713  i++;
714 
715  return i;
716 }
717 
719 {
721 }
722 
723 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
724 {
725  int inc = 0, depth_left = 0, depth_top = 0;
726  int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
727  int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
728  int x_cb = x0 >> s->sps->log2_min_cb_size;
729  int y_cb = y0 >> s->sps->log2_min_cb_size;
730 
731  if (s->HEVClc->ctb_left_flag || x0b)
732  depth_left = s->tab_ct_depth[(y_cb) * s->sps->min_cb_width + x_cb - 1];
733  if (s->HEVClc->ctb_up_flag || y0b)
734  depth_top = s->tab_ct_depth[(y_cb - 1) * s->sps->min_cb_width + x_cb];
735 
736  inc += (depth_left > ct_depth);
737  inc += (depth_top > ct_depth);
738 
740 }
741 
742 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
743 {
744  if (GET_CABAC(elem_offset[PART_MODE])) // 1
745  return PART_2Nx2N;
746  if (log2_cb_size == s->sps->log2_min_cb_size) {
747  if (s->HEVClc->cu.pred_mode == MODE_INTRA) // 0
748  return PART_NxN;
749  if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
750  return PART_2NxN;
751  if (log2_cb_size == 3) // 00
752  return PART_Nx2N;
753  if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001
754  return PART_Nx2N;
755  return PART_NxN; // 000
756  }
757 
758  if (!s->sps->amp_enabled_flag) {
759  if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
760  return PART_2NxN;
761  return PART_Nx2N;
762  }
763 
764  if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX
765  if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011
766  return PART_2NxN;
767  if (get_cabac_bypass(&s->HEVClc->cc)) // 0101
768  return PART_2NxnD;
769  return PART_2NxnU; // 0100
770  }
771 
772  if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001
773  return PART_Nx2N;
774  if (get_cabac_bypass(&s->HEVClc->cc)) // 0001
775  return PART_nRx2N;
776  return PART_nLx2N; // 0000
777 }
778 
780 {
781  return get_cabac_terminate(&s->HEVClc->cc);
782 }
783 
785 {
787 }
788 
790 {
791  int i = 0;
792  while (i < 2 && get_cabac_bypass(&s->HEVClc->cc))
793  i++;
794  return i;
795 }
796 
798 {
799  int i;
800  int value = get_cabac_bypass(&s->HEVClc->cc);
801 
802  for (i = 0; i < 4; i++)
803  value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
804  return value;
805 }
806 
808 {
809  int ret;
811  return 4;
812 
813  ret = get_cabac_bypass(&s->HEVClc->cc) << 1;
814  ret |= get_cabac_bypass(&s->HEVClc->cc);
815  return ret;
816 }
817 
819 {
820  int i = GET_CABAC(elem_offset[MERGE_IDX]);
821 
822  if (i != 0) {
823  while (i < s->sh.max_num_merge_cand-1 && get_cabac_bypass(&s->HEVClc->cc))
824  i++;
825  }
826  return i;
827 }
828 
830 {
832 }
833 
834 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
835 {
836  if (nPbW + nPbH == 12)
837  return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
839  return PRED_BI;
840 
841  return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
842 }
843 
844 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
845 {
846  int i = 0;
847  int max = num_ref_idx_lx - 1;
848  int max_ctx = FFMIN(max, 2);
849 
850  while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i))
851  i++;
852  if (i == 2) {
853  while (i < max && get_cabac_bypass(&s->HEVClc->cc))
854  i++;
855  }
856 
857  return i;
858 }
859 
861 {
863 }
864 
866 {
868 }
869 
871 {
873 }
874 
876 {
878 }
879 
881 {
882  int ret = 2;
883  int k = 1;
884 
885  while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc)) {
886  ret += 1 << k;
887  k++;
888  }
889  if (k == CABAC_MAX_BIN)
890  av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
891  while (k--)
892  ret += get_cabac_bypass(&s->HEVClc->cc) << k;
893  return get_cabac_bypass_sign(&s->HEVClc->cc, -ret);
894 }
895 
897 {
898  return get_cabac_bypass_sign(&s->HEVClc->cc, -1);
899 }
900 
902 {
903  return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
904 }
905 
906 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
907 {
908  return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
909 }
910 
911 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
912 {
913  return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
914 }
915 
917 {
918  return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx);
919 }
920 
921 static int explicit_rdpcm_flag_decode(HEVCContext *s, int c_idx)
922 {
923  return GET_CABAC(elem_offset[EXPLICIT_RDPCM_FLAG] + !!c_idx);
924 }
925 
927 {
928  return GET_CABAC(elem_offset[EXPLICIT_RDPCM_DIR_FLAG] + !!c_idx);
929 }
930 
932  int i =0;
933 
934  while (i < 4 && GET_CABAC(elem_offset[LOG2_RES_SCALE_ABS] + 4 * idx + i))
935  i++;
936 
937  return i;
938 }
939 
942 }
943 
945  int log2_size, int *last_scx_prefix, int *last_scy_prefix)
946 {
947  int i = 0;
948  int max = (log2_size << 1) - 1;
949  int ctx_offset, ctx_shift;
950 
951  if (!c_idx) {
952  ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2);
953  ctx_shift = (log2_size + 1) >> 2;
954  } else {
955  ctx_offset = 15;
956  ctx_shift = log2_size - 2;
957  }
958  while (i < max &&
959  GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_X_PREFIX] + (i >> ctx_shift) + ctx_offset))
960  i++;
961  *last_scx_prefix = i;
962 
963  i = 0;
964  while (i < max &&
965  GET_CABAC(elem_offset[LAST_SIGNIFICANT_COEFF_Y_PREFIX] + (i >> ctx_shift) + ctx_offset))
966  i++;
967  *last_scy_prefix = i;
968 }
969 
971  int last_significant_coeff_prefix)
972 {
973  int i;
974  int length = (last_significant_coeff_prefix >> 1) - 1;
975  int value = get_cabac_bypass(&s->HEVClc->cc);
976 
977  for (i = 1; i < length; i++)
978  value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
979  return value;
980 }
981 
983 {
984  int inc;
985 
986  inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
987 
989 }
991  int offset, const uint8_t *ctx_idx_map)
992 {
993  int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
995 }
996 
998 {
999  return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + offset);
1000 }
1001 
1003 {
1004 
1005  if (c_idx > 0)
1006  inc += 16;
1007 
1009 }
1010 
1012 {
1013  if (c_idx > 0)
1014  inc += 4;
1015 
1017 }
1018 
1020 {
1021  int prefix = 0;
1022  int suffix = 0;
1023  int last_coeff_abs_level_remaining;
1024  int i;
1025 
1026  while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc))
1027  prefix++;
1028  if (prefix == CABAC_MAX_BIN)
1029  av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
1030  if (prefix < 3) {
1031  for (i = 0; i < rc_rice_param; i++)
1032  suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1033  last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1034  } else {
1035  int prefix_minus3 = prefix - 3;
1036  for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1037  suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1038  last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1039  << rc_rice_param) + suffix;
1040  }
1041  return last_coeff_abs_level_remaining;
1042 }
1043 
1045 {
1046  int i;
1047  int ret = 0;
1048 
1049  for (i = 0; i < nb; i++)
1050  ret = (ret << 1) | get_cabac_bypass(&s->HEVClc->cc);
1051  return ret;
1052 }
1053 
1055  int log2_trafo_size, enum ScanType scan_idx,
1056  int c_idx)
1057 {
1058 #define GET_COORD(offset, n) \
1059  do { \
1060  x_c = (x_cg << 2) + scan_x_off[n]; \
1061  y_c = (y_cg << 2) + scan_y_off[n]; \
1062  } while (0)
1063  HEVCLocalContext *lc = s->HEVClc;
1064  int transform_skip_flag = 0;
1065 
1066  int last_significant_coeff_x, last_significant_coeff_y;
1067  int last_scan_pos;
1068  int n_end;
1069  int num_coeff = 0;
1070  int greater1_ctx = 1;
1071 
1072  int num_last_subset;
1073  int x_cg_last_sig, y_cg_last_sig;
1074 
1075  const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1076 
1077  ptrdiff_t stride = s->frame->linesize[c_idx];
1078  int hshift = s->sps->hshift[c_idx];
1079  int vshift = s->sps->vshift[c_idx];
1080  uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1081  ((x0 >> hshift) << s->sps->pixel_shift)];
1082  int16_t *coeffs = lc->tu.coeffs[c_idx > 0];
1083  uint8_t significant_coeff_group_flag[8][8] = {{0}};
1084  int explicit_rdpcm_flag = 0;
1085  int explicit_rdpcm_dir_flag;
1086 
1087  int trafo_size = 1 << log2_trafo_size;
1088  int i;
1089  int qp,shift,add,scale,scale_m;
1090  const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
1091  const uint8_t *scale_matrix = NULL;
1092  uint8_t dc_scale;
1093  int pred_mode_intra = (c_idx == 0) ? lc->tu.intra_pred_mode :
1094  lc->tu.intra_pred_mode_c;
1095 
1096  memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1097 
1098  // Derive QP for dequant
1099  if (!lc->cu.cu_transquant_bypass_flag) {
1100  static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1101  static const uint8_t rem6[51 + 4 * 6 + 1] = {
1102  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1103  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1104  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1105  4, 5, 0, 1, 2, 3, 4, 5, 0, 1
1106  };
1107 
1108  static const uint8_t div6[51 + 4 * 6 + 1] = {
1109  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1110  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1111  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1112  10, 10, 11, 11, 11, 11, 11, 11, 12, 12
1113  };
1114  int qp_y = lc->qp_y;
1115 
1116  if (c_idx == 0) {
1117  qp = qp_y + s->sps->qp_bd_offset;
1118  } else {
1119  int qp_i, offset;
1120 
1121  if (c_idx == 1)
1122  offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset +
1123  lc->tu.cu_qp_offset_cb;
1124  else
1125  offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset +
1126  lc->tu.cu_qp_offset_cr;
1127 
1128  qp_i = av_clip(qp_y + offset, - s->sps->qp_bd_offset, 57);
1129  if (s->sps->chroma_format_idc == 1) {
1130  if (qp_i < 30)
1131  qp = qp_i;
1132  else if (qp_i > 43)
1133  qp = qp_i - 6;
1134  else
1135  qp = qp_c[qp_i - 30];
1136  } else {
1137  if (qp_i > 51)
1138  qp = 51;
1139  else
1140  qp = qp_i;
1141  }
1142 
1143  qp += s->sps->qp_bd_offset;
1144  }
1145 
1146  shift = s->sps->bit_depth + log2_trafo_size - 5;
1147  add = 1 << (shift-1);
1148  scale = level_scale[rem6[qp]] << (div6[qp]);
1149  scale_m = 16; // default when no custom scaling lists.
1150  dc_scale = 16;
1151 
1152  if (s->sps->scaling_list_enable_flag) {
1154  &s->pps->scaling_list : &s->sps->scaling_list;
1155  int matrix_id = lc->cu.pred_mode != MODE_INTRA;
1156 
1157  if (log2_trafo_size != 5)
1158  matrix_id = 3 * matrix_id + c_idx;
1159 
1160  scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
1161  if (log2_trafo_size >= 4)
1162  dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
1163  }
1164  } else {
1165  shift = 0;
1166  add = 0;
1167  scale = 0;
1168  dc_scale = 0;
1169  }
1170 
1172  log2_trafo_size <= s->pps->log2_max_transform_skip_block_size) {
1173  transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
1174  }
1175 
1177  (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1178  explicit_rdpcm_flag = explicit_rdpcm_flag_decode(s, c_idx);
1179  if (explicit_rdpcm_flag) {
1180  explicit_rdpcm_dir_flag = explicit_rdpcm_dir_flag_decode(s, c_idx);
1181  }
1182  }
1183 
1184  last_significant_coeff_xy_prefix_decode(s, c_idx, log2_trafo_size,
1185  &last_significant_coeff_x, &last_significant_coeff_y);
1186 
1187  if (last_significant_coeff_x > 3) {
1188  int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
1189  last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1190  (2 + (last_significant_coeff_x & 1)) +
1191  suffix;
1192  }
1193 
1194  if (last_significant_coeff_y > 3) {
1195  int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
1196  last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1197  (2 + (last_significant_coeff_y & 1)) +
1198  suffix;
1199  }
1200 
1201  if (scan_idx == SCAN_VERT)
1202  FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1203 
1204  x_cg_last_sig = last_significant_coeff_x >> 2;
1205  y_cg_last_sig = last_significant_coeff_y >> 2;
1206 
1207  switch (scan_idx) {
1208  case SCAN_DIAG: {
1209  int last_x_c = last_significant_coeff_x & 3;
1210  int last_y_c = last_significant_coeff_y & 3;
1211 
1212  scan_x_off = ff_hevc_diag_scan4x4_x;
1213  scan_y_off = ff_hevc_diag_scan4x4_y;
1214  num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1215  if (trafo_size == 4) {
1216  scan_x_cg = scan_1x1;
1217  scan_y_cg = scan_1x1;
1218  } else if (trafo_size == 8) {
1219  num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1220  scan_x_cg = diag_scan2x2_x;
1221  scan_y_cg = diag_scan2x2_y;
1222  } else if (trafo_size == 16) {
1223  num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1224  scan_x_cg = ff_hevc_diag_scan4x4_x;
1225  scan_y_cg = ff_hevc_diag_scan4x4_y;
1226  } else { // trafo_size == 32
1227  num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1228  scan_x_cg = ff_hevc_diag_scan8x8_x;
1229  scan_y_cg = ff_hevc_diag_scan8x8_y;
1230  }
1231  break;
1232  }
1233  case SCAN_HORIZ:
1234  scan_x_cg = horiz_scan2x2_x;
1235  scan_y_cg = horiz_scan2x2_y;
1236  scan_x_off = horiz_scan4x4_x;
1237  scan_y_off = horiz_scan4x4_y;
1238  num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1239  break;
1240  default: //SCAN_VERT
1241  scan_x_cg = horiz_scan2x2_y;
1242  scan_y_cg = horiz_scan2x2_x;
1243  scan_x_off = horiz_scan4x4_y;
1244  scan_y_off = horiz_scan4x4_x;
1245  num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1246  break;
1247  }
1248  num_coeff++;
1249  num_last_subset = (num_coeff - 1) >> 4;
1250 
1251  for (i = num_last_subset; i >= 0; i--) {
1252  int n, m;
1253  int x_cg, y_cg, x_c, y_c, pos;
1254  int implicit_non_zero_coeff = 0;
1255  int64_t trans_coeff_level;
1256  int prev_sig = 0;
1257  int offset = i << 4;
1258  int rice_init = 0;
1259 
1260  uint8_t significant_coeff_flag_idx[16];
1261  uint8_t nb_significant_coeff_flag = 0;
1262 
1263  x_cg = scan_x_cg[i];
1264  y_cg = scan_y_cg[i];
1265 
1266  if ((i < num_last_subset) && (i > 0)) {
1267  int ctx_cg = 0;
1268  if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1269  ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1270  if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1271  ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1272 
1273  significant_coeff_group_flag[x_cg][y_cg] =
1274  significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
1275  implicit_non_zero_coeff = 1;
1276  } else {
1277  significant_coeff_group_flag[x_cg][y_cg] =
1278  ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1279  (x_cg == 0 && y_cg == 0));
1280  }
1281 
1282  last_scan_pos = num_coeff - offset - 1;
1283 
1284  if (i == num_last_subset) {
1285  n_end = last_scan_pos - 1;
1286  significant_coeff_flag_idx[0] = last_scan_pos;
1287  nb_significant_coeff_flag = 1;
1288  } else {
1289  n_end = 15;
1290  }
1291 
1292  if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1293  prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1294  if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1295  prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1296 
1297  if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) {
1298  static const uint8_t ctx_idx_map[] = {
1299  0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8, // log2_trafo_size == 2
1300  1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0
1301  2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 1
1302  2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, // prev_sig == 2
1303  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // default
1304  };
1305  const uint8_t *ctx_idx_map_p;
1306  int scf_offset = 0;
1308  (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1309  ctx_idx_map_p = (uint8_t*) &ctx_idx_map[4 * 16];
1310  if (c_idx == 0) {
1311  scf_offset = 40;
1312  } else {
1313  scf_offset = 14 + 27;
1314  }
1315  } else {
1316  if (c_idx != 0)
1317  scf_offset = 27;
1318  if (log2_trafo_size == 2) {
1319  ctx_idx_map_p = (uint8_t*) &ctx_idx_map[0];
1320  } else {
1321  ctx_idx_map_p = (uint8_t*) &ctx_idx_map[(prev_sig + 1) << 4];
1322  if (c_idx == 0) {
1323  if ((x_cg > 0 || y_cg > 0))
1324  scf_offset += 3;
1325  if (log2_trafo_size == 3) {
1326  scf_offset += (scan_idx == SCAN_DIAG) ? 9 : 15;
1327  } else {
1328  scf_offset += 21;
1329  }
1330  } else {
1331  if (log2_trafo_size == 3)
1332  scf_offset += 9;
1333  else
1334  scf_offset += 12;
1335  }
1336  }
1337  }
1338  for (n = n_end; n > 0; n--) {
1339  x_c = scan_x_off[n];
1340  y_c = scan_y_off[n];
1341  if (significant_coeff_flag_decode(s, x_c, y_c, scf_offset, ctx_idx_map_p)) {
1342  significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1343  nb_significant_coeff_flag++;
1344  implicit_non_zero_coeff = 0;
1345  }
1346  }
1347  if (implicit_non_zero_coeff == 0) {
1349  (transform_skip_flag || lc->cu.cu_transquant_bypass_flag)) {
1350  if (c_idx == 0) {
1351  scf_offset = 42;
1352  } else {
1353  scf_offset = 16 + 27;
1354  }
1355  } else {
1356  if (i == 0) {
1357  if (c_idx == 0)
1358  scf_offset = 0;
1359  else
1360  scf_offset = 27;
1361  } else {
1362  scf_offset = 2 + scf_offset;
1363  }
1364  }
1365  if (significant_coeff_flag_decode_0(s, c_idx, scf_offset) == 1) {
1366  significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1367  nb_significant_coeff_flag++;
1368  }
1369  } else {
1370  significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1371  nb_significant_coeff_flag++;
1372  }
1373  }
1374 
1375  n_end = nb_significant_coeff_flag;
1376 
1377 
1378  if (n_end) {
1379  int first_nz_pos_in_cg;
1380  int last_nz_pos_in_cg;
1381  int c_rice_param = 0;
1382  int first_greater1_coeff_idx = -1;
1383  uint8_t coeff_abs_level_greater1_flag[8];
1384  uint16_t coeff_sign_flag;
1385  int sum_abs = 0;
1386  int sign_hidden;
1387  int sb_type;
1388 
1389 
1390  // initialize first elem of coeff_bas_level_greater1_flag
1391  int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1392 
1394  if (!transform_skip_flag && !lc->cu.cu_transquant_bypass_flag)
1395  sb_type = 2 * (c_idx == 0 ? 1 : 0);
1396  else
1397  sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1;
1398  c_rice_param = lc->stat_coeff[sb_type] / 4;
1399  }
1400 
1401  if (!(i == num_last_subset) && greater1_ctx == 0)
1402  ctx_set++;
1403  greater1_ctx = 1;
1404  last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1405 
1406  for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1407  int inc = (ctx_set << 2) + greater1_ctx;
1408  coeff_abs_level_greater1_flag[m] =
1409  coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
1410  if (coeff_abs_level_greater1_flag[m]) {
1411  greater1_ctx = 0;
1412  if (first_greater1_coeff_idx == -1)
1413  first_greater1_coeff_idx = m;
1414  } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1415  greater1_ctx++;
1416  }
1417  }
1418  first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1419 
1420  if (lc->cu.cu_transquant_bypass_flag ||
1421  (lc->cu.pred_mode == MODE_INTRA &&
1422  s->sps->implicit_rdpcm_enabled_flag && transform_skip_flag &&
1423  (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1424  explicit_rdpcm_flag)
1425  sign_hidden = 0;
1426  else
1427  sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1428 
1429  if (first_greater1_coeff_idx != -1) {
1430  coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
1431  }
1432  if (!s->pps->sign_data_hiding_flag || !sign_hidden ) {
1433  coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1434  } else {
1435  coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1436  }
1437 
1438  for (m = 0; m < n_end; m++) {
1439  n = significant_coeff_flag_idx[m];
1440  GET_COORD(offset, n);
1441  if (m < 8) {
1442  trans_coeff_level = 1 + coeff_abs_level_greater1_flag[m];
1443  if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1444  int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(s, c_rice_param);
1445 
1446  trans_coeff_level += last_coeff_abs_level_remaining;
1447  if (trans_coeff_level > (3 << c_rice_param))
1448  c_rice_param = s->sps->persistent_rice_adaptation_enabled_flag ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1449  if (s->sps->persistent_rice_adaptation_enabled_flag && !rice_init) {
1450  int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1451  if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1452  lc->stat_coeff[sb_type]++;
1453  else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1454  if (lc->stat_coeff[sb_type] > 0)
1455  lc->stat_coeff[sb_type]--;
1456  rice_init = 1;
1457  }
1458  }
1459  } else {
1460  int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(s, c_rice_param);
1461 
1462  trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1463  if (trans_coeff_level > (3 << c_rice_param))
1464  c_rice_param = s->sps->persistent_rice_adaptation_enabled_flag ? c_rice_param + 1 : FFMIN(c_rice_param + 1, 4);
1465  if (s->sps->persistent_rice_adaptation_enabled_flag && !rice_init) {
1466  int c_rice_p_init = lc->stat_coeff[sb_type] / 4;
1467  if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1468  lc->stat_coeff[sb_type]++;
1469  else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1470  if (lc->stat_coeff[sb_type] > 0)
1471  lc->stat_coeff[sb_type]--;
1472  rice_init = 1;
1473  }
1474  }
1475  if (s->pps->sign_data_hiding_flag && sign_hidden) {
1476  sum_abs += trans_coeff_level;
1477  if (n == first_nz_pos_in_cg && (sum_abs&1))
1478  trans_coeff_level = -trans_coeff_level;
1479  }
1480  if (coeff_sign_flag >> 15)
1481  trans_coeff_level = -trans_coeff_level;
1482  coeff_sign_flag <<= 1;
1483  if(!lc->cu.cu_transquant_bypass_flag) {
1484  if(s->sps->scaling_list_enable_flag) {
1485  if(y_c || x_c || log2_trafo_size < 4) {
1486  switch(log2_trafo_size) {
1487  case 3: pos = (y_c << 3) + x_c; break;
1488  case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1489  case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1490  default: pos = (y_c << 2) + x_c; break;
1491  }
1492  scale_m = scale_matrix[pos];
1493  } else {
1494  scale_m = dc_scale;
1495  }
1496  }
1497  trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1498  if(trans_coeff_level < 0) {
1499  if((~trans_coeff_level) & 0xFffffffffff8000)
1500  trans_coeff_level = -32768;
1501  } else {
1502  if(trans_coeff_level & 0xffffffffffff8000)
1503  trans_coeff_level = 32767;
1504  }
1505  }
1506  coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1507  }
1508  }
1509  }
1510 
1511  if (lc->cu.cu_transquant_bypass_flag) {
1512  if (explicit_rdpcm_flag || (s->sps->implicit_rdpcm_enabled_flag &&
1513  (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1514  int mode = s->sps->implicit_rdpcm_enabled_flag ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag;
1515 
1516  s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1517  }
1518  } else {
1519  if (transform_skip_flag) {
1520  int rot = s->sps->transform_skip_rotation_enabled_flag &&
1521  lc->cu.pred_mode == MODE_INTRA;
1522  if (rot) {
1523  for (i = 0; i < (trafo_size * trafo_size >> 1); i++)
1524  FFSWAP(int16_t, coeffs[i], coeffs[trafo_size * trafo_size - i - 1]);
1525  }
1526 
1527  s->hevcdsp.transform_skip(coeffs, log2_trafo_size);
1528 
1529  if (explicit_rdpcm_flag || (s->sps->implicit_rdpcm_enabled_flag &&
1530  lc->cu.pred_mode == MODE_INTRA &&
1531  (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1532  int mode = s->sps->implicit_rdpcm_enabled_flag ? (pred_mode_intra == 26) : explicit_rdpcm_dir_flag;
1533 
1534  s->hevcdsp.transform_rdpcm(coeffs, log2_trafo_size, mode);
1535  }
1536  } else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2) {
1537  s->hevcdsp.idct_4x4_luma(coeffs);
1538  } else {
1539  int max_xy = FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1540  if (max_xy == 0)
1541  s->hevcdsp.idct_dc[log2_trafo_size-2](coeffs);
1542  else {
1543  int col_limit = last_significant_coeff_x + last_significant_coeff_y + 4;
1544  if (max_xy < 4)
1545  col_limit = FFMIN(4, col_limit);
1546  else if (max_xy < 8)
1547  col_limit = FFMIN(8, col_limit);
1548  else if (max_xy < 12)
1549  col_limit = FFMIN(24, col_limit);
1550  s->hevcdsp.idct[log2_trafo_size-2](coeffs, col_limit);
1551  }
1552  }
1553  }
1554  if (lc->tu.cross_pf) {
1555  int16_t *coeffs_y = lc->tu.coeffs[0];
1556 
1557  for (i = 0; i < (trafo_size * trafo_size); i++) {
1558  coeffs[i] = coeffs[i] + ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1559  }
1560  }
1561  s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
1562 }
1563 
1564 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1565 {
1566  HEVCLocalContext *lc = s->HEVClc;
1567  int x = abs_mvd_greater0_flag_decode(s);
1569 
1570  if (x)
1572  if (y)
1574 
1575  switch (x) {
1576  case 2: lc->pu.mvd.x = mvd_decode(s); break;
1577  case 1: lc->pu.mvd.x = mvd_sign_flag_decode(s); break;
1578  case 0: lc->pu.mvd.x = 0; break;
1579  }
1580 
1581  switch (y) {
1582  case 2: lc->pu.mvd.y = mvd_decode(s); break;
1583  case 1: lc->pu.mvd.y = mvd_sign_flag_decode(s); break;
1584  case 0: lc->pu.mvd.y = 0; break;
1585  }
1586 }
1587