• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00007  *
00008  * Libav 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  * Libav 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 Libav; 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 
00037 static void init_vaapi_pic(VAPictureH264 *va_pic)
00038 {
00039     va_pic->picture_id          = VA_INVALID_ID;
00040     va_pic->flags               = VA_PICTURE_H264_INVALID;
00041     va_pic->TopFieldOrderCnt    = 0;
00042     va_pic->BottomFieldOrderCnt = 0;
00043 }
00044 
00053 static void fill_vaapi_pic(VAPictureH264 *va_pic,
00054                            Picture       *pic,
00055                            int            pic_structure)
00056 {
00057     if (pic_structure == 0)
00058         pic_structure = pic->f.reference;
00059     pic_structure &= PICT_FRAME; /* PICT_TOP_FIELD|PICT_BOTTOM_FIELD */
00060 
00061     va_pic->picture_id = ff_vaapi_get_surface_id(pic);
00062     va_pic->frame_idx  = pic->long_ref ? pic->pic_id : pic->frame_num;
00063 
00064     va_pic->flags      = 0;
00065     if (pic_structure != PICT_FRAME)
00066         va_pic->flags |= (pic_structure & PICT_TOP_FIELD) ? VA_PICTURE_H264_TOP_FIELD : VA_PICTURE_H264_BOTTOM_FIELD;
00067     if (pic->f.reference)
00068         va_pic->flags |= pic->long_ref ? VA_PICTURE_H264_LONG_TERM_REFERENCE : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
00069 
00070     va_pic->TopFieldOrderCnt = 0;
00071     if (pic->field_poc[0] != INT_MAX)
00072         va_pic->TopFieldOrderCnt = pic->field_poc[0];
00073 
00074     va_pic->BottomFieldOrderCnt = 0;
00075     if (pic->field_poc[1] != INT_MAX)
00076         va_pic->BottomFieldOrderCnt = pic->field_poc[1];
00077 }
00078 
00080 typedef struct DPB {
00081     int            size;        
00082     int            max_size;    
00083     VAPictureH264 *va_pics;     
00084 } DPB;
00085 
00092 static int dpb_add(DPB *dpb, Picture *pic)
00093 {
00094     int i;
00095 
00096     if (dpb->size >= dpb->max_size)
00097         return -1;
00098 
00099     for (i = 0; i < dpb->size; i++) {
00100         VAPictureH264 * const va_pic = &dpb->va_pics[i];
00101         if (va_pic->picture_id == ff_vaapi_get_surface_id(pic)) {
00102             VAPictureH264 temp_va_pic;
00103             fill_vaapi_pic(&temp_va_pic, pic, 0);
00104 
00105             if ((temp_va_pic.flags ^ va_pic->flags) & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD)) {
00106                 va_pic->flags |= temp_va_pic.flags & (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
00107                 /* Merge second field */
00108                 if (temp_va_pic.flags & VA_PICTURE_H264_TOP_FIELD) {
00109                     va_pic->TopFieldOrderCnt    = temp_va_pic.TopFieldOrderCnt;
00110                 } else {
00111                     va_pic->BottomFieldOrderCnt = temp_va_pic.BottomFieldOrderCnt;
00112                 }
00113             }
00114             return 0;
00115         }
00116     }
00117 
00118     fill_vaapi_pic(&dpb->va_pics[dpb->size++], pic, 0);
00119     return 0;
00120 }
00121 
00123 static int fill_vaapi_ReferenceFrames(VAPictureParameterBufferH264 *pic_param,
00124                                       H264Context                  *h)
00125 {
00126     DPB dpb;
00127     int i;
00128 
00129     dpb.size     = 0;
00130     dpb.max_size = FF_ARRAY_ELEMS(pic_param->ReferenceFrames);
00131     dpb.va_pics  = pic_param->ReferenceFrames;
00132     for (i = 0; i < dpb.max_size; i++)
00133         init_vaapi_pic(&dpb.va_pics[i]);
00134 
00135     for (i = 0; i < h->short_ref_count; i++) {
00136         Picture * const pic = h->short_ref[i];
00137         if (pic && pic->f.reference && dpb_add(&dpb, pic) < 0)
00138             return -1;
00139     }
00140 
00141     for (i = 0; i < 16; i++) {
00142         Picture * const pic = h->long_ref[i];
00143         if (pic && pic->f.reference && dpb_add(&dpb, pic) < 0)
00144             return -1;
00145     }
00146     return 0;
00147 }
00148 
00157 static void fill_vaapi_RefPicList(VAPictureH264 RefPicList[32],
00158                                   Picture      *ref_list,
00159                                   unsigned int  ref_count)
00160 {
00161     unsigned int i, n = 0;
00162     for (i = 0; i < ref_count; i++)
00163         if (ref_list[i].f.reference)
00164             fill_vaapi_pic(&RefPicList[n++], &ref_list[i], 0);
00165 
00166     for (; n < 32; n++)
00167         init_vaapi_pic(&RefPicList[n]);
00168 }
00169 
00185 static void fill_vaapi_plain_pred_weight_table(H264Context   *h,
00186                                                int            list,
00187                                                unsigned char *luma_weight_flag,
00188                                                short          luma_weight[32],
00189                                                short          luma_offset[32],
00190                                                unsigned char *chroma_weight_flag,
00191                                                short          chroma_weight[32][2],
00192                                                short          chroma_offset[32][2])
00193 {
00194     unsigned int i, j;
00195 
00196     *luma_weight_flag    = h->luma_weight_flag[list];
00197     *chroma_weight_flag  = h->chroma_weight_flag[list];
00198 
00199     for (i = 0; i < h->ref_count[list]; i++) {
00200         /* VA API also wants the inferred (default) values, not
00201            only what is available in the bitstream (7.4.3.2). */
00202         if (h->luma_weight_flag[list]) {
00203             luma_weight[i] = h->luma_weight[i][list][0];
00204             luma_offset[i] = h->luma_weight[i][list][1];
00205         } else {
00206             luma_weight[i] = 1 << h->luma_log2_weight_denom;
00207             luma_offset[i] = 0;
00208         }
00209         for (j = 0; j < 2; j++) {
00210             if (h->chroma_weight_flag[list]) {
00211                 chroma_weight[i][j] = h->chroma_weight[i][list][j][0];
00212                 chroma_offset[i][j] = h->chroma_weight[i][list][j][1];
00213             } else {
00214                 chroma_weight[i][j] = 1 << h->chroma_log2_weight_denom;
00215                 chroma_offset[i][j] = 0;
00216             }
00217         }
00218     }
00219 }
00220 
00222 static int start_frame(AVCodecContext          *avctx,
00223                        av_unused const uint8_t *buffer,
00224                        av_unused uint32_t       size)
00225 {
00226     H264Context * const h = avctx->priv_data;
00227     MpegEncContext * const s = &h->s;
00228     struct vaapi_context * const vactx = avctx->hwaccel_context;
00229     VAPictureParameterBufferH264 *pic_param;
00230     VAIQMatrixBufferH264 *iq_matrix;
00231 
00232     av_dlog(avctx, "start_frame()\n");
00233 
00234     vactx->slice_param_size = sizeof(VASliceParameterBufferH264);
00235 
00236     /* Fill in VAPictureParameterBufferH264. */
00237     pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferH264));
00238     if (!pic_param)
00239         return -1;
00240     fill_vaapi_pic(&pic_param->CurrPic, s->current_picture_ptr, s->picture_structure);
00241     if (fill_vaapi_ReferenceFrames(pic_param, h) < 0)
00242         return -1;
00243     pic_param->picture_width_in_mbs_minus1                      = s->mb_width - 1;
00244     pic_param->picture_height_in_mbs_minus1                     = s->mb_height - 1;
00245     pic_param->bit_depth_luma_minus8                            = h->sps.bit_depth_luma - 8;
00246     pic_param->bit_depth_chroma_minus8                          = h->sps.bit_depth_chroma - 8;
00247     pic_param->num_ref_frames                                   = h->sps.ref_frame_count;
00248     pic_param->seq_fields.value                                 = 0; /* reset all bits */
00249     pic_param->seq_fields.bits.chroma_format_idc                = h->sps.chroma_format_idc;
00250     pic_param->seq_fields.bits.residual_colour_transform_flag   = h->sps.residual_color_transform_flag; /* XXX: only for 4:4:4 high profile? */
00251     pic_param->seq_fields.bits.gaps_in_frame_num_value_allowed_flag = h->sps.gaps_in_frame_num_allowed_flag;
00252     pic_param->seq_fields.bits.frame_mbs_only_flag              = h->sps.frame_mbs_only_flag;
00253     pic_param->seq_fields.bits.mb_adaptive_frame_field_flag     = h->sps.mb_aff;
00254     pic_param->seq_fields.bits.direct_8x8_inference_flag        = h->sps.direct_8x8_inference_flag;
00255     pic_param->seq_fields.bits.MinLumaBiPredSize8x8             = h->sps.level_idc >= 31; /* A.3.3.2 */
00256     pic_param->seq_fields.bits.log2_max_frame_num_minus4        = h->sps.log2_max_frame_num - 4;
00257     pic_param->seq_fields.bits.pic_order_cnt_type               = h->sps.poc_type;
00258     pic_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
00259     pic_param->seq_fields.bits.delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
00260     pic_param->num_slice_groups_minus1                          = h->pps.slice_group_count - 1;
00261     pic_param->slice_group_map_type                             = h->pps.mb_slice_group_map_type;
00262     pic_param->slice_group_change_rate_minus1                   = 0; /* XXX: unimplemented in Libav */
00263     pic_param->pic_init_qp_minus26                              = h->pps.init_qp - 26;
00264     pic_param->pic_init_qs_minus26                              = h->pps.init_qs - 26;
00265     pic_param->chroma_qp_index_offset                           = h->pps.chroma_qp_index_offset[0];
00266     pic_param->second_chroma_qp_index_offset                    = h->pps.chroma_qp_index_offset[1];
00267     pic_param->pic_fields.value                                 = 0; /* reset all bits */
00268     pic_param->pic_fields.bits.entropy_coding_mode_flag         = h->pps.cabac;
00269     pic_param->pic_fields.bits.weighted_pred_flag               = h->pps.weighted_pred;
00270     pic_param->pic_fields.bits.weighted_bipred_idc              = h->pps.weighted_bipred_idc;
00271     pic_param->pic_fields.bits.transform_8x8_mode_flag          = h->pps.transform_8x8_mode;
00272     pic_param->pic_fields.bits.field_pic_flag                   = s->picture_structure != PICT_FRAME;
00273     pic_param->pic_fields.bits.constrained_intra_pred_flag      = h->pps.constrained_intra_pred;
00274     pic_param->pic_fields.bits.pic_order_present_flag           = h->pps.pic_order_present;
00275     pic_param->pic_fields.bits.deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
00276     pic_param->pic_fields.bits.redundant_pic_cnt_present_flag   = h->pps.redundant_pic_cnt_present;
00277     pic_param->pic_fields.bits.reference_pic_flag               = h->nal_ref_idc != 0;
00278     pic_param->frame_num                                        = h->frame_num;
00279 
00280     /* Fill in VAIQMatrixBufferH264. */
00281     iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferH264));
00282     if (!iq_matrix)
00283         return -1;
00284     memcpy(iq_matrix->ScalingList4x4, h->pps.scaling_matrix4, sizeof(iq_matrix->ScalingList4x4));
00285     memcpy(iq_matrix->ScalingList8x8, h->pps.scaling_matrix8, sizeof(iq_matrix->ScalingList8x8));
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     /* Fill in VASliceParameterBufferH264. */
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; /* bit buffer started beyond nal_unit_type */
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     .start_frame    = start_frame,
00345     .end_frame      = end_frame,
00346     .decode_slice   = decode_slice,
00347 };
Generated on Sun Apr 22 2012 21:54:05 for Libav by doxygen 1.7.1