Coverage Report

Created: 2026-08-31 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/vui.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 "vui.h"
22
#include "decctx.h"
23
24
#include <assert.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#define READ_VLC(variable, vlctype)   \
29
36.0k
  if ((vlc = br->get_ ## vlctype()) == UVLC_ERROR) {   \
30
305
    errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);  \
31
305
    return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE; \
32
305
  } \
33
36.0k
  (variable) = vlc;
34
35
36
7.93k
#define NUM_SAR_PRESETS 17
37
38
static uint16_t sar_presets[NUM_SAR_PRESETS+1][2] = {
39
  { 0,0 },
40
  { 1,1 },
41
  { 12,11 },
42
  { 10,11 },
43
  { 16,11 },
44
  { 40,33 },
45
  { 24,11 },
46
  { 20,11 },
47
  { 32,11 },
48
  { 80,33 },
49
  { 18,11 },
50
  { 15,11 },
51
  { 64,33 },
52
  { 160,99 },
53
  { 4,3 },
54
  { 3,2 },
55
  { 2,1 }
56
};
57
58
7.61k
#define EXTENDED_SAR 255
59
60
61
const char* get_video_format_name(enum VideoFormat format)
62
0
{
63
0
  switch (format) {
64
0
  case VideoFormat_Component: return "component";
65
0
  case VideoFormat_PAL:       return "PAL";
66
0
  case VideoFormat_NTSC:      return "NTSC";
67
0
  case VideoFormat_SECAM:     return "SECAM";
68
0
  case VideoFormat_MAC:       return "MAC";
69
0
  default:                    return "unspecified";
70
0
  }
71
0
}
72
73
74
26.6k
video_usability_information::video_usability_information() = default;
75
76
77
de265_error video_usability_information::hrd_parameters(error_queue* errqueue, bitreader* br, const seq_parameter_set* sps)
78
742
{
79
742
  uint32_t vlc;
80
81
742
  nal_hrd_parameters_present_flag = br->get_bits(1);
82
742
  vcl_hrd_parameters_present_flag = br->get_bits(1);
83
84
742
  if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
85
622
  {
86
622
    sub_pic_hrd_params_present_flag = br->get_bits(1);
87
622
    if (sub_pic_hrd_params_present_flag)
88
464
    {
89
464
      tick_divisor_minus2 = br->get_bits(8);
90
464
      du_cpb_removal_delay_increment_length_minus1 = br->get_bits(5);
91
464
      sub_pic_cpb_params_in_pic_timing_sei_flag = br->get_bits(1);
92
464
      dpb_output_delay_du_length_minus1 = br->get_bits(5);
93
464
    }
94
622
    bit_rate_scale = br->get_bits(4);
95
622
    cpb_size_scale = br->get_bits(4);
96
97
98
622
    if (sub_pic_hrd_params_present_flag)
99
464
    {
100
464
      cpb_size_du_scale = br->get_bits(4);
101
464
    }
102
622
    initial_cpb_removal_delay_length_minus1 = br->get_bits(5);
103
622
    au_cpb_removal_delay_length_minus1 = br->get_bits(5);
104
622
    dpb_output_delay_length_minus1 = br->get_bits(5);
105
622
  }
106
742
  int  i, nalOrVcl;
107
108
1.45k
  for (i = 0; i < sps->sps_max_sub_layers; i++)
109
1.00k
  {
110
1.00k
    fixed_pic_rate_general_flag[i] = br->get_bits(1);
111
1.00k
    if (!fixed_pic_rate_general_flag[i])
112
558
    {
113
558
      fixed_pic_rate_within_cvs_flag[i] = br->get_bits(1);
114
558
    }
115
450
    else
116
450
    {
117
450
      fixed_pic_rate_within_cvs_flag[i] = true;
118
450
    }
119
120
1.00k
    low_delay_hrd_flag[i] = 0;// Inferred to be 0 when not present
121
1.00k
    cpb_cnt_minus1[i] = 0;    // Inferred to be 0 when not present
122
123
1.00k
    if (fixed_pic_rate_within_cvs_flag[i])
124
572
    {
125
572
      READ_VLC(elemental_duration_in_tc_minus1[i], uvlc);
126
555
    }
127
436
    else
128
436
    {
129
436
      low_delay_hrd_flag[i] = br->get_bits(1);
130
436
    }
131
991
    if (!low_delay_hrd_flag[i])
132
799
    {
133
799
      READ_VLC(cpb_cnt_minus1[i], uvlc);
134
715
      if (cpb_cnt_minus1[i] > 31) {
135
70
  return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
136
70
      }
137
715
    }
138
139
2.33k
    for (nalOrVcl = 0; nalOrVcl < 2; nalOrVcl++)
140
1.61k
    {
141
1.61k
      if (((nalOrVcl == 0) && nal_hrd_parameters_present_flag) ||
142
1.09k
        ((nalOrVcl == 1) && vcl_hrd_parameters_present_flag))
143
1.10k
      {
144
7.42k
        for (uint32_t j = 0; j <= cpb_cnt_minus1[i]; j++)
145
6.44k
        {
146
6.44k
          READ_VLC(bit_rate_value_minus1[i][j][nalOrVcl], uvlc);
147
6.41k
          READ_VLC(cpb_size_value_minus1[i][j][nalOrVcl], uvlc);
148
149
6.37k
          if (sub_pic_hrd_params_present_flag)
150
2.72k
          {
151
2.72k
            READ_VLC(cpb_size_du_value_minus1[i][j][nalOrVcl], uvlc);
152
2.69k
            READ_VLC(bit_rate_du_value_minus1[i][j][nalOrVcl], uvlc);
153
2.66k
          }
154
6.32k
          cbr_flag[i][j][nalOrVcl] = br->get_bits(1);
155
6.32k
        }
156
1.10k
      }
157
1.61k
    }
158
837
  }
159
449
  return DE265_OK;
160
742
}
161
162
de265_error video_usability_information::read(error_queue* errqueue, bitreader* br,
163
                                              const seq_parameter_set* sps)
