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.

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