Coverage Report

Created: 2026-04-01 07:49

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