3854fad8b4d0e3c359492e0521e7c91c1e6de152
[mesa.git] / src / gallium / drivers / radeon / radeon_vcn_dec.c
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
38 #include <assert.h>
39 #include <stdio.h>
40
41 #define FB_BUFFER_OFFSET 0x1000
42 #define FB_BUFFER_SIZE 2048
43 #define IT_SCALING_TABLE_SIZE 992
44 #define VP9_PROBS_TABLE_SIZE (RDECODE_VP9_PROBS_DATA_SIZE + 256)
45 #define RDECODE_SESSION_CONTEXT_SIZE (128 * 1024)
46
47 #define RDECODE_VCN1_GPCOM_VCPU_CMD 0x2070c
48 #define RDECODE_VCN1_GPCOM_VCPU_DATA0 0x20710
49 #define RDECODE_VCN1_GPCOM_VCPU_DATA1 0x20714
50 #define RDECODE_VCN1_ENGINE_CNTL 0x20718
51
52 #define RDECODE_VCN2_GPCOM_VCPU_CMD (0x503 << 2)
53 #define RDECODE_VCN2_GPCOM_VCPU_DATA0 (0x504 << 2)
54 #define RDECODE_VCN2_GPCOM_VCPU_DATA1 (0x505 << 2)
55 #define RDECODE_VCN2_ENGINE_CNTL (0x506 << 2)
56
57 #define RDECODE_VCN2_5_GPCOM_VCPU_CMD 0x3c
58 #define RDECODE_VCN2_5_GPCOM_VCPU_DATA0 0x40
59 #define RDECODE_VCN2_5_GPCOM_VCPU_DATA1 0x44
60 #define RDECODE_VCN2_5_ENGINE_CNTL 0x9b4
61
62 #define NUM_MPEG2_REFS 6
63 #define NUM_H264_REFS 17
64 #define NUM_VC1_REFS 5
65 #define NUM_VP9_REFS 8
66
67 static rvcn_dec_message_avc_t get_h264_msg(struct radeon_decoder *dec,
68 struct pipe_h264_picture_desc *pic)
69 {
70 rvcn_dec_message_avc_t result;
71
72 memset(&result, 0, sizeof(result));
73 switch (pic->base.profile) {
74 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
75 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
76 result.profile = RDECODE_H264_PROFILE_BASELINE;
77 break;
78
79 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
80 result.profile = RDECODE_H264_PROFILE_MAIN;
81 break;
82
83 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
84 result.profile = RDECODE_H264_PROFILE_HIGH;
85 break;
86
87 default:
88 assert(0);
89 break;
90 }
91
92 result.level = dec->base.level;
93
94 result.sps_info_flags = 0;
95 result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
96 result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
97 result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
98 result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
99 result.sps_info_flags |= 1 << RDECODE_SPS_INFO_H264_EXTENSION_SUPPORT_FLAG_SHIFT;
100
101 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
102 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
103 result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
104 result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
105 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
106
107 switch (dec->base.chroma_format) {
108 case PIPE_VIDEO_CHROMA_FORMAT_NONE:
109 break;
110 case PIPE_VIDEO_CHROMA_FORMAT_400:
111 result.chroma_format = 0;
112 break;
113 case PIPE_VIDEO_CHROMA_FORMAT_420:
114 result.chroma_format = 1;
115 break;
116 case PIPE_VIDEO_CHROMA_FORMAT_422:
117 result.chroma_format = 2;
118 break;
119 case PIPE_VIDEO_CHROMA_FORMAT_444:
120 result.chroma_format = 3;
121 break;
122 }
123
124 result.pps_info_flags = 0;
125 result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
126 result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
127 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
128 result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
129 result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
130 result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
131 result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
132 result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
133
134 result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
135 result.slice_group_map_type = pic->pps->slice_group_map_type;
136 result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
137 result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
138 result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
139 result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
140
141 memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6 * 16);
142 memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2 * 64);
143
144 memcpy(dec->it, result.scaling_list_4x4, 6 * 16);
145 memcpy((dec->it + 96), result.scaling_list_8x8, 2 * 64);
146
147 result.num_ref_frames = pic->num_ref_frames;
148
149 result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
150 result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
151
152 result.frame_num = pic->frame_num;
153 memcpy(result.frame_num_list, pic->frame_num_list, 4 * 16);
154 result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
155 result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
156 memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4 * 16 * 2);
157
158 result.decoded_pic_idx = pic->frame_num;
159
160 return result;
161 }
162
163 static void radeon_dec_destroy_associated_data(void *data)
164 {
165 /* NOOP, since we only use an intptr */
166 }
167
168 static rvcn_dec_message_hevc_t get_h265_msg(struct radeon_decoder *dec,
169 struct pipe_video_buffer *target,
170 struct pipe_h265_picture_desc *pic)
171 {
172 rvcn_dec_message_hevc_t result;
173 unsigned i, j;
174
175 memset(&result, 0, sizeof(result));
176 result.sps_info_flags = 0;
177 result.sps_info_flags |= pic->pps->sps->scaling_list_enabled_flag << 0;
178 result.sps_info_flags |= pic->pps->sps->amp_enabled_flag << 1;
179 result.sps_info_flags |= pic->pps->sps->sample_adaptive_offset_enabled_flag << 2;
180 result.sps_info_flags |= pic->pps->sps->pcm_enabled_flag << 3;
181 result.sps_info_flags |= pic->pps->sps->pcm_loop_filter_disabled_flag << 4;
182 result.sps_info_flags |= pic->pps->sps->long_term_ref_pics_present_flag << 5;
183 result.sps_info_flags |= pic->pps->sps->sps_temporal_mvp_enabled_flag << 6;
184 result.sps_info_flags |= pic->pps->sps->strong_intra_smoothing_enabled_flag << 7;
185 result.sps_info_flags |= pic->pps->sps->separate_colour_plane_flag << 8;
186 if (((struct si_screen *)dec->screen)->info.family == CHIP_CARRIZO)
187 result.sps_info_flags |= 1 << 9;
188 if (pic->UseRefPicList == true)
189 result.sps_info_flags |= 1 << 10;
190
191 result.chroma_format = pic->pps->sps->chroma_format_idc;
192 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
193 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
194 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
195 result.sps_max_dec_pic_buffering_minus1 = pic->pps->sps->sps_max_dec_pic_buffering_minus1;
196 result.log2_min_luma_coding_block_size_minus3 =
197 pic->pps->sps->log2_min_luma_coding_block_size_minus3;
198 result.log2_diff_max_min_luma_coding_block_size =
199 pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
200 result.log2_min_transform_block_size_minus2 =
201 pic->pps->sps->log2_min_transform_block_size_minus2;
202 result.log2_diff_max_min_transform_block_size =
203 pic->pps->sps->log2_diff_max_min_transform_block_size;
204 result.max_transform_hierarchy_depth_inter = pic->pps->sps->max_transform_hierarchy_depth_inter;
205 result.max_transform_hierarchy_depth_intra = pic->pps->sps->max_transform_hierarchy_depth_intra;
206 result.pcm_sample_bit_depth_luma_minus1 = pic->pps->sps->pcm_sample_bit_depth_luma_minus1;
207 result.pcm_sample_bit_depth_chroma_minus1 = pic->pps->sps->pcm_sample_bit_depth_chroma_minus1;
208 result.log2_min_pcm_luma_coding_block_size_minus3 =
209 pic->pps->sps->log2_min_pcm_luma_coding_block_size_minus3;
210 result.log2_diff_max_min_pcm_luma_coding_block_size =
211 pic->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size;
212 result.num_short_term_ref_pic_sets = pic->pps->sps->num_short_term_ref_pic_sets;
213
214 result.pps_info_flags = 0;
215 result.pps_info_flags |= pic->pps->dependent_slice_segments_enabled_flag << 0;
216 result.pps_info_flags |= pic->pps->output_flag_present_flag << 1;
217 result.pps_info_flags |= pic->pps->sign_data_hiding_enabled_flag << 2;
218 result.pps_info_flags |= pic->pps->cabac_init_present_flag << 3;
219 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 4;
220 result.pps_info_flags |= pic->pps->transform_skip_enabled_flag << 5;
221 result.pps_info_flags |= pic->pps->cu_qp_delta_enabled_flag << 6;
222 result.pps_info_flags |= pic->pps->pps_slice_chroma_qp_offsets_present_flag << 7;
223 result.pps_info_flags |= pic->pps->weighted_pred_flag << 8;
224 result.pps_info_flags |= pic->pps->weighted_bipred_flag << 9;
225 result.pps_info_flags |= pic->pps->transquant_bypass_enabled_flag << 10;
226 result.pps_info_flags |= pic->pps->tiles_enabled_flag << 11;
227 result.pps_info_flags |= pic->pps->entropy_coding_sync_enabled_flag << 12;
228 result.pps_info_flags |= pic->pps->uniform_spacing_flag << 13;
229 result.pps_info_flags |= pic->pps->loop_filter_across_tiles_enabled_flag << 14;
230 result.pps_info_flags |= pic->pps->pps_loop_filter_across_slices_enabled_flag << 15;
231 result.pps_info_flags |= pic->pps->deblocking_filter_override_enabled_flag << 16;
232 result.pps_info_flags |= pic->pps->pps_deblocking_filter_disabled_flag << 17;
233 result.pps_info_flags |= pic->pps->lists_modification_present_flag << 18;
234 result.pps_info_flags |= pic->pps->slice_segment_header_extension_present_flag << 19;
235
236 result.num_extra_slice_header_bits = pic->pps->num_extra_slice_header_bits;
237 result.num_long_term_ref_pic_sps = pic->pps->sps->num_long_term_ref_pics_sps;
238 result.num_ref_idx_l0_default_active_minus1 = pic->pps->num_ref_idx_l0_default_active_minus1;
239 result.num_ref_idx_l1_default_active_minus1 = pic->pps->num_ref_idx_l1_default_active_minus1;
240 result.pps_cb_qp_offset = pic->pps->pps_cb_qp_offset;
241 result.pps_cr_qp_offset = pic->pps->pps_cr_qp_offset;
242 result.pps_beta_offset_div2 = pic->pps->pps_beta_offset_div2;
243 result.pps_tc_offset_div2 = pic->pps->pps_tc_offset_div2;
244 result.diff_cu_qp_delta_depth = pic->pps->diff_cu_qp_delta_depth;
245 result.num_tile_columns_minus1 = pic->pps->num_tile_columns_minus1;
246 result.num_tile_rows_minus1 = pic->pps->num_tile_rows_minus1;
247 result.log2_parallel_merge_level_minus2 = pic->pps->log2_parallel_merge_level_minus2;
248 result.init_qp_minus26 = pic->pps->init_qp_minus26;
249
250 for (i = 0; i < 19; ++i)
251 result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
252
253 for (i = 0; i < 21; ++i)
254 result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
255
256 result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
257 result.curr_poc = pic->CurrPicOrderCntVal;
258
259 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
260 for (j = 0;
261 (pic->ref[j] != NULL) && (j < ARRAY_SIZE(dec->render_pic_list));
262 j++) {
263 if (dec->render_pic_list[i] == pic->ref[j])
264 break;
265 if (j == ARRAY_SIZE(dec->render_pic_list) - 1)
266 dec->render_pic_list[i] = NULL;
267 else if (pic->ref[j + 1] == NULL)
268 dec->render_pic_list[i] = NULL;
269 }
270 }
271 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
272 if (dec->render_pic_list[i] == NULL) {
273 dec->render_pic_list[i] = target;
274 result.curr_idx = i;
275 break;
276 }
277 }
278
279 vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)result.curr_idx,
280 &radeon_dec_destroy_associated_data);
281
282 for (i = 0; i < 16; ++i) {
283 struct pipe_video_buffer *ref = pic->ref[i];
284 uintptr_t ref_pic = 0;
285
286 result.poc_list[i] = pic->PicOrderCntVal[i];
287
288 if (ref)
289 ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
290 else
291 ref_pic = 0x7F;
292 result.ref_pic_list[i] = ref_pic;
293 }
294
295 for (i = 0; i < 8; ++i) {
296 result.ref_pic_set_st_curr_before[i] = 0xFF;
297 result.ref_pic_set_st_curr_after[i] = 0xFF;
298 result.ref_pic_set_lt_curr[i] = 0xFF;
299 }
300
301 for (i = 0; i < pic->NumPocStCurrBefore; ++i)
302 result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
303
304 for (i = 0; i < pic->NumPocStCurrAfter; ++i)
305 result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
306
307 for (i = 0; i < pic->NumPocLtCurr; ++i)
308 result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
309
310 for (i = 0; i < 6; ++i)
311 result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
312
313 for (i = 0; i < 2; ++i)
314 result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
315
316 memcpy(dec->it, pic->pps->sps->ScalingList4x4, 6 * 16);
317 memcpy(dec->it + 96, pic->pps->sps->ScalingList8x8, 6 * 64);
318 memcpy(dec->it + 480, pic->pps->sps->ScalingList16x16, 6 * 64);
319 memcpy(dec->it + 864, pic->pps->sps->ScalingList32x32, 2 * 64);
320
321 for (i = 0; i < 2; i++) {
322 for (j = 0; j < 15; j++)
323 result.direct_reflist[i][j] = pic->RefPicList[i][j];
324 }
325
326 if (pic->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
327 if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
328 result.p010_mode = 1;
329 result.msb_mode = 1;
330 } else {
331 result.p010_mode = 0;
332 result.luma_10to8 = 5;
333 result.chroma_10to8 = 5;
334 result.hevc_reserved[0] = 4; /* sclr_luma10to8 */
335 result.hevc_reserved[1] = 4; /* sclr_chroma10to8 */
336 }
337 }
338
339 return result;
340 }
341
342 static void fill_probs_table(void *ptr)
343 {
344 rvcn_dec_vp9_probs_t *probs = (rvcn_dec_vp9_probs_t *)ptr;
345
346 memcpy(&probs->coef_probs[0], default_coef_probs_4x4, sizeof(default_coef_probs_4x4));
347 memcpy(&probs->coef_probs[1], default_coef_probs_8x8, sizeof(default_coef_probs_8x8));
348 memcpy(&probs->coef_probs[2], default_coef_probs_16x16, sizeof(default_coef_probs_16x16));
349 memcpy(&probs->coef_probs[3], default_coef_probs_32x32, sizeof(default_coef_probs_32x32));
350 memcpy(probs->y_mode_prob, default_if_y_probs, sizeof(default_if_y_probs));
351 memcpy(probs->uv_mode_prob, default_if_uv_probs, sizeof(default_if_uv_probs));
352 memcpy(probs->single_ref_prob, default_single_ref_p, sizeof(default_single_ref_p));
353 memcpy(probs->switchable_interp_prob, default_switchable_interp_prob,
354 sizeof(default_switchable_interp_prob));
355 memcpy(probs->partition_prob, default_partition_probs, sizeof(default_partition_probs));
356 memcpy(probs->inter_mode_probs, default_inter_mode_probs, sizeof(default_inter_mode_probs));
357 memcpy(probs->mbskip_probs, default_skip_probs, sizeof(default_skip_probs));
358 memcpy(probs->intra_inter_prob, default_intra_inter_p, sizeof(default_intra_inter_p));
359 memcpy(probs->comp_inter_prob, default_comp_inter_p, sizeof(default_comp_inter_p));
360 memcpy(probs->comp_ref_prob, default_comp_ref_p, sizeof(default_comp_ref_p));
361 memcpy(probs->tx_probs_32x32, default_tx_probs_32x32, sizeof(default_tx_probs_32x32));
362 memcpy(probs->tx_probs_16x16, default_tx_probs_16x16, sizeof(default_tx_probs_16x16));
363 memcpy(probs->tx_probs_8x8, default_tx_probs_8x8, sizeof(default_tx_probs_8x8));
364 memcpy(probs->mv_joints, default_nmv_joints, sizeof(default_nmv_joints));
365 memcpy(&probs->mv_comps[0], default_nmv_components, sizeof(default_nmv_components));
366 memset(&probs->nmvc_mask, 0, sizeof(rvcn_dec_vp9_nmv_ctx_mask_t));
367 }
368
369 static rvcn_dec_message_vp9_t get_vp9_msg(struct radeon_decoder *dec,
370 struct pipe_video_buffer *target,
371 struct pipe_vp9_picture_desc *pic)
372 {
373 rvcn_dec_message_vp9_t result;
374 unsigned i;
375
376 memset(&result, 0, sizeof(result));
377
378 /* segment table */
379 rvcn_dec_vp9_probs_segment_t *prbs = (rvcn_dec_vp9_probs_segment_t *)(dec->probs);
380
381 if (pic->picture_parameter.pic_fields.segmentation_enabled) {
382 for (i = 0; i < 8; ++i) {
383 prbs->seg.feature_data[i] =
384 (pic->slice_parameter.seg_param[i].alt_quant & 0xffff) |
385 ((pic->slice_parameter.seg_param[i].alt_lf & 0xff) << 16) |
386 ((pic->slice_parameter.seg_param[i].segment_flags.segment_reference & 0xf) << 24);
387 prbs->seg.feature_mask[i] =
388 (pic->slice_parameter.seg_param[i].alt_quant_enabled << 0) |
389 (pic->slice_parameter.seg_param[i].alt_lf_enabled << 1) |
390 (pic->slice_parameter.seg_param[i].segment_flags.segment_reference_enabled << 2) |
391 (pic->slice_parameter.seg_param[i].segment_flags.segment_reference_skipped << 3);
392 }
393
394 for (i = 0; i < 7; ++i)
395 prbs->seg.tree_probs[i] = pic->picture_parameter.mb_segment_tree_probs[i];
396
397 for (i = 0; i < 3; ++i)
398 prbs->seg.pred_probs[i] = pic->picture_parameter.segment_pred_probs[i];
399
400 prbs->seg.abs_delta = 0;
401 } else
402 memset(&prbs->seg, 0, 256);
403
404 result.frame_header_flags = (pic->picture_parameter.pic_fields.frame_type
405 << RDECODE_FRAME_HDR_INFO_VP9_FRAME_TYPE_SHIFT) &
406 RDECODE_FRAME_HDR_INFO_VP9_FRAME_TYPE_MASK;
407
408 result.frame_header_flags |= (pic->picture_parameter.pic_fields.error_resilient_mode
409 << RDECODE_FRAME_HDR_INFO_VP9_ERROR_RESILIENT_MODE_SHIFT) &
410 RDECODE_FRAME_HDR_INFO_VP9_ERROR_RESILIENT_MODE_MASK;
411
412 result.frame_header_flags |= (pic->picture_parameter.pic_fields.intra_only
413 << RDECODE_FRAME_HDR_INFO_VP9_INTRA_ONLY_SHIFT) &
414 RDECODE_FRAME_HDR_INFO_VP9_INTRA_ONLY_MASK;
415
416 result.frame_header_flags |= (pic->picture_parameter.pic_fields.allow_high_precision_mv
417 << RDECODE_FRAME_HDR_INFO_VP9_ALLOW_HIGH_PRECISION_MV_SHIFT) &
418 RDECODE_FRAME_HDR_INFO_VP9_ALLOW_HIGH_PRECISION_MV_MASK;
419
420 result.frame_header_flags |= (pic->picture_parameter.pic_fields.frame_parallel_decoding_mode
421 << RDECODE_FRAME_HDR_INFO_VP9_FRAME_PARALLEL_DECODING_MODE_SHIFT) &
422 RDECODE_FRAME_HDR_INFO_VP9_FRAME_PARALLEL_DECODING_MODE_MASK;
423
424 result.frame_header_flags |= (pic->picture_parameter.pic_fields.refresh_frame_context
425 << RDECODE_FRAME_HDR_INFO_VP9_REFRESH_FRAME_CONTEXT_SHIFT) &
426 RDECODE_FRAME_HDR_INFO_VP9_REFRESH_FRAME_CONTEXT_MASK;
427
428 result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_enabled
429 << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_ENABLED_SHIFT) &
430 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_ENABLED_MASK;
431
432 result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_update_map
433 << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_UPDATE_MAP_SHIFT) &
434 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_UPDATE_MAP_MASK;
435
436 result.frame_header_flags |= (pic->picture_parameter.pic_fields.segmentation_temporal_update
437 << RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_TEMPORAL_UPDATE_SHIFT) &
438 RDECODE_FRAME_HDR_INFO_VP9_SEGMENTATION_TEMPORAL_UPDATE_MASK;
439
440 result.frame_header_flags |= (pic->picture_parameter.mode_ref_delta_enabled
441 << RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_ENABLED_SHIFT) &
442 RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_ENABLED_MASK;
443
444 result.frame_header_flags |= (pic->picture_parameter.mode_ref_delta_update
445 << RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_UPDATE_SHIFT) &
446 RDECODE_FRAME_HDR_INFO_VP9_MODE_REF_DELTA_UPDATE_MASK;
447
448 result.frame_header_flags |=
449 ((dec->show_frame && !pic->picture_parameter.pic_fields.error_resilient_mode)
450 << RDECODE_FRAME_HDR_INFO_VP9_USE_PREV_IN_FIND_MV_REFS_SHIFT) &
451 RDECODE_FRAME_HDR_INFO_VP9_USE_PREV_IN_FIND_MV_REFS_MASK;
452 dec->show_frame = pic->picture_parameter.pic_fields.show_frame;
453
454 result.interp_filter = pic->picture_parameter.pic_fields.mcomp_filter_type;
455
456 result.frame_context_idx = pic->picture_parameter.pic_fields.frame_context_idx;
457 result.reset_frame_context = pic->picture_parameter.pic_fields.reset_frame_context;
458
459 result.filter_level = pic->picture_parameter.filter_level;
460 result.sharpness_level = pic->picture_parameter.sharpness_level;
461
462 for (i = 0; i < 8; ++i)
463 memcpy(result.lf_adj_level[i], pic->slice_parameter.seg_param[i].filter_level, 4 * 2);
464
465 if (pic->picture_parameter.pic_fields.lossless_flag) {
466 result.base_qindex = 0;
467 result.y_dc_delta_q = 0;
468 result.uv_ac_delta_q = 0;
469 result.uv_dc_delta_q = 0;
470 } else {
471 result.base_qindex = pic->picture_parameter.base_qindex;
472 result.y_dc_delta_q = pic->picture_parameter.y_dc_delta_q;
473 result.uv_ac_delta_q = pic->picture_parameter.uv_ac_delta_q;
474 result.uv_dc_delta_q = pic->picture_parameter.uv_dc_delta_q;
475 }
476
477 result.log2_tile_cols = pic->picture_parameter.log2_tile_columns;
478 result.log2_tile_rows = pic->picture_parameter.log2_tile_rows;
479 result.chroma_format = 1;
480 result.bit_depth_luma_minus8 = result.bit_depth_chroma_minus8 =
481 (pic->picture_parameter.bit_depth - 8);
482
483 result.vp9_frame_size = align(dec->bs_size, 128);
484 result.uncompressed_header_size = pic->picture_parameter.frame_header_length_in_bytes;
485 result.compressed_header_size = pic->picture_parameter.first_partition_size;
486
487 assert(dec->base.max_references + 1 <= ARRAY_SIZE(dec->render_pic_list));
488
489 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); ++i) {
490 if (dec->render_pic_list[i] && dec->render_pic_list[i] == target) {
491 result.curr_pic_idx = (uintptr_t)vl_video_buffer_get_associated_data(target, &dec->base);
492 break;
493 } else if (!dec->render_pic_list[i]) {
494 dec->render_pic_list[i] = target;
495 result.curr_pic_idx = dec->ref_idx;
496 vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)dec->ref_idx++,
497 &radeon_dec_destroy_associated_data);
498 break;
499 }
500 }
501
502 for (i = 0; i < 8; i++) {
503 result.ref_frame_map[i] =
504 (pic->ref[i]) ? (uintptr_t)vl_video_buffer_get_associated_data(pic->ref[i], &dec->base)
505 : 0x7f;
506 }
507
508 result.frame_refs[0] = result.ref_frame_map[pic->picture_parameter.pic_fields.last_ref_frame];
509 result.ref_frame_sign_bias[0] = pic->picture_parameter.pic_fields.last_ref_frame_sign_bias;
510 result.frame_refs[1] = result.ref_frame_map[pic->picture_parameter.pic_fields.golden_ref_frame];
511 result.ref_frame_sign_bias[1] = pic->picture_parameter.pic_fields.golden_ref_frame_sign_bias;
512 result.frame_refs[2] = result.ref_frame_map[pic->picture_parameter.pic_fields.alt_ref_frame];
513 result.ref_frame_sign_bias[2] = pic->picture_parameter.pic_fields.alt_ref_frame_sign_bias;
514
515 if (pic->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2) {
516 if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
517 result.p010_mode = 1;
518 result.msb_mode = 1;
519 } else {
520 result.p010_mode = 0;
521 result.luma_10to8 = 1;
522 result.chroma_10to8 = 1;
523 }
524 }
525
526 return result;
527 }
528
529 static unsigned calc_ctx_size_h265_main(struct radeon_decoder *dec)
530 {
531 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
532 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
533
534 unsigned max_references = dec->base.max_references + 1;
535
536 if (dec->base.width * dec->base.height >= 4096 * 2000)
537 max_references = MAX2(max_references, 8);
538 else
539 max_references = MAX2(max_references, 17);
540
541 width = align(width, 16);
542 height = align(height, 16);
543 return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
544 }
545
546 static unsigned calc_ctx_size_h265_main10(struct radeon_decoder *dec,
547 struct pipe_h265_picture_desc *pic)
548 {
549 unsigned log2_ctb_size, width_in_ctb, height_in_ctb, num_16x16_block_per_ctb;
550 unsigned context_buffer_size_per_ctb_row, cm_buffer_size, max_mb_address, db_left_tile_pxl_size;
551 unsigned db_left_tile_ctx_size = 4096 / 16 * (32 + 16 * 4);
552
553 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
554 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
555 unsigned coeff_10bit =
556 (pic->pps->sps->bit_depth_luma_minus8 || pic->pps->sps->bit_depth_chroma_minus8) ? 2 : 1;
557
558 unsigned max_references = dec->base.max_references + 1;
559
560 if (dec->base.width * dec->base.height >= 4096 * 2000)
561 max_references = MAX2(max_references, 8);
562 else
563 max_references = MAX2(max_references, 17);
564
565 log2_ctb_size = pic->pps->sps->log2_min_luma_coding_block_size_minus3 + 3 +
566 pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
567
568 width_in_ctb = (width + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
569 height_in_ctb = (height + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
570
571 num_16x16_block_per_ctb = ((1 << log2_ctb_size) >> 4) * ((1 << log2_ctb_size) >> 4);
572 context_buffer_size_per_ctb_row = align(width_in_ctb * num_16x16_block_per_ctb * 16, 256);
573 max_mb_address = (unsigned)ceil(height * 8 / 2048.0);
574
575 cm_buffer_size = max_references * context_buffer_size_per_ctb_row * height_in_ctb;
576 db_left_tile_pxl_size = coeff_10bit * (max_mb_address * 2 * 2048 + 1024);
577
578 return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
579 }
580
581 static rvcn_dec_message_vc1_t get_vc1_msg(struct pipe_vc1_picture_desc *pic)
582 {
583 rvcn_dec_message_vc1_t result;
584
585 memset(&result, 0, sizeof(result));
586 switch (pic->base.profile) {
587 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
588 result.profile = RDECODE_VC1_PROFILE_SIMPLE;
589 result.level = 1;
590 break;
591
592 case PIPE_VIDEO_PROFILE_VC1_MAIN:
593 result.profile = RDECODE_VC1_PROFILE_MAIN;
594 result.level = 2;
595 break;
596
597 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
598 result.profile = RDECODE_VC1_PROFILE_ADVANCED;
599 result.level = 4;
600 break;
601
602 default:
603 assert(0);
604 }
605
606 result.sps_info_flags |= pic->postprocflag << 7;
607 result.sps_info_flags |= pic->pulldown << 6;
608 result.sps_info_flags |= pic->interlace << 5;
609 result.sps_info_flags |= pic->tfcntrflag << 4;
610 result.sps_info_flags |= pic->finterpflag << 3;
611 result.sps_info_flags |= pic->psf << 1;
612
613 result.pps_info_flags |= pic->range_mapy_flag << 31;
614 result.pps_info_flags |= pic->range_mapy << 28;
615 result.pps_info_flags |= pic->range_mapuv_flag << 27;
616 result.pps_info_flags |= pic->range_mapuv << 24;
617 result.pps_info_flags |= pic->multires << 21;
618 result.pps_info_flags |= pic->maxbframes << 16;
619 result.pps_info_flags |= pic->overlap << 11;
620 result.pps_info_flags |= pic->quantizer << 9;
621 result.pps_info_flags |= pic->panscan_flag << 7;
622 result.pps_info_flags |= pic->refdist_flag << 6;
623 result.pps_info_flags |= pic->vstransform << 0;
624
625 if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
626 result.pps_info_flags |= pic->syncmarker << 20;
627 result.pps_info_flags |= pic->rangered << 19;
628 result.pps_info_flags |= pic->loopfilter << 5;
629 result.pps_info_flags |= pic->fastuvmc << 4;
630 result.pps_info_flags |= pic->extended_mv << 3;
631 result.pps_info_flags |= pic->extended_dmv << 8;
632 result.pps_info_flags |= pic->dquant << 1;
633 }
634
635 result.chroma_format = 1;
636
637 return result;
638 }
639
640 static uint32_t get_ref_pic_idx(struct radeon_decoder *dec, struct pipe_video_buffer *ref)
641 {
642 uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
643 uint32_t max = MAX2(dec->frame_number, 1) - 1;
644 uintptr_t frame;
645
646 /* seems to be the most sane fallback */
647 if (!ref)
648 return max;
649
650 /* get the frame number from the associated data */
651 frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
652
653 /* limit the frame number to a valid range */
654 return MAX2(MIN2(frame, max), min);
655 }
656
657 static rvcn_dec_message_mpeg2_vld_t get_mpeg2_msg(struct radeon_decoder *dec,
658 struct pipe_mpeg12_picture_desc *pic)
659 {
660 const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
661 rvcn_dec_message_mpeg2_vld_t result;
662 unsigned i;
663
664 memset(&result, 0, sizeof(result));
665 result.decoded_pic_idx = dec->frame_number;
666
667 result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
668 result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
669
670 if (pic->intra_matrix) {
671 result.load_intra_quantiser_matrix = 1;
672 for (i = 0; i < 64; ++i) {
673 result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
674 }
675 }
676 if (pic->non_intra_matrix) {
677 result.load_nonintra_quantiser_matrix = 1;
678 for (i = 0; i < 64; ++i) {
679 result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
680 }
681 }
682
683 result.profile_and_level_indication = 0;
684 result.chroma_format = 0x1;
685
686 result.picture_coding_type = pic->picture_coding_type;
687 result.f_code[0][0] = pic->f_code[0][0] + 1;
688 result.f_code[0][1] = pic->f_code[0][1] + 1;
689 result.f_code[1][0] = pic->f_code[1][0] + 1;
690 result.f_code[1][1] = pic->f_code[1][1] + 1;
691 result.intra_dc_precision = pic->intra_dc_precision;
692 result.pic_structure = pic->picture_structure;
693 result.top_field_first = pic->top_field_first;
694 result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
695 result.concealment_motion_vectors = pic->concealment_motion_vectors;
696 result.q_scale_type = pic->q_scale_type;
697 result.intra_vlc_format = pic->intra_vlc_format;
698 result.alternate_scan = pic->alternate_scan;
699
700 return result;
701 }
702
703 static rvcn_dec_message_mpeg4_asp_vld_t get_mpeg4_msg(struct radeon_decoder *dec,
704 struct pipe_mpeg4_picture_desc *pic)
705 {
706 rvcn_dec_message_mpeg4_asp_vld_t result;
707 unsigned i;
708
709 memset(&result, 0, sizeof(result));
710 result.decoded_pic_idx = dec->frame_number;
711
712 result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
713 result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
714
715 result.variant_type = 0;
716 result.profile_and_level_indication = 0xF0;
717
718 result.video_object_layer_verid = 0x5;
719 result.video_object_layer_shape = 0x0;
720
721 result.video_object_layer_width = dec->base.width;
722 result.video_object_layer_height = dec->base.height;
723
724 result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
725
726 result.short_video_header = pic->short_video_header;
727 result.interlaced = pic->interlaced;
728 result.load_intra_quant_mat = 1;
729 result.load_nonintra_quant_mat = 1;
730 result.quarter_sample = pic->quarter_sample;
731 result.complexity_estimation_disable = 1;
732 result.resync_marker_disable = pic->resync_marker_disable;
733 result.newpred_enable = 0;
734 result.reduced_resolution_vop_enable = 0;
735
736 result.quant_type = pic->quant_type;
737
738 for (i = 0; i < 64; ++i) {
739 result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
740 result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
741 }
742
743 return result;
744 }
745
746 static void rvcn_dec_message_create(struct radeon_decoder *dec)
747 {
748 rvcn_dec_message_header_t *header = dec->msg;
749 rvcn_dec_message_create_t *create = dec->msg + sizeof(rvcn_dec_message_header_t);
750 unsigned sizes = sizeof(rvcn_dec_message_header_t) + sizeof(rvcn_dec_message_create_t);
751
752 memset(dec->msg, 0, sizes);
753 header->header_size = sizeof(rvcn_dec_message_header_t);
754 header->total_size = sizes;
755 header->num_buffers = 1;
756 header->msg_type = RDECODE_MSG_CREATE;
757 header->stream_handle = dec->stream_handle;
758 header->status_report_feedback_number = 0;
759
760 header->index[0].message_id = RDECODE_MESSAGE_CREATE;
761 header->index[0].offset = sizeof(rvcn_dec_message_header_t);
762 header->index[0].size = sizeof(rvcn_dec_message_create_t);
763 header->index[0].filled = 0;
764
765 create->stream_type = dec->stream_type;
766 create->session_flags = 0;
767 create->width_in_samples = dec->base.width;
768 create->height_in_samples = dec->base.height;
769 }
770
771 static struct pb_buffer *rvcn_dec_message_decode(struct radeon_decoder *dec,
772 struct pipe_video_buffer *target,
773 struct pipe_picture_desc *picture)
774 {
775 struct si_texture *luma = (struct si_texture *)((struct vl_video_buffer *)target)->resources[0];
776 struct si_texture *chroma =
777 (struct si_texture *)((struct vl_video_buffer *)target)->resources[1];
778 rvcn_dec_message_header_t *header;
779 rvcn_dec_message_index_t *index;
780 rvcn_dec_message_decode_t *decode;
781 unsigned sizes = 0, offset_decode, offset_codec;
782 void *codec;
783
784 header = dec->msg;
785 sizes += sizeof(rvcn_dec_message_header_t);
786 index = (void *)header + sizeof(rvcn_dec_message_header_t);
787 sizes += sizeof(rvcn_dec_message_index_t);
788 offset_decode = sizes;
789 decode = (void *)index + sizeof(rvcn_dec_message_index_t);
790 sizes += sizeof(rvcn_dec_message_decode_t);
791 offset_codec = sizes;
792 codec = (void *)decode + sizeof(rvcn_dec_message_decode_t);
793
794 memset(dec->msg, 0, sizes);
795 header->header_size = sizeof(rvcn_dec_message_header_t);
796 header->total_size = sizes;
797 header->num_buffers = 2;
798 header->msg_type = RDECODE_MSG_DECODE;
799 header->stream_handle = dec->stream_handle;
800 header->status_report_feedback_number = dec->frame_number;
801
802 header->index[0].message_id = RDECODE_MESSAGE_DECODE;
803 header->index[0].offset = offset_decode;
804 header->index[0].size = sizeof(rvcn_dec_message_decode_t);
805 header->index[0].filled = 0;
806
807 index->offset = offset_codec;
808 index->size = sizeof(rvcn_dec_message_avc_t);
809 index->filled = 0;
810
811 decode->stream_type = dec->stream_type;
812 decode->decode_flags = 0;
813 decode->width_in_samples = dec->base.width;
814 decode->height_in_samples = dec->base.height;
815
816 decode->bsd_size = align(dec->bs_size, 128);
817 decode->dpb_size = dec->dpb.res->buf->size;
818 decode->dt_size = si_resource(((struct vl_video_buffer *)target)->resources[0])->buf->size +
819 si_resource(((struct vl_video_buffer *)target)->resources[1])->buf->size;
820
821 decode->sct_size = 0;
822 decode->sc_coeff_size = 0;
823
824 decode->sw_ctxt_size = RDECODE_SESSION_CONTEXT_SIZE;
825 decode->db_pitch = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR &&
826 dec->base.width > 32 && dec->stream_type == RDECODE_CODEC_VP9)
827 ? align(dec->base.width, 64)
828 : align(dec->base.width, 32);
829 if (((struct si_screen*)dec->screen)->info.family >= CHIP_SIENNA &&
830 dec->stream_type == RDECODE_CODEC_VP9)
831 decode->db_aligned_height = align(dec->base.height, 64);
832
833 decode->db_surf_tile_config = 0;
834
835 decode->dt_pitch = luma->surface.u.gfx9.surf_pitch * luma->surface.blk_w;
836 decode->dt_uv_pitch = decode->dt_pitch / 2;
837
838 decode->dt_tiling_mode = 0;
839 decode->dt_swizzle_mode = RDECODE_SW_MODE_LINEAR;
840 decode->dt_array_mode = RDECODE_ARRAY_MODE_LINEAR;
841 decode->dt_field_mode = ((struct vl_video_buffer *)target)->base.interlaced;
842 decode->dt_surf_tile_config = 0;
843 decode->dt_uv_surf_tile_config = 0;
844
845 decode->dt_luma_top_offset = luma->surface.u.gfx9.surf_offset;
846 decode->dt_chroma_top_offset = chroma->surface.u.gfx9.surf_offset;
847 if (decode->dt_field_mode) {
848 decode->dt_luma_bottom_offset =
849 luma->surface.u.gfx9.surf_offset + luma->surface.u.gfx9.surf_slice_size;
850 decode->dt_chroma_bottom_offset =
851 chroma->surface.u.gfx9.surf_offset + chroma->surface.u.gfx9.surf_slice_size;
852 } else {
853 decode->dt_luma_bottom_offset = decode->dt_luma_top_offset;
854 decode->dt_chroma_bottom_offset = decode->dt_chroma_top_offset;
855 }
856
857 switch (u_reduce_video_profile(picture->profile)) {
858 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
859 rvcn_dec_message_avc_t avc = get_h264_msg(dec, (struct pipe_h264_picture_desc *)picture);
860 memcpy(codec, (void *)&avc, sizeof(rvcn_dec_message_avc_t));
861 index->message_id = RDECODE_MESSAGE_AVC;
862 break;
863 }
864 case PIPE_VIDEO_FORMAT_HEVC: {
865 rvcn_dec_message_hevc_t hevc =
866 get_h265_msg(dec, target, (struct pipe_h265_picture_desc *)picture);
867
868 memcpy(codec, (void *)&hevc, sizeof(rvcn_dec_message_hevc_t));
869 index->message_id = RDECODE_MESSAGE_HEVC;
870 if (dec->ctx.res == NULL) {
871 unsigned ctx_size;
872 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
873 ctx_size = calc_ctx_size_h265_main10(dec, (struct pipe_h265_picture_desc *)picture);
874 else
875 ctx_size = calc_ctx_size_h265_main(dec);
876 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT))
877 RVID_ERR("Can't allocated context buffer.\n");
878 si_vid_clear_buffer(dec->base.context, &dec->ctx);
879 }
880 break;
881 }
882 case PIPE_VIDEO_FORMAT_VC1: {
883 rvcn_dec_message_vc1_t vc1 = get_vc1_msg((struct pipe_vc1_picture_desc *)picture);
884
885 memcpy(codec, (void *)&vc1, sizeof(rvcn_dec_message_vc1_t));
886 if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
887 (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
888 decode->width_in_samples = align(decode->width_in_samples, 16) / 16;
889 decode->height_in_samples = align(decode->height_in_samples, 16) / 16;
890 }
891 index->message_id = RDECODE_MESSAGE_VC1;
892 break;
893 }
894 case PIPE_VIDEO_FORMAT_MPEG12: {
895 rvcn_dec_message_mpeg2_vld_t mpeg2 =
896 get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc *)picture);
897
898 memcpy(codec, (void *)&mpeg2, sizeof(rvcn_dec_message_mpeg2_vld_t));
899 index->message_id = RDECODE_MESSAGE_MPEG2_VLD;
900 break;
901 }
902 case PIPE_VIDEO_FORMAT_MPEG4: {
903 rvcn_dec_message_mpeg4_asp_vld_t mpeg4 =
904 get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc *)picture);
905
906 memcpy(codec, (void *)&mpeg4, sizeof(rvcn_dec_message_mpeg4_asp_vld_t));
907 index->message_id = RDECODE_MESSAGE_MPEG4_ASP_VLD;
908 break;
909 }
910 case PIPE_VIDEO_FORMAT_VP9: {
911 rvcn_dec_message_vp9_t vp9 =
912 get_vp9_msg(dec, target, (struct pipe_vp9_picture_desc *)picture);
913
914 memcpy(codec, (void *)&vp9, sizeof(rvcn_dec_message_vp9_t));
915 index->message_id = RDECODE_MESSAGE_VP9;
916
917 if (dec->ctx.res == NULL) {
918 unsigned ctx_size;
919 uint8_t *ptr;
920
921 /* default probability + probability data */
922 ctx_size = 2304 * 5;
923
924 if (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR) {
925 /* SRE collocated context data */
926 ctx_size += 32 * 2 * 128 * 68;
927 /* SMP collocated context data */
928 ctx_size += 9 * 64 * 2 * 128 * 68;
929 /* SDB left tile pixel */
930 ctx_size += 8 * 2 * 2 * 8192;
931 } else {
932 ctx_size += 32 * 2 * 64 * 64;
933 ctx_size += 9 * 64 * 2 * 64 * 64;
934 ctx_size += 8 * 2 * 4096;
935 }
936
937 if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
938 ctx_size += 8 * 2 * 4096;
939
940 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT))
941 RVID_ERR("Can't allocated context buffer.\n");
942 si_vid_clear_buffer(dec->base.context, &dec->ctx);
943
944 /* ctx needs probs table */
945 ptr = dec->ws->buffer_map(dec->ctx.res->buf, dec->cs,
946 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
947 fill_probs_table(ptr);
948 dec->ws->buffer_unmap(dec->ctx.res->buf);
949 }
950 break;
951 }
952 default:
953 assert(0);
954 return NULL;
955 }
956
957 if (dec->ctx.res)
958 decode->hw_ctxt_size = dec->ctx.res->buf->size;
959
960 return luma->buffer.buf;
961 }
962
963 static void rvcn_dec_message_destroy(struct radeon_decoder *dec)
964 {
965 rvcn_dec_message_header_t *header = dec->msg;
966
967 memset(dec->msg, 0, sizeof(rvcn_dec_message_header_t));
968 header->header_size = sizeof(rvcn_dec_message_header_t);
969 header->total_size = sizeof(rvcn_dec_message_header_t) - sizeof(rvcn_dec_message_index_t);
970 header->num_buffers = 0;
971 header->msg_type = RDECODE_MSG_DESTROY;
972 header->stream_handle = dec->stream_handle;
973 header->status_report_feedback_number = 0;
974 }
975
976 static void rvcn_dec_message_feedback(struct radeon_decoder *dec)
977 {
978 rvcn_dec_feedback_header_t *header = (void *)dec->fb;
979
980 header->header_size = sizeof(rvcn_dec_feedback_header_t);
981 header->total_size = sizeof(rvcn_dec_feedback_header_t);
982 header->num_buffers = 0;
983 }
984
985 /* flush IB to the hardware */
986 static int flush(struct radeon_decoder *dec, unsigned flags)
987 {
988 return dec->ws->cs_flush(dec->cs, flags, NULL);
989 }
990
991 /* add a new set register command to the IB */
992 static void set_reg(struct radeon_decoder *dec, unsigned reg, uint32_t val)
993 {
994 radeon_emit(dec->cs, RDECODE_PKT0(reg >> 2, 0));
995 radeon_emit(dec->cs, val);
996 }
997
998 /* send a command to the VCPU through the GPCOM registers */
999 static void send_cmd(struct radeon_decoder *dec, unsigned cmd, struct pb_buffer *buf, uint32_t off,
1000 enum radeon_bo_usage usage, enum radeon_bo_domain domain)
1001 {
1002 uint64_t addr;
1003
1004 dec->ws->cs_add_buffer(dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain, 0);
1005 addr = dec->ws->buffer_get_virtual_address(buf);
1006 addr = addr + off;
1007
1008 set_reg(dec, dec->reg.data0, addr);
1009 set_reg(dec, dec->reg.data1, addr >> 32);
1010 set_reg(dec, dec->reg.cmd, cmd << 1);
1011 }
1012
1013 /* do the codec needs an IT buffer ?*/
1014 static bool have_it(struct radeon_decoder *dec)
1015 {
1016 return dec->stream_type == RDECODE_CODEC_H264_PERF || dec->stream_type == RDECODE_CODEC_H265;
1017 }
1018
1019 /* do the codec needs an probs buffer? */
1020 static bool have_probs(struct radeon_decoder *dec)
1021 {
1022 return dec->stream_type == RDECODE_CODEC_VP9;
1023 }
1024
1025 /* map the next available message/feedback/itscaling buffer */
1026 static void map_msg_fb_it_probs_buf(struct radeon_decoder *dec)
1027 {
1028 struct rvid_buffer *buf;
1029 uint8_t *ptr;
1030
1031 /* grab the current message/feedback buffer */
1032 buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1033
1034 /* and map it for CPU access */
1035 ptr =
1036 dec->ws->buffer_map(buf->res->buf, dec->cs, PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1037
1038 /* calc buffer offsets */
1039 dec->msg = ptr;
1040
1041 dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
1042 if (have_it(dec))
1043 dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1044 else if (have_probs(dec))
1045 dec->probs = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1046 }
1047
1048 /* unmap and send a message command to the VCPU */
1049 static void send_msg_buf(struct radeon_decoder *dec)
1050 {
1051 struct rvid_buffer *buf;
1052
1053 /* ignore the request if message/feedback buffer isn't mapped */
1054 if (!dec->msg || !dec->fb)
1055 return;
1056
1057 /* grab the current message buffer */
1058 buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1059
1060 /* unmap the buffer */
1061 dec->ws->buffer_unmap(buf->res->buf);
1062 dec->msg = NULL;
1063 dec->fb = NULL;
1064 dec->it = NULL;
1065 dec->probs = NULL;
1066
1067 if (dec->sessionctx.res)
1068 send_cmd(dec, RDECODE_CMD_SESSION_CONTEXT_BUFFER, dec->sessionctx.res->buf, 0,
1069 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1070
1071 /* and send it to the hardware */
1072 send_cmd(dec, RDECODE_CMD_MSG_BUFFER, buf->res->buf, 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1073 }
1074
1075 /* cycle to the next set of buffers */
1076 static void next_buffer(struct radeon_decoder *dec)
1077 {
1078 ++dec->cur_buffer;
1079 dec->cur_buffer %= NUM_BUFFERS;
1080 }
1081
1082 static unsigned calc_ctx_size_h264_perf(struct radeon_decoder *dec)
1083 {
1084 unsigned width_in_mb, height_in_mb, ctx_size;
1085 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1086 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1087
1088 unsigned max_references = dec->base.max_references + 1;
1089
1090 // picture width & height in 16 pixel units
1091 width_in_mb = width / VL_MACROBLOCK_WIDTH;
1092 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
1093
1094 unsigned fs_in_mb = width_in_mb * height_in_mb;
1095 unsigned num_dpb_buffer;
1096 switch (dec->base.level) {
1097 case 30:
1098 num_dpb_buffer = 8100 / fs_in_mb;
1099 break;
1100 case 31:
1101 num_dpb_buffer = 18000 / fs_in_mb;
1102 break;
1103 case 32:
1104 num_dpb_buffer = 20480 / fs_in_mb;
1105 break;
1106 case 41:
1107 num_dpb_buffer = 32768 / fs_in_mb;
1108 break;
1109 case 42:
1110 num_dpb_buffer = 34816 / fs_in_mb;
1111 break;
1112 case 50:
1113 num_dpb_buffer = 110400 / fs_in_mb;
1114 break;
1115 case 51:
1116 num_dpb_buffer = 184320 / fs_in_mb;
1117 break;
1118 default:
1119 num_dpb_buffer = 184320 / fs_in_mb;
1120 break;
1121 }
1122 num_dpb_buffer++;
1123 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
1124 ctx_size = max_references * align(width_in_mb * height_in_mb * 192, 256);
1125
1126 return ctx_size;
1127 }
1128
1129 /* calculate size of reference picture buffer */
1130 static unsigned calc_dpb_size(struct radeon_decoder *dec)
1131 {
1132 unsigned width_in_mb, height_in_mb, image_size, dpb_size;
1133
1134 // always align them to MB size for dpb calculation
1135 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1136 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1137
1138 // always one more for currently decoded picture
1139 unsigned max_references = dec->base.max_references + 1;
1140
1141 // aligned size of a single frame
1142 image_size = align(width, 32) * height;
1143 image_size += image_size / 2;
1144 image_size = align(image_size, 1024);
1145
1146 // picture width & height in 16 pixel units
1147 width_in_mb = width / VL_MACROBLOCK_WIDTH;
1148 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
1149
1150 switch (u_reduce_video_profile(dec->base.profile)) {
1151 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
1152 unsigned fs_in_mb = width_in_mb * height_in_mb;
1153 unsigned num_dpb_buffer;
1154
1155 switch (dec->base.level) {
1156 case 30:
1157 num_dpb_buffer = 8100 / fs_in_mb;
1158 break;
1159 case 31:
1160 num_dpb_buffer = 18000 / fs_in_mb;
1161 break;
1162 case 32:
1163 num_dpb_buffer = 20480 / fs_in_mb;
1164 break;
1165 case 41:
1166 num_dpb_buffer = 32768 / fs_in_mb;
1167 break;
1168 case 42:
1169 num_dpb_buffer = 34816 / fs_in_mb;
1170 break;
1171 case 50:
1172 num_dpb_buffer = 110400 / fs_in_mb;
1173 break;
1174 case 51:
1175 num_dpb_buffer = 184320 / fs_in_mb;
1176 break;
1177 default:
1178 num_dpb_buffer = 184320 / fs_in_mb;
1179 break;
1180 }
1181 num_dpb_buffer++;
1182 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
1183 dpb_size = image_size * max_references;
1184 break;
1185 }
1186
1187 case PIPE_VIDEO_FORMAT_HEVC:
1188 if (dec->base.width * dec->base.height >= 4096 * 2000)
1189 max_references = MAX2(max_references, 8);
1190 else
1191 max_references = MAX2(max_references, 17);
1192
1193 width = align(width, 16);
1194 height = align(height, 16);
1195 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
1196 dpb_size = align((align(width, 32) * height * 9) / 4, 256) * max_references;
1197 else
1198 dpb_size = align((align(width, 32) * height * 3) / 2, 256) * max_references;
1199 break;
1200
1201 case PIPE_VIDEO_FORMAT_VC1:
1202 // the firmware seems to allways assume a minimum of ref frames
1203 max_references = MAX2(NUM_VC1_REFS, max_references);
1204
1205 // reference picture buffer
1206 dpb_size = image_size * max_references;
1207
1208 // CONTEXT_BUFFER
1209 dpb_size += width_in_mb * height_in_mb * 128;
1210
1211 // IT surface buffer
1212 dpb_size += width_in_mb * 64;
1213
1214 // DB surface buffer
1215 dpb_size += width_in_mb * 128;
1216
1217 // BP
1218 dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
1219 break;
1220
1221 case PIPE_VIDEO_FORMAT_MPEG12:
1222 // reference picture buffer, must be big enough for all frames
1223 dpb_size = image_size * NUM_MPEG2_REFS;
1224 break;
1225
1226 case PIPE_VIDEO_FORMAT_MPEG4:
1227 // reference picture buffer
1228 dpb_size = image_size * max_references;
1229
1230 // CM
1231 dpb_size += width_in_mb * height_in_mb * 64;
1232
1233 // IT surface buffer
1234 dpb_size += align(width_in_mb * height_in_mb * 32, 64);
1235
1236 dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
1237 break;
1238
1239 case PIPE_VIDEO_FORMAT_VP9:
1240 max_references = MAX2(max_references, 9);
1241
1242 dpb_size = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR)
1243 ? (8192 * 4320 * 3 / 2) * max_references
1244 : (4096 * 3000 * 3 / 2) * max_references;
1245
1246 if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
1247 dpb_size *= (3 / 2);
1248 break;
1249
1250 case PIPE_VIDEO_FORMAT_JPEG:
1251 dpb_size = 0;
1252 break;
1253
1254 default:
1255 // something is missing here
1256 assert(0);
1257
1258 // at least use a sane default value
1259 dpb_size = 32 * 1024 * 1024;
1260 break;
1261 }
1262 return dpb_size;
1263 }
1264
1265 /**
1266 * destroy this video decoder
1267 */
1268 static void radeon_dec_destroy(struct pipe_video_codec *decoder)
1269 {
1270 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1271 unsigned i;
1272
1273 assert(decoder);
1274
1275 map_msg_fb_it_probs_buf(dec);
1276 rvcn_dec_message_destroy(dec);
1277 send_msg_buf(dec);
1278
1279 flush(dec, 0);
1280
1281 dec->ws->cs_destroy(dec->cs);
1282
1283 for (i = 0; i < NUM_BUFFERS; ++i) {
1284 si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
1285 si_vid_destroy_buffer(&dec->bs_buffers[i]);
1286 }
1287
1288 si_vid_destroy_buffer(&dec->dpb);
1289 si_vid_destroy_buffer(&dec->ctx);
1290 si_vid_destroy_buffer(&dec->sessionctx);
1291
1292 FREE(dec);
1293 }
1294
1295 /**
1296 * start decoding of a new frame
1297 */
1298 static void radeon_dec_begin_frame(struct pipe_video_codec *decoder,
1299 struct pipe_video_buffer *target,
1300 struct pipe_picture_desc *picture)
1301 {
1302 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1303 uintptr_t frame;
1304
1305 assert(decoder);
1306
1307 frame = ++dec->frame_number;
1308 if (dec->stream_type != RDECODE_CODEC_VP9)
1309 vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
1310 &radeon_dec_destroy_associated_data);
1311
1312 dec->bs_size = 0;
1313 dec->bs_ptr = dec->ws->buffer_map(dec->bs_buffers[dec->cur_buffer].res->buf, dec->cs,
1314 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1315 }
1316
1317 /**
1318 * decode a macroblock
1319 */
1320 static void radeon_dec_decode_macroblock(struct pipe_video_codec *decoder,
1321 struct pipe_video_buffer *target,
1322 struct pipe_picture_desc *picture,
1323 const struct pipe_macroblock *macroblocks,
1324 unsigned num_macroblocks)
1325 {
1326 /* not supported (yet) */
1327 assert(0);
1328 }
1329
1330 /**
1331 * decode a bitstream
1332 */
1333 static void radeon_dec_decode_bitstream(struct pipe_video_codec *decoder,
1334 struct pipe_video_buffer *target,
1335 struct pipe_picture_desc *picture, unsigned num_buffers,
1336 const void *const *buffers, const unsigned *sizes)
1337 {
1338 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1339 unsigned i;
1340
1341 assert(decoder);
1342
1343 if (!dec->bs_ptr)
1344 return;
1345
1346 for (i = 0; i < num_buffers; ++i) {
1347 struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
1348 unsigned new_size = dec->bs_size + sizes[i];
1349
1350 if (new_size > buf->res->buf->size) {
1351 dec->ws->buffer_unmap(buf->res->buf);
1352 if (!si_vid_resize_buffer(dec->screen, dec->cs, buf, new_size)) {
1353 RVID_ERR("Can't resize bitstream buffer!");
1354 return;
1355 }
1356
1357 dec->bs_ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1358 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1359 if (!dec->bs_ptr)
1360 return;
1361
1362 dec->bs_ptr += dec->bs_size;
1363 }
1364
1365 memcpy(dec->bs_ptr, buffers[i], sizes[i]);
1366 dec->bs_size += sizes[i];
1367 dec->bs_ptr += sizes[i];
1368 }
1369 }
1370
1371 /**
1372 * send cmd for vcn dec
1373 */
1374 void send_cmd_dec(struct radeon_decoder *dec, struct pipe_video_buffer *target,
1375 struct pipe_picture_desc *picture)
1376 {
1377 struct pb_buffer *dt;
1378 struct rvid_buffer *msg_fb_it_probs_buf, *bs_buf;
1379
1380 msg_fb_it_probs_buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1381 bs_buf = &dec->bs_buffers[dec->cur_buffer];
1382
1383 memset(dec->bs_ptr, 0, align(dec->bs_size, 128) - dec->bs_size);
1384 dec->ws->buffer_unmap(bs_buf->res->buf);
1385
1386 map_msg_fb_it_probs_buf(dec);
1387 dt = rvcn_dec_message_decode(dec, target, picture);
1388 rvcn_dec_message_feedback(dec);
1389 send_msg_buf(dec);
1390
1391 send_cmd(dec, RDECODE_CMD_DPB_BUFFER, dec->dpb.res->buf, 0, RADEON_USAGE_READWRITE,
1392 RADEON_DOMAIN_VRAM);
1393 if (dec->ctx.res)
1394 send_cmd(dec, RDECODE_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0, RADEON_USAGE_READWRITE,
1395 RADEON_DOMAIN_VRAM);
1396 send_cmd(dec, RDECODE_CMD_BITSTREAM_BUFFER, bs_buf->res->buf, 0, RADEON_USAGE_READ,
1397 RADEON_DOMAIN_GTT);
1398 send_cmd(dec, RDECODE_CMD_DECODING_TARGET_BUFFER, dt, 0, RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1399 send_cmd(dec, RDECODE_CMD_FEEDBACK_BUFFER, msg_fb_it_probs_buf->res->buf, FB_BUFFER_OFFSET,
1400 RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1401 if (have_it(dec))
1402 send_cmd(dec, RDECODE_CMD_IT_SCALING_TABLE_BUFFER, msg_fb_it_probs_buf->res->buf,
1403 FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1404 else if (have_probs(dec))
1405 send_cmd(dec, RDECODE_CMD_PROB_TBL_BUFFER, msg_fb_it_probs_buf->res->buf,
1406 FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1407 set_reg(dec, dec->reg.cntl, 1);
1408 }
1409
1410 /**
1411 * end decoding of the current frame
1412 */
1413 static void radeon_dec_end_frame(struct pipe_video_codec *decoder, struct pipe_video_buffer *target,
1414 struct pipe_picture_desc *picture)
1415 {
1416 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1417
1418 assert(decoder);
1419
1420 if (!dec->bs_ptr)
1421 return;
1422
1423 dec->send_cmd(dec, target, picture);
1424 flush(dec, PIPE_FLUSH_ASYNC);
1425 next_buffer(dec);
1426 }
1427
1428 /**
1429 * flush any outstanding command buffers to the hardware
1430 */
1431 static void radeon_dec_flush(struct pipe_video_codec *decoder)
1432 {
1433 }
1434
1435 /**
1436 * create and HW decoder
1437 */
1438 struct pipe_video_codec *radeon_create_decoder(struct pipe_context *context,
1439 const struct pipe_video_codec *templ)
1440 {
1441 struct si_context *sctx = (struct si_context *)context;
1442 struct radeon_winsys *ws = sctx->ws;
1443 unsigned width = templ->width, height = templ->height;
1444 unsigned dpb_size, bs_buf_size, stream_type = 0, ring = RING_VCN_DEC;
1445 struct radeon_decoder *dec;
1446 int r, i;
1447
1448 switch (u_reduce_video_profile(templ->profile)) {
1449 case PIPE_VIDEO_FORMAT_MPEG12:
1450 if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
1451 return vl_create_mpeg12_decoder(context, templ);
1452 stream_type = RDECODE_CODEC_MPEG2_VLD;
1453 break;
1454 case PIPE_VIDEO_FORMAT_MPEG4:
1455 width = align(width, VL_MACROBLOCK_WIDTH);
1456 height = align(height, VL_MACROBLOCK_HEIGHT);
1457 stream_type = RDECODE_CODEC_MPEG4;
1458 break;
1459 case PIPE_VIDEO_FORMAT_VC1:
1460 stream_type = RDECODE_CODEC_VC1;
1461 break;
1462 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1463 width = align(width, VL_MACROBLOCK_WIDTH);
1464 height = align(height, VL_MACROBLOCK_HEIGHT);
1465 stream_type = RDECODE_CODEC_H264_PERF;
1466 break;
1467 case PIPE_VIDEO_FORMAT_HEVC:
1468 stream_type = RDECODE_CODEC_H265;
1469 break;
1470 case PIPE_VIDEO_FORMAT_VP9:
1471 stream_type = RDECODE_CODEC_VP9;
1472 break;
1473 case PIPE_VIDEO_FORMAT_JPEG:
1474 stream_type = RDECODE_CODEC_JPEG;
1475 ring = RING_VCN_JPEG;
1476 break;
1477 default:
1478 assert(0);
1479 break;
1480 }
1481
1482 dec = CALLOC_STRUCT(radeon_decoder);
1483
1484 if (!dec)
1485 return NULL;
1486
1487 dec->base = *templ;
1488 dec->base.context = context;
1489 dec->base.width = width;
1490 dec->base.height = height;
1491
1492 dec->base.destroy = radeon_dec_destroy;
1493 dec->base.begin_frame = radeon_dec_begin_frame;
1494 dec->base.decode_macroblock = radeon_dec_decode_macroblock;
1495 dec->base.decode_bitstream = radeon_dec_decode_bitstream;
1496 dec->base.end_frame = radeon_dec_end_frame;
1497 dec->base.flush = radeon_dec_flush;
1498
1499 dec->stream_type = stream_type;
1500 dec->stream_handle = si_vid_alloc_stream_handle();
1501 dec->screen = context->screen;
1502 dec->ws = ws;
1503 dec->cs = ws->cs_create(sctx->ctx, ring, NULL, NULL, false);
1504 if (!dec->cs) {
1505 RVID_ERR("Can't get command submission context.\n");
1506 goto error;
1507 }
1508
1509 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++)
1510 dec->render_pic_list[i] = NULL;
1511 bs_buf_size = width * height * (512 / (16 * 16));
1512 for (i = 0; i < NUM_BUFFERS; ++i) {
1513 unsigned msg_fb_it_probs_size = FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
1514 if (have_it(dec))
1515 msg_fb_it_probs_size += IT_SCALING_TABLE_SIZE;
1516 else if (have_probs(dec))
1517 msg_fb_it_probs_size += VP9_PROBS_TABLE_SIZE;
1518 /* use vram to improve performance, workaround an unknown bug */
1519 if (!si_vid_create_buffer(dec->screen, &dec->msg_fb_it_probs_buffers[i], msg_fb_it_probs_size,
1520 PIPE_USAGE_DEFAULT)) {
1521 RVID_ERR("Can't allocated message buffers.\n");
1522 goto error;
1523 }
1524
1525 if (!si_vid_create_buffer(dec->screen, &dec->bs_buffers[i], bs_buf_size,
1526 PIPE_USAGE_STAGING)) {
1527 RVID_ERR("Can't allocated bitstream buffers.\n");
1528 goto error;
1529 }
1530
1531 si_vid_clear_buffer(context, &dec->msg_fb_it_probs_buffers[i]);
1532 si_vid_clear_buffer(context, &dec->bs_buffers[i]);
1533
1534 if (have_probs(dec)) {
1535 struct rvid_buffer *buf;
1536 void *ptr;
1537
1538 buf = &dec->msg_fb_it_probs_buffers[i];
1539 ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1540 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1541 ptr += FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
1542 fill_probs_table(ptr);
1543 dec->ws->buffer_unmap(buf->res->buf);
1544 }
1545 }
1546
1547 dpb_size = calc_dpb_size(dec);
1548 if (dpb_size) {
1549 if (!si_vid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1550 RVID_ERR("Can't allocated dpb.\n");
1551 goto error;
1552 }
1553 si_vid_clear_buffer(context, &dec->dpb);
1554 }
1555
1556 if (dec->stream_type == RDECODE_CODEC_H264_PERF) {
1557 unsigned ctx_size = calc_ctx_size_h264_perf(dec);
1558 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1559 RVID_ERR("Can't allocated context buffer.\n");
1560 goto error;
1561 }
1562 si_vid_clear_buffer(context, &dec->ctx);
1563 }
1564
1565 if (!si_vid_create_buffer(dec->screen, &dec->sessionctx, RDECODE_SESSION_CONTEXT_SIZE,
1566 PIPE_USAGE_DEFAULT)) {
1567 RVID_ERR("Can't allocated session ctx.\n");
1568 goto error;
1569 }
1570 si_vid_clear_buffer(context, &dec->sessionctx);
1571
1572 switch (sctx->family) {
1573 case CHIP_RAVEN:
1574 case CHIP_RAVEN2:
1575 dec->reg.data0 = RDECODE_VCN1_GPCOM_VCPU_DATA0;
1576 dec->reg.data1 = RDECODE_VCN1_GPCOM_VCPU_DATA1;
1577 dec->reg.cmd = RDECODE_VCN1_GPCOM_VCPU_CMD;
1578 dec->reg.cntl = RDECODE_VCN1_ENGINE_CNTL;
1579 dec->jpg.direct_reg = false;
1580 break;
1581 case CHIP_NAVI10:
1582 case CHIP_NAVI12:
1583 case CHIP_NAVI14:
1584 case CHIP_RENOIR:
1585 dec->reg.data0 = RDECODE_VCN2_GPCOM_VCPU_DATA0;
1586 dec->reg.data1 = RDECODE_VCN2_GPCOM_VCPU_DATA1;
1587 dec->reg.cmd = RDECODE_VCN2_GPCOM_VCPU_CMD;
1588 dec->reg.cntl = RDECODE_VCN2_ENGINE_CNTL;
1589 dec->jpg.direct_reg = true;
1590 break;
1591 case CHIP_ARCTURUS:
1592 case CHIP_SIENNA:
1593 dec->reg.data0 = RDECODE_VCN2_5_GPCOM_VCPU_DATA0;
1594 dec->reg.data1 = RDECODE_VCN2_5_GPCOM_VCPU_DATA1;
1595 dec->reg.cmd = RDECODE_VCN2_5_GPCOM_VCPU_CMD;
1596 dec->reg.cntl = RDECODE_VCN2_5_ENGINE_CNTL;
1597 dec->jpg.direct_reg = true;
1598 break;
1599 default:
1600 RVID_ERR("VCN is not supported.\n");
1601 goto error;
1602 }
1603
1604 map_msg_fb_it_probs_buf(dec);
1605 rvcn_dec_message_create(dec);
1606 send_msg_buf(dec);
1607 r = flush(dec, 0);
1608 if (r)
1609 goto error;
1610
1611 next_buffer(dec);
1612
1613 if (stream_type == RDECODE_CODEC_JPEG)
1614 dec->send_cmd = send_cmd_jpeg;
1615 else
1616 dec->send_cmd = send_cmd_dec;
1617
1618 return &dec->base;
1619
1620 error:
1621 if (dec->cs)
1622 dec->ws->cs_destroy(dec->cs);
1623
1624 for (i = 0; i < NUM_BUFFERS; ++i) {
1625 si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
1626 si_vid_destroy_buffer(&dec->bs_buffers[i]);
1627 }
1628
1629 si_vid_destroy_buffer(&dec->dpb);
1630 si_vid_destroy_buffer(&dec->ctx);
1631 si_vid_destroy_buffer(&dec->sessionctx);
1632
1633 FREE(dec);
1634
1635 return NULL;
1636 }