1 /**************************************************************************
2  *
3  * Copyright 2017 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_vcn_enc.h"
30 #include "radeon_video.h"
31 #include "si_pipe.h"
32 #include "util/u_video.h"
33 
34 #include <stdio.h>
35 
36 #define RENCODE_FW_INTERFACE_MAJOR_VERSION         1
37 #define RENCODE_FW_INTERFACE_MINOR_VERSION         1
38 
39 #define RENCODE_IB_PARAM_SESSION_INFO              0x00000001
40 #define RENCODE_IB_PARAM_TASK_INFO                 0x00000002
41 #define RENCODE_IB_PARAM_SESSION_INIT              0x00000003
42 #define RENCODE_IB_PARAM_LAYER_CONTROL             0x00000004
43 #define RENCODE_IB_PARAM_LAYER_SELECT              0x00000005
44 #define RENCODE_IB_PARAM_RATE_CONTROL_SESSION_INIT 0x00000006
45 #define RENCODE_IB_PARAM_RATE_CONTROL_LAYER_INIT   0x00000007
46 #define RENCODE_IB_PARAM_RATE_CONTROL_PER_PICTURE  0x00000008
47 #define RENCODE_IB_PARAM_QUALITY_PARAMS            0x00000009
48 #define RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU        0x0000000a
49 #define RENCODE_IB_PARAM_SLICE_HEADER              0x0000000b
50 #define RENCODE_IB_PARAM_INPUT_FORMAT              0x0000000c
51 #define RENCODE_IB_PARAM_OUTPUT_FORMAT             0x0000000d
52 #define RENCODE_IB_PARAM_ENCODE_PARAMS             0x0000000f
53 #define RENCODE_IB_PARAM_INTRA_REFRESH             0x00000010
54 #define RENCODE_IB_PARAM_ENCODE_CONTEXT_BUFFER     0x00000011
55 #define RENCODE_IB_PARAM_VIDEO_BITSTREAM_BUFFER    0x00000012
56 #define RENCODE_IB_PARAM_FEEDBACK_BUFFER           0x00000015
57 
58 #define RENCODE_HEVC_IB_PARAM_SLICE_CONTROL        0x00100001
59 #define RENCODE_HEVC_IB_PARAM_SPEC_MISC            0x00100002
60 #define RENCODE_HEVC_IB_PARAM_LOOP_FILTER          0x00100003
61 
62 #define RENCODE_H264_IB_PARAM_SLICE_CONTROL        0x00200001
63 #define RENCODE_H264_IB_PARAM_SPEC_MISC            0x00200002
64 #define RENCODE_H264_IB_PARAM_ENCODE_PARAMS        0x00200003
65 #define RENCODE_H264_IB_PARAM_DEBLOCKING_FILTER    0x00200004
66 
67 #define RENCODE_COLOR_VOLUME_G22_BT709             0
68 #define RENCODE_COLOR_VOLUME_G10_BT2020            3
69 
70 #define RENCODE_COLOR_BIT_DEPTH_8_BIT              0
71 #define RENCODE_COLOR_BIT_DEPTH_10_BIT             1
72 
73 #define RENCODE_COLOR_PACKING_FORMAT_NV12          0
74 #define RENCODE_COLOR_PACKING_FORMAT_P010          1
75 
radeon_enc_op_balance(struct radeon_encoder * enc)76 static void radeon_enc_op_balance(struct radeon_encoder *enc)
77 {
78    RADEON_ENC_BEGIN(RENCODE_IB_OP_SET_BALANCE_ENCODING_MODE);
79    RADEON_ENC_END();
80 }
81 
radeon_enc_slice_header_hevc(struct radeon_encoder * enc)82 static void radeon_enc_slice_header_hevc(struct radeon_encoder *enc)
83 {
84    uint32_t instruction[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};
85    uint32_t num_bits[RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS] = {0};
86    unsigned int inst_index = 0;
87    unsigned int cdw_start = 0;
88    unsigned int cdw_filled = 0;
89    unsigned int bits_copied = 0;
90    RADEON_ENC_BEGIN(enc->cmd.slice_header);
91    radeon_enc_reset(enc);
92    radeon_enc_set_emulation_prevention(enc, false);
93 
94    cdw_start = enc->cs.current.cdw;
95    radeon_enc_code_fixed_bits(enc, 0x0, 1);
96    radeon_enc_code_fixed_bits(enc, enc->enc_pic.nal_unit_type, 6);
97    radeon_enc_code_fixed_bits(enc, 0x0, 6);
98    radeon_enc_code_fixed_bits(enc, 0x1, 3);
99 
100    radeon_enc_flush_headers(enc);
101    instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
102    num_bits[inst_index] = enc->bits_output - bits_copied;
103    bits_copied = enc->bits_output;
104    inst_index++;
105 
106    instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_FIRST_SLICE;
107    inst_index++;
108 
109    if ((enc->enc_pic.nal_unit_type >= 16) && (enc->enc_pic.nal_unit_type <= 23))
110       radeon_enc_code_fixed_bits(enc, 0x0, 1);
111 
112    radeon_enc_code_ue(enc, 0x0);
113 
114    radeon_enc_flush_headers(enc);
115    instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
116    num_bits[inst_index] = enc->bits_output - bits_copied;
117    bits_copied = enc->bits_output;
118    inst_index++;
119 
120    instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_SEGMENT;
121    inst_index++;
122 
123    instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_DEPENDENT_SLICE_END;
124    inst_index++;
125 
126    switch (enc->enc_pic.picture_type) {
127    case PIPE_H2645_ENC_PICTURE_TYPE_I:
128    case PIPE_H2645_ENC_PICTURE_TYPE_IDR:
129       radeon_enc_code_ue(enc, 0x2);
130       break;
131    case PIPE_H2645_ENC_PICTURE_TYPE_P:
132    case PIPE_H2645_ENC_PICTURE_TYPE_SKIP:
133       radeon_enc_code_ue(enc, 0x1);
134       break;
135    case PIPE_H2645_ENC_PICTURE_TYPE_B:
136       radeon_enc_code_ue(enc, 0x0);
137       break;
138    default:
139       radeon_enc_code_ue(enc, 0x1);
140    }
141 
142    if ((enc->enc_pic.nal_unit_type != 19) && (enc->enc_pic.nal_unit_type != 20)) {
143       radeon_enc_code_fixed_bits(enc, enc->enc_pic.pic_order_cnt, enc->enc_pic.log2_max_poc);
144       if (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P)
145          radeon_enc_code_fixed_bits(enc, 0x1, 1);
146       else {
147          radeon_enc_code_fixed_bits(enc, 0x0, 1);
148          radeon_enc_code_fixed_bits(enc, 0x0, 1);
149          radeon_enc_code_ue(enc, 0x0);
150          radeon_enc_code_ue(enc, 0x0);
151       }
152    }
153 
154    if (enc->enc_pic.sample_adaptive_offset_enabled_flag) {
155       radeon_enc_flush_headers(enc);
156       instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
157       num_bits[inst_index] = enc->bits_output - bits_copied;
158       bits_copied = enc->bits_output;
159       inst_index++;
160 
161       instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_SAO_ENABLE;
162       inst_index++;
163    }
164 
165    if ((enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P) ||
166        (enc->enc_pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B)) {
167       radeon_enc_code_fixed_bits(enc, 0x0, 1);
168       radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.cabac_init_flag, 1);
169       radeon_enc_code_ue(enc, 5 - enc->enc_pic.max_num_merge_cand);
170    }
171 
172    radeon_enc_flush_headers(enc);
173    instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
174    num_bits[inst_index] = enc->bits_output - bits_copied;
175    bits_copied = enc->bits_output;
176    inst_index++;
177 
178    instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_SLICE_QP_DELTA;
179    inst_index++;
180 
181    if ((enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled) &&
182        (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled ||
183         enc->enc_pic.sample_adaptive_offset_enabled_flag)) {
184        if (enc->enc_pic.sample_adaptive_offset_enabled_flag) {
185            radeon_enc_flush_headers(enc);
186            instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
187            num_bits[inst_index] = enc->bits_output - bits_copied;
188            bits_copied = enc->bits_output;
189            inst_index++;
190 
191            instruction[inst_index] = RENCODE_HEVC_HEADER_INSTRUCTION_LOOP_FILTER_ACROSS_SLICES_ENABLE;
192            inst_index++;
193        }
194        else
195            radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled, 1);
196    }
197 
198    radeon_enc_flush_headers(enc);
199    instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_COPY;
200    num_bits[inst_index] = enc->bits_output - bits_copied;
201    bits_copied = enc->bits_output;
202    inst_index++;
203    instruction[inst_index] = RENCODE_HEADER_INSTRUCTION_END;
204 
205    cdw_filled = enc->cs.current.cdw - cdw_start;
206    for (int i = 0; i < RENCODE_SLICE_HEADER_TEMPLATE_MAX_TEMPLATE_SIZE_IN_DWORDS - cdw_filled; i++)
207       RADEON_ENC_CS(0x00000000);
208 
209    for (int j = 0; j < RENCODE_SLICE_HEADER_TEMPLATE_MAX_NUM_INSTRUCTIONS; j++) {
210       RADEON_ENC_CS(instruction[j]);
211       RADEON_ENC_CS(num_bits[j]);
212    }
213 
214    RADEON_ENC_END();
215 }
216 
radeon_enc_quality_params(struct radeon_encoder * enc)217 static void radeon_enc_quality_params(struct radeon_encoder *enc)
218 {
219    enc->enc_pic.quality_params.vbaq_mode = 0;
220    enc->enc_pic.quality_params.scene_change_sensitivity = 0;
221    enc->enc_pic.quality_params.scene_change_min_idr_interval = 0;
222    enc->enc_pic.quality_params.two_pass_search_center_map_mode = 0;
223 
224    RADEON_ENC_BEGIN(enc->cmd.quality_params);
225    RADEON_ENC_CS(enc->enc_pic.quality_params.vbaq_mode);
226    RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_sensitivity);
227    RADEON_ENC_CS(enc->enc_pic.quality_params.scene_change_min_idr_interval);
228    RADEON_ENC_CS(enc->enc_pic.quality_params.two_pass_search_center_map_mode);
229    RADEON_ENC_END();
230 }
231 
radeon_enc_loop_filter_hevc(struct radeon_encoder * enc)232 static void radeon_enc_loop_filter_hevc(struct radeon_encoder *enc)
233 {
234    RADEON_ENC_BEGIN(enc->cmd.deblocking_filter_hevc);
235    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled);
236    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.deblocking_filter_disabled);
237    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.beta_offset_div2);
238    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.tc_offset_div2);
239    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cb_qp_offset);
240    RADEON_ENC_CS(enc->enc_pic.hevc_deblock.cr_qp_offset);
241    RADEON_ENC_CS(!enc->enc_pic.sample_adaptive_offset_enabled_flag);
242    RADEON_ENC_END();
243 }
244 
radeon_enc_nalu_sps_hevc(struct radeon_encoder * enc)245 static void radeon_enc_nalu_sps_hevc(struct radeon_encoder *enc)
246 {
247    RADEON_ENC_BEGIN(enc->cmd.nalu);
248    RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_SPS);
249    uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];
250    int i;
251 
252    radeon_enc_reset(enc);
253    radeon_enc_set_emulation_prevention(enc, false);
254    radeon_enc_code_fixed_bits(enc, 0x00000001, 32);
255    radeon_enc_code_fixed_bits(enc, 0x4201, 16);
256    radeon_enc_byte_align(enc);
257    radeon_enc_set_emulation_prevention(enc, true);
258    radeon_enc_code_fixed_bits(enc, 0x0, 4);
259    radeon_enc_code_fixed_bits(enc, enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1, 3);
260    radeon_enc_code_fixed_bits(enc, 0x1, 1);
261    radeon_enc_code_fixed_bits(enc, 0x0, 2);
262    radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_tier_flag, 1);
263    radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_profile_idc, 5);
264 
265    if (enc->enc_pic.general_profile_idc == 2)
266       radeon_enc_code_fixed_bits(enc, 0x20000000, 32);
267    else
268       radeon_enc_code_fixed_bits(enc, 0x60000000, 32);
269 
270    radeon_enc_code_fixed_bits(enc, 0xb0000000, 32);
271    radeon_enc_code_fixed_bits(enc, 0x0, 16);
272    radeon_enc_code_fixed_bits(enc, enc->enc_pic.general_level_idc, 8);
273 
274    for (i = 0; i < (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i++)
275       radeon_enc_code_fixed_bits(enc, 0x0, 2);
276 
277    if ((enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1) > 0) {
278       for (i = (enc->enc_pic.layer_ctrl.max_num_temporal_layers - 1); i < 8; i++)
279          radeon_enc_code_fixed_bits(enc, 0x0, 2);
280    }
281 
282    radeon_enc_code_ue(enc, 0x0);
283    radeon_enc_code_ue(enc, enc->enc_pic.chroma_format_idc);
284    radeon_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_width);
285    radeon_enc_code_ue(enc, enc->enc_pic.session_init.aligned_picture_height);
286 
287    if ((enc->enc_pic.crop_left != 0) || (enc->enc_pic.crop_right  != 0) ||
288        (enc->enc_pic.crop_top  != 0) || (enc->enc_pic.crop_bottom != 0)) {
289       radeon_enc_code_fixed_bits(enc, 0x1, 1);
290       radeon_enc_code_ue(enc, enc->enc_pic.crop_left);
291       radeon_enc_code_ue(enc, enc->enc_pic.crop_right);
292       radeon_enc_code_ue(enc, enc->enc_pic.crop_top);
293       radeon_enc_code_ue(enc, enc->enc_pic.crop_bottom);
294    } else if (enc->enc_pic.session_init.padding_width  != 0 ||
295               enc->enc_pic.session_init.padding_height != 0) {
296       radeon_enc_code_fixed_bits(enc, 0x1, 1);
297       radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_width / 2);
298       radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_width / 2);
299       radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_height / 2);
300       radeon_enc_code_ue(enc, enc->enc_pic.session_init.padding_height / 2);
301    } else
302    radeon_enc_code_fixed_bits(enc, 0x0, 1);
303 
304    radeon_enc_code_ue(enc, enc->enc_pic.bit_depth_luma_minus8);
305    radeon_enc_code_ue(enc, enc->enc_pic.bit_depth_chroma_minus8);
306    radeon_enc_code_ue(enc, enc->enc_pic.log2_max_poc - 4);
307    radeon_enc_code_fixed_bits(enc, 0x0, 1);
308    radeon_enc_code_ue(enc, 1);
309    radeon_enc_code_ue(enc, 0x0);
310    radeon_enc_code_ue(enc, 0x0);
311    radeon_enc_code_ue(enc, enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3);
312    // Only support CTBSize 64
313    radeon_enc_code_ue(enc,
314                       6 - (enc->enc_pic.hevc_spec_misc.log2_min_luma_coding_block_size_minus3 + 3));
315    radeon_enc_code_ue(enc, enc->enc_pic.log2_min_transform_block_size_minus2);
316    radeon_enc_code_ue(enc, enc->enc_pic.log2_diff_max_min_transform_block_size);
317    radeon_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_inter);
318    radeon_enc_code_ue(enc, enc->enc_pic.max_transform_hierarchy_depth_intra);
319 
320    radeon_enc_code_fixed_bits(enc, 0x0, 1);
321    radeon_enc_code_fixed_bits(enc, !enc->enc_pic.hevc_spec_misc.amp_disabled, 1);
322    radeon_enc_code_fixed_bits(enc, enc->enc_pic.sample_adaptive_offset_enabled_flag, 1);
323    radeon_enc_code_fixed_bits(enc, enc->enc_pic.pcm_enabled_flag, 1);
324 
325    radeon_enc_code_ue(enc, 1);
326    radeon_enc_code_ue(enc, 1);
327    radeon_enc_code_ue(enc, 0);
328    radeon_enc_code_ue(enc, 0);
329    radeon_enc_code_fixed_bits(enc, 0x1, 1);
330 
331    radeon_enc_code_fixed_bits(enc, 0x0, 1);
332 
333    radeon_enc_code_fixed_bits(enc, 0, 1);
334    radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.strong_intra_smoothing_enabled, 1);
335 
336    radeon_enc_code_fixed_bits(enc, 0x0, 1);
337 
338    radeon_enc_code_fixed_bits(enc, 0x0, 1);
339 
340    radeon_enc_code_fixed_bits(enc, 0x1, 1);
341 
342    radeon_enc_byte_align(enc);
343    radeon_enc_flush_headers(enc);
344    *size_in_bytes = (enc->bits_output + 7) / 8;
345    RADEON_ENC_END();
346 }
347 
radeon_enc_nalu_pps_hevc(struct radeon_encoder * enc)348 static void radeon_enc_nalu_pps_hevc(struct radeon_encoder *enc)
349 {
350    RADEON_ENC_BEGIN(enc->cmd.nalu);
351    RADEON_ENC_CS(RENCODE_DIRECT_OUTPUT_NALU_TYPE_PPS);
352    uint32_t *size_in_bytes = &enc->cs.current.buf[enc->cs.current.cdw++];
353    radeon_enc_reset(enc);
354    radeon_enc_set_emulation_prevention(enc, false);
355    radeon_enc_code_fixed_bits(enc, 0x00000001, 32);
356    radeon_enc_code_fixed_bits(enc, 0x4401, 16);
357    radeon_enc_byte_align(enc);
358    radeon_enc_set_emulation_prevention(enc, true);
359    radeon_enc_code_ue(enc, 0x0);
360    radeon_enc_code_ue(enc, 0x0);
361    radeon_enc_code_fixed_bits(enc, 0x1, 1);
362    radeon_enc_code_fixed_bits(enc, 0x0, 4);
363    radeon_enc_code_fixed_bits(enc, 0x0, 1);
364    radeon_enc_code_fixed_bits(enc, 0x1, 1);
365    radeon_enc_code_ue(enc, 0x0);
366    radeon_enc_code_ue(enc, 0x0);
367    radeon_enc_code_se(enc, 0x0);
368    radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_spec_misc.constrained_intra_pred_flag, 1);
369    radeon_enc_code_fixed_bits(enc, 0x0, 1);
370    if (enc->enc_pic.rc_session_init.rate_control_method == RENCODE_RATE_CONTROL_METHOD_NONE)
371       radeon_enc_code_fixed_bits(enc, 0x0, 1);
372    else {
373       radeon_enc_code_fixed_bits(enc, 0x1, 1);
374       radeon_enc_code_ue(enc, 0x0);
375    }
376    radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cb_qp_offset);
377    radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.cr_qp_offset);
378    radeon_enc_code_fixed_bits(enc, 0x0, 1);
379    radeon_enc_code_fixed_bits(enc, 0x0, 2);
380    radeon_enc_code_fixed_bits(enc, 0x0, 1);
381    radeon_enc_code_fixed_bits(enc, 0x0, 1);
382    radeon_enc_code_fixed_bits(enc, 0x0, 1);
383    radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.loop_filter_across_slices_enabled, 1);
384    radeon_enc_code_fixed_bits(enc, 0x1, 1);
385    radeon_enc_code_fixed_bits(enc, 0x0, 1);
386    radeon_enc_code_fixed_bits(enc, enc->enc_pic.hevc_deblock.deblocking_filter_disabled, 1);
387 
388    if (!enc->enc_pic.hevc_deblock.deblocking_filter_disabled) {
389       radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.beta_offset_div2);
390       radeon_enc_code_se(enc, enc->enc_pic.hevc_deblock.tc_offset_div2);
391    }
392 
393    radeon_enc_code_fixed_bits(enc, 0x0, 1);
394    radeon_enc_code_fixed_bits(enc, 0x0, 1);
395    radeon_enc_code_ue(enc, enc->enc_pic.log2_parallel_merge_level_minus2);
396    radeon_enc_code_fixed_bits(enc, 0x0, 2);
397 
398    radeon_enc_code_fixed_bits(enc, 0x1, 1);
399 
400    radeon_enc_byte_align(enc);
401    radeon_enc_flush_headers(enc);
402    *size_in_bytes = (enc->bits_output + 7) / 8;
403    RADEON_ENC_END();
404 }
405 
radeon_enc_input_format(struct radeon_encoder * enc)406 static void radeon_enc_input_format(struct radeon_encoder *enc)
407 {
408    RADEON_ENC_BEGIN(enc->cmd.input_format);
409    if (enc->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
410       RADEON_ENC_CS(RENCODE_COLOR_VOLUME_G10_BT2020);
411       RADEON_ENC_CS(0);
412       RADEON_ENC_CS(0);
413       RADEON_ENC_CS(0);
414       RADEON_ENC_CS(0);
415       RADEON_ENC_CS(RENCODE_COLOR_BIT_DEPTH_10_BIT);
416       RADEON_ENC_CS(RENCODE_COLOR_PACKING_FORMAT_P010);
417    } else {
418       RADEON_ENC_CS(RENCODE_COLOR_VOLUME_G22_BT709);
419       RADEON_ENC_CS(0);
420       RADEON_ENC_CS(0);
421       RADEON_ENC_CS(0);
422       RADEON_ENC_CS(0);
423       RADEON_ENC_CS(RENCODE_COLOR_BIT_DEPTH_8_BIT);
424       RADEON_ENC_CS(RENCODE_COLOR_PACKING_FORMAT_NV12);
425    }
426    RADEON_ENC_END();
427 }
428 
radeon_enc_output_format(struct radeon_encoder * enc)429 static void radeon_enc_output_format(struct radeon_encoder *enc)
430 {
431    RADEON_ENC_BEGIN(enc->cmd.output_format);
432    if (enc->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
433       RADEON_ENC_CS(RENCODE_COLOR_VOLUME_G10_BT2020);
434       RADEON_ENC_CS(0);
435       RADEON_ENC_CS(0);
436       RADEON_ENC_CS(RENCODE_COLOR_BIT_DEPTH_10_BIT);
437    } else {
438       RADEON_ENC_CS(RENCODE_COLOR_VOLUME_G22_BT709);
439       RADEON_ENC_CS(0);
440       RADEON_ENC_CS(0);
441       RADEON_ENC_CS(RENCODE_COLOR_BIT_DEPTH_8_BIT);
442    }
443    RADEON_ENC_END();
444 }
445 
radeon_enc_ctx(struct radeon_encoder * enc)446 static void radeon_enc_ctx(struct radeon_encoder *enc)
447 {
448    enc->enc_pic.ctx_buf.swizzle_mode = 0;
449 
450    uint32_t aligned_width = enc->enc_pic.session_init.aligned_picture_width;
451    uint32_t aligned_height = enc->enc_pic.session_init.aligned_picture_height;
452 
453    enc->enc_pic.ctx_buf.rec_luma_pitch = align(aligned_width, enc->alignment);
454    enc->enc_pic.ctx_buf.rec_chroma_pitch = align(aligned_width, enc->alignment);
455 
456    int luma_size = enc->enc_pic.ctx_buf.rec_luma_pitch * align(aligned_height, enc->alignment);
457    if (enc->enc_pic.bit_depth_luma_minus8 == 2)
458       luma_size *= 2;
459    int chroma_size = align(luma_size / 2, enc->alignment);
460    int offset = 0;
461 
462    enc->enc_pic.ctx_buf.num_reconstructed_pictures = 2;
463    for (int i = 0; i < enc->enc_pic.ctx_buf.num_reconstructed_pictures; i++) {
464       enc->enc_pic.ctx_buf.reconstructed_pictures[i].luma_offset = offset;
465       offset += luma_size;
466       enc->enc_pic.ctx_buf.reconstructed_pictures[i].chroma_offset = offset;
467       offset += chroma_size;
468    }
469 
470    RADEON_ENC_BEGIN(enc->cmd.ctx);
471    RADEON_ENC_READWRITE(enc->cpb.res->buf, enc->cpb.res->domains, 0);
472    RADEON_ENC_CS(enc->enc_pic.ctx_buf.swizzle_mode);
473    RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_luma_pitch);
474    RADEON_ENC_CS(enc->enc_pic.ctx_buf.rec_chroma_pitch);
475    RADEON_ENC_CS(enc->enc_pic.ctx_buf.num_reconstructed_pictures);
476 
477    for (int i = 0; i < enc->enc_pic.ctx_buf.num_reconstructed_pictures; i++) {
478       RADEON_ENC_CS(enc->enc_pic.ctx_buf.reconstructed_pictures[i].luma_offset);
479       RADEON_ENC_CS(enc->enc_pic.ctx_buf.reconstructed_pictures[i].chroma_offset);
480    }
481 
482    for (int i = 0; i < 136; i++)
483       RADEON_ENC_CS(0x00000000);
484 
485    RADEON_ENC_END();
486 }
487 
encode(struct radeon_encoder * enc)488 static void encode(struct radeon_encoder *enc)
489 {
490    enc->session_info(enc);
491    enc->total_task_size = 0;
492    enc->task_info(enc, enc->need_feedback);
493 
494    enc->encode_headers(enc);
495    enc->ctx(enc);
496    enc->bitstream(enc);
497    enc->feedback(enc);
498    enc->intra_refresh(enc);
499    enc->input_format(enc);
500    enc->output_format(enc);
501 
502    enc->op_preset(enc);
503    enc->op_enc(enc);
504    *enc->p_task_size = (enc->total_task_size);
505 }
506 
radeon_enc_2_0_init(struct radeon_encoder * enc)507 void radeon_enc_2_0_init(struct radeon_encoder *enc)
508 {
509    radeon_enc_1_2_init(enc);
510    enc->encode = encode;
511    enc->ctx = radeon_enc_ctx;
512    enc->quality_params = radeon_enc_quality_params;
513    enc->input_format = radeon_enc_input_format;
514    enc->output_format = radeon_enc_output_format;
515 
516    if (u_reduce_video_profile(enc->base.profile) == PIPE_VIDEO_FORMAT_HEVC) {
517       enc->deblocking_filter = radeon_enc_loop_filter_hevc;
518       enc->nalu_sps = radeon_enc_nalu_sps_hevc;
519       enc->nalu_pps = radeon_enc_nalu_pps_hevc;
520       enc->slice_header = radeon_enc_slice_header_hevc;
521       enc->op_preset = radeon_enc_op_balance;
522    }
523 
524    enc->cmd.session_info = RENCODE_IB_PARAM_SESSION_INFO;
525    enc->cmd.task_info = RENCODE_IB_PARAM_TASK_INFO;
526    enc->cmd.session_init = RENCODE_IB_PARAM_SESSION_INIT;
527    enc->cmd.layer_control = RENCODE_IB_PARAM_LAYER_CONTROL;
528    enc->cmd.layer_select = RENCODE_IB_PARAM_LAYER_SELECT;
529    enc->cmd.rc_session_init = RENCODE_IB_PARAM_RATE_CONTROL_SESSION_INIT;
530    enc->cmd.rc_layer_init = RENCODE_IB_PARAM_RATE_CONTROL_LAYER_INIT;
531    enc->cmd.rc_per_pic = RENCODE_IB_PARAM_RATE_CONTROL_PER_PICTURE;
532    enc->cmd.quality_params = RENCODE_IB_PARAM_QUALITY_PARAMS;
533    enc->cmd.nalu = RENCODE_IB_PARAM_DIRECT_OUTPUT_NALU;
534    enc->cmd.slice_header = RENCODE_IB_PARAM_SLICE_HEADER;
535    enc->cmd.input_format = RENCODE_IB_PARAM_INPUT_FORMAT;
536    enc->cmd.output_format = RENCODE_IB_PARAM_OUTPUT_FORMAT;
537    enc->cmd.enc_params = RENCODE_IB_PARAM_ENCODE_PARAMS;
538    enc->cmd.intra_refresh = RENCODE_IB_PARAM_INTRA_REFRESH;
539    enc->cmd.ctx = RENCODE_IB_PARAM_ENCODE_CONTEXT_BUFFER;
540    enc->cmd.bitstream = RENCODE_IB_PARAM_VIDEO_BITSTREAM_BUFFER;
541    enc->cmd.feedback = RENCODE_IB_PARAM_FEEDBACK_BUFFER;
542    enc->cmd.slice_control_hevc = RENCODE_HEVC_IB_PARAM_SLICE_CONTROL;
543    enc->cmd.spec_misc_hevc = RENCODE_HEVC_IB_PARAM_SPEC_MISC;
544    enc->cmd.deblocking_filter_hevc = RENCODE_HEVC_IB_PARAM_LOOP_FILTER;
545    enc->cmd.slice_control_h264 = RENCODE_H264_IB_PARAM_SLICE_CONTROL;
546    enc->cmd.spec_misc_h264 = RENCODE_H264_IB_PARAM_SPEC_MISC;
547    enc->cmd.enc_params_h264 = RENCODE_H264_IB_PARAM_ENCODE_PARAMS;
548    enc->cmd.deblocking_filter_h264 = RENCODE_H264_IB_PARAM_DEBLOCKING_FILTER;
549 
550    enc->enc_pic.session_info.interface_version =
551       ((RENCODE_FW_INTERFACE_MAJOR_VERSION << RENCODE_IF_MAJOR_VERSION_SHIFT) |
552        (RENCODE_FW_INTERFACE_MINOR_VERSION << RENCODE_IF_MINOR_VERSION_SHIFT));
553 }
554