forked from nvpro-samples/vk_video_samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVkEncoderConfigAV1.h
202 lines (166 loc) · 11.7 KB
/
VkEncoderConfigAV1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright 2023 NVIDIA Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef VKVIDEOENCODER_VKENCODERCONFIG_AV1_H_
#define VKVIDEOENCODER_VKENCODERCONFIG_AV1_H_
#include "VkVideoEncoder/VkEncoderConfig.h"
#include "VkVideoEncoder/VkVideoEncoderStateAV1.h"
#define FRAME_ID_BITS 15
#define DELTA_FRAME_ID_BITS 14
#define ORDER_HINT_BITS 7
#define BASE_QIDX_INTRA 114
#define BASE_QIDX_INTER_P 131
#define BASE_QIDX_INTER_B 147
struct EncoderConfigAV1 : public EncoderConfig {
enum { FRAME_RATE_NUM_DEFAULT = 30000 };
enum { FRAME_RATE_DEN_DEFAULT = 1001 };
enum { IDR_PERIOD_DEFAULT = 60 };
enum { GOP_LENGTH_DEFAULT = 60 };
struct LevelLimits {
StdVideoAV1Level level;
uint32_t maxPicSize; // samples
uint32_t maxHSize; // samples
uint32_t maxVSize; // samples
uint64_t maxDisplayRate; // samples/sec
uint64_t maxDecodeRate; // samples/sec
uint32_t maxHeaderRate; // /sec
uint32_t mainBps; // bits/sec
uint32_t highBps; // bits/sec
double mainCR; // ratio
double highCR; // ratio
uint32_t maxTiles; //
uint32_t maxTileCols; //
};
EncoderConfigAV1()
{
static const LevelLimits levelLimitsTbl[] = {
{ STD_VIDEO_AV1_LEVEL_2_0, 147456, 2048, 1152, 4423680, 5529600, 150, 1500000, 0, 2, -1, 8, 4 }, // level 2.0
{ STD_VIDEO_AV1_LEVEL_2_1, 278784, 2816, 1584, 8363520, 10454400, 150, 3000000, 0, 2, -1, 8, 4 }, // level 2.1
{ STD_VIDEO_AV1_LEVEL_INVALID, 278784, 2816, 1584, 8363520, 10454400, 150, 3000000, 0, 2, -1, 8, 4 }, // level 2.2 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 278784, 2816, 1584, 8363520, 10454400, 150, 3000000, 0, 2, -1, 8, 4 }, // level 2.3 - undefined
{ STD_VIDEO_AV1_LEVEL_3_0, 665856, 4352, 2448, 19975680, 24969600, 150, 6000000, 0, 2, -1, 16, 6 }, // level 3.0
{ STD_VIDEO_AV1_LEVEL_3_1, 1065024, 5504, 3096, 31950720, 39938400, 150, 10000000, 0, 2, -1, 16, 6 }, // level 3.1
{ STD_VIDEO_AV1_LEVEL_INVALID, 1065024, 5504, 3096, 31950720, 39938400, 150, 10000000, 0, 2, -1, 16, 6 }, // level 3.2 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 1065024, 5504, 3096, 31950720, 39938400, 150, 10000000, 0, 2, -1, 16, 6 }, // level 3.3 - undefined
{ STD_VIDEO_AV1_LEVEL_4_0, 2359296, 6144, 3456, 70778880, 77856768, 300, 12000000, 30000000, 4, 4, 32, 8 }, // level 4.0
{ STD_VIDEO_AV1_LEVEL_4_1, 2359296, 6144, 3456, 141557760, 155713536, 300, 20000000, 50000000, 4, 4, 32, 8 }, // level 4.1
{ STD_VIDEO_AV1_LEVEL_INVALID, 2359296, 6144, 3456, 141557760, 155713536, 300, 20000000, 50000000, 4, 4, 32, 8 }, // level 4.2 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 2359296, 6144, 3456, 141557760, 155713536, 300, 20000000, 50000000, 4, 4, 32, 8 }, // level 4.3 - undefined
{ STD_VIDEO_AV1_LEVEL_5_0, 8912896, 8192, 4352, 267386880, 273715200, 300, 30000000, 100000000, 6, 4, 64, 8 }, // level 5.0
{ STD_VIDEO_AV1_LEVEL_5_1, 8912896, 8192, 4352, 534773760, 547430400, 300, 40000000, 160000000, 8, 4, 64, 8 }, // level 5.1
{ STD_VIDEO_AV1_LEVEL_5_2, 8912896, 8192, 4352, 1069547520, 1094860800, 300, 60000000, 240000000, 8, 4, 64, 8 }, // level 5.2
{ STD_VIDEO_AV1_LEVEL_5_3, 8912896, 8192, 4352, 1069547520, 1176502272, 300, 60000000, 240000000, 8, 4, 64, 8 }, // level 5.3
{ STD_VIDEO_AV1_LEVEL_6_0, 35651584, 16384, 8704, 1069547520, 1176502272, 300, 60000000, 240000000, 8, 4, 128, 16 }, // level 6.0
{ STD_VIDEO_AV1_LEVEL_6_1, 35651584, 16384, 8704, 2139095040, 2189721600, 300, 100000000, 480000000, 8, 4, 128, 16 }, // level 6.1
{ STD_VIDEO_AV1_LEVEL_6_2, 35651584, 16384, 8704, 4278190080, 4379443200, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 6.2
{ STD_VIDEO_AV1_LEVEL_6_3, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 6.3
{ STD_VIDEO_AV1_LEVEL_INVALID, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 7.0 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 7.1 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 7.2 - undefined
{ STD_VIDEO_AV1_LEVEL_INVALID, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160000000, 800000000, 8, 4, 128, 16 }, // level 7.3 - undefined
};
levelLimits = levelLimitsTbl;
levelLimitsSize = ARRAYSIZE(levelLimitsTbl);
frameRateNumerator = FRAME_RATE_NUM_DEFAULT;
frameRateDenominator = FRAME_RATE_DEN_DEFAULT;
}
virtual ~EncoderConfigAV1() {}
virtual int DoParseArguments(int argc, char* argv[]) override;
virtual VkResult InitializeParameters() override
{
VkResult result = EncoderConfig::InitializeParameters();
if (result != VK_SUCCESS) {
return result;
}
hrdBitrate = maxBitrate;
pic_width_in_sbs = DivUp<uint32_t>(encodeWidth, 64);
pic_height_in_sbs = DivUp<uint32_t>(encodeHeight, 16);
if ((pic_width_in_sbs > 0) && (pic_height_in_sbs > 0)) {
return VK_SUCCESS;
}
assert(!"Invalid pic_width_in_sbs and pic_height_in_sbs");
return VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR;
}
virtual VkResult InitDeviceCapabilities(const VulkanDeviceContext* vkDevCtx) override;
virtual uint32_t GetDefaultVideoProfileIdc() override { return STD_VIDEO_AV1_PROFILE_MAIN; }
virtual int8_t InitDpbCount() override;
virtual bool InitRateControl() override;
virtual uint8_t GetMaxBFrameCount() { return static_cast<uint8_t>(av1EncodeCapabilities.maxBidirectionalCompoundReferenceCount); }
bool GetRateControlParameters(VkVideoEncodeRateControlInfoKHR* rcInfo,
VkVideoEncodeRateControlLayerInfoKHR* rcLayerInfo,
VkVideoEncodeAV1RateControlInfoKHR* rcInfoAV1,
VkVideoEncodeAV1RateControlLayerInfoKHR* rcLayerInfoAV1);
bool InitSequenceHeader(StdVideoAV1SequenceHeader* seqHeader);
virtual EncoderConfigAV1* GetEncoderConfigAV1() override {
return this;
}
bool DetermineLevelTier();
uint32_t GetLevelMaxBitrate() {
return (tier == 0) ? levelLimits[level].mainBps : levelLimits[level].highBps;
}
uint32_t GetLevelBitrate() {
uint32_t bitrateProfileFactor = (profile == STD_VIDEO_AV1_PROFILE_MAIN) ? 1 :
((profile == STD_VIDEO_AV1_PROFILE_HIGH) ? 2 : 3);
uint32_t _maxBitrate = (tier == 0) ? levelLimits[level].mainBps : levelLimits[level].highBps;
return _maxBitrate * bitrateProfileFactor;
}
double GetMinCompressRatio(uint32_t decodeRate) {
double speedAdj = (double)decodeRate / (double)levelLimits[level].maxDisplayRate;
double minCompBasis = (tier == 0) ? levelLimits[level].mainCR : levelLimits[level].highCR;
return std::max(0.8, minCompBasis * speedAdj);
}
uint32_t GetUncompressedSize() {
uint32_t picSizeProfileFactor = (profile == STD_VIDEO_AV1_PROFILE_MAIN) ? 15 :
((profile == STD_VIDEO_AV1_PROFILE_HIGH) ? 30 : 36);
return ((encodeWidth * encodeHeight * picSizeProfileFactor) >> 3);
}
StdVideoAV1Profile profile{ STD_VIDEO_AV1_PROFILE_MAIN };
StdVideoAV1Level level{ STD_VIDEO_AV1_LEVEL_5_0 };
uint8_t tier{};
VkVideoEncodeAV1CapabilitiesKHR av1EncodeCapabilities{ VK_STRUCTURE_TYPE_VIDEO_ENCODE_AV1_CAPABILITIES_KHR };
VkVideoEncodeAV1QuantizationMapCapabilitiesKHR av1QuantizationMapCapabilities { VK_STRUCTURE_TYPE_VIDEO_ENCODE_AV1_QUANTIZATION_MAP_CAPABILITIES_KHR };
uint32_t maxBitrate{};
uint32_t hrdBitrate{};
uint32_t vbvBufferSize{};
uint32_t vbvInitialDelay{};
uint32_t pic_width_in_sbs{};
uint32_t pic_height_in_sbs{};
VkVideoEncodeAV1QIndexKHR minQIndex{};
VkVideoEncodeAV1QIndexKHR maxQIndex{255, 255, 255};
VkVideoEncodeAV1RateControlInfoKHR rcInfoAV1{ VK_STRUCTURE_TYPE_VIDEO_ENCODE_AV1_RATE_CONTROL_INFO_KHR };
VkVideoEncodeAV1RateControlLayerInfoKHR rcLayerInfoAV1{ VK_STRUCTURE_TYPE_VIDEO_ENCODE_AV1_RATE_CONTROL_LAYER_INFO_KHR };
VkVideoEncodeRateControlInfoKHR rcInfo{ VK_STRUCTURE_TYPE_VIDEO_ENCODE_RATE_CONTROL_INFO_KHR };
VkVideoEncodeRateControlLayerInfoKHR rcLayerInfo{ VK_STRUCTURE_TYPE_VIDEO_ENCODE_RATE_CONTROL_LAYER_INFO_KHR };
const LevelLimits* levelLimits;
size_t levelLimitsSize;
bool enableTiles{};
bool customTileConfig{};
StdVideoAV1TileInfo tileConfig{};
uint16_t tileWidthInSbsMinus1[STD_VIDEO_AV1_MAX_TILE_COLS]{};
uint16_t tileHeightInSbsMinus1[STD_VIDEO_AV1_MAX_TILE_ROWS]{};
bool enableQuant{};
bool customQuantConfig{};
StdVideoAV1Quantization quantConfig{};
bool enableLf{};
bool customLfConfig{};
StdVideoAV1LoopFilter lfConfig{};
bool enableCdef{};
bool customCdefConfig{};
StdVideoAV1CDEF cdefConfig{};
bool enableLr{};
bool customLrConfig{};
StdVideoAV1LoopRestoration lrConfig{};
};
#endif /* VKVIDEOENCODER_VKENCODERCONFIG_AV1_H_ */