/work/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 | 0 | { |
151 | 0 | uint32_t uvlc; |
152 | 0 | int32_t svlc; |
153 | |
|
154 | 0 | pic_parameter_set* pps = ctx->get_pps((int) shdr->slice_pic_parameter_set_id); |
155 | 0 | assert(pps); |
156 | 0 | seq_parameter_set* sps = ctx->get_sps((int) pps->seq_parameter_set_id); |
157 | 0 | assert(sps); |
158 | | |
159 | 0 | uvlc = br->get_uvlc(); |
160 | 0 | if (uvlc > 7) return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
161 | 0 | shdr->luma_log2_weight_denom = uvlc; |
162 | |
|
163 | 0 | if (sps->chroma_format_idc != 0) { |
164 | 0 | svlc = br->get_svlc(); |
165 | 0 | svlc += shdr->luma_log2_weight_denom; |
166 | 0 | if (svlc < 0 || svlc > 7) return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
167 | 0 | shdr->ChromaLog2WeightDenom = svlc; |
168 | 0 | } |
169 | | |
170 | 0 | int sumWeightFlags[2]{}; |
171 | |
|
172 | 0 | for (int l = 0; l <= 1; l++) |
173 | 0 | if (l == 0 || (l == 1 && shdr->slice_type == SLICE_TYPE_B)) { |
174 | 0 | int num_ref = (l == 0 ? shdr->num_ref_idx_l0_active - 1 : shdr->num_ref_idx_l1_active - 1); |
175 | |
|
176 | 0 | for (int i = 0; i <= num_ref; i++) { |
177 | 0 | shdr->luma_weight_flag[l][i] = br->get_bits(1); |
178 | 0 | if (shdr->luma_weight_flag[l][i]) sumWeightFlags[l]++; |
179 | 0 | } |
180 | |
|
181 | 0 | if (sps->chroma_format_idc != 0) { |
182 | 0 | for (int i = 0; i <= num_ref; i++) { |
183 | 0 | shdr->chroma_weight_flag[l][i] = br->get_bits(1); |
184 | 0 | if (shdr->chroma_weight_flag[l][i]) sumWeightFlags[l] += 2; |
185 | 0 | } |
186 | 0 | } |
187 | |
|
188 | 0 | for (int i = 0; i <= num_ref; i++) { |
189 | 0 | if (shdr->luma_weight_flag[l][i]) { |
190 | | // delta_luma_weight |
191 | |
|
192 | 0 | svlc = br->get_svlc(); |
193 | 0 | if (svlc < -128 || svlc > 127) return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
194 | | |
195 | 0 | shdr->LumaWeight[l][i] = (1 << shdr->luma_log2_weight_denom) + svlc; |
196 | | |
197 | | // luma_offset |
198 | |
|
199 | 0 | svlc = br->get_svlc(); |
200 | 0 | if (svlc < -sps->WpOffsetHalfRangeY || svlc > sps->WpOffsetHalfRangeY - 1) return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
201 | 0 | shdr->luma_offset[l][i] = svlc; |
202 | 0 | } |
203 | 0 | else { |
204 | 0 | shdr->LumaWeight[l][i] = 1 << shdr->luma_log2_weight_denom; |
205 | 0 | shdr->luma_offset[l][i] = 0; |
206 | 0 | } |
207 | | |
208 | 0 | if (shdr->chroma_weight_flag[l][i]) |
209 | 0 | for (int j = 0; j < 2; j++) { |
210 | | // delta_chroma_weight |
211 | |
|
212 | 0 | svlc = br->get_svlc(); |
213 | 0 | if (svlc < -128 || svlc > 127) return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
214 | | |
215 | 0 | shdr->ChromaWeight[l][i][j] = (1 << shdr->ChromaLog2WeightDenom) + svlc; |
216 | | |
217 | | // delta_chroma_offset |
218 | |
|
219 | 0 | svlc = br->get_svlc(); |
220 | 0 | if (svlc < -4 * sps->WpOffsetHalfRangeC || |
221 | 0 | svlc > 4 * sps->WpOffsetHalfRangeC - 1) |
222 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
223 | | |
224 | 0 | svlc = Clip3(-sps->WpOffsetHalfRangeC, |
225 | 0 | sps->WpOffsetHalfRangeC-1, |
226 | 0 | (sps->WpOffsetHalfRangeC |
227 | 0 | +svlc |
228 | 0 | -((sps->WpOffsetHalfRangeC*shdr->ChromaWeight[l][i][j]) |
229 | 0 | >> shdr->ChromaLog2WeightDenom))); |
230 | |
|
231 | 0 | shdr->ChromaOffset[l][i][j] = svlc; |
232 | 0 | } |
233 | 0 | else { |
234 | 0 | for (int j = 0; j < 2; j++) { |
235 | 0 | shdr->ChromaWeight[l][i][j] = 1 << shdr->ChromaLog2WeightDenom; |
236 | 0 | shdr->ChromaOffset[l][i][j] = 0; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | // check sumWeightFlags against limits (H.265, Section 7.4.7.3) |
243 | | |
244 | 0 | if (shdr->slice_type == SLICE_TYPE_P && sumWeightFlags[0] > 24) { |
245 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
246 | 0 | } |
247 | | |
248 | 0 | if (shdr->slice_type == SLICE_TYPE_B && sumWeightFlags[0] + sumWeightFlags[1] > 24) { |
249 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
250 | 0 | } |
251 | | |
252 | 0 | return DE265_OK; |
253 | 0 | } |
254 | | |
255 | | |
256 | | void slice_segment_header::reset() |
257 | 0 | { |
258 | 0 | pps = nullptr; |
259 | |
|
260 | 0 | slice_index = 0; |
261 | |
|
262 | 0 | first_slice_segment_in_pic_flag = 0; |
263 | 0 | no_output_of_prior_pics_flag = 0; |
264 | 0 | slice_pic_parameter_set_id = 0; |
265 | 0 | dependent_slice_segment_flag = 0; |
266 | 0 | slice_segment_address = 0; |
267 | |
|
268 | 0 | slice_type = 0; |
269 | 0 | pic_output_flag = 0; |
270 | 0 | colour_plane_id = 0; |
271 | 0 | slice_pic_order_cnt_lsb = 0; |
272 | 0 | short_term_ref_pic_set_sps_flag = 0; |
273 | 0 | slice_ref_pic_set.reset(); |
274 | |
|
275 | 0 | short_term_ref_pic_set_idx = 0; |
276 | 0 | num_long_term_sps = 0; |
277 | 0 | num_long_term_pics = 0; |
278 | |
|
279 | 0 | for (int i = 0; i < MAX_NUM_REF_PICS; i++) { |
280 | 0 | lt_idx_sps[i] = 0; |
281 | 0 | poc_lsb_lt[i] = 0; |
282 | 0 | used_by_curr_pic_lt_flag[i] = 0; |
283 | 0 | delta_poc_msb_present_flag[i] = 0; |
284 | 0 | delta_poc_msb_cycle_lt[i] = 0; |
285 | 0 | } |
286 | |
|
287 | 0 | slice_temporal_mvp_enabled_flag = 0; |
288 | 0 | slice_sao_luma_flag = 0; |
289 | 0 | slice_sao_chroma_flag = 0; |
290 | |
|
291 | 0 | num_ref_idx_active_override_flag = 0; |
292 | 0 | num_ref_idx_l0_active = 0; |
293 | 0 | num_ref_idx_l1_active = 0; |
294 | |
|
295 | 0 | ref_pic_list_modification_flag_l0 = 0; |
296 | 0 | ref_pic_list_modification_flag_l1 = 0; |
297 | 0 | for (int i = 0; i < 16; i++) { |
298 | 0 | list_entry_l0[i] = 0; |
299 | 0 | list_entry_l1[i] = 0; |
300 | 0 | } |
301 | |
|
302 | 0 | mvd_l1_zero_flag = 0; |
303 | 0 | cabac_init_flag = 0; |
304 | 0 | collocated_from_l0_flag = 0; |
305 | 0 | collocated_ref_idx = 0; |
306 | |
|
307 | 0 | luma_log2_weight_denom = 0; |
308 | 0 | ChromaLog2WeightDenom = 0; |
309 | |
|
310 | 0 | for (int i = 0; i < 2; i++) |
311 | 0 | for (int j = 0; j < 16; j++) { |
312 | 0 | luma_weight_flag[i][j] = 0; |
313 | 0 | chroma_weight_flag[i][j] = 0; |
314 | 0 | LumaWeight[i][j] = 0; |
315 | 0 | luma_offset[i][j] = 0; |
316 | 0 | ChromaWeight[i][j][0] = ChromaWeight[i][j][1] = 0; |
317 | 0 | ChromaOffset[i][j][0] = ChromaOffset[i][j][1] = 0; |
318 | 0 | } |
319 | |
|
320 | 0 | five_minus_max_num_merge_cand = 0; |
321 | 0 | slice_qp_delta = 0; |
322 | |
|
323 | 0 | slice_cb_qp_offset = 0; |
324 | 0 | slice_cr_qp_offset = 0; |
325 | |
|
326 | 0 | cu_chroma_qp_offset_enabled_flag = 0; |
327 | |
|
328 | 0 | deblocking_filter_override_flag = 0; |
329 | 0 | slice_deblocking_filter_disabled_flag = 0; |
330 | 0 | slice_beta_offset = 0; |
331 | 0 | slice_tc_offset = 0; |
332 | |
|
333 | 0 | slice_loop_filter_across_slices_enabled_flag = 0; |
334 | |
|
335 | 0 | num_entry_point_offsets = 0; |
336 | 0 | offset_len = 0; |
337 | 0 | entry_point_offset.clear(); |
338 | |
|
339 | 0 | slice_segment_header_extension_length = 0; |
340 | |
|
341 | 0 | SliceAddrRS = 0; |
342 | 0 | SliceQPY = 0; |
343 | |
|
344 | 0 | initType = 0; |
345 | |
|
346 | 0 | MaxNumMergeCand = 0; |
347 | 0 | CurrRpsIdx = 0; |
348 | 0 | CurrRps.reset(); |
349 | 0 | NumPocTotalCurr = 0; |
350 | |
|
351 | 0 | for (int i = 0; i < 2; i++) |
352 | 0 | for (int j = 0; j < MAX_NUM_REF_PICS; j++) { |
353 | 0 | RefPicList[i][j] = 0; |
354 | 0 | RefPicList_POC[i][j] = 0; |
355 | 0 | RefPicList_PicState[i][j] = 0; |
356 | 0 | LongTermRefPic[i][j] = 0; |
357 | 0 | } |
358 | | |
359 | | //context_model ctx_model_storage[CONTEXT_MODEL_TABLE_LENGTH]; |
360 | |
|
361 | 0 | RemoveReferencesList.clear(); |
362 | |
|
363 | 0 | for (int i = 0; i < 4; i++) { |
364 | 0 | ctx_model_storage_StatCoeff[i] = 0; |
365 | 0 | } |
366 | 0 | ctx_model_storage_defined = false; |
367 | 0 | } |
368 | | |
369 | | |
370 | | de265_error slice_segment_header::read(bitreader* br, decoder_context* ctx, |
371 | | bool* continueDecoding) |
372 | 0 | { |
373 | 0 | *continueDecoding = false; |
374 | 0 | reset(); |
375 | |
|
376 | 0 | uint32_t uvlc; |
377 | 0 | int32_t svlc; |
378 | | |
379 | | // set defaults |
380 | |
|
381 | 0 | dependent_slice_segment_flag = 0; |
382 | | |
383 | | |
384 | | // read bitstream |
385 | |
|
386 | 0 | first_slice_segment_in_pic_flag = br->get_bits(1); |
387 | |
|
388 | 0 | if (ctx->get_RapPicFlag()) { |
389 | | // TODO: is this still correct ? Should we drop RapPicFlag ? |
390 | 0 | no_output_of_prior_pics_flag = br->get_bits(1); |
391 | 0 | } |
392 | |
|
393 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || |
394 | 0 | uvlc >= DE265_MAX_PPS_SETS) { |
395 | 0 | ctx->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false); |
396 | 0 | return DE265_OK; |
397 | 0 | } |
398 | 0 | slice_pic_parameter_set_id = uvlc; |
399 | |
|
400 | 0 | if (!ctx->has_pps(slice_pic_parameter_set_id)) { |
401 | 0 | ctx->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false); |
402 | 0 | return DE265_OK; |
403 | 0 | } |
404 | | |
405 | 0 | pps = ctx->get_shared_pps(slice_pic_parameter_set_id); |
406 | |
|
407 | 0 | const seq_parameter_set* sps = pps->sps.get(); |
408 | 0 | 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 | 0 | if (!first_slice_segment_in_pic_flag) { |
415 | 0 | if (pps->dependent_slice_segments_enabled_flag) { |
416 | 0 | dependent_slice_segment_flag = br->get_bits(1); |
417 | 0 | } |
418 | 0 | else { |
419 | 0 | dependent_slice_segment_flag = 0; |
420 | 0 | } |
421 | |
|
422 | 0 | uint32_t slice_segment_address = br->get_bits(ceil_log2(sps->PicSizeInCtbsY)); |
423 | |
|
424 | 0 | if (dependent_slice_segment_flag) { |
425 | 0 | if (slice_segment_address == 0) { |
426 | 0 | *continueDecoding = false; |
427 | 0 | ctx->add_warning(DE265_WARNING_DEPENDENT_SLICE_WITH_ADDRESS_ZERO, false); |
428 | 0 | return DE265_OK; |
429 | 0 | } |
430 | | |
431 | 0 | if (ctx->previous_slice_header == nullptr) { |
432 | 0 | return DE265_ERROR_NO_INITIAL_SLICE_HEADER; |
433 | 0 | } |
434 | | |
435 | 0 | *this = *ctx->previous_slice_header; |
436 | |
|
437 | 0 | first_slice_segment_in_pic_flag = 0; |
438 | 0 | dependent_slice_segment_flag = 1; |
439 | 0 | } |
440 | | |
441 | 0 | this->slice_segment_address = slice_segment_address; |
442 | 0 | } |
443 | 0 | else { |
444 | 0 | dependent_slice_segment_flag = 0; |
445 | 0 | slice_segment_address = 0; |
446 | 0 | } |
447 | | |
448 | 0 | if (slice_segment_address >= sps->PicSizeInCtbsY) { |
449 | 0 | ctx->add_warning(DE265_WARNING_SLICE_SEGMENT_ADDRESS_INVALID, false); |
450 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
451 | 0 | } |
452 | | |
453 | | //printf("SLICE %d (%d)\n",slice_segment_address, sps->PicSizeInCtbsY); |
454 | | |
455 | | |
456 | 0 | if (!dependent_slice_segment_flag) { |
457 | 0 | for (int i = 0; i < pps->num_extra_slice_header_bits; i++) { |
458 | | //slice_reserved_undetermined_flag[i] |
459 | 0 | br->skip_bits(1); |
460 | 0 | } |
461 | |
|
462 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || |
463 | 0 | uvlc > 2) { |
464 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
465 | 0 | *continueDecoding = false; |
466 | 0 | return DE265_OK; |
467 | 0 | } |
468 | 0 | slice_type = uvlc; |
469 | |
|
470 | 0 | if (pps->output_flag_present_flag) { |
471 | 0 | pic_output_flag = br->get_bits(1); |
472 | 0 | } |
473 | 0 | else { |
474 | 0 | pic_output_flag = 1; |
475 | 0 | } |
476 | |
|
477 | 0 | if (sps->separate_colour_plane_flag == 1) { |
478 | 0 | colour_plane_id = br->get_bits(2); |
479 | 0 | } |
480 | | |
481 | |
|
482 | 0 | slice_pic_order_cnt_lsb = 0; |
483 | 0 | short_term_ref_pic_set_sps_flag = 0; |
484 | |
|
485 | 0 | int NumLtPics = 0; |
486 | |
|
487 | 0 | if (ctx->get_nal_unit_type() != NAL_UNIT_IDR_W_RADL && |
488 | 0 | ctx->get_nal_unit_type() != NAL_UNIT_IDR_N_LP) { |
489 | 0 | slice_pic_order_cnt_lsb = br->get_bits(sps->log2_max_pic_order_cnt_lsb); |
490 | 0 | short_term_ref_pic_set_sps_flag = br->get_bits(1); |
491 | |
|
492 | 0 | if (!short_term_ref_pic_set_sps_flag) { |
493 | 0 | read_short_term_ref_pic_set(ctx, sps, |
494 | 0 | br, &slice_ref_pic_set, |
495 | 0 | sps->num_short_term_ref_pic_sets(), |
496 | 0 | sps->ref_pic_sets, |
497 | 0 | true); |
498 | |
|
499 | 0 | CurrRpsIdx = sps->num_short_term_ref_pic_sets(); |
500 | 0 | CurrRps = slice_ref_pic_set; |
501 | 0 | } |
502 | 0 | else { |
503 | 0 | int nBits = ceil_log2(sps->num_short_term_ref_pic_sets()); |
504 | 0 | if (nBits > 0) short_term_ref_pic_set_idx = br->get_bits(nBits); |
505 | 0 | else short_term_ref_pic_set_idx = 0; |
506 | |
|
507 | 0 | if (short_term_ref_pic_set_idx >= sps->num_short_term_ref_pic_sets()) { |
508 | 0 | ctx->add_warning(DE265_WARNING_SHORT_TERM_REF_PIC_SET_OUT_OF_RANGE, false); |
509 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
510 | 0 | } |
511 | | |
512 | 0 | CurrRpsIdx = short_term_ref_pic_set_idx; |
513 | 0 | CurrRps = sps->ref_pic_sets[CurrRpsIdx]; |
514 | 0 | } |
515 | | |
516 | | |
517 | | // --- long-term MC --- |
518 | | |
519 | 0 | if (sps->long_term_ref_pics_present_flag) { |
520 | 0 | if (sps->num_long_term_ref_pics_sps > 0) { |
521 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || |
522 | 0 | uvlc > sps->num_long_term_ref_pics_sps) { |
523 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
524 | 0 | } |
525 | 0 | num_long_term_sps = uvlc; |
526 | 0 | } |
527 | 0 | else { |
528 | 0 | num_long_term_sps = 0; |
529 | 0 | } |
530 | | |
531 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > MAX_NUM_LT_REF_PICS_SPS) { |
532 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
533 | 0 | } |
534 | 0 | num_long_term_pics = uvlc; |
535 | | |
536 | | // check maximum number of reference frames |
537 | |
|
538 | 0 | if (num_long_term_sps + |
539 | 0 | num_long_term_pics + |
540 | 0 | CurrRps.NumNegativePics + |
541 | 0 | CurrRps.NumPositivePics |
542 | 0 | > sps->sps_max_dec_pic_buffering[sps->sps_max_sub_layers - 1]) { |
543 | 0 | ctx->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false); |
544 | 0 | *continueDecoding = false; |
545 | 0 | return DE265_OK; |
546 | 0 | } |
547 | | |
548 | 0 | for (int i = 0; i < num_long_term_sps + num_long_term_pics; i++) { |
549 | 0 | if (i < num_long_term_sps) { |
550 | 0 | int nBits = ceil_log2(sps->num_long_term_ref_pics_sps); |
551 | 0 | lt_idx_sps[i] = br->get_bits(nBits); |
552 | | |
553 | | // check that the referenced lt-reference really exists |
554 | |
|
555 | 0 | if (lt_idx_sps[i] >= sps->num_long_term_ref_pics_sps) { |
556 | 0 | ctx->add_warning(DE265_NON_EXISTING_LT_REFERENCE_CANDIDATE_IN_SLICE_HEADER, false); |
557 | 0 | *continueDecoding = false; |
558 | 0 | return DE265_OK; |
559 | 0 | } |
560 | | |
561 | | // delta_poc_msb_present_flag[i] = 0; // TODO ? |
562 | | |
563 | 0 | ctx->PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps[i]]; |
564 | 0 | ctx->UsedByCurrPicLt[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps[i]]; |
565 | 0 | } |
566 | 0 | else { |
567 | 0 | int nBits = sps->log2_max_pic_order_cnt_lsb; |
568 | 0 | poc_lsb_lt[i] = br->get_bits(nBits); |
569 | 0 | used_by_curr_pic_lt_flag[i] = br->get_bits(1); |
570 | |
|
571 | 0 | ctx->PocLsbLt[i] = poc_lsb_lt[i]; |
572 | 0 | ctx->UsedByCurrPicLt[i] = used_by_curr_pic_lt_flag[i]; |
573 | 0 | } |
574 | | |
575 | 0 | if (ctx->UsedByCurrPicLt[i]) { |
576 | 0 | NumLtPics++; |
577 | 0 | } |
578 | |
|
579 | 0 | delta_poc_msb_present_flag[i] = br->get_bits(1); |
580 | 0 | if (delta_poc_msb_present_flag[i]) { |
581 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR) { |
582 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
583 | 0 | } |
584 | 0 | delta_poc_msb_cycle_lt[i] = uvlc; |
585 | 0 | } |
586 | 0 | else { |
587 | 0 | delta_poc_msb_cycle_lt[i] = 0; |
588 | 0 | } |
589 | | |
590 | 0 | if (i == 0 || i == num_long_term_sps) { |
591 | 0 | ctx->DeltaPocMsbCycleLt[i] = delta_poc_msb_cycle_lt[i]; |
592 | 0 | } |
593 | 0 | else { |
594 | 0 | 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 | 0 | ctx->DeltaPocMsbCycleLt[i] = (delta_poc_msb_cycle_lt[i] + |
599 | 0 | ctx->DeltaPocMsbCycleLt[i - 1]); |
600 | 0 | } |
601 | 0 | } |
602 | 0 | } |
603 | 0 | else { |
604 | 0 | num_long_term_sps = 0; |
605 | 0 | num_long_term_pics = 0; |
606 | 0 | } |
607 | | |
608 | 0 | if (sps->sps_temporal_mvp_enabled_flag) { |
609 | 0 | slice_temporal_mvp_enabled_flag = br->get_bits(1); |
610 | 0 | } |
611 | 0 | else { |
612 | 0 | slice_temporal_mvp_enabled_flag = 0; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | else { |
616 | 0 | slice_pic_order_cnt_lsb = 0; |
617 | 0 | num_long_term_sps = 0; |
618 | 0 | num_long_term_pics = 0; |
619 | 0 | } |
620 | | |
621 | | |
622 | | // --- SAO --- |
623 | | |
624 | 0 | if (sps->sample_adaptive_offset_enabled_flag) { |
625 | 0 | slice_sao_luma_flag = br->get_bits(1); |
626 | |
|
627 | 0 | if (sps->ChromaArrayType != CHROMA_MONO) { |
628 | 0 | slice_sao_chroma_flag = br->get_bits(1); |
629 | 0 | } |
630 | 0 | else { |
631 | 0 | slice_sao_chroma_flag = 0; |
632 | 0 | } |
633 | 0 | } |
634 | 0 | else { |
635 | 0 | slice_sao_luma_flag = 0; |
636 | 0 | slice_sao_chroma_flag = 0; |
637 | 0 | } |
638 | |
|
639 | 0 | num_ref_idx_l0_active = 0; |
640 | 0 | num_ref_idx_l1_active = 0; |
641 | |
|
642 | 0 | if (slice_type == SLICE_TYPE_P || |
643 | 0 | slice_type == SLICE_TYPE_B) { |
644 | 0 | num_ref_idx_active_override_flag = br->get_bits(1); |
645 | 0 | if (num_ref_idx_active_override_flag) { |
646 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > 15) { |
647 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
648 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
649 | 0 | } |
650 | 0 | num_ref_idx_l0_active = uvlc + 1; |
651 | |
|
652 | 0 | if (slice_type == SLICE_TYPE_B) { |
653 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > 15) { |
654 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
655 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
656 | 0 | } |
657 | 0 | num_ref_idx_l1_active = uvlc + 1; |
658 | 0 | } |
659 | 0 | } |
660 | 0 | else { |
661 | 0 | num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active; |
662 | 0 | num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active; |
663 | 0 | } |
664 | | |
665 | 0 | NumPocTotalCurr = CurrRps.NumPocTotalCurr_shortterm_only + NumLtPics; |
666 | |
|
667 | 0 | if (pps->lists_modification_present_flag && NumPocTotalCurr > 1) { |
668 | 0 | int nBits = ceil_log2(NumPocTotalCurr); |
669 | |
|
670 | 0 | ref_pic_list_modification_flag_l0 = br->get_bits(1); |
671 | 0 | if (ref_pic_list_modification_flag_l0) { |
672 | 0 | for (int i = 0; i < num_ref_idx_l0_active; i++) { |
673 | 0 | list_entry_l0[i] = br->get_bits(nBits); |
674 | 0 | } |
675 | 0 | } |
676 | |
|
677 | 0 | if (slice_type == SLICE_TYPE_B) { |
678 | 0 | ref_pic_list_modification_flag_l1 = br->get_bits(1); |
679 | 0 | if (ref_pic_list_modification_flag_l1) { |
680 | 0 | for (int i = 0; i < num_ref_idx_l1_active; i++) { |
681 | 0 | list_entry_l1[i] = br->get_bits(nBits); |
682 | 0 | } |
683 | 0 | } |
684 | 0 | } |
685 | 0 | else { |
686 | 0 | ref_pic_list_modification_flag_l1 = 0; |
687 | 0 | } |
688 | 0 | } |
689 | 0 | else { |
690 | 0 | ref_pic_list_modification_flag_l0 = 0; |
691 | 0 | ref_pic_list_modification_flag_l1 = 0; |
692 | 0 | } |
693 | |
|
694 | 0 | if (slice_type == SLICE_TYPE_B) { |
695 | 0 | mvd_l1_zero_flag = br->get_bits(1); |
696 | 0 | } |
697 | |
|
698 | 0 | if (pps->cabac_init_present_flag) { |
699 | 0 | cabac_init_flag = br->get_bits(1); |
700 | 0 | } |
701 | 0 | else { |
702 | 0 | cabac_init_flag = 0; |
703 | 0 | } |
704 | |
|
705 | 0 | if (slice_temporal_mvp_enabled_flag) { |
706 | 0 | if (slice_type == SLICE_TYPE_B) |
707 | 0 | collocated_from_l0_flag = br->get_bits(1); |
708 | 0 | else |
709 | 0 | collocated_from_l0_flag = 1; |
710 | |
|
711 | 0 | if ((collocated_from_l0_flag && num_ref_idx_l0_active > 1) || |
712 | 0 | (!collocated_from_l0_flag && num_ref_idx_l1_active > 1)) { |
713 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > 15) { |
714 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
715 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
716 | 0 | } |
717 | 0 | collocated_ref_idx = uvlc; |
718 | 0 | } |
719 | 0 | else { |
720 | 0 | collocated_ref_idx = 0; |
721 | 0 | } |
722 | | |
723 | | // check whether collocated_ref_idx points to a valid index |
724 | | |
725 | 0 | if ((collocated_from_l0_flag && collocated_ref_idx >= num_ref_idx_l0_active) || |
726 | 0 | (!collocated_from_l0_flag && collocated_ref_idx >= num_ref_idx_l1_active)) { |
727 | 0 | ctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false); |
728 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
729 | 0 | } |
730 | 0 | } |
731 | | |
732 | | |
733 | 0 | if ((pps->weighted_pred_flag && slice_type == SLICE_TYPE_P) || |
734 | 0 | (pps->weighted_bipred_flag && slice_type == SLICE_TYPE_B)) { |
735 | 0 | de265_error err = read_pred_weight_table(br, this, ctx); |
736 | 0 | if (err) { |
737 | 0 | ctx->add_warning(err, false); |
738 | 0 | return err; |
739 | 0 | } |
740 | 0 | } |
741 | | |
742 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > 5) { |
743 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
744 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
745 | 0 | } |
746 | | |
747 | 0 | five_minus_max_num_merge_cand = uvlc; |
748 | 0 | MaxNumMergeCand = 5 - five_minus_max_num_merge_cand; |
749 | 0 | } |
750 | | |
751 | 0 | if ((svlc = br->get_svlc()) == SVLC_ERROR) { |
752 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
753 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
754 | 0 | } |
755 | | |
756 | 0 | slice_qp_delta = svlc; |
757 | | //logtrace(LogSlice,"slice_qp_delta: %d\n",shdr->slice_qp_delta); |
758 | |
|
759 | 0 | if (pps->pps_slice_chroma_qp_offsets_present_flag) { |
760 | 0 | if ((svlc = br->get_svlc()) == SVLC_ERROR) { |
761 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
762 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
763 | 0 | } |
764 | | |
765 | 0 | slice_cb_qp_offset = svlc; |
766 | |
|
767 | 0 | if ((svlc = br->get_svlc()) == SVLC_ERROR) { |
768 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
769 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
770 | 0 | } |
771 | | |
772 | 0 | slice_cr_qp_offset = svlc; |
773 | 0 | } |
774 | 0 | else { |
775 | 0 | slice_cb_qp_offset = 0; |
776 | 0 | slice_cr_qp_offset = 0; |
777 | 0 | } |
778 | | |
779 | 0 | 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 | 0 | if (pps->deblocking_filter_override_enabled_flag) { |
784 | 0 | deblocking_filter_override_flag = br->get_bits(1); |
785 | 0 | } |
786 | 0 | else { |
787 | 0 | deblocking_filter_override_flag = 0; |
788 | 0 | } |
789 | |
|
790 | 0 | slice_beta_offset = pps->beta_offset; |
791 | 0 | slice_tc_offset = pps->tc_offset; |
792 | |
|
793 | 0 | if (deblocking_filter_override_flag) { |
794 | 0 | slice_deblocking_filter_disabled_flag = br->get_bits(1); |
795 | 0 | if (!slice_deblocking_filter_disabled_flag) { |
796 | | // slice_beta_offset_div2 shall be in [-6, 6] (Sec. 7.4.7.1) |
797 | 0 | if ((svlc = br->get_svlc()) == SVLC_ERROR || svlc < -6 || svlc > 6) { |
798 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
799 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
800 | 0 | } |
801 | 0 | slice_beta_offset = svlc * 2; |
802 | | |
803 | | // slice_tc_offset_div2 shall be in [-6, 6] (Sec. 7.4.7.1) |
804 | 0 | if ((svlc = br->get_svlc()) == SVLC_ERROR || svlc < -6 || svlc > 6) { |
805 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
806 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
807 | 0 | } |
808 | 0 | slice_tc_offset = svlc * 2; |
809 | 0 | } |
810 | 0 | } |
811 | 0 | else { |
812 | 0 | slice_deblocking_filter_disabled_flag = pps->pic_disable_deblocking_filter_flag; |
813 | 0 | } |
814 | | |
815 | 0 | if (pps->pps_loop_filter_across_slices_enabled_flag && |
816 | 0 | (slice_sao_luma_flag || slice_sao_chroma_flag || |
817 | 0 | !slice_deblocking_filter_disabled_flag)) { |
818 | 0 | slice_loop_filter_across_slices_enabled_flag = br->get_bits(1); |
819 | 0 | } |
820 | 0 | else { |
821 | 0 | slice_loop_filter_across_slices_enabled_flag = |
822 | 0 | pps->pps_loop_filter_across_slices_enabled_flag; |
823 | 0 | } |
824 | 0 | } |
825 | | |
826 | 0 | if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) { |
827 | | |
828 | | // compute the spec limit for num_entry_point_offsets |
829 | |
|
830 | 0 | int maxEntryPointOffsets; |
831 | 0 | if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag) { |
832 | 0 | maxEntryPointOffsets = sps->PicHeightInCtbsY - 1; |
833 | 0 | } |
834 | 0 | else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag) { |
835 | 0 | maxEntryPointOffsets = pps->num_tile_columns * pps->num_tile_rows - 1; |
836 | 0 | } |
837 | 0 | else { |
838 | 0 | maxEntryPointOffsets = pps->num_tile_columns * sps->PicHeightInCtbsY - 1; |
839 | 0 | } |
840 | |
|
841 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || |
842 | 0 | uvlc > static_cast<uint32_t>(maxEntryPointOffsets)) { |
843 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
844 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
845 | 0 | } |
846 | 0 | num_entry_point_offsets = uvlc; |
847 | |
|
848 | 0 | entry_point_offset.resize(num_entry_point_offsets); |
849 | |
|
850 | 0 | if (num_entry_point_offsets > 0) { |
851 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || uvlc > 31) { |
852 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
853 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
854 | 0 | } |
855 | 0 | offset_len = uvlc + 1; |
856 | |
|
857 | 0 | for (uint32_t i = 0; i < num_entry_point_offsets; i++) { |
858 | 0 | { |
859 | 0 | uint32_t offset_minus1 = br->get_bits(offset_len); |
860 | 0 | if (offset_minus1 == UINT32_MAX) { |
861 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
862 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
863 | 0 | } |
864 | 0 | entry_point_offset[i] = offset_minus1 + 1; |
865 | 0 | } |
866 | | |
867 | 0 | if (i > 0) { |
868 | 0 | if (entry_point_offset[i] > UINT32_MAX - entry_point_offset[i - 1]) { |
869 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
870 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
871 | 0 | } |
872 | 0 | entry_point_offset[i] += entry_point_offset[i - 1]; |
873 | 0 | } |
874 | 0 | } |
875 | 0 | } |
876 | 0 | } |
877 | 0 | else { |
878 | 0 | num_entry_point_offsets = 0; |
879 | 0 | } |
880 | | |
881 | 0 | if (pps->slice_segment_header_extension_present_flag) { |
882 | 0 | if ((uvlc = br->get_uvlc()) == UVLC_ERROR || |
883 | 0 | uvlc > 1000) { |
884 | | // TODO: safety check against too large values |
885 | 0 | ctx->add_warning(DE265_WARNING_SLICEHEADER_INVALID, false); |
886 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
887 | 0 | } |
888 | 0 | slice_segment_header_extension_length = uvlc; |
889 | |
|
890 | 0 | for (int i = 0; i < slice_segment_header_extension_length; i++) { |
891 | | //slice_segment_header_extension_data_byte[i] |
892 | 0 | br->get_bits(8); |
893 | 0 | } |
894 | 0 | } |
895 | | |
896 | | |
897 | 0 | compute_derived_values(pps.get()); |
898 | | |
899 | | // SliceQpY shall be in [-QpBdOffsetY, 51] (Sec. 7.4.7.1) |
900 | 0 | if (SliceQPY < -sps->QpBdOffset_Y || SliceQPY > 51) { |
901 | 0 | ctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false); |
902 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
903 | 0 | } |
904 | | |
905 | 0 | *continueDecoding = true; |
906 | 0 | return DE265_OK; |
907 | 0 | } |
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 | 0 | { |
1273 | | // --- init variables --- |
1274 | |
|
1275 | 0 | SliceQPY = pps->pic_init_qp + slice_qp_delta; |
1276 | |
|
1277 | 0 | switch (slice_type) { |
1278 | 0 | case SLICE_TYPE_I: initType = 0; |
1279 | 0 | break; |
1280 | 0 | case SLICE_TYPE_P: initType = cabac_init_flag + 1; |
1281 | 0 | break; |
1282 | 0 | case SLICE_TYPE_B: initType = 2 - cabac_init_flag; |
1283 | 0 | break; |
1284 | 0 | } |
1285 | | |
1286 | 0 | MaxNumMergeCand = 5 - five_minus_max_num_merge_cand; |
1287 | 0 | } |
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 | 0 | { |
1538 | 0 | const int QPY = tctx->shdr->SliceQPY; |
1539 | 0 | const int initType = tctx->shdr->initType; |
1540 | 0 | assert(initType >= 0 && initType <= 2); |
1541 | | |
1542 | 0 | tctx->ctx_model.init(initType, QPY); |
1543 | |
|
1544 | 0 | for (int i = 0; i < 4; i++) { |
1545 | 0 | tctx->StatCoeff[i] = 0; |
1546 | 0 | } |
1547 | 0 | } |
1548 | | |
1549 | | |
1550 | | static int decode_transform_skip_flag(thread_context* tctx, int cIdx) |
1551 | 0 | { |
1552 | 0 | const int context = (cIdx == 0) ? 0 : 1; |
1553 | |
|
1554 | 0 | logtrace(LogSlice, "# transform_skip_flag (context=%d)\n", context); |
1555 | |
|
1556 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
1557 | 0 | &tctx->ctx_model[CONTEXT_MODEL_TRANSFORM_SKIP_FLAG + context]); |
1558 | |
|
1559 | 0 | logtrace(LogSymbols, "$1 transform_skip_flag=%d\n", bit); |
1560 | |
|
1561 | 0 | return bit; |
1562 | 0 | } |
1563 | | |
1564 | | |
1565 | | static int decode_sao_merge_flag(thread_context* tctx) |
1566 | 0 | { |
1567 | 0 | logtrace(LogSlice, "# sao_merge_left/up_flag\n"); |
1568 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
1569 | 0 | &tctx->ctx_model[CONTEXT_MODEL_SAO_MERGE_FLAG]); |
1570 | |
|
1571 | 0 | logtrace(LogSymbols, "$1 sao_merge_flag=%d\n", bit); |
1572 | |
|
1573 | 0 | return bit; |
1574 | 0 | } |
1575 | | |
1576 | | |
1577 | | static uint8_t decode_sao_type_idx(thread_context* tctx) |
1578 | 0 | { |
1579 | 0 | logtrace(LogSlice, "# sao_type_idx_luma/chroma\n"); |
1580 | |
|
1581 | 0 | int bit0 = tctx->cabac_decoder.decode_bit( |
1582 | 0 | &tctx->ctx_model[CONTEXT_MODEL_SAO_TYPE_IDX]); |
1583 | |
|
1584 | 0 | if (bit0 == 0) { |
1585 | 0 | logtrace(LogSymbols, "$1 sao_type_idx=%d\n", 0); |
1586 | 0 | return 0; |
1587 | 0 | } |
1588 | 0 | else { |
1589 | 0 | int bit1 = tctx->cabac_decoder.decode_bypass(); |
1590 | 0 | if (bit1 == 0) { |
1591 | 0 | logtrace(LogSymbols, "$1 sao_type_idx=%d\n", 1); |
1592 | 0 | return 1; |
1593 | 0 | } |
1594 | 0 | else { |
1595 | 0 | logtrace(LogSymbols, "$1 sao_type_idx=%d\n", 2); |
1596 | 0 | return 2; |
1597 | 0 | } |
1598 | 0 | } |
1599 | 0 | } |
1600 | | |
1601 | | |
1602 | | static uint8_t decode_sao_offset_abs(thread_context* tctx, int bitDepth) |
1603 | 0 | { |
1604 | 0 | logtrace(LogSlice, "# sao_offset_abs\n"); |
1605 | 0 | int cMax = (1 << (libde265_min(bitDepth, 10) - 5)) - 1; |
1606 | 0 | assert(cMax >= 7 && cMax<=31); |
1607 | 0 | uint8_t value = static_cast<uint8_t>(tctx->cabac_decoder.decode_TU_bypass( cMax)); |
1608 | 0 | logtrace(LogSymbols, "$1 sao_offset_abs=%d\n", value); |
1609 | 0 | return value; |
1610 | 0 | } |
1611 | | |
1612 | | |
1613 | | static int decode_sao_class(thread_context* tctx) |
1614 | 0 | { |
1615 | 0 | logtrace(LogSlice, "# sao_class\n"); |
1616 | 0 | int value = tctx->cabac_decoder.decode_FL_bypass( 2); |
1617 | 0 | logtrace(LogSymbols, "$1 sao_class=%d\n", value); |
1618 | 0 | return value; |
1619 | 0 | } |
1620 | | |
1621 | | |
1622 | | static int decode_sao_offset_sign(thread_context* tctx) |
1623 | 0 | { |
1624 | 0 | logtrace(LogSlice, "# sao_offset_sign\n"); |
1625 | 0 | int value = tctx->cabac_decoder.decode_bypass(); |
1626 | 0 | logtrace(LogSymbols, "$1 sao_offset_sign=%d\n", value); |
1627 | 0 | return value; |
1628 | 0 | } |
1629 | | |
1630 | | |
1631 | | static int decode_sao_band_position(thread_context* tctx) |
1632 | 0 | { |
1633 | 0 | logtrace(LogSlice, "# sao_band_position\n"); |
1634 | 0 | int value = tctx->cabac_decoder.decode_FL_bypass( 5); |
1635 | 0 | logtrace(LogSymbols, "$1 sao_band_position=%d\n", value); |
1636 | 0 | return value; |
1637 | 0 | } |
1638 | | |
1639 | | |
1640 | | static int decode_transquant_bypass_flag(thread_context* tctx) |
1641 | 0 | { |
1642 | 0 | logtrace(LogSlice, "# cu_transquant_bypass_enable_flag\n"); |
1643 | 0 | int value = tctx->cabac_decoder.decode_bit( |
1644 | 0 | &tctx->ctx_model[CONTEXT_MODEL_CU_TRANSQUANT_BYPASS_FLAG]); |
1645 | 0 | logtrace(LogSymbols, "$1 transquant_bypass_flag=%d\n", value); |
1646 | 0 | return value; |
1647 | 0 | } |
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 | 0 | { |
1656 | | // check if neighbors are available |
1657 | |
|
1658 | 0 | int availableL = check_CTB_available(tctx->img, x0, y0, x0 - 1, y0); |
1659 | 0 | int availableA = check_CTB_available(tctx->img, x0, y0, x0, y0 - 1); |
1660 | |
|
1661 | 0 | int condL = 0; |
1662 | 0 | int condA = 0; |
1663 | |
|
1664 | 0 | if (availableL && tctx->img->get_ctDepth(x0 - 1, y0) > ctDepth) condL = 1; |
1665 | 0 | if (availableA && tctx->img->get_ctDepth(x0, y0 - 1) > ctDepth) condA = 1; |
1666 | |
|
1667 | 0 | int contextOffset = condL + condA; |
1668 | 0 | int context = contextOffset; |
1669 | | |
1670 | | // decode bit |
1671 | |
|
1672 | 0 | logtrace(LogSlice, "# split_cu_flag context=%d R=%x\n", context, tctx->cabac_decoder.range); |
1673 | |
|
1674 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_SPLIT_CU_FLAG + context]); |
1675 | |
|
1676 | 0 | logtrace(LogSlice, "> split_cu_flag R=%x, ctx=%d, bit=%d\n", tctx->cabac_decoder.range, context, bit); |
1677 | |
|
1678 | 0 | logtrace(LogSymbols, "$1 split_cu_flag=%d\n", bit); |
1679 | |
|
1680 | 0 | return bit; |
1681 | 0 | } |
1682 | | |
1683 | | |
1684 | | static int decode_cu_skip_flag(thread_context* tctx, |
1685 | | int x0, int y0, int ctDepth) |
1686 | 0 | { |
1687 | | // check if neighbors are available |
1688 | |
|
1689 | 0 | int availableL = check_CTB_available(tctx->img, x0, y0, x0 - 1, y0); |
1690 | 0 | int availableA = check_CTB_available(tctx->img, x0, y0, x0, y0 - 1); |
1691 | |
|
1692 | 0 | int condL = 0; |
1693 | 0 | int condA = 0; |
1694 | |
|
1695 | 0 | if (availableL && tctx->img->get_cu_skip_flag(x0 - 1, y0)) condL = 1; |
1696 | 0 | if (availableA && tctx->img->get_cu_skip_flag(x0, y0 - 1)) condA = 1; |
1697 | |
|
1698 | 0 | int contextOffset = condL + condA; |
1699 | 0 | int context = contextOffset; |
1700 | | |
1701 | | // decode bit |
1702 | |
|
1703 | 0 | logtrace(LogSlice, "# cu_skip_flag context=%d R=%x\n", context, tctx->cabac_decoder.range); |
1704 | |
|
1705 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_CU_SKIP_FLAG + context]); |
1706 | |
|
1707 | 0 | logtrace(LogSlice, "> cu_skip_flag R=%x, ctx=%d, bit=%d\n", tctx->cabac_decoder.range, context, bit); |
1708 | |
|
1709 | 0 | logtrace(LogSymbols, "$1 cu_skip_flag=%d\n", bit); |
1710 | |
|
1711 | 0 | return bit; |
1712 | 0 | } |
1713 | | |
1714 | | |
1715 | | static enum PartMode decode_part_mode(thread_context* tctx, |
1716 | | enum PredMode pred_mode, int cLog2CbSize) |
1717 | 0 | { |
1718 | 0 | de265_image* img = tctx->img; |
1719 | |
|
1720 | 0 | if (pred_mode == MODE_INTRA) { |
1721 | 0 | logtrace(LogSlice, "# part_mode (INTRA)\n"); |
1722 | |
|
1723 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PART_MODE]); |
1724 | |
|
1725 | 0 | logtrace(LogSlice, "> %s\n", bit ? "2Nx2N" : "NxN"); |
1726 | |
|
1727 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", bit ? PART_2Nx2N : PART_NxN); |
1728 | |
|
1729 | 0 | return bit ? PART_2Nx2N : PART_NxN; |
1730 | 0 | } |
1731 | 0 | else { |
1732 | 0 | const seq_parameter_set& sps = img->get_sps(); |
1733 | |
|
1734 | 0 | int bit0 = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PART_MODE + 0]); |
1735 | 0 | if (bit0) { |
1736 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_2Nx2N); |
1737 | 0 | return PART_2Nx2N; |
1738 | 0 | } |
1739 | | |
1740 | | // CHECK_ME: I optimize code and fix bug here, need more VERIFY! |
1741 | 0 | int bit1 = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PART_MODE + 1]); |
1742 | 0 | if (cLog2CbSize > sps.Log2MinCbSizeY) { |
1743 | 0 | if (!sps.amp_enabled_flag) { |
1744 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", bit1 ? PART_2NxN : PART_Nx2N); |
1745 | 0 | return bit1 ? PART_2NxN : PART_Nx2N; |
1746 | 0 | } |
1747 | 0 | else { |
1748 | 0 | int bit3 = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PART_MODE + 3]); |
1749 | 0 | if (bit3) { |
1750 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", bit1 ? PART_2NxN : PART_Nx2N); |
1751 | 0 | return bit1 ? PART_2NxN : PART_Nx2N; |
1752 | 0 | } |
1753 | | |
1754 | 0 | int bit4 = tctx->cabac_decoder.decode_bypass(); |
1755 | 0 | if (bit1 && bit4) { |
1756 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_2NxnD); |
1757 | 0 | return PART_2NxnD; |
1758 | 0 | } |
1759 | 0 | if (bit1 && !bit4) { |
1760 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_2NxnU); |
1761 | 0 | return PART_2NxnU; |
1762 | 0 | } |
1763 | 0 | if (!bit1 && !bit4) { |
1764 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_nLx2N); |
1765 | 0 | return PART_nLx2N; |
1766 | 0 | } |
1767 | 0 | if (!bit1 && bit4) { |
1768 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_nRx2N); |
1769 | 0 | return PART_nRx2N; |
1770 | 0 | } |
1771 | 0 | } |
1772 | 0 | } |
1773 | 0 | 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 | 0 | if (bit1) { |
1778 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_2NxN); |
1779 | 0 | return PART_2NxN; |
1780 | 0 | } |
1781 | | |
1782 | 0 | if (cLog2CbSize == 3) { |
1783 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_Nx2N); |
1784 | 0 | return PART_Nx2N; |
1785 | 0 | } |
1786 | 0 | else { |
1787 | 0 | int bit2 = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PART_MODE + 2]); |
1788 | 0 | logtrace(LogSymbols, "$1 part_mode=%d\n", PART_NxN - bit2); |
1789 | 0 | return (enum PartMode) ((int) PART_NxN - bit2)/*bit2 ? PART_Nx2N : PART_NxN*/; |
1790 | 0 | } |
1791 | 0 | } |
1792 | 0 | } |
1793 | | |
1794 | 0 | assert(false); // should never be reached |
1795 | 0 | return PART_2Nx2N; |
1796 | 0 | } |
1797 | | |
1798 | | |
1799 | | static inline int decode_prev_intra_luma_pred_flag(thread_context* tctx) |
1800 | 0 | { |
1801 | 0 | logtrace(LogSlice, "# prev_intra_luma_pred_flag\n"); |
1802 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_PREV_INTRA_LUMA_PRED_FLAG]); |
1803 | 0 | logtrace(LogSymbols, "$1 prev_intra_luma_pred_flag=%d\n", bit); |
1804 | 0 | return bit; |
1805 | 0 | } |
1806 | | |
1807 | | |
1808 | | static inline int decode_mpm_idx(thread_context* tctx) |
1809 | 0 | { |
1810 | 0 | logtrace(LogSlice, "# mpm_idx (TU:2)\n"); |
1811 | 0 | int mpm = tctx->cabac_decoder.decode_TU_bypass( 2); |
1812 | 0 | logtrace(LogSlice, "> mpm_idx = %d\n", mpm); |
1813 | 0 | logtrace(LogSymbols, "$1 mpm_idx=%d\n", mpm); |
1814 | 0 | return mpm; |
1815 | 0 | } |
1816 | | |
1817 | | |
1818 | | static inline int decode_rem_intra_luma_pred_mode(thread_context* tctx) |
1819 | 0 | { |
1820 | 0 | logtrace(LogSlice, "# rem_intra_luma_pred_mode (5 bits)\n"); |
1821 | 0 | int value = tctx->cabac_decoder.decode_FL_bypass( 5); |
1822 | 0 | logtrace(LogSymbols, "$1 rem_intra_luma_pred_mode=%d\n", value); |
1823 | 0 | return value; |
1824 | 0 | } |
1825 | | |
1826 | | |
1827 | | static int decode_intra_chroma_pred_mode(thread_context* tctx) |
1828 | 0 | { |
1829 | 0 | logtrace(LogSlice, "# intra_chroma_pred_mode\n"); |
1830 | |
|
1831 | 0 | int prefix = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_INTRA_CHROMA_PRED_MODE]); |
1832 | |
|
1833 | 0 | int mode; |
1834 | 0 | if (prefix == 0) { |
1835 | 0 | mode = 4; |
1836 | 0 | } |
1837 | 0 | else { |
1838 | 0 | mode = tctx->cabac_decoder.decode_FL_bypass( 2); |
1839 | 0 | } |
1840 | |
|
1841 | 0 | logtrace(LogSlice, "> intra_chroma_pred_mode = %d\n", mode); |
1842 | 0 | logtrace(LogSymbols, "$1 intra_chroma_pred_mode=%d\n", mode); |
1843 | |
|
1844 | 0 | return mode; |
1845 | 0 | } |
1846 | | |
1847 | | |
1848 | | static int decode_split_transform_flag(thread_context* tctx, |
1849 | | int log2TrafoSize) |
1850 | 0 | { |
1851 | 0 | logtrace(LogSlice, "# split_transform_flag (log2TrafoSize=%d)\n", log2TrafoSize); |
1852 | |
|
1853 | 0 | int context = 5 - log2TrafoSize; |
1854 | 0 | assert(context >= 0 && context <= 2); |
1855 | | |
1856 | 0 | logtrace(LogSlice, "# context: %d\n", context); |
1857 | |
|
1858 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_SPLIT_TRANSFORM_FLAG + context]); |
1859 | 0 | logtrace(LogSymbols, "$1 split_transform_flag=%d\n", bit); |
1860 | 0 | return bit; |
1861 | 0 | } |
1862 | | |
1863 | | |
1864 | | static int decode_cbf_chroma(thread_context* tctx, |
1865 | | int trafoDepth) |
1866 | 0 | { |
1867 | 0 | logtrace(LogSlice, "# cbf_chroma\n"); |
1868 | |
|
1869 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_CBF_CHROMA + trafoDepth]); |
1870 | |
|
1871 | 0 | logtrace(LogSymbols, "$1 cbf_chroma=%d\n", bit); |
1872 | 0 | return bit; |
1873 | 0 | } |
1874 | | |
1875 | | |
1876 | | static int decode_cbf_luma(thread_context* tctx, |
1877 | | int trafoDepth) |
1878 | 0 | { |
1879 | 0 | logtrace(LogSlice, "# cbf_luma\n"); |
1880 | |
|
1881 | 0 | int bit = tctx->cabac_decoder.decode_bit( &tctx->ctx_model[CONTEXT_MODEL_CBF_LUMA + (trafoDepth == 0)]); |
1882 | |
|
1883 | 0 | logtrace(LogSlice, "> cbf_luma = %d\n", bit); |
1884 | |
|
1885 | 0 | logtrace(LogSymbols, "$1 cbf_luma=%d\n", bit); |
1886 | 0 | return bit; |
1887 | 0 | } |
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 | 0 | { |
1894 | 0 | logtrace(LogSlice, "# coded_sub_block_flag\n"); |
1895 | | |
1896 | | // tricky computation of csbfCtx |
1897 | 0 | int csbfCtx = ((coded_sub_block_neighbors & 1) | // right neighbor set or |
1898 | 0 | (coded_sub_block_neighbors >> 1)); // bottom neighbor set -> csbfCtx=1 |
1899 | |
|
1900 | 0 | int ctxIdxInc = csbfCtx; |
1901 | 0 | if (cIdx != 0) { |
1902 | 0 | ctxIdxInc += 2; |
1903 | 0 | } |
1904 | |
|
1905 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
1906 | 0 | &tctx->ctx_model[CONTEXT_MODEL_CODED_SUB_BLOCK_FLAG + ctxIdxInc]); |
1907 | |
|
1908 | 0 | logtrace(LogSymbols, "$1 coded_sub_block_flag=%d\n", bit); |
1909 | 0 | return bit; |
1910 | 0 | } |
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 | 0 | { |
1917 | 0 | logtrace(LogSlice, "# cu_qp_delta_abs\n"); |
1918 | |
|
1919 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
1920 | 0 | &tctx->ctx_model[CONTEXT_MODEL_CU_QP_DELTA_ABS + 0]); |
1921 | 0 | if (bit == 0) { |
1922 | 0 | logtrace(LogSymbols, "$1 cu_qp_delta_abs=%d\n", 0); |
1923 | 0 | return 0; |
1924 | 0 | } |
1925 | | |
1926 | 0 | uint8_t prefix = 1; |
1927 | 0 | for (uint8_t i = 0; i < 4; i++) { |
1928 | 0 | bit = tctx->cabac_decoder.decode_bit( |
1929 | 0 | &tctx->ctx_model[CONTEXT_MODEL_CU_QP_DELTA_ABS + 1]); |
1930 | 0 | if (bit == 0) { break; } |
1931 | 0 | else { prefix++; } |
1932 | 0 | } |
1933 | |
|
1934 | 0 | if (prefix == 5) { |
1935 | 0 | uint32_t value = tctx->cabac_decoder.decode_EGk_bypass( 0); |
1936 | 0 | if (value >= 250) { return CABAC_QP_DELTA_ABS_ERROR; } |
1937 | 0 | logtrace(LogSymbols, "$1 cu_qp_delta_abs=%d\n", value + 5); |
1938 | 0 | return value + 5; |
1939 | 0 | } |
1940 | 0 | else { |
1941 | 0 | logtrace(LogSymbols, "$1 cu_qp_delta_abs=%d\n", prefix); |
1942 | 0 | return prefix; |
1943 | 0 | } |
1944 | 0 | } |
1945 | | |
1946 | | |
1947 | | static int decode_last_significant_coeff_prefix(thread_context* tctx, |
1948 | | int log2TrafoSize, |
1949 | | int cIdx, |
1950 | | context_model* model) |
1951 | 0 | { |
1952 | 0 | logtrace(LogSlice, "# last_significant_coeff_prefix log2TrafoSize:%d cIdx:%d\n", log2TrafoSize, cIdx); |
1953 | |
|
1954 | 0 | int cMax = (log2TrafoSize << 1) - 1; |
1955 | |
|
1956 | 0 | int ctxOffset, ctxShift; |
1957 | 0 | if (cIdx == 0) { |
1958 | 0 | ctxOffset = 3 * (log2TrafoSize - 2) + ((log2TrafoSize - 1) >> 2); |
1959 | 0 | ctxShift = (log2TrafoSize + 1) >> 2; |
1960 | 0 | } |
1961 | 0 | else { |
1962 | 0 | ctxOffset = 15; |
1963 | 0 | ctxShift = log2TrafoSize - 2; |
1964 | 0 | } |
1965 | |
|
1966 | 0 | int binIdx; |
1967 | 0 | int value = cMax; |
1968 | 0 | for (binIdx = 0; binIdx < cMax; binIdx++) { |
1969 | 0 | int ctxIdxInc = (binIdx >> ctxShift); |
1970 | |
|
1971 | 0 | logtrace(LogSlice, "context: %d+%d\n", ctxOffset, ctxIdxInc); |
1972 | |
|
1973 | 0 | int bit = tctx->cabac_decoder.decode_bit( &model[ctxOffset + ctxIdxInc]); |
1974 | 0 | if (bit == 0) { |
1975 | 0 | value = binIdx; |
1976 | 0 | break; |
1977 | 0 | } |
1978 | 0 | } |
1979 | |
|
1980 | 0 | logtrace(LogSlice, "> last_significant_coeff_prefix: %d\n", value); |
1981 | |
|
1982 | 0 | return value; |
1983 | 0 | } |
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 | 2 | { |
1997 | 2 | int tableSize = 4 * 4 * (2) + 8 * 8 * (2 * 2 * 4) + 16 * 16 * (2 * 4) + 32 * 32 * (2 * 4); |
1998 | | |
1999 | 2 | uint8_t* p = (uint8_t*) malloc(tableSize); |
2000 | 2 | if (p == nullptr) { |
2001 | 0 | return false; |
2002 | 0 | } |
2003 | | |
2004 | 2 | 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 | 6 | for (int cIdx = 0; cIdx < 2; cIdx++) { |
2012 | 12 | for (int scanIdx = 0; scanIdx < 2; scanIdx++) |
2013 | 40 | for (int prevCsbf = 0; prevCsbf < 4; prevCsbf++) |
2014 | 32 | ctxIdxLookup[0][cIdx][scanIdx][prevCsbf] = p; |
2015 | | |
2016 | 4 | p += 4 * 4; |
2017 | 4 | } |
2018 | | |
2019 | | // 8x8 |
2020 | | |
2021 | 6 | for (int cIdx = 0; cIdx < 2; cIdx++) |
2022 | 12 | for (int scanIdx = 0; scanIdx < 2; scanIdx++) |
2023 | 40 | for (int prevCsbf = 0; prevCsbf < 4; prevCsbf++) { |
2024 | 32 | ctxIdxLookup[1][cIdx][scanIdx][prevCsbf] = p; |
2025 | 32 | p += 8 * 8; |
2026 | 32 | } |
2027 | | |
2028 | | // 16x16 |
2029 | | |
2030 | 6 | for (int cIdx = 0; cIdx < 2; cIdx++) |
2031 | 20 | for (int prevCsbf = 0; prevCsbf < 4; prevCsbf++) { |
2032 | 48 | for (int scanIdx = 0; scanIdx < 2; scanIdx++) { |
2033 | 32 | ctxIdxLookup[2][cIdx][scanIdx][prevCsbf] = p; |
2034 | 32 | } |
2035 | | |
2036 | 16 | p += 16 * 16; |
2037 | 16 | } |
2038 | | |
2039 | | // 32x32 |
2040 | | |
2041 | 6 | for (int cIdx = 0; cIdx < 2; cIdx++) |
2042 | 20 | for (int prevCsbf = 0; prevCsbf < 4; prevCsbf++) { |
2043 | 48 | for (int scanIdx = 0; scanIdx < 2; scanIdx++) { |
2044 | 32 | ctxIdxLookup[3][cIdx][scanIdx][prevCsbf] = p; |
2045 | 32 | } |
2046 | | |
2047 | 16 | p += 32 * 32; |
2048 | 16 | } |
2049 | | |
2050 | | |
2051 | | // --- precompute ctxIdx tables --- |
2052 | | |
2053 | 10 | for (int log2w = 2; log2w <= 5; log2w++) |
2054 | 24 | for (int cIdx = 0; cIdx < 2; cIdx++) |
2055 | 48 | for (int scanIdx = 0; scanIdx < 2; scanIdx++) |
2056 | 160 | for (int prevCsbf = 0; prevCsbf < 4; prevCsbf++) { |
2057 | 2.04k | for (int yC = 0; yC < (1 << log2w); yC++) |
2058 | 45.4k | for (int xC = 0; xC < (1 << log2w); xC++) { |
2059 | 43.5k | int w = 1 << log2w; |
2060 | 43.5k | int sbWidth = w >> 2; |
2061 | | |
2062 | 43.5k | int sigCtx; |
2063 | | |
2064 | | // if log2TrafoSize==2 |
2065 | 43.5k | if (sbWidth == 1) { |
2066 | 512 | sigCtx = ctxIdxMap[(yC << 2) + xC]; |
2067 | 512 | } |
2068 | 43.0k | else if (xC + yC == 0) { |
2069 | 96 | sigCtx = 0; |
2070 | 96 | } |
2071 | 42.9k | else { |
2072 | 42.9k | int xS = xC >> 2; |
2073 | 42.9k | 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 | 42.9k | int xP = xC & 3; |
2081 | 42.9k | int yP = yC & 3; |
2082 | | |
2083 | | //logtrace(LogSlice,"posInSubset: %d,%d\n",xP,yP); |
2084 | | //logtrace(LogSlice,"prevCsbf: %d\n",prevCsbf); |
2085 | | |
2086 | 42.9k | switch (prevCsbf) { |
2087 | 10.7k | case 0: |
2088 | 10.7k | sigCtx = (xP + yP >= 3) ? 0 : (xP + yP > 0) ? 1 : 2; |
2089 | 10.7k | break; |
2090 | 10.7k | case 1: |
2091 | 10.7k | sigCtx = (yP == 0) ? 2 : (yP == 1) ? 1 : 0; |
2092 | 10.7k | break; |
2093 | 10.7k | case 2: |
2094 | 10.7k | sigCtx = (xP == 0) ? 2 : (xP == 1) ? 1 : 0; |
2095 | 10.7k | break; |
2096 | 10.7k | default: |
2097 | 10.7k | sigCtx = 2; |
2098 | 10.7k | break; |
2099 | 42.9k | } |
2100 | | |
2101 | | //logtrace(LogSlice,"a) sigCtx=%d\n",sigCtx); |
2102 | | |
2103 | 42.9k | if (cIdx == 0) { |
2104 | 21.4k | if (xS + yS > 0) sigCtx += 3; |
2105 | | |
2106 | | //logtrace(LogSlice,"b) sigCtx=%d\n",sigCtx); |
2107 | | |
2108 | | // if log2TrafoSize==3 |
2109 | 21.4k | if (sbWidth == 2) { |
2110 | | // 8x8 block |
2111 | 1.00k | sigCtx += (scanIdx == 0) ? 9 : 15; |
2112 | 1.00k | } |
2113 | 20.4k | else { |
2114 | 20.4k | sigCtx += 21; |
2115 | 20.4k | } |
2116 | | |
2117 | | //logtrace(LogSlice,"c) sigCtx=%d\n",sigCtx); |
2118 | 21.4k | } |
2119 | 21.4k | else { |
2120 | | // if log2TrafoSize==3 |
2121 | 21.4k | if (sbWidth == 2) { |
2122 | | // 8x8 block |
2123 | 1.00k | sigCtx += 9; |
2124 | 1.00k | } |
2125 | 20.4k | else { |
2126 | 20.4k | sigCtx += 12; |
2127 | 20.4k | } |
2128 | 21.4k | } |
2129 | 42.9k | } |
2130 | | |
2131 | 43.5k | int ctxIdxInc; |
2132 | 43.5k | if (cIdx == 0) { ctxIdxInc = sigCtx; } |
2133 | 21.7k | else { ctxIdxInc = 27 + sigCtx; } |
2134 | | |
2135 | 43.5k | if (ctxIdxLookup[log2w - 2][cIdx][scanIdx][prevCsbf][xC + (yC << log2w)] != 0xFF) { |
2136 | 20.9k | assert(ctxIdxLookup[log2w-2][cIdx][scanIdx][prevCsbf][xC+(yC<<log2w)] == ctxIdxInc); |
2137 | 20.9k | } |
2138 | | |
2139 | 43.5k | 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 | 43.5k | } |
2144 | 128 | } |
2145 | | |
2146 | 2 | return true; |
2147 | 2 | } |
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 | 0 | { |
2376 | 0 | logtrace(LogSlice, "# significant_coeff_flag\n"); |
2377 | 0 | logtrace(LogSlice, "context: %d\n", ctxIdxInc); |
2378 | |
|
2379 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2380 | 0 | &tctx->ctx_model[CONTEXT_MODEL_SIGNIFICANT_COEFF_FLAG + ctxIdxInc]); |
2381 | |
|
2382 | 0 | logtrace(LogSymbols, "$1 significant_coeff_flag=%d\n", bit); |
2383 | |
|
2384 | 0 | return bit; |
2385 | 0 | } |
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 | 0 | { |
2397 | 0 | logtrace(LogSlice, "# coeff_abs_level_greater1\n"); |
2398 | |
|
2399 | 0 | 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 | 0 | *lastInvocation_greater1Ctx, |
2401 | 0 | *lastInvocation_coeff_abs_level_greater1_flag, |
2402 | 0 | *lastInvocation_ctxSet); |
2403 | |
|
2404 | 0 | int lastGreater1Ctx; |
2405 | 0 | int greater1Ctx; |
2406 | 0 | int ctxSet; |
2407 | |
|
2408 | 0 | logtrace(LogSlice, "c1: %d\n", c1); |
2409 | |
|
2410 | 0 | if (firstCoeffInSubblock) { |
2411 | | // block with real DC -> ctx 0 |
2412 | 0 | if (i == 0 || cIdx > 0) { ctxSet = 0; } |
2413 | 0 | else { ctxSet = 2; } |
2414 | |
|
2415 | 0 | if (firstSubblock) { lastGreater1Ctx = 1; } |
2416 | 0 | else { lastGreater1Ctx = lastSubblock_greater1Ctx; } |
2417 | |
|
2418 | 0 | if (lastGreater1Ctx == 0) { ctxSet++; } |
2419 | |
|
2420 | 0 | logtrace(LogSlice, "ctxSet: %d\n", ctxSet); |
2421 | |
|
2422 | 0 | greater1Ctx = 1; |
2423 | 0 | } |
2424 | 0 | else { |
2425 | | // !firstCoeffInSubblock |
2426 | 0 | ctxSet = *lastInvocation_ctxSet; |
2427 | 0 | logtrace(LogSlice, "ctxSet (old): %d\n", ctxSet); |
2428 | |
|
2429 | 0 | greater1Ctx = *lastInvocation_greater1Ctx; |
2430 | 0 | if (greater1Ctx > 0) { |
2431 | 0 | int lastGreater1Flag = *lastInvocation_coeff_abs_level_greater1_flag; |
2432 | 0 | if (lastGreater1Flag == 1) greater1Ctx = 0; |
2433 | 0 | else { |
2434 | | /*if (greater1Ctx>0)*/ |
2435 | 0 | greater1Ctx++; |
2436 | 0 | } |
2437 | 0 | } |
2438 | 0 | } |
2439 | |
|
2440 | 0 | ctxSet = c1; // use HM algo |
2441 | |
|
2442 | 0 | int ctxIdxInc = (ctxSet * 4) + (greater1Ctx >= 3 ? 3 : greater1Ctx); |
2443 | |
|
2444 | 0 | if (cIdx > 0) { ctxIdxInc += 16; } |
2445 | |
|
2446 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2447 | 0 | &tctx->ctx_model[CONTEXT_MODEL_COEFF_ABS_LEVEL_GREATER1_FLAG + ctxIdxInc]); |
2448 | |
|
2449 | 0 | *lastInvocation_greater1Ctx = greater1Ctx; |
2450 | 0 | *lastInvocation_coeff_abs_level_greater1_flag = bit; |
2451 | 0 | *lastInvocation_ctxSet = ctxSet; |
2452 | | |
2453 | | //logtrace(LogSymbols,"$1 coeff_abs_level_greater1=%d\n",bit); |
2454 | |
|
2455 | 0 | return bit; |
2456 | 0 | } |
2457 | | |
2458 | | |
2459 | | static int decode_coeff_abs_level_greater2(thread_context* tctx, |
2460 | | int cIdx, // int i,int n, |
2461 | | int ctxSet) |
2462 | 0 | { |
2463 | 0 | logtrace(LogSlice, "# coeff_abs_level_greater2\n"); |
2464 | |
|
2465 | 0 | int ctxIdxInc = ctxSet; |
2466 | |
|
2467 | 0 | if (cIdx > 0) ctxIdxInc += 4; |
2468 | |
|
2469 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2470 | 0 | &tctx->ctx_model[CONTEXT_MODEL_COEFF_ABS_LEVEL_GREATER2_FLAG + ctxIdxInc]); |
2471 | |
|
2472 | 0 | logtrace(LogSymbols, "$1 coeff_abs_level_greater2=%d\n", bit); |
2473 | |
|
2474 | 0 | return bit; |
2475 | 0 | } |
2476 | | |
2477 | | |
2478 | 0 | #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 | 0 | #define MAX_RICE_PARAM 29 |
2487 | 0 | #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 | 0 | { |
2492 | 0 | logtrace(LogSlice, "# decode_coeff_abs_level_remaining\n"); |
2493 | |
|
2494 | 0 | uint32_t prefix = 0; |
2495 | 0 | while (tctx->cabac_decoder.decode_bypass()) { |
2496 | 0 | prefix++; |
2497 | 0 | if (prefix > MAX_PREFIX) { |
2498 | 0 | return 0; // TODO: error |
2499 | 0 | } |
2500 | 0 | } |
2501 | | |
2502 | | // prefix = nb. 1 bits |
2503 | | |
2504 | 0 | int32_t value; |
2505 | |
|
2506 | 0 | if (prefix <= 3) { |
2507 | | // when code only TR part (level < TRMax) |
2508 | |
|
2509 | 0 | int codeword = tctx->cabac_decoder.decode_FL_bypass( cRiceParam); |
2510 | 0 | value = (prefix << cRiceParam) + codeword; |
2511 | 0 | } |
2512 | 0 | else { |
2513 | | // Suffix coded with EGk. Note that the unary part of EGk is already |
2514 | | // included in the 'prefix' counter above. |
2515 | |
|
2516 | 0 | int codeword = tctx->cabac_decoder.decode_FL_bypass( prefix - 3 + cRiceParam); |
2517 | 0 | value = (((UINT32_C(1) << (prefix - 3)) + 3 - 1) << cRiceParam) + codeword; |
2518 | 0 | } |
2519 | |
|
2520 | 0 | logtrace(LogSymbols, "$1 coeff_abs_level_remaining=%d\n", value); |
2521 | |
|
2522 | 0 | return value; |
2523 | 0 | } |
2524 | | |
2525 | | |
2526 | | static int decode_merge_flag(thread_context* tctx) |
2527 | 0 | { |
2528 | 0 | logtrace(LogSlice, "# merge_flag\n"); |
2529 | |
|
2530 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2531 | 0 | &tctx->ctx_model[CONTEXT_MODEL_MERGE_FLAG]); |
2532 | |
|
2533 | 0 | logtrace(LogSymbols, "$1 merge_flag=%d\n", bit); |
2534 | |
|
2535 | 0 | return bit; |
2536 | 0 | } |
2537 | | |
2538 | | |
2539 | | static int decode_merge_idx(thread_context* tctx) |
2540 | 0 | { |
2541 | 0 | logtrace(LogSlice, "# merge_idx\n"); |
2542 | |
|
2543 | 0 | if (tctx->shdr->MaxNumMergeCand <= 1) { |
2544 | 0 | logtrace(LogSymbols, "$1 merge_idx=%d\n", 0); |
2545 | 0 | return 0; |
2546 | 0 | } |
2547 | | |
2548 | | // TU coding, first bin is CABAC, remaining are bypass. |
2549 | | // cMax = MaxNumMergeCand-1 |
2550 | | |
2551 | 0 | int idx = tctx->cabac_decoder.decode_bit( |
2552 | 0 | &tctx->ctx_model[CONTEXT_MODEL_MERGE_IDX]); |
2553 | |
|
2554 | 0 | if (idx == 0) { |
2555 | | // nothing |
2556 | 0 | } |
2557 | 0 | else { |
2558 | 0 | idx = 1; |
2559 | |
|
2560 | 0 | while (idx < tctx->shdr->MaxNumMergeCand - 1) { |
2561 | 0 | if (tctx->cabac_decoder.decode_bypass()) { |
2562 | 0 | idx++; |
2563 | 0 | } |
2564 | 0 | else { |
2565 | 0 | break; |
2566 | 0 | } |
2567 | 0 | } |
2568 | 0 | } |
2569 | |
|
2570 | 0 | logtrace(LogSlice, "> merge_idx = %d\n", idx); |
2571 | 0 | logtrace(LogSymbols, "$1 merge_idx=%d\n", idx); |
2572 | |
|
2573 | 0 | return idx; |
2574 | 0 | } |
2575 | | |
2576 | | |
2577 | | static int decode_pred_mode_flag(thread_context* tctx) |
2578 | 0 | { |
2579 | 0 | logtrace(LogSlice, "# pred_mode_flag\n"); |
2580 | |
|
2581 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2582 | 0 | &tctx->ctx_model[CONTEXT_MODEL_PRED_MODE_FLAG]); |
2583 | |
|
2584 | 0 | logtrace(LogSymbols, "$1 pred_mode=%d\n", bit); |
2585 | 0 | return bit; |
2586 | 0 | } |
2587 | | |
2588 | | static int decode_mvp_lx_flag(thread_context* tctx) |
2589 | 0 | { |
2590 | 0 | logtrace(LogSlice, "# mvp_lx_flag\n"); |
2591 | |
|
2592 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2593 | 0 | &tctx->ctx_model[CONTEXT_MODEL_MVP_LX_FLAG]); |
2594 | |
|
2595 | 0 | logtrace(LogSymbols, "$1 mvp_lx_flag=%d\n", bit); |
2596 | 0 | return bit; |
2597 | 0 | } |
2598 | | |
2599 | | static int decode_rqt_root_cbf(thread_context* tctx) |
2600 | 0 | { |
2601 | 0 | logtrace(LogSlice, "# rqt_root_cbf\n"); |
2602 | |
|
2603 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2604 | 0 | &tctx->ctx_model[CONTEXT_MODEL_RQT_ROOT_CBF]); |
2605 | |
|
2606 | 0 | logtrace(LogSymbols, "$1 rqt_root_cbf=%d\n", bit); |
2607 | 0 | return bit; |
2608 | 0 | } |
2609 | | |
2610 | | static int decode_ref_idx_lX(thread_context* tctx, int numRefIdxLXActive) |
2611 | 0 | { |
2612 | | // prevent endless loop when 'numRefIdxLXActive' is invalid |
2613 | 0 | if (numRefIdxLXActive <= 1) { |
2614 | 0 | return 0; |
2615 | 0 | } |
2616 | | |
2617 | 0 | logtrace(LogSlice, "# ref_idx_lX\n"); |
2618 | |
|
2619 | 0 | int cMax = numRefIdxLXActive - 1; |
2620 | |
|
2621 | 0 | 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 | 0 | int bit = tctx->cabac_decoder.decode_bit( |
2627 | 0 | &tctx->ctx_model[CONTEXT_MODEL_REF_IDX_LX + 0]); |
2628 | |
|
2629 | 0 | int idx = 0; |
2630 | |
|
2631 | 0 | while (bit) { |
2632 | 0 | idx++; |
2633 | 0 | if (idx == cMax) { break; } |
2634 | | |
2635 | 0 | if (idx == 1) { |
2636 | 0 | bit = tctx->cabac_decoder.decode_bit( |
2637 | 0 | &tctx->ctx_model[CONTEXT_MODEL_REF_IDX_LX + 1]); |
2638 | 0 | } |
2639 | 0 | else { |
2640 | 0 | bit = tctx->cabac_decoder.decode_bypass(); |
2641 | 0 | } |
2642 | 0 | } |
2643 | |
|
2644 | 0 | logtrace(LogSlice, "> ref_idx = %d\n", idx); |
2645 | |
|
2646 | 0 | logtrace(LogSymbols, "$1 ref_idx_lX=%d\n", idx); |
2647 | 0 | return idx; |
2648 | 0 | } |
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 | 0 | { |
2656 | 0 | logtrace(LogSlice, "# inter_pred_idc\n"); |
2657 | |
|
2658 | 0 | int value; |
2659 | |
|
2660 | 0 | context_model* model = &tctx->ctx_model[CONTEXT_MODEL_INTER_PRED_IDC]; |
2661 | |
|
2662 | 0 | if (nPbW + nPbH == 12) { |
2663 | 0 | value = tctx->cabac_decoder.decode_bit( |
2664 | 0 | &model[4]); |
2665 | 0 | } |
2666 | 0 | else { |
2667 | 0 | int bit0 = tctx->cabac_decoder.decode_bit( |
2668 | 0 | &model[ctDepth]); |
2669 | 0 | if (bit0 == 0) { |
2670 | 0 | value = tctx->cabac_decoder.decode_bit( |
2671 | 0 | &model[4]); |
2672 | 0 | } |
2673 | 0 | else { |
2674 | 0 | value = 2; |
2675 | 0 | } |
2676 | 0 | } |
2677 | |
|
2678 | 0 | logtrace(LogSlice, "> inter_pred_idc = %d (%s)\n", value, |
2679 | 0 | value == 0 ? "L0" : (value == 1 ? "L1" : "BI")); |
2680 | |
|
2681 | 0 | logtrace(LogSymbols, "$1 decode_inter_pred_idx=%d\n", value + 1); |
2682 | |
|
2683 | 0 | return (enum InterPredIdc) (value + 1); |
2684 | 0 | } |
2685 | | |
2686 | | |
2687 | | static int decode_explicit_rdpcm_flag(thread_context* tctx, int cIdx) |
2688 | 0 | { |
2689 | 0 | context_model* model = &tctx->ctx_model[CONTEXT_MODEL_RDPCM_FLAG]; |
2690 | 0 | int value = tctx->cabac_decoder.decode_bit( &model[cIdx ? 1 : 0]); |
2691 | 0 | return value; |
2692 | 0 | } |
2693 | | |
2694 | | |
2695 | | static int decode_explicit_rdpcm_dir(thread_context* tctx, int cIdx) |
2696 | 0 | { |
2697 | 0 | context_model* model = &tctx->ctx_model[CONTEXT_MODEL_RDPCM_DIR]; |
2698 | 0 | int value = tctx->cabac_decoder.decode_bit( &model[cIdx ? 1 : 0]); |
2699 | 0 | return value; |
2700 | 0 | } |
2701 | | |
2702 | | |
2703 | | /* Take CtbAddrInTS and compute |
2704 | | -> CtbAddrInRS, CtbX, CtbY |
2705 | | */ |
2706 | | bool setCtbAddrFromTS(thread_context* tctx) |
2707 | 0 | { |
2708 | 0 | const seq_parameter_set& sps = tctx->img->get_sps(); |
2709 | |
|
2710 | 0 | if (tctx->CtbAddrInTS < sps.PicSizeInCtbsY) { |
2711 | 0 | tctx->CtbAddrInRS = tctx->img->get_pps().CtbAddrTStoRS[tctx->CtbAddrInTS]; |
2712 | |
|
2713 | 0 | tctx->CtbX = tctx->CtbAddrInRS % sps.PicWidthInCtbsY; |
2714 | 0 | tctx->CtbY = tctx->CtbAddrInRS / sps.PicWidthInCtbsY; |
2715 | 0 | return false; |
2716 | 0 | } |
2717 | 0 | else { |
2718 | 0 | tctx->CtbAddrInRS = sps.PicSizeInCtbsY; |
2719 | |
|
2720 | 0 | tctx->CtbX = tctx->CtbAddrInRS % sps.PicWidthInCtbsY; |
2721 | 0 | tctx->CtbY = tctx->CtbAddrInRS / sps.PicWidthInCtbsY; |
2722 | 0 | return true; |
2723 | 0 | } |
2724 | 0 | } |
2725 | | |
2726 | | // returns true when we reached the end of the image (ctbAddr==picSizeInCtbsY) |
2727 | | bool advanceCtbAddr(thread_context* tctx) |
2728 | 0 | { |
2729 | 0 | tctx->CtbAddrInTS++; |
2730 | |
|
2731 | 0 | return setCtbAddrFromTS(tctx); |
2732 | 0 | } |
2733 | | |
2734 | | |
2735 | | void read_sao(thread_context* tctx, int xCtb, int yCtb, |
2736 | | int CtbAddrInSliceSeg) |
2737 | 0 | { |
2738 | 0 | slice_segment_header* shdr = tctx->shdr; |
2739 | 0 | de265_image* img = tctx->img; |
2740 | 0 | const seq_parameter_set& sps = img->get_sps(); |
2741 | 0 | const pic_parameter_set& pps = img->get_pps(); |
2742 | |
|
2743 | 0 | logtrace(LogSlice, "# read_sao(%d,%d)\n", xCtb, yCtb); |
2744 | |
|
2745 | 0 | sao_info saoinfo; |
2746 | 0 | memset(&saoinfo, 0, sizeof(sao_info)); |
2747 | 0 | logtrace(LogSlice, "sizeof saoinfo: %d\n", sizeof(sao_info)); |
2748 | | |
2749 | |
|
2750 | 0 | char sao_merge_left_flag = 0; |
2751 | 0 | char sao_merge_up_flag = 0; |
2752 | |
|
2753 | 0 | if (xCtb > 0) { |
2754 | | //char leftCtbInSliceSeg = (CtbAddrInSliceSeg>0); |
2755 | 0 | char leftCtbInSliceSeg = (tctx->CtbAddrInRS > shdr->SliceAddrRS); |
2756 | 0 | char leftCtbInTile = (pps.TileIdRS[xCtb + yCtb * sps.PicWidthInCtbsY] == |
2757 | 0 | pps.TileIdRS[xCtb - 1 + yCtb * sps.PicWidthInCtbsY]); |
2758 | |
|
2759 | 0 | if (leftCtbInSliceSeg && leftCtbInTile) { |
2760 | 0 | sao_merge_left_flag = decode_sao_merge_flag(tctx); |
2761 | 0 | logtrace(LogSlice, "sao_merge_left_flag: %d\n", sao_merge_left_flag); |
2762 | 0 | } |
2763 | 0 | } |
2764 | |
|
2765 | 0 | if (yCtb > 0 && sao_merge_left_flag == 0) { |
2766 | 0 | logtrace(LogSlice, "CtbAddrInRS:%d PicWidthInCtbsY:%d slice_segment_address:%d\n", |
2767 | 0 | tctx->CtbAddrInRS, |
2768 | 0 | sps.PicWidthInCtbsY, |
2769 | 0 | shdr->slice_segment_address); |
2770 | 0 | bool upCtbInSliceSeg = (tctx->CtbAddrInRS - sps.PicWidthInCtbsY) >= shdr->SliceAddrRS; |
2771 | 0 | bool upCtbInTile = (pps.TileIdRS[xCtb + yCtb * sps.PicWidthInCtbsY] == |
2772 | 0 | pps.TileIdRS[xCtb + (yCtb - 1) * sps.PicWidthInCtbsY]); |
2773 | |
|
2774 | 0 | if (upCtbInSliceSeg && upCtbInTile) { |
2775 | 0 | sao_merge_up_flag = decode_sao_merge_flag(tctx); |
2776 | 0 | logtrace(LogSlice, "sao_merge_up_flag: %d\n", sao_merge_up_flag); |
2777 | 0 | } |
2778 | 0 | } |
2779 | |
|
2780 | 0 | if (!sao_merge_up_flag && !sao_merge_left_flag) { |
2781 | 0 | int nChroma = 3; |
2782 | 0 | if (sps.ChromaArrayType == CHROMA_MONO) nChroma = 1; |
2783 | |
|
2784 | 0 | for (int cIdx = 0; cIdx < nChroma; cIdx++) { |
2785 | 0 | if ((shdr->slice_sao_luma_flag && cIdx == 0) || |
2786 | 0 | (shdr->slice_sao_chroma_flag && cIdx > 0)) { |
2787 | 0 | uint8_t SaoTypeIdx = 0; |
2788 | |
|
2789 | 0 | if (cIdx == 0) { |
2790 | 0 | uint8_t sao_type_idx_luma = decode_sao_type_idx(tctx); |
2791 | 0 | logtrace(LogSlice, "sao_type_idx_luma: %d\n", sao_type_idx_luma); |
2792 | 0 | saoinfo.SaoTypeIdx = SaoTypeIdx = sao_type_idx_luma; |
2793 | 0 | } |
2794 | 0 | else if (cIdx == 1) { |
2795 | 0 | uint8_t sao_type_idx_chroma = decode_sao_type_idx(tctx); |
2796 | 0 | logtrace(LogSlice, "sao_type_idx_chroma: %d\n", sao_type_idx_chroma); |
2797 | 0 | SaoTypeIdx = sao_type_idx_chroma; |
2798 | 0 | saoinfo.SaoTypeIdx |= SaoTypeIdx << (2 * 1); |
2799 | 0 | saoinfo.SaoTypeIdx |= SaoTypeIdx << (2 * 2); // set for both chroma components |
2800 | 0 | } |
2801 | 0 | else { |
2802 | | // SaoTypeIdx = 0 |
2803 | |
|
2804 | 0 | SaoTypeIdx = (saoinfo.SaoTypeIdx >> (2 * cIdx)) & 0x3; |
2805 | 0 | } |
2806 | |
|
2807 | 0 | if (SaoTypeIdx != 0) { |
2808 | 0 | for (int i = 0; i < 4; i++) { |
2809 | 0 | saoinfo.saoOffsetVal[cIdx][i] = decode_sao_offset_abs(tctx, img->get_bit_depth(cIdx)); |
2810 | 0 | logtrace(LogSlice, "saoOffsetVal[%d][%d] = %d\n", cIdx, i, saoinfo.saoOffsetVal[cIdx][i]); |
2811 | 0 | } |
2812 | |
|
2813 | 0 | int sign[4]; |
2814 | 0 | if (SaoTypeIdx == 1) { |
2815 | 0 | for (int i = 0; i < 4; i++) { |
2816 | 0 | if (saoinfo.saoOffsetVal[cIdx][i] != 0) { |
2817 | 0 | sign[i] = decode_sao_offset_sign(tctx) ? -1 : 1; |
2818 | 0 | } |
2819 | 0 | else { |
2820 | 0 | sign[i] = 0; // not really required, but compiler warns about uninitialized values |
2821 | 0 | } |
2822 | 0 | } |
2823 | |
|
2824 | 0 | saoinfo.sao_band_position[cIdx] = decode_sao_band_position(tctx); |
2825 | 0 | } |
2826 | 0 | else { |
2827 | 0 | uint8_t SaoEoClass = 0; |
2828 | |
|
2829 | 0 | sign[0] = sign[1] = 1; |
2830 | 0 | sign[2] = sign[3] = -1; |
2831 | |
|
2832 | 0 | if (cIdx == 0) { |
2833 | 0 | saoinfo.SaoEoClass = SaoEoClass = decode_sao_class(tctx); |
2834 | 0 | } |
2835 | 0 | else if (cIdx == 1) { |
2836 | 0 | SaoEoClass = decode_sao_class(tctx); |
2837 | 0 | saoinfo.SaoEoClass |= SaoEoClass << (2 * 1); |
2838 | 0 | saoinfo.SaoEoClass |= SaoEoClass << (2 * 2); |
2839 | 0 | } |
2840 | |
|
2841 | 0 | logtrace(LogSlice, "SaoEoClass[%d] = %d\n", cIdx, SaoEoClass); |
2842 | 0 | } |
2843 | |
|
2844 | 0 | int log2OffsetScale; |
2845 | |
|
2846 | 0 | if (cIdx == 0) { |
2847 | 0 | log2OffsetScale = pps.range_extension.log2_sao_offset_scale_luma; |
2848 | 0 | } |
2849 | 0 | else { |
2850 | 0 | log2OffsetScale = pps.range_extension.log2_sao_offset_scale_chroma; |
2851 | 0 | } |
2852 | |
|
2853 | 0 | for (int i = 0; i < 4; i++) { |
2854 | 0 | saoinfo.saoOffsetVal[cIdx][i] = sign[i] * (saoinfo.saoOffsetVal[cIdx][i] << log2OffsetScale); |
2855 | 0 | } |
2856 | 0 | } |
2857 | 0 | } |
2858 | 0 | } |
2859 | |
|
2860 | 0 | img->set_sao_info(xCtb, yCtb, &saoinfo); |
2861 | 0 | } |
2862 | | |
2863 | |
|
2864 | 0 | if (sao_merge_left_flag) { |
2865 | 0 | img->set_sao_info(xCtb, yCtb, img->get_sao_info(xCtb - 1, yCtb)); |
2866 | 0 | } |
2867 | |
|
2868 | 0 | if (sao_merge_up_flag) { |
2869 | 0 | img->set_sao_info(xCtb, yCtb, img->get_sao_info(xCtb, yCtb - 1)); |
2870 | 0 | } |
2871 | 0 | } |
2872 | | |
2873 | | |
2874 | | void read_coding_tree_unit(thread_context* tctx) |
2875 | 0 | { |
2876 | 0 | slice_segment_header* shdr = tctx->shdr; |
2877 | 0 | de265_image* img = tctx->img; |
2878 | 0 | const seq_parameter_set& sps = img->get_sps(); |
2879 | |
|
2880 | 0 | int xCtb = (tctx->CtbAddrInRS % sps.PicWidthInCtbsY); |
2881 | 0 | int yCtb = (tctx->CtbAddrInRS / sps.PicWidthInCtbsY); |
2882 | 0 | int xCtbPixels = xCtb << sps.Log2CtbSizeY; |
2883 | 0 | int yCtbPixels = yCtb << sps.Log2CtbSizeY; |
2884 | |
|
2885 | 0 | logtrace(LogSlice, "----- decode CTB %d;%d (%d;%d) POC=%d, SliceAddrRS=%d\n", |
2886 | 0 | xCtbPixels, yCtbPixels, xCtb, yCtb, |
2887 | 0 | tctx->img->PicOrderCntVal, tctx->shdr->SliceAddrRS); |
2888 | |
|
2889 | 0 | img->set_SliceAddrRS(xCtb, yCtb, tctx->shdr->SliceAddrRS); |
2890 | |
|
2891 | 0 | img->set_SliceHeaderIndex(xCtbPixels, yCtbPixels, shdr->slice_index); |
2892 | |
|
2893 | 0 | int CtbAddrInSliceSeg = tctx->CtbAddrInRS - shdr->slice_segment_address; |
2894 | |
|
2895 | 0 | if (shdr->slice_sao_luma_flag || shdr->slice_sao_chroma_flag) { |
2896 | 0 | read_sao(tctx, xCtb, yCtb, CtbAddrInSliceSeg); |
2897 | 0 | } |
2898 | |
|
2899 | 0 | read_coding_quadtree(tctx, xCtbPixels, yCtbPixels, sps.Log2CtbSizeY, 0); |
2900 | 0 | } |
2901 | | |
2902 | | |
2903 | | LIBDE265_INLINE static int luma_pos_to_ctbAddrRS(const seq_parameter_set* sps, int x, int y) |
2904 | 0 | { |
2905 | 0 | int ctbX = x >> sps->Log2CtbSizeY; |
2906 | 0 | int ctbY = y >> sps->Log2CtbSizeY; |
2907 | |
|
2908 | 0 | return ctbY * sps->PicWidthInCtbsY + ctbX; |
2909 | 0 | } |
2910 | | |
2911 | | |
2912 | | int check_CTB_available(const de265_image* img, |
2913 | | int xC, int yC, int xN, int yN) |
2914 | 0 | { |
2915 | | // check whether neighbor is outside of frame |
2916 | |
|
2917 | 0 | if (xN < 0 || yN < 0) { return 0; } |
2918 | 0 | if (xN >= img->get_sps().pic_width_in_luma_samples) { return 0; } |
2919 | 0 | if (yN >= img->get_sps().pic_height_in_luma_samples) { return 0; } |
2920 | | |
2921 | | |
2922 | 0 | int current_ctbAddrRS = luma_pos_to_ctbAddrRS(&img->get_sps(), xC, yC); |
2923 | 0 | 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 | 0 | if (img->get_SliceAddrRS_atCtbRS(current_ctbAddrRS) != |
2928 | 0 | img->get_SliceAddrRS_atCtbRS(neighbor_ctbAddrRS)) { |
2929 | 0 | return 0; |
2930 | 0 | } |
2931 | | |
2932 | | // check if both CTBs are in the same tile. |
2933 | | |
2934 | 0 | if (img->get_pps().TileIdRS[current_ctbAddrRS] != |
2935 | 0 | img->get_pps().TileIdRS[neighbor_ctbAddrRS]) { |
2936 | 0 | return 0; |
2937 | 0 | } |
2938 | | |
2939 | 0 | return 1; |
2940 | 0 | } |
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 | 0 | { |
2948 | 0 | 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 | 0 | de265_image* img = tctx->img; |
2953 | 0 | const seq_parameter_set& sps = img->get_sps(); |
2954 | 0 | const pic_parameter_set& pps = img->get_pps(); |
2955 | |
|
2956 | 0 | enum PredMode PredMode = img->get_pred_mode(x0, y0); |
2957 | |
|
2958 | 0 | if (cIdx == 0) { |
2959 | 0 | img->set_nonzero_coefficient(x0, y0, log2TrafoSize); |
2960 | 0 | } |
2961 | | |
2962 | |
|
2963 | 0 | if (pps.transform_skip_enabled_flag && |
2964 | 0 | !tctx->cu_transquant_bypass_flag && |
2965 | 0 | (log2TrafoSize <= pps.Log2MaxTransformSkipSize)) { |
2966 | 0 | tctx->transform_skip_flag[cIdx] = decode_transform_skip_flag(tctx, cIdx); |
2967 | 0 | } |
2968 | 0 | else { |
2969 | 0 | tctx->transform_skip_flag[cIdx] = 0; |
2970 | 0 | } |
2971 | | |
2972 | |
|
2973 | 0 | tctx->explicit_rdpcm_flag = false; |
2974 | |
|
2975 | 0 | if (PredMode == MODE_INTER && sps.range_extension.explicit_rdpcm_enabled_flag && |
2976 | 0 | (tctx->transform_skip_flag[cIdx] || tctx->cu_transquant_bypass_flag)) { |
2977 | 0 | tctx->explicit_rdpcm_flag = decode_explicit_rdpcm_flag(tctx, cIdx); |
2978 | 0 | if (tctx->explicit_rdpcm_flag) { |
2979 | 0 | tctx->explicit_rdpcm_dir = decode_explicit_rdpcm_dir(tctx, cIdx); |
2980 | 0 | } |
2981 | | |
2982 | | //printf("EXPLICIT RDPCM %d;%d\n",x0,y0); |
2983 | 0 | } |
2984 | 0 | else { |
2985 | 0 | tctx->explicit_rdpcm_flag = false; |
2986 | 0 | } |
2987 | | |
2988 | | |
2989 | | // sbType for persistent_rice_adaptation_enabled_flag |
2990 | |
|
2991 | 0 | int sbType = (cIdx == 0) ? 2 : 0; |
2992 | 0 | if (tctx->transform_skip_flag[cIdx] || tctx->cu_transquant_bypass_flag) { |
2993 | 0 | sbType++; |
2994 | 0 | } |
2995 | | |
2996 | | |
2997 | | // --- decode position of last coded coefficient --- |
2998 | |
|
2999 | 0 | int last_significant_coeff_x_prefix = |
3000 | 0 | decode_last_significant_coeff_prefix(tctx, log2TrafoSize, cIdx, |
3001 | 0 | &tctx->ctx_model[CONTEXT_MODEL_LAST_SIGNIFICANT_COEFFICIENT_X_PREFIX]); |
3002 | |
|
3003 | 0 | int last_significant_coeff_y_prefix = |
3004 | 0 | decode_last_significant_coeff_prefix(tctx, log2TrafoSize, cIdx, |
3005 | 0 | &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 | 0 | int LastSignificantCoeffX; |
3011 | 0 | if (last_significant_coeff_x_prefix > 3) { |
3012 | 0 | int nBits = (last_significant_coeff_x_prefix >> 1) - 1; |
3013 | 0 | int last_significant_coeff_x_suffix = tctx->cabac_decoder.decode_FL_bypass( nBits); |
3014 | |
|
3015 | 0 | LastSignificantCoeffX = |
3016 | 0 | ((2 + (last_significant_coeff_x_prefix & 1)) << nBits) + last_significant_coeff_x_suffix; |
3017 | 0 | } |
3018 | 0 | else { |
3019 | 0 | LastSignificantCoeffX = last_significant_coeff_x_prefix; |
3020 | 0 | } |
3021 | |
|
3022 | 0 | int LastSignificantCoeffY; |
3023 | 0 | if (last_significant_coeff_y_prefix > 3) { |
3024 | 0 | int nBits = (last_significant_coeff_y_prefix >> 1) - 1; |
3025 | 0 | int last_significant_coeff_y_suffix = tctx->cabac_decoder.decode_FL_bypass( nBits); |
3026 | |
|
3027 | 0 | LastSignificantCoeffY = |
3028 | 0 | ((2 + (last_significant_coeff_y_prefix & 1)) << nBits) + last_significant_coeff_y_suffix; |
3029 | 0 | } |
3030 | 0 | else { |
3031 | 0 | LastSignificantCoeffY = last_significant_coeff_y_prefix; |
3032 | 0 | } |
3033 | | |
3034 | | |
3035 | | // --- determine scanIdx --- |
3036 | |
|
3037 | 0 | int scanIdx; |
3038 | |
|
3039 | 0 | if (PredMode == MODE_INTRA) { |
3040 | 0 | if (cIdx == 0) { |
3041 | 0 | 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 | 0 | } |
3044 | 0 | else { |
3045 | 0 | 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 | 0 | } |
3049 | 0 | } |
3050 | 0 | else { |
3051 | 0 | scanIdx = 0; |
3052 | 0 | } |
3053 | |
|
3054 | 0 | if (scanIdx == 2) { |
3055 | 0 | std::swap(LastSignificantCoeffX, LastSignificantCoeffY); |
3056 | 0 | } |
3057 | |
|
3058 | 0 | logtrace(LogSlice, "LastSignificantCoeff: x=%d;y=%d\n", LastSignificantCoeffX, LastSignificantCoeffY); |
3059 | |
|
3060 | 0 | const position* ScanOrderSub = get_scan_order(log2TrafoSize - 2, scanIdx); |
3061 | 0 | const position* ScanOrderPos = get_scan_order(2, scanIdx); |
3062 | |
|
3063 | 0 | logtrace(LogSlice, "ScanOrderPos: "); |
3064 | 0 | for (int n = 0; n < 4 * 4; n++) |
3065 | 0 | logtrace(LogSlice, "*%d,%d ", ScanOrderPos[n].x, ScanOrderPos[n].y); |
3066 | 0 | logtrace(LogSlice, "*\n"); |
3067 | | |
3068 | | |
3069 | | // --- find last sub block and last scan pos --- |
3070 | |
|
3071 | 0 | int xC, yC; |
3072 | |
|
3073 | 0 | scan_position lastScanP = get_scan_position(LastSignificantCoeffX, LastSignificantCoeffY, |
3074 | 0 | scanIdx, log2TrafoSize); |
3075 | |
|
3076 | 0 | int lastScanPos = lastScanP.scanPos; |
3077 | 0 | int lastSubBlock = lastScanP.subBlock; |
3078 | | |
3079 | |
|
3080 | 0 | int sbWidth = 1 << (log2TrafoSize - 2); |
3081 | |
|
3082 | 0 | uint8_t coded_sub_block_neighbors[32 / 4 * 32 / 4]; |
3083 | 0 | memset(coded_sub_block_neighbors, 0, sbWidth * sbWidth); |
3084 | |
|
3085 | 0 | int c1 = 1; |
3086 | 0 | bool firstSubblock = true; // for coeff_abs_level_greater1_flag context model |
3087 | 0 | 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 | 0 | int CoeffStride = 1 << log2TrafoSize; |
3097 | |
|
3098 | 0 | int lastInvocation_greater1Ctx = 0; |
3099 | 0 | int lastInvocation_coeff_abs_level_greater1_flag = 0; |
3100 | 0 | int lastInvocation_ctxSet = 0; |
3101 | | |
3102 | | |
3103 | | // ----- decode coefficients ----- |
3104 | |
|
3105 | 0 | tctx->nCoeff[cIdx] = 0; |
3106 | | |
3107 | | |
3108 | | // i - subblock index |
3109 | | // n - coefficient index in subblock |
3110 | |
|
3111 | 0 | for (int i = lastSubBlock; i >= 0; i--) { |
3112 | 0 | position S = ScanOrderSub[i]; |
3113 | 0 | int inferSbDcSigCoeffFlag = 0; |
3114 | |
|
3115 | 0 | logtrace(LogSlice, "sub block scan idx: %d\n", i); |
3116 | | |
3117 | | |
3118 | | // --- check whether this sub-block is coded --- |
3119 | |
|
3120 | 0 | int sub_block_is_coded = 0; |
3121 | |
|
3122 | 0 | if ((i < lastSubBlock) && (i > 0)) { |
3123 | 0 | sub_block_is_coded = decode_coded_sub_block_flag(tctx, cIdx, |
3124 | 0 | coded_sub_block_neighbors[S.x + S.y * sbWidth]); |
3125 | 0 | inferSbDcSigCoeffFlag = 1; |
3126 | 0 | } |
3127 | 0 | 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 | 0 | sub_block_is_coded = 1; |
3133 | 0 | } |
3134 | |
|
3135 | 0 | if (sub_block_is_coded) { |
3136 | 0 | if (S.x > 0) coded_sub_block_neighbors[S.x - 1 + S.y * sbWidth] |= 1; |
3137 | 0 | if (S.y > 0) coded_sub_block_neighbors[S.x + (S.y - 1) * sbWidth] |= 2; |
3138 | 0 | } |
3139 | | |
3140 | | |
3141 | | // ----- find significant coefficients in this sub-block ----- |
3142 | |
|
3143 | 0 | int16_t coeff_value[16]; |
3144 | 0 | int8_t coeff_scan_pos[16]; |
3145 | 0 | int8_t coeff_sign[16]; |
3146 | 0 | int8_t coeff_has_max_base_level[16]; |
3147 | 0 | int nCoefficients = 0; |
3148 | | |
3149 | |
|
3150 | 0 | if (sub_block_is_coded) { |
3151 | 0 | int x0 = S.x << 2; |
3152 | 0 | int y0 = S.y << 2; |
3153 | |
|
3154 | 0 | int log2w = log2TrafoSize - 2; |
3155 | 0 | int prevCsbf = coded_sub_block_neighbors[S.x + S.y * sbWidth]; |
3156 | 0 | uint8_t* ctxIdxMap = ctxIdxLookup[log2w][!!cIdx][!!scanIdx][prevCsbf]; |
3157 | |
|
3158 | 0 | logdebug(LogSlice, "log2w:%d cIdx:%d scanIdx:%d prevCsbf:%d\n", |
3159 | 0 | log2w, cIdx, scanIdx, prevCsbf); |
3160 | | |
3161 | | |
3162 | | // set the last coded coefficient in the last subblock |
3163 | |
|
3164 | 0 | int last_coeff = (i == lastSubBlock) ? lastScanPos - 1 : 15; |
3165 | |
|
3166 | 0 | if (i == lastSubBlock) { |
3167 | 0 | coeff_value[nCoefficients] = 1; |
3168 | 0 | coeff_has_max_base_level[nCoefficients] = 1; |
3169 | 0 | coeff_scan_pos[nCoefficients] = lastScanPos; |
3170 | 0 | nCoefficients++; |
3171 | 0 | } |
3172 | | |
3173 | | |
3174 | | // --- decode all coefficients' significant_coeff flags except for the DC coefficient --- |
3175 | |
|
3176 | 0 | for (int n = last_coeff; n > 0; n--) { |
3177 | 0 | int subX = ScanOrderPos[n].x; |
3178 | 0 | int subY = ScanOrderPos[n].y; |
3179 | 0 | xC = x0 + subX; |
3180 | 0 | yC = y0 + subY; |
3181 | | |
3182 | | |
3183 | | // for all AC coefficients in sub-block, a significant_coeff flag is coded |
3184 | |
|
3185 | 0 | int ctxInc; |
3186 | 0 | if (sps.range_extension.transform_skip_context_enabled_flag && |
3187 | 0 | (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx])) { |
3188 | 0 | ctxInc = (cIdx == 0) ? 42 : (16 + 27); |
3189 | 0 | } |
3190 | 0 | else { |
3191 | 0 | ctxInc = ctxIdxMap[xC + (yC << log2TrafoSize)]; |
3192 | 0 | } |
3193 | |
|
3194 | 0 | logtrace(LogSlice, "trafoSize: %d\n", 1 << log2TrafoSize); |
3195 | |
|
3196 | 0 | int significant_coeff = decode_significant_coeff_flag_lookup(tctx, ctxInc); |
3197 | |
|
3198 | 0 | if (significant_coeff) { |
3199 | 0 | coeff_value[nCoefficients] = 1; |
3200 | 0 | coeff_has_max_base_level[nCoefficients] = 1; |
3201 | 0 | coeff_scan_pos[nCoefficients] = n; |
3202 | 0 | nCoefficients++; |
3203 | | |
3204 | | // since we have a coefficient in the sub-block, |
3205 | | // we cannot infer the DC coefficient anymore |
3206 | 0 | inferSbDcSigCoeffFlag = 0; |
3207 | 0 | } |
3208 | 0 | } |
3209 | | |
3210 | | |
3211 | | // --- decode DC coefficient significance --- |
3212 | |
|
3213 | 0 | if (last_coeff >= 0) // last coded coefficient (always set to 1) is not the DC coefficient |
3214 | 0 | { |
3215 | 0 | if (inferSbDcSigCoeffFlag == 0) { |
3216 | | // if we cannot infert the DC coefficient, it is coded |
3217 | |
|
3218 | 0 | int ctxInc; |
3219 | 0 | if (sps.range_extension.transform_skip_context_enabled_flag && |
3220 | 0 | (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx])) { |
3221 | 0 | ctxInc = (cIdx == 0) ? 42 : (16 + 27); |
3222 | 0 | } |
3223 | 0 | else { |
3224 | 0 | ctxInc = ctxIdxMap[x0 + (y0 << log2TrafoSize)]; |
3225 | 0 | } |
3226 | |
|
3227 | 0 | int significant_coeff = decode_significant_coeff_flag_lookup(tctx, ctxInc); |
3228 | | |
3229 | |
|
3230 | 0 | if (significant_coeff) { |
3231 | 0 | coeff_value[nCoefficients] = 1; |
3232 | 0 | coeff_has_max_base_level[nCoefficients] = 1; |
3233 | 0 | coeff_scan_pos[nCoefficients] = 0; |
3234 | 0 | nCoefficients++; |
3235 | 0 | } |
3236 | 0 | } |
3237 | 0 | else { |
3238 | | // we can infer that the DC coefficient must be present |
3239 | 0 | coeff_value[nCoefficients] = 1; |
3240 | 0 | coeff_has_max_base_level[nCoefficients] = 1; |
3241 | 0 | coeff_scan_pos[nCoefficients] = 0; |
3242 | 0 | nCoefficients++; |
3243 | 0 | } |
3244 | 0 | } |
3245 | 0 | } |
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 | 0 | if (nCoefficients) { |
3261 | 0 | int ctxSet; |
3262 | 0 | if (i == 0 || cIdx > 0) { ctxSet = 0; } |
3263 | 0 | else { ctxSet = 2; } |
3264 | |
|
3265 | 0 | if (c1 == 0) { ctxSet++; } |
3266 | 0 | c1 = 1; |
3267 | | |
3268 | | |
3269 | | // --- decode greater-1 flags --- |
3270 | |
|
3271 | 0 | int newLastGreater1ScanPos = -1; |
3272 | |
|
3273 | 0 | int lastGreater1Coefficient = libde265_min(8, nCoefficients); |
3274 | 0 | for (int c = 0; c < lastGreater1Coefficient; c++) { |
3275 | 0 | int greater1_flag = |
3276 | 0 | decode_coeff_abs_level_greater1(tctx, cIdx, i, |
3277 | 0 | c == 0, |
3278 | 0 | firstSubblock, |
3279 | 0 | lastSubblock_greater1Ctx, |
3280 | 0 | &lastInvocation_greater1Ctx, |
3281 | 0 | &lastInvocation_coeff_abs_level_greater1_flag, |
3282 | 0 | &lastInvocation_ctxSet, ctxSet); |
3283 | |
|
3284 | 0 | if (greater1_flag) { |
3285 | 0 | coeff_value[c]++; |
3286 | |
|
3287 | 0 | c1 = 0; |
3288 | |
|
3289 | 0 | if (newLastGreater1ScanPos == -1) { |
3290 | 0 | newLastGreater1ScanPos = c; |
3291 | 0 | } |
3292 | 0 | } |
3293 | 0 | else { |
3294 | 0 | coeff_has_max_base_level[c] = 0; |
3295 | |
|
3296 | 0 | if (c1 < 3 && c1 > 0) { |
3297 | 0 | c1++; |
3298 | 0 | } |
3299 | 0 | } |
3300 | 0 | } |
3301 | |
|
3302 | 0 | firstSubblock = false; |
3303 | 0 | lastSubblock_greater1Ctx = lastInvocation_greater1Ctx; |
3304 | | |
3305 | | |
3306 | | // --- decode greater-2 flag --- |
3307 | |
|
3308 | 0 | if (newLastGreater1ScanPos != -1) { |
3309 | 0 | int flag = decode_coeff_abs_level_greater2(tctx, cIdx, lastInvocation_ctxSet); |
3310 | 0 | coeff_value[newLastGreater1ScanPos] += flag; |
3311 | 0 | coeff_has_max_base_level[newLastGreater1ScanPos] = flag; |
3312 | 0 | } |
3313 | | |
3314 | | |
3315 | | // --- decode coefficient signs --- |
3316 | |
|
3317 | 0 | int signHidden; |
3318 | | |
3319 | |
|
3320 | 0 | IntraPredMode predModeIntra; |
3321 | 0 | if (cIdx == 0) predModeIntra = img->get_IntraPredMode(x0, y0); |
3322 | 0 | else predModeIntra = img->get_IntraPredModeC(x0, y0); |
3323 | | |
3324 | |
|
3325 | 0 | if (tctx->cu_transquant_bypass_flag || |
3326 | 0 | (PredMode == MODE_INTRA && |
3327 | 0 | sps.range_extension.implicit_rdpcm_enabled_flag && |
3328 | 0 | tctx->transform_skip_flag[cIdx] && |
3329 | 0 | (predModeIntra == 10 || predModeIntra == 26)) || |
3330 | 0 | tctx->explicit_rdpcm_flag) { |
3331 | 0 | signHidden = 0; |
3332 | 0 | } |
3333 | 0 | else { |
3334 | 0 | signHidden = (coeff_scan_pos[0] - coeff_scan_pos[nCoefficients - 1] > 3); |
3335 | 0 | } |
3336 | | |
3337 | |
|
3338 | 0 | for (int n = 0; n < nCoefficients - 1; n++) { |
3339 | 0 | coeff_sign[n] = tctx->cabac_decoder.decode_bypass(); |
3340 | 0 | logtrace(LogSlice, "sign[%d] = %d\n", n, coeff_sign[n]); |
3341 | 0 | } |
3342 | | |
3343 | | // n==nCoefficients-1 |
3344 | 0 | if (!pps.sign_data_hiding_flag || !signHidden) { |
3345 | 0 | coeff_sign[nCoefficients - 1] = tctx->cabac_decoder.decode_bypass(); |
3346 | 0 | logtrace(LogSlice, "sign[%d] = %d\n", nCoefficients - 1, coeff_sign[nCoefficients - 1]); |
3347 | 0 | } |
3348 | 0 | else { |
3349 | 0 | coeff_sign[nCoefficients - 1] = 0; |
3350 | 0 | } |
3351 | | |
3352 | | |
3353 | | // --- decode coefficient value --- |
3354 | |
|
3355 | 0 | int sumAbsLevel = 0; |
3356 | 0 | int uiGoRiceParam; |
3357 | |
|
3358 | 0 | if (sps.range_extension.persistent_rice_adaptation_enabled_flag == 0) { |
3359 | 0 | uiGoRiceParam = 0; |
3360 | 0 | } |
3361 | 0 | else { |
3362 | 0 | uiGoRiceParam = tctx->StatCoeff[sbType] / 4; |
3363 | 0 | } |
3364 | | |
3365 | | // printf("initial uiGoRiceParam=%d\n",uiGoRiceParam); |
3366 | 0 | bool firstCoeffWithAbsLevelRemaining = true; |
3367 | |
|
3368 | 0 | for (int n = 0; n < nCoefficients; n++) { |
3369 | 0 | int16_t baseLevel = coeff_value[n]; |
3370 | |
|
3371 | 0 | int32_t coeff_abs_level_remaining; |
3372 | | |
3373 | | // printf("coeff %d/%d, uiRiceParam: %d\n",n,nCoefficients,uiGoRiceParam); |
3374 | |
|
3375 | 0 | if (coeff_has_max_base_level[n]) { |
3376 | 0 | coeff_abs_level_remaining = |
3377 | 0 | decode_coeff_abs_level_remaining(tctx, uiGoRiceParam); |
3378 | |
|
3379 | 0 | if (sps.range_extension.persistent_rice_adaptation_enabled_flag == 0) { |
3380 | | // (2014.10 / 9-20) |
3381 | 0 | if (baseLevel + coeff_abs_level_remaining > 3 * (1 << uiGoRiceParam)) { |
3382 | 0 | uiGoRiceParam++; |
3383 | 0 | if (uiGoRiceParam > 4) uiGoRiceParam = 4; |
3384 | 0 | } |
3385 | 0 | } |
3386 | 0 | else { |
3387 | 0 | if (baseLevel + coeff_abs_level_remaining > 3 * (1 << uiGoRiceParam)) { |
3388 | 0 | uiGoRiceParam++; |
3389 | 0 | 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 | 0 | } |
3394 | 0 | } |
3395 | | |
3396 | | // persistent_rice_adaptation_enabled_flag |
3397 | 0 | if (sps.range_extension.persistent_rice_adaptation_enabled_flag && |
3398 | 0 | firstCoeffWithAbsLevelRemaining) { |
3399 | 0 | if (coeff_abs_level_remaining >= (3 << (tctx->StatCoeff[sbType] / 4))) { |
3400 | 0 | if (tctx->StatCoeff[sbType] < MAX_STAT_COEFF) { |
3401 | 0 | tctx->StatCoeff[sbType]++; |
3402 | 0 | } else { |
3403 | 0 | tctx->decctx->add_warning(DE265_WARNING_RICE_PARAMETER_OUT_OF_RANGE, true); |
3404 | 0 | } |
3405 | 0 | } |
3406 | 0 | else if (2 * coeff_abs_level_remaining < (1 << (tctx->StatCoeff[sbType] / 4)) && |
3407 | 0 | tctx->StatCoeff[sbType] > 0) { |
3408 | 0 | tctx->StatCoeff[sbType]--; |
3409 | 0 | } |
3410 | 0 | } |
3411 | |
|
3412 | 0 | firstCoeffWithAbsLevelRemaining = false; |
3413 | 0 | } |
3414 | 0 | else { |
3415 | 0 | coeff_abs_level_remaining = 0; |
3416 | 0 | } |
3417 | |
|
3418 | 0 | logtrace(LogSlice, "coeff_abs_level_remaining=%d\n", coeff_abs_level_remaining); |
3419 | | |
3420 | |
|
3421 | 0 | int32_t currCoeff = baseLevel + coeff_abs_level_remaining; |
3422 | 0 | if (coeff_sign[n]) { |
3423 | 0 | currCoeff = -currCoeff; |
3424 | 0 | } |
3425 | |
|
3426 | 0 | if (pps.sign_data_hiding_flag && signHidden) { |
3427 | 0 | sumAbsLevel += currCoeff; |
3428 | |
|
3429 | 0 | if (n == nCoefficients - 1 && (sumAbsLevel & 1)) { |
3430 | 0 | currCoeff = -currCoeff; |
3431 | 0 | } |
3432 | 0 | } |
3433 | |
|
3434 | 0 | 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 | 0 | int p = coeff_scan_pos[n]; |
3442 | 0 | xC = (S.x << 2) + ScanOrderPos[p].x; |
3443 | 0 | yC = (S.y << 2) + ScanOrderPos[p].y; |
3444 | |
|
3445 | 0 | tctx->coeffList[cIdx][tctx->nCoeff[cIdx]] = Clip3(-32768, 32767, currCoeff); |
3446 | 0 | tctx->coeffPos[cIdx][tctx->nCoeff[cIdx]] = xC + yC * CoeffStride; |
3447 | 0 | tctx->nCoeff[cIdx]++; |
3448 | | |
3449 | | //printf("%d ",currCoeff); |
3450 | 0 | } // iterate through coefficients in sub-block |
3451 | | |
3452 | | //printf(" (%d;%d)\n",x0,y0); |
3453 | 0 | } // if nonZero |
3454 | 0 | } // next sub-block |
3455 | |
|
3456 | 0 | return DE265_OK; |
3457 | 0 | } |
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 | 0 | { |
3465 | 0 | de265_image* img = tctx->img; |
3466 | 0 | const seq_parameter_set& sps = img->get_sps(); |
3467 | |
|
3468 | 0 | int residualDpcm = 0; |
3469 | |
|
3470 | 0 | if (cuPredMode == MODE_INTRA) // if intra mode |
3471 | 0 | { |
3472 | 0 | enum IntraPredMode intraPredMode; |
3473 | |
|
3474 | 0 | if (cIdx == 0) { |
3475 | 0 | intraPredMode = img->get_IntraPredMode(x0, y0); |
3476 | 0 | } |
3477 | 0 | else { |
3478 | 0 | const int SubWidthC = sps.SubWidthC; |
3479 | 0 | const int SubHeightC = sps.SubHeightC; |
3480 | |
|
3481 | 0 | intraPredMode = img->get_IntraPredModeC(x0 * SubWidthC, y0 * SubHeightC); |
3482 | 0 | } |
3483 | |
|
3484 | 0 | if (intraPredMode < 0 || intraPredMode >= 35) { |
3485 | | // TODO: ERROR |
3486 | 0 | intraPredMode = INTRA_DC; |
3487 | 0 | } |
3488 | |
|
3489 | 0 | decode_intra_prediction(img, x0, y0, intraPredMode, nT, cIdx); |
3490 | | |
3491 | |
|
3492 | 0 | residualDpcm = sps.range_extension.implicit_rdpcm_enabled_flag && |
3493 | 0 | (tctx->cu_transquant_bypass_flag || tctx->transform_skip_flag[cIdx]) && |
3494 | 0 | (intraPredMode == 10 || intraPredMode == 26); |
3495 | |
|
3496 | 0 | if (residualDpcm && intraPredMode == 26) |
3497 | 0 | residualDpcm = 2; |
3498 | 0 | } |
3499 | 0 | else // INTER |
3500 | 0 | { |
3501 | 0 | if (tctx->explicit_rdpcm_flag) { |
3502 | 0 | residualDpcm = (tctx->explicit_rdpcm_dir ? 2 : 1); |
3503 | 0 | } |
3504 | 0 | } |
3505 | |
|
3506 | 0 | if (cbf) { |
3507 | 0 | scale_coefficients(tctx, x0, y0, xCUBase, yCUBase, nT, cIdx, |
3508 | 0 | tctx->transform_skip_flag[cIdx], cuPredMode == MODE_INTRA, residualDpcm); |
3509 | 0 | } |
3510 | | /* |
3511 | | else if (!cbf && cIdx==0) { |
3512 | | memset(tctx->residual_luma,0,32*32*sizeof(int32_t)); |
3513 | | } |
3514 | | */ |
3515 | 0 | 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 | 0 | } |
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 | 0 | { |
3593 | 0 | logtrace(LogSlice, "- read_transform_unit x0:%d y0:%d xBase:%d yBase:%d nT:%d cbf:%d:%d:%d\n", |
3594 | 0 | x0, y0, xBase, yBase, 1 << log2TrafoSize, cbf_luma, cbf_cb, cbf_cr); |
3595 | |
|
3596 | 0 | assert(cbf_cb != -1); |
3597 | 0 | assert(cbf_cr != -1); |
3598 | 0 | assert(cbf_luma != -1); |
3599 | | |
3600 | 0 | const seq_parameter_set& sps = tctx->img->get_sps(); |
3601 | |
|
3602 | 0 | const int ChromaArrayType = sps.ChromaArrayType; |
3603 | |
|
3604 | 0 | int log2TrafoSizeC = (ChromaArrayType == CHROMA_444 ? log2TrafoSize : log2TrafoSize - 1); |
3605 | 0 | log2TrafoSizeC = libde265_max(2, log2TrafoSizeC); |
3606 | |
|
3607 | 0 | const int cbfLuma = cbf_luma; |
3608 | 0 | const int cbfChroma = cbf_cb | cbf_cr; |
3609 | |
|
3610 | 0 | tctx->transform_skip_flag[0] = 0; |
3611 | 0 | tctx->transform_skip_flag[1] = 0; |
3612 | 0 | tctx->transform_skip_flag[2] = 0; |
3613 | |
|
3614 | 0 | tctx->explicit_rdpcm_flag = false; |
3615 | | |
3616 | |
|
3617 | 0 | enum PredMode cuPredMode = tctx->img->get_pred_mode(x0, y0); |
3618 | |
|
3619 | 0 | if (cbfLuma || cbfChroma) { |
3620 | 0 | bool doDecodeQuantParameters = false; |
3621 | |
|
3622 | 0 | if (tctx->img->get_pps().cu_qp_delta_enabled_flag && |
3623 | 0 | !tctx->IsCuQpDeltaCoded) { |
3624 | 0 | uint8_t cu_qp_delta_abs = decode_cu_qp_delta_abs(tctx); |
3625 | 0 | if (cu_qp_delta_abs == CABAC_QP_DELTA_ABS_ERROR) { |
3626 | 0 | tctx->decctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false); |
3627 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
3628 | 0 | } |
3629 | | |
3630 | 0 | int cu_qp_delta_sign = 0; |
3631 | 0 | if (cu_qp_delta_abs) { |
3632 | 0 | cu_qp_delta_sign = tctx->cabac_decoder.decode_bypass(); |
3633 | 0 | } |
3634 | | |
3635 | | // CuQpDeltaVal shall be in [-(26 + QpBdOffsetY/2), 25 + QpBdOffsetY/2] (Sec. 7.4.9.10) |
3636 | 0 | int maxCuQpDeltaAbs = 25 + tctx->img->get_sps().QpBdOffset_Y / 2; |
3637 | 0 | if (cu_qp_delta_abs > maxCuQpDeltaAbs) { |
3638 | 0 | tctx->decctx->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false); |
3639 | 0 | return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; |
3640 | 0 | } |
3641 | | |
3642 | 0 | tctx->IsCuQpDeltaCoded = 1; |
3643 | 0 | 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 | 0 | logtrace(LogSlice, "cu_qp_delta_abs = %d\n", cu_qp_delta_abs); |
3648 | 0 | logtrace(LogSlice, "cu_qp_delta_sign = %d\n", cu_qp_delta_sign); |
3649 | 0 | logtrace(LogSlice, "CuQpDelta = %d\n", tctx->CuQpDelta); |
3650 | |
|
3651 | 0 | doDecodeQuantParameters = true; |
3652 | | //decode_quantization_parameters(tctx, x0,y0, xCUBase, yCUBase); |
3653 | 0 | } |
3654 | | |
3655 | 0 | 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 | 0 | if (doDecodeQuantParameters) { |
3688 | 0 | decode_quantization_parameters(tctx, x0, y0, xCUBase, yCUBase); |
3689 | 0 | } |
3690 | 0 | } |
3691 | | |
3692 | | // position of TU in local CU |
3693 | | //int xL = x0 - xCUBase; |
3694 | | //int yL = y0 - yCUBase; |
3695 | 0 | int nT = 1 << log2TrafoSize; |
3696 | 0 | int nTC = 1 << log2TrafoSizeC; |
3697 | |
|
3698 | 0 | const int SubWidthC = sps.SubWidthC; |
3699 | 0 | const int SubHeightC = sps.SubHeightC; |
3700 | | |
3701 | | // --- luma --- |
3702 | |
|
3703 | 0 | tctx->ResScaleVal = 0; |
3704 | |
|
3705 | 0 | int err; |
3706 | 0 | if (cbf_luma) { |
3707 | 0 | if ((err = residual_coding(tctx, x0, y0, log2TrafoSize, 0)) != DE265_OK) return err; |
3708 | 0 | } |
3709 | | |
3710 | 0 | 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 | 0 | if (log2TrafoSize > 2 || ChromaArrayType == CHROMA_444) { |
3718 | | // TODO: cross-component prediction |
3719 | |
|
3720 | 0 | const bool do_cross_component_prediction = |
3721 | 0 | (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 | 0 | if (do_cross_component_prediction) { |
3726 | 0 | read_cross_comp_pred(tctx, 0); |
3727 | 0 | } |
3728 | 0 | else { |
3729 | 0 | tctx->ResScaleVal = 0; |
3730 | 0 | } { |
3731 | 0 | if (cbf_cb & 1) { |
3732 | 0 | if ((err = residual_coding(tctx, x0, y0, log2TrafoSizeC, 1)) != DE265_OK) return err; |
3733 | 0 | } |
3734 | | |
3735 | 0 | if (sps.ChromaArrayType != CHROMA_MONO) { |
3736 | 0 | decode_TU(tctx, |
3737 | 0 | x0 / SubWidthC, y0 / SubHeightC, |
3738 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, nTC, 1, cuPredMode, cbf_cb & 1); |
3739 | 0 | } |
3740 | 0 | } |
3741 | | |
3742 | | // 4:2:2 |
3743 | 0 | if (ChromaArrayType == CHROMA_422) { |
3744 | 0 | const int yOffset = 1 << log2TrafoSizeC; |
3745 | |
|
3746 | 0 | if (cbf_cb & 2) { |
3747 | 0 | if ((err = residual_coding(tctx, |
3748 | 0 | x0, y0 + yOffset * SubHeightC, |
3749 | 0 | log2TrafoSizeC, 1)) != DE265_OK) |
3750 | 0 | return err; |
3751 | 0 | } |
3752 | | |
3753 | 0 | decode_TU(tctx, |
3754 | 0 | x0 / SubWidthC, y0 / SubHeightC + yOffset, |
3755 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC + yOffset, |
3756 | 0 | nTC, 1, cuPredMode, cbf_cb & 2); |
3757 | 0 | } |
3758 | | |
3759 | | |
3760 | 0 | if (do_cross_component_prediction) { |
3761 | 0 | read_cross_comp_pred(tctx, 1); |
3762 | 0 | } |
3763 | 0 | else { |
3764 | 0 | tctx->ResScaleVal = 0; |
3765 | 0 | } { |
3766 | 0 | if (cbf_cr & 1) { |
3767 | 0 | if ((err = residual_coding(tctx, x0, y0, log2TrafoSizeC, 2)) != DE265_OK) return err; |
3768 | 0 | } |
3769 | | |
3770 | 0 | if (sps.ChromaArrayType != CHROMA_MONO) { |
3771 | 0 | decode_TU(tctx, |
3772 | 0 | x0 / SubWidthC, y0 / SubHeightC, |
3773 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, |
3774 | 0 | nTC, 2, cuPredMode, cbf_cr & 1); |
3775 | 0 | } |
3776 | 0 | } |
3777 | | |
3778 | | // 4:2:2 |
3779 | 0 | if (ChromaArrayType == CHROMA_422) { |
3780 | 0 | const int yOffset = 1 << log2TrafoSizeC; |
3781 | |
|
3782 | 0 | if (cbf_cr & 2) { |
3783 | 0 | if ((err = residual_coding(tctx, |
3784 | 0 | x0, y0 + yOffset * SubHeightC, |
3785 | 0 | log2TrafoSizeC, 2)) != DE265_OK) |
3786 | 0 | return err; |
3787 | 0 | } |
3788 | | |
3789 | 0 | decode_TU(tctx, |
3790 | 0 | x0 / SubWidthC, y0 / SubHeightC + yOffset, |
3791 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC + yOffset, |
3792 | 0 | nTC, 2, cuPredMode, cbf_cr & 2); |
3793 | 0 | } |
3794 | 0 | } |
3795 | 0 | else if (blkIdx == 3) { |
3796 | 0 | if (cbf_cb & 1) { |
3797 | 0 | if ((err = residual_coding(tctx, xBase, yBase, |
3798 | 0 | log2TrafoSize, 1)) != DE265_OK) |
3799 | 0 | return err; |
3800 | 0 | } |
3801 | | |
3802 | 0 | if (sps.ChromaArrayType != CHROMA_MONO) { |
3803 | 0 | decode_TU(tctx, |
3804 | 0 | xBase / SubWidthC, yBase / SubHeightC, |
3805 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, nT, 1, cuPredMode, cbf_cb & 1); |
3806 | 0 | } |
3807 | | |
3808 | | // 4:2:2 |
3809 | 0 | if (cbf_cb & 2) { |
3810 | 0 | if ((err = residual_coding(tctx, |
3811 | 0 | xBase, yBase + (1 << log2TrafoSize), |
3812 | 0 | log2TrafoSize, 1)) != DE265_OK) |
3813 | 0 | return err; |
3814 | 0 | } |
3815 | | |
3816 | 0 | if (ChromaArrayType == CHROMA_422) { |
3817 | 0 | decode_TU(tctx, |
3818 | 0 | xBase / SubWidthC, yBase / SubHeightC + (1 << log2TrafoSize), |
3819 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, nT, 1, cuPredMode, cbf_cb & 2); |
3820 | 0 | } |
3821 | |
|
3822 | 0 | if (cbf_cr & 1) { |
3823 | 0 | if ((err = residual_coding(tctx, xBase, yBase, |
3824 | 0 | log2TrafoSize, 2)) != DE265_OK) |
3825 | 0 | return err; |
3826 | 0 | } |
3827 | | |
3828 | 0 | if (sps.ChromaArrayType != CHROMA_MONO) { |
3829 | 0 | decode_TU(tctx, |
3830 | 0 | xBase / SubWidthC, yBase / SubHeightC, |
3831 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, nT, 2, cuPredMode, cbf_cr & 1); |
3832 | 0 | } |
3833 | | |
3834 | | // 4:2:2 |
3835 | 0 | if (cbf_cr & 2) { |
3836 | 0 | if ((err = residual_coding(tctx, |
3837 | 0 | xBase, yBase + (1 << log2TrafoSizeC), |
3838 | 0 | log2TrafoSize, 2)) != DE265_OK) |
3839 | 0 | return err; |
3840 | 0 | } |
3841 | | |
3842 | 0 | if (ChromaArrayType == CHROMA_422) { |
3843 | 0 | decode_TU(tctx, |
3844 | 0 | xBase / SubWidthC, yBase / SubHeightC + (1 << log2TrafoSize), |
3845 | 0 | xCUBase / SubWidthC, yCUBase / SubHeightC, nT, 2, cuPredMode, cbf_cr & 2); |
3846 | 0 | } |
3847 | 0 | } |
3848 | | |
3849 | | |
3850 | 0 | return DE265_OK; |
3851 | 0 | } |
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 | 0 | { |
3882 | 0 | logtrace(LogSlice, "- read_transform_tree (interleaved) x0:%d y0:%d xBase:%d yBase:%d " |
3883 | 0 | "log2TrafoSize:%d trafoDepth:%d MaxTrafoDepth:%d parent-cbf-cb:%d parent-cbf-cr:%d\n", |
3884 | 0 | x0, y0, xBase, yBase, log2TrafoSize, trafoDepth, MaxTrafoDepth, parent_cbf_cb, parent_cbf_cr); |
3885 | |
|
3886 | 0 | de265_image* img = tctx->img; |
3887 | 0 | const seq_parameter_set& sps = img->get_sps(); |
3888 | |
|
3889 | 0 | int split_transform_flag; |
3890 | |
|
3891 | 0 | enum PredMode PredMode = img->get_pred_mode(x0, y0); |
3892 | 0 | 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 | 0 | if (log2TrafoSize <= sps.Log2MaxTrafoSize && |
3901 | 0 | log2TrafoSize > sps.Log2MinTrafoSize && |
3902 | 0 | trafoDepth < MaxTrafoDepth && |
3903 | 0 | !(IntraSplitFlag && trafoDepth == 0)) { |
3904 | 0 | split_transform_flag = decode_split_transform_flag(tctx, log2TrafoSize); |
3905 | 0 | } |
3906 | 0 | else { |
3907 | 0 | enum PartMode PartMode = img->get_PartMode(x0, y0); |
3908 | |
|
3909 | 0 | int interSplitFlag = (sps.max_transform_hierarchy_depth_inter == 0 && |
3910 | 0 | trafoDepth == 0 && |
3911 | 0 | PredMode == MODE_INTER && |
3912 | 0 | PartMode != PART_2Nx2N); |
3913 | |
|
3914 | 0 | split_transform_flag = (log2TrafoSize > sps.Log2MaxTrafoSize || |
3915 | 0 | (IntraSplitFlag == 1 && trafoDepth == 0) || |
3916 | 0 | interSplitFlag == 1) |
3917 | 0 | ? 1 |
3918 | 0 | : 0; |
3919 | 0 | } |
3920 | |
|
3921 | 0 | 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 | 0 | img->decctx->add_warning(DE265_WARNING_INVALID_TU_BLOCK_SPLIT, true); |
3927 | 0 | split_transform_flag = 0; |
3928 | 0 | } |
3929 | |
|
3930 | 0 | if (split_transform_flag) { |
3931 | 0 | logtrace(LogSlice, "set_split_transform_flag(%d,%d, %d)\n", x0, y0, trafoDepth); |
3932 | 0 | img->set_split_transform_flag(x0, y0, trafoDepth); |
3933 | 0 | } |
3934 | |
|
3935 | 0 | int cbf_cb = -1; |
3936 | 0 | 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 | 0 | if ((log2TrafoSize > 2 && sps.ChromaArrayType != CHROMA_MONO) || |
3943 | 0 | sps.ChromaArrayType == CHROMA_444) { |
3944 | | // we do not have to test for trafoDepth==0, because parent_cbf_cb is 1 at depth 0 |
3945 | 0 | if (/*trafoDepth==0 ||*/ parent_cbf_cb) { |
3946 | 0 | cbf_cb = decode_cbf_chroma(tctx, trafoDepth); |
3947 | |
|
3948 | 0 | if (sps.ChromaArrayType == CHROMA_422 && (!split_transform_flag || log2TrafoSize == 3)) { |
3949 | 0 | cbf_cb |= (decode_cbf_chroma(tctx, trafoDepth) << 1); |
3950 | 0 | } |
3951 | 0 | } |
3952 | | |
3953 | | // we do not have to test for trafoDepth==0, because parent_cbf_cb is 1 at depth 0 |
3954 | 0 | if (/*trafoDepth==0 ||*/ parent_cbf_cr) { |
3955 | 0 | cbf_cr = decode_cbf_chroma(tctx, trafoDepth); |
3956 | |
|
3957 | 0 | if (sps.ChromaArrayType == CHROMA_422 && (!split_transform_flag || log2TrafoSize == 3)) { |
3958 | 0 | cbf_cr |= (decode_cbf_chroma(tctx, trafoDepth) << 1); |
3959 | 0 | } |
3960 | 0 | } |
3961 | 0 | } |
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 | 0 | if (cbf_cb < 0) { |
3968 | 0 | 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 | 0 | if (trafoDepth > 0 && log2TrafoSize == 2) { |
3976 | 0 | cbf_cb = parent_cbf_cb; |
3977 | 0 | } |
3978 | 0 | else { |
3979 | 0 | cbf_cb = 0; |
3980 | 0 | } |
3981 | 0 | } |
3982 | | |
3983 | 0 | if (cbf_cr < 0) { |
3984 | 0 | if (trafoDepth > 0 && log2TrafoSize == 2) { |
3985 | 0 | cbf_cr = parent_cbf_cr; |
3986 | 0 | } |
3987 | 0 | else { |
3988 | 0 | cbf_cr = 0; |
3989 | 0 | } |
3990 | 0 | } |
3991 | |
|
3992 | 0 | if (split_transform_flag) { |
3993 | 0 | int x1 = x0 + (1 << (log2TrafoSize - 1)); |
3994 | 0 | int y1 = y0 + (1 << (log2TrafoSize - 1)); |
3995 | |
|
3996 | 0 | logtrace(LogSlice, "transform split.\n"); |
3997 | |
|
3998 | 0 | read_transform_tree(tctx, x0, y0, x0, y0, xCUBase, yCUBase, log2TrafoSize - 1, trafoDepth + 1, 0, |
3999 | 0 | MaxTrafoDepth, IntraSplitFlag, cuPredMode, cbf_cb, cbf_cr); |
4000 | 0 | read_transform_tree(tctx, x1, y0, x0, y0, xCUBase, yCUBase, log2TrafoSize - 1, trafoDepth + 1, 1, |
4001 | 0 | MaxTrafoDepth, IntraSplitFlag, cuPredMode, cbf_cb, cbf_cr); |
4002 | 0 | read_transform_tree(tctx, x0, y1, x0, y0, xCUBase, yCUBase, log2TrafoSize - 1, trafoDepth + 1, 2, |
4003 | 0 | MaxTrafoDepth, IntraSplitFlag, cuPredMode, cbf_cb, cbf_cr); |
4004 | 0 | read_transform_tree(tctx, x1, y1, x0, y0, xCUBase, yCUBase, log2TrafoSize - 1, trafoDepth + 1, 3, |
4005 | 0 | MaxTrafoDepth, IntraSplitFlag, cuPredMode, cbf_cb, cbf_cr); |
4006 | 0 | } |
4007 | 0 | else { |
4008 | 0 | int cbf_luma; |
4009 | |
|
4010 | 0 | if (PredMode == MODE_INTRA || trafoDepth != 0 || cbf_cb || cbf_cr) { |
4011 | 0 | cbf_luma = decode_cbf_luma(tctx, trafoDepth); |
4012 | 0 | } |
4013 | 0 | else { |
4014 | | /* There cannot be INTER blocks with no residual data. |
4015 | | That case is already handled with rqt_root_cbf. |
4016 | | */ |
4017 | |
|
4018 | 0 | cbf_luma = 1; |
4019 | 0 | } |
4020 | |
|
4021 | 0 | logtrace(LogSlice, "call read_transform_unit %d/%d\n", x0, y0); |
4022 | |
|
4023 | 0 | read_transform_unit(tctx, x0, y0, xBase, yBase, xCUBase, yCUBase, log2TrafoSize, trafoDepth, blkIdx, |
4024 | 0 | cbf_luma, cbf_cb, cbf_cr); |
4025 | 0 | } |
4026 | 0 | } |
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 | 0 | { |
4049 | 0 | int abs_mvd_greater0_flag[2]; |
4050 | 0 | abs_mvd_greater0_flag[0] = tctx->cabac_decoder.decode_bit( |
4051 | 0 | &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG + 0]); |
4052 | 0 | abs_mvd_greater0_flag[1] = tctx->cabac_decoder.decode_bit( |
4053 | 0 | &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG + 0]); |
4054 | |
|
4055 | 0 | int abs_mvd_greater1_flag[2]; |
4056 | 0 | if (abs_mvd_greater0_flag[0]) { |
4057 | 0 | abs_mvd_greater1_flag[0] = tctx->cabac_decoder.decode_bit( |
4058 | 0 | &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG + 1]); |
4059 | 0 | } |
4060 | 0 | else { |
4061 | 0 | abs_mvd_greater1_flag[0] = 0; |
4062 | 0 | } |
4063 | |
|
4064 | 0 | if (abs_mvd_greater0_flag[1]) { |
4065 | 0 | abs_mvd_greater1_flag[1] = tctx->cabac_decoder.decode_bit( |
4066 | 0 | &tctx->ctx_model[CONTEXT_MODEL_ABS_MVD_GREATER01_FLAG + 1]); |
4067 | 0 | } |
4068 | 0 | else { |
4069 | 0 | abs_mvd_greater1_flag[1] = 0; |
4070 | 0 | } |
4071 | | |
4072 | |
|
4073 | 0 | int mvd_sign_flag[2]; |
4074 | 0 | int16_t value[2]; |
4075 | |
|
4076 | 0 | for (int c = 0; c < 2; c++) { |
4077 | 0 | if (abs_mvd_greater0_flag[c]) { |
4078 | 0 | int32_t absMvd; |
4079 | 0 | if (abs_mvd_greater1_flag[c]) { |
4080 | 0 | 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 | 0 | absMvd = static_cast<int32_t>(std::min(abs_mvd_minus2, uint32_t{32768 - 2})) + 2; |
4083 | 0 | } |
4084 | 0 | else { |
4085 | 0 | absMvd = 1; |
4086 | 0 | } |
4087 | |
|
4088 | 0 | mvd_sign_flag[c] = tctx->cabac_decoder.decode_bypass(); |
4089 | 0 | int32_t mvd = mvd_sign_flag[c] ? -absMvd : absMvd; |
4090 | 0 | value[c] = Clip3(-32768, 32767, mvd); |
4091 | 0 | } |
4092 | 0 | else { |
4093 | 0 | value[c] = 0; |
4094 | 0 | } |
4095 | 0 | } |
4096 | |
|
4097 | 0 | tctx->motion.mvd[refList][0] = value[0]; |
4098 | 0 | tctx->motion.mvd[refList][1] = value[1]; |
4099 | |
|
4100 | 0 | logtrace(LogSlice, "MVD[%d;%d|%d] = %d;%d\n", x0, y0, refList, value[0], value[1]); |
4101 | 0 | } |
4102 | | |
4103 | | |
4104 | | void read_prediction_unit_SKIP(thread_context* tctx, |
4105 | | int x0, int y0, |
4106 | | int nPbW, int nPbH) |
4107 | 0 | { |
4108 | 0 | int merge_idx = decode_merge_idx(tctx); |
4109 | |
|
4110 | 0 | tctx->motion.merge_idx = merge_idx; |
4111 | 0 | tctx->motion.merge_flag = true; |
4112 | |
|
4113 | 0 | logtrace(LogSlice, "prediction skip 2Nx2N, merge_idx: %d\n", merge_idx); |
4114 | 0 | } |
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 | 0 | { |
4127 | 0 | logtrace(LogSlice, "read_prediction_unit %d;%d %dx%d\n", xC + xB, yC + xB, nPbW, nPbH); |
4128 | |
|
4129 | 0 | int x0 = xC + xB; |
4130 | 0 | int y0 = yC + yB; |
4131 | |
|
4132 | 0 | slice_segment_header* shdr = tctx->shdr; |
4133 | |
|
4134 | 0 | int merge_flag = decode_merge_flag(tctx); |
4135 | 0 | tctx->motion.merge_flag = merge_flag; |
4136 | |
|
4137 | 0 | if (merge_flag) { |
4138 | 0 | int merge_idx = decode_merge_idx(tctx); |
4139 | |
|
4140 | 0 | logtrace(LogSlice, "prediction unit %d,%d, merge mode, index: %d\n", x0, y0, merge_idx); |
4141 | |
|
4142 | 0 | tctx->motion.merge_idx = merge_idx; |
4143 | 0 | } |
4144 | 0 | else { |
4145 | | // no merge flag |
4146 | 0 | enum InterPredIdc inter_pred_idc; |
4147 | |
|
4148 | 0 | if (shdr->slice_type == SLICE_TYPE_B) { |
4149 | 0 | inter_pred_idc = decode_inter_pred_idc(tctx, x0, y0, nPbW, nPbH, ctDepth); |
4150 | 0 | } |
4151 | 0 | else { |
4152 | 0 | inter_pred_idc = PRED_L0; |
4153 | 0 | } |
4154 | |
|
4155 | 0 | tctx->motion.inter_pred_idc = inter_pred_idc; // set_inter_pred_idc(ctx,x0,y0, inter_pred_idc); |
4156 | |
|
4157 | 0 | if (inter_pred_idc != PRED_L1) { |
4158 | 0 | 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 | 0 | 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 | 0 | tctx->motion.refIdx[0] = ref_idx_l0; |
4167 | |
|
4168 | 0 | read_mvd_coding(tctx, x0, y0, 0); |
4169 | |
|
4170 | 0 | int mvp_l0_flag = decode_mvp_lx_flag(tctx); // l0 |
4171 | 0 | tctx->motion.mvp_l0_flag = mvp_l0_flag; |
4172 | |
|
4173 | 0 | logtrace(LogSlice, "prediction unit %d,%d, L0, refIdx=%d mvp_l0_flag:%d\n", |
4174 | 0 | x0, y0, tctx->motion.refIdx[0], mvp_l0_flag); |
4175 | 0 | } |
4176 | | |
4177 | 0 | if (inter_pred_idc != PRED_L0) { |
4178 | 0 | 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 | 0 | 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 | 0 | tctx->motion.refIdx[1] = ref_idx_l1; |
4187 | |
|
4188 | 0 | if (shdr->mvd_l1_zero_flag && |
4189 | 0 | inter_pred_idc == PRED_BI) { |
4190 | 0 | tctx->motion.mvd[1][0] = 0; |
4191 | 0 | tctx->motion.mvd[1][1] = 0; |
4192 | 0 | } |
4193 | 0 | else { |
4194 | 0 | read_mvd_coding(tctx, x0, y0, 1); |
4195 | 0 | } |
4196 | |
|
4197 | 0 | int mvp_l1_flag = decode_mvp_lx_flag(tctx); // l1 |
4198 | 0 | tctx->motion.mvp_l1_flag = mvp_l1_flag; |
4199 | |
|
4200 | 0 | logtrace(LogSlice, "prediction unit %d,%d, L1, refIdx=%d mvp_l1_flag:%d\n", |
4201 | 0 | x0, y0, tctx->motion.refIdx[1], mvp_l1_flag); |
4202 | 0 | } |
4203 | 0 | } |
4204 | | |
4205 | | |
4206 | 0 | decode_prediction_unit(tctx->decctx, tctx->shdr, tctx->img, tctx->motion, |
4207 | 0 | xC, yC, xB, yB, nCS, nPbW, nPbH, partIdx); |
4208 | 0 | } |
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 | 0 | { |
4215 | 0 | const seq_parameter_set& sps = tctx->img->get_sps(); |
4216 | |
|
4217 | 0 | int nPcmBits; |
4218 | 0 | int bitDepth; |
4219 | |
|
4220 | 0 | int w = 1 << log2CbSize; |
4221 | 0 | int h = 1 << log2CbSize; |
4222 | |
|
4223 | 0 | if (cIdx > 0) { |
4224 | 0 | w /= sps.SubWidthC; |
4225 | 0 | h /= sps.SubHeightC; |
4226 | |
|
4227 | 0 | x0 /= sps.SubWidthC; |
4228 | 0 | y0 /= sps.SubHeightC; |
4229 | |
|
4230 | 0 | nPcmBits = sps.pcm_sample_bit_depth_chroma; |
4231 | 0 | bitDepth = sps.BitDepth_C; |
4232 | 0 | } |
4233 | 0 | else { |
4234 | 0 | nPcmBits = sps.pcm_sample_bit_depth_luma; |
4235 | 0 | bitDepth = sps.BitDepth_Y; |
4236 | 0 | } |
4237 | |
|
4238 | 0 | pixel_t* ptr; |
4239 | 0 | int stride; |
4240 | 0 | ptr = tctx->img->get_image_plane_at_pos_NEW<pixel_t>(cIdx, x0, y0); |
4241 | 0 | stride = tctx->img->get_image_stride(cIdx); |
4242 | |
|
4243 | 0 | int shift = bitDepth - nPcmBits; |
4244 | | |
4245 | | // a shift < 0 may result when the SPS sequence header is broken |
4246 | 0 | if (shift < 0) { |
4247 | 0 | shift = 0; |
4248 | 0 | } |
4249 | |
|
4250 | 0 | for (int y = 0; y < h; y++) |
4251 | 0 | for (int x = 0; x < w; x++) { |
4252 | 0 | int value = br.get_bits(nPcmBits); |
4253 | 0 | ptr[y * stride + x] = value << shift; |
4254 | 0 | } |
4255 | 0 | } Unexecuted instantiation: void read_pcm_samples_internal<unsigned short>(thread_context*, int, int, int, int, bitreader&) Unexecuted instantiation: void read_pcm_samples_internal<unsigned char>(thread_context*, int, int, int, int, bitreader&) |
4256 | | |
4257 | | static void read_pcm_samples(thread_context* tctx, int x0, int y0, int log2CbSize) |
4258 | 0 | { |
4259 | 0 | bitreader br(tctx->cabac_decoder.bitstream_curr, |
4260 | 0 | tctx->cabac_decoder.bitstream_end - tctx->cabac_decoder.bitstream_curr); |
4261 | |
|
4262 | 0 | if (tctx->img->high_bit_depth(0)) { |
4263 | 0 | read_pcm_samples_internal<uint16_t>(tctx, x0, y0, log2CbSize, 0, br); |
4264 | 0 | } |
4265 | 0 | else { |
4266 | 0 | read_pcm_samples_internal<uint8_t>(tctx, x0, y0, log2CbSize, 0, br); |
4267 | 0 | } |
4268 | |
|
4269 | 0 | if (tctx->img->get_sps().ChromaArrayType != CHROMA_MONO) { |
4270 | 0 | if (tctx->img->high_bit_depth(1)) { |
4271 | 0 | read_pcm_samples_internal<uint16_t>(tctx, x0, y0, log2CbSize, 1, br); |
4272 | 0 | read_pcm_samples_internal<uint16_t>(tctx, x0, y0, log2CbSize, 2, br); |
4273 | 0 | } |
4274 | 0 | else { |
4275 | 0 | read_pcm_samples_internal<uint8_t>(tctx, x0, y0, log2CbSize, 1, br); |
4276 | 0 | read_pcm_samples_internal<uint8_t>(tctx, x0, y0, log2CbSize, 2, br); |
4277 | 0 | } |
4278 | 0 | } |
4279 | |
|
4280 | 0 | br.prepare_for_CABAC(); |
4281 | 0 | tctx->cabac_decoder.bitstream_curr = br.data; |
4282 | 0 | tctx->cabac_decoder.init_CABAC(); |
4283 | 0 | } |
4284 | | |
4285 | | |
4286 | | int map_chroma_pred_mode(int intra_chroma_pred_mode, int IntraPredMode) |
4287 | 0 | { |
4288 | 0 | if (intra_chroma_pred_mode == 4) { |
4289 | 0 | return IntraPredMode; |
4290 | 0 | } |
4291 | 0 | else { |
4292 | 0 | static const enum IntraPredMode IntraPredModeCCand[4] = { |
4293 | 0 | INTRA_PLANAR, |
4294 | 0 | INTRA_ANGULAR_26, // vertical |
4295 | 0 | INTRA_ANGULAR_10, // horizontal |
4296 | 0 | INTRA_DC |
4297 | 0 | }; |
4298 | |
|
4299 | 0 | int IntraPredModeC = IntraPredModeCCand[intra_chroma_pred_mode]; |
4300 | 0 | if (IntraPredModeC == IntraPredMode) { |
4301 | 0 | return INTRA_ANGULAR_34; |
4302 | 0 | } |
4303 | 0 | else { |
4304 | 0 | return IntraPredModeC; |
4305 | 0 | } |
4306 | 0 | } |
4307 | 0 | } |
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 | 0 | { |
4320 | 0 | de265_image* img = tctx->img; |
4321 | 0 | const seq_parameter_set& sps = img->get_sps(); |
4322 | 0 | const pic_parameter_set& pps = img->get_pps(); |
4323 | 0 | slice_segment_header* shdr = tctx->shdr; |
4324 | |
|
4325 | 0 | 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 | 0 | 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 | 0 | img->clear_split_transform_flags(x0, y0, log2CbSize); |
4337 | |
|
4338 | 0 | int nCbS = 1 << log2CbSize; // number of coding block samples |
4339 | |
|
4340 | 0 | decode_quantization_parameters(tctx, x0, y0, x0, y0); |
4341 | | |
4342 | |
|
4343 | 0 | if (pps.transquant_bypass_enable_flag) { |
4344 | 0 | int transquant_bypass = decode_transquant_bypass_flag(tctx); |
4345 | |
|
4346 | 0 | tctx->cu_transquant_bypass_flag = transquant_bypass; |
4347 | |
|
4348 | 0 | if (transquant_bypass) { |
4349 | 0 | img->set_cu_transquant_bypass(x0, y0, log2CbSize); |
4350 | 0 | } |
4351 | 0 | } |
4352 | 0 | else { |
4353 | 0 | tctx->cu_transquant_bypass_flag = 0; |
4354 | 0 | } |
4355 | |
|
4356 | 0 | uint8_t cu_skip_flag = 0; |
4357 | 0 | if (shdr->slice_type != SLICE_TYPE_I) { |
4358 | 0 | cu_skip_flag = decode_cu_skip_flag(tctx, x0, y0, ctDepth); |
4359 | 0 | } |
4360 | |
|
4361 | 0 | int IntraSplitFlag = 0; |
4362 | |
|
4363 | 0 | enum PredMode cuPredMode; |
4364 | |
|
4365 | 0 | if (cu_skip_flag) { |
4366 | 0 | read_prediction_unit_SKIP(tctx, x0, y0, nCbS, nCbS); |
4367 | |
|
4368 | 0 | img->set_PartMode(x0, y0, PART_2Nx2N); // need this for deblocking filter |
4369 | 0 | img->set_pred_mode(x0, y0, log2CbSize, MODE_SKIP); |
4370 | 0 | cuPredMode = MODE_SKIP; |
4371 | |
|
4372 | 0 | logtrace(LogSlice, "CU pred mode: SKIP\n"); |
4373 | | |
4374 | | |
4375 | | // DECODE |
4376 | |
|
4377 | 0 | int nCS_L = 1 << log2CbSize; |
4378 | 0 | decode_prediction_unit(tctx->decctx, tctx->shdr, tctx->img, tctx->motion, |
4379 | 0 | x0, y0, 0, 0, nCS_L, nCS_L, nCS_L, 0); |
4380 | 0 | } |
4381 | 0 | else /* not skipped */ { |
4382 | 0 | if (shdr->slice_type != SLICE_TYPE_I) { |
4383 | 0 | int pred_mode_flag = decode_pred_mode_flag(tctx); |
4384 | 0 | cuPredMode = pred_mode_flag ? MODE_INTRA : MODE_INTER; |
4385 | 0 | } |
4386 | 0 | else { |
4387 | 0 | cuPredMode = MODE_INTRA; |
4388 | 0 | } |
4389 | |
|
4390 | 0 | img->set_pred_mode(x0, y0, log2CbSize, cuPredMode); |
4391 | |
|
4392 | 0 | logtrace(LogSlice, "CU pred mode: %s\n", cuPredMode == MODE_INTRA ? "INTRA" : "INTER"); |
4393 | | |
4394 | |
|
4395 | 0 | enum PartMode PartMode; |
4396 | |
|
4397 | 0 | if (cuPredMode != MODE_INTRA || |
4398 | 0 | log2CbSize == sps.Log2MinCbSizeY) { |
4399 | 0 | PartMode = decode_part_mode(tctx, cuPredMode, log2CbSize); |
4400 | |
|
4401 | 0 | if (PartMode == PART_NxN && cuPredMode == MODE_INTRA) { |
4402 | 0 | IntraSplitFlag = 1; |
4403 | 0 | } |
4404 | 0 | } |
4405 | 0 | else { |
4406 | 0 | PartMode = PART_2Nx2N; |
4407 | 0 | } |
4408 | |
|
4409 | 0 | img->set_PartMode(x0, y0, PartMode); // needed for deblocking ? |
4410 | |
|
4411 | 0 | logtrace(LogSlice, "PartMode: %s\n", part_mode_name(PartMode)); |
4412 | | |
4413 | |
|
4414 | 0 | bool pcm_flag = false; |
4415 | |
|
4416 | 0 | if (cuPredMode == MODE_INTRA) { |
4417 | 0 | if (PartMode == PART_2Nx2N && sps.pcm_enabled_flag && |
4418 | 0 | log2CbSize >= sps.Log2MinIpcmCbSizeY && |
4419 | 0 | log2CbSize <= sps.Log2MaxIpcmCbSizeY) { |
4420 | 0 | pcm_flag = tctx->cabac_decoder.decode_term_bit(); |
4421 | 0 | } |
4422 | |
|
4423 | 0 | if (pcm_flag) { |
4424 | 0 | img->set_pcm_flag(x0, y0, log2CbSize); |
4425 | |
|
4426 | 0 | read_pcm_samples(tctx, x0, y0, log2CbSize); |
4427 | 0 | } |
4428 | 0 | else { |
4429 | 0 | int pbOffset = (PartMode == PART_NxN) ? (nCbS / 2) : nCbS; |
4430 | 0 | int log2IntraPredSize = (PartMode == PART_NxN) ? (log2CbSize - 1) : log2CbSize; |
4431 | |
|
4432 | 0 | logtrace(LogSlice, "nCbS:%d pbOffset:%d\n", nCbS, pbOffset); |
4433 | |
|
4434 | 0 | int prev_intra_luma_pred_flag[4]; |
4435 | |
|
4436 | 0 | int idx = 0; |
4437 | 0 | for (int j = 0; j < nCbS; j += pbOffset) |
4438 | 0 | for (int i = 0; i < nCbS; i += pbOffset) { |
4439 | 0 | prev_intra_luma_pred_flag[idx++] = decode_prev_intra_luma_pred_flag(tctx); |
4440 | 0 | } |
4441 | |
|
4442 | 0 | int mpm_idx[4], rem_intra_luma_pred_mode[4]; |
4443 | 0 | idx = 0; |
4444 | |
|
4445 | 0 | int availableA0 = check_CTB_available(img, x0, y0, x0 - 1, y0); |
4446 | 0 | int availableB0 = check_CTB_available(img, x0, y0, x0, y0 - 1); |
4447 | |
|
4448 | 0 | for (int j = 0; j < nCbS; j += pbOffset) |
4449 | 0 | for (int i = 0; i < nCbS; i += pbOffset) { |
4450 | 0 | if (prev_intra_luma_pred_flag[idx]) { |
4451 | 0 | mpm_idx[idx] = decode_mpm_idx(tctx); |
4452 | 0 | } |
4453 | 0 | else { |
4454 | 0 | rem_intra_luma_pred_mode[idx] = decode_rem_intra_luma_pred_mode(tctx); |
4455 | 0 | } |
4456 | | |
4457 | |
|
4458 | 0 | int x = x0 + i; |
4459 | 0 | int y = y0 + j; |
4460 | | |
4461 | | // --- find intra prediction mode --- |
4462 | |
|
4463 | 0 | int IntraPredMode; |
4464 | |
|
4465 | 0 | int availableA = availableA0 || (i > 0); // left candidate always available for right blk |
4466 | 0 | int availableB = availableB0 || (j > 0); // top candidate always available for bottom blk |
4467 | | |
4468 | |
|
4469 | 0 | int PUidx = (x >> sps.Log2MinPUSize) + (y >> sps.Log2MinPUSize) * sps.PicWidthInMinPUs; |
4470 | |
|
4471 | 0 | enum IntraPredMode candModeList[3]; |
4472 | |
|
4473 | 0 | fillIntraPredModeCandidates(candModeList, x, y, PUidx, |
4474 | 0 | availableA, availableB, img); |
4475 | |
|
4476 | 0 | for (int i = 0; i < 3; i++) |
4477 | 0 | logtrace(LogSlice, "candModeList[%d] = %d\n", i, candModeList[i]); |
4478 | |
|
4479 | 0 | if (prev_intra_luma_pred_flag[idx] == 1) { |
4480 | 0 | IntraPredMode = candModeList[mpm_idx[idx]]; |
4481 | 0 | } |
4482 | 0 | else { |
4483 | | // sort candModeList |
4484 | |
|
4485 | 0 | if (candModeList[0] > candModeList[1]) { |
4486 | 0 | std::swap(candModeList[0], candModeList[1]); |
4487 | 0 | } |
4488 | 0 | if (candModeList[0] > candModeList[2]) { |
4489 | 0 | std::swap(candModeList[0], candModeList[2]); |
4490 | 0 | } |
4491 | 0 | if (candModeList[1] > candModeList[2]) { |
4492 | 0 | std::swap(candModeList[1], candModeList[2]); |
4493 | 0 | } |
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 | 0 | IntraPredMode = rem_intra_luma_pred_mode[idx]; |
4498 | 0 | for (int n = 0; n <= 2; n++) { |
4499 | 0 | if (IntraPredMode >= candModeList[n]) { IntraPredMode++; } |
4500 | 0 | } |
4501 | 0 | } |
4502 | |
|
4503 | 0 | logtrace(LogSlice, "IntraPredMode[%d][%d] = %d (log2blk:%d)\n", x, y, IntraPredMode, log2IntraPredSize); |
4504 | |
|
4505 | 0 | img->set_IntraPredMode(PUidx, log2IntraPredSize, |
4506 | 0 | (enum IntraPredMode) IntraPredMode); |
4507 | |
|
4508 | 0 | idx++; |
4509 | 0 | } |
4510 | | |
4511 | | |
4512 | | // set chroma intra prediction mode |
4513 | |
|
4514 | 0 | if (sps.ChromaArrayType == CHROMA_444) { |
4515 | | // chroma 4:4:4 |
4516 | |
|
4517 | 0 | idx = 0; |
4518 | 0 | for (int j = 0; j < nCbS; j += pbOffset) |
4519 | 0 | for (int i = 0; i < nCbS; i += pbOffset) { |
4520 | 0 | int x = x0 + i; |
4521 | 0 | int y = y0 + j; |
4522 | |
|
4523 | 0 | int intra_chroma_pred_mode = decode_intra_chroma_pred_mode(tctx); |
4524 | 0 | int IntraPredMode = img->get_IntraPredMode(x, y); |
4525 | |
|
4526 | 0 | int IntraPredModeC = map_chroma_pred_mode(intra_chroma_pred_mode, IntraPredMode); |
4527 | |
|
4528 | 0 | logtrace(LogSlice, "IntraPredModeC[%d][%d]: %d (blksize:%d)\n", x, y, IntraPredModeC, |
4529 | 0 | 1 << log2IntraPredSize); |
4530 | |
|
4531 | 0 | img->set_IntraPredModeC(x, y, log2IntraPredSize, |
4532 | 0 | (enum IntraPredMode) IntraPredModeC, |
4533 | 0 | intra_chroma_pred_mode == 4); |
4534 | 0 | idx++; |
4535 | 0 | } |
4536 | 0 | } |
4537 | 0 | else if (sps.ChromaArrayType != CHROMA_MONO) { |
4538 | | // chroma 4:2:0 and 4:2:2 |
4539 | |
|
4540 | 0 | int intra_chroma_pred_mode = decode_intra_chroma_pred_mode(tctx); |
4541 | 0 | int IntraPredMode = img->get_IntraPredMode(x0, y0); |
4542 | 0 | logtrace(LogSlice, "IntraPredMode: %d\n", IntraPredMode); |
4543 | 0 | int IntraPredModeC = map_chroma_pred_mode(intra_chroma_pred_mode, IntraPredMode); |
4544 | |
|
4545 | 0 | if (sps.ChromaArrayType == CHROMA_422) { |
4546 | 0 | IntraPredModeC = map_chroma_422[IntraPredModeC]; |
4547 | 0 | } |
4548 | |
|
4549 | 0 | img->set_IntraPredModeC(x0, y0, log2CbSize, |
4550 | 0 | (enum IntraPredMode) IntraPredModeC, |
4551 | 0 | intra_chroma_pred_mode == 4); |
4552 | 0 | } |
4553 | 0 | } |
4554 | 0 | } |
4555 | 0 | else { |
4556 | | // INTER |
4557 | 0 | int nCS = 1 << log2CbSize; |
4558 | |
|
4559 | 0 | if (PartMode == PART_2Nx2N) { |
4560 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS, nCbS, ctDepth, nCS, 0); |
4561 | 0 | } |
4562 | 0 | else if (PartMode == PART_2NxN) { |
4563 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS, nCbS / 2, ctDepth, nCS, 0); |
4564 | 0 | read_prediction_unit(tctx, x0, y0, 0, nCbS / 2, nCbS, nCbS / 2, ctDepth, nCS, 1); |
4565 | 0 | } |
4566 | 0 | else if (PartMode == PART_Nx2N) { |
4567 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS / 2, nCbS, ctDepth, nCS, 0); |
4568 | 0 | read_prediction_unit(tctx, x0, y0, nCbS / 2, 0, nCbS / 2, nCbS, ctDepth, nCS, 1); |
4569 | 0 | } |
4570 | 0 | else if (PartMode == PART_2NxnU) { |
4571 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS, nCbS / 4, ctDepth, nCS, 0); |
4572 | 0 | read_prediction_unit(tctx, x0, y0, 0, nCbS / 4, nCbS, nCbS * 3 / 4, ctDepth, nCS, 1); |
4573 | 0 | } |
4574 | 0 | else if (PartMode == PART_2NxnD) { |
4575 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS, nCbS * 3 / 4, ctDepth, nCS, 0); |
4576 | 0 | read_prediction_unit(tctx, x0, y0, 0, nCbS * 3 / 4, nCbS, nCbS / 4, ctDepth, nCS, 1); |
4577 | 0 | } |
4578 | 0 | else if (PartMode == PART_nLx2N) { |
4579 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS / 4, nCbS, ctDepth, nCS, 0); |
4580 | 0 | read_prediction_unit(tctx, x0, y0, nCbS / 4, 0, nCbS * 3 / 4, nCbS, ctDepth, nCS, 1); |
4581 | 0 | } |
4582 | 0 | else if (PartMode == PART_nRx2N) { |
4583 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS * 3 / 4, nCbS, ctDepth, nCS, 0); |
4584 | 0 | read_prediction_unit(tctx, x0, y0, nCbS * 3 / 4, 0, nCbS / 4, nCbS, ctDepth, nCS, 1); |
4585 | 0 | } |
4586 | 0 | else if (PartMode == PART_NxN) { |
4587 | 0 | read_prediction_unit(tctx, x0, y0, 0, 0, nCbS / 2, nCbS / 2, ctDepth, nCS, 0); |
4588 | 0 | read_prediction_unit(tctx, x0, y0, nCbS / 2, 0, nCbS / 2, nCbS / 2, ctDepth, nCS, 1); |
4589 | 0 | read_prediction_unit(tctx, x0, y0, 0, nCbS / 2, nCbS / 2, nCbS / 2, ctDepth, nCS, 2); |
4590 | 0 | read_prediction_unit(tctx, x0, y0, nCbS / 2, nCbS / 2, nCbS / 2, nCbS / 2, ctDepth, nCS, 3); |
4591 | 0 | } |
4592 | 0 | else { |
4593 | 0 | assert(0); // undefined PartMode |
4594 | 0 | } |
4595 | 0 | } // INTER |
4596 | | |
4597 | | |
4598 | | // decode residual |
4599 | | |
4600 | 0 | if (!pcm_flag) { |
4601 | | // !pcm |
4602 | 0 | bool rqt_root_cbf; |
4603 | |
|
4604 | 0 | uint8_t merge_flag = tctx->motion.merge_flag; // !!get_merge_flag(ctx,x0,y0); |
4605 | |
|
4606 | 0 | if (cuPredMode != MODE_INTRA && |
4607 | 0 | !(PartMode == PART_2Nx2N && merge_flag)) { |
4608 | 0 | rqt_root_cbf = !!decode_rqt_root_cbf(tctx); |
4609 | 0 | } |
4610 | 0 | 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 | 0 | rqt_root_cbf = true; |
4617 | 0 | } |
4618 | | |
4619 | | //set_rqt_root_cbf(ctx,x0,y0, log2CbSize, rqt_root_cbf); |
4620 | |
|
4621 | 0 | if (rqt_root_cbf) { |
4622 | 0 | int MaxTrafoDepth; |
4623 | |
|
4624 | 0 | if (cuPredMode == MODE_INTRA) { |
4625 | 0 | MaxTrafoDepth = sps.max_transform_hierarchy_depth_intra + IntraSplitFlag; |
4626 | 0 | } |
4627 | 0 | else { |
4628 | 0 | MaxTrafoDepth = sps.max_transform_hierarchy_depth_inter; |
4629 | 0 | } |
4630 | |
|
4631 | 0 | logtrace(LogSlice, "MaxTrafoDepth: %d\n", MaxTrafoDepth); |
4632 | |
|
4633 | 0 | uint8_t initial_chroma_cbf = 1; |
4634 | 0 | if (sps.ChromaArrayType == CHROMA_MONO) { |
4635 | 0 | initial_chroma_cbf = 0; |
4636 | 0 | } |
4637 | |
|
4638 | 0 | read_transform_tree(tctx, x0, y0, x0, y0, x0, y0, log2CbSize, 0, 0, |
4639 | 0 | MaxTrafoDepth, IntraSplitFlag, cuPredMode, |
4640 | 0 | initial_chroma_cbf, initial_chroma_cbf); |
4641 | 0 | } |
4642 | 0 | } // !pcm |
4643 | 0 | } |
4644 | 0 | } |
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 | 0 | { |
4655 | 0 | logtrace(LogSlice, "- read_coding_quadtree %d;%d cbsize:%d depth:%d POC:%d\n", x0, y0, 1 << log2CbSize, ctDepth, tctx->img->PicOrderCntVal); |
4656 | |
|
4657 | 0 | de265_image* img = tctx->img; |
4658 | 0 | const seq_parameter_set& sps = img->get_sps(); |
4659 | 0 | const pic_parameter_set& pps = img->get_pps(); |
4660 | |
|
4661 | 0 | 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 | 0 | if (x0 + (1 << log2CbSize) <= sps.pic_width_in_luma_samples && |
4668 | 0 | y0 + (1 << log2CbSize) <= sps.pic_height_in_luma_samples && |
4669 | 0 | log2CbSize > sps.Log2MinCbSizeY) { |
4670 | 0 | split_flag = decode_split_cu_flag(tctx, x0, y0, ctDepth); |
4671 | 0 | } |
4672 | 0 | else { |
4673 | 0 | if (log2CbSize > sps.Log2MinCbSizeY) { split_flag = 1; } |
4674 | 0 | else { split_flag = 0; } |
4675 | 0 | } |
4676 | | |
4677 | |
|
4678 | 0 | if (pps.cu_qp_delta_enabled_flag && |
4679 | 0 | log2CbSize >= pps.Log2MinCuQpDeltaSize) { |
4680 | 0 | tctx->IsCuQpDeltaCoded = 0; |
4681 | 0 | tctx->CuQpDelta = 0; |
4682 | 0 | } |
4683 | 0 | else { |
4684 | | // shdr->CuQpDelta = 0; // TODO check: is this the right place to set to default value ? |
4685 | 0 | } |
4686 | | |
4687 | |
|
4688 | 0 | if (tctx->shdr->cu_chroma_qp_offset_enabled_flag && |
4689 | 0 | log2CbSize >= pps.Log2MinCuChromaQpOffsetSize) { |
4690 | 0 | tctx->IsCuChromaQpOffsetCoded = 0; |
4691 | 0 | } |
4692 | |
|
4693 | 0 | if (split_flag) { |
4694 | 0 | int x1 = x0 + (1 << (log2CbSize - 1)); |
4695 | 0 | int y1 = y0 + (1 << (log2CbSize - 1)); |
4696 | |
|
4697 | 0 | read_coding_quadtree(tctx, x0, y0, log2CbSize - 1, ctDepth + 1); |
4698 | |
|
4699 | 0 | if (x1 < sps.pic_width_in_luma_samples) |
4700 | 0 | read_coding_quadtree(tctx, x1, y0, log2CbSize - 1, ctDepth + 1); |
4701 | |
|
4702 | 0 | if (y1 < sps.pic_height_in_luma_samples) |
4703 | 0 | read_coding_quadtree(tctx, x0, y1, log2CbSize - 1, ctDepth + 1); |
4704 | |
|
4705 | 0 | if (x1 < sps.pic_width_in_luma_samples && |
4706 | 0 | y1 < sps.pic_height_in_luma_samples) |
4707 | 0 | read_coding_quadtree(tctx, x1, y1, log2CbSize - 1, ctDepth + 1); |
4708 | 0 | } |
4709 | 0 | else { |
4710 | | // set ctDepth of this CU |
4711 | |
|
4712 | 0 | img->set_ctDepth(x0, y0, log2CbSize, ctDepth); |
4713 | |
|
4714 | 0 | read_coding_unit(tctx, x0, y0, log2CbSize, ctDepth); |
4715 | 0 | } |
4716 | |
|
4717 | 0 | logtrace(LogSlice, "-\n"); |
4718 | 0 | } |
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 | 0 | { |
4736 | 0 | const pic_parameter_set& pps = tctx->img->get_pps(); |
4737 | 0 | const seq_parameter_set& sps = tctx->img->get_sps(); |
4738 | |
|
4739 | 0 | const uint16_t ctbW = sps.PicWidthInCtbsY; |
4740 | 0 | 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 | 0 | if ((!first_independent_substream || tctx->CtbY != startCtbY) && |
4747 | 0 | pps.entropy_coding_sync_enabled_flag && |
4748 | 0 | tctx->CtbY >= 1 && tctx->CtbX == 0) { |
4749 | 0 | if (sps.PicWidthInCtbsY > 1) { |
4750 | 0 | assert(tctx->CtbY >= 1); |
4751 | 0 | 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 | 0 | tctx->img->wait_for_progress(tctx->task, 1, tctx->CtbY - 1,CTB_PROGRESS_PREFILTER); |
4759 | | |
4760 | | // copy CABAC model from previous CTB row |
4761 | 0 | tctx->ctx_model = tctx->imgunit->ctx_models[(tctx->CtbY - 1)]; |
4762 | 0 | tctx->imgunit->ctx_models[(tctx->CtbY - 1)].release(); // not used anymore |
4763 | | |
4764 | | // also restore the StatCoeff[] state for persistent_rice_adaptation |
4765 | 0 | for (int i = 0; i < 4; i++) { |
4766 | 0 | tctx->StatCoeff[i] = tctx->imgunit->StatCoeff_models[(tctx->CtbY - 1)][i]; |
4767 | 0 | } |
4768 | 0 | } |
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 | 0 | } |
4774 | | |
4775 | | |
4776 | 0 | do { |
4777 | 0 | const uint32_t ctbx = tctx->CtbX; |
4778 | 0 | const uint32_t ctby = tctx->CtbY; |
4779 | |
|
4780 | 0 | if (ctbx + ctby * ctbW >= pps.CtbAddrRStoTS.size()) { |
4781 | 0 | return Decode_Error; |
4782 | 0 | } |
4783 | | |
4784 | 0 | if (ctbx >= sps.PicWidthInCtbsY || |
4785 | 0 | ctby >= sps.PicHeightInCtbsY) { |
4786 | 0 | return Decode_Error; |
4787 | 0 | } |
4788 | | |
4789 | 0 | 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 | 0 | tctx->img->wait_for_progress(tctx->task, ctbx + 1, ctby - 1, CTB_PROGRESS_PREFILTER); |
4795 | 0 | } |
4796 | | |
4797 | | //printf("%p: decode %d;%d\n", tctx, tctx->CtbX,tctx->CtbY); |
4798 | | |
4799 | | |
4800 | | // read and decode CTB |
4801 | |
|
4802 | 0 | if (tctx->ctx_model.empty() == false) { |
4803 | 0 | return Decode_Error; |
4804 | 0 | } |
4805 | | |
4806 | 0 | read_coding_tree_unit(tctx); |
4807 | | |
4808 | | |
4809 | | // save CABAC-model for WPP (except in last CTB row) |
4810 | |
|
4811 | 0 | if (pps.entropy_coding_sync_enabled_flag && |
4812 | 0 | ctbx == 1 && |
4813 | 0 | ctby + 1 < sps.PicHeightInCtbsY) { |
4814 | | // no storage for context table has been allocated |
4815 | 0 | if (tctx->imgunit->ctx_models.size() <= ctby) { |
4816 | 0 | return Decode_Error; |
4817 | 0 | } |
4818 | | |
4819 | 0 | tctx->imgunit->ctx_models[ctby] = tctx->ctx_model; |
4820 | 0 | tctx->imgunit->ctx_models[ctby].decouple(); // store an independent copy |
4821 | | |
4822 | | // also save the StatCoeff[] state for persistent_rice_adaptation |
4823 | 0 | for (int i = 0; i < 4; i++) { |
4824 | 0 | tctx->imgunit->StatCoeff_models[ctby][i] = tctx->StatCoeff[i]; |
4825 | 0 | } |
4826 | 0 | } |
4827 | | |
4828 | | |
4829 | | // end of slice segment ? |
4830 | | |
4831 | 0 | 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 | 0 | 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 | 0 | if (pps.dependent_slice_segments_enabled_flag) { |
4839 | 0 | tctx->shdr->ctx_model_storage = tctx->ctx_model; |
4840 | 0 | tctx->shdr->ctx_model_storage.decouple(); // store an independent copy |
4841 | | |
4842 | | // also save the StatCoeff[] state for persistent_rice_adaptation |
4843 | 0 | for (int i = 0; i < 4; i++) { |
4844 | 0 | tctx->shdr->ctx_model_storage_StatCoeff[i] = tctx->StatCoeff[i]; |
4845 | 0 | } |
4846 | |
|
4847 | 0 | tctx->shdr->ctx_model_storage_defined = true; |
4848 | 0 | } |
4849 | 0 | } |
4850 | |
|
4851 | 0 | 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 | 0 | 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 | 0 | const int lastCtbY = tctx->CtbY; |
4860 | |
|
4861 | 0 | bool endOfPicture = advanceCtbAddr(tctx); // true if we read past the end of the image |
4862 | |
|
4863 | 0 | if (endOfPicture && |
4864 | 0 | end_of_slice_segment_flag == false) { |
4865 | 0 | tctx->decctx->add_warning(DE265_WARNING_CTB_OUTSIDE_IMAGE_AREA, false); |
4866 | 0 | tctx->img->integrity = INTEGRITY_DECODING_ERRORS; |
4867 | 0 | return Decode_Error; |
4868 | 0 | } |
4869 | | |
4870 | | |
4871 | 0 | 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 | 0 | return Decode_EndOfSliceSegment; |
4884 | 0 | } |
4885 | | |
4886 | | |
4887 | 0 | if (!end_of_slice_segment_flag) { |
4888 | 0 | bool end_of_sub_stream = false; |
4889 | 0 | end_of_sub_stream |= (pps.tiles_enabled_flag && |
4890 | 0 | pps.TileId[tctx->CtbAddrInTS] != pps.TileId[tctx->CtbAddrInTS - 1]); |
4891 | 0 | end_of_sub_stream |= (pps.entropy_coding_sync_enabled_flag && |
4892 | 0 | lastCtbY != tctx->CtbY); |
4893 | |
|
4894 | 0 | if (end_of_sub_stream) { |
4895 | 0 | int end_of_sub_stream_one_bit = tctx->cabac_decoder.decode_term_bit(); |
4896 | 0 | if (!end_of_sub_stream_one_bit) { |
4897 | 0 | tctx->decctx->add_warning(DE265_WARNING_EOSS_BIT_NOT_SET, false); |
4898 | 0 | tctx->img->integrity = INTEGRITY_DECODING_ERRORS; |
4899 | 0 | return Decode_Error; |
4900 | 0 | } |
4901 | | |
4902 | 0 | tctx->cabac_decoder.init_CABAC(); // byte alignment |
4903 | 0 | return Decode_EndOfSubstream; |
4904 | 0 | } |
4905 | 0 | } |
4906 | 0 | } while (true); |
4907 | 0 | } |
4908 | | |
4909 | | |
4910 | | bool initialize_CABAC_at_slice_segment_start(thread_context* tctx) |
4911 | 0 | { |
4912 | 0 | de265_image* img = tctx->img; |
4913 | 0 | const pic_parameter_set& pps = img->get_pps(); |
4914 | 0 | const seq_parameter_set& sps = img->get_sps(); |
4915 | 0 | slice_segment_header* shdr = tctx->shdr; |
4916 | |
|
4917 | 0 | if (shdr->dependent_slice_segment_flag) { |
4918 | 0 | int prevCtb = pps.CtbAddrTStoRS[pps.CtbAddrRStoTS[shdr->slice_segment_address] - 1]; |
4919 | |
|
4920 | 0 | uint16_t sliceIdx = img->get_SliceHeaderIndex_atIndex(prevCtb); |
4921 | 0 | if (sliceIdx >= img->slices.size()) { |
4922 | 0 | return false; |
4923 | 0 | } |
4924 | 0 | slice_segment_header* prevCtbHdr = img->slices[sliceIdx]; |
4925 | |
|
4926 | 0 | if (pps.is_tile_start_CTB(shdr->slice_segment_address % sps.PicWidthInCtbsY, |
4927 | 0 | shdr->slice_segment_address / sps.PicWidthInCtbsY |
4928 | 0 | )) { |
4929 | 0 | initialize_CABAC_models(tctx); |
4930 | 0 | } |
4931 | 0 | else { |
4932 | | // wait for previous slice to finish decoding |
4933 | | |
4934 | | //printf("wait for previous slice to finish decoding\n"); |
4935 | | |
4936 | |
|
4937 | 0 | slice_unit* prevSliceSegment = tctx->imgunit->get_prev_slice_segment(tctx->sliceunit); |
4938 | | //assert(prevSliceSegment); |
4939 | 0 | if (prevSliceSegment == nullptr) { |
4940 | 0 | return false; |
4941 | 0 | } |
4942 | | |
4943 | 0 | 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 | 0 | if (!prevCtbHdr->ctx_model_storage_defined) { |
4954 | 0 | return false; |
4955 | 0 | } |
4956 | | |
4957 | 0 | tctx->ctx_model = prevCtbHdr->ctx_model_storage; |
4958 | 0 | prevCtbHdr->ctx_model_storage.release(); |
4959 | | |
4960 | | // also restore the StatCoeff[] state for persistent_rice_adaptation |
4961 | 0 | for (int i = 0; i < 4; i++) { |
4962 | 0 | tctx->StatCoeff[i] = prevCtbHdr->ctx_model_storage_StatCoeff[i]; |
4963 | 0 | } |
4964 | 0 | } |
4965 | 0 | } |
4966 | 0 | else { |
4967 | 0 | initialize_CABAC_models(tctx); |
4968 | 0 | } |
4969 | | |
4970 | 0 | return true; |
4971 | 0 | } |
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 | 0 | { |
4992 | 0 | thread_task_slice_segment* data = this; |
4993 | 0 | thread_context* tctx = data->tctx; |
4994 | 0 | de265_image* img = tctx->img; |
4995 | |
|
4996 | 0 | state = Running; |
4997 | 0 | img->thread_run(this); |
4998 | |
|
4999 | 0 | setCtbAddrFromTS(tctx); |
5000 | | |
5001 | | //printf("%p: A start decoding at %d/%d\n", tctx, tctx->CtbX,tctx->CtbY); |
5002 | |
|
5003 | 0 | if (data->firstSliceSubstream) { |
5004 | 0 | bool success = initialize_CABAC_at_slice_segment_start(tctx); |
5005 | 0 | if (!success) { |
5006 | 0 | state = Finished; |
5007 | 0 | tctx->sliceunit->finished_threads.increase_progress(1); |
5008 | 0 | img->thread_finishes(this); |
5009 | 0 | return; |
5010 | 0 | } |
5011 | 0 | } |
5012 | 0 | else { |
5013 | 0 | initialize_CABAC_models(tctx); |
5014 | 0 | } |
5015 | | |
5016 | 0 | tctx->cabac_decoder.init_CABAC(); |
5017 | | |
5018 | | /*enum DecodeResult result =*/ |
5019 | 0 | decode_substream(tctx, false, data->firstSliceSubstream); |
5020 | |
|
5021 | 0 | state = Finished; |
5022 | 0 | tctx->sliceunit->finished_threads.increase_progress(1); |
5023 | 0 | img->thread_finishes(this); |
5024 | |
|
5025 | 0 | return; // DE265_OK; |
5026 | 0 | } |
5027 | | |
5028 | | |
5029 | | void thread_task_ctb_row::work() |
5030 | 0 | { |
5031 | 0 | thread_task_ctb_row* data = this; |
5032 | 0 | thread_context* tctx = data->tctx; |
5033 | 0 | de265_image* img = tctx->img; |
5034 | |
|
5035 | 0 | const seq_parameter_set& sps = img->get_sps(); |
5036 | 0 | int ctbW = sps.PicWidthInCtbsY; |
5037 | |
|
5038 | 0 | state = Running; |
5039 | 0 | img->thread_run(this); |
5040 | |
|
5041 | 0 | setCtbAddrFromTS(tctx); |
5042 | |
|
5043 | 0 | int ctby = tctx->CtbAddrInRS / ctbW; |
5044 | 0 | int myCtbRow = ctby; |
5045 | | |
5046 | | //printf("start CTB-row decoding at row %d\n", ctby); |
5047 | |
|
5048 | 0 | if (data->firstSliceSubstream) { |
5049 | 0 | bool success = initialize_CABAC_at_slice_segment_start(tctx); |
5050 | 0 | if (!success) { |
5051 | | // could not decode this row, mark whole row as finished |
5052 | 0 | for (int x = 0; x < ctbW; x++) { |
5053 | 0 | img->ctb_progress[myCtbRow * ctbW + x].set_progress(CTB_PROGRESS_PREFILTER); |
5054 | 0 | } |
5055 | |
|
5056 | 0 | state = Finished; |
5057 | 0 | tctx->sliceunit->finished_threads.increase_progress(1); |
5058 | 0 | img->thread_finishes(this); |
5059 | 0 | return; |
5060 | 0 | } |
5061 | | //initialize_CABAC(tctx); |
5062 | 0 | } |
5063 | | |
5064 | 0 | tctx->cabac_decoder.init_CABAC(); |
5065 | |
|
5066 | 0 | bool firstIndependentSubstream = |
5067 | 0 | data->firstSliceSubstream && !tctx->shdr->dependent_slice_segment_flag; |
5068 | | |
5069 | | /*enum DecodeResult result =*/ |
5070 | 0 | 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 | 0 | if (tctx->CtbY == myCtbRow) { |
5077 | 0 | int lastCtbX = sps.PicWidthInCtbsY; // assume no tiles when WPP is on |
5078 | 0 | for (int x = tctx->CtbX; x < lastCtbX; x++) { |
5079 | 0 | if (x < sps.PicWidthInCtbsY && |
5080 | 0 | myCtbRow < sps.PicHeightInCtbsY) { |
5081 | 0 | img->ctb_progress[myCtbRow * ctbW + x].set_progress(CTB_PROGRESS_PREFILTER); |
5082 | 0 | } |
5083 | 0 | } |
5084 | 0 | } |
5085 | |
|
5086 | 0 | state = Finished; |
5087 | 0 | tctx->sliceunit->finished_threads.increase_progress(1); |
5088 | 0 | img->thread_finishes(this); |
5089 | 0 | } |
5090 | | |
5091 | | |
5092 | | de265_error read_slice_segment_data(thread_context* tctx) |
5093 | 0 | { |
5094 | 0 | setCtbAddrFromTS(tctx); |
5095 | |
|
5096 | 0 | de265_image* img = tctx->img; |
5097 | 0 | const pic_parameter_set& pps = img->get_pps(); |
5098 | | //const seq_parameter_set& sps = img->get_sps(); |
5099 | 0 | slice_segment_header* shdr = tctx->shdr; |
5100 | |
|
5101 | 0 | bool success = initialize_CABAC_at_slice_segment_start(tctx); |
5102 | 0 | if (!success) { |
5103 | 0 | return DE265_ERROR_UNSPECIFIED_DECODING_ERROR; |
5104 | 0 | } |
5105 | | |
5106 | 0 | tctx->cabac_decoder.init_CABAC(); |
5107 | | |
5108 | | //printf("-----\n"); |
5109 | |
|
5110 | 0 | bool first_slice_substream = !shdr->dependent_slice_segment_flag; |
5111 | |
|
5112 | 0 | uint32_t substream = 0; |
5113 | |
|
5114 | 0 | enum DecodeResult result; |
5115 | 0 | do { |
5116 | | //int ctby = tctx->CtbY; |
5117 | | |
5118 | | |
5119 | | // check whether entry_points[] are correct in the bitstream |
5120 | |
|
5121 | 0 | 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 | 0 | substream++; |
5130 | | |
5131 | |
|
5132 | 0 | result = decode_substream(tctx, false, first_slice_substream); |
5133 | | |
5134 | |
|
5135 | 0 | if (result == Decode_EndOfSliceSegment || |
5136 | 0 | result == Decode_Error) { |
5137 | 0 | break; |
5138 | 0 | } |
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 | 0 | return DE265_OK; |
5148 | 0 | } |
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 | | */ |