Coverage Report

Created: 2026-10-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libde265/libde265/sps.cc
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
#include "sps.h"
22
#include "util.h"
23
#include "scan.h"
24
#include "decctx.h"
25
26
#include <assert.h>
27
#include <stdlib.h>
28
#include <string.h>
29
30
60
#define D 0
31
32
#define READ_VLC(variable, vlctype)   \
33
25.9k
  if ((vlc = br->get_ ## vlctype()) == UVLC_ERROR) {   \
34
2
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);  \
35
2
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; \
36
2
  } \
37
25.9k
  variable = vlc;
38
39
40
static int SubWidthC_tab[]  = { 1,2,2,1 };
41
static int SubHeightC_tab[] = { 1,2,1,1 };
42
43
44
// TODO if (!check_high(ctx, vlc, 15)) return false;
45
// TODO if (!check_ulvc(ctx, vlc)) return false;
46
47
48
// TODO: should be in some header-file of refpic.c
49
extern bool read_short_term_ref_pic_set(error_queue* errqueue,
50
                                        const seq_parameter_set* sps,
51
                                        bitreader* br,
52
                                        ref_pic_set* out_set,
53
                                        uint32_t idxRps,  // index of the set to be read
54
                                        const std::vector<ref_pic_set>& sets,
55
                                        bool sliceRefPicSet);
56
57
extern bool write_short_term_ref_pic_set(error_queue* errqueue,
58
                                         const seq_parameter_set* sps,
59
                                         CABAC_encoder& out,
60
                                         const ref_pic_set* in_set, // which set to write
61
                                         uint32_t idxRps,  // index of the set to be read
62
                                         const std::vector<ref_pic_set>& sets, // previously read sets
63
                                         bool sliceRefPicSet); // is this in the slice header?
64
65
66
13.3k
sps_range_extension::sps_range_extension() = default;
67
68
69
13.3k
seq_parameter_set::seq_parameter_set() = default;
70
71
72
seq_parameter_set::~seq_parameter_set()
73
13.3k
{
74
  //free(ref_pic_sets);
75
13.3k
}
76
77
78
void seq_parameter_set::set_defaults(enum PresetSet)
79
0
{
80
0
  video_parameter_set_id = 0;
81
0
  sps_max_sub_layers = 1;
82
0
  sps_temporal_id_nesting_flag = 1;
83
84
0
  profile_tier_level_.general.set_defaults(Profile_Main, 6,2); // TODO
85
86
0
  seq_parameter_set_id = 0;
87
0
  chroma_format_idc = 1;
88
0
  ChromaArrayType = chroma_format_idc;
89
90
0
  separate_colour_plane_flag = 0;
91
0
  pic_width_in_luma_samples = 0;
92
0
  pic_height_in_luma_samples = 0;
93
0
  conformance_window_flag = 0;
94
95
0
  conf_win_left_offset   = 0;
96
0
  conf_win_right_offset  = 0;
97
0
  conf_win_top_offset    = 0;
98
0
  conf_win_bottom_offset = 0;
99
100
0
  bit_depth_luma  =8;
101
0
  bit_depth_chroma=8;
102
103
0
  log2_max_pic_order_cnt_lsb = 8;
104
0
  sps_sub_layer_ordering_info_present_flag = 0;
105
106
0
  sps_max_dec_pic_buffering[0] = 1;
107
0
  sps_max_num_reorder_pics[0]  = 0;
108
0
  sps_max_latency_increase_plus1[0] = 0;
109
110
0
  set_CB_log2size_range(4,4);
111
0
  set_TB_log2size_range(3,4);
112
0
  max_transform_hierarchy_depth_inter = 1;
113
0
  max_transform_hierarchy_depth_intra = 1;
114
115
0
  scaling_list_enable_flag = 0;
116
0
  sps_scaling_list_data_present_flag = 0;
117
118
  // TODO struct scaling_list_data scaling_list;
119
120
0
  amp_enabled_flag = 0;
121
0
  sample_adaptive_offset_enabled_flag = 0;
122
0
  pcm_enabled_flag = 0;
123
124
0
  pcm_sample_bit_depth_luma = 8;
125
0
  pcm_sample_bit_depth_chroma = 8;
126
  // TODO log2_min_pcm_luma_coding_block_size;
127
  // TODO log2_diff_max_min_pcm_luma_coding_block_size;
128
0
  pcm_loop_filter_disable_flag = 1;
129
130
  // num_short_term_ref_pic_sets = 0;
131
  // std::vector<ref_pic_set> ref_pic_sets; // [0 ; num_short_term_ref_pic_set (<=MAX_REF_PIC_SETS) )
132
0
  ref_pic_sets.clear();
133
134
0
  long_term_ref_pics_present_flag = 0;
135
136
0
  num_long_term_ref_pics_sps = 0;
137
138
  /* TODO
139
  int  lt_ref_pic_poc_lsb_sps[MAX_NUM_LT_REF_PICS_SPS];
140
  char used_by_curr_pic_lt_sps_flag[MAX_NUM_LT_REF_PICS_SPS];
141
  */
142
143
0
  sps_temporal_mvp_enabled_flag = 0;
144
0
  strong_intra_smoothing_enable_flag = 0;
145
0
  vui_parameters_present_flag = 0;
146
147
  /*
148
    if( vui_parameters_present_flag )
149
      vui_parameters()
150
  */
151
152
0
  sps_extension_present_flag = 0;
153
0
  sps_range_extension_flag = 0;
154
0
  sps_multilayer_extension_flag = 0;
155
0
  sps_extension_6bits = 0;
156
0
}
157
158
159
void seq_parameter_set::set_CB_log2size_range(int mini,int maxi)
160
0
{
161
0
  log2_min_luma_coding_block_size = mini;
162
0
  log2_diff_max_min_luma_coding_block_size = maxi-mini;
163
0
}
164
165
166
void seq_parameter_set::set_TB_log2size_range(int mini,int maxi)
167
0
{
168
0
  log2_min_transform_block_size = mini;
169
0
  log2_diff_max_min_transform_block_size = maxi-mini;
170
0
}
171
172
173
void seq_parameter_set::set_resolution(int w,int h)
174
0
{
175
0
  pic_width_in_luma_samples  = w;
176
0
  pic_height_in_luma_samples = h;
177
0
}
178
179
180
de265_error seq_parameter_set::read(error_queue* errqueue, bitreader* br)
181
13.3k
{
182
13.3k
  uint32_t vlc;
183
184
13.3k
  video_parameter_set_id = br->get_bits(4);
185
13.3k
  sps_max_sub_layers     = br->get_bits(3) +1;
186
13.3k
  if (sps_max_sub_layers>7) {
187
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
188
0
  }
189
190
13.3k
  sps_temporal_id_nesting_flag = br->get_bits(1);
191
192
13.3k
  profile_tier_level_.read(br, sps_max_sub_layers);
193
194
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc >= DE265_MAX_SPS_SETS) {
195
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
196
0
  }
197
13.3k
  seq_parameter_set_id = vlc;
198
199
200
  // --- decode chroma type ---
201
202
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
203
0
    errqueue->add_warning(DE265_WARNING_INVALID_CHROMA_FORMAT, false);
204
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
205
0
  }
206
13.3k
  chroma_format_idc = vlc;
207
208
13.3k
  if (chroma_format_idc == 3) {
209
3.02k
    separate_colour_plane_flag = br->get_bits(1);
210
3.02k
  }
211
10.3k
  else {
212
10.3k
    separate_colour_plane_flag = 0;
213
10.3k
  }
214
215
216
  // --- picture size ---
217
218
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc == 0 || vlc > MAX_PICTURE_WIDTH) {
219
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
220
0
  }
221
13.3k
  pic_width_in_luma_samples = vlc;
222
223
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc == 0 || vlc > MAX_PICTURE_HEIGHT) {
224
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
225
0
  }
226
13.3k
  pic_height_in_luma_samples = vlc;
227
228
13.3k
  conformance_window_flag = br->get_bits(1);