164
11.9k
{
165
11.9k
  uint32_t vlc;
166
167
168
  // --- sample aspect ratio (SAR) ---
169
170
11.9k
  aspect_ratio_info_present_flag = br->get_bits(1);
171
11.9k
  if (aspect_ratio_info_present_flag) {
172
7.93k
    int aspect_ratio_idc = br->get_bits(8);
173
7.93k
    if (aspect_ratio_idc <= NUM_SAR_PRESETS) {
174
321
      sar_width = sar_presets[aspect_ratio_idc][0];
175
321
      sar_height = sar_presets[aspect_ratio_idc][1];
176
321
    }
177
7.61k
    else if (aspect_ratio_idc == EXTENDED_SAR) {
178
1.08k
      sar_width = br->get_bits(16);
179
1.08k
      sar_height = br->get_bits(16);
180
1.08k
    }
181
6.52k
    else {
182
6.52k
      sar_width = 0;
183
6.52k
      sar_height = 0;
184
6.52k
    }
185
7.93k
  }
186
3.98k
  else {
187
3.98k
    sar_width = 0;
188
3.98k
    sar_height = 0;
189
3.98k
  }
190
191
192
  // --- overscan ---
193
194
11.9k
  overscan_info_present_flag = br->get_bits(1);
195
11.9k
  if (overscan_info_present_flag) {
196
2.55k
    overscan_appropriate_flag = br->get_bits(1);
197
2.55k
  }
198
199
200
  // --- video signal type ---
201
202
11.9k
  { // defaults
203
11.9k
    video_format = VideoFormat_Unspecified;
204
11.9k
    video_full_range_flag = false;
205
11.9k
    colour_primaries = 2;
206
11.9k
    transfer_characteristics = 2;
207
11.9k
    matrix_coeffs = 2;
208
11.9k
  }
209
210
11.9k
  video_signal_type_present_flag = br->get_bits(1);
211
11.9k
  if (video_signal_type_present_flag) {
212
8.01k
    int video_format_idc = br->get_bits(3);
213
8.01k
    if (video_format_idc > 5) {
214
3.75k
      video_format_idc = VideoFormat_Unspecified;
215
3.75k
    }
216
8.01k
    video_format = (VideoFormat)video_format_idc;
217
218
8.01k
    video_full_range_flag = br->get_bits(1);
219
220
8.01k
    colour_description_present_flag = br->get_bits(1);
221
8.01k
    if (colour_description_present_flag) {
222
4.78k
      colour_primaries = br->get_bits(8);
223
4.78k
      if (colour_primaries == 0 ||
224
4.74k
        colour_primaries == 3 ||
225
4.68k
        colour_primaries >= 11) {
226
4.67k
        colour_primaries = 2;
227
4.67k
      }
228
229
4.78k
      transfer_characteristics = br->get_bits(8);
230
4.78k
      if (transfer_characteristics == 0 ||
231
4.46k
        transfer_characteristics == 3 ||
232
4.55k
        transfer_characteristics >= 18) {
233
4.55k
        transfer_characteristics = 2;
234
4.55k
      }
235
236
4.78k
      matrix_coeffs = br->get_bits(8);
237
      
238
4.78k
      if (matrix_coeffs >= 11) {
239
4.08k
        matrix_coeffs = 2;
240
4.08k
      }
241
4.78k
    }
242
8.01k
  }
243
244
245
  // --- chroma / interlaced ---
246
247
11.9k
  chroma_loc_info_present_flag = br->get_bits(1);
248
11.9k
  if (chroma_loc_info_present_flag) {
249
4.43k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 5) {
250
76
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
251
76
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
252
76
    }
253
4.35k
    chroma_sample_loc_type_top_field = vlc;
254
255
4.35k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 5) {
256
155
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
257
155
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
258
155
    }
