Coverage Report

Created: 2026-09-14 06:44

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
229
#define D 0
31
32
#define READ_VLC(variable, vlctype)   \
33
23.7k
  if ((vlc = br->get_ ## vlctype()) == UVLC_ERROR) {   \
34
39
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);  \
35
39
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; \
36
39
  } \
37
23.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
13.2k
sps_range_extension::sps_range_extension() = default;
67
68
69
13.2k
seq_parameter_set::seq_parameter_set() = default;
70
71
72
seq_parameter_set::~seq_parameter_set()
73
13.2k
{
74
  //free(ref_pic_sets);
75
13.2k
}
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.2k
{
182
13.2k
  uint32_t vlc;
183
184
13.2k
  video_parameter_set_id = br->get_bits(4);
185
13.2k
  sps_max_sub_layers     = br->get_bits(3) +1;
186
13.2k
  if (sps_max_sub_layers>7) {
187
16
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
188
16
  }
189
190
13.2k
  sps_temporal_id_nesting_flag = br->get_bits(1);
191
192
13.2k
  profile_tier_level_.read(br, sps_max_sub_layers);
193
194
13.2k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc >= DE265_MAX_SPS_SETS) {
195
52
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
196
52
  }
197
13.1k
  seq_parameter_set_id = vlc;
198
199
200
  // --- decode chroma type ---
201
202
13.1k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
203
47
    errqueue->add_warning(DE265_WARNING_INVALID_CHROMA_FORMAT, false);
204
47
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
205
47
  }
206
13.1k
  chroma_format_idc = vlc;
207
208
13.1k
  if (chroma_format_idc == 3) {
209
59
    separate_colour_plane_flag = br->get_bits(1);
210
59
  }
211
13.0k
  else {
212
13.0k
    separate_colour_plane_flag = 0;
213
13.0k
  }
214
215
216
  // --- picture size ---
217
218
13.1k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc == 0 || vlc > MAX_PICTURE_WIDTH) {
219
91
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
220
91
  }
221
13.0k
  pic_width_in_luma_samples = vlc;
222
223
13.0k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc == 0 || vlc > MAX_PICTURE_HEIGHT) {
224
49
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
225
49
  }
226
13.0k
  pic_height_in_luma_samples = vlc;
227
228
13.0k
  conformance_window_flag = br->get_bits(1);
229
230
13.0k
  if (conformance_window_flag) {
231
292
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc >= static_cast<uint32_t>(pic_width_in_luma_samples)) {
232
18
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
233
18
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
234
18
    }
235
274
    conf_win_left_offset = vlc;
236
237
274
    if ((vlc = br->get_uvlc()) == UVLC_ERROR ||
238
262
        vlc + conf_win_left_offset >= static_cast<uint32_t>(pic_width_in_luma_samples)) {
239
30
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
240
30
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
241
30
    }
242
244
    conf_win_right_offset = vlc;
243
244
244
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc >= static_cast<uint32_t>(pic_height_in_luma_samples)) {
245
31
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
246
31
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
247
31
    }
248
213
    conf_win_top_offset = vlc;
249
250
213
    if ((vlc = br->get_uvlc()) == UVLC_ERROR ||
251
203
        vlc + conf_win_top_offset >= static_cast<uint32_t>(pic_height_in_luma_samples)) {
252
27
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
253
27
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
254
27
    }
255
186
    conf_win_bottom_offset = vlc;
256
186
  }
257
12.7k
  else {
258
12.7k
    conf_win_left_offset  = 0;
259
12.7k
    conf_win_right_offset = 0;
260
12.7k
    conf_win_top_offset   = 0;
261
12.7k
    conf_win_bottom_offset= 0;
262
12.7k
  }
263
264
12.8k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 8) {
265
46
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
266
46
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
267
46
  }
268
12.8k
  bit_depth_luma = vlc + 8;
269
270
12.8k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 8) {
271
23
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
272
23
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
273
23
  }
274
12.8k
  bit_depth_chroma = vlc + 8;
275
276
12.8k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 12) {
277
51
    errqueue->add_warning(DE265_WARNING_SPS_HEADER_INVALID, false);
278
51
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
279
51
  }