229
230
13.3k
  if (conformance_window_flag) {
231
    // SubWidthC * (left + right) has to be smaller than the picture width and
232
    // SubHeightC * (top + bottom) smaller than the picture height (7.4.3.2.1).
233
    // Compare against the remaining range instead of adding the offsets: a sum
234
    // of two coded values can wrap.
235
236
1.60k
    const bool monochrome_planes = (separate_colour_plane_flag || chroma_format_idc == 0);
237
1.60k
    const uint32_t unitX = monochrome_planes ? 1 : SubWidthC_tab [chroma_format_idc];
238
1.60k
    const uint32_t unitY = monochrome_planes ? 1 : SubHeightC_tab[chroma_format_idc];
239
240
1.60k
    const uint32_t max_sum_x = (pic_width_in_luma_samples  - 1) / unitX;
241
1.60k
    const uint32_t max_sum_y = (pic_height_in_luma_samples - 1) / unitY;
242
243
1.60k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > max_sum_x) {
244
0
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
245
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
246
0
    }
247
1.60k
    conf_win_left_offset = static_cast<uint16_t>(vlc);
248
249
1.60k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > max_sum_x - conf_win_left_offset) {
250
0
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
251
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
252
0
    }
253
1.60k
    conf_win_right_offset = static_cast<uint16_t>(vlc);
254
255
1.60k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > max_sum_y) {
256
0
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
257
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
258
0
    }
259
1.60k
    conf_win_top_offset = static_cast<uint16_t>(vlc);
260
261
1.60k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > max_sum_y - conf_win_top_offset) {
262
0
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
263
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
264
0
    }
265
1.60k
    conf_win_bottom_offset = static_cast<uint16_t>(vlc);
266
1.60k
  }
267
11.7k
  else {
268
11.7k
    conf_win_left_offset  = 0;
269
11.7k
    conf_win_right_offset = 0;
270
11.7k
    conf_win_top_offset   = 0;
271
11.7k
    conf_win_bottom_offset= 0;
272
11.7k
  }
273
274
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 8) {
275
0
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
276
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
277
0
  }
278
13.3k
  bit_depth_luma = vlc + 8;
279
280
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 8) {
281
0
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
282
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
283
0
  }
284
13.3k
  bit_depth_chroma = vlc + 8;
285
286
13.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 12) {
287
89
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
288
89
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
289
89
  }
290
13.2k
  log2_max_pic_order_cnt_lsb = vlc + 4;
291
13.2k
  MaxPicOrderCntLsb = 1<<(log2_max_pic_order_cnt_lsb);
292
293
294
  // --- sub_layer_ordering_info ---
295
296
13.2k
  sps_sub_layer_ordering_info_present_flag = br->get_bits(1);
297
298
13.2k
  int firstLayer = (sps_sub_layer_ordering_info_present_flag ?
299
12.6k
                    0 : sps_max_sub_layers-1 );
300
301
26.6k
  for (int i=firstLayer ; i <= sps_max_sub_layers-1; i++ ) {
302
303
    // sps_max_dec_pic_buffering[i]
304
305
13.4k
    vlc=br->get_uvlc();
306
13.4k
    if (vlc == UVLC_ERROR ||
307
13.4k
        vlc+1 > MAX_NUM_REF_PICS) {
308
23
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
309
23
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
310
23
    }
311
312
13.3k
    sps_max_dec_pic_buffering[i] = vlc+1;
313
314
    // sps_max_num_reorder_pics[i]
315
316
13.3k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > sps_max_dec_pic_buffering[i]) {
317
21
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
318
21
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
319
21
    }
320
13.3k
    sps_max_num_reorder_pics[i] = vlc;
321
322
323
    // sps_max_latency_increase[i]
324
325
13.3k
    READ_VLC(sps_max_latency_increase_plus1[i], uvlc);
326
327
13.3k
    sps_max_latency_increase_present[i] = (sps_max_latency_increase_plus1[i] != 0);
328
13.3k
    if (sps_max_latency_increase_present[i]) {
329
3.56k
      SpsMaxLatencyPictures[i] = (sps_max_num_reorder_pics[i] +
330
3.56k
                                  sps_max_latency_increase_plus1[i] - 1);
331
3.56k
    }
332
13.3k
  }
333
334
  // copy info to all layers if only specified once
335
336
13.1k
  if (sps_sub_layer_ordering_info_present_flag) {
337
12.6k
    int ref = sps_max_sub_layers-1;
338
12.6k
    assert(ref<7);
339
340
12.8k
    for (int i=0 ; i < sps_max_sub_layers-1; i++ ) {
341
167
      sps_max_dec_pic_buffering[i] = sps_max_dec_pic_buffering[ref];
342
167
      sps_max_num_reorder_pics[i]  = sps_max_num_reorder_pics[ref];
343
167
      sps_max_latency_increase_plus1[i]  = sps_max_latency_increase_plus1[ref];
344
167
      sps_max_latency_increase_present[i] = sps_max_latency_increase_present[ref];
345
167
      SpsMaxLatencyPictures[i] = SpsMaxLatencyPictures[ref];
346
167
    }
347
12.6k
  }
348
349
350
13.2k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
351
15
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
352
15
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
353
15
  }
354
13.1k
  log2_min_luma_coding_block_size = vlc + 3;
355
356
13.1k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(6 - log2_min_luma_coding_block_size)) {
357
31
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
358
31
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
359
31
  }
360
13.1k
  log2_diff_max_min_luma_coding_block_size = vlc;
361
362
13.1k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
363
138
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
364
138
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
365
138
  }
366
13.0k
  log2_min_transform_block_size = vlc + 2;
367
368
13.0k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(5 - log2_min_transform_block_size)) {
369
55
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
370
55
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
371
55
  }
372
12.9k
  log2_diff_max_min_transform_block_size = vlc;
373
374
  // log2_min_transform_block_size must not exceed the max coding block size (Log2CtbSizeY)
375
12.9k
  if (log2_min_transform_block_size > log2_min_luma_coding_block_size + log2_diff_max_min_luma_coding_block_size) {
376
100
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
377
100
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
378
100
  }
379
380
12.8k
  uint32_t maxDepth = log2_min_luma_coding_block_size + log2_diff_max_min_luma_coding_block_size
381
12.8k
                    - log2_min_transform_block_size;
382
383
12.8k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > maxDepth) {
384
16
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
385
16
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
386
16
  }
387
12.8k
  max_transform_hierarchy_depth_inter = vlc;
388
389
12.8k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > maxDepth) {
390
94
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
391
94
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
392
94
  }
393
12.7k
  max_transform_hierarchy_depth_intra = vlc;
394
395
12.7k
  scaling_list_enable_flag = br->get_bits(1);
396
397
12.7k
  if (scaling_list_enable_flag) {
398
399
558
    sps_scaling_list_data_present_flag = br->get_bits(1);
400
558
    if (sps_scaling_list_data_present_flag) {
401
402
100
      de265_error err;
403
100
      if ((err=read_scaling_list(br,this, &scaling_list, false)) != DE265_OK) {
404
86
        return err;
405
86
      }
406
100
    }
407
458
    else {
408
458
      set_default_scaling_lists(&scaling_list);
409
458
    }
410
558
  }
411
412
12.6k
  amp_enabled_flag = br->get_bits(1);
413
12.6k
  sample_adaptive_offset_enabled_flag = br->get_bits(1);
414
12.6k
  pcm_enabled_flag = br->get_bits(1);
415
12.6k
  if (pcm_enabled_flag) {
416
664
    pcm_sample_bit_depth_luma = br->get_bits(4)+1;
417
664
    pcm_sample_bit_depth_chroma = br->get_bits(4)+1;
418
664
    int log2PcmCbSizeMax = std::min(static_cast<int>(log2_min_luma_coding_block_size +
419
664
                                                      log2_diff_max_min_luma_coding_block_size), 5);
420
421
664
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc + 3 > static_cast<uint32_t>(log2PcmCbSizeMax)) {
422
49
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
423
49
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
424
49
    }
425
615
    log2_min_pcm_luma_coding_block_size = vlc + 3;
