OSDN Git Service

Add vdenc common commands for CNL
[android-x86/hardware-intel-common-vaapi.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 #include <limits.h>
26
27 #include "intel_batchbuffer.h"
28 #include "intel_media.h"
29 #include "i965_drv_video.h"
30 #include "i965_decoder_utils.h"
31 #include "i965_defines.h"
32
33 static const int fptype_to_picture_type[8][2] = {
34     {VC1_I_PICTURE, VC1_I_PICTURE},
35     {VC1_I_PICTURE, VC1_P_PICTURE},
36     {VC1_P_PICTURE, VC1_I_PICTURE},
37     {VC1_P_PICTURE, VC1_P_PICTURE},
38     {VC1_B_PICTURE, VC1_B_PICTURE},
39     {VC1_B_PICTURE, VC1_BI_PICTURE},
40     {VC1_BI_PICTURE, VC1_B_PICTURE},
41     {VC1_BI_PICTURE, VC1_BI_PICTURE}
42 };
43
44 /* Set reference surface if backing store exists */
45 static inline int
46 set_ref_frame(
47     struct i965_driver_data *i965,
48     GenFrameStore           *ref_frame,
49     VASurfaceID              va_surface,
50     struct object_surface   *obj_surface
51 )
52 {
53     if (va_surface == VA_INVALID_ID)
54         return 0;
55
56     if (!obj_surface || !obj_surface->bo)
57         return 0;
58
59     ref_frame->surface_id = va_surface;
60     ref_frame->obj_surface = obj_surface;
61     return 1;
62 }
63
64 /* Check wether codec layer incorrectly fills in slice_vertical_position */
65 int
66 mpeg2_wa_slice_vertical_position(
67     struct decode_state           *decode_state,
68     VAPictureParameterBufferMPEG2 *pic_param
69 )
70 {
71     unsigned int i, j, mb_height, vpos, last_vpos = 0;
72
73     /* Assume progressive sequence if we got a progressive frame */
74     if (pic_param->picture_coding_extension.bits.progressive_frame)
75         return 0;
76
77     /* Wait for a field coded picture */
78     if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_FRAME)
79         return -1;
80
81     assert(decode_state && decode_state->slice_params);
82
83     mb_height = (pic_param->vertical_size + 31) / 32;
84
85     for (j = 0; j < decode_state->num_slice_params; j++) {
86         struct buffer_store * const buffer_store =
87                     decode_state->slice_params[j];
88
89         for (i = 0; i < buffer_store->num_elements; i++) {
90             VASliceParameterBufferMPEG2 * const slice_param =
91                 ((VASliceParameterBufferMPEG2 *)buffer_store->buffer) + i;
92
93             vpos = slice_param->slice_vertical_position;
94             if (vpos >= mb_height || vpos == last_vpos + 2) {
95                 WARN_ONCE("codec layer incorrectly fills in MPEG-2 slice_vertical_position. Workaround applied\n");
96                 return 1;
97             }
98             last_vpos = vpos;
99         }
100     }
101     return 0;
102 }
103
104 /* Build MPEG-2 reference frames array */
105 void
106 mpeg2_set_reference_surfaces(
107     VADriverContextP               ctx,
108     GenFrameStore                  ref_frames[MAX_GEN_REFERENCE_FRAMES],
109     struct decode_state           *decode_state,
110     VAPictureParameterBufferMPEG2 *pic_param
111 )
112 {
113     struct i965_driver_data * const i965 = i965_driver_data(ctx);
114     VASurfaceID va_surface;
115     unsigned pic_structure, is_second_field, n = 0;
116     struct object_surface *obj_surface;
117
118     pic_structure = pic_param->picture_coding_extension.bits.picture_structure;
119     is_second_field = pic_structure != MPEG_FRAME &&
120                       !pic_param->picture_coding_extension.bits.is_first_field;
121
122     ref_frames[0].surface_id = VA_INVALID_ID;
123     ref_frames[0].obj_surface = NULL;
124
125     /* Reference frames are indexed by frame store ID  (0:top, 1:bottom) */
126     switch (pic_param->picture_coding_type) {
127     case MPEG_P_PICTURE:
128         if (is_second_field && pic_structure == MPEG_BOTTOM_FIELD) {
129             va_surface = decode_state->current_render_target;
130             obj_surface = decode_state->render_object;
131             n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
132         }
133         va_surface = pic_param->forward_reference_picture;
134         obj_surface = decode_state->reference_objects[0];
135         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
136         break;
137
138     case MPEG_B_PICTURE:
139         va_surface = pic_param->forward_reference_picture;
140         obj_surface = decode_state->reference_objects[0];
141         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
142         va_surface = pic_param->backward_reference_picture;
143         obj_surface = decode_state->reference_objects[1];
144         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
145         break;
146     }
147
148     while (n != 2) {
149         ref_frames[n].obj_surface = ref_frames[0].obj_surface;
150         ref_frames[n++].surface_id = ref_frames[0].surface_id;
151     }
152
153     if (pic_param->picture_coding_extension.bits.frame_pred_frame_dct)
154         return;
155
156     ref_frames[2].surface_id = VA_INVALID_ID;
157     ref_frames[2].obj_surface = NULL;
158
159     /* Bottom field pictures used as reference */
160     switch (pic_param->picture_coding_type) {
161     case MPEG_P_PICTURE:
162         if (is_second_field && pic_structure == MPEG_TOP_FIELD) {
163             va_surface = decode_state->current_render_target;
164             obj_surface = decode_state->render_object;
165             n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
166         }
167         va_surface = pic_param->forward_reference_picture;
168         obj_surface = decode_state->reference_objects[0];
169         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
170         break;
171
172     case MPEG_B_PICTURE:
173         va_surface = pic_param->forward_reference_picture;
174         obj_surface = decode_state->reference_objects[0];
175         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
176         va_surface = pic_param->backward_reference_picture;
177         obj_surface = decode_state->reference_objects[1];
178         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
179         break;
180     }
181
182     while (n != 4) {
183         ref_frames[n].obj_surface = ref_frames[2].obj_surface;
184         ref_frames[n++].surface_id = ref_frames[2].surface_id;
185     }
186 }
187
188 /* Ensure the supplied VA surface has valid storage for decoding the
189    current picture */
190 VAStatus
191 avc_ensure_surface_bo(
192     VADriverContextP                    ctx,
193     struct decode_state                *decode_state,
194     struct object_surface              *obj_surface,
195     const VAPictureParameterBufferH264 *pic_param
196 )
197 {
198     VAStatus va_status;
199     uint32_t hw_fourcc, fourcc, subsample, chroma_format;
200
201     /* Validate chroma format */
202     switch (pic_param->seq_fields.bits.chroma_format_idc) {
203     case 0: // Grayscale
204         fourcc = VA_FOURCC_Y800;
205         subsample = SUBSAMPLE_YUV400;
206         chroma_format = VA_RT_FORMAT_YUV400;
207         break;
208     case 1: // YUV 4:2:0
209         fourcc = VA_FOURCC_NV12;
210         subsample = SUBSAMPLE_YUV420;
211         chroma_format = VA_RT_FORMAT_YUV420;
212         break;
213     default:
214         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
215     }
216
217     /* Determine the HW surface format, bound to VA config needs */
218     if ((decode_state->base.chroma_formats & chroma_format) == chroma_format)
219         hw_fourcc = fourcc;
220     else {
221         hw_fourcc = 0;
222         switch (fourcc) {
223         case VA_FOURCC_Y800: // Implement with an NV12 surface
224             if (decode_state->base.chroma_formats & VA_RT_FORMAT_YUV420) {
225                 hw_fourcc = VA_FOURCC_NV12;
226                 subsample = SUBSAMPLE_YUV420;
227             }
228             break;
229         }
230     }
231     if (!hw_fourcc)
232         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
233
234     /* (Re-)allocate the underlying surface buffer store, if necessary */
235     if (!obj_surface->bo || obj_surface->fourcc != hw_fourcc) {
236         struct i965_driver_data * const i965 = i965_driver_data(ctx);
237
238         i965_destroy_surface_storage(obj_surface);
239         va_status = i965_check_alloc_surface_bo(ctx, obj_surface,
240                                                 i965->codec_info->has_tiled_surface, hw_fourcc, subsample);
241         if (va_status != VA_STATUS_SUCCESS)
242             return va_status;
243     }
244
245     /* Fake chroma components if grayscale is implemented on top of NV12 */
246     if (fourcc == VA_FOURCC_Y800 && hw_fourcc == VA_FOURCC_NV12) {
247         const uint32_t uv_offset = obj_surface->width * obj_surface->height;
248         const uint32_t uv_size   = obj_surface->width * obj_surface->height / 2;
249
250         drm_intel_gem_bo_map_gtt(obj_surface->bo);
251         memset(obj_surface->bo->virtual + uv_offset, 0x80, uv_size);
252         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
253     }
254     return VA_STATUS_SUCCESS;
255 }
256
257 /* Generate flat scaling matrices for H.264 decoding */
258 void
259 avc_gen_default_iq_matrix(VAIQMatrixBufferH264 *iq_matrix)
260 {
261     /* Flat_4x4_16 */
262     memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
263
264     /* Flat_8x8_16 */
265     memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
266 }
267
268 /* Returns the POC of the supplied VA picture */
269 static int
270 avc_get_picture_poc(const VAPictureH264 *va_pic)
271 {
272     int structure, field_poc[2];
273
274     structure = va_pic->flags &
275                 (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
276     field_poc[0] = structure != VA_PICTURE_H264_BOTTOM_FIELD ?
277                    va_pic->TopFieldOrderCnt : INT_MAX;
278     field_poc[1] = structure != VA_PICTURE_H264_TOP_FIELD ?
279                    va_pic->BottomFieldOrderCnt : INT_MAX;
280     return MIN(field_poc[0], field_poc[1]);
281 }
282
283 /* Returns a unique picture ID that represents the supplied VA surface object */
284 int
285 avc_get_picture_id(struct object_surface *obj_surface)
286 {
287     int pic_id;
288
289     /* This highly depends on how the internal VA objects are organized.
290
291        Theory of operations:
292        The VA objects are maintained in heaps so that any released VA
293        surface will become free again for future allocation. This means
294        that holes in there are filled in for subsequent allocations.
295        So, this ultimately means that we could just use the Heap ID of
296        the VA surface as the resulting picture ID (16 bits) */
297     pic_id = 1 + (obj_surface->base.id & OBJECT_HEAP_ID_MASK);
298     return (pic_id <= 0xffff) ? pic_id : -1;
299 }
300
301 /* Finds the VA/H264 picture associated with the specified VA surface id */
302 VAPictureH264 *
303 avc_find_picture(VASurfaceID id, VAPictureH264 *pic_list, int pic_list_count)
304 {
305     int i;
306
307     if (id != VA_INVALID_ID) {
308         for (i = 0; i < pic_list_count; i++) {
309             VAPictureH264 * const va_pic = &pic_list[i];
310             if (va_pic->picture_id == id &&
311                 !(va_pic->flags & VA_PICTURE_H264_INVALID))
312                 return va_pic;
313         }
314     }
315     return NULL;
316 }
317
318 /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
319 /* XXX: slice_data_bit_offset does not account for EPB */
320 unsigned int
321 avc_get_first_mb_bit_offset(
322     dri_bo                     *slice_data_bo,
323     VASliceParameterBufferH264 *slice_param,
324     unsigned int                mode_flag
325 )
326 {
327     unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
328
329     if (mode_flag == ENTROPY_CABAC)
330         slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
331     return slice_data_bit_offset;
332 }
333
334 /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
335 /* XXX: slice_data_bit_offset does not account for EPB */
336 unsigned int
337 avc_get_first_mb_bit_offset_with_epb(
338     dri_bo                     *slice_data_bo,
339     VASliceParameterBufferH264 *slice_param,
340     unsigned int                mode_flag
341 )
342 {
343     unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
344     unsigned int out_slice_data_bit_offset;
345     unsigned int i, j, n = 0, buf_size, data_size, header_size;
346     uint8_t *buf;
347     int ret;
348
349     header_size = slice_param->slice_data_bit_offset / 8;
350     data_size   = slice_param->slice_data_size - slice_param->slice_data_offset;
351     buf_size    = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
352
353     if (buf_size > data_size)
354         buf_size = data_size;
355
356     buf = malloc(buf_size);
357
358     if (!buf)
359         goto out;
360
361     ret = dri_bo_get_subdata(
362               slice_data_bo, slice_param->slice_data_offset,
363               buf_size, buf
364           );
365     assert(ret == 0);
366
367     for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
368         if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
369             i += 2, j++, n++;
370     }
371
372     free(buf);
373
374 out:
375     out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
376
377     if (mode_flag == ENTROPY_CABAC)
378         out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
379     return out_slice_data_bit_offset;
380 }
381
382 static inline uint8_t
383 get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
384 {
385     /* The H.264 standard, and the VA-API specification, allows for at
386        least 3 states for a picture: "used for short-term reference",
387        "used for long-term reference", or considered as not used for
388        reference.
389
390        The latter is used in the MVC inter prediction and inter-view
391        prediction process (H.8.4). This has an incidence on the
392        colZeroFlag variable, as defined in 8.4.1.2.
393
394        Since it is not possible to directly program that flag, let's
395        make the hardware derive this value by assimilating "considered
396        as not used for reference" to a "not used for short-term
397        reference", and subsequently making it "used for long-term
398        reference" to fit the definition of Bit6 here */
399     const unsigned int ref_flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE |
400                                    VA_PICTURE_H264_LONG_TERM_REFERENCE;
401     const unsigned int is_long_term =
402         ((va_pic->flags & ref_flags) != VA_PICTURE_H264_SHORT_TERM_REFERENCE);
403     const unsigned int is_top_field =
404         !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
405     const unsigned int is_bottom_field =
406         !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
407
408     return ((is_long_term                         << 6) |
409             ((is_top_field ^ is_bottom_field ^ 1) << 5) |
410             (frame_store_id                       << 1) |
411             ((is_top_field ^ 1) & is_bottom_field));
412 }
413
414 /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
415 void
416 gen5_fill_avc_ref_idx_state(
417     uint8_t             state[32],
418     const VAPictureH264 ref_list[32],
419     unsigned int        ref_list_count,
420     const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
421 )
422 {
423     int i, j;
424
425     for (i = 0; i < ref_list_count; i++) {
426         const VAPictureH264 * const va_pic = &ref_list[i];
427
428         if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
429             va_pic->picture_id == VA_INVALID_ID) {
430             state[i] = 0xff;
431             continue;
432         }
433
434         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
435             if (frame_store[j].surface_id == va_pic->picture_id)
436                 break;
437         }
438
439         if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
440             const GenFrameStore * const fs = &frame_store[j];
441             assert(fs->frame_store_id == j); // Current architecture/assumption
442             state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
443         } else {
444             WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
445             state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
446         }
447     }
448
449     for (; i < 32; i++)
450         state[i] = 0xff;
451 }
452
453 /* Emit Reference List Entries (Gen6+: SNB, IVB) */
454 static void
455 gen6_send_avc_ref_idx_state_1(
456     struct intel_batchbuffer         *batch,
457     unsigned int                      list,
458     const VAPictureH264              *ref_list,
459     unsigned int                      ref_list_count,
460     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
461 )
462 {
463     uint8_t ref_idx_state[32];
464
465     BEGIN_BCS_BATCH(batch, 10);
466     OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
467     OUT_BCS_BATCH(batch, list);
468     gen5_fill_avc_ref_idx_state(
469         ref_idx_state,
470         ref_list, ref_list_count,
471         frame_store
472     );
473     intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
474     ADVANCE_BCS_BATCH(batch);
475 }
476
477 void
478 gen6_send_avc_ref_idx_state(
479     struct intel_batchbuffer         *batch,
480     const VASliceParameterBufferH264 *slice_param,
481     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
482 )
483 {
484     if (slice_param->slice_type == SLICE_TYPE_I ||
485         slice_param->slice_type == SLICE_TYPE_SI)
486         return;
487
488     /* RefPicList0 */
489     gen6_send_avc_ref_idx_state_1(
490         batch, 0,
491         slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
492         frame_store
493     );
494
495     if (slice_param->slice_type != SLICE_TYPE_B)
496         return;
497
498     /* RefPicList1 */
499     gen6_send_avc_ref_idx_state_1(
500         batch, 1,
501         slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
502         frame_store
503     );
504 }
505
506 static void
507 gen6_mfd_avc_phantom_slice_state(VADriverContextP ctx,
508                                  VAPictureParameterBufferH264 *pic_param,
509                                  VASliceParameterBufferH264 *next_slice_param,
510                                  struct intel_batchbuffer *batch)
511 {
512     int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
513     int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1; /* frame height */
514     int slice_hor_pos, slice_ver_pos, slice_start_mb_num, next_slice_hor_pos, next_slice_ver_pos;
515     int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
516                          pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
517
518     if (next_slice_param) {
519         int first_mb_in_next_slice;
520
521         slice_hor_pos = 0;
522         slice_ver_pos = 0;
523         slice_start_mb_num = 0;
524         first_mb_in_next_slice = next_slice_param->first_mb_in_slice << mbaff_picture;
525         next_slice_hor_pos = first_mb_in_next_slice % width_in_mbs;
526         next_slice_ver_pos = first_mb_in_next_slice / width_in_mbs;
527     } else {
528         slice_hor_pos = 0;
529         slice_ver_pos = height_in_mbs;
530         slice_start_mb_num = width_in_mbs * height_in_mbs / (1 + !!pic_param->pic_fields.bits.field_pic_flag);
531         next_slice_hor_pos = 0;
532         next_slice_ver_pos = 0;
533     }
534
535     BEGIN_BCS_BATCH(batch, 11);
536     OUT_BCS_BATCH(batch, MFX_AVC_SLICE_STATE | (11 - 2));
537     OUT_BCS_BATCH(batch, 0);
538     OUT_BCS_BATCH(batch, 0);
539     OUT_BCS_BATCH(batch, 0);
540     OUT_BCS_BATCH(batch,
541                   slice_ver_pos << 24 |
542                   slice_hor_pos << 16 |
543                   slice_start_mb_num << 0);
544     OUT_BCS_BATCH(batch,
545                   next_slice_ver_pos << 16 |
546                   next_slice_hor_pos << 0);
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);
553 }
554
555 static void
556 gen6_mfd_avc_phantom_slice_bsd_object(VADriverContextP ctx,
557                                       VAPictureParameterBufferH264 *pic_param,
558                                       struct intel_batchbuffer *batch)
559 {
560
561     BEGIN_BCS_BATCH(batch, 6);
562     OUT_BCS_BATCH(batch, MFD_AVC_BSD_OBJECT | (6 - 2));
563     OUT_BCS_BATCH(batch, 0);
564     OUT_BCS_BATCH(batch, 0);
565     OUT_BCS_BATCH(batch, 0);
566     OUT_BCS_BATCH(batch, 0);
567     OUT_BCS_BATCH(batch, 0);
568     ADVANCE_BCS_BATCH(batch);
569 }
570
571 void
572 gen6_mfd_avc_phantom_slice(VADriverContextP ctx,
573                            VAPictureParameterBufferH264 *pic_param,
574                            VASliceParameterBufferH264 *next_slice_param,
575                            struct intel_batchbuffer *batch)
576 {
577     gen6_mfd_avc_phantom_slice_state(ctx, pic_param, next_slice_param, batch);
578     gen6_mfd_avc_phantom_slice_bsd_object(ctx, pic_param, batch);
579 }
580
581 /* Comparison function for sorting out the array of free frame store entries */
582 static int
583 compare_avc_ref_store_func(const void *p1, const void *p2)
584 {
585     const GenFrameStore * const fs1 = *((GenFrameStore **)p1);
586     const GenFrameStore * const fs2 = *((GenFrameStore **)p2);
587
588     return fs1->ref_age - fs2->ref_age;
589 }
590
591 static void
592 intel_update_codec_frame_store_index(
593     VADriverContextP              ctx,
594     struct decode_state          *decode_state,
595     int poc,
596     GenFrameStore                 frame_store[],
597     int num_elements,
598     GenFrameStoreContext         *fs_ctx
599 )
600 {
601     GenFrameStore **free_refs = calloc(num_elements, sizeof(GenFrameStore *));
602     uint32_t used_refs = 0, add_refs = 0;
603     uint64_t age;
604     int i, n, num_free_refs;
605
606     if (!free_refs)
607         return;
608
609     /* Detect changes of access unit */
610     if (fs_ctx->age == 0 || fs_ctx->prev_poc != poc)
611         fs_ctx->age++;
612     fs_ctx->prev_poc = poc;
613     age = fs_ctx->age;
614
615     /* Tag entries that are still available in our Frame Store */
616     for (i = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
617         struct object_surface * const obj_surface =
618                     decode_state->reference_objects[i];
619         if (!obj_surface)
620             continue;
621
622         GenCodecSurface * const codec_surface = obj_surface->private_data;
623         if (!codec_surface)
624             continue;
625         if (codec_surface->frame_store_id >= 0) {
626             GenFrameStore * const fs =
627                 &frame_store[codec_surface->frame_store_id];
628             if (fs->surface_id == obj_surface->base.id) {
629                 fs->obj_surface = obj_surface;
630                 fs->ref_age = age;
631                 used_refs |= 1 << fs->frame_store_id;
632                 continue;
633             }
634         }
635         add_refs |= 1 << i;
636     }
637
638     /* Build and sort out the list of retired candidates. The resulting
639        list is ordered by increasing age when they were last used */
640     for (i = 0, n = 0; i < num_elements; i++) {
641         if (!(used_refs & (1 << i))) {
642             GenFrameStore * const fs = &frame_store[i];
643             fs->obj_surface = NULL;
644             free_refs[n++] = fs;
645         }
646     }
647     num_free_refs = n;
648     qsort(&free_refs[0], n, sizeof(free_refs[0]), compare_avc_ref_store_func);
649
650     /* Append the new reference frames */
651     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
652         struct object_surface * const obj_surface =
653                     decode_state->reference_objects[i];
654         if (!obj_surface || !(add_refs & (1 << i)))
655             continue;
656
657         GenCodecSurface * const codec_surface = obj_surface->private_data;
658         if (!codec_surface)
659             continue;
660         if (n < num_free_refs) {
661             GenFrameStore * const fs = free_refs[n++];
662             fs->surface_id = obj_surface->base.id;
663             fs->obj_surface = obj_surface;
664             fs->frame_store_id = fs - frame_store;
665             fs->ref_age = age;
666             codec_surface->frame_store_id = fs->frame_store_id;
667             continue;
668         }
669         WARN_ONCE("No free slot found for DPB reference list!!!\n");
670     }
671
672     free(free_refs);
673 }
674
675 void
676 intel_update_avc_frame_store_index(
677     VADriverContextP              ctx,
678     struct decode_state          *decode_state,
679     VAPictureParameterBufferH264 *pic_param,
680     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES],
681     GenFrameStoreContext         *fs_ctx
682 )
683 {
684     intel_update_codec_frame_store_index(ctx,
685                                          decode_state,
686                                          avc_get_picture_poc(&pic_param->CurrPic),
687                                          frame_store,
688                                          MAX_GEN_REFERENCE_FRAMES,
689                                          fs_ctx);
690 }
691
692 void
693 intel_update_hevc_frame_store_index(
694     VADriverContextP              ctx,
695     struct decode_state          *decode_state,
696     VAPictureParameterBufferHEVC *pic_param,
697     GenFrameStore                 frame_store[MAX_GEN_HCP_REFERENCE_FRAMES],
698     GenFrameStoreContext         *fs_ctx
699 )
700 {
701     int i, n = 0;
702
703     for (i = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
704         struct object_surface * const obj_surface = decode_state->reference_objects[i];
705
706         if (!obj_surface)
707             continue;
708
709         GenFrameStore * const fs = &frame_store[n];
710         fs->surface_id = obj_surface->base.id;
711         fs->obj_surface = obj_surface;
712         fs->frame_store_id = n++;
713
714         if (n == MAX_GEN_HCP_REFERENCE_FRAMES)
715             break;
716     }
717
718     for (; n < MAX_GEN_HCP_REFERENCE_FRAMES; n++) {
719         GenFrameStore * const fs = &frame_store[n];
720
721         fs->surface_id = VA_INVALID_ID;
722         fs->obj_surface = NULL;
723         fs->frame_store_id = -1;
724     }
725 }
726
727 void
728 gen75_update_avc_frame_store_index(
729     VADriverContextP              ctx,
730     struct decode_state          *decode_state,
731     VAPictureParameterBufferH264 *pic_param,
732     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES]
733 )
734 {
735     int i, n;
736
737     /* Construct the Frame Store array, in compact form. i.e. empty or
738        invalid entries are discarded. */
739     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
740         struct object_surface * const obj_surface =
741                     decode_state->reference_objects[i];
742         if (!obj_surface)
743             continue;
744
745         GenFrameStore * const fs = &frame_store[n];
746         fs->surface_id = obj_surface->base.id;
747         fs->obj_surface = obj_surface;
748         fs->frame_store_id = n++;
749     }
750
751     /* Any remaining entry is marked as invalid */
752     for (; n < MAX_GEN_REFERENCE_FRAMES; n++) {
753         GenFrameStore * const fs = &frame_store[n];
754         fs->surface_id = VA_INVALID_ID;
755         fs->obj_surface = NULL;
756         fs->frame_store_id = -1;
757     }
758 }
759
760 bool
761 gen75_fill_avc_picid_list(
762     uint16_t                    pic_ids[16],
763     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
764 )
765 {
766     int i, pic_id;
767
768     /* Fill in with known picture IDs. The Frame Store array is in
769        compact form, i.e. empty entries are only to be found at the
770        end of the array: there are no holes in the set of active
771        reference frames */
772     for (i = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
773         GenFrameStore * const fs = &frame_store[i];
774         if (!fs->obj_surface)
775             break;
776         pic_id = avc_get_picture_id(fs->obj_surface);
777         if (pic_id < 0)
778             return false;
779         pic_ids[i] = pic_id;
780     }
781
782     /* When an element of the list is not relevant the value of the
783        picture ID shall be set to 0 */
784     for (; i < MAX_GEN_REFERENCE_FRAMES; i++)
785         pic_ids[i] = 0;
786     return true;
787 }
788
789 bool
790 gen75_send_avc_picid_state(
791     struct intel_batchbuffer   *batch,
792     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
793 )
794 {
795     uint16_t pic_ids[16];
796
797     if (!gen75_fill_avc_picid_list(pic_ids, frame_store))
798         return false;
799
800     BEGIN_BCS_BATCH(batch, 10);
801     OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
802     OUT_BCS_BATCH(batch, 0); // enable Picture ID Remapping
803     intel_batchbuffer_data(batch, pic_ids, sizeof(pic_ids));
804     ADVANCE_BCS_BATCH(batch);
805     return true;
806 }
807
808 void
809 intel_update_vc1_frame_store_index(VADriverContextP ctx,
810                                    struct decode_state *decode_state,
811                                    VAPictureParameterBufferVC1 *pic_param,
812                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
813 {
814     struct object_surface *obj_surface;
815     int i;
816
817     obj_surface = decode_state->reference_objects[0];
818
819     if (pic_param->forward_reference_picture == VA_INVALID_ID ||
820         !obj_surface ||
821         !obj_surface->bo) {
822         frame_store[0].surface_id = VA_INVALID_ID;
823         frame_store[0].obj_surface = NULL;
824         frame_store[2].surface_id = VA_INVALID_ID;
825         frame_store[2].obj_surface = NULL;
826     } else {
827         frame_store[0].surface_id = pic_param->forward_reference_picture;
828         frame_store[0].obj_surface = obj_surface;
829         frame_store[2].surface_id = pic_param->forward_reference_picture;
830         frame_store[2].obj_surface = obj_surface;
831     }
832
833     if (pic_param->sequence_fields.bits.interlace &&
834         pic_param->picture_fields.bits.frame_coding_mode == 2 && /* Field-Interlace */
835         !pic_param->picture_fields.bits.is_first_field) {
836         if (pic_param->picture_fields.bits.top_field_first) {
837             frame_store[0].surface_id = decode_state->current_render_target;
838             frame_store[0].obj_surface = decode_state->render_object;
839         } else {
840             frame_store[2].surface_id = decode_state->current_render_target;
841             frame_store[2].obj_surface = decode_state->render_object;
842         }
843     }
844
845     obj_surface= decode_state->reference_objects[1];
846
847     if (pic_param->backward_reference_picture == VA_INVALID_ID ||
848         !obj_surface ||
849         !obj_surface->bo) {
850         frame_store[1].surface_id = frame_store[0].surface_id;
851         frame_store[1].obj_surface = frame_store[0].obj_surface;
852         frame_store[3].surface_id = frame_store[2].surface_id;
853         frame_store[3].obj_surface = frame_store[2].obj_surface;
854     } else {
855         frame_store[1].surface_id = pic_param->backward_reference_picture;
856         frame_store[1].obj_surface = obj_surface;
857         frame_store[3].surface_id = pic_param->backward_reference_picture;
858         frame_store[3].obj_surface = obj_surface;
859     }
860
861     for (i = 4; i < MAX_GEN_REFERENCE_FRAMES; i++) {
862         frame_store[i].surface_id = frame_store[i % 4].surface_id;
863         frame_store[i].obj_surface = frame_store[i % 4].obj_surface;
864     }
865 }
866
867 void
868 intel_update_vp8_frame_store_index(VADriverContextP ctx,
869                                    struct decode_state *decode_state,
870                                    VAPictureParameterBufferVP8 *pic_param,
871                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
872 {
873     struct object_surface *obj_surface;
874     int i;
875
876     obj_surface = decode_state->reference_objects[0];
877
878     if (pic_param->last_ref_frame == VA_INVALID_ID ||
879         !obj_surface ||
880         !obj_surface->bo) {
881         frame_store[0].surface_id = VA_INVALID_ID;
882         frame_store[0].obj_surface = NULL;
883     } else {
884         frame_store[0].surface_id = pic_param->last_ref_frame;
885         frame_store[0].obj_surface = obj_surface;
886     }
887
888     obj_surface = decode_state->reference_objects[1];
889
890     if (pic_param->golden_ref_frame == VA_INVALID_ID ||
891         !obj_surface ||
892         !obj_surface->bo) {
893         frame_store[1].surface_id = frame_store[0].surface_id;
894         frame_store[1].obj_surface = frame_store[0].obj_surface;
895     } else {
896         frame_store[1].surface_id = pic_param->golden_ref_frame;
897         frame_store[1].obj_surface = obj_surface;
898     }
899
900     obj_surface = decode_state->reference_objects[2];
901
902     if (pic_param->alt_ref_frame == VA_INVALID_ID ||
903         !obj_surface ||
904         !obj_surface->bo) {
905         frame_store[2].surface_id = frame_store[0].surface_id;
906         frame_store[2].obj_surface = frame_store[0].obj_surface;
907     } else {
908         frame_store[2].surface_id = pic_param->alt_ref_frame;
909         frame_store[2].obj_surface = obj_surface;
910     }
911
912     for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
913         frame_store[i].surface_id = frame_store[i % 2].surface_id;
914         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
915     }
916
917 }
918
919 //Obtain the reference frames from the decode state and store them in frame store.
920 void
921 intel_update_vp9_frame_store_index(VADriverContextP ctx,
922                                    struct decode_state *decode_state,
923                                    VADecPictureParameterBufferVP9 *pic_param,
924                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
925 {
926     struct object_surface *obj_surface;
927     int i = 0, index = 0;
928
929     //Check for the validity of the last reference frame
930     obj_surface = decode_state->reference_objects[0];
931
932     index = pic_param->pic_fields.bits.last_ref_frame;
933     if (pic_param->reference_frames[index] == VA_INVALID_ID ||
934         !obj_surface ||
935         !obj_surface->bo) {
936         frame_store[0].surface_id = VA_INVALID_ID;
937         frame_store[0].obj_surface = NULL;
938     } else {
939         frame_store[0].surface_id = pic_param->reference_frames[index];
940         frame_store[0].obj_surface = obj_surface;
941     }
942
943     //Check for the validity of the golden reference frame
944     obj_surface = decode_state->reference_objects[1];
945
946     index = pic_param->pic_fields.bits.golden_ref_frame;
947     if (pic_param->reference_frames[index] == VA_INVALID_ID ||
948         !obj_surface ||
949         !obj_surface->bo) {
950         frame_store[1].surface_id = frame_store[0].surface_id;
951         frame_store[1].obj_surface = frame_store[0].obj_surface;
952     } else {
953         frame_store[1].surface_id = pic_param->reference_frames[index];
954         frame_store[1].obj_surface = obj_surface;
955     }
956
957     //Check for the validity of the altref reference frame
958     obj_surface = decode_state->reference_objects[2];
959
960     index = pic_param->pic_fields.bits.alt_ref_frame;
961     if (pic_param->reference_frames[index] == VA_INVALID_ID ||
962         !obj_surface ||
963         !obj_surface->bo) {
964         frame_store[2].surface_id = frame_store[0].surface_id;
965         frame_store[2].obj_surface = frame_store[0].obj_surface;
966     } else {
967         frame_store[2].surface_id = pic_param->reference_frames[index];
968         frame_store[2].obj_surface = obj_surface;
969     }
970
971     //Set the remaining framestores to either last/golden/altref
972     for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
973         frame_store[i].surface_id = frame_store[i % 2].surface_id;
974         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
975     }
976
977 }
978
979 static VAStatus
980 intel_decoder_check_avc_parameter(VADriverContextP ctx,
981                                   VAProfile h264_profile,
982                                   struct decode_state *decode_state)
983 {
984     struct i965_driver_data *i965 = i965_driver_data(ctx);
985     VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
986     VAStatus va_status;
987     struct object_surface *obj_surface;
988     int i;
989     VASliceParameterBufferH264 *slice_param, *next_slice_param, *next_slice_group_param;
990     int j;
991
992     ASSERT_RET(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID), VA_STATUS_ERROR_INVALID_PARAMETER);
993     ASSERT_RET((pic_param->CurrPic.picture_id != VA_INVALID_SURFACE), VA_STATUS_ERROR_INVALID_PARAMETER);
994     ASSERT_RET((pic_param->CurrPic.picture_id == decode_state->current_render_target), VA_STATUS_ERROR_INVALID_PARAMETER);
995
996     if (pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
997         WARN_ONCE("Unsupported the ASO constraints!!!\n");
998         goto error;
999     }
1000
1001     /* Fill in the reference objects array with the actual VA surface
1002        objects with 1:1 correspondance with any entry in ReferenceFrames[],
1003        i.e. including "holes" for invalid entries, that are expanded
1004        to NULL in the reference_objects[] array */
1005     for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
1006         const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
1007
1008         obj_surface = NULL;
1009         if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
1010             va_pic->picture_id != VA_INVALID_ID) {
1011             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
1012             if (!obj_surface)
1013                 return VA_STATUS_ERROR_INVALID_SURFACE;
1014
1015             /*
1016              * Sometimes a dummy frame comes from the upper layer
1017              * library, call i965_check_alloc_surface_bo() to make
1018              * sure the store buffer is allocated for this reference
1019              * frame
1020              */
1021             va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
1022                                               pic_param);
1023             if (va_status != VA_STATUS_SUCCESS)
1024                 return va_status;
1025         }
1026         decode_state->reference_objects[i] = obj_surface;
1027     }
1028
1029     for (j = 0; j < decode_state->num_slice_params; j++) {
1030         ASSERT_RET((decode_state->slice_params && decode_state->slice_params[j]->buffer), VA_STATUS_ERROR_INVALID_PARAMETER);
1031         slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
1032
1033         if (j == decode_state->num_slice_params - 1)
1034             next_slice_group_param = NULL;
1035         else
1036             next_slice_group_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j + 1]->buffer;
1037
1038         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
1039
1040             if (i < decode_state->slice_params[j]->num_elements - 1)
1041                 next_slice_param = slice_param + 1;
1042             else
1043                 next_slice_param = next_slice_group_param;
1044
1045             if (next_slice_param != NULL) {
1046                 /* If the mb position of next_slice is less than or equal to the current slice,
1047                  * discard the current frame.
1048                  */
1049                 if (next_slice_param->first_mb_in_slice <= slice_param->first_mb_in_slice) {
1050                     next_slice_param = NULL;
1051                     WARN_ONCE("!!!incorrect slice_param. The first_mb_in_slice of next_slice is less"
1052                               " than or equal to that in current slice\n");
1053                     goto error;
1054                 }
1055             }
1056         }
1057     }
1058
1059     return VA_STATUS_SUCCESS;
1060
1061 error:
1062     return VA_STATUS_ERROR_INVALID_PARAMETER;
1063 }
1064
1065 static VAStatus
1066 intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
1067                                     struct decode_state *decode_state)
1068 {
1069     struct i965_driver_data *i965 = i965_driver_data(ctx);
1070     VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
1071     struct object_surface *obj_surface;
1072     int i = 0;
1073
1074     if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
1075     } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
1076         obj_surface = SURFACE(pic_param->forward_reference_picture);
1077
1078         if (!obj_surface || !obj_surface->bo)
1079             decode_state->reference_objects[i++] = NULL;
1080         else
1081             decode_state->reference_objects[i++] = obj_surface;
1082     } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
1083         obj_surface = SURFACE(pic_param->forward_reference_picture);
1084
1085         if (!obj_surface || !obj_surface->bo)
1086             decode_state->reference_objects[i++] = NULL;
1087         else
1088             decode_state->reference_objects[i++] = obj_surface;
1089
1090         obj_surface = SURFACE(pic_param->backward_reference_picture);
1091
1092         if (!obj_surface || !obj_surface->bo)
1093             decode_state->reference_objects[i++] = NULL;
1094         else
1095             decode_state->reference_objects[i++] = obj_surface;
1096     } else
1097         goto error;
1098
1099     for (; i < 16; i++)
1100         decode_state->reference_objects[i] = NULL;
1101
1102     return VA_STATUS_SUCCESS;
1103
1104 error:
1105     return VA_STATUS_ERROR_INVALID_PARAMETER;
1106 }
1107
1108 static VAStatus
1109 intel_decoder_check_vc1_parameter(VADriverContextP ctx,
1110                                   struct decode_state *decode_state)
1111 {
1112     struct i965_driver_data *i965 = i965_driver_data(ctx);
1113     VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
1114     struct object_surface *obj_surface;
1115     int picture_type;
1116     int is_first_field = 1;
1117     int i = 0;
1118
1119     if (!pic_param->sequence_fields.bits.interlace ||
1120         (pic_param->picture_fields.bits.frame_coding_mode < 2)) { /* Progressive or Frame-Interlace */
1121         picture_type = pic_param->picture_fields.bits.picture_type;
1122     } else {/* Field-Interlace */
1123         is_first_field = pic_param->picture_fields.bits.is_first_field;
1124         picture_type = fptype_to_picture_type[pic_param->picture_fields.bits.picture_type][!is_first_field];
1125     }
1126
1127     if (picture_type == VC1_I_PICTURE ||
1128         picture_type == VC1_BI_PICTURE) {
1129     } else if (picture_type == VC1_P_PICTURE ||
1130                picture_type == VC1_SKIPPED_PICTURE) {
1131         obj_surface = SURFACE(pic_param->forward_reference_picture);
1132
1133         if (!obj_surface || !obj_surface->bo)
1134             decode_state->reference_objects[i++] = NULL;
1135         else
1136             decode_state->reference_objects[i++] = obj_surface;
1137     } else if (picture_type == VC1_B_PICTURE) {
1138         obj_surface = SURFACE(pic_param->forward_reference_picture);
1139
1140         if (!obj_surface || !obj_surface->bo)
1141             decode_state->reference_objects[i++] = NULL;
1142         else
1143             decode_state->reference_objects[i++] = obj_surface;
1144
1145         obj_surface = SURFACE(pic_param->backward_reference_picture);
1146
1147         if (!obj_surface || !obj_surface->bo)
1148             decode_state->reference_objects[i++] = NULL;
1149         else
1150             decode_state->reference_objects[i++] = obj_surface;
1151     } else
1152         goto error;
1153
1154     for (; i < 16; i++)
1155         decode_state->reference_objects[i] = NULL;
1156
1157     return VA_STATUS_SUCCESS;
1158
1159 error:
1160     return VA_STATUS_ERROR_INVALID_PARAMETER;
1161 }
1162
1163 static VAStatus
1164 intel_decoder_check_vp8_parameter(VADriverContextP ctx,
1165                                   struct decode_state *decode_state)
1166 {
1167     struct i965_driver_data *i965 = i965_driver_data(ctx);
1168     VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
1169     struct object_surface *obj_surface;
1170     int i = 0;
1171
1172     if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
1173         obj_surface = SURFACE(pic_param->last_ref_frame);
1174
1175         if (obj_surface && obj_surface->bo)
1176             decode_state->reference_objects[i++] = obj_surface;
1177         else
1178             decode_state->reference_objects[i++] = NULL;
1179     }
1180
1181     if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
1182         obj_surface = SURFACE(pic_param->golden_ref_frame);
1183
1184         if (obj_surface && obj_surface->bo)
1185             decode_state->reference_objects[i++] = obj_surface;
1186         else
1187             decode_state->reference_objects[i++] = NULL;
1188     }
1189
1190     if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
1191         obj_surface = SURFACE(pic_param->alt_ref_frame);
1192
1193         if (obj_surface && obj_surface->bo)
1194             decode_state->reference_objects[i++] = obj_surface;
1195         else
1196             decode_state->reference_objects[i++] = NULL;
1197     }
1198
1199     for (; i < 16; i++)
1200         decode_state->reference_objects[i] = NULL;
1201
1202     return VA_STATUS_SUCCESS;
1203 }
1204
1205 VAStatus
1206 hevc_ensure_surface_bo(
1207     VADriverContextP                    ctx,
1208     struct decode_state                *decode_state,
1209     struct object_surface              *obj_surface,
1210     const VAPictureParameterBufferHEVC *pic_param
1211 )
1212 {
1213     VAStatus va_status = VA_STATUS_SUCCESS;
1214     int update = 0;
1215     unsigned int fourcc = VA_FOURCC_NV12;
1216
1217     if ((pic_param->bit_depth_luma_minus8 > 0)
1218         || (pic_param->bit_depth_chroma_minus8 > 0)) {
1219         if (obj_surface->fourcc != VA_FOURCC_P010) {
1220             update = 1;
1221             fourcc = VA_FOURCC_P010;
1222         }
1223     } else if (obj_surface->fourcc != VA_FOURCC_NV12) {
1224         update = 1;
1225         fourcc = VA_FOURCC_NV12;
1226     }
1227
1228     /* (Re-)allocate the underlying surface buffer store, if necessary */
1229     if (!obj_surface->bo || update) {
1230         struct i965_driver_data * const i965 = i965_driver_data(ctx);
1231
1232         i965_destroy_surface_storage(obj_surface);
1233
1234         va_status = i965_check_alloc_surface_bo(ctx,
1235                                                 obj_surface,
1236                                                 i965->codec_info->has_tiled_surface,
1237                                                 fourcc,
1238                                                 SUBSAMPLE_YUV420);
1239     }
1240
1241     return va_status;
1242 }
1243
1244 //Ensure there is a tiled render surface in NV12 format. If not, create one.
1245 VAStatus
1246 vp9_ensure_surface_bo(
1247     VADriverContextP                    ctx,
1248     struct decode_state                *decode_state,
1249     struct object_surface              *obj_surface,
1250     const VADecPictureParameterBufferVP9 *pic_param
1251 )
1252 {
1253     VAStatus va_status = VA_STATUS_SUCCESS;
1254     int update = 0;
1255     unsigned int fourcc = VA_FOURCC_NV12;
1256
1257     if (pic_param->profile >= 2) {
1258         if (obj_surface->fourcc != VA_FOURCC_P010) {
1259             update = 1;
1260             fourcc = VA_FOURCC_P010;
1261         }
1262     } else if (obj_surface->fourcc != VA_FOURCC_NV12) {
1263         update = 1;
1264         fourcc = VA_FOURCC_NV12;
1265     }
1266
1267     /* (Re-)allocate the underlying surface buffer store, if necessary */
1268     if (!obj_surface->bo || update) {
1269         struct i965_driver_data * const i965 = i965_driver_data(ctx);
1270
1271         i965_destroy_surface_storage(obj_surface);
1272
1273         va_status = i965_check_alloc_surface_bo(ctx,
1274                                                 obj_surface,
1275                                                 i965->codec_info->has_tiled_surface,
1276                                                 fourcc,
1277                                                 SUBSAMPLE_YUV420);
1278     }
1279
1280     return va_status;
1281 }
1282
1283 static VAStatus
1284 intel_decoder_check_hevc_parameter(VADriverContextP ctx,
1285                                    struct decode_state *decode_state)
1286 {
1287     struct i965_driver_data *i965 = i965_driver_data(ctx);
1288     VAPictureParameterBufferHEVC *pic_param = (VAPictureParameterBufferHEVC *)decode_state->pic_param->buffer;
1289     VAStatus va_status = VA_STATUS_ERROR_INVALID_PARAMETER;
1290     struct object_surface *obj_surface;
1291     int i;
1292     int min_cb_size;
1293
1294     if (pic_param->CurrPic.flags & VA_PICTURE_HEVC_INVALID ||
1295         pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
1296         goto error;
1297
1298     if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
1299         goto error;
1300
1301     min_cb_size = (1 << (pic_param->log2_min_luma_coding_block_size_minus3 + 3));
1302
1303     if (pic_param->pic_width_in_luma_samples % min_cb_size ||
1304         pic_param->pic_height_in_luma_samples % min_cb_size)
1305         goto error;
1306
1307     /* Fill in the reference objects array with the actual VA surface
1308        objects with 1:1 correspondance with any entry in ReferenceFrames[],
1309        i.e. including "holes" for invalid entries, that are expanded
1310        to NULL in the reference_objects[] array */
1311     for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
1312         const VAPictureHEVC * const va_pic = &pic_param->ReferenceFrames[i];
1313
1314         obj_surface = NULL;
1315
1316         /*
1317          * Only the index with (VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE |
1318          * VA_PICTURE_HEVC_RPS_ST_CURR_AFTER | VA_PICTURE_HEVC_RPS_LT_CURR)
1319          * is valid
1320          */
1321         if (!(va_pic->flags & VA_PICTURE_HEVC_INVALID) &&
1322             (va_pic->picture_id != VA_INVALID_ID) &&
1323             (va_pic->flags & (VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE |
1324                               VA_PICTURE_HEVC_RPS_ST_CURR_AFTER |
1325                               VA_PICTURE_HEVC_RPS_LT_CURR))) {
1326
1327             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
1328
1329             if (!obj_surface) {
1330                 va_status = VA_STATUS_ERROR_INVALID_SURFACE;
1331                 goto error;
1332             }
1333
1334             va_status = hevc_ensure_surface_bo(ctx, decode_state, obj_surface,
1335                                                pic_param);
1336
1337             if (va_status != VA_STATUS_SUCCESS)
1338                 goto error;
1339         }
1340
1341         decode_state->reference_objects[i] = obj_surface;
1342     }
1343
1344     va_status = VA_STATUS_SUCCESS;
1345
1346 error:
1347     return va_status;
1348 }
1349
1350 //Obtains reference frames from the picture parameter and
1351 //then sets the reference frames in the decode_state
1352 static VAStatus
1353 intel_decoder_check_vp9_parameter(VADriverContextP ctx,
1354                                   VAProfile profile,
1355                                   struct decode_state *decode_state)
1356 {
1357     struct i965_driver_data *i965 = i965_driver_data(ctx);
1358     VADecPictureParameterBufferVP9 *pic_param = (VADecPictureParameterBufferVP9 *)decode_state->pic_param->buffer;
1359     VAStatus va_status = VA_STATUS_ERROR_INVALID_PARAMETER;
1360     struct object_surface *obj_surface;
1361     int i = 0, index = 0;
1362
1363     if ((profile - VAProfileVP9Profile0) < pic_param->profile)
1364         return va_status;
1365
1366     //Max support upto 4k for BXT
1367     if ((pic_param->frame_width - 1 < 0) || (pic_param->frame_width - 1 > 4095))
1368         return va_status;
1369
1370     if ((pic_param->frame_height - 1 < 0) || (pic_param->frame_height - 1 > 4095))
1371         return va_status;
1372
1373     //Set the reference object in decode state for last reference
1374     index = pic_param->pic_fields.bits.last_ref_frame;
1375     if (pic_param->reference_frames[index] != VA_INVALID_SURFACE) {
1376         obj_surface = SURFACE(pic_param->reference_frames[index]);
1377
1378         if (obj_surface && obj_surface->bo)
1379             decode_state->reference_objects[i++] = obj_surface;
1380         else
1381             decode_state->reference_objects[i++] = NULL;
1382     }
1383
1384     //Set the reference object in decode state for golden reference
1385     index = pic_param->pic_fields.bits.golden_ref_frame;
1386     if (pic_param->reference_frames[index] != VA_INVALID_SURFACE) {
1387         obj_surface = SURFACE(pic_param->reference_frames[index]);
1388
1389         if (obj_surface && obj_surface->bo)
1390             decode_state->reference_objects[i++] = obj_surface;
1391         else
1392             decode_state->reference_objects[i++] = NULL;
1393     }
1394
1395     //Set the reference object in decode state for altref reference
1396     index = pic_param->pic_fields.bits.alt_ref_frame;
1397     if (pic_param->reference_frames[index] != VA_INVALID_SURFACE) {
1398         obj_surface = SURFACE(pic_param->reference_frames[index]);
1399
1400         if (obj_surface && obj_surface->bo)
1401             decode_state->reference_objects[i++] = obj_surface;
1402         else
1403             decode_state->reference_objects[i++] = NULL;
1404     }
1405
1406     for (; i < 16; i++)
1407         decode_state->reference_objects[i] = NULL;
1408
1409     return VA_STATUS_SUCCESS;
1410 }
1411
1412 VAStatus
1413 intel_decoder_sanity_check_input(VADriverContextP ctx,
1414                                  VAProfile profile,
1415                                  struct decode_state *decode_state)
1416 {
1417     struct i965_driver_data *i965 = i965_driver_data(ctx);
1418     struct object_surface *obj_surface;
1419     VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
1420
1421     if (decode_state->current_render_target == VA_INVALID_SURFACE)
1422         goto out;
1423
1424     obj_surface = SURFACE(decode_state->current_render_target);
1425
1426     if (!obj_surface)
1427         goto out;
1428
1429     decode_state->render_object = obj_surface;
1430
1431     switch (profile) {
1432     case VAProfileMPEG2Simple:
1433     case VAProfileMPEG2Main:
1434         vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
1435         break;
1436
1437     case VAProfileH264ConstrainedBaseline:
1438     case VAProfileH264Main:
1439     case VAProfileH264High:
1440     case VAProfileH264StereoHigh:
1441     case VAProfileH264MultiviewHigh:
1442         vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
1443         break;
1444
1445     case VAProfileVC1Simple:
1446     case VAProfileVC1Main:
1447     case VAProfileVC1Advanced:
1448         vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
1449         break;
1450
1451     case VAProfileJPEGBaseline:
1452         vaStatus = VA_STATUS_SUCCESS;
1453         break;
1454
1455     case VAProfileVP8Version0_3:
1456         vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
1457         break;
1458
1459     case VAProfileHEVCMain:
1460     case VAProfileHEVCMain10:
1461         vaStatus = intel_decoder_check_hevc_parameter(ctx, decode_state);
1462         break;
1463
1464     case VAProfileVP9Profile0:
1465     case VAProfileVP9Profile2:
1466         vaStatus = intel_decoder_check_vp9_parameter(ctx, profile, decode_state);
1467         break;
1468
1469     default:
1470         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
1471         break;
1472     }
1473
1474 out:
1475     return vaStatus;
1476 }
1477
1478 /*
1479  * Return the next slice paramter
1480  *
1481  * Input:
1482  *      slice_param: the current slice
1483  *      *group_idx & *element_idx the current slice position in slice groups
1484  * Output:
1485  *      Return the next slice parameter
1486  *      *group_idx & *element_idx the next slice position in slice groups,
1487  *      if the next slice is NULL, *group_idx & *element_idx will be ignored
1488  */
1489 VASliceParameterBufferMPEG2 *
1490 intel_mpeg2_find_next_slice(struct decode_state *decode_state,
1491                             VAPictureParameterBufferMPEG2 *pic_param,
1492                             VASliceParameterBufferMPEG2 *slice_param,
1493                             int *group_idx,
1494                             int *element_idx)
1495 {
1496     VASliceParameterBufferMPEG2 *next_slice_param;
1497     unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
1498     int j = *group_idx, i = *element_idx + 1;
1499
1500     for (; j < decode_state->num_slice_params; j++) {
1501         for (; i < decode_state->slice_params[j]->num_elements; i++) {
1502             next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
1503
1504             if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
1505                 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
1506                 *group_idx = j;
1507                 *element_idx = i;
1508
1509                 return next_slice_param;
1510             }
1511         }
1512
1513         i = 0;
1514     }
1515
1516     return NULL;
1517 }
1518
1519 /* Ensure the segmentation buffer is large enough for the supplied
1520    number of MBs, or re-allocate it */
1521 bool
1522 intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
1523                                      unsigned int mb_width, unsigned int mb_height)
1524 {
1525     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1526     /* The segmentation map is a 64-byte aligned linear buffer, with
1527        each cache line holding only 8 bits for 4 continuous MBs */
1528     const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
1529
1530     if (buf->valid) {
1531         if (buf->bo && buf->bo->size >= buf_size)
1532             return true;
1533         drm_intel_bo_unreference(buf->bo);
1534         buf->valid = false;
1535     }
1536
1537     buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
1538                                  buf_size, 0x1000);
1539     buf->valid = buf->bo != NULL;
1540     return buf->valid;
1541 }
1542
1543 void
1544 hevc_gen_default_iq_matrix(VAIQMatrixBufferHEVC *iq_matrix)
1545 {
1546     /* Flat_4x4_16 */
1547     memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
1548
1549     /* Flat_8x8_16 */
1550     memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
1551
1552     /* Flat_16x16_16 */
1553     memset(&iq_matrix->ScalingList16x16, 16, sizeof(iq_matrix->ScalingList16x16));
1554
1555     /* Flat_32x32_16 */
1556     memset(&iq_matrix->ScalingList32x32, 16, sizeof(iq_matrix->ScalingList32x32));
1557
1558     /* Flat_16x16_dc_16 */
1559     memset(&iq_matrix->ScalingListDC16x16, 16, sizeof(iq_matrix->ScalingListDC16x16));
1560
1561     /* Flat_32x32_dc_16 */
1562     memset(&iq_matrix->ScalingListDC32x32, 16, sizeof(iq_matrix->ScalingListDC32x32));
1563 }