280
12.7k
  log2_max_pic_order_cnt_lsb = vlc + 4;
281
12.7k
  MaxPicOrderCntLsb = 1<<(log2_max_pic_order_cnt_lsb);
282
283
284
  // --- sub_layer_ordering_info ---
285
286
12.7k
  sps_sub_layer_ordering_info_present_flag = br->get_bits(1);
287
288
12.7k
  int firstLayer = (sps_sub_layer_ordering_info_present_flag ?
289
6.80k
                    0 : sps_max_sub_layers-1 );
290
291
25.5k
  for (int i=firstLayer ; i <= sps_max_sub_layers-1; i++ ) {
292
293
    // sps_max_dec_pic_buffering[i]
294
295
12.9k
    vlc=br->get_uvlc();
296
12.9k
    if (vlc == UVLC_ERROR ||
297
12.9k
        vlc+1 > MAX_NUM_REF_PICS) {
298
71
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
299
71
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
300
71
    }
301
302
12.8k
    sps_max_dec_pic_buffering[i] = vlc+1;
303
304
    // sps_max_num_reorder_pics[i]
305
306
12.8k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > sps_max_dec_pic_buffering[i]) {
307
76
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
308
76
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
309
76
    }
310
12.7k
    sps_max_num_reorder_pics[i] = vlc;
311
312
313
    // sps_max_latency_increase[i]
314
315
12.7k
    READ_VLC(sps_max_latency_increase_plus1[i], uvlc);
316
317
12.7k
    sps_max_latency_increase_present[i] = (sps_max_latency_increase_plus1[i] != 0);
318
12.7k
    if (sps_max_latency_increase_present[i]) {
319
6.34k
      SpsMaxLatencyPictures[i] = (sps_max_num_reorder_pics[i] +
320
6.34k
                                  sps_max_latency_increase_plus1[i] - 1);
321
6.34k
    }
322
12.7k
  }
323
324
  // copy info to all layers if only specified once
325
326
12.6k
  if (sps_sub_layer_ordering_info_present_flag) {
327
6.71k
    int ref = sps_max_sub_layers-1;
328
6.71k
    assert(ref<7);
329
330
6.79k
    for (int i=0 ; i < sps_max_sub_layers-1; i++ ) {
331
75
      sps_max_dec_pic_buffering[i] = sps_max_dec_pic_buffering[ref];
332
75
      sps_max_num_reorder_pics[i]  = sps_max_num_reorder_pics[ref];
333
75
      sps_max_latency_increase_plus1[i]  = sps_max_latency_increase_plus1[ref];
334
75
      sps_max_latency_increase_present[i] = sps_max_latency_increase_present[ref];
335
75
      SpsMaxLatencyPictures[i] = SpsMaxLatencyPictures[ref];
336
75
    }
337
6.71k
  }
338
339
340
12.6k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
341
54
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
342
54
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
343
54
  }
344
12.5k
  log2_min_luma_coding_block_size = vlc + 3;
345
346
12.5k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(6 - log2_min_luma_coding_block_size)) {
347
95
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
348
95
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
349
95
  }
350
12.4k
  log2_diff_max_min_luma_coding_block_size = vlc;
351
352
12.4k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 3) {
353
45
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
354
45
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
355
45
  }
356
12.4k
  log2_min_transform_block_size = vlc + 2;
357
358
12.4k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(5 - log2_min_transform_block_size)) {
359
57
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
360
57
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
361
57
  }
362
12.3k
  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
12.3k
  if (log2_min_transform_block_size > log2_min_luma_coding_block_size + log2_diff_max_min_luma_coding_block_size) {
366
40
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
367
40
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
368
40
  }
369
370
12.3k
  uint32_t maxDepth = log2_min_luma_coding_block_size + log2_diff_max_min_luma_coding_block_size
371
12.3k
                    - log2_min_transform_block_size;
372
373
12.3k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > maxDepth) {
374
74
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
375
74
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
376
74
  }
377
12.2k
  max_transform_hierarchy_depth_inter = vlc;
378
379
12.2k
  if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > maxDepth) {
380
59
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
381
59
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
382
59
  }
383
12.1k
  max_transform_hierarchy_depth_intra = vlc;