426
427
615
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(log2PcmCbSizeMax - log2_min_pcm_luma_coding_block_size)) {
428
15
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
429
15
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
430
15
    }
431
600
    log2_diff_max_min_pcm_luma_coding_block_size = vlc;
432
600
    pcm_loop_filter_disable_flag = br->get_bits(1);
433
434
600
    if (pcm_sample_bit_depth_luma > bit_depth_luma) {
435
12
      errqueue->add_warning(DE265_WARNING_PCM_BITDEPTH_TOO_LARGE, false);
436
12
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
437
12
    }
438
439
588
    if (pcm_sample_bit_depth_chroma > bit_depth_chroma) {
440
6
      errqueue->add_warning(DE265_WARNING_PCM_BITDEPTH_TOO_LARGE, false);
441
6
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
442
6
    }
443
588
  }
444
11.9k
  else {
445
11.9k
    pcm_sample_bit_depth_luma = 0;
446
11.9k
    pcm_sample_bit_depth_chroma = 0;
447
11.9k
    log2_min_pcm_luma_coding_block_size = 0;
448
11.9k
    log2_diff_max_min_pcm_luma_coding_block_size = 0;
449
11.9k
    pcm_loop_filter_disable_flag = 0;
450
11.9k
  }
451
452
12.5k
  int num_short_term_ref_pic_sets;
453
12.5k
  READ_VLC(num_short_term_ref_pic_sets, uvlc);
454
12.5k
  if (num_short_term_ref_pic_sets < 0 ||
455
12.5k
      num_short_term_ref_pic_sets > 64) {
456
27
    errqueue->add_warning(DE265_WARNING_NUMBER_OF_SHORT_TERM_REF_PIC_SETS_OUT_OF_RANGE, false);
457
27
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
458
27
  }
459
460
  // --- allocate reference pic set ---
461
462
  // we do not allocate the ref-pic-set for the slice header here, but in the slice header itself
463
464
12.5k
  ref_pic_sets.resize(num_short_term_ref_pic_sets);
465
466
25.5k
  for (int i = 0; i < num_short_term_ref_pic_sets; i++) {
467
468
13.0k
    bool success = read_short_term_ref_pic_set(errqueue,this,br,
469
13.0k
                                               &ref_pic_sets[i], i,
470
13.0k
                                               ref_pic_sets,
471
13.0k
                                               false);
472
473
13.0k
    if (!success) {
474
117
      return DE265_WARNING_SPS_HEADER_INVALID;
475
117
    }
476
477
    // dump_short_term_ref_pic_set(&(*ref_pic_sets)[i], fh);
478
13.0k
  }
479
480
12.4k
  long_term_ref_pics_present_flag = br->get_bits(1);
481
482
12.4k
  if (long_term_ref_pics_present_flag) {
483
484
1.28k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > MAX_NUM_LT_REF_PICS_SPS) {
485
6
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
486
6
    }
487
1.28k
    num_long_term_ref_pics_sps = vlc;
488
489
2.03k
    for (int i = 0; i < num_long_term_ref_pics_sps; i++ ) {
490
756
      lt_ref_pic_poc_lsb_sps[i] = br->get_bits(log2_max_pic_order_cnt_lsb);
491
756
      used_by_curr_pic_lt_sps_flag[i] = br->get_bits(1);
492
756
    }
493
1.28k
  }
494
11.1k
  else {
495
11.1k
    num_long_term_ref_pics_sps = 0; // NOTE: missing definition in standard !
496
11.1k
  }
497
498
12.4k
  sps_temporal_mvp_enabled_flag = br->get_bits(1);
499
12.4k
  strong_intra_smoothing_enable_flag = br->get_bits(1);
500
501
12.4k
  vui_parameters_present_flag = br->get_bits(1);
502
12.4k
  if (vui_parameters_present_flag) {
503
11.4k
    de265_error err = vui.read(errqueue, br, this);
504
11.4k
    if (err) {
505
156
      return err;
506
156
    }
507
11.4k
  }
508
509
510
12.2k
  sps_extension_present_flag = br->get_bits(1);
511
12.2k
  if (sps_extension_present_flag) {
512
1.15k
    sps_range_extension_flag = br->get_bits(1);
513
1.15k
    sps_multilayer_extension_flag = br->get_bits(1);
514
1.15k
    sps_extension_6bits = br->get_bits(6);
515
1.15k
  }
516
11.1k
  else {
517
11.1k
    sps_range_extension_flag = 0;
518
11.1k
  }
519
520
12.2k
  if (sps_range_extension_flag) {
521
770
    de265_error err = range_extension.read(errqueue, br);
522
770
    if (err != DE265_OK) { return err; }
523
770
  }
524
525
  /*
526
  sps_extension_flag = br->get_bits(1);
527
  if (sps_extension_flag) {
528
    assert(false);
529
  }
530
  */
531
532
533
12.2k
  de265_error err = compute_derived_values();
534
12.2k
  if (err != DE265_OK) { return err; }
535
536
12.2k
  sps_read = true;
537
538
12.2k
  return DE265_OK;
539
12.2k
}
540
541
542
de265_error seq_parameter_set::compute_derived_values(bool sanitize_values)
543
12.2k
{
544
  // --- compute derived values ---
545
546
12.2k
  SubWidthC  = SubWidthC_tab [chroma_format_idc];
547
12.2k
  SubHeightC = SubHeightC_tab[chroma_format_idc];
548
549
12.2k
  if (separate_colour_plane_flag) {
550
59
    ChromaArrayType = 0;
551
59
  }
552
12.2k
  else {
553
12.2k
    ChromaArrayType = chroma_format_idc;
554
12.2k
  }
555
556
12.2k
  if (ChromaArrayType==0) {
557
439
    WinUnitX = 1;
558
439
    WinUnitY = 1;
559
439
  }
560
11.8k
  else {
561
11.8k
    WinUnitX = SubWidthC_tab [chroma_format_idc];
562
11.8k
    WinUnitY = SubHeightC_tab[chroma_format_idc];
563
11.8k
  }
564
565
566
567
12.2k
  BitDepth_Y   = bit_depth_luma;
568
12.2k
  QpBdOffset_Y = 6*(bit_depth_luma-8);
569
12.2k
  BitDepth_C   = bit_depth_chroma;
570
12.2k
  QpBdOffset_C = 6*(bit_depth_chroma-8);
571
572
12.2k
  Log2MinCbSizeY = log2_min_luma_coding_block_size;
573
12.2k
  Log2CtbSizeY = Log2MinCbSizeY + log2_diff_max_min_luma_coding_block_size;
574
12.2k
  MinCbSizeY = 1 << Log2MinCbSizeY;
575
12.2k
  CtbSizeY = 1 << Log2CtbSizeY;
576
577
12.2k
  PicWidthInMinCbsY = ceil_div(pic_width_in_luma_samples, MinCbSizeY);
578
12.2k
  PicWidthInCtbsY   = ceil_div(pic_width_in_luma_samples, CtbSizeY);
579
12.2k
  PicHeightInMinCbsY = ceil_div(pic_height_in_luma_samples, MinCbSizeY);
580
12.2k
  PicHeightInCtbsY   = ceil_div(pic_height_in_luma_samples,CtbSizeY);
581
12.2k
  PicSizeInMinCbsY   = PicWidthInMinCbsY * PicHeightInMinCbsY;
582
12.2k
  PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY;
583
12.2k
  PicSizeInSamplesY = static_cast<uint32_t>(pic_width_in_luma_samples) * pic_height_in_luma_samples;
584
585
12.2k
  if (chroma_format_idc==0 || separate_colour_plane_flag) {
586
437
    CtbWidthC  = 0;
587
437
    CtbHeightC = 0;
588
437
  }
589
11.8k
  else {
590
11.8k
    CtbWidthC  = CtbSizeY / SubWidthC;
591
11.8k
    CtbHeightC = CtbSizeY / SubHeightC;
592
11.8k
  }
593
594
12.2k
  Log2MinTrafoSize = log2_min_transform_block_size;
595
12.2k
  Log2MaxTrafoSize = log2_min_transform_block_size + log2_diff_max_min_transform_block_size;
596
12.2k
  assert(Log2MaxTrafoSize >= 2);  // log2_min_transform_block_size >= 2 by spec; relied on by pps.cc
597
598
12.2k
  if (max_transform_hierarchy_depth_inter > Log2CtbSizeY - Log2MinTrafoSize) {
599
0
    if (sanitize_values) {
600
0
      max_transform_hierarchy_depth_inter = Log2CtbSizeY - Log2MinTrafoSize;
601
0
    } else {
602
0
      if (D) fprintf(stderr,"SPS error: transform hierarchy depth (inter) > CTB size - min TB size\n");
603
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
604
0
    }
605
0
  }
606
607
12.2k
  if (max_transform_hierarchy_depth_intra > Log2CtbSizeY - Log2MinTrafoSize) {
608
0
    if (sanitize_values) {
609
0
      max_transform_hierarchy_depth_intra = Log2CtbSizeY - Log2MinTrafoSize;
610
0
    } else {
611
0
      if (D) fprintf(stderr,"SPS error: transform hierarchy depth (intra) > CTB size - min TB size\n");
612
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
613
0
    }
614
0
  }
615
616
617
12.2k
  if (sanitize_values) {
618
0
    if (max_transform_hierarchy_depth_inter < Log2CtbSizeY - Log2MaxTrafoSize) {
619
0
      max_transform_hierarchy_depth_inter = Log2CtbSizeY - Log2MaxTrafoSize;
620
0
    }
621
622
0
    if (max_transform_hierarchy_depth_intra < Log2CtbSizeY - Log2MaxTrafoSize) {
623
0
      max_transform_hierarchy_depth_intra = Log2CtbSizeY - Log2MaxTrafoSize;
624
0
    }
625
0
  }
626
627
628
12.2k
  Log2MinPUSize = Log2MinCbSizeY-1;
629
12.2k
  PicWidthInMinPUs  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinPUSize);
