0c643e5cd594cf6b7142a7049b7d78758c96b2fc
[mesa.git] / src / gallium / drivers / radeon / radeon_uvd.c
1 /**************************************************************************
2 *
3 * Copyright 2011 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 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 #include <sys/types.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <stdio.h>
39
40 #include "pipe/p_video_codec.h"
41
42 #include "util/u_memory.h"
43 #include "util/u_video.h"
44
45 #include "vl/vl_defines.h"
46 #include "vl/vl_mpeg12_decoder.h"
47
48 #include "r600_pipe_common.h"
49 #include "radeon_video.h"
50 #include "radeon_uvd.h"
51
52 #define NUM_BUFFERS 4
53
54 #define NUM_MPEG2_REFS 6
55 #define NUM_H264_REFS 17
56 #define NUM_VC1_REFS 5
57
58 #define FB_BUFFER_OFFSET 0x1000
59 #define FB_BUFFER_SIZE 2048
60 #define IT_SCALING_TABLE_SIZE 992
61
62 /* UVD decoder representation */
63 struct ruvd_decoder {
64 struct pipe_video_codec base;
65
66 ruvd_set_dtb set_dtb;
67
68 unsigned stream_handle;
69 unsigned stream_type;
70 unsigned frame_number;
71
72 struct pipe_screen *screen;
73 struct radeon_winsys* ws;
74 struct radeon_winsys_cs* cs;
75
76 unsigned cur_buffer;
77
78 struct rvid_buffer msg_fb_it_buffers[NUM_BUFFERS];
79 struct ruvd_msg *msg;
80 uint32_t *fb;
81 uint8_t *it;
82
83 struct rvid_buffer bs_buffers[NUM_BUFFERS];
84 void* bs_ptr;
85 unsigned bs_size;
86
87 struct rvid_buffer dpb;
88 bool use_legacy;
89 struct rvid_buffer ctx;
90 };
91
92 /* flush IB to the hardware */
93 static void flush(struct ruvd_decoder *dec)
94 {
95 dec->ws->cs_flush(dec->cs, RADEON_FLUSH_ASYNC, NULL, 0);
96 }
97
98 /* add a new set register command to the IB */
99 static void set_reg(struct ruvd_decoder *dec, unsigned reg, uint32_t val)
100 {
101 uint32_t *pm4 = dec->cs->buf;
102 pm4[dec->cs->cdw++] = RUVD_PKT0(reg >> 2, 0);
103 pm4[dec->cs->cdw++] = val;
104 }
105
106 /* send a command to the VCPU through the GPCOM registers */
107 static void send_cmd(struct ruvd_decoder *dec, unsigned cmd,
108 struct radeon_winsys_cs_handle* cs_buf, uint32_t off,
109 enum radeon_bo_usage usage, enum radeon_bo_domain domain)
110 {
111 int reloc_idx;
112
113 reloc_idx = dec->ws->cs_add_buffer(dec->cs, cs_buf, usage, domain,
114 RADEON_PRIO_UVD);
115 if (!dec->use_legacy) {
116 uint64_t addr;
117 addr = dec->ws->buffer_get_virtual_address(cs_buf);
118 addr = addr + off;
119 set_reg(dec, RUVD_GPCOM_VCPU_DATA0, addr);
120 set_reg(dec, RUVD_GPCOM_VCPU_DATA1, addr >> 32);
121 } else {
122 set_reg(dec, RUVD_GPCOM_VCPU_DATA0, off);
123 set_reg(dec, RUVD_GPCOM_VCPU_DATA1, reloc_idx * 4);
124 }
125 set_reg(dec, RUVD_GPCOM_VCPU_CMD, cmd << 1);
126 }
127
128 /* do the codec needs an IT buffer ?*/
129 static bool have_it(struct ruvd_decoder *dec)
130 {
131 return dec->stream_type == RUVD_CODEC_H264_PERF ||
132 dec->stream_type == RUVD_CODEC_H265;
133 }
134
135 /* map the next available message/feedback/itscaling buffer */
136 static void map_msg_fb_it_buf(struct ruvd_decoder *dec)
137 {
138 struct rvid_buffer* buf;
139 uint8_t *ptr;
140
141 /* grab the current message/feedback buffer */
142 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
143
144 /* and map it for CPU access */
145 ptr = dec->ws->buffer_map(buf->res->cs_buf, dec->cs, PIPE_TRANSFER_WRITE);
146
147 /* calc buffer offsets */
148 dec->msg = (struct ruvd_msg *)ptr;
149 dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
150 if (have_it(dec))
151 dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + FB_BUFFER_SIZE);
152 }
153
154 /* unmap and send a message command to the VCPU */
155 static void send_msg_buf(struct ruvd_decoder *dec)
156 {
157 struct rvid_buffer* buf;
158
159 /* ignore the request if message/feedback buffer isn't mapped */
160 if (!dec->msg || !dec->fb)
161 return;
162
163 /* grab the current message buffer */
164 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
165
166 /* unmap the buffer */
167 dec->ws->buffer_unmap(buf->res->cs_buf);
168 dec->msg = NULL;
169 dec->fb = NULL;
170 dec->it = NULL;
171
172 /* and send it to the hardware */
173 send_cmd(dec, RUVD_CMD_MSG_BUFFER, buf->res->cs_buf, 0,
174 RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
175 }
176
177 /* cycle to the next set of buffers */
178 static void next_buffer(struct ruvd_decoder *dec)
179 {
180 ++dec->cur_buffer;
181 dec->cur_buffer %= NUM_BUFFERS;
182 }
183
184 /* convert the profile into something UVD understands */
185 static uint32_t profile2stream_type(struct ruvd_decoder *dec, unsigned family)
186 {
187 switch (u_reduce_video_profile(dec->base.profile)) {
188 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
189 return (family >= CHIP_TONGA) ?
190 RUVD_CODEC_H264_PERF : RUVD_CODEC_H264;
191
192 case PIPE_VIDEO_FORMAT_VC1:
193 return RUVD_CODEC_VC1;
194
195 case PIPE_VIDEO_FORMAT_MPEG12:
196 return RUVD_CODEC_MPEG2;
197
198 case PIPE_VIDEO_FORMAT_MPEG4:
199 return RUVD_CODEC_MPEG4;
200
201 case PIPE_VIDEO_FORMAT_HEVC:
202 return RUVD_CODEC_H265;
203
204 default:
205 assert(0);
206 return 0;
207 }
208 }
209
210 static unsigned calc_ctx_size(struct ruvd_decoder *dec)
211 {
212 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
213 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
214
215 unsigned max_references = dec->base.max_references + 1;
216
217 if (dec->base.width * dec->base.height >= 4096*2000)
218 max_references = MAX2(max_references, 8);
219 else
220 max_references = MAX2(max_references, 17);
221
222 width = align (width, 16);
223 height = align (height, 16);
224 return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
225 }
226
227 /* calculate size of reference picture buffer */
228 static unsigned calc_dpb_size(struct ruvd_decoder *dec)
229 {
230 unsigned width_in_mb, height_in_mb, image_size, dpb_size;
231
232 // always align them to MB size for dpb calculation
233 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
234 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
235
236 // always one more for currently decoded picture
237 unsigned max_references = dec->base.max_references + 1;
238
239 // aligned size of a single frame
240 image_size = width * height;
241 image_size += image_size / 2;
242 image_size = align(image_size, 1024);
243
244 // picture width & height in 16 pixel units
245 width_in_mb = width / VL_MACROBLOCK_WIDTH;
246 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
247
248 switch (u_reduce_video_profile(dec->base.profile)) {
249 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
250 if (!dec->use_legacy) {
251 unsigned fs_in_mb = width_in_mb * height_in_mb;
252 unsigned alignment = 64, num_dpb_buffer;
253
254 if (dec->stream_type == RUVD_CODEC_H264_PERF)
255 alignment = 256;
256 switch(dec->base.level) {
257 case 30:
258 num_dpb_buffer = 8100 / fs_in_mb;
259 break;
260 case 31:
261 num_dpb_buffer = 18000 / fs_in_mb;
262 break;
263 case 32:
264 num_dpb_buffer = 20480 / fs_in_mb;
265 break;
266 case 41:
267 num_dpb_buffer = 32768 / fs_in_mb;
268 break;
269 case 42:
270 num_dpb_buffer = 34816 / fs_in_mb;
271 break;
272 case 50:
273 num_dpb_buffer = 110400 / fs_in_mb;
274 break;
275 case 51:
276 num_dpb_buffer = 184320 / fs_in_mb;
277 break;
278 default:
279 num_dpb_buffer = 184320 / fs_in_mb;
280 break;
281 }
282 num_dpb_buffer++;
283 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
284 dpb_size = image_size * max_references;
285 dpb_size += max_references * align(width_in_mb * height_in_mb * 192, alignment);
286 dpb_size += align(width_in_mb * height_in_mb * 32, alignment);
287 } else {
288 // the firmware seems to allways assume a minimum of ref frames
289 max_references = MAX2(NUM_H264_REFS, max_references);
290 // reference picture buffer
291 dpb_size = image_size * max_references;
292 // macroblock context buffer
293 dpb_size += width_in_mb * height_in_mb * max_references * 192;
294 // IT surface buffer
295 dpb_size += width_in_mb * height_in_mb * 32;
296 }
297 break;
298 }
299
300 case PIPE_VIDEO_FORMAT_HEVC:
301 if (dec->base.width * dec->base.height >= 4096*2000)
302 max_references = MAX2(max_references, 8);
303 else
304 max_references = MAX2(max_references, 17);
305
306 width = align (width, 16);
307 height = align (height, 16);
308 dpb_size = align((width * height * 3) / 2, 256) * max_references;
309 break;
310
311 case PIPE_VIDEO_FORMAT_VC1:
312 // the firmware seems to allways assume a minimum of ref frames
313 max_references = MAX2(NUM_VC1_REFS, max_references);
314
315 // reference picture buffer
316 dpb_size = image_size * max_references;
317
318 // CONTEXT_BUFFER
319 dpb_size += width_in_mb * height_in_mb * 128;
320
321 // IT surface buffer
322 dpb_size += width_in_mb * 64;
323
324 // DB surface buffer
325 dpb_size += width_in_mb * 128;
326
327 // BP
328 dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
329 break;
330
331 case PIPE_VIDEO_FORMAT_MPEG12:
332 // reference picture buffer, must be big enough for all frames
333 dpb_size = image_size * NUM_MPEG2_REFS;
334 break;
335
336 case PIPE_VIDEO_FORMAT_MPEG4:
337 // reference picture buffer
338 dpb_size = image_size * max_references;
339
340 // CM
341 dpb_size += width_in_mb * height_in_mb * 64;
342
343 // IT surface buffer
344 dpb_size += align(width_in_mb * height_in_mb * 32, 64);
345
346 dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
347 break;
348
349 default:
350 // something is missing here
351 assert(0);
352
353 // at least use a sane default value
354 dpb_size = 32 * 1024 * 1024;
355 break;
356 }
357 return dpb_size;
358 }
359
360 /* free associated data in the video buffer callback */
361 static void ruvd_destroy_associated_data(void *data)
362 {
363 /* NOOP, since we only use an intptr */
364 }
365
366 /* get h264 specific message bits */
367 static struct ruvd_h264 get_h264_msg(struct ruvd_decoder *dec, struct pipe_h264_picture_desc *pic)
368 {
369 struct ruvd_h264 result;
370
371 memset(&result, 0, sizeof(result));
372 switch (pic->base.profile) {
373 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
374 result.profile = RUVD_H264_PROFILE_BASELINE;
375 break;
376
377 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
378 result.profile = RUVD_H264_PROFILE_MAIN;
379 break;
380
381 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
382 result.profile = RUVD_H264_PROFILE_HIGH;
383 break;
384
385 default:
386 assert(0);
387 break;
388 }
389
390 result.level = dec->base.level;
391
392 result.sps_info_flags = 0;
393 result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
394 result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
395 result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
396 result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
397
398 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
399 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
400 result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
401 result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
402 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
403
404 switch (dec->base.chroma_format) {
405 case PIPE_VIDEO_CHROMA_FORMAT_400:
406 result.chroma_format = 0;
407 break;
408 case PIPE_VIDEO_CHROMA_FORMAT_420:
409 result.chroma_format = 1;
410 break;
411 case PIPE_VIDEO_CHROMA_FORMAT_422:
412 result.chroma_format = 2;
413 break;
414 case PIPE_VIDEO_CHROMA_FORMAT_444:
415 result.chroma_format = 3;
416 break;
417 }
418
419 result.pps_info_flags = 0;
420 result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
421 result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
422 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
423 result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
424 result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
425 result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
426 result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
427 result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
428
429 result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
430 result.slice_group_map_type = pic->pps->slice_group_map_type;
431 result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
432 result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
433 result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
434 result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
435
436 memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6*16);
437 memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2*64);
438
439 if (dec->stream_type == RUVD_CODEC_H264_PERF) {
440 memcpy(dec->it, result.scaling_list_4x4, 6*16);
441 memcpy((dec->it + 96), result.scaling_list_8x8, 2*64);
442 }
443
444 result.num_ref_frames = pic->num_ref_frames;
445
446 result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
447 result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
448
449 result.frame_num = pic->frame_num;
450 memcpy(result.frame_num_list, pic->frame_num_list, 4*16);
451 result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
452 result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
453 memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4*16*2);
454
455 result.decoded_pic_idx = pic->frame_num;
456
457 return result;
458 }
459
460 /* get h265 specific message bits */
461 static struct ruvd_h265 get_h265_msg(struct ruvd_decoder *dec, struct pipe_video_buffer *target,
462 struct pipe_h265_picture_desc *pic)
463 {
464 struct ruvd_h265 result;
465 unsigned i;
466
467 memset(&result, 0, sizeof(result));
468
469 result.sps_info_flags = 0;
470 result.sps_info_flags |= pic->pps->sps->scaling_list_enabled_flag << 0;
471 result.sps_info_flags |= pic->pps->sps->amp_enabled_flag << 1;
472 result.sps_info_flags |= pic->pps->sps->sample_adaptive_offset_enabled_flag << 2;
473 result.sps_info_flags |= pic->pps->sps->pcm_enabled_flag << 3;
474 result.sps_info_flags |= pic->pps->sps->pcm_loop_filter_disabled_flag << 4;
475 result.sps_info_flags |= pic->pps->sps->long_term_ref_pics_present_flag << 5;
476 result.sps_info_flags |= pic->pps->sps->sps_temporal_mvp_enabled_flag << 6;
477 result.sps_info_flags |= pic->pps->sps->strong_intra_smoothing_enabled_flag << 7;
478 result.sps_info_flags |= pic->pps->sps->separate_colour_plane_flag << 8;
479 if (((struct r600_common_screen*)dec->screen)->family == CHIP_CARRIZO)
480 result.sps_info_flags |= 1 << 9;
481 if (pic->UseRefPicList == true)
482 result.sps_info_flags |= 1 << 10;
483
484 result.chroma_format = pic->pps->sps->chroma_format_idc;
485 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
486 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
487 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
488 result.sps_max_dec_pic_buffering_minus1 = pic->pps->sps->sps_max_dec_pic_buffering_minus1;
489 result.log2_min_luma_coding_block_size_minus3 = pic->pps->sps->log2_min_luma_coding_block_size_minus3;
490 result.log2_diff_max_min_luma_coding_block_size = pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
491 result.log2_min_transform_block_size_minus2 = pic->pps->sps->log2_min_transform_block_size_minus2;
492 result.log2_diff_max_min_transform_block_size = pic->pps->sps->log2_diff_max_min_transform_block_size;
493 result.max_transform_hierarchy_depth_inter = pic->pps->sps->max_transform_hierarchy_depth_inter;
494 result.max_transform_hierarchy_depth_intra = pic->pps->sps->max_transform_hierarchy_depth_intra;
495 result.pcm_sample_bit_depth_luma_minus1 = pic->pps->sps->pcm_sample_bit_depth_luma_minus1;
496 result.pcm_sample_bit_depth_chroma_minus1 = pic->pps->sps->pcm_sample_bit_depth_chroma_minus1;
497 result.log2_min_pcm_luma_coding_block_size_minus3 = pic->pps->sps->log2_min_pcm_luma_coding_block_size_minus3;
498 result.log2_diff_max_min_pcm_luma_coding_block_size = pic->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size;
499 result.num_short_term_ref_pic_sets = pic->pps->sps->num_short_term_ref_pic_sets;
500
501 result.pps_info_flags = 0;
502 result.pps_info_flags |= pic->pps->dependent_slice_segments_enabled_flag << 0;
503 result.pps_info_flags |= pic->pps->output_flag_present_flag << 1;
504 result.pps_info_flags |= pic->pps->sign_data_hiding_enabled_flag << 2;
505 result.pps_info_flags |= pic->pps->cabac_init_present_flag << 3;
506 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 4;
507 result.pps_info_flags |= pic->pps->transform_skip_enabled_flag << 5;
508 result.pps_info_flags |= pic->pps->cu_qp_delta_enabled_flag << 6;
509 result.pps_info_flags |= pic->pps->pps_slice_chroma_qp_offsets_present_flag << 7;
510 result.pps_info_flags |= pic->pps->weighted_pred_flag << 8;
511 result.pps_info_flags |= pic->pps->weighted_bipred_flag << 9;
512 result.pps_info_flags |= pic->pps->transquant_bypass_enabled_flag << 10;
513 result.pps_info_flags |= pic->pps->tiles_enabled_flag << 11;
514 result.pps_info_flags |= pic->pps->entropy_coding_sync_enabled_flag << 12;
515 result.pps_info_flags |= pic->pps->uniform_spacing_flag << 13;
516 result.pps_info_flags |= pic->pps->loop_filter_across_tiles_enabled_flag << 14;
517 result.pps_info_flags |= pic->pps->pps_loop_filter_across_slices_enabled_flag << 15;
518 result.pps_info_flags |= pic->pps->deblocking_filter_override_enabled_flag << 16;
519 result.pps_info_flags |= pic->pps->pps_deblocking_filter_disabled_flag << 17;
520 result.pps_info_flags |= pic->pps->lists_modification_present_flag << 18;
521 result.pps_info_flags |= pic->pps->slice_segment_header_extension_present_flag << 19;
522 //result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag; ???
523
524 result.num_extra_slice_header_bits = pic->pps->num_extra_slice_header_bits;
525 result.num_long_term_ref_pic_sps = pic->pps->sps->num_long_term_ref_pics_sps;
526 result.num_ref_idx_l0_default_active_minus1 = pic->pps->num_ref_idx_l0_default_active_minus1;
527 result.num_ref_idx_l1_default_active_minus1 = pic->pps->num_ref_idx_l1_default_active_minus1;
528 result.pps_cb_qp_offset = pic->pps->pps_cb_qp_offset;
529 result.pps_cr_qp_offset = pic->pps->pps_cr_qp_offset;
530 result.pps_beta_offset_div2 = pic->pps->pps_beta_offset_div2;
531 result.pps_tc_offset_div2 = pic->pps->pps_tc_offset_div2;
532 result.diff_cu_qp_delta_depth = pic->pps->diff_cu_qp_delta_depth;
533 result.num_tile_columns_minus1 = pic->pps->num_tile_columns_minus1;
534 result.num_tile_rows_minus1 = pic->pps->num_tile_rows_minus1;
535 result.log2_parallel_merge_level_minus2 = pic->pps->log2_parallel_merge_level_minus2;
536 result.init_qp_minus26 = pic->pps->init_qp_minus26;
537
538 for (i = 0; i < 19; ++i)
539 result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
540
541 for (i = 0; i < 21; ++i)
542 result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
543
544 result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
545 result.curr_idx = pic->CurrPicOrderCntVal;
546 result.curr_poc = pic->CurrPicOrderCntVal;
547
548 vl_video_buffer_set_associated_data(target, &dec->base,
549 (void *)(uintptr_t)pic->CurrPicOrderCntVal,
550 &ruvd_destroy_associated_data);
551
552 for (i = 0; i < 16; ++i) {
553 struct pipe_video_buffer *ref = pic->ref[i];
554 uintptr_t ref_pic = 0;
555
556 result.poc_list[i] = pic->PicOrderCntVal[i];
557
558 if (ref)
559 ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
560 else
561 ref_pic = 0x7F;
562 result.ref_pic_list[i] = ref_pic;
563 }
564
565 for (i = 0; i < 8; ++i) {
566 result.ref_pic_set_st_curr_before[i] = 0xFF;
567 result.ref_pic_set_st_curr_after[i] = 0xFF;
568 result.ref_pic_set_lt_curr[i] = 0xFF;
569 }
570
571 for (i = 0; i < pic->NumPocStCurrBefore; ++i)
572 result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
573
574 for (i = 0; i < pic->NumPocStCurrAfter; ++i)
575 result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
576
577 for (i = 0; i < pic->NumPocLtCurr; ++i)
578 result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
579
580 for (i = 0; i < 6; ++i)
581 result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
582
583 for (i = 0; i < 2; ++i)
584 result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
585
586 memcpy(dec->it, pic->pps->sps->ScalingList4x4, 6 * 16);
587 memcpy(dec->it + 96, pic->pps->sps->ScalingList8x8, 6 * 64);
588 memcpy(dec->it + 480, pic->pps->sps->ScalingList16x16, 6 * 64);
589 memcpy(dec->it + 864, pic->pps->sps->ScalingList32x32, 2 * 64);
590
591 for (i = 0 ; i < 2 ; i++) {
592 for (int j = 0 ; j < 15 ; j++)
593 result.direct_reflist[i][j] = pic->RefPicList[i][j];
594 }
595
596 /* TODO
597 result.highestTid;
598 result.isNonRef;
599
600 IDRPicFlag;
601 RAPPicFlag;
602 NumPocTotalCurr;
603 NumShortTermPictureSliceHeaderBits;
604 NumLongTermPictureSliceHeaderBits;
605
606 IsLongTerm[16];
607 */
608
609 return result;
610 }
611
612 /* get vc1 specific message bits */
613 static struct ruvd_vc1 get_vc1_msg(struct pipe_vc1_picture_desc *pic)
614 {
615 struct ruvd_vc1 result;
616
617 memset(&result, 0, sizeof(result));
618
619 switch(pic->base.profile) {
620 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
621 result.profile = RUVD_VC1_PROFILE_SIMPLE;
622 result.level = 1;
623 break;
624
625 case PIPE_VIDEO_PROFILE_VC1_MAIN:
626 result.profile = RUVD_VC1_PROFILE_MAIN;
627 result.level = 2;
628 break;
629
630 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
631 result.profile = RUVD_VC1_PROFILE_ADVANCED;
632 result.level = 4;
633 break;
634
635 default:
636 assert(0);
637 }
638
639 /* fields common for all profiles */
640 result.sps_info_flags |= pic->postprocflag << 7;
641 result.sps_info_flags |= pic->pulldown << 6;
642 result.sps_info_flags |= pic->interlace << 5;
643 result.sps_info_flags |= pic->tfcntrflag << 4;
644 result.sps_info_flags |= pic->finterpflag << 3;
645 result.sps_info_flags |= pic->psf << 1;
646
647 result.pps_info_flags |= pic->range_mapy_flag << 31;
648 result.pps_info_flags |= pic->range_mapy << 28;
649 result.pps_info_flags |= pic->range_mapuv_flag << 27;
650 result.pps_info_flags |= pic->range_mapuv << 24;
651 result.pps_info_flags |= pic->multires << 21;
652 result.pps_info_flags |= pic->maxbframes << 16;
653 result.pps_info_flags |= pic->overlap << 11;
654 result.pps_info_flags |= pic->quantizer << 9;
655 result.pps_info_flags |= pic->panscan_flag << 7;
656 result.pps_info_flags |= pic->refdist_flag << 6;
657 result.pps_info_flags |= pic->vstransform << 0;
658
659 /* some fields only apply to main/advanced profile */
660 if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
661 result.pps_info_flags |= pic->syncmarker << 20;
662 result.pps_info_flags |= pic->rangered << 19;
663 result.pps_info_flags |= pic->loopfilter << 5;
664 result.pps_info_flags |= pic->fastuvmc << 4;
665 result.pps_info_flags |= pic->extended_mv << 3;
666 result.pps_info_flags |= pic->extended_dmv << 8;
667 result.pps_info_flags |= pic->dquant << 1;
668 }
669
670 result.chroma_format = 1;
671
672 #if 0
673 //(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
674 uint32_t slice_count
675 uint8_t picture_type
676 uint8_t frame_coding_mode
677 uint8_t deblockEnable
678 uint8_t pquant
679 #endif
680
681 return result;
682 }
683
684 /* extract the frame number from a referenced video buffer */
685 static uint32_t get_ref_pic_idx(struct ruvd_decoder *dec, struct pipe_video_buffer *ref)
686 {
687 uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
688 uint32_t max = MAX2(dec->frame_number, 1) - 1;
689 uintptr_t frame;
690
691 /* seems to be the most sane fallback */
692 if (!ref)
693 return max;
694
695 /* get the frame number from the associated data */
696 frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
697
698 /* limit the frame number to a valid range */
699 return MAX2(MIN2(frame, max), min);
700 }
701
702 /* get mpeg2 specific msg bits */
703 static struct ruvd_mpeg2 get_mpeg2_msg(struct ruvd_decoder *dec,
704 struct pipe_mpeg12_picture_desc *pic)
705 {
706 const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
707 struct ruvd_mpeg2 result;
708 unsigned i;
709
710 memset(&result, 0, sizeof(result));
711 result.decoded_pic_idx = dec->frame_number;
712 for (i = 0; i < 2; ++i)
713 result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
714
715 result.load_intra_quantiser_matrix = 1;
716 result.load_nonintra_quantiser_matrix = 1;
717
718 for (i = 0; i < 64; ++i) {
719 result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
720 result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
721 }
722
723 result.profile_and_level_indication = 0;
724 result.chroma_format = 0x1;
725
726 result.picture_coding_type = pic->picture_coding_type;
727 result.f_code[0][0] = pic->f_code[0][0] + 1;
728 result.f_code[0][1] = pic->f_code[0][1] + 1;
729 result.f_code[1][0] = pic->f_code[1][0] + 1;
730 result.f_code[1][1] = pic->f_code[1][1] + 1;
731 result.intra_dc_precision = pic->intra_dc_precision;
732 result.pic_structure = pic->picture_structure;
733 result.top_field_first = pic->top_field_first;
734 result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
735 result.concealment_motion_vectors = pic->concealment_motion_vectors;
736 result.q_scale_type = pic->q_scale_type;
737 result.intra_vlc_format = pic->intra_vlc_format;
738 result.alternate_scan = pic->alternate_scan;
739
740 return result;
741 }
742
743 /* get mpeg4 specific msg bits */
744 static struct ruvd_mpeg4 get_mpeg4_msg(struct ruvd_decoder *dec,
745 struct pipe_mpeg4_picture_desc *pic)
746 {
747 struct ruvd_mpeg4 result;
748 unsigned i;
749
750 memset(&result, 0, sizeof(result));
751 result.decoded_pic_idx = dec->frame_number;
752 for (i = 0; i < 2; ++i)
753 result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
754
755 result.variant_type = 0;
756 result.profile_and_level_indication = 0xF0; // ASP Level0
757
758 result.video_object_layer_verid = 0x5; // advanced simple
759 result.video_object_layer_shape = 0x0; // rectangular
760
761 result.video_object_layer_width = dec->base.width;
762 result.video_object_layer_height = dec->base.height;
763
764 result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
765
766 result.flags |= pic->short_video_header << 0;
767 //result.flags |= obmc_disable << 1;
768 result.flags |= pic->interlaced << 2;
769 result.flags |= 1 << 3; // load_intra_quant_mat
770 result.flags |= 1 << 4; // load_nonintra_quant_mat
771 result.flags |= pic->quarter_sample << 5;
772 result.flags |= 1 << 6; // complexity_estimation_disable
773 result.flags |= pic->resync_marker_disable << 7;
774 //result.flags |= data_partitioned << 8;
775 //result.flags |= reversible_vlc << 9;
776 result.flags |= 0 << 10; // newpred_enable
777 result.flags |= 0 << 11; // reduced_resolution_vop_enable
778 //result.flags |= scalability << 12;
779 //result.flags |= is_object_layer_identifier << 13;
780 //result.flags |= fixed_vop_rate << 14;
781 //result.flags |= newpred_segment_type << 15;
782
783 result.quant_type = pic->quant_type;
784
785 for (i = 0; i < 64; ++i) {
786 result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
787 result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
788 }
789
790 /*
791 int32_t trd [2]
792 int32_t trb [2]
793 uint8_t vop_coding_type
794 uint8_t vop_fcode_forward
795 uint8_t vop_fcode_backward
796 uint8_t rounding_control
797 uint8_t alternate_vertical_scan_flag
798 uint8_t top_field_first
799 */
800
801 return result;
802 }
803
804 /**
805 * destroy this video decoder
806 */
807 static void ruvd_destroy(struct pipe_video_codec *decoder)
808 {
809 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
810 unsigned i;
811
812 assert(decoder);
813
814 map_msg_fb_it_buf(dec);
815 memset(dec->msg, 0, sizeof(*dec->msg));
816 dec->msg->size = sizeof(*dec->msg);
817 dec->msg->msg_type = RUVD_MSG_DESTROY;
818 dec->msg->stream_handle = dec->stream_handle;
819 send_msg_buf(dec);
820
821 flush(dec);
822
823 dec->ws->cs_destroy(dec->cs);
824
825 for (i = 0; i < NUM_BUFFERS; ++i) {
826 rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
827 rvid_destroy_buffer(&dec->bs_buffers[i]);
828 }
829
830 rvid_destroy_buffer(&dec->dpb);
831 if (u_reduce_video_profile(dec->base.profile) == PIPE_VIDEO_FORMAT_HEVC)
832 rvid_destroy_buffer(&dec->ctx);
833
834 FREE(dec);
835 }
836
837 /**
838 * start decoding of a new frame
839 */
840 static void ruvd_begin_frame(struct pipe_video_codec *decoder,
841 struct pipe_video_buffer *target,
842 struct pipe_picture_desc *picture)
843 {
844 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
845 uintptr_t frame;
846
847 assert(decoder);
848
849 frame = ++dec->frame_number;
850 vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
851 &ruvd_destroy_associated_data);
852
853 dec->bs_size = 0;
854 dec->bs_ptr = dec->ws->buffer_map(
855 dec->bs_buffers[dec->cur_buffer].res->cs_buf,
856 dec->cs, PIPE_TRANSFER_WRITE);
857 }
858
859 /**
860 * decode a macroblock
861 */
862 static void ruvd_decode_macroblock(struct pipe_video_codec *decoder,
863 struct pipe_video_buffer *target,
864 struct pipe_picture_desc *picture,
865 const struct pipe_macroblock *macroblocks,
866 unsigned num_macroblocks)
867 {
868 /* not supported (yet) */
869 assert(0);
870 }
871
872 /**
873 * decode a bitstream
874 */
875 static void ruvd_decode_bitstream(struct pipe_video_codec *decoder,
876 struct pipe_video_buffer *target,
877 struct pipe_picture_desc *picture,
878 unsigned num_buffers,
879 const void * const *buffers,
880 const unsigned *sizes)
881 {
882 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
883 unsigned i;
884
885 assert(decoder);
886
887 if (!dec->bs_ptr)
888 return;
889
890 for (i = 0; i < num_buffers; ++i) {
891 struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
892 unsigned new_size = dec->bs_size + sizes[i];
893
894 if (new_size > buf->res->buf->size) {
895 dec->ws->buffer_unmap(buf->res->cs_buf);
896 if (!rvid_resize_buffer(dec->screen, dec->cs, buf, new_size)) {
897 RVID_ERR("Can't resize bitstream buffer!");
898 return;
899 }
900
901 dec->bs_ptr = dec->ws->buffer_map(buf->res->cs_buf, dec->cs,
902 PIPE_TRANSFER_WRITE);
903 if (!dec->bs_ptr)
904 return;
905
906 dec->bs_ptr += dec->bs_size;
907 }
908
909 memcpy(dec->bs_ptr, buffers[i], sizes[i]);
910 dec->bs_size += sizes[i];
911 dec->bs_ptr += sizes[i];
912 }
913 }
914
915 /**
916 * end decoding of the current frame
917 */
918 static void ruvd_end_frame(struct pipe_video_codec *decoder,
919 struct pipe_video_buffer *target,
920 struct pipe_picture_desc *picture)
921 {
922 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
923 struct radeon_winsys_cs_handle *dt;
924 struct rvid_buffer *msg_fb_it_buf, *bs_buf;
925 unsigned bs_size;
926
927 assert(decoder);
928
929 if (!dec->bs_ptr)
930 return;
931
932 msg_fb_it_buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
933 bs_buf = &dec->bs_buffers[dec->cur_buffer];
934
935 bs_size = align(dec->bs_size, 128);
936 memset(dec->bs_ptr, 0, bs_size - dec->bs_size);
937 dec->ws->buffer_unmap(bs_buf->res->cs_buf);
938
939 map_msg_fb_it_buf(dec);
940 dec->msg->size = sizeof(*dec->msg);
941 dec->msg->msg_type = RUVD_MSG_DECODE;
942 dec->msg->stream_handle = dec->stream_handle;
943 dec->msg->status_report_feedback_number = dec->frame_number;
944
945 dec->msg->body.decode.stream_type = dec->stream_type;
946 dec->msg->body.decode.decode_flags = 0x1;
947 dec->msg->body.decode.width_in_samples = dec->base.width;
948 dec->msg->body.decode.height_in_samples = dec->base.height;
949
950 if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
951 (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
952 dec->msg->body.decode.width_in_samples = align(dec->msg->body.decode.width_in_samples, 16) / 16;
953 dec->msg->body.decode.height_in_samples = align(dec->msg->body.decode.height_in_samples, 16) / 16;
954 }
955
956 dec->msg->body.decode.dpb_size = dec->dpb.res->buf->size;
957 dec->msg->body.decode.bsd_size = bs_size;
958 dec->msg->body.decode.db_pitch = dec->base.width;
959
960 dt = dec->set_dtb(dec->msg, (struct vl_video_buffer *)target);
961
962 switch (u_reduce_video_profile(picture->profile)) {
963 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
964 dec->msg->body.decode.codec.h264 = get_h264_msg(dec, (struct pipe_h264_picture_desc*)picture);
965 break;
966
967 case PIPE_VIDEO_FORMAT_HEVC:
968 dec->msg->body.decode.codec.h265 = get_h265_msg(dec, target, (struct pipe_h265_picture_desc*)picture);
969 break;
970
971 case PIPE_VIDEO_FORMAT_VC1:
972 dec->msg->body.decode.codec.vc1 = get_vc1_msg((struct pipe_vc1_picture_desc*)picture);
973 break;
974
975 case PIPE_VIDEO_FORMAT_MPEG12:
976 dec->msg->body.decode.codec.mpeg2 = get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc*)picture);
977 break;
978
979 case PIPE_VIDEO_FORMAT_MPEG4:
980 dec->msg->body.decode.codec.mpeg4 = get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc*)picture);
981 break;
982
983 default:
984 assert(0);
985 return;
986 }
987
988 dec->msg->body.decode.db_surf_tile_config = dec->msg->body.decode.dt_surf_tile_config;
989 dec->msg->body.decode.extension_support = 0x1;
990
991 /* set at least the feedback buffer size */
992 dec->fb[0] = FB_BUFFER_SIZE;
993
994 send_msg_buf(dec);
995
996 send_cmd(dec, RUVD_CMD_DPB_BUFFER, dec->dpb.res->cs_buf, 0,
997 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
998 if (u_reduce_video_profile(picture->profile) == PIPE_VIDEO_FORMAT_HEVC) {
999 send_cmd(dec, RUVD_CMD_CONTEXT_BUFFER, dec->ctx.res->cs_buf, 0,
1000 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1001 }
1002 send_cmd(dec, RUVD_CMD_BITSTREAM_BUFFER, bs_buf->res->cs_buf,
1003 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1004 send_cmd(dec, RUVD_CMD_DECODING_TARGET_BUFFER, dt, 0,
1005 RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1006 send_cmd(dec, RUVD_CMD_FEEDBACK_BUFFER, msg_fb_it_buf->res->cs_buf,
1007 FB_BUFFER_OFFSET, RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1008 if (have_it(dec))
1009 send_cmd(dec, RUVD_CMD_ITSCALING_TABLE_BUFFER, msg_fb_it_buf->res->cs_buf,
1010 FB_BUFFER_OFFSET + FB_BUFFER_SIZE, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1011 set_reg(dec, RUVD_ENGINE_CNTL, 1);
1012
1013 flush(dec);
1014 next_buffer(dec);
1015 }
1016
1017 /**
1018 * flush any outstanding command buffers to the hardware
1019 */
1020 static void ruvd_flush(struct pipe_video_codec *decoder)
1021 {
1022 }
1023
1024 /**
1025 * create and UVD decoder
1026 */
1027 struct pipe_video_codec *ruvd_create_decoder(struct pipe_context *context,
1028 const struct pipe_video_codec *templ,
1029 ruvd_set_dtb set_dtb)
1030 {
1031 struct radeon_winsys* ws = ((struct r600_common_context *)context)->ws;
1032 struct r600_common_context *rctx = (struct r600_common_context*)context;
1033 unsigned dpb_size;
1034 unsigned width = templ->width, height = templ->height;
1035 unsigned bs_buf_size;
1036 struct radeon_info info;
1037 struct ruvd_decoder *dec;
1038 int i;
1039
1040 ws->query_info(ws, &info);
1041
1042 switch(u_reduce_video_profile(templ->profile)) {
1043 case PIPE_VIDEO_FORMAT_MPEG12:
1044 if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM || info.family < CHIP_PALM)
1045 return vl_create_mpeg12_decoder(context, templ);
1046
1047 /* fall through */
1048 case PIPE_VIDEO_FORMAT_MPEG4:
1049 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1050 width = align(width, VL_MACROBLOCK_WIDTH);
1051 height = align(height, VL_MACROBLOCK_HEIGHT);
1052 break;
1053
1054 default:
1055 break;
1056 }
1057
1058
1059 dec = CALLOC_STRUCT(ruvd_decoder);
1060
1061 if (!dec)
1062 return NULL;
1063
1064 if (info.drm_major < 3)
1065 dec->use_legacy = TRUE;
1066
1067 dec->base = *templ;
1068 dec->base.context = context;
1069 dec->base.width = width;
1070 dec->base.height = height;
1071
1072 dec->base.destroy = ruvd_destroy;
1073 dec->base.begin_frame = ruvd_begin_frame;
1074 dec->base.decode_macroblock = ruvd_decode_macroblock;
1075 dec->base.decode_bitstream = ruvd_decode_bitstream;
1076 dec->base.end_frame = ruvd_end_frame;
1077 dec->base.flush = ruvd_flush;
1078
1079 dec->stream_type = profile2stream_type(dec, info.family);
1080 dec->set_dtb = set_dtb;
1081 dec->stream_handle = rvid_alloc_stream_handle();
1082 dec->screen = context->screen;
1083 dec->ws = ws;
1084 dec->cs = ws->cs_create(rctx->ctx, RING_UVD, NULL, NULL, NULL);
1085 if (!dec->cs) {
1086 RVID_ERR("Can't get command submission context.\n");
1087 goto error;
1088 }
1089
1090 bs_buf_size = width * height * 512 / (16 * 16);
1091 for (i = 0; i < NUM_BUFFERS; ++i) {
1092 unsigned msg_fb_it_size = FB_BUFFER_OFFSET + FB_BUFFER_SIZE;
1093 STATIC_ASSERT(sizeof(struct ruvd_msg) <= FB_BUFFER_OFFSET);
1094 if (have_it(dec))
1095 msg_fb_it_size += IT_SCALING_TABLE_SIZE;
1096 if (!rvid_create_buffer(dec->screen, &dec->msg_fb_it_buffers[i],
1097 msg_fb_it_size, PIPE_USAGE_STAGING)) {
1098 RVID_ERR("Can't allocated message buffers.\n");
1099 goto error;
1100 }
1101
1102 if (!rvid_create_buffer(dec->screen, &dec->bs_buffers[i],
1103 bs_buf_size, PIPE_USAGE_STAGING)) {
1104 RVID_ERR("Can't allocated bitstream buffers.\n");
1105 goto error;
1106 }
1107
1108 rvid_clear_buffer(context, &dec->msg_fb_it_buffers[i]);
1109 rvid_clear_buffer(context, &dec->bs_buffers[i]);
1110 }
1111
1112 dpb_size = calc_dpb_size(dec);
1113
1114 if (!rvid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1115 RVID_ERR("Can't allocated dpb.\n");
1116 goto error;
1117 }
1118
1119 rvid_clear_buffer(context, &dec->dpb);
1120
1121 if (u_reduce_video_profile(dec->base.profile) == PIPE_VIDEO_FORMAT_HEVC) {
1122 unsigned ctx_size = calc_ctx_size(dec);
1123 if (!rvid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1124 RVID_ERR("Can't allocated context buffer.\n");
1125 goto error;
1126 }
1127 rvid_clear_buffer(context, &dec->ctx);
1128 }
1129
1130 map_msg_fb_it_buf(dec);
1131 dec->msg->size = sizeof(*dec->msg);
1132 dec->msg->msg_type = RUVD_MSG_CREATE;
1133 dec->msg->stream_handle = dec->stream_handle;
1134 dec->msg->body.create.stream_type = dec->stream_type;
1135 dec->msg->body.create.width_in_samples = dec->base.width;
1136 dec->msg->body.create.height_in_samples = dec->base.height;
1137 dec->msg->body.create.dpb_size = dpb_size;
1138 send_msg_buf(dec);
1139 flush(dec);
1140 next_buffer(dec);
1141
1142 return &dec->base;
1143
1144 error:
1145 if (dec->cs) dec->ws->cs_destroy(dec->cs);
1146
1147 for (i = 0; i < NUM_BUFFERS; ++i) {
1148 rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
1149 rvid_destroy_buffer(&dec->bs_buffers[i]);
1150 }
1151
1152 rvid_destroy_buffer(&dec->dpb);
1153 if (u_reduce_video_profile(dec->base.profile) == PIPE_VIDEO_FORMAT_HEVC)
1154 rvid_destroy_buffer(&dec->ctx);
1155
1156 FREE(dec);
1157
1158 return NULL;
1159 }
1160
1161 /* calculate top/bottom offset */
1162 static unsigned texture_offset(struct radeon_surf *surface, unsigned layer)
1163 {
1164 return surface->level[0].offset +
1165 layer * surface->level[0].slice_size;
1166 }
1167
1168 /* hw encode the aspect of macro tiles */
1169 static unsigned macro_tile_aspect(unsigned macro_tile_aspect)
1170 {
1171 switch (macro_tile_aspect) {
1172 default:
1173 case 1: macro_tile_aspect = 0; break;
1174 case 2: macro_tile_aspect = 1; break;
1175 case 4: macro_tile_aspect = 2; break;
1176 case 8: macro_tile_aspect = 3; break;
1177 }
1178 return macro_tile_aspect;
1179 }
1180
1181 /* hw encode the bank width and height */
1182 static unsigned bank_wh(unsigned bankwh)
1183 {
1184 switch (bankwh) {
1185 default:
1186 case 1: bankwh = 0; break;
1187 case 2: bankwh = 1; break;
1188 case 4: bankwh = 2; break;
1189 case 8: bankwh = 3; break;
1190 }
1191 return bankwh;
1192 }
1193
1194 /**
1195 * fill decoding target field from the luma and chroma surfaces
1196 */
1197 void ruvd_set_dt_surfaces(struct ruvd_msg *msg, struct radeon_surf *luma,
1198 struct radeon_surf *chroma)
1199 {
1200 msg->body.decode.dt_pitch = luma->level[0].pitch_bytes;
1201 switch (luma->level[0].mode) {
1202 case RADEON_SURF_MODE_LINEAR_ALIGNED:
1203 msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1204 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1205 break;
1206 case RADEON_SURF_MODE_1D:
1207 msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1208 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_1D_THIN;
1209 break;
1210 case RADEON_SURF_MODE_2D:
1211 msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1212 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_2D_THIN;
1213 break;
1214 default:
1215 assert(0);
1216 break;
1217 }
1218
1219 msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0);
1220 msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0);
1221 if (msg->body.decode.dt_field_mode) {
1222 msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1);
1223 msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1);
1224 } else {
1225 msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1226 msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1227 }
1228
1229 assert(luma->bankw == chroma->bankw);
1230 assert(luma->bankh == chroma->bankh);
1231 assert(luma->mtilea == chroma->mtilea);
1232
1233 msg->body.decode.dt_surf_tile_config |= RUVD_BANK_WIDTH(bank_wh(luma->bankw));
1234 msg->body.decode.dt_surf_tile_config |= RUVD_BANK_HEIGHT(bank_wh(luma->bankh));
1235 msg->body.decode.dt_surf_tile_config |= RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma->mtilea));
1236 }