1 /**************************************************************************
2 *
3 * Copyright 2024 Advanced Micro Devices, Inc.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 **************************************************************************/
8 #include "pipe/p_video_codec.h"
9
10 #include "util/u_video.h"
11
12 #include "si_pipe.h"
13 #include "radeon_vcn_enc.h"
14
15 #define RENCODE_FW_INTERFACE_MAJOR_VERSION 1
16 #define RENCODE_FW_INTERFACE_MINOR_VERSION 3
17
18 #define RENCODE_AV1_MIN_TILE_WIDTH 256
19
radeon_enc_cdf_default_table(struct radeon_encoder * enc)20 static void radeon_enc_cdf_default_table(struct radeon_encoder *enc)
21 {
22 bool use_cdf_default = enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_KEY ||
23 enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_INTRA_ONLY ||
24 enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_SWITCH ||
25 (enc->enc_pic.enable_error_resilient_mode);
26
27 enc->enc_pic.av1_cdf_default_table.use_cdf_default = use_cdf_default ? 1 : 0;
28
29 RADEON_ENC_BEGIN(enc->cmd.cdf_default_table_av1);
30 RADEON_ENC_CS(enc->enc_pic.av1_cdf_default_table.use_cdf_default);
31 RADEON_ENC_READWRITE(enc->cdf->res->buf, enc->cdf->res->domains, 0);
32 RADEON_ENC_END();
33 }
34
radeon_enc_spec_misc(struct radeon_encoder * enc)35 static void radeon_enc_spec_misc(struct radeon_encoder *enc)
36 {
37 RADEON_ENC_BEGIN(enc->cmd.spec_misc_h264);
38 RADEON_ENC_CS(enc->enc_pic.spec_misc.constrained_intra_pred_flag);
39 RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_enable);
40 RADEON_ENC_CS(enc->enc_pic.spec_misc.cabac_init_idc);
41 RADEON_ENC_CS(enc->enc_pic.spec_misc.transform_8x8_mode);
42 RADEON_ENC_CS(enc->enc_pic.spec_misc.half_pel_enabled);
43 RADEON_ENC_CS(enc->enc_pic.spec_misc.quarter_pel_enabled);
44 RADEON_ENC_CS(enc->enc_pic.spec_misc.profile_idc);
45 RADEON_ENC_CS(enc->enc_pic.spec_misc.level_idc);
46 RADEON_ENC_CS(enc->enc_pic.spec_misc.b_picture_enabled);
47 RADEON_ENC_CS(enc->enc_pic.spec_misc.weighted_bipred_idc);
48 RADEON_ENC_END();
49 }
50
radeon_enc_encode_params(struct radeon_encoder * enc)51 static void radeon_enc_encode_params(struct radeon_encoder *enc)
52 {
53
54 bool is_av1 = u_reduce_video_profile(enc->base.profile)
55 == PIPE_VIDEO_FORMAT_AV1;
56 if ( !is_av1 ) {
57 switch (enc->enc_pic.picture_type) {
58 case PIPE_H2645_ENC_PICTURE_TYPE_I:
59 case PIPE_H2645_ENC_PICTURE_TYPE_IDR:
60 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;
61 break;
62 case PIPE_H2645_ENC_PICTURE_TYPE_P:
63 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P;
64 break;
65 case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:
66 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P_SKIP;
67 break;
68 case PIPE_H2645_ENC_PICTURE_TYPE_B:
69 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_B;
70 break;
71 default:
72 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;
73 }
74 } else {
75 switch (enc->enc_pic.frame_type) {
76 case PIPE_AV1_ENC_FRAME_TYPE_KEY:
77 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;
78 break;
79 case PIPE_AV1_ENC_FRAME_TYPE_INTRA_ONLY:
80 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_I;
81 break;
82 case PIPE_AV1_ENC_FRAME_TYPE_INTER:
83 case PIPE_AV1_ENC_FRAME_TYPE_SWITCH:
84 if (enc->enc_pic.av1.compound)
85 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_B;
86 else
87 enc->enc_pic.enc_params.pic_type = RENCODE_PICTURE_TYPE_P;
88 break;
89 default:
90 assert(0); /* never come to this condition */
91 }
92 }
93
94 if (enc->luma->meta_offset)
95 RADEON_ENC_ERR("DCC surfaces not supported.\n");
96
97 enc->enc_pic.enc_params.input_pic_luma_pitch = enc->luma->u.gfx9.surf_pitch;
98 enc->enc_pic.enc_params.input_pic_chroma_pitch = enc->chroma ?
99 enc->chroma->u.gfx9.surf_pitch : enc->luma->u.gfx9.surf_pitch;
100 enc->enc_pic.enc_params.input_pic_swizzle_mode = enc->luma->u.gfx9.swizzle_mode;
101
102 RADEON_ENC_BEGIN(enc->cmd.enc_params);
103 RADEON_ENC_CS(enc->enc_pic.enc_params.pic_type);
104 RADEON_ENC_CS(enc->enc_pic.enc_params.allowed_max_bitstream_size);
105 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->luma->u.gfx9.surf_offset);
106 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM, enc->chroma ?
107 enc->chroma->u.gfx9.surf_offset : enc->luma->u.gfx9.surf_pitch);
108 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_luma_pitch);
109 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_chroma_pitch);
110 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_swizzle_mode);
111 RADEON_ENC_CS(enc->enc_pic.enc_params.reconstructed_picture_index);
112 RADEON_ENC_END();
113 }
114
radeon_enc_encode_params_h264(struct radeon_encoder * enc)115 static void radeon_enc_encode_params_h264(struct radeon_encoder *enc)
116 {
117 if (enc->enc_pic.enc_params.reference_picture_index != 0xFFFFFFFF){
118 enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list = 0;
119 enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list_index = 0;
120 enc->enc_pic.h264_enc_params.ref_list0[0] =
121 enc->enc_pic.enc_params.reference_picture_index;
122 enc->enc_pic.h264_enc_params.num_active_references_l0 = 1;
123 } else {
124 enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list = 0;
125 enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list_index = 0xFFFFFFFF;
126 enc->enc_pic.h264_enc_params.ref_list0[0] = 0xFFFFFFFF;
127 enc->enc_pic.h264_enc_params.num_active_references_l0 = 0;
128 }
129
130 if (enc->enc_pic.h264_enc_params.l1_reference_picture0_index != 0xFFFFFFFF) {
131 enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list = 1;
132 enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list_index = 0;
133 enc->enc_pic.h264_enc_params.ref_list1[0] =
134 enc->enc_pic.h264_enc_params.l1_reference_picture0_index;
135 enc->enc_pic.h264_enc_params.num_active_references_l1 = 1;
136 } else {
137 enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list = 0;
138 enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list_index = 0xFFFFFFFF;
139 enc->enc_pic.h264_enc_params.ref_list0[1] = 0;
140 enc->enc_pic.h264_enc_params.ref_list1[0] = 0;
141 enc->enc_pic.h264_enc_params.num_active_references_l1 = 0;
142 }
143
144 RADEON_ENC_BEGIN(enc->cmd.enc_params_h264);
145 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.input_picture_structure);
146 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.input_pic_order_cnt);
147 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.is_reference);
148 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.is_long_term);
149 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.interlaced_mode);
150 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.ref_list0[0]);
151 for (int i = 1; i < RENCODE_H264_MAX_REFERENCE_LIST_SIZE; i++)
152 RADEON_ENC_CS(0x00000000);
153 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.num_active_references_l0);
154 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.ref_list1[0]);
155 for (int i = 1; i < RENCODE_H264_MAX_REFERENCE_LIST_SIZE; i++)
156 RADEON_ENC_CS(0x00000000);
157 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.num_active_references_l1);
158 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list);
159 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.lsm_reference_pictures[0].list_index);
160 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list);
161 RADEON_ENC_CS(enc->enc_pic.h264_enc_params.lsm_reference_pictures[1].list_index);
162 RADEON_ENC_END();
163 }
164
radeon_enc_spec_misc_av1(struct radeon_encoder * enc)165 static void radeon_enc_spec_misc_av1(struct radeon_encoder *enc)
166 {
167 /* if enabled using the input parameters, it is required to have cdef_bits
168 * > 0 */
169 if (enc->enc_pic.av1_spec_misc.cdef_mode && !!(enc->enc_pic.av1_spec_misc.cdef_bits))
170 enc->enc_pic.av1_spec_misc.cdef_mode = RENCODE_AV1_CDEF_MODE_EXPLICIT;
171 else if (enc->enc_pic.av1_spec_misc.cdef_mode)
172 enc->enc_pic.av1_spec_misc.cdef_mode = RENCODE_AV1_CDEF_MODE_DEFAULT;
173
174 RADEON_ENC_BEGIN(enc->cmd.spec_misc_av1);
175 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.palette_mode_enable);
176 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.mv_precision);
177 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_mode);
178 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_bits);
179 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_damping_minus3);
180 for (int i = 0; i < RENCODE_AV1_CDEF_MAX_NUM; i++)
181 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_y_pri_strength[i]);
182 for (int i = 0; i < RENCODE_AV1_CDEF_MAX_NUM; i++)
183 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_y_sec_strength[i]);
184 for (int i = 0; i < RENCODE_AV1_CDEF_MAX_NUM; i++)
185 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_uv_pri_strength[i]);
186 for (int i = 0; i < RENCODE_AV1_CDEF_MAX_NUM; i++)
187 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.cdef_uv_sec_strength[i]);
188 RADEON_ENC_CS(0);
189 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.disable_cdf_update);
190 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.disable_frame_end_update_cdf);
191 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.disallow_skip_mode);
192 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.delta_q_y_dc);
193 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.delta_q_u_dc);
194 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.delta_q_u_ac);
195 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.delta_q_v_dc);
196 RADEON_ENC_CS(enc->enc_pic.av1_spec_misc.delta_q_v_ac);
197 RADEON_ENC_CS(0);
198 RADEON_ENC_CS(0);
199 RADEON_ENC_END();
200 }
201
radeon_enc_ref_swizzle_mode(struct radeon_encoder * enc)202 static uint32_t radeon_enc_ref_swizzle_mode(struct radeon_encoder *enc)
203 {
204 /* return RENCODE_REC_SWIZZLE_MODE_LINEAR; for debugging purpose */
205 return RENCODE_REC_SWIZZLE_MODE_256B_D_VCN5;
206 }
207
radeon_enc_ctx(struct radeon_encoder * enc)208 static void radeon_enc_ctx(struct radeon_encoder *enc)
209 {
210 int i;
211 uint32_t swizzle_mode = radeon_enc_ref_swizzle_mode(enc);
212 bool is_h264 = u_reduce_video_profile(enc->base.profile)
213 == PIPE_VIDEO_FORMAT_MPEG4_AVC;
214 bool is_av1 = u_reduce_video_profile(enc->base.profile)
215 == PIPE_VIDEO_FORMAT_AV1;
216
217 RADEON_ENC_BEGIN(enc->cmd.ctx);
218 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
219 RADEON_ENC_CS(enc->enc_pic.ctx_buf.num_reconstructed_pictures);
220
221 for (i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
222 rvcn_enc_reconstructed_picture_t *pic =
223 &enc->enc_pic.ctx_buf.reconstructed_pictures[i];
224 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
225 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);
226 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
227 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);
228 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
229 RADEON_ENC_CS(0);
230 RADEON_ENC_CS(swizzle_mode);
231 RADEON_ENC_READWRITE(enc->meta->res->buf, enc->meta->res->domains,
232 pic->frame_context_buffer_offset);
233 if (is_h264) {
234 RADEON_ENC_CS(pic->h264.colloc_buffer_offset);
235 RADEON_ENC_CS(0);
236 } else if (is_av1) {
237 RADEON_ENC_CS(pic->av1.av1_cdf_frame_context_offset);
238 RADEON_ENC_CS(pic->av1.av1_cdef_algorithm_context_offset);
239 } else {
240 RADEON_ENC_CS(0);
241 RADEON_ENC_CS(0);
242 }
243 RADEON_ENC_CS(pic->encode_metadata_offset);
244 }
245
246 for (int i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
247 rvcn_enc_reconstructed_picture_t *pic =
248 &enc->enc_pic.ctx_buf.pre_encode_reconstructed_pictures[i];
249 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
250 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);
251 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
252 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);
253 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
254 RADEON_ENC_CS(0);
255 RADEON_ENC_CS(swizzle_mode);
256 RADEON_ENC_READWRITE(enc->meta->res->buf, enc->meta->res->domains,
257 pic->frame_context_buffer_offset);
258 if (is_h264) {
259 RADEON_ENC_CS(pic->h264.colloc_buffer_offset);
260 RADEON_ENC_CS(0);
261 } else if (is_av1) {
262 RADEON_ENC_CS(pic->av1.av1_cdf_frame_context_offset);
263 RADEON_ENC_CS(pic->av1.av1_cdef_algorithm_context_offset);
264 } else {
265 RADEON_ENC_CS(0);
266 RADEON_ENC_CS(0);
267 }
268 RADEON_ENC_CS(pic->encode_metadata_offset);
269 }
270
271 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_picture_luma_pitch);
272 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_picture_chroma_pitch);
273 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.red_offset);
274 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.green_offset);
275 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.blue_offset);
276 RADEON_ENC_CS(enc->enc_pic.ctx_buf.av1.av1_sdb_intermediate_context_offset);
277 RADEON_ENC_END();
278 }
279
radeon_enc_ctx_tier2(struct radeon_encoder * enc)280 static void radeon_enc_ctx_tier2(struct radeon_encoder *enc)
281 {
282 uint32_t num_refs = 0;
283 bool is_h264 = u_reduce_video_profile(enc->base.profile)
284 == PIPE_VIDEO_FORMAT_MPEG4_AVC;
285 bool is_av1 = u_reduce_video_profile(enc->base.profile)
286 == PIPE_VIDEO_FORMAT_AV1;
287
288 for (uint32_t i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
289 if (enc->enc_pic.dpb_bufs[i]) {
290 radeon_enc_create_dpb_aux_buffers(enc, enc->enc_pic.dpb_bufs[i]);
291 num_refs = i + 1;
292 }
293 }
294
295 RADEON_ENC_BEGIN(enc->cmd.ctx);
296 if (enc->dpb->res) {
297 RADEON_ENC_READWRITE(enc->dpb->res->buf, enc->dpb->res->domains, 0);
298 } else {
299 RADEON_ENC_CS(0);
300 RADEON_ENC_CS(0);
301 }
302 RADEON_ENC_CS(num_refs);
303
304 for (uint32_t i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
305 if (!enc->enc_pic.dpb_bufs[i]) {
306 for (int j = 0; j < 15; j++)
307 RADEON_ENC_CS(0);
308 continue;
309 }
310 struct si_texture *luma = enc->enc_pic.dpb_bufs[i]->luma;
311 struct si_texture *chroma = enc->enc_pic.dpb_bufs[i]->chroma;
312 struct rvid_buffer *fcb = enc->enc_pic.dpb_bufs[i]->fcb;
313 RADEON_ENC_READWRITE(luma->buffer.buf, luma->buffer.domains, luma->surface.u.gfx9.surf_offset);
314 RADEON_ENC_CS(luma->surface.u.gfx9.surf_pitch);
315 RADEON_ENC_READWRITE(chroma->buffer.buf, chroma->buffer.domains, chroma->surface.u.gfx9.surf_offset);
316 RADEON_ENC_CS(chroma->surface.u.gfx9.surf_pitch);
317 RADEON_ENC_CS(0);
318 RADEON_ENC_CS(0);
319 RADEON_ENC_CS(0);
320 RADEON_ENC_CS(luma->surface.u.gfx9.swizzle_mode);
321 RADEON_ENC_READWRITE(fcb->res->buf, fcb->res->domains, 0);
322 if (is_h264) {
323 RADEON_ENC_CS(enc->enc_pic.fcb_offset.h264.colloc_buffer_offset);
324 RADEON_ENC_CS(0);
325 } else if (is_av1) {
326 RADEON_ENC_CS(enc->enc_pic.fcb_offset.av1.av1_cdf_frame_context_offset);
327 RADEON_ENC_CS(enc->enc_pic.fcb_offset.av1.av1_cdef_algorithm_context_offset);
328 } else {
329 RADEON_ENC_CS(0);
330 RADEON_ENC_CS(0);
331 }
332 RADEON_ENC_CS(0);
333 }
334
335 /* pre-encoding */
336 for (uint32_t i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
337 if (!enc->enc_pic.quality_modes.pre_encode_mode || !enc->enc_pic.dpb_bufs[i]) {
338 for (int j = 0; j < 15; j++)
339 RADEON_ENC_CS(0);
340 continue;
341 }
342 struct si_texture *luma = enc->enc_pic.dpb_bufs[i]->pre_luma;
343 struct si_texture *chroma = enc->enc_pic.dpb_bufs[i]->pre_chroma;
344 struct rvid_buffer *fcb = enc->enc_pic.dpb_bufs[i]->pre_fcb;
345 RADEON_ENC_READWRITE(luma->buffer.buf, luma->buffer.domains, luma->surface.u.gfx9.surf_offset);
346 RADEON_ENC_CS(luma->surface.u.gfx9.surf_pitch);
347 RADEON_ENC_READWRITE(chroma->buffer.buf, chroma->buffer.domains, chroma->surface.u.gfx9.surf_offset);
348 RADEON_ENC_CS(chroma->surface.u.gfx9.surf_pitch);
349 RADEON_ENC_CS(0);
350 RADEON_ENC_CS(0);
351 RADEON_ENC_CS(0);
352 RADEON_ENC_CS(luma->surface.u.gfx9.swizzle_mode);
353 RADEON_ENC_READWRITE(fcb->res->buf, fcb->res->domains, 0);
354 if (is_h264) {
355 RADEON_ENC_CS(enc->enc_pic.fcb_offset.h264.colloc_buffer_offset);
356 RADEON_ENC_CS(0);
357 } else if (is_av1) {
358 RADEON_ENC_CS(enc->enc_pic.fcb_offset.av1.av1_cdf_frame_context_offset);
359 RADEON_ENC_CS(enc->enc_pic.fcb_offset.av1.av1_cdef_algorithm_context_offset);
360 } else {
361 RADEON_ENC_CS(0);
362 RADEON_ENC_CS(0);
363 }
364 RADEON_ENC_CS(0);
365 }
366
367 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_picture_luma_pitch);
368 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_picture_chroma_pitch);
369 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.red_offset);
370 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.green_offset);
371 RADEON_ENC_CS(enc->enc_pic.ctx_buf.pre_encode_input_picture.rgb.blue_offset);
372 RADEON_ENC_CS(enc->enc_pic.ctx_buf.av1.av1_sdb_intermediate_context_offset);
373 RADEON_ENC_END();
374 }
375
radeon_enc_ctx_override(struct radeon_encoder * enc)376 static void radeon_enc_ctx_override(struct radeon_encoder *enc)
377 {
378 RADEON_ENC_BEGIN(enc->cmd.ctx_override);
379 for (int i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
380 rvcn_enc_reconstructed_picture_t *pic =
381 &enc->enc_pic.ctx_buf.reconstructed_pictures[i];
382 RADEON_ENC_CS(pic->luma_offset);
383 RADEON_ENC_CS(pic->chroma_offset);
384 RADEON_ENC_CS(pic->chroma_v_offset);
385 }
386 for (int i = 0; i < RENCODE_MAX_NUM_RECONSTRUCTED_PICTURES; i++) {
387 rvcn_enc_reconstructed_picture_t *pic =
388 &enc->enc_pic.ctx_buf.pre_encode_reconstructed_pictures[i];
389 RADEON_ENC_CS(pic->luma_offset);
390 RADEON_ENC_CS(pic->chroma_offset);
391 RADEON_ENC_CS(pic->chroma_v_offset);
392 }
393 RADEON_ENC_END();
394 }
395
radeon_enc_metadata(struct radeon_encoder * enc)396 static void radeon_enc_metadata(struct radeon_encoder *enc)
397 {
398 if (!enc->meta)
399 return;
400
401 enc->enc_pic.metadata.two_pass_search_center_map_offset =
402 enc->enc_pic.ctx_buf.two_pass_search_center_map_offset;
403 RADEON_ENC_BEGIN(enc->cmd.metadata);
404 RADEON_ENC_READWRITE(enc->meta->res->buf, enc->meta->res->domains, 0);
405 RADEON_ENC_CS(enc->enc_pic.metadata.two_pass_search_center_map_offset);
406 RADEON_ENC_END();
407 }
408
radeon_enc_output_format(struct radeon_encoder * enc)409 static void radeon_enc_output_format(struct radeon_encoder *enc)
410 {
411 enc->enc_pic.enc_output_format.output_chroma_subsampling = 0;
412
413 RADEON_ENC_BEGIN(enc->cmd.output_format);
414 RADEON_ENC_CS(enc->enc_pic.enc_output_format.output_color_volume);
415 RADEON_ENC_CS(enc->enc_pic.enc_output_format.output_color_range);
416 RADEON_ENC_CS(enc->enc_pic.enc_output_format.output_chroma_subsampling);
417 RADEON_ENC_CS(enc->enc_pic.enc_output_format.output_chroma_location);
418 RADEON_ENC_CS(enc->enc_pic.enc_output_format.output_color_bit_depth);
419 RADEON_ENC_END();
420 }
421
radeon_enc_rc_per_pic(struct radeon_encoder * enc)422 static void radeon_enc_rc_per_pic(struct radeon_encoder *enc)
423 {
424 RADEON_ENC_BEGIN(enc->cmd.rc_per_pic);
425 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp_i);
426 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp_p);
427 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp_b);
428 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_i);
429 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_i);
430 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_p);
431 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_p);
432 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_b);
433 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_b);
434 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size_i);
435 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size_p);
436 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size_b);
437 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enabled_filler_data);
438 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.skip_frame_enable);
439 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enforce_hrd);
440 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qvbr_quality_level);
441 RADEON_ENC_END();
442 }
443
radeon_enc_encode_params_hevc(struct radeon_encoder * enc)444 static void radeon_enc_encode_params_hevc(struct radeon_encoder *enc)
445 {
446 enc->enc_pic.hevc_enc_params.lsm_reference_pictures_list_index = 0;
447 enc->enc_pic.hevc_enc_params.ref_list0[0] =
448 enc->enc_pic.enc_params.reference_picture_index;
449 enc->enc_pic.hevc_enc_params.num_active_references_l0 =
450 (enc->enc_pic.enc_params.pic_type == RENCODE_PICTURE_TYPE_I) ? 0 : 1;
451
452 RADEON_ENC_BEGIN(enc->cmd.enc_params_hevc);
453 RADEON_ENC_CS(enc->enc_pic.hevc_enc_params.ref_list0[0]);
454 for (int i = 1; i < RENCODE_HEVC_MAX_REFERENCE_LIST_SIZE; i++)
455 RADEON_ENC_CS(0x00000000);
456 RADEON_ENC_CS(enc->enc_pic.hevc_enc_params.num_active_references_l0);
457 RADEON_ENC_CS(enc->enc_pic.hevc_enc_params.lsm_reference_pictures_list_index);
458 RADEON_ENC_END();
459 }
460
radeon_enc_encode_params_av1(struct radeon_encoder * enc)461 static void radeon_enc_encode_params_av1(struct radeon_encoder *enc)
462 {
463 RADEON_ENC_BEGIN(enc->cmd.enc_params_av1);
464 for (int i = 0; i < RENCODE_AV1_REFS_PER_FRAME; i++)
465 RADEON_ENC_CS(enc->enc_pic.av1_enc_params.ref_frames[i]);
466 RADEON_ENC_CS(enc->enc_pic.av1_enc_params.lsm_reference_frame_index[0]);
467 RADEON_ENC_CS(enc->enc_pic.av1_enc_params.lsm_reference_frame_index[1]);
468 RADEON_ENC_END();
469 }
470
radeon_enc_spec_misc_hevc(struct radeon_encoder * enc)471 static void radeon_enc_spec_misc_hevc(struct radeon_encoder *enc)
472 {
473 RADEON_ENC_BEGIN(enc->cmd.spec_misc_hevc);
474 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);
475 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.amp_disabled);
476 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled);
477 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag);
478 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.cabac_init_flag);
479 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.half_pel_enabled);
480 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.quarter_pel_enabled);
481 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.transform_skip_disabled);
482 RADEON_ENC_CS(0);
483 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.cu_qp_delta_enabled_flag);
484 RADEON_ENC_END();
485 }
486
487 /* nb_sb: number of super blocks in width/height
488 * nb_tiles: number of tiles trying to partition
489 * min_nb_sb: the minimum amount of sbs in a tile
490 */
radeon_enc_is_av1_uniform_tile(uint32_t nb_sb,uint32_t nb_tiles,uint32_t min_nb_sb,struct tile_1d_layout * p)491 bool radeon_enc_is_av1_uniform_tile (uint32_t nb_sb, uint32_t nb_tiles,
492 uint32_t min_nb_sb, struct tile_1d_layout *p)
493 {
494 if (!min_nb_sb)
495 min_nb_sb = 1;
496
497 if (IS_POT_NONZERO(nb_tiles)) {
498 uint32_t nb_main_sb = DIV_ROUND_UP(nb_sb, nb_tiles);
499 uint32_t nb_main_tile = nb_sb / nb_main_sb;
500 uint32_t nb_remainder_sb = nb_sb % nb_main_sb;
501
502 /* all nb in tile has to be larger than min_nb_sb */
503 if (nb_main_sb < min_nb_sb)
504 return false;
505
506 /* if remainder exists it needs to larger than min_nb_sb */
507 if ((nb_remainder_sb && (nb_remainder_sb < min_nb_sb))
508 || ((nb_main_sb * nb_main_tile + nb_remainder_sb) != nb_sb)
509 || (nb_main_tile + !!(nb_remainder_sb) != nb_tiles))
510 return false;
511
512 p->nb_main_sb = nb_main_sb;
513 p->nb_main_tile = nb_main_tile;
514 p->nb_border_sb = nb_remainder_sb;
515 p->nb_border_tile = !!(nb_remainder_sb);
516
517 return true;
518 }
519
520 /* the number of tiles is not power of 2 */
521 return false;
522 }
523
radeon_enc_av1_tile_layout(uint32_t nb_sb,uint32_t nb_tiles,uint32_t min_nb_sb,struct tile_1d_layout * p)524 void radeon_enc_av1_tile_layout (uint32_t nb_sb, uint32_t nb_tiles, uint32_t min_nb_sb,
525 struct tile_1d_layout *p)
526 {
527 if (!min_nb_sb)
528 min_nb_sb = 1;
529
530 if (radeon_enc_is_av1_uniform_tile(nb_sb, nb_tiles, min_nb_sb, p))
531 p->uniform_tile_flag = true;
532 else {
533 uint32_t nb_main_sb = nb_sb / nb_tiles;
534
535 /* if some tile size is less than min_nb_sb need to re-divide tiles */
536 if (nb_main_sb < min_nb_sb) {
537 /* using maximum tile size (64), to recalc nb_tiles */
538 nb_tiles = DIV_ROUND_UP(nb_sb, (RENCODE_AV1_MAX_TILE_WIDTH >> 6));
539 nb_main_sb = nb_sb / nb_tiles;
540 if (radeon_enc_is_av1_uniform_tile(nb_sb, nb_tiles, min_nb_sb, p)) {
541 p->uniform_tile_flag = true;
542 return;
543 }
544 }
545
546 p->uniform_tile_flag = false;
547 if (nb_tiles <= 1) {
548 p->nb_main_sb = nb_sb;
549 p->nb_main_tile = 1;
550 p->nb_border_sb = 0;
551 p->nb_border_tile = 0;
552 } else {
553 uint32_t nb_remainder_sb = nb_sb % nb_tiles;
554
555 if (nb_remainder_sb) {
556 p->nb_main_sb = nb_main_sb + 1;
557 p->nb_main_tile = nb_remainder_sb; /* in unit of tile */
558 p->nb_border_sb = nb_main_sb;
559 p->nb_border_tile = nb_tiles - nb_remainder_sb;
560 } else {
561 p->nb_main_sb = nb_main_sb;
562 p->nb_main_tile = nb_tiles;
563 p->nb_border_sb = 0;
564 p->nb_border_tile = 0;
565 }
566 }
567 }
568 }
569
570 /* num_tile_cols and num_tile_rows will be changed if not fit */
radeon_enc_av1_tile_default(struct radeon_encoder * enc,uint32_t * num_tile_cols,uint32_t * num_tile_rows)571 static void radeon_enc_av1_tile_default(struct radeon_encoder *enc,
572 uint32_t *num_tile_cols,
573 uint32_t *num_tile_rows)
574 {
575 struct tile_1d_layout tile_layout;
576 uint32_t i;
577 bool uniform_col, uniform_row;
578 rvcn_enc_av1_tile_config_t *p_config = &enc->enc_pic.av1_tile_config;
579 uint32_t frame_width_in_sb =
580 DIV_ROUND_UP(enc->enc_pic.pic_width_in_luma_samples, PIPE_AV1_ENC_SB_SIZE);
581 uint32_t frame_height_in_sb =
582 DIV_ROUND_UP(enc->enc_pic.pic_height_in_luma_samples, PIPE_AV1_ENC_SB_SIZE);
583 uint32_t min_tile_width_in_sb = RENCODE_AV1_MIN_TILE_WIDTH >> 6;
584 uint32_t max_tile_area_sb = RENCODE_AV1_MAX_TILE_AREA >> (2 * 6);
585 uint32_t max_tile_width_in_sb = RENCODE_AV1_MAX_TILE_WIDTH >> 6;
586 uint32_t widest_tiles_in_sb = 0;
587 uint32_t max_tile_ares_in_sb = 0;
588 uint32_t max_tile_height_in_sb = 0;
589 uint32_t min_log2_tiles_width_in_sb =
590 radeon_enc_av1_tile_log2(max_tile_width_in_sb, frame_width_in_sb);
591 uint32_t min_log2_tiles = MAX2(min_log2_tiles_width_in_sb,
592 radeon_enc_av1_tile_log2(max_tile_area_sb,
593 frame_width_in_sb * frame_height_in_sb));
594
595 radeon_enc_av1_tile_layout(frame_width_in_sb, *num_tile_cols,
596 min_tile_width_in_sb, &tile_layout);
597
598 *num_tile_cols = tile_layout.nb_main_tile + tile_layout.nb_border_tile;
599 uniform_col = tile_layout.uniform_tile_flag;
600
601 for (i = 0; i < tile_layout.nb_main_tile; i++) {
602 p_config->tile_widths[i] = tile_layout.nb_main_sb;
603 widest_tiles_in_sb = MAX2(p_config->tile_widths[i], widest_tiles_in_sb);
604 }
605
606 for (i = 0; i < tile_layout.nb_border_tile; i++) {
607 p_config->tile_widths[i + tile_layout.nb_main_tile] = tile_layout.nb_border_sb;
608 widest_tiles_in_sb = MAX2(p_config->tile_widths[i], widest_tiles_in_sb);
609 }
610
611 if (min_log2_tiles)
612 max_tile_ares_in_sb = (frame_width_in_sb * frame_height_in_sb)
613 >> (min_log2_tiles + 1);
614 else
615 max_tile_ares_in_sb = frame_width_in_sb * frame_height_in_sb;
616
617 max_tile_height_in_sb = DIV_ROUND_UP(max_tile_ares_in_sb, widest_tiles_in_sb);
618 *num_tile_rows = MAX2(*num_tile_rows,
619 DIV_ROUND_UP(frame_height_in_sb, max_tile_height_in_sb));
620
621 radeon_enc_av1_tile_layout(frame_height_in_sb, *num_tile_rows, 1, &tile_layout);
622 *num_tile_rows = tile_layout.nb_main_tile + tile_layout.nb_border_tile;
623 uniform_row = tile_layout.uniform_tile_flag;
624
625 for (i = 0; i < tile_layout.nb_main_tile; i++)
626 p_config->tile_height[i] = tile_layout.nb_main_sb;
627
628 for (i = 0; i < tile_layout.nb_border_tile; i++)
629 p_config->tile_height[i + tile_layout.nb_main_tile] = tile_layout.nb_border_sb;
630
631 p_config->uniform_tile_spacing = !!(uniform_col && uniform_row);
632
633 if (enc->enc_pic.is_obu_frame) {
634 p_config->num_tile_groups = 1;
635 p_config->tile_groups[0].start = 0;
636 p_config->tile_groups[0].end = (*num_tile_rows) * (*num_tile_cols) - 1;
637 } else {
638 p_config->num_tile_groups = (*num_tile_rows) * (*num_tile_cols);
639 for (int32_t i = 0; i < *num_tile_rows; i++)
640 for (int32_t j = 0; j < *num_tile_cols; j++) {
641 int32_t k = *num_tile_cols * i + j;
642 p_config->tile_groups[k].start = k;
643 p_config->tile_groups[k].end = k;
644 }
645 }
646 }
647
radeon_enc_tile_config_av1(struct radeon_encoder * enc)648 static void radeon_enc_tile_config_av1(struct radeon_encoder *enc)
649 {
650 /* It needs to check if the input tile setting meet the current capability
651 * or not, if not then regenerate the setting, if needed at the corresponding
652 * variables for obu instruction to program headers easier and consistent
653 * with this logic */
654
655 rvcn_enc_av1_tile_config_t *p_config = &enc->enc_pic.av1_tile_config;
656 uint32_t i = 0;
657 uint32_t frame_width_in_sb =
658 DIV_ROUND_UP(enc->enc_pic.pic_width_in_luma_samples, PIPE_AV1_ENC_SB_SIZE);
659 uint32_t min_tile_width_in_sb = RENCODE_AV1_MIN_TILE_WIDTH >> 6;
660 uint32_t max_tile_num_in_width = frame_width_in_sb / min_tile_width_in_sb;
661 uint32_t max_tile_width_in_sb = RENCODE_AV1_MAX_TILE_WIDTH >> 6;
662 uint32_t min_tile_num_in_width = DIV_ROUND_UP(frame_width_in_sb, max_tile_width_in_sb);
663 uint32_t num_tile_cols, num_tile_rows;
664
665 num_tile_cols = CLAMP(p_config->num_tile_cols,
666 MAX2(1, min_tile_num_in_width),
667 MIN2(RENCODE_AV1_TILE_CONFIG_MAX_NUM_COLS, max_tile_num_in_width));
668 /* legacy way of splitting tiles, if width is less than or equal to 64 sbs, it cannot be
669 * split */
670 if (enc->enc_pic.av1_tile_splitting_legacy_flag)
671 num_tile_cols = (frame_width_in_sb <= 64) ? 1 : num_tile_cols;
672
673 num_tile_rows = CLAMP(p_config->num_tile_rows,
674 1, RENCODE_AV1_TILE_CONFIG_MAX_NUM_ROWS);
675
676 p_config->apply_app_setting = false;
677
678 /* if no adjust necessary then use user's setting */
679 if (num_tile_rows == p_config->num_tile_rows &&
680 num_tile_cols == p_config->num_tile_cols) {
681 for (i = 0; i < num_tile_cols; i++) {
682 if (p_config->tile_widths[i] <= min_tile_width_in_sb)
683 break;
684 }
685 if (i == num_tile_cols)
686 p_config->apply_app_setting = true;
687 }
688 p_config->tile_size_bytes_minus_1 = 3; /* fixed value */
689
690 if (p_config->apply_app_setting && p_config->context_update_tile_id)
691 p_config->context_update_tile_id_mode = RENCODE_AV1_CONTEXT_UPDATE_TILE_ID_MODE_CUSTOMIZED;
692 else
693 p_config->context_update_tile_id_mode = RENCODE_AV1_CONTEXT_UPDATE_TILE_ID_MODE_DEFAULT;
694
695 if (!p_config->apply_app_setting) {
696 radeon_enc_av1_tile_default(enc, &num_tile_cols, &num_tile_rows);
697
698 /* re-layout tile */
699 p_config->num_tile_cols = num_tile_cols;
700 p_config->num_tile_rows = num_tile_rows;
701 }
702
703 RADEON_ENC_BEGIN(enc->cmd.tile_config_av1);
704 RADEON_ENC_CS(p_config->num_tile_cols);
705 RADEON_ENC_CS(p_config->num_tile_rows);
706 for (i = 0; i < RENCODE_AV1_TILE_CONFIG_MAX_NUM_COLS; i++)
707 RADEON_ENC_CS(p_config->tile_widths[i]);
708 for (i = 0; i < RENCODE_AV1_TILE_CONFIG_MAX_NUM_ROWS; i++)
709 RADEON_ENC_CS(p_config->tile_height[i]);
710 p_config->num_tile_groups = MIN2(p_config->num_tile_groups,
711 p_config->num_tile_cols * p_config->num_tile_rows);
712 RADEON_ENC_CS(p_config->num_tile_groups);
713 for (i = 0;
714 i < RENCODE_AV1_TILE_CONFIG_MAX_NUM_COLS * RENCODE_AV1_TILE_CONFIG_MAX_NUM_ROWS;
715 i++ ) {
716 RADEON_ENC_CS(p_config->tile_groups[i].start);
717 RADEON_ENC_CS(p_config->tile_groups[i].end);
718 }
719 RADEON_ENC_CS(p_config->context_update_tile_id_mode);
720 RADEON_ENC_CS(p_config->context_update_tile_id);
721 RADEON_ENC_CS(p_config->tile_size_bytes_minus_1);
722 RADEON_ENC_END();
723 }
724
radeon_enc_av1_tile_info(struct radeon_encoder * enc,struct radeon_bitstream * bs)725 static void radeon_enc_av1_tile_info(struct radeon_encoder *enc, struct radeon_bitstream *bs)
726 {
727 rvcn_enc_av1_tile_config_t *p_config = &enc->enc_pic.av1_tile_config;
728 uint32_t i = 0;
729 uint32_t sbCols = DIV_ROUND_UP(enc->enc_pic.pic_width_in_luma_samples, PIPE_AV1_ENC_SB_SIZE);
730 uint32_t sbRows = DIV_ROUND_UP(enc->enc_pic.pic_height_in_luma_samples, PIPE_AV1_ENC_SB_SIZE);
731 uint32_t maxTileWidthSb = RENCODE_AV1_MAX_TILE_WIDTH >> 6;
732 uint32_t maxTileAreaSb = RENCODE_AV1_MAX_TILE_AREA >> (2 * 6);
733 uint32_t minLog2TileCols = radeon_enc_av1_tile_log2(maxTileWidthSb, sbCols);
734 uint32_t minLog2Tiles = MAX2(minLog2TileCols,
735 radeon_enc_av1_tile_log2(maxTileAreaSb, sbRows * sbCols));
736 uint32_t TileColsLog2, TileRowsLog2;
737
738 TileColsLog2 = util_logbase2_ceil(p_config->num_tile_cols);
739 TileRowsLog2 = util_logbase2_ceil(p_config->num_tile_rows);
740
741 radeon_bs_code_fixed_bits(bs, p_config->uniform_tile_spacing, 1);
742 if (p_config->uniform_tile_spacing) {
743 for ( i = minLog2TileCols; i < TileColsLog2; i++)
744 radeon_bs_code_fixed_bits(bs, 1, 1);
745
746 radeon_bs_code_fixed_bits(bs, 0, 1);
747
748 for ( i = minLog2Tiles - TileColsLog2; i < TileRowsLog2; i++)
749 radeon_bs_code_fixed_bits(bs, 1, 1);
750
751 radeon_bs_code_fixed_bits(bs, 0, 1);
752 } else {
753 uint32_t widestTileSb = 0;
754 uint32_t maxWidthInSb = 0;
755 uint32_t maxHeightInSb = 0;
756 uint32_t maxTileHeightSb = 0;
757 uint32_t startSb = 0;
758
759 for (i = 0; i < p_config->num_tile_cols; i++) {
760 maxWidthInSb = MIN2(sbCols - startSb, maxTileWidthSb);
761 radeon_bs_code_ns(bs, p_config->tile_widths[i] - 1, maxWidthInSb);
762 startSb += p_config->tile_widths[i];
763 widestTileSb = MAX2( p_config->tile_widths[i], widestTileSb);
764 }
765
766 if (minLog2Tiles > 0)
767 maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1);
768 else
769 maxTileAreaSb = sbRows * sbCols;
770
771 maxTileHeightSb = MAX2( maxTileAreaSb / widestTileSb, 1);
772 startSb = 0;
773
774 for (i = 0; i < p_config->num_tile_rows; i++) {
775 maxHeightInSb = MIN2(sbRows - startSb, maxTileHeightSb);
776 radeon_bs_code_ns(bs, p_config->tile_height[i] - 1, maxHeightInSb);
777 startSb += p_config->tile_height[i];
778 }
779 }
780
781 if (TileColsLog2 > 0 || TileRowsLog2 > 0) {
782 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_V5_AV1_BITSTREAM_INSTRUCTION_CONTEXT_UPDATE_TILE_ID, 0);
783
784 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_COPY, 0);
785
786 radeon_bs_code_fixed_bits(bs, p_config->tile_size_bytes_minus_1, 2);
787 }
788 }
789
radeon_enc_av1_write_delta_q(struct radeon_bitstream * bs,int32_t q)790 static void radeon_enc_av1_write_delta_q(struct radeon_bitstream *bs, int32_t q)
791 {
792 radeon_bs_code_fixed_bits(bs, !!(q), 1);
793
794 if (q)
795 radeon_bs_code_fixed_bits(bs, q, ( 1 + 6 ));
796 }
797
radeon_enc_av1_quantization_params(struct radeon_encoder * enc,struct radeon_bitstream * bs)798 static void radeon_enc_av1_quantization_params(struct radeon_encoder *enc, struct radeon_bitstream *bs)
799 {
800 rvcn_enc_av1_spec_misc_t *p = &enc->enc_pic.av1_spec_misc;
801
802 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_V5_AV1_BITSTREAM_INSTRUCTION_BASE_Q_IDX, 0);
803
804 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_COPY, 0);
805
806 radeon_enc_av1_write_delta_q(bs, p->delta_q_y_dc);
807
808 /* only support multi-planes at the time */
809 if (p->separate_delta_q)
810 radeon_bs_code_fixed_bits(bs, 1, 1);
811
812 radeon_enc_av1_write_delta_q(bs, p->delta_q_u_dc);
813 radeon_enc_av1_write_delta_q(bs, p->delta_q_u_ac);
814
815 if (p->separate_delta_q) {
816 radeon_enc_av1_write_delta_q(bs, p->delta_q_v_dc);
817 radeon_enc_av1_write_delta_q(bs, p->delta_q_v_ac);
818 }
819
820 /* using qmatrix */
821 radeon_bs_code_fixed_bits(bs, 0, 1);
822 }
823
radeon_enc_av1_get_relative_dist(struct radeon_encoder * enc,uint32_t a,uint32_t b)824 static int32_t radeon_enc_av1_get_relative_dist(struct radeon_encoder *enc, uint32_t a, uint32_t b)
825 {
826 uint32_t diff = a - b;
827 uint32_t m = 1 << (enc->enc_pic.av1.desc->seq.order_hint_bits - 1);
828 diff = (diff & (m - 1)) - (diff & m);
829 return diff;
830 }
831
radeon_enc_av1_skip_mode_allowed(struct radeon_encoder * enc,uint32_t frames[2])832 bool radeon_enc_av1_skip_mode_allowed(struct radeon_encoder *enc, uint32_t frames[2])
833 {
834 if (enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_KEY ||
835 enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_INTRA_ONLY ||
836 !enc->enc_pic.av1.compound ||
837 !enc->enc_pic.av1.desc->seq.seq_bits.enable_order_hint)
838 return false;
839
840 int32_t forward_idx = -1, backward_idx = -1;
841 uint32_t forward_hint = 0, backward_hint = 0;
842
843 for (uint32_t i = 0; i < RENCODE_AV1_REFS_PER_FRAME; i++) {
844 uint32_t ref_hint = enc->enc_pic.av1.desc->dpb[enc->enc_pic.av1.desc->dpb_ref_frame_idx[i]].order_hint;
845 int32_t dist = radeon_enc_av1_get_relative_dist(enc, ref_hint, enc->enc_pic.av1.desc->order_hint);
846 if (dist < 0) {
847 if (forward_idx < 0 || radeon_enc_av1_get_relative_dist(enc, ref_hint, forward_hint) > 0) {
848 forward_idx = i;
849 forward_hint = ref_hint;
850 }
851 } else if (dist > 0) {
852 if (backward_idx < 0 || radeon_enc_av1_get_relative_dist(enc, ref_hint, backward_hint) < 0) {
853 backward_idx = i;
854 backward_hint = ref_hint;
855 }
856 }
857 }
858
859 if (forward_idx < 0)
860 return false;
861
862 if (backward_idx >= 0) {
863 frames[0] = MIN2(forward_idx, backward_idx);
864 frames[1] = MAX2(forward_idx, backward_idx);
865 return true;
866 }
867
868 int32_t second_forward_idx = -1;
869 uint32_t second_forward_hint;
870
871 for (uint32_t i = 0; i < RENCODE_AV1_REFS_PER_FRAME; i++) {
872 uint32_t ref_hint = enc->enc_pic.av1.desc->dpb[enc->enc_pic.av1.desc->dpb_ref_frame_idx[i]].order_hint;
873 if (radeon_enc_av1_get_relative_dist(enc, ref_hint, forward_hint) < 0) {
874 if (second_forward_idx < 0 || radeon_enc_av1_get_relative_dist(enc, ref_hint, second_forward_hint) > 0) {
875 second_forward_idx = i;
876 second_forward_hint = ref_hint;
877 }
878 }
879 }
880
881 if (second_forward_idx < 0)
882 return false;
883
884 frames[0] = MIN2(forward_idx, second_forward_idx);
885 frames[1] = MAX2(forward_idx, second_forward_idx);
886 return true;
887 }
888
radeon_enc_av1_frame_header(struct radeon_encoder * enc,struct radeon_bitstream * bs,bool frame_header)889 static void radeon_enc_av1_frame_header(struct radeon_encoder *enc, struct radeon_bitstream *bs, bool frame_header)
890 {
891 bool frame_is_intra = enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_KEY ||
892 enc->enc_pic.frame_type == PIPE_AV1_ENC_FRAME_TYPE_INTRA_ONLY;
893
894 radeon_enc_av1_frame_header_common(enc, bs, frame_header);
895
896 /* tile_info */
897 radeon_enc_av1_tile_info(enc, bs);
898 /* quantization_params */
899 radeon_enc_av1_quantization_params(enc, bs);
900 /* segmentation_enable */
901 radeon_bs_code_fixed_bits(bs, 0, 1);
902 /* delta_q_params */
903 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_DELTA_Q_PARAMS, 0);
904 /* delta_lf_params */
905 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_DELTA_LF_PARAMS, 0);
906 /* loop_filter_params */
907 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_LOOP_FILTER_PARAMS, 0);
908 /* cdef_params */
909 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_CDEF_PARAMS, 0);
910 /* lr_params */
911 /* read_tx_mode */
912 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_READ_TX_MODE, 0);
913
914 radeon_enc_av1_bs_instruction_type(enc, bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_COPY, 0);
915
916 if (!frame_is_intra)
917 /* reference_select */
918 radeon_bs_code_fixed_bits(bs, enc->enc_pic.av1.compound, 1);
919
920 if (enc->enc_pic.av1.skip_mode_allowed)
921 /* skip_mode_present */
922 radeon_bs_code_fixed_bits(bs, !enc->enc_pic.av1_spec_misc.disallow_skip_mode, 1);
923
924 /* reduced_tx_set */
925 radeon_bs_code_fixed_bits(bs, 0, 1);
926
927 if (!frame_is_intra)
928 for (uint32_t ref = 1 /*LAST_FRAME*/; ref <= 7 /*ALTREF_FRAME*/; ref++)
929 /* is_global */
930 radeon_bs_code_fixed_bits(bs, 0, 1);
931 /* film_grain_params() */
932 }
933
radeon_enc_obu_instruction(struct radeon_encoder * enc)934 static void radeon_enc_obu_instruction(struct radeon_encoder *enc)
935 {
936 bool frame_header = !enc->enc_pic.is_obu_frame;
937 struct radeon_bitstream bs;
938
939 radeon_bs_reset(&bs, NULL, &enc->cs);
940
941 RADEON_ENC_BEGIN(enc->cmd.bitstream_instruction_av1);
942
943 radeon_enc_av1_bs_instruction_type(enc, &bs,
944 RENCODE_AV1_BITSTREAM_INSTRUCTION_OBU_START,
945 frame_header ? RENCODE_OBU_START_TYPE_FRAME_HEADER
946 : RENCODE_OBU_START_TYPE_FRAME);
947
948 radeon_enc_av1_frame_header(enc, &bs, frame_header);
949
950 if (!frame_header)
951 radeon_enc_av1_bs_instruction_type(enc, &bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_TILE_GROUP_OBU, 0);
952
953 radeon_enc_av1_bs_instruction_type(enc, &bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_OBU_END, 0);
954
955 if (frame_header)
956 radeon_enc_av1_tile_group(enc, &bs);
957
958 radeon_enc_av1_bs_instruction_type(enc, &bs, RENCODE_AV1_BITSTREAM_INSTRUCTION_END, 0);
959 RADEON_ENC_END();
960 }
961
radeon_enc_session_init(struct radeon_encoder * enc)962 static void radeon_enc_session_init(struct radeon_encoder *enc)
963 {
964 enc->enc_pic.session_init.slice_output_enabled = 0;
965 enc->enc_pic.session_init.display_remote = 0;
966 enc->enc_pic.session_init.pre_encode_mode = enc->enc_pic.quality_modes.pre_encode_mode;
967 enc->enc_pic.session_init.pre_encode_chroma_enabled = !!(enc->enc_pic.quality_modes.pre_encode_mode);
968
969 RADEON_ENC_BEGIN(enc->cmd.session_init);
970 RADEON_ENC_CS(enc->enc_pic.session_init.encode_standard);
971 RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_width);
972 RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_height);
973 RADEON_ENC_CS(enc->enc_pic.session_init.padding_width);
974 RADEON_ENC_CS(enc->enc_pic.session_init.padding_height);
975 RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_mode);
976 RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_chroma_enabled);
977 RADEON_ENC_CS(enc->enc_pic.session_init.slice_output_enabled);
978 RADEON_ENC_CS(enc->enc_pic.session_init.display_remote);
979 RADEON_ENC_END();
980 }
981
radeon_enc_5_0_init(struct radeon_encoder * enc)982 void radeon_enc_5_0_init(struct radeon_encoder *enc)
983 {
984 radeon_enc_4_0_init(enc);
985
986 enc->session_init = radeon_enc_session_init;
987 enc->output_format = radeon_enc_output_format;
988 enc->metadata = radeon_enc_metadata;
989 enc->encode_params = radeon_enc_encode_params;
990 enc->rc_per_pic = radeon_enc_rc_per_pic;
991
992 if (enc->dpb_type == DPB_LEGACY) {
993 enc->ctx = radeon_enc_ctx;
994 enc->ctx_override = radeon_enc_ctx_override;
995 } else if (enc->dpb_type == DPB_TIER_2) {
996 enc->ctx = radeon_enc_ctx_tier2;
997 enc->ctx_override = radeon_enc_dummy;
998 }
999
1000 if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_MPEG4_AVC) {
1001 enc->spec_misc = radeon_enc_spec_misc;
1002 enc->encode_params_codec_spec = radeon_enc_encode_params_h264;
1003 } else if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC) {
1004 enc->encode_params_codec_spec = radeon_enc_encode_params_hevc;
1005 enc->spec_misc = radeon_enc_spec_misc_hevc;
1006 } else if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_AV1) {
1007 enc->cdf_default_table = radeon_enc_cdf_default_table;
1008 enc->spec_misc = radeon_enc_spec_misc_av1;
1009 enc->tile_config = radeon_enc_tile_config_av1;
1010 enc->obu_instructions = radeon_enc_obu_instruction;
1011 enc->encode_params_codec_spec = radeon_enc_encode_params_av1;
1012 }
1013
1014 enc->enc_pic.session_info.interface_version =
1015 ((RENCODE_FW_INTERFACE_MAJOR_VERSION << RENCODE_IF_MAJOR_VERSION_SHIFT) |
1016 (RENCODE_FW_INTERFACE_MINOR_VERSION << RENCODE_IF_MINOR_VERSION_SHIFT));
1017 }
1018