2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Xiang Haihao <haihao.xiang@intel.com>
33 #include "intel_batchbuffer.h"
34 #include "intel_driver.h"
36 #include "i965_defines.h"
37 #include "i965_drv_video.h"
38 #include "i965_avc_bsd.h"
39 #include "i965_media_h264.h"
40 #include "i965_media.h"
41 #include "i965_decoder_utils.h"
42 #include "intel_media.h"
45 i965_avc_bsd_init_avc_bsd_surface(VADriverContextP ctx,
46 struct object_surface *obj_surface,
47 VAPictureParameterBufferH264 *pic_param,
48 struct i965_h264_context *i965_h264_context)
50 struct i965_driver_data *i965 = i965_driver_data(ctx);
51 GenAvcSurface *avc_bsd_surface = obj_surface->private_data;
53 obj_surface->free_private_data = gen_free_avc_surface;
55 if (!avc_bsd_surface) {
56 avc_bsd_surface = calloc(sizeof(GenAvcSurface), 1);
57 assert((obj_surface->size & 0x3f) == 0);
58 obj_surface->private_data = avc_bsd_surface;
61 avc_bsd_surface->dmv_bottom_flag = (pic_param->pic_fields.bits.field_pic_flag &&
62 !pic_param->seq_fields.bits.direct_8x8_inference_flag);
64 if (avc_bsd_surface->dmv_top == NULL) {
65 avc_bsd_surface->dmv_top = dri_bo_alloc(i965->intel.bufmgr,
66 "direct mv w/r buffer",
71 if (avc_bsd_surface->dmv_bottom_flag &&
72 avc_bsd_surface->dmv_bottom == NULL) {
73 avc_bsd_surface->dmv_bottom = dri_bo_alloc(i965->intel.bufmgr,
74 "direct mv w/r buffer",
81 i965_bsd_ind_obj_base_address(VADriverContextP ctx,
82 struct decode_state *decode_state,
84 struct i965_h264_context *i965_h264_context)
87 struct intel_batchbuffer *batch = i965_h264_context->batch;
89 dri_bo *ind_bo = decode_state->slice_datas[slice]->bo;
91 BEGIN_BCS_BATCH(batch, 3);
92 OUT_BCS_BATCH(batch, CMD_BSD_IND_OBJ_BASE_ADDR | (3 - 2));
93 OUT_BCS_RELOC(batch, ind_bo,
94 I915_GEM_DOMAIN_INSTRUCTION, 0,
96 OUT_BCS_BATCH(batch, 0);
97 ADVANCE_BCS_BATCH(batch);
101 i965_avc_bsd_img_state(VADriverContextP ctx,
102 struct decode_state *decode_state,
103 struct i965_h264_context *i965_h264_context)
105 struct intel_batchbuffer *batch = i965_h264_context->batch;
108 int mbaff_frame_flag;
109 unsigned int avc_it_command_header;
110 unsigned int width_in_mbs, height_in_mbs;
111 VAPictureParameterBufferH264 *pic_param;
113 if (decode_state->iq_matrix && decode_state->iq_matrix->buffer)
116 qm_present_flag = 0; /* built-in QM matrices */
118 assert(decode_state->pic_param && decode_state->pic_param->buffer);
119 pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
121 assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
123 if (pic_param->CurrPic.flags & VA_PICTURE_H264_TOP_FIELD)
125 else if (pic_param->CurrPic.flags & VA_PICTURE_H264_BOTTOM_FIELD)
130 if ((img_struct & 0x1) == 0x1) {
131 assert(pic_param->pic_fields.bits.field_pic_flag == 0x1);
133 assert(pic_param->pic_fields.bits.field_pic_flag == 0x0);
136 if (pic_param->seq_fields.bits.frame_mbs_only_flag) { /* a frame containing only frame macroblocks */
137 assert(pic_param->seq_fields.bits.mb_adaptive_frame_field_flag == 0);
138 assert(pic_param->pic_fields.bits.field_pic_flag == 0);
140 assert(pic_param->seq_fields.bits.direct_8x8_inference_flag == 1); /* see H.264 spec */
143 mbaff_frame_flag = (pic_param->seq_fields.bits.mb_adaptive_frame_field_flag &&
144 !pic_param->pic_fields.bits.field_pic_flag);
146 width_in_mbs = ((pic_param->picture_width_in_mbs_minus1 + 1) & 0xff);
147 height_in_mbs = ((pic_param->picture_height_in_mbs_minus1 + 1) & 0xff); /* frame height */
149 assert(!((width_in_mbs * height_in_mbs) & 0x8000)); /* hardware requirement */
151 /* BSD unit doesn't support 4:2:2 and 4:4:4 picture */
152 assert(pic_param->seq_fields.bits.chroma_format_idc == 0 || /* monochrome picture */
153 pic_param->seq_fields.bits.chroma_format_idc == 1); /* 4:2:0 */
154 assert(pic_param->seq_fields.bits.residual_colour_transform_flag == 0); /* only available for 4:4:4 */
156 avc_it_command_header = (CMD_MEDIA_OBJECT_EX | (12 - 2));
158 BEGIN_BCS_BATCH(batch, 6);
159 OUT_BCS_BATCH(batch, CMD_AVC_BSD_IMG_STATE | (6 - 2));
161 ((width_in_mbs * height_in_mbs) & 0x7fff));
163 (height_in_mbs << 16) |
164 (width_in_mbs << 0));
166 ((pic_param->second_chroma_qp_index_offset & 0x1f) << 24) |
167 ((pic_param->chroma_qp_index_offset & 0x1f) << 16) |
168 (SCAN_RASTER_ORDER << 15) | /* AVC ILDB Data */
169 (SCAN_SPECIAL_ORDER << 14) | /* AVC IT Command */
170 (SCAN_RASTER_ORDER << 13) | /* AVC IT Data */
171 (1 << 12) | /* always 1, hardware requirement */
172 (qm_present_flag << 10) |
174 (16 << 0)); /* FIXME: always support 16 reference frames ??? */
176 (RESIDUAL_DATA_OFFSET << 24) | /* residual data offset */
177 (0 << 17) | /* don't overwrite SRT */
178 (0 << 16) | /* Un-SRT (Unsynchronized Root Thread) */
179 (0 << 12) | /* FIXME: no 16MV ??? */
180 (pic_param->seq_fields.bits.chroma_format_idc << 10) |
181 (i965_h264_context->enable_avc_ildb << 8) | /* Enable ILDB writing output */
182 (pic_param->pic_fields.bits.entropy_coding_mode_flag << 7) |
183 ((!pic_param->pic_fields.bits.reference_pic_flag) << 6) |
184 (pic_param->pic_fields.bits.constrained_intra_pred_flag << 5) |
185 (pic_param->seq_fields.bits.direct_8x8_inference_flag << 4) |
186 (pic_param->pic_fields.bits.transform_8x8_mode_flag << 3) |
187 (pic_param->seq_fields.bits.frame_mbs_only_flag << 2) |
188 (mbaff_frame_flag << 1) |
189 (pic_param->pic_fields.bits.field_pic_flag << 0));
190 OUT_BCS_BATCH(batch, avc_it_command_header);
191 ADVANCE_BCS_BATCH(batch);
195 i965_avc_bsd_qm_state(VADriverContextP ctx,
196 struct decode_state *decode_state,
197 struct i965_h264_context *i965_h264_context)
199 struct intel_batchbuffer *batch = i965_h264_context->batch;
201 VAIQMatrixBufferH264 *iq_matrix;
202 VAPictureParameterBufferH264 *pic_param;
204 if (!decode_state->iq_matrix || !decode_state->iq_matrix->buffer)
207 iq_matrix = (VAIQMatrixBufferH264 *)decode_state->iq_matrix->buffer;
209 assert(decode_state->pic_param && decode_state->pic_param->buffer);
210 pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
212 cmd_len = 2 + 6 * 4; /* always load six 4x4 scaling matrices */
214 if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
215 cmd_len += 2 * 16; /* load two 8x8 scaling matrices */
217 BEGIN_BCS_BATCH(batch, cmd_len);
218 OUT_BCS_BATCH(batch, CMD_AVC_BSD_QM_STATE | (cmd_len - 2));
220 if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
222 (0x0 << 8) | /* don't use default built-in matrices */
223 (0xff << 0)); /* six 4x4 and two 8x8 scaling matrices */
226 (0x0 << 8) | /* don't use default built-in matrices */
227 (0x3f << 0)); /* six 4x4 scaling matrices */
229 intel_batchbuffer_data(batch, &iq_matrix->ScalingList4x4[0][0], 6 * 4 * 4);
231 if (pic_param->pic_fields.bits.transform_8x8_mode_flag)
232 intel_batchbuffer_data(batch, &iq_matrix->ScalingList8x8[0][0], 2 * 16 * 4);
234 ADVANCE_BCS_BATCH(batch);
238 i965_avc_bsd_slice_state(VADriverContextP ctx,
239 VAPictureParameterBufferH264 *pic_param,
240 VASliceParameterBufferH264 *slice_param,
241 struct i965_h264_context *i965_h264_context)
243 struct intel_batchbuffer *batch = i965_h264_context->batch;
244 int present_flag, cmd_len, list, j;
245 uint8_t ref_idx_state[32];
246 char weightoffsets[32 * 6];
248 /* don't issue SLICE_STATE for intra-prediction decoding */
249 if (slice_param->slice_type == SLICE_TYPE_I ||
250 slice_param->slice_type == SLICE_TYPE_SI)
255 if (slice_param->slice_type == SLICE_TYPE_P ||
256 slice_param->slice_type == SLICE_TYPE_SP) {
257 present_flag = PRESENT_REF_LIST0;
260 present_flag = PRESENT_REF_LIST0 | PRESENT_REF_LIST1;
264 if ((slice_param->slice_type == SLICE_TYPE_P ||
265 slice_param->slice_type == SLICE_TYPE_SP) &&
266 (pic_param->pic_fields.bits.weighted_pred_flag == 1)) {
267 present_flag |= PRESENT_WEIGHT_OFFSET_L0;
271 if ((slice_param->slice_type == SLICE_TYPE_B) &&
272 (pic_param->pic_fields.bits.weighted_bipred_idc == 1)) {
273 present_flag |= PRESENT_WEIGHT_OFFSET_L0 | PRESENT_WEIGHT_OFFSET_L1;
277 BEGIN_BCS_BATCH(batch, cmd_len);
278 OUT_BCS_BATCH(batch, CMD_AVC_BSD_SLICE_STATE | (cmd_len - 2));
279 OUT_BCS_BATCH(batch, present_flag);
281 for (list = 0; list < 2; list++) {
282 int flag, num_va_pics;
283 VAPictureH264 *va_pic;
286 flag = PRESENT_REF_LIST0;
287 va_pic = slice_param->RefPicList0;
288 num_va_pics = slice_param->num_ref_idx_l0_active_minus1 + 1;
290 flag = PRESENT_REF_LIST1;
291 va_pic = slice_param->RefPicList1;
292 num_va_pics = slice_param->num_ref_idx_l1_active_minus1 + 1;
295 if (!(present_flag & flag))
298 gen5_fill_avc_ref_idx_state(
301 i965_h264_context->fsid_list
303 intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
306 i965_h264_context->weight128_luma_l0 = 0;
307 i965_h264_context->weight128_luma_l1 = 0;
308 i965_h264_context->weight128_chroma_l0 = 0;
309 i965_h264_context->weight128_chroma_l1 = 0;
311 i965_h264_context->weight128_offset0_flag = 0;
312 i965_h264_context->weight128_offset0 = 0;
314 if (present_flag & PRESENT_WEIGHT_OFFSET_L0) {
315 for (j = 0; j < 32; j++) {
316 weightoffsets[j * 6 + 0] = slice_param->luma_offset_l0[j];
317 weightoffsets[j * 6 + 1] = slice_param->luma_weight_l0[j];
318 weightoffsets[j * 6 + 2] = slice_param->chroma_offset_l0[j][0];
319 weightoffsets[j * 6 + 3] = slice_param->chroma_weight_l0[j][0];
320 weightoffsets[j * 6 + 4] = slice_param->chroma_offset_l0[j][1];
321 weightoffsets[j * 6 + 5] = slice_param->chroma_weight_l0[j][1];
323 if (pic_param->pic_fields.bits.weighted_pred_flag == 1 ||
324 pic_param->pic_fields.bits.weighted_bipred_idc == 1) {
325 if (i965_h264_context->use_hw_w128) {
326 if (slice_param->luma_weight_l0[j] == 128)
327 i965_h264_context->weight128_luma_l0 |= (1 << j);
329 if (slice_param->chroma_weight_l0[j][0] == 128 ||
330 slice_param->chroma_weight_l0[j][1] == 128)
331 i965_h264_context->weight128_chroma_l0 |= (1 << j);
333 /* FIXME: workaround for weight 128 */
334 if (slice_param->luma_weight_l0[j] == 128 ||
335 slice_param->chroma_weight_l0[j][0] == 128 ||
336 slice_param->chroma_weight_l0[j][1] == 128)
337 i965_h264_context->weight128_offset0_flag = 1;
342 intel_batchbuffer_data(batch, weightoffsets, sizeof(weightoffsets));
345 if (present_flag & PRESENT_WEIGHT_OFFSET_L1) {
346 for (j = 0; j < 32; j++) {
347 weightoffsets[j * 6 + 0] = slice_param->luma_offset_l1[j];
348 weightoffsets[j * 6 + 1] = slice_param->luma_weight_l1[j];
349 weightoffsets[j * 6 + 2] = slice_param->chroma_offset_l1[j][0];
350 weightoffsets[j * 6 + 3] = slice_param->chroma_weight_l1[j][0];
351 weightoffsets[j * 6 + 4] = slice_param->chroma_offset_l1[j][1];
352 weightoffsets[j * 6 + 5] = slice_param->chroma_weight_l1[j][1];
354 if (pic_param->pic_fields.bits.weighted_bipred_idc == 1) {
355 if (i965_h264_context->use_hw_w128) {
356 if (slice_param->luma_weight_l1[j] == 128)
357 i965_h264_context->weight128_luma_l1 |= (1 << j);
359 if (slice_param->chroma_weight_l1[j][0] == 128 ||
360 slice_param->chroma_weight_l1[j][1] == 128)
361 i965_h264_context->weight128_chroma_l1 |= (1 << j);
363 if (slice_param->luma_weight_l0[j] == 128 ||
364 slice_param->chroma_weight_l0[j][0] == 128 ||
365 slice_param->chroma_weight_l0[j][1] == 128)
366 i965_h264_context->weight128_offset0_flag = 1;
371 intel_batchbuffer_data(batch, weightoffsets, sizeof(weightoffsets));
374 ADVANCE_BCS_BATCH(batch);
378 i965_avc_bsd_buf_base_state(VADriverContextP ctx,
379 VAPictureParameterBufferH264 *pic_param,
380 VASliceParameterBufferH264 *slice_param,
381 struct i965_h264_context *i965_h264_context)
383 struct i965_driver_data *i965 = i965_driver_data(ctx);
384 struct intel_batchbuffer *batch = i965_h264_context->batch;
385 struct i965_avc_bsd_context *i965_avc_bsd_context;
387 VAPictureH264 *va_pic;
388 struct object_surface *obj_surface;
389 GenAvcSurface *avc_bsd_surface;
391 i965_avc_bsd_context = &i965_h264_context->i965_avc_bsd_context;
393 BEGIN_BCS_BATCH(batch, 74);
394 OUT_BCS_BATCH(batch, CMD_AVC_BSD_BUF_BASE_STATE | (74 - 2));
395 OUT_BCS_RELOC(batch, i965_avc_bsd_context->bsd_raw_store.bo,
396 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
398 OUT_BCS_RELOC(batch, i965_avc_bsd_context->mpr_row_store.bo,
399 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
401 OUT_BCS_RELOC(batch, i965_h264_context->avc_it_command_mb_info.bo,
402 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
403 i965_h264_context->avc_it_command_mb_info.mbs * i965_h264_context->use_avc_hw_scoreboard * MB_CMD_IN_BYTES);
404 OUT_BCS_RELOC(batch, i965_h264_context->avc_it_data.bo,
405 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
406 (i965_h264_context->avc_it_data.write_offset << 6));
408 if (i965_h264_context->enable_avc_ildb)
409 OUT_BCS_RELOC(batch, i965_h264_context->avc_ildb_data.bo,
410 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
413 OUT_BCS_BATCH(batch, 0);
415 for (i = 0; i < ARRAY_ELEMS(i965_h264_context->fsid_list); i++) {
416 if (i965_h264_context->fsid_list[i].surface_id != VA_INVALID_ID) {
418 for (j = 0; j < ARRAY_ELEMS(pic_param->ReferenceFrames); j++) {
419 va_pic = &pic_param->ReferenceFrames[j];
421 if (va_pic->flags & VA_PICTURE_H264_INVALID)
424 if (va_pic->picture_id == i965_h264_context->fsid_list[i].surface_id) {
432 if (!(va_pic->flags & VA_PICTURE_H264_INVALID)) {
433 obj_surface = SURFACE(va_pic->picture_id);
435 avc_bsd_surface = obj_surface->private_data;
437 if (avc_bsd_surface == NULL) {
438 OUT_BCS_BATCH(batch, 0);
439 OUT_BCS_BATCH(batch, 0);
441 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_top,
442 I915_GEM_DOMAIN_INSTRUCTION, 0,
445 if (avc_bsd_surface->dmv_bottom_flag == 1)
446 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_bottom,
447 I915_GEM_DOMAIN_INSTRUCTION, 0,
450 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_top,
451 I915_GEM_DOMAIN_INSTRUCTION, 0,
456 OUT_BCS_BATCH(batch, 0);
457 OUT_BCS_BATCH(batch, 0);
461 va_pic = &pic_param->CurrPic;
462 assert(!(va_pic->flags & VA_PICTURE_H264_INVALID));
463 obj_surface = SURFACE(va_pic->picture_id);
465 obj_surface->flags &= ~SURFACE_REF_DIS_MASK;
466 obj_surface->flags |= (pic_param->pic_fields.bits.reference_pic_flag ? SURFACE_REFERENCED : 0);
467 i965_check_alloc_surface_bo(ctx, obj_surface, 0, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
468 i965_avc_bsd_init_avc_bsd_surface(ctx, obj_surface, pic_param, i965_h264_context);
469 avc_bsd_surface = obj_surface->private_data;
471 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_top,
472 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
475 if (avc_bsd_surface->dmv_bottom_flag == 1)
476 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_bottom,
477 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
480 OUT_BCS_RELOC(batch, avc_bsd_surface->dmv_top,
481 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
485 for (i = 0; i < ARRAY_ELEMS(i965_h264_context->fsid_list); i++) {
486 if (i965_h264_context->fsid_list[i].surface_id != VA_INVALID_ID) {
488 for (j = 0; j < ARRAY_ELEMS(pic_param->ReferenceFrames); j++) {
489 va_pic = &pic_param->ReferenceFrames[j];
491 if (va_pic->flags & VA_PICTURE_H264_INVALID)
494 if (va_pic->picture_id == i965_h264_context->fsid_list[i].surface_id) {
502 if (!(va_pic->flags & VA_PICTURE_H264_INVALID)) {
503 OUT_BCS_BATCH(batch, va_pic->TopFieldOrderCnt);
504 OUT_BCS_BATCH(batch, va_pic->BottomFieldOrderCnt);
507 OUT_BCS_BATCH(batch, 0);
508 OUT_BCS_BATCH(batch, 0);
512 va_pic = &pic_param->CurrPic;
513 OUT_BCS_BATCH(batch, va_pic->TopFieldOrderCnt);
514 OUT_BCS_BATCH(batch, va_pic->BottomFieldOrderCnt);
516 ADVANCE_BCS_BATCH(batch);
520 * Return the bit offset to the first bit of the slice data
522 * VASliceParameterBufferH264.slice_data_bit_offset will point into the part
523 * of slice header if there are some escaped bytes in the slice header. The offset
524 * to slice data is needed for BSD unit so that BSD unit can fetch right slice data
525 * for processing. This fixes conformance case BASQP1_Sony_C.jsv
528 i965_avc_bsd_get_slice_bit_offset(uint8_t *buf, int mode_flag, int in_slice_data_bit_offset)
530 int out_slice_data_bit_offset;
531 int slice_header_size = in_slice_data_bit_offset / 8;
534 for (i = 0, j = 0; i < slice_header_size; i++, j++) {
535 if (!buf[j] && !buf[j + 1] && buf[j + 2] == 3) {
540 out_slice_data_bit_offset = 8 * j + in_slice_data_bit_offset % 8;
542 if (mode_flag == ENTROPY_CABAC)
543 out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
545 return out_slice_data_bit_offset;
549 g4x_avc_bsd_object(VADriverContextP ctx,
550 struct decode_state *decode_state,
551 VAPictureParameterBufferH264 *pic_param,
552 VASliceParameterBufferH264 *slice_param,
554 struct i965_h264_context *i965_h264_context)
556 struct intel_batchbuffer *batch = i965_h264_context->batch;
557 int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
558 int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1; /* frame height */
561 int encrypted, counter_value, cmd_len;
562 int slice_hor_pos, slice_ver_pos;
563 int num_ref_idx_l0, num_ref_idx_l1;
564 int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
565 pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
566 int slice_data_bit_offset;
567 int weighted_pred_idc = 0;
568 int first_mb_in_slice = 0;
570 uint8_t *slice_data = NULL;
572 encrypted = 0; /* FIXME: which flag in VAAPI is used for encryption? */
576 counter_value = 0; /* FIXME: ??? */
580 dri_bo_map(decode_state->slice_datas[slice_index]->bo, 0);
581 slice_data = (uint8_t *)(decode_state->slice_datas[slice_index]->bo->virtual + slice_param->slice_data_offset);
582 slice_data_bit_offset = i965_avc_bsd_get_slice_bit_offset(slice_data,
583 pic_param->pic_fields.bits.entropy_coding_mode_flag,
584 slice_param->slice_data_bit_offset);
585 dri_bo_unmap(decode_state->slice_datas[slice_index]->bo);
587 if (slice_param->slice_type == SLICE_TYPE_I ||
588 slice_param->slice_type == SLICE_TYPE_SI)
589 slice_type = SLICE_TYPE_I;
590 else if (slice_param->slice_type == SLICE_TYPE_P ||
591 slice_param->slice_type == SLICE_TYPE_SP)
592 slice_type = SLICE_TYPE_P;
594 assert(slice_param->slice_type == SLICE_TYPE_B);
595 slice_type = SLICE_TYPE_B;
598 if (slice_type == SLICE_TYPE_I) {
599 assert(slice_param->num_ref_idx_l0_active_minus1 == 0);
600 assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
603 } else if (slice_type == SLICE_TYPE_P) {
604 assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
605 num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
608 num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
609 num_ref_idx_l1 = slice_param->num_ref_idx_l1_active_minus1 + 1;
612 if (slice_type == SLICE_TYPE_P)
613 weighted_pred_idc = pic_param->pic_fields.bits.weighted_pred_flag;
614 else if (slice_type == SLICE_TYPE_B)
615 weighted_pred_idc = pic_param->pic_fields.bits.weighted_bipred_idc;
617 first_mb_in_slice = slice_param->first_mb_in_slice << mbaff_picture;
618 slice_hor_pos = first_mb_in_slice % width_in_mbs;
619 slice_ver_pos = first_mb_in_slice / width_in_mbs;
621 BEGIN_BCS_BATCH(batch, cmd_len);
622 OUT_BCS_BATCH(batch, CMD_AVC_BSD_OBJECT | (cmd_len - 2));
625 ((slice_param->slice_data_size - (slice_data_bit_offset >> 3)) << 0));
627 (slice_param->slice_data_offset +
628 (slice_data_bit_offset >> 3)));
630 (0 << 31) | /* concealment mode: 0->intra 16x16 prediction, 1->inter P Copy */
631 (0 << 14) | /* ignore BSDPrematureComplete Error handling */
632 (0 << 13) | /* FIXME: ??? */
633 (0 << 12) | /* ignore MPR Error handling */
634 (0 << 10) | /* ignore Entropy Error handling */
635 (0 << 8) | /* ignore MB Header Error handling */
638 (num_ref_idx_l1 << 24) |
639 (num_ref_idx_l0 << 16) |
640 (slice_param->chroma_log2_weight_denom << 8) |
641 (slice_param->luma_log2_weight_denom << 0));
643 (weighted_pred_idc << 30) |
644 (slice_param->direct_spatial_mv_pred_flag << 29) |
645 (slice_param->disable_deblocking_filter_idc << 27) |
646 (slice_param->cabac_init_idc << 24) |
647 ((pic_param->pic_init_qp_minus26 + 26 + slice_param->slice_qp_delta) << 16) |
648 ((slice_param->slice_beta_offset_div2 & 0xf) << 8) |
649 ((slice_param->slice_alpha_c0_offset_div2 & 0xf) << 0));
651 (slice_ver_pos << 24) |
652 (slice_hor_pos << 16) |
653 (first_mb_in_slice << 0));
655 (0 << 7) | /* FIXME: ??? */
656 ((0x7 - (slice_data_bit_offset & 0x7)) << 0));
659 OUT_BCS_BATCH(batch, counter_value);
662 ADVANCE_BCS_BATCH(batch);
664 BEGIN_BCS_BATCH(batch, 8);
665 OUT_BCS_BATCH(batch, CMD_AVC_BSD_OBJECT | (8 - 2));
666 OUT_BCS_BATCH(batch, 0); /* indirect data length for phantom slice is 0 */
667 OUT_BCS_BATCH(batch, 0); /* indirect data start address for phantom slice is 0 */
668 OUT_BCS_BATCH(batch, 0);
669 OUT_BCS_BATCH(batch, 0);
670 OUT_BCS_BATCH(batch, 0);
671 OUT_BCS_BATCH(batch, width_in_mbs * height_in_mbs / (1 + !!pic_param->pic_fields.bits.field_pic_flag));
672 OUT_BCS_BATCH(batch, 0);
673 ADVANCE_BCS_BATCH(batch);
678 ironlake_avc_bsd_object(VADriverContextP ctx,
679 struct decode_state *decode_state,
680 VAPictureParameterBufferH264 *pic_param,
681 VASliceParameterBufferH264 *slice_param,
683 struct i965_h264_context *i965_h264_context)
685 struct intel_batchbuffer *batch = i965_h264_context->batch;
686 int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
687 int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1; /* frame height */
690 int encrypted, counter_value;
691 int slice_hor_pos, slice_ver_pos;
692 int num_ref_idx_l0, num_ref_idx_l1;
693 int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
694 pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
695 int slice_data_bit_offset;
696 int weighted_pred_idc = 0;
697 int first_mb_in_slice;
699 uint8_t *slice_data = NULL;
701 encrypted = 0; /* FIXME: which flag in VAAPI is used for encryption? */
704 counter_value = 0; /* FIXME: ??? */
708 dri_bo_map(decode_state->slice_datas[slice_index]->bo, 0);
709 slice_data = (uint8_t *)(decode_state->slice_datas[slice_index]->bo->virtual + slice_param->slice_data_offset);
710 slice_data_bit_offset = i965_avc_bsd_get_slice_bit_offset(slice_data,
711 pic_param->pic_fields.bits.entropy_coding_mode_flag,
712 slice_param->slice_data_bit_offset);
713 dri_bo_unmap(decode_state->slice_datas[slice_index]->bo);
715 if (slice_param->slice_type == SLICE_TYPE_I ||
716 slice_param->slice_type == SLICE_TYPE_SI)
717 slice_type = SLICE_TYPE_I;
718 else if (slice_param->slice_type == SLICE_TYPE_P ||
719 slice_param->slice_type == SLICE_TYPE_SP)
720 slice_type = SLICE_TYPE_P;
722 assert(slice_param->slice_type == SLICE_TYPE_B);
723 slice_type = SLICE_TYPE_B;
726 if (slice_type == SLICE_TYPE_I) {
727 assert(slice_param->num_ref_idx_l0_active_minus1 == 0);
728 assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
731 } else if (slice_type == SLICE_TYPE_P) {
732 assert(slice_param->num_ref_idx_l1_active_minus1 == 0);
733 num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
736 num_ref_idx_l0 = slice_param->num_ref_idx_l0_active_minus1 + 1;
737 num_ref_idx_l1 = slice_param->num_ref_idx_l1_active_minus1 + 1;
740 if (slice_type == SLICE_TYPE_P)
741 weighted_pred_idc = pic_param->pic_fields.bits.weighted_pred_flag;
742 else if (slice_type == SLICE_TYPE_B)
743 weighted_pred_idc = pic_param->pic_fields.bits.weighted_bipred_idc;
745 first_mb_in_slice = slice_param->first_mb_in_slice << mbaff_picture;
746 slice_hor_pos = first_mb_in_slice % width_in_mbs;
747 slice_ver_pos = first_mb_in_slice / width_in_mbs;
749 BEGIN_BCS_BATCH(batch, 16);
750 OUT_BCS_BATCH(batch, CMD_AVC_BSD_OBJECT | (16 - 2));
753 (0 << 30) | /* FIXME: packet based bit stream */
754 (0 << 29) | /* FIXME: packet format */
755 ((slice_param->slice_data_size - (slice_data_bit_offset >> 3)) << 0));
757 (slice_param->slice_data_offset +
758 (slice_data_bit_offset >> 3)));
760 (0 << 31) | /* concealment mode: 0->intra 16x16 prediction, 1->inter P Copy */
761 (0 << 14) | /* ignore BSDPrematureComplete Error handling */
762 (0 << 13) | /* FIXME: ??? */
763 (0 << 12) | /* ignore MPR Error handling */
764 (0 << 10) | /* ignore Entropy Error handling */
765 (0 << 8) | /* ignore MB Header Error handling */
768 (num_ref_idx_l1 << 24) |
769 (num_ref_idx_l0 << 16) |
770 (slice_param->chroma_log2_weight_denom << 8) |
771 (slice_param->luma_log2_weight_denom << 0));
773 (weighted_pred_idc << 30) |
774 (slice_param->direct_spatial_mv_pred_flag << 29) |
775 (slice_param->disable_deblocking_filter_idc << 27) |
776 (slice_param->cabac_init_idc << 24) |
777 ((pic_param->pic_init_qp_minus26 + 26 + slice_param->slice_qp_delta) << 16) |
778 ((slice_param->slice_beta_offset_div2 & 0xf) << 8) |
779 ((slice_param->slice_alpha_c0_offset_div2 & 0xf) << 0));
781 (slice_ver_pos << 24) |
782 (slice_hor_pos << 16) |
783 (first_mb_in_slice << 0));
785 (0 << 7) | /* FIXME: ??? */
786 ((0x7 - (slice_data_bit_offset & 0x7)) << 0));
787 OUT_BCS_BATCH(batch, counter_value);
789 /* FIXME: dw9-dw11 */
790 OUT_BCS_BATCH(batch, 0);
791 OUT_BCS_BATCH(batch, 0);
792 OUT_BCS_BATCH(batch, 0);
793 OUT_BCS_BATCH(batch, i965_h264_context->weight128_luma_l0);
794 OUT_BCS_BATCH(batch, i965_h264_context->weight128_luma_l1);
795 OUT_BCS_BATCH(batch, i965_h264_context->weight128_chroma_l0);
796 OUT_BCS_BATCH(batch, i965_h264_context->weight128_chroma_l1);
798 ADVANCE_BCS_BATCH(batch);
800 BEGIN_BCS_BATCH(batch, 16);
801 OUT_BCS_BATCH(batch, CMD_AVC_BSD_OBJECT | (16 - 2));
802 OUT_BCS_BATCH(batch, 0); /* indirect data length for phantom slice is 0 */
803 OUT_BCS_BATCH(batch, 0); /* indirect data start address for phantom slice is 0 */
804 OUT_BCS_BATCH(batch, 0);
805 OUT_BCS_BATCH(batch, 0);
806 OUT_BCS_BATCH(batch, 0);
807 OUT_BCS_BATCH(batch, width_in_mbs * height_in_mbs / (1 + !!pic_param->pic_fields.bits.field_pic_flag));
808 OUT_BCS_BATCH(batch, 0);
809 OUT_BCS_BATCH(batch, 0);
810 OUT_BCS_BATCH(batch, 0);
811 OUT_BCS_BATCH(batch, 0);
812 OUT_BCS_BATCH(batch, 0);
813 OUT_BCS_BATCH(batch, 0);
814 OUT_BCS_BATCH(batch, 0);
815 OUT_BCS_BATCH(batch, 0);
816 OUT_BCS_BATCH(batch, 0);
817 ADVANCE_BCS_BATCH(batch);
822 i965_avc_bsd_object(VADriverContextP ctx,
823 struct decode_state *decode_state,
824 VAPictureParameterBufferH264 *pic_param,
825 VASliceParameterBufferH264 *slice_param,
827 struct i965_h264_context *i965_h264_context)
829 struct i965_driver_data *i965 = i965_driver_data(ctx);
831 if (IS_IRONLAKE(i965->intel.device_id))
832 ironlake_avc_bsd_object(ctx, decode_state, pic_param, slice_param, slice_index, i965_h264_context);
834 g4x_avc_bsd_object(ctx, decode_state, pic_param, slice_param, slice_index, i965_h264_context);
838 i965_avc_bsd_phantom_slice(VADriverContextP ctx,
839 struct decode_state *decode_state,
840 VAPictureParameterBufferH264 *pic_param,
841 struct i965_h264_context *i965_h264_context)
843 i965_avc_bsd_object(ctx, decode_state, pic_param, NULL, 0, i965_h264_context);
847 i965_avc_bsd_frame_store_index(VADriverContextP ctx,
848 VAPictureParameterBufferH264 *pic_param,
849 struct i965_h264_context *i965_h264_context)
851 struct i965_driver_data *i965 = i965_driver_data(ctx);
854 assert(ARRAY_ELEMS(i965_h264_context->fsid_list) == ARRAY_ELEMS(pic_param->ReferenceFrames));
856 for (i = 0; i < ARRAY_ELEMS(i965_h264_context->fsid_list); i++) {
859 if (i965_h264_context->fsid_list[i].surface_id == VA_INVALID_ID)
862 for (j = 0; j < ARRAY_ELEMS(pic_param->ReferenceFrames); j++) {
863 VAPictureH264 *ref_pic = &pic_param->ReferenceFrames[j];
864 if (ref_pic->flags & VA_PICTURE_H264_INVALID)
867 if (i965_h264_context->fsid_list[i].surface_id == ref_pic->picture_id) {
874 struct object_surface *obj_surface = SURFACE(i965_h264_context->fsid_list[i].surface_id);
875 obj_surface->flags &= ~SURFACE_REFERENCED;
877 if ((obj_surface->flags & SURFACE_ALL_MASK) == SURFACE_DISPLAYED) {
878 dri_bo_unreference(obj_surface->bo);
879 obj_surface->bo = NULL;
880 obj_surface->flags &= ~SURFACE_REF_DIS_MASK;
883 if (obj_surface->free_private_data)
884 obj_surface->free_private_data(&obj_surface->private_data);
886 i965_h264_context->fsid_list[i].surface_id = VA_INVALID_ID;
887 i965_h264_context->fsid_list[i].frame_store_id = -1;
891 for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
892 VAPictureH264 *ref_pic = &pic_param->ReferenceFrames[i];
895 if (ref_pic->flags & VA_PICTURE_H264_INVALID)
898 for (j = 0; j < ARRAY_ELEMS(i965_h264_context->fsid_list); j++) {
899 if (i965_h264_context->fsid_list[j].surface_id == VA_INVALID_ID)
902 if (i965_h264_context->fsid_list[j].surface_id == ref_pic->picture_id) {
910 struct object_surface *obj_surface = SURFACE(ref_pic->picture_id);
912 i965_check_alloc_surface_bo(ctx, obj_surface, 0, VA_FOURCC('N','V','1','2'), SUBSAMPLE_YUV420);
914 for (frame_idx = 0; frame_idx < ARRAY_ELEMS(i965_h264_context->fsid_list); frame_idx++) {
915 for (j = 0; j < ARRAY_ELEMS(i965_h264_context->fsid_list); j++) {
916 if (i965_h264_context->fsid_list[j].surface_id == VA_INVALID_ID)
919 if (i965_h264_context->fsid_list[j].frame_store_id == frame_idx)
923 if (j == ARRAY_ELEMS(i965_h264_context->fsid_list))
927 assert(frame_idx < ARRAY_ELEMS(i965_h264_context->fsid_list));
929 for (j = 0; j < ARRAY_ELEMS(i965_h264_context->fsid_list); j++) {
930 if (i965_h264_context->fsid_list[j].surface_id == VA_INVALID_ID) {
931 i965_h264_context->fsid_list[j].surface_id = ref_pic->picture_id;
932 i965_h264_context->fsid_list[j].frame_store_id = frame_idx;
939 for (i = 0; i < ARRAY_ELEMS(i965_h264_context->fsid_list) - 1; i++) {
940 if (i965_h264_context->fsid_list[i].surface_id != VA_INVALID_ID &&
941 i965_h264_context->fsid_list[i].frame_store_id == i)
944 for (j = i + 1; j < ARRAY_ELEMS(i965_h264_context->fsid_list); j++) {
945 if (i965_h264_context->fsid_list[j].surface_id != VA_INVALID_ID &&
946 i965_h264_context->fsid_list[j].frame_store_id == i) {
947 VASurfaceID id = i965_h264_context->fsid_list[i].surface_id;
948 int frame_idx = i965_h264_context->fsid_list[i].frame_store_id;
950 i965_h264_context->fsid_list[i].surface_id = i965_h264_context->fsid_list[j].surface_id;
951 i965_h264_context->fsid_list[i].frame_store_id = i965_h264_context->fsid_list[j].frame_store_id;
952 i965_h264_context->fsid_list[j].surface_id = id;
953 i965_h264_context->fsid_list[j].frame_store_id = frame_idx;
961 i965_avc_bsd_pipeline(VADriverContextP ctx, struct decode_state *decode_state, void *h264_context)
963 struct i965_h264_context *i965_h264_context = (struct i965_h264_context *)h264_context;
964 struct intel_batchbuffer *batch = i965_h264_context->batch;
965 VAPictureParameterBufferH264 *pic_param;
966 VASliceParameterBufferH264 *slice_param;
969 assert(decode_state->pic_param && decode_state->pic_param->buffer);
970 pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
971 i965_avc_bsd_frame_store_index(ctx, pic_param, i965_h264_context);
973 i965_h264_context->enable_avc_ildb = 0;
974 i965_h264_context->picture.i_flag = 1;
976 for (j = 0; j < decode_state->num_slice_params && i965_h264_context->enable_avc_ildb == 0; j++) {
977 assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
978 slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
980 for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
981 assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
982 assert((slice_param->slice_type == SLICE_TYPE_I) ||
983 (slice_param->slice_type == SLICE_TYPE_SI) ||
984 (slice_param->slice_type == SLICE_TYPE_P) ||
985 (slice_param->slice_type == SLICE_TYPE_SP) ||
986 (slice_param->slice_type == SLICE_TYPE_B));
988 if (slice_param->disable_deblocking_filter_idc != 1) {
989 i965_h264_context->enable_avc_ildb = 1;
997 intel_batchbuffer_start_atomic_bcs(batch, 0x1000);
999 i965_avc_bsd_img_state(ctx, decode_state, i965_h264_context);
1000 i965_avc_bsd_qm_state(ctx, decode_state, i965_h264_context);
1002 for (j = 0; j < decode_state->num_slice_params; j++) {
1003 assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
1004 slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
1006 i965_bsd_ind_obj_base_address(ctx, decode_state, j, i965_h264_context);
1008 for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
1009 assert(slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL);
1010 assert((slice_param->slice_type == SLICE_TYPE_I) ||
1011 (slice_param->slice_type == SLICE_TYPE_SI) ||
1012 (slice_param->slice_type == SLICE_TYPE_P) ||
1013 (slice_param->slice_type == SLICE_TYPE_SP) ||
1014 (slice_param->slice_type == SLICE_TYPE_B));
1016 if (i965_h264_context->picture.i_flag &&
1017 (slice_param->slice_type != SLICE_TYPE_I ||
1018 slice_param->slice_type != SLICE_TYPE_SI))
1019 i965_h264_context->picture.i_flag = 0;
1021 i965_avc_bsd_slice_state(ctx, pic_param, slice_param, i965_h264_context);
1022 i965_avc_bsd_buf_base_state(ctx, pic_param, slice_param, i965_h264_context);
1023 i965_avc_bsd_object(ctx, decode_state, pic_param, slice_param, j, i965_h264_context);
1028 i965_avc_bsd_phantom_slice(ctx, decode_state, pic_param, i965_h264_context);
1029 intel_batchbuffer_emit_mi_flush(batch);
1030 intel_batchbuffer_end_atomic(batch);
1031 intel_batchbuffer_flush(batch);
1035 i965_avc_bsd_decode_init(VADriverContextP ctx, void *h264_context)
1037 struct i965_driver_data *i965 = i965_driver_data(ctx);
1038 struct i965_h264_context *i965_h264_context = (struct i965_h264_context *)h264_context;
1039 struct i965_avc_bsd_context *i965_avc_bsd_context;
1042 assert(i965_h264_context);
1043 i965_avc_bsd_context = &i965_h264_context->i965_avc_bsd_context;
1045 dri_bo_unreference(i965_avc_bsd_context->bsd_raw_store.bo);
1046 bo = dri_bo_alloc(i965->intel.bufmgr,
1048 0x3000, /* at least 11520 bytes to support 120 MBs per row */
1051 i965_avc_bsd_context->bsd_raw_store.bo = bo;
1053 dri_bo_unreference(i965_avc_bsd_context->mpr_row_store.bo);
1054 bo = dri_bo_alloc(i965->intel.bufmgr,
1056 0x2000, /* at least 7680 bytes to support 120 MBs per row */
1059 i965_avc_bsd_context->mpr_row_store.bo = bo;
1063 i965_avc_bsd_ternimate(struct i965_avc_bsd_context *i965_avc_bsd_context)
1065 dri_bo_unreference(i965_avc_bsd_context->bsd_raw_store.bo);
1066 dri_bo_unreference(i965_avc_bsd_context->mpr_row_store.bo);