Coverage Report

Created: 2026-10-17 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/sps.h
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef DE265_SPS_H
22
#define DE265_SPS_H
23
24
#include "libde265/vps.h"
25
#include "libde265/vui.h"
26
#include "libde265/bitstream.h"
27
#include "libde265/refpic.h"
28
#include "libde265/de265.h"
29
#include "libde265/cabac.h"
30
31
#include <vector>
32
33
class error_queue;
34
35
// #define MAX_REF_PIC_SETS 64  // maximum according to standard
36
constexpr int MAX_NUM_LT_REF_PICS_SPS = 32;
37
38
// This is just a safety range. It is chosen such that width/height fits into 16bit integers and the total number of pixels in 32bit integers.
39
constexpr int MAX_PICTURE_WIDTH  = 65535;
40
constexpr int MAX_PICTURE_HEIGHT = 65535;
41
42
// pic_width/height_in_luma_samples are stored as uint16_t and PicSizeInSamplesY as uint32_t,
43
// so these limits must keep width/height in 16 bits and their product in 32 bits.
44
static_assert(MAX_PICTURE_WIDTH  <= 0xFFFF, "picture width must fit in uint16_t");
45
static_assert(MAX_PICTURE_HEIGHT <= 0xFFFF, "picture height must fit in uint16_t");
46
static_assert((uint64_t)MAX_PICTURE_WIDTH * MAX_PICTURE_HEIGHT <= 0xFFFFFFFFu,
47
              "total luma sample count must fit in uint32_t");
