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.

382 lines
17KB

  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(struct dxva_context *ctx, const HEVCContext *h,
  50. DXVA_PicParams_HEVC *pp)
  51. {
  52. const HEVCFrame *current_picture = h->ref;
  53. int i, j, k;
  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(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. // empty the lists
  139. memset(&pp->RefPicList, 0xff, sizeof(pp->RefPicList));
  140. memset(&pp->RefPicSetStCurrBefore, 0xff, sizeof(pp->RefPicSetStCurrBefore));
  141. memset(&pp->RefPicSetStCurrAfter, 0xff, sizeof(pp->RefPicSetStCurrAfter));
  142. memset(&pp->RefPicSetLtCurr, 0xff, sizeof(pp->RefPicSetLtCurr));
  143. // fill RefPicList from the DPB
  144. for (i = 0, j = 0; i < FF_ARRAY_ELEMS(h->DPB); i++) {
  145. const HEVCFrame *frame = &h->DPB[i];
  146. if (frame != current_picture && (frame->flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF))) {
  147. fill_picture_entry(&pp->RefPicList[j], ff_dxva2_get_surface_index(ctx, frame->frame), !!(frame->flags & HEVC_FRAME_FLAG_LONG_REF));
  148. pp->PicOrderCntValList[j] = frame->poc;
  149. j++;
  150. }
  151. }
  152. #define DO_REF_LIST(ref_idx, ref_list) { \
  153. const RefPicList *rpl = &h->rps[ref_idx]; \
  154. av_assert0(rpl->nb_refs <= FF_ARRAY_ELEMS(pp->ref_list)); \
  155. for (j = 0, k = 0; j < rpl->nb_refs; j++) { \
  156. if (rpl->ref[j]) { \
  157. pp->ref_list[k] = get_refpic_index(pp, ff_dxva2_get_surface_index(ctx, rpl->ref[j]->frame)); \
  158. k++; \
  159. } \
  160. } \
  161. }
  162. // Fill short term and long term lists
  163. DO_REF_LIST(ST_CURR_BEF, RefPicSetStCurrBefore);
  164. DO_REF_LIST(ST_CURR_AFT, RefPicSetStCurrAfter);
  165. DO_REF_LIST(LT_CURR, RefPicSetLtCurr);
  166. pp->StatusReportFeedbackNumber = 1 + ctx->report_id++;
  167. }
  168. static void fill_scaling_lists(struct dxva_context *ctx, const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
  169. {
  170. unsigned i, j, pos;
  171. const ScalingList *sl = h->pps->scaling_list_data_present_flag ?
  172. &h->pps->scaling_list : &h->sps->scaling_list;
  173. memset(qm, 0, sizeof(*qm));
  174. for (i = 0; i < 6; i++) {
  175. for (j = 0; j < 16; j++) {
  176. pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
  177. qm->ucScalingLists0[i][j] = sl->sl[0][i][pos];
  178. }
  179. for (j = 0; j < 64; j++) {
  180. pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
  181. qm->ucScalingLists1[i][j] = sl->sl[1][i][pos];
  182. qm->ucScalingLists2[i][j] = sl->sl[2][i][pos];
  183. if (i < 2)
  184. qm->ucScalingLists3[i][j] = sl->sl[3][i][pos];
  185. }
  186. qm->ucScalingListDCCoefSizeID2[i] = sl->sl_dc[0][i];
  187. if (i < 2)
  188. qm->ucScalingListDCCoefSizeID3[i] = sl->sl_dc[1][i];
  189. }
  190. }
  191. static void fill_slice_short(DXVA_Slice_HEVC_Short *slice,
  192. unsigned position, unsigned size)
  193. {
  194. memset(slice, 0, sizeof(*slice));
  195. slice->BSNALunitDataLocation = position;
  196. slice->SliceBytesInBuffer = size;
  197. slice->wBadSliceChopping = 0;
  198. }
  199. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  200. DXVA2_DecodeBufferDesc *bs,
  201. DXVA2_DecodeBufferDesc *sc)
  202. {
  203. const HEVCContext *h = avctx->priv_data;
  204. struct dxva_context *ctx = avctx->hwaccel_context;
  205. const HEVCFrame *current_picture = h->ref;
  206. struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  207. DXVA_Slice_HEVC_Short *slice = NULL;
  208. void *dxva_data_ptr;
  209. uint8_t *dxva_data, *current, *end;
  210. unsigned dxva_size;
  211. void *slice_data;
  212. unsigned slice_size;
  213. unsigned padding;
  214. unsigned i;
  215. /* Create an annex B bitstream buffer with only slice NAL and finalize slice */
  216. if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
  217. DXVA2_BitStreamDateBufferType,
  218. &dxva_data_ptr, &dxva_size)))
  219. return -1;
  220. dxva_data = dxva_data_ptr;
  221. current = dxva_data;
  222. end = dxva_data + dxva_size;
  223. for (i = 0; i < ctx_pic->slice_count; i++) {
  224. static const uint8_t start_code[] = { 0, 0, 1 };
  225. static const unsigned start_code_size = sizeof(start_code);
  226. unsigned position, size;
  227. slice = &ctx_pic->slice_short[i];
  228. position = slice->BSNALunitDataLocation;
  229. size = slice->SliceBytesInBuffer;
  230. if (start_code_size + size > end - current) {
  231. av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
  232. break;
  233. }
  234. slice->BSNALunitDataLocation = current - dxva_data;
  235. slice->SliceBytesInBuffer = start_code_size + size;
  236. memcpy(current, start_code, start_code_size);
  237. current += start_code_size;
  238. memcpy(current, &ctx_pic->bitstream[position], size);
  239. current += size;
  240. }
  241. padding = FFMIN(128 - ((current - dxva_data) & 127), end - current);
  242. if (slice && padding > 0) {
  243. memset(current, 0, padding);
  244. current += padding;
  245. slice->SliceBytesInBuffer += padding;
  246. }
  247. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
  248. DXVA2_BitStreamDateBufferType)))
  249. return -1;
  250. if (i < ctx_pic->slice_count)
  251. return -1;
  252. memset(bs, 0, sizeof(*bs));
  253. bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
  254. bs->DataSize = current - dxva_data;
  255. bs->NumMBsInBuffer = 0;
  256. slice_data = ctx_pic->slice_short;
  257. slice_size = ctx_pic->slice_count * sizeof(*ctx_pic->slice_short);
  258. av_assert0((bs->DataSize & 127) == 0);
  259. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  260. DXVA2_SliceControlBufferType,
  261. slice_data, slice_size, 0);
  262. }
  263. static int dxva2_hevc_start_frame(AVCodecContext *avctx,
  264. av_unused const uint8_t *buffer,
  265. av_unused uint32_t size)
  266. {
  267. const HEVCContext *h = avctx->priv_data;
  268. struct dxva_context *ctx = avctx->hwaccel_context;
  269. struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
  270. if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
  271. return -1;
  272. av_assert0(ctx_pic);
  273. /* Fill up DXVA_PicParams_HEVC */
  274. fill_picture_parameters(ctx, h, &ctx_pic->pp);
  275. /* Fill up DXVA_Qmatrix_HEVC */
  276. fill_scaling_lists(ctx, h, &ctx_pic->qm);
  277. ctx_pic->slice_count = 0;
  278. ctx_pic->bitstream_size = 0;
  279. ctx_pic->bitstream = NULL;
  280. return 0;
  281. }
  282. static int dxva2_hevc_decode_slice(AVCodecContext *avctx,
  283. const uint8_t *buffer,
  284. uint32_t size)
  285. {
  286. const HEVCContext *h = avctx->priv_data;
  287. const HEVCFrame *current_picture = h->ref;
  288. struct hevc_dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  289. unsigned position;
  290. if (ctx_pic->slice_count >= MAX_SLICES)
  291. return -1;
  292. if (!ctx_pic->bitstream)
  293. ctx_pic->bitstream = buffer;
  294. ctx_pic->bitstream_size += size;
  295. position = buffer - ctx_pic->bitstream;
  296. fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count], position, size);
  297. ctx_pic->slice_count++;
  298. return 0;
  299. }
  300. static int dxva2_hevc_end_frame(AVCodecContext *avctx)
  301. {
  302. HEVCContext *h = avctx->priv_data;
  303. struct hevc_dxva2_picture_context *ctx_pic = h->ref->hwaccel_picture_private;
  304. int scale = ctx_pic->pp.dwCodingParamToolFlags & 1;
  305. int ret;
  306. if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
  307. return -1;
  308. ret = ff_dxva2_common_end_frame(avctx, h->ref->frame,
  309. &ctx_pic->pp, sizeof(ctx_pic->pp),
  310. scale ? &ctx_pic->qm : NULL, scale ? sizeof(ctx_pic->qm) : 0,
  311. commit_bitstream_and_slice_buffer);
  312. return ret;
  313. }
  314. AVHWAccel ff_hevc_dxva2_hwaccel = {
  315. .name = "hevc_dxva2",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .id = AV_CODEC_ID_HEVC,
  318. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  319. .start_frame = dxva2_hevc_start_frame,
  320. .decode_slice = dxva2_hevc_decode_slice,
  321. .end_frame = dxva2_hevc_end_frame,
  322. .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
  323. };