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