1 /**************************************************************************
2 *
3 * Copyright 2017 Advanced Micro Devices, Inc.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 **************************************************************************/
8
9 #include "radeon_vcn_enc.h"
10 #include "ac_vcn_enc_av1_default_cdf.h"
11
12 #include "pipe/p_video_codec.h"
13 #include "radeon_video.h"
14 #include "radeonsi/si_pipe.h"
15 #include "util/u_memory.h"
16 #include "util/u_video.h"
17 #include "vl/vl_video_buffer.h"
18
19 #include <stdio.h>
20
21 static const unsigned index_to_shifts[4] = {24, 16, 8, 0};
22
23 /* set quality modes from the input */
radeon_vcn_enc_quality_modes(struct radeon_encoder * enc,struct pipe_enc_quality_modes * in)24 static void radeon_vcn_enc_quality_modes(struct radeon_encoder *enc,
25 struct pipe_enc_quality_modes *in)
26 {
27 rvcn_enc_quality_modes_t *p = &enc->enc_pic.quality_modes;
28
29 p->preset_mode = in->preset_mode > RENCODE_PRESET_MODE_HIGH_QUALITY
30 ? RENCODE_PRESET_MODE_HIGH_QUALITY
31 : in->preset_mode;
32
33 if (u_reduce_video_profile(enc->base.profile) != PIPE_VIDEO_FORMAT_AV1 &&
34 p->preset_mode == RENCODE_PRESET_MODE_HIGH_QUALITY)
35 p->preset_mode = RENCODE_PRESET_MODE_QUALITY;
36
37 p->pre_encode_mode = in->pre_encode_mode ? RENCODE_PREENCODE_MODE_4X
38 : RENCODE_PREENCODE_MODE_NONE;
39 p->vbaq_mode = in->vbaq_mode ? RENCODE_VBAQ_AUTO : RENCODE_VBAQ_NONE;
40 }
41
42 /* to process invalid frame rate */
radeon_vcn_enc_invalid_frame_rate(uint32_t * den,uint32_t * num)43 static void radeon_vcn_enc_invalid_frame_rate(uint32_t *den, uint32_t *num)
44 {
45 if (*den == 0 || *num == 0) {
46 *den = 1;
47 *num = 30;
48 }
49 }
50
radeon_vcn_per_frame_integer(uint32_t bitrate,uint32_t den,uint32_t num)51 static uint32_t radeon_vcn_per_frame_integer(uint32_t bitrate, uint32_t den, uint32_t num)
52 {
53 uint64_t rate_den = (uint64_t)bitrate * (uint64_t)den;
54
55 return (uint32_t)(rate_den/num);
56 }
57
radeon_vcn_per_frame_frac(uint32_t bitrate,uint32_t den,uint32_t num)58 static uint32_t radeon_vcn_per_frame_frac(uint32_t bitrate, uint32_t den, uint32_t num)
59 {
60 uint64_t rate_den = (uint64_t)bitrate * (uint64_t)den;
61 uint64_t remainder = rate_den % num;
62
63 return (uint32_t)((remainder << 32) / num);
64 }
65
66 /* block length for av1 and hevc is the same, 64, for avc 16 */
radeon_vcn_enc_blocks_in_frame(struct radeon_encoder * enc,uint32_t * width_in_block,uint32_t * height_in_block)67 static uint32_t radeon_vcn_enc_blocks_in_frame(struct radeon_encoder *enc,
68 uint32_t *width_in_block,
69 uint32_t *height_in_block)
70 {
71 bool is_h264 = u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC;
72 uint32_t block_length = is_h264 ? PIPE_H264_MB_SIZE : PIPE_H265_ENC_CTB_SIZE;
73
74 *width_in_block = PIPE_ALIGN_IN_BLOCK_SIZE(enc->base.width, block_length);
75 *height_in_block = PIPE_ALIGN_IN_BLOCK_SIZE(enc->base.height, block_length);
76
77 return block_length;
78 }
79
radeon_vcn_enc_get_intra_refresh_param(struct radeon_encoder * enc,bool need_filter_overlap,struct pipe_enc_intra_refresh * intra_refresh)80 static void radeon_vcn_enc_get_intra_refresh_param(struct radeon_encoder *enc,
81 bool need_filter_overlap,
82 struct pipe_enc_intra_refresh *intra_refresh)
83 {
84 uint32_t width_in_block, height_in_block;
85
86 enc->enc_pic.intra_refresh.intra_refresh_mode = RENCODE_INTRA_REFRESH_MODE_NONE;
87 /* some exceptions where intra-refresh is disabled:
88 * 1. if B frame is enabled
89 * 2. if SVC (number of temproal layers is larger than 1) is enabled
90 */
91 if (enc->enc_pic.spec_misc.b_picture_enabled || enc->enc_pic.num_temporal_layers > 1) {
92 enc->enc_pic.intra_refresh.region_size = 0;
93 enc->enc_pic.intra_refresh.offset = 0;
94 enc->enc_pic.need_sequence_header = 0;
95 return;
96 }
97
98 radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block);
99
100 switch(intra_refresh->mode) {
101 case INTRA_REFRESH_MODE_UNIT_ROWS:
102 if (intra_refresh->offset < height_in_block)
103 enc->enc_pic.intra_refresh.intra_refresh_mode
104 = RENCODE_INTRA_REFRESH_MODE_CTB_MB_ROWS;
105 break;
106 case INTRA_REFRESH_MODE_UNIT_COLUMNS:
107 if (intra_refresh->offset < width_in_block)
108 enc->enc_pic.intra_refresh.intra_refresh_mode
109 = RENCODE_INTRA_REFRESH_MODE_CTB_MB_COLUMNS;
110 break;
111 case INTRA_REFRESH_MODE_NONE:
112 default:
113 break;
114 };
115
116 /* with loop filters (avc/hevc/av1) enabled the region_size has to increase 1 to
117 * get overlapped (av1 is enabling it all the time). The region_size and offset
118 * require to be in unit of MB or CTB or SB according to different codecs.
119 */
120 if (enc->enc_pic.intra_refresh.intra_refresh_mode != RENCODE_INTRA_REFRESH_MODE_NONE) {
121 enc->enc_pic.intra_refresh.region_size = (need_filter_overlap) ?
122 intra_refresh->region_size + 1 :
123 intra_refresh->region_size;
124 enc->enc_pic.intra_refresh.offset = intra_refresh->offset;
125 enc->enc_pic.need_sequence_header = !!(intra_refresh->need_sequence_header);
126 } else {
127 enc->enc_pic.intra_refresh.region_size = 0;
128 enc->enc_pic.intra_refresh.offset = 0;
129 enc->enc_pic.need_sequence_header = 0;
130 }
131 }
132
radeon_vcn_enc_get_roi_param(struct radeon_encoder * enc,struct pipe_enc_roi * roi)133 static void radeon_vcn_enc_get_roi_param(struct radeon_encoder *enc,
134 struct pipe_enc_roi *roi)
135 {
136 bool is_av1 = u_reduce_video_profile(enc->base.profile)
137 == PIPE_VIDEO_FORMAT_AV1;
138 if (!roi->num)
139 enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_NONE;
140 else {
141 uint32_t width_in_block, height_in_block;
142 uint32_t block_length;
143 int32_t i, j, pa_format = 0;
144
145 /* rate control is using a different qp map type */
146 if (enc->enc_pic.rc_session_init.rate_control_method) {
147 enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_MAP_PA;
148 pa_format = 1;
149 }
150 else
151 enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_DELTA;
152
153 block_length = radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block);
154
155 for (i = RENCODE_QP_MAP_MAX_REGIONS; i >= roi->num; i--)
156 enc->enc_pic.enc_qp_map.map[i].is_valid = false;
157
158 /* reverse the map sequence */
159 for (j = 0; i >= 0; i--, j++) {
160 struct rvcn_enc_qp_map_region *map = &enc->enc_pic.enc_qp_map.map[j];
161 struct pipe_enc_region_in_roi *region = &roi->region[i];
162
163 map->is_valid = region->valid;
164 if (region->valid) {
165 int32_t av1_qi_value;
166 /* mapped av1 qi into the legacy qp range by dividing by 5 and
167 * rounding up in any rate control mode.
168 */
169 if (is_av1 && pa_format) {
170 if (region->qp_value > 0)
171 av1_qi_value = (region->qp_value + 2) / 5;
172 else if (region->qp_value < 0)
173 av1_qi_value = (region->qp_value - 2) / 5;
174 else
175 av1_qi_value = region->qp_value;
176 map->qp_delta = av1_qi_value;
177 } else
178 map->qp_delta = region->qp_value;
179
180 map->x_in_unit = CLAMP((region->x / block_length), 0, width_in_block - 1);
181 map->y_in_unit = CLAMP((region->y / block_length), 0, height_in_block - 1);
182 map->width_in_unit = CLAMP((region->width / block_length), 0, width_in_block);
183 map->height_in_unit = CLAMP((region->height / block_length), 0, width_in_block);
184 }
185 }
186 }
187 }
188
radeon_vcn_enc_h264_get_cropping_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)189 static void radeon_vcn_enc_h264_get_cropping_param(struct radeon_encoder *enc,
190 struct pipe_h264_enc_picture_desc *pic)
191 {
192 if (pic->seq.enc_frame_cropping_flag) {
193 enc->enc_pic.crop_left = pic->seq.enc_frame_crop_left_offset;
194 enc->enc_pic.crop_right = pic->seq.enc_frame_crop_right_offset;
195 enc->enc_pic.crop_top = pic->seq.enc_frame_crop_top_offset;
196 enc->enc_pic.crop_bottom = pic->seq.enc_frame_crop_bottom_offset;
197 } else {
198 enc->enc_pic.crop_left = 0;
199 enc->enc_pic.crop_right = (align(enc->base.width, 16) - enc->base.width) / 2;
200 enc->enc_pic.crop_top = 0;
201 enc->enc_pic.crop_bottom = (align(enc->base.height, 16) - enc->base.height) / 2;
202 }
203 }
204
radeon_vcn_enc_h264_get_dbk_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)205 static void radeon_vcn_enc_h264_get_dbk_param(struct radeon_encoder *enc,
206 struct pipe_h264_enc_picture_desc *pic)
207 {
208 enc->enc_pic.h264_deblock.disable_deblocking_filter_idc =
209 CLAMP(pic->dbk.disable_deblocking_filter_idc, 0, 2);
210 enc->enc_pic.h264_deblock.alpha_c0_offset_div2 = pic->dbk.alpha_c0_offset_div2;
211 enc->enc_pic.h264_deblock.beta_offset_div2 = pic->dbk.beta_offset_div2;
212 enc->enc_pic.h264_deblock.cb_qp_offset = pic->pic_ctrl.chroma_qp_index_offset;
213 enc->enc_pic.h264_deblock.cr_qp_offset = pic->pic_ctrl.second_chroma_qp_index_offset;
214 }
215
radeon_vcn_enc_h264_get_spec_misc_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)216 static void radeon_vcn_enc_h264_get_spec_misc_param(struct radeon_encoder *enc,
217 struct pipe_h264_enc_picture_desc *pic)
218 {
219 enc->enc_pic.spec_misc.profile_idc = u_get_h264_profile_idc(enc->base.profile);
220 if (enc->enc_pic.spec_misc.profile_idc >= PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN &&
221 enc->enc_pic.spec_misc.profile_idc != PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED)
222 enc->enc_pic.spec_misc.cabac_enable = pic->pic_ctrl.enc_cabac_enable;
223 else
224 enc->enc_pic.spec_misc.cabac_enable = false;
225
226 enc->enc_pic.spec_misc.cabac_init_idc = enc->enc_pic.spec_misc.cabac_enable ?
227 pic->pic_ctrl.enc_cabac_init_idc : 0;
228 enc->enc_pic.spec_misc.deblocking_filter_control_present_flag =
229 pic->pic_ctrl.deblocking_filter_control_present_flag;
230 enc->enc_pic.spec_misc.redundant_pic_cnt_present_flag =
231 pic->pic_ctrl.redundant_pic_cnt_present_flag;
232 enc->enc_pic.spec_misc.b_picture_enabled = !!pic->seq.max_num_reorder_frames;
233 }
234
radeon_vcn_enc_h264_get_rc_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)235 static void radeon_vcn_enc_h264_get_rc_param(struct radeon_encoder *enc,
236 struct pipe_h264_enc_picture_desc *pic)
237 {
238 uint32_t frame_rate_den, frame_rate_num;
239
240 enc->enc_pic.num_temporal_layers = pic->seq.num_temporal_layers ? pic->seq.num_temporal_layers : 1;
241 enc->enc_pic.temporal_id = 0;
242 for (int i = 0; i < enc->enc_pic.num_temporal_layers; i++) {
243 enc->enc_pic.rc_layer_init[i].target_bit_rate = pic->rate_ctrl[i].target_bitrate;
244 enc->enc_pic.rc_layer_init[i].peak_bit_rate = pic->rate_ctrl[i].peak_bitrate;
245 frame_rate_den = pic->rate_ctrl[i].frame_rate_den;
246 frame_rate_num = pic->rate_ctrl[i].frame_rate_num;
247 radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num);
248 enc->enc_pic.rc_layer_init[i].frame_rate_den = frame_rate_den;
249 enc->enc_pic.rc_layer_init[i].frame_rate_num = frame_rate_num;
250 enc->enc_pic.rc_layer_init[i].vbv_buffer_size = pic->rate_ctrl[i].vbv_buffer_size;
251 enc->enc_pic.rc_layer_init[i].avg_target_bits_per_picture =
252 radeon_vcn_per_frame_integer(pic->rate_ctrl[i].target_bitrate,
253 frame_rate_den,
254 frame_rate_num);
255 enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_integer =
256 radeon_vcn_per_frame_integer(pic->rate_ctrl[i].peak_bitrate,
257 frame_rate_den,
258 frame_rate_num);
259 enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_fractional =
260 radeon_vcn_per_frame_frac(pic->rate_ctrl[i].peak_bitrate,
261 frame_rate_den,
262 frame_rate_num);
263 }
264 enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rate_ctrl[0].vbv_buf_lv;
265 enc->enc_pic.rc_per_pic.qp = pic->quant_i_frames;
266 enc->enc_pic.rc_per_pic.min_qp_app = pic->rate_ctrl[0].min_qp;
267 enc->enc_pic.rc_per_pic.max_qp_app = pic->rate_ctrl[0].max_qp ?
268 pic->rate_ctrl[0].max_qp : 51;
269 enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rate_ctrl[0].fill_data_enable;
270 enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rate_ctrl[0].skip_frame_enable;
271 enc->enc_pic.rc_per_pic.enforce_hrd = pic->rate_ctrl[0].enforce_hrd;
272
273 switch (pic->rate_ctrl[0].rate_ctrl_method) {
274 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE:
275 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
276 break;
277 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:
278 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT:
279 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR;
280 break;
281 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:
282 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE:
283 enc->enc_pic.rc_session_init.rate_control_method =
284 RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
285 break;
286 default:
287 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
288 }
289 enc->enc_pic.rc_per_pic.max_au_size = pic->rate_ctrl[0].max_au_size;
290 }
291
radeon_vcn_enc_h264_get_vui_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)292 static void radeon_vcn_enc_h264_get_vui_param(struct radeon_encoder *enc,
293 struct pipe_h264_enc_picture_desc *pic)
294 {
295 enc->enc_pic.vui_info.vui_parameters_present_flag =
296 pic->seq.vui_parameters_present_flag;
297 enc->enc_pic.vui_info.flags.aspect_ratio_info_present_flag =
298 pic->seq.vui_flags.aspect_ratio_info_present_flag;
299 enc->enc_pic.vui_info.flags.timing_info_present_flag =
300 pic->seq.vui_flags.timing_info_present_flag;
301 enc->enc_pic.vui_info.flags.video_signal_type_present_flag =
302 pic->seq.vui_flags.video_signal_type_present_flag;
303 enc->enc_pic.vui_info.flags.colour_description_present_flag =
304 pic->seq.vui_flags.colour_description_present_flag;
305 enc->enc_pic.vui_info.flags.chroma_loc_info_present_flag =
306 pic->seq.vui_flags.chroma_loc_info_present_flag;
307 enc->enc_pic.vui_info.aspect_ratio_idc = pic->seq.aspect_ratio_idc;
308 enc->enc_pic.vui_info.sar_width = pic->seq.sar_width;
309 enc->enc_pic.vui_info.sar_height = pic->seq.sar_height;
310 enc->enc_pic.vui_info.num_units_in_tick = pic->seq.num_units_in_tick;
311 enc->enc_pic.vui_info.time_scale = pic->seq.time_scale;
312 enc->enc_pic.vui_info.video_format = pic->seq.video_format;
313 enc->enc_pic.vui_info.video_full_range_flag = pic->seq.video_full_range_flag;
314 enc->enc_pic.vui_info.colour_primaries = pic->seq.colour_primaries;
315 enc->enc_pic.vui_info.transfer_characteristics = pic->seq.transfer_characteristics;
316 enc->enc_pic.vui_info.matrix_coefficients = pic->seq.matrix_coefficients;
317 enc->enc_pic.vui_info.chroma_sample_loc_type_top_field =
318 pic->seq.chroma_sample_loc_type_top_field;
319 enc->enc_pic.vui_info.chroma_sample_loc_type_bottom_field =
320 pic->seq.chroma_sample_loc_type_bottom_field;
321 enc->enc_pic.vui_info.max_num_reorder_frames = pic->seq.max_num_reorder_frames;
322 }
323
324 /* only checking the first slice to get num of mbs in slice to
325 * determine the number of slices in this frame, only fixed MB mode
326 * is supported now, the last slice in frame could have less number of
327 * MBs.
328 */
radeon_vcn_enc_h264_get_slice_ctrl_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)329 static void radeon_vcn_enc_h264_get_slice_ctrl_param(struct radeon_encoder *enc,
330 struct pipe_h264_enc_picture_desc *pic)
331 {
332 uint32_t width_in_mb, height_in_mb, num_mbs_in_slice;
333
334 width_in_mb = PIPE_ALIGN_IN_BLOCK_SIZE(enc->base.width, PIPE_H264_MB_SIZE);
335 height_in_mb = PIPE_ALIGN_IN_BLOCK_SIZE(enc->base.height, PIPE_H264_MB_SIZE);
336
337 if (pic->slices_descriptors[0].num_macroblocks >= width_in_mb * height_in_mb ||
338 pic->slices_descriptors[0].num_macroblocks == 0)
339 num_mbs_in_slice = width_in_mb * height_in_mb;
340 else
341 num_mbs_in_slice = pic->slices_descriptors[0].num_macroblocks;
342
343 enc->enc_pic.slice_ctrl.num_mbs_per_slice = num_mbs_in_slice;
344 }
345
radeon_vcn_enc_get_output_format_param(struct radeon_encoder * enc,bool full_range)346 static void radeon_vcn_enc_get_output_format_param(struct radeon_encoder *enc, bool full_range)
347 {
348 switch (enc->enc_pic.bit_depth_luma_minus8) {
349 case 2: /* 10 bits */
350 enc->enc_pic.enc_output_format.output_color_volume = RENCODE_COLOR_VOLUME_G22_BT709;
351 enc->enc_pic.enc_output_format.output_color_range = full_range ?
352 RENCODE_COLOR_RANGE_FULL : RENCODE_COLOR_RANGE_STUDIO;
353 enc->enc_pic.enc_output_format.output_chroma_location = RENCODE_CHROMA_LOCATION_INTERSTITIAL;
354 enc->enc_pic.enc_output_format.output_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT;
355 break;
356 default: /* 8 bits */
357 enc->enc_pic.enc_output_format.output_color_volume = RENCODE_COLOR_VOLUME_G22_BT709;
358 enc->enc_pic.enc_output_format.output_color_range = full_range ?
359 RENCODE_COLOR_RANGE_FULL : RENCODE_COLOR_RANGE_STUDIO;
360 enc->enc_pic.enc_output_format.output_chroma_location = RENCODE_CHROMA_LOCATION_INTERSTITIAL;
361 enc->enc_pic.enc_output_format.output_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT;
362 break;
363 }
364 }
365
radeon_vcn_enc_get_input_format_param(struct radeon_encoder * enc,struct pipe_picture_desc * pic_base)366 static void radeon_vcn_enc_get_input_format_param(struct radeon_encoder *enc,
367 struct pipe_picture_desc *pic_base)
368 {
369 switch (pic_base->input_format) {
370 case PIPE_FORMAT_P010:
371 enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT;
372 enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_P010;
373 enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_2_0;
374 enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_YUV;
375 break;
376 case PIPE_FORMAT_B8G8R8A8_UNORM:
377 case PIPE_FORMAT_B8G8R8X8_UNORM:
378 enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT;
379 enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4;
380 enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A8R8G8B8;
381 enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB;
382 break;
383 case PIPE_FORMAT_R8G8B8A8_UNORM:
384 case PIPE_FORMAT_R8G8B8X8_UNORM:
385 enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT;
386 enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4;
387 enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A8B8G8R8;
388 enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB;
389 break;
390 case PIPE_FORMAT_NV12: /* FALL THROUGH */
391 default:
392 enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT;
393 enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_NV12;
394 enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_2_0;
395 enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_YUV;
396 break;
397 }
398
399 enc->enc_pic.enc_input_format.input_color_volume = RENCODE_COLOR_VOLUME_G22_BT709;
400 enc->enc_pic.enc_input_format.input_color_range = pic_base->input_full_range ?
401 RENCODE_COLOR_RANGE_FULL : RENCODE_COLOR_RANGE_STUDIO;
402 enc->enc_pic.enc_input_format.input_chroma_location = RENCODE_CHROMA_LOCATION_INTERSTITIAL;
403 }
404
radeon_vcn_enc_h264_get_param(struct radeon_encoder * enc,struct pipe_h264_enc_picture_desc * pic)405 static void radeon_vcn_enc_h264_get_param(struct radeon_encoder *enc,
406 struct pipe_h264_enc_picture_desc *pic)
407 {
408 bool use_filter;
409
410 enc->enc_pic.picture_type = pic->picture_type;
411 enc->enc_pic.bit_depth_luma_minus8 = 0;
412 enc->enc_pic.bit_depth_chroma_minus8 = 0;
413 radeon_vcn_enc_quality_modes(enc, &pic->quality_modes);
414 enc->enc_pic.frame_num = pic->frame_num;
415 enc->enc_pic.pic_order_cnt = pic->pic_order_cnt;
416 enc->enc_pic.pic_order_cnt_type = pic->seq.pic_order_cnt_type;
417 enc->enc_pic.ref_idx_l0 = pic->ref_idx_l0_list[0];
418 enc->enc_pic.ref_idx_l0_is_ltr = pic->l0_is_long_term[0];
419 enc->enc_pic.ref_idx_l1 = pic->ref_idx_l1_list[0];
420 enc->enc_pic.ref_idx_l1_is_ltr = pic->l1_is_long_term[0];
421 enc->enc_pic.not_referenced = pic->not_referenced;
422 enc->enc_pic.is_idr = (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR);
423 enc->enc_pic.is_ltr = pic->is_ltr;
424 enc->enc_pic.ltr_idx = pic->is_ltr ? pic->ltr_index : 0;
425 radeon_vcn_enc_h264_get_cropping_param(enc, pic);
426 radeon_vcn_enc_h264_get_dbk_param(enc, pic);
427 radeon_vcn_enc_h264_get_rc_param(enc, pic);
428 radeon_vcn_enc_h264_get_spec_misc_param(enc, pic);
429 radeon_vcn_enc_h264_get_vui_param(enc, pic);
430 radeon_vcn_enc_h264_get_slice_ctrl_param(enc, pic);
431 radeon_vcn_enc_get_input_format_param(enc, &pic->base);
432 radeon_vcn_enc_get_output_format_param(enc, pic->seq.video_full_range_flag);
433
434 use_filter = enc->enc_pic.h264_deblock.disable_deblocking_filter_idc != 1;
435 radeon_vcn_enc_get_intra_refresh_param(enc, use_filter, &pic->intra_refresh);
436 radeon_vcn_enc_get_roi_param(enc, &pic->roi);
437 }
438
radeon_vcn_enc_hevc_get_cropping_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)439 static void radeon_vcn_enc_hevc_get_cropping_param(struct radeon_encoder *enc,
440 struct pipe_h265_enc_picture_desc *pic)
441 {
442 if (pic->seq.conformance_window_flag) {
443 enc->enc_pic.crop_left = pic->seq.conf_win_left_offset;
444 enc->enc_pic.crop_right = pic->seq.conf_win_right_offset;
445 enc->enc_pic.crop_top = pic->seq.conf_win_top_offset;
446 enc->enc_pic.crop_bottom = pic->seq.conf_win_bottom_offset;
447 } else {
448 enc->enc_pic.crop_left = 0;
449 enc->enc_pic.crop_right = (align(enc->base.width, 16) - enc->base.width) / 2;
450 enc->enc_pic.crop_top = 0;
451 enc->enc_pic.crop_bottom = (align(enc->base.height, 16) - enc->base.height) / 2;
452 }
453 }
454
radeon_vcn_enc_hevc_get_dbk_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)455 static void radeon_vcn_enc_hevc_get_dbk_param(struct radeon_encoder *enc,
456 struct pipe_h265_enc_picture_desc *pic)
457 {
458 enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled =
459 pic->slice.slice_loop_filter_across_slices_enabled_flag;
460 enc->enc_pic.hevc_deblock.deblocking_filter_disabled =
461 pic->slice.slice_deblocking_filter_disabled_flag;
462 enc->enc_pic.hevc_deblock.beta_offset_div2 = pic->slice.slice_beta_offset_div2;
463 enc->enc_pic.hevc_deblock.tc_offset_div2 = pic->slice.slice_tc_offset_div2;
464 enc->enc_pic.hevc_deblock.cb_qp_offset = pic->slice.slice_cb_qp_offset;
465 enc->enc_pic.hevc_deblock.cr_qp_offset = pic->slice.slice_cr_qp_offset;
466 }
467
radeon_vcn_enc_hevc_get_spec_misc_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)468 static void radeon_vcn_enc_hevc_get_spec_misc_param(struct radeon_encoder *enc,
469 struct pipe_h265_enc_picture_desc *pic)
470 {
471 enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 =
472 pic->seq.log2_min_luma_coding_block_size_minus3;
473 enc->enc_pic.hevc_spec_misc.amp_disabled = !pic->seq.amp_enabled_flag;
474 enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled =
475 pic->seq.strong_intra_smoothing_enabled_flag;
476 enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag =
477 pic->pic.constrained_intra_pred_flag;
478 enc->enc_pic.hevc_spec_misc.cabac_init_flag = pic->slice.cabac_init_flag;
479 enc->enc_pic.hevc_spec_misc.half_pel_enabled = 1;
480 enc->enc_pic.hevc_spec_misc.quarter_pel_enabled = 1;
481 }
482
radeon_vcn_enc_hevc_get_rc_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)483 static void radeon_vcn_enc_hevc_get_rc_param(struct radeon_encoder *enc,
484 struct pipe_h265_enc_picture_desc *pic)
485 {
486 uint32_t frame_rate_den, frame_rate_num;
487
488 enc->enc_pic.rc_layer_init[0].target_bit_rate = pic->rc.target_bitrate;
489 enc->enc_pic.rc_layer_init[0].peak_bit_rate = pic->rc.peak_bitrate;
490 frame_rate_den = pic->rc.frame_rate_den;
491 frame_rate_num = pic->rc.frame_rate_num;
492 radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num);
493 enc->enc_pic.rc_layer_init[0].frame_rate_den = frame_rate_den;
494 enc->enc_pic.rc_layer_init[0].frame_rate_num = frame_rate_num;
495 enc->enc_pic.rc_layer_init[0].vbv_buffer_size = pic->rc.vbv_buffer_size;
496 enc->enc_pic.rc_layer_init[0].avg_target_bits_per_picture =
497 radeon_vcn_per_frame_integer(pic->rc.target_bitrate,
498 frame_rate_den,
499 frame_rate_num);
500 enc->enc_pic.rc_layer_init[0].peak_bits_per_picture_integer =
501 radeon_vcn_per_frame_integer(pic->rc.peak_bitrate,
502 frame_rate_den,
503 frame_rate_num);
504 enc->enc_pic.rc_layer_init[0].peak_bits_per_picture_fractional =
505 radeon_vcn_per_frame_frac(pic->rc.peak_bitrate,
506 frame_rate_den,
507 frame_rate_num);
508 enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc.vbv_buf_lv;
509 enc->enc_pic.rc_per_pic.qp = pic->rc.quant_i_frames;
510 enc->enc_pic.rc_per_pic.min_qp_app = pic->rc.min_qp;
511 enc->enc_pic.rc_per_pic.max_qp_app = pic->rc.max_qp ? pic->rc.max_qp : 51;
512 enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc.fill_data_enable;
513 enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rc.skip_frame_enable;
514 enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc.enforce_hrd;
515 switch (pic->rc.rate_ctrl_method) {
516 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE:
517 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
518 break;
519 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:
520 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT:
521 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR;
522 break;
523 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:
524 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE:
525 enc->enc_pic.rc_session_init.rate_control_method =
526 RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
527 break;
528 default:
529 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
530 }
531 enc->enc_pic.rc_per_pic.max_au_size = pic->rc.max_au_size;
532 }
533
radeon_vcn_enc_hevc_get_vui_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)534 static void radeon_vcn_enc_hevc_get_vui_param(struct radeon_encoder *enc,
535 struct pipe_h265_enc_picture_desc *pic)
536 {
537 enc->enc_pic.vui_info.vui_parameters_present_flag = pic->seq.vui_parameters_present_flag;
538 enc->enc_pic.vui_info.flags.aspect_ratio_info_present_flag =
539 pic->seq.vui_flags.aspect_ratio_info_present_flag;
540 enc->enc_pic.vui_info.flags.timing_info_present_flag =
541 pic->seq.vui_flags.timing_info_present_flag;
542 enc->enc_pic.vui_info.flags.video_signal_type_present_flag =
543 pic->seq.vui_flags.video_signal_type_present_flag;
544 enc->enc_pic.vui_info.flags.colour_description_present_flag =
545 pic->seq.vui_flags.colour_description_present_flag;
546 enc->enc_pic.vui_info.flags.chroma_loc_info_present_flag =
547 pic->seq.vui_flags.chroma_loc_info_present_flag;
548 enc->enc_pic.vui_info.aspect_ratio_idc = pic->seq.aspect_ratio_idc;
549 enc->enc_pic.vui_info.sar_width = pic->seq.sar_width;
550 enc->enc_pic.vui_info.sar_height = pic->seq.sar_height;
551 enc->enc_pic.vui_info.num_units_in_tick = pic->seq.num_units_in_tick;
552 enc->enc_pic.vui_info.time_scale = pic->seq.time_scale;
553 enc->enc_pic.vui_info.video_format = pic->seq.video_format;
554 enc->enc_pic.vui_info.video_full_range_flag = pic->seq.video_full_range_flag;
555 enc->enc_pic.vui_info.colour_primaries = pic->seq.colour_primaries;
556 enc->enc_pic.vui_info.transfer_characteristics = pic->seq.transfer_characteristics;
557 enc->enc_pic.vui_info.matrix_coefficients = pic->seq.matrix_coefficients;
558 enc->enc_pic.vui_info.chroma_sample_loc_type_top_field =
559 pic->seq.chroma_sample_loc_type_top_field;
560 enc->enc_pic.vui_info.chroma_sample_loc_type_bottom_field =
561 pic->seq.chroma_sample_loc_type_bottom_field;
562 }
563
564 /* only checking the first slice to get num of ctbs in slice to
565 * determine the number of slices in this frame, only fixed CTB mode
566 * is supported now, the last slice in frame could have less number of
567 * ctbs.
568 */
radeon_vcn_enc_hevc_get_slice_ctrl_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)569 static void radeon_vcn_enc_hevc_get_slice_ctrl_param(struct radeon_encoder *enc,
570 struct pipe_h265_enc_picture_desc *pic)
571 {
572 uint32_t width_in_ctb, height_in_ctb, num_ctbs_in_slice;
573
574 width_in_ctb = PIPE_ALIGN_IN_BLOCK_SIZE(pic->seq.pic_width_in_luma_samples,
575 PIPE_H265_ENC_CTB_SIZE);
576 height_in_ctb = PIPE_ALIGN_IN_BLOCK_SIZE(pic->seq.pic_height_in_luma_samples,
577 PIPE_H265_ENC_CTB_SIZE);
578
579 if (pic->slices_descriptors[0].num_ctu_in_slice >= width_in_ctb * height_in_ctb ||
580 pic->slices_descriptors[0].num_ctu_in_slice == 0)
581 num_ctbs_in_slice = width_in_ctb * height_in_ctb;
582 else
583 num_ctbs_in_slice = pic->slices_descriptors[0].num_ctu_in_slice;
584
585 enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice =
586 num_ctbs_in_slice;
587
588 enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment =
589 num_ctbs_in_slice;
590 }
591
radeon_vcn_enc_hevc_get_param(struct radeon_encoder * enc,struct pipe_h265_enc_picture_desc * pic)592 static void radeon_vcn_enc_hevc_get_param(struct radeon_encoder *enc,
593 struct pipe_h265_enc_picture_desc *pic)
594 {
595 enc->enc_pic.picture_type = pic->picture_type;
596 enc->enc_pic.frame_num = pic->frame_num;
597 radeon_vcn_enc_quality_modes(enc, &pic->quality_modes);
598 enc->enc_pic.pic_order_cnt_type = pic->pic_order_cnt_type;
599 enc->enc_pic.ref_idx_l0 = pic->ref_idx_l0_list[0];
600 enc->enc_pic.ref_idx_l1 = pic->ref_idx_l1_list[0];
601 enc->enc_pic.not_referenced = pic->not_referenced;
602 enc->enc_pic.is_idr = (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR);
603 radeon_vcn_enc_hevc_get_cropping_param(enc, pic);
604 enc->enc_pic.general_tier_flag = pic->seq.general_tier_flag;
605 enc->enc_pic.general_profile_idc = pic->seq.general_profile_idc;
606 enc->enc_pic.general_level_idc = pic->seq.general_level_idc;
607 /* use fixed value for max_poc until new feature added */
608 enc->enc_pic.max_poc = 16;
609 enc->enc_pic.log2_max_poc = 4;
610 enc->enc_pic.num_temporal_layers = 1;
611 enc->enc_pic.pic_order_cnt = pic->pic_order_cnt % enc->enc_pic.max_poc;
612 enc->enc_pic.chroma_format_idc = pic->seq.chroma_format_idc;
613 enc->enc_pic.pic_width_in_luma_samples = pic->seq.pic_width_in_luma_samples;
614 enc->enc_pic.pic_height_in_luma_samples = pic->seq.pic_height_in_luma_samples;
615 enc->enc_pic.log2_diff_max_min_luma_coding_block_size =
616 pic->seq.log2_diff_max_min_luma_coding_block_size;
617 enc->enc_pic.log2_min_transform_block_size_minus2 =
618 pic->seq.log2_min_transform_block_size_minus2;
619 enc->enc_pic.log2_diff_max_min_transform_block_size =
620 pic->seq.log2_diff_max_min_transform_block_size;
621
622 /* To fix incorrect hardcoded values set by player
623 * log2_diff_max_min_luma_coding_block_size = log2(64) - (log2_min_luma_coding_block_size_minus3 + 3)
624 * max_transform_hierarchy_depth_inter = log2_diff_max_min_luma_coding_block_size + 1
625 * max_transform_hierarchy_depth_intra = log2_diff_max_min_luma_coding_block_size + 1
626 */
627 enc->enc_pic.max_transform_hierarchy_depth_inter =
628 6 - (pic->seq.log2_min_luma_coding_block_size_minus3 + 3) + 1;
629 enc->enc_pic.max_transform_hierarchy_depth_intra =
630 enc->enc_pic.max_transform_hierarchy_depth_inter;
631
632 enc->enc_pic.log2_parallel_merge_level_minus2 = pic->pic.log2_parallel_merge_level_minus2;
633 enc->enc_pic.bit_depth_luma_minus8 = pic->seq.bit_depth_luma_minus8;
634 enc->enc_pic.bit_depth_chroma_minus8 = pic->seq.bit_depth_chroma_minus8;
635 enc->enc_pic.nal_unit_type = pic->pic.nal_unit_type;
636 enc->enc_pic.max_num_merge_cand = pic->slice.max_num_merge_cand;
637 enc->enc_pic.sample_adaptive_offset_enabled_flag =
638 pic->seq.sample_adaptive_offset_enabled_flag;
639 enc->enc_pic.pcm_enabled_flag = pic->seq.pcm_enabled_flag;
640 enc->enc_pic.sps_temporal_mvp_enabled_flag = pic->seq.sps_temporal_mvp_enabled_flag;
641 radeon_vcn_enc_hevc_get_spec_misc_param(enc, pic);
642 radeon_vcn_enc_hevc_get_dbk_param(enc, pic);
643 radeon_vcn_enc_hevc_get_rc_param(enc, pic);
644 radeon_vcn_enc_hevc_get_vui_param(enc, pic);
645 radeon_vcn_enc_hevc_get_slice_ctrl_param(enc, pic);
646 radeon_vcn_enc_get_input_format_param(enc, &pic->base);
647 radeon_vcn_enc_get_output_format_param(enc, pic->seq.video_full_range_flag);
648 radeon_vcn_enc_get_intra_refresh_param(enc,
649 !(enc->enc_pic.hevc_deblock.deblocking_filter_disabled),
650 &pic->intra_refresh);
651 radeon_vcn_enc_get_roi_param(enc, &pic->roi);
652 }
653
radeon_vcn_enc_av1_get_spec_misc_param(struct radeon_encoder * enc,struct pipe_av1_enc_picture_desc * pic)654 static void radeon_vcn_enc_av1_get_spec_misc_param(struct radeon_encoder *enc,
655 struct pipe_av1_enc_picture_desc *pic)
656 {
657 enc->enc_pic.av1_spec_misc.cdef_mode = pic->seq.seq_bits.enable_cdef;
658 enc->enc_pic.av1_spec_misc.disable_cdf_update = pic->disable_cdf_update;
659 enc->enc_pic.av1_spec_misc.disable_frame_end_update_cdf = pic->disable_frame_end_update_cdf;
660 enc->enc_pic.av1_spec_misc.num_tiles_per_picture = pic->num_tiles_in_pic;
661 enc->enc_pic.av1_spec_misc.palette_mode_enable = pic->palette_mode_enable;
662
663 if (enc->enc_pic.disable_screen_content_tools) {
664 enc->enc_pic.force_integer_mv = 0;
665 enc->enc_pic.av1_spec_misc.palette_mode_enable = 0;
666 }
667
668 if (enc->enc_pic.force_integer_mv)
669 enc->enc_pic.av1_spec_misc.mv_precision = RENCODE_AV1_MV_PRECISION_FORCE_INTEGER_MV;
670 else
671 enc->enc_pic.av1_spec_misc.mv_precision = RENCODE_AV1_MV_PRECISION_ALLOW_HIGH_PRECISION;
672 }
673
radeon_vcn_enc_av1_timing_info(struct radeon_encoder * enc,struct pipe_av1_enc_picture_desc * pic)674 static void radeon_vcn_enc_av1_timing_info(struct radeon_encoder *enc,
675 struct pipe_av1_enc_picture_desc *pic)
676 {
677 if (pic->seq.seq_bits.timing_info_present_flag)
678 {
679 enc->enc_pic.av1_timing_info.num_units_in_display_tick =
680 pic->seq.num_units_in_display_tick;
681 enc->enc_pic.av1_timing_info.time_scale = pic->seq.time_scale;
682 enc->enc_pic.av1_timing_info.num_tick_per_picture_minus1 =
683 pic->seq.num_tick_per_picture_minus1;
684 }
685 }
686
radeon_vcn_enc_av1_color_description(struct radeon_encoder * enc,struct pipe_av1_enc_picture_desc * pic)687 static void radeon_vcn_enc_av1_color_description(struct radeon_encoder *enc,
688 struct pipe_av1_enc_picture_desc *pic)
689 {
690 if (pic->seq.seq_bits.color_description_present_flag)
691 {
692 enc->enc_pic.av1_color_description.color_primaries = pic->seq.color_config.color_primaries;
693 enc->enc_pic.av1_color_description.transfer_characteristics = pic->seq.color_config.transfer_characteristics;
694 enc->enc_pic.av1_color_description.maxtrix_coefficients = pic->seq.color_config.matrix_coefficients;
695 }
696 enc->enc_pic.av1_color_description.color_range = pic->seq.color_config.color_range;
697 enc->enc_pic.av1_color_description.chroma_sample_position = pic->seq.color_config.chroma_sample_position;
698 }
699
radeon_vcn_enc_av1_get_rc_param(struct radeon_encoder * enc,struct pipe_av1_enc_picture_desc * pic)700 static void radeon_vcn_enc_av1_get_rc_param(struct radeon_encoder *enc,
701 struct pipe_av1_enc_picture_desc *pic)
702 {
703 uint32_t frame_rate_den, frame_rate_num;
704
705 for (int i = 0; i < ARRAY_SIZE(enc->enc_pic.rc_layer_init); i++) {
706 enc->enc_pic.rc_layer_init[i].target_bit_rate = pic->rc[i].target_bitrate;
707 enc->enc_pic.rc_layer_init[i].peak_bit_rate = pic->rc[i].peak_bitrate;
708 frame_rate_den = pic->rc[i].frame_rate_den;
709 frame_rate_num = pic->rc[i].frame_rate_num;
710 radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num);
711 enc->enc_pic.rc_layer_init[i].frame_rate_den = frame_rate_den;
712 enc->enc_pic.rc_layer_init[i].frame_rate_num = frame_rate_num;
713 enc->enc_pic.rc_layer_init[i].vbv_buffer_size = pic->rc[i].vbv_buffer_size;
714 enc->enc_pic.rc_layer_init[i].avg_target_bits_per_picture =
715 radeon_vcn_per_frame_integer(pic->rc[i].target_bitrate,
716 frame_rate_den,
717 frame_rate_num);
718 enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_integer =
719 radeon_vcn_per_frame_integer(pic->rc[i].peak_bitrate,
720 frame_rate_den,
721 frame_rate_num);
722 enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_fractional =
723 radeon_vcn_per_frame_frac(pic->rc[i].peak_bitrate,
724 frame_rate_den,
725 frame_rate_num);
726 }
727 enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc[0].vbv_buf_lv;
728 enc->enc_pic.rc_per_pic.qp = pic->rc[0].qp;
729 enc->enc_pic.rc_per_pic.min_qp_app = pic->rc[0].min_qp ? pic->rc[0].min_qp : 1;
730 enc->enc_pic.rc_per_pic.max_qp_app = pic->rc[0].max_qp ? pic->rc[0].max_qp : 255;
731 enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc[0].fill_data_enable;
732 enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rc[0].skip_frame_enable;
733 enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc[0].enforce_hrd;
734 switch (pic->rc[0].rate_ctrl_method) {
735 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE:
736 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
737 break;
738 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:
739 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT:
740 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR;
741 break;
742 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:
743 case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE:
744 enc->enc_pic.rc_session_init.rate_control_method =
745 RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
746 break;
747 default:
748 enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE;
749 }
750 enc->enc_pic.rc_per_pic.max_au_size = pic->rc[0].max_au_size;
751 }
752
radeon_vcn_enc_av1_get_param(struct radeon_encoder * enc,struct pipe_av1_enc_picture_desc * pic)753 static void radeon_vcn_enc_av1_get_param(struct radeon_encoder *enc,
754 struct pipe_av1_enc_picture_desc *pic)
755 {
756 struct radeon_enc_pic *enc_pic = &enc->enc_pic;
757 enc_pic->frame_type = pic->frame_type;
758 enc_pic->frame_num = pic->frame_num;
759 enc_pic->bit_depth_luma_minus8 = enc_pic->bit_depth_chroma_minus8 =
760 pic->seq.bit_depth_minus8;
761 enc_pic->pic_width_in_luma_samples = pic->seq.pic_width_in_luma_samples;
762 enc_pic->pic_height_in_luma_samples = pic->seq.pic_height_in_luma_samples;
763 enc_pic->general_profile_idc = pic->seq.profile;
764 enc_pic->general_level_idc = pic->seq.level;
765 enc_pic->general_tier_flag = pic->seq.tier;
766
767 enc_pic->num_temporal_layers =
768 pic->seq.num_temporal_layers <= RENCODE_MAX_NUM_TEMPORAL_LAYERS ?
769 pic->seq.num_temporal_layers : RENCODE_MAX_NUM_TEMPORAL_LAYERS;
770
771 /* 1, 2 layer needs 1 reference, and 3, 4 layer needs 2 references */
772 enc->base.max_references = (enc_pic->num_temporal_layers + 1) / 2
773 + RENCODE_VCN4_AV1_MAX_NUM_LTR;
774 for (int i = 0; i < RENCDOE_AV1_REFS_PER_FRAME; i++)
775 enc_pic->av1_ref_frame_idx[i] = pic->ref_frame_idx[i];
776
777 for (int i = 0; i < RENCDOE_AV1_NUM_REF_FRAMES; i++)
778 enc_pic->av1_ref_list[i] = pic->ref_list[i];
779
780 enc_pic->av1_recon_frame = pic->recon_frame;
781 enc_pic->av1_ref_frame_ctrl_l0 = pic->ref_frame_ctrl_l0;
782
783 radeon_vcn_enc_quality_modes(enc, &pic->quality_modes);
784 enc_pic->frame_id_numbers_present = pic->seq.seq_bits.frame_id_number_present_flag;
785 enc_pic->enable_error_resilient_mode = pic->error_resilient_mode;
786 enc_pic->force_integer_mv = pic->force_integer_mv;
787 enc_pic->enable_order_hint = pic->seq.seq_bits.enable_order_hint;
788 enc_pic->order_hint_bits = pic->seq.order_hint_bits;
789 enc_pic->enable_render_size = pic->enable_render_size;
790 enc_pic->render_width = pic->render_width;
791 enc_pic->render_height = pic->render_height;
792 enc_pic->enable_color_description = pic->seq.seq_bits.color_description_present_flag;
793 enc_pic->timing_info_present = pic->seq.seq_bits.timing_info_present_flag;
794 enc_pic->timing_info_equal_picture_interval = pic->seq.seq_bits.equal_picture_interval;
795 enc_pic->disable_screen_content_tools = !pic->allow_screen_content_tools;
796 enc_pic->is_obu_frame = pic->enable_frame_obu;
797 enc_pic->need_av1_seq = (pic->frame_type == PIPE_AV1_ENC_FRAME_TYPE_KEY);
798 enc_pic->av1_mark_long_term_reference = pic->long_term_reference;
799
800 radeon_vcn_enc_av1_get_spec_misc_param(enc, pic);
801 radeon_vcn_enc_av1_timing_info(enc, pic);
802 radeon_vcn_enc_av1_color_description(enc, pic);
803 radeon_vcn_enc_av1_get_rc_param(enc, pic);
804 radeon_vcn_enc_get_input_format_param(enc, &pic->base);
805 radeon_vcn_enc_get_output_format_param(enc, pic->seq.color_config.color_range);
806 /* loop filter enabled all the time */
807 radeon_vcn_enc_get_intra_refresh_param(enc,
808 true,
809 &pic->intra_refresh);
810 radeon_vcn_enc_get_roi_param(enc, &pic->roi);
811 }
812
radeon_vcn_enc_get_param(struct radeon_encoder * enc,struct pipe_picture_desc * picture)813 static void radeon_vcn_enc_get_param(struct radeon_encoder *enc, struct pipe_picture_desc *picture)
814 {
815 if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC)
816 radeon_vcn_enc_h264_get_param(enc, (struct pipe_h264_enc_picture_desc *)picture);
817 else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_HEVC)
818 radeon_vcn_enc_hevc_get_param(enc, (struct pipe_h265_enc_picture_desc *)picture);
819 else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_AV1)
820 radeon_vcn_enc_av1_get_param(enc, (struct pipe_av1_enc_picture_desc *)picture);
821 }
822
flush(struct radeon_encoder * enc)823 static void flush(struct radeon_encoder *enc)
824 {
825 enc->ws->cs_flush(&enc->cs, PIPE_FLUSH_ASYNC, NULL);
826 }
827
radeon_enc_flush(struct pipe_video_codec * encoder)828 static void radeon_enc_flush(struct pipe_video_codec *encoder)
829 {
830 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
831 flush(enc);
832 }
833
radeon_enc_cs_flush(void * ctx,unsigned flags,struct pipe_fence_handle ** fence)834 static void radeon_enc_cs_flush(void *ctx, unsigned flags, struct pipe_fence_handle **fence)
835 {
836 // just ignored
837 }
838
839 /* configure reconstructed picture offset */
radeon_enc_rec_offset(rvcn_enc_reconstructed_picture_t * recon,uint32_t * offset,uint32_t luma_size,uint32_t chroma_size,bool is_av1)840 static void radeon_enc_rec_offset(rvcn_enc_reconstructed_picture_t *recon,
841 uint32_t *offset,
842 uint32_t luma_size,
843 uint32_t chroma_size,
844 bool is_av1)
845 {
846 if (offset) {
847 recon->luma_offset = *offset;
848 *offset += luma_size;
849 recon->chroma_offset = *offset;
850 *offset += chroma_size;
851 if (is_av1) {
852 recon->av1.av1_cdf_frame_context_offset = *offset;
853 *offset += RENCODE_AV1_FRAME_CONTEXT_CDF_TABLE_SIZE;
854 recon->av1.av1_cdef_algorithm_context_offset = *offset;
855 *offset += RENCODE_AV1_CDEF_ALGORITHM_FRAME_CONTEXT_SIZE;
856 }
857 } else {
858 recon->luma_offset = 0;
859 recon->chroma_offset = 0;
860 recon->av1.av1_cdf_frame_context_offset = 0;
861 recon->av1.av1_cdef_algorithm_context_offset = 0;
862 }
863 }
864
setup_cdf(struct radeon_encoder * enc)865 static int setup_cdf(struct radeon_encoder *enc)
866 {
867 unsigned char *p_cdf = NULL;
868
869 if (!enc->cdf ||
870 !si_vid_create_buffer(enc->screen,
871 enc->cdf,
872 VCN_ENC_AV1_DEFAULT_CDF_SIZE,
873 PIPE_USAGE_DYNAMIC)) {
874 RVID_ERR("Can't create CDF buffer.\n");
875 goto error;
876 }
877
878 p_cdf = enc->ws->buffer_map(enc->ws,
879 enc->cdf->res->buf,
880 &enc->cs,
881 PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY);
882 if (!p_cdf)
883 goto error;
884
885 memcpy(p_cdf, rvcn_av1_cdf_default_table, VCN_ENC_AV1_DEFAULT_CDF_SIZE);
886 enc->ws->buffer_unmap(enc->ws, enc->cdf->res->buf);
887
888 return 0;
889
890 error:
891 return -1;
892 }
893
setup_dpb(struct radeon_encoder * enc)894 static int setup_dpb(struct radeon_encoder *enc)
895 {
896 bool is_h264 = u_reduce_video_profile(enc->base.profile)
897 == PIPE_VIDEO_FORMAT_MPEG4_AVC;
898 bool is_av1 = u_reduce_video_profile(enc->base.profile)
899 == PIPE_VIDEO_FORMAT_AV1;
900 uint32_t rec_alignment = is_h264 ? 16 : 64;
901 uint32_t aligned_width = align(enc->base.width, rec_alignment);
902 uint32_t aligned_height = align(enc->base.height, rec_alignment);
903 uint32_t pitch = align(aligned_width, enc->alignment);
904 uint32_t num_reconstructed_pictures = enc->base.max_references + 1;
905 uint32_t luma_size, chroma_size, offset;
906 struct radeon_enc_pic *enc_pic = &enc->enc_pic;
907 int i;
908 uint32_t aligned_dpb_height = MAX2(256, aligned_height);
909
910 luma_size = align(pitch * aligned_dpb_height , enc->alignment);
911 chroma_size = align(luma_size / 2 , enc->alignment);
912 if (enc_pic->bit_depth_luma_minus8 || enc_pic->bit_depth_chroma_minus8) {
913 luma_size *= 2;
914 chroma_size *= 2;
915 }
916
917 assert(num_reconstructed_pictures <= RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES);
918
919 enc_pic->ctx_buf.rec_luma_pitch = pitch;
920 enc_pic->ctx_buf.rec_chroma_pitch = pitch;
921 enc_pic->ctx_buf.pre_encode_picture_luma_pitch = pitch;
922 enc_pic->ctx_buf.pre_encode_picture_chroma_pitch = pitch;
923
924 offset = 0;
925 if (enc_pic->quality_modes.pre_encode_mode) {
926 uint32_t pre_size = DIV_ROUND_UP((aligned_width >> 2), rec_alignment) *
927 DIV_ROUND_UP((aligned_height >> 2), rec_alignment);
928 uint32_t full_size = DIV_ROUND_UP(aligned_width, rec_alignment) *
929 DIV_ROUND_UP(aligned_height, rec_alignment);
930 pre_size = align(pre_size, 4);
931 full_size = align(full_size, 4);
932
933 enc_pic->ctx_buf.two_pass_search_center_map_offset = offset;
934 if (is_h264 && !enc_pic->spec_misc.b_picture_enabled)
935 offset += align((pre_size * 4 + full_size) * sizeof(uint32_t), enc->alignment);
936 else if (!is_h264)
937 offset += align((pre_size * 52 + full_size) * sizeof(uint32_t), enc->alignment);
938 } else
939 enc_pic->ctx_buf.two_pass_search_center_map_offset = 0;
940
941 if (is_av1) {
942 enc_pic->ctx_buf.av1.av1_sdb_intermedidate_context_offset = offset;
943 offset += RENCODE_AV1_SDB_FRAME_CONTEXT_SIZE;
944 }
945
946 for (i = 0; i < num_reconstructed_pictures; i++) {
947 radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i],
948 &offset, luma_size, chroma_size, is_av1);
949
950 if (enc_pic->quality_modes.pre_encode_mode)
951 radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i],
952 &offset, luma_size, chroma_size, is_av1);
953 }
954
955 for (; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
956 radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i],
957 NULL, 0, 0, false);
958 if (enc_pic->quality_modes.pre_encode_mode)
959 radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i],
960 NULL, 0, 0, false);
961 }
962
963 if (enc_pic->quality_modes.pre_encode_mode) {
964 enc_pic->ctx_buf.pre_encode_input_picture.rgb.red_offset = offset;
965 offset += luma_size;
966 enc_pic->ctx_buf.pre_encode_input_picture.rgb.green_offset = offset;
967 offset += luma_size;
968 enc_pic->ctx_buf.pre_encode_input_picture.rgb.blue_offset = offset;
969 offset += luma_size;
970 }
971
972 enc_pic->ctx_buf.num_reconstructed_pictures = num_reconstructed_pictures;
973 enc->max_ltr_idx = 0;
974
975 if (enc_pic->spec_misc.b_picture_enabled) {
976 enc_pic->ctx_buf.colloc_buffer_offset = offset;
977 offset += (align((aligned_width / 16), 64) / 2) * (aligned_height / 16);
978 } else
979 enc_pic->ctx_buf.colloc_buffer_offset = 0;
980
981 enc->dpb_size = offset;
982
983 return offset;
984 }
985
986 /* each block (MB/CTB/SB) has one QP/QI value */
roi_buffer_size(struct radeon_encoder * enc)987 static uint32_t roi_buffer_size(struct radeon_encoder *enc)
988 {
989 uint32_t width_in_block, height_in_block;
990
991 radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block);
992
993 return width_in_block * height_in_block * sizeof(uint32_t);
994 }
995
arrange_qp_map(uint32_t * start,struct rvcn_enc_qp_map_region * map,uint32_t width_in_block,uint32_t height_in_block)996 static void arrange_qp_map(uint32_t *start,
997 struct rvcn_enc_qp_map_region *map,
998 uint32_t width_in_block,
999 uint32_t height_in_block)
1000 {
1001 uint32_t i, j;
1002 uint32_t offset;
1003 uint32_t num_in_x = MIN2(map->x_in_unit + map->width_in_unit, width_in_block)
1004 - map->x_in_unit;
1005 uint32_t num_in_y = MIN2(map->y_in_unit + map->height_in_unit, height_in_block)
1006 - map->y_in_unit;;
1007
1008 for (j = 0; j < num_in_y; j++) {
1009 for (i = 0; i < num_in_x; i++) {
1010 offset = map->x_in_unit + i + (map->y_in_unit + j) * width_in_block;
1011 *(start + offset) = (int32_t)map->qp_delta;
1012 }
1013 }
1014 }
1015
1016 /* Arrange roi map values according to the input regions.
1017 * The arrangment will consider the lower sequence region
1018 * higher priority and that could overlap the higher sequence
1019 * map region. */
generate_roi_map(struct radeon_encoder * enc)1020 static int generate_roi_map(struct radeon_encoder *enc)
1021 {
1022 uint32_t width_in_block, height_in_block;
1023 uint32_t i;
1024 uint32_t *p_roi = NULL;
1025
1026 radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block);
1027
1028 assert (enc->roi_size >= width_in_block * height_in_block);
1029 p_roi = (uint32_t *)enc->ws->buffer_map(enc->ws,
1030 enc->roi->res->buf,
1031 &enc->cs,
1032 PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY);
1033 if (!p_roi)
1034 goto error;
1035
1036 memset(p_roi, 0, width_in_block * height_in_block * sizeof(uint32_t));
1037
1038 for (i = 0; i < ARRAY_SIZE(enc->enc_pic.enc_qp_map.map); i++) {
1039 struct rvcn_enc_qp_map_region *map = &enc->enc_pic.enc_qp_map.map[i];
1040 if (map->is_valid)
1041 arrange_qp_map(p_roi, map, width_in_block, height_in_block);
1042 }
1043
1044 enc->ws->buffer_unmap(enc->ws, enc->roi->res->buf);
1045 return 0;
1046 error:
1047 return -1;
1048 }
1049
radeon_enc_begin_frame(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_picture_desc * picture)1050 static void radeon_enc_begin_frame(struct pipe_video_codec *encoder,
1051 struct pipe_video_buffer *source,
1052 struct pipe_picture_desc *picture)
1053 {
1054 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1055 struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
1056 enc->need_rate_control = false;
1057
1058 if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
1059 struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;
1060 enc->need_rate_control =
1061 (enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rate_ctrl[0].target_bitrate) ||
1062 (enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rate_ctrl[0].frame_rate_num) ||
1063 (enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rate_ctrl[0].frame_rate_den);
1064 } else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_HEVC) {
1065 struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;
1066 enc->need_rate_control =
1067 (enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rc.target_bitrate) ||
1068 (enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rc.frame_rate_num) ||
1069 (enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rc.frame_rate_den);
1070 } else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_AV1) {
1071 struct pipe_av1_enc_picture_desc *pic = (struct pipe_av1_enc_picture_desc *)picture;
1072 enc->need_rate_control =
1073 (enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rc[0].target_bitrate) ||
1074 (enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rc[0].frame_rate_num) ||
1075 (enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rc[0].frame_rate_den);
1076
1077 if (!enc->cdf) {
1078 enc->cdf = CALLOC_STRUCT(rvid_buffer);
1079 if (setup_cdf(enc)) {
1080 RVID_ERR("Can't create cdf buffer.\n");
1081 goto error;
1082 }
1083 }
1084 }
1085
1086 radeon_vcn_enc_get_param(enc, picture);
1087 if (!enc->dpb) {
1088 enc->dpb = CALLOC_STRUCT(rvid_buffer);
1089 setup_dpb(enc);
1090 if (!enc->dpb ||
1091 !si_vid_create_buffer(enc->screen, enc->dpb, enc->dpb_size, PIPE_USAGE_DEFAULT)) {
1092 RVID_ERR("Can't create DPB buffer.\n");
1093 goto error;
1094 }
1095 }
1096
1097 /* qp map buffer could be created here, and release at the end */
1098 if (enc->enc_pic.enc_qp_map.qp_map_type != RENCODE_QP_MAP_TYPE_NONE) {
1099 if (!enc->roi) {
1100 enc->roi = CALLOC_STRUCT(rvid_buffer);
1101 enc->roi_size = roi_buffer_size(enc);
1102 if (!enc->roi || !enc->roi_size ||
1103 !si_vid_create_buffer(enc->screen, enc->roi, enc->roi_size, PIPE_USAGE_DYNAMIC)) {
1104 RVID_ERR("Can't create ROI buffer.\n");
1105 goto error;
1106 }
1107 }
1108 if(generate_roi_map(enc)) {
1109 RVID_ERR("Can't form roi map.\n");
1110 goto error;
1111 }
1112 }
1113
1114 if (source->buffer_format == PIPE_FORMAT_NV12 ||
1115 source->buffer_format == PIPE_FORMAT_P010 ||
1116 source->buffer_format == PIPE_FORMAT_P016) {
1117 enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
1118 enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
1119 }
1120 else {
1121 enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
1122 enc->chroma = NULL;
1123 }
1124
1125 enc->need_feedback = false;
1126
1127 if (!enc->stream_handle) {
1128 struct rvid_buffer fb;
1129 enc->stream_handle = si_vid_alloc_stream_handle();
1130 enc->si = CALLOC_STRUCT(rvid_buffer);
1131 if (!enc->si ||
1132 !enc->stream_handle ||
1133 !si_vid_create_buffer(enc->screen, enc->si, 128 * 1024, PIPE_USAGE_STAGING)) {
1134 RVID_ERR("Can't create session buffer.\n");
1135 goto error;
1136 }
1137 si_vid_create_buffer(enc->screen, &fb, 4096, PIPE_USAGE_STAGING);
1138 enc->fb = &fb;
1139 enc->begin(enc);
1140 flush(enc);
1141 si_vid_destroy_buffer(&fb);
1142 enc->need_rate_control = false;
1143 }
1144
1145 return;
1146
1147 error:
1148 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->dpb);
1149 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->si);
1150 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->cdf);
1151 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->roi);
1152 }
1153
radeon_enc_encode_bitstream(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_resource * destination,void ** fb)1154 static void radeon_enc_encode_bitstream(struct pipe_video_codec *encoder,
1155 struct pipe_video_buffer *source,
1156 struct pipe_resource *destination, void **fb)
1157 {
1158 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1159 struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
1160
1161 enc->get_buffer(destination, &enc->bs_handle, NULL);
1162 enc->bs_size = destination->width0;
1163
1164 *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
1165
1166 if (!si_vid_create_buffer(enc->screen, enc->fb, 4096, PIPE_USAGE_STAGING)) {
1167 RVID_ERR("Can't create feedback buffer.\n");
1168 return;
1169 }
1170
1171 if (vid_buf->base.statistics_data) {
1172 enc->get_buffer(vid_buf->base.statistics_data, &enc->stats, NULL);
1173 if (enc->stats->size < sizeof(rvcn_encode_stats_type_0_t)) {
1174 RVID_ERR("Encoder statistics output buffer is too small.\n");
1175 enc->stats = NULL;
1176 }
1177 vid_buf->base.statistics_data = NULL;
1178 }
1179 else
1180 enc->stats = NULL;
1181
1182 enc->need_feedback = true;
1183 enc->encode(enc);
1184 }
1185
radeon_enc_end_frame(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_picture_desc * picture)1186 static void radeon_enc_end_frame(struct pipe_video_codec *encoder, struct pipe_video_buffer *source,
1187 struct pipe_picture_desc *picture)
1188 {
1189 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1190 flush(enc);
1191 }
1192
radeon_enc_destroy(struct pipe_video_codec * encoder)1193 static void radeon_enc_destroy(struct pipe_video_codec *encoder)
1194 {
1195 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1196
1197 if (enc->stream_handle) {
1198 struct rvid_buffer fb;
1199 enc->need_feedback = false;
1200 si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
1201 enc->fb = &fb;
1202 enc->destroy(enc);
1203 flush(enc);
1204 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->si);
1205 si_vid_destroy_buffer(&fb);
1206 }
1207
1208 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->dpb);
1209 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->cdf);
1210 RADEON_ENC_DESTROY_VIDEO_BUFFER(enc->roi);
1211 enc->ws->cs_destroy(&enc->cs);
1212 if (enc->ectx)
1213 enc->ectx->destroy(enc->ectx);
1214
1215 FREE(enc);
1216 }
1217
radeon_enc_get_feedback(struct pipe_video_codec * encoder,void * feedback,unsigned * size,struct pipe_enc_feedback_metadata * metadata)1218 static void radeon_enc_get_feedback(struct pipe_video_codec *encoder, void *feedback,
1219 unsigned *size, struct pipe_enc_feedback_metadata* metadata)
1220 {
1221 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1222 struct rvid_buffer *fb = feedback;
1223
1224 if (size) {
1225 uint32_t *ptr = enc->ws->buffer_map(enc->ws, fb->res->buf, &enc->cs,
1226 PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY);
1227 if (ptr[1])
1228 *size = ptr[6] - ptr[8];
1229 else
1230 *size = 0;
1231 enc->ws->buffer_unmap(enc->ws, fb->res->buf);
1232 }
1233
1234 RADEON_ENC_DESTROY_VIDEO_BUFFER(fb);
1235 }
1236
radeon_enc_destroy_fence(struct pipe_video_codec * encoder,struct pipe_fence_handle * fence)1237 static void radeon_enc_destroy_fence(struct pipe_video_codec *encoder,
1238 struct pipe_fence_handle *fence)
1239 {
1240 struct radeon_encoder *enc = (struct radeon_encoder *)encoder;
1241
1242 enc->ws->fence_reference(enc->ws, &fence, NULL);
1243 }
1244
radeon_create_encoder(struct pipe_context * context,const struct pipe_video_codec * templ,struct radeon_winsys * ws,radeon_enc_get_buffer get_buffer)1245 struct pipe_video_codec *radeon_create_encoder(struct pipe_context *context,
1246 const struct pipe_video_codec *templ,
1247 struct radeon_winsys *ws,
1248 radeon_enc_get_buffer get_buffer)
1249 {
1250 struct si_screen *sscreen = (struct si_screen *)context->screen;
1251 struct si_context *sctx = (struct si_context *)context;
1252 struct radeon_encoder *enc;
1253
1254 enc = CALLOC_STRUCT(radeon_encoder);
1255
1256 if (!enc)
1257 return NULL;
1258
1259 if (sctx->vcn_has_ctx) {
1260 enc->ectx = pipe_create_multimedia_context(context->screen);
1261 if (!enc->ectx)
1262 sctx->vcn_has_ctx = false;
1263 }
1264
1265 enc->alignment = 256;
1266 enc->base = *templ;
1267 enc->base.context = (sctx->vcn_has_ctx)? enc->ectx : context;
1268 enc->base.destroy = radeon_enc_destroy;
1269 enc->base.begin_frame = radeon_enc_begin_frame;
1270 enc->base.encode_bitstream = radeon_enc_encode_bitstream;
1271 enc->base.end_frame = radeon_enc_end_frame;
1272 enc->base.flush = radeon_enc_flush;
1273 enc->base.get_feedback = radeon_enc_get_feedback;
1274 enc->base.destroy_fence = radeon_enc_destroy_fence;
1275 enc->get_buffer = get_buffer;
1276 enc->bits_in_shifter = 0;
1277 enc->screen = context->screen;
1278 enc->ws = ws;
1279
1280 if (!ws->cs_create(&enc->cs,
1281 (sctx->vcn_has_ctx) ? ((struct si_context *)enc->ectx)->ctx : sctx->ctx,
1282 AMD_IP_VCN_ENC, radeon_enc_cs_flush, enc)) {
1283 RVID_ERR("Can't get command submission context.\n");
1284 goto error;
1285 }
1286
1287 if (sscreen->info.vcn_ip_version >= VCN_4_0_0)
1288 radeon_enc_4_0_init(enc);
1289 else if (sscreen->info.vcn_ip_version >= VCN_3_0_0)
1290 radeon_enc_3_0_init(enc);
1291 else if (sscreen->info.vcn_ip_version >= VCN_2_0_0)
1292 radeon_enc_2_0_init(enc);
1293 else
1294 radeon_enc_1_2_init(enc);
1295
1296 return &enc->base;
1297
1298 error:
1299 enc->ws->cs_destroy(&enc->cs);
1300 FREE(enc);
1301 return NULL;
1302 }
1303
radeon_enc_add_buffer(struct radeon_encoder * enc,struct pb_buffer_lean * buf,unsigned usage,enum radeon_bo_domain domain,signed offset)1304 void radeon_enc_add_buffer(struct radeon_encoder *enc, struct pb_buffer_lean *buf,
1305 unsigned usage, enum radeon_bo_domain domain, signed offset)
1306 {
1307 enc->ws->cs_add_buffer(&enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain);
1308 uint64_t addr;
1309 addr = enc->ws->buffer_get_virtual_address(buf);
1310 addr = addr + offset;
1311 RADEON_ENC_CS(addr >> 32);
1312 RADEON_ENC_CS(addr);
1313 }
1314
radeon_enc_set_emulation_prevention(struct radeon_encoder * enc,bool set)1315 void radeon_enc_set_emulation_prevention(struct radeon_encoder *enc, bool set)
1316 {
1317 if (set != enc->emulation_prevention) {
1318 enc->emulation_prevention = set;
1319 enc->num_zeros = 0;
1320 }
1321 }
1322
radeon_enc_output_one_byte(struct radeon_encoder * enc,unsigned char byte)1323 void radeon_enc_output_one_byte(struct radeon_encoder *enc, unsigned char byte)
1324 {
1325 if (enc->byte_index == 0)
1326 enc->cs.current.buf[enc->cs.current.cdw] = 0;
1327 enc->cs.current.buf[enc->cs.current.cdw] |=
1328 ((unsigned int)(byte) << index_to_shifts[enc->byte_index]);
1329 enc->byte_index++;
1330
1331 if (enc->byte_index >= 4) {
1332 enc->byte_index = 0;
1333 enc->cs.current.cdw++;
1334 }
1335 }
1336
radeon_enc_emulation_prevention(struct radeon_encoder * enc,unsigned char byte)1337 void radeon_enc_emulation_prevention(struct radeon_encoder *enc, unsigned char byte)
1338 {
1339 if (enc->emulation_prevention) {
1340 if ((enc->num_zeros >= 2) && ((byte == 0x00) || (byte == 0x01) ||
1341 (byte == 0x02) || (byte == 0x03))) {
1342 radeon_enc_output_one_byte(enc, 0x03);
1343 enc->bits_output += 8;
1344 enc->num_zeros = 0;
1345 }
1346 enc->num_zeros = (byte == 0 ? (enc->num_zeros + 1) : 0);
1347 }
1348 }
1349
radeon_enc_code_fixed_bits(struct radeon_encoder * enc,unsigned int value,unsigned int num_bits)1350 void radeon_enc_code_fixed_bits(struct radeon_encoder *enc, unsigned int value,
1351 unsigned int num_bits)
1352 {
1353 unsigned int bits_to_pack = 0;
1354 enc->bits_size += num_bits;
1355
1356 while (num_bits > 0) {
1357 unsigned int value_to_pack = value & (0xffffffff >> (32 - num_bits));
1358 bits_to_pack =
1359 num_bits > (32 - enc->bits_in_shifter) ? (32 - enc->bits_in_shifter) : num_bits;
1360
1361 if (bits_to_pack < num_bits)
1362 value_to_pack = value_to_pack >> (num_bits - bits_to_pack);
1363
1364 enc->shifter |= value_to_pack << (32 - enc->bits_in_shifter - bits_to_pack);
1365 num_bits -= bits_to_pack;
1366 enc->bits_in_shifter += bits_to_pack;
1367
1368 while (enc->bits_in_shifter >= 8) {
1369 unsigned char output_byte = (unsigned char)(enc->shifter >> 24);
1370 enc->shifter <<= 8;
1371 radeon_enc_emulation_prevention(enc, output_byte);
1372 radeon_enc_output_one_byte(enc, output_byte);
1373 enc->bits_in_shifter -= 8;
1374 enc->bits_output += 8;
1375 }
1376 }
1377 }
1378
radeon_enc_code_uvlc(struct radeon_encoder * enc,unsigned int value)1379 void radeon_enc_code_uvlc(struct radeon_encoder *enc, unsigned int value)
1380 {
1381 uint32_t num_bits = 0;
1382 uint64_t value_plus1 = (uint64_t)value + 1;
1383 uint32_t num_leading_zeros = 0;
1384
1385 while ((uint64_t)1 << num_bits <= value_plus1)
1386 num_bits++;
1387
1388 num_leading_zeros = num_bits - 1;
1389 radeon_enc_code_fixed_bits(enc, 0, num_leading_zeros);
1390 radeon_enc_code_fixed_bits(enc, 1, 1);
1391 radeon_enc_code_fixed_bits(enc, (uint32_t)value_plus1, num_leading_zeros);
1392 }
1393
radeon_enc_code_leb128(uint8_t * buf,uint32_t value,uint32_t num_bytes)1394 void radeon_enc_code_leb128(uint8_t *buf, uint32_t value,
1395 uint32_t num_bytes)
1396 {
1397 uint8_t leb128_byte = 0;
1398 uint32_t i = 0;
1399
1400 do {
1401 leb128_byte = (value & 0x7f);
1402 value >>= 7;
1403 if (num_bytes > 1)
1404 leb128_byte |= 0x80;
1405
1406 *(buf + i) = leb128_byte;
1407 num_bytes--;
1408 i++;
1409 } while((leb128_byte & 0x80));
1410 }
1411
radeon_enc_reset(struct radeon_encoder * enc)1412 void radeon_enc_reset(struct radeon_encoder *enc)
1413 {
1414 enc->emulation_prevention = false;
1415 enc->shifter = 0;
1416 enc->bits_in_shifter = 0;
1417 enc->bits_output = 0;
1418 enc->num_zeros = 0;
1419 enc->byte_index = 0;
1420 enc->bits_size = 0;
1421 }
1422
radeon_enc_byte_align(struct radeon_encoder * enc)1423 void radeon_enc_byte_align(struct radeon_encoder *enc)
1424 {
1425 unsigned int num_padding_zeros = (32 - enc->bits_in_shifter) % 8;
1426
1427 if (num_padding_zeros > 0)
1428 radeon_enc_code_fixed_bits(enc, 0, num_padding_zeros);
1429 }
1430
radeon_enc_flush_headers(struct radeon_encoder * enc)1431 void radeon_enc_flush_headers(struct radeon_encoder *enc)
1432 {
1433 if (enc->bits_in_shifter != 0) {
1434 unsigned char output_byte = (unsigned char)(enc->shifter >> 24);
1435 radeon_enc_emulation_prevention(enc, output_byte);
1436 radeon_enc_output_one_byte(enc, output_byte);
1437 enc->bits_output += enc->bits_in_shifter;
1438 enc->shifter = 0;
1439 enc->bits_in_shifter = 0;
1440 enc->num_zeros = 0;
1441 }
1442
1443 if (enc->byte_index > 0) {
1444 enc->cs.current.cdw++;
1445 enc->byte_index = 0;
1446 }
1447 }
1448
radeon_enc_code_ue(struct radeon_encoder * enc,unsigned int value)1449 void radeon_enc_code_ue(struct radeon_encoder *enc, unsigned int value)
1450 {
1451 int x = -1;
1452 unsigned int ue_code = value + 1;
1453 value += 1;
1454
1455 while (value) {
1456 value = (value >> 1);
1457 x += 1;
1458 }
1459
1460 unsigned int ue_length = (x << 1) + 1;
1461 radeon_enc_code_fixed_bits(enc, ue_code, ue_length);
1462 }
1463
radeon_enc_code_se(struct radeon_encoder * enc,int value)1464 void radeon_enc_code_se(struct radeon_encoder *enc, int value)
1465 {
1466 unsigned int v = 0;
1467
1468 if (value != 0)
1469 v = (value < 0 ? ((unsigned int)(0 - value) << 1) : (((unsigned int)(value) << 1) - 1));
1470
1471 radeon_enc_code_ue(enc, v);
1472 }
1473
1474 /* dummy function for re-using the same pipeline */
radeon_enc_dummy(struct radeon_encoder * enc)1475 void radeon_enc_dummy(struct radeon_encoder *enc) {}
1476
1477 /* this function has to be in pair with AV1 header copy instruction type at the end */
radeon_enc_av1_bs_copy_end(struct radeon_encoder * enc,uint32_t bits)1478 static void radeon_enc_av1_bs_copy_end(struct radeon_encoder *enc, uint32_t bits)
1479 {
1480 assert(bits > 0);
1481 /* it must be dword aligned at the end */
1482 *enc->enc_pic.copy_start = DIV_ROUND_UP(bits, 32) * 4 + 12;
1483 *(enc->enc_pic.copy_start + 2) = bits;
1484 }
1485
1486 /* av1 bitstream instruction type */
radeon_enc_av1_bs_instruction_type(struct radeon_encoder * enc,uint32_t inst,uint32_t obu_type)1487 void radeon_enc_av1_bs_instruction_type(struct radeon_encoder *enc,
1488 uint32_t inst,
1489 uint32_t obu_type)
1490 {
1491 radeon_enc_flush_headers(enc);
1492
1493 if (enc->bits_output)
1494 radeon_enc_av1_bs_copy_end(enc, enc->bits_output);
1495
1496 enc->enc_pic.copy_start = &enc->cs.current.buf[enc->cs.current.cdw++];
1497 RADEON_ENC_CS(inst);
1498
1499 if (inst != RENCODE_HEADER_INSTRUCTION_COPY) {
1500 *enc->enc_pic.copy_start = 8;
1501 if (inst == RENCODE_AV1_BITSTREAM_INSTRUCTION_OBU_START) {
1502 *enc->enc_pic.copy_start += 4;
1503 RADEON_ENC_CS(obu_type);
1504 }
1505 } else
1506 RADEON_ENC_CS(0); /* allocate a dword for number of bits */
1507
1508 radeon_enc_reset(enc);
1509 }
1510
radeon_enc_value_bits(uint32_t value)1511 uint32_t radeon_enc_value_bits(uint32_t value)
1512 {
1513 uint32_t i = 1;
1514
1515 while (value > 1) {
1516 i++;
1517 value >>= 1;
1518 }
1519
1520 return i;
1521 }
1522