panfrost: Allocate a state uploader
[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 ,j;
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 //clear the dec->render list if it is not used as a reference
490 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++) {
491 if (dec->render_pic_list[i]) {
492 for (j=0;j<8;j++) {
493 if (dec->render_pic_list[i] == pic->ref[j])
494 break;
495 }
496 if(j == 8)
497 dec->render_pic_list[i] = NULL;
498 }
499 }
500
501 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); ++i) {
502 if (dec->render_pic_list[i] && dec->render_pic_list[i] == target) {
503 if (target->codec != NULL){
504 result.curr_pic_idx =(uintptr_t)vl_video_buffer_get_associated_data(target, &dec->base);
505 } else {
506 result.curr_pic_idx = i;
507 vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)i,
508 &radeon_dec_destroy_associated_data);
509 }
510 break;
511 } else if (!dec->render_pic_list[i]) {
512 dec->render_pic_list[i] = target;
513 result.curr_pic_idx = i;
514 vl_video_buffer_set_associated_data(target, &dec->base, (void *)(uintptr_t)i,
515 &radeon_dec_destroy_associated_data);
516 break;
517 }
518 }
519
520 for (i = 0; i < 8; i++) {
521 result.ref_frame_map[i] =
522 (pic->ref[i]) ? (uintptr_t)vl_video_buffer_get_associated_data(pic->ref[i], &dec->base)
523 : 0x7f;
524 }
525
526 result.frame_refs[0] = result.ref_frame_map[pic->picture_parameter.pic_fields.last_ref_frame];
527 result.ref_frame_sign_bias[0] = pic->picture_parameter.pic_fields.last_ref_frame_sign_bias;
528 result.frame_refs[1] = result.ref_frame_map[pic->picture_parameter.pic_fields.golden_ref_frame];
529 result.ref_frame_sign_bias[1] = pic->picture_parameter.pic_fields.golden_ref_frame_sign_bias;
530 result.frame_refs[2] = result.ref_frame_map[pic->picture_parameter.pic_fields.alt_ref_frame];
531 result.ref_frame_sign_bias[2] = pic->picture_parameter.pic_fields.alt_ref_frame_sign_bias;
532
533 if (pic->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2) {
534 if (target->buffer_format == PIPE_FORMAT_P010 || target->buffer_format == PIPE_FORMAT_P016) {
535 result.p010_mode = 1;
536 result.msb_mode = 1;
537 } else {
538 result.p010_mode = 0;
539 result.luma_10to8 = 1;
540 result.chroma_10to8 = 1;
541 }
542 }
543
544 return result;
545 }
546
547 static unsigned calc_ctx_size_h265_main(struct radeon_decoder *dec)
548 {
549 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
550 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
551
552 unsigned max_references = dec->base.max_references + 1;
553
554 if (dec->base.width * dec->base.height >= 4096 * 2000)
555 max_references = MAX2(max_references, 8);
556 else
557 max_references = MAX2(max_references, 17);
558
559 width = align(width, 16);
560 height = align(height, 16);
561 return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
562 }
563
564 static unsigned calc_ctx_size_h265_main10(struct radeon_decoder *dec,
565 struct pipe_h265_picture_desc *pic)
566 {
567 unsigned log2_ctb_size, width_in_ctb, height_in_ctb, num_16x16_block_per_ctb;
568 unsigned context_buffer_size_per_ctb_row, cm_buffer_size, max_mb_address, db_left_tile_pxl_size;
569 unsigned db_left_tile_ctx_size = 4096 / 16 * (32 + 16 * 4);
570
571 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
572 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
573 unsigned coeff_10bit =
574 (pic->pps->sps->bit_depth_luma_minus8 || pic->pps->sps->bit_depth_chroma_minus8) ? 2 : 1;
575
576 unsigned max_references = dec->base.max_references + 1;
577
578 if (dec->base.width * dec->base.height >= 4096 * 2000)
579 max_references = MAX2(max_references, 8);
580 else
581 max_references = MAX2(max_references, 17);
582
583 log2_ctb_size = pic->pps->sps->log2_min_luma_coding_block_size_minus3 + 3 +
584 pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
585
586 width_in_ctb = (width + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
587 height_in_ctb = (height + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
588
589 num_16x16_block_per_ctb = ((1 << log2_ctb_size) >> 4) * ((1 << log2_ctb_size) >> 4);
590 context_buffer_size_per_ctb_row = align(width_in_ctb * num_16x16_block_per_ctb * 16, 256);
591 max_mb_address = (unsigned)ceil(height * 8 / 2048.0);
592
593 cm_buffer_size = max_references * context_buffer_size_per_ctb_row * height_in_ctb;
594 db_left_tile_pxl_size = coeff_10bit * (max_mb_address * 2 * 2048 + 1024);
595
596 return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
597 }
598
599 static rvcn_dec_message_vc1_t get_vc1_msg(struct pipe_vc1_picture_desc *pic)
600 {
601 rvcn_dec_message_vc1_t result;
602
603 memset(&result, 0, sizeof(result));
604 switch (pic->base.profile) {
605 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
606 result.profile = RDECODE_VC1_PROFILE_SIMPLE;
607 result.level = 1;
608 break;
609
610 case PIPE_VIDEO_PROFILE_VC1_MAIN:
611 result.profile = RDECODE_VC1_PROFILE_MAIN;
612 result.level = 2;
613 break;
614
615 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
616 result.profile = RDECODE_VC1_PROFILE_ADVANCED;
617 result.level = 4;
618 break;
619
620 default:
621 assert(0);
622 }
623
624 result.sps_info_flags |= pic->postprocflag << 7;
625 result.sps_info_flags |= pic->pulldown << 6;
626 result.sps_info_flags |= pic->interlace << 5;
627 result.sps_info_flags |= pic->tfcntrflag << 4;
628 result.sps_info_flags |= pic->finterpflag << 3;
629 result.sps_info_flags |= pic->psf << 1;
630
631 result.pps_info_flags |= pic->range_mapy_flag << 31;
632 result.pps_info_flags |= pic->range_mapy << 28;
633 result.pps_info_flags |= pic->range_mapuv_flag << 27;
634 result.pps_info_flags |= pic->range_mapuv << 24;
635 result.pps_info_flags |= pic->multires << 21;
636 result.pps_info_flags |= pic->maxbframes << 16;
637 result.pps_info_flags |= pic->overlap << 11;
638 result.pps_info_flags |= pic->quantizer << 9;
639 result.pps_info_flags |= pic->panscan_flag << 7;
640 result.pps_info_flags |= pic->refdist_flag << 6;
641 result.pps_info_flags |= pic->vstransform << 0;
642
643 if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
644 result.pps_info_flags |= pic->syncmarker << 20;
645 result.pps_info_flags |= pic->rangered << 19;
646 result.pps_info_flags |= pic->loopfilter << 5;
647 result.pps_info_flags |= pic->fastuvmc << 4;
648 result.pps_info_flags |= pic->extended_mv << 3;
649 result.pps_info_flags |= pic->extended_dmv << 8;
650 result.pps_info_flags |= pic->dquant << 1;
651 }
652
653 result.chroma_format = 1;
654
655 return result;
656 }
657
658 static uint32_t get_ref_pic_idx(struct radeon_decoder *dec, struct pipe_video_buffer *ref)
659 {
660 uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
661 uint32_t max = MAX2(dec->frame_number, 1) - 1;
662 uintptr_t frame;
663
664 /* seems to be the most sane fallback */
665 if (!ref)
666 return max;
667
668 /* get the frame number from the associated data */
669 frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
670
671 /* limit the frame number to a valid range */
672 return MAX2(MIN2(frame, max), min);
673 }
674
675 static rvcn_dec_message_mpeg2_vld_t get_mpeg2_msg(struct radeon_decoder *dec,
676 struct pipe_mpeg12_picture_desc *pic)
677 {
678 const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
679 rvcn_dec_message_mpeg2_vld_t result;
680 unsigned i;
681
682 memset(&result, 0, sizeof(result));
683 result.decoded_pic_idx = dec->frame_number;
684
685 result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
686 result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
687
688 if (pic->intra_matrix) {
689 result.load_intra_quantiser_matrix = 1;
690 for (i = 0; i < 64; ++i) {
691 result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
692 }
693 }
694 if (pic->non_intra_matrix) {
695 result.load_nonintra_quantiser_matrix = 1;
696 for (i = 0; i < 64; ++i) {
697 result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
698 }
699 }
700
701 result.profile_and_level_indication = 0;
702 result.chroma_format = 0x1;
703
704 result.picture_coding_type = pic->picture_coding_type;
705 result.f_code[0][0] = pic->f_code[0][0] + 1;
706 result.f_code[0][1] = pic->f_code[0][1] + 1;
707 result.f_code[1][0] = pic->f_code[1][0] + 1;
708 result.f_code[1][1] = pic->f_code[1][1] + 1;
709 result.intra_dc_precision = pic->intra_dc_precision;
710 result.pic_structure = pic->picture_structure;
711 result.top_field_first = pic->top_field_first;
712 result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
713 result.concealment_motion_vectors = pic->concealment_motion_vectors;
714 result.q_scale_type = pic->q_scale_type;
715 result.intra_vlc_format = pic->intra_vlc_format;
716 result.alternate_scan = pic->alternate_scan;
717
718 return result;
719 }
720
721 static rvcn_dec_message_mpeg4_asp_vld_t get_mpeg4_msg(struct radeon_decoder *dec,
722 struct pipe_mpeg4_picture_desc *pic)
723 {
724 rvcn_dec_message_mpeg4_asp_vld_t result;
725 unsigned i;
726
727 memset(&result, 0, sizeof(result));
728 result.decoded_pic_idx = dec->frame_number;
729
730 result.forward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[0]);
731 result.backward_ref_pic_idx = get_ref_pic_idx(dec, pic->ref[1]);
732
733 result.variant_type = 0;
734 result.profile_and_level_indication = 0xF0;
735
736 result.video_object_layer_verid = 0x5;
737 result.video_object_layer_shape = 0x0;
738
739 result.video_object_layer_width = dec->base.width;
740 result.video_object_layer_height = dec->base.height;
741
742 result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
743
744 result.short_video_header = pic->short_video_header;
745 result.interlaced = pic->interlaced;
746 result.load_intra_quant_mat = 1;
747 result.load_nonintra_quant_mat = 1;
748 result.quarter_sample = pic->quarter_sample;
749 result.complexity_estimation_disable = 1;
750 result.resync_marker_disable = pic->resync_marker_disable;
751 result.newpred_enable = 0;
752 result.reduced_resolution_vop_enable = 0;
753
754 result.quant_type = pic->quant_type;
755
756 for (i = 0; i < 64; ++i) {
757 result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
758 result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
759 }
760
761 return result;
762 }
763
764 static void rvcn_dec_message_create(struct radeon_decoder *dec)
765 {
766 rvcn_dec_message_header_t *header = dec->msg;
767 rvcn_dec_message_create_t *create = dec->msg + sizeof(rvcn_dec_message_header_t);
768 unsigned sizes = sizeof(rvcn_dec_message_header_t) + sizeof(rvcn_dec_message_create_t);
769
770 memset(dec->msg, 0, sizes);
771 header->header_size = sizeof(rvcn_dec_message_header_t);
772 header->total_size = sizes;
773 header->num_buffers = 1;
774 header->msg_type = RDECODE_MSG_CREATE;
775 header->stream_handle = dec->stream_handle;
776 header->status_report_feedback_number = 0;
777
778 header->index[0].message_id = RDECODE_MESSAGE_CREATE;
779 header->index[0].offset = sizeof(rvcn_dec_message_header_t);
780 header->index[0].size = sizeof(rvcn_dec_message_create_t);
781 header->index[0].filled = 0;
782
783 create->stream_type = dec->stream_type;
784 create->session_flags = 0;
785 create->width_in_samples = dec->base.width;
786 create->height_in_samples = dec->base.height;
787 }
788
789 static struct pb_buffer *rvcn_dec_message_decode(struct radeon_decoder *dec,
790 struct pipe_video_buffer *target,
791 struct pipe_picture_desc *picture)
792 {
793 struct si_texture *luma = (struct si_texture *)((struct vl_video_buffer *)target)->resources[0];
794 struct si_texture *chroma =
795 (struct si_texture *)((struct vl_video_buffer *)target)->resources[1];
796 rvcn_dec_message_header_t *header;
797 rvcn_dec_message_index_t *index;
798 rvcn_dec_message_decode_t *decode;
799 unsigned sizes = 0, offset_decode, offset_codec;
800 void *codec;
801
802 header = dec->msg;
803 sizes += sizeof(rvcn_dec_message_header_t);
804 index = (void *)header + sizeof(rvcn_dec_message_header_t);
805 sizes += sizeof(rvcn_dec_message_index_t);
806 offset_decode = sizes;
807 decode = (void *)index + sizeof(rvcn_dec_message_index_t);
808 sizes += sizeof(rvcn_dec_message_decode_t);
809 offset_codec = sizes;
810 codec = (void *)decode + sizeof(rvcn_dec_message_decode_t);
811
812 memset(dec->msg, 0, sizes);
813 header->header_size = sizeof(rvcn_dec_message_header_t);
814 header->total_size = sizes;
815 header->num_buffers = 2;
816 header->msg_type = RDECODE_MSG_DECODE;
817 header->stream_handle = dec->stream_handle;
818 header->status_report_feedback_number = dec->frame_number;
819
820 header->index[0].message_id = RDECODE_MESSAGE_DECODE;
821 header->index[0].offset = offset_decode;
822 header->index[0].size = sizeof(rvcn_dec_message_decode_t);
823 header->index[0].filled = 0;
824
825 index->offset = offset_codec;
826 index->size = sizeof(rvcn_dec_message_avc_t);
827 index->filled = 0;
828
829 decode->stream_type = dec->stream_type;
830 decode->decode_flags = 0;
831 decode->width_in_samples = dec->base.width;
832 decode->height_in_samples = dec->base.height;
833
834 decode->bsd_size = align(dec->bs_size, 128);
835 decode->dpb_size = dec->dpb.res->buf->size;
836 decode->dt_size = si_resource(((struct vl_video_buffer *)target)->resources[0])->buf->size +
837 si_resource(((struct vl_video_buffer *)target)->resources[1])->buf->size;
838
839 decode->sct_size = 0;
840 decode->sc_coeff_size = 0;
841
842 decode->sw_ctxt_size = RDECODE_SESSION_CONTEXT_SIZE;
843 decode->db_pitch = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR &&
844 dec->base.width > 32 && dec->stream_type == RDECODE_CODEC_VP9)
845 ? align(dec->base.width, 64)
846 : align(dec->base.width, 32);
847 if (((struct si_screen*)dec->screen)->info.family >= CHIP_SIENNA_CICHLID &&
848 dec->stream_type == RDECODE_CODEC_VP9)
849 decode->db_aligned_height = align(dec->base.height, 64);
850
851 decode->db_surf_tile_config = 0;
852
853 decode->dt_pitch = luma->surface.u.gfx9.surf_pitch * luma->surface.blk_w;
854 decode->dt_uv_pitch = decode->dt_pitch / 2;
855
856 decode->dt_tiling_mode = 0;
857 decode->dt_swizzle_mode = RDECODE_SW_MODE_LINEAR;
858 decode->dt_array_mode = RDECODE_ARRAY_MODE_LINEAR;
859 decode->dt_field_mode = ((struct vl_video_buffer *)target)->base.interlaced;
860 decode->dt_surf_tile_config = 0;
861 decode->dt_uv_surf_tile_config = 0;
862
863 decode->dt_luma_top_offset = luma->surface.u.gfx9.surf_offset;
864 decode->dt_chroma_top_offset = chroma->surface.u.gfx9.surf_offset;
865 if (decode->dt_field_mode) {
866 decode->dt_luma_bottom_offset =
867 luma->surface.u.gfx9.surf_offset + luma->surface.u.gfx9.surf_slice_size;
868 decode->dt_chroma_bottom_offset =
869 chroma->surface.u.gfx9.surf_offset + chroma->surface.u.gfx9.surf_slice_size;
870 } else {
871 decode->dt_luma_bottom_offset = decode->dt_luma_top_offset;
872 decode->dt_chroma_bottom_offset = decode->dt_chroma_top_offset;
873 }
874
875 switch (u_reduce_video_profile(picture->profile)) {
876 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
877 rvcn_dec_message_avc_t avc = get_h264_msg(dec, (struct pipe_h264_picture_desc *)picture);
878 memcpy(codec, (void *)&avc, sizeof(rvcn_dec_message_avc_t));
879 index->message_id = RDECODE_MESSAGE_AVC;
880 break;
881 }
882 case PIPE_VIDEO_FORMAT_HEVC: {
883 rvcn_dec_message_hevc_t hevc =
884 get_h265_msg(dec, target, (struct pipe_h265_picture_desc *)picture);
885
886 memcpy(codec, (void *)&hevc, sizeof(rvcn_dec_message_hevc_t));
887 index->message_id = RDECODE_MESSAGE_HEVC;
888 if (dec->ctx.res == NULL) {
889 unsigned ctx_size;
890 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
891 ctx_size = calc_ctx_size_h265_main10(dec, (struct pipe_h265_picture_desc *)picture);
892 else
893 ctx_size = calc_ctx_size_h265_main(dec);
894 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT))
895 RVID_ERR("Can't allocated context buffer.\n");
896 si_vid_clear_buffer(dec->base.context, &dec->ctx);
897 }
898 break;
899 }
900 case PIPE_VIDEO_FORMAT_VC1: {
901 rvcn_dec_message_vc1_t vc1 = get_vc1_msg((struct pipe_vc1_picture_desc *)picture);
902
903 memcpy(codec, (void *)&vc1, sizeof(rvcn_dec_message_vc1_t));
904 if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
905 (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
906 decode->width_in_samples = align(decode->width_in_samples, 16) / 16;
907 decode->height_in_samples = align(decode->height_in_samples, 16) / 16;
908 }
909 index->message_id = RDECODE_MESSAGE_VC1;
910 break;
911 }
912 case PIPE_VIDEO_FORMAT_MPEG12: {
913 rvcn_dec_message_mpeg2_vld_t mpeg2 =
914 get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc *)picture);
915
916 memcpy(codec, (void *)&mpeg2, sizeof(rvcn_dec_message_mpeg2_vld_t));
917 index->message_id = RDECODE_MESSAGE_MPEG2_VLD;
918 break;
919 }
920 case PIPE_VIDEO_FORMAT_MPEG4: {
921 rvcn_dec_message_mpeg4_asp_vld_t mpeg4 =
922 get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc *)picture);
923
924 memcpy(codec, (void *)&mpeg4, sizeof(rvcn_dec_message_mpeg4_asp_vld_t));
925 index->message_id = RDECODE_MESSAGE_MPEG4_ASP_VLD;
926 break;
927 }
928 case PIPE_VIDEO_FORMAT_VP9: {
929 rvcn_dec_message_vp9_t vp9 =
930 get_vp9_msg(dec, target, (struct pipe_vp9_picture_desc *)picture);
931
932 memcpy(codec, (void *)&vp9, sizeof(rvcn_dec_message_vp9_t));
933 index->message_id = RDECODE_MESSAGE_VP9;
934
935 if (dec->ctx.res == NULL) {
936 unsigned ctx_size;
937 uint8_t *ptr;
938
939 /* default probability + probability data */
940 ctx_size = 2304 * 5;
941
942 if (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR) {
943 /* SRE collocated context data */
944 ctx_size += 32 * 2 * 128 * 68;
945 /* SMP collocated context data */
946 ctx_size += 9 * 64 * 2 * 128 * 68;
947 /* SDB left tile pixel */
948 ctx_size += 8 * 2 * 2 * 8192;
949 } else {
950 ctx_size += 32 * 2 * 64 * 64;
951 ctx_size += 9 * 64 * 2 * 64 * 64;
952 ctx_size += 8 * 2 * 4096;
953 }
954
955 if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
956 ctx_size += 8 * 2 * 4096;
957
958 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT))
959 RVID_ERR("Can't allocated context buffer.\n");
960 si_vid_clear_buffer(dec->base.context, &dec->ctx);
961
962 /* ctx needs probs table */
963 ptr = dec->ws->buffer_map(dec->ctx.res->buf, dec->cs,
964 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
965 fill_probs_table(ptr);
966 dec->ws->buffer_unmap(dec->ctx.res->buf);
967 }
968 break;
969 }
970 default:
971 assert(0);
972 return NULL;
973 }
974
975 if (dec->ctx.res)
976 decode->hw_ctxt_size = dec->ctx.res->buf->size;
977
978 return luma->buffer.buf;
979 }
980
981 static void rvcn_dec_message_destroy(struct radeon_decoder *dec)
982 {
983 rvcn_dec_message_header_t *header = dec->msg;
984
985 memset(dec->msg, 0, sizeof(rvcn_dec_message_header_t));
986 header->header_size = sizeof(rvcn_dec_message_header_t);
987 header->total_size = sizeof(rvcn_dec_message_header_t) - sizeof(rvcn_dec_message_index_t);
988 header->num_buffers = 0;
989 header->msg_type = RDECODE_MSG_DESTROY;
990 header->stream_handle = dec->stream_handle;
991 header->status_report_feedback_number = 0;
992 }
993
994 static void rvcn_dec_message_feedback(struct radeon_decoder *dec)
995 {
996 rvcn_dec_feedback_header_t *header = (void *)dec->fb;
997
998 header->header_size = sizeof(rvcn_dec_feedback_header_t);
999 header->total_size = sizeof(rvcn_dec_feedback_header_t);
1000 header->num_buffers = 0;
1001 }
1002
1003 /* flush IB to the hardware */
1004 static int flush(struct radeon_decoder *dec, unsigned flags)
1005 {
1006 return dec->ws->cs_flush(dec->cs, flags, NULL);
1007 }
1008
1009 /* add a new set register command to the IB */
1010 static void set_reg(struct radeon_decoder *dec, unsigned reg, uint32_t val)
1011 {
1012 radeon_emit(dec->cs, RDECODE_PKT0(reg >> 2, 0));
1013 radeon_emit(dec->cs, val);
1014 }
1015
1016 /* send a command to the VCPU through the GPCOM registers */
1017 static void send_cmd(struct radeon_decoder *dec, unsigned cmd, struct pb_buffer *buf, uint32_t off,
1018 enum radeon_bo_usage usage, enum radeon_bo_domain domain)
1019 {
1020 uint64_t addr;
1021
1022 dec->ws->cs_add_buffer(dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain, 0);
1023 addr = dec->ws->buffer_get_virtual_address(buf);
1024 addr = addr + off;
1025
1026 set_reg(dec, dec->reg.data0, addr);
1027 set_reg(dec, dec->reg.data1, addr >> 32);
1028 set_reg(dec, dec->reg.cmd, cmd << 1);
1029 }
1030
1031 /* do the codec needs an IT buffer ?*/
1032 static bool have_it(struct radeon_decoder *dec)
1033 {
1034 return dec->stream_type == RDECODE_CODEC_H264_PERF || dec->stream_type == RDECODE_CODEC_H265;
1035 }
1036
1037 /* do the codec needs an probs buffer? */
1038 static bool have_probs(struct radeon_decoder *dec)
1039 {
1040 return dec->stream_type == RDECODE_CODEC_VP9;
1041 }
1042
1043 /* map the next available message/feedback/itscaling buffer */
1044 static void map_msg_fb_it_probs_buf(struct radeon_decoder *dec)
1045 {
1046 struct rvid_buffer *buf;
1047 uint8_t *ptr;
1048
1049 /* grab the current message/feedback buffer */
1050 buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1051
1052 /* and map it for CPU access */
1053 ptr =
1054 dec->ws->buffer_map(buf->res->buf, dec->cs, PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1055
1056 /* calc buffer offsets */
1057 dec->msg = ptr;
1058
1059 dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
1060 if (have_it(dec))
1061 dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1062 else if (have_probs(dec))
1063 dec->probs = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
1064 }
1065
1066 /* unmap and send a message command to the VCPU */
1067 static void send_msg_buf(struct radeon_decoder *dec)
1068 {
1069 struct rvid_buffer *buf;
1070
1071 /* ignore the request if message/feedback buffer isn't mapped */
1072 if (!dec->msg || !dec->fb)
1073 return;
1074
1075 /* grab the current message buffer */
1076 buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1077
1078 /* unmap the buffer */
1079 dec->ws->buffer_unmap(buf->res->buf);
1080 dec->msg = NULL;
1081 dec->fb = NULL;
1082 dec->it = NULL;
1083 dec->probs = NULL;
1084
1085 if (dec->sessionctx.res)
1086 send_cmd(dec, RDECODE_CMD_SESSION_CONTEXT_BUFFER, dec->sessionctx.res->buf, 0,
1087 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1088
1089 /* and send it to the hardware */
1090 send_cmd(dec, RDECODE_CMD_MSG_BUFFER, buf->res->buf, 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1091 }
1092
1093 /* cycle to the next set of buffers */
1094 static void next_buffer(struct radeon_decoder *dec)
1095 {
1096 ++dec->cur_buffer;
1097 dec->cur_buffer %= NUM_BUFFERS;
1098 }
1099
1100 static unsigned calc_ctx_size_h264_perf(struct radeon_decoder *dec)
1101 {
1102 unsigned width_in_mb, height_in_mb, ctx_size;
1103 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1104 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1105
1106 unsigned max_references = dec->base.max_references + 1;
1107
1108 // picture width & height in 16 pixel units
1109 width_in_mb = width / VL_MACROBLOCK_WIDTH;
1110 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
1111
1112 unsigned fs_in_mb = width_in_mb * height_in_mb;
1113 unsigned num_dpb_buffer;
1114 switch (dec->base.level) {
1115 case 30:
1116 num_dpb_buffer = 8100 / fs_in_mb;
1117 break;
1118 case 31:
1119 num_dpb_buffer = 18000 / fs_in_mb;
1120 break;
1121 case 32:
1122 num_dpb_buffer = 20480 / fs_in_mb;
1123 break;
1124 case 41:
1125 num_dpb_buffer = 32768 / fs_in_mb;
1126 break;
1127 case 42:
1128 num_dpb_buffer = 34816 / fs_in_mb;
1129 break;
1130 case 50:
1131 num_dpb_buffer = 110400 / fs_in_mb;
1132 break;
1133 case 51:
1134 num_dpb_buffer = 184320 / fs_in_mb;
1135 break;
1136 default:
1137 num_dpb_buffer = 184320 / fs_in_mb;
1138 break;
1139 }
1140 num_dpb_buffer++;
1141 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
1142 ctx_size = max_references * align(width_in_mb * height_in_mb * 192, 256);
1143
1144 return ctx_size;
1145 }
1146
1147 /* calculate size of reference picture buffer */
1148 static unsigned calc_dpb_size(struct radeon_decoder *dec)
1149 {
1150 unsigned width_in_mb, height_in_mb, image_size, dpb_size;
1151
1152 // always align them to MB size for dpb calculation
1153 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
1154 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
1155
1156 // always one more for currently decoded picture
1157 unsigned max_references = dec->base.max_references + 1;
1158
1159 // aligned size of a single frame
1160 image_size = align(width, 32) * height;
1161 image_size += image_size / 2;
1162 image_size = align(image_size, 1024);
1163
1164 // picture width & height in 16 pixel units
1165 width_in_mb = width / VL_MACROBLOCK_WIDTH;
1166 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
1167
1168 switch (u_reduce_video_profile(dec->base.profile)) {
1169 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
1170 unsigned fs_in_mb = width_in_mb * height_in_mb;
1171 unsigned num_dpb_buffer;
1172
1173 switch (dec->base.level) {
1174 case 30:
1175 num_dpb_buffer = 8100 / fs_in_mb;
1176 break;
1177 case 31:
1178 num_dpb_buffer = 18000 / fs_in_mb;
1179 break;
1180 case 32:
1181 num_dpb_buffer = 20480 / fs_in_mb;
1182 break;
1183 case 41:
1184 num_dpb_buffer = 32768 / fs_in_mb;
1185 break;
1186 case 42:
1187 num_dpb_buffer = 34816 / fs_in_mb;
1188 break;
1189 case 50:
1190 num_dpb_buffer = 110400 / fs_in_mb;
1191 break;
1192 case 51:
1193 num_dpb_buffer = 184320 / fs_in_mb;
1194 break;
1195 default:
1196 num_dpb_buffer = 184320 / fs_in_mb;
1197 break;
1198 }
1199 num_dpb_buffer++;
1200 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
1201 dpb_size = image_size * max_references;
1202 break;
1203 }
1204
1205 case PIPE_VIDEO_FORMAT_HEVC:
1206 if (dec->base.width * dec->base.height >= 4096 * 2000)
1207 max_references = MAX2(max_references, 8);
1208 else
1209 max_references = MAX2(max_references, 17);
1210
1211 width = align(width, 16);
1212 height = align(height, 16);
1213 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
1214 dpb_size = align((align(width, 32) * height * 9) / 4, 256) * max_references;
1215 else
1216 dpb_size = align((align(width, 32) * height * 3) / 2, 256) * max_references;
1217 break;
1218
1219 case PIPE_VIDEO_FORMAT_VC1:
1220 // the firmware seems to allways assume a minimum of ref frames
1221 max_references = MAX2(NUM_VC1_REFS, max_references);
1222
1223 // reference picture buffer
1224 dpb_size = image_size * max_references;
1225
1226 // CONTEXT_BUFFER
1227 dpb_size += width_in_mb * height_in_mb * 128;
1228
1229 // IT surface buffer
1230 dpb_size += width_in_mb * 64;
1231
1232 // DB surface buffer
1233 dpb_size += width_in_mb * 128;
1234
1235 // BP
1236 dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
1237 break;
1238
1239 case PIPE_VIDEO_FORMAT_MPEG12:
1240 // reference picture buffer, must be big enough for all frames
1241 dpb_size = image_size * NUM_MPEG2_REFS;
1242 break;
1243
1244 case PIPE_VIDEO_FORMAT_MPEG4:
1245 // reference picture buffer
1246 dpb_size = image_size * max_references;
1247
1248 // CM
1249 dpb_size += width_in_mb * height_in_mb * 64;
1250
1251 // IT surface buffer
1252 dpb_size += align(width_in_mb * height_in_mb * 32, 64);
1253
1254 dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
1255 break;
1256
1257 case PIPE_VIDEO_FORMAT_VP9:
1258 max_references = MAX2(max_references, 9);
1259
1260 dpb_size = (((struct si_screen *)dec->screen)->info.family >= CHIP_RENOIR)
1261 ? (8192 * 4320 * 3 / 2) * max_references
1262 : (4096 * 3000 * 3 / 2) * max_references;
1263
1264 if (dec->base.profile == PIPE_VIDEO_PROFILE_VP9_PROFILE2)
1265 dpb_size *= (3 / 2);
1266 break;
1267
1268 case PIPE_VIDEO_FORMAT_JPEG:
1269 dpb_size = 0;
1270 break;
1271
1272 default:
1273 // something is missing here
1274 assert(0);
1275
1276 // at least use a sane default value
1277 dpb_size = 32 * 1024 * 1024;
1278 break;
1279 }
1280 return dpb_size;
1281 }
1282
1283 /**
1284 * destroy this video decoder
1285 */
1286 static void radeon_dec_destroy(struct pipe_video_codec *decoder)
1287 {
1288 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1289 unsigned i;
1290
1291 assert(decoder);
1292
1293 map_msg_fb_it_probs_buf(dec);
1294 rvcn_dec_message_destroy(dec);
1295 send_msg_buf(dec);
1296
1297 flush(dec, 0);
1298
1299 dec->ws->cs_destroy(dec->cs);
1300
1301 for (i = 0; i < NUM_BUFFERS; ++i) {
1302 si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
1303 si_vid_destroy_buffer(&dec->bs_buffers[i]);
1304 }
1305
1306 si_vid_destroy_buffer(&dec->dpb);
1307 si_vid_destroy_buffer(&dec->ctx);
1308 si_vid_destroy_buffer(&dec->sessionctx);
1309
1310 FREE(dec);
1311 }
1312
1313 /**
1314 * start decoding of a new frame
1315 */
1316 static void radeon_dec_begin_frame(struct pipe_video_codec *decoder,
1317 struct pipe_video_buffer *target,
1318 struct pipe_picture_desc *picture)
1319 {
1320 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1321 uintptr_t frame;
1322
1323 assert(decoder);
1324
1325 frame = ++dec->frame_number;
1326 if (dec->stream_type != RDECODE_CODEC_VP9)
1327 vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
1328 &radeon_dec_destroy_associated_data);
1329
1330 dec->bs_size = 0;
1331 dec->bs_ptr = dec->ws->buffer_map(dec->bs_buffers[dec->cur_buffer].res->buf, dec->cs,
1332 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1333 }
1334
1335 /**
1336 * decode a macroblock
1337 */
1338 static void radeon_dec_decode_macroblock(struct pipe_video_codec *decoder,
1339 struct pipe_video_buffer *target,
1340 struct pipe_picture_desc *picture,
1341 const struct pipe_macroblock *macroblocks,
1342 unsigned num_macroblocks)
1343 {
1344 /* not supported (yet) */
1345 assert(0);
1346 }
1347
1348 /**
1349 * decode a bitstream
1350 */
1351 static void radeon_dec_decode_bitstream(struct pipe_video_codec *decoder,
1352 struct pipe_video_buffer *target,
1353 struct pipe_picture_desc *picture, unsigned num_buffers,
1354 const void *const *buffers, const unsigned *sizes)
1355 {
1356 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1357 unsigned i;
1358
1359 assert(decoder);
1360
1361 if (!dec->bs_ptr)
1362 return;
1363
1364 for (i = 0; i < num_buffers; ++i) {
1365 struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
1366 unsigned new_size = dec->bs_size + sizes[i];
1367
1368 if (new_size > buf->res->buf->size) {
1369 dec->ws->buffer_unmap(buf->res->buf);
1370 if (!si_vid_resize_buffer(dec->screen, dec->cs, buf, new_size)) {
1371 RVID_ERR("Can't resize bitstream buffer!");
1372 return;
1373 }
1374
1375 dec->bs_ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1376 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1377 if (!dec->bs_ptr)
1378 return;
1379
1380 dec->bs_ptr += dec->bs_size;
1381 }
1382
1383 memcpy(dec->bs_ptr, buffers[i], sizes[i]);
1384 dec->bs_size += sizes[i];
1385 dec->bs_ptr += sizes[i];
1386 }
1387 }
1388
1389 /**
1390 * send cmd for vcn dec
1391 */
1392 void send_cmd_dec(struct radeon_decoder *dec, struct pipe_video_buffer *target,
1393 struct pipe_picture_desc *picture)
1394 {
1395 struct pb_buffer *dt;
1396 struct rvid_buffer *msg_fb_it_probs_buf, *bs_buf;
1397
1398 msg_fb_it_probs_buf = &dec->msg_fb_it_probs_buffers[dec->cur_buffer];
1399 bs_buf = &dec->bs_buffers[dec->cur_buffer];
1400
1401 memset(dec->bs_ptr, 0, align(dec->bs_size, 128) - dec->bs_size);
1402 dec->ws->buffer_unmap(bs_buf->res->buf);
1403
1404 map_msg_fb_it_probs_buf(dec);
1405 dt = rvcn_dec_message_decode(dec, target, picture);
1406 rvcn_dec_message_feedback(dec);
1407 send_msg_buf(dec);
1408
1409 send_cmd(dec, RDECODE_CMD_DPB_BUFFER, dec->dpb.res->buf, 0, RADEON_USAGE_READWRITE,
1410 RADEON_DOMAIN_VRAM);
1411 if (dec->ctx.res)
1412 send_cmd(dec, RDECODE_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0, RADEON_USAGE_READWRITE,
1413 RADEON_DOMAIN_VRAM);
1414 send_cmd(dec, RDECODE_CMD_BITSTREAM_BUFFER, bs_buf->res->buf, 0, RADEON_USAGE_READ,
1415 RADEON_DOMAIN_GTT);
1416 send_cmd(dec, RDECODE_CMD_DECODING_TARGET_BUFFER, dt, 0, RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1417 send_cmd(dec, RDECODE_CMD_FEEDBACK_BUFFER, msg_fb_it_probs_buf->res->buf, FB_BUFFER_OFFSET,
1418 RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1419 if (have_it(dec))
1420 send_cmd(dec, RDECODE_CMD_IT_SCALING_TABLE_BUFFER, msg_fb_it_probs_buf->res->buf,
1421 FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1422 else if (have_probs(dec))
1423 send_cmd(dec, RDECODE_CMD_PROB_TBL_BUFFER, msg_fb_it_probs_buf->res->buf,
1424 FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1425 set_reg(dec, dec->reg.cntl, 1);
1426 }
1427
1428 /**
1429 * end decoding of the current frame
1430 */
1431 static void radeon_dec_end_frame(struct pipe_video_codec *decoder, struct pipe_video_buffer *target,
1432 struct pipe_picture_desc *picture)
1433 {
1434 struct radeon_decoder *dec = (struct radeon_decoder *)decoder;
1435
1436 assert(decoder);
1437
1438 if (!dec->bs_ptr)
1439 return;
1440
1441 dec->send_cmd(dec, target, picture);
1442 flush(dec, PIPE_FLUSH_ASYNC);
1443 next_buffer(dec);
1444 }
1445
1446 /**
1447 * flush any outstanding command buffers to the hardware
1448 */
1449 static void radeon_dec_flush(struct pipe_video_codec *decoder)
1450 {
1451 }
1452
1453 /**
1454 * create and HW decoder
1455 */
1456 struct pipe_video_codec *radeon_create_decoder(struct pipe_context *context,
1457 const struct pipe_video_codec *templ)
1458 {
1459 struct si_context *sctx = (struct si_context *)context;
1460 struct radeon_winsys *ws = sctx->ws;
1461 unsigned width = templ->width, height = templ->height;
1462 unsigned dpb_size, bs_buf_size, stream_type = 0, ring = RING_VCN_DEC;
1463 struct radeon_decoder *dec;
1464 int r, i;
1465
1466 switch (u_reduce_video_profile(templ->profile)) {
1467 case PIPE_VIDEO_FORMAT_MPEG12:
1468 if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM)
1469 return vl_create_mpeg12_decoder(context, templ);
1470 stream_type = RDECODE_CODEC_MPEG2_VLD;
1471 break;
1472 case PIPE_VIDEO_FORMAT_MPEG4:
1473 width = align(width, VL_MACROBLOCK_WIDTH);
1474 height = align(height, VL_MACROBLOCK_HEIGHT);
1475 stream_type = RDECODE_CODEC_MPEG4;
1476 break;
1477 case PIPE_VIDEO_FORMAT_VC1:
1478 stream_type = RDECODE_CODEC_VC1;
1479 break;
1480 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1481 width = align(width, VL_MACROBLOCK_WIDTH);
1482 height = align(height, VL_MACROBLOCK_HEIGHT);
1483 stream_type = RDECODE_CODEC_H264_PERF;
1484 break;
1485 case PIPE_VIDEO_FORMAT_HEVC:
1486 stream_type = RDECODE_CODEC_H265;
1487 break;
1488 case PIPE_VIDEO_FORMAT_VP9:
1489 stream_type = RDECODE_CODEC_VP9;
1490 break;
1491 case PIPE_VIDEO_FORMAT_JPEG:
1492 stream_type = RDECODE_CODEC_JPEG;
1493 ring = RING_VCN_JPEG;
1494 break;
1495 default:
1496 assert(0);
1497 break;
1498 }
1499
1500 dec = CALLOC_STRUCT(radeon_decoder);
1501
1502 if (!dec)
1503 return NULL;
1504
1505 dec->base = *templ;
1506 dec->base.context = context;
1507 dec->base.width = width;
1508 dec->base.height = height;
1509
1510 dec->base.destroy = radeon_dec_destroy;
1511 dec->base.begin_frame = radeon_dec_begin_frame;
1512 dec->base.decode_macroblock = radeon_dec_decode_macroblock;
1513 dec->base.decode_bitstream = radeon_dec_decode_bitstream;
1514 dec->base.end_frame = radeon_dec_end_frame;
1515 dec->base.flush = radeon_dec_flush;
1516
1517 dec->stream_type = stream_type;
1518 dec->stream_handle = si_vid_alloc_stream_handle();
1519 dec->screen = context->screen;
1520 dec->ws = ws;
1521 dec->cs = ws->cs_create(sctx->ctx, ring, NULL, NULL, false);
1522 if (!dec->cs) {
1523 RVID_ERR("Can't get command submission context.\n");
1524 goto error;
1525 }
1526
1527 for (i = 0; i < ARRAY_SIZE(dec->render_pic_list); i++)
1528 dec->render_pic_list[i] = NULL;
1529 bs_buf_size = width * height * (512 / (16 * 16));
1530 for (i = 0; i < NUM_BUFFERS; ++i) {
1531 unsigned msg_fb_it_probs_size = FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
1532 if (have_it(dec))
1533 msg_fb_it_probs_size += IT_SCALING_TABLE_SIZE;
1534 else if (have_probs(dec))
1535 msg_fb_it_probs_size += VP9_PROBS_TABLE_SIZE;
1536 /* use vram to improve performance, workaround an unknown bug */
1537 if (!si_vid_create_buffer(dec->screen, &dec->msg_fb_it_probs_buffers[i], msg_fb_it_probs_size,
1538 PIPE_USAGE_DEFAULT)) {
1539 RVID_ERR("Can't allocated message buffers.\n");
1540 goto error;
1541 }
1542
1543 if (!si_vid_create_buffer(dec->screen, &dec->bs_buffers[i], bs_buf_size,
1544 PIPE_USAGE_STAGING)) {
1545 RVID_ERR("Can't allocated bitstream buffers.\n");
1546 goto error;
1547 }
1548
1549 si_vid_clear_buffer(context, &dec->msg_fb_it_probs_buffers[i]);
1550 si_vid_clear_buffer(context, &dec->bs_buffers[i]);
1551
1552 if (have_probs(dec)) {
1553 struct rvid_buffer *buf;
1554 void *ptr;
1555
1556 buf = &dec->msg_fb_it_probs_buffers[i];
1557 ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1558 PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
1559 ptr += FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
1560 fill_probs_table(ptr);
1561 dec->ws->buffer_unmap(buf->res->buf);
1562 }
1563 }
1564
1565 dpb_size = calc_dpb_size(dec);
1566 if (dpb_size) {
1567 if (!si_vid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1568 RVID_ERR("Can't allocated dpb.\n");
1569 goto error;
1570 }
1571 si_vid_clear_buffer(context, &dec->dpb);
1572 }
1573
1574 if (dec->stream_type == RDECODE_CODEC_H264_PERF) {
1575 unsigned ctx_size = calc_ctx_size_h264_perf(dec);
1576 if (!si_vid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1577 RVID_ERR("Can't allocated context buffer.\n");
1578 goto error;
1579 }
1580 si_vid_clear_buffer(context, &dec->ctx);
1581 }
1582
1583 if (!si_vid_create_buffer(dec->screen, &dec->sessionctx, RDECODE_SESSION_CONTEXT_SIZE,
1584 PIPE_USAGE_DEFAULT)) {
1585 RVID_ERR("Can't allocated session ctx.\n");
1586 goto error;
1587 }
1588 si_vid_clear_buffer(context, &dec->sessionctx);
1589
1590 switch (sctx->family) {
1591 case CHIP_RAVEN:
1592 case CHIP_RAVEN2:
1593 dec->reg.data0 = RDECODE_VCN1_GPCOM_VCPU_DATA0;
1594 dec->reg.data1 = RDECODE_VCN1_GPCOM_VCPU_DATA1;
1595 dec->reg.cmd = RDECODE_VCN1_GPCOM_VCPU_CMD;
1596 dec->reg.cntl = RDECODE_VCN1_ENGINE_CNTL;
1597 dec->jpg.direct_reg = false;
1598 break;
1599 case CHIP_NAVI10:
1600 case CHIP_NAVI12:
1601 case CHIP_NAVI14:
1602 case CHIP_RENOIR:
1603 dec->reg.data0 = RDECODE_VCN2_GPCOM_VCPU_DATA0;
1604 dec->reg.data1 = RDECODE_VCN2_GPCOM_VCPU_DATA1;
1605 dec->reg.cmd = RDECODE_VCN2_GPCOM_VCPU_CMD;
1606 dec->reg.cntl = RDECODE_VCN2_ENGINE_CNTL;
1607 dec->jpg.direct_reg = true;
1608 break;
1609 case CHIP_ARCTURUS:
1610 case CHIP_SIENNA_CICHLID:
1611 case CHIP_NAVY_FLOUNDER:
1612 dec->reg.data0 = RDECODE_VCN2_5_GPCOM_VCPU_DATA0;
1613 dec->reg.data1 = RDECODE_VCN2_5_GPCOM_VCPU_DATA1;
1614 dec->reg.cmd = RDECODE_VCN2_5_GPCOM_VCPU_CMD;
1615 dec->reg.cntl = RDECODE_VCN2_5_ENGINE_CNTL;
1616 dec->jpg.direct_reg = true;
1617 break;
1618 default:
1619 RVID_ERR("VCN is not supported.\n");
1620 goto error;
1621 }
1622
1623 map_msg_fb_it_probs_buf(dec);
1624 rvcn_dec_message_create(dec);
1625 send_msg_buf(dec);
1626 r = flush(dec, 0);
1627 if (r)
1628 goto error;
1629
1630 next_buffer(dec);
1631
1632 if (stream_type == RDECODE_CODEC_JPEG)
1633 dec->send_cmd = send_cmd_jpeg;
1634 else
1635 dec->send_cmd = send_cmd_dec;
1636
1637 return &dec->base;
1638
1639 error:
1640 if (dec->cs)
1641 dec->ws->cs_destroy(dec->cs);
1642
1643 for (i = 0; i < NUM_BUFFERS; ++i) {
1644 si_vid_destroy_buffer(&dec->msg_fb_it_probs_buffers[i]);
1645 si_vid_destroy_buffer(&dec->bs_buffers[i]);
1646 }
1647
1648 si_vid_destroy_buffer(&dec->dpb);
1649 si_vid_destroy_buffer(&dec->ctx);
1650 si_vid_destroy_buffer(&dec->sessionctx);
1651
1652 FREE(dec);
1653
1654 return NULL;
1655 }