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
26
29
30 #define CABAC_MAX_BIN 31
31
32 /**
33 * number of bin by SyntaxElement.
34 */
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 */
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
143 /**
144 * Indexed by init_type
145 */
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
157 // cu_qp_delta
158 154, 154, 154,
159 // pred_mode
161 // part_mode
163 // prev_intra_luma_pred_mode
164 184,
165 // intra_chroma_pred_mode
166 63, 139,
167 // merge_flag
169 // merge_idx
171 // inter_pred_idc
173 // ref_idx_l0
175 // ref_idx_l1
177 // abs_mvd_greater1_flag
179 // abs_mvd_greater1_flag
181 // mvp_lx_flag
183 // no_residual_data_flag
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
381 0,
382 };
383
385 0, 1, 0, 1,
386 };
387
389 0, 0, 1, 1
390 };
391
393 0, 1, 2, 3,
394 0, 1, 2, 3,
395 0, 1, 2, 3,
396 0, 1, 2, 3,
397 };
398
400 0, 0, 0, 0,
401 1, 1, 1, 1,
402 2, 2, 2, 2,
403 3, 3, 3, 3,
404 };
405
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
418 0, 0, 1, 1,
419 };
420
422 0, 1, 0, 1,
423 };
424
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
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
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
501 {
507 }
508 }
509
511 {
513 }
514
516 {
518 }
519
521 {
528 }
529
531 {
533 int i;
534
536 init_type ^= 3;
537
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);
548 }
549
550 for (i = 0; i < 4; i++)
552 }
553
555 {
562
570 }
571 }
572 } else {
577 else
580 }
586 else
588
591 else
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
613 }
614
616 {
617 int i;
619
620 for (i = 0; i < 4; i++)
623 }
624
626 {
627 int i = 0;
629
631 i++;
632 return i;
633 }
634
636 {
638 }
639
641 {
645 }
646
648 {
650 }
651
653 {
655 }
656
658 {
660 int inc = 0;
663
668
670 }
671
673 {
674 int prefix_val = 0;
675 int suffix_val = 0;
676 int inc = 0;
677
679 prefix_val++;
680 inc = 1;
681 }
682 if (prefix_val >= 5) {
683 int k = 0;
685 suffix_val += 1 << k;
686 k++;
687 }
690
691 while (k--)
693 }
694 return prefix_val + suffix_val;
695 }
696
698 {
700 }
701
703 {
705 }
706
708 {
710 int i = 0;
711
713 i++;
714
715 return i;
716 }
717
719 {
721 }
722
724 {
725 int inc = 0, depth_left = 0, depth_top = 0;
730
735
736 inc += (depth_left > ct_depth);
737 inc += (depth_top > ct_depth);
738
740 }
741
743 {
751 if (log2_cb_size == 3) // 00
756 }
757
762 }
763
770 }
771
777 }
778
780 {
782 }
783
785 {
787 }
788
790 {
791 int i = 0;
793 i++;
794 return i;
795 }
796
798 {
799 int i;
801
802 for (i = 0; i < 4; i++)
805 }
806
808 {
811 return 4;
812
816 }
817
819 {
821
822 if (i != 0) {
824 i++;
825 }
826 return i;
827 }
828
830 {
832 }
833
835 {
836 if (nPbW + nPbH == 12)
840
842 }
843
845 {
846 int i = 0;
847 int max = num_ref_idx_lx - 1;
848 int max_ctx =
FFMIN(max, 2);
849
851 i++;
852 if (i == 2) {
854 i++;
855 }
856
857 return i;
858 }
859
861 {
863 }
864
866 {
868 }
869
871 {
873 }
874
876 {
878 }
879
881 {
883 int k = 1;
884
886 ret += 1 << k;
887 k++;
888 }
891 while (k--)
894 }
895
897 {
899 }
900
902 {
904 }
905
907 {
909 }
910
912 {
914 }
915
917 {
919 }
920
922 {
924 }
925
927 {
929 }
930
932 int i =0;
933
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 &&
960 i++;
961 *last_scx_prefix = i;
962
963 i = 0;
964 while (i < max &&
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;
976
977 for (i = 1; i <
length; i++)
980 }
981
983 {
984 int inc;
985
986 inc =
FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
987
989 }
992 {
993 int inc = ctx_idx_map[(y_c << 2) + x_c] + offset;
995 }
996
998 {
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
1027 prefix++;
1030 if (prefix < 3) {
1031 for (i = 0; i < rc_rice_param; i++)
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++)
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;
1048
1049 for (i = 0; i < nb; i++)
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)
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
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 };
1095
1096 memset(coeffs, 0, trafo_size * trafo_size * sizeof(int16_t));
1097
1098 // Derive QP for dequant
1100 static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
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
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
1117 log2_trafo_size <= s->pps->log2_max_transform_skip_block_size) {
1119 }
1120
1121 if (c_idx == 0) {
1123 } else {
1125
1126 if (c_idx == 1)
1129 else
1132
1135 if (qp_i < 30)
1136 qp = qp_i;
1137 else if (qp_i > 43)
1138 qp = qp_i - 6;
1139 else
1140 qp = qp_c[qp_i - 30];
1141 } else {
1142 if (qp_i > 51)
1143 qp = 51;
1144 else
1145 qp = qp_i;
1146 }
1147
1149 }
1150
1152 add = 1 << (shift-1);
1153 scale = level_scale[rem6[qp]] << (div6[qp]);
1154 scale_m = 16; // default when no custom scaling lists.
1155 dc_scale = 16;
1156
1161
1162 matrix_id = 3 * matrix_id + c_idx;
1163
1164 scale_matrix = sl->
sl[log2_trafo_size - 2][matrix_id];
1165 if (log2_trafo_size >= 4)
1166 dc_scale = sl->
sl_dc[log2_trafo_size - 4][matrix_id];
1167 }
1168 } else {
1169 shift = 0;
1170 add = 0;
1171 scale = 0;
1172 dc_scale = 0;
1173 }
1174
1178 if (explicit_rdpcm_flag) {
1180 }
1181 }
1182
1184 &last_significant_coeff_x, &last_significant_coeff_y);
1185
1186 if (last_significant_coeff_x > 3) {
1188 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1189 (2 + (last_significant_coeff_x & 1)) +
1190 suffix;
1191 }
1192
1193 if (last_significant_coeff_y > 3) {
1195 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1196 (2 + (last_significant_coeff_y & 1)) +
1197 suffix;
1198 }
1199
1201 FFSWAP(
int, last_significant_coeff_x, last_significant_coeff_y);
1202
1203 x_cg_last_sig = last_significant_coeff_x >> 2;
1204 y_cg_last_sig = last_significant_coeff_y >> 2;
1205
1206 switch (scan_idx) {
1208 int last_x_c = last_significant_coeff_x & 3;
1209 int last_y_c = last_significant_coeff_y & 3;
1210
1214 if (trafo_size == 4) {
1217 } else if (trafo_size == 8) {
1221 } else if (trafo_size == 16) {
1225 } else { // trafo_size == 32
1229 }
1230 break;
1231 }
1237 num_coeff =
horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1238 break;
1239 default: //SCAN_VERT
1244 num_coeff =
horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1245 break;
1246 }
1247 num_coeff++;
1248 num_last_subset = (num_coeff - 1) >> 4;
1249
1250 for (i = num_last_subset; i >= 0; i--) {
1252 int x_cg, y_cg, x_c, y_c, pos;
1253 int implicit_non_zero_coeff = 0;
1254 int64_t trans_coeff_level;
1255 int prev_sig = 0;
1257 int rice_init = 0;
1258
1259 uint8_t significant_coeff_flag_idx[16];
1260 uint8_t nb_significant_coeff_flag = 0;
1261
1262 x_cg = scan_x_cg[i];
1263 y_cg = scan_y_cg[i];
1264
1265 if ((i < num_last_subset) && (i > 0)) {
1266 int ctx_cg = 0;
1267 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1268 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1269 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1270 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1271
1272 significant_coeff_group_flag[x_cg][y_cg] =
1274 implicit_non_zero_coeff = 1;
1275 } else {
1276 significant_coeff_group_flag[x_cg][y_cg] =
1277 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1278 (x_cg == 0 && y_cg == 0));
1279 }
1280
1281 last_scan_pos = num_coeff - offset - 1;
1282
1283 if (i == num_last_subset) {
1284 n_end = last_scan_pos - 1;
1285 significant_coeff_flag_idx[0] = last_scan_pos;
1286 nb_significant_coeff_flag = 1;
1287 } else {
1288 n_end = 15;
1289 }
1290
1291 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1292 prev_sig = !!significant_coeff_group_flag[x_cg + 1][y_cg];
1293 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1294 prev_sig += (!!significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1295
1296 if (significant_coeff_group_flag[x_cg][y_cg] && n_end >= 0) {
1297 static const uint8_t ctx_idx_map[] = {
1298 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8, // log2_trafo_size == 2
1299 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 0
1300 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, // prev_sig == 1
1301 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, // prev_sig == 2
1302 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // default
1303 };
1305 int scf_offset = 0;
1308 ctx_idx_map_p = (
uint8_t*) &ctx_idx_map[4 * 16];
1309 if (c_idx == 0) {
1310 scf_offset = 40;
1311 } else {
1312 scf_offset = 14 + 27;
1313 }
1314 } else {
1315 if (c_idx != 0)
1316 scf_offset = 27;
1317 if (log2_trafo_size == 2) {
1318 ctx_idx_map_p = (
uint8_t*) &ctx_idx_map[0];
1319 } else {
1320 ctx_idx_map_p = (
uint8_t*) &ctx_idx_map[(prev_sig + 1) << 4];
1321 if (c_idx == 0) {
1322 if ((x_cg > 0 || y_cg > 0))
1323 scf_offset += 3;
1324 if (log2_trafo_size == 3) {
1325 scf_offset += (scan_idx ==
SCAN_DIAG) ? 9 : 15;
1326 } else {
1327 scf_offset += 21;
1328 }
1329 } else {
1330 if (log2_trafo_size == 3)
1331 scf_offset += 9;
1332 else
1333 scf_offset += 12;
1334 }
1335 }
1336 }
1337 for (n = n_end; n > 0; n--) {
1338 x_c = scan_x_off[
n];
1339 y_c = scan_y_off[
n];
1341 significant_coeff_flag_idx[nb_significant_coeff_flag] =
n;
1342 nb_significant_coeff_flag++;
1343 implicit_non_zero_coeff = 0;
1344 }
1345 }
1346 if (implicit_non_zero_coeff == 0) {
1349 if (c_idx == 0) {
1350 scf_offset = 42;
1351 } else {
1352 scf_offset = 16 + 27;
1353 }
1354 } else {
1355 if (i == 0) {
1356 if (c_idx == 0)
1357 scf_offset = 0;
1358 else
1359 scf_offset = 27;
1360 } else {
1361 scf_offset = 2 + scf_offset;
1362 }
1363 }
1365 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1366 nb_significant_coeff_flag++;
1367 }
1368 } else {
1369 significant_coeff_flag_idx[nb_significant_coeff_flag] = 0;
1370 nb_significant_coeff_flag++;
1371 }
1372 }
1373
1374 n_end = nb_significant_coeff_flag;
1375
1376
1377 if (n_end) {
1378 int first_nz_pos_in_cg;
1379 int last_nz_pos_in_cg;
1380 int c_rice_param = 0;
1381 int first_greater1_coeff_idx = -1;
1382 uint8_t coeff_abs_level_greater1_flag[8];
1383 uint16_t coeff_sign_flag;
1384 int sum_abs = 0;
1385 int sign_hidden;
1386 int sb_type;
1387
1388
1389 // initialize first elem of coeff_bas_level_greater1_flag
1390 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1391
1394 sb_type = 2 * (c_idx == 0 ? 1 : 0);
1395 else
1396 sb_type = 2 * (c_idx == 0 ? 1 : 0) + 1;
1398 }
1399
1400 if (!(i == num_last_subset) && greater1_ctx == 0)
1401 ctx_set++;
1402 greater1_ctx = 1;
1403 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1404
1405 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1406 int inc = (ctx_set << 2) + greater1_ctx;
1407 coeff_abs_level_greater1_flag[
m] =
1409 if (coeff_abs_level_greater1_flag[m]) {
1410 greater1_ctx = 0;
1411 if (first_greater1_coeff_idx == -1)
1412 first_greater1_coeff_idx =
m;
1413 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1414 greater1_ctx++;
1415 }
1416 }
1417 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1418
1422 (pred_mode_intra == 10 || pred_mode_intra == 26 )) ||
1423 explicit_rdpcm_flag)
1424 sign_hidden = 0;
1425 else
1426 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4);
1427
1428 if (first_greater1_coeff_idx != -1) {
1430 }
1432 coeff_sign_flag =
coeff_sign_flag_decode(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1433 } else {
1434 coeff_sign_flag =
coeff_sign_flag_decode(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1435 }
1436
1437 for (m = 0; m < n_end; m++) {
1438 n = significant_coeff_flag_idx[
m];
1440 if (m < 8) {
1441 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[
m];
1442 if (trans_coeff_level == ((m == first_greater1_coeff_idx) ? 3 : 2)) {
1444
1445 trans_coeff_level += last_coeff_abs_level_remaining;
1446 if (trans_coeff_level > (3 << c_rice_param))
1449 int c_rice_p_init = lc->
stat_coeff[sb_type] / 4;
1450 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1452 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1455 rice_init = 1;
1456 }
1457 }
1458 } else {
1460
1461 trans_coeff_level = 1 + last_coeff_abs_level_remaining;
1462 if (trans_coeff_level > (3 << c_rice_param))
1465 int c_rice_p_init = lc->
stat_coeff[sb_type] / 4;
1466 if (last_coeff_abs_level_remaining >= (3 << c_rice_p_init))
1468 else if (2 * last_coeff_abs_level_remaining < (1 << c_rice_p_init))
1471 rice_init = 1;
1472 }
1473 }
1475 sum_abs += trans_coeff_level;
1476 if (n == first_nz_pos_in_cg && (sum_abs&1))
1477 trans_coeff_level = -trans_coeff_level;
1478 }
1479 if (coeff_sign_flag >> 15)
1480 trans_coeff_level = -trans_coeff_level;
1481 coeff_sign_flag <<= 1;
1484 if(y_c || x_c || log2_trafo_size < 4) {
1485 switch(log2_trafo_size) {
1486 case 3: pos = (y_c << 3) + x_c; break;
1487 case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1488 case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1489 default: pos = (y_c << 2) + x_c; break;
1490 }
1491 scale_m = scale_matrix[pos];
1492 } else {
1493 scale_m = dc_scale;
1494 }
1495 }
1496 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1497 if(trans_coeff_level < 0) {
1498 if((~trans_coeff_level) & 0xFffffffffff8000)
1499 trans_coeff_level = -32768;
1500 } else {
1501 if(trans_coeff_level & 0xffffffffffff8000)
1502 trans_coeff_level = 32767;
1503 }
1504 }
1505 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1506 }
1507 }
1508 }
1509
1512 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1514
1516 }
1517 } else {
1518 if (transform_skip_flag) {
1520 log2_trafo_size == 2 &&
1522 if (rot) {
1523 for (i = 0; i < 8; i++)
1524 FFSWAP(int16_t, coeffs[i], coeffs[16 - i - 1]);
1525 }
1526
1528
1531 (pred_mode_intra == 10 || pred_mode_intra == 26))) {
1532 int mode = explicit_rdpcm_flag ? explicit_rdpcm_dir_flag : (pred_mode_intra == 26);
1533
1535 }
1538 } else {
1539 int max_xy =
FFMAX(last_significant_coeff_x, last_significant_coeff_y);
1540 if (max_xy == 0)
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 }
1556
1557 for (i = 0; i < (trafo_size * trafo_size); i++) {
1559 }
1560 }
1562 }
1563
1565 {
1569
1570 if (x)
1572 if (y)
1574
1575 switch (x) {
1578 case 0: lc->
pu.
mvd.
x = 0;
break;
1579 }
1580
1581 switch (y) {
1584 case 0: lc->
pu.
mvd.
y = 0;
break;
1585 }
1586 }
1587