1 /**************************************************************************
3 * Copyright 2017 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "pipe/p_video_codec.h"
32 #include "util/u_video.h"
35 #include "radeon_video.h"
36 #include "radeon_vcn_enc.h"
38 static void radeon_enc_session_info(struct radeon_encoder
*enc
)
40 unsigned int interface_version
= ((RENCODE_FW_INTERFACE_MAJOR_VERSION
<< RENCODE_IF_MAJOR_VERSION_SHIFT
) |
41 (RENCODE_FW_INTERFACE_MINOR_VERSION
<< RENCODE_IF_MINOR_VERSION_SHIFT
));
42 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_SESSION_INFO
);
43 RADEON_ENC_CS(interface_version
);
44 RADEON_ENC_READWRITE(enc
->si
->res
->buf
, enc
->si
->res
->domains
, 0x0);
48 static void radeon_enc_task_info(struct radeon_encoder
*enc
, bool need_feedback
)
50 enc
->enc_pic
.task_info
.task_id
++;
53 enc
->enc_pic
.task_info
.allowed_max_num_feedbacks
= 1;
55 enc
->enc_pic
.task_info
.allowed_max_num_feedbacks
= 0;
57 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_TASK_INFO
);
58 enc
->p_task_size
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
59 RADEON_ENC_CS(enc
->enc_pic
.task_info
.task_id
);
60 RADEON_ENC_CS(enc
->enc_pic
.task_info
.allowed_max_num_feedbacks
);
64 static void radeon_enc_session_init(struct radeon_encoder
*enc
)
66 enc
->enc_pic
.session_init
.encode_standard
= RENCODE_ENCODE_STANDARD_H264
;
67 enc
->enc_pic
.session_init
.aligned_picture_width
= align(enc
->base
.width
, 16);
68 enc
->enc_pic
.session_init
.aligned_picture_height
= align(enc
->base
.height
, 16);
69 enc
->enc_pic
.session_init
.padding_width
= enc
->enc_pic
.session_init
.aligned_picture_width
- enc
->base
.width
;
70 enc
->enc_pic
.session_init
.padding_height
= enc
->enc_pic
.session_init
.aligned_picture_height
- enc
->base
.height
;
71 enc
->enc_pic
.session_init
.pre_encode_mode
= RENCODE_PREENCODE_MODE_NONE
;
72 enc
->enc_pic
.session_init
.pre_encode_chroma_enabled
= false;
74 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_SESSION_INIT
);
75 RADEON_ENC_CS(enc
->enc_pic
.session_init
.encode_standard
);
76 RADEON_ENC_CS(enc
->enc_pic
.session_init
.aligned_picture_width
);
77 RADEON_ENC_CS(enc
->enc_pic
.session_init
.aligned_picture_height
);
78 RADEON_ENC_CS(enc
->enc_pic
.session_init
.padding_width
);
79 RADEON_ENC_CS(enc
->enc_pic
.session_init
.padding_height
);
80 RADEON_ENC_CS(enc
->enc_pic
.session_init
.pre_encode_mode
);
81 RADEON_ENC_CS(enc
->enc_pic
.session_init
.pre_encode_chroma_enabled
);
85 static void radeon_enc_session_init_hevc(struct radeon_encoder
*enc
)
87 enc
->enc_pic
.session_init
.encode_standard
= RENCODE_ENCODE_STANDARD_HEVC
;
88 enc
->enc_pic
.session_init
.aligned_picture_width
= align(enc
->base
.width
, 64);
89 enc
->enc_pic
.session_init
.aligned_picture_height
= align(enc
->base
.height
, 16);
90 enc
->enc_pic
.session_init
.padding_width
= enc
->enc_pic
.session_init
.aligned_picture_width
- enc
->base
.width
;
91 enc
->enc_pic
.session_init
.padding_height
= enc
->enc_pic
.session_init
.aligned_picture_height
- enc
->base
.height
;
92 enc
->enc_pic
.session_init
.pre_encode_mode
= RENCODE_PREENCODE_MODE_NONE
;
93 enc
->enc_pic
.session_init
.pre_encode_chroma_enabled
= false;
95 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_SESSION_INIT
);
96 RADEON_ENC_CS(enc
->enc_pic
.session_init
.encode_standard
);
97 RADEON_ENC_CS(enc
->enc_pic
.session_init
.aligned_picture_width
);
98 RADEON_ENC_CS(enc
->enc_pic
.session_init
.aligned_picture_height
);
99 RADEON_ENC_CS(enc
->enc_pic
.session_init
.padding_width
);
100 RADEON_ENC_CS(enc
->enc_pic
.session_init
.padding_height
);
101 RADEON_ENC_CS(enc
->enc_pic
.session_init
.pre_encode_mode
);
102 RADEON_ENC_CS(enc
->enc_pic
.session_init
.pre_encode_chroma_enabled
);
106 static void radeon_enc_layer_control(struct radeon_encoder
*enc
)
108 enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
= 1;
109 enc
->enc_pic
.layer_ctrl
.num_temporal_layers
= 1;
111 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_LAYER_CONTROL
);
112 RADEON_ENC_CS(enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
);
113 RADEON_ENC_CS(enc
->enc_pic
.layer_ctrl
.num_temporal_layers
);
117 static void radeon_enc_layer_select(struct radeon_encoder
*enc
)
119 enc
->enc_pic
.layer_sel
.temporal_layer_index
= 0;
121 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_LAYER_SELECT
);
122 RADEON_ENC_CS(enc
->enc_pic
.layer_sel
.temporal_layer_index
);
126 static void radeon_enc_slice_control(struct radeon_encoder
*enc
)
128 enc
->enc_pic
.slice_ctrl
.slice_control_mode
= RENCODE_H264_SLICE_CONTROL_MODE_FIXED_MBS
;
129 enc
->enc_pic
.slice_ctrl
.num_mbs_per_slice
= align(enc
->base
.width
, 16) / 16 * align(enc
->base
.height
, 16) / 16;
131 RADEON_ENC_BEGIN(RENCODE_H264_IB_PARAM_SLICE_CONTROL
);
132 RADEON_ENC_CS(enc
->enc_pic
.slice_ctrl
.slice_control_mode
);
133 RADEON_ENC_CS(enc
->enc_pic
.slice_ctrl
.num_mbs_per_slice
);
137 static void radeon_enc_slice_control_hevc(struct radeon_encoder
*enc
)
139 enc
->enc_pic
.hevc_slice_ctrl
.slice_control_mode
= RENCODE_HEVC_SLICE_CONTROL_MODE_FIXED_CTBS
;
140 enc
->enc_pic
.hevc_slice_ctrl
.fixed_ctbs_per_slice
.num_ctbs_per_slice
= align(enc
->base
.width
, 64) / 64 * align(enc
->base
.height
, 64) / 64;
141 enc
->enc_pic
.hevc_slice_ctrl
.fixed_ctbs_per_slice
.num_ctbs_per_slice_segment
= enc
->enc_pic
.hevc_slice_ctrl
.fixed_ctbs_per_slice
.num_ctbs_per_slice
;
143 RADEON_ENC_BEGIN(RENCODE_HEVC_IB_PARAM_SLICE_CONTROL
);
144 RADEON_ENC_CS(enc
->enc_pic
.hevc_slice_ctrl
.slice_control_mode
);
145 RADEON_ENC_CS(enc
->enc_pic
.hevc_slice_ctrl
.fixed_ctbs_per_slice
.num_ctbs_per_slice
);
146 RADEON_ENC_CS(enc
->enc_pic
.hevc_slice_ctrl
.fixed_ctbs_per_slice
.num_ctbs_per_slice_segment
);
150 static void radeon_enc_spec_misc(struct radeon_encoder
*enc
)
152 enc
->enc_pic
.spec_misc
.constrained_intra_pred_flag
= 0;
153 enc
->enc_pic
.spec_misc
.cabac_enable
= 0;
154 enc
->enc_pic
.spec_misc
.cabac_init_idc
= 0;
155 enc
->enc_pic
.spec_misc
.half_pel_enabled
= 1;
156 enc
->enc_pic
.spec_misc
.quarter_pel_enabled
= 1;
157 enc
->enc_pic
.spec_misc
.profile_idc
= u_get_h264_profile_idc(enc
->base
.profile
);
158 enc
->enc_pic
.spec_misc
.level_idc
= enc
->base
.level
;
160 RADEON_ENC_BEGIN(RENCODE_H264_IB_PARAM_SPEC_MISC
);
161 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.constrained_intra_pred_flag
);
162 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.cabac_enable
);
163 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.cabac_init_idc
);
164 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.half_pel_enabled
);
165 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.quarter_pel_enabled
);
166 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.profile_idc
);
167 RADEON_ENC_CS(enc
->enc_pic
.spec_misc
.level_idc
);
171 static void radeon_enc_spec_misc_hevc(struct radeon_encoder
*enc
)
173 RADEON_ENC_BEGIN(RENCODE_HEVC_IB_PARAM_SPEC_MISC
);
174 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.log2_min_luma_coding_block_size_minus3
);
175 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.amp_disabled
);
176 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.strong_intra_smoothing_enabled
);
177 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.constrained_intra_pred_flag
);
178 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.cabac_init_flag
);
179 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.half_pel_enabled
);
180 RADEON_ENC_CS(enc
->enc_pic
.hevc_spec_misc
.quarter_pel_enabled
);
184 static void radeon_enc_rc_session_init(struct radeon_encoder
*enc
)
186 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_RATE_CONTROL_SESSION_INIT
);
187 RADEON_ENC_CS(enc
->enc_pic
.rc_session_init
.rate_control_method
);
188 RADEON_ENC_CS(enc
->enc_pic
.rc_session_init
.vbv_buffer_level
);
192 static void radeon_enc_rc_layer_init(struct radeon_encoder
*enc
)
194 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_RATE_CONTROL_LAYER_INIT
);
195 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.target_bit_rate
);
196 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.peak_bit_rate
);
197 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.frame_rate_num
);
198 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.frame_rate_den
);
199 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.vbv_buffer_size
);
200 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.avg_target_bits_per_picture
);
201 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.peak_bits_per_picture_integer
);
202 RADEON_ENC_CS(enc
->enc_pic
.rc_layer_init
.peak_bits_per_picture_fractional
);
206 static void radeon_enc_deblocking_filter_h264(struct radeon_encoder
*enc
)
208 enc
->enc_pic
.h264_deblock
.disable_deblocking_filter_idc
= 0;
209 enc
->enc_pic
.h264_deblock
.alpha_c0_offset_div2
= 0;
210 enc
->enc_pic
.h264_deblock
.beta_offset_div2
= 0;
211 enc
->enc_pic
.h264_deblock
.cb_qp_offset
= 0;
212 enc
->enc_pic
.h264_deblock
.cr_qp_offset
= 0;
214 RADEON_ENC_BEGIN(RENCODE_H264_IB_PARAM_DEBLOCKING_FILTER
);
215 RADEON_ENC_CS(enc
->enc_pic
.h264_deblock
.disable_deblocking_filter_idc
);
216 RADEON_ENC_CS(enc
->enc_pic
.h264_deblock
.alpha_c0_offset_div2
);
217 RADEON_ENC_CS(enc
->enc_pic
.h264_deblock
.beta_offset_div2
);
218 RADEON_ENC_CS(enc
->enc_pic
.h264_deblock
.cb_qp_offset
);
219 RADEON_ENC_CS(enc
->enc_pic
.h264_deblock
.cr_qp_offset
);
223 static void radeon_enc_deblocking_filter_hevc(struct radeon_encoder
*enc
)
225 RADEON_ENC_BEGIN(RENCODE_HEVC_IB_PARAM_DEBLOCKING_FILTER
);
226 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.loop_filter_across_slices_enabled
);
227 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.deblocking_filter_disabled
);
228 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.beta_offset_div2
);
229 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.tc_offset_div2
);
230 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.cb_qp_offset
);
231 RADEON_ENC_CS(enc
->enc_pic
.hevc_deblock
.cr_qp_offset
);
235 static void radeon_enc_quality_params(struct radeon_encoder
*enc
)
237 enc
->enc_pic
.quality_params
.vbaq_mode
= 0;
238 enc
->enc_pic
.quality_params
.scene_change_sensitivity
= 0;
239 enc
->enc_pic
.quality_params
.scene_change_min_idr_interval
= 0;
241 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_QUALITY_PARAMS
);
242 RADEON_ENC_CS(enc
->enc_pic
.quality_params
.vbaq_mode
);
243 RADEON_ENC_CS(enc
->enc_pic
.quality_params
.scene_change_sensitivity
);
244 RADEON_ENC_CS(enc
->enc_pic
.quality_params
.scene_change_min_idr_interval
);
248 static void radeon_enc_nalu_sps(struct radeon_encoder
*enc
)
250 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
251 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_SPS
);
252 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
253 radeon_enc_reset(enc
);
254 radeon_enc_set_emulation_prevention(enc
, false);
255 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
256 radeon_enc_code_fixed_bits(enc
, 0x67, 8);
257 radeon_enc_byte_align(enc
);
258 radeon_enc_set_emulation_prevention(enc
, true);
259 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.spec_misc
.profile_idc
, 8);
260 radeon_enc_code_fixed_bits(enc
, 0x44, 8); //hardcode to constrained baseline
261 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.spec_misc
.level_idc
, 8);
262 radeon_enc_code_ue(enc
, 0x0);
264 if(enc
->enc_pic
.spec_misc
.profile_idc
== 100 || enc
->enc_pic
.spec_misc
.profile_idc
== 110 || enc
->enc_pic
.spec_misc
.profile_idc
== 122 ||
265 enc
->enc_pic
.spec_misc
.profile_idc
== 244 || enc
->enc_pic
.spec_misc
.profile_idc
== 44 || enc
->enc_pic
.spec_misc
.profile_idc
== 83 ||
266 enc
->enc_pic
.spec_misc
.profile_idc
== 86 || enc
->enc_pic
.spec_misc
.profile_idc
== 118 || enc
->enc_pic
.spec_misc
.profile_idc
== 128 ||
267 enc
->enc_pic
.spec_misc
.profile_idc
== 138) {
268 radeon_enc_code_ue(enc
, 0x1);
269 radeon_enc_code_ue(enc
, 0x0);
270 radeon_enc_code_ue(enc
, 0x0);
271 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
274 radeon_enc_code_ue(enc
, 1);
275 radeon_enc_code_ue(enc
, enc
->enc_pic
.pic_order_cnt_type
);
277 if (enc
->enc_pic
.pic_order_cnt_type
== 0)
278 radeon_enc_code_ue(enc
, 1);
280 radeon_enc_code_ue(enc
, (enc
->base
.max_references
+ 1));
281 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
> 1 ? 0x1 : 0x0, 1);
282 radeon_enc_code_ue(enc
, (enc
->enc_pic
.session_init
.aligned_picture_width
/ 16 - 1));
283 radeon_enc_code_ue(enc
, (enc
->enc_pic
.session_init
.aligned_picture_height
/ 16 - 1));
284 bool progressive_only
= true;
285 radeon_enc_code_fixed_bits(enc
, progressive_only
? 0x1 : 0x0, 1);
287 if (!progressive_only
)
288 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
290 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
292 if ((enc
->enc_pic
.crop_left
!= 0) || (enc
->enc_pic
.crop_right
!= 0) ||
293 (enc
->enc_pic
.crop_top
!= 0) || (enc
->enc_pic
.crop_bottom
!= 0)) {
294 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
295 radeon_enc_code_ue(enc
, enc
->enc_pic
.crop_left
);
296 radeon_enc_code_ue(enc
, enc
->enc_pic
.crop_right
);
297 radeon_enc_code_ue(enc
, enc
->enc_pic
.crop_top
);
298 radeon_enc_code_ue(enc
, enc
->enc_pic
.crop_bottom
);
300 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
302 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
303 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
304 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
305 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
306 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
307 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
308 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
309 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
310 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
311 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
312 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
313 radeon_enc_code_ue(enc
, 0x0);
314 radeon_enc_code_ue(enc
, 0x0);
315 radeon_enc_code_ue(enc
, 16);
316 radeon_enc_code_ue(enc
, 16);
317 radeon_enc_code_ue(enc
, 0x0);
318 radeon_enc_code_ue(enc
, (enc
->base
.max_references
+ 1));
320 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
322 radeon_enc_byte_align(enc
);
323 radeon_enc_flush_headers(enc
);
324 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
328 static void radeon_enc_nalu_sps_hevc(struct radeon_encoder
*enc
)
330 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
331 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_SPS
);
332 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
335 radeon_enc_reset(enc
);
336 radeon_enc_set_emulation_prevention(enc
, false);
337 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
338 radeon_enc_code_fixed_bits(enc
, 0x4201, 16);
339 radeon_enc_byte_align(enc
);
340 radeon_enc_set_emulation_prevention(enc
, true);
341 radeon_enc_code_fixed_bits(enc
, 0x0, 4);
342 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1, 3);
343 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
344 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
345 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_tier_flag
, 1);
346 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_profile_idc
, 5);
347 radeon_enc_code_fixed_bits(enc
, 0x60000000, 32);
348 radeon_enc_code_fixed_bits(enc
, 0xb0000000, 32);
349 radeon_enc_code_fixed_bits(enc
, 0x0, 16);
350 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_level_idc
, 8);
352 for (i
= 0; i
< (enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1) ; i
++)
353 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
355 if ((enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1) > 0) {
356 for (i
= (enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1); i
< 8; i
++)
357 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
360 radeon_enc_code_ue(enc
, 0x0);
361 radeon_enc_code_ue(enc
, enc
->enc_pic
.chroma_format_idc
);
362 radeon_enc_code_ue(enc
, enc
->enc_pic
.session_init
.aligned_picture_width
);
363 radeon_enc_code_ue(enc
, enc
->enc_pic
.session_init
.aligned_picture_height
);
364 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
365 radeon_enc_code_ue(enc
, enc
->enc_pic
.bit_depth_luma_minus8
);
366 radeon_enc_code_ue(enc
, enc
->enc_pic
.bit_depth_chroma_minus8
);
367 radeon_enc_code_ue(enc
, enc
->enc_pic
.log2_max_poc
- 4);
368 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
369 radeon_enc_code_ue(enc
, 1);
370 radeon_enc_code_ue(enc
, 0x0);
371 radeon_enc_code_ue(enc
, 0x0);
372 radeon_enc_code_ue(enc
, enc
->enc_pic
.hevc_spec_misc
.log2_min_luma_coding_block_size_minus3
);
373 //Only support CTBSize 64
374 radeon_enc_code_ue(enc
, 6 - (enc
->enc_pic
.hevc_spec_misc
.log2_min_luma_coding_block_size_minus3
+ 3));
375 radeon_enc_code_ue(enc
, enc
->enc_pic
.log2_min_transform_block_size_minus2
);
376 radeon_enc_code_ue(enc
, enc
->enc_pic
.log2_diff_max_min_transform_block_size
);
377 radeon_enc_code_ue(enc
, enc
->enc_pic
.max_transform_hierarchy_depth_inter
);
378 radeon_enc_code_ue(enc
, enc
->enc_pic
.max_transform_hierarchy_depth_intra
);
380 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
381 radeon_enc_code_fixed_bits(enc
, !enc
->enc_pic
.hevc_spec_misc
.amp_disabled
, 1);
382 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.sample_adaptive_offset_enabled_flag
, 1);
383 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.pcm_enabled_flag
, 1);
385 radeon_enc_code_ue(enc
, 1);
386 radeon_enc_code_ue(enc
, 1);
387 radeon_enc_code_ue(enc
, 0);
388 radeon_enc_code_ue(enc
, 0);
389 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
391 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
393 radeon_enc_code_fixed_bits(enc
, 0, 1);
394 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_spec_misc
.strong_intra_smoothing_enabled
, 1);
396 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
398 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
400 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
402 radeon_enc_byte_align(enc
);
403 radeon_enc_flush_headers(enc
);
404 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
408 static void radeon_enc_nalu_pps(struct radeon_encoder
*enc
)
410 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
411 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS
);
412 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
413 radeon_enc_reset(enc
);
414 radeon_enc_set_emulation_prevention(enc
, false);
415 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
416 radeon_enc_code_fixed_bits(enc
, 0x68, 8);
417 radeon_enc_byte_align(enc
);
418 radeon_enc_set_emulation_prevention(enc
, true);
419 radeon_enc_code_ue(enc
, 0x0);
420 radeon_enc_code_ue(enc
, 0x0);
421 radeon_enc_code_fixed_bits(enc
, (enc
->enc_pic
.spec_misc
.cabac_enable
? 0x1 : 0x0), 1);
422 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
423 radeon_enc_code_ue(enc
, 0x0);
424 radeon_enc_code_ue(enc
, 0x0);
425 radeon_enc_code_ue(enc
, 0x0);
426 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
427 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
428 radeon_enc_code_se(enc
, 0x0);
429 radeon_enc_code_se(enc
, 0x0);
430 radeon_enc_code_se(enc
, 0x0);
431 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
432 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
433 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
435 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
437 radeon_enc_byte_align(enc
);
438 radeon_enc_flush_headers(enc
);
439 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
443 static void radeon_enc_nalu_pps_hevc(struct radeon_encoder
*enc
)
445 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
446 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS
);
447 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
448 radeon_enc_reset(enc
);
449 radeon_enc_set_emulation_prevention(enc
, false);
450 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
451 radeon_enc_code_fixed_bits(enc
, 0x4401, 16);
452 radeon_enc_byte_align(enc
);
453 radeon_enc_set_emulation_prevention(enc
, true);
454 radeon_enc_code_ue(enc
, 0x0);
455 radeon_enc_code_ue(enc
, 0x0);
456 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
457 radeon_enc_code_fixed_bits(enc
, 0x0, 4);
458 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
459 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
460 radeon_enc_code_ue(enc
, 0x0);
461 radeon_enc_code_ue(enc
, 0x0);
462 radeon_enc_code_se(enc
, 0x0);
463 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_spec_misc
.constrained_intra_pred_flag
, 1);
464 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
465 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
466 radeon_enc_code_se(enc
, enc
->enc_pic
.hevc_deblock
.cb_qp_offset
);
467 radeon_enc_code_se(enc
, enc
->enc_pic
.hevc_deblock
.cr_qp_offset
);
468 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
469 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
470 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
471 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
472 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
473 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_deblock
.loop_filter_across_slices_enabled
, 1);
474 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
475 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
476 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_deblock
.deblocking_filter_disabled
, 1);
478 if (!enc
->enc_pic
.hevc_deblock
.deblocking_filter_disabled
) {
479 radeon_enc_code_se(enc
, enc
->enc_pic
.hevc_deblock
.beta_offset_div2
);
480 radeon_enc_code_se(enc
, enc
->enc_pic
.hevc_deblock
.tc_offset_div2
);
483 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
484 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
485 radeon_enc_code_ue(enc
, enc
->enc_pic
.log2_parallel_merge_level_minus2
);
486 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
488 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
490 radeon_enc_byte_align(enc
);
491 radeon_enc_flush_headers(enc
);
492 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
496 static void radeon_enc_nalu_vps(struct radeon_encoder
*enc
)
498 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
499 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_VPS
);
500 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
503 radeon_enc_reset(enc
);
504 radeon_enc_set_emulation_prevention(enc
, false);
505 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
506 radeon_enc_code_fixed_bits(enc
, 0x4001, 16);
507 radeon_enc_byte_align(enc
);
508 radeon_enc_set_emulation_prevention(enc
, true);
510 radeon_enc_code_fixed_bits(enc
, 0x0, 4);
511 radeon_enc_code_fixed_bits(enc
, 0x3, 2);
512 radeon_enc_code_fixed_bits(enc
, 0x0, 6);
513 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1, 3);
514 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
515 radeon_enc_code_fixed_bits(enc
, 0xffff, 16);
516 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
517 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_tier_flag
, 1);
518 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_profile_idc
, 5);
519 radeon_enc_code_fixed_bits(enc
, 0x60000000, 32);
520 radeon_enc_code_fixed_bits(enc
, 0xb0000000, 32);
521 radeon_enc_code_fixed_bits(enc
, 0x0, 16);
522 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.general_level_idc
, 8);
524 for (i
= 0; i
< (enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1) ; i
++)
525 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
527 if ((enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1) > 0) {
528 for (i
= (enc
->enc_pic
.layer_ctrl
.max_num_temporal_layers
- 1); i
< 8; i
++)
529 radeon_enc_code_fixed_bits(enc
, 0x0, 2);
532 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
533 radeon_enc_code_ue(enc
, 0x1);
534 radeon_enc_code_ue(enc
, 0x0);
535 radeon_enc_code_ue(enc
, 0x0);
537 radeon_enc_code_fixed_bits(enc
, 0x0, 6);
538 radeon_enc_code_ue(enc
, 0x0);
539 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
540 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
542 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
544 radeon_enc_byte_align(enc
);
545 radeon_enc_flush_headers(enc
);
546 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
550 static void radeon_enc_nalu_aud_hevc(struct radeon_encoder
*enc
)
552 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU
);
553 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_AUD
);
554 uint32_t *size_in_bytes
= &enc
->cs
->current
.buf
[enc
->cs
->current
.cdw
++];
555 radeon_enc_reset(enc
);
556 radeon_enc_set_emulation_prevention(enc
, false);
557 radeon_enc_code_fixed_bits(enc
, 0x00000001, 32);
558 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
559 radeon_enc_code_fixed_bits(enc
, 35, 6);
560 radeon_enc_code_fixed_bits(enc
, 0x0, 6);
561 radeon_enc_code_fixed_bits(enc
, 0x1, 3);
562 radeon_enc_byte_align(enc
);
563 radeon_enc_set_emulation_prevention(enc
, true);
564 switch(enc
->enc_pic
.picture_type
) {
565 case PIPE_H265_ENC_PICTURE_TYPE_I
:
566 case PIPE_H265_ENC_PICTURE_TYPE_IDR
:
567 radeon_enc_code_fixed_bits(enc
, 0x00, 3);
569 case PIPE_H265_ENC_PICTURE_TYPE_P
:
570 radeon_enc_code_fixed_bits(enc
, 0x01, 3);
572 case PIPE_H265_ENC_PICTURE_TYPE_B
:
573 radeon_enc_code_fixed_bits(enc
, 0x02, 3);
576 radeon_enc_code_fixed_bits(enc
, 0x02, 3);
579 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
581 radeon_enc_byte_align(enc
);
582 radeon_enc_flush_headers(enc
);
583 *size_in_bytes
= (enc
->bits_output
+ 7) / 8;
587 static void radeon_enc_slice_header(struct radeon_encoder
*enc
)
589 uint32_t instruction
[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
] = {0};
590 uint32_t num_bits
[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
] = {0};
591 unsigned int inst_index
= 0;
592 unsigned int bit_index
= 0;
593 unsigned int bits_copied
= 0;
594 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_SLICE_HEADER
);
595 radeon_enc_reset(enc
);
596 radeon_enc_set_emulation_prevention(enc
, false);
598 if (enc
->enc_pic
.is_idr
)
599 radeon_enc_code_fixed_bits(enc
, 0x65, 8);
600 else if (enc
->enc_pic
.not_referenced
)
601 radeon_enc_code_fixed_bits(enc
, 0x01, 8);
603 radeon_enc_code_fixed_bits(enc
, 0x41, 8);
605 radeon_enc_flush_headers(enc
);
607 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
608 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
609 bits_copied
= enc
->bits_output
;
612 instruction
[inst_index
] = RENCODE_H264_HEADER_INSTRUCTION_FIRST_MB
;
615 switch(enc
->enc_pic
.picture_type
) {
616 case PIPE_H264_ENC_PICTURE_TYPE_I
:
617 case PIPE_H264_ENC_PICTURE_TYPE_IDR
:
618 radeon_enc_code_fixed_bits(enc
, 0x08, 7);
620 case PIPE_H264_ENC_PICTURE_TYPE_P
:
621 case PIPE_H264_ENC_PICTURE_TYPE_SKIP
:
622 radeon_enc_code_fixed_bits(enc
, 0x06, 5);
624 case PIPE_H264_ENC_PICTURE_TYPE_B
:
625 radeon_enc_code_fixed_bits(enc
, 0x07, 5);
628 radeon_enc_code_fixed_bits(enc
, 0x08, 7);
631 radeon_enc_code_ue(enc
, 0x0);
632 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.frame_num
% 32, 5);
634 if (enc
->enc_pic
.h264_enc_params
.input_picture_structure
!= RENCODE_H264_PICTURE_STRUCTURE_FRAME
) {
635 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
636 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.h264_enc_params
.input_picture_structure
== RENCODE_H264_PICTURE_STRUCTURE_BOTTOM_FIELD
? 1 : 0, 1);
639 if (enc
->enc_pic
.is_idr
)
640 radeon_enc_code_ue(enc
, enc
->enc_pic
.is_even_frame
);
642 enc
->enc_pic
.is_even_frame
= !enc
->enc_pic
.is_even_frame
;
644 if (enc
->enc_pic
.pic_order_cnt_type
== 0)
645 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.pic_order_cnt
% 32, 5);
647 if (enc
->enc_pic
.picture_type
!= PIPE_H264_ENC_PICTURE_TYPE_IDR
) {
648 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
650 if (enc
->enc_pic
.frame_num
- enc
->enc_pic
.ref_idx_l0
> 1) {
651 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
652 radeon_enc_code_ue(enc
, 0x0);
653 radeon_enc_code_ue(enc
, (enc
->enc_pic
.frame_num
- enc
->enc_pic
.ref_idx_l0
- 1));
654 radeon_enc_code_ue(enc
, 0x3);
656 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
659 if (enc
->enc_pic
.is_idr
) {
660 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
661 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
663 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
665 if ((enc
->enc_pic
.picture_type
!= PIPE_H264_ENC_PICTURE_TYPE_IDR
) && (enc
->enc_pic
.spec_misc
.cabac_enable
))
666 radeon_enc_code_ue(enc
, enc
->enc_pic
.spec_misc
.cabac_init_idc
);
668 radeon_enc_flush_headers(enc
);
670 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
671 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
672 bits_copied
= enc
->bits_output
;
675 instruction
[inst_index
] = RENCODE_H264_HEADER_INSTRUCTION_SLICE_QP_DELTA
;
678 radeon_enc_code_ue(enc
, enc
->enc_pic
.h264_deblock
.disable_deblocking_filter_idc
? 1: 0);
680 if (!enc
->enc_pic
.h264_deblock
.disable_deblocking_filter_idc
) {
681 radeon_enc_code_se(enc
, enc
->enc_pic
.h264_deblock
.alpha_c0_offset_div2
);
682 radeon_enc_code_se(enc
, enc
->enc_pic
.h264_deblock
.beta_offset_div2
);
685 radeon_enc_flush_headers(enc
);
687 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
688 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
689 bits_copied
= enc
->bits_output
;
692 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_END
;
694 for (int i
= bit_index
; i
< RENCODE_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS
; i
++)
695 RADEON_ENC_CS(0x00000000);
697 for (int j
= 0; j
< RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
; j
++) {
698 RADEON_ENC_CS(instruction
[j
]);
699 RADEON_ENC_CS(num_bits
[j
]);
705 static void radeon_enc_slice_header_hevc(struct radeon_encoder
*enc
)
707 uint32_t instruction
[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
] = {0};
708 uint32_t num_bits
[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
] = {0};
709 unsigned int inst_index
= 0;
710 unsigned int bit_index
= 0;
711 unsigned int bits_copied
= 0;
712 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_SLICE_HEADER
);
713 radeon_enc_reset(enc
);
714 radeon_enc_set_emulation_prevention(enc
, false);
716 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
717 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.nal_unit_type
, 6);
718 radeon_enc_code_fixed_bits(enc
, 0x0, 6);
719 radeon_enc_code_fixed_bits(enc
, 0x1, 3);
721 radeon_enc_flush_headers(enc
);
723 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
724 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
725 bits_copied
= enc
->bits_output
;
728 instruction
[inst_index
] = RENCODE_HEVC_HEADER_INSTRUCTION_FIRST_SLICE
;
731 if ((enc
->enc_pic
.nal_unit_type
>= 16) && (enc
->enc_pic
.nal_unit_type
<= 23))
732 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
734 radeon_enc_code_ue(enc
, 0x0);
736 radeon_enc_flush_headers(enc
);
738 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
739 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
740 bits_copied
= enc
->bits_output
;
743 instruction
[inst_index
] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_SEGMENT
;
746 instruction
[inst_index
] = RENCODE_HEVC_HEADER_INSTRUCTION_DEPENDENT_SLICE_END
;
749 switch(enc
->enc_pic
.picture_type
) {
750 case PIPE_H265_ENC_PICTURE_TYPE_I
:
751 case PIPE_H265_ENC_PICTURE_TYPE_IDR
:
752 radeon_enc_code_ue(enc
, 0x2);
754 case PIPE_H265_ENC_PICTURE_TYPE_P
:
755 case PIPE_H265_ENC_PICTURE_TYPE_SKIP
:
756 radeon_enc_code_ue(enc
, 0x1);
758 case PIPE_H265_ENC_PICTURE_TYPE_B
:
759 radeon_enc_code_ue(enc
, 0x0);
762 radeon_enc_code_ue(enc
, 0x1);
765 if ((enc
->enc_pic
.nal_unit_type
!= 19) && (enc
->enc_pic
.nal_unit_type
!= 20)) {
766 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.frame_num
% enc
->enc_pic
.max_poc
, enc
->enc_pic
.log2_max_poc
);
767 if (enc
->enc_pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_P
)
768 radeon_enc_code_fixed_bits(enc
, 0x1, 1);
770 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
771 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
772 radeon_enc_code_ue(enc
, 0x0);
773 radeon_enc_code_ue(enc
, 0x0);
777 if ((enc
->enc_pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_P
) ||
778 (enc
->enc_pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_B
)) {
779 radeon_enc_code_fixed_bits(enc
, 0x0, 1);
780 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_spec_misc
.cabac_init_flag
, 1);
781 radeon_enc_code_ue(enc
, 5 - enc
->enc_pic
.max_num_merge_cand
);
784 radeon_enc_flush_headers(enc
);
786 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
787 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
788 bits_copied
= enc
->bits_output
;
791 instruction
[inst_index
] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_QP_DELTA
;
794 if ((enc
->enc_pic
.hevc_deblock
.loop_filter_across_slices_enabled
) &&
795 (!enc
->enc_pic
.hevc_deblock
.deblocking_filter_disabled
)){
796 radeon_enc_code_fixed_bits(enc
, enc
->enc_pic
.hevc_deblock
.loop_filter_across_slices_enabled
, 1);
798 radeon_enc_flush_headers(enc
);
800 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_COPY
;
801 num_bits
[inst_index
] = enc
->bits_output
- bits_copied
;
802 bits_copied
= enc
->bits_output
;
806 instruction
[inst_index
] = RENCODE_HEADER_INSTRUCTION_END
;
808 for (int i
= bit_index
; i
< RENCODE_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS
; i
++)
809 RADEON_ENC_CS(0x00000000);
811 for (int j
= 0; j
< RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS
; j
++) {
812 RADEON_ENC_CS(instruction
[j
]);
813 RADEON_ENC_CS(num_bits
[j
]);
819 static void radeon_enc_ctx(struct radeon_encoder
*enc
)
821 enc
->enc_pic
.ctx_buf
.swizzle_mode
= 0;
822 enc
->enc_pic
.ctx_buf
.rec_luma_pitch
= align(enc
->base
.width
, enc
->alignment
);
823 enc
->enc_pic
.ctx_buf
.rec_chroma_pitch
= align(enc
->base
.width
, enc
->alignment
);
824 enc
->enc_pic
.ctx_buf
.num_reconstructed_pictures
= 2;
826 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_ENCODE_CONTEXT_BUFFER
);
827 RADEON_ENC_READWRITE(enc
->cpb
.res
->buf
, enc
->cpb
.res
->domains
, 0);
828 RADEON_ENC_CS(enc
->enc_pic
.ctx_buf
.swizzle_mode
);
829 RADEON_ENC_CS(enc
->enc_pic
.ctx_buf
.rec_luma_pitch
);
830 RADEON_ENC_CS(enc
->enc_pic
.ctx_buf
.rec_chroma_pitch
);
831 RADEON_ENC_CS(enc
->enc_pic
.ctx_buf
.num_reconstructed_pictures
);
832 /* reconstructed_picture_1_luma_offset */
833 RADEON_ENC_CS(0x00000000);
834 /* reconstructed_picture_1_chroma_offset */
835 RADEON_ENC_CS(align(enc
->base
.width
, enc
->alignment
) * align(enc
->base
.height
, 16));
836 /* reconstructed_picture_2_luma_offset */
837 RADEON_ENC_CS(align(enc
->base
.width
, enc
->alignment
) * align(enc
->base
.height
, 16) * 3 / 2);
838 /* reconstructed_picture_2_chroma_offset */
839 RADEON_ENC_CS(align(enc
->base
.width
, enc
->alignment
) * align(enc
->base
.height
, 16) * 5 / 2);
841 for (int i
= 0; i
< 136 ; i
++)
842 RADEON_ENC_CS(0x00000000);
847 static void radeon_enc_bitstream(struct radeon_encoder
*enc
)
849 enc
->enc_pic
.bit_buf
.mode
= RENCODE_REC_SWIZZLE_MODE_LINEAR
;
850 enc
->enc_pic
.bit_buf
.video_bitstream_buffer_size
= enc
->bs_size
;
851 enc
->enc_pic
.bit_buf
.video_bitstream_data_offset
= 0;
853 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_VIDEO_BITSTREAM_BUFFER
);
854 RADEON_ENC_CS(enc
->enc_pic
.bit_buf
.mode
);
855 RADEON_ENC_WRITE(enc
->bs_handle
, RADEON_DOMAIN_GTT
, 0);
856 RADEON_ENC_CS(enc
->enc_pic
.bit_buf
.video_bitstream_buffer_size
);
857 RADEON_ENC_CS(enc
->enc_pic
.bit_buf
.video_bitstream_data_offset
);
861 static void radeon_enc_feedback(struct radeon_encoder
*enc
)
863 enc
->enc_pic
.fb_buf
.mode
= RENCODE_FEEDBACK_BUFFER_MODE_LINEAR
;
864 enc
->enc_pic
.fb_buf
.feedback_buffer_size
= 16;
865 enc
->enc_pic
.fb_buf
.feedback_data_size
= 40;
867 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_FEEDBACK_BUFFER
);
868 RADEON_ENC_CS(enc
->enc_pic
.fb_buf
.mode
);
869 RADEON_ENC_WRITE(enc
->fb
->res
->buf
, enc
->fb
->res
->domains
, 0x0);
870 RADEON_ENC_CS(enc
->enc_pic
.fb_buf
.feedback_buffer_size
);
871 RADEON_ENC_CS(enc
->enc_pic
.fb_buf
.feedback_data_size
);
875 static void radeon_enc_intra_refresh(struct radeon_encoder
*enc
)
877 enc
->enc_pic
.intra_ref
.intra_refresh_mode
= RENCODE_INTRA_REFRESH_MODE_NONE
;
878 enc
->enc_pic
.intra_ref
.offset
= 0;
879 enc
->enc_pic
.intra_ref
.region_size
= 0;
881 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_INTRA_REFRESH
);
882 RADEON_ENC_CS(enc
->enc_pic
.intra_ref
.intra_refresh_mode
);
883 RADEON_ENC_CS(enc
->enc_pic
.intra_ref
.offset
);
884 RADEON_ENC_CS(enc
->enc_pic
.intra_ref
.region_size
);
888 static void radeon_enc_rc_per_pic(struct radeon_encoder
*enc
)
890 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_RATE_CONTROL_PER_PICTURE
);
891 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.qp
);
892 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.min_qp_app
);
893 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.max_qp_app
);
894 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.max_au_size
);
895 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.enabled_filler_data
);
896 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.skip_frame_enable
);
897 RADEON_ENC_CS(enc
->enc_pic
.rc_per_pic
.enforce_hrd
);
901 static void radeon_enc_encode_params(struct radeon_encoder
*enc
)
903 switch(enc
->enc_pic
.picture_type
) {
904 case PIPE_H264_ENC_PICTURE_TYPE_I
:
905 case PIPE_H264_ENC_PICTURE_TYPE_IDR
:
906 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_I
;
908 case PIPE_H264_ENC_PICTURE_TYPE_P
:
909 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_P
;
911 case PIPE_H264_ENC_PICTURE_TYPE_SKIP
:
912 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_P_SKIP
;
914 case PIPE_H264_ENC_PICTURE_TYPE_B
:
915 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_B
;
918 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_I
;
921 enc
->enc_pic
.enc_params
.allowed_max_bitstream_size
= enc
->bs_size
;
922 enc
->enc_pic
.enc_params
.input_pic_luma_pitch
= enc
->luma
->u
.gfx9
.surf_pitch
;
923 enc
->enc_pic
.enc_params
.input_pic_chroma_pitch
= enc
->chroma
->u
.gfx9
.surf_pitch
;
924 enc
->enc_pic
.enc_params
.input_pic_swizzle_mode
= RENCODE_INPUT_SWIZZLE_MODE_LINEAR
;
926 if(enc
->enc_pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_IDR
)
927 enc
->enc_pic
.enc_params
.reference_picture_index
= 0xFFFFFFFF;
929 enc
->enc_pic
.enc_params
.reference_picture_index
= (enc
->enc_pic
.frame_num
- 1) % 2;
931 enc
->enc_pic
.enc_params
.reconstructed_picture_index
= enc
->enc_pic
.frame_num
% 2;
933 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_ENCODE_PARAMS
);
934 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.pic_type
);
935 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.allowed_max_bitstream_size
);
936 RADEON_ENC_READ(enc
->handle
, RADEON_DOMAIN_VRAM
, enc
->luma
->u
.gfx9
.surf_offset
);
937 RADEON_ENC_READ(enc
->handle
, RADEON_DOMAIN_VRAM
, enc
->chroma
->u
.gfx9
.surf_offset
);
938 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_luma_pitch
);
939 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_chroma_pitch
);
940 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_swizzle_mode
);
941 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.reference_picture_index
);
942 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.reconstructed_picture_index
);
946 static void radeon_enc_encode_params_hevc(struct radeon_encoder
*enc
)
948 switch(enc
->enc_pic
.picture_type
) {
949 case PIPE_H265_ENC_PICTURE_TYPE_I
:
950 case PIPE_H265_ENC_PICTURE_TYPE_IDR
:
951 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_I
;
953 case PIPE_H265_ENC_PICTURE_TYPE_P
:
954 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_P
;
956 case PIPE_H265_ENC_PICTURE_TYPE_SKIP
:
957 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_P_SKIP
;
959 case PIPE_H265_ENC_PICTURE_TYPE_B
:
960 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_B
;
963 enc
->enc_pic
.enc_params
.pic_type
= RENCODE_PICTURE_TYPE_I
;
966 enc
->enc_pic
.enc_params
.allowed_max_bitstream_size
= enc
->bs_size
;
967 enc
->enc_pic
.enc_params
.input_pic_luma_pitch
= enc
->luma
->u
.gfx9
.surf_pitch
;
968 enc
->enc_pic
.enc_params
.input_pic_chroma_pitch
= enc
->chroma
->u
.gfx9
.surf_pitch
;
969 enc
->enc_pic
.enc_params
.input_pic_swizzle_mode
= RENCODE_INPUT_SWIZZLE_MODE_LINEAR
;
971 if(enc
->enc_pic
.enc_params
.pic_type
== RENCODE_PICTURE_TYPE_I
)
972 enc
->enc_pic
.enc_params
.reference_picture_index
= 0xFFFFFFFF;
974 enc
->enc_pic
.enc_params
.reference_picture_index
= (enc
->enc_pic
.frame_num
- 1) % 2;
976 enc
->enc_pic
.enc_params
.reconstructed_picture_index
= enc
->enc_pic
.frame_num
% 2;
978 RADEON_ENC_BEGIN(RENCODE_IB_PARAM_ENCODE_PARAMS
);
979 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.pic_type
);
980 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.allowed_max_bitstream_size
);
981 RADEON_ENC_READ(enc
->handle
, RADEON_DOMAIN_VRAM
, enc
->luma
->u
.gfx9
.surf_offset
);
982 RADEON_ENC_READ(enc
->handle
, RADEON_DOMAIN_VRAM
, enc
->chroma
->u
.gfx9
.surf_offset
);
983 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_luma_pitch
);
984 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_chroma_pitch
);
985 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.input_pic_swizzle_mode
);
986 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.reference_picture_index
);
987 RADEON_ENC_CS(enc
->enc_pic
.enc_params
.reconstructed_picture_index
);
991 static void radeon_enc_encode_params_h264(struct radeon_encoder
*enc
)
993 enc
->enc_pic
.h264_enc_params
.input_picture_structure
= RENCODE_H264_PICTURE_STRUCTURE_FRAME
;
994 enc
->enc_pic
.h264_enc_params
.interlaced_mode
= RENCODE_H264_INTERLACING_MODE_PROGRESSIVE
;
995 enc
->enc_pic
.h264_enc_params
.reference_picture_structure
= RENCODE_H264_PICTURE_STRUCTURE_FRAME
;
996 enc
->enc_pic
.h264_enc_params
.reference_picture1_index
= 0xFFFFFFFF;
998 RADEON_ENC_BEGIN(RENCODE_H264_IB_PARAM_ENCODE_PARAMS
);
999 RADEON_ENC_CS(enc
->enc_pic
.h264_enc_params
.input_picture_structure
);
1000 RADEON_ENC_CS(enc
->enc_pic
.h264_enc_params
.interlaced_mode
);
1001 RADEON_ENC_CS(enc
->enc_pic
.h264_enc_params
.reference_picture_structure
);
1002 RADEON_ENC_CS(enc
->enc_pic
.h264_enc_params
.reference_picture1_index
);
1006 static void radeon_enc_op_init(struct radeon_encoder
*enc
)
1008 RADEON_ENC_BEGIN(RENCODE_IB_OP_INITIALIZE
);
1012 static void radeon_enc_op_close(struct radeon_encoder
*enc
)
1014 RADEON_ENC_BEGIN(RENCODE_IB_OP_CLOSE_SESSION
);
1018 static void radeon_enc_op_enc(struct radeon_encoder
*enc
)
1020 RADEON_ENC_BEGIN(RENCODE_IB_OP_ENCODE
);
1024 static void radeon_enc_op_init_rc(struct radeon_encoder
*enc
)
1026 RADEON_ENC_BEGIN(RENCODE_IB_OP_INIT_RC
);
1030 static void radeon_enc_op_init_rc_vbv(struct radeon_encoder
*enc
)
1032 RADEON_ENC_BEGIN(RENCODE_IB_OP_INIT_RC_VBV_BUFFER_LEVEL
);
1036 static void radeon_enc_op_speed(struct radeon_encoder
*enc
)
1038 RADEON_ENC_BEGIN(RENCODE_IB_OP_SET_SPEED_ENCODING_MODE
);
1042 static void begin(struct radeon_encoder
*enc
)
1044 enc
->session_info(enc
);
1045 enc
->total_task_size
= 0;
1046 enc
->task_info(enc
, enc
->need_feedback
);
1049 enc
->session_init(enc
);
1050 enc
->slice_control(enc
);
1051 enc
->spec_misc(enc
);
1052 enc
->deblocking_filter(enc
);
1054 enc
->layer_control(enc
);
1055 enc
->rc_session_init(enc
);
1056 enc
->quality_params(enc
);
1057 enc
->layer_select(enc
);
1058 enc
->rc_layer_init(enc
);
1059 enc
->layer_select(enc
);
1060 enc
->rc_per_pic(enc
);
1061 enc
->op_init_rc(enc
);
1062 enc
->op_init_rc_vbv(enc
);
1063 *enc
->p_task_size
= (enc
->total_task_size
);
1066 static void radeon_enc_headers_h264(struct radeon_encoder
*enc
)
1068 if (enc
->enc_pic
.is_idr
) {
1072 enc
->slice_header(enc
);
1073 enc
->encode_params(enc
);
1074 enc
->encode_params_codec_spec(enc
);
1077 static void radeon_enc_headers_hevc(struct radeon_encoder
*enc
)
1080 if (enc
->enc_pic
.is_idr
) {
1085 enc
->slice_header(enc
);
1086 enc
->encode_params_codec_spec(enc
);
1089 static void encode(struct radeon_encoder
*enc
)
1091 enc
->session_info(enc
);
1092 enc
->total_task_size
= 0;
1093 enc
->task_info(enc
, enc
->need_feedback
);
1095 enc
->encode_headers(enc
);
1097 enc
->bitstream(enc
);
1099 enc
->intra_refresh(enc
);
1103 *enc
->p_task_size
= (enc
->total_task_size
);
1106 static void destroy(struct radeon_encoder
*enc
)
1108 enc
->session_info(enc
);
1109 enc
->total_task_size
= 0;
1110 enc
->task_info(enc
, enc
->need_feedback
);
1112 *enc
->p_task_size
= (enc
->total_task_size
);
1115 void radeon_enc_1_2_init(struct radeon_encoder
*enc
)
1118 enc
->encode
= encode
;
1119 enc
->destroy
= destroy
;
1120 enc
->session_info
= radeon_enc_session_info
;
1121 enc
->task_info
= radeon_enc_task_info
;
1122 enc
->layer_control
= radeon_enc_layer_control
;
1123 enc
->layer_select
= radeon_enc_layer_select
;
1124 enc
->rc_session_init
= radeon_enc_rc_session_init
;
1125 enc
->rc_layer_init
= radeon_enc_rc_layer_init
;
1126 enc
->quality_params
= radeon_enc_quality_params
;
1127 enc
->ctx
= radeon_enc_ctx
;
1128 enc
->bitstream
= radeon_enc_bitstream
;
1129 enc
->feedback
= radeon_enc_feedback
;
1130 enc
->intra_refresh
= radeon_enc_intra_refresh
;
1131 enc
->rc_per_pic
= radeon_enc_rc_per_pic
;
1132 enc
->encode_params
= radeon_enc_encode_params
;
1133 enc
->op_init
= radeon_enc_op_init
;
1134 enc
->op_close
= radeon_enc_op_close
;
1135 enc
->op_enc
= radeon_enc_op_enc
;
1136 enc
->op_init_rc
= radeon_enc_op_init_rc
;
1137 enc
->op_init_rc_vbv
= radeon_enc_op_init_rc_vbv
;
1138 enc
->op_speed
= radeon_enc_op_speed
;
1140 if (u_reduce_video_profile(enc
->base
.profile
) == PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
1141 enc
->session_init
= radeon_enc_session_init
;
1142 enc
->slice_control
= radeon_enc_slice_control
;
1143 enc
->spec_misc
= radeon_enc_spec_misc
;
1144 enc
->deblocking_filter
= radeon_enc_deblocking_filter_h264
;
1145 enc
->nalu_sps
= radeon_enc_nalu_sps
;
1146 enc
->nalu_pps
= radeon_enc_nalu_pps
;
1147 enc
->slice_header
= radeon_enc_slice_header
;
1148 enc
->encode_params_codec_spec
= radeon_enc_encode_params_h264
;
1149 enc
->encode_headers
= radeon_enc_headers_h264
;
1150 } else if (u_reduce_video_profile(enc
->base
.profile
) == PIPE_VIDEO_FORMAT_HEVC
) {
1151 enc
->session_init
= radeon_enc_session_init_hevc
;
1152 enc
->slice_control
= radeon_enc_slice_control_hevc
;
1153 enc
->spec_misc
= radeon_enc_spec_misc_hevc
;
1154 enc
->deblocking_filter
= radeon_enc_deblocking_filter_hevc
;
1155 enc
->nalu_sps
= radeon_enc_nalu_sps_hevc
;
1156 enc
->nalu_pps
= radeon_enc_nalu_pps_hevc
;
1157 enc
->nalu_vps
= radeon_enc_nalu_vps
;
1158 enc
->nalu_aud
= radeon_enc_nalu_aud_hevc
;
1159 enc
->slice_header
= radeon_enc_slice_header_hevc
;
1160 enc
->encode_params_codec_spec
= radeon_enc_encode_params_hevc
;
1161 enc
->encode_headers
= radeon_enc_headers_hevc
;