Coverage Report

Created: 2026-06-10 06:55

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