Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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