259
4.19k
    chroma_sample_loc_type_bottom_field = vlc;
260
4.19k
  }
261
7.49k
  else {
262
7.49k
    chroma_sample_loc_type_top_field = 0;
263
7.49k
    chroma_sample_loc_type_bottom_field = 0;
264
7.49k
  }
265
266
11.6k
  neutral_chroma_indication_flag = br->get_bits(1);
267
11.6k
  field_seq_flag = br->get_bits(1);
268
11.6k
  frame_field_info_present_flag = br->get_bits(1);
269
270
271
  // --- default display window ---
272
273
11.6k
  default_display_window_flag = br->get_bits(1);
274
11.6k
  if (default_display_window_flag) {
275
4.12k
    READ_VLC(def_disp_win_left_offset, uvlc);
276
4.11k
    READ_VLC(def_disp_win_right_offset, uvlc);
277
4.09k
    READ_VLC(def_disp_win_top_offset, uvlc);
278
4.07k
    READ_VLC(def_disp_win_bottom_offset, uvlc);
279
4.04k
  }
280
7.56k
  else {
281
7.56k
    def_disp_win_left_offset = 0;
282
7.56k
    def_disp_win_right_offset = 0;
283
7.56k
    def_disp_win_top_offset = 0;
284
7.56k
    def_disp_win_bottom_offset = 0;
285
7.56k
  }
286
287
288
  // --- timing ---
289
290
11.6k
  vui_timing_info_present_flag = br->get_bits(1);
291
11.6k
  if (vui_timing_info_present_flag) {
292
4.39k
    vui_num_units_in_tick = br->get_bits(32);
293
4.39k
    vui_time_scale = br->get_bits(32);
294
295
4.39k
    vui_poc_proportional_to_timing_flag = br->get_bits(1);
296
4.39k
    if (vui_poc_proportional_to_timing_flag) {
297
701
      if ((vlc = br->get_uvlc()) == UVLC_ERROR) {
298
9
        errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
299
9
        return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
300
9
      }
301
302
692
      vui_num_ticks_poc_diff_one = vlc + 1;
303
692
    }
304
305
    // --- hrd parameters ---
306
307
4.38k
    vui_hrd_parameters_present_flag = br->get_bits(1);
308
4.38k
    if (vui_hrd_parameters_present_flag) {
309
742
      de265_error err;
310
742
      err = hrd_parameters(errqueue, br, sps);
311
742
      if (err) {
312
293
  return err;
313
293
      }
314
742
    }
315
4.38k
  }