630
12.2k
  PicHeightInMinPUs = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinPUSize);
631
632
12.2k
  Log2MinIpcmCbSizeY = log2_min_pcm_luma_coding_block_size;
633
12.2k
  Log2MaxIpcmCbSizeY = (log2_min_pcm_luma_coding_block_size +
634
12.2k
                        log2_diff_max_min_pcm_luma_coding_block_size);
635
636
  // the following are not in the standard
637
12.2k
  PicWidthInTbsY  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinTrafoSize);
638
12.2k
  PicHeightInTbsY = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinTrafoSize);
639
12.2k
  PicSizeInTbsY = PicWidthInTbsY * PicHeightInTbsY;
640
641
642
12.2k
  if (range_extension.high_precision_offsets_enabled_flag) {
643
224
    WpOffsetBdShiftY = 0;
644
224
    WpOffsetBdShiftC = 0;
645
224
    WpOffsetHalfRangeY = 1 << (BitDepth_Y - 1);
646
224
    WpOffsetHalfRangeC = 1 << (BitDepth_C - 1);
647
224
  }
648
12.0k
  else {
649
12.0k
    WpOffsetBdShiftY = ( BitDepth_Y - 8 );
650
12.0k
    WpOffsetBdShiftC = ( BitDepth_C - 8 );
651
12.0k
    WpOffsetHalfRangeY = 1 << 7;
652
12.0k
    WpOffsetHalfRangeC = 1 << 7;
653
12.0k
  }
654
655
656
  // --- check SPS sanity ---
657
658
12.2k
  if (pic_width_in_luma_samples  % MinCbSizeY != 0 ||
659
12.2k
      pic_height_in_luma_samples % MinCbSizeY != 0) {
660
    // TODO: warn that image size is coded wrong in bitstream (must be multiple of MinCbSizeY)
661
45
    if (D) fprintf(stderr,"SPS error: CB alignment\n");
662
45
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
663
45
  }
664
665
12.2k
  if (Log2MinTrafoSize > Log2MinCbSizeY) {
666
7
    if (D) fprintf(stderr,"SPS error: TB > CB\n");
667
7
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
668
7
  }
669
670
12.2k
  if (Log2MaxTrafoSize > std::min((int)Log2CtbSizeY,5)) {
671
8
    if (D) fprintf(stderr,"SPS error: TB_max > 32 or CTB\n");
672
8
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
673
8
  }
674
675
676
12.2k
  if (BitDepth_Y < 8 || BitDepth_Y > 16) {
677
0
    if (D) fprintf(stderr,"SPS error: bitdepth Y not in [8;16]\n");
678
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
679
0
  }
680
681
12.2k
  if (BitDepth_C < 8 || BitDepth_C > 16) {
682
0
    if (D) fprintf(stderr,"SPS error: bitdepth C not in [8;16]\n");
683
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
684
0
  }
685
686
687
12.1k
  sps_read = true;
688
689
12.1k
  return DE265_OK;
