radeon/uvd: fix MJPEG quantization table index
[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 struct {
95 unsigned data0;
96 unsigned data1;
97 unsigned cmd;
98 unsigned cntl;
99 } reg;
100 };
101
102 /* flush IB to the hardware */
103 static int flush(struct ruvd_decoder *dec, unsigned flags)
104 {
105 return dec->ws->cs_flush(dec->cs, flags, NULL);
106 }
107
108 /* add a new set register command to the IB */
109 static void set_reg(struct ruvd_decoder *dec, unsigned reg, uint32_t val)
110 {
111 radeon_emit(dec->cs, RUVD_PKT0(reg >> 2, 0));
112 radeon_emit(dec->cs, val);
113 }
114
115 /* send a command to the VCPU through the GPCOM registers */
116 static void send_cmd(struct ruvd_decoder *dec, unsigned cmd,
117 struct pb_buffer* buf, uint32_t off,
118 enum radeon_bo_usage usage, enum radeon_bo_domain domain)
119 {
120 int reloc_idx;
121
122 reloc_idx = dec->ws->cs_add_buffer(dec->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
123 domain,
124 RADEON_PRIO_UVD);
125 if (!dec->use_legacy) {
126 uint64_t addr;
127 addr = dec->ws->buffer_get_virtual_address(buf);
128 addr = addr + off;
129 set_reg(dec, dec->reg.data0, addr);
130 set_reg(dec, dec->reg.data1, addr >> 32);
131 } else {
132 off += dec->ws->buffer_get_reloc_offset(buf);
133 set_reg(dec, RUVD_GPCOM_VCPU_DATA0, off);
134 set_reg(dec, RUVD_GPCOM_VCPU_DATA1, reloc_idx * 4);
135 }
136 set_reg(dec, dec->reg.cmd, cmd << 1);
137 }
138
139 /* do the codec needs an IT buffer ?*/
140 static bool have_it(struct ruvd_decoder *dec)
141 {
142 return dec->stream_type == RUVD_CODEC_H264_PERF ||
143 dec->stream_type == RUVD_CODEC_H265;
144 }
145
146 /* map the next available message/feedback/itscaling buffer */
147 static void map_msg_fb_it_buf(struct ruvd_decoder *dec)
148 {
149 struct rvid_buffer* buf;
150 uint8_t *ptr;
151
152 /* grab the current message/feedback buffer */
153 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
154
155 /* and map it for CPU access */
156 ptr = dec->ws->buffer_map(buf->res->buf, dec->cs, PIPE_TRANSFER_WRITE);
157
158 /* calc buffer offsets */
159 dec->msg = (struct ruvd_msg *)ptr;
160 memset(dec->msg, 0, sizeof(*dec->msg));
161
162 dec->fb = (uint32_t *)(ptr + FB_BUFFER_OFFSET);
163 if (have_it(dec))
164 dec->it = (uint8_t *)(ptr + FB_BUFFER_OFFSET + dec->fb_size);
165 }
166
167 /* unmap and send a message command to the VCPU */
168 static void send_msg_buf(struct ruvd_decoder *dec)
169 {
170 struct rvid_buffer* buf;
171
172 /* ignore the request if message/feedback buffer isn't mapped */
173 if (!dec->msg || !dec->fb)
174 return;
175
176 /* grab the current message buffer */
177 buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
178
179 /* unmap the buffer */
180 dec->ws->buffer_unmap(buf->res->buf);
181 dec->msg = NULL;
182 dec->fb = NULL;
183 dec->it = NULL;
184
185
186 if (dec->sessionctx.res)
187 send_cmd(dec, RUVD_CMD_SESSION_CONTEXT_BUFFER,
188 dec->sessionctx.res->buf, 0, RADEON_USAGE_READWRITE,
189 RADEON_DOMAIN_VRAM);
190
191 /* and send it to the hardware */
192 send_cmd(dec, RUVD_CMD_MSG_BUFFER, buf->res->buf, 0,
193 RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
194 }
195
196 /* cycle to the next set of buffers */
197 static void next_buffer(struct ruvd_decoder *dec)
198 {
199 ++dec->cur_buffer;
200 dec->cur_buffer %= NUM_BUFFERS;
201 }
202
203 /* convert the profile into something UVD understands */
204 static uint32_t profile2stream_type(struct ruvd_decoder *dec, unsigned family)
205 {
206 switch (u_reduce_video_profile(dec->base.profile)) {
207 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
208 return (family >= CHIP_TONGA) ?
209 RUVD_CODEC_H264_PERF : RUVD_CODEC_H264;
210
211 case PIPE_VIDEO_FORMAT_VC1:
212 return RUVD_CODEC_VC1;
213
214 case PIPE_VIDEO_FORMAT_MPEG12:
215 return RUVD_CODEC_MPEG2;
216
217 case PIPE_VIDEO_FORMAT_MPEG4:
218 return RUVD_CODEC_MPEG4;
219
220 case PIPE_VIDEO_FORMAT_HEVC:
221 return RUVD_CODEC_H265;
222
223 case PIPE_VIDEO_FORMAT_JPEG:
224 return RUVD_CODEC_MJPEG;
225
226 default:
227 assert(0);
228 return 0;
229 }
230 }
231
232 static unsigned calc_ctx_size_h264_perf(struct ruvd_decoder *dec)
233 {
234 unsigned width_in_mb, height_in_mb, ctx_size;
235 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
236 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
237
238 unsigned max_references = dec->base.max_references + 1;
239
240 // picture width & height in 16 pixel units
241 width_in_mb = width / VL_MACROBLOCK_WIDTH;
242 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
243
244 if (!dec->use_legacy) {
245 unsigned fs_in_mb = width_in_mb * height_in_mb;
246 unsigned num_dpb_buffer;
247 switch(dec->base.level) {
248 case 30:
249 num_dpb_buffer = 8100 / fs_in_mb;
250 break;
251 case 31:
252 num_dpb_buffer = 18000 / fs_in_mb;
253 break;
254 case 32:
255 num_dpb_buffer = 20480 / fs_in_mb;
256 break;
257 case 41:
258 num_dpb_buffer = 32768 / fs_in_mb;
259 break;
260 case 42:
261 num_dpb_buffer = 34816 / fs_in_mb;
262 break;
263 case 50:
264 num_dpb_buffer = 110400 / fs_in_mb;
265 break;
266 case 51:
267 num_dpb_buffer = 184320 / fs_in_mb;
268 break;
269 default:
270 num_dpb_buffer = 184320 / fs_in_mb;
271 break;
272 }
273 num_dpb_buffer++;
274 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
275 ctx_size = max_references * align(width_in_mb * height_in_mb * 192, 256);
276 } else {
277 // the firmware seems to always assume a minimum of ref frames
278 max_references = MAX2(NUM_H264_REFS, max_references);
279 // macroblock context buffer
280 ctx_size = align(width_in_mb * height_in_mb * max_references * 192, 256);
281 }
282
283 return ctx_size;
284 }
285
286 static unsigned calc_ctx_size_h265_main(struct ruvd_decoder *dec)
287 {
288 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
289 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
290
291 unsigned max_references = dec->base.max_references + 1;
292
293 if (dec->base.width * dec->base.height >= 4096*2000)
294 max_references = MAX2(max_references, 8);
295 else
296 max_references = MAX2(max_references, 17);
297
298 width = align (width, 16);
299 height = align (height, 16);
300 return ((width + 255) / 16) * ((height + 255) / 16) * 16 * max_references + 52 * 1024;
301 }
302
303 static unsigned calc_ctx_size_h265_main10(struct ruvd_decoder *dec, struct pipe_h265_picture_desc *pic)
304 {
305 unsigned block_size, log2_ctb_size, width_in_ctb, height_in_ctb, num_16x16_block_per_ctb;
306 unsigned context_buffer_size_per_ctb_row, cm_buffer_size, max_mb_address, db_left_tile_pxl_size;
307 unsigned db_left_tile_ctx_size = 4096 / 16 * (32 + 16 * 4);
308
309 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
310 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
311 unsigned coeff_10bit = (pic->pps->sps->bit_depth_luma_minus8 || pic->pps->sps->bit_depth_chroma_minus8) ? 2 : 1;
312
313 unsigned max_references = dec->base.max_references + 1;
314
315 if (dec->base.width * dec->base.height >= 4096*2000)
316 max_references = MAX2(max_references, 8);
317 else
318 max_references = MAX2(max_references, 17);
319
320 block_size = (1 << (pic->pps->sps->log2_min_luma_coding_block_size_minus3 + 3));
321 log2_ctb_size = block_size + pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
322
323 width_in_ctb = (width + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
324 height_in_ctb = (height + ((1 << log2_ctb_size) - 1)) >> log2_ctb_size;
325
326 num_16x16_block_per_ctb = ((1 << log2_ctb_size) >> 4) * ((1 << log2_ctb_size) >> 4);
327 context_buffer_size_per_ctb_row = align(width_in_ctb * num_16x16_block_per_ctb * 16, 256);
328 max_mb_address = (unsigned) ceil(height * 8 / 2048.0);
329
330 cm_buffer_size = max_references * context_buffer_size_per_ctb_row * height_in_ctb;
331 db_left_tile_pxl_size = coeff_10bit * (max_mb_address * 2 * 2048 + 1024);
332
333 return cm_buffer_size + db_left_tile_ctx_size + db_left_tile_pxl_size;
334 }
335
336 static unsigned get_db_pitch_alignment(struct ruvd_decoder *dec)
337 {
338 if (((struct r600_common_screen*)dec->screen)->family < CHIP_VEGA10)
339 return 16;
340 else
341 return 32;
342 }
343
344 /* calculate size of reference picture buffer */
345 static unsigned calc_dpb_size(struct ruvd_decoder *dec)
346 {
347 unsigned width_in_mb, height_in_mb, image_size, dpb_size;
348
349 // always align them to MB size for dpb calculation
350 unsigned width = align(dec->base.width, VL_MACROBLOCK_WIDTH);
351 unsigned height = align(dec->base.height, VL_MACROBLOCK_HEIGHT);
352
353 // always one more for currently decoded picture
354 unsigned max_references = dec->base.max_references + 1;
355
356 // aligned size of a single frame
357 image_size = align(width, get_db_pitch_alignment(dec)) * height;
358 image_size += image_size / 2;
359 image_size = align(image_size, 1024);
360
361 // picture width & height in 16 pixel units
362 width_in_mb = width / VL_MACROBLOCK_WIDTH;
363 height_in_mb = align(height / VL_MACROBLOCK_HEIGHT, 2);
364
365 switch (u_reduce_video_profile(dec->base.profile)) {
366 case PIPE_VIDEO_FORMAT_MPEG4_AVC: {
367 if (!dec->use_legacy) {
368 unsigned fs_in_mb = width_in_mb * height_in_mb;
369 unsigned alignment = 64, num_dpb_buffer;
370
371 if (dec->stream_type == RUVD_CODEC_H264_PERF)
372 alignment = 256;
373 switch(dec->base.level) {
374 case 30:
375 num_dpb_buffer = 8100 / fs_in_mb;
376 break;
377 case 31:
378 num_dpb_buffer = 18000 / fs_in_mb;
379 break;
380 case 32:
381 num_dpb_buffer = 20480 / fs_in_mb;
382 break;
383 case 41:
384 num_dpb_buffer = 32768 / fs_in_mb;
385 break;
386 case 42:
387 num_dpb_buffer = 34816 / fs_in_mb;
388 break;
389 case 50:
390 num_dpb_buffer = 110400 / fs_in_mb;
391 break;
392 case 51:
393 num_dpb_buffer = 184320 / fs_in_mb;
394 break;
395 default:
396 num_dpb_buffer = 184320 / fs_in_mb;
397 break;
398 }
399 num_dpb_buffer++;
400 max_references = MAX2(MIN2(NUM_H264_REFS, num_dpb_buffer), max_references);
401 dpb_size = image_size * max_references;
402 if ((dec->stream_type != RUVD_CODEC_H264_PERF) ||
403 (((struct r600_common_screen*)dec->screen)->family < CHIP_POLARIS10)) {
404 dpb_size += max_references * align(width_in_mb * height_in_mb * 192, alignment);
405 dpb_size += align(width_in_mb * height_in_mb * 32, alignment);
406 }
407 } else {
408 // the firmware seems to allways assume a minimum of ref frames
409 max_references = MAX2(NUM_H264_REFS, max_references);
410 // reference picture buffer
411 dpb_size = image_size * max_references;
412 if ((dec->stream_type != RUVD_CODEC_H264_PERF) ||
413 (((struct r600_common_screen*)dec->screen)->family < CHIP_POLARIS10)) {
414 // macroblock context buffer
415 dpb_size += width_in_mb * height_in_mb * max_references * 192;
416 // IT surface buffer
417 dpb_size += width_in_mb * height_in_mb * 32;
418 }
419 }
420 break;
421 }
422
423 case PIPE_VIDEO_FORMAT_HEVC:
424 if (dec->base.width * dec->base.height >= 4096*2000)
425 max_references = MAX2(max_references, 8);
426 else
427 max_references = MAX2(max_references, 17);
428
429 width = align (width, 16);
430 height = align (height, 16);
431 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
432 dpb_size = align((align(width, get_db_pitch_alignment(dec)) * height * 9) / 4, 256) * max_references;
433 else
434 dpb_size = align((align(width, get_db_pitch_alignment(dec)) * height * 3) / 2, 256) * max_references;
435 break;
436
437 case PIPE_VIDEO_FORMAT_VC1:
438 // the firmware seems to allways assume a minimum of ref frames
439 max_references = MAX2(NUM_VC1_REFS, max_references);
440
441 // reference picture buffer
442 dpb_size = image_size * max_references;
443
444 // CONTEXT_BUFFER
445 dpb_size += width_in_mb * height_in_mb * 128;
446
447 // IT surface buffer
448 dpb_size += width_in_mb * 64;
449
450 // DB surface buffer
451 dpb_size += width_in_mb * 128;
452
453 // BP
454 dpb_size += align(MAX2(width_in_mb, height_in_mb) * 7 * 16, 64);
455 break;
456
457 case PIPE_VIDEO_FORMAT_MPEG12:
458 // reference picture buffer, must be big enough for all frames
459 dpb_size = image_size * NUM_MPEG2_REFS;
460 break;
461
462 case PIPE_VIDEO_FORMAT_MPEG4:
463 // reference picture buffer
464 dpb_size = image_size * max_references;
465
466 // CM
467 dpb_size += width_in_mb * height_in_mb * 64;
468
469 // IT surface buffer
470 dpb_size += align(width_in_mb * height_in_mb * 32, 64);
471
472 dpb_size = MAX2(dpb_size, 30 * 1024 * 1024);
473 break;
474
475 case PIPE_VIDEO_FORMAT_JPEG:
476 dpb_size = 0;
477 break;
478
479 default:
480 // something is missing here
481 assert(0);
482
483 // at least use a sane default value
484 dpb_size = 32 * 1024 * 1024;
485 break;
486 }
487 return dpb_size;
488 }
489
490 /* free associated data in the video buffer callback */
491 static void ruvd_destroy_associated_data(void *data)
492 {
493 /* NOOP, since we only use an intptr */
494 }
495
496 /* get h264 specific message bits */
497 static struct ruvd_h264 get_h264_msg(struct ruvd_decoder *dec, struct pipe_h264_picture_desc *pic)
498 {
499 struct ruvd_h264 result;
500
501 memset(&result, 0, sizeof(result));
502 switch (pic->base.profile) {
503 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
504 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
505 result.profile = RUVD_H264_PROFILE_BASELINE;
506 break;
507
508 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
509 result.profile = RUVD_H264_PROFILE_MAIN;
510 break;
511
512 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
513 result.profile = RUVD_H264_PROFILE_HIGH;
514 break;
515
516 default:
517 assert(0);
518 break;
519 }
520
521 result.level = dec->base.level;
522
523 result.sps_info_flags = 0;
524 result.sps_info_flags |= pic->pps->sps->direct_8x8_inference_flag << 0;
525 result.sps_info_flags |= pic->pps->sps->mb_adaptive_frame_field_flag << 1;
526 result.sps_info_flags |= pic->pps->sps->frame_mbs_only_flag << 2;
527 result.sps_info_flags |= pic->pps->sps->delta_pic_order_always_zero_flag << 3;
528
529 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
530 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
531 result.log2_max_frame_num_minus4 = pic->pps->sps->log2_max_frame_num_minus4;
532 result.pic_order_cnt_type = pic->pps->sps->pic_order_cnt_type;
533 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
534
535 switch (dec->base.chroma_format) {
536 case PIPE_VIDEO_CHROMA_FORMAT_NONE:
537 /* TODO: assert? */
538 break;
539 case PIPE_VIDEO_CHROMA_FORMAT_400:
540 result.chroma_format = 0;
541 break;
542 case PIPE_VIDEO_CHROMA_FORMAT_420:
543 result.chroma_format = 1;
544 break;
545 case PIPE_VIDEO_CHROMA_FORMAT_422:
546 result.chroma_format = 2;
547 break;
548 case PIPE_VIDEO_CHROMA_FORMAT_444:
549 result.chroma_format = 3;
550 break;
551 }
552
553 result.pps_info_flags = 0;
554 result.pps_info_flags |= pic->pps->transform_8x8_mode_flag << 0;
555 result.pps_info_flags |= pic->pps->redundant_pic_cnt_present_flag << 1;
556 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 2;
557 result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag << 3;
558 result.pps_info_flags |= pic->pps->weighted_bipred_idc << 4;
559 result.pps_info_flags |= pic->pps->weighted_pred_flag << 6;
560 result.pps_info_flags |= pic->pps->bottom_field_pic_order_in_frame_present_flag << 7;
561 result.pps_info_flags |= pic->pps->entropy_coding_mode_flag << 8;
562
563 result.num_slice_groups_minus1 = pic->pps->num_slice_groups_minus1;
564 result.slice_group_map_type = pic->pps->slice_group_map_type;
565 result.slice_group_change_rate_minus1 = pic->pps->slice_group_change_rate_minus1;
566 result.pic_init_qp_minus26 = pic->pps->pic_init_qp_minus26;
567 result.chroma_qp_index_offset = pic->pps->chroma_qp_index_offset;
568 result.second_chroma_qp_index_offset = pic->pps->second_chroma_qp_index_offset;
569
570 memcpy(result.scaling_list_4x4, pic->pps->ScalingList4x4, 6*16);
571 memcpy(result.scaling_list_8x8, pic->pps->ScalingList8x8, 2*64);
572
573 if (dec->stream_type == RUVD_CODEC_H264_PERF) {
574 memcpy(dec->it, result.scaling_list_4x4, 6*16);
575 memcpy((dec->it + 96), result.scaling_list_8x8, 2*64);
576 }
577
578 result.num_ref_frames = pic->num_ref_frames;
579
580 result.num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
581 result.num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
582
583 result.frame_num = pic->frame_num;
584 memcpy(result.frame_num_list, pic->frame_num_list, 4*16);
585 result.curr_field_order_cnt_list[0] = pic->field_order_cnt[0];
586 result.curr_field_order_cnt_list[1] = pic->field_order_cnt[1];
587 memcpy(result.field_order_cnt_list, pic->field_order_cnt_list, 4*16*2);
588
589 result.decoded_pic_idx = pic->frame_num;
590
591 return result;
592 }
593
594 /* get h265 specific message bits */
595 static struct ruvd_h265 get_h265_msg(struct ruvd_decoder *dec, struct pipe_video_buffer *target,
596 struct pipe_h265_picture_desc *pic)
597 {
598 struct ruvd_h265 result;
599 unsigned i;
600
601 memset(&result, 0, sizeof(result));
602
603 result.sps_info_flags = 0;
604 result.sps_info_flags |= pic->pps->sps->scaling_list_enabled_flag << 0;
605 result.sps_info_flags |= pic->pps->sps->amp_enabled_flag << 1;
606 result.sps_info_flags |= pic->pps->sps->sample_adaptive_offset_enabled_flag << 2;
607 result.sps_info_flags |= pic->pps->sps->pcm_enabled_flag << 3;
608 result.sps_info_flags |= pic->pps->sps->pcm_loop_filter_disabled_flag << 4;
609 result.sps_info_flags |= pic->pps->sps->long_term_ref_pics_present_flag << 5;
610 result.sps_info_flags |= pic->pps->sps->sps_temporal_mvp_enabled_flag << 6;
611 result.sps_info_flags |= pic->pps->sps->strong_intra_smoothing_enabled_flag << 7;
612 result.sps_info_flags |= pic->pps->sps->separate_colour_plane_flag << 8;
613 if (((struct r600_common_screen*)dec->screen)->family == CHIP_CARRIZO)
614 result.sps_info_flags |= 1 << 9;
615 if (pic->UseRefPicList == true)
616 result.sps_info_flags |= 1 << 10;
617
618 result.chroma_format = pic->pps->sps->chroma_format_idc;
619 result.bit_depth_luma_minus8 = pic->pps->sps->bit_depth_luma_minus8;
620 result.bit_depth_chroma_minus8 = pic->pps->sps->bit_depth_chroma_minus8;
621 result.log2_max_pic_order_cnt_lsb_minus4 = pic->pps->sps->log2_max_pic_order_cnt_lsb_minus4;
622 result.sps_max_dec_pic_buffering_minus1 = pic->pps->sps->sps_max_dec_pic_buffering_minus1;
623 result.log2_min_luma_coding_block_size_minus3 = pic->pps->sps->log2_min_luma_coding_block_size_minus3;
624 result.log2_diff_max_min_luma_coding_block_size = pic->pps->sps->log2_diff_max_min_luma_coding_block_size;
625 result.log2_min_transform_block_size_minus2 = pic->pps->sps->log2_min_transform_block_size_minus2;
626 result.log2_diff_max_min_transform_block_size = pic->pps->sps->log2_diff_max_min_transform_block_size;
627 result.max_transform_hierarchy_depth_inter = pic->pps->sps->max_transform_hierarchy_depth_inter;
628 result.max_transform_hierarchy_depth_intra = pic->pps->sps->max_transform_hierarchy_depth_intra;
629 result.pcm_sample_bit_depth_luma_minus1 = pic->pps->sps->pcm_sample_bit_depth_luma_minus1;
630 result.pcm_sample_bit_depth_chroma_minus1 = pic->pps->sps->pcm_sample_bit_depth_chroma_minus1;
631 result.log2_min_pcm_luma_coding_block_size_minus3 = pic->pps->sps->log2_min_pcm_luma_coding_block_size_minus3;
632 result.log2_diff_max_min_pcm_luma_coding_block_size = pic->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size;
633 result.num_short_term_ref_pic_sets = pic->pps->sps->num_short_term_ref_pic_sets;
634
635 result.pps_info_flags = 0;
636 result.pps_info_flags |= pic->pps->dependent_slice_segments_enabled_flag << 0;
637 result.pps_info_flags |= pic->pps->output_flag_present_flag << 1;
638 result.pps_info_flags |= pic->pps->sign_data_hiding_enabled_flag << 2;
639 result.pps_info_flags |= pic->pps->cabac_init_present_flag << 3;
640 result.pps_info_flags |= pic->pps->constrained_intra_pred_flag << 4;
641 result.pps_info_flags |= pic->pps->transform_skip_enabled_flag << 5;
642 result.pps_info_flags |= pic->pps->cu_qp_delta_enabled_flag << 6;
643 result.pps_info_flags |= pic->pps->pps_slice_chroma_qp_offsets_present_flag << 7;
644 result.pps_info_flags |= pic->pps->weighted_pred_flag << 8;
645 result.pps_info_flags |= pic->pps->weighted_bipred_flag << 9;
646 result.pps_info_flags |= pic->pps->transquant_bypass_enabled_flag << 10;
647 result.pps_info_flags |= pic->pps->tiles_enabled_flag << 11;
648 result.pps_info_flags |= pic->pps->entropy_coding_sync_enabled_flag << 12;
649 result.pps_info_flags |= pic->pps->uniform_spacing_flag << 13;
650 result.pps_info_flags |= pic->pps->loop_filter_across_tiles_enabled_flag << 14;
651 result.pps_info_flags |= pic->pps->pps_loop_filter_across_slices_enabled_flag << 15;
652 result.pps_info_flags |= pic->pps->deblocking_filter_override_enabled_flag << 16;
653 result.pps_info_flags |= pic->pps->pps_deblocking_filter_disabled_flag << 17;
654 result.pps_info_flags |= pic->pps->lists_modification_present_flag << 18;
655 result.pps_info_flags |= pic->pps->slice_segment_header_extension_present_flag << 19;
656 //result.pps_info_flags |= pic->pps->deblocking_filter_control_present_flag; ???
657
658 result.num_extra_slice_header_bits = pic->pps->num_extra_slice_header_bits;
659 result.num_long_term_ref_pic_sps = pic->pps->sps->num_long_term_ref_pics_sps;
660 result.num_ref_idx_l0_default_active_minus1 = pic->pps->num_ref_idx_l0_default_active_minus1;
661 result.num_ref_idx_l1_default_active_minus1 = pic->pps->num_ref_idx_l1_default_active_minus1;
662 result.pps_cb_qp_offset = pic->pps->pps_cb_qp_offset;
663 result.pps_cr_qp_offset = pic->pps->pps_cr_qp_offset;
664 result.pps_beta_offset_div2 = pic->pps->pps_beta_offset_div2;
665 result.pps_tc_offset_div2 = pic->pps->pps_tc_offset_div2;
666 result.diff_cu_qp_delta_depth = pic->pps->diff_cu_qp_delta_depth;
667 result.num_tile_columns_minus1 = pic->pps->num_tile_columns_minus1;
668 result.num_tile_rows_minus1 = pic->pps->num_tile_rows_minus1;
669 result.log2_parallel_merge_level_minus2 = pic->pps->log2_parallel_merge_level_minus2;
670 result.init_qp_minus26 = pic->pps->init_qp_minus26;
671
672 for (i = 0; i < 19; ++i)
673 result.column_width_minus1[i] = pic->pps->column_width_minus1[i];
674
675 for (i = 0; i < 21; ++i)
676 result.row_height_minus1[i] = pic->pps->row_height_minus1[i];
677
678 result.num_delta_pocs_ref_rps_idx = pic->NumDeltaPocsOfRefRpsIdx;
679 result.curr_idx = pic->CurrPicOrderCntVal;
680 result.curr_poc = pic->CurrPicOrderCntVal;
681
682 vl_video_buffer_set_associated_data(target, &dec->base,
683 (void *)(uintptr_t)pic->CurrPicOrderCntVal,
684 &ruvd_destroy_associated_data);
685
686 for (i = 0; i < 16; ++i) {
687 struct pipe_video_buffer *ref = pic->ref[i];
688 uintptr_t ref_pic = 0;
689
690 result.poc_list[i] = pic->PicOrderCntVal[i];
691
692 if (ref)
693 ref_pic = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
694 else
695 ref_pic = 0x7F;
696 result.ref_pic_list[i] = ref_pic;
697 }
698
699 for (i = 0; i < 8; ++i) {
700 result.ref_pic_set_st_curr_before[i] = 0xFF;
701 result.ref_pic_set_st_curr_after[i] = 0xFF;
702 result.ref_pic_set_lt_curr[i] = 0xFF;
703 }
704
705 for (i = 0; i < pic->NumPocStCurrBefore; ++i)
706 result.ref_pic_set_st_curr_before[i] = pic->RefPicSetStCurrBefore[i];
707
708 for (i = 0; i < pic->NumPocStCurrAfter; ++i)
709 result.ref_pic_set_st_curr_after[i] = pic->RefPicSetStCurrAfter[i];
710
711 for (i = 0; i < pic->NumPocLtCurr; ++i)
712 result.ref_pic_set_lt_curr[i] = pic->RefPicSetLtCurr[i];
713
714 for (i = 0; i < 6; ++i)
715 result.ucScalingListDCCoefSizeID2[i] = pic->pps->sps->ScalingListDCCoeff16x16[i];
716
717 for (i = 0; i < 2; ++i)
718 result.ucScalingListDCCoefSizeID3[i] = pic->pps->sps->ScalingListDCCoeff32x32[i];
719
720 memcpy(dec->it, pic->pps->sps->ScalingList4x4, 6 * 16);
721 memcpy(dec->it + 96, pic->pps->sps->ScalingList8x8, 6 * 64);
722 memcpy(dec->it + 480, pic->pps->sps->ScalingList16x16, 6 * 64);
723 memcpy(dec->it + 864, pic->pps->sps->ScalingList32x32, 2 * 64);
724
725 for (i = 0 ; i < 2 ; i++) {
726 for (int j = 0 ; j < 15 ; j++)
727 result.direct_reflist[i][j] = pic->RefPicList[i][j];
728 }
729
730 if (pic->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10) {
731 if (target->buffer_format == PIPE_FORMAT_P016) {
732 result.p010_mode = 1;
733 result.msb_mode = 1;
734 } else {
735 result.luma_10to8 = 5;
736 result.chroma_10to8 = 5;
737 result.sclr_luma10to8 = 4;
738 result.sclr_chroma10to8 = 4;
739 }
740 }
741
742 /* TODO
743 result.highestTid;
744 result.isNonRef;
745
746 IDRPicFlag;
747 RAPPicFlag;
748 NumPocTotalCurr;
749 NumShortTermPictureSliceHeaderBits;
750 NumLongTermPictureSliceHeaderBits;
751
752 IsLongTerm[16];
753 */
754
755 return result;
756 }
757
758 /* get vc1 specific message bits */
759 static struct ruvd_vc1 get_vc1_msg(struct pipe_vc1_picture_desc *pic)
760 {
761 struct ruvd_vc1 result;
762
763 memset(&result, 0, sizeof(result));
764
765 switch(pic->base.profile) {
766 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
767 result.profile = RUVD_VC1_PROFILE_SIMPLE;
768 result.level = 1;
769 break;
770
771 case PIPE_VIDEO_PROFILE_VC1_MAIN:
772 result.profile = RUVD_VC1_PROFILE_MAIN;
773 result.level = 2;
774 break;
775
776 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
777 result.profile = RUVD_VC1_PROFILE_ADVANCED;
778 result.level = 4;
779 break;
780
781 default:
782 assert(0);
783 }
784
785 /* fields common for all profiles */
786 result.sps_info_flags |= pic->postprocflag << 7;
787 result.sps_info_flags |= pic->pulldown << 6;
788 result.sps_info_flags |= pic->interlace << 5;
789 result.sps_info_flags |= pic->tfcntrflag << 4;
790 result.sps_info_flags |= pic->finterpflag << 3;
791 result.sps_info_flags |= pic->psf << 1;
792
793 result.pps_info_flags |= pic->range_mapy_flag << 31;
794 result.pps_info_flags |= pic->range_mapy << 28;
795 result.pps_info_flags |= pic->range_mapuv_flag << 27;
796 result.pps_info_flags |= pic->range_mapuv << 24;
797 result.pps_info_flags |= pic->multires << 21;
798 result.pps_info_flags |= pic->maxbframes << 16;
799 result.pps_info_flags |= pic->overlap << 11;
800 result.pps_info_flags |= pic->quantizer << 9;
801 result.pps_info_flags |= pic->panscan_flag << 7;
802 result.pps_info_flags |= pic->refdist_flag << 6;
803 result.pps_info_flags |= pic->vstransform << 0;
804
805 /* some fields only apply to main/advanced profile */
806 if (pic->base.profile != PIPE_VIDEO_PROFILE_VC1_SIMPLE) {
807 result.pps_info_flags |= pic->syncmarker << 20;
808 result.pps_info_flags |= pic->rangered << 19;
809 result.pps_info_flags |= pic->loopfilter << 5;
810 result.pps_info_flags |= pic->fastuvmc << 4;
811 result.pps_info_flags |= pic->extended_mv << 3;
812 result.pps_info_flags |= pic->extended_dmv << 8;
813 result.pps_info_flags |= pic->dquant << 1;
814 }
815
816 result.chroma_format = 1;
817
818 #if 0
819 //(((unsigned int)(pPicParams->advance.reserved1)) << SPS_INFO_VC1_RESERVED_SHIFT)
820 uint32_t slice_count
821 uint8_t picture_type
822 uint8_t frame_coding_mode
823 uint8_t deblockEnable
824 uint8_t pquant
825 #endif
826
827 return result;
828 }
829
830 /* extract the frame number from a referenced video buffer */
831 static uint32_t get_ref_pic_idx(struct ruvd_decoder *dec, struct pipe_video_buffer *ref)
832 {
833 uint32_t min = MAX2(dec->frame_number, NUM_MPEG2_REFS) - NUM_MPEG2_REFS;
834 uint32_t max = MAX2(dec->frame_number, 1) - 1;
835 uintptr_t frame;
836
837 /* seems to be the most sane fallback */
838 if (!ref)
839 return max;
840
841 /* get the frame number from the associated data */
842 frame = (uintptr_t)vl_video_buffer_get_associated_data(ref, &dec->base);
843
844 /* limit the frame number to a valid range */
845 return MAX2(MIN2(frame, max), min);
846 }
847
848 /* get mpeg2 specific msg bits */
849 static struct ruvd_mpeg2 get_mpeg2_msg(struct ruvd_decoder *dec,
850 struct pipe_mpeg12_picture_desc *pic)
851 {
852 const int *zscan = pic->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
853 struct ruvd_mpeg2 result;
854 unsigned i;
855
856 memset(&result, 0, sizeof(result));
857 result.decoded_pic_idx = dec->frame_number;
858 for (i = 0; i < 2; ++i)
859 result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
860
861 result.load_intra_quantiser_matrix = 1;
862 result.load_nonintra_quantiser_matrix = 1;
863
864 for (i = 0; i < 64; ++i) {
865 result.intra_quantiser_matrix[i] = pic->intra_matrix[zscan[i]];
866 result.nonintra_quantiser_matrix[i] = pic->non_intra_matrix[zscan[i]];
867 }
868
869 result.profile_and_level_indication = 0;
870 result.chroma_format = 0x1;
871
872 result.picture_coding_type = pic->picture_coding_type;
873 result.f_code[0][0] = pic->f_code[0][0] + 1;
874 result.f_code[0][1] = pic->f_code[0][1] + 1;
875 result.f_code[1][0] = pic->f_code[1][0] + 1;
876 result.f_code[1][1] = pic->f_code[1][1] + 1;
877 result.intra_dc_precision = pic->intra_dc_precision;
878 result.pic_structure = pic->picture_structure;
879 result.top_field_first = pic->top_field_first;
880 result.frame_pred_frame_dct = pic->frame_pred_frame_dct;
881 result.concealment_motion_vectors = pic->concealment_motion_vectors;
882 result.q_scale_type = pic->q_scale_type;
883 result.intra_vlc_format = pic->intra_vlc_format;
884 result.alternate_scan = pic->alternate_scan;
885
886 return result;
887 }
888
889 /* get mpeg4 specific msg bits */
890 static struct ruvd_mpeg4 get_mpeg4_msg(struct ruvd_decoder *dec,
891 struct pipe_mpeg4_picture_desc *pic)
892 {
893 struct ruvd_mpeg4 result;
894 unsigned i;
895
896 memset(&result, 0, sizeof(result));
897 result.decoded_pic_idx = dec->frame_number;
898 for (i = 0; i < 2; ++i)
899 result.ref_pic_idx[i] = get_ref_pic_idx(dec, pic->ref[i]);
900
901 result.variant_type = 0;
902 result.profile_and_level_indication = 0xF0; // ASP Level0
903
904 result.video_object_layer_verid = 0x5; // advanced simple
905 result.video_object_layer_shape = 0x0; // rectangular
906
907 result.video_object_layer_width = dec->base.width;
908 result.video_object_layer_height = dec->base.height;
909
910 result.vop_time_increment_resolution = pic->vop_time_increment_resolution;
911
912 result.flags |= pic->short_video_header << 0;
913 //result.flags |= obmc_disable << 1;
914 result.flags |= pic->interlaced << 2;
915 result.flags |= 1 << 3; // load_intra_quant_mat
916 result.flags |= 1 << 4; // load_nonintra_quant_mat
917 result.flags |= pic->quarter_sample << 5;
918 result.flags |= 1 << 6; // complexity_estimation_disable
919 result.flags |= pic->resync_marker_disable << 7;
920 //result.flags |= data_partitioned << 8;
921 //result.flags |= reversible_vlc << 9;
922 result.flags |= 0 << 10; // newpred_enable
923 result.flags |= 0 << 11; // reduced_resolution_vop_enable
924 //result.flags |= scalability << 12;
925 //result.flags |= is_object_layer_identifier << 13;
926 //result.flags |= fixed_vop_rate << 14;
927 //result.flags |= newpred_segment_type << 15;
928
929 result.quant_type = pic->quant_type;
930
931 for (i = 0; i < 64; ++i) {
932 result.intra_quant_mat[i] = pic->intra_matrix[vl_zscan_normal[i]];
933 result.nonintra_quant_mat[i] = pic->non_intra_matrix[vl_zscan_normal[i]];
934 }
935
936 /*
937 int32_t trd [2]
938 int32_t trb [2]
939 uint8_t vop_coding_type
940 uint8_t vop_fcode_forward
941 uint8_t vop_fcode_backward
942 uint8_t rounding_control
943 uint8_t alternate_vertical_scan_flag
944 uint8_t top_field_first
945 */
946
947 return result;
948 }
949
950 static void get_mjpeg_slice_header(struct ruvd_decoder *dec, struct pipe_mjpeg_picture_desc *pic)
951 {
952 int size = 0, saved_size, len_pos, i;
953 uint16_t *bs;
954 uint8_t *buf = dec->bs_ptr;
955
956 /* SOI */
957 buf[size++] = 0xff;
958 buf[size++] = 0xd8;
959
960 /* DQT */
961 buf[size++] = 0xff;
962 buf[size++] = 0xdb;
963
964 len_pos = size++;
965 size++;
966
967 for (i = 0; i < 4; ++i) {
968 if (pic->quantization_table.load_quantiser_table[i] == 0)
969 continue;
970
971 buf[size++] = i;
972 memcpy((buf + size), &pic->quantization_table.quantiser_table[i], 64);
973 size += 64;
974 }
975
976 bs = (uint16_t*)&buf[len_pos];
977 *bs = util_bswap16(size - 4);
978
979 saved_size = size;
980
981 /* DHT */
982 buf[size++] = 0xff;
983 buf[size++] = 0xc4;
984
985 len_pos = size++;
986 size++;
987
988 for (i = 0; i < 2; ++i) {
989 if (pic->huffman_table.load_huffman_table[i] == 0)
990 continue;
991
992 buf[size++] = 0x00 | i;
993 memcpy((buf + size), &pic->huffman_table.table[i].num_dc_codes, 16);
994 size += 16;
995 memcpy((buf + size), &pic->huffman_table.table[i].dc_values, 12);
996 size += 12;
997 }
998
999 for (i = 0; i < 2; ++i) {
1000 if (pic->huffman_table.load_huffman_table[i] == 0)
1001 continue;
1002
1003 buf[size++] = 0x10 | i;
1004 memcpy((buf + size), &pic->huffman_table.table[i].num_ac_codes, 16);
1005 size += 16;
1006 memcpy((buf + size), &pic->huffman_table.table[i].ac_values, 162);
1007 size += 162;
1008 }
1009
1010 bs = (uint16_t*)&buf[len_pos];
1011 *bs = util_bswap16(size - saved_size - 2);
1012
1013 saved_size = size;
1014
1015 /* SOF */
1016 buf[size++] = 0xff;
1017 buf[size++] = 0xc0;
1018
1019 len_pos = size++;
1020 size++;
1021
1022 buf[size++] = 0x08;
1023
1024 bs = (uint16_t*)&buf[size++];
1025 *bs = util_bswap16(pic->picture_parameter.picture_height);
1026 size++;
1027
1028 bs = (uint16_t*)&buf[size++];
1029 *bs = util_bswap16(pic->picture_parameter.picture_width);
1030 size++;
1031
1032 buf[size++] = pic->picture_parameter.num_components;
1033
1034 for (i = 0; i < pic->picture_parameter.num_components; ++i) {
1035 buf[size++] = pic->picture_parameter.components[i].component_id;
1036 buf[size++] = pic->picture_parameter.components[i].h_sampling_factor << 4 |
1037 pic->picture_parameter.components[i].v_sampling_factor;
1038 buf[size++] = pic->picture_parameter.components[i].quantiser_table_selector;
1039 }
1040
1041 bs = (uint16_t*)&buf[len_pos];
1042 *bs = util_bswap16(size - saved_size - 2);
1043
1044 saved_size = size;
1045
1046 /* SOS */
1047 buf[size++] = 0xff;
1048 buf[size++] = 0xda;
1049
1050 len_pos = size++;
1051 size++;
1052
1053 buf[size++] = pic->slice_parameter.num_components;
1054
1055 for (i = 0; i < pic->slice_parameter.num_components; ++i) {
1056 buf[size++] = pic->slice_parameter.components[i].component_selector;
1057 buf[size++] = pic->slice_parameter.components[i].dc_table_selector << 4 |
1058 pic->slice_parameter.components[i].ac_table_selector;
1059 }
1060
1061 buf[size++] = 0x00;
1062 buf[size++] = 0x3f;
1063 buf[size++] = 0x00;
1064
1065 bs = (uint16_t*)&buf[len_pos];
1066 *bs = util_bswap16(size - saved_size - 2);
1067
1068 dec->bs_ptr += size;
1069 dec->bs_size += size;
1070 }
1071
1072 /**
1073 * destroy this video decoder
1074 */
1075 static void ruvd_destroy(struct pipe_video_codec *decoder)
1076 {
1077 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1078 unsigned i;
1079
1080 assert(decoder);
1081
1082 map_msg_fb_it_buf(dec);
1083 dec->msg->size = sizeof(*dec->msg);
1084 dec->msg->msg_type = RUVD_MSG_DESTROY;
1085 dec->msg->stream_handle = dec->stream_handle;
1086 send_msg_buf(dec);
1087
1088 flush(dec, 0);
1089
1090 dec->ws->cs_destroy(dec->cs);
1091
1092 for (i = 0; i < NUM_BUFFERS; ++i) {
1093 rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
1094 rvid_destroy_buffer(&dec->bs_buffers[i]);
1095 }
1096
1097 rvid_destroy_buffer(&dec->dpb);
1098 rvid_destroy_buffer(&dec->ctx);
1099 rvid_destroy_buffer(&dec->sessionctx);
1100
1101 FREE(dec);
1102 }
1103
1104 /**
1105 * start decoding of a new frame
1106 */
1107 static void ruvd_begin_frame(struct pipe_video_codec *decoder,
1108 struct pipe_video_buffer *target,
1109 struct pipe_picture_desc *picture)
1110 {
1111 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1112 uintptr_t frame;
1113
1114 assert(decoder);
1115
1116 frame = ++dec->frame_number;
1117 vl_video_buffer_set_associated_data(target, decoder, (void *)frame,
1118 &ruvd_destroy_associated_data);
1119
1120 dec->bs_size = 0;
1121 dec->bs_ptr = dec->ws->buffer_map(
1122 dec->bs_buffers[dec->cur_buffer].res->buf,
1123 dec->cs, PIPE_TRANSFER_WRITE);
1124 }
1125
1126 /**
1127 * decode a macroblock
1128 */
1129 static void ruvd_decode_macroblock(struct pipe_video_codec *decoder,
1130 struct pipe_video_buffer *target,
1131 struct pipe_picture_desc *picture,
1132 const struct pipe_macroblock *macroblocks,
1133 unsigned num_macroblocks)
1134 {
1135 /* not supported (yet) */
1136 assert(0);
1137 }
1138
1139 /**
1140 * decode a bitstream
1141 */
1142 static void ruvd_decode_bitstream(struct pipe_video_codec *decoder,
1143 struct pipe_video_buffer *target,
1144 struct pipe_picture_desc *picture,
1145 unsigned num_buffers,
1146 const void * const *buffers,
1147 const unsigned *sizes)
1148 {
1149 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1150 enum pipe_video_format format = u_reduce_video_profile(picture->profile);
1151 unsigned i;
1152
1153 assert(decoder);
1154
1155 if (!dec->bs_ptr)
1156 return;
1157
1158 if (format == PIPE_VIDEO_FORMAT_JPEG)
1159 get_mjpeg_slice_header(dec, (struct pipe_mjpeg_picture_desc*)picture);
1160
1161 for (i = 0; i < num_buffers; ++i) {
1162 struct rvid_buffer *buf = &dec->bs_buffers[dec->cur_buffer];
1163 unsigned new_size = dec->bs_size + sizes[i];
1164
1165 if (format == PIPE_VIDEO_FORMAT_JPEG)
1166 new_size += 2; /* save for EOI */
1167
1168 if (new_size > buf->res->buf->size) {
1169 dec->ws->buffer_unmap(buf->res->buf);
1170 if (!rvid_resize_buffer(dec->screen, dec->cs, buf, new_size)) {
1171 RVID_ERR("Can't resize bitstream buffer!");
1172 return;
1173 }
1174
1175 dec->bs_ptr = dec->ws->buffer_map(buf->res->buf, dec->cs,
1176 PIPE_TRANSFER_WRITE);
1177 if (!dec->bs_ptr)
1178 return;
1179
1180 dec->bs_ptr += dec->bs_size;
1181 }
1182
1183 memcpy(dec->bs_ptr, buffers[i], sizes[i]);
1184 dec->bs_size += sizes[i];
1185 dec->bs_ptr += sizes[i];
1186 }
1187
1188 if (format == PIPE_VIDEO_FORMAT_JPEG) {
1189 ((uint8_t *)dec->bs_ptr)[0] = 0xff; /* EOI */
1190 ((uint8_t *)dec->bs_ptr)[1] = 0xd9;
1191 dec->bs_size += 2;
1192 dec->bs_ptr += 2;
1193 }
1194 }
1195
1196 /**
1197 * end decoding of the current frame
1198 */
1199 static void ruvd_end_frame(struct pipe_video_codec *decoder,
1200 struct pipe_video_buffer *target,
1201 struct pipe_picture_desc *picture)
1202 {
1203 struct ruvd_decoder *dec = (struct ruvd_decoder*)decoder;
1204 struct pb_buffer *dt;
1205 struct rvid_buffer *msg_fb_it_buf, *bs_buf;
1206 unsigned bs_size;
1207
1208 assert(decoder);
1209
1210 if (!dec->bs_ptr)
1211 return;
1212
1213 msg_fb_it_buf = &dec->msg_fb_it_buffers[dec->cur_buffer];
1214 bs_buf = &dec->bs_buffers[dec->cur_buffer];
1215
1216 bs_size = align(dec->bs_size, 128);
1217 memset(dec->bs_ptr, 0, bs_size - dec->bs_size);
1218 dec->ws->buffer_unmap(bs_buf->res->buf);
1219
1220 map_msg_fb_it_buf(dec);
1221 dec->msg->size = sizeof(*dec->msg);
1222 dec->msg->msg_type = RUVD_MSG_DECODE;
1223 dec->msg->stream_handle = dec->stream_handle;
1224 dec->msg->status_report_feedback_number = dec->frame_number;
1225
1226 dec->msg->body.decode.stream_type = dec->stream_type;
1227 dec->msg->body.decode.decode_flags = 0x1;
1228 dec->msg->body.decode.width_in_samples = dec->base.width;
1229 dec->msg->body.decode.height_in_samples = dec->base.height;
1230
1231 if ((picture->profile == PIPE_VIDEO_PROFILE_VC1_SIMPLE) ||
1232 (picture->profile == PIPE_VIDEO_PROFILE_VC1_MAIN)) {
1233 dec->msg->body.decode.width_in_samples = align(dec->msg->body.decode.width_in_samples, 16) / 16;
1234 dec->msg->body.decode.height_in_samples = align(dec->msg->body.decode.height_in_samples, 16) / 16;
1235 }
1236
1237 if (dec->dpb.res)
1238 dec->msg->body.decode.dpb_size = dec->dpb.res->buf->size;
1239 dec->msg->body.decode.bsd_size = bs_size;
1240 dec->msg->body.decode.db_pitch = align(dec->base.width, get_db_pitch_alignment(dec));
1241
1242 if (dec->stream_type == RUVD_CODEC_H264_PERF &&
1243 ((struct r600_common_screen*)dec->screen)->family >= CHIP_POLARIS10)
1244 dec->msg->body.decode.dpb_reserved = dec->ctx.res->buf->size;
1245
1246 dt = dec->set_dtb(dec->msg, (struct vl_video_buffer *)target);
1247 if (((struct r600_common_screen*)dec->screen)->family >= CHIP_STONEY)
1248 dec->msg->body.decode.dt_wa_chroma_top_offset = dec->msg->body.decode.dt_pitch / 2;
1249
1250 switch (u_reduce_video_profile(picture->profile)) {
1251 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1252 dec->msg->body.decode.codec.h264 = get_h264_msg(dec, (struct pipe_h264_picture_desc*)picture);
1253 break;
1254
1255 case PIPE_VIDEO_FORMAT_HEVC:
1256 dec->msg->body.decode.codec.h265 = get_h265_msg(dec, target, (struct pipe_h265_picture_desc*)picture);
1257 if (dec->ctx.res == NULL) {
1258 unsigned ctx_size;
1259 if (dec->base.profile == PIPE_VIDEO_PROFILE_HEVC_MAIN_10)
1260 ctx_size = calc_ctx_size_h265_main10(dec, (struct pipe_h265_picture_desc*)picture);
1261 else
1262 ctx_size = calc_ctx_size_h265_main(dec);
1263 if (!rvid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1264 RVID_ERR("Can't allocated context buffer.\n");
1265 }
1266 rvid_clear_buffer(decoder->context, &dec->ctx);
1267 }
1268
1269 if (dec->ctx.res)
1270 dec->msg->body.decode.dpb_reserved = dec->ctx.res->buf->size;
1271 break;
1272
1273 case PIPE_VIDEO_FORMAT_VC1:
1274 dec->msg->body.decode.codec.vc1 = get_vc1_msg((struct pipe_vc1_picture_desc*)picture);
1275 break;
1276
1277 case PIPE_VIDEO_FORMAT_MPEG12:
1278 dec->msg->body.decode.codec.mpeg2 = get_mpeg2_msg(dec, (struct pipe_mpeg12_picture_desc*)picture);
1279 break;
1280
1281 case PIPE_VIDEO_FORMAT_MPEG4:
1282 dec->msg->body.decode.codec.mpeg4 = get_mpeg4_msg(dec, (struct pipe_mpeg4_picture_desc*)picture);
1283 break;
1284
1285 case PIPE_VIDEO_FORMAT_JPEG:
1286 break;
1287
1288 default:
1289 assert(0);
1290 return;
1291 }
1292
1293 dec->msg->body.decode.db_surf_tile_config = dec->msg->body.decode.dt_surf_tile_config;
1294 dec->msg->body.decode.extension_support = 0x1;
1295
1296 /* set at least the feedback buffer size */
1297 dec->fb[0] = dec->fb_size;
1298
1299 send_msg_buf(dec);
1300
1301 if (dec->dpb.res)
1302 send_cmd(dec, RUVD_CMD_DPB_BUFFER, dec->dpb.res->buf, 0,
1303 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1304
1305 if (dec->ctx.res)
1306 send_cmd(dec, RUVD_CMD_CONTEXT_BUFFER, dec->ctx.res->buf, 0,
1307 RADEON_USAGE_READWRITE, RADEON_DOMAIN_VRAM);
1308 send_cmd(dec, RUVD_CMD_BITSTREAM_BUFFER, bs_buf->res->buf,
1309 0, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1310 send_cmd(dec, RUVD_CMD_DECODING_TARGET_BUFFER, dt, 0,
1311 RADEON_USAGE_WRITE, RADEON_DOMAIN_VRAM);
1312 send_cmd(dec, RUVD_CMD_FEEDBACK_BUFFER, msg_fb_it_buf->res->buf,
1313 FB_BUFFER_OFFSET, RADEON_USAGE_WRITE, RADEON_DOMAIN_GTT);
1314 if (have_it(dec))
1315 send_cmd(dec, RUVD_CMD_ITSCALING_TABLE_BUFFER, msg_fb_it_buf->res->buf,
1316 FB_BUFFER_OFFSET + dec->fb_size, RADEON_USAGE_READ, RADEON_DOMAIN_GTT);
1317 set_reg(dec, dec->reg.cntl, 1);
1318
1319 flush(dec, RADEON_FLUSH_ASYNC);
1320 next_buffer(dec);
1321 }
1322
1323 /**
1324 * flush any outstanding command buffers to the hardware
1325 */
1326 static void ruvd_flush(struct pipe_video_codec *decoder)
1327 {
1328 }
1329
1330 /**
1331 * create and UVD decoder
1332 */
1333 struct pipe_video_codec *ruvd_create_decoder(struct pipe_context *context,
1334 const struct pipe_video_codec *templ,
1335 ruvd_set_dtb set_dtb)
1336 {
1337 struct radeon_winsys* ws = ((struct r600_common_context *)context)->ws;
1338 struct r600_common_context *rctx = (struct r600_common_context*)context;
1339 unsigned dpb_size;
1340 unsigned width = templ->width, height = templ->height;
1341 unsigned bs_buf_size;
1342 struct radeon_info info;
1343 struct ruvd_decoder *dec;
1344 int r, i;
1345
1346 ws->query_info(ws, &info);
1347
1348 switch(u_reduce_video_profile(templ->profile)) {
1349 case PIPE_VIDEO_FORMAT_MPEG12:
1350 if (templ->entrypoint > PIPE_VIDEO_ENTRYPOINT_BITSTREAM || info.family < CHIP_PALM)
1351 return vl_create_mpeg12_decoder(context, templ);
1352
1353 /* fall through */
1354 case PIPE_VIDEO_FORMAT_MPEG4:
1355 width = align(width, VL_MACROBLOCK_WIDTH);
1356 height = align(height, VL_MACROBLOCK_HEIGHT);
1357 break;
1358 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
1359 width = align(width, VL_MACROBLOCK_WIDTH);
1360 height = align(height, VL_MACROBLOCK_HEIGHT);
1361 break;
1362
1363 default:
1364 break;
1365 }
1366
1367
1368 dec = CALLOC_STRUCT(ruvd_decoder);
1369
1370 if (!dec)
1371 return NULL;
1372
1373 if (info.drm_major < 3)
1374 dec->use_legacy = true;
1375
1376 dec->base = *templ;
1377 dec->base.context = context;
1378 dec->base.width = width;
1379 dec->base.height = height;
1380
1381 dec->base.destroy = ruvd_destroy;
1382 dec->base.begin_frame = ruvd_begin_frame;
1383 dec->base.decode_macroblock = ruvd_decode_macroblock;
1384 dec->base.decode_bitstream = ruvd_decode_bitstream;
1385 dec->base.end_frame = ruvd_end_frame;
1386 dec->base.flush = ruvd_flush;
1387
1388 dec->stream_type = profile2stream_type(dec, info.family);
1389 dec->set_dtb = set_dtb;
1390 dec->stream_handle = rvid_alloc_stream_handle();
1391 dec->screen = context->screen;
1392 dec->ws = ws;
1393 dec->cs = ws->cs_create(rctx->ctx, RING_UVD, NULL, NULL);
1394 if (!dec->cs) {
1395 RVID_ERR("Can't get command submission context.\n");
1396 goto error;
1397 }
1398
1399 dec->fb_size = (info.family == CHIP_TONGA) ? FB_BUFFER_SIZE_TONGA :
1400 FB_BUFFER_SIZE;
1401 bs_buf_size = width * height * (512 / (16 * 16));
1402 for (i = 0; i < NUM_BUFFERS; ++i) {
1403 unsigned msg_fb_it_size = FB_BUFFER_OFFSET + dec->fb_size;
1404 STATIC_ASSERT(sizeof(struct ruvd_msg) <= FB_BUFFER_OFFSET);
1405 if (have_it(dec))
1406 msg_fb_it_size += IT_SCALING_TABLE_SIZE;
1407 if (!rvid_create_buffer(dec->screen, &dec->msg_fb_it_buffers[i],
1408 msg_fb_it_size, PIPE_USAGE_STAGING)) {
1409 RVID_ERR("Can't allocated message buffers.\n");
1410 goto error;
1411 }
1412
1413 if (!rvid_create_buffer(dec->screen, &dec->bs_buffers[i],
1414 bs_buf_size, PIPE_USAGE_STAGING)) {
1415 RVID_ERR("Can't allocated bitstream buffers.\n");
1416 goto error;
1417 }
1418
1419 rvid_clear_buffer(context, &dec->msg_fb_it_buffers[i]);
1420 rvid_clear_buffer(context, &dec->bs_buffers[i]);
1421 }
1422
1423 dpb_size = calc_dpb_size(dec);
1424 if (dpb_size) {
1425 if (!rvid_create_buffer(dec->screen, &dec->dpb, dpb_size, PIPE_USAGE_DEFAULT)) {
1426 RVID_ERR("Can't allocated dpb.\n");
1427 goto error;
1428 }
1429 rvid_clear_buffer(context, &dec->dpb);
1430 }
1431
1432 if (dec->stream_type == RUVD_CODEC_H264_PERF && info.family >= CHIP_POLARIS10) {
1433 unsigned ctx_size = calc_ctx_size_h264_perf(dec);
1434 if (!rvid_create_buffer(dec->screen, &dec->ctx, ctx_size, PIPE_USAGE_DEFAULT)) {
1435 RVID_ERR("Can't allocated context buffer.\n");
1436 goto error;
1437 }
1438 rvid_clear_buffer(context, &dec->ctx);
1439 }
1440
1441 if (info.family >= CHIP_POLARIS10 && info.drm_minor >= 3) {
1442 if (!rvid_create_buffer(dec->screen, &dec->sessionctx,
1443 UVD_SESSION_CONTEXT_SIZE,
1444 PIPE_USAGE_DEFAULT)) {
1445 RVID_ERR("Can't allocated session ctx.\n");
1446 goto error;
1447 }
1448 rvid_clear_buffer(context, &dec->sessionctx);
1449 }
1450
1451 if (info.family >= CHIP_VEGA10) {
1452 dec->reg.data0 = RUVD_GPCOM_VCPU_DATA0_SOC15;
1453 dec->reg.data1 = RUVD_GPCOM_VCPU_DATA1_SOC15;
1454 dec->reg.cmd = RUVD_GPCOM_VCPU_CMD_SOC15;
1455 dec->reg.cntl = RUVD_ENGINE_CNTL_SOC15;
1456 } else {
1457 dec->reg.data0 = RUVD_GPCOM_VCPU_DATA0;
1458 dec->reg.data1 = RUVD_GPCOM_VCPU_DATA1;
1459 dec->reg.cmd = RUVD_GPCOM_VCPU_CMD;
1460 dec->reg.cntl = RUVD_ENGINE_CNTL;
1461 }
1462
1463 map_msg_fb_it_buf(dec);
1464 dec->msg->size = sizeof(*dec->msg);
1465 dec->msg->msg_type = RUVD_MSG_CREATE;
1466 dec->msg->stream_handle = dec->stream_handle;
1467 dec->msg->body.create.stream_type = dec->stream_type;
1468 dec->msg->body.create.width_in_samples = dec->base.width;
1469 dec->msg->body.create.height_in_samples = dec->base.height;
1470 dec->msg->body.create.dpb_size = dpb_size;
1471 send_msg_buf(dec);
1472 r = flush(dec, 0);
1473 if (r)
1474 goto error;
1475
1476 next_buffer(dec);
1477
1478 return &dec->base;
1479
1480 error:
1481 if (dec->cs) dec->ws->cs_destroy(dec->cs);
1482
1483 for (i = 0; i < NUM_BUFFERS; ++i) {
1484 rvid_destroy_buffer(&dec->msg_fb_it_buffers[i]);
1485 rvid_destroy_buffer(&dec->bs_buffers[i]);
1486 }
1487
1488 rvid_destroy_buffer(&dec->dpb);
1489 rvid_destroy_buffer(&dec->ctx);
1490 rvid_destroy_buffer(&dec->sessionctx);
1491
1492 FREE(dec);
1493
1494 return NULL;
1495 }
1496
1497 /* calculate top/bottom offset */
1498 static unsigned texture_offset(struct radeon_surf *surface, unsigned layer,
1499 enum ruvd_surface_type type)
1500 {
1501 switch (type) {
1502 default:
1503 case RUVD_SURFACE_TYPE_LEGACY:
1504 return surface->u.legacy.level[0].offset +
1505 layer * surface->u.legacy.level[0].slice_size;
1506 break;
1507 case RUVD_SURFACE_TYPE_GFX9:
1508 return surface->u.gfx9.surf_offset +
1509 layer * surface->u.gfx9.surf_slice_size;
1510 break;
1511 }
1512 }
1513
1514 /* hw encode the aspect of macro tiles */
1515 static unsigned macro_tile_aspect(unsigned macro_tile_aspect)
1516 {
1517 switch (macro_tile_aspect) {
1518 default:
1519 case 1: macro_tile_aspect = 0; break;
1520 case 2: macro_tile_aspect = 1; break;
1521 case 4: macro_tile_aspect = 2; break;
1522 case 8: macro_tile_aspect = 3; break;
1523 }
1524 return macro_tile_aspect;
1525 }
1526
1527 /* hw encode the bank width and height */
1528 static unsigned bank_wh(unsigned bankwh)
1529 {
1530 switch (bankwh) {
1531 default:
1532 case 1: bankwh = 0; break;
1533 case 2: bankwh = 1; break;
1534 case 4: bankwh = 2; break;
1535 case 8: bankwh = 3; break;
1536 }
1537 return bankwh;
1538 }
1539
1540 /**
1541 * fill decoding target field from the luma and chroma surfaces
1542 */
1543 void ruvd_set_dt_surfaces(struct ruvd_msg *msg, struct radeon_surf *luma,
1544 struct radeon_surf *chroma, enum ruvd_surface_type type)
1545 {
1546 switch (type) {
1547 default:
1548 case RUVD_SURFACE_TYPE_LEGACY:
1549 msg->body.decode.dt_pitch = luma->u.legacy.level[0].nblk_x * luma->blk_w;
1550 switch (luma->u.legacy.level[0].mode) {
1551 case RADEON_SURF_MODE_LINEAR_ALIGNED:
1552 msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1553 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1554 break;
1555 case RADEON_SURF_MODE_1D:
1556 msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1557 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_1D_THIN;
1558 break;
1559 case RADEON_SURF_MODE_2D:
1560 msg->body.decode.dt_tiling_mode = RUVD_TILE_8X8;
1561 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_2D_THIN;
1562 break;
1563 default:
1564 assert(0);
1565 break;
1566 }
1567
1568 msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0, type);
1569 if (chroma)
1570 msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0, type);
1571 if (msg->body.decode.dt_field_mode) {
1572 msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1, type);
1573 if (chroma)
1574 msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1, type);
1575 } else {
1576 msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1577 msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1578 }
1579
1580 assert(luma->u.legacy.bankw == chroma->u.legacy.bankw);
1581 assert(luma->u.legacy.bankh == chroma->u.legacy.bankh);
1582 assert(luma->u.legacy.mtilea == chroma->u.legacy.mtilea);
1583
1584 msg->body.decode.dt_surf_tile_config |= RUVD_BANK_WIDTH(bank_wh(luma->u.legacy.bankw));
1585 msg->body.decode.dt_surf_tile_config |= RUVD_BANK_HEIGHT(bank_wh(luma->u.legacy.bankh));
1586 msg->body.decode.dt_surf_tile_config |= RUVD_MACRO_TILE_ASPECT_RATIO(macro_tile_aspect(luma->u.legacy.mtilea));
1587 break;
1588 case RUVD_SURFACE_TYPE_GFX9:
1589 msg->body.decode.dt_pitch = luma->u.gfx9.surf_pitch * luma->blk_w;
1590 /* SWIZZLE LINEAR MODE */
1591 msg->body.decode.dt_tiling_mode = RUVD_TILE_LINEAR;
1592 msg->body.decode.dt_array_mode = RUVD_ARRAY_MODE_LINEAR;
1593 msg->body.decode.dt_luma_top_offset = texture_offset(luma, 0, type);
1594 msg->body.decode.dt_chroma_top_offset = texture_offset(chroma, 0, type);
1595 if (msg->body.decode.dt_field_mode) {
1596 msg->body.decode.dt_luma_bottom_offset = texture_offset(luma, 1, type);
1597 msg->body.decode.dt_chroma_bottom_offset = texture_offset(chroma, 1, type);
1598 } else {
1599 msg->body.decode.dt_luma_bottom_offset = msg->body.decode.dt_luma_top_offset;
1600 msg->body.decode.dt_chroma_bottom_offset = msg->body.decode.dt_chroma_top_offset;
1601 }
1602 msg->body.decode.dt_surf_tile_config = 0;
1603 break;
1604 }
1605 }