316
317
  // --- bitstream restriction ---
318
319
11.3k
  bitstream_restriction_flag = br->get_bits(1);
320
11.3k
  if (bitstream_restriction_flag) {
321
1.30k
    tiles_fixed_structure_flag = br->get_bits(1);
322
1.30k
    motion_vectors_over_pic_boundaries_flag = br->get_bits(1);
323
1.30k
    restricted_ref_pic_lists_flag = br->get_bits(1);
324
325
1.30k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 4095) {
326
37
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
327
37
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
328
37
    }
329
1.26k
    min_spatial_segmentation_idc = vlc;
330
331
1.26k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 16) {
332
84
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
333
84
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
334
84
    }
335
1.18k
    max_bytes_per_pic_denom = vlc;
336
337
1.18k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 16) {
338
144
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
339
144
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
340
144
    }
341
1.03k
    max_bits_per_min_cu_denom = vlc;
342
343
1.03k
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 15) {
344
100
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
345
100
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
346
100
    }
347
937
    log2_max_mv_length_horizontal = vlc;
348
349
937
    if ((vlc = br->get_uvlc()) == UVLC_ERROR || vlc > 15) {
350
55
      errqueue->add_warning(DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE, false);
351
55
      return DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE;
352
55
    }
353
882
    log2_max_mv_length_vertical = vlc;
354
882
  }
355
10.0k
  else {
356
10.0k
    tiles_fixed_structure_flag = false;
357
10.0k
    motion_vectors_over_pic_boundaries_flag = true;
358
10.0k
    restricted_ref_pic_lists_flag = false; // NOTE: default not specified in standard 2014/10
359
360
10.0k
    min_spatial_segmentation_idc = 0;
361
10.0k
    max_bytes_per_pic_denom   = 2;
362
10.0k
    max_bits_per_min_cu_denom = 1;
363
10.0k
    log2_max_mv_length_horizontal = 15;
364
10.0k
    log2_max_mv_length_vertical   = 15;
365
10.0k
  }
366
367
  //vui_read = true;
368
369
10.8k
  return DE265_OK;
