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