1 /**************************************************************************
2 *
3 * Copyright 2015 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 "pipe/p_video_codec.h"
29 #include "radeon_vce.h"
30 #include "radeon_video.h"
31 #include "radeonsi/si_pipe.h"
32 #include "util/u_memory.h"
33 #include "util/u_video.h"
34 #include "vl/vl_video_buffer.h"
35
36 #include <stdio.h>
37
get_rate_control_param(struct rvce_encoder * enc,struct pipe_h264_enc_picture_desc * pic)38 static void get_rate_control_param(struct rvce_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
39 {
40 enc->enc_pic.rc.rc_method = pic->rate_ctrl[0].rate_ctrl_method;
41 enc->enc_pic.rc.target_bitrate = pic->rate_ctrl[0].target_bitrate;
42 enc->enc_pic.rc.peak_bitrate = pic->rate_ctrl[0].peak_bitrate;
43 enc->enc_pic.rc.quant_i_frames = pic->quant_i_frames;
44 enc->enc_pic.rc.quant_p_frames = pic->quant_p_frames;
45 enc->enc_pic.rc.quant_b_frames = pic->quant_b_frames;
46 enc->enc_pic.rc.gop_size = pic->gop_size;
47 enc->enc_pic.rc.frame_rate_num = pic->rate_ctrl[0].frame_rate_num;
48 enc->enc_pic.rc.frame_rate_den = pic->rate_ctrl[0].frame_rate_den;
49 enc->enc_pic.rc.max_qp = 51;
50
51 /* For CBR mode, to guarantee bitrate of generated stream complies with
52 * target bitrate (e.g. no over +/-10%), vbv_buffer_size should be same
53 * as target bitrate.
54 */
55 if (enc->enc_pic.rc.rc_method == PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT) {
56 enc->enc_pic.rc.vbv_buffer_size = pic->rate_ctrl[0].target_bitrate;
57 } else {
58 enc->enc_pic.rc.vbv_buffer_size = pic->rate_ctrl[0].vbv_buffer_size;
59 }
60
61 enc->enc_pic.rc.vbv_buf_lv = pic->rate_ctrl[0].vbv_buf_lv;
62 enc->enc_pic.rc.fill_data_enable = pic->rate_ctrl[0].fill_data_enable;
63 enc->enc_pic.rc.enforce_hrd = pic->rate_ctrl[0].enforce_hrd;
64 enc->enc_pic.rc.target_bits_picture = pic->rate_ctrl[0].target_bits_picture;
65 enc->enc_pic.rc.peak_bits_picture_integer = pic->rate_ctrl[0].peak_bits_picture_integer;
66 enc->enc_pic.rc.peak_bits_picture_fraction = pic->rate_ctrl[0].peak_bits_picture_fraction;
67 }
68
get_motion_estimation_param(struct rvce_encoder * enc,struct pipe_h264_enc_picture_desc * pic)69 static void get_motion_estimation_param(struct rvce_encoder *enc,
70 struct pipe_h264_enc_picture_desc *pic)
71 {
72 enc->enc_pic.me.motion_est_quarter_pixel = pic->motion_est.motion_est_quarter_pixel;
73 enc->enc_pic.me.enc_disable_sub_mode = pic->motion_est.enc_disable_sub_mode;
74 enc->enc_pic.me.lsmvert = pic->motion_est.lsmvert;
75 enc->enc_pic.me.enc_en_ime_overw_dis_subm = pic->motion_est.enc_en_ime_overw_dis_subm;
76 enc->enc_pic.me.enc_ime_overw_dis_subm_no = pic->motion_est.enc_ime_overw_dis_subm_no;
77 enc->enc_pic.me.enc_ime2_search_range_x = pic->motion_est.enc_ime2_search_range_x;
78 enc->enc_pic.me.enc_ime2_search_range_y = pic->motion_est.enc_ime2_search_range_y;
79 enc->enc_pic.me.enc_ime_decimation_search = 0x00000001;
80 enc->enc_pic.me.motion_est_half_pixel = 0x00000001;
81 enc->enc_pic.me.enc_search_range_x = 0x00000010;
82 enc->enc_pic.me.enc_search_range_y = 0x00000010;
83 enc->enc_pic.me.enc_search1_range_x = 0x00000010;
84 enc->enc_pic.me.enc_search1_range_y = 0x00000010;
85 }
86
get_pic_control_param(struct rvce_encoder * enc,struct pipe_h264_enc_picture_desc * pic)87 static void get_pic_control_param(struct rvce_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
88 {
89 unsigned encNumMBsPerSlice;
90 encNumMBsPerSlice = align(enc->base.width, 16) / 16;
91 encNumMBsPerSlice *= align(enc->base.height, 16) / 16;
92 if (pic->pic_ctrl.enc_frame_cropping_flag) {
93 enc->enc_pic.pc.enc_crop_left_offset = pic->pic_ctrl.enc_frame_crop_left_offset;
94 enc->enc_pic.pc.enc_crop_right_offset = pic->pic_ctrl.enc_frame_crop_right_offset;
95 enc->enc_pic.pc.enc_crop_top_offset = pic->pic_ctrl.enc_frame_crop_top_offset;
96 enc->enc_pic.pc.enc_crop_bottom_offset = pic->pic_ctrl.enc_frame_crop_bottom_offset;
97 } else {
98 enc->enc_pic.pc.enc_crop_right_offset = (align(enc->base.width, 16) - enc->base.width) >> 1;
99 enc->enc_pic.pc.enc_crop_bottom_offset =
100 (align(enc->base.height, 16) - enc->base.height) >> 1;
101 }
102 enc->enc_pic.pc.enc_num_mbs_per_slice = encNumMBsPerSlice;
103 enc->enc_pic.pc.enc_b_pic_pattern = MAX2(enc->base.max_references, 1) - 1;
104 enc->enc_pic.pc.enc_number_of_reference_frames = MIN2(enc->base.max_references, 2);
105 enc->enc_pic.pc.enc_max_num_ref_frames = enc->base.max_references + 1;
106 enc->enc_pic.pc.enc_num_default_active_ref_l0 = 0x00000001;
107 enc->enc_pic.pc.enc_num_default_active_ref_l1 = 0x00000001;
108 enc->enc_pic.pc.enc_cabac_enable = pic->pic_ctrl.enc_cabac_enable;
109 enc->enc_pic.pc.enc_constraint_set_flags = pic->pic_ctrl.enc_constraint_set_flags;
110 }
111
get_task_info_param(struct rvce_encoder * enc)112 static void get_task_info_param(struct rvce_encoder *enc)
113 {
114 enc->enc_pic.ti.offset_of_next_task_info = 0xffffffff;
115 }
116
get_feedback_buffer_param(struct rvce_encoder * enc)117 static void get_feedback_buffer_param(struct rvce_encoder *enc)
118 {
119 enc->enc_pic.fb.feedback_ring_size = 0x00000001;
120 }
121
get_config_ext_param(struct rvce_encoder * enc)122 static void get_config_ext_param(struct rvce_encoder *enc)
123 {
124 enc->enc_pic.ce.enc_enable_perf_logging = 0x00000003;
125 }
126
get_vui_param(struct rvce_encoder * enc,struct pipe_h264_enc_picture_desc * pic)127 static void get_vui_param(struct rvce_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
128 {
129 enc->enc_pic.enable_vui = pic->enable_vui;
130 enc->enc_pic.vui.video_format = 0x00000005;
131 enc->enc_pic.vui.color_prim = 0x00000002;
132 enc->enc_pic.vui.transfer_char = 0x00000002;
133 enc->enc_pic.vui.matrix_coef = 0x00000002;
134 enc->enc_pic.vui.timing_info_present_flag = 0x00000001;
135 enc->enc_pic.vui.num_units_in_tick = pic->rate_ctrl[0].frame_rate_den;
136 enc->enc_pic.vui.time_scale = pic->rate_ctrl[0].frame_rate_num * 2;
137 enc->enc_pic.vui.fixed_frame_rate_flag = 0x00000001;
138 enc->enc_pic.vui.bit_rate_scale = 0x00000004;
139 enc->enc_pic.vui.cpb_size_scale = 0x00000006;
140 enc->enc_pic.vui.initial_cpb_removal_delay_length_minus1 = 0x00000017;
141 enc->enc_pic.vui.cpb_removal_delay_length_minus1 = 0x00000017;
142 enc->enc_pic.vui.dpb_output_delay_length_minus1 = 0x00000017;
143 enc->enc_pic.vui.time_offset_length = 0x00000018;
144 enc->enc_pic.vui.motion_vectors_over_pic_boundaries_flag = 0x00000001;
145 enc->enc_pic.vui.max_bytes_per_pic_denom = 0x00000002;
146 enc->enc_pic.vui.max_bits_per_mb_denom = 0x00000001;
147 enc->enc_pic.vui.log2_max_mv_length_hori = 0x00000010;
148 enc->enc_pic.vui.log2_max_mv_length_vert = 0x00000010;
149 enc->enc_pic.vui.num_reorder_frames = 0x00000003;
150 enc->enc_pic.vui.max_dec_frame_buffering = 0x00000003;
151 }
152
si_vce_52_get_param(struct rvce_encoder * enc,struct pipe_h264_enc_picture_desc * pic)153 void si_vce_52_get_param(struct rvce_encoder *enc, struct pipe_h264_enc_picture_desc *pic)
154 {
155 get_rate_control_param(enc, pic);
156 get_motion_estimation_param(enc, pic);
157 get_pic_control_param(enc, pic);
158 get_task_info_param(enc);
159 get_feedback_buffer_param(enc);
160 get_vui_param(enc, pic);
161 get_config_ext_param(enc);
162
163 enc->enc_pic.picture_type = pic->picture_type;
164 enc->enc_pic.frame_num = pic->frame_num;
165 enc->enc_pic.frame_num_cnt = pic->frame_num_cnt;
166 enc->enc_pic.p_remain = pic->p_remain;
167 enc->enc_pic.i_remain = pic->i_remain;
168 enc->enc_pic.gop_cnt = pic->gop_cnt;
169 enc->enc_pic.pic_order_cnt = pic->pic_order_cnt;
170 enc->enc_pic.ref_idx_l0 = pic->ref_idx_l0;
171 enc->enc_pic.ref_idx_l1 = pic->ref_idx_l1;
172 enc->enc_pic.not_referenced = pic->not_referenced;
173 if (enc->dual_inst)
174 enc->enc_pic.addrmode_arraymode_disrdo_distwoinstants = 0x00000201;
175 else
176 enc->enc_pic.addrmode_arraymode_disrdo_distwoinstants = 0x01000201;
177 enc->enc_pic.is_idr = (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR);
178 }
179
create(struct rvce_encoder * enc)180 static void create(struct rvce_encoder *enc)
181 {
182 struct si_screen *sscreen = (struct si_screen *)enc->screen;
183 enc->task_info(enc, 0x00000000, 0, 0, 0);
184
185 RVCE_BEGIN(0x01000001); // create cmd
186 RVCE_CS(enc->enc_pic.ec.enc_use_circular_buffer);
187 RVCE_CS(u_get_h264_profile_idc(enc->base.profile)); // encProfile
188 RVCE_CS(enc->base.level); // encLevel
189 RVCE_CS(enc->enc_pic.ec.enc_pic_struct_restriction);
190 RVCE_CS(enc->base.width); // encImageWidth
191 RVCE_CS(enc->base.height); // encImageHeight
192
193 if (sscreen->info.chip_class < GFX9) {
194 RVCE_CS(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe); // encRefPicLumaPitch
195 RVCE_CS(enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe); // encRefPicChromaPitch
196 RVCE_CS(align(enc->luma->u.legacy.level[0].nblk_y, 16) / 8); // encRefYHeightInQw
197 } else {
198 RVCE_CS(enc->luma->u.gfx9.surf_pitch * enc->luma->bpe); // encRefPicLumaPitch
199 RVCE_CS(enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe); // encRefPicChromaPitch
200 RVCE_CS(align(enc->luma->u.gfx9.surf_height, 16) / 8); // encRefYHeightInQw
201 }
202
203 RVCE_CS(enc->enc_pic.addrmode_arraymode_disrdo_distwoinstants);
204
205 RVCE_CS(enc->enc_pic.ec.enc_pre_encode_context_buffer_offset);
206 RVCE_CS(enc->enc_pic.ec.enc_pre_encode_input_luma_buffer_offset);
207 RVCE_CS(enc->enc_pic.ec.enc_pre_encode_input_chroma_buffer_offset);
208 RVCE_CS(enc->enc_pic.ec.enc_pre_encode_mode_chromaflag_vbaqmode_scenechangesensitivity);
209 RVCE_END();
210 }
211
encode(struct rvce_encoder * enc)212 static void encode(struct rvce_encoder *enc)
213 {
214 struct si_screen *sscreen = (struct si_screen *)enc->screen;
215 signed luma_offset, chroma_offset, bs_offset;
216 unsigned dep, bs_idx = enc->bs_idx++;
217 int i;
218
219 if (enc->dual_inst) {
220 if (bs_idx == 0)
221 dep = 1;
222 else if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
223 dep = 0;
224 else
225 dep = 2;
226 } else
227 dep = 0;
228
229 enc->task_info(enc, 0x00000003, dep, 0, bs_idx);
230
231 RVCE_BEGIN(0x05000001); // context buffer
232 RVCE_READWRITE(enc->cpb.res->buf, enc->cpb.res->domains, 0); // encodeContextAddressHi/Lo
233 RVCE_END();
234
235 bs_offset = -(signed)(bs_idx * enc->bs_size);
236
237 RVCE_BEGIN(0x05000004); // video bitstream buffer
238 RVCE_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT, bs_offset); // videoBitstreamRingAddressHi/Lo
239 RVCE_CS(enc->bs_size); // videoBitstreamRingSize
240 RVCE_END();
241
242 if (enc->dual_pipe) {
243 unsigned aux_offset =
244 enc->cpb.res->buf->size - RVCE_MAX_AUX_BUFFER_NUM * RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE * 2;
245 RVCE_BEGIN(0x05000002); // auxiliary buffer
246 for (i = 0; i < 8; ++i) {
247 RVCE_CS(aux_offset);
248 aux_offset += RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE;
249 }
250 for (i = 0; i < 8; ++i)
251 RVCE_CS(RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE);
252 RVCE_END();
253 }
254
255 RVCE_BEGIN(0x03000001); // encode
256 RVCE_CS(enc->enc_pic.frame_num ? 0x0 : 0x11); // insertHeaders
257 RVCE_CS(enc->enc_pic.eo.picture_structure);
258 RVCE_CS(enc->bs_size); // allowedMaxBitstreamSize
259 RVCE_CS(enc->enc_pic.eo.force_refresh_map);
260 RVCE_CS(enc->enc_pic.eo.insert_aud);
261 RVCE_CS(enc->enc_pic.eo.end_of_sequence);
262 RVCE_CS(enc->enc_pic.eo.end_of_stream);
263
264 if (sscreen->info.chip_class < GFX9) {
265 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM,
266 (uint64_t)enc->luma->u.legacy.level[0].offset_256B * 256); // inputPictureLumaAddressHi/Lo
267 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM,
268 (uint64_t)enc->chroma->u.legacy.level[0].offset_256B * 256); // inputPictureChromaAddressHi/Lo
269 RVCE_CS(align(enc->luma->u.legacy.level[0].nblk_y, 16)); // encInputFrameYPitch
270 RVCE_CS(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe); // encInputPicLumaPitch
271 RVCE_CS(enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe); // encInputPicChromaPitch
272 } else {
273 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM,
274 enc->luma->u.gfx9.surf_offset); // inputPictureLumaAddressHi/Lo
275 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM,
276 enc->chroma->u.gfx9.surf_offset); // inputPictureChromaAddressHi/Lo
277 RVCE_CS(align(enc->luma->u.gfx9.surf_height, 16)); // encInputFrameYPitch
278 RVCE_CS(enc->luma->u.gfx9.surf_pitch * enc->luma->bpe); // encInputPicLumaPitch
279 RVCE_CS(enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe); // encInputPicChromaPitch
280 }
281
282 if (enc->dual_pipe)
283 enc->enc_pic.eo.enc_input_pic_addr_array_disable2pipe_disablemboffload = 0x00000000;
284 else
285 enc->enc_pic.eo.enc_input_pic_addr_array_disable2pipe_disablemboffload = 0x00010000;
286 RVCE_CS(enc->enc_pic.eo.enc_input_pic_addr_array_disable2pipe_disablemboffload);
287 RVCE_CS(enc->enc_pic.eo.enc_input_pic_tile_config);
288 RVCE_CS(enc->enc_pic.picture_type); // encPicType
289 RVCE_CS(enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR); // encIdrFlag
290 if ((enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR) &&
291 (enc->enc_pic.eo.enc_idr_pic_id != 0))
292 enc->enc_pic.eo.enc_idr_pic_id = enc->enc_pic.idr_pic_id - 1;
293 else
294 enc->enc_pic.eo.enc_idr_pic_id = 0x00000000;
295 RVCE_CS(enc->enc_pic.eo.enc_idr_pic_id);
296 RVCE_CS(enc->enc_pic.eo.enc_mgs_key_pic);
297 RVCE_CS(!enc->enc_pic.not_referenced);
298 RVCE_CS(enc->enc_pic.eo.enc_temporal_layer_index);
299 RVCE_CS(enc->enc_pic.eo.num_ref_idx_active_override_flag);
300 RVCE_CS(enc->enc_pic.eo.num_ref_idx_l0_active_minus1);
301 RVCE_CS(enc->enc_pic.eo.num_ref_idx_l1_active_minus1);
302
303 i = enc->enc_pic.frame_num - enc->enc_pic.ref_idx_l0;
304 if (i > 1 && enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P) {
305 enc->enc_pic.eo.enc_ref_list_modification_op = 0x00000001;
306 enc->enc_pic.eo.enc_ref_list_modification_num = i - 1;
307 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_op);
308 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_num);
309 } else {
310 enc->enc_pic.eo.enc_ref_list_modification_op = 0x00000000;
311 enc->enc_pic.eo.enc_ref_list_modification_num = 0x00000000;
312 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_op);
313 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_num);
314 }
315
316 for (i = 0; i < 3; ++i) {
317 enc->enc_pic.eo.enc_ref_list_modification_op = 0x00000000;
318 enc->enc_pic.eo.enc_ref_list_modification_num = 0x00000000;
319 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_op);
320 RVCE_CS(enc->enc_pic.eo.enc_ref_list_modification_num);
321 }
322 for (i = 0; i < 4; ++i) {
323 RVCE_CS(enc->enc_pic.eo.enc_decoded_picture_marking_op);
324 RVCE_CS(enc->enc_pic.eo.enc_decoded_picture_marking_num);
325 RVCE_CS(enc->enc_pic.eo.enc_decoded_picture_marking_idx);
326 RVCE_CS(enc->enc_pic.eo.enc_decoded_ref_base_picture_marking_op);
327 RVCE_CS(enc->enc_pic.eo.enc_decoded_ref_base_picture_marking_num);
328 }
329
330 // encReferencePictureL0[0]
331 RVCE_CS(0x00000000); // pictureStructure
332 if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P ||
333 enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B) {
334 struct rvce_cpb_slot *l0 = si_l0_slot(enc);
335 si_vce_frame_offset(enc, l0, &luma_offset, &chroma_offset);
336 RVCE_CS(l0->picture_type);
337 RVCE_CS(l0->frame_num);
338 RVCE_CS(l0->pic_order_cnt);
339 RVCE_CS(luma_offset);
340 RVCE_CS(chroma_offset);
341 } else {
342 enc->enc_pic.eo.l0_enc_pic_type = 0x00000000;
343 enc->enc_pic.eo.l0_frame_number = 0x00000000;
344 enc->enc_pic.eo.l0_picture_order_count = 0x00000000;
345 enc->enc_pic.eo.l0_luma_offset = 0xffffffff;
346 enc->enc_pic.eo.l0_chroma_offset = 0xffffffff;
347 RVCE_CS(enc->enc_pic.eo.l0_enc_pic_type);
348 RVCE_CS(enc->enc_pic.eo.l0_frame_number);
349 RVCE_CS(enc->enc_pic.eo.l0_picture_order_count);
350 RVCE_CS(enc->enc_pic.eo.l0_luma_offset);
351 RVCE_CS(enc->enc_pic.eo.l0_chroma_offset);
352 }
353
354 // encReferencePictureL0[1]
355 enc->enc_pic.eo.l0_picture_structure = 0x00000000;
356 enc->enc_pic.eo.l0_enc_pic_type = 0x00000000;
357 enc->enc_pic.eo.l0_frame_number = 0x00000000;
358 enc->enc_pic.eo.l0_picture_order_count = 0x00000000;
359 enc->enc_pic.eo.l0_luma_offset = 0xffffffff;
360 enc->enc_pic.eo.l0_chroma_offset = 0xffffffff;
361 RVCE_CS(enc->enc_pic.eo.l0_picture_structure);
362 RVCE_CS(enc->enc_pic.eo.l0_enc_pic_type);
363 RVCE_CS(enc->enc_pic.eo.l0_frame_number);
364 RVCE_CS(enc->enc_pic.eo.l0_picture_order_count);
365 RVCE_CS(enc->enc_pic.eo.l0_luma_offset);
366 RVCE_CS(enc->enc_pic.eo.l0_chroma_offset);
367
368 // encReferencePictureL1[0]
369 RVCE_CS(0x00000000); // pictureStructure
370 if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B) {
371 struct rvce_cpb_slot *l1 = si_l1_slot(enc);
372 si_vce_frame_offset(enc, l1, &luma_offset, &chroma_offset);
373 RVCE_CS(l1->picture_type);
374 RVCE_CS(l1->frame_num);
375 RVCE_CS(l1->pic_order_cnt);
376 RVCE_CS(luma_offset);
377 RVCE_CS(chroma_offset);
378 } else {
379 enc->enc_pic.eo.l1_enc_pic_type = 0x00000000;
380 enc->enc_pic.eo.l1_frame_number = 0x00000000;
381 enc->enc_pic.eo.l1_picture_order_count = 0x00000000;
382 enc->enc_pic.eo.l1_luma_offset = 0xffffffff;
383 enc->enc_pic.eo.l1_chroma_offset = 0xffffffff;
384 RVCE_CS(enc->enc_pic.eo.l1_enc_pic_type);
385 RVCE_CS(enc->enc_pic.eo.l1_frame_number);
386 RVCE_CS(enc->enc_pic.eo.l1_picture_order_count);
387 RVCE_CS(enc->enc_pic.eo.l1_luma_offset);
388 RVCE_CS(enc->enc_pic.eo.l1_chroma_offset);
389 }
390
391 si_vce_frame_offset(enc, si_current_slot(enc), &luma_offset, &chroma_offset);
392 RVCE_CS(luma_offset);
393 RVCE_CS(chroma_offset);
394 RVCE_CS(enc->enc_pic.eo.enc_coloc_buffer_offset);
395 RVCE_CS(enc->enc_pic.eo.enc_reconstructed_ref_base_picture_luma_offset);
396 RVCE_CS(enc->enc_pic.eo.enc_reconstructed_ref_base_picture_chroma_offset);
397 RVCE_CS(enc->enc_pic.eo.enc_reference_ref_base_picture_luma_offset);
398 RVCE_CS(enc->enc_pic.eo.enc_reference_ref_base_picture_chroma_offset);
399 RVCE_CS(enc->enc_pic.frame_num_cnt - 1);
400 RVCE_CS(enc->enc_pic.frame_num);
401 RVCE_CS(enc->enc_pic.pic_order_cnt);
402 RVCE_CS(enc->enc_pic.i_remain);
403 RVCE_CS(enc->enc_pic.p_remain);
404 RVCE_CS(enc->enc_pic.eo.num_b_pic_remain_in_rcgop);
405 RVCE_CS(enc->enc_pic.eo.num_ir_pic_remain_in_rcgop);
406 RVCE_CS(enc->enc_pic.eo.enable_intra_refresh);
407
408 RVCE_CS(enc->enc_pic.eo.aq_variance_en);
409 RVCE_CS(enc->enc_pic.eo.aq_block_size);
410 RVCE_CS(enc->enc_pic.eo.aq_mb_variance_sel);
411 RVCE_CS(enc->enc_pic.eo.aq_frame_variance_sel);
412 RVCE_CS(enc->enc_pic.eo.aq_param_a);
413 RVCE_CS(enc->enc_pic.eo.aq_param_b);
414 RVCE_CS(enc->enc_pic.eo.aq_param_c);
415 RVCE_CS(enc->enc_pic.eo.aq_param_d);
416 RVCE_CS(enc->enc_pic.eo.aq_param_e);
417
418 RVCE_CS(enc->enc_pic.eo.context_in_sfb);
419 RVCE_END();
420 }
421
rate_control(struct rvce_encoder * enc)422 static void rate_control(struct rvce_encoder *enc)
423 {
424 RVCE_BEGIN(0x04000005); // rate control
425 RVCE_CS(enc->enc_pic.rc.rc_method);
426 RVCE_CS(enc->enc_pic.rc.target_bitrate);
427 RVCE_CS(enc->enc_pic.rc.peak_bitrate);
428 RVCE_CS(enc->enc_pic.rc.frame_rate_num);
429 RVCE_CS(enc->enc_pic.rc.gop_size);
430 RVCE_CS(enc->enc_pic.rc.quant_i_frames);
431 RVCE_CS(enc->enc_pic.rc.quant_p_frames);
432 RVCE_CS(enc->enc_pic.rc.quant_b_frames);
433 RVCE_CS(enc->enc_pic.rc.vbv_buffer_size);
434 RVCE_CS(enc->enc_pic.rc.frame_rate_den);
435 RVCE_CS(enc->enc_pic.rc.vbv_buf_lv);
436 RVCE_CS(enc->enc_pic.rc.max_au_size);
437 RVCE_CS(enc->enc_pic.rc.qp_initial_mode);
438 RVCE_CS(enc->enc_pic.rc.target_bits_picture);
439 RVCE_CS(enc->enc_pic.rc.peak_bits_picture_integer);
440 RVCE_CS(enc->enc_pic.rc.peak_bits_picture_fraction);
441 RVCE_CS(enc->enc_pic.rc.min_qp);
442 RVCE_CS(enc->enc_pic.rc.max_qp);
443 RVCE_CS(enc->enc_pic.rc.skip_frame_enable);
444 RVCE_CS(enc->enc_pic.rc.fill_data_enable);
445 RVCE_CS(enc->enc_pic.rc.enforce_hrd);
446 RVCE_CS(enc->enc_pic.rc.b_pics_delta_qp);
447 RVCE_CS(enc->enc_pic.rc.ref_b_pics_delta_qp);
448 RVCE_CS(enc->enc_pic.rc.rc_reinit_disable);
449 RVCE_CS(enc->enc_pic.rc.enc_lcvbr_init_qp_flag);
450 RVCE_CS(enc->enc_pic.rc.lcvbrsatd_based_nonlinear_bit_budget_flag);
451 RVCE_END();
452 }
453
config(struct rvce_encoder * enc)454 static void config(struct rvce_encoder *enc)
455 {
456 enc->task_info(enc, 0x00000002, 0, 0xffffffff, 0);
457 enc->rate_control(enc);
458 enc->config_extension(enc);
459 enc->motion_estimation(enc);
460 enc->rdo(enc);
461 if (enc->use_vui)
462 enc->vui(enc);
463 enc->pic_control(enc);
464 }
465
config_extension(struct rvce_encoder * enc)466 static void config_extension(struct rvce_encoder *enc)
467 {
468 RVCE_BEGIN(0x04000001); // config extension
469 RVCE_CS(enc->enc_pic.ce.enc_enable_perf_logging);
470 RVCE_END();
471 }
472
feedback(struct rvce_encoder * enc)473 static void feedback(struct rvce_encoder *enc)
474 {
475 RVCE_BEGIN(0x05000005); // feedback buffer
476 RVCE_WRITE(enc->fb->res->buf, enc->fb->res->domains, 0x0); // feedbackRingAddressHi/Lo
477 RVCE_CS(enc->enc_pic.fb.feedback_ring_size);
478 RVCE_END();
479 }
480
destroy(struct rvce_encoder * enc)481 static void destroy(struct rvce_encoder *enc)
482 {
483 enc->task_info(enc, 0x00000001, 0, 0, 0);
484
485 feedback(enc);
486
487 RVCE_BEGIN(0x02000001); // destroy
488 RVCE_END();
489 }
490
motion_estimation(struct rvce_encoder * enc)491 static void motion_estimation(struct rvce_encoder *enc)
492 {
493 RVCE_BEGIN(0x04000007); // motion estimation
494 RVCE_CS(enc->enc_pic.me.enc_ime_decimation_search);
495 RVCE_CS(enc->enc_pic.me.motion_est_half_pixel);
496 RVCE_CS(enc->enc_pic.me.motion_est_quarter_pixel);
497 RVCE_CS(enc->enc_pic.me.disable_favor_pmv_point);
498 RVCE_CS(enc->enc_pic.me.force_zero_point_center);
499 RVCE_CS(enc->enc_pic.me.lsmvert);
500 RVCE_CS(enc->enc_pic.me.enc_search_range_x);
501 RVCE_CS(enc->enc_pic.me.enc_search_range_y);
502 RVCE_CS(enc->enc_pic.me.enc_search1_range_x);
503 RVCE_CS(enc->enc_pic.me.enc_search1_range_y);
504 RVCE_CS(enc->enc_pic.me.disable_16x16_frame1);
505 RVCE_CS(enc->enc_pic.me.disable_satd);
506 RVCE_CS(enc->enc_pic.me.enable_amd);
507 RVCE_CS(enc->enc_pic.me.enc_disable_sub_mode);
508 RVCE_CS(enc->enc_pic.me.enc_ime_skip_x);
509 RVCE_CS(enc->enc_pic.me.enc_ime_skip_y);
510 RVCE_CS(enc->enc_pic.me.enc_en_ime_overw_dis_subm);
511 RVCE_CS(enc->enc_pic.me.enc_ime_overw_dis_subm_no);
512 RVCE_CS(enc->enc_pic.me.enc_ime2_search_range_x);
513 RVCE_CS(enc->enc_pic.me.enc_ime2_search_range_y);
514 RVCE_CS(enc->enc_pic.me.parallel_mode_speedup_enable);
515 RVCE_CS(enc->enc_pic.me.fme0_enc_disable_sub_mode);
516 RVCE_CS(enc->enc_pic.me.fme1_enc_disable_sub_mode);
517 RVCE_CS(enc->enc_pic.me.ime_sw_speedup_enable);
518 RVCE_END();
519 }
520
pic_control(struct rvce_encoder * enc)521 static void pic_control(struct rvce_encoder *enc)
522 {
523 RVCE_BEGIN(0x04000002); // pic control
524 RVCE_CS(enc->enc_pic.pc.enc_use_constrained_intra_pred);
525 RVCE_CS(enc->enc_pic.pc.enc_cabac_enable);
526 RVCE_CS(enc->enc_pic.pc.enc_cabac_idc);
527 RVCE_CS(enc->enc_pic.pc.enc_loop_filter_disable);
528 RVCE_CS(enc->enc_pic.pc.enc_lf_beta_offset);
529 RVCE_CS(enc->enc_pic.pc.enc_lf_alpha_c0_offset);
530 RVCE_CS(enc->enc_pic.pc.enc_crop_left_offset);
531 RVCE_CS(enc->enc_pic.pc.enc_crop_right_offset);
532 RVCE_CS(enc->enc_pic.pc.enc_crop_top_offset);
533 RVCE_CS(enc->enc_pic.pc.enc_crop_bottom_offset);
534 RVCE_CS(enc->enc_pic.pc.enc_num_mbs_per_slice);
535 RVCE_CS(enc->enc_pic.pc.enc_intra_refresh_num_mbs_per_slot);
536 RVCE_CS(enc->enc_pic.pc.enc_force_intra_refresh);
537 RVCE_CS(enc->enc_pic.pc.enc_force_imb_period);
538 RVCE_CS(enc->enc_pic.pc.enc_pic_order_cnt_type);
539 RVCE_CS(enc->enc_pic.pc.log2_max_pic_order_cnt_lsb_minus4);
540 RVCE_CS(enc->enc_pic.pc.enc_sps_id);
541 RVCE_CS(enc->enc_pic.pc.enc_pps_id);
542 RVCE_CS(enc->enc_pic.pc.enc_constraint_set_flags);
543 RVCE_CS(enc->enc_pic.pc.enc_b_pic_pattern);
544 RVCE_CS(enc->enc_pic.pc.weight_pred_mode_b_picture);
545 RVCE_CS(enc->enc_pic.pc.enc_number_of_reference_frames);
546 RVCE_CS(enc->enc_pic.pc.enc_max_num_ref_frames);
547 RVCE_CS(enc->enc_pic.pc.enc_num_default_active_ref_l0);
548 RVCE_CS(enc->enc_pic.pc.enc_num_default_active_ref_l1);
549 RVCE_CS(enc->enc_pic.pc.enc_slice_mode);
550 RVCE_CS(enc->enc_pic.pc.enc_max_slice_size);
551 RVCE_END();
552 }
553
rdo(struct rvce_encoder * enc)554 static void rdo(struct rvce_encoder *enc)
555 {
556 RVCE_BEGIN(0x04000008); // rdo
557 RVCE_CS(enc->enc_pic.rdo.enc_disable_tbe_pred_i_frame);
558 RVCE_CS(enc->enc_pic.rdo.enc_disable_tbe_pred_p_frame);
559 RVCE_CS(enc->enc_pic.rdo.use_fme_interpol_y);
560 RVCE_CS(enc->enc_pic.rdo.use_fme_interpol_uv);
561 RVCE_CS(enc->enc_pic.rdo.use_fme_intrapol_y);
562 RVCE_CS(enc->enc_pic.rdo.use_fme_intrapol_uv);
563 RVCE_CS(enc->enc_pic.rdo.use_fme_interpol_y_1);
564 RVCE_CS(enc->enc_pic.rdo.use_fme_interpol_uv_1);
565 RVCE_CS(enc->enc_pic.rdo.use_fme_intrapol_y_1);
566 RVCE_CS(enc->enc_pic.rdo.use_fme_intrapol_uv_1);
567 RVCE_CS(enc->enc_pic.rdo.enc_16x16_cost_adj);
568 RVCE_CS(enc->enc_pic.rdo.enc_skip_cost_adj);
569 RVCE_CS(enc->enc_pic.rdo.enc_force_16x16_skip);
570 RVCE_CS(enc->enc_pic.rdo.enc_disable_threshold_calc_a);
571 RVCE_CS(enc->enc_pic.rdo.enc_luma_coeff_cost);
572 RVCE_CS(enc->enc_pic.rdo.enc_luma_mb_coeff_cost);
573 RVCE_CS(enc->enc_pic.rdo.enc_chroma_coeff_cost);
574 RVCE_END();
575 }
576
session(struct rvce_encoder * enc)577 static void session(struct rvce_encoder *enc)
578 {
579 RVCE_BEGIN(0x00000001); // session cmd
580 RVCE_CS(enc->stream_handle);
581 RVCE_END();
582 }
583
task_info(struct rvce_encoder * enc,uint32_t op,uint32_t dep,uint32_t fb_idx,uint32_t ring_idx)584 static void task_info(struct rvce_encoder *enc, uint32_t op, uint32_t dep, uint32_t fb_idx,
585 uint32_t ring_idx)
586 {
587 RVCE_BEGIN(0x00000002); // task info
588 if (op == 0x3) {
589 if (enc->task_info_idx) {
590 uint32_t offs = enc->cs.current.cdw - enc->task_info_idx + 3;
591 // Update offsetOfNextTaskInfo
592 enc->cs.current.buf[enc->task_info_idx] = offs;
593 }
594 enc->task_info_idx = enc->cs.current.cdw;
595 }
596 enc->enc_pic.ti.task_operation = op;
597 enc->enc_pic.ti.reference_picture_dependency = dep;
598 enc->enc_pic.ti.feedback_index = fb_idx;
599 enc->enc_pic.ti.video_bitstream_ring_index = ring_idx;
600 RVCE_CS(enc->enc_pic.ti.offset_of_next_task_info);
601 RVCE_CS(enc->enc_pic.ti.task_operation);
602 RVCE_CS(enc->enc_pic.ti.reference_picture_dependency);
603 RVCE_CS(enc->enc_pic.ti.collocate_flag_dependency);
604 RVCE_CS(enc->enc_pic.ti.feedback_index);
605 RVCE_CS(enc->enc_pic.ti.video_bitstream_ring_index);
606 RVCE_END();
607 }
608
vui(struct rvce_encoder * enc)609 static void vui(struct rvce_encoder *enc)
610 {
611 int i;
612
613 if (!enc->enc_pic.enable_vui)
614 return;
615
616 RVCE_BEGIN(0x04000009); // vui
617 RVCE_CS(enc->enc_pic.vui.aspect_ratio_info_present_flag);
618 RVCE_CS(enc->enc_pic.vui.aspect_ratio_idc);
619 RVCE_CS(enc->enc_pic.vui.sar_width);
620 RVCE_CS(enc->enc_pic.vui.sar_height);
621 RVCE_CS(enc->enc_pic.vui.overscan_info_present_flag);
622 RVCE_CS(enc->enc_pic.vui.overscan_Approp_flag);
623 RVCE_CS(enc->enc_pic.vui.video_signal_type_present_flag);
624 RVCE_CS(enc->enc_pic.vui.video_format);
625 RVCE_CS(enc->enc_pic.vui.video_full_range_flag);
626 RVCE_CS(enc->enc_pic.vui.color_description_present_flag);
627 RVCE_CS(enc->enc_pic.vui.color_prim);
628 RVCE_CS(enc->enc_pic.vui.transfer_char);
629 RVCE_CS(enc->enc_pic.vui.matrix_coef);
630 RVCE_CS(enc->enc_pic.vui.chroma_loc_info_present_flag);
631 RVCE_CS(enc->enc_pic.vui.chroma_loc_top);
632 RVCE_CS(enc->enc_pic.vui.chroma_loc_bottom);
633 RVCE_CS(enc->enc_pic.vui.timing_info_present_flag);
634 RVCE_CS(enc->enc_pic.vui.num_units_in_tick);
635 RVCE_CS(enc->enc_pic.vui.time_scale);
636 RVCE_CS(enc->enc_pic.vui.fixed_frame_rate_flag);
637 RVCE_CS(enc->enc_pic.vui.nal_hrd_parameters_present_flag);
638 RVCE_CS(enc->enc_pic.vui.cpb_cnt_minus1);
639 RVCE_CS(enc->enc_pic.vui.bit_rate_scale);
640 RVCE_CS(enc->enc_pic.vui.cpb_size_scale);
641 for (i = 0; i < 32; i++) {
642 RVCE_CS(enc->enc_pic.vui.bit_rate_value_minus);
643 RVCE_CS(enc->enc_pic.vui.cpb_size_value_minus);
644 RVCE_CS(enc->enc_pic.vui.cbr_flag);
645 }
646 RVCE_CS(enc->enc_pic.vui.initial_cpb_removal_delay_length_minus1);
647 RVCE_CS(enc->enc_pic.vui.cpb_removal_delay_length_minus1);
648 RVCE_CS(enc->enc_pic.vui.dpb_output_delay_length_minus1);
649 RVCE_CS(enc->enc_pic.vui.time_offset_length);
650 RVCE_CS(enc->enc_pic.vui.low_delay_hrd_flag);
651 RVCE_CS(enc->enc_pic.vui.pic_struct_present_flag);
652 RVCE_CS(enc->enc_pic.vui.bitstream_restriction_present_flag);
653 RVCE_CS(enc->enc_pic.vui.motion_vectors_over_pic_boundaries_flag);
654 RVCE_CS(enc->enc_pic.vui.max_bytes_per_pic_denom);
655 RVCE_CS(enc->enc_pic.vui.max_bits_per_mb_denom);
656 RVCE_CS(enc->enc_pic.vui.log2_max_mv_length_hori);
657 RVCE_CS(enc->enc_pic.vui.log2_max_mv_length_vert);
658 RVCE_CS(enc->enc_pic.vui.num_reorder_frames);
659 RVCE_CS(enc->enc_pic.vui.max_dec_frame_buffering);
660 RVCE_END();
661 }
662
si_vce_52_init(struct rvce_encoder * enc)663 void si_vce_52_init(struct rvce_encoder *enc)
664 {
665 enc->session = session;
666 enc->task_info = task_info;
667 enc->create = create;
668 enc->feedback = feedback;
669 enc->rate_control = rate_control;
670 enc->config_extension = config_extension;
671 enc->pic_control = pic_control;
672 enc->motion_estimation = motion_estimation;
673 enc->rdo = rdo;
674 enc->vui = vui;
675 enc->config = config;
676 enc->encode = encode;
677 enc->destroy = destroy;
678 enc->si_get_pic_param = si_vce_52_get_param;
679 }
680