384
385
12.1k
  scaling_list_enable_flag = br->get_bits(1);
386
387
12.1k
  if (scaling_list_enable_flag) {
388
389
6.67k
    sps_scaling_list_data_present_flag = br->get_bits(1);
390
6.67k
    if (sps_scaling_list_data_present_flag) {
391
392
4.12k
      de265_error err;
393
4.12k
      if ((err=read_scaling_list(br,this, &scaling_list, false)) != DE265_OK) {
394
698
        return err;
395
698
      }
396
4.12k
    }
397
2.55k
    else {
398
2.55k
      set_default_scaling_lists(&scaling_list);
399
2.55k
    }
400
6.67k
  }
401
402
11.4k
  amp_enabled_flag = br->get_bits(1);
403
11.4k
  sample_adaptive_offset_enabled_flag = br->get_bits(1);
404
11.4k
  pcm_enabled_flag = br->get_bits(1);
405
11.4k
  if (pcm_enabled_flag) {
406
2.65k
    pcm_sample_bit_depth_luma = br->get_bits(4)+1;
407
2.65k
    pcm_sample_bit_depth_chroma = br->get_bits(4)+1;
408
2.65k
    int log2PcmCbSizeMax = std::min(static_cast<int>(log2_min_luma_coding_block_size +
409
2.65k
                                                      log2_diff_max_min_luma_coding_block_size), 5);
410
411
2.65k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc + 3 > static_cast<uint32_t>(log2PcmCbSizeMax)) {
412
332
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
413
332
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
414
332
    }
415
2.31k
    log2_min_pcm_luma_coding_block_size = vlc + 3;
416
417
2.31k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > static_cast<uint32_t>(log2PcmCbSizeMax - log2_min_pcm_luma_coding_block_size)) {
418
62
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
419
62
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
420
62
    }
421
2.25k
    log2_diff_max_min_pcm_luma_coding_block_size = vlc;
422
2.25k
    pcm_loop_filter_disable_flag = br->get_bits(1);
423
424
2.25k
    if (pcm_sample_bit_depth_luma > bit_depth_luma) {
425
60
      errqueue->add_warning(DE265_WARNING_PCM_BITDEPTH_TOO_LARGE, false);
426
60
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
427
60
    }
428
429
2.19k
    if (pcm_sample_bit_depth_chroma > bit_depth_chroma) {
430
40
      errqueue->add_warning(DE265_WARNING_PCM_BITDEPTH_TOO_LARGE, false);
431
40
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
432
40
    }
433
2.19k
  }
434
8.83k
  else {
435
8.83k
    pcm_sample_bit_depth_luma = 0;
436
8.83k
    pcm_sample_bit_depth_chroma = 0;
437
8.83k
    log2_min_pcm_luma_coding_block_size = 0;
438
8.83k
    log2_diff_max_min_pcm_luma_coding_block_size = 0;
439
8.83k
    pcm_loop_filter_disable_flag = 0;
440
8.83k
  }
441
442
10.9k
  int num_short_term_ref_pic_sets;
443
10.9k
  READ_VLC(num_short_term_ref_pic_sets, uvlc);
444
10.9k
  if (num_short_term_ref_pic_sets < 0 ||
445
10.9k
      num_short_term_ref_pic_sets > 64) {
446
41
    errqueue->add_warning(DE265_WARNING_NUMBER_OF_SHORT_TERM_REF_PIC_SETS_OUT_OF_RANGE, false);
447
41
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
448
41
  }
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
10.9k
  ref_pic_sets.resize(num_short_term_ref_pic_sets);
455
456
15.2k
  for (int i = 0; i < num_short_term_ref_pic_sets; i++) {
457
458
4.67k
    bool success = read_short_term_ref_pic_set(errqueue,this,br,
459
4.67k
                                               &ref_pic_sets[i], i,
460
4.67k
                                               ref_pic_sets,
461
4.67k
                                               false);
462
463
4.67k
    if (!success) {
464
365
      return DE265_WARNING_SPS_HEADER_INVALID;
465
365
    }
466
467
    // dump_short_term_ref_pic_set(&(*ref_pic_sets)[i], fh);
468
4.67k
  }
469
470
10.5k
  long_term_ref_pics_present_flag = br->get_bits(1);
