Coverage Report

Created: 2026-09-02 06:41

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