• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/vaapi_h264.c

Go to the documentation of this file.
00001 /*
00002  * H.264 HW decode acceleration through VA API
00003  *
00004  * Copyright (C) 2008-2009 Splitted-Desktop Systems
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 
00059     va_pic->picture_id = ff_vaapi_get_surface_id(pic);
00060     va_pic->frame_idx  = pic->long_ref ? pic->pic_id : pic->frame_num;
00061 
00062     va_pic->flags      = 0;
00063     if (pic_structure != PICT_FRAME)
00064         va_pic->flags |= (pic_structure & PICT_TOP_FIELD) ? VA_PICTURE_H264_TOP_FIELD : VA_PICTURE_H264_BOTTOM_FIELD;
00065     if (pic->reference)
00066         va_pic->flags |= pic->long_ref ? VA_PICTURE_H264_LONG_TERM_REFERENCE : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
00067 
00068     va_pic->TopFieldOrderCnt = 0;
00069     if (pic->field_poc[0] != INT_MAX)
00070         va_pic->TopFieldOrderCnt = pic->field_poc[0];
00071 
00072     va_pic->BottomFieldOrderCnt = 0;
00073     if (pic->field_poc[1] != INT_MAX)
00074         va_pic->BottomFieldOrderCnt = pic->field_poc[1];
00075 }
00076 
00078 typedef struct DPB {
00079     int            size;        
00080     int            max_size;    
00081     VAPictureH264 *va_pics;     
00082 } DPB;
00083 
00090 static int dpb_add(DPB *dpb, Picture *pic)
00091 {
00092     int i;
00093 
00094     if (dpb->size >= dpb->max_size)
00095         return -1;
00096 
00097     for (i = 0; i < dpb->size; i++) {
00098         VAPictureH264 * const va_pic = &dpb->va_pics[i];
00099         if (va_pic->picture_id == ff_vaapi_get_surface_id(pic)) {
00100             VAPictureH264 temp_va_pic;
00101             fill_vaapi_pic(&temp_va_pic, pic, 0);
00102 
00103             if ((temp_va_pic.flags ^ va_pic->flags) & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD)) {
00104                 va_pic->flags |= temp_va_pic.flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
00105                 /* Merge second field */
00106                 if (temp_va_pic.flags & VA_PICTURE_H264_TOP_FIELD) {
00107                     va_pic->TopFieldOrderCnt    = temp_va_pic.TopFieldOrderCnt;
00108                 } else {
00109                     va_pic->BottomFieldOrderCnt = temp_va_pic.BottomFieldOrderCnt;
00110                 }
00111             }
00112             return 0;
00113         }
00114     }
00115 
00116     fill_vaapi_pic(&dpb->va_pics[dpb->size++], pic, 0);
00117     return 0;
00118 }
00119 
00121 static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param,
00122                                       H264Context                  *h)
00123 {
00124     DPB dpb;
00125     int i;
00126 
00127     dpb.size     = 0;
00128     dpb.max_size = FF_ARRAY_ELEMS(pic_param->ReferenceFrames);
00129     dpb.va_pics  = pic_param->ReferenceFrames;
00130     for (i = 0; i < dpb.max_size; i++)
00131         init_vaapi_pic(&dpb.va_pics[i]);
00132 
00133     for (i = 0; i < h->short_ref_count; i++) {
00134         Picture * const pic = h->short_ref[i];
00135         if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
00136             return -1;
00137     }
00138 
00139     for (i = 0; i < 16; i++) {
00140         Picture * const pic = h->long_ref[i];
00141         if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
00142             return -1;
00143     }
00144     return 0;
00145 }
00146 
00155 static void fill_vaapi_RefPicList(VAPictureH264 RefPicList[32],
00156                                   Picture      *ref_list,
00157                                   unsigned int  ref_count)
00158 {
00159     unsigned int i, n = 0;
00160     for (i = 0; i < ref_count; i++)
00161         if (ref_list[i].reference)
00162             fill_vaapi_pic(&RefPicList[n++], &ref_list[i], 0);
00163 
00164     for (; n < 32; n++)
00165         init_vaapi_pic(&RefPicList[n]);
00166 }
00167 
00183 static void fill_vaapi_plain_pred_weight_table(H264Context   *h,
00184                                                int            list,
00185                                                unsigned char *luma_weight_flag,
00186                                                short          luma_weight[32],
00187                                                short          luma_offset[32],
00188                                                unsigned char *chroma_weight_flag,
00189                                                short          chroma_weight[32][2],
00190                                                short          chroma_offset[32][2])
00191 {
00192     unsigned int i, j;
00193 
00194     *luma_weight_flag    = h->luma_weight_flag[list];
00195     *chroma_weight_flag  = h->chroma_weight_flag[list];
00196 
00197     for (i = 0; i < h->ref_count[list]; i++) {
00198         /* VA API also wants the inferred (default) values, not
00199            only what is available in the bitstream (7.4.3.2). */
00200         if (h->luma_weight_flag[list]) {
00201             luma_weight[i] = h->luma_weight[i][list][0];
00202             luma_offset[i] = h->luma_weight[i][list][1];
00203         } else {
00204             luma_weight[i] = 1 << h->luma_log2_weight_denom;
00205             luma_offset[i] = 0;
00206         }
00207         for (j = 0; j < 2; j++) {
00208             if (h->chroma_weight_flag[list]) {
00209                 chroma_weight[i][j] = h->chroma_weight[i][list][j][0];
00210                 chroma_offset[i][j] = h->chroma_weight[i][list][j][1];
00211             } else {
00212                 chroma_weight[i][j] = 1 << h->chroma_log2_weight_denom;
00213                 chroma_offset[i][j] = 0;
00214             }
00215         }
00216     }
00217 }
00218 
00220 static int start_frame(AVCodecContext          *avctx,
00221                        av_unused const uint8_t *buffer,
00222                        av_unused uint32_t       size)
00223 {
00224     H264Context * const h = avctx->priv_data;
00225     MpegEncContext * const s = &h->s;
00226     struct vaapi_context * const vactx = avctx->hwaccel_context;
00227     VAPictureParameterBufferH264 *pic_param;
00228     VAIQMatrixBufferH264 *iq_matrix;
00229 
00230     dprintf(avctx, "start_frame()\n");
00231 
00232     vactx->slice_param_size = sizeof(VASliceParameterBufferH264);
00233 
00234     /* Fill in VAPictureParameterBufferH264. */
00235     pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferH264));
00236     if (!pic_param)
00237         return -1;
00238     fill_vaapi_pic(&pic_param->CurrPic, s->current_picture_ptr, s->picture_structure);
00239     if (fill_vaapi_ReferenceFrames(pic_param, h) < 0)
00240         return -1;
00241     pic_param->picture_width_in_mbs_minus1                      = s->mb_width - 1;
00242     pic_param->picture_height_in_mbs_minus1                     = s->mb_height - 1;
00243     pic_param->bit_depth_luma_minus8                            = h->sps.bit_depth_luma - 8;
00244     pic_param->bit_depth_chroma_minus8                          = h->sps.bit_depth_chroma - 8;
00245     pic_param->num_ref_frames                                   = h->sps.ref_frame_count;
00246     pic_param->seq_fields.value                                 = 0; /* reset all bits */
00247     pic_param->seq_fields.bits.chroma_format_idc                = h->sps.chroma_format_idc;
00248     pic_param->seq_fields.bits.residual_colour_transform_flag   = h->sps.residual_color_transform_flag; /* XXX: only for 4:4:4 high profile? */
00249     pic_param->seq_fields.bits.gaps_in_frame_num_value_allowed_flag = h->sps.gaps_in_frame_num_allowed_flag;
00250     pic_param->seq_fields.bits.frame_mbs_only_flag              = h->sps.frame_mbs_only_flag;
00251     pic_param->seq_fields.bits.mb_adaptive_frame_field_flag     = h->sps.mb_aff;
00252     pic_param->seq_fields.bits.direct_8x8_inference_flag        = h->sps.direct_8x8_inference_flag;
00253     pic_param->seq_fields.bits.MinLumaBiPredSize8x8             = h->sps.level_idc >= 31; /* A.3.3.2 */
00254     pic_param->seq_fields.bits.log2_max_frame_num_minus4        = h->sps.log2_max_frame_num - 4;
00255     pic_param->seq_fields.bits.pic_order_cnt_type               = h->sps.poc_type;
00256     pic_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
00257     pic_param->seq_fields.bits.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
00258     pic_param->num_slice_groups_minus1                          = h->pps.slice_group_count - 1;
00259     pic_param->slice_group_map_type                             = h->pps.mb_slice_group_map_type;
00260     pic_param->slice_group_change_rate_minus1                   = 0; /* XXX: unimplemented in FFmpeg */
00261     pic_param->pic_init_qp_minus26                              = h->pps.init_qp - 26;
00262     pic_param->pic_init_qs_minus26                              = h->pps.init_qs - 26;
00263     pic_param->chroma_qp_index_offset                           = h->pps.chroma_qp_index_offset[0];
00264     pic_param->second_chroma_qp_index_offset                    = h->pps.chroma_qp_index_offset[1];
00265     pic_param->pic_fields.value                                 = 0; /* reset all bits */
00266     pic_param->pic_fields.bits.entropy_coding_mode_flag         = h->pps.cabac;
00267     pic_param->pic_fields.bits.weighted_pred_flag               = h->pps.weighted_pred;
00268     pic_param->pic_fields.bits.weighted_bipred_idc              = h->pps.weighted_bipred_idc;
00269     pic_param->pic_fields.bits.transform_8x8_mode_flag          = h->pps.transform_8x8_mode;
00270     pic_param->pic_fields.bits.field_pic_flag                   = s->picture_structure != PICT_FRAME;
00271     pic_param->pic_fields.bits.constrained_intra_pred_flag      = h->pps.constrained_intra_pred;
00272     pic_param->pic_fields.bits.pic_order_present_flag           = h->pps.pic_order_present;
00273     pic_param->pic_fields.bits.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
00274     pic_param->pic_fields.bits.redundant_pic_cnt_present_flag   = h->pps.redundant_pic_cnt_present;
00275     pic_param->pic_fields.bits.reference_pic_flag               = h->nal_ref_idc != 0;
00276     pic_param->frame_num                                        = h->frame_num;
00277 
00278     /* Fill in VAIQMatrixBufferH264. */
00279     iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferH264));
00280     if (!iq_matrix)
00281         return -1;
00282     memcpy(iq_matrix->ScalingList4x4, h->pps.scaling_matrix4, sizeof(iq_matrix->ScalingList4x4));
00283     memcpy(iq_matrix->ScalingList8x8, h->pps.scaling_matrix8, sizeof(iq_matrix->ScalingList8x8));
00284     return 0;
00285 }
00286 
00288 static int end_frame(AVCodecContext *avctx)
00289 {
00290     H264Context * const h = avctx->priv_data;
00291 
00292     dprintf(avctx, "end_frame()\n");
00293     return ff_vaapi_common_end_frame(&h->s);
00294 }
00295 
00297 static int decode_slice(AVCodecContext *avctx,
00298                         const uint8_t  *buffer,
00299                         uint32_t        size)
00300 {
00301     H264Context * const h = avctx->priv_data;
00302     MpegEncContext * const s = &h->s;
00303     VASliceParameterBufferH264 *slice_param;
00304 
00305     dprintf(avctx, "decode_slice(): buffer %p, size %d\n", buffer, size);
00306 
00307     /* Fill in VASliceParameterBufferH264. */
00308     slice_param = (VASliceParameterBufferH264 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
00309     if (!slice_param)
00310         return -1;
00311     slice_param->slice_data_bit_offset          = get_bits_count(&h->s.gb) + 8; /* bit buffer started beyond nal_unit_type */
00312     slice_param->first_mb_in_slice              = (s->mb_y >> FIELD_OR_MBAFF_PICTURE) * s->mb_width + s->mb_x;
00313     slice_param->slice_type                     = ff_h264_get_slice_type(h);
00314     slice_param->direct_spatial_mv_pred_flag    = h->slice_type == FF_B_TYPE ? h->direct_spatial_mv_pred : 0;
00315     slice_param->num_ref_idx_l0_active_minus1   = h->list_count > 0 ? h->ref_count[0] - 1 : 0;
00316     slice_param->num_ref_idx_l1_active_minus1   = h->list_count > 1 ? h->ref_count[1] - 1 : 0;
00317     slice_param->cabac_init_idc                 = h->cabac_init_idc;
00318     slice_param->slice_qp_delta                 = s->qscale - h->pps.init_qp;
00319     slice_param->disable_deblocking_filter_idc  = h->deblocking_filter < 2 ? !h->deblocking_filter : h->deblocking_filter;
00320     slice_param->slice_alpha_c0_offset_div2     = h->slice_alpha_c0_offset / 2 - 26;
00321     slice_param->slice_beta_offset_div2         = h->slice_beta_offset     / 2 - 26;
00322     slice_param->luma_log2_weight_denom         = h->luma_log2_weight_denom;
00323     slice_param->chroma_log2_weight_denom       = h->chroma_log2_weight_denom;
00324 
00325     fill_vaapi_RefPicList(slice_param->RefPicList0, h->ref_list[0], h->list_count > 0 ? h->ref_count[0] : 0);
00326     fill_vaapi_RefPicList(slice_param->RefPicList1, h->ref_list[1], h->list_count > 1 ? h->ref_count[1] : 0);
00327 
00328     fill_vaapi_plain_pred_weight_table(h, 0,
00329                                        &slice_param->luma_weight_l0_flag,   slice_param->luma_weight_l0,   slice_param->luma_offset_l0,
00330                                        &slice_param->chroma_weight_l0_flag, slice_param->chroma_weight_l0, slice_param->chroma_offset_l0);
00331     fill_vaapi_plain_pred_weight_table(h, 1,
00332                                        &slice_param->luma_weight_l1_flag,   slice_param->luma_weight_l1,   slice_param->luma_offset_l1,
00333                                        &slice_param->chroma_weight_l1_flag, slice_param->chroma_weight_l1, slice_param->chroma_offset_l1);
00334     return 0;
00335 }
00336 
00337 AVHWAccel h264_vaapi_hwaccel = {
00338     .name           = "h264_vaapi",
00339     .type           = AVMEDIA_TYPE_VIDEO,
00340     .id             = CODEC_ID_H264,
00341     .pix_fmt        = PIX_FMT_VAAPI_VLD,
00342     .capabilities   = 0,
00343     .start_frame    = start_frame,
00344     .end_frame      = end_frame,
00345     .decode_slice   = decode_slice,
00346     .priv_data_size = 0,
00347 };

Generated on Fri Sep 16 2011 17:17:44 for FFmpeg by  doxygen 1.7.1