471
472
10.5k
  if (long_term_ref_pics_present_flag) {
473
474
3.71k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > MAX_NUM_LT_REF_PICS_SPS) {
475
59
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
476
59
    }
477
3.65k
    num_long_term_ref_pics_sps = vlc;
478
479
12.3k
    for (int i = 0; i < num_long_term_ref_pics_sps; i++ ) {
480
8.70k
      lt_ref_pic_poc_lsb_sps[i] = br->get_bits(log2_max_pic_order_cnt_lsb);
481
8.70k
      used_by_curr_pic_lt_sps_flag[i] = br->get_bits(1);
482
8.70k
    }
483
3.65k
  }
484
6.85k
  else {
485
6.85k
    num_long_term_ref_pics_sps = 0; // NOTE: missing definition in standard !
486
6.85k
  }
487
488
10.5k
  sps_temporal_mvp_enabled_flag = br->get_bits(1);
489
10.5k
  strong_intra_smoothing_enable_flag = br->get_bits(1);
490
491
10.5k
  vui_parameters_present_flag = br->get_bits(1);
492
10.5k
  if (vui_parameters_present_flag) {
493
4.69k
    de265_error err = vui.read(errqueue, br, this);
494
4.69k
    if (err) {
495
368
      return err;
496
368
    }
497
4.69k
  }
498
499
500
10.1k
  sps_extension_present_flag = br->get_bits(1);
501
10.1k
  if (sps_extension_present_flag) {
502
2.83k
    sps_range_extension_flag = br->get_bits(1);
503
2.83k
    sps_multilayer_extension_flag = br->get_bits(1);
504
2.83k
    sps_extension_6bits = br->get_bits(6);
505
2.83k
  }
506
7.30k
  else {
507
7.30k
    sps_range_extension_flag = 0;
508
7.30k
  }
509
510
10.1k
  if (sps_range_extension_flag) {
511
2.15k
    de265_error err = range_extension.read(errqueue, br);
512
2.15k
    if (err != DE265_OK) { return err; }
513
2.15k
  }
514
515
  /*
516
  sps_extension_flag = br->get_bits(1);
517
  if (sps_extension_flag) {
518
    assert(false);
519
  }
520
  */
521
522
523
10.1k
  de265_error err = compute_derived_values();
524
10.1k
  if (err != DE265_OK) { return err; }
525
526
9.91k
  sps_read = true;
527
528
9.91k
  return DE265_OK;
