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 "intel_media.h"
30 #include "i965_drv_video.h"
31 #include "i965_decoder_utils.h"
32 #include "i965_defines.h"
34 /* Set reference surface if backing store exists */
37 struct i965_driver_data *i965,
38 GenFrameStore *ref_frame,
39 VASurfaceID va_surface,
40 struct object_surface *obj_surface
43 if (va_surface == VA_INVALID_ID)
46 if (!obj_surface || !obj_surface->bo)
49 ref_frame->surface_id = va_surface;
50 ref_frame->obj_surface = obj_surface;
54 /* Check wether codec layer incorrectly fills in slice_vertical_position */
56 mpeg2_wa_slice_vertical_position(
57 struct decode_state *decode_state,
58 VAPictureParameterBufferMPEG2 *pic_param
61 unsigned int i, j, mb_height, vpos, last_vpos = 0;
63 /* Assume progressive sequence if we got a progressive frame */
64 if (pic_param->picture_coding_extension.bits.progressive_frame)
67 /* Wait for a field coded picture */
68 if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_FRAME)
71 assert(decode_state && decode_state->slice_params);
73 mb_height = (pic_param->vertical_size + 31) / 32;
75 for (j = 0; j < decode_state->num_slice_params; j++) {
76 struct buffer_store * const buffer_store =
77 decode_state->slice_params[j];
79 for (i = 0; i < buffer_store->num_elements; i++) {
80 VASliceParameterBufferMPEG2 * const slice_param =
81 ((VASliceParameterBufferMPEG2 *)buffer_store->buffer) + i;
83 vpos = slice_param->slice_vertical_position;
84 if (vpos >= mb_height || vpos == last_vpos + 2) {
85 WARN_ONCE("codec layer incorrectly fills in MPEG-2 slice_vertical_position. Workaround applied\n");
94 /* Build MPEG-2 reference frames array */
96 mpeg2_set_reference_surfaces(
98 GenFrameStore ref_frames[MAX_GEN_REFERENCE_FRAMES],
99 struct decode_state *decode_state,
100 VAPictureParameterBufferMPEG2 *pic_param
103 struct i965_driver_data * const i965 = i965_driver_data(ctx);
104 VASurfaceID va_surface;
105 unsigned pic_structure, is_second_field, n = 0;
106 struct object_surface *obj_surface;
108 pic_structure = pic_param->picture_coding_extension.bits.picture_structure;
109 is_second_field = pic_structure != MPEG_FRAME &&
110 !pic_param->picture_coding_extension.bits.is_first_field;
112 ref_frames[0].surface_id = VA_INVALID_ID;
113 ref_frames[0].obj_surface = NULL;
115 /* Reference frames are indexed by frame store ID (0:top, 1:bottom) */
116 switch (pic_param->picture_coding_type) {
118 if (is_second_field && pic_structure == MPEG_BOTTOM_FIELD) {
119 va_surface = decode_state->current_render_target;
120 obj_surface = decode_state->render_object;
121 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
123 va_surface = pic_param->forward_reference_picture;
124 obj_surface = decode_state->reference_objects[0];
125 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
129 va_surface = pic_param->forward_reference_picture;
130 obj_surface = decode_state->reference_objects[0];
131 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
132 va_surface = pic_param->backward_reference_picture;
133 obj_surface = decode_state->reference_objects[1];
134 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
139 ref_frames[n].obj_surface = ref_frames[0].obj_surface;
140 ref_frames[n++].surface_id = ref_frames[0].surface_id;
143 if (pic_param->picture_coding_extension.bits.frame_pred_frame_dct)
146 ref_frames[2].surface_id = VA_INVALID_ID;
147 ref_frames[2].obj_surface = NULL;
149 /* Bottom field pictures used as reference */
150 switch (pic_param->picture_coding_type) {
152 if (is_second_field && pic_structure == MPEG_TOP_FIELD) {
153 va_surface = decode_state->current_render_target;
154 obj_surface = decode_state->render_object;
155 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
157 va_surface = pic_param->forward_reference_picture;
158 obj_surface = decode_state->reference_objects[0];
159 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
163 va_surface = pic_param->forward_reference_picture;
164 obj_surface = decode_state->reference_objects[0];
165 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
166 va_surface = pic_param->backward_reference_picture;
167 obj_surface = decode_state->reference_objects[1];
168 n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
173 ref_frames[n].obj_surface = ref_frames[2].obj_surface;
174 ref_frames[n++].surface_id = ref_frames[2].surface_id;
178 /* Ensure the supplied VA surface has valid storage for decoding the
181 avc_ensure_surface_bo(
182 VADriverContextP ctx,
183 struct decode_state *decode_state,
184 struct object_surface *obj_surface,
185 const VAPictureParameterBufferH264 *pic_param
189 uint32_t hw_fourcc, fourcc, subsample, chroma_format;
191 /* Validate chroma format */
192 switch (pic_param->seq_fields.bits.chroma_format_idc) {
194 fourcc = VA_FOURCC_Y800;
195 subsample = SUBSAMPLE_YUV400;
196 chroma_format = VA_RT_FORMAT_YUV400;
199 fourcc = VA_FOURCC_NV12;
200 subsample = SUBSAMPLE_YUV420;
201 chroma_format = VA_RT_FORMAT_YUV420;
204 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
207 /* Determine the HW surface format, bound to VA config needs */
208 if ((decode_state->base.chroma_formats & chroma_format) == chroma_format)
213 case VA_FOURCC_Y800: // Implement with an NV12 surface
214 if (decode_state->base.chroma_formats & VA_RT_FORMAT_YUV420) {
215 hw_fourcc = VA_FOURCC_NV12;
216 subsample = SUBSAMPLE_YUV420;
222 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
224 /* (Re-)allocate the underlying surface buffer store, if necessary */
225 if (!obj_surface->bo || obj_surface->fourcc != hw_fourcc) {
226 struct i965_driver_data * const i965 = i965_driver_data(ctx);
228 i965_destroy_surface_storage(obj_surface);
229 va_status = i965_check_alloc_surface_bo(ctx, obj_surface,
230 i965->codec_info->has_tiled_surface, hw_fourcc, subsample);
231 if (va_status != VA_STATUS_SUCCESS)
235 /* Fake chroma components if grayscale is implemented on top of NV12 */
236 if (fourcc == VA_FOURCC_Y800 && hw_fourcc == VA_FOURCC_NV12) {
237 const uint32_t uv_offset = obj_surface->width * obj_surface->height;
238 const uint32_t uv_size = obj_surface->width * obj_surface->height / 2;
240 drm_intel_gem_bo_map_gtt(obj_surface->bo);
241 memset(obj_surface->bo->virtual + uv_offset, 0x80, uv_size);
242 drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
244 return VA_STATUS_SUCCESS;
247 /* Generate flat scaling matrices for H.264 decoding */
249 avc_gen_default_iq_matrix(VAIQMatrixBufferH264 *iq_matrix)
252 memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
255 memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
258 /* Returns the POC of the supplied VA picture */
260 avc_get_picture_poc(const VAPictureH264 *va_pic)
262 int structure, field_poc[2];
264 structure = va_pic->flags &
265 (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
266 field_poc[0] = structure != VA_PICTURE_H264_BOTTOM_FIELD ?
267 va_pic->TopFieldOrderCnt : INT_MAX;
268 field_poc[1] = structure != VA_PICTURE_H264_TOP_FIELD ?
269 va_pic->BottomFieldOrderCnt : INT_MAX;
270 return MIN(field_poc[0], field_poc[1]);
273 /* Returns a unique picture ID that represents the supplied VA surface object */
275 avc_get_picture_id(struct object_surface *obj_surface)
279 /* This highly depends on how the internal VA objects are organized.
281 Theory of operations:
282 The VA objects are maintained in heaps so that any released VA
283 surface will become free again for future allocation. This means
284 that holes in there are filled in for subsequent allocations.
285 So, this ultimately means that we could just use the Heap ID of
286 the VA surface as the resulting picture ID (16 bits) */
287 pic_id = 1 + (obj_surface->base.id & OBJECT_HEAP_ID_MASK);
288 return (pic_id <= 0xffff) ? pic_id : -1;
291 /* Finds the VA/H264 picture associated with the specified VA surface id */
293 avc_find_picture(VASurfaceID id, VAPictureH264 *pic_list, int pic_list_count)
297 if (id != VA_INVALID_ID) {
298 for (i = 0; i < pic_list_count; i++) {
299 VAPictureH264 * const va_pic = &pic_list[i];
300 if (va_pic->picture_id == id &&
301 !(va_pic->flags & VA_PICTURE_H264_INVALID))
308 /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
309 /* XXX: slice_data_bit_offset does not account for EPB */
311 avc_get_first_mb_bit_offset(
312 dri_bo *slice_data_bo,
313 VASliceParameterBufferH264 *slice_param,
314 unsigned int mode_flag
317 unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
319 if (mode_flag == ENTROPY_CABAC)
320 slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
321 return slice_data_bit_offset;
324 /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
325 /* XXX: slice_data_bit_offset does not account for EPB */
327 avc_get_first_mb_bit_offset_with_epb(
328 dri_bo *slice_data_bo,
329 VASliceParameterBufferH264 *slice_param,
330 unsigned int mode_flag
333 unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
334 unsigned int out_slice_data_bit_offset;
335 unsigned int i, j, n, buf_size, data_size, header_size;
339 header_size = slice_param->slice_data_bit_offset / 8;
340 data_size = slice_param->slice_data_size - slice_param->slice_data_offset;
341 buf_size = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
343 if (buf_size > data_size)
344 buf_size = data_size;
346 buf = alloca(buf_size);
347 ret = dri_bo_get_subdata(
348 slice_data_bo, slice_param->slice_data_offset,
353 for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
354 if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
358 out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
360 if (mode_flag == ENTROPY_CABAC)
361 out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
362 return out_slice_data_bit_offset;
365 static inline uint8_t
366 get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
368 /* The H.264 standard, and the VA-API specification, allows for at
369 least 3 states for a picture: "used for short-term reference",
370 "used for long-term reference", or considered as not used for
373 The latter is used in the MVC inter prediction and inter-view
374 prediction process (H.8.4). This has an incidence on the
375 colZeroFlag variable, as defined in 8.4.1.2.
377 Since it is not possible to directly program that flag, let's
378 make the hardware derive this value by assimilating "considered
379 as not used for reference" to a "not used for short-term
380 reference", and subsequently making it "used for long-term
381 reference" to fit the definition of Bit6 here */
382 const unsigned int ref_flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE |
383 VA_PICTURE_H264_LONG_TERM_REFERENCE;
384 const unsigned int is_long_term =
385 ((va_pic->flags & ref_flags) != VA_PICTURE_H264_SHORT_TERM_REFERENCE);
386 const unsigned int is_top_field =
387 !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
388 const unsigned int is_bottom_field =
389 !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
391 return ((is_long_term << 6) |
392 ((is_top_field ^ is_bottom_field ^ 1) << 5) |
393 (frame_store_id << 1) |
394 ((is_top_field ^ 1) & is_bottom_field));
397 /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
399 gen5_fill_avc_ref_idx_state(
401 const VAPictureH264 ref_list[32],
402 unsigned int ref_list_count,
403 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
408 for (i = 0; i < ref_list_count; i++) {
409 const VAPictureH264 * const va_pic = &ref_list[i];
411 if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
412 va_pic->picture_id == VA_INVALID_ID) {
417 for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
418 if (frame_store[j].surface_id == va_pic->picture_id)
422 if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
423 const GenFrameStore * const fs = &frame_store[j];
424 assert(fs->frame_store_id == j); // Current architecture/assumption
425 state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
428 WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
429 state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
437 /* Emit Reference List Entries (Gen6+: SNB, IVB) */
439 gen6_send_avc_ref_idx_state_1(
440 struct intel_batchbuffer *batch,
442 const VAPictureH264 *ref_list,
443 unsigned int ref_list_count,
444 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
447 uint8_t ref_idx_state[32];
449 BEGIN_BCS_BATCH(batch, 10);
450 OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
451 OUT_BCS_BATCH(batch, list);
452 gen5_fill_avc_ref_idx_state(
454 ref_list, ref_list_count,
457 intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
458 ADVANCE_BCS_BATCH(batch);
462 gen6_send_avc_ref_idx_state(
463 struct intel_batchbuffer *batch,
464 const VASliceParameterBufferH264 *slice_param,
465 const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
468 if (slice_param->slice_type == SLICE_TYPE_I ||
469 slice_param->slice_type == SLICE_TYPE_SI)
473 gen6_send_avc_ref_idx_state_1(
475 slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
479 if (slice_param->slice_type != SLICE_TYPE_B)
483 gen6_send_avc_ref_idx_state_1(
485 slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
491 gen6_mfd_avc_phantom_slice_state(VADriverContextP ctx,
492 VAPictureParameterBufferH264 *pic_param,
493 VASliceParameterBufferH264 *next_slice_param,
494 struct intel_batchbuffer *batch)
496 int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
497 int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1; /* frame height */
498 int slice_hor_pos, slice_ver_pos, slice_start_mb_num, next_slice_hor_pos, next_slice_ver_pos;
499 int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
500 pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
502 if (next_slice_param) {
503 int first_mb_in_next_slice;
507 slice_start_mb_num = 0;
508 first_mb_in_next_slice = next_slice_param->first_mb_in_slice << mbaff_picture;
509 next_slice_hor_pos = first_mb_in_next_slice % width_in_mbs;
510 next_slice_ver_pos = first_mb_in_next_slice / width_in_mbs;
513 slice_ver_pos = height_in_mbs;
514 slice_start_mb_num = width_in_mbs * height_in_mbs / (1 + !!pic_param->pic_fields.bits.field_pic_flag);
515 next_slice_hor_pos = 0;
516 next_slice_ver_pos = 0;
519 BEGIN_BCS_BATCH(batch, 11);
520 OUT_BCS_BATCH(batch, MFX_AVC_SLICE_STATE | (11 - 2));
521 OUT_BCS_BATCH(batch, 0);
522 OUT_BCS_BATCH(batch, 0);
523 OUT_BCS_BATCH(batch, 0);
525 slice_ver_pos << 24 |
526 slice_hor_pos << 16 |
527 slice_start_mb_num << 0);
529 next_slice_ver_pos << 16 |
530 next_slice_hor_pos << 0);
531 OUT_BCS_BATCH(batch, 0);
532 OUT_BCS_BATCH(batch, 0);
533 OUT_BCS_BATCH(batch, 0);
534 OUT_BCS_BATCH(batch, 0);
535 OUT_BCS_BATCH(batch, 0);
536 ADVANCE_BCS_BATCH(batch);
540 gen6_mfd_avc_phantom_slice_bsd_object(VADriverContextP ctx,
541 VAPictureParameterBufferH264 *pic_param,
542 struct intel_batchbuffer *batch)
545 BEGIN_BCS_BATCH(batch, 6);
546 OUT_BCS_BATCH(batch, MFD_AVC_BSD_OBJECT | (6 - 2));
547 OUT_BCS_BATCH(batch, 0);
548 OUT_BCS_BATCH(batch, 0);
549 OUT_BCS_BATCH(batch, 0);
550 OUT_BCS_BATCH(batch, 0);
551 OUT_BCS_BATCH(batch, 0);
552 ADVANCE_BCS_BATCH(batch);
556 gen6_mfd_avc_phantom_slice(VADriverContextP ctx,
557 VAPictureParameterBufferH264 *pic_param,
558 VASliceParameterBufferH264 *next_slice_param,
559 struct intel_batchbuffer *batch)
561 gen6_mfd_avc_phantom_slice_state(ctx, pic_param, next_slice_param, batch);
562 gen6_mfd_avc_phantom_slice_bsd_object(ctx, pic_param, batch);
565 /* Comparison function for sorting out the array of free frame store entries */
567 compare_avc_ref_store_func(const void *p1, const void *p2)
569 const GenFrameStore * const fs1 = *((GenFrameStore **)p1);
570 const GenFrameStore * const fs2 = *((GenFrameStore **)p2);
572 return fs1->ref_age - fs2->ref_age;
576 intel_update_avc_frame_store_index(
577 VADriverContextP ctx,
578 struct decode_state *decode_state,
579 VAPictureParameterBufferH264 *pic_param,
580 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES],
581 GenFrameStoreContext *fs_ctx
584 GenFrameStore *free_refs[MAX_GEN_REFERENCE_FRAMES];
585 uint32_t used_refs = 0, add_refs = 0;
587 int i, n, num_free_refs;
589 /* Detect changes of access unit */
590 const int poc = avc_get_picture_poc(&pic_param->CurrPic);
591 if (fs_ctx->age == 0 || fs_ctx->prev_poc != poc)
593 fs_ctx->prev_poc = poc;
596 /* Tag entries that are still available in our Frame Store */
597 for (i = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
598 struct object_surface * const obj_surface =
599 decode_state->reference_objects[i];
603 GenAvcSurface * const avc_surface = obj_surface->private_data;
606 if (avc_surface->frame_store_id >= 0) {
607 GenFrameStore * const fs =
608 &frame_store[avc_surface->frame_store_id];
609 if (fs->surface_id == obj_surface->base.id) {
610 fs->obj_surface = obj_surface;
612 used_refs |= 1 << fs->frame_store_id;
619 /* Build and sort out the list of retired candidates. The resulting
620 list is ordered by increasing age when they were last used */
621 for (i = 0, n = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
622 if (!(used_refs & (1 << i))) {
623 GenFrameStore * const fs = &frame_store[i];
624 fs->obj_surface = NULL;
629 qsort(&free_refs[0], n, sizeof(free_refs[0]), compare_avc_ref_store_func);
631 /* Append the new reference frames */
632 for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
633 struct object_surface * const obj_surface =
634 decode_state->reference_objects[i];
635 if (!obj_surface || !(add_refs & (1 << i)))
638 GenAvcSurface * const avc_surface = obj_surface->private_data;
641 if (n < num_free_refs) {
642 GenFrameStore * const fs = free_refs[n++];
643 fs->surface_id = obj_surface->base.id;
644 fs->obj_surface = obj_surface;
645 fs->frame_store_id = fs - frame_store;
647 avc_surface->frame_store_id = fs->frame_store_id;
650 WARN_ONCE("No free slot found for DPB reference list!!!\n");
655 gen75_update_avc_frame_store_index(
656 VADriverContextP ctx,
657 struct decode_state *decode_state,
658 VAPictureParameterBufferH264 *pic_param,
659 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
664 /* Construct the Frame Store array, in compact form. i.e. empty or
665 invalid entries are discarded. */
666 for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
667 struct object_surface * const obj_surface =
668 decode_state->reference_objects[i];
672 GenFrameStore * const fs = &frame_store[n];
673 fs->surface_id = obj_surface->base.id;
674 fs->obj_surface = obj_surface;
675 fs->frame_store_id = n++;
678 /* Any remaining entry is marked as invalid */
679 for (; n < MAX_GEN_REFERENCE_FRAMES; n++) {
680 GenFrameStore * const fs = &frame_store[n];
681 fs->surface_id = VA_INVALID_ID;
682 fs->obj_surface = NULL;
683 fs->frame_store_id = -1;
688 gen75_fill_avc_picid_list(
689 uint16_t pic_ids[16],
690 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
695 /* Fill in with known picture IDs. The Frame Store array is in
696 compact form, i.e. empty entries are only to be found at the
697 end of the array: there are no holes in the set of active
699 for (i = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
700 GenFrameStore * const fs = &frame_store[i];
701 if (!fs->obj_surface)
703 pic_id = avc_get_picture_id(fs->obj_surface);
709 /* When an element of the list is not relevant the value of the
710 picture ID shall be set to 0 */
711 for (; i < MAX_GEN_REFERENCE_FRAMES; i++)
717 gen75_send_avc_picid_state(
718 struct intel_batchbuffer *batch,
719 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
722 uint16_t pic_ids[16];
724 if (!gen75_fill_avc_picid_list(pic_ids, frame_store))
727 BEGIN_BCS_BATCH(batch, 10);
728 OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
729 OUT_BCS_BATCH(batch, 0); // enable Picture ID Remapping
730 intel_batchbuffer_data(batch, pic_ids, sizeof(pic_ids));
731 ADVANCE_BCS_BATCH(batch);
736 intel_update_vc1_frame_store_index(VADriverContextP ctx,
737 struct decode_state *decode_state,
738 VAPictureParameterBufferVC1 *pic_param,
739 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
741 struct object_surface *obj_surface;
744 obj_surface = decode_state->reference_objects[0];
746 if (pic_param->forward_reference_picture == VA_INVALID_ID ||
749 frame_store[0].surface_id = VA_INVALID_ID;
750 frame_store[0].obj_surface = NULL;
752 frame_store[0].surface_id = pic_param->forward_reference_picture;
753 frame_store[0].obj_surface = obj_surface;
756 obj_surface = decode_state->reference_objects[1];
758 if (pic_param->backward_reference_picture == VA_INVALID_ID ||
761 frame_store[1].surface_id = frame_store[0].surface_id;
762 frame_store[1].obj_surface = frame_store[0].obj_surface;
764 frame_store[1].surface_id = pic_param->backward_reference_picture;
765 frame_store[1].obj_surface = obj_surface;
767 for (i = 2; i < MAX_GEN_REFERENCE_FRAMES; i++) {
768 frame_store[i].surface_id = frame_store[i % 2].surface_id;
769 frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
775 intel_update_vp8_frame_store_index(VADriverContextP ctx,
776 struct decode_state *decode_state,
777 VAPictureParameterBufferVP8 *pic_param,
778 GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
780 struct object_surface *obj_surface;
783 obj_surface = decode_state->reference_objects[0];
785 if (pic_param->last_ref_frame == VA_INVALID_ID ||
788 frame_store[0].surface_id = VA_INVALID_ID;
789 frame_store[0].obj_surface = NULL;
791 frame_store[0].surface_id = pic_param->last_ref_frame;
792 frame_store[0].obj_surface = obj_surface;
795 obj_surface = decode_state->reference_objects[1];
797 if (pic_param->golden_ref_frame == VA_INVALID_ID ||
800 frame_store[1].surface_id = frame_store[0].surface_id;
801 frame_store[1].obj_surface = frame_store[0].obj_surface;
803 frame_store[1].surface_id = pic_param->golden_ref_frame;
804 frame_store[1].obj_surface = obj_surface;
807 obj_surface = decode_state->reference_objects[2];
809 if (pic_param->alt_ref_frame == VA_INVALID_ID ||
812 frame_store[2].surface_id = frame_store[0].surface_id;
813 frame_store[2].obj_surface = frame_store[0].obj_surface;
815 frame_store[2].surface_id = pic_param->alt_ref_frame;
816 frame_store[2].obj_surface = obj_surface;
819 for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
820 frame_store[i].surface_id = frame_store[i % 2].surface_id;
821 frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
827 intel_decoder_check_avc_parameter(VADriverContextP ctx,
828 VAProfile h264_profile,
829 struct decode_state *decode_state)
831 struct i965_driver_data *i965 = i965_driver_data(ctx);
832 VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
834 struct object_surface *obj_surface;
836 VASliceParameterBufferH264 *slice_param, *next_slice_param, *next_slice_group_param;
839 assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
840 assert(pic_param->CurrPic.picture_id != VA_INVALID_SURFACE);
842 if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID ||
843 pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
846 assert(pic_param->CurrPic.picture_id == decode_state->current_render_target);
848 if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
851 if ((h264_profile != VAProfileH264Baseline)) {
852 if (pic_param->num_slice_groups_minus1 ||
853 pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
854 WARN_ONCE("Unsupported the FMO/ASO constraints!!!\n");
859 /* Fill in the reference objects array with the actual VA surface
860 objects with 1:1 correspondance with any entry in ReferenceFrames[],
861 i.e. including "holes" for invalid entries, that are expanded
862 to NULL in the reference_objects[] array */
863 for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
864 const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
867 if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
868 va_pic->picture_id != VA_INVALID_ID) {
869 obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
871 return VA_STATUS_ERROR_INVALID_SURFACE;
874 * Sometimes a dummy frame comes from the upper layer
875 * library, call i965_check_alloc_surface_bo() to make
876 * sure the store buffer is allocated for this reference
879 va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
881 if (va_status != VA_STATUS_SUCCESS)
884 decode_state->reference_objects[i] = obj_surface;
887 for (j = 0; j < decode_state->num_slice_params; j++) {
888 assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
889 slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
891 if (j == decode_state->num_slice_params - 1)
892 next_slice_group_param = NULL;
894 next_slice_group_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j + 1]->buffer;
896 for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
898 if (i < decode_state->slice_params[j]->num_elements - 1)
899 next_slice_param = slice_param + 1;
901 next_slice_param = next_slice_group_param;
903 if (next_slice_param != NULL) {
904 /* If the mb position of next_slice is less than or equal to the current slice,
905 * discard the current frame.
907 if (next_slice_param->first_mb_in_slice <= slice_param->first_mb_in_slice) {
908 next_slice_param = NULL;
909 WARN_ONCE("!!!incorrect slice_param. The first_mb_in_slice of next_slice is less"
910 " than or equal to that in current slice\n");
917 return VA_STATUS_SUCCESS;
920 return VA_STATUS_ERROR_INVALID_PARAMETER;
924 intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
925 struct decode_state *decode_state)
927 struct i965_driver_data *i965 = i965_driver_data(ctx);
928 VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
929 struct object_surface *obj_surface;
932 if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
933 } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
934 obj_surface = SURFACE(pic_param->forward_reference_picture);
936 if (!obj_surface || !obj_surface->bo)
937 decode_state->reference_objects[i++] = NULL;
939 decode_state->reference_objects[i++] = obj_surface;
940 } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
941 obj_surface = SURFACE(pic_param->forward_reference_picture);
943 if (!obj_surface || !obj_surface->bo)
944 decode_state->reference_objects[i++] = NULL;
946 decode_state->reference_objects[i++] = obj_surface;
948 obj_surface = SURFACE(pic_param->backward_reference_picture);
950 if (!obj_surface || !obj_surface->bo)
951 decode_state->reference_objects[i++] = NULL;
953 decode_state->reference_objects[i++] = obj_surface;
958 decode_state->reference_objects[i] = NULL;
960 return VA_STATUS_SUCCESS;
963 return VA_STATUS_ERROR_INVALID_PARAMETER;
967 intel_decoder_check_vc1_parameter(VADriverContextP ctx,
968 struct decode_state *decode_state)
970 struct i965_driver_data *i965 = i965_driver_data(ctx);
971 VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
972 struct object_surface *obj_surface;
975 if (pic_param->sequence_fields.bits.interlace == 1 &&
976 pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
977 return VA_STATUS_ERROR_DECODING_ERROR;
980 if (pic_param->picture_fields.bits.picture_type == 0 ||
981 pic_param->picture_fields.bits.picture_type == 3) {
982 } else if (pic_param->picture_fields.bits.picture_type == 1 ||
983 pic_param->picture_fields.bits.picture_type == 4) {
984 obj_surface = SURFACE(pic_param->forward_reference_picture);
986 if (!obj_surface || !obj_surface->bo)
987 decode_state->reference_objects[i++] = NULL;
989 decode_state->reference_objects[i++] = obj_surface;
990 } else if (pic_param->picture_fields.bits.picture_type == 2) {
991 obj_surface = SURFACE(pic_param->forward_reference_picture);
993 if (!obj_surface || !obj_surface->bo)
994 decode_state->reference_objects[i++] = NULL;
996 decode_state->reference_objects[i++] = obj_surface;
998 obj_surface = SURFACE(pic_param->backward_reference_picture);
1000 if (!obj_surface || !obj_surface->bo)
1001 decode_state->reference_objects[i++] = NULL;
1003 decode_state->reference_objects[i++] = obj_surface;
1007 for ( ; i < 16; i++)
1008 decode_state->reference_objects[i] = NULL;
1010 return VA_STATUS_SUCCESS;
1013 return VA_STATUS_ERROR_INVALID_PARAMETER;
1017 intel_decoder_check_vp8_parameter(VADriverContextP ctx,
1018 struct decode_state *decode_state)
1020 struct i965_driver_data *i965 = i965_driver_data(ctx);
1021 VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
1022 struct object_surface *obj_surface;
1025 if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
1026 obj_surface = SURFACE(pic_param->last_ref_frame);
1028 if (obj_surface && obj_surface->bo)
1029 decode_state->reference_objects[i++] = obj_surface;
1031 decode_state->reference_objects[i++] = NULL;
1034 if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
1035 obj_surface = SURFACE(pic_param->golden_ref_frame);
1037 if (obj_surface && obj_surface->bo)
1038 decode_state->reference_objects[i++] = obj_surface;
1040 decode_state->reference_objects[i++] = NULL;
1043 if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
1044 obj_surface = SURFACE(pic_param->alt_ref_frame);
1046 if (obj_surface && obj_surface->bo)
1047 decode_state->reference_objects[i++] = obj_surface;
1049 decode_state->reference_objects[i++] = NULL;
1052 for ( ; i < 16; i++)
1053 decode_state->reference_objects[i] = NULL;
1055 return VA_STATUS_SUCCESS;
1059 intel_decoder_sanity_check_input(VADriverContextP ctx,
1061 struct decode_state *decode_state)
1063 struct i965_driver_data *i965 = i965_driver_data(ctx);
1064 struct object_surface *obj_surface;
1065 VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
1067 if (decode_state->current_render_target == VA_INVALID_SURFACE)
1070 obj_surface = SURFACE(decode_state->current_render_target);
1075 decode_state->render_object = obj_surface;
1078 case VAProfileMPEG2Simple:
1079 case VAProfileMPEG2Main:
1080 vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
1083 case VAProfileH264ConstrainedBaseline:
1084 case VAProfileH264Main:
1085 case VAProfileH264High:
1086 case VAProfileH264StereoHigh:
1087 case VAProfileH264MultiviewHigh:
1088 vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
1091 case VAProfileVC1Simple:
1092 case VAProfileVC1Main:
1093 case VAProfileVC1Advanced:
1094 vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
1097 case VAProfileJPEGBaseline:
1098 vaStatus = VA_STATUS_SUCCESS;
1101 case VAProfileVP8Version0_3:
1102 vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
1106 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
1115 * Return the next slice paramter
1118 * slice_param: the current slice
1119 * *group_idx & *element_idx the current slice position in slice groups
1121 * Return the next slice parameter
1122 * *group_idx & *element_idx the next slice position in slice groups,
1123 * if the next slice is NULL, *group_idx & *element_idx will be ignored
1125 VASliceParameterBufferMPEG2 *
1126 intel_mpeg2_find_next_slice(struct decode_state *decode_state,
1127 VAPictureParameterBufferMPEG2 *pic_param,
1128 VASliceParameterBufferMPEG2 *slice_param,
1132 VASliceParameterBufferMPEG2 *next_slice_param;
1133 unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
1134 int j = *group_idx, i = *element_idx + 1;
1136 for (; j < decode_state->num_slice_params; j++) {
1137 for (; i < decode_state->slice_params[j]->num_elements; i++) {
1138 next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
1140 if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
1141 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
1145 return next_slice_param;
1155 /* Ensure the segmentation buffer is large enough for the supplied
1156 number of MBs, or re-allocate it */
1158 intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
1159 unsigned int mb_width, unsigned int mb_height)
1161 struct i965_driver_data * const i965 = i965_driver_data(ctx);
1162 /* The segmentation map is a 64-byte aligned linear buffer, with
1163 each cache line holding only 8 bits for 4 continuous MBs */
1164 const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
1167 if (buf->bo && buf->bo->size >= buf_size)
1169 drm_intel_bo_unreference(buf->bo);
1173 buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
1175 buf->valid = buf->bo != NULL;