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.

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