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