48
49
enum {
50
  CHROMA_MONO = 0,
51
  CHROMA_420 = 1,
52
  CHROMA_422 = 2,
53
  CHROMA_444 = 3,
54
  CHROMA_444_SEPARATE
55
};
56
57
58
struct scaling_list_data {
59
  // structure size: approx. 4 kB
60
61
  uint8_t ScalingFactor_Size0[6][4][4];
62
  uint8_t ScalingFactor_Size1[6][8][8];
63
  uint8_t ScalingFactor_Size2[6][16][16];
64
  uint8_t ScalingFactor_Size3[6][32][32];
65
};
66
67
68
enum PresetSet {
69
  Preset_Default
70
};
71
72
73
class sps_range_extension
74
{
75
 public:
76
  sps_range_extension();
77
78
  uint8_t transform_skip_rotation_enabled_flag = 0;
79
  uint8_t transform_skip_context_enabled_flag = 0;
80
  uint8_t implicit_rdpcm_enabled_flag = 0;
81
  uint8_t explicit_rdpcm_enabled_flag = 0;
82
83
  // TODO: extended_precision_processing_flag is parsed, but the decoding process does not implement it.
84
  // A stream that sets the flag is decoded as if it were 0: the entropy decoder loses synchronization
85
  // and the pictures are wrong. The flag may be set in the 16 bit profiles only (Monochrome 16,
86
  // Main 4:4:4 16 Intra, Main 4:4:4 16 Still Picture, High Throughput 4:4:4 16 Intra). What is missing:
87
  // - the limited EGk binarization of coeff_abs_level_remaining (9.3.3.4, 9.3.3.11),
88
  // - the coefficient range CoeffMin..CoeffMax of Max(15, BitDepth+6) bits (7-27 to 7-30): coefficients
89
  //   are stored as int16_t and clipped to -32768..32767 in slice.cc, transform.cc and fallback-dct.cc,
90
  // - log2TransformRange in the scaling process (8-300 to 8-307),
91
  // - bdShift of the residual (8-295): transform.cc uses a local constant 0 instead of this flag.
92
  // Test streams can be encoded with HM (--ExtendedPrecision=1). FFmpeg does not implement the flag
93
  // either, so the HM decoder has to serve as the reference.
94
  uint8_t extended_precision_processing_flag = 0;
95
96
  uint8_t intra_smoothing_disabled_flag = 0;
97
  uint8_t high_precision_offsets_enabled_flag = 0;
98
  uint8_t persistent_rice_adaptation_enabled_flag = 0;
99
  uint8_t cabac_bypass_alignment_enabled_flag = 0;
100
101
  de265_error read(error_queue*, bitreader*);
102
  void dump(int fd) const;
103
};
104
105
106
class seq_parameter_set {
107
public:
108
  seq_parameter_set();
109
  ~seq_parameter_set();
110
111
  de265_error read(error_queue*, bitreader*);
112
  de265_error write(error_queue*, CABAC_encoder&);
113
114
  void dump(int fd) const;
115
116
  void set_defaults(enum PresetSet = Preset_Default);
117
  void set_CB_log2size_range(int mini,int maxi);
118
  void set_TB_log2size_range(int mini,int maxi);
119
  void set_resolution(int w,int h);
120
121
  bool sps_read = false; // whether the sps has been read from the bitstream
122
123
124
  uint8_t video_parameter_set_id;
125
  uint8_t sps_max_sub_layers;            // [1;7]
126
  bool sps_temporal_id_nesting_flag;
127
128
  profile_tier_level profile_tier_level_;
129
130
  uint8_t seq_parameter_set_id;       // [0;15]
131
  uint8_t chroma_format_idc;          // [0;3]
132
133
  bool separate_colour_plane_flag;
134
  uint16_t pic_width_in_luma_samples;   // <= MAX_PICTURE_WIDTH  (validated on parse)
135
  uint16_t pic_height_in_luma_samples;  // <= MAX_PICTURE_HEIGHT (validated on parse)
136
  bool conformance_window_flag;
137
138
  // In units of WinUnitX and WinUnitY. Validated on parse:
139
  // WinUnitX * (left + right) < pic_width_in_luma_samples, and the same vertically.
140
  uint16_t conf_win_left_offset;
141
  uint16_t conf_win_right_offset;
142
  uint16_t conf_win_top_offset;
143
  uint16_t conf_win_bottom_offset;
144
145
  uint8_t bit_depth_luma;              // [8;16]
146
  uint8_t bit_depth_chroma;            // [8;16]
147
148
  uint8_t log2_max_pic_order_cnt_lsb; // [4;16]
149
  bool sps_sub_layer_ordering_info_present_flag;
150
151
  uint8_t sps_max_dec_pic_buffering[7]; // for each temporal layer
152
  uint8_t sps_max_num_reorder_pics[7];
153
  uint32_t sps_max_latency_increase_plus1[7];
154
  bool     sps_max_latency_increase_present[7] = {};
155
156
  uint8_t log2_min_luma_coding_block_size;             // smallest CB size [3;6]
157
  uint8_t log2_diff_max_min_luma_coding_block_size;    // largest  CB size
158
  uint8_t log2_min_transform_block_size;               // smallest TB size [2;5]
159
  uint8_t log2_diff_max_min_transform_block_size;      // largest  TB size
160
  uint8_t max_transform_hierarchy_depth_inter;
161
  uint8_t max_transform_hierarchy_depth_intra;
162
163
  bool scaling_list_enable_flag;
164
  bool sps_scaling_list_data_present_flag; /* if not set, the default scaling lists will be set
165
                                              in scaling_list */
166
167
  struct scaling_list_data scaling_list;
168
169
  bool amp_enabled_flag;
170
  bool sample_adaptive_offset_enabled_flag;
171
  bool pcm_enabled_flag;
172
173
  uint8_t pcm_sample_bit_depth_luma;
174
  uint8_t pcm_sample_bit_depth_chroma;
175
  int  log2_min_pcm_luma_coding_block_size;
176
  int  log2_diff_max_min_pcm_luma_coding_block_size;
177
  bool pcm_loop_filter_disable_flag;
178
179
5.03k
  int num_short_term_ref_pic_sets() const { return ref_pic_sets.size(); }
180
  std::vector<ref_pic_set> ref_pic_sets; // [0 ; num_short_term_ref_pic_set (<=MAX_REF_PIC_SETS) )
181
182
  bool long_term_ref_pics_present_flag;
183
184
  uint8_t num_long_term_ref_pics_sps; // [0;32]
185
186
  int  lt_ref_pic_poc_lsb_sps[MAX_NUM_LT_REF_PICS_SPS];
187
  bool used_by_curr_pic_lt_sps_flag[MAX_NUM_LT_REF_PICS_SPS];
188
189
  bool sps_temporal_mvp_enabled_flag;
190
  bool strong_intra_smoothing_enable_flag;
191
192
  bool vui_parameters_present_flag;
193
  video_usability_information vui;
194
195
  bool sps_extension_present_flag;
196
  bool sps_range_extension_flag;
197
  bool sps_multilayer_extension_flag;
198
  uint8_t sps_extension_6bits;
199
200
  sps_range_extension range_extension;
201
202
  /*
203
    if( sps_extension_flag )
204
    while( more_rbsp_data() )
205
    sps_extension_data_flag
206
    u(1)
207
    rbsp_trailing_bits()
208
  */
209
210
211
  // --- derived values ---
212
213
  de265_error compute_derived_values(bool sanitize_values = false);
214
215
  int BitDepth_Y;
216
  int QpBdOffset_Y;
217
  int BitDepth_C;
218
  int QpBdOffset_C;
219
220
  int ChromaArrayType;
221
  int SubWidthC, SubHeightC;
222
  int WinUnitX, WinUnitY;
223
224
  int MaxPicOrderCntLsb;
225
226
  uint8_t Log2MinCbSizeY;
227
  uint8_t Log2CtbSizeY;
228
  uint8_t MinCbSizeY;
229
  uint8_t CtbSizeY;
230
  uint16_t PicWidthInMinCbsY;
231
  uint16_t PicWidthInCtbsY;
232
  uint16_t PicHeightInMinCbsY;
233
  uint16_t PicHeightInCtbsY;
234
  uint32_t PicSizeInMinCbsY;
235
  uint32_t PicSizeInCtbsY;
236
  uint32_t PicSizeInSamplesY;
237
238
  int CtbWidthC, CtbHeightC;
239
240
  int PicWidthInTbsY; // not in standard
241
  int PicHeightInTbsY; // not in standard
242
  int PicSizeInTbsY; // not in standard
243
244
  int Log2MinTrafoSize;
245
  int Log2MaxTrafoSize;
246
247
  int Log2MinPUSize;
248
  int PicWidthInMinPUs;  // might be rounded up
249
  int PicHeightInMinPUs; // might be rounded up
250
251
  int Log2MinIpcmCbSizeY;
252
  int Log2MaxIpcmCbSizeY;
253
254
  int SpsMaxLatencyPictures[7] = {}; // [temporal layer]
255
256
  uint8_t WpOffsetBdShiftY;
257
  uint8_t WpOffsetBdShiftC;
258
  uint16_t WpOffsetHalfRangeY;
259
  uint16_t WpOffsetHalfRangeC;
260
261
262
0
  int getPUIndexRS(int pixelX,int pixelY) const {
263
0
    return (pixelX>>Log2MinPUSize) + (pixelY>>Log2MinPUSize)*PicWidthInMinPUs;
264
0
  }
265
266
6.83M
  int get_bit_depth(int cIdx) const {
267
6.83M
    if (cIdx==0) return BitDepth_Y;
268
4.21M
    else         return BitDepth_C;
269
6.83M
  }
270
271
44.5k
  int get_chroma_shift_W(int cIdx) const { return cIdx ? SubWidthC -1 : 0; }
272
44.5k
  int get_chroma_shift_H(int cIdx) const { return cIdx ? SubHeightC-1 : 0; }
273
};
274
275
de265_error read_scaling_list(bitreader*, const seq_parameter_set*, scaling_list_data*, bool inPPS);
276
de265_error write_scaling_list(CABAC_encoder& out, const seq_parameter_set* sps,
277
                               scaling_list_data* sclist, bool inPPS);
278
void set_default_scaling_lists(scaling_list_data*);
279
280
#endif