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.

450 lines
19KB

  1. /*
  2. * DXVA2 HEVC HW acceleration.
  3. *
  4. * copyright (c) 2014 - 2015 Hendrik Leppkes
  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 "libavutil/avassert.h"
  23. #include "hevc.h"
  24. // The headers above may include w32threads.h, which uses the original
  25. // _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
  26. // potentially newer version.
  27. #include "dxva2_internal.h"
  28. #define MAX_SLICES 256
  29. struct hevc_dxva2_picture_context {
  30. DXVA_PicParams_HEVC pp;
  31. DXVA_Qmatrix_HEVC qm;
  32. unsigned slice_count;
  33. DXVA_Slice_HEVC_Short slice_short[MAX_SLICES];
  34. const uint8_t *bitstream;
  35. unsigned bitstream_size;
  36. };
  37. static void fill_picture_entry(DXVA_PicEntry_HEVC *pic,
  38. unsigned index, unsigned flag)
  39. {
  40. av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
  41. pic->bPicEntry = index | (flag << 7);
  42. }
  43. static int get_refpic_index(const DXVA_PicParams_HEVC *pp, int surface_index)
  44. {
  45. int i;
  46. for (i = 0; i < FF_ARRAY_ELEMS(pp->RefPicList); i++) {
  47. if ((pp->RefPicList[i].bPicEntry & 0x7f) == surface_index)
  48. return i;
  49. }
  50. return 0xff;
  51. }
  52. static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const HEVCContext *h,
  53. DXVA_PicParams_HEVC *pp)
  54. {
  55. const HEVCFrame *current_picture = h->ref;
  56. const HEVCSPS *sps = h->ps.sps;
  57. const HEVCPPS *pps = h->ps.pps;
  58. int i, j;
  59. memset(pp, 0, sizeof(*pp));
  60. pp->PicWidthInMinCbsY = sps->min_cb_width;
  61. pp->PicHeightInMinCbsY = sps->min_cb_height;
  62. pp->wFormatAndSequenceInfoFlags = (sps->chroma_format_idc << 0) |
  63. (sps->separate_colour_plane_flag << 2) |
  64. ((sps->bit_depth - 8) << 3) |
  65. ((sps->bit_depth - 8) << 6) |
  66. ((sps->log2_max_poc_lsb - 4) << 9) |
  67. (0 << 13) |
  68. (0 << 14) |
  69. (0 << 15);
  70. fill_picture_entry(&pp->CurrPic, ff_dxva2_get_surface_index(avctx, ctx, current_picture->frame), 0);
  71. pp->sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
  72. pp->log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3;
  73. pp->log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size;
  74. pp->log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2;
  75. pp->log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size;
  76. pp->max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter;
  77. pp->max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra;
  78. pp->num_short_term_ref_pic_sets = sps->nb_st_rps;
  79. pp->num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps;
  80. pp->num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1;
  81. pp->num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1;
  82. pp->init_qp_minus26 = pps->pic_init_qp_minus26;
  83. if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
  84. pp->ucNumDeltaPocsOfRefRpsIdx = h->sh.short_term_rps->rps_idx_num_delta_pocs;
  85. pp->wNumBitsForShortTermRPSInSlice = h->sh.short_term_ref_pic_set_size;
  86. }
  87. pp->dwCodingParamToolFlags = (sps->scaling_list_enable_flag << 0) |
  88. (sps->amp_enabled_flag << 1) |
  89. (sps->sao_enabled << 2) |
  90. (sps->pcm_enabled_flag << 3) |
  91. ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth - 1) : 0) << 4) |
  92. ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth_chroma - 1) : 0) << 8) |
  93. ((sps->pcm_enabled_flag ? (sps->pcm.log2_min_pcm_cb_size - 3) : 0) << 12) |
  94. ((sps->pcm_enabled_flag ? (sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size) : 0) << 14) |
  95. (sps->pcm.loop_filter_disable_flag << 16) |
  96. (sps->long_term_ref_pics_present_flag << 17) |
  97. (sps->sps_temporal_mvp_enabled_flag << 18) |
  98. (sps->sps_strong_intra_smoothing_enable_flag << 19) |
  99. (pps->dependent_slice_segments_enabled_flag << 20) |
  100. (pps->output_flag_present_flag << 21) |
  101. (pps->num_extra_slice_header_bits << 22) |
  102. (pps->sign_data_hiding_flag << 25) |
  103. (pps->cabac_init_present_flag << 26) |
  104. (0 << 27);
  105. pp->dwCodingSettingPicturePropertyFlags = (pps->constrained_intra_pred_flag << 0) |
  106. (pps->transform_skip_enabled_flag << 1) |
  107. (pps->cu_qp_delta_enabled_flag << 2) |
  108. (pps->pic_slice_level_chroma_qp_offsets_present_flag << 3) |
  109. (pps->weighted_pred_flag << 4) |
  110. (pps->weighted_bipred_flag << 5) |
  111. (pps->transquant_bypass_enable_flag << 6) |
  112. (pps->tiles_enabled_flag << 7) |
  113. (pps->entropy_coding_sync_enabled_flag << 8) |
  114. (pps->uniform_spacing_flag << 9) |
  115. ((pps->tiles_enabled_flag ? pps->loop_filter_across_tiles_enabled_flag : 0) << 10) |
  116. (pps->seq_loop_filter_across_slices_enabled_flag << 11) |
  117. (pps->deblocking_filter_override_enabled_flag << 12) |
  118. (pps->disable_dbf << 13) |
  119. (pps->lists_modification_present_flag << 14) |
  120. (pps->slice_header_extension_present_flag << 15) |
  121. (IS_IRAP(h) << 16) |
  122. (IS_IDR(h) << 17) |
  123. /* IntraPicFlag */
  124. (IS_IRAP(h) << 18) |
  125. (0 << 19);
  126. pp->pps_cb_qp_offset = pps->cb_qp_offset;
  127. pp->pps_cr_qp_offset = pps->cr_qp_offset;
  128. if (pps->tiles_enabled_flag) {
  129. pp->num_tile_columns_minus1 = pps->num_tile_columns - 1;
  130. pp->num_tile_rows_minus1 = pps->num_tile_rows - 1;
  131. if (!pps->uniform_spacing_flag) {
  132. for (i = 0; i < pps->num_tile_columns; i++)
  133. pp->column_width_minus1[i] = pps->column_width[i] - 1;
  134. for (i = 0; i < pps->num_tile_rows; i++)
  135. pp->row_height_minus1[i] = pps->row_height[i] - 1;
  136. }
  137. }
  138. pp->diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth;
  139. pp->pps_beta_offset_div2 = pps->beta_offset / 2;
  140. pp->pps_tc_offset_div2 = pps->tc_offset / 2;
  141. pp->log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2;
  142. pp->CurrPicOrderCntVal = h->poc;
  143. // fill RefPicList from the DPB
  144. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->RefPicList); i++) {
  145. const HEVCFrame *frame = NULL;
  146. while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
  147. if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
  148. frame = &h->DPB[j];
  149. j++;
  150. }
  151. if (frame) {
  152. fill_picture_entry(&pp->RefPicList[i], ff_dxva2_get_surface_index(avctx, ctx, frame->frame), !!(frame->flags & HEVC_FRAME_FLAG_LONG_REF));
  153. pp->PicOrderCntValList[i] = frame->poc;
  154. } else {
  155. pp->RefPicList[i].bPicEntry = 0xff;
  156. pp->PicOrderCntValList[i] = 0;
  157. }
  158. }
  159. #define DO_REF_LIST(ref_idx, ref_list) { \
  160. const RefPicList *rpl = &h->rps[ref_idx]; \
  161. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ref_list); i++) { \
  162. const HEVCFrame *frame = NULL; \
  163. while (!frame && j < rpl->nb_refs) \
  164. frame = rpl->ref[j++]; \
  165. if (frame) \
  166. pp->ref_list[i] = get_refpic_index(pp, ff_dxva2_get_surface_index(avctx, ctx, frame->frame)); \
  167. else \
  168. pp->ref_list[i] = 0xff; \
  169. } \
  170. }
  171. // Fill short term and long term lists
  172. DO_REF_LIST(ST_CURR_BEF, RefPicSetStCurrBefore);
  173. DO_REF_LIST(ST_CURR_AFT, RefPicSetStCurrAfter);
  174. DO_REF_LIST(LT_CURR, RefPicSetLtCurr);
  175. pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
  176. }
  177. static void fill_scaling_lists(AVDXVAContext *ctx, const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
  178. {
  179. unsigned i, j, pos;
  180. const ScalingList *sl = h->ps.pps->scaling_list_data_present_flag ?
  181. &h->ps.pps->scaling_list : &h->ps.sps->scaling_list;
  182. memset(qm, 0, sizeof(*qm));
  183. for (i = 0; i < 6; i++) {
  184. for (j = 0; j < 16; j++) {
  185. pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
  186. qm->ucScalingLists0[i][j] = sl->sl[0][i][pos];
  187. }
  188. for (j = 0; j < 64; j++) {
  189. pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
  190. qm->ucScalingLists1[i][j] = sl->sl[1][i][pos];
  191. qm->ucScalingLists2[i][j] = sl->sl[2][i][pos];
  192. if (i < 2)
  193. qm->ucScalingLists3[i][j] = sl->sl[3][i * 3][pos];
  194. }
  195. qm->ucScalingListDCCoefSizeID2[i] = sl->sl_dc[0][i];
  196. if (i < 2)
  197. qm->ucScalingListDCCoefSizeID3[i] = sl->sl_dc[1][i * 3];
  198. }
  199. }
  200. static void fill_slice_short(DXVA_Slice_HEVC_Short *slice,
  201. unsigned position, unsigned size)
  202. {
  203. memset(slice, 0, sizeof(*slice));
  204. slice->BSNALunitDataLocation = position;
  205. slice->SliceBytesInBuffer = size;
  206. slice->wBadSliceChopping = 0;
  207. }
  208. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  209. DECODER_BUFFER_DESC *bs,
  210. DECODER_BUFFER_DESC *sc)
  211. {
  212. const HEVCContext *h = avctx->priv_data;
  213. AVDXVAContext *ctx = avctx->hwaccel_context;
  214. const HEVCFrame *current_picture = h->ref;
  215. struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  216. DXVA_Slice_HEVC_Short *slice = NULL;
  217. void *dxva_data_ptr;
  218. uint8_t *dxva_data, *current, *end;
  219. unsigned dxva_size;
  220. void *slice_data;
  221. unsigned slice_size;
  222. unsigned padding;
  223. unsigned i;
  224. unsigned type;
  225. /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
  226. #if CONFIG_D3D11VA
  227. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  228. type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
  229. if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  230. D3D11VA_CONTEXT(ctx)->decoder,
  231. type,
  232. &dxva_size, &dxva_data_ptr)))
  233. return -1;
  234. }
  235. #endif
  236. #if CONFIG_DXVA2
  237. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  238. type = DXVA2_BitStreamDateBufferType;
  239. if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
  240. type,
  241. &dxva_data_ptr, &dxva_size)))
  242. return -1;
  243. }
  244. #endif
  245. dxva_data = dxva_data_ptr;
  246. current = dxva_data;
  247. end = dxva_data + dxva_size;
  248. for (i = 0; i < ctx_pic->slice_count; i++) {
  249. static const uint8_t start_code[] = { 0, 0, 1 };
  250. static const unsigned start_code_size = sizeof(start_code);
  251. unsigned position, size;
  252. slice = &ctx_pic->slice_short[i];
  253. position = slice->BSNALunitDataLocation;
  254. size = slice->SliceBytesInBuffer;
  255. if (start_code_size + size > end - current) {
  256. av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
  257. break;
  258. }
  259. slice->BSNALunitDataLocation = current - dxva_data;
  260. slice->SliceBytesInBuffer = start_code_size + size;
  261. memcpy(current, start_code, start_code_size);
  262. current += start_code_size;
  263. memcpy(current, &ctx_pic->bitstream[position], size);
  264. current += size;
  265. }
  266. padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
  267. if (slice && padding > 0) {
  268. memset(current, 0, padding);
  269. current += padding;
  270. slice->SliceBytesInBuffer += padding;
  271. }
  272. #if CONFIG_D3D11VA
  273. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
  274. if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
  275. return -1;
  276. #endif
  277. #if CONFIG_DXVA2
  278. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  279. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
  280. return -1;
  281. #endif
  282. if (i < ctx_pic->slice_count)
  283. return -1;
  284. #if CONFIG_D3D11VA
  285. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  286. D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
  287. memset(dsc11, 0, sizeof(*dsc11));
  288. dsc11->BufferType = type;
  289. dsc11->DataSize = current - dxva_data;
  290. dsc11->NumMBsInBuffer = 0;
  291. type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
  292. }
  293. #endif
  294. #if CONFIG_DXVA2
  295. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  296. DXVA2_DecodeBufferDesc *dsc2 = bs;
  297. memset(dsc2, 0, sizeof(*dsc2));
  298. dsc2->CompressedBufferType = type;
  299. dsc2->DataSize = current - dxva_data;
  300. dsc2->NumMBsInBuffer = 0;
  301. type = DXVA2_SliceControlBufferType;
  302. }
  303. #endif
  304. slice_data = ctx_pic->slice_short;
  305. slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
  306. av_assert0(((current - dxva_data) & 127) == 0);
  307. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  308. type,
  309. slice_data, slice_size, 0);
  310. }
  311. static int dxva2_hevc_start_frame(AVCodecContext *avctx,
  312. av_unused const uint8_t *buffer,
  313. av_unused uint32_t size)
  314. {
  315. const HEVCContext *h = avctx->priv_data;
  316. AVDXVAContext *ctx = avctx->hwaccel_context;
  317. struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
  318. if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
  319. DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
  320. DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
  321. return -1;
  322. av_assert0(ctx_pic);
  323. /* Fill up DXVA_PicParams_HEVC */
  324. fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp);
  325. /* Fill up DXVA_Qmatrix_HEVC */
  326. fill_scaling_lists(ctx, h, &ctx_pic->qm);
  327. ctx_pic->slice_count = 0;
  328. ctx_pic->bitstream_size = 0;
  329. ctx_pic->bitstream = NULL;
  330. return 0;
  331. }
  332. static int dxva2_hevc_decode_slice(AVCodecContext *avctx,
  333. const uint8_t *buffer,
  334. uint32_t size)
  335. {
  336. const HEVCContext *h = avctx->priv_data;
  337. const HEVCFrame *current_picture = h->ref;
  338. struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  339. unsigned position;
  340. if (ctx_pic->slice_count >= MAX_SLICES)
  341. return -1;
  342. if (!ctx_pic->bitstream)
  343. ctx_pic->bitstream = buffer;
  344. ctx_pic->bitstream_size += size;
  345. position = buffer - ctx_pic->bitstream;
  346. fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count], position, size);
  347. ctx_pic->slice_count++;
  348. return 0;
  349. }
  350. static int dxva2_hevc_end_frame(AVCodecContext *avctx)
  351. {
  352. HEVCContext *h = avctx->priv_data;
  353. struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
  354. int scale = ctx_pic->pp.dwCodingParamToolFlags & 1;
  355. int ret;
  356. if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
  357. return -1;
  358. ret = ff_dxva2_common_end_frame(avctx, h->ref->frame,
  359. &ctx_pic->pp, sizeof(ctx_pic->pp),
  360. scale ? &ctx_pic->qm : NULL, scale ? sizeof(ctx_pic->qm) : 0,
  361. commit_bitstream_and_slice_buffer);
  362. return ret;
  363. }
  364. #if CONFIG_HEVC_DXVA2_HWACCEL
  365. AVHWAccel ff_hevc_dxva2_hwaccel = {
  366. .name = "hevc_dxva2",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .id = AV_CODEC_ID_HEVC,
  369. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  370. .start_frame = dxva2_hevc_start_frame,
  371. .decode_slice = dxva2_hevc_decode_slice,
  372. .end_frame = dxva2_hevc_end_frame,
  373. .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
  374. };
  375. #endif
  376. #if CONFIG_HEVC_D3D11VA_HWACCEL
  377. AVHWAccel ff_hevc_d3d11va_hwaccel = {
  378. .name = "hevc_d3d11va",
  379. .type = AVMEDIA_TYPE_VIDEO,
  380. .id = AV_CODEC_ID_HEVC,
  381. .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
  382. .start_frame = dxva2_hevc_start_frame,
  383. .decode_slice = dxva2_hevc_decode_slice,
  384. .end_frame = dxva2_hevc_end_frame,
  385. .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
  386. };
  387. #endif