529
10.1k
}
530
531
532
de265_error seq_parameter_set::compute_derived_values(bool sanitize_values)
533
10.1k
{
534
  // --- compute derived values ---
535
536
10.1k
  SubWidthC  = SubWidthC_tab [chroma_format_idc];
537
10.1k
  SubHeightC = SubHeightC_tab[chroma_format_idc];
538
539
10.1k
  if (separate_colour_plane_flag) {
540
3
    ChromaArrayType = 0;
541
3
  }
542
10.1k
  else {
543
10.1k
    ChromaArrayType = chroma_format_idc;
544
10.1k
  }
545
546
10.1k
  if (ChromaArrayType==0) {
547
15
    WinUnitX = 1;
548
15
    WinUnitY = 1;
549
15
  }
550
10.1k
  else {
551
10.1k
    WinUnitX = SubWidthC_tab [chroma_format_idc];
552
10.1k
    WinUnitY = SubHeightC_tab[chroma_format_idc];
553
10.1k
  }
554
555
556
557
10.1k
  BitDepth_Y   = bit_depth_luma;
558
10.1k
  QpBdOffset_Y = 6*(bit_depth_luma-8);
559
10.1k
  BitDepth_C   = bit_depth_chroma;
560
10.1k
  QpBdOffset_C = 6*(bit_depth_chroma-8);
561
562
10.1k
  Log2MinCbSizeY = log2_min_luma_coding_block_size;
563
10.1k
  Log2CtbSizeY = Log2MinCbSizeY + log2_diff_max_min_luma_coding_block_size;
564
10.1k
  MinCbSizeY = 1 << Log2MinCbSizeY;
565
10.1k
  CtbSizeY = 1 << Log2CtbSizeY;
566
567
10.1k
  PicWidthInMinCbsY = ceil_div(pic_width_in_luma_samples, MinCbSizeY);
568
10.1k
  PicWidthInCtbsY   = ceil_div(pic_width_in_luma_samples, CtbSizeY);
569
10.1k
  PicHeightInMinCbsY = ceil_div(pic_height_in_luma_samples, MinCbSizeY);
570
10.1k
  PicHeightInCtbsY   = ceil_div(pic_height_in_luma_samples,CtbSizeY);
571
10.1k
  PicSizeInMinCbsY   = PicWidthInMinCbsY * PicHeightInMinCbsY;
572
10.1k
  PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY;
573
10.1k
  PicSizeInSamplesY = static_cast<uint32_t>(pic_width_in_luma_samples) * pic_height_in_luma_samples;
574
575
10.1k
  if (chroma_format_idc==0 || separate_colour_plane_flag) {
576
15
    CtbWidthC  = 0;
577
15
    CtbHeightC = 0;
578
15
  }
579
10.1k
  else {
580
10.1k
    CtbWidthC  = CtbSizeY / SubWidthC;
581
10.1k
    CtbHeightC = CtbSizeY / SubHeightC;
582
10.1k
  }
583
584
10.1k
  Log2MinTrafoSize = log2_min_transform_block_size;
585
10.1k
  Log2MaxTrafoSize = log2_min_transform_block_size + log2_diff_max_min_transform_block_size;
586
10.1k
  assert(Log2MaxTrafoSize >= 2);  // log2_min_transform_block_size >= 2 by spec; relied on by pps.cc
587
588
10.1k
  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
10.1k
  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
10.1k
  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
10.1k
  Log2MinPUSize = Log2MinCbSizeY-1;
619
10.1k
  PicWidthInMinPUs  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinPUSize);
620
10.1k
  PicHeightInMinPUs = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinPUSize);
621
622
10.1k
  Log2MinIpcmCbSizeY = log2_min_pcm_luma_coding_block_size;
623
10.1k
  Log2MaxIpcmCbSizeY = (log2_min_pcm_luma_coding_block_size +
624
10.1k
                        log2_diff_max_min_pcm_luma_coding_block_size);
625
626
  // the following are not in the standard
627
10.1k
  PicWidthInTbsY  = PicWidthInCtbsY  << (Log2CtbSizeY - Log2MinTrafoSize);
628
10.1k
  PicHeightInTbsY = PicHeightInCtbsY << (Log2CtbSizeY - Log2MinTrafoSize);
629
10.1k
  PicSizeInTbsY = PicWidthInTbsY * PicHeightInTbsY;
630
631
632
10.1k
  if (range_extension.high_precision_offsets_enabled_flag) {
633
1.35k
    WpOffsetBdShiftY = 0;
634
1.35k
    WpOffsetBdShiftC = 0;
635
1.35k
    WpOffsetHalfRangeY = 1 << (BitDepth_Y - 1);
636
1.35k
    WpOffsetHalfRangeC = 1 << (BitDepth_C - 1);
637
1.35k
  }
638
8.79k
  else {
639
8.79k
    WpOffsetBdShiftY = ( BitDepth_Y - 8 );
640
8.79k
    WpOffsetBdShiftC = ( BitDepth_C - 8 );
641
8.79k
    WpOffsetHalfRangeY = 1 << 7;
642
8.79k
    WpOffsetHalfRangeC = 1 << 7;
643
8.79k
  }
644
645
646
  // --- check SPS sanity ---
647
648
10.1k
  if (pic_width_in_luma_samples  % MinCbSizeY != 0 ||
649
10.0k
      pic_height_in_luma_samples % MinCbSizeY != 0) {
650
    // TODO: warn that image size is coded wrong in bitstream (must be multiple of MinCbSizeY)
651
213
    if (D) fprintf(stderr,"SPS error: CB alignment\n");
652
213
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
653
213
  }
654
655
9.93k
  if (Log2MinTrafoSize > Log2MinCbSizeY) {
656
6
    if (D) fprintf(stderr,"SPS error: TB > CB\n");
657
6
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
658
6
  }
