Coverage Report

Created: 2026-05-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/EncoderLib/EncHRD.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
#include "EncHRD.h"
44
#include "CommonLib/ProfileLevelTier.h"
45
46
namespace vvenc {
47
48
// calculate scale value of bitrate and initial delay
49
int EncHRD::xCalcScale(uint32_t x)
50
3.14k
{
51
3.14k
  if (x==0)
52
1.29k
  {
53
1.29k
    return 0;
54
1.29k
  }
55
1.84k
  uint32_t mask = 0xffffffff;
56
1.84k
  int scaleValue = 32;
57
58
52.3k
  while ((x&mask) != 0)
59
50.5k
  {
60
50.5k
    scaleValue--;
61
50.5k
    mask = (mask >> 1);
62
50.5k
  }
63
64
1.84k
  return scaleValue;
65
3.14k
}
66
67
void EncHRD::initHRDParameters(const VVEncCfg& encCfg, const SPS& sps)
68
1.29k
{
69
//  if (!encCfg.m_hrdParametersPresent && !encCfg.getCpbSaturationEnabled())
70
//  {
71
//    return;
72
//  }
73
1.29k
  ProfileLevelTierFeatures profileLevelTierFeatures;
74
1.29k
  profileLevelTierFeatures.extractPTLInformation( sps );
75
76
1.29k
  bool useSubCpbParams = false; //encCfg.getNoPicPartitionFlag() == false;
77
1.29k
  uint32_t bitRate = (uint32_t)encCfg.m_RCTargetBitrate;
78
1.29k
  uint32_t cpbSize = (uint32_t)profileLevelTierFeatures.getCpbSizeInBits();
79
80
1.29k
  CHECK(!(cpbSize != 0), "Unspecified error");  // CPB size may not be equal to zero. ToDo: have a better default and check for level constraints
81
82
1.29k
  generalHrdParams.timeScale      = encCfg.m_FrameRate;
83
1.29k
  generalHrdParams.numUnitsInTick = encCfg.m_FrameScale;
84
85
1.29k
  bool rateCnt = (bitRate > 0);
86
87
1.29k
  generalHrdParams.generalNalHrdParamsPresent = rateCnt;
88
1.29k
  generalHrdParams.generalVclHrdParamsPresent = rateCnt;
89
90
1.29k
  generalHrdParams.generalSamePicTimingInAllOlsFlag = true;
91
1.29k
  useSubCpbParams &= (generalHrdParams.generalNalHrdParamsPresent || generalHrdParams.generalVclHrdParamsPresent);
92
1.29k
  generalHrdParams.generalDecodingUnitHrdParamsPresent = useSubCpbParams;
93
94
1.29k
  if (generalHrdParams.generalDecodingUnitHrdParamsPresent)
95
0
  {
96
0
    generalHrdParams.tickDivisorMinus2 = (100 - 2);
97
0
  }
98
99
1.29k
  if (xCalcScale(bitRate) <= 6)
100
1.29k
  {
101
1.29k
    generalHrdParams.bitRateScale = 0;
102
1.29k
  }
103
0
  else
104
0
  {
105
0
    generalHrdParams.bitRateScale = xCalcScale(bitRate) - 6;
106
0
  }
107
108
1.29k
  if (xCalcScale(cpbSize) <= 4)
109
752
  {
110
752
    generalHrdParams.cpbSizeScale = 0;
111
752
  }
112
546
  else
113
546
  {
114
546
    generalHrdParams.cpbSizeScale = xCalcScale(cpbSize) - 4;
115
546
  }
116
117
1.29k
  generalHrdParams.cpbSizeDuScale = 6;                                     // in units of 2^( 4 + 6 ) = 1,024 bit
118
1.29k
  generalHrdParams.hrdCpbCntMinus1 = 0;
119
120
121
  // Note: parameters for all temporal layers are initialized with the same values
122
1.29k
  int i, j;
123
1.29k
  uint32_t bitrateValue, cpbSizeValue;
124
1.29k
  uint32_t duCpbSizeValue;
125
1.29k
  uint32_t duBitRateValue = 0;
126
127
10.3k
  for (i = 0; i < VVENC_MAX_TLAYER; i++)
128
9.08k
  {
129
9.08k
    olsHrdParams[i].fixedPicRateGeneralFlag = true;
130
9.08k
    olsHrdParams[i].fixedPicRateWithinCvsFlag = true;
131
9.08k
    olsHrdParams[i].elementDurationInTcMinus1 = 0;
132
9.08k
    olsHrdParams[i].lowDelayHrdFlag = false;
133
134
    //! \todo check for possible PTL violations
135
    // BitRate[ i ] = ( bit_rate_value_minus1[ i ] + 1 ) * 2^( 6 + bit_rate_scale )
136
9.08k
    bitrateValue = bitRate / (1 << (6 + generalHrdParams.bitRateScale));      // bitRate is in bits, so it needs to be scaled down
137
                                                                              // CpbSize[ i ] = ( cpb_size_value_minus1[ i ] + 1 ) * 2^( 4 + cpb_size_scale )
138
9.08k
    cpbSizeValue = cpbSize / (1 << (4 + generalHrdParams.cpbSizeScale));      // using bitRate results in 1 second CPB size
139
140
                                                                              // DU CPB size could be smaller (i.e. bitrateValue / number of DUs), but we don't know
141
                                                                              // in how many DUs the slice segment settings will result
142
9.08k
    duCpbSizeValue = bitrateValue;
143
9.08k
    duBitRateValue = cpbSizeValue;
144
145
18.1k
    for (j = 0; j < (generalHrdParams.hrdCpbCntMinus1 + 1); j++)
146
9.08k
    {
147
9.08k
      olsHrdParams[i].bitRateValueMinus1[j][0] =  bitrateValue - 1;
148
9.08k
      olsHrdParams[i].cpbSizeValueMinus1[j][0] =  cpbSizeValue - 1;
149
9.08k
      olsHrdParams[i].duCpbSizeValueMinus1[j][0] =  duCpbSizeValue - 1;
150
9.08k
      olsHrdParams[i].duBitRateValueMinus1[j][0] =  duBitRateValue - 1;
151
9.08k
      olsHrdParams[i].cbrFlag[j][0] =  false;
152
153
9.08k
      olsHrdParams[i].bitRateValueMinus1[j][1] =  bitrateValue - 1;
154
9.08k
      olsHrdParams[i].cpbSizeValueMinus1[j][1] =  cpbSizeValue - 1;
155
9.08k
      olsHrdParams[i].duCpbSizeValueMinus1[j][1] =  duCpbSizeValue - 1;
156
9.08k
      olsHrdParams[i].duBitRateValueMinus1[j][1] =  duBitRateValue - 1;
157
9.08k
      olsHrdParams[i].cbrFlag[j][1] =  false;
158
9.08k
    }
159
9.08k
  }
160
1.29k
}
161
162
} //namespace