2 * Copyright (C) 2006-2012 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 "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
28 #include "intel_batchbuffer.h"
29 #include "i965_drv_video.h"
30 #include "i965_decoder_utils.h"
31 #include "i965_defines.h"
33 /* Set reference surface if backing store exists */
36 struct i965_driver_data *i965,
37 GenFrameStore *ref_frame,
38 VASurfaceID va_surface,
39 struct object_surface *obj_surface
42 if (va_surface == VA_INVALID_ID)
45 if (!obj_surface || !obj_surface->bo)
48 ref_frame->surface_id = va_surface;
49 ref_frame->obj_surface = obj_surface;
53 /* Check wether codec layer incorrectly fills in slice_vertical_position */
55 mpeg2_wa_slice_vertical_position(
56 struct decode_state *decode_state,
57 VAPictureParameterBufferMPEG2 *pic_param
60 unsigned int i, j, mb_height, vpos, last_vpos = 0;
62 /* Assume progressive sequence if we got a progressive frame */
63 if (pic_param->picture_coding_extension.bits.progressive_frame)
66 /* Wait for a field coded picture */
67 if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_FRAME)
70 assert(decode_state && decode_state->slice_params);
72 mb_height = (pic_param->vertical_size + 31) / 32;
74 for (j = 0; j < decode_state->num_slice_params; j++) {
75 struct buffer_store * const buffer_store =
76 decode_state->slice_params[j];
78 for (i = 0; i < buffer_store->num_elements; i++) {
79 VASliceParameterBufferMPEG2 * const slice_param =
80 ((VASliceParameterBufferMPEG2 *)buffer_store->buffer) + i;
82 vpos = slice_param->slice_vertical_position;
83 if (vpos >= mb_height || vpos == last_vpos + 2) {
84 WARN_ONCE("codec layer incorrectly fills in MPEG-2 slice_vertical_position. Workaround applied\n");
93 /* Build MPEG-2 reference frames array */
95 mpeg2_set_reference_surfaces(
97 GenFrameStore ref_frames[MAX_GEN_REFERENCE_FRAMES],
98 struct decode_state *decode_state,
99 VAPictureParameterBufferMPEG2 *pic_param
102 struct i965_driver_data * const i965 = i965_driver_data(ctx);
103 VASurfaceID va_surface;
104 unsigned pic_structure, is_second_field, n = 0;
105 struct object_surface *obj_surface;
107 pic_structure = pic_param->picture_coding_extension.bits.picture_structure;
108 is_second_field = pic_structure != MPEG_FRAME &&
109 !pic_param->picture_coding_extension.bits.is_first_field;
111 ref_frames[0].surface_id = VA_INVALID_ID;
112 ref_frames[0].obj_surface = NULL;
114 /* Reference frames are indexed by frame store ID (0:top, 1:bottom) */
115 switch (pic_param->picture_coding_type) {
117 if (is_second_field && pic_structure == MPEG_BOTTOM_FIELD) {
118 va_surface = decode_state->current_render_target;
119 obj_surface = decode_state->render_object;
120 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
122 va_surface = pic_param->forward_reference_picture;
123 obj_surface = decode_state->reference_objects[0];
124 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
128 va_surface = pic_param->forward_reference_picture;
129 obj_surface = decode_state->reference_objects[0];
130 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
131 va_surface = pic_param->backward_reference_picture;
132 obj_surface = decode_state->reference_objects[1];
133 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
138 ref_frames[n].obj_surface = ref_frames[0].obj_surface;
139 ref_frames[n++].surface_id = ref_frames[0].surface_id;
142 if (pic_param->picture_coding_extension.bits.frame_pred_frame_dct)
145 ref_frames[2].surface_id = VA_INVALID_ID;
146 ref_frames[2].obj_surface = NULL;
148 /* Bottom field pictures used as reference */
149 switch (pic_param->picture_coding_type) {
151 if (is_second_field && pic_structure == MPEG_TOP_FIELD) {
152 va_surface = decode_state->current_render_target;
153 obj_surface = decode_state->render_object;
154 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
156 va_surface = pic_param->forward_reference_picture;
157 obj_surface = decode_state->reference_objects[0];
158 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
162 va_surface = pic_param->forward_reference_picture;
163 obj_surface = decode_state->reference_objects[0];
164 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
165 va_surface = pic_param->backward_reference_picture;
166 obj_surface = decode_state->reference_objects[1];
167 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
172 ref_frames[n].obj_surface = ref_frames[2].obj_surface;
173 ref_frames[n++].surface_id = ref_frames[2].surface_id;
177 /* Ensure the supplied VA surface has valid storage for decoding the
180 avc_ensure_surface_bo(
181 VADriverContextP ctx,
182 struct decode_state *decode_state,
183 struct object_surface *obj_surface,
184 const VAPictureParameterBufferH264 *pic_param
188 uint32_t hw_fourcc, fourcc, subsample, chroma_format;
190 /* Validate chroma format */
191 switch (pic_param->seq_fields.bits.chroma_format_idc) {
193 fourcc = VA_FOURCC_Y800;
194 subsample = SUBSAMPLE_YUV400;
195 chroma_format = VA_RT_FORMAT_YUV400;
198 fourcc = VA_FOURCC_NV12;
199 subsample = SUBSAMPLE_YUV420;
200 chroma_format = VA_RT_FORMAT_YUV420;
203 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
206 /* Determine the HW surface format, bound to VA config needs */
207 if ((decode_state->base.chroma_formats & chroma_format) == chroma_format)
212 case VA_FOURCC_Y800: // Implement with an NV12 surface
213 if (decode_state->base.chroma_formats & VA_RT_FORMAT_YUV420) {
214 hw_fourcc = VA_FOURCC_NV12;
215 subsample = SUBSAMPLE_YUV420;
221 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
223 /* (Re-)allocate the underlying surface buffer store, if necessary */
224 if (!obj_surface->bo || obj_surface->fourcc != hw_fourcc) {
225 struct i965_driver_data * const i965 = i965_driver_data(ctx);
227 i965_destroy_surface_storage(obj_surface);
228 va_status = i965_check_alloc_surface_bo(ctx, obj_surface,
229 i965->codec_info->has_tiled_surface, hw_fourcc, subsample);
230 if (va_status != VA_STATUS_SUCCESS)
234 /* Fake chroma components if grayscale is implemented on top of NV12 */
235 if (fourcc == VA_FOURCC_Y800 && hw_fourcc == VA_FOURCC_NV12) {
236 const uint32_t uv_offset = obj_surface->width * obj_surface->height;
237 const uint32_t uv_size = obj_surface->width * obj_surface->height / 2;
239 drm_intel_gem_bo_map_gtt(obj_surface->bo);
240 memset(obj_surface->bo->virtual + uv_offset, 0x80, uv_size);
241 drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
243 return VA_STATUS_SUCCESS;
246 /* Generate flat scaling matrices for H.264 decoding */
248 avc_gen_default_iq_matrix(VAIQMatrixBufferH264 *iq_matrix)
251 memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
254 memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
257 /* Returns a unique picture ID that represents the supplied VA surface object */
259 avc_get_picture_id(struct object_surface *obj_surface)
263 /* This highly depends on how the internal VA objects are organized.
265 Theory of operations:
266 The VA objects are maintained in heaps so that any released VA
267 surface will become free again for future allocation. This means
268 that holes in there are filled in for subsequent allocations.
269 So, this ultimately means that we could just use the Heap ID of
270 the VA surface as the resulting picture ID (16 bits) */
271 pic_id = 1 + (obj_surface->base.id & OBJECT_HEAP_ID_MASK);
272 return (pic_id <= 0xffff) ? pic_id : -1;
275 /* Finds the VA/H264 picture associated with the specified VA surface id */
277 avc_find_picture(VASurfaceID id, VAPictureH264 *pic_list, int pic_list_count)
281 if (id != VA_INVALID_ID) {
282 for (i = 0; i < pic_list_count; i++) {
283 VAPictureH264 * const va_pic = &pic_list[i];
284 if (va_pic->picture_id == id &&
285 !(va_pic->flags & VA_PICTURE_H264_INVALID))
292 /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
293 /* XXX: slice_data_bit_offset does not account for EPB */
295 avc_get_first_mb_bit_offset(
296 dri_bo *slice_data_bo,
297 VASliceParameterBufferH264 *slice_param,
298 unsigned int mode_flag
301 unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
303 if (mode_flag == ENTROPY_CABAC)
304 slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
305 return slice_data_bit_offset;
308 /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
309 /* XXX: slice_data_bit_offset does not account for EPB */
311 avc_get_first_mb_bit_offset_with_epb(
312 dri_bo *slice_data_bo,
313 VASliceParameterBufferH264 *slice_param,
314 unsigned int mode_flag
317 unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
318 unsigned int out_slice_data_bit_offset;
319 unsigned int i, j, n, buf_size, data_size, header_size;
323 header_size = slice_param->slice_data_bit_offset / 8;
324 data_size = slice_param->slice_data_size - slice_param->slice_data_offset;
325 buf_size = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
327 if (buf_size > data_size)
328 buf_size = data_size;
330 buf = alloca(buf_size);
331 ret = dri_bo_get_subdata(
332 slice_data_bo, slice_param->slice_data_offset,
337 for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
338 if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
342 out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
344 if (mode_flag == ENTROPY_CABAC)
345 out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
346 return out_slice_data_bit_offset;
349 static inline uint8_t
350 get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
352 /* The H.264 standard, and the VA-API specification, allows for at
353 least 3 states for a picture: "used for short-term reference",
354 "used for long-term reference", or considered as not used for
357 The latter is used in the MVC inter prediction and inter-view
358 prediction process (H.8.4). This has an incidence on the
359 colZeroFlag variable, as defined in 8.4.1.2.
361 Since it is not possible to directly program that flag, let's
362 make the hardware derive this value by assimilating "considered
363 as not used for reference" to a "not used for short-term
364 reference", and subsequently making it "used for long-term
365 reference" to fit the definition of Bit6 here */
366 const unsigned int ref_flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE |
367 VA_PICTURE_H264_LONG_TERM_REFERENCE;
368 const unsigned int is_long_term =
369 ((va_pic->flags & ref_flags) != VA_PICTURE_H264_SHORT_TERM_REFERENCE);
370 const unsigned int is_top_field =
371 !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
372 const unsigned int is_bottom_field =
373 !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
375 return ((is_long_term << 6) |
376 ((is_top_field ^ is_bottom_field ^ 1) << 5) |
377 (frame_store_id << 1) |
378 ((is_top_field ^ 1) & is_bottom_field));
381 /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
383 gen5_fill_avc_ref_idx_state(
385 const VAPictureH264 ref_list[32],
386 unsigned int ref_list_count,
387 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
392 for (i = 0; i < ref_list_count; i++) {
393 const VAPictureH264 * const va_pic = &ref_list[i];
395 if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
396 va_pic->picture_id == VA_INVALID_ID) {
401 for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
402 if (frame_store[j].surface_id == va_pic->picture_id)
406 if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
407 const GenFrameStore * const fs = &frame_store[j];
408 assert(fs->frame_store_id == j); // Current architecture/assumption
409 state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
412 WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
413 state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
421 /* Emit Reference List Entries (Gen6+: SNB, IVB) */
423 gen6_send_avc_ref_idx_state_1(
424 struct intel_batchbuffer *batch,
426 const VAPictureH264 *ref_list,
427 unsigned int ref_list_count,
428 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
431 uint8_t ref_idx_state[32];
433 BEGIN_BCS_BATCH(batch, 10);
434 OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
435 OUT_BCS_BATCH(batch, list);
436 gen5_fill_avc_ref_idx_state(
438 ref_list, ref_list_count,
441 intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
442 ADVANCE_BCS_BATCH(batch);
446 gen6_send_avc_ref_idx_state(
447 struct intel_batchbuffer *batch,
448 const VASliceParameterBufferH264 *slice_param,
449 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
452 if (slice_param->slice_type == SLICE_TYPE_I ||
453 slice_param->slice_type == SLICE_TYPE_SI)
457 gen6_send_avc_ref_idx_state_1(
459 slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
463 if (slice_param->slice_type != SLICE_TYPE_B)
467 gen6_send_avc_ref_idx_state_1(
469 slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
475 intel_update_avc_frame_store_index(
476 VADriverContextP ctx,
477 struct decode_state *decode_state,
478 VAPictureParameterBufferH264 *pic_param,
479 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
482 GenFrameStore *free_refs[MAX_GEN_REFERENCE_FRAMES];
483 int i, j, n, num_free_refs;
485 /* Remove obsolete entries from the internal DPB */
486 for (i = 0, n = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
487 GenFrameStore * const fs = &frame_store[i];
488 if (fs->surface_id == VA_INVALID_ID || !fs->obj_surface) {
493 // Find whether the current entry is still a valid reference frame
494 for (j = 0; j < ARRAY_ELEMS(decode_state->reference_objects); j++) {
495 struct object_surface * const obj_surface =
496 decode_state->reference_objects[j];
497 if (obj_surface && obj_surface == fs->obj_surface)
502 if (j == ARRAY_ELEMS(decode_state->reference_objects)) {
503 fs->surface_id = VA_INVALID_ID;
504 fs->obj_surface = NULL;
505 fs->frame_store_id = -1;
511 /* Append the new reference frames */
512 for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
513 struct object_surface * const obj_surface =
514 decode_state->reference_objects[i];
518 // Find whether the current frame is not already in our frame store
519 for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
520 GenFrameStore * const fs = &frame_store[j];
521 if (fs->obj_surface == obj_surface)
526 if (j == MAX_GEN_REFERENCE_FRAMES) {
527 if (n < num_free_refs) {
528 GenFrameStore * const fs = free_refs[n++];
529 fs->surface_id = obj_surface->base.id;
530 fs->obj_surface = obj_surface;
531 fs->frame_store_id = fs - frame_store;
534 WARN_ONCE("No free slot found for DPB reference list!!!\n");
540 gen75_update_avc_frame_store_index(
541 VADriverContextP ctx,
542 struct decode_state *decode_state,
543 VAPictureParameterBufferH264 *pic_param,
544 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
549 /* Construct the Frame Store array, in compact form. i.e. empty or
550 invalid entries are discarded. */
551 for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
552 struct object_surface * const obj_surface =
553 decode_state->reference_objects[i];
557 GenFrameStore * const fs = &frame_store[n];
558 fs->surface_id = obj_surface->base.id;
559 fs->obj_surface = obj_surface;
560 fs->frame_store_id = n++;
563 /* Any remaining entry is marked as invalid */
564 for (; n < MAX_GEN_REFERENCE_FRAMES; n++) {
565 GenFrameStore * const fs = &frame_store[n];
566 fs->surface_id = VA_INVALID_ID;
567 fs->obj_surface = NULL;
568 fs->frame_store_id = -1;
573 gen75_fill_avc_picid_list(
574 uint16_t pic_ids[16],
575 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
580 /* Fill in with known picture IDs. The Frame Store array is in
581 compact form, i.e. empty entries are only to be found at the
582 end of the array: there are no holes in the set of active
584 for (i = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
585 GenFrameStore * const fs = &frame_store[i];
586 if (!fs->obj_surface)
588 pic_id = avc_get_picture_id(fs->obj_surface);
594 /* When an element of the list is not relevant the value of the
595 picture ID shall be set to 0 */
596 for (; i < MAX_GEN_REFERENCE_FRAMES; i++)
602 gen75_send_avc_picid_state(
603 struct intel_batchbuffer *batch,
604 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
607 uint16_t pic_ids[16];
609 if (!gen75_fill_avc_picid_list(pic_ids, frame_store))
612 BEGIN_BCS_BATCH(batch, 10);
613 OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
614 OUT_BCS_BATCH(batch, 0); // enable Picture ID Remapping
615 intel_batchbuffer_data(batch, pic_ids, sizeof(pic_ids));
616 ADVANCE_BCS_BATCH(batch);
621 intel_update_vc1_frame_store_index(VADriverContextP ctx,
622 struct decode_state *decode_state,
623 VAPictureParameterBufferVC1 *pic_param,
624 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
626 struct object_surface *obj_surface;
629 obj_surface = decode_state->reference_objects[0];
631 if (pic_param->forward_reference_picture == VA_INVALID_ID ||
634 frame_store[0].surface_id = VA_INVALID_ID;
635 frame_store[0].obj_surface = NULL;
637 frame_store[0].surface_id = pic_param->forward_reference_picture;
638 frame_store[0].obj_surface = obj_surface;
641 obj_surface = decode_state->reference_objects[1];
643 if (pic_param->backward_reference_picture == VA_INVALID_ID ||
646 frame_store[1].surface_id = frame_store[0].surface_id;
647 frame_store[1].obj_surface = frame_store[0].obj_surface;
649 frame_store[1].surface_id = pic_param->backward_reference_picture;
650 frame_store[1].obj_surface = obj_surface;
652 for (i = 2; i < MAX_GEN_REFERENCE_FRAMES; i++) {
653 frame_store[i].surface_id = frame_store[i % 2].surface_id;
654 frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
660 intel_update_vp8_frame_store_index(VADriverContextP ctx,
661 struct decode_state *decode_state,
662 VAPictureParameterBufferVP8 *pic_param,
663 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
665 struct object_surface *obj_surface;
668 obj_surface = decode_state->reference_objects[0];
670 if (pic_param->last_ref_frame == VA_INVALID_ID ||
673 frame_store[0].surface_id = VA_INVALID_ID;
674 frame_store[0].obj_surface = NULL;
676 frame_store[0].surface_id = pic_param->last_ref_frame;
677 frame_store[0].obj_surface = obj_surface;
680 obj_surface = decode_state->reference_objects[1];
682 if (pic_param->golden_ref_frame == VA_INVALID_ID ||
685 frame_store[1].surface_id = frame_store[0].surface_id;
686 frame_store[1].obj_surface = frame_store[0].obj_surface;
688 frame_store[1].surface_id = pic_param->golden_ref_frame;
689 frame_store[1].obj_surface = obj_surface;
692 obj_surface = decode_state->reference_objects[2];
694 if (pic_param->alt_ref_frame == VA_INVALID_ID ||
697 frame_store[2].surface_id = frame_store[0].surface_id;
698 frame_store[2].obj_surface = frame_store[0].obj_surface;
700 frame_store[2].surface_id = pic_param->alt_ref_frame;
701 frame_store[2].obj_surface = obj_surface;
704 for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
705 frame_store[i].surface_id = frame_store[i % 2].surface_id;
706 frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
712 intel_decoder_check_avc_parameter(VADriverContextP ctx,
713 VAProfile h264_profile,
714 struct decode_state *decode_state)
716 struct i965_driver_data *i965 = i965_driver_data(ctx);
717 VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
719 struct object_surface *obj_surface;
722 assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
723 assert(pic_param->CurrPic.picture_id != VA_INVALID_SURFACE);
725 if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID ||
726 pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
729 assert(pic_param->CurrPic.picture_id == decode_state->current_render_target);
731 if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
734 if ((h264_profile != VAProfileH264Baseline)) {
735 if (pic_param->num_slice_groups_minus1 ||
736 pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
737 WARN_ONCE("Unsupported the FMO/ASO constraints!!!\n");
742 /* Fill in the reference objects array with the actual VA surface
743 objects with 1:1 correspondance with any entry in ReferenceFrames[],
744 i.e. including "holes" for invalid entries, that are expanded
745 to NULL in the reference_objects[] array */
746 for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
747 const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
750 if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
751 va_pic->picture_id != VA_INVALID_ID) {
752 obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
754 return VA_STATUS_ERROR_INVALID_SURFACE;
757 * Sometimes a dummy frame comes from the upper layer
758 * library, call i965_check_alloc_surface_bo() to make
759 * sure the store buffer is allocated for this reference
762 va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
764 if (va_status != VA_STATUS_SUCCESS)
767 decode_state->reference_objects[i] = obj_surface;
769 return VA_STATUS_SUCCESS;
772 return VA_STATUS_ERROR_INVALID_PARAMETER;
776 intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
777 struct decode_state *decode_state)
779 struct i965_driver_data *i965 = i965_driver_data(ctx);
780 VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
781 struct object_surface *obj_surface;
784 if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
785 } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
786 obj_surface = SURFACE(pic_param->forward_reference_picture);
788 if (!obj_surface || !obj_surface->bo)
789 decode_state->reference_objects[i++] = NULL;
791 decode_state->reference_objects[i++] = obj_surface;
792 } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
793 obj_surface = SURFACE(pic_param->forward_reference_picture);
795 if (!obj_surface || !obj_surface->bo)
796 decode_state->reference_objects[i++] = NULL;
798 decode_state->reference_objects[i++] = obj_surface;
800 obj_surface = SURFACE(pic_param->backward_reference_picture);
802 if (!obj_surface || !obj_surface->bo)
803 decode_state->reference_objects[i++] = NULL;
805 decode_state->reference_objects[i++] = obj_surface;
810 decode_state->reference_objects[i] = NULL;
812 return VA_STATUS_SUCCESS;
815 return VA_STATUS_ERROR_INVALID_PARAMETER;
819 intel_decoder_check_vc1_parameter(VADriverContextP ctx,
820 struct decode_state *decode_state)
822 struct i965_driver_data *i965 = i965_driver_data(ctx);
823 VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
824 struct object_surface *obj_surface;
827 if (pic_param->sequence_fields.bits.interlace == 1 &&
828 pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
829 return VA_STATUS_ERROR_DECODING_ERROR;
832 if (pic_param->picture_fields.bits.picture_type == 0 ||
833 pic_param->picture_fields.bits.picture_type == 3) {
834 } else if (pic_param->picture_fields.bits.picture_type == 1 ||
835 pic_param->picture_fields.bits.picture_type == 4) {
836 obj_surface = SURFACE(pic_param->forward_reference_picture);
838 if (!obj_surface || !obj_surface->bo)
839 decode_state->reference_objects[i++] = NULL;
841 decode_state->reference_objects[i++] = obj_surface;
842 } else if (pic_param->picture_fields.bits.picture_type == 2) {
843 obj_surface = SURFACE(pic_param->forward_reference_picture);
845 if (!obj_surface || !obj_surface->bo)
846 decode_state->reference_objects[i++] = NULL;
848 decode_state->reference_objects[i++] = obj_surface;
850 obj_surface = SURFACE(pic_param->backward_reference_picture);
852 if (!obj_surface || !obj_surface->bo)
853 decode_state->reference_objects[i++] = NULL;
855 decode_state->reference_objects[i++] = obj_surface;
860 decode_state->reference_objects[i] = NULL;
862 return VA_STATUS_SUCCESS;
865 return VA_STATUS_ERROR_INVALID_PARAMETER;
869 intel_decoder_check_vp8_parameter(VADriverContextP ctx,
870 struct decode_state *decode_state)
872 struct i965_driver_data *i965 = i965_driver_data(ctx);
873 VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
874 struct object_surface *obj_surface;
877 if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
878 obj_surface = SURFACE(pic_param->last_ref_frame);
880 if (obj_surface && obj_surface->bo)
881 decode_state->reference_objects[i++] = obj_surface;
883 decode_state->reference_objects[i++] = NULL;
886 if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
887 obj_surface = SURFACE(pic_param->golden_ref_frame);
889 if (obj_surface && obj_surface->bo)
890 decode_state->reference_objects[i++] = obj_surface;
892 decode_state->reference_objects[i++] = NULL;
895 if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
896 obj_surface = SURFACE(pic_param->alt_ref_frame);
898 if (obj_surface && obj_surface->bo)
899 decode_state->reference_objects[i++] = obj_surface;
901 decode_state->reference_objects[i++] = NULL;
905 decode_state->reference_objects[i] = NULL;
907 return VA_STATUS_SUCCESS;
911 intel_decoder_sanity_check_input(VADriverContextP ctx,
913 struct decode_state *decode_state)
915 struct i965_driver_data *i965 = i965_driver_data(ctx);
916 struct object_surface *obj_surface;
917 VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
919 if (decode_state->current_render_target == VA_INVALID_SURFACE)
922 obj_surface = SURFACE(decode_state->current_render_target);
927 decode_state->render_object = obj_surface;
930 case VAProfileMPEG2Simple:
931 case VAProfileMPEG2Main:
932 vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
935 case VAProfileH264ConstrainedBaseline:
936 case VAProfileH264Main:
937 case VAProfileH264High:
938 case VAProfileH264StereoHigh:
939 case VAProfileH264MultiviewHigh:
940 vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
943 case VAProfileVC1Simple:
944 case VAProfileVC1Main:
945 case VAProfileVC1Advanced:
946 vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
949 case VAProfileJPEGBaseline:
950 vaStatus = VA_STATUS_SUCCESS;
953 case VAProfileVP8Version0_3:
954 vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
958 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
967 * Return the next slice paramter
970 * slice_param: the current slice
971 * *group_idx & *element_idx the current slice position in slice groups
973 * Return the next slice parameter
974 * *group_idx & *element_idx the next slice position in slice groups,
975 * if the next slice is NULL, *group_idx & *element_idx will be ignored
977 VASliceParameterBufferMPEG2 *
978 intel_mpeg2_find_next_slice(struct decode_state *decode_state,
979 VAPictureParameterBufferMPEG2 *pic_param,
980 VASliceParameterBufferMPEG2 *slice_param,
984 VASliceParameterBufferMPEG2 *next_slice_param;
985 unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
986 int j = *group_idx, i = *element_idx + 1;
988 for (; j < decode_state->num_slice_params; j++) {
989 for (; i < decode_state->slice_params[j]->num_elements; i++) {
990 next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
992 if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
993 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
997 return next_slice_param;
1007 /* Ensure the segmentation buffer is large enough for the supplied
1008 number of MBs, or re-allocate it */
1010 intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
1011 unsigned int mb_width, unsigned int mb_height)
1013 struct i965_driver_data * const i965 = i965_driver_data(ctx);
1014 /* The segmentation map is a 64-byte aligned linear buffer, with
1015 each cache line holding only 8 bits for 4 continuous MBs */
1016 const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
1019 if (buf->bo && buf->bo->size >= buf_size)
1021 drm_intel_bo_unreference(buf->bo);
1025 buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
1027 buf->valid = buf->bo != NULL;