00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00053 fill_picture_entry(&pp->CurrPic,
00054 ff_dxva2_get_surface_index(ctx, current_picture),
00055 s->picture_structure == PICT_BOTTOM_FIELD);
00056
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->f.reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
00074 pp->FieldOrderCntList[i][0] = r->field_poc[0];
00075 if ((r->f.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->f.reference & PICT_TOP_FIELD)
00080 pp->UsedForReferenceFlags |= 1 << (2*i + 0);
00081 if (r->f.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
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
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
00111
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 if (ctx->workaround & FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG)
00117 pp->Reserved16Bits = 0;
00118 else
00119 pp->Reserved16Bits = 3;
00120 pp->StatusReportFeedbackNumber = 1 + ctx->report_id++;
00121 pp->CurrFieldOrderCnt[0] = 0;
00122 if ((s->picture_structure & PICT_TOP_FIELD) &&
00123 current_picture->field_poc[0] != INT_MAX)
00124 pp->CurrFieldOrderCnt[0] = current_picture->field_poc[0];
00125 pp->CurrFieldOrderCnt[1] = 0;
00126 if ((s->picture_structure & PICT_BOTTOM_FIELD) &&
00127 current_picture->field_poc[1] != INT_MAX)
00128 pp->CurrFieldOrderCnt[1] = current_picture->field_poc[1];
00129 pp->pic_init_qs_minus26 = h->pps.init_qs - 26;
00130 pp->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
00131 pp->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
00132 pp->ContinuationFlag = 1;
00133 pp->pic_init_qp_minus26 = h->pps.init_qp - 26;
00134 pp->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
00135 pp->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
00136 pp->Reserved8BitsA = 0;
00137 pp->frame_num = h->frame_num;
00138 pp->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
00139 pp->pic_order_cnt_type = h->sps.poc_type;
00140 if (h->sps.poc_type == 0)
00141 pp->log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
00142 else if (h->sps.poc_type == 1)
00143 pp->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
00144 pp->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
00145 pp->entropy_coding_mode_flag = h->pps.cabac;
00146 pp->pic_order_present_flag = h->pps.pic_order_present;
00147 pp->num_slice_groups_minus1 = h->pps.slice_group_count - 1;
00148 pp->slice_group_map_type = h->pps.mb_slice_group_map_type;
00149 pp->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
00150 pp->redundant_pic_cnt_present_flag= h->pps.redundant_pic_cnt_present;
00151 pp->Reserved8BitsB = 0;
00152 pp->slice_group_change_rate_minus1= 0;
00153
00154 }
00155
00156 static void fill_scaling_lists(struct dxva_context *ctx, const H264Context *h, DXVA_Qmatrix_H264 *qm)
00157 {
00158 unsigned i, j;
00159 memset(qm, 0, sizeof(*qm));
00160 if (ctx->workaround & FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG) {
00161 for (i = 0; i < 6; i++)
00162 for (j = 0; j < 16; j++)
00163 qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][j];
00164
00165 for (i = 0; i < 64; i++) {
00166 qm->bScalingLists8x8[0][i] = h->pps.scaling_matrix8[0][i];
00167 qm->bScalingLists8x8[1][i] = h->pps.scaling_matrix8[3][i];
00168 }
00169 } else {
00170 for (i = 0; i < 6; i++)
00171 for (j = 0; j < 16; j++)
00172 qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][zigzag_scan[j]];
00173
00174 for (i = 0; i < 64; i++) {
00175 qm->bScalingLists8x8[0][i] = h->pps.scaling_matrix8[0][ff_zigzag_direct[i]];
00176 qm->bScalingLists8x8[1][i] = h->pps.scaling_matrix8[3][ff_zigzag_direct[i]];
00177 }
00178 }
00179 }
00180
00181 static int is_slice_short(struct dxva_context *ctx)
00182 {
00183 assert(ctx->cfg->ConfigBitstreamRaw == 1 ||
00184 ctx->cfg->ConfigBitstreamRaw == 2);
00185 return ctx->cfg->ConfigBitstreamRaw == 2;
00186 }
00187
00188 static void fill_slice_short(DXVA_Slice_H264_Short *slice,
00189 unsigned position, unsigned size)
00190 {
00191 memset(slice, 0, sizeof(*slice));
00192 slice->BSNALunitDataLocation = position;
00193 slice->SliceBytesInBuffer = size;
00194 slice->wBadSliceChopping = 0;
00195 }
00196
00197 static void fill_slice_long(AVCodecContext *avctx, DXVA_Slice_H264_Long *slice,
00198 unsigned position, unsigned size)
00199 {
00200 const H264Context *h = avctx->priv_data;
00201 struct dxva_context *ctx = avctx->hwaccel_context;
00202 const MpegEncContext *s = &h->s;
00203 unsigned list;
00204
00205 memset(slice, 0, sizeof(*slice));
00206 slice->BSNALunitDataLocation = position;
00207 slice->SliceBytesInBuffer = size;
00208 slice->wBadSliceChopping = 0;
00209
00210 slice->first_mb_in_slice = (s->mb_y >> FIELD_OR_MBAFF_PICTURE) * s->mb_width + s->mb_x;
00211 slice->NumMbsForSlice = 0;
00212 slice->BitOffsetToSliceData = get_bits_count(&s->gb);
00213 slice->slice_type = ff_h264_get_slice_type(h);
00214 if (h->slice_type_fixed)
00215 slice->slice_type += 5;
00216 slice->luma_log2_weight_denom = h->luma_log2_weight_denom;
00217 slice->chroma_log2_weight_denom = h->chroma_log2_weight_denom;
00218 if (h->list_count > 0)
00219 slice->num_ref_idx_l0_active_minus1 = h->ref_count[0] - 1;
00220 if (h->list_count > 1)
00221 slice->num_ref_idx_l1_active_minus1 = h->ref_count[1] - 1;
00222 slice->slice_alpha_c0_offset_div2 = h->slice_alpha_c0_offset / 2 - 26;
00223 slice->slice_beta_offset_div2 = h->slice_beta_offset / 2 - 26;
00224 slice->Reserved8Bits = 0;
00225
00226 for (list = 0; list < 2; list++) {
00227 unsigned i;
00228 for (i = 0; i < FF_ARRAY_ELEMS(slice->RefPicList[list]); i++) {
00229 if (list < h->list_count && i < h->ref_count[list]) {
00230 const Picture *r = &h->ref_list[list][i];
00231 unsigned plane;
00232 fill_picture_entry(&slice->RefPicList[list][i],
00233 ff_dxva2_get_surface_index(ctx, r),
00234 r->f.reference == PICT_BOTTOM_FIELD);
00235 for (plane = 0; plane < 3; plane++) {
00236 int w, o;
00237 if (plane == 0 && h->luma_weight_flag[list]) {
00238 w = h->luma_weight[i][list][0];
00239 o = h->luma_weight[i][list][1];
00240 } else if (plane >= 1 && h->chroma_weight_flag[list]) {
00241 w = h->chroma_weight[i][list][plane-1][0];
00242 o = h->chroma_weight[i][list][plane-1][1];
00243 } else {
00244 w = 1 << (plane == 0 ? h->luma_log2_weight_denom :
00245 h->chroma_log2_weight_denom);
00246 o = 0;
00247 }
00248 slice->Weights[list][i][plane][0] = w;
00249 slice->Weights[list][i][plane][1] = o;
00250 }
00251 } else {
00252 unsigned plane;
00253 slice->RefPicList[list][i].bPicEntry = 0xff;
00254 for (plane = 0; plane < 3; plane++) {
00255 slice->Weights[list][i][plane][0] = 0;
00256 slice->Weights[list][i][plane][1] = 0;
00257 }
00258 }
00259 }
00260 }
00261 slice->slice_qs_delta = 0;
00262 slice->slice_qp_delta = s->qscale - h->pps.init_qp;
00263 slice->redundant_pic_cnt = h->redundant_pic_count;
00264 if (h->slice_type == AV_PICTURE_TYPE_B)
00265 slice->direct_spatial_mv_pred_flag = h->direct_spatial_mv_pred;
00266 slice->cabac_init_idc = h->pps.cabac ? h->cabac_init_idc : 0;
00267 if (h->deblocking_filter < 2)
00268 slice->disable_deblocking_filter_idc = 1 - h->deblocking_filter;
00269 else
00270 slice->disable_deblocking_filter_idc = h->deblocking_filter;
00271 slice->slice_id = h->current_slice - 1;
00272 }
00273
00274 static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
00275 DXVA2_DecodeBufferDesc *bs,
00276 DXVA2_DecodeBufferDesc *sc)
00277 {
00278 const H264Context *h = avctx->priv_data;
00279 const MpegEncContext *s = &h->s;
00280 const unsigned mb_count = s->mb_width * s->mb_height;
00281 struct dxva_context *ctx = avctx->hwaccel_context;
00282 const Picture *current_picture = h->s.current_picture_ptr;
00283 struct dxva2_picture_context *ctx_pic = current_picture->f.hwaccel_picture_private;
00284 DXVA_Slice_H264_Short *slice = NULL;
00285 uint8_t *dxva_data, *current, *end;
00286 unsigned dxva_size;
00287 void *slice_data;
00288 unsigned slice_size;
00289 unsigned padding;
00290 unsigned i;
00291
00292
00293 if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
00294 DXVA2_BitStreamDateBufferType,
00295 &dxva_data, &dxva_size)))
00296 return -1;
00297 current = dxva_data;
00298 end = dxva_data + dxva_size;
00299
00300 for (i = 0; i < ctx_pic->slice_count; i++) {
00301 static const uint8_t start_code[] = { 0, 0, 1 };
00302 static const unsigned start_code_size = sizeof(start_code);
00303 unsigned position, size;
00304
00305 assert(offsetof(DXVA_Slice_H264_Short, BSNALunitDataLocation) ==
00306 offsetof(DXVA_Slice_H264_Long, BSNALunitDataLocation));
00307 assert(offsetof(DXVA_Slice_H264_Short, SliceBytesInBuffer) ==
00308 offsetof(DXVA_Slice_H264_Long, SliceBytesInBuffer));
00309
00310 if (is_slice_short(ctx))
00311 slice = &ctx_pic->slice_short[i];
00312 else
00313 slice = (DXVA_Slice_H264_Short*)&ctx_pic->slice_long[i];
00314
00315 position = slice->BSNALunitDataLocation;
00316 size = slice->SliceBytesInBuffer;
00317 if (start_code_size + size > end - current) {
00318 av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
00319 break;
00320 }
00321
00322 slice->BSNALunitDataLocation = current - dxva_data;
00323 slice->SliceBytesInBuffer = start_code_size + size;
00324
00325 if (!is_slice_short(ctx)) {
00326 DXVA_Slice_H264_Long *slice_long = (DXVA_Slice_H264_Long*)slice;
00327 if (i < ctx_pic->slice_count - 1)
00328 slice_long->NumMbsForSlice =
00329 slice_long[1].first_mb_in_slice - slice_long[0].first_mb_in_slice;
00330 else
00331 slice_long->NumMbsForSlice = mb_count - slice_long->first_mb_in_slice;
00332 }
00333
00334 memcpy(current, start_code, start_code_size);
00335 current += start_code_size;
00336
00337 memcpy(current, &ctx_pic->bitstream[position], size);
00338 current += size;
00339 }
00340 padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
00341 if (slice && padding > 0) {
00342 memset(current, 0, padding);
00343 current += padding;
00344
00345 slice->SliceBytesInBuffer += padding;
00346 }
00347 if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
00348 DXVA2_BitStreamDateBufferType)))
00349 return -1;
00350 if (i < ctx_pic->slice_count)
00351 return -1;
00352
00353 memset(bs, 0, sizeof(*bs));
00354 bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
00355 bs->DataSize = current - dxva_data;
00356 bs->NumMBsInBuffer = mb_count;
00357
00358 if (is_slice_short(ctx)) {
00359 slice_data = ctx_pic->slice_short;
00360 slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
00361 } else {
00362 slice_data = ctx_pic->slice_long;
00363 slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_long);
00364 }
00365 assert((bs->DataSize & 127) == 0);
00366 return ff_dxva2_commit_buffer(avctx, ctx, sc,
00367 DXVA2_SliceControlBufferType,
00368 slice_data, slice_size, mb_count);
00369 }
00370
00371
00372 static int start_frame(AVCodecContext *avctx,
00373 av_unused const uint8_t *buffer,
00374 av_unused uint32_t size)
00375 {
00376 const H264Context *h = avctx->priv_data;
00377 struct dxva_context *ctx = avctx->hwaccel_context;
00378 struct dxva2_picture_context *ctx_pic = h->s.current_picture_ptr->f.hwaccel_picture_private;
00379
00380 if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
00381 return -1;
00382 assert(ctx_pic);
00383
00384
00385 fill_picture_parameters(ctx, h, &ctx_pic->pp);
00386
00387
00388 fill_scaling_lists(ctx, h, &ctx_pic->qm);
00389
00390 ctx_pic->slice_count = 0;
00391 ctx_pic->bitstream_size = 0;
00392 ctx_pic->bitstream = NULL;
00393 return 0;
00394 }
00395
00396 static int decode_slice(AVCodecContext *avctx,
00397 const uint8_t *buffer, uint32_t size)
00398 {
00399 const H264Context *h = avctx->priv_data;
00400 struct dxva_context *ctx = avctx->hwaccel_context;
00401 const Picture *current_picture = h->s.current_picture_ptr;
00402 struct dxva2_picture_context *ctx_pic = current_picture->f.hwaccel_picture_private;
00403 unsigned position;
00404
00405 if (ctx_pic->slice_count >= MAX_SLICES)
00406 return -1;
00407
00408 if (!ctx_pic->bitstream)
00409 ctx_pic->bitstream = buffer;
00410 ctx_pic->bitstream_size += size;
00411
00412 position = buffer - ctx_pic->bitstream;
00413 if (is_slice_short(ctx))
00414 fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count],
00415 position, size);
00416 else
00417 fill_slice_long(avctx, &ctx_pic->slice_long[ctx_pic->slice_count],
00418 position, size);
00419 ctx_pic->slice_count++;
00420
00421 if (h->slice_type != AV_PICTURE_TYPE_I && h->slice_type != AV_PICTURE_TYPE_SI)
00422 ctx_pic->pp.wBitFields &= ~(1 << 15);
00423 return 0;
00424 }
00425
00426 static int end_frame(AVCodecContext *avctx)
00427 {
00428 H264Context *h = avctx->priv_data;
00429 MpegEncContext *s = &h->s;
00430 struct dxva2_picture_context *ctx_pic =
00431 h->s.current_picture_ptr->f.hwaccel_picture_private;
00432
00433 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
00434 return -1;
00435 return ff_dxva2_common_end_frame(avctx, s,
00436 &ctx_pic->pp, sizeof(ctx_pic->pp),
00437 &ctx_pic->qm, sizeof(ctx_pic->qm),
00438 commit_bitstream_and_slice_buffer);
00439 }
00440
00441 AVHWAccel ff_h264_dxva2_hwaccel = {
00442 .name = "h264_dxva2",
00443 .type = AVMEDIA_TYPE_VIDEO,
00444 .id = CODEC_ID_H264,
00445 .pix_fmt = PIX_FMT_DXVA2_VLD,
00446 .start_frame = start_frame,
00447 .decode_slice = decode_slice,
00448 .end_frame = end_frame,
00449 .priv_data_size = sizeof(struct dxva2_picture_context),
00450 };