659
660
9.92k
  if (Log2MaxTrafoSize > std::min((int)Log2CtbSizeY,5)) {
661
10
    if (D) fprintf(stderr,"SPS error: TB_max > 32 or CTB\n");
662
10
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
663
10
  }
664
665
666
9.91k
  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
9.91k
  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
9.91k
  sps_read = true;
678
679
9.91k
  return DE265_OK;
680
9.91k
}
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
125k
{
863
125k
  const position* scan;
864
125k
  int width;
865
125k
  int subWidth;
866
867
125k
  switch (sizeId) {
868
39.2k
  case 0:
869
39.2k
    width=4;
870
39.2k
    subWidth=1;
871
39.2k
    scan = get_scan_order(2, 0 /* diag */);
872
873
667k
    for (int i=0;i<4*4;i++) {
874
628k
      scalingFactors[scan[i].x + width*scan[i].y] = sclist[i];
875
628k
    }
876
39.2k
    break;
877
878
38.5k
  case 1:
879
38.5k
    width=8;
880
38.5k
    subWidth=1;
881
38.5k
    scan = get_scan_order(3, 0 /* diag */);
882
883
2.50M
    for (int i=0;i<8*8;i++) {
884
2.46M
      scalingFactors[scan[i].x + width*scan[i].y] = sclist[i];
885
2.46M
    }
886
38.5k
    break;
887
888
36.7k
  case 2:
889
36.7k
    width=8;
890
36.7k
    subWidth=2;
891
36.7k
    scan = get_scan_order(3, 0 /* diag */);
892
893
2.35M
    for (int i=0;i<8*8;i++) {
894
6.94M
      for (int dy=0;dy<2;dy++)
895
13.8M
        for (int dx=0;dx<2;dx++)
896
9.26M
          {
897
9.26M
            int x = 2*scan[i].x+dx;
898
9.26M
            int y = 2*scan[i].y+dy;
899
9.26M
            scalingFactors[x+width*subWidth*y] = sclist[i];
900
9.26M
          }
901
2.31M
    }
902
36.7k
    break;
903
904
12.0k
  case 3:
905
12.0k
    width=8;
906
12.0k
    subWidth=4;
907
12.0k
    scan = get_scan_order(3, 0 /* diag */);
908
909
756k
    for (int i=0;i<8*8;i++) {
910
3.72M
      for (int dy=0;dy<4;dy++)
911
14.8M
        for (int dx=0;dx<4;dx++)
912
11.9M
          {
913
11.9M
            int x = 4*scan[i].x+dx;
914
11.9M
            int y = 4*scan[i].y+dy;
915
11.9M
            scalingFactors[x+width*subWidth*y] = sclist[i];
916
11.9M
          }
917
744k
    }
918
12.0k
    break;
919
920
0
  default:
921
0
    assert(0);
922
0
    break;
923
125k
  }
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
125k
}
937
938
939
de265_error read_scaling_list(bitreader* br, const seq_parameter_set* sps,
940
                              scaling_list_data* sclist, bool inPPS)
