decoder: h264: fix submission of AVC_REF_IDX_STATE command.
[platform/upstream/libva-intel-driver.git] / src / i965_decoder_utils.c
1 /*
2  * Copyright (C) 2006-2012 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "sysdeps.h"
25
26 #include <alloca.h>
27
28 #include "intel_batchbuffer.h"
29 #include "i965_drv_video.h"
30 #include "i965_decoder_utils.h"
31 #include "i965_defines.h"
32
33 /* Set reference surface if backing store exists */
34 static inline int
35 set_ref_frame(
36     struct i965_driver_data *i965,
37     GenFrameStore           *ref_frame,
38     VASurfaceID              va_surface,
39     struct object_surface   *obj_surface
40 )
41 {
42     if (va_surface == VA_INVALID_ID)
43         return 0;
44
45     if (!obj_surface || !obj_surface->bo)
46         return 0;
47
48     ref_frame->surface_id = va_surface;
49     ref_frame->obj_surface = obj_surface;
50     return 1;
51 }
52
53 /* Check wether codec layer incorrectly fills in slice_vertical_position */
54 int
55 mpeg2_wa_slice_vertical_position(
56     struct decode_state           *decode_state,
57     VAPictureParameterBufferMPEG2 *pic_param
58 )
59 {
60     unsigned int i, j, mb_height, vpos, last_vpos = 0;
61
62     /* Assume progressive sequence if we got a progressive frame */
63     if (pic_param->picture_coding_extension.bits.progressive_frame)
64         return 0;
65
66     /* Wait for a field coded picture */
67     if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_FRAME)
68         return -1;
69
70     assert(decode_state && decode_state->slice_params);
71
72     mb_height = (pic_param->vertical_size + 31) / 32;
73
74     for (j = 0; j < decode_state->num_slice_params; j++) {
75         struct buffer_store * const buffer_store =
76             decode_state->slice_params[j];
77
78         for (i = 0; i < buffer_store->num_elements; i++) {
79             VASliceParameterBufferMPEG2 * const slice_param =
80                 ((VASliceParameterBufferMPEG2 *)buffer_store->buffer) + i;
81
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");
85                 return 1;
86             }
87             last_vpos = vpos;
88         }
89     }
90     return 0;
91 }
92
93 /* Build MPEG-2 reference frames array */
94 void
95 mpeg2_set_reference_surfaces(
96     VADriverContextP               ctx,
97     GenFrameStore                  ref_frames[MAX_GEN_REFERENCE_FRAMES],
98     struct decode_state           *decode_state,
99     VAPictureParameterBufferMPEG2 *pic_param
100 )
101 {
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;
106
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;
110
111     ref_frames[0].surface_id = VA_INVALID_ID;
112     ref_frames[0].obj_surface = NULL;
113
114     /* Reference frames are indexed by frame store ID  (0:top, 1:bottom) */
115     switch (pic_param->picture_coding_type) {
116     case MPEG_P_PICTURE:
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);
121         }
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);
125         break;
126
127     case MPEG_B_PICTURE:
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);
134         break;
135     }
136
137     while (n != 2) {
138         ref_frames[n].obj_surface = ref_frames[0].obj_surface;
139         ref_frames[n++].surface_id = ref_frames[0].surface_id;
140     }
141
142     if (pic_param->picture_coding_extension.bits.frame_pred_frame_dct)
143         return;
144
145     ref_frames[2].surface_id = VA_INVALID_ID;
146     ref_frames[2].obj_surface = NULL;
147
148     /* Bottom field pictures used as reference */
149     switch (pic_param->picture_coding_type) {
150     case MPEG_P_PICTURE:
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);
155         }
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);
159         break;
160
161     case MPEG_B_PICTURE:
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);
168         break;
169     }
170
171     while (n != 4) {
172         ref_frames[n].obj_surface = ref_frames[2].obj_surface;
173         ref_frames[n++].surface_id = ref_frames[2].surface_id;
174     }
175 }
176
177 /* Ensure the supplied VA surface has valid storage for decoding the
178    current picture */
179 VAStatus
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
185 )
186 {
187     VAStatus va_status;
188     uint32_t hw_fourcc, fourcc, subsample, chroma_format;
189
190     /* Validate chroma format */
191     switch (pic_param->seq_fields.bits.chroma_format_idc) {
192     case 0: // Grayscale
193         fourcc = VA_FOURCC_Y800;
194         subsample = SUBSAMPLE_YUV400;
195         chroma_format = VA_RT_FORMAT_YUV400;
196         break;
197     case 1: // YUV 4:2:0
198         fourcc = VA_FOURCC_NV12;
199         subsample = SUBSAMPLE_YUV420;
200         chroma_format = VA_RT_FORMAT_YUV420;
201         break;
202     default:
203         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
204     }
205
206     /* Determine the HW surface format, bound to VA config needs */
207     if ((decode_state->base.chroma_formats & chroma_format) == chroma_format)
208         hw_fourcc = fourcc;
209     else {
210         hw_fourcc = 0;
211         switch (fourcc) {
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;
216             }
217             break;
218         }
219     }
220     if (!hw_fourcc)
221         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
222
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);
226
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)
231             return va_status;
232     }
233
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;
238
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);
242     }
243     return VA_STATUS_SUCCESS;
244 }
245
246 /* Generate flat scaling matrices for H.264 decoding */
247 void
248 avc_gen_default_iq_matrix(VAIQMatrixBufferH264 *iq_matrix)
249 {
250     /* Flat_4x4_16 */
251     memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
252
253     /* Flat_8x8_16 */
254     memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
255 }
256
257 /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
258 /* XXX: slice_data_bit_offset does not account for EPB */
259 unsigned int
260 avc_get_first_mb_bit_offset(
261     dri_bo                     *slice_data_bo,
262     VASliceParameterBufferH264 *slice_param,
263     unsigned int                mode_flag
264 )
265 {
266     unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
267
268     if (mode_flag == ENTROPY_CABAC)
269         slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
270     return slice_data_bit_offset;
271 }
272
273 /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
274 /* XXX: slice_data_bit_offset does not account for EPB */
275 unsigned int
276 avc_get_first_mb_bit_offset_with_epb(
277     dri_bo                     *slice_data_bo,
278     VASliceParameterBufferH264 *slice_param,
279     unsigned int                mode_flag
280 )
281 {
282     unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
283     unsigned int out_slice_data_bit_offset;
284     unsigned int i, j, n, buf_size, data_size, header_size;
285     uint8_t *buf;
286     int ret;
287
288     header_size = slice_param->slice_data_bit_offset / 8;
289     data_size   = slice_param->slice_data_size - slice_param->slice_data_offset;
290     buf_size    = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
291
292     if (buf_size > data_size)
293         buf_size = data_size;
294
295     buf = alloca(buf_size);
296     ret = dri_bo_get_subdata(
297         slice_data_bo, slice_param->slice_data_offset,
298         buf_size, buf
299     );
300     assert(ret == 0);
301
302     for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
303         if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
304             i += 2, j++, n++;
305     }
306
307     out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
308
309     if (mode_flag == ENTROPY_CABAC)
310         out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
311     return out_slice_data_bit_offset;
312 }
313
314 static inline uint8_t
315 get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
316 {
317     const unsigned int is_long_term =
318         !!(va_pic->flags & VA_PICTURE_H264_LONG_TERM_REFERENCE);
319     const unsigned int is_top_field =
320         !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
321     const unsigned int is_bottom_field =
322         !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
323
324     return ((is_long_term                         << 6) |
325             ((is_top_field ^ is_bottom_field ^ 1) << 5) |
326             (frame_store_id                       << 1) |
327             ((is_top_field ^ 1) & is_bottom_field));
328 }
329
330 /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
331 void
332 gen5_fill_avc_ref_idx_state(
333     uint8_t             state[32],
334     const VAPictureH264 ref_list[32],
335     unsigned int        ref_list_count,
336     const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
337 )
338 {
339     int i, j;
340
341     for (i = 0; i < ref_list_count; i++) {
342         const VAPictureH264 * const va_pic = &ref_list[i];
343
344         if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
345             va_pic->picture_id == VA_INVALID_ID) {
346             state[i] = 0xff;
347             continue;
348         }
349
350         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
351             if (frame_store[j].surface_id == va_pic->picture_id)
352                 break;
353         }
354
355         if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
356             const GenFrameStore * const fs = &frame_store[j];
357             assert(fs->frame_store_id == j); // Current architecture/assumption
358             state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
359         }
360         else {
361             WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
362             state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
363         }
364     }
365
366     for (; i < 32; i++)
367         state[i] = 0xff;
368 }
369
370 /* Emit Reference List Entries (Gen6+: SNB, IVB) */
371 static void
372 gen6_send_avc_ref_idx_state_1(
373     struct intel_batchbuffer         *batch,
374     unsigned int                      list,
375     const VAPictureH264              *ref_list,
376     unsigned int                      ref_list_count,
377     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
378 )
379 {
380     uint8_t ref_idx_state[32];
381
382     BEGIN_BCS_BATCH(batch, 10);
383     OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
384     OUT_BCS_BATCH(batch, list);
385     gen5_fill_avc_ref_idx_state(
386         ref_idx_state,
387         ref_list, ref_list_count,
388         frame_store
389     );
390     intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
391     ADVANCE_BCS_BATCH(batch);
392 }
393
394 void
395 gen6_send_avc_ref_idx_state(
396     struct intel_batchbuffer         *batch,
397     const VASliceParameterBufferH264 *slice_param,
398     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
399 )
400 {
401     if (slice_param->slice_type == SLICE_TYPE_I ||
402         slice_param->slice_type == SLICE_TYPE_SI)
403         return;
404
405     /* RefPicList0 */
406     gen6_send_avc_ref_idx_state_1(
407         batch, 0,
408         slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
409         frame_store
410     );
411
412     if (slice_param->slice_type != SLICE_TYPE_B)
413         return;
414
415     /* RefPicList1 */
416     gen6_send_avc_ref_idx_state_1(
417         batch, 1,
418         slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
419         frame_store
420     );
421 }
422
423 void
424 intel_update_avc_frame_store_index(
425     VADriverContextP              ctx,
426     struct decode_state          *decode_state,
427     VAPictureParameterBufferH264 *pic_param,
428     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES]
429 )
430 {
431     GenFrameStore *free_refs[MAX_GEN_REFERENCE_FRAMES];
432     int i, j, n, num_free_refs;
433
434     /* Remove obsolete entries from the internal DPB */
435     for (i = 0, n = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
436         GenFrameStore * const fs = &frame_store[i];
437         if (fs->surface_id == VA_INVALID_ID || !fs->obj_surface) {
438             free_refs[n++] = fs;
439             continue;
440         }
441
442         // Find whether the current entry is still a valid reference frame
443         for (j = 0; j < ARRAY_ELEMS(decode_state->reference_objects); j++) {
444             struct object_surface * const obj_surface =
445                 decode_state->reference_objects[j];
446             if (obj_surface && obj_surface == fs->obj_surface)
447                 break;
448         }
449
450         // ... or remove it
451         if (j == ARRAY_ELEMS(decode_state->reference_objects)) {
452             fs->surface_id = VA_INVALID_ID;
453             fs->obj_surface = NULL;
454             fs->frame_store_id = -1;
455             free_refs[n++] = fs;
456         }
457     }
458     num_free_refs = n;
459
460     /* Append the new reference frames */
461     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
462         struct object_surface * const obj_surface =
463             decode_state->reference_objects[i];
464         if (!obj_surface)
465             continue;
466
467         // Find whether the current frame is not already in our frame store
468         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
469             GenFrameStore * const fs = &frame_store[j];
470             if (fs->obj_surface == obj_surface)
471                 break;
472         }
473
474         // ... or add it
475         if (j == MAX_GEN_REFERENCE_FRAMES) {
476             if (n < num_free_refs) {
477                 GenFrameStore * const fs = free_refs[n++];
478                 fs->surface_id = obj_surface->base.id;
479                 fs->obj_surface = obj_surface;
480                 fs->frame_store_id = fs - frame_store;
481                 continue;
482             }
483             WARN_ONCE("No free slot found for DPB reference list!!!\n");
484         }
485     }
486 }
487
488 void
489 intel_update_vc1_frame_store_index(VADriverContextP ctx,
490                                    struct decode_state *decode_state,
491                                    VAPictureParameterBufferVC1 *pic_param,
492                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
493 {
494     struct object_surface *obj_surface;
495     int i;
496
497     obj_surface = decode_state->reference_objects[0];
498
499     if (pic_param->forward_reference_picture == VA_INVALID_ID ||
500         !obj_surface || 
501         !obj_surface->bo) {
502         frame_store[0].surface_id = VA_INVALID_ID;
503         frame_store[0].obj_surface = NULL;
504     } else {
505         frame_store[0].surface_id = pic_param->forward_reference_picture;
506         frame_store[0].obj_surface = obj_surface;
507     }
508
509     obj_surface = decode_state->reference_objects[1];
510
511     if (pic_param->backward_reference_picture == VA_INVALID_ID ||
512         !obj_surface || 
513         !obj_surface->bo) {
514         frame_store[1].surface_id = frame_store[0].surface_id;
515         frame_store[1].obj_surface = frame_store[0].obj_surface;
516     } else {
517         frame_store[1].surface_id = pic_param->backward_reference_picture;
518         frame_store[1].obj_surface = obj_surface;
519     }
520     for (i = 2; i < MAX_GEN_REFERENCE_FRAMES; i++) {
521         frame_store[i].surface_id = frame_store[i % 2].surface_id;
522         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
523     }
524
525 }
526
527 void
528 intel_update_vp8_frame_store_index(VADriverContextP ctx,
529                                    struct decode_state *decode_state,
530                                    VAPictureParameterBufferVP8 *pic_param,
531                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
532 {
533     struct object_surface *obj_surface;
534     int i;
535
536     obj_surface = decode_state->reference_objects[0];
537
538     if (pic_param->last_ref_frame == VA_INVALID_ID ||
539         !obj_surface ||
540         !obj_surface->bo) {
541         frame_store[0].surface_id = VA_INVALID_ID;
542         frame_store[0].obj_surface = NULL;
543     } else {
544         frame_store[0].surface_id = pic_param->last_ref_frame;
545         frame_store[0].obj_surface = obj_surface;
546     }
547
548     obj_surface = decode_state->reference_objects[1];
549
550     if (pic_param->golden_ref_frame == VA_INVALID_ID ||
551         !obj_surface ||
552         !obj_surface->bo) {
553         frame_store[1].surface_id = frame_store[0].surface_id;
554         frame_store[1].obj_surface = frame_store[0].obj_surface;
555     } else {
556         frame_store[1].surface_id = pic_param->golden_ref_frame;
557         frame_store[1].obj_surface = obj_surface;
558     }
559
560     obj_surface = decode_state->reference_objects[2];
561
562     if (pic_param->alt_ref_frame == VA_INVALID_ID ||
563         !obj_surface ||
564         !obj_surface->bo) {
565         frame_store[2].surface_id = frame_store[0].surface_id;
566         frame_store[2].obj_surface = frame_store[0].obj_surface;
567     } else {
568         frame_store[2].surface_id = pic_param->alt_ref_frame;
569         frame_store[2].obj_surface = obj_surface;
570     }
571
572     for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
573         frame_store[i].surface_id = frame_store[i % 2].surface_id;
574         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
575     }
576
577 }
578
579 static VAStatus
580 intel_decoder_check_avc_parameter(VADriverContextP ctx,
581                                   VAProfile h264_profile,
582                                   struct decode_state *decode_state)
583 {
584     struct i965_driver_data *i965 = i965_driver_data(ctx);
585     VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
586     VAStatus va_status;
587     struct object_surface *obj_surface; 
588     int i;
589
590     assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
591     assert(pic_param->CurrPic.picture_id != VA_INVALID_SURFACE);
592
593     if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID ||
594         pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
595         goto error;
596
597     assert(pic_param->CurrPic.picture_id == decode_state->current_render_target);
598
599     if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
600         goto error;
601
602     if ((h264_profile != VAProfileH264Baseline)) {
603        if (pic_param->num_slice_groups_minus1 ||
604            pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
605            WARN_ONCE("Unsupported the FMO/ASO constraints!!!\n");
606            goto error;
607        }
608     }
609
610     /* Fill in the reference objects array with the actual VA surface
611        objects with 1:1 correspondance with any entry in ReferenceFrames[],
612        i.e. including "holes" for invalid entries, that are expanded
613        to NULL in the reference_objects[] array */
614     for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
615         const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
616
617         obj_surface = NULL;
618         if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
619             va_pic->picture_id != VA_INVALID_ID) {
620             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
621             if (!obj_surface)
622                 return VA_STATUS_ERROR_INVALID_SURFACE;
623
624             /*
625              * Sometimes a dummy frame comes from the upper layer
626              * library, call i965_check_alloc_surface_bo() to make
627              * sure the store buffer is allocated for this reference
628              * frame
629              */
630             va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
631                 pic_param);
632             if (va_status != VA_STATUS_SUCCESS)
633                 return va_status;
634         }
635         decode_state->reference_objects[i] = obj_surface;
636     }
637     return VA_STATUS_SUCCESS;
638
639 error:
640     return VA_STATUS_ERROR_INVALID_PARAMETER;
641 }
642
643 static VAStatus
644 intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
645                                     struct decode_state *decode_state)
646 {
647     struct i965_driver_data *i965 = i965_driver_data(ctx);
648     VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
649     struct object_surface *obj_surface; 
650     int i = 0;
651     
652     if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
653     } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
654         obj_surface = SURFACE(pic_param->forward_reference_picture);
655
656         if (!obj_surface || !obj_surface->bo)
657             decode_state->reference_objects[i++] = NULL;
658         else
659             decode_state->reference_objects[i++] = obj_surface;
660     } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
661         obj_surface = SURFACE(pic_param->forward_reference_picture);
662
663         if (!obj_surface || !obj_surface->bo)
664             decode_state->reference_objects[i++] = NULL;
665         else
666             decode_state->reference_objects[i++] = obj_surface;
667
668         obj_surface = SURFACE(pic_param->backward_reference_picture);
669
670         if (!obj_surface || !obj_surface->bo)
671             decode_state->reference_objects[i++] = NULL;
672         else
673             decode_state->reference_objects[i++] = obj_surface;
674     } else
675         goto error;
676
677     for ( ; i < 16; i++)
678         decode_state->reference_objects[i] = NULL;
679
680     return VA_STATUS_SUCCESS;
681
682 error:
683     return VA_STATUS_ERROR_INVALID_PARAMETER;
684 }
685
686 static VAStatus
687 intel_decoder_check_vc1_parameter(VADriverContextP ctx,
688                                   struct decode_state *decode_state)
689 {
690     struct i965_driver_data *i965 = i965_driver_data(ctx);
691     VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
692     struct object_surface *obj_surface; 
693     int i = 0;
694
695     if (pic_param->sequence_fields.bits.interlace == 1 &&
696         pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
697         return VA_STATUS_ERROR_DECODING_ERROR;
698     }
699
700     if (pic_param->picture_fields.bits.picture_type == 0 ||
701         pic_param->picture_fields.bits.picture_type == 3) {
702     } else if (pic_param->picture_fields.bits.picture_type == 1 ||
703                pic_param->picture_fields.bits.picture_type == 4) {
704         obj_surface = SURFACE(pic_param->forward_reference_picture);
705
706         if (!obj_surface || !obj_surface->bo)
707             decode_state->reference_objects[i++] = NULL;
708         else
709             decode_state->reference_objects[i++] = obj_surface;
710     } else if (pic_param->picture_fields.bits.picture_type == 2) {
711         obj_surface = SURFACE(pic_param->forward_reference_picture);
712
713         if (!obj_surface || !obj_surface->bo)
714             decode_state->reference_objects[i++] = NULL;
715         else
716             decode_state->reference_objects[i++] = obj_surface;
717
718         obj_surface = SURFACE(pic_param->backward_reference_picture);
719
720         if (!obj_surface || !obj_surface->bo)
721             decode_state->reference_objects[i++] = NULL;
722         else
723             decode_state->reference_objects[i++] = obj_surface;
724     } else 
725         goto error;
726
727     for ( ; i < 16; i++)
728         decode_state->reference_objects[i] = NULL;
729
730     return VA_STATUS_SUCCESS;
731
732 error:
733     return VA_STATUS_ERROR_INVALID_PARAMETER;
734 }
735
736 static VAStatus
737 intel_decoder_check_vp8_parameter(VADriverContextP ctx,
738                                   struct decode_state *decode_state)
739 {
740     struct i965_driver_data *i965 = i965_driver_data(ctx);
741     VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
742     struct object_surface *obj_surface; 
743     int i = 0;
744
745     if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
746         obj_surface = SURFACE(pic_param->last_ref_frame);
747
748         if (obj_surface && obj_surface->bo)
749             decode_state->reference_objects[i++] = obj_surface;
750         else
751             decode_state->reference_objects[i++] = NULL;
752     }
753
754     if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
755         obj_surface = SURFACE(pic_param->golden_ref_frame);
756
757         if (obj_surface && obj_surface->bo)
758             decode_state->reference_objects[i++] = obj_surface;
759         else
760             decode_state->reference_objects[i++] = NULL;
761     }
762
763     if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
764         obj_surface = SURFACE(pic_param->alt_ref_frame);
765
766         if (obj_surface && obj_surface->bo)
767             decode_state->reference_objects[i++] = obj_surface;
768         else
769             decode_state->reference_objects[i++] = NULL;
770     }
771
772     for ( ; i < 16; i++)
773         decode_state->reference_objects[i] = NULL;
774
775     return VA_STATUS_SUCCESS;
776 }
777
778 VAStatus
779 intel_decoder_sanity_check_input(VADriverContextP ctx,
780                                  VAProfile profile,
781                                  struct decode_state *decode_state)
782 {
783     struct i965_driver_data *i965 = i965_driver_data(ctx);
784     struct object_surface *obj_surface;
785     VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
786
787     if (decode_state->current_render_target == VA_INVALID_SURFACE)
788         goto out;
789         
790     obj_surface = SURFACE(decode_state->current_render_target);
791
792     if (!obj_surface)
793         goto out;
794
795     decode_state->render_object = obj_surface;
796
797     switch (profile) {
798     case VAProfileMPEG2Simple:
799     case VAProfileMPEG2Main:
800         vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
801         break;
802         
803     case VAProfileH264ConstrainedBaseline:
804     case VAProfileH264Main:
805     case VAProfileH264High:
806         vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
807         break;
808
809     case VAProfileVC1Simple:
810     case VAProfileVC1Main:
811     case VAProfileVC1Advanced:
812         vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
813         break;
814
815     case VAProfileJPEGBaseline:
816         vaStatus = VA_STATUS_SUCCESS;
817         break;
818
819     case VAProfileVP8Version0_3:
820         vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
821         break;
822
823     default:
824         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
825         break;
826     }
827
828 out:
829     return vaStatus;
830 }
831
832 /*
833  * Return the next slice paramter
834  *
835  * Input:
836  *      slice_param: the current slice
837  *      *group_idx & *element_idx the current slice position in slice groups
838  * Output:
839  *      Return the next slice parameter
840  *      *group_idx & *element_idx the next slice position in slice groups,
841  *      if the next slice is NULL, *group_idx & *element_idx will be ignored
842  */
843 VASliceParameterBufferMPEG2 *
844 intel_mpeg2_find_next_slice(struct decode_state *decode_state,
845                             VAPictureParameterBufferMPEG2 *pic_param,
846                             VASliceParameterBufferMPEG2 *slice_param,
847                             int *group_idx,
848                             int *element_idx)
849 {
850     VASliceParameterBufferMPEG2 *next_slice_param;
851     unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
852     int j = *group_idx, i = *element_idx + 1;
853
854     for (; j < decode_state->num_slice_params; j++) {
855         for (; i < decode_state->slice_params[j]->num_elements; i++) {
856             next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
857
858             if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
859                 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
860                 *group_idx = j;
861                 *element_idx = i;
862
863                 return next_slice_param;
864             }
865         }
866
867         i = 0;
868     }
869
870     return NULL;
871 }
872
873 /* Ensure the segmentation buffer is large enough for the supplied
874    number of MBs, or re-allocate it */
875 bool
876 intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
877     unsigned int mb_width, unsigned int mb_height)
878 {
879     struct i965_driver_data * const i965 = i965_driver_data(ctx);
880     /* The segmentation map is a 64-byte aligned linear buffer, with
881        each cache line holding only 8 bits for 4 continuous MBs */
882     const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
883
884     if (buf->valid) {
885         if (buf->bo && buf->bo->size >= buf_size)
886             return true;
887         drm_intel_bo_unreference(buf->bo);
888         buf->valid = false;
889     }
890
891     buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
892         buf_size, 0x1000);
893     buf->valid = buf->bo != NULL;
894     return buf->valid;
895 }