690
12.1k
}
691
692
693
694
void seq_parameter_set::dump(int fd) const
695
0
{
696
  //#if (_MSC_VER >= 1500)
697
  //#define LOG0(t) loginfo(LogHeaders, t)
698
  //#define LOG1(t,d) loginfo(LogHeaders, t,d)
699
  //#define LOG2(t,d1,d2) loginfo(LogHeaders, t,d1,d2)
700
  //#define LOG3(t,d1,d2,d3) loginfo(LogHeaders, t,d1,d2,d3)
701
702
0
  FILE* fh;
703
0
  if (fd==1) fh=stdout;
704
0
  else if (fd==2) fh=stderr;
705
0
  else { return; }
706
707
0
#define LOG0(t) log2fh(fh, t)
708
0
#define LOG1(t,d) log2fh(fh, t,d)
709
0
#define LOG2(t,d1,d2) log2fh(fh, t,d1,d2)
710
0
#define LOG3(t,d1,d2,d3) log2fh(fh, t,d1,d2,d3)
711
712
713
0
  LOG0("----------------- SPS -----------------\n");
714
0
  LOG1("video_parameter_set_id  : %d\n", video_parameter_set_id);
715
0
  LOG1("sps_max_sub_layers      : %d\n", sps_max_sub_layers);
716
0
  LOG1("sps_temporal_id_nesting_flag : %d\n", sps_temporal_id_nesting_flag);
717
718
0
  profile_tier_level_.dump(sps_max_sub_layers, fh);
719
720
0
  LOG1("seq_parameter_set_id    : %d\n", seq_parameter_set_id);
721
0
  LOG2("chroma_format_idc       : %d (%s)\n", chroma_format_idc,
722
0
       chroma_format_idc == 0 ? "monochrome" :
723
0
       chroma_format_idc == 1 ? "4:2:0" :
724
0
       chroma_format_idc == 2 ? "4:2:2" :
725
0
       chroma_format_idc == 3 ? "4:4:4" : "unknown");
726
727
0
  if (chroma_format_idc == 3) {
728
0
    LOG1("separate_colour_plane_flag : %d\n", separate_colour_plane_flag);
729
0
  }
730
731
0
  LOG1("pic_width_in_luma_samples  : %d\n", pic_width_in_luma_samples);
732
0
  LOG1("pic_height_in_luma_samples : %d\n", pic_height_in_luma_samples);
733
0
  LOG1("conformance_window_flag    : %d\n", conformance_window_flag);
734
735
0
  if (conformance_window_flag) {
736
0
    LOG1("conf_win_left_offset  : %d\n", conf_win_left_offset);
737
0
    LOG1("conf_win_right_offset : %d\n", conf_win_right_offset);
738
0
    LOG1("conf_win_top_offset   : %d\n", conf_win_top_offset);
739
0
    LOG1("conf_win_bottom_offset: %d\n", conf_win_bottom_offset);
740
0
  }
741
742
0
  LOG1("bit_depth_luma   : %d\n", bit_depth_luma);
743
0
  LOG1("bit_depth_chroma : %d\n", bit_depth_chroma);
744
745
0
  LOG1("log2_max_pic_order_cnt_lsb : %d\n", log2_max_pic_order_cnt_lsb);
746
0
  LOG1("sps_sub_layer_ordering_info_present_flag : %d\n", sps_sub_layer_ordering_info_present_flag);
747
748
0
  int firstLayer = (sps_sub_layer_ordering_info_present_flag ?
749
0
                    0 : sps_max_sub_layers-1 );
750
751
0
  for (int i=firstLayer ; i <= sps_max_sub_layers-1; i++ ) {
752
0
    LOG1("Layer %d\n",i);
753
0
    LOG1("  sps_max_dec_pic_buffering      : %d\n", sps_max_dec_pic_buffering[i]);
754
0
    LOG1("  sps_max_num_reorder_pics       : %d\n", sps_max_num_reorder_pics[i]);
755
0
    LOG1("  sps_max_latency_increase_plus1 : %d\n", sps_max_latency_increase_plus1[i]);
756
0
  }
757
758
0
  LOG1("log2_min_luma_coding_block_size : %d\n", log2_min_luma_coding_block_size);
759
0
  LOG1("log2_diff_max_min_luma_coding_block_size : %d\n",log2_diff_max_min_luma_coding_block_size);
760
0
  LOG1("log2_min_transform_block_size   : %d\n", log2_min_transform_block_size);
761
0
  LOG1("log2_diff_max_min_transform_block_size : %d\n", log2_diff_max_min_transform_block_size);
762
0
  LOG1("max_transform_hierarchy_depth_inter : %d\n", max_transform_hierarchy_depth_inter);
763
0
  LOG1("max_transform_hierarchy_depth_intra : %d\n", max_transform_hierarchy_depth_intra);
764
0
  LOG1("scaling_list_enable_flag : %d\n", scaling_list_enable_flag);
765
766
0
  if (scaling_list_enable_flag) {
767
768
0
    LOG1("sps_scaling_list_data_present_flag : %d\n", sps_scaling_list_data_present_flag);
769
0
    if (sps_scaling_list_data_present_flag) {
770
771
0
      LOG0("scaling list logging output not implemented");
772
      //assert(0);
773
      //scaling_list_data()
774
0
    }
775
0
  }
776
777
0
  LOG1("amp_enabled_flag                    : %d\n", amp_enabled_flag);
778
0
  LOG1("sample_adaptive_offset_enabled_flag : %d\n", sample_adaptive_offset_enabled_flag);
779
0
  LOG1("pcm_enabled_flag                    : %d\n", pcm_enabled_flag);
780
781
0
  if (pcm_enabled_flag) {
782
0
    LOG1("pcm_sample_bit_depth_luma     : %d\n", pcm_sample_bit_depth_luma);
783
0
    LOG1("pcm_sample_bit_depth_chroma   : %d\n", pcm_sample_bit_depth_chroma);
784
0
    LOG1("log2_min_pcm_luma_coding_block_size : %d\n", log2_min_pcm_luma_coding_block_size);
785
0
    LOG1("log2_diff_max_min_pcm_luma_coding_block_size : %d\n", log2_diff_max_min_pcm_luma_coding_block_size);
786
0
    LOG1("pcm_loop_filter_disable_flag  : %d\n", pcm_loop_filter_disable_flag);
787
0
  }
788
789
0
  LOG1("num_short_term_ref_pic_sets : %d\n", ref_pic_sets.size());
790
791
0
  for (size_t i = 0; i < ref_pic_sets.size(); i++) {
792
0
    LOG1("ref_pic_set[ %2d ]: ",i);
793
0
    dump_compact_short_term_ref_pic_set(&ref_pic_sets[i], 16, fh);
794
0
  }
795
796
0
  LOG1("long_term_ref_pics_present_flag : %d\n", long_term_ref_pics_present_flag);
797
798
0
  if (long_term_ref_pics_present_flag) {
799
800
0
    LOG1("num_long_term_ref_pics_sps : %d\n", num_long_term_ref_pics_sps);
801
802
0
    for (int i = 0; i < num_long_term_ref_pics_sps; i++ ) {
803
0
      LOG3("lt_ref_pic_poc_lsb_sps[%d] : %d   (used_by_curr_pic_lt_sps_flag=%d)\n",
804
0
           i, lt_ref_pic_poc_lsb_sps[i], used_by_curr_pic_lt_sps_flag[i]);
805
0
    }
806
0
  }
807
808
0
  LOG1("sps_temporal_mvp_enabled_flag      : %d\n", sps_temporal_mvp_enabled_flag);
809
0
  LOG1("strong_intra_smoothing_enable_flag : %d\n", strong_intra_smoothing_enable_flag);
810
0
  LOG1("vui_parameters_present_flag        : %d\n", vui_parameters_present_flag);
811
812
0
  LOG1("sps_extension_present_flag    : %d\n", sps_extension_present_flag);
813
0
  LOG1("sps_range_extension_flag      : %d\n", sps_range_extension_flag);
814
0
  LOG1("sps_multilayer_extension_flag : %d\n", sps_multilayer_extension_flag);
815
0
  LOG1("sps_extension_6bits           : %d\n", sps_extension_6bits);
816
817
0
  LOG1("CtbSizeY     : %d\n", CtbSizeY);
818
0
  LOG1("MinCbSizeY   : %d\n", MinCbSizeY);
819
0
  LOG1("MaxCbSizeY   : %d\n", 1<<(log2_min_luma_coding_block_size + log2_diff_max_min_luma_coding_block_size));
820
0
  LOG1("MinTBSizeY   : %d\n", 1<<log2_min_transform_block_size);
821
0
  LOG1("MaxTBSizeY   : %d\n", 1<<(log2_min_transform_block_size + log2_diff_max_min_transform_block_size));
822
823
0
  LOG1("PicWidthInCtbsY         : %d\n", PicWidthInCtbsY);
824
0
  LOG1("PicHeightInCtbsY        : %d\n", PicHeightInCtbsY);
825
0
  LOG1("SubWidthC               : %d\n", SubWidthC);
826
0
  LOG1("SubHeightC              : %d\n", SubHeightC);
827
828
0
  if (sps_range_extension_flag) {
829
0
    range_extension.dump(fd);
830
0
  }
831
832
0
  if (vui_parameters_present_flag) {
833
0
    vui.dump(fd);
834
0
  }
835
0
#undef LOG0
836
0
#undef LOG1
837
0
#undef LOG2
838
0
#undef LOG3
839
  //#endif
840
0
}
841
842
843
static uint8_t default_ScalingList_4x4[16] = {
844
  16,16,16,16,16,16,16,16,
845
  16,16,16,16,16,16,16,16
846
};
847
848
static uint8_t default_ScalingList_8x8_intra[64] = {
849
  16,16,16,16,16,16,16,16,
850
  16,16,17,16,17,16,17,18,
851
  17,18,18,17,18,21,19,20,
852
  21,20,19,21,24,22,22,24,
853
  24,22,22,24,25,25,27,30,
854
  27,25,25,29,31,35,35,31,
855
  29,36,41,44,41,36,47,54,
856
  54,47,65,70,65,88,88,115
857
};
858
859
static uint8_t default_ScalingList_8x8_inter[64] = {
860
  16,16,16,16,16,16,16,16,
861
  16,16,17,17,17,17,17,18,
862
  18,18,18,18,18,20,20,20,
863
  20,20,20,20,24,24,24,24,
864
  24,24,24,24,25,25,25,25,
865
  25,25,25,28,28,28,28,28,
866
  28,33,33,33,33,33,41,41,
867
  41,41,54,54,54,71,71,91
868
};
869
870
871
void fill_scaling_factor(uint8_t* scalingFactors, const uint8_t* sclist, int sizeId)
872
9.99k
{
873
9.99k
  const position* scan;
874
9.99k
  int width;
875
9.99k
  int subWidth;
876
877
9.99k
  switch (sizeId) {
878
3.16k
  case 0:
879
3.16k
    width=4;
880
3.16k
    subWidth=1;
881
3.16k
    scan = get_scan_order(2, 0 /* diag */);
882
883
53.7k
    for (int i=0;i<4*4;i++) {
884
50.6k
      scalingFactors[scan[i].x + width*scan[i].y] = sclist[i];
885
50.6k
    }
886
3.16k
    break;
887
888
2.97k
  case 1:
889
2.97k
    width=8;
890
2.97k
    subWidth=1;
891
2.97k
    scan = get_scan_order(3, 0 /* diag */);
892
893
193k
    for (int i=0;i<8*8;i++) {
894
190k
      scalingFactors[scan[i].x + width*scan[i].y] = sclist[i];
895
190k
    }
896
2.97k
    break;
897
898
2.90k
  case 2:
899
2.90k
    width=8;
900
2.90k
    subWidth=2;
901
2.90k
    scan = get_scan_order(3, 0 /* diag */);
902
903
188k
    for (int i=0;i<8*8;i++) {
904
556k
      for (int dy=0;dy<2;dy++)
905
1.11M
        for (int dx=0;dx<2;dx++)
906
741k
          {
907
741k
            int x = 2*scan[i].x+dx;
908
741k
            int y = 2*scan[i].y+dy;
909
741k
            scalingFactors[x+width*subWidth*y] = sclist[i];
910
741k
          }
911
185k
    }
912
2.90k
    break;
913
914
950
  case 3:
915
950
    width=8;
916
950
    subWidth=4;
917
950
    scan = get_scan_order(3, 0 /* diag */);
918
919
61.7k
    for (int i=0;i<8*8;i++) {
920
303k
      for (int dy=0;dy<4;dy++)
921
1.21M
        for (int dx=0;dx<4;dx++)
922
971k
          {
923
971k
            int x = 4*scan[i].x+dx;
924
971k
            int y = 4*scan[i].y+dy;
925
971k
            scalingFactors[x+width*subWidth*y] = sclist[i];
926
971k
          }
927
60.8k
    }
928
950
    break;
929
930
0
  default:
931
0
    assert(0);
932
0
    break;
933
9.99k
  }
934
935
936
  // --- dump matrix ---
937
938
#if 0
939
  for (int y=0;y<width;y++) {
940
    for (int x=0;x<width;x++)
941
      printf("%d,",scalingFactors[x*subWidth + width*subWidth*subWidth*y]);
942
943
    printf("\n");
944
  }
945
#endif
946
9.99k
}
947
948
949
de265_error read_scaling_list(bitreader* br, const seq_parameter_set* sps,
950
                              scaling_list_data* sclist, bool inPPS)
