radeonsi: remove r600_common_screen
[mesa.git] / src / gallium / drivers / radeon / radeon_uvd.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <sys/types.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <stdio.h>
33
34 #include "pipe/p_video_codec.h"
35
36 #include "util/u_memory.h"
37 #include "util/u_video.h"
38
39 #include "vl/vl_defines.h"
40 #include "vl/vl_mpeg12_decoder.h"
41
42 #include "radeonsi/si_pipe.h"
43 #include "radeon_video.h"
44 #include "radeon_uvd.h"
45
46 #define NUM_BUFFERS 4
47
48 #define NUM_MPEG2_REFS 6
49 #define NUM_H264_REFS 17
50 #define NUM_VC1_REFS 5
51
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)
57
58 /* UVD decoder representation */
59 struct ruvd_decoder {
60 struct pipe_video_codec base;
61
62 ruvd_set_dtb set_dtb;
63
64 unsigned stream_handle;
65 unsigned stream_type;
66 unsigned frame_number;
67
68 struct pipe_screen *screen;
69 struct radeon_winsys* ws;
70 struct radeon_winsys_cs* cs;
71
72 unsigned cur_buffer;
73
74 struct rvid_buffer msg_fb_it_buffers[NUM_BUFFERS];
75 struct ruvd_msg *msg;
76 uint32_t *fb;
77 unsigned fb_size;
78 uint8_t *it;
79
80 struct rvid_buffer bs_buffers[NUM_BUFFERS];
81 void* bs_ptr;
82 unsigned bs_size;
83
84 struct rvid_buffer dpb;
85 bool use_legacy;
86 struct rvid_buffer ctx;
87 struct rvid_buffer sessionctx;
88 struct {
89 unsigned data0;
90 unsigned data1;
91 unsigned cmd;
92 unsigned cntl;
93 } reg;
94 };
95
96 /* flush IB to the hardware */
97 static int flush(struct ruvd_decoder *dec, unsigned flags)
98 {
99 return dec->ws->cs_flush(dec->cs, flags, NULL);
100 }
101
102 /* add a new set register command to the IB */
103 static void set_reg(struct ruvd_decoder *dec, unsigned reg, uint32_t val)
104 {
105 radeon_emit(dec->cs, RUVD_PKT0(reg >> 2, 0));
106 radeon_emit(dec->cs, val);
107 }
108
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)
113 {
114 int reloc_idx;
115
116 reloc_idx = dec->ws->cs_add_buffer(dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
117 domain,
118 RADEON_PRIO_UVD);
119 if (!dec->use_legacy) {
120 uint64_t addr;
121 addr = dec->ws->buffer_get_virtual_address(buf);
122 addr = addr + off;
123 set_reg(dec, dec->reg.data0, addr);
124 set_reg(dec, dec->reg.data1, addr >> 32);
125 } else {
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);
129 }
130 set_reg(dec, dec->reg.cmd, cmd << 1);
131 }
132
133 /* do the codec needs an IT buffer ?*/
134 static bool have_it(struct ruvd_decoder *dec)
135 {
136 return dec->stream_type == RUVD_CODEC_H264_PERF ||
137 dec->stream_type == RUVD_CODEC_H265;
138 }
139
140 /* map the next available message/feedback/itscaling buffer */
141 static void map_msg_fb_it_buf(struct ruvd_decoder *dec)
142 {
143 struct rvid_buffer* buf;
144 uint8_t *ptr;
145
146 /* grab the current message/feedback buffer */
147 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
148
149 /* and map it for CPU access */
150 ptr = dec->ws->buffer_map(buf->res->buf, dec->cs, PIPE_TRANSFER_WRITE);
151
152 /* calc buffer offsets */
153 dec->msg = (struct ruvd_msg *)ptr;
154 memset(dec->msg, 0, sizeof(*dec->msg));
155
156 dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
157 if (have_it(dec))
158 dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + dec->fb_size);
159 }
160
161 /* unmap and send a message command to the VCPU */
162 static void send_msg_buf(struct ruvd_decoder *dec)
163 {
164 struct rvid_buffer* buf;
165
166 /* ignore the request if message/feedback buffer isn't mapped */
167 if (!dec->msg || !dec->fb)
168 return;
169
170 /* grab the current message buffer */
171 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
172
173 /* unmap the buffer */
174 dec->ws->buffer_unmap(buf->res->buf);
175 dec->msg = NULL;
176 dec->fb = NULL;
177 dec->it = NULL;
178
179
180 if (dec->sessionctx.res)
181 send_cmd(dec, RUVD_CMD_SESSION_CONTEXT_BUFFER,
182 dec->sessionctx.res->buf, 0, RADEON_USAGE_READWRITE,
183 RADEON_DOMAIN_VRAM);
184
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);
188 }
189
190 /* cycle to the next set of buffers */
191 static void next_buffer(struct ruvd_decoder *dec)
192 {
193 ++dec->cur_buffer;
194 dec->cur_buffer %= NUM_BUFFERS;
195 }
196
197 /* convert the profile into something UVD understands */
198 static uint32_t profile2stream_type(struct ruvd_decoder *dec, unsigned family)
199 {
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;
204
205 case PIPE_VIDEO_FORMAT_VC1:
206 return RUVD_CODEC_VC1;
207
208 case PIPE_VIDEO_FORMAT_MPEG12:
209 return RUVD_CODEC_MPEG2;
210
211 case PIPE_VIDEO_FORMAT_MPEG4:
212 return RUVD_CODEC_MPEG4;
213
214 case PIPE_VIDEO_FORMAT_HEVC:
215 return RUVD_CODEC_H265;
216
217 case PIPE_VIDEO_FORMAT_JPEG:
218 return RUVD_CODEC_MJPEG;
219
220 default:
221 assert(0);
222 return 0;
223 }
224 }
225
226 static unsigned calc_ctx_size_h264_perf(struct ruvd_decoder *dec)
227 {
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);
231
232 unsigned max_references = dec->base.max_references + 1;
233
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);
237
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) {
242 case 30:
243 num_dpb_buffer = 8100 / fs_in_mb;
244 break;
245 case 31:
246 num_dpb_buffer = 18000 / fs_in_mb;
247 break;
248 case 32:
249 num_dpb_buffer = 20480 / fs_in_mb;
250 break;
251 case 41:
252 num_dpb_buffer = 32768 / fs_in_mb;
253 break;
254 case 42:
255 num_dpb_buffer = 34816 / fs_in_mb;
256 break;
257 case 50:
258 num_dpb_buffer = 110400 / fs_in_mb;
259 break;
260 case 51:
261 num_dpb_buffer = 184320 / fs_in_mb;
262 break;
263 default:
264 num_dpb_buffer = 184320 / fs_in_mb;
265 break;
266 }
267 num_dpb_buffer++;
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);
270 } else {
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);
275 }
276
277 return ctx_size;
278 }
279
280 static unsigned calc_ctx_size_h265_main(struct ruvd_decoder *dec)
281 {
282 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
283 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
284
285 unsigned max_references = dec->base.max_references + 1;
286
287 if (dec->base.width * dec->base.height >= 4096*2000)
288 max_references = MAX2(max_references, 8);
289 else
290 max_references = MAX2(max_references, 17);
291
292 width = align (width, 16);
293 height = align (height, 16);
294 return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
295 }
296
297 static unsigned calc_ctx_size_h265_main10(struct ruvd_decoder *dec, struct pipe_h265_picture_desc *pic)
298 {
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);
302
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;
306
307 unsigned max_references = dec->base.max_references + 1;
308
309 if (dec->base.width * dec->base.height >= 4096*2000)
310 max_references = MAX2(max_references, 8);
311 else
312 max_references = MAX2(max_references, 17);
313
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;
316
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;
319
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);
323
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);
326
327 return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
328 }
329
330 static unsigned get_db_pitch_alignment(struct ruvd_decoder *dec)
331 {
332 if (((struct si_screen*)dec->screen)->info.family < CHIP_VEGA10)
333 return 16;
334 else
335 return 32;
336 }
337
338 /* calculate size of reference picture buffer */
339 static unsigned calc_dpb_size(struct ruvd_decoder *dec)
340 {
341 unsigned width_in_mb, height_in_mb, image_size, dpb_size;
342
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);
346
347 // always one more for currently decoded picture
348 unsigned max_references = dec->base.max_references + 1;
349
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);
354
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);
358
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;
364
365 if (dec->stream_type == RUVD_CODEC_H264_PERF)
366 alignment = 256;
367 switch(dec->base.level) {
368 case 30:
369 num_dpb_buffer = 8100 / fs_in_mb;
370 break;
371 case 31:
372 num_dpb_buffer = 18000 / fs_in_mb;
373 break;
374 case 32:
375 num_dpb_buffer = 20480 / fs_in_mb;
376 break;
377 case 41:
378 num_dpb_buffer = 32768 / fs_in_mb;
379 break;
380 case 42:
381 num_dpb_buffer = 34816 / fs_in_mb;
382 break;
383 case 50:
384 num_dpb_buffer = 110400 / fs_in_mb;
385 break;
386 case 51:
387 num_dpb_buffer = 184320 / fs_in_mb;
388 break;
389 default:
390 num_dpb_buffer = 184320 / fs_in_mb;
391 break;
392 }
393 num_dpb_buffer++;
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);
400 }
401 } else {
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;
410 // IT surface buffer
411 dpb_size += width_in_mb * height_in_mb * 32;
412 }
413 }
414 break;
415 }
416
417 case PIPE_VIDEO_FORMAT_HEVC:
418 if (dec->base.width * dec->base.height >= 4096*2000)
419 max_references = MAX2(max_references, 8);
420 else
421 max_references = MAX2(max_references, 17);
422
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;
427 else
428 dpb_size = align((align(width, get_db_pitch_alignment(dec)) * height * 3) / 2, 256) * max_references;
429 break;
430
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);
434
435 // reference picture buffer
436 dpb_size = image_size * max_references;
437
438 // CONTEXT_BUFFER
439 dpb_size += width_in_mb * height_in_mb * 128;
440
441 // IT surface buffer
442 dpb_size += width_in_mb * 64;
443
444 // DB surface buffer
445 dpb_size += width_in_mb * 128;
446
447 // BP
448 dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
449 break;
450
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;
454 break;
455
456 case PIPE_VIDEO_FORMAT_MPEG4:
457 // reference picture buffer
458 dpb_size = image_size * max_references;
459
460 // CM
461 dpb_size += width_in_mb * height_in_mb * 64;
462
463 // IT surface buffer
464 dpb_size += align(width_in_mb * height_in_mb * 32, 64);
465
466 dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
467 break;
468
469 case PIPE_VIDEO_FORMAT_JPEG:
470 dpb_size = 0;
471 break;
472
473 default:
474 // something is missing here
475 assert(0);
476
477 // at least use a sane default value
478 dpb_size = 32 * 1024 * 1024;
479 break;
480 }
481 return dpb_size;
482 }
483
484 /* free associated data in the video buffer callback */
485 static void ruvd_destroy_associated_data(void *data)
486 {
487 /* NOOP, since we only use an intptr */
488 }
489
490 /* get h264 specific message bits */
491 static struct ruvd_h264 get_h264_msg(struct ruvd_decoder *dec, struct pipe_h264_picture_desc *pic)
492 {
493 struct ruvd_h264 result;
494
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;
500 break;
501
502 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
503 result.profile = RUVD_H264_PROFILE_MAIN;
504 break;
505
506 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
507 result.profile = RUVD_H264_PROFILE_HIGH;
508 break;
509
510 default:
511 assert(0);
512 break;
513 }
514
515 result.level = dec->base.level;
516
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;
522
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;
528
529 switch (dec->base.chroma_format) {
530 case PIPE_VIDEO_CHROMA_FORMAT_NONE:
531 /* TODO: assert? */
532 break;
533 case PIPE_VIDEO_CHROMA_FORMAT_400:
534 result.chroma_format = 0;
535 break;
536 case PIPE_VIDEO_CHROMA_FORMAT_420:
537 result.chroma_format = 1;
538 break;
539 case PIPE_VIDEO_CHROMA_FORMAT_422:
540 result.chroma_format = 2;
541 break;
542 case PIPE_VIDEO_CHROMA_FORMAT_444:
543 result.chroma_format = 3;
544 break;
545 }
546
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;
556
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;
563
564 memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6*16);
565 memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2*64);
566
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);
570 }
571
572 result.num_ref_frames = pic->num_ref_frames;
573
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;
576
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);
582
583 result.decoded_pic_idx = pic->frame_num;
584
585 return result;
586 }
587
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)
591 {
592 struct ruvd_h265 result;
593 unsigned i;
594
595 memset(&result, 0, sizeof(result));
596
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;
611
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;
628
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; ???
651
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;
665
666 for (i = 0; i < 19; ++i)
667 result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
668
669 for (i = 0; i < 21; ++i)
670 result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
671
672 result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
673 result.curr_idx = pic->CurrPicOrderCntVal;
674 result.curr_poc = pic->CurrPicOrderCntVal;
675
676 vl_video_buffer_set_associated_data(target, &dec->base,
677 (void *)(uintptr_t)pic->CurrPicOrderCntVal,
678 &ruvd_destroy_associated_data);
679
680 for (i = 0; i < 16; ++i) {
681 struct pipe_video_buffer *ref = pic->ref[i];
682 uintptr_t ref_pic = 0;
683
684 result.poc_list[i] = pic->PicOrderCntVal[i];
685
686 if (ref)
687 ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
688 else
689 ref_pic = 0x7F;
690 result.ref_pic_list[i] = ref_pic;
691 }
692
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;
697 }
698
699 for (i = 0; i < pic->NumPocStCurrBefore; ++i)
700 result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
701
702 for (i = 0; i < pic->NumPocStCurrAfter; ++i)
703 result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
704
705 for (i = 0; i < pic->NumPocLtCurr; ++i)
706 result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
707
708 for (i = 0; i < 6; ++i)
709 result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
710
711 for (i = 0; i < 2; ++i)
712 result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
713
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);
718
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];
722 }
723
724 if (pic->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
725 if (target->buffer_format == PIPE_FORMAT_P016) {
726 result.p010_mode = 1;
727 result.msb_mode = 1;
728 } else {
729 result.luma_10to8 = 5;
730 result.chroma_10to8 = 5;
731 result.sclr_luma10to8 = 4;
732 result.sclr_chroma10to8 = 4;
733 }
734 }
735
736 /* TODO
737 result.highestTid;
738 result.isNonRef;
739
740 IDRPicFlag;
741 RAPPicFlag;
742 NumPocTotalCurr;
743 NumShortTermPictureSliceHeaderBits;
744 NumLongTermPictureSliceHeaderBits;
745
746 IsLongTerm[16];
747 */
748
749 return result;
750 }
751
752 /* get vc1 specific message bits */
753 static struct ruvd_vc1 get_vc1_msg(struct pipe_vc1_picture_desc *pic)
754 {
755 struct ruvd_vc1 result;
756
757 memset(&result, 0, sizeof(result));
758
759 switch(pic->base.profile) {
760 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
761 result.profile = RUVD_VC1_PROFILE_SIMPLE;
762 result.level = 1;
763 break;
764
765 case PIPE_VIDEO_PROFILE_VC1_MAIN:
766 result.profile = RUVD_VC1_PROFILE_MAIN;
767 result.level = 2;
768 break;
769
770 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
771 result.profile = RUVD_VC1_PROFILE_ADVANCED;
772 result.level = 4;
773 break;
774
775 default:
776 assert(0);
777 }
778
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;
786
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;
798
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;
808 }
809
810 result.chroma_format = 1;
811
812 #if 0
813 //(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
814 uint32_t slice_count
815 uint8_t picture_type
816 uint8_t frame_coding_mode
817 uint8_t deblockEnable
818 uint8_t pquant
819 #endif
820
821 return result;
822 }
823
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)
826 {
827 uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
828 uint32_t max = MAX2(dec->frame_number, 1) - 1;
829 uintptr_t frame;
830
831 /* seems to be the most sane fallback */
832 if (!ref)
833 return max;
834
835 /* get the frame number from the associated data */
836 frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
837
838 /* limit the frame number to a valid range */
839 return MAX2(MIN2(frame, max), min);
840 }
841
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)
845 {
846 const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
847 struct ruvd_mpeg2 result;
848 unsigned i;
849
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]);
854
855 result.load_intra_quantiser_matrix = 1;
856 result.load_nonintra_quantiser_matrix = 1;
857
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]];
861 }
862
863 result.profile_and_level_indication = 0;
864 result.chroma_format = 0x1;
865
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;
879
880 return result;
881 }
882
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)
886 {
887 struct ruvd_mpeg4 result;
888 unsigned i;
889
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]);
894
895 result.variant_type = 0;
896 result.profile_and_level_indication = 0xF0; // ASP Level0
897
898 result.video_object_layer_verid = 0x5; // advanced simple
899 result.video_object_layer_shape = 0x0; // rectangular
900
901 result.video_object_layer_width = dec->base.width;
902 result.video_object_layer_height = dec->base.height;
903
904 result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
905
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;
922
923 result.quant_type = pic->quant_type;
924
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]];
928 }
929
930 /*
931 int32_t trd [2]
932 int32_t trb [2]
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
939 */
940
941 return result;
942 }
943
944 static void get_mjpeg_slice_header(struct ruvd_decoder *dec, struct pipe_mjpeg_picture_desc *pic)
945 {
946 int size = 0, saved_size, len_pos, i;
947 uint16_t *bs;
948 uint8_t *buf = dec->bs_ptr;
949
950 /* SOI */
951 buf[size++] = 0xff;
952 buf[size++] = 0xd8;
953
954 /* DQT */
955 buf[size++] = 0xff;
956 buf[size++] = 0xdb;
957
958 len_pos = size++;
959 size++;
960
961 for (i = 0; i < 4; ++i) {
962 if (pic->quantization_table.load_quantiser_table[i] == 0)
963 continue;
964
965 buf[size++] = i;
966 memcpy((buf + size), &pic->quantization_table.quantiser_table[i], 64);
967 size += 64;
968 }
969
970 bs = (uint16_t*)&buf[len_pos];
971 *bs = util_bswap16(size - 4);
972
973 saved_size = size;
974
975 /* DHT */
976 buf[size++] = 0xff;
977 buf[size++] = 0xc4;
978
979 len_pos = size++;
980 size++;
981
982 for (i = 0; i < 2; ++i) {
983 if (pic->huffman_table.load_huffman_table[i] == 0)
984 continue;
985
986 buf[size++] = 0x00 | i;
987 memcpy((buf + size), &pic->huffman_table.table[i].num_dc_codes, 16);
988 size += 16;
989 memcpy((buf + size), &pic->huffman_table.table[i].dc_values, 12);
990 size += 12;
991 }
992
993 for (i = 0; i < 2; ++i) {
994 if (pic->huffman_table.load_huffman_table[i] == 0)
995 continue;
996
997 buf[size++] = 0x10 | i;
998 memcpy((buf + size), &pic->huffman_table.table[i].num_ac_codes, 16);
999 size += 16;
1000 memcpy((buf + size), &pic->huffman_table.table[i].ac_values, 162);
1001 size += 162;
1002 }
1003
1004 bs = (uint16_t*)&buf[len_pos];
1005 *bs = util_bswap16(size - saved_size - 2);
1006
1007 saved_size = size;
1008
1009 /* DRI */
1010 if (pic->slice_parameter.restart_interval) {
1011 buf[size++] = 0xff;
1012 buf[size++] = 0xdd;
1013 buf[size++] = 0x00;
1014 buf[size++] = 0x04;
1015 bs = (uint16_t*)&buf[size++];
1016 *bs = util_bswap16(pic->slice_parameter.restart_interval);
1017 saved_size = ++size;
1018 }
1019
1020 /* SOF */
1021 buf[size++] = 0xff;
1022 buf[size++] = 0xc0;
1023
1024 len_pos = size++;
1025 size++;
1026
1027 buf[size++] = 0x08;
1028
1029 bs = (uint16_t*)&buf[size++];
1030 *bs = util_bswap16(pic->picture_parameter.picture_height);
1031 size++;
1032
1033 bs = (uint16_t*)&buf[size++];
1034 *bs = util_bswap16(pic->picture_parameter.picture_width);
1035 size++;
1036
1037 buf[size++] = pic->picture_parameter.num_components;
1038
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;
1044 }
1045
1046 bs = (uint16_t*)&buf[len_pos];
1047 *bs = util_bswap16(size - saved_size - 2);
1048
1049 saved_size = size;
1050
1051 /* SOS */
1052 buf[size++] = 0xff;
1053 buf[size++] = 0xda;
1054
1055 len_pos = size++;
1056 size++;
1057
1058 buf[size++] = pic->slice_parameter.num_components;
1059
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;
1064 }
1065
1066 buf[size++] = 0x00;
1067 buf[size++] = 0x3f;
1068 buf[size++] = 0x00;
1069
1070 bs = (uint16_t*)&buf[len_pos];
1071 *bs = util_bswap16(size - saved_size - 2);
1072
1073 dec->bs_ptr += size;
1074 dec->bs_size += size;
1075 }
1076
1077 /**
1078 * destroy this video decoder
1079 */
1080 static void ruvd_destroy(struct pipe_video_codec *decoder)
1081 {
1082 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1083 unsigned i;
1084
1085 assert(decoder);
1086
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;
1091 send_msg_buf(dec);
1092
1093 flush(dec, 0);
1094
1095 dec->ws->cs_destroy(dec->cs);
1096
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]);
1100 }
1101
1102 si_vid_destroy_buffer(&dec->dpb);
1103 si_vid_destroy_buffer(&dec->ctx);
1104 si_vid_destroy_buffer(&dec->sessionctx);
1105
1106 FREE(dec);
1107 }
1108
1109 /**
1110 * start decoding of a new frame
1111 */
1112 static void ruvd_begin_frame(struct pipe_video_codec *decoder,
1113 struct pipe_video_buffer *target,
1114 struct pipe_picture_desc *picture)
1115 {
1116 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1117 uintptr_t frame;
1118
1119 assert(decoder);
1120
1121 frame = ++dec->frame_number;
1122 vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
1123 &ruvd_destroy_associated_data);
1124
1125 dec->bs_size = 0;
1126 dec->bs_ptr = dec->ws->buffer_map(
1127 dec->bs_buffers[dec->cur_buffer].res->buf,
1128 dec->cs, PIPE_TRANSFER_WRITE);
1129 }
1130
1131 /**
1132 * decode a macroblock
1133 */
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)
1139 {
1140 /* not supported (yet) */
1141 assert(0);
1142 }
1143
1144 /**
1145 * decode a bitstream
1146 */
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)
1153 {
1154 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1155 enum pipe_video_format format = u_reduce_video_profile(picture->profile);
1156 unsigned i;
1157
1158 assert(decoder);
1159
1160 if (!dec->bs_ptr)
1161 return;
1162
1163 if (format == PIPE_VIDEO_FORMAT_JPEG)
1164 get_mjpeg_slice_header(dec, (struct pipe_mjpeg_picture_desc*)picture);
1165
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];
1169
1170 if (format == PIPE_VIDEO_FORMAT_JPEG)
1171 new_size += 2; /* save for EOI */
1172
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!");
1177 return;
1178 }
1179
1180 dec->bs_ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1181 PIPE_TRANSFER_WRITE);
1182 if (!dec->bs_ptr)
1183 return;
1184
1185 dec->bs_ptr += dec->bs_size;
1186 }
1187
1188 memcpy(dec->bs_ptr, buffers[i], sizes[i]);
1189 dec->bs_size += sizes[i];
1190 dec->bs_ptr += sizes[i];
1191 }
1192
1193 if (format == PIPE_VIDEO_FORMAT_JPEG) {
1194 ((uint8_t *)dec->bs_ptr)[0] = 0xff; /* EOI */
1195 ((uint8_t *)dec->bs_ptr)[1] = 0xd9;
1196 dec->bs_size += 2;
1197 dec->bs_ptr += 2;
1198 }
1199 }
1200
1201 /**
1202 * end decoding of the current frame
1203 */
1204 static void ruvd_end_frame(struct pipe_video_codec *decoder,
1205 struct pipe_video_buffer *target,
1206 struct pipe_picture_desc *picture)
1207 {
1208 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1209 struct pb_buffer *dt;
1210 struct rvid_buffer *msg_fb_it_buf, *bs_buf;
1211 unsigned bs_size;
1212
1213 assert(decoder);
1214
1215 if (!dec->bs_ptr)
1216 return;
1217
1218 msg_fb_it_buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
1219 bs_buf = &dec->bs_buffers[dec->cur_buffer];
1220
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);
1224
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;
1230
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;
1235
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;
1240 }
1241
1242 if (dec->dpb.res)
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));
1246
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;
1250
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;
1254
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);
1258 break;
1259
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) {
1263 unsigned ctx_size;
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);
1266 else
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");
1270 }
1271 si_vid_clear_buffer(decoder->context, &dec->ctx);
1272 }
1273
1274 if (dec->ctx.res)
1275 dec->msg->body.decode.dpb_reserved = dec->ctx.res->buf->size;
1276 break;
1277
1278 case PIPE_VIDEO_FORMAT_VC1:
1279 dec->msg->body.decode.codec.vc1 = get_vc1_msg((struct pipe_vc1_picture_desc*)picture);
1280 break;
1281
1282 case PIPE_VIDEO_FORMAT_MPEG12:
1283 dec->msg->body.decode.codec.mpeg2 = get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc*)picture);
1284 break;
1285
1286 case PIPE_VIDEO_FORMAT_MPEG4:
1287 dec->msg->body.decode.codec.mpeg4 = get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc*)picture);
1288 break;
1289
1290 case PIPE_VIDEO_FORMAT_JPEG:
1291 break;
1292
1293 default:
1294 assert(0);
1295 return;
1296 }
1297
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;
1300
1301 /* set at least the feedback buffer size */
1302 dec->fb[0] = dec->fb_size;
1303
1304 send_msg_buf(dec);
1305
1306 if (dec->dpb.res)
1307 send_cmd(dec, RUVD_CMD_DPB_BUFFER, dec->dpb.res->buf, 0,
1308 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1309
1310 if (dec->ctx.res)
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);
1319 if (have_it(dec))
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);
1323
1324 flush(dec, RADEON_FLUSH_ASYNC);
1325 next_buffer(dec);
1326 }
1327
1328 /**
1329 * flush any outstanding command buffers to the hardware
1330 */
1331 static void ruvd_flush(struct pipe_video_codec *decoder)
1332 {
1333 }
1334
1335 /**
1336 * create and UVD decoder
1337 */
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)
1341 {
1342 struct radeon_winsys* ws = ((struct r600_common_context *)context)->ws;
1343 struct r600_common_context *rctx = (struct r600_common_context*)context;
1344 unsigned dpb_size;
1345 unsigned width = templ->width, height = templ->height;
1346 unsigned bs_buf_size;
1347 struct ruvd_decoder *dec;
1348 int r, i;
1349
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);
1354
1355 /* fall through */
1356 case PIPE_VIDEO_FORMAT_MPEG4:
1357 width = align(width, VL_MACROBLOCK_WIDTH);
1358 height = align(height, VL_MACROBLOCK_HEIGHT);
1359 break;
1360 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1361 width = align(width, VL_MACROBLOCK_WIDTH);
1362 height = align(height, VL_MACROBLOCK_HEIGHT);
1363 break;
1364
1365 default:
1366 break;
1367 }
1368
1369
1370 dec = CALLOC_STRUCT(ruvd_decoder);
1371
1372 if (!dec)
1373 return NULL;
1374
1375 if (rctx->screen->info.drm_major < 3)
1376 dec->use_legacy = true;
1377
1378 dec->base = *templ;
1379 dec->base.context = context;
1380 dec->base.width = width;
1381 dec->base.height = height;
1382
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;
1389
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;
1394 dec->ws = ws;
1395 dec->cs = ws->cs_create(rctx->ctx, RING_UVD, NULL, NULL);
1396 if (!dec->cs) {
1397 RVID_ERR("Can't get command submission context.\n");
1398 goto error;
1399 }
1400
1401 dec->fb_size = (rctx->family == CHIP_TONGA) ? FB_BUFFER_SIZE_TONGA :
1402 FB_BUFFER_SIZE;
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);
1407 if (have_it(dec))
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");
1412 goto error;
1413 }
1414
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");
1418 goto error;
1419 }
1420
1421 si_vid_clear_buffer(context, &dec->msg_fb_it_buffers[i]);
1422 si_vid_clear_buffer(context, &dec->bs_buffers[i]);
1423 }
1424
1425 dpb_size = calc_dpb_size(dec);
1426 if (dpb_size) {
1427 if (!si_vid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1428 RVID_ERR("Can't allocated dpb.\n");
1429 goto error;
1430 }
1431 si_vid_clear_buffer(context, &dec->dpb);
1432 }
1433
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");
1438 goto error;
1439 }
1440 si_vid_clear_buffer(context, &dec->ctx);
1441 }
1442
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");
1448 goto error;
1449 }
1450 si_vid_clear_buffer(context, &dec->sessionctx);
1451 }
1452
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;
1458 } else {
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;
1463 }
1464
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;
1473 send_msg_buf(dec);
1474 r = flush(dec, 0);
1475 if (r)
1476 goto error;
1477
1478 next_buffer(dec);
1479
1480 return &dec->base;
1481
1482 error:
1483 if (dec->cs) dec->ws->cs_destroy(dec->cs);
1484
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]);
1488 }
1489
1490 si_vid_destroy_buffer(&dec->dpb);
1491 si_vid_destroy_buffer(&dec->ctx);
1492 si_vid_destroy_buffer(&dec->sessionctx);
1493
1494 FREE(dec);
1495
1496 return NULL;
1497 }
1498
1499 /* calculate top/bottom offset */
1500 static unsigned texture_offset(struct radeon_surf *surface, unsigned layer,
1501 enum ruvd_surface_type type)
1502 {
1503 switch (type) {
1504 default:
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;
1508 break;
1509 case RUVD_SURFACE_TYPE_GFX9:
1510 return surface->u.gfx9.surf_offset +
1511 layer * surface->u.gfx9.surf_slice_size;
1512 break;
1513 }
1514 }
1515
1516 /* hw encode the aspect of macro tiles */
1517 static unsigned macro_tile_aspect(unsigned macro_tile_aspect)
1518 {
1519 switch (macro_tile_aspect) {
1520 default:
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;
1525 }
1526 return macro_tile_aspect;
1527 }
1528
1529 /* hw encode the bank width and height */
1530 static unsigned bank_wh(unsigned bankwh)
1531 {
1532 switch (bankwh) {
1533 default:
1534 case 1: bankwh = 0; break;
1535 case 2: bankwh = 1; break;
1536 case 4: bankwh = 2; break;
1537 case 8: bankwh = 3; break;
1538 }
1539 return bankwh;
1540 }
1541
1542 /**
1543 * fill decoding target field from the luma and chroma surfaces
1544 */
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)
1547 {
1548 switch (type) {
1549 default:
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;
1556 break;
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;
1560 break;
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;
1564 break;
1565 default:
1566 assert(0);
1567 break;
1568 }
1569
1570 msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0, type);
1571 if (chroma)
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);
1575 if (chroma)
1576 msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1, type);
1577 } else {
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;
1580 }
1581
1582 if (chroma) {
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);
1586 }
1587
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));
1591 break;
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);
1602 } else {
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;
1605 }
1606 msg->body.decode.dt_surf_tile_config = 0;
1607 break;
1608 }
1609 }