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 #ifndef DE265_SLICE_H
25 #define DE265_SLICE_H
26 
27 #include "libde265/cabac.h"
28 #include "libde265/de265.h"
29 #include "libde265/util.h"
30 #include "libde265/refpic.h"
31 #include "libde265/threads.h"
32 #include "contextmodel.h"
33 
34 #include <vector>
35 #include <string.h>
36 
37 #define MAX_NUM_REF_PICS    16
38 
39 class decoder_context;
40 class thread_context;
41 class error_queue;
42 class seq_parameter_set;
43 class pic_parameter_set;
44 
45 enum SliceType
46   {
47     SLICE_TYPE_B = 0,
48     SLICE_TYPE_P = 1,
49     SLICE_TYPE_I = 2
50   };
51 
52 /*
53         2Nx2N           2NxN             Nx2N            NxN
54       +-------+       +-------+       +---+---+       +---+---+
55       |       |       |       |       |   |   |       |   |   |
56       |       |       |_______|       |   |   |       |___|___|
57       |       |       |       |       |   |   |       |   |   |
58       |       |       |       |       |   |   |       |   |   |
59       +-------+       +-------+       +---+---+       +---+---+
60 
61         2NxnU           2NxnD           nLx2N           nRx2N
62       +-------+       +-------+       +-+-----+       +-----+-+
63       |_______|       |       |       | |     |       |     | |
64       |       |       |       |       | |     |       |     | |
65       |       |       |_______|       | |     |       |     | |
66       |       |       |       |       | |     |       |     | |
67       +-------+       +-------+       +-+-----+       +-----+-+
68 
69       - AMP only if CU size > min CU size -> minimum PU size = CUsize/2
70       - NxN only if size >= 16x16 (-> minimum block size = 8x8)
71       - minimum block size for Bi-Pred is 8x8 (wikipedia: Coding_tree_unit)
72 */
73 enum PartMode
74   {
75     PART_2Nx2N = 0,
76     PART_2NxN  = 1,
77     PART_Nx2N  = 2,
78     PART_NxN   = 3,
79     PART_2NxnU = 4,
80     PART_2NxnD = 5,
81     PART_nLx2N = 6,
82     PART_nRx2N = 7
83   };
84 
85 enum PredMode
86   {
87     MODE_INTRA, MODE_INTER, MODE_SKIP
88   };
89 
90 enum IntraPredMode
91   {
92     INTRA_PLANAR = 0,
93     INTRA_DC = 1,
94     INTRA_ANGULAR_2 = 2,    INTRA_ANGULAR_3 = 3,    INTRA_ANGULAR_4 = 4,    INTRA_ANGULAR_5 = 5,
95     INTRA_ANGULAR_6 = 6,    INTRA_ANGULAR_7 = 7,    INTRA_ANGULAR_8 = 8,    INTRA_ANGULAR_9 = 9,
96     INTRA_ANGULAR_10 = 10,  INTRA_ANGULAR_11 = 11,  INTRA_ANGULAR_12 = 12,  INTRA_ANGULAR_13 = 13,
97     INTRA_ANGULAR_14 = 14,  INTRA_ANGULAR_15 = 15,  INTRA_ANGULAR_16 = 16,  INTRA_ANGULAR_17 = 17,
98     INTRA_ANGULAR_18 = 18,  INTRA_ANGULAR_19 = 19,  INTRA_ANGULAR_20 = 20,  INTRA_ANGULAR_21 = 21,
99     INTRA_ANGULAR_22 = 22,  INTRA_ANGULAR_23 = 23,  INTRA_ANGULAR_24 = 24,  INTRA_ANGULAR_25 = 25,
100     INTRA_ANGULAR_26 = 26,  INTRA_ANGULAR_27 = 27,  INTRA_ANGULAR_28 = 28,  INTRA_ANGULAR_29 = 29,
101     INTRA_ANGULAR_30 = 30,  INTRA_ANGULAR_31 = 31,  INTRA_ANGULAR_32 = 32,  INTRA_ANGULAR_33 = 33,
102     INTRA_ANGULAR_34 = 34
103   };
104 
105 
106 enum IntraChromaPredMode
107   {
108     INTRA_CHROMA_PLANAR_OR_34     = 0,
109     INTRA_CHROMA_ANGULAR_26_OR_34 = 1,
110     INTRA_CHROMA_ANGULAR_10_OR_34 = 2,
111     INTRA_CHROMA_DC_OR_34         = 3,
112     INTRA_CHROMA_LIKE_LUMA  = 4
113   };
114 
115 
116 enum InterPredIdc
117   {
118     // note: values have to match the decoding function decode_inter_pred_idc()
119     PRED_L0=1,
120     PRED_L1=2,
121     PRED_BI=3
122   };
123 
124 
125 
126 class slice_segment_header {
127 public:
slice_segment_header()128   slice_segment_header() {
129     reset();
130   }
131 
132   de265_error read(bitreader* br, decoder_context*, bool* continueDecoding);
133   de265_error write(error_queue*, CABAC_encoder&,
134                     const seq_parameter_set* sps,
135                     const pic_parameter_set* pps,
136                     uint8_t nal_unit_type);
137 
138   void dump_slice_segment_header(const decoder_context*, int fd) const;
139 
140   void set_defaults();
141   void reset();
142 
143 
144   int  slice_index; // index through all slices in a picture  (internal only)
145 
146   char first_slice_segment_in_pic_flag;
147   char no_output_of_prior_pics_flag;
148   int  slice_pic_parameter_set_id;
149   char dependent_slice_segment_flag;
150   int  slice_segment_address;
151 
152   int  slice_type;
153   char pic_output_flag;
154   char colour_plane_id;
155   int  slice_pic_order_cnt_lsb;
156   char short_term_ref_pic_set_sps_flag;
157   ref_pic_set slice_ref_pic_set;
158 
159   int  short_term_ref_pic_set_idx;
160   int  num_long_term_sps;
161   int  num_long_term_pics;
162 
163   uint8_t lt_idx_sps[MAX_NUM_REF_PICS];
164   int     poc_lsb_lt[MAX_NUM_REF_PICS];
165   char    used_by_curr_pic_lt_flag[MAX_NUM_REF_PICS];
166 
167   char delta_poc_msb_present_flag[MAX_NUM_REF_PICS];
168   int delta_poc_msb_cycle_lt[MAX_NUM_REF_PICS];
169 
170   char slice_temporal_mvp_enabled_flag;
171   char slice_sao_luma_flag;
172   char slice_sao_chroma_flag;
173 
174   char num_ref_idx_active_override_flag;
175   int  num_ref_idx_l0_active; // [1;16]
176   int  num_ref_idx_l1_active; // [1;16]
177 
178   char ref_pic_list_modification_flag_l0;
179   char ref_pic_list_modification_flag_l1;
180   uint8_t list_entry_l0[16];
181   uint8_t list_entry_l1[16];
182 
183   char mvd_l1_zero_flag;
184   char cabac_init_flag;
185   char collocated_from_l0_flag;
186   int  collocated_ref_idx;
187 
188   // --- pred_weight_table ---
189 
190   uint8_t luma_log2_weight_denom; // [0;7]
191   uint8_t ChromaLog2WeightDenom;  // [0;7]
192 
193   // first index is L0/L1
194   uint8_t luma_weight_flag[2][16];   // bool
195   uint8_t chroma_weight_flag[2][16]; // bool
196   int16_t LumaWeight[2][16];
197   int8_t  luma_offset[2][16];
198   int16_t ChromaWeight[2][16][2];
199   int8_t  ChromaOffset[2][16][2];
200 
201 
202   int  five_minus_max_num_merge_cand;
203   int  slice_qp_delta;
204 
205   int  slice_cb_qp_offset;
206   int  slice_cr_qp_offset;
207 
208   char cu_chroma_qp_offset_enabled_flag;
209 
210   char deblocking_filter_override_flag;
211   char slice_deblocking_filter_disabled_flag;
212   int  slice_beta_offset; // = pps->beta_offset if undefined
213   int  slice_tc_offset;   // = pps->tc_offset if undefined
214 
215   char slice_loop_filter_across_slices_enabled_flag;
216 
217   int  num_entry_point_offsets;
218   int  offset_len;
219   std::vector<int> entry_point_offset;
220 
221   int  slice_segment_header_extension_length;
222 
223 
224   // --- derived data ---
225 
226   int SliceQPY;
227   int initType;
228 
229   void compute_derived_values(const pic_parameter_set* pps);
230 
231 
232   // --- data for external modules ---
233 
234   int SliceAddrRS;  // slice_segment_address of last independent slice
235 
236   int MaxNumMergeCand;  // directly derived from 'five_minus_max_num_merge_cand'
237   int CurrRpsIdx;
238   ref_pic_set CurrRps;  // the active reference-picture set
239   int NumPocTotalCurr;
240 
241   // number of entries: num_ref_idx_l0_active / num_ref_idx_l1_active
242   int RefPicList[2][MAX_NUM_REF_PICS]; // contains buffer IDs (D:indices into DPB/E:frame number)
243   int RefPicList_POC[2][MAX_NUM_REF_PICS];
244   int RefPicList_PicState[2][MAX_NUM_REF_PICS]; /* We have to save the PicState because the decoding
245                                                    of an image may be delayed and the PicState can
246                                                    change in the mean-time (e.g. from ShortTerm to
247                                                    LongTerm). PicState is used in motion.cc */
248 
249   char LongTermRefPic[2][MAX_NUM_REF_PICS]; /* Flag whether the picture at this ref-pic-list
250                                                is a long-term picture. */
251 
252   // context storage for dependent slices (stores CABAC model at end of slice segment)
253   context_model_table ctx_model_storage;
254   bool ctx_model_storage_defined; // whether there is valid data in ctx_model_storage
255 
256   std::vector<int> RemoveReferencesList; // images that can be removed from the DPB before decoding this slice
257 
258 };
259 
260 
261 
262 typedef struct {
263   // TODO: we could combine SaoTypeIdx and SaoEoClass into one byte to make the struct 16 bytes only
264 
265   unsigned char SaoTypeIdx; // use with (SaoTypeIdx>>(2*cIdx)) & 0x3
266   unsigned char SaoEoClass; // use with (SaoTypeIdx>>(2*cIdx)) & 0x3
267 
268   uint8_t sao_band_position[3];
269   int8_t  saoOffsetVal[3][4]; // index with [][idx-1] as saoOffsetVal[][0]==0 always
270 } sao_info;
271 
272 
273 
274 
275 de265_error read_slice_segment_data(thread_context* tctx);
276 
277 bool alloc_and_init_significant_coeff_ctxIdx_lookupTable();
278 void free_significant_coeff_ctxIdx_lookupTable();
279 
280 
281 class thread_task_ctb_row : public thread_task
282 {
283 public:
284   bool   firstSliceSubstream;
285   int    debug_startCtbRow;
286   thread_context* tctx;
287 
288   virtual void work();
289   virtual std::string name() const;
290 };
291 
292 class thread_task_slice_segment : public thread_task
293 {
294 public:
295   bool   firstSliceSubstream;
296   int    debug_startCtbX, debug_startCtbY;
297   thread_context* tctx;
298 
299   virtual void work();
300   virtual std::string name() const;
301 };
302 
303 
304 int check_CTB_available(const de265_image* img,
305                         int xC,int yC, int xN,int yN);
306 
307 #endif
308