941
4.17k
{
942
4.17k
  int dc_coeff[4][6];
943
944
19.0k
  for (int sizeId=0;sizeId<4;sizeId++) {
945
    //int n = ((sizeId==3) ? 2 : 6);
946
15.5k
    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
91.3k
    for (int matrixId=0 ; matrixId<6 ; matrixId += (sizeId==3 ? 3 : 1)) {
950
76.5k
      uint8_t* curr_scaling_list = scaling_list[matrixId];
951
76.5k
      int scaling_list_dc_coef;
952
953
954
      //printf("----- matrix %d\n",matrixId);
955
956
76.5k
      char scaling_list_pred_mode_flag = br->get_bits(1);
957
76.5k
      if (!scaling_list_pred_mode_flag) {
958
8.71k
        uint32_t scaling_list_pred_matrix_id_delta = br->get_uvlc();
959
960
8.71k
        if (scaling_list_pred_matrix_id_delta == UVLC_ERROR) {
961
39
          return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
962
39
        }
963
964
8.68k
        if (sizeId == 3) {
965
          // adapt to our changed matrixId for size 3
966
2.58k
          scaling_list_pred_matrix_id_delta *= 3;
967
2.58k
        }
968
969
8.68k
        if (scaling_list_pred_matrix_id_delta > (uint32_t)matrixId) {
970
315
          return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
971
315
        }
972
973
        //printf("scaling_list_pred_matrix_id_delta=%d\n", scaling_list_pred_matrix_id_delta);
974
975
8.36k
        dc_coeff[sizeId][matrixId] = 16;
976
8.36k
        scaling_list_dc_coef       = 16;
977
978
8.36k
        if (scaling_list_pred_matrix_id_delta==0) {
979
5.77k
          if (sizeId==0) {
980
170
            memcpy(curr_scaling_list, default_ScalingList_4x4, 16);
981
170
          }
982
5.60k
          else {
983
5.60k
            if (matrixId<3)
984
3.15k
              { memcpy(curr_scaling_list, default_ScalingList_8x8_intra,64); }
985
2.45k
            else
986
2.45k
              { memcpy(curr_scaling_list, default_ScalingList_8x8_inter,64); }
987
5.60k
          }
988
5.77k
        }
989
2.59k
        else {
990
2.59k
          if (sizeId==3) { assert(scaling_list_pred_matrix_id_delta==3); }
991
992
2.59k
          int mID = matrixId - scaling_list_pred_matrix_id_delta;
993
994
2.59k
          int len = (sizeId == 0 ? 16 : 64);
995
2.59k
          memcpy(curr_scaling_list, scaling_list[mID], len);
996
997
2.59k
          scaling_list_dc_coef       = dc_coeff[sizeId][mID];
998
2.59k
          dc_coeff[sizeId][matrixId] = dc_coeff[sizeId][mID];
999
2.59k
        }
1000
8.36k
      }
1001
67.8k
      else {
1002
67.8k
        int nextCoef=8;
1003
67.8k
        int coefNum = (sizeId==0 ? 16 : 64);
1004
67.8k
        if (sizeId>1) {
1005
21.6k
          scaling_list_dc_coef = br->get_svlc();
1006
21.6k
          if (scaling_list_dc_coef < -7 ||
1007
21.5k
              scaling_list_dc_coef > 247) {
1008
58
            return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1009
58
          }
1010
1011
21.6k
          scaling_list_dc_coef += 8;
1012
21.6k
          nextCoef=scaling_list_dc_coef;
1013
21.6k
          dc_coeff[sizeId][matrixId] = scaling_list_dc_coef;
1014
21.6k
        }
1015
46.1k
        else {
1016
46.1k
          scaling_list_dc_coef = 16;
1017
46.1k
        }
1018
        //printf("DC = %d\n",scaling_list_dc_coef);
1019
1020
2.74M
        for (int i=0;i<coefNum;i++) {
1021
2.67M
          int scaling_list_delta_coef = br->get_svlc();
1022
2.67M
          if (scaling_list_delta_coef < -128 ||
1023
2.66M
              scaling_list_delta_coef >  127) {
1024
336
            return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
1025
336
          }
1026
1027
2.67M
          nextCoef = (nextCoef + scaling_list_delta_coef + 256) % 256;
1028
2.67M
          curr_scaling_list[i] = nextCoef;
1029
          //printf("curr %d = %d\n",i,nextCoef);
1030
2.67M
        }
1031
67.7k
      }
1032
1033
1034
      // --- generate ScalingFactor arrays ---
1035
1036
75.7k
      switch (sizeId) {
1037
23.9k
      case 0:
1038
23.9k
        fill_scaling_factor(&sclist->ScalingFactor_Size0[matrixId][0][0], curr_scaling_list, 0);
1039
23.9k
        break;
1040
1041
23.2k
      case 1:
1042
23.2k
        fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId][0][0], curr_scaling_list, 1);
1043
23.2k
        break;
1044
1045
21.4k
      case 2:
1046
21.4k
        fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId][0][0], curr_scaling_list, 2);
1047
21.4k
        sclist->ScalingFactor_Size2[matrixId][0][0] = scaling_list_dc_coef;
1048
        //printf("DC coeff: %d\n", scaling_list_dc_coef);
1049
21.4k
        break;
1050
1051
6.91k
      case 3:
1052
6.91k
        fill_scaling_factor(&sclist->ScalingFactor_Size3[matrixId][0][0], curr_scaling_list, 3);