951
128
{
952
128
  int dc_coeff[4][6];
953
954
248
  for (int sizeId=0;sizeId<4;sizeId++) {
955
    //int n = ((sizeId==3) ? 2 : 6);
956
234
    uint8_t scaling_list[6][32*32];
957
958
    // Note: we use a different matrixId for the second matrix of size 3 (we use '3' instead of '1').
959
1.06k
    for (int matrixId=0 ; matrixId<6 ; matrixId += (sizeId==3 ? 3 : 1)) {
960
949
      uint8_t* curr_scaling_list = scaling_list[matrixId];
961
949
      int scaling_list_dc_coef;
962
963
964
      //printf("----- matrix %d\n",matrixId);
965
966
949
      char scaling_list_pred_mode_flag = br->get_bits(1);
967
949
      if (!scaling_list_pred_mode_flag) {
968
426
        uint32_t scaling_list_pred_matrix_id_delta = br->get_uvlc();
969
970
426
        if (scaling_list_pred_matrix_id_delta == UVLC_ERROR) {
971
15
          return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
972
15
        }
973
974
411
        if (sizeId == 3) {
975
          // adapt to our changed matrixId for size 3
976
29
          scaling_list_pred_matrix_id_delta *= 3;
977
29
        }
978
979
411
        if (scaling_list_pred_matrix_id_delta > (uint32_t)matrixId) {
980
26
          return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
981
26
        }
982
983
        //printf("scaling_list_pred_matrix_id_delta=%d\n", scaling_list_pred_matrix_id_delta);
984
985
385
        dc_coeff[sizeId][matrixId] = 16;
986
385
        scaling_list_dc_coef       = 16;
987
988
385
        if (scaling_list_pred_matrix_id_delta==0) {
989
274
          if (sizeId==0) {
990
109
            memcpy(curr_scaling_list, default_ScalingList_4x4, 16);
991
109
          }
992
165
          else {
993
165
            if (matrixId<3)
994
96
              { memcpy(curr_scaling_list, default_ScalingList_8x8_intra,64); }
995
69
            else
996
69
              { memcpy(curr_scaling_list, default_ScalingList_8x8_inter,64); }
997
165
          }
998
274
        }
999
111
        else {
1000
111
          if (sizeId==3) { assert(scaling_list_pred_matrix_id_delta==3); }
1001
1002
111
          int mID = matrixId - scaling_list_pred_matrix_id_delta;
1003
1004
111
          int len = (sizeId == 0 ? 16 : 64);
1005
111
          memcpy(curr_scaling_list, scaling_list[mID], len);
1006
1007
111
          scaling_list_dc_coef       = dc_coeff[sizeId][mID];
1008
111
          dc_coeff[sizeId][matrixId] = dc_coeff[sizeId][mID];
1009
111
        }
1010
385
      }
1011
523
      else {
1012
523
        int nextCoef=8;
1013
523
        int coefNum = (sizeId==0 ? 16 : 64);
1014
523
        if (sizeId>1) {
1015
90
          scaling_list_dc_coef = br->get_svlc();
1016
90
          if (scaling_list_dc_coef < -7 ||
1017
89
              scaling_list_dc_coef > 247) {
1018
1
            return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1019
1
          }
1020
1021
89
          scaling_list_dc_coef += 8;
1022
89
          nextCoef=scaling_list_dc_coef;
1023
89
          dc_coeff[sizeId][matrixId] = scaling_list_dc_coef;
1024
89
        }
1025
433
        else {
1026
433
          scaling_list_dc_coef = 16;
1027
433
        }
1028
        //printf("DC = %d\n",scaling_list_dc_coef);
1029
1030
17.7k
        for (int i=0;i<coefNum;i++) {
1031
17.3k
          int scaling_list_delta_coef = br->get_svlc();
1032
17.3k
          if (scaling_list_delta_coef < -128 ||
1033
17.2k
              scaling_list_delta_coef >  127) {
1034
72
            return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1035
72
          }
1036
1037
17.2k
          nextCoef = (nextCoef + scaling_list_delta_coef + 256) % 256;
1038
17.2k
          curr_scaling_list[i] = nextCoef;
1039
          //printf("curr %d = %d\n",i,nextCoef);
1040
17.2k
        }
1041
522
      }
1042
1043
1044
      // --- generate ScalingFactor arrays ---
1045
1046
835
      switch (sizeId) {
1047
418
      case 0:
1048
418
        fill_scaling_factor(&sclist->ScalingFactor_Size0[matrixId][0][0], curr_scaling_list, 0);
1049
418
        break;
1050
1051
227
      case 1:
1052
227
        fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId][0][0], curr_scaling_list, 1);
1053
227
        break;
1054
1055
155
      case 2:
1056
155
        fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId][0][0], curr_scaling_list, 2);
1057
155
        sclist->ScalingFactor_Size2[matrixId][0][0] = scaling_list_dc_coef;
1058
        //printf("DC coeff: %d\n", scaling_list_dc_coef);
1059
155
        break;
1060
1061
35
      case 3:
1062
35
        fill_scaling_factor(&sclist->ScalingFactor_Size3[matrixId][0][0], curr_scaling_list, 3);
1063
35
        sclist->ScalingFactor_Size3[matrixId][0][0] = scaling_list_dc_coef;
1064
        //printf("DC coeff: %d\n", scaling_list_dc_coef);
1065
35
        break;
1066
835
      }
1067
835
    }
1068
234
  }
1069
1070
1071
  // --- fill 32x32 matrices for chroma
