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