| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "radeon_vcn_enc.h" |
| 10 |
#include "ac_vcn_enc_av1_default_cdf.h" |
| 11 |
#include "ac_debug.h" |
| 12 |
|
| 13 |
#include "si_video.h" |
| 14 |
#include "si_pipe.h" |
| 15 |
#include "util/u_memory.h" |
| 16 |
#include "util/u_video.h" |
| 17 |
#include "vl/vl_video_buffer.h" |
| 18 |
|
| 19 |
|
| 20 |
static void radeon_vcn_enc_quality_modes(struct radeon_encoder *enc, |
| 21 |
struct pipe_enc_quality_modes *in) |
| 22 |
{ |
| 23 |
rvcn_enc_quality_modes_t *p = &enc->enc_pic.quality_modes; |
| 24 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 25 |
|
| 26 |
p->preset_mode = in->preset_mode > RENCODE_PRESET_MODE_HIGH_QUALITY |
| 27 |
? RENCODE_PRESET_MODE_HIGH_QUALITY |
| 28 |
: in->preset_mode; |
| 29 |
|
| 30 |
if (sscreen->info.vcn_ip_version < VCN_4_0_0 && |
| 31 |
p->preset_mode == RENCODE_PRESET_MODE_HIGH_QUALITY) |
| 32 |
p->preset_mode = RENCODE_PRESET_MODE_QUALITY; |
| 33 |
|
| 34 |
if (sscreen->info.vcn_ip_version >= VCN_2_0_0 && |
| 35 |
sscreen->info.vcn_ip_version < VCN_5_0_0 && |
| 36 |
p->preset_mode == RENCODE_PRESET_MODE_SPEED && |
| 37 |
!enc->enc_pic.hevc_deblock.disable_sao && |
| 38 |
u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC) |
| 39 |
p->preset_mode = RENCODE_PRESET_MODE_BALANCE; |
| 40 |
|
| 41 |
if (enc->first_frame) { |
| 42 |
p->pre_encode_mode = in->pre_encode_mode ? RENCODE_PREENCODE_MODE_4X |
| 43 |
: RENCODE_PREENCODE_MODE_NONE; |
| 44 |
|
| 45 |
if (enc->enc_pic.rc_session_init.rate_control_method == RENCODE_RATE_CONTROL_METHOD_QUALITY_VBR) |
| 46 |
p->pre_encode_mode = RENCODE_PREENCODE_MODE_4X; |
| 47 |
} |
| 48 |
|
| 49 |
p->vbaq_mode = in->vbaq_mode ? RENCODE_VBAQ_AUTO : RENCODE_VBAQ_NONE; |
| 50 |
|
| 51 |
if (enc->enc_pic.rc_session_init.rate_control_method == RENCODE_RATE_CONTROL_METHOD_NONE) |
| 52 |
p->vbaq_mode = RENCODE_VBAQ_NONE; |
| 53 |
|
| 54 |
enc->enc_pic.quality_params.vbaq_mode = p->vbaq_mode; |
| 55 |
enc->enc_pic.quality_params.scene_change_sensitivity = 0; |
| 56 |
enc->enc_pic.quality_params.scene_change_min_idr_interval = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
enc->enc_pic.quality_params.two_pass_search_center_map_mode = |
| 63 |
(sscreen->info.vcn_ip_version < VCN_5_0_0 && |
| 64 |
enc->enc_pic.quality_modes.pre_encode_mode && |
| 65 |
!enc->enc_pic.spec_misc.b_picture_enabled) ? 1 : 0; |
| 66 |
enc->enc_pic.quality_params.vbaq_strength = 0; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static void radeon_vcn_enc_invalid_frame_rate(uint32_t *den, uint32_t *num) |
| 71 |
{ |
| 72 |
if (*den == 0 || *num == 0) { |
| 73 |
*den = 1; |
| 74 |
*num = 30; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static unsigned get_rec_alignment(enum pipe_video_format format) |
| 79 |
{ |
| 80 |
switch (format) { |
| 81 |
case PIPE_VIDEO_FORMAT_MPEG4_AVC: |
| 82 |
return 16; |
| 83 |
case PIPE_VIDEO_FORMAT_HEVC: |
| 84 |
case PIPE_VIDEO_FORMAT_AV1: |
| 85 |
return 64; |
| 86 |
default: |
| 87 |
UNREACHABLE("unsupported codec"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static uint32_t radeon_vcn_per_frame_integer(uint32_t bitrate, uint32_t den, uint32_t num) |
| 92 |
{ |
| 93 |
uint64_t rate_den = (uint64_t)bitrate * (uint64_t)den; |
| 94 |
|
| 95 |
return (uint32_t)(rate_den/num); |
| 96 |
} |
| 97 |
|
| 98 |
static uint32_t radeon_vcn_per_frame_frac(uint32_t bitrate, uint32_t den, uint32_t num) |
| 99 |
{ |
| 100 |
uint64_t rate_den = (uint64_t)bitrate * (uint64_t)den; |
| 101 |
uint64_t remainder = rate_den % num; |
| 102 |
|
| 103 |
return (uint32_t)((remainder << 32) / num); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
static uint32_t radeon_vcn_enc_blocks_in_frame(struct radeon_encoder *enc, |
| 108 |
uint32_t *width_in_block, |
| 109 |
uint32_t *height_in_block) |
| 110 |
{ |
| 111 |
bool is_h264 = u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC; |
| 112 |
uint32_t block_length = is_h264 ? PIPE_H264_MB_SIZE : PIPE_H265_ENC_CTB_SIZE; |
| 113 |
|
| 114 |
*width_in_block = DIV_ROUND_UP(enc->base.width, block_length); |
| 115 |
*height_in_block = DIV_ROUND_UP(enc->base.height, block_length); |
| 116 |
|
| 117 |
return block_length; |
| 118 |
} |
| 119 |
|
| 120 |
static void radeon_vcn_enc_get_intra_refresh_param(struct radeon_encoder *enc, |
| 121 |
bool need_filter_overlap, |
| 122 |
struct pipe_enc_intra_refresh *intra_refresh) |
| 123 |
{ |
| 124 |
uint32_t width_in_block, height_in_block; |
| 125 |
|
| 126 |
enc->enc_pic.intra_refresh.intra_refresh_mode = RENCODE_INTRA_REFRESH_MODE_NONE; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if (enc->enc_pic.spec_misc.b_picture_enabled || enc->enc_pic.num_temporal_layers > 1) { |
| 132 |
enc->enc_pic.intra_refresh.region_size = 0; |
| 133 |
enc->enc_pic.intra_refresh.offset = 0; |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block); |
| 138 |
|
| 139 |
switch(intra_refresh->mode) { |
| 140 |
case INTRA_REFRESH_MODE_UNIT_ROWS: |
| 141 |
if (intra_refresh->offset < height_in_block) |
| 142 |
enc->enc_pic.intra_refresh.intra_refresh_mode |
| 143 |
= RENCODE_INTRA_REFRESH_MODE_CTB_MB_ROWS; |
| 144 |
break; |
| 145 |
case INTRA_REFRESH_MODE_UNIT_COLUMNS: |
| 146 |
if (intra_refresh->offset < width_in_block) |
| 147 |
enc->enc_pic.intra_refresh.intra_refresh_mode |
| 148 |
= RENCODE_INTRA_REFRESH_MODE_CTB_MB_COLUMNS; |
| 149 |
break; |
| 150 |
case INTRA_REFRESH_MODE_NONE: |
| 151 |
default: |
| 152 |
break; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if (enc->enc_pic.intra_refresh.intra_refresh_mode != RENCODE_INTRA_REFRESH_MODE_NONE) { |
| 160 |
enc->enc_pic.intra_refresh.region_size = (need_filter_overlap) ? |
| 161 |
intra_refresh->region_size + 1 : |
| 162 |
intra_refresh->region_size; |
| 163 |
enc->enc_pic.intra_refresh.offset = intra_refresh->offset; |
| 164 |
} else { |
| 165 |
enc->enc_pic.intra_refresh.region_size = 0; |
| 166 |
enc->enc_pic.intra_refresh.offset = 0; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void radeon_vcn_enc_get_roi_param(struct radeon_encoder *enc, |
| 171 |
struct pipe_enc_roi *roi) |
| 172 |
{ |
| 173 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 174 |
bool is_av1 = u_reduce_video_profile(enc->base.profile) |
| 175 |
== PIPE_VIDEO_FORMAT_AV1; |
| 176 |
rvcn_enc_qp_map_t *qp_map = &enc->enc_pic.enc_qp_map; |
| 177 |
|
| 178 |
if (!roi->num) |
| 179 |
enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_NONE; |
| 180 |
else { |
| 181 |
uint32_t width_in_block, height_in_block; |
| 182 |
uint32_t block_length; |
| 183 |
int32_t i, j; |
| 184 |
bool pa_format = false; |
| 185 |
|
| 186 |
qp_map->version = sscreen->info.vcn_ip_version >= VCN_5_0_0 |
| 187 |
? RENCODE_QP_MAP_VCN5 : RENCODE_QP_MAP_LEGACY; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if (enc->enc_pic.rc_session_init.rate_control_method && |
| 192 |
(qp_map->version == RENCODE_QP_MAP_LEGACY)) { |
| 193 |
enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_MAP_PA; |
| 194 |
pa_format = true; |
| 195 |
} |
| 196 |
else |
| 197 |
enc->enc_pic.enc_qp_map.qp_map_type = RENCODE_QP_MAP_TYPE_DELTA; |
| 198 |
|
| 199 |
block_length = radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block); |
| 200 |
|
| 201 |
qp_map->width_in_block = width_in_block; |
| 202 |
qp_map->height_in_block = height_in_block; |
| 203 |
|
| 204 |
for (i = RENCODE_QP_MAP_MAX_REGIONS - 1; i >= roi->num; i--) |
| 205 |
enc->enc_pic.enc_qp_map.map[i].is_valid = false; |
| 206 |
|
| 207 |
|
| 208 |
for (j = 0; i >= 0; i--, j++) { |
| 209 |
struct rvcn_enc_qp_map_region *map = &enc->enc_pic.enc_qp_map.map[j]; |
| 210 |
struct pipe_enc_region_in_roi *region = &roi->region[i]; |
| 211 |
|
| 212 |
map->is_valid = region->valid; |
| 213 |
if (region->valid) { |
| 214 |
int32_t av1_qi_value; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (is_av1 && pa_format) { |
| 219 |
if (region->qp_value > 0) |
| 220 |
av1_qi_value = (region->qp_value + 2) / 5; |
| 221 |
else if (region->qp_value < 0) |
| 222 |
av1_qi_value = (region->qp_value - 2) / 5; |
| 223 |
else |
| 224 |
av1_qi_value = region->qp_value; |
| 225 |
map->qp_delta = av1_qi_value; |
| 226 |
} else |
| 227 |
map->qp_delta = region->qp_value; |
| 228 |
|
| 229 |
map->x_in_unit = MIN2(DIV_ROUND_UP(region->x, block_length), width_in_block - 1); |
| 230 |
map->y_in_unit = MIN2(DIV_ROUND_UP(region->y, block_length), height_in_block - 1); |
| 231 |
map->width_in_unit = MIN2(DIV_ROUND_UP(region->width, block_length), width_in_block); |
| 232 |
map->height_in_unit = MIN2(DIV_ROUND_UP(region->height, block_length), width_in_block); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
static void radeon_vcn_enc_get_latency_param(struct radeon_encoder *enc) |
| 239 |
{ |
| 240 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 241 |
|
| 242 |
enc->enc_pic.enc_latency.encode_latency = |
| 243 |
sscreen->multimedia_debug_flags & DBG(LOW_LATENCY_ENCODE) ? 1000 : 0; |
| 244 |
} |
| 245 |
|
| 246 |
static void radeon_vcn_enc_h264_get_session_param(struct radeon_encoder *enc, |
| 247 |
struct pipe_h264_enc_picture_desc *pic) |
| 248 |
{ |
| 249 |
if (enc->enc_pic.session_init.aligned_picture_width) |
| 250 |
return; |
| 251 |
|
| 252 |
uint32_t align_width = PIPE_H264_MB_SIZE; |
| 253 |
uint32_t align_height = PIPE_H264_MB_SIZE; |
| 254 |
|
| 255 |
enc->enc_pic.session_init.encode_standard = RENCODE_ENCODE_STANDARD_H264; |
| 256 |
enc->enc_pic.session_init.aligned_picture_width = align(enc->base.width, align_width); |
| 257 |
enc->enc_pic.session_init.aligned_picture_height = align(enc->base.height, align_height); |
| 258 |
|
| 259 |
uint32_t padding_width = 0; |
| 260 |
uint32_t padding_height = 0; |
| 261 |
uint32_t max_padding_width = align_width - 2; |
| 262 |
uint32_t max_padding_height = align_height - 2; |
| 263 |
|
| 264 |
if (enc->enc_pic.session_init.aligned_picture_width > enc->source->width) |
| 265 |
padding_width = enc->enc_pic.session_init.aligned_picture_width - enc->source->width; |
| 266 |
if (enc->enc_pic.session_init.aligned_picture_height > enc->source->height) |
| 267 |
padding_height = enc->enc_pic.session_init.aligned_picture_height - enc->source->height; |
| 268 |
|
| 269 |
|
| 270 |
if (padding_width > max_padding_width || padding_height > max_padding_height) |
| 271 |
RADEON_ENC_ERR("Input surface size doesn't match aligned size\n"); |
| 272 |
|
| 273 |
if (pic->seq.enc_frame_cropping_flag) { |
| 274 |
uint32_t pad_w = |
| 275 |
(pic->seq.enc_frame_crop_left_offset + pic->seq.enc_frame_crop_right_offset) * 2; |
| 276 |
uint32_t pad_h = |
| 277 |
(pic->seq.enc_frame_crop_top_offset + pic->seq.enc_frame_crop_bottom_offset) * 2; |
| 278 |
padding_width = CLAMP(pad_w, padding_width, max_padding_width); |
| 279 |
padding_height = CLAMP(pad_h, padding_height, max_padding_height); |
| 280 |
} |
| 281 |
|
| 282 |
enc->enc_pic.session_init.padding_width = padding_width; |
| 283 |
enc->enc_pic.session_init.padding_height = padding_height; |
| 284 |
} |
| 285 |
|
| 286 |
static void radeon_vcn_enc_h264_get_dbk_param(struct radeon_encoder *enc, |
| 287 |
struct pipe_h264_enc_picture_desc *pic) |
| 288 |
{ |
| 289 |
enc->enc_pic.h264_deblock.disable_deblocking_filter_idc = |
| 290 |
CLAMP(pic->dbk.disable_deblocking_filter_idc, 0, 2); |
| 291 |
enc->enc_pic.h264_deblock.alpha_c0_offset_div2 = pic->dbk.alpha_c0_offset_div2; |
| 292 |
enc->enc_pic.h264_deblock.beta_offset_div2 = pic->dbk.beta_offset_div2; |
| 293 |
enc->enc_pic.h264_deblock.cb_qp_offset = pic->pic_ctrl.chroma_qp_index_offset; |
| 294 |
enc->enc_pic.h264_deblock.cr_qp_offset = pic->pic_ctrl.second_chroma_qp_index_offset; |
| 295 |
} |
| 296 |
|
| 297 |
static void radeon_vcn_enc_h264_get_spec_misc_param(struct radeon_encoder *enc, |
| 298 |
struct pipe_h264_enc_picture_desc *pic) |
| 299 |
{ |
| 300 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 301 |
|
| 302 |
enc->enc_pic.spec_misc.profile_idc = u_get_h264_profile_idc(enc->base.profile); |
| 303 |
if (enc->enc_pic.spec_misc.profile_idc >= PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN && |
| 304 |
enc->enc_pic.spec_misc.profile_idc != PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED) |
| 305 |
enc->enc_pic.spec_misc.cabac_enable = pic->pic_ctrl.enc_cabac_enable; |
| 306 |
else |
| 307 |
enc->enc_pic.spec_misc.cabac_enable = false; |
| 308 |
|
| 309 |
enc->enc_pic.spec_misc.cabac_init_idc = enc->enc_pic.spec_misc.cabac_enable ? |
| 310 |
pic->pic_ctrl.enc_cabac_init_idc : 0; |
| 311 |
enc->enc_pic.spec_misc.deblocking_filter_control_present_flag = |
| 312 |
pic->pic_ctrl.deblocking_filter_control_present_flag; |
| 313 |
enc->enc_pic.spec_misc.redundant_pic_cnt_present_flag = |
| 314 |
pic->pic_ctrl.redundant_pic_cnt_present_flag; |
| 315 |
enc->enc_pic.spec_misc.b_picture_enabled = !!pic->seq.max_num_reorder_frames; |
| 316 |
|
| 317 |
enc->enc_pic.spec_misc.constrained_intra_pred_flag = |
| 318 |
!enc->enc_pic.rc_session_init.rate_control_method && |
| 319 |
pic->pic_ctrl.constrained_intra_pred_flag; |
| 320 |
enc->enc_pic.spec_misc.half_pel_enabled = 1; |
| 321 |
enc->enc_pic.spec_misc.quarter_pel_enabled = 1; |
| 322 |
enc->enc_pic.spec_misc.weighted_bipred_idc = |
| 323 |
pic->pic_ctrl.weighted_bipred_idc != 1 ? |
| 324 |
pic->pic_ctrl.weighted_bipred_idc : 0; |
| 325 |
enc->enc_pic.spec_misc.transform_8x8_mode = |
| 326 |
sscreen->info.vcn_ip_version >= VCN_5_0_0 && |
| 327 |
enc->enc_pic.spec_misc.cabac_enable && |
| 328 |
pic->pic_ctrl.transform_8x8_mode_flag; |
| 329 |
enc->enc_pic.spec_misc.level_idc = pic->seq.level_idc; |
| 330 |
} |
| 331 |
|
| 332 |
static void radeon_vcn_enc_h264_get_rc_param(struct radeon_encoder *enc, |
| 333 |
struct pipe_h264_enc_picture_desc *pic) |
| 334 |
{ |
| 335 |
uint32_t frame_rate_den, frame_rate_num, max_qp; |
| 336 |
|
| 337 |
enc->enc_pic.num_temporal_layers = pic->seq.num_temporal_layers ? pic->seq.num_temporal_layers : 1; |
| 338 |
enc->enc_pic.temporal_id = MIN2(pic->pic_ctrl.temporal_id, enc->enc_pic.num_temporal_layers - 1); |
| 339 |
|
| 340 |
for (int i = 0; i < enc->enc_pic.num_temporal_layers; i++) { |
| 341 |
enc->enc_pic.rc_layer_init[i].target_bit_rate = pic->rate_ctrl[i].target_bitrate; |
| 342 |
enc->enc_pic.rc_layer_init[i].peak_bit_rate = pic->rate_ctrl[i].peak_bitrate; |
| 343 |
frame_rate_den = pic->rate_ctrl[i].frame_rate_den; |
| 344 |
frame_rate_num = pic->rate_ctrl[i].frame_rate_num; |
| 345 |
radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num); |
| 346 |
enc->enc_pic.rc_layer_init[i].frame_rate_den = frame_rate_den; |
| 347 |
enc->enc_pic.rc_layer_init[i].frame_rate_num = frame_rate_num; |
| 348 |
enc->enc_pic.rc_layer_init[i].vbv_buffer_size = pic->rate_ctrl[i].vbv_buffer_size; |
| 349 |
enc->enc_pic.rc_layer_init[i].avg_target_bits_per_picture = |
| 350 |
radeon_vcn_per_frame_integer(pic->rate_ctrl[i].target_bitrate, |
| 351 |
frame_rate_den, |
| 352 |
frame_rate_num); |
| 353 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_integer = |
| 354 |
radeon_vcn_per_frame_integer(pic->rate_ctrl[i].peak_bitrate, |
| 355 |
frame_rate_den, |
| 356 |
frame_rate_num); |
| 357 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_fractional = |
| 358 |
radeon_vcn_per_frame_frac(pic->rate_ctrl[i].peak_bitrate, |
| 359 |
frame_rate_den, |
| 360 |
frame_rate_num); |
| 361 |
} |
| 362 |
enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rate_ctrl[0].vbv_buf_lv; |
| 363 |
enc->enc_pic.rc_per_pic.qp_obs = pic->quant_i_frames; |
| 364 |
enc->enc_pic.rc_per_pic.min_qp_app_obs = pic->rate_ctrl[0].min_qp; |
| 365 |
enc->enc_pic.rc_per_pic.max_qp_app_obs = pic->rate_ctrl[0].max_qp ? |
| 366 |
pic->rate_ctrl[0].max_qp : 51; |
| 367 |
enc->enc_pic.rc_per_pic.qp_i = pic->quant_i_frames; |
| 368 |
enc->enc_pic.rc_per_pic.qp_p = pic->quant_p_frames; |
| 369 |
enc->enc_pic.rc_per_pic.qp_b = pic->quant_b_frames; |
| 370 |
enc->enc_pic.rc_per_pic.min_qp_i = pic->rate_ctrl[0].min_qp; |
| 371 |
enc->enc_pic.rc_per_pic.min_qp_p = pic->rate_ctrl[0].min_qp; |
| 372 |
enc->enc_pic.rc_per_pic.min_qp_b = pic->rate_ctrl[0].min_qp; |
| 373 |
max_qp = pic->rate_ctrl[0].max_qp ? pic->rate_ctrl[0].max_qp : 51; |
| 374 |
enc->enc_pic.rc_per_pic.max_qp_i = max_qp; |
| 375 |
enc->enc_pic.rc_per_pic.max_qp_p = max_qp; |
| 376 |
enc->enc_pic.rc_per_pic.max_qp_b = max_qp; |
| 377 |
enc->enc_pic.rc_per_pic.enabled_filler_data = 0; |
| 378 |
enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rate_ctrl[0].skip_frame_enable; |
| 379 |
enc->enc_pic.rc_per_pic.enforce_hrd = pic->rate_ctrl[0].enforce_hrd; |
| 380 |
enc->enc_pic.rc_per_pic.qvbr_quality_level = pic->rate_ctrl[0].vbr_quality_factor; |
| 381 |
|
| 382 |
switch (pic->rate_ctrl[0].rate_ctrl_method) { |
| 383 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE: |
| 384 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 385 |
break; |
| 386 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP: |
| 387 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT: |
| 388 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR; |
| 389 |
enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rate_ctrl[0].fill_data_enable; |
| 390 |
break; |
| 391 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP: |
| 392 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE: |
| 393 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 394 |
RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR; |
| 395 |
break; |
| 396 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_QUALITY_VARIABLE: |
| 397 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 398 |
RENCODE_RATE_CONTROL_METHOD_QUALITY_VBR; |
| 399 |
break; |
| 400 |
default: |
| 401 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 402 |
} |
| 403 |
enc->enc_pic.rc_per_pic.max_au_size_obs = pic->rate_ctrl[0].max_au_size; |
| 404 |
enc->enc_pic.rc_per_pic.max_au_size_i = pic->rate_ctrl[0].max_au_size; |
| 405 |
enc->enc_pic.rc_per_pic.max_au_size_p = pic->rate_ctrl[0].max_au_size; |
| 406 |
enc->enc_pic.rc_per_pic.max_au_size_b = pic->rate_ctrl[0].max_au_size; |
| 407 |
} |
| 408 |
|
| 409 |
static void radeon_vcn_enc_h264_get_slice_ctrl_param(struct radeon_encoder *enc, |
| 410 |
struct pipe_h264_enc_picture_desc *pic) |
| 411 |
{ |
| 412 |
uint32_t num_mbs_total, num_mbs_in_slice; |
| 413 |
|
| 414 |
num_mbs_total = |
| 415 |
DIV_ROUND_UP(enc->base.width, PIPE_H264_MB_SIZE) * |
| 416 |
DIV_ROUND_UP(enc->base.height, PIPE_H264_MB_SIZE); |
| 417 |
|
| 418 |
if (pic->num_slice_descriptors <= 1) { |
| 419 |
num_mbs_in_slice = num_mbs_total; |
| 420 |
} else { |
| 421 |
bool use_app_config = true; |
| 422 |
num_mbs_in_slice = pic->slices_descriptors[0].num_macroblocks; |
| 423 |
|
| 424 |
|
| 425 |
for (unsigned i = 1; i < pic->num_slice_descriptors - 1; i++) { |
| 426 |
if (num_mbs_in_slice != pic->slices_descriptors[i].num_macroblocks) |
| 427 |
use_app_config = false; |
| 428 |
} |
| 429 |
|
| 430 |
if (pic->slices_descriptors[pic->num_slice_descriptors - 1].num_macroblocks > num_mbs_in_slice) |
| 431 |
use_app_config = false; |
| 432 |
|
| 433 |
if (!use_app_config) { |
| 434 |
assert(num_mbs_total >= pic->num_slice_descriptors); |
| 435 |
num_mbs_in_slice = |
| 436 |
(num_mbs_total + pic->num_slice_descriptors - 1) / pic->num_slice_descriptors; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
num_mbs_in_slice = MAX2(4, num_mbs_in_slice); |
| 441 |
|
| 442 |
enc->enc_pic.slice_ctrl.slice_control_mode = RENCODE_H264_SLICE_CONTROL_MODE_FIXED_MBS; |
| 443 |
enc->enc_pic.slice_ctrl.num_mbs_per_slice = num_mbs_in_slice; |
| 444 |
|
| 445 |
if (ac_vcn_enc_variable_slice_mode_supported(&((struct si_screen *)enc->screen)->info, |
| 446 |
enc->enc_pic.quality_modes.pre_encode_mode) && |
| 447 |
pic->num_slice_descriptors <= RENCODE_MAX_NUM_SLICES) |
| 448 |
{ |
| 449 |
uint32_t cumulative_mbs = 0; |
| 450 |
enc->enc_pic.h264_slice_info_var.num_slices = pic->num_slice_descriptors; |
| 451 |
for (unsigned i = 0; i < pic->num_slice_descriptors; i++) { |
| 452 |
enc->enc_pic.h264_slice_info_var.slice_info[i].num_mbs_per_slice = |
| 453 |
pic->slices_descriptors[i].num_macroblocks; |
| 454 |
cumulative_mbs += pic->slices_descriptors[i].num_macroblocks; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if (cumulative_mbs == num_mbs_total) |
| 460 |
enc->enc_pic.slice_ctrl.slice_control_mode = RENCODE_H264_SLICE_CONTROL_MODE_VARIABLE_MBS; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
static void radeon_vcn_enc_get_output_format_param(struct radeon_encoder *enc) |
| 465 |
{ |
| 466 |
switch (enc->enc_pic.bit_depth_luma_minus8) { |
| 467 |
case 2: |
| 468 |
enc->enc_pic.enc_output_format.output_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT; |
| 469 |
break; |
| 470 |
default: |
| 471 |
enc->enc_pic.enc_output_format.output_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT; |
| 472 |
break; |
| 473 |
} |
| 474 |
|
| 475 |
enc->enc_pic.enc_output_format.output_color_volume = enc->output_color_volume; |
| 476 |
enc->enc_pic.enc_output_format.output_color_range = enc->output_color_range; |
| 477 |
enc->enc_pic.enc_output_format.output_chroma_location = enc->output_chroma_location; |
| 478 |
} |
| 479 |
|
| 480 |
static void radeon_vcn_enc_get_input_format_param(struct radeon_encoder *enc) |
| 481 |
{ |
| 482 |
switch (enc->source->buffer_format) { |
| 483 |
case PIPE_FORMAT_P010: |
| 484 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT; |
| 485 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_P010; |
| 486 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_2_0; |
| 487 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_YUV; |
| 488 |
break; |
| 489 |
case PIPE_FORMAT_B8G8R8A8_UNORM: |
| 490 |
case PIPE_FORMAT_B8G8R8X8_UNORM: |
| 491 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT; |
| 492 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4; |
| 493 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A8R8G8B8; |
| 494 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB; |
| 495 |
break; |
| 496 |
case PIPE_FORMAT_R8G8B8A8_UNORM: |
| 497 |
case PIPE_FORMAT_R8G8B8X8_UNORM: |
| 498 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT; |
| 499 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4; |
| 500 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A8B8G8R8; |
| 501 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB; |
| 502 |
break; |
| 503 |
case PIPE_FORMAT_B10G10R10A2_UNORM: |
| 504 |
case PIPE_FORMAT_B10G10R10X2_UNORM: |
| 505 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT; |
| 506 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4; |
| 507 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A2R10G10B10; |
| 508 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB; |
| 509 |
break; |
| 510 |
case PIPE_FORMAT_R10G10B10A2_UNORM: |
| 511 |
case PIPE_FORMAT_R10G10B10X2_UNORM: |
| 512 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_10_BIT; |
| 513 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_4_4; |
| 514 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_A2B10G10R10; |
| 515 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_RGB; |
| 516 |
break; |
| 517 |
case PIPE_FORMAT_NV12: |
| 518 |
default: |
| 519 |
enc->enc_pic.enc_input_format.input_color_bit_depth = RENCODE_COLOR_BIT_DEPTH_8_BIT; |
| 520 |
enc->enc_pic.enc_input_format.input_color_packing_format = RENCODE_COLOR_PACKING_FORMAT_NV12; |
| 521 |
enc->enc_pic.enc_input_format.input_chroma_subsampling = RENCODE_CHROMA_SUBSAMPLING_4_2_0; |
| 522 |
enc->enc_pic.enc_input_format.input_color_space = RENCODE_COLOR_SPACE_YUV; |
| 523 |
break; |
| 524 |
} |
| 525 |
|
| 526 |
enc->enc_pic.enc_input_format.input_color_volume = enc->input_color_volume; |
| 527 |
enc->enc_pic.enc_input_format.input_color_range = enc->input_color_range; |
| 528 |
enc->enc_pic.enc_input_format.input_chroma_location = RENCODE_CHROMA_LOCATION_INTERSTITIAL; |
| 529 |
} |
| 530 |
|
| 531 |
static void radeon_vcn_enc_h264_get_param(struct radeon_encoder *enc, |
| 532 |
struct pipe_h264_enc_picture_desc *pic) |
| 533 |
{ |
| 534 |
bool use_filter; |
| 535 |
|
| 536 |
enc->enc_pic.h264.desc = pic; |
| 537 |
enc->enc_pic.picture_type = pic->picture_type; |
| 538 |
enc->enc_pic.bit_depth_luma_minus8 = 0; |
| 539 |
enc->enc_pic.bit_depth_chroma_minus8 = 0; |
| 540 |
enc->enc_pic.h264_enc_params.input_pic_order_cnt = pic->pic_order_cnt; |
| 541 |
enc->enc_pic.h264_enc_params.input_picture_structure = RENCODE_H264_PICTURE_STRUCTURE_FRAME; |
| 542 |
enc->enc_pic.h264_enc_params.interlaced_mode = RENCODE_H264_INTERLACING_MODE_PROGRESSIVE; |
| 543 |
enc->enc_pic.h264_enc_params.l0_reference_picture1_index = 0xffffffff; |
| 544 |
enc->enc_pic.enc_params.reconstructed_picture_index = pic->dpb_curr_pic; |
| 545 |
enc->enc_pic.h264_enc_params.is_reference = !pic->not_referenced; |
| 546 |
enc->enc_pic.h264_enc_params.is_long_term = pic->is_ltr; |
| 547 |
|
| 548 |
if (pic->ref_list0[0] != PIPE_H2645_LIST_REF_INVALID_ENTRY) { |
| 549 |
uint8_t ref_l0 = pic->ref_list0[0]; |
| 550 |
|
| 551 |
enc->enc_pic.enc_params.reference_picture_index = ref_l0; |
| 552 |
enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.pic_type = |
| 553 |
radeon_enc_h2645_picture_type(pic->dpb[ref_l0].picture_type); |
| 554 |
enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.pic_order_cnt = |
| 555 |
pic->dpb[ref_l0].pic_order_cnt; |
| 556 |
enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.is_long_term = |
| 557 |
pic->dpb[ref_l0].is_ltr; |
| 558 |
} else { |
| 559 |
enc->enc_pic.enc_params.reference_picture_index = 0xffffffff; |
| 560 |
} |
| 561 |
|
| 562 |
if (pic->ref_list1[0] != PIPE_H2645_LIST_REF_INVALID_ENTRY) { |
| 563 |
uint8_t ref_l1 = pic->ref_list1[0]; |
| 564 |
|
| 565 |
enc->enc_pic.h264_enc_params.l1_reference_picture0_index = ref_l1; |
| 566 |
enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.pic_type = |
| 567 |
radeon_enc_h2645_picture_type(pic->dpb[ref_l1].picture_type); |
| 568 |
enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.pic_order_cnt = |
| 569 |
pic->dpb[ref_l1].pic_order_cnt; |
| 570 |
enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.is_long_term = |
| 571 |
pic->dpb[ref_l1].is_ltr; |
| 572 |
} else { |
| 573 |
enc->enc_pic.h264_enc_params.l1_reference_picture0_index = 0xffffffff; |
| 574 |
} |
| 575 |
|
| 576 |
if (pic->ref_list1[0] != PIPE_H2645_LIST_REF_INVALID_ENTRY && |
| 577 |
pic->dpb[pic->ref_list1[0]].picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B) |
| 578 |
RADEON_ENC_ERR("B-frame as L1 reference not supported\n"); |
| 579 |
|
| 580 |
if (enc->dpb_type == DPB_TIER_2) { |
| 581 |
for (uint32_t i = 0; i < ARRAY_SIZE(pic->dpb); i++) { |
| 582 |
struct pipe_video_buffer *buf = pic->dpb[i].buffer; |
| 583 |
enc->enc_pic.dpb_bufs[i] = |
| 584 |
buf ? vl_video_buffer_get_associated_data(buf, &enc->base) : NULL; |
| 585 |
assert(!buf || enc->enc_pic.dpb_bufs[i]); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
radeon_vcn_enc_h264_get_session_param(enc, pic); |
| 590 |
radeon_vcn_enc_h264_get_dbk_param(enc, pic); |
| 591 |
radeon_vcn_enc_h264_get_rc_param(enc, pic); |
| 592 |
radeon_vcn_enc_h264_get_spec_misc_param(enc, pic); |
| 593 |
radeon_vcn_enc_quality_modes(enc, &pic->quality_modes); |
| 594 |
radeon_vcn_enc_h264_get_slice_ctrl_param(enc, pic); |
| 595 |
radeon_vcn_enc_get_input_format_param(enc); |
| 596 |
radeon_vcn_enc_get_output_format_param(enc); |
| 597 |
|
| 598 |
use_filter = enc->enc_pic.h264_deblock.disable_deblocking_filter_idc != 1; |
| 599 |
radeon_vcn_enc_get_intra_refresh_param(enc, use_filter, &pic->intra_refresh); |
| 600 |
radeon_vcn_enc_get_roi_param(enc, &pic->roi); |
| 601 |
radeon_vcn_enc_get_latency_param(enc); |
| 602 |
} |
| 603 |
|
| 604 |
static void radeon_vcn_enc_hevc_get_session_param(struct radeon_encoder *enc, |
| 605 |
struct pipe_h265_enc_picture_desc *pic) |
| 606 |
{ |
| 607 |
if (enc->enc_pic.session_init.aligned_picture_width) |
| 608 |
return; |
| 609 |
|
| 610 |
uint32_t align_width = PIPE_H265_ENC_CTB_SIZE; |
| 611 |
uint32_t align_height = 16; |
| 612 |
|
| 613 |
enc->enc_pic.session_init.encode_standard = RENCODE_ENCODE_STANDARD_HEVC; |
| 614 |
enc->enc_pic.session_init.aligned_picture_width = align(enc->base.width, align_width); |
| 615 |
enc->enc_pic.session_init.aligned_picture_height = align(enc->base.height, align_height); |
| 616 |
|
| 617 |
uint32_t padding_width = 0; |
| 618 |
uint32_t padding_height = 0; |
| 619 |
uint32_t max_padding_width = align_width - 2; |
| 620 |
uint32_t max_padding_height = align_height - 2; |
| 621 |
|
| 622 |
if (enc->enc_pic.session_init.aligned_picture_width > enc->source->width) |
| 623 |
padding_width = enc->enc_pic.session_init.aligned_picture_width - enc->source->width; |
| 624 |
if (enc->enc_pic.session_init.aligned_picture_height > enc->source->height) |
| 625 |
padding_height = enc->enc_pic.session_init.aligned_picture_height - enc->source->height; |
| 626 |
|
| 627 |
|
| 628 |
if (padding_width > max_padding_width || padding_height > max_padding_height) |
| 629 |
RADEON_ENC_ERR("Input surface size doesn't match aligned size\n"); |
| 630 |
|
| 631 |
if (pic->seq.conformance_window_flag) { |
| 632 |
uint32_t pad_w = |
| 633 |
(pic->seq.conf_win_left_offset + pic->seq.conf_win_right_offset) * 2; |
| 634 |
uint32_t pad_h = |
| 635 |
(pic->seq.conf_win_top_offset + pic->seq.conf_win_bottom_offset) * 2; |
| 636 |
padding_width = CLAMP(pad_w, padding_width, max_padding_width); |
| 637 |
padding_height = CLAMP(pad_h, padding_height, max_padding_height); |
| 638 |
} |
| 639 |
|
| 640 |
enc->enc_pic.session_init.padding_width = padding_width; |
| 641 |
enc->enc_pic.session_init.padding_height = padding_height; |
| 642 |
} |
| 643 |
|
| 644 |
static void radeon_vcn_enc_hevc_get_dbk_param(struct radeon_encoder *enc, |
| 645 |
struct pipe_h265_enc_picture_desc *pic) |
| 646 |
{ |
| 647 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 648 |
|
| 649 |
enc->enc_pic.hevc_deblock.deblocking_filter_disabled = 0; |
| 650 |
enc->enc_pic.hevc_deblock.beta_offset_div2 = 0; |
| 651 |
enc->enc_pic.hevc_deblock.tc_offset_div2 = 0; |
| 652 |
enc->enc_pic.hevc_deblock.disable_sao = |
| 653 |
sscreen->info.vcn_ip_version < VCN_2_0_0 || |
| 654 |
!pic->seq.sample_adaptive_offset_enabled_flag; |
| 655 |
|
| 656 |
if (pic->pic.deblocking_filter_override_enabled_flag && |
| 657 |
pic->slice.deblocking_filter_override_flag) { |
| 658 |
enc->enc_pic.hevc_deblock.deblocking_filter_disabled = |
| 659 |
pic->slice.slice_deblocking_filter_disabled_flag; |
| 660 |
enc->enc_pic.hevc_deblock.beta_offset_div2 = pic->slice.slice_beta_offset_div2; |
| 661 |
enc->enc_pic.hevc_deblock.tc_offset_div2 = pic->slice.slice_tc_offset_div2; |
| 662 |
} else if (pic->pic.deblocking_filter_control_present_flag) { |
| 663 |
enc->enc_pic.hevc_deblock.deblocking_filter_disabled = |
| 664 |
pic->pic.pps_deblocking_filter_disabled_flag; |
| 665 |
enc->enc_pic.hevc_deblock.beta_offset_div2 = pic->slice.slice_beta_offset_div2; |
| 666 |
enc->enc_pic.hevc_deblock.tc_offset_div2 = pic->slice.slice_tc_offset_div2; |
| 667 |
} |
| 668 |
|
| 669 |
if (pic->pic.pps_slice_chroma_qp_offsets_present_flag) { |
| 670 |
enc->enc_pic.hevc_deblock.cb_qp_offset = pic->slice.slice_cb_qp_offset; |
| 671 |
enc->enc_pic.hevc_deblock.cr_qp_offset = pic->slice.slice_cr_qp_offset; |
| 672 |
} else { |
| 673 |
enc->enc_pic.hevc_deblock.cb_qp_offset = pic->pic.pps_cb_qp_offset; |
| 674 |
enc->enc_pic.hevc_deblock.cr_qp_offset = pic->pic.pps_cr_qp_offset; |
| 675 |
} |
| 676 |
|
| 677 |
if (pic->pic.pps_loop_filter_across_slices_enabled_flag && |
| 678 |
(!enc->enc_pic.hevc_deblock.disable_sao || |
| 679 |
!enc->enc_pic.hevc_deblock.deblocking_filter_disabled)) { |
| 680 |
enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled = |
| 681 |
pic->slice.slice_loop_filter_across_slices_enabled_flag; |
| 682 |
} else { |
| 683 |
enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled = |
| 684 |
pic->pic.pps_loop_filter_across_slices_enabled_flag; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
static bool cu_qp_delta_supported(struct si_screen *sscreen) |
| 689 |
{ |
| 690 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0) |
| 691 |
return true; |
| 692 |
else if (sscreen->info.vcn_ip_version >= VCN_4_0_0) |
| 693 |
return sscreen->info.vcn_enc_minor_version >= 7; |
| 694 |
else if (sscreen->info.vcn_ip_version >= VCN_3_0_0) |
| 695 |
return sscreen->info.vcn_enc_minor_version >= 26; |
| 696 |
else if (sscreen->info.vcn_ip_version >= VCN_2_0_0) |
| 697 |
return sscreen->info.vcn_enc_minor_version >= 20; |
| 698 |
else |
| 699 |
return false; |
| 700 |
} |
| 701 |
|
| 702 |
static bool transform_skip_supported(struct si_screen *sscreen) |
| 703 |
{ |
| 704 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0) |
| 705 |
return true; |
| 706 |
else if (sscreen->info.vcn_ip_version >= VCN_4_0_0) |
| 707 |
return sscreen->info.vcn_enc_minor_version >= 2; |
| 708 |
else if (sscreen->info.vcn_ip_version >= VCN_3_0_0) |
| 709 |
return sscreen->info.vcn_enc_minor_version >= 23; |
| 710 |
else |
| 711 |
return false; |
| 712 |
} |
| 713 |
|
| 714 |
static void radeon_vcn_enc_hevc_get_spec_misc_param(struct radeon_encoder *enc, |
| 715 |
struct pipe_h265_enc_picture_desc *pic) |
| 716 |
{ |
| 717 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 718 |
|
| 719 |
enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 = |
| 720 |
pic->seq.log2_min_luma_coding_block_size_minus3; |
| 721 |
enc->enc_pic.hevc_spec_misc.amp_disabled = !pic->seq.amp_enabled_flag; |
| 722 |
enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled = |
| 723 |
pic->seq.strong_intra_smoothing_enabled_flag; |
| 724 |
|
| 725 |
enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag = |
| 726 |
!enc->enc_pic.rc_session_init.rate_control_method && |
| 727 |
pic->pic.constrained_intra_pred_flag; |
| 728 |
enc->enc_pic.hevc_spec_misc.cabac_init_flag = pic->slice.cabac_init_flag; |
| 729 |
enc->enc_pic.hevc_spec_misc.half_pel_enabled = 1; |
| 730 |
enc->enc_pic.hevc_spec_misc.quarter_pel_enabled = 1; |
| 731 |
enc->enc_pic.hevc_spec_misc.transform_skip_disabled = |
| 732 |
!transform_skip_supported(sscreen) || |
| 733 |
!pic->pic.transform_skip_enabled_flag; |
| 734 |
enc->enc_pic.hevc_spec_misc.cu_qp_delta_enabled_flag = |
| 735 |
(cu_qp_delta_supported(sscreen) && pic->pic.cu_qp_delta_enabled_flag) || |
| 736 |
enc->enc_pic.enc_qp_map.qp_map_type || |
| 737 |
enc->enc_pic.rc_session_init.rate_control_method; |
| 738 |
} |
| 739 |
|
| 740 |
static void radeon_vcn_enc_hevc_get_rc_param(struct radeon_encoder *enc, |
| 741 |
struct pipe_h265_enc_picture_desc *pic) |
| 742 |
{ |
| 743 |
uint32_t frame_rate_den, frame_rate_num, max_qp; |
| 744 |
|
| 745 |
enc->enc_pic.num_temporal_layers = pic->seq.num_temporal_layers ? pic->seq.num_temporal_layers : 1; |
| 746 |
enc->enc_pic.temporal_id = MIN2(pic->pic.temporal_id, enc->enc_pic.num_temporal_layers - 1); |
| 747 |
|
| 748 |
for (int i = 0; i < enc->enc_pic.num_temporal_layers; i++) { |
| 749 |
enc->enc_pic.rc_layer_init[i].target_bit_rate = pic->rc[i].target_bitrate; |
| 750 |
enc->enc_pic.rc_layer_init[i].peak_bit_rate = pic->rc[i].peak_bitrate; |
| 751 |
frame_rate_den = pic->rc[i].frame_rate_den; |
| 752 |
frame_rate_num = pic->rc[i].frame_rate_num; |
| 753 |
radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num); |
| 754 |
enc->enc_pic.rc_layer_init[i].frame_rate_den = frame_rate_den; |
| 755 |
enc->enc_pic.rc_layer_init[i].frame_rate_num = frame_rate_num; |
| 756 |
enc->enc_pic.rc_layer_init[i].vbv_buffer_size = pic->rc[i].vbv_buffer_size; |
| 757 |
enc->enc_pic.rc_layer_init[i].avg_target_bits_per_picture = |
| 758 |
radeon_vcn_per_frame_integer(pic->rc[i].target_bitrate, |
| 759 |
frame_rate_den, |
| 760 |
frame_rate_num); |
| 761 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_integer = |
| 762 |
radeon_vcn_per_frame_integer(pic->rc[i].peak_bitrate, |
| 763 |
frame_rate_den, |
| 764 |
frame_rate_num); |
| 765 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_fractional = |
| 766 |
radeon_vcn_per_frame_frac(pic->rc[i].peak_bitrate, |
| 767 |
frame_rate_den, |
| 768 |
frame_rate_num); |
| 769 |
} |
| 770 |
enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc[0].vbv_buf_lv; |
| 771 |
enc->enc_pic.rc_per_pic.qp_obs = pic->rc[0].quant_i_frames; |
| 772 |
enc->enc_pic.rc_per_pic.min_qp_app_obs = pic->rc[0].min_qp; |
| 773 |
enc->enc_pic.rc_per_pic.max_qp_app_obs = pic->rc[0].max_qp ? pic->rc[0].max_qp : 51; |
| 774 |
enc->enc_pic.rc_per_pic.qp_i = pic->rc[0].quant_i_frames; |
| 775 |
enc->enc_pic.rc_per_pic.qp_p = pic->rc[0].quant_p_frames; |
| 776 |
enc->enc_pic.rc_per_pic.min_qp_i = pic->rc[0].min_qp; |
| 777 |
enc->enc_pic.rc_per_pic.min_qp_p = pic->rc[0].min_qp; |
| 778 |
max_qp = pic->rc[0].max_qp ? pic->rc[0].max_qp : 51; |
| 779 |
enc->enc_pic.rc_per_pic.max_qp_i = max_qp; |
| 780 |
enc->enc_pic.rc_per_pic.max_qp_p = max_qp; |
| 781 |
enc->enc_pic.rc_per_pic.enabled_filler_data = 0; |
| 782 |
enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rc[0].skip_frame_enable; |
| 783 |
enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc[0].enforce_hrd; |
| 784 |
enc->enc_pic.rc_per_pic.qvbr_quality_level = pic->rc[0].vbr_quality_factor; |
| 785 |
switch (pic->rc[0].rate_ctrl_method) { |
| 786 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE: |
| 787 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 788 |
break; |
| 789 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP: |
| 790 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT: |
| 791 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR; |
| 792 |
enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc[0].fill_data_enable; |
| 793 |
break; |
| 794 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP: |
| 795 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE: |
| 796 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 797 |
RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR; |
| 798 |
break; |
| 799 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_QUALITY_VARIABLE: |
| 800 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 801 |
RENCODE_RATE_CONTROL_METHOD_QUALITY_VBR; |
| 802 |
break; |
| 803 |
default: |
| 804 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 805 |
} |
| 806 |
enc->enc_pic.rc_per_pic.max_au_size_obs = pic->rc[0].max_au_size; |
| 807 |
enc->enc_pic.rc_per_pic.max_au_size_i = pic->rc[0].max_au_size; |
| 808 |
enc->enc_pic.rc_per_pic.max_au_size_p = pic->rc[0].max_au_size; |
| 809 |
} |
| 810 |
|
| 811 |
static void radeon_vcn_enc_hevc_get_slice_ctrl_param(struct radeon_encoder *enc, |
| 812 |
struct pipe_h265_enc_picture_desc *pic) |
| 813 |
{ |
| 814 |
uint32_t num_ctbs_total, num_ctbs_in_slice; |
| 815 |
|
| 816 |
num_ctbs_total = |
| 817 |
DIV_ROUND_UP(pic->seq.pic_width_in_luma_samples, PIPE_H265_ENC_CTB_SIZE) * |
| 818 |
DIV_ROUND_UP(pic->seq.pic_height_in_luma_samples, PIPE_H265_ENC_CTB_SIZE); |
| 819 |
|
| 820 |
if (pic->num_slice_descriptors <= 1) { |
| 821 |
num_ctbs_in_slice = num_ctbs_total; |
| 822 |
} else { |
| 823 |
bool use_app_config = true; |
| 824 |
num_ctbs_in_slice = pic->slices_descriptors[0].num_ctu_in_slice; |
| 825 |
|
| 826 |
|
| 827 |
for (unsigned i = 1; i < pic->num_slice_descriptors - 1; i++) { |
| 828 |
if (num_ctbs_in_slice != pic->slices_descriptors[i].num_ctu_in_slice) |
| 829 |
use_app_config = false; |
| 830 |
} |
| 831 |
|
| 832 |
if (pic->slices_descriptors[pic->num_slice_descriptors - 1].num_ctu_in_slice > num_ctbs_in_slice) |
| 833 |
use_app_config = false; |
| 834 |
|
| 835 |
if (!use_app_config) { |
| 836 |
assert(num_ctbs_total >= pic->num_slice_descriptors); |
| 837 |
num_ctbs_in_slice = |
| 838 |
(num_ctbs_total + pic->num_slice_descriptors - 1) / pic->num_slice_descriptors; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
num_ctbs_in_slice = MAX2(4, num_ctbs_in_slice); |
| 843 |
|
| 844 |
enc->enc_pic.hevc_slice_ctrl.slice_control_mode = RENCODE_HEVC_SLICE_CONTROL_MODE_FIXED_CTBS; |
| 845 |
enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice = |
| 846 |
num_ctbs_in_slice; |
| 847 |
enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment = |
| 848 |
num_ctbs_in_slice; |
| 849 |
|
| 850 |
if (ac_vcn_enc_variable_slice_mode_supported(&((struct si_screen *)enc->screen)->info, |
| 851 |
enc->enc_pic.quality_modes.pre_encode_mode) && |
| 852 |
pic->num_slice_descriptors <= RENCODE_MAX_NUM_SLICES) |
| 853 |
{ |
| 854 |
uint32_t cumulative_ctbs = 0; |
| 855 |
enc->enc_pic.hevc_slice_info_var.num_slice_segments = pic->num_slice_descriptors; |
| 856 |
for (unsigned i = 0; i < pic->num_slice_descriptors; i++) { |
| 857 |
enc->enc_pic.hevc_slice_info_var.slice_segment_info[i].num_ctbs_per_segment = |
| 858 |
pic->slices_descriptors[i].num_ctu_in_slice; |
| 859 |
enc->enc_pic.hevc_slice_info_var.slice_segment_info[i].is_independent = 1; |
| 860 |
cumulative_ctbs += pic->slices_descriptors[i].num_ctu_in_slice; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
if (cumulative_ctbs == num_ctbs_total) |
| 866 |
enc->enc_pic.slice_ctrl.slice_control_mode = RENCODE_HEVC_SLICE_CONTROL_MODE_VARIABLE_CTBS; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
static void radeon_vcn_enc_hevc_get_param(struct radeon_encoder *enc, |
| 871 |
struct pipe_h265_enc_picture_desc *pic) |
| 872 |
{ |
| 873 |
enc->enc_pic.hevc.desc = pic; |
| 874 |
enc->enc_pic.picture_type = pic->picture_type; |
| 875 |
enc->enc_pic.enc_params.reference_picture_index = |
| 876 |
pic->ref_list0[0] == PIPE_H2645_LIST_REF_INVALID_ENTRY ? 0xffffffff : pic->ref_list0[0]; |
| 877 |
enc->enc_pic.enc_params.reconstructed_picture_index = pic->dpb_curr_pic; |
| 878 |
enc->enc_pic.bit_depth_luma_minus8 = pic->seq.bit_depth_luma_minus8; |
| 879 |
enc->enc_pic.bit_depth_chroma_minus8 = pic->seq.bit_depth_chroma_minus8; |
| 880 |
enc->enc_pic.nal_unit_type = pic->pic.nal_unit_type; |
| 881 |
|
| 882 |
if (enc->dpb_type == DPB_TIER_2) { |
| 883 |
for (uint32_t i = 0; i < ARRAY_SIZE(pic->dpb); i++) { |
| 884 |
struct pipe_video_buffer *buf = pic->dpb[i].buffer; |
| 885 |
enc->enc_pic.dpb_bufs[i] = |
| 886 |
buf ? vl_video_buffer_get_associated_data(buf, &enc->base) : NULL; |
| 887 |
assert(!buf || enc->enc_pic.dpb_bufs[i]); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
radeon_vcn_enc_hevc_get_session_param(enc, pic); |
| 892 |
radeon_vcn_enc_hevc_get_dbk_param(enc, pic); |
| 893 |
radeon_vcn_enc_hevc_get_rc_param(enc, pic); |
| 894 |
radeon_vcn_enc_quality_modes(enc, &pic->quality_modes); |
| 895 |
radeon_vcn_enc_hevc_get_slice_ctrl_param(enc, pic); |
| 896 |
radeon_vcn_enc_get_input_format_param(enc); |
| 897 |
radeon_vcn_enc_get_output_format_param(enc); |
| 898 |
radeon_vcn_enc_get_intra_refresh_param(enc, |
| 899 |
!(enc->enc_pic.hevc_deblock.deblocking_filter_disabled), |
| 900 |
&pic->intra_refresh); |
| 901 |
radeon_vcn_enc_get_roi_param(enc, &pic->roi); |
| 902 |
radeon_vcn_enc_hevc_get_spec_misc_param(enc, pic); |
| 903 |
radeon_vcn_enc_get_latency_param(enc); |
| 904 |
} |
| 905 |
|
| 906 |
static void radeon_vcn_enc_av1_get_session_param(struct radeon_encoder *enc, |
| 907 |
struct pipe_av1_enc_picture_desc *pic) |
| 908 |
{ |
| 909 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 910 |
|
| 911 |
if (enc->enc_pic.session_init.aligned_picture_width) |
| 912 |
return; |
| 913 |
|
| 914 |
enc->enc_pic.session_init.encode_standard = RENCODE_ENCODE_STANDARD_AV1; |
| 915 |
|
| 916 |
uint32_t width = enc->enc_pic.pic_width_in_luma_samples; |
| 917 |
uint32_t height = enc->enc_pic.pic_height_in_luma_samples; |
| 918 |
uint32_t align_width, align_height; |
| 919 |
|
| 920 |
if (sscreen->info.vcn_ip_version < VCN_5_0_0) { |
| 921 |
align_width = PIPE_AV1_ENC_SB_SIZE; |
| 922 |
align_height = 16; |
| 923 |
enc->enc_pic.session_init.aligned_picture_width = align(width, align_width); |
| 924 |
enc->enc_pic.session_init.aligned_picture_height = align(height, align_height); |
| 925 |
if (!(height % 8) && (height % 16)) |
| 926 |
enc->enc_pic.session_init.aligned_picture_height = height + 2; |
| 927 |
if (sscreen->info.vcn_ip_version == VCN_4_0_2 || |
| 928 |
sscreen->info.vcn_ip_version == VCN_4_0_5 || |
| 929 |
sscreen->info.vcn_ip_version == VCN_4_0_6) |
| 930 |
enc->enc_pic.session_init.WA_flags = 1; |
| 931 |
} else { |
| 932 |
align_width = 8; |
| 933 |
align_height = 2; |
| 934 |
enc->enc_pic.session_init.aligned_picture_width = align(width, align_width); |
| 935 |
enc->enc_pic.session_init.aligned_picture_height = align(height, align_height); |
| 936 |
} |
| 937 |
enc->enc_pic.av1.coded_width = enc->enc_pic.session_init.aligned_picture_width; |
| 938 |
enc->enc_pic.av1.coded_height = enc->enc_pic.session_init.aligned_picture_height; |
| 939 |
|
| 940 |
uint32_t padding_width = 0; |
| 941 |
uint32_t padding_height = 0; |
| 942 |
uint32_t max_padding_width = align_width - 2; |
| 943 |
uint32_t max_padding_height = align_height - 2; |
| 944 |
|
| 945 |
if (enc->enc_pic.session_init.aligned_picture_width > enc->source->width) |
| 946 |
padding_width = enc->enc_pic.session_init.aligned_picture_width - enc->source->width; |
| 947 |
if (enc->enc_pic.session_init.aligned_picture_height > enc->source->height) |
| 948 |
padding_height = enc->enc_pic.session_init.aligned_picture_height - enc->source->height; |
| 949 |
|
| 950 |
|
| 951 |
if (padding_width > max_padding_width || padding_height > max_padding_height) |
| 952 |
RADEON_ENC_ERR("Input surface size doesn't match aligned size\n"); |
| 953 |
|
| 954 |
padding_width = MAX2(padding_width, enc->enc_pic.session_init.aligned_picture_width - width); |
| 955 |
padding_height = MAX2(padding_height, enc->enc_pic.session_init.aligned_picture_height - height); |
| 956 |
|
| 957 |
enc->enc_pic.session_init.padding_width = padding_width; |
| 958 |
enc->enc_pic.session_init.padding_height = padding_height; |
| 959 |
} |
| 960 |
|
| 961 |
static void radeon_vcn_enc_av1_get_spec_misc_param(struct radeon_encoder *enc, |
| 962 |
struct pipe_av1_enc_picture_desc *pic) |
| 963 |
{ |
| 964 |
enc->enc_pic.av1_spec_misc.cdef_mode = pic->seq.seq_bits.enable_cdef; |
| 965 |
enc->enc_pic.av1_spec_misc.disable_cdf_update = pic->disable_cdf_update; |
| 966 |
enc->enc_pic.av1_spec_misc.disable_frame_end_update_cdf = pic->disable_frame_end_update_cdf; |
| 967 |
enc->enc_pic.av1_spec_misc.palette_mode_enable = pic->palette_mode_enable; |
| 968 |
enc->enc_pic.av1_spec_misc.cdef_bits = pic->cdef.cdef_bits; |
| 969 |
enc->enc_pic.av1_spec_misc.cdef_damping_minus3 = pic->cdef.cdef_damping_minus_3; |
| 970 |
for (int i = 0; i < (pic->cdef.cdef_bits << 1); i++ ){ |
| 971 |
enc->enc_pic.av1_spec_misc.cdef_y_pri_strength[i] = (pic->cdef.cdef_y_strengths[i] >> 2); |
| 972 |
enc->enc_pic.av1_spec_misc.cdef_y_sec_strength[i] = (pic->cdef.cdef_y_strengths[i] & 0x3); |
| 973 |
enc->enc_pic.av1_spec_misc.cdef_uv_pri_strength[i] = (pic->cdef.cdef_uv_strengths[i] >> 2); |
| 974 |
enc->enc_pic.av1_spec_misc.cdef_uv_sec_strength[i] = (pic->cdef.cdef_uv_strengths[i] & 0x3); |
| 975 |
} |
| 976 |
|
| 977 |
enc->enc_pic.av1_spec_misc.delta_q_y_dc = pic->quantization.y_dc_delta_q; |
| 978 |
enc->enc_pic.av1_spec_misc.delta_q_u_dc = pic->quantization.u_dc_delta_q; |
| 979 |
enc->enc_pic.av1_spec_misc.delta_q_u_ac = pic->quantization.u_ac_delta_q; |
| 980 |
enc->enc_pic.av1_spec_misc.delta_q_v_dc = pic->quantization.v_dc_delta_q; |
| 981 |
enc->enc_pic.av1_spec_misc.delta_q_v_ac = pic->quantization.v_ac_delta_q; |
| 982 |
|
| 983 |
if (enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_KEY) |
| 984 |
enc->enc_pic.av1_spec_misc.separate_delta_q = |
| 985 |
(pic->quantization.u_dc_delta_q != pic->quantization.v_dc_delta_q) || |
| 986 |
(pic->quantization.u_ac_delta_q != pic->quantization.v_ac_delta_q); |
| 987 |
|
| 988 |
if (pic->allow_screen_content_tools && pic->force_integer_mv) |
| 989 |
enc->enc_pic.av1_spec_misc.mv_precision = RENCODE_AV1_MV_PRECISION_FORCE_INTEGER_MV; |
| 990 |
else if (pic->allow_high_precision_mv) |
| 991 |
enc->enc_pic.av1_spec_misc.mv_precision = RENCODE_AV1_MV_PRECISION_ALLOW_HIGH_PRECISION; |
| 992 |
else |
| 993 |
enc->enc_pic.av1_spec_misc.mv_precision = RENCODE_AV1_MV_PRECISION_DISALLOW_HIGH_PRECISION; |
| 994 |
} |
| 995 |
|
| 996 |
static void radeon_vcn_enc_av1_get_rc_param(struct radeon_encoder *enc, |
| 997 |
struct pipe_av1_enc_picture_desc *pic) |
| 998 |
{ |
| 999 |
uint32_t frame_rate_den, frame_rate_num, min_qp, max_qp; |
| 1000 |
|
| 1001 |
enc->enc_pic.num_temporal_layers = pic->seq.num_temporal_layers ? pic->seq.num_temporal_layers : 1; |
| 1002 |
enc->enc_pic.temporal_id = MIN2(pic->temporal_id, enc->enc_pic.num_temporal_layers - 1); |
| 1003 |
|
| 1004 |
for (int i = 0; i < ARRAY_SIZE(enc->enc_pic.rc_layer_init); i++) { |
| 1005 |
enc->enc_pic.rc_layer_init[i].target_bit_rate = pic->rc[i].target_bitrate; |
| 1006 |
enc->enc_pic.rc_layer_init[i].peak_bit_rate = pic->rc[i].peak_bitrate; |
| 1007 |
frame_rate_den = pic->rc[i].frame_rate_den; |
| 1008 |
frame_rate_num = pic->rc[i].frame_rate_num; |
| 1009 |
radeon_vcn_enc_invalid_frame_rate(&frame_rate_den, &frame_rate_num); |
| 1010 |
enc->enc_pic.rc_layer_init[i].frame_rate_den = frame_rate_den; |
| 1011 |
enc->enc_pic.rc_layer_init[i].frame_rate_num = frame_rate_num; |
| 1012 |
enc->enc_pic.rc_layer_init[i].vbv_buffer_size = pic->rc[i].vbv_buffer_size; |
| 1013 |
enc->enc_pic.rc_layer_init[i].avg_target_bits_per_picture = |
| 1014 |
radeon_vcn_per_frame_integer(pic->rc[i].target_bitrate, |
| 1015 |
frame_rate_den, |
| 1016 |
frame_rate_num); |
| 1017 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_integer = |
| 1018 |
radeon_vcn_per_frame_integer(pic->rc[i].peak_bitrate, |
| 1019 |
frame_rate_den, |
| 1020 |
frame_rate_num); |
| 1021 |
enc->enc_pic.rc_layer_init[i].peak_bits_per_picture_fractional = |
| 1022 |
radeon_vcn_per_frame_frac(pic->rc[i].peak_bitrate, |
| 1023 |
frame_rate_den, |
| 1024 |
frame_rate_num); |
| 1025 |
} |
| 1026 |
enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc[0].vbv_buf_lv; |
| 1027 |
enc->enc_pic.rc_per_pic.qp_obs = pic->rc[0].qp; |
| 1028 |
enc->enc_pic.rc_per_pic.min_qp_app_obs = pic->rc[0].min_qp ? pic->rc[0].min_qp : 1; |
| 1029 |
enc->enc_pic.rc_per_pic.max_qp_app_obs = pic->rc[0].max_qp ? pic->rc[0].max_qp : 255; |
| 1030 |
enc->enc_pic.rc_per_pic.qp_i = pic->rc[0].qp; |
| 1031 |
enc->enc_pic.rc_per_pic.qp_p = pic->rc[0].qp_inter; |
| 1032 |
enc->enc_pic.rc_per_pic.qp_b = pic->rc[0].qp_inter; |
| 1033 |
min_qp = pic->rc[0].min_qp ? pic->rc[0].min_qp : 1; |
| 1034 |
enc->enc_pic.rc_per_pic.min_qp_i = min_qp; |
| 1035 |
enc->enc_pic.rc_per_pic.min_qp_p = min_qp; |
| 1036 |
enc->enc_pic.rc_per_pic.min_qp_b = min_qp; |
| 1037 |
max_qp = pic->rc[0].max_qp ? pic->rc[0].max_qp : 255; |
| 1038 |
enc->enc_pic.rc_per_pic.max_qp_i = max_qp; |
| 1039 |
enc->enc_pic.rc_per_pic.max_qp_p = max_qp; |
| 1040 |
enc->enc_pic.rc_per_pic.max_qp_b = max_qp; |
| 1041 |
enc->enc_pic.rc_per_pic.enabled_filler_data = 0; |
| 1042 |
enc->enc_pic.rc_per_pic.skip_frame_enable = pic->rc[0].skip_frame_enable; |
| 1043 |
enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc[0].enforce_hrd; |
| 1044 |
enc->enc_pic.rc_per_pic.qvbr_quality_level = (pic->rc[0].vbr_quality_factor + 2) / 5; |
| 1045 |
switch (pic->rc[0].rate_ctrl_method) { |
| 1046 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE: |
| 1047 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 1048 |
break; |
| 1049 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP: |
| 1050 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT: |
| 1051 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_CBR; |
| 1052 |
enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc[0].fill_data_enable; |
| 1053 |
break; |
| 1054 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP: |
| 1055 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE: |
| 1056 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 1057 |
RENCODE_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR; |
| 1058 |
break; |
| 1059 |
case PIPE_H2645_ENC_RATE_CONTROL_METHOD_QUALITY_VARIABLE: |
| 1060 |
enc->enc_pic.rc_session_init.rate_control_method = |
| 1061 |
RENCODE_RATE_CONTROL_METHOD_QUALITY_VBR; |
| 1062 |
break; |
| 1063 |
default: |
| 1064 |
enc->enc_pic.rc_session_init.rate_control_method = RENCODE_RATE_CONTROL_METHOD_NONE; |
| 1065 |
} |
| 1066 |
enc->enc_pic.rc_per_pic.max_au_size_obs = pic->rc[0].max_au_size; |
| 1067 |
enc->enc_pic.rc_per_pic.max_au_size_i = pic->rc[0].max_au_size; |
| 1068 |
enc->enc_pic.rc_per_pic.max_au_size_p = pic->rc[0].max_au_size; |
| 1069 |
enc->enc_pic.rc_per_pic.max_au_size_b = pic->rc[0].max_au_size; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static void radeon_vcn_enc_av1_get_tile_config(struct radeon_encoder *enc, |
| 1073 |
struct pipe_av1_enc_picture_desc *pic) |
| 1074 |
{ |
| 1075 |
uint32_t num_tile_cols, num_tile_rows; |
| 1076 |
|
| 1077 |
num_tile_cols = MIN2(RENCODE_AV1_TILE_CONFIG_MAX_NUM_COLS, pic->tile_cols); |
| 1078 |
num_tile_rows = MIN2(RENCODE_AV1_TILE_CONFIG_MAX_NUM_ROWS, pic->tile_rows); |
| 1079 |
|
| 1080 |
enc->enc_pic.av1_tile_config.uniform_tile_spacing = !!(pic->uniform_tile_spacing); |
| 1081 |
enc->enc_pic.av1_tile_config.num_tile_cols = pic->tile_cols; |
| 1082 |
enc->enc_pic.av1_tile_config.num_tile_rows = pic->tile_rows; |
| 1083 |
enc->enc_pic.av1_tile_config.num_tile_groups = pic->num_tile_groups; |
| 1084 |
for (int i = 0; i < num_tile_cols; i++ ) |
| 1085 |
enc->enc_pic.av1_tile_config.tile_widths[i] = pic->width_in_sbs_minus_1[i] + 1; |
| 1086 |
for (int i = 0; i < num_tile_rows; i++ ) |
| 1087 |
enc->enc_pic.av1_tile_config.tile_height[i] = pic->height_in_sbs_minus_1[i] + 1; |
| 1088 |
for (int i = 0; i < num_tile_cols * num_tile_rows; i++ ) { |
| 1089 |
enc->enc_pic.av1_tile_config.tile_groups[i].start = |
| 1090 |
(uint32_t)pic->tile_groups[i].tile_group_start; |
| 1091 |
enc->enc_pic.av1_tile_config.tile_groups[i].end = |
| 1092 |
(uint32_t)pic->tile_groups[i].tile_group_end; |
| 1093 |
} |
| 1094 |
enc->enc_pic.av1_tile_config.context_update_tile_id = pic->context_update_tile_id; |
| 1095 |
} |
| 1096 |
|
| 1097 |
static void radeon_vcn_enc_av1_get_param(struct radeon_encoder *enc, |
| 1098 |
struct pipe_av1_enc_picture_desc *pic) |
| 1099 |
{ |
| 1100 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1101 |
struct radeon_enc_pic *enc_pic = &enc->enc_pic; |
| 1102 |
|
| 1103 |
enc_pic->av1.desc = pic; |
| 1104 |
enc_pic->frame_type = pic->frame_type; |
| 1105 |
enc_pic->bit_depth_luma_minus8 = enc_pic->bit_depth_chroma_minus8 = |
| 1106 |
pic->seq.bit_depth_minus8; |
| 1107 |
enc_pic->pic_width_in_luma_samples = pic->seq.pic_width_in_luma_samples; |
| 1108 |
enc_pic->pic_height_in_luma_samples = pic->seq.pic_height_in_luma_samples; |
| 1109 |
enc_pic->av1_enc_params.cur_order_hint = pic->order_hint; |
| 1110 |
|
| 1111 |
enc_pic->enc_params.reference_picture_index = |
| 1112 |
pic->ref_list0[0] == PIPE_H2645_LIST_REF_INVALID_ENTRY ? |
| 1113 |
0xffffffff : pic->dpb_ref_frame_idx[pic->ref_list0[0]]; |
| 1114 |
enc_pic->enc_params.reconstructed_picture_index = pic->dpb_curr_pic; |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
if (pic->primary_ref_frame == 7 || |
| 1119 |
pic->ref_list0[0] == PIPE_H2645_LIST_REF_INVALID_ENTRY) |
| 1120 |
enc_pic->av1.primary_ref_frame = 7; |
| 1121 |
else if (pic->dpb_ref_frame_idx[pic->primary_ref_frame] == |
| 1122 |
pic->dpb_ref_frame_idx[pic->ref_list0[0]]) |
| 1123 |
enc_pic->av1.primary_ref_frame = pic->primary_ref_frame; |
| 1124 |
else |
| 1125 |
enc_pic->av1.primary_ref_frame = pic->ref_list0[0]; |
| 1126 |
|
| 1127 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0) { |
| 1128 |
bool allow_unidir = enc_pic->av1_unidir_rc_available || |
| 1129 |
pic->rc[0].rate_ctrl_method == PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE; |
| 1130 |
|
| 1131 |
for (uint32_t i = 0; i < RENCODE_AV1_REFS_PER_FRAME; i++) |
| 1132 |
enc_pic->av1_enc_params.ref_frames[i] = pic->dpb_ref_frame_idx[i]; |
| 1133 |
|
| 1134 |
enc_pic->av1_enc_params.lsm_reference_frame_index[0] = |
| 1135 |
pic->ref_list0[0] == PIPE_H2645_LIST_REF_INVALID_ENTRY ? 0xffffffff : pic->ref_list0[0]; |
| 1136 |
enc_pic->av1_enc_params.lsm_reference_frame_index[1] = 0xffffffff; |
| 1137 |
enc_pic->av1.compound = false; |
| 1138 |
|
| 1139 |
if (pic->ref_list1[0] != PIPE_H2645_LIST_REF_INVALID_ENTRY) { |
| 1140 |
enc_pic->av1.compound = true; |
| 1141 |
enc_pic->av1_enc_params.lsm_reference_frame_index[1] = pic->ref_list1[0]; |
| 1142 |
} else if (allow_unidir && pic->ref_list0[1] != PIPE_H2645_LIST_REF_INVALID_ENTRY) { |
| 1143 |
enc_pic->av1.compound = true; |
| 1144 |
enc_pic->av1_enc_params.lsm_reference_frame_index[1] = pic->ref_list0[1]; |
| 1145 |
} |
| 1146 |
|
| 1147 |
uint32_t skip_frames[2]; |
| 1148 |
enc_pic->av1.skip_mode_allowed = radeon_enc_av1_skip_mode_allowed(enc, skip_frames); |
| 1149 |
|
| 1150 |
if (enc_pic->av1.compound) { |
| 1151 |
enc_pic->av1_spec_misc.disallow_skip_mode = !enc_pic->av1.skip_mode_allowed; |
| 1152 |
|
| 1153 |
if (enc_pic->av1.skip_mode_allowed) { |
| 1154 |
enc_pic->av1_spec_misc.disallow_skip_mode = |
| 1155 |
skip_frames[0] != enc_pic->av1_enc_params.lsm_reference_frame_index[0] || |
| 1156 |
skip_frames[1] != enc_pic->av1_enc_params.lsm_reference_frame_index[1]; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (enc->dpb_type == DPB_TIER_2) { |
| 1162 |
for (uint32_t i = 0; i < ARRAY_SIZE(pic->dpb); i++) { |
| 1163 |
struct pipe_video_buffer *buf = pic->dpb[i].buffer; |
| 1164 |
enc->enc_pic.dpb_bufs[i] = |
| 1165 |
buf ? vl_video_buffer_get_associated_data(buf, &enc->base) : NULL; |
| 1166 |
assert(!buf || enc->enc_pic.dpb_bufs[i]); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
radeon_vcn_enc_av1_get_session_param(enc, pic); |
| 1171 |
radeon_vcn_enc_av1_get_spec_misc_param(enc, pic); |
| 1172 |
radeon_vcn_enc_av1_get_rc_param(enc, pic); |
| 1173 |
radeon_vcn_enc_av1_get_tile_config(enc, pic); |
| 1174 |
radeon_vcn_enc_get_input_format_param(enc); |
| 1175 |
radeon_vcn_enc_get_output_format_param(enc); |
| 1176 |
|
| 1177 |
radeon_vcn_enc_get_intra_refresh_param(enc, |
| 1178 |
true, |
| 1179 |
&pic->intra_refresh); |
| 1180 |
radeon_vcn_enc_get_roi_param(enc, &pic->roi); |
| 1181 |
radeon_vcn_enc_get_latency_param(enc); |
| 1182 |
radeon_vcn_enc_quality_modes(enc, &pic->quality_modes); |
| 1183 |
} |
| 1184 |
|
| 1185 |
static void radeon_vcn_enc_get_param(struct radeon_encoder *enc, struct pipe_picture_desc *picture) |
| 1186 |
{ |
| 1187 |
enc->enc_pic.enc_params.allowed_max_bitstream_size = enc->bs_size - enc->bs_offset; |
| 1188 |
|
| 1189 |
if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) |
| 1190 |
radeon_vcn_enc_h264_get_param(enc, (struct pipe_h264_enc_picture_desc *)picture); |
| 1191 |
else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_HEVC) |
| 1192 |
radeon_vcn_enc_hevc_get_param(enc, (struct pipe_h265_enc_picture_desc *)picture); |
| 1193 |
else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_AV1) |
| 1194 |
radeon_vcn_enc_av1_get_param(enc, (struct pipe_av1_enc_picture_desc *)picture); |
| 1195 |
} |
| 1196 |
|
| 1197 |
static int flush(struct radeon_encoder *enc, unsigned flags, struct pipe_fence_handle **fence) |
| 1198 |
{ |
| 1199 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1200 |
|
| 1201 |
if (sscreen->debug_flags & DBG(IB)) { |
| 1202 |
struct ac_ib_parser ib_parser = { |
| 1203 |
.f = stderr, |
| 1204 |
.ib = enc->cs.current.buf, |
| 1205 |
.num_dw = enc->cs.current.cdw, |
| 1206 |
.gfx_level = sscreen->info.gfx_level, |
| 1207 |
.vcn_version = sscreen->info.vcn_ip_version, |
| 1208 |
.family = sscreen->info.family, |
| 1209 |
.ip_type = AMD_IP_VCN_ENC, |
| 1210 |
}; |
| 1211 |
ac_parse_ib(&ib_parser, "IB"); |
| 1212 |
} |
| 1213 |
|
| 1214 |
return enc->ws->cs_flush(&enc->cs, flags, fence); |
| 1215 |
} |
| 1216 |
|
| 1217 |
static void radeon_enc_flush(struct pipe_video_codec *encoder) |
| 1218 |
{ |
| 1219 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 1220 |
flush(enc, PIPE_FLUSH_ASYNC, NULL); |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
static void radeon_enc_rec_offset(rvcn_enc_reconstructed_picture_t *recon, |
| 1225 |
uint32_t *offset, |
| 1226 |
uint32_t luma_size, |
| 1227 |
uint32_t chroma_size, |
| 1228 |
bool is_av1) |
| 1229 |
{ |
| 1230 |
if (offset) { |
| 1231 |
recon->luma_offset = *offset; |
| 1232 |
*offset += luma_size; |
| 1233 |
recon->chroma_offset = *offset; |
| 1234 |
*offset += chroma_size; |
| 1235 |
if (is_av1) { |
| 1236 |
recon->av1.av1_cdf_frame_context_offset = *offset; |
| 1237 |
*offset += RENCODE_AV1_FRAME_CONTEXT_CDF_TABLE_SIZE; |
| 1238 |
recon->av1.av1_cdef_algorithm_context_offset = *offset; |
| 1239 |
*offset += RENCODE_AV1_CDEF_ALGORITHM_FRAME_CONTEXT_SIZE; |
| 1240 |
} |
| 1241 |
} else { |
| 1242 |
recon->luma_offset = 0; |
| 1243 |
recon->chroma_offset = 0; |
| 1244 |
recon->av1.av1_cdf_frame_context_offset = 0; |
| 1245 |
recon->av1.av1_cdef_algorithm_context_offset = 0; |
| 1246 |
} |
| 1247 |
recon->chroma_v_offset = 0; |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
static void radeon_enc_rec_meta_offset(rvcn_enc_reconstructed_picture_t *recon, |
| 1252 |
uint32_t *offset, |
| 1253 |
uint32_t total_coloc_size, |
| 1254 |
uint32_t alignment, |
| 1255 |
bool has_b, |
| 1256 |
bool is_h264, |
| 1257 |
bool is_av1) |
| 1258 |
{ |
| 1259 |
uint32_t context_offset = 0; |
| 1260 |
|
| 1261 |
if (offset) { |
| 1262 |
recon->frame_context_buffer_offset = *offset; |
| 1263 |
recon->encode_metadata_offset = context_offset; |
| 1264 |
context_offset += RENCODE_MAX_METADATA_BUFFER_SIZE_PER_FRAME; |
| 1265 |
if (is_h264) { |
| 1266 |
if (has_b) { |
| 1267 |
recon->h264.colloc_buffer_offset = context_offset; |
| 1268 |
context_offset += total_coloc_size; |
| 1269 |
} else |
| 1270 |
recon->h264.colloc_buffer_offset = RENCODE_INVALID_COLOC_OFFSET; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (is_av1) { |
| 1274 |
recon->av1.av1_cdf_frame_context_offset = context_offset; |
| 1275 |
context_offset += RENCODE_AV1_FRAME_CONTEXT_CDF_TABLE_SIZE; |
| 1276 |
recon->av1.av1_cdef_algorithm_context_offset = context_offset; |
| 1277 |
context_offset += RENCODE_AV1_CDEF_ALGORITHM_FRAME_CONTEXT_SIZE; |
| 1278 |
} |
| 1279 |
context_offset = align(context_offset, alignment); |
| 1280 |
*offset += context_offset; |
| 1281 |
} else { |
| 1282 |
recon->frame_context_buffer_offset = 0; |
| 1283 |
recon->encode_metadata_offset = 0; |
| 1284 |
recon->av1.av1_cdf_frame_context_offset = 0; |
| 1285 |
recon->av1.av1_cdef_algorithm_context_offset = 0; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
static int setup_cdf(struct radeon_encoder *enc) |
| 1290 |
{ |
| 1291 |
unsigned char *p_cdf = NULL; |
| 1292 |
|
| 1293 |
enc->cdf = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, |
| 1294 |
VCN_ENC_AV1_DEFAULT_CDF_SIZE)); |
| 1295 |
if (!enc->cdf) { |
| 1296 |
RADEON_ENC_ERR("Can't create CDF buffer.\n"); |
| 1297 |
goto error; |
| 1298 |
} |
| 1299 |
|
| 1300 |
p_cdf = enc->ws->buffer_map(enc->ws, |
| 1301 |
enc->cdf->buf, |
| 1302 |
NULL, |
| 1303 |
PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY); |
| 1304 |
if (!p_cdf) |
| 1305 |
goto error; |
| 1306 |
|
| 1307 |
memcpy(p_cdf, rvcn_av1_cdf_default_table, VCN_ENC_AV1_DEFAULT_CDF_SIZE); |
| 1308 |
enc->ws->buffer_unmap(enc->ws, enc->cdf->buf); |
| 1309 |
|
| 1310 |
return 0; |
| 1311 |
|
| 1312 |
error: |
| 1313 |
return -1; |
| 1314 |
} |
| 1315 |
|
| 1316 |
static void pre_encode_size(struct radeon_encoder *enc, |
| 1317 |
uint32_t *offset) |
| 1318 |
{ |
| 1319 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1320 |
bool is_h264 = u_reduce_video_profile(enc->base.profile) |
| 1321 |
== PIPE_VIDEO_FORMAT_MPEG4_AVC; |
| 1322 |
uint32_t rec_alignment = |
| 1323 |
get_rec_alignment(u_reduce_video_profile(enc->base.profile)); |
| 1324 |
uint32_t aligned_width = align(enc->base.width, rec_alignment); |
| 1325 |
uint32_t aligned_height = align(enc->base.height, rec_alignment); |
| 1326 |
struct radeon_enc_pic *enc_pic = &enc->enc_pic; |
| 1327 |
bool has_b = enc_pic->spec_misc.b_picture_enabled; |
| 1328 |
uint32_t pre_size = DIV_ROUND_UP((aligned_width >> 2), rec_alignment) * |
| 1329 |
DIV_ROUND_UP((aligned_height >> 2), rec_alignment); |
| 1330 |
uint32_t full_size = DIV_ROUND_UP(aligned_width, rec_alignment) * |
| 1331 |
DIV_ROUND_UP(aligned_height, rec_alignment); |
| 1332 |
|
| 1333 |
enc_pic->ctx_buf.two_pass_search_center_map_offset = *offset; |
| 1334 |
|
| 1335 |
if (sscreen->info.vcn_ip_version < VCN_5_0_0) { |
| 1336 |
if (is_h264 && !has_b) |
| 1337 |
*offset += align((pre_size * 4 + full_size) * sizeof(uint32_t), enc->alignment); |
| 1338 |
else if (!is_h264) |
| 1339 |
*offset += align((pre_size * 52 + full_size) * sizeof(uint32_t), enc->alignment); |
| 1340 |
} else { |
| 1341 |
if (is_h264 && !has_b) |
| 1342 |
*offset += align(full_size * 8, enc->alignment); |
| 1343 |
else if (!is_h264) |
| 1344 |
*offset += align(full_size * 24, enc->alignment); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
static int setup_dpb(struct radeon_encoder *enc, uint32_t num_reconstructed_pictures) |
| 1349 |
{ |
| 1350 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1351 |
bool is_h264 = u_reduce_video_profile(enc->base.profile) |
| 1352 |
== PIPE_VIDEO_FORMAT_MPEG4_AVC; |
| 1353 |
bool is_av1 = u_reduce_video_profile(enc->base.profile) |
| 1354 |
== PIPE_VIDEO_FORMAT_AV1; |
| 1355 |
uint32_t rec_alignment = |
| 1356 |
get_rec_alignment(u_reduce_video_profile(enc->base.profile)); |
| 1357 |
uint32_t aligned_width = align(enc->base.width, rec_alignment); |
| 1358 |
uint32_t aligned_height = align(enc->base.height, rec_alignment); |
| 1359 |
uint32_t pitch = align(aligned_width, enc->alignment); |
| 1360 |
uint32_t luma_size, chroma_size, offset; |
| 1361 |
uint32_t pre_luma_size = 0, pre_chroma_size = 0, pre_input_plane_size = 0; |
| 1362 |
struct radeon_enc_pic *enc_pic = &enc->enc_pic; |
| 1363 |
int i; |
| 1364 |
bool has_b = enc_pic->spec_misc.b_picture_enabled; |
| 1365 |
uint32_t aligned_dpb_height = MAX2(256, aligned_height); |
| 1366 |
uint32_t total_coloc_bytes = (align((aligned_width / 16), 64) / 2) |
| 1367 |
* (aligned_height / 16); |
| 1368 |
|
| 1369 |
luma_size = align(pitch * aligned_dpb_height , enc->alignment); |
| 1370 |
chroma_size = align(luma_size / 2 , enc->alignment); |
| 1371 |
|
| 1372 |
if (enc_pic->quality_modes.pre_encode_mode) { |
| 1373 |
uint32_t scale = enc_pic->quality_modes.pre_encode_mode; |
| 1374 |
uint32_t pre_pitch = align(aligned_width / scale, enc->alignment); |
| 1375 |
uint32_t pre_aligned_height = MAX2(align(aligned_height / scale, enc->alignment), 256); |
| 1376 |
|
| 1377 |
|
| 1378 |
pre_input_plane_size = align(pitch * pre_aligned_height, enc->alignment); |
| 1379 |
pre_luma_size = align(pre_pitch * pre_aligned_height, enc->alignment); |
| 1380 |
pre_chroma_size = align(pre_luma_size / 2, enc->alignment); |
| 1381 |
} |
| 1382 |
|
| 1383 |
if (enc_pic->bit_depth_luma_minus8 || enc_pic->bit_depth_chroma_minus8) { |
| 1384 |
luma_size *= 2; |
| 1385 |
chroma_size *= 2; |
| 1386 |
pre_input_plane_size *= 2; |
| 1387 |
pre_luma_size *= 2; |
| 1388 |
pre_chroma_size *= 2; |
| 1389 |
} |
| 1390 |
|
| 1391 |
assert(num_reconstructed_pictures <= RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES); |
| 1392 |
|
| 1393 |
enc_pic->ctx_buf.rec_luma_pitch = pitch; |
| 1394 |
enc_pic->ctx_buf.pre_encode_picture_luma_pitch = pitch; |
| 1395 |
enc_pic->ctx_buf.num_reconstructed_pictures = num_reconstructed_pictures; |
| 1396 |
enc_pic->total_coloc_bytes = total_coloc_bytes; |
| 1397 |
|
| 1398 |
offset = 0; |
| 1399 |
enc->metadata_size = 0; |
| 1400 |
if (sscreen->info.vcn_ip_version < VCN_5_0_0) { |
| 1401 |
enc_pic->ctx_buf.rec_chroma_pitch = pitch; |
| 1402 |
enc_pic->ctx_buf.pre_encode_picture_chroma_pitch = pitch; |
| 1403 |
if (has_b) { |
| 1404 |
enc_pic->ctx_buf.colloc_buffer_offset = offset; |
| 1405 |
offset += total_coloc_bytes; |
| 1406 |
} else |
| 1407 |
enc_pic->ctx_buf.colloc_buffer_offset = 0; |
| 1408 |
|
| 1409 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1410 |
pre_encode_size(enc, &offset); |
| 1411 |
else |
| 1412 |
enc_pic->ctx_buf.two_pass_search_center_map_offset = 0; |
| 1413 |
|
| 1414 |
if (enc_pic->quality_modes.pre_encode_mode) { |
| 1415 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.red_offset = offset; |
| 1416 |
offset += pre_input_plane_size; |
| 1417 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.green_offset = offset; |
| 1418 |
offset += pre_input_plane_size; |
| 1419 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.blue_offset = offset; |
| 1420 |
offset += pre_input_plane_size; |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (is_av1) { |
| 1424 |
enc_pic->ctx_buf.av1.av1_sdb_intermediate_context_offset = offset; |
| 1425 |
offset += RENCODE_AV1_SDB_FRAME_CONTEXT_SIZE; |
| 1426 |
} |
| 1427 |
|
| 1428 |
for (i = 0; i < num_reconstructed_pictures; i++) { |
| 1429 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1430 |
&offset, luma_size, chroma_size, is_av1); |
| 1431 |
|
| 1432 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1433 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1434 |
&offset, pre_luma_size, pre_chroma_size, is_av1); |
| 1435 |
} |
| 1436 |
|
| 1437 |
for (; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) { |
| 1438 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1439 |
NULL, 0, 0, false); |
| 1440 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1441 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1442 |
NULL, 0, 0, false); |
| 1443 |
} |
| 1444 |
|
| 1445 |
enc->dpb_size = offset; |
| 1446 |
} else { |
| 1447 |
enc_pic->ctx_buf.rec_chroma_pitch = pitch / 2; |
| 1448 |
enc_pic->ctx_buf.pre_encode_picture_chroma_pitch = pitch / 2; |
| 1449 |
|
| 1450 |
if (is_av1) { |
| 1451 |
enc_pic->ctx_buf.av1.av1_sdb_intermediate_context_offset = offset; |
| 1452 |
offset += RENCODE_AV1_SDB_FRAME_CONTEXT_SIZE; |
| 1453 |
} else |
| 1454 |
enc_pic->ctx_buf.av1.av1_sdb_intermediate_context_offset = 0; |
| 1455 |
|
| 1456 |
if (enc_pic->quality_modes.pre_encode_mode) { |
| 1457 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.red_offset = offset; |
| 1458 |
offset += pre_input_plane_size; |
| 1459 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.green_offset = offset; |
| 1460 |
offset += pre_input_plane_size; |
| 1461 |
enc_pic->ctx_buf.pre_encode_input_picture.rgb.blue_offset = offset; |
| 1462 |
offset += pre_input_plane_size; |
| 1463 |
} |
| 1464 |
|
| 1465 |
for (i = 0; i < num_reconstructed_pictures; i++) { |
| 1466 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1467 |
&offset, luma_size, chroma_size, false); |
| 1468 |
|
| 1469 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1470 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1471 |
&offset, pre_luma_size, pre_chroma_size, false); |
| 1472 |
} |
| 1473 |
|
| 1474 |
for (; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) { |
| 1475 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1476 |
NULL, 0, 0, false); |
| 1477 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1478 |
radeon_enc_rec_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1479 |
NULL, 0, 0, false); |
| 1480 |
} |
| 1481 |
|
| 1482 |
enc->dpb_size = offset; |
| 1483 |
|
| 1484 |
|
| 1485 |
offset = 0; |
| 1486 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1487 |
pre_encode_size(enc, &offset); |
| 1488 |
else |
| 1489 |
enc_pic->ctx_buf.two_pass_search_center_map_offset = 0; |
| 1490 |
|
| 1491 |
for (i = 0; i < num_reconstructed_pictures; i++) { |
| 1492 |
radeon_enc_rec_meta_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1493 |
&offset, total_coloc_bytes, enc->alignment, has_b, is_h264, is_av1); |
| 1494 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1495 |
radeon_enc_rec_meta_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1496 |
&offset, total_coloc_bytes, enc->alignment, has_b, is_h264, is_av1); |
| 1497 |
} |
| 1498 |
for (; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) { |
| 1499 |
radeon_enc_rec_meta_offset(&enc_pic->ctx_buf.reconstructed_pictures[i], |
| 1500 |
NULL, 0, 0, false, false, false); |
| 1501 |
if (enc_pic->quality_modes.pre_encode_mode) |
| 1502 |
radeon_enc_rec_meta_offset(&enc_pic->ctx_buf.pre_encode_reconstructed_pictures[i], |
| 1503 |
NULL, 0, 0, false, false, false); |
| 1504 |
} |
| 1505 |
enc->metadata_size = offset; |
| 1506 |
} |
| 1507 |
|
| 1508 |
enc->dpb_slots = num_reconstructed_pictures; |
| 1509 |
|
| 1510 |
return enc->dpb_size; |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
static uint32_t roi_buffer_size(struct radeon_encoder *enc) |
| 1515 |
{ |
| 1516 |
uint32_t pitch_size_in_dword = 0; |
| 1517 |
rvcn_enc_qp_map_t *qp_map = &enc->enc_pic.enc_qp_map; |
| 1518 |
|
| 1519 |
if ( qp_map->version == RENCODE_QP_MAP_LEGACY){ |
| 1520 |
pitch_size_in_dword = qp_map->width_in_block; |
| 1521 |
qp_map->qp_map_pitch = qp_map->width_in_block; |
| 1522 |
} else { |
| 1523 |
|
| 1524 |
pitch_size_in_dword = DIV_ROUND_UP(qp_map->width_in_block, 2); |
| 1525 |
qp_map->qp_map_pitch = pitch_size_in_dword * 2; |
| 1526 |
} |
| 1527 |
|
| 1528 |
return pitch_size_in_dword * qp_map->height_in_block * sizeof(uint32_t); |
| 1529 |
} |
| 1530 |
|
| 1531 |
static void arrange_qp_map(void *start, |
| 1532 |
struct rvcn_enc_qp_map_region *regin, |
| 1533 |
rvcn_enc_qp_map_t *map) |
| 1534 |
{ |
| 1535 |
uint32_t i, j; |
| 1536 |
uint32_t offset; |
| 1537 |
uint32_t num_in_x = MIN2(regin->x_in_unit + regin->width_in_unit, map->width_in_block) |
| 1538 |
- regin->x_in_unit; |
| 1539 |
uint32_t num_in_y = MIN2(regin->y_in_unit + regin->height_in_unit, map->height_in_block) |
| 1540 |
- regin->y_in_unit;; |
| 1541 |
|
| 1542 |
for (j = 0; j < num_in_y; j++) { |
| 1543 |
for (i = 0; i < num_in_x; i++) { |
| 1544 |
offset = regin->x_in_unit + i + (regin->y_in_unit + j) * map->qp_map_pitch; |
| 1545 |
if (map->version == RENCODE_QP_MAP_LEGACY) |
| 1546 |
*((uint32_t *)start + offset) = (int32_t)regin->qp_delta; |
| 1547 |
else |
| 1548 |
*((int16_t *)start + offset) = |
| 1549 |
(int16_t)(regin->qp_delta << RENCODE_QP_MAP_UNIFIED_QP_BITS_SHIFT); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
static int generate_roi_map(struct radeon_encoder *enc) |
| 1559 |
{ |
| 1560 |
uint32_t width_in_block, height_in_block; |
| 1561 |
uint32_t i; |
| 1562 |
void *p_roi = NULL; |
| 1563 |
|
| 1564 |
radeon_vcn_enc_blocks_in_frame(enc, &width_in_block, &height_in_block); |
| 1565 |
|
| 1566 |
p_roi = enc->ws->buffer_map(enc->ws, |
| 1567 |
enc->roi->buf, |
| 1568 |
NULL, |
| 1569 |
PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY); |
| 1570 |
if (!p_roi) |
| 1571 |
goto error; |
| 1572 |
|
| 1573 |
memset(p_roi, 0, enc->roi_size); |
| 1574 |
|
| 1575 |
for (i = 0; i < ARRAY_SIZE(enc->enc_pic.enc_qp_map.map); i++) { |
| 1576 |
struct rvcn_enc_qp_map_region *region = &enc->enc_pic.enc_qp_map.map[i]; |
| 1577 |
if (region->is_valid) |
| 1578 |
arrange_qp_map(p_roi, region, &enc->enc_pic.enc_qp_map); |
| 1579 |
} |
| 1580 |
|
| 1581 |
enc->ws->buffer_unmap(enc->ws, enc->roi->buf); |
| 1582 |
return 0; |
| 1583 |
error: |
| 1584 |
return -1; |
| 1585 |
} |
| 1586 |
|
| 1587 |
static void radeon_enc_begin_frame(struct pipe_video_codec *encoder, |
| 1588 |
struct pipe_video_buffer *source, |
| 1589 |
struct pipe_picture_desc *picture) |
| 1590 |
{ |
| 1591 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 1592 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1593 |
struct vl_video_buffer *vid_buf; |
| 1594 |
unsigned dpb_slots = 0; |
| 1595 |
|
| 1596 |
if (enc->efc_source) { |
| 1597 |
enc->source = enc->efc_source; |
| 1598 |
enc->efc_source = NULL; |
| 1599 |
} else { |
| 1600 |
enc->source = source; |
| 1601 |
enc->input_color_volume = enc->output_color_volume = RENCODE_COLOR_VOLUME_G22_BT709; |
| 1602 |
enc->input_color_range = enc->output_color_range = RENCODE_COLOR_RANGE_FULL; |
| 1603 |
enc->output_chroma_location = RENCODE_CHROMA_LOCATION_INTERSTITIAL; |
| 1604 |
} |
| 1605 |
enc->need_rate_control = false; |
| 1606 |
enc->need_rc_per_pic = false; |
| 1607 |
|
| 1608 |
if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) { |
| 1609 |
struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture; |
| 1610 |
dpb_slots = MAX2(pic->seq.max_num_ref_frames + 1, pic->dpb_size); |
| 1611 |
enc->need_rate_control = |
| 1612 |
(enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rate_ctrl[0].target_bitrate) || |
| 1613 |
(enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rate_ctrl[0].frame_rate_num) || |
| 1614 |
(enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rate_ctrl[0].frame_rate_den); |
| 1615 |
|
| 1616 |
enc->need_rc_per_pic = |
| 1617 |
(enc->enc_pic.rc_per_pic.qp_i != pic->quant_i_frames) || |
| 1618 |
(enc->enc_pic.rc_per_pic.qp_p != pic->quant_p_frames) || |
| 1619 |
(enc->enc_pic.rc_per_pic.qp_b != pic->quant_b_frames) || |
| 1620 |
(enc->enc_pic.rc_per_pic.max_au_size_i != pic->rate_ctrl[0].max_au_size) || |
| 1621 |
(enc->enc_pic.rc_per_pic.qvbr_quality_level != pic->rate_ctrl[0].vbr_quality_factor); |
| 1622 |
} else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_HEVC) { |
| 1623 |
struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture; |
| 1624 |
dpb_slots = MAX2(pic->seq.sps_max_dec_pic_buffering_minus1[0] + 1, pic->dpb_size); |
| 1625 |
enc->need_rate_control = |
| 1626 |
(enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rc[0].target_bitrate) || |
| 1627 |
(enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rc[0].frame_rate_num) || |
| 1628 |
(enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rc[0].frame_rate_den); |
| 1629 |
|
| 1630 |
enc->need_rc_per_pic = |
| 1631 |
(enc->enc_pic.rc_per_pic.qp_i != pic->rc[0].quant_i_frames) || |
| 1632 |
(enc->enc_pic.rc_per_pic.qp_p != pic->rc[0].quant_p_frames) || |
| 1633 |
(enc->enc_pic.rc_per_pic.max_au_size_i != pic->rc[0].max_au_size) || |
| 1634 |
(enc->enc_pic.rc_per_pic.qvbr_quality_level != pic->rc[0].vbr_quality_factor); |
| 1635 |
} else if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_AV1) { |
| 1636 |
struct pipe_av1_enc_picture_desc *pic = (struct pipe_av1_enc_picture_desc *)picture; |
| 1637 |
dpb_slots = pic->dpb_size; |
| 1638 |
enc->need_rate_control = |
| 1639 |
(enc->enc_pic.rc_layer_init[0].target_bit_rate != pic->rc[0].target_bitrate) || |
| 1640 |
(enc->enc_pic.rc_layer_init[0].frame_rate_num != pic->rc[0].frame_rate_num) || |
| 1641 |
(enc->enc_pic.rc_layer_init[0].frame_rate_den != pic->rc[0].frame_rate_den); |
| 1642 |
|
| 1643 |
enc->need_rc_per_pic = |
| 1644 |
(enc->enc_pic.rc_per_pic.qp_i != pic->rc[0].qp) || |
| 1645 |
(enc->enc_pic.rc_per_pic.qp_p != pic->rc[0].qp_inter) || |
| 1646 |
(enc->enc_pic.rc_per_pic.qp_b != pic->rc[0].qp_inter) || |
| 1647 |
(enc->enc_pic.rc_per_pic.max_au_size_i != pic->rc[0].max_au_size) || |
| 1648 |
(enc->enc_pic.rc_per_pic.qvbr_quality_level != pic->rc[0].vbr_quality_factor); |
| 1649 |
|
| 1650 |
if (!enc->cdf && setup_cdf(enc)) { |
| 1651 |
RADEON_ENC_ERR("Can't create cdf buffer.\n"); |
| 1652 |
goto error; |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (enc->dpb_type == DPB_TIER_2) |
| 1657 |
dpb_slots = 0; |
| 1658 |
|
| 1659 |
radeon_vcn_enc_get_param(enc, picture); |
| 1660 |
if (enc->first_frame && setup_dpb(enc, dpb_slots)) { |
| 1661 |
enc->dpb = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, enc->dpb_size)); |
| 1662 |
if (!enc->dpb) { |
| 1663 |
RADEON_ENC_ERR("Can't create DPB buffer.\n"); |
| 1664 |
goto error; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
if ((sscreen->info.vcn_ip_version >= VCN_5_0_0) && enc->metadata_size && !enc->meta) { |
| 1669 |
enc->meta = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, enc->metadata_size)); |
| 1670 |
if (!enc->meta) { |
| 1671 |
RADEON_ENC_ERR("Can't create meta buffer.\n"); |
| 1672 |
goto error; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (dpb_slots > enc->dpb_slots) { |
| 1677 |
setup_dpb(enc, dpb_slots); |
| 1678 |
if (!si_vid_resize_buffer(enc->base.context, &enc->dpb, enc->dpb_size)) { |
| 1679 |
RADEON_ENC_ERR("Can't resize DPB buffer.\n"); |
| 1680 |
goto error; |
| 1681 |
} |
| 1682 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0 && enc->metadata_size && |
| 1683 |
!si_vid_resize_buffer(enc->base.context, &enc->meta, enc->metadata_size)) { |
| 1684 |
RADEON_ENC_ERR("Can't resize meta buffer.\n"); |
| 1685 |
goto error; |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
if (enc->enc_pic.enc_qp_map.qp_map_type != RENCODE_QP_MAP_TYPE_NONE) { |
| 1691 |
if (!enc->roi) { |
| 1692 |
enc->roi_size = roi_buffer_size(enc); |
| 1693 |
enc->roi = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_STAGING, enc->roi_size)); |
| 1694 |
if (!enc->roi) { |
| 1695 |
RADEON_ENC_ERR("Can't create ROI buffer.\n"); |
| 1696 |
goto error; |
| 1697 |
} |
| 1698 |
} |
| 1699 |
if (generate_roi_map(enc)) { |
| 1700 |
RADEON_ENC_ERR("Can't form roi map.\n"); |
| 1701 |
goto error; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
vid_buf = (struct vl_video_buffer *)enc->source; |
| 1706 |
|
| 1707 |
if (enc->source->buffer_format == PIPE_FORMAT_NV12 || |
| 1708 |
enc->source->buffer_format == PIPE_FORMAT_P010 || |
| 1709 |
enc->source->buffer_format == PIPE_FORMAT_P016) { |
| 1710 |
enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma); |
| 1711 |
enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma); |
| 1712 |
} |
| 1713 |
else { |
| 1714 |
enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma); |
| 1715 |
enc->chroma = NULL; |
| 1716 |
} |
| 1717 |
|
| 1718 |
enc->need_feedback = false; |
| 1719 |
|
| 1720 |
if (!enc->si) { |
| 1721 |
enc->si = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, 128 * 1024)); |
| 1722 |
if (!enc->si) { |
| 1723 |
RADEON_ENC_ERR("Can't create session buffer.\n"); |
| 1724 |
goto error; |
| 1725 |
} |
| 1726 |
enc->begin(enc); |
| 1727 |
flush(enc, PIPE_FLUSH_ASYNC, NULL); |
| 1728 |
enc->need_rate_control = false; |
| 1729 |
enc->need_rc_per_pic = false; |
| 1730 |
} |
| 1731 |
|
| 1732 |
return; |
| 1733 |
|
| 1734 |
error: |
| 1735 |
si_resource_reference(&enc->dpb, NULL); |
| 1736 |
si_resource_reference(&enc->si, NULL); |
| 1737 |
si_resource_reference(&enc->cdf, NULL); |
| 1738 |
si_resource_reference(&enc->roi, NULL); |
| 1739 |
si_resource_reference(&enc->meta, NULL); |
| 1740 |
} |
| 1741 |
|
| 1742 |
static uint32_t radeon_vcn_enc_encode_h264_header(struct radeon_encoder *enc, |
| 1743 |
struct pipe_enc_raw_header *header, |
| 1744 |
uint8_t *out) |
| 1745 |
{ |
| 1746 |
|
| 1747 |
const uint8_t nal_byte = header->buffer[header->buffer[2] == 0x1 ? 3 : 4]; |
| 1748 |
|
| 1749 |
switch (header->type) { |
| 1750 |
case PIPE_H264_NAL_SPS: |
| 1751 |
return radeon_enc_write_sps(enc, nal_byte, out); |
| 1752 |
case PIPE_H264_NAL_PPS: |
| 1753 |
return radeon_enc_write_pps(enc, nal_byte, out); |
| 1754 |
default: |
| 1755 |
assert(header->buffer); |
| 1756 |
memcpy(out, header->buffer, header->size); |
| 1757 |
return header->size; |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
static uint32_t radeon_vcn_enc_encode_hevc_header(struct radeon_encoder *enc, |
| 1762 |
struct pipe_enc_raw_header *header, |
| 1763 |
uint8_t *out) |
| 1764 |
{ |
| 1765 |
switch (header->type) { |
| 1766 |
case PIPE_H265_NAL_VPS: |
| 1767 |
return radeon_enc_write_vps(enc, out); |
| 1768 |
case PIPE_H265_NAL_SPS: |
| 1769 |
return radeon_enc_write_sps_hevc(enc, out); |
| 1770 |
case PIPE_H265_NAL_PPS: |
| 1771 |
return radeon_enc_write_pps_hevc(enc, out); |
| 1772 |
default: |
| 1773 |
assert(header->buffer); |
| 1774 |
memcpy(out, header->buffer, header->size); |
| 1775 |
return header->size; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
static uint32_t radeon_vcn_enc_encode_av1_header(struct radeon_encoder *enc, |
| 1780 |
struct pipe_enc_raw_header *header, |
| 1781 |
uint8_t *out) |
| 1782 |
{ |
| 1783 |
switch (header->type) { |
| 1784 |
case 1: |
| 1785 |
return radeon_enc_write_sequence_header(enc, header->buffer, out); |
| 1786 |
default: |
| 1787 |
assert(header->buffer); |
| 1788 |
memcpy(out, header->buffer, header->size); |
| 1789 |
return header->size; |
| 1790 |
} |
| 1791 |
} |
| 1792 |
|
| 1793 |
static void *radeon_vcn_enc_encode_headers(struct radeon_encoder *enc) |
| 1794 |
{ |
| 1795 |
const bool is_h264 = u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC; |
| 1796 |
const bool is_hevc = u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC; |
| 1797 |
const bool is_av1 = u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_AV1; |
| 1798 |
struct util_dynarray *headers; |
| 1799 |
unsigned num_slices = 0, num_headers = 0; |
| 1800 |
|
| 1801 |
if (is_h264) |
| 1802 |
headers = &enc->enc_pic.h264.desc->raw_headers; |
| 1803 |
else if (is_hevc) |
| 1804 |
headers = &enc->enc_pic.hevc.desc->raw_headers; |
| 1805 |
else if (is_av1) |
| 1806 |
headers = &enc->enc_pic.av1.desc->raw_headers; |
| 1807 |
else |
| 1808 |
return NULL; |
| 1809 |
|
| 1810 |
util_dynarray_foreach(headers, struct pipe_enc_raw_header, header) { |
| 1811 |
if (header->is_slice) |
| 1812 |
num_slices++; |
| 1813 |
num_headers++; |
| 1814 |
} |
| 1815 |
|
| 1816 |
if (!num_headers || !num_slices || num_headers == num_slices) |
| 1817 |
return NULL; |
| 1818 |
|
| 1819 |
size_t segments_size = |
| 1820 |
sizeof(struct rvcn_enc_output_unit_segment) * (num_headers - num_slices + 1); |
| 1821 |
struct rvcn_enc_feedback_data *data = |
| 1822 |
CALLOC_VARIANT_LENGTH_STRUCT(rvcn_enc_feedback_data, segments_size); |
| 1823 |
if (!data) |
| 1824 |
return NULL; |
| 1825 |
|
| 1826 |
uint8_t *ptr = enc->ws->buffer_map(enc->ws, enc->bs_handle, NULL, |
| 1827 |
PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY); |
| 1828 |
if (!ptr) { |
| 1829 |
RADEON_ENC_ERR("Can't map bs buffer.\n"); |
| 1830 |
FREE(data); |
| 1831 |
return NULL; |
| 1832 |
} |
| 1833 |
|
| 1834 |
unsigned offset = 0; |
| 1835 |
struct rvcn_enc_output_unit_segment *slice_segment = NULL; |
| 1836 |
|
| 1837 |
util_dynarray_foreach(headers, struct pipe_enc_raw_header, header) { |
| 1838 |
if (header->is_slice) { |
| 1839 |
if (slice_segment) |
| 1840 |
continue; |
| 1841 |
slice_segment = &data->segments[data->num_segments]; |
| 1842 |
slice_segment->is_slice = true; |
| 1843 |
} else { |
| 1844 |
unsigned size = 0; |
| 1845 |
if (is_h264) |
| 1846 |
size = radeon_vcn_enc_encode_h264_header(enc, header, ptr + offset); |
| 1847 |
else if (is_hevc) |
| 1848 |
size = radeon_vcn_enc_encode_hevc_header(enc, header, ptr + offset); |
| 1849 |
else if (is_av1) |
| 1850 |
size = radeon_vcn_enc_encode_av1_header(enc, header, ptr + offset); |
| 1851 |
data->segments[data->num_segments].size = size; |
| 1852 |
data->segments[data->num_segments].offset = offset; |
| 1853 |
offset += size; |
| 1854 |
} |
| 1855 |
data->num_segments++; |
| 1856 |
} |
| 1857 |
|
| 1858 |
enc->bs_offset = align(offset, 16); |
| 1859 |
assert(enc->bs_offset < enc->bs_size); |
| 1860 |
|
| 1861 |
assert(slice_segment); |
| 1862 |
slice_segment->offset = enc->bs_offset; |
| 1863 |
|
| 1864 |
enc->ws->buffer_unmap(enc->ws, enc->bs_handle); |
| 1865 |
|
| 1866 |
return data; |
| 1867 |
} |
| 1868 |
|
| 1869 |
static void radeon_enc_encode_bitstream(struct pipe_video_codec *encoder, |
| 1870 |
struct pipe_video_buffer *source, |
| 1871 |
struct pipe_resource *destination, void **fb) |
| 1872 |
{ |
| 1873 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 1874 |
struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)enc->source; |
| 1875 |
|
| 1876 |
if (enc->error) |
| 1877 |
return; |
| 1878 |
|
| 1879 |
enc->get_buffer(destination, &enc->bs_handle, NULL); |
| 1880 |
enc->bs_size = destination->width0; |
| 1881 |
enc->bs_offset = 0; |
| 1882 |
|
| 1883 |
*fb = enc->fb = CALLOC_STRUCT(radeon_enc_fb_buffer); |
| 1884 |
|
| 1885 |
enc->fb->res = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_STAGING, 4096)); |
| 1886 |
if (!enc->fb->res) { |
| 1887 |
RADEON_ENC_ERR("Can't create feedback buffer.\n"); |
| 1888 |
return; |
| 1889 |
} |
| 1890 |
|
| 1891 |
enc->fb->data = radeon_vcn_enc_encode_headers(enc); |
| 1892 |
|
| 1893 |
if (vid_buf->base.statistics_data) { |
| 1894 |
enc->get_buffer(vid_buf->base.statistics_data, &enc->stats, NULL); |
| 1895 |
if (enc->stats->size < sizeof(rvcn_encode_stats_type_0_t)) { |
| 1896 |
RADEON_ENC_ERR("Encoder statistics output buffer is too small.\n"); |
| 1897 |
enc->stats = NULL; |
| 1898 |
} |
| 1899 |
vid_buf->base.statistics_data = NULL; |
| 1900 |
} |
| 1901 |
else |
| 1902 |
enc->stats = NULL; |
| 1903 |
|
| 1904 |
enc->need_feedback = true; |
| 1905 |
enc->encode(enc); |
| 1906 |
} |
| 1907 |
|
| 1908 |
static bool radeon_vcn_enc_efc_supported(struct radeon_encoder *enc, |
| 1909 |
struct pipe_video_buffer *src, |
| 1910 |
const struct pipe_vpp_desc *vpp) |
| 1911 |
{ |
| 1912 |
struct si_screen *sscreen = (struct si_screen *)enc->screen; |
| 1913 |
struct si_texture *tex = (struct si_texture *)((struct vl_video_buffer *)src)->resources[0]; |
| 1914 |
enum pipe_format src_format = src->buffer_format; |
| 1915 |
enum pipe_format dst_format = vpp->dst->buffer_format; |
| 1916 |
|
| 1917 |
if (sscreen->info.vcn_ip_version < VCN_2_0_0 || |
| 1918 |
sscreen->info.vcn_ip_version == VCN_2_2_0 || |
| 1919 |
sscreen->multimedia_debug_flags & DBG(NO_EFC)) |
| 1920 |
return false; |
| 1921 |
|
| 1922 |
if (vpp->orientation != PIPE_VIDEO_VPP_ORIENTATION_DEFAULT || |
| 1923 |
vpp->blend.mode != PIPE_VIDEO_VPP_BLEND_MODE_NONE) |
| 1924 |
return false; |
| 1925 |
|
| 1926 |
if (vpp->src_region.x0 || vpp->src_region.y0 || |
| 1927 |
vpp->dst_region.x0 || vpp->dst_region.y0 || |
| 1928 |
vpp->src_region.x1 != vpp->dst_region.x1 || |
| 1929 |
vpp->src_region.y1 != vpp->dst_region.y1) |
| 1930 |
return false; |
| 1931 |
|
| 1932 |
if (vpp->in_color_primaries != vpp->out_color_primaries || |
| 1933 |
vpp->in_transfer_characteristics != vpp->out_transfer_characteristics || |
| 1934 |
(vpp->out_matrix_coefficients != PIPE_VIDEO_VPP_MCF_BT709 && |
| 1935 |
vpp->out_matrix_coefficients != PIPE_VIDEO_VPP_MCF_BT2020_NCL)) |
| 1936 |
return false; |
| 1937 |
|
| 1938 |
if (vpp->out_chroma_siting & (PIPE_VIDEO_VPP_CHROMA_SITING_VERTICAL_BOTTOM | |
| 1939 |
PIPE_VIDEO_VPP_CHROMA_SITING_HORIZONTAL_CENTER)) |
| 1940 |
return false; |
| 1941 |
|
| 1942 |
|
| 1943 |
if (tex->surface.meta_offset) |
| 1944 |
return false; |
| 1945 |
|
| 1946 |
const bool input_8bit = |
| 1947 |
src_format == PIPE_FORMAT_B8G8R8A8_UNORM || |
| 1948 |
src_format == PIPE_FORMAT_B8G8R8X8_UNORM || |
| 1949 |
src_format == PIPE_FORMAT_R8G8B8A8_UNORM || |
| 1950 |
src_format == PIPE_FORMAT_R8G8B8X8_UNORM; |
| 1951 |
|
| 1952 |
const bool input_10bit = |
| 1953 |
src_format == PIPE_FORMAT_B10G10R10A2_UNORM || |
| 1954 |
src_format == PIPE_FORMAT_B10G10R10X2_UNORM || |
| 1955 |
src_format == PIPE_FORMAT_R10G10B10A2_UNORM || |
| 1956 |
src_format == PIPE_FORMAT_R10G10B10X2_UNORM; |
| 1957 |
|
| 1958 |
|
| 1959 |
if (!input_8bit && !input_10bit) |
| 1960 |
return false; |
| 1961 |
|
| 1962 |
if (input_8bit && dst_format != PIPE_FORMAT_NV12) |
| 1963 |
return false; |
| 1964 |
|
| 1965 |
if (input_10bit && dst_format != PIPE_FORMAT_NV12 && dst_format != PIPE_FORMAT_P010) |
| 1966 |
return false; |
| 1967 |
|
| 1968 |
return true; |
| 1969 |
} |
| 1970 |
|
| 1971 |
static uint32_t radeon_vcn_enc_color_volume(enum pipe_video_vpp_matrix_coefficients coeffs) |
| 1972 |
{ |
| 1973 |
switch (coeffs) { |
| 1974 |
case PIPE_VIDEO_VPP_MCF_BT709: |
| 1975 |
return RENCODE_COLOR_VOLUME_G22_BT709; |
| 1976 |
case PIPE_VIDEO_VPP_MCF_BT2020_NCL: |
| 1977 |
return RENCODE_COLOR_VOLUME_G2084_BT2020; |
| 1978 |
default: |
| 1979 |
assert(0); |
| 1980 |
return 0; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
static uint32_t radeon_vcn_enc_color_range(enum pipe_video_vpp_color_range color_range) |
| 1985 |
{ |
| 1986 |
switch (color_range) { |
| 1987 |
case PIPE_VIDEO_VPP_CHROMA_COLOR_RANGE_REDUCED: |
| 1988 |
return RENCODE_COLOR_RANGE_STUDIO; |
| 1989 |
case PIPE_VIDEO_VPP_CHROMA_COLOR_RANGE_FULL: |
| 1990 |
default: |
| 1991 |
return RENCODE_COLOR_RANGE_FULL; |
| 1992 |
} |
| 1993 |
} |
| 1994 |
|
| 1995 |
static uint32_t radeon_vcn_enc_chroma_location(enum pipe_video_vpp_chroma_siting chroma_siting) |
| 1996 |
{ |
| 1997 |
if (chroma_siting & PIPE_VIDEO_VPP_CHROMA_SITING_VERTICAL_TOP) |
| 1998 |
return RENCODE_CHROMA_LOCATION_CO_SITE; |
| 1999 |
else |
| 2000 |
return RENCODE_CHROMA_LOCATION_INTERSTITIAL; |
| 2001 |
} |
| 2002 |
|
| 2003 |
static int radeon_enc_process_frame(struct pipe_video_codec *encoder, |
| 2004 |
struct pipe_video_buffer *source, |
| 2005 |
const struct pipe_vpp_desc *vpp) |
| 2006 |
{ |
| 2007 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2008 |
|
| 2009 |
if (radeon_vcn_enc_efc_supported(enc, source, vpp)) { |
| 2010 |
enc->efc_source = source; |
| 2011 |
enc->input_color_volume = radeon_vcn_enc_color_volume(vpp->out_matrix_coefficients); |
| 2012 |
enc->input_color_range = radeon_vcn_enc_color_range(vpp->in_color_range); |
| 2013 |
enc->output_color_volume = radeon_vcn_enc_color_volume(vpp->out_matrix_coefficients); |
| 2014 |
enc->output_color_range = radeon_vcn_enc_color_range(vpp->out_color_range); |
| 2015 |
enc->output_chroma_location = radeon_vcn_enc_chroma_location(vpp->out_chroma_siting); |
| 2016 |
return 0; |
| 2017 |
} |
| 2018 |
|
| 2019 |
return 1; |
| 2020 |
} |
| 2021 |
|
| 2022 |
static int radeon_enc_end_frame(struct pipe_video_codec *encoder, struct pipe_video_buffer *source, |
| 2023 |
struct pipe_picture_desc *picture) |
| 2024 |
{ |
| 2025 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2026 |
|
| 2027 |
if (enc->error) |
| 2028 |
return -1; |
| 2029 |
|
| 2030 |
enc->first_frame = false; |
| 2031 |
|
| 2032 |
return flush(enc, picture->flush_flags, picture->out_fence); |
| 2033 |
} |
| 2034 |
|
| 2035 |
static void radeon_enc_destroy(struct pipe_video_codec *encoder) |
| 2036 |
{ |
| 2037 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2038 |
|
| 2039 |
if (enc->si) { |
| 2040 |
enc->need_feedback = false; |
| 2041 |
enc->destroy(enc); |
| 2042 |
flush(enc, PIPE_FLUSH_ASYNC, NULL); |
| 2043 |
si_resource_reference(&enc->si, NULL); |
| 2044 |
} |
| 2045 |
|
| 2046 |
si_resource_reference(&enc->dpb, NULL); |
| 2047 |
si_resource_reference(&enc->cdf, NULL); |
| 2048 |
si_resource_reference(&enc->roi, NULL); |
| 2049 |
si_resource_reference(&enc->meta, NULL); |
| 2050 |
enc->ws->cs_destroy(&enc->cs); |
| 2051 |
if (enc->ectx) |
| 2052 |
enc->ectx->destroy(enc->ectx); |
| 2053 |
|
| 2054 |
FREE(enc); |
| 2055 |
} |
| 2056 |
|
| 2057 |
static void radeon_enc_get_feedback(struct pipe_video_codec *encoder, void *feedback, |
| 2058 |
unsigned *size, struct pipe_enc_feedback_metadata *metadata) |
| 2059 |
{ |
| 2060 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2061 |
struct radeon_enc_fb_buffer *fb = feedback; |
| 2062 |
|
| 2063 |
uint32_t *ptr = enc->ws->buffer_map(enc->ws, fb->res->buf, NULL, |
| 2064 |
PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY); |
| 2065 |
if (ptr[1]) |
| 2066 |
*size = ptr[6] - ptr[8]; |
| 2067 |
else |
| 2068 |
*size = 0; |
| 2069 |
enc->ws->buffer_unmap(enc->ws, fb->res->buf); |
| 2070 |
|
| 2071 |
metadata->present_metadata = PIPE_VIDEO_FEEDBACK_METADATA_TYPE_CODEC_UNIT_LOCATION; |
| 2072 |
|
| 2073 |
if (fb->data) { |
| 2074 |
struct rvcn_enc_feedback_data *data = fb->data; |
| 2075 |
metadata->codec_unit_metadata_count = data->num_segments; |
| 2076 |
for (unsigned i = 0; i < data->num_segments; i++) { |
| 2077 |
metadata->codec_unit_metadata[i].offset = data->segments[i].offset; |
| 2078 |
if (data->segments[i].is_slice) { |
| 2079 |
metadata->codec_unit_metadata[i].size = *size; |
| 2080 |
metadata->codec_unit_metadata[i].flags = 0; |
| 2081 |
} else { |
| 2082 |
metadata->codec_unit_metadata[i].size = data->segments[i].size; |
| 2083 |
metadata->codec_unit_metadata[i].flags = PIPE_VIDEO_CODEC_UNIT_LOCATION_FLAG_SINGLE_NALU; |
| 2084 |
} |
| 2085 |
} |
| 2086 |
FREE(fb->data); |
| 2087 |
fb->data = NULL; |
| 2088 |
} else { |
| 2089 |
metadata->codec_unit_metadata_count = 1; |
| 2090 |
metadata->codec_unit_metadata[0].offset = 0; |
| 2091 |
metadata->codec_unit_metadata[0].size = *size; |
| 2092 |
metadata->codec_unit_metadata[0].flags = 0; |
| 2093 |
} |
| 2094 |
|
| 2095 |
si_resource_reference(&fb->res, NULL); |
| 2096 |
FREE(fb); |
| 2097 |
} |
| 2098 |
|
| 2099 |
static int radeon_enc_fence_wait(struct pipe_video_codec *encoder, |
| 2100 |
struct pipe_fence_handle *fence, |
| 2101 |
uint64_t timeout) |
| 2102 |
{ |
| 2103 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2104 |
|
| 2105 |
return enc->ws->fence_wait(enc->ws, fence, timeout); |
| 2106 |
} |
| 2107 |
|
| 2108 |
static void radeon_enc_destroy_fence(struct pipe_video_codec *encoder, |
| 2109 |
struct pipe_fence_handle *fence) |
| 2110 |
{ |
| 2111 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2112 |
|
| 2113 |
enc->ws->fence_reference(enc->ws, &fence, NULL); |
| 2114 |
} |
| 2115 |
|
| 2116 |
static unsigned int radeon_enc_frame_context_buffer_size(struct radeon_encoder *enc) |
| 2117 |
{ |
| 2118 |
unsigned int size = 0; |
| 2119 |
bool is_h264 = u_reduce_video_profile(enc->base.profile) |
| 2120 |
== PIPE_VIDEO_FORMAT_MPEG4_AVC; |
| 2121 |
bool is_av1 = u_reduce_video_profile(enc->base.profile) |
| 2122 |
== PIPE_VIDEO_FORMAT_AV1; |
| 2123 |
bool has_b = enc->enc_pic.spec_misc.b_picture_enabled; |
| 2124 |
|
| 2125 |
size = RENCODE_MAX_METADATA_BUFFER_SIZE_PER_FRAME; |
| 2126 |
if (is_h264) { |
| 2127 |
if (has_b) { |
| 2128 |
enc->enc_pic.fcb_offset.h264.colloc_buffer_offset = size; |
| 2129 |
size += enc->enc_pic.total_coloc_bytes; |
| 2130 |
} else |
| 2131 |
enc->enc_pic.fcb_offset.h264.colloc_buffer_offset = |
| 2132 |
RENCODE_INVALID_COLOC_OFFSET; |
| 2133 |
} |
| 2134 |
|
| 2135 |
if (is_av1) { |
| 2136 |
enc->enc_pic.fcb_offset.av1.av1_cdf_frame_context_offset = size; |
| 2137 |
size += RENCODE_AV1_FRAME_CONTEXT_CDF_TABLE_SIZE; |
| 2138 |
enc->enc_pic.fcb_offset.av1.av1_cdef_algorithm_context_offset = size; |
| 2139 |
size += RENCODE_AV1_CDEF_ALGORITHM_FRAME_CONTEXT_SIZE; |
| 2140 |
} |
| 2141 |
|
| 2142 |
size = align(size, enc->alignment); |
| 2143 |
return size; |
| 2144 |
} |
| 2145 |
|
| 2146 |
void radeon_enc_create_dpb_aux_buffers(struct radeon_encoder *enc, struct radeon_enc_dpb_buffer *buf) |
| 2147 |
{ |
| 2148 |
if (buf->fcb) |
| 2149 |
return; |
| 2150 |
|
| 2151 |
uint32_t fcb_size = radeon_enc_frame_context_buffer_size(enc); |
| 2152 |
|
| 2153 |
buf->fcb = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, fcb_size)); |
| 2154 |
if (!buf->fcb) { |
| 2155 |
RADEON_ENC_ERR("Can't create fcb buffer!\n"); |
| 2156 |
return; |
| 2157 |
} |
| 2158 |
|
| 2159 |
if (enc->enc_pic.quality_modes.pre_encode_mode) { |
| 2160 |
struct pipe_video_buffer templ = buf->templ; |
| 2161 |
templ.width /= enc->enc_pic.quality_modes.pre_encode_mode; |
| 2162 |
templ.height /= enc->enc_pic.quality_modes.pre_encode_mode; |
| 2163 |
buf->pre = enc->base.context->create_video_buffer(enc->base.context, &templ); |
| 2164 |
if (!buf->pre) { |
| 2165 |
RADEON_ENC_ERR("Can't create preenc buffer!\n"); |
| 2166 |
return; |
| 2167 |
} |
| 2168 |
buf->pre_luma = (struct si_texture *)((struct vl_video_buffer *)buf->pre)->resources[0]; |
| 2169 |
buf->pre_chroma = (struct si_texture *)((struct vl_video_buffer *)buf->pre)->resources[1]; |
| 2170 |
|
| 2171 |
buf->pre_fcb = si_resource(pipe_buffer_create(enc->screen, 0, PIPE_USAGE_DEFAULT, fcb_size)); |
| 2172 |
if (!buf->pre_fcb) { |
| 2173 |
RADEON_ENC_ERR("Can't create preenc fcb buffer!\n"); |
| 2174 |
return; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
static void radeon_enc_destroy_dpb_buffer(void *data) |
| 2180 |
{ |
| 2181 |
struct radeon_enc_dpb_buffer *dpb = data; |
| 2182 |
|
| 2183 |
if (dpb->pre) |
| 2184 |
dpb->pre->destroy(dpb->pre); |
| 2185 |
|
| 2186 |
si_resource_reference(&dpb->fcb, NULL); |
| 2187 |
si_resource_reference(&dpb->pre_fcb, NULL); |
| 2188 |
FREE(dpb); |
| 2189 |
} |
| 2190 |
|
| 2191 |
static struct pipe_video_buffer *radeon_enc_create_dpb_buffer(struct pipe_video_codec *encoder, |
| 2192 |
struct pipe_picture_desc *picture, |
| 2193 |
const struct pipe_video_buffer *templat) |
| 2194 |
{ |
| 2195 |
struct radeon_encoder *enc = (struct radeon_encoder *)encoder; |
| 2196 |
uint32_t rec_alignment = |
| 2197 |
get_rec_alignment(u_reduce_video_profile(enc->base.profile)); |
| 2198 |
|
| 2199 |
struct pipe_video_buffer templ = *templat; |
| 2200 |
templ.bind |= PIPE_BIND_VIDEO_ENCODE_DPB; |
| 2201 |
templ.width = align(templ.width, rec_alignment); |
| 2202 |
templ.height = align(templ.height, rec_alignment); |
| 2203 |
|
| 2204 |
struct pipe_video_buffer *buf = enc->base.context->create_video_buffer(enc->base.context, &templ); |
| 2205 |
if (!buf) { |
| 2206 |
RADEON_ENC_ERR("Can't create dpb buffer!\n"); |
| 2207 |
return NULL; |
| 2208 |
} |
| 2209 |
|
| 2210 |
struct radeon_enc_dpb_buffer *dpb = CALLOC_STRUCT(radeon_enc_dpb_buffer); |
| 2211 |
dpb->templ = templ; |
| 2212 |
dpb->luma = (struct si_texture *)((struct vl_video_buffer *)buf)->resources[0]; |
| 2213 |
dpb->chroma = (struct si_texture *)((struct vl_video_buffer *)buf)->resources[1]; |
| 2214 |
|
| 2215 |
vl_video_buffer_set_associated_data(buf, &enc->base, dpb, &radeon_enc_destroy_dpb_buffer); |
| 2216 |
|
| 2217 |
return buf; |
| 2218 |
} |
| 2219 |
|
| 2220 |
struct pipe_video_codec *radeon_create_encoder(struct pipe_context *context, |
| 2221 |
const struct pipe_video_codec *templ, |
| 2222 |
struct radeon_winsys *ws, |
| 2223 |
radeon_enc_get_buffer get_buffer) |
| 2224 |
{ |
| 2225 |
struct si_screen *sscreen = (struct si_screen *)context->screen; |
| 2226 |
struct si_context *sctx = (struct si_context *)context; |
| 2227 |
struct radeon_encoder *enc; |
| 2228 |
|
| 2229 |
enc = CALLOC_STRUCT(radeon_encoder); |
| 2230 |
|
| 2231 |
if (!enc) |
| 2232 |
return NULL; |
| 2233 |
|
| 2234 |
if (sctx->vcn_has_ctx) { |
| 2235 |
enc->ectx = context->screen->context_create(context->screen, NULL, PIPE_CONTEXT_COMPUTE_ONLY); |
| 2236 |
if (!enc->ectx) |
| 2237 |
sctx->vcn_has_ctx = false; |
| 2238 |
} |
| 2239 |
|
| 2240 |
enc->alignment = 256; |
| 2241 |
enc->base = *templ; |
| 2242 |
enc->base.context = (sctx->vcn_has_ctx)? enc->ectx : context; |
| 2243 |
enc->base.destroy = radeon_enc_destroy; |
| 2244 |
enc->base.begin_frame = radeon_enc_begin_frame; |
| 2245 |
enc->base.encode_bitstream = radeon_enc_encode_bitstream; |
| 2246 |
enc->base.process_frame = radeon_enc_process_frame; |
| 2247 |
enc->base.end_frame = radeon_enc_end_frame; |
| 2248 |
enc->base.flush = radeon_enc_flush; |
| 2249 |
enc->base.get_feedback = radeon_enc_get_feedback; |
| 2250 |
enc->base.fence_wait = radeon_enc_fence_wait; |
| 2251 |
enc->base.destroy_fence = radeon_enc_destroy_fence; |
| 2252 |
enc->get_buffer = get_buffer; |
| 2253 |
enc->screen = context->screen; |
| 2254 |
enc->ws = ws; |
| 2255 |
|
| 2256 |
if (!ws->cs_create(&enc->cs, |
| 2257 |
(sctx->vcn_has_ctx) ? ((struct si_context *)enc->ectx)->ctx : sctx->ctx, |
| 2258 |
AMD_IP_VCN_ENC, NULL, NULL)) { |
| 2259 |
RADEON_ENC_ERR("Can't get command submission context.\n"); |
| 2260 |
goto error; |
| 2261 |
} |
| 2262 |
|
| 2263 |
enc->enc_pic.use_rc_per_pic_ex = false; |
| 2264 |
|
| 2265 |
ac_vcn_enc_init_cmds(&enc->cmd, sscreen->info.vcn_ip_version); |
| 2266 |
|
| 2267 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0 && |
| 2268 |
!(sscreen->multimedia_debug_flags & DBG(NO_ENCODE_TIER2))) |
| 2269 |
enc->dpb_type = DPB_TIER_2; |
| 2270 |
|
| 2271 |
if (enc->dpb_type == DPB_TIER_2) |
| 2272 |
enc->base.create_dpb_buffer = radeon_enc_create_dpb_buffer; |
| 2273 |
|
| 2274 |
if (sscreen->info.vcn_ip_version >= VCN_5_0_0) { |
| 2275 |
radeon_enc_5_0_init(enc); |
| 2276 |
if (sscreen->info.vcn_ip_version == VCN_5_0_0) { |
| 2277 |
|
| 2278 |
enc->enc_pic.av1_tile_splitting_legacy_flag = true; |
| 2279 |
} |
| 2280 |
if (sscreen->info.vcn_enc_minor_version >= 8) |
| 2281 |
enc->enc_pic.has_dependent_slice_instructions = true; |
| 2282 |
if (sscreen->info.vcn_enc_minor_version > 8 || |
| 2283 |
(sscreen->info.vcn_enc_minor_version == 8 && |
| 2284 |
sscreen->info.vcn_fw_revision >= 6)) |
| 2285 |
enc->enc_pic.av1_unidir_rc_available = true; |
| 2286 |
} |
| 2287 |
else if (sscreen->info.vcn_ip_version >= VCN_4_0_0) { |
| 2288 |
if (sscreen->info.vcn_enc_minor_version >= 1) |
| 2289 |
enc->enc_pic.use_rc_per_pic_ex = true; |
| 2290 |
if (sscreen->info.vcn_enc_minor_version >= 23) |
| 2291 |
enc->enc_pic.has_dependent_slice_instructions = true; |
| 2292 |
radeon_enc_4_0_init(enc); |
| 2293 |
} |
| 2294 |
else if (sscreen->info.vcn_ip_version >= VCN_3_0_0) { |
| 2295 |
if (sscreen->info.vcn_enc_minor_version >= 29) |
| 2296 |
enc->enc_pic.use_rc_per_pic_ex = true; |
| 2297 |
radeon_enc_3_0_init(enc); |
| 2298 |
} |
| 2299 |
else if (sscreen->info.vcn_ip_version >= VCN_2_0_0) { |
| 2300 |
if (sscreen->info.vcn_enc_minor_version >= 18) |
| 2301 |
enc->enc_pic.use_rc_per_pic_ex = true; |
| 2302 |
radeon_enc_2_0_init(enc); |
| 2303 |
} |
| 2304 |
else { |
| 2305 |
if (sscreen->info.vcn_enc_minor_version >= 15) |
| 2306 |
enc->enc_pic.use_rc_per_pic_ex = true; |
| 2307 |
radeon_enc_1_2_init(enc); |
| 2308 |
} |
| 2309 |
|
| 2310 |
enc->first_frame = true; |
| 2311 |
|
| 2312 |
return &enc->base; |
| 2313 |
|
| 2314 |
error: |
| 2315 |
enc->ws->cs_destroy(&enc->cs); |
| 2316 |
FREE(enc); |
| 2317 |
return NULL; |
| 2318 |
} |
| 2319 |
|
| 2320 |
void radeon_enc_add_buffer(struct radeon_encoder *enc, struct pb_buffer_lean *buf, |
| 2321 |
unsigned usage, enum radeon_bo_domain domain, signed offset) |
| 2322 |
{ |
| 2323 |
enc->ws->cs_add_buffer(&enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain); |
| 2324 |
uint64_t addr; |
| 2325 |
addr = enc->ws->buffer_get_virtual_address(buf); |
| 2326 |
addr = addr + offset; |
| 2327 |
RADEON_ENC_CS(addr >> 32); |
| 2328 |
RADEON_ENC_CS(addr); |
| 2329 |
} |
| 2330 |
|
| 2331 |
void radeon_enc_code_leb128(uint8_t *buf, uint32_t value, |
| 2332 |
uint32_t num_bytes) |
| 2333 |
{ |
| 2334 |
uint8_t leb128_byte = 0; |
| 2335 |
uint32_t i = 0; |
| 2336 |
|
| 2337 |
do { |
| 2338 |
leb128_byte = (value & 0x7f); |
| 2339 |
value >>= 7; |
| 2340 |
if (num_bytes > 1) |
| 2341 |
leb128_byte |= 0x80; |
| 2342 |
|
| 2343 |
*(buf + i) = leb128_byte; |
| 2344 |
num_bytes--; |
| 2345 |
i++; |
| 2346 |
} while((leb128_byte & 0x80)); |
| 2347 |
} |
| 2348 |
|
| 2349 |
unsigned int radeon_enc_av1_tile_log2(unsigned int blk_size, unsigned int max) |
| 2350 |
{ |
| 2351 |
unsigned int k; |
| 2352 |
|
| 2353 |
assert(blk_size); |
| 2354 |
for (k = 0; (blk_size << k) < max; k++) {} |
| 2355 |
|
| 2356 |
return k; |
| 2357 |
} |
| 2358 |
|
| 2359 |
unsigned int radeon_enc_h2645_picture_type(enum pipe_h2645_enc_picture_type type) |
| 2360 |
{ |
| 2361 |
switch (type) { |
| 2362 |
case PIPE_H2645_ENC_PICTURE_TYPE_I: |
| 2363 |
case PIPE_H2645_ENC_PICTURE_TYPE_IDR: |
| 2364 |
return RENCODE_PICTURE_TYPE_I; |
| 2365 |
case PIPE_H2645_ENC_PICTURE_TYPE_P: |
| 2366 |
return RENCODE_PICTURE_TYPE_P; |
| 2367 |
case PIPE_H2645_ENC_PICTURE_TYPE_SKIP: |
| 2368 |
return RENCODE_PICTURE_TYPE_P_SKIP; |
| 2369 |
case PIPE_H2645_ENC_PICTURE_TYPE_B: |
| 2370 |
return RENCODE_PICTURE_TYPE_B; |
| 2371 |
default: |
| 2372 |
assert(false); |
| 2373 |
return 0; |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
unsigned int radeon_enc_av1_picture_type(enum pipe_av1_enc_frame_type type) |
| 2378 |
{ |
| 2379 |
switch (type) { |
| 2380 |
case PIPE_AV1_ENC_FRAME_TYPE_KEY: |
| 2381 |
case PIPE_AV1_ENC_FRAME_TYPE_INTRA_ONLY: |
| 2382 |
return RENCODE_PICTURE_TYPE_I; |
| 2383 |
case PIPE_AV1_ENC_FRAME_TYPE_INTER: |
| 2384 |
case PIPE_AV1_ENC_FRAME_TYPE_SWITCH: |
| 2385 |
return RENCODE_PICTURE_TYPE_P; |
| 2386 |
default: |
| 2387 |
assert(false); |
| 2388 |
return 0; |
| 2389 |
} |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
void radeon_enc_dummy(struct radeon_encoder *enc) {} |
| 2394 |
|
| 2395 |
|
| 2396 |
static void radeon_enc_av1_bs_copy_end(struct radeon_encoder *enc, uint32_t bits) |
| 2397 |
{ |
| 2398 |
assert(bits > 0); |
| 2399 |
|
| 2400 |
*enc->enc_pic.copy_start = DIV_ROUND_UP(bits, 32) * 4 + 12; |
| 2401 |
*(enc->enc_pic.copy_start + 2) = bits; |
| 2402 |
} |
| 2403 |
|
| 2404 |
|
| 2405 |
void radeon_enc_av1_bs_instruction_type(struct radeon_encoder *enc, |
| 2406 |
struct radeon_bitstream *bs, |
| 2407 |
uint32_t inst, |
| 2408 |
uint32_t obu_type) |
| 2409 |
{ |
| 2410 |
radeon_bs_flush_headers(bs); |
| 2411 |
|
| 2412 |
if (bs->bits_output) |
| 2413 |
radeon_enc_av1_bs_copy_end(enc, bs->bits_output); |
| 2414 |
|
| 2415 |
enc->enc_pic.copy_start = &enc->cs.current.buf[enc->cs.current.cdw++]; |
| 2416 |
RADEON_ENC_CS(inst); |
| 2417 |
|
| 2418 |
if (inst != RENCODE_HEADER_INSTRUCTION_COPY) { |
| 2419 |
*enc->enc_pic.copy_start = 8; |
| 2420 |
if (inst == RENCODE_AV1_BITSTREAM_INSTRUCTION_OBU_START) { |
| 2421 |
*enc->enc_pic.copy_start += 4; |
| 2422 |
RADEON_ENC_CS(obu_type); |
| 2423 |
} |
| 2424 |
} else |
| 2425 |
RADEON_ENC_CS(0); |
| 2426 |
|
| 2427 |
radeon_bs_reset(bs, NULL, &enc->cs); |
| 2428 |
} |
| 2429 |
|
| 2430 |
uint32_t radeon_enc_value_bits(uint32_t value) |
| 2431 |
{ |
| 2432 |
uint32_t i = 1; |
| 2433 |
|
| 2434 |
while (value > 1) { |
| 2435 |
i++; |
| 2436 |
value >>= 1; |
| 2437 |
} |
| 2438 |
|
| 2439 |
return i; |
| 2440 |
} |