radeonsi: remove r600_pipe_common::set_atom_dirty
[mesa.git] / src / gallium / drivers / radeon / radeon_uvd_enc_1_1.c
1 /**************************************************************************
2 *
3 * Copyright 2018 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 #include "util/u_memory.h"
34
35 #include "vl/vl_video_buffer.h"
36 #include "radeonsi/si_pipe.h"
37 #include "radeon_video.h"
38 #include "radeon_uvd_enc.h"
39
40 #define RADEON_ENC_CS(value) (enc->cs->current.buf[enc->cs->current.cdw++] = (value))
41 #define RADEON_ENC_BEGIN(cmd) { \
42 uint32_t *begin = &enc->cs->current.buf[enc->cs->current.cdw++]; \
43 RADEON_ENC_CS(cmd)
44 #define RADEON_ENC_READ(buf, domain, off) radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_READ, (domain), (off))
45 #define RADEON_ENC_WRITE(buf, domain, off) radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_WRITE, (domain), (off))
46 #define RADEON_ENC_READWRITE(buf, domain, off) radeon_uvd_enc_add_buffer(enc, (buf), RADEON_USAGE_READWRITE, (domain), (off))
47 #define RADEON_ENC_END() *begin = (&enc->cs->current.buf[enc->cs->current.cdw] - begin) * 4; \
48 enc->total_task_size += *begin;}
49
50 static const unsigned index_to_shifts[4] = { 24, 16, 8, 0 };
51
52 static void
53 radeon_uvd_enc_add_buffer(struct radeon_uvd_encoder *enc,
54 struct pb_buffer *buf, enum radeon_bo_usage usage,
55 enum radeon_bo_domain domain, signed offset)
56 {
57 enc->ws->cs_add_buffer(enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
58 domain, RADEON_PRIO_VCE);
59 uint64_t addr;
60 addr = enc->ws->buffer_get_virtual_address(buf);
61 addr = addr + offset;
62 RADEON_ENC_CS(addr >> 32);
63 RADEON_ENC_CS(addr);
64 }
65
66 static void
67 radeon_uvd_enc_set_emulation_prevention(struct radeon_uvd_encoder *enc,
68 bool set)
69 {
70 if (set != enc->emulation_prevention) {
71 enc->emulation_prevention = set;
72 enc->num_zeros = 0;
73 }
74 }
75
76 static void
77 radeon_uvd_enc_output_one_byte(struct radeon_uvd_encoder *enc,
78 unsigned char byte)
79 {
80 if (enc->byte_index == 0)
81 enc->cs->current.buf[enc->cs->current.cdw] = 0;
82 enc->cs->current.buf[enc->cs->current.cdw] |=
83 ((unsigned int) (byte) << index_to_shifts[enc->byte_index]);
84 enc->byte_index++;
85
86 if (enc->byte_index >= 4) {
87 enc->byte_index = 0;
88 enc->cs->current.cdw++;
89 }
90 }
91
92 static void
93 radeon_uvd_enc_emulation_prevention(struct radeon_uvd_encoder *enc,
94 unsigned char byte)
95 {
96 if (enc->emulation_prevention) {
97 if ((enc->num_zeros >= 2)
98 && ((byte == 0x00) || (byte == 0x01)
99 || (byte == 0x02) || (byte == 0x03))) {
100 radeon_uvd_enc_output_one_byte(enc, 0x03);
101 enc->bits_output += 8;
102 enc->num_zeros = 0;
103 }
104 enc->num_zeros = (byte == 0 ? (enc->num_zeros + 1) : 0);
105 }
106 }
107
108 static void
109 radeon_uvd_enc_code_fixed_bits(struct radeon_uvd_encoder *enc,
110 unsigned int value, unsigned int num_bits)
111 {
112 unsigned int bits_to_pack = 0;
113
114 while (num_bits > 0) {
115 unsigned int value_to_pack = value & (0xffffffff >> (32 - num_bits));
116 bits_to_pack =
117 num_bits >
118 (32 - enc->bits_in_shifter) ? (32 - enc->bits_in_shifter) : num_bits;
119
120 if (bits_to_pack < num_bits)
121 value_to_pack = value_to_pack >> (num_bits - bits_to_pack);
122
123 enc->shifter |=
124 value_to_pack << (32 - enc->bits_in_shifter - bits_to_pack);
125 num_bits -= bits_to_pack;
126 enc->bits_in_shifter += bits_to_pack;
127
128 while (enc->bits_in_shifter >= 8) {
129 unsigned char output_byte = (unsigned char) (enc->shifter >> 24);
130 enc->shifter <<= 8;
131 radeon_uvd_enc_emulation_prevention(enc, output_byte);
132 radeon_uvd_enc_output_one_byte(enc, output_byte);
133 enc->bits_in_shifter -= 8;
134 enc->bits_output += 8;
135 }
136 }
137 }
138
139 static void
140 radeon_uvd_enc_reset(struct radeon_uvd_encoder *enc)
141 {
142 enc->emulation_prevention = false;
143 enc->shifter = 0;
144 enc->bits_in_shifter = 0;
145 enc->bits_output = 0;
146 enc->num_zeros = 0;
147 enc->byte_index = 0;
148 }
149
150 static void
151 radeon_uvd_enc_byte_align(struct radeon_uvd_encoder *enc)
152 {
153 unsigned int num_padding_zeros = (32 - enc->bits_in_shifter) % 8;
154
155 if (num_padding_zeros > 0)
156 radeon_uvd_enc_code_fixed_bits(enc, 0, num_padding_zeros);
157 }
158
159 static void
160 radeon_uvd_enc_flush_headers(struct radeon_uvd_encoder *enc)
161 {
162 if (enc->bits_in_shifter != 0) {
163 unsigned char output_byte = (unsigned char) (enc->shifter >> 24);
164 radeon_uvd_enc_emulation_prevention(enc, output_byte);
165 radeon_uvd_enc_output_one_byte(enc, output_byte);
166 enc->bits_output += enc->bits_in_shifter;
167 enc->shifter = 0;
168 enc->bits_in_shifter = 0;
169 enc->num_zeros = 0;
170 }
171
172 if (enc->byte_index > 0) {
173 enc->cs->current.cdw++;
174 enc->byte_index = 0;
175 }
176 }
177
178 static void
179 radeon_uvd_enc_code_ue(struct radeon_uvd_encoder *enc, unsigned int value)
180 {
181 int x = -1;
182 unsigned int ue_code = value + 1;
183 value += 1;
184
185 while (value) {
186 value = (value >> 1);
187 x += 1;
188 }
189
190 unsigned int ue_length = (x << 1) + 1;
191 radeon_uvd_enc_code_fixed_bits(enc, ue_code, ue_length);
192 }
193
194 static void
195 radeon_uvd_enc_code_se(struct radeon_uvd_encoder *enc, int value)
196 {
197 unsigned int v = 0;
198
199 if (value != 0)
200 v = (value < 0 ? ((unsigned int) (0 - value) << 1)
201 : (((unsigned int) (value) << 1) - 1));
202
203 radeon_uvd_enc_code_ue(enc, v);
204 }
205
206 static void
207 radeon_uvd_enc_session_info(struct radeon_uvd_encoder *enc)
208 {
209 unsigned int interface_version =
210 ((RENC_UVD_FW_INTERFACE_MAJOR_VERSION <<
211 RENC_UVD_IF_MAJOR_VERSION_SHIFT) |
212 (RENC_UVD_FW_INTERFACE_MINOR_VERSION <<
213 RENC_UVD_IF_MINOR_VERSION_SHIFT));
214 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SESSION_INFO);
215 RADEON_ENC_CS(0x00000000); // reserved
216 RADEON_ENC_CS(interface_version);
217 RADEON_ENC_READWRITE(enc->si->res->buf, enc->si->res->domains, 0x0);
218 RADEON_ENC_END();
219 }
220
221 static void
222 radeon_uvd_enc_task_info(struct radeon_uvd_encoder *enc, bool need_feedback)
223 {
224 enc->enc_pic.task_info.task_id++;
225
226 if (need_feedback)
227 enc->enc_pic.task_info.allowed_max_num_feedbacks = 1;
228 else
229 enc->enc_pic.task_info.allowed_max_num_feedbacks = 0;
230
231 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_TASK_INFO);
232 enc->p_task_size = &enc->cs->current.buf[enc->cs->current.cdw++];
233 RADEON_ENC_CS(enc->enc_pic.task_info.task_id);
234 RADEON_ENC_CS(enc->enc_pic.task_info.allowed_max_num_feedbacks);
235 RADEON_ENC_END();
236 }
237
238 static void
239 radeon_uvd_enc_session_init_hevc(struct radeon_uvd_encoder *enc)
240 {
241 enc->enc_pic.session_init.aligned_picture_width =
242 align(enc->base.width, 64);
243 enc->enc_pic.session_init.aligned_picture_height =
244 align(enc->base.height, 16);
245 enc->enc_pic.session_init.padding_width =
246 enc->enc_pic.session_init.aligned_picture_width - enc->base.width;
247 enc->enc_pic.session_init.padding_height =
248 enc->enc_pic.session_init.aligned_picture_height - enc->base.height;
249 enc->enc_pic.session_init.pre_encode_mode = RENC_UVD_PREENCODE_MODE_NONE;
250 enc->enc_pic.session_init.pre_encode_chroma_enabled = false;
251
252 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SESSION_INIT);
253 RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_width);
254 RADEON_ENC_CS(enc->enc_pic.session_init.aligned_picture_height);
255 RADEON_ENC_CS(enc->enc_pic.session_init.padding_width);
256 RADEON_ENC_CS(enc->enc_pic.session_init.padding_height);
257 RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_mode);
258 RADEON_ENC_CS(enc->enc_pic.session_init.pre_encode_chroma_enabled);
259 RADEON_ENC_END();
260 }
261
262 static void
263 radeon_uvd_enc_layer_control(struct radeon_uvd_encoder *enc)
264 {
265 enc->enc_pic.layer_ctrl.max_num_temporal_layers = 1;
266 enc->enc_pic.layer_ctrl.num_temporal_layers = 1;
267
268 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_LAYER_CONTROL);
269 RADEON_ENC_CS(enc->enc_pic.layer_ctrl.max_num_temporal_layers);
270 RADEON_ENC_CS(enc->enc_pic.layer_ctrl.num_temporal_layers);
271 RADEON_ENC_END();
272 }
273
274 static void
275 radeon_uvd_enc_layer_select(struct radeon_uvd_encoder *enc)
276 {
277 enc->enc_pic.layer_sel.temporal_layer_index = 0;
278
279 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_LAYER_SELECT);
280 RADEON_ENC_CS(enc->enc_pic.layer_sel.temporal_layer_index);
281 RADEON_ENC_END();
282 }
283
284 static void
285 radeon_uvd_enc_slice_control_hevc(struct radeon_uvd_encoder *enc)
286 {
287 enc->enc_pic.hevc_slice_ctrl.slice_control_mode =
288 RENC_UVD_SLICE_CONTROL_MODE_FIXED_CTBS;
289 enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice =
290 align(enc->base.width, 64) / 64 * align(enc->base.height, 64) / 64;
291 enc->enc_pic.hevc_slice_ctrl.
292 fixed_ctbs_per_slice.num_ctbs_per_slice_segment =
293 enc->enc_pic.hevc_slice_ctrl.fixed_ctbs_per_slice.num_ctbs_per_slice;
294
295 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SLICE_CONTROL);
296 RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.slice_control_mode);
297 RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.
298 fixed_ctbs_per_slice.num_ctbs_per_slice);
299 RADEON_ENC_CS(enc->enc_pic.hevc_slice_ctrl.
300 fixed_ctbs_per_slice.num_ctbs_per_slice_segment);
301 RADEON_ENC_END();
302 }
303
304 static void
305 radeon_uvd_enc_spec_misc_hevc(struct radeon_uvd_encoder *enc,
306 struct pipe_picture_desc *picture)
307 {
308 struct pipe_h265_enc_picture_desc *pic =
309 (struct pipe_h265_enc_picture_desc *) picture;
310 enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 =
311 pic->seq.log2_min_luma_coding_block_size_minus3;
312 enc->enc_pic.hevc_spec_misc.amp_disabled = !pic->seq.amp_enabled_flag;
313 enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled =
314 pic->seq.strong_intra_smoothing_enabled_flag;
315 enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag =
316 pic->pic.constrained_intra_pred_flag;
317 enc->enc_pic.hevc_spec_misc.cabac_init_flag = pic->slice.cabac_init_flag;
318 enc->enc_pic.hevc_spec_misc.half_pel_enabled = 1;
319 enc->enc_pic.hevc_spec_misc.quarter_pel_enabled = 1;
320
321 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SPEC_MISC);
322 RADEON_ENC_CS(enc->enc_pic.
323 hevc_spec_misc.log2_min_luma_coding_block_size_minus3);
324 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.amp_disabled);
325 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled);
326 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag);
327 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.cabac_init_flag);
328 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.half_pel_enabled);
329 RADEON_ENC_CS(enc->enc_pic.hevc_spec_misc.quarter_pel_enabled);
330 RADEON_ENC_END();
331 }
332
333 static void
334 radeon_uvd_enc_rc_session_init(struct radeon_uvd_encoder *enc,
335 struct pipe_picture_desc *picture)
336 {
337 struct pipe_h265_enc_picture_desc *pic =
338 (struct pipe_h265_enc_picture_desc *) picture;
339 enc->enc_pic.rc_session_init.vbv_buffer_level = pic->rc.vbv_buf_lv;
340 switch (pic->rc.rate_ctrl_method) {
341 case PIPE_H265_ENC_RATE_CONTROL_METHOD_DISABLE:
342 enc->enc_pic.rc_session_init.rate_control_method =
343 RENC_UVD_RATE_CONTROL_METHOD_NONE;
344 break;
345 case PIPE_H265_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP:
346 case PIPE_H265_ENC_RATE_CONTROL_METHOD_CONSTANT:
347 enc->enc_pic.rc_session_init.rate_control_method =
348 RENC_UVD_RATE_CONTROL_METHOD_CBR;
349 break;
350 case PIPE_H265_ENC_RATE_CONTROL_METHOD_VARIABLE_SKIP:
351 case PIPE_H265_ENC_RATE_CONTROL_METHOD_VARIABLE:
352 enc->enc_pic.rc_session_init.rate_control_method =
353 RENC_UVD_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
354 break;
355 default:
356 enc->enc_pic.rc_session_init.rate_control_method =
357 RENC_UVD_RATE_CONTROL_METHOD_NONE;
358 }
359
360 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_SESSION_INIT);
361 RADEON_ENC_CS(enc->enc_pic.rc_session_init.rate_control_method);
362 RADEON_ENC_CS(enc->enc_pic.rc_session_init.vbv_buffer_level);
363 RADEON_ENC_END();
364 }
365
366 static void
367 radeon_uvd_enc_rc_layer_init(struct radeon_uvd_encoder *enc,
368 struct pipe_picture_desc *picture)
369 {
370 struct pipe_h265_enc_picture_desc *pic =
371 (struct pipe_h265_enc_picture_desc *) picture;
372 enc->enc_pic.rc_layer_init.target_bit_rate = pic->rc.target_bitrate;
373 enc->enc_pic.rc_layer_init.peak_bit_rate = pic->rc.peak_bitrate;
374 enc->enc_pic.rc_layer_init.frame_rate_num = pic->rc.frame_rate_num;
375 enc->enc_pic.rc_layer_init.frame_rate_den = pic->rc.frame_rate_den;
376 enc->enc_pic.rc_layer_init.vbv_buffer_size = pic->rc.vbv_buffer_size;
377 enc->enc_pic.rc_layer_init.avg_target_bits_per_picture =
378 pic->rc.target_bits_picture;
379 enc->enc_pic.rc_layer_init.peak_bits_per_picture_integer =
380 pic->rc.peak_bits_picture_integer;
381 enc->enc_pic.rc_layer_init.peak_bits_per_picture_fractional =
382 pic->rc.peak_bits_picture_fraction;
383
384 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_LAYER_INIT);
385 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.target_bit_rate);
386 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bit_rate);
387 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_num);
388 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.frame_rate_den);
389 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.vbv_buffer_size);
390 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.avg_target_bits_per_picture);
391 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_integer);
392 RADEON_ENC_CS(enc->enc_pic.rc_layer_init.peak_bits_per_picture_fractional);
393 RADEON_ENC_END();
394 }
395
396 static void
397 radeon_uvd_enc_deblocking_filter_hevc(struct radeon_uvd_encoder *enc,
398 struct pipe_picture_desc *picture)
399 {
400 struct pipe_h265_enc_picture_desc *pic =
401 (struct pipe_h265_enc_picture_desc *) picture;
402 enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled =
403 pic->slice.slice_loop_filter_across_slices_enabled_flag;
404 enc->enc_pic.hevc_deblock.deblocking_filter_disabled =
405 pic->slice.slice_deblocking_filter_disabled_flag;
406 enc->enc_pic.hevc_deblock.beta_offset_div2 =
407 pic->slice.slice_beta_offset_div2;
408 enc->enc_pic.hevc_deblock.tc_offset_div2 = pic->slice.slice_tc_offset_div2;
409 enc->enc_pic.hevc_deblock.cb_qp_offset = pic->slice.slice_cb_qp_offset;
410 enc->enc_pic.hevc_deblock.cr_qp_offset = pic->slice.slice_cr_qp_offset;
411
412 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_DEBLOCKING_FILTER);
413 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled);
414 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.deblocking_filter_disabled);
415 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.beta_offset_div2);
416 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.tc_offset_div2);
417 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cb_qp_offset);
418 RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cr_qp_offset);
419 RADEON_ENC_END();
420 }
421
422 static void
423 radeon_uvd_enc_quality_params(struct radeon_uvd_encoder *enc)
424 {
425 enc->enc_pic.quality_params.vbaq_mode = 0;
426 enc->enc_pic.quality_params.scene_change_sensitivity = 0;
427 enc->enc_pic.quality_params.scene_change_min_idr_interval = 0;
428
429 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_QUALITY_PARAMS);
430 RADEON_ENC_CS(enc->enc_pic.quality_params.vbaq_mode);
431 RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_sensitivity);
432 RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_min_idr_interval);
433 RADEON_ENC_END();
434 }
435
436 static void
437 radeon_uvd_enc_nalu_sps_hevc(struct radeon_uvd_encoder *enc)
438 {
439 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);
440 RADEON_ENC_CS(RENC_UVD_NALU_TYPE_SPS);
441 uint32_t *size_in_bytes = &enc->cs->current.buf[enc->cs->current.cdw++];
442 int i;
443
444 radeon_uvd_enc_reset(enc);
445 radeon_uvd_enc_set_emulation_prevention(enc, false);
446 radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);
447 radeon_uvd_enc_code_fixed_bits(enc, 0x4201, 16);
448 radeon_uvd_enc_byte_align(enc);
449 radeon_uvd_enc_set_emulation_prevention(enc, true);
450 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 4);
451 radeon_uvd_enc_code_fixed_bits(enc,
452 enc->enc_pic.
453 layer_ctrl.max_num_temporal_layers - 1, 3);
454 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
455 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
456 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);
457 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);
458 radeon_uvd_enc_code_fixed_bits(enc, 0x60000000, 32);
459 radeon_uvd_enc_code_fixed_bits(enc, 0xb0000000, 32);
460 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 16);
461 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);
462
463 for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)
464 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
465
466 if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {
467 for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)
468 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
469 }
470
471 radeon_uvd_enc_code_ue(enc, 0x0);
472 radeon_uvd_enc_code_ue(enc, enc->enc_pic.chroma_format_idc);
473 radeon_uvd_enc_code_ue(enc,
474 enc->enc_pic.session_init.aligned_picture_width);
475 radeon_uvd_enc_code_ue(enc,
476 enc->enc_pic.session_init.aligned_picture_height);
477
478 int conformance_window_flag =
479 (enc->enc_pic.crop_top > 0) ||
480 (enc->enc_pic.crop_bottom > 0) ||
481 (enc->enc_pic.crop_left > 0) ||
482 (enc->enc_pic.crop_right > 0) ? 0x1 : 0x0;
483 radeon_uvd_enc_code_fixed_bits(enc, conformance_window_flag, 1);
484 if (conformance_window_flag == 1) {
485 radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_left);
486 radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_right);
487 radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_top);
488 radeon_uvd_enc_code_ue(enc, enc->enc_pic.crop_bottom);
489 }
490
491 radeon_uvd_enc_code_ue(enc, enc->enc_pic.bit_depth_luma_minus8);
492 radeon_uvd_enc_code_ue(enc, enc->enc_pic.bit_depth_chroma_minus8);
493 radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_max_poc - 4);
494 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
495 radeon_uvd_enc_code_ue(enc, 1);
496 radeon_uvd_enc_code_ue(enc, 0x0);
497 radeon_uvd_enc_code_ue(enc, 0x0);
498 radeon_uvd_enc_code_ue(enc,
499 enc->enc_pic.hevc_spec_misc.
500 log2_min_luma_coding_block_size_minus3);
501 /* Only support CTBSize 64 */
502 radeon_uvd_enc_code_ue(enc,
503 6 -
504 (enc->enc_pic.hevc_spec_misc.
505 log2_min_luma_coding_block_size_minus3 + 3));
506 radeon_uvd_enc_code_ue(enc,
507 enc->enc_pic.log2_min_transform_block_size_minus2);
508 radeon_uvd_enc_code_ue(enc,
509 enc->enc_pic.
510 log2_diff_max_min_transform_block_size);
511 radeon_uvd_enc_code_ue(enc,
512 enc->enc_pic.max_transform_hierarchy_depth_inter);
513 radeon_uvd_enc_code_ue(enc,
514 enc->enc_pic.max_transform_hierarchy_depth_intra);
515
516 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
517 radeon_uvd_enc_code_fixed_bits(enc,
518 !enc->enc_pic.hevc_spec_misc.amp_disabled,
519 1);
520 radeon_uvd_enc_code_fixed_bits(enc,
521 enc->enc_pic.
522 sample_adaptive_offset_enabled_flag, 1);
523 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.pcm_enabled_flag, 1);
524
525 radeon_uvd_enc_code_ue(enc, 1);
526 radeon_uvd_enc_code_ue(enc, 1);
527 radeon_uvd_enc_code_ue(enc, 0);
528 radeon_uvd_enc_code_ue(enc, 0);
529 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
530
531 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
532
533 radeon_uvd_enc_code_fixed_bits(enc, 0, 1);
534 radeon_uvd_enc_code_fixed_bits(enc,
535 enc->enc_pic.hevc_spec_misc.
536 strong_intra_smoothing_enabled, 1);
537
538 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
539
540 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
541
542 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
543
544 radeon_uvd_enc_byte_align(enc);
545 radeon_uvd_enc_flush_headers(enc);
546 *size_in_bytes = (enc->bits_output + 7) / 8;
547 RADEON_ENC_END();
548 }
549
550 static void
551 radeon_uvd_enc_nalu_pps_hevc(struct radeon_uvd_encoder *enc)
552 {
553 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);
554 RADEON_ENC_CS(RENC_UVD_NALU_TYPE_PPS);
555 uint32_t *size_in_bytes = &enc->cs->current.buf[enc->cs->current.cdw++];
556 radeon_uvd_enc_reset(enc);
557 radeon_uvd_enc_set_emulation_prevention(enc, false);
558 radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);
559 radeon_uvd_enc_code_fixed_bits(enc, 0x4401, 16);
560 radeon_uvd_enc_byte_align(enc);
561 radeon_uvd_enc_set_emulation_prevention(enc, true);
562 radeon_uvd_enc_code_ue(enc, 0x0);
563 radeon_uvd_enc_code_ue(enc, 0x0);
564 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
565 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1); /* output_flag_resent_flag */
566 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 3); /* num_extra_slice_header_bits */
567 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
568 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
569 radeon_uvd_enc_code_ue(enc, 0x0);
570 radeon_uvd_enc_code_ue(enc, 0x0);
571 radeon_uvd_enc_code_se(enc, 0x0);
572 radeon_uvd_enc_code_fixed_bits(enc,
573 enc->enc_pic.hevc_spec_misc.
574 constrained_intra_pred_flag, 1);
575 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
576 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
577 radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.cb_qp_offset);
578 radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.cr_qp_offset);
579 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
580 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
581 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
582 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
583 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
584 radeon_uvd_enc_code_fixed_bits(enc,
585 enc->enc_pic.hevc_deblock.
586 loop_filter_across_slices_enabled, 1);
587 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
588 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
589 radeon_uvd_enc_code_fixed_bits(enc,
590 enc->enc_pic.hevc_deblock.
591 deblocking_filter_disabled, 1);
592
593 if (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled) {
594 radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.beta_offset_div2);
595 radeon_uvd_enc_code_se(enc, enc->enc_pic.hevc_deblock.tc_offset_div2);
596 }
597
598 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
599 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
600 radeon_uvd_enc_code_ue(enc, enc->enc_pic.log2_parallel_merge_level_minus2);
601 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
602
603 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
604
605 radeon_uvd_enc_byte_align(enc);
606 radeon_uvd_enc_flush_headers(enc);
607 *size_in_bytes = (enc->bits_output + 7) / 8;
608 RADEON_ENC_END();
609 }
610
611 static void
612 radeon_uvd_enc_nalu_vps_hevc(struct radeon_uvd_encoder *enc)
613 {
614 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);
615 RADEON_ENC_CS(RENC_UVD_NALU_TYPE_VPS);
616 uint32_t *size_in_bytes = &enc->cs->current.buf[enc->cs->current.cdw++];
617 int i;
618
619 radeon_uvd_enc_reset(enc);
620 radeon_uvd_enc_set_emulation_prevention(enc, false);
621 radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);
622 radeon_uvd_enc_code_fixed_bits(enc, 0x4001, 16);
623 radeon_uvd_enc_byte_align(enc);
624 radeon_uvd_enc_set_emulation_prevention(enc, true);
625
626 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 4);
627 radeon_uvd_enc_code_fixed_bits(enc, 0x3, 2);
628 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);
629 radeon_uvd_enc_code_fixed_bits(enc,
630 enc->enc_pic.layer_ctrl.
631 max_num_temporal_layers - 1, 3);
632 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
633 radeon_uvd_enc_code_fixed_bits(enc, 0xffff, 16);
634 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
635 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);
636 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);
637 radeon_uvd_enc_code_fixed_bits(enc, 0x60000000, 32);
638 radeon_uvd_enc_code_fixed_bits(enc, 0xb0000000, 32);
639 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 16);
640 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);
641
642 for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)
643 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
644
645 if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {
646 for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)
647 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 2);
648 }
649
650 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
651 radeon_uvd_enc_code_ue(enc, 0x1);
652 radeon_uvd_enc_code_ue(enc, 0x0);
653 radeon_uvd_enc_code_ue(enc, 0x0);
654
655 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);
656 radeon_uvd_enc_code_ue(enc, 0x0);
657 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
658 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
659
660 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
661
662 radeon_uvd_enc_byte_align(enc);
663 radeon_uvd_enc_flush_headers(enc);
664 *size_in_bytes = (enc->bits_output + 7) / 8;
665 RADEON_ENC_END();
666 }
667
668 static void
669 radeon_uvd_enc_nalu_aud_hevc(struct radeon_uvd_encoder *enc)
670 {
671 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INSERT_NALU_BUFFER);
672 RADEON_ENC_CS(RENC_UVD_NALU_TYPE_AUD);
673 uint32_t *size_in_bytes = &enc->cs->current.buf[enc->cs->current.cdw++];
674 radeon_uvd_enc_reset(enc);
675 radeon_uvd_enc_set_emulation_prevention(enc, false);
676 radeon_uvd_enc_code_fixed_bits(enc, 0x00000001, 32);
677 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
678 radeon_uvd_enc_code_fixed_bits(enc, 35, 6);
679 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);
680 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 3);
681 radeon_uvd_enc_byte_align(enc);
682 radeon_uvd_enc_set_emulation_prevention(enc, true);
683 switch (enc->enc_pic.picture_type) {
684 case PIPE_H265_ENC_PICTURE_TYPE_I:
685 case PIPE_H265_ENC_PICTURE_TYPE_IDR:
686 radeon_uvd_enc_code_fixed_bits(enc, 0x00, 3);
687 break;
688 case PIPE_H265_ENC_PICTURE_TYPE_P:
689 radeon_uvd_enc_code_fixed_bits(enc, 0x01, 3);
690 break;
691 case PIPE_H265_ENC_PICTURE_TYPE_B:
692 radeon_uvd_enc_code_fixed_bits(enc, 0x02, 3);
693 break;
694 default:
695 assert(0 && "Unsupported picture type!");
696 }
697
698 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
699
700 radeon_uvd_enc_byte_align(enc);
701 radeon_uvd_enc_flush_headers(enc);
702 *size_in_bytes = (enc->bits_output + 7) / 8;
703 RADEON_ENC_END();
704 }
705
706 static void
707 radeon_uvd_enc_slice_header_hevc(struct radeon_uvd_encoder *enc)
708 {
709 uint32_t instruction[RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = { 0 };
710 uint32_t num_bits[RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = { 0 };
711 unsigned int inst_index = 0;
712 unsigned int bit_index = 0;
713 unsigned int bits_copied = 0;
714 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_SLICE_HEADER);
715 radeon_uvd_enc_reset(enc);
716 radeon_uvd_enc_set_emulation_prevention(enc, false);
717
718 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
719 radeon_uvd_enc_code_fixed_bits(enc, enc->enc_pic.nal_unit_type, 6);
720 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 6);
721 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 3);
722
723 radeon_uvd_enc_flush_headers(enc);
724 bit_index++;
725 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;
726 num_bits[inst_index] = enc->bits_output - bits_copied;
727 bits_copied = enc->bits_output;
728 inst_index++;
729
730 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_FIRST_SLICE;
731 inst_index++;
732
733 if ((enc->enc_pic.nal_unit_type >= 16)
734 && (enc->enc_pic.nal_unit_type <= 23))
735 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
736
737 radeon_uvd_enc_code_ue(enc, 0x0);
738
739 radeon_uvd_enc_flush_headers(enc);
740 bit_index++;
741 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;
742 num_bits[inst_index] = enc->bits_output - bits_copied;
743 bits_copied = enc->bits_output;
744 inst_index++;
745
746 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_SLICE_SEGMENT;
747 inst_index++;
748
749 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_DEPENDENT_SLICE_END;
750 inst_index++;
751
752 switch (enc->enc_pic.picture_type) {
753 case PIPE_H265_ENC_PICTURE_TYPE_I:
754 case PIPE_H265_ENC_PICTURE_TYPE_IDR:
755 radeon_uvd_enc_code_ue(enc, 0x2);
756 break;
757 case PIPE_H265_ENC_PICTURE_TYPE_P:
758 case PIPE_H265_ENC_PICTURE_TYPE_SKIP:
759 radeon_uvd_enc_code_ue(enc, 0x1);
760 break;
761 case PIPE_H265_ENC_PICTURE_TYPE_B:
762 radeon_uvd_enc_code_ue(enc, 0x0);
763 break;
764 default:
765 radeon_uvd_enc_code_ue(enc, 0x1);
766 }
767
768 if ((enc->enc_pic.nal_unit_type != 19)
769 && (enc->enc_pic.nal_unit_type != 20)) {
770 radeon_uvd_enc_code_fixed_bits(enc,
771 enc->enc_pic.frame_num %
772 enc->enc_pic.max_poc,
773 enc->enc_pic.log2_max_poc);
774 if (enc->enc_pic.picture_type == PIPE_H265_ENC_PICTURE_TYPE_P)
775 radeon_uvd_enc_code_fixed_bits(enc, 0x1, 1);
776 else {
777 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
778 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
779 radeon_uvd_enc_code_ue(enc, 0x0);
780 radeon_uvd_enc_code_ue(enc, 0x0);
781 }
782 }
783
784 if (enc->enc_pic.sample_adaptive_offset_enabled_flag)
785 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1); /* slice_sao_luma_flag */
786
787 if ((enc->enc_pic.picture_type == PIPE_H265_ENC_PICTURE_TYPE_P) ||
788 (enc->enc_pic.picture_type == PIPE_H265_ENC_PICTURE_TYPE_B)) {
789 radeon_uvd_enc_code_fixed_bits(enc, 0x0, 1);
790 radeon_uvd_enc_code_fixed_bits(enc,
791 enc->enc_pic.hevc_spec_misc.
792 cabac_init_flag, 1);
793 radeon_uvd_enc_code_ue(enc, 5 - enc->enc_pic.max_num_merge_cand);
794 }
795
796 radeon_uvd_enc_flush_headers(enc);
797 bit_index++;
798 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;
799 num_bits[inst_index] = enc->bits_output - bits_copied;
800 bits_copied = enc->bits_output;
801 inst_index++;
802
803 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_SLICE_QP_DELTA;
804 inst_index++;
805
806 if ((enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled) &&
807 (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled)) {
808 radeon_uvd_enc_code_fixed_bits(enc,
809 enc->enc_pic.hevc_deblock.
810 loop_filter_across_slices_enabled, 1);
811
812 radeon_uvd_enc_flush_headers(enc);
813 bit_index++;
814 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_COPY;
815 num_bits[inst_index] = enc->bits_output - bits_copied;
816 bits_copied = enc->bits_output;
817 inst_index++;
818 }
819
820 instruction[inst_index] = RENC_UVD_HEADER_INSTRUCTION_END;
821
822 for (int i = bit_index;
823 i < RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS; i++)
824 RADEON_ENC_CS(0x00000000);
825
826 for (int j = 0; j < RENC_UVD_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS;
827 j++) {
828 RADEON_ENC_CS(instruction[j]);
829 RADEON_ENC_CS(num_bits[j]);
830 }
831
832 RADEON_ENC_END();
833 }
834
835 static void
836 radeon_uvd_enc_ctx(struct radeon_uvd_encoder *enc)
837 {
838 struct si_screen *rscreen = (struct si_screen *) enc->screen;
839
840 enc->enc_pic.ctx_buf.swizzle_mode = 0;
841 if (rscreen->info.chip_class < GFX9) {
842 enc->enc_pic.ctx_buf.rec_luma_pitch =
843 (enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe);
844 enc->enc_pic.ctx_buf.rec_chroma_pitch =
845 (enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe);
846 }
847 else {
848 enc->enc_pic.ctx_buf.rec_luma_pitch =
849 enc->luma->u.gfx9.surf_pitch * enc->luma->bpe;
850 enc->enc_pic.ctx_buf.rec_chroma_pitch =
851 enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe;
852 }
853 enc->enc_pic.ctx_buf.num_reconstructed_pictures = 2;
854
855 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_ENCODE_CONTEXT_BUFFER);
856 RADEON_ENC_READWRITE(enc->cpb.res->buf, enc->cpb.res->domains, 0);
857 RADEON_ENC_CS(0x00000000); // reserved
858 RADEON_ENC_CS(enc->enc_pic.ctx_buf.swizzle_mode);
859 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);
860 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);
861 RADEON_ENC_CS(enc->enc_pic.ctx_buf.num_reconstructed_pictures);
862 /* reconstructed_picture_1_luma_offset */
863 RADEON_ENC_CS(0x00000000);
864 /* reconstructed_picture_1_chroma_offset */
865 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch *
866 align(enc->base.height, 16));
867 /* reconstructed_picture_2_luma_offset */
868 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch *
869 align(enc->base.height, 16) * 3 / 2);
870 /* reconstructed_picture_2_chroma_offset */
871 RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch *
872 align(enc->base.height, 16) * 5 / 2);
873
874 for (int i = 0; i < 136; i++)
875 RADEON_ENC_CS(0x00000000);
876
877 RADEON_ENC_END();
878 }
879
880 static void
881 radeon_uvd_enc_bitstream(struct radeon_uvd_encoder *enc)
882 {
883 enc->enc_pic.bit_buf.mode = RENC_UVD_SWIZZLE_MODE_LINEAR;
884 enc->enc_pic.bit_buf.video_bitstream_buffer_size = enc->bs_size;
885 enc->enc_pic.bit_buf.video_bitstream_data_offset = 0;
886
887 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_VIDEO_BITSTREAM_BUFFER);
888 RADEON_ENC_CS(enc->enc_pic.bit_buf.mode);
889 RADEON_ENC_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT, 0);
890 RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_buffer_size);
891 RADEON_ENC_CS(enc->enc_pic.bit_buf.video_bitstream_data_offset);
892 RADEON_ENC_END();
893 }
894
895 static void
896 radeon_uvd_enc_feedback(struct radeon_uvd_encoder *enc)
897 {
898 enc->enc_pic.fb_buf.mode = RENC_UVD_FEEDBACK_BUFFER_MODE_LINEAR;
899 enc->enc_pic.fb_buf.feedback_buffer_size = 16;
900 enc->enc_pic.fb_buf.feedback_data_size = 40;
901
902 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_FEEDBACK_BUFFER);
903 RADEON_ENC_CS(enc->enc_pic.fb_buf.mode);
904 RADEON_ENC_WRITE(enc->fb->res->buf, enc->fb->res->domains, 0x0);
905 RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_buffer_size);
906 RADEON_ENC_CS(enc->enc_pic.fb_buf.feedback_data_size);
907 RADEON_ENC_END();
908 }
909
910 static void
911 radeon_uvd_enc_intra_refresh(struct radeon_uvd_encoder *enc)
912 {
913 enc->enc_pic.intra_ref.intra_refresh_mode =
914 RENC_UVD_INTRA_REFRESH_MODE_NONE;
915 enc->enc_pic.intra_ref.offset = 0;
916 enc->enc_pic.intra_ref.region_size = 0;
917
918 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_INTRA_REFRESH);
919 RADEON_ENC_CS(enc->enc_pic.intra_ref.intra_refresh_mode);
920 RADEON_ENC_CS(enc->enc_pic.intra_ref.offset);
921 RADEON_ENC_CS(enc->enc_pic.intra_ref.region_size);
922 RADEON_ENC_END();
923 }
924
925 static void
926 radeon_uvd_enc_rc_per_pic(struct radeon_uvd_encoder *enc,
927 struct pipe_picture_desc *picture)
928 {
929 struct pipe_h265_enc_picture_desc *pic =
930 (struct pipe_h265_enc_picture_desc *) picture;
931 enc->enc_pic.rc_per_pic.qp = pic->rc.quant_i_frames;
932 enc->enc_pic.rc_per_pic.min_qp_app = 0;
933 enc->enc_pic.rc_per_pic.max_qp_app = 51;
934 enc->enc_pic.rc_per_pic.max_au_size = 0;
935 enc->enc_pic.rc_per_pic.enabled_filler_data = pic->rc.fill_data_enable;
936 enc->enc_pic.rc_per_pic.skip_frame_enable = false;
937 enc->enc_pic.rc_per_pic.enforce_hrd = pic->rc.enforce_hrd;
938
939 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_RATE_CONTROL_PER_PICTURE);
940 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.qp);
941 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.min_qp_app);
942 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_qp_app);
943 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.max_au_size);
944 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enabled_filler_data);
945 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.skip_frame_enable);
946 RADEON_ENC_CS(enc->enc_pic.rc_per_pic.enforce_hrd);
947 RADEON_ENC_END();
948 }
949
950 static void
951 radeon_uvd_enc_encode_params_hevc(struct radeon_uvd_encoder *enc)
952 {
953 struct si_screen *rscreen = (struct si_screen *) enc->screen;
954 switch (enc->enc_pic.picture_type) {
955 case PIPE_H265_ENC_PICTURE_TYPE_I:
956 case PIPE_H265_ENC_PICTURE_TYPE_IDR:
957 enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_I;
958 break;
959 case PIPE_H265_ENC_PICTURE_TYPE_P:
960 enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_P;
961 break;
962 case PIPE_H265_ENC_PICTURE_TYPE_SKIP:
963 enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_P_SKIP;
964 break;
965 case PIPE_H265_ENC_PICTURE_TYPE_B:
966 enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_B;
967 break;
968 default:
969 enc->enc_pic.enc_params.pic_type = RENC_UVD_PICTURE_TYPE_I;
970 }
971
972 enc->enc_pic.enc_params.allowed_max_bitstream_size = enc->bs_size;
973 if (rscreen->info.chip_class < GFX9) {
974 enc->enc_pic.enc_params.input_pic_luma_pitch =
975 (enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe);
976 enc->enc_pic.enc_params.input_pic_chroma_pitch =
977 (enc->chroma->u.legacy.level[0].nblk_x * enc->chroma->bpe);
978 }
979 else {
980 enc->enc_pic.enc_params.input_pic_luma_pitch =
981 enc->luma->u.gfx9.surf_pitch * enc->luma->bpe;
982 enc->enc_pic.enc_params.input_pic_chroma_pitch =
983 enc->chroma->u.gfx9.surf_pitch * enc->chroma->bpe;
984 }
985 enc->enc_pic.enc_params.input_pic_swizzle_mode =
986 RENC_UVD_SWIZZLE_MODE_LINEAR;
987
988 if (enc->enc_pic.enc_params.pic_type == RENC_UVD_PICTURE_TYPE_I)
989 enc->enc_pic.enc_params.reference_picture_index = 0xFFFFFFFF;
990 else
991 enc->enc_pic.enc_params.reference_picture_index =
992 (enc->enc_pic.frame_num - 1) % 2;
993
994 enc->enc_pic.enc_params.reconstructed_picture_index =
995 enc->enc_pic.frame_num % 2;
996
997 RADEON_ENC_BEGIN(RENC_UVD_IB_PARAM_ENCODE_PARAMS);
998 RADEON_ENC_CS(enc->enc_pic.enc_params.pic_type);
999 RADEON_ENC_CS(enc->enc_pic.enc_params.allowed_max_bitstream_size);
1000
1001 if (rscreen->info.chip_class < GFX9) {
1002 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM,
1003 enc->luma->u.legacy.level[0].offset);
1004 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM,
1005 enc->chroma->u.legacy.level[0].offset);
1006 }
1007 else {
1008 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM,
1009 enc->luma->u.gfx9.surf_offset);
1010 RADEON_ENC_READ(enc->handle, RADEON_DOMAIN_VRAM,
1011 enc->chroma->u.gfx9.surf_offset);
1012 }
1013 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_luma_pitch);
1014 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_chroma_pitch);
1015 RADEON_ENC_CS(0x00000000); // reserved
1016 RADEON_ENC_CS(enc->enc_pic.enc_params.input_pic_swizzle_mode);
1017 RADEON_ENC_CS(enc->enc_pic.enc_params.reference_picture_index);
1018 RADEON_ENC_CS(enc->enc_pic.enc_params.reconstructed_picture_index);
1019 RADEON_ENC_END();
1020 }
1021
1022 static void
1023 radeon_uvd_enc_op_init(struct radeon_uvd_encoder *enc)
1024 {
1025 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INITIALIZE);
1026 RADEON_ENC_END();
1027 }
1028
1029 static void
1030 radeon_uvd_enc_op_close(struct radeon_uvd_encoder *enc)
1031 {
1032 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_CLOSE_SESSION);
1033 RADEON_ENC_END();
1034 }
1035
1036 static void
1037 radeon_uvd_enc_op_enc(struct radeon_uvd_encoder *enc)
1038 {
1039 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_ENCODE);
1040 RADEON_ENC_END();
1041 }
1042
1043 static void
1044 radeon_uvd_enc_op_init_rc(struct radeon_uvd_encoder *enc)
1045 {
1046 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INIT_RC);
1047 RADEON_ENC_END();
1048 }
1049
1050 static void
1051 radeon_uvd_enc_op_init_rc_vbv(struct radeon_uvd_encoder *enc)
1052 {
1053 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_INIT_RC_VBV_BUFFER_LEVEL);
1054 RADEON_ENC_END();
1055 }
1056
1057 static void
1058 radeon_uvd_enc_op_speed(struct radeon_uvd_encoder *enc)
1059 {
1060 RADEON_ENC_BEGIN(RENC_UVD_IB_OP_SET_SPEED_ENCODING_MODE);
1061 RADEON_ENC_END();
1062 }
1063
1064 static void
1065 begin(struct radeon_uvd_encoder *enc, struct pipe_picture_desc *pic)
1066 {
1067 radeon_uvd_enc_session_info(enc);
1068 enc->total_task_size = 0;
1069 radeon_uvd_enc_task_info(enc, enc->need_feedback);
1070 radeon_uvd_enc_op_init(enc);
1071
1072 radeon_uvd_enc_session_init_hevc(enc);
1073 radeon_uvd_enc_slice_control_hevc(enc);
1074 radeon_uvd_enc_spec_misc_hevc(enc, pic);
1075 radeon_uvd_enc_deblocking_filter_hevc(enc, pic);
1076
1077 radeon_uvd_enc_layer_control(enc);
1078 radeon_uvd_enc_rc_session_init(enc, pic);
1079 radeon_uvd_enc_quality_params(enc);
1080 radeon_uvd_enc_layer_select(enc);
1081 radeon_uvd_enc_rc_layer_init(enc, pic);
1082 radeon_uvd_enc_layer_select(enc);
1083 radeon_uvd_enc_rc_per_pic(enc, pic);
1084 radeon_uvd_enc_op_init_rc(enc);
1085 radeon_uvd_enc_op_init_rc_vbv(enc);
1086 *enc->p_task_size = (enc->total_task_size);
1087 }
1088
1089 static void
1090 encode(struct radeon_uvd_encoder *enc)
1091 {
1092 radeon_uvd_enc_session_info(enc);
1093 enc->total_task_size = 0;
1094 radeon_uvd_enc_task_info(enc, enc->need_feedback);
1095
1096 radeon_uvd_enc_nalu_aud_hevc(enc);
1097
1098 if (enc->enc_pic.is_iframe) {
1099 radeon_uvd_enc_nalu_vps_hevc(enc);
1100 radeon_uvd_enc_nalu_pps_hevc(enc);
1101 radeon_uvd_enc_nalu_sps_hevc(enc);
1102 }
1103 radeon_uvd_enc_slice_header_hevc(enc);
1104 radeon_uvd_enc_encode_params_hevc(enc);
1105
1106 radeon_uvd_enc_ctx(enc);
1107 radeon_uvd_enc_bitstream(enc);
1108 radeon_uvd_enc_feedback(enc);
1109 radeon_uvd_enc_intra_refresh(enc);
1110
1111 radeon_uvd_enc_op_speed(enc);
1112 radeon_uvd_enc_op_enc(enc);
1113 *enc->p_task_size = (enc->total_task_size);
1114 }
1115
1116 static void
1117 destroy(struct radeon_uvd_encoder *enc)
1118 {
1119 radeon_uvd_enc_session_info(enc);
1120 enc->total_task_size = 0;
1121 radeon_uvd_enc_task_info(enc, enc->need_feedback);
1122 radeon_uvd_enc_op_close(enc);
1123 *enc->p_task_size = (enc->total_task_size);
1124 }
1125
1126 void
1127 radeon_uvd_enc_1_1_init(struct radeon_uvd_encoder *enc)
1128 {
1129 enc->begin = begin;
1130 enc->encode = encode;
1131 enc->destroy = destroy;
1132 }