decoder: h264: improve AVC_REF_IDX_STATE for MVC.
[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 /* Returns a unique picture ID that represents the supplied VA surface object */
258 int
259 avc_get_picture_id(struct object_surface *obj_surface)
260 {
261     int pic_id;
262
263     /* This highly depends on how the internal VA objects are organized.
264
265        Theory of operations:
266        The VA objects are maintained in heaps so that any released VA
267        surface will become free again for future allocation. This means
268        that holes in there are filled in for subsequent allocations.
269        So, this ultimately means that we could just use the Heap ID of
270        the VA surface as the resulting picture ID (16 bits) */
271     pic_id = 1 + (obj_surface->base.id & OBJECT_HEAP_ID_MASK);
272     return (pic_id <= 0xffff) ? pic_id : -1;
273 }
274
275 /* Finds the VA/H264 picture associated with the specified VA surface id */
276 VAPictureH264 *
277 avc_find_picture(VASurfaceID id, VAPictureH264 *pic_list, int pic_list_count)
278 {
279     int i;
280
281     if (id != VA_INVALID_ID) {
282         for (i = 0; i < pic_list_count; i++) {
283             VAPictureH264 * const va_pic = &pic_list[i];
284             if (va_pic->picture_id == id &&
285                 !(va_pic->flags & VA_PICTURE_H264_INVALID))
286                 return va_pic;
287         }
288     }
289     return NULL;
290 }
291
292 /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
293 /* XXX: slice_data_bit_offset does not account for EPB */
294 unsigned int
295 avc_get_first_mb_bit_offset(
296     dri_bo                     *slice_data_bo,
297     VASliceParameterBufferH264 *slice_param,
298     unsigned int                mode_flag
299 )
300 {
301     unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
302
303     if (mode_flag == ENTROPY_CABAC)
304         slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
305     return slice_data_bit_offset;
306 }
307
308 /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
309 /* XXX: slice_data_bit_offset does not account for EPB */
310 unsigned int
311 avc_get_first_mb_bit_offset_with_epb(
312     dri_bo                     *slice_data_bo,
313     VASliceParameterBufferH264 *slice_param,
314     unsigned int                mode_flag
315 )
316 {
317     unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
318     unsigned int out_slice_data_bit_offset;
319     unsigned int i, j, n, buf_size, data_size, header_size;
320     uint8_t *buf;
321     int ret;
322
323     header_size = slice_param->slice_data_bit_offset / 8;
324     data_size   = slice_param->slice_data_size - slice_param->slice_data_offset;
325     buf_size    = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
326
327     if (buf_size > data_size)
328         buf_size = data_size;
329
330     buf = alloca(buf_size);
331     ret = dri_bo_get_subdata(
332         slice_data_bo, slice_param->slice_data_offset,
333         buf_size, buf
334     );
335     assert(ret == 0);
336
337     for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
338         if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
339             i += 2, j++, n++;
340     }
341
342     out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
343
344     if (mode_flag == ENTROPY_CABAC)
345         out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
346     return out_slice_data_bit_offset;
347 }
348
349 static inline uint8_t
350 get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
351 {
352     /* The H.264 standard, and the VA-API specification, allows for at
353        least 3 states for a picture: "used for short-term reference",
354        "used for long-term reference", or considered as not used for
355        reference.
356
357        The latter is used in the MVC inter prediction and inter-view
358        prediction process (H.8.4). This has an incidence on the
359        colZeroFlag variable, as defined in 8.4.1.2.
360
361        Since it is not possible to directly program that flag, let's
362        make the hardware derive this value by assimilating "considered
363        as not used for reference" to a "not used for short-term
364        reference", and subsequently making it "used for long-term
365        reference" to fit the definition of Bit6 here */
366     const unsigned int ref_flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE |
367         VA_PICTURE_H264_LONG_TERM_REFERENCE;
368     const unsigned int is_long_term =
369         ((va_pic->flags & ref_flags) != VA_PICTURE_H264_SHORT_TERM_REFERENCE);
370     const unsigned int is_top_field =
371         !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
372     const unsigned int is_bottom_field =
373         !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
374
375     return ((is_long_term                         << 6) |
376             ((is_top_field ^ is_bottom_field ^ 1) << 5) |
377             (frame_store_id                       << 1) |
378             ((is_top_field ^ 1) & is_bottom_field));
379 }
380
381 /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
382 void
383 gen5_fill_avc_ref_idx_state(
384     uint8_t             state[32],
385     const VAPictureH264 ref_list[32],
386     unsigned int        ref_list_count,
387     const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
388 )
389 {
390     int i, j;
391
392     for (i = 0; i < ref_list_count; i++) {
393         const VAPictureH264 * const va_pic = &ref_list[i];
394
395         if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
396             va_pic->picture_id == VA_INVALID_ID) {
397             state[i] = 0xff;
398             continue;
399         }
400
401         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
402             if (frame_store[j].surface_id == va_pic->picture_id)
403                 break;
404         }
405
406         if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
407             const GenFrameStore * const fs = &frame_store[j];
408             assert(fs->frame_store_id == j); // Current architecture/assumption
409             state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
410         }
411         else {
412             WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
413             state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
414         }
415     }
416
417     for (; i < 32; i++)
418         state[i] = 0xff;
419 }
420
421 /* Emit Reference List Entries (Gen6+: SNB, IVB) */
422 static void
423 gen6_send_avc_ref_idx_state_1(
424     struct intel_batchbuffer         *batch,
425     unsigned int                      list,
426     const VAPictureH264              *ref_list,
427     unsigned int                      ref_list_count,
428     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
429 )
430 {
431     uint8_t ref_idx_state[32];
432
433     BEGIN_BCS_BATCH(batch, 10);
434     OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
435     OUT_BCS_BATCH(batch, list);
436     gen5_fill_avc_ref_idx_state(
437         ref_idx_state,
438         ref_list, ref_list_count,
439         frame_store
440     );
441     intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
442     ADVANCE_BCS_BATCH(batch);
443 }
444
445 void
446 gen6_send_avc_ref_idx_state(
447     struct intel_batchbuffer         *batch,
448     const VASliceParameterBufferH264 *slice_param,
449     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
450 )
451 {
452     if (slice_param->slice_type == SLICE_TYPE_I ||
453         slice_param->slice_type == SLICE_TYPE_SI)
454         return;
455
456     /* RefPicList0 */
457     gen6_send_avc_ref_idx_state_1(
458         batch, 0,
459         slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
460         frame_store
461     );
462
463     if (slice_param->slice_type != SLICE_TYPE_B)
464         return;
465
466     /* RefPicList1 */
467     gen6_send_avc_ref_idx_state_1(
468         batch, 1,
469         slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
470         frame_store
471     );
472 }
473
474 void
475 intel_update_avc_frame_store_index(
476     VADriverContextP              ctx,
477     struct decode_state          *decode_state,
478     VAPictureParameterBufferH264 *pic_param,
479     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES]
480 )
481 {
482     GenFrameStore *free_refs[MAX_GEN_REFERENCE_FRAMES];
483     int i, j, n, num_free_refs;
484
485     /* Remove obsolete entries from the internal DPB */
486     for (i = 0, n = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
487         GenFrameStore * const fs = &frame_store[i];
488         if (fs->surface_id == VA_INVALID_ID || !fs->obj_surface) {
489             free_refs[n++] = fs;
490             continue;
491         }
492
493         // Find whether the current entry is still a valid reference frame
494         for (j = 0; j < ARRAY_ELEMS(decode_state->reference_objects); j++) {
495             struct object_surface * const obj_surface =
496                 decode_state->reference_objects[j];
497             if (obj_surface && obj_surface == fs->obj_surface)
498                 break;
499         }
500
501         // ... or remove it
502         if (j == ARRAY_ELEMS(decode_state->reference_objects)) {
503             fs->surface_id = VA_INVALID_ID;
504             fs->obj_surface = NULL;
505             fs->frame_store_id = -1;
506             free_refs[n++] = fs;
507         }
508     }
509     num_free_refs = n;
510
511     /* Append the new reference frames */
512     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
513         struct object_surface * const obj_surface =
514             decode_state->reference_objects[i];
515         if (!obj_surface)
516             continue;
517
518         // Find whether the current frame is not already in our frame store
519         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
520             GenFrameStore * const fs = &frame_store[j];
521             if (fs->obj_surface == obj_surface)
522                 break;
523         }
524
525         // ... or add it
526         if (j == MAX_GEN_REFERENCE_FRAMES) {
527             if (n < num_free_refs) {
528                 GenFrameStore * const fs = free_refs[n++];
529                 fs->surface_id = obj_surface->base.id;
530                 fs->obj_surface = obj_surface;
531                 fs->frame_store_id = fs - frame_store;
532                 continue;
533             }
534             WARN_ONCE("No free slot found for DPB reference list!!!\n");
535         }
536     }
537 }
538
539 void
540 gen75_update_avc_frame_store_index(
541     VADriverContextP              ctx,
542     struct decode_state          *decode_state,
543     VAPictureParameterBufferH264 *pic_param,
544     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES]
545 )
546 {
547     int i, n;
548
549     /* Construct the Frame Store array, in compact form. i.e. empty or
550        invalid entries are discarded. */
551     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
552         struct object_surface * const obj_surface =
553             decode_state->reference_objects[i];
554         if (!obj_surface)
555             continue;
556
557         GenFrameStore * const fs = &frame_store[n];
558         fs->surface_id = obj_surface->base.id;
559         fs->obj_surface = obj_surface;
560         fs->frame_store_id = n++;
561     }
562
563     /* Any remaining entry is marked as invalid */
564     for (; n < MAX_GEN_REFERENCE_FRAMES; n++) {
565         GenFrameStore * const fs = &frame_store[n];
566         fs->surface_id = VA_INVALID_ID;
567         fs->obj_surface = NULL;
568         fs->frame_store_id = -1;
569     }
570 }
571
572 bool
573 gen75_fill_avc_picid_list(
574     uint16_t                    pic_ids[16],
575     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
576 )
577 {
578     int i, pic_id;
579
580     /* Fill in with known picture IDs. The Frame Store array is in
581        compact form, i.e. empty entries are only to be found at the
582        end of the array: there are no holes in the set of active
583        reference frames */
584     for (i = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
585         GenFrameStore * const fs = &frame_store[i];
586         if (!fs->obj_surface)
587             break;
588         pic_id = avc_get_picture_id(fs->obj_surface);
589         if (pic_id < 0)
590             return false;
591         pic_ids[i] = pic_id;
592     }
593
594     /* When an element of the list is not relevant the value of the
595        picture ID shall be set to 0 */
596     for (; i < MAX_GEN_REFERENCE_FRAMES; i++)
597         pic_ids[i] = 0;
598     return true;
599 }
600
601 bool
602 gen75_send_avc_picid_state(
603     struct intel_batchbuffer   *batch,
604     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
605 )
606 {
607     uint16_t pic_ids[16];
608
609     if (!gen75_fill_avc_picid_list(pic_ids, frame_store))
610         return false;
611
612     BEGIN_BCS_BATCH(batch, 10);
613     OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
614     OUT_BCS_BATCH(batch, 0); // enable Picture ID Remapping
615     intel_batchbuffer_data(batch, pic_ids, sizeof(pic_ids));
616     ADVANCE_BCS_BATCH(batch);
617     return true;
618 }
619
620 void
621 intel_update_vc1_frame_store_index(VADriverContextP ctx,
622                                    struct decode_state *decode_state,
623                                    VAPictureParameterBufferVC1 *pic_param,
624                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
625 {
626     struct object_surface *obj_surface;
627     int i;
628
629     obj_surface = decode_state->reference_objects[0];
630
631     if (pic_param->forward_reference_picture == VA_INVALID_ID ||
632         !obj_surface || 
633         !obj_surface->bo) {
634         frame_store[0].surface_id = VA_INVALID_ID;
635         frame_store[0].obj_surface = NULL;
636     } else {
637         frame_store[0].surface_id = pic_param->forward_reference_picture;
638         frame_store[0].obj_surface = obj_surface;
639     }
640
641     obj_surface = decode_state->reference_objects[1];
642
643     if (pic_param->backward_reference_picture == VA_INVALID_ID ||
644         !obj_surface || 
645         !obj_surface->bo) {
646         frame_store[1].surface_id = frame_store[0].surface_id;
647         frame_store[1].obj_surface = frame_store[0].obj_surface;
648     } else {
649         frame_store[1].surface_id = pic_param->backward_reference_picture;
650         frame_store[1].obj_surface = obj_surface;
651     }
652     for (i = 2; i < MAX_GEN_REFERENCE_FRAMES; i++) {
653         frame_store[i].surface_id = frame_store[i % 2].surface_id;
654         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
655     }
656
657 }
658
659 void
660 intel_update_vp8_frame_store_index(VADriverContextP ctx,
661                                    struct decode_state *decode_state,
662                                    VAPictureParameterBufferVP8 *pic_param,
663                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
664 {
665     struct object_surface *obj_surface;
666     int i;
667
668     obj_surface = decode_state->reference_objects[0];
669
670     if (pic_param->last_ref_frame == VA_INVALID_ID ||
671         !obj_surface ||
672         !obj_surface->bo) {
673         frame_store[0].surface_id = VA_INVALID_ID;
674         frame_store[0].obj_surface = NULL;
675     } else {
676         frame_store[0].surface_id = pic_param->last_ref_frame;
677         frame_store[0].obj_surface = obj_surface;
678     }
679
680     obj_surface = decode_state->reference_objects[1];
681
682     if (pic_param->golden_ref_frame == VA_INVALID_ID ||
683         !obj_surface ||
684         !obj_surface->bo) {
685         frame_store[1].surface_id = frame_store[0].surface_id;
686         frame_store[1].obj_surface = frame_store[0].obj_surface;
687     } else {
688         frame_store[1].surface_id = pic_param->golden_ref_frame;
689         frame_store[1].obj_surface = obj_surface;
690     }
691
692     obj_surface = decode_state->reference_objects[2];
693
694     if (pic_param->alt_ref_frame == VA_INVALID_ID ||
695         !obj_surface ||
696         !obj_surface->bo) {
697         frame_store[2].surface_id = frame_store[0].surface_id;
698         frame_store[2].obj_surface = frame_store[0].obj_surface;
699     } else {
700         frame_store[2].surface_id = pic_param->alt_ref_frame;
701         frame_store[2].obj_surface = obj_surface;
702     }
703
704     for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
705         frame_store[i].surface_id = frame_store[i % 2].surface_id;
706         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
707     }
708
709 }
710
711 static VAStatus
712 intel_decoder_check_avc_parameter(VADriverContextP ctx,
713                                   VAProfile h264_profile,
714                                   struct decode_state *decode_state)
715 {
716     struct i965_driver_data *i965 = i965_driver_data(ctx);
717     VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
718     VAStatus va_status;
719     struct object_surface *obj_surface; 
720     int i;
721
722     assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
723     assert(pic_param->CurrPic.picture_id != VA_INVALID_SURFACE);
724
725     if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID ||
726         pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
727         goto error;
728
729     assert(pic_param->CurrPic.picture_id == decode_state->current_render_target);
730
731     if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
732         goto error;
733
734     if ((h264_profile != VAProfileH264Baseline)) {
735        if (pic_param->num_slice_groups_minus1 ||
736            pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
737            WARN_ONCE("Unsupported the FMO/ASO constraints!!!\n");
738            goto error;
739        }
740     }
741
742     /* Fill in the reference objects array with the actual VA surface
743        objects with 1:1 correspondance with any entry in ReferenceFrames[],
744        i.e. including "holes" for invalid entries, that are expanded
745        to NULL in the reference_objects[] array */
746     for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
747         const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
748
749         obj_surface = NULL;
750         if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
751             va_pic->picture_id != VA_INVALID_ID) {
752             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
753             if (!obj_surface)
754                 return VA_STATUS_ERROR_INVALID_SURFACE;
755
756             /*
757              * Sometimes a dummy frame comes from the upper layer
758              * library, call i965_check_alloc_surface_bo() to make
759              * sure the store buffer is allocated for this reference
760              * frame
761              */
762             va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
763                 pic_param);
764             if (va_status != VA_STATUS_SUCCESS)
765                 return va_status;
766         }
767         decode_state->reference_objects[i] = obj_surface;
768     }
769     return VA_STATUS_SUCCESS;
770
771 error:
772     return VA_STATUS_ERROR_INVALID_PARAMETER;
773 }
774
775 static VAStatus
776 intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
777                                     struct decode_state *decode_state)
778 {
779     struct i965_driver_data *i965 = i965_driver_data(ctx);
780     VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
781     struct object_surface *obj_surface; 
782     int i = 0;
783     
784     if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
785     } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
786         obj_surface = SURFACE(pic_param->forward_reference_picture);
787
788         if (!obj_surface || !obj_surface->bo)
789             decode_state->reference_objects[i++] = NULL;
790         else
791             decode_state->reference_objects[i++] = obj_surface;
792     } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
793         obj_surface = SURFACE(pic_param->forward_reference_picture);
794
795         if (!obj_surface || !obj_surface->bo)
796             decode_state->reference_objects[i++] = NULL;
797         else
798             decode_state->reference_objects[i++] = obj_surface;
799
800         obj_surface = SURFACE(pic_param->backward_reference_picture);
801
802         if (!obj_surface || !obj_surface->bo)
803             decode_state->reference_objects[i++] = NULL;
804         else
805             decode_state->reference_objects[i++] = obj_surface;
806     } else
807         goto error;
808
809     for ( ; i < 16; i++)
810         decode_state->reference_objects[i] = NULL;
811
812     return VA_STATUS_SUCCESS;
813
814 error:
815     return VA_STATUS_ERROR_INVALID_PARAMETER;
816 }
817
818 static VAStatus
819 intel_decoder_check_vc1_parameter(VADriverContextP ctx,
820                                   struct decode_state *decode_state)
821 {
822     struct i965_driver_data *i965 = i965_driver_data(ctx);
823     VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
824     struct object_surface *obj_surface; 
825     int i = 0;
826
827     if (pic_param->sequence_fields.bits.interlace == 1 &&
828         pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
829         return VA_STATUS_ERROR_DECODING_ERROR;
830     }
831
832     if (pic_param->picture_fields.bits.picture_type == 0 ||
833         pic_param->picture_fields.bits.picture_type == 3) {
834     } else if (pic_param->picture_fields.bits.picture_type == 1 ||
835                pic_param->picture_fields.bits.picture_type == 4) {
836         obj_surface = SURFACE(pic_param->forward_reference_picture);
837
838         if (!obj_surface || !obj_surface->bo)
839             decode_state->reference_objects[i++] = NULL;
840         else
841             decode_state->reference_objects[i++] = obj_surface;
842     } else if (pic_param->picture_fields.bits.picture_type == 2) {
843         obj_surface = SURFACE(pic_param->forward_reference_picture);
844
845         if (!obj_surface || !obj_surface->bo)
846             decode_state->reference_objects[i++] = NULL;
847         else
848             decode_state->reference_objects[i++] = obj_surface;
849
850         obj_surface = SURFACE(pic_param->backward_reference_picture);
851
852         if (!obj_surface || !obj_surface->bo)
853             decode_state->reference_objects[i++] = NULL;
854         else
855             decode_state->reference_objects[i++] = obj_surface;
856     } else 
857         goto error;
858
859     for ( ; i < 16; i++)
860         decode_state->reference_objects[i] = NULL;
861
862     return VA_STATUS_SUCCESS;
863
864 error:
865     return VA_STATUS_ERROR_INVALID_PARAMETER;
866 }
867
868 static VAStatus
869 intel_decoder_check_vp8_parameter(VADriverContextP ctx,
870                                   struct decode_state *decode_state)
871 {
872     struct i965_driver_data *i965 = i965_driver_data(ctx);
873     VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
874     struct object_surface *obj_surface; 
875     int i = 0;
876
877     if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
878         obj_surface = SURFACE(pic_param->last_ref_frame);
879
880         if (obj_surface && obj_surface->bo)
881             decode_state->reference_objects[i++] = obj_surface;
882         else
883             decode_state->reference_objects[i++] = NULL;
884     }
885
886     if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
887         obj_surface = SURFACE(pic_param->golden_ref_frame);
888
889         if (obj_surface && obj_surface->bo)
890             decode_state->reference_objects[i++] = obj_surface;
891         else
892             decode_state->reference_objects[i++] = NULL;
893     }
894
895     if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
896         obj_surface = SURFACE(pic_param->alt_ref_frame);
897
898         if (obj_surface && obj_surface->bo)
899             decode_state->reference_objects[i++] = obj_surface;
900         else
901             decode_state->reference_objects[i++] = NULL;
902     }
903
904     for ( ; i < 16; i++)
905         decode_state->reference_objects[i] = NULL;
906
907     return VA_STATUS_SUCCESS;
908 }
909
910 VAStatus
911 intel_decoder_sanity_check_input(VADriverContextP ctx,
912                                  VAProfile profile,
913                                  struct decode_state *decode_state)
914 {
915     struct i965_driver_data *i965 = i965_driver_data(ctx);
916     struct object_surface *obj_surface;
917     VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
918
919     if (decode_state->current_render_target == VA_INVALID_SURFACE)
920         goto out;
921         
922     obj_surface = SURFACE(decode_state->current_render_target);
923
924     if (!obj_surface)
925         goto out;
926
927     decode_state->render_object = obj_surface;
928
929     switch (profile) {
930     case VAProfileMPEG2Simple:
931     case VAProfileMPEG2Main:
932         vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
933         break;
934         
935     case VAProfileH264ConstrainedBaseline:
936     case VAProfileH264Main:
937     case VAProfileH264High:
938     case VAProfileH264StereoHigh:
939     case VAProfileH264MultiviewHigh:
940         vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
941         break;
942
943     case VAProfileVC1Simple:
944     case VAProfileVC1Main:
945     case VAProfileVC1Advanced:
946         vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
947         break;
948
949     case VAProfileJPEGBaseline:
950         vaStatus = VA_STATUS_SUCCESS;
951         break;
952
953     case VAProfileVP8Version0_3:
954         vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
955         break;
956
957     default:
958         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
959         break;
960     }
961
962 out:
963     return vaStatus;
964 }
965
966 /*
967  * Return the next slice paramter
968  *
969  * Input:
970  *      slice_param: the current slice
971  *      *group_idx & *element_idx the current slice position in slice groups
972  * Output:
973  *      Return the next slice parameter
974  *      *group_idx & *element_idx the next slice position in slice groups,
975  *      if the next slice is NULL, *group_idx & *element_idx will be ignored
976  */
977 VASliceParameterBufferMPEG2 *
978 intel_mpeg2_find_next_slice(struct decode_state *decode_state,
979                             VAPictureParameterBufferMPEG2 *pic_param,
980                             VASliceParameterBufferMPEG2 *slice_param,
981                             int *group_idx,
982                             int *element_idx)
983 {
984     VASliceParameterBufferMPEG2 *next_slice_param;
985     unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
986     int j = *group_idx, i = *element_idx + 1;
987
988     for (; j < decode_state->num_slice_params; j++) {
989         for (; i < decode_state->slice_params[j]->num_elements; i++) {
990             next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
991
992             if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
993                 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
994                 *group_idx = j;
995                 *element_idx = i;
996
997                 return next_slice_param;
998             }
999         }
1000
1001         i = 0;
1002     }
1003
1004     return NULL;
1005 }
1006
1007 /* Ensure the segmentation buffer is large enough for the supplied
1008    number of MBs, or re-allocate it */
1009 bool
1010 intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
1011     unsigned int mb_width, unsigned int mb_height)
1012 {
1013     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1014     /* The segmentation map is a 64-byte aligned linear buffer, with
1015        each cache line holding only 8 bits for 4 continuous MBs */
1016     const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
1017
1018     if (buf->valid) {
1019         if (buf->bo && buf->bo->size >= buf_size)
1020             return true;
1021         drm_intel_bo_unreference(buf->bo);
1022         buf->valid = false;
1023     }
1024
1025     buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
1026         buf_size, 0x1000);
1027     buf->valid = buf->bo != NULL;
1028     return buf->valid;
1029 }