Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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. #include <alloca.h>
  27.  
  28. #include "intel_batchbuffer.h"
  29. #include "intel_media.h"
  30. #include "i965_drv_video.h"
  31. #include "i965_decoder_utils.h"
  32. #include "i965_defines.h"
  33.  
  34. /* Set reference surface if backing store exists */
  35. static inline int
  36. set_ref_frame(
  37.     struct i965_driver_data *i965,
  38.     GenFrameStore           *ref_frame,
  39.     VASurfaceID              va_surface,
  40.     struct object_surface   *obj_surface
  41. )
  42. {
  43.     if (va_surface == VA_INVALID_ID)
  44.         return 0;
  45.  
  46.     if (!obj_surface || !obj_surface->bo)
  47.         return 0;
  48.  
  49.     ref_frame->surface_id = va_surface;
  50.     ref_frame->obj_surface = obj_surface;
  51.     return 1;
  52. }
  53.  
  54. /* Check wether codec layer incorrectly fills in slice_vertical_position */
  55. int
  56. mpeg2_wa_slice_vertical_position(
  57.     struct decode_state           *decode_state,
  58.     VAPictureParameterBufferMPEG2 *pic_param
  59. )
  60. {
  61.     unsigned int i, j, mb_height, vpos, last_vpos = 0;
  62.  
  63.     /* Assume progressive sequence if we got a progressive frame */
  64.     if (pic_param->picture_coding_extension.bits.progressive_frame)
  65.         return 0;
  66.  
  67.     /* Wait for a field coded picture */
  68.     if (pic_param->picture_coding_extension.bits.picture_structure == MPEG_FRAME)
  69.         return -1;
  70.  
  71.     assert(decode_state && decode_state->slice_params);
  72.  
  73.     mb_height = (pic_param->vertical_size + 31) / 32;
  74.  
  75.     for (j = 0; j < decode_state->num_slice_params; j++) {
  76.         struct buffer_store * const buffer_store =
  77.             decode_state->slice_params[j];
  78.  
  79.         for (i = 0; i < buffer_store->num_elements; i++) {
  80.             VASliceParameterBufferMPEG2 * const slice_param =
  81.                 ((VASliceParameterBufferMPEG2 *)buffer_store->buffer) + i;
  82.  
  83.             vpos = slice_param->slice_vertical_position;
  84.             if (vpos >= mb_height || vpos == last_vpos + 2) {
  85.                 WARN_ONCE("codec layer incorrectly fills in MPEG-2 slice_vertical_position. Workaround applied\n");
  86.                 return 1;
  87.             }
  88.             last_vpos = vpos;
  89.         }
  90.     }
  91.     return 0;
  92. }
  93.  
  94. /* Build MPEG-2 reference frames array */
  95. void
  96. mpeg2_set_reference_surfaces(
  97.     VADriverContextP               ctx,
  98.     GenFrameStore                  ref_frames[MAX_GEN_REFERENCE_FRAMES],
  99.     struct decode_state           *decode_state,
  100.     VAPictureParameterBufferMPEG2 *pic_param
  101. )
  102. {
  103.     struct i965_driver_data * const i965 = i965_driver_data(ctx);
  104.     VASurfaceID va_surface;
  105.     unsigned pic_structure, is_second_field, n = 0;
  106.     struct object_surface *obj_surface;
  107.  
  108.     pic_structure = pic_param->picture_coding_extension.bits.picture_structure;
  109.     is_second_field = pic_structure != MPEG_FRAME &&
  110.         !pic_param->picture_coding_extension.bits.is_first_field;
  111.  
  112.     ref_frames[0].surface_id = VA_INVALID_ID;
  113.     ref_frames[0].obj_surface = NULL;
  114.  
  115.     /* Reference frames are indexed by frame store ID  (0:top, 1:bottom) */
  116.     switch (pic_param->picture_coding_type) {
  117.     case MPEG_P_PICTURE:
  118.         if (is_second_field && pic_structure == MPEG_BOTTOM_FIELD) {
  119.             va_surface = decode_state->current_render_target;
  120.             obj_surface = decode_state->render_object;
  121.             n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  122.         }
  123.         va_surface = pic_param->forward_reference_picture;
  124.         obj_surface = decode_state->reference_objects[0];
  125.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  126.         break;
  127.  
  128.     case MPEG_B_PICTURE:
  129.         va_surface = pic_param->forward_reference_picture;
  130.         obj_surface = decode_state->reference_objects[0];
  131.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  132.         va_surface = pic_param->backward_reference_picture;
  133.         obj_surface = decode_state->reference_objects[1];
  134.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  135.         break;
  136.     }
  137.  
  138.     while (n != 2) {
  139.         ref_frames[n].obj_surface = ref_frames[0].obj_surface;
  140.         ref_frames[n++].surface_id = ref_frames[0].surface_id;
  141.     }
  142.  
  143.     if (pic_param->picture_coding_extension.bits.frame_pred_frame_dct)
  144.         return;
  145.  
  146.     ref_frames[2].surface_id = VA_INVALID_ID;
  147.     ref_frames[2].obj_surface = NULL;
  148.  
  149.     /* Bottom field pictures used as reference */
  150.     switch (pic_param->picture_coding_type) {
  151.     case MPEG_P_PICTURE:
  152.         if (is_second_field && pic_structure == MPEG_TOP_FIELD) {
  153.             va_surface = decode_state->current_render_target;
  154.             obj_surface = decode_state->render_object;
  155.             n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  156.         }
  157.         va_surface = pic_param->forward_reference_picture;
  158.         obj_surface = decode_state->reference_objects[0];
  159.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  160.         break;
  161.  
  162.     case MPEG_B_PICTURE:
  163.         va_surface = pic_param->forward_reference_picture;
  164.         obj_surface = decode_state->reference_objects[0];
  165.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  166.         va_surface = pic_param->backward_reference_picture;
  167.         obj_surface = decode_state->reference_objects[1];
  168.         n += set_ref_frame(i965, &ref_frames[n], va_surface, obj_surface);
  169.         break;
  170.     }
  171.  
  172.     while (n != 4) {
  173.         ref_frames[n].obj_surface = ref_frames[2].obj_surface;
  174.         ref_frames[n++].surface_id = ref_frames[2].surface_id;
  175.     }
  176. }
  177.  
  178. /* Ensure the supplied VA surface has valid storage for decoding the
  179.    current picture */
  180. VAStatus
  181. avc_ensure_surface_bo(
  182.     VADriverContextP                    ctx,
  183.     struct decode_state                *decode_state,
  184.     struct object_surface              *obj_surface,
  185.     const VAPictureParameterBufferH264 *pic_param
  186. )
  187. {
  188.     VAStatus va_status;
  189.     uint32_t hw_fourcc, fourcc, subsample, chroma_format;
  190.  
  191.     /* Validate chroma format */
  192.     switch (pic_param->seq_fields.bits.chroma_format_idc) {
  193.     case 0: // Grayscale
  194.         fourcc = VA_FOURCC_Y800;
  195.         subsample = SUBSAMPLE_YUV400;
  196.         chroma_format = VA_RT_FORMAT_YUV400;
  197.         break;
  198.     case 1: // YUV 4:2:0
  199.         fourcc = VA_FOURCC_NV12;
  200.         subsample = SUBSAMPLE_YUV420;
  201.         chroma_format = VA_RT_FORMAT_YUV420;
  202.         break;
  203.     default:
  204.         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
  205.     }
  206.  
  207.     /* Determine the HW surface format, bound to VA config needs */
  208.     if ((decode_state->base.chroma_formats & chroma_format) == chroma_format)
  209.         hw_fourcc = fourcc;
  210.     else {
  211.         hw_fourcc = 0;
  212.         switch (fourcc) {
  213.         case VA_FOURCC_Y800: // Implement with an NV12 surface
  214.             if (decode_state->base.chroma_formats & VA_RT_FORMAT_YUV420) {
  215.                 hw_fourcc = VA_FOURCC_NV12;
  216.                 subsample = SUBSAMPLE_YUV420;
  217.             }
  218.             break;
  219.         }
  220.     }
  221.     if (!hw_fourcc)
  222.         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
  223.  
  224.     /* (Re-)allocate the underlying surface buffer store, if necessary */
  225.     if (!obj_surface->bo || obj_surface->fourcc != hw_fourcc) {
  226.         struct i965_driver_data * const i965 = i965_driver_data(ctx);
  227.  
  228.         i965_destroy_surface_storage(obj_surface);
  229.         va_status = i965_check_alloc_surface_bo(ctx, obj_surface,
  230.             i965->codec_info->has_tiled_surface, hw_fourcc, subsample);
  231.         if (va_status != VA_STATUS_SUCCESS)
  232.             return va_status;
  233.     }
  234.  
  235.     /* Fake chroma components if grayscale is implemented on top of NV12 */
  236.     if (fourcc == VA_FOURCC_Y800 && hw_fourcc == VA_FOURCC_NV12) {
  237.         const uint32_t uv_offset = obj_surface->width * obj_surface->height;
  238.         const uint32_t uv_size   = obj_surface->width * obj_surface->height / 2;
  239.  
  240.         drm_intel_gem_bo_map_gtt(obj_surface->bo);
  241.         memset(obj_surface->bo->virtual + uv_offset, 0x80, uv_size);
  242.         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
  243.     }
  244.     return VA_STATUS_SUCCESS;
  245. }
  246.  
  247. /* Generate flat scaling matrices for H.264 decoding */
  248. void
  249. avc_gen_default_iq_matrix(VAIQMatrixBufferH264 *iq_matrix)
  250. {
  251.     /* Flat_4x4_16 */
  252.     memset(&iq_matrix->ScalingList4x4, 16, sizeof(iq_matrix->ScalingList4x4));
  253.  
  254.     /* Flat_8x8_16 */
  255.     memset(&iq_matrix->ScalingList8x8, 16, sizeof(iq_matrix->ScalingList8x8));
  256. }
  257.  
  258. /* Returns the POC of the supplied VA picture */
  259. static int
  260. avc_get_picture_poc(const VAPictureH264 *va_pic)
  261. {
  262.     int structure, field_poc[2];
  263.  
  264.     structure = va_pic->flags &
  265.         (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
  266.     field_poc[0] = structure != VA_PICTURE_H264_BOTTOM_FIELD ?
  267.         va_pic->TopFieldOrderCnt : INT_MAX;
  268.     field_poc[1] = structure != VA_PICTURE_H264_TOP_FIELD ?
  269.         va_pic->BottomFieldOrderCnt : INT_MAX;
  270.     return MIN(field_poc[0], field_poc[1]);
  271. }
  272.  
  273. /* Returns a unique picture ID that represents the supplied VA surface object */
  274. int
  275. avc_get_picture_id(struct object_surface *obj_surface)
  276. {
  277.     int pic_id;
  278.  
  279.     /* This highly depends on how the internal VA objects are organized.
  280.  
  281.        Theory of operations:
  282.        The VA objects are maintained in heaps so that any released VA
  283.        surface will become free again for future allocation. This means
  284.        that holes in there are filled in for subsequent allocations.
  285.        So, this ultimately means that we could just use the Heap ID of
  286.        the VA surface as the resulting picture ID (16 bits) */
  287.     pic_id = 1 + (obj_surface->base.id & OBJECT_HEAP_ID_MASK);
  288.     return (pic_id <= 0xffff) ? pic_id : -1;
  289. }
  290.  
  291. /* Finds the VA/H264 picture associated with the specified VA surface id */
  292. VAPictureH264 *
  293. avc_find_picture(VASurfaceID id, VAPictureH264 *pic_list, int pic_list_count)
  294. {
  295.     int i;
  296.  
  297.     if (id != VA_INVALID_ID) {
  298.         for (i = 0; i < pic_list_count; i++) {
  299.             VAPictureH264 * const va_pic = &pic_list[i];
  300.             if (va_pic->picture_id == id &&
  301.                 !(va_pic->flags & VA_PICTURE_H264_INVALID))
  302.                 return va_pic;
  303.         }
  304.     }
  305.     return NULL;
  306. }
  307.  
  308. /* Get first macroblock bit offset for BSD, minus EPB count (AVC) */
  309. /* XXX: slice_data_bit_offset does not account for EPB */
  310. unsigned int
  311. avc_get_first_mb_bit_offset(
  312.     dri_bo                     *slice_data_bo,
  313.     VASliceParameterBufferH264 *slice_param,
  314.     unsigned int                mode_flag
  315. )
  316. {
  317.     unsigned int slice_data_bit_offset = slice_param->slice_data_bit_offset;
  318.  
  319.     if (mode_flag == ENTROPY_CABAC)
  320.         slice_data_bit_offset = ALIGN(slice_data_bit_offset, 0x8);
  321.     return slice_data_bit_offset;
  322. }
  323.  
  324. /* Get first macroblock bit offset for BSD, with EPB count (AVC) */
  325. /* XXX: slice_data_bit_offset does not account for EPB */
  326. unsigned int
  327. avc_get_first_mb_bit_offset_with_epb(
  328.     dri_bo                     *slice_data_bo,
  329.     VASliceParameterBufferH264 *slice_param,
  330.     unsigned int                mode_flag
  331. )
  332. {
  333.     unsigned int in_slice_data_bit_offset = slice_param->slice_data_bit_offset;
  334.     unsigned int out_slice_data_bit_offset;
  335.     unsigned int i, j, n, buf_size, data_size, header_size;
  336.     uint8_t *buf;
  337.     int ret;
  338.  
  339.     header_size = slice_param->slice_data_bit_offset / 8;
  340.     data_size   = slice_param->slice_data_size - slice_param->slice_data_offset;
  341.     buf_size    = (header_size * 3 + 1) / 2; // Max possible header size (x1.5)
  342.  
  343.     if (buf_size > data_size)
  344.         buf_size = data_size;
  345.  
  346.     buf = alloca(buf_size);
  347.     ret = dri_bo_get_subdata(
  348.         slice_data_bo, slice_param->slice_data_offset,
  349.         buf_size, buf
  350.     );
  351.     assert(ret == 0);
  352.  
  353.     for (i = 2, j = 2, n = 0; i < buf_size && j < header_size; i++, j++) {
  354.         if (buf[i] == 0x03 && buf[i - 1] == 0x00 && buf[i - 2] == 0x00)
  355.             i += 2, j++, n++;
  356.     }
  357.  
  358.     out_slice_data_bit_offset = in_slice_data_bit_offset + n * 8;
  359.  
  360.     if (mode_flag == ENTROPY_CABAC)
  361.         out_slice_data_bit_offset = ALIGN(out_slice_data_bit_offset, 0x8);
  362.     return out_slice_data_bit_offset;
  363. }
  364.  
  365. static inline uint8_t
  366. get_ref_idx_state_1(const VAPictureH264 *va_pic, unsigned int frame_store_id)
  367. {
  368.     /* The H.264 standard, and the VA-API specification, allows for at
  369.        least 3 states for a picture: "used for short-term reference",
  370.        "used for long-term reference", or considered as not used for
  371.        reference.
  372.  
  373.        The latter is used in the MVC inter prediction and inter-view
  374.        prediction process (H.8.4). This has an incidence on the
  375.        colZeroFlag variable, as defined in 8.4.1.2.
  376.  
  377.        Since it is not possible to directly program that flag, let's
  378.        make the hardware derive this value by assimilating "considered
  379.        as not used for reference" to a "not used for short-term
  380.        reference", and subsequently making it "used for long-term
  381.        reference" to fit the definition of Bit6 here */
  382.     const unsigned int ref_flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE |
  383.         VA_PICTURE_H264_LONG_TERM_REFERENCE;
  384.     const unsigned int is_long_term =
  385.         ((va_pic->flags & ref_flags) != VA_PICTURE_H264_SHORT_TERM_REFERENCE);
  386.     const unsigned int is_top_field =
  387.         !!(va_pic->flags & VA_PICTURE_H264_TOP_FIELD);
  388.     const unsigned int is_bottom_field =
  389.         !!(va_pic->flags & VA_PICTURE_H264_BOTTOM_FIELD);
  390.  
  391.     return ((is_long_term                         << 6) |
  392.             ((is_top_field ^ is_bottom_field ^ 1) << 5) |
  393.             (frame_store_id                       << 1) |
  394.             ((is_top_field ^ 1) & is_bottom_field));
  395. }
  396.  
  397. /* Fill in Reference List Entries (Gen5+: ILK, SNB, IVB) */
  398. void
  399. gen5_fill_avc_ref_idx_state(
  400.     uint8_t             state[32],
  401.     const VAPictureH264 ref_list[32],
  402.     unsigned int        ref_list_count,
  403.     const GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES]
  404. )
  405. {
  406.     int i, j;
  407.  
  408.     for (i = 0; i < ref_list_count; i++) {
  409.         const VAPictureH264 * const va_pic = &ref_list[i];
  410.  
  411.         if ((va_pic->flags & VA_PICTURE_H264_INVALID) ||
  412.             va_pic->picture_id == VA_INVALID_ID) {
  413.             state[i] = 0xff;
  414.             continue;
  415.         }
  416.  
  417.         for (j = 0; j < MAX_GEN_REFERENCE_FRAMES; j++) {
  418.             if (frame_store[j].surface_id == va_pic->picture_id)
  419.                 break;
  420.         }
  421.  
  422.         if (j != MAX_GEN_REFERENCE_FRAMES) { // Found picture in the Frame Store
  423.             const GenFrameStore * const fs = &frame_store[j];
  424.             assert(fs->frame_store_id == j); // Current architecture/assumption
  425.             state[i] = get_ref_idx_state_1(va_pic, fs->frame_store_id);
  426.         }
  427.         else {
  428.             WARN_ONCE("Invalid RefPicListX[] entry!!! It is not included in DPB\n");
  429.             state[i] = get_ref_idx_state_1(va_pic, 0) | 0x80;
  430.         }
  431.     }
  432.  
  433.     for (; i < 32; i++)
  434.         state[i] = 0xff;
  435. }
  436.  
  437. /* Emit Reference List Entries (Gen6+: SNB, IVB) */
  438. static void
  439. gen6_send_avc_ref_idx_state_1(
  440.     struct intel_batchbuffer         *batch,
  441.     unsigned int                      list,
  442.     const VAPictureH264              *ref_list,
  443.     unsigned int                      ref_list_count,
  444.     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
  445. )
  446. {
  447.     uint8_t ref_idx_state[32];
  448.  
  449.     BEGIN_BCS_BATCH(batch, 10);
  450.     OUT_BCS_BATCH(batch, MFX_AVC_REF_IDX_STATE | (10 - 2));
  451.     OUT_BCS_BATCH(batch, list);
  452.     gen5_fill_avc_ref_idx_state(
  453.         ref_idx_state,
  454.         ref_list, ref_list_count,
  455.         frame_store
  456.     );
  457.     intel_batchbuffer_data(batch, ref_idx_state, sizeof(ref_idx_state));
  458.     ADVANCE_BCS_BATCH(batch);
  459. }
  460.  
  461. void
  462. gen6_send_avc_ref_idx_state(
  463.     struct intel_batchbuffer         *batch,
  464.     const VASliceParameterBufferH264 *slice_param,
  465.     const GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
  466. )
  467. {
  468.     if (slice_param->slice_type == SLICE_TYPE_I ||
  469.         slice_param->slice_type == SLICE_TYPE_SI)
  470.         return;
  471.  
  472.     /* RefPicList0 */
  473.     gen6_send_avc_ref_idx_state_1(
  474.         batch, 0,
  475.         slice_param->RefPicList0, slice_param->num_ref_idx_l0_active_minus1 + 1,
  476.         frame_store
  477.     );
  478.  
  479.     if (slice_param->slice_type != SLICE_TYPE_B)
  480.         return;
  481.  
  482.     /* RefPicList1 */
  483.     gen6_send_avc_ref_idx_state_1(
  484.         batch, 1,
  485.         slice_param->RefPicList1, slice_param->num_ref_idx_l1_active_minus1 + 1,
  486.         frame_store
  487.     );
  488. }
  489.  
  490. static void
  491. gen6_mfd_avc_phantom_slice_state(VADriverContextP ctx,
  492.                                  VAPictureParameterBufferH264 *pic_param,
  493.                                  VASliceParameterBufferH264 *next_slice_param,
  494.                                  struct intel_batchbuffer *batch)
  495. {
  496.     int width_in_mbs = pic_param->picture_width_in_mbs_minus1 + 1;
  497.     int height_in_mbs = pic_param->picture_height_in_mbs_minus1 + 1; /* frame height */
  498.     int slice_hor_pos, slice_ver_pos, slice_start_mb_num, next_slice_hor_pos, next_slice_ver_pos;
  499.     int mbaff_picture = (!pic_param->pic_fields.bits.field_pic_flag &&
  500.                          pic_param->seq_fields.bits.mb_adaptive_frame_field_flag);
  501.  
  502.     if (next_slice_param) {
  503.         int first_mb_in_next_slice;
  504.  
  505.         slice_hor_pos = 0;
  506.         slice_ver_pos = 0;
  507.         slice_start_mb_num = 0;
  508.         first_mb_in_next_slice = next_slice_param->first_mb_in_slice << mbaff_picture;
  509.         next_slice_hor_pos = first_mb_in_next_slice % width_in_mbs;
  510.         next_slice_ver_pos = first_mb_in_next_slice / width_in_mbs;
  511.     } else {
  512.         slice_hor_pos = 0;
  513.         slice_ver_pos = height_in_mbs;
  514.         slice_start_mb_num = width_in_mbs * height_in_mbs / (1 + !!pic_param->pic_fields.bits.field_pic_flag);
  515.         next_slice_hor_pos = 0;
  516.         next_slice_ver_pos = 0;
  517.     }
  518.  
  519.     BEGIN_BCS_BATCH(batch, 11);
  520.     OUT_BCS_BATCH(batch, MFX_AVC_SLICE_STATE | (11 - 2));
  521.     OUT_BCS_BATCH(batch, 0);
  522.     OUT_BCS_BATCH(batch, 0);
  523.     OUT_BCS_BATCH(batch, 0);
  524.     OUT_BCS_BATCH(batch,
  525.                   slice_ver_pos << 24 |
  526.                   slice_hor_pos << 16 |
  527.                   slice_start_mb_num << 0);
  528.     OUT_BCS_BATCH(batch,
  529.                   next_slice_ver_pos << 16 |
  530.                   next_slice_hor_pos << 0);
  531.     OUT_BCS_BATCH(batch, 0);
  532.     OUT_BCS_BATCH(batch, 0);
  533.     OUT_BCS_BATCH(batch, 0);
  534.     OUT_BCS_BATCH(batch, 0);
  535.     OUT_BCS_BATCH(batch, 0);
  536.     ADVANCE_BCS_BATCH(batch);
  537. }
  538.  
  539. static void
  540. gen6_mfd_avc_phantom_slice_bsd_object(VADriverContextP ctx,
  541.                                       VAPictureParameterBufferH264 *pic_param,
  542.                                       struct intel_batchbuffer *batch)
  543. {
  544.  
  545.     BEGIN_BCS_BATCH(batch, 6);
  546.     OUT_BCS_BATCH(batch, MFD_AVC_BSD_OBJECT | (6 - 2));
  547.     OUT_BCS_BATCH(batch, 0);
  548.     OUT_BCS_BATCH(batch, 0);
  549.     OUT_BCS_BATCH(batch, 0);
  550.     OUT_BCS_BATCH(batch, 0);
  551.     OUT_BCS_BATCH(batch, 0);
  552.     ADVANCE_BCS_BATCH(batch);
  553. }
  554.  
  555. void
  556. gen6_mfd_avc_phantom_slice(VADriverContextP ctx,
  557.                            VAPictureParameterBufferH264 *pic_param,
  558.                            VASliceParameterBufferH264 *next_slice_param,
  559.                            struct intel_batchbuffer *batch)
  560. {
  561.     gen6_mfd_avc_phantom_slice_state(ctx, pic_param, next_slice_param, batch);
  562.     gen6_mfd_avc_phantom_slice_bsd_object(ctx, pic_param, batch);
  563. }
  564.  
  565. /* Comparison function for sorting out the array of free frame store entries */
  566. static int
  567. compare_avc_ref_store_func(const void *p1, const void *p2)
  568. {
  569.     const GenFrameStore * const fs1 = *((GenFrameStore **)p1);
  570.     const GenFrameStore * const fs2 = *((GenFrameStore **)p2);
  571.  
  572.     return fs1->ref_age - fs2->ref_age;
  573. }
  574.  
  575. void
  576. intel_update_avc_frame_store_index(
  577.     VADriverContextP              ctx,
  578.     struct decode_state          *decode_state,
  579.     VAPictureParameterBufferH264 *pic_param,
  580.     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES],
  581.     GenFrameStoreContext         *fs_ctx
  582. )
  583. {
  584.     GenFrameStore *free_refs[MAX_GEN_REFERENCE_FRAMES];
  585.     uint32_t used_refs = 0, add_refs = 0;
  586.     uint64_t age;
  587.     int i, n, num_free_refs;
  588.  
  589.     /* Detect changes of access unit */
  590.     const int poc = avc_get_picture_poc(&pic_param->CurrPic);
  591.     if (fs_ctx->age == 0 || fs_ctx->prev_poc != poc)
  592.         fs_ctx->age++;
  593.     fs_ctx->prev_poc = poc;
  594.     age = fs_ctx->age;
  595.  
  596.     /* Tag entries that are still available in our Frame Store */
  597.     for (i = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
  598.         struct object_surface * const obj_surface =
  599.             decode_state->reference_objects[i];
  600.         if (!obj_surface)
  601.             continue;
  602.  
  603.         GenAvcSurface * const avc_surface = obj_surface->private_data;
  604.         if (!avc_surface)
  605.             continue;
  606.         if (avc_surface->frame_store_id >= 0) {
  607.             GenFrameStore * const fs =
  608.                 &frame_store[avc_surface->frame_store_id];
  609.             if (fs->surface_id == obj_surface->base.id) {
  610.                 fs->obj_surface = obj_surface;
  611.                 fs->ref_age = age;
  612.                 used_refs |= 1 << fs->frame_store_id;
  613.                 continue;
  614.             }
  615.         }
  616.         add_refs |= 1 << i;
  617.     }
  618.  
  619.     /* Build and sort out the list of retired candidates. The resulting
  620.        list is ordered by increasing age when they were last used */
  621.     for (i = 0, n = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
  622.         if (!(used_refs & (1 << i))) {
  623.             GenFrameStore * const fs = &frame_store[i];
  624.             fs->obj_surface = NULL;
  625.             free_refs[n++] = fs;
  626.         }
  627.     }
  628.     num_free_refs = n;
  629.     qsort(&free_refs[0], n, sizeof(free_refs[0]), compare_avc_ref_store_func);
  630.  
  631.     /* Append the new reference frames */
  632.     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
  633.         struct object_surface * const obj_surface =
  634.             decode_state->reference_objects[i];
  635.         if (!obj_surface || !(add_refs & (1 << i)))
  636.             continue;
  637.  
  638.         GenAvcSurface * const avc_surface = obj_surface->private_data;
  639.         if (!avc_surface)
  640.             continue;
  641.         if (n < num_free_refs) {
  642.             GenFrameStore * const fs = free_refs[n++];
  643.             fs->surface_id = obj_surface->base.id;
  644.             fs->obj_surface = obj_surface;
  645.             fs->frame_store_id = fs - frame_store;
  646.             fs->ref_age = age;
  647.             avc_surface->frame_store_id = fs->frame_store_id;
  648.             continue;
  649.         }
  650.         WARN_ONCE("No free slot found for DPB reference list!!!\n");
  651.     }
  652. }
  653.  
  654. void
  655. gen75_update_avc_frame_store_index(
  656.     VADriverContextP              ctx,
  657.     struct decode_state          *decode_state,
  658.     VAPictureParameterBufferH264 *pic_param,
  659.     GenFrameStore                 frame_store[MAX_GEN_REFERENCE_FRAMES]
  660. )
  661. {
  662.     int i, n;
  663.  
  664.     /* Construct the Frame Store array, in compact form. i.e. empty or
  665.        invalid entries are discarded. */
  666.     for (i = 0, n = 0; i < ARRAY_ELEMS(decode_state->reference_objects); i++) {
  667.         struct object_surface * const obj_surface =
  668.             decode_state->reference_objects[i];
  669.         if (!obj_surface)
  670.             continue;
  671.  
  672.         GenFrameStore * const fs = &frame_store[n];
  673.         fs->surface_id = obj_surface->base.id;
  674.         fs->obj_surface = obj_surface;
  675.         fs->frame_store_id = n++;
  676.     }
  677.  
  678.     /* Any remaining entry is marked as invalid */
  679.     for (; n < MAX_GEN_REFERENCE_FRAMES; n++) {
  680.         GenFrameStore * const fs = &frame_store[n];
  681.         fs->surface_id = VA_INVALID_ID;
  682.         fs->obj_surface = NULL;
  683.         fs->frame_store_id = -1;
  684.     }
  685. }
  686.  
  687. bool
  688. gen75_fill_avc_picid_list(
  689.     uint16_t                    pic_ids[16],
  690.     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
  691. )
  692. {
  693.     int i, pic_id;
  694.  
  695.     /* Fill in with known picture IDs. The Frame Store array is in
  696.        compact form, i.e. empty entries are only to be found at the
  697.        end of the array: there are no holes in the set of active
  698.        reference frames */
  699.     for (i = 0; i < MAX_GEN_REFERENCE_FRAMES; i++) {
  700.         GenFrameStore * const fs = &frame_store[i];
  701.         if (!fs->obj_surface)
  702.             break;
  703.         pic_id = avc_get_picture_id(fs->obj_surface);
  704.         if (pic_id < 0)
  705.             return false;
  706.         pic_ids[i] = pic_id;
  707.     }
  708.  
  709.     /* When an element of the list is not relevant the value of the
  710.        picture ID shall be set to 0 */
  711.     for (; i < MAX_GEN_REFERENCE_FRAMES; i++)
  712.         pic_ids[i] = 0;
  713.     return true;
  714. }
  715.  
  716. bool
  717. gen75_send_avc_picid_state(
  718.     struct intel_batchbuffer   *batch,
  719.     GenFrameStore               frame_store[MAX_GEN_REFERENCE_FRAMES]
  720. )
  721. {
  722.     uint16_t pic_ids[16];
  723.  
  724.     if (!gen75_fill_avc_picid_list(pic_ids, frame_store))
  725.         return false;
  726.  
  727.     BEGIN_BCS_BATCH(batch, 10);
  728.     OUT_BCS_BATCH(batch, MFD_AVC_PICID_STATE | (10 - 2));
  729.     OUT_BCS_BATCH(batch, 0); // enable Picture ID Remapping
  730.     intel_batchbuffer_data(batch, pic_ids, sizeof(pic_ids));
  731.     ADVANCE_BCS_BATCH(batch);
  732.     return true;
  733. }
  734.  
  735. void
  736. intel_update_vc1_frame_store_index(VADriverContextP ctx,
  737.                                    struct decode_state *decode_state,
  738.                                    VAPictureParameterBufferVC1 *pic_param,
  739.                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
  740. {
  741.     struct object_surface *obj_surface;
  742.     int i;
  743.  
  744.     obj_surface = decode_state->reference_objects[0];
  745.  
  746.     if (pic_param->forward_reference_picture == VA_INVALID_ID ||
  747.         !obj_surface ||
  748.         !obj_surface->bo) {
  749.         frame_store[0].surface_id = VA_INVALID_ID;
  750.         frame_store[0].obj_surface = NULL;
  751.     } else {
  752.         frame_store[0].surface_id = pic_param->forward_reference_picture;
  753.         frame_store[0].obj_surface = obj_surface;
  754.     }
  755.  
  756.     obj_surface = decode_state->reference_objects[1];
  757.  
  758.     if (pic_param->backward_reference_picture == VA_INVALID_ID ||
  759.         !obj_surface ||
  760.         !obj_surface->bo) {
  761.         frame_store[1].surface_id = frame_store[0].surface_id;
  762.         frame_store[1].obj_surface = frame_store[0].obj_surface;
  763.     } else {
  764.         frame_store[1].surface_id = pic_param->backward_reference_picture;
  765.         frame_store[1].obj_surface = obj_surface;
  766.     }
  767.     for (i = 2; i < MAX_GEN_REFERENCE_FRAMES; i++) {
  768.         frame_store[i].surface_id = frame_store[i % 2].surface_id;
  769.         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
  770.     }
  771.  
  772. }
  773.  
  774. void
  775. intel_update_vp8_frame_store_index(VADriverContextP ctx,
  776.                                    struct decode_state *decode_state,
  777.                                    VAPictureParameterBufferVP8 *pic_param,
  778.                                    GenFrameStore frame_store[MAX_GEN_REFERENCE_FRAMES])
  779. {
  780.     struct object_surface *obj_surface;
  781.     int i;
  782.  
  783.     obj_surface = decode_state->reference_objects[0];
  784.  
  785.     if (pic_param->last_ref_frame == VA_INVALID_ID ||
  786.         !obj_surface ||
  787.         !obj_surface->bo) {
  788.         frame_store[0].surface_id = VA_INVALID_ID;
  789.         frame_store[0].obj_surface = NULL;
  790.     } else {
  791.         frame_store[0].surface_id = pic_param->last_ref_frame;
  792.         frame_store[0].obj_surface = obj_surface;
  793.     }
  794.  
  795.     obj_surface = decode_state->reference_objects[1];
  796.  
  797.     if (pic_param->golden_ref_frame == VA_INVALID_ID ||
  798.         !obj_surface ||
  799.         !obj_surface->bo) {
  800.         frame_store[1].surface_id = frame_store[0].surface_id;
  801.         frame_store[1].obj_surface = frame_store[0].obj_surface;
  802.     } else {
  803.         frame_store[1].surface_id = pic_param->golden_ref_frame;
  804.         frame_store[1].obj_surface = obj_surface;
  805.     }
  806.  
  807.     obj_surface = decode_state->reference_objects[2];
  808.  
  809.     if (pic_param->alt_ref_frame == VA_INVALID_ID ||
  810.         !obj_surface ||
  811.         !obj_surface->bo) {
  812.         frame_store[2].surface_id = frame_store[0].surface_id;
  813.         frame_store[2].obj_surface = frame_store[0].obj_surface;
  814.     } else {
  815.         frame_store[2].surface_id = pic_param->alt_ref_frame;
  816.         frame_store[2].obj_surface = obj_surface;
  817.     }
  818.  
  819.     for (i = 3; i < MAX_GEN_REFERENCE_FRAMES; i++) {
  820.         frame_store[i].surface_id = frame_store[i % 2].surface_id;
  821.         frame_store[i].obj_surface = frame_store[i % 2].obj_surface;
  822.     }
  823.  
  824. }
  825.  
  826. static VAStatus
  827. intel_decoder_check_avc_parameter(VADriverContextP ctx,
  828.                                   VAProfile h264_profile,
  829.                                   struct decode_state *decode_state)
  830. {
  831.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  832.     VAPictureParameterBufferH264 *pic_param = (VAPictureParameterBufferH264 *)decode_state->pic_param->buffer;
  833.     VAStatus va_status;
  834.     struct object_surface *obj_surface;
  835.     int i;
  836.     VASliceParameterBufferH264 *slice_param, *next_slice_param, *next_slice_group_param;
  837.     int j;
  838.  
  839.     assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
  840.     assert(pic_param->CurrPic.picture_id != VA_INVALID_SURFACE);
  841.  
  842.     if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID ||
  843.         pic_param->CurrPic.picture_id == VA_INVALID_SURFACE)
  844.         goto error;
  845.  
  846.     assert(pic_param->CurrPic.picture_id == decode_state->current_render_target);
  847.  
  848.     if (pic_param->CurrPic.picture_id != decode_state->current_render_target)
  849.         goto error;
  850.  
  851.     if ((h264_profile != VAProfileH264Baseline)) {
  852.        if (pic_param->num_slice_groups_minus1 ||
  853.            pic_param->pic_fields.bits.redundant_pic_cnt_present_flag) {
  854.            WARN_ONCE("Unsupported the FMO/ASO constraints!!!\n");
  855.            goto error;
  856.        }
  857.     }
  858.  
  859.     /* Fill in the reference objects array with the actual VA surface
  860.        objects with 1:1 correspondance with any entry in ReferenceFrames[],
  861.        i.e. including "holes" for invalid entries, that are expanded
  862.        to NULL in the reference_objects[] array */
  863.     for (i = 0; i < ARRAY_ELEMS(pic_param->ReferenceFrames); i++) {
  864.         const VAPictureH264 * const va_pic = &pic_param->ReferenceFrames[i];
  865.  
  866.         obj_surface = NULL;
  867.         if (!(va_pic->flags & VA_PICTURE_H264_INVALID) &&
  868.             va_pic->picture_id != VA_INVALID_ID) {
  869.             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
  870.             if (!obj_surface)
  871.                 return VA_STATUS_ERROR_INVALID_SURFACE;
  872.  
  873.             /*
  874.              * Sometimes a dummy frame comes from the upper layer
  875.              * library, call i965_check_alloc_surface_bo() to make
  876.              * sure the store buffer is allocated for this reference
  877.              * frame
  878.              */
  879.             va_status = avc_ensure_surface_bo(ctx, decode_state, obj_surface,
  880.                 pic_param);
  881.             if (va_status != VA_STATUS_SUCCESS)
  882.                 return va_status;
  883.         }
  884.         decode_state->reference_objects[i] = obj_surface;
  885.     }
  886.  
  887.     for (j = 0; j < decode_state->num_slice_params; j++) {
  888.         assert(decode_state->slice_params && decode_state->slice_params[j]->buffer);
  889.         slice_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j]->buffer;
  890.  
  891.         if (j == decode_state->num_slice_params - 1)
  892.             next_slice_group_param = NULL;
  893.         else
  894.             next_slice_group_param = (VASliceParameterBufferH264 *)decode_state->slice_params[j + 1]->buffer;
  895.  
  896.         for (i = 0; i < decode_state->slice_params[j]->num_elements; i++) {
  897.  
  898.             if (i < decode_state->slice_params[j]->num_elements - 1)
  899.                 next_slice_param = slice_param + 1;
  900.             else
  901.                 next_slice_param = next_slice_group_param;
  902.  
  903.             if (next_slice_param != NULL) {
  904.                 /* If the mb position of next_slice is less than or equal to the current slice,
  905.                  * discard the current frame.
  906.                  */
  907.                 if (next_slice_param->first_mb_in_slice <= slice_param->first_mb_in_slice) {
  908.                     next_slice_param = NULL;
  909.                     WARN_ONCE("!!!incorrect slice_param. The first_mb_in_slice of next_slice is less"
  910.                                " than or equal to that in current slice\n");
  911.                     goto error;
  912.                 }
  913.             }
  914.         }
  915.     }
  916.  
  917.     return VA_STATUS_SUCCESS;
  918.  
  919. error:
  920.     return VA_STATUS_ERROR_INVALID_PARAMETER;
  921. }
  922.  
  923. static VAStatus
  924. intel_decoder_check_mpeg2_parameter(VADriverContextP ctx,
  925.                                     struct decode_state *decode_state)
  926. {
  927.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  928.     VAPictureParameterBufferMPEG2 *pic_param = (VAPictureParameterBufferMPEG2 *)decode_state->pic_param->buffer;
  929.     struct object_surface *obj_surface;
  930.     int i = 0;
  931.    
  932.     if (pic_param->picture_coding_type == MPEG_I_PICTURE) {
  933.     } else if (pic_param->picture_coding_type == MPEG_P_PICTURE) {
  934.         obj_surface = SURFACE(pic_param->forward_reference_picture);
  935.  
  936.         if (!obj_surface || !obj_surface->bo)
  937.             decode_state->reference_objects[i++] = NULL;
  938.         else
  939.             decode_state->reference_objects[i++] = obj_surface;
  940.     } else if (pic_param->picture_coding_type == MPEG_B_PICTURE) {
  941.         obj_surface = SURFACE(pic_param->forward_reference_picture);
  942.  
  943.         if (!obj_surface || !obj_surface->bo)
  944.             decode_state->reference_objects[i++] = NULL;
  945.         else
  946.             decode_state->reference_objects[i++] = obj_surface;
  947.  
  948.         obj_surface = SURFACE(pic_param->backward_reference_picture);
  949.  
  950.         if (!obj_surface || !obj_surface->bo)
  951.             decode_state->reference_objects[i++] = NULL;
  952.         else
  953.             decode_state->reference_objects[i++] = obj_surface;
  954.     } else
  955.         goto error;
  956.  
  957.     for ( ; i < 16; i++)
  958.         decode_state->reference_objects[i] = NULL;
  959.  
  960.     return VA_STATUS_SUCCESS;
  961.  
  962. error:
  963.     return VA_STATUS_ERROR_INVALID_PARAMETER;
  964. }
  965.  
  966. static VAStatus
  967. intel_decoder_check_vc1_parameter(VADriverContextP ctx,
  968.                                   struct decode_state *decode_state)
  969. {
  970.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  971.     VAPictureParameterBufferVC1 *pic_param = (VAPictureParameterBufferVC1 *)decode_state->pic_param->buffer;
  972.     struct object_surface *obj_surface;
  973.     int i = 0;
  974.  
  975.     if (pic_param->sequence_fields.bits.interlace == 1 &&
  976.         pic_param->picture_fields.bits.frame_coding_mode != 0) { /* frame-interlace or field-interlace */
  977.         return VA_STATUS_ERROR_DECODING_ERROR;
  978.     }
  979.  
  980.     if (pic_param->picture_fields.bits.picture_type == 0 ||
  981.         pic_param->picture_fields.bits.picture_type == 3) {
  982.     } else if (pic_param->picture_fields.bits.picture_type == 1 ||
  983.                pic_param->picture_fields.bits.picture_type == 4) {
  984.         obj_surface = SURFACE(pic_param->forward_reference_picture);
  985.  
  986.         if (!obj_surface || !obj_surface->bo)
  987.             decode_state->reference_objects[i++] = NULL;
  988.         else
  989.             decode_state->reference_objects[i++] = obj_surface;
  990.     } else if (pic_param->picture_fields.bits.picture_type == 2) {
  991.         obj_surface = SURFACE(pic_param->forward_reference_picture);
  992.  
  993.         if (!obj_surface || !obj_surface->bo)
  994.             decode_state->reference_objects[i++] = NULL;
  995.         else
  996.             decode_state->reference_objects[i++] = obj_surface;
  997.  
  998.         obj_surface = SURFACE(pic_param->backward_reference_picture);
  999.  
  1000.         if (!obj_surface || !obj_surface->bo)
  1001.             decode_state->reference_objects[i++] = NULL;
  1002.         else
  1003.             decode_state->reference_objects[i++] = obj_surface;
  1004.     } else
  1005.         goto error;
  1006.  
  1007.     for ( ; i < 16; i++)
  1008.         decode_state->reference_objects[i] = NULL;
  1009.  
  1010.     return VA_STATUS_SUCCESS;
  1011.  
  1012. error:
  1013.     return VA_STATUS_ERROR_INVALID_PARAMETER;
  1014. }
  1015.  
  1016. static VAStatus
  1017. intel_decoder_check_vp8_parameter(VADriverContextP ctx,
  1018.                                   struct decode_state *decode_state)
  1019. {
  1020.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  1021.     VAPictureParameterBufferVP8 *pic_param = (VAPictureParameterBufferVP8 *)decode_state->pic_param->buffer;
  1022.     struct object_surface *obj_surface;
  1023.     int i = 0;
  1024.  
  1025.     if (pic_param->last_ref_frame != VA_INVALID_SURFACE) {
  1026.         obj_surface = SURFACE(pic_param->last_ref_frame);
  1027.  
  1028.         if (obj_surface && obj_surface->bo)
  1029.             decode_state->reference_objects[i++] = obj_surface;
  1030.         else
  1031.             decode_state->reference_objects[i++] = NULL;
  1032.     }
  1033.  
  1034.     if (pic_param->golden_ref_frame != VA_INVALID_SURFACE) {
  1035.         obj_surface = SURFACE(pic_param->golden_ref_frame);
  1036.  
  1037.         if (obj_surface && obj_surface->bo)
  1038.             decode_state->reference_objects[i++] = obj_surface;
  1039.         else
  1040.             decode_state->reference_objects[i++] = NULL;
  1041.     }
  1042.  
  1043.     if (pic_param->alt_ref_frame != VA_INVALID_SURFACE) {
  1044.         obj_surface = SURFACE(pic_param->alt_ref_frame);
  1045.  
  1046.         if (obj_surface && obj_surface->bo)
  1047.             decode_state->reference_objects[i++] = obj_surface;
  1048.         else
  1049.             decode_state->reference_objects[i++] = NULL;
  1050.     }
  1051.  
  1052.     for ( ; i < 16; i++)
  1053.         decode_state->reference_objects[i] = NULL;
  1054.  
  1055.     return VA_STATUS_SUCCESS;
  1056. }
  1057.  
  1058. VAStatus
  1059. intel_decoder_sanity_check_input(VADriverContextP ctx,
  1060.                                  VAProfile profile,
  1061.                                  struct decode_state *decode_state)
  1062. {
  1063.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  1064.     struct object_surface *obj_surface;
  1065.     VAStatus vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
  1066.  
  1067.     if (decode_state->current_render_target == VA_INVALID_SURFACE)
  1068.         goto out;
  1069.        
  1070.     obj_surface = SURFACE(decode_state->current_render_target);
  1071.  
  1072.     if (!obj_surface)
  1073.         goto out;
  1074.  
  1075.     decode_state->render_object = obj_surface;
  1076.  
  1077.     switch (profile) {
  1078.     case VAProfileMPEG2Simple:
  1079.     case VAProfileMPEG2Main:
  1080.         vaStatus = intel_decoder_check_mpeg2_parameter(ctx, decode_state);
  1081.         break;
  1082.        
  1083.     case VAProfileH264ConstrainedBaseline:
  1084.     case VAProfileH264Main:
  1085.     case VAProfileH264High:
  1086.     case VAProfileH264StereoHigh:
  1087.     case VAProfileH264MultiviewHigh:
  1088.         vaStatus = intel_decoder_check_avc_parameter(ctx, profile, decode_state);
  1089.         break;
  1090.  
  1091.     case VAProfileVC1Simple:
  1092.     case VAProfileVC1Main:
  1093.     case VAProfileVC1Advanced:
  1094.         vaStatus = intel_decoder_check_vc1_parameter(ctx, decode_state);
  1095.         break;
  1096.  
  1097.     case VAProfileJPEGBaseline:
  1098.         vaStatus = VA_STATUS_SUCCESS;
  1099.         break;
  1100.  
  1101.     case VAProfileVP8Version0_3:
  1102.         vaStatus = intel_decoder_check_vp8_parameter(ctx, decode_state);
  1103.         break;
  1104.  
  1105.     default:
  1106.         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
  1107.         break;
  1108.     }
  1109.  
  1110. out:
  1111.     return vaStatus;
  1112. }
  1113.  
  1114. /*
  1115.  * Return the next slice paramter
  1116.  *
  1117.  * Input:
  1118.  *      slice_param: the current slice
  1119.  *      *group_idx & *element_idx the current slice position in slice groups
  1120.  * Output:
  1121.  *      Return the next slice parameter
  1122.  *      *group_idx & *element_idx the next slice position in slice groups,
  1123.  *      if the next slice is NULL, *group_idx & *element_idx will be ignored
  1124.  */
  1125. VASliceParameterBufferMPEG2 *
  1126. intel_mpeg2_find_next_slice(struct decode_state *decode_state,
  1127.                             VAPictureParameterBufferMPEG2 *pic_param,
  1128.                             VASliceParameterBufferMPEG2 *slice_param,
  1129.                             int *group_idx,
  1130.                             int *element_idx)
  1131. {
  1132.     VASliceParameterBufferMPEG2 *next_slice_param;
  1133.     unsigned int width_in_mbs = ALIGN(pic_param->horizontal_size, 16) / 16;
  1134.     int j = *group_idx, i = *element_idx + 1;
  1135.  
  1136.     for (; j < decode_state->num_slice_params; j++) {
  1137.         for (; i < decode_state->slice_params[j]->num_elements; i++) {
  1138.             next_slice_param = ((VASliceParameterBufferMPEG2 *)decode_state->slice_params[j]->buffer) + i;
  1139.  
  1140.             if ((next_slice_param->slice_vertical_position * width_in_mbs + next_slice_param->slice_horizontal_position) >=
  1141.                 (slice_param->slice_vertical_position * width_in_mbs + slice_param->slice_horizontal_position)) {
  1142.                 *group_idx = j;
  1143.                 *element_idx = i;
  1144.  
  1145.                 return next_slice_param;
  1146.             }
  1147.         }
  1148.  
  1149.         i = 0;
  1150.     }
  1151.  
  1152.     return NULL;
  1153. }
  1154.  
  1155. /* Ensure the segmentation buffer is large enough for the supplied
  1156.    number of MBs, or re-allocate it */
  1157. bool
  1158. intel_ensure_vp8_segmentation_buffer(VADriverContextP ctx, GenBuffer *buf,
  1159.     unsigned int mb_width, unsigned int mb_height)
  1160. {
  1161.     struct i965_driver_data * const i965 = i965_driver_data(ctx);
  1162.     /* The segmentation map is a 64-byte aligned linear buffer, with
  1163.        each cache line holding only 8 bits for 4 continuous MBs */
  1164.     const unsigned int buf_size = ((mb_width + 3) / 4) * 64 * mb_height;
  1165.  
  1166.     if (buf->valid) {
  1167.         if (buf->bo && buf->bo->size >= buf_size)
  1168.             return true;
  1169.         drm_intel_bo_unreference(buf->bo);
  1170.         buf->valid = false;
  1171.     }
  1172.  
  1173.     buf->bo = drm_intel_bo_alloc(i965->intel.bufmgr, "segmentation map",
  1174.         buf_size, 0x1000);
  1175.     buf->valid = buf->bo != NULL;
  1176.     return buf->valid;
  1177. }
  1178.