1072
1073
14
  const position* scan = get_scan_order(3, 0 /* diag */);
1074
  
1075
98
  for (int matrixId=0;matrixId<6;matrixId++)
1076
84
    if (matrixId!=0 && matrixId!=3) {
1077
3.64k
      for (int i=0;i<64;i++) {
1078
3.58k
  int x = scan[i].x;
1079
3.58k
  int y = scan[i].y;
1080
3.58k
  int v = sclist->ScalingFactor_Size1[matrixId][y][x];
1081
1082
17.9k
  for (int dy=0;dy<4;dy++)
1083
71.6k
    for (int dx=0;dx<4;dx++) {
1084
57.3k
      sclist->ScalingFactor_Size3[matrixId][4*y+dy][4*x+dx] = v;
1085
57.3k
    }
1086
3.58k
      }
1087
1088
56
      sclist->ScalingFactor_Size3[matrixId][0][0] = sclist->ScalingFactor_Size1[matrixId][0][0];
1089
56
    }
1090
  
1091
14
  return DE265_OK;
1092
128
}
1093
1094
1095
de265_error write_scaling_list(CABAC_encoder& out, const seq_parameter_set* sps,
1096
                              scaling_list_data* sclist, bool inPPS)
1097
0
{
1098
0
  assert(false);
1099
  // TODO
1100
1101
0
  return DE265_OK;
1102
0
}
1103
1104
1105
void set_default_scaling_lists(scaling_list_data* sclist)
1106
458
{
1107
  // 4x4
1108
1109
3.20k
  for (int matrixId=0;matrixId<6;matrixId++) {
1110
2.74k
    fill_scaling_factor(&sclist->ScalingFactor_Size0[matrixId][0][0],
1111
2.74k
                        default_ScalingList_4x4, 0);
1112
2.74k
  }
1113
1114
  // 8x8
1115
1116
1.83k
  for (int matrixId=0;matrixId<3;matrixId++) {
1117
1.37k
    fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId+0][0][0],
1118
1.37k
                        default_ScalingList_8x8_intra, 1);
1119
1.37k
    fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId+3][0][0],
1120
1.37k
                        default_ScalingList_8x8_inter, 1);
1121
1.37k
  }
1122
1123
  // 16x16
1124
1125
1.83k
  for (int matrixId=0;matrixId<3;matrixId++) {
1126
1.37k
    fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId+0][0][0],
1127
1.37k
                        default_ScalingList_8x8_intra, 2);
1128
1.37k
    fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId+3][0][0],
1129
1.37k
                        default_ScalingList_8x8_inter, 2);
1130
1.37k
  }
1131
1132
  // 32x32
1133
1134
458
  fill_scaling_factor(&sclist->ScalingFactor_Size3[0][0][0],
1135
458
                      default_ScalingList_8x8_intra, 3);
1136
458
  fill_scaling_factor(&sclist->ScalingFactor_Size3[1][0][0],
1137
458
                      default_ScalingList_8x8_inter, 3);
1138
458
}
1139
1140
1141
de265_error seq_parameter_set::write(error_queue* errqueue, CABAC_encoder& out)
1142
0
{
1143
0
  out.write_bits(video_parameter_set_id, 4);
1144
0
  if (sps_max_sub_layers>7) {
1145
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1146
0
  }
1147
0
  out.write_bits(sps_max_sub_layers-1, 3);
1148
1149
0
  out.write_bit(sps_temporal_id_nesting_flag);
1150
1151
0
  profile_tier_level_.write(out, sps_max_sub_layers);
1152
1153
0
  out.write_uvlc(seq_parameter_set_id);
1154
1155
1156
  // --- encode chroma type ---
1157
1158
0
  out.write_uvlc(chroma_format_idc);
1159
1160
0
  if (chroma_format_idc>3) {
1161
0
    errqueue->add_warning(DE265_WARNING_INVALID_CHROMA_FORMAT, false);
1162
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1163
0
  }
1164
1165
0
  if (chroma_format_idc == 3) {
1166
0
    out.write_bit(separate_colour_plane_flag);
1167
0
  }
1168
1169
1170
  // --- picture size ---
1171
1172
0
  out.write_uvlc(pic_width_in_luma_samples);
1173
0
  out.write_uvlc(pic_height_in_luma_samples);
1174
1175
0
  out.write_bit(conformance_window_flag);
1176
1177
0
  if (conformance_window_flag) {
1178
0
    out.write_uvlc(conf_win_left_offset);
1179
0
    out.write_uvlc(conf_win_right_offset);
1180
0
    out.write_uvlc(conf_win_top_offset);
1181
0
    out.write_uvlc(conf_win_bottom_offset);
1182
0
  }
1183
1184
1185
0
  out.write_uvlc(bit_depth_luma-8);
1186
0
  out.write_uvlc(bit_depth_chroma-8);
1187
1188
0
  out.write_uvlc(log2_max_pic_order_cnt_lsb-4);
1189
1190
1191
  // --- sub_layer_ordering_info ---
1192
1193
0
  out.write_bit(sps_sub_layer_ordering_info_present_flag);
1194
1195
0
  int firstLayer = (sps_sub_layer_ordering_info_present_flag ?
1196
0
                    0 : sps_max_sub_layers-1 );
1197
1198
0
  for (int i=firstLayer ; i <= sps_max_sub_layers-1; i++ ) {
1199
1200
    // sps_max_dec_pic_buffering[i]
1201
1202
0
    if (sps_max_dec_pic_buffering[i] > MAX_NUM_REF_PICS) {
1203
0
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
1204
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1205
0
    }
1206
1207
0
    out.write_uvlc(sps_max_dec_pic_buffering[i]-1);
1208
1209
    // sps_max_num_reorder_pics[i]
1210
1211
0
    out.write_uvlc(sps_max_num_reorder_pics[i]);
1212
1213
1214
    // sps_max_latency_increase[i]
1215
1216
0
    out.write_uvlc(sps_max_latency_increase_plus1[i]);
1217
0
  }
1218
1219
1220
0
  out.write_uvlc(log2_min_luma_coding_block_size-3);
1221
0
  out.write_uvlc(log2_diff_max_min_luma_coding_block_size);
1222
0
  out.write_uvlc(log2_min_transform_block_size-2);
1223
0
  out.write_uvlc(log2_diff_max_min_transform_block_size);
1224
0
  out.write_uvlc(max_transform_hierarchy_depth_inter);
1225
0
  out.write_uvlc(max_transform_hierarchy_depth_intra);
1226
0
  out.write_bit(scaling_list_enable_flag);
1227
1228
0
  if (scaling_list_enable_flag) {
1229
1230
0
    out.write_bit(sps_scaling_list_data_present_flag);
1231
0
    if (sps_scaling_list_data_present_flag) {
1232
1233
0
      de265_error err;
1234
0
      if ((err=write_scaling_list(out,this, &scaling_list, false)) != DE265_OK) {
1235
0
        return err;
1236
0
      }
1237
0
    }
1238
0
  }
1239
1240
0
  out.write_bit(amp_enabled_flag);
1241
0
  out.write_bit(sample_adaptive_offset_enabled_flag);
1242
0
  out.write_bit(pcm_enabled_flag);
1243
0
  if (pcm_enabled_flag) {
1244
0
    out.write_bits(pcm_sample_bit_depth_luma  -1,4);
1245
0
    out.write_bits(pcm_sample_bit_depth_chroma-1,4);
1246
0
    out.write_uvlc(log2_min_pcm_luma_coding_block_size-3);
1247
0
    out.write_uvlc(log2_diff_max_min_pcm_luma_coding_block_size);
1248
0
    out.write_bit(pcm_loop_filter_disable_flag);
1249
0
  }
1250
1251
0
  int num_short_term_ref_pic_sets = ref_pic_sets.size();
1252
0
  if (num_short_term_ref_pic_sets < 0 ||
1253
0
      num_short_term_ref_pic_sets > 64) {
1254
0
    errqueue->add_warning(DE265_WARNING_NUMBER_OF_SHORT_TERM_REF_PIC_SETS_OUT_OF_RANGE, false);
1255
0
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1256
0
  }
1257
0
  out.write_uvlc(num_short_term_ref_pic_sets);
1258
1259
  // --- allocate reference pic set ---
1260
1261
  // we do not allocate the ref-pic-set for the slice header here, but in the slice header itself
1262
1263
0
  for (int i = 0; i < num_short_term_ref_pic_sets; i++) {
1264
1265
0
    bool success = write_short_term_ref_pic_set(errqueue,this,out,
1266
0
                                                &ref_pic_sets[i], i,
1267
0
                                                ref_pic_sets,
1268
0
                                                false);
1269
1270
0
    if (!success) {
1271
0
      return DE265_WARNING_SPS_HEADER_INVALID;
1272
0
    }
1273
1274
    // dump_short_term_ref_pic_set(&(*ref_pic_sets)[i], fh);
1275
0
  }
1276
1277
0
  out.write_bit(long_term_ref_pics_present_flag);
1278
1279
0
  if (long_term_ref_pics_present_flag) {
1280
1281
0
    if (num_long_term_ref_pics_sps > MAX_NUM_LT_REF_PICS_SPS) {
1282
0
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1283
0
    }
1284
0
    out.write_uvlc(num_long_term_ref_pics_sps);
1285
1286
0
    for (int i = 0; i < num_long_term_ref_pics_sps; i++ ) {
1287
0
      out.write_bits(lt_ref_pic_poc_lsb_sps[i], log2_max_pic_order_cnt_lsb);
1288
0
      out.write_bit (used_by_curr_pic_lt_sps_flag[i]);
1289
0
    }
1290
0
  }
1291
1292
0
  out.write_bit(sps_temporal_mvp_enabled_flag);
1293
0
  out.write_bit(strong_intra_smoothing_enable_flag);
1294
0
  out.write_bit(vui_parameters_present_flag);
1295
1296
#if 0
1297
  if (vui_parameters_present_flag) {
1298
    assert(false);
1299
    /*
1300
      vui_parameters()
1301
        sps_extension_flag
1302
        u(1)
1303
        if( sps_extension_flag )
1304
          while( more_rbsp_data() )
1305
            sps_extension_data_flag
1306
              u(1)
1307
              rbsp_trailing_bits()
1308
    */
1309
  }
1310
#endif
1311
1312
0
  out.write_bit(sps_extension_present_flag);
1313
1314
#if 0
1315
  if (sps_extension_flag) {
1316
    assert(false);
1317
  }
1318
  br->check_rbsp_trailing_bits();
1319
#endif
1320
1321
  // --- compute derived values ---
1322
1323
#if 0
1324
  BitDepth_Y   = bit_depth_luma;
1325
  QpBdOffset_Y = 6*(bit_depth_luma-8);
1326
  BitDepth_C   = bit_depth_chroma;
1327
  QpBdOffset_C = 6*(bit_depth_chroma-8);
1328
  Log2MinCbSizeY = log2_min_luma_coding_block_size;
1329
  Log2CtbSizeY = Log2MinCbSizeY + log2_diff_max_min_luma_coding_block_size;
1330
  MinCbSizeY = 1 << Log2MinCbSizeY;
1331
  CtbSizeY = 1 << Log2CtbSizeY;
1332
  PicWidthInMinCbsY = pic_width_in_luma_samples / MinCbSizeY;
1333
  PicWidthInCtbsY   = ceil_div(pic_width_in_luma_samples, CtbSizeY);
1334
  PicHeightInMinCbsY = pic_height_in_luma_samples / MinCbSizeY;
1335
  PicHeightInCtbsY   = ceil_div(pic_height_in_luma_samples,CtbSizeY);
1336
  PicSizeInMinCbsY   = PicWidthInMinCbsY * PicHeightInMinCbsY;
1337
  PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY;
1338
  PicSizeInSamplesY = static_cast<uint32_t>(pic_width_in_luma_samples) * pic_height_in_luma_samples;
1339
  if (chroma_format_idc==0 || separate_colour_plane_flag) {
1340
    CtbWidthC  = 0;
1341
    CtbHeightC = 0;
1342
  }
1343
  else {
1344
    CtbWidthC  = CtbSizeY / SubWidthC;
1345
    CtbHeightC = CtbSizeY / SubHeightC;
1346
  }
1347
  Log2MinTrafoSize = log2_min_transform_block_size;
1348
  Log2MaxTrafoSize = log2_min_transform_block_size + log2_diff_max_min_transform_block_size;
1349
  assert(Log2MaxTrafoSize >= 2);  // log2_min_transform_block_size >= 2 by spec; relied on by pps.cc
1350
  Log2MinPUSize = Log2MinCbSizeY-1;
1351
  PicWidthInMinPUs  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinPUSize);
