1 /*
2 * H.265 video codec.
3 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4 *
5 * Authors: struktur AG, Dirk Farin <farin@struktur.de>
6 * Min Chen <chenm003@163.com>
7 *
8 * This file is part of libde265.
9 *
10 * libde265 is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of
13 * the License, or (at your option) any later version.
14 *
15 * libde265 is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with libde265. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "slice.h"
25 #include "motion.h"
26 #include "util.h"
27 #include "scan.h"
28 #include "intrapred.h"
29 #include "transform.h"
30 #include "threads.h"
31 #include "image.h"
32
33 #include <assert.h>
34 #include <string.h>
35 #include <stdlib.h>
36
37
38 #define LOCK de265_mutex_lock(&ctx->thread_pool.mutex)
39 #define UNLOCK de265_mutex_unlock(&ctx->thread_pool.mutex)
40
41 extern bool read_short_term_ref_pic_set(error_queue* errqueue,
42 const seq_parameter_set* sps,
43 bitreader* br,
44 ref_pic_set* out_set,
45 int idxRps, // index of the set to be read
46 const std::vector<ref_pic_set>& sets,
47 bool sliceRefPicSet);
48
49
50 void read_coding_tree_unit(thread_context* tctx);
51 void read_coding_quadtree(thread_context* tctx,
52 int xCtb, int yCtb,
53 int Log2CtbSizeY,
54 int ctDepth);
55 /*
56 void decode_inter_block(decoder_context* ctx,thread_context* tctx,
57 int xC, int yC, int log2CbSize);
58 */
59
set_defaults()60 void slice_segment_header::set_defaults()
61 {
62 slice_index = 0;
63
64 first_slice_segment_in_pic_flag = 1;
65 no_output_of_prior_pics_flag = 0;
66 slice_pic_parameter_set_id = 0;
67 dependent_slice_segment_flag = 0;
68 slice_segment_address = 0;
69
70 slice_type = SLICE_TYPE_I;
71 pic_output_flag = 1;
72 colour_plane_id = 0;
73 slice_pic_order_cnt_lsb = 0;
74 short_term_ref_pic_set_sps_flag = 1;
75 // ref_pic_set slice_ref_pic_set;
76
77 short_term_ref_pic_set_idx = 0;
78 num_long_term_sps = 0;
79 num_long_term_pics = 0;
80
81 //uint8_t lt_idx_sps[MAX_NUM_REF_PICS];
82 //int poc_lsb_lt[MAX_NUM_REF_PICS];
83 //char used_by_curr_pic_lt_flag[MAX_NUM_REF_PICS];
84
85 //char delta_poc_msb_present_flag[MAX_NUM_REF_PICS];
86 //int delta_poc_msb_cycle_lt[MAX_NUM_REF_PICS];
87
88 slice_temporal_mvp_enabled_flag = 0;
89 slice_sao_luma_flag = 0;
90 slice_sao_chroma_flag = 0;
91
92 num_ref_idx_active_override_flag = 0;
93 num_ref_idx_l0_active=1; // [1;16]
94 num_ref_idx_l1_active=1; // [1;16]
95
96 ref_pic_list_modification_flag_l0 = 0;
97 ref_pic_list_modification_flag_l1 = 0;
98 //uint8_t list_entry_l0[16];
99 //uint8_t list_entry_l1[16];
100
101 mvd_l1_zero_flag = 0;
102 cabac_init_flag = 0;
103 collocated_from_l0_flag = 0;
104 collocated_ref_idx = 0;
105
106 // --- pred_weight_table ---
107
108 luma_log2_weight_denom=0; // [0;7]
109 ChromaLog2WeightDenom=0; // [0;7]
110
111 // first index is L0/L1
112 /*
113 uint8_t luma_weight_flag[2][16]; // bool
114 uint8_t chroma_weight_flag[2][16]; // bool
115 int16_t LumaWeight[2][16];
116 int8_t luma_offset[2][16];
117 int16_t ChromaWeight[2][16][2];
118 int8_t ChromaOffset[2][16][2];
119 */
120
121
122 five_minus_max_num_merge_cand = 0;
123 slice_qp_delta = 0;
124
125 slice_cb_qp_offset = 0;
126 slice_cr_qp_offset = 0;
127
128 cu_chroma_qp_offset_enabled_flag = 0;
129
130 deblocking_filter_override_flag = 0;
131 slice_deblocking_filter_disabled_flag = 0;
132 slice_beta_offset=0; // = pps->beta_offset if undefined
133 slice_tc_offset=0; // = pps->tc_offset if undefined
134
135 slice_loop_filter_across_slices_enabled_flag = 0;
136
137 num_entry_point_offsets = 0;
138 //int offset_len;
139 //std::vector<int> entry_point_offset;
140
141 slice_segment_header_extension_length = 0;
142
143 SliceAddrRS = slice_segment_address;
144 }
145
146
read_pred_weight_table(bitreader * br,slice_segment_header * shdr,decoder_context * ctx)147 bool read_pred_weight_table(bitreader* br, slice_segment_header* shdr, decoder_context* ctx)
148 {
149 int vlc;
150
151 pic_parameter_set* pps = ctx->get_pps((int)shdr->slice_pic_parameter_set_id);
152 assert(pps);
153 seq_parameter_set* sps = ctx->get_sps((int)pps->seq_parameter_set_id);
154 assert(sps);
155
156 shdr->luma_log2_weight_denom = vlc = get_uvlc(br);
157 if (vlc<0 || vlc>7) return false;
158
159 if (sps->chroma_format_idc != 0) {
160 vlc = get_svlc(br);
161 vlc += shdr->luma_log2_weight_denom;
162 if (vlc<0 || vlc>7) return false;
163 shdr->ChromaLog2WeightDenom = vlc;
164 }
165
166 int sumWeightFlags = 0;
167
168 for (int l=0;l<=1;l++)
169 if (l==0 || (l==1 && shdr->slice_type == SLICE_TYPE_B))
170 {
171 int num_ref = (l==0 ? shdr->num_ref_idx_l0_active-1 : shdr->num_ref_idx_l1_active-1);
172
173 for (int i=0;i<=num_ref;i++) {
174 shdr->luma_weight_flag[l][i] = get_bits(br,1);
175 if (shdr->luma_weight_flag[l][i]) sumWeightFlags++;
176 }
177
178 if (sps->chroma_format_idc != 0) {
179 for (int i=0;i<=num_ref;i++) {
180 shdr->chroma_weight_flag[l][i] = get_bits(br,1);
181 if (shdr->chroma_weight_flag[l][i]) sumWeightFlags+=2;
182 }
183 }
184
185 for (int i=0;i<=num_ref;i++) {
186 if (shdr->luma_weight_flag[l][i]) {
187
188 // delta_luma_weight
189
190 vlc = get_svlc(br);
191 if (vlc < -128 || vlc > 127) return false;
192
193 shdr->LumaWeight[l][i] = (1<<shdr->luma_log2_weight_denom) + vlc;
194
195 // luma_offset
196
197 vlc = get_svlc(br);
198 if (vlc < -sps->WpOffsetHalfRangeY || vlc > sps->WpOffsetHalfRangeY-1) return false;
199 shdr->luma_offset[l][i] = vlc;
200 }
201 else {
202 shdr->LumaWeight[l][i] = 1<<shdr->luma_log2_weight_denom;
203 shdr->luma_offset[l][i] = 0;
204 }
205
206 if (shdr->chroma_weight_flag[l][i])
207 for (int j=0;j<2;j++) {
208 // delta_chroma_weight
209
210 vlc = get_svlc(br);
211 if (vlc < -128 || vlc > 127) return false;
212
213 shdr->ChromaWeight[l][i][j] = (1<<shdr->ChromaLog2WeightDenom) + vlc;
214
215 // delta_chroma_offset
216
217 vlc = get_svlc(br);
218 if (vlc < -4*sps->WpOffsetHalfRangeC ||
219 vlc > 4*sps->WpOffsetHalfRangeC-1) return false;
220
221 vlc = Clip3(-sps->WpOffsetHalfRangeC,
222 sps->WpOffsetHalfRangeC-1,
223 (sps->WpOffsetHalfRangeC
224 +vlc
225 -((sps->WpOffsetHalfRangeC*shdr->ChromaWeight[l][i][j])
226 >> shdr->ChromaLog2WeightDenom)));
227
228 shdr->ChromaOffset[l][i][j] = vlc;
229 }
230 else {
231 for (int j=0;j<2;j++) {
232 shdr->ChromaWeight[l][i][j] = 1<<shdr->ChromaLog2WeightDenom;
233 shdr->ChromaOffset[l][i][j] = 0;
234 }
235 }
236 }
237 }
238
239 // TODO: bitstream conformance requires that 'sumWeightFlags<=24'
240
241 return true;
242 }
243
244
reset()245 void slice_segment_header::reset()
246 {
247 pps = NULL;
248
249 slice_index = 0;
250
251 first_slice_segment_in_pic_flag = 0;
252 no_output_of_prior_pics_flag = 0;
253 slice_pic_parameter_set_id = 0;
254 dependent_slice_segment_flag = 0;
255 slice_segment_address = 0;
256
257 slice_type = 0;
258 pic_output_flag = 0;
259 colour_plane_id = 0;
260 slice_pic_order_cnt_lsb = 0;
261 short_term_ref_pic_set_sps_flag = 0;
262 slice_ref_pic_set.reset();
263
264 short_term_ref_pic_set_idx = 0;
265 num_long_term_sps = 0;
266 num_long_term_pics= 0;
267
268 for (int i=0;i<MAX_NUM_REF_PICS;i++) {
269 lt_idx_sps[i] = 0;
270 poc_lsb_lt[i] = 0;
271 used_by_curr_pic_lt_flag[i] = 0;
272 delta_poc_msb_present_flag[i] = 0;
273 delta_poc_msb_cycle_lt[i] = 0;
274 }
275
276 slice_temporal_mvp_enabled_flag = 0;
277 slice_sao_luma_flag = 0;
278 slice_sao_chroma_flag = 0;
279
280 num_ref_idx_active_override_flag = 0;
281 num_ref_idx_l0_active = 0;
282 num_ref_idx_l1_active = 0;
283
284 ref_pic_list_modification_flag_l0 = 0;
285 ref_pic_list_modification_flag_l1 = 0;
286 for (int i=0;i<16;i++) {
287 list_entry_l0[i] = 0;
288 list_entry_l1[i] = 0;
289 }
290
291 mvd_l1_zero_flag = 0;
292 cabac_init_flag = 0;
293 collocated_from_l0_flag = 0;
294 collocated_ref_idx = 0;
295
296 luma_log2_weight_denom = 0;
297 ChromaLog2WeightDenom = 0;
298
299 for (int i=0;i<2;i++)
300 for (int j=0;j<16;j++) {
301 luma_weight_flag[i][j] = 0;
302 chroma_weight_flag[i][j] = 0;
303 LumaWeight[i][j] = 0;
304 luma_offset[i][j] = 0;
305 ChromaWeight[i][j][0] = ChromaWeight[i][j][1] = 0;
306 ChromaOffset[i][j][0] = ChromaOffset[i][j][1] = 0;
307 }
308
309 five_minus_max_num_merge_cand = 0;
310 slice_qp_delta = 0;
311
312 slice_cb_qp_offset = 0;
313 slice_cr_qp_offset = 0;
314
315 cu_chroma_qp_offset_enabled_flag = 0;
316
317 deblocking_filter_override_flag = 0;
318 slice_deblocking_filter_disabled_flag = 0;
319 slice_beta_offset = 0;
320 slice_tc_offset = 0;
321
322 slice_loop_filter_across_slices_enabled_flag = 0;
323
324 num_entry_point_offsets = 0;
325 offset_len = 0;
326 entry_point_offset.clear();
327
328 slice_segment_header_extension_length = 0;
329
330 SliceAddrRS = 0;
331 SliceQPY = 0;
332
333 initType = 0;
334
335 MaxNumMergeCand = 0;
336 CurrRpsIdx = 0;
337 CurrRps.reset();
338 NumPocTotalCurr = 0;
339
340 for (int i=0;i<2;i++)
341 for (int j=0;j<MAX_NUM_REF_PICS;j++) {
342 RefPicList[i][j] = 0;
343 RefPicList_POC[i][j] = 0;
344 RefPicList_PicState[i][j] = 0;
345 LongTermRefPic[i][j] = 0;
346 }
347
348 //context_model ctx_model_storage[CONTEXT_MODEL_TABLE_LENGTH];
349
350 RemoveReferencesList.clear();
351
352 ctx_model_storage_defined = false;
353 }
354
355
read(bitreader * br,decoder_context * ctx,bool * continueDecoding)356 de265_error slice_segment_header::read(bitreader* br, decoder_context* ctx,
357 bool* continueDecoding)
358 {
359 *continueDecoding = false;
360 reset();
361
362 // set defaults
363
364 dependent_slice_segment_flag = 0;
365
366
367 // read bitstream
368
369 first_slice_segment_in_pic_flag = get_bits(br,1);
370
371 if (ctx->get_RapPicFlag()) { // TODO: is this still correct ? Should we drop RapPicFlag ?
372 no_output_of_prior_pics_flag = get_bits(br,1);
373 }
374
375 slice_pic_parameter_set_id = get_uvlc(br);
376 if (slice_pic_parameter_set_id > DE265_MAX_PPS_SETS ||
377 slice_pic_parameter_set_id == UVLC_ERROR) {
378 ctx->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false);
379 return DE265_OK;
380 }
381
382 if (!ctx->has_pps(slice_pic_parameter_set_id)) {
383 ctx->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false);
384 return DE265_OK;
385 }
386
387 pps = ctx->get_shared_pps(slice_pic_parameter_set_id);
388
389 const seq_parameter_set* sps = pps->sps.get();
390 if (!sps->sps_read) {
391 ctx->add_warning(DE265_WARNING_NONEXISTING_SPS_REFERENCED, false);
392 *continueDecoding = false;
393 return DE265_OK;
394 }
395
396 if (!first_slice_segment_in_pic_flag) {
397 if (pps->dependent_slice_segments_enabled_flag) {
398 dependent_slice_segment_flag = get_bits(br,1);
399 } else {
400 dependent_slice_segment_flag = 0;
401 }
402
403 int slice_segment_address = get_bits(br, ceil_log2(sps->PicSizeInCtbsY));
404
405 if (dependent_slice_segment_flag) {
406 if (slice_segment_address == 0) {
407 *continueDecoding = false;
408 ctx->add_warning(DE265_WARNING_DEPENDENT_SLICE_WITH_ADDRESS_ZERO, false);
409 return DE265_OK;
410 }
411
412 if (ctx->previous_slice_header == NULL) {
413 return DE265_ERROR_NO_INITIAL_SLICE_HEADER;
414 }
415
416 *this = *ctx->previous_slice_header;
417
418 first_slice_segment_in_pic_flag = 0;
419 dependent_slice_segment_flag = 1;
420 }
421
422 this->slice_segment_address = slice_segment_address;
423 } else {
424 dependent_slice_segment_flag = 0;
425 slice_segment_address = 0;
426 }
427
428 if (slice_segment_address < 0 ||
429 slice_segment_address >= sps->PicSizeInCtbsY) {
430 ctx->add_warning(DE265_WARNING_SLICE_SEGMENT_ADDRESS_INVALID, false);
431 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
432 }
433
434 //printf("SLICE %d (%d)\n",slice_segment_address, sps->PicSizeInCtbsY);
435
436
437 if (!dependent_slice_segment_flag) {
438 for (int i=0; i<pps->num_extra_slice_header_bits; i++) {
439 //slice_reserved_undetermined_flag[i]
440 skip_bits(br,1);
441 }
442
443 slice_type = get_uvlc(br);
444 if (slice_type > 2 ||
445 slice_type == UVLC_ERROR) {
446 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
447 *continueDecoding = false;
448 return DE265_OK;
449 }
450
451 if (pps->output_flag_present_flag) {
452 pic_output_flag = get_bits(br,1);
453 }
454 else {
455 pic_output_flag = 1;
456 }
457
458 if (sps->separate_colour_plane_flag == 1) {
459 colour_plane_id = get_bits(br,2);
460 }
461
462
463 slice_pic_order_cnt_lsb = 0;
464 short_term_ref_pic_set_sps_flag = 0;
465
466 int NumLtPics = 0;
467
468 if (ctx->get_nal_unit_type() != NAL_UNIT_IDR_W_RADL &&
469 ctx->get_nal_unit_type() != NAL_UNIT_IDR_N_LP) {
470 slice_pic_order_cnt_lsb = get_bits(br, sps->log2_max_pic_order_cnt_lsb);
471 short_term_ref_pic_set_sps_flag = get_bits(br,1);
472
473 if (!short_term_ref_pic_set_sps_flag) {
474 read_short_term_ref_pic_set(ctx, sps,
475 br, &slice_ref_pic_set,
476 sps->num_short_term_ref_pic_sets(),
477 sps->ref_pic_sets,
478 true);
479
480 CurrRpsIdx = sps->num_short_term_ref_pic_sets();
481 CurrRps = slice_ref_pic_set;
482 }
483 else {
484 int nBits = ceil_log2(sps->num_short_term_ref_pic_sets());
485 if (nBits>0) short_term_ref_pic_set_idx = get_bits(br,nBits);
486 else short_term_ref_pic_set_idx = 0;
487
488 if (short_term_ref_pic_set_idx >= sps->num_short_term_ref_pic_sets()) {
489 ctx->add_warning(DE265_WARNING_SHORT_TERM_REF_PIC_SET_OUT_OF_RANGE, false);
490 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
491 }
492
493 CurrRpsIdx = short_term_ref_pic_set_idx;
494 CurrRps = sps->ref_pic_sets[CurrRpsIdx];
495 }
496
497
498 // --- long-term MC ---
499
500 if (sps->long_term_ref_pics_present_flag) {
501 if (sps->num_long_term_ref_pics_sps > 0) {
502 num_long_term_sps = get_uvlc(br);
503 if (num_long_term_sps == UVLC_ERROR) {
504 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
505 }
506 }
507 else {
508 num_long_term_sps = 0;
509 }
510
511 num_long_term_pics= get_uvlc(br);
512 if (num_long_term_pics == UVLC_ERROR) {
513 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
514 }
515
516 // check maximum number of reference frames
517
518 if (num_long_term_sps +
519 num_long_term_pics +
520 CurrRps.NumNegativePics +
521 CurrRps.NumPositivePics
522 > sps->sps_max_dec_pic_buffering[sps->sps_max_sub_layers-1])
523 {
524 ctx->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false);
525 *continueDecoding = false;
526 return DE265_OK;
527 }
528
529 for (int i=0; i<num_long_term_sps + num_long_term_pics; i++) {
530 if (i < num_long_term_sps) {
531 int nBits = ceil_log2(sps->num_long_term_ref_pics_sps);
532 lt_idx_sps[i] = get_bits(br, nBits);
533
534 // check that the referenced lt-reference really exists
535
536 if (lt_idx_sps[i] >= sps->num_long_term_ref_pics_sps) {
537 ctx->add_warning(DE265_NON_EXISTING_LT_REFERENCE_CANDIDATE_IN_SLICE_HEADER, false);
538 *continueDecoding = false;
539 return DE265_OK;
540 }
541
542 // delta_poc_msb_present_flag[i] = 0; // TODO ?
543
544 ctx->PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[ lt_idx_sps[i] ];
545 ctx->UsedByCurrPicLt[i] = sps->used_by_curr_pic_lt_sps_flag[ lt_idx_sps[i] ];
546 }
547 else {
548 int nBits = sps->log2_max_pic_order_cnt_lsb;
549 poc_lsb_lt[i] = get_bits(br, nBits);
550 used_by_curr_pic_lt_flag[i] = get_bits(br,1);
551
552 ctx->PocLsbLt[i] = poc_lsb_lt[i];
553 ctx->UsedByCurrPicLt[i] = used_by_curr_pic_lt_flag[i];
554 }
555
556 if (ctx->UsedByCurrPicLt[i]) {
557 NumLtPics++;
558 }
559
560 delta_poc_msb_present_flag[i] = get_bits(br,1);
561 if (delta_poc_msb_present_flag[i]) {
562 delta_poc_msb_cycle_lt[i] = get_uvlc(br);
563 if (delta_poc_msb_cycle_lt[i]==UVLC_ERROR) {
564 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
565 }
566 }
567 else {
568 delta_poc_msb_cycle_lt[i] = 0;
569 }
570
571 if (i==0 || i==num_long_term_sps) {
572 ctx->DeltaPocMsbCycleLt[i] = delta_poc_msb_cycle_lt[i];
573 }
574 else {
575 ctx->DeltaPocMsbCycleLt[i] = (delta_poc_msb_cycle_lt[i] +
576 ctx->DeltaPocMsbCycleLt[i-1]);
577 }
578 }
579 }
580 else {
581 num_long_term_sps = 0;
582 num_long_term_pics= 0;
583 }
584
585 if (sps->sps_temporal_mvp_enabled_flag) {
586 slice_temporal_mvp_enabled_flag = get_bits(br,1);
587 }
588 else {
589 slice_temporal_mvp_enabled_flag = 0;
590 }
591 }
592 else {
593 slice_pic_order_cnt_lsb = 0;
594 num_long_term_sps = 0;
595 num_long_term_pics= 0;
596 }
597
598
599 // --- SAO ---
600
601 if (sps->sample_adaptive_offset_enabled_flag) {
602 slice_sao_luma_flag = get_bits(br,1);
603
604 if (sps->ChromaArrayType != CHROMA_MONO) {
605 slice_sao_chroma_flag = get_bits(br,1);
606 }
607 else {
608 slice_sao_chroma_flag = 0;
609 }
610 }
611 else {
612 slice_sao_luma_flag = 0;
613 slice_sao_chroma_flag = 0;
614 }
615
616 num_ref_idx_l0_active = 0;
617 num_ref_idx_l1_active = 0;
618
619 if (slice_type == SLICE_TYPE_P ||
620 slice_type == SLICE_TYPE_B) {
621 num_ref_idx_active_override_flag = get_bits(br,1);
622 if (num_ref_idx_active_override_flag) {
623 num_ref_idx_l0_active = get_uvlc(br);
624 if (num_ref_idx_l0_active == UVLC_ERROR) {
625 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
626 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
627 }
628 num_ref_idx_l0_active++;;
629
630 if (slice_type == SLICE_TYPE_B) {
631 num_ref_idx_l1_active = get_uvlc(br);
632 if (num_ref_idx_l1_active == UVLC_ERROR) {
633 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
634 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
635 }
636 num_ref_idx_l1_active++;
637 }
638 }
639 else {
640 num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active;
641 num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active;
642 }
643
644 if (num_ref_idx_l0_active > 16) { return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; }
645 if (num_ref_idx_l1_active > 16) { return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; }
646
647 NumPocTotalCurr = CurrRps.NumPocTotalCurr_shortterm_only + NumLtPics;
648
649 if (pps->lists_modification_present_flag && NumPocTotalCurr > 1) {
650
651 int nBits = ceil_log2(NumPocTotalCurr);
652
653 ref_pic_list_modification_flag_l0 = get_bits(br,1);
654 if (ref_pic_list_modification_flag_l0) {
655 for (int i=0;i<num_ref_idx_l0_active;i++) {
656 list_entry_l0[i] = get_bits(br, nBits);
657 }
658 }
659
660 if (slice_type == SLICE_TYPE_B) {
661 ref_pic_list_modification_flag_l1 = get_bits(br,1);
662 if (ref_pic_list_modification_flag_l1) {
663 for (int i=0;i<num_ref_idx_l1_active;i++) {
664 list_entry_l1[i] = get_bits(br, nBits);
665 }
666 }
667 }
668 else {
669 ref_pic_list_modification_flag_l1 = 0;
670 }
671 }
672 else {
673 ref_pic_list_modification_flag_l0 = 0;
674 ref_pic_list_modification_flag_l1 = 0;
675 }
676
677 if (slice_type == SLICE_TYPE_B) {
678 mvd_l1_zero_flag = get_bits(br,1);
679 }
680
681 if (pps->cabac_init_present_flag) {
682 cabac_init_flag = get_bits(br,1);
683 }
684 else {
685 cabac_init_flag = 0;
686 }
687
688 if (slice_temporal_mvp_enabled_flag) {
689 if (slice_type == SLICE_TYPE_B)
690 collocated_from_l0_flag = get_bits(br,1);
691 else
692 collocated_from_l0_flag = 1;
693
694 if (( collocated_from_l0_flag && num_ref_idx_l0_active > 1) ||
695 (!collocated_from_l0_flag && num_ref_idx_l1_active > 1)) {
696 collocated_ref_idx = get_uvlc(br);
697 if (collocated_ref_idx == UVLC_ERROR) {
698 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
699 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
700 }
701 }
702 else {
703 collocated_ref_idx = 0;
704 }
705
706 // check whether collocated_ref_idx points to a valid index
707
708 if (( collocated_from_l0_flag && collocated_ref_idx >= num_ref_idx_l0_active) ||
709 (!collocated_from_l0_flag && collocated_ref_idx >= num_ref_idx_l1_active)) {
710 ctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
711 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
712 }
713 }
714
715
716 if ((pps->weighted_pred_flag && slice_type == SLICE_TYPE_P) ||
717 (pps->weighted_bipred_flag && slice_type == SLICE_TYPE_B)) {
718
719 if (!read_pred_weight_table(br,this,ctx))
720 {
721 ctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
722 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
723 }
724 }
725
726 five_minus_max_num_merge_cand = get_uvlc(br);
727 if (five_minus_max_num_merge_cand == UVLC_ERROR) {
728 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
729 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
730 }
731 MaxNumMergeCand = 5-five_minus_max_num_merge_cand;
732 }
733
734 slice_qp_delta = get_svlc(br);
735 if (slice_qp_delta == UVLC_ERROR) {
736 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
737 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
738 }
739 //logtrace(LogSlice,"slice_qp_delta: %d\n",shdr->slice_qp_delta);
740
741 if (pps->pps_slice_chroma_qp_offsets_present_flag) {
742 slice_cb_qp_offset = get_svlc(br);
743 if (slice_cb_qp_offset == UVLC_ERROR) {
744 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
745 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
746 }
747
748 slice_cr_qp_offset = get_svlc(br);
749 if (slice_cr_qp_offset == UVLC_ERROR) {
750 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
751 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
752 }
753 }
754 else {
755 slice_cb_qp_offset = 0;
756 slice_cr_qp_offset = 0;
757 }
758
759 if (pps->range_extension.chroma_qp_offset_list_enabled_flag) {
760 cu_chroma_qp_offset_enabled_flag = get_bits(br,1);
761 }
762
763 if (pps->deblocking_filter_override_enabled_flag) {
764 deblocking_filter_override_flag = get_bits(br,1);
765 }
766 else {
767 deblocking_filter_override_flag = 0;
768 }
769
770 slice_beta_offset = pps->beta_offset;
771 slice_tc_offset = pps->tc_offset;
772
773 if (deblocking_filter_override_flag) {
774 slice_deblocking_filter_disabled_flag = get_bits(br,1);
775 if (!slice_deblocking_filter_disabled_flag) {
776 slice_beta_offset = get_svlc(br);
777 if (slice_beta_offset == UVLC_ERROR) {
778 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
779 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
780 }
781 slice_beta_offset *= 2;
782
783 slice_tc_offset = get_svlc(br);
784 if (slice_tc_offset == UVLC_ERROR) {
785 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
786 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
787 }
788 slice_tc_offset *= 2;
789 }
790 }
791 else {
792 slice_deblocking_filter_disabled_flag = pps->pic_disable_deblocking_filter_flag;
793 }
794
795 if (pps->pps_loop_filter_across_slices_enabled_flag &&
796 (slice_sao_luma_flag || slice_sao_chroma_flag ||
797 !slice_deblocking_filter_disabled_flag )) {
798 slice_loop_filter_across_slices_enabled_flag = get_bits(br,1);
799 }
800 else {
801 slice_loop_filter_across_slices_enabled_flag =
802 pps->pps_loop_filter_across_slices_enabled_flag;
803 }
804 }
805
806 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag ) {
807 num_entry_point_offsets = get_uvlc(br);
808 if (num_entry_point_offsets == UVLC_ERROR) {
809 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
810 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
811 }
812
813 if (pps->entropy_coding_sync_enabled_flag) {
814 // check num_entry_points for valid range
815
816 int firstCTBRow = slice_segment_address / sps->PicWidthInCtbsY;
817 int lastCTBRow = firstCTBRow + num_entry_point_offsets;
818 if (lastCTBRow >= sps->PicHeightInCtbsY) {
819 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
820 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
821 }
822 }
823
824 if (pps->tiles_enabled_flag) {
825 if (num_entry_point_offsets > pps->num_tile_columns * pps->num_tile_rows) {
826 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
827 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
828 }
829 }
830
831 entry_point_offset.resize( num_entry_point_offsets );
832
833 if (num_entry_point_offsets > 0) {
834 offset_len = get_uvlc(br);
835 if (offset_len == UVLC_ERROR) {
836 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
837 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
838 }
839 offset_len++;
840
841 if (offset_len > 32) {
842 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
843 }
844
845 for (int i=0; i<num_entry_point_offsets; i++) {
846 {
847 entry_point_offset[i] = get_bits(br,offset_len)+1;
848 }
849
850 if (i>0) {
851 entry_point_offset[i] += entry_point_offset[i-1];
852 }
853 }
854 }
855 }
856 else {
857 num_entry_point_offsets = 0;
858 }
859
860 if (pps->slice_segment_header_extension_present_flag) {
861 slice_segment_header_extension_length = get_uvlc(br);
862 if (slice_segment_header_extension_length == UVLC_ERROR ||
863 slice_segment_header_extension_length > 1000) { // TODO: safety check against too large values
864 ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
865 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
866 }
867
868 for (int i=0; i<slice_segment_header_extension_length; i++) {
869 //slice_segment_header_extension_data_byte[i]
870 get_bits(br,8);
871 }
872 }
873
874
875 compute_derived_values(pps.get());
876
877 *continueDecoding = true;
878 return DE265_OK;
879 }
880
881
write(error_queue * errqueue,CABAC_encoder & out,const seq_parameter_set * sps,const pic_parameter_set * pps,uint8_t nal_unit_type)882 de265_error slice_segment_header::write(error_queue* errqueue, CABAC_encoder& out,
883 const seq_parameter_set* sps,
884 const pic_parameter_set* pps,
885 uint8_t nal_unit_type)
886 {
887 out.write_bit(first_slice_segment_in_pic_flag);
888
889 if (isRapPic(nal_unit_type)) { // TODO: is this still correct ? Should we drop RapPicFlag ?
890 out.write_bit(no_output_of_prior_pics_flag);
891 }
892
893 if (slice_pic_parameter_set_id > DE265_MAX_PPS_SETS) {
894 errqueue->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false);
895 return DE265_OK;
896 }
897 out.write_uvlc(slice_pic_parameter_set_id);
898
899 if (!first_slice_segment_in_pic_flag) {
900 if (pps->dependent_slice_segments_enabled_flag) {
901 out.write_bit(dependent_slice_segment_flag);
902 }
903
904 out.write_bits(slice_segment_address, ceil_log2(sps->PicSizeInCtbsY));
905
906 if (dependent_slice_segment_flag) {
907 if (slice_segment_address == 0) {
908 errqueue->add_warning(DE265_WARNING_DEPENDENT_SLICE_WITH_ADDRESS_ZERO, false);
909 return DE265_OK;
910 }
911 }
912 }
913
914 if (slice_segment_address < 0 ||
915 slice_segment_address > sps->PicSizeInCtbsY) {
916 errqueue->add_warning(DE265_WARNING_SLICE_SEGMENT_ADDRESS_INVALID, false);
917 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
918 }
919
920
921
922 if (!dependent_slice_segment_flag) {
923 for (int i=0; i<pps->num_extra_slice_header_bits; i++) {
924 //slice_reserved_undetermined_flag[i]
925 out.skip_bits(1);
926 }
927
928 if (slice_type > 2) {
929 errqueue->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
930 return DE265_OK;
931 }
932 out.write_uvlc(slice_type);
933
934 if (pps->output_flag_present_flag) {
935 out.write_bit(pic_output_flag);
936 }
937
938 if (sps->separate_colour_plane_flag == 1) {
939 out.write_bits(colour_plane_id,2);
940 }
941
942
943 int NumLtPics = 0;
944
945 if (nal_unit_type != NAL_UNIT_IDR_W_RADL &&
946 nal_unit_type != NAL_UNIT_IDR_N_LP) {
947 out.write_bits(slice_pic_order_cnt_lsb, sps->log2_max_pic_order_cnt_lsb);
948 out.write_bit(short_term_ref_pic_set_sps_flag);
949
950 if (!short_term_ref_pic_set_sps_flag) {
951 /* TODO
952 read_short_term_ref_pic_set(ctx, sps,
953 br, &slice_ref_pic_set,
954 sps->num_short_term_ref_pic_sets,
955 sps->ref_pic_sets,
956 true);
957 */
958 //CurrRpsIdx = sps->num_short_term_ref_pic_sets;
959 //CurrRps = slice_ref_pic_set;
960 }
961 else {
962 int nBits = ceil_log2(sps->num_short_term_ref_pic_sets());
963 if (nBits>0) out.write_bits(short_term_ref_pic_set_idx,nBits);
964 else { assert(short_term_ref_pic_set_idx==0); }
965
966 if (short_term_ref_pic_set_idx > sps->num_short_term_ref_pic_sets()) {
967 errqueue->add_warning(DE265_WARNING_SHORT_TERM_REF_PIC_SET_OUT_OF_RANGE, false);
968 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
969 }
970
971 //CurrRpsIdx = short_term_ref_pic_set_idx;
972 //CurrRps = sps->ref_pic_sets[CurrRpsIdx];
973 }
974
975
976 // --- long-term MC ---
977
978 if (sps->long_term_ref_pics_present_flag) {
979 if (sps->num_long_term_ref_pics_sps > 0) {
980 out.write_uvlc(num_long_term_sps);
981 }
982 else {
983 assert(num_long_term_sps == 0);
984 }
985
986 out.write_uvlc(num_long_term_pics);
987
988
989 // check maximum number of reference frames
990
991 if (num_long_term_sps +
992 num_long_term_pics +
993 CurrRps.NumNegativePics +
994 CurrRps.NumPositivePics
995 > sps->sps_max_dec_pic_buffering[sps->sps_max_sub_layers-1])
996 {
997 errqueue->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false);
998 return DE265_OK;
999 }
1000
1001 for (int i=0; i<num_long_term_sps + num_long_term_pics; i++) {
1002 if (i < num_long_term_sps) {
1003 int nBits = ceil_log2(sps->num_long_term_ref_pics_sps);
1004 out.write_bits(lt_idx_sps[i], nBits);
1005
1006 // check that the referenced lt-reference really exists
1007
1008 if (lt_idx_sps[i] >= sps->num_long_term_ref_pics_sps) {
1009 errqueue->add_warning(DE265_NON_EXISTING_LT_REFERENCE_CANDIDATE_IN_SLICE_HEADER, false);
1010 return DE265_OK;
1011 }
1012
1013 //ctx->PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[ lt_idx_sps[i] ];
1014 //ctx->UsedByCurrPicLt[i] = sps->used_by_curr_pic_lt_sps_flag[ lt_idx_sps[i] ];
1015 }
1016 else {
1017 int nBits = sps->log2_max_pic_order_cnt_lsb;
1018 out.write_bits(poc_lsb_lt[i], nBits);
1019 out.write_bit(used_by_curr_pic_lt_flag[i]);
1020
1021 //ctx->PocLsbLt[i] = poc_lsb_lt[i];
1022 //ctx->UsedByCurrPicLt[i] = used_by_curr_pic_lt_flag[i];
1023 }
1024
1025 //if (ctx->UsedByCurrPicLt[i]) {
1026 //NumLtPics++;
1027 //}
1028
1029 out.write_bit(delta_poc_msb_present_flag[i]);
1030 if (delta_poc_msb_present_flag[i]) {
1031 out.write_uvlc(delta_poc_msb_cycle_lt[i]);
1032 }
1033 else {
1034 assert(delta_poc_msb_cycle_lt[i] == 0);
1035 }
1036
1037 /*
1038 if (i==0 || i==num_long_term_sps) {
1039 ctx->DeltaPocMsbCycleLt[i] = delta_poc_msb_cycle_lt[i];
1040 }
1041 else {
1042 ctx->DeltaPocMsbCycleLt[i] = (delta_poc_msb_cycle_lt[i] +
1043 ctx->DeltaPocMsbCycleLt[i-1]);
1044 }
1045 */
1046 }
1047 }
1048 else {
1049 assert(num_long_term_sps == 0);
1050 assert(num_long_term_pics== 0);
1051 }
1052
1053 if (sps->sps_temporal_mvp_enabled_flag) {
1054 out.write_bit(slice_temporal_mvp_enabled_flag);
1055 }
1056 else {
1057 assert(slice_temporal_mvp_enabled_flag == 0);
1058 }
1059 }
1060 else {
1061 assert(slice_pic_order_cnt_lsb == 0);
1062 assert(num_long_term_sps == 0);
1063 assert(num_long_term_pics== 0);
1064 }
1065
1066
1067 // --- SAO ---
1068
1069 if (sps->sample_adaptive_offset_enabled_flag) {
1070 out.write_bit(slice_sao_luma_flag);
1071 out.write_bit(slice_sao_chroma_flag);
1072 }
1073 else {
1074 assert(slice_sao_luma_flag == 0);
1075 assert(slice_sao_chroma_flag== 0);
1076 }
1077
1078 if (slice_type == SLICE_TYPE_P ||
1079 slice_type == SLICE_TYPE_B) {
1080 out.write_bit(num_ref_idx_active_override_flag);
1081
1082 if (num_ref_idx_active_override_flag) {
1083 out.write_uvlc(num_ref_idx_l0_active);
1084 num_ref_idx_l0_active++;;
1085
1086 if (slice_type == SLICE_TYPE_B) {
1087 out.write_uvlc(num_ref_idx_l1_active);
1088 num_ref_idx_l1_active++;
1089 }
1090 }
1091 else {
1092 assert(num_ref_idx_l0_active == pps->num_ref_idx_l0_default_active);
1093 assert(num_ref_idx_l1_active == pps->num_ref_idx_l1_default_active);
1094 }
1095
1096 NumPocTotalCurr = CurrRps.NumPocTotalCurr_shortterm_only + NumLtPics;
1097
1098 if (pps->lists_modification_present_flag && NumPocTotalCurr > 1) {
1099
1100 int nBits = ceil_log2(NumPocTotalCurr);
1101
1102 out.write_bit(ref_pic_list_modification_flag_l0);
1103 if (ref_pic_list_modification_flag_l0) {
1104 for (int i=0;i<num_ref_idx_l0_active;i++) {
1105 out.write_bits(list_entry_l0[i], nBits);
1106 }
1107 }
1108
1109 if (slice_type == SLICE_TYPE_B) {
1110 out.write_bit(ref_pic_list_modification_flag_l1);
1111 if (ref_pic_list_modification_flag_l1) {
1112 for (int i=0;i<num_ref_idx_l1_active;i++) {
1113 out.write_bits(list_entry_l1[i], nBits);
1114 }
1115 }
1116 }
1117 else {
1118 assert(ref_pic_list_modification_flag_l1 == 0);
1119 }
1120 }
1121 else {
1122 assert(ref_pic_list_modification_flag_l0 == 0);
1123 assert(ref_pic_list_modification_flag_l1 == 0);
1124 }
1125
1126 if (slice_type == SLICE_TYPE_B) {
1127 out.write_bit(mvd_l1_zero_flag);
1128 }
1129
1130 if (pps->cabac_init_present_flag) {
1131 out.write_bit(cabac_init_flag);
1132 }
1133 else {
1134 assert(cabac_init_flag == 0);
1135 }
1136
1137 if (slice_temporal_mvp_enabled_flag) {
1138 if (slice_type == SLICE_TYPE_B)
1139 out.write_bit(collocated_from_l0_flag);
1140 else
1141 { assert(collocated_from_l0_flag == 1); }
1142
1143 if (( collocated_from_l0_flag && num_ref_idx_l0_active > 1) ||
1144 (!collocated_from_l0_flag && num_ref_idx_l1_active > 1)) {
1145 out.write_uvlc(collocated_ref_idx);
1146 }
1147 else {
1148 assert(collocated_ref_idx == 0);
1149 }
1150 }
1151
1152 if ((pps->weighted_pred_flag && slice_type == SLICE_TYPE_P) ||
1153 (pps->weighted_bipred_flag && slice_type == SLICE_TYPE_B)) {
1154
1155 assert(0);
1156 /* TODO
1157 if (!read_pred_weight_table(br,this,ctx))
1158 {
1159 ctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
1160 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1161 }
1162 */
1163 }
1164
1165 out.write_uvlc(five_minus_max_num_merge_cand);
1166 //MaxNumMergeCand = 5-five_minus_max_num_merge_cand;
1167 }
1168
1169 out.write_svlc(slice_qp_delta);
1170
1171 if (pps->pps_slice_chroma_qp_offsets_present_flag) {
1172 out.write_svlc(slice_cb_qp_offset);
1173 out.write_svlc(slice_cr_qp_offset);
1174 }
1175 else {
1176 assert(slice_cb_qp_offset == 0);
1177 assert(slice_cr_qp_offset == 0);
1178 }
1179
1180 if (pps->deblocking_filter_override_enabled_flag) {
1181 out.write_bit(deblocking_filter_override_flag);
1182 }
1183 else {
1184 assert(deblocking_filter_override_flag == 0);
1185 }
1186
1187 //slice_beta_offset = pps->beta_offset;
1188 //slice_tc_offset = pps->tc_offset;
1189
1190 if (deblocking_filter_override_flag) {
1191 out.write_bit(slice_deblocking_filter_disabled_flag);
1192 if (!slice_deblocking_filter_disabled_flag) {
1193 out.write_svlc(slice_beta_offset/2);
1194 out.write_svlc(slice_tc_offset /2);
1195 }
1196 }
1197 else {
1198 assert(slice_deblocking_filter_disabled_flag == pps->pic_disable_deblocking_filter_flag);
1199 }
1200
1201 if (pps->pps_loop_filter_across_slices_enabled_flag &&
1202 (slice_sao_luma_flag || slice_sao_chroma_flag ||
1203 !slice_deblocking_filter_disabled_flag )) {
1204 out.write_bit(slice_loop_filter_across_slices_enabled_flag);
1205 }
1206 else {
1207 assert(slice_loop_filter_across_slices_enabled_flag ==
1208 pps->pps_loop_filter_across_slices_enabled_flag);
1209 }
1210 }
1211
1212 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag ) {
1213 out.write_uvlc(num_entry_point_offsets);
1214
1215 if (num_entry_point_offsets > 0) {
1216 out.write_uvlc(offset_len-1);
1217
1218 for (int i=0; i<num_entry_point_offsets; i++) {
1219 {
1220 int prev=0;
1221 if (i>0) prev = entry_point_offset[i-1];
1222 out.write_bits(entry_point_offset[i]-prev-1, offset_len);
1223 }
1224 }
1225 }
1226 }
1227 else {
1228 assert(num_entry_point_offsets == 0);
1229 }
1230
1231 if (pps->slice_segment_header_extension_present_flag) {
1232 out.write_uvlc(slice_segment_header_extension_length);
1233 if (slice_segment_header_extension_length > 1000) { // TODO: safety check against too large values
1234 errqueue->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false);
1235 return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1236 }
1237
1238 for (int i=0; i<slice_segment_header_extension_length; i++) {
1239 //slice_segment_header_extension_data_byte[i]
1240 out.skip_bits(8);
1241 }
1242 }
1243
1244 return DE265_OK;
1245 }
1246
compute_derived_values(const pic_parameter_set * pps)1247 void slice_segment_header::compute_derived_values(const pic_parameter_set* pps)
1248 {
1249 // --- init variables ---
1250
1251 SliceQPY = pps->pic_init_qp + slice_qp_delta;
1252
1253 switch (slice_type)
1254 {
1255 case SLICE_TYPE_I: initType = 0; break;
1256 case SLICE_TYPE_P: initType = cabac_init_flag + 1; break;
1257 case SLICE_TYPE_B: initType = 2 - cabac_init_flag; break;
1258 }
1259
1260 MaxNumMergeCand = 5-five_minus_max_num_merge_cand;
1261 }
1262
1263
1264 //-----------------------------------------------------------------------
1265
1266
dump_slice_segment_header(const decoder_context * ctx,int fd) const1267 void slice_segment_header::dump_slice_segment_header(const decoder_context* ctx, int fd) const
1268 {
1269 FILE* fh;
1270 if (fd==1) fh=stdout;
1271 else if (fd==2) fh=stderr;
1272 else { return; }
1273
1274 #define LOG0(t) log2fh(fh, t)
1275 #define LOG1(t,d) log2fh(fh, t,d)
1276 #define LOG2(t,d1,d2) log2fh(fh, t,d1,d2)
1277 #define LOG3(t,d1,d2,d3) log2fh(fh, t,d1,d2,d3)
1278 #define LOG4(t,d1,d2,d3,d4) log2fh(fh, t,d1,d2,d3,d4)
1279
1280 const pic_parameter_set* pps = ctx->get_pps(slice_pic_parameter_set_id);
1281 assert(pps->pps_read); // TODO: error handling
1282
1283 const seq_parameter_set* sps = ctx->get_sps((int)pps->seq_parameter_set_id);
1284 assert(sps->sps_read); // TODO: error handling
1285
1286
1287 LOG0("----------------- SLICE -----------------\n");
1288 LOG1("first_slice_segment_in_pic_flag : %d\n", first_slice_segment_in_pic_flag);
1289 if (ctx->get_nal_unit_type() >= NAL_UNIT_BLA_W_LP &&
1290 ctx->get_nal_unit_type() <= NAL_UNIT_RESERVED_IRAP_VCL23) {
1291 LOG1("no_output_of_prior_pics_flag : %d\n", no_output_of_prior_pics_flag);
1292 }
1293
1294 LOG1("slice_pic_parameter_set_id : %d\n", slice_pic_parameter_set_id);
1295
1296 if (!first_slice_segment_in_pic_flag) {
1297 //if (pps->dependent_slice_segments_enabled_flag) {
1298 LOG1("dependent_slice_segment_flag : %d\n", dependent_slice_segment_flag);
1299 //}
1300 LOG1("slice_segment_address : %d\n", slice_segment_address);
1301 }
1302
1303 //if (!dependent_slice_segment_flag)
1304 {
1305 //for (int i=0; i<pps->num_extra_slice_header_bits; i++) {
1306 //slice_reserved_flag[i]
1307
1308 LOG1("slice_type : %c\n",
1309 slice_type == 0 ? 'B' :
1310 slice_type == 1 ? 'P' : 'I');
1311
1312 if (pps->output_flag_present_flag) {
1313 LOG1("pic_output_flag : %d\n", pic_output_flag);
1314 }
1315
1316 if (sps->separate_colour_plane_flag == 1) {
1317 LOG1("colour_plane_id : %d\n", colour_plane_id);
1318 }
1319
1320 LOG1("slice_pic_order_cnt_lsb : %d\n", slice_pic_order_cnt_lsb);
1321
1322 if (ctx->get_nal_unit_type() != NAL_UNIT_IDR_W_RADL &&
1323 ctx->get_nal_unit_type() != NAL_UNIT_IDR_N_LP) {
1324 LOG1("short_term_ref_pic_set_sps_flag : %d\n", short_term_ref_pic_set_sps_flag);
1325
1326 if (!short_term_ref_pic_set_sps_flag) {
1327 LOG1("ref_pic_set[ %2d ]: ",sps->num_short_term_ref_pic_sets());
1328 dump_compact_short_term_ref_pic_set(&slice_ref_pic_set, 16, fh);
1329 }
1330 else if (sps->num_short_term_ref_pic_sets() > 1) {
1331 LOG1("short_term_ref_pic_set_idx : %d\n", short_term_ref_pic_set_idx);
1332 dump_compact_short_term_ref_pic_set(&sps->ref_pic_sets[short_term_ref_pic_set_idx], 16, fh);
1333 }
1334
1335 if (sps->long_term_ref_pics_present_flag) {
1336 if (sps->num_long_term_ref_pics_sps > 0) {
1337 LOG1("num_long_term_sps : %d\n", num_long_term_sps);
1338 }
1339
1340 LOG1("num_long_term_pics : %d\n", num_long_term_pics);
1341
1342 #if 0
1343 for (int i=0; i<num_long_term_sps + num_long_term_pics; i++) {
1344 LOG2("PocLsbLt[%d] : %d\n", i, ctx->PocLsbLt[i]);
1345 LOG2("UsedByCurrPicLt[%d] : %d\n", i, ctx->UsedByCurrPicLt[i]);
1346 LOG2("DeltaPocMsbCycleLt[%d] : %d\n", i, ctx->DeltaPocMsbCycleLt[i]);
1347 }
1348 #endif
1349 }
1350
1351 if (sps->sps_temporal_mvp_enabled_flag) {
1352 LOG1("slice_temporal_mvp_enabled_flag : %d\n", slice_temporal_mvp_enabled_flag);
1353 }
1354 }
1355
1356
1357 if (sps->sample_adaptive_offset_enabled_flag) {
1358 LOG1("slice_sao_luma_flag : %d\n", slice_sao_luma_flag);
1359 LOG1("slice_sao_chroma_flag : %d\n", slice_sao_chroma_flag);
1360 }
1361
1362
1363 if (slice_type == SLICE_TYPE_P || slice_type == SLICE_TYPE_B) {
1364 LOG1("num_ref_idx_active_override_flag : %d\n", num_ref_idx_active_override_flag);
1365
1366 LOG2("num_ref_idx_l0_active : %d %s\n", num_ref_idx_l0_active,
1367 num_ref_idx_active_override_flag ? "" : "(from PPS)");
1368
1369 if (slice_type == SLICE_TYPE_B) {
1370 LOG2("num_ref_idx_l1_active : %d %s\n", num_ref_idx_l1_active,
1371 num_ref_idx_active_override_flag ? "" : "(from PPS)");
1372 }
1373
1374 if (pps->lists_modification_present_flag && NumPocTotalCurr > 1)
1375 {
1376 LOG1("ref_pic_list_modification_flag_l0 : %d\n", ref_pic_list_modification_flag_l0);
1377 if (ref_pic_list_modification_flag_l0) {
1378 for (int i=0;i<num_ref_idx_l0_active;i++) {
1379 LOG2(" %d: %d\n",i,list_entry_l0[i]);
1380 }
1381 }
1382
1383 LOG1("ref_pic_list_modification_flag_l1 : %d\n", ref_pic_list_modification_flag_l1);
1384 if (ref_pic_list_modification_flag_l1) {
1385 for (int i=0;i<num_ref_idx_l1_active;i++) {
1386 LOG2(" %d: %d\n",i,list_entry_l1[i]);
1387 }
1388 }
1389 }
1390
1391 if (slice_type == SLICE_TYPE_B) {
1392 LOG1("mvd_l1_zero_flag : %d\n", mvd_l1_zero_flag);
1393 }
1394
1395 LOG1("cabac_init_flag : %d\n", cabac_init_flag);
1396
1397 if (slice_temporal_mvp_enabled_flag) {
1398 LOG1("collocated_from_l0_flag : %d\n", collocated_from_l0_flag);
1399 LOG1("collocated_ref_idx : %d\n", collocated_ref_idx);
1400 }
1401
1402 if ((pps->weighted_pred_flag && slice_type == SLICE_TYPE_P) ||
1403 (pps->weighted_bipred_flag && slice_type == SLICE_TYPE_B))
1404 {
1405 LOG1("luma_log2_weight_denom : %d\n", luma_log2_weight_denom);
1406 if (sps->chroma_format_idc != 0) {
1407 LOG1("ChromaLog2WeightDenom : %d\n", ChromaLog2WeightDenom);
1408 }
1409
1410 for (int l=0;l<=1;l++)
1411 if (l==0 || (l==1 && slice_type == SLICE_TYPE_B))
1412 {
1413 int num_ref = (l==0 ?
1414 num_ref_idx_l0_active-1 :
1415 num_ref_idx_l1_active-1);
1416
1417 if (false) { // do not show these flags
1418 for (int i=0;i<=num_ref;i++) {
1419 LOG3("luma_weight_flag_l%d[%d] : %d\n",l,i,luma_weight_flag[l][i]);
1420 }
1421
1422 if (sps->chroma_format_idc != 0) {
1423 for (int i=0;i<=num_ref;i++) {
1424 LOG3("chroma_weight_flag_l%d[%d] : %d\n",l,i,chroma_weight_flag[l][i]);
1425 }
1426 }
1427 }
1428
1429 for (int i=0;i<=num_ref;i++) {
1430 LOG3("LumaWeight_L%d[%d] : %d\n",l,i,LumaWeight[l][i]);
1431 LOG3("luma_offset_l%d[%d] : %d\n",l,i,luma_offset[l][i]);
1432
1433 for (int j=0;j<2;j++) {
1434 LOG4("ChromaWeight_L%d[%d][%d] : %d\n",l,i,j,ChromaWeight[l][i][j]);
1435 LOG4("ChromaOffset_L%d[%d][%d] : %d\n",l,i,j,ChromaOffset[l][i][j]);
1436 }
1437 }
1438 }
1439 }
1440
1441 LOG1("five_minus_max_num_merge_cand : %d\n", five_minus_max_num_merge_cand);
1442 }
1443
1444
1445 LOG1("slice_qp_delta : %d\n", slice_qp_delta);
1446 if (pps->pps_slice_chroma_qp_offsets_present_flag) {
1447 LOG1("slice_cb_qp_offset : %d\n", slice_cb_qp_offset);
1448 LOG1("slice_cr_qp_offset : %d\n", slice_cr_qp_offset);
1449 }
1450
1451 if (pps->deblocking_filter_override_enabled_flag) {
1452 LOG1("deblocking_filter_override_flag : %d\n", deblocking_filter_override_flag);
1453 }
1454
1455 LOG2("slice_deblocking_filter_disabled_flag : %d %s\n",
1456 slice_deblocking_filter_disabled_flag,
1457 (deblocking_filter_override_flag ? "(override)" : "(from pps)"));
1458
1459 if (deblocking_filter_override_flag) {
1460
1461 if (!slice_deblocking_filter_disabled_flag) {
1462 LOG1("slice_beta_offset : %d\n", slice_beta_offset);
1463 LOG1("slice_tc_offset : %d\n", slice_tc_offset);
1464 }
1465 }
1466
1467 if (pps->pps_loop_filter_across_slices_enabled_flag &&
1468 (slice_sao_luma_flag || slice_sao_chroma_flag ||
1469 !slice_deblocking_filter_disabled_flag)) {
1470 LOG1("slice_loop_filter_across_slices_enabled_flag : %d\n",
1471 slice_loop_filter_across_slices_enabled_flag);
1472 }
1473 }
1474
1475 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1476 LOG1("num_entry_point_offsets : %d\n", num_entry_point_offsets);
1477
1478 if (num_entry_point_offsets > 0) {
1479 LOG1("offset_len : %d\n", offset_len);
1480
1481 for (int i=0; i<num_entry_point_offsets; i++) {
1482 LOG2("entry point [%i] : %d\n", i, entry_point_offset[i]);
1483 }
1484 }
1485 }
1486
1487 /*
1488 if( slice_segment_header_extension_present_flag ) {
1489 slice_segment_header_extension_length
1490 for( i = 0; i < slice_segment_header_extension_length; i++)
1491 slice_segment_header_extension_data_byte[i]
1492 }
1493 byte_alignment()
1494 }
1495 */
1496
1497 #undef LOG0
1498 #undef LOG1
1499 #undef LOG2
1500 #undef LOG3
1501 #undef LOG4
1502 //#endif
1503 }
1504
1505
1506
initialize_CABAC_models(thread_context * tctx)1507 void initialize_CABAC_models(thread_context* tctx)
1508 {
1509 const int QPY = tctx->shdr->SliceQPY;
1510 const int initType = tctx->shdr->initType;
1511 assert(initType >= 0 && initType <= 2);
1512
1513 tctx->ctx_model.init(initType, QPY);
1514
1515 for (int i=0;i<4;i++) {
1516 tctx->StatCoeff[i] = 0;
1517 }
1518 }
1519
1520
1521
decode_transform_skip_flag(thread_context * tctx,int cIdx)1522 static int decode_transform_skip_flag(thread_context* tctx, int cIdx)
1523 {
1524 const int context = (cIdx==0) ? 0 : 1;
1525
1526 logtrace(LogSlice,"# transform_skip_flag (context=%d)\n",context);
1527
1528 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
1529 &tctx->ctx_model[CONTEXT_MODEL_TRANSFORM_SKIP_FLAG+context]);
1530
1531 logtrace(LogSymbols,"$1 transform_skip_flag=%d\n",bit);
1532
1533 return bit;
1534 }
1535
1536
decode_sao_merge_flag(thread_context * tctx)1537 static int decode_sao_merge_flag(thread_context* tctx)
1538 {
1539 logtrace(LogSlice,"# sao_merge_left/up_flag\n");
1540 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
1541 &tctx->ctx_model[CONTEXT_MODEL_SAO_MERGE_FLAG]);
1542
1543 logtrace(LogSymbols,"$1 sao_merge_flag=%d\n",bit);
1544
1545 return bit;
1546 }
1547
1548
1549
decode_sao_type_idx(thread_context * tctx)1550 static int decode_sao_type_idx(thread_context* tctx)
1551 {
1552 logtrace(LogSlice,"# sao_type_idx_luma/chroma\n");
1553
1554 int bit0 = decode_CABAC_bit(&tctx->cabac_decoder,
1555 &tctx->ctx_model[CONTEXT_MODEL_SAO_TYPE_IDX]);
1556
1557 if (bit0==0) {
1558 logtrace(LogSymbols,"$1 sao_type_idx=%d\n",0);
1559 return 0;
1560 }
1561 else {
1562 int bit1 = decode_CABAC_bypass(&tctx->cabac_decoder);
1563 if (bit1==0) {
1564 logtrace(LogSymbols,"$1 sao_type_idx=%d\n",1);
1565 return 1;
1566 }
1567 else {
1568 logtrace(LogSymbols,"$1 sao_type_idx=%d\n",2);
1569 return 2;
1570 }
1571 }
1572 }
1573
1574
decode_sao_offset_abs(thread_context * tctx,int bitDepth)1575 static int decode_sao_offset_abs(thread_context* tctx, int bitDepth)
1576 {
1577 logtrace(LogSlice,"# sao_offset_abs\n");
1578 int cMax = (1<<(libde265_min(bitDepth,10)-5))-1;
1579 int value = decode_CABAC_TU_bypass(&tctx->cabac_decoder, cMax);
1580 logtrace(LogSymbols,"$1 sao_offset_abs=%d\n",value);
1581 return value;
1582 }
1583
1584
decode_sao_class(thread_context * tctx)1585 static int decode_sao_class(thread_context* tctx)
1586 {
1587 logtrace(LogSlice,"# sao_class\n");
1588 int value = decode_CABAC_FL_bypass(&tctx->cabac_decoder, 2);
1589 logtrace(LogSymbols,"$1 sao_class=%d\n",value);
1590 return value;
1591 }
1592
1593
decode_sao_offset_sign(thread_context * tctx)1594 static int decode_sao_offset_sign(thread_context* tctx)
1595 {
1596 logtrace(LogSlice,"# sao_offset_sign\n");
1597 int value = decode_CABAC_bypass(&tctx->cabac_decoder);
1598 logtrace(LogSymbols,"$1 sao_offset_sign=%d\n",value);
1599 return value;
1600 }
1601
1602
decode_sao_band_position(thread_context * tctx)1603 static int decode_sao_band_position(thread_context* tctx)
1604 {
1605 logtrace(LogSlice,"# sao_band_position\n");
1606 int value = decode_CABAC_FL_bypass(&tctx->cabac_decoder,5);
1607 logtrace(LogSymbols,"$1 sao_band_position=%d\n",value);
1608 return value;
1609 }
1610
1611
decode_transquant_bypass_flag(thread_context * tctx)1612 static int decode_transquant_bypass_flag(thread_context* tctx)
1613 {
1614 logtrace(LogSlice,"# cu_transquant_bypass_enable_flag\n");
1615 int value = decode_CABAC_bit(&tctx->cabac_decoder,
1616 &tctx->ctx_model[CONTEXT_MODEL_CU_TRANSQUANT_BYPASS_FLAG]);
1617 logtrace(LogSymbols,"$1 transquant_bypass_flag=%d\n",value);
1618 return value;
1619 }
1620
1621
1622 #include <sys/types.h>
1623 #include <signal.h>
1624
decode_split_cu_flag(thread_context * tctx,int x0,int y0,int ctDepth)1625 static int decode_split_cu_flag(thread_context* tctx,
1626 int x0, int y0, int ctDepth)
1627 {
1628 // check if neighbors are available
1629
1630 int availableL = check_CTB_available(tctx->img, x0,y0, x0-1,y0);
1631 int availableA = check_CTB_available(tctx->img, x0,y0, x0,y0-1);
1632
1633 int condL = 0;
1634 int condA = 0;
1635
1636 if (availableL && tctx->img->get_ctDepth(x0-1,y0) > ctDepth) condL=1;
1637 if (availableA && tctx->img->get_ctDepth(x0,y0-1) > ctDepth) condA=1;
1638
1639 int contextOffset = condL + condA;
1640 int context = contextOffset;
1641
1642 // decode bit
1643
1644 logtrace(LogSlice,"# split_cu_flag context=%d R=%x\n", context, tctx->cabac_decoder.range);
1645
1646 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_SPLIT_CU_FLAG + context]);
1647
1648 logtrace(LogSlice,"> split_cu_flag R=%x, ctx=%d, bit=%d\n", tctx->cabac_decoder.range,context,bit);
1649
1650 logtrace(LogSymbols,"$1 split_cu_flag=%d\n",bit);
1651
1652 return bit;
1653 }
1654
1655
decode_cu_skip_flag(thread_context * tctx,int x0,int y0,int ctDepth)1656 static int decode_cu_skip_flag(thread_context* tctx,
1657 int x0, int y0, int ctDepth)
1658 {
1659 decoder_context* ctx = tctx->decctx;
1660
1661 // check if neighbors are available
1662
1663 int availableL = check_CTB_available(tctx->img, x0,y0, x0-1,y0);
1664 int availableA = check_CTB_available(tctx->img, x0,y0, x0,y0-1);
1665
1666 int condL = 0;
1667 int condA = 0;
1668
1669 if (availableL && tctx->img->get_cu_skip_flag(x0-1,y0)) condL=1;
1670 if (availableA && tctx->img->get_cu_skip_flag(x0,y0-1)) condA=1;
1671
1672 int contextOffset = condL + condA;
1673 int context = contextOffset;
1674
1675 // decode bit
1676
1677 logtrace(LogSlice,"# cu_skip_flag context=%d R=%x\n", context, tctx->cabac_decoder.range);
1678
1679 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_CU_SKIP_FLAG + context]);
1680
1681 logtrace(LogSlice,"> cu_skip_flag R=%x, ctx=%d, bit=%d\n", tctx->cabac_decoder.range,context,bit);
1682
1683 logtrace(LogSymbols,"$1 cu_skip_flag=%d\n",bit);
1684
1685 return bit;
1686 }
1687
1688
decode_part_mode(thread_context * tctx,enum PredMode pred_mode,int cLog2CbSize)1689 static enum PartMode decode_part_mode(thread_context* tctx,
1690 enum PredMode pred_mode, int cLog2CbSize)
1691 {
1692 de265_image* img = tctx->img;
1693
1694 if (pred_mode == MODE_INTRA) {
1695 logtrace(LogSlice,"# part_mode (INTRA)\n");
1696
1697 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PART_MODE]);
1698
1699 logtrace(LogSlice,"> %s\n",bit ? "2Nx2N" : "NxN");
1700
1701 logtrace(LogSymbols,"$1 part_mode=%d\n",bit ? PART_2Nx2N : PART_NxN);
1702
1703 return bit ? PART_2Nx2N : PART_NxN;
1704 }
1705 else {
1706 const seq_parameter_set& sps = img->get_sps();
1707
1708 int bit0 = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PART_MODE+0]);
1709 if (bit0) { logtrace(LogSymbols,"$1 part_mode=%d\n",PART_2Nx2N); return PART_2Nx2N; }
1710
1711 // CHECK_ME: I optimize code and fix bug here, need more VERIFY!
1712 int bit1 = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PART_MODE+1]);
1713 if (cLog2CbSize > sps.Log2MinCbSizeY) {
1714 if (!sps.amp_enabled_flag) {
1715 logtrace(LogSymbols,"$1 part_mode=%d\n",bit1 ? PART_2NxN : PART_Nx2N);
1716 return bit1 ? PART_2NxN : PART_Nx2N;
1717 }
1718 else {
1719 int bit3 = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PART_MODE+3]);
1720 if (bit3) {
1721 logtrace(LogSymbols,"$1 part_mode=%d\n",bit1 ? PART_2NxN : PART_Nx2N);
1722 return bit1 ? PART_2NxN : PART_Nx2N;
1723 }
1724
1725 int bit4 = decode_CABAC_bypass(&tctx->cabac_decoder);
1726 if ( bit1 && bit4) {
1727 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_2NxnD);
1728 return PART_2NxnD;
1729 }
1730 if ( bit1 && !bit4) {
1731 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_2NxnU);
1732 return PART_2NxnU;
1733 }
1734 if (!bit1 && !bit4) {
1735 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_nLx2N);
1736 return PART_nLx2N;
1737 }
1738 if (!bit1 && bit4) {
1739 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_nRx2N);
1740 return PART_nRx2N;
1741 }
1742 }
1743 }
1744 else {
1745 // TODO, we could save one if here when first decoding the next bin and then
1746 // checkcLog2CbSize==3 when it is '0'
1747
1748 if (bit1) {
1749 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_2NxN);
1750 return PART_2NxN;
1751 }
1752
1753 if (cLog2CbSize==3) {
1754 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_Nx2N);
1755 return PART_Nx2N;
1756 }
1757 else {
1758 int bit2 = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PART_MODE+2]);
1759 logtrace(LogSymbols,"$1 part_mode=%d\n",PART_NxN-bit2);
1760 return (enum PartMode)((int)PART_NxN - bit2)/*bit2 ? PART_Nx2N : PART_NxN*/;
1761 }
1762 }
1763 }
1764
1765 assert(false); // should never be reached
1766 return PART_2Nx2N;
1767 }
1768
1769
decode_prev_intra_luma_pred_flag(thread_context * tctx)1770 static inline int decode_prev_intra_luma_pred_flag(thread_context* tctx)
1771 {
1772 logtrace(LogSlice,"# prev_intra_luma_pred_flag\n");
1773 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_PREV_INTRA_LUMA_PRED_FLAG]);
1774 logtrace(LogSymbols,"$1 prev_intra_luma_pred_flag=%d\n",bit);
1775 return bit;
1776 }
1777
1778
decode_mpm_idx(thread_context * tctx)1779 static inline int decode_mpm_idx(thread_context* tctx)
1780 {
1781 logtrace(LogSlice,"# mpm_idx (TU:2)\n");
1782 int mpm = decode_CABAC_TU_bypass(&tctx->cabac_decoder, 2);
1783 logtrace(LogSlice,"> mpm_idx = %d\n",mpm);
1784 logtrace(LogSymbols,"$1 mpm_idx=%d\n",mpm);
1785 return mpm;
1786 }
1787
1788
decode_rem_intra_luma_pred_mode(thread_context * tctx)1789 static inline int decode_rem_intra_luma_pred_mode(thread_context* tctx)
1790 {
1791 logtrace(LogSlice,"# rem_intra_luma_pred_mode (5 bits)\n");
1792 int value = decode_CABAC_FL_bypass(&tctx->cabac_decoder, 5);
1793 logtrace(LogSymbols,"$1 rem_intra_luma_pred_mode=%d\n",value);
1794 return value;
1795 }
1796
1797
decode_intra_chroma_pred_mode(thread_context * tctx)1798 static int decode_intra_chroma_pred_mode(thread_context* tctx)
1799 {
1800 logtrace(LogSlice,"# intra_chroma_pred_mode\n");
1801
1802 int prefix = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_INTRA_CHROMA_PRED_MODE]);
1803
1804 int mode;
1805 if (prefix==0) {
1806 mode=4;
1807 }
1808 else {
1809 mode = decode_CABAC_FL_bypass(&tctx->cabac_decoder, 2);
1810 }
1811
1812 logtrace(LogSlice,"> intra_chroma_pred_mode = %d\n",mode);
1813 logtrace(LogSymbols,"$1 intra_chroma_pred_mode=%d\n",mode);
1814
1815 return mode;
1816 }
1817
1818
decode_split_transform_flag(thread_context * tctx,int log2TrafoSize)1819 static int decode_split_transform_flag(thread_context* tctx,
1820 int log2TrafoSize)
1821 {
1822 logtrace(LogSlice,"# split_transform_flag (log2TrafoSize=%d)\n",log2TrafoSize);
1823
1824 int context = 5-log2TrafoSize;
1825 assert(context >= 0 && context <= 2);
1826
1827 logtrace(LogSlice,"# context: %d\n",context);
1828
1829 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_SPLIT_TRANSFORM_FLAG + context]);
1830 logtrace(LogSymbols,"$1 split_transform_flag=%d\n",bit);
1831 return bit;
1832 }
1833
1834
decode_cbf_chroma(thread_context * tctx,int trafoDepth)1835 static int decode_cbf_chroma(thread_context* tctx,
1836 int trafoDepth)
1837 {
1838 logtrace(LogSlice,"# cbf_chroma\n");
1839
1840 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_CBF_CHROMA + trafoDepth]);
1841
1842 logtrace(LogSymbols,"$1 cbf_chroma=%d\n",bit);
1843 return bit;
1844 }
1845
1846
decode_cbf_luma(thread_context * tctx,int trafoDepth)1847 static int decode_cbf_luma(thread_context* tctx,
1848 int trafoDepth)
1849 {
1850 logtrace(LogSlice,"# cbf_luma\n");
1851
1852 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &tctx->ctx_model[CONTEXT_MODEL_CBF_LUMA + (trafoDepth==0)]);
1853
1854 logtrace(LogSlice,"> cbf_luma = %d\n",bit);
1855
1856 logtrace(LogSymbols,"$1 cbf_luma=%d\n",bit);
1857 return bit;
1858 }
1859
1860
decode_coded_sub_block_flag(thread_context * tctx,int cIdx,uint8_t coded_sub_block_neighbors)1861 static inline int decode_coded_sub_block_flag(thread_context* tctx,
1862 int cIdx,
1863 uint8_t coded_sub_block_neighbors)
1864 {
1865 logtrace(LogSlice,"# coded_sub_block_flag\n");
1866
1867 // tricky computation of csbfCtx
1868 int csbfCtx = ((coded_sub_block_neighbors & 1) | // right neighbor set or
1869 (coded_sub_block_neighbors >> 1)); // bottom neighbor set -> csbfCtx=1
1870
1871 int ctxIdxInc = csbfCtx;
1872 if (cIdx!=0) {
1873 ctxIdxInc += 2;
1874 }
1875
1876 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
1877 &tctx->ctx_model[CONTEXT_MODEL_CODED_SUB_BLOCK_FLAG + ctxIdxInc]);
1878
1879 logtrace(LogSymbols,"$1 coded_sub_block_flag=%d\n",bit);
1880 return bit;
1881 }
1882
1883
decode_cu_qp_delta_abs(thread_context * tctx)1884 static int decode_cu_qp_delta_abs(thread_context* tctx)
1885 {
1886 logtrace(LogSlice,"# cu_qp_delta_abs\n");
1887
1888 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
1889 &tctx->ctx_model[CONTEXT_MODEL_CU_QP_DELTA_ABS + 0]);
1890 if (bit==0) {
1891 logtrace(LogSymbols,"$1 cu_qp_delta_abs=%d\n",0);
1892 return 0;
1893 }
1894
1895 int prefix=1;
1896 for (int i=0;i<4;i++) {
1897 bit = decode_CABAC_bit(&tctx->cabac_decoder,
1898 &tctx->ctx_model[CONTEXT_MODEL_CU_QP_DELTA_ABS + 1]);
1899 if (bit==0) { break; }
1900 else { prefix++; }
1901 }
1902
1903 if (prefix==5) {
1904 int value = decode_CABAC_EGk_bypass(&tctx->cabac_decoder, 0);
1905 logtrace(LogSymbols,"$1 cu_qp_delta_abs=%d\n",value+5);
1906 return value + 5;
1907 }
1908 else {
1909 logtrace(LogSymbols,"$1 cu_qp_delta_abs=%d\n",prefix);
1910 return prefix;
1911 }
1912 }
1913
1914
decode_last_significant_coeff_prefix(thread_context * tctx,int log2TrafoSize,int cIdx,context_model * model)1915 static int decode_last_significant_coeff_prefix(thread_context* tctx,
1916 int log2TrafoSize,
1917 int cIdx,
1918 context_model* model)
1919 {
1920 logtrace(LogSlice,"# last_significant_coeff_prefix log2TrafoSize:%d cIdx:%d\n",log2TrafoSize,cIdx);
1921
1922 int cMax = (log2TrafoSize<<1)-1;
1923
1924 int ctxOffset, ctxShift;
1925 if (cIdx==0) {
1926 ctxOffset = 3*(log2TrafoSize-2) + ((log2TrafoSize-1)>>2);
1927 ctxShift = (log2TrafoSize+1)>>2;
1928 }
1929 else {
1930 ctxOffset = 15;
1931 ctxShift = log2TrafoSize-2;
1932 }
1933
1934 int binIdx;
1935 int value = cMax;
1936 for (binIdx=0;binIdx<cMax;binIdx++)
1937 {
1938 int ctxIdxInc = (binIdx >> ctxShift);
1939
1940 logtrace(LogSlice,"context: %d+%d\n",ctxOffset,ctxIdxInc);
1941
1942 int bit = decode_CABAC_bit(&tctx->cabac_decoder, &model[ctxOffset + ctxIdxInc]);
1943 if (bit==0) {
1944 value=binIdx;
1945 break;
1946 }
1947 }
1948
1949 logtrace(LogSlice,"> last_significant_coeff_prefix: %d\n", value);
1950
1951 return value;
1952 }
1953
1954
1955 static const uint8_t ctxIdxMap[16] = {
1956 0,1,4,5,
1957 2,3,4,5,
1958 6,6,8,8,
1959 7,7,8,99
1960 };
1961
1962 uint8_t* ctxIdxLookup[4 /* 4-log2-32 */][2 /* !!cIdx */][2 /* !!scanIdx */][4 /* prevCsbf */];
1963
alloc_and_init_significant_coeff_ctxIdx_lookupTable()1964 bool alloc_and_init_significant_coeff_ctxIdx_lookupTable()
1965 {
1966 int tableSize = 4*4*(2) + 8*8*(2*2*4) + 16*16*(2*4) + 32*32*(2*4);
1967
1968 uint8_t* p = (uint8_t*)malloc(tableSize);
1969 if (p==NULL) {
1970 return false;
1971 }
1972
1973 memset(p,0xFF,tableSize); // just for debugging
1974
1975
1976 // --- Set pointers to memory areas. Note that some parameters share the same memory. ---
1977
1978 // 4x4
1979
1980 for (int cIdx=0;cIdx<2;cIdx++) {
1981 for (int scanIdx=0;scanIdx<2;scanIdx++)
1982 for (int prevCsbf=0;prevCsbf<4;prevCsbf++)
1983 ctxIdxLookup[0][cIdx][scanIdx][prevCsbf] = p;
1984
1985 p += 4*4;
1986 }
1987
1988 // 8x8
1989
1990 for (int cIdx=0;cIdx<2;cIdx++)
1991 for (int scanIdx=0;scanIdx<2;scanIdx++)
1992 for (int prevCsbf=0;prevCsbf<4;prevCsbf++) {
1993 ctxIdxLookup[1][cIdx][scanIdx][prevCsbf] = p;
1994 p += 8*8;
1995 }
1996
1997 // 16x16
1998
1999 for (int cIdx=0;cIdx<2;cIdx++)
2000 for (int prevCsbf=0;prevCsbf<4;prevCsbf++) {
2001 for (int scanIdx=0;scanIdx<2;scanIdx++) {
2002 ctxIdxLookup[2][cIdx][scanIdx][prevCsbf] = p;
2003 }
2004
2005 p += 16*16;
2006 }
2007
2008 // 32x32
2009
2010 for (int cIdx=0;cIdx<2;cIdx++)
2011 for (int prevCsbf=0;prevCsbf<4;prevCsbf++) {
2012 for (int scanIdx=0;scanIdx<2;scanIdx++) {
2013 ctxIdxLookup[3][cIdx][scanIdx][prevCsbf] = p;
2014 }
2015
2016 p += 32*32;
2017 }
2018
2019
2020 // --- precompute ctxIdx tables ---
2021
2022 for (int log2w=2; log2w<=5 ; log2w++)
2023 for (int cIdx=0;cIdx<2;cIdx++)
2024 for (int scanIdx=0;scanIdx<2;scanIdx++)
2025 for (int prevCsbf=0;prevCsbf<4;prevCsbf++)
2026 {
2027 for (int yC=0;yC<(1<<log2w);yC++)
2028 for (int xC=0;xC<(1<<log2w);xC++)
2029 {
2030 int w = 1<<log2w;
2031 int sbWidth = w>>2;
2032
2033 int sigCtx;
2034
2035 // if log2TrafoSize==2
2036 if (sbWidth==1) {
2037 sigCtx = ctxIdxMap[(yC<<2) + xC];
2038 }
2039 else if (xC+yC==0) {
2040 sigCtx = 0;
2041 }
2042 else {
2043 int xS = xC>>2;
2044 int yS = yC>>2;
2045 /*
2046 int prevCsbf = 0;
2047
2048 if (xS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+1 +yS*sbWidth]; }
2049 if (yS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+(1+yS)*sbWidth]<<1; }
2050 */
2051 int xP = xC & 3;
2052 int yP = yC & 3;
2053
2054 //logtrace(LogSlice,"posInSubset: %d,%d\n",xP,yP);
2055 //logtrace(LogSlice,"prevCsbf: %d\n",prevCsbf);
2056
2057 switch (prevCsbf) {
2058 case 0:
2059 sigCtx = (xP+yP>=3) ? 0 : (xP+yP>0) ? 1 : 2;
2060 break;
2061 case 1:
2062 sigCtx = (yP==0) ? 2 : (yP==1) ? 1 : 0;
2063 break;
2064 case 2:
2065 sigCtx = (xP==0) ? 2 : (xP==1) ? 1 : 0;
2066 break;
2067 default:
2068 sigCtx = 2;
2069 break;
2070 }
2071
2072 //logtrace(LogSlice,"a) sigCtx=%d\n",sigCtx);
2073
2074 if (cIdx==0) {
2075 if (xS+yS > 0) sigCtx+=3;
2076
2077 //logtrace(LogSlice,"b) sigCtx=%d\n",sigCtx);
2078
2079 // if log2TrafoSize==3
2080 if (sbWidth==2) { // 8x8 block
2081 sigCtx += (scanIdx==0) ? 9 : 15;
2082 } else {
2083 sigCtx += 21;
2084 }
2085
2086 //logtrace(LogSlice,"c) sigCtx=%d\n",sigCtx);
2087 }
2088 else {
2089 // if log2TrafoSize==3
2090 if (sbWidth==2) { // 8x8 block
2091 sigCtx+=9;
2092 }
2093 else {
2094 sigCtx+=12;
2095 }
2096 }
2097
2098 }
2099
2100 int ctxIdxInc;
2101 if (cIdx==0) { ctxIdxInc=sigCtx; }
2102 else { ctxIdxInc=27+sigCtx; }
2103
2104 if (ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][xC+(yC<<log2w)] != 0xFF) {
2105 assert(ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][xC+(yC<<log2w)] == ctxIdxInc);
2106 }
2107
2108 ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][xC+(yC<<log2w)] = ctxIdxInc;
2109
2110 //NOTE: when using this option, we have to include all three scanIdx in the table
2111 //ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][s] = ctxIdxInc;
2112 }
2113 }
2114
2115 return true;
2116 }
2117
2118
alloc_and_init_significant_coeff_ctxIdx_lookupTable_OLD()2119 bool alloc_and_init_significant_coeff_ctxIdx_lookupTable_OLD()
2120 {
2121 int tableSize = 2*2*4*(4*4 + 8*8 + 16*16 + 32*32);
2122 uint8_t* p = (uint8_t*)malloc(tableSize);
2123 if (p==NULL) {
2124 return false;
2125 }
2126
2127 for (int log2w=2; log2w<=5 ; log2w++)
2128 for (int cIdx=0;cIdx<2;cIdx++)
2129 for (int scanIdx=0;scanIdx<2;scanIdx++)
2130 for (int prevCsbf=0;prevCsbf<4;prevCsbf++)
2131 {
2132 // assign pointer into reserved memory area
2133
2134 ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf] = p;
2135 p += (1<<log2w)*(1<<log2w);
2136
2137 const position* ScanOrderSub = get_scan_order(log2w-2, scanIdx);
2138 const position* ScanOrderPos = get_scan_order(2, scanIdx);
2139
2140 //for (int yC=0;yC<(1<<log2w);yC++)
2141 // for (int xC=0;xC<(1<<log2w);xC++)
2142 for (int s=0;s<(1<<log2w)*(1<<log2w);s++)
2143 {
2144 position S = ScanOrderSub[s>>4];
2145 int x0 = S.x<<2;
2146 int y0 = S.y<<2;
2147
2148 int subX = ScanOrderPos[s & 0xF].x;
2149 int subY = ScanOrderPos[s & 0xF].y;
2150 int xC = x0 + subX;
2151 int yC = y0 + subY;
2152
2153
2154 int w = 1<<log2w;
2155 int sbWidth = w>>2;
2156
2157 int sigCtx;
2158
2159 // if log2TrafoSize==2
2160 if (sbWidth==1) {
2161 sigCtx = ctxIdxMap[(yC<<2) + xC];
2162 }
2163 else if (xC+yC==0) {
2164 sigCtx = 0;
2165 }
2166 else {
2167 int xS = xC>>2;
2168 int yS = yC>>2;
2169 /*
2170 int prevCsbf = 0;
2171
2172 if (xS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+1 +yS*sbWidth]; }
2173 if (yS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+(1+yS)*sbWidth]<<1; }
2174 */
2175 int xP = xC & 3;
2176 int yP = yC & 3;
2177
2178 logtrace(LogSlice,"posInSubset: %d,%d\n",xP,yP);
2179 logtrace(LogSlice,"prevCsbf: %d\n",prevCsbf);
2180
2181 //printf("%d | %d %d\n",prevCsbf,xP,yP);
2182
2183 switch (prevCsbf) {
2184 case 0:
2185 //sigCtx = (xP+yP==0) ? 2 : (xP+yP<3) ? 1 : 0;
2186 sigCtx = (xP+yP>=3) ? 0 : (xP+yP>0) ? 1 : 2;
2187 break;
2188 case 1:
2189 sigCtx = (yP==0) ? 2 : (yP==1) ? 1 : 0;
2190 break;
2191 case 2:
2192 sigCtx = (xP==0) ? 2 : (xP==1) ? 1 : 0;
2193 break;
2194 default:
2195 sigCtx = 2;
2196 break;
2197 }
2198
2199 logtrace(LogSlice,"a) sigCtx=%d\n",sigCtx);
2200
2201 if (cIdx==0) {
2202 if (xS+yS > 0) sigCtx+=3;
2203
2204 logtrace(LogSlice,"b) sigCtx=%d\n",sigCtx);
2205
2206 // if log2TrafoSize==3
2207 if (sbWidth==2) { // 8x8 block
2208 sigCtx += (scanIdx==0) ? 9 : 15;
2209 } else {
2210 sigCtx += 21;
2211 }
2212
2213 logtrace(LogSlice,"c) sigCtx=%d\n",sigCtx);
2214 }
2215 else {
2216 // if log2TrafoSize==3
2217 if (sbWidth==2) { // 8x8 block
2218 sigCtx+=9;
2219 }
2220 else {
2221 sigCtx+=12;
2222 }
2223 }
2224 }
2225
2226 int ctxIdxInc;
2227 if (cIdx==0) { ctxIdxInc=sigCtx; }
2228 else { ctxIdxInc=27+sigCtx; }
2229
2230
2231 ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][xC+(yC<<log2w)] = ctxIdxInc;
2232
2233 //NOTE: when using this option, we have to include all three scanIdx in the table
2234 //ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][s] = ctxIdxInc;
2235 }
2236 }
2237
2238 return true;
2239 }
2240
free_significant_coeff_ctxIdx_lookupTable()2241 void free_significant_coeff_ctxIdx_lookupTable()
2242 {
2243 free(ctxIdxLookup[0][0][0][0]);
2244 ctxIdxLookup[0][0][0][0]=NULL;
2245 }
2246
2247
2248
2249
2250 #if 0
2251 static int decode_significant_coeff_flag(thread_context* tctx,
2252 int xC,int yC,
2253 const uint8_t* coded_sub_block_flag,
2254 int sbWidth,
2255 int cIdx,
2256 int scanIdx)
2257 {
2258 logtrace(LogSlice,"# significant_coeff_flag (xC:%d yC:%d sbWidth:%d cIdx:%d scanIdx:%d)\n",
2259 xC,yC,sbWidth,cIdx,scanIdx);
2260
2261 int sigCtx;
2262
2263 // if log2TrafoSize==2
2264 if (sbWidth==1) {
2265 sigCtx = ctxIdxMap[(yC<<2) + xC];
2266 }
2267 else if (xC+yC==0) {
2268 sigCtx = 0;
2269 }
2270 else {
2271 int xS = xC>>2;
2272 int yS = yC>>2;
2273 int prevCsbf = 0;
2274 if (xS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+1 +yS*sbWidth]; }
2275 if (yS < sbWidth-1) { prevCsbf += coded_sub_block_flag[xS+(1+yS)*sbWidth]<<1; }
2276
2277 int xP = xC & 3;
2278 int yP = yC & 3;
2279
2280 logtrace(LogSlice,"posInSubset: %d,%d\n",xP,yP);
2281 logtrace(LogSlice,"prevCsbf: %d\n",prevCsbf);
2282
2283 //printf("%d | %d %d\n",prevCsbf,xP,yP);
2284
2285 switch (prevCsbf) {
2286 case 0:
2287 //sigCtx = (xP+yP==0) ? 2 : (xP+yP<3) ? 1 : 0;
2288 sigCtx = (xP+yP>=3) ? 0 : (xP+yP>0) ? 1 : 2;
2289 break;
2290 case 1:
2291 sigCtx = (yP==0) ? 2 : (yP==1) ? 1 : 0;
2292 break;
2293 case 2:
2294 sigCtx = (xP==0) ? 2 : (xP==1) ? 1 : 0;
2295 break;
2296 default:
2297 sigCtx = 2;
2298 break;
2299 }
2300
2301 logtrace(LogSlice,"a) sigCtx=%d\n",sigCtx);
2302
2303 if (cIdx==0) {
2304 if (xS+yS > 0) sigCtx+=3;
2305
2306 logtrace(LogSlice,"b) sigCtx=%d\n",sigCtx);
2307
2308 // if log2TrafoSize==3
2309 if (sbWidth==2) {
2310 sigCtx += (scanIdx==0) ? 9 : 15;
2311 } else {
2312 sigCtx += 21;
2313 }
2314
2315 logtrace(LogSlice,"c) sigCtx=%d\n",sigCtx);
2316 }
2317 else {
2318 // if log2TrafoSize==3
2319 if (sbWidth==2) {
2320 sigCtx+=9;
2321 }
2322 else {
2323 sigCtx+=12;
2324 }
2325 }
2326 }
2327
2328 int ctxIdxInc;
2329 if (cIdx==0) { ctxIdxInc=sigCtx; }
2330 else { ctxIdxInc=27+sigCtx; }
2331
2332 int context = tctx->shdr->initType*42 + ctxIdxInc;
2333 logtrace(LogSlice,"context: %d\n",context);
2334
2335 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2336 &tctx->ctx_model[CONTEXT_MODEL_SIGNIFICANT_COEFF_FLAG + context]);
2337 return bit;
2338 }
2339 #endif
2340
2341
2342
decode_significant_coeff_flag_lookup(thread_context * tctx,uint8_t ctxIdxInc)2343 static inline int decode_significant_coeff_flag_lookup(thread_context* tctx,
2344 uint8_t ctxIdxInc)
2345 {
2346 logtrace(LogSlice,"# significant_coeff_flag\n");
2347 logtrace(LogSlice,"context: %d\n",ctxIdxInc);
2348
2349 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2350 &tctx->ctx_model[CONTEXT_MODEL_SIGNIFICANT_COEFF_FLAG + ctxIdxInc]);
2351
2352 logtrace(LogSymbols,"$1 significant_coeff_flag=%d\n",bit);
2353
2354 return bit;
2355 }
2356
2357
2358
2359
2360
decode_coeff_abs_level_greater1(thread_context * tctx,int cIdx,int i,bool firstCoeffInSubblock,bool firstSubblock,int lastSubblock_greater1Ctx,int * lastInvocation_greater1Ctx,int * lastInvocation_coeff_abs_level_greater1_flag,int * lastInvocation_ctxSet,int c1)2361 static inline int decode_coeff_abs_level_greater1(thread_context* tctx,
2362 int cIdx, int i,
2363 bool firstCoeffInSubblock,
2364 bool firstSubblock,
2365 int lastSubblock_greater1Ctx,
2366 int* lastInvocation_greater1Ctx,
2367 int* lastInvocation_coeff_abs_level_greater1_flag,
2368 int* lastInvocation_ctxSet, int c1)
2369 {
2370 logtrace(LogSlice,"# coeff_abs_level_greater1\n");
2371
2372 logtrace(LogSlice," cIdx:%d i:%d firstCoeffInSB:%d firstSB:%d lastSB>1:%d last>1Ctx:%d lastLev>1:%d lastCtxSet:%d\n", cIdx,i,firstCoeffInSubblock,firstSubblock,lastSubblock_greater1Ctx,
2373 *lastInvocation_greater1Ctx,
2374 *lastInvocation_coeff_abs_level_greater1_flag,
2375 *lastInvocation_ctxSet);
2376
2377 int lastGreater1Ctx;
2378 int greater1Ctx;
2379 int ctxSet;
2380
2381 logtrace(LogSlice,"c1: %d\n",c1);
2382
2383 if (firstCoeffInSubblock) {
2384 // block with real DC -> ctx 0
2385 if (i==0 || cIdx>0) { ctxSet=0; }
2386 else { ctxSet=2; }
2387
2388 if (firstSubblock) { lastGreater1Ctx=1; }
2389 else { lastGreater1Ctx = lastSubblock_greater1Ctx; }
2390
2391 if (lastGreater1Ctx==0) { ctxSet++; }
2392
2393 logtrace(LogSlice,"ctxSet: %d\n",ctxSet);
2394
2395 greater1Ctx=1;
2396 }
2397 else { // !firstCoeffInSubblock
2398 ctxSet = *lastInvocation_ctxSet;
2399 logtrace(LogSlice,"ctxSet (old): %d\n",ctxSet);
2400
2401 greater1Ctx = *lastInvocation_greater1Ctx;
2402 if (greater1Ctx>0) {
2403 int lastGreater1Flag=*lastInvocation_coeff_abs_level_greater1_flag;
2404 if (lastGreater1Flag==1) greater1Ctx=0;
2405 else { /*if (greater1Ctx>0)*/ greater1Ctx++; }
2406 }
2407 }
2408
2409 ctxSet = c1; // use HM algo
2410
2411 int ctxIdxInc = (ctxSet*4) + (greater1Ctx>=3 ? 3 : greater1Ctx);
2412
2413 if (cIdx>0) { ctxIdxInc+=16; }
2414
2415 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2416 &tctx->ctx_model[CONTEXT_MODEL_COEFF_ABS_LEVEL_GREATER1_FLAG + ctxIdxInc]);
2417
2418 *lastInvocation_greater1Ctx = greater1Ctx;
2419 *lastInvocation_coeff_abs_level_greater1_flag = bit;
2420 *lastInvocation_ctxSet = ctxSet;
2421
2422 //logtrace(LogSymbols,"$1 coeff_abs_level_greater1=%d\n",bit);
2423
2424 return bit;
2425 }
2426
2427
decode_coeff_abs_level_greater2(thread_context * tctx,int cIdx,int ctxSet)2428 static int decode_coeff_abs_level_greater2(thread_context* tctx,
2429 int cIdx, // int i,int n,
2430 int ctxSet)
2431 {
2432 logtrace(LogSlice,"# coeff_abs_level_greater2\n");
2433
2434 int ctxIdxInc = ctxSet;
2435
2436 if (cIdx>0) ctxIdxInc+=4;
2437
2438 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2439 &tctx->ctx_model[CONTEXT_MODEL_COEFF_ABS_LEVEL_GREATER2_FLAG + ctxIdxInc]);
2440
2441 logtrace(LogSymbols,"$1 coeff_abs_level_greater2=%d\n",bit);
2442
2443 return bit;
2444 }
2445
2446
2447 #define MAX_PREFIX 64
2448
decode_coeff_abs_level_remaining(thread_context * tctx,int cRiceParam)2449 static int decode_coeff_abs_level_remaining(thread_context* tctx,
2450 int cRiceParam)
2451 {
2452 logtrace(LogSlice,"# decode_coeff_abs_level_remaining\n");
2453
2454 int prefix=-1;
2455 int codeword=0;
2456 do {
2457 prefix++;
2458 codeword = decode_CABAC_bypass(&tctx->cabac_decoder);
2459
2460 if (prefix>MAX_PREFIX) {
2461 return 0; // TODO: error
2462 }
2463 }
2464 while (codeword);
2465
2466 // prefix = nb. 1 bits
2467
2468 int value;
2469
2470 if (prefix <= 3) {
2471 // when code only TR part (level < TRMax)
2472
2473 codeword = decode_CABAC_FL_bypass(&tctx->cabac_decoder, cRiceParam);
2474 value = (prefix<<cRiceParam) + codeword;
2475 }
2476 else {
2477 // Suffix coded with EGk. Note that the unary part of EGk is already
2478 // included in the 'prefix' counter above.
2479
2480 codeword = decode_CABAC_FL_bypass(&tctx->cabac_decoder, prefix-3+cRiceParam);
2481 value = (((1<<(prefix-3))+3-1)<<cRiceParam)+codeword;
2482 }
2483
2484 logtrace(LogSymbols,"$1 coeff_abs_level_remaining=%d\n",value);
2485
2486 return value;
2487 }
2488
2489
decode_merge_flag(thread_context * tctx)2490 static int decode_merge_flag(thread_context* tctx)
2491 {
2492 logtrace(LogSlice,"# merge_flag\n");
2493
2494 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2495 &tctx->ctx_model[CONTEXT_MODEL_MERGE_FLAG]);
2496
2497 logtrace(LogSymbols,"$1 merge_flag=%d\n",bit);
2498
2499 return bit;
2500 }
2501
2502
decode_merge_idx(thread_context * tctx)2503 static int decode_merge_idx(thread_context* tctx)
2504 {
2505 logtrace(LogSlice,"# merge_idx\n");
2506
2507 if (tctx->shdr->MaxNumMergeCand <= 1) {
2508 logtrace(LogSymbols,"$1 merge_idx=%d\n",0);
2509 return 0;
2510 }
2511
2512 // TU coding, first bin is CABAC, remaining are bypass.
2513 // cMax = MaxNumMergeCand-1
2514
2515 int idx = decode_CABAC_bit(&tctx->cabac_decoder,
2516 &tctx->ctx_model[CONTEXT_MODEL_MERGE_IDX]);
2517
2518 if (idx==0) {
2519 // nothing
2520 }
2521 else {
2522 idx=1;
2523
2524 while (idx<tctx->shdr->MaxNumMergeCand-1) {
2525 if (decode_CABAC_bypass(&tctx->cabac_decoder)) {
2526 idx++;
2527 }
2528 else {
2529 break;
2530 }
2531 }
2532 }
2533
2534 logtrace(LogSlice,"> merge_idx = %d\n",idx);
2535 logtrace(LogSymbols,"$1 merge_idx=%d\n",idx);
2536
2537 return idx;
2538 }
2539
2540
decode_pred_mode_flag(thread_context * tctx)2541 static int decode_pred_mode_flag(thread_context* tctx)
2542 {
2543 logtrace(LogSlice,"# pred_mode_flag\n");
2544
2545 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2546 &tctx->ctx_model[CONTEXT_MODEL_PRED_MODE_FLAG]);
2547
2548 logtrace(LogSymbols,"$1 pred_mode=%d\n",bit);
2549 return bit;
2550 }
2551
decode_mvp_lx_flag(thread_context * tctx)2552 static int decode_mvp_lx_flag(thread_context* tctx)
2553 {
2554 logtrace(LogSlice,"# mvp_lx_flag\n");
2555
2556 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2557 &tctx->ctx_model[CONTEXT_MODEL_MVP_LX_FLAG]);
2558
2559 logtrace(LogSymbols,"$1 mvp_lx_flag=%d\n",bit);
2560 return bit;
2561 }
2562
decode_rqt_root_cbf(thread_context * tctx)2563 static int decode_rqt_root_cbf(thread_context* tctx)
2564 {
2565 logtrace(LogSlice,"# rqt_root_cbf\n");
2566
2567 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2568 &tctx->ctx_model[CONTEXT_MODEL_RQT_ROOT_CBF]);
2569
2570 logtrace(LogSymbols,"$1 rqt_root_cbf=%d\n",bit);
2571 return bit;
2572 }
2573
decode_ref_idx_lX(thread_context * tctx,int numRefIdxLXActive)2574 static int decode_ref_idx_lX(thread_context* tctx, int numRefIdxLXActive)
2575 {
2576 logtrace(LogSlice,"# ref_idx_lX\n");
2577
2578 int cMax = numRefIdxLXActive-1;
2579
2580 if (cMax==0) {
2581 logtrace(LogSlice,"> ref_idx = 0 (cMax==0)\n");
2582 return 0;
2583 } // do check for single reference frame here
2584
2585 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
2586 &tctx->ctx_model[CONTEXT_MODEL_REF_IDX_LX + 0]);
2587
2588 int idx=0;
2589
2590 while (bit) {
2591 idx++;
2592 if (idx==cMax) { break; }
2593
2594 if (idx==1) {
2595 bit = decode_CABAC_bit(&tctx->cabac_decoder,
2596 &tctx->ctx_model[CONTEXT_MODEL_REF_IDX_LX + 1]);
2597 }
2598 else {
2599 bit = decode_CABAC_bypass(&tctx->cabac_decoder);
2600 }
2601 }
2602
2603 logtrace(LogSlice,"> ref_idx = %d\n",idx);
2604
2605 logtrace(LogSymbols,"$1 ref_idx_lX=%d\n",idx);
2606 return idx;
2607 }
2608
2609
decode_inter_pred_idc(thread_context * tctx,int x0,int y0,int nPbW,int nPbH,int ctDepth)2610 static enum InterPredIdc decode_inter_pred_idc(thread_context* tctx,
2611 int x0, int y0,
2612 int nPbW, int nPbH,
2613 int ctDepth)
2614 {
2615 logtrace(LogSlice,"# inter_pred_idc\n");
2616
2617 int value;
2618
2619 context_model* model = &tctx->ctx_model[CONTEXT_MODEL_INTER_PRED_IDC];
2620
2621 if (nPbW+nPbH==12) {
2622 value = decode_CABAC_bit(&tctx->cabac_decoder,
2623 &model[4]);
2624 }
2625 else {
2626 int bit0 = decode_CABAC_bit(&tctx->cabac_decoder,
2627 &model[ctDepth]);
2628 if (bit0==0) {
2629 value = decode_CABAC_bit(&tctx->cabac_decoder,
2630 &model[4]);
2631 }
2632 else {
2633 value = 2;
2634 }
2635 }
2636
2637 logtrace(LogSlice,"> inter_pred_idc = %d (%s)\n",value,
2638 value==0 ? "L0" : (value==1 ? "L1" : "BI"));
2639
2640 logtrace(LogSymbols,"$1 decode_inter_pred_idx=%d\n",value+1);
2641
2642 return (enum InterPredIdc) (value+1);
2643 }
2644
2645
decode_explicit_rdpcm_flag(thread_context * tctx,int cIdx)2646 static int decode_explicit_rdpcm_flag(thread_context* tctx,int cIdx)
2647 {
2648 context_model* model = &tctx->ctx_model[CONTEXT_MODEL_RDPCM_FLAG];
2649 int value = decode_CABAC_bit(&tctx->cabac_decoder, &model[cIdx ? 1 : 0]);
2650 return value;
2651 }
2652
2653
decode_explicit_rdpcm_dir(thread_context * tctx,int cIdx)2654 static int decode_explicit_rdpcm_dir(thread_context* tctx,int cIdx)
2655 {
2656 context_model* model = &tctx->ctx_model[CONTEXT_MODEL_RDPCM_DIR];
2657 int value = decode_CABAC_bit(&tctx->cabac_decoder, &model[cIdx ? 1 : 0]);
2658 return value;
2659 }
2660
2661
2662
2663 /* Take CtbAddrInTS and compute
2664 -> CtbAddrInRS, CtbX, CtbY
2665 */
setCtbAddrFromTS(thread_context * tctx)2666 bool setCtbAddrFromTS(thread_context* tctx)
2667 {
2668 const seq_parameter_set& sps = tctx->img->get_sps();
2669
2670 if (tctx->CtbAddrInTS < sps.PicSizeInCtbsY) {
2671 tctx->CtbAddrInRS = tctx->img->get_pps().CtbAddrTStoRS[tctx->CtbAddrInTS];
2672
2673 tctx->CtbX = tctx->CtbAddrInRS % sps.PicWidthInCtbsY;
2674 tctx->CtbY = tctx->CtbAddrInRS / sps.PicWidthInCtbsY;
2675 return false;
2676 }
2677 else {
2678 tctx->CtbAddrInRS = sps.PicSizeInCtbsY;
2679
2680 tctx->CtbX = tctx->CtbAddrInRS % sps.PicWidthInCtbsY;
2681 tctx->CtbY = tctx->CtbAddrInRS / sps.PicWidthInCtbsY;
2682 return true;
2683 }
2684 }
2685
2686 // returns true when we reached the end of the image (ctbAddr==picSizeInCtbsY)
advanceCtbAddr(thread_context * tctx)2687 bool advanceCtbAddr(thread_context* tctx)
2688 {
2689 tctx->CtbAddrInTS++;
2690
2691 return setCtbAddrFromTS(tctx);
2692 }
2693
2694
read_sao(thread_context * tctx,int xCtb,int yCtb,int CtbAddrInSliceSeg)2695 void read_sao(thread_context* tctx, int xCtb,int yCtb,
2696 int CtbAddrInSliceSeg)
2697 {
2698 slice_segment_header* shdr = tctx->shdr;
2699 de265_image* img = tctx->img;
2700 const seq_parameter_set& sps = img->get_sps();
2701 const pic_parameter_set& pps = img->get_pps();
2702
2703 logtrace(LogSlice,"# read_sao(%d,%d)\n",xCtb,yCtb);
2704
2705 sao_info saoinfo;
2706 memset(&saoinfo,0,sizeof(sao_info));
2707 logtrace(LogSlice,"sizeof saoinfo: %d\n",sizeof(sao_info));
2708
2709
2710 char sao_merge_left_flag = 0;
2711 char sao_merge_up_flag = 0;
2712
2713 if (xCtb>0) {
2714 //char leftCtbInSliceSeg = (CtbAddrInSliceSeg>0);
2715 char leftCtbInSliceSeg = (tctx->CtbAddrInRS > shdr->SliceAddrRS);
2716 char leftCtbInTile = (pps.TileIdRS[xCtb + yCtb * sps.PicWidthInCtbsY] ==
2717 pps.TileIdRS[xCtb-1 + yCtb * sps.PicWidthInCtbsY]);
2718
2719 if (leftCtbInSliceSeg && leftCtbInTile) {
2720 sao_merge_left_flag = decode_sao_merge_flag(tctx);
2721 logtrace(LogSlice,"sao_merge_left_flag: %d\n",sao_merge_left_flag);
2722 }
2723 }
2724
2725 if (yCtb>0 && sao_merge_left_flag==0) {
2726 logtrace(LogSlice,"CtbAddrInRS:%d PicWidthInCtbsY:%d slice_segment_address:%d\n",
2727 tctx->CtbAddrInRS,
2728 sps.PicWidthInCtbsY,
2729 shdr->slice_segment_address);
2730 char upCtbInSliceSeg = (tctx->CtbAddrInRS - sps.PicWidthInCtbsY) >= shdr->SliceAddrRS;
2731 char upCtbInTile = (pps.TileIdRS[xCtb + yCtb * sps.PicWidthInCtbsY] ==
2732 pps.TileIdRS[xCtb + (yCtb-1) * sps.PicWidthInCtbsY]);
2733
2734 if (upCtbInSliceSeg && upCtbInTile) {
2735 sao_merge_up_flag = decode_sao_merge_flag(tctx);
2736 logtrace(LogSlice,"sao_merge_up_flag: %d\n",sao_merge_up_flag);
2737 }
2738 }
2739
2740 if (!sao_merge_up_flag && !sao_merge_left_flag) {
2741 int nChroma = 3;
2742 if (sps.ChromaArrayType == CHROMA_MONO) nChroma=1;
2743
2744 for (int cIdx=0; cIdx<nChroma; cIdx++) {
2745 if ((shdr->slice_sao_luma_flag && cIdx==0) ||
2746 (shdr->slice_sao_chroma_flag && cIdx>0)) {
2747
2748 uint8_t SaoTypeIdx = 0;
2749
2750 if (cIdx==0) {
2751 char sao_type_idx_luma = decode_sao_type_idx(tctx);
2752 logtrace(LogSlice,"sao_type_idx_luma: %d\n", sao_type_idx_luma);
2753 saoinfo.SaoTypeIdx = SaoTypeIdx = sao_type_idx_luma;
2754 }
2755 else if (cIdx==1) {
2756 char sao_type_idx_chroma = decode_sao_type_idx(tctx);
2757 logtrace(LogSlice,"sao_type_idx_chroma: %d\n", sao_type_idx_chroma);
2758 SaoTypeIdx = sao_type_idx_chroma;
2759 saoinfo.SaoTypeIdx |= SaoTypeIdx<<(2*1);
2760 saoinfo.SaoTypeIdx |= SaoTypeIdx<<(2*2); // set for both chroma components
2761 }
2762 else {
2763 // SaoTypeIdx = 0
2764
2765 SaoTypeIdx = (saoinfo.SaoTypeIdx >> (2*cIdx)) & 0x3;
2766 }
2767
2768 if (SaoTypeIdx != 0) {
2769 for (int i=0;i<4;i++) {
2770 saoinfo.saoOffsetVal[cIdx][i] = decode_sao_offset_abs(tctx, img->get_bit_depth(cIdx));
2771 logtrace(LogSlice,"saoOffsetVal[%d][%d] = %d\n",cIdx,i, saoinfo.saoOffsetVal[cIdx][i]);
2772 }
2773
2774 int sign[4];
2775 if (SaoTypeIdx==1) {
2776 for (int i=0;i<4;i++) {
2777 if (saoinfo.saoOffsetVal[cIdx][i] != 0) {
2778 sign[i] = decode_sao_offset_sign(tctx) ? -1 : 1;
2779 }
2780 else {
2781 sign[i] = 0; // not really required, but compiler warns about uninitialized values
2782 }
2783 }
2784
2785 saoinfo.sao_band_position[cIdx] = decode_sao_band_position(tctx);
2786 }
2787 else {
2788 uint8_t SaoEoClass = 0;
2789
2790 sign[0] = sign[1] = 1;
2791 sign[2] = sign[3] = -1;
2792
2793 if (cIdx==0) {
2794 saoinfo.SaoEoClass = SaoEoClass = decode_sao_class(tctx);
2795 }
2796 else if (cIdx==1) {
2797 SaoEoClass = decode_sao_class(tctx);
2798 saoinfo.SaoEoClass |= SaoEoClass << (2*1);
2799 saoinfo.SaoEoClass |= SaoEoClass << (2*2);
2800 }
2801
2802 logtrace(LogSlice,"SaoEoClass[%d] = %d\n",cIdx,SaoEoClass);
2803 }
2804
2805 int log2OffsetScale;
2806
2807 if (cIdx==0) {
2808 log2OffsetScale = pps.range_extension.log2_sao_offset_scale_luma;
2809 }
2810 else {
2811 log2OffsetScale = pps.range_extension.log2_sao_offset_scale_chroma;
2812 }
2813
2814 for (int i=0;i<4;i++) {
2815 saoinfo.saoOffsetVal[cIdx][i] = sign[i]*(saoinfo.saoOffsetVal[cIdx][i] << log2OffsetScale);
2816 }
2817 }
2818 }
2819 }
2820
2821 img->set_sao_info(xCtb,yCtb, &saoinfo);
2822 }
2823
2824
2825 if (sao_merge_left_flag) {
2826 img->set_sao_info(xCtb,yCtb, img->get_sao_info(xCtb-1,yCtb));
2827 }
2828
2829 if (sao_merge_up_flag) {
2830 img->set_sao_info(xCtb,yCtb, img->get_sao_info(xCtb,yCtb-1));
2831 }
2832 }
2833
2834
read_coding_tree_unit(thread_context * tctx)2835 void read_coding_tree_unit(thread_context* tctx)
2836 {
2837 slice_segment_header* shdr = tctx->shdr;
2838 de265_image* img = tctx->img;
2839 const seq_parameter_set& sps = img->get_sps();
2840
2841 int xCtb = (tctx->CtbAddrInRS % sps.PicWidthInCtbsY);
2842 int yCtb = (tctx->CtbAddrInRS / sps.PicWidthInCtbsY);
2843 int xCtbPixels = xCtb << sps.Log2CtbSizeY;
2844 int yCtbPixels = yCtb << sps.Log2CtbSizeY;
2845
2846 logtrace(LogSlice,"----- decode CTB %d;%d (%d;%d) POC=%d, SliceAddrRS=%d\n",
2847 xCtbPixels,yCtbPixels, xCtb,yCtb,
2848 tctx->img->PicOrderCntVal, tctx->shdr->SliceAddrRS);
2849
2850 img->set_SliceAddrRS(xCtb, yCtb, tctx->shdr->SliceAddrRS);
2851
2852 img->set_SliceHeaderIndex(xCtbPixels,yCtbPixels, shdr->slice_index);
2853
2854 int CtbAddrInSliceSeg = tctx->CtbAddrInRS - shdr->slice_segment_address;
2855
2856 if (shdr->slice_sao_luma_flag || shdr->slice_sao_chroma_flag)
2857 {
2858 read_sao(tctx, xCtb,yCtb, CtbAddrInSliceSeg);
2859 }
2860
2861 read_coding_quadtree(tctx, xCtbPixels, yCtbPixels, sps.Log2CtbSizeY, 0);
2862 }
2863
2864
luma_pos_to_ctbAddrRS(const seq_parameter_set * sps,int x,int y)2865 LIBDE265_INLINE static int luma_pos_to_ctbAddrRS(const seq_parameter_set* sps, int x,int y)
2866 {
2867 int ctbX = x >> sps->Log2CtbSizeY;
2868 int ctbY = y >> sps->Log2CtbSizeY;
2869
2870 return ctbY * sps->PicWidthInCtbsY + ctbX;
2871 }
2872
2873
check_CTB_available(const de265_image * img,int xC,int yC,int xN,int yN)2874 int check_CTB_available(const de265_image* img,
2875 int xC,int yC, int xN,int yN)
2876 {
2877 // check whether neighbor is outside of frame
2878
2879 if (xN < 0 || yN < 0) { return 0; }
2880 if (xN >= img->get_sps().pic_width_in_luma_samples) { return 0; }
2881 if (yN >= img->get_sps().pic_height_in_luma_samples) { return 0; }
2882
2883
2884 int current_ctbAddrRS = luma_pos_to_ctbAddrRS(&img->get_sps(), xC,yC);
2885 int neighbor_ctbAddrRS = luma_pos_to_ctbAddrRS(&img->get_sps(), xN,yN);
2886
2887 // TODO: check if this is correct (6.4.1)
2888
2889 if (img->get_SliceAddrRS_atCtbRS(current_ctbAddrRS) !=
2890 img->get_SliceAddrRS_atCtbRS(neighbor_ctbAddrRS)) {
2891 return 0;
2892 }
2893
2894 // check if both CTBs are in the same tile.
2895
2896 if (img->get_pps().TileIdRS[current_ctbAddrRS] !=
2897 img->get_pps().TileIdRS[neighbor_ctbAddrRS]) {
2898 return 0;
2899 }
2900
2901 return 1;
2902 }
2903
2904
residual_coding(thread_context * tctx,int x0,int y0,int log2TrafoSize,int cIdx)2905 int residual_coding(thread_context* tctx,
2906 int x0, int y0, // position of TU in frame
2907 int log2TrafoSize,
2908 int cIdx)
2909 {
2910 logtrace(LogSlice,"- residual_coding x0:%d y0:%d log2TrafoSize:%d cIdx:%d\n",x0,y0,log2TrafoSize,cIdx);
2911
2912 //slice_segment_header* shdr = tctx->shdr;
2913
2914 de265_image* img = tctx->img;
2915 const seq_parameter_set& sps = img->get_sps();
2916 const pic_parameter_set& pps = img->get_pps();
2917
2918 enum PredMode PredMode = img->get_pred_mode(x0,y0);
2919
2920 if (cIdx==0) {
2921 img->set_nonzero_coefficient(x0,y0,log2TrafoSize);
2922 }
2923
2924
2925 if (pps.transform_skip_enabled_flag &&
2926 !tctx->cu_transquant_bypass_flag &&
2927 (log2TrafoSize <= pps.Log2MaxTransformSkipSize))
2928 {
2929 tctx->transform_skip_flag[cIdx] = decode_transform_skip_flag(tctx,cIdx);
2930 }
2931 else
2932 {
2933 tctx->transform_skip_flag[cIdx] = 0;
2934 }
2935
2936
2937 tctx->explicit_rdpcm_flag = false;
2938
2939 if (PredMode == MODE_INTER && sps.range_extension.explicit_rdpcm_enabled_flag &&
2940 ( tctx->transform_skip_flag[cIdx] || tctx->cu_transquant_bypass_flag))
2941 {
2942 tctx->explicit_rdpcm_flag = decode_explicit_rdpcm_flag(tctx,cIdx);
2943 if (tctx->explicit_rdpcm_flag) {
2944 tctx->explicit_rdpcm_dir = decode_explicit_rdpcm_dir(tctx,cIdx);
2945 }
2946
2947 //printf("EXPLICIT RDPCM %d;%d\n",x0,y0);
2948 }
2949 else
2950 {
2951 tctx->explicit_rdpcm_flag = false;
2952 }
2953
2954
2955
2956 // sbType for persistent_rice_adaptation_enabled_flag
2957
2958 int sbType = (cIdx==0) ? 2 : 0;
2959 if (tctx->transform_skip_flag[cIdx] || tctx->cu_transquant_bypass_flag) {
2960 sbType++;
2961 }
2962
2963
2964 // --- decode position of last coded coefficient ---
2965
2966 int last_significant_coeff_x_prefix =
2967 decode_last_significant_coeff_prefix(tctx,log2TrafoSize,cIdx,
2968 &tctx->ctx_model[CONTEXT_MODEL_LAST_SIGNIFICANT_COEFFICIENT_X_PREFIX]);
2969
2970 int last_significant_coeff_y_prefix =
2971 decode_last_significant_coeff_prefix(tctx,log2TrafoSize,cIdx,
2972 &tctx->ctx_model[CONTEXT_MODEL_LAST_SIGNIFICANT_COEFFICIENT_Y_PREFIX]);
2973
2974
2975 // TODO: we can combine both FL-bypass calls into one, but the gain may be limited...
2976
2977 int LastSignificantCoeffX;
2978 if (last_significant_coeff_x_prefix > 3) {
2979 int nBits = (last_significant_coeff_x_prefix>>1)-1;
2980 int last_significant_coeff_x_suffix = decode_CABAC_FL_bypass(&tctx->cabac_decoder,nBits);
2981
2982 LastSignificantCoeffX =
2983 ((2+(last_significant_coeff_x_prefix & 1)) << nBits) + last_significant_coeff_x_suffix;
2984 }
2985 else {
2986 LastSignificantCoeffX = last_significant_coeff_x_prefix;
2987 }
2988
2989 int LastSignificantCoeffY;
2990 if (last_significant_coeff_y_prefix > 3) {
2991 int nBits = (last_significant_coeff_y_prefix>>1)-1;
2992 int last_significant_coeff_y_suffix = decode_CABAC_FL_bypass(&tctx->cabac_decoder,nBits);
2993
2994 LastSignificantCoeffY =
2995 ((2+(last_significant_coeff_y_prefix & 1)) << nBits) + last_significant_coeff_y_suffix;
2996 }
2997 else {
2998 LastSignificantCoeffY = last_significant_coeff_y_prefix;
2999 }
3000
3001
3002
3003 // --- determine scanIdx ---
3004
3005 int scanIdx;
3006
3007 if (PredMode == MODE_INTRA) {
3008 if (cIdx==0) {
3009 scanIdx = get_intra_scan_idx(log2TrafoSize, img->get_IntraPredMode(x0,y0), cIdx, &sps);
3010 //printf("luma scan idx=%d <- intra mode=%d\n",scanIdx, img->get_IntraPredMode(x0,y0));
3011 }
3012 else {
3013 scanIdx = get_intra_scan_idx(log2TrafoSize, img->get_IntraPredModeC(x0,y0), cIdx, &sps);
3014 //printf("chroma scan idx=%d <- intra mode=%d chroma:%d trsize:%d\n",scanIdx,
3015 // img->get_IntraPredModeC(x0,y0), sps->chroma_format_idc, 1<<log2TrafoSize);
3016 }
3017 }
3018 else {
3019 scanIdx=0;
3020 }
3021
3022 if (scanIdx==2) {
3023 std::swap(LastSignificantCoeffX, LastSignificantCoeffY);
3024 }
3025
3026 logtrace(LogSlice,"LastSignificantCoeff: x=%d;y=%d\n",LastSignificantCoeffX,LastSignificantCoeffY);
3027
3028 const position* ScanOrderSub = get_scan_order(log2TrafoSize-2, scanIdx);
3029 const position* ScanOrderPos = get_scan_order(2, scanIdx);
3030
3031 logtrace(LogSlice,"ScanOrderPos: ");
3032 for (int n=0;n<4*4;n++)
3033 logtrace(LogSlice,"*%d,%d ", ScanOrderPos[n].x, ScanOrderPos[n].y);
3034 logtrace(LogSlice,"*\n");
3035
3036
3037 // --- find last sub block and last scan pos ---
3038
3039 int xC,yC;
3040
3041 scan_position lastScanP = get_scan_position(LastSignificantCoeffX, LastSignificantCoeffY,
3042 scanIdx, log2TrafoSize);
3043
3044 int lastScanPos = lastScanP.scanPos;
3045 int lastSubBlock = lastScanP.subBlock;
3046
3047
3048 int sbWidth = 1<<(log2TrafoSize-2);
3049
3050 uint8_t coded_sub_block_neighbors[32/4*32/4];
3051 memset(coded_sub_block_neighbors,0,sbWidth*sbWidth);
3052
3053 int c1 = 1;
3054 bool firstSubblock = true; // for coeff_abs_level_greater1_flag context model
3055 int lastSubblock_greater1Ctx=false; /* for coeff_abs_level_greater1_flag context model
3056 (initialization not strictly needed)
3057 */
3058
3059 #ifdef DE265_LOG_TRACE
3060 int16_t TransCoeffLevel[32 * 32];
3061 memset(TransCoeffLevel,0, sizeof(uint16_t)*32*32);
3062 #endif
3063
3064 int CoeffStride = 1<<log2TrafoSize;
3065
3066 int lastInvocation_greater1Ctx=0;
3067 int lastInvocation_coeff_abs_level_greater1_flag=0;
3068 int lastInvocation_ctxSet=0;
3069
3070
3071
3072 // ----- decode coefficients -----
3073
3074 tctx->nCoeff[cIdx] = 0;
3075
3076
3077 // i - subblock index
3078 // n - coefficient index in subblock
3079
3080 for (int i=lastSubBlock;i>=0;i--) {
3081 position S = ScanOrderSub[i];
3082 int inferSbDcSigCoeffFlag=0;
3083
3084 logtrace(LogSlice,"sub block scan idx: %d\n",i);
3085
3086
3087 // --- check whether this sub-block is coded ---
3088
3089 int sub_block_is_coded = 0;
3090
3091 if ((i<lastSubBlock) && (i>0)) {
3092 sub_block_is_coded = decode_coded_sub_block_flag(tctx, cIdx,
3093 coded_sub_block_neighbors[S.x+S.y*sbWidth]);
3094 inferSbDcSigCoeffFlag=1;
3095 }
3096 else if (i==0 || i==lastSubBlock) {
3097 // first (DC) and last sub-block are always coded
3098 // - the first will most probably contain coefficients
3099 // - the last obviously contains the last coded coefficient
3100
3101 sub_block_is_coded = 1;
3102 }
3103
3104 if (sub_block_is_coded) {
3105 if (S.x > 0) coded_sub_block_neighbors[S.x-1 + S.y *sbWidth] |= 1;
3106 if (S.y > 0) coded_sub_block_neighbors[S.x + (S.y-1)*sbWidth] |= 2;
3107 }
3108
3109
3110 // ----- find significant coefficients in this sub-block -----
3111
3112 int16_t coeff_value[16];
3113 int8_t coeff_scan_pos[16];
3114 int8_t coeff_sign[16];
3115 int8_t coeff_has_max_base_level[16];
3116 int nCoefficients=0;
3117
3118
3119 if (sub_block_is_coded) {
3120 int x0 = S.x<<2;
3121 int y0 = S.y<<2;
3122
3123 int log2w = log2TrafoSize-2;
3124 int prevCsbf = coded_sub_block_neighbors[S.x+S.y*sbWidth];
3125 uint8_t* ctxIdxMap = ctxIdxLookup[log2w][!!cIdx][!!scanIdx][prevCsbf];
3126
3127 logdebug(LogSlice,"log2w:%d cIdx:%d scanIdx:%d prevCsbf:%d\n",
3128 log2w,cIdx,scanIdx,prevCsbf);
3129
3130
3131 // set the last coded coefficient in the last subblock
3132
3133 int last_coeff = (i==lastSubBlock) ? lastScanPos-1 : 15;
3134
3135 if (i==lastSubBlock) {
3136 coeff_value[nCoefficients] = 1;
3137 coeff_has_max_base_level[nCoefficients] = 1;
3138 coeff_scan_pos[nCoefficients] = lastScanPos;
3139 nCoefficients++;
3140 }
3141
3142
3143 // --- decode all coefficients' significant_coeff flags except for the DC coefficient ---
3144
3145 for (int n= last_coeff ; n>0 ; n--) {
3146 int subX = ScanOrderPos[n].x;
3147 int subY = ScanOrderPos[n].y;
3148 xC = x0 + subX;
3149 yC = y0 + subY;
3150
3151
3152 // for all AC coefficients in sub-block, a significant_coeff flag is coded
3153
3154 int ctxInc;
3155 if (sps.range_extension.transform_skip_context_enabled_flag &&
3156 (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx])) {
3157 ctxInc = ( cIdx == 0 ) ? 42 : (16+27);
3158 }
3159 else {
3160 ctxInc = ctxIdxMap[xC+(yC<<log2TrafoSize)];
3161 }
3162
3163 logtrace(LogSlice,"trafoSize: %d\n",1<<log2TrafoSize);
3164
3165 int significant_coeff = decode_significant_coeff_flag_lookup(tctx, ctxInc);
3166
3167 if (significant_coeff) {
3168 coeff_value[nCoefficients] = 1;
3169 coeff_has_max_base_level[nCoefficients] = 1;
3170 coeff_scan_pos[nCoefficients] = n;
3171 nCoefficients++;
3172
3173 // since we have a coefficient in the sub-block,
3174 // we cannot infer the DC coefficient anymore
3175 inferSbDcSigCoeffFlag = 0;
3176 }
3177 }
3178
3179
3180 // --- decode DC coefficient significance ---
3181
3182 if (last_coeff>=0) // last coded coefficient (always set to 1) is not the DC coefficient
3183 {
3184 if (inferSbDcSigCoeffFlag==0) {
3185 // if we cannot infert the DC coefficient, it is coded
3186
3187 int ctxInc;
3188 if (sps.range_extension.transform_skip_context_enabled_flag &&
3189 (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx])) {
3190 ctxInc = ( cIdx == 0 ) ? 42 : (16+27);
3191 }
3192 else {
3193 ctxInc = ctxIdxMap[x0+(y0<<log2TrafoSize)];
3194 }
3195
3196 int significant_coeff = decode_significant_coeff_flag_lookup(tctx, ctxInc);
3197
3198
3199 if (significant_coeff) {
3200 coeff_value[nCoefficients] = 1;
3201 coeff_has_max_base_level[nCoefficients] = 1;
3202 coeff_scan_pos[nCoefficients] = 0;
3203 nCoefficients++;
3204 }
3205 }
3206 else {
3207 // we can infer that the DC coefficient must be present
3208 coeff_value[nCoefficients] = 1;
3209 coeff_has_max_base_level[nCoefficients] = 1;
3210 coeff_scan_pos[nCoefficients] = 0;
3211 nCoefficients++;
3212 }
3213 }
3214
3215 }
3216
3217
3218 /*
3219 logtrace(LogSlice,"significant_coeff_flags:\n");
3220 for (int y=0;y<4;y++) {
3221 logtrace(LogSlice," ");
3222 for (int x=0;x<4;x++) {
3223 logtrace(LogSlice,"*%d ",significant_coeff_flag[y][x]);
3224 }
3225 logtrace(LogSlice,"*\n");
3226 }
3227 */
3228
3229
3230 if (nCoefficients) {
3231 int ctxSet;
3232 if (i==0 || cIdx>0) { ctxSet=0; }
3233 else { ctxSet=2; }
3234
3235 if (c1==0) { ctxSet++; }
3236 c1=1;
3237
3238
3239 // --- decode greater-1 flags ---
3240
3241 int newLastGreater1ScanPos=-1;
3242
3243 int lastGreater1Coefficient = libde265_min(8,nCoefficients);
3244 for (int c=0;c<lastGreater1Coefficient;c++) {
3245 int greater1_flag =
3246 decode_coeff_abs_level_greater1(tctx, cIdx,i,
3247 c==0,
3248 firstSubblock,
3249 lastSubblock_greater1Ctx,
3250 &lastInvocation_greater1Ctx,
3251 &lastInvocation_coeff_abs_level_greater1_flag,
3252 &lastInvocation_ctxSet, ctxSet);
3253
3254 if (greater1_flag) {
3255 coeff_value[c]++;
3256
3257 c1=0;
3258
3259 if (newLastGreater1ScanPos == -1) {
3260 newLastGreater1ScanPos=c;
3261 }
3262 }
3263 else {
3264 coeff_has_max_base_level[c] = 0;
3265
3266 if (c1<3 && c1>0) {
3267 c1++;
3268 }
3269 }
3270 }
3271
3272 firstSubblock = false;
3273 lastSubblock_greater1Ctx = lastInvocation_greater1Ctx;
3274
3275
3276 // --- decode greater-2 flag ---
3277
3278 if (newLastGreater1ScanPos != -1) {
3279 int flag = decode_coeff_abs_level_greater2(tctx,cIdx, lastInvocation_ctxSet);
3280 coeff_value[newLastGreater1ScanPos] += flag;
3281 coeff_has_max_base_level[newLastGreater1ScanPos] = flag;
3282 }
3283
3284
3285 // --- decode coefficient signs ---
3286
3287 int signHidden;
3288
3289
3290 IntraPredMode predModeIntra;
3291 if (cIdx==0) predModeIntra = img->get_IntraPredMode(x0,y0);
3292 else predModeIntra = img->get_IntraPredModeC(x0,y0);
3293
3294
3295 if (tctx->cu_transquant_bypass_flag ||
3296 (PredMode == MODE_INTRA &&
3297 sps.range_extension.implicit_rdpcm_enabled_flag &&
3298 tctx->transform_skip_flag[cIdx] &&
3299 ( predModeIntra == 10 || predModeIntra == 26 )) ||
3300 tctx->explicit_rdpcm_flag)
3301 {
3302 signHidden = 0;
3303 }
3304 else
3305 {
3306 signHidden = (coeff_scan_pos[0]-coeff_scan_pos[nCoefficients-1] > 3);
3307 }
3308
3309
3310 for (int n=0;n<nCoefficients-1;n++) {
3311 coeff_sign[n] = decode_CABAC_bypass(&tctx->cabac_decoder);
3312 logtrace(LogSlice,"sign[%d] = %d\n", n, coeff_sign[n]);
3313 }
3314
3315 // n==nCoefficients-1
3316 if (!pps.sign_data_hiding_flag || !signHidden) {
3317 coeff_sign[nCoefficients-1] = decode_CABAC_bypass(&tctx->cabac_decoder);
3318 logtrace(LogSlice,"sign[%d] = %d\n", nCoefficients-1, coeff_sign[nCoefficients-1]);
3319 }
3320 else {
3321 coeff_sign[nCoefficients-1] = 0;
3322 }
3323
3324
3325 // --- decode coefficient value ---
3326
3327 int sumAbsLevel=0;
3328 int uiGoRiceParam;
3329
3330 if (sps.range_extension.persistent_rice_adaptation_enabled_flag==0) {
3331 uiGoRiceParam = 0;
3332 }
3333 else {
3334 uiGoRiceParam = tctx->StatCoeff[sbType]/4;
3335 }
3336
3337 // printf("initial uiGoRiceParam=%d\n",uiGoRiceParam);
3338 bool firstCoeffWithAbsLevelRemaining = true;
3339
3340 for (int n=0;n<nCoefficients;n++) {
3341 int baseLevel = coeff_value[n];
3342
3343 int coeff_abs_level_remaining;
3344
3345 // printf("coeff %d/%d, uiRiceParam: %d\n",n,nCoefficients,uiGoRiceParam);
3346
3347 if (coeff_has_max_base_level[n]) {
3348 coeff_abs_level_remaining =
3349 decode_coeff_abs_level_remaining(tctx, uiGoRiceParam);
3350
3351 if (sps.range_extension.persistent_rice_adaptation_enabled_flag == 0) {
3352 // (2014.10 / 9-20)
3353 if (baseLevel + coeff_abs_level_remaining > 3*(1<<uiGoRiceParam)) {
3354 uiGoRiceParam++;
3355 if (uiGoRiceParam>4) uiGoRiceParam=4;
3356 }
3357 }
3358 else {
3359 if (baseLevel + coeff_abs_level_remaining > 3*(1<<uiGoRiceParam))
3360 uiGoRiceParam++;
3361 }
3362
3363 // persistent_rice_adaptation_enabled_flag
3364 if (sps.range_extension.persistent_rice_adaptation_enabled_flag &&
3365 firstCoeffWithAbsLevelRemaining) {
3366 if (coeff_abs_level_remaining >= (3 << (tctx->StatCoeff[sbType]/4 ))) {
3367 tctx->StatCoeff[sbType]++;
3368 }
3369 else if (2*coeff_abs_level_remaining < (1 << (tctx->StatCoeff[sbType]/4 )) &&
3370 tctx->StatCoeff[sbType] > 0) {
3371 tctx->StatCoeff[sbType]--;
3372 }
3373 }
3374
3375 firstCoeffWithAbsLevelRemaining=false;
3376 }
3377 else {
3378 coeff_abs_level_remaining = 0;
3379 }
3380
3381 logtrace(LogSlice, "coeff_abs_level_remaining=%d\n",coeff_abs_level_remaining);
3382
3383
3384 int16_t currCoeff = baseLevel + coeff_abs_level_remaining;
3385 if (coeff_sign[n]) {
3386 currCoeff = -currCoeff;
3387 }
3388
3389 if (pps.sign_data_hiding_flag && signHidden) {
3390 sumAbsLevel += baseLevel + coeff_abs_level_remaining;
3391
3392 if (n==nCoefficients-1 && (sumAbsLevel & 1)) {
3393 currCoeff = -currCoeff;
3394 }
3395 }
3396
3397 logtrace(LogSlice, "quantized coefficient=%d\n",currCoeff);
3398
3399 #ifdef DE265_LOG_TRACE
3400 //TransCoeffLevel[yC*CoeffStride + xC] = currCoeff;
3401 #endif
3402
3403 // put coefficient in list
3404 int p = coeff_scan_pos[n];
3405 xC = (S.x<<2) + ScanOrderPos[p].x;
3406 yC = (S.y<<2) + ScanOrderPos[p].y;
3407
3408 tctx->coeffList[cIdx][ tctx->nCoeff[cIdx] ] = currCoeff;
3409 tctx->coeffPos [cIdx][ tctx->nCoeff[cIdx] ] = xC + yC*CoeffStride;
3410 tctx->nCoeff[cIdx]++;
3411
3412 //printf("%d ",currCoeff);
3413 } // iterate through coefficients in sub-block
3414
3415 //printf(" (%d;%d)\n",x0,y0);
3416
3417 } // if nonZero
3418 } // next sub-block
3419
3420 return DE265_OK;
3421 }
3422
3423
decode_TU(thread_context * tctx,int x0,int y0,int xCUBase,int yCUBase,int nT,int cIdx,enum PredMode cuPredMode,bool cbf)3424 static void decode_TU(thread_context* tctx,
3425 int x0,int y0,
3426 int xCUBase,int yCUBase,
3427 int nT, int cIdx, enum PredMode cuPredMode, bool cbf)
3428 {
3429 de265_image* img = tctx->img;
3430 const seq_parameter_set& sps = img->get_sps();
3431
3432 int residualDpcm = 0;
3433
3434 if (cuPredMode == MODE_INTRA) // if intra mode
3435 {
3436 enum IntraPredMode intraPredMode;
3437
3438 if (cIdx==0) {
3439 intraPredMode = img->get_IntraPredMode(x0,y0);
3440 }
3441 else {
3442 const int SubWidthC = sps.SubWidthC;
3443 const int SubHeightC = sps.SubHeightC;
3444
3445 intraPredMode = img->get_IntraPredModeC(x0*SubWidthC,y0*SubHeightC);
3446 }
3447
3448 if (intraPredMode<0 || intraPredMode>=35) {
3449 // TODO: ERROR
3450 intraPredMode = INTRA_DC;
3451 }
3452
3453 decode_intra_prediction(img, x0,y0, intraPredMode, nT, cIdx);
3454
3455
3456 residualDpcm = sps.range_extension.implicit_rdpcm_enabled_flag &&
3457 (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx]) &&
3458 (intraPredMode == 10 || intraPredMode == 26);
3459
3460 if (residualDpcm && intraPredMode == 26)
3461 residualDpcm = 2;
3462 }
3463 else // INTER
3464 {
3465 if (tctx->explicit_rdpcm_flag) {
3466 residualDpcm = (tctx->explicit_rdpcm_dir ? 2 : 1);
3467 }
3468 }
3469
3470 if (cbf) {
3471 scale_coefficients(tctx, x0,y0, xCUBase,yCUBase, nT, cIdx,
3472 tctx->transform_skip_flag[cIdx], cuPredMode==MODE_INTRA, residualDpcm);
3473 }
3474 /*
3475 else if (!cbf && cIdx==0) {
3476 memset(tctx->residual_luma,0,32*32*sizeof(int32_t));
3477 }
3478 */
3479 else if (!cbf && cIdx!=0 && tctx->ResScaleVal) {
3480 // --- cross-component-prediction when CBF==0 ---
3481
3482 tctx->nCoeff[cIdx] = 0;
3483 residualDpcm=0;
3484
3485 scale_coefficients(tctx, x0,y0, xCUBase,yCUBase, nT, cIdx,
3486 tctx->transform_skip_flag[cIdx], cuPredMode==MODE_INTRA, residualDpcm);
3487 }
3488 }
3489
3490
decode_log2_res_scale_abs_plus1(thread_context * tctx,int cIdxMinus1)3491 static int decode_log2_res_scale_abs_plus1(thread_context* tctx, int cIdxMinus1)
3492 {
3493 //const int context = (cIdx==0) ? 0 : 1;
3494
3495 logtrace(LogSlice,"# log2_res_scale_abs_plus1 (c=%d)\n",cIdxMinus1);
3496
3497 int value = 0;
3498 int cMax = 4;
3499 for (int binIdx=0;binIdx<cMax;binIdx++)
3500 {
3501 int ctxIdxInc = 4*cIdxMinus1 + binIdx;
3502
3503 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
3504 &tctx->ctx_model[CONTEXT_MODEL_LOG2_RES_SCALE_ABS_PLUS1+ctxIdxInc]);
3505 if (!bit) break;
3506 value++;
3507 }
3508
3509 logtrace(LogSymbols,"$1 log2_res_scale_abs_plus1=%d\n",value);
3510
3511 return value;
3512 }
3513
3514
decode_res_scale_sign_flag(thread_context * tctx,int cIdxMinus1)3515 static int decode_res_scale_sign_flag(thread_context* tctx, int cIdxMinus1)
3516 {
3517 //const int context = (cIdx==0) ? 0 : 1;
3518
3519 logtrace(LogSlice,"# res_scale_sign_flag (c=%d)\n",cIdxMinus1);
3520
3521 int bit = decode_CABAC_bit(&tctx->cabac_decoder,
3522 &tctx->ctx_model[CONTEXT_MODEL_RES_SCALE_SIGN_FLAG+cIdxMinus1]);
3523
3524 logtrace(LogSymbols,"$1 res_scale_sign_flag=%d\n",bit);
3525
3526 return bit;
3527 }
3528
3529
read_cross_comp_pred(thread_context * tctx,int cIdxMinus1)3530 static void read_cross_comp_pred(thread_context* tctx, int cIdxMinus1)
3531 {
3532 int log2_res_scale_abs_plus1 = decode_log2_res_scale_abs_plus1(tctx,cIdxMinus1);
3533 int ResScaleVal;
3534
3535 if (log2_res_scale_abs_plus1 != 0) {
3536 int res_scale_sign_flag = decode_res_scale_sign_flag(tctx,cIdxMinus1);
3537
3538 ResScaleVal = 1 << (log2_res_scale_abs_plus1 - 1);
3539 ResScaleVal *= 1 - 2 * res_scale_sign_flag;
3540 }
3541 else {
3542 ResScaleVal = 0;
3543 }
3544
3545 tctx->ResScaleVal = ResScaleVal;
3546 }
3547
3548
read_transform_unit(thread_context * tctx,int x0,int y0,int xBase,int yBase,int xCUBase,int yCUBase,int log2TrafoSize,int trafoDepth,int blkIdx,int cbf_luma,int cbf_cb,int cbf_cr)3549 int read_transform_unit(thread_context* tctx,
3550 int x0, int y0, // position of TU in frame
3551 int xBase, int yBase, // position of parent TU in frame
3552 int xCUBase,int yCUBase, // position of CU in frame
3553 int log2TrafoSize,
3554 int trafoDepth,
3555 int blkIdx,
3556 int cbf_luma, int cbf_cb, int cbf_cr)
3557 {
3558 logtrace(LogSlice,"- read_transform_unit x0:%d y0:%d xBase:%d yBase:%d nT:%d cbf:%d:%d:%d\n",
3559 x0,y0,xBase,yBase, 1<<log2TrafoSize, cbf_luma, cbf_cb, cbf_cr);
3560
3561 assert(cbf_cb != -1);
3562 assert(cbf_cr != -1);
3563 assert(cbf_luma != -1);
3564
3565 const seq_parameter_set& sps = tctx->img->get_sps();
3566
3567 const int ChromaArrayType = sps.ChromaArrayType;
3568
3569 int log2TrafoSizeC = (ChromaArrayType==CHROMA_444 ? log2TrafoSize : log2TrafoSize-1);
3570 log2TrafoSizeC = libde265_max(2, log2TrafoSizeC);
3571
3572 const int cbfLuma = cbf_luma;
3573 const int cbfChroma = cbf_cb | cbf_cr;
3574
3575 tctx->transform_skip_flag[0]=0;
3576 tctx->transform_skip_flag[1]=0;
3577 tctx->transform_skip_flag[2]=0;
3578
3579 tctx->explicit_rdpcm_flag = false;
3580
3581
3582 enum PredMode cuPredMode = tctx->img->get_pred_mode(x0,y0);
3583
3584 if (cbfLuma || cbfChroma)
3585 {
3586 bool doDecodeQuantParameters = false;
3587
3588 if (tctx->img->get_pps().cu_qp_delta_enabled_flag &&
3589 !tctx->IsCuQpDeltaCoded) {
3590
3591 int cu_qp_delta_abs = decode_cu_qp_delta_abs(tctx);
3592 int cu_qp_delta_sign=0;
3593 if (cu_qp_delta_abs) {
3594 cu_qp_delta_sign = decode_CABAC_bypass(&tctx->cabac_decoder);
3595 }
3596
3597 tctx->IsCuQpDeltaCoded = 1;
3598 tctx->CuQpDelta = cu_qp_delta_abs*(1-2*cu_qp_delta_sign);
3599
3600 //printf("read cu_qp_delta (%d;%d) = %d\n",x0,y0,tctx->CuQpDelta);
3601
3602 logtrace(LogSlice,"cu_qp_delta_abs = %d\n",cu_qp_delta_abs);
3603 logtrace(LogSlice,"cu_qp_delta_sign = %d\n",cu_qp_delta_sign);
3604 logtrace(LogSlice,"CuQpDelta = %d\n",tctx->CuQpDelta);
3605
3606 doDecodeQuantParameters = true;
3607 //decode_quantization_parameters(tctx, x0,y0, xCUBase, yCUBase);
3608 }
3609
3610 if (tctx->shdr->cu_chroma_qp_offset_enabled_flag && cbfChroma &&
3611 !tctx->cu_transquant_bypass_flag && !tctx->IsCuChromaQpOffsetCoded ) {
3612 logtrace(LogSlice,"# cu_chroma_qp_offset_flag\n");
3613
3614 int cu_chroma_qp_offset_flag = decode_CABAC_bit(&tctx->cabac_decoder,
3615 &tctx->ctx_model[CONTEXT_MODEL_CU_CHROMA_QP_OFFSET_FLAG]);
3616
3617
3618 const pic_parameter_set& pps = tctx->img->get_pps();
3619
3620 int cu_chroma_qp_offset_idx = 0;
3621 if (cu_chroma_qp_offset_flag && pps.range_extension.chroma_qp_offset_list_len > 1) {
3622 cu_chroma_qp_offset_idx = decode_CABAC_bit(&tctx->cabac_decoder,
3623 &tctx->ctx_model[CONTEXT_MODEL_CU_CHROMA_QP_OFFSET_IDX]);
3624 }
3625
3626 tctx->IsCuChromaQpOffsetCoded = 1;
3627
3628 if (cu_chroma_qp_offset_flag) {
3629 tctx->CuQpOffsetCb = pps.range_extension.cb_qp_offset_list[ cu_chroma_qp_offset_idx ];
3630 tctx->CuQpOffsetCr = pps.range_extension.cr_qp_offset_list[ cu_chroma_qp_offset_idx ];
3631 }
3632 else {
3633 tctx->CuQpOffsetCb = 0;
3634 tctx->CuQpOffsetCr = 0;
3635 }
3636
3637 doDecodeQuantParameters = true;
3638 //decode_quantization_parameters(tctx, x0,y0, xCUBase, yCUBase);
3639 }
3640
3641
3642 if (doDecodeQuantParameters) {
3643 decode_quantization_parameters(tctx, x0,y0, xCUBase, yCUBase);
3644 }
3645 }
3646
3647 // position of TU in local CU
3648 int xL = x0 - xCUBase;
3649 int yL = y0 - yCUBase;
3650 int nT = 1<<log2TrafoSize;
3651 int nTC = 1<<log2TrafoSizeC;
3652
3653 const int SubWidthC = sps.SubWidthC;
3654 const int SubHeightC = sps.SubHeightC;
3655
3656 // --- luma ---
3657
3658 tctx->ResScaleVal = 0;
3659
3660 int err;
3661 if (cbf_luma) {
3662 if ((err=residual_coding(tctx,x0,y0, log2TrafoSize,0)) != DE265_OK) return err;
3663 }
3664
3665 decode_TU(tctx, x0,y0, xCUBase,yCUBase, nT, 0, cuPredMode, cbf_luma);
3666
3667
3668 // --- chroma ---
3669
3670 const int yOffset422 = 1<<log2TrafoSizeC;
3671
3672 if (log2TrafoSize>2 || ChromaArrayType == CHROMA_444) {
3673 // TODO: cross-component prediction
3674
3675 const bool do_cross_component_prediction =
3676 (tctx->img->get_pps().range_extension.cross_component_prediction_enabled_flag &&
3677 cbf_luma &&
3678 (cuPredMode == MODE_INTER || tctx->img->is_IntraPredModeC_Mode4(x0,y0)));
3679
3680 if (do_cross_component_prediction) {
3681 read_cross_comp_pred(tctx, 0);
3682 }
3683 else {
3684 tctx->ResScaleVal = 0;
3685 }
3686
3687 {
3688 if (cbf_cb & 1) {
3689 if ((err=residual_coding(tctx,x0,y0,log2TrafoSizeC,1)) != DE265_OK) return err;
3690 }
3691
3692 if (sps.ChromaArrayType != CHROMA_MONO) {
3693 decode_TU(tctx,
3694 x0/SubWidthC,y0/SubHeightC,
3695 xCUBase/SubWidthC,yCUBase/SubHeightC, nTC, 1, cuPredMode, cbf_cb & 1);
3696 }
3697 }
3698
3699 // 4:2:2
3700 if (ChromaArrayType == CHROMA_422) {
3701 const int yOffset = 1<<log2TrafoSizeC;
3702
3703 if (cbf_cb & 2) {
3704 if ((err=residual_coding(tctx,
3705 x0,y0+yOffset*SubHeightC,
3706 log2TrafoSizeC,1)) != DE265_OK) return err;
3707 }
3708
3709 decode_TU(tctx,
3710 x0/SubWidthC,y0/SubHeightC + yOffset,
3711 xCUBase/SubWidthC,yCUBase/SubHeightC +yOffset,
3712 nTC, 1, cuPredMode, cbf_cb & 2);
3713 }
3714
3715
3716 if (do_cross_component_prediction) {
3717 read_cross_comp_pred(tctx, 1);
3718 }
3719 else {
3720 tctx->ResScaleVal = 0;
3721 }
3722
3723 {
3724 if (cbf_cr & 1) {
3725 if ((err=residual_coding(tctx,x0,y0,log2TrafoSizeC,2)) != DE265_OK) return err;
3726 }
3727
3728 if (sps.ChromaArrayType != CHROMA_MONO) {
3729 decode_TU(tctx,
3730 x0/SubWidthC,y0/SubHeightC,
3731 xCUBase/SubWidthC,yCUBase/SubHeightC,
3732 nTC, 2, cuPredMode, cbf_cr & 1);
3733 }
3734 }
3735
3736 // 4:2:2
3737 if (ChromaArrayType == CHROMA_422) {
3738 const int yOffset = 1<<log2TrafoSizeC;
3739
3740 if (cbf_cr & 2) {
3741 if ((err=residual_coding(tctx,
3742 x0,y0+yOffset*SubHeightC,
3743 log2TrafoSizeC,2)) != DE265_OK) return err;
3744 }
3745
3746 decode_TU(tctx,
3747 x0/SubWidthC,y0/SubHeightC+yOffset,
3748 xCUBase/SubWidthC,yCUBase/SubHeightC+yOffset,
3749 nTC, 2, cuPredMode, cbf_cr & 2);
3750 }
3751 }
3752 else if (blkIdx==3) {
3753 if (cbf_cb & 1) {
3754 if ((err=residual_coding(tctx,xBase,yBase,
3755 log2TrafoSize,1)) != DE265_OK) return err;
3756 }
3757
3758 if (sps.ChromaArrayType != CHROMA_MONO) {
3759 decode_TU(tctx,
3760 xBase/SubWidthC, yBase/SubHeightC,
3761 xCUBase/SubWidthC,yCUBase/SubHeightC, nT, 1, cuPredMode, cbf_cb & 1);
3762 }
3763
3764 // 4:2:2
3765 if (cbf_cb & 2) {
3766 if ((err=residual_coding(tctx,
3767 xBase ,yBase +(1<<log2TrafoSize),
3768 log2TrafoSize,1)) != DE265_OK) return err;
3769 }
3770
3771 if (ChromaArrayType == CHROMA_422) {
3772 decode_TU(tctx,
3773 xBase/SubWidthC, yBase/SubHeightC + (1<<log2TrafoSize),
3774 xCUBase/SubWidthC,yCUBase/SubHeightC, nT, 1, cuPredMode, cbf_cb & 2);
3775 }
3776
3777 if (cbf_cr & 1) {
3778 if ((err=residual_coding(tctx,xBase,yBase,
3779 log2TrafoSize,2)) != DE265_OK) return err;
3780 }
3781
3782 if (sps.ChromaArrayType != CHROMA_MONO) {
3783 decode_TU(tctx,
3784 xBase/SubWidthC, yBase/SubHeightC,
3785 xCUBase/SubWidthC,yCUBase/SubHeightC, nT, 2, cuPredMode, cbf_cr & 1);
3786 }
3787
3788 // 4:2:2
3789 if (cbf_cr & 2) {
3790 if ((err=residual_coding(tctx,
3791 xBase ,yBase +(1<<log2TrafoSizeC),
3792 log2TrafoSize,2)) != DE265_OK) return err;
3793 }
3794
3795 if (ChromaArrayType == CHROMA_422) {
3796 decode_TU(tctx,
3797 xBase/SubWidthC, yBase/SubHeightC + (1<<log2TrafoSize),
3798 xCUBase/SubWidthC,yCUBase/SubHeightC, nT, 2, cuPredMode, cbf_cr & 2);
3799 }
3800 }
3801
3802
3803 return DE265_OK;
3804 }
3805
3806
dump_cbsize(de265_image * img)3807 static void dump_cbsize(de265_image* img)
3808 {
3809 int w = img->get_width(0);
3810 int h = img->get_height(0);
3811
3812 for (int y=0;y<h;y+=8) {
3813 for (int x=0;x<w;x+=8) {
3814 printf("%d",img->get_log2CbSize(x,y));
3815 }
3816 printf("\n");
3817 }
3818 }
3819
3820
read_transform_tree(thread_context * tctx,int x0,int y0,int xBase,int yBase,int xCUBase,int yCUBase,int log2TrafoSize,int trafoDepth,int blkIdx,int MaxTrafoDepth,int IntraSplitFlag,enum PredMode cuPredMode,uint8_t parent_cbf_cb,uint8_t parent_cbf_cr)3821 void read_transform_tree(thread_context* tctx,
3822 int x0, int y0, // position of TU in frame
3823 int xBase, int yBase, // position of parent TU in frame
3824 int xCUBase, int yCUBase, // position of CU in frame
3825 int log2TrafoSize,
3826 int trafoDepth,
3827 int blkIdx,
3828 int MaxTrafoDepth,
3829 int IntraSplitFlag,
3830 enum PredMode cuPredMode,
3831 uint8_t parent_cbf_cb,uint8_t parent_cbf_cr)
3832 {
3833 logtrace(LogSlice,"- read_transform_tree (interleaved) x0:%d y0:%d xBase:%d yBase:%d "
3834 "log2TrafoSize:%d trafoDepth:%d MaxTrafoDepth:%d parent-cbf-cb:%d parent-cbf-cr:%d\n",
3835 x0,y0,xBase,yBase,log2TrafoSize,trafoDepth,MaxTrafoDepth,parent_cbf_cb,parent_cbf_cr);
3836
3837 de265_image* img = tctx->img;
3838 const seq_parameter_set& sps = img->get_sps();
3839
3840 int split_transform_flag;
3841
3842 enum PredMode PredMode = img->get_pred_mode(x0,y0);
3843 assert(PredMode == cuPredMode);
3844
3845 /* If TrafoSize is larger than maximum size -> split automatically
3846 If TrafoSize is at minimum size -> do not split
3847 If maximum transformation depth is reached -> do not split
3848 If intra-prediction is NxN mode -> split automatically (only at level 0)
3849 Otherwise -> read split flag
3850 */
3851 if (log2TrafoSize <= sps.Log2MaxTrafoSize &&
3852 log2TrafoSize > sps.Log2MinTrafoSize &&
3853 trafoDepth < MaxTrafoDepth &&
3854 !(IntraSplitFlag && trafoDepth==0))
3855 {
3856 split_transform_flag = decode_split_transform_flag(tctx, log2TrafoSize);
3857 }
3858 else
3859 {
3860 enum PartMode PartMode = img->get_PartMode(x0,y0);
3861
3862 int interSplitFlag= (sps.max_transform_hierarchy_depth_inter==0 &&
3863 trafoDepth == 0 &&
3864 PredMode == MODE_INTER &&
3865 PartMode != PART_2Nx2N);
3866
3867 split_transform_flag = (log2TrafoSize > sps.Log2MaxTrafoSize ||
3868 (IntraSplitFlag==1 && trafoDepth==0) ||
3869 interSplitFlag==1) ? 1:0;
3870 }
3871
3872 if (split_transform_flag) {
3873 logtrace(LogSlice,"set_split_transform_flag(%d,%d, %d)\n",x0,y0,trafoDepth);
3874 img->set_split_transform_flag(x0,y0,trafoDepth);
3875 }
3876
3877 int cbf_cb=-1;
3878 int cbf_cr=-1;
3879
3880 // CBF_CB/CR flags are encoded like this:
3881 // 4:2:0 and 4:4:4 modes: binary flag in bit 0
3882 // 4:2:2 mode: bit 0: top block, bit 1: bottom block
3883
3884 if ((log2TrafoSize>2 && sps.ChromaArrayType != CHROMA_MONO) ||
3885 sps.ChromaArrayType == CHROMA_444) {
3886 // we do not have to test for trafoDepth==0, because parent_cbf_cb is 1 at depth 0
3887 if (/*trafoDepth==0 ||*/ parent_cbf_cb) {
3888 cbf_cb = decode_cbf_chroma(tctx,trafoDepth);
3889
3890 if (sps.ChromaArrayType == CHROMA_422 && (!split_transform_flag || log2TrafoSize==3)) {
3891 cbf_cb |= (decode_cbf_chroma(tctx,trafoDepth) << 1);
3892 }
3893 }
3894
3895 // we do not have to test for trafoDepth==0, because parent_cbf_cb is 1 at depth 0
3896 if (/*trafoDepth==0 ||*/ parent_cbf_cr) {
3897 cbf_cr = decode_cbf_chroma(tctx,trafoDepth);
3898
3899 if (sps.ChromaArrayType == CHROMA_422 && (!split_transform_flag || log2TrafoSize==3)) {
3900 cbf_cr |= (decode_cbf_chroma(tctx,trafoDepth) << 1);
3901 }
3902 }
3903 }
3904
3905 //printf("CBF: cb:%d cr:%d\n",cbf_cb,cbf_cr);
3906
3907 // cbf_cr/cbf_cb not present in bitstream -> induce values
3908
3909 if (cbf_cb<0) {
3910 assert(!(trafoDepth==0 && log2TrafoSize==2));
3911
3912 /* The standard specifies to check trafoDepth>0 AND log2TrafoSize==2.
3913 However, I think that trafoDepth>0 is redundant as a CB is always
3914 at least 8x8 and hence trafoDepth>0.
3915 */
3916
3917 if (trafoDepth>0 && log2TrafoSize==2) {
3918 cbf_cb = parent_cbf_cb;
3919 } else {
3920 cbf_cb=0;
3921 }
3922 }
3923
3924 if (cbf_cr<0) {
3925 if (trafoDepth>0 && log2TrafoSize==2) {
3926 cbf_cr = parent_cbf_cr;
3927 } else {
3928 cbf_cr=0;
3929 }
3930 }
3931
3932 if (split_transform_flag) {
3933 int x1 = x0 + (1<<(log2TrafoSize-1));
3934 int y1 = y0 + (1<<(log2TrafoSize-1));
3935
3936 logtrace(LogSlice,"transform split.\n");
3937
3938 read_transform_tree(tctx, x0,y0, x0,y0, xCUBase,yCUBase, log2TrafoSize-1, trafoDepth+1, 0,
3939 MaxTrafoDepth,IntraSplitFlag, cuPredMode, cbf_cb,cbf_cr);
3940 read_transform_tree(tctx, x1,y0, x0,y0, xCUBase,yCUBase, log2TrafoSize-1, trafoDepth+1, 1,
3941 MaxTrafoDepth,IntraSplitFlag, cuPredMode, cbf_cb,cbf_cr);
3942 read_transform_tree(tctx, x0,y1, x0,y0, xCUBase,yCUBase, log2TrafoSize-1, trafoDepth+1, 2,
3943 MaxTrafoDepth,IntraSplitFlag, cuPredMode, cbf_cb,cbf_cr);
3944 read_transform_tree(tctx, x1,y1, x0,y0, xCUBase,yCUBase, log2TrafoSize-1, trafoDepth+1, 3,
3945 MaxTrafoDepth,IntraSplitFlag, cuPredMode, cbf_cb,cbf_cr);
3946 }
3947 else {
3948 int cbf_luma;
3949
3950 if (PredMode==MODE_INTRA || trafoDepth!=0 || cbf_cb || cbf_cr) {
3951 cbf_luma = decode_cbf_luma(tctx,trafoDepth);
3952 }
3953 else {
3954 /* There cannot be INTER blocks with no residual data.
3955 That case is already handled with rqt_root_cbf.
3956 */
3957
3958 cbf_luma = 1;
3959 }
3960
3961 logtrace(LogSlice,"call read_transform_unit %d/%d\n",x0,y0);
3962
3963 read_transform_unit(tctx, x0,y0,xBase,yBase, xCUBase,yCUBase, log2TrafoSize,trafoDepth, blkIdx,
3964 cbf_luma, cbf_cb, cbf_cr);
3965 }
3966 }
3967
3968
part_mode_name(enum PartMode pm)3969 const char* part_mode_name(enum PartMode pm)
3970 {
3971 switch (pm) {
3972 case PART_2Nx2N: return "2Nx2N";
3973 case PART_2NxN: return "2NxN";
3974 case PART_Nx2N: return "Nx2N";
3975 case PART_NxN: return "NxN";
3976 case PART_2NxnU: return "2NxnU";
3977 case PART_2NxnD: return "2NxnD";
3978 case PART_nLx2N: return "nLx2N";
3979 case PART_nRx2N: return "nRx2N";
3980 }
3981
3982 return "undefined part mode";
3983 }
3984
3985
read_mvd_coding(thread_context * tctx,int x0,int y0,int refList)3986 void read_mvd_coding(thread_context* tctx,
3987 int x0,int y0, int refList)
3988 {
3989 int abs_mvd_greater0_flag[2];
3990 abs_mvd_greater0_flag[0] = decode_CABAC_bit(&tctx->cabac_decoder,
3991 &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG+0]);
3992 abs_mvd_greater0_flag[1] = decode_CABAC_bit(&tctx->cabac_decoder,
3993 &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG+0]);
3994
3995 int abs_mvd_greater1_flag[2];
3996 if (abs_mvd_greater0_flag[0]) {
3997 abs_mvd_greater1_flag[0] = decode_CABAC_bit(&tctx->cabac_decoder,
3998 &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG+1]);
3999 }
4000 else {
4001 abs_mvd_greater1_flag[0]=0;
4002 }
4003
4004 if (abs_mvd_greater0_flag[1]) {
4005 abs_mvd_greater1_flag[1] = decode_CABAC_bit(&tctx->cabac_decoder,
4006 &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG+1]);
4007 }
4008 else {
4009 abs_mvd_greater1_flag[1]=0;
4010 }
4011
4012
4013 int abs_mvd_minus2[2];
4014 int mvd_sign_flag[2];
4015 int value[2];
4016
4017 for (int c=0;c<2;c++) {
4018 if (abs_mvd_greater0_flag[c]) {
4019 if (abs_mvd_greater1_flag[c]) {
4020 abs_mvd_minus2[c] = decode_CABAC_EGk_bypass(&tctx->cabac_decoder, 1);
4021 }
4022 else {
4023 abs_mvd_minus2[c] = abs_mvd_greater1_flag[c] -1;
4024 }
4025
4026 mvd_sign_flag[c] = decode_CABAC_bypass(&tctx->cabac_decoder);
4027
4028 value[c] = abs_mvd_minus2[c]+2;
4029 if (mvd_sign_flag[c]) { value[c] = -value[c]; }
4030 }
4031 else {
4032 value[c] = 0;
4033 }
4034 }
4035
4036 //set_mvd(tctx->decctx, x0,y0, refList, value[0],value[1]);
4037 tctx->motion.mvd[refList][0] = value[0];
4038 tctx->motion.mvd[refList][1] = value[1];
4039
4040 logtrace(LogSlice, "MVD[%d;%d|%d] = %d;%d\n",x0,y0,refList, value[0],value[1]);
4041 }
4042
4043
read_prediction_unit_SKIP(thread_context * tctx,int x0,int y0,int nPbW,int nPbH)4044 void read_prediction_unit_SKIP(thread_context* tctx,
4045 int x0, int y0,
4046 int nPbW, int nPbH)
4047 {
4048 int merge_idx = decode_merge_idx(tctx);
4049
4050 tctx->motion.merge_idx = merge_idx;
4051 tctx->motion.merge_flag = true;
4052
4053 logtrace(LogSlice,"prediction skip 2Nx2N, merge_idx: %d\n",merge_idx);
4054 }
4055
4056
4057 /* xC/yC : CB position
4058 xB/yB : position offset of the PB
4059 nPbW/nPbH : size of PB
4060 nCS : CB size
4061 */
read_prediction_unit(thread_context * tctx,int xC,int yC,int xB,int yB,int nPbW,int nPbH,int ctDepth,int nCS,int partIdx)4062 void read_prediction_unit(thread_context* tctx,
4063 int xC,int yC, int xB,int yB,
4064 int nPbW, int nPbH,
4065 int ctDepth, int nCS,int partIdx)
4066 {
4067 logtrace(LogSlice,"read_prediction_unit %d;%d %dx%d\n",xC+xB,yC+xB,nPbW,nPbH);
4068
4069 int x0 = xC+xB;
4070 int y0 = yC+yB;
4071
4072 slice_segment_header* shdr = tctx->shdr;
4073
4074 int merge_flag = decode_merge_flag(tctx);
4075 tctx->motion.merge_flag = merge_flag;
4076
4077 if (merge_flag) {
4078 int merge_idx = decode_merge_idx(tctx);
4079
4080 logtrace(LogSlice,"prediction unit %d,%d, merge mode, index: %d\n",x0,y0,merge_idx);
4081
4082 tctx->motion.merge_idx = merge_idx;
4083 }
4084 else { // no merge flag
4085 enum InterPredIdc inter_pred_idc;
4086
4087 if (shdr->slice_type == SLICE_TYPE_B) {
4088 inter_pred_idc = decode_inter_pred_idc(tctx,x0,y0,nPbW,nPbH,ctDepth);
4089 }
4090 else {
4091 inter_pred_idc = PRED_L0;
4092 }
4093
4094 tctx->motion.inter_pred_idc = inter_pred_idc; // set_inter_pred_idc(ctx,x0,y0, inter_pred_idc);
4095
4096 if (inter_pred_idc != PRED_L1) {
4097 int ref_idx_l0 = decode_ref_idx_lX(tctx, shdr->num_ref_idx_l0_active);
4098
4099 // NOTE: case for only one reference frame is handles in decode_ref_idx_lX()
4100 tctx->motion.refIdx[0] = ref_idx_l0;
4101
4102 read_mvd_coding(tctx,x0,y0, 0);
4103
4104 int mvp_l0_flag = decode_mvp_lx_flag(tctx); // l0
4105 tctx->motion.mvp_l0_flag = mvp_l0_flag;
4106
4107 logtrace(LogSlice,"prediction unit %d,%d, L0, refIdx=%d mvp_l0_flag:%d\n",
4108 x0,y0, tctx->motion.refIdx[0], mvp_l0_flag);
4109 }
4110
4111 if (inter_pred_idc != PRED_L0) {
4112 int ref_idx_l1 = decode_ref_idx_lX(tctx, shdr->num_ref_idx_l1_active);
4113
4114 // NOTE: case for only one reference frame is handles in decode_ref_idx_lX()
4115 tctx->motion.refIdx[1] = ref_idx_l1;
4116
4117 if (shdr->mvd_l1_zero_flag &&
4118 inter_pred_idc == PRED_BI) {
4119 tctx->motion.mvd[1][0] = 0;
4120 tctx->motion.mvd[1][1] = 0;
4121 }
4122 else {
4123 read_mvd_coding(tctx,x0,y0, 1);
4124 }
4125
4126 int mvp_l1_flag = decode_mvp_lx_flag(tctx); // l1
4127 tctx->motion.mvp_l1_flag = mvp_l1_flag;
4128
4129 logtrace(LogSlice,"prediction unit %d,%d, L1, refIdx=%d mvp_l1_flag:%d\n",
4130 x0,y0, tctx->motion.refIdx[1], mvp_l1_flag);
4131 }
4132 }
4133
4134
4135
4136 decode_prediction_unit(tctx->decctx, tctx->shdr, tctx->img, tctx->motion,
4137 xC,yC,xB,yB, nCS, nPbW,nPbH, partIdx);
4138 }
4139
4140
4141
4142
4143 template <class pixel_t>
read_pcm_samples_internal(thread_context * tctx,int x0,int y0,int log2CbSize,int cIdx,bitreader & br)4144 void read_pcm_samples_internal(thread_context* tctx, int x0, int y0, int log2CbSize,
4145 int cIdx, bitreader& br)
4146 {
4147 const seq_parameter_set& sps = tctx->img->get_sps();
4148
4149 int nPcmBits;
4150 int bitDepth;
4151
4152 int w = 1<<log2CbSize;
4153 int h = 1<<log2CbSize;
4154
4155 if (cIdx>0) {
4156 w /= sps.SubWidthC;
4157 h /= sps.SubHeightC;
4158
4159 x0 /= sps.SubWidthC;
4160 y0 /= sps.SubHeightC;
4161
4162 nPcmBits = sps.pcm_sample_bit_depth_chroma;
4163 bitDepth = sps.BitDepth_C;
4164 }
4165 else {
4166 nPcmBits = sps.pcm_sample_bit_depth_luma;
4167 bitDepth = sps.BitDepth_Y;
4168 }
4169
4170 pixel_t* ptr;
4171 int stride;
4172 ptr = tctx->img->get_image_plane_at_pos_NEW<pixel_t>(cIdx,x0,y0);
4173 stride = tctx->img->get_image_stride(cIdx);
4174
4175 int shift = bitDepth - nPcmBits;
4176
4177 for (int y=0;y<h;y++)
4178 for (int x=0;x<w;x++)
4179 {
4180 int value = get_bits(&br, nPcmBits);
4181 ptr[y*stride+x] = value << shift;
4182 }
4183 }
4184
read_pcm_samples(thread_context * tctx,int x0,int y0,int log2CbSize)4185 static void read_pcm_samples(thread_context* tctx, int x0, int y0, int log2CbSize)
4186 {
4187 bitreader br;
4188 br.data = tctx->cabac_decoder.bitstream_curr;
4189 br.bytes_remaining = tctx->cabac_decoder.bitstream_end - tctx->cabac_decoder.bitstream_curr;
4190 br.nextbits = 0;
4191 br.nextbits_cnt = 0;
4192
4193
4194 if (tctx->img->high_bit_depth(0)) {
4195 read_pcm_samples_internal<uint16_t>(tctx,x0,y0,log2CbSize,0,br);
4196 } else {
4197 read_pcm_samples_internal<uint8_t>(tctx,x0,y0,log2CbSize,0,br);
4198 }
4199
4200 if (tctx->img->get_sps().ChromaArrayType != CHROMA_MONO) {
4201 if (tctx->img->high_bit_depth(1)) {
4202 read_pcm_samples_internal<uint16_t>(tctx,x0,y0,log2CbSize,1,br);
4203 read_pcm_samples_internal<uint16_t>(tctx,x0,y0,log2CbSize,2,br);
4204 } else {
4205 read_pcm_samples_internal<uint8_t>(tctx,x0,y0,log2CbSize,1,br);
4206 read_pcm_samples_internal<uint8_t>(tctx,x0,y0,log2CbSize,2,br);
4207 }
4208 }
4209
4210 prepare_for_CABAC(&br);
4211 tctx->cabac_decoder.bitstream_curr = br.data;
4212 init_CABAC_decoder_2(&tctx->cabac_decoder);
4213 }
4214
4215
map_chroma_pred_mode(int intra_chroma_pred_mode,int IntraPredMode)4216 int map_chroma_pred_mode(int intra_chroma_pred_mode, int IntraPredMode)
4217 {
4218 if (intra_chroma_pred_mode==4) {
4219 return IntraPredMode;
4220 }
4221 else {
4222 static const enum IntraPredMode IntraPredModeCCand[4] = {
4223 INTRA_PLANAR,
4224 INTRA_ANGULAR_26, // vertical
4225 INTRA_ANGULAR_10, // horizontal
4226 INTRA_DC
4227 };
4228
4229 int IntraPredModeC = IntraPredModeCCand[intra_chroma_pred_mode];
4230 if (IntraPredModeC == IntraPredMode) {
4231 return INTRA_ANGULAR_34;
4232 }
4233 else {
4234 return IntraPredModeC;
4235 }
4236 }
4237 }
4238
4239 // h.265-V2 Table 8-3
4240 static const uint8_t map_chroma_422[35] = {
4241 0,1,2, 2, 2, 2, 3, 5, 7, 8,10,12,13,15,17,18,19,20,
4242 21,22,23,23,24,24,25,25,26,27,27,28,28,29,29,30,31
4243 };
4244
read_coding_unit(thread_context * tctx,int x0,int y0,int log2CbSize,int ctDepth)4245 void read_coding_unit(thread_context* tctx,
4246 int x0, int y0, // position of coding unit in frame
4247 int log2CbSize,
4248 int ctDepth)
4249 {
4250 de265_image* img = tctx->img;
4251 const seq_parameter_set& sps = img->get_sps();
4252 const pic_parameter_set& pps = img->get_pps();
4253 slice_segment_header* shdr = tctx->shdr;
4254
4255 logtrace(LogSlice,"- read_coding_unit %d;%d cbsize:%d\n",x0,y0,1<<log2CbSize);
4256
4257
4258 //QQprintf("- read_coding_unit %d;%d cbsize:%d\n",x0,y0,1<<log2CbSize);
4259
4260 img->set_log2CbSize(x0,y0, log2CbSize, true);
4261
4262 /* This is only required on corrupted input streams.
4263 It may happen that there are several slices in the image that overlap.
4264 In this case, flags would accumulate from both slices.
4265 */
4266 img->clear_split_transform_flags(x0,y0, log2CbSize);
4267
4268 int nCbS = 1<<log2CbSize; // number of coding block samples
4269
4270 decode_quantization_parameters(tctx, x0,y0, x0, y0);
4271
4272
4273 if (pps.transquant_bypass_enable_flag)
4274 {
4275 int transquant_bypass = decode_transquant_bypass_flag(tctx);
4276
4277 tctx->cu_transquant_bypass_flag = transquant_bypass;
4278
4279 if (transquant_bypass) {
4280 img->set_cu_transquant_bypass(x0,y0,log2CbSize);
4281 }
4282 }
4283 else {
4284 tctx->cu_transquant_bypass_flag = 0;
4285 }
4286
4287 uint8_t cu_skip_flag = 0;
4288 if (shdr->slice_type != SLICE_TYPE_I) {
4289 cu_skip_flag = decode_cu_skip_flag(tctx,x0,y0,ctDepth);
4290 }
4291
4292 int IntraSplitFlag = 0;
4293
4294 enum PredMode cuPredMode;
4295
4296 if (cu_skip_flag) {
4297 read_prediction_unit_SKIP(tctx,x0,y0,nCbS,nCbS);
4298
4299 img->set_PartMode(x0,y0, PART_2Nx2N); // need this for deblocking filter
4300 img->set_pred_mode(x0,y0,log2CbSize, MODE_SKIP);
4301 cuPredMode = MODE_SKIP;
4302
4303 logtrace(LogSlice,"CU pred mode: SKIP\n");
4304
4305
4306 // DECODE
4307
4308 int nCS_L = 1<<log2CbSize;
4309 decode_prediction_unit(tctx->decctx,tctx->shdr,tctx->img,tctx->motion,
4310 x0,y0, 0,0, nCS_L, nCS_L,nCS_L, 0);
4311 }
4312 else /* not skipped */ {
4313 if (shdr->slice_type != SLICE_TYPE_I) {
4314 int pred_mode_flag = decode_pred_mode_flag(tctx);
4315 cuPredMode = pred_mode_flag ? MODE_INTRA : MODE_INTER;
4316 }
4317 else {
4318 cuPredMode = MODE_INTRA;
4319 }
4320
4321 img->set_pred_mode(x0,y0,log2CbSize, cuPredMode);
4322
4323 logtrace(LogSlice,"CU pred mode: %s\n", cuPredMode==MODE_INTRA ? "INTRA" : "INTER");
4324
4325
4326 enum PartMode PartMode;
4327
4328 if (cuPredMode != MODE_INTRA ||
4329 log2CbSize == sps.Log2MinCbSizeY) {
4330 PartMode = decode_part_mode(tctx, cuPredMode, log2CbSize);
4331
4332 if (PartMode==PART_NxN && cuPredMode==MODE_INTRA) {
4333 IntraSplitFlag=1;
4334 }
4335 } else {
4336 PartMode = PART_2Nx2N;
4337 }
4338
4339 img->set_PartMode(x0,y0, PartMode); // needed for deblocking ?
4340
4341 logtrace(LogSlice, "PartMode: %s\n", part_mode_name(PartMode));
4342
4343
4344 bool pcm_flag = false;
4345
4346 if (cuPredMode == MODE_INTRA) {
4347 if (PartMode == PART_2Nx2N && sps.pcm_enabled_flag &&
4348 log2CbSize >= sps.Log2MinIpcmCbSizeY &&
4349 log2CbSize <= sps.Log2MaxIpcmCbSizeY) {
4350 pcm_flag = decode_CABAC_term_bit(&tctx->cabac_decoder);
4351 }
4352
4353 if (pcm_flag) {
4354 img->set_pcm_flag(x0,y0,log2CbSize);
4355
4356 read_pcm_samples(tctx, x0,y0, log2CbSize);
4357 }
4358 else {
4359 int pbOffset = (PartMode == PART_NxN) ? (nCbS/2) : nCbS;
4360 int log2IntraPredSize = (PartMode == PART_NxN) ? (log2CbSize-1) : log2CbSize;
4361
4362 logtrace(LogSlice,"nCbS:%d pbOffset:%d\n",nCbS,pbOffset);
4363
4364 int prev_intra_luma_pred_flag[4];
4365
4366 int idx=0;
4367 for (int j=0;j<nCbS;j+=pbOffset)
4368 for (int i=0;i<nCbS;i+=pbOffset)
4369 {
4370 prev_intra_luma_pred_flag[idx++] = decode_prev_intra_luma_pred_flag(tctx);
4371 }
4372
4373 int mpm_idx[4], rem_intra_luma_pred_mode[4];
4374 idx=0;
4375
4376 int availableA0 = check_CTB_available(img, x0,y0, x0-1,y0);
4377 int availableB0 = check_CTB_available(img, x0,y0, x0,y0-1);
4378
4379 for (int j=0;j<nCbS;j+=pbOffset)
4380 for (int i=0;i<nCbS;i+=pbOffset)
4381 {
4382 if (prev_intra_luma_pred_flag[idx]) {
4383 mpm_idx[idx] = decode_mpm_idx(tctx);
4384 }
4385 else {
4386 rem_intra_luma_pred_mode[idx] = decode_rem_intra_luma_pred_mode(tctx);
4387 }
4388
4389
4390 int x = x0+i;
4391 int y = y0+j;
4392
4393 // --- find intra prediction mode ---
4394
4395 int IntraPredMode;
4396
4397 int availableA = availableA0 || (i>0); // left candidate always available for right blk
4398 int availableB = availableB0 || (j>0); // top candidate always available for bottom blk
4399
4400
4401
4402 int PUidx = (x>>sps.Log2MinPUSize) + (y>>sps.Log2MinPUSize)*sps.PicWidthInMinPUs;
4403
4404 enum IntraPredMode candModeList[3];
4405
4406 fillIntraPredModeCandidates(candModeList,x,y,PUidx,
4407 availableA, availableB, img);
4408
4409 for (int i=0;i<3;i++)
4410 logtrace(LogSlice,"candModeList[%d] = %d\n", i, candModeList[i]);
4411
4412 if (prev_intra_luma_pred_flag[idx]==1) {
4413 IntraPredMode = candModeList[ mpm_idx[idx] ];
4414 }
4415 else {
4416 // sort candModeList
4417
4418 if (candModeList[0] > candModeList[1]) {
4419 std::swap(candModeList[0],candModeList[1]);
4420 }
4421 if (candModeList[0] > candModeList[2]) {
4422 std::swap(candModeList[0],candModeList[2]);
4423 }
4424 if (candModeList[1] > candModeList[2]) {
4425 std::swap(candModeList[1],candModeList[2]);
4426 }
4427
4428 // skip modes in the list
4429 // (we have 35 modes. skipping the 3 in the list gives us 32, which can be selected by 5 bits)
4430 IntraPredMode = rem_intra_luma_pred_mode[idx];
4431 for (int n=0;n<=2;n++) {
4432 if (IntraPredMode >= candModeList[n]) { IntraPredMode++; }
4433 }
4434 }
4435
4436 logtrace(LogSlice,"IntraPredMode[%d][%d] = %d (log2blk:%d)\n",x,y,IntraPredMode, log2IntraPredSize);
4437
4438 img->set_IntraPredMode(PUidx, log2IntraPredSize,
4439 (enum IntraPredMode)IntraPredMode);
4440
4441 idx++;
4442 }
4443
4444
4445 // set chroma intra prediction mode
4446
4447 if (sps.ChromaArrayType == CHROMA_444) {
4448 // chroma 4:4:4
4449
4450 idx = 0;
4451 for (int j=0;j<nCbS;j+=pbOffset)
4452 for (int i=0;i<nCbS;i+=pbOffset) {
4453 int x = x0+i;
4454 int y = y0+j;
4455
4456 int intra_chroma_pred_mode = decode_intra_chroma_pred_mode(tctx);
4457 int IntraPredMode = img->get_IntraPredMode(x,y);
4458
4459 int IntraPredModeC = map_chroma_pred_mode(intra_chroma_pred_mode, IntraPredMode);
4460
4461 logtrace(LogSlice,"IntraPredModeC[%d][%d]: %d (blksize:%d)\n",x,y,IntraPredModeC,
4462 1<<log2IntraPredSize);
4463
4464 img->set_IntraPredModeC(x,y, log2IntraPredSize,
4465 (enum IntraPredMode)IntraPredModeC,
4466 intra_chroma_pred_mode == 4);
4467 idx++;
4468 }
4469 }
4470 else if (sps.ChromaArrayType != CHROMA_MONO) {
4471 // chroma 4:2:0 and 4:2:2
4472
4473 int intra_chroma_pred_mode = decode_intra_chroma_pred_mode(tctx);
4474 int IntraPredMode = img->get_IntraPredMode(x0,y0);
4475 logtrace(LogSlice,"IntraPredMode: %d\n",IntraPredMode);
4476 int IntraPredModeC = map_chroma_pred_mode(intra_chroma_pred_mode, IntraPredMode);
4477
4478 if (sps.ChromaArrayType == CHROMA_422) {
4479 IntraPredModeC = map_chroma_422[ IntraPredModeC ];
4480 }
4481
4482 img->set_IntraPredModeC(x0,y0, log2CbSize,
4483 (enum IntraPredMode)IntraPredModeC,
4484 intra_chroma_pred_mode == 4);
4485 }
4486 }
4487 }
4488 else { // INTER
4489 int nCS = 1<<log2CbSize;
4490
4491 if (PartMode == PART_2Nx2N) {
4492 read_prediction_unit(tctx,x0,y0,0,0,nCbS,nCbS,ctDepth,nCS,0);
4493 }
4494 else if (PartMode == PART_2NxN) {
4495 read_prediction_unit(tctx,x0,y0,0,0 ,nCbS,nCbS/2,ctDepth,nCS,0);
4496 read_prediction_unit(tctx,x0,y0,0,nCbS/2,nCbS,nCbS/2,ctDepth,nCS,1);
4497 }
4498 else if (PartMode == PART_Nx2N) {
4499 read_prediction_unit(tctx,x0,y0,0,0 , nCbS/2,nCbS,ctDepth,nCS,0);
4500 read_prediction_unit(tctx,x0,y0,nCbS/2,0,nCbS/2,nCbS,ctDepth,nCS,1);
4501 }
4502 else if (PartMode == PART_2NxnU) {
4503 read_prediction_unit(tctx,x0,y0,0,0, nCbS,nCbS/4,ctDepth,nCS,0);
4504 read_prediction_unit(tctx,x0,y0,0,nCbS/4,nCbS,nCbS*3/4,ctDepth,nCS,1);
4505 }
4506 else if (PartMode == PART_2NxnD) {
4507 read_prediction_unit(tctx,x0,y0,0,0, nCbS,nCbS*3/4,ctDepth,nCS,0);
4508 read_prediction_unit(tctx,x0,y0,0,nCbS*3/4,nCbS,nCbS/4,ctDepth,nCS,1);
4509 }
4510 else if (PartMode == PART_nLx2N) {
4511 read_prediction_unit(tctx,x0,y0,0,0, nCbS/4,nCbS,ctDepth,nCS,0);
4512 read_prediction_unit(tctx,x0,y0,nCbS/4,0,nCbS*3/4,nCbS,ctDepth,nCS,1);
4513 }
4514 else if (PartMode == PART_nRx2N) {
4515 read_prediction_unit(tctx,x0,y0,0,0, nCbS*3/4,nCbS,ctDepth,nCS,0);
4516 read_prediction_unit(tctx,x0,y0,nCbS*3/4,0,nCbS/4,nCbS,ctDepth,nCS,1);
4517 }
4518 else if (PartMode == PART_NxN) {
4519 read_prediction_unit(tctx,x0,y0,0,0, nCbS/2,nCbS/2,ctDepth,nCS,0);
4520 read_prediction_unit(tctx,x0,y0,nCbS/2,0, nCbS/2,nCbS/2,ctDepth,nCS,1);
4521 read_prediction_unit(tctx,x0,y0,0,nCbS/2, nCbS/2,nCbS/2,ctDepth,nCS,2);
4522 read_prediction_unit(tctx,x0,y0,nCbS/2,nCbS/2,nCbS/2,nCbS/2,ctDepth,nCS,3);
4523 }
4524 else {
4525 assert(0); // undefined PartMode
4526 }
4527 } // INTER
4528
4529
4530 // decode residual
4531
4532 if (!pcm_flag) { // !pcm
4533 bool rqt_root_cbf;
4534
4535 uint8_t merge_flag = tctx->motion.merge_flag; // !!get_merge_flag(ctx,x0,y0);
4536
4537 if (cuPredMode != MODE_INTRA &&
4538 !(PartMode == PART_2Nx2N && merge_flag)) {
4539
4540 rqt_root_cbf = !!decode_rqt_root_cbf(tctx);
4541 }
4542 else {
4543 /* rqt_root_cbf=1 is inferred for Inter blocks with 2Nx2N, merge mode.
4544 These must be some residual data, because otherwise, the CB could
4545 also be coded in SKIP mode.
4546 */
4547
4548 rqt_root_cbf = true;
4549 }
4550
4551 //set_rqt_root_cbf(ctx,x0,y0, log2CbSize, rqt_root_cbf);
4552
4553 if (rqt_root_cbf) {
4554 int MaxTrafoDepth;
4555
4556 if (cuPredMode==MODE_INTRA) {
4557 MaxTrafoDepth = sps.max_transform_hierarchy_depth_intra + IntraSplitFlag;
4558 }
4559 else {
4560 MaxTrafoDepth = sps.max_transform_hierarchy_depth_inter;
4561 }
4562
4563 logtrace(LogSlice,"MaxTrafoDepth: %d\n",MaxTrafoDepth);
4564
4565 uint8_t initial_chroma_cbf = 1;
4566 if (sps.ChromaArrayType == CHROMA_MONO) {
4567 initial_chroma_cbf = 0;
4568 }
4569
4570 read_transform_tree(tctx, x0,y0, x0,y0, x0,y0, log2CbSize, 0,0,
4571 MaxTrafoDepth, IntraSplitFlag, cuPredMode,
4572 initial_chroma_cbf, initial_chroma_cbf);
4573 }
4574 } // !pcm
4575 }
4576 }
4577
4578
4579 // ------------------------------------------------------------------------------------------
4580
4581
read_coding_quadtree(thread_context * tctx,int x0,int y0,int log2CbSize,int ctDepth)4582 void read_coding_quadtree(thread_context* tctx,
4583 int x0, int y0,
4584 int log2CbSize,
4585 int ctDepth)
4586 {
4587 logtrace(LogSlice,"- read_coding_quadtree %d;%d cbsize:%d depth:%d POC:%d\n",x0,y0,1<<log2CbSize,ctDepth,tctx->img->PicOrderCntVal);
4588
4589 de265_image* img = tctx->img;
4590 const seq_parameter_set& sps = img->get_sps();
4591 const pic_parameter_set& pps = img->get_pps();
4592
4593 int split_flag;
4594
4595 // We only send a split flag if CU is larger than minimum size and
4596 // completely contained within the image area.
4597 // If it is partly outside the image area and not at minimum size,
4598 // it is split. If already at minimum size, it is not split further.
4599 if (x0+(1<<log2CbSize) <= sps.pic_width_in_luma_samples &&
4600 y0+(1<<log2CbSize) <= sps.pic_height_in_luma_samples &&
4601 log2CbSize > sps.Log2MinCbSizeY) {
4602 split_flag = decode_split_cu_flag(tctx, x0,y0, ctDepth);
4603 } else {
4604 if (log2CbSize > sps.Log2MinCbSizeY) { split_flag=1; }
4605 else { split_flag=0; }
4606 }
4607
4608
4609 if (pps.cu_qp_delta_enabled_flag &&
4610 log2CbSize >= pps.Log2MinCuQpDeltaSize)
4611 {
4612 tctx->IsCuQpDeltaCoded = 0;
4613 tctx->CuQpDelta = 0;
4614 }
4615 else
4616 {
4617 // shdr->CuQpDelta = 0; // TODO check: is this the right place to set to default value ?
4618 }
4619
4620
4621 if (tctx->shdr->cu_chroma_qp_offset_enabled_flag &&
4622 log2CbSize >= pps.Log2MinCuChromaQpOffsetSize) {
4623 tctx->IsCuChromaQpOffsetCoded = 0;
4624 }
4625
4626 if (split_flag) {
4627 int x1 = x0 + (1<<(log2CbSize-1));
4628 int y1 = y0 + (1<<(log2CbSize-1));
4629
4630 read_coding_quadtree(tctx,x0,y0, log2CbSize-1, ctDepth+1);
4631
4632 if (x1<sps.pic_width_in_luma_samples)
4633 read_coding_quadtree(tctx,x1,y0, log2CbSize-1, ctDepth+1);
4634
4635 if (y1<sps.pic_height_in_luma_samples)
4636 read_coding_quadtree(tctx,x0,y1, log2CbSize-1, ctDepth+1);
4637
4638 if (x1<sps.pic_width_in_luma_samples &&
4639 y1<sps.pic_height_in_luma_samples)
4640 read_coding_quadtree(tctx,x1,y1, log2CbSize-1, ctDepth+1);
4641 }
4642 else {
4643 // set ctDepth of this CU
4644
4645 img->set_ctDepth(x0,y0, log2CbSize, ctDepth);
4646
4647 read_coding_unit(tctx, x0,y0, log2CbSize, ctDepth);
4648 }
4649
4650 logtrace(LogSlice,"-\n");
4651 }
4652
4653
4654 // ---------------------------------------------------------------------------
4655
4656 enum DecodeResult {
4657 Decode_EndOfSliceSegment,
4658 Decode_EndOfSubstream,
4659 Decode_Error
4660 };
4661
4662 /* Decode CTBs until the end of sub-stream, the end-of-slice, or some error occurs.
4663 */
decode_substream(thread_context * tctx,bool block_wpp,bool first_independent_substream)4664 enum DecodeResult decode_substream(thread_context* tctx,
4665 bool block_wpp, // block on WPP dependencies
4666 bool first_independent_substream)
4667 {
4668 const pic_parameter_set& pps = tctx->img->get_pps();
4669 const seq_parameter_set& sps = tctx->img->get_sps();
4670
4671 const int ctbW = sps.PicWidthInCtbsY;
4672
4673
4674 const int startCtbY = tctx->CtbY;
4675
4676 //printf("start decoding substream at %d;%d\n",tctx->CtbX,tctx->CtbY);
4677
4678 // in WPP mode: initialize CABAC model with stored model from row above
4679
4680 if ((!first_independent_substream || tctx->CtbY != startCtbY) &&
4681 pps.entropy_coding_sync_enabled_flag &&
4682 tctx->CtbY>=1 && tctx->CtbX==0)
4683 {
4684 if (sps.PicWidthInCtbsY>1) {
4685 if ((tctx->CtbY-1) >= tctx->imgunit->ctx_models.size()) {
4686 return Decode_Error;
4687 }
4688
4689 //printf("CTX wait on %d/%d\n",1,tctx->CtbY-1);
4690
4691 // we have to wait until the context model data is there
4692 tctx->img->wait_for_progress(tctx->task, 1,tctx->CtbY-1,CTB_PROGRESS_PREFILTER);
4693
4694 // copy CABAC model from previous CTB row
4695 tctx->ctx_model = tctx->imgunit->ctx_models[(tctx->CtbY-1)];
4696 tctx->imgunit->ctx_models[(tctx->CtbY-1)].release(); // not used anymore
4697 }
4698 else {
4699 tctx->img->wait_for_progress(tctx->task, 0,tctx->CtbY-1,CTB_PROGRESS_PREFILTER);
4700 initialize_CABAC_models(tctx);
4701 }
4702 }
4703
4704
4705 do {
4706 const int ctbx = tctx->CtbX;
4707 const int ctby = tctx->CtbY;
4708
4709 if (ctbx+ctby*ctbW >= pps.CtbAddrRStoTS.size()) {
4710 return Decode_Error;
4711 }
4712
4713 if (ctbx >= sps.PicWidthInCtbsY ||
4714 ctby >= sps.PicHeightInCtbsY) {
4715 return Decode_Error;
4716 }
4717
4718 if (block_wpp && ctby>0 && ctbx < ctbW-1) {
4719
4720 // TODO: if we are in tiles mode and at the right border, do not wait for x+1,y-1
4721
4722 //printf("wait on %d/%d (%d)\n",ctbx+1,ctby-1, ctbx+1+(ctby-1)*sps->PicWidthInCtbsY);
4723
4724 tctx->img->wait_for_progress(tctx->task, ctbx+1,ctby-1, CTB_PROGRESS_PREFILTER);
4725 }
4726
4727 //printf("%p: decode %d;%d\n", tctx, tctx->CtbX,tctx->CtbY);
4728
4729
4730 // read and decode CTB
4731
4732 if (tctx->ctx_model.empty() == false) {
4733 return Decode_Error;
4734 }
4735
4736 read_coding_tree_unit(tctx);
4737
4738
4739 // save CABAC-model for WPP (except in last CTB row)
4740
4741 if (pps.entropy_coding_sync_enabled_flag &&
4742 ctbx == 1 &&
4743 ctby < sps.PicHeightInCtbsY-1)
4744 {
4745 // no storage for context table has been allocated
4746 if (tctx->imgunit->ctx_models.size() <= ctby) {
4747 return Decode_Error;
4748 }
4749
4750 tctx->imgunit->ctx_models[ctby] = tctx->ctx_model;
4751 tctx->imgunit->ctx_models[ctby].decouple(); // store an independent copy
4752 }
4753
4754
4755 // end of slice segment ?
4756
4757 int end_of_slice_segment_flag = decode_CABAC_term_bit(&tctx->cabac_decoder);
4758 //printf("end-of-slice flag: %d\n", end_of_slice_segment_flag);
4759
4760 if (end_of_slice_segment_flag) {
4761 // at the end of the slice segment, we store the CABAC model if we need it
4762 // because a dependent slice may follow
4763
4764 if (pps.dependent_slice_segments_enabled_flag) {
4765 tctx->shdr->ctx_model_storage = tctx->ctx_model;
4766 tctx->shdr->ctx_model_storage.decouple(); // store an independent copy
4767
4768 tctx->shdr->ctx_model_storage_defined = true;
4769 }
4770 }
4771
4772 tctx->img->ctb_progress[ctbx+ctby*ctbW].set_progress(CTB_PROGRESS_PREFILTER);
4773
4774 //printf("%p: decoded %d|%d\n",tctx, ctby,ctbx);
4775
4776
4777 logtrace(LogSlice,"read CTB %d -> end=%d\n", tctx->CtbAddrInRS, end_of_slice_segment_flag);
4778 //printf("read CTB %d -> end=%d\n", tctx->CtbAddrInRS, end_of_slice_segment_flag);
4779
4780 const int lastCtbY = tctx->CtbY;
4781
4782 bool endOfPicture = advanceCtbAddr(tctx); // true if we read past the end of the image
4783
4784 if (endOfPicture &&
4785 end_of_slice_segment_flag == false)
4786 {
4787 tctx->decctx->add_warning(DE265_WARNING_CTB_OUTSIDE_IMAGE_AREA, false);
4788 tctx->img->integrity = INTEGRITY_DECODING_ERRORS;
4789 return Decode_Error;
4790 }
4791
4792
4793 if (end_of_slice_segment_flag) {
4794 /* corrupted inputs may send the end_of_slice_segment_flag even if not all
4795 CTBs in a row have been coded. Hence, we mark all of them as finished.
4796 */
4797
4798 /*
4799 for (int x = ctbx+1 ; x<sps->PicWidthInCtbsY; x++) {
4800 printf("mark skipped %d;%d\n",ctbx,ctby);
4801 tctx->img->ctb_progress[ctbx+ctby*ctbW].set_progress(CTB_PROGRESS_PREFILTER);
4802 }
4803 */
4804
4805 return Decode_EndOfSliceSegment;
4806 }
4807
4808
4809 if (!end_of_slice_segment_flag) {
4810 bool end_of_sub_stream = false;
4811 end_of_sub_stream |= (pps.tiles_enabled_flag &&
4812 pps.TileId[tctx->CtbAddrInTS] != pps.TileId[tctx->CtbAddrInTS-1]);
4813 end_of_sub_stream |= (pps.entropy_coding_sync_enabled_flag &&
4814 lastCtbY != tctx->CtbY);
4815
4816 if (end_of_sub_stream) {
4817 int end_of_sub_stream_one_bit = decode_CABAC_term_bit(&tctx->cabac_decoder);
4818 if (!end_of_sub_stream_one_bit) {
4819 tctx->decctx->add_warning(DE265_WARNING_EOSS_BIT_NOT_SET, false);
4820 tctx->img->integrity = INTEGRITY_DECODING_ERRORS;
4821 return Decode_Error;
4822 }
4823
4824 init_CABAC_decoder_2(&tctx->cabac_decoder); // byte alignment
4825 return Decode_EndOfSubstream;
4826 }
4827 }
4828
4829 } while (true);
4830 }
4831
4832
4833
initialize_CABAC_at_slice_segment_start(thread_context * tctx)4834 bool initialize_CABAC_at_slice_segment_start(thread_context* tctx)
4835 {
4836 de265_image* img = tctx->img;
4837 const pic_parameter_set& pps = img->get_pps();
4838 const seq_parameter_set& sps = img->get_sps();
4839 slice_segment_header* shdr = tctx->shdr;
4840
4841 if (shdr->dependent_slice_segment_flag) {
4842 int prevCtb = pps.CtbAddrTStoRS[ pps.CtbAddrRStoTS[shdr->slice_segment_address] -1 ];
4843
4844 int sliceIdx = img->get_SliceHeaderIndex_atIndex(prevCtb);
4845 if (sliceIdx >= img->slices.size()) {
4846 return false;
4847 }
4848 slice_segment_header* prevCtbHdr = img->slices[ sliceIdx ];
4849
4850 if (pps.is_tile_start_CTB(shdr->slice_segment_address % sps.PicWidthInCtbsY,
4851 shdr->slice_segment_address / sps.PicWidthInCtbsY
4852 )) {
4853 initialize_CABAC_models(tctx);
4854 }
4855 else {
4856 // wait for previous slice to finish decoding
4857
4858 //printf("wait for previous slice to finish decoding\n");
4859
4860
4861 slice_unit* prevSliceSegment = tctx->imgunit->get_prev_slice_segment(tctx->sliceunit);
4862 //assert(prevSliceSegment);
4863 if (prevSliceSegment==NULL) {
4864 return false;
4865 }
4866
4867 prevSliceSegment->finished_threads.wait_for_progress(prevSliceSegment->nThreads);
4868
4869
4870 /*
4871 printf("wait for %d,%d (init)\n",
4872 prevCtb / sps->PicWidthInCtbsY,
4873 prevCtb % sps->PicWidthInCtbsY);
4874 tctx->img->wait_for_progress(tctx->task, prevCtb, CTB_PROGRESS_PREFILTER);
4875 */
4876
4877 if (!prevCtbHdr->ctx_model_storage_defined) {
4878 return false;
4879 }
4880
4881 tctx->ctx_model = prevCtbHdr->ctx_model_storage;
4882 prevCtbHdr->ctx_model_storage.release();
4883 }
4884 }
4885 else {
4886 initialize_CABAC_models(tctx);
4887 }
4888
4889 return true;
4890 }
4891
4892
name() const4893 std::string thread_task_ctb_row::name() const {
4894 char buf[100];
4895 sprintf(buf,"ctb-row-%d",debug_startCtbRow);
4896 return buf;
4897 }
4898
4899
name() const4900 std::string thread_task_slice_segment::name() const {
4901 char buf[100];
4902 sprintf(buf,"slice-segment-%d;%d",debug_startCtbX,debug_startCtbY);
4903 return buf;
4904 }
4905
4906
work()4907 void thread_task_slice_segment::work()
4908 {
4909 thread_task_slice_segment* data = this;
4910 thread_context* tctx = data->tctx;
4911 de265_image* img = tctx->img;
4912
4913 state = Running;
4914 img->thread_run(this);
4915
4916 setCtbAddrFromTS(tctx);
4917
4918 //printf("%p: A start decoding at %d/%d\n", tctx, tctx->CtbX,tctx->CtbY);
4919
4920 if (data->firstSliceSubstream) {
4921 bool success = initialize_CABAC_at_slice_segment_start(tctx);
4922 if (!success) {
4923 state = Finished;
4924 tctx->sliceunit->finished_threads.increase_progress(1);
4925 img->thread_finishes(this);
4926 return;
4927 }
4928 }
4929 else {
4930 initialize_CABAC_models(tctx);
4931 }
4932
4933 init_CABAC_decoder_2(&tctx->cabac_decoder);
4934
4935 /*enum DecodeResult result =*/ decode_substream(tctx, false, data->firstSliceSubstream);
4936
4937 state = Finished;
4938 tctx->sliceunit->finished_threads.increase_progress(1);
4939 img->thread_finishes(this);
4940
4941 return; // DE265_OK;
4942 }
4943
4944
work()4945 void thread_task_ctb_row::work()
4946 {
4947 thread_task_ctb_row* data = this;
4948 thread_context* tctx = data->tctx;
4949 de265_image* img = tctx->img;
4950
4951 const seq_parameter_set& sps = img->get_sps();
4952 int ctbW = sps.PicWidthInCtbsY;
4953
4954 state = Running;
4955 img->thread_run(this);
4956
4957 setCtbAddrFromTS(tctx);
4958
4959 int ctby = tctx->CtbAddrInRS / ctbW;
4960 int myCtbRow = ctby;
4961
4962 //printf("start CTB-row decoding at row %d\n", ctby);
4963
4964 if (data->firstSliceSubstream) {
4965 bool success = initialize_CABAC_at_slice_segment_start(tctx);
4966 if (!success) {
4967 // could not decode this row, mark whole row as finished
4968 for (int x=0;x<ctbW;x++) {
4969 img->ctb_progress[myCtbRow*ctbW + x].set_progress(CTB_PROGRESS_PREFILTER);
4970 }
4971
4972 state = Finished;
4973 tctx->sliceunit->finished_threads.increase_progress(1);
4974 img->thread_finishes(this);
4975 return;
4976 }
4977 //initialize_CABAC(tctx);
4978 }
4979
4980 init_CABAC_decoder_2(&tctx->cabac_decoder);
4981
4982 bool firstIndependentSubstream =
4983 data->firstSliceSubstream && !tctx->shdr->dependent_slice_segment_flag;
4984
4985 /*enum DecodeResult result =*/
4986 decode_substream(tctx, true, firstIndependentSubstream);
4987
4988 // mark progress on remaining CTBs in row (in case of decoder error and early termination)
4989
4990 // TODO: what about slices that end properly in the middle of a CTB row?
4991
4992 if (tctx->CtbY == myCtbRow) {
4993 int lastCtbX = sps.PicWidthInCtbsY; // assume no tiles when WPP is on
4994 for (int x = tctx->CtbX; x<lastCtbX ; x++) {
4995
4996 if (x < sps.PicWidthInCtbsY &&
4997 myCtbRow < sps.PicHeightInCtbsY) {
4998 img->ctb_progress[myCtbRow*ctbW + x].set_progress(CTB_PROGRESS_PREFILTER);
4999 }
5000 }
5001 }
5002
5003 state = Finished;
5004 tctx->sliceunit->finished_threads.increase_progress(1);
5005 img->thread_finishes(this);
5006 }
5007
5008
read_slice_segment_data(thread_context * tctx)5009 de265_error read_slice_segment_data(thread_context* tctx)
5010 {
5011 setCtbAddrFromTS(tctx);
5012
5013 de265_image* img = tctx->img;
5014 const pic_parameter_set& pps = img->get_pps();
5015 const seq_parameter_set& sps = img->get_sps();
5016 slice_segment_header* shdr = tctx->shdr;
5017
5018 bool success = initialize_CABAC_at_slice_segment_start(tctx);
5019 if (!success) {
5020 return DE265_ERROR_UNSPECIFIED_DECODING_ERROR;
5021 }
5022
5023 init_CABAC_decoder_2(&tctx->cabac_decoder);
5024
5025 //printf("-----\n");
5026
5027 bool first_slice_substream = !shdr->dependent_slice_segment_flag;
5028
5029 int substream=0;
5030
5031 enum DecodeResult result;
5032 do {
5033 int ctby = tctx->CtbY;
5034
5035
5036 // check whether entry_points[] are correct in the bitstream
5037
5038 if (substream>0) {
5039 if (substream-1 >= tctx->shdr->entry_point_offset.size() ||
5040 tctx->cabac_decoder.bitstream_curr - tctx->cabac_decoder.bitstream_start -2 /* -2 because of CABAC init */
5041 != tctx->shdr->entry_point_offset[substream-1]) {
5042 tctx->decctx->add_warning(DE265_WARNING_INCORRECT_ENTRY_POINT_OFFSET, true);
5043 }
5044 }
5045
5046 substream++;
5047
5048
5049 result = decode_substream(tctx, false, first_slice_substream);
5050
5051
5052 if (result == Decode_EndOfSliceSegment ||
5053 result == Decode_Error) {
5054 break;
5055 }
5056
5057 first_slice_substream = false;
5058
5059 if (pps.tiles_enabled_flag) {
5060 initialize_CABAC_models(tctx);
5061 }
5062 } while (true);
5063
5064 return DE265_OK;
5065 }
5066
5067
5068 /* TODO:
5069 When a task wants to block, but is the first in the list of pending tasks,
5070 do some error concealment instead of blocking, since it will never be deblocked.
5071 This will only happen in the case of input error.
5072 */
5073