1053
6.91k
        sclist->ScalingFactor_Size3[matrixId][0][0] = scaling_list_dc_coef;
1054
        //printf("DC coeff: %d\n", scaling_list_dc_coef);
1055
6.91k
        break;
1056
75.7k
      }
1057
75.7k
    }
1058
15.5k
  }
1059
1060
1061
  // --- fill 32x32 matrices for chroma
1062
1063
3.43k
  const position* scan = get_scan_order(3, 0 /* diag */);
1064
  
1065
24.0k
  for (int matrixId=0;matrixId<6;matrixId++)
1066
20.6k
    if (matrixId!=0 && matrixId!=3) {
1067
814k
      for (int i=0;i<64;i++) {
1068
800k
  int x = scan[i].x;
1069
800k
  int y = scan[i].y;
1070
800k
  int v = sclist->ScalingFactor_Size1[matrixId][y][x];
1071
1072
4.00M
  for (int dy=0;dy<4;dy++)
1073
15.9M
    for (int dx=0;dx<4;dx++) {
1074
12.7M
      sclist->ScalingFactor_Size3[matrixId][4*y+dy][4*x+dx] = v;
1075
12.7M
    }
1076
800k
      }
1077
1078
13.7k
      sclist->ScalingFactor_Size3[matrixId][0][0] = sclist->ScalingFactor_Size1[matrixId][0][0];
1079
13.7k
    }
1080
  
1081
3.43k
  return DE265_OK;
1082
4.17k
}
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.55k
{
1097
  // 4x4
1098
1099
17.8k
  for (int matrixId=0;matrixId<6;matrixId++) {
1100
15.3k
    fill_scaling_factor(&sclist->ScalingFactor_Size0[matrixId][0][0],
1101
15.3k
                        default_ScalingList_4x4, 0);
1102
15.3k
  }
1103
1104
  // 8x8
1105
1106
10.2k
  for (int matrixId=0;matrixId<3;matrixId++) {
1107
7.65k
    fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId+0][0][0],
1108
7.65k
                        default_ScalingList_8x8_intra, 1);
1109
7.65k
    fill_scaling_factor(&sclist->ScalingFactor_Size1[matrixId+3][0][0],
1110
7.65k
                        default_ScalingList_8x8_inter, 1);
1111
7.65k
  }
1112
1113
  // 16x16
1114
1115
10.2k
  for (int matrixId=0;matrixId<3;matrixId++) {
1116
7.65k
    fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId+0][0][0],
1117
7.65k
                        default_ScalingList_8x8_intra, 2);
1118
7.65k
    fill_scaling_factor(&sclist->ScalingFactor_Size2[matrixId+3][0][0],
1119
7.65k
                        default_ScalingList_8x8_inter, 2);
1120
7.65k
  }
1121
1122
  // 32x32
1123
1124
2.55k
  fill_scaling_factor(&sclist->ScalingFactor_Size3[0][0][0],
1125
2.55k
                      default_ScalingList_8x8_intra, 3);
1126
2.55k
  fill_scaling_factor(&sclist->ScalingFactor_Size3[1][0][0],
1127
2.55k
                      default_ScalingList_8x8_inter, 3);
1128
2.55k
}
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
2.15k
{
1359
2.15k
  transform_skip_rotation_enabled_flag    = br->get_bits(1);
1360
2.15k
  transform_skip_context_enabled_flag     = br->get_bits(1);
1361
2.15k
  implicit_rdpcm_enabled_flag             = br->get_bits(1);
1362
2.15k
  explicit_rdpcm_enabled_flag             = br->get_bits(1);
1363
2.15k
  extended_precision_processing_flag      = br->get_bits(1);
1364
2.15k
  intra_smoothing_disabled_flag           = br->get_bits(1);
1365
2.15k
  high_precision_offsets_enabled_flag     = br->get_bits(1);
1366
2.15k
  persistent_rice_adaptation_enabled_flag = br->get_bits(1);
1367
2.15k
  cabac_bypass_alignment_enabled_flag     = br->get_bits(1);
1368
1369
2.15k
  return DE265_OK;
1370
2.15k
}
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