LCOV - code coverage report
Current view: top level - libavcodec - nvdec_hevc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 170 0.0 %
Date: 2017-12-16 13:57:32 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * HEVC HW decode acceleration through NVDEC
       3             :  *
       4             :  * Copyright (c) 2017 Anton Khirnov
       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 <stdint.h>
      24             : #include <string.h>
      25             : 
      26             : #include "avcodec.h"
      27             : #include "nvdec.h"
      28             : #include "decode.h"
      29             : #include "internal.h"
      30             : #include "hevcdec.h"
      31             : #include "hevc_data.h"
      32             : 
      33           0 : static void dpb_add(CUVIDHEVCPICPARAMS *pp, int idx, const HEVCFrame *src)
      34             : {
      35           0 :     FrameDecodeData *fdd = (FrameDecodeData*)src->frame->private_ref->data;
      36           0 :     const NVDECFrame *cf = fdd->hwaccel_priv;
      37             : 
      38           0 :     pp->RefPicIdx[idx]      = cf ? cf->idx : -1;
      39           0 :     pp->PicOrderCntVal[idx] = src->poc;
      40           0 :     pp->IsLongTerm[idx]     = !!(src->flags & HEVC_FRAME_FLAG_LONG_REF);
      41           0 : }
      42             : 
      43           0 : static void fill_scaling_lists(CUVIDHEVCPICPARAMS *ppc, const HEVCContext *s)
      44             : {
      45           0 :     const ScalingList *sl = s->ps.pps->scaling_list_data_present_flag ?
      46           0 :                             &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
      47             :     int i, j, pos;
      48             : 
      49           0 :     for (i = 0; i < 6; i++) {
      50           0 :         for (j = 0; j < 16; j++) {
      51           0 :             pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
      52           0 :             ppc->ScalingList4x4[i][j] = sl->sl[0][i][pos];
      53             :         }
      54             : 
      55           0 :         for (j = 0; j < 64; j++) {
      56           0 :             pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
      57           0 :             ppc->ScalingList8x8[i][j]   = sl->sl[1][i][pos];
      58           0 :             ppc->ScalingList16x16[i][j] = sl->sl[2][i][pos];
      59             : 
      60           0 :             if (i < 2)
      61           0 :                 ppc->ScalingList32x32[i][j] = sl->sl[3][i][pos];
      62             :         }
      63             :     }
      64             : 
      65           0 :     memcpy(ppc->ScalingListDCCoeff16x16, sl->sl_dc[0], sizeof(ppc->ScalingListDCCoeff16x16));
      66           0 :     memcpy(ppc->ScalingListDCCoeff32x32, sl->sl_dc[1], sizeof(ppc->ScalingListDCCoeff32x32));
      67           0 : }
      68             : 
      69           0 : static int nvdec_hevc_start_frame(AVCodecContext *avctx,
      70             :                                   const uint8_t *buffer, uint32_t size)
      71             : {
      72           0 :     const HEVCContext *s = avctx->priv_data;
      73           0 :     const HEVCPPS *pps = s->ps.pps;
      74           0 :     const HEVCSPS *sps = s->ps.sps;
      75             : 
      76           0 :     NVDECContext       *ctx = avctx->internal->hwaccel_priv_data;
      77           0 :     CUVIDPICPARAMS      *pp = &ctx->pic_params;
      78           0 :     CUVIDHEVCPICPARAMS *ppc = &pp->CodecSpecific.hevc;
      79             :     FrameDecodeData *fdd;
      80             :     NVDECFrame *cf;
      81             : 
      82             :     int i, j, dpb_size, ret;
      83             : 
      84           0 :     ret = ff_nvdec_start_frame(avctx, s->ref->frame);
      85           0 :     if (ret < 0)
      86           0 :         return ret;
      87             : 
      88           0 :     fdd = (FrameDecodeData*)s->ref->frame->private_ref->data;
      89           0 :     cf  = (NVDECFrame*)fdd->hwaccel_priv;
      90             : 
      91           0 :     *pp = (CUVIDPICPARAMS) {
      92           0 :         .PicWidthInMbs     = sps->width  / 16,
      93           0 :         .FrameHeightInMbs  = sps->height / 16,
      94           0 :         .CurrPicIdx        = cf->idx,
      95             :         .ref_pic_flag      = 1,
      96             :         .intra_pic_flag    = 0,
      97             : 
      98             :         .CodecSpecific.hevc = {
      99           0 :             .pic_width_in_luma_samples                    = sps->width,
     100           0 :             .pic_height_in_luma_samples                   = sps->height,
     101           0 :             .log2_min_luma_coding_block_size_minus3       = sps->log2_min_cb_size - 3,
     102           0 :             .log2_diff_max_min_luma_coding_block_size     = sps->log2_diff_max_min_coding_block_size,
     103           0 :             .log2_min_transform_block_size_minus2         = sps->log2_min_tb_size - 2,
     104           0 :             .log2_diff_max_min_transform_block_size       = sps->log2_max_trafo_size - sps->log2_min_tb_size,
     105           0 :             .pcm_enabled_flag                             = sps->pcm_enabled_flag,
     106           0 :             .log2_min_pcm_luma_coding_block_size_minus3   = sps->pcm_enabled_flag ? sps->pcm.log2_min_pcm_cb_size - 3 : 0,
     107           0 :             .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
     108           0 :             .pcm_sample_bit_depth_luma_minus1             = sps->pcm_enabled_flag ? sps->pcm.bit_depth - 1 : 0,
     109           0 :             .pcm_sample_bit_depth_chroma_minus1           = sps->pcm_enabled_flag ? sps->pcm.bit_depth_chroma - 1 : 0,
     110           0 :             .pcm_loop_filter_disabled_flag                = sps->pcm.loop_filter_disable_flag,
     111           0 :             .strong_intra_smoothing_enabled_flag          = sps->sps_strong_intra_smoothing_enable_flag,
     112           0 :             .max_transform_hierarchy_depth_intra          = sps->max_transform_hierarchy_depth_intra,
     113           0 :             .max_transform_hierarchy_depth_inter          = sps->max_transform_hierarchy_depth_inter,
     114           0 :             .amp_enabled_flag                             = sps->amp_enabled_flag,
     115           0 :             .separate_colour_plane_flag                   = sps->separate_colour_plane_flag,
     116           0 :             .log2_max_pic_order_cnt_lsb_minus4            = sps->log2_max_poc_lsb - 4,
     117           0 :             .num_short_term_ref_pic_sets                  = sps->nb_st_rps,
     118           0 :             .long_term_ref_pics_present_flag              = sps->long_term_ref_pics_present_flag,
     119           0 :             .num_long_term_ref_pics_sps                   = sps->num_long_term_ref_pics_sps,
     120           0 :             .sps_temporal_mvp_enabled_flag                = sps->sps_temporal_mvp_enabled_flag,
     121           0 :             .sample_adaptive_offset_enabled_flag          = sps->sao_enabled,
     122           0 :             .scaling_list_enable_flag                     = sps->scaling_list_enable_flag,
     123           0 :             .IrapPicFlag                                  = IS_IRAP(s),
     124           0 :             .IdrPicFlag                                   = IS_IDR(s),
     125           0 :             .bit_depth_luma_minus8                        = sps->bit_depth - 8,
     126           0 :             .bit_depth_chroma_minus8                      = sps->bit_depth - 8,
     127             : 
     128           0 :             .dependent_slice_segments_enabled_flag        = pps->dependent_slice_segments_enabled_flag,
     129           0 :             .slice_segment_header_extension_present_flag  = pps->slice_header_extension_present_flag,
     130           0 :             .sign_data_hiding_enabled_flag                = pps->sign_data_hiding_flag,
     131           0 :             .cu_qp_delta_enabled_flag                     = pps->cu_qp_delta_enabled_flag,
     132           0 :             .diff_cu_qp_delta_depth                       = pps->diff_cu_qp_delta_depth,
     133           0 :             .init_qp_minus26                              = pps->pic_init_qp_minus26,
     134           0 :             .pps_cb_qp_offset                             = pps->cb_qp_offset,
     135           0 :             .pps_cr_qp_offset                             = pps->cr_qp_offset,
     136           0 :             .constrained_intra_pred_flag                  = pps->constrained_intra_pred_flag,
     137           0 :             .weighted_pred_flag                           = pps->weighted_pred_flag,
     138           0 :             .weighted_bipred_flag                         = pps->weighted_bipred_flag,
     139           0 :             .transform_skip_enabled_flag                  = pps->transform_skip_enabled_flag,
     140           0 :             .transquant_bypass_enabled_flag               = pps->transquant_bypass_enable_flag,
     141           0 :             .entropy_coding_sync_enabled_flag             = pps->entropy_coding_sync_enabled_flag,
     142           0 :             .log2_parallel_merge_level_minus2             = pps->log2_parallel_merge_level - 2,
     143           0 :             .num_extra_slice_header_bits                  = pps->num_extra_slice_header_bits,
     144           0 :             .loop_filter_across_tiles_enabled_flag        = pps->loop_filter_across_tiles_enabled_flag,
     145           0 :             .loop_filter_across_slices_enabled_flag       = pps->seq_loop_filter_across_slices_enabled_flag,
     146           0 :             .output_flag_present_flag                     = pps->output_flag_present_flag,
     147           0 :             .num_ref_idx_l0_default_active_minus1         = pps->num_ref_idx_l0_default_active - 1,
     148           0 :             .num_ref_idx_l1_default_active_minus1         = pps->num_ref_idx_l1_default_active - 1,
     149           0 :             .lists_modification_present_flag              = pps->lists_modification_present_flag,
     150           0 :             .cabac_init_present_flag                      = pps->cabac_init_present_flag,
     151           0 :             .pps_slice_chroma_qp_offsets_present_flag     = pps->pic_slice_level_chroma_qp_offsets_present_flag,
     152           0 :             .deblocking_filter_override_enabled_flag      = pps->deblocking_filter_override_enabled_flag,
     153           0 :             .pps_deblocking_filter_disabled_flag          = pps->disable_dbf,
     154           0 :             .pps_beta_offset_div2                         = pps->beta_offset / 2,
     155           0 :             .pps_tc_offset_div2                           = pps->tc_offset / 2,
     156           0 :             .tiles_enabled_flag                           = pps->tiles_enabled_flag,
     157           0 :             .uniform_spacing_flag                         = pps->uniform_spacing_flag,
     158           0 :             .num_tile_columns_minus1                      = pps->num_tile_columns - 1,
     159           0 :             .num_tile_rows_minus1                         = pps->num_tile_rows - 1,
     160             : 
     161           0 :             .NumBitsForShortTermRPSInSlice                = s->sh.short_term_rps ? s->sh.short_term_ref_pic_set_size : 0,
     162           0 :             .NumDeltaPocsOfRefRpsIdx                      = s->sh.short_term_rps ? s->sh.short_term_rps->rps_idx_num_delta_pocs : 0,
     163           0 :             .NumPocTotalCurr                              = s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
     164           0 :                                                             s->rps[LT_CURR].nb_refs,
     165           0 :             .NumPocStCurrBefore                           = s->rps[ST_CURR_BEF].nb_refs,
     166           0 :             .NumPocStCurrAfter                            = s->rps[ST_CURR_AFT].nb_refs,
     167           0 :             .NumPocLtCurr                                 = s->rps[LT_CURR].nb_refs,
     168           0 :             .CurrPicOrderCntVal                           = s->ref->poc,
     169             :         },
     170             :     };
     171             : 
     172           0 :     if (pps->num_tile_columns > FF_ARRAY_ELEMS(ppc->column_width_minus1) ||
     173           0 :         pps->num_tile_rows    > FF_ARRAY_ELEMS(ppc->row_height_minus1)) {
     174           0 :         av_log(avctx, AV_LOG_ERROR, "Too many tiles\n");
     175           0 :         return AVERROR(ENOSYS);
     176             :     }
     177           0 :     for (i = 0; i < pps->num_tile_columns; i++)
     178           0 :         ppc->column_width_minus1[i] = pps->column_width[i] - 1;
     179           0 :     for (i = 0; i < pps->num_tile_rows; i++)
     180           0 :         ppc->row_height_minus1[i] = pps->row_height[i] - 1;
     181             : 
     182           0 :     if (s->rps[LT_CURR].nb_refs     > FF_ARRAY_ELEMS(ppc->RefPicSetLtCurr)       ||
     183           0 :         s->rps[ST_CURR_BEF].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrBefore) ||
     184           0 :         s->rps[ST_CURR_AFT].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrAfter)) {
     185           0 :         av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
     186           0 :         return AVERROR(ENOSYS);
     187             :     }
     188             : 
     189           0 :     dpb_size = 0;
     190           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
     191           0 :         const HEVCFrame *ref = &s->DPB[i];
     192           0 :         if (!(ref->flags & (HEVC_FRAME_FLAG_SHORT_REF | HEVC_FRAME_FLAG_LONG_REF)))
     193           0 :             continue;
     194           0 :         if (dpb_size >= FF_ARRAY_ELEMS(ppc->RefPicIdx)) {
     195           0 :             av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
     196           0 :             return AVERROR_INVALIDDATA;
     197             :         }
     198           0 :         dpb_add(ppc, dpb_size++, ref);
     199             : 
     200             :     }
     201           0 :     for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->RefPicIdx); i++)
     202           0 :         ppc->RefPicIdx[i] = -1;
     203             : 
     204           0 :     for (i = 0; i < s->rps[ST_CURR_BEF].nb_refs; i++) {
     205           0 :         for (j = 0; j < dpb_size; j++) {
     206           0 :             if (ppc->PicOrderCntVal[j] == s->rps[ST_CURR_BEF].list[i]) {
     207           0 :                 ppc->RefPicSetStCurrBefore[i] = j;
     208           0 :                 break;
     209             :             }
     210             :         }
     211             :     }
     212           0 :     for (i = 0; i < s->rps[ST_CURR_AFT].nb_refs; i++) {
     213           0 :         for (j = 0; j < dpb_size; j++) {
     214           0 :             if (ppc->PicOrderCntVal[j] == s->rps[ST_CURR_AFT].list[i]) {
     215           0 :                 ppc->RefPicSetStCurrAfter[i] = j;
     216           0 :                 break;
     217             :             }
     218             :         }
     219             :     }
     220           0 :     for (i = 0; i < s->rps[LT_CURR].nb_refs; i++) {
     221           0 :         for (j = 0; j < dpb_size; j++) {
     222           0 :             if (ppc->PicOrderCntVal[j] == s->rps[LT_CURR].list[i]) {
     223           0 :                 ppc->RefPicSetLtCurr[i] = j;
     224           0 :                 break;
     225             :             }
     226             :         }
     227             :     }
     228             : 
     229           0 :     fill_scaling_lists(ppc, s);
     230             : 
     231           0 :     return 0;
     232             : }
     233             : 
     234           0 : static int nvdec_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
     235             :                                    uint32_t size)
     236             : {
     237           0 :     NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
     238             :     void *tmp;
     239             : 
     240           0 :     tmp = av_fast_realloc(ctx->bitstream, &ctx->bitstream_allocated,
     241           0 :                           ctx->bitstream_len + size + 3);
     242           0 :     if (!tmp)
     243           0 :         return AVERROR(ENOMEM);
     244           0 :     ctx->bitstream = tmp;
     245             : 
     246           0 :     tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
     247           0 :                           (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
     248           0 :     if (!tmp)
     249           0 :         return AVERROR(ENOMEM);
     250           0 :     ctx->slice_offsets = tmp;
     251             : 
     252           0 :     AV_WB24(ctx->bitstream + ctx->bitstream_len, 1);
     253           0 :     memcpy(ctx->bitstream + ctx->bitstream_len + 3, buffer, size);
     254           0 :     ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
     255           0 :     ctx->bitstream_len += size + 3;
     256           0 :     ctx->nb_slices++;
     257             : 
     258           0 :     return 0;
     259             : }
     260             : 
     261           0 : static int nvdec_hevc_frame_params(AVCodecContext *avctx,
     262             :                                    AVBufferRef *hw_frames_ctx)
     263             : {
     264           0 :     const HEVCContext *s = avctx->priv_data;
     265           0 :     const HEVCSPS *sps = s->ps.sps;
     266           0 :     return ff_nvdec_frame_params(avctx, hw_frames_ctx, sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering + 1);
     267             : }
     268             : 
     269             : const AVHWAccel ff_hevc_nvdec_hwaccel = {
     270             :     .name                 = "hevc_nvdec",
     271             :     .type                 = AVMEDIA_TYPE_VIDEO,
     272             :     .id                   = AV_CODEC_ID_HEVC,
     273             :     .pix_fmt              = AV_PIX_FMT_CUDA,
     274             :     .start_frame          = nvdec_hevc_start_frame,
     275             :     .end_frame            = ff_nvdec_end_frame,
     276             :     .decode_slice         = nvdec_hevc_decode_slice,
     277             :     .frame_params         = nvdec_hevc_frame_params,
     278             :     .init                 = ff_nvdec_decode_init,
     279             :     .uninit               = ff_nvdec_decode_uninit,
     280             :     .priv_data_size       = sizeof(NVDECContext),
     281             : };

Generated by: LCOV version 1.13