00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "vaapi_internal.h"
00024 #include "h264.h"
00025
00036 static void init_vaapi_pic(VAPictureH264 *va_pic)
00037 {
00038 va_pic->picture_id = VA_INVALID_ID;
00039 va_pic->flags = VA_PICTURE_H264_INVALID;
00040 va_pic->TopFieldOrderCnt = 0;
00041 va_pic->BottomFieldOrderCnt = 0;
00042 }
00043
00052 static void fill_vaapi_pic(VAPictureH264 *va_pic,
00053 Picture *pic,
00054 int pic_structure)
00055 {
00056 if (pic_structure == 0)
00057 pic_structure = pic->reference;
00058 pic_structure &= PICT_FRAME;
00059
00060 va_pic->picture_id = ff_vaapi_get_surface_id(pic);
00061 va_pic->frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
00062
00063 va_pic->flags = 0;
00064 if (pic_structure != PICT_FRAME)
00065 va_pic->flags |= (pic_structure & PICT_TOP_FIELD) ? VA_PICTURE_H264_TOP_FIELD : VA_PICTURE_H264_BOTTOM_FIELD;
00066 if (pic->reference)
00067 va_pic->flags |= pic->long_ref ? VA_PICTURE_H264_LONG_TERM_REFERENCE : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
00068
00069 va_pic->TopFieldOrderCnt = 0;
00070 if (pic->field_poc[0] != INT_MAX)
00071 va_pic->TopFieldOrderCnt = pic->field_poc[0];
00072
00073 va_pic->BottomFieldOrderCnt = 0;
00074 if (pic->field_poc[1] != INT_MAX)
00075 va_pic->BottomFieldOrderCnt = pic->field_poc[1];
00076 }
00077
00079 typedef struct DPB {
00080 int size;
00081 int max_size;
00082 VAPictureH264 *va_pics;
00083 } DPB;
00084
00091 static int dpb_add(DPB *dpb, Picture *pic)
00092 {
00093 int i;
00094
00095 if (dpb->size >= dpb->max_size)
00096 return -1;
00097
00098 for (i = 0; i < dpb->size; i++) {
00099 VAPictureH264 * const va_pic = &dpb->va_pics[i];
00100 if (va_pic->picture_id == ff_vaapi_get_surface_id(pic)) {
00101 VAPictureH264 temp_va_pic;
00102 fill_vaapi_pic(&temp_va_pic, pic, 0);
00103
00104 if ((temp_va_pic.flags ^ va_pic->flags) & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD)) {
00105 va_pic->flags |= temp_va_pic.flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
00106
00107 if (temp_va_pic.flags & VA_PICTURE_H264_TOP_FIELD) {
00108 va_pic->TopFieldOrderCnt = temp_va_pic.TopFieldOrderCnt;
00109 } else {
00110 va_pic->BottomFieldOrderCnt = temp_va_pic.BottomFieldOrderCnt;
00111 }
00112 }
00113 return 0;
00114 }
00115 }
00116
00117 fill_vaapi_pic(&dpb->va_pics[dpb->size++], pic, 0);
00118 return 0;
00119 }
00120
00122 static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param,
00123 H264Context *h)
00124 {
00125 DPB dpb;
00126 int i;
00127
00128 dpb.size = 0;
00129 dpb.max_size = FF_ARRAY_ELEMS(pic_param->ReferenceFrames);
00130 dpb.va_pics = pic_param->ReferenceFrames;
00131 for (i = 0; i < dpb.max_size; i++)
00132 init_vaapi_pic(&dpb.va_pics[i]);
00133
00134 for (i = 0; i < h->short_ref_count; i++) {
00135 Picture * const pic = h->short_ref[i];
00136 if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
00137 return -1;
00138 }
00139
00140 for (i = 0; i < 16; i++) {
00141 Picture * const pic = h->long_ref[i];
00142 if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
00143 return -1;
00144 }
00145 return 0;
00146 }
00147
00156 static void fill_vaapi_RefPicList(VAPictureH264 RefPicList[32],
00157 Picture *ref_list,
00158 unsigned int ref_count)
00159 {
00160 unsigned int i, n = 0;
00161 for (i = 0; i < ref_count; i++)
00162 if (ref_list[i].reference)
00163 fill_vaapi_pic(&RefPicList[n++], &ref_list[i], 0);
00164
00165 for (; n < 32; n++)
00166 init_vaapi_pic(&RefPicList[n]);
00167 }
00168
00184 static void fill_vaapi_plain_pred_weight_table(H264Context *h,
00185 int list,
00186 unsigned char *luma_weight_flag,
00187 short luma_weight[32],
00188 short luma_offset[32],
00189 unsigned char *chroma_weight_flag,
00190 short chroma_weight[32][2],
00191 short chroma_offset[32][2])
00192 {
00193 unsigned int i, j;
00194
00195 *luma_weight_flag = h->luma_weight_flag[list];
00196 *chroma_weight_flag = h->chroma_weight_flag[list];
00197
00198 for (i = 0; i < h->ref_count[list]; i++) {
00199
00200
00201 if (h->luma_weight_flag[list]) {
00202 luma_weight[i] = h->luma_weight[i][list][0];
00203 luma_offset[i] = h->luma_weight[i][list][1];
00204 } else {
00205 luma_weight[i] = 1 << h->luma_log2_weight_denom;
00206 luma_offset[i] = 0;
00207 }
00208 for (j = 0; j < 2; j++) {
00209 if (h->chroma_weight_flag[list]) {
00210 chroma_weight[i][j] = h->chroma_weight[i][list][j][0];
00211 chroma_offset[i][j] = h->chroma_weight[i][list][j][1];
00212 } else {
00213 chroma_weight[i][j] = 1 << h->chroma_log2_weight_denom;
00214 chroma_offset[i][j] = 0;
00215 }
00216 }
00217 }
00218 }
00219
00221 static int start_frame(AVCodecContext *avctx,
00222 av_unused const uint8_t *buffer,
00223 av_unused uint32_t size)
00224 {
00225 H264Context * const h = avctx->priv_data;
00226 MpegEncContext * const s = &h->s;
00227 struct vaapi_context * const vactx = avctx->hwaccel_context;
00228 VAPictureParameterBufferH264 *pic_param;
00229 VAIQMatrixBufferH264 *iq_matrix;
00230
00231 av_dlog(avctx, "start_frame()\n");
00232
00233 vactx->slice_param_size = sizeof(VASliceParameterBufferH264);
00234
00235
00236 pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferH264));
00237 if (!pic_param)
00238 return -1;
00239 fill_vaapi_pic(&pic_param->CurrPic, s->current_picture_ptr, s->picture_structure);
00240 if (fill_vaapi_ReferenceFrames(pic_param, h) < 0)
00241 return -1;
00242 pic_param->picture_width_in_mbs_minus1 = s->mb_width - 1;
00243 pic_param->picture_height_in_mbs_minus1 = s->mb_height - 1;
00244 pic_param->bit_depth_luma_minus8 = h->sps.bit_depth_luma - 8;
00245 pic_param->bit_depth_chroma_minus8 = h->sps.bit_depth_chroma - 8;
00246 pic_param->num_ref_frames = h->sps.ref_frame_count;
00247 pic_param->seq_fields.value = 0;
00248 pic_param->seq_fields.bits.chroma_format_idc = h->sps.chroma_format_idc;
00249 pic_param->seq_fields.bits.residual_colour_transform_flag = h->sps.residual_color_transform_flag;
00250 pic_param->seq_fields.bits.gaps_in_frame_num_value_allowed_flag = h->sps.gaps_in_frame_num_allowed_flag;
00251 pic_param->seq_fields.bits.frame_mbs_only_flag = h->sps.frame_mbs_only_flag;
00252 pic_param->seq_fields.bits.mb_adaptive_frame_field_flag = h->sps.mb_aff;
00253 pic_param->seq_fields.bits.direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
00254 pic_param->seq_fields.bits.MinLumaBiPredSize8x8 = h->sps.level_idc >= 31;
00255 pic_param->seq_fields.bits.log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
00256 pic_param->seq_fields.bits.pic_order_cnt_type = h->sps.poc_type;
00257 pic_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
00258 pic_param->seq_fields.bits.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
00259 pic_param->num_slice_groups_minus1 = h->pps.slice_group_count - 1;
00260 pic_param->slice_group_map_type = h->pps.mb_slice_group_map_type;
00261 pic_param->slice_group_change_rate_minus1 = 0;
00262 pic_param->pic_init_qp_minus26 = h->pps.init_qp - 26;
00263 pic_param->pic_init_qs_minus26 = h->pps.init_qs - 26;
00264 pic_param->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
00265 pic_param->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
00266 pic_param->pic_fields.value = 0;
00267 pic_param->pic_fields.bits.entropy_coding_mode_flag = h->pps.cabac;
00268 pic_param->pic_fields.bits.weighted_pred_flag = h->pps.weighted_pred;
00269 pic_param->pic_fields.bits.weighted_bipred_idc = h->pps.weighted_bipred_idc;
00270 pic_param->pic_fields.bits.transform_8x8_mode_flag = h->pps.transform_8x8_mode;
00271 pic_param->pic_fields.bits.field_pic_flag = s->picture_structure != PICT_FRAME;
00272 pic_param->pic_fields.bits.constrained_intra_pred_flag = h->pps.constrained_intra_pred;
00273 pic_param->pic_fields.bits.pic_order_present_flag = h->pps.pic_order_present;
00274 pic_param->pic_fields.bits.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
00275 pic_param->pic_fields.bits.redundant_pic_cnt_present_flag = h->pps.redundant_pic_cnt_present;
00276 pic_param->pic_fields.bits.reference_pic_flag = h->nal_ref_idc != 0;
00277 pic_param->frame_num = h->frame_num;
00278
00279
00280 iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferH264));
00281 if (!iq_matrix)
00282 return -1;
00283 memcpy(iq_matrix->ScalingList4x4, h->pps.scaling_matrix4, sizeof(iq_matrix->ScalingList4x4));
00284 memcpy(iq_matrix->ScalingList8x8[0], h->pps.scaling_matrix8[0], sizeof(iq_matrix->ScalingList8x8[0]));
00285 memcpy(iq_matrix->ScalingList8x8[1], h->pps.scaling_matrix8[3], sizeof(iq_matrix->ScalingList8x8[0]));
00286 return 0;
00287 }
00288
00290 static int end_frame(AVCodecContext *avctx)
00291 {
00292 H264Context * const h = avctx->priv_data;
00293
00294 av_dlog(avctx, "end_frame()\n");
00295 return ff_vaapi_common_end_frame(&h->s);
00296 }
00297
00299 static int decode_slice(AVCodecContext *avctx,
00300 const uint8_t *buffer,
00301 uint32_t size)
00302 {
00303 H264Context * const h = avctx->priv_data;
00304 MpegEncContext * const s = &h->s;
00305 VASliceParameterBufferH264 *slice_param;
00306
00307 av_dlog(avctx, "decode_slice(): buffer %p, size %d\n", buffer, size);
00308
00309
00310 slice_param = (VASliceParameterBufferH264 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
00311 if (!slice_param)
00312 return -1;
00313 slice_param->slice_data_bit_offset = get_bits_count(&h->s.gb) + 8;
00314 slice_param->first_mb_in_slice = (s->mb_y >> FIELD_OR_MBAFF_PICTURE) * s->mb_width + s->mb_x;
00315 slice_param->slice_type = ff_h264_get_slice_type(h);
00316 slice_param->direct_spatial_mv_pred_flag = h->slice_type == AV_PICTURE_TYPE_B ? h->direct_spatial_mv_pred : 0;
00317 slice_param->num_ref_idx_l0_active_minus1 = h->list_count > 0 ? h->ref_count[0] - 1 : 0;
00318 slice_param->num_ref_idx_l1_active_minus1 = h->list_count > 1 ? h->ref_count[1] - 1 : 0;
00319 slice_param->cabac_init_idc = h->cabac_init_idc;
00320 slice_param->slice_qp_delta = s->qscale - h->pps.init_qp;
00321 slice_param->disable_deblocking_filter_idc = h->deblocking_filter < 2 ? !h->deblocking_filter : h->deblocking_filter;
00322 slice_param->slice_alpha_c0_offset_div2 = h->slice_alpha_c0_offset / 2 - 26;
00323 slice_param->slice_beta_offset_div2 = h->slice_beta_offset / 2 - 26;
00324 slice_param->luma_log2_weight_denom = h->luma_log2_weight_denom;
00325 slice_param->chroma_log2_weight_denom = h->chroma_log2_weight_denom;
00326
00327 fill_vaapi_RefPicList(slice_param->RefPicList0, h->ref_list[0], h->list_count > 0 ? h->ref_count[0] : 0);
00328 fill_vaapi_RefPicList(slice_param->RefPicList1, h->ref_list[1], h->list_count > 1 ? h->ref_count[1] : 0);
00329
00330 fill_vaapi_plain_pred_weight_table(h, 0,
00331 &slice_param->luma_weight_l0_flag, slice_param->luma_weight_l0, slice_param->luma_offset_l0,
00332 &slice_param->chroma_weight_l0_flag, slice_param->chroma_weight_l0, slice_param->chroma_offset_l0);
00333 fill_vaapi_plain_pred_weight_table(h, 1,
00334 &slice_param->luma_weight_l1_flag, slice_param->luma_weight_l1, slice_param->luma_offset_l1,
00335 &slice_param->chroma_weight_l1_flag, slice_param->chroma_weight_l1, slice_param->chroma_offset_l1);
00336 return 0;
00337 }
00338
00339 AVHWAccel ff_h264_vaapi_hwaccel = {
00340 .name = "h264_vaapi",
00341 .type = AVMEDIA_TYPE_VIDEO,
00342 .id = CODEC_ID_H264,
00343 .pix_fmt = PIX_FMT_VAAPI_VLD,
00344 .capabilities = 0,
00345 .start_frame = start_frame,
00346 .end_frame = end_frame,
00347 .decode_slice = decode_slice,
00348 .priv_data_size = 0,
00349 };