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 "radeon_vcn_dec.h"
29 
30 #include "pipe/p_video_codec.h"
31 #include "radeon_video.h"
32 #include "radeonsi/si_pipe.h"
33 #include "util/u_memory.h"
34 #include "util/u_video.h"
35 #include "vl/vl_mpeg12_decoder.h"
36 #include "vl/vl_probs_table.h"
37 #include "pspdecryptionparam.h"
38 
39 #include <assert.h>
40 #include <stdio.h>
41 
42 #include "radeon_vcn_av1_default.h"
43 
44 #define FB_BUFFER_OFFSET             0x1000
45 #define FB_BUFFER_SIZE               2048
46 #define IT_SCALING_TABLE_SIZE        992
47 #define VP9_PROBS_TABLE_SIZE         (RDECODE_VP9_PROBS_DATA_SIZE + 256)
48 #define RDECODE_SESSION_CONTEXT_SIZE (128 * 1024)
49 
50 #define RDECODE_VCN1_GPCOM_VCPU_CMD   0x2070c
51 #define RDECODE_VCN1_GPCOM_VCPU_DATA0 0x20710
52 #define RDECODE_VCN1_GPCOM_VCPU_DATA1 0x20714
53 #define RDECODE_VCN1_ENGINE_CNTL      0x20718
54 
55 #define RDECODE_VCN2_GPCOM_VCPU_CMD   (0x503 << 2)
56 #define RDECODE_VCN2_GPCOM_VCPU_DATA0 (0x504 << 2)
57 #define RDECODE_VCN2_GPCOM_VCPU_DATA1 (0x505 << 2)
58 #define RDECODE_VCN2_ENGINE_CNTL      (0x506 << 2)
59 
60 #define RDECODE_VCN2_5_GPCOM_VCPU_CMD   0x3c
61 #define RDECODE_VCN2_5_GPCOM_VCPU_DATA0 0x40
62 #define RDECODE_VCN2_5_GPCOM_VCPU_DATA1 0x44
63 #define RDECODE_VCN2_5_ENGINE_CNTL      0x9b4
64 
65 #define NUM_MPEG2_REFS 6
66 #define NUM_H264_REFS  17
67 #define NUM_VC1_REFS   5
68 #define NUM_VP9_REFS   8
69 #define NUM_AV1_REFS   8
70 #define NUM_AV1_REFS_PER_FRAME 7
71 
72 static unsigned calc_dpb_size(struct radeon_decoder *dec);
73 static unsigned calc_ctx_size_h264_perf(struct radeon_decoder *dec);
74 static unsigned calc_ctx_size_h265_main(struct radeon_decoder *dec);
75 static unsigned calc_ctx_size_h265_main10(struct radeon_decoder *dec,
76                                           struct pipe_h265_picture_desc *pic);
77 
get_h264_msg(struct radeon_decoder * dec,struct pipe_h264_picture_desc * pic)78 static rvcn_dec_message_avc_t get_h264_msg(struct radeon_decoder *dec,
79                                            struct pipe_h264_picture_desc *pic)
80 {
81    rvcn_dec_message_avc_t result;
82 
83    memset(&result, 0, sizeof(result));
84    switch (pic->base.profile) {
85    case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
86    case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
87       result.profile = RDECODE_H264_PROFILE_BASELINE;
88       break;
89 
90    case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
91       result.profile = RDECODE_H264_PROFILE_MAIN;
92       break;
93 
94    case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
95       result.profile = RDECODE_H264_PROFILE_HIGH;
96       break;
97 
98    default:
99       assert(0);
100       break;
101    }
102 
103    result.level = dec->base.level;
104 
105    result.sps_info_flags = 0;
106    result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
107    result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
108    result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
109    result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
110    result.sps_info_flags |= 1 << RDECODE_SPS_INFO_H264_EXTENSION_SUPPORT_FLAG_SHIFT;
111 
112    result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
113    result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
114    result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
115    result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
116    result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
117 
118    switch (dec->base.chroma_format) {
119    case PIPE_VIDEO_CHROMA_FORMAT_NONE:
120       break;
121    case PIPE_VIDEO_CHROMA_FORMAT_400:
122       result.chroma_format = 0;
123       break;
124    case PIPE_VIDEO_CHROMA_FORMAT_420:
125       result.chroma_format = 1;
126       break;
127    case PIPE_VIDEO_CHROMA_FORMAT_422:
128       result.chroma_format = 2;
129       break;
130    case PIPE_VIDEO_CHROMA_FORMAT_444:
131       result.chroma_format = 3;
132       break;
133    }
134 
135    result.pps_info_flags = 0;
136    result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
137    result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
138    result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
139    result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
140    result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
141    result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
142    result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
143    result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
144 
145    result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
146    result.slice_group_map_type = pic->pps->slice_group_map_type;
147    result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
148    result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
149    result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
150    result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
151 
152    memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6 * 16);
153    memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2 * 64);
154 
155    memcpy(dec->it, result.scaling_list_4x4, 6 * 16);
156    memcpy((dec->it + 96), result.scaling_list_8x8, 2 * 64);
157 
158    result.num_ref_frames = pic->num_ref_frames;
159 
160    result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
161    result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
162 
163    result.frame_num = pic->frame_num;
164    memcpy(result.frame_num_list, pic->frame_num_list, 4 * 16);
165    result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
166    result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
167    memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4 * 16 * 2);
168 
169    result.decoded_pic_idx = pic->frame_num;
170 
171    return result;
172 }
173 
radeon_dec_destroy_associated_data(void * data)174 static void radeon_dec_destroy_associated_data(void *data)
175 {
176    /* NOOP, since we only use an intptr */
177 }
178 
get_h265_msg(struct radeon_decoder * dec,struct pipe_video_buffer * target,struct pipe_h265_picture_desc * pic)179 static rvcn_dec_message_hevc_t get_h265_msg(struct radeon_decoder *dec,
180                                             struct pipe_video_buffer *target,
181                                             struct pipe_h265_picture_desc *pic)
182 {
183    rvcn_dec_message_hevc_t result;
184    unsigned i, j;
185 
186    memset(&result, 0, sizeof(result));
187    result.sps_info_flags = 0;
188    result.sps_info_flags |= pic->pps->sps->scaling_list_enabled_flag << 0;
189    result.sps_info_flags |= pic->pps->sps->amp_enabled_flag << 1;
190    result.sps_info_flags |= pic->pps->sps->sample_adaptive_offset_enabled_flag << 2;
191    result.sps_info_flags |= pic->pps->sps->pcm_enabled_flag << 3;
192    result.sps_info_flags |= pic->pps->sps->pcm_loop_filter_disabled_flag << 4;
193    result.sps_info_flags |= pic->pps->sps->long_term_ref_pics_present_flag << 5;
194    result.sps_info_flags |= pic->pps->sps->sps_temporal_mvp_enabled_flag << 6;
195    result.sps_info_flags |= pic->pps->sps->strong_intra_smoothing_enabled_flag << 7;
196    result.sps_info_flags |= pic->pps->sps->separate_colour_plane_flag << 8;
197    if (((struct si_screen *)dec->screen)->info.family == CHIP_CARRIZO)
198       result.sps_info_flags |= 1 << 9;
199    if (pic->UseRefPicList == true)
200       result.sps_info_flags |= 1 << 10;
201    if (pic->UseStRpsBits == true && pic->pps->st_rps_bits != 0) {
202       result.sps_info_flags |= 1 << 11;
203       result.st_rps_bits = pic->pps->st_rps_bits;
204   }
205 
206    result.chroma_format = pic->pps->sps->chroma_format_idc;
207    result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
208    result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
209    result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
210    result.sps_max_dec_pic_buffering_minus1 = pic->pps->sps->sps_max_dec_pic_buffering_minus1;
211    result.log2_min_luma_coding_block_size_minus3 =
212       pic->pps->sps->log2_min_luma_coding_block_size_minus3;
213    result.log2_diff_max_min_luma_coding_block_size =
214       pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
215    result.log2_min_transform_block_size_minus2 =
216       pic->pps->sps->log2_min_transform_block_size_minus2;
217    result.log2_diff_max_min_transform_block_size =
218       pic->pps->sps->log2_diff_max_min_transform_block_size;
219    result.max_transform_hierarchy_depth_inter = pic->pps->sps->max_transform_hierarchy_depth_inter;
220    result.max_transform_hierarchy_depth_intra = pic->pps->sps->max_transform_hierarchy_depth_intra;
221    result.pcm_sample_bit_depth_luma_minus1 = pic->pps->sps->pcm_sample_bit_depth_luma_minus1;
222    result.pcm_sample_bit_depth_chroma_minus1 = pic->pps->sps->pcm_sample_bit_depth_chroma_minus1;
223    result.log2_min_pcm_luma_coding_block_size_minus3 =
224       pic->pps->sps->log2_min_pcm_luma_coding_block_size_minus3;
225    result.log2_diff_max_min_pcm_luma_coding_block_size =
226       pic->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size;
227    result.num_short_term_ref_pic_sets = pic->pps->sps->num_short_term_ref_pic_sets;
228 
229    result.pps_info_flags = 0;
230    result.pps_info_flags |= pic->pps->dependent_slice_segments_enabled_flag << 0;
231    result.pps_info_flags |= pic->pps->output_flag_present_flag << 1;
232    result.pps_info_flags |= pic->pps->sign_data_hiding_enabled_flag << 2;
233    result.pps_info_flags |= pic->pps->cabac_init_present_flag << 3;
234    result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 4;
235    result.pps_info_flags |= pic->pps->transform_skip_enabled_flag << 5;
236    result.pps_info_flags |= pic->pps->cu_qp_delta_enabled_flag << 6;
237    result.pps_info_flags |= pic->pps->pps_slice_chroma_qp_offsets_present_flag << 7;
238    result.pps_info_flags |= pic->pps->weighted_pred_flag << 8;
239    result.pps_info_flags |= pic->pps->weighted_bipred_flag << 9;
240    result.pps_info_flags |= pic->pps->transquant_bypass_enabled_flag << 10;
241    result.pps_info_flags |= pic->pps->tiles_enabled_flag << 11;
242    result.pps_info_flags |= pic->pps->entropy_coding_sync_enabled_flag << 12;
243    result.pps_info_flags |= pic->pps->uniform_spacing_flag << 13;
244    result.pps_info_flags |= pic->pps->loop_filter_across_tiles_enabled_flag << 14;
245    result.pps_info_flags |= pic->pps->pps_loop_filter_across_slices_enabled_flag << 15;
246    result.pps_info_flags |= pic->pps->deblocking_filter_override_enabled_flag << 16;
247    result.pps_info_flags |= pic->pps->pps_deblocking_filter_disabled_flag << 17;
248    result.pps_info_flags |= pic->pps->lists_modification_present_flag << 18;
249    result.pps_info_flags |= pic->pps->slice_segment_header_extension_present_flag << 19;
250 
251    result.num_extra_slice_header_bits = pic->pps->num_extra_slice_header_bits;
252    result.num_long_term_ref_pic_sps = pic->pps->sps->num_long_term_ref_pics_sps;
253    result.num_ref_idx_l0_default_active_minus1 = pic->pps->num_ref_idx_l0_default_active_minus1;
254    result.num_ref_idx_l1_default_active_minus1 = pic->pps->num_ref_idx_l1_default_active_minus1;
255    result.pps_cb_qp_offset = pic->pps->pps_cb_qp_offset;
256    result.pps_cr_qp_offset = pic->pps->pps_cr_qp_offset;
257    result.pps_beta_offset_div2 = pic->pps->pps_beta_offset_div2;
258    result.pps_tc_offset_div2 = pic->pps->pps_tc_offset_div2;
259    result.diff_cu_qp_delta_depth = pic->pps->diff_cu_qp_delta_depth;
260    result.num_tile_columns_minus1 = pic->pps->num_tile_columns_minus1;
261    result.num_tile_rows_minus1 = pic->pps->num_tile_rows_minus1;
262    result.log2_parallel_merge_level_minus2 = pic->pps->log2_parallel_merge_level_minus2;
263    result.init_qp_minus26 = pic->pps->init_qp_minus26;
264 
265    for (i = 0; i < 19; ++i)
266       result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
267 
268    for (i = 0; i < 21; ++i)
269       result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
270 
271    result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
272    result.curr_poc = pic->CurrPicOrderCntVal;
273 
274    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
275       for (j = 0;
276            (pic->ref[j] != NULL) && (j < ARRAY_SIZE(dec->render_pic_list));
277            j++) {
278          if (dec->render_pic_list[i] == pic->ref[j])
279             break;
280          if (j == ARRAY_SIZE(dec->render_pic_list) - 1)
281             dec->render_pic_list[i] = NULL;
282          else if (pic->ref[j + 1] == NULL)
283             dec->render_pic_list[i] = NULL;
284       }
285    }
286    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
287       if (dec->render_pic_list[i] == NULL) {
288          dec->render_pic_list[i] = target;
289          result.curr_idx = i;
290          break;
291       }
292    }
293 
294    vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)result.curr_idx,
295                                        &radeon_dec_destroy_associated_data);
296 
297    for (i = 0; i < 16; ++i) {
298       struct pipe_video_buffer *ref = pic->ref[i];
299       uintptr_t ref_pic = 0;
300 
301       result.poc_list[i] = pic->PicOrderCntVal[i];
302 
303       if (ref)
304          ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
305       else
306          ref_pic = 0x7F;
307       result.ref_pic_list[i] = ref_pic;
308    }
309 
310    for (i = 0; i < 8; ++i) {
311       result.ref_pic_set_st_curr_before[i] = 0xFF;
312       result.ref_pic_set_st_curr_after[i] = 0xFF;
313       result.ref_pic_set_lt_curr[i] = 0xFF;
314    }
315 
316    for (i = 0; i < pic->NumPocStCurrBefore; ++i)
317       result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
318 
319    for (i = 0; i < pic->NumPocStCurrAfter; ++i)
320       result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
321 
322    for (i = 0; i < pic->NumPocLtCurr; ++i)
323       result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
324 
325    for (i = 0; i < 6; ++i)
326       result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
327 
328    for (i = 0; i < 2; ++i)
329       result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
330 
331    memcpy(dec->it, pic->pps->sps->ScalingList4x4, 6 * 16);
332    memcpy(dec->it + 96, pic->pps->sps->ScalingList8x8, 6 * 64);
333    memcpy(dec->it + 480, pic->pps->sps->ScalingList16x16, 6 * 64);
334    memcpy(dec->it + 864, pic->pps->sps->ScalingList32x32, 2 * 64);
335 
336    for (i = 0; i < 2; i++) {
337       for (j = 0; j < 15; j++)
338          result.direct_reflist[i][j] = pic->RefPicList[i][j];
339    }
340 
341    if (pic->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
342       if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
343          result.p010_mode = 1;
344          result.msb_mode = 1;
345       } else {
346          result.p010_mode = 0;
347          result.luma_10to8 = 5;
348          result.chroma_10to8 = 5;
349          result.hevc_reserved[0] = 4; /* sclr_luma10to8 */
350          result.hevc_reserved[1] = 4; /* sclr_chroma10to8 */
351       }
352    }
353 
354    return result;
355 }
356 
fill_probs_table(void * ptr)357 static void fill_probs_table(void *ptr)
358 {
359    rvcn_dec_vp9_probs_t *probs = (rvcn_dec_vp9_probs_t *)ptr;
360 
361    memcpy(&probs->coef_probs[0], default_coef_probs_4x4, sizeof(default_coef_probs_4x4));
362    memcpy(&probs->coef_probs[1], default_coef_probs_8x8, sizeof(default_coef_probs_8x8));
363    memcpy(&probs->coef_probs[2], default_coef_probs_16x16, sizeof(default_coef_probs_16x16));
364    memcpy(&probs->coef_probs[3], default_coef_probs_32x32, sizeof(default_coef_probs_32x32));
365    memcpy(probs->y_mode_prob, default_if_y_probs, sizeof(default_if_y_probs));
366    memcpy(probs->uv_mode_prob, default_if_uv_probs, sizeof(default_if_uv_probs));
367    memcpy(probs->single_ref_prob, default_single_ref_p, sizeof(default_single_ref_p));
368    memcpy(probs->switchable_interp_prob, default_switchable_interp_prob,
369           sizeof(default_switchable_interp_prob));
370    memcpy(probs->partition_prob, default_partition_probs, sizeof(default_partition_probs));
371    memcpy(probs->inter_mode_probs, default_inter_mode_probs, sizeof(default_inter_mode_probs));
372    memcpy(probs->mbskip_probs, default_skip_probs, sizeof(default_skip_probs));
373    memcpy(probs->intra_inter_prob, default_intra_inter_p, sizeof(default_intra_inter_p));
374    memcpy(probs->comp_inter_prob, default_comp_inter_p, sizeof(default_comp_inter_p));
375    memcpy(probs->comp_ref_prob, default_comp_ref_p, sizeof(default_comp_ref_p));
376    memcpy(probs->tx_probs_32x32, default_tx_probs_32x32, sizeof(default_tx_probs_32x32));
377    memcpy(probs->tx_probs_16x16, default_tx_probs_16x16, sizeof(default_tx_probs_16x16));
378    memcpy(probs->tx_probs_8x8, default_tx_probs_8x8, sizeof(default_tx_probs_8x8));
379    memcpy(probs->mv_joints, default_nmv_joints, sizeof(default_nmv_joints));
380    memcpy(&probs->mv_comps[0], default_nmv_components, sizeof(default_nmv_components));
381    memset(&probs->nmvc_mask, 0, sizeof(rvcn_dec_vp9_nmv_ctx_mask_t));
382 }
383 
get_vp9_msg(struct radeon_decoder * dec,struct pipe_video_buffer * target,struct pipe_vp9_picture_desc * pic)384 static rvcn_dec_message_vp9_t get_vp9_msg(struct radeon_decoder *dec,
385                                           struct pipe_video_buffer *target,
386                                           struct pipe_vp9_picture_desc *pic)
387 {
388    rvcn_dec_message_vp9_t result;
389    unsigned i ,j;
390 
391    memset(&result, 0, sizeof(result));
392 
393    /* segment table */
394    rvcn_dec_vp9_probs_segment_t *prbs = (rvcn_dec_vp9_probs_segment_t *)(dec->probs);
395 
396    if (pic->picture_parameter.pic_fields.segmentation_enabled) {
397       for (i = 0; i < 8; ++i) {
398          prbs->seg.feature_data[i] =
399             (pic->slice_parameter.seg_param[i].alt_quant & 0xffff) |
400             ((pic->slice_parameter.seg_param[i].alt_lf & 0xff) << 16) |
401             ((pic->slice_parameter.seg_param[i].segment_flags.segment_reference & 0xf) << 24);
402          prbs->seg.feature_mask[i] =
403             (pic->slice_parameter.seg_param[i].alt_quant_enabled << 0) |
404             (pic->slice_parameter.seg_param[i].alt_lf_enabled << 1) |
405             (pic->slice_parameter.seg_param[i].segment_flags.segment_reference_enabled << 2) |
406             (pic->slice_parameter.seg_param[i].segment_flags.segment_reference_skipped << 3);
407       }
408 
409       for (i = 0; i < 7; ++i)
410          prbs->seg.tree_probs[i] = pic->picture_parameter.mb_segment_tree_probs[i];
411 
412       for (i = 0; i < 3; ++i)
413          prbs->seg.pred_probs[i] = pic->picture_parameter.segment_pred_probs[i];
414 
415       prbs->seg.abs_delta = pic->picture_parameter.abs_delta;
416    } else
417       memset(&prbs->seg, 0, 256);
418 
419    result.frame_header_flags = (pic->picture_parameter.pic_fields.frame_type
420                                 << RDECODE_FRAME_HDR_INFO_VP9_FRAME_TYPE_SHIFT) &
421                                RDECODE_FRAME_HDR_INFO_VP9_FRAME_TYPE_MASK;
422 
423    result.frame_header_flags |= (pic->picture_parameter.pic_fields.error_resilient_mode
424                                  << RDECODE_FRAME_HDR_INFO_VP9_ERROR_RESILIENT_MODE_SHIFT) &
425                                 RDECODE_FRAME_HDR_INFO_VP9_ERROR_RESILIENT_MODE_MASK;
426 
427    result.frame_header_flags |= (pic->picture_parameter.pic_fields.intra_only
428                                  << RDECODE_FRAME_HDR_INFO_VP9_INTRA_ONLY_SHIFT) &
429                                 RDECODE_FRAME_HDR_INFO_VP9_INTRA_ONLY_MASK;
430 
431    result.frame_header_flags |= (pic->picture_parameter.pic_fields.allow_high_precision_mv
432                                  << RDECODE_FRAME_HDR_INFO_VP9_ALLOW_HIGH_PRECISION_MV_SHIFT) &
433                                 RDECODE_FRAME_HDR_INFO_VP9_ALLOW_HIGH_PRECISION_MV_MASK;
434 
435    result.frame_header_flags |= (pic->picture_parameter.pic_fields.frame_parallel_decoding_mode
436                                  << RDECODE_FRAME_HDR_INFO_VP9_FRAME_PARALLEL_DECODING_MODE_SHIFT) &
437                                 RDECODE_FRAME_HDR_INFO_VP9_FRAME_PARALLEL_DECODING_MODE_MASK;
438 
439    result.frame_header_flags |= (pic->picture_parameter.pic_fields.refresh_frame_context
440                                  << RDECODE_FRAME_HDR_INFO_VP9_REFRESH_FRAME_CONTEXT_SHIFT) &
441                                 RDECODE_FRAME_HDR_INFO_VP9_REFRESH_FRAME_CONTEXT_MASK;
442 
443    result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_enabled
444                                  << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_ENABLED_SHIFT) &
445                                 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_ENABLED_MASK;
446 
447    result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_update_map
448                                  << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_UPDATE_MAP_SHIFT) &
449                                 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_UPDATE_MAP_MASK;
450 
451    result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_temporal_update
452                                  << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_TEMPORAL_UPDATE_SHIFT) &
453                                 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_TEMPORAL_UPDATE_MASK;
454 
455    result.frame_header_flags |= (pic->picture_parameter.mode_ref_delta_enabled
456                                  << RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_ENABLED_SHIFT) &
457                                 RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_ENABLED_MASK;
458 
459    result.frame_header_flags |= (pic->picture_parameter.mode_ref_delta_update
460                                  << RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_UPDATE_SHIFT) &
461                                 RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_UPDATE_MASK;
462 
463    result.frame_header_flags |=
464       ((dec->show_frame && !pic->picture_parameter.pic_fields.error_resilient_mode &&
465         dec->last_width == dec->base.width && dec->last_height == dec->base.height)
466        << RDECODE_FRAME_HDR_INFO_VP9_USE_PREV_IN_FIND_MV_REFS_SHIFT) &
467       RDECODE_FRAME_HDR_INFO_VP9_USE_PREV_IN_FIND_MV_REFS_MASK;
468    dec->show_frame = pic->picture_parameter.pic_fields.show_frame;
469 
470    result.frame_header_flags |=  (1 << RDECODE_FRAME_HDR_INFO_VP9_USE_UNCOMPRESSED_HEADER_SHIFT) &
471                                  RDECODE_FRAME_HDR_INFO_VP9_USE_UNCOMPRESSED_HEADER_MASK;
472 
473    result.interp_filter = pic->picture_parameter.pic_fields.mcomp_filter_type;
474 
475    result.frame_context_idx = pic->picture_parameter.pic_fields.frame_context_idx;
476    result.reset_frame_context = pic->picture_parameter.pic_fields.reset_frame_context;
477 
478    result.filter_level = pic->picture_parameter.filter_level;
479    result.sharpness_level = pic->picture_parameter.sharpness_level;
480 
481    for (i = 0; i < 8; ++i)
482       memcpy(result.lf_adj_level[i], pic->slice_parameter.seg_param[i].filter_level, 4 * 2);
483 
484    if (pic->picture_parameter.pic_fields.lossless_flag) {
485       result.base_qindex = 0;
486       result.y_dc_delta_q = 0;
487       result.uv_ac_delta_q = 0;
488       result.uv_dc_delta_q = 0;
489    } else {
490       result.base_qindex = pic->picture_parameter.base_qindex;
491       result.y_dc_delta_q = pic->picture_parameter.y_dc_delta_q;
492       result.uv_ac_delta_q = pic->picture_parameter.uv_ac_delta_q;
493       result.uv_dc_delta_q = pic->picture_parameter.uv_dc_delta_q;
494    }
495 
496    result.log2_tile_cols = pic->picture_parameter.log2_tile_columns;
497    result.log2_tile_rows = pic->picture_parameter.log2_tile_rows;
498    result.chroma_format = 1;
499    result.bit_depth_luma_minus8 = result.bit_depth_chroma_minus8 =
500       (pic->picture_parameter.bit_depth - 8);
501 
502    result.vp9_frame_size = align(dec->bs_size, 128);
503    result.uncompressed_header_size = pic->picture_parameter.frame_header_length_in_bytes;
504    result.compressed_header_size = pic->picture_parameter.first_partition_size;
505 
506    assert(dec->base.max_references + 1 <= ARRAY_SIZE(dec->render_pic_list));
507 
508    //clear the dec->render list if it is not used as a reference
509    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
510       if (dec->render_pic_list[i]) {
511           for (j=0;j<8;j++) {
512             if (dec->render_pic_list[i] == pic->ref[j])
513                  break;
514 	  }
515 	  if(j == 8)
516              dec->render_pic_list[i] = NULL;
517       }
518    }
519 
520    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); ++i) {
521       if (dec->render_pic_list[i] && dec->render_pic_list[i] == target) {
522 	if (target->codec != NULL){
523 	    result.curr_pic_idx =(uintptr_t)vl_video_buffer_get_associated_data(target, &dec->base);
524 	} else {
525 	    result.curr_pic_idx = i;
526 	    vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)i,
527 						&radeon_dec_destroy_associated_data);
528 	}
529 	break;
530       } else if (!dec->render_pic_list[i]) {
531          dec->render_pic_list[i] = target;
532          result.curr_pic_idx = i;
533          vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)i,
534                                              &radeon_dec_destroy_associated_data);
535          break;
536       }
537    }
538 
539    for (i = 0; i < 8; i++) {
540       result.ref_frame_map[i] =
541          (pic->ref[i]) ? (uintptr_t)vl_video_buffer_get_associated_data(pic->ref[i], &dec->base)
542                        : 0x7f;
543    }
544 
545    result.frame_refs[0] = result.ref_frame_map[pic->picture_parameter.pic_fields.last_ref_frame];
546    result.ref_frame_sign_bias[0] = pic->picture_parameter.pic_fields.last_ref_frame_sign_bias;
547    result.frame_refs[1] = result.ref_frame_map[pic->picture_parameter.pic_fields.golden_ref_frame];
548    result.ref_frame_sign_bias[1] = pic->picture_parameter.pic_fields.golden_ref_frame_sign_bias;
549    result.frame_refs[2] = result.ref_frame_map[pic->picture_parameter.pic_fields.alt_ref_frame];
550    result.ref_frame_sign_bias[2] = pic->picture_parameter.pic_fields.alt_ref_frame_sign_bias;
551 
552    if (pic->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2) {
553       if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
554          result.p010_mode = 1;
555          result.msb_mode = 1;
556       } else {
557          result.p010_mode = 0;
558          result.luma_10to8 = 1;
559          result.chroma_10to8 = 1;
560       }
561    }
562 
563    if (dec->dpb_type == DPB_DYNAMIC_TIER_2) {
564       dec->ref_codec.bts = (pic->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2) ?
565          CODEC_10_BITS : CODEC_8_BITS;
566       dec->ref_codec.index = result.curr_pic_idx;
567       dec->ref_codec.ref_size = 8;
568       memset(dec->ref_codec.ref_list, 0x7f, sizeof(dec->ref_codec.ref_list));
569       memcpy(dec->ref_codec.ref_list, result.ref_frame_map, sizeof(result.ref_frame_map));
570    }
571 
572    dec->last_width = dec->base.width;
573    dec->last_height = dec->base.height;
574 
575    return result;
576 }
577 
set_drm_keys(rvcn_dec_message_drm_t * drm,DECRYPT_PARAMETERS * decrypted)578 static void set_drm_keys(rvcn_dec_message_drm_t *drm, DECRYPT_PARAMETERS *decrypted)
579 {
580    int cbc = decrypted->u.s.cbc;
581    int ctr = decrypted->u.s.ctr;
582    int id = decrypted->u.s.drm_id;
583    int ekc = 1;
584    int data1 = 1;
585    int data2 = 1;
586 
587    drm->drm_cmd = 0;
588    drm->drm_cntl = 0;
589 
590    drm->drm_cntl = 1 << DRM_CNTL_BYPASS_SHIFT;
591 
592    if (cbc || ctr) {
593       drm->drm_cntl = 0 << DRM_CNTL_BYPASS_SHIFT;
594       drm->drm_cmd |= 0xff << DRM_CMD_BYTE_MASK_SHIFT;
595 
596       if (ctr)
597          drm->drm_cmd |= 0x00 << DRM_CMD_ALGORITHM_SHIFT;
598       else if (cbc)
599          drm->drm_cmd |= 0x02 << DRM_CMD_ALGORITHM_SHIFT;
600 
601       drm->drm_cmd |= 1 << DRM_CMD_GEN_MASK_SHIFT;
602       drm->drm_cmd |= ekc << DRM_CMD_UNWRAP_KEY_SHIFT;
603       drm->drm_cmd |= 0 << DRM_CMD_OFFSET_SHIFT;
604       drm->drm_cmd |= data2 << DRM_CMD_CNT_DATA_SHIFT;
605       drm->drm_cmd |= data1 << DRM_CMD_CNT_KEY_SHIFT;
606       drm->drm_cmd |= ekc << DRM_CMD_KEY_SHIFT;
607       drm->drm_cmd |= id << DRM_CMD_SESSION_SEL_SHIFT;
608 
609       if (ekc)
610          memcpy(drm->drm_wrapped_key, decrypted->encrypted_key, 16);
611       if (data1)
612          memcpy(drm->drm_key, decrypted->session_iv, 16);
613       if (data2)
614          memcpy(drm->drm_counter, decrypted->encrypted_iv, 16);
615       drm->drm_offset = 0;
616    }
617 }
618 
get_av1_msg(struct radeon_decoder * dec,struct pipe_video_buffer * target,struct pipe_av1_picture_desc * pic)619 static rvcn_dec_message_av1_t get_av1_msg(struct radeon_decoder *dec,
620                                           struct pipe_video_buffer *target,
621                                           struct pipe_av1_picture_desc *pic)
622 {
623    rvcn_dec_message_av1_t result;
624    unsigned i, j;
625 
626    memset(&result, 0, sizeof(result));
627 
628    result.frame_header_flags = (pic->picture_parameter.pic_info_fields.show_frame
629                                 << RDECODE_FRAME_HDR_INFO_AV1_SHOW_FRAME_SHIFT) &
630                                 RDECODE_FRAME_HDR_INFO_AV1_SHOW_FRAME_MASK;
631 
632    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.disable_cdf_update
633                                  << RDECODE_FRAME_HDR_INFO_AV1_DISABLE_CDF_UPDATE_SHIFT) &
634                                  RDECODE_FRAME_HDR_INFO_AV1_DISABLE_CDF_UPDATE_MASK;
635 
636    result.frame_header_flags |= ((!pic->picture_parameter.pic_info_fields.disable_frame_end_update_cdf)
637                                  << RDECODE_FRAME_HDR_INFO_AV1_REFRESH_FRAME_CONTEXT_SHIFT) &
638                                  RDECODE_FRAME_HDR_INFO_AV1_REFRESH_FRAME_CONTEXT_MASK;
639 
640    result.frame_header_flags |= ((pic->picture_parameter.pic_info_fields.frame_type ==
641                                  2 /* INTRA_ONLY_FRAME */) << RDECODE_FRAME_HDR_INFO_AV1_INTRA_ONLY_SHIFT) &
642                                  RDECODE_FRAME_HDR_INFO_AV1_INTRA_ONLY_MASK;
643 
644    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.allow_intrabc
645                                  << RDECODE_FRAME_HDR_INFO_AV1_ALLOW_INTRABC_SHIFT) &
646                                  RDECODE_FRAME_HDR_INFO_AV1_ALLOW_INTRABC_MASK;
647 
648    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.allow_high_precision_mv
649                                  << RDECODE_FRAME_HDR_INFO_AV1_ALLOW_HIGH_PRECISION_MV_SHIFT) &
650                                  RDECODE_FRAME_HDR_INFO_AV1_ALLOW_HIGH_PRECISION_MV_MASK;
651 
652    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.mono_chrome
653                                  << RDECODE_FRAME_HDR_INFO_AV1_MONOCHROME_SHIFT) &
654                                  RDECODE_FRAME_HDR_INFO_AV1_MONOCHROME_MASK;
655 
656    result.frame_header_flags |= (pic->picture_parameter.mode_control_fields.skip_mode_present
657                                  << RDECODE_FRAME_HDR_INFO_AV1_SKIP_MODE_FLAG_SHIFT) &
658                                  RDECODE_FRAME_HDR_INFO_AV1_SKIP_MODE_FLAG_MASK;
659 
660    result.frame_header_flags |= (((pic->picture_parameter.qmatrix_fields.qm_y == 0xf) ? 0 : 1)
661                                  << RDECODE_FRAME_HDR_INFO_AV1_USING_QMATRIX_SHIFT) &
662                                  RDECODE_FRAME_HDR_INFO_AV1_USING_QMATRIX_MASK;
663 
664    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_filter_intra
665                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_FILTER_INTRA_SHIFT) &
666                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_FILTER_INTRA_MASK;
667 
668    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_intra_edge_filter
669                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_INTRA_EDGE_FILTER_SHIFT) &
670                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_INTRA_EDGE_FILTER_MASK;
671 
672    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_interintra_compound
673                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_INTERINTRA_COMPOUND_SHIFT) &
674                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_INTERINTRA_COMPOUND_MASK;
675 
676    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_masked_compound
677                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_MASKED_COMPOUND_SHIFT) &
678                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_MASKED_COMPOUND_MASK;
679 
680    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.allow_warped_motion
681                                  << RDECODE_FRAME_HDR_INFO_AV1_ALLOW_WARPED_MOTION_SHIFT) &
682                                  RDECODE_FRAME_HDR_INFO_AV1_ALLOW_WARPED_MOTION_MASK;
683 
684    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_dual_filter
685                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_DUAL_FILTER_SHIFT) &
686                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_DUAL_FILTER_MASK;
687 
688    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_order_hint
689                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_ORDER_HINT_SHIFT) &
690                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_ORDER_HINT_MASK;
691 
692    result.frame_header_flags |= (pic->picture_parameter.seq_info_fields.enable_jnt_comp
693                                  << RDECODE_FRAME_HDR_INFO_AV1_ENABLE_JNT_COMP_SHIFT) &
694                                  RDECODE_FRAME_HDR_INFO_AV1_ENABLE_JNT_COMP_MASK;
695 
696    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.use_ref_frame_mvs
697                                  << RDECODE_FRAME_HDR_INFO_AV1_ALLOW_REF_FRAME_MVS_SHIFT) &
698                                  RDECODE_FRAME_HDR_INFO_AV1_ALLOW_REF_FRAME_MVS_MASK;
699 
700    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.allow_screen_content_tools
701                                  << RDECODE_FRAME_HDR_INFO_AV1_ALLOW_SCREEN_CONTENT_TOOLS_SHIFT) &
702                                  RDECODE_FRAME_HDR_INFO_AV1_ALLOW_SCREEN_CONTENT_TOOLS_MASK;
703 
704    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.force_integer_mv
705                                  << RDECODE_FRAME_HDR_INFO_AV1_CUR_FRAME_FORCE_INTEGER_MV_SHIFT) &
706                                  RDECODE_FRAME_HDR_INFO_AV1_CUR_FRAME_FORCE_INTEGER_MV_MASK;
707 
708    result.frame_header_flags |= (pic->picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled
709                                  << RDECODE_FRAME_HDR_INFO_AV1_MODE_REF_DELTA_ENABLED_SHIFT) &
710                                  RDECODE_FRAME_HDR_INFO_AV1_MODE_REF_DELTA_ENABLED_MASK;
711 
712    result.frame_header_flags |= (pic->picture_parameter.loop_filter_info_fields.mode_ref_delta_update
713                                  << RDECODE_FRAME_HDR_INFO_AV1_MODE_REF_DELTA_UPDATE_SHIFT) &
714                                  RDECODE_FRAME_HDR_INFO_AV1_MODE_REF_DELTA_UPDATE_MASK;
715 
716    result.frame_header_flags |= (pic->picture_parameter.mode_control_fields.delta_q_present_flag
717                                  << RDECODE_FRAME_HDR_INFO_AV1_DELTA_Q_PRESENT_FLAG_SHIFT) &
718                                  RDECODE_FRAME_HDR_INFO_AV1_DELTA_Q_PRESENT_FLAG_MASK;
719 
720    result.frame_header_flags |= (pic->picture_parameter.mode_control_fields.delta_lf_present_flag
721                                  << RDECODE_FRAME_HDR_INFO_AV1_DELTA_LF_PRESENT_FLAG_SHIFT) &
722                                  RDECODE_FRAME_HDR_INFO_AV1_DELTA_LF_PRESENT_FLAG_MASK;
723 
724    result.frame_header_flags |= (pic->picture_parameter.mode_control_fields.reduced_tx_set_used
725                                  << RDECODE_FRAME_HDR_INFO_AV1_REDUCED_TX_SET_USED_SHIFT) &
726                                  RDECODE_FRAME_HDR_INFO_AV1_REDUCED_TX_SET_USED_MASK;
727 
728    result.frame_header_flags |= (pic->picture_parameter.seg_info.segment_info_fields.enabled
729                                  << RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_ENABLED_SHIFT) &
730                                  RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_ENABLED_MASK;
731 
732    result.frame_header_flags |= (pic->picture_parameter.seg_info.segment_info_fields.update_map
733                                  << RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_UPDATE_MAP_SHIFT) &
734                                  RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_UPDATE_MAP_MASK;
735 
736    result.frame_header_flags |= (pic->picture_parameter.seg_info.segment_info_fields.temporal_update
737                                  << RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_TEMPORAL_UPDATE_SHIFT) &
738                                  RDECODE_FRAME_HDR_INFO_AV1_SEGMENTATION_TEMPORAL_UPDATE_MASK;
739 
740    result.frame_header_flags |= (pic->picture_parameter.mode_control_fields.delta_lf_multi
741                                  << RDECODE_FRAME_HDR_INFO_AV1_DELTA_LF_MULTI_SHIFT) &
742                                  RDECODE_FRAME_HDR_INFO_AV1_DELTA_LF_MULTI_MASK;
743 
744    result.frame_header_flags |= (pic->picture_parameter.pic_info_fields.is_motion_mode_switchable
745                                  << RDECODE_FRAME_HDR_INFO_AV1_SWITCHABLE_SKIP_MODE_SHIFT) &
746                                  RDECODE_FRAME_HDR_INFO_AV1_SWITCHABLE_SKIP_MODE_MASK;
747 
748    result.frame_header_flags |= ((!pic->picture_parameter.refresh_frame_flags)
749                                  << RDECODE_FRAME_HDR_INFO_AV1_SKIP_REFERENCE_UPDATE_SHIFT) &
750                                  RDECODE_FRAME_HDR_INFO_AV1_SKIP_REFERENCE_UPDATE_MASK;
751 
752    result.frame_header_flags |= ((!pic->picture_parameter.seq_info_fields.ref_frame_mvs)
753                                  << RDECODE_FRAME_HDR_INFO_AV1_DISABLE_REF_FRAME_MVS_SHIFT) &
754                                  RDECODE_FRAME_HDR_INFO_AV1_DISABLE_REF_FRAME_MVS_MASK;
755 
756    result.current_frame_id = pic->picture_parameter.current_frame_id;
757    result.frame_offset = pic->picture_parameter.order_hint;
758 
759    result.profile = pic->picture_parameter.profile;
760    result.is_annexb = 0;
761    result.frame_type = pic->picture_parameter.pic_info_fields.frame_type;
762    result.primary_ref_frame = pic->picture_parameter.primary_ref_frame;
763    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); ++i) {
764       if (dec->render_pic_list[i] && dec->render_pic_list[i] == target) {
765          result.curr_pic_idx = (uintptr_t)vl_video_buffer_get_associated_data(target, &dec->base);
766          break;
767       } else if (!dec->render_pic_list[i]) {
768          dec->render_pic_list[i] = target;
769          result.curr_pic_idx = dec->ref_idx;
770          vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)dec->ref_idx++,
771                                              &radeon_dec_destroy_associated_data);
772          break;
773       }
774    }
775 
776    result.sb_size = pic->picture_parameter.seq_info_fields.use_128x128_superblock;
777    result.interp_filter = pic->picture_parameter.interp_filter;
778    for (i = 0; i < 2; ++i)
779       result.filter_level[i] = pic->picture_parameter.filter_level[i];
780    result.filter_level_u = pic->picture_parameter.filter_level_u;
781    result.filter_level_v = pic->picture_parameter.filter_level_v;
782    result.sharpness_level = pic->picture_parameter.loop_filter_info_fields.sharpness_level;
783    for (i = 0; i < 8; ++i)
784       result.ref_deltas[i] = pic->picture_parameter.ref_deltas[i];
785    for (i = 0; i < 2; ++i)
786       result.mode_deltas[i] = pic->picture_parameter.mode_deltas[i];
787    result.base_qindex = pic->picture_parameter.base_qindex;
788    result.y_dc_delta_q = pic->picture_parameter.y_dc_delta_q;
789    result.u_dc_delta_q = pic->picture_parameter.u_dc_delta_q;
790    result.v_dc_delta_q = pic->picture_parameter.v_dc_delta_q;
791    result.u_ac_delta_q = pic->picture_parameter.u_ac_delta_q;
792    result.v_ac_delta_q = pic->picture_parameter.v_ac_delta_q;
793    result.qm_y = pic->picture_parameter.qmatrix_fields.qm_y | 0xf0;
794    result.qm_u = pic->picture_parameter.qmatrix_fields.qm_u | 0xf0;
795    result.qm_v = pic->picture_parameter.qmatrix_fields.qm_v | 0xf0;
796    result.delta_q_res = 1 << pic->picture_parameter.mode_control_fields.log2_delta_q_res;
797    result.delta_lf_res = 1 << pic->picture_parameter.mode_control_fields.log2_delta_lf_res;
798 
799    result.tile_cols = pic->picture_parameter.tile_cols;
800    result.tile_rows = pic->picture_parameter.tile_rows;
801    result.tx_mode = pic->picture_parameter.mode_control_fields.tx_mode;
802    result.reference_mode = (pic->picture_parameter.mode_control_fields.reference_select == 1) ? 2 : 0;
803    result.chroma_format = pic->picture_parameter.seq_info_fields.mono_chrome ? 0 : 1;
804    result.tile_size_bytes = 0xff;
805    result.context_update_tile_id = pic->picture_parameter.context_update_tile_id;
806    for (i = 0; i < 65; ++i) {
807       result.tile_col_start_sb[i] = pic->picture_parameter.tile_col_start_sb[i];
808       result.tile_row_start_sb[i] = pic->picture_parameter.tile_row_start_sb[i];
809    }
810    result.max_width = pic->picture_parameter.max_width;
811    result.max_height = pic->picture_parameter.max_height;
812    if (pic->picture_parameter.pic_info_fields.use_superres) {
813       result.width = (pic->picture_parameter.frame_width * 8 + pic->picture_parameter.superres_scale_denominator / 2) /
814          pic->picture_parameter.superres_scale_denominator;
815       result.superres_scale_denominator = pic->picture_parameter.superres_scale_denominator;
816    } else {
817       result.width = pic->picture_parameter.frame_width;
818       result.superres_scale_denominator = pic->picture_parameter.superres_scale_denominator;
819    }
820    result.height = pic->picture_parameter.frame_height;
821    result.superres_upscaled_width = pic->picture_parameter.frame_width;
822    result.order_hint_bits = pic->picture_parameter.order_hint_bits_minus_1 + 1;
823 
824    for (i = 0; i < NUM_AV1_REFS; ++i) {
825       result.ref_frame_map[i] =
826          (pic->ref[i]) ? (uintptr_t)vl_video_buffer_get_associated_data(pic->ref[i], &dec->base)
827                        : 0x7f;
828    }
829    for (i = 0; i < NUM_AV1_REFS_PER_FRAME; ++i)
830        result.frame_refs[i] = result.ref_frame_map[pic->picture_parameter.ref_frame_idx[i]];
831 
832    result.bit_depth_luma_minus8 = result.bit_depth_chroma_minus8 = pic->picture_parameter.bit_depth_idx << 1;
833 
834    for (i = 0; i < 8; ++i) {
835       for (j = 0; j < 8; ++j)
836          result.feature_data[i][j] = pic->picture_parameter.seg_info.feature_data[i][j];
837       result.feature_mask[i] = pic->picture_parameter.seg_info.feature_mask[i];
838    }
839    memcpy(dec->probs, &pic->picture_parameter.seg_info.feature_data, 128);
840    memcpy((dec->probs + 128), &pic->picture_parameter.seg_info.feature_mask, 8);
841 
842    result.cdef_damping = pic->picture_parameter.cdef_damping_minus_3 + 3;
843    result.cdef_bits = pic->picture_parameter.cdef_bits;
844    for (i = 0; i < 8; ++i) {
845       result.cdef_strengths[i] = pic->picture_parameter.cdef_y_strengths[i];
846       result.cdef_uv_strengths[i] = pic->picture_parameter.cdef_uv_strengths[i];
847    }
848    result.frame_restoration_type[0] = pic->picture_parameter.loop_restoration_fields.yframe_restoration_type;
849    result.frame_restoration_type[1] = pic->picture_parameter.loop_restoration_fields.cbframe_restoration_type;
850    result.frame_restoration_type[2] = pic->picture_parameter.loop_restoration_fields.crframe_restoration_type;
851    for (i = 0; i < 3; ++i) {
852       int log2_num = 0;
853       int unit_size = pic->picture_parameter.lr_unit_size[i];
854       if (unit_size) {
855          while (unit_size >>= 1)
856             log2_num++;
857          result.log2_restoration_unit_size_minus5[i] = log2_num - 5;
858       } else {
859          result.log2_restoration_unit_size_minus5[i] = 0;
860       }
861    }
862 
863    if (pic->picture_parameter.bit_depth_idx) {
864       if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
865          result.p010_mode = 1;
866          result.msb_mode = 1;
867       } else {
868          result.luma_10to8 = 1;
869          result.chroma_10to8 = 1;
870       }
871    }
872 
873    result.preskip_segid = 0;
874    result.last_active_segid = 0;
875    for (i = 0; i < 8; i++) {
876       for (j = 0; j < 8; j++) {
877          if (pic->picture_parameter.seg_info.feature_mask[i] & (1 << j)) {
878             result.last_active_segid = i;
879             if (j >= 5)
880                result.preskip_segid = 1;
881          }
882       }
883    }
884 
885    result.seg_lossless_flag = 0;
886    for (i = 0; i < 8; ++i) {
887       int av1_get_qindex, qindex;
888       int segfeature_active = pic->picture_parameter.seg_info.feature_mask[i] & (1 << 0);
889       if (segfeature_active) {
890          int seg_qindex = pic->picture_parameter.base_qindex +
891                           pic->picture_parameter.seg_info.feature_data[i][0];
892          av1_get_qindex = seg_qindex < 0 ? 0 : (seg_qindex > 255 ? 255 : seg_qindex);
893       } else {
894          av1_get_qindex = pic->picture_parameter.base_qindex;
895       }
896       qindex = pic->picture_parameter.seg_info.segment_info_fields.enabled ?
897                av1_get_qindex :
898                pic->picture_parameter.base_qindex;
899       result.seg_lossless_flag |= (((qindex == 0) && result.y_dc_delta_q == 0 &&
900                                     result.u_dc_delta_q == 0 && result.v_dc_delta_q == 0 &&
901                                     result.u_ac_delta_q == 0 && result.v_ac_delta_q == 0) << i);
902    }
903 
904    rvcn_dec_film_grain_params_t* fg_params = &result.film_grain;
905    fg_params->apply_grain = pic->picture_parameter.film_grain_info.film_grain_info_fields.apply_grain;
906    if (fg_params->apply_grain) {
907       fg_params->random_seed = pic->picture_parameter.film_grain_info.grain_seed;
908       fg_params->grain_scale_shift =
909          pic->picture_parameter.film_grain_info.film_grain_info_fields.grain_scale_shift;
910       fg_params->scaling_shift =
911          pic->picture_parameter.film_grain_info.film_grain_info_fields.grain_scaling_minus_8 + 8;
912       fg_params->chroma_scaling_from_luma =
913          pic->picture_parameter.film_grain_info.film_grain_info_fields.chroma_scaling_from_luma;
914       fg_params->num_y_points = pic->picture_parameter.film_grain_info.num_y_points;
915       fg_params->num_cb_points = pic->picture_parameter.film_grain_info.num_cb_points;
916       fg_params->num_cr_points = pic->picture_parameter.film_grain_info.num_cr_points;
917       fg_params->cb_mult = pic->picture_parameter.film_grain_info.cb_mult;
918       fg_params->cb_luma_mult = pic->picture_parameter.film_grain_info.cb_luma_mult;
919       fg_params->cb_offset = pic->picture_parameter.film_grain_info.cb_offset;
920       fg_params->cr_mult = pic->picture_parameter.film_grain_info.cr_mult;
921       fg_params->cr_luma_mult = pic->picture_parameter.film_grain_info.cr_luma_mult;
922       fg_params->cr_offset = pic->picture_parameter.film_grain_info.cr_offset;
923       fg_params->bit_depth_minus_8 = pic->picture_parameter.bit_depth_idx << 1;
924 
925       for (i = 0; i < fg_params->num_y_points; ++i) {
926          fg_params->scaling_points_y[i][0] = pic->picture_parameter.film_grain_info.point_y_value[i];
927          fg_params->scaling_points_y[i][1] = pic->picture_parameter.film_grain_info.point_y_scaling[i];
928       }
929       for (i = 0; i < fg_params->num_cb_points; ++i) {
930          fg_params->scaling_points_cb[i][0] = pic->picture_parameter.film_grain_info.point_cb_value[i];
931          fg_params->scaling_points_cb[i][1] = pic->picture_parameter.film_grain_info.point_cb_scaling[i];
932       }
933       for (i = 0; i < fg_params->num_cr_points; ++i) {
934          fg_params->scaling_points_cr[i][0] = pic->picture_parameter.film_grain_info.point_cr_value[i];
935          fg_params->scaling_points_cr[i][1] = pic->picture_parameter.film_grain_info.point_cr_scaling[i];
936       }
937 
938       fg_params->ar_coeff_lag = pic->picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_lag;
939       fg_params->ar_coeff_shift =
940          pic->picture_parameter.film_grain_info.film_grain_info_fields.ar_coeff_shift_minus_6 + 6;
941 
942       for (i = 0; i < 24; ++i)
943          fg_params->ar_coeffs_y[i] = pic->picture_parameter.film_grain_info.ar_coeffs_y[i];
944 
945       for (i = 0; i < 25; ++i) {
946          fg_params->ar_coeffs_cb[i] = pic->picture_parameter.film_grain_info.ar_coeffs_cb[i];
947          fg_params->ar_coeffs_cr[i] = pic->picture_parameter.film_grain_info.ar_coeffs_cr[i];
948       }
949 
950       fg_params->overlap_flag = pic->picture_parameter.film_grain_info.film_grain_info_fields.overlap_flag;
951       fg_params->clip_to_restricted_range =
952          pic->picture_parameter.film_grain_info.film_grain_info_fields.clip_to_restricted_range;
953    }
954 
955    result.uncompressed_header_size = 0;
956    for (i = 0; i < 7; ++i) {
957       result.global_motion[i + 1].wmtype = (rvcn_dec_transformation_type_e)pic->picture_parameter.wm[i].wmtype;
958       for (j = 0; j < 6; ++j)
959          result.global_motion[i + 1].wmmat[j] = pic->picture_parameter.wm[i].wmmat[j];
960    }
961    for (i = 0; i < 256; ++i) {
962       result.tile_info[i].offset = pic->slice_parameter.slice_data_offset[i];
963       result.tile_info[i].size = pic->slice_parameter.slice_data_size[i];
964    }
965 
966    if (dec->dpb_type == DPB_DYNAMIC_TIER_2) {
967       dec->ref_codec.bts = pic->picture_parameter.bit_depth_idx ? CODEC_10_BITS : CODEC_8_BITS;
968       dec->ref_codec.index = result.curr_pic_idx;
969       dec->ref_codec.ref_size = 8;
970       memset(dec->ref_codec.ref_list, 0x7f, sizeof(dec->ref_codec.ref_list));
971       memcpy(dec->ref_codec.ref_list, result.ref_frame_map, sizeof(result.ref_frame_map));
972    }
973 
974    return result;
975 }
976 
rvcn_init_mode_probs(void * prob)977 static void rvcn_init_mode_probs(void *prob)
978 {
979    rvcn_av1_frame_context_t * fc = (rvcn_av1_frame_context_t*)prob;
980    int i;
981 
982    memcpy(fc->palette_y_size_cdf, default_palette_y_size_cdf, sizeof(default_palette_y_size_cdf));
983    memcpy(fc->palette_uv_size_cdf, default_palette_uv_size_cdf, sizeof(default_palette_uv_size_cdf));
984    memcpy(fc->palette_y_color_index_cdf, default_palette_y_color_index_cdf, sizeof(default_palette_y_color_index_cdf));
985    memcpy(fc->palette_uv_color_index_cdf, default_palette_uv_color_index_cdf, sizeof(default_palette_uv_color_index_cdf));
986    memcpy(fc->kf_y_cdf, default_kf_y_mode_cdf, sizeof(default_kf_y_mode_cdf));
987    memcpy(fc->angle_delta_cdf, default_angle_delta_cdf, sizeof(default_angle_delta_cdf));
988    memcpy(fc->comp_inter_cdf, default_comp_inter_cdf, sizeof(default_comp_inter_cdf));
989    memcpy(fc->comp_ref_type_cdf, default_comp_ref_type_cdf,sizeof(default_comp_ref_type_cdf));
990    memcpy(fc->uni_comp_ref_cdf, default_uni_comp_ref_cdf, sizeof(default_uni_comp_ref_cdf));
991    memcpy(fc->palette_y_mode_cdf, default_palette_y_mode_cdf, sizeof(default_palette_y_mode_cdf));
992    memcpy(fc->palette_uv_mode_cdf, default_palette_uv_mode_cdf, sizeof(default_palette_uv_mode_cdf));
993    memcpy(fc->comp_ref_cdf, default_comp_ref_cdf, sizeof(default_comp_ref_cdf));
994    memcpy(fc->comp_bwdref_cdf, default_comp_bwdref_cdf, sizeof(default_comp_bwdref_cdf));
995    memcpy(fc->single_ref_cdf, default_single_ref_cdf, sizeof(default_single_ref_cdf));
996    memcpy(fc->txfm_partition_cdf, default_txfm_partition_cdf, sizeof(default_txfm_partition_cdf));
997    memcpy(fc->compound_index_cdf, default_compound_idx_cdfs, sizeof(default_compound_idx_cdfs));
998    memcpy(fc->comp_group_idx_cdf, default_comp_group_idx_cdfs, sizeof(default_comp_group_idx_cdfs));
999    memcpy(fc->newmv_cdf, default_newmv_cdf, sizeof(default_newmv_cdf));
1000    memcpy(fc->zeromv_cdf, default_zeromv_cdf, sizeof(default_zeromv_cdf));
1001    memcpy(fc->refmv_cdf, default_refmv_cdf, sizeof(default_refmv_cdf));
1002    memcpy(fc->drl_cdf, default_drl_cdf, sizeof(default_drl_cdf));
1003    memcpy(fc->motion_mode_cdf, default_motion_mode_cdf, sizeof(default_motion_mode_cdf));
1004    memcpy(fc->obmc_cdf, default_obmc_cdf, sizeof(default_obmc_cdf));
1005    memcpy(fc->inter_compound_mode_cdf, default_inter_compound_mode_cdf, sizeof(default_inter_compound_mode_cdf));
1006    memcpy(fc->compound_type_cdf, default_compound_type_cdf, sizeof(default_compound_type_cdf));
1007    memcpy(fc->wedge_idx_cdf, default_wedge_idx_cdf, sizeof(default_wedge_idx_cdf));
1008    memcpy(fc->interintra_cdf, default_interintra_cdf, sizeof(default_interintra_cdf));
1009    memcpy(fc->wedge_interintra_cdf, default_wedge_interintra_cdf, sizeof(default_wedge_interintra_cdf));
1010    memcpy(fc->interintra_mode_cdf, default_interintra_mode_cdf, sizeof(default_interintra_mode_cdf));
1011    memcpy(fc->pred_cdf, default_segment_pred_cdf, sizeof(default_segment_pred_cdf));
1012    memcpy(fc->switchable_restore_cdf, default_switchable_restore_cdf, sizeof(default_switchable_restore_cdf));
1013    memcpy(fc->wiener_restore_cdf, default_wiener_restore_cdf, sizeof(default_wiener_restore_cdf));
1014    memcpy(fc->sgrproj_restore_cdf, default_sgrproj_restore_cdf, sizeof(default_sgrproj_restore_cdf));
1015    memcpy(fc->y_mode_cdf, default_if_y_mode_cdf, sizeof(default_if_y_mode_cdf));
1016    memcpy(fc->uv_mode_cdf, default_uv_mode_cdf, sizeof(default_uv_mode_cdf));
1017    memcpy(fc->switchable_interp_cdf, default_switchable_interp_cdf, sizeof(default_switchable_interp_cdf));
1018    memcpy(fc->partition_cdf, default_partition_cdf, sizeof(default_partition_cdf));
1019    memcpy(fc->intra_ext_tx_cdf, default_intra_ext_tx_cdf, sizeof(default_intra_ext_tx_cdf));
1020    memcpy(fc->inter_ext_tx_cdf, default_inter_ext_tx_cdf, sizeof(default_inter_ext_tx_cdf));
1021    memcpy(fc->skip_cdfs, default_skip_cdfs, sizeof(default_skip_cdfs));
1022    memcpy(fc->intra_inter_cdf, default_intra_inter_cdf, sizeof(default_intra_inter_cdf));
1023    memcpy(fc->tree_cdf, default_seg_tree_cdf, sizeof(default_seg_tree_cdf));
1024    for (i = 0; i < SPATIAL_PREDICTION_PROBS; ++i)
1025       memcpy(fc->spatial_pred_seg_cdf[i], default_spatial_pred_seg_tree_cdf[i], sizeof(default_spatial_pred_seg_tree_cdf[i]));
1026    memcpy(fc->tx_size_cdf, default_tx_size_cdf, sizeof(default_tx_size_cdf));
1027    memcpy(fc->delta_q_cdf, default_delta_q_cdf, sizeof(default_delta_q_cdf));
1028    memcpy(fc->skip_mode_cdfs, default_skip_mode_cdfs, sizeof(default_skip_mode_cdfs));
1029    memcpy(fc->delta_lf_cdf, default_delta_lf_cdf, sizeof(default_delta_lf_cdf));
1030    memcpy(fc->delta_lf_multi_cdf, default_delta_lf_multi_cdf, sizeof(default_delta_lf_multi_cdf));
1031    memcpy(fc->cfl_sign_cdf, default_cfl_sign_cdf, sizeof(default_cfl_sign_cdf));
1032    memcpy(fc->cfl_alpha_cdf, default_cfl_alpha_cdf, sizeof(default_cfl_alpha_cdf));
1033    memcpy(fc->filter_intra_cdfs, default_filter_intra_cdfs, sizeof(default_filter_intra_cdfs));
1034    memcpy(fc->filter_intra_mode_cdf, default_filter_intra_mode_cdf, sizeof(default_filter_intra_mode_cdf));
1035    memcpy(fc->intrabc_cdf, default_intrabc_cdf, sizeof(default_intrabc_cdf));
1036 }
1037 
rvcn_av1_init_mv_probs(void * prob)1038 static void rvcn_av1_init_mv_probs(void *prob)
1039 {
1040    rvcn_av1_frame_context_t * fc = (rvcn_av1_frame_context_t*)prob;
1041 
1042    memcpy(fc->nmvc_joints_cdf, default_nmv_context.joints_cdf, sizeof(default_nmv_context.joints_cdf));
1043    memcpy(fc->nmvc_0_bits_cdf, default_nmv_context.comps[0].bits_cdf, sizeof(default_nmv_context.comps[0].bits_cdf));
1044    memcpy(fc->nmvc_0_class0_cdf, default_nmv_context.comps[0].class0_cdf, sizeof(default_nmv_context.comps[0].class0_cdf));
1045    memcpy(fc->nmvc_0_class0_fp_cdf, default_nmv_context.comps[0].class0_fp_cdf, sizeof(default_nmv_context.comps[0].class0_fp_cdf));
1046    memcpy(fc->nmvc_0_class0_hp_cdf, default_nmv_context.comps[0].class0_hp_cdf, sizeof(default_nmv_context.comps[0].class0_hp_cdf));
1047    memcpy(fc->nmvc_0_classes_cdf, default_nmv_context.comps[0].classes_cdf, sizeof(default_nmv_context.comps[0].classes_cdf));
1048    memcpy(fc->nmvc_0_fp_cdf, default_nmv_context.comps[0].fp_cdf, sizeof(default_nmv_context.comps[0].fp_cdf));
1049    memcpy(fc->nmvc_0_hp_cdf, default_nmv_context.comps[0].hp_cdf, sizeof(default_nmv_context.comps[0].hp_cdf));
1050    memcpy(fc->nmvc_0_sign_cdf, default_nmv_context.comps[0].sign_cdf, sizeof(default_nmv_context.comps[0].sign_cdf));
1051    memcpy(fc->nmvc_1_bits_cdf, default_nmv_context.comps[1].bits_cdf, sizeof(default_nmv_context.comps[1].bits_cdf));
1052    memcpy(fc->nmvc_1_class0_cdf, default_nmv_context.comps[1].class0_cdf, sizeof(default_nmv_context.comps[1].class0_cdf));
1053    memcpy(fc->nmvc_1_class0_fp_cdf, default_nmv_context.comps[1].class0_fp_cdf, sizeof(default_nmv_context.comps[1].class0_fp_cdf));
1054    memcpy(fc->nmvc_1_class0_hp_cdf, default_nmv_context.comps[1].class0_hp_cdf, sizeof(default_nmv_context.comps[1].class0_hp_cdf));
1055    memcpy(fc->nmvc_1_classes_cdf, default_nmv_context.comps[1].classes_cdf, sizeof(default_nmv_context.comps[1].classes_cdf));
1056    memcpy(fc->nmvc_1_fp_cdf, default_nmv_context.comps[1].fp_cdf, sizeof(default_nmv_context.comps[1].fp_cdf));
1057    memcpy(fc->nmvc_1_hp_cdf, default_nmv_context.comps[1].hp_cdf, sizeof(default_nmv_context.comps[1].hp_cdf));
1058    memcpy(fc->nmvc_1_sign_cdf, default_nmv_context.comps[1].sign_cdf, sizeof(default_nmv_context.comps[1].sign_cdf));
1059    memcpy(fc->ndvc_joints_cdf, default_nmv_context.joints_cdf, sizeof(default_nmv_context.joints_cdf));
1060    memcpy(fc->ndvc_0_bits_cdf, default_nmv_context.comps[0].bits_cdf, sizeof(default_nmv_context.comps[0].bits_cdf));
1061    memcpy(fc->ndvc_0_class0_cdf, default_nmv_context.comps[0].class0_cdf, sizeof(default_nmv_context.comps[0].class0_cdf));
1062    memcpy(fc->ndvc_0_class0_fp_cdf, default_nmv_context.comps[0].class0_fp_cdf, sizeof(default_nmv_context.comps[0].class0_fp_cdf));
1063    memcpy(fc->ndvc_0_class0_hp_cdf, default_nmv_context.comps[0].class0_hp_cdf, sizeof(default_nmv_context.comps[0].class0_hp_cdf));
1064    memcpy(fc->ndvc_0_classes_cdf, default_nmv_context.comps[0].classes_cdf, sizeof(default_nmv_context.comps[0].classes_cdf));
1065    memcpy(fc->ndvc_0_fp_cdf, default_nmv_context.comps[0].fp_cdf, sizeof(default_nmv_context.comps[0].fp_cdf));
1066    memcpy(fc->ndvc_0_hp_cdf, default_nmv_context.comps[0].hp_cdf, sizeof(default_nmv_context.comps[0].hp_cdf));
1067    memcpy(fc->ndvc_0_sign_cdf, default_nmv_context.comps[0].sign_cdf, sizeof(default_nmv_context.comps[0].sign_cdf));
1068    memcpy(fc->ndvc_1_bits_cdf, default_nmv_context.comps[1].bits_cdf, sizeof(default_nmv_context.comps[1].bits_cdf));
1069    memcpy(fc->ndvc_1_class0_cdf, default_nmv_context.comps[1].class0_cdf, sizeof(default_nmv_context.comps[1].class0_cdf));
1070    memcpy(fc->ndvc_1_class0_fp_cdf, default_nmv_context.comps[1].class0_fp_cdf, sizeof(default_nmv_context.comps[1].class0_fp_cdf));
1071    memcpy(fc->ndvc_1_class0_hp_cdf, default_nmv_context.comps[1].class0_hp_cdf, sizeof(default_nmv_context.comps[1].class0_hp_cdf));
1072    memcpy(fc->ndvc_1_classes_cdf, default_nmv_context.comps[1].classes_cdf, sizeof(default_nmv_context.comps[1].classes_cdf));
1073    memcpy(fc->ndvc_1_fp_cdf, default_nmv_context.comps[1].fp_cdf, sizeof(default_nmv_context.comps[1].fp_cdf));
1074    memcpy(fc->ndvc_1_hp_cdf, default_nmv_context.comps[1].hp_cdf, sizeof(default_nmv_context.comps[1].hp_cdf));
1075    memcpy(fc->ndvc_1_sign_cdf, default_nmv_context.comps[1].sign_cdf, sizeof(default_nmv_context.comps[1].sign_cdf));
1076 }
1077 
rvcn_av1_default_coef_probs(void * prob,int index)1078 static void rvcn_av1_default_coef_probs(void *prob, int index)
1079 {
1080    rvcn_av1_frame_context_t * fc = (rvcn_av1_frame_context_t*)prob;
1081 
1082    memcpy(fc->txb_skip_cdf, av1_default_txb_skip_cdfs[index], sizeof(av1_default_txb_skip_cdfs[index]));
1083    memcpy(fc->eob_extra_cdf, av1_default_eob_extra_cdfs[index], sizeof(av1_default_eob_extra_cdfs[index]));
1084    memcpy(fc->dc_sign_cdf, av1_default_dc_sign_cdfs[index], sizeof(av1_default_dc_sign_cdfs[index]));
1085    memcpy(fc->coeff_br_cdf, av1_default_coeff_lps_multi_cdfs[index], sizeof(av1_default_coeff_lps_multi_cdfs[index]));
1086    memcpy(fc->coeff_base_cdf, av1_default_coeff_base_multi_cdfs[index], sizeof(av1_default_coeff_base_multi_cdfs[index]));
1087    memcpy(fc->coeff_base_eob_cdf, av1_default_coeff_base_eob_multi_cdfs[index], sizeof(av1_default_coeff_base_eob_multi_cdfs[index]));
1088    memcpy(fc->eob_flag_cdf16, av1_default_eob_multi16_cdfs[index], sizeof(av1_default_eob_multi16_cdfs[index]));
1089    memcpy(fc->eob_flag_cdf32, av1_default_eob_multi32_cdfs[index], sizeof(av1_default_eob_multi32_cdfs[index]));
1090    memcpy(fc->eob_flag_cdf64, av1_default_eob_multi64_cdfs[index], sizeof(av1_default_eob_multi64_cdfs[index]));
1091    memcpy(fc->eob_flag_cdf128, av1_default_eob_multi128_cdfs[index], sizeof(av1_default_eob_multi128_cdfs[index]));
1092    memcpy(fc->eob_flag_cdf256, av1_default_eob_multi256_cdfs[index], sizeof(av1_default_eob_multi256_cdfs[index]));
1093    memcpy(fc->eob_flag_cdf512, av1_default_eob_multi512_cdfs[index], sizeof(av1_default_eob_multi512_cdfs[index]));
1094    memcpy(fc->eob_flag_cdf1024, av1_default_eob_multi1024_cdfs[index], sizeof(av1_default_eob_multi1024_cdfs[index]));
1095 }
1096 
calc_ctx_size_h265_main(struct radeon_decoder * dec)1097 static unsigned calc_ctx_size_h265_main(struct radeon_decoder *dec)
1098 {
1099    unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1100    unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1101 
1102    unsigned max_references = dec->base.max_references + 1;
1103 
1104    if (dec->base.width * dec->base.height >= 4096 * 2000)
1105       max_references = MAX2(max_references, 8);
1106    else
1107       max_references = MAX2(max_references, 17);
1108 
1109    width = align(width, 16);
1110    height = align(height, 16);
1111    return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
1112 }
1113 
calc_ctx_size_h265_main10(struct radeon_decoder * dec,struct pipe_h265_picture_desc * pic)1114 static unsigned calc_ctx_size_h265_main10(struct radeon_decoder *dec,
1115                                           struct pipe_h265_picture_desc *pic)
1116 {
1117    unsigned log2_ctb_size, width_in_ctb, height_in_ctb, num_16x16_block_per_ctb;
1118    unsigned context_buffer_size_per_ctb_row, cm_buffer_size, max_mb_address, db_left_tile_pxl_size;
1119    unsigned db_left_tile_ctx_size = 4096 / 16 * (32 + 16 * 4);
1120 
1121    unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1122    unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1123    unsigned coeff_10bit =
1124       (pic->pps->sps->bit_depth_luma_minus8 || pic->pps->sps->bit_depth_chroma_minus8) ? 2 : 1;
1125 
1126    unsigned max_references = dec->base.max_references + 1;
1127 
1128    if (dec->base.width * dec->base.height >= 4096 * 2000)
1129       max_references = MAX2(max_references, 8);
1130    else
1131       max_references = MAX2(max_references, 17);
1132 
1133    log2_ctb_size = pic->pps->sps->log2_min_luma_coding_block_size_minus3 + 3 +
1134                    pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
1135 
1136    width_in_ctb = (width + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
1137    height_in_ctb = (height + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
1138 
1139    num_16x16_block_per_ctb = ((1 << log2_ctb_size) >> 4) * ((1 << log2_ctb_size) >> 4);
1140    context_buffer_size_per_ctb_row = align(width_in_ctb * num_16x16_block_per_ctb * 16, 256);
1141    max_mb_address = (unsigned)ceil(height * 8 / 2048.0);
1142 
1143    cm_buffer_size = max_references * context_buffer_size_per_ctb_row * height_in_ctb;
1144    db_left_tile_pxl_size = coeff_10bit * (max_mb_address * 2 * 2048 + 1024);
1145 
1146    return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
1147 }
1148 
get_vc1_msg(struct pipe_vc1_picture_desc * pic)1149 static rvcn_dec_message_vc1_t get_vc1_msg(struct pipe_vc1_picture_desc *pic)
1150 {
1151    rvcn_dec_message_vc1_t result;
1152 
1153    memset(&result, 0, sizeof(result));
1154    switch (pic->base.profile) {
1155    case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
1156       result.profile = RDECODE_VC1_PROFILE_SIMPLE;
1157       result.level = 1;
1158       break;
1159 
1160    case PIPE_VIDEO_PROFILE_VC1_MAIN:
1161       result.profile = RDECODE_VC1_PROFILE_MAIN;
1162       result.level = 2;
1163       break;
1164 
1165    case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
1166       result.profile = RDECODE_VC1_PROFILE_ADVANCED;
1167       result.level = 4;
1168       break;
1169 
1170    default:
1171       assert(0);
1172    }
1173 
1174    result.sps_info_flags |= pic->postprocflag << 7;
1175    result.sps_info_flags |= pic->pulldown << 6;
1176    result.sps_info_flags |= pic->interlace << 5;
1177    result.sps_info_flags |= pic->tfcntrflag << 4;
1178    result.sps_info_flags |= pic->finterpflag << 3;
1179    result.sps_info_flags |= pic->psf << 1;
1180 
1181    result.pps_info_flags |= pic->range_mapy_flag << 31;
1182    result.pps_info_flags |= pic->range_mapy << 28;
1183    result.pps_info_flags |= pic->range_mapuv_flag << 27;
1184    result.pps_info_flags |= pic->range_mapuv << 24;
1185    result.pps_info_flags |= pic->multires << 21;
1186    result.pps_info_flags |= pic->maxbframes << 16;
1187    result.pps_info_flags |= pic->overlap << 11;
1188    result.pps_info_flags |= pic->quantizer << 9;
1189    result.pps_info_flags |= pic->panscan_flag << 7;
1190    result.pps_info_flags |= pic->refdist_flag << 6;
1191    result.pps_info_flags |= pic->vstransform << 0;
1192 
1193    if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
1194       result.pps_info_flags |= pic->syncmarker << 20;
1195       result.pps_info_flags |= pic->rangered << 19;
1196       result.pps_info_flags |= pic->loopfilter << 5;
1197       result.pps_info_flags |= pic->fastuvmc << 4;
1198       result.pps_info_flags |= pic->extended_mv << 3;
1199       result.pps_info_flags |= pic->extended_dmv << 8;
1200       result.pps_info_flags |= pic->dquant << 1;
1201    }
1202 
1203    result.chroma_format = 1;
1204 
1205    return result;
1206 }
1207 
get_ref_pic_idx(struct radeon_decoder * dec,struct pipe_video_buffer * ref)1208 static uint32_t get_ref_pic_idx(struct radeon_decoder *dec, struct pipe_video_buffer *ref)
1209 {
1210    uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
1211    uint32_t max = MAX2(dec->frame_number, 1) - 1;
1212    uintptr_t frame;
1213 
1214    /* seems to be the most sane fallback */
1215    if (!ref)
1216       return max;
1217 
1218    /* get the frame number from the associated data */
1219    frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
1220 
1221    /* limit the frame number to a valid range */
1222    return MAX2(MIN2(frame, max), min);
1223 }
1224 
get_mpeg2_msg(struct radeon_decoder * dec,struct pipe_mpeg12_picture_desc * pic)1225 static rvcn_dec_message_mpeg2_vld_t get_mpeg2_msg(struct radeon_decoder *dec,
1226                                                   struct pipe_mpeg12_picture_desc *pic)
1227 {
1228    const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
1229    rvcn_dec_message_mpeg2_vld_t result;
1230    unsigned i;
1231 
1232    memset(&result, 0, sizeof(result));
1233    result.decoded_pic_idx = dec->frame_number;
1234 
1235    result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
1236    result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
1237 
1238    if (pic->intra_matrix) {
1239       result.load_intra_quantiser_matrix = 1;
1240       for (i = 0; i < 64; ++i) {
1241          result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
1242       }
1243    }
1244    if (pic->non_intra_matrix) {
1245       result.load_nonintra_quantiser_matrix = 1;
1246       for (i = 0; i < 64; ++i) {
1247          result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
1248       }
1249    }
1250 
1251    result.profile_and_level_indication = 0;
1252    result.chroma_format = 0x1;
1253 
1254    result.picture_coding_type = pic->picture_coding_type;
1255    result.f_code[0][0] = pic->f_code[0][0] + 1;
1256    result.f_code[0][1] = pic->f_code[0][1] + 1;
1257    result.f_code[1][0] = pic->f_code[1][0] + 1;
1258    result.f_code[1][1] = pic->f_code[1][1] + 1;
1259    result.intra_dc_precision = pic->intra_dc_precision;
1260    result.pic_structure = pic->picture_structure;
1261    result.top_field_first = pic->top_field_first;
1262    result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
1263    result.concealment_motion_vectors = pic->concealment_motion_vectors;
1264    result.q_scale_type = pic->q_scale_type;
1265    result.intra_vlc_format = pic->intra_vlc_format;
1266    result.alternate_scan = pic->alternate_scan;
1267 
1268    return result;
1269 }
1270 
get_mpeg4_msg(struct radeon_decoder * dec,struct pipe_mpeg4_picture_desc * pic)1271 static rvcn_dec_message_mpeg4_asp_vld_t get_mpeg4_msg(struct radeon_decoder *dec,
1272                                                       struct pipe_mpeg4_picture_desc *pic)
1273 {
1274    rvcn_dec_message_mpeg4_asp_vld_t result;
1275    unsigned i;
1276 
1277    memset(&result, 0, sizeof(result));
1278    result.decoded_pic_idx = dec->frame_number;
1279 
1280    result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
1281    result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
1282 
1283    result.variant_type = 0;
1284    result.profile_and_level_indication = 0xF0;
1285 
1286    result.video_object_layer_verid = 0x5;
1287    result.video_object_layer_shape = 0x0;
1288 
1289    result.video_object_layer_width = dec->base.width;
1290    result.video_object_layer_height = dec->base.height;
1291 
1292    result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
1293 
1294    result.short_video_header = pic->short_video_header;
1295    result.interlaced = pic->interlaced;
1296    result.load_intra_quant_mat = 1;
1297    result.load_nonintra_quant_mat = 1;
1298    result.quarter_sample = pic->quarter_sample;
1299    result.complexity_estimation_disable = 1;
1300    result.resync_marker_disable = pic->resync_marker_disable;
1301    result.newpred_enable = 0;
1302    result.reduced_resolution_vop_enable = 0;
1303 
1304    result.quant_type = pic->quant_type;
1305 
1306    for (i = 0; i < 64; ++i) {
1307       result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
1308       result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
1309    }
1310 
1311    return result;
1312 }
1313 
rvcn_dec_message_create(struct radeon_decoder * dec)1314 static void rvcn_dec_message_create(struct radeon_decoder *dec)
1315 {
1316    rvcn_dec_message_header_t *header = dec->msg;
1317    rvcn_dec_message_create_t *create = dec->msg + sizeof(rvcn_dec_message_header_t);
1318    unsigned sizes = sizeof(rvcn_dec_message_header_t) + sizeof(rvcn_dec_message_create_t);
1319 
1320    memset(dec->msg, 0, sizes);
1321    header->header_size = sizeof(rvcn_dec_message_header_t);
1322    header->total_size = sizes;
1323    header->num_buffers = 1;
1324    header->msg_type = RDECODE_MSG_CREATE;
1325    header->stream_handle = dec->stream_handle;
1326    header->status_report_feedback_number = 0;
1327 
1328    header->index[0].message_id = RDECODE_MESSAGE_CREATE;
1329    header->index[0].offset = sizeof(rvcn_dec_message_header_t);
1330    header->index[0].size = sizeof(rvcn_dec_message_create_t);
1331    header->index[0].filled = 0;
1332 
1333    create->stream_type = dec->stream_type;
1334    create->session_flags = 0;
1335    create->width_in_samples = dec->base.width;
1336    create->height_in_samples = dec->base.height;
1337 }
1338 
rvcn_dec_dynamic_dpb_t2_message(struct radeon_decoder * dec,rvcn_dec_message_decode_t * decode,rvcn_dec_message_dynamic_dpb_t2_t * dynamic_dpb_t2)1339 static unsigned rvcn_dec_dynamic_dpb_t2_message(struct radeon_decoder *dec, rvcn_dec_message_decode_t *decode,
1340       rvcn_dec_message_dynamic_dpb_t2_t *dynamic_dpb_t2)
1341 {
1342    struct rvcn_dec_dynamic_dpb_t2 *dpb = NULL, *dummy = NULL;
1343    unsigned width, height, size;
1344    uint64_t addr;
1345    int i;
1346 
1347    width = align(decode->width_in_samples, dec->db_alignment);
1348    height = align(decode->height_in_samples, dec->db_alignment);
1349    size = align((width * height * 3) / 2, 256);
1350    if (dec->ref_codec.bts == CODEC_10_BITS)
1351       size = size * 3 / 2;
1352 
1353    list_for_each_entry_safe(struct rvcn_dec_dynamic_dpb_t2, d, &dec->dpb_ref_list, list) {
1354       for (i = 0; i < dec->ref_codec.ref_size; ++i) {
1355          if ((dec->ref_codec.ref_list[i] != 0x7f) && (d->index == (dec->ref_codec.ref_list[i] & 0x7f))) {
1356             if (!dummy)
1357                dummy = d;
1358 
1359             addr = dec->ws->buffer_get_virtual_address(d->dpb.res->buf);
1360             if (!addr && dummy) {
1361                RVID_ERR("Ref list from application is incorrect, using dummy buffer instead.\n");
1362                addr = dec->ws->buffer_get_virtual_address(dummy->dpb.res->buf);
1363             }
1364             dynamic_dpb_t2->dpbAddrLo[i] = addr;
1365             dynamic_dpb_t2->dpbAddrHi[i] = addr >> 32;
1366             ++dynamic_dpb_t2->dpbArraySize;
1367             break;
1368          }
1369       }
1370       if (i == dec->ref_codec.ref_size) {
1371          if (d->dpb.res->b.b.width0 * d->dpb.res->b.b.height0 != size) {
1372             list_del(&d->list);
1373             list_addtail(&d->list, &dec->dpb_unref_list);
1374          } else {
1375             d->index = 0x7f;
1376          }
1377       }
1378    }
1379 
1380    list_for_each_entry_safe(struct rvcn_dec_dynamic_dpb_t2, d, &dec->dpb_ref_list, list) {
1381       if (d->dpb.res->b.b.width0 * d->dpb.res->b.b.height0 == size && d->index == dec->ref_codec.index) {
1382          dpb = d;
1383          break;
1384       }
1385    }
1386 
1387    if (!dpb) {
1388       list_for_each_entry_safe(struct rvcn_dec_dynamic_dpb_t2, d, &dec->dpb_ref_list, list) {
1389          if (d->index == 0x7f) {
1390             d->index = dec->ref_codec.index;
1391             dpb = d;
1392             break;
1393          }
1394       }
1395    }
1396 
1397    list_for_each_entry_safe(struct rvcn_dec_dynamic_dpb_t2, d, &dec->dpb_unref_list, list) {
1398       list_del(&d->list);
1399       si_vid_destroy_buffer(&d->dpb);
1400       FREE(d);
1401    }
1402 
1403    if (!dpb) {
1404       dpb = CALLOC_STRUCT(rvcn_dec_dynamic_dpb_t2);
1405       if (!dpb)
1406          return 1;
1407       dpb->index = dec->ref_codec.index;
1408       if (!si_vid_create_buffer(dec->screen, &dpb->dpb, size, PIPE_USAGE_DEFAULT)) {
1409          RVID_ERR("Can't allocated dpb buffer.\n");
1410          FREE(dpb);
1411          return 1;
1412       }
1413       list_addtail(&dpb->list, &dec->dpb_ref_list);
1414    }
1415 
1416    dec->ws->cs_add_buffer(&dec->cs, dpb->dpb.res->buf,
1417       RADEON_USAGE_READWRITE | RADEON_USAGE_SYNCHRONIZED, RADEON_DOMAIN_VRAM, 0);
1418    addr = dec->ws->buffer_get_virtual_address(dpb->dpb.res->buf);
1419    dynamic_dpb_t2->dpbCurrLo = addr;
1420    dynamic_dpb_t2->dpbCurrHi = addr >> 32;
1421 
1422    decode->decode_flags = 1;
1423    dynamic_dpb_t2->dpbConfigFlags = 0;
1424    dynamic_dpb_t2->dpbLumaPitch = align(decode->width_in_samples, dec->db_alignment);
1425    dynamic_dpb_t2->dpbLumaAlignedHeight = align(decode->height_in_samples, dec->db_alignment);
1426    dynamic_dpb_t2->dpbLumaAlignedSize = dynamic_dpb_t2->dpbLumaPitch *
1427       dynamic_dpb_t2->dpbLumaAlignedHeight;
1428    dynamic_dpb_t2->dpbChromaPitch = dynamic_dpb_t2->dpbLumaPitch >> 1;
1429    dynamic_dpb_t2->dpbChromaAlignedHeight = dynamic_dpb_t2->dpbLumaAlignedHeight >> 1;
1430    dynamic_dpb_t2->dpbChromaAlignedSize = dynamic_dpb_t2->dpbChromaPitch *
1431       dynamic_dpb_t2->dpbChromaAlignedHeight * 2;
1432 
1433    if (dec->ref_codec.bts == CODEC_10_BITS) {
1434       dynamic_dpb_t2->dpbLumaAlignedSize = dynamic_dpb_t2->dpbLumaAlignedSize * 3 / 2;
1435       dynamic_dpb_t2->dpbChromaAlignedSize = dynamic_dpb_t2->dpbChromaAlignedSize * 3 / 2;
1436    }
1437 
1438    return 0;
1439 }
1440 
rvcn_dec_message_decode(struct radeon_decoder * dec,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)1441 static struct pb_buffer *rvcn_dec_message_decode(struct radeon_decoder *dec,
1442                                                  struct pipe_video_buffer *target,
1443                                                  struct pipe_picture_desc *picture)
1444 {
1445    DECRYPT_PARAMETERS *decrypt = (DECRYPT_PARAMETERS *)picture->decrypt_key;
1446    bool encrypted = (DECRYPT_PARAMETERS *)picture->protected_playback;
1447    struct si_texture *luma = (struct si_texture *)((struct vl_video_buffer *)target)->resources[0];
1448    struct si_texture *chroma =
1449       (struct si_texture *)((struct vl_video_buffer *)target)->resources[1];
1450    ASSERTED struct si_screen *sscreen = (struct si_screen *)dec->screen;
1451    rvcn_dec_message_header_t *header;
1452    rvcn_dec_message_index_t *index_codec;
1453    rvcn_dec_message_index_t *index_drm = NULL;
1454    rvcn_dec_message_index_t *index_dynamic_dpb = NULL;
1455    rvcn_dec_message_decode_t *decode;
1456    unsigned sizes = 0, offset_decode, offset_codec;
1457    unsigned offset_drm = 0, offset_dynamic_dpb = 0;
1458    void *codec;
1459    rvcn_dec_message_drm_t *drm = NULL;
1460    rvcn_dec_message_dynamic_dpb_t *dynamic_dpb = NULL;
1461    rvcn_dec_message_dynamic_dpb_t2_t *dynamic_dpb_t2 = NULL;
1462 
1463    header = dec->msg;
1464    sizes += sizeof(rvcn_dec_message_header_t);
1465 
1466    index_codec = (void*)header + sizes;
1467    sizes += sizeof(rvcn_dec_message_index_t);
1468 
1469    if (encrypted) {
1470       index_drm = (void*)header + sizes;
1471       sizes += sizeof(rvcn_dec_message_index_t);
1472    }
1473 
1474    if (dec->dpb_type >= DPB_DYNAMIC_TIER_1) {
1475       index_dynamic_dpb = (void*)header + sizes;
1476       sizes += sizeof(rvcn_dec_message_index_t);
1477    }
1478 
1479    offset_decode = sizes;
1480    decode = (void*)header + sizes;
1481    sizes += sizeof(rvcn_dec_message_decode_t);
1482 
1483    if (encrypted) {
1484       offset_drm = sizes;
1485       drm = (void*)header + sizes;
1486       sizes += sizeof(rvcn_dec_message_drm_t);
1487    }
1488 
1489    if (dec->dpb_type >= DPB_DYNAMIC_TIER_1) {
1490       offset_dynamic_dpb = sizes;
1491       if (dec->dpb_type == DPB_DYNAMIC_TIER_1) {
1492          dynamic_dpb = (void*)header + sizes;
1493          sizes += sizeof(rvcn_dec_message_dynamic_dpb_t);
1494       }
1495       else if (dec->dpb_type == DPB_DYNAMIC_TIER_2) {
1496          dynamic_dpb_t2 = (void*)header + sizes;
1497          sizes += sizeof(rvcn_dec_message_dynamic_dpb_t2_t);
1498       }
1499    }
1500 
1501    offset_codec = sizes;
1502    codec = (void*)header + sizes;
1503 
1504    memset(dec->msg, 0, sizes);
1505    header->header_size = sizeof(rvcn_dec_message_header_t);
1506    header->total_size = sizes;
1507    header->msg_type = RDECODE_MSG_DECODE;
1508    header->stream_handle = dec->stream_handle;
1509    header->status_report_feedback_number = dec->frame_number;
1510 
1511    header->index[0].message_id = RDECODE_MESSAGE_DECODE;
1512    header->index[0].offset = offset_decode;
1513    header->index[0].size = sizeof(rvcn_dec_message_decode_t);
1514    header->index[0].filled = 0;
1515    header->num_buffers = 1;
1516 
1517    index_codec->offset = offset_codec;
1518    index_codec->size = sizeof(rvcn_dec_message_avc_t);
1519    index_codec->filled = 0;
1520    ++header->num_buffers;
1521 
1522    if (encrypted) {
1523       index_drm->message_id = RDECODE_MESSAGE_DRM;
1524       index_drm->offset = offset_drm;
1525       index_drm->size = sizeof(rvcn_dec_message_drm_t);
1526       index_drm->filled = 0;
1527       ++header->num_buffers;
1528    }
1529 
1530    if (dec->dpb_type >= DPB_DYNAMIC_TIER_1) {
1531       index_dynamic_dpb->message_id = RDECODE_MESSAGE_DYNAMIC_DPB;
1532       index_dynamic_dpb->offset = offset_dynamic_dpb;
1533       index_dynamic_dpb->filled = 0;
1534       ++header->num_buffers;
1535       if (dec->dpb_type == DPB_DYNAMIC_TIER_1)
1536          index_dynamic_dpb->size = sizeof(rvcn_dec_message_dynamic_dpb_t);
1537       else if (dec->dpb_type == DPB_DYNAMIC_TIER_2)
1538          index_dynamic_dpb->size = sizeof(rvcn_dec_message_dynamic_dpb_t2_t);
1539    }
1540 
1541    decode->stream_type = dec->stream_type;
1542    decode->decode_flags = 0;
1543    decode->width_in_samples = dec->base.width;
1544    decode->height_in_samples = dec->base.height;
1545 
1546    decode->bsd_size = align(dec->bs_size, 128);
1547 
1548    if (!dec->dpb.res && dec->dpb_type != DPB_DYNAMIC_TIER_2) {
1549       bool r;
1550       if (dec->dpb_size) {
1551          if (encrypted) {
1552             r = si_vid_create_tmz_buffer(dec->screen, &dec->dpb, dec->dpb_size, PIPE_USAGE_DEFAULT);
1553          } else {
1554             r = si_vid_create_buffer(dec->screen, &dec->dpb, dec->dpb_size, PIPE_USAGE_DEFAULT);
1555          }
1556          assert(encrypted == (bool)(dec->dpb.res->flags & RADEON_FLAG_ENCRYPTED));
1557          if (!r) {
1558             RVID_ERR("Can't allocated dpb.\n");
1559             return NULL;
1560          }
1561          si_vid_clear_buffer(dec->base.context, &dec->dpb);
1562       }
1563    }
1564 
1565    if (!dec->ctx.res) {
1566       enum pipe_video_format fmt = u_reduce_video_profile(picture->profile);
1567       if (dec->stream_type == RDECODE_CODEC_H264_PERF) {
1568          unsigned ctx_size = calc_ctx_size_h264_perf(dec);
1569          bool r;
1570          if (encrypted && dec->tmz_ctx) {
1571             r = si_vid_create_tmz_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1572          } else {
1573             r = si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1574          }
1575          assert((encrypted && dec->tmz_ctx) == (bool)(dec->ctx.res->flags & RADEON_FLAG_ENCRYPTED));
1576 
1577          if (!r) {
1578             RVID_ERR("Can't allocated context buffer.\n");
1579             return NULL;
1580          }
1581          si_vid_clear_buffer(dec->base.context, &dec->ctx);
1582       } else if (fmt == PIPE_VIDEO_FORMAT_VP9) {
1583          unsigned ctx_size;
1584          uint8_t *ptr;
1585          bool r;
1586 
1587          /* default probability + probability data */
1588          ctx_size = 2304 * 5;
1589 
1590          if (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR) {
1591             /* SRE collocated context data */
1592             ctx_size += 32 * 2 * 128 * 68;
1593             /* SMP collocated context data */
1594             ctx_size += 9 * 64 * 2 * 128 * 68;
1595             /* SDB left tile pixel */
1596             ctx_size += 8 * 2 * 2 * 8192;
1597          } else {
1598             ctx_size += 32 * 2 * 64 * 64;
1599             ctx_size += 9 * 64 * 2 * 64 * 64;
1600             ctx_size += 8 * 2 * 4096;
1601          }
1602 
1603          if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
1604             ctx_size += 8 * 2 * 4096;
1605 
1606          if (encrypted && dec->tmz_ctx) {
1607             r = si_vid_create_tmz_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1608          } else {
1609             r = si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1610          }
1611          if (!r) {
1612             RVID_ERR("Can't allocated context buffer.\n");
1613             return NULL;
1614          }
1615          si_vid_clear_buffer(dec->base.context, &dec->ctx);
1616 
1617          /* ctx needs probs table */
1618          ptr = dec->ws->buffer_map(dec->ws, dec->ctx.res->buf, &dec->cs,
1619                                    PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1620          fill_probs_table(ptr);
1621          dec->ws->buffer_unmap(dec->ws, dec->ctx.res->buf);
1622          dec->bs_ptr = NULL;
1623       } else if (fmt == PIPE_VIDEO_FORMAT_HEVC) {
1624          unsigned ctx_size;
1625          bool r;
1626          if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
1627             ctx_size = calc_ctx_size_h265_main10(dec, (struct pipe_h265_picture_desc *)picture);
1628          else
1629             ctx_size = calc_ctx_size_h265_main(dec);
1630 
1631          if (encrypted && dec->tmz_ctx) {
1632             r = si_vid_create_tmz_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1633          } else {
1634             r = si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT);
1635          }
1636          if (!r) {
1637             RVID_ERR("Can't allocated context buffer.\n");
1638             return NULL;
1639          }
1640          si_vid_clear_buffer(dec->base.context, &dec->ctx);
1641       }
1642    }
1643    if (encrypted != dec->ws->cs_is_secure(&dec->cs)) {
1644       dec->ws->cs_flush(&dec->cs, RADEON_FLUSH_TOGGLE_SECURE_SUBMISSION, NULL);
1645    }
1646 
1647    decode->dpb_size = (dec->dpb_type != DPB_DYNAMIC_TIER_2) ? dec->dpb.res->buf->size : 0;
1648    decode->dt_size = si_resource(((struct vl_video_buffer *)target)->resources[0])->buf->size +
1649                      si_resource(((struct vl_video_buffer *)target)->resources[1])->buf->size;
1650 
1651    decode->sct_size = 0;
1652    decode->sc_coeff_size = 0;
1653 
1654    decode->sw_ctxt_size = RDECODE_SESSION_CONTEXT_SIZE;
1655    decode->db_pitch = align(dec->base.width, dec->db_alignment);
1656 
1657    if (((struct si_screen*)dec->screen)->info.family >= CHIP_SIENNA_CICHLID &&
1658        (dec->stream_type == RDECODE_CODEC_VP9 || dec->stream_type == RDECODE_CODEC_AV1 ||
1659         dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10))
1660       decode->db_aligned_height = align(dec->base.height, 64);
1661 
1662    decode->db_surf_tile_config = 0;
1663 
1664    decode->dt_pitch = luma->surface.u.gfx9.surf_pitch * luma->surface.blk_w;
1665    decode->dt_uv_pitch = chroma->surface.u.gfx9.surf_pitch * chroma->surface.blk_w;
1666 
1667    if (luma->surface.meta_offset) {
1668       RVID_ERR("DCC surfaces not supported.\n");
1669       return NULL;
1670    }
1671 
1672    decode->dt_tiling_mode = 0;
1673    decode->dt_swizzle_mode = luma->surface.u.gfx9.swizzle_mode;
1674    decode->dt_array_mode = RDECODE_ARRAY_MODE_LINEAR;
1675    decode->dt_field_mode = ((struct vl_video_buffer *)target)->base.interlaced;
1676    decode->dt_surf_tile_config = 0;
1677    decode->dt_uv_surf_tile_config = 0;
1678 
1679    decode->dt_luma_top_offset = luma->surface.u.gfx9.surf_offset;
1680    decode->dt_chroma_top_offset = chroma->surface.u.gfx9.surf_offset;
1681    if (decode->dt_field_mode) {
1682       decode->dt_luma_bottom_offset =
1683          luma->surface.u.gfx9.surf_offset + luma->surface.u.gfx9.surf_slice_size;
1684       decode->dt_chroma_bottom_offset =
1685          chroma->surface.u.gfx9.surf_offset + chroma->surface.u.gfx9.surf_slice_size;
1686    } else {
1687       decode->dt_luma_bottom_offset = decode->dt_luma_top_offset;
1688       decode->dt_chroma_bottom_offset = decode->dt_chroma_top_offset;
1689    }
1690    if (dec->stream_type == RDECODE_CODEC_AV1)
1691       decode->db_pitch_uv = chroma->surface.u.gfx9.surf_pitch * chroma->surface.blk_w;
1692 
1693    if (encrypted) {
1694       assert(sscreen->info.has_tmz_support);
1695       set_drm_keys(drm, decrypt);
1696    }
1697 
1698    if (dec->dpb_type == DPB_DYNAMIC_TIER_1) {
1699       decode->decode_flags = 1;
1700       dynamic_dpb->dpbArraySize = NUM_VP9_REFS + 1;
1701       dynamic_dpb->dpbLumaPitch = align(decode->width_in_samples, dec->db_alignment);
1702       dynamic_dpb->dpbLumaAlignedHeight = align(decode->height_in_samples, dec->db_alignment);
1703       dynamic_dpb->dpbLumaAlignedSize =
1704          dynamic_dpb->dpbLumaPitch * dynamic_dpb->dpbLumaAlignedHeight;
1705       dynamic_dpb->dpbChromaPitch = dynamic_dpb->dpbLumaPitch >> 1;
1706       dynamic_dpb->dpbChromaAlignedHeight = dynamic_dpb->dpbLumaAlignedHeight >> 1;
1707       dynamic_dpb->dpbChromaAlignedSize =
1708          dynamic_dpb->dpbChromaPitch * dynamic_dpb->dpbChromaAlignedHeight * 2;
1709       dynamic_dpb->dpbReserved0[0] = dec->db_alignment;
1710 
1711       if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2) {
1712          dynamic_dpb->dpbLumaAlignedSize = dynamic_dpb->dpbLumaAlignedSize * 3 / 2;
1713          dynamic_dpb->dpbChromaAlignedSize = dynamic_dpb->dpbChromaAlignedSize * 3 / 2;
1714       }
1715    }
1716 
1717    switch (u_reduce_video_profile(picture->profile)) {
1718    case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
1719       rvcn_dec_message_avc_t avc = get_h264_msg(dec, (struct pipe_h264_picture_desc *)picture);
1720       memcpy(codec, (void *)&avc, sizeof(rvcn_dec_message_avc_t));
1721       index_codec->message_id = RDECODE_MESSAGE_AVC;
1722       break;
1723    }
1724    case PIPE_VIDEO_FORMAT_HEVC: {
1725       rvcn_dec_message_hevc_t hevc =
1726          get_h265_msg(dec, target, (struct pipe_h265_picture_desc *)picture);
1727 
1728       memcpy(codec, (void *)&hevc, sizeof(rvcn_dec_message_hevc_t));
1729       index_codec->message_id = RDECODE_MESSAGE_HEVC;
1730       break;
1731    }
1732    case PIPE_VIDEO_FORMAT_VC1: {
1733       rvcn_dec_message_vc1_t vc1 = get_vc1_msg((struct pipe_vc1_picture_desc *)picture);
1734 
1735       memcpy(codec, (void *)&vc1, sizeof(rvcn_dec_message_vc1_t));
1736       if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
1737           (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
1738          decode->width_in_samples = align(decode->width_in_samples, 16) / 16;
1739          decode->height_in_samples = align(decode->height_in_samples, 16) / 16;
1740       }
1741       index_codec->message_id = RDECODE_MESSAGE_VC1;
1742       break;
1743    }
1744    case PIPE_VIDEO_FORMAT_MPEG12: {
1745       rvcn_dec_message_mpeg2_vld_t mpeg2 =
1746          get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc *)picture);
1747 
1748       memcpy(codec, (void *)&mpeg2, sizeof(rvcn_dec_message_mpeg2_vld_t));
1749       index_codec->message_id = RDECODE_MESSAGE_MPEG2_VLD;
1750       break;
1751    }
1752    case PIPE_VIDEO_FORMAT_MPEG4: {
1753       rvcn_dec_message_mpeg4_asp_vld_t mpeg4 =
1754          get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc *)picture);
1755 
1756       memcpy(codec, (void *)&mpeg4, sizeof(rvcn_dec_message_mpeg4_asp_vld_t));
1757       index_codec->message_id = RDECODE_MESSAGE_MPEG4_ASP_VLD;
1758       break;
1759    }
1760    case PIPE_VIDEO_FORMAT_VP9: {
1761       rvcn_dec_message_vp9_t vp9 =
1762          get_vp9_msg(dec, target, (struct pipe_vp9_picture_desc *)picture);
1763 
1764       memcpy(codec, (void *)&vp9, sizeof(rvcn_dec_message_vp9_t));
1765       index_codec->message_id = RDECODE_MESSAGE_VP9;
1766       break;
1767    }
1768    case PIPE_VIDEO_FORMAT_AV1: {
1769       rvcn_dec_message_av1_t av1 =
1770          get_av1_msg(dec, target, (struct pipe_av1_picture_desc *)picture);
1771 
1772       memcpy(codec, (void *)&av1, sizeof(rvcn_dec_message_av1_t));
1773       index_codec->message_id = RDECODE_MESSAGE_AV1;
1774 
1775       if (dec->ctx.res == NULL) {
1776          unsigned ctx_size = (9 + 4) * align(sizeof(rvcn_av1_hw_frame_context_t), 2048) +
1777                              9 * 64 * 34 * 512 + 9 * 64 * 34 * 256 * 5;
1778          int num_64x64_CTB_8k = 68;
1779          int num_128x128_CTB_8k = 34;
1780          int sdb_pitch_64x64 = align(32 * num_64x64_CTB_8k, 256);
1781          int sdb_pitch_128x128 = align(32 * num_128x128_CTB_8k, 256);
1782          int sdb_lf_size_ctb_64x64 = sdb_pitch_64x64 * (1728 / 32);
1783          int sdb_lf_size_ctb_128x128 = sdb_pitch_128x128 * (3008 / 32);
1784          int sdb_superres_size_ctb_64x64 = sdb_pitch_64x64 * (3232 / 32);
1785          int sdb_superres_size_ctb_128x128 = sdb_pitch_128x128 * (6208 / 32);
1786          int sdb_output_size_ctb_64x64 = sdb_pitch_64x64 * (1312 / 32);
1787          int sdb_output_size_ctb_128x128 = sdb_pitch_128x128 * (2336 / 32);
1788          int sdb_fg_avg_luma_size_ctb_64x64 = sdb_pitch_64x64 * (384 / 32);
1789          int sdb_fg_avg_luma_size_ctb_128x128 = sdb_pitch_128x128 * (640 / 32);
1790          uint8_t *ptr;
1791          int i;
1792 
1793          ctx_size += (MAX2(sdb_lf_size_ctb_64x64, sdb_lf_size_ctb_128x128) +
1794                       MAX2(sdb_superres_size_ctb_64x64, sdb_superres_size_ctb_128x128) +
1795                       MAX2(sdb_output_size_ctb_64x64, sdb_output_size_ctb_128x128) +
1796                       MAX2(sdb_fg_avg_luma_size_ctb_64x64, sdb_fg_avg_luma_size_ctb_128x128)) * 2  + 68 * 512;
1797 
1798          if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT))
1799             RVID_ERR("Can't allocated context buffer.\n");
1800          si_vid_clear_buffer(dec->base.context, &dec->ctx);
1801 
1802          ptr = dec->ws->buffer_map(dec->ws, dec->ctx.res->buf, &dec->cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1803 
1804          for (i = 0; i < 4; ++i) {
1805             rvcn_init_mode_probs((void*)(ptr + i * align(sizeof(rvcn_av1_frame_context_t), 2048)));
1806             rvcn_av1_init_mv_probs((void*)(ptr + i * align(sizeof(rvcn_av1_frame_context_t), 2048)));
1807             rvcn_av1_default_coef_probs((void*)(ptr + i * align(sizeof(rvcn_av1_frame_context_t), 2048)), i);
1808          }
1809          dec->ws->buffer_unmap(dec->ws, dec->ctx.res->buf);
1810       }
1811 
1812       break;
1813    }
1814    default:
1815       assert(0);
1816       return NULL;
1817    }
1818 
1819    if (dec->ctx.res)
1820       decode->hw_ctxt_size = dec->ctx.res->buf->size;
1821 
1822    if (dec->dpb_type == DPB_DYNAMIC_TIER_2)
1823       if (rvcn_dec_dynamic_dpb_t2_message(dec, decode, dynamic_dpb_t2))
1824          return NULL;
1825 
1826    return luma->buffer.buf;
1827 }
1828 
rvcn_dec_message_destroy(struct radeon_decoder * dec)1829 static void rvcn_dec_message_destroy(struct radeon_decoder *dec)
1830 {
1831    rvcn_dec_message_header_t *header = dec->msg;
1832 
1833    memset(dec->msg, 0, sizeof(rvcn_dec_message_header_t));
1834    header->header_size = sizeof(rvcn_dec_message_header_t);
1835    header->total_size = sizeof(rvcn_dec_message_header_t) - sizeof(rvcn_dec_message_index_t);
1836    header->num_buffers = 0;
1837    header->msg_type = RDECODE_MSG_DESTROY;
1838    header->stream_handle = dec->stream_handle;
1839    header->status_report_feedback_number = 0;
1840 }
1841 
rvcn_dec_message_feedback(struct radeon_decoder * dec)1842 static void rvcn_dec_message_feedback(struct radeon_decoder *dec)
1843 {
1844    rvcn_dec_feedback_header_t *header = (void *)dec->fb;
1845 
1846    header->header_size = sizeof(rvcn_dec_feedback_header_t);
1847    header->total_size = sizeof(rvcn_dec_feedback_header_t);
1848    header->num_buffers = 0;
1849 }
1850 
1851 /* flush IB to the hardware */
flush(struct radeon_decoder * dec,unsigned flags)1852 static int flush(struct radeon_decoder *dec, unsigned flags)
1853 {
1854    return dec->ws->cs_flush(&dec->cs, flags, NULL);
1855 }
1856 
1857 /* add a new set register command to the IB */
set_reg(struct radeon_decoder * dec,unsigned reg,uint32_t val)1858 static void set_reg(struct radeon_decoder *dec, unsigned reg, uint32_t val)
1859 {
1860    radeon_emit(&dec->cs, RDECODE_PKT0(reg >> 2, 0));
1861    radeon_emit(&dec->cs, val);
1862 }
1863 
1864 /* send a command to the VCPU through the GPCOM registers */
send_cmd(struct radeon_decoder * dec,unsigned cmd,struct pb_buffer * buf,uint32_t off,enum radeon_bo_usage usage,enum radeon_bo_domain domain)1865 static void send_cmd(struct radeon_decoder *dec, unsigned cmd, struct pb_buffer *buf, uint32_t off,
1866                      enum radeon_bo_usage usage, enum radeon_bo_domain domain)
1867 {
1868    uint64_t addr;
1869 
1870    dec->ws->cs_add_buffer(&dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain, 0);
1871    addr = dec->ws->buffer_get_virtual_address(buf);
1872    addr = addr + off;
1873 
1874    set_reg(dec, dec->reg.data0, addr);
1875    set_reg(dec, dec->reg.data1, addr >> 32);
1876    set_reg(dec, dec->reg.cmd, cmd << 1);
1877 }
1878 
1879 /* do the codec needs an IT buffer ?*/
have_it(struct radeon_decoder * dec)1880 static bool have_it(struct radeon_decoder *dec)
1881 {
1882    return dec->stream_type == RDECODE_CODEC_H264_PERF || dec->stream_type == RDECODE_CODEC_H265;
1883 }
1884 
1885 /* do the codec needs an probs buffer? */
have_probs(struct radeon_decoder * dec)1886 static bool have_probs(struct radeon_decoder *dec)
1887 {
1888    return (dec->stream_type == RDECODE_CODEC_VP9 || dec->stream_type == RDECODE_CODEC_AV1);
1889 }
1890 
1891 /* map the next available message/feedback/itscaling buffer */
map_msg_fb_it_probs_buf(struct radeon_decoder * dec)1892 static void map_msg_fb_it_probs_buf(struct radeon_decoder *dec)
1893 {
1894    struct rvid_buffer *buf;
1895    uint8_t *ptr;
1896 
1897    /* grab the current message/feedback buffer */
1898    buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1899 
1900    /* and map it for CPU access */
1901    ptr =
1902       dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1903 
1904    /* calc buffer offsets */
1905    dec->msg = ptr;
1906 
1907    dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
1908    if (have_it(dec))
1909       dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1910    else if (have_probs(dec))
1911       dec->probs = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1912 }
1913 
1914 /* unmap and send a message command to the VCPU */
send_msg_buf(struct radeon_decoder * dec)1915 static void send_msg_buf(struct radeon_decoder *dec)
1916 {
1917    struct rvid_buffer *buf;
1918 
1919    /* ignore the request if message/feedback buffer isn't mapped */
1920    if (!dec->msg || !dec->fb)
1921       return;
1922 
1923    /* grab the current message buffer */
1924    buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1925 
1926    /* unmap the buffer */
1927    dec->ws->buffer_unmap(dec->ws, buf->res->buf);
1928    dec->bs_ptr = NULL;
1929    dec->msg = NULL;
1930    dec->fb = NULL;
1931    dec->it = NULL;
1932    dec->probs = NULL;
1933 
1934    if (dec->sessionctx.res)
1935       send_cmd(dec, RDECODE_CMD_SESSION_CONTEXT_BUFFER, dec->sessionctx.res->buf, 0,
1936                RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1937 
1938    /* and send it to the hardware */
1939    send_cmd(dec, RDECODE_CMD_MSG_BUFFER, buf->res->buf, 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1940 }
1941 
1942 /* cycle to the next set of buffers */
next_buffer(struct radeon_decoder * dec)1943 static void next_buffer(struct radeon_decoder *dec)
1944 {
1945    ++dec->cur_buffer;
1946    dec->cur_buffer %= NUM_BUFFERS;
1947 }
1948 
calc_ctx_size_h264_perf(struct radeon_decoder * dec)1949 static unsigned calc_ctx_size_h264_perf(struct radeon_decoder *dec)
1950 {
1951    unsigned width_in_mb, height_in_mb, ctx_size;
1952    unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1953    unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1954 
1955    unsigned max_references = dec->base.max_references + 1;
1956 
1957    // picture width & height in 16 pixel units
1958    width_in_mb = width / VL_MACROBLOCK_WIDTH;
1959    height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
1960 
1961    unsigned fs_in_mb = width_in_mb * height_in_mb;
1962    unsigned num_dpb_buffer;
1963    switch (dec->base.level) {
1964    case 30:
1965       num_dpb_buffer = 8100 / fs_in_mb;
1966       break;
1967    case 31:
1968       num_dpb_buffer = 18000 / fs_in_mb;
1969       break;
1970    case 32:
1971       num_dpb_buffer = 20480 / fs_in_mb;
1972       break;
1973    case 41:
1974       num_dpb_buffer = 32768 / fs_in_mb;
1975       break;
1976    case 42:
1977       num_dpb_buffer = 34816 / fs_in_mb;
1978       break;
1979    case 50:
1980       num_dpb_buffer = 110400 / fs_in_mb;
1981       break;
1982    case 51:
1983       num_dpb_buffer = 184320 / fs_in_mb;
1984       break;
1985    default:
1986       num_dpb_buffer = 184320 / fs_in_mb;
1987       break;
1988    }
1989    num_dpb_buffer++;
1990    max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
1991    ctx_size = max_references * align(width_in_mb * height_in_mb * 192, 256);
1992 
1993    return ctx_size;
1994 }
1995 
1996 /* calculate size of reference picture buffer */
calc_dpb_size(struct radeon_decoder * dec)1997 static unsigned calc_dpb_size(struct radeon_decoder *dec)
1998 {
1999    unsigned width_in_mb, height_in_mb, image_size, dpb_size;
2000 
2001    // always align them to MB size for dpb calculation
2002    unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
2003    unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
2004 
2005    // always one more for currently decoded picture
2006    unsigned max_references = dec->base.max_references + 1;
2007 
2008    // aligned size of a single frame
2009    image_size = align(width, 32) * height;
2010    image_size += image_size / 2;
2011    image_size = align(image_size, 1024);
2012 
2013    // picture width & height in 16 pixel units
2014    width_in_mb = width / VL_MACROBLOCK_WIDTH;
2015    height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
2016 
2017    switch (u_reduce_video_profile(dec->base.profile)) {
2018    case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
2019       unsigned fs_in_mb = width_in_mb * height_in_mb;
2020       unsigned num_dpb_buffer;
2021 
2022       switch (dec->base.level) {
2023       case 30:
2024          num_dpb_buffer = 8100 / fs_in_mb;
2025          break;
2026       case 31:
2027          num_dpb_buffer = 18000 / fs_in_mb;
2028          break;
2029       case 32:
2030          num_dpb_buffer = 20480 / fs_in_mb;
2031          break;
2032       case 41:
2033          num_dpb_buffer = 32768 / fs_in_mb;
2034          break;
2035       case 42:
2036          num_dpb_buffer = 34816 / fs_in_mb;
2037          break;
2038       case 50:
2039          num_dpb_buffer = 110400 / fs_in_mb;
2040          break;
2041       case 51:
2042          num_dpb_buffer = 184320 / fs_in_mb;
2043          break;
2044       default:
2045          num_dpb_buffer = 184320 / fs_in_mb;
2046          break;
2047       }
2048       num_dpb_buffer++;
2049       max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
2050       dpb_size = image_size * max_references;
2051       break;
2052    }
2053 
2054    case PIPE_VIDEO_FORMAT_HEVC:
2055       if (dec->base.width * dec->base.height >= 4096 * 2000)
2056          max_references = MAX2(max_references, 8);
2057       else
2058          max_references = MAX2(max_references, 17);
2059 
2060       width = align(width, 16);
2061       height = align(height, 16);
2062       if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
2063          dpb_size = align((align(width, 64) * align(height, 64) * 9) / 4, 256) * max_references;
2064       else
2065          dpb_size = align((align(width, 32) * height * 3) / 2, 256) * max_references;
2066       break;
2067 
2068    case PIPE_VIDEO_FORMAT_VC1:
2069       // the firmware seems to allways assume a minimum of ref frames
2070       max_references = MAX2(NUM_VC1_REFS, max_references);
2071 
2072       // reference picture buffer
2073       dpb_size = image_size * max_references;
2074 
2075       // CONTEXT_BUFFER
2076       dpb_size += width_in_mb * height_in_mb * 128;
2077 
2078       // IT surface buffer
2079       dpb_size += width_in_mb * 64;
2080 
2081       // DB surface buffer
2082       dpb_size += width_in_mb * 128;
2083 
2084       // BP
2085       dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
2086       break;
2087 
2088    case PIPE_VIDEO_FORMAT_MPEG12:
2089       // reference picture buffer, must be big enough for all frames
2090       dpb_size = image_size * NUM_MPEG2_REFS;
2091       break;
2092 
2093    case PIPE_VIDEO_FORMAT_MPEG4:
2094       // reference picture buffer
2095       dpb_size = image_size * max_references;
2096 
2097       // CM
2098       dpb_size += width_in_mb * height_in_mb * 64;
2099 
2100       // IT surface buffer
2101       dpb_size += align(width_in_mb * height_in_mb * 32, 64);
2102 
2103       dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
2104       break;
2105 
2106    case PIPE_VIDEO_FORMAT_VP9:
2107       max_references = MAX2(max_references, 9);
2108 
2109       if (dec->dpb_type == DPB_MAX_RES)
2110          dpb_size = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR)
2111             ? (8192 * 4320 * 3 / 2) * max_references
2112             : (4096 * 3000 * 3 / 2) * max_references;
2113       else
2114          dpb_size = (align(dec->base.width, dec->db_alignment) *
2115             align(dec->base.height, dec->db_alignment) * 3 / 2) * max_references;
2116 
2117       if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
2118          dpb_size = dpb_size * 3 / 2;
2119       break;
2120 
2121    case PIPE_VIDEO_FORMAT_AV1:
2122       max_references = MAX2(max_references, 9);
2123       dpb_size = 8192 * 4320 * 3 / 2 * max_references * 3 / 2;
2124       break;
2125 
2126    case PIPE_VIDEO_FORMAT_JPEG:
2127       dpb_size = 0;
2128       break;
2129 
2130    default:
2131       // something is missing here
2132       assert(0);
2133 
2134       // at least use a sane default value
2135       dpb_size = 32 * 1024 * 1024;
2136       break;
2137    }
2138    return dpb_size;
2139 }
2140 
2141 /**
2142  * destroy this video decoder
2143  */
radeon_dec_destroy(struct pipe_video_codec * decoder)2144 static void radeon_dec_destroy(struct pipe_video_codec *decoder)
2145 {
2146    struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
2147    unsigned i;
2148 
2149    assert(decoder);
2150 
2151    map_msg_fb_it_probs_buf(dec);
2152    rvcn_dec_message_destroy(dec);
2153    send_msg_buf(dec);
2154 
2155    flush(dec, 0);
2156 
2157    dec->ws->cs_destroy(&dec->cs);
2158 
2159    for (i = 0; i < NUM_BUFFERS; ++i) {
2160       si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
2161       si_vid_destroy_buffer(&dec->bs_buffers[i]);
2162    }
2163 
2164    if (dec->dpb_type != DPB_DYNAMIC_TIER_2) {
2165       si_vid_destroy_buffer(&dec->dpb);
2166    } else {
2167       list_for_each_entry_safe(struct rvcn_dec_dynamic_dpb_t2, d, &dec->dpb_ref_list, list) {
2168          list_del(&d->list);
2169          si_vid_destroy_buffer(&d->dpb);
2170          FREE(d);
2171       }
2172    }
2173    si_vid_destroy_buffer(&dec->ctx);
2174    si_vid_destroy_buffer(&dec->sessionctx);
2175 
2176    FREE(dec);
2177 }
2178 
2179 /**
2180  * start decoding of a new frame
2181  */
radeon_dec_begin_frame(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)2182 static void radeon_dec_begin_frame(struct pipe_video_codec *decoder,
2183                                    struct pipe_video_buffer *target,
2184                                    struct pipe_picture_desc *picture)
2185 {
2186    struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
2187    uintptr_t frame;
2188 
2189    assert(decoder);
2190 
2191    frame = ++dec->frame_number;
2192    if (dec->stream_type != RDECODE_CODEC_VP9 && dec->stream_type != RDECODE_CODEC_AV1)
2193       vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
2194                                           &radeon_dec_destroy_associated_data);
2195 
2196    dec->bs_size = 0;
2197    dec->bs_ptr = dec->ws->buffer_map(dec->ws, dec->bs_buffers[dec->cur_buffer].res->buf, &dec->cs,
2198                                      PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
2199 }
2200 
2201 /**
2202  * decode a macroblock
2203  */
radeon_dec_decode_macroblock(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture,const struct pipe_macroblock * macroblocks,unsigned num_macroblocks)2204 static void radeon_dec_decode_macroblock(struct pipe_video_codec *decoder,
2205                                          struct pipe_video_buffer *target,
2206                                          struct pipe_picture_desc *picture,
2207                                          const struct pipe_macroblock *macroblocks,
2208                                          unsigned num_macroblocks)
2209 {
2210    /* not supported (yet) */
2211    assert(0);
2212 }
2213 
2214 /**
2215  * decode a bitstream
2216  */
radeon_dec_decode_bitstream(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture,unsigned num_buffers,const void * const * buffers,const unsigned * sizes)2217 static void radeon_dec_decode_bitstream(struct pipe_video_codec *decoder,
2218                                         struct pipe_video_buffer *target,
2219                                         struct pipe_picture_desc *picture, unsigned num_buffers,
2220                                         const void *const *buffers, const unsigned *sizes)
2221 {
2222    struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
2223    unsigned i;
2224 
2225    assert(decoder);
2226 
2227    if (!dec->bs_ptr)
2228       return;
2229 
2230    for (i = 0; i < num_buffers; ++i) {
2231       struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
2232       unsigned new_size = dec->bs_size + sizes[i];
2233 
2234       if (new_size > buf->res->buf->size) {
2235          dec->ws->buffer_unmap(dec->ws, buf->res->buf);
2236          dec->bs_ptr = NULL;
2237          if (!si_vid_resize_buffer(dec->screen, &dec->cs, buf, new_size)) {
2238             RVID_ERR("Can't resize bitstream buffer!");
2239             return;
2240          }
2241 
2242          dec->bs_ptr = dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs,
2243                                            PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
2244          if (!dec->bs_ptr)
2245             return;
2246 
2247          dec->bs_ptr += dec->bs_size;
2248       }
2249 
2250       memcpy(dec->bs_ptr, buffers[i], sizes[i]);
2251       dec->bs_size += sizes[i];
2252       dec->bs_ptr += sizes[i];
2253    }
2254 }
2255 
2256 /**
2257  * send cmd for vcn dec
2258  */
send_cmd_dec(struct radeon_decoder * dec,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)2259 void send_cmd_dec(struct radeon_decoder *dec, struct pipe_video_buffer *target,
2260                   struct pipe_picture_desc *picture)
2261 {
2262    struct pb_buffer *dt;
2263    struct rvid_buffer *msg_fb_it_probs_buf, *bs_buf;
2264 
2265    msg_fb_it_probs_buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
2266    bs_buf = &dec->bs_buffers[dec->cur_buffer];
2267 
2268    memset(dec->bs_ptr, 0, align(dec->bs_size, 128) - dec->bs_size);
2269    dec->ws->buffer_unmap(dec->ws, bs_buf->res->buf);
2270    dec->bs_ptr = NULL;
2271 
2272    map_msg_fb_it_probs_buf(dec);
2273    dt = rvcn_dec_message_decode(dec, target, picture);
2274    rvcn_dec_message_feedback(dec);
2275    send_msg_buf(dec);
2276 
2277    if (dec->dpb_type != DPB_DYNAMIC_TIER_2)
2278       send_cmd(dec, RDECODE_CMD_DPB_BUFFER, dec->dpb.res->buf, 0, RADEON_USAGE_READWRITE,
2279             RADEON_DOMAIN_VRAM);
2280    if (dec->ctx.res)
2281       send_cmd(dec, RDECODE_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0, RADEON_USAGE_READWRITE,
2282                RADEON_DOMAIN_VRAM);
2283    send_cmd(dec, RDECODE_CMD_BITSTREAM_BUFFER, bs_buf->res->buf, 0, RADEON_USAGE_READ,
2284             RADEON_DOMAIN_GTT);
2285    send_cmd(dec, RDECODE_CMD_DECODING_TARGET_BUFFER, dt, 0, RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
2286    send_cmd(dec, RDECODE_CMD_FEEDBACK_BUFFER, msg_fb_it_probs_buf->res->buf, FB_BUFFER_OFFSET,
2287             RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
2288    if (have_it(dec))
2289       send_cmd(dec, RDECODE_CMD_IT_SCALING_TABLE_BUFFER, msg_fb_it_probs_buf->res->buf,
2290                FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
2291    else if (have_probs(dec))
2292       send_cmd(dec, RDECODE_CMD_PROB_TBL_BUFFER, msg_fb_it_probs_buf->res->buf,
2293                FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
2294    set_reg(dec, dec->reg.cntl, 1);
2295 }
2296 
2297 /**
2298  * end decoding of the current frame
2299  */
radeon_dec_end_frame(struct pipe_video_codec * decoder,struct pipe_video_buffer * target,struct pipe_picture_desc * picture)2300 static void radeon_dec_end_frame(struct pipe_video_codec *decoder, struct pipe_video_buffer *target,
2301                                  struct pipe_picture_desc *picture)
2302 {
2303    struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
2304 
2305    assert(decoder);
2306 
2307    if (!dec->bs_ptr)
2308       return;
2309 
2310    dec->send_cmd(dec, target, picture);
2311    flush(dec, PIPE_FLUSH_ASYNC);
2312    next_buffer(dec);
2313 }
2314 
2315 /**
2316  * flush any outstanding command buffers to the hardware
2317  */
radeon_dec_flush(struct pipe_video_codec * decoder)2318 static void radeon_dec_flush(struct pipe_video_codec *decoder)
2319 {
2320 }
2321 
2322 /**
2323  * create and HW decoder
2324  */
radeon_create_decoder(struct pipe_context * context,const struct pipe_video_codec * templ)2325 struct pipe_video_codec *radeon_create_decoder(struct pipe_context *context,
2326                                                const struct pipe_video_codec *templ)
2327 {
2328    struct si_context *sctx = (struct si_context *)context;
2329    struct radeon_winsys *ws = sctx->ws;
2330    unsigned width = templ->width, height = templ->height;
2331    unsigned bs_buf_size, stream_type = 0, ring = RING_VCN_DEC;
2332    struct radeon_decoder *dec;
2333    int r, i;
2334 
2335    switch (u_reduce_video_profile(templ->profile)) {
2336    case PIPE_VIDEO_FORMAT_MPEG12:
2337       if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
2338          return vl_create_mpeg12_decoder(context, templ);
2339       stream_type = RDECODE_CODEC_MPEG2_VLD;
2340       break;
2341    case PIPE_VIDEO_FORMAT_MPEG4:
2342       width = align(width, VL_MACROBLOCK_WIDTH);
2343       height = align(height, VL_MACROBLOCK_HEIGHT);
2344       stream_type = RDECODE_CODEC_MPEG4;
2345       break;
2346    case PIPE_VIDEO_FORMAT_VC1:
2347       stream_type = RDECODE_CODEC_VC1;
2348       break;
2349    case PIPE_VIDEO_FORMAT_MPEG4_AVC:
2350       width = align(width, VL_MACROBLOCK_WIDTH);
2351       height = align(height, VL_MACROBLOCK_HEIGHT);
2352       stream_type = RDECODE_CODEC_H264_PERF;
2353       break;
2354    case PIPE_VIDEO_FORMAT_HEVC:
2355       stream_type = RDECODE_CODEC_H265;
2356       break;
2357    case PIPE_VIDEO_FORMAT_VP9:
2358       stream_type = RDECODE_CODEC_VP9;
2359       break;
2360    case PIPE_VIDEO_FORMAT_AV1:
2361       stream_type = RDECODE_CODEC_AV1;
2362       break;
2363    case PIPE_VIDEO_FORMAT_JPEG:
2364       stream_type = RDECODE_CODEC_JPEG;
2365       ring = RING_VCN_JPEG;
2366       break;
2367    default:
2368       assert(0);
2369       break;
2370    }
2371 
2372    dec = CALLOC_STRUCT(radeon_decoder);
2373 
2374    if (!dec)
2375       return NULL;
2376 
2377    dec->base = *templ;
2378    dec->base.context = context;
2379    dec->base.width = width;
2380    dec->base.height = height;
2381 
2382    dec->base.destroy = radeon_dec_destroy;
2383    dec->base.begin_frame = radeon_dec_begin_frame;
2384    dec->base.decode_macroblock = radeon_dec_decode_macroblock;
2385    dec->base.decode_bitstream = radeon_dec_decode_bitstream;
2386    dec->base.end_frame = radeon_dec_end_frame;
2387    dec->base.flush = radeon_dec_flush;
2388 
2389    dec->stream_type = stream_type;
2390    dec->stream_handle = si_vid_alloc_stream_handle();
2391    dec->screen = context->screen;
2392    dec->ws = ws;
2393 
2394    if (!ws->cs_create(&dec->cs, sctx->ctx, ring, NULL, NULL, false)) {
2395       RVID_ERR("Can't get command submission context.\n");
2396       goto error;
2397    }
2398 
2399    for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++)
2400       dec->render_pic_list[i] = NULL;
2401    bs_buf_size = width * height * (512 / (16 * 16));
2402    for (i = 0; i < NUM_BUFFERS; ++i) {
2403       unsigned msg_fb_it_probs_size = FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
2404       if (have_it(dec))
2405          msg_fb_it_probs_size += IT_SCALING_TABLE_SIZE;
2406       else if (have_probs(dec))
2407          msg_fb_it_probs_size += (dec->stream_type == RDECODE_CODEC_VP9) ?
2408                                  VP9_PROBS_TABLE_SIZE :
2409                                  sizeof(rvcn_dec_av1_segment_fg_t);
2410       /* use vram to improve performance, workaround an unknown bug */
2411       if (!si_vid_create_buffer(dec->screen, &dec->msg_fb_it_probs_buffers[i], msg_fb_it_probs_size,
2412                                 PIPE_USAGE_DEFAULT)) {
2413          RVID_ERR("Can't allocated message buffers.\n");
2414          goto error;
2415       }
2416 
2417       if (!si_vid_create_buffer(dec->screen, &dec->bs_buffers[i], bs_buf_size,
2418                                 PIPE_USAGE_STAGING)) {
2419          RVID_ERR("Can't allocated bitstream buffers.\n");
2420          goto error;
2421       }
2422 
2423       si_vid_clear_buffer(context, &dec->msg_fb_it_probs_buffers[i]);
2424       si_vid_clear_buffer(context, &dec->bs_buffers[i]);
2425 
2426       if (have_probs(dec) && dec->stream_type == RDECODE_CODEC_VP9) {
2427          struct rvid_buffer *buf;
2428          void *ptr;
2429 
2430          buf = &dec->msg_fb_it_probs_buffers[i];
2431          ptr = dec->ws->buffer_map(dec->ws, buf->res->buf, &dec->cs,
2432                                    PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
2433          ptr += FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
2434          fill_probs_table(ptr);
2435          dec->ws->buffer_unmap(dec->ws, buf->res->buf);
2436          dec->bs_ptr = NULL;
2437       }
2438    }
2439 
2440    if (sctx->family >= CHIP_SIENNA_CICHLID &&
2441        (stream_type == RDECODE_CODEC_VP9 || stream_type == RDECODE_CODEC_AV1))
2442       dec->dpb_type = DPB_DYNAMIC_TIER_2;
2443    else if (sctx->family <= CHIP_NAVI14 && stream_type == RDECODE_CODEC_VP9)
2444       dec->dpb_type = DPB_DYNAMIC_TIER_1;
2445    else
2446       dec->dpb_type = DPB_MAX_RES;
2447 
2448    dec->db_alignment = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR &&
2449                    dec->base.width > 32 && (dec->stream_type == RDECODE_CODEC_VP9 ||
2450                    dec->stream_type == RDECODE_CODEC_AV1 ||
2451                    dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)) ? 64 : 32;
2452 
2453    dec->dpb_size = calc_dpb_size(dec);
2454 
2455    if (!si_vid_create_buffer(dec->screen, &dec->sessionctx, RDECODE_SESSION_CONTEXT_SIZE,
2456                              PIPE_USAGE_DEFAULT)) {
2457       RVID_ERR("Can't allocated session ctx.\n");
2458       goto error;
2459    }
2460    si_vid_clear_buffer(context, &dec->sessionctx);
2461 
2462    switch (sctx->family) {
2463    case CHIP_RAVEN:
2464    case CHIP_RAVEN2:
2465       dec->reg.data0 = RDECODE_VCN1_GPCOM_VCPU_DATA0;
2466       dec->reg.data1 = RDECODE_VCN1_GPCOM_VCPU_DATA1;
2467       dec->reg.cmd = RDECODE_VCN1_GPCOM_VCPU_CMD;
2468       dec->reg.cntl = RDECODE_VCN1_ENGINE_CNTL;
2469       dec->jpg.direct_reg = false;
2470       break;
2471    case CHIP_NAVI10:
2472    case CHIP_NAVI12:
2473    case CHIP_NAVI14:
2474    case CHIP_RENOIR:
2475       dec->reg.data0 = RDECODE_VCN2_GPCOM_VCPU_DATA0;
2476       dec->reg.data1 = RDECODE_VCN2_GPCOM_VCPU_DATA1;
2477       dec->reg.cmd = RDECODE_VCN2_GPCOM_VCPU_CMD;
2478       dec->reg.cntl = RDECODE_VCN2_ENGINE_CNTL;
2479       dec->jpg.direct_reg = true;
2480       break;
2481    case CHIP_ARCTURUS:
2482    case CHIP_ALDEBARAN:
2483    case CHIP_SIENNA_CICHLID:
2484    case CHIP_NAVY_FLOUNDER:
2485    case CHIP_DIMGREY_CAVEFISH:
2486    case CHIP_BEIGE_GOBY:
2487    case CHIP_VANGOGH:
2488    case CHIP_YELLOW_CARP:
2489       dec->reg.data0 = RDECODE_VCN2_5_GPCOM_VCPU_DATA0;
2490       dec->reg.data1 = RDECODE_VCN2_5_GPCOM_VCPU_DATA1;
2491       dec->reg.cmd = RDECODE_VCN2_5_GPCOM_VCPU_CMD;
2492       dec->reg.cntl = RDECODE_VCN2_5_ENGINE_CNTL;
2493       dec->jpg.direct_reg = true;
2494       break;
2495    default:
2496       RVID_ERR("VCN is not supported.\n");
2497       goto error;
2498    }
2499 
2500    map_msg_fb_it_probs_buf(dec);
2501    rvcn_dec_message_create(dec);
2502    send_msg_buf(dec);
2503    r = flush(dec, 0);
2504    if (r)
2505       goto error;
2506 
2507    next_buffer(dec);
2508 
2509    if (stream_type == RDECODE_CODEC_JPEG)
2510       dec->send_cmd = send_cmd_jpeg;
2511    else
2512       dec->send_cmd = send_cmd_dec;
2513 
2514 
2515    if (dec->dpb_type == DPB_DYNAMIC_TIER_2) {
2516       list_inithead(&dec->dpb_ref_list);
2517       list_inithead(&dec->dpb_unref_list);
2518    }
2519 
2520    dec->tmz_ctx = sctx->family < CHIP_RENOIR;
2521 
2522    return &dec->base;
2523 
2524 error:
2525    dec->ws->cs_destroy(&dec->cs);
2526 
2527    for (i = 0; i < NUM_BUFFERS; ++i) {
2528       si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
2529       si_vid_destroy_buffer(&dec->bs_buffers[i]);
2530    }
2531 
2532    if (dec->dpb_type != DPB_DYNAMIC_TIER_2)
2533       si_vid_destroy_buffer(&dec->dpb);
2534    si_vid_destroy_buffer(&dec->ctx);
2535    si_vid_destroy_buffer(&dec->sessionctx);
2536 
2537    FREE(dec);
2538 
2539    return NULL;
2540 }
2541