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