370
11.3k
}
371
372
373
void video_usability_information::dump(int fd) const
374
0
{
375
  //#if (_MSC_VER >= 1500)
376
  //#define LOG0(t) loginfo(LogHeaders, t)
377
  //#define LOG1(t,d) loginfo(LogHeaders, t,d)
378
  //#define LOG2(t,d1,d2) loginfo(LogHeaders, t,d1,d2)
379
  //#define LOG3(t,d1,d2,d3) loginfo(LogHeaders, t,d1,d2,d3)
380
381
0
  FILE* fh;
382
0
  if (fd==1) fh=stdout;
383
0
  else if (fd==2) fh=stderr;
384
0
  else { return; }
385
386
0
#define LOG0(t) log2fh(fh, t)
387
0
#define LOG1(t,d) log2fh(fh, t,d)
388
0
#define LOG2(t,d1,d2) log2fh(fh, t,d1,d2)
389
0
#define LOG3(t,d1,d2,d3) log2fh(fh, t,d1,d2,d3)
390
391
0
  LOG0("----------------- VUI -----------------\n");
392
0
  LOG2("sample aspect ratio        : %d:%d\n", sar_width,sar_height);
393
0
  LOG1("overscan_info_present_flag : %d\n", overscan_info_present_flag);
394
0
  LOG1("overscan_appropriate_flag  : %d\n", overscan_appropriate_flag);
395
396
0
  LOG1("video_signal_type_present_flag: %d\n", video_signal_type_present_flag);
397
0
  if (video_signal_type_present_flag) {
398
0
    LOG1("  video_format                : %s\n", get_video_format_name(video_format));
399
0
    LOG1("  video_full_range_flag       : %d\n", video_full_range_flag);
400
0
    LOG1("  colour_description_present_flag : %d\n", colour_description_present_flag);
401
0
    LOG1("  colour_primaries            : %d\n", colour_primaries);
402
0
    LOG1("  transfer_characteristics    : %d\n", transfer_characteristics);
403
0
    LOG1("  matrix_coeffs               : %d\n", matrix_coeffs);
404
0
  }
405
406
0
  LOG1("chroma_loc_info_present_flag: %d\n", chroma_loc_info_present_flag);
407
0
  if (chroma_loc_info_present_flag) {
408
0
    LOG1("  chroma_sample_loc_type_top_field   : %d\n", chroma_sample_loc_type_top_field);
409
0
    LOG1("  chroma_sample_loc_type_bottom_field: %d\n", chroma_sample_loc_type_bottom_field);
410
0
  }
411
412
0
  LOG1("neutral_chroma_indication_flag: %d\n", neutral_chroma_indication_flag);
413
0
  LOG1("field_seq_flag                : %d\n", field_seq_flag);
414
0
  LOG1("frame_field_info_present_flag : %d\n", frame_field_info_present_flag);
415
416
0
  LOG1("default_display_window_flag   : %d\n", default_display_window_flag);
417
0
  LOG1("  def_disp_win_left_offset    : %d\n", def_disp_win_left_offset);
418
0
  LOG1("  def_disp_win_right_offset   : %d\n", def_disp_win_right_offset);
419
0
  LOG1("  def_disp_win_top_offset     : %d\n", def_disp_win_top_offset);
420
0
  LOG1("  def_disp_win_bottom_offset  : %d\n", def_disp_win_bottom_offset);
421
422
0
  LOG1("vui_timing_info_present_flag  : %d\n", vui_timing_info_present_flag);
423
0
  if (vui_timing_info_present_flag) {
424
0
    LOG1("  vui_num_units_in_tick       : %d\n", vui_num_units_in_tick);
425
0
    LOG1("  vui_time_scale              : %d\n", vui_time_scale);
426
0
  }
427
428
0
  LOG1("vui_poc_proportional_to_timing_flag : %d\n", vui_poc_proportional_to_timing_flag);
429
0
  LOG1("vui_num_ticks_poc_diff_one          : %d\n", vui_num_ticks_poc_diff_one);
430
431
0
  LOG1("vui_hrd_parameters_present_flag : %d\n", vui_hrd_parameters_present_flag);
432
0
  if (vui_hrd_parameters_present_flag) {
433
    //hrd_parameters vui_hrd_parameters;
434
0
  }
435
436
437
  // --- bitstream restriction ---
438
439
0
  LOG1("bitstream_restriction_flag         : %d\n", bitstream_restriction_flag);
440
0
  if (bitstream_restriction_flag) {
441
0
    LOG1("  tiles_fixed_structure_flag       : %d\n", tiles_fixed_structure_flag);
442
0
    LOG1("  motion_vectors_over_pic_boundaries_flag : %d\n", motion_vectors_over_pic_boundaries_flag);
443
0
    LOG1("  restricted_ref_pic_lists_flag    : %d\n", restricted_ref_pic_lists_flag);
444
0
    LOG1("  min_spatial_segmentation_idc     : %d\n", min_spatial_segmentation_idc);
445
0
    LOG1("  max_bytes_per_pic_denom          : %d\n", max_bytes_per_pic_denom);
446
0
    LOG1("  max_bits_per_min_cu_denom        : %d\n", max_bits_per_min_cu_denom);
447
0
    LOG1("  log2_max_mv_length_horizontal    : %d\n", log2_max_mv_length_horizontal);
448
0
    LOG1("  log2_max_mv_length_vertical      : %d\n", log2_max_mv_length_vertical);
449
0
  }
450
451
0
#undef LOG0
452
0
#undef LOG1
453
0
#undef LOG2
454
0
#undef LOG3
455
  //#endif
456
0
}