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