1 /**************************************************************************
3 * Copyright 2011 Advanced Micro Devices, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include <sys/types.h>
34 #include "pipe/p_video_codec.h"
36 #include "util/u_memory.h"
37 #include "util/u_video.h"
39 #include "vl/vl_defines.h"
40 #include "vl/vl_mpeg12_decoder.h"
42 #include "radeonsi/si_pipe.h"
43 #include "radeon_video.h"
44 #include "radeon_uvd.h"
48 #define NUM_MPEG2_REFS 6
49 #define NUM_H264_REFS 17
50 #define NUM_VC1_REFS 5
52 #define FB_BUFFER_OFFSET 0x1000
53 #define FB_BUFFER_SIZE 2048
54 #define FB_BUFFER_SIZE_TONGA (2048 * 64)
55 #define IT_SCALING_TABLE_SIZE 992
56 #define UVD_SESSION_CONTEXT_SIZE (128 * 1024)
58 /* UVD decoder representation */
60 struct pipe_video_codec base
;
64 unsigned stream_handle
;
66 unsigned frame_number
;
68 struct pipe_screen
*screen
;
69 struct radeon_winsys
* ws
;
70 struct radeon_winsys_cs
* cs
;
74 struct rvid_buffer msg_fb_it_buffers
[NUM_BUFFERS
];
80 struct rvid_buffer bs_buffers
[NUM_BUFFERS
];
84 struct rvid_buffer dpb
;
86 struct rvid_buffer ctx
;
87 struct rvid_buffer sessionctx
;
96 /* flush IB to the hardware */
97 static int flush(struct ruvd_decoder
*dec
, unsigned flags
)
99 return dec
->ws
->cs_flush(dec
->cs
, flags
, NULL
);
102 /* add a new set register command to the IB */
103 static void set_reg(struct ruvd_decoder
*dec
, unsigned reg
, uint32_t val
)
105 radeon_emit(dec
->cs
, RUVD_PKT0(reg
>> 2, 0));
106 radeon_emit(dec
->cs
, val
);
109 /* send a command to the VCPU through the GPCOM registers */
110 static void send_cmd(struct ruvd_decoder
*dec
, unsigned cmd
,
111 struct pb_buffer
* buf
, uint32_t off
,
112 enum radeon_bo_usage usage
, enum radeon_bo_domain domain
)
116 reloc_idx
= dec
->ws
->cs_add_buffer(dec
->cs
, buf
, usage
| RADEON_USAGE_SYNCHRONIZED
,
119 if (!dec
->use_legacy
) {
121 addr
= dec
->ws
->buffer_get_virtual_address(buf
);
123 set_reg(dec
, dec
->reg
.data0
, addr
);
124 set_reg(dec
, dec
->reg
.data1
, addr
>> 32);
126 off
+= dec
->ws
->buffer_get_reloc_offset(buf
);
127 set_reg(dec
, RUVD_GPCOM_VCPU_DATA0
, off
);
128 set_reg(dec
, RUVD_GPCOM_VCPU_DATA1
, reloc_idx
* 4);
130 set_reg(dec
, dec
->reg
.cmd
, cmd
<< 1);
133 /* do the codec needs an IT buffer ?*/
134 static bool have_it(struct ruvd_decoder
*dec
)
136 return dec
->stream_type
== RUVD_CODEC_H264_PERF
||
137 dec
->stream_type
== RUVD_CODEC_H265
;
140 /* map the next available message/feedback/itscaling buffer */
141 static void map_msg_fb_it_buf(struct ruvd_decoder
*dec
)
143 struct rvid_buffer
* buf
;
146 /* grab the current message/feedback buffer */
147 buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
149 /* and map it for CPU access */
150 ptr
= dec
->ws
->buffer_map(buf
->res
->buf
, dec
->cs
, PIPE_TRANSFER_WRITE
);
152 /* calc buffer offsets */
153 dec
->msg
= (struct ruvd_msg
*)ptr
;
154 memset(dec
->msg
, 0, sizeof(*dec
->msg
));
156 dec
->fb
= (uint32_t *)(ptr
+ FB_BUFFER_OFFSET
);
158 dec
->it
= (uint8_t *)(ptr
+ FB_BUFFER_OFFSET
+ dec
->fb_size
);
161 /* unmap and send a message command to the VCPU */
162 static void send_msg_buf(struct ruvd_decoder
*dec
)
164 struct rvid_buffer
* buf
;
166 /* ignore the request if message/feedback buffer isn't mapped */
167 if (!dec
->msg
|| !dec
->fb
)
170 /* grab the current message buffer */
171 buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
173 /* unmap the buffer */
174 dec
->ws
->buffer_unmap(buf
->res
->buf
);
180 if (dec
->sessionctx
.res
)
181 send_cmd(dec
, RUVD_CMD_SESSION_CONTEXT_BUFFER
,
182 dec
->sessionctx
.res
->buf
, 0, RADEON_USAGE_READWRITE
,
185 /* and send it to the hardware */
186 send_cmd(dec
, RUVD_CMD_MSG_BUFFER
, buf
->res
->buf
, 0,
187 RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
190 /* cycle to the next set of buffers */
191 static void next_buffer(struct ruvd_decoder
*dec
)
194 dec
->cur_buffer
%= NUM_BUFFERS
;
197 /* convert the profile into something UVD understands */
198 static uint32_t profile2stream_type(struct ruvd_decoder
*dec
, unsigned family
)
200 switch (u_reduce_video_profile(dec
->base
.profile
)) {
201 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
202 return (family
>= CHIP_TONGA
) ?
203 RUVD_CODEC_H264_PERF
: RUVD_CODEC_H264
;
205 case PIPE_VIDEO_FORMAT_VC1
:
206 return RUVD_CODEC_VC1
;
208 case PIPE_VIDEO_FORMAT_MPEG12
:
209 return RUVD_CODEC_MPEG2
;
211 case PIPE_VIDEO_FORMAT_MPEG4
:
212 return RUVD_CODEC_MPEG4
;
214 case PIPE_VIDEO_FORMAT_HEVC
:
215 return RUVD_CODEC_H265
;
217 case PIPE_VIDEO_FORMAT_JPEG
:
218 return RUVD_CODEC_MJPEG
;
226 static unsigned calc_ctx_size_h264_perf(struct ruvd_decoder
*dec
)
228 unsigned width_in_mb
, height_in_mb
, ctx_size
;
229 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
230 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
232 unsigned max_references
= dec
->base
.max_references
+ 1;
234 // picture width & height in 16 pixel units
235 width_in_mb
= width
/ VL_MACROBLOCK_WIDTH
;
236 height_in_mb
= align(height
/ VL_MACROBLOCK_HEIGHT
, 2);
238 if (!dec
->use_legacy
) {
239 unsigned fs_in_mb
= width_in_mb
* height_in_mb
;
240 unsigned num_dpb_buffer
;
241 switch(dec
->base
.level
) {
243 num_dpb_buffer
= 8100 / fs_in_mb
;
246 num_dpb_buffer
= 18000 / fs_in_mb
;
249 num_dpb_buffer
= 20480 / fs_in_mb
;
252 num_dpb_buffer
= 32768 / fs_in_mb
;
255 num_dpb_buffer
= 34816 / fs_in_mb
;
258 num_dpb_buffer
= 110400 / fs_in_mb
;
261 num_dpb_buffer
= 184320 / fs_in_mb
;
264 num_dpb_buffer
= 184320 / fs_in_mb
;
268 max_references
= MAX2(MIN2(NUM_H264_REFS
, num_dpb_buffer
), max_references
);
269 ctx_size
= max_references
* align(width_in_mb
* height_in_mb
* 192, 256);
271 // the firmware seems to always assume a minimum of ref frames
272 max_references
= MAX2(NUM_H264_REFS
, max_references
);
273 // macroblock context buffer
274 ctx_size
= align(width_in_mb
* height_in_mb
* max_references
* 192, 256);
280 static unsigned calc_ctx_size_h265_main(struct ruvd_decoder
*dec
)
282 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
283 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
285 unsigned max_references
= dec
->base
.max_references
+ 1;
287 if (dec
->base
.width
* dec
->base
.height
>= 4096*2000)
288 max_references
= MAX2(max_references
, 8);
290 max_references
= MAX2(max_references
, 17);
292 width
= align (width
, 16);
293 height
= align (height
, 16);
294 return ((width
+ 255) / 16) * ((height
+ 255) / 16) * 16 * max_references
+ 52 * 1024;
297 static unsigned calc_ctx_size_h265_main10(struct ruvd_decoder
*dec
, struct pipe_h265_picture_desc
*pic
)
299 unsigned block_size
, log2_ctb_size
, width_in_ctb
, height_in_ctb
, num_16x16_block_per_ctb
;
300 unsigned context_buffer_size_per_ctb_row
, cm_buffer_size
, max_mb_address
, db_left_tile_pxl_size
;
301 unsigned db_left_tile_ctx_size
= 4096 / 16 * (32 + 16 * 4);
303 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
304 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
305 unsigned coeff_10bit
= (pic
->pps
->sps
->bit_depth_luma_minus8
|| pic
->pps
->sps
->bit_depth_chroma_minus8
) ? 2 : 1;
307 unsigned max_references
= dec
->base
.max_references
+ 1;
309 if (dec
->base
.width
* dec
->base
.height
>= 4096*2000)
310 max_references
= MAX2(max_references
, 8);
312 max_references
= MAX2(max_references
, 17);
314 block_size
= (1 << (pic
->pps
->sps
->log2_min_luma_coding_block_size_minus3
+ 3));
315 log2_ctb_size
= block_size
+ pic
->pps
->sps
->log2_diff_max_min_luma_coding_block_size
;
317 width_in_ctb
= (width
+ ((1 << log2_ctb_size
) - 1)) >> log2_ctb_size
;
318 height_in_ctb
= (height
+ ((1 << log2_ctb_size
) - 1)) >> log2_ctb_size
;
320 num_16x16_block_per_ctb
= ((1 << log2_ctb_size
) >> 4) * ((1 << log2_ctb_size
) >> 4);
321 context_buffer_size_per_ctb_row
= align(width_in_ctb
* num_16x16_block_per_ctb
* 16, 256);
322 max_mb_address
= (unsigned) ceil(height
* 8 / 2048.0);
324 cm_buffer_size
= max_references
* context_buffer_size_per_ctb_row
* height_in_ctb
;
325 db_left_tile_pxl_size
= coeff_10bit
* (max_mb_address
* 2 * 2048 + 1024);
327 return cm_buffer_size
+ db_left_tile_ctx_size
+ db_left_tile_pxl_size
;
330 static unsigned get_db_pitch_alignment(struct ruvd_decoder
*dec
)
332 if (((struct si_screen
*)dec
->screen
)->info
.family
< CHIP_VEGA10
)
338 /* calculate size of reference picture buffer */
339 static unsigned calc_dpb_size(struct ruvd_decoder
*dec
)
341 unsigned width_in_mb
, height_in_mb
, image_size
, dpb_size
;
343 // always align them to MB size for dpb calculation
344 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
345 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
347 // always one more for currently decoded picture
348 unsigned max_references
= dec
->base
.max_references
+ 1;
350 // aligned size of a single frame
351 image_size
= align(width
, get_db_pitch_alignment(dec
)) * height
;
352 image_size
+= image_size
/ 2;
353 image_size
= align(image_size
, 1024);
355 // picture width & height in 16 pixel units
356 width_in_mb
= width
/ VL_MACROBLOCK_WIDTH
;
357 height_in_mb
= align(height
/ VL_MACROBLOCK_HEIGHT
, 2);
359 switch (u_reduce_video_profile(dec
->base
.profile
)) {
360 case PIPE_VIDEO_FORMAT_MPEG4_AVC
: {
361 if (!dec
->use_legacy
) {
362 unsigned fs_in_mb
= width_in_mb
* height_in_mb
;
363 unsigned alignment
= 64, num_dpb_buffer
;
365 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
)
367 switch(dec
->base
.level
) {
369 num_dpb_buffer
= 8100 / fs_in_mb
;
372 num_dpb_buffer
= 18000 / fs_in_mb
;
375 num_dpb_buffer
= 20480 / fs_in_mb
;
378 num_dpb_buffer
= 32768 / fs_in_mb
;
381 num_dpb_buffer
= 34816 / fs_in_mb
;
384 num_dpb_buffer
= 110400 / fs_in_mb
;
387 num_dpb_buffer
= 184320 / fs_in_mb
;
390 num_dpb_buffer
= 184320 / fs_in_mb
;
394 max_references
= MAX2(MIN2(NUM_H264_REFS
, num_dpb_buffer
), max_references
);
395 dpb_size
= image_size
* max_references
;
396 if ((dec
->stream_type
!= RUVD_CODEC_H264_PERF
) ||
397 (((struct si_screen
*)dec
->screen
)->info
.family
< CHIP_POLARIS10
)) {
398 dpb_size
+= max_references
* align(width_in_mb
* height_in_mb
* 192, alignment
);
399 dpb_size
+= align(width_in_mb
* height_in_mb
* 32, alignment
);
402 // the firmware seems to allways assume a minimum of ref frames
403 max_references
= MAX2(NUM_H264_REFS
, max_references
);
404 // reference picture buffer
405 dpb_size
= image_size
* max_references
;
406 if ((dec
->stream_type
!= RUVD_CODEC_H264_PERF
) ||
407 (((struct si_screen
*)dec
->screen
)->info
.family
< CHIP_POLARIS10
)) {
408 // macroblock context buffer
409 dpb_size
+= width_in_mb
* height_in_mb
* max_references
* 192;
411 dpb_size
+= width_in_mb
* height_in_mb
* 32;
417 case PIPE_VIDEO_FORMAT_HEVC
:
418 if (dec
->base
.width
* dec
->base
.height
>= 4096*2000)
419 max_references
= MAX2(max_references
, 8);
421 max_references
= MAX2(max_references
, 17);
423 width
= align (width
, 16);
424 height
= align (height
, 16);
425 if (dec
->base
.profile
== PIPE_VIDEO_PROFILE_HEVC_MAIN_10
)
426 dpb_size
= align((align(width
, get_db_pitch_alignment(dec
)) * height
* 9) / 4, 256) * max_references
;
428 dpb_size
= align((align(width
, get_db_pitch_alignment(dec
)) * height
* 3) / 2, 256) * max_references
;
431 case PIPE_VIDEO_FORMAT_VC1
:
432 // the firmware seems to allways assume a minimum of ref frames
433 max_references
= MAX2(NUM_VC1_REFS
, max_references
);
435 // reference picture buffer
436 dpb_size
= image_size
* max_references
;
439 dpb_size
+= width_in_mb
* height_in_mb
* 128;
442 dpb_size
+= width_in_mb
* 64;
445 dpb_size
+= width_in_mb
* 128;
448 dpb_size
+= align(MAX2(width_in_mb
, height_in_mb
) * 7 * 16, 64);
451 case PIPE_VIDEO_FORMAT_MPEG12
:
452 // reference picture buffer, must be big enough for all frames
453 dpb_size
= image_size
* NUM_MPEG2_REFS
;
456 case PIPE_VIDEO_FORMAT_MPEG4
:
457 // reference picture buffer
458 dpb_size
= image_size
* max_references
;
461 dpb_size
+= width_in_mb
* height_in_mb
* 64;
464 dpb_size
+= align(width_in_mb
* height_in_mb
* 32, 64);
466 dpb_size
= MAX2(dpb_size
, 30 * 1024 * 1024);
469 case PIPE_VIDEO_FORMAT_JPEG
:
474 // something is missing here
477 // at least use a sane default value
478 dpb_size
= 32 * 1024 * 1024;
484 /* free associated data in the video buffer callback */
485 static void ruvd_destroy_associated_data(void *data
)
487 /* NOOP, since we only use an intptr */
490 /* get h264 specific message bits */
491 static struct ruvd_h264
get_h264_msg(struct ruvd_decoder
*dec
, struct pipe_h264_picture_desc
*pic
)
493 struct ruvd_h264 result
;
495 memset(&result
, 0, sizeof(result
));
496 switch (pic
->base
.profile
) {
497 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE
:
498 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE
:
499 result
.profile
= RUVD_H264_PROFILE_BASELINE
;
502 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN
:
503 result
.profile
= RUVD_H264_PROFILE_MAIN
;
506 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
:
507 result
.profile
= RUVD_H264_PROFILE_HIGH
;
515 result
.level
= dec
->base
.level
;
517 result
.sps_info_flags
= 0;
518 result
.sps_info_flags
|= pic
->pps
->sps
->direct_8x8_inference_flag
<< 0;
519 result
.sps_info_flags
|= pic
->pps
->sps
->mb_adaptive_frame_field_flag
<< 1;
520 result
.sps_info_flags
|= pic
->pps
->sps
->frame_mbs_only_flag
<< 2;
521 result
.sps_info_flags
|= pic
->pps
->sps
->delta_pic_order_always_zero_flag
<< 3;
523 result
.bit_depth_luma_minus8
= pic
->pps
->sps
->bit_depth_luma_minus8
;
524 result
.bit_depth_chroma_minus8
= pic
->pps
->sps
->bit_depth_chroma_minus8
;
525 result
.log2_max_frame_num_minus4
= pic
->pps
->sps
->log2_max_frame_num_minus4
;
526 result
.pic_order_cnt_type
= pic
->pps
->sps
->pic_order_cnt_type
;
527 result
.log2_max_pic_order_cnt_lsb_minus4
= pic
->pps
->sps
->log2_max_pic_order_cnt_lsb_minus4
;
529 switch (dec
->base
.chroma_format
) {
530 case PIPE_VIDEO_CHROMA_FORMAT_NONE
:
533 case PIPE_VIDEO_CHROMA_FORMAT_400
:
534 result
.chroma_format
= 0;
536 case PIPE_VIDEO_CHROMA_FORMAT_420
:
537 result
.chroma_format
= 1;
539 case PIPE_VIDEO_CHROMA_FORMAT_422
:
540 result
.chroma_format
= 2;
542 case PIPE_VIDEO_CHROMA_FORMAT_444
:
543 result
.chroma_format
= 3;
547 result
.pps_info_flags
= 0;
548 result
.pps_info_flags
|= pic
->pps
->transform_8x8_mode_flag
<< 0;
549 result
.pps_info_flags
|= pic
->pps
->redundant_pic_cnt_present_flag
<< 1;
550 result
.pps_info_flags
|= pic
->pps
->constrained_intra_pred_flag
<< 2;
551 result
.pps_info_flags
|= pic
->pps
->deblocking_filter_control_present_flag
<< 3;
552 result
.pps_info_flags
|= pic
->pps
->weighted_bipred_idc
<< 4;
553 result
.pps_info_flags
|= pic
->pps
->weighted_pred_flag
<< 6;
554 result
.pps_info_flags
|= pic
->pps
->bottom_field_pic_order_in_frame_present_flag
<< 7;
555 result
.pps_info_flags
|= pic
->pps
->entropy_coding_mode_flag
<< 8;
557 result
.num_slice_groups_minus1
= pic
->pps
->num_slice_groups_minus1
;
558 result
.slice_group_map_type
= pic
->pps
->slice_group_map_type
;
559 result
.slice_group_change_rate_minus1
= pic
->pps
->slice_group_change_rate_minus1
;
560 result
.pic_init_qp_minus26
= pic
->pps
->pic_init_qp_minus26
;
561 result
.chroma_qp_index_offset
= pic
->pps
->chroma_qp_index_offset
;
562 result
.second_chroma_qp_index_offset
= pic
->pps
->second_chroma_qp_index_offset
;
564 memcpy(result
.scaling_list_4x4
, pic
->pps
->ScalingList4x4
, 6*16);
565 memcpy(result
.scaling_list_8x8
, pic
->pps
->ScalingList8x8
, 2*64);
567 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
) {
568 memcpy(dec
->it
, result
.scaling_list_4x4
, 6*16);
569 memcpy((dec
->it
+ 96), result
.scaling_list_8x8
, 2*64);
572 result
.num_ref_frames
= pic
->num_ref_frames
;
574 result
.num_ref_idx_l0_active_minus1
= pic
->num_ref_idx_l0_active_minus1
;
575 result
.num_ref_idx_l1_active_minus1
= pic
->num_ref_idx_l1_active_minus1
;
577 result
.frame_num
= pic
->frame_num
;
578 memcpy(result
.frame_num_list
, pic
->frame_num_list
, 4*16);
579 result
.curr_field_order_cnt_list
[0] = pic
->field_order_cnt
[0];
580 result
.curr_field_order_cnt_list
[1] = pic
->field_order_cnt
[1];
581 memcpy(result
.field_order_cnt_list
, pic
->field_order_cnt_list
, 4*16*2);
583 result
.decoded_pic_idx
= pic
->frame_num
;
588 /* get h265 specific message bits */
589 static struct ruvd_h265
get_h265_msg(struct ruvd_decoder
*dec
, struct pipe_video_buffer
*target
,
590 struct pipe_h265_picture_desc
*pic
)
592 struct ruvd_h265 result
;
595 memset(&result
, 0, sizeof(result
));
597 result
.sps_info_flags
= 0;
598 result
.sps_info_flags
|= pic
->pps
->sps
->scaling_list_enabled_flag
<< 0;
599 result
.sps_info_flags
|= pic
->pps
->sps
->amp_enabled_flag
<< 1;
600 result
.sps_info_flags
|= pic
->pps
->sps
->sample_adaptive_offset_enabled_flag
<< 2;
601 result
.sps_info_flags
|= pic
->pps
->sps
->pcm_enabled_flag
<< 3;
602 result
.sps_info_flags
|= pic
->pps
->sps
->pcm_loop_filter_disabled_flag
<< 4;
603 result
.sps_info_flags
|= pic
->pps
->sps
->long_term_ref_pics_present_flag
<< 5;
604 result
.sps_info_flags
|= pic
->pps
->sps
->sps_temporal_mvp_enabled_flag
<< 6;
605 result
.sps_info_flags
|= pic
->pps
->sps
->strong_intra_smoothing_enabled_flag
<< 7;
606 result
.sps_info_flags
|= pic
->pps
->sps
->separate_colour_plane_flag
<< 8;
607 if (((struct si_screen
*)dec
->screen
)->info
.family
== CHIP_CARRIZO
)
608 result
.sps_info_flags
|= 1 << 9;
609 if (pic
->UseRefPicList
== true)
610 result
.sps_info_flags
|= 1 << 10;
612 result
.chroma_format
= pic
->pps
->sps
->chroma_format_idc
;
613 result
.bit_depth_luma_minus8
= pic
->pps
->sps
->bit_depth_luma_minus8
;
614 result
.bit_depth_chroma_minus8
= pic
->pps
->sps
->bit_depth_chroma_minus8
;
615 result
.log2_max_pic_order_cnt_lsb_minus4
= pic
->pps
->sps
->log2_max_pic_order_cnt_lsb_minus4
;
616 result
.sps_max_dec_pic_buffering_minus1
= pic
->pps
->sps
->sps_max_dec_pic_buffering_minus1
;
617 result
.log2_min_luma_coding_block_size_minus3
= pic
->pps
->sps
->log2_min_luma_coding_block_size_minus3
;
618 result
.log2_diff_max_min_luma_coding_block_size
= pic
->pps
->sps
->log2_diff_max_min_luma_coding_block_size
;
619 result
.log2_min_transform_block_size_minus2
= pic
->pps
->sps
->log2_min_transform_block_size_minus2
;
620 result
.log2_diff_max_min_transform_block_size
= pic
->pps
->sps
->log2_diff_max_min_transform_block_size
;
621 result
.max_transform_hierarchy_depth_inter
= pic
->pps
->sps
->max_transform_hierarchy_depth_inter
;
622 result
.max_transform_hierarchy_depth_intra
= pic
->pps
->sps
->max_transform_hierarchy_depth_intra
;
623 result
.pcm_sample_bit_depth_luma_minus1
= pic
->pps
->sps
->pcm_sample_bit_depth_luma_minus1
;
624 result
.pcm_sample_bit_depth_chroma_minus1
= pic
->pps
->sps
->pcm_sample_bit_depth_chroma_minus1
;
625 result
.log2_min_pcm_luma_coding_block_size_minus3
= pic
->pps
->sps
->log2_min_pcm_luma_coding_block_size_minus3
;
626 result
.log2_diff_max_min_pcm_luma_coding_block_size
= pic
->pps
->sps
->log2_diff_max_min_pcm_luma_coding_block_size
;
627 result
.num_short_term_ref_pic_sets
= pic
->pps
->sps
->num_short_term_ref_pic_sets
;
629 result
.pps_info_flags
= 0;
630 result
.pps_info_flags
|= pic
->pps
->dependent_slice_segments_enabled_flag
<< 0;
631 result
.pps_info_flags
|= pic
->pps
->output_flag_present_flag
<< 1;
632 result
.pps_info_flags
|= pic
->pps
->sign_data_hiding_enabled_flag
<< 2;
633 result
.pps_info_flags
|= pic
->pps
->cabac_init_present_flag
<< 3;
634 result
.pps_info_flags
|= pic
->pps
->constrained_intra_pred_flag
<< 4;
635 result
.pps_info_flags
|= pic
->pps
->transform_skip_enabled_flag
<< 5;
636 result
.pps_info_flags
|= pic
->pps
->cu_qp_delta_enabled_flag
<< 6;
637 result
.pps_info_flags
|= pic
->pps
->pps_slice_chroma_qp_offsets_present_flag
<< 7;
638 result
.pps_info_flags
|= pic
->pps
->weighted_pred_flag
<< 8;
639 result
.pps_info_flags
|= pic
->pps
->weighted_bipred_flag
<< 9;
640 result
.pps_info_flags
|= pic
->pps
->transquant_bypass_enabled_flag
<< 10;
641 result
.pps_info_flags
|= pic
->pps
->tiles_enabled_flag
<< 11;
642 result
.pps_info_flags
|= pic
->pps
->entropy_coding_sync_enabled_flag
<< 12;
643 result
.pps_info_flags
|= pic
->pps
->uniform_spacing_flag
<< 13;
644 result
.pps_info_flags
|= pic
->pps
->loop_filter_across_tiles_enabled_flag
<< 14;
645 result
.pps_info_flags
|= pic
->pps
->pps_loop_filter_across_slices_enabled_flag
<< 15;
646 result
.pps_info_flags
|= pic
->pps
->deblocking_filter_override_enabled_flag
<< 16;
647 result
.pps_info_flags
|= pic
->pps
->pps_deblocking_filter_disabled_flag
<< 17;
648 result
.pps_info_flags
|= pic
->pps
->lists_modification_present_flag
<< 18;
649 result
.pps_info_flags
|= pic
->pps
->slice_segment_header_extension_present_flag
<< 19;
650 //result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag; ???
652 result
.num_extra_slice_header_bits
= pic
->pps
->num_extra_slice_header_bits
;
653 result
.num_long_term_ref_pic_sps
= pic
->pps
->sps
->num_long_term_ref_pics_sps
;
654 result
.num_ref_idx_l0_default_active_minus1
= pic
->pps
->num_ref_idx_l0_default_active_minus1
;
655 result
.num_ref_idx_l1_default_active_minus1
= pic
->pps
->num_ref_idx_l1_default_active_minus1
;
656 result
.pps_cb_qp_offset
= pic
->pps
->pps_cb_qp_offset
;
657 result
.pps_cr_qp_offset
= pic
->pps
->pps_cr_qp_offset
;
658 result
.pps_beta_offset_div2
= pic
->pps
->pps_beta_offset_div2
;
659 result
.pps_tc_offset_div2
= pic
->pps
->pps_tc_offset_div2
;
660 result
.diff_cu_qp_delta_depth
= pic
->pps
->diff_cu_qp_delta_depth
;
661 result
.num_tile_columns_minus1
= pic
->pps
->num_tile_columns_minus1
;
662 result
.num_tile_rows_minus1
= pic
->pps
->num_tile_rows_minus1
;
663 result
.log2_parallel_merge_level_minus2
= pic
->pps
->log2_parallel_merge_level_minus2
;
664 result
.init_qp_minus26
= pic
->pps
->init_qp_minus26
;
666 for (i
= 0; i
< 19; ++i
)
667 result
.column_width_minus1
[i
] = pic
->pps
->column_width_minus1
[i
];
669 for (i
= 0; i
< 21; ++i
)
670 result
.row_height_minus1
[i
] = pic
->pps
->row_height_minus1
[i
];
672 result
.num_delta_pocs_ref_rps_idx
= pic
->NumDeltaPocsOfRefRpsIdx
;
673 result
.curr_idx
= pic
->CurrPicOrderCntVal
;
674 result
.curr_poc
= pic
->CurrPicOrderCntVal
;
676 vl_video_buffer_set_associated_data(target
, &dec
->base
,
677 (void *)(uintptr_t)pic
->CurrPicOrderCntVal
,
678 &ruvd_destroy_associated_data
);
680 for (i
= 0; i
< 16; ++i
) {
681 struct pipe_video_buffer
*ref
= pic
->ref
[i
];
682 uintptr_t ref_pic
= 0;
684 result
.poc_list
[i
] = pic
->PicOrderCntVal
[i
];
687 ref_pic
= (uintptr_t)vl_video_buffer_get_associated_data(ref
, &dec
->base
);
690 result
.ref_pic_list
[i
] = ref_pic
;
693 for (i
= 0; i
< 8; ++i
) {
694 result
.ref_pic_set_st_curr_before
[i
] = 0xFF;
695 result
.ref_pic_set_st_curr_after
[i
] = 0xFF;
696 result
.ref_pic_set_lt_curr
[i
] = 0xFF;
699 for (i
= 0; i
< pic
->NumPocStCurrBefore
; ++i
)
700 result
.ref_pic_set_st_curr_before
[i
] = pic
->RefPicSetStCurrBefore
[i
];
702 for (i
= 0; i
< pic
->NumPocStCurrAfter
; ++i
)
703 result
.ref_pic_set_st_curr_after
[i
] = pic
->RefPicSetStCurrAfter
[i
];
705 for (i
= 0; i
< pic
->NumPocLtCurr
; ++i
)
706 result
.ref_pic_set_lt_curr
[i
] = pic
->RefPicSetLtCurr
[i
];
708 for (i
= 0; i
< 6; ++i
)
709 result
.ucScalingListDCCoefSizeID2
[i
] = pic
->pps
->sps
->ScalingListDCCoeff16x16
[i
];
711 for (i
= 0; i
< 2; ++i
)
712 result
.ucScalingListDCCoefSizeID3
[i
] = pic
->pps
->sps
->ScalingListDCCoeff32x32
[i
];
714 memcpy(dec
->it
, pic
->pps
->sps
->ScalingList4x4
, 6 * 16);
715 memcpy(dec
->it
+ 96, pic
->pps
->sps
->ScalingList8x8
, 6 * 64);
716 memcpy(dec
->it
+ 480, pic
->pps
->sps
->ScalingList16x16
, 6 * 64);
717 memcpy(dec
->it
+ 864, pic
->pps
->sps
->ScalingList32x32
, 2 * 64);
719 for (i
= 0 ; i
< 2 ; i
++) {
720 for (int j
= 0 ; j
< 15 ; j
++)
721 result
.direct_reflist
[i
][j
] = pic
->RefPicList
[i
][j
];
724 if (pic
->base
.profile
== PIPE_VIDEO_PROFILE_HEVC_MAIN_10
) {
725 if (target
->buffer_format
== PIPE_FORMAT_P016
) {
726 result
.p010_mode
= 1;
729 result
.luma_10to8
= 5;
730 result
.chroma_10to8
= 5;
731 result
.sclr_luma10to8
= 4;
732 result
.sclr_chroma10to8
= 4;
743 NumShortTermPictureSliceHeaderBits;
744 NumLongTermPictureSliceHeaderBits;
752 /* get vc1 specific message bits */
753 static struct ruvd_vc1
get_vc1_msg(struct pipe_vc1_picture_desc
*pic
)
755 struct ruvd_vc1 result
;
757 memset(&result
, 0, sizeof(result
));
759 switch(pic
->base
.profile
) {
760 case PIPE_VIDEO_PROFILE_VC1_SIMPLE
:
761 result
.profile
= RUVD_VC1_PROFILE_SIMPLE
;
765 case PIPE_VIDEO_PROFILE_VC1_MAIN
:
766 result
.profile
= RUVD_VC1_PROFILE_MAIN
;
770 case PIPE_VIDEO_PROFILE_VC1_ADVANCED
:
771 result
.profile
= RUVD_VC1_PROFILE_ADVANCED
;
779 /* fields common for all profiles */
780 result
.sps_info_flags
|= pic
->postprocflag
<< 7;
781 result
.sps_info_flags
|= pic
->pulldown
<< 6;
782 result
.sps_info_flags
|= pic
->interlace
<< 5;
783 result
.sps_info_flags
|= pic
->tfcntrflag
<< 4;
784 result
.sps_info_flags
|= pic
->finterpflag
<< 3;
785 result
.sps_info_flags
|= pic
->psf
<< 1;
787 result
.pps_info_flags
|= pic
->range_mapy_flag
<< 31;
788 result
.pps_info_flags
|= pic
->range_mapy
<< 28;
789 result
.pps_info_flags
|= pic
->range_mapuv_flag
<< 27;
790 result
.pps_info_flags
|= pic
->range_mapuv
<< 24;
791 result
.pps_info_flags
|= pic
->multires
<< 21;
792 result
.pps_info_flags
|= pic
->maxbframes
<< 16;
793 result
.pps_info_flags
|= pic
->overlap
<< 11;
794 result
.pps_info_flags
|= pic
->quantizer
<< 9;
795 result
.pps_info_flags
|= pic
->panscan_flag
<< 7;
796 result
.pps_info_flags
|= pic
->refdist_flag
<< 6;
797 result
.pps_info_flags
|= pic
->vstransform
<< 0;
799 /* some fields only apply to main/advanced profile */
800 if (pic
->base
.profile
!= PIPE_VIDEO_PROFILE_VC1_SIMPLE
) {
801 result
.pps_info_flags
|= pic
->syncmarker
<< 20;
802 result
.pps_info_flags
|= pic
->rangered
<< 19;
803 result
.pps_info_flags
|= pic
->loopfilter
<< 5;
804 result
.pps_info_flags
|= pic
->fastuvmc
<< 4;
805 result
.pps_info_flags
|= pic
->extended_mv
<< 3;
806 result
.pps_info_flags
|= pic
->extended_dmv
<< 8;
807 result
.pps_info_flags
|= pic
->dquant
<< 1;
810 result
.chroma_format
= 1;
813 //(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
816 uint8_t frame_coding_mode
817 uint8_t deblockEnable
824 /* extract the frame number from a referenced video buffer */
825 static uint32_t get_ref_pic_idx(struct ruvd_decoder
*dec
, struct pipe_video_buffer
*ref
)
827 uint32_t min
= MAX2(dec
->frame_number
, NUM_MPEG2_REFS
) - NUM_MPEG2_REFS
;
828 uint32_t max
= MAX2(dec
->frame_number
, 1) - 1;
831 /* seems to be the most sane fallback */
835 /* get the frame number from the associated data */
836 frame
= (uintptr_t)vl_video_buffer_get_associated_data(ref
, &dec
->base
);
838 /* limit the frame number to a valid range */
839 return MAX2(MIN2(frame
, max
), min
);
842 /* get mpeg2 specific msg bits */
843 static struct ruvd_mpeg2
get_mpeg2_msg(struct ruvd_decoder
*dec
,
844 struct pipe_mpeg12_picture_desc
*pic
)
846 const int *zscan
= pic
->alternate_scan
? vl_zscan_alternate
: vl_zscan_normal
;
847 struct ruvd_mpeg2 result
;
850 memset(&result
, 0, sizeof(result
));
851 result
.decoded_pic_idx
= dec
->frame_number
;
852 for (i
= 0; i
< 2; ++i
)
853 result
.ref_pic_idx
[i
] = get_ref_pic_idx(dec
, pic
->ref
[i
]);
855 result
.load_intra_quantiser_matrix
= 1;
856 result
.load_nonintra_quantiser_matrix
= 1;
858 for (i
= 0; i
< 64; ++i
) {
859 result
.intra_quantiser_matrix
[i
] = pic
->intra_matrix
[zscan
[i
]];
860 result
.nonintra_quantiser_matrix
[i
] = pic
->non_intra_matrix
[zscan
[i
]];
863 result
.profile_and_level_indication
= 0;
864 result
.chroma_format
= 0x1;
866 result
.picture_coding_type
= pic
->picture_coding_type
;
867 result
.f_code
[0][0] = pic
->f_code
[0][0] + 1;
868 result
.f_code
[0][1] = pic
->f_code
[0][1] + 1;
869 result
.f_code
[1][0] = pic
->f_code
[1][0] + 1;
870 result
.f_code
[1][1] = pic
->f_code
[1][1] + 1;
871 result
.intra_dc_precision
= pic
->intra_dc_precision
;
872 result
.pic_structure
= pic
->picture_structure
;
873 result
.top_field_first
= pic
->top_field_first
;
874 result
.frame_pred_frame_dct
= pic
->frame_pred_frame_dct
;
875 result
.concealment_motion_vectors
= pic
->concealment_motion_vectors
;
876 result
.q_scale_type
= pic
->q_scale_type
;
877 result
.intra_vlc_format
= pic
->intra_vlc_format
;
878 result
.alternate_scan
= pic
->alternate_scan
;
883 /* get mpeg4 specific msg bits */
884 static struct ruvd_mpeg4
get_mpeg4_msg(struct ruvd_decoder
*dec
,
885 struct pipe_mpeg4_picture_desc
*pic
)
887 struct ruvd_mpeg4 result
;
890 memset(&result
, 0, sizeof(result
));
891 result
.decoded_pic_idx
= dec
->frame_number
;
892 for (i
= 0; i
< 2; ++i
)
893 result
.ref_pic_idx
[i
] = get_ref_pic_idx(dec
, pic
->ref
[i
]);
895 result
.variant_type
= 0;
896 result
.profile_and_level_indication
= 0xF0; // ASP Level0
898 result
.video_object_layer_verid
= 0x5; // advanced simple
899 result
.video_object_layer_shape
= 0x0; // rectangular
901 result
.video_object_layer_width
= dec
->base
.width
;
902 result
.video_object_layer_height
= dec
->base
.height
;
904 result
.vop_time_increment_resolution
= pic
->vop_time_increment_resolution
;
906 result
.flags
|= pic
->short_video_header
<< 0;
907 //result.flags |= obmc_disable << 1;
908 result
.flags
|= pic
->interlaced
<< 2;
909 result
.flags
|= 1 << 3; // load_intra_quant_mat
910 result
.flags
|= 1 << 4; // load_nonintra_quant_mat
911 result
.flags
|= pic
->quarter_sample
<< 5;
912 result
.flags
|= 1 << 6; // complexity_estimation_disable
913 result
.flags
|= pic
->resync_marker_disable
<< 7;
914 //result.flags |= data_partitioned << 8;
915 //result.flags |= reversible_vlc << 9;
916 result
.flags
|= 0 << 10; // newpred_enable
917 result
.flags
|= 0 << 11; // reduced_resolution_vop_enable
918 //result.flags |= scalability << 12;
919 //result.flags |= is_object_layer_identifier << 13;
920 //result.flags |= fixed_vop_rate << 14;
921 //result.flags |= newpred_segment_type << 15;
923 result
.quant_type
= pic
->quant_type
;
925 for (i
= 0; i
< 64; ++i
) {
926 result
.intra_quant_mat
[i
] = pic
->intra_matrix
[vl_zscan_normal
[i
]];
927 result
.nonintra_quant_mat
[i
] = pic
->non_intra_matrix
[vl_zscan_normal
[i
]];
933 uint8_t vop_coding_type
934 uint8_t vop_fcode_forward
935 uint8_t vop_fcode_backward
936 uint8_t rounding_control
937 uint8_t alternate_vertical_scan_flag
938 uint8_t top_field_first
944 static void get_mjpeg_slice_header(struct ruvd_decoder
*dec
, struct pipe_mjpeg_picture_desc
*pic
)
946 int size
= 0, saved_size
, len_pos
, i
;
948 uint8_t *buf
= dec
->bs_ptr
;
961 for (i
= 0; i
< 4; ++i
) {
962 if (pic
->quantization_table
.load_quantiser_table
[i
] == 0)
966 memcpy((buf
+ size
), &pic
->quantization_table
.quantiser_table
[i
], 64);
970 bs
= (uint16_t*)&buf
[len_pos
];
971 *bs
= util_bswap16(size
- 4);
982 for (i
= 0; i
< 2; ++i
) {
983 if (pic
->huffman_table
.load_huffman_table
[i
] == 0)
986 buf
[size
++] = 0x00 | i
;
987 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].num_dc_codes
, 16);
989 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].dc_values
, 12);
993 for (i
= 0; i
< 2; ++i
) {
994 if (pic
->huffman_table
.load_huffman_table
[i
] == 0)
997 buf
[size
++] = 0x10 | i
;
998 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].num_ac_codes
, 16);
1000 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].ac_values
, 162);
1004 bs
= (uint16_t*)&buf
[len_pos
];
1005 *bs
= util_bswap16(size
- saved_size
- 2);
1010 if (pic
->slice_parameter
.restart_interval
) {
1015 bs
= (uint16_t*)&buf
[size
++];
1016 *bs
= util_bswap16(pic
->slice_parameter
.restart_interval
);
1017 saved_size
= ++size
;
1029 bs
= (uint16_t*)&buf
[size
++];
1030 *bs
= util_bswap16(pic
->picture_parameter
.picture_height
);
1033 bs
= (uint16_t*)&buf
[size
++];
1034 *bs
= util_bswap16(pic
->picture_parameter
.picture_width
);
1037 buf
[size
++] = pic
->picture_parameter
.num_components
;
1039 for (i
= 0; i
< pic
->picture_parameter
.num_components
; ++i
) {
1040 buf
[size
++] = pic
->picture_parameter
.components
[i
].component_id
;
1041 buf
[size
++] = pic
->picture_parameter
.components
[i
].h_sampling_factor
<< 4 |
1042 pic
->picture_parameter
.components
[i
].v_sampling_factor
;
1043 buf
[size
++] = pic
->picture_parameter
.components
[i
].quantiser_table_selector
;
1046 bs
= (uint16_t*)&buf
[len_pos
];
1047 *bs
= util_bswap16(size
- saved_size
- 2);
1058 buf
[size
++] = pic
->slice_parameter
.num_components
;
1060 for (i
= 0; i
< pic
->slice_parameter
.num_components
; ++i
) {
1061 buf
[size
++] = pic
->slice_parameter
.components
[i
].component_selector
;
1062 buf
[size
++] = pic
->slice_parameter
.components
[i
].dc_table_selector
<< 4 |
1063 pic
->slice_parameter
.components
[i
].ac_table_selector
;
1070 bs
= (uint16_t*)&buf
[len_pos
];
1071 *bs
= util_bswap16(size
- saved_size
- 2);
1073 dec
->bs_ptr
+= size
;
1074 dec
->bs_size
+= size
;
1078 * destroy this video decoder
1080 static void ruvd_destroy(struct pipe_video_codec
*decoder
)
1082 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1087 map_msg_fb_it_buf(dec
);
1088 dec
->msg
->size
= sizeof(*dec
->msg
);
1089 dec
->msg
->msg_type
= RUVD_MSG_DESTROY
;
1090 dec
->msg
->stream_handle
= dec
->stream_handle
;
1095 dec
->ws
->cs_destroy(dec
->cs
);
1097 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1098 si_vid_destroy_buffer(&dec
->msg_fb_it_buffers
[i
]);
1099 si_vid_destroy_buffer(&dec
->bs_buffers
[i
]);
1102 si_vid_destroy_buffer(&dec
->dpb
);
1103 si_vid_destroy_buffer(&dec
->ctx
);
1104 si_vid_destroy_buffer(&dec
->sessionctx
);
1110 * start decoding of a new frame
1112 static void ruvd_begin_frame(struct pipe_video_codec
*decoder
,
1113 struct pipe_video_buffer
*target
,
1114 struct pipe_picture_desc
*picture
)
1116 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1121 frame
= ++dec
->frame_number
;
1122 vl_video_buffer_set_associated_data(target
, decoder
, (void *)frame
,
1123 &ruvd_destroy_associated_data
);
1126 dec
->bs_ptr
= dec
->ws
->buffer_map(
1127 dec
->bs_buffers
[dec
->cur_buffer
].res
->buf
,
1128 dec
->cs
, PIPE_TRANSFER_WRITE
);
1132 * decode a macroblock
1134 static void ruvd_decode_macroblock(struct pipe_video_codec
*decoder
,
1135 struct pipe_video_buffer
*target
,
1136 struct pipe_picture_desc
*picture
,
1137 const struct pipe_macroblock
*macroblocks
,
1138 unsigned num_macroblocks
)
1140 /* not supported (yet) */
1145 * decode a bitstream
1147 static void ruvd_decode_bitstream(struct pipe_video_codec
*decoder
,
1148 struct pipe_video_buffer
*target
,
1149 struct pipe_picture_desc
*picture
,
1150 unsigned num_buffers
,
1151 const void * const *buffers
,
1152 const unsigned *sizes
)
1154 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1155 enum pipe_video_format format
= u_reduce_video_profile(picture
->profile
);
1163 if (format
== PIPE_VIDEO_FORMAT_JPEG
)
1164 get_mjpeg_slice_header(dec
, (struct pipe_mjpeg_picture_desc
*)picture
);
1166 for (i
= 0; i
< num_buffers
; ++i
) {
1167 struct rvid_buffer
*buf
= &dec
->bs_buffers
[dec
->cur_buffer
];
1168 unsigned new_size
= dec
->bs_size
+ sizes
[i
];
1170 if (format
== PIPE_VIDEO_FORMAT_JPEG
)
1171 new_size
+= 2; /* save for EOI */
1173 if (new_size
> buf
->res
->buf
->size
) {
1174 dec
->ws
->buffer_unmap(buf
->res
->buf
);
1175 if (!si_vid_resize_buffer(dec
->screen
, dec
->cs
, buf
, new_size
)) {
1176 RVID_ERR("Can't resize bitstream buffer!");
1180 dec
->bs_ptr
= dec
->ws
->buffer_map(buf
->res
->buf
, dec
->cs
,
1181 PIPE_TRANSFER_WRITE
);
1185 dec
->bs_ptr
+= dec
->bs_size
;
1188 memcpy(dec
->bs_ptr
, buffers
[i
], sizes
[i
]);
1189 dec
->bs_size
+= sizes
[i
];
1190 dec
->bs_ptr
+= sizes
[i
];
1193 if (format
== PIPE_VIDEO_FORMAT_JPEG
) {
1194 ((uint8_t *)dec
->bs_ptr
)[0] = 0xff; /* EOI */
1195 ((uint8_t *)dec
->bs_ptr
)[1] = 0xd9;
1202 * end decoding of the current frame
1204 static void ruvd_end_frame(struct pipe_video_codec
*decoder
,
1205 struct pipe_video_buffer
*target
,
1206 struct pipe_picture_desc
*picture
)
1208 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1209 struct pb_buffer
*dt
;
1210 struct rvid_buffer
*msg_fb_it_buf
, *bs_buf
;
1218 msg_fb_it_buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
1219 bs_buf
= &dec
->bs_buffers
[dec
->cur_buffer
];
1221 bs_size
= align(dec
->bs_size
, 128);
1222 memset(dec
->bs_ptr
, 0, bs_size
- dec
->bs_size
);
1223 dec
->ws
->buffer_unmap(bs_buf
->res
->buf
);
1225 map_msg_fb_it_buf(dec
);
1226 dec
->msg
->size
= sizeof(*dec
->msg
);
1227 dec
->msg
->msg_type
= RUVD_MSG_DECODE
;
1228 dec
->msg
->stream_handle
= dec
->stream_handle
;
1229 dec
->msg
->status_report_feedback_number
= dec
->frame_number
;
1231 dec
->msg
->body
.decode
.stream_type
= dec
->stream_type
;
1232 dec
->msg
->body
.decode
.decode_flags
= 0x1;
1233 dec
->msg
->body
.decode
.width_in_samples
= dec
->base
.width
;
1234 dec
->msg
->body
.decode
.height_in_samples
= dec
->base
.height
;
1236 if ((picture
->profile
== PIPE_VIDEO_PROFILE_VC1_SIMPLE
) ||
1237 (picture
->profile
== PIPE_VIDEO_PROFILE_VC1_MAIN
)) {
1238 dec
->msg
->body
.decode
.width_in_samples
= align(dec
->msg
->body
.decode
.width_in_samples
, 16) / 16;
1239 dec
->msg
->body
.decode
.height_in_samples
= align(dec
->msg
->body
.decode
.height_in_samples
, 16) / 16;
1243 dec
->msg
->body
.decode
.dpb_size
= dec
->dpb
.res
->buf
->size
;
1244 dec
->msg
->body
.decode
.bsd_size
= bs_size
;
1245 dec
->msg
->body
.decode
.db_pitch
= align(dec
->base
.width
, get_db_pitch_alignment(dec
));
1247 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
&&
1248 ((struct si_screen
*)dec
->screen
)->info
.family
>= CHIP_POLARIS10
)
1249 dec
->msg
->body
.decode
.dpb_reserved
= dec
->ctx
.res
->buf
->size
;
1251 dt
= dec
->set_dtb(dec
->msg
, (struct vl_video_buffer
*)target
);
1252 if (((struct si_screen
*)dec
->screen
)->info
.family
>= CHIP_STONEY
)
1253 dec
->msg
->body
.decode
.dt_wa_chroma_top_offset
= dec
->msg
->body
.decode
.dt_pitch
/ 2;
1255 switch (u_reduce_video_profile(picture
->profile
)) {
1256 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
1257 dec
->msg
->body
.decode
.codec
.h264
= get_h264_msg(dec
, (struct pipe_h264_picture_desc
*)picture
);
1260 case PIPE_VIDEO_FORMAT_HEVC
:
1261 dec
->msg
->body
.decode
.codec
.h265
= get_h265_msg(dec
, target
, (struct pipe_h265_picture_desc
*)picture
);
1262 if (dec
->ctx
.res
== NULL
) {
1264 if (dec
->base
.profile
== PIPE_VIDEO_PROFILE_HEVC_MAIN_10
)
1265 ctx_size
= calc_ctx_size_h265_main10(dec
, (struct pipe_h265_picture_desc
*)picture
);
1267 ctx_size
= calc_ctx_size_h265_main(dec
);
1268 if (!si_vid_create_buffer(dec
->screen
, &dec
->ctx
, ctx_size
, PIPE_USAGE_DEFAULT
)) {
1269 RVID_ERR("Can't allocated context buffer.\n");
1271 si_vid_clear_buffer(decoder
->context
, &dec
->ctx
);
1275 dec
->msg
->body
.decode
.dpb_reserved
= dec
->ctx
.res
->buf
->size
;
1278 case PIPE_VIDEO_FORMAT_VC1
:
1279 dec
->msg
->body
.decode
.codec
.vc1
= get_vc1_msg((struct pipe_vc1_picture_desc
*)picture
);
1282 case PIPE_VIDEO_FORMAT_MPEG12
:
1283 dec
->msg
->body
.decode
.codec
.mpeg2
= get_mpeg2_msg(dec
, (struct pipe_mpeg12_picture_desc
*)picture
);
1286 case PIPE_VIDEO_FORMAT_MPEG4
:
1287 dec
->msg
->body
.decode
.codec
.mpeg4
= get_mpeg4_msg(dec
, (struct pipe_mpeg4_picture_desc
*)picture
);
1290 case PIPE_VIDEO_FORMAT_JPEG
:
1298 dec
->msg
->body
.decode
.db_surf_tile_config
= dec
->msg
->body
.decode
.dt_surf_tile_config
;
1299 dec
->msg
->body
.decode
.extension_support
= 0x1;
1301 /* set at least the feedback buffer size */
1302 dec
->fb
[0] = dec
->fb_size
;
1307 send_cmd(dec
, RUVD_CMD_DPB_BUFFER
, dec
->dpb
.res
->buf
, 0,
1308 RADEON_USAGE_READWRITE
, RADEON_DOMAIN_VRAM
);
1311 send_cmd(dec
, RUVD_CMD_CONTEXT_BUFFER
, dec
->ctx
.res
->buf
, 0,
1312 RADEON_USAGE_READWRITE
, RADEON_DOMAIN_VRAM
);
1313 send_cmd(dec
, RUVD_CMD_BITSTREAM_BUFFER
, bs_buf
->res
->buf
,
1314 0, RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
1315 send_cmd(dec
, RUVD_CMD_DECODING_TARGET_BUFFER
, dt
, 0,
1316 RADEON_USAGE_WRITE
, RADEON_DOMAIN_VRAM
);
1317 send_cmd(dec
, RUVD_CMD_FEEDBACK_BUFFER
, msg_fb_it_buf
->res
->buf
,
1318 FB_BUFFER_OFFSET
, RADEON_USAGE_WRITE
, RADEON_DOMAIN_GTT
);
1320 send_cmd(dec
, RUVD_CMD_ITSCALING_TABLE_BUFFER
, msg_fb_it_buf
->res
->buf
,
1321 FB_BUFFER_OFFSET
+ dec
->fb_size
, RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
1322 set_reg(dec
, dec
->reg
.cntl
, 1);
1324 flush(dec
, RADEON_FLUSH_ASYNC
);
1329 * flush any outstanding command buffers to the hardware
1331 static void ruvd_flush(struct pipe_video_codec
*decoder
)
1336 * create and UVD decoder
1338 struct pipe_video_codec
*si_common_uvd_create_decoder(struct pipe_context
*context
,
1339 const struct pipe_video_codec
*templ
,
1340 ruvd_set_dtb set_dtb
)
1342 struct radeon_winsys
* ws
= ((struct r600_common_context
*)context
)->ws
;
1343 struct r600_common_context
*rctx
= (struct r600_common_context
*)context
;
1345 unsigned width
= templ
->width
, height
= templ
->height
;
1346 unsigned bs_buf_size
;
1347 struct ruvd_decoder
*dec
;
1350 switch(u_reduce_video_profile(templ
->profile
)) {
1351 case PIPE_VIDEO_FORMAT_MPEG12
:
1352 if (templ
->entrypoint
> PIPE_VIDEO_ENTRYPOINT_BITSTREAM
)
1353 return vl_create_mpeg12_decoder(context
, templ
);
1356 case PIPE_VIDEO_FORMAT_MPEG4
:
1357 width
= align(width
, VL_MACROBLOCK_WIDTH
);
1358 height
= align(height
, VL_MACROBLOCK_HEIGHT
);
1360 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
1361 width
= align(width
, VL_MACROBLOCK_WIDTH
);
1362 height
= align(height
, VL_MACROBLOCK_HEIGHT
);
1370 dec
= CALLOC_STRUCT(ruvd_decoder
);
1375 if (rctx
->screen
->info
.drm_major
< 3)
1376 dec
->use_legacy
= true;
1379 dec
->base
.context
= context
;
1380 dec
->base
.width
= width
;
1381 dec
->base
.height
= height
;
1383 dec
->base
.destroy
= ruvd_destroy
;
1384 dec
->base
.begin_frame
= ruvd_begin_frame
;
1385 dec
->base
.decode_macroblock
= ruvd_decode_macroblock
;
1386 dec
->base
.decode_bitstream
= ruvd_decode_bitstream
;
1387 dec
->base
.end_frame
= ruvd_end_frame
;
1388 dec
->base
.flush
= ruvd_flush
;
1390 dec
->stream_type
= profile2stream_type(dec
, rctx
->family
);
1391 dec
->set_dtb
= set_dtb
;
1392 dec
->stream_handle
= si_vid_alloc_stream_handle();
1393 dec
->screen
= context
->screen
;
1395 dec
->cs
= ws
->cs_create(rctx
->ctx
, RING_UVD
, NULL
, NULL
);
1397 RVID_ERR("Can't get command submission context.\n");
1401 dec
->fb_size
= (rctx
->family
== CHIP_TONGA
) ? FB_BUFFER_SIZE_TONGA
:
1403 bs_buf_size
= width
* height
* (512 / (16 * 16));
1404 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1405 unsigned msg_fb_it_size
= FB_BUFFER_OFFSET
+ dec
->fb_size
;
1406 STATIC_ASSERT(sizeof(struct ruvd_msg
) <= FB_BUFFER_OFFSET
);
1408 msg_fb_it_size
+= IT_SCALING_TABLE_SIZE
;
1409 if (!si_vid_create_buffer(dec
->screen
, &dec
->msg_fb_it_buffers
[i
],
1410 msg_fb_it_size
, PIPE_USAGE_STAGING
)) {
1411 RVID_ERR("Can't allocated message buffers.\n");
1415 if (!si_vid_create_buffer(dec
->screen
, &dec
->bs_buffers
[i
],
1416 bs_buf_size
, PIPE_USAGE_STAGING
)) {
1417 RVID_ERR("Can't allocated bitstream buffers.\n");
1421 si_vid_clear_buffer(context
, &dec
->msg_fb_it_buffers
[i
]);
1422 si_vid_clear_buffer(context
, &dec
->bs_buffers
[i
]);
1425 dpb_size
= calc_dpb_size(dec
);
1427 if (!si_vid_create_buffer(dec
->screen
, &dec
->dpb
, dpb_size
, PIPE_USAGE_DEFAULT
)) {
1428 RVID_ERR("Can't allocated dpb.\n");
1431 si_vid_clear_buffer(context
, &dec
->dpb
);
1434 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
&& rctx
->family
>= CHIP_POLARIS10
) {
1435 unsigned ctx_size
= calc_ctx_size_h264_perf(dec
);
1436 if (!si_vid_create_buffer(dec
->screen
, &dec
->ctx
, ctx_size
, PIPE_USAGE_DEFAULT
)) {
1437 RVID_ERR("Can't allocated context buffer.\n");
1440 si_vid_clear_buffer(context
, &dec
->ctx
);
1443 if (rctx
->family
>= CHIP_POLARIS10
&& rctx
->screen
->info
.drm_minor
>= 3) {
1444 if (!si_vid_create_buffer(dec
->screen
, &dec
->sessionctx
,
1445 UVD_SESSION_CONTEXT_SIZE
,
1446 PIPE_USAGE_DEFAULT
)) {
1447 RVID_ERR("Can't allocated session ctx.\n");
1450 si_vid_clear_buffer(context
, &dec
->sessionctx
);
1453 if (rctx
->family
>= CHIP_VEGA10
) {
1454 dec
->reg
.data0
= RUVD_GPCOM_VCPU_DATA0_SOC15
;
1455 dec
->reg
.data1
= RUVD_GPCOM_VCPU_DATA1_SOC15
;
1456 dec
->reg
.cmd
= RUVD_GPCOM_VCPU_CMD_SOC15
;
1457 dec
->reg
.cntl
= RUVD_ENGINE_CNTL_SOC15
;
1459 dec
->reg
.data0
= RUVD_GPCOM_VCPU_DATA0
;
1460 dec
->reg
.data1
= RUVD_GPCOM_VCPU_DATA1
;
1461 dec
->reg
.cmd
= RUVD_GPCOM_VCPU_CMD
;
1462 dec
->reg
.cntl
= RUVD_ENGINE_CNTL
;
1465 map_msg_fb_it_buf(dec
);
1466 dec
->msg
->size
= sizeof(*dec
->msg
);
1467 dec
->msg
->msg_type
= RUVD_MSG_CREATE
;
1468 dec
->msg
->stream_handle
= dec
->stream_handle
;
1469 dec
->msg
->body
.create
.stream_type
= dec
->stream_type
;
1470 dec
->msg
->body
.create
.width_in_samples
= dec
->base
.width
;
1471 dec
->msg
->body
.create
.height_in_samples
= dec
->base
.height
;
1472 dec
->msg
->body
.create
.dpb_size
= dpb_size
;
1483 if (dec
->cs
) dec
->ws
->cs_destroy(dec
->cs
);
1485 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1486 si_vid_destroy_buffer(&dec
->msg_fb_it_buffers
[i
]);
1487 si_vid_destroy_buffer(&dec
->bs_buffers
[i
]);
1490 si_vid_destroy_buffer(&dec
->dpb
);
1491 si_vid_destroy_buffer(&dec
->ctx
);
1492 si_vid_destroy_buffer(&dec
->sessionctx
);
1499 /* calculate top/bottom offset */
1500 static unsigned texture_offset(struct radeon_surf
*surface
, unsigned layer
,
1501 enum ruvd_surface_type type
)
1505 case RUVD_SURFACE_TYPE_LEGACY
:
1506 return surface
->u
.legacy
.level
[0].offset
+
1507 layer
* (uint64_t)surface
->u
.legacy
.level
[0].slice_size_dw
* 4;
1509 case RUVD_SURFACE_TYPE_GFX9
:
1510 return surface
->u
.gfx9
.surf_offset
+
1511 layer
* surface
->u
.gfx9
.surf_slice_size
;
1516 /* hw encode the aspect of macro tiles */
1517 static unsigned macro_tile_aspect(unsigned macro_tile_aspect
)
1519 switch (macro_tile_aspect
) {
1521 case 1: macro_tile_aspect
= 0; break;
1522 case 2: macro_tile_aspect
= 1; break;
1523 case 4: macro_tile_aspect
= 2; break;
1524 case 8: macro_tile_aspect
= 3; break;
1526 return macro_tile_aspect
;
1529 /* hw encode the bank width and height */
1530 static unsigned bank_wh(unsigned bankwh
)
1534 case 1: bankwh
= 0; break;
1535 case 2: bankwh
= 1; break;
1536 case 4: bankwh
= 2; break;
1537 case 8: bankwh
= 3; break;
1543 * fill decoding target field from the luma and chroma surfaces
1545 void si_uvd_set_dt_surfaces(struct ruvd_msg
*msg
, struct radeon_surf
*luma
,
1546 struct radeon_surf
*chroma
, enum ruvd_surface_type type
)
1550 case RUVD_SURFACE_TYPE_LEGACY
:
1551 msg
->body
.decode
.dt_pitch
= luma
->u
.legacy
.level
[0].nblk_x
* luma
->blk_w
;
1552 switch (luma
->u
.legacy
.level
[0].mode
) {
1553 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
1554 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_LINEAR
;
1555 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_LINEAR
;
1557 case RADEON_SURF_MODE_1D
:
1558 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_8X8
;
1559 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_1D_THIN
;
1561 case RADEON_SURF_MODE_2D
:
1562 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_8X8
;
1563 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_2D_THIN
;
1570 msg
->body
.decode
.dt_luma_top_offset
= texture_offset(luma
, 0, type
);
1572 msg
->body
.decode
.dt_chroma_top_offset
= texture_offset(chroma
, 0, type
);
1573 if (msg
->body
.decode
.dt_field_mode
) {
1574 msg
->body
.decode
.dt_luma_bottom_offset
= texture_offset(luma
, 1, type
);
1576 msg
->body
.decode
.dt_chroma_bottom_offset
= texture_offset(chroma
, 1, type
);
1578 msg
->body
.decode
.dt_luma_bottom_offset
= msg
->body
.decode
.dt_luma_top_offset
;
1579 msg
->body
.decode
.dt_chroma_bottom_offset
= msg
->body
.decode
.dt_chroma_top_offset
;
1583 assert(luma
->u
.legacy
.bankw
== chroma
->u
.legacy
.bankw
);
1584 assert(luma
->u
.legacy
.bankh
== chroma
->u
.legacy
.bankh
);
1585 assert(luma
->u
.legacy
.mtilea
== chroma
->u
.legacy
.mtilea
);
1588 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_BANK_WIDTH(bank_wh(luma
->u
.legacy
.bankw
));
1589 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_BANK_HEIGHT(bank_wh(luma
->u
.legacy
.bankh
));
1590 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma
->u
.legacy
.mtilea
));
1592 case RUVD_SURFACE_TYPE_GFX9
:
1593 msg
->body
.decode
.dt_pitch
= luma
->u
.gfx9
.surf_pitch
* luma
->blk_w
;
1594 /* SWIZZLE LINEAR MODE */
1595 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_LINEAR
;
1596 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_LINEAR
;
1597 msg
->body
.decode
.dt_luma_top_offset
= texture_offset(luma
, 0, type
);
1598 msg
->body
.decode
.dt_chroma_top_offset
= texture_offset(chroma
, 0, type
);
1599 if (msg
->body
.decode
.dt_field_mode
) {
1600 msg
->body
.decode
.dt_luma_bottom_offset
= texture_offset(luma
, 1, type
);
1601 msg
->body
.decode
.dt_chroma_bottom_offset
= texture_offset(chroma
, 1, type
);
1603 msg
->body
.decode
.dt_luma_bottom_offset
= msg
->body
.decode
.dt_luma_top_offset
;
1604 msg
->body
.decode
.dt_chroma_bottom_offset
= msg
->body
.decode
.dt_chroma_top_offset
;
1606 msg
->body
.decode
.dt_surf_tile_config
= 0;