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 100
31 
32 /**
33  * number of bin by SyntaxElement.
34  */
35 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  18, // last_significant_coeff_x_prefix
70  18, // last_significant_coeff_y_prefix
71  0, // last_significant_coeff_x_suffix
72  0, // last_significant_coeff_y_suffix
73  4, // significant_coeff_group_flag
74  42, // significant_coeff_flag
75  24, // coeff_abs_level_greater1_flag
76  6, // coeff_abs_level_greater2_flag
77  0, // coeff_abs_level_remaining
78  0, // coeff_sign_flag
79 };
80 
81 /**
82  * Offset to ctxIdx 0 in init_values and states, indexed by SyntaxElement.
83  */
84 static const int elem_offset[sizeof(num_bins_in_se)] = {
85  0,
86  1,
87  2,
88  2,
89  2,
90  2,
91  2,
92  2,
93  5,
94  6,
95  9,
96  12,
97  13,
98  17,
99  17,
100  18,
101  18,
102  18,
103  20,
104  21,
105  22,
106  27,
107  29,
108  31,
109  33,
110  35,
111  35,
112  35,
113  36,
114  37,
115  40,
116  42,
117  46,
118  48,
119  66,
120  84,
121  84,
122  84,
123  88,
124  130,
125  154,
126  160,
127  160,
128 };
129 
130 #define CNU 154
131 /**
132  * Indexed by init_type
133  */
134 static const uint8_t init_values[3][HEVC_CONTEXTS] = {
135  {
136  // sao_merge_flag
137  153,
138  // sao_type_idx
139  200,
140  // split_coding_unit_flag
141  139, 141, 157,
142  // cu_transquant_bypass_flag
143  154,
144  // skip_flag
145  CNU, CNU, CNU,
146  // cu_qp_delta
147  154, 154, 154,
148  // pred_mode
149  CNU,
150  // part_mode
151  184, CNU, CNU, CNU,
152  // prev_intra_luma_pred_mode
153  184,
154  // intra_chroma_pred_mode
155  63, 139,
156  // merge_flag
157  CNU,
158  // merge_idx
159  CNU,
160  // inter_pred_idc
161  CNU, CNU, CNU, CNU, CNU,
162  // ref_idx_l0
163  CNU, CNU,
164  // ref_idx_l1
165  CNU, CNU,
166  // abs_mvd_greater1_flag
167  CNU, CNU,
168  // abs_mvd_greater1_flag
169  CNU, CNU,
170  // mvp_lx_flag
171  CNU,
172  // no_residual_data_flag
173  CNU,
174  // split_transform_flag
175  153, 138, 138,
176  // cbf_luma
177  111, 141,
178  // cbf_cb, cbf_cr
179  94, 138, 182, 154,
180  // transform_skip_flag
181  139, 139,
182  // last_significant_coeff_x_prefix
183  110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
184  79, 108, 123, 63,
185  // last_significant_coeff_y_prefix
186  110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
187  79, 108, 123, 63,
188  // significant_coeff_group_flag
189  91, 171, 134, 141,
190  // significant_coeff_flag
191  111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
192  125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
193  139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
194  // coeff_abs_level_greater1_flag
195  140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
196  122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
197  // coeff_abs_level_greater2_flag
198  138, 153, 136, 167, 152, 152,
199  },
200  {
201  // sao_merge_flag
202  153,
203  // sao_type_idx
204  185,
205  // split_coding_unit_flag
206  107, 139, 126,
207  // cu_transquant_bypass_flag
208  154,
209  // skip_flag
210  197, 185, 201,
211  // cu_qp_delta
212  154, 154, 154,
213  // pred_mode
214  149,
215  // part_mode
216  154, 139, 154, 154,
217  // prev_intra_luma_pred_mode
218  154,
219  // intra_chroma_pred_mode
220  152, 139,
221  // merge_flag
222  110,
223  // merge_idx
224  122,
225  // inter_pred_idc
226  95, 79, 63, 31, 31,
227  // ref_idx_l0
228  153, 153,
229  // ref_idx_l1
230  153, 153,
231  // abs_mvd_greater1_flag
232  140, 198,
233  // abs_mvd_greater1_flag
234  140, 198,
235  // mvp_lx_flag
236  168,
237  // no_residual_data_flag
238  79,
239  // split_transform_flag
240  124, 138, 94,
241  // cbf_luma
242  153, 111,
243  // cbf_cb, cbf_cr
244  149, 107, 167, 154,
245  // transform_skip_flag
246  139, 139,
247  // last_significant_coeff_x_prefix
248  125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
249  94, 108, 123, 108,
250  // last_significant_coeff_y_prefix
251  125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
252  94, 108, 123, 108,
253  // significant_coeff_group_flag
254  121, 140, 61, 154,
255  // significant_coeff_flag
256  155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
257  154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
258  153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
259  // coeff_abs_level_greater1_flag
260  154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
261  136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
262  // coeff_abs_level_greater2_flag
263  107, 167, 91, 122, 107, 167,
264  },
265  {
266  // sao_merge_flag
267  153,
268  // sao_type_idx
269  160,
270  // split_coding_unit_flag
271  107, 139, 126,
272  // cu_transquant_bypass_flag
273  154,
274  // skip_flag
275  197, 185, 201,
276  // cu_qp_delta
277  154, 154, 154,
278  // pred_mode
279  134,
280  // part_mode
281  154, 139, 154, 154,
282  // prev_intra_luma_pred_mode
283  183,
284  // intra_chroma_pred_mode
285  152, 139,
286  // merge_flag
287  154,
288  // merge_idx
289  137,
290  // inter_pred_idc
291  95, 79, 63, 31, 31,
292  // ref_idx_l0
293  153, 153,
294  // ref_idx_l1
295  153, 153,
296  // abs_mvd_greater1_flag
297  169, 198,
298  // abs_mvd_greater1_flag
299  169, 198,
300  // mvp_lx_flag
301  168,
302  // no_residual_data_flag
303  79,
304  // split_transform_flag
305  224, 167, 122,
306  // cbf_luma
307  153, 111,
308  // cbf_cb, cbf_cr
309  149, 92, 167, 154,
310  // transform_skip_flag
311  139, 139,
312  // last_significant_coeff_x_prefix
313  125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
314  79, 108, 123, 93,
315  // last_significant_coeff_y_prefix
316  125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
317  79, 108, 123, 93,
318  // significant_coeff_group_flag
319  121, 140, 61, 154,
320  // significant_coeff_flag
321  170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
322  154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
323  153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
324  // coeff_abs_level_greater1_flag
325  154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
326  136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
327  // coeff_abs_level_greater2_flag
328  107, 167, 91, 107, 107, 167,
329  },
330 };
331 
332 static const uint8_t scan_1x1[1] = {
333  0,
334 };
335 
336 static const uint8_t horiz_scan2x2_x[4] = {
337  0, 1, 0, 1,
338 };
339 
340 static const uint8_t horiz_scan2x2_y[4] = {
341  0, 0, 1, 1
342 };
343 
344 static const uint8_t horiz_scan4x4_x[16] = {
345  0, 1, 2, 3,
346  0, 1, 2, 3,
347  0, 1, 2, 3,
348  0, 1, 2, 3,
349 };
350 
351 static const uint8_t horiz_scan4x4_y[16] = {
352  0, 0, 0, 0,
353  1, 1, 1, 1,
354  2, 2, 2, 2,
355  3, 3, 3, 3,
356 };
357 
358 static const uint8_t horiz_scan8x8_inv[8][8] = {
359  { 0, 1, 2, 3, 16, 17, 18, 19, },
360  { 4, 5, 6, 7, 20, 21, 22, 23, },
361  { 8, 9, 10, 11, 24, 25, 26, 27, },
362  { 12, 13, 14, 15, 28, 29, 30, 31, },
363  { 32, 33, 34, 35, 48, 49, 50, 51, },
364  { 36, 37, 38, 39, 52, 53, 54, 55, },
365  { 40, 41, 42, 43, 56, 57, 58, 59, },
366  { 44, 45, 46, 47, 60, 61, 62, 63, },
367 };
368 
369 static const uint8_t diag_scan4x1_x[4] = {
370  0, 1, 2, 3,
371 };
372 
373 static const uint8_t diag_scan1x4_y[4] = {
374  0, 1, 2, 3,
375 };
376 
377 static const uint8_t diag_scan2x2_x[4] = {
378  0, 0, 1, 1,
379 };
380 
381 static const uint8_t diag_scan2x2_y[4] = {
382  0, 1, 0, 1,
383 };
384 
385 static const uint8_t diag_scan2x2_inv[2][2] = {
386  { 0, 2, },
387  { 1, 3, },
388 };
389 
390 static const uint8_t diag_scan8x2_x[16] = {
391  0, 0, 1, 1,
392  2, 2, 3, 3,
393  4, 4, 5, 5,
394  6, 6, 7, 7,
395 };
396 
397 static const uint8_t diag_scan8x2_y[16] = {
398  0, 1, 0, 1,
399  0, 1, 0, 1,
400  0, 1, 0, 1,
401  0, 1, 0, 1,
402 };
403 
404 static const uint8_t diag_scan8x2_inv[2][8] = {
405  { 0, 2, 4, 6, 8, 10, 12, 14, },
406  { 1, 3, 5, 7, 9, 11, 13, 15, },
407 };
408 
409 static const uint8_t diag_scan2x8_x[16] = {
410  0, 0, 1, 0,
411  1, 0, 1, 0,
412  1, 0, 1, 0,
413  1, 0, 1, 1,
414 };
415 
416 static const uint8_t diag_scan2x8_y[16] = {
417  0, 1, 0, 2,
418  1, 3, 2, 4,
419  3, 5, 4, 6,
420  5, 7, 6, 7,
421 };
422 
423 static const uint8_t diag_scan2x8_inv[8][2] = {
424  { 0, 2, },
425  { 1, 4, },
426  { 3, 6, },
427  { 5, 8, },
428  { 7, 10, },
429  { 9, 12, },
430  { 11, 14, },
431  { 13, 15, },
432 };
433 
435  0, 0, 1, 0,
436  1, 2, 0, 1,
437  2, 3, 1, 2,
438  3, 2, 3, 3,
439 };
440 
442  0, 1, 0, 2,
443  1, 0, 3, 2,
444  1, 0, 3, 2,
445  1, 3, 2, 3,
446 };
447 
448 static const uint8_t diag_scan4x4_inv[4][4] = {
449  { 0, 2, 5, 9, },
450  { 1, 4, 8, 12, },
451  { 3, 7, 11, 14, },
452  { 6, 10, 13, 15, },
453 };
454 
456  0, 0, 1, 0,
457  1, 2, 0, 1,
458  2, 3, 0, 1,
459  2, 3, 4, 0,
460  1, 2, 3, 4,
461  5, 0, 1, 2,
462  3, 4, 5, 6,
463  0, 1, 2, 3,
464  4, 5, 6, 7,
465  1, 2, 3, 4,
466  5, 6, 7, 2,
467  3, 4, 5, 6,
468  7, 3, 4, 5,
469  6, 7, 4, 5,
470  6, 7, 5, 6,
471  7, 6, 7, 7,
472 };
473 
475  0, 1, 0, 2,
476  1, 0, 3, 2,
477  1, 0, 4, 3,
478  2, 1, 0, 5,
479  4, 3, 2, 1,
480  0, 6, 5, 4,
481  3, 2, 1, 0,
482  7, 6, 5, 4,
483  3, 2, 1, 0,
484  7, 6, 5, 4,
485  3, 2, 1, 7,
486  6, 5, 4, 3,
487  2, 7, 6, 5,
488  4, 3, 7, 6,
489  5, 4, 7, 6,
490  5, 7, 6, 7,
491 };
492 
493 static const uint8_t diag_scan8x8_inv[8][8] = {
494  { 0, 2, 5, 9, 14, 20, 27, 35, },
495  { 1, 4, 8, 13, 19, 26, 34, 42, },
496  { 3, 7, 12, 18, 25, 33, 41, 48, },
497  { 6, 11, 17, 24, 32, 40, 47, 53, },
498  { 10, 16, 23, 31, 39, 46, 52, 57, },
499  { 15, 22, 30, 38, 45, 51, 56, 60, },
500  { 21, 29, 37, 44, 50, 55, 59, 62, },
501  { 28, 36, 43, 49, 54, 58, 61, 63, },
502 };
503 
504 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
505 {
507  ((ctb_addr_ts % s->sps->ctb_width) == 2 ||
508  (s->sps->ctb_width == 2 &&
509  (ctb_addr_ts % s->sps->ctb_width) == 0))) {
510  memcpy(s->cabac_state, s->HEVClc->cabac_state, HEVC_CONTEXTS);
511  }
512 }
513 
514 static void load_states(HEVCContext *s)
515 {
516  memcpy(s->HEVClc->cabac_state, s->cabac_state, HEVC_CONTEXTS);
517 }
518 
520 {
521  skip_bytes(&lc->cc, 0);
522 }
523 
525 {
526  GetBitContext *gb = &s->HEVClc->gb;
527  skip_bits(gb, 1);
528  align_get_bits(gb);
530  gb->buffer + get_bits_count(gb) / 8,
531  (get_bits_left(gb) + 7) / 8);
532 }
533 
535 {
536  int init_type = 2 - s->sh.slice_type;
537  int i;
538 
539  if (s->sh.cabac_init_flag && s->sh.slice_type != I_SLICE)
540  init_type ^= 3;
541 
542  for (i = 0; i < HEVC_CONTEXTS; i++) {
543  int init_value = init_values[init_type][i];
544  int m = (init_value >> 4) * 5 - 45;
545  int n = ((init_value & 15) << 3) - 16;
546  int pre = 2 * (((m * av_clip_c(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
547 
548  pre ^= pre >> 31;
549  if (pre > 124)
550  pre = 124 + (pre & 1);
551  s->HEVClc->cabac_state[i] = pre;
552  }
553 }
554 
555 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
556 {
557  if (ctb_addr_ts == s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
559  if ((s->sh.dependent_slice_segment_flag == 0) ||
560  (s->pps->tiles_enabled_flag &&
561  (s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1])))
562  cabac_init_state(s);
563 
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 
707 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
708 {
709  int inc = 0, depth_left = 0, depth_top = 0;
710  int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
711  int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
712  int x_cb = x0 >> s->sps->log2_min_cb_size;
713  int y_cb = y0 >> s->sps->log2_min_cb_size;
714 
715  if (s->HEVClc->ctb_left_flag || x0b)
716  depth_left = s->tab_ct_depth[(y_cb)*s->sps->min_cb_width + x_cb-1];
717  if (s->HEVClc->ctb_up_flag || y0b)
718  depth_top = s->tab_ct_depth[(y_cb-1)*s->sps->min_cb_width + x_cb];
719 
720  inc += (depth_left > ct_depth);
721  inc += (depth_top > ct_depth);
723 }
724 
725 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
726 {
727  if (GET_CABAC(elem_offset[PART_MODE])) // 1
728  return PART_2Nx2N;
729  if (log2_cb_size == s->sps->log2_min_cb_size) {
730  if (s->HEVClc->cu.pred_mode == MODE_INTRA) // 0
731  return PART_NxN;
732  if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
733  return PART_2NxN;
734  if (log2_cb_size == 3) // 00
735  return PART_Nx2N;
736  if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001
737  return PART_Nx2N;
738  return PART_NxN; // 000
739  }
740 
741  if (!s->sps->amp_enabled_flag) {
742  if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
743  return PART_2NxN;
744  return PART_Nx2N;
745  }
746 
747  if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX
748  if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011
749  return PART_2NxN;
750  if (get_cabac_bypass(&s->HEVClc->cc)) // 0101
751  return PART_2NxnD;
752  return PART_2NxnU; // 0100
753  }
754 
755  if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001
756  return PART_Nx2N;
757  if (get_cabac_bypass(&s->HEVClc->cc)) // 0001
758  return PART_nRx2N;
759  return PART_nLx2N; // 0000
760 }
761 
763 {
764  return get_cabac_terminate(&s->HEVClc->cc);
765 }
766 
768 {
770 }
771 
773 {
774  int i = 0;
775  while (i < 2 && get_cabac_bypass(&s->HEVClc->cc))
776  i++;
777  return i;
778 }
779 
781 {
782  int i;
783  int value = get_cabac_bypass(&s->HEVClc->cc);
784 
785  for (i = 0; i < 4; i++)
786  value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
787  return value;
788 }
789 
791 {
792  int ret;
794  return 4;
795 
796  ret = (get_cabac_bypass(&s->HEVClc->cc) << 1);
797  ret |= get_cabac_bypass(&s->HEVClc->cc);
798  return ret;
799 }
800 
802 {
803  int i = GET_CABAC(elem_offset[MERGE_IDX]);
804 
805  if (i != 0) {
806  while (i < s->sh.max_num_merge_cand-1 && get_cabac_bypass(&s->HEVClc->cc))
807  i++;
808  }
809  return i;
810 }
811 
813 {
815 }
816 
817 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
818 {
819  if (nPbW + nPbH == 12)
820  return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
822  return PRED_BI;
823 
824  return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
825 }
826 
827 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
828 {
829  int i = 0;
830  int max = num_ref_idx_lx - 1;
831  int max_ctx = FFMIN(max, 2);
832 
833  while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i))
834  i++;
835  if (i == 2) {
836  while (i < max && get_cabac_bypass(&s->HEVClc->cc))
837  i++;
838  }
839 
840  return i;
841 }
842 
844 {
846 }
847 
849 {
851 }
852 
854 {
856 }
857 
859 {
861 }
862 
864 {
865  int ret = 2;
866  int k = 1;
867 
868  while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc)) {
869  ret += 1 << k;
870  k++;
871  }
872  if (k == CABAC_MAX_BIN)
873  av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
874  while (k--)
875  ret += get_cabac_bypass(&s->HEVClc->cc) << k;
876  return get_cabac_bypass_sign(&s->HEVClc->cc, -ret);
877 }
878 
880 {
881  return get_cabac_bypass_sign(&s->HEVClc->cc, -1);
882 }
883 
885 {
886  return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
887 }
888 
889 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
890 {
891  return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
892 }
893 
894 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
895 {
896  return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
897 }
898 
900 {
901  return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx);
902 }
903 
904 #define LAST_SIG_COEFF(elem) \
905  int i = 0; \
906  int max = (log2_size << 1) - 1; \
907  int ctx_offset, ctx_shift; \
908  \
909  if (c_idx == 0) { \
910  ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2); \
911  ctx_shift = (log2_size + 1) >> 2; \
912  } else { \
913  ctx_offset = 15; \
914  ctx_shift = log2_size - 2; \
915  } \
916  while (i < max && \
917  GET_CABAC(elem_offset[elem] + (i >> ctx_shift) + ctx_offset)) \
918  i++; \
919  return i;
920 
922  int log2_size)
923 {
925 }
926 
928  int log2_size)
929 {
931 }
932 
934  int last_significant_coeff_prefix)
935 {
936  int i;
937  int length = (last_significant_coeff_prefix >> 1) - 1;
938  int value = get_cabac_bypass(&s->HEVClc->cc);
939 
940  for (i = 1; i < length; i++)
941  value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
942  return value;
943 }
944 
946 {
947  int inc;
948 
949  inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
950 
952 }
953 
954 static av_always_inline int significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c,
955  int log2_trafo_size, int scan_idx, int prev_sig)
956 {
957  static const uint8_t ctx_idx_map[] = {
958  0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8
959  };
960  int x_cg = x_c >> 2;
961  int y_cg = y_c >> 2;
962  int sig_ctx;
963  int inc;
964 
965  if (x_c + y_c == 0) {
966  sig_ctx = 0;
967  } else if (log2_trafo_size == 2) {
968  sig_ctx = ctx_idx_map[(y_c << 2) + x_c];
969  } else {
970  switch (prev_sig) {
971  case 0: {
972  int x_off = x_c & 3;
973  int y_off = y_c & 3;
974  sig_ctx = ((x_off + y_off) == 0) ? 2 : ((x_off + y_off) <= 2) ? 1 : 0;
975  }
976  break;
977  case 1:
978  sig_ctx = 2 - FFMIN(y_c & 3, 2);
979  break;
980  case 2:
981  sig_ctx = 2 - FFMIN(x_c & 3, 2);
982  break;
983  default:
984  sig_ctx = 2;
985  }
986 
987  if (c_idx == 0 && (x_cg > 0 || y_cg > 0))
988  sig_ctx += 3;
989 
990  if (log2_trafo_size == 3) {
991  sig_ctx += (scan_idx == SCAN_DIAG) ? 9 : 15;
992  } else {
993  sig_ctx += c_idx ? 12 : 21;
994  }
995  }
996 
997  if (c_idx == 0) {
998  inc = sig_ctx;
999  } else {
1000  inc = sig_ctx + 27;
1001  }
1002 
1004 }
1005 
1007 {
1008 
1009  if (c_idx > 0)
1010  inc += 16;
1011 
1013 }
1014 
1016 {
1017  if (c_idx > 0)
1018  inc += 4;
1019 
1021 }
1022 
1023 static av_always_inline int coeff_abs_level_remaining_decode(HEVCContext *s, int base_level, int rc_rice_param)
1024 {
1025  int prefix = 0;
1026  int suffix = 0;
1027  int last_coeff_abs_level_remaining;
1028  int i;
1029 
1030  while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc))
1031  prefix++;
1032  if (prefix == CABAC_MAX_BIN)
1033  av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
1034  if (prefix < 3) {
1035  for (i = 0; i < rc_rice_param; i++)
1036  suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1037  last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1038  } else {
1039  int prefix_minus3 = prefix - 3;
1040  for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1041  suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1042  last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1043  << rc_rice_param) + suffix;
1044  }
1045  return last_coeff_abs_level_remaining;
1046 }
1047 
1049 {
1050  int i;
1051  int ret = 0;
1052 
1053  for (i = 0; i < nb; i++)
1054  ret = (ret << 1) | get_cabac_bypass(&s->HEVClc->cc);
1055  return ret;
1056 }
1057 
1059  int log2_trafo_size, enum ScanType scan_idx,
1060  int c_idx)
1061 {
1062 #define GET_COORD(offset, n) \
1063  do { \
1064  x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n]; \
1065  y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n]; \
1066  } while (0)
1067  HEVCLocalContext *lc = s->HEVClc;
1068  int transform_skip_flag = 0;
1069 
1070  int last_significant_coeff_x, last_significant_coeff_y;
1071  int last_scan_pos;
1072  int n_end;
1073  int num_coeff = 0;
1074  int greater1_ctx = 1;
1075 
1076  int num_last_subset;
1077  int x_cg_last_sig, y_cg_last_sig;
1078 
1079  const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1080 
1081  ptrdiff_t stride = s->frame->linesize[c_idx];
1082  int hshift = s->sps->hshift[c_idx];
1083  int vshift = s->sps->vshift[c_idx];
1084  uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1085  ((x0 >> hshift) << s->sps->pixel_shift)];
1086  DECLARE_ALIGNED(16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = {0};
1087  DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = {{0}};
1088 
1089  int trafo_size = 1 << log2_trafo_size;
1090  int i;
1091  int qp,shift,add,scale,scale_m;
1092  const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
1093  const uint8_t *scale_matrix;
1094  uint8_t dc_scale;
1095 
1096  // Derive QP for dequant
1097  if (!lc->cu.cu_transquant_bypass_flag) {
1098  static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1099  static const uint8_t rem6[51 + 2 * 6 + 1] = {
1100  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1101  3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1102  0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1103  };
1104 
1105  static const uint8_t div6[51 + 2 * 6 + 1] = {
1106  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1107  3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1108  7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1109  };
1110  int qp_y = lc->qp_y;
1111 
1112  if (c_idx == 0) {
1113  qp = qp_y + s->sps->qp_bd_offset;
1114  } else {
1115  int qp_i, offset;
1116 
1117  if (c_idx == 1)
1118  offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
1119  else
1120  offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
1121 
1122  qp_i = av_clip_c(qp_y + offset, - s->sps->qp_bd_offset, 57);
1123  if (qp_i < 30)
1124  qp = qp_i;
1125  else if (qp_i > 43)
1126  qp = qp_i - 6;
1127  else
1128  qp = qp_c[qp_i - 30];
1129 
1130  qp += s->sps->qp_bd_offset;
1131  }
1132 
1133  shift = s->sps->bit_depth + log2_trafo_size - 5;
1134  add = 1 << (shift-1);
1135  scale = level_scale[rem6[qp]] << (div6[qp]);
1136  scale_m = 16; // default when no custom scaling lists.
1137  dc_scale = 16;
1138 
1139  if (s->sps->scaling_list_enable_flag) {
1141  &s->pps->scaling_list : &s->sps->scaling_list;
1142  int matrix_id = lc->cu.pred_mode != MODE_INTRA;
1143 
1144  if (log2_trafo_size != 5)
1145  matrix_id = 3 * matrix_id + c_idx;
1146 
1147  scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
1148  if (log2_trafo_size >= 4)
1149  dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
1150  }
1151  }
1152 
1154  log2_trafo_size == 2) {
1155  transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
1156  }
1157 
1158  last_significant_coeff_x =
1159  last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
1160  last_significant_coeff_y =
1161  last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
1162 
1163  if (last_significant_coeff_x > 3) {
1164  int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
1165  last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1166  (2 + (last_significant_coeff_x & 1)) +
1167  suffix;
1168  }
1169 
1170  if (last_significant_coeff_y > 3) {
1171  int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
1172  last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1173  (2 + (last_significant_coeff_y & 1)) +
1174  suffix;
1175  }
1176 
1177  if (scan_idx == SCAN_VERT)
1178  FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1179 
1180  x_cg_last_sig = last_significant_coeff_x >> 2;
1181  y_cg_last_sig = last_significant_coeff_y >> 2;
1182 
1183  switch (scan_idx) {
1184  case SCAN_DIAG: {
1185  int last_x_c = last_significant_coeff_x & 3;
1186  int last_y_c = last_significant_coeff_y & 3;
1187 
1188  scan_x_off = ff_hevc_diag_scan4x4_x;
1189  scan_y_off = ff_hevc_diag_scan4x4_y;
1190  num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1191  if (trafo_size == 4) {
1192  scan_x_cg = scan_1x1;
1193  scan_y_cg = scan_1x1;
1194  } else if (trafo_size == 8) {
1195  num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1196  scan_x_cg = diag_scan2x2_x;
1197  scan_y_cg = diag_scan2x2_y;
1198  } else if (trafo_size == 16) {
1199  num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1200  scan_x_cg = ff_hevc_diag_scan4x4_x;
1201  scan_y_cg = ff_hevc_diag_scan4x4_y;
1202  } else { // trafo_size == 32
1203  num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1204  scan_x_cg = ff_hevc_diag_scan8x8_x;
1205  scan_y_cg = ff_hevc_diag_scan8x8_y;
1206  }
1207  break;
1208  }
1209  case SCAN_HORIZ:
1210  scan_x_cg = horiz_scan2x2_x;
1211  scan_y_cg = horiz_scan2x2_y;
1212  scan_x_off = horiz_scan4x4_x;
1213  scan_y_off = horiz_scan4x4_y;
1214  num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1215  break;
1216  default: //SCAN_VERT
1217  scan_x_cg = horiz_scan2x2_y;
1218  scan_y_cg = horiz_scan2x2_x;
1219  scan_x_off = horiz_scan4x4_y;
1220  scan_y_off = horiz_scan4x4_x;
1221  num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1222  break;
1223  }
1224  num_coeff++;
1225  num_last_subset = (num_coeff - 1) >> 4;
1226 
1227  for (i = num_last_subset; i >= 0; i--) {
1228  int n, m;
1229  int x_cg, y_cg, x_c, y_c, pos;
1230  int implicit_non_zero_coeff = 0;
1231  int64_t trans_coeff_level;
1232  int prev_sig = 0;
1233  int offset = i << 4;
1234 
1235  uint8_t significant_coeff_flag_idx[16];
1236  uint8_t nb_significant_coeff_flag = 0;
1237 
1238  x_cg = scan_x_cg[i];
1239  y_cg = scan_y_cg[i];
1240 
1241  if ((i < num_last_subset) && (i > 0)) {
1242  int ctx_cg = 0;
1243  if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1244  ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1245  if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1246  ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1247 
1248  significant_coeff_group_flag[x_cg][y_cg] =
1249  significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
1250  implicit_non_zero_coeff = 1;
1251  } else {
1252  significant_coeff_group_flag[x_cg][y_cg] =
1253  ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1254  (x_cg == 0 && y_cg == 0));
1255  }
1256 
1257  last_scan_pos = num_coeff - offset - 1;
1258 
1259  if (i == num_last_subset) {
1260  n_end = last_scan_pos - 1;
1261  significant_coeff_flag_idx[0] = last_scan_pos;
1262  nb_significant_coeff_flag = 1;
1263  } else {
1264  n_end = 15;
1265  }
1266 
1267  if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1268  prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
1269  if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1270  prev_sig += (significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1271 
1272  for (n = n_end; n >= 0; n--) {
1273  GET_COORD(offset, n);
1274 
1275  if (significant_coeff_group_flag[x_cg][y_cg] &&
1276  (n > 0 || implicit_non_zero_coeff == 0)) {
1277  if (significant_coeff_flag_decode(s, c_idx, x_c, y_c, log2_trafo_size, scan_idx, prev_sig) == 1) {
1278  significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1279  nb_significant_coeff_flag++;
1280  implicit_non_zero_coeff = 0;
1281  }
1282  } else {
1283  int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
1284  if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
1285  significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1286  nb_significant_coeff_flag++;
1287  }
1288  }
1289  }
1290 
1291  n_end = nb_significant_coeff_flag;
1292 
1293 
1294  if (n_end) {
1295  int first_nz_pos_in_cg = 16;
1296  int last_nz_pos_in_cg = -1;
1297  int c_rice_param = 0;
1298  int first_greater1_coeff_idx = -1;
1299  uint8_t coeff_abs_level_greater1_flag[16] = {0};
1300  uint16_t coeff_sign_flag;
1301  int sum_abs = 0;
1302  int sign_hidden = 0;
1303 
1304  // initialize first elem of coeff_bas_level_greater1_flag
1305  int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1306 
1307  if (!(i == num_last_subset) && greater1_ctx == 0)
1308  ctx_set++;
1309  greater1_ctx = 1;
1310  last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1311 
1312  for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1313  int n_idx = significant_coeff_flag_idx[m];
1314  int inc = (ctx_set << 2) + greater1_ctx;
1315  coeff_abs_level_greater1_flag[n_idx] =
1316  coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
1317  if (coeff_abs_level_greater1_flag[n_idx]) {
1318  greater1_ctx = 0;
1319  } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1320  greater1_ctx++;
1321  }
1322 
1323  if (coeff_abs_level_greater1_flag[n_idx] &&
1324  first_greater1_coeff_idx == -1)
1325  first_greater1_coeff_idx = n_idx;
1326  }
1327  first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1328  sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
1330 
1331  if (first_greater1_coeff_idx != -1) {
1332  coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
1333  }
1334  if (!s->pps->sign_data_hiding_flag || !sign_hidden ) {
1335  coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1336  } else {
1337  coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1338  }
1339 
1340  for (m = 0; m < n_end; m++) {
1341  n = significant_coeff_flag_idx[m];
1342  GET_COORD(offset, n);
1343  trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
1344  if (trans_coeff_level == ((m < 8) ?
1345  ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
1346  int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(s, trans_coeff_level, c_rice_param);
1347 
1348  trans_coeff_level += last_coeff_abs_level_remaining;
1349  if (trans_coeff_level > (3 << c_rice_param))
1350  c_rice_param = FFMIN(c_rice_param + 1, 4);
1351 
1352  }
1353  if (s->pps->sign_data_hiding_flag && sign_hidden) {
1354  sum_abs += trans_coeff_level;
1355  if (n == first_nz_pos_in_cg && (sum_abs&1))
1356  trans_coeff_level = -trans_coeff_level;
1357  }
1358  if (coeff_sign_flag >> 15)
1359  trans_coeff_level = -trans_coeff_level;
1360  coeff_sign_flag <<= 1;
1361  if(!lc->cu.cu_transquant_bypass_flag) {
1362  if(s->sps->scaling_list_enable_flag) {
1363  if(y_c || x_c || log2_trafo_size < 4) {
1364  switch(log2_trafo_size) {
1365  case 3: pos = (y_c << 3) + x_c; break;
1366  case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1367  case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1368  default: pos = (y_c << 2) + x_c;
1369  }
1370  scale_m = scale_matrix[pos];
1371  } else {
1372  scale_m = dc_scale;
1373  }
1374  }
1375  trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1376  if(trans_coeff_level < 0) {
1377  if((~trans_coeff_level) & 0xFffffffffff8000)
1378  trans_coeff_level = -32768;
1379  } else {
1380  if(trans_coeff_level & 0xffffffffffff8000)
1381  trans_coeff_level = 32767;
1382  }
1383  }
1384  coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1385  }
1386  }
1387  }
1388 
1389  if (lc->cu.cu_transquant_bypass_flag) {
1390  s->hevcdsp.transquant_bypass[log2_trafo_size-2](dst, coeffs, stride);
1391  } else {
1392  if (transform_skip_flag)
1393  s->hevcdsp.transform_skip(dst, coeffs, stride);
1394  else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)
1395  s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);
1396  else
1397  s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
1398  }
1399 }
1400 
1401 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1402 {
1403  HEVCLocalContext *lc = s->HEVClc;
1404  int x = abs_mvd_greater0_flag_decode(s);
1406 
1407  if (x)
1409  if (y)
1411 
1412  switch (x) {
1413  case 2: lc->pu.mvd.x = mvd_decode(s); break;
1414  case 1: lc->pu.mvd.x = mvd_sign_flag_decode(s); break;
1415  case 0: lc->pu.mvd.x = 0; break;
1416  }
1417 
1418  switch (y) {
1419  case 2: lc->pu.mvd.y = mvd_decode(s); break;
1420  case 1: lc->pu.mvd.y = mvd_sign_flag_decode(s); break;
1421  case 0: lc->pu.mvd.y = 0; break;
1422  }
1423 }
1424