xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_enc_h264.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "d3d12_video_enc.h"
25 #include "d3d12_video_enc_h264.h"
26 #include "util/u_video.h"
27 #include "d3d12_screen.h"
28 #include "d3d12_format.h"
29 
30 #include <cmath>
31 #include <algorithm>
32 #include <numeric>
33 
34 void
d3d12_video_encoder_update_current_rate_control_h264(struct d3d12_video_encoder * pD3D12Enc,pipe_h264_enc_picture_desc * picture)35 d3d12_video_encoder_update_current_rate_control_h264(struct d3d12_video_encoder *pD3D12Enc,
36                                                      pipe_h264_enc_picture_desc *picture)
37 {
38    struct pipe_h264_enc_picture_desc *h264Pic = (struct pipe_h264_enc_picture_desc *) picture;
39 
40    assert(h264Pic->pic_ctrl.temporal_id < ARRAY_SIZE(pipe_h264_enc_picture_desc::rate_ctrl));
41    assert(h264Pic->pic_ctrl.temporal_id < std::max(1u, pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificSequenceStateDescH264.num_temporal_layers));
42    assert(h264Pic->pic_ctrl.temporal_id < ARRAY_SIZE(D3D12EncodeConfiguration::m_encoderRateControlDesc));
43 
44    struct D3D12EncodeRateControlState m_prevRCState = pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[picture->pic_ctrl.temporal_id];
45    pD3D12Enc->m_currentEncodeConfig.m_activeRateControlIndex = h264Pic->pic_ctrl.temporal_id;
46    pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id] = {};
47    pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_FrameRate.Numerator =
48       picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].frame_rate_num;
49    pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_FrameRate.Denominator =
50       picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].frame_rate_den;
51    pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags = D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_NONE;
52 
53    if (picture->roi.num > 0)
54       pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
55          D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_DELTA_QP;
56 
57    switch (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].rate_ctrl_method) {
58       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:
59       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE:
60       {
61          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Mode = D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_VBR;
62          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.TargetAvgBitRate =
63             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].target_bitrate;
64          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.PeakBitRate =
65             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].peak_bitrate;
66 
67          if (D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE) {
68             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE environment variable is set, "
69                        ", forcing VBV Size = VBV Initial Capacity = Target Bitrate = %" PRIu64 " (bits)\n", pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate);
70             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
71                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
72             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.VBVCapacity =
73                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate;
74             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.InitialVBVFullness =
75                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate;
76          } else if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_hrd_buffer) {
77             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 HRD required by app,"
78                        " setting VBV Size = %d (bits) - VBV Initial Capacity %d (bits)\n", picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size);
79             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
80                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
81             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.VBVCapacity =
82                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size;
83             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.InitialVBVFullness =
84                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size;
85          }
86 
87          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].max_frame_size = picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
88          if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size > 0) {
89             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
90                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE;
91             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.MaxFrameBitSize =
92                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
93 
94             debug_printf(
95                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
96                "Upper layer requested explicit MaxFrameBitSize: %" PRIu64 "\n",
97                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.MaxFrameBitSize);
98          }
99 
100          if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_qp_range) {
101             debug_printf(
102                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
103                "Upper layer requested explicit MinQP: %d MaxQP: %d\n",
104                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp);
105             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
106                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
107             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.MinQP =
108                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp;
109             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR.MaxQP =
110                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp;
111          }
112 
113          if (picture->quality_modes.level > 0) {
114             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
115                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED;
116             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
117                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
118 
119             // Convert between D3D12 definition and PIPE definition
120             // D3D12: QualityVsSpeed must be in the range [0, D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1.MaxQualityVsSpeed]
121             // The lower the value, the fastest the encode operation
122             // PIPE: The quality level range can be queried through the VAConfigAttribEncQualityRange attribute.
123             // A lower value means higher quality, and a value of 1 represents the highest quality.
124             // The quality level setting is used as a trade-off between quality and speed/power
125             // consumption, with higher quality corresponds to lower speed and higher power consumption.
126 
127             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_VBR1.QualityVsSpeed =
128                pD3D12Enc->max_quality_levels - picture->quality_modes.level;
129          }
130 
131       } break;
132       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_QUALITY_VARIABLE:
133       {
134          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Mode = D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_QVBR;
135          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.TargetAvgBitRate =
136             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].target_bitrate;
137          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.PeakBitRate =
138             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].peak_bitrate;
139             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.ConstantQualityTarget =
140             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbr_quality_factor;
141 
142          if (D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE) {
143             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE environment variable is set, "
144                        ", forcing VBV Size = VBV Initial Capacity = Target Bitrate = %" PRIu64 " (bits)\n", pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.TargetAvgBitRate);
145             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
146                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
147             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
148                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
149             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.VBVCapacity =
150                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.TargetAvgBitRate;
151             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.InitialVBVFullness =
152                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.TargetAvgBitRate;
153          } else if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_hrd_buffer) {
154             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 HRD required by app,"
155                        " setting VBV Size = %d (bits) - VBV Initial Capacity %d (bits)\n", picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size);
156             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
157                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
158             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
159                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
160             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.VBVCapacity =
161                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size;
162             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.InitialVBVFullness =
163                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size;
164          }
165       pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].max_frame_size = picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
166       if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size > 0) {
167             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
168                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE;
169             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.MaxFrameBitSize =
170                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
171 
172             debug_printf(
173                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
174                "Upper layer requested explicit MaxFrameBitSize: %" PRIu64 "\n",
175                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.MaxFrameBitSize);
176          }
177 
178          if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_qp_range) {
179             debug_printf(
180                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
181                "Upper layer requested explicit MinQP: %d MaxQP: %d\n",
182                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp);
183             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
184                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
185             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.MinQP =
186                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp;
187             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR.MaxQP =
188                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp;
189          }
190          if (picture->quality_modes.level > 0) {
191             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
192                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED;
193             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
194                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
195 
196             // Convert between D3D12 definition and PIPE definition
197             // D3D12: QualityVsSpeed must be in the range [0, D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1.MaxQualityVsSpeed]
198             // The lower the value, the fastest the encode operation
199             // PIPE: The quality level range can be queried through the VAConfigAttribEncQualityRange attribute.
200             // A lower value means higher quality, and a value of 1 represents the highest quality.
201             // The quality level setting is used as a trade-off between quality and speed/power
202             // consumption, with higher quality corresponds to lower speed and higher power consumption.
203 
204             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_QVBR1.QualityVsSpeed =
205                pD3D12Enc->max_quality_levels - picture->quality_modes.level;
206          }
207       } break;
208       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:
209       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT:
210       {
211          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Mode = D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CBR;
212          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate =
213             picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].target_bitrate;
214 
215          /* For CBR mode, to guarantee bitrate of generated stream complies with
216           * target bitrate (e.g. no over +/-10%), vbv_buffer_size and initial capacity should be same
217           * as target bitrate. Controlled by OS env var D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE
218           */
219          if (D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE) {
220             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE environment variable is set, "
221                        ", forcing VBV Size = VBV Initial Capacity = Target Bitrate = %" PRIu64 " (bits)\n", pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate);
222             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
223                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
224             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.VBVCapacity =
225                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate;
226             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.InitialVBVFullness =
227                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.TargetBitRate;
228          } else if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_hrd_buffer) {
229             debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 HRD required by app,"
230                        " setting VBV Size = %d (bits) - VBV Initial Capacity %d (bits)\n", picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size);
231             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
232                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_VBV_SIZES;
233             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.VBVCapacity =
234                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buffer_size;
235             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.InitialVBVFullness =
236                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].vbv_buf_initial_size;
237          }
238 
239          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].max_frame_size = picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
240          if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size > 0) {
241             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
242                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_MAX_FRAME_SIZE;
243             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.MaxFrameBitSize =
244                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_au_size;
245 
246             debug_printf(
247                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
248                "Upper layer requested explicit MaxFrameBitSize: %" PRIu64 "\n",
249                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.MaxFrameBitSize);
250          }
251 
252          if (picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].app_requested_qp_range) {
253             debug_printf(
254                "[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 "
255                "Upper layer requested explicit MinQP: %d MaxQP: %d\n",
256                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp, picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp);
257 
258             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
259                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QP_RANGE;
260             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.MinQP =
261                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].min_qp;
262             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR.MaxQP =
263                picture->rate_ctrl[h264Pic->pic_ctrl.temporal_id].max_qp;
264          }
265 
266          if (picture->quality_modes.level > 0) {
267             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
268                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED;
269             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
270                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
271 
272             // Convert between D3D12 definition and PIPE definition
273             // D3D12: QualityVsSpeed must be in the range [0, D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1.MaxQualityVsSpeed]
274             // The lower the value, the fastest the encode operation
275             // PIPE: The quality level range can be queried through the VAConfigAttribEncQualityRange attribute.
276             // A lower value means higher quality, and a value of 1 represents the highest quality.
277             // The quality level setting is used as a trade-off between quality and speed/power
278             // consumption, with higher quality corresponds to lower speed and higher power consumption.
279 
280             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CBR1.QualityVsSpeed =
281                pD3D12Enc->max_quality_levels - picture->quality_modes.level;
282          }
283       } break;
284       case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE:
285       {
286          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Mode = D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP;
287 
288          // Load previous RC state for all frames and only update the current frame
289          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP =
290                   m_prevRCState.m_Config.m_Configuration_CQP;
291          switch (picture->picture_type) {
292             case PIPE_H2645_ENC_PICTURE_TYPE_P:
293             {
294                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
295                   .ConstantQP_InterPredictedFrame_PrevRefOnly = picture->quant_p_frames;
296             } break;
297             case PIPE_H2645_ENC_PICTURE_TYPE_B:
298             {
299                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
300                   .ConstantQP_InterPredictedFrame_BiDirectionalRef = picture->quant_b_frames;
301             } break;
302             case PIPE_H2645_ENC_PICTURE_TYPE_I:
303             case PIPE_H2645_ENC_PICTURE_TYPE_IDR:
304             {
305                pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
306                   .ConstantQP_FullIntracodedFrame = picture->quant_i_frames;
307             } break;
308             default:
309             {
310                unreachable("Unsupported pipe_h2645_enc_picture_type");
311             } break;
312          }
313 
314          if (picture->quality_modes.level > 0) {
315             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
316                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_QUALITY_VS_SPEED;
317             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags |=
318                D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_EXTENSION1_SUPPORT;
319 
320             // Convert between D3D12 definition and PIPE definition
321             // D3D12: QualityVsSpeed must be in the range [0, D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1.MaxQualityVsSpeed]
322             // The lower the value, the fastest the encode operation
323             // PIPE: The quality level range can be queried through the VAConfigAttribEncQualityRange attribute.
324             // A lower value means higher quality, and a value of 1 represents the highest quality.
325             // The quality level setting is used as a trade-off between quality and speed/power
326             // consumption, with higher quality corresponds to lower speed and higher power consumption.
327 
328             pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP1.QualityVsSpeed =
329                pD3D12Enc->max_quality_levels - picture->quality_modes.level;
330          }
331       } break;
332       default:
333       {
334          debug_printf("[d3d12_video_encoder_h264] d3d12_video_encoder_update_current_rate_control_h264 invalid RC "
335                        "config, using default RC CQP mode\n");
336          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Mode = D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP;
337          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
338             .ConstantQP_FullIntracodedFrame = 30;
339          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
340             .ConstantQP_InterPredictedFrame_PrevRefOnly = 30;
341          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Config.m_Configuration_CQP
342             .ConstantQP_InterPredictedFrame_BiDirectionalRef = 30;
343       } break;
344    }
345 }
346 
347 void
d3d12_video_encoder_update_current_frame_pic_params_info_h264(struct d3d12_video_encoder * pD3D12Enc,struct pipe_video_buffer * srcTexture,struct pipe_picture_desc * picture,D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA & picParams,bool & bUsedAsReference)348 d3d12_video_encoder_update_current_frame_pic_params_info_h264(struct d3d12_video_encoder *pD3D12Enc,
349                                                               struct pipe_video_buffer *srcTexture,
350                                                               struct pipe_picture_desc *picture,
351                                                               D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA &picParams,
352                                                               bool &bUsedAsReference)
353 {
354    struct pipe_h264_enc_picture_desc *h264Pic = (struct pipe_h264_enc_picture_desc *) picture;
355    d3d12_video_bitstream_builder_h264 *pH264BitstreamBuilder =
356       static_cast<d3d12_video_bitstream_builder_h264 *>(pD3D12Enc->m_upBitstreamBuilder.get());
357    assert(pH264BitstreamBuilder != nullptr);
358 
359    bUsedAsReference = !h264Pic->not_referenced;
360 
361    if (pD3D12Enc->m_currentEncodeCapabilities.m_encoderCodecSpecificConfigCaps.m_H264CodecCaps.SupportFlags &
362        D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE_FLAG_SLICE_SUPPORT)
363    {
364       picParams.pH264PicData->Flags |= D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_H264_FLAG_REQUEST_NUM_REF_IDX_ACTIVE_OVERRIDE_FLAG_SLICE;
365    }
366 
367    //
368    // These need to be set here so they're available for SPS/PPS header building (reference manager updates after that, for slice header params)
369    //
370    picParams.pH264PicData->pic_parameter_set_id = pH264BitstreamBuilder->get_active_pps().pic_parameter_set_id;
371    picParams.pH264PicData->List0ReferenceFramesCount = 0;
372    picParams.pH264PicData->List1ReferenceFramesCount = 0;
373    if ((h264Pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P) ||
374        (h264Pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B))
375       picParams.pH264PicData->List0ReferenceFramesCount = h264Pic->num_ref_idx_l0_active_minus1 + 1;
376 
377    if (h264Pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B)
378       picParams.pH264PicData->List1ReferenceFramesCount = h264Pic->num_ref_idx_l1_active_minus1 + 1;
379 
380    if ((pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_Flags & D3D12_VIDEO_ENCODER_RATE_CONTROL_FLAG_ENABLE_DELTA_QP) != 0)
381    {
382       // Use 8 bit qpmap array for H264 picparams (-51, 51 range and int8_t pRateControlQPMap type)
383       const int32_t h264_min_delta_qp = -51;
384       const int32_t h264_max_delta_qp = 51;
385       d3d12_video_encoder_update_picparams_region_of_interest_qpmap(
386          pD3D12Enc,
387          &h264Pic->roi,
388          h264_min_delta_qp,
389          h264_max_delta_qp,
390          pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_pRateControlQPMap8Bit);
391       picParams.pH264PicData->pRateControlQPMap = pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_pRateControlQPMap8Bit.data();
392       picParams.pH264PicData->QPMapValuesCount = pD3D12Enc->m_currentEncodeConfig.m_encoderRateControlDesc[h264Pic->pic_ctrl.temporal_id].m_pRateControlQPMap8Bit.size();
393    }
394 }
395 
396 ///
397 /// Tries to configurate the encoder using the requested slice configuration
398 /// or falls back to single slice encoding.
399 ///
400 bool
d3d12_video_encoder_negotiate_current_h264_slices_configuration(struct d3d12_video_encoder * pD3D12Enc,pipe_h264_enc_picture_desc * picture)401 d3d12_video_encoder_negotiate_current_h264_slices_configuration(struct d3d12_video_encoder *pD3D12Enc,
402                                                                 pipe_h264_enc_picture_desc *picture)
403 {
404    ///
405    /// Initialize single slice by default
406    ///
407    D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE requestedSlicesMode =
408       D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME;
409    D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES requestedSlicesConfig = {};
410    requestedSlicesConfig.NumberOfSlicesPerFrame = 1;
411 
412    ///
413    /// Try to see if can accomodate for multi-slice request by user
414    ///
415    if ((picture->slice_mode == PIPE_VIDEO_SLICE_MODE_BLOCKS) && (picture->num_slice_descriptors > 1)) {
416       /* Some apps send all same size slices minus 1 slice in any position in the descriptors */
417       /* Lets validate that there are at most 2 different slice sizes in all the descriptors */
418       std::vector<int> slice_sizes(picture->num_slice_descriptors);
419       for (uint32_t i = 0; i < picture->num_slice_descriptors; i++)
420          slice_sizes[i] = picture->slices_descriptors[i].num_macroblocks;
421       std::sort(slice_sizes.begin(), slice_sizes.end());
422       bool bUniformSizeSlices = (std::unique(slice_sizes.begin(), slice_sizes.end()) - slice_sizes.begin()) <= 2;
423 
424       uint32_t mbPerScanline =
425          pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Width / D3D12_VIDEO_H264_MB_IN_PIXELS;
426       bool bSliceAligned = ((picture->slices_descriptors[0].num_macroblocks % mbPerScanline) == 0);
427 
428       if (bUniformSizeSlices) {
429          if (picture->intra_refresh.mode != INTRA_REFRESH_MODE_NONE) {
430             /*
431             * When intra-refresh is active, we must use D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME
432             */
433             if (d3d12_video_encoder_check_subregion_mode_support(
434                      pD3D12Enc,
435                      D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME)) {
436                requestedSlicesMode =
437                   D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME;
438                requestedSlicesConfig.NumberOfSlicesPerFrame = picture->num_slice_descriptors;
439                debug_printf("[d3d12_video_encoder_h264] Intra-refresh is active and per DX12 spec it requires using multi slice encoding mode: "
440                               "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME "
441                               "with %d slices per frame.\n",
442                               requestedSlicesConfig.NumberOfSlicesPerFrame);
443             } else {
444                debug_printf("[d3d12_video_encoder_h264] Intra-refresh is active which requires "
445                               "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME "
446                               "mode but there is HW support for such mode.\n");
447                return false;
448             }
449          } else if (bSliceAligned &&
450                   d3d12_video_encoder_check_subregion_mode_support(
451                      pD3D12Enc,
452                      D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_ROWS_PER_SUBREGION)) {
453 
454             // Number of macroblocks per slice is aligned to a scanline width, in which case we can
455             // use D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_ROWS_PER_SUBREGION
456             requestedSlicesMode = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_ROWS_PER_SUBREGION;
457             requestedSlicesConfig.NumberOfRowsPerSlice = (picture->slices_descriptors[0].num_macroblocks / mbPerScanline);
458             debug_printf("[d3d12_video_encoder_h264] Using multi slice encoding mode: "
459                            "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_ROWS_PER_SUBREGION with "
460                            "%d macroblocks rows per slice.\n",
461                            requestedSlicesConfig.NumberOfRowsPerSlice);
462          } else if (d3d12_video_encoder_check_subregion_mode_support(
463                      pD3D12Enc,
464                      D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME)) {
465                requestedSlicesMode =
466                   D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME;
467                requestedSlicesConfig.NumberOfSlicesPerFrame = picture->num_slice_descriptors;
468                debug_printf("[d3d12_video_encoder_h264] Using multi slice encoding mode: "
469                               "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME "
470                               "with %d slices per frame.\n",
471                               requestedSlicesConfig.NumberOfSlicesPerFrame);
472          } else if (d3d12_video_encoder_check_subregion_mode_support(
473                      pD3D12Enc,
474                      D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_SQUARE_UNITS_PER_SUBREGION_ROW_UNALIGNED)) {
475                requestedSlicesMode =
476                   D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_SQUARE_UNITS_PER_SUBREGION_ROW_UNALIGNED;
477                requestedSlicesConfig.NumberOfCodingUnitsPerSlice = picture->slices_descriptors[0].num_macroblocks;
478                debug_printf("[d3d12_video_encoder_h264] Using multi slice encoding mode: "
479                               "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_SQUARE_UNITS_PER_SUBREGION_ROW_UNALIGNED "
480                               "with %d NumberOfCodingUnitsPerSlice per frame.\n",
481                               requestedSlicesConfig.NumberOfCodingUnitsPerSlice);
482          } else {
483             debug_printf("[d3d12_video_encoder_h264] Requested slice control mode is not supported by hardware: No HW support for "
484                            "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_ROWS_PER_SUBREGION or"
485                            "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_UNIFORM_PARTITIONING_SUBREGIONS_PER_FRAME or"
486                            "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_SQUARE_UNITS_PER_SUBREGION_ROW_UNALIGNED.\n");
487             return false;
488          }
489       } else {
490          debug_printf("[d3d12_video_encoder_h264] Requested slice control mode is not supported: All slices must "
491                          "have the same number of macroblocks.\n");
492          return false;
493       }
494    } else if(picture->slice_mode == PIPE_VIDEO_SLICE_MODE_MAX_SLICE_SIZE) {
495       if ((picture->max_slice_bytes > 0) &&
496                  d3d12_video_encoder_check_subregion_mode_support(
497                     pD3D12Enc,
498                     D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_BYTES_PER_SUBREGION )) {
499             requestedSlicesMode =
500                D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_BYTES_PER_SUBREGION;
501             requestedSlicesConfig.MaxBytesPerSlice = picture->max_slice_bytes;
502             debug_printf("[d3d12_video_encoder_h264] Using multi slice encoding mode: "
503                            "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_BYTES_PER_SUBREGION  "
504                            "with %d MaxBytesPerSlice per frame.\n",
505                            requestedSlicesConfig.MaxBytesPerSlice);
506       } else {
507          debug_printf("[d3d12_video_encoder_h264] Requested slice control mode is not supported: No HW support for "
508                          "D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_BYTES_PER_SUBREGION.\n");
509          return false;
510       }
511    } else {
512       requestedSlicesMode = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME;
513       requestedSlicesConfig.NumberOfSlicesPerFrame = 1;
514       debug_printf("[d3d12_video_encoder_h264] Requested slice control mode is full frame. m_SlicesPartition_H264.NumberOfSlicesPerFrame = %d - m_encoderSliceConfigMode = %d \n",
515       requestedSlicesConfig.NumberOfSlicesPerFrame, requestedSlicesMode);
516    }
517 
518    if (!d3d12_video_encoder_compare_slice_config_h264_hevc(
519           pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigMode,
520           pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigDesc.m_SlicesPartition_H264,
521           requestedSlicesMode,
522           requestedSlicesConfig)) {
523       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_slices;
524    }
525 
526    pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigDesc.m_SlicesPartition_H264 = requestedSlicesConfig;
527    pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigMode = requestedSlicesMode;
528 
529    return true;
530 }
531 
532 D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE
d3d12_video_encoder_convert_h264_motion_configuration(struct d3d12_video_encoder * pD3D12Enc,pipe_h264_enc_picture_desc * picture)533 d3d12_video_encoder_convert_h264_motion_configuration(struct d3d12_video_encoder *pD3D12Enc,
534                                                       pipe_h264_enc_picture_desc *picture)
535 {
536    return D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM;
537 }
538 
539 D3D12_VIDEO_ENCODER_LEVELS_H264
d3d12_video_encoder_convert_level_h264(uint32_t h264SpecLevel)540 d3d12_video_encoder_convert_level_h264(uint32_t h264SpecLevel)
541 {
542    switch (h264SpecLevel) {
543       case 10:
544       {
545          return D3D12_VIDEO_ENCODER_LEVELS_H264_1;
546       } break;
547       case 11:
548       {
549          return D3D12_VIDEO_ENCODER_LEVELS_H264_11;
550       } break;
551       case 12:
552       {
553          return D3D12_VIDEO_ENCODER_LEVELS_H264_12;
554       } break;
555       case 13:
556       {
557          return D3D12_VIDEO_ENCODER_LEVELS_H264_13;
558       } break;
559       case 20:
560       {
561          return D3D12_VIDEO_ENCODER_LEVELS_H264_2;
562       } break;
563       case 21:
564       {
565          return D3D12_VIDEO_ENCODER_LEVELS_H264_21;
566       } break;
567       case 22:
568       {
569          return D3D12_VIDEO_ENCODER_LEVELS_H264_22;
570       } break;
571       case 30:
572       {
573          return D3D12_VIDEO_ENCODER_LEVELS_H264_3;
574       } break;
575       case 31:
576       {
577          return D3D12_VIDEO_ENCODER_LEVELS_H264_31;
578       } break;
579       case 32:
580       {
581          return D3D12_VIDEO_ENCODER_LEVELS_H264_32;
582       } break;
583       case 40:
584       {
585          return D3D12_VIDEO_ENCODER_LEVELS_H264_4;
586       } break;
587       case 41:
588       {
589          return D3D12_VIDEO_ENCODER_LEVELS_H264_41;
590       } break;
591       case 42:
592       {
593          return D3D12_VIDEO_ENCODER_LEVELS_H264_42;
594       } break;
595       case 50:
596       {
597          return D3D12_VIDEO_ENCODER_LEVELS_H264_5;
598       } break;
599       case 51:
600       {
601          return D3D12_VIDEO_ENCODER_LEVELS_H264_51;
602       } break;
603       case 52:
604       {
605          return D3D12_VIDEO_ENCODER_LEVELS_H264_52;
606       } break;
607       case 60:
608       {
609          return D3D12_VIDEO_ENCODER_LEVELS_H264_6;
610       } break;
611       case 61:
612       {
613          return D3D12_VIDEO_ENCODER_LEVELS_H264_61;
614       } break;
615       case 62:
616       {
617          return D3D12_VIDEO_ENCODER_LEVELS_H264_62;
618       } break;
619       default:
620       {
621          unreachable("Unsupported H264 level");
622       } break;
623    }
624 }
625 
626 void
d3d12_video_encoder_convert_from_d3d12_level_h264(D3D12_VIDEO_ENCODER_LEVELS_H264 level12,uint32_t & specLevel)627 d3d12_video_encoder_convert_from_d3d12_level_h264(D3D12_VIDEO_ENCODER_LEVELS_H264 level12,
628                                                   uint32_t &specLevel)
629 {
630    specLevel = 0;
631 
632    switch (level12) {
633       case D3D12_VIDEO_ENCODER_LEVELS_H264_1:
634       {
635          specLevel = 10;
636       } break;
637       case D3D12_VIDEO_ENCODER_LEVELS_H264_1b:
638       {
639          specLevel = 11;
640       } break;
641       case D3D12_VIDEO_ENCODER_LEVELS_H264_11:
642       {
643          specLevel = 11;
644       } break;
645       case D3D12_VIDEO_ENCODER_LEVELS_H264_12:
646       {
647          specLevel = 12;
648       } break;
649       case D3D12_VIDEO_ENCODER_LEVELS_H264_13:
650       {
651          specLevel = 13;
652       } break;
653       case D3D12_VIDEO_ENCODER_LEVELS_H264_2:
654       {
655          specLevel = 20;
656       } break;
657       case D3D12_VIDEO_ENCODER_LEVELS_H264_21:
658       {
659          specLevel = 21;
660       } break;
661       case D3D12_VIDEO_ENCODER_LEVELS_H264_22:
662       {
663          specLevel = 22;
664       } break;
665       case D3D12_VIDEO_ENCODER_LEVELS_H264_3:
666       {
667          specLevel = 30;
668       } break;
669       case D3D12_VIDEO_ENCODER_LEVELS_H264_31:
670       {
671          specLevel = 31;
672       } break;
673       case D3D12_VIDEO_ENCODER_LEVELS_H264_32:
674       {
675          specLevel = 32;
676       } break;
677       case D3D12_VIDEO_ENCODER_LEVELS_H264_4:
678       {
679          specLevel = 40;
680       } break;
681       case D3D12_VIDEO_ENCODER_LEVELS_H264_41:
682       {
683          specLevel = 41;
684       } break;
685       case D3D12_VIDEO_ENCODER_LEVELS_H264_42:
686       {
687          specLevel = 42;
688       } break;
689       case D3D12_VIDEO_ENCODER_LEVELS_H264_5:
690       {
691          specLevel = 50;
692       } break;
693       case D3D12_VIDEO_ENCODER_LEVELS_H264_51:
694       {
695          specLevel = 51;
696       } break;
697       case D3D12_VIDEO_ENCODER_LEVELS_H264_52:
698       {
699          specLevel = 52;
700       } break;
701       case D3D12_VIDEO_ENCODER_LEVELS_H264_6:
702       {
703          specLevel = 60;
704       } break;
705       case D3D12_VIDEO_ENCODER_LEVELS_H264_61:
706       {
707          specLevel = 61;
708       } break;
709       case D3D12_VIDEO_ENCODER_LEVELS_H264_62:
710       {
711          specLevel = 62;
712       } break;
713       default:
714       {
715          unreachable("Unsupported D3D12_VIDEO_ENCODER_LEVELS_H264 value");
716       } break;
717    }
718 }
719 
720 bool
d3d12_video_encoder_update_h264_gop_configuration(struct d3d12_video_encoder * pD3D12Enc,pipe_h264_enc_picture_desc * picture)721 d3d12_video_encoder_update_h264_gop_configuration(struct d3d12_video_encoder *pD3D12Enc,
722                                                   pipe_h264_enc_picture_desc *picture)
723 {
724    // Only update GOP when it begins
725    // Only update GOP when it begins
726    // This triggers DPB/encoder/heap re-creation, so only check on IDR when a GOP might change
727    if ((picture->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
728       || (picture->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_I)) {
729       uint32_t GOPLength = picture->intra_idr_period;
730       uint32_t PPicturePeriod = picture->ip_period;
731 
732       if (picture->seq.pic_order_cnt_type == 1u) {
733          debug_printf("[d3d12_video_encoder_h264] Upper layer is requesting pic_order_cnt_type %d but D3D12 Video "
734                          "only supports pic_order_cnt_type = 0 or pic_order_cnt_type = 2\n",
735                          picture->seq.pic_order_cnt_type);
736          return false;
737       }
738 
739       // Workaround: D3D12 needs to use the POC in the DPB to track reference frames
740       // even when there's no frame reordering (picture->seq.pic_order_cnt_type == 2)
741       // So in that case, derive an artificial log2_max_pic_order_cnt_lsb_minus4
742       // to avoid unexpected wrapping
743       if (picture->seq.pic_order_cnt_type == 2u) {
744          if (GOPLength == 0) // Use max frame num to wrap on infinite GOPs
745             GOPLength = 1 << (picture->seq.log2_max_frame_num_minus4 + 4);
746          const uint32_t max_pic_order_cnt_lsb = 2 * GOPLength;
747          picture->seq.log2_max_pic_order_cnt_lsb_minus4 = std::max(0.0, std::ceil(std::log2(max_pic_order_cnt_lsb)) - 4);
748          assert(picture->seq.log2_max_pic_order_cnt_lsb_minus4 < UCHAR_MAX);
749       }
750 
751       assert(picture->seq.pic_order_cnt_type < UCHAR_MAX);
752 
753       // Set dirty flag if m_H264GroupOfPictures changed
754       auto previousGOPConfig = pD3D12Enc->m_currentEncodeConfig.m_encoderGOPConfigDesc.m_H264GroupOfPictures;
755       pD3D12Enc->m_currentEncodeConfig.m_encoderGOPConfigDesc.m_H264GroupOfPictures = {
756          GOPLength,
757          PPicturePeriod,
758          static_cast<uint8_t>(picture->seq.pic_order_cnt_type),
759          static_cast<uint8_t>(picture->seq.log2_max_frame_num_minus4),
760          static_cast<uint8_t>(picture->seq.log2_max_pic_order_cnt_lsb_minus4)
761       };
762 
763       if (memcmp(&previousGOPConfig,
764                  &pD3D12Enc->m_currentEncodeConfig.m_encoderGOPConfigDesc.m_H264GroupOfPictures,
765                  sizeof(D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_H264)) != 0) {
766          pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_gop;
767       }
768    }
769    return true;
770 }
771 
772 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264
d3d12_video_encoder_convert_h264_codec_configuration(struct d3d12_video_encoder * pD3D12Enc,pipe_h264_enc_picture_desc * picture,bool & is_supported)773 d3d12_video_encoder_convert_h264_codec_configuration(struct d3d12_video_encoder *pD3D12Enc,
774                                                      pipe_h264_enc_picture_desc *picture,
775                                                      bool &is_supported)
776 {
777    is_supported = true;
778    D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264 config = {
779       D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_NONE,
780       D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_DIRECT_MODES_DISABLED,
781       // Definition of D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODES matches disable_deblocking_filter_idc syntax
782       static_cast<D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODES>(picture->dbk.disable_deblocking_filter_idc),
783    };
784 
785    if (picture->pic_ctrl.enc_cabac_enable) {
786       config.ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_ENABLE_CABAC_ENCODING;
787    }
788 
789    if (picture->pic_ctrl.constrained_intra_pred_flag) {
790       config.ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_CONSTRAINED_INTRAPREDICTION;
791    }
792 
793    if (picture->pic_ctrl.transform_8x8_mode_flag) {
794       config.ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_ADAPTIVE_8x8_TRANSFORM;
795    }
796 
797    pD3D12Enc->m_currentEncodeCapabilities.m_encoderCodecSpecificConfigCaps.m_H264CodecCaps =
798    {
799       D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_NONE,
800       D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODE_FLAG_NONE
801    };
802 
803    D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT capCodecConfigData = { };
804    capCodecConfigData.NodeIndex = pD3D12Enc->m_NodeIndex;
805    capCodecConfigData.Codec = D3D12_VIDEO_ENCODER_CODEC_H264;
806    D3D12_VIDEO_ENCODER_PROFILE_H264 prof = d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_h264(pD3D12Enc->base.profile);
807    capCodecConfigData.Profile.pH264Profile = &prof;
808    capCodecConfigData.Profile.DataSize = sizeof(prof);
809    capCodecConfigData.CodecSupportLimits.pH264Support = &pD3D12Enc->m_currentEncodeCapabilities.m_encoderCodecSpecificConfigCaps.m_H264CodecCaps;
810    capCodecConfigData.CodecSupportLimits.DataSize = sizeof(pD3D12Enc->m_currentEncodeCapabilities.m_encoderCodecSpecificConfigCaps.m_H264CodecCaps);
811 
812    if(FAILED(pD3D12Enc->m_spD3D12VideoDevice->CheckFeatureSupport(D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT, &capCodecConfigData, sizeof(capCodecConfigData)))
813       || !capCodecConfigData.IsSupported)
814    {
815          debug_printf("D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT call failed.");
816          is_supported = false;
817          return config;
818    }
819 
820    if(((1 << config.DisableDeblockingFilterConfig) & capCodecConfigData.CodecSupportLimits.pH264Support->DisableDeblockingFilterSupportedModes) == 0)
821    {
822          debug_printf("D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION arguments not supported - DisableDeblockingFilterConfig (value %d) "
823                   "not allowed by DisableDeblockingFilterSupportedModes 0x%x cap reporting.",
824                   config.DisableDeblockingFilterConfig,
825                   capCodecConfigData.CodecSupportLimits.pH264Support->DisableDeblockingFilterSupportedModes);
826          is_supported = false;
827          return config;
828    }
829 
830    if(((config.ConfigurationFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_ENABLE_CABAC_ENCODING) != 0)
831       && ((capCodecConfigData.CodecSupportLimits.pH264Support->SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_CABAC_ENCODING_SUPPORT) == 0))
832    {
833       debug_printf("D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION arguments are not supported - CABAC encoding mode not supported."
834          " Ignoring the request for this feature flag on this encode session");
835          // Disable it and keep going with a warning
836          config.ConfigurationFlags &= ~D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_ENABLE_CABAC_ENCODING;
837    }
838 
839    if(((config.ConfigurationFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_CONSTRAINED_INTRAPREDICTION) != 0)
840       && ((capCodecConfigData.CodecSupportLimits.pH264Support->SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_CONSTRAINED_INTRAPREDICTION_SUPPORT) == 0))
841    {
842       debug_printf("D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION arguments are not supported - constrained_intra_pred_flag not supported."
843          " Ignoring the request for this feature flag on this encode session");
844          // Disable it and keep going with a warning
845          config.ConfigurationFlags &= ~D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_CONSTRAINED_INTRAPREDICTION;
846    }
847 
848    if(((config.ConfigurationFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_ADAPTIVE_8x8_TRANSFORM) != 0)
849       && ((capCodecConfigData.CodecSupportLimits.pH264Support->SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_ADAPTIVE_8x8_TRANSFORM_ENCODING_SUPPORT) == 0))
850    {
851       debug_printf("D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION arguments are not supported - transform_8x8_mode_flag not supported."
852          " Ignoring the request for this feature flag on this encode session");
853          // Disable it and keep going with a warning
854          config.ConfigurationFlags &= ~D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_ADAPTIVE_8x8_TRANSFORM;
855    }
856 
857    return config;
858 }
859 
860 static bool
d3d12_video_encoder_update_intra_refresh_h264(struct d3d12_video_encoder * pD3D12Enc,D3D12_VIDEO_SAMPLE srcTextureDesc,struct pipe_h264_enc_picture_desc * picture)861 d3d12_video_encoder_update_intra_refresh_h264(struct d3d12_video_encoder *pD3D12Enc,
862                                                         D3D12_VIDEO_SAMPLE srcTextureDesc,
863                                                         struct pipe_h264_enc_picture_desc *  picture)
864 {
865    if (picture->intra_refresh.mode != INTRA_REFRESH_MODE_NONE)
866    {
867       // D3D12 only supports row intra-refresh
868       if (picture->intra_refresh.mode != INTRA_REFRESH_MODE_UNIT_ROWS)
869       {
870          debug_printf("[d3d12_video_encoder_update_intra_refresh_h264] Unsupported INTRA_REFRESH_MODE %d\n", picture->intra_refresh.mode);
871          return false;
872       }
873 
874       uint32_t total_frame_blocks = static_cast<uint32_t>(std::ceil(srcTextureDesc.Height / D3D12_VIDEO_H264_MB_IN_PIXELS)) *
875                               static_cast<uint32_t>(std::ceil(srcTextureDesc.Width / D3D12_VIDEO_H264_MB_IN_PIXELS));
876       D3D12_VIDEO_ENCODER_INTRA_REFRESH targetIntraRefresh = {
877          D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_ROW_BASED,
878          total_frame_blocks / picture->intra_refresh.region_size,
879       };
880       double ir_wave_progress = (picture->intra_refresh.offset == 0) ? 0 :
881          picture->intra_refresh.offset / (double) total_frame_blocks;
882       pD3D12Enc->m_currentEncodeConfig.m_IntraRefreshCurrentFrameIndex =
883          static_cast<uint32_t>(std::ceil(ir_wave_progress * targetIntraRefresh.IntraRefreshDuration));
884 
885       // Set intra refresh state
886       pD3D12Enc->m_currentEncodeConfig.m_IntraRefresh = targetIntraRefresh;
887       // Need to send the sequence flag during all the IR duration
888       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_intra_refresh;
889    } else {
890       pD3D12Enc->m_currentEncodeConfig.m_IntraRefreshCurrentFrameIndex = 0;
891       pD3D12Enc->m_currentEncodeConfig.m_IntraRefresh = {
892          D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE,
893          0,
894       };
895    }
896 
897    return true;
898 }
899 
900 bool
d3d12_video_encoder_update_current_encoder_config_state_h264(struct d3d12_video_encoder * pD3D12Enc,D3D12_VIDEO_SAMPLE srcTextureDesc,struct pipe_picture_desc * picture)901 d3d12_video_encoder_update_current_encoder_config_state_h264(struct d3d12_video_encoder *pD3D12Enc,
902                                                              D3D12_VIDEO_SAMPLE srcTextureDesc,
903                                                              struct pipe_picture_desc *picture)
904 {
905    struct pipe_h264_enc_picture_desc *h264Pic = (struct pipe_h264_enc_picture_desc *) picture;
906 
907    // Reset reconfig dirty flags
908    pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags = d3d12_video_encoder_config_dirty_flag_none;
909    // Reset sequence changes flags
910    pD3D12Enc->m_currentEncodeConfig.m_seqFlags = D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_NONE;
911 
912    // Set codec
913    if (pD3D12Enc->m_currentEncodeConfig.m_encoderCodecDesc != D3D12_VIDEO_ENCODER_CODEC_H264) {
914       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_codec;
915    }
916    pD3D12Enc->m_currentEncodeConfig.m_encoderCodecDesc = D3D12_VIDEO_ENCODER_CODEC_H264;
917 
918    // Set Sequence information
919    if (memcmp(&pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificSequenceStateDescH264,
920               &h264Pic->seq,
921               sizeof(h264Pic->seq)) != 0) {
922       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_sequence_header;
923    }
924    pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificSequenceStateDescH264 = h264Pic->seq;
925 
926    // Iterate over the headers the app requested and set flags to emit those for this frame
927    util_dynarray_foreach(&h264Pic->raw_headers, struct pipe_enc_raw_header, header) {
928       if (header->type == PIPE_H264_NAL_SPS)
929          pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_sequence_header;
930       else if (header->type == PIPE_H264_NAL_PPS)
931          pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_picture_header;
932       else if (header->type == PIPE_H264_NAL_AUD)
933          pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_aud_header;
934    }
935 
936    // Set input format
937    DXGI_FORMAT targetFmt = d3d12_convert_pipe_video_profile_to_dxgi_format(pD3D12Enc->base.profile);
938    if (pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo.Format != targetFmt) {
939       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_input_format;
940    }
941 
942    pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo = {};
943    pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo.Format = targetFmt;
944    HRESULT hr = pD3D12Enc->m_pD3D12Screen->dev->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO,
945                                                           &pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo,
946                                                           sizeof(pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo));
947    if (FAILED(hr)) {
948       debug_printf("CheckFeatureSupport failed with HR %x\n", hr);
949       return false;
950    }
951 
952    // Set intra-refresh config
953    if(!d3d12_video_encoder_update_intra_refresh_h264(pD3D12Enc, srcTextureDesc, h264Pic)) {
954       debug_printf("d3d12_video_encoder_update_intra_refresh_h264 failed!\n");
955       return false;
956    }
957 
958    // Set resolution
959    if ((pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Width != srcTextureDesc.Width) ||
960        (pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Height != srcTextureDesc.Height)) {
961       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_resolution;
962    }
963    pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Width = srcTextureDesc.Width;
964    pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Height = srcTextureDesc.Height;
965 
966    // Set resolution codec dimensions (ie. cropping)
967    if (h264Pic->seq.enc_frame_cropping_flag) {
968       pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig.left = h264Pic->seq.enc_frame_crop_left_offset;
969       pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig.right = h264Pic->seq.enc_frame_crop_right_offset;
970       pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig.top = h264Pic->seq.enc_frame_crop_top_offset;
971       pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig.bottom =
972          h264Pic->seq.enc_frame_crop_bottom_offset;
973    } else {
974       memset(&pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig,
975              0,
976              sizeof(pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig));
977    }
978 
979    // Set profile
980    auto targetProfile = d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_h264(pD3D12Enc->base.profile);
981    if (pD3D12Enc->m_currentEncodeConfig.m_encoderProfileDesc.m_H264Profile != targetProfile) {
982       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_profile;
983    }
984    pD3D12Enc->m_currentEncodeConfig.m_encoderProfileDesc.m_H264Profile = targetProfile;
985 
986    // Set level
987    auto targetLevel = d3d12_video_encoder_convert_level_h264(h264Pic->seq.level_idc);
988    if (pD3D12Enc->m_currentEncodeConfig.m_encoderLevelDesc.m_H264LevelSetting != targetLevel) {
989       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_level;
990    }
991    pD3D12Enc->m_currentEncodeConfig.m_encoderLevelDesc.m_H264LevelSetting = targetLevel;
992 
993    // Set codec config
994    bool is_supported = false;
995    auto targetCodecConfig = d3d12_video_encoder_convert_h264_codec_configuration(pD3D12Enc, h264Pic, is_supported);
996    if (!is_supported) {
997       return false;
998    }
999 
1000    if (memcmp(&pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificConfigDesc.m_H264Config,
1001               &targetCodecConfig,
1002               sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264)) != 0) {
1003       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |= d3d12_video_encoder_config_dirty_flag_codec_config;
1004    }
1005    pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificConfigDesc.m_H264Config = targetCodecConfig;
1006 
1007    // Set rate control
1008    d3d12_video_encoder_update_current_rate_control_h264(pD3D12Enc, h264Pic);
1009 
1010    // Set slices config
1011    if(!d3d12_video_encoder_negotiate_current_h264_slices_configuration(pD3D12Enc, h264Pic)) {
1012       debug_printf("d3d12_video_encoder_negotiate_current_h264_slices_configuration failed!\n");
1013       return false;
1014    }
1015 
1016    // Set GOP config
1017    if(!d3d12_video_encoder_update_h264_gop_configuration(pD3D12Enc, h264Pic)) {
1018       debug_printf("d3d12_video_encoder_update_h264_gop_configuration failed!\n");
1019       return false;
1020    }
1021 
1022    // m_currentEncodeConfig.m_encoderPicParamsDesc pic params are set in d3d12_video_encoder_reconfigure_encoder_objects
1023    // after re-allocating objects if needed
1024 
1025    // Set motion estimation config
1026    auto targetMotionLimit = d3d12_video_encoder_convert_h264_motion_configuration(pD3D12Enc, h264Pic);
1027    if (pD3D12Enc->m_currentEncodeConfig.m_encoderMotionPrecisionLimit != targetMotionLimit) {
1028       pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags |=
1029          d3d12_video_encoder_config_dirty_flag_motion_precision_limit;
1030    }
1031    pD3D12Enc->m_currentEncodeConfig.m_encoderMotionPrecisionLimit = targetMotionLimit;
1032 
1033    ///
1034    /// Check for video encode support detailed capabilities
1035    ///
1036 
1037    // Will call for d3d12 driver support based on the initial requested features, then
1038    // try to fallback if any of them is not supported and return the negotiated d3d12 settings
1039    D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT1 capEncoderSupportData1 = {};
1040    if (!d3d12_video_encoder_negotiate_requested_features_and_d3d12_driver_caps(pD3D12Enc, capEncoderSupportData1)) {
1041       debug_printf("[d3d12_video_encoder_h264] After negotiating caps, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT1 "
1042                       "arguments are not supported - "
1043                       "ValidationFlags: 0x%x - SupportFlags: 0x%x\n",
1044                       capEncoderSupportData1.ValidationFlags,
1045                       capEncoderSupportData1.SupportFlags);
1046       return false;
1047    }
1048 
1049    ///
1050    // Calculate current settings based on the returned values from the caps query
1051    //
1052    pD3D12Enc->m_currentEncodeCapabilities.m_MaxSlicesInOutput =
1053       d3d12_video_encoder_calculate_max_slices_count_in_output(
1054          pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigMode,
1055          &pD3D12Enc->m_currentEncodeConfig.m_encoderSliceConfigDesc.m_SlicesPartition_H264,
1056          pD3D12Enc->m_currentEncodeCapabilities.m_currentResolutionSupportCaps.MaxSubregionsNumber,
1057          pD3D12Enc->m_currentEncodeConfig.m_currentResolution,
1058          pD3D12Enc->m_currentEncodeCapabilities.m_currentResolutionSupportCaps.SubregionBlockPixelsSize);
1059 
1060    //
1061    // Validate caps support returned values against current settings
1062    //
1063    if (pD3D12Enc->m_currentEncodeConfig.m_encoderProfileDesc.m_H264Profile !=
1064        pD3D12Enc->m_currentEncodeCapabilities.m_encoderSuggestedProfileDesc.m_H264Profile) {
1065       debug_printf("[d3d12_video_encoder_h264] Warning: Requested D3D12_VIDEO_ENCODER_PROFILE_H264 by upper layer: %d "
1066                     "mismatches UMD suggested D3D12_VIDEO_ENCODER_PROFILE_H264: %d\n",
1067                     pD3D12Enc->m_currentEncodeConfig.m_encoderProfileDesc.m_H264Profile,
1068                     pD3D12Enc->m_currentEncodeCapabilities.m_encoderSuggestedProfileDesc.m_H264Profile);
1069    }
1070 
1071    if (pD3D12Enc->m_currentEncodeConfig.m_encoderLevelDesc.m_H264LevelSetting !=
1072        pD3D12Enc->m_currentEncodeCapabilities.m_encoderLevelSuggestedDesc.m_H264LevelSetting) {
1073       debug_printf("[d3d12_video_encoder_h264] Warning: Requested D3D12_VIDEO_ENCODER_LEVELS_H264 by upper layer: %d "
1074                     "mismatches UMD suggested D3D12_VIDEO_ENCODER_LEVELS_H264: %d\n",
1075                     pD3D12Enc->m_currentEncodeConfig.m_encoderLevelDesc.m_H264LevelSetting,
1076                     pD3D12Enc->m_currentEncodeCapabilities.m_encoderLevelSuggestedDesc.m_H264LevelSetting);
1077    }
1078 
1079    if (pD3D12Enc->m_currentEncodeCapabilities.m_MaxSlicesInOutput >
1080        pD3D12Enc->m_currentEncodeCapabilities.m_currentResolutionSupportCaps.MaxSubregionsNumber) {
1081       debug_printf("[d3d12_video_encoder_h264] Desired number of subregions %d is not supported (higher than max "
1082                       "reported slice number %d in query caps) for current resolution (%d, %d)\n.",
1083                       pD3D12Enc->m_currentEncodeCapabilities.m_MaxSlicesInOutput,
1084                       pD3D12Enc->m_currentEncodeCapabilities.m_currentResolutionSupportCaps.MaxSubregionsNumber,
1085                       pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Width,
1086                       pD3D12Enc->m_currentEncodeConfig.m_currentResolution.Height);
1087       return false;
1088    }
1089    return true;
1090 }
1091 
1092 D3D12_VIDEO_ENCODER_PROFILE_H264
d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_h264(enum pipe_video_profile profile)1093 d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_h264(enum pipe_video_profile profile)
1094 {
1095    switch (profile) {
1096       case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
1097       case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
1098       case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
1099       {
1100          return D3D12_VIDEO_ENCODER_PROFILE_H264_MAIN;
1101 
1102       } break;
1103       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
1104       {
1105          return D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH;
1106       } break;
1107       case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
1108       {
1109          return D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH_10;
1110       } break;
1111       default:
1112       {
1113          unreachable("Unsupported pipe_video_profile");
1114       } break;
1115    }
1116 }
1117 
1118 bool
d3d12_video_encoder_compare_slice_config_h264_hevc(D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE targetMode,D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES targetConfig,D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE otherMode,D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES otherConfig)1119 d3d12_video_encoder_compare_slice_config_h264_hevc(
1120    D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE targetMode,
1121    D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES targetConfig,
1122    D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE otherMode,
1123    D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES otherConfig)
1124 {
1125    return (targetMode == otherMode) &&
1126           (memcmp(&targetConfig,
1127                   &otherConfig,
1128                   sizeof(D3D12_VIDEO_ENCODER_PICTURE_CONTROL_SUBREGIONS_LAYOUT_DATA_SLICES)) == 0);
1129 }
1130 
1131 static inline bool
d3d12_video_encoder_needs_new_pps_h264(struct d3d12_video_encoder * pD3D12Enc,bool writeNewSPS,H264_PPS & tentative_pps,const H264_PPS & active_pps)1132 d3d12_video_encoder_needs_new_pps_h264(struct d3d12_video_encoder *pD3D12Enc,
1133                                        bool writeNewSPS,
1134                                        H264_PPS &tentative_pps,
1135                                        const H264_PPS &active_pps)
1136 {
1137    bool bUseSliceL0L1Override = (pD3D12Enc->m_currentEncodeConfig.m_encoderPicParamsDesc.m_H264PicData.Flags &
1138                                  D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_H264_FLAG_REQUEST_NUM_REF_IDX_ACTIVE_OVERRIDE_FLAG_SLICE);
1139 
1140    bool bDifferentL0L1Lists = !bUseSliceL0L1Override &&
1141          ((tentative_pps.num_ref_idx_l0_active_minus1 != active_pps.num_ref_idx_l0_active_minus1) ||
1142          (tentative_pps.num_ref_idx_l1_active_minus1 != active_pps.num_ref_idx_l1_active_minus1));
1143 
1144    bool bDidPPSChange =
1145       ((tentative_pps.constrained_intra_pred_flag != active_pps.constrained_intra_pred_flag) ||
1146        (tentative_pps.entropy_coding_mode_flag != active_pps.entropy_coding_mode_flag) ||
1147        bDifferentL0L1Lists ||
1148        (tentative_pps.pic_order_present_flag != active_pps.pic_order_present_flag) ||
1149        (tentative_pps.pic_parameter_set_id != active_pps.pic_parameter_set_id) ||
1150        (tentative_pps.seq_parameter_set_id != active_pps.seq_parameter_set_id) ||
1151        (tentative_pps.transform_8x8_mode_flag != active_pps.transform_8x8_mode_flag));
1152 
1153    return writeNewSPS || bDidPPSChange;
1154 }
1155 
1156 uint32_t
d3d12_video_encoder_build_codec_headers_h264(struct d3d12_video_encoder * pD3D12Enc,std::vector<uint64_t> & pWrittenCodecUnitsSizes)1157 d3d12_video_encoder_build_codec_headers_h264(struct d3d12_video_encoder *pD3D12Enc,
1158                                              std::vector<uint64_t> &pWrittenCodecUnitsSizes)
1159 {
1160    D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA currentPicParams =
1161       d3d12_video_encoder_get_current_picture_param_settings(pD3D12Enc);
1162 
1163    auto levelDesc = d3d12_video_encoder_get_current_level_desc(pD3D12Enc);
1164    auto codecConfigDesc = d3d12_video_encoder_get_current_codec_config_desc(pD3D12Enc);
1165 
1166    d3d12_video_bitstream_builder_h264 *pH264BitstreamBuilder =
1167       static_cast<d3d12_video_bitstream_builder_h264 *>(pD3D12Enc->m_upBitstreamBuilder.get());
1168    assert(pH264BitstreamBuilder);
1169 
1170    size_t writtenAUDBytesCount = 0;
1171    pWrittenCodecUnitsSizes.clear();
1172 
1173    bool forceWriteAUD = (pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags & d3d12_video_encoder_config_dirty_flag_aud_header);
1174    if (forceWriteAUD)
1175    {
1176       pH264BitstreamBuilder->write_aud(pD3D12Enc->m_BitstreamHeadersBuffer,
1177                                        pD3D12Enc->m_BitstreamHeadersBuffer.begin(),
1178                                        writtenAUDBytesCount);
1179       pWrittenCodecUnitsSizes.push_back(writtenAUDBytesCount);
1180    }
1181 
1182    bool isFirstFrame = (pD3D12Enc->m_fenceValue == 1);
1183    bool forceWriteSPS = (pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags & d3d12_video_encoder_config_dirty_flag_sequence_header);
1184    bool writeNewSPS = isFirstFrame                                         // on first frame
1185                       || ((pD3D12Enc->m_currentEncodeConfig.m_seqFlags &   // also on resolution change
1186                            D3D12_VIDEO_ENCODER_SEQUENCE_CONTROL_FLAG_RESOLUTION_CHANGE) != 0)
1187                       || forceWriteSPS;
1188 
1189    uint32_t active_seq_parameter_set_id = pH264BitstreamBuilder->get_active_sps().seq_parameter_set_id;
1190 
1191    size_t writtenSPSBytesCount = 0;
1192    if (writeNewSPS) {
1193       H264_SPS sps = pH264BitstreamBuilder->build_sps(pD3D12Enc->m_currentEncodeConfig.m_encoderCodecSpecificSequenceStateDescH264,
1194                                                       pD3D12Enc->base.profile,
1195                                                       *levelDesc.pH264LevelSetting,
1196                                                       pD3D12Enc->m_currentEncodeConfig.m_encodeFormatInfo.Format,
1197                                                       *codecConfigDesc.pH264Config,
1198                                                       pD3D12Enc->m_currentEncodeConfig.m_encoderGOPConfigDesc.m_H264GroupOfPictures,
1199                                                       active_seq_parameter_set_id,
1200                                                       pD3D12Enc->m_currentEncodeConfig.m_currentResolution,
1201                                                       pD3D12Enc->m_currentEncodeConfig.m_FrameCroppingCodecConfig,
1202                                                       pD3D12Enc->m_BitstreamHeadersBuffer,
1203                                                       pD3D12Enc->m_BitstreamHeadersBuffer.begin() + writtenAUDBytesCount,
1204                                                       writtenSPSBytesCount);
1205       pH264BitstreamBuilder->set_active_sps(sps);
1206       pWrittenCodecUnitsSizes.push_back(writtenSPSBytesCount);
1207    }
1208 
1209    size_t writtenPPSBytesCount = 0;
1210    H264_PPS tentative_pps = pH264BitstreamBuilder->build_pps(pD3D12Enc->base.profile,
1211                                                              *codecConfigDesc.pH264Config,
1212                                                              *currentPicParams.pH264PicData,
1213                                                              currentPicParams.pH264PicData->pic_parameter_set_id,
1214                                                              active_seq_parameter_set_id,
1215                                                              pD3D12Enc->m_StagingHeadersBuffer,
1216                                                              pD3D12Enc->m_StagingHeadersBuffer.begin(),
1217                                                              writtenPPSBytesCount);
1218 
1219    const H264_PPS &active_pps = pH264BitstreamBuilder->get_active_pps();
1220    bool forceWritePPS = (pD3D12Enc->m_currentEncodeConfig.m_ConfigDirtyFlags & d3d12_video_encoder_config_dirty_flag_picture_header);
1221    if (forceWritePPS || d3d12_video_encoder_needs_new_pps_h264(pD3D12Enc, writeNewSPS, tentative_pps, active_pps)) {
1222       pH264BitstreamBuilder->set_active_pps(tentative_pps);
1223       pD3D12Enc->m_BitstreamHeadersBuffer.resize(writtenAUDBytesCount + writtenSPSBytesCount + writtenPPSBytesCount);
1224       memcpy(&pD3D12Enc->m_BitstreamHeadersBuffer.data()[writtenAUDBytesCount + writtenSPSBytesCount], pD3D12Enc->m_StagingHeadersBuffer.data(), writtenPPSBytesCount);
1225       pWrittenCodecUnitsSizes.push_back(writtenPPSBytesCount);
1226    } else {
1227       writtenPPSBytesCount = 0;
1228       debug_printf("Skipping PPS (same as active PPS) for fenceValue: %" PRIu64 "\n", pD3D12Enc->m_fenceValue);
1229    }
1230 
1231    // Shrink buffer to fit the headers
1232    if (pD3D12Enc->m_BitstreamHeadersBuffer.size() > (writtenAUDBytesCount + writtenSPSBytesCount + writtenPPSBytesCount)) {
1233       pD3D12Enc->m_BitstreamHeadersBuffer.resize(writtenAUDBytesCount + writtenSPSBytesCount + writtenPPSBytesCount);
1234    }
1235 
1236    assert(std::accumulate(pWrittenCodecUnitsSizes.begin(), pWrittenCodecUnitsSizes.end(), 0u) ==
1237       static_cast<uint64_t>(pD3D12Enc->m_BitstreamHeadersBuffer.size()));
1238    return pD3D12Enc->m_BitstreamHeadersBuffer.size();
1239 }
1240