1352
  PicHeightInMinPUs = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinPUSize);
1353
  Log2MinIpcmCbSizeY = log2_min_pcm_luma_coding_block_size;
1354
  Log2MaxIpcmCbSizeY = (log2_min_pcm_luma_coding_block_size +
1355
                        log2_diff_max_min_pcm_luma_coding_block_size);
1356
  // the following are not in the standard
1357
  PicWidthInTbsY  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinTrafoSize);
1358
  PicHeightInTbsY = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinTrafoSize);
1359
  PicSizeInTbsY = PicWidthInTbsY * PicHeightInTbsY;
1360
  sps_read = true;
1361
#endif
1362
1363
0
  return DE265_OK;
1364
0
}
1365
1366
1367
de265_error sps_range_extension::read(error_queue* errqueue, bitreader* br)
1368
770
{
1369
770
  transform_skip_rotation_enabled_flag    = br->get_bits(1);
1370
770
  transform_skip_context_enabled_flag     = br->get_bits(1);
1371
770
  implicit_rdpcm_enabled_flag             = br->get_bits(1);
1372
770
  explicit_rdpcm_enabled_flag             = br->get_bits(1);
1373
770
  extended_precision_processing_flag      = br->get_bits(1);
1374
770
  intra_smoothing_disabled_flag           = br->get_bits(1);
1375
770
  high_precision_offsets_enabled_flag     = br->get_bits(1);
1376
770
  persistent_rice_adaptation_enabled_flag = br->get_bits(1);
1377
770
  cabac_bypass_alignment_enabled_flag     = br->get_bits(1);
1378
1379
770
  return DE265_OK;
1380
770
}
1381
1382
1383
0
#define LOG0(t) log2fh(fh, t)
1384
0
#define LOG1(t,d) log2fh(fh, t,d)
1385
void sps_range_extension::dump(int fd) const
1386
0
{
1387
0
  FILE* fh;
1388
0
  if (fd==1) fh=stdout;
1389
0
  else if (fd==2) fh=stderr;
1390
0
  else { return; }
1391
1392
0
  LOG0("----------------- SPS-range-extension -----------------\n");
1393
0
  LOG1("transform_skip_rotation_enabled_flag    : %d\n", transform_skip_rotation_enabled_flag);
1394
0
  LOG1("transform_skip_context_enabled_flag     : %d\n", transform_skip_context_enabled_flag);
1395
0
  LOG1("implicit_rdpcm_enabled_flag             : %d\n", implicit_rdpcm_enabled_flag);
1396
0
  LOG1("explicit_rdpcm_enabled_flag             : %d\n", explicit_rdpcm_enabled_flag);
1397
0
  LOG1("extended_precision_processing_flag      : %d\n", extended_precision_processing_flag);
1398
0
  LOG1("intra_smoothing_disabled_flag           : %d\n", intra_smoothing_disabled_flag);
1399
0
  LOG1("high_precision_offsets_enabled_flag     : %d\n", high_precision_offsets_enabled_flag);
1400
0
  LOG1("persistent_rice_adaptation_enabled_flag : %d\n", persistent_rice_adaptation_enabled_flag);
1401
0
  LOG1("cabac_bypass_alignment_enabled_flag     : %d\n", cabac_bypass_alignment_enabled_flag);
1402
0
}
1403
#undef LOG1
1404
#undef LOG0