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