Android: gallium_dri: pass dri.sym to linker
[mesa.git] / src / gallium / state_trackers / va / picture.c
index c7c377ad43ecde9e853f73dff9ce512e0117011e..b2be7af8c4699e35215c433151ce19538d60f9b8 100644 (file)
@@ -50,30 +50,40 @@ vlVaBeginPicture(VADriverContextP ctx, VAContextID context_id, VASurfaceID rende
    if (!drv)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
+   mtx_lock(&drv->mutex);
    context = handle_table_get(drv->htab, context_id);
-   if (!context)
+   if (!context) {
+      mtx_unlock(&drv->mutex);
       return VA_STATUS_ERROR_INVALID_CONTEXT;
+   }
 
    surf = handle_table_get(drv->htab, render_target);
+   mtx_unlock(&drv->mutex);
    if (!surf || !surf->buffer)
       return VA_STATUS_ERROR_INVALID_SURFACE;
 
+   context->target_id = render_target;
+   surf->ctx = context_id;
    context->target = surf->buffer;
+   context->mjpeg.sampling_factor = 0;
 
    if (!context->decoder) {
+
       /* VPP */
       if (context->templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN &&
-         ((context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM  &&
-           context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM  &&
-           context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM  &&
-           context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM) ||
-           context->target->interlaced))
+          context->target->buffer_format != PIPE_FORMAT_B8G8R8A8_UNORM &&
+          context->target->buffer_format != PIPE_FORMAT_R8G8B8A8_UNORM &&
+          context->target->buffer_format != PIPE_FORMAT_B8G8R8X8_UNORM &&
+          context->target->buffer_format != PIPE_FORMAT_R8G8B8X8_UNORM &&
+          context->target->buffer_format != PIPE_FORMAT_NV12 &&
+          context->target->buffer_format != PIPE_FORMAT_P016)
          return VA_STATUS_ERROR_UNIMPLEMENTED;
 
       return VA_STATUS_SUCCESS;
    }
 
-   context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
+   if (context->decoder->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE)
+      context->needs_begin_frame = true;
 
    return VA_STATUS_SUCCESS;
 }
@@ -89,11 +99,49 @@ vlVaGetReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
       *ref_frame = NULL;
 }
 
+static void
+getEncParamPreset(vlVaContext *context)
+{
+   //motion estimation preset
+   context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0x00000001;
+   context->desc.h264enc.motion_est.lsmvert = 0x00000002;
+   context->desc.h264enc.motion_est.enc_disable_sub_mode = 0x00000078;
+   context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0x00000001;
+   context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0x00000001;
+   context->desc.h264enc.motion_est.enc_ime2_search_range_x = 0x00000004;
+   context->desc.h264enc.motion_est.enc_ime2_search_range_y = 0x00000004;
+
+   //pic control preset
+   context->desc.h264enc.pic_ctrl.enc_cabac_enable = 0x00000001;
+   context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;
+
+   //rate control
+   context->desc.h264enc.rate_ctrl.vbv_buffer_size = 20000000;
+   context->desc.h264enc.rate_ctrl.vbv_buf_lv = 48;
+   context->desc.h264enc.rate_ctrl.fill_data_enable = 1;
+   context->desc.h264enc.rate_ctrl.enforce_hrd = 1;
+   context->desc.h264enc.enable_vui = false;
+   if (context->desc.h264enc.rate_ctrl.frame_rate_num == 0 ||
+       context->desc.h264enc.rate_ctrl.frame_rate_den == 0) {
+         context->desc.h264enc.rate_ctrl.frame_rate_num = 30;
+         context->desc.h264enc.rate_ctrl.frame_rate_den = 1;
+   }
+   context->desc.h264enc.rate_ctrl.target_bits_picture =
+      context->desc.h264enc.rate_ctrl.target_bitrate *
+      ((float)context->desc.h264enc.rate_ctrl.frame_rate_den /
+      context->desc.h264enc.rate_ctrl.frame_rate_num);
+   context->desc.h264enc.rate_ctrl.peak_bits_picture_integer =
+      context->desc.h264enc.rate_ctrl.peak_bitrate *
+      ((float)context->desc.h264enc.rate_ctrl.frame_rate_den /
+      context->desc.h264enc.rate_ctrl.frame_rate_num);
+
+   context->desc.h264enc.rate_ctrl.peak_bits_picture_fraction = 0;
+   context->desc.h264enc.ref_pic_mode = 0x00000201;
+}
+
 static VAStatus
 handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
 {
-   VAPictureParameterBufferHEVC *hevc;
-   unsigned int i;
    VAStatus vaStatus = VA_STATUS_SUCCESS;
 
    switch (u_reduce_video_profile(context->templat.profile)) {
@@ -114,154 +162,11 @@ handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *
       break;
 
   case PIPE_VIDEO_FORMAT_HEVC:
-      assert(buf->size >= sizeof(VAPictureParameterBufferHEVC) && buf->num_elements == 1);
-      hevc = buf->data;
-      context->desc.h265.pps->sps->chroma_format_idc = hevc->pic_fields.bits.chroma_format_idc;
-      context->desc.h265.pps->sps->separate_colour_plane_flag =
-         hevc->pic_fields.bits.separate_colour_plane_flag;
-      context->desc.h265.pps->sps->pic_width_in_luma_samples = hevc->pic_width_in_luma_samples;
-      context->desc.h265.pps->sps->pic_height_in_luma_samples = hevc->pic_height_in_luma_samples;
-      context->desc.h265.pps->sps->bit_depth_luma_minus8 = hevc->bit_depth_luma_minus8;
-      context->desc.h265.pps->sps->bit_depth_chroma_minus8 = hevc->bit_depth_chroma_minus8;
-      context->desc.h265.pps->sps->log2_max_pic_order_cnt_lsb_minus4 =
-         hevc->log2_max_pic_order_cnt_lsb_minus4;
-      context->desc.h265.pps->sps->sps_max_dec_pic_buffering_minus1 =
-         hevc->sps_max_dec_pic_buffering_minus1;
-      context->desc.h265.pps->sps->log2_min_luma_coding_block_size_minus3 =
-         hevc->log2_min_luma_coding_block_size_minus3;
-      context->desc.h265.pps->sps->log2_diff_max_min_luma_coding_block_size =
-         hevc->log2_diff_max_min_luma_coding_block_size;
-      context->desc.h265.pps->sps->log2_min_transform_block_size_minus2 =
-         hevc->log2_min_transform_block_size_minus2;
-      context->desc.h265.pps->sps->log2_diff_max_min_transform_block_size =
-         hevc->log2_diff_max_min_transform_block_size;
-      context->desc.h265.pps->sps->max_transform_hierarchy_depth_inter =
-         hevc->max_transform_hierarchy_depth_inter;
-      context->desc.h265.pps->sps->max_transform_hierarchy_depth_intra =
-         hevc->max_transform_hierarchy_depth_intra;
-      context->desc.h265.pps->sps->scaling_list_enabled_flag =
-         hevc->pic_fields.bits.scaling_list_enabled_flag;
-      context->desc.h265.pps->sps->amp_enabled_flag = hevc->pic_fields.bits.amp_enabled_flag;
-      context->desc.h265.pps->sps->sample_adaptive_offset_enabled_flag =
-         hevc->slice_parsing_fields.bits.sample_adaptive_offset_enabled_flag;
-      context->desc.h265.pps->sps->pcm_enabled_flag = hevc->pic_fields.bits.pcm_enabled_flag;
-      if (hevc->pic_fields.bits.pcm_enabled_flag == 1) {
-         context->desc.h265.pps->sps->pcm_sample_bit_depth_luma_minus1 =
-            hevc->pcm_sample_bit_depth_luma_minus1;
-         context->desc.h265.pps->sps->pcm_sample_bit_depth_chroma_minus1 =
-            hevc->pcm_sample_bit_depth_chroma_minus1;
-         context->desc.h265.pps->sps->log2_min_pcm_luma_coding_block_size_minus3 =
-            hevc->log2_min_pcm_luma_coding_block_size_minus3;
-         context->desc.h265.pps->sps->log2_diff_max_min_pcm_luma_coding_block_size =
-            hevc->log2_diff_max_min_pcm_luma_coding_block_size;
-         context->desc.h265.pps->sps->pcm_loop_filter_disabled_flag =
-            hevc->pic_fields.bits.pcm_loop_filter_disabled_flag;
-      }
-      context->desc.h265.pps->sps->num_short_term_ref_pic_sets = hevc->num_short_term_ref_pic_sets;
-      context->desc.h265.pps->sps->long_term_ref_pics_present_flag =
-         hevc->slice_parsing_fields.bits.long_term_ref_pics_present_flag;
-      context->desc.h265.pps->sps->num_long_term_ref_pics_sps = hevc->num_long_term_ref_pic_sps;
-      context->desc.h265.pps->sps->sps_temporal_mvp_enabled_flag =
-         hevc->slice_parsing_fields.bits.sps_temporal_mvp_enabled_flag;
-      context->desc.h265.pps->sps->strong_intra_smoothing_enabled_flag =
-         hevc->pic_fields.bits.strong_intra_smoothing_enabled_flag;
-
-      context->desc.h265.pps->dependent_slice_segments_enabled_flag =
-         hevc->slice_parsing_fields.bits.dependent_slice_segments_enabled_flag;
-      context->desc.h265.pps->output_flag_present_flag =
-         hevc->slice_parsing_fields.bits.output_flag_present_flag;
-      context->desc.h265.pps->num_extra_slice_header_bits = hevc->num_extra_slice_header_bits;
-      context->desc.h265.pps->sign_data_hiding_enabled_flag =
-         hevc->pic_fields.bits.sign_data_hiding_enabled_flag;
-      context->desc.h265.pps->cabac_init_present_flag =
-         hevc->slice_parsing_fields.bits.cabac_init_present_flag;
-      context->desc.h265.pps->num_ref_idx_l0_default_active_minus1 =
-         hevc->num_ref_idx_l0_default_active_minus1;
-      context->desc.h265.pps->num_ref_idx_l1_default_active_minus1 =
-         hevc->num_ref_idx_l1_default_active_minus1;
-      context->desc.h265.pps->init_qp_minus26 = hevc->init_qp_minus26;
-      context->desc.h265.pps->constrained_intra_pred_flag =
-         hevc->pic_fields.bits.constrained_intra_pred_flag;
-      context->desc.h265.pps->transform_skip_enabled_flag =
-         hevc->pic_fields.bits.transform_skip_enabled_flag;
-      context->desc.h265.pps->cu_qp_delta_enabled_flag =
-         hevc->pic_fields.bits.cu_qp_delta_enabled_flag;
-      context->desc.h265.pps->diff_cu_qp_delta_depth = hevc->diff_cu_qp_delta_depth;
-      context->desc.h265.pps->pps_cb_qp_offset = hevc->pps_cb_qp_offset;
-      context->desc.h265.pps->pps_cr_qp_offset = hevc->pps_cr_qp_offset;
-      context->desc.h265.pps->pps_slice_chroma_qp_offsets_present_flag =
-         hevc->slice_parsing_fields.bits.pps_slice_chroma_qp_offsets_present_flag;
-      context->desc.h265.pps->weighted_pred_flag = hevc->pic_fields.bits.weighted_pred_flag;
-      context->desc.h265.pps->weighted_bipred_flag = hevc->pic_fields.bits.weighted_bipred_flag;
-      context->desc.h265.pps->transquant_bypass_enabled_flag =
-         hevc->pic_fields.bits.transquant_bypass_enabled_flag;
-      context->desc.h265.pps->tiles_enabled_flag = hevc->pic_fields.bits.tiles_enabled_flag;
-      context->desc.h265.pps->entropy_coding_sync_enabled_flag =
-         hevc->pic_fields.bits.entropy_coding_sync_enabled_flag;
-      if (hevc->pic_fields.bits.tiles_enabled_flag == 1) {
-         context->desc.h265.pps->num_tile_columns_minus1 = hevc->num_tile_columns_minus1;
-         context->desc.h265.pps->num_tile_rows_minus1 = hevc->num_tile_rows_minus1;
-         for (i = 0 ; i < 19 ; i++)
-            context->desc.h265.pps->column_width_minus1[i] = hevc->column_width_minus1[i];
-         for (i = 0 ; i < 21 ; i++)
-            context->desc.h265.pps->row_height_minus1[i] = hevc->row_height_minus1[i];
-         context->desc.h265.pps->loop_filter_across_tiles_enabled_flag =
-            hevc->pic_fields.bits.loop_filter_across_tiles_enabled_flag;
-      }
-      context->desc.h265.pps->pps_loop_filter_across_slices_enabled_flag =
-         hevc->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag;
-      context->desc.h265.pps->deblocking_filter_override_enabled_flag =
-         hevc->slice_parsing_fields.bits.deblocking_filter_override_enabled_flag;
-      context->desc.h265.pps->pps_deblocking_filter_disabled_flag =
-         hevc->slice_parsing_fields.bits.pps_disable_deblocking_filter_flag;
-      context->desc.h265.pps->pps_beta_offset_div2 = hevc->pps_beta_offset_div2;
-      context->desc.h265.pps->pps_tc_offset_div2 = hevc->pps_tc_offset_div2;
-      context->desc.h265.pps->lists_modification_present_flag =
-         hevc->slice_parsing_fields.bits.lists_modification_present_flag;
-      context->desc.h265.pps->log2_parallel_merge_level_minus2 =
-         hevc->log2_parallel_merge_level_minus2;
-      context->desc.h265.pps->slice_segment_header_extension_present_flag =
-         hevc->slice_parsing_fields.bits.slice_segment_header_extension_present_flag;
-
-      context->desc.h265.IDRPicFlag = hevc->slice_parsing_fields.bits.IdrPicFlag;
-      context->desc.h265.RAPPicFlag = hevc->slice_parsing_fields.bits.RapPicFlag;
-
-      context->desc.h265.CurrPicOrderCntVal = hevc->CurrPic.pic_order_cnt;
-
-      for (i = 0 ; i < 8 ; i++) {
-         context->desc.h265.RefPicSetStCurrBefore[i] = 0xFF;
-         context->desc.h265.RefPicSetStCurrAfter[i] = 0xFF;
-         context->desc.h265.RefPicSetLtCurr[i] = 0xFF;
-      }
-      context->desc.h265.NumPocStCurrBefore = 0;
-      context->desc.h265.NumPocStCurrAfter = 0;
-      context->desc.h265.NumPocLtCurr = 0;
-      unsigned int iBefore = 0;
-      unsigned int iAfter = 0;
-      unsigned int iCurr = 0;
-      for (i = 0 ; i < 15 ; i++) {
-         context->desc.h265.PicOrderCntVal[i] = hevc->ReferenceFrames[i].pic_order_cnt;
-
-         unsigned int index = hevc->ReferenceFrames[i].picture_id & 0x7F;
-
-         if (index == 0x7F)
-            continue;
-
-         vlVaGetReferenceFrame(drv, hevc->ReferenceFrames[i].picture_id, &context->desc.h265.ref[i]);
+      vlVaHandlePictureParameterBufferHEVC(drv, context, buf);
+      break;
 
-         if ((hevc->ReferenceFrames[i].flags & VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE) && (iBefore < 8)) {
-            context->desc.h265.RefPicSetStCurrBefore[iBefore++] = i;
-            context->desc.h265.NumPocStCurrBefore++;
-         }
-         if ((hevc->ReferenceFrames[i].flags & VA_PICTURE_HEVC_RPS_ST_CURR_AFTER) && (iAfter < 8)) {
-            context->desc.h265.RefPicSetStCurrAfter[iAfter++] = i;
-            context->desc.h265.NumPocStCurrAfter++;
-         }
-         if ((hevc->ReferenceFrames[i].flags & VA_PICTURE_HEVC_RPS_LT_CURR) && (iCurr < 8)) {
-            context->desc.h265.RefPicSetLtCurr[iCurr++] = i;
-            context->desc.h265.NumPocLtCurr++;
-         }
-      }
+  case PIPE_VIDEO_FORMAT_JPEG:
+      vlVaHandlePictureParameterBufferMJPEG(drv, context, buf);
       break;
 
    default:
@@ -270,14 +175,17 @@ handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *
 
    /* Create the decoder once max_references is known. */
    if (!context->decoder) {
+      enum pipe_video_format format =
+         u_reduce_video_profile(context->templat.profile);
+
       if (!context->target)
          return VA_STATUS_ERROR_INVALID_CONTEXT;
 
-      if (context->templat.max_references == 0)
+      if (context->templat.max_references == 0 &&
+         format != PIPE_VIDEO_FORMAT_JPEG)
          return VA_STATUS_ERROR_INVALID_BUFFER;
 
-      if (u_reduce_video_profile(context->templat.profile) ==
-          PIPE_VIDEO_FORMAT_MPEG4_AVC)
+      if (format == PIPE_VIDEO_FORMAT_MPEG4_AVC)
          context->templat.level = u_get_h264_level(context->templat.width,
             context->templat.height, &context->templat.max_references);
 
@@ -287,8 +195,7 @@ handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *
       if (!context->decoder)
          return VA_STATUS_ERROR_ALLOCATION_FAILED;
 
-      context->decoder->begin_frame(context->decoder, context->target,
-         &context->desc.base);
+      context->needs_begin_frame = true;
    }
 
    return vaStatus;
@@ -297,8 +204,6 @@ handlePictureParameterBuffer(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *
 static void
 handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
 {
-   VAIQMatrixBufferHEVC *h265;
-
    switch (u_reduce_video_profile(context->templat.profile)) {
    case PIPE_VIDEO_FORMAT_MPEG12:
       vlVaHandleIQMatrixBufferMPEG12(context, buf);
@@ -313,14 +218,11 @@ handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
       break;
 
    case PIPE_VIDEO_FORMAT_HEVC:
-      assert(buf->size >= sizeof(VAIQMatrixBufferH264) && buf->num_elements == 1);
-      h265 = buf->data;
-      memcpy(&context->desc.h265.pps->sps->ScalingList4x4, h265->ScalingList4x4, 6 * 16);
-      memcpy(&context->desc.h265.pps->sps->ScalingList8x8, h265->ScalingList8x8, 6 * 64);
-      memcpy(&context->desc.h265.pps->sps->ScalingList16x16, h265->ScalingList16x16, 6 * 64);
-      memcpy(&context->desc.h265.pps->sps->ScalingList32x32, h265->ScalingList32x32, 2 * 64);
-      memcpy(&context->desc.h265.pps->sps->ScalingListDCCoeff16x16, h265->ScalingListDC16x16, 6);
-      memcpy(&context->desc.h265.pps->sps->ScalingListDCCoeff32x32, h265->ScalingListDC32x32, 2);
+      vlVaHandleIQMatrixBufferHEVC(context, buf);
+      break;
+
+   case PIPE_VIDEO_FORMAT_JPEG:
+      vlVaHandleIQMatrixBufferMJPEG(context, buf);
       break;
 
    default:
@@ -331,9 +233,15 @@ handleIQMatrixBuffer(vlVaContext *context, vlVaBuffer *buf)
 static void
 handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
 {
-   VASliceParameterBufferHEVC *h265;
-
    switch (u_reduce_video_profile(context->templat.profile)) {
+   case PIPE_VIDEO_FORMAT_MPEG12:
+      vlVaHandleSliceParameterBufferMPEG12(context, buf);
+      break;
+
+   case PIPE_VIDEO_FORMAT_VC1:
+      vlVaHandleSliceParameterBufferVC1(context, buf);
+      break;
+
    case PIPE_VIDEO_FORMAT_MPEG4_AVC:
       vlVaHandleSliceParameterBufferH264(context, buf);
       break;
@@ -343,14 +251,13 @@ handleSliceParameterBuffer(vlVaContext *context, vlVaBuffer *buf)
       break;
 
    case PIPE_VIDEO_FORMAT_HEVC:
-      assert(buf->size >= sizeof(VASliceParameterBufferHEVC) && buf->num_elements == 1);
-      h265 = buf->data;
-      for (int i = 0 ; i < 2 ; i++) {
-         for (int j = 0 ; j < 15 ; j++)
-            context->desc.h265.RefPicList[i][j] = h265->RefPicList[i][j];
-      }
-      context->desc.h265.UseRefPicList = true;
+      vlVaHandleSliceParameterBufferHEVC(context, buf);
+      break;
+
+   case PIPE_VIDEO_FORMAT_JPEG:
+      vlVaHandleSliceParameterBufferMJPEG(context, buf);
       break;
+
    default:
       break;
    }
@@ -419,6 +326,9 @@ handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
       vlVaDecoderFixMPEG4Startcode(context);
       buffers[num_buffers] = (void *)context->mpeg4.start_code;
       sizes[num_buffers++] = context->mpeg4.start_code_size;
+   case PIPE_VIDEO_FORMAT_JPEG:
+      /* TODO */
+      break;
    default:
       break;
    }
@@ -426,10 +336,173 @@ handleVASliceDataBufferType(vlVaContext *context, vlVaBuffer *buf)
    buffers[num_buffers] = buf->data;
    sizes[num_buffers] = buf->size;
    ++num_buffers;
+
+   if (context->needs_begin_frame) {
+      context->decoder->begin_frame(context->decoder, context->target,
+         &context->desc.base);
+      context->needs_begin_frame = false;
+   }
    context->decoder->decode_bitstream(context->decoder, context->target, &context->desc.base,
       num_buffers, (const void * const*)buffers, sizes);
 }
 
+static VAStatus
+handleVAEncMiscParameterTypeRateControl(vlVaContext *context, VAEncMiscParameterBuffer *misc)
+{
+   VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
+   if (context->desc.h264enc.rate_ctrl.rate_ctrl_method ==
+       PIPE_H264_ENC_RATE_CONTROL_METHOD_CONSTANT)
+      context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second;
+   else
+      context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second * (rc->target_percentage / 100.0);
+   context->desc.h264enc.rate_ctrl.peak_bitrate = rc->bits_per_second;
+   if (context->desc.h264enc.rate_ctrl.target_bitrate < 2000000)
+      context->desc.h264enc.rate_ctrl.vbv_buffer_size = MIN2((context->desc.h264enc.rate_ctrl.target_bitrate * 2.75), 2000000);
+   else
+      context->desc.h264enc.rate_ctrl.vbv_buffer_size = context->desc.h264enc.rate_ctrl.target_bitrate;
+
+   return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+handleVAEncMiscParameterTypeFrameRate(vlVaContext *context, VAEncMiscParameterBuffer *misc)
+{
+   VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
+   if (fr->framerate & 0xffff0000) {
+      context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate       & 0xffff;
+      context->desc.h264enc.rate_ctrl.frame_rate_den = fr->framerate >> 16 & 0xffff;
+   } else {
+      context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate;
+      context->desc.h264enc.rate_ctrl.frame_rate_den = 1;
+   }
+   return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+handleVAEncSequenceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
+{
+   VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;
+   if (!context->decoder) {
+      context->templat.max_references = h264->max_num_ref_frames;
+      context->templat.level = h264->level_idc;
+      context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
+      if (!context->decoder)
+         return VA_STATUS_ERROR_ALLOCATION_FAILED;
+   }
+
+   context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;
+   if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
+      context->gop_coeff = VL_VA_ENC_GOP_COEFF;
+   context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;
+   context->desc.h264enc.rate_ctrl.frame_rate_num = h264->time_scale / 2;
+   context->desc.h264enc.rate_ctrl.frame_rate_den = h264->num_units_in_tick;
+   return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+handleVAEncMiscParameterBufferType(vlVaContext *context, vlVaBuffer *buf)
+{
+   VAStatus vaStatus = VA_STATUS_SUCCESS;
+   VAEncMiscParameterBuffer *misc;
+   misc = buf->data;
+
+   switch (misc->type) {
+   case VAEncMiscParameterTypeRateControl:
+      vaStatus = handleVAEncMiscParameterTypeRateControl(context, misc);
+      break;
+
+   case VAEncMiscParameterTypeFrameRate:
+      vaStatus = handleVAEncMiscParameterTypeFrameRate(context, misc);
+      break;
+
+   default:
+      break;
+   }
+
+   return vaStatus;
+}
+
+static VAStatus
+handleVAEncPictureParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
+{
+   VAEncPictureParameterBufferH264 *h264;
+   vlVaBuffer *coded_buf;
+
+   h264 = buf->data;
+   context->desc.h264enc.frame_num = h264->frame_num;
+   context->desc.h264enc.not_referenced = false;
+   context->desc.h264enc.is_idr = (h264->pic_fields.bits.idr_pic_flag == 1);
+   context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
+   if (context->desc.h264enc.gop_cnt == 0)
+      context->desc.h264enc.i_remain = context->gop_coeff;
+   else if (context->desc.h264enc.frame_num == 1)
+      context->desc.h264enc.i_remain--;
+
+   context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
+
+   coded_buf = handle_table_get(drv->htab, h264->coded_buf);
+   if (!coded_buf->derived_surface.resource)
+      coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
+                                            PIPE_USAGE_STREAM, coded_buf->size);
+   context->coded_buf = coded_buf;
+
+   util_hash_table_set(context->desc.h264enc.frame_idx,
+                      UINT_TO_PTR(h264->CurrPic.picture_id),
+                      UINT_TO_PTR(h264->frame_num));
+
+   if (context->desc.h264enc.is_idr)
+      context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
+   else
+      context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
+
+   context->desc.h264enc.quant_i_frames = h264->pic_init_qp;
+   context->desc.h264enc.quant_b_frames = h264->pic_init_qp;
+   context->desc.h264enc.quant_p_frames = h264->pic_init_qp;
+   context->desc.h264enc.gop_cnt++;
+   if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
+      context->desc.h264enc.gop_cnt = 0;
+
+   return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+handleVAEncSliceParameterBufferType(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
+{
+   VAEncSliceParameterBufferH264 *h264;
+
+   h264 = buf->data;
+   context->desc.h264enc.ref_idx_l0 = VA_INVALID_ID;
+   context->desc.h264enc.ref_idx_l1 = VA_INVALID_ID;
+
+   for (int i = 0; i < 32; i++) {
+      if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
+         if (context->desc.h264enc.ref_idx_l0 == VA_INVALID_ID)
+            context->desc.h264enc.ref_idx_l0 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
+                                                                              UINT_TO_PTR(h264->RefPicList0[i].picture_id)));
+      }
+      if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
+         if (context->desc.h264enc.ref_idx_l1 == VA_INVALID_ID)
+            context->desc.h264enc.ref_idx_l1 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
+                                                                              UINT_TO_PTR(h264->RefPicList1[i].picture_id)));
+      }
+   }
+
+   if (h264->slice_type == 1)
+      context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_B;
+   else if (h264->slice_type == 0)
+      context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_P;
+   else if (h264->slice_type == 2) {
+      if (context->desc.h264enc.is_idr){
+         context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_IDR;
+         context->desc.h264enc.idr_pic_id++;
+          } else
+         context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_I;
+   } else
+      context->desc.h264enc.picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
+
+   return VA_STATUS_SUCCESS;
+}
+
 VAStatus
 vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buffers, int num_buffers)
 {
@@ -446,14 +519,19 @@ vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buff
    if (!drv)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
+   mtx_lock(&drv->mutex);
    context = handle_table_get(drv->htab, context_id);
-   if (!context)
+   if (!context) {
+      mtx_unlock(&drv->mutex);
       return VA_STATUS_ERROR_INVALID_CONTEXT;
+   }
 
    for (i = 0; i < num_buffers; ++i) {
       vlVaBuffer *buf = handle_table_get(drv->htab, buffers[i]);
-      if (!buf)
+      if (!buf) {
+         mtx_unlock(&drv->mutex);
          return VA_STATUS_ERROR_INVALID_BUFFER;
+      }
 
       switch (buf->type) {
       case VAPictureParameterBufferType:
@@ -475,10 +553,31 @@ vlVaRenderPicture(VADriverContextP ctx, VAContextID context_id, VABufferID *buff
          vaStatus = vlVaHandleVAProcPipelineParameterBufferType(drv, context, buf);
          break;
 
+      case VAEncSequenceParameterBufferType:
+         vaStatus = handleVAEncSequenceParameterBufferType(drv, context, buf);
+         break;
+
+      case VAEncMiscParameterBufferType:
+         vaStatus = handleVAEncMiscParameterBufferType(context, buf);
+         break;
+
+      case VAEncPictureParameterBufferType:
+         vaStatus = handleVAEncPictureParameterBufferType(drv, context, buf);
+         break;
+
+      case VAEncSliceParameterBufferType:
+         vaStatus = handleVAEncSliceParameterBufferType(drv, context, buf);
+         break;
+
+      case VAHuffmanTableBufferType:
+         vlVaHandleHuffmanTableBufferType(context, buf);
+         break;
+
       default:
          break;
       }
    }
+   mtx_unlock(&drv->mutex);
 
    return vaStatus;
 }
@@ -488,6 +587,13 @@ vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
 {
    vlVaDriver *drv;
    vlVaContext *context;
+   vlVaBuffer *coded_buf;
+   vlVaSurface *surf;
+   void *feedback;
+   struct pipe_screen *screen;
+   bool interlaced;
+   bool realloc = false;
+   enum pipe_format format;
 
    if (!ctx)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
@@ -496,7 +602,9 @@ vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
    if (!drv)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
+   mtx_lock(&drv->mutex);
    context = handle_table_get(drv->htab, context_id);
+   mtx_unlock(&drv->mutex);
    if (!context)
       return VA_STATUS_ERROR_INVALID_CONTEXT;
 
@@ -508,8 +616,89 @@ vlVaEndPicture(VADriverContextP ctx, VAContextID context_id)
       return VA_STATUS_SUCCESS;
    }
 
+   mtx_lock(&drv->mutex);
+   surf = handle_table_get(drv->htab, context->target_id);
    context->mpeg4.frame_num++;
-   context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
 
+   screen = context->decoder->context->screen;
+   interlaced = screen->get_video_param(screen, context->decoder->profile,
+                                        PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+                                        PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);
+
+   if (surf->buffer->interlaced != interlaced) {
+      surf->templat.interlaced = screen->get_video_param(screen, context->decoder->profile,
+                                                         PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+                                                         PIPE_VIDEO_CAP_PREFERS_INTERLACED);
+      realloc = true;
+   }
+
+   format = screen->get_video_param(screen, context->decoder->profile,
+                                    PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
+                                    PIPE_VIDEO_CAP_PREFERED_FORMAT);
+
+   if (surf->buffer->buffer_format != format &&
+       surf->buffer->buffer_format == PIPE_FORMAT_NV12) {
+      /* check originally as NV12 only */
+      surf->templat.buffer_format = format;
+      realloc = true;
+   }
+
+   if (u_reduce_video_profile(context->templat.profile) == PIPE_VIDEO_FORMAT_JPEG &&
+       surf->buffer->buffer_format == PIPE_FORMAT_NV12) {
+      if (context->mjpeg.sampling_factor == 0x211111 ||
+          context->mjpeg.sampling_factor == 0x221212) {
+         surf->templat.buffer_format = PIPE_FORMAT_YUYV;
+         realloc = true;
+      } else if (context->mjpeg.sampling_factor != 0x221111) {
+         /* Not NV12 either */
+         mtx_unlock(&drv->mutex);
+         return VA_STATUS_ERROR_INVALID_SURFACE;
+      }
+   }
+
+   if (realloc) {
+      surf->buffer->destroy(surf->buffer);
+
+      if (vlVaHandleSurfaceAllocate(ctx, surf, &surf->templat) != VA_STATUS_SUCCESS) {
+         mtx_unlock(&drv->mutex);
+         return VA_STATUS_ERROR_ALLOCATION_FAILED;
+      }
+
+      context->target = surf->buffer;
+   }
+
+   if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
+      coded_buf = context->coded_buf;
+      getEncParamPreset(context);
+      context->desc.h264enc.frame_num_cnt++;
+      context->decoder->begin_frame(context->decoder, context->target, &context->desc.base);
+      context->decoder->encode_bitstream(context->decoder, context->target,
+                                         coded_buf->derived_surface.resource, &feedback);
+      surf->feedback = feedback;
+      surf->coded_buf = coded_buf;
+   }
+
+   context->decoder->end_frame(context->decoder, context->target, &context->desc.base);
+   if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
+      int idr_period = context->desc.h264enc.gop_size / context->gop_coeff;
+      int p_remain_in_idr = idr_period - context->desc.h264enc.frame_num;
+      surf->frame_num_cnt = context->desc.h264enc.frame_num_cnt;
+      surf->force_flushed = false;
+      if (context->first_single_submitted) {
+         context->decoder->flush(context->decoder);
+         context->first_single_submitted = false;
+         surf->force_flushed = true;
+      }
+      if (p_remain_in_idr == 1) {
+         if ((context->desc.h264enc.frame_num_cnt % 2) != 0) {
+            context->decoder->flush(context->decoder);
+            context->first_single_submitted = true;
+         }
+         else
+            context->first_single_submitted = false;
+         surf->force_flushed = true;
+      }
+   }
+   mtx_unlock(&drv->mutex);
    return VA_STATUS_SUCCESS;
 }