You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
22KB

  1. /*
  2. * DXVA2 H264 HW acceleration.
  3. *
  4. * copyright (c) 2009 Laurent Aimar
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "h264.h"
  23. #include "h264data.h"
  24. #include "mpegutils.h"
  25. // The headers above may include w32threads.h, which uses the original
  26. // _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
  27. // potentially newer version.
  28. #include "dxva2_internal.h"
  29. struct dxva2_picture_context {
  30. DXVA_PicParams_H264 pp;
  31. DXVA_Qmatrix_H264 qm;
  32. unsigned slice_count;
  33. DXVA_Slice_H264_Short slice_short[MAX_SLICES];
  34. DXVA_Slice_H264_Long slice_long[MAX_SLICES];
  35. const uint8_t *bitstream;
  36. unsigned bitstream_size;
  37. };
  38. static void fill_picture_entry(DXVA_PicEntry_H264 *pic,
  39. unsigned index, unsigned flag)
  40. {
  41. assert((index&0x7f) == index && (flag&0x01) == flag);
  42. pic->bPicEntry = index | (flag << 7);
  43. }
  44. static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const H264Context *h,
  45. DXVA_PicParams_H264 *pp)
  46. {
  47. const H264Picture *current_picture = h->cur_pic_ptr;
  48. int i, j;
  49. memset(pp, 0, sizeof(*pp));
  50. /* Configure current picture */
  51. fill_picture_entry(&pp->CurrPic,
  52. ff_dxva2_get_surface_index(avctx, ctx, current_picture->f),
  53. h->picture_structure == PICT_BOTTOM_FIELD);
  54. /* Configure the set of references */
  55. pp->UsedForReferenceFlags = 0;
  56. pp->NonExistingFrameFlags = 0;
  57. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->RefFrameList); i++) {
  58. const H264Picture *r;
  59. if (j < h->short_ref_count) {
  60. r = h->short_ref[j++];
  61. } else {
  62. r = NULL;
  63. while (!r && j < h->short_ref_count + 16)
  64. r = h->long_ref[j++ - h->short_ref_count];
  65. }
  66. if (r) {
  67. fill_picture_entry(&pp->RefFrameList[i],
  68. ff_dxva2_get_surface_index(avctx, ctx, r->f),
  69. r->long_ref != 0);
  70. if ((r->reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
  71. pp->FieldOrderCntList[i][0] = r->field_poc[0];
  72. if ((r->reference & PICT_BOTTOM_FIELD) && r->field_poc[1] != INT_MAX)
  73. pp->FieldOrderCntList[i][1] = r->field_poc[1];
  74. pp->FrameNumList[i] = r->long_ref ? r->pic_id : r->frame_num;
  75. if (r->reference & PICT_TOP_FIELD)
  76. pp->UsedForReferenceFlags |= 1 << (2*i + 0);
  77. if (r->reference & PICT_BOTTOM_FIELD)
  78. pp->UsedForReferenceFlags |= 1 << (2*i + 1);
  79. } else {
  80. pp->RefFrameList[i].bPicEntry = 0xff;
  81. pp->FieldOrderCntList[i][0] = 0;
  82. pp->FieldOrderCntList[i][1] = 0;
  83. pp->FrameNumList[i] = 0;
  84. }
  85. }
  86. pp->wFrameWidthInMbsMinus1 = h->mb_width - 1;
  87. pp->wFrameHeightInMbsMinus1 = h->mb_height - 1;
  88. pp->num_ref_frames = h->sps.ref_frame_count;
  89. pp->wBitFields = ((h->picture_structure != PICT_FRAME) << 0) |
  90. ((h->sps.mb_aff &&
  91. (h->picture_structure == PICT_FRAME)) << 1) |
  92. (h->sps.residual_color_transform_flag << 2) |
  93. /* sp_for_switch_flag (not implemented by Libav) */
  94. (0 << 3) |
  95. (h->sps.chroma_format_idc << 4) |
  96. ((h->nal_ref_idc != 0) << 6) |
  97. (h->pps.constrained_intra_pred << 7) |
  98. (h->pps.weighted_pred << 8) |
  99. (h->pps.weighted_bipred_idc << 9) |
  100. /* MbsConsecutiveFlag */
  101. (1 << 11) |
  102. (h->sps.frame_mbs_only_flag << 12) |
  103. (h->pps.transform_8x8_mode << 13) |
  104. ((h->sps.level_idc >= 31) << 14) |
  105. /* IntraPicFlag (Modified if we detect a non
  106. * intra slice in dxva2_h264_decode_slice) */
  107. (1 << 15);
  108. pp->bit_depth_luma_minus8 = h->sps.bit_depth_luma - 8;
  109. pp->bit_depth_chroma_minus8 = h->sps.bit_depth_chroma - 8;
  110. if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG)
  111. pp->Reserved16Bits = 0;
  112. else if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO)
  113. pp->Reserved16Bits = 0x34c;
  114. else
  115. pp->Reserved16Bits = 3; /* FIXME is there a way to detect the right mode ? */
  116. pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
  117. pp->CurrFieldOrderCnt[0] = 0;
  118. if ((h->picture_structure & PICT_TOP_FIELD) &&
  119. current_picture->field_poc[0] != INT_MAX)
  120. pp->CurrFieldOrderCnt[0] = current_picture->field_poc[0];
  121. pp->CurrFieldOrderCnt[1] = 0;
  122. if ((h->picture_structure & PICT_BOTTOM_FIELD) &&
  123. current_picture->field_poc[1] != INT_MAX)
  124. pp->CurrFieldOrderCnt[1] = current_picture->field_poc[1];
  125. pp->pic_init_qs_minus26 = h->pps.init_qs - 26;
  126. pp->chroma_qp_index_offset = h->pps.chroma_qp_index_offset[0];
  127. pp->second_chroma_qp_index_offset = h->pps.chroma_qp_index_offset[1];
  128. pp->ContinuationFlag = 1;
  129. pp->pic_init_qp_minus26 = h->pps.init_qp - 26;
  130. pp->num_ref_idx_l0_active_minus1 = h->pps.ref_count[0] - 1;
  131. pp->num_ref_idx_l1_active_minus1 = h->pps.ref_count[1] - 1;
  132. pp->Reserved8BitsA = 0;
  133. pp->frame_num = h->frame_num;
  134. pp->log2_max_frame_num_minus4 = h->sps.log2_max_frame_num - 4;
  135. pp->pic_order_cnt_type = h->sps.poc_type;
  136. if (h->sps.poc_type == 0)
  137. pp->log2_max_pic_order_cnt_lsb_minus4 = h->sps.log2_max_poc_lsb - 4;
  138. else if (h->sps.poc_type == 1)
  139. pp->delta_pic_order_always_zero_flag = h->sps.delta_pic_order_always_zero_flag;
  140. pp->direct_8x8_inference_flag = h->sps.direct_8x8_inference_flag;
  141. pp->entropy_coding_mode_flag = h->pps.cabac;
  142. pp->pic_order_present_flag = h->pps.pic_order_present;
  143. pp->num_slice_groups_minus1 = h->pps.slice_group_count - 1;
  144. pp->slice_group_map_type = h->pps.mb_slice_group_map_type;
  145. pp->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
  146. pp->redundant_pic_cnt_present_flag= h->pps.redundant_pic_cnt_present;
  147. pp->Reserved8BitsB = 0;
  148. pp->slice_group_change_rate_minus1= 0; /* XXX not implemented by Libav */
  149. //pp->SliceGroupMap[810]; /* XXX not implemented by Libav */
  150. }
  151. static void fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, const H264Context *h, DXVA_Qmatrix_H264 *qm)
  152. {
  153. unsigned i, j;
  154. memset(qm, 0, sizeof(*qm));
  155. if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG) {
  156. for (i = 0; i < 6; i++)
  157. for (j = 0; j < 16; j++)
  158. qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][j];
  159. for (i = 0; i < 64; i++) {
  160. qm->bScalingLists8x8[0][i] = h->pps.scaling_matrix8[0][i];
  161. qm->bScalingLists8x8[1][i] = h->pps.scaling_matrix8[3][i];
  162. }
  163. } else {
  164. for (i = 0; i < 6; i++)
  165. for (j = 0; j < 16; j++)
  166. qm->bScalingLists4x4[i][j] = h->pps.scaling_matrix4[i][zigzag_scan[j]];
  167. for (i = 0; i < 64; i++) {
  168. qm->bScalingLists8x8[0][i] = h->pps.scaling_matrix8[0][ff_zigzag_direct[i]];
  169. qm->bScalingLists8x8[1][i] = h->pps.scaling_matrix8[3][ff_zigzag_direct[i]];
  170. }
  171. }
  172. }
  173. static int is_slice_short(const AVCodecContext *avctx, AVDXVAContext *ctx)
  174. {
  175. assert(DXVA_CONTEXT_CFG_BITSTREAM(avctx, ctx) == 1 ||
  176. DXVA_CONTEXT_CFG_BITSTREAM(avctx, ctx) == 2);
  177. return DXVA_CONTEXT_CFG_BITSTREAM(avctx, ctx) == 2;
  178. }
  179. static void fill_slice_short(DXVA_Slice_H264_Short *slice,
  180. unsigned position, unsigned size)
  181. {
  182. memset(slice, 0, sizeof(*slice));
  183. slice->BSNALunitDataLocation = position;
  184. slice->SliceBytesInBuffer = size;
  185. slice->wBadSliceChopping = 0;
  186. }
  187. static int get_refpic_index(const DXVA_PicParams_H264 *pp, int surface_index)
  188. {
  189. int i;
  190. for (i = 0; i < FF_ARRAY_ELEMS(pp->RefFrameList); i++) {
  191. if ((pp->RefFrameList[i].bPicEntry & 0x7f) == surface_index)
  192. return i;
  193. }
  194. return 0x7f;
  195. }
  196. static void fill_slice_long(AVCodecContext *avctx, DXVA_Slice_H264_Long *slice,
  197. const DXVA_PicParams_H264 *pp, unsigned position, unsigned size)
  198. {
  199. const H264Context *h = avctx->priv_data;
  200. H264SliceContext *sl = &h->slice_ctx[0];
  201. AVDXVAContext *ctx = avctx->hwaccel_context;
  202. unsigned list;
  203. memset(slice, 0, sizeof(*slice));
  204. slice->BSNALunitDataLocation = position;
  205. slice->SliceBytesInBuffer = size;
  206. slice->wBadSliceChopping = 0;
  207. slice->first_mb_in_slice = (sl->mb_y >> FIELD_OR_MBAFF_PICTURE(h)) * h->mb_width + sl->mb_x;
  208. slice->NumMbsForSlice = 0; /* XXX it is set once we have all slices */
  209. slice->BitOffsetToSliceData = get_bits_count(&sl->gb);
  210. slice->slice_type = ff_h264_get_slice_type(sl);
  211. if (sl->slice_type_fixed)
  212. slice->slice_type += 5;
  213. slice->luma_log2_weight_denom = sl->luma_log2_weight_denom;
  214. slice->chroma_log2_weight_denom = sl->chroma_log2_weight_denom;
  215. if (sl->list_count > 0)
  216. slice->num_ref_idx_l0_active_minus1 = sl->ref_count[0] - 1;
  217. if (sl->list_count > 1)
  218. slice->num_ref_idx_l1_active_minus1 = sl->ref_count[1] - 1;
  219. slice->slice_alpha_c0_offset_div2 = sl->slice_alpha_c0_offset / 2;
  220. slice->slice_beta_offset_div2 = sl->slice_beta_offset / 2;
  221. slice->Reserved8Bits = 0;
  222. for (list = 0; list < 2; list++) {
  223. unsigned i;
  224. for (i = 0; i < FF_ARRAY_ELEMS(slice->RefPicList[list]); i++) {
  225. if (list < sl->list_count && i < sl->ref_count[list]) {
  226. const H264Picture *r = sl->ref_list[list][i].parent;
  227. unsigned plane;
  228. unsigned index;
  229. if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO)
  230. index = ff_dxva2_get_surface_index(avctx, ctx, r->f);
  231. else
  232. index = get_refpic_index(pp, ff_dxva2_get_surface_index(avctx, ctx, r->f));
  233. fill_picture_entry(&slice->RefPicList[list][i], index,
  234. r->reference == PICT_BOTTOM_FIELD);
  235. for (plane = 0; plane < 3; plane++) {
  236. int w, o;
  237. if (plane == 0 && sl->luma_weight_flag[list]) {
  238. w = sl->luma_weight[i][list][0];
  239. o = sl->luma_weight[i][list][1];
  240. } else if (plane >= 1 && sl->chroma_weight_flag[list]) {
  241. w = sl->chroma_weight[i][list][plane-1][0];
  242. o = sl->chroma_weight[i][list][plane-1][1];
  243. } else {
  244. w = 1 << (plane == 0 ? sl->luma_log2_weight_denom :
  245. sl->chroma_log2_weight_denom);
  246. o = 0;
  247. }
  248. slice->Weights[list][i][plane][0] = w;
  249. slice->Weights[list][i][plane][1] = o;
  250. }
  251. } else {
  252. unsigned plane;
  253. slice->RefPicList[list][i].bPicEntry = 0xff;
  254. for (plane = 0; plane < 3; plane++) {
  255. slice->Weights[list][i][plane][0] = 0;
  256. slice->Weights[list][i][plane][1] = 0;
  257. }
  258. }
  259. }
  260. }
  261. slice->slice_qs_delta = 0; /* XXX not implemented by Libav */
  262. slice->slice_qp_delta = sl->qscale - h->pps.init_qp;
  263. slice->redundant_pic_cnt = sl->redundant_pic_count;
  264. if (sl->slice_type == AV_PICTURE_TYPE_B)
  265. slice->direct_spatial_mv_pred_flag = sl->direct_spatial_mv_pred;
  266. slice->cabac_init_idc = h->pps.cabac ? sl->cabac_init_idc : 0;
  267. if (sl->deblocking_filter < 2)
  268. slice->disable_deblocking_filter_idc = 1 - sl->deblocking_filter;
  269. else
  270. slice->disable_deblocking_filter_idc = sl->deblocking_filter;
  271. slice->slice_id = h->current_slice - 1;
  272. }
  273. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  274. DECODER_BUFFER_DESC *bs,
  275. DECODER_BUFFER_DESC *sc)
  276. {
  277. const H264Context *h = avctx->priv_data;
  278. const unsigned mb_count = h->mb_width * h->mb_height;
  279. AVDXVAContext *ctx = avctx->hwaccel_context;
  280. const H264Picture *current_picture = h->cur_pic_ptr;
  281. struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  282. DXVA_Slice_H264_Short *slice = NULL;
  283. void *dxva_data_ptr;
  284. uint8_t *dxva_data, *current, *end;
  285. unsigned dxva_size;
  286. void *slice_data;
  287. unsigned slice_size;
  288. unsigned padding;
  289. unsigned i;
  290. unsigned type;
  291. /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
  292. #if CONFIG_D3D11VA
  293. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  294. type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
  295. if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  296. D3D11VA_CONTEXT(ctx)->decoder,
  297. type,
  298. &dxva_size, &dxva_data_ptr)))
  299. return -1;
  300. }
  301. #endif
  302. #if CONFIG_DXVA2
  303. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  304. type = DXVA2_BitStreamDateBufferType;
  305. if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
  306. type,
  307. &dxva_data_ptr, &dxva_size)))
  308. return -1;
  309. }
  310. #endif
  311. dxva_data = dxva_data_ptr;
  312. current = dxva_data;
  313. end = dxva_data + dxva_size;
  314. for (i = 0; i < ctx_pic->slice_count; i++) {
  315. static const uint8_t start_code[] = { 0, 0, 1 };
  316. static const unsigned start_code_size = sizeof(start_code);
  317. unsigned position, size;
  318. assert(offsetof(DXVA_Slice_H264_Short, BSNALunitDataLocation) ==
  319. offsetof(DXVA_Slice_H264_Long, BSNALunitDataLocation));
  320. assert(offsetof(DXVA_Slice_H264_Short, SliceBytesInBuffer) ==
  321. offsetof(DXVA_Slice_H264_Long, SliceBytesInBuffer));
  322. if (is_slice_short(avctx, ctx))
  323. slice = &ctx_pic->slice_short[i];
  324. else
  325. slice = (DXVA_Slice_H264_Short*)&ctx_pic->slice_long[i];
  326. position = slice->BSNALunitDataLocation;
  327. size = slice->SliceBytesInBuffer;
  328. if (start_code_size + size > end - current) {
  329. av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
  330. break;
  331. }
  332. slice->BSNALunitDataLocation = current - dxva_data;
  333. slice->SliceBytesInBuffer = start_code_size + size;
  334. if (!is_slice_short(avctx, ctx)) {
  335. DXVA_Slice_H264_Long *slice_long = (DXVA_Slice_H264_Long*)slice;
  336. if (i < ctx_pic->slice_count - 1)
  337. slice_long->NumMbsForSlice =
  338. slice_long[1].first_mb_in_slice - slice_long[0].first_mb_in_slice;
  339. else
  340. slice_long->NumMbsForSlice = mb_count - slice_long->first_mb_in_slice;
  341. }
  342. memcpy(current, start_code, start_code_size);
  343. current += start_code_size;
  344. memcpy(current, &ctx_pic->bitstream[position], size);
  345. current += size;
  346. }
  347. padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
  348. if (slice && padding > 0) {
  349. memset(current, 0, padding);
  350. current += padding;
  351. slice->SliceBytesInBuffer += padding;
  352. }
  353. #if CONFIG_D3D11VA
  354. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
  355. if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
  356. return -1;
  357. #endif
  358. #if CONFIG_DXVA2
  359. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  360. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
  361. return -1;
  362. #endif
  363. if (i < ctx_pic->slice_count)
  364. return -1;
  365. #if CONFIG_D3D11VA
  366. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  367. D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
  368. memset(dsc11, 0, sizeof(*dsc11));
  369. dsc11->BufferType = type;
  370. dsc11->DataSize = current - dxva_data;
  371. dsc11->NumMBsInBuffer = mb_count;
  372. type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
  373. }
  374. #endif
  375. #if CONFIG_DXVA2
  376. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  377. DXVA2_DecodeBufferDesc *dsc2 = bs;
  378. memset(dsc2, 0, sizeof(*dsc2));
  379. dsc2->CompressedBufferType = type;
  380. dsc2->DataSize = current - dxva_data;
  381. dsc2->NumMBsInBuffer = mb_count;
  382. type = DXVA2_SliceControlBufferType;
  383. }
  384. #endif
  385. if (is_slice_short(avctx, ctx)) {
  386. slice_data = ctx_pic->slice_short;
  387. slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
  388. } else {
  389. slice_data = ctx_pic->slice_long;
  390. slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_long);
  391. }
  392. assert((bs->DataSize & 127) == 0);
  393. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  394. type,
  395. slice_data, slice_size, mb_count);
  396. }
  397. static int dxva2_h264_start_frame(AVCodecContext *avctx,
  398. av_unused const uint8_t *buffer,
  399. av_unused uint32_t size)
  400. {
  401. const H264Context *h = avctx->priv_data;
  402. AVDXVAContext *ctx = avctx->hwaccel_context;
  403. struct dxva2_picture_context *ctx_pic = h->cur_pic_ptr->hwaccel_picture_private;
  404. if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
  405. DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
  406. DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
  407. return -1;
  408. assert(ctx_pic);
  409. /* Fill up DXVA_PicParams_H264 */
  410. fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp);
  411. /* Fill up DXVA_Qmatrix_H264 */
  412. fill_scaling_lists(avctx, ctx, h, &ctx_pic->qm);
  413. ctx_pic->slice_count = 0;
  414. ctx_pic->bitstream_size = 0;
  415. ctx_pic->bitstream = NULL;
  416. return 0;
  417. }
  418. static int dxva2_h264_decode_slice(AVCodecContext *avctx,
  419. const uint8_t *buffer,
  420. uint32_t size)
  421. {
  422. const H264Context *h = avctx->priv_data;
  423. const H264SliceContext *sl = &h->slice_ctx[0];
  424. AVDXVAContext *ctx = avctx->hwaccel_context;
  425. const H264Picture *current_picture = h->cur_pic_ptr;
  426. struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  427. unsigned position;
  428. if (ctx_pic->slice_count >= MAX_SLICES)
  429. return -1;
  430. if (!ctx_pic->bitstream)
  431. ctx_pic->bitstream = buffer;
  432. ctx_pic->bitstream_size += size;
  433. position = buffer - ctx_pic->bitstream;
  434. if (is_slice_short(avctx, ctx))
  435. fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count],
  436. position, size);
  437. else
  438. fill_slice_long(avctx, &ctx_pic->slice_long[ctx_pic->slice_count],
  439. &ctx_pic->pp, position, size);
  440. ctx_pic->slice_count++;
  441. if (sl->slice_type != AV_PICTURE_TYPE_I && sl->slice_type != AV_PICTURE_TYPE_SI)
  442. ctx_pic->pp.wBitFields &= ~(1 << 15); /* Set IntraPicFlag to 0 */
  443. return 0;
  444. }
  445. static int dxva2_h264_end_frame(AVCodecContext *avctx)
  446. {
  447. H264Context *h = avctx->priv_data;
  448. H264SliceContext *sl = &h->slice_ctx[0];
  449. struct dxva2_picture_context *ctx_pic =
  450. h->cur_pic_ptr->hwaccel_picture_private;
  451. int ret;
  452. if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
  453. return -1;
  454. ret = ff_dxva2_common_end_frame(avctx, h->cur_pic_ptr->f,
  455. &ctx_pic->pp, sizeof(ctx_pic->pp),
  456. &ctx_pic->qm, sizeof(ctx_pic->qm),
  457. commit_bitstream_and_slice_buffer);
  458. if (!ret)
  459. ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
  460. return ret;
  461. }
  462. #if CONFIG_H264_DXVA2_HWACCEL
  463. AVHWAccel ff_h264_dxva2_hwaccel = {
  464. .name = "h264_dxva2",
  465. .type = AVMEDIA_TYPE_VIDEO,
  466. .id = AV_CODEC_ID_H264,
  467. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  468. .start_frame = dxva2_h264_start_frame,
  469. .decode_slice = dxva2_h264_decode_slice,
  470. .end_frame = dxva2_h264_end_frame,
  471. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  472. };
  473. #endif
  474. #if CONFIG_H264_D3D11VA_HWACCEL
  475. AVHWAccel ff_h264_d3d11va_hwaccel = {
  476. .name = "h264_d3d11va",
  477. .type = AVMEDIA_TYPE_VIDEO,
  478. .id = AV_CODEC_ID_H264,
  479. .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
  480. .start_frame = dxva2_h264_start_frame,
  481. .decode_slice = dxva2_h264_decode_slice,
  482. .end_frame = dxva2_h264_end_frame,
  483. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  484. };
  485. #endif