Path: blob/21.2-virgl/src/gallium/drivers/radeon/radeon_vcn_enc_1_2.c
4570 views
/**************************************************************************1*2* Copyright 2017 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_vcn_enc.h"29#include "radeon_video.h"30#include "si_pipe.h"31#include "util/u_video.h"3233#include <stdio.h>3435#define RENCODE_FW_INTERFACE_MAJOR_VERSION 136#define RENCODE_FW_INTERFACE_MINOR_VERSION 23738#define RENCODE_IB_PARAM_SESSION_INFO 0x0000000139#define RENCODE_IB_PARAM_TASK_INFO 0x0000000240#define RENCODE_IB_PARAM_SESSION_INIT 0x0000000341#define RENCODE_IB_PARAM_LAYER_CONTROL 0x0000000442#define RENCODE_IB_PARAM_LAYER_SELECT 0x0000000543#define RENCODE_IB_PARAM_RATE_CONTROL_SESSION_INIT 0x0000000644#define RENCODE_IB_PARAM_RATE_CONTROL_LAYER_INIT 0x0000000745#define RENCODE_IB_PARAM_RATE_CONTROL_PER_PICTURE 0x0000000846#define RENCODE_IB_PARAM_QUALITY_PARAMS 0x0000000947#define RENCODE_IB_PARAM_SLICE_HEADER 0x0000000a48#define RENCODE_IB_PARAM_ENCODE_PARAMS 0x0000000b49#define RENCODE_IB_PARAM_INTRA_REFRESH 0x0000000c50#define RENCODE_IB_PARAM_ENCODE_CONTEXT_BUFFER 0x0000000d51#define RENCODE_IB_PARAM_VIDEO_BITSTREAM_BUFFER 0x0000000e52#define RENCODE_IB_PARAM_FEEDBACK_BUFFER 0x0000001053#define RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU 0x000000205455#define RENCODE_HEVC_IB_PARAM_SLICE_CONTROL 0x0010000156#define RENCODE_HEVC_IB_PARAM_SPEC_MISC 0x0010000257#define RENCODE_HEVC_IB_PARAM_DEBLOCKING_FILTER 0x001000035859#define RENCODE_H264_IB_PARAM_SLICE_CONTROL 0x0020000160#define RENCODE_H264_IB_PARAM_SPEC_MISC 0x0020000261#define RENCODE_H264_IB_PARAM_ENCODE_PARAMS 0x0020000362#define RENCODE_H264_IB_PARAM_DEBLOCKING_FILTER 0x002000046364static void radeon_enc_session_info(struct radeon_encoder *enc)65{66RADEON_ENC_BEGIN(enc->cmd.session_info);67RADEON_ENC_CS(enc->enc_pic.session_info.interface_version);68RADEON_ENC_READWRITE(enc->si->res->buf, enc->si->res->domains, 0x0);69RADEON_ENC_CS(RENCODE_ENGINE_TYPE_ENCODE);70RADEON_ENC_END();71}7273static void radeon_enc_task_info(struct radeon_encoder *enc, bool need_feedback)74{75enc->enc_pic.task_info.task_id++;7677if (need_feedback)78enc->enc_pic.task_info.allowed_max_num_feedbacks = 1;79else80enc->enc_pic.task_info.allowed_max_num_feedbacks = 0;8182RADEON_ENC_BEGIN(enc->cmd.task_info);83enc->p_task_size = &enc->cs.current.buf[enc->cs.current.cdw++];84RADEON_ENC_CS(enc->enc_pic.task_info.task_id);85RADEON_ENC_CS(enc->enc_pic.task_info.allowed_max_num_feedbacks);86RADEON_ENC_END();87}8889static void radeon_enc_session_init(struct radeon_encoder *enc)90{91enc->enc_pic.session_init.encode_standard = RENCODE_ENCODE_STANDARD_H264;92enc->enc_pic.session_init.aligned_picture_width = align(enc->base.width, 16);93enc->enc_pic.session_init.aligned_picture_height = align(enc->base.height, 16);94enc->enc_pic.session_init.padding_width =95enc->enc_pic.session_init.aligned_picture_width - enc->base.width;96enc->enc_pic.session_init.padding_height =97enc->enc_pic.session_init.aligned_picture_height - enc->base.height;98enc->enc_pic.session_init.pre_encode_mode = RENCODE_PREENCODE_MODE_NONE;99enc->enc_pic.session_init.pre_encode_chroma_enabled = false;100101RADEON_ENC_BEGIN(enc->cmd.session_init);102RADEON_ENC_CS(enc->enc_pic.session_init.encode_standard);103RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_width);104RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_height);105RADEON_ENC_CS(enc->enc_pic.session_init.padding_width);106RADEON_ENC_CS(enc->enc_pic.session_init.padding_height);107RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_mode);108RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_chroma_enabled);109RADEON_ENC_END();110}111112static void radeon_enc_session_init_hevc(struct radeon_encoder *enc)113{114enc->enc_pic.session_init.encode_standard = RENCODE_ENCODE_STANDARD_HEVC;115enc->enc_pic.session_init.aligned_picture_width = align(enc->base.width, 64);116enc->enc_pic.session_init.aligned_picture_height = align(enc->base.height, 16);117enc->enc_pic.session_init.padding_width =118enc->enc_pic.session_init.aligned_picture_width - enc->base.width;119enc->enc_pic.session_init.padding_height =120enc->enc_pic.session_init.aligned_picture_height - enc->base.height;121enc->enc_pic.session_init.pre_encode_mode = RENCODE_PREENCODE_MODE_NONE;122enc->enc_pic.session_init.pre_encode_chroma_enabled = false;123124RADEON_ENC_BEGIN(enc->cmd.session_init);125RADEON_ENC_CS(enc->enc_pic.session_init.encode_standard);126RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_width);127RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_height);128RADEON_ENC_CS(enc->enc_pic.session_init.padding_width);129RADEON_ENC_CS(enc->enc_pic.session_init.padding_height);130RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_mode);131RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_chroma_enabled);132RADEON_ENC_END();133}134135static void radeon_enc_layer_control(struct radeon_encoder *enc)136{137enc->enc_pic.layer_ctrl.max_num_temporal_layers = 1;138enc->enc_pic.layer_ctrl.num_temporal_layers = 1;139140RADEON_ENC_BEGIN(enc->cmd.layer_control);141RADEON_ENC_CS(enc->enc_pic.layer_ctrl.max_num_temporal_layers);142RADEON_ENC_CS(enc->enc_pic.layer_ctrl.num_temporal_layers);143RADEON_ENC_END();144}145146static void radeon_enc_layer_select(struct radeon_encoder *enc)147{148enc->enc_pic.layer_sel.temporal_layer_index = 0;149150RADEON_ENC_BEGIN(enc->cmd.layer_select);151RADEON_ENC_CS(enc->enc_pic.layer_sel.temporal_layer_index);152RADEON_ENC_END();153}154155static void radeon_enc_slice_control(struct radeon_encoder *enc)156{157enc->enc_pic.slice_ctrl.slice_control_mode = RENCODE_H264_SLICE_CONTROL_MODE_FIXED_MBS;158enc->enc_pic.slice_ctrl.num_mbs_per_slice =159align(enc->base.width, 16) / 16 * align(enc->base.height, 16) / 16;160161RADEON_ENC_BEGIN(enc->cmd.slice_control_h264);162RADEON_ENC_CS(enc->enc_pic.slice_ctrl.slice_control_mode);163RADEON_ENC_CS(enc->enc_pic.slice_ctrl.num_mbs_per_slice);164RADEON_ENC_END();165}166167static void radeon_enc_slice_control_hevc(struct radeon_encoder *enc)168{169enc->enc_pic.hevc_slice_ctrl.slice_control_mode = RENCODE_HEVC_SLICE_CONTROL_MODE_FIXED_CTBS;170enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice =171align(enc->base.width, 64) / 64 * align(enc->base.height, 64) / 64;172enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment =173enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice;174175RADEON_ENC_BEGIN(enc->cmd.slice_control_hevc);176RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.slice_control_mode);177RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice);178RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice_segment);179RADEON_ENC_END();180}181182static void radeon_enc_spec_misc(struct radeon_encoder *enc)183{184enc->enc_pic.spec_misc.constrained_intra_pred_flag = 0;185enc->enc_pic.spec_misc.cabac_enable = 0;186enc->enc_pic.spec_misc.cabac_init_idc = 0;187enc->enc_pic.spec_misc.half_pel_enabled = 1;188enc->enc_pic.spec_misc.quarter_pel_enabled = 1;189enc->enc_pic.spec_misc.profile_idc = u_get_h264_profile_idc(enc->base.profile);190enc->enc_pic.spec_misc.level_idc = enc->base.level;191192RADEON_ENC_BEGIN(enc->cmd.spec_misc_h264);193RADEON_ENC_CS(enc->enc_pic.spec_misc.constrained_intra_pred_flag);194RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_enable);195RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_init_idc);196RADEON_ENC_CS(enc->enc_pic.spec_misc.half_pel_enabled);197RADEON_ENC_CS(enc->enc_pic.spec_misc.quarter_pel_enabled);198RADEON_ENC_CS(enc->enc_pic.spec_misc.profile_idc);199RADEON_ENC_CS(enc->enc_pic.spec_misc.level_idc);200RADEON_ENC_END();201}202203static void radeon_enc_spec_misc_hevc(struct radeon_encoder *enc)204{205RADEON_ENC_BEGIN(enc->cmd.spec_misc_hevc);206RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);207RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.amp_disabled);208RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled);209RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag);210RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.cabac_init_flag);211RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.half_pel_enabled);212RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.quarter_pel_enabled);213RADEON_ENC_END();214}215216static void radeon_enc_rc_session_init(struct radeon_encoder *enc)217{218RADEON_ENC_BEGIN(enc->cmd.rc_session_init);219RADEON_ENC_CS(enc->enc_pic.rc_session_init.rate_control_method);220RADEON_ENC_CS(enc->enc_pic.rc_session_init.vbv_buffer_level);221RADEON_ENC_END();222}223224static void radeon_enc_rc_layer_init(struct radeon_encoder *enc)225{226RADEON_ENC_BEGIN(enc->cmd.rc_layer_init);227RADEON_ENC_CS(enc->enc_pic.rc_layer_init.target_bit_rate);228RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bit_rate);229RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_num);230RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_den);231RADEON_ENC_CS(enc->enc_pic.rc_layer_init.vbv_buffer_size);232RADEON_ENC_CS(enc->enc_pic.rc_layer_init.avg_target_bits_per_picture);233RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_integer);234RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_fractional);235RADEON_ENC_END();236}237238static void radeon_enc_deblocking_filter_h264(struct radeon_encoder *enc)239{240enc->enc_pic.h264_deblock.disable_deblocking_filter_idc = 0;241enc->enc_pic.h264_deblock.alpha_c0_offset_div2 = 0;242enc->enc_pic.h264_deblock.beta_offset_div2 = 0;243enc->enc_pic.h264_deblock.cb_qp_offset = 0;244enc->enc_pic.h264_deblock.cr_qp_offset = 0;245246RADEON_ENC_BEGIN(enc->cmd.deblocking_filter_h264);247RADEON_ENC_CS(enc->enc_pic.h264_deblock.disable_deblocking_filter_idc);248RADEON_ENC_CS(enc->enc_pic.h264_deblock.alpha_c0_offset_div2);249RADEON_ENC_CS(enc->enc_pic.h264_deblock.beta_offset_div2);250RADEON_ENC_CS(enc->enc_pic.h264_deblock.cb_qp_offset);251RADEON_ENC_CS(enc->enc_pic.h264_deblock.cr_qp_offset);252RADEON_ENC_END();253}254255static void radeon_enc_deblocking_filter_hevc(struct radeon_encoder *enc)256{257RADEON_ENC_BEGIN(enc->cmd.deblocking_filter_hevc);258RADEON_ENC_CS(enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled);259RADEON_ENC_CS(enc->enc_pic.hevc_deblock.deblocking_filter_disabled);260RADEON_ENC_CS(enc->enc_pic.hevc_deblock.beta_offset_div2);261RADEON_ENC_CS(enc->enc_pic.hevc_deblock.tc_offset_div2);262RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cb_qp_offset);263RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cr_qp_offset);264RADEON_ENC_END();265}266267static void radeon_enc_quality_params(struct radeon_encoder *enc)268{269enc->enc_pic.quality_params.vbaq_mode = 0;270enc->enc_pic.quality_params.scene_change_sensitivity = 0;271enc->enc_pic.quality_params.scene_change_min_idr_interval = 0;272273RADEON_ENC_BEGIN(enc->cmd.quality_params);274RADEON_ENC_CS(enc->enc_pic.quality_params.vbaq_mode);275RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_sensitivity);276RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_min_idr_interval);277RADEON_ENC_END();278}279280static void radeon_enc_nalu_sps(struct radeon_encoder *enc)281{282RADEON_ENC_BEGIN(enc->cmd.nalu);283RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_SPS);284uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];285radeon_enc_reset(enc);286radeon_enc_set_emulation_prevention(enc, false);287radeon_enc_code_fixed_bits(enc, 0x00000001, 32);288radeon_enc_code_fixed_bits(enc, 0x67, 8);289radeon_enc_byte_align(enc);290radeon_enc_set_emulation_prevention(enc, true);291radeon_enc_code_fixed_bits(enc, enc->enc_pic.spec_misc.profile_idc, 8);292radeon_enc_code_fixed_bits(enc, 0x44, 8); // hardcode to constrained baseline293radeon_enc_code_fixed_bits(enc, enc->enc_pic.spec_misc.level_idc, 8);294radeon_enc_code_ue(enc, 0x0);295296if (enc->enc_pic.spec_misc.profile_idc == 100 || enc->enc_pic.spec_misc.profile_idc == 110 ||297enc->enc_pic.spec_misc.profile_idc == 122 || enc->enc_pic.spec_misc.profile_idc == 244 ||298enc->enc_pic.spec_misc.profile_idc == 44 || enc->enc_pic.spec_misc.profile_idc == 83 ||299enc->enc_pic.spec_misc.profile_idc == 86 || enc->enc_pic.spec_misc.profile_idc == 118 ||300enc->enc_pic.spec_misc.profile_idc == 128 || enc->enc_pic.spec_misc.profile_idc == 138) {301radeon_enc_code_ue(enc, 0x1);302radeon_enc_code_ue(enc, 0x0);303radeon_enc_code_ue(enc, 0x0);304radeon_enc_code_fixed_bits(enc, 0x0, 2);305}306307radeon_enc_code_ue(enc, 1);308radeon_enc_code_ue(enc, enc->enc_pic.pic_order_cnt_type);309310if (enc->enc_pic.pic_order_cnt_type == 0)311radeon_enc_code_ue(enc, 1);312313radeon_enc_code_ue(enc, (enc->base.max_references + 1));314radeon_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers > 1 ? 0x1 : 0x0,3151);316radeon_enc_code_ue(enc, (enc->enc_pic.session_init.aligned_picture_width / 16 - 1));317radeon_enc_code_ue(enc, (enc->enc_pic.session_init.aligned_picture_height / 16 - 1));318bool progressive_only = true;319radeon_enc_code_fixed_bits(enc, progressive_only ? 0x1 : 0x0, 1);320321if (!progressive_only)322radeon_enc_code_fixed_bits(enc, 0x0, 1);323324radeon_enc_code_fixed_bits(enc, 0x1, 1);325326if ((enc->enc_pic.crop_left != 0) || (enc->enc_pic.crop_right != 0) ||327(enc->enc_pic.crop_top != 0) || (enc->enc_pic.crop_bottom != 0)) {328radeon_enc_code_fixed_bits(enc, 0x1, 1);329radeon_enc_code_ue(enc, enc->enc_pic.crop_left);330radeon_enc_code_ue(enc, enc->enc_pic.crop_right);331radeon_enc_code_ue(enc, enc->enc_pic.crop_top);332radeon_enc_code_ue(enc, enc->enc_pic.crop_bottom);333} else334radeon_enc_code_fixed_bits(enc, 0x0, 1);335336radeon_enc_code_fixed_bits(enc, 0x1, 1);337radeon_enc_code_fixed_bits(enc, 0x0, 1);338radeon_enc_code_fixed_bits(enc, 0x0, 1);339radeon_enc_code_fixed_bits(enc, 0x0, 1);340radeon_enc_code_fixed_bits(enc, 0x0, 1);341radeon_enc_code_fixed_bits(enc, 0x0, 1);342radeon_enc_code_fixed_bits(enc, 0x0, 1);343radeon_enc_code_fixed_bits(enc, 0x0, 1);344radeon_enc_code_fixed_bits(enc, 0x0, 1);345radeon_enc_code_fixed_bits(enc, 0x1, 1);346radeon_enc_code_fixed_bits(enc, 0x1, 1);347radeon_enc_code_ue(enc, 0x0);348radeon_enc_code_ue(enc, 0x0);349radeon_enc_code_ue(enc, 16);350radeon_enc_code_ue(enc, 16);351radeon_enc_code_ue(enc, 0x0);352radeon_enc_code_ue(enc, (enc->base.max_references + 1));353354radeon_enc_code_fixed_bits(enc, 0x1, 1);355356radeon_enc_byte_align(enc);357radeon_enc_flush_headers(enc);358*size_in_bytes = (enc->bits_output + 7) / 8;359RADEON_ENC_END();360}361362static void radeon_enc_nalu_sps_hevc(struct radeon_encoder *enc)363{364RADEON_ENC_BEGIN(enc->cmd.nalu);365RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_SPS);366uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];367int i;368369radeon_enc_reset(enc);370radeon_enc_set_emulation_prevention(enc, false);371radeon_enc_code_fixed_bits(enc, 0x00000001, 32);372radeon_enc_code_fixed_bits(enc, 0x4201, 16);373radeon_enc_byte_align(enc);374radeon_enc_set_emulation_prevention(enc, true);375radeon_enc_code_fixed_bits(enc, 0x0, 4);376radeon_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1, 3);377radeon_enc_code_fixed_bits(enc, 0x1, 1);378radeon_enc_code_fixed_bits(enc, 0x0, 2);379radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);380radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);381radeon_enc_code_fixed_bits(enc, 0x60000000, 32);382radeon_enc_code_fixed_bits(enc, 0xb0000000, 32);383radeon_enc_code_fixed_bits(enc, 0x0, 16);384radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);385386for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)387radeon_enc_code_fixed_bits(enc, 0x0, 2);388389if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {390for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)391radeon_enc_code_fixed_bits(enc, 0x0, 2);392}393394radeon_enc_code_ue(enc, 0x0);395radeon_enc_code_ue(enc, enc->enc_pic.chroma_format_idc);396radeon_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_width);397radeon_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_height);398399if ((enc->enc_pic.crop_left != 0) || (enc->enc_pic.crop_right != 0) ||400(enc->enc_pic.crop_top != 0) || (enc->enc_pic.crop_bottom != 0)) {401radeon_enc_code_fixed_bits(enc, 0x1, 1);402radeon_enc_code_ue(enc, enc->enc_pic.crop_left);403radeon_enc_code_ue(enc, enc->enc_pic.crop_right);404radeon_enc_code_ue(enc, enc->enc_pic.crop_top);405radeon_enc_code_ue(enc, enc->enc_pic.crop_bottom);406} else if (enc->enc_pic.session_init.padding_width != 0 ||407enc->enc_pic.session_init.padding_height != 0) {408radeon_enc_code_fixed_bits(enc, 0x1, 1);409radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_width / 2);410radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_width / 2);411radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_height / 2);412radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_height / 2);413} else414radeon_enc_code_fixed_bits(enc, 0x0, 1);415416radeon_enc_code_ue(enc, enc->enc_pic.bit_depth_luma_minus8);417radeon_enc_code_ue(enc, enc->enc_pic.bit_depth_chroma_minus8);418radeon_enc_code_ue(enc, enc->enc_pic.log2_max_poc - 4);419radeon_enc_code_fixed_bits(enc, 0x0, 1);420radeon_enc_code_ue(enc, 1);421radeon_enc_code_ue(enc, 0x0);422radeon_enc_code_ue(enc, 0x0);423radeon_enc_code_ue(enc, enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);424// Only support CTBSize 64425radeon_enc_code_ue(enc,4266 - (enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 + 3));427radeon_enc_code_ue(enc, enc->enc_pic.log2_min_transform_block_size_minus2);428radeon_enc_code_ue(enc, enc->enc_pic.log2_diff_max_min_transform_block_size);429radeon_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_inter);430radeon_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_intra);431432radeon_enc_code_fixed_bits(enc, 0x0, 1);433radeon_enc_code_fixed_bits(enc, !enc->enc_pic.hevc_spec_misc.amp_disabled, 1);434radeon_enc_code_fixed_bits(enc, enc->enc_pic.sample_adaptive_offset_enabled_flag, 1);435radeon_enc_code_fixed_bits(enc, enc->enc_pic.pcm_enabled_flag, 1);436437radeon_enc_code_ue(enc, 1);438radeon_enc_code_ue(enc, 1);439radeon_enc_code_ue(enc, 0);440radeon_enc_code_ue(enc, 0);441radeon_enc_code_fixed_bits(enc, 0x1, 1);442443radeon_enc_code_fixed_bits(enc, 0x0, 1);444445radeon_enc_code_fixed_bits(enc, 0, 1);446radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled, 1);447448radeon_enc_code_fixed_bits(enc, 0x0, 1);449450radeon_enc_code_fixed_bits(enc, 0x0, 1);451452radeon_enc_code_fixed_bits(enc, 0x1, 1);453454radeon_enc_byte_align(enc);455radeon_enc_flush_headers(enc);456*size_in_bytes = (enc->bits_output + 7) / 8;457RADEON_ENC_END();458}459460static void radeon_enc_nalu_pps(struct radeon_encoder *enc)461{462RADEON_ENC_BEGIN(enc->cmd.nalu);463RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS);464uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];465radeon_enc_reset(enc);466radeon_enc_set_emulation_prevention(enc, false);467radeon_enc_code_fixed_bits(enc, 0x00000001, 32);468radeon_enc_code_fixed_bits(enc, 0x68, 8);469radeon_enc_byte_align(enc);470radeon_enc_set_emulation_prevention(enc, true);471radeon_enc_code_ue(enc, 0x0);472radeon_enc_code_ue(enc, 0x0);473radeon_enc_code_fixed_bits(enc, (enc->enc_pic.spec_misc.cabac_enable ? 0x1 : 0x0), 1);474radeon_enc_code_fixed_bits(enc, 0x0, 1);475radeon_enc_code_ue(enc, 0x0);476radeon_enc_code_ue(enc, 0x0);477radeon_enc_code_ue(enc, 0x0);478radeon_enc_code_fixed_bits(enc, 0x0, 1);479radeon_enc_code_fixed_bits(enc, 0x0, 2);480radeon_enc_code_se(enc, 0x0);481radeon_enc_code_se(enc, 0x0);482radeon_enc_code_se(enc, 0x0);483radeon_enc_code_fixed_bits(enc, 0x1, 1);484radeon_enc_code_fixed_bits(enc, 0x0, 1);485radeon_enc_code_fixed_bits(enc, 0x0, 1);486487radeon_enc_code_fixed_bits(enc, 0x1, 1);488489radeon_enc_byte_align(enc);490radeon_enc_flush_headers(enc);491*size_in_bytes = (enc->bits_output + 7) / 8;492RADEON_ENC_END();493}494495static void radeon_enc_nalu_pps_hevc(struct radeon_encoder *enc)496{497RADEON_ENC_BEGIN(enc->cmd.nalu);498RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS);499uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];500radeon_enc_reset(enc);501radeon_enc_set_emulation_prevention(enc, false);502radeon_enc_code_fixed_bits(enc, 0x00000001, 32);503radeon_enc_code_fixed_bits(enc, 0x4401, 16);504radeon_enc_byte_align(enc);505radeon_enc_set_emulation_prevention(enc, true);506radeon_enc_code_ue(enc, 0x0);507radeon_enc_code_ue(enc, 0x0);508radeon_enc_code_fixed_bits(enc, 0x1, 1);509radeon_enc_code_fixed_bits(enc, 0x0, 4);510radeon_enc_code_fixed_bits(enc, 0x0, 1);511radeon_enc_code_fixed_bits(enc, 0x1, 1);512radeon_enc_code_ue(enc, 0x0);513radeon_enc_code_ue(enc, 0x0);514radeon_enc_code_se(enc, 0x0);515radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag, 1);516radeon_enc_code_fixed_bits(enc, 0x0, 1);517if (enc->enc_pic.rc_session_init.rate_control_method == RENCODE_RATE_CONTROL_METHOD_NONE)518radeon_enc_code_fixed_bits(enc, 0x0, 1);519else {520radeon_enc_code_fixed_bits(enc, 0x1, 1);521radeon_enc_code_ue(enc, 0x0);522}523radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cb_qp_offset);524radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cr_qp_offset);525radeon_enc_code_fixed_bits(enc, 0x0, 1);526radeon_enc_code_fixed_bits(enc, 0x0, 2);527radeon_enc_code_fixed_bits(enc, 0x0, 1);528radeon_enc_code_fixed_bits(enc, 0x0, 1);529radeon_enc_code_fixed_bits(enc, 0x0, 1);530radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled, 1);531radeon_enc_code_fixed_bits(enc, 0x1, 1);532radeon_enc_code_fixed_bits(enc, 0x0, 1);533radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.deblocking_filter_disabled, 1);534535if (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled) {536radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.beta_offset_div2);537radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.tc_offset_div2);538}539540radeon_enc_code_fixed_bits(enc, 0x0, 1);541radeon_enc_code_fixed_bits(enc, 0x0, 1);542radeon_enc_code_ue(enc, enc->enc_pic.log2_parallel_merge_level_minus2);543radeon_enc_code_fixed_bits(enc, 0x0, 2);544545radeon_enc_code_fixed_bits(enc, 0x1, 1);546547radeon_enc_byte_align(enc);548radeon_enc_flush_headers(enc);549*size_in_bytes = (enc->bits_output + 7) / 8;550RADEON_ENC_END();551}552553static void radeon_enc_nalu_vps(struct radeon_encoder *enc)554{555RADEON_ENC_BEGIN(enc->cmd.nalu);556RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_VPS);557uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];558int i;559560radeon_enc_reset(enc);561radeon_enc_set_emulation_prevention(enc, false);562radeon_enc_code_fixed_bits(enc, 0x00000001, 32);563radeon_enc_code_fixed_bits(enc, 0x4001, 16);564radeon_enc_byte_align(enc);565radeon_enc_set_emulation_prevention(enc, true);566567radeon_enc_code_fixed_bits(enc, 0x0, 4);568radeon_enc_code_fixed_bits(enc, 0x3, 2);569radeon_enc_code_fixed_bits(enc, 0x0, 6);570radeon_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1, 3);571radeon_enc_code_fixed_bits(enc, 0x1, 1);572radeon_enc_code_fixed_bits(enc, 0xffff, 16);573radeon_enc_code_fixed_bits(enc, 0x0, 2);574radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);575radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);576radeon_enc_code_fixed_bits(enc, 0x60000000, 32);577radeon_enc_code_fixed_bits(enc, 0xb0000000, 32);578radeon_enc_code_fixed_bits(enc, 0x0, 16);579radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);580581for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)582radeon_enc_code_fixed_bits(enc, 0x0, 2);583584if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {585for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)586radeon_enc_code_fixed_bits(enc, 0x0, 2);587}588589radeon_enc_code_fixed_bits(enc, 0x0, 1);590radeon_enc_code_ue(enc, 0x1);591radeon_enc_code_ue(enc, 0x0);592radeon_enc_code_ue(enc, 0x0);593594radeon_enc_code_fixed_bits(enc, 0x0, 6);595radeon_enc_code_ue(enc, 0x0);596radeon_enc_code_fixed_bits(enc, 0x0, 1);597radeon_enc_code_fixed_bits(enc, 0x0, 1);598599radeon_enc_code_fixed_bits(enc, 0x1, 1);600601radeon_enc_byte_align(enc);602radeon_enc_flush_headers(enc);603*size_in_bytes = (enc->bits_output + 7) / 8;604RADEON_ENC_END();605}606607static void radeon_enc_nalu_aud_hevc(struct radeon_encoder *enc)608{609RADEON_ENC_BEGIN(enc->cmd.nalu);610RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_AUD);611uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];612radeon_enc_reset(enc);613radeon_enc_set_emulation_prevention(enc, false);614radeon_enc_code_fixed_bits(enc, 0x00000001, 32);615radeon_enc_code_fixed_bits(enc, 0x0, 1);616radeon_enc_code_fixed_bits(enc, 35, 6);617radeon_enc_code_fixed_bits(enc, 0x0, 6);618radeon_enc_code_fixed_bits(enc, 0x1, 3);619radeon_enc_byte_align(enc);620radeon_enc_set_emulation_prevention(enc, true);621switch (enc->enc_pic.picture_type) {622case PIPE_H2645_ENC_PICTURE_TYPE_I:623case PIPE_H2645_ENC_PICTURE_TYPE_IDR:624radeon_enc_code_fixed_bits(enc, 0x00, 3);625break;626case PIPE_H2645_ENC_PICTURE_TYPE_P:627radeon_enc_code_fixed_bits(enc, 0x01, 3);628break;629case PIPE_H2645_ENC_PICTURE_TYPE_B:630radeon_enc_code_fixed_bits(enc, 0x02, 3);631break;632default:633radeon_enc_code_fixed_bits(enc, 0x02, 3);634}635636radeon_enc_code_fixed_bits(enc, 0x1, 1);637638radeon_enc_byte_align(enc);639radeon_enc_flush_headers(enc);640*size_in_bytes = (enc->bits_output + 7) / 8;641RADEON_ENC_END();642}643644static void radeon_enc_slice_header(struct radeon_encoder *enc)645{646uint32_t instruction[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};647uint32_t num_bits[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};648unsigned int inst_index = 0;649unsigned int cdw_start = 0;650unsigned int cdw_filled = 0;651unsigned int bits_copied = 0;652RADEON_ENC_BEGIN(enc->cmd.slice_header);653radeon_enc_reset(enc);654radeon_enc_set_emulation_prevention(enc, false);655656cdw_start = enc->cs.current.cdw;657if (enc->enc_pic.is_idr)658radeon_enc_code_fixed_bits(enc, 0x65, 8);659else if (enc->enc_pic.not_referenced)660radeon_enc_code_fixed_bits(enc, 0x01, 8);661else662radeon_enc_code_fixed_bits(enc, 0x41, 8);663664radeon_enc_flush_headers(enc);665instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;666num_bits[inst_index] = enc->bits_output - bits_copied;667bits_copied = enc->bits_output;668inst_index++;669670instruction[inst_index] = RENCODE_H264_HEADER_INSTRUCTION_FIRST_MB;671inst_index++;672673switch (enc->enc_pic.picture_type) {674case PIPE_H2645_ENC_PICTURE_TYPE_I:675case PIPE_H2645_ENC_PICTURE_TYPE_IDR:676radeon_enc_code_fixed_bits(enc, 0x08, 7);677break;678case PIPE_H2645_ENC_PICTURE_TYPE_P:679case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:680radeon_enc_code_fixed_bits(enc, 0x06, 5);681break;682case PIPE_H2645_ENC_PICTURE_TYPE_B:683radeon_enc_code_fixed_bits(enc, 0x07, 5);684break;685default:686radeon_enc_code_fixed_bits(enc, 0x08, 7);687}688689radeon_enc_code_ue(enc, 0x0);690radeon_enc_code_fixed_bits(enc, enc->enc_pic.frame_num % 32, 5);691692if (enc->enc_pic.h264_enc_params.input_picture_structure !=693RENCODE_H264_PICTURE_STRUCTURE_FRAME) {694radeon_enc_code_fixed_bits(enc, 0x1, 1);695radeon_enc_code_fixed_bits(enc,696enc->enc_pic.h264_enc_params.input_picture_structure ==697RENCODE_H264_PICTURE_STRUCTURE_BOTTOM_FIELD698? 1699: 0,7001);701}702703if (enc->enc_pic.is_idr)704radeon_enc_code_ue(enc, enc->enc_pic.is_even_frame);705706enc->enc_pic.is_even_frame = !enc->enc_pic.is_even_frame;707708if (enc->enc_pic.pic_order_cnt_type == 0)709radeon_enc_code_fixed_bits(enc, enc->enc_pic.pic_order_cnt % 32, 5);710711if (enc->enc_pic.picture_type != PIPE_H2645_ENC_PICTURE_TYPE_IDR) {712radeon_enc_code_fixed_bits(enc, 0x0, 1);713714if (enc->enc_pic.frame_num - enc->enc_pic.ref_idx_l0 > 1) {715radeon_enc_code_fixed_bits(enc, 0x1, 1);716radeon_enc_code_ue(enc, 0x0);717radeon_enc_code_ue(enc, (enc->enc_pic.frame_num - enc->enc_pic.ref_idx_l0 - 1));718radeon_enc_code_ue(enc, 0x3);719} else720radeon_enc_code_fixed_bits(enc, 0x0, 1);721}722723if (enc->enc_pic.is_idr) {724radeon_enc_code_fixed_bits(enc, 0x0, 1);725radeon_enc_code_fixed_bits(enc, 0x0, 1);726} else727radeon_enc_code_fixed_bits(enc, 0x0, 1);728729if ((enc->enc_pic.picture_type != PIPE_H2645_ENC_PICTURE_TYPE_IDR) &&730(enc->enc_pic.spec_misc.cabac_enable))731radeon_enc_code_ue(enc, enc->enc_pic.spec_misc.cabac_init_idc);732733radeon_enc_flush_headers(enc);734instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;735num_bits[inst_index] = enc->bits_output - bits_copied;736bits_copied = enc->bits_output;737inst_index++;738739instruction[inst_index] = RENCODE_H264_HEADER_INSTRUCTION_SLICE_QP_DELTA;740inst_index++;741742radeon_enc_code_ue(enc, enc->enc_pic.h264_deblock.disable_deblocking_filter_idc ? 1 : 0);743744if (!enc->enc_pic.h264_deblock.disable_deblocking_filter_idc) {745radeon_enc_code_se(enc, enc->enc_pic.h264_deblock.alpha_c0_offset_div2);746radeon_enc_code_se(enc, enc->enc_pic.h264_deblock.beta_offset_div2);747}748749radeon_enc_flush_headers(enc);750instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;751num_bits[inst_index] = enc->bits_output - bits_copied;752bits_copied = enc->bits_output;753inst_index++;754755instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_END;756757cdw_filled = enc->cs.current.cdw - cdw_start;758for (int i = 0; i < RENCODE_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS - cdw_filled; i++)759RADEON_ENC_CS(0x00000000);760761for (int j = 0; j < RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS; j++) {762RADEON_ENC_CS(instruction[j]);763RADEON_ENC_CS(num_bits[j]);764}765766RADEON_ENC_END();767}768769static void radeon_enc_slice_header_hevc(struct radeon_encoder *enc)770{771uint32_t instruction[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};772uint32_t num_bits[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};773unsigned int inst_index = 0;774unsigned int cdw_start = 0;775unsigned int cdw_filled = 0;776unsigned int bits_copied = 0;777RADEON_ENC_BEGIN(enc->cmd.slice_header);778radeon_enc_reset(enc);779radeon_enc_set_emulation_prevention(enc, false);780781cdw_start = enc->cs.current.cdw;782radeon_enc_code_fixed_bits(enc, 0x0, 1);783radeon_enc_code_fixed_bits(enc, enc->enc_pic.nal_unit_type, 6);784radeon_enc_code_fixed_bits(enc, 0x0, 6);785radeon_enc_code_fixed_bits(enc, 0x1, 3);786787radeon_enc_flush_headers(enc);788instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;789num_bits[inst_index] = enc->bits_output - bits_copied;790bits_copied = enc->bits_output;791inst_index++;792793instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_FIRST_SLICE;794inst_index++;795796if ((enc->enc_pic.nal_unit_type >= 16) && (enc->enc_pic.nal_unit_type <= 23))797radeon_enc_code_fixed_bits(enc, 0x0, 1);798799radeon_enc_code_ue(enc, 0x0);800801radeon_enc_flush_headers(enc);802instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;803num_bits[inst_index] = enc->bits_output - bits_copied;804bits_copied = enc->bits_output;805inst_index++;806807instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_SEGMENT;808inst_index++;809810instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_DEPENDENT_SLICE_END;811inst_index++;812813switch (enc->enc_pic.picture_type) {814case PIPE_H2645_ENC_PICTURE_TYPE_I:815case PIPE_H2645_ENC_PICTURE_TYPE_IDR:816radeon_enc_code_ue(enc, 0x2);817break;818case PIPE_H2645_ENC_PICTURE_TYPE_P:819case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:820radeon_enc_code_ue(enc, 0x1);821break;822case PIPE_H2645_ENC_PICTURE_TYPE_B:823radeon_enc_code_ue(enc, 0x0);824break;825default:826radeon_enc_code_ue(enc, 0x1);827}828829if ((enc->enc_pic.nal_unit_type != 19) && (enc->enc_pic.nal_unit_type != 20)) {830radeon_enc_code_fixed_bits(enc, enc->enc_pic.pic_order_cnt, enc->enc_pic.log2_max_poc);831if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P)832radeon_enc_code_fixed_bits(enc, 0x1, 1);833else {834radeon_enc_code_fixed_bits(enc, 0x0, 1);835radeon_enc_code_fixed_bits(enc, 0x0, 1);836radeon_enc_code_ue(enc, 0x0);837radeon_enc_code_ue(enc, 0x0);838}839}840841if ((enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P) ||842(enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B)) {843radeon_enc_code_fixed_bits(enc, 0x0, 1);844radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.cabac_init_flag, 1);845radeon_enc_code_ue(enc, 5 - enc->enc_pic.max_num_merge_cand);846}847848radeon_enc_flush_headers(enc);849instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;850num_bits[inst_index] = enc->bits_output - bits_copied;851bits_copied = enc->bits_output;852inst_index++;853854instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_QP_DELTA;855inst_index++;856857if ((enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled) &&858(!enc->enc_pic.hevc_deblock.deblocking_filter_disabled)) {859radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled,8601);861862radeon_enc_flush_headers(enc);863instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;864num_bits[inst_index] = enc->bits_output - bits_copied;865bits_copied = enc->bits_output;866inst_index++;867}868869instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_END;870871cdw_filled = enc->cs.current.cdw - cdw_start;872for (int i = 0; i < RENCODE_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS - cdw_filled; i++)873RADEON_ENC_CS(0x00000000);874875for (int j = 0; j < RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS; j++) {876RADEON_ENC_CS(instruction[j]);877RADEON_ENC_CS(num_bits[j]);878}879880RADEON_ENC_END();881}882883static void radeon_enc_ctx(struct radeon_encoder *enc)884{885enc->enc_pic.ctx_buf.swizzle_mode = 0;886enc->enc_pic.ctx_buf.rec_luma_pitch = align(enc->base.width, enc->alignment);887enc->enc_pic.ctx_buf.rec_chroma_pitch = align(enc->base.width, enc->alignment);888enc->enc_pic.ctx_buf.num_reconstructed_pictures = 2;889890RADEON_ENC_BEGIN(enc->cmd.ctx);891RADEON_ENC_READWRITE(enc->cpb.res->buf, enc->cpb.res->domains, 0);892RADEON_ENC_CS(enc->enc_pic.ctx_buf.swizzle_mode);893RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);894RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);895RADEON_ENC_CS(enc->enc_pic.ctx_buf.num_reconstructed_pictures);896/* reconstructed_picture_1_luma_offset */897RADEON_ENC_CS(0x00000000);898/* reconstructed_picture_1_chroma_offset */899RADEON_ENC_CS(align(enc->base.width, enc->alignment) * align(enc->base.height, 16));900/* reconstructed_picture_2_luma_offset */901RADEON_ENC_CS(align(enc->base.width, enc->alignment) * align(enc->base.height, 16) * 3 / 2);902/* reconstructed_picture_2_chroma_offset */903RADEON_ENC_CS(align(enc->base.width, enc->alignment) * align(enc->base.height, 16) * 5 / 2);904905for (int i = 0; i < 136; i++)906RADEON_ENC_CS(0x00000000);907908RADEON_ENC_END();909}910911static void radeon_enc_bitstream(struct radeon_encoder *enc)912{913enc->enc_pic.bit_buf.mode = RENCODE_REC_SWIZZLE_MODE_LINEAR;914enc->enc_pic.bit_buf.video_bitstream_buffer_size = enc->bs_size;915enc->enc_pic.bit_buf.video_bitstream_data_offset = 0;916917RADEON_ENC_BEGIN(enc->cmd.bitstream);918RADEON_ENC_CS(enc->enc_pic.bit_buf.mode);919RADEON_ENC_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT, 0);920RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_buffer_size);921RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_data_offset);922RADEON_ENC_END();923}924925static void radeon_enc_feedback(struct radeon_encoder *enc)926{927enc->enc_pic.fb_buf.mode = RENCODE_FEEDBACK_BUFFER_MODE_LINEAR;928enc->enc_pic.fb_buf.feedback_buffer_size = 16;929enc->enc_pic.fb_buf.feedback_data_size = 40;930931RADEON_ENC_BEGIN(enc->cmd.feedback);932RADEON_ENC_CS(enc->enc_pic.fb_buf.mode);933RADEON_ENC_WRITE(enc->fb->res->buf, enc->fb->res->domains, 0x0);934RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_buffer_size);935RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_data_size);936RADEON_ENC_END();937}938939static void radeon_enc_intra_refresh(struct radeon_encoder *enc)940{941enc->enc_pic.intra_ref.intra_refresh_mode = RENCODE_INTRA_REFRESH_MODE_NONE;942enc->enc_pic.intra_ref.offset = 0;943enc->enc_pic.intra_ref.region_size = 0;944945RADEON_ENC_BEGIN(enc->cmd.intra_refresh);946RADEON_ENC_CS(enc->enc_pic.intra_ref.intra_refresh_mode);947RADEON_ENC_CS(enc->enc_pic.intra_ref.offset);948RADEON_ENC_CS(enc->enc_pic.intra_ref.region_size);949RADEON_ENC_END();950}951952static void radeon_enc_rc_per_pic(struct radeon_encoder *enc)953{954RADEON_ENC_BEGIN(enc->cmd.rc_per_pic);955RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp);956RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_app);957RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_app);958RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size);959RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enabled_filler_data);960RADEON_ENC_CS(enc->enc_pic.rc_per_pic.skip_frame_enable);961RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enforce_hrd);962RADEON_ENC_END();963}964965static void radeon_enc_encode_params(struct radeon_encoder *enc)966{967switch (enc->enc_pic.picture_type) {968case PIPE_H2645_ENC_PICTURE_TYPE_I:969case PIPE_H2645_ENC_PICTURE_TYPE_IDR:970enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;971break;972case PIPE_H2645_ENC_PICTURE_TYPE_P:973enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P;974break;975case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:976enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P_SKIP;977break;978case PIPE_H2645_ENC_PICTURE_TYPE_B:979enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_B;980break;981default:982enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;983}984985if (enc->luma->meta_offset) {986RVID_ERR("DCC surfaces not supported.\n");987return;988}989990enc->enc_pic.enc_params.allowed_max_bitstream_size = enc->bs_size;991enc->enc_pic.enc_params.input_pic_luma_pitch = enc->luma->u.gfx9.surf_pitch;992enc->enc_pic.enc_params.input_pic_chroma_pitch = enc->chroma->u.gfx9.surf_pitch;993enc->enc_pic.enc_params.input_pic_swizzle_mode = enc->luma->u.gfx9.swizzle_mode;994995if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)996enc->enc_pic.enc_params.reference_picture_index = 0xFFFFFFFF;997else998enc->enc_pic.enc_params.reference_picture_index = (enc->enc_pic.frame_num - 1) % 2;9991000enc->enc_pic.enc_params.reconstructed_picture_index = enc->enc_pic.frame_num % 2;10011002RADEON_ENC_BEGIN(enc->cmd.enc_params);1003RADEON_ENC_CS(enc->enc_pic.enc_params.pic_type);1004RADEON_ENC_CS(enc->enc_pic.enc_params.allowed_max_bitstream_size);1005RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->luma->u.gfx9.surf_offset);1006RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->chroma->u.gfx9.surf_offset);1007RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_luma_pitch);1008RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_chroma_pitch);1009RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_swizzle_mode);1010RADEON_ENC_CS(enc->enc_pic.enc_params.reference_picture_index);1011RADEON_ENC_CS(enc->enc_pic.enc_params.reconstructed_picture_index);1012RADEON_ENC_END();1013}10141015static void radeon_enc_encode_params_hevc(struct radeon_encoder *enc)1016{1017switch (enc->enc_pic.picture_type) {1018case PIPE_H2645_ENC_PICTURE_TYPE_I:1019case PIPE_H2645_ENC_PICTURE_TYPE_IDR:1020enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;1021break;1022case PIPE_H2645_ENC_PICTURE_TYPE_P:1023enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P;1024break;1025case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:1026enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P_SKIP;1027break;1028case PIPE_H2645_ENC_PICTURE_TYPE_B:1029enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_B;1030break;1031default:1032enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;1033}10341035if (enc->luma->meta_offset) {1036RVID_ERR("DCC surfaces not supported.\n");1037return;1038}10391040enc->enc_pic.enc_params.allowed_max_bitstream_size = enc->bs_size;1041enc->enc_pic.enc_params.input_pic_luma_pitch = enc->luma->u.gfx9.surf_pitch;1042enc->enc_pic.enc_params.input_pic_chroma_pitch = enc->chroma->u.gfx9.surf_pitch;1043enc->enc_pic.enc_params.input_pic_swizzle_mode = enc->luma->u.gfx9.swizzle_mode;10441045if (enc->enc_pic.enc_params.pic_type == RENCODE_PICTURE_TYPE_I)1046enc->enc_pic.enc_params.reference_picture_index = 0xFFFFFFFF;1047else1048enc->enc_pic.enc_params.reference_picture_index = (enc->enc_pic.frame_num - 1) % 2;10491050enc->enc_pic.enc_params.reconstructed_picture_index = enc->enc_pic.frame_num % 2;10511052RADEON_ENC_BEGIN(enc->cmd.enc_params);1053RADEON_ENC_CS(enc->enc_pic.enc_params.pic_type);1054RADEON_ENC_CS(enc->enc_pic.enc_params.allowed_max_bitstream_size);1055RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->luma->u.gfx9.surf_offset);1056RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->chroma->u.gfx9.surf_offset);1057RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_luma_pitch);1058RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_chroma_pitch);1059RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_swizzle_mode);1060RADEON_ENC_CS(enc->enc_pic.enc_params.reference_picture_index);1061RADEON_ENC_CS(enc->enc_pic.enc_params.reconstructed_picture_index);1062RADEON_ENC_END();1063}10641065static void radeon_enc_encode_params_h264(struct radeon_encoder *enc)1066{1067enc->enc_pic.h264_enc_params.input_picture_structure = RENCODE_H264_PICTURE_STRUCTURE_FRAME;1068enc->enc_pic.h264_enc_params.interlaced_mode = RENCODE_H264_INTERLACING_MODE_PROGRESSIVE;1069enc->enc_pic.h264_enc_params.reference_picture_structure = RENCODE_H264_PICTURE_STRUCTURE_FRAME;1070enc->enc_pic.h264_enc_params.reference_picture1_index = 0xFFFFFFFF;10711072RADEON_ENC_BEGIN(enc->cmd.enc_params_h264);1073RADEON_ENC_CS(enc->enc_pic.h264_enc_params.input_picture_structure);1074RADEON_ENC_CS(enc->enc_pic.h264_enc_params.interlaced_mode);1075RADEON_ENC_CS(enc->enc_pic.h264_enc_params.reference_picture_structure);1076RADEON_ENC_CS(enc->enc_pic.h264_enc_params.reference_picture1_index);1077RADEON_ENC_END();1078}10791080static void radeon_enc_op_init(struct radeon_encoder *enc)1081{1082RADEON_ENC_BEGIN(RENCODE_IB_OP_INITIALIZE);1083RADEON_ENC_END();1084}10851086static void radeon_enc_op_close(struct radeon_encoder *enc)1087{1088RADEON_ENC_BEGIN(RENCODE_IB_OP_CLOSE_SESSION);1089RADEON_ENC_END();1090}10911092static void radeon_enc_op_enc(struct radeon_encoder *enc)1093{1094RADEON_ENC_BEGIN(RENCODE_IB_OP_ENCODE);1095RADEON_ENC_END();1096}10971098static void radeon_enc_op_init_rc(struct radeon_encoder *enc)1099{1100RADEON_ENC_BEGIN(RENCODE_IB_OP_INIT_RC);1101RADEON_ENC_END();1102}11031104static void radeon_enc_op_init_rc_vbv(struct radeon_encoder *enc)1105{1106RADEON_ENC_BEGIN(RENCODE_IB_OP_INIT_RC_VBV_BUFFER_LEVEL);1107RADEON_ENC_END();1108}11091110static void radeon_enc_op_speed(struct radeon_encoder *enc)1111{1112RADEON_ENC_BEGIN(RENCODE_IB_OP_SET_SPEED_ENCODING_MODE);1113RADEON_ENC_END();1114}11151116static void begin(struct radeon_encoder *enc)1117{1118enc->session_info(enc);1119enc->total_task_size = 0;1120enc->task_info(enc, enc->need_feedback);1121enc->op_init(enc);11221123enc->session_init(enc);1124enc->slice_control(enc);1125enc->spec_misc(enc);1126enc->deblocking_filter(enc);11271128enc->layer_control(enc);1129enc->rc_session_init(enc);1130enc->quality_params(enc);1131enc->layer_select(enc);1132enc->rc_layer_init(enc);1133enc->layer_select(enc);1134enc->rc_per_pic(enc);1135enc->op_init_rc(enc);1136enc->op_init_rc_vbv(enc);1137*enc->p_task_size = (enc->total_task_size);1138}11391140static void radeon_enc_headers_h264(struct radeon_encoder *enc)1141{1142if (enc->enc_pic.is_idr) {1143enc->nalu_sps(enc);1144enc->nalu_pps(enc);1145}1146enc->slice_header(enc);1147enc->encode_params(enc);1148enc->encode_params_codec_spec(enc);1149}11501151static void radeon_enc_headers_hevc(struct radeon_encoder *enc)1152{1153enc->nalu_aud(enc);1154if (enc->enc_pic.is_idr) {1155enc->nalu_vps(enc);1156enc->nalu_pps(enc);1157enc->nalu_sps(enc);1158}1159enc->slice_header(enc);1160enc->encode_params(enc);1161}11621163static void encode(struct radeon_encoder *enc)1164{1165enc->session_info(enc);1166enc->total_task_size = 0;1167enc->task_info(enc, enc->need_feedback);11681169enc->encode_headers(enc);1170enc->ctx(enc);1171enc->bitstream(enc);1172enc->feedback(enc);1173enc->intra_refresh(enc);11741175enc->op_preset(enc);1176enc->op_enc(enc);1177*enc->p_task_size = (enc->total_task_size);1178}11791180static void destroy(struct radeon_encoder *enc)1181{1182enc->session_info(enc);1183enc->total_task_size = 0;1184enc->task_info(enc, enc->need_feedback);1185enc->op_close(enc);1186*enc->p_task_size = (enc->total_task_size);1187}11881189void radeon_enc_1_2_init(struct radeon_encoder *enc)1190{1191enc->begin = begin;1192enc->encode = encode;1193enc->destroy = destroy;1194enc->session_info = radeon_enc_session_info;1195enc->task_info = radeon_enc_task_info;1196enc->layer_control = radeon_enc_layer_control;1197enc->layer_select = radeon_enc_layer_select;1198enc->rc_session_init = radeon_enc_rc_session_init;1199enc->rc_layer_init = radeon_enc_rc_layer_init;1200enc->quality_params = radeon_enc_quality_params;1201enc->ctx = radeon_enc_ctx;1202enc->bitstream = radeon_enc_bitstream;1203enc->feedback = radeon_enc_feedback;1204enc->intra_refresh = radeon_enc_intra_refresh;1205enc->rc_per_pic = radeon_enc_rc_per_pic;1206enc->encode_params = radeon_enc_encode_params;1207enc->op_init = radeon_enc_op_init;1208enc->op_close = radeon_enc_op_close;1209enc->op_enc = radeon_enc_op_enc;1210enc->op_init_rc = radeon_enc_op_init_rc;1211enc->op_init_rc_vbv = radeon_enc_op_init_rc_vbv;1212enc->op_preset = radeon_enc_op_speed;12131214if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {1215enc->session_init = radeon_enc_session_init;1216enc->slice_control = radeon_enc_slice_control;1217enc->spec_misc = radeon_enc_spec_misc;1218enc->deblocking_filter = radeon_enc_deblocking_filter_h264;1219enc->nalu_sps = radeon_enc_nalu_sps;1220enc->nalu_pps = radeon_enc_nalu_pps;1221enc->slice_header = radeon_enc_slice_header;1222enc->encode_params = radeon_enc_encode_params;1223enc->encode_params_codec_spec = radeon_enc_encode_params_h264;1224enc->encode_headers = radeon_enc_headers_h264;1225} else if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC) {1226enc->session_init = radeon_enc_session_init_hevc;1227enc->slice_control = radeon_enc_slice_control_hevc;1228enc->spec_misc = radeon_enc_spec_misc_hevc;1229enc->deblocking_filter = radeon_enc_deblocking_filter_hevc;1230enc->nalu_sps = radeon_enc_nalu_sps_hevc;1231enc->nalu_pps = radeon_enc_nalu_pps_hevc;1232enc->nalu_vps = radeon_enc_nalu_vps;1233enc->nalu_aud = radeon_enc_nalu_aud_hevc;1234enc->slice_header = radeon_enc_slice_header_hevc;1235enc->encode_params = radeon_enc_encode_params_hevc;1236enc->encode_headers = radeon_enc_headers_hevc;1237}12381239enc->cmd.session_info = RENCODE_IB_PARAM_SESSION_INFO;1240enc->cmd.task_info = RENCODE_IB_PARAM_TASK_INFO;1241enc->cmd.session_init = RENCODE_IB_PARAM_SESSION_INIT;1242enc->cmd.layer_control = RENCODE_IB_PARAM_LAYER_CONTROL;1243enc->cmd.layer_select = RENCODE_IB_PARAM_LAYER_SELECT;1244enc->cmd.rc_session_init = RENCODE_IB_PARAM_RATE_CONTROL_SESSION_INIT;1245enc->cmd.rc_layer_init = RENCODE_IB_PARAM_RATE_CONTROL_LAYER_INIT;1246enc->cmd.rc_per_pic = RENCODE_IB_PARAM_RATE_CONTROL_PER_PICTURE;1247enc->cmd.quality_params = RENCODE_IB_PARAM_QUALITY_PARAMS;1248enc->cmd.nalu = RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU;1249enc->cmd.slice_header = RENCODE_IB_PARAM_SLICE_HEADER;1250enc->cmd.enc_params = RENCODE_IB_PARAM_ENCODE_PARAMS;1251enc->cmd.intra_refresh = RENCODE_IB_PARAM_INTRA_REFRESH;1252enc->cmd.ctx = RENCODE_IB_PARAM_ENCODE_CONTEXT_BUFFER;1253enc->cmd.bitstream = RENCODE_IB_PARAM_VIDEO_BITSTREAM_BUFFER;1254enc->cmd.feedback = RENCODE_IB_PARAM_FEEDBACK_BUFFER;1255enc->cmd.slice_control_hevc = RENCODE_HEVC_IB_PARAM_SLICE_CONTROL;1256enc->cmd.spec_misc_hevc = RENCODE_HEVC_IB_PARAM_SPEC_MISC;1257enc->cmd.deblocking_filter_hevc = RENCODE_HEVC_IB_PARAM_DEBLOCKING_FILTER;1258enc->cmd.slice_control_h264 = RENCODE_H264_IB_PARAM_SLICE_CONTROL;1259enc->cmd.spec_misc_h264 = RENCODE_H264_IB_PARAM_SPEC_MISC;1260enc->cmd.enc_params_h264 = RENCODE_H264_IB_PARAM_ENCODE_PARAMS;1261enc->cmd.deblocking_filter_h264 = RENCODE_H264_IB_PARAM_DEBLOCKING_FILTER;12621263enc->enc_pic.session_info.interface_version =1264((RENCODE_FW_INTERFACE_MAJOR_VERSION << RENCODE_IF_MAJOR_VERSION_SHIFT) |1265(RENCODE_FW_INTERFACE_MINOR_VERSION << RENCODE_IF_MINOR_VERSION_SHIFT));1266}126712681269