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

libavcodec/dxva2_h264.c

Go to the documentation of this file.
00001 /*
00002  * DXVA2 H264 HW acceleration.
00003  *
00004  * copyright (c) 2009 Laurent Aimar
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 "dxva2_internal.h"
00024 #include "h264.h"
00025 #include "h264data.h"
00026 
00027 struct dxva2_picture_context {
00028     DXVA_PicParams_H264   pp;
00029     DXVA_Qmatrix_H264     qm;
00030     unsigned              slice_count;
00031     DXVA_Slice_H264_Short slice_short[MAX_SLICES];
00032     DXVA_Slice_H264_Long  slice_long[MAX_SLICES];
00033     const uint8_t         *bitstream;
00034     unsigned              bitstream_size;
00035 };
00036 
00037 static void fill_picture_entry(DXVA_PicEntry_H264 *pic,
00038                                unsigned index, unsigned flag)
00039 {
00040     assert((index&0x7f) == index && (flag&0x01) == flag);
00041     pic->bPicEntry = index | (flag << 7);
00042 }
00043 
00044 static void fill_picture_parameters(struct dxva_context *ctx, const H264Context *h,
00045                                     DXVA_PicParams_H264 *pp)
00046 {
00047     const MpegEncContext *s = &h->s;
00048     const Picture *current_picture = s->current_picture_ptr;
00049     int i, j;
00050 
00051     memset(pp, 0, sizeof(*pp));
00052     /* Configure current picture */
00053     fill_picture_entry(&pp->CurrPic,
00054                        ff_dxva2_get_surface_index(ctx, current_picture),
00055                        s->picture_structure == PICT_BOTTOM_FIELD);
00056     /* Configure the set of references */
00057     pp->UsedForReferenceFlags  = 0;
00058     pp->NonExistingFrameFlags  = 0;
00059     for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->RefFrameList); i++) {
00060         const Picture *r;
00061         if (j < h->short_ref_count) {
00062             r = h->short_ref[j++];
00063         } else {
00064             r = NULL;
00065             while (!r && j < h->short_ref_count + 16)
00066                 r = h->long_ref[j++ - h->short_ref_count];
00067         }
00068         if (r) {
00069             fill_picture_entry(&pp->RefFrameList[i],
00070                                ff_dxva2_get_surface_index(ctx, r),
00071                                r->long_ref != 0);
00072 
00073             if ((r->reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
00074                 pp->FieldOrderCntList[i][0] = r->field_poc[0];
00075             if ((r->reference & PICT_BOTTOM_FIELD) && r->field_poc[1] != INT_MAX)
00076                 pp->FieldOrderCntList[i][1] = r->field_poc[1];
00077 
00078             pp->FrameNumList[i] = r->long_ref ? r->pic_id : r->frame_num;
00079             if (r->reference & PICT_TOP_FIELD)
00080                 pp->UsedForReferenceFlags |= 1 << (2*i + 0);
00081             if (r->reference & PICT_BOTTOM_FIELD)
00082                 pp->UsedForReferenceFlags |= 1 << (2*i + 1);
00083         } else {
00084             pp->RefFrameList[i].bPicEntry = 0xff;
00085             pp->FieldOrderCntList[i][0]   = 0;
00086             pp->FieldOrderCntList[i][1]   = 0;
00087             pp->FrameNumList[i]           = 0;
00088         }
00089     }
00090 
00091     pp->wFrameWidthInMbsMinus1        = s->mb_width  - 1;
00092     pp->wFrameHeightInMbsMinus1       = s->mb_height - 1;
00093     pp->num_ref_frames                = h->sps.ref_frame_count;
00094 
00095     pp->wBitFields                    = ((s->picture_structure != PICT_FRAME) <<  0) |
00096                                         (h->sps.mb_aff                        <<  1) |
00097                                         (h->sps.residual_color_transform_flag <<  2) |
00098                                         /* sp_for_switch_flag (not implemented by FFmpeg) */
00099                                         (0                                    <<  3) |
00100                                         (h->sps.chroma_format_idc             <<  4) |
00101                                         ((h->nal_ref_idc != 0)                <<  6) |
00102                                         (h->pps.constrained_intra_pred        <<  7) |
00103                                         (h->pps.weighted_pred                 <<  8) |
00104                                         (h->pps.weighted_bipred_idc           <<  9) |
00105                                         /* MbsConsecutiveFlag */
00106                                         (1                                    << 11) |
00107                                         (h->sps.frame_mbs_only_flag           << 12) |
00108                                         (h->pps.transform_8x8_mode            << 13) |
00109                                         ((h->sps.level_idc >= 31)             << 14) |
00110                                         /* IntraPicFlag (Modified if we detect a non
00111                                          * intra slice in decode_slice) */
00112                                         (1                                    << 15);
00113 
00114     pp->bit_depth_luma_minus8         = h->sps.bit_depth_luma - 8;
00115     pp->bit_depth_chroma_minus8       = h->sps.bit_depth_chroma - 8;
00116     pp->Reserved16Bits                = 3; /* FIXME is there a way to detect the right mode ? */
00117     pp->StatusReportFeedbackNumber    = 1 + ctx->report_id++;
00118     pp->CurrFieldOrderCnt[0] = 0;
00119     if ((s->picture_structure & PICT_TOP_FIELD) &&
00120         current_picture->field_poc[0] != INT_MAX)
00121         pp->CurrFieldOrderCnt[0] = current_picture->field_poc[0];
00122     pp->CurrFieldOrderCnt[1] = 0;
00123     if ((s->picture_structure & PICT_BOTTOM_FIELD) &&
00124         current_picture->field_poc[1] != INT_MAX)
00125         pp->CurrFieldOrderCnt[1] = current_picture->field_poc[1];
00126     pp->pic_init_qs_minus26           = h->pps.init_qs - 26;
00127     pp->chroma_qp_index_offset        = h->pps.chroma_qp_index_offset[0];
00128     pp->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
00129     pp->ContinuationFlag              = 1;
00130     pp->pic_init_qp_minus26           = h->pps.init_qp - 26;
00131     pp->num_ref_idx_l0_active_minus1  = h->pps.ref_count[0] - 1;
00132     pp->num_ref_idx_l1_active_minus1  = h->pps.ref_count[1] - 1;
00133     pp->Reserved8BitsA                = 0;
00134     pp->frame_num                     = h->frame_num;
00135     pp->log2_max_frame_num_minus4     = h->sps.log2_max_frame_num - 4;
00136     pp->pic_order_cnt_type            = h->sps.poc_type;
00137     if (h->sps.poc_type == 0)
00138         pp->log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
00139     else if (h->sps.poc_type == 1)
00140         pp->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
00141     pp->direct_8x8_inference_flag     = h->sps.direct_8x8_inference_flag;
00142     pp->entropy_coding_mode_flag      = h->pps.cabac;
00143     pp->pic_order_present_flag        = h->pps.pic_order_present;
00144     pp->num_slice_groups_minus1       = h->pps.slice_group_count - 1;
00145     pp->slice_group_map_type          = h->pps.mb_slice_group_map_type;
00146     pp->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
00147     pp->redundant_pic_cnt_present_flag= h->pps.redundant_pic_cnt_present;
00148     pp->Reserved8BitsB                = 0;
00149     pp->slice_group_change_rate_minus1= 0;  /* XXX not implemented by FFmpeg */
00150     //pp->SliceGroupMap[810];               /* XXX not implemented by FFmpeg */
00151 }
00152 
00153 static void fill_scaling_lists(const H264Context *h, DXVA_Qmatrix_H264 *qm)
00154 {
00155     unsigned i, j;
00156     memset(qm, 0, sizeof(*qm));
00157     for (i = 0; i < 6; i++)
00158         for (j = 0; j < 16; j++)
00159             qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][zigzag_scan[j]];
00160 
00161     for (j = 0; j < 64; j++) {
00162         qm->bScalingLists8x8[0][j] = h->pps.scaling_matrix8[0][ff_zigzag_direct[j]];
00163         qm->bScalingLists8x8[1][j] = h->pps.scaling_matrix8[3][ff_zigzag_direct[j]];
00164     }
00165 }
00166 
00167 static int is_slice_short(struct dxva_context *ctx)
00168 {
00169     assert(ctx->cfg->ConfigBitstreamRaw == 1 ||
00170            ctx->cfg->ConfigBitstreamRaw == 2);
00171     return ctx->cfg->ConfigBitstreamRaw == 2;
00172 }
00173 
00174 static void fill_slice_short(DXVA_Slice_H264_Short *slice,
00175                              unsigned position, unsigned size)
00176 {
00177     memset(slice, 0, sizeof(*slice));
00178     slice->BSNALunitDataLocation = position;
00179     slice->SliceBytesInBuffer    = size;
00180     slice->wBadSliceChopping     = 0;
00181 }
00182 
00183 static void fill_slice_long(AVCodecContext *avctx, DXVA_Slice_H264_Long *slice,
00184                             unsigned position, unsigned size)
00185 {
00186     const H264Context *h = avctx->priv_data;
00187     struct dxva_context *ctx = avctx->hwaccel_context;
00188     const MpegEncContext *s = &h->s;
00189     unsigned list;
00190 
00191     memset(slice, 0, sizeof(*slice));
00192     slice->BSNALunitDataLocation = position;
00193     slice->SliceBytesInBuffer    = size;
00194     slice->wBadSliceChopping     = 0;
00195 
00196     slice->first_mb_in_slice     = (s->mb_y >> FIELD_OR_MBAFF_PICTURE) * s->mb_width + s->mb_x;
00197     slice->NumMbsForSlice        = 0; /* XXX it is set once we have all slices */
00198     slice->BitOffsetToSliceData  = get_bits_count(&s->gb);
00199     slice->slice_type            = ff_h264_get_slice_type(h);
00200     if (h->slice_type_fixed)
00201         slice->slice_type += 5;
00202     slice->luma_log2_weight_denom       = h->luma_log2_weight_denom;
00203     slice->chroma_log2_weight_denom     = h->chroma_log2_weight_denom;
00204     if (h->list_count > 0)
00205         slice->num_ref_idx_l0_active_minus1 = h->ref_count[0] - 1;
00206     if (h->list_count > 1)
00207         slice->num_ref_idx_l1_active_minus1 = h->ref_count[1] - 1;
00208     slice->slice_alpha_c0_offset_div2   = h->slice_alpha_c0_offset / 2 - 26;
00209     slice->slice_beta_offset_div2       = h->slice_beta_offset     / 2 - 26;
00210     slice->Reserved8Bits                = 0;
00211 
00212     for (list = 0; list < 2; list++) {
00213         unsigned i;
00214         for (i = 0; i < FF_ARRAY_ELEMS(slice->RefPicList[list]); i++) {
00215             if (list < h->list_count && i < h->ref_count[list]) {
00216                 const Picture *r = &h->ref_list[list][i];
00217                 unsigned plane;
00218                 fill_picture_entry(&slice->RefPicList[list][i],
00219                                    ff_dxva2_get_surface_index(ctx, r),
00220                                    r->reference == PICT_BOTTOM_FIELD);
00221                 for (plane = 0; plane < 3; plane++) {
00222                     int w, o;
00223                     if (plane == 0 && h->luma_weight_flag[list]) {
00224                         w = h->luma_weight[i][list][0];
00225                         o = h->luma_weight[i][list][1];
00226                     } else if (plane >= 1 && h->chroma_weight_flag[list]) {
00227                         w = h->chroma_weight[i][list][plane-1][0];
00228                         o = h->chroma_weight[i][list][plane-1][1];
00229                     } else {
00230                         w = 1 << (plane == 0 ? h->luma_log2_weight_denom :
00231                                                h->chroma_log2_weight_denom);
00232                         o = 0;
00233                     }
00234                     slice->Weights[list][i][plane][0] = w;
00235                     slice->Weights[list][i][plane][1] = o;
00236                 }
00237             } else {
00238                 unsigned plane;
00239                 slice->RefPicList[list][i].bPicEntry = 0xff;
00240                 for (plane = 0; plane < 3; plane++) {
00241                     slice->Weights[list][i][plane][0] = 0;
00242                     slice->Weights[list][i][plane][1] = 0;
00243                 }
00244             }
00245         }
00246     }
00247     slice->slice_qs_delta    = 0; /* XXX not implemented by FFmpeg */
00248     slice->slice_qp_delta    = s->qscale - h->pps.init_qp;
00249     slice->redundant_pic_cnt = h->redundant_pic_count;
00250     if (h->slice_type == AV_PICTURE_TYPE_B)
00251         slice->direct_spatial_mv_pred_flag = h->direct_spatial_mv_pred;
00252     slice->cabac_init_idc = h->pps.cabac ? h->cabac_init_idc : 0;
00253     if (h->deblocking_filter < 2)
00254         slice->disable_deblocking_filter_idc = 1 - h->deblocking_filter;
00255     else
00256         slice->disable_deblocking_filter_idc = h->deblocking_filter;
00257     slice->slice_id = h->current_slice - 1;
00258 }
00259 
00260 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
00261                                              DXVA2_DecodeBufferDesc *bs,
00262                                              DXVA2_DecodeBufferDesc *sc)
00263 {
00264     const H264Context *h = avctx->priv_data;
00265     const MpegEncContext *s = &h->s;
00266     const unsigned mb_count = s->mb_width * s->mb_height;
00267     struct dxva_context *ctx = avctx->hwaccel_context;
00268     const Picture *current_picture = h->s.current_picture_ptr;
00269     struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
00270     DXVA_Slice_H264_Short *slice = NULL;
00271     uint8_t  *dxva_data, *current, *end;
00272     unsigned dxva_size;
00273     void     *slice_data;
00274     unsigned slice_size;
00275     unsigned padding;
00276     unsigned i;
00277 
00278     /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
00279     if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
00280                                                DXVA2_BitStreamDateBufferType,
00281                                                &dxva_data, &dxva_size)))
00282         return -1;
00283     current = dxva_data;
00284     end = dxva_data + dxva_size;
00285 
00286     for (i = 0; i < ctx_pic->slice_count; i++) {
00287         static const uint8_t start_code[] = { 0, 0, 1 };
00288         static const unsigned start_code_size = sizeof(start_code);
00289         unsigned position, size;
00290 
00291         assert(offsetof(DXVA_Slice_H264_Short, BSNALunitDataLocation) ==
00292                offsetof(DXVA_Slice_H264_Long,  BSNALunitDataLocation));
00293         assert(offsetof(DXVA_Slice_H264_Short, SliceBytesInBuffer) ==
00294                offsetof(DXVA_Slice_H264_Long,  SliceBytesInBuffer));
00295 
00296         if (is_slice_short(ctx))
00297             slice = &ctx_pic->slice_short[i];
00298         else
00299             slice = (DXVA_Slice_H264_Short*)&ctx_pic->slice_long[i];
00300 
00301         position = slice->BSNALunitDataLocation;
00302         size     = slice->SliceBytesInBuffer;
00303         if (start_code_size + size > end - current) {
00304             av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
00305             break;
00306         }
00307 
00308         slice->BSNALunitDataLocation = current - dxva_data;
00309         slice->SliceBytesInBuffer    = start_code_size + size;
00310 
00311         if (!is_slice_short(ctx)) {
00312             DXVA_Slice_H264_Long *slice_long = (DXVA_Slice_H264_Long*)slice;
00313             if (i < ctx_pic->slice_count - 1)
00314                 slice_long->NumMbsForSlice =
00315                     slice_long[1].first_mb_in_slice - slice_long[0].first_mb_in_slice;
00316             else
00317                 slice_long->NumMbsForSlice = mb_count - slice_long->first_mb_in_slice;
00318         }
00319 
00320         memcpy(current, start_code, start_code_size);
00321         current += start_code_size;
00322 
00323         memcpy(current, &ctx_pic->bitstream[position], size);
00324         current += size;
00325     }
00326     padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
00327     if (slice && padding > 0) {
00328         memset(current, 0, padding);
00329         current += padding;
00330 
00331         slice->SliceBytesInBuffer += padding;
00332     }
00333     if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
00334                                                   DXVA2_BitStreamDateBufferType)))
00335         return -1;
00336     if (i < ctx_pic->slice_count)
00337         return -1;
00338 
00339     memset(bs, 0, sizeof(*bs));
00340     bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
00341     bs->DataSize             = current - dxva_data;
00342     bs->NumMBsInBuffer       = mb_count;
00343 
00344     if (is_slice_short(ctx)) {
00345         slice_data = ctx_pic->slice_short;
00346         slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
00347     } else {
00348         slice_data = ctx_pic->slice_long;
00349         slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_long);
00350     }
00351     assert((bs->DataSize & 127) == 0);
00352     return ff_dxva2_commit_buffer(avctx, ctx, sc,
00353                                   DXVA2_SliceControlBufferType,
00354                                   slice_data, slice_size, mb_count);
00355 }
00356 
00357 
00358 static int start_frame(AVCodecContext *avctx,
00359                        av_unused const uint8_t *buffer,
00360                        av_unused uint32_t size)
00361 {
00362     const H264Context *h = avctx->priv_data;
00363     struct dxva_context *ctx = avctx->hwaccel_context;
00364     struct dxva2_picture_context *ctx_pic = h->s.current_picture_ptr->hwaccel_picture_private;
00365 
00366     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
00367         return -1;
00368     assert(ctx_pic);
00369 
00370     /* Fill up DXVA_PicParams_H264 */
00371     fill_picture_parameters(ctx, h, &ctx_pic->pp);
00372 
00373     /* Fill up DXVA_Qmatrix_H264 */
00374     fill_scaling_lists(h, &ctx_pic->qm);
00375 
00376     ctx_pic->slice_count    = 0;
00377     ctx_pic->bitstream_size = 0;
00378     ctx_pic->bitstream      = NULL;
00379     return 0;
00380 }
00381 
00382 static int decode_slice(AVCodecContext *avctx,
00383                         const uint8_t *buffer, uint32_t size)
00384 {
00385     const H264Context *h = avctx->priv_data;
00386     struct dxva_context *ctx = avctx->hwaccel_context;
00387     const Picture *current_picture = h->s.current_picture_ptr;
00388     struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
00389     unsigned position;
00390 
00391     if (ctx_pic->slice_count >= MAX_SLICES)
00392         return -1;
00393 
00394     if (!ctx_pic->bitstream)
00395         ctx_pic->bitstream = buffer;
00396     ctx_pic->bitstream_size += size;
00397 
00398     position = buffer - ctx_pic->bitstream;
00399     if (is_slice_short(ctx))
00400         fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count],
00401                          position, size);
00402     else
00403         fill_slice_long(avctx, &ctx_pic->slice_long[ctx_pic->slice_count],
00404                         position, size);
00405     ctx_pic->slice_count++;
00406 
00407     if (h->slice_type != AV_PICTURE_TYPE_I && h->slice_type != AV_PICTURE_TYPE_SI)
00408         ctx_pic->pp.wBitFields &= ~(1 << 15); /* Set IntraPicFlag to 0 */
00409     return 0;
00410 }
00411 
00412 static int end_frame(AVCodecContext *avctx)
00413 {
00414     H264Context *h = avctx->priv_data;
00415     MpegEncContext *s = &h->s;
00416     struct dxva2_picture_context *ctx_pic =
00417         h->s.current_picture_ptr->hwaccel_picture_private;
00418 
00419     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
00420         return -1;
00421     return ff_dxva2_common_end_frame(avctx, s,
00422                                      &ctx_pic->pp, sizeof(ctx_pic->pp),
00423                                      &ctx_pic->qm, sizeof(ctx_pic->qm),
00424                                      commit_bitstream_and_slice_buffer);
00425 }
00426 
00427 AVHWAccel ff_h264_dxva2_hwaccel = {
00428     .name           = "h264_dxva2",
00429     .type           = AVMEDIA_TYPE_VIDEO,
00430     .id             = CODEC_ID_H264,
00431     .pix_fmt        = PIX_FMT_DXVA2_VLD,
00432     .capabilities   = 0,
00433     .start_frame    = start_frame,
00434     .decode_slice   = decode_slice,
00435     .end_frame      = end_frame,
00436     .priv_data_size = sizeof(struct dxva2_picture_context),
00437 };
00438 

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1