Path: blob/21.2-virgl/src/gallium/drivers/radeon/radeon_uvd_enc_1_1.c
4570 views
/**************************************************************************1*2* Copyright 2018 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "pipe/p_video_codec.h"28#include "radeon_uvd_enc.h"29#include "radeon_video.h"30#include "radeonsi/si_pipe.h"31#include "util/u_memory.h"32#include "util/u_video.h"33#include "vl/vl_video_buffer.h"3435#include <stdio.h>3637#define RADEON_ENC_CS(value) (enc->cs.current.buf[enc->cs.current.cdw++] = (value))38#define RADEON_ENC_BEGIN(cmd) \39{ \40uint32_t *begin = &enc->cs.current.buf[enc->cs.current.cdw++]; \41RADEON_ENC_CS(cmd)42#define RADEON_ENC_READ(buf, domain, off) \43radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_READ, (domain), (off))44#define RADEON_ENC_WRITE(buf, domain, off) \45radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_WRITE, (domain), (off))46#define RADEON_ENC_READWRITE(buf, domain, off) \47radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_READWRITE, (domain), (off))48#define RADEON_ENC_END() \49*begin = (&enc->cs.current.buf[enc->cs.current.cdw] - begin) * 4; \50enc->total_task_size += *begin; \51}5253static const unsigned index_to_shifts[4] = {24, 16, 8, 0};5455static void radeon_uvd_enc_add_buffer(struct radeon_uvd_encoder *enc, struct pb_buffer *buf,56enum radeon_bo_usage usage, enum radeon_bo_domain domain,57signed offset)58{59enc->ws->cs_add_buffer(&enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain, 0);60uint64_t addr;61addr = enc->ws->buffer_get_virtual_address(buf);62addr = addr + offset;63RADEON_ENC_CS(addr >> 32);64RADEON_ENC_CS(addr);65}6667static void radeon_uvd_enc_set_emulation_prevention(struct radeon_uvd_encoder *enc, bool set)68{69if (set != enc->emulation_prevention) {70enc->emulation_prevention = set;71enc->num_zeros = 0;72}73}7475static void radeon_uvd_enc_output_one_byte(struct radeon_uvd_encoder *enc, unsigned char byte)76{77if (enc->byte_index == 0)78enc->cs.current.buf[enc->cs.current.cdw] = 0;79enc->cs.current.buf[enc->cs.current.cdw] |=80((unsigned int)(byte) << index_to_shifts[enc->byte_index]);81enc->byte_index++;8283if (enc->byte_index >= 4) {84enc->byte_index = 0;85enc->cs.current.cdw++;86}87}8889static void radeon_uvd_enc_emulation_prevention(struct radeon_uvd_encoder *enc, unsigned char byte)90{91if (enc->emulation_prevention) {92if ((enc->num_zeros >= 2) &&93((byte == 0x00) || (byte == 0x01) || (byte == 0x02) || (byte == 0x03))) {94radeon_uvd_enc_output_one_byte(enc, 0x03);95enc->bits_output += 8;96enc->num_zeros = 0;97}98enc->num_zeros = (byte == 0 ? (enc->num_zeros + 1) : 0);99}100}101102static void radeon_uvd_enc_code_fixed_bits(struct radeon_uvd_encoder *enc, unsigned int value,103unsigned int num_bits)104{105unsigned int bits_to_pack = 0;106107while (num_bits > 0) {108unsigned int value_to_pack = value & (0xffffffff >> (32 - num_bits));109bits_to_pack =110num_bits > (32 - enc->bits_in_shifter) ? (32 - enc->bits_in_shifter) : num_bits;111112if (bits_to_pack < num_bits)113value_to_pack = value_to_pack >> (num_bits - bits_to_pack);114115enc->shifter |= value_to_pack << (32 - enc->bits_in_shifter - bits_to_pack);116num_bits -= bits_to_pack;117enc->bits_in_shifter += bits_to_pack;118119while (enc->bits_in_shifter >= 8) {120unsigned char output_byte = (unsigned char)(enc->shifter >> 24);121enc->shifter <<= 8;122radeon_uvd_enc_emulation_prevention(enc, output_byte);123radeon_uvd_enc_output_one_byte(enc, output_byte);124enc->bits_in_shifter -= 8;125enc->bits_output += 8;126}127}128}129130static void radeon_uvd_enc_reset(struct radeon_uvd_encoder *enc)131{132enc->emulation_prevention = false;133enc->shifter = 0;134enc->bits_in_shifter = 0;135enc->bits_output = 0;136enc->num_zeros = 0;137enc->byte_index = 0;138}139140static void radeon_uvd_enc_byte_align(struct radeon_uvd_encoder *enc)141{142unsigned int num_padding_zeros = (32 - enc->bits_in_shifter) % 8;143144if (num_padding_zeros > 0)145radeon_uvd_enc_code_fixed_bits(enc, 0, num_padding_zeros);146}147148static void radeon_uvd_enc_flush_headers(struct radeon_uvd_encoder *enc)149{150if (enc->bits_in_shifter != 0) {151unsigned char output_byte = (unsigned char)(enc->shifter >> 24);152radeon_uvd_enc_emulation_prevention(enc, output_byte);153radeon_uvd_enc_output_one_byte(enc, output_byte);154enc->bits_output += enc->bits_in_shifter;155enc->shifter = 0;156enc->bits_in_shifter = 0;157enc->num_zeros = 0;158}159160if (enc->byte_index > 0) {161enc->cs.current.cdw++;162enc->byte_index = 0;163}164}165166static void radeon_uvd_enc_code_ue(struct radeon_uvd_encoder *enc, unsigned int value)167{168int x = -1;169unsigned int ue_code = value + 1;170value += 1;171172while (value) {173value = (value >> 1);174x += 1;175}176177unsigned int ue_length = (x << 1) + 1;178radeon_uvd_enc_code_fixed_bits(enc, ue_code, ue_length);179}180181static void radeon_uvd_enc_code_se(struct radeon_uvd_encoder *enc, int value)182{183unsigned int v = 0;184185if (value != 0)186v = (value < 0 ? ((unsigned int)(0 - value) << 1) : (((unsigned int)(value) << 1) - 1));187188radeon_uvd_enc_code_ue(enc, v);189}190191static void radeon_uvd_enc_session_info(struct radeon_uvd_encoder *enc)192{193unsigned int interface_version =194((RENC_UVD_FW_INTERFACE_MAJOR_VERSION << RENC_UVD_IF_MAJOR_VERSION_SHIFT) |195(RENC_UVD_FW_INTERFACE_MINOR_VERSION << RENC_UVD_IF_MINOR_VERSION_SHIFT));196RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SESSION_INFO);197RADEON_ENC_CS(0x00000000); // reserved198RADEON_ENC_CS(interface_version);199RADEON_ENC_READWRITE(enc->si->res->buf, enc->si->res->domains, 0x0);200RADEON_ENC_END();201}202203static void radeon_uvd_enc_task_info(struct radeon_uvd_encoder *enc, bool need_feedback)204{205enc->enc_pic.task_info.task_id++;206207if (need_feedback)208enc->enc_pic.task_info.allowed_max_num_feedbacks = 1;209else210enc->enc_pic.task_info.allowed_max_num_feedbacks = 0;211212RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_TASK_INFO);213enc->p_task_size = &enc->cs.current.buf[enc->cs.current.cdw++];214RADEON_ENC_CS(enc->enc_pic.task_info.task_id);215RADEON_ENC_CS(enc->enc_pic.task_info.allowed_max_num_feedbacks);216RADEON_ENC_END();217}218219static void radeon_uvd_enc_session_init_hevc(struct radeon_uvd_encoder *enc)220{221enc->enc_pic.session_init.aligned_picture_width = align(enc->base.width, 64);222enc->enc_pic.session_init.aligned_picture_height = align(enc->base.height, 16);223enc->enc_pic.session_init.padding_width =224enc->enc_pic.session_init.aligned_picture_width - enc->base.width;225enc->enc_pic.session_init.padding_height =226enc->enc_pic.session_init.aligned_picture_height - enc->base.height;227enc->enc_pic.session_init.pre_encode_mode = RENC_UVD_PREENCODE_MODE_NONE;228enc->enc_pic.session_init.pre_encode_chroma_enabled = false;229230RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SESSION_INIT);231RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_width);232RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_height);233RADEON_ENC_CS(enc->enc_pic.session_init.padding_width);234RADEON_ENC_CS(enc->enc_pic.session_init.padding_height);235RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_mode);236RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_chroma_enabled);237RADEON_ENC_END();238}239240static void radeon_uvd_enc_layer_control(struct radeon_uvd_encoder *enc)241{242enc->enc_pic.layer_ctrl.max_num_temporal_layers = 1;243enc->enc_pic.layer_ctrl.num_temporal_layers = 1;244245RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_LAYER_CONTROL);246RADEON_ENC_CS(enc->enc_pic.layer_ctrl.max_num_temporal_layers);247RADEON_ENC_CS(enc->enc_pic.layer_ctrl.num_temporal_layers);248RADEON_ENC_END();249}250251static void radeon_uvd_enc_layer_select(struct radeon_uvd_encoder *enc)252{253enc->enc_pic.layer_sel.temporal_layer_index = 0;254255RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_LAYER_SELECT);256RADEON_ENC_CS(enc->enc_pic.layer_sel.temporal_layer_index);257RADEON_ENC_END();258}259260static void radeon_uvd_enc_slice_control_hevc(struct radeon_uvd_encoder *enc)261{262enc->enc_pic.hevc_slice_ctrl.slice_control_mode = RENC_UVD_SLICE_CONTROL_MODE_FIXED_CTBS;263enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice =264align(enc->base.width, 64) / 64 * align(enc->base.height, 64) / 64;265enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment =266enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice;267268RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SLICE_CONTROL);269RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.slice_control_mode);270RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice);271RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment);272RADEON_ENC_END();273}274275static void radeon_uvd_enc_spec_misc_hevc(struct radeon_uvd_encoder *enc,276struct pipe_picture_desc *picture)277{278struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;279enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 =280pic->seq.log2_min_luma_coding_block_size_minus3;281enc->enc_pic.hevc_spec_misc.amp_disabled = !pic->seq.amp_enabled_flag;282enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled =283pic->seq.strong_intra_smoothing_enabled_flag;284enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag = pic->pic.constrained_intra_pred_flag;285enc->enc_pic.hevc_spec_misc.cabac_init_flag = pic->slice.cabac_init_flag;286enc->enc_pic.hevc_spec_misc.half_pel_enabled = 1;287enc->enc_pic.hevc_spec_misc.quarter_pel_enabled = 1;288289RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SPEC_MISC);290RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);291RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.amp_disabled);292RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled);293RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag);294RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.cabac_init_flag);295RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.half_pel_enabled);296RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.quarter_pel_enabled);297RADEON_ENC_END();298}299300static void radeon_uvd_enc_rc_session_init(struct radeon_uvd_encoder *enc,301struct pipe_picture_desc *picture)302{303struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;304enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc.vbv_buf_lv;305switch (pic->rc.rate_ctrl_method) {306case PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE:307enc->enc_pic.rc_session_init.rate_control_method = RENC_UVD_RATE_CONTROL_METHOD_NONE;308break;309case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:310case PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT:311enc->enc_pic.rc_session_init.rate_control_method = RENC_UVD_RATE_CONTROL_METHOD_CBR;312break;313case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:314case PIPE_H2645_ENC_RATE_CONTROL_METHOD_VARIABLE:315enc->enc_pic.rc_session_init.rate_control_method =316RENC_UVD_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;317break;318default:319enc->enc_pic.rc_session_init.rate_control_method = RENC_UVD_RATE_CONTROL_METHOD_NONE;320}321322RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_SESSION_INIT);323RADEON_ENC_CS(enc->enc_pic.rc_session_init.rate_control_method);324RADEON_ENC_CS(enc->enc_pic.rc_session_init.vbv_buffer_level);325RADEON_ENC_END();326}327328static void radeon_uvd_enc_rc_layer_init(struct radeon_uvd_encoder *enc,329struct pipe_picture_desc *picture)330{331struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;332enc->enc_pic.rc_layer_init.target_bit_rate = pic->rc.target_bitrate;333enc->enc_pic.rc_layer_init.peak_bit_rate = pic->rc.peak_bitrate;334enc->enc_pic.rc_layer_init.frame_rate_num = pic->rc.frame_rate_num;335enc->enc_pic.rc_layer_init.frame_rate_den = pic->rc.frame_rate_den;336enc->enc_pic.rc_layer_init.vbv_buffer_size = pic->rc.vbv_buffer_size;337enc->enc_pic.rc_layer_init.avg_target_bits_per_picture = pic->rc.target_bits_picture;338enc->enc_pic.rc_layer_init.peak_bits_per_picture_integer = pic->rc.peak_bits_picture_integer;339enc->enc_pic.rc_layer_init.peak_bits_per_picture_fractional = pic->rc.peak_bits_picture_fraction;340341RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_LAYER_INIT);342RADEON_ENC_CS(enc->enc_pic.rc_layer_init.target_bit_rate);343RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bit_rate);344RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_num);345RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_den);346RADEON_ENC_CS(enc->enc_pic.rc_layer_init.vbv_buffer_size);347RADEON_ENC_CS(enc->enc_pic.rc_layer_init.avg_target_bits_per_picture);348RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_integer);349RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_fractional);350RADEON_ENC_END();351}352353static void radeon_uvd_enc_deblocking_filter_hevc(struct radeon_uvd_encoder *enc,354struct pipe_picture_desc *picture)355{356struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;357enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled =358pic->slice.slice_loop_filter_across_slices_enabled_flag;359enc->enc_pic.hevc_deblock.deblocking_filter_disabled =360pic->slice.slice_deblocking_filter_disabled_flag;361enc->enc_pic.hevc_deblock.beta_offset_div2 = pic->slice.slice_beta_offset_div2;362enc->enc_pic.hevc_deblock.tc_offset_div2 = pic->slice.slice_tc_offset_div2;363enc->enc_pic.hevc_deblock.cb_qp_offset = pic->slice.slice_cb_qp_offset;364enc->enc_pic.hevc_deblock.cr_qp_offset = pic->slice.slice_cr_qp_offset;365366RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_DEBLOCKING_FILTER);367RADEON_ENC_CS(enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled);368RADEON_ENC_CS(enc->enc_pic.hevc_deblock.deblocking_filter_disabled);369RADEON_ENC_CS(enc->enc_pic.hevc_deblock.beta_offset_div2);370RADEON_ENC_CS(enc->enc_pic.hevc_deblock.tc_offset_div2);371RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cb_qp_offset);372RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cr_qp_offset);373RADEON_ENC_END();374}375376static void radeon_uvd_enc_quality_params(struct radeon_uvd_encoder *enc)377{378enc->enc_pic.quality_params.vbaq_mode = 0;379enc->enc_pic.quality_params.scene_change_sensitivity = 0;380enc->enc_pic.quality_params.scene_change_min_idr_interval = 0;381382RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_QUALITY_PARAMS);383RADEON_ENC_CS(enc->enc_pic.quality_params.vbaq_mode);384RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_sensitivity);385RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_min_idr_interval);386RADEON_ENC_END();387}388389static void radeon_uvd_enc_nalu_sps_hevc(struct radeon_uvd_encoder *enc)390{391RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);392RADEON_ENC_CS(RENC_UVD_NALU_TYPE_SPS);393uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];394int i;395396radeon_uvd_enc_reset(enc);397radeon_uvd_enc_set_emulation_prevention(enc, false);398radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);399radeon_uvd_enc_code_fixed_bits(enc, 0x4201, 16);400radeon_uvd_enc_byte_align(enc);401radeon_uvd_enc_set_emulation_prevention(enc, true);402radeon_uvd_enc_code_fixed_bits(enc, 0x0, 4);403radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1, 3);404radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);405radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);406radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);407radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);408radeon_uvd_enc_code_fixed_bits(enc, 0x60000000, 32);409radeon_uvd_enc_code_fixed_bits(enc, 0xb0000000, 32);410radeon_uvd_enc_code_fixed_bits(enc, 0x0, 16);411radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);412413for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)414radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);415416if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {417for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)418radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);419}420421radeon_uvd_enc_code_ue(enc, 0x0);422radeon_uvd_enc_code_ue(enc, enc->enc_pic.chroma_format_idc);423radeon_uvd_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_width);424radeon_uvd_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_height);425426int conformance_window_flag = (enc->enc_pic.crop_top > 0) || (enc->enc_pic.crop_bottom > 0) ||427(enc->enc_pic.crop_left > 0) || (enc->enc_pic.crop_right > 0)428? 0x1429: 0x0;430radeon_uvd_enc_code_fixed_bits(enc, conformance_window_flag, 1);431432if (conformance_window_flag == 1) {433radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_left);434radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_right);435radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_top);436radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_bottom);437}438439radeon_uvd_enc_code_ue(enc, enc->enc_pic.bit_depth_luma_minus8);440radeon_uvd_enc_code_ue(enc, enc->enc_pic.bit_depth_chroma_minus8);441radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_max_poc - 4);442radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);443radeon_uvd_enc_code_ue(enc, 1);444radeon_uvd_enc_code_ue(enc, 0x0);445radeon_uvd_enc_code_ue(enc, 0x0);446radeon_uvd_enc_code_ue(enc, enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);447/* Only support CTBSize 64 */448radeon_uvd_enc_code_ue(449enc, 6 - (enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 + 3));450radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_min_transform_block_size_minus2);451radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_diff_max_min_transform_block_size);452radeon_uvd_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_inter);453radeon_uvd_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_intra);454455radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);456radeon_uvd_enc_code_fixed_bits(enc, !enc->enc_pic.hevc_spec_misc.amp_disabled, 1);457radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.sample_adaptive_offset_enabled_flag, 1);458radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.pcm_enabled_flag, 1);459460radeon_uvd_enc_code_ue(enc, 1);461radeon_uvd_enc_code_ue(enc, 1);462radeon_uvd_enc_code_ue(enc, 0);463radeon_uvd_enc_code_ue(enc, 0);464radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);465466radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);467468radeon_uvd_enc_code_fixed_bits(enc, 0, 1);469radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled,4701);471472radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);473474radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);475476radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);477478radeon_uvd_enc_byte_align(enc);479radeon_uvd_enc_flush_headers(enc);480*size_in_bytes = (enc->bits_output + 7) / 8;481RADEON_ENC_END();482}483484static void radeon_uvd_enc_nalu_pps_hevc(struct radeon_uvd_encoder *enc)485{486RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);487RADEON_ENC_CS(RENC_UVD_NALU_TYPE_PPS);488uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];489radeon_uvd_enc_reset(enc);490radeon_uvd_enc_set_emulation_prevention(enc, false);491radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);492radeon_uvd_enc_code_fixed_bits(enc, 0x4401, 16);493radeon_uvd_enc_byte_align(enc);494radeon_uvd_enc_set_emulation_prevention(enc, true);495radeon_uvd_enc_code_ue(enc, 0x0);496radeon_uvd_enc_code_ue(enc, 0x0);497radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);498radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1); /* output_flag_resent_flag */499radeon_uvd_enc_code_fixed_bits(enc, 0x0, 3); /* num_extra_slice_header_bits */500radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);501radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);502radeon_uvd_enc_code_ue(enc, 0x0);503radeon_uvd_enc_code_ue(enc, 0x0);504radeon_uvd_enc_code_se(enc, 0x0);505radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag, 1);506radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);507if (enc->enc_pic.rc_session_init.rate_control_method == RENC_UVD_RATE_CONTROL_METHOD_NONE)508radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);509else {510radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);511radeon_uvd_enc_code_ue(enc, 0x0);512}513radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.cb_qp_offset);514radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.cr_qp_offset);515radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);516radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);517radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);518radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);519radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);520radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled,5211);522radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);523radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);524radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.deblocking_filter_disabled, 1);525526if (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled) {527radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.beta_offset_div2);528radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.tc_offset_div2);529}530531radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);532radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);533radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_parallel_merge_level_minus2);534radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);535536radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);537538radeon_uvd_enc_byte_align(enc);539radeon_uvd_enc_flush_headers(enc);540*size_in_bytes = (enc->bits_output + 7) / 8;541RADEON_ENC_END();542}543544static void radeon_uvd_enc_nalu_vps_hevc(struct radeon_uvd_encoder *enc)545{546RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);547RADEON_ENC_CS(RENC_UVD_NALU_TYPE_VPS);548uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];549int i;550551radeon_uvd_enc_reset(enc);552radeon_uvd_enc_set_emulation_prevention(enc, false);553radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);554radeon_uvd_enc_code_fixed_bits(enc, 0x4001, 16);555radeon_uvd_enc_byte_align(enc);556radeon_uvd_enc_set_emulation_prevention(enc, true);557558radeon_uvd_enc_code_fixed_bits(enc, 0x0, 4);559radeon_uvd_enc_code_fixed_bits(enc, 0x3, 2);560radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);561radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1, 3);562radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);563radeon_uvd_enc_code_fixed_bits(enc, 0xffff, 16);564radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);565radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);566radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);567radeon_uvd_enc_code_fixed_bits(enc, 0x60000000, 32);568radeon_uvd_enc_code_fixed_bits(enc, 0xb0000000, 32);569radeon_uvd_enc_code_fixed_bits(enc, 0x0, 16);570radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);571572for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)573radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);574575if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {576for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)577radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);578}579580radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);581radeon_uvd_enc_code_ue(enc, 0x1);582radeon_uvd_enc_code_ue(enc, 0x0);583radeon_uvd_enc_code_ue(enc, 0x0);584585radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);586radeon_uvd_enc_code_ue(enc, 0x0);587radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);588radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);589590radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);591592radeon_uvd_enc_byte_align(enc);593radeon_uvd_enc_flush_headers(enc);594*size_in_bytes = (enc->bits_output + 7) / 8;595RADEON_ENC_END();596}597598static void radeon_uvd_enc_nalu_aud_hevc(struct radeon_uvd_encoder *enc)599{600RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);601RADEON_ENC_CS(RENC_UVD_NALU_TYPE_AUD);602uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];603radeon_uvd_enc_reset(enc);604radeon_uvd_enc_set_emulation_prevention(enc, false);605radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);606radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);607radeon_uvd_enc_code_fixed_bits(enc, 35, 6);608radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);609radeon_uvd_enc_code_fixed_bits(enc, 0x1, 3);610radeon_uvd_enc_byte_align(enc);611radeon_uvd_enc_set_emulation_prevention(enc, true);612switch (enc->enc_pic.picture_type) {613case PIPE_H2645_ENC_PICTURE_TYPE_I:614case PIPE_H2645_ENC_PICTURE_TYPE_IDR:615radeon_uvd_enc_code_fixed_bits(enc, 0x00, 3);616break;617case PIPE_H2645_ENC_PICTURE_TYPE_P:618radeon_uvd_enc_code_fixed_bits(enc, 0x01, 3);619break;620case PIPE_H2645_ENC_PICTURE_TYPE_B:621radeon_uvd_enc_code_fixed_bits(enc, 0x02, 3);622break;623default:624assert(0 && "Unsupported picture type!");625}626627radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);628629radeon_uvd_enc_byte_align(enc);630radeon_uvd_enc_flush_headers(enc);631*size_in_bytes = (enc->bits_output + 7) / 8;632RADEON_ENC_END();633}634635static void radeon_uvd_enc_slice_header_hevc(struct radeon_uvd_encoder *enc)636{637uint32_t instruction[RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};638uint32_t num_bits[RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};639unsigned int inst_index = 0;640unsigned int bit_index = 0;641unsigned int bits_copied = 0;642RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SLICE_HEADER);643radeon_uvd_enc_reset(enc);644radeon_uvd_enc_set_emulation_prevention(enc, false);645646radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);647radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.nal_unit_type, 6);648radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);649radeon_uvd_enc_code_fixed_bits(enc, 0x1, 3);650651radeon_uvd_enc_flush_headers(enc);652bit_index++;653instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;654num_bits[inst_index] = enc->bits_output - bits_copied;655bits_copied = enc->bits_output;656inst_index++;657658instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_FIRST_SLICE;659inst_index++;660661if ((enc->enc_pic.nal_unit_type >= 16) && (enc->enc_pic.nal_unit_type <= 23))662radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);663664radeon_uvd_enc_code_ue(enc, 0x0);665666radeon_uvd_enc_flush_headers(enc);667bit_index++;668instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;669num_bits[inst_index] = enc->bits_output - bits_copied;670bits_copied = enc->bits_output;671inst_index++;672673instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_SLICE_SEGMENT;674inst_index++;675676instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_DEPENDENT_SLICE_END;677inst_index++;678679switch (enc->enc_pic.picture_type) {680case PIPE_H2645_ENC_PICTURE_TYPE_I:681case PIPE_H2645_ENC_PICTURE_TYPE_IDR:682radeon_uvd_enc_code_ue(enc, 0x2);683break;684case PIPE_H2645_ENC_PICTURE_TYPE_P:685case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:686radeon_uvd_enc_code_ue(enc, 0x1);687break;688case PIPE_H2645_ENC_PICTURE_TYPE_B:689radeon_uvd_enc_code_ue(enc, 0x0);690break;691default:692radeon_uvd_enc_code_ue(enc, 0x1);693}694695if ((enc->enc_pic.nal_unit_type != 19) && (enc->enc_pic.nal_unit_type != 20)) {696radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.pic_order_cnt, enc->enc_pic.log2_max_poc);697if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P)698radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);699else {700radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);701radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);702radeon_uvd_enc_code_ue(enc, 0x0);703radeon_uvd_enc_code_ue(enc, 0x0);704}705}706707if (enc->enc_pic.sample_adaptive_offset_enabled_flag)708radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1); /* slice_sao_luma_flag */709710if ((enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P) ||711(enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B)) {712radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);713radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.cabac_init_flag, 1);714radeon_uvd_enc_code_ue(enc, 5 - enc->enc_pic.max_num_merge_cand);715}716717radeon_uvd_enc_flush_headers(enc);718bit_index++;719instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;720num_bits[inst_index] = enc->bits_output - bits_copied;721bits_copied = enc->bits_output;722inst_index++;723724instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_SLICE_QP_DELTA;725inst_index++;726727if ((enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled) &&728(!enc->enc_pic.hevc_deblock.deblocking_filter_disabled)) {729radeon_uvd_enc_code_fixed_bits(730enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled, 1);731732radeon_uvd_enc_flush_headers(enc);733bit_index++;734instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;735num_bits[inst_index] = enc->bits_output - bits_copied;736bits_copied = enc->bits_output;737inst_index++;738}739740instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_END;741742for (int i = bit_index; i < RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS; i++)743RADEON_ENC_CS(0x00000000);744745for (int j = 0; j < RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS; j++) {746RADEON_ENC_CS(instruction[j]);747RADEON_ENC_CS(num_bits[j]);748}749750RADEON_ENC_END();751}752753static void radeon_uvd_enc_ctx(struct radeon_uvd_encoder *enc)754{755struct si_screen *sscreen = (struct si_screen *)enc->screen;756757enc->enc_pic.ctx_buf.swizzle_mode = 0;758if (sscreen->info.chip_class < GFX9) {759enc->enc_pic.ctx_buf.rec_luma_pitch = (enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe);760enc->enc_pic.ctx_buf.rec_chroma_pitch =761(enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe);762} else {763enc->enc_pic.ctx_buf.rec_luma_pitch = enc->luma->u.gfx9.surf_pitch * enc->luma->bpe;764enc->enc_pic.ctx_buf.rec_chroma_pitch = enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe;765}766enc->enc_pic.ctx_buf.num_reconstructed_pictures = 2;767768RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_ENCODE_CONTEXT_BUFFER);769RADEON_ENC_READWRITE(enc->cpb.res->buf, enc->cpb.res->domains, 0);770RADEON_ENC_CS(0x00000000); // reserved771RADEON_ENC_CS(enc->enc_pic.ctx_buf.swizzle_mode);772RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);773RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);774RADEON_ENC_CS(enc->enc_pic.ctx_buf.num_reconstructed_pictures);775/* reconstructed_picture_1_luma_offset */776RADEON_ENC_CS(0x00000000);777/* reconstructed_picture_1_chroma_offset */778RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch * align(enc->base.height, 16));779/* reconstructed_picture_2_luma_offset */780RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch * align(enc->base.height, 16) * 3 / 2);781/* reconstructed_picture_2_chroma_offset */782RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch * align(enc->base.height, 16) * 5 / 2);783784for (int i = 0; i < 136; i++)785RADEON_ENC_CS(0x00000000);786787RADEON_ENC_END();788}789790static void radeon_uvd_enc_bitstream(struct radeon_uvd_encoder *enc)791{792enc->enc_pic.bit_buf.mode = RENC_UVD_SWIZZLE_MODE_LINEAR;793enc->enc_pic.bit_buf.video_bitstream_buffer_size = enc->bs_size;794enc->enc_pic.bit_buf.video_bitstream_data_offset = 0;795796RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_VIDEO_BITSTREAM_BUFFER);797RADEON_ENC_CS(enc->enc_pic.bit_buf.mode);798RADEON_ENC_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT, 0);799RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_buffer_size);800RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_data_offset);801RADEON_ENC_END();802}803804static void radeon_uvd_enc_feedback(struct radeon_uvd_encoder *enc)805{806enc->enc_pic.fb_buf.mode = RENC_UVD_FEEDBACK_BUFFER_MODE_LINEAR;807enc->enc_pic.fb_buf.feedback_buffer_size = 16;808enc->enc_pic.fb_buf.feedback_data_size = 40;809810RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_FEEDBACK_BUFFER);811RADEON_ENC_CS(enc->enc_pic.fb_buf.mode);812RADEON_ENC_WRITE(enc->fb->res->buf, enc->fb->res->domains, 0x0);813RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_buffer_size);814RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_data_size);815RADEON_ENC_END();816}817818static void radeon_uvd_enc_intra_refresh(struct radeon_uvd_encoder *enc)819{820enc->enc_pic.intra_ref.intra_refresh_mode = RENC_UVD_INTRA_REFRESH_MODE_NONE;821enc->enc_pic.intra_ref.offset = 0;822enc->enc_pic.intra_ref.region_size = 0;823824RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INTRA_REFRESH);825RADEON_ENC_CS(enc->enc_pic.intra_ref.intra_refresh_mode);826RADEON_ENC_CS(enc->enc_pic.intra_ref.offset);827RADEON_ENC_CS(enc->enc_pic.intra_ref.region_size);828RADEON_ENC_END();829}830831static void radeon_uvd_enc_rc_per_pic(struct radeon_uvd_encoder *enc,832struct pipe_picture_desc *picture)833{834struct pipe_h265_enc_picture_desc *pic = (struct pipe_h265_enc_picture_desc *)picture;835enc->enc_pic.rc_per_pic.qp = pic->rc.quant_i_frames;836enc->enc_pic.rc_per_pic.min_qp_app = 0;837enc->enc_pic.rc_per_pic.max_qp_app = 51;838enc->enc_pic.rc_per_pic.max_au_size = 0;839enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc.fill_data_enable;840enc->enc_pic.rc_per_pic.skip_frame_enable = false;841enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc.enforce_hrd;842843RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_PER_PICTURE);844RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp);845RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_app);846RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_app);847RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size);848RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enabled_filler_data);849RADEON_ENC_CS(enc->enc_pic.rc_per_pic.skip_frame_enable);850RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enforce_hrd);851RADEON_ENC_END();852}853854static void radeon_uvd_enc_encode_params_hevc(struct radeon_uvd_encoder *enc)855{856struct si_screen *sscreen = (struct si_screen *)enc->screen;857switch (enc->enc_pic.picture_type) {858case PIPE_H2645_ENC_PICTURE_TYPE_I:859case PIPE_H2645_ENC_PICTURE_TYPE_IDR:860enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_I;861break;862case PIPE_H2645_ENC_PICTURE_TYPE_P:863enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_P;864break;865case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:866enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_P_SKIP;867break;868case PIPE_H2645_ENC_PICTURE_TYPE_B:869enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_B;870break;871default:872enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_I;873}874875enc->enc_pic.enc_params.allowed_max_bitstream_size = enc->bs_size;876if (sscreen->info.chip_class < GFX9) {877enc->enc_pic.enc_params.input_pic_luma_pitch =878(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe);879enc->enc_pic.enc_params.input_pic_chroma_pitch =880(enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe);881} else {882enc->enc_pic.enc_params.input_pic_luma_pitch = enc->luma->u.gfx9.surf_pitch * enc->luma->bpe;883enc->enc_pic.enc_params.input_pic_chroma_pitch =884enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe;885}886enc->enc_pic.enc_params.input_pic_swizzle_mode = RENC_UVD_SWIZZLE_MODE_LINEAR;887888if (enc->enc_pic.enc_params.pic_type == RENC_UVD_PICTURE_TYPE_I)889enc->enc_pic.enc_params.reference_picture_index = 0xFFFFFFFF;890else891enc->enc_pic.enc_params.reference_picture_index = (enc->enc_pic.frame_num - 1) % 2;892893enc->enc_pic.enc_params.reconstructed_picture_index = enc->enc_pic.frame_num % 2;894895RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_ENCODE_PARAMS);896RADEON_ENC_CS(enc->enc_pic.enc_params.pic_type);897RADEON_ENC_CS(enc->enc_pic.enc_params.allowed_max_bitstream_size);898899if (sscreen->info.chip_class < GFX9) {900RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, (uint64_t)enc->luma->u.legacy.level[0].offset_256B * 256);901RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, (uint64_t)enc->chroma->u.legacy.level[0].offset_256B * 256);902} else {903RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->luma->u.gfx9.surf_offset);904RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->chroma->u.gfx9.surf_offset);905}906RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_luma_pitch);907RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_chroma_pitch);908RADEON_ENC_CS(0x00000000); // reserved909RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_swizzle_mode);910RADEON_ENC_CS(enc->enc_pic.enc_params.reference_picture_index);911RADEON_ENC_CS(enc->enc_pic.enc_params.reconstructed_picture_index);912RADEON_ENC_END();913}914915static void radeon_uvd_enc_op_init(struct radeon_uvd_encoder *enc)916{917RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INITIALIZE);918RADEON_ENC_END();919}920921static void radeon_uvd_enc_op_close(struct radeon_uvd_encoder *enc)922{923RADEON_ENC_BEGIN(RENC_UVD_IB_OP_CLOSE_SESSION);924RADEON_ENC_END();925}926927static void radeon_uvd_enc_op_enc(struct radeon_uvd_encoder *enc)928{929RADEON_ENC_BEGIN(RENC_UVD_IB_OP_ENCODE);930RADEON_ENC_END();931}932933static void radeon_uvd_enc_op_init_rc(struct radeon_uvd_encoder *enc)934{935RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INIT_RC);936RADEON_ENC_END();937}938939static void radeon_uvd_enc_op_init_rc_vbv(struct radeon_uvd_encoder *enc)940{941RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INIT_RC_VBV_BUFFER_LEVEL);942RADEON_ENC_END();943}944945static void radeon_uvd_enc_op_speed(struct radeon_uvd_encoder *enc)946{947RADEON_ENC_BEGIN(RENC_UVD_IB_OP_SET_SPEED_ENCODING_MODE);948RADEON_ENC_END();949}950951static void begin(struct radeon_uvd_encoder *enc, struct pipe_picture_desc *pic)952{953radeon_uvd_enc_session_info(enc);954enc->total_task_size = 0;955radeon_uvd_enc_task_info(enc, enc->need_feedback);956radeon_uvd_enc_op_init(enc);957958radeon_uvd_enc_session_init_hevc(enc);959radeon_uvd_enc_slice_control_hevc(enc);960radeon_uvd_enc_spec_misc_hevc(enc, pic);961radeon_uvd_enc_deblocking_filter_hevc(enc, pic);962963radeon_uvd_enc_layer_control(enc);964radeon_uvd_enc_rc_session_init(enc, pic);965radeon_uvd_enc_quality_params(enc);966radeon_uvd_enc_layer_select(enc);967radeon_uvd_enc_rc_layer_init(enc, pic);968radeon_uvd_enc_layer_select(enc);969radeon_uvd_enc_rc_per_pic(enc, pic);970radeon_uvd_enc_op_init_rc(enc);971radeon_uvd_enc_op_init_rc_vbv(enc);972*enc->p_task_size = (enc->total_task_size);973}974975static void encode(struct radeon_uvd_encoder *enc)976{977radeon_uvd_enc_session_info(enc);978enc->total_task_size = 0;979radeon_uvd_enc_task_info(enc, enc->need_feedback);980981radeon_uvd_enc_nalu_aud_hevc(enc);982983if (enc->enc_pic.is_iframe) {984radeon_uvd_enc_nalu_vps_hevc(enc);985radeon_uvd_enc_nalu_pps_hevc(enc);986radeon_uvd_enc_nalu_sps_hevc(enc);987}988radeon_uvd_enc_slice_header_hevc(enc);989radeon_uvd_enc_encode_params_hevc(enc);990991radeon_uvd_enc_ctx(enc);992radeon_uvd_enc_bitstream(enc);993radeon_uvd_enc_feedback(enc);994radeon_uvd_enc_intra_refresh(enc);995996radeon_uvd_enc_op_speed(enc);997radeon_uvd_enc_op_enc(enc);998*enc->p_task_size = (enc->total_task_size);999}10001001static void destroy(struct radeon_uvd_encoder *enc)1002{1003radeon_uvd_enc_session_info(enc);1004enc->total_task_size = 0;1005radeon_uvd_enc_task_info(enc, enc->need_feedback);1006radeon_uvd_enc_op_close(enc);1007*enc->p_task_size = (enc->total_task_size);1008}10091010void radeon_uvd_enc_1_1_init(struct radeon_uvd_encoder *enc)1011{1012enc->begin = begin;1013enc->encode = encode;1014enc->destroy = destroy;1015}101610171018