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 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
34 #include <sys/types.h>
40 #include "pipe/p_video_codec.h"
42 #include "util/u_memory.h"
43 #include "util/u_video.h"
45 #include "vl/vl_defines.h"
46 #include "vl/vl_mpeg12_decoder.h"
48 #include "r600_pipe_common.h"
49 #include "radeon_video.h"
50 #include "radeon_uvd.h"
54 #define NUM_MPEG2_REFS 6
55 #define NUM_H264_REFS 17
56 #define NUM_VC1_REFS 5
58 #define FB_BUFFER_OFFSET 0x1000
59 #define FB_BUFFER_SIZE 2048
60 #define FB_BUFFER_SIZE_TONGA (2048 * 64)
61 #define IT_SCALING_TABLE_SIZE 992
62 #define UVD_SESSION_CONTEXT_SIZE (128 * 1024)
64 /* UVD decoder representation */
66 struct pipe_video_codec base
;
70 unsigned stream_handle
;
72 unsigned frame_number
;
74 struct pipe_screen
*screen
;
75 struct radeon_winsys
* ws
;
76 struct radeon_cmdbuf
* cs
;
80 struct rvid_buffer msg_fb_it_buffers
[NUM_BUFFERS
];
86 struct rvid_buffer bs_buffers
[NUM_BUFFERS
];
90 struct rvid_buffer dpb
;
92 struct rvid_buffer ctx
;
93 struct rvid_buffer sessionctx
;
102 /* flush IB to the hardware */
103 static int flush(struct ruvd_decoder
*dec
, unsigned flags
)
105 return dec
->ws
->cs_flush(dec
->cs
, flags
, NULL
);
108 /* add a new set register command to the IB */
109 static void set_reg(struct ruvd_decoder
*dec
, unsigned reg
, uint32_t val
)
111 radeon_emit(dec
->cs
, RUVD_PKT0(reg
>> 2, 0));
112 radeon_emit(dec
->cs
, val
);
115 /* send a command to the VCPU through the GPCOM registers */
116 static void send_cmd(struct ruvd_decoder
*dec
, unsigned cmd
,
117 struct pb_buffer
* buf
, uint32_t off
,
118 enum radeon_bo_usage usage
, enum radeon_bo_domain domain
)
122 reloc_idx
= dec
->ws
->cs_add_buffer(dec
->cs
, buf
, usage
| RADEON_USAGE_SYNCHRONIZED
,
124 if (!dec
->use_legacy
) {
126 addr
= dec
->ws
->buffer_get_virtual_address(buf
);
128 set_reg(dec
, dec
->reg
.data0
, addr
);
129 set_reg(dec
, dec
->reg
.data1
, addr
>> 32);
131 off
+= dec
->ws
->buffer_get_reloc_offset(buf
);
132 set_reg(dec
, RUVD_GPCOM_VCPU_DATA0
, off
);
133 set_reg(dec
, RUVD_GPCOM_VCPU_DATA1
, reloc_idx
* 4);
135 set_reg(dec
, dec
->reg
.cmd
, cmd
<< 1);
138 /* do the codec needs an IT buffer ?*/
139 static bool have_it(struct ruvd_decoder
*dec
)
141 return dec
->stream_type
== RUVD_CODEC_H264_PERF
||
142 dec
->stream_type
== RUVD_CODEC_H265
;
145 /* map the next available message/feedback/itscaling buffer */
146 static void map_msg_fb_it_buf(struct ruvd_decoder
*dec
)
148 struct rvid_buffer
* buf
;
151 /* grab the current message/feedback buffer */
152 buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
154 /* and map it for CPU access */
155 ptr
= dec
->ws
->buffer_map(buf
->res
->buf
, dec
->cs
,
156 PIPE_TRANSFER_WRITE
| RADEON_TRANSFER_TEMPORARY
);
158 /* calc buffer offsets */
159 dec
->msg
= (struct ruvd_msg
*)ptr
;
160 memset(dec
->msg
, 0, sizeof(*dec
->msg
));
162 dec
->fb
= (uint32_t *)(ptr
+ FB_BUFFER_OFFSET
);
164 dec
->it
= (uint8_t *)(ptr
+ FB_BUFFER_OFFSET
+ dec
->fb_size
);
167 /* unmap and send a message command to the VCPU */
168 static void send_msg_buf(struct ruvd_decoder
*dec
)
170 struct rvid_buffer
* buf
;
172 /* ignore the request if message/feedback buffer isn't mapped */
173 if (!dec
->msg
|| !dec
->fb
)
176 /* grab the current message buffer */
177 buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
179 /* unmap the buffer */
180 dec
->ws
->buffer_unmap(buf
->res
->buf
);
186 if (dec
->sessionctx
.res
)
187 send_cmd(dec
, RUVD_CMD_SESSION_CONTEXT_BUFFER
,
188 dec
->sessionctx
.res
->buf
, 0, RADEON_USAGE_READWRITE
,
191 /* and send it to the hardware */
192 send_cmd(dec
, RUVD_CMD_MSG_BUFFER
, buf
->res
->buf
, 0,
193 RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
196 /* cycle to the next set of buffers */
197 static void next_buffer(struct ruvd_decoder
*dec
)
200 dec
->cur_buffer
%= NUM_BUFFERS
;
203 /* convert the profile into something UVD understands */
204 static uint32_t profile2stream_type(struct ruvd_decoder
*dec
, unsigned family
)
206 switch (u_reduce_video_profile(dec
->base
.profile
)) {
207 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
208 return RUVD_CODEC_H264
;
210 case PIPE_VIDEO_FORMAT_VC1
:
211 return RUVD_CODEC_VC1
;
213 case PIPE_VIDEO_FORMAT_MPEG12
:
214 return RUVD_CODEC_MPEG2
;
216 case PIPE_VIDEO_FORMAT_MPEG4
:
217 return RUVD_CODEC_MPEG4
;
219 case PIPE_VIDEO_FORMAT_JPEG
:
220 return RUVD_CODEC_MJPEG
;
228 static unsigned calc_ctx_size_h265_main(struct ruvd_decoder
*dec
)
230 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
231 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
233 unsigned max_references
= dec
->base
.max_references
+ 1;
235 if (dec
->base
.width
* dec
->base
.height
>= 4096*2000)
236 max_references
= MAX2(max_references
, 8);
238 max_references
= MAX2(max_references
, 17);
240 width
= align (width
, 16);
241 height
= align (height
, 16);
242 return ((width
+ 255) / 16) * ((height
+ 255) / 16) * 16 * max_references
+ 52 * 1024;
245 static unsigned calc_ctx_size_h265_main10(struct ruvd_decoder
*dec
, struct pipe_h265_picture_desc
*pic
)
247 unsigned log2_ctb_size
, width_in_ctb
, height_in_ctb
, num_16x16_block_per_ctb
;
248 unsigned context_buffer_size_per_ctb_row
, cm_buffer_size
, max_mb_address
, db_left_tile_pxl_size
;
249 unsigned db_left_tile_ctx_size
= 4096 / 16 * (32 + 16 * 4);
251 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
252 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
253 unsigned coeff_10bit
= (pic
->pps
->sps
->bit_depth_luma_minus8
|| pic
->pps
->sps
->bit_depth_chroma_minus8
) ? 2 : 1;
255 unsigned max_references
= dec
->base
.max_references
+ 1;
257 if (dec
->base
.width
* dec
->base
.height
>= 4096*2000)
258 max_references
= MAX2(max_references
, 8);
260 max_references
= MAX2(max_references
, 17);
262 log2_ctb_size
= pic
->pps
->sps
->log2_min_luma_coding_block_size_minus3
+ 3 +
263 pic
->pps
->sps
->log2_diff_max_min_luma_coding_block_size
;
265 width_in_ctb
= (width
+ ((1 << log2_ctb_size
) - 1)) >> log2_ctb_size
;
266 height_in_ctb
= (height
+ ((1 << log2_ctb_size
) - 1)) >> log2_ctb_size
;
268 num_16x16_block_per_ctb
= ((1 << log2_ctb_size
) >> 4) * ((1 << log2_ctb_size
) >> 4);
269 context_buffer_size_per_ctb_row
= align(width_in_ctb
* num_16x16_block_per_ctb
* 16, 256);
270 max_mb_address
= (unsigned) ceil(height
* 8 / 2048.0);
272 cm_buffer_size
= max_references
* context_buffer_size_per_ctb_row
* height_in_ctb
;
273 db_left_tile_pxl_size
= coeff_10bit
* (max_mb_address
* 2 * 2048 + 1024);
275 return cm_buffer_size
+ db_left_tile_ctx_size
+ db_left_tile_pxl_size
;
278 static unsigned get_db_pitch_alignment(struct ruvd_decoder
*dec
)
283 /* calculate size of reference picture buffer */
284 static unsigned calc_dpb_size(struct ruvd_decoder
*dec
)
286 unsigned width_in_mb
, height_in_mb
, image_size
, dpb_size
;
288 // always align them to MB size for dpb calculation
289 unsigned width
= align(dec
->base
.width
, VL_MACROBLOCK_WIDTH
);
290 unsigned height
= align(dec
->base
.height
, VL_MACROBLOCK_HEIGHT
);
292 // always one more for currently decoded picture
293 unsigned max_references
= dec
->base
.max_references
+ 1;
295 // aligned size of a single frame
296 image_size
= align(width
, get_db_pitch_alignment(dec
)) * height
;
297 image_size
+= image_size
/ 2;
298 image_size
= align(image_size
, 1024);
300 // picture width & height in 16 pixel units
301 width_in_mb
= width
/ VL_MACROBLOCK_WIDTH
;
302 height_in_mb
= align(height
/ VL_MACROBLOCK_HEIGHT
, 2);
304 switch (u_reduce_video_profile(dec
->base
.profile
)) {
305 case PIPE_VIDEO_FORMAT_MPEG4_AVC
: {
306 if (!dec
->use_legacy
) {
307 unsigned fs_in_mb
= width_in_mb
* height_in_mb
;
308 unsigned alignment
= 64, num_dpb_buffer
;
310 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
)
312 switch(dec
->base
.level
) {
314 num_dpb_buffer
= 8100 / fs_in_mb
;
317 num_dpb_buffer
= 18000 / fs_in_mb
;
320 num_dpb_buffer
= 20480 / fs_in_mb
;
323 num_dpb_buffer
= 32768 / fs_in_mb
;
326 num_dpb_buffer
= 34816 / fs_in_mb
;
329 num_dpb_buffer
= 110400 / fs_in_mb
;
332 num_dpb_buffer
= 184320 / fs_in_mb
;
335 num_dpb_buffer
= 184320 / fs_in_mb
;
339 max_references
= MAX2(MIN2(NUM_H264_REFS
, num_dpb_buffer
), max_references
);
340 dpb_size
= image_size
* max_references
;
341 if ((dec
->stream_type
!= RUVD_CODEC_H264_PERF
)) {
342 dpb_size
+= max_references
* align(width_in_mb
* height_in_mb
* 192, alignment
);
343 dpb_size
+= align(width_in_mb
* height_in_mb
* 32, alignment
);
346 // the firmware seems to allways assume a minimum of ref frames
347 max_references
= MAX2(NUM_H264_REFS
, max_references
);
348 // reference picture buffer
349 dpb_size
= image_size
* max_references
;
350 if ((dec
->stream_type
!= RUVD_CODEC_H264_PERF
)) {
351 // macroblock context buffer
352 dpb_size
+= width_in_mb
* height_in_mb
* max_references
* 192;
354 dpb_size
+= width_in_mb
* height_in_mb
* 32;
360 case PIPE_VIDEO_FORMAT_VC1
:
361 // the firmware seems to allways assume a minimum of ref frames
362 max_references
= MAX2(NUM_VC1_REFS
, max_references
);
364 // reference picture buffer
365 dpb_size
= image_size
* max_references
;
368 dpb_size
+= width_in_mb
* height_in_mb
* 128;
371 dpb_size
+= width_in_mb
* 64;
374 dpb_size
+= width_in_mb
* 128;
377 dpb_size
+= align(MAX2(width_in_mb
, height_in_mb
) * 7 * 16, 64);
380 case PIPE_VIDEO_FORMAT_MPEG12
:
381 // reference picture buffer, must be big enough for all frames
382 dpb_size
= image_size
* NUM_MPEG2_REFS
;
385 case PIPE_VIDEO_FORMAT_MPEG4
:
386 // reference picture buffer
387 dpb_size
= image_size
* max_references
;
390 dpb_size
+= width_in_mb
* height_in_mb
* 64;
393 dpb_size
+= align(width_in_mb
* height_in_mb
* 32, 64);
395 dpb_size
= MAX2(dpb_size
, 30 * 1024 * 1024);
398 case PIPE_VIDEO_FORMAT_JPEG
:
403 // something is missing here
406 // at least use a sane default value
407 dpb_size
= 32 * 1024 * 1024;
413 /* free associated data in the video buffer callback */
414 static void ruvd_destroy_associated_data(void *data
)
416 /* NOOP, since we only use an intptr */
419 /* get h264 specific message bits */
420 static struct ruvd_h264
get_h264_msg(struct ruvd_decoder
*dec
, struct pipe_h264_picture_desc
*pic
)
422 struct ruvd_h264 result
;
424 memset(&result
, 0, sizeof(result
));
425 switch (pic
->base
.profile
) {
426 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE
:
427 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE
:
428 result
.profile
= RUVD_H264_PROFILE_BASELINE
;
431 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN
:
432 result
.profile
= RUVD_H264_PROFILE_MAIN
;
435 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
:
436 result
.profile
= RUVD_H264_PROFILE_HIGH
;
444 result
.level
= dec
->base
.level
;
446 result
.sps_info_flags
= 0;
447 result
.sps_info_flags
|= pic
->pps
->sps
->direct_8x8_inference_flag
<< 0;
448 result
.sps_info_flags
|= pic
->pps
->sps
->mb_adaptive_frame_field_flag
<< 1;
449 result
.sps_info_flags
|= pic
->pps
->sps
->frame_mbs_only_flag
<< 2;
450 result
.sps_info_flags
|= pic
->pps
->sps
->delta_pic_order_always_zero_flag
<< 3;
452 result
.bit_depth_luma_minus8
= pic
->pps
->sps
->bit_depth_luma_minus8
;
453 result
.bit_depth_chroma_minus8
= pic
->pps
->sps
->bit_depth_chroma_minus8
;
454 result
.log2_max_frame_num_minus4
= pic
->pps
->sps
->log2_max_frame_num_minus4
;
455 result
.pic_order_cnt_type
= pic
->pps
->sps
->pic_order_cnt_type
;
456 result
.log2_max_pic_order_cnt_lsb_minus4
= pic
->pps
->sps
->log2_max_pic_order_cnt_lsb_minus4
;
458 switch (dec
->base
.chroma_format
) {
459 case PIPE_VIDEO_CHROMA_FORMAT_NONE
:
462 case PIPE_VIDEO_CHROMA_FORMAT_400
:
463 result
.chroma_format
= 0;
465 case PIPE_VIDEO_CHROMA_FORMAT_420
:
466 result
.chroma_format
= 1;
468 case PIPE_VIDEO_CHROMA_FORMAT_422
:
469 result
.chroma_format
= 2;
471 case PIPE_VIDEO_CHROMA_FORMAT_444
:
472 result
.chroma_format
= 3;
476 result
.pps_info_flags
= 0;
477 result
.pps_info_flags
|= pic
->pps
->transform_8x8_mode_flag
<< 0;
478 result
.pps_info_flags
|= pic
->pps
->redundant_pic_cnt_present_flag
<< 1;
479 result
.pps_info_flags
|= pic
->pps
->constrained_intra_pred_flag
<< 2;
480 result
.pps_info_flags
|= pic
->pps
->deblocking_filter_control_present_flag
<< 3;
481 result
.pps_info_flags
|= pic
->pps
->weighted_bipred_idc
<< 4;
482 result
.pps_info_flags
|= pic
->pps
->weighted_pred_flag
<< 6;
483 result
.pps_info_flags
|= pic
->pps
->bottom_field_pic_order_in_frame_present_flag
<< 7;
484 result
.pps_info_flags
|= pic
->pps
->entropy_coding_mode_flag
<< 8;
486 result
.num_slice_groups_minus1
= pic
->pps
->num_slice_groups_minus1
;
487 result
.slice_group_map_type
= pic
->pps
->slice_group_map_type
;
488 result
.slice_group_change_rate_minus1
= pic
->pps
->slice_group_change_rate_minus1
;
489 result
.pic_init_qp_minus26
= pic
->pps
->pic_init_qp_minus26
;
490 result
.chroma_qp_index_offset
= pic
->pps
->chroma_qp_index_offset
;
491 result
.second_chroma_qp_index_offset
= pic
->pps
->second_chroma_qp_index_offset
;
493 memcpy(result
.scaling_list_4x4
, pic
->pps
->ScalingList4x4
, 6*16);
494 memcpy(result
.scaling_list_8x8
, pic
->pps
->ScalingList8x8
, 2*64);
496 if (dec
->stream_type
== RUVD_CODEC_H264_PERF
) {
497 memcpy(dec
->it
, result
.scaling_list_4x4
, 6*16);
498 memcpy((dec
->it
+ 96), result
.scaling_list_8x8
, 2*64);
501 result
.num_ref_frames
= pic
->num_ref_frames
;
503 result
.num_ref_idx_l0_active_minus1
= pic
->num_ref_idx_l0_active_minus1
;
504 result
.num_ref_idx_l1_active_minus1
= pic
->num_ref_idx_l1_active_minus1
;
506 result
.frame_num
= pic
->frame_num
;
507 memcpy(result
.frame_num_list
, pic
->frame_num_list
, 4*16);
508 result
.curr_field_order_cnt_list
[0] = pic
->field_order_cnt
[0];
509 result
.curr_field_order_cnt_list
[1] = pic
->field_order_cnt
[1];
510 memcpy(result
.field_order_cnt_list
, pic
->field_order_cnt_list
, 4*16*2);
512 result
.decoded_pic_idx
= pic
->frame_num
;
517 /* get h265 specific message bits */
518 static struct ruvd_h265
get_h265_msg(struct ruvd_decoder
*dec
, struct pipe_video_buffer
*target
,
519 struct pipe_h265_picture_desc
*pic
)
521 struct ruvd_h265 result
;
524 memset(&result
, 0, sizeof(result
));
526 result
.sps_info_flags
= 0;
527 result
.sps_info_flags
|= pic
->pps
->sps
->scaling_list_enabled_flag
<< 0;
528 result
.sps_info_flags
|= pic
->pps
->sps
->amp_enabled_flag
<< 1;
529 result
.sps_info_flags
|= pic
->pps
->sps
->sample_adaptive_offset_enabled_flag
<< 2;
530 result
.sps_info_flags
|= pic
->pps
->sps
->pcm_enabled_flag
<< 3;
531 result
.sps_info_flags
|= pic
->pps
->sps
->pcm_loop_filter_disabled_flag
<< 4;
532 result
.sps_info_flags
|= pic
->pps
->sps
->long_term_ref_pics_present_flag
<< 5;
533 result
.sps_info_flags
|= pic
->pps
->sps
->sps_temporal_mvp_enabled_flag
<< 6;
534 result
.sps_info_flags
|= pic
->pps
->sps
->strong_intra_smoothing_enabled_flag
<< 7;
535 result
.sps_info_flags
|= pic
->pps
->sps
->separate_colour_plane_flag
<< 8;
536 if (pic
->UseRefPicList
== true)
537 result
.sps_info_flags
|= 1 << 10;
539 result
.chroma_format
= pic
->pps
->sps
->chroma_format_idc
;
540 result
.bit_depth_luma_minus8
= pic
->pps
->sps
->bit_depth_luma_minus8
;
541 result
.bit_depth_chroma_minus8
= pic
->pps
->sps
->bit_depth_chroma_minus8
;
542 result
.log2_max_pic_order_cnt_lsb_minus4
= pic
->pps
->sps
->log2_max_pic_order_cnt_lsb_minus4
;
543 result
.sps_max_dec_pic_buffering_minus1
= pic
->pps
->sps
->sps_max_dec_pic_buffering_minus1
;
544 result
.log2_min_luma_coding_block_size_minus3
= pic
->pps
->sps
->log2_min_luma_coding_block_size_minus3
;
545 result
.log2_diff_max_min_luma_coding_block_size
= pic
->pps
->sps
->log2_diff_max_min_luma_coding_block_size
;
546 result
.log2_min_transform_block_size_minus2
= pic
->pps
->sps
->log2_min_transform_block_size_minus2
;
547 result
.log2_diff_max_min_transform_block_size
= pic
->pps
->sps
->log2_diff_max_min_transform_block_size
;
548 result
.max_transform_hierarchy_depth_inter
= pic
->pps
->sps
->max_transform_hierarchy_depth_inter
;
549 result
.max_transform_hierarchy_depth_intra
= pic
->pps
->sps
->max_transform_hierarchy_depth_intra
;
550 result
.pcm_sample_bit_depth_luma_minus1
= pic
->pps
->sps
->pcm_sample_bit_depth_luma_minus1
;
551 result
.pcm_sample_bit_depth_chroma_minus1
= pic
->pps
->sps
->pcm_sample_bit_depth_chroma_minus1
;
552 result
.log2_min_pcm_luma_coding_block_size_minus3
= pic
->pps
->sps
->log2_min_pcm_luma_coding_block_size_minus3
;
553 result
.log2_diff_max_min_pcm_luma_coding_block_size
= pic
->pps
->sps
->log2_diff_max_min_pcm_luma_coding_block_size
;
554 result
.num_short_term_ref_pic_sets
= pic
->pps
->sps
->num_short_term_ref_pic_sets
;
556 result
.pps_info_flags
= 0;
557 result
.pps_info_flags
|= pic
->pps
->dependent_slice_segments_enabled_flag
<< 0;
558 result
.pps_info_flags
|= pic
->pps
->output_flag_present_flag
<< 1;
559 result
.pps_info_flags
|= pic
->pps
->sign_data_hiding_enabled_flag
<< 2;
560 result
.pps_info_flags
|= pic
->pps
->cabac_init_present_flag
<< 3;
561 result
.pps_info_flags
|= pic
->pps
->constrained_intra_pred_flag
<< 4;
562 result
.pps_info_flags
|= pic
->pps
->transform_skip_enabled_flag
<< 5;
563 result
.pps_info_flags
|= pic
->pps
->cu_qp_delta_enabled_flag
<< 6;
564 result
.pps_info_flags
|= pic
->pps
->pps_slice_chroma_qp_offsets_present_flag
<< 7;
565 result
.pps_info_flags
|= pic
->pps
->weighted_pred_flag
<< 8;
566 result
.pps_info_flags
|= pic
->pps
->weighted_bipred_flag
<< 9;
567 result
.pps_info_flags
|= pic
->pps
->transquant_bypass_enabled_flag
<< 10;
568 result
.pps_info_flags
|= pic
->pps
->tiles_enabled_flag
<< 11;
569 result
.pps_info_flags
|= pic
->pps
->entropy_coding_sync_enabled_flag
<< 12;
570 result
.pps_info_flags
|= pic
->pps
->uniform_spacing_flag
<< 13;
571 result
.pps_info_flags
|= pic
->pps
->loop_filter_across_tiles_enabled_flag
<< 14;
572 result
.pps_info_flags
|= pic
->pps
->pps_loop_filter_across_slices_enabled_flag
<< 15;
573 result
.pps_info_flags
|= pic
->pps
->deblocking_filter_override_enabled_flag
<< 16;
574 result
.pps_info_flags
|= pic
->pps
->pps_deblocking_filter_disabled_flag
<< 17;
575 result
.pps_info_flags
|= pic
->pps
->lists_modification_present_flag
<< 18;
576 result
.pps_info_flags
|= pic
->pps
->slice_segment_header_extension_present_flag
<< 19;
577 //result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag; ???
579 result
.num_extra_slice_header_bits
= pic
->pps
->num_extra_slice_header_bits
;
580 result
.num_long_term_ref_pic_sps
= pic
->pps
->sps
->num_long_term_ref_pics_sps
;
581 result
.num_ref_idx_l0_default_active_minus1
= pic
->pps
->num_ref_idx_l0_default_active_minus1
;
582 result
.num_ref_idx_l1_default_active_minus1
= pic
->pps
->num_ref_idx_l1_default_active_minus1
;
583 result
.pps_cb_qp_offset
= pic
->pps
->pps_cb_qp_offset
;
584 result
.pps_cr_qp_offset
= pic
->pps
->pps_cr_qp_offset
;
585 result
.pps_beta_offset_div2
= pic
->pps
->pps_beta_offset_div2
;
586 result
.pps_tc_offset_div2
= pic
->pps
->pps_tc_offset_div2
;
587 result
.diff_cu_qp_delta_depth
= pic
->pps
->diff_cu_qp_delta_depth
;
588 result
.num_tile_columns_minus1
= pic
->pps
->num_tile_columns_minus1
;
589 result
.num_tile_rows_minus1
= pic
->pps
->num_tile_rows_minus1
;
590 result
.log2_parallel_merge_level_minus2
= pic
->pps
->log2_parallel_merge_level_minus2
;
591 result
.init_qp_minus26
= pic
->pps
->init_qp_minus26
;
593 for (i
= 0; i
< 19; ++i
)
594 result
.column_width_minus1
[i
] = pic
->pps
->column_width_minus1
[i
];
596 for (i
= 0; i
< 21; ++i
)
597 result
.row_height_minus1
[i
] = pic
->pps
->row_height_minus1
[i
];
599 result
.num_delta_pocs_ref_rps_idx
= pic
->NumDeltaPocsOfRefRpsIdx
;
600 result
.curr_idx
= pic
->CurrPicOrderCntVal
;
601 result
.curr_poc
= pic
->CurrPicOrderCntVal
;
603 vl_video_buffer_set_associated_data(target
, &dec
->base
,
604 (void *)(uintptr_t)pic
->CurrPicOrderCntVal
,
605 &ruvd_destroy_associated_data
);
607 for (i
= 0; i
< 16; ++i
) {
608 struct pipe_video_buffer
*ref
= pic
->ref
[i
];
609 uintptr_t ref_pic
= 0;
611 result
.poc_list
[i
] = pic
->PicOrderCntVal
[i
];
614 ref_pic
= (uintptr_t)vl_video_buffer_get_associated_data(ref
, &dec
->base
);
617 result
.ref_pic_list
[i
] = ref_pic
;
620 for (i
= 0; i
< 8; ++i
) {
621 result
.ref_pic_set_st_curr_before
[i
] = 0xFF;
622 result
.ref_pic_set_st_curr_after
[i
] = 0xFF;
623 result
.ref_pic_set_lt_curr
[i
] = 0xFF;
626 for (i
= 0; i
< pic
->NumPocStCurrBefore
; ++i
)
627 result
.ref_pic_set_st_curr_before
[i
] = pic
->RefPicSetStCurrBefore
[i
];
629 for (i
= 0; i
< pic
->NumPocStCurrAfter
; ++i
)
630 result
.ref_pic_set_st_curr_after
[i
] = pic
->RefPicSetStCurrAfter
[i
];
632 for (i
= 0; i
< pic
->NumPocLtCurr
; ++i
)
633 result
.ref_pic_set_lt_curr
[i
] = pic
->RefPicSetLtCurr
[i
];
635 for (i
= 0; i
< 6; ++i
)
636 result
.ucScalingListDCCoefSizeID2
[i
] = pic
->pps
->sps
->ScalingListDCCoeff16x16
[i
];
638 for (i
= 0; i
< 2; ++i
)
639 result
.ucScalingListDCCoefSizeID3
[i
] = pic
->pps
->sps
->ScalingListDCCoeff32x32
[i
];
641 memcpy(dec
->it
, pic
->pps
->sps
->ScalingList4x4
, 6 * 16);
642 memcpy(dec
->it
+ 96, pic
->pps
->sps
->ScalingList8x8
, 6 * 64);
643 memcpy(dec
->it
+ 480, pic
->pps
->sps
->ScalingList16x16
, 6 * 64);
644 memcpy(dec
->it
+ 864, pic
->pps
->sps
->ScalingList32x32
, 2 * 64);
646 for (i
= 0 ; i
< 2 ; i
++) {
647 for (int j
= 0 ; j
< 15 ; j
++)
648 result
.direct_reflist
[i
][j
] = pic
->RefPicList
[i
][j
];
658 NumShortTermPictureSliceHeaderBits;
659 NumLongTermPictureSliceHeaderBits;
667 /* get vc1 specific message bits */
668 static struct ruvd_vc1
get_vc1_msg(struct pipe_vc1_picture_desc
*pic
)
670 struct ruvd_vc1 result
;
672 memset(&result
, 0, sizeof(result
));
674 switch(pic
->base
.profile
) {
675 case PIPE_VIDEO_PROFILE_VC1_SIMPLE
:
676 result
.profile
= RUVD_VC1_PROFILE_SIMPLE
;
680 case PIPE_VIDEO_PROFILE_VC1_MAIN
:
681 result
.profile
= RUVD_VC1_PROFILE_MAIN
;
685 case PIPE_VIDEO_PROFILE_VC1_ADVANCED
:
686 result
.profile
= RUVD_VC1_PROFILE_ADVANCED
;
694 /* fields common for all profiles */
695 result
.sps_info_flags
|= pic
->postprocflag
<< 7;
696 result
.sps_info_flags
|= pic
->pulldown
<< 6;
697 result
.sps_info_flags
|= pic
->interlace
<< 5;
698 result
.sps_info_flags
|= pic
->tfcntrflag
<< 4;
699 result
.sps_info_flags
|= pic
->finterpflag
<< 3;
700 result
.sps_info_flags
|= pic
->psf
<< 1;
702 result
.pps_info_flags
|= pic
->range_mapy_flag
<< 31;
703 result
.pps_info_flags
|= pic
->range_mapy
<< 28;
704 result
.pps_info_flags
|= pic
->range_mapuv_flag
<< 27;
705 result
.pps_info_flags
|= pic
->range_mapuv
<< 24;
706 result
.pps_info_flags
|= pic
->multires
<< 21;
707 result
.pps_info_flags
|= pic
->maxbframes
<< 16;
708 result
.pps_info_flags
|= pic
->overlap
<< 11;
709 result
.pps_info_flags
|= pic
->quantizer
<< 9;
710 result
.pps_info_flags
|= pic
->panscan_flag
<< 7;
711 result
.pps_info_flags
|= pic
->refdist_flag
<< 6;
712 result
.pps_info_flags
|= pic
->vstransform
<< 0;
714 /* some fields only apply to main/advanced profile */
715 if (pic
->base
.profile
!= PIPE_VIDEO_PROFILE_VC1_SIMPLE
) {
716 result
.pps_info_flags
|= pic
->syncmarker
<< 20;
717 result
.pps_info_flags
|= pic
->rangered
<< 19;
718 result
.pps_info_flags
|= pic
->loopfilter
<< 5;
719 result
.pps_info_flags
|= pic
->fastuvmc
<< 4;
720 result
.pps_info_flags
|= pic
->extended_mv
<< 3;
721 result
.pps_info_flags
|= pic
->extended_dmv
<< 8;
722 result
.pps_info_flags
|= pic
->dquant
<< 1;
725 result
.chroma_format
= 1;
728 //(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
731 uint8_t frame_coding_mode
732 uint8_t deblockEnable
739 /* extract the frame number from a referenced video buffer */
740 static uint32_t get_ref_pic_idx(struct ruvd_decoder
*dec
, struct pipe_video_buffer
*ref
)
742 uint32_t min
= MAX2(dec
->frame_number
, NUM_MPEG2_REFS
) - NUM_MPEG2_REFS
;
743 uint32_t max
= MAX2(dec
->frame_number
, 1) - 1;
746 /* seems to be the most sane fallback */
750 /* get the frame number from the associated data */
751 frame
= (uintptr_t)vl_video_buffer_get_associated_data(ref
, &dec
->base
);
753 /* limit the frame number to a valid range */
754 return MAX2(MIN2(frame
, max
), min
);
757 /* get mpeg2 specific msg bits */
758 static struct ruvd_mpeg2
get_mpeg2_msg(struct ruvd_decoder
*dec
,
759 struct pipe_mpeg12_picture_desc
*pic
)
761 const int *zscan
= pic
->alternate_scan
? vl_zscan_alternate
: vl_zscan_normal
;
762 struct ruvd_mpeg2 result
;
765 memset(&result
, 0, sizeof(result
));
766 result
.decoded_pic_idx
= dec
->frame_number
;
767 for (i
= 0; i
< 2; ++i
)
768 result
.ref_pic_idx
[i
] = get_ref_pic_idx(dec
, pic
->ref
[i
]);
770 result
.load_intra_quantiser_matrix
= 1;
771 result
.load_nonintra_quantiser_matrix
= 1;
773 for (i
= 0; i
< 64; ++i
) {
774 result
.intra_quantiser_matrix
[i
] = pic
->intra_matrix
[zscan
[i
]];
775 result
.nonintra_quantiser_matrix
[i
] = pic
->non_intra_matrix
[zscan
[i
]];
778 result
.profile_and_level_indication
= 0;
779 result
.chroma_format
= 0x1;
781 result
.picture_coding_type
= pic
->picture_coding_type
;
782 result
.f_code
[0][0] = pic
->f_code
[0][0] + 1;
783 result
.f_code
[0][1] = pic
->f_code
[0][1] + 1;
784 result
.f_code
[1][0] = pic
->f_code
[1][0] + 1;
785 result
.f_code
[1][1] = pic
->f_code
[1][1] + 1;
786 result
.intra_dc_precision
= pic
->intra_dc_precision
;
787 result
.pic_structure
= pic
->picture_structure
;
788 result
.top_field_first
= pic
->top_field_first
;
789 result
.frame_pred_frame_dct
= pic
->frame_pred_frame_dct
;
790 result
.concealment_motion_vectors
= pic
->concealment_motion_vectors
;
791 result
.q_scale_type
= pic
->q_scale_type
;
792 result
.intra_vlc_format
= pic
->intra_vlc_format
;
793 result
.alternate_scan
= pic
->alternate_scan
;
798 /* get mpeg4 specific msg bits */
799 static struct ruvd_mpeg4
get_mpeg4_msg(struct ruvd_decoder
*dec
,
800 struct pipe_mpeg4_picture_desc
*pic
)
802 struct ruvd_mpeg4 result
;
805 memset(&result
, 0, sizeof(result
));
806 result
.decoded_pic_idx
= dec
->frame_number
;
807 for (i
= 0; i
< 2; ++i
)
808 result
.ref_pic_idx
[i
] = get_ref_pic_idx(dec
, pic
->ref
[i
]);
810 result
.variant_type
= 0;
811 result
.profile_and_level_indication
= 0xF0; // ASP Level0
813 result
.video_object_layer_verid
= 0x5; // advanced simple
814 result
.video_object_layer_shape
= 0x0; // rectangular
816 result
.video_object_layer_width
= dec
->base
.width
;
817 result
.video_object_layer_height
= dec
->base
.height
;
819 result
.vop_time_increment_resolution
= pic
->vop_time_increment_resolution
;
821 result
.flags
|= pic
->short_video_header
<< 0;
822 //result.flags |= obmc_disable << 1;
823 result
.flags
|= pic
->interlaced
<< 2;
824 result
.flags
|= 1 << 3; // load_intra_quant_mat
825 result
.flags
|= 1 << 4; // load_nonintra_quant_mat
826 result
.flags
|= pic
->quarter_sample
<< 5;
827 result
.flags
|= 1 << 6; // complexity_estimation_disable
828 result
.flags
|= pic
->resync_marker_disable
<< 7;
829 //result.flags |= data_partitioned << 8;
830 //result.flags |= reversible_vlc << 9;
831 result
.flags
|= 0 << 10; // newpred_enable
832 result
.flags
|= 0 << 11; // reduced_resolution_vop_enable
833 //result.flags |= scalability << 12;
834 //result.flags |= is_object_layer_identifier << 13;
835 //result.flags |= fixed_vop_rate << 14;
836 //result.flags |= newpred_segment_type << 15;
838 result
.quant_type
= pic
->quant_type
;
840 for (i
= 0; i
< 64; ++i
) {
841 result
.intra_quant_mat
[i
] = pic
->intra_matrix
[vl_zscan_normal
[i
]];
842 result
.nonintra_quant_mat
[i
] = pic
->non_intra_matrix
[vl_zscan_normal
[i
]];
848 uint8_t vop_coding_type
849 uint8_t vop_fcode_forward
850 uint8_t vop_fcode_backward
851 uint8_t rounding_control
852 uint8_t alternate_vertical_scan_flag
853 uint8_t top_field_first
859 static void get_mjpeg_slice_header(struct ruvd_decoder
*dec
, struct pipe_mjpeg_picture_desc
*pic
)
861 int size
= 0, saved_size
, len_pos
, i
;
863 uint8_t *buf
= dec
->bs_ptr
;
876 for (i
= 0; i
< 4; ++i
) {
877 if (pic
->quantization_table
.load_quantiser_table
[i
] == 0)
881 memcpy((buf
+ size
), &pic
->quantization_table
.quantiser_table
[i
], 64);
885 bs
= (uint16_t*)&buf
[len_pos
];
886 *bs
= util_bswap16(size
- 4);
897 for (i
= 0; i
< 2; ++i
) {
898 if (pic
->huffman_table
.load_huffman_table
[i
] == 0)
901 buf
[size
++] = 0x00 | i
;
902 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].num_dc_codes
, 16);
904 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].dc_values
, 12);
908 for (i
= 0; i
< 2; ++i
) {
909 if (pic
->huffman_table
.load_huffman_table
[i
] == 0)
912 buf
[size
++] = 0x10 | i
;
913 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].num_ac_codes
, 16);
915 memcpy((buf
+ size
), &pic
->huffman_table
.table
[i
].ac_values
, 162);
919 bs
= (uint16_t*)&buf
[len_pos
];
920 *bs
= util_bswap16(size
- saved_size
- 2);
925 if (pic
->slice_parameter
.restart_interval
) {
930 bs
= (uint16_t*)&buf
[size
++];
931 *bs
= util_bswap16(pic
->slice_parameter
.restart_interval
);
944 bs
= (uint16_t*)&buf
[size
++];
945 *bs
= util_bswap16(pic
->picture_parameter
.picture_height
);
948 bs
= (uint16_t*)&buf
[size
++];
949 *bs
= util_bswap16(pic
->picture_parameter
.picture_width
);
952 buf
[size
++] = pic
->picture_parameter
.num_components
;
954 for (i
= 0; i
< pic
->picture_parameter
.num_components
; ++i
) {
955 buf
[size
++] = pic
->picture_parameter
.components
[i
].component_id
;
956 buf
[size
++] = pic
->picture_parameter
.components
[i
].h_sampling_factor
<< 4 |
957 pic
->picture_parameter
.components
[i
].v_sampling_factor
;
958 buf
[size
++] = pic
->picture_parameter
.components
[i
].quantiser_table_selector
;
961 bs
= (uint16_t*)&buf
[len_pos
];
962 *bs
= util_bswap16(size
- saved_size
- 2);
973 buf
[size
++] = pic
->slice_parameter
.num_components
;
975 for (i
= 0; i
< pic
->slice_parameter
.num_components
; ++i
) {
976 buf
[size
++] = pic
->slice_parameter
.components
[i
].component_selector
;
977 buf
[size
++] = pic
->slice_parameter
.components
[i
].dc_table_selector
<< 4 |
978 pic
->slice_parameter
.components
[i
].ac_table_selector
;
985 bs
= (uint16_t*)&buf
[len_pos
];
986 *bs
= util_bswap16(size
- saved_size
- 2);
989 dec
->bs_size
+= size
;
993 * destroy this video decoder
995 static void ruvd_destroy(struct pipe_video_codec
*decoder
)
997 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1002 map_msg_fb_it_buf(dec
);
1003 dec
->msg
->size
= sizeof(*dec
->msg
);
1004 dec
->msg
->msg_type
= RUVD_MSG_DESTROY
;
1005 dec
->msg
->stream_handle
= dec
->stream_handle
;
1010 dec
->ws
->cs_destroy(dec
->cs
);
1012 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1013 rvid_destroy_buffer(&dec
->msg_fb_it_buffers
[i
]);
1014 rvid_destroy_buffer(&dec
->bs_buffers
[i
]);
1017 rvid_destroy_buffer(&dec
->dpb
);
1018 rvid_destroy_buffer(&dec
->ctx
);
1019 rvid_destroy_buffer(&dec
->sessionctx
);
1025 * start decoding of a new frame
1027 static void ruvd_begin_frame(struct pipe_video_codec
*decoder
,
1028 struct pipe_video_buffer
*target
,
1029 struct pipe_picture_desc
*picture
)
1031 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1036 frame
= ++dec
->frame_number
;
1037 vl_video_buffer_set_associated_data(target
, decoder
, (void *)frame
,
1038 &ruvd_destroy_associated_data
);
1041 dec
->bs_ptr
= dec
->ws
->buffer_map(
1042 dec
->bs_buffers
[dec
->cur_buffer
].res
->buf
,
1043 dec
->cs
, PIPE_TRANSFER_WRITE
| RADEON_TRANSFER_TEMPORARY
);
1047 * decode a macroblock
1049 static void ruvd_decode_macroblock(struct pipe_video_codec
*decoder
,
1050 struct pipe_video_buffer
*target
,
1051 struct pipe_picture_desc
*picture
,
1052 const struct pipe_macroblock
*macroblocks
,
1053 unsigned num_macroblocks
)
1055 /* not supported (yet) */
1060 * decode a bitstream
1062 static void ruvd_decode_bitstream(struct pipe_video_codec
*decoder
,
1063 struct pipe_video_buffer
*target
,
1064 struct pipe_picture_desc
*picture
,
1065 unsigned num_buffers
,
1066 const void * const *buffers
,
1067 const unsigned *sizes
)
1069 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1070 enum pipe_video_format format
= u_reduce_video_profile(picture
->profile
);
1078 if (format
== PIPE_VIDEO_FORMAT_JPEG
)
1079 get_mjpeg_slice_header(dec
, (struct pipe_mjpeg_picture_desc
*)picture
);
1081 for (i
= 0; i
< num_buffers
; ++i
) {
1082 struct rvid_buffer
*buf
= &dec
->bs_buffers
[dec
->cur_buffer
];
1083 unsigned new_size
= dec
->bs_size
+ sizes
[i
];
1085 if (format
== PIPE_VIDEO_FORMAT_JPEG
)
1086 new_size
+= 2; /* save for EOI */
1088 if (new_size
> buf
->res
->buf
->size
) {
1089 dec
->ws
->buffer_unmap(buf
->res
->buf
);
1090 if (!rvid_resize_buffer(dec
->screen
, dec
->cs
, buf
, new_size
)) {
1091 RVID_ERR("Can't resize bitstream buffer!");
1095 dec
->bs_ptr
= dec
->ws
->buffer_map(buf
->res
->buf
, dec
->cs
,
1096 PIPE_TRANSFER_WRITE
|
1097 RADEON_TRANSFER_TEMPORARY
);
1101 dec
->bs_ptr
+= dec
->bs_size
;
1104 memcpy(dec
->bs_ptr
, buffers
[i
], sizes
[i
]);
1105 dec
->bs_size
+= sizes
[i
];
1106 dec
->bs_ptr
+= sizes
[i
];
1109 if (format
== PIPE_VIDEO_FORMAT_JPEG
) {
1110 ((uint8_t *)dec
->bs_ptr
)[0] = 0xff; /* EOI */
1111 ((uint8_t *)dec
->bs_ptr
)[1] = 0xd9;
1118 * end decoding of the current frame
1120 static void ruvd_end_frame(struct pipe_video_codec
*decoder
,
1121 struct pipe_video_buffer
*target
,
1122 struct pipe_picture_desc
*picture
)
1124 struct ruvd_decoder
*dec
= (struct ruvd_decoder
*)decoder
;
1125 struct pb_buffer
*dt
;
1126 struct rvid_buffer
*msg_fb_it_buf
, *bs_buf
;
1134 msg_fb_it_buf
= &dec
->msg_fb_it_buffers
[dec
->cur_buffer
];
1135 bs_buf
= &dec
->bs_buffers
[dec
->cur_buffer
];
1137 bs_size
= align(dec
->bs_size
, 128);
1138 memset(dec
->bs_ptr
, 0, bs_size
- dec
->bs_size
);
1139 dec
->ws
->buffer_unmap(bs_buf
->res
->buf
);
1141 map_msg_fb_it_buf(dec
);
1142 dec
->msg
->size
= sizeof(*dec
->msg
);
1143 dec
->msg
->msg_type
= RUVD_MSG_DECODE
;
1144 dec
->msg
->stream_handle
= dec
->stream_handle
;
1145 dec
->msg
->status_report_feedback_number
= dec
->frame_number
;
1147 dec
->msg
->body
.decode
.stream_type
= dec
->stream_type
;
1148 dec
->msg
->body
.decode
.decode_flags
= 0x1;
1149 dec
->msg
->body
.decode
.width_in_samples
= dec
->base
.width
;
1150 dec
->msg
->body
.decode
.height_in_samples
= dec
->base
.height
;
1152 if ((picture
->profile
== PIPE_VIDEO_PROFILE_VC1_SIMPLE
) ||
1153 (picture
->profile
== PIPE_VIDEO_PROFILE_VC1_MAIN
)) {
1154 dec
->msg
->body
.decode
.width_in_samples
= align(dec
->msg
->body
.decode
.width_in_samples
, 16) / 16;
1155 dec
->msg
->body
.decode
.height_in_samples
= align(dec
->msg
->body
.decode
.height_in_samples
, 16) / 16;
1159 dec
->msg
->body
.decode
.dpb_size
= dec
->dpb
.res
->buf
->size
;
1160 dec
->msg
->body
.decode
.bsd_size
= bs_size
;
1161 dec
->msg
->body
.decode
.db_pitch
= align(dec
->base
.width
, get_db_pitch_alignment(dec
));
1163 dt
= dec
->set_dtb(dec
->msg
, (struct vl_video_buffer
*)target
);
1165 switch (u_reduce_video_profile(picture
->profile
)) {
1166 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
1167 dec
->msg
->body
.decode
.codec
.h264
= get_h264_msg(dec
, (struct pipe_h264_picture_desc
*)picture
);
1170 case PIPE_VIDEO_FORMAT_VC1
:
1171 dec
->msg
->body
.decode
.codec
.vc1
= get_vc1_msg((struct pipe_vc1_picture_desc
*)picture
);
1174 case PIPE_VIDEO_FORMAT_MPEG12
:
1175 dec
->msg
->body
.decode
.codec
.mpeg2
= get_mpeg2_msg(dec
, (struct pipe_mpeg12_picture_desc
*)picture
);
1178 case PIPE_VIDEO_FORMAT_MPEG4
:
1179 dec
->msg
->body
.decode
.codec
.mpeg4
= get_mpeg4_msg(dec
, (struct pipe_mpeg4_picture_desc
*)picture
);
1182 case PIPE_VIDEO_FORMAT_JPEG
:
1190 dec
->msg
->body
.decode
.db_surf_tile_config
= dec
->msg
->body
.decode
.dt_surf_tile_config
;
1191 dec
->msg
->body
.decode
.extension_support
= 0x1;
1193 /* set at least the feedback buffer size */
1194 dec
->fb
[0] = dec
->fb_size
;
1199 send_cmd(dec
, RUVD_CMD_DPB_BUFFER
, dec
->dpb
.res
->buf
, 0,
1200 RADEON_USAGE_READWRITE
, RADEON_DOMAIN_VRAM
);
1203 send_cmd(dec
, RUVD_CMD_CONTEXT_BUFFER
, dec
->ctx
.res
->buf
, 0,
1204 RADEON_USAGE_READWRITE
, RADEON_DOMAIN_VRAM
);
1205 send_cmd(dec
, RUVD_CMD_BITSTREAM_BUFFER
, bs_buf
->res
->buf
,
1206 0, RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
1207 send_cmd(dec
, RUVD_CMD_DECODING_TARGET_BUFFER
, dt
, 0,
1208 RADEON_USAGE_WRITE
, RADEON_DOMAIN_VRAM
);
1209 send_cmd(dec
, RUVD_CMD_FEEDBACK_BUFFER
, msg_fb_it_buf
->res
->buf
,
1210 FB_BUFFER_OFFSET
, RADEON_USAGE_WRITE
, RADEON_DOMAIN_GTT
);
1212 send_cmd(dec
, RUVD_CMD_ITSCALING_TABLE_BUFFER
, msg_fb_it_buf
->res
->buf
,
1213 FB_BUFFER_OFFSET
+ dec
->fb_size
, RADEON_USAGE_READ
, RADEON_DOMAIN_GTT
);
1214 set_reg(dec
, dec
->reg
.cntl
, 1);
1216 flush(dec
, PIPE_FLUSH_ASYNC
);
1221 * flush any outstanding command buffers to the hardware
1223 static void ruvd_flush(struct pipe_video_codec
*decoder
)
1228 * create and UVD decoder
1230 struct pipe_video_codec
*ruvd_create_decoder(struct pipe_context
*context
,
1231 const struct pipe_video_codec
*templ
,
1232 ruvd_set_dtb set_dtb
)
1234 struct radeon_winsys
* ws
= ((struct r600_common_context
*)context
)->ws
;
1235 struct r600_common_context
*rctx
= (struct r600_common_context
*)context
;
1237 unsigned width
= templ
->width
, height
= templ
->height
;
1238 unsigned bs_buf_size
;
1239 struct radeon_info info
;
1240 struct ruvd_decoder
*dec
;
1243 ws
->query_info(ws
, &info
);
1245 switch(u_reduce_video_profile(templ
->profile
)) {
1246 case PIPE_VIDEO_FORMAT_MPEG12
:
1247 if (templ
->entrypoint
> PIPE_VIDEO_ENTRYPOINT_BITSTREAM
|| info
.family
< CHIP_PALM
)
1248 return vl_create_mpeg12_decoder(context
, templ
);
1251 case PIPE_VIDEO_FORMAT_MPEG4
:
1252 width
= align(width
, VL_MACROBLOCK_WIDTH
);
1253 height
= align(height
, VL_MACROBLOCK_HEIGHT
);
1255 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
1256 width
= align(width
, VL_MACROBLOCK_WIDTH
);
1257 height
= align(height
, VL_MACROBLOCK_HEIGHT
);
1265 dec
= CALLOC_STRUCT(ruvd_decoder
);
1270 dec
->use_legacy
= true;
1273 dec
->base
.context
= context
;
1274 dec
->base
.width
= width
;
1275 dec
->base
.height
= height
;
1277 dec
->base
.destroy
= ruvd_destroy
;
1278 dec
->base
.begin_frame
= ruvd_begin_frame
;
1279 dec
->base
.decode_macroblock
= ruvd_decode_macroblock
;
1280 dec
->base
.decode_bitstream
= ruvd_decode_bitstream
;
1281 dec
->base
.end_frame
= ruvd_end_frame
;
1282 dec
->base
.flush
= ruvd_flush
;
1284 dec
->stream_type
= profile2stream_type(dec
, info
.family
);
1285 dec
->set_dtb
= set_dtb
;
1286 dec
->stream_handle
= rvid_alloc_stream_handle();
1287 dec
->screen
= context
->screen
;
1289 dec
->cs
= ws
->cs_create(rctx
->ctx
, RING_UVD
, NULL
, NULL
, false);
1291 RVID_ERR("Can't get command submission context.\n");
1295 dec
->fb_size
= FB_BUFFER_SIZE
;
1296 bs_buf_size
= width
* height
* (512 / (16 * 16));
1297 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1298 unsigned msg_fb_it_size
= FB_BUFFER_OFFSET
+ dec
->fb_size
;
1299 STATIC_ASSERT(sizeof(struct ruvd_msg
) <= FB_BUFFER_OFFSET
);
1301 msg_fb_it_size
+= IT_SCALING_TABLE_SIZE
;
1302 if (!rvid_create_buffer(dec
->screen
, &dec
->msg_fb_it_buffers
[i
],
1303 msg_fb_it_size
, PIPE_USAGE_STAGING
)) {
1304 RVID_ERR("Can't allocated message buffers.\n");
1308 if (!rvid_create_buffer(dec
->screen
, &dec
->bs_buffers
[i
],
1309 bs_buf_size
, PIPE_USAGE_STAGING
)) {
1310 RVID_ERR("Can't allocated bitstream buffers.\n");
1314 rvid_clear_buffer(context
, &dec
->msg_fb_it_buffers
[i
]);
1315 rvid_clear_buffer(context
, &dec
->bs_buffers
[i
]);
1318 dpb_size
= calc_dpb_size(dec
);
1320 if (!rvid_create_buffer(dec
->screen
, &dec
->dpb
, dpb_size
, PIPE_USAGE_DEFAULT
)) {
1321 RVID_ERR("Can't allocated dpb.\n");
1324 rvid_clear_buffer(context
, &dec
->dpb
);
1327 dec
->reg
.data0
= RUVD_GPCOM_VCPU_DATA0
;
1328 dec
->reg
.data1
= RUVD_GPCOM_VCPU_DATA1
;
1329 dec
->reg
.cmd
= RUVD_GPCOM_VCPU_CMD
;
1330 dec
->reg
.cntl
= RUVD_ENGINE_CNTL
;
1332 map_msg_fb_it_buf(dec
);
1333 dec
->msg
->size
= sizeof(*dec
->msg
);
1334 dec
->msg
->msg_type
= RUVD_MSG_CREATE
;
1335 dec
->msg
->stream_handle
= dec
->stream_handle
;
1336 dec
->msg
->body
.create
.stream_type
= dec
->stream_type
;
1337 dec
->msg
->body
.create
.width_in_samples
= dec
->base
.width
;
1338 dec
->msg
->body
.create
.height_in_samples
= dec
->base
.height
;
1339 dec
->msg
->body
.create
.dpb_size
= dpb_size
;
1350 if (dec
->cs
) dec
->ws
->cs_destroy(dec
->cs
);
1352 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
1353 rvid_destroy_buffer(&dec
->msg_fb_it_buffers
[i
]);
1354 rvid_destroy_buffer(&dec
->bs_buffers
[i
]);
1357 rvid_destroy_buffer(&dec
->dpb
);
1358 rvid_destroy_buffer(&dec
->ctx
);
1359 rvid_destroy_buffer(&dec
->sessionctx
);
1366 /* calculate top/bottom offset */
1367 static unsigned texture_offset(struct radeon_surf
*surface
, unsigned layer
)
1369 return surface
->u
.legacy
.level
[0].offset
+
1370 layer
* (uint64_t)surface
->u
.legacy
.level
[0].slice_size_dw
* 4;
1373 /* hw encode the aspect of macro tiles */
1374 static unsigned macro_tile_aspect(unsigned macro_tile_aspect
)
1376 switch (macro_tile_aspect
) {
1378 case 1: macro_tile_aspect
= 0; break;
1379 case 2: macro_tile_aspect
= 1; break;
1380 case 4: macro_tile_aspect
= 2; break;
1381 case 8: macro_tile_aspect
= 3; break;
1383 return macro_tile_aspect
;
1386 /* hw encode the bank width and height */
1387 static unsigned bank_wh(unsigned bankwh
)
1391 case 1: bankwh
= 0; break;
1392 case 2: bankwh
= 1; break;
1393 case 4: bankwh
= 2; break;
1394 case 8: bankwh
= 3; break;
1400 * fill decoding target field from the luma and chroma surfaces
1402 void ruvd_set_dt_surfaces(struct ruvd_msg
*msg
, struct radeon_surf
*luma
,
1403 struct radeon_surf
*chroma
)
1405 msg
->body
.decode
.dt_pitch
= luma
->u
.legacy
.level
[0].nblk_x
* luma
->blk_w
;
1406 switch (luma
->u
.legacy
.level
[0].mode
) {
1407 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
1408 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_LINEAR
;
1409 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_LINEAR
;
1411 case RADEON_SURF_MODE_1D
:
1412 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_8X8
;
1413 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_1D_THIN
;
1415 case RADEON_SURF_MODE_2D
:
1416 msg
->body
.decode
.dt_tiling_mode
= RUVD_TILE_8X8
;
1417 msg
->body
.decode
.dt_array_mode
= RUVD_ARRAY_MODE_2D_THIN
;
1424 msg
->body
.decode
.dt_luma_top_offset
= texture_offset(luma
, 0);
1426 msg
->body
.decode
.dt_chroma_top_offset
= texture_offset(chroma
, 0);
1427 if (msg
->body
.decode
.dt_field_mode
) {
1428 msg
->body
.decode
.dt_luma_bottom_offset
= texture_offset(luma
, 1);
1430 msg
->body
.decode
.dt_chroma_bottom_offset
= texture_offset(chroma
, 1);
1432 msg
->body
.decode
.dt_luma_bottom_offset
= msg
->body
.decode
.dt_luma_top_offset
;
1433 msg
->body
.decode
.dt_chroma_bottom_offset
= msg
->body
.decode
.dt_chroma_top_offset
;
1437 assert(luma
->u
.legacy
.bankw
== chroma
->u
.legacy
.bankw
);
1438 assert(luma
->u
.legacy
.bankh
== chroma
->u
.legacy
.bankh
);
1439 assert(luma
->u
.legacy
.mtilea
== chroma
->u
.legacy
.mtilea
);
1442 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_BANK_WIDTH(bank_wh(luma
->u
.legacy
.bankw
));
1443 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_BANK_HEIGHT(bank_wh(luma
->u
.legacy
.bankh
));
1444 msg
->body
.decode
.dt_surf_tile_config
|= RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma
->u
.legacy
.mtilea
));