1 /**************************************************************************
2 *
3 * Copyright 2020 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29
30 #include "pipe/p_video_codec.h"
31
32 #include "util/u_video.h"
33
34 #include "si_pipe.h"
35 #include "radeon_video.h"
36 #include "radeon_vcn_enc.h"
37
38 #define RENCODE_FW_INTERFACE_MAJOR_VERSION 1
39 #define RENCODE_FW_INTERFACE_MINOR_VERSION 0
40
radeon_enc_spec_misc(struct radeon_encoder * enc)41 static void radeon_enc_spec_misc(struct radeon_encoder *enc)
42 {
43 enc->enc_pic.spec_misc.constrained_intra_pred_flag = 0;
44 enc->enc_pic.spec_misc.half_pel_enabled = 1;
45 enc->enc_pic.spec_misc.quarter_pel_enabled = 1;
46 enc->enc_pic.spec_misc.level_idc = enc->base.level;
47 enc->enc_pic.spec_misc.b_picture_enabled = 0;
48 enc->enc_pic.spec_misc.weighted_bipred_idc = 0;
49
50 RADEON_ENC_BEGIN(enc->cmd.spec_misc_h264);
51 RADEON_ENC_CS(enc->enc_pic.spec_misc.constrained_intra_pred_flag);
52 RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_enable);
53 RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_init_idc);
54 RADEON_ENC_CS(enc->enc_pic.spec_misc.half_pel_enabled);
55 RADEON_ENC_CS(enc->enc_pic.spec_misc.quarter_pel_enabled);
56 RADEON_ENC_CS(enc->enc_pic.spec_misc.profile_idc);
57 RADEON_ENC_CS(enc->enc_pic.spec_misc.level_idc);
58 RADEON_ENC_CS(enc->enc_pic.spec_misc.b_picture_enabled);
59 RADEON_ENC_CS(enc->enc_pic.spec_misc.weighted_bipred_idc);
60 RADEON_ENC_END();
61 }
62
radeon_enc_quality_params(struct radeon_encoder * enc)63 static void radeon_enc_quality_params(struct radeon_encoder *enc)
64 {
65 enc->enc_pic.quality_params.vbaq_mode = 0;
66 enc->enc_pic.quality_params.scene_change_sensitivity = 0;
67 enc->enc_pic.quality_params.scene_change_min_idr_interval = 0;
68 enc->enc_pic.quality_params.two_pass_search_center_map_mode = 0;
69
70 RADEON_ENC_BEGIN(enc->cmd.quality_params);
71 RADEON_ENC_CS(enc->enc_pic.quality_params.vbaq_mode);
72 RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_sensitivity);
73 RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_min_idr_interval);
74 RADEON_ENC_CS(enc->enc_pic.quality_params.two_pass_search_center_map_mode);
75 RADEON_ENC_CS(0);
76 RADEON_ENC_END();
77 }
78
radeon_enc_encode_params_h264(struct radeon_encoder * enc)79 static void radeon_enc_encode_params_h264(struct radeon_encoder *enc)
80 {
81 enc->enc_pic.h264_enc_params.input_picture_structure = RENCODE_H264_PICTURE_STRUCTURE_FRAME;
82 enc->enc_pic.h264_enc_params.input_pic_order_cnt = 0;
83 enc->enc_pic.h264_enc_params.interlaced_mode = RENCODE_H264_INTERLACING_MODE_PROGRESSIVE;
84 enc->enc_pic.h264_enc_params.l0_reference_picture1_index = 0xFFFFFFFF;
85 enc->enc_pic.h264_enc_params.l1_reference_picture0_index= 0xFFFFFFFF;
86
87 RADEON_ENC_BEGIN(enc->cmd.enc_params_h264);
88 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.input_picture_structure);
89 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.input_pic_order_cnt);
90 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.interlaced_mode);
91 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.pic_type);
92 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.is_long_term);
93 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.picture_structure);
94 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture0.pic_order_cnt);
95 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.l0_reference_picture1_index);
96 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture1.pic_type);
97 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture1.is_long_term);
98 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture1.picture_structure);
99 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l0_reference_picture1.pic_order_cnt);
100 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.l1_reference_picture0_index);
101 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.pic_type);
102 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.is_long_term);
103 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.picture_structure);
104 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.picture_info_l1_reference_picture0.pic_order_cnt);
105 RADEON_ENC_END();
106 }
107
radeon_enc_nalu_pps_hevc(struct radeon_encoder * enc)108 static void radeon_enc_nalu_pps_hevc(struct radeon_encoder *enc)
109 {
110 uint32_t *size_in_bytes;
111
112 RADEON_ENC_BEGIN(enc->cmd.nalu);
113 RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS);
114 size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];
115
116 radeon_enc_reset(enc);
117 radeon_enc_set_emulation_prevention(enc, false);
118 radeon_enc_code_fixed_bits(enc, 0x00000001, 32);
119 radeon_enc_code_fixed_bits(enc, 0x4401, 16);
120 radeon_enc_byte_align(enc);
121 radeon_enc_set_emulation_prevention(enc, true);
122 radeon_enc_code_ue(enc, 0x0);
123 radeon_enc_code_ue(enc, 0x0);
124 radeon_enc_code_fixed_bits(enc, 0x1, 1);
125 radeon_enc_code_fixed_bits(enc, 0x0, 4);
126 radeon_enc_code_fixed_bits(enc, 0x0, 1);
127 radeon_enc_code_fixed_bits(enc, 0x1, 1);
128 radeon_enc_code_ue(enc, 0x0);
129 radeon_enc_code_ue(enc, 0x0);
130 radeon_enc_code_se(enc, 0x0);
131 radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag, 1);
132 radeon_enc_code_fixed_bits(enc, 0x1, 1);
133 if (enc->enc_pic.rc_session_init.rate_control_method ==
134 RENCODE_RATE_CONTROL_METHOD_NONE)
135 radeon_enc_code_fixed_bits(enc, 0x0, 1);
136 else {
137 radeon_enc_code_fixed_bits(enc, 0x1, 1);
138 radeon_enc_code_ue(enc, 0x0);
139 }
140 radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cb_qp_offset);
141 radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cr_qp_offset);
142 radeon_enc_code_fixed_bits(enc, 0x0, 1);
143 radeon_enc_code_fixed_bits(enc, 0x0, 2);
144 radeon_enc_code_fixed_bits(enc, 0x0, 1);
145 radeon_enc_code_fixed_bits(enc, 0x0, 1);
146 radeon_enc_code_fixed_bits(enc, 0x0, 1);
147 radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled, 1);
148 radeon_enc_code_fixed_bits(enc, 0x1, 1);
149 radeon_enc_code_fixed_bits(enc, 0x0, 1);
150 radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.deblocking_filter_disabled, 1);
151
152 if (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled) {
153 radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.beta_offset_div2);
154 radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.tc_offset_div2);
155 }
156
157 radeon_enc_code_fixed_bits(enc, 0x0, 1);
158 radeon_enc_code_fixed_bits(enc, 0x0, 1);
159 radeon_enc_code_ue(enc, enc->enc_pic.log2_parallel_merge_level_minus2);
160 radeon_enc_code_fixed_bits(enc, 0x0, 2);
161
162 radeon_enc_code_fixed_bits(enc, 0x1, 1);
163
164 radeon_enc_byte_align(enc);
165 radeon_enc_flush_headers(enc);
166 *size_in_bytes = (enc->bits_output + 7) / 8;
167 RADEON_ENC_END();
168 }
169
radeon_enc_3_0_init(struct radeon_encoder * enc)170 void radeon_enc_3_0_init(struct radeon_encoder *enc)
171 {
172 radeon_enc_2_0_init(enc);
173
174 if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
175 enc->spec_misc = radeon_enc_spec_misc;
176 enc->encode_params_codec_spec = radeon_enc_encode_params_h264;
177 enc->quality_params = radeon_enc_quality_params;
178 }
179
180 if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC)
181 enc->nalu_pps = radeon_enc_nalu_pps_hevc;
182
183 enc->enc_pic.session_info.interface_version =
184 ((RENCODE_FW_INTERFACE_MAJOR_VERSION << RENCODE_IF_MAJOR_VERSION_SHIFT) |
185 (RENCODE_FW_INTERFACE_MINOR_VERSION << RENCODE_IF_MINOR_VERSION_SHIFT));
186 }
187