Coverage Report

Created